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
;
47 dapa
= lmalloc(sizeof(*dapa
));
51 memcpy(buf
, data
, size
);
52 memset(&inreg
, 0, sizeof inreg
);
54 if (drive_info
->ebios
) {
55 dapa
->len
= sizeof(*dapa
);
57 dapa
->off
= OFFS(buf
);
61 inreg
.esi
.w
[0] = OFFS(dapa
);
63 inreg
.edx
.b
[0] = drive_info
->disk
;
64 inreg
.eax
.w
[0] = 0x4300; /* Extended write */
68 if (!drive_info
->cbios
) { // XXX errno
69 /* We failed to get the geometry */
71 goto out
; /* Can only write MBR */
77 lba_to_chs(drive_info
, lba
, &s
, &h
, &c
);
80 if (s
> 63 || h
> 256 || c
> 1023)
83 inreg
.eax
.w
[0] = 0x0301; /* Write one sector */
84 inreg
.ecx
.b
[1] = c
& 0xff;
85 inreg
.ecx
.b
[0] = s
+ (c
>> 6);
87 inreg
.edx
.b
[0] = drive_info
->disk
;
88 inreg
.ebx
.w
[0] = OFFS(buf
);
92 /* Perform the write */
93 if (int13_retry(&inreg
, &outreg
)) {
94 errno_disk
= outreg
.eax
.b
[1]; /* Give up */
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
* sizeof(char));
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
);
137 return status
? -1 : 0;