Releasing debian version 4.06+dfsg-2.
[syslinux-debian/hramrach.git] / com32 / gpllib / disk / write.c
blob89e530d90d7f5c652b809b58fb43cfe82cbf0430
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 = __com32.cs_bounce;
40 void *buf = (char *)__com32.cs_bounce + size;
42 memcpy(buf, data, size);
43 memset(&inreg, 0, sizeof inreg);
45 if (drive_info->ebios) {
46 dapa->len = sizeof(*dapa);
47 dapa->count = size;
48 dapa->off = OFFS(buf);
49 dapa->seg = SEG(buf);
50 dapa->lba = lba;
52 inreg.esi.w[0] = OFFS(dapa);
53 inreg.ds = SEG(dapa);
54 inreg.edx.b[0] = drive_info->disk;
55 inreg.eax.w[0] = 0x4300; /* Extended write */
56 } else {
57 unsigned int c, h, s;
59 if (!drive_info->cbios) { // XXX errno
60 /* We failed to get the geometry */
61 if (lba)
62 return -1; /* Can only write MBR */
64 s = 1;
65 h = 0;
66 c = 0;
67 } else
68 lba_to_chs(drive_info, lba, &s, &h, &c);
70 // XXX errno
71 if (s > 63 || h > 256 || c > 1023)
72 return -1;
74 inreg.eax.w[0] = 0x0301; /* Write one sector */
75 inreg.ecx.b[1] = c & 0xff;
76 inreg.ecx.b[0] = s + (c >> 6);
77 inreg.edx.b[1] = h;
78 inreg.edx.b[0] = drive_info->disk;
79 inreg.ebx.w[0] = OFFS(buf);
80 inreg.es = SEG(buf);
83 /* Perform the write */
84 if (int13_retry(&inreg, &outreg)) {
85 errno_disk = outreg.eax.b[1];
86 return -1; /* Give up */
87 } else
88 return size;
91 /**
92 * write_verify_sectors - write several sectors from disk
93 * @drive_info: driveinfo struct describing the disk
94 * @lba: Position to write
95 * @data: Buffer to write
96 **/
97 int write_verify_sector(struct driveinfo *drive_info,
98 const unsigned int lba, const void *data)
100 return write_verify_sectors(drive_info, lba, data, SECTOR);
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
108 * @size: Size of the buffer (number of sectors)
110 int write_verify_sectors(struct driveinfo *drive_info,
111 const unsigned int lba,
112 const void *data, const int size)
114 char *rb = malloc(SECTOR * size * sizeof(char));
115 int status;
117 if (write_sectors(drive_info, lba, data, size) == -1)
118 return -1; /* Write failure */
120 if (read_sectors(drive_info, rb, lba, size) == -1)
121 return -1; /* Readback failure */
123 status = memcmp(data, rb, SECTOR * size);
124 free(rb);
125 return status ? -1 : 0;