2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $FreeBSD: src/sys/boot/i386/libi386/biosdisk.c,v 1.45 2004/09/21 06:46:44 wes Exp $
30 * BIOS disk device handling.
32 * Ideas and algorithms from:
34 * - NetBSD libi386/biosdisk.c
35 * - FreeBSD biosboot/disk.c
41 #include <sys/disklabel32.h>
42 #include <sys/disklabel64.h>
43 #include <sys/diskmbr.h>
44 #include <sys/dtype.h>
45 #include <machine/bootinfo.h>
46 #include <machine/psl.h>
50 #include <bootstrap.h>
54 #define BIOS_NUMDRIVES 0x475
55 #define BIOSDISK_SECSIZE 512
56 #define BUFSIZE (4 * BIOSDISK_SECSIZE)
57 #define MAXBDDEV MAXDEV
59 #define DT_ATAPI 0x10 /* disk type for ATAPI floppies */
60 #define WDMAJOR 0 /* major numbers for devices we frontend for */
66 # define DEBUG(fmt, args...) printf("%s: " fmt "\n" , __func__ , ## args)
68 # define DEBUG(fmt, args...)
72 int od_dkunit; /* disk unit number */
73 int od_unit; /* BIOS unit number */
74 int od_cyl; /* BIOS geometry */
77 int od_boff; /* block offset from beginning of BIOS disk */
79 #define BD_MODEINT13 0x0000
80 #define BD_MODEEDD1 0x0001
81 #define BD_MODEEDD3 0x0002
82 #define BD_MODEMASK 0x0003
83 #define BD_FLOPPY 0x0004
84 #define BD_LABELOK 0x0008
85 #define BD_PARTTABOK 0x0010
87 struct disklabel32 od_disklabel;
88 struct disklabel64 od_disklabel64;
90 int od_nslices; /* slice count */
91 struct dos_partition od_slicetab[NEXTDOSPART];
95 * List of BIOS devices, translation from disk unit number to
100 int bd_unit; /* BIOS unit number */
102 int bd_type; /* BIOS 'drive type' (floppy only) */
104 static int nbdinfo = 0;
106 static int bd_getgeom(struct open_disk *od);
107 static int bd_read(struct open_disk *od, daddr_t dblk, int blks,
109 static int bd_write(struct open_disk *od, daddr_t dblk, int blks,
112 static int bd_int13probe(struct bdinfo *bd);
114 static void bd_printslice(struct open_disk *od, struct dos_partition *dp,
115 char *prefix, int verbose);
116 static void bd_printbsdslice(struct open_disk *od, daddr_t offset,
117 char *prefix, int verbose);
119 static int bd_init(void);
120 static int bd_strategy(void *devdata, int flag, daddr_t dblk,
121 size_t size, char *buf, size_t *rsize);
122 static int bd_realstrategy(void *devdata, int flag, daddr_t dblk,
123 size_t size, char *buf, size_t *rsize);
124 static int bd_open(struct open_file *f, ...);
125 static int bd_close(struct open_file *f);
126 static void bd_print(int verbose);
128 struct devsw biosdisk = {
140 static int bd_opendisk(struct open_disk **odp, struct i386_devdesc *dev);
141 static void bd_closedisk(struct open_disk *od);
142 static int bd_bestslice(struct open_disk *od);
143 static void bd_chainextended(struct open_disk *od, u_int32_t base,
146 char bounce_base[BOUNCEBUF_SIZE]; /* also used by CD code */
150 * Translate between BIOS device numbers and our private unit numbers.
153 bd_bios2unit(int biosdev)
157 DEBUG("looking for bios device 0x%x", biosdev);
158 for (i = 0; i < nbdinfo; i++) {
159 DEBUG("bd unit %d is BIOS device 0x%x", i, bdinfo[i].bd_unit);
160 if (bdinfo[i].bd_unit == biosdev)
167 bd_unit2bios(int unit)
169 if ((unit >= 0) && (unit < nbdinfo))
170 return(bdinfo[unit].bd_unit);
175 * Quiz the BIOS for disk devices, save a little info about them.
180 int base, unit, nfd = 0;
182 /* sequence 0, 0x80 */
183 for (base = 0; base <= 0x80; base += 0x80) {
184 for (unit = base; (nbdinfo < MAXBDDEV); unit++) {
185 /* check the BIOS equipment list for number of fixed disks */
187 (nfd >= *(unsigned char *)PTOV(BIOS_NUMDRIVES)))
190 bdinfo[nbdinfo].bd_unit = unit;
191 bdinfo[nbdinfo].bd_flags = (unit < 0x80) ? BD_FLOPPY : 0;
193 if (!bd_int13probe(&bdinfo[nbdinfo]))
196 /* XXX we need "disk aliases" to make this simpler */
197 printf("BIOS drive %c: is disk%d\n",
198 (unit < 0x80) ? ('A' + unit) : ('C' + unit - 0x80), nbdinfo);
208 * Try to detect a device supported by the legacy int13 BIOS
211 bd_int13probe(struct bdinfo *bd)
216 v86.edx = bd->bd_unit;
219 if (!(v86.efl & PSL_C) && /* carry clear */
220 ((v86.edx & 0xff) > ((unsigned)bd->bd_unit & 0x7f))) { /* unit # OK */
223 * Ignore devices with an absurd sector size.
225 if ((v86.ecx & 0x3f) == 0) {
226 DEBUG("Invalid geometry for unit %d", bd->bd_unit);
229 bd->bd_flags |= BD_MODEINT13;
230 bd->bd_type = v86.ebx & 0xff;
232 /* Determine if we can use EDD with this device. */
236 v86.edx = bd->bd_unit;
239 if (!(v86.efl & PSL_C) && /* carry clear */
240 ((v86.ebx & 0xffff) == 0xaa55) && /* signature */
241 (v86.ecx & 0x1)) { /* packets mode ok */
242 bd->bd_flags |= BD_MODEEDD1;
243 if((v86.eax & 0xff00) > 0x300)
244 bd->bd_flags |= BD_MODEEDD3;
252 * Print information about disks
255 bd_print(int verbose)
259 struct i386_devdesc dev;
260 struct open_disk *od;
261 struct dos_partition *dptr;
263 for (i = 0; i < nbdinfo; i++) {
264 sprintf(line, " disk%d: BIOS drive %c:\n", i,
265 (bdinfo[i].bd_unit < 0x80) ? ('A' + bdinfo[i].bd_unit) : ('C' + bdinfo[i].bd_unit - 0x80));
268 /* try to open the whole disk */
269 dev.d_kind.biosdisk.unit = i;
270 dev.d_kind.biosdisk.slice = -1;
271 dev.d_kind.biosdisk.partition = -1;
273 if (!bd_opendisk(&od, &dev)) {
275 /* Do we have a partition table? */
276 if (od->od_flags & BD_PARTTABOK) {
277 dptr = &od->od_slicetab[0];
279 /* Check for a "dedicated" disk */
280 if (((dptr[3].dp_typ == DOSPTYP_386BSD) ||
281 (dptr[3].dp_typ == DOSPTYP_NETBSD)) &&
282 (dptr[3].dp_start == 0) &&
283 (dptr[3].dp_size == 50000)) {
284 sprintf(line, " disk%d", i);
285 bd_printbsdslice(od, 0, line, verbose);
287 for (j = 0; j < od->od_nslices; j++) {
288 sprintf(line, " disk%ds%d", i, j + 1);
289 bd_printslice(od, &dptr[j], line, verbose);
299 * Print information about slices on a disk. For the size calculations we
300 * assume a 512 byte sector.
303 bd_printslice(struct open_disk *od, struct dos_partition *dp, char *prefix,
308 switch (dp->dp_typ) {
311 /* XXX: possibly add types 0 and 1, as in subr_disk, for gpt magic */
312 bd_printbsdslice(od, (daddr_t)dp->dp_start, prefix, verbose);
316 sprintf(line, "%s: Linux swap %.6dMB (%d - %d)\n",
317 prefix, dp->dp_size / 2048,
318 dp->dp_start, dp->dp_start + dp->dp_size);
320 sprintf(line, "%s: Linux swap\n", prefix);
325 * read the superblock to confirm this is an ext2fs partition?
328 sprintf(line, "%s: ext2fs %.6dMB (%d - %d)\n", prefix,
329 dp->dp_size / 2048, dp->dp_start,
330 dp->dp_start + dp->dp_size);
332 sprintf(line, "%s: ext2fs\n", prefix);
334 case 0x00: /* unused partition */
339 sprintf(line, "%s: FAT-12 %.6dMB (%d - %d)\n", prefix,
340 dp->dp_size / 2048, dp->dp_start,
341 dp->dp_start + dp->dp_size);
343 sprintf(line, "%s: FAT-12\n", prefix);
349 sprintf(line, "%s: FAT-16 %.6dMB (%d - %d)\n", prefix,
350 dp->dp_size / 2048, dp->dp_start,
351 dp->dp_start + dp->dp_size);
353 sprintf(line, "%s: FAT-16\n", prefix);
358 sprintf(line, "%s: FAT-32 %.6dMB (%d - %d)\n", prefix,
359 dp->dp_size / 2048, dp->dp_start,
360 dp->dp_start + dp->dp_size);
362 sprintf(line, "%s: FAT-32\n", prefix);
366 sprintf(line, "%s: Unknown fs: 0x%x %.6dMB (%d - %d)\n",
367 prefix, dp->dp_typ, dp->dp_size / 2048,
368 dp->dp_start, dp->dp_start + dp->dp_size);
370 sprintf(line, "%s: Unknown fs: 0x%x\n", prefix,
377 print_partition(u_int8_t fstype, unsigned long long offset,
378 unsigned long long size, int i, int od_flags,
379 char *prefix, int verbose, int type)
384 * For each partition, make sure we know what type of fs it is. If
385 * not, then skip it. However, since floppies often have bogus
386 * fstypes, print the 'a' partition on a floppy even if it is marked
389 if ((fstype == FS_SWAP) ||
390 (fstype == FS_VINUM) ||
391 (fstype == FS_HAMMER) ||
392 (fstype == FS_HAMMER2) ||
393 (fstype == FS_BSDFFS) ||
394 (fstype == FS_ZFS) ||
395 (fstype == FS_JFS2) ||
396 ((fstype == FS_UNUSED) &&
397 (od_flags & BD_FLOPPY) && (i == 0))) {
399 /* Only print out statistics in verbose mode */
401 sprintf(line, "%c %s%c: %s %.6lluMB (%llu - %llu)\n",
402 /* prefix disks that can be used to load modules with '*' */
403 ((fstype == FS_BSDFFS) || (fstype == FS_UNUSED) ||
404 (fstype == FS_VINUM)) ? '*' : ' ',
406 (fstype == FS_SWAP) ? "swap" :
407 (fstype == FS_VINUM) ? "vinum" :
408 (fstype == FS_HAMMER) ? "HAMMER" :
409 (fstype == FS_HAMMER2) ? "HAMMER2" :
410 (fstype == FS_JFS2) ? "JFS2" :
411 (fstype == FS_ZFS) ? "ZFS" :
412 (fstype == FS_BSDFFS) ? "FFS" :
414 (type==32)?(size / 2048):(size/1024/1024),
418 sprintf(line, "%c %s%c: %s\n",
419 /* prefix disks that can be used to load modules with '*' */
420 ((fstype == FS_BSDFFS) || (fstype == FS_UNUSED) ||
421 (fstype == FS_VINUM)) ? '*' : ' ',
423 (fstype == FS_SWAP) ? "swap" :
424 (fstype == FS_VINUM) ? "vinum" :
425 (fstype == FS_HAMMER) ? "HAMMER" :
426 (fstype == FS_HAMMER2) ? "HAMMER2" :
427 (fstype == FS_JFS2) ? "JFS2" :
428 (fstype == FS_ZFS) ? "ZFS" :
429 (fstype == FS_BSDFFS) ? "FFS" :
438 * Print out each valid partition in the disklabel of a FreeBSD slice.
439 * For size calculations, we assume a 512 byte sector size.
442 bd_printbsdslice(struct open_disk *od, daddr_t offset, char *prefix,
446 char buf[BIOSDISK_SECSIZE*2];
447 struct disklabel32 *lp = NULL;
448 struct disklabel64 *lp64 = NULL;
452 if (bd_read(od, offset + LABELSECTOR32, 1, buf))
454 lp =(struct disklabel32 *)(&buf[0]);
455 if (lp->d_magic != DISKMAGIC32) {
456 if (bd_read(od, offset, 2, buf))
459 lp64 =(struct disklabel64 *)(&buf[0]);
460 if (lp64->d_magic != DISKMAGIC64) {
461 sprintf(line, "%s: bad disklabel\n", prefix);
465 lp = NULL; /* PARANOID */
468 /* We got ourselves a disklabel64 here */
469 for (i = 0; i < lp64->d_npartitions; i++) {
470 if (lp64->d_partitions[i].p_bsize == 0)
473 print_partition(lp64->d_partitions[i].p_fstype,
474 lp64->d_partitions[i].p_boffset,
475 lp64->d_partitions[i].p_bsize,
476 i, od->od_flags, prefix, verbose, 64);
479 /* Print partitions */
480 for (i = 0; i < lp->d_npartitions; i++) {
481 print_partition(lp->d_partitions[i].p_fstype,
482 lp->d_partitions[i].p_offset,
483 lp->d_partitions[i].p_size,
484 i, od->od_flags, prefix, verbose, 32);
491 * Attempt to open the disk described by (dev) for use by (f).
493 * Note that the philosophy here is "give them exactly what
494 * they ask for". This is necessary because being too "smart"
495 * about what the user might want leads to complications.
496 * (eg. given no slice or partition value, with a disk that is
497 * sliced - are they after the first BSD slice, or the DOS
501 bd_open(struct open_file *f, ...)
504 struct i386_devdesc *dev;
505 struct open_disk *od;
509 dev = va_arg(ap, struct i386_devdesc *);
511 if ((error = bd_opendisk(&od, dev)))
517 ((struct i386_devdesc *)(f->f_devdata))->d_kind.biosdisk.data = od;
518 DEBUG("open_disk %p, partition at 0x%x", od, od->od_boff);
523 bd_opendisk(struct open_disk **odp, struct i386_devdesc *dev)
525 struct dos_partition *dptr;
526 struct disklabel32 *lp;
527 struct disklabel64 *lp64;
528 struct open_disk *od;
529 int sector, slice, i;
531 static char buf[BUFSIZE];
533 if (dev->d_kind.biosdisk.unit >= nbdinfo) {
534 DEBUG("attempt to open nonexistent disk");
538 od = (struct open_disk *)malloc(sizeof(struct open_disk));
544 /* Look up BIOS unit number, intialise open_disk structure */
545 od->od_dkunit = dev->d_kind.biosdisk.unit;
546 od->od_unit = bdinfo[od->od_dkunit].bd_unit;
547 od->od_flags = bdinfo[od->od_dkunit].bd_flags;
551 DEBUG("open '%s', unit 0x%x slice %d partition %c",
552 i386_fmtdev(dev), dev->d_kind.biosdisk.unit,
553 dev->d_kind.biosdisk.slice, dev->d_kind.biosdisk.partition + 'a');
555 /* Get geometry for this open (removable device may have changed) */
556 if (bd_getgeom(od)) {
557 DEBUG("can't get geometry");
563 * Following calculations attempt to determine the correct value
564 * for d->od_boff by looking for the slice and partition specified,
565 * or searching for reasonable defaults.
569 * Find the slice in the DOS slice table.
571 if (bd_read(od, 0, 1, buf)) {
572 DEBUG("error reading MBR");
578 * Check the slice table magic.
580 if (((u_char)buf[0x1fe] != 0x55) || ((u_char)buf[0x1ff] != 0xaa)) {
581 /* If a slice number was explicitly supplied, this is an error */
582 if (dev->d_kind.biosdisk.slice > 0) {
583 DEBUG("no slice table/MBR (no magic)");
588 goto unsliced; /* may be a floppy */
592 * copy the partition table, then pick up any extended partitions. The
593 * base partition table always has four entries, even if some of them
594 * represented extended partitions. However, any additional sub-extended
595 * partitions will be silently recursed and not included in the slice
598 bcopy(buf + DOSPARTOFF, &od->od_slicetab,
599 sizeof(struct dos_partition) * NDOSPART);
600 od->od_nslices = NDOSPART;
602 dptr = &od->od_slicetab[0];
603 for (i = 0; i < NDOSPART; i++, dptr++) {
604 if ((dptr->dp_typ == DOSPTYP_EXT) || (dptr->dp_typ == DOSPTYP_EXTLBA))
605 bd_chainextended(od, dptr->dp_start, 0); /* 1st offset is zero */
607 od->od_flags |= BD_PARTTABOK;
608 dptr = &od->od_slicetab[0];
611 * Overflow entries are not loaded into memory but we still keep
612 * track of the count. Fix it up now.
614 if (od->od_nslices > NEXTDOSPART)
615 od->od_nslices = NEXTDOSPART;
618 * Is this a request for the whole disk?
620 if (dev->d_kind.biosdisk.slice == -1) {
626 * if a slice number was supplied but not found, this is an error.
628 if (dev->d_kind.biosdisk.slice > 0) {
629 slice = dev->d_kind.biosdisk.slice - 1;
630 if (slice >= od->od_nslices) {
631 DEBUG("slice %d not found", slice);
638 * Check for the historically bogus MBR found on true dedicated disks
640 if ((dptr[3].dp_typ == DOSPTYP_386BSD) &&
641 (dptr[3].dp_start == 0) &&
642 (dptr[3].dp_size == 50000)) {
647 /* Try to auto-detect the best slice; this should always give a slice number */
648 if (dev->d_kind.biosdisk.slice == 0) {
649 slice = bd_bestslice(od);
654 dev->d_kind.biosdisk.slice = slice;
657 dptr = &od->od_slicetab[0];
659 * Accept the supplied slice number unequivocally (we may be looking
660 * at a DOS partition).
662 dptr += (dev->d_kind.biosdisk.slice - 1); /* we number 1-4, offsets are 0-3 */
663 sector = dptr->dp_start;
664 DEBUG("slice entry %d at %d, %d sectors", dev->d_kind.biosdisk.slice - 1, sector, dptr->dp_size);
667 * If we are looking at a BSD slice, and the partition is < 0, assume the 'a' partition
669 if ((dptr->dp_typ == DOSPTYP_386BSD) && (dev->d_kind.biosdisk.partition < 0))
670 dev->d_kind.biosdisk.partition = 0;
674 * Now we have the slice offset, look for the partition in the disklabel if we have
675 * a partition to start with.
677 * XXX we might want to check the label checksum.
679 if (dev->d_kind.biosdisk.partition < 0) {
680 od->od_boff = sector; /* no partition, must be after the slice */
681 DEBUG("opening raw slice");
684 if (bd_read(od, sector + LABELSECTOR32, 1, buf)) {
685 DEBUG("error reading disklabel");
689 DEBUG("copy %d bytes of label from %p to %p", sizeof(struct disklabel32), buf + LABELOFFSET32, &od->od_disklabel);
690 bcopy(buf + LABELOFFSET32, &od->od_disklabel, sizeof(struct disklabel32));
691 lp = &od->od_disklabel;
693 if (lp->d_magic == DISKMAGIC32) {
694 od->od_flags |= BD_LABELOK;
696 if (dev->d_kind.biosdisk.partition >= lp->d_npartitions) {
697 DEBUG("partition '%c' exceeds partitions in table (a-'%c')",
698 'a' + dev->d_kind.biosdisk.partition, 'a' + lp->d_npartitions);
705 /* Complain if the partition is unused unless this is a floppy. */
706 if ((lp->d_partitions[dev->d_kind.biosdisk.partition].p_fstype == FS_UNUSED) &&
707 !(od->od_flags & BD_FLOPPY))
708 DEBUG("warning, partition marked as unused");
712 lp->d_partitions[dev->d_kind.biosdisk.partition].p_offset -
713 lp->d_partitions[RAW_PART].p_offset +
719 /* else maybe DISKLABEL64? */
721 if (bd_read(od, sector, (sizeof(od->od_disklabel64) + 511) / 512, buf)) {
722 DEBUG("error reading disklabel");
726 DEBUG("copy %d bytes of label from %p to %p", sizeof(od->od_disklabel64), buf, &od->od_disklabel64);
727 bcopy(buf, &od->od_disklabel64, sizeof(od->od_disklabel64));
728 lp64 = &od->od_disklabel64;
730 if (lp64->d_magic == DISKMAGIC64) {
731 od->od_flags |= BD_LABELOK;
733 if (dev->d_kind.biosdisk.partition >= lp64->d_npartitions ||
734 lp64->d_partitions[dev->d_kind.biosdisk.partition].p_bsize == 0) {
735 DEBUG("partition '%c' exceeds partitions in table (a-'%c')",
736 'a' + dev->d_kind.biosdisk.partition, 'a' + lp64->d_npartitions);
743 lp64->d_partitions[dev->d_kind.biosdisk.partition].p_boffset / 512 +
746 DEBUG("disklabel64 slice at %d", od->od_boff);
749 DEBUG("no disklabel");
758 *odp = od; /* return the open disk */
765 bd_chainextended(struct open_disk *od, u_int32_t base, u_int32_t offset)
767 char buf[BIOSDISK_SECSIZE];
768 struct dos_partition *dp1, *dp2;
771 if (bd_read(od, (daddr_t)(base + offset), 1, buf)) {
772 printf("\nerror reading extended partition table");
777 * dp1 points to the first record in the on-disk XPT,
778 * dp2 points to the next entry in the in-memory parition table.
780 * NOTE: dp2 may be out of bounds if od_nslices >= NEXTDOSPART.
782 * NOTE: unlike the extended partitions in our primary dos partition
783 * table, we do not record recursed extended partitions themselves
784 * in our in-memory partition table.
786 * NOTE: recording within our in-memory table must be breadth first
787 * ot match what the kernel does. Thus, two passes are required.
789 * NOTE: partitioning programs which support extended partitions seem
790 * to always use the first entry for the user partition and the
791 * second entry to chain, and also appear to disallow more then one
792 * extended partition at each level. Nevertheless we make our code
793 * somewhat more generic (and the same as the kernel's own slice
796 dp1 = (struct dos_partition *)(&buf[DOSPARTOFF]);
797 dp2 = &od->od_slicetab[od->od_nslices];
799 for (i = 0; i < NDOSPART; ++i, ++dp1) {
800 if (dp1->dp_scyl == 0 && dp1->dp_shd == 0 &&
801 dp1->dp_ssect == 0 && dp1->dp_start == 0 &&
805 if ((dp1->dp_typ == DOSPTYP_EXT) ||
806 (dp1->dp_typ == DOSPTYP_EXTLBA)) {
808 * breadth first traversal, must skip in the
815 * Only load up the in-memory data if we haven't overflowed
816 * our in-memory array.
818 if (od->od_nslices < NEXTDOSPART) {
819 dp2->dp_typ = dp1->dp_typ;
820 dp2->dp_start = base + offset + dp1->dp_start;
821 dp2->dp_size = dp1->dp_size;
828 * Pass 2 - handle extended partitions. Note that the extended
829 * slice itself is not stored in the slice array when we recurse,
830 * but any 'original' top-level extended slices are. This is to
831 * match what the kernel does.
834 for (i = 0; i < NDOSPART; ++i, ++dp1) {
835 if (dp1->dp_scyl == 0 && dp1->dp_shd == 0 &&
836 dp1->dp_ssect == 0 && dp1->dp_start == 0 &&
840 if ((dp1->dp_typ == DOSPTYP_EXT) ||
841 (dp1->dp_typ == DOSPTYP_EXTLBA))
842 bd_chainextended(od, base, dp1->dp_start);
847 * Search for a slice with the following preferences:
849 * 1: Active FreeBSD slice
850 * 2: Non-active FreeBSD slice
851 * 3: Active Linux slice
852 * 4: non-active Linux slice
853 * 5: Active FAT/FAT32 slice
854 * 6: non-active FAT/FAT32 slice
856 #define PREF_RAWDISK 0
857 #define PREF_FBSD_ACT 1
859 #define PREF_LINUX_ACT 3
861 #define PREF_DOS_ACT 5
866 * slicelimit is in the range 0 .. NDOSPART
869 bd_bestslice(struct open_disk *od)
871 struct dos_partition *dp;
876 preflevel = PREF_NONE;
878 dp = &od->od_slicetab[0];
879 for (i = 0; i < od->od_nslices; i++, dp++) {
880 switch (dp->dp_typ) {
881 case DOSPTYP_386BSD: /* FreeBSD */
882 pref = dp->dp_flag & 0x80 ? PREF_FBSD_ACT : PREF_FBSD;
886 pref = dp->dp_flag & 0x80 ? PREF_LINUX_ACT : PREF_LINUX;
889 case 0x01: /* DOS/Windows */
895 pref = dp->dp_flag & 0x80 ? PREF_DOS_ACT : PREF_DOS;
901 if (pref < preflevel) {
910 bd_close(struct open_file *f)
912 struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)(f->f_devdata))->d_kind.biosdisk.data);
919 bd_closedisk(struct open_disk *od)
921 DEBUG("open_disk %p", od);
923 /* XXX is this required? (especially if disk already open...) */
924 if (od->od_flags & BD_FLOPPY)
931 bd_strategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
933 struct bcache_devdata bcd;
934 struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)devdata)->d_kind.biosdisk.data);
936 bcd.dv_strategy = bd_realstrategy;
937 bcd.dv_devdata = devdata;
938 return(bcache_strategy(&bcd, od->od_unit, rw, dblk+od->od_boff, size, buf, rsize));
942 bd_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
944 struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)devdata)->d_kind.biosdisk.data);
946 #ifdef BD_SUPPORT_FRAGS
947 char fragbuf[BIOSDISK_SECSIZE];
950 fragsize = size % BIOSDISK_SECSIZE;
952 if (size % BIOSDISK_SECSIZE)
953 panic("bd_strategy: %d bytes I/O not multiple of block size", size);
956 DEBUG("open_disk %p", od);
960 blks = size / BIOSDISK_SECSIZE;
961 DEBUG("read %d from %d to %p", blks, dblk, buf);
965 if (blks && bd_read(od, dblk, blks, buf)) {
969 #ifdef BD_SUPPORT_FRAGS
970 DEBUG("bd_strategy: frag read %d from %d+%d to %p",
971 fragsize, dblk, blks, buf + (blks * BIOSDISK_SECSIZE));
972 if (fragsize && bd_read(od, dblk + blks, 1, fragsize)) {
973 DEBUG("frag read error");
976 bcopy(fragbuf, buf + (blks * BIOSDISK_SECSIZE), fragsize);
982 blks = size / BIOSDISK_SECSIZE;
983 DEBUG("write %d from %d to %p", blks, dblk, buf);
987 if (blks && bd_write(od, dblk, blks, buf)) {
988 DEBUG("write error");
991 #ifdef BD_SUPPORT_FRAGS
993 DEBUG("Attempted to write a frag");
1001 break; /* DO NOTHING */
1007 bd_read(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest)
1009 u_int x, bpc, cyl, hd, sec, result, resid, retry, maxfer;
1010 caddr_t p, xp, bbuf, breg;
1012 /* Just in case some idiot actually tries to read -1 blocks... */
1016 bpc = (od->od_sec * od->od_hds); /* blocks per cylinder */
1021 * Always bounce. Our buffer is probably not segment-addressable.
1026 * There is a 64k physical boundary somewhere in the destination
1027 * buffer, so we have to arrange a suitable bounce buffer. Allocate
1028 * a buffer twice as large as we need to. Use the bottom half unless
1029 * there is a break there, in which case we use the top half.
1032 x = min(BOUNCEBUF_SECTS, (unsigned)blks);
1034 if (((u_int32_t)VTOP(bbuf) & 0xffff8000) ==
1035 ((u_int32_t)VTOP(bbuf + x * BIOSDISK_SECSIZE - 1) & 0xffff8000)) {
1038 breg = bbuf + x * BIOSDISK_SECSIZE;
1040 maxfer = x; /* limit transfers to bounce region size */
1048 cyl = x / bpc; /* block # / blocks per cylinder */
1049 x %= bpc; /* block offset into cylinder */
1050 hd = x / od->od_sec; /* offset / blocks per track */
1051 sec = x % od->od_sec; /* offset into track */
1053 /* play it safe and don't cross track boundaries (XXX this is probably unnecessary) */
1054 x = szmin(od->od_sec - sec, resid);
1056 x = min(x, maxfer); /* fit bounce buffer */
1058 /* where do we transfer to? */
1059 xp = (bbuf == NULL ? p : breg);
1061 /* correct sector number for 1-based BIOS numbering */
1065 * Loop retrying the operation a couple of times. The BIOS may also
1068 for (retry = 0; retry < 3; retry++) {
1070 * If retrying, reset the drive.
1073 v86.ctl = V86_FLAGS;
1076 v86.edx = od->od_unit;
1081 * Always use EDD if the disk supports it, otherwise fall back
1082 * to CHS mode (returning an error if the cylinder number is
1085 if (od->od_flags & BD_MODEEDD1) {
1086 static unsigned short packet[8];
1090 packet[2] = VTOPOFF(xp);
1091 packet[3] = VTOPSEG(xp);
1092 packet[4] = dblk & 0xffff;
1093 packet[5] = dblk >> 16;
1096 v86.ctl = V86_FLAGS;
1099 v86.edx = od->od_unit;
1100 v86.ds = VTOPSEG(packet);
1101 v86.esi = VTOPOFF(packet);
1103 result = (v86.efl & PSL_C);
1106 } else if (cyl < 1024) {
1107 /* Use normal CHS addressing */
1108 v86.ctl = V86_FLAGS;
1110 v86.eax = 0x200 | x;
1111 v86.ecx = ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec;
1112 v86.edx = (hd << 8) | od->od_unit;
1113 v86.es = VTOPSEG(xp);
1114 v86.ebx = VTOPOFF(xp);
1116 result = (v86.efl & PSL_C);
1125 DEBUG("%u sectors from %u/%u/%d to %p (0x%x) %s",
1126 x, cyl, hd, sec - 1, p, VTOP(p), result ? "failed" : "ok");
1127 /* BUG here, cannot use v86 in printf because putchar uses it too */
1128 DEBUG("ax = 0x%04x cx = 0x%04x dx = 0x%04x status 0x%x",
1130 ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec,
1131 (hd << 8) | od->od_unit,
1132 (v86.eax >> 8) & 0xff);
1136 bcopy(breg, p, x * BIOSDISK_SECSIZE);
1137 p += (x * BIOSDISK_SECSIZE);
1142 /* hexdump(dest, (blks * BIOSDISK_SECSIZE)); */
1148 bd_write(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest)
1150 u_int x, bpc, cyl, hd, sec, result, resid, retry, maxfer;
1151 caddr_t p, xp, bbuf, breg;
1153 /* Just in case some idiot actually tries to read -1 blocks... */
1157 bpc = (od->od_sec * od->od_hds); /* blocks per cylinder */
1162 * Always bounce. Our buffer is probably not segment-addressable.
1166 * There is a 64k physical boundary somewhere in the destination
1167 * buffer, so we have to arrange a suitable bounce buffer. Allocate
1168 * a buffer twice as large as we need to. Use the bottom half
1169 * unless there is a break there, in which case we use the top half.
1172 x = min(BOUNCEBUF_SECTS, (unsigned)blks);
1174 if (((u_int32_t)VTOP(bbuf) & 0xffff0000) ==
1175 ((u_int32_t)VTOP(bbuf + x * BIOSDISK_SECSIZE - 1) & 0xffff0000)) {
1178 breg = bbuf + x * BIOSDISK_SECSIZE;
1180 maxfer = x; /* limit transfers to bounce region size */
1188 cyl = x / bpc; /* block # / blocks per cylinder */
1189 x %= bpc; /* block offset into cylinder */
1190 hd = x / od->od_sec; /* offset / blocks per track */
1191 sec = x % od->od_sec; /* offset into track */
1193 /* play it safe and don't cross track boundaries (XXX this is probably unnecessary) */
1194 x = szmin(od->od_sec - sec, resid);
1196 x = szmin(x, maxfer); /* fit bounce buffer */
1198 /* where do we transfer to? */
1199 xp = (bbuf == NULL ? p : breg);
1201 /* correct sector number for 1-based BIOS numbering */
1205 /* Put your Data In, Put your Data out,
1206 Put your Data In, and shake it all about
1209 bcopy(p, breg, x * BIOSDISK_SECSIZE);
1210 p += (x * BIOSDISK_SECSIZE);
1215 * Loop retrying the operation a couple of times. The BIOS may also
1218 for (retry = 0; retry < 3; retry++) {
1220 * If retrying, reset the drive.
1223 v86.ctl = V86_FLAGS;
1226 v86.edx = od->od_unit;
1231 * Always use EDD if the disk supports it, otherwise fall back
1232 * to CHS mode (returning an error if the cylinder number is
1235 if (od->od_flags & BD_MODEEDD1) {
1236 static unsigned short packet[8];
1240 packet[2] = VTOPOFF(xp);
1241 packet[3] = VTOPSEG(xp);
1242 packet[4] = dblk & 0xffff;
1243 packet[5] = dblk >> 16;
1246 v86.ctl = V86_FLAGS;
1248 /* Should we Write with verify ?? 0x4302 ? */
1250 v86.edx = od->od_unit;
1251 v86.ds = VTOPSEG(packet);
1252 v86.esi = VTOPOFF(packet);
1254 result = (v86.efl & PSL_C);
1257 } else if (cyl < 1024) {
1258 /* Use normal CHS addressing */
1259 v86.ctl = V86_FLAGS;
1261 v86.eax = 0x300 | x;
1262 v86.ecx = ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec;
1263 v86.edx = (hd << 8) | od->od_unit;
1264 v86.es = VTOPSEG(xp);
1265 v86.ebx = VTOPOFF(xp);
1267 result = (v86.efl & PSL_C);
1276 DEBUG("%u sectors from %u/%u/%d to %p (0x%x) %s", x, cyl, hd, sec - 1, p, VTOP(p), result ? "failed" : "ok");
1277 /* BUG here, cannot use v86 in printf because putchar uses it too */
1278 DEBUG("ax = 0x%04x cx = 0x%04x dx = 0x%04x status 0x%x",
1279 0x200 | x, ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec, (hd << 8) | od->od_unit, (v86.eax >> 8) & 0xff);
1284 /* hexdump(dest, (blks * BIOSDISK_SECSIZE)); */
1288 bd_getgeom(struct open_disk *od)
1291 v86.ctl = V86_FLAGS;
1294 v86.edx = od->od_unit;
1297 if ((v86.efl & PSL_C) || /* carry set */
1298 ((v86.edx & 0xff) <= (unsigned)(od->od_unit & 0x7f))) /* unit # bad */
1301 /* convert max cyl # -> # of cylinders */
1302 od->od_cyl = ((v86.ecx & 0xc0) << 2) + ((v86.ecx & 0xff00) >> 8) + 1;
1303 /* convert max head # -> # of heads */
1304 od->od_hds = ((v86.edx & 0xff00) >> 8) + 1;
1305 od->od_sec = v86.ecx & 0x3f;
1307 DEBUG("unit 0x%x geometry %d/%d/%d", od->od_unit, od->od_cyl, od->od_hds, od->od_sec);
1312 * Return the BIOS geometry of a given "fixed drive" in a format
1313 * suitable for the legacy bootinfo structure. Since the kernel is
1314 * expecting raw int 0x13/0x8 values for N_BIOS_GEOM drives, we
1315 * prefer to get the information directly, rather than rely on being
1316 * able to put it together from information already maintained for
1317 * different purposes and for a probably different number of drives.
1319 * For valid drives, the geometry is expected in the format (31..0)
1320 * "000000cc cccccccc hhhhhhhh 00ssssss"; and invalid drives are
1321 * indicated by returning the geometry of a "1.2M" PC-format floppy
1322 * disk. And, incidentally, what is returned is not the geometry as
1323 * such but the highest valid cylinder, head, and sector numbers.
1326 bd_getbigeom(int bunit)
1329 v86.ctl = V86_FLAGS;
1332 v86.edx = 0x80 + bunit;
1334 if (v86.efl & PSL_C)
1336 return ((v86.ecx & 0xc0) << 18) | ((v86.ecx & 0xff00) << 8) |
1337 (v86.edx & 0xff00) | (v86.ecx & 0x3f);
1341 * Return a suitable cdev_t value for (dev).
1343 * In the case where it looks like (dev) is a SCSI disk, we allow the number of
1344 * IDE disks to be specified in $num_ide_disks. There should be a Better Way.
1347 bd_getdev(struct i386_devdesc *dev)
1349 struct open_disk *od;
1354 int unitofs = 0, i, unit;
1356 biosdev = bd_unit2bios(dev->d_kind.biosdisk.unit);
1357 DEBUG("unit %d BIOS device %d", dev->d_kind.biosdisk.unit, biosdev);
1358 if (biosdev == -1) /* not a BIOS device */
1360 if (bd_opendisk(&od, dev) != 0) /* oops, not a viable device */
1363 if (biosdev < 0x80) {
1364 /* floppy (or emulated floppy) or ATAPI device */
1365 if (bdinfo[dev->d_kind.biosdisk.unit].bd_type == DT_ATAPI) {
1366 /* is an ATAPI disk */
1369 /* is a floppy disk */
1374 if ((od->od_flags & BD_LABELOK) && (od->od_disklabel.d_type == DTYPE_SCSI)) {
1375 /* label OK, disk labelled as SCSI */
1377 /* check for unit number correction hint, now deprecated */
1378 if ((nip = getenv("num_ide_disks")) != NULL) {
1379 i = strtol(nip, &cp, 0);
1380 /* check for parse error */
1381 if ((cp != nip) && (*cp == 0))
1385 /* assume an IDE disk */
1389 /* default root disk unit number */
1390 unit = (biosdev & 0x7f) - unitofs;
1392 /* XXX a better kludge to set the root disk unit number */
1393 if ((nip = getenv("root_disk_unit")) != NULL) {
1394 i = strtol(nip, &cp, 0);
1395 /* check for parse error */
1396 if ((cp != nip) && (*cp == 0))
1400 rootdev = MAKEBOOTDEV(major,
1401 (dev->d_kind.biosdisk.slice + 1) >> 4, /* XXX slices may be wrong here */
1402 (dev->d_kind.biosdisk.slice + 1) & 0xf,
1404 dev->d_kind.biosdisk.partition);
1405 DEBUG("dev is 0x%x\n", rootdev);