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