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 * ----------------------------------------------------------------------- */
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>
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.
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
);
48 dapa
->off
= OFFS(buf
);
52 inreg
.esi
.w
[0] = OFFS(dapa
);
54 inreg
.edx
.b
[0] = drive_info
->disk
;
55 inreg
.eax
.w
[0] = 0x4300; /* Extended write */
59 if (!drive_info
->cbios
) { // XXX errno
60 /* We failed to get the geometry */
62 return -1; /* Can only write MBR */
68 lba_to_chs(drive_info
, lba
, &s
, &h
, &c
);
71 if (s
> 63 || h
> 256 || c
> 1023)
74 inreg
.eax
.w
[0] = 0x0301; /* Write one sector */
75 inreg
.ecx
.b
[1] = c
& 0xff;
76 inreg
.ecx
.b
[0] = s
+ (c
>> 6);
78 inreg
.edx
.b
[0] = drive_info
->disk
;
79 inreg
.ebx
.w
[0] = OFFS(buf
);
83 /* Perform the write */
84 if (int13_retry(&inreg
, &outreg
)) {
85 errno_disk
= outreg
.eax
.b
[1];
86 return -1; /* Give up */
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
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));
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
);
125 return status
? -1 : 0;