Debian Codegraph @

grub-core/disk/i386/pc/biosdisk.c

raw
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
4 *
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 3 of the License, or
8 * (at your option) any later version.
9 *
10 * GRUB 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.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <grub/machine/biosdisk.h>
20 #include <grub/machine/kernel.h>
21 #include <grub/machine/memory.h>
22 #include <grub/machine/int.h>
23 #include <grub/disk.h>
24 #include <grub/dl.h>
25 #include <grub/mm.h>
26 #include <grub/types.h>
27 #include <grub/misc.h>
28 #include <grub/err.h>
29 #include <grub/term.h>
30 #include <grub/i18n.h>
31
32 GRUB_MOD_LICENSE ("GPLv3+");
33
34 static int cd_drive = 0;
35 static int grub_biosdisk_rw_int13_extensions (int ah, int drive, void *dap);
36
37 static int grub_biosdisk_get_num_floppies (void)
38 {
39 struct grub_bios_int_registers regs;
40 int drive;
41
42 /* reset the disk system first */
43 regs.eax = 0;
44 regs.edx = 0;
45 regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
46
47 grub_bios_interrupt (0x13, &regs);
48
49 for (drive = 0; drive < 2; drive++)
50 {
51 regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT | GRUB_CPU_INT_FLAGS_CARRY;
52 regs.edx = drive;
53
54 /* call GET DISK TYPE */
55 regs.eax = 0x1500;
56 grub_bios_interrupt (0x13, &regs);
57 if (regs.flags & GRUB_CPU_INT_FLAGS_CARRY)
58 break;
59
60 /* check if this drive exists */
61 if (!(regs.eax & 0x300))
62 break;
63 }
64
65 return drive;
66 }
67
68 /*
69 * Call IBM/MS INT13 Extensions (int 13 %ah=AH) for DRIVE. DAP
70 * is passed for disk address packet. If an error occurs, return
71 * non-zero, otherwise zero.
72 */
73
74 static int
75 grub_biosdisk_rw_int13_extensions (int ah, int drive, void *dap)
76 {
77 struct grub_bios_int_registers regs;
78 regs.eax = ah << 8;
79 /* compute the address of disk_address_packet */
80 regs.ds = (((grub_addr_t) dap) & 0xffff0000) >> 4;
81 regs.esi = (((grub_addr_t) dap) & 0xffff);
82 regs.edx = drive;
83 regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
84
85 grub_bios_interrupt (0x13, &regs);
86 return (regs.eax >> 8) & 0xff;
87 }
88
89 /*
90 * Call standard and old INT13 (int 13 %ah=AH) for DRIVE. Read/write
91 * NSEC sectors from COFF/HOFF/SOFF into SEGMENT. If an error occurs,
92 * return non-zero, otherwise zero.
93 */
94 static int
95 grub_biosdisk_rw_standard (int ah, int drive, int coff, int hoff,
96 int soff, int nsec, int segment)
97 {
98 int ret, i;
99
100 /* Try 3 times. */
101 for (i = 0; i < 3; i++)
102 {
103 struct grub_bios_int_registers regs;
104
105 /* set up CHS information */
106 /* set %ch to low eight bits of cylinder */
107 regs.ecx = (coff << 8) & 0xff00;
108 /* set bits 6-7 of %cl to high two bits of cylinder */
109 regs.ecx |= (coff >> 2) & 0xc0;
110 /* set bits 0-5 of %cl to sector */
111 regs.ecx |= soff & 0x3f;
112
113 /* set %dh to head and %dl to drive */
114 regs.edx = (drive & 0xff) | ((hoff << 8) & 0xff00);
115 /* set %ah to AH */
116 regs.eax = (ah << 8) & 0xff00;
117 /* set %al to NSEC */
118 regs.eax |= nsec & 0xff;
119
120 regs.ebx = 0;
121 regs.es = segment;
122
123 regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
124
125 grub_bios_interrupt (0x13, &regs);
126 /* check if successful */
127 if (!(regs.flags & GRUB_CPU_INT_FLAGS_CARRY))
128 return 0;
129
130 /* save return value */
131 ret = regs.eax >> 8;
132
133 /* if fail, reset the disk system */
134 regs.eax = 0;
135 regs.edx = (drive & 0xff);
136 regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
137 grub_bios_interrupt (0x13, &regs);
138 }
139 return ret;
140 }
141
142 /*
143 * Check if LBA is supported for DRIVE. If it is supported, then return
144 * the major version of extensions, otherwise zero.
145 */
146 static int
147 grub_biosdisk_check_int13_extensions (int drive)
148 {
149 struct grub_bios_int_registers regs;
150
151 regs.edx = drive & 0xff;
152 regs.eax = 0x4100;
153 regs.ebx = 0x55aa;
154 regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
155 grub_bios_interrupt (0x13, &regs);
156
157 if (regs.flags & GRUB_CPU_INT_FLAGS_CARRY)
158 return 0;
159
160 if ((regs.ebx & 0xffff) != 0xaa55)
161 return 0;
162
163 /* check if AH=0x42 is supported */
164 if (!(regs.ecx & 1))
165 return 0;
166
167 return (regs.eax >> 8) & 0xff;
168 }
169
170 /*
171 * Return the geometry of DRIVE in CYLINDERS, HEADS and SECTORS. If an
172 * error occurs, then return non-zero, otherwise zero.
173 */
174 static int
175 grub_biosdisk_get_diskinfo_standard (int drive,
176 unsigned long *cylinders,
177 unsigned long *heads,
178 unsigned long *sectors)
179 {
180 struct grub_bios_int_registers regs;
181
182 regs.eax = 0x0800;
183 regs.edx = drive & 0xff;
184
185 regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
186 grub_bios_interrupt (0x13, &regs);
187
188 /* Check if unsuccessful. Ignore return value if carry isn't set to
189 workaround some buggy BIOSes. */
190 if ((regs.flags & GRUB_CPU_INT_FLAGS_CARRY) && ((regs.eax & 0xff00) != 0))
191 return (regs.eax & 0xff00) >> 8;
192
193 /* bogus BIOSes may not return an error number */
194 /* 0 sectors means no disk */
195 if (!(regs.ecx & 0x3f))
196 /* XXX 0x60 is one of the unused error numbers */
197 return 0x60;
198
199 /* the number of heads is counted from zero */
200 *heads = ((regs.edx >> 8) & 0xff) + 1;
201 *cylinders = (((regs.ecx >> 8) & 0xff) | ((regs.ecx << 2) & 0x0300)) + 1;
202 *sectors = regs.ecx & 0x3f;
203 return 0;
204 }
205
206 static int
207 grub_biosdisk_get_diskinfo_real (int drive, void *drp, grub_uint16_t ax)
208 {
209 struct grub_bios_int_registers regs;
210
211 regs.eax = ax;
212
213 /* compute the address of drive parameters */
214 regs.esi = ((grub_addr_t) drp) & 0xf;
215 regs.ds = ((grub_addr_t) drp) >> 4;
216 regs.edx = drive & 0xff;
217
218 regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
219 grub_bios_interrupt (0x13, &regs);
220
221 /* Check if unsuccessful. Ignore return value if carry isn't set to
222 workaround some buggy BIOSes. */
223 if ((regs.flags & GRUB_CPU_INT_FLAGS_CARRY) && ((regs.eax & 0xff00) != 0))
224 return (regs.eax & 0xff00) >> 8;
225
226 return 0;
227 }
228
229 /*
230 * Return the cdrom information of DRIVE in CDRP. If an error occurs,
231 * then return non-zero, otherwise zero.
232 */
233 static int
234 grub_biosdisk_get_cdinfo_int13_extensions (int drive, void *cdrp)
235 {
236 return grub_biosdisk_get_diskinfo_real (drive, cdrp, 0x4b01);
237 }
238
239 /*
240 * Return the geometry of DRIVE in a drive parameters, DRP. If an error
241 * occurs, then return non-zero, otherwise zero.
242 */
243 static int
244 grub_biosdisk_get_diskinfo_int13_extensions (int drive, void *drp)
245 {
246 return grub_biosdisk_get_diskinfo_real (drive, drp, 0x4800);
247 }
248
249 static int
250 grub_biosdisk_get_drive (const char *name)
251 {
252 unsigned long drive;
253
254 if (name[0] == 'c' && name[1] == 'd' && name[2] == 0 && cd_drive)
255 return cd_drive;
256
257 if ((name[0] != 'f' && name[0] != 'h') || name[1] != 'd')
258 goto fail;
259
260 drive = grub_strtoul (name + 2, 0, 10);
261 if (grub_errno != GRUB_ERR_NONE)
262 goto fail;
263
264 if (name[0] == 'h')
265 drive += 0x80;
266
267 return (int) drive ;
268
269 fail:
270 grub_error (GRUB_ERR_UNKNOWN_DEVICE, "not a biosdisk");
271 return -1;
272 }
273
274 static int
275 grub_biosdisk_call_hook (grub_disk_dev_iterate_hook_t hook, void *hook_data,
276 int drive)
277 {
278 char name[10];
279
280 if (cd_drive && drive == cd_drive)
281 return hook ("cd", hook_data);
282
283 grub_snprintf (name, sizeof (name),
284 (drive & 0x80) ? "hd%d" : "fd%d", drive & (~0x80));
285 return hook (name, hook_data);
286 }
287
288 static int
289 grub_biosdisk_iterate (grub_disk_dev_iterate_hook_t hook, void *hook_data,
290 grub_disk_pull_t pull)
291 {
292 int num_floppies;
293 int drive;
294
295 /* For hard disks, attempt to read the MBR. */
296 switch (pull)
297 {
298 case GRUB_DISK_PULL_NONE:
299 for (drive = 0x80; drive < 0x90; drive++)
300 {
301 if (grub_biosdisk_rw_standard (0x02, drive, 0, 0, 1, 1,
302 GRUB_MEMORY_MACHINE_SCRATCH_SEG) != 0)
303 {
304 grub_dprintf ("disk", "Read error when probing drive 0x%2x\n", drive);
305 break;
306 }
307
308 if (grub_biosdisk_call_hook (hook, hook_data, drive))
309 return 1;
310 }
311 return 0;
312
313 case GRUB_DISK_PULL_REMOVABLE:
314 if (cd_drive)
315 {
316 if (grub_biosdisk_call_hook (hook, hook_data, cd_drive))
317 return 1;
318 }
319
320 /* For floppy disks, we can get the number safely. */
321 num_floppies = grub_biosdisk_get_num_floppies ();
322 for (drive = 0; drive < num_floppies; drive++)
323 if (grub_biosdisk_call_hook (hook, hook_data, drive))
324 return 1;
325 return 0;
326 default:
327 return 0;
328 }
329 return 0;
330 }
331
332 static grub_err_t
333 grub_biosdisk_open (const char *name, grub_disk_t disk)
334 {
335 grub_uint64_t total_sectors = 0;
336 int drive;
337 struct grub_biosdisk_data *data;
338
339 drive = grub_biosdisk_get_drive (name);
340 if (drive < 0)
341 return grub_errno;
342
343 disk->id = drive;
344
345 data = (struct grub_biosdisk_data *) grub_zalloc (sizeof (*data));
346 if (! data)
347 return grub_errno;
348
349 data->drive = drive;
350
351 if ((cd_drive) && (drive == cd_drive))
352 {
353 data->flags = GRUB_BIOSDISK_FLAG_LBA | GRUB_BIOSDISK_FLAG_CDROM;
354 data->sectors = 8;
355 disk->log_sector_size = 11;
356 /* TODO: get the correct size. */
357 total_sectors = GRUB_DISK_SIZE_UNKNOWN;
358 }
359 else
360 {
361 /* HDD */
362 int version;
363
364 disk->log_sector_size = 9;
365
366 version = grub_biosdisk_check_int13_extensions (drive);
367 if (version)
368 {
369 struct grub_biosdisk_drp *drp
370 = (struct grub_biosdisk_drp *) grub_absolute_pointer (GRUB_MEMORY_MACHINE_SCRATCH_ADDR);
371
372 /* Clear out the DRP. */
373 grub_memset (drp, 0, sizeof (*drp));
374 drp->size = sizeof (*drp);
375 if (! grub_biosdisk_get_diskinfo_int13_extensions (drive, drp))
376 {
377 data->flags = GRUB_BIOSDISK_FLAG_LBA;
378
379 if (drp->total_sectors)
380 total_sectors = drp->total_sectors;
381 else
382 /* Some buggy BIOSes doesn't return the total sectors
383 correctly but returns zero. So if it is zero, compute
384 it by C/H/S returned by the LBA BIOS call. */
385 total_sectors = ((grub_uint64_t) drp->cylinders)
386 * drp->heads * drp->sectors;
387 if (drp->bytes_per_sector
388 && !(drp->bytes_per_sector & (drp->bytes_per_sector - 1))
389 && drp->bytes_per_sector >= 512
390 && drp->bytes_per_sector <= 16384)
391 disk->log_sector_size = grub_log2ull (drp->bytes_per_sector);
392 }
393 }
394 }
395
396 if (! (data->flags & GRUB_BIOSDISK_FLAG_CDROM))
397 {
398 if (grub_biosdisk_get_diskinfo_standard (drive,
399 &data->cylinders,
400 &data->heads,
401 &data->sectors) != 0)
402 {
403 if (total_sectors && (data->flags & GRUB_BIOSDISK_FLAG_LBA))
404 {
405 data->sectors = 63;
406 data->heads = 255;
407 data->cylinders
408 = grub_divmod64 (total_sectors
409 + data->heads * data->sectors - 1,
410 data->heads * data->sectors, 0);
411 }
412 else
413 {
414 grub_free (data);
415 return grub_error (GRUB_ERR_BAD_DEVICE, "%s cannot get C/H/S values", disk->name);
416 }
417 }
418
419 if (data->sectors == 0)
420 data->sectors = 63;
421 if (data->heads == 0)
422 data->heads = 255;
423
424 if (! total_sectors)
425 total_sectors = ((grub_uint64_t) data->cylinders)
426 * data->heads * data->sectors;
427 }
428
429 disk->total_sectors = total_sectors;
430 /* Limit the max to 0x7f because of Phoenix EDD. */
431 disk->max_agglomerate = 0x7f >> GRUB_DISK_CACHE_BITS;
432 COMPILE_TIME_ASSERT ((0x7f >> GRUB_DISK_CACHE_BITS
433 << (GRUB_DISK_SECTOR_BITS + GRUB_DISK_CACHE_BITS))
434 + sizeof (struct grub_biosdisk_dap)
435 < GRUB_MEMORY_MACHINE_SCRATCH_SIZE);
436
437 disk->data = data;
438
439 return GRUB_ERR_NONE;
440 }
441
442 static void
443 grub_biosdisk_close (grub_disk_t disk)
444 {
445 grub_free (disk->data);
446 }
447
448 /* For readability. */
449 #define GRUB_BIOSDISK_READ 0
450 #define GRUB_BIOSDISK_WRITE 1
451
452 #define GRUB_BIOSDISK_CDROM_RETRY_COUNT 3
453
454 static grub_err_t
455 grub_biosdisk_rw (int cmd, grub_disk_t disk,
456 grub_disk_addr_t sector, grub_size_t size,
457 unsigned segment)
458 {
459 struct grub_biosdisk_data *data = disk->data;
460
461 /* VirtualBox fails with sectors above 2T on CDs.
462 Since even BD-ROMS are never that big anyway, return error. */
463 if ((data->flags & GRUB_BIOSDISK_FLAG_CDROM)
464 && (sector >> 32))
465 return grub_error (GRUB_ERR_OUT_OF_RANGE,
466 N_("attempt to read or write outside of disk `%s'"),
467 disk->name);
468
469 if (data->flags & GRUB_BIOSDISK_FLAG_LBA)
470 {
471 struct grub_biosdisk_dap *dap;
472
473 dap = (struct grub_biosdisk_dap *) (GRUB_MEMORY_MACHINE_SCRATCH_ADDR
474 + (GRUB_DISK_MAX_LBA_SECTORS
475 << disk->log_sector_size));
476 dap->length = sizeof (*dap);
477 dap->reserved = 0;
478 dap->blocks = size;
479 dap->buffer = segment << 16; /* The format SEGMENT:ADDRESS. */
480 dap->block = sector;
481
482 if (data->flags & GRUB_BIOSDISK_FLAG_CDROM)
483 {
484 int i;
485
486 if (cmd)
487 return grub_error (GRUB_ERR_WRITE_ERROR, N_("cannot write to CD-ROM"));
488
489 for (i = 0; i < GRUB_BIOSDISK_CDROM_RETRY_COUNT; i++)
490 if (! grub_biosdisk_rw_int13_extensions (0x42, data->drive, dap))
491 break;
492
493 if (i == GRUB_BIOSDISK_CDROM_RETRY_COUNT)
494 return grub_error (GRUB_ERR_READ_ERROR, N_("failure reading sector 0x%llx "
495 "from `%s'"),
496 (unsigned long long) sector,
497 disk->name);
498 }
499 else
500 if (grub_biosdisk_rw_int13_extensions (cmd + 0x42, data->drive, dap))
501 {
502 /* Fall back to the CHS mode. */
503 data->flags &= ~GRUB_BIOSDISK_FLAG_LBA;
504 disk->total_sectors = data->cylinders * data->heads * data->sectors;
505 return grub_biosdisk_rw (cmd, disk, sector, size, segment);
506 }
507 }
508 else
509 {
510 unsigned coff, hoff, soff;
511 unsigned head;
512
513 /* It is impossible to reach over 8064 MiB (a bit less than LBA24) with
514 the traditional CHS access. */
515 if (sector >
516 1024 /* cylinders */ *
517 256 /* heads */ *
518 63 /* spt */)
519 return grub_error (GRUB_ERR_OUT_OF_RANGE,
520 N_("attempt to read or write outside of disk `%s'"),
521 disk->name);
522
523 soff = ((grub_uint32_t) sector) % data->sectors + 1;
524 head = ((grub_uint32_t) sector) / data->sectors;
525 hoff = head % data->heads;
526 coff = head / data->heads;
527
528 if (coff >= data->cylinders)
529 return grub_error (GRUB_ERR_OUT_OF_RANGE,
530 N_("attempt to read or write outside of disk `%s'"),
531 disk->name);
532
533 if (grub_biosdisk_rw_standard (cmd + 0x02, data->drive,
534 coff, hoff, soff, size, segment))
535 {
536 switch (cmd)
537 {
538 case GRUB_BIOSDISK_READ:
539 return grub_error (GRUB_ERR_READ_ERROR, N_("failure reading sector 0x%llx "
540 "from `%s'"),
541 (unsigned long long) sector,
542 disk->name);
543 case GRUB_BIOSDISK_WRITE:
544 return grub_error (GRUB_ERR_WRITE_ERROR, N_("failure writing sector 0x%llx "
545 "to `%s'"),
546 (unsigned long long) sector,
547 disk->name);
548 }
549 }
550 }
551
552 return GRUB_ERR_NONE;
553 }
554
555 /* Return the number of sectors which can be read safely at a time. */
556 static grub_size_t
557 get_safe_sectors (grub_disk_t disk, grub_disk_addr_t sector)
558 {
559 grub_size_t size;
560 grub_uint64_t offset;
561 struct grub_biosdisk_data *data = disk->data;
562 grub_uint32_t sectors = data->sectors;
563
564 if (data->flags & GRUB_BIOSDISK_FLAG_LBA)
565 sectors = GRUB_DISK_MAX_LBA_SECTORS;
566
567 /* OFFSET = SECTOR % SECTORS */
568 grub_divmod64 (sector, sectors, &offset);
569
570 size = sectors - offset;
571
572 return size;
573 }
574
575 static grub_err_t
576 grub_biosdisk_read (grub_disk_t disk, grub_disk_addr_t sector,
577 grub_size_t size, char *buf)
578 {
579 while (size)
580 {
581 grub_size_t len;
582
583 len = get_safe_sectors (disk, sector);
584
585 if (len > size)
586 len = size;
587
588 if (grub_biosdisk_rw (GRUB_BIOSDISK_READ, disk, sector, len,
589 GRUB_MEMORY_MACHINE_SCRATCH_SEG))
590 return grub_errno;
591
592 grub_memcpy (buf, (void *) GRUB_MEMORY_MACHINE_SCRATCH_ADDR,
593 len << disk->log_sector_size);
594
595 buf += len << disk->log_sector_size;
596 sector += len;
597 size -= len;
598 }
599
600 return grub_errno;
601 }
602
603 static grub_err_t
604 grub_biosdisk_write (grub_disk_t disk, grub_disk_addr_t sector,
605 grub_size_t size, const char *buf)
606 {
607 struct grub_biosdisk_data *data = disk->data;
608
609 if (data->flags & GRUB_BIOSDISK_FLAG_CDROM)
610 return grub_error (GRUB_ERR_IO, N_("cannot write to CD-ROM"));
611
612 while (size)
613 {
614 grub_size_t len;
615
616 len = get_safe_sectors (disk, sector);
617 if (len > size)
618 len = size;
619
620 grub_memcpy ((void *) GRUB_MEMORY_MACHINE_SCRATCH_ADDR, buf,
621 len << disk->log_sector_size);
622
623 if (grub_biosdisk_rw (GRUB_BIOSDISK_WRITE, disk, sector, len,
624 GRUB_MEMORY_MACHINE_SCRATCH_SEG))
625 return grub_errno;
626
627 buf += len << disk->log_sector_size;
628 sector += len;
629 size -= len;
630 }
631
632 return grub_errno;
633 }
634
635 static struct grub_disk_dev grub_biosdisk_dev =
636 {
637 .name = "biosdisk",
638 .id = GRUB_DISK_DEVICE_BIOSDISK_ID,
639 .disk_iterate = grub_biosdisk_iterate,
640 .disk_open = grub_biosdisk_open,
641 .disk_close = grub_biosdisk_close,
642 .disk_read = grub_biosdisk_read,
643 .disk_write = grub_biosdisk_write,
644 .next = 0
645 };
646
647 static void
648 grub_disk_biosdisk_fini (void)
649 {
650 grub_disk_dev_unregister (&grub_biosdisk_dev);
651 }
652
653 GRUB_MOD_INIT(biosdisk)
654 {
655 struct grub_biosdisk_cdrp *cdrp
656 = (struct grub_biosdisk_cdrp *) grub_absolute_pointer (GRUB_MEMORY_MACHINE_SCRATCH_ADDR);
657 grub_uint8_t boot_drive;
658
659 if (grub_disk_firmware_is_tainted)
660 {
661 grub_puts_ (N_("Native disk drivers are in use. "
662 "Refusing to use firmware disk interface."));
663 return;
664 }
665 grub_disk_firmware_fini = grub_disk_biosdisk_fini;
666
667 grub_memset (cdrp, 0, sizeof (*cdrp));
668 cdrp->size = sizeof (*cdrp);
669 cdrp->media_type = 0xFF;
670 boot_drive = (grub_boot_device >> 24);
671 if ((! grub_biosdisk_get_cdinfo_int13_extensions (boot_drive, cdrp))
672 && ((cdrp->media_type & GRUB_BIOSDISK_CDTYPE_MASK)
673 == GRUB_BIOSDISK_CDTYPE_NO_EMUL))
674 cd_drive = cdrp->drive_no;
675 /* Since diskboot.S rejects devices over 0x90 it must be a CD booted with
676 cdboot.S
677 */
678 if (boot_drive >= 0x90)
679 cd_drive = boot_drive;
680
681 grub_disk_dev_register (&grub_biosdisk_dev);
682 }
683
684 GRUB_MOD_FINI(biosdisk)
685 {
686 grub_disk_biosdisk_fini ();
687 }