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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
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/diskpc98.h>
44 #include <machine/bootinfo.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" , __func__ , ## 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
84 #define BD_OPTICAL 0x0020
85 struct disklabel od_disklabel;
86 int od_nslices; /* slice count */
87 struct pc98_partition od_slicetab[NDOSPART];
91 * List of BIOS devices, translation from disk unit number to
96 int bd_unit; /* BIOS unit number */
98 int bd_type; /* BIOS 'drive type' (floppy only) */
99 int bd_da_unit; /* kernel unit number for da */
101 static int nbdinfo = 0;
103 static int bd_getgeom(struct open_disk *od);
104 static int bd_read(struct open_disk *od, daddr_t dblk, int blks,
106 static int bd_write(struct open_disk *od, daddr_t dblk, int blks,
109 static int bd_int13probe(struct bdinfo *bd);
111 static void bd_printslice(struct open_disk *od, struct pc98_partition *dp,
112 char *prefix, int verbose);
113 static void bd_printbsdslice(struct open_disk *od, daddr_t offset,
114 char *prefix, int verbose);
116 static int bd_init(void);
117 static int bd_strategy(void *devdata, int flag, daddr_t dblk,
118 size_t size, char *buf, size_t *rsize);
119 static int bd_realstrategy(void *devdata, int flag, daddr_t dblk,
120 size_t size, char *buf, size_t *rsize);
121 static int bd_open(struct open_file *f, ...);
122 static int bd_close(struct open_file *f);
123 static void bd_print(int verbose);
125 struct devsw biosdisk = {
137 static int bd_opendisk(struct open_disk **odp, struct i386_devdesc *dev);
138 static void bd_closedisk(struct open_disk *od);
139 static int bd_open_pc98(struct open_disk *od, struct i386_devdesc *dev);
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.
175 int da_drive=0, n=-0x10;
177 /* sequence 0x90, 0x80, 0xa0 */
178 for (base = 0x90; base <= 0xa0; base += n, n += 0x30) {
179 for (unit = base; (nbdinfo < MAXBDDEV) || ((unit & 0x0f) < 4); unit++) {
180 bdinfo[nbdinfo].bd_unit = unit;
181 bdinfo[nbdinfo].bd_flags = (unit & 0xf0) == 0x90 ? BD_FLOPPY : 0;
183 if (!bd_int13probe(&bdinfo[nbdinfo])){
184 if (((unit & 0xf0) == 0x90 && (unit & 0x0f) < 4) ||
185 ((unit & 0xf0) == 0xa0 && (unit & 0x0f) < 6))
186 continue; /* Target IDs are not contiguous. */
191 if (bdinfo[nbdinfo].bd_flags & BD_FLOPPY){
192 /* available 1.44MB access? */
193 if (*(u_char *)PTOV(0xA15AE) & (1<<(unit & 0xf))) {
194 /* boot media 1.2MB FD? */
195 if ((*(u_char *)PTOV(0xA1584) & 0xf0) != 0x90)
196 bdinfo[nbdinfo].bd_unit = 0x30 + (unit & 0xf);
200 if ((unit & 0xF0) == 0xA0) /* SCSI HD or MO */
201 bdinfo[nbdinfo].bd_da_unit = da_drive++;
203 /* XXX we need "disk aliases" to make this simpler */
204 printf("BIOS drive %c: is disk%d\n",
205 'A' + nbdinfo, nbdinfo);
213 * Try to detect a device supported by the legacy int13 BIOS
216 bd_int13probe(struct bdinfo *bd)
220 if (bd->bd_flags & BD_FLOPPY) {
223 if ((bd->bd_unit & 0xf0) == 0x80)
228 if ( *(u_char *)PTOV(addr) & (1<<(bd->bd_unit & 0x0f))) {
229 bd->bd_flags |= BD_MODEINT13;
232 if ((bd->bd_unit & 0xF0) == 0xA0) {
233 int media = ((unsigned *)PTOV(0xA1460))[bd->bd_unit & 0x0F] & 0x1F;
235 if (media == 7) { /* MO */
236 bd->bd_flags |= BD_MODEINT13 | BD_OPTICAL;
244 * Print information about disks
247 bd_print(int verbose)
251 struct i386_devdesc dev;
252 struct open_disk *od;
253 struct pc98_partition *dptr;
255 for (i = 0; i < nbdinfo; i++) {
256 sprintf(line, " disk%d: BIOS drive %c:\n", i, 'A' + i);
259 /* try to open the whole disk */
261 dev.d_kind.biosdisk.slice = -1;
262 dev.d_kind.biosdisk.partition = -1;
264 if (!bd_opendisk(&od, &dev)) {
266 /* Do we have a partition table? */
267 if (od->od_flags & BD_PARTTABOK) {
268 dptr = &od->od_slicetab[0];
270 /* Check for a "dedicated" disk */
271 for (j = 0; j < od->od_nslices; j++) {
272 switch(dptr[j].dp_mid) {
274 sprintf(line, " disk%ds%d", i, j + 1);
276 dptr[j].dp_scyl * od->od_hds * od->od_sec +
277 dptr[j].dp_shd * od->od_sec + dptr[j].dp_ssect,
291 * Print out each valid partition in the disklabel of a FreeBSD slice.
292 * For size calculations, we assume a 512 byte sector size.
295 bd_printbsdslice(struct open_disk *od, daddr_t offset, char *prefix,
299 char buf[BIOSDISK_SECSIZE];
300 struct disklabel *lp;
304 if (bd_read(od, offset + LABELSECTOR, 1, buf))
306 lp =(struct disklabel *)(&buf[0]);
307 if (lp->d_magic != DISKMAGIC) {
308 sprintf(line, "%s: FFS bad disklabel\n", prefix);
313 /* Print partitions */
314 for (i = 0; i < lp->d_npartitions; i++) {
316 * For each partition, make sure we know what type of fs it is. If
317 * not, then skip it. However, since floppies often have bogus
318 * fstypes, print the 'a' partition on a floppy even if it is marked
321 if ((lp->d_partitions[i].p_fstype == FS_BSDFFS) ||
322 (lp->d_partitions[i].p_fstype == FS_SWAP) ||
323 (lp->d_partitions[i].p_fstype == FS_VINUM) ||
324 ((lp->d_partitions[i].p_fstype == FS_UNUSED) &&
325 (od->od_flags & BD_FLOPPY) && (i == 0))) {
327 /* Only print out statistics in verbose mode */
329 sprintf(line, " %s%c: %s %.6dMB (%d - %d)\n", prefix, 'a' + i,
330 (lp->d_partitions[i].p_fstype == FS_SWAP) ? "swap" :
331 (lp->d_partitions[i].p_fstype == FS_VINUM) ? "vinum" :
333 lp->d_partitions[i].p_size / 2048,
334 lp->d_partitions[i].p_offset,
335 lp->d_partitions[i].p_offset + lp->d_partitions[i].p_size);
337 sprintf(line, " %s%c: %s\n", prefix, 'a' + i,
338 (lp->d_partitions[i].p_fstype == FS_SWAP) ? "swap" :
339 (lp->d_partitions[i].p_fstype == FS_VINUM) ? "vinum" :
348 * Attempt to open the disk described by (dev) for use by (f).
350 * Note that the philosophy here is "give them exactly what
351 * they ask for". This is necessary because being too "smart"
352 * about what the user might want leads to complications.
353 * (eg. given no slice or partition value, with a disk that is
354 * sliced - are they after the first BSD slice, or the DOS
358 bd_open(struct open_file *f, ...)
361 struct i386_devdesc *dev;
362 struct open_disk *od;
366 dev = va_arg(ap, struct i386_devdesc *);
368 if ((error = bd_opendisk(&od, dev)))
374 ((struct i386_devdesc *)(f->f_devdata))->d_kind.biosdisk.data = od;
375 DEBUG("open_disk %p, partition at 0x%x", od, od->od_boff);
380 bd_opendisk(struct open_disk **odp, struct i386_devdesc *dev)
382 struct open_disk *od;
385 if (dev->d_unit >= nbdinfo) {
386 DEBUG("attempt to open nonexistent disk");
390 od = (struct open_disk *)malloc(sizeof(struct open_disk));
396 /* Look up BIOS unit number, intialise open_disk structure */
397 od->od_dkunit = dev->d_unit;
398 od->od_unit = bdinfo[od->od_dkunit].bd_unit;
399 od->od_flags = bdinfo[od->od_dkunit].bd_flags;
402 DEBUG("open '%s', unit 0x%x slice %d partition %d",
403 i386_fmtdev(dev), dev->d_unit,
404 dev->d_kind.biosdisk.slice, dev->d_kind.biosdisk.partition);
406 /* Get geometry for this open (removable device may have changed) */
407 if (bd_getgeom(od)) {
408 DEBUG("can't get geometry");
413 /* Determine disk layout. */
414 error = bd_open_pc98(od, dev);
420 *odp = od; /* return the open disk */
426 bd_open_pc98(struct open_disk *od, struct i386_devdesc *dev)
428 struct pc98_partition *dptr;
429 struct disklabel *lp;
430 int sector, slice, i;
434 * Following calculations attempt to determine the correct value
435 * for d->od_boff by looking for the slice and partition specified,
436 * or searching for reasonable defaults.
440 * Find the slice in the DOS slice table.
443 if (od->od_flags & BD_FLOPPY) {
447 if (bd_read(od, 0, 1, buf)) {
448 DEBUG("error reading MBR");
453 * Check the slice table magic.
455 if (((u_char)buf[0x1fe] != 0x55) || ((u_char)buf[0x1ff] != 0xaa)) {
456 /* If a slice number was explicitly supplied, this is an error */
457 if (dev->d_kind.biosdisk.slice > 0) {
458 DEBUG("no slice table/MBR (no magic)");
462 goto unsliced; /* may be a floppy */
464 if (bd_read(od, 1, 1, buf)) {
465 DEBUG("error reading MBR");
470 * copy the partition table, then pick up any extended partitions.
472 bcopy(buf + DOSPARTOFF, &od->od_slicetab,
473 sizeof(struct pc98_partition) * NDOSPART);
474 od->od_nslices = NDOSPART; /* extended slices start here */
475 od->od_flags |= BD_PARTTABOK;
476 dptr = &od->od_slicetab[0];
478 /* Is this a request for the whole disk? */
479 if (dev->d_kind.biosdisk.slice == -1) {
485 * if a slice number was supplied but not found, this is an error.
487 if (dev->d_kind.biosdisk.slice > 0) {
488 slice = dev->d_kind.biosdisk.slice - 1;
489 if (slice >= od->od_nslices) {
490 DEBUG("slice %d not found", slice);
495 /* Try to auto-detect the best slice; this should always give a slice number */
496 if (dev->d_kind.biosdisk.slice == 0) {
497 slice = bd_bestslice(od);
501 dev->d_kind.biosdisk.slice = slice;
504 dptr = &od->od_slicetab[0];
506 * Accept the supplied slice number unequivocally (we may be looking
507 * at a DOS partition).
509 dptr += (dev->d_kind.biosdisk.slice - 1); /* we number 1-4, offsets are 0-3 */
510 sector = dptr->dp_scyl * od->od_hds * od->od_sec +
511 dptr->dp_shd * od->od_sec + dptr->dp_ssect;
513 int end = dptr->dp_ecyl * od->od_hds * od->od_sec +
514 dptr->dp_ehd * od->od_sec + dptr->dp_esect;
515 DEBUG("slice entry %d at %d, %d sectors",
516 dev->d_kind.biosdisk.slice - 1, sector, end-sector);
520 * If we are looking at a BSD slice, and the partition is < 0, assume the 'a' partition
522 if ((dptr->dp_mid == DOSMID_386BSD) && (dev->d_kind.biosdisk.partition < 0))
523 dev->d_kind.biosdisk.partition = 0;
527 * Now we have the slice offset, look for the partition in the disklabel if we have
528 * a partition to start with.
530 * XXX we might want to check the label checksum.
532 if (dev->d_kind.biosdisk.partition < 0) {
533 od->od_boff = sector; /* no partition, must be after the slice */
534 DEBUG("opening raw slice");
537 if (bd_read(od, sector + LABELSECTOR, 1, buf)) {
538 DEBUG("error reading disklabel");
541 DEBUG("copy %d bytes of label from %p to %p", sizeof(struct disklabel), buf + LABELOFFSET, &od->od_disklabel);
542 bcopy(buf + LABELOFFSET, &od->od_disklabel, sizeof(struct disklabel));
543 lp = &od->od_disklabel;
544 od->od_flags |= BD_LABELOK;
546 if (lp->d_magic != DISKMAGIC) {
547 DEBUG("no disklabel");
550 if (dev->d_kind.biosdisk.partition >= lp->d_npartitions) {
551 DEBUG("partition '%c' exceeds partitions in table (a-'%c')",
552 'a' + dev->d_kind.biosdisk.partition, 'a' + lp->d_npartitions);
557 /* Complain if the partition is unused unless this is a floppy. */
558 if ((lp->d_partitions[dev->d_kind.biosdisk.partition].p_fstype == FS_UNUSED) &&
559 !(od->od_flags & BD_FLOPPY))
560 DEBUG("warning, partition marked as unused");
564 lp->d_partitions[dev->d_kind.biosdisk.partition].p_offset -
565 lp->d_partitions[RAW_PART].p_offset +
572 * Search for a slice with the following preferences:
574 * 1: Active FreeBSD slice
575 * 2: Non-active FreeBSD slice
576 * 3: Active Linux slice
577 * 4: non-active Linux slice
578 * 5: Active FAT/FAT32 slice
579 * 6: non-active FAT/FAT32 slice
581 #define PREF_RAWDISK 0
582 #define PREF_FBSD_ACT 1
584 #define PREF_LINUX_ACT 3
586 #define PREF_DOS_ACT 5
591 * slicelimit is in the range 0 .. NDOSPART
594 bd_bestslice(struct open_disk *od)
596 struct pc98_partition *dp;
601 preflevel = PREF_NONE;
603 dp = &od->od_slicetab[0];
604 for (i = 0; i < od->od_nslices; i++, dp++) {
605 switch(dp->dp_mid & PC98_MID_MASK) {
606 case PC98_MID_386BSD: /* FreeBSD */
607 if ((dp->dp_mid & PC98_MID_BOOTABLE) &&
608 (preflevel > PREF_FBSD_ACT)) {
610 preflevel = PREF_FBSD_ACT;
611 } else if (preflevel > PREF_FBSD) {
613 preflevel = PREF_FBSD;
617 case 0x11: /* DOS/Windows */
623 if ((dp->dp_mid & PC98_MID_BOOTABLE) &&
624 (preflevel > PREF_DOS_ACT)) {
626 preflevel = PREF_DOS_ACT;
627 } else if (preflevel > PREF_DOS) {
629 preflevel = PREF_DOS;
638 bd_close(struct open_file *f)
640 struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)(f->f_devdata))->d_kind.biosdisk.data);
647 bd_closedisk(struct open_disk *od)
649 DEBUG("open_disk %p", od);
651 /* XXX is this required? (especially if disk already open...) */
652 if (od->od_flags & BD_FLOPPY)
659 bd_strategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
661 struct bcache_devdata bcd;
662 struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)devdata)->d_kind.biosdisk.data);
664 bcd.dv_strategy = bd_realstrategy;
665 bcd.dv_devdata = devdata;
666 return(bcache_strategy(&bcd, od->od_unit, rw, dblk+od->od_boff, size, buf, rsize));
670 bd_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
672 struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)devdata)->d_kind.biosdisk.data);
674 #ifdef BD_SUPPORT_FRAGS
675 char fragbuf[BIOSDISK_SECSIZE];
678 fragsize = size % BIOSDISK_SECSIZE;
680 if (size % BIOSDISK_SECSIZE)
681 panic("bd_strategy: %d bytes I/O not multiple of block size", size);
684 DEBUG("open_disk %p", od);
685 blks = size / BIOSDISK_SECSIZE;
691 DEBUG("read %d from %d to %p", blks, dblk, buf);
693 if (blks && bd_read(od, dblk, blks, buf)) {
697 #ifdef BD_SUPPORT_FRAGS
698 DEBUG("bd_strategy: frag read %d from %d+%d to %p",
699 fragsize, dblk, blks, buf + (blks * BIOSDISK_SECSIZE));
700 if (fragsize && bd_read(od, dblk + blks, 1, fragsize)) {
701 DEBUG("frag read error");
704 bcopy(fragbuf, buf + (blks * BIOSDISK_SECSIZE), fragsize);
708 DEBUG("write %d from %d to %p", blks, dblk, buf);
710 if (blks && bd_write(od, dblk, blks, buf)) {
711 DEBUG("write error");
714 #ifdef BD_SUPPORT_FRAGS
716 DEBUG("Attempted to write a frag");
731 /* Max number of sectors to bounce-buffer if the request crosses a 64k boundary */
732 #define FLOPPY_BOUNCEBUF 18
735 bd_chs_io(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest, int write)
737 u_int x, bpc, cyl, hd, sec;
739 bpc = (od->od_sec * od->od_hds); /* blocks per cylinder */
741 cyl = x / bpc; /* block # / blocks per cylinder */
742 x %= bpc; /* block offset into cylinder */
743 hd = x / od->od_sec; /* offset / blocks per track */
744 sec = x % od->od_sec; /* offset into track */
749 v86.eax = 0x0500 | od->od_unit;
751 v86.eax = 0x0600 | od->od_unit;
752 if (od->od_flags & BD_FLOPPY) {
754 v86.ecx = 0x0200 | (cyl & 0xff);
755 v86.edx = (hd << 8) | (sec + 1);
756 } else if (od->od_flags & BD_OPTICAL) {
758 v86.ecx = dblk & 0xFFFF;
759 v86.edx = dblk >> 16;
762 v86.edx = (hd << 8) | sec;
764 v86.ebx = blks * BIOSDISK_SECSIZE;
765 v86.es = VTOPSEG(dest);
766 v86.ebp = VTOPOFF(dest);
768 return (v86.efl & 0x1);
772 bd_io(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest, int write)
774 u_int x, sec, result, resid, retry, maxfer;
775 caddr_t p, xp, bbuf, breg;
777 /* Just in case some idiot actually tries to read/write -1 blocks... */
784 /* Decide whether we have to bounce */
785 if (VTOP(dest) >> 20 != 0 ||
786 ((VTOP(dest) >> 16) != (VTOP(dest + blks * BIOSDISK_SECSIZE) >> 16))) {
789 * There is a 64k physical boundary somewhere in the
790 * destination buffer, or the destination buffer is above
791 * first 1MB of physical memory so we have to arrange a
792 * suitable bounce buffer. Allocate a buffer twice as large
793 * as we need to. Use the bottom half unless there is a break
794 * there, in which case we use the top half.
796 x = min(od->od_sec, (unsigned)blks);
797 bbuf = alloca(x * 2 * BIOSDISK_SECSIZE);
798 if (((u_int32_t)VTOP(bbuf) & 0xffff0000) ==
799 ((u_int32_t)VTOP(bbuf + x * BIOSDISK_SECSIZE) & 0xffff0000)) {
802 breg = bbuf + x * BIOSDISK_SECSIZE;
804 maxfer = x; /* limit transfers to bounce region size */
812 * Play it safe and don't cross track boundaries.
813 * (XXX this is probably unnecessary)
815 sec = dblk % od->od_sec; /* offset into track */
816 x = min(od->od_sec - sec, resid);
818 x = min(x, maxfer); /* fit bounce buffer */
820 /* where do we transfer to? */
821 xp = bbuf == NULL ? p : breg;
824 * Put your Data In, Put your Data out,
825 * Put your Data In, and shake it all about
827 if (write && bbuf != NULL)
828 bcopy(p, breg, x * BIOSDISK_SECSIZE);
831 * Loop retrying the operation a couple of times. The BIOS
834 for (retry = 0; retry < 3; retry++) {
835 /* if retrying, reset the drive */
839 v86.eax = 0x0300 | od->od_unit;
843 result = bd_chs_io(od, dblk, x, xp, write);
849 DEBUG("%d sectors from %lld to %p (0x%x) %s", x, dblk, p, VTOP(p),
850 result ? "failed" : "ok");
852 DEBUG("%d sectors from %p (0x%x) to %lld %s", x, p, VTOP(p), dblk,
853 result ? "failed" : "ok");
857 if (!write && bbuf != NULL)
858 bcopy(breg, p, x * BIOSDISK_SECSIZE);
859 p += (x * BIOSDISK_SECSIZE);
864 /* hexdump(dest, (blks * BIOSDISK_SECSIZE)); */
869 bd_read(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest)
872 return (bd_io(od, dblk, blks, dest, 0));
876 bd_write(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest)
879 return (bd_io(od, dblk, blks, dest, 1));
883 bd_getgeom(struct open_disk *od)
886 if (od->od_flags & BD_FLOPPY) {
889 od->od_sec = (od->od_unit & 0xf0) == 0x30 ? 18 : 15;
890 } else if (od->od_flags & BD_OPTICAL) {
897 v86.eax = 0x8400 | od->od_unit;
900 od->od_cyl = v86.ecx;
901 od->od_hds = (v86.edx >> 8) & 0xff;
902 od->od_sec = v86.edx & 0xff;
907 DEBUG("unit 0x%x geometry %d/%d/%d", od->od_unit, od->od_cyl, od->od_hds, od->od_sec);
912 * Return the BIOS geometry of a given "fixed drive" in a format
913 * suitable for the legacy bootinfo structure. Since the kernel is
914 * expecting raw int 0x13/0x8 values for N_BIOS_GEOM drives, we
915 * prefer to get the information directly, rather than rely on being
916 * able to put it together from information already maintained for
917 * different purposes and for a probably different number of drives.
919 * For valid drives, the geometry is expected in the format (31..0)
920 * "000000cc cccccccc hhhhhhhh 00ssssss"; and invalid drives are
921 * indicated by returning the geometry of a "1.2M" PC-format floppy
922 * disk. And, incidentally, what is returned is not the geometry as
923 * such but the highest valid cylinder, head, and sector numbers.
926 bd_getbigeom(int bunit)
929 int unit = 0x80; /* IDE HDD */
930 u_int addr = 0xA155d;
932 while (unit < 0xa7) {
933 if (*(u_char *)PTOV(addr) & (1 << (unit & 0x0f)))
938 int media = ((unsigned *)PTOV(0xA1460))[unit & 0x0F] & 0x1F;
940 if (media == 7 && hds++ == bunit) /* SCSI MO */
941 return(0xFFFE0820); /* C:65535 H:8 S:32 */
943 if (++unit == 0x84) {
944 unit = 0xA0; /* SCSI HDD */
949 return 0x4F020F; /* 1200KB FD C:80 H:2 S:15 */
952 v86.eax = 0x8400 | unit;
955 return 0x4F020F; /* 1200KB FD C:80 H:2 S:15 */
956 return ((v86.ecx & 0xffff) << 16) | (v86.edx & 0xffff);
960 * Return a suitable dev_t value for (dev).
962 * In the case where it looks like (dev) is a SCSI disk, we allow the number of
963 * IDE disks to be specified in $num_ide_disks. There should be a Better Way.
966 bd_getdev(struct i386_devdesc *dev)
968 struct open_disk *od;
973 int unitofs = 0, i, unit;
975 biosdev = bd_unit2bios(dev->d_unit);
976 DEBUG("unit %d BIOS device %d", dev->d_unit, biosdev);
977 if (biosdev == -1) /* not a BIOS device */
979 if (bd_opendisk(&od, dev) != 0) /* oops, not a viable device */
982 if ((biosdev & 0xf0) == 0x90 || (biosdev & 0xf0) == 0x30) {
983 /* floppy (or emulated floppy) or ATAPI device */
984 if (bdinfo[dev->d_unit].bd_type == DT_ATAPI) {
985 /* is an ATAPI disk */
988 /* is a floppy disk */
993 if ((od->od_flags & BD_LABELOK) && (od->od_disklabel.d_type == DTYPE_SCSI)) {
994 /* label OK, disk labelled as SCSI */
996 /* check for unit number correction hint, now deprecated */
997 if ((nip = getenv("num_ide_disks")) != NULL) {
998 i = strtol(nip, &cp, 0);
999 /* check for parse error */
1000 if ((cp != nip) && (*cp == 0))
1004 /* assume an IDE disk */
1008 /* default root disk unit number */
1009 if ((biosdev & 0xf0) == 0xa0)
1010 unit = bdinfo[dev->d_unit].bd_da_unit;
1012 unit = biosdev & 0xf;
1014 /* XXX a better kludge to set the root disk unit number */
1015 if ((nip = getenv("root_disk_unit")) != NULL) {
1016 i = strtol(nip, &cp, 0);
1017 /* check for parse error */
1018 if ((cp != nip) && (*cp == 0))
1022 rootdev = MAKEBOOTDEV(major, dev->d_kind.biosdisk.slice + 1, unit,
1023 dev->d_kind.biosdisk.partition);
1024 DEBUG("dev is 0x%x\n", rootdev);