f61649e7b942846f5964b231d0c04e412628fc9d
[dragonfly.git] / sys / boot / pc98 / libpc98 / biosdisk.c
1 /*-
2  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
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.2 2003/06/17 04:28:18 dillon Exp $
28  */
29
30 /*
31  * BIOS disk device handling.
32  * 
33  * Ideas and algorithms from:
34  *
35  * - NetBSD libi386/biosdisk.c
36  * - FreeBSD biosboot/disk.c
37  *
38  */
39
40 #include <stand.h>
41
42 #include <sys/disklabel.h>
43 #include <sys/diskslice.h>
44 #include <sys/reboot.h>
45
46 #include <stdarg.h>
47
48 #include <bootstrap.h>
49 #include <btxv86.h>
50 #include "libi386.h"
51
52 #define BIOS_NUMDRIVES          0x475
53 #define BIOSDISK_SECSIZE        512
54 #define BUFSIZE                 (1 * BIOSDISK_SECSIZE)
55 #define MAXBDDEV                MAXDEV
56
57 #define DT_ATAPI                0x10            /* disk type for ATAPI floppies */
58 #define WDMAJOR                 0               /* major numbers for devices we frontend for */
59 #define WFDMAJOR                1
60 #define FDMAJOR                 2
61 #define DAMAJOR                 4
62
63 #ifdef DISK_DEBUG
64 # define DEBUG(fmt, args...)    printf("%s: " fmt "\n" , __FUNCTION__ , ## args)
65 #else
66 # define DEBUG(fmt, args...)
67 #endif
68
69 struct open_disk {
70     int                 od_dkunit;              /* disk unit number */
71     int                 od_unit;                /* BIOS unit number */
72     int                 od_cyl;                 /* BIOS geometry */
73     int                 od_hds;
74     int                 od_sec;
75     int                 od_boff;                /* block offset from beginning of BIOS disk */
76     int                 od_flags;
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 #ifdef PC98
85 #define BD_OPTICAL              0x0020
86 #endif
87     struct disklabel            od_disklabel;
88     int                         od_nslices;     /* slice count */
89     struct dos_partition        od_slicetab[MAX_SLICES];
90 };
91
92 /*
93  * List of BIOS devices, translation from disk unit number to
94  * BIOS unit number.
95  */
96 static struct bdinfo
97 {
98     int         bd_unit;                /* BIOS unit number */
99     int         bd_flags;
100     int         bd_type;                /* BIOS 'drive type' (floppy only) */
101 #ifdef PC98
102     int         bd_da_unit;             /* kernel unit number for da */
103 #endif
104 } bdinfo [MAXBDDEV];
105 static int nbdinfo = 0;
106
107 static int      bd_getgeom(struct open_disk *od);
108 static int      bd_read(struct open_disk *od, daddr_t dblk, int blks,
109                     caddr_t dest);
110
111 static int      bd_int13probe(struct bdinfo *bd);
112
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);
117
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);
126
127 struct devsw biosdisk = {
128     "disk", 
129     DEVT_DISK, 
130     bd_init,
131     bd_strategy, 
132     bd_open, 
133     bd_close, 
134     noioctl,
135     bd_print,
136     NULL
137 };
138
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);
143
144 /*
145  * Translate between BIOS device numbers and our private unit numbers.
146  */
147 int
148 bd_bios2unit(int biosdev)
149 {
150     int         i;
151     
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)
156             return(i);
157     }
158     return(-1);
159 }
160
161 int
162 bd_unit2bios(int unit)
163 {
164     if ((unit >= 0) && (unit < nbdinfo))
165         return(bdinfo[unit].bd_unit);
166     return(-1);
167 }
168
169 /*    
170  * Quiz the BIOS for disk devices, save a little info about them.
171  */
172 static int
173 bd_init(void) 
174 {
175 #ifdef PC98
176     int         base, unit;
177     int         da_drive=0, n=-0x10;
178
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;
184
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. */
189                 else
190                     break;
191             }
192
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);
199                 }
200             }
201             else {
202                 if ((unit & 0xF0) == 0xA0)      /* SCSI HD or MO */
203                     bdinfo[nbdinfo].bd_da_unit = da_drive++;
204             }
205             /* XXX we need "disk aliases" to make this simpler */
206             printf("BIOS drive %c: is disk%d\n", 
207                    'A' + nbdinfo, nbdinfo);
208             nbdinfo++;
209         }
210     }
211 #else
212     int         base, unit, nfd = 0;
213
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 */
218             if((base == 0x80) &&
219                (nfd >= *(unsigned char *)PTOV(BIOS_NUMDRIVES)))
220                 break;
221
222             bdinfo[nbdinfo].bd_unit = unit;
223             bdinfo[nbdinfo].bd_flags = (unit < 0x80) ? BD_FLOPPY : 0;
224
225             if (!bd_int13probe(&bdinfo[nbdinfo]))
226                 break;
227
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);
231             nbdinfo++;
232             if (base == 0x80)
233                 nfd++;
234         }
235     }
236 #endif
237     return(0);
238 }
239
240 /*
241  * Try to detect a device supported by the legacy int13 BIOS
242  */
243 static int
244 bd_int13probe(struct bdinfo *bd)
245 {
246 #ifdef PC98
247     int addr;
248
249     if (bd->bd_flags & BD_FLOPPY) {
250         addr = 0xa155c;
251     } else {
252         if ((bd->bd_unit & 0xf0) == 0x80)
253             addr = 0xa155d;
254         else
255             addr = 0xa1482;
256     }
257     if ( *(u_char *)PTOV(addr) & (1<<(bd->bd_unit & 0x0f))) {
258         bd->bd_flags |= BD_MODEINT13;
259         return(1);
260     }
261     if ((bd->bd_unit & 0xF0) == 0xA0) {
262         int media = ((unsigned *)PTOV(0xA1460))[bd->bd_unit & 0x0F] & 0x1F;
263
264         if (media == 7) { /* MO */
265             bd->bd_flags |= BD_MODEINT13 | BD_OPTICAL;
266             return(1);
267         }
268     }
269     return(0);
270 #else
271     v86.ctl = V86_FLAGS;
272     v86.addr = 0x13;
273     v86.eax = 0x800;
274     v86.edx = bd->bd_unit;
275     v86int();
276     
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;
281
282         /* Determine if we can use EDD with this device. */
283         v86.eax = 0x4100;
284         v86.edx = bd->bd_unit;
285         v86.ebx = 0x55aa;
286         v86int();
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;
293         }
294         return(1);
295     }
296     return(0);
297 #endif
298 }
299
300 /*
301  * Print information about disks
302  */
303 static void
304 bd_print(int verbose)
305 {
306     int                         i, j;
307     char                        line[80];
308     struct i386_devdesc         dev;
309     struct open_disk            *od;
310     struct dos_partition        *dptr;
311     
312     for (i = 0; i < nbdinfo; i++) {
313 #ifdef PC98
314         sprintf(line, "    disk%d:   BIOS drive %c:\n", i, 'A' + i);
315 #else
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));
318 #endif
319         pager_output(line);
320
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;
325         
326         if (!bd_opendisk(&od, &dev)) {
327
328             /* Do we have a partition table? */
329             if (od->od_flags & BD_PARTTABOK) {
330                 dptr = &od->od_slicetab[0];
331
332                 /* Check for a "dedicated" disk */
333 #ifdef PC98
334                 for (j = 0; j < od->od_nslices; j++) {
335                     switch(dptr[j].dp_mid) {
336                     case DOSMID_386BSD:
337                         sprintf(line, "      disk%ds%d", i, j + 1);
338                         bd_printbsdslice(od,
339                             dptr[j].dp_scyl * od->od_hds * od->od_sec +
340                             dptr[j].dp_shd * od->od_sec + dptr[j].dp_ssect,
341                             line, verbose);
342                         break;
343                     default:
344                     }
345                 }
346 #else
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);
352                 } else {
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);
356                     }
357                 }
358 #endif
359             }
360             bd_closedisk(od);
361         }
362     }
363 }
364
365 #ifndef PC98
366 /*
367  * Print information about slices on a disk.  For the size calculations we
368  * assume a 512 byte sector.
369  */
370 static void
371 bd_printslice(struct open_disk *od, struct dos_partition *dp, char *prefix,
372         int verbose)
373 {
374         char line[80];
375
376         switch (dp->dp_typ) {
377         case DOSPTYP_386BSD:
378                 bd_printbsdslice(od, (daddr_t)dp->dp_start, prefix, verbose);
379                 return;
380         case DOSPTYP_LINSWP:
381                 if (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);
385                 else
386                         sprintf(line, "%s: Linux swap\n", prefix);
387                 break;
388         case DOSPTYP_LINUX:
389                 /*
390                  * XXX
391                  * read the superblock to confirm this is an ext2fs partition?
392                  */
393                 if (verbose)
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);
397                 else
398                         sprintf(line, "%s: ext2fs\n", prefix);
399                 break;
400         case 0x00:                              /* unused partition */
401         case DOSPTYP_EXT:
402                 return;
403         case 0x01:
404                 if (verbose)
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);
408                 else
409                         sprintf(line, "%s: FAT-12\n", prefix);
410                 break;
411         case 0x04:
412         case 0x06:
413         case 0x0e:
414                 if (verbose)
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);
418                 else
419                         sprintf(line, "%s: FAT-16\n", prefix);
420                 break;
421         case 0x0b:
422         case 0x0c:
423                 if (verbose)
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);
427                 else
428                         sprintf(line, "%s: FAT-32\n", prefix);
429                 break;
430         default:
431                 if (verbose)
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);
435                 else
436                         sprintf(line, "%s: Unknown fs: 0x%x\n", prefix,
437                             dp->dp_typ);
438         }
439         pager_output(line);
440 }
441 #endif
442
443 /*
444  * Print out each valid partition in the disklabel of a FreeBSD slice.
445  * For size calculations, we assume a 512 byte sector size.
446  */
447 static void
448 bd_printbsdslice(struct open_disk *od, daddr_t offset, char *prefix,
449     int verbose)
450 {
451     char                line[80];
452     char                buf[BIOSDISK_SECSIZE];
453     struct disklabel    *lp;
454     int                 i;
455
456     /* read disklabel */
457     if (bd_read(od, offset + LABELSECTOR, 1, buf))
458         return;
459     lp =(struct disklabel *)(&buf[0]);
460     if (lp->d_magic != DISKMAGIC) {
461         sprintf(line, "%s: FFS  bad disklabel\n", prefix);
462         pager_output(line);
463         return;
464     }
465     
466     /* Print partitions */
467     for (i = 0; i < lp->d_npartitions; i++) {
468         /*
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
472          * unused.
473          */
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))) {
479
480             /* Only print out statistics in verbose mode */
481             if (verbose)
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" :
485                     "FFS",
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);
489             else
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" :
493                     "FFS");
494             pager_output(line);
495         }
496     }
497 }
498
499
500 /*
501  * Attempt to open the disk described by (dev) for use by (f).
502  *
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
508  *  slice before it?)
509  */
510 static int 
511 bd_open(struct open_file *f, ...)
512 {
513     va_list                     ap;
514     struct i386_devdesc         *dev;
515     struct open_disk            *od;
516     int                         error;
517
518     va_start(ap, f);
519     dev = va_arg(ap, struct i386_devdesc *);
520     va_end(ap);
521     if ((error = bd_opendisk(&od, dev)))
522         return(error);
523     
524     /*
525      * Save our context
526      */
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);
529     return(0);
530 }
531
532 static int
533 bd_opendisk(struct open_disk **odp, struct i386_devdesc *dev)
534 {
535     struct dos_partition        *dptr;
536     struct disklabel            *lp;
537     struct open_disk            *od;
538     int                         sector, slice, i;
539     int                         error;
540     char                        buf[BUFSIZE];
541
542     if (dev->d_kind.biosdisk.unit >= nbdinfo) {
543         DEBUG("attempt to open nonexistent disk");
544         return(ENXIO);
545     }
546     
547     od = (struct open_disk *)malloc(sizeof(struct open_disk));
548     if (!od) {
549         DEBUG("no memory");
550         return (ENOMEM);
551     }
552
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;
557     od->od_boff = 0;
558     od->od_nslices = 0;
559     error = 0;
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');
563
564     /* Get geometry for this open (removable device may have changed) */
565     if (bd_getgeom(od)) {
566         DEBUG("can't get geometry");
567         error = ENXIO;
568         goto out;
569     }
570
571     /*
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.
575      */
576
577     /*
578      * Find the slice in the DOS slice table.
579      */
580 #ifdef PC98
581     if (od->od_flags & BD_FLOPPY) {
582         sector = 0;
583         goto unsliced;
584     }
585 #endif
586     if (bd_read(od, 0, 1, buf)) {
587         DEBUG("error reading MBR");
588         error = EIO;
589         goto out;
590     }
591
592     /* 
593      * Check the slice table magic.
594      */
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)");
599             error = ENOENT;
600             goto out;
601         }
602         sector = 0;
603         goto unsliced;          /* may be a floppy */
604     }
605 #ifdef PC98
606     if (bd_read(od, 1, 1, buf)) {
607         DEBUG("error reading MBR");
608         error = EIO;
609         goto out;
610     }
611 #endif
612
613     /*
614      * copy the partition table, then pick up any extended partitions.
615      */
616     bcopy(buf + DOSPARTOFF, &od->od_slicetab,
617       sizeof(struct dos_partition) * NDOSPART);
618 #ifdef PC98
619     od->od_nslices = NDOSPART;          /* extended slices start here */
620 #else
621     od->od_nslices = 4;                 /* extended slices start here */
622     for (i = 0; i < NDOSPART; i++)
623         bd_checkextended(od, i);
624 #endif
625     od->od_flags |= BD_PARTTABOK;
626     dptr = &od->od_slicetab[0];
627
628     /* Is this a request for the whole disk? */
629     if (dev->d_kind.biosdisk.slice == -1) {
630         sector = 0;
631         goto unsliced;
632     }
633
634     /*
635      * if a slice number was supplied but not found, this is an error.
636      */
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);
641             error = ENOENT;
642             goto out;
643         }
644     }
645
646 #ifndef PC98
647     /*
648      * Check for the historically bogus MBR found on true dedicated disks
649      */
650     if ((dptr[3].dp_typ == DOSPTYP_386BSD) &&
651       (dptr[3].dp_start == 0) &&
652       (dptr[3].dp_size == 50000)) {
653         sector = 0;
654         goto unsliced;
655     }
656 #endif
657
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);
661         if (slice == -1) {
662             error = ENOENT;
663             goto out;
664         }
665         dev->d_kind.biosdisk.slice = slice;
666     }
667
668     dptr = &od->od_slicetab[0];
669     /*
670      * Accept the supplied slice number unequivocally (we may be looking
671      * at a DOS partition).
672      */
673     dptr += (dev->d_kind.biosdisk.slice - 1);   /* we number 1-4, offsets are 0-3 */
674 #ifdef PC98
675     sector = dptr->dp_scyl * od->od_hds * od->od_sec +
676         dptr->dp_shd * od->od_sec + dptr->dp_ssect;
677     {
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);
682     }
683 #else
684     sector = dptr->dp_start;
685     DEBUG("slice entry %d at %d, %d sectors", dev->d_kind.biosdisk.slice - 1, sector, dptr->dp_size);
686 #endif
687
688     /*
689      * If we are looking at a BSD slice, and the partition is < 0, assume the 'a' partition
690      */
691 #ifdef PC98
692     if ((dptr->dp_mid == DOSMID_386BSD) && (dev->d_kind.biosdisk.partition < 0))
693 #else
694     if ((dptr->dp_typ == DOSPTYP_386BSD) && (dev->d_kind.biosdisk.partition < 0))
695 #endif
696         dev->d_kind.biosdisk.partition = 0;
697
698  unsliced:
699     /* 
700      * Now we have the slice offset, look for the partition in the disklabel if we have
701      * a partition to start with.
702      *
703      * XXX we might want to check the label checksum.
704      */
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");
708     } else {
709         
710         if (bd_read(od, sector + LABELSECTOR, 1, buf)) {
711             DEBUG("error reading disklabel");
712             error = EIO;
713             goto out;
714         }
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;
719
720         if (lp->d_magic != DISKMAGIC) {
721             DEBUG("no disklabel");
722             error = ENOENT;
723             goto out;
724         }
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);
728             error = EPART;
729             goto out;
730
731         }
732
733 #ifdef DISK_DEBUG
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");
738 #endif
739         
740         od->od_boff = lp->d_partitions[dev->d_kind.biosdisk.partition].p_offset;
741     }
742     
743  out:
744     if (error) {
745         free(od);
746     } else {
747         *odp = od;      /* return the open disk */
748     }
749     return(error);
750 }
751
752 #ifndef PC98
753 static void
754 bd_checkextended(struct open_disk *od, int slicenum)
755 {
756         char    buf[BIOSDISK_SECSIZE];
757         struct dos_partition *dp;
758         u_int base;
759         int i, start, end;
760
761         dp = &od->od_slicetab[slicenum];
762         start = od->od_nslices;
763
764         if (dp->dp_size == 0)
765                 goto done;
766         if (dp->dp_typ != DOSPTYP_EXT)
767                 goto done;
768         if (bd_read(od, (daddr_t)dp->dp_start, 1, buf))
769                 goto done;
770         if (((u_char)buf[0x1fe] != 0x55) || ((u_char)buf[0x1ff] != 0xaa)) {
771                 DEBUG("no magic in extended table");
772                 goto done;
773         }
774         base = dp->dp_start;
775         dp = (struct dos_partition *)(&buf[DOSPARTOFF]);
776         for (i = 0; i < NDOSPART; i++, dp++) {
777                 if (dp->dp_size == 0)
778                         continue;
779                 if (od->od_nslices == MAX_SLICES)
780                         goto done;
781                 dp->dp_start += base;
782                 bcopy(dp, &od->od_slicetab[od->od_nslices], sizeof(*dp));
783                 od->od_nslices++;
784         }
785         end = od->od_nslices;
786
787         /*
788          * now, recursively check the slices we just added
789          */
790         for (i = start; i < end; i++)
791                 bd_checkextended(od, i);
792 done:
793         return;
794 }
795 #endif
796
797 /*
798  * Search for a slice with the following preferences:
799  *
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
806  */
807 #define PREF_RAWDISK    0
808 #define PREF_FBSD_ACT   1
809 #define PREF_FBSD       2
810 #define PREF_LINUX_ACT  3
811 #define PREF_LINUX      4
812 #define PREF_DOS_ACT    5
813 #define PREF_DOS        6
814 #define PREF_NONE       7
815
816 /*
817  * slicelimit is in the range 0 .. NDOSPART
818  */
819 static int
820 bd_bestslice(struct open_disk *od)
821 {
822         struct dos_partition *dp;
823         int pref, preflevel;
824         int i, prefslice;
825         
826         prefslice = 0;
827         preflevel = PREF_NONE;
828
829         dp = &od->od_slicetab[0];
830         for (i = 0; i < od->od_nslices; i++, dp++) {
831 #ifdef PC98
832                 switch(dp->dp_mid & 0x7f) {
833                 case DOSMID_386BSD & 0x7f:              /* FreeBSD */
834                         if ((dp->dp_mid & 0x80) &&
835                             (preflevel > PREF_FBSD_ACT)) {
836                                 pref = i;
837                                 preflevel = PREF_FBSD_ACT;
838                         } else if (preflevel > PREF_FBSD) {
839                                 pref = i;
840                                 preflevel = PREF_FBSD;
841                         }
842                         break;
843
844                 case 0x11:                              /* DOS/Windows */
845                 case 0x20:
846                 case 0x21:
847                 case 0x22:
848                 case 0x23:
849                 case 0x63:
850                         if ((dp->dp_mid & 0x80) &&
851                             (preflevel > PREF_DOS_ACT)) {
852                                 pref = i;
853                                 preflevel = PREF_DOS_ACT;
854                         } else if (preflevel > PREF_DOS) {
855                                 pref = i;
856                                 preflevel = PREF_DOS;
857                         }
858                         break;
859                 }
860 #else
861                 switch (dp->dp_typ) {
862                 case DOSPTYP_386BSD:            /* FreeBSD */
863                         pref = dp->dp_flag & 0x80 ? PREF_FBSD_ACT : PREF_FBSD;
864                         break;
865
866                 case DOSPTYP_LINUX:
867                         pref = dp->dp_flag & 0x80 ? PREF_LINUX_ACT : PREF_LINUX;
868                         break;
869     
870                 case 0x01:              /* DOS/Windows */
871                 case 0x04:
872                 case 0x06:
873                 case 0x0b:
874                 case 0x0c:
875                 case 0x0e:
876                         pref = dp->dp_flag & 0x80 ? PREF_DOS_ACT : PREF_DOS;
877                         break;
878
879                 default:
880                         pref = PREF_NONE;
881                 }
882                 if (pref < preflevel) {
883                         preflevel = pref;
884                         prefslice = i + 1;
885                 }
886 #endif
887         }
888         return (prefslice);
889 }
890
891 static int 
892 bd_close(struct open_file *f)
893 {
894     struct open_disk    *od = (struct open_disk *)(((struct i386_devdesc *)(f->f_devdata))->d_kind.biosdisk.data);
895
896     bd_closedisk(od);
897     return(0);
898 }
899
900 static void
901 bd_closedisk(struct open_disk *od)
902 {
903     DEBUG("open_disk %p", od);
904 #if 0
905     /* XXX is this required? (especially if disk already open...) */
906     if (od->od_flags & BD_FLOPPY)
907         delay(3000000);
908 #endif
909     free(od);
910 }
911
912 static int 
913 bd_strategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
914 {
915     struct bcache_devdata       bcd;
916     struct open_disk    *od = (struct open_disk *)(((struct i386_devdesc *)devdata)->d_kind.biosdisk.data);
917
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));
921 }
922
923 static int 
924 bd_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
925 {
926     struct open_disk    *od = (struct open_disk *)(((struct i386_devdesc *)devdata)->d_kind.biosdisk.data);
927     int                 blks;
928 #ifdef BD_SUPPORT_FRAGS
929     char                fragbuf[BIOSDISK_SECSIZE];
930     size_t              fragsize;
931
932     fragsize = size % BIOSDISK_SECSIZE;
933 #else
934     if (size % BIOSDISK_SECSIZE)
935         panic("bd_strategy: %d bytes I/O not multiple of block size", size);
936 #endif
937
938     DEBUG("open_disk %p", od);
939
940     if (rw != F_READ)
941         return(EROFS);
942
943
944     blks = size / BIOSDISK_SECSIZE;
945     DEBUG("read %d from %d to %p", blks, dblk, buf);
946
947     if (rsize)
948         *rsize = 0;
949     if (blks && bd_read(od, dblk, blks, buf)) {
950         DEBUG("read error");
951         return (EIO);
952     }
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");
958         return(EIO);
959     }
960     bcopy(fragbuf, buf + (blks * BIOSDISK_SECSIZE), fragsize);
961 #endif
962     if (rsize)
963         *rsize = size;
964     return (0);
965 }
966
967 /* Max number of sectors to bounce-buffer if the request crosses a 64k boundary */
968 #define FLOPPY_BOUNCEBUF        18
969
970 static int
971 bd_read(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest)
972 {
973     u_int       x, bpc, cyl, hd, sec, result, resid, retry, maxfer;
974     caddr_t     p, xp, bbuf, breg;
975     
976     /* Just in case some idiot actually tries to read -1 blocks... */
977     if (blks < 0)
978         return (-1);
979
980     bpc = (od->od_sec * od->od_hds);            /* blocks per cylinder */
981     resid = blks;
982     p = dest;
983
984     /* Decide whether we have to bounce */
985 #ifdef PC98
986     if (
987 #else
988     if ((od->od_unit < 0x80) && 
989 #endif
990         ((VTOP(dest) >> 16) != (VTOP(dest + blks * BIOSDISK_SECSIZE) >> 16))) {
991
992         /* 
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
996          * use the top half.
997          */
998 #ifdef PC98
999         x = min(od->od_sec, (unsigned)blks);
1000 #else
1001         x = min(FLOPPY_BOUNCEBUF, (unsigned)blks);
1002 #endif
1003         bbuf = malloc(x * 2 * BIOSDISK_SECSIZE);
1004         if (((u_int32_t)VTOP(bbuf) & 0xffff0000) == ((u_int32_t)VTOP(dest + x * BIOSDISK_SECSIZE) & 0xffff0000)) {
1005             breg = bbuf;
1006         } else {
1007             breg = bbuf + x * BIOSDISK_SECSIZE;
1008         }
1009         maxfer = x;                     /* limit transfers to bounce region size */
1010     } else {
1011         breg = bbuf = NULL;
1012         maxfer = 0;
1013     }
1014     
1015     while (resid > 0) {
1016         x = dblk;
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 */
1021
1022         /* play it safe and don't cross track boundaries (XXX this is probably unnecessary) */
1023         x = min(od->od_sec - sec, resid);
1024         if (maxfer > 0)
1025             x = min(x, maxfer);         /* fit bounce buffer */
1026
1027         /* where do we transfer to? */
1028         xp = bbuf == NULL ? p : breg;
1029
1030         /* correct sector number for 1-based BIOS numbering */
1031 #ifdef PC98
1032         if ((od->od_unit & 0xf0) == 0x30 || (od->od_unit & 0xf0) == 0x90)
1033             sec++;
1034 #else
1035         sec++;
1036 #endif
1037
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 */
1041             if (retry > 0) {
1042 #ifdef PC98
1043                 v86.ctl = V86_FLAGS;
1044                 v86.addr = 0x1b;
1045                 v86.eax = 0x0300 | od->od_unit;
1046 #else
1047                 v86.ctl = V86_FLAGS;
1048                 v86.addr = 0x13;
1049                 v86.eax = 0;
1050                 v86.edx = od->od_unit;
1051 #endif
1052                 v86int();
1053             }
1054             
1055 #ifdef PC98
1056             v86.ctl = V86_FLAGS;
1057             v86.addr = 0x1b;
1058             if (od->od_flags & BD_FLOPPY) {
1059                 v86.eax = 0xd600 | od->od_unit;
1060                 v86.ecx = 0x0200 | (cyl & 0xff);
1061             }
1062             else {
1063                 v86.eax = 0x0600 | od->od_unit;
1064                 v86.ecx = cyl;
1065             }
1066             if (od->od_flags & BD_OPTICAL) {
1067                 v86.eax &= 0xFF7F;
1068                 v86.ecx = dblk & 0xFFFF;
1069                 v86.edx = dblk >> 16;
1070             } else {
1071                 v86.edx = (hd << 8) | sec;
1072             }
1073             v86.ebx = x * BIOSDISK_SECSIZE;
1074             v86.es = VTOPSEG(xp);
1075             v86.ebp = VTOPOFF(xp);
1076             v86int();
1077             result = (v86.efl & 0x1);
1078             if (result == 0)
1079                 break;
1080 #else
1081             if(cyl > 1023) {
1082                 /* use EDD if the disk supports it, otherwise, return error */
1083                 if(od->od_flags & BD_MODEEDD1) {
1084                     static unsigned short packet[8];
1085
1086                     packet[0] = 0x10;
1087                     packet[1] = x;
1088                     packet[2] = VTOPOFF(xp);
1089                     packet[3] = VTOPSEG(xp);
1090                     packet[4] = dblk & 0xffff;
1091                     packet[5] = dblk >> 16;
1092                     packet[6] = 0;
1093                     packet[7] = 0;
1094                     v86.ctl = V86_FLAGS;
1095                     v86.addr = 0x13;
1096                     v86.eax = 0x4200;
1097                     v86.edx = od->od_unit;
1098                     v86.ds = VTOPSEG(packet);
1099                     v86.esi = VTOPOFF(packet);
1100                     v86int();
1101                     result = (v86.efl & 0x1);
1102                     if(result == 0)
1103                       break;
1104                 } else {
1105                     result = 1;
1106                     break;
1107                 }
1108             } else {
1109                 /* Use normal CHS addressing */
1110                 v86.ctl = V86_FLAGS;
1111                 v86.addr = 0x13;
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);
1117                 v86int();
1118                 result = (v86.efl & 0x1);
1119                 if (result == 0)
1120                   break;
1121             }
1122 #endif
1123         }
1124         
1125 #ifdef PC98
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);
1132 #else
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);
1137 #endif
1138         if (result) {
1139             if (bbuf != NULL)
1140                 free(bbuf);
1141             return(-1);
1142         }
1143         if (bbuf != NULL)
1144             bcopy(breg, p, x * BIOSDISK_SECSIZE);
1145         p += (x * BIOSDISK_SECSIZE);
1146         dblk += x;
1147         resid -= x;
1148     }
1149         
1150 /*    hexdump(dest, (blks * BIOSDISK_SECSIZE)); */
1151     if (bbuf != NULL)
1152         free(bbuf);
1153     return(0);
1154 }
1155
1156 static int
1157 bd_getgeom(struct open_disk *od)
1158 {
1159
1160 #ifdef PC98
1161     if (od->od_flags & BD_FLOPPY) {
1162         od->od_cyl = 79;
1163         od->od_hds = 2;
1164         od->od_sec = (od->od_unit & 0xf0) == 0x30 ? 18 : 15;
1165     } else if (od->od_flags & BD_OPTICAL) {
1166         od->od_cyl = 0xFFFE;
1167         od->od_hds = 8;
1168         od->od_sec = 32;
1169     } else {
1170         v86.ctl = V86_FLAGS;
1171         v86.addr = 0x1b;
1172         v86.eax = 0x8400 | od->od_unit;
1173         v86int();
1174       
1175         od->od_cyl = v86.ecx;
1176         od->od_hds = (v86.edx >> 8) & 0xff;
1177         od->od_sec = v86.edx & 0xff;
1178         if (v86.efl & 0x1)
1179             return(1);
1180     }
1181 #else
1182     v86.ctl = V86_FLAGS;
1183     v86.addr = 0x13;
1184     v86.eax = 0x800;
1185     v86.edx = od->od_unit;
1186     v86int();
1187
1188     if ((v86.efl & 0x1) ||                              /* carry set */
1189         ((v86.edx & 0xff) <= (unsigned)(od->od_unit & 0x7f)))   /* unit # bad */
1190         return(1);
1191     
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;
1197 #endif
1198
1199     DEBUG("unit 0x%x geometry %d/%d/%d", od->od_unit, od->od_cyl, od->od_hds, od->od_sec);
1200     return(0);
1201 }
1202
1203 /*
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.
1210  *
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.
1216  */
1217 u_int32_t
1218 bd_getbigeom(int bunit)
1219 {
1220
1221 #ifdef PC98
1222     int hds = 0;
1223     int unit = 0x80;            /* IDE HDD */
1224     u_int addr = 0xA155d;
1225
1226     while (unit < 0xa7) {
1227         if (*(u_char *)PTOV(addr) & (1 << (unit & 0x0f)))
1228             if (hds++ == bunit)
1229                 break;
1230
1231         if (unit >= 0xA0) {
1232             int  media = ((unsigned *)PTOV(0xA1460))[unit & 0x0F] & 0x1F;
1233
1234             if (media == 7 && hds++ == bunit)   /* SCSI MO */
1235                 return(0xFFFE0820); /* C:65535 H:8 S:32 */
1236         }
1237         if (++unit == 0x84) {
1238             unit = 0xA0;        /* SCSI HDD */
1239             addr = 0xA1482;
1240         }
1241     }
1242     if (unit == 0xa7)
1243         return 0x4F020F;        /* 1200KB FD C:80 H:2 S:15 */
1244     v86.ctl = V86_FLAGS;
1245     v86.addr = 0x1b;
1246     v86.eax = 0x8400 | unit;
1247     v86int();
1248     if (v86.efl & 0x1)
1249         return 0x4F020F;        /* 1200KB FD C:80 H:2 S:15 */
1250     return ((v86.ecx & 0xffff) << 16) | (v86.edx & 0xffff);
1251 #else
1252     v86.ctl = V86_FLAGS;
1253     v86.addr = 0x13;
1254     v86.eax = 0x800;
1255     v86.edx = 0x80 + bunit;
1256     v86int();
1257     if (v86.efl & 0x1)
1258         return 0x4f010f;
1259     return ((v86.ecx & 0xc0) << 18) | ((v86.ecx & 0xff00) << 8) |
1260            (v86.edx & 0xff00) | (v86.ecx & 0x3f);
1261 #endif
1262 }
1263
1264 /*
1265  * Return a suitable dev_t value for (dev).
1266  *
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.
1269  */
1270 int
1271 bd_getdev(struct i386_devdesc *dev)
1272 {
1273     struct open_disk            *od;
1274     int                         biosdev;
1275     int                         major;
1276     int                         rootdev;
1277     char                        *nip, *cp;
1278     int                         unitofs = 0, i, unit;
1279
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 */
1283         return(-1);
1284     if (bd_opendisk(&od, dev) != 0)             /* oops, not a viable device */
1285         return(-1);
1286
1287 #ifdef PC98
1288     if ((biosdev & 0xf0) == 0x90 || (biosdev & 0xf0) == 0x30) {
1289 #else
1290     if (biosdev < 0x80) {
1291 #endif
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 */
1295             major = WFDMAJOR;
1296         } else {
1297             /* is a floppy disk */
1298             major = FDMAJOR;
1299         }
1300     } else {
1301         /* harddisk */
1302         if ((od->od_flags & BD_LABELOK) && (od->od_disklabel.d_type == DTYPE_SCSI)) {
1303             /* label OK, disk labelled as SCSI */
1304             major = DAMAJOR;
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))
1310                     unitofs = i;
1311             }
1312         } else {
1313             /* assume an IDE disk */
1314             major = WDMAJOR;
1315         }
1316     }
1317     /* default root disk unit number */
1318 #ifdef PC98
1319     if ((biosdev & 0xf0) == 0xa0)
1320         unit = bdinfo[dev->d_kind.biosdisk.unit].bd_da_unit;
1321     else
1322         unit = biosdev & 0xf;
1323 #else
1324     unit = (biosdev & 0x7f) - unitofs;
1325 #endif
1326
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))
1332             unit = i;
1333     }
1334
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, 
1338                           unit,
1339                           dev->d_kind.biosdisk.partition);
1340     DEBUG("dev is 0x%x\n", rootdev);
1341     return(rootdev);
1342 }