Merge from vendor branch LIBARCHIVE:
[dragonfly.git] / sys / boot / pc32 / libi386 / 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/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.12 2007/06/18 05:13:42 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/disklabel32.h>
43 #include <sys/diskmbr.h>
44 #include <sys/dtype.h>
45 #include <machine/bootinfo.h>
46
47 #include <stdarg.h>
48
49 #include <bootstrap.h>
50 #include <btxv86.h>
51 #include "libi386.h"
52
53 #define BIOS_NUMDRIVES          0x475
54 #define BIOSDISK_SECSIZE        512
55 #define BUFSIZE                 (1 * BIOSDISK_SECSIZE)
56 #define MAXBDDEV                MAXDEV
57
58 #define DT_ATAPI                0x10            /* disk type for ATAPI floppies */
59 #define WDMAJOR                 0               /* major numbers for devices we frontend for */
60 #define WFDMAJOR                1
61 #define FDMAJOR                 2
62 #define DAMAJOR                 4
63
64 #ifdef DISK_DEBUG
65 # define DEBUG(fmt, args...)    printf("%s: " fmt "\n" , __func__ , ## args)
66 #else
67 # define DEBUG(fmt, args...)
68 #endif
69
70 struct open_disk {
71     int                 od_dkunit;              /* disk unit number */
72     int                 od_unit;                /* BIOS unit number */
73     int                 od_cyl;                 /* BIOS geometry */
74     int                 od_hds;
75     int                 od_sec;
76     int                 od_boff;                /* block offset from beginning of BIOS disk */
77     int                 od_flags;
78 #define BD_MODEINT13            0x0000
79 #define BD_MODEEDD1             0x0001
80 #define BD_MODEEDD3             0x0002
81 #define BD_MODEMASK             0x0003
82 #define BD_FLOPPY               0x0004
83 #define BD_LABELOK              0x0008
84 #define BD_PARTTABOK            0x0010
85     struct disklabel32          od_disklabel;
86     int                         od_nslices;     /* slice count */
87     struct dos_partition        od_slicetab[NEXTDOSPART];
88 };
89
90 /*
91  * List of BIOS devices, translation from disk unit number to
92  * BIOS unit number.
93  */
94 static struct bdinfo
95 {
96     int         bd_unit;                /* BIOS unit number */
97     int         bd_flags;
98     int         bd_type;                /* BIOS 'drive type' (floppy only) */
99 } bdinfo [MAXBDDEV];
100 static int nbdinfo = 0;
101
102 static int      bd_getgeom(struct open_disk *od);
103 static int      bd_read(struct open_disk *od, daddr_t dblk, int blks,
104                     caddr_t dest);
105 static int      bd_write(struct open_disk *od, daddr_t dblk, int blks,
106                     caddr_t dest);
107
108 static int      bd_int13probe(struct bdinfo *bd);
109
110 static void     bd_printslice(struct open_disk *od, struct dos_partition *dp,
111                     char *prefix, int verbose);
112 static void     bd_printbsdslice(struct open_disk *od, daddr_t offset,
113                     char *prefix, int verbose);
114
115 static int      bd_init(void);
116 static int      bd_strategy(void *devdata, int flag, daddr_t dblk,
117                     size_t size, char *buf, size_t *rsize);
118 static int      bd_realstrategy(void *devdata, int flag, daddr_t dblk,
119                     size_t size, char *buf, size_t *rsize);
120 static int      bd_open(struct open_file *f, ...);
121 static int      bd_close(struct open_file *f);
122 static void     bd_print(int verbose);
123
124 struct devsw biosdisk = {
125     "disk", 
126     DEVT_DISK, 
127     bd_init,
128     bd_strategy, 
129     bd_open, 
130     bd_close, 
131     noioctl,
132     bd_print,
133     NULL
134 };
135
136 static int      bd_opendisk(struct open_disk **odp, struct i386_devdesc *dev);
137 static void     bd_closedisk(struct open_disk *od);
138 static int      bd_bestslice(struct open_disk *od);
139 static void     bd_chainextended(struct open_disk *od, u_int32_t base, u_int32_t offset);
140
141 /*
142  * Translate between BIOS device numbers and our private unit numbers.
143  */
144 int
145 bd_bios2unit(int biosdev)
146 {
147     int         i;
148     
149     DEBUG("looking for bios device 0x%x", biosdev);
150     for (i = 0; i < nbdinfo; i++) {
151         DEBUG("bd unit %d is BIOS device 0x%x", i, bdinfo[i].bd_unit);
152         if (bdinfo[i].bd_unit == biosdev)
153             return(i);
154     }
155     return(-1);
156 }
157
158 int
159 bd_unit2bios(int unit)
160 {
161     if ((unit >= 0) && (unit < nbdinfo))
162         return(bdinfo[unit].bd_unit);
163     return(-1);
164 }
165
166 /*    
167  * Quiz the BIOS for disk devices, save a little info about them.
168  */
169 static int
170 bd_init(void) 
171 {
172     int         base, unit, nfd = 0;
173
174     /* sequence 0, 0x80 */
175     for (base = 0; base <= 0x80; base += 0x80) {
176         for (unit = base; (nbdinfo < MAXBDDEV); unit++) {
177             /* check the BIOS equipment list for number of fixed disks */
178             if((base == 0x80) &&
179                (nfd >= *(unsigned char *)PTOV(BIOS_NUMDRIVES)))
180                 break;
181
182             bdinfo[nbdinfo].bd_unit = unit;
183             bdinfo[nbdinfo].bd_flags = (unit < 0x80) ? BD_FLOPPY : 0;
184
185             if (!bd_int13probe(&bdinfo[nbdinfo]))
186                 break;
187
188             /* XXX we need "disk aliases" to make this simpler */
189             printf("BIOS drive %c: is disk%d\n", 
190                    (unit < 0x80) ? ('A' + unit) : ('C' + unit - 0x80), nbdinfo);
191             nbdinfo++;
192             if (base == 0x80)
193                 nfd++;
194         }
195     }
196     return(0);
197 }
198
199 /*
200  * Try to detect a device supported by the legacy int13 BIOS
201  */
202 static int
203 bd_int13probe(struct bdinfo *bd)
204 {
205     v86.ctl = V86_FLAGS;
206     v86.addr = 0x13;
207     v86.eax = 0x800;
208     v86.edx = bd->bd_unit;
209     v86int();
210     
211     if (!(v86.efl & 0x1) &&                             /* carry clear */
212         ((v86.edx & 0xff) > ((unsigned)bd->bd_unit & 0x7f))) {  /* unit # OK */
213
214         /*
215          * Ignore devices with an absurd sector size.
216          */
217         if ((v86.ecx & 0x3f) == 0) {
218                 DEBUG("Invalid geometry for unit %d", bd->bd_unit);
219                 return(0);
220         }
221         bd->bd_flags |= BD_MODEINT13;
222         bd->bd_type = v86.ebx & 0xff;
223
224         /* Determine if we can use EDD with this device. */
225         v86.eax = 0x4100;
226         v86.edx = bd->bd_unit;
227         v86.ebx = 0x55aa;
228         v86int();
229         if (!(v86.efl & 0x1) &&                         /* carry clear */
230             ((v86.ebx & 0xffff) == 0xaa55) &&           /* signature */
231             (v86.ecx & 0x1)) {                          /* packets mode ok */
232             bd->bd_flags |= BD_MODEEDD1;
233             if((v86.eax & 0xff00) > 0x300)
234                 bd->bd_flags |= BD_MODEEDD3;
235         }
236         return(1);
237     }
238     return(0);
239 }
240
241 /*
242  * Print information about disks
243  */
244 static void
245 bd_print(int verbose)
246 {
247     int                         i, j;
248     char                        line[80];
249     struct i386_devdesc         dev;
250     struct open_disk            *od;
251     struct dos_partition        *dptr;
252     
253     for (i = 0; i < nbdinfo; i++) {
254         sprintf(line, "    disk%d:   BIOS drive %c:\n", i, 
255                 (bdinfo[i].bd_unit < 0x80) ? ('A' + bdinfo[i].bd_unit) : ('C' + bdinfo[i].bd_unit - 0x80));
256         pager_output(line);
257
258         /* try to open the whole disk */
259         dev.d_kind.biosdisk.unit = i;
260         dev.d_kind.biosdisk.slice = -1;
261         dev.d_kind.biosdisk.partition = -1;
262         
263         if (!bd_opendisk(&od, &dev)) {
264
265             /* Do we have a partition table? */
266             if (od->od_flags & BD_PARTTABOK) {
267                 dptr = &od->od_slicetab[0];
268
269                 /* Check for a "dedicated" disk */
270                 if ((dptr[3].dp_typ == DOSPTYP_386BSD) &&
271                     (dptr[3].dp_start == 0) &&
272                     (dptr[3].dp_size == 50000)) {
273                     sprintf(line, "      disk%d", i);
274                     bd_printbsdslice(od, 0, line, verbose);
275                 } else {
276                     for (j = 0; j < od->od_nslices; j++) {
277                         sprintf(line, "      disk%ds%d", i, j + 1);
278                         bd_printslice(od, &dptr[j], line, verbose);
279                     }
280                 }
281             }
282             bd_closedisk(od);
283         }
284     }
285 }
286
287 /*
288  * Print information about slices on a disk.  For the size calculations we
289  * assume a 512 byte sector.
290  */
291 static void
292 bd_printslice(struct open_disk *od, struct dos_partition *dp, char *prefix,
293         int verbose)
294 {
295         char line[80];
296
297         switch (dp->dp_typ) {
298         case DOSPTYP_386BSD:
299                 bd_printbsdslice(od, (daddr_t)dp->dp_start, prefix, verbose);
300                 return;
301         case DOSPTYP_LINSWP:
302                 if (verbose)
303                         sprintf(line, "%s: Linux swap %.6dMB (%d - %d)\n",
304                             prefix, dp->dp_size / 2048,
305                             dp->dp_start, dp->dp_start + dp->dp_size);
306                 else
307                         sprintf(line, "%s: Linux swap\n", prefix);
308                 break;
309         case DOSPTYP_LINUX:
310                 /*
311                  * XXX
312                  * read the superblock to confirm this is an ext2fs partition?
313                  */
314                 if (verbose)
315                         sprintf(line, "%s: ext2fs  %.6dMB (%d - %d)\n", prefix,
316                             dp->dp_size / 2048, dp->dp_start,
317                             dp->dp_start + dp->dp_size);
318                 else
319                         sprintf(line, "%s: ext2fs\n", prefix);
320                 break;
321         case 0x00:                              /* unused partition */
322         case DOSPTYP_EXT:
323                 return;
324         case 0x01:
325                 if (verbose)
326                         sprintf(line, "%s: FAT-12  %.6dMB (%d - %d)\n", prefix,
327                             dp->dp_size / 2048, dp->dp_start,
328                             dp->dp_start + dp->dp_size);
329                 else
330                         sprintf(line, "%s: FAT-12\n", prefix);
331                 break;
332         case 0x04:
333         case 0x06:
334         case 0x0e:
335                 if (verbose)
336                         sprintf(line, "%s: FAT-16  %.6dMB (%d - %d)\n", prefix,
337                             dp->dp_size / 2048, dp->dp_start,
338                             dp->dp_start + dp->dp_size);
339                 else
340                         sprintf(line, "%s: FAT-16\n", prefix);
341                 break;
342         case 0x0b:
343         case 0x0c:
344                 if (verbose)
345                         sprintf(line, "%s: FAT-32  %.6dMB (%d - %d)\n", prefix,
346                             dp->dp_size / 2048, dp->dp_start,
347                             dp->dp_start + dp->dp_size);
348                 else
349                         sprintf(line, "%s: FAT-32\n", prefix);
350                 break;
351         default:
352                 if (verbose)
353                         sprintf(line, "%s: Unknown fs: 0x%x  %.6dMB (%d - %d)\n",
354                             prefix, dp->dp_typ, dp->dp_size / 2048,
355                             dp->dp_start, dp->dp_start + dp->dp_size);
356                 else
357                         sprintf(line, "%s: Unknown fs: 0x%x\n", prefix,
358                             dp->dp_typ);
359         }
360         pager_output(line);
361 }
362
363 /*
364  * Print out each valid partition in the disklabel of a FreeBSD slice.
365  * For size calculations, we assume a 512 byte sector size.
366  */
367 static void
368 bd_printbsdslice(struct open_disk *od, daddr_t offset, char *prefix,
369     int verbose)
370 {
371     char                line[80];
372     char                buf[BIOSDISK_SECSIZE];
373     struct disklabel32  *lp;
374     int                 i;
375
376     /* read disklabel */
377     if (bd_read(od, offset + LABELSECTOR32, 1, buf))
378         return;
379     lp =(struct disklabel32 *)(&buf[0]);
380     if (lp->d_magic != DISKMAGIC32) {
381         sprintf(line, "%s: FFS  bad disklabel\n", prefix);
382         pager_output(line);
383         return;
384     }
385     
386     /* Print partitions */
387     for (i = 0; i < lp->d_npartitions; i++) {
388         /*
389          * For each partition, make sure we know what type of fs it is.  If
390          * not, then skip it.  However, since floppies often have bogus
391          * fstypes, print the 'a' partition on a floppy even if it is marked
392          * unused.
393          */
394         if ((lp->d_partitions[i].p_fstype == FS_BSDFFS) ||
395             (lp->d_partitions[i].p_fstype == FS_SWAP) ||
396             (lp->d_partitions[i].p_fstype == FS_VINUM) ||
397             ((lp->d_partitions[i].p_fstype == FS_UNUSED) && 
398              (od->od_flags & BD_FLOPPY) && (i == 0))) {
399
400             /* Only print out statistics in verbose mode */
401             if (verbose)
402                 sprintf(line, "  %s%c: %s  %.6dMB (%d - %d)\n", prefix, 'a' + i,
403                     (lp->d_partitions[i].p_fstype == FS_SWAP) ? "swap" : 
404                     (lp->d_partitions[i].p_fstype == FS_VINUM) ? "vinum" :
405                     "FFS",
406                     lp->d_partitions[i].p_size / 2048,
407                     lp->d_partitions[i].p_offset,
408                     lp->d_partitions[i].p_offset + lp->d_partitions[i].p_size);
409             else
410                 sprintf(line, "  %s%c: %s\n", prefix, 'a' + i,
411                     (lp->d_partitions[i].p_fstype == FS_SWAP) ? "swap" : 
412                     (lp->d_partitions[i].p_fstype == FS_VINUM) ? "vinum" :
413                     "FFS");
414             pager_output(line);
415         }
416     }
417 }
418
419
420 /*
421  * Attempt to open the disk described by (dev) for use by (f).
422  *
423  * Note that the philosophy here is "give them exactly what
424  * they ask for".  This is necessary because being too "smart"
425  * about what the user might want leads to complications.
426  * (eg. given no slice or partition value, with a disk that is
427  *  sliced - are they after the first BSD slice, or the DOS
428  *  slice before it?)
429  */
430 static int 
431 bd_open(struct open_file *f, ...)
432 {
433     va_list                     ap;
434     struct i386_devdesc         *dev;
435     struct open_disk            *od;
436     int                         error;
437
438     va_start(ap, f);
439     dev = va_arg(ap, struct i386_devdesc *);
440     va_end(ap);
441     if ((error = bd_opendisk(&od, dev)))
442         return(error);
443     
444     /*
445      * Save our context
446      */
447     ((struct i386_devdesc *)(f->f_devdata))->d_kind.biosdisk.data = od;
448     DEBUG("open_disk %p, partition at 0x%x", od, od->od_boff);
449     return(0);
450 }
451
452 static int
453 bd_opendisk(struct open_disk **odp, struct i386_devdesc *dev)
454 {
455     struct dos_partition        *dptr;
456     struct disklabel32          *lp;
457     struct open_disk            *od;
458     int                         sector, slice, i;
459     int                         error;
460     char                        buf[BUFSIZE];
461
462     if (dev->d_kind.biosdisk.unit >= nbdinfo) {
463         DEBUG("attempt to open nonexistent disk");
464         return(ENXIO);
465     }
466     
467     od = (struct open_disk *)malloc(sizeof(struct open_disk));
468     if (!od) {
469         DEBUG("no memory");
470         return (ENOMEM);
471     }
472
473     /* Look up BIOS unit number, intialise open_disk structure */
474     od->od_dkunit = dev->d_kind.biosdisk.unit;
475     od->od_unit = bdinfo[od->od_dkunit].bd_unit;
476     od->od_flags = bdinfo[od->od_dkunit].bd_flags;
477     od->od_boff = 0;
478     od->od_nslices = 0;
479     error = 0;
480     DEBUG("open '%s', unit 0x%x slice %d partition %c",
481              i386_fmtdev(dev), dev->d_kind.biosdisk.unit, 
482              dev->d_kind.biosdisk.slice, dev->d_kind.biosdisk.partition + 'a');
483
484     /* Get geometry for this open (removable device may have changed) */
485     if (bd_getgeom(od)) {
486         DEBUG("can't get geometry");
487         error = ENXIO;
488         goto out;
489     }
490
491     /*
492      * Following calculations attempt to determine the correct value
493      * for d->od_boff by looking for the slice and partition specified,
494      * or searching for reasonable defaults.
495      */
496
497     /*
498      * Find the slice in the DOS slice table.
499      */
500     if (bd_read(od, 0, 1, buf)) {
501         DEBUG("error reading MBR");
502         error = EIO;
503         goto out;
504     }
505
506     /* 
507      * Check the slice table magic.
508      */
509     if (((u_char)buf[0x1fe] != 0x55) || ((u_char)buf[0x1ff] != 0xaa)) {
510         /* If a slice number was explicitly supplied, this is an error */
511         if (dev->d_kind.biosdisk.slice > 0) {
512             DEBUG("no slice table/MBR (no magic)");
513             error = ENOENT;
514             goto out;
515         }
516         sector = 0;
517         goto unsliced;          /* may be a floppy */
518     }
519
520     /*
521      * copy the partition table, then pick up any extended partitions.  The
522      * base partition table always has four entries, even if some of them
523      * represented extended partitions.  However, any additional sub-extended
524      * partitions will be silently recursed and not included in the slice
525      * table.
526      */
527     bcopy(buf + DOSPARTOFF, &od->od_slicetab,
528       sizeof(struct dos_partition) * NDOSPART);
529     od->od_nslices = NDOSPART;
530
531     dptr = &od->od_slicetab[0];
532     for (i = 0; i < NDOSPART; i++, dptr++) {
533         if ((dptr->dp_typ == DOSPTYP_EXT) || (dptr->dp_typ == DOSPTYP_EXTLBA))
534             bd_chainextended(od, dptr->dp_start, 0); /* 1st offset is zero */
535     }
536     od->od_flags |= BD_PARTTABOK;
537     dptr = &od->od_slicetab[0];
538
539     /*
540      * Overflow entries are not loaded into memory but we still keep
541      * track of the count.  Fix it up now.
542      */
543     if (od->od_nslices > NEXTDOSPART)
544         od->od_nslices = NEXTDOSPART;
545
546     /* 
547      * Is this a request for the whole disk? 
548      */
549     if (dev->d_kind.biosdisk.slice == -1) {
550         sector = 0;
551         goto unsliced;
552     }
553
554     /*
555      * if a slice number was supplied but not found, this is an error.
556      */
557     if (dev->d_kind.biosdisk.slice > 0) {
558         slice = dev->d_kind.biosdisk.slice - 1;
559         if (slice >= od->od_nslices) {
560             DEBUG("slice %d not found", slice);
561             error = ENOENT;
562             goto out;
563         }
564     }
565
566     /*
567      * Check for the historically bogus MBR found on true dedicated disks
568      */
569     if ((dptr[3].dp_typ == DOSPTYP_386BSD) &&
570       (dptr[3].dp_start == 0) &&
571       (dptr[3].dp_size == 50000)) {
572         sector = 0;
573         goto unsliced;
574     }
575
576     /* Try to auto-detect the best slice; this should always give a slice number */
577     if (dev->d_kind.biosdisk.slice == 0) {
578         slice = bd_bestslice(od);
579         if (slice == -1) {
580             error = ENOENT;
581             goto out;
582         }
583         dev->d_kind.biosdisk.slice = slice;
584     }
585
586     dptr = &od->od_slicetab[0];
587     /*
588      * Accept the supplied slice number unequivocally (we may be looking
589      * at a DOS partition).
590      */
591     dptr += (dev->d_kind.biosdisk.slice - 1);   /* we number 1-4, offsets are 0-3 */
592     sector = dptr->dp_start;
593     DEBUG("slice entry %d at %d, %d sectors", dev->d_kind.biosdisk.slice - 1, sector, dptr->dp_size);
594
595     /*
596      * If we are looking at a BSD slice, and the partition is < 0, assume the 'a' partition
597      */
598     if ((dptr->dp_typ == DOSPTYP_386BSD) && (dev->d_kind.biosdisk.partition < 0))
599         dev->d_kind.biosdisk.partition = 0;
600
601  unsliced:
602     /* 
603      * Now we have the slice offset, look for the partition in the disklabel if we have
604      * a partition to start with.
605      *
606      * XXX we might want to check the label checksum.
607      */
608     if (dev->d_kind.biosdisk.partition < 0) {
609         od->od_boff = sector;           /* no partition, must be after the slice */
610         DEBUG("opening raw slice");
611     } else {
612         
613         if (bd_read(od, sector + LABELSECTOR32, 1, buf)) {
614             DEBUG("error reading disklabel");
615             error = EIO;
616             goto out;
617         }
618         DEBUG("copy %d bytes of label from %p to %p", sizeof(struct disklabel32), buf + LABELOFFSET32, &od->od_disklabel);
619         bcopy(buf + LABELOFFSET32, &od->od_disklabel, sizeof(struct disklabel32));
620         lp = &od->od_disklabel;
621         od->od_flags |= BD_LABELOK;
622
623         if (lp->d_magic != DISKMAGIC32) {
624             DEBUG("no disklabel");
625             error = ENOENT;
626             goto out;
627         }
628         if (dev->d_kind.biosdisk.partition >= lp->d_npartitions) {
629             DEBUG("partition '%c' exceeds partitions in table (a-'%c')",
630                   'a' + dev->d_kind.biosdisk.partition, 'a' + lp->d_npartitions);
631             error = EPART;
632             goto out;
633
634         }
635
636 #ifdef DISK_DEBUG
637         /* Complain if the partition is unused unless this is a floppy. */
638         if ((lp->d_partitions[dev->d_kind.biosdisk.partition].p_fstype == FS_UNUSED) &&
639             !(od->od_flags & BD_FLOPPY))
640             DEBUG("warning, partition marked as unused");
641 #endif
642         
643         od->od_boff = 
644                 lp->d_partitions[dev->d_kind.biosdisk.partition].p_offset -
645                 lp->d_partitions[RAW_PART].p_offset +
646                 sector;
647     }
648     
649  out:
650     if (error) {
651         free(od);
652     } else {
653         *odp = od;      /* return the open disk */
654     }
655     return(error);
656 }
657
658
659 static void
660 bd_chainextended(struct open_disk *od, u_int32_t base, u_int32_t offset)
661 {
662         char   buf[BIOSDISK_SECSIZE];
663         struct dos_partition *dp1, *dp2;
664         int i;
665
666         if (bd_read(od, (daddr_t)(base + offset), 1, buf)) {
667                 printf("\nerror reading extended partition table");
668                 return;
669         }
670
671         /*
672          * dp1 points to the first record in the on-disk XPT,
673          * dp2 points to the next entry in the in-memory parition table.
674          *
675          * NOTE: dp2 may be out of bounds if od_nslices >= NEXTDOSPART.
676          *
677          * NOTE: unlike the extended partitions in our primary dos partition
678          * table, we do not record recursed extended partitions themselves 
679          * in our in-memory partition table.
680          *
681          * NOTE: recording within our in-memory table must be breadth first
682          * ot match what the kernel does.  Thus, two passes are required.
683          *
684          * NOTE: partitioning programs which support extended partitions seem
685          * to always use the first entry for the user partition and the
686          * second entry to chain, and also appear to disallow more then one
687          * extended partition at each level.  Nevertheless we make our code
688          * somewhat more generic (and the same as the kernel's own slice
689          * scan).
690          */
691         dp1 = (struct dos_partition *)(&buf[DOSPARTOFF]);
692         dp2 = &od->od_slicetab[od->od_nslices];
693
694         for (i = 0; i < NDOSPART; ++i, ++dp1) {
695                 if (dp1->dp_scyl == 0 && dp1->dp_shd == 0 &&
696                     dp1->dp_ssect == 0 && dp1->dp_start == 0 && 
697                     dp1->dp_size == 0) {
698                         continue;
699                 }
700                 if ((dp1->dp_typ == DOSPTYP_EXT) || 
701                     (dp1->dp_typ == DOSPTYP_EXTLBA)) {
702                         /*
703                          * breadth first traversal, must skip in the 
704                          * first pass
705                          */
706                         continue;
707                 }
708
709                 /*
710                  * Only load up the in-memory data if we haven't overflowed
711                  * our in-memory array.
712                  */
713                 if (od->od_nslices < NEXTDOSPART) {
714                         dp2->dp_typ = dp1->dp_typ;
715                         dp2->dp_start = base + offset + dp1->dp_start;
716                         dp2->dp_size = dp1->dp_size;
717                 }
718                 ++od->od_nslices;
719                 ++dp2;
720         }
721
722         /*
723          * Pass 2 - handle extended partitions.  Note that the extended
724          * slice itself is not stored in the slice array when we recurse,
725          * but any 'original' top-level extended slices are.  This is to
726          * match what the kernel does.
727          */
728         dp1 -= NDOSPART;
729         for (i = 0; i < NDOSPART; ++i, ++dp1) {
730                 if (dp1->dp_scyl == 0 && dp1->dp_shd == 0 &&
731                     dp1->dp_ssect == 0 && dp1->dp_start == 0 && 
732                     dp1->dp_size == 0) {
733                         continue;
734                 }
735                 if ((dp1->dp_typ == DOSPTYP_EXT) || 
736                     (dp1->dp_typ == DOSPTYP_EXTLBA)) {
737                         bd_chainextended(od, base, dp1->dp_start);
738                         continue;
739                 }
740         }
741 }
742
743 /*
744  * Search for a slice with the following preferences:
745  *
746  * 1: Active FreeBSD slice
747  * 2: Non-active FreeBSD slice
748  * 3: Active Linux slice
749  * 4: non-active Linux slice
750  * 5: Active FAT/FAT32 slice
751  * 6: non-active FAT/FAT32 slice
752  */
753 #define PREF_RAWDISK    0
754 #define PREF_FBSD_ACT   1
755 #define PREF_FBSD       2
756 #define PREF_LINUX_ACT  3
757 #define PREF_LINUX      4
758 #define PREF_DOS_ACT    5
759 #define PREF_DOS        6
760 #define PREF_NONE       7
761
762 /*
763  * slicelimit is in the range 0 .. NDOSPART
764  */
765 static int
766 bd_bestslice(struct open_disk *od)
767 {
768         struct dos_partition *dp;
769         int pref, preflevel;
770         int i, prefslice;
771         
772         prefslice = 0;
773         preflevel = PREF_NONE;
774
775         dp = &od->od_slicetab[0];
776         for (i = 0; i < od->od_nslices; i++, dp++) {
777                 switch (dp->dp_typ) {
778                 case DOSPTYP_386BSD:            /* FreeBSD */
779                         pref = dp->dp_flag & 0x80 ? PREF_FBSD_ACT : PREF_FBSD;
780                         break;
781
782                 case DOSPTYP_LINUX:
783                         pref = dp->dp_flag & 0x80 ? PREF_LINUX_ACT : PREF_LINUX;
784                         break;
785     
786                 case 0x01:              /* DOS/Windows */
787                 case 0x04:
788                 case 0x06:
789                 case 0x0b:
790                 case 0x0c:
791                 case 0x0e:
792                         pref = dp->dp_flag & 0x80 ? PREF_DOS_ACT : PREF_DOS;
793                         break;
794
795                 default:
796                         pref = PREF_NONE;
797                 }
798                 if (pref < preflevel) {
799                         preflevel = pref;
800                         prefslice = i + 1;
801                 }
802         }
803         return (prefslice);
804 }
805  
806 static int 
807 bd_close(struct open_file *f)
808 {
809     struct open_disk    *od = (struct open_disk *)(((struct i386_devdesc *)(f->f_devdata))->d_kind.biosdisk.data);
810
811     bd_closedisk(od);
812     return(0);
813 }
814
815 static void
816 bd_closedisk(struct open_disk *od)
817 {
818     DEBUG("open_disk %p", od);
819 #if 0
820     /* XXX is this required? (especially if disk already open...) */
821     if (od->od_flags & BD_FLOPPY)
822         delay(3000000);
823 #endif
824     free(od);
825 }
826
827 static int 
828 bd_strategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
829 {
830     struct bcache_devdata       bcd;
831     struct open_disk    *od = (struct open_disk *)(((struct i386_devdesc *)devdata)->d_kind.biosdisk.data);
832
833     bcd.dv_strategy = bd_realstrategy;
834     bcd.dv_devdata = devdata;
835     return(bcache_strategy(&bcd, od->od_unit, rw, dblk+od->od_boff, size, buf, rsize));
836 }
837
838 static int 
839 bd_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
840 {
841     struct open_disk    *od = (struct open_disk *)(((struct i386_devdesc *)devdata)->d_kind.biosdisk.data);
842     int                 blks;
843 #ifdef BD_SUPPORT_FRAGS
844     char                fragbuf[BIOSDISK_SECSIZE];
845     size_t              fragsize;
846
847     fragsize = size % BIOSDISK_SECSIZE;
848 #else
849     if (size % BIOSDISK_SECSIZE)
850         panic("bd_strategy: %d bytes I/O not multiple of block size", size);
851 #endif
852
853     DEBUG("open_disk %p", od);
854
855     switch(rw){
856     case F_READ:
857         blks = size / BIOSDISK_SECSIZE;
858         DEBUG("read %d from %d to %p", blks, dblk, buf);
859
860         if (rsize)
861             *rsize = 0;
862         if (blks && bd_read(od, dblk, blks, buf)) {
863             DEBUG("read error");
864             return (EIO);
865         }
866 #ifdef BD_SUPPORT_FRAGS
867         DEBUG("bd_strategy: frag read %d from %d+%d to %p", 
868              fragsize, dblk, blks, buf + (blks * BIOSDISK_SECSIZE));
869         if (fragsize && bd_read(od, dblk + blks, 1, fragsize)) {
870             DEBUG("frag read error");
871             return(EIO);
872         }
873         bcopy(fragbuf, buf + (blks * BIOSDISK_SECSIZE), fragsize);
874 #endif
875         if (rsize)
876             *rsize = size;
877         return (0);
878     case F_WRITE :
879         blks = size / BIOSDISK_SECSIZE;
880         DEBUG("write %d from %d to %p", blks, dblk, buf);
881
882         if (rsize)
883             *rsize = 0;
884         if (blks && bd_write(od, dblk, blks, buf)) {
885             DEBUG("write error");
886             return (EIO);
887         }
888 #ifdef BD_SUPPORT_FRAGS
889         if(fragsize) {
890             DEBUG("Attempted to write a frag");
891             return (EIO);
892         }
893 #endif
894         if (rsize)
895             *rsize = size;
896         return (0);
897     default:
898         break; /* DO NOTHING */
899     }
900     return EROFS;
901 }
902
903 /* Max number of sectors to bounce-buffer if the request crosses a 64k boundary */
904 #define FLOPPY_BOUNCEBUF        18
905
906 static int
907 bd_read(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest)
908 {
909     u_int       x, bpc, cyl, hd, sec, result, resid, retry, maxfer;
910     caddr_t     p, xp, bbuf, breg;
911     
912     /* Just in case some idiot actually tries to read -1 blocks... */
913     if (blks < 0)
914         return (-1);
915
916     bpc = (od->od_sec * od->od_hds);            /* blocks per cylinder */
917     resid = blks;
918     p = dest;
919
920     /* Decide whether we have to bounce */
921     if ((od->od_unit < 0x80) && 
922         ((VTOP(dest) >> 16) != (VTOP(dest + blks * BIOSDISK_SECSIZE) >> 16))) {
923
924         /* 
925          * There is a 64k physical boundary somewhere in the destination buffer, so we have
926          * to arrange a suitable bounce buffer.  Allocate a buffer twice as large as we
927          * need to.  Use the bottom half unless there is a break there, in which case we
928          * use the top half.
929          */
930         x = min(FLOPPY_BOUNCEBUF, (unsigned)blks);
931         bbuf = malloc(x * 2 * BIOSDISK_SECSIZE);
932         if (((u_int32_t)VTOP(bbuf) & 0xffff0000) == ((u_int32_t)VTOP(bbuf + x * BIOSDISK_SECSIZE) & 0xffff0000)) {
933             breg = bbuf;
934         } else {
935             breg = bbuf + x * BIOSDISK_SECSIZE;
936         }
937         maxfer = x;                     /* limit transfers to bounce region size */
938     } else {
939         breg = bbuf = NULL;
940         maxfer = 0;
941     }
942     
943     while (resid > 0) {
944         x = dblk;
945         cyl = x / bpc;                  /* block # / blocks per cylinder */
946         x %= bpc;                       /* block offset into cylinder */
947         hd = x / od->od_sec;            /* offset / blocks per track */
948         sec = x % od->od_sec;           /* offset into track */
949
950         /* play it safe and don't cross track boundaries (XXX this is probably unnecessary) */
951         x = min(od->od_sec - sec, resid);
952         if (maxfer > 0)
953             x = min(x, maxfer);         /* fit bounce buffer */
954
955         /* where do we transfer to? */
956         xp = bbuf == NULL ? p : breg;
957
958         /* correct sector number for 1-based BIOS numbering */
959         sec++;
960
961         /*
962          * Loop retrying the operation a couple of times.  The BIOS may also
963          * retry.
964          */
965         for (retry = 0; retry < 3; retry++) {
966             /*
967              * If retrying, reset the drive.
968              */
969             if (retry > 0) {
970                 v86.ctl = V86_FLAGS;
971                 v86.addr = 0x13;
972                 v86.eax = 0;
973                 v86.edx = od->od_unit;
974                 v86int();
975             }
976             
977             /*
978              * Always use EDD if the disk supports it, otherwise fall back
979              * to CHS mode (returning an error if the cylinder number is
980              * too large).
981              */
982             if (od->od_flags & BD_MODEEDD1) {
983                 static unsigned short packet[8];
984
985                 packet[0] = 0x10;
986                 packet[1] = x;
987                 packet[2] = VTOPOFF(xp);
988                 packet[3] = VTOPSEG(xp);
989                 packet[4] = dblk & 0xffff;
990                 packet[5] = dblk >> 16;
991                 packet[6] = 0;
992                 packet[7] = 0;
993                 v86.ctl = V86_FLAGS;
994                 v86.addr = 0x13;
995                 v86.eax = 0x4200;
996                 v86.edx = od->od_unit;
997                 v86.ds = VTOPSEG(packet);
998                 v86.esi = VTOPOFF(packet);
999                 v86int();
1000                 result = (v86.efl & 0x1);
1001                 if (result == 0)
1002                     break;
1003             } else if (cyl < 1024) {
1004                 /* Use normal CHS addressing */
1005                 v86.ctl = V86_FLAGS;
1006                 v86.addr = 0x13;
1007                 v86.eax = 0x200 | x;
1008                 v86.ecx = ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec;
1009                 v86.edx = (hd << 8) | od->od_unit;
1010                 v86.es = VTOPSEG(xp);
1011                 v86.ebx = VTOPOFF(xp);
1012                 v86int();
1013                 result = (v86.efl & 0x1);
1014                 if (result == 0)
1015                     break;
1016             } else {
1017                 result = 1;
1018                 break;
1019             }
1020         }
1021         
1022         DEBUG("%d sectors from %d/%d/%d to %p (0x%x) %s", x, cyl, hd, sec - 1, p, VTOP(p), result ? "failed" : "ok");
1023         /* BUG here, cannot use v86 in printf because putchar uses it too */
1024         DEBUG("ax = 0x%04x cx = 0x%04x dx = 0x%04x status 0x%x", 
1025               0x200 | x, ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec, (hd << 8) | od->od_unit, (v86.eax >> 8) & 0xff);
1026         if (result) {
1027             if (bbuf != NULL)
1028                 free(bbuf);
1029             return(-1);
1030         }
1031         if (bbuf != NULL)
1032             bcopy(breg, p, x * BIOSDISK_SECSIZE);
1033         p += (x * BIOSDISK_SECSIZE);
1034         dblk += x;
1035         resid -= x;
1036     }
1037         
1038 /*    hexdump(dest, (blks * BIOSDISK_SECSIZE)); */
1039     if (bbuf != NULL)
1040         free(bbuf);
1041     return(0);
1042 }
1043
1044
1045 static int
1046 bd_write(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest)
1047 {
1048     u_int       x, bpc, cyl, hd, sec, result, resid, retry, maxfer;
1049     caddr_t     p, xp, bbuf, breg;
1050     
1051     /* Just in case some idiot actually tries to read -1 blocks... */
1052     if (blks < 0)
1053         return (-1);
1054
1055     bpc = (od->od_sec * od->od_hds);            /* blocks per cylinder */
1056     resid = blks;
1057     p = dest;
1058
1059     /* Decide whether we have to bounce */
1060     if ((od->od_unit < 0x80) && 
1061         ((VTOP(dest) >> 16) != (VTOP(dest + blks * BIOSDISK_SECSIZE) >> 16))) {
1062
1063         /* 
1064          * There is a 64k physical boundary somewhere in the destination buffer, so we have
1065          * to arrange a suitable bounce buffer.  Allocate a buffer twice as large as we
1066          * need to.  Use the bottom half unless there is a break there, in which case we
1067          * use the top half.
1068          */
1069
1070         x = min(FLOPPY_BOUNCEBUF, (unsigned)blks);
1071         bbuf = malloc(x * 2 * BIOSDISK_SECSIZE);
1072         if (((u_int32_t)VTOP(bbuf) & 0xffff0000) == ((u_int32_t)VTOP(bbuf + x * BIOSDISK_SECSIZE) & 0xffff0000)) {
1073             breg = bbuf;
1074         } else {
1075             breg = bbuf + x * BIOSDISK_SECSIZE;
1076         }
1077         maxfer = x;                     /* limit transfers to bounce region size */
1078     } else {
1079         breg = bbuf = NULL;
1080         maxfer = 0;
1081     }
1082
1083     while (resid > 0) {
1084         x = dblk;
1085         cyl = x / bpc;                  /* block # / blocks per cylinder */
1086         x %= bpc;                       /* block offset into cylinder */
1087         hd = x / od->od_sec;            /* offset / blocks per track */
1088         sec = x % od->od_sec;           /* offset into track */
1089
1090         /* play it safe and don't cross track boundaries (XXX this is probably unnecessary) */
1091         x = min(od->od_sec - sec, resid);
1092         if (maxfer > 0)
1093             x = min(x, maxfer);         /* fit bounce buffer */
1094
1095         /* where do we transfer to? */
1096         xp = bbuf == NULL ? p : breg;
1097
1098         /* correct sector number for 1-based BIOS numbering */
1099         sec++;
1100
1101
1102         /* Put your Data In, Put your Data out,
1103            Put your Data In, and shake it all about 
1104         */
1105         if (bbuf != NULL)
1106             bcopy(p, breg, x * BIOSDISK_SECSIZE);
1107         p += (x * BIOSDISK_SECSIZE);
1108         dblk += x;
1109         resid -= x;
1110
1111         /*
1112          * Loop retrying the operation a couple of times.  The BIOS may also
1113          * retry.
1114          */
1115         for (retry = 0; retry < 3; retry++) {
1116             /*
1117              * If retrying, reset the drive.
1118              */
1119             if (retry > 0) {
1120                 v86.ctl = V86_FLAGS;
1121                 v86.addr = 0x13;
1122                 v86.eax = 0;
1123                 v86.edx = od->od_unit;
1124                 v86int();
1125             }
1126
1127             /*
1128              * Always use EDD if the disk supports it, otherwise fall back
1129              * to CHS mode (returning an error if the cylinder number is
1130              * too large).
1131              */
1132             if (od->od_flags & BD_MODEEDD1) {
1133                 static unsigned short packet[8];
1134
1135                 packet[0] = 0x10;
1136                 packet[1] = x;
1137                 packet[2] = VTOPOFF(xp);
1138                 packet[3] = VTOPSEG(xp);
1139                 packet[4] = dblk & 0xffff;
1140                 packet[5] = dblk >> 16;
1141                 packet[6] = 0;
1142                 packet[7] = 0;
1143                 v86.ctl = V86_FLAGS;
1144                 v86.addr = 0x13;
1145                     /* Should we Write with verify ?? 0x4302 ? */
1146                 v86.eax = 0x4300;
1147                 v86.edx = od->od_unit;
1148                 v86.ds = VTOPSEG(packet);
1149                 v86.esi = VTOPOFF(packet);
1150                 v86int();
1151                 result = (v86.efl & 0x1);
1152                 if (result == 0)
1153                     break;
1154             } else if (cyl < 1024) {
1155                 /* Use normal CHS addressing */
1156                 v86.ctl = V86_FLAGS;
1157                 v86.addr = 0x13;
1158                 v86.eax = 0x300 | x;
1159                 v86.ecx = ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec;
1160                 v86.edx = (hd << 8) | od->od_unit;
1161                 v86.es = VTOPSEG(xp);
1162                 v86.ebx = VTOPOFF(xp);
1163                 v86int();
1164                 result = (v86.efl & 0x1);
1165                 if (result == 0)
1166                     break;
1167             } else {
1168                 result = 1;
1169                 break;
1170             }
1171         }
1172         
1173         DEBUG("%d sectors from %d/%d/%d to %p (0x%x) %s", x, cyl, hd, sec - 1, p, VTOP(p), result ? "failed" : "ok");
1174         /* BUG here, cannot use v86 in printf because putchar uses it too */
1175         DEBUG("ax = 0x%04x cx = 0x%04x dx = 0x%04x status 0x%x", 
1176               0x200 | x, ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec, (hd << 8) | od->od_unit, (v86.eax >> 8) & 0xff);
1177         if (result) {
1178             if (bbuf != NULL)
1179                 free(bbuf);
1180             return(-1);
1181         }
1182     }
1183         
1184 /*    hexdump(dest, (blks * BIOSDISK_SECSIZE)); */
1185     if (bbuf != NULL)
1186         free(bbuf);
1187     return(0);
1188 }
1189 static int
1190 bd_getgeom(struct open_disk *od)
1191 {
1192
1193     v86.ctl = V86_FLAGS;
1194     v86.addr = 0x13;
1195     v86.eax = 0x800;
1196     v86.edx = od->od_unit;
1197     v86int();
1198
1199     if ((v86.efl & 0x1) ||                              /* carry set */
1200         ((v86.edx & 0xff) <= (unsigned)(od->od_unit & 0x7f)))   /* unit # bad */
1201         return(1);
1202     
1203     /* convert max cyl # -> # of cylinders */
1204     od->od_cyl = ((v86.ecx & 0xc0) << 2) + ((v86.ecx & 0xff00) >> 8) + 1;
1205     /* convert max head # -> # of heads */
1206     od->od_hds = ((v86.edx & 0xff00) >> 8) + 1;
1207     od->od_sec = v86.ecx & 0x3f;
1208
1209     DEBUG("unit 0x%x geometry %d/%d/%d", od->od_unit, od->od_cyl, od->od_hds, od->od_sec);
1210     return(0);
1211 }
1212
1213 /*
1214  * Return the BIOS geometry of a given "fixed drive" in a format
1215  * suitable for the legacy bootinfo structure.  Since the kernel is
1216  * expecting raw int 0x13/0x8 values for N_BIOS_GEOM drives, we
1217  * prefer to get the information directly, rather than rely on being
1218  * able to put it together from information already maintained for
1219  * different purposes and for a probably different number of drives.
1220  *
1221  * For valid drives, the geometry is expected in the format (31..0)
1222  * "000000cc cccccccc hhhhhhhh 00ssssss"; and invalid drives are
1223  * indicated by returning the geometry of a "1.2M" PC-format floppy
1224  * disk.  And, incidentally, what is returned is not the geometry as
1225  * such but the highest valid cylinder, head, and sector numbers.
1226  */
1227 u_int32_t
1228 bd_getbigeom(int bunit)
1229 {
1230
1231     v86.ctl = V86_FLAGS;
1232     v86.addr = 0x13;
1233     v86.eax = 0x800;
1234     v86.edx = 0x80 + bunit;
1235     v86int();
1236     if (v86.efl & 0x1)
1237         return 0x4f010f;
1238     return ((v86.ecx & 0xc0) << 18) | ((v86.ecx & 0xff00) << 8) |
1239            (v86.edx & 0xff00) | (v86.ecx & 0x3f);
1240 }
1241
1242 /*
1243  * Return a suitable cdev_t value for (dev).
1244  *
1245  * In the case where it looks like (dev) is a SCSI disk, we allow the number of
1246  * IDE disks to be specified in $num_ide_disks.  There should be a Better Way.
1247  */
1248 int
1249 bd_getdev(struct i386_devdesc *dev)
1250 {
1251     struct open_disk            *od;
1252     int                         biosdev;
1253     int                         major;
1254     int                         rootdev;
1255     char                        *nip, *cp;
1256     int                         unitofs = 0, i, unit;
1257
1258     biosdev = bd_unit2bios(dev->d_kind.biosdisk.unit);
1259     DEBUG("unit %d BIOS device %d", dev->d_kind.biosdisk.unit, biosdev);
1260     if (biosdev == -1)                          /* not a BIOS device */
1261         return(-1);
1262     if (bd_opendisk(&od, dev) != 0)             /* oops, not a viable device */
1263         return(-1);
1264
1265     if (biosdev < 0x80) {
1266         /* floppy (or emulated floppy) or ATAPI device */
1267         if (bdinfo[dev->d_kind.biosdisk.unit].bd_type == DT_ATAPI) {
1268             /* is an ATAPI disk */
1269             major = WFDMAJOR;
1270         } else {
1271             /* is a floppy disk */
1272             major = FDMAJOR;
1273         }
1274     } else {
1275         /* harddisk */
1276         if ((od->od_flags & BD_LABELOK) && (od->od_disklabel.d_type == DTYPE_SCSI)) {
1277             /* label OK, disk labelled as SCSI */
1278             major = DAMAJOR;
1279             /* check for unit number correction hint, now deprecated */
1280             if ((nip = getenv("num_ide_disks")) != NULL) {
1281                 i = strtol(nip, &cp, 0);
1282                 /* check for parse error */
1283                 if ((cp != nip) && (*cp == 0))
1284                     unitofs = i;
1285             }
1286         } else {
1287             /* assume an IDE disk */
1288             major = WDMAJOR;
1289         }
1290     }
1291     /* default root disk unit number */
1292     unit = (biosdev & 0x7f) - unitofs;
1293
1294     /* XXX a better kludge to set the root disk unit number */
1295     if ((nip = getenv("root_disk_unit")) != NULL) {
1296         i = strtol(nip, &cp, 0);
1297         /* check for parse error */
1298         if ((cp != nip) && (*cp == 0))
1299             unit = i;
1300     }
1301
1302     rootdev = MAKEBOOTDEV(major,
1303                           (dev->d_kind.biosdisk.slice + 1) >> 4,        /* XXX slices may be wrong here */
1304                           (dev->d_kind.biosdisk.slice + 1) & 0xf, 
1305                           unit,
1306                           dev->d_kind.biosdisk.partition);
1307     DEBUG("dev is 0x%x\n", rootdev);
1308     return(rootdev);
1309 }