com32: write_sectors() sizing corrections
[syslinux.git] / com32 / gpllib / disk / write.c
blob3c9edffdf6a72aa9bc248bbbbceeb7725c23dae0
1 /* ----------------------------------------------------------------------- *
3 * Copyright 2009 Pierre-Alexandre Meyer
5 * Some parts borrowed from chain.c32:
7 * Copyright 2003-2009 H. Peter Anvin - All Rights Reserved
8 * Copyright 2009 Intel Corporation; author: H. Peter Anvin
10 * This file is part of Syslinux, and is made available under
11 * the terms of the GNU General Public License version 2.
13 * ----------------------------------------------------------------------- */
15 #include <com32.h>
16 #include <stdlib.h>
17 #include <string.h>
19 #include <disk/common.h>
20 #include <disk/errno_disk.h>
21 #include <disk/read.h>
22 #include <disk/util.h>
23 #include <disk/write.h>
25 /**
26 * write_sectors - write several sectors from disk
27 * @drive_info: driveinfo struct describing the disk
28 * @lba: Position to write
29 * @data: Buffer to write
30 * @size: Size of the buffer (number of sectors)
32 * Return the number of sectors write on success or -1 on failure.
33 * errno_disk contains the error number.
34 **/
35 int write_sectors(const struct driveinfo *drive_info, const unsigned int lba,
36 const void *data, const int size)
38 com32sys_t inreg, outreg;
39 struct ebios_dapa *dapa;
40 void *buf;
41 int rv = -1;
43 buf = lmalloc(SECTOR * size);
44 if (!buf)
45 return -1;
47 dapa = lmalloc(sizeof(*dapa));
48 if (!dapa)
49 goto out;
51 memcpy(buf, data, SECTOR * size);
52 memset(&inreg, 0, sizeof inreg);
54 if (drive_info->ebios) {
55 dapa->len = sizeof(*dapa);
56 dapa->count = size;
57 dapa->off = OFFS(buf);
58 dapa->seg = SEG(buf);
59 dapa->lba = lba;
61 inreg.esi.w[0] = OFFS(dapa);
62 inreg.ds = SEG(dapa);
63 inreg.edx.b[0] = drive_info->disk;
64 inreg.eax.w[0] = 0x4300; /* Extended write */
65 } else {
66 unsigned int c, h, s;
68 if (!drive_info->cbios) { // XXX errno
69 /* We failed to get the geometry */
70 if (lba)
71 goto out; /* Can only write MBR */
73 s = 1;
74 h = 0;
75 c = 0;
76 } else
77 lba_to_chs(drive_info, lba, &s, &h, &c);
79 // XXX errno
80 if (s > 63 || h > 256 || c > 1023)
81 goto out;
83 inreg.eax.w[0] = 0x0301; /* Write one sector */
84 inreg.ecx.b[1] = c & 0xff;
85 inreg.ecx.b[0] = s + (c >> 6);
86 inreg.edx.b[1] = h;
87 inreg.edx.b[0] = drive_info->disk;
88 inreg.ebx.w[0] = OFFS(buf);
89 inreg.es = SEG(buf);
92 /* Perform the write */
93 if (int13_retry(&inreg, &outreg)) {
94 errno_disk = outreg.eax.b[1]; /* Give up */
95 } else
96 rv = size;
97 out:
98 lfree(dapa);
99 lfree(buf);
100 return rv;
104 * write_verify_sectors - write several sectors from disk
105 * @drive_info: driveinfo struct describing the disk
106 * @lba: Position to write
107 * @data: Buffer to write
109 int write_verify_sector(struct driveinfo *drive_info,
110 const unsigned int lba, const void *data)
112 return write_verify_sectors(drive_info, lba, data, SECTOR);
116 * write_verify_sectors - write several sectors from disk
117 * @drive_info: driveinfo struct describing the disk
118 * @lba: Position to write
119 * @data: Buffer to write
120 * @size: Size of the buffer (number of sectors)
122 int write_verify_sectors(struct driveinfo *drive_info,
123 const unsigned int lba,
124 const void *data, const int size)
126 char *rb = malloc(SECTOR * size);
127 int status;
129 if (write_sectors(drive_info, lba, data, size) == -1)
130 return -1; /* Write failure */
132 if (read_sectors(drive_info, rb, lba, size) == -1)
133 return -1; /* Readback failure */
135 status = memcmp(data, rb, SECTOR * size);
136 free(rb);
137 return status ? -1 : 0;