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 $
27 * $DragonFly: src/sys/boot/pc32/libi386/biosdisk.c,v 1.12 2007/06/18 05:13:42 dillon Exp $
31 * BIOS disk device handling.
33 * Ideas and algorithms from:
35 * - NetBSD libi386/biosdisk.c
36 * - FreeBSD biosboot/disk.c
42 #include <sys/disklabel32.h>
43 #include <sys/disklabel64.h>
44 #include <sys/diskmbr.h>
45 #include <sys/dtype.h>
46 #include <machine/bootinfo.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, u_int32_t offset);
146 * Translate between BIOS device numbers and our private unit numbers.
149 bd_bios2unit(int biosdev)
153 DEBUG("looking for bios device 0x%x", biosdev);
154 for (i = 0; i < nbdinfo; i++) {
155 DEBUG("bd unit %d is BIOS device 0x%x", i, bdinfo[i].bd_unit);
156 if (bdinfo[i].bd_unit == biosdev)
163 bd_unit2bios(int unit)
165 if ((unit >= 0) && (unit < nbdinfo))
166 return(bdinfo[unit].bd_unit);
171 * Quiz the BIOS for disk devices, save a little info about them.
176 int base, unit, nfd = 0;
178 /* sequence 0, 0x80 */
179 for (base = 0; base <= 0x80; base += 0x80) {
180 for (unit = base; (nbdinfo < MAXBDDEV); unit++) {
181 /* check the BIOS equipment list for number of fixed disks */
183 (nfd >= *(unsigned char *)PTOV(BIOS_NUMDRIVES)))
186 bdinfo[nbdinfo].bd_unit = unit;
187 bdinfo[nbdinfo].bd_flags = (unit < 0x80) ? BD_FLOPPY : 0;
189 if (!bd_int13probe(&bdinfo[nbdinfo]))
192 /* XXX we need "disk aliases" to make this simpler */
193 printf("BIOS drive %c: is disk%d\n",
194 (unit < 0x80) ? ('A' + unit) : ('C' + unit - 0x80), nbdinfo);
204 * Try to detect a device supported by the legacy int13 BIOS
207 bd_int13probe(struct bdinfo *bd)
212 v86.edx = bd->bd_unit;
215 if (!(v86.efl & 0x1) && /* carry clear */
216 ((v86.edx & 0xff) > ((unsigned)bd->bd_unit & 0x7f))) { /* unit # OK */
219 * Ignore devices with an absurd sector size.
221 if ((v86.ecx & 0x3f) == 0) {
222 DEBUG("Invalid geometry for unit %d", bd->bd_unit);
225 bd->bd_flags |= BD_MODEINT13;
226 bd->bd_type = v86.ebx & 0xff;
228 /* Determine if we can use EDD with this device. */
230 v86.edx = bd->bd_unit;
233 if (!(v86.efl & 0x1) && /* carry clear */
234 ((v86.ebx & 0xffff) == 0xaa55) && /* signature */
235 (v86.ecx & 0x1)) { /* packets mode ok */
236 bd->bd_flags |= BD_MODEEDD1;
237 if((v86.eax & 0xff00) > 0x300)
238 bd->bd_flags |= BD_MODEEDD3;
246 * Print information about disks
249 bd_print(int verbose)
253 struct i386_devdesc dev;
254 struct open_disk *od;
255 struct dos_partition *dptr;
257 for (i = 0; i < nbdinfo; i++) {
258 sprintf(line, " disk%d: BIOS drive %c:\n", i,
259 (bdinfo[i].bd_unit < 0x80) ? ('A' + bdinfo[i].bd_unit) : ('C' + bdinfo[i].bd_unit - 0x80));
262 /* try to open the whole disk */
263 dev.d_kind.biosdisk.unit = i;
264 dev.d_kind.biosdisk.slice = -1;
265 dev.d_kind.biosdisk.partition = -1;
267 if (!bd_opendisk(&od, &dev)) {
269 /* Do we have a partition table? */
270 if (od->od_flags & BD_PARTTABOK) {
271 dptr = &od->od_slicetab[0];
273 /* Check for a "dedicated" disk */
274 if (((dptr[3].dp_typ == DOSPTYP_386BSD) ||
275 (dptr[3].dp_typ == DOSPTYP_NETBSD)) &&
276 (dptr[3].dp_start == 0) &&
277 (dptr[3].dp_size == 50000)) {
278 sprintf(line, " disk%d", i);
279 bd_printbsdslice(od, 0, line, verbose);
281 for (j = 0; j < od->od_nslices; j++) {
282 sprintf(line, " disk%ds%d", i, j + 1);
283 bd_printslice(od, &dptr[j], line, verbose);
293 * Print information about slices on a disk. For the size calculations we
294 * assume a 512 byte sector.
297 bd_printslice(struct open_disk *od, struct dos_partition *dp, char *prefix,
302 switch (dp->dp_typ) {
305 /* XXX: possibly add types 0 and 1, as in subr_disk, for gpt magic */
306 bd_printbsdslice(od, (daddr_t)dp->dp_start, prefix, verbose);
310 sprintf(line, "%s: Linux swap %.6dMB (%d - %d)\n",
311 prefix, dp->dp_size / 2048,
312 dp->dp_start, dp->dp_start + dp->dp_size);
314 sprintf(line, "%s: Linux swap\n", prefix);
319 * read the superblock to confirm this is an ext2fs partition?
322 sprintf(line, "%s: ext2fs %.6dMB (%d - %d)\n", prefix,
323 dp->dp_size / 2048, dp->dp_start,
324 dp->dp_start + dp->dp_size);
326 sprintf(line, "%s: ext2fs\n", prefix);
328 case 0x00: /* unused partition */
333 sprintf(line, "%s: FAT-12 %.6dMB (%d - %d)\n", prefix,
334 dp->dp_size / 2048, dp->dp_start,
335 dp->dp_start + dp->dp_size);
337 sprintf(line, "%s: FAT-12\n", prefix);
343 sprintf(line, "%s: FAT-16 %.6dMB (%d - %d)\n", prefix,
344 dp->dp_size / 2048, dp->dp_start,
345 dp->dp_start + dp->dp_size);
347 sprintf(line, "%s: FAT-16\n", prefix);
352 sprintf(line, "%s: FAT-32 %.6dMB (%d - %d)\n", prefix,
353 dp->dp_size / 2048, dp->dp_start,
354 dp->dp_start + dp->dp_size);
356 sprintf(line, "%s: FAT-32\n", prefix);
360 sprintf(line, "%s: Unknown fs: 0x%x %.6dMB (%d - %d)\n",
361 prefix, dp->dp_typ, dp->dp_size / 2048,
362 dp->dp_start, dp->dp_start + dp->dp_size);
364 sprintf(line, "%s: Unknown fs: 0x%x\n", prefix,
371 print_partition(u_int8_t fstype, unsigned long long offset,
372 unsigned long long size, int i, int od_flags,
373 char *prefix, int verbose, int type)
378 * For each partition, make sure we know what type of fs it is. If
379 * not, then skip it. However, since floppies often have bogus
380 * fstypes, print the 'a' partition on a floppy even if it is marked
383 if ((fstype == FS_SWAP) ||
384 (fstype == FS_VINUM) ||
385 (fstype == FS_HAMMER) ||
386 (fstype == FS_BSDFFS) ||
387 (fstype == FS_ZFS) ||
388 (fstype == FS_JFS2) ||
389 ((fstype == FS_UNUSED) &&
390 (od_flags & BD_FLOPPY) && (i == 0))) {
392 /* Only print out statistics in verbose mode */
394 sprintf(line, " %s%c: %s %.6lluMB (%llu - %llu)\n",
396 (fstype == FS_SWAP) ? "swap" :
397 (fstype == FS_VINUM) ? "vinum" :
398 (fstype == FS_HAMMER) ? "HAMMER" :
399 (fstype == FS_JFS2) ? "JFS2" :
400 (fstype == FS_ZFS) ? "ZFS" :
401 (fstype == FS_BSDFFS) ? "FFS" :
403 (type==32)?(size / 2048):(size/1024/1024),
407 sprintf(line, " %s%c: %s\n", prefix, 'a' + i,
408 (fstype == FS_SWAP) ? "swap" :
409 (fstype == FS_VINUM) ? "vinum" :
410 (fstype == FS_HAMMER) ? "HAMMER" :
411 (fstype == FS_JFS2) ? "JFS2" :
412 (fstype == FS_ZFS) ? "ZFS" :
413 (fstype == FS_BSDFFS) ? "FFS" :
422 * Print out each valid partition in the disklabel of a FreeBSD slice.
423 * For size calculations, we assume a 512 byte sector size.
426 bd_printbsdslice(struct open_disk *od, daddr_t offset, char *prefix,
430 char buf[BIOSDISK_SECSIZE*2];
431 struct disklabel32 *lp = NULL;
432 struct disklabel64 *lp64 = NULL;
436 if (bd_read(od, offset + LABELSECTOR32, 1, buf))
438 lp =(struct disklabel32 *)(&buf[0]);
439 if (lp->d_magic != DISKMAGIC32) {
440 if (bd_read(od, offset, 2, buf))
443 lp64 =(struct disklabel64 *)(&buf[0]);
444 if (lp64->d_magic != DISKMAGIC64) {
445 sprintf(line, "%s: bad disklabel\n", prefix);
449 lp = NULL; /* PARANOID */
452 /* We got ourselves a disklabel64 here */
453 for (i = 0; i < lp64->d_npartitions; i++) {
454 if (lp64->d_partitions[i].p_bsize == 0)
457 print_partition(lp64->d_partitions[i].p_fstype,
458 lp64->d_partitions[i].p_boffset,
459 lp64->d_partitions[i].p_bsize,
460 i, od->od_flags, prefix, verbose, 64);
463 /* Print partitions */
464 for (i = 0; i < lp->d_npartitions; i++) {
465 print_partition(lp->d_partitions[i].p_fstype,
466 lp->d_partitions[i].p_offset,
467 lp->d_partitions[i].p_size,
468 i, od->od_flags, prefix, verbose, 32);
475 * Attempt to open the disk described by (dev) for use by (f).
477 * Note that the philosophy here is "give them exactly what
478 * they ask for". This is necessary because being too "smart"
479 * about what the user might want leads to complications.
480 * (eg. given no slice or partition value, with a disk that is
481 * sliced - are they after the first BSD slice, or the DOS
485 bd_open(struct open_file *f, ...)
488 struct i386_devdesc *dev;
489 struct open_disk *od;
493 dev = va_arg(ap, struct i386_devdesc *);
495 if ((error = bd_opendisk(&od, dev)))
501 ((struct i386_devdesc *)(f->f_devdata))->d_kind.biosdisk.data = od;
502 DEBUG("open_disk %p, partition at 0x%x", od, od->od_boff);
507 bd_opendisk(struct open_disk **odp, struct i386_devdesc *dev)
509 struct dos_partition *dptr;
510 struct disklabel32 *lp;
511 struct disklabel64 *lp64;
512 struct open_disk *od;
513 int sector, slice, i;
515 static char buf[BUFSIZE];
517 if (dev->d_kind.biosdisk.unit >= nbdinfo) {
518 DEBUG("attempt to open nonexistent disk");
522 od = (struct open_disk *)malloc(sizeof(struct open_disk));
528 /* Look up BIOS unit number, intialise open_disk structure */
529 od->od_dkunit = dev->d_kind.biosdisk.unit;
530 od->od_unit = bdinfo[od->od_dkunit].bd_unit;
531 od->od_flags = bdinfo[od->od_dkunit].bd_flags;
535 DEBUG("open '%s', unit 0x%x slice %d partition %c",
536 i386_fmtdev(dev), dev->d_kind.biosdisk.unit,
537 dev->d_kind.biosdisk.slice, dev->d_kind.biosdisk.partition + 'a');
539 /* Get geometry for this open (removable device may have changed) */
540 if (bd_getgeom(od)) {
541 DEBUG("can't get geometry");
547 * Following calculations attempt to determine the correct value
548 * for d->od_boff by looking for the slice and partition specified,
549 * or searching for reasonable defaults.
553 * Find the slice in the DOS slice table.
555 if (bd_read(od, 0, 1, buf)) {
556 DEBUG("error reading MBR");
562 * Check the slice table magic.
564 if (((u_char)buf[0x1fe] != 0x55) || ((u_char)buf[0x1ff] != 0xaa)) {
565 /* If a slice number was explicitly supplied, this is an error */
566 if (dev->d_kind.biosdisk.slice > 0) {
567 DEBUG("no slice table/MBR (no magic)");
572 goto unsliced; /* may be a floppy */
576 * copy the partition table, then pick up any extended partitions. The
577 * base partition table always has four entries, even if some of them
578 * represented extended partitions. However, any additional sub-extended
579 * partitions will be silently recursed and not included in the slice
582 bcopy(buf + DOSPARTOFF, &od->od_slicetab,
583 sizeof(struct dos_partition) * NDOSPART);
584 od->od_nslices = NDOSPART;
586 dptr = &od->od_slicetab[0];
587 for (i = 0; i < NDOSPART; i++, dptr++) {
588 if ((dptr->dp_typ == DOSPTYP_EXT) || (dptr->dp_typ == DOSPTYP_EXTLBA))
589 bd_chainextended(od, dptr->dp_start, 0); /* 1st offset is zero */
591 od->od_flags |= BD_PARTTABOK;
592 dptr = &od->od_slicetab[0];
595 * Overflow entries are not loaded into memory but we still keep
596 * track of the count. Fix it up now.
598 if (od->od_nslices > NEXTDOSPART)
599 od->od_nslices = NEXTDOSPART;
602 * Is this a request for the whole disk?
604 if (dev->d_kind.biosdisk.slice == -1) {
610 * if a slice number was supplied but not found, this is an error.
612 if (dev->d_kind.biosdisk.slice > 0) {
613 slice = dev->d_kind.biosdisk.slice - 1;
614 if (slice >= od->od_nslices) {
615 DEBUG("slice %d not found", slice);
622 * Check for the historically bogus MBR found on true dedicated disks
624 if ((dptr[3].dp_typ == DOSPTYP_386BSD) &&
625 (dptr[3].dp_start == 0) &&
626 (dptr[3].dp_size == 50000)) {
631 /* Try to auto-detect the best slice; this should always give a slice number */
632 if (dev->d_kind.biosdisk.slice == 0) {
633 slice = bd_bestslice(od);
638 dev->d_kind.biosdisk.slice = slice;
641 dptr = &od->od_slicetab[0];
643 * Accept the supplied slice number unequivocally (we may be looking
644 * at a DOS partition).
646 dptr += (dev->d_kind.biosdisk.slice - 1); /* we number 1-4, offsets are 0-3 */
647 sector = dptr->dp_start;
648 DEBUG("slice entry %d at %d, %d sectors", dev->d_kind.biosdisk.slice - 1, sector, dptr->dp_size);
651 * If we are looking at a BSD slice, and the partition is < 0, assume the 'a' partition
653 if ((dptr->dp_typ == DOSPTYP_386BSD) && (dev->d_kind.biosdisk.partition < 0))
654 dev->d_kind.biosdisk.partition = 0;
658 * Now we have the slice offset, look for the partition in the disklabel if we have
659 * a partition to start with.
661 * XXX we might want to check the label checksum.
663 if (dev->d_kind.biosdisk.partition < 0) {
664 od->od_boff = sector; /* no partition, must be after the slice */
665 DEBUG("opening raw slice");
668 if (bd_read(od, sector + LABELSECTOR32, 1, buf)) {
669 DEBUG("error reading disklabel");
673 DEBUG("copy %d bytes of label from %p to %p", sizeof(struct disklabel32), buf + LABELOFFSET32, &od->od_disklabel);
674 bcopy(buf + LABELOFFSET32, &od->od_disklabel, sizeof(struct disklabel32));
675 lp = &od->od_disklabel;
677 if (lp->d_magic == DISKMAGIC32) {
678 od->od_flags |= BD_LABELOK;
680 if (dev->d_kind.biosdisk.partition >= lp->d_npartitions) {
681 DEBUG("partition '%c' exceeds partitions in table (a-'%c')",
682 'a' + dev->d_kind.biosdisk.partition, 'a' + lp->d_npartitions);
689 /* Complain if the partition is unused unless this is a floppy. */
690 if ((lp->d_partitions[dev->d_kind.biosdisk.partition].p_fstype == FS_UNUSED) &&
691 !(od->od_flags & BD_FLOPPY))
692 DEBUG("warning, partition marked as unused");
696 lp->d_partitions[dev->d_kind.biosdisk.partition].p_offset -
697 lp->d_partitions[RAW_PART].p_offset +
703 /* else maybe DISKLABEL64? */
705 if (bd_read(od, sector, (sizeof(od->od_disklabel64) + 511) / 512, buf)) {
706 DEBUG("error reading disklabel");
710 DEBUG("copy %d bytes of label from %p to %p", sizeof(od->od_disklabel64), buf, &od->od_disklabel64);
711 bcopy(buf, &od->od_disklabel64, sizeof(od->od_disklabel64));
712 lp64 = &od->od_disklabel64;
714 if (lp64->d_magic == DISKMAGIC64) {
715 od->od_flags |= BD_LABELOK;
717 if (dev->d_kind.biosdisk.partition >= lp64->d_npartitions ||
718 lp64->d_partitions[dev->d_kind.biosdisk.partition].p_bsize == 0) {
719 DEBUG("partition '%c' exceeds partitions in table (a-'%c')",
720 'a' + dev->d_kind.biosdisk.partition, 'a' + lp64->d_npartitions);
727 lp64->d_partitions[dev->d_kind.biosdisk.partition].p_boffset / 512 +
730 DEBUG("disklabel64 slice at %d", od->od_boff);
733 DEBUG("no disklabel");
742 *odp = od; /* return the open disk */
749 bd_chainextended(struct open_disk *od, u_int32_t base, u_int32_t offset)
751 char buf[BIOSDISK_SECSIZE];
752 struct dos_partition *dp1, *dp2;
755 if (bd_read(od, (daddr_t)(base + offset), 1, buf)) {
756 printf("\nerror reading extended partition table");
761 * dp1 points to the first record in the on-disk XPT,
762 * dp2 points to the next entry in the in-memory parition table.
764 * NOTE: dp2 may be out of bounds if od_nslices >= NEXTDOSPART.
766 * NOTE: unlike the extended partitions in our primary dos partition
767 * table, we do not record recursed extended partitions themselves
768 * in our in-memory partition table.
770 * NOTE: recording within our in-memory table must be breadth first
771 * ot match what the kernel does. Thus, two passes are required.
773 * NOTE: partitioning programs which support extended partitions seem
774 * to always use the first entry for the user partition and the
775 * second entry to chain, and also appear to disallow more then one
776 * extended partition at each level. Nevertheless we make our code
777 * somewhat more generic (and the same as the kernel's own slice
780 dp1 = (struct dos_partition *)(&buf[DOSPARTOFF]);
781 dp2 = &od->od_slicetab[od->od_nslices];
783 for (i = 0; i < NDOSPART; ++i, ++dp1) {
784 if (dp1->dp_scyl == 0 && dp1->dp_shd == 0 &&
785 dp1->dp_ssect == 0 && dp1->dp_start == 0 &&
789 if ((dp1->dp_typ == DOSPTYP_EXT) ||
790 (dp1->dp_typ == DOSPTYP_EXTLBA)) {
792 * breadth first traversal, must skip in the
799 * Only load up the in-memory data if we haven't overflowed
800 * our in-memory array.
802 if (od->od_nslices < NEXTDOSPART) {
803 dp2->dp_typ = dp1->dp_typ;
804 dp2->dp_start = base + offset + dp1->dp_start;
805 dp2->dp_size = dp1->dp_size;
812 * Pass 2 - handle extended partitions. Note that the extended
813 * slice itself is not stored in the slice array when we recurse,
814 * but any 'original' top-level extended slices are. This is to
815 * match what the kernel does.
818 for (i = 0; i < NDOSPART; ++i, ++dp1) {
819 if (dp1->dp_scyl == 0 && dp1->dp_shd == 0 &&
820 dp1->dp_ssect == 0 && dp1->dp_start == 0 &&
824 if ((dp1->dp_typ == DOSPTYP_EXT) ||
825 (dp1->dp_typ == DOSPTYP_EXTLBA)) {
826 bd_chainextended(od, base, dp1->dp_start);
833 * Search for a slice with the following preferences:
835 * 1: Active FreeBSD slice
836 * 2: Non-active FreeBSD slice
837 * 3: Active Linux slice
838 * 4: non-active Linux slice
839 * 5: Active FAT/FAT32 slice
840 * 6: non-active FAT/FAT32 slice
842 #define PREF_RAWDISK 0
843 #define PREF_FBSD_ACT 1
845 #define PREF_LINUX_ACT 3
847 #define PREF_DOS_ACT 5
852 * slicelimit is in the range 0 .. NDOSPART
855 bd_bestslice(struct open_disk *od)
857 struct dos_partition *dp;
862 preflevel = PREF_NONE;
864 dp = &od->od_slicetab[0];
865 for (i = 0; i < od->od_nslices; i++, dp++) {
866 switch (dp->dp_typ) {
867 case DOSPTYP_386BSD: /* FreeBSD */
868 pref = dp->dp_flag & 0x80 ? PREF_FBSD_ACT : PREF_FBSD;
872 pref = dp->dp_flag & 0x80 ? PREF_LINUX_ACT : PREF_LINUX;
875 case 0x01: /* DOS/Windows */
881 pref = dp->dp_flag & 0x80 ? PREF_DOS_ACT : PREF_DOS;
887 if (pref < preflevel) {
896 bd_close(struct open_file *f)
898 struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)(f->f_devdata))->d_kind.biosdisk.data);
905 bd_closedisk(struct open_disk *od)
907 DEBUG("open_disk %p", od);
909 /* XXX is this required? (especially if disk already open...) */
910 if (od->od_flags & BD_FLOPPY)
917 bd_strategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
919 struct bcache_devdata bcd;
920 struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)devdata)->d_kind.biosdisk.data);
922 bcd.dv_strategy = bd_realstrategy;
923 bcd.dv_devdata = devdata;
924 return(bcache_strategy(&bcd, od->od_unit, rw, dblk+od->od_boff, size, buf, rsize));
928 bd_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
930 struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)devdata)->d_kind.biosdisk.data);
932 #ifdef BD_SUPPORT_FRAGS
933 char fragbuf[BIOSDISK_SECSIZE];
936 fragsize = size % BIOSDISK_SECSIZE;
938 if (size % BIOSDISK_SECSIZE)
939 panic("bd_strategy: %d bytes I/O not multiple of block size", size);
942 DEBUG("open_disk %p", od);
946 blks = size / BIOSDISK_SECSIZE;
947 DEBUG("read %d from %d to %p", blks, dblk, buf);
951 if (blks && bd_read(od, dblk, blks, buf)) {
955 #ifdef BD_SUPPORT_FRAGS
956 DEBUG("bd_strategy: frag read %d from %d+%d to %p",
957 fragsize, dblk, blks, buf + (blks * BIOSDISK_SECSIZE));
958 if (fragsize && bd_read(od, dblk + blks, 1, fragsize)) {
959 DEBUG("frag read error");
962 bcopy(fragbuf, buf + (blks * BIOSDISK_SECSIZE), fragsize);
968 blks = size / BIOSDISK_SECSIZE;
969 DEBUG("write %d from %d to %p", blks, dblk, buf);
973 if (blks && bd_write(od, dblk, blks, buf)) {
974 DEBUG("write error");
977 #ifdef BD_SUPPORT_FRAGS
979 DEBUG("Attempted to write a frag");
987 break; /* DO NOTHING */
992 /* Max number of sectors to bounce-buffer if the request crosses a 64k boundary */
993 #define FLOPPY_BOUNCEBUF 18
996 bd_read(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest)
998 u_int x, bpc, cyl, hd, sec, result, resid, retry, maxfer;
999 caddr_t p, xp, bbuf, breg;
1001 /* Just in case some idiot actually tries to read -1 blocks... */
1005 bpc = (od->od_sec * od->od_hds); /* blocks per cylinder */
1009 /* Decide whether we have to bounce */
1010 if ((od->od_unit < 0x80) &&
1011 ((VTOP(dest) >> 16) != (VTOP(dest + blks * BIOSDISK_SECSIZE) >> 16))) {
1014 * There is a 64k physical boundary somewhere in the destination buffer, so we have
1015 * to arrange a suitable bounce buffer. Allocate a buffer twice as large as we
1016 * need to. Use the bottom half unless there is a break there, in which case we
1019 x = min(FLOPPY_BOUNCEBUF, (unsigned)blks);
1020 bbuf = malloc(x * 2 * BIOSDISK_SECSIZE);
1021 if (((u_int32_t)VTOP(bbuf) & 0xffff0000) == ((u_int32_t)VTOP(bbuf + x * BIOSDISK_SECSIZE) & 0xffff0000)) {
1024 breg = bbuf + x * BIOSDISK_SECSIZE;
1026 maxfer = x; /* limit transfers to bounce region size */
1034 cyl = x / bpc; /* block # / blocks per cylinder */
1035 x %= bpc; /* block offset into cylinder */
1036 hd = x / od->od_sec; /* offset / blocks per track */
1037 sec = x % od->od_sec; /* offset into track */
1039 /* play it safe and don't cross track boundaries (XXX this is probably unnecessary) */
1040 x = szmin(od->od_sec - sec, resid);
1042 x = min(x, maxfer); /* fit bounce buffer */
1044 /* where do we transfer to? */
1045 xp = bbuf == NULL ? p : breg;
1047 /* correct sector number for 1-based BIOS numbering */
1051 * Loop retrying the operation a couple of times. The BIOS may also
1054 for (retry = 0; retry < 3; retry++) {
1056 * If retrying, reset the drive.
1059 v86.ctl = V86_FLAGS;
1062 v86.edx = od->od_unit;
1067 * Always use EDD if the disk supports it, otherwise fall back
1068 * to CHS mode (returning an error if the cylinder number is
1071 if (od->od_flags & BD_MODEEDD1) {
1072 static unsigned short packet[8];
1076 packet[2] = VTOPOFF(xp);
1077 packet[3] = VTOPSEG(xp);
1078 packet[4] = dblk & 0xffff;
1079 packet[5] = dblk >> 16;
1082 v86.ctl = V86_FLAGS;
1085 v86.edx = od->od_unit;
1086 v86.ds = VTOPSEG(packet);
1087 v86.esi = VTOPOFF(packet);
1089 result = (v86.efl & 0x1);
1092 } else if (cyl < 1024) {
1093 /* Use normal CHS addressing */
1094 v86.ctl = V86_FLAGS;
1096 v86.eax = 0x200 | x;
1097 v86.ecx = ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec;
1098 v86.edx = (hd << 8) | od->od_unit;
1099 v86.es = VTOPSEG(xp);
1100 v86.ebx = VTOPOFF(xp);
1102 result = (v86.efl & 0x1);
1111 DEBUG("%d sectors from %d/%d/%d to %p (0x%x) %s", x, cyl, hd, sec - 1, p, VTOP(p), result ? "failed" : "ok");
1112 /* BUG here, cannot use v86 in printf because putchar uses it too */
1113 DEBUG("ax = 0x%04x cx = 0x%04x dx = 0x%04x status 0x%x",
1114 0x200 | x, ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec, (hd << 8) | od->od_unit, (v86.eax >> 8) & 0xff);
1121 bcopy(breg, p, x * BIOSDISK_SECSIZE);
1122 p += (x * BIOSDISK_SECSIZE);
1127 /* hexdump(dest, (blks * BIOSDISK_SECSIZE)); */
1135 bd_write(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest)
1137 u_int x, bpc, cyl, hd, sec, result, resid, retry, maxfer;
1138 caddr_t p, xp, bbuf, breg;
1140 /* Just in case some idiot actually tries to read -1 blocks... */
1144 bpc = (od->od_sec * od->od_hds); /* blocks per cylinder */
1148 /* Decide whether we have to bounce */
1149 if ((od->od_unit < 0x80) &&
1150 ((VTOP(dest) >> 16) != (VTOP(dest + blks * BIOSDISK_SECSIZE) >> 16))) {
1153 * There is a 64k physical boundary somewhere in the destination buffer, so we have
1154 * to arrange a suitable bounce buffer. Allocate a buffer twice as large as we
1155 * need to. Use the bottom half unless there is a break there, in which case we
1159 x = min(FLOPPY_BOUNCEBUF, (unsigned)blks);
1160 bbuf = malloc(x * 2 * BIOSDISK_SECSIZE);
1161 if (((u_int32_t)VTOP(bbuf) & 0xffff0000) == ((u_int32_t)VTOP(bbuf + x * BIOSDISK_SECSIZE) & 0xffff0000)) {
1164 breg = bbuf + x * BIOSDISK_SECSIZE;
1166 maxfer = x; /* limit transfers to bounce region size */
1174 cyl = x / bpc; /* block # / blocks per cylinder */
1175 x %= bpc; /* block offset into cylinder */
1176 hd = x / od->od_sec; /* offset / blocks per track */
1177 sec = x % od->od_sec; /* offset into track */
1179 /* play it safe and don't cross track boundaries (XXX this is probably unnecessary) */
1180 x = szmin(od->od_sec - sec, resid);
1182 x = szmin(x, maxfer); /* fit bounce buffer */
1184 /* where do we transfer to? */
1185 xp = bbuf == NULL ? p : breg;
1187 /* correct sector number for 1-based BIOS numbering */
1191 /* Put your Data In, Put your Data out,
1192 Put your Data In, and shake it all about
1195 bcopy(p, breg, x * BIOSDISK_SECSIZE);
1196 p += (x * BIOSDISK_SECSIZE);
1201 * Loop retrying the operation a couple of times. The BIOS may also
1204 for (retry = 0; retry < 3; retry++) {
1206 * If retrying, reset the drive.
1209 v86.ctl = V86_FLAGS;
1212 v86.edx = od->od_unit;
1217 * Always use EDD if the disk supports it, otherwise fall back
1218 * to CHS mode (returning an error if the cylinder number is
1221 if (od->od_flags & BD_MODEEDD1) {
1222 static unsigned short packet[8];
1226 packet[2] = VTOPOFF(xp);
1227 packet[3] = VTOPSEG(xp);
1228 packet[4] = dblk & 0xffff;
1229 packet[5] = dblk >> 16;
1232 v86.ctl = V86_FLAGS;
1234 /* Should we Write with verify ?? 0x4302 ? */
1236 v86.edx = od->od_unit;
1237 v86.ds = VTOPSEG(packet);
1238 v86.esi = VTOPOFF(packet);
1240 result = (v86.efl & 0x1);
1243 } else if (cyl < 1024) {
1244 /* Use normal CHS addressing */
1245 v86.ctl = V86_FLAGS;
1247 v86.eax = 0x300 | x;
1248 v86.ecx = ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec;
1249 v86.edx = (hd << 8) | od->od_unit;
1250 v86.es = VTOPSEG(xp);
1251 v86.ebx = VTOPOFF(xp);
1253 result = (v86.efl & 0x1);
1262 DEBUG("%d sectors from %d/%d/%d to %p (0x%x) %s", x, cyl, hd, sec - 1, p, VTOP(p), result ? "failed" : "ok");
1263 /* BUG here, cannot use v86 in printf because putchar uses it too */
1264 DEBUG("ax = 0x%04x cx = 0x%04x dx = 0x%04x status 0x%x",
1265 0x200 | x, ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec, (hd << 8) | od->od_unit, (v86.eax >> 8) & 0xff);
1273 /* hexdump(dest, (blks * BIOSDISK_SECSIZE)); */
1279 bd_getgeom(struct open_disk *od)
1282 v86.ctl = V86_FLAGS;
1285 v86.edx = od->od_unit;
1288 if ((v86.efl & 0x1) || /* carry set */
1289 ((v86.edx & 0xff) <= (unsigned)(od->od_unit & 0x7f))) /* unit # bad */
1292 /* convert max cyl # -> # of cylinders */
1293 od->od_cyl = ((v86.ecx & 0xc0) << 2) + ((v86.ecx & 0xff00) >> 8) + 1;
1294 /* convert max head # -> # of heads */
1295 od->od_hds = ((v86.edx & 0xff00) >> 8) + 1;
1296 od->od_sec = v86.ecx & 0x3f;
1298 DEBUG("unit 0x%x geometry %d/%d/%d", od->od_unit, od->od_cyl, od->od_hds, od->od_sec);
1303 * Return the BIOS geometry of a given "fixed drive" in a format
1304 * suitable for the legacy bootinfo structure. Since the kernel is
1305 * expecting raw int 0x13/0x8 values for N_BIOS_GEOM drives, we
1306 * prefer to get the information directly, rather than rely on being
1307 * able to put it together from information already maintained for
1308 * different purposes and for a probably different number of drives.
1310 * For valid drives, the geometry is expected in the format (31..0)
1311 * "000000cc cccccccc hhhhhhhh 00ssssss"; and invalid drives are
1312 * indicated by returning the geometry of a "1.2M" PC-format floppy
1313 * disk. And, incidentally, what is returned is not the geometry as
1314 * such but the highest valid cylinder, head, and sector numbers.
1317 bd_getbigeom(int bunit)
1320 v86.ctl = V86_FLAGS;
1323 v86.edx = 0x80 + bunit;
1327 return ((v86.ecx & 0xc0) << 18) | ((v86.ecx & 0xff00) << 8) |
1328 (v86.edx & 0xff00) | (v86.ecx & 0x3f);
1332 * Return a suitable cdev_t value for (dev).
1334 * In the case where it looks like (dev) is a SCSI disk, we allow the number of
1335 * IDE disks to be specified in $num_ide_disks. There should be a Better Way.
1338 bd_getdev(struct i386_devdesc *dev)
1340 struct open_disk *od;
1345 int unitofs = 0, i, unit;
1347 biosdev = bd_unit2bios(dev->d_kind.biosdisk.unit);
1348 DEBUG("unit %d BIOS device %d", dev->d_kind.biosdisk.unit, biosdev);
1349 if (biosdev == -1) /* not a BIOS device */
1351 if (bd_opendisk(&od, dev) != 0) /* oops, not a viable device */
1354 if (biosdev < 0x80) {
1355 /* floppy (or emulated floppy) or ATAPI device */
1356 if (bdinfo[dev->d_kind.biosdisk.unit].bd_type == DT_ATAPI) {
1357 /* is an ATAPI disk */
1360 /* is a floppy disk */
1365 if ((od->od_flags & BD_LABELOK) && (od->od_disklabel.d_type == DTYPE_SCSI)) {
1366 /* label OK, disk labelled as SCSI */
1368 /* check for unit number correction hint, now deprecated */
1369 if ((nip = getenv("num_ide_disks")) != NULL) {
1370 i = strtol(nip, &cp, 0);
1371 /* check for parse error */
1372 if ((cp != nip) && (*cp == 0))
1376 /* assume an IDE disk */
1380 /* default root disk unit number */
1381 unit = (biosdev & 0x7f) - unitofs;
1383 /* XXX a better kludge to set the root disk unit number */
1384 if ((nip = getenv("root_disk_unit")) != NULL) {
1385 i = strtol(nip, &cp, 0);
1386 /* check for parse error */
1387 if ((cp != nip) && (*cp == 0))
1391 rootdev = MAKEBOOTDEV(major,
1392 (dev->d_kind.biosdisk.slice + 1) >> 4, /* XXX slices may be wrong here */
1393 (dev->d_kind.biosdisk.slice + 1) & 0xf,
1395 dev->d_kind.biosdisk.partition);
1396 DEBUG("dev is 0x%x\n", rootdev);