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