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/pc98/libpc98/biosdisk.c,v 1.11.2.7 2003/01/13 08:52:53 nyan Exp $
30 * BIOS disk device handling.
32 * Ideas and algorithms from:
34 * - NetBSD libi386/biosdisk.c
35 * - FreeBSD biosboot/disk.c
41 #include <sys/disklabel.h>
42 #include <sys/diskslice.h>
43 #include <sys/reboot.h>
47 #include <bootstrap.h>
51 #define BIOS_NUMDRIVES 0x475
52 #define BIOSDISK_SECSIZE 512
53 #define BUFSIZE (1 * BIOSDISK_SECSIZE)
54 #define MAXBDDEV MAXDEV
56 #define DT_ATAPI 0x10 /* disk type for ATAPI floppies */
57 #define WDMAJOR 0 /* major numbers for devices we frontend for */
63 # define DEBUG(fmt, args...) printf("%s: " fmt "\n" , __FUNCTION__ , ## args)
65 # define DEBUG(fmt, args...)
69 int od_dkunit; /* disk unit number */
70 int od_unit; /* BIOS unit number */
71 int od_cyl; /* BIOS geometry */
74 int od_boff; /* block offset from beginning of BIOS disk */
76 #define BD_MODEINT13 0x0000
77 #define BD_MODEEDD1 0x0001
78 #define BD_MODEEDD3 0x0002
79 #define BD_MODEMASK 0x0003
80 #define BD_FLOPPY 0x0004
81 #define BD_LABELOK 0x0008
82 #define BD_PARTTABOK 0x0010
84 #define BD_OPTICAL 0x0020
86 struct disklabel od_disklabel;
87 int od_nslices; /* slice count */
88 struct dos_partition od_slicetab[MAX_SLICES];
92 * List of BIOS devices, translation from disk unit number to
97 int bd_unit; /* BIOS unit number */
99 int bd_type; /* BIOS 'drive type' (floppy only) */
101 int bd_da_unit; /* kernel unit number for da */
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,
110 static int bd_int13probe(struct bdinfo *bd);
112 static void bd_printslice(struct open_disk *od, struct dos_partition *dp,
113 char *prefix, int verbose);
114 static void bd_printbsdslice(struct open_disk *od, daddr_t offset,
115 char *prefix, int verbose);
117 static int bd_init(void);
118 static int bd_strategy(void *devdata, int flag, daddr_t dblk,
119 size_t size, char *buf, size_t *rsize);
120 static int bd_realstrategy(void *devdata, int flag, daddr_t dblk,
121 size_t size, char *buf, size_t *rsize);
122 static int bd_open(struct open_file *f, ...);
123 static int bd_close(struct open_file *f);
124 static void bd_print(int verbose);
126 struct devsw biosdisk = {
138 static int bd_opendisk(struct open_disk **odp, struct i386_devdesc *dev);
139 static void bd_closedisk(struct open_disk *od);
140 static int bd_bestslice(struct open_disk *od);
141 static void bd_checkextended(struct open_disk *od, int slicenum);
144 * Translate between BIOS device numbers and our private unit numbers.
147 bd_bios2unit(int biosdev)
151 DEBUG("looking for bios device 0x%x", biosdev);
152 for (i = 0; i < nbdinfo; i++) {
153 DEBUG("bd unit %d is BIOS device 0x%x", i, bdinfo[i].bd_unit);
154 if (bdinfo[i].bd_unit == biosdev)
161 bd_unit2bios(int unit)
163 if ((unit >= 0) && (unit < nbdinfo))
164 return(bdinfo[unit].bd_unit);
169 * Quiz the BIOS for disk devices, save a little info about them.
176 int da_drive=0, n=-0x10;
178 /* sequence 0x90, 0x80, 0xa0 */
179 for (base = 0x90; base <= 0xa0; base += n, n += 0x30) {
180 for (unit = base; (nbdinfo < MAXBDDEV) || ((unit & 0x0f) < 4); unit++) {
181 bdinfo[nbdinfo].bd_unit = unit;
182 bdinfo[nbdinfo].bd_flags = (unit & 0xf0) == 0x90 ? BD_FLOPPY : 0;
184 if (!bd_int13probe(&bdinfo[nbdinfo])){
185 if (((unit & 0xf0) == 0x90 && (unit & 0x0f) < 4) ||
186 ((unit & 0xf0) == 0xa0 && (unit & 0x0f) < 6))
187 continue; /* Target IDs are not contiguous. */
192 if (bdinfo[nbdinfo].bd_flags & BD_FLOPPY){
193 /* available 1.44MB access? */
194 if (*(u_char *)PTOV(0xA15AE) & (1<<(unit & 0xf))) {
195 /* boot media 1.2MB FD? */
196 if ((*(u_char *)PTOV(0xA1584) & 0xf0) != 0x90)
197 bdinfo[nbdinfo].bd_unit = 0x30 + (unit & 0xf);
201 if ((unit & 0xF0) == 0xA0) /* SCSI HD or MO */
202 bdinfo[nbdinfo].bd_da_unit = da_drive++;
204 /* XXX we need "disk aliases" to make this simpler */
205 printf("BIOS drive %c: is disk%d\n",
206 'A' + nbdinfo, nbdinfo);
211 int base, unit, nfd = 0;
213 /* sequence 0, 0x80 */
214 for (base = 0; base <= 0x80; base += 0x80) {
215 for (unit = base; (nbdinfo < MAXBDDEV); unit++) {
216 /* check the BIOS equipment list for number of fixed disks */
218 (nfd >= *(unsigned char *)PTOV(BIOS_NUMDRIVES)))
221 bdinfo[nbdinfo].bd_unit = unit;
222 bdinfo[nbdinfo].bd_flags = (unit < 0x80) ? BD_FLOPPY : 0;
224 if (!bd_int13probe(&bdinfo[nbdinfo]))
227 /* XXX we need "disk aliases" to make this simpler */
228 printf("BIOS drive %c: is disk%d\n",
229 (unit < 0x80) ? ('A' + unit) : ('C' + unit - 0x80), nbdinfo);
240 * Try to detect a device supported by the legacy int13 BIOS
243 bd_int13probe(struct bdinfo *bd)
248 if (bd->bd_flags & BD_FLOPPY) {
251 if ((bd->bd_unit & 0xf0) == 0x80)
256 if ( *(u_char *)PTOV(addr) & (1<<(bd->bd_unit & 0x0f))) {
257 bd->bd_flags |= BD_MODEINT13;
260 if ((bd->bd_unit & 0xF0) == 0xA0) {
261 int media = ((unsigned *)PTOV(0xA1460))[bd->bd_unit & 0x0F] & 0x1F;
263 if (media == 7) { /* MO */
264 bd->bd_flags |= BD_MODEINT13 | BD_OPTICAL;
273 v86.edx = bd->bd_unit;
276 if (!(v86.efl & 0x1) && /* carry clear */
277 ((v86.edx & 0xff) > ((unsigned)bd->bd_unit & 0x7f))) { /* unit # OK */
278 bd->bd_flags |= BD_MODEINT13;
279 bd->bd_type = v86.ebx & 0xff;
281 /* Determine if we can use EDD with this device. */
283 v86.edx = bd->bd_unit;
286 if (!(v86.efl & 0x1) && /* carry clear */
287 ((v86.ebx & 0xffff) == 0xaa55) && /* signature */
288 (v86.ecx & 0x1)) { /* packets mode ok */
289 bd->bd_flags |= BD_MODEEDD1;
290 if((v86.eax & 0xff00) > 0x300)
291 bd->bd_flags |= BD_MODEEDD3;
300 * Print information about disks
303 bd_print(int verbose)
307 struct i386_devdesc dev;
308 struct open_disk *od;
309 struct dos_partition *dptr;
311 for (i = 0; i < nbdinfo; i++) {
313 sprintf(line, " disk%d: BIOS drive %c:\n", i, 'A' + i);
315 sprintf(line, " disk%d: BIOS drive %c:\n", i,
316 (bdinfo[i].bd_unit < 0x80) ? ('A' + bdinfo[i].bd_unit) : ('C' + bdinfo[i].bd_unit - 0x80));
320 /* try to open the whole disk */
321 dev.d_kind.biosdisk.unit = i;
322 dev.d_kind.biosdisk.slice = -1;
323 dev.d_kind.biosdisk.partition = -1;
325 if (!bd_opendisk(&od, &dev)) {
327 /* Do we have a partition table? */
328 if (od->od_flags & BD_PARTTABOK) {
329 dptr = &od->od_slicetab[0];
331 /* Check for a "dedicated" disk */
333 for (j = 0; j < od->od_nslices; j++) {
334 switch(dptr[j].dp_mid) {
336 sprintf(line, " disk%ds%d", i, j + 1);
338 dptr[j].dp_scyl * od->od_hds * od->od_sec +
339 dptr[j].dp_shd * od->od_sec + dptr[j].dp_ssect,
346 if ((dptr[3].dp_typ == DOSPTYP_386BSD) &&
347 (dptr[3].dp_start == 0) &&
348 (dptr[3].dp_size == 50000)) {
349 sprintf(line, " disk%d", i);
350 bd_printbsdslice(od, 0, line, verbose);
352 for (j = 0; j < od->od_nslices; j++) {
353 sprintf(line, " disk%ds%d", i, j + 1);
354 bd_printslice(od, &dptr[j], line, verbose);
366 * Print information about slices on a disk. For the size calculations we
367 * assume a 512 byte sector.
370 bd_printslice(struct open_disk *od, struct dos_partition *dp, char *prefix,
375 switch (dp->dp_typ) {
377 bd_printbsdslice(od, (daddr_t)dp->dp_start, prefix, verbose);
381 sprintf(line, "%s: Linux swap %.6dMB (%d - %d)\n",
382 prefix, dp->dp_size / 2048,
383 dp->dp_start, dp->dp_start + dp->dp_size);
385 sprintf(line, "%s: Linux swap\n", prefix);
390 * read the superblock to confirm this is an ext2fs partition?
393 sprintf(line, "%s: ext2fs %.6dMB (%d - %d)\n", prefix,
394 dp->dp_size / 2048, dp->dp_start,
395 dp->dp_start + dp->dp_size);
397 sprintf(line, "%s: ext2fs\n", prefix);
399 case 0x00: /* unused partition */
404 sprintf(line, "%s: FAT-12 %.6dMB (%d - %d)\n", prefix,
405 dp->dp_size / 2048, dp->dp_start,
406 dp->dp_start + dp->dp_size);
408 sprintf(line, "%s: FAT-12\n", prefix);
414 sprintf(line, "%s: FAT-16 %.6dMB (%d - %d)\n", prefix,
415 dp->dp_size / 2048, dp->dp_start,
416 dp->dp_start + dp->dp_size);
418 sprintf(line, "%s: FAT-16\n", prefix);
423 sprintf(line, "%s: FAT-32 %.6dMB (%d - %d)\n", prefix,
424 dp->dp_size / 2048, dp->dp_start,
425 dp->dp_start + dp->dp_size);
427 sprintf(line, "%s: FAT-32\n", prefix);
431 sprintf(line, "%s: Unknown fs: 0x%x %.6dMB (%d - %d)\n",
432 prefix, dp->dp_typ, dp->dp_size / 2048,
433 dp->dp_start, dp->dp_start + dp->dp_size);
435 sprintf(line, "%s: Unknown fs: 0x%x\n", prefix,
443 * Print out each valid partition in the disklabel of a FreeBSD slice.
444 * For size calculations, we assume a 512 byte sector size.
447 bd_printbsdslice(struct open_disk *od, daddr_t offset, char *prefix,
451 char buf[BIOSDISK_SECSIZE];
452 struct disklabel *lp;
456 if (bd_read(od, offset + LABELSECTOR, 1, buf))
458 lp =(struct disklabel *)(&buf[0]);
459 if (lp->d_magic != DISKMAGIC) {
460 sprintf(line, "%s: FFS bad disklabel\n", prefix);
465 /* Print partitions */
466 for (i = 0; i < lp->d_npartitions; i++) {
468 * For each partition, make sure we know what type of fs it is. If
469 * not, then skip it. However, since floppies often have bogus
470 * fstypes, print the 'a' partition on a floppy even if it is marked
473 if ((lp->d_partitions[i].p_fstype == FS_BSDFFS) ||
474 (lp->d_partitions[i].p_fstype == FS_SWAP) ||
475 (lp->d_partitions[i].p_fstype == FS_VINUM) ||
476 ((lp->d_partitions[i].p_fstype == FS_UNUSED) &&
477 (od->od_flags & BD_FLOPPY) && (i == 0))) {
479 /* Only print out statistics in verbose mode */
481 sprintf(line, " %s%c: %s %.6dMB (%d - %d)\n", prefix, 'a' + i,
482 (lp->d_partitions[i].p_fstype == FS_SWAP) ? "swap" :
483 (lp->d_partitions[i].p_fstype == FS_VINUM) ? "vinum" :
485 lp->d_partitions[i].p_size / 2048,
486 lp->d_partitions[i].p_offset,
487 lp->d_partitions[i].p_offset + lp->d_partitions[i].p_size);
489 sprintf(line, " %s%c: %s\n", prefix, 'a' + i,
490 (lp->d_partitions[i].p_fstype == FS_SWAP) ? "swap" :
491 (lp->d_partitions[i].p_fstype == FS_VINUM) ? "vinum" :
500 * Attempt to open the disk described by (dev) for use by (f).
502 * Note that the philosophy here is "give them exactly what
503 * they ask for". This is necessary because being too "smart"
504 * about what the user might want leads to complications.
505 * (eg. given no slice or partition value, with a disk that is
506 * sliced - are they after the first BSD slice, or the DOS
510 bd_open(struct open_file *f, ...)
513 struct i386_devdesc *dev;
514 struct open_disk *od;
518 dev = va_arg(ap, struct i386_devdesc *);
520 if ((error = bd_opendisk(&od, dev)))
526 ((struct i386_devdesc *)(f->f_devdata))->d_kind.biosdisk.data = od;
527 DEBUG("open_disk %p, partition at 0x%x", od, od->od_boff);
532 bd_opendisk(struct open_disk **odp, struct i386_devdesc *dev)
534 struct dos_partition *dptr;
535 struct disklabel *lp;
536 struct open_disk *od;
537 int sector, slice, i;
541 if (dev->d_kind.biosdisk.unit >= nbdinfo) {
542 DEBUG("attempt to open nonexistent disk");
546 od = (struct open_disk *)malloc(sizeof(struct open_disk));
552 /* Look up BIOS unit number, intialise open_disk structure */
553 od->od_dkunit = dev->d_kind.biosdisk.unit;
554 od->od_unit = bdinfo[od->od_dkunit].bd_unit;
555 od->od_flags = bdinfo[od->od_dkunit].bd_flags;
559 DEBUG("open '%s', unit 0x%x slice %d partition %c",
560 i386_fmtdev(dev), dev->d_kind.biosdisk.unit,
561 dev->d_kind.biosdisk.slice, dev->d_kind.biosdisk.partition + 'a');
563 /* Get geometry for this open (removable device may have changed) */
564 if (bd_getgeom(od)) {
565 DEBUG("can't get geometry");
571 * Following calculations attempt to determine the correct value
572 * for d->od_boff by looking for the slice and partition specified,
573 * or searching for reasonable defaults.
577 * Find the slice in the DOS slice table.
580 if (od->od_flags & BD_FLOPPY) {
585 if (bd_read(od, 0, 1, buf)) {
586 DEBUG("error reading MBR");
592 * Check the slice table magic.
594 if (((u_char)buf[0x1fe] != 0x55) || ((u_char)buf[0x1ff] != 0xaa)) {
595 /* If a slice number was explicitly supplied, this is an error */
596 if (dev->d_kind.biosdisk.slice > 0) {
597 DEBUG("no slice table/MBR (no magic)");
602 goto unsliced; /* may be a floppy */
605 if (bd_read(od, 1, 1, buf)) {
606 DEBUG("error reading MBR");
613 * copy the partition table, then pick up any extended partitions.
615 bcopy(buf + DOSPARTOFF, &od->od_slicetab,
616 sizeof(struct dos_partition) * NDOSPART);
618 od->od_nslices = NDOSPART; /* extended slices start here */
620 od->od_nslices = 4; /* extended slices start here */
621 for (i = 0; i < NDOSPART; i++)
622 bd_checkextended(od, i);
624 od->od_flags |= BD_PARTTABOK;
625 dptr = &od->od_slicetab[0];
627 /* Is this a request for the whole disk? */
628 if (dev->d_kind.biosdisk.slice == -1) {
634 * if a slice number was supplied but not found, this is an error.
636 if (dev->d_kind.biosdisk.slice > 0) {
637 slice = dev->d_kind.biosdisk.slice - 1;
638 if (slice >= od->od_nslices) {
639 DEBUG("slice %d not found", slice);
647 * Check for the historically bogus MBR found on true dedicated disks
649 if ((dptr[3].dp_typ == DOSPTYP_386BSD) &&
650 (dptr[3].dp_start == 0) &&
651 (dptr[3].dp_size == 50000)) {
657 /* Try to auto-detect the best slice; this should always give a slice number */
658 if (dev->d_kind.biosdisk.slice == 0) {
659 slice = bd_bestslice(od);
664 dev->d_kind.biosdisk.slice = slice;
667 dptr = &od->od_slicetab[0];
669 * Accept the supplied slice number unequivocally (we may be looking
670 * at a DOS partition).
672 dptr += (dev->d_kind.biosdisk.slice - 1); /* we number 1-4, offsets are 0-3 */
674 sector = dptr->dp_scyl * od->od_hds * od->od_sec +
675 dptr->dp_shd * od->od_sec + dptr->dp_ssect;
677 int end = dptr->dp_ecyl * od->od_hds * od->od_sec +
678 dptr->dp_ehd * od->od_sec + dptr->dp_esect;
679 DEBUG("slice entry %d at %d, %d sectors",
680 dev->d_kind.biosdisk.slice - 1, sector, end-sector);
683 sector = dptr->dp_start;
684 DEBUG("slice entry %d at %d, %d sectors", dev->d_kind.biosdisk.slice - 1, sector, dptr->dp_size);
688 * If we are looking at a BSD slice, and the partition is < 0, assume the 'a' partition
691 if ((dptr->dp_mid == DOSMID_386BSD) && (dev->d_kind.biosdisk.partition < 0))
693 if ((dptr->dp_typ == DOSPTYP_386BSD) && (dev->d_kind.biosdisk.partition < 0))
695 dev->d_kind.biosdisk.partition = 0;
699 * Now we have the slice offset, look for the partition in the disklabel if we have
700 * a partition to start with.
702 * XXX we might want to check the label checksum.
704 if (dev->d_kind.biosdisk.partition < 0) {
705 od->od_boff = sector; /* no partition, must be after the slice */
706 DEBUG("opening raw slice");
709 if (bd_read(od, sector + LABELSECTOR, 1, buf)) {
710 DEBUG("error reading disklabel");
714 DEBUG("copy %d bytes of label from %p to %p", sizeof(struct disklabel), buf + LABELOFFSET, &od->od_disklabel);
715 bcopy(buf + LABELOFFSET, &od->od_disklabel, sizeof(struct disklabel));
716 lp = &od->od_disklabel;
717 od->od_flags |= BD_LABELOK;
719 if (lp->d_magic != DISKMAGIC) {
720 DEBUG("no disklabel");
724 if (dev->d_kind.biosdisk.partition >= lp->d_npartitions) {
725 DEBUG("partition '%c' exceeds partitions in table (a-'%c')",
726 'a' + dev->d_kind.biosdisk.partition, 'a' + lp->d_npartitions);
733 /* Complain if the partition is unused unless this is a floppy. */
734 if ((lp->d_partitions[dev->d_kind.biosdisk.partition].p_fstype == FS_UNUSED) &&
735 !(od->od_flags & BD_FLOPPY))
736 DEBUG("warning, partition marked as unused");
739 od->od_boff = lp->d_partitions[dev->d_kind.biosdisk.partition].p_offset;
746 *odp = od; /* return the open disk */
753 bd_checkextended(struct open_disk *od, int slicenum)
755 char buf[BIOSDISK_SECSIZE];
756 struct dos_partition *dp;
760 dp = &od->od_slicetab[slicenum];
761 start = od->od_nslices;
763 if (dp->dp_size == 0)
765 if (dp->dp_typ != DOSPTYP_EXT)
767 if (bd_read(od, (daddr_t)dp->dp_start, 1, buf))
769 if (((u_char)buf[0x1fe] != 0x55) || ((u_char)buf[0x1ff] != 0xaa)) {
770 DEBUG("no magic in extended table");
774 dp = (struct dos_partition *)(&buf[DOSPARTOFF]);
775 for (i = 0; i < NDOSPART; i++, dp++) {
776 if (dp->dp_size == 0)
778 if (od->od_nslices == MAX_SLICES)
780 dp->dp_start += base;
781 bcopy(dp, &od->od_slicetab[od->od_nslices], sizeof(*dp));
784 end = od->od_nslices;
787 * now, recursively check the slices we just added
789 for (i = start; i < end; i++)
790 bd_checkextended(od, i);
797 * Search for a slice with the following preferences:
799 * 1: Active FreeBSD slice
800 * 2: Non-active FreeBSD slice
801 * 3: Active Linux slice
802 * 4: non-active Linux slice
803 * 5: Active FAT/FAT32 slice
804 * 6: non-active FAT/FAT32 slice
806 #define PREF_RAWDISK 0
807 #define PREF_FBSD_ACT 1
809 #define PREF_LINUX_ACT 3
811 #define PREF_DOS_ACT 5
816 * slicelimit is in the range 0 .. NDOSPART
819 bd_bestslice(struct open_disk *od)
821 struct dos_partition *dp;
826 preflevel = PREF_NONE;
828 dp = &od->od_slicetab[0];
829 for (i = 0; i < od->od_nslices; i++, dp++) {
831 switch(dp->dp_mid & 0x7f) {
832 case DOSMID_386BSD & 0x7f: /* FreeBSD */
833 if ((dp->dp_mid & 0x80) &&
834 (preflevel > PREF_FBSD_ACT)) {
836 preflevel = PREF_FBSD_ACT;
837 } else if (preflevel > PREF_FBSD) {
839 preflevel = PREF_FBSD;
843 case 0x11: /* DOS/Windows */
849 if ((dp->dp_mid & 0x80) &&
850 (preflevel > PREF_DOS_ACT)) {
852 preflevel = PREF_DOS_ACT;
853 } else if (preflevel > PREF_DOS) {
855 preflevel = PREF_DOS;
860 switch (dp->dp_typ) {
861 case DOSPTYP_386BSD: /* FreeBSD */
862 pref = dp->dp_flag & 0x80 ? PREF_FBSD_ACT : PREF_FBSD;
866 pref = dp->dp_flag & 0x80 ? PREF_LINUX_ACT : PREF_LINUX;
869 case 0x01: /* DOS/Windows */
875 pref = dp->dp_flag & 0x80 ? PREF_DOS_ACT : PREF_DOS;
881 if (pref < preflevel) {
891 bd_close(struct open_file *f)
893 struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)(f->f_devdata))->d_kind.biosdisk.data);
900 bd_closedisk(struct open_disk *od)
902 DEBUG("open_disk %p", od);
904 /* XXX is this required? (especially if disk already open...) */
905 if (od->od_flags & BD_FLOPPY)
912 bd_strategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
914 struct bcache_devdata bcd;
915 struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)devdata)->d_kind.biosdisk.data);
917 bcd.dv_strategy = bd_realstrategy;
918 bcd.dv_devdata = devdata;
919 return(bcache_strategy(&bcd, od->od_unit, rw, dblk+od->od_boff, size, buf, rsize));
923 bd_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
925 struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)devdata)->d_kind.biosdisk.data);
927 #ifdef BD_SUPPORT_FRAGS
928 char fragbuf[BIOSDISK_SECSIZE];
931 fragsize = size % BIOSDISK_SECSIZE;
933 if (size % BIOSDISK_SECSIZE)
934 panic("bd_strategy: %d bytes I/O not multiple of block size", size);
937 DEBUG("open_disk %p", od);
943 blks = size / BIOSDISK_SECSIZE;
944 DEBUG("read %d from %d to %p", blks, dblk, buf);
948 if (blks && bd_read(od, dblk, blks, buf)) {
952 #ifdef BD_SUPPORT_FRAGS
953 DEBUG("bd_strategy: frag read %d from %d+%d to %p",
954 fragsize, dblk, blks, buf + (blks * BIOSDISK_SECSIZE));
955 if (fragsize && bd_read(od, dblk + blks, 1, fragsize)) {
956 DEBUG("frag read error");
959 bcopy(fragbuf, buf + (blks * BIOSDISK_SECSIZE), fragsize);
966 /* Max number of sectors to bounce-buffer if the request crosses a 64k boundary */
967 #define FLOPPY_BOUNCEBUF 18
970 bd_read(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest)
972 u_int x, bpc, cyl, hd, sec, result, resid, retry, maxfer;
973 caddr_t p, xp, bbuf, breg;
975 /* Just in case some idiot actually tries to read -1 blocks... */
979 bpc = (od->od_sec * od->od_hds); /* blocks per cylinder */
983 /* Decide whether we have to bounce */
987 if ((od->od_unit < 0x80) &&
989 ((VTOP(dest) >> 16) != (VTOP(dest + blks * BIOSDISK_SECSIZE) >> 16))) {
992 * There is a 64k physical boundary somewhere in the destination buffer, so we have
993 * to arrange a suitable bounce buffer. Allocate a buffer twice as large as we
994 * need to. Use the bottom half unless there is a break there, in which case we
998 x = min(od->od_sec, (unsigned)blks);
1000 x = min(FLOPPY_BOUNCEBUF, (unsigned)blks);
1002 bbuf = malloc(x * 2 * BIOSDISK_SECSIZE);
1003 if (((u_int32_t)VTOP(bbuf) & 0xffff0000) == ((u_int32_t)VTOP(dest + x * BIOSDISK_SECSIZE) & 0xffff0000)) {
1006 breg = bbuf + x * BIOSDISK_SECSIZE;
1008 maxfer = x; /* limit transfers to bounce region size */
1016 cyl = x / bpc; /* block # / blocks per cylinder */
1017 x %= bpc; /* block offset into cylinder */
1018 hd = x / od->od_sec; /* offset / blocks per track */
1019 sec = x % od->od_sec; /* offset into track */
1021 /* play it safe and don't cross track boundaries (XXX this is probably unnecessary) */
1022 x = min(od->od_sec - sec, resid);
1024 x = min(x, maxfer); /* fit bounce buffer */
1026 /* where do we transfer to? */
1027 xp = bbuf == NULL ? p : breg;
1029 /* correct sector number for 1-based BIOS numbering */
1031 if ((od->od_unit & 0xf0) == 0x30 || (od->od_unit & 0xf0) == 0x90)
1037 /* Loop retrying the operation a couple of times. The BIOS may also retry. */
1038 for (retry = 0; retry < 3; retry++) {
1039 /* if retrying, reset the drive */
1042 v86.ctl = V86_FLAGS;
1044 v86.eax = 0x0300 | od->od_unit;
1046 v86.ctl = V86_FLAGS;
1049 v86.edx = od->od_unit;
1055 v86.ctl = V86_FLAGS;
1057 if (od->od_flags & BD_FLOPPY) {
1058 v86.eax = 0xd600 | od->od_unit;
1059 v86.ecx = 0x0200 | (cyl & 0xff);
1062 v86.eax = 0x0600 | od->od_unit;
1065 if (od->od_flags & BD_OPTICAL) {
1067 v86.ecx = dblk & 0xFFFF;
1068 v86.edx = dblk >> 16;
1070 v86.edx = (hd << 8) | sec;
1072 v86.ebx = x * BIOSDISK_SECSIZE;
1073 v86.es = VTOPSEG(xp);
1074 v86.ebp = VTOPOFF(xp);
1076 result = (v86.efl & 0x1);
1081 /* use EDD if the disk supports it, otherwise, return error */
1082 if(od->od_flags & BD_MODEEDD1) {
1083 static unsigned short packet[8];
1087 packet[2] = VTOPOFF(xp);
1088 packet[3] = VTOPSEG(xp);
1089 packet[4] = dblk & 0xffff;
1090 packet[5] = dblk >> 16;
1093 v86.ctl = V86_FLAGS;
1096 v86.edx = od->od_unit;
1097 v86.ds = VTOPSEG(packet);
1098 v86.esi = VTOPOFF(packet);
1100 result = (v86.efl & 0x1);
1108 /* Use normal CHS addressing */
1109 v86.ctl = V86_FLAGS;
1111 v86.eax = 0x200 | x;
1112 v86.ecx = ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec;
1113 v86.edx = (hd << 8) | od->od_unit;
1114 v86.es = VTOPSEG(xp);
1115 v86.ebx = VTOPOFF(xp);
1117 result = (v86.efl & 0x1);
1125 DEBUG("%d sectors from %d/%d/%d to %p (0x%x) %s", x, cyl, hd, od->od_flags & BD_FLOPPY ? sec - 1 : sec, p, VTOP(p), result ? "failed" : "ok");
1126 /* BUG here, cannot use v86 in printf because putchar uses it too */
1127 DEBUG("ax = 0x%04x cx = 0x%04x dx = 0x%04x status 0x%x",
1128 od->od_flags & BD_FLOPPY ? 0xd600 | od->od_unit : 0x0600 | od->od_unit,
1129 od->od_flags & BD_FLOPPY ? 0x0200 | cyl : cyl, (hd << 8) | sec,
1130 (v86.eax >> 8) & 0xff);
1132 DEBUG("%d sectors from %d/%d/%d to %p (0x%x) %s", x, cyl, hd, sec - 1, p, VTOP(p), result ? "failed" : "ok");
1133 /* BUG here, cannot use v86 in printf because putchar uses it too */
1134 DEBUG("ax = 0x%04x cx = 0x%04x dx = 0x%04x status 0x%x",
1135 0x200 | x, ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec, (hd << 8) | od->od_unit, (v86.eax >> 8) & 0xff);
1143 bcopy(breg, p, x * BIOSDISK_SECSIZE);
1144 p += (x * BIOSDISK_SECSIZE);
1149 /* hexdump(dest, (blks * BIOSDISK_SECSIZE)); */
1156 bd_getgeom(struct open_disk *od)
1160 if (od->od_flags & BD_FLOPPY) {
1163 od->od_sec = (od->od_unit & 0xf0) == 0x30 ? 18 : 15;
1164 } else if (od->od_flags & BD_OPTICAL) {
1165 od->od_cyl = 0xFFFE;
1169 v86.ctl = V86_FLAGS;
1171 v86.eax = 0x8400 | od->od_unit;
1174 od->od_cyl = v86.ecx;
1175 od->od_hds = (v86.edx >> 8) & 0xff;
1176 od->od_sec = v86.edx & 0xff;
1181 v86.ctl = V86_FLAGS;
1184 v86.edx = od->od_unit;
1187 if ((v86.efl & 0x1) || /* carry set */
1188 ((v86.edx & 0xff) <= (unsigned)(od->od_unit & 0x7f))) /* unit # bad */
1191 /* convert max cyl # -> # of cylinders */
1192 od->od_cyl = ((v86.ecx & 0xc0) << 2) + ((v86.ecx & 0xff00) >> 8) + 1;
1193 /* convert max head # -> # of heads */
1194 od->od_hds = ((v86.edx & 0xff00) >> 8) + 1;
1195 od->od_sec = v86.ecx & 0x3f;
1198 DEBUG("unit 0x%x geometry %d/%d/%d", od->od_unit, od->od_cyl, od->od_hds, od->od_sec);
1203 * Return the BIOS geometry of a given "fixed drive" in a format
1204 * suitable for the legacy bootinfo structure. Since the kernel is
1205 * expecting raw int 0x13/0x8 values for N_BIOS_GEOM drives, we
1206 * prefer to get the information directly, rather than rely on being
1207 * able to put it together from information already maintained for
1208 * different purposes and for a probably different number of drives.
1210 * For valid drives, the geometry is expected in the format (31..0)
1211 * "000000cc cccccccc hhhhhhhh 00ssssss"; and invalid drives are
1212 * indicated by returning the geometry of a "1.2M" PC-format floppy
1213 * disk. And, incidentally, what is returned is not the geometry as
1214 * such but the highest valid cylinder, head, and sector numbers.
1217 bd_getbigeom(int bunit)
1222 int unit = 0x80; /* IDE HDD */
1223 u_int addr = 0xA155d;
1225 while (unit < 0xa7) {
1226 if (*(u_char *)PTOV(addr) & (1 << (unit & 0x0f)))
1231 int media = ((unsigned *)PTOV(0xA1460))[unit & 0x0F] & 0x1F;
1233 if (media == 7 && hds++ == bunit) /* SCSI MO */
1234 return(0xFFFE0820); /* C:65535 H:8 S:32 */
1236 if (++unit == 0x84) {
1237 unit = 0xA0; /* SCSI HDD */
1242 return 0x4F020F; /* 1200KB FD C:80 H:2 S:15 */
1243 v86.ctl = V86_FLAGS;
1245 v86.eax = 0x8400 | unit;
1248 return 0x4F020F; /* 1200KB FD C:80 H:2 S:15 */
1249 return ((v86.ecx & 0xffff) << 16) | (v86.edx & 0xffff);
1251 v86.ctl = V86_FLAGS;
1254 v86.edx = 0x80 + bunit;
1258 return ((v86.ecx & 0xc0) << 18) | ((v86.ecx & 0xff00) << 8) |
1259 (v86.edx & 0xff00) | (v86.ecx & 0x3f);
1264 * Return a suitable dev_t value for (dev).
1266 * In the case where it looks like (dev) is a SCSI disk, we allow the number of
1267 * IDE disks to be specified in $num_ide_disks. There should be a Better Way.
1270 bd_getdev(struct i386_devdesc *dev)
1272 struct open_disk *od;
1277 int unitofs = 0, i, unit;
1279 biosdev = bd_unit2bios(dev->d_kind.biosdisk.unit);
1280 DEBUG("unit %d BIOS device %d", dev->d_kind.biosdisk.unit, biosdev);
1281 if (biosdev == -1) /* not a BIOS device */
1283 if (bd_opendisk(&od, dev) != 0) /* oops, not a viable device */
1287 if ((biosdev & 0xf0) == 0x90 || (biosdev & 0xf0) == 0x30) {
1289 if (biosdev < 0x80) {
1291 /* floppy (or emulated floppy) or ATAPI device */
1292 if (bdinfo[dev->d_kind.biosdisk.unit].bd_type == DT_ATAPI) {
1293 /* is an ATAPI disk */
1296 /* is a floppy disk */
1301 if ((od->od_flags & BD_LABELOK) && (od->od_disklabel.d_type == DTYPE_SCSI)) {
1302 /* label OK, disk labelled as SCSI */
1304 /* check for unit number correction hint, now deprecated */
1305 if ((nip = getenv("num_ide_disks")) != NULL) {
1306 i = strtol(nip, &cp, 0);
1307 /* check for parse error */
1308 if ((cp != nip) && (*cp == 0))
1312 /* assume an IDE disk */
1316 /* default root disk unit number */
1318 if ((biosdev & 0xf0) == 0xa0)
1319 unit = bdinfo[dev->d_kind.biosdisk.unit].bd_da_unit;
1321 unit = biosdev & 0xf;
1323 unit = (biosdev & 0x7f) - unitofs;
1326 /* XXX a better kludge to set the root disk unit number */
1327 if ((nip = getenv("root_disk_unit")) != NULL) {
1328 i = strtol(nip, &cp, 0);
1329 /* check for parse error */
1330 if ((cp != nip) && (*cp == 0))
1334 rootdev = MAKEBOOTDEV(major,
1335 (dev->d_kind.biosdisk.slice + 1) >> 4, /* XXX slices may be wrong here */
1336 (dev->d_kind.biosdisk.slice + 1) & 0xf,
1338 dev->d_kind.biosdisk.partition);
1339 DEBUG("dev is 0x%x\n", rootdev);