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