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.8 2004/10/24 18:36:05 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/diskmbr.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 struct disklabel od_disklabel;
85 int od_nslices; /* slice count */
86 struct dos_partition od_slicetab[NEXTDOSPART];
90 * List of BIOS devices, translation from disk unit number to
95 int bd_unit; /* BIOS unit number */
97 int bd_type; /* BIOS 'drive type' (floppy only) */
99 static int nbdinfo = 0;
101 static int bd_getgeom(struct open_disk *od);
102 static int bd_read(struct open_disk *od, daddr_t dblk, int blks,
104 static int bd_write(struct open_disk *od, daddr_t dblk, int blks,
107 static int bd_int13probe(struct bdinfo *bd);
109 static void bd_printslice(struct open_disk *od, struct dos_partition *dp,
110 char *prefix, int verbose);
111 static void bd_printbsdslice(struct open_disk *od, daddr_t offset,
112 char *prefix, int verbose);
114 static int bd_init(void);
115 static int bd_strategy(void *devdata, int flag, daddr_t dblk,
116 size_t size, char *buf, size_t *rsize);
117 static int bd_realstrategy(void *devdata, int flag, daddr_t dblk,
118 size_t size, char *buf, size_t *rsize);
119 static int bd_open(struct open_file *f, ...);
120 static int bd_close(struct open_file *f);
121 static void bd_print(int verbose);
123 struct devsw biosdisk = {
135 static int bd_opendisk(struct open_disk **odp, struct i386_devdesc *dev);
136 static void bd_closedisk(struct open_disk *od);
137 static int bd_bestslice(struct open_disk *od);
138 static void bd_checkextended(struct open_disk *od, int slicenum);
141 * Translate between BIOS device numbers and our private unit numbers.
144 bd_bios2unit(int biosdev)
148 DEBUG("looking for bios device 0x%x", biosdev);
149 for (i = 0; i < nbdinfo; i++) {
150 DEBUG("bd unit %d is BIOS device 0x%x", i, bdinfo[i].bd_unit);
151 if (bdinfo[i].bd_unit == biosdev)
158 bd_unit2bios(int unit)
160 if ((unit >= 0) && (unit < nbdinfo))
161 return(bdinfo[unit].bd_unit);
166 * Quiz the BIOS for disk devices, save a little info about them.
171 int base, unit, nfd = 0;
173 /* sequence 0, 0x80 */
174 for (base = 0; base <= 0x80; base += 0x80) {
175 for (unit = base; (nbdinfo < MAXBDDEV); unit++) {
176 /* check the BIOS equipment list for number of fixed disks */
178 (nfd >= *(unsigned char *)PTOV(BIOS_NUMDRIVES)))
181 bdinfo[nbdinfo].bd_unit = unit;
182 bdinfo[nbdinfo].bd_flags = (unit < 0x80) ? BD_FLOPPY : 0;
184 if (!bd_int13probe(&bdinfo[nbdinfo]))
187 /* XXX we need "disk aliases" to make this simpler */
188 printf("BIOS drive %c: is disk%d\n",
189 (unit < 0x80) ? ('A' + unit) : ('C' + unit - 0x80), nbdinfo);
199 * Try to detect a device supported by the legacy int13 BIOS
202 bd_int13probe(struct bdinfo *bd)
207 v86.edx = bd->bd_unit;
210 if (!(v86.efl & 0x1) && /* carry clear */
211 ((v86.edx & 0xff) > ((unsigned)bd->bd_unit & 0x7f))) { /* unit # OK */
214 * Ignore devices with an absurd sector size.
216 if ((v86.ecx & 0x3f) == 0) {
217 DEBUG("Invalid geometry for unit %d", bd->bd_unit);
220 bd->bd_flags |= BD_MODEINT13;
221 bd->bd_type = v86.ebx & 0xff;
223 /* Determine if we can use EDD with this device. */
225 v86.edx = bd->bd_unit;
228 if (!(v86.efl & 0x1) && /* carry clear */
229 ((v86.ebx & 0xffff) == 0xaa55) && /* signature */
230 (v86.ecx & 0x1)) { /* packets mode ok */
231 bd->bd_flags |= BD_MODEEDD1;
232 if((v86.eax & 0xff00) > 0x300)
233 bd->bd_flags |= BD_MODEEDD3;
241 * Print information about disks
244 bd_print(int verbose)
248 struct i386_devdesc dev;
249 struct open_disk *od;
250 struct dos_partition *dptr;
252 for (i = 0; i < nbdinfo; i++) {
253 sprintf(line, " disk%d: BIOS drive %c:\n", i,
254 (bdinfo[i].bd_unit < 0x80) ? ('A' + bdinfo[i].bd_unit) : ('C' + bdinfo[i].bd_unit - 0x80));
257 /* try to open the whole disk */
258 dev.d_kind.biosdisk.unit = i;
259 dev.d_kind.biosdisk.slice = -1;
260 dev.d_kind.biosdisk.partition = -1;
262 if (!bd_opendisk(&od, &dev)) {
264 /* Do we have a partition table? */
265 if (od->od_flags & BD_PARTTABOK) {
266 dptr = &od->od_slicetab[0];
268 /* Check for a "dedicated" disk */
269 if ((dptr[3].dp_typ == DOSPTYP_386BSD) &&
270 (dptr[3].dp_start == 0) &&
271 (dptr[3].dp_size == 50000)) {
272 sprintf(line, " disk%d", i);
273 bd_printbsdslice(od, 0, line, verbose);
275 for (j = 0; j < od->od_nslices; j++) {
276 sprintf(line, " disk%ds%d", i, j + 1);
277 bd_printslice(od, &dptr[j], line, verbose);
287 * Print information about slices on a disk. For the size calculations we
288 * assume a 512 byte sector.
291 bd_printslice(struct open_disk *od, struct dos_partition *dp, char *prefix,
296 switch (dp->dp_typ) {
298 bd_printbsdslice(od, (daddr_t)dp->dp_start, prefix, verbose);
302 sprintf(line, "%s: Linux swap %.6dMB (%d - %d)\n",
303 prefix, dp->dp_size / 2048,
304 dp->dp_start, dp->dp_start + dp->dp_size);
306 sprintf(line, "%s: Linux swap\n", prefix);
311 * read the superblock to confirm this is an ext2fs partition?
314 sprintf(line, "%s: ext2fs %.6dMB (%d - %d)\n", prefix,
315 dp->dp_size / 2048, dp->dp_start,
316 dp->dp_start + dp->dp_size);
318 sprintf(line, "%s: ext2fs\n", prefix);
320 case 0x00: /* unused partition */
325 sprintf(line, "%s: FAT-12 %.6dMB (%d - %d)\n", prefix,
326 dp->dp_size / 2048, dp->dp_start,
327 dp->dp_start + dp->dp_size);
329 sprintf(line, "%s: FAT-12\n", prefix);
335 sprintf(line, "%s: FAT-16 %.6dMB (%d - %d)\n", prefix,
336 dp->dp_size / 2048, dp->dp_start,
337 dp->dp_start + dp->dp_size);
339 sprintf(line, "%s: FAT-16\n", prefix);
344 sprintf(line, "%s: FAT-32 %.6dMB (%d - %d)\n", prefix,
345 dp->dp_size / 2048, dp->dp_start,
346 dp->dp_start + dp->dp_size);
348 sprintf(line, "%s: FAT-32\n", prefix);
352 sprintf(line, "%s: Unknown fs: 0x%x %.6dMB (%d - %d)\n",
353 prefix, dp->dp_typ, dp->dp_size / 2048,
354 dp->dp_start, dp->dp_start + dp->dp_size);
356 sprintf(line, "%s: Unknown fs: 0x%x\n", prefix,
363 * Print out each valid partition in the disklabel of a FreeBSD slice.
364 * For size calculations, we assume a 512 byte sector size.
367 bd_printbsdslice(struct open_disk *od, daddr_t offset, char *prefix,
371 char buf[BIOSDISK_SECSIZE];
372 struct disklabel *lp;
376 if (bd_read(od, offset + LABELSECTOR, 1, buf))
378 lp =(struct disklabel *)(&buf[0]);
379 if (lp->d_magic != DISKMAGIC) {
380 sprintf(line, "%s: FFS bad disklabel\n", prefix);
385 /* Print partitions */
386 for (i = 0; i < lp->d_npartitions; i++) {
388 * For each partition, make sure we know what type of fs it is. If
389 * not, then skip it. However, since floppies often have bogus
390 * fstypes, print the 'a' partition on a floppy even if it is marked
393 if ((lp->d_partitions[i].p_fstype == FS_BSDFFS) ||
394 (lp->d_partitions[i].p_fstype == FS_SWAP) ||
395 (lp->d_partitions[i].p_fstype == FS_VINUM) ||
396 ((lp->d_partitions[i].p_fstype == FS_UNUSED) &&
397 (od->od_flags & BD_FLOPPY) && (i == 0))) {
399 /* Only print out statistics in verbose mode */
401 sprintf(line, " %s%c: %s %.6dMB (%d - %d)\n", prefix, 'a' + i,
402 (lp->d_partitions[i].p_fstype == FS_SWAP) ? "swap" :
403 (lp->d_partitions[i].p_fstype == FS_VINUM) ? "vinum" :
405 lp->d_partitions[i].p_size / 2048,
406 lp->d_partitions[i].p_offset,
407 lp->d_partitions[i].p_offset + lp->d_partitions[i].p_size);
409 sprintf(line, " %s%c: %s\n", prefix, 'a' + i,
410 (lp->d_partitions[i].p_fstype == FS_SWAP) ? "swap" :
411 (lp->d_partitions[i].p_fstype == FS_VINUM) ? "vinum" :
420 * Attempt to open the disk described by (dev) for use by (f).
422 * Note that the philosophy here is "give them exactly what
423 * they ask for". This is necessary because being too "smart"
424 * about what the user might want leads to complications.
425 * (eg. given no slice or partition value, with a disk that is
426 * sliced - are they after the first BSD slice, or the DOS
430 bd_open(struct open_file *f, ...)
433 struct i386_devdesc *dev;
434 struct open_disk *od;
438 dev = va_arg(ap, struct i386_devdesc *);
440 if ((error = bd_opendisk(&od, dev)))
446 ((struct i386_devdesc *)(f->f_devdata))->d_kind.biosdisk.data = od;
447 DEBUG("open_disk %p, partition at 0x%x", od, od->od_boff);
452 bd_opendisk(struct open_disk **odp, struct i386_devdesc *dev)
454 struct dos_partition *dptr;
455 struct disklabel *lp;
456 struct open_disk *od;
457 int sector, slice, i;
461 if (dev->d_kind.biosdisk.unit >= nbdinfo) {
462 DEBUG("attempt to open nonexistent disk");
466 od = (struct open_disk *)malloc(sizeof(struct open_disk));
472 /* Look up BIOS unit number, intialise open_disk structure */
473 od->od_dkunit = dev->d_kind.biosdisk.unit;
474 od->od_unit = bdinfo[od->od_dkunit].bd_unit;
475 od->od_flags = bdinfo[od->od_dkunit].bd_flags;
479 DEBUG("open '%s', unit 0x%x slice %d partition %c",
480 i386_fmtdev(dev), dev->d_kind.biosdisk.unit,
481 dev->d_kind.biosdisk.slice, dev->d_kind.biosdisk.partition + 'a');
483 /* Get geometry for this open (removable device may have changed) */
484 if (bd_getgeom(od)) {
485 DEBUG("can't get geometry");
491 * Following calculations attempt to determine the correct value
492 * for d->od_boff by looking for the slice and partition specified,
493 * or searching for reasonable defaults.
497 * Find the slice in the DOS slice table.
499 if (bd_read(od, 0, 1, buf)) {
500 DEBUG("error reading MBR");
506 * Check the slice table magic.
508 if (((u_char)buf[0x1fe] != 0x55) || ((u_char)buf[0x1ff] != 0xaa)) {
509 /* If a slice number was explicitly supplied, this is an error */
510 if (dev->d_kind.biosdisk.slice > 0) {
511 DEBUG("no slice table/MBR (no magic)");
516 goto unsliced; /* may be a floppy */
520 * copy the partition table, then pick up any extended partitions.
522 bcopy(buf + DOSPARTOFF, &od->od_slicetab,
523 sizeof(struct dos_partition) * NDOSPART);
524 od->od_nslices = 4; /* extended slices start here */
525 for (i = 0; i < NDOSPART; i++)
526 bd_checkextended(od, i);
527 od->od_flags |= BD_PARTTABOK;
528 dptr = &od->od_slicetab[0];
530 /* Is this a request for the whole disk? */
531 if (dev->d_kind.biosdisk.slice == -1) {
537 * if a slice number was supplied but not found, this is an error.
539 if (dev->d_kind.biosdisk.slice > 0) {
540 slice = dev->d_kind.biosdisk.slice - 1;
541 if (slice >= od->od_nslices) {
542 DEBUG("slice %d not found", slice);
549 * Check for the historically bogus MBR found on true dedicated disks
551 if ((dptr[3].dp_typ == DOSPTYP_386BSD) &&
552 (dptr[3].dp_start == 0) &&
553 (dptr[3].dp_size == 50000)) {
558 /* Try to auto-detect the best slice; this should always give a slice number */
559 if (dev->d_kind.biosdisk.slice == 0) {
560 slice = bd_bestslice(od);
565 dev->d_kind.biosdisk.slice = slice;
568 dptr = &od->od_slicetab[0];
570 * Accept the supplied slice number unequivocally (we may be looking
571 * at a DOS partition).
573 dptr += (dev->d_kind.biosdisk.slice - 1); /* we number 1-4, offsets are 0-3 */
574 sector = dptr->dp_start;
575 DEBUG("slice entry %d at %d, %d sectors", dev->d_kind.biosdisk.slice - 1, sector, dptr->dp_size);
578 * If we are looking at a BSD slice, and the partition is < 0, assume the 'a' partition
580 if ((dptr->dp_typ == DOSPTYP_386BSD) && (dev->d_kind.biosdisk.partition < 0))
581 dev->d_kind.biosdisk.partition = 0;
585 * Now we have the slice offset, look for the partition in the disklabel if we have
586 * a partition to start with.
588 * XXX we might want to check the label checksum.
590 if (dev->d_kind.biosdisk.partition < 0) {
591 od->od_boff = sector; /* no partition, must be after the slice */
592 DEBUG("opening raw slice");
595 if (bd_read(od, sector + LABELSECTOR, 1, buf)) {
596 DEBUG("error reading disklabel");
600 DEBUG("copy %d bytes of label from %p to %p", sizeof(struct disklabel), buf + LABELOFFSET, &od->od_disklabel);
601 bcopy(buf + LABELOFFSET, &od->od_disklabel, sizeof(struct disklabel));
602 lp = &od->od_disklabel;
603 od->od_flags |= BD_LABELOK;
605 if (lp->d_magic != DISKMAGIC) {
606 DEBUG("no disklabel");
610 if (dev->d_kind.biosdisk.partition >= lp->d_npartitions) {
611 DEBUG("partition '%c' exceeds partitions in table (a-'%c')",
612 'a' + dev->d_kind.biosdisk.partition, 'a' + lp->d_npartitions);
619 /* Complain if the partition is unused unless this is a floppy. */
620 if ((lp->d_partitions[dev->d_kind.biosdisk.partition].p_fstype == FS_UNUSED) &&
621 !(od->od_flags & BD_FLOPPY))
622 DEBUG("warning, partition marked as unused");
626 lp->d_partitions[dev->d_kind.biosdisk.partition].p_offset -
627 lp->d_partitions[RAW_PART].p_offset +
635 *odp = od; /* return the open disk */
641 bd_checkextended(struct open_disk *od, int slicenum)
643 char buf[BIOSDISK_SECSIZE];
644 struct dos_partition *dp;
648 dp = &od->od_slicetab[slicenum];
649 start = od->od_nslices;
651 if (dp->dp_size == 0)
653 if (dp->dp_typ != DOSPTYP_EXT)
655 if (bd_read(od, (daddr_t)dp->dp_start, 1, buf))
657 if (((u_char)buf[0x1fe] != 0x55) || ((u_char)buf[0x1ff] != 0xaa)) {
658 DEBUG("no magic in extended table");
662 dp = (struct dos_partition *)(&buf[DOSPARTOFF]);
663 for (i = 0; i < NDOSPART; i++, dp++) {
664 if (dp->dp_size == 0)
666 if (od->od_nslices == NEXTDOSPART)
668 dp->dp_start += base;
669 bcopy(dp, &od->od_slicetab[od->od_nslices], sizeof(*dp));
672 end = od->od_nslices;
675 * now, recursively check the slices we just added
677 for (i = start; i < end; i++)
678 bd_checkextended(od, i);
684 * Search for a slice with the following preferences:
686 * 1: Active FreeBSD slice
687 * 2: Non-active FreeBSD slice
688 * 3: Active Linux slice
689 * 4: non-active Linux slice
690 * 5: Active FAT/FAT32 slice
691 * 6: non-active FAT/FAT32 slice
693 #define PREF_RAWDISK 0
694 #define PREF_FBSD_ACT 1
696 #define PREF_LINUX_ACT 3
698 #define PREF_DOS_ACT 5
703 * slicelimit is in the range 0 .. NDOSPART
706 bd_bestslice(struct open_disk *od)
708 struct dos_partition *dp;
713 preflevel = PREF_NONE;
715 dp = &od->od_slicetab[0];
716 for (i = 0; i < od->od_nslices; i++, dp++) {
718 switch (dp->dp_typ) {
719 case DOSPTYP_386BSD: /* FreeBSD */
720 pref = dp->dp_flag & 0x80 ? PREF_FBSD_ACT : PREF_FBSD;
724 pref = dp->dp_flag & 0x80 ? PREF_LINUX_ACT : PREF_LINUX;
727 case 0x01: /* DOS/Windows */
733 pref = dp->dp_flag & 0x80 ? PREF_DOS_ACT : PREF_DOS;
739 if (pref < preflevel) {
748 bd_close(struct open_file *f)
750 struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)(f->f_devdata))->d_kind.biosdisk.data);
757 bd_closedisk(struct open_disk *od)
759 DEBUG("open_disk %p", od);
761 /* XXX is this required? (especially if disk already open...) */
762 if (od->od_flags & BD_FLOPPY)
769 bd_strategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
771 struct bcache_devdata bcd;
772 struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)devdata)->d_kind.biosdisk.data);
774 bcd.dv_strategy = bd_realstrategy;
775 bcd.dv_devdata = devdata;
776 return(bcache_strategy(&bcd, od->od_unit, rw, dblk+od->od_boff, size, buf, rsize));
780 bd_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
782 struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)devdata)->d_kind.biosdisk.data);
784 #ifdef BD_SUPPORT_FRAGS
785 char fragbuf[BIOSDISK_SECSIZE];
788 fragsize = size % BIOSDISK_SECSIZE;
790 if (size % BIOSDISK_SECSIZE)
791 panic("bd_strategy: %d bytes I/O not multiple of block size", size);
794 DEBUG("open_disk %p", od);
798 blks = size / BIOSDISK_SECSIZE;
799 DEBUG("read %d from %d to %p", blks, dblk, buf);
803 if (blks && bd_read(od, dblk, blks, buf)) {
807 #ifdef BD_SUPPORT_FRAGS
808 DEBUG("bd_strategy: frag read %d from %d+%d to %p",
809 fragsize, dblk, blks, buf + (blks * BIOSDISK_SECSIZE));
810 if (fragsize && bd_read(od, dblk + blks, 1, fragsize)) {
811 DEBUG("frag read error");
814 bcopy(fragbuf, buf + (blks * BIOSDISK_SECSIZE), fragsize);
820 blks = size / BIOSDISK_SECSIZE;
821 DEBUG("write %d from %d to %p", blks, dblk, buf);
825 if (blks && bd_write(od, dblk, blks, buf)) {
826 DEBUG("write error");
829 #ifdef BD_SUPPORT_FRAGS
831 DEBUG("Attempted to write a frag");
839 break; /* DO NOTHING */
844 /* Max number of sectors to bounce-buffer if the request crosses a 64k boundary */
845 #define FLOPPY_BOUNCEBUF 18
848 bd_read(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest)
850 u_int x, bpc, cyl, hd, sec, result, resid, retry, maxfer;
851 caddr_t p, xp, bbuf, breg;
853 /* Just in case some idiot actually tries to read -1 blocks... */
857 bpc = (od->od_sec * od->od_hds); /* blocks per cylinder */
861 /* Decide whether we have to bounce */
862 if ((od->od_unit < 0x80) &&
863 ((VTOP(dest) >> 16) != (VTOP(dest + blks * BIOSDISK_SECSIZE) >> 16))) {
866 * There is a 64k physical boundary somewhere in the destination buffer, so we have
867 * to arrange a suitable bounce buffer. Allocate a buffer twice as large as we
868 * need to. Use the bottom half unless there is a break there, in which case we
871 x = min(FLOPPY_BOUNCEBUF, (unsigned)blks);
872 bbuf = malloc(x * 2 * BIOSDISK_SECSIZE);
873 if (((u_int32_t)VTOP(bbuf) & 0xffff0000) == ((u_int32_t)VTOP(bbuf + x * BIOSDISK_SECSIZE) & 0xffff0000)) {
876 breg = bbuf + x * BIOSDISK_SECSIZE;
878 maxfer = x; /* limit transfers to bounce region size */
886 cyl = x / bpc; /* block # / blocks per cylinder */
887 x %= bpc; /* block offset into cylinder */
888 hd = x / od->od_sec; /* offset / blocks per track */
889 sec = x % od->od_sec; /* offset into track */
891 /* play it safe and don't cross track boundaries (XXX this is probably unnecessary) */
892 x = min(od->od_sec - sec, resid);
894 x = min(x, maxfer); /* fit bounce buffer */
896 /* where do we transfer to? */
897 xp = bbuf == NULL ? p : breg;
899 /* correct sector number for 1-based BIOS numbering */
903 * Loop retrying the operation a couple of times. The BIOS may also
906 for (retry = 0; retry < 3; retry++) {
908 * If retrying, reset the drive.
914 v86.edx = od->od_unit;
919 * Always use EDD if the disk supports it, otherwise fall back
920 * to CHS mode (returning an error if the cylinder number is
923 if (od->od_flags & BD_MODEEDD1) {
924 static unsigned short packet[8];
928 packet[2] = VTOPOFF(xp);
929 packet[3] = VTOPSEG(xp);
930 packet[4] = dblk & 0xffff;
931 packet[5] = dblk >> 16;
937 v86.edx = od->od_unit;
938 v86.ds = VTOPSEG(packet);
939 v86.esi = VTOPOFF(packet);
941 result = (v86.efl & 0x1);
944 } else if (cyl < 1024) {
945 /* Use normal CHS addressing */
949 v86.ecx = ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec;
950 v86.edx = (hd << 8) | od->od_unit;
951 v86.es = VTOPSEG(xp);
952 v86.ebx = VTOPOFF(xp);
954 result = (v86.efl & 0x1);
963 DEBUG("%d sectors from %d/%d/%d to %p (0x%x) %s", x, cyl, hd, sec - 1, p, VTOP(p), result ? "failed" : "ok");
964 /* BUG here, cannot use v86 in printf because putchar uses it too */
965 DEBUG("ax = 0x%04x cx = 0x%04x dx = 0x%04x status 0x%x",
966 0x200 | x, ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec, (hd << 8) | od->od_unit, (v86.eax >> 8) & 0xff);
973 bcopy(breg, p, x * BIOSDISK_SECSIZE);
974 p += (x * BIOSDISK_SECSIZE);
979 /* hexdump(dest, (blks * BIOSDISK_SECSIZE)); */
987 bd_write(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest)
989 u_int x, bpc, cyl, hd, sec, result, resid, retry, maxfer;
990 caddr_t p, xp, bbuf, breg;
992 /* Just in case some idiot actually tries to read -1 blocks... */
996 bpc = (od->od_sec * od->od_hds); /* blocks per cylinder */
1000 /* Decide whether we have to bounce */
1001 if ((od->od_unit < 0x80) &&
1002 ((VTOP(dest) >> 16) != (VTOP(dest + blks * BIOSDISK_SECSIZE) >> 16))) {
1005 * There is a 64k physical boundary somewhere in the destination buffer, so we have
1006 * to arrange a suitable bounce buffer. Allocate a buffer twice as large as we
1007 * need to. Use the bottom half unless there is a break there, in which case we
1011 x = min(FLOPPY_BOUNCEBUF, (unsigned)blks);
1012 bbuf = malloc(x * 2 * BIOSDISK_SECSIZE);
1013 if (((u_int32_t)VTOP(bbuf) & 0xffff0000) == ((u_int32_t)VTOP(bbuf + x * BIOSDISK_SECSIZE) & 0xffff0000)) {
1016 breg = bbuf + x * BIOSDISK_SECSIZE;
1018 maxfer = x; /* limit transfers to bounce region size */
1026 cyl = x / bpc; /* block # / blocks per cylinder */
1027 x %= bpc; /* block offset into cylinder */
1028 hd = x / od->od_sec; /* offset / blocks per track */
1029 sec = x % od->od_sec; /* offset into track */
1031 /* play it safe and don't cross track boundaries (XXX this is probably unnecessary) */
1032 x = min(od->od_sec - sec, resid);
1034 x = min(x, maxfer); /* fit bounce buffer */
1036 /* where do we transfer to? */
1037 xp = bbuf == NULL ? p : breg;
1039 /* correct sector number for 1-based BIOS numbering */
1043 /* Put your Data In, Put your Data out,
1044 Put your Data In, and shake it all about
1047 bcopy(p, breg, x * BIOSDISK_SECSIZE);
1048 p += (x * BIOSDISK_SECSIZE);
1053 * Loop retrying the operation a couple of times. The BIOS may also
1056 for (retry = 0; retry < 3; retry++) {
1058 * If retrying, reset the drive.
1061 v86.ctl = V86_FLAGS;
1064 v86.edx = od->od_unit;
1069 * Always use EDD if the disk supports it, otherwise fall back
1070 * to CHS mode (returning an error if the cylinder number is
1073 if (od->od_flags & BD_MODEEDD1) {
1074 static unsigned short packet[8];
1078 packet[2] = VTOPOFF(xp);
1079 packet[3] = VTOPSEG(xp);
1080 packet[4] = dblk & 0xffff;
1081 packet[5] = dblk >> 16;
1084 v86.ctl = V86_FLAGS;
1086 /* Should we Write with verify ?? 0x4302 ? */
1088 v86.edx = od->od_unit;
1089 v86.ds = VTOPSEG(packet);
1090 v86.esi = VTOPOFF(packet);
1092 result = (v86.efl & 0x1);
1095 } else if (cyl < 1024) {
1096 /* Use normal CHS addressing */
1097 v86.ctl = V86_FLAGS;
1099 v86.eax = 0x300 | x;
1100 v86.ecx = ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec;
1101 v86.edx = (hd << 8) | od->od_unit;
1102 v86.es = VTOPSEG(xp);
1103 v86.ebx = VTOPOFF(xp);
1105 result = (v86.efl & 0x1);
1114 DEBUG("%d sectors from %d/%d/%d to %p (0x%x) %s", x, cyl, hd, sec - 1, p, VTOP(p), result ? "failed" : "ok");
1115 /* BUG here, cannot use v86 in printf because putchar uses it too */
1116 DEBUG("ax = 0x%04x cx = 0x%04x dx = 0x%04x status 0x%x",
1117 0x200 | x, ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec, (hd << 8) | od->od_unit, (v86.eax >> 8) & 0xff);
1125 /* hexdump(dest, (blks * BIOSDISK_SECSIZE)); */
1131 bd_getgeom(struct open_disk *od)
1134 v86.ctl = V86_FLAGS;
1137 v86.edx = od->od_unit;
1140 if ((v86.efl & 0x1) || /* carry set */
1141 ((v86.edx & 0xff) <= (unsigned)(od->od_unit & 0x7f))) /* unit # bad */
1144 /* convert max cyl # -> # of cylinders */
1145 od->od_cyl = ((v86.ecx & 0xc0) << 2) + ((v86.ecx & 0xff00) >> 8) + 1;
1146 /* convert max head # -> # of heads */
1147 od->od_hds = ((v86.edx & 0xff00) >> 8) + 1;
1148 od->od_sec = v86.ecx & 0x3f;
1150 DEBUG("unit 0x%x geometry %d/%d/%d", od->od_unit, od->od_cyl, od->od_hds, od->od_sec);
1155 * Return the BIOS geometry of a given "fixed drive" in a format
1156 * suitable for the legacy bootinfo structure. Since the kernel is
1157 * expecting raw int 0x13/0x8 values for N_BIOS_GEOM drives, we
1158 * prefer to get the information directly, rather than rely on being
1159 * able to put it together from information already maintained for
1160 * different purposes and for a probably different number of drives.
1162 * For valid drives, the geometry is expected in the format (31..0)
1163 * "000000cc cccccccc hhhhhhhh 00ssssss"; and invalid drives are
1164 * indicated by returning the geometry of a "1.2M" PC-format floppy
1165 * disk. And, incidentally, what is returned is not the geometry as
1166 * such but the highest valid cylinder, head, and sector numbers.
1169 bd_getbigeom(int bunit)
1172 v86.ctl = V86_FLAGS;
1175 v86.edx = 0x80 + bunit;
1179 return ((v86.ecx & 0xc0) << 18) | ((v86.ecx & 0xff00) << 8) |
1180 (v86.edx & 0xff00) | (v86.ecx & 0x3f);
1184 * Return a suitable dev_t value for (dev).
1186 * In the case where it looks like (dev) is a SCSI disk, we allow the number of
1187 * IDE disks to be specified in $num_ide_disks. There should be a Better Way.
1190 bd_getdev(struct i386_devdesc *dev)
1192 struct open_disk *od;
1197 int unitofs = 0, i, unit;
1199 biosdev = bd_unit2bios(dev->d_kind.biosdisk.unit);
1200 DEBUG("unit %d BIOS device %d", dev->d_kind.biosdisk.unit, biosdev);
1201 if (biosdev == -1) /* not a BIOS device */
1203 if (bd_opendisk(&od, dev) != 0) /* oops, not a viable device */
1206 if (biosdev < 0x80) {
1207 /* floppy (or emulated floppy) or ATAPI device */
1208 if (bdinfo[dev->d_kind.biosdisk.unit].bd_type == DT_ATAPI) {
1209 /* is an ATAPI disk */
1212 /* is a floppy disk */
1217 if ((od->od_flags & BD_LABELOK) && (od->od_disklabel.d_type == DTYPE_SCSI)) {
1218 /* label OK, disk labelled as SCSI */
1220 /* check for unit number correction hint, now deprecated */
1221 if ((nip = getenv("num_ide_disks")) != NULL) {
1222 i = strtol(nip, &cp, 0);
1223 /* check for parse error */
1224 if ((cp != nip) && (*cp == 0))
1228 /* assume an IDE disk */
1232 /* default root disk unit number */
1233 unit = (biosdev & 0x7f) - unitofs;
1235 /* XXX a better kludge to set the root disk unit number */
1236 if ((nip = getenv("root_disk_unit")) != NULL) {
1237 i = strtol(nip, &cp, 0);
1238 /* check for parse error */
1239 if ((cp != nip) && (*cp == 0))
1243 rootdev = MAKEBOOTDEV(major,
1244 (dev->d_kind.biosdisk.slice + 1) >> 4, /* XXX slices may be wrong here */
1245 (dev->d_kind.biosdisk.slice + 1) & 0xf,
1247 dev->d_kind.biosdisk.partition);
1248 DEBUG("dev is 0x%x\n", rootdev);