2
* GRUB -- GRand Unified Bootloader
3
* Copyright (C) 1999,2000,2001,2002,2003,2004,2005 Free Software Foundation, Inc.
5
* GRUB is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with GRUB; if not, write to the Free Software
17
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
#include <grub/machine/biosdisk.h>
21
#include <grub/machine/memory.h>
22
#include <grub/disk.h>
24
#include <grub/types.h>
25
#include <grub/misc.h>
27
#include <grub/term.h>
29
/* Drive Parameters. */
30
struct grub_biosdisk_drp
34
grub_uint32_t cylinders;
36
grub_uint32_t sectors;
37
grub_uint64_t total_sectors;
38
grub_uint16_t bytes_per_sector;
39
/* ver 2.0 or higher */
40
grub_uint32_t EDD_configuration_parameters;
41
/* ver 3.0 or higher */
42
grub_uint16_t signature_dpi;
43
grub_uint8_t length_dpi;
44
grub_uint8_t reserved[3];
45
grub_uint8_t name_of_host_bus[4];
46
grub_uint8_t name_of_interface_type[8];
47
grub_uint8_t interface_path[8];
48
grub_uint8_t device_path[8];
49
grub_uint8_t reserved2;
50
grub_uint8_t checksum;
52
/* XXX: This is necessary, because the BIOS of Thinkpad X20
53
writes a garbage to the tail of drive parameters,
54
regardless of a size specified in a caller. */
55
grub_uint8_t dummy[16];
56
} __attribute__ ((packed));
58
/* Disk Address Packet. */
59
struct grub_biosdisk_dap
62
grub_uint8_t reserved;
66
} __attribute__ ((packed));
70
grub_biosdisk_get_drive (const char *name)
74
if ((name[0] != 'f' && name[0] != 'h') || name[1] != 'd')
77
drive = grub_strtoul (name + 2, 0, 10);
78
if (grub_errno != GRUB_ERR_NONE)
87
grub_error (GRUB_ERR_UNKNOWN_DEVICE, "not a biosdisk");
92
grub_biosdisk_call_hook (int (*hook) (const char *name), int drive)
96
grub_sprintf (name, (drive & 0x80) ? "hd%d" : "fd%d", drive & (~0x80));
101
grub_biosdisk_iterate (int (*hook) (const char *name))
106
/* For floppy disks, we can get the number safely. */
107
num_floppies = grub_biosdisk_get_num_floppies ();
108
for (drive = 0; drive < num_floppies; drive++)
109
if (grub_biosdisk_call_hook (hook, drive))
112
/* For hard disks, attempt to read the MBR. */
113
for (drive = 0x80; drive < 0x90; drive++)
115
if (grub_biosdisk_rw_standard (0x02, drive, 0, 0, 1, 1,
116
GRUB_MEMORY_MACHINE_SCRATCH_SEG) != 0)
119
if (grub_biosdisk_call_hook (hook, drive))
127
grub_biosdisk_open (const char *name, grub_disk_t disk)
129
unsigned long total_sectors = 0;
131
struct grub_biosdisk_data *data;
133
drive = grub_biosdisk_get_drive (name);
137
disk->has_partitions = (drive & 0x80);
140
data = (struct grub_biosdisk_data *) grub_malloc (sizeof (*data));
152
version = grub_biosdisk_check_int13_extensions (drive);
155
struct grub_biosdisk_drp *drp
156
= (struct grub_biosdisk_drp *) GRUB_MEMORY_MACHINE_SCRATCH_ADDR;
158
/* Clear out the DRP. */
159
grub_memset (drp, 0, sizeof (*drp));
160
drp->size = sizeof (*drp);
161
if (!grub_biosdisk_get_diskinfo_int13_extensions (drive, drp))
163
data->flags = GRUB_BIOSDISK_FLAG_LBA;
165
/* FIXME: 2TB limit. */
166
if (drp->total_sectors)
167
total_sectors = drp->total_sectors & ~0L;
169
/* Some buggy BIOSes doesn't return the total sectors
170
correctly but returns zero. So if it is zero, compute
171
it by C/H/S returned by the LBA BIOS call. */
172
total_sectors = drp->cylinders * drp->heads * drp->sectors;
177
if (grub_biosdisk_get_diskinfo_standard (drive,
180
&data->sectors) != 0)
183
return grub_error (GRUB_ERR_BAD_DEVICE, "cannot get C/H/S values");
187
total_sectors = data->cylinders * data->heads * data->sectors;
189
disk->total_sectors = total_sectors;
192
return GRUB_ERR_NONE;
196
grub_biosdisk_close (grub_disk_t disk)
198
grub_free (disk->data);
201
/* For readability. */
202
#define GRUB_BIOSDISK_READ 0
203
#define GRUB_BIOSDISK_WRITE 1
206
grub_biosdisk_rw (int cmd, grub_disk_t disk,
207
unsigned long sector, unsigned long size,
210
struct grub_biosdisk_data *data = disk->data;
212
if (data->flags & GRUB_BIOSDISK_FLAG_LBA)
214
struct grub_biosdisk_dap *dap;
216
dap = (struct grub_biosdisk_dap *) (GRUB_MEMORY_MACHINE_SCRATCH_ADDR
218
<< GRUB_DISK_SECTOR_BITS));
219
dap->length = sizeof (*dap);
222
dap->buffer = segment << 16; /* The format SEGMENT:ADDRESS. */
225
if (grub_biosdisk_rw_int13_extensions (cmd + 0x42, data->drive, dap))
227
/* Fall back to the CHS mode. */
228
data->flags &= ~GRUB_BIOSDISK_FLAG_LBA;
229
disk->total_sectors = data->cylinders * data->heads * data->sectors;
230
return grub_biosdisk_rw (cmd, disk, sector, size, segment);
235
unsigned coff, hoff, soff;
238
soff = sector % data->sectors + 1;
239
head = sector / data->sectors;
240
hoff = head % data->heads;
241
coff = head / data->heads;
243
if (coff >= data->cylinders)
244
return grub_error (GRUB_ERR_OUT_OF_RANGE, "out of disk");
246
if (grub_biosdisk_rw_standard (cmd + 0x02, data->drive,
247
coff, hoff, soff, size, segment))
251
case GRUB_BIOSDISK_READ:
252
return grub_error (GRUB_ERR_READ_ERROR, "biosdisk read error");
253
case GRUB_BIOSDISK_WRITE:
254
return grub_error (GRUB_ERR_WRITE_ERROR, "biosdisk write error");
259
return GRUB_ERR_NONE;
263
grub_biosdisk_read (grub_disk_t disk, unsigned long sector,
264
unsigned long size, char *buf)
266
struct grub_biosdisk_data *data = disk->data;
272
len = data->sectors - (sector % data->sectors);
276
if (grub_biosdisk_rw (GRUB_BIOSDISK_READ, disk, sector, len,
277
GRUB_MEMORY_MACHINE_SCRATCH_SEG))
280
grub_memcpy (buf, (void *) GRUB_MEMORY_MACHINE_SCRATCH_ADDR,
281
len << GRUB_DISK_SECTOR_BITS);
282
buf += len << GRUB_DISK_SECTOR_BITS;
291
grub_biosdisk_write (grub_disk_t disk, unsigned long sector,
292
unsigned long size, const char *buf)
294
struct grub_biosdisk_data *data = disk->data;
300
len = data->sectors - (sector % data->sectors);
304
grub_memcpy ((void *) GRUB_MEMORY_MACHINE_SCRATCH_ADDR, buf,
305
len << GRUB_DISK_SECTOR_BITS);
307
if (grub_biosdisk_rw (GRUB_BIOSDISK_WRITE, disk, sector, len,
308
GRUB_MEMORY_MACHINE_SCRATCH_SEG))
311
buf += len << GRUB_DISK_SECTOR_BITS;
319
static struct grub_disk_dev grub_biosdisk_dev =
322
.id = GRUB_DISK_DEVICE_BIOSDISK_ID,
323
.iterate = grub_biosdisk_iterate,
324
.open = grub_biosdisk_open,
325
.close = grub_biosdisk_close,
326
.read = grub_biosdisk_read,
327
.write = grub_biosdisk_write,
332
grub_biosdisk_init (void)
334
grub_disk_dev_register (&grub_biosdisk_dev);
338
grub_biosdisk_fini (void)
340
grub_disk_dev_unregister (&grub_biosdisk_dev);