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 $
27 * $DragonFly: src/sys/boot/pc98/libpc98/Attic/biosdisk.c,v 1.3 2003/11/09 02:22:33 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/disklabel.h>
43 #include <sys/diskslice.h>
44 #include <sys/reboot.h>
48 #include <bootstrap.h>
52 #define BIOS_NUMDRIVES 0x475
53 #define BIOSDISK_SECSIZE 512
54 #define BUFSIZE (1 * BIOSDISK_SECSIZE)
55 #define MAXBDDEV MAXDEV
57 #define DT_ATAPI 0x10 /* disk type for ATAPI floppies */
58 #define WDMAJOR 0 /* major numbers for devices we frontend for */
64 # define DEBUG(fmt, args...) printf("%s: " fmt "\n" , __FUNCTION__ , ## args)
66 # define DEBUG(fmt, args...)
70 int od_dkunit; /* disk unit number */
71 int od_unit; /* BIOS unit number */
72 int od_cyl; /* BIOS geometry */
75 int od_boff; /* block offset from beginning of BIOS disk */
77 #define BD_MODEINT13 0x0000
78 #define BD_MODEEDD1 0x0001
79 #define BD_MODEEDD3 0x0002
80 #define BD_MODEMASK 0x0003
81 #define BD_FLOPPY 0x0004
82 #define BD_LABELOK 0x0008
83 #define BD_PARTTABOK 0x0010
85 #define BD_OPTICAL 0x0020
87 struct disklabel od_disklabel;
88 int od_nslices; /* slice count */
89 struct dos_partition od_slicetab[MAX_SLICES];
93 * List of BIOS devices, translation from disk unit number to
98 int bd_unit; /* BIOS unit number */
100 int bd_type; /* BIOS 'drive type' (floppy only) */
102 int bd_da_unit; /* kernel unit number for da */
105 static int nbdinfo = 0;
107 static int bd_getgeom(struct open_disk *od);
108 static int bd_read(struct open_disk *od, daddr_t dblk, int blks,
111 static int bd_int13probe(struct bdinfo *bd);
113 static void bd_printslice(struct open_disk *od, struct dos_partition *dp,
114 char *prefix, int verbose);
115 static void bd_printbsdslice(struct open_disk *od, daddr_t offset,
116 char *prefix, int verbose);
118 static int bd_init(void);
119 static int bd_strategy(void *devdata, int flag, daddr_t dblk,
120 size_t size, char *buf, size_t *rsize);
121 static int bd_realstrategy(void *devdata, int flag, daddr_t dblk,
122 size_t size, char *buf, size_t *rsize);
123 static int bd_open(struct open_file *f, ...);
124 static int bd_close(struct open_file *f);
125 static void bd_print(int verbose);
127 struct devsw biosdisk = {
139 static int bd_opendisk(struct open_disk **odp, struct i386_devdesc *dev);
140 static void bd_closedisk(struct open_disk *od);
141 static int bd_bestslice(struct open_disk *od);
142 static void bd_checkextended(struct open_disk *od, int slicenum);
145 * Translate between BIOS device numbers and our private unit numbers.
148 bd_bios2unit(int biosdev)
152 DEBUG("looking for bios device 0x%x", biosdev);
153 for (i = 0; i < nbdinfo; i++) {
154 DEBUG("bd unit %d is BIOS device 0x%x", i, bdinfo[i].bd_unit);
155 if (bdinfo[i].bd_unit == biosdev)
162 bd_unit2bios(int unit)
164 if ((unit >= 0) && (unit < nbdinfo))
165 return(bdinfo[unit].bd_unit);
170 * Quiz the BIOS for disk devices, save a little info about them.
177 int da_drive=0, n=-0x10;
179 /* sequence 0x90, 0x80, 0xa0 */
180 for (base = 0x90; base <= 0xa0; base += n, n += 0x30) {
181 for (unit = base; (nbdinfo < MAXBDDEV) || ((unit & 0x0f) < 4); unit++) {
182 bdinfo[nbdinfo].bd_unit = unit;
183 bdinfo[nbdinfo].bd_flags = (unit & 0xf0) == 0x90 ? BD_FLOPPY : 0;
185 if (!bd_int13probe(&bdinfo[nbdinfo])){
186 if (((unit & 0xf0) == 0x90 && (unit & 0x0f) < 4) ||
187 ((unit & 0xf0) == 0xa0 && (unit & 0x0f) < 6))
188 continue; /* Target IDs are not contiguous. */
193 if (bdinfo[nbdinfo].bd_flags & BD_FLOPPY){
194 /* available 1.44MB access? */
195 if (*(u_char *)PTOV(0xA15AE) & (1<<(unit & 0xf))) {
196 /* boot media 1.2MB FD? */
197 if ((*(u_char *)PTOV(0xA1584) & 0xf0) != 0x90)
198 bdinfo[nbdinfo].bd_unit = 0x30 + (unit & 0xf);
202 if ((unit & 0xF0) == 0xA0) /* SCSI HD or MO */
203 bdinfo[nbdinfo].bd_da_unit = da_drive++;
205 /* XXX we need "disk aliases" to make this simpler */
206 printf("BIOS drive %c: is disk%d\n",
207 'A' + nbdinfo, nbdinfo);
212 int base, unit, nfd = 0;
214 /* sequence 0, 0x80 */
215 for (base = 0; base <= 0x80; base += 0x80) {
216 for (unit = base; (nbdinfo < MAXBDDEV); unit++) {
217 /* check the BIOS equipment list for number of fixed disks */
219 (nfd >= *(unsigned char *)PTOV(BIOS_NUMDRIVES)))
222 bdinfo[nbdinfo].bd_unit = unit;
223 bdinfo[nbdinfo].bd_flags = (unit < 0x80) ? BD_FLOPPY : 0;
225 if (!bd_int13probe(&bdinfo[nbdinfo]))
228 /* XXX we need "disk aliases" to make this simpler */
229 printf("BIOS drive %c: is disk%d\n",
230 (unit < 0x80) ? ('A' + unit) : ('C' + unit - 0x80), nbdinfo);
241 * Try to detect a device supported by the legacy int13 BIOS
244 bd_int13probe(struct bdinfo *bd)
249 if (bd->bd_flags & BD_FLOPPY) {
252 if ((bd->bd_unit & 0xf0) == 0x80)
257 if ( *(u_char *)PTOV(addr) & (1<<(bd->bd_unit & 0x0f))) {
258 bd->bd_flags |= BD_MODEINT13;
261 if ((bd->bd_unit & 0xF0) == 0xA0) {
262 int media = ((unsigned *)PTOV(0xA1460))[bd->bd_unit & 0x0F] & 0x1F;
264 if (media == 7) { /* MO */
265 bd->bd_flags |= BD_MODEINT13 | BD_OPTICAL;
274 v86.edx = bd->bd_unit;
277 if (!(v86.efl & 0x1) && /* carry clear */
278 ((v86.edx & 0xff) > ((unsigned)bd->bd_unit & 0x7f))) { /* unit # OK */
279 bd->bd_flags |= BD_MODEINT13;
280 bd->bd_type = v86.ebx & 0xff;
282 /* Determine if we can use EDD with this device. */
284 v86.edx = bd->bd_unit;
287 if (!(v86.efl & 0x1) && /* carry clear */
288 ((v86.ebx & 0xffff) == 0xaa55) && /* signature */
289 (v86.ecx & 0x1)) { /* packets mode ok */
290 bd->bd_flags |= BD_MODEEDD1;
291 if((v86.eax & 0xff00) > 0x300)
292 bd->bd_flags |= BD_MODEEDD3;
301 * Print information about disks
304 bd_print(int verbose)
308 struct i386_devdesc dev;
309 struct open_disk *od;
310 struct dos_partition *dptr;
312 for (i = 0; i < nbdinfo; i++) {
314 sprintf(line, " disk%d: BIOS drive %c:\n", i, 'A' + i);
316 sprintf(line, " disk%d: BIOS drive %c:\n", i,
317 (bdinfo[i].bd_unit < 0x80) ? ('A' + bdinfo[i].bd_unit) : ('C' + bdinfo[i].bd_unit - 0x80));
321 /* try to open the whole disk */
322 dev.d_kind.biosdisk.unit = i;
323 dev.d_kind.biosdisk.slice = -1;
324 dev.d_kind.biosdisk.partition = -1;
326 if (!bd_opendisk(&od, &dev)) {
328 /* Do we have a partition table? */
329 if (od->od_flags & BD_PARTTABOK) {
330 dptr = &od->od_slicetab[0];
332 /* Check for a "dedicated" disk */
334 for (j = 0; j < od->od_nslices; j++) {
335 switch(dptr[j].dp_mid) {
337 sprintf(line, " disk%ds%d", i, j + 1);
339 dptr[j].dp_scyl * od->od_hds * od->od_sec +
340 dptr[j].dp_shd * od->od_sec + dptr[j].dp_ssect,
347 if ((dptr[3].dp_typ == DOSPTYP_386BSD) &&
348 (dptr[3].dp_start == 0) &&
349 (dptr[3].dp_size == 50000)) {
350 sprintf(line, " disk%d", i);
351 bd_printbsdslice(od, 0, line, verbose);
353 for (j = 0; j < od->od_nslices; j++) {
354 sprintf(line, " disk%ds%d", i, j + 1);
355 bd_printslice(od, &dptr[j], line, verbose);
367 * Print information about slices on a disk. For the size calculations we
368 * assume a 512 byte sector.
371 bd_printslice(struct open_disk *od, struct dos_partition *dp, char *prefix,
376 switch (dp->dp_typ) {
378 bd_printbsdslice(od, (daddr_t)dp->dp_start, prefix, verbose);
382 sprintf(line, "%s: Linux swap %.6dMB (%d - %d)\n",
383 prefix, dp->dp_size / 2048,
384 dp->dp_start, dp->dp_start + dp->dp_size);
386 sprintf(line, "%s: Linux swap\n", prefix);
391 * read the superblock to confirm this is an ext2fs partition?
394 sprintf(line, "%s: ext2fs %.6dMB (%d - %d)\n", prefix,
395 dp->dp_size / 2048, dp->dp_start,
396 dp->dp_start + dp->dp_size);
398 sprintf(line, "%s: ext2fs\n", prefix);
400 case 0x00: /* unused partition */
405 sprintf(line, "%s: FAT-12 %.6dMB (%d - %d)\n", prefix,
406 dp->dp_size / 2048, dp->dp_start,
407 dp->dp_start + dp->dp_size);
409 sprintf(line, "%s: FAT-12\n", prefix);
415 sprintf(line, "%s: FAT-16 %.6dMB (%d - %d)\n", prefix,
416 dp->dp_size / 2048, dp->dp_start,
417 dp->dp_start + dp->dp_size);
419 sprintf(line, "%s: FAT-16\n", prefix);
424 sprintf(line, "%s: FAT-32 %.6dMB (%d - %d)\n", prefix,
425 dp->dp_size / 2048, dp->dp_start,
426 dp->dp_start + dp->dp_size);
428 sprintf(line, "%s: FAT-32\n", prefix);
432 sprintf(line, "%s: Unknown fs: 0x%x %.6dMB (%d - %d)\n",
433 prefix, dp->dp_typ, dp->dp_size / 2048,
434 dp->dp_start, dp->dp_start + dp->dp_size);
436 sprintf(line, "%s: Unknown fs: 0x%x\n", prefix,
444 * Print out each valid partition in the disklabel of a FreeBSD slice.
445 * For size calculations, we assume a 512 byte sector size.
448 bd_printbsdslice(struct open_disk *od, daddr_t offset, char *prefix,
452 char buf[BIOSDISK_SECSIZE];
453 struct disklabel *lp;
457 if (bd_read(od, offset + LABELSECTOR, 1, buf))
459 lp =(struct disklabel *)(&buf[0]);
460 if (lp->d_magic != DISKMAGIC) {
461 sprintf(line, "%s: FFS bad disklabel\n", prefix);
466 /* Print partitions */
467 for (i = 0; i < lp->d_npartitions; i++) {
469 * For each partition, make sure we know what type of fs it is. If
470 * not, then skip it. However, since floppies often have bogus
471 * fstypes, print the 'a' partition on a floppy even if it is marked
474 if ((lp->d_partitions[i].p_fstype == FS_BSDFFS) ||
475 (lp->d_partitions[i].p_fstype == FS_SWAP) ||
476 (lp->d_partitions[i].p_fstype == FS_VINUM) ||
477 ((lp->d_partitions[i].p_fstype == FS_UNUSED) &&
478 (od->od_flags & BD_FLOPPY) && (i == 0))) {
480 /* Only print out statistics in verbose mode */
482 sprintf(line, " %s%c: %s %.6dMB (%d - %d)\n", prefix, 'a' + i,
483 (lp->d_partitions[i].p_fstype == FS_SWAP) ? "swap" :
484 (lp->d_partitions[i].p_fstype == FS_VINUM) ? "vinum" :
486 lp->d_partitions[i].p_size / 2048,
487 lp->d_partitions[i].p_offset,
488 lp->d_partitions[i].p_offset + lp->d_partitions[i].p_size);
490 sprintf(line, " %s%c: %s\n", prefix, 'a' + i,
491 (lp->d_partitions[i].p_fstype == FS_SWAP) ? "swap" :
492 (lp->d_partitions[i].p_fstype == FS_VINUM) ? "vinum" :
501 * Attempt to open the disk described by (dev) for use by (f).
503 * Note that the philosophy here is "give them exactly what
504 * they ask for". This is necessary because being too "smart"
505 * about what the user might want leads to complications.
506 * (eg. given no slice or partition value, with a disk that is
507 * sliced - are they after the first BSD slice, or the DOS
511 bd_open(struct open_file *f, ...)
514 struct i386_devdesc *dev;
515 struct open_disk *od;
519 dev = __va_arg(ap, struct i386_devdesc *);
521 if ((error = bd_opendisk(&od, dev)))
527 ((struct i386_devdesc *)(f->f_devdata))->d_kind.biosdisk.data = od;
528 DEBUG("open_disk %p, partition at 0x%x", od, od->od_boff);
533 bd_opendisk(struct open_disk **odp, struct i386_devdesc *dev)
535 struct dos_partition *dptr;
536 struct disklabel *lp;
537 struct open_disk *od;
538 int sector, slice, i;
542 if (dev->d_kind.biosdisk.unit >= nbdinfo) {
543 DEBUG("attempt to open nonexistent disk");
547 od = (struct open_disk *)malloc(sizeof(struct open_disk));
553 /* Look up BIOS unit number, intialise open_disk structure */
554 od->od_dkunit = dev->d_kind.biosdisk.unit;
555 od->od_unit = bdinfo[od->od_dkunit].bd_unit;
556 od->od_flags = bdinfo[od->od_dkunit].bd_flags;
560 DEBUG("open '%s', unit 0x%x slice %d partition %c",
561 i386_fmtdev(dev), dev->d_kind.biosdisk.unit,
562 dev->d_kind.biosdisk.slice, dev->d_kind.biosdisk.partition + 'a');
564 /* Get geometry for this open (removable device may have changed) */
565 if (bd_getgeom(od)) {
566 DEBUG("can't get geometry");
572 * Following calculations attempt to determine the correct value
573 * for d->od_boff by looking for the slice and partition specified,
574 * or searching for reasonable defaults.
578 * Find the slice in the DOS slice table.
581 if (od->od_flags & BD_FLOPPY) {
586 if (bd_read(od, 0, 1, buf)) {
587 DEBUG("error reading MBR");
593 * Check the slice table magic.
595 if (((u_char)buf[0x1fe] != 0x55) || ((u_char)buf[0x1ff] != 0xaa)) {
596 /* If a slice number was explicitly supplied, this is an error */
597 if (dev->d_kind.biosdisk.slice > 0) {
598 DEBUG("no slice table/MBR (no magic)");
603 goto unsliced; /* may be a floppy */
606 if (bd_read(od, 1, 1, buf)) {
607 DEBUG("error reading MBR");
614 * copy the partition table, then pick up any extended partitions.
616 bcopy(buf + DOSPARTOFF, &od->od_slicetab,
617 sizeof(struct dos_partition) * NDOSPART);
619 od->od_nslices = NDOSPART; /* extended slices start here */
621 od->od_nslices = 4; /* extended slices start here */
622 for (i = 0; i < NDOSPART; i++)
623 bd_checkextended(od, i);
625 od->od_flags |= BD_PARTTABOK;
626 dptr = &od->od_slicetab[0];
628 /* Is this a request for the whole disk? */
629 if (dev->d_kind.biosdisk.slice == -1) {
635 * if a slice number was supplied but not found, this is an error.
637 if (dev->d_kind.biosdisk.slice > 0) {
638 slice = dev->d_kind.biosdisk.slice - 1;
639 if (slice >= od->od_nslices) {
640 DEBUG("slice %d not found", slice);
648 * Check for the historically bogus MBR found on true dedicated disks
650 if ((dptr[3].dp_typ == DOSPTYP_386BSD) &&
651 (dptr[3].dp_start == 0) &&
652 (dptr[3].dp_size == 50000)) {
658 /* Try to auto-detect the best slice; this should always give a slice number */
659 if (dev->d_kind.biosdisk.slice == 0) {
660 slice = bd_bestslice(od);
665 dev->d_kind.biosdisk.slice = slice;
668 dptr = &od->od_slicetab[0];
670 * Accept the supplied slice number unequivocally (we may be looking
671 * at a DOS partition).
673 dptr += (dev->d_kind.biosdisk.slice - 1); /* we number 1-4, offsets are 0-3 */
675 sector = dptr->dp_scyl * od->od_hds * od->od_sec +
676 dptr->dp_shd * od->od_sec + dptr->dp_ssect;
678 int end = dptr->dp_ecyl * od->od_hds * od->od_sec +
679 dptr->dp_ehd * od->od_sec + dptr->dp_esect;
680 DEBUG("slice entry %d at %d, %d sectors",
681 dev->d_kind.biosdisk.slice - 1, sector, end-sector);
684 sector = dptr->dp_start;
685 DEBUG("slice entry %d at %d, %d sectors", dev->d_kind.biosdisk.slice - 1, sector, dptr->dp_size);
689 * If we are looking at a BSD slice, and the partition is < 0, assume the 'a' partition
692 if ((dptr->dp_mid == DOSMID_386BSD) && (dev->d_kind.biosdisk.partition < 0))
694 if ((dptr->dp_typ == DOSPTYP_386BSD) && (dev->d_kind.biosdisk.partition < 0))
696 dev->d_kind.biosdisk.partition = 0;
700 * Now we have the slice offset, look for the partition in the disklabel if we have
701 * a partition to start with.
703 * XXX we might want to check the label checksum.
705 if (dev->d_kind.biosdisk.partition < 0) {
706 od->od_boff = sector; /* no partition, must be after the slice */
707 DEBUG("opening raw slice");
710 if (bd_read(od, sector + LABELSECTOR, 1, buf)) {
711 DEBUG("error reading disklabel");
715 DEBUG("copy %d bytes of label from %p to %p", sizeof(struct disklabel), buf + LABELOFFSET, &od->od_disklabel);
716 bcopy(buf + LABELOFFSET, &od->od_disklabel, sizeof(struct disklabel));
717 lp = &od->od_disklabel;
718 od->od_flags |= BD_LABELOK;
720 if (lp->d_magic != DISKMAGIC) {
721 DEBUG("no disklabel");
725 if (dev->d_kind.biosdisk.partition >= lp->d_npartitions) {
726 DEBUG("partition '%c' exceeds partitions in table (a-'%c')",
727 'a' + dev->d_kind.biosdisk.partition, 'a' + lp->d_npartitions);
734 /* Complain if the partition is unused unless this is a floppy. */
735 if ((lp->d_partitions[dev->d_kind.biosdisk.partition].p_fstype == FS_UNUSED) &&
736 !(od->od_flags & BD_FLOPPY))
737 DEBUG("warning, partition marked as unused");
740 od->od_boff = lp->d_partitions[dev->d_kind.biosdisk.partition].p_offset;
747 *odp = od; /* return the open disk */
754 bd_checkextended(struct open_disk *od, int slicenum)
756 char buf[BIOSDISK_SECSIZE];
757 struct dos_partition *dp;
761 dp = &od->od_slicetab[slicenum];
762 start = od->od_nslices;
764 if (dp->dp_size == 0)
766 if (dp->dp_typ != DOSPTYP_EXT)
768 if (bd_read(od, (daddr_t)dp->dp_start, 1, buf))
770 if (((u_char)buf[0x1fe] != 0x55) || ((u_char)buf[0x1ff] != 0xaa)) {
771 DEBUG("no magic in extended table");
775 dp = (struct dos_partition *)(&buf[DOSPARTOFF]);
776 for (i = 0; i < NDOSPART; i++, dp++) {
777 if (dp->dp_size == 0)
779 if (od->od_nslices == MAX_SLICES)
781 dp->dp_start += base;
782 bcopy(dp, &od->od_slicetab[od->od_nslices], sizeof(*dp));
785 end = od->od_nslices;
788 * now, recursively check the slices we just added
790 for (i = start; i < end; i++)
791 bd_checkextended(od, i);
798 * Search for a slice with the following preferences:
800 * 1: Active FreeBSD slice
801 * 2: Non-active FreeBSD slice
802 * 3: Active Linux slice
803 * 4: non-active Linux slice
804 * 5: Active FAT/FAT32 slice
805 * 6: non-active FAT/FAT32 slice
807 #define PREF_RAWDISK 0
808 #define PREF_FBSD_ACT 1
810 #define PREF_LINUX_ACT 3
812 #define PREF_DOS_ACT 5
817 * slicelimit is in the range 0 .. NDOSPART
820 bd_bestslice(struct open_disk *od)
822 struct dos_partition *dp;
827 preflevel = PREF_NONE;
829 dp = &od->od_slicetab[0];
830 for (i = 0; i < od->od_nslices; i++, dp++) {
832 switch(dp->dp_mid & 0x7f) {
833 case DOSMID_386BSD & 0x7f: /* FreeBSD */
834 if ((dp->dp_mid & 0x80) &&
835 (preflevel > PREF_FBSD_ACT)) {
837 preflevel = PREF_FBSD_ACT;
838 } else if (preflevel > PREF_FBSD) {
840 preflevel = PREF_FBSD;
844 case 0x11: /* DOS/Windows */
850 if ((dp->dp_mid & 0x80) &&
851 (preflevel > PREF_DOS_ACT)) {
853 preflevel = PREF_DOS_ACT;
854 } else if (preflevel > PREF_DOS) {
856 preflevel = PREF_DOS;
861 switch (dp->dp_typ) {
862 case DOSPTYP_386BSD: /* FreeBSD */
863 pref = dp->dp_flag & 0x80 ? PREF_FBSD_ACT : PREF_FBSD;
867 pref = dp->dp_flag & 0x80 ? PREF_LINUX_ACT : PREF_LINUX;
870 case 0x01: /* DOS/Windows */
876 pref = dp->dp_flag & 0x80 ? PREF_DOS_ACT : PREF_DOS;
882 if (pref < preflevel) {
892 bd_close(struct open_file *f)
894 struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)(f->f_devdata))->d_kind.biosdisk.data);
901 bd_closedisk(struct open_disk *od)
903 DEBUG("open_disk %p", od);
905 /* XXX is this required? (especially if disk already open...) */
906 if (od->od_flags & BD_FLOPPY)
913 bd_strategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
915 struct bcache_devdata bcd;
916 struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)devdata)->d_kind.biosdisk.data);
918 bcd.dv_strategy = bd_realstrategy;
919 bcd.dv_devdata = devdata;
920 return(bcache_strategy(&bcd, od->od_unit, rw, dblk+od->od_boff, size, buf, rsize));
924 bd_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
926 struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)devdata)->d_kind.biosdisk.data);
928 #ifdef BD_SUPPORT_FRAGS
929 char fragbuf[BIOSDISK_SECSIZE];
932 fragsize = size % BIOSDISK_SECSIZE;
934 if (size % BIOSDISK_SECSIZE)
935 panic("bd_strategy: %d bytes I/O not multiple of block size", size);
938 DEBUG("open_disk %p", od);
944 blks = size / BIOSDISK_SECSIZE;
945 DEBUG("read %d from %d to %p", blks, dblk, buf);
949 if (blks && bd_read(od, dblk, blks, buf)) {
953 #ifdef BD_SUPPORT_FRAGS
954 DEBUG("bd_strategy: frag read %d from %d+%d to %p",
955 fragsize, dblk, blks, buf + (blks * BIOSDISK_SECSIZE));
956 if (fragsize && bd_read(od, dblk + blks, 1, fragsize)) {
957 DEBUG("frag read error");
960 bcopy(fragbuf, buf + (blks * BIOSDISK_SECSIZE), fragsize);
967 /* Max number of sectors to bounce-buffer if the request crosses a 64k boundary */
968 #define FLOPPY_BOUNCEBUF 18
971 bd_read(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest)
973 u_int x, bpc, cyl, hd, sec, result, resid, retry, maxfer;
974 caddr_t p, xp, bbuf, breg;
976 /* Just in case some idiot actually tries to read -1 blocks... */
980 bpc = (od->od_sec * od->od_hds); /* blocks per cylinder */
984 /* Decide whether we have to bounce */
988 if ((od->od_unit < 0x80) &&
990 ((VTOP(dest) >> 16) != (VTOP(dest + blks * BIOSDISK_SECSIZE) >> 16))) {
993 * There is a 64k physical boundary somewhere in the destination buffer, so we have
994 * to arrange a suitable bounce buffer. Allocate a buffer twice as large as we
995 * need to. Use the bottom half unless there is a break there, in which case we
999 x = min(od->od_sec, (unsigned)blks);
1001 x = min(FLOPPY_BOUNCEBUF, (unsigned)blks);
1003 bbuf = malloc(x * 2 * BIOSDISK_SECSIZE);
1004 if (((u_int32_t)VTOP(bbuf) & 0xffff0000) == ((u_int32_t)VTOP(dest + x * BIOSDISK_SECSIZE) & 0xffff0000)) {
1007 breg = bbuf + x * BIOSDISK_SECSIZE;
1009 maxfer = x; /* limit transfers to bounce region size */
1017 cyl = x / bpc; /* block # / blocks per cylinder */
1018 x %= bpc; /* block offset into cylinder */
1019 hd = x / od->od_sec; /* offset / blocks per track */
1020 sec = x % od->od_sec; /* offset into track */
1022 /* play it safe and don't cross track boundaries (XXX this is probably unnecessary) */
1023 x = min(od->od_sec - sec, resid);
1025 x = min(x, maxfer); /* fit bounce buffer */
1027 /* where do we transfer to? */
1028 xp = bbuf == NULL ? p : breg;
1030 /* correct sector number for 1-based BIOS numbering */
1032 if ((od->od_unit & 0xf0) == 0x30 || (od->od_unit & 0xf0) == 0x90)
1038 /* Loop retrying the operation a couple of times. The BIOS may also retry. */
1039 for (retry = 0; retry < 3; retry++) {
1040 /* if retrying, reset the drive */
1043 v86.ctl = V86_FLAGS;
1045 v86.eax = 0x0300 | od->od_unit;
1047 v86.ctl = V86_FLAGS;
1050 v86.edx = od->od_unit;
1056 v86.ctl = V86_FLAGS;
1058 if (od->od_flags & BD_FLOPPY) {
1059 v86.eax = 0xd600 | od->od_unit;
1060 v86.ecx = 0x0200 | (cyl & 0xff);
1063 v86.eax = 0x0600 | od->od_unit;
1066 if (od->od_flags & BD_OPTICAL) {
1068 v86.ecx = dblk & 0xFFFF;
1069 v86.edx = dblk >> 16;
1071 v86.edx = (hd << 8) | sec;
1073 v86.ebx = x * BIOSDISK_SECSIZE;
1074 v86.es = VTOPSEG(xp);
1075 v86.ebp = VTOPOFF(xp);
1077 result = (v86.efl & 0x1);
1082 /* use EDD if the disk supports it, otherwise, return error */
1083 if(od->od_flags & BD_MODEEDD1) {
1084 static unsigned short packet[8];
1088 packet[2] = VTOPOFF(xp);
1089 packet[3] = VTOPSEG(xp);
1090 packet[4] = dblk & 0xffff;
1091 packet[5] = dblk >> 16;
1094 v86.ctl = V86_FLAGS;
1097 v86.edx = od->od_unit;
1098 v86.ds = VTOPSEG(packet);
1099 v86.esi = VTOPOFF(packet);
1101 result = (v86.efl & 0x1);
1109 /* Use normal CHS addressing */
1110 v86.ctl = V86_FLAGS;
1112 v86.eax = 0x200 | x;
1113 v86.ecx = ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec;
1114 v86.edx = (hd << 8) | od->od_unit;
1115 v86.es = VTOPSEG(xp);
1116 v86.ebx = VTOPOFF(xp);
1118 result = (v86.efl & 0x1);
1126 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");
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",
1129 od->od_flags & BD_FLOPPY ? 0xd600 | od->od_unit : 0x0600 | od->od_unit,
1130 od->od_flags & BD_FLOPPY ? 0x0200 | cyl : cyl, (hd << 8) | sec,
1131 (v86.eax >> 8) & 0xff);
1133 DEBUG("%d sectors from %d/%d/%d to %p (0x%x) %s", x, cyl, hd, sec - 1, p, VTOP(p), result ? "failed" : "ok");
1134 /* BUG here, cannot use v86 in printf because putchar uses it too */
1135 DEBUG("ax = 0x%04x cx = 0x%04x dx = 0x%04x status 0x%x",
1136 0x200 | x, ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec, (hd << 8) | od->od_unit, (v86.eax >> 8) & 0xff);
1144 bcopy(breg, p, x * BIOSDISK_SECSIZE);
1145 p += (x * BIOSDISK_SECSIZE);
1150 /* hexdump(dest, (blks * BIOSDISK_SECSIZE)); */
1157 bd_getgeom(struct open_disk *od)
1161 if (od->od_flags & BD_FLOPPY) {
1164 od->od_sec = (od->od_unit & 0xf0) == 0x30 ? 18 : 15;
1165 } else if (od->od_flags & BD_OPTICAL) {
1166 od->od_cyl = 0xFFFE;
1170 v86.ctl = V86_FLAGS;
1172 v86.eax = 0x8400 | od->od_unit;
1175 od->od_cyl = v86.ecx;
1176 od->od_hds = (v86.edx >> 8) & 0xff;
1177 od->od_sec = v86.edx & 0xff;
1182 v86.ctl = V86_FLAGS;
1185 v86.edx = od->od_unit;
1188 if ((v86.efl & 0x1) || /* carry set */
1189 ((v86.edx & 0xff) <= (unsigned)(od->od_unit & 0x7f))) /* unit # bad */
1192 /* convert max cyl # -> # of cylinders */
1193 od->od_cyl = ((v86.ecx & 0xc0) << 2) + ((v86.ecx & 0xff00) >> 8) + 1;
1194 /* convert max head # -> # of heads */
1195 od->od_hds = ((v86.edx & 0xff00) >> 8) + 1;
1196 od->od_sec = v86.ecx & 0x3f;
1199 DEBUG("unit 0x%x geometry %d/%d/%d", od->od_unit, od->od_cyl, od->od_hds, od->od_sec);
1204 * Return the BIOS geometry of a given "fixed drive" in a format
1205 * suitable for the legacy bootinfo structure. Since the kernel is
1206 * expecting raw int 0x13/0x8 values for N_BIOS_GEOM drives, we
1207 * prefer to get the information directly, rather than rely on being
1208 * able to put it together from information already maintained for
1209 * different purposes and for a probably different number of drives.
1211 * For valid drives, the geometry is expected in the format (31..0)
1212 * "000000cc cccccccc hhhhhhhh 00ssssss"; and invalid drives are
1213 * indicated by returning the geometry of a "1.2M" PC-format floppy
1214 * disk. And, incidentally, what is returned is not the geometry as
1215 * such but the highest valid cylinder, head, and sector numbers.
1218 bd_getbigeom(int bunit)
1223 int unit = 0x80; /* IDE HDD */
1224 u_int addr = 0xA155d;
1226 while (unit < 0xa7) {
1227 if (*(u_char *)PTOV(addr) & (1 << (unit & 0x0f)))
1232 int media = ((unsigned *)PTOV(0xA1460))[unit & 0x0F] & 0x1F;
1234 if (media == 7 && hds++ == bunit) /* SCSI MO */
1235 return(0xFFFE0820); /* C:65535 H:8 S:32 */
1237 if (++unit == 0x84) {
1238 unit = 0xA0; /* SCSI HDD */
1243 return 0x4F020F; /* 1200KB FD C:80 H:2 S:15 */
1244 v86.ctl = V86_FLAGS;
1246 v86.eax = 0x8400 | unit;
1249 return 0x4F020F; /* 1200KB FD C:80 H:2 S:15 */
1250 return ((v86.ecx & 0xffff) << 16) | (v86.edx & 0xffff);
1252 v86.ctl = V86_FLAGS;
1255 v86.edx = 0x80 + bunit;
1259 return ((v86.ecx & 0xc0) << 18) | ((v86.ecx & 0xff00) << 8) |
1260 (v86.edx & 0xff00) | (v86.ecx & 0x3f);
1265 * Return a suitable dev_t value for (dev).
1267 * In the case where it looks like (dev) is a SCSI disk, we allow the number of
1268 * IDE disks to be specified in $num_ide_disks. There should be a Better Way.
1271 bd_getdev(struct i386_devdesc *dev)
1273 struct open_disk *od;
1278 int unitofs = 0, i, unit;
1280 biosdev = bd_unit2bios(dev->d_kind.biosdisk.unit);
1281 DEBUG("unit %d BIOS device %d", dev->d_kind.biosdisk.unit, biosdev);
1282 if (biosdev == -1) /* not a BIOS device */
1284 if (bd_opendisk(&od, dev) != 0) /* oops, not a viable device */
1288 if ((biosdev & 0xf0) == 0x90 || (biosdev & 0xf0) == 0x30) {
1290 if (biosdev < 0x80) {
1292 /* floppy (or emulated floppy) or ATAPI device */
1293 if (bdinfo[dev->d_kind.biosdisk.unit].bd_type == DT_ATAPI) {
1294 /* is an ATAPI disk */
1297 /* is a floppy disk */
1302 if ((od->od_flags & BD_LABELOK) && (od->od_disklabel.d_type == DTYPE_SCSI)) {
1303 /* label OK, disk labelled as SCSI */
1305 /* check for unit number correction hint, now deprecated */
1306 if ((nip = getenv("num_ide_disks")) != NULL) {
1307 i = strtol(nip, &cp, 0);
1308 /* check for parse error */
1309 if ((cp != nip) && (*cp == 0))
1313 /* assume an IDE disk */
1317 /* default root disk unit number */
1319 if ((biosdev & 0xf0) == 0xa0)
1320 unit = bdinfo[dev->d_kind.biosdisk.unit].bd_da_unit;
1322 unit = biosdev & 0xf;
1324 unit = (biosdev & 0x7f) - unitofs;
1327 /* XXX a better kludge to set the root disk unit number */
1328 if ((nip = getenv("root_disk_unit")) != NULL) {
1329 i = strtol(nip, &cp, 0);
1330 /* check for parse error */
1331 if ((cp != nip) && (*cp == 0))
1335 rootdev = MAKEBOOTDEV(major,
1336 (dev->d_kind.biosdisk.slice + 1) >> 4, /* XXX slices may be wrong here */
1337 (dev->d_kind.biosdisk.slice + 1) & 0xf,
1339 dev->d_kind.biosdisk.partition);
1340 DEBUG("dev is 0x%x\n", rootdev);