Fully synchronize sys/boot from FreeBSD-5.x, but add / to the module path
[dragonfly.git] / sys / boot / pc98 / libpc98 / biosdisk.c
1 /*-
2  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/boot/pc98/libpc98/biosdisk.c,v 1.25 2003/09/08 09:11:20 obrien Exp $
27  * $DragonFly: src/sys/boot/pc98/libpc98/Attic/biosdisk.c,v 1.4 2003/11/10 06:08:39 dillon Exp $
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/diskpc98.h>
43 #include <machine/bootinfo.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" , __func__ , ## 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 #ifdef PC98
84 #define BD_OPTICAL              0x0020
85 #endif
86     struct disklabel            od_disklabel;
87     int                         od_nslices;     /* slice count */
88     struct pc98_partition       od_slicetab[NDOSPART];
89 };
90
91 /*
92  * List of BIOS devices, translation from disk unit number to
93  * BIOS unit number.
94  */
95 static struct bdinfo
96 {
97     int         bd_unit;                /* BIOS unit number */
98     int         bd_flags;
99     int         bd_type;                /* BIOS 'drive type' (floppy only) */
100 #ifdef PC98
101     int         bd_da_unit;             /* kernel unit number for da */
102 #endif
103 } bdinfo [MAXBDDEV];
104 static int nbdinfo = 0;
105
106 static int      bd_getgeom(struct open_disk *od);
107 static int      bd_read(struct open_disk *od, daddr_t dblk, int blks,
108                     caddr_t dest);
109 static int      bd_write(struct open_disk *od, daddr_t dblk, int blks,
110                     caddr_t dest);
111
112 static int      bd_int13probe(struct bdinfo *bd);
113
114 static void     bd_printslice(struct open_disk *od, struct pc98_partition *dp,
115                     char *prefix, int verbose);
116 static void     bd_printbsdslice(struct open_disk *od, daddr_t offset,
117                     char *prefix, int verbose);
118
119 static int      bd_init(void);
120 static int      bd_strategy(void *devdata, int flag, daddr_t dblk,
121                     size_t size, char *buf, size_t *rsize);
122 static int      bd_realstrategy(void *devdata, int flag, daddr_t dblk,
123                     size_t size, char *buf, size_t *rsize);
124 static int      bd_open(struct open_file *f, ...);
125 static int      bd_close(struct open_file *f);
126 static void     bd_print(int verbose);
127
128 struct devsw biosdisk = {
129     "disk", 
130     DEVT_DISK, 
131     bd_init,
132     bd_strategy, 
133     bd_open, 
134     bd_close, 
135     noioctl,
136     bd_print,
137     NULL
138 };
139
140 static int      bd_opendisk(struct open_disk **odp, struct i386_devdesc *dev);
141 static void     bd_closedisk(struct open_disk *od);
142 static int      bd_bestslice(struct open_disk *od);
143 static void     bd_checkextended(struct open_disk *od, int slicenum);
144
145 /*
146  * Translate between BIOS device numbers and our private unit numbers.
147  */
148 int
149 bd_bios2unit(int biosdev)
150 {
151     int         i;
152     
153     DEBUG("looking for bios device 0x%x", biosdev);
154     for (i = 0; i < nbdinfo; i++) {
155         DEBUG("bd unit %d is BIOS device 0x%x", i, bdinfo[i].bd_unit);
156         if (bdinfo[i].bd_unit == biosdev)
157             return(i);
158     }
159     return(-1);
160 }
161
162 int
163 bd_unit2bios(int unit)
164 {
165     if ((unit >= 0) && (unit < nbdinfo))
166         return(bdinfo[unit].bd_unit);
167     return(-1);
168 }
169
170 /*    
171  * Quiz the BIOS for disk devices, save a little info about them.
172  */
173 static int
174 bd_init(void) 
175 {
176 #ifdef PC98
177     int         base, unit;
178     int         da_drive=0, n=-0x10;
179
180     /* sequence 0x90, 0x80, 0xa0 */
181     for (base = 0x90; base <= 0xa0; base += n, n += 0x30) {
182         for (unit = base; (nbdinfo < MAXBDDEV) || ((unit & 0x0f) < 4); unit++) {
183             bdinfo[nbdinfo].bd_unit = unit;
184             bdinfo[nbdinfo].bd_flags = (unit & 0xf0) == 0x90 ? BD_FLOPPY : 0;
185
186             if (!bd_int13probe(&bdinfo[nbdinfo])){
187                 if (((unit & 0xf0) == 0x90 && (unit & 0x0f) < 4) ||
188                     ((unit & 0xf0) == 0xa0 && (unit & 0x0f) < 6))
189                     continue;   /* Target IDs are not contiguous. */
190                 else
191                     break;
192             }
193
194             if (bdinfo[nbdinfo].bd_flags & BD_FLOPPY){
195                 /* available 1.44MB access? */
196                 if (*(u_char *)PTOV(0xA15AE) & (1<<(unit & 0xf))) {
197                     /* boot media 1.2MB FD? */
198                     if ((*(u_char *)PTOV(0xA1584) & 0xf0) != 0x90)
199                         bdinfo[nbdinfo].bd_unit = 0x30 + (unit & 0xf);
200                 }
201             }
202             else {
203                 if ((unit & 0xF0) == 0xA0)      /* SCSI HD or MO */
204                     bdinfo[nbdinfo].bd_da_unit = da_drive++;
205             }
206             /* XXX we need "disk aliases" to make this simpler */
207             printf("BIOS drive %c: is disk%d\n", 
208                    'A' + nbdinfo, nbdinfo);
209             nbdinfo++;
210         }
211     }
212 #else
213     int         base, unit, nfd = 0;
214
215     /* sequence 0, 0x80 */
216     for (base = 0; base <= 0x80; base += 0x80) {
217         for (unit = base; (nbdinfo < MAXBDDEV); unit++) {
218             /* check the BIOS equipment list for number of fixed disks */
219             if((base == 0x80) &&
220                (nfd >= *(unsigned char *)PTOV(BIOS_NUMDRIVES)))
221                 break;
222
223             bdinfo[nbdinfo].bd_unit = unit;
224             bdinfo[nbdinfo].bd_flags = (unit < 0x80) ? BD_FLOPPY : 0;
225
226             if (!bd_int13probe(&bdinfo[nbdinfo]))
227                 break;
228
229             /* XXX we need "disk aliases" to make this simpler */
230             printf("BIOS drive %c: is disk%d\n", 
231                    (unit < 0x80) ? ('A' + unit) : ('C' + unit - 0x80), nbdinfo);
232             nbdinfo++;
233             if (base == 0x80)
234                 nfd++;
235         }
236     }
237 #endif
238     return(0);
239 }
240
241 /*
242  * Try to detect a device supported by the legacy int13 BIOS
243  */
244 static int
245 bd_int13probe(struct bdinfo *bd)
246 {
247 #ifdef PC98
248     int addr;
249
250     if (bd->bd_flags & BD_FLOPPY) {
251         addr = 0xa155c;
252     } else {
253         if ((bd->bd_unit & 0xf0) == 0x80)
254             addr = 0xa155d;
255         else
256             addr = 0xa1482;
257     }
258     if ( *(u_char *)PTOV(addr) & (1<<(bd->bd_unit & 0x0f))) {
259         bd->bd_flags |= BD_MODEINT13;
260         return(1);
261     }
262     if ((bd->bd_unit & 0xF0) == 0xA0) {
263         int media = ((unsigned *)PTOV(0xA1460))[bd->bd_unit & 0x0F] & 0x1F;
264
265         if (media == 7) { /* MO */
266             bd->bd_flags |= BD_MODEINT13 | BD_OPTICAL;
267             return(1);
268         }
269     }
270     return(0);
271 #else
272     v86.ctl = V86_FLAGS;
273     v86.addr = 0x13;
274     v86.eax = 0x800;
275     v86.edx = bd->bd_unit;
276     v86int();
277     
278     if (!(v86.efl & 0x1) &&                             /* carry clear */
279         ((v86.edx & 0xff) > ((unsigned)bd->bd_unit & 0x7f))) {  /* unit # OK */
280         bd->bd_flags |= BD_MODEINT13;
281         bd->bd_type = v86.ebx & 0xff;
282
283         /* Determine if we can use EDD with this device. */
284         v86.eax = 0x4100;
285         v86.edx = bd->bd_unit;
286         v86.ebx = 0x55aa;
287         v86int();
288         if (!(v86.efl & 0x1) &&                         /* carry clear */
289             ((v86.ebx & 0xffff) == 0xaa55) &&           /* signature */
290             (v86.ecx & 0x1)) {                          /* packets mode ok */
291             bd->bd_flags |= BD_MODEEDD1;
292             if((v86.eax & 0xff00) > 0x300)
293                 bd->bd_flags |= BD_MODEEDD3;
294         }
295         return(1);
296     }
297     return(0);
298 #endif
299 }
300
301 /*
302  * Print information about disks
303  */
304 static void
305 bd_print(int verbose)
306 {
307     int                         i, j;
308     char                        line[80];
309     struct i386_devdesc         dev;
310     struct open_disk            *od;
311     struct pc98_partition       *dptr;
312     
313     for (i = 0; i < nbdinfo; i++) {
314 #ifdef PC98
315         sprintf(line, "    disk%d:   BIOS drive %c:\n", i, 'A' + i);
316 #else
317         sprintf(line, "    disk%d:   BIOS drive %c:\n", i, 
318                 (bdinfo[i].bd_unit < 0x80) ? ('A' + bdinfo[i].bd_unit) : ('C' + bdinfo[i].bd_unit - 0x80));
319 #endif
320         pager_output(line);
321
322         /* try to open the whole disk */
323         dev.d_kind.biosdisk.unit = i;
324         dev.d_kind.biosdisk.slice = -1;
325         dev.d_kind.biosdisk.partition = -1;
326         
327         if (!bd_opendisk(&od, &dev)) {
328
329             /* Do we have a partition table? */
330             if (od->od_flags & BD_PARTTABOK) {
331                 dptr = &od->od_slicetab[0];
332
333                 /* Check for a "dedicated" disk */
334 #ifdef PC98
335                 for (j = 0; j < od->od_nslices; j++) {
336                     switch(dptr[j].dp_mid) {
337                     case DOSMID_386BSD:
338                         sprintf(line, "      disk%ds%d", i, j + 1);
339                         bd_printbsdslice(od,
340                             dptr[j].dp_scyl * od->od_hds * od->od_sec +
341                             dptr[j].dp_shd * od->od_sec + dptr[j].dp_ssect,
342                             line, verbose);
343                         break;
344                     default:
345                     }
346                 }
347 #else
348                 if ((dptr[3].dp_typ == DOSPTYP_386BSD) &&
349                     (dptr[3].dp_start == 0) &&
350                     (dptr[3].dp_size == 50000)) {
351                     sprintf(line, "      disk%d", i);
352                     bd_printbsdslice(od, 0, line, verbose);
353                 } else {
354                     for (j = 0; j < od->od_nslices; j++) {
355                         sprintf(line, "      disk%ds%d", i, j + 1);
356                         bd_printslice(od, &dptr[j], line, verbose);
357                     }
358                 }
359 #endif
360             }
361             bd_closedisk(od);
362         }
363     }
364 }
365
366 #ifndef PC98
367 /*
368  * Print information about slices on a disk.  For the size calculations we
369  * assume a 512 byte sector.
370  */
371 static void
372 bd_printslice(struct open_disk *od, struct dos_partition *dp, char *prefix,
373         int verbose)
374 {
375         char line[80];
376
377         switch (dp->dp_typ) {
378         case DOSPTYP_386BSD:
379                 bd_printbsdslice(od, (daddr_t)dp->dp_start, prefix, verbose);
380                 return;
381         case DOSPTYP_LINSWP:
382                 if (verbose)
383                         sprintf(line, "%s: Linux swap %.6dMB (%d - %d)\n",
384                             prefix, dp->dp_size / 2048,
385                             dp->dp_start, dp->dp_start + dp->dp_size);
386                 else
387                         sprintf(line, "%s: Linux swap\n", prefix);
388                 break;
389         case DOSPTYP_LINUX:
390                 /*
391                  * XXX
392                  * read the superblock to confirm this is an ext2fs partition?
393                  */
394                 if (verbose)
395                         sprintf(line, "%s: ext2fs  %.6dMB (%d - %d)\n", prefix,
396                             dp->dp_size / 2048, dp->dp_start,
397                             dp->dp_start + dp->dp_size);
398                 else
399                         sprintf(line, "%s: ext2fs\n", prefix);
400                 break;
401         case 0x00:                              /* unused partition */
402         case DOSPTYP_EXT:
403                 return;
404         case 0x01:
405                 if (verbose)
406                         sprintf(line, "%s: FAT-12  %.6dMB (%d - %d)\n", prefix,
407                             dp->dp_size / 2048, dp->dp_start,
408                             dp->dp_start + dp->dp_size);
409                 else
410                         sprintf(line, "%s: FAT-12\n", prefix);
411                 break;
412         case 0x04:
413         case 0x06:
414         case 0x0e:
415                 if (verbose)
416                         sprintf(line, "%s: FAT-16  %.6dMB (%d - %d)\n", prefix,
417                             dp->dp_size / 2048, dp->dp_start,
418                             dp->dp_start + dp->dp_size);
419                 else
420                         sprintf(line, "%s: FAT-16\n", prefix);
421                 break;
422         case 0x0b:
423         case 0x0c:
424                 if (verbose)
425                         sprintf(line, "%s: FAT-32  %.6dMB (%d - %d)\n", prefix,
426                             dp->dp_size / 2048, dp->dp_start,
427                             dp->dp_start + dp->dp_size);
428                 else
429                         sprintf(line, "%s: FAT-32\n", prefix);
430                 break;
431         default:
432                 if (verbose)
433                         sprintf(line, "%s: Unknown fs: 0x%x  %.6dMB (%d - %d)\n",
434                             prefix, dp->dp_typ, dp->dp_size / 2048,
435                             dp->dp_start, dp->dp_start + dp->dp_size);
436                 else
437                         sprintf(line, "%s: Unknown fs: 0x%x\n", prefix,
438                             dp->dp_typ);
439         }
440         pager_output(line);
441 }
442 #endif
443
444 /*
445  * Print out each valid partition in the disklabel of a FreeBSD slice.
446  * For size calculations, we assume a 512 byte sector size.
447  */
448 static void
449 bd_printbsdslice(struct open_disk *od, daddr_t offset, char *prefix,
450     int verbose)
451 {
452     char                line[80];
453     char                buf[BIOSDISK_SECSIZE];
454     struct disklabel    *lp;
455     int                 i;
456
457     /* read disklabel */
458     if (bd_read(od, offset + LABELSECTOR, 1, buf))
459         return;
460     lp =(struct disklabel *)(&buf[0]);
461     if (lp->d_magic != DISKMAGIC) {
462         sprintf(line, "%s: FFS  bad disklabel\n", prefix);
463         pager_output(line);
464         return;
465     }
466     
467     /* Print partitions */
468     for (i = 0; i < lp->d_npartitions; i++) {
469         /*
470          * For each partition, make sure we know what type of fs it is.  If
471          * not, then skip it.  However, since floppies often have bogus
472          * fstypes, print the 'a' partition on a floppy even if it is marked
473          * unused.
474          */
475         if ((lp->d_partitions[i].p_fstype == FS_BSDFFS) ||
476             (lp->d_partitions[i].p_fstype == FS_SWAP) ||
477             (lp->d_partitions[i].p_fstype == FS_VINUM) ||
478             ((lp->d_partitions[i].p_fstype == FS_UNUSED) && 
479              (od->od_flags & BD_FLOPPY) && (i == 0))) {
480
481             /* Only print out statistics in verbose mode */
482             if (verbose)
483                 sprintf(line, "  %s%c: %s  %.6dMB (%d - %d)\n", prefix, 'a' + i,
484                     (lp->d_partitions[i].p_fstype == FS_SWAP) ? "swap" : 
485                     (lp->d_partitions[i].p_fstype == FS_VINUM) ? "vinum" :
486                     "FFS",
487                     lp->d_partitions[i].p_size / 2048,
488                     lp->d_partitions[i].p_offset,
489                     lp->d_partitions[i].p_offset + lp->d_partitions[i].p_size);
490             else
491                 sprintf(line, "  %s%c: %s\n", prefix, 'a' + i,
492                     (lp->d_partitions[i].p_fstype == FS_SWAP) ? "swap" : 
493                     (lp->d_partitions[i].p_fstype == FS_VINUM) ? "vinum" :
494                     "FFS");
495             pager_output(line);
496         }
497     }
498 }
499
500
501 /*
502  * Attempt to open the disk described by (dev) for use by (f).
503  *
504  * Note that the philosophy here is "give them exactly what
505  * they ask for".  This is necessary because being too "smart"
506  * about what the user might want leads to complications.
507  * (eg. given no slice or partition value, with a disk that is
508  *  sliced - are they after the first BSD slice, or the DOS
509  *  slice before it?)
510  */
511 static int 
512 bd_open(struct open_file *f, ...)
513 {
514     va_list                     ap;
515     struct i386_devdesc         *dev;
516     struct open_disk            *od;
517     int                         error;
518
519     va_start(ap, f);
520     dev = va_arg(ap, struct i386_devdesc *);
521     va_end(ap);
522     if ((error = bd_opendisk(&od, dev)))
523         return(error);
524     
525     /*
526      * Save our context
527      */
528     ((struct i386_devdesc *)(f->f_devdata))->d_kind.biosdisk.data = od;
529     DEBUG("open_disk %p, partition at 0x%x", od, od->od_boff);
530     return(0);
531 }
532
533 static int
534 bd_opendisk(struct open_disk **odp, struct i386_devdesc *dev)
535 {
536     struct pc98_partition       *dptr;
537     struct disklabel            *lp;
538     struct open_disk            *od;
539     int                         sector, slice, i;
540     int                         error;
541     char                        buf[BUFSIZE];
542
543     if (dev->d_kind.biosdisk.unit >= nbdinfo) {
544         DEBUG("attempt to open nonexistent disk");
545         return(ENXIO);
546     }
547     
548     od = (struct open_disk *)malloc(sizeof(struct open_disk));
549     if (!od) {
550         DEBUG("no memory");
551         return (ENOMEM);
552     }
553
554     /* Look up BIOS unit number, intialise open_disk structure */
555     od->od_dkunit = dev->d_kind.biosdisk.unit;
556     od->od_unit = bdinfo[od->od_dkunit].bd_unit;
557     od->od_flags = bdinfo[od->od_dkunit].bd_flags;
558     od->od_boff = 0;
559     od->od_nslices = 0;
560     error = 0;
561     DEBUG("open '%s', unit 0x%x slice %d partition %c",
562              i386_fmtdev(dev), dev->d_kind.biosdisk.unit, 
563              dev->d_kind.biosdisk.slice, dev->d_kind.biosdisk.partition + 'a');
564
565     /* Get geometry for this open (removable device may have changed) */
566     if (bd_getgeom(od)) {
567         DEBUG("can't get geometry");
568         error = ENXIO;
569         goto out;
570     }
571
572     /*
573      * Following calculations attempt to determine the correct value
574      * for d->od_boff by looking for the slice and partition specified,
575      * or searching for reasonable defaults.
576      */
577
578     /*
579      * Find the slice in the DOS slice table.
580      */
581 #ifdef PC98
582     if (od->od_flags & BD_FLOPPY) {
583         sector = 0;
584         goto unsliced;
585     }
586 #endif
587     if (bd_read(od, 0, 1, buf)) {
588         DEBUG("error reading MBR");
589         error = EIO;
590         goto out;
591     }
592
593     /* 
594      * Check the slice table magic.
595      */
596     if (((u_char)buf[0x1fe] != 0x55) || ((u_char)buf[0x1ff] != 0xaa)) {
597         /* If a slice number was explicitly supplied, this is an error */
598         if (dev->d_kind.biosdisk.slice > 0) {
599             DEBUG("no slice table/MBR (no magic)");
600             error = ENOENT;
601             goto out;
602         }
603         sector = 0;
604         goto unsliced;          /* may be a floppy */
605     }
606 #ifdef PC98
607     if (bd_read(od, 1, 1, buf)) {
608         DEBUG("error reading MBR");
609         error = EIO;
610         goto out;
611     }
612 #endif
613
614     /*
615      * copy the partition table, then pick up any extended partitions.
616      */
617     bcopy(buf + DOSPARTOFF, &od->od_slicetab,
618       sizeof(struct pc98_partition) * NDOSPART);
619 #ifdef PC98
620     od->od_nslices = NDOSPART;          /* extended slices start here */
621 #else
622     od->od_nslices = 4;                 /* extended slices start here */
623     for (i = 0; i < NDOSPART; i++)
624         bd_checkextended(od, i);
625 #endif
626     od->od_flags |= BD_PARTTABOK;
627     dptr = &od->od_slicetab[0];
628
629     /* Is this a request for the whole disk? */
630     if (dev->d_kind.biosdisk.slice == -1) {
631         sector = 0;
632         goto unsliced;
633     }
634
635     /*
636      * if a slice number was supplied but not found, this is an error.
637      */
638     if (dev->d_kind.biosdisk.slice > 0) {
639         slice = dev->d_kind.biosdisk.slice - 1;
640         if (slice >= od->od_nslices) {
641             DEBUG("slice %d not found", slice);
642             error = ENOENT;
643             goto out;
644         }
645     }
646
647 #ifndef PC98
648     /*
649      * Check for the historically bogus MBR found on true dedicated disks
650      */
651     if ((dptr[3].dp_typ == DOSPTYP_386BSD) &&
652       (dptr[3].dp_start == 0) &&
653       (dptr[3].dp_size == 50000)) {
654         sector = 0;
655         goto unsliced;
656     }
657 #endif
658
659     /* Try to auto-detect the best slice; this should always give a slice number */
660     if (dev->d_kind.biosdisk.slice == 0) {
661         slice = bd_bestslice(od);
662         if (slice == -1) {
663             error = ENOENT;
664             goto out;
665         }
666         dev->d_kind.biosdisk.slice = slice;
667     }
668
669     dptr = &od->od_slicetab[0];
670     /*
671      * Accept the supplied slice number unequivocally (we may be looking
672      * at a DOS partition).
673      */
674     dptr += (dev->d_kind.biosdisk.slice - 1);   /* we number 1-4, offsets are 0-3 */
675 #ifdef PC98
676     sector = dptr->dp_scyl * od->od_hds * od->od_sec +
677         dptr->dp_shd * od->od_sec + dptr->dp_ssect;
678     {
679         int end = dptr->dp_ecyl * od->od_hds * od->od_sec +
680             dptr->dp_ehd * od->od_sec + dptr->dp_esect;
681         DEBUG("slice entry %d at %d, %d sectors",
682               dev->d_kind.biosdisk.slice - 1, sector, end-sector);
683     }
684 #else
685     sector = dptr->dp_start;
686     DEBUG("slice entry %d at %d, %d sectors", dev->d_kind.biosdisk.slice - 1, sector, dptr->dp_size);
687 #endif
688
689     /*
690      * If we are looking at a BSD slice, and the partition is < 0, assume the 'a' partition
691      */
692 #ifdef PC98
693     if ((dptr->dp_mid == DOSMID_386BSD) && (dev->d_kind.biosdisk.partition < 0))
694 #else
695     if ((dptr->dp_typ == DOSPTYP_386BSD) && (dev->d_kind.biosdisk.partition < 0))
696 #endif
697         dev->d_kind.biosdisk.partition = 0;
698
699  unsliced:
700     /* 
701      * Now we have the slice offset, look for the partition in the disklabel if we have
702      * a partition to start with.
703      *
704      * XXX we might want to check the label checksum.
705      */
706     if (dev->d_kind.biosdisk.partition < 0) {
707         od->od_boff = sector;           /* no partition, must be after the slice */
708         DEBUG("opening raw slice");
709     } else {
710         
711         if (bd_read(od, sector + LABELSECTOR, 1, buf)) {
712             DEBUG("error reading disklabel");
713             error = EIO;
714             goto out;
715         }
716         DEBUG("copy %d bytes of label from %p to %p", sizeof(struct disklabel), buf + LABELOFFSET, &od->od_disklabel);
717         bcopy(buf + LABELOFFSET, &od->od_disklabel, sizeof(struct disklabel));
718         lp = &od->od_disklabel;
719         od->od_flags |= BD_LABELOK;
720
721         if (lp->d_magic != DISKMAGIC) {
722             DEBUG("no disklabel");
723             error = ENOENT;
724             goto out;
725         }
726         if (dev->d_kind.biosdisk.partition >= lp->d_npartitions) {
727             DEBUG("partition '%c' exceeds partitions in table (a-'%c')",
728                   'a' + dev->d_kind.biosdisk.partition, 'a' + lp->d_npartitions);
729             error = EPART;
730             goto out;
731
732         }
733
734 #ifdef DISK_DEBUG
735         /* Complain if the partition is unused unless this is a floppy. */
736         if ((lp->d_partitions[dev->d_kind.biosdisk.partition].p_fstype == FS_UNUSED) &&
737             !(od->od_flags & BD_FLOPPY))
738             DEBUG("warning, partition marked as unused");
739 #endif
740         
741         od->od_boff = lp->d_partitions[dev->d_kind.biosdisk.partition].p_offset;
742     }
743     
744  out:
745     if (error) {
746         free(od);
747     } else {
748         *odp = od;      /* return the open disk */
749     }
750     return(error);
751 }
752
753 #ifndef PC98
754 static void
755 bd_checkextended(struct open_disk *od, int slicenum)
756 {
757         char    buf[BIOSDISK_SECSIZE];
758         struct dos_partition *dp;
759         u_int base;
760         int i, start, end;
761
762         dp = &od->od_slicetab[slicenum];
763         start = od->od_nslices;
764
765         if (dp->dp_size == 0)
766                 goto done;
767         if (dp->dp_typ != DOSPTYP_EXT)
768                 goto done;
769         if (bd_read(od, (daddr_t)dp->dp_start, 1, buf))
770                 goto done;
771         if (((u_char)buf[0x1fe] != 0x55) || ((u_char)buf[0x1ff] != 0xaa)) {
772                 DEBUG("no magic in extended table");
773                 goto done;
774         }
775         base = dp->dp_start;
776         dp = (struct dos_partition *)(&buf[DOSPARTOFF]);
777         for (i = 0; i < NDOSPART; i++, dp++) {
778                 if (dp->dp_size == 0)
779                         continue;
780                 if (od->od_nslices == NDOSPART)
781                         goto done;
782                 dp->dp_start += base;
783                 bcopy(dp, &od->od_slicetab[od->od_nslices], sizeof(*dp));
784                 od->od_nslices++;
785         }
786         end = od->od_nslices;
787
788         /*
789          * now, recursively check the slices we just added
790          */
791         for (i = start; i < end; i++)
792                 bd_checkextended(od, i);
793 done:
794         return;
795 }
796 #endif
797
798 /*
799  * Search for a slice with the following preferences:
800  *
801  * 1: Active FreeBSD slice
802  * 2: Non-active FreeBSD slice
803  * 3: Active Linux slice
804  * 4: non-active Linux slice
805  * 5: Active FAT/FAT32 slice
806  * 6: non-active FAT/FAT32 slice
807  */
808 #define PREF_RAWDISK    0
809 #define PREF_FBSD_ACT   1
810 #define PREF_FBSD       2
811 #define PREF_LINUX_ACT  3
812 #define PREF_LINUX      4
813 #define PREF_DOS_ACT    5
814 #define PREF_DOS        6
815 #define PREF_NONE       7
816
817 /*
818  * slicelimit is in the range 0 .. NDOSPART
819  */
820 static int
821 bd_bestslice(struct open_disk *od)
822 {
823         struct pc98_partition *dp;
824         int pref, preflevel;
825         int i, prefslice;
826         
827         prefslice = 0;
828         preflevel = PREF_NONE;
829
830         dp = &od->od_slicetab[0];
831         for (i = 0; i < od->od_nslices; i++, dp++) {
832
833 #ifdef PC98
834                 switch(dp->dp_mid & 0x7f) {
835                 case DOSMID_386BSD & 0x7f:              /* FreeBSD */
836                         if ((dp->dp_mid & 0x80) &&
837                             (preflevel > PREF_FBSD_ACT)) {
838                                 pref = i;
839                                 preflevel = PREF_FBSD_ACT;
840                         } else if (preflevel > PREF_FBSD) {
841                                 pref = i;
842                                 preflevel = PREF_FBSD;
843                         }
844                         break;
845
846                 case 0x11:                              /* DOS/Windows */
847                 case 0x20:
848                 case 0x21:
849                 case 0x22:
850                 case 0x23:
851                 case 0x63:
852                         if ((dp->dp_mid & 0x80) &&
853                             (preflevel > PREF_DOS_ACT)) {
854                                 pref = i;
855                                 preflevel = PREF_DOS_ACT;
856                         } else if (preflevel > PREF_DOS) {
857                                 pref = i;
858                                 preflevel = PREF_DOS;
859                         }
860                         break;
861                 }
862 #else
863                 switch (dp->dp_typ) {
864                 case DOSPTYP_386BSD:            /* FreeBSD */
865                         pref = dp->dp_flag & 0x80 ? PREF_FBSD_ACT : PREF_FBSD;
866                         break;
867
868                 case DOSPTYP_LINUX:
869                         pref = dp->dp_flag & 0x80 ? PREF_LINUX_ACT : PREF_LINUX;
870                         break;
871     
872                 case 0x01:              /* DOS/Windows */
873                 case 0x04:
874                 case 0x06:
875                 case 0x0b:
876                 case 0x0c:
877                 case 0x0e:
878                         pref = dp->dp_flag & 0x80 ? PREF_DOS_ACT : PREF_DOS;
879                         break;
880
881                 default:
882                         pref = PREF_NONE;
883                 }
884                 if (pref < preflevel) {
885                         preflevel = pref;
886                         prefslice = i + 1;
887                 }
888 #endif
889         }
890         return (prefslice);
891 }
892  
893 static int 
894 bd_close(struct open_file *f)
895 {
896     struct open_disk    *od = (struct open_disk *)(((struct i386_devdesc *)(f->f_devdata))->d_kind.biosdisk.data);
897
898     bd_closedisk(od);
899     return(0);
900 }
901
902 static void
903 bd_closedisk(struct open_disk *od)
904 {
905     DEBUG("open_disk %p", od);
906 #if 0
907     /* XXX is this required? (especially if disk already open...) */
908     if (od->od_flags & BD_FLOPPY)
909         delay(3000000);
910 #endif
911     free(od);
912 }
913
914 static int 
915 bd_strategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
916 {
917     struct bcache_devdata       bcd;
918     struct open_disk    *od = (struct open_disk *)(((struct i386_devdesc *)devdata)->d_kind.biosdisk.data);
919
920     bcd.dv_strategy = bd_realstrategy;
921     bcd.dv_devdata = devdata;
922     return(bcache_strategy(&bcd, od->od_unit, rw, dblk+od->od_boff, size, buf, rsize));
923 }
924
925 static int 
926 bd_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
927 {
928     struct open_disk    *od = (struct open_disk *)(((struct i386_devdesc *)devdata)->d_kind.biosdisk.data);
929     int                 blks;
930 #ifdef BD_SUPPORT_FRAGS
931     char                fragbuf[BIOSDISK_SECSIZE];
932     size_t              fragsize;
933
934     fragsize = size % BIOSDISK_SECSIZE;
935 #else
936     if (size % BIOSDISK_SECSIZE)
937         panic("bd_strategy: %d bytes I/O not multiple of block size", size);
938 #endif
939
940     DEBUG("open_disk %p", od);
941
942
943         switch(rw){
944                 case F_READ:
945     blks = size / BIOSDISK_SECSIZE;
946     DEBUG("read %d from %d to %p", blks, dblk, buf);
947
948     if (rsize)
949         *rsize = 0;
950     if (blks && bd_read(od, dblk, blks, buf)) {
951         DEBUG("read error");
952         return (EIO);
953     }
954 #ifdef BD_SUPPORT_FRAGS
955     DEBUG("bd_strategy: frag read %d from %d+%d to %p", 
956              fragsize, dblk, blks, buf + (blks * BIOSDISK_SECSIZE));
957     if (fragsize && bd_read(od, dblk + blks, 1, fragsize)) {
958         DEBUG("frag read error");
959         return(EIO);
960     }
961     bcopy(fragbuf, buf + (blks * BIOSDISK_SECSIZE), fragsize);
962 #endif
963     if (rsize)
964         *rsize = size;
965     return (0);
966                 break;
967
968                 case F_WRITE :
969     blks = size / BIOSDISK_SECSIZE;
970     DEBUG("write %d from %d to %p", blks, dblk, buf);
971
972     if (rsize)
973         *rsize = 0;
974     if (blks && bd_write(od, dblk, blks, buf)) {
975         DEBUG("write error");
976         return (EIO);
977     }
978 #ifdef BD_SUPPORT_FRAGS
979         if(fragsize) {
980         DEBUG("Attempted to write a frag");
981                 return (EIO);
982         }
983 #endif
984
985     if (rsize)
986         *rsize = size;
987     return (0);
988                 default:
989                  /* DO NOTHING */
990         }
991
992         return EROFS;
993 }
994
995 /* Max number of sectors to bounce-buffer if the request crosses a 64k boundary */
996 #define FLOPPY_BOUNCEBUF        18
997
998 static int
999 bd_read(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest)
1000 {
1001     u_int       x, bpc, cyl, hd, sec, result, resid, retry, maxfer;
1002     caddr_t     p, xp, bbuf, breg;
1003     
1004     /* Just in case some idiot actually tries to read -1 blocks... */
1005     if (blks < 0)
1006         return (-1);
1007
1008     bpc = (od->od_sec * od->od_hds);            /* blocks per cylinder */
1009     resid = blks;
1010     p = dest;
1011
1012     /* Decide whether we have to bounce */
1013 #ifdef PC98
1014     if (
1015 #else
1016     if ((od->od_unit < 0x80) && 
1017 #endif
1018         ((VTOP(dest) >> 16) != (VTOP(dest + blks * BIOSDISK_SECSIZE) >> 16))) {
1019
1020         /* 
1021          * There is a 64k physical boundary somewhere in the destination buffer, so we have
1022          * to arrange a suitable bounce buffer.  Allocate a buffer twice as large as we
1023          * need to.  Use the bottom half unless there is a break there, in which case we
1024          * use the top half.
1025          */
1026 #ifdef PC98
1027         x = min(od->od_sec, (unsigned)blks);
1028 #else
1029         x = min(FLOPPY_BOUNCEBUF, (unsigned)blks);
1030 #endif
1031         bbuf = malloc(x * 2 * BIOSDISK_SECSIZE);
1032         if (((u_int32_t)VTOP(bbuf) & 0xffff0000) == ((u_int32_t)VTOP(dest + x * BIOSDISK_SECSIZE) & 0xffff0000)) {
1033             breg = bbuf;
1034         } else {
1035             breg = bbuf + x * BIOSDISK_SECSIZE;
1036         }
1037         maxfer = x;                     /* limit transfers to bounce region size */
1038     } else {
1039         breg = bbuf = NULL;
1040         maxfer = 0;
1041     }
1042     
1043     while (resid > 0) {
1044         x = dblk;
1045         cyl = x / bpc;                  /* block # / blocks per cylinder */
1046         x %= bpc;                       /* block offset into cylinder */
1047         hd = x / od->od_sec;            /* offset / blocks per track */
1048         sec = x % od->od_sec;           /* offset into track */
1049
1050         /* play it safe and don't cross track boundaries (XXX this is probably unnecessary) */
1051         x = min(od->od_sec - sec, resid);
1052         if (maxfer > 0)
1053             x = min(x, maxfer);         /* fit bounce buffer */
1054
1055         /* where do we transfer to? */
1056         xp = bbuf == NULL ? p : breg;
1057
1058         /* correct sector number for 1-based BIOS numbering */
1059 #ifdef PC98
1060         if ((od->od_unit & 0xf0) == 0x30 || (od->od_unit & 0xf0) == 0x90)
1061             sec++;
1062 #else
1063         sec++;
1064 #endif
1065
1066         /* Loop retrying the operation a couple of times.  The BIOS may also retry. */
1067         for (retry = 0; retry < 3; retry++) {
1068             /* if retrying, reset the drive */
1069             if (retry > 0) {
1070 #ifdef PC98
1071                 v86.ctl = V86_FLAGS;
1072                 v86.addr = 0x1b;
1073                 v86.eax = 0x0300 | od->od_unit;
1074 #else
1075                 v86.ctl = V86_FLAGS;
1076                 v86.addr = 0x13;
1077                 v86.eax = 0;
1078                 v86.edx = od->od_unit;
1079 #endif
1080                 v86int();
1081             }
1082             
1083 #ifdef PC98
1084             v86.ctl = V86_FLAGS;
1085             v86.addr = 0x1b;
1086             if (od->od_flags & BD_FLOPPY) {
1087                 v86.eax = 0xd600 | od->od_unit;
1088                 v86.ecx = 0x0200 | (cyl & 0xff);
1089             }
1090             else {
1091                 v86.eax = 0x0600 | od->od_unit;
1092                 v86.ecx = cyl;
1093             }
1094             if (od->od_flags & BD_OPTICAL) {
1095                 v86.eax &= 0xFF7F;
1096                 v86.ecx = dblk & 0xFFFF;
1097                 v86.edx = dblk >> 16;
1098             } else {
1099                 v86.edx = (hd << 8) | sec;
1100             }
1101             v86.ebx = x * BIOSDISK_SECSIZE;
1102             v86.es = VTOPSEG(xp);
1103             v86.ebp = VTOPOFF(xp);
1104             v86int();
1105             result = (v86.efl & 0x1);
1106             if (result == 0)
1107                 break;
1108 #else
1109             if(cyl > 1023) {
1110                 /* use EDD if the disk supports it, otherwise, return error */
1111                 if(od->od_flags & BD_MODEEDD1) {
1112                     static unsigned short packet[8];
1113
1114                     packet[0] = 0x10;
1115                     packet[1] = x;
1116                     packet[2] = VTOPOFF(xp);
1117                     packet[3] = VTOPSEG(xp);
1118                     packet[4] = dblk & 0xffff;
1119                     packet[5] = dblk >> 16;
1120                     packet[6] = 0;
1121                     packet[7] = 0;
1122                     v86.ctl = V86_FLAGS;
1123                     v86.addr = 0x13;
1124                     v86.eax = 0x4200;
1125                     v86.edx = od->od_unit;
1126                     v86.ds = VTOPSEG(packet);
1127                     v86.esi = VTOPOFF(packet);
1128                     v86int();
1129                     result = (v86.efl & 0x1);
1130                     if(result == 0)
1131                       break;
1132                 } else {
1133                     result = 1;
1134                     break;
1135                 }
1136             } else {
1137                 /* Use normal CHS addressing */
1138                 v86.ctl = V86_FLAGS;
1139                 v86.addr = 0x13;
1140                 v86.eax = 0x200 | x;
1141                 v86.ecx = ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec;
1142                 v86.edx = (hd << 8) | od->od_unit;
1143                 v86.es = VTOPSEG(xp);
1144                 v86.ebx = VTOPOFF(xp);
1145                 v86int();
1146                 result = (v86.efl & 0x1);
1147                 if (result == 0)
1148                   break;
1149             }
1150 #endif
1151         }
1152         
1153 #ifdef PC98
1154         DEBUG("%d sectors from %d/%d/%d to %p (0x%x) %s", x, cyl, hd, od->od_flags & BD_FLOPPY ? sec - 1 : sec, p, VTOP(p), result ? "failed" : "ok");
1155         /* BUG here, cannot use v86 in printf because putchar uses it too */
1156         DEBUG("ax = 0x%04x cx = 0x%04x dx = 0x%04x status 0x%x", 
1157               od->od_flags & BD_FLOPPY ? 0xd600 | od->od_unit : 0x0600 | od->od_unit,
1158               od->od_flags & BD_FLOPPY ? 0x0200 | cyl : cyl, (hd << 8) | sec,
1159               (v86.eax >> 8) & 0xff);
1160 #else
1161         DEBUG("%d sectors from %d/%d/%d to %p (0x%x) %s", x, cyl, hd, sec - 1, p, VTOP(p), result ? "failed" : "ok");
1162         /* BUG here, cannot use v86 in printf because putchar uses it too */
1163         DEBUG("ax = 0x%04x cx = 0x%04x dx = 0x%04x status 0x%x", 
1164               0x200 | x, ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec, (hd << 8) | od->od_unit, (v86.eax >> 8) & 0xff);
1165 #endif
1166         if (result) {
1167             if (bbuf != NULL)
1168                 free(bbuf);
1169             return(-1);
1170         }
1171         if (bbuf != NULL)
1172             bcopy(breg, p, x * BIOSDISK_SECSIZE);
1173         p += (x * BIOSDISK_SECSIZE);
1174         dblk += x;
1175         resid -= x;
1176     }
1177         
1178 /*    hexdump(dest, (blks * BIOSDISK_SECSIZE)); */
1179     if (bbuf != NULL)
1180         free(bbuf);
1181     return(0);
1182 }
1183
1184
1185 static int
1186 bd_write(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest)
1187 {
1188     u_int       x, bpc, cyl, hd, sec, result, resid, retry, maxfer;
1189     caddr_t     p, xp, bbuf, breg;
1190     
1191     /* Just in case some idiot actually tries to read -1 blocks... */
1192     if (blks < 0)
1193         return (-1);
1194
1195     bpc = (od->od_sec * od->od_hds);            /* blocks per cylinder */
1196     resid = blks;
1197     p = dest;
1198
1199     /* Decide whether we have to bounce */
1200 #ifdef PC98
1201     if (
1202 #else
1203     if ((od->od_unit < 0x80) && 
1204 #endif
1205         ((VTOP(dest) >> 16) != (VTOP(dest + blks * BIOSDISK_SECSIZE) >> 16))) {
1206
1207         /* 
1208          * There is a 64k physical boundary somewhere in the destination buffer, so we have
1209          * to arrange a suitable bounce buffer.  Allocate a buffer twice as large as we
1210          * need to.  Use the bottom half unless there is a break there, in which case we
1211          * use the top half.
1212          */
1213
1214 #ifdef PC98
1215         x = min(od->od_sec, (unsigned)blks);
1216 #else
1217         x = min(FLOPPY_BOUNCEBUF, (unsigned)blks);
1218 #endif
1219         bbuf = malloc(x * 2 * BIOSDISK_SECSIZE);
1220         if (((u_int32_t)VTOP(bbuf) & 0xffff0000) == ((u_int32_t)VTOP(dest + x * BIOSDISK_SECSIZE) & 0xffff0000)) {
1221             breg = bbuf;
1222         } else {
1223             breg = bbuf + x * BIOSDISK_SECSIZE;
1224         }
1225         maxfer = x;                     /* limit transfers to bounce region size */
1226     } else {
1227         breg = bbuf = NULL;
1228         maxfer = 0;
1229     }
1230
1231     while (resid > 0) {
1232         x = dblk;
1233         cyl = x / bpc;                  /* block # / blocks per cylinder */
1234         x %= bpc;                       /* block offset into cylinder */
1235         hd = x / od->od_sec;            /* offset / blocks per track */
1236         sec = x % od->od_sec;           /* offset into track */
1237
1238         /* play it safe and don't cross track boundaries (XXX this is probably unnecessary) */
1239         x = min(od->od_sec - sec, resid);
1240         if (maxfer > 0)
1241             x = min(x, maxfer);         /* fit bounce buffer */
1242
1243         /* where do we transfer to? */
1244         xp = bbuf == NULL ? p : breg;
1245
1246         /* correct sector number for 1-based BIOS numbering */
1247 #ifdef PC98
1248         if ((od->od_unit & 0xf0) == 0x30 || (od->od_unit & 0xf0) == 0x90)
1249             sec++;
1250 #else
1251         sec++;
1252 #endif
1253
1254
1255         /* Put your Data In, Put your Data out,
1256            Put your Data In, and shake it all about 
1257         */
1258         if (bbuf != NULL)
1259             bcopy(p, breg, x * BIOSDISK_SECSIZE);
1260         p += (x * BIOSDISK_SECSIZE);
1261         dblk += x;
1262         resid -= x;
1263
1264         /* Loop retrying the operation a couple of times.  The BIOS may also retry. */
1265         for (retry = 0; retry < 3; retry++) {
1266             /* if retrying, reset the drive */
1267             if (retry > 0) {
1268 #ifdef PC98
1269                 v86.ctl = V86_FLAGS;
1270                 v86.addr = 0x1b;
1271                 v86.eax = 0x0300 | od->od_unit;
1272 #else
1273                 v86.ctl = V86_FLAGS;
1274                 v86.addr = 0x13;
1275                 v86.eax = 0;
1276                 v86.edx = od->od_unit;
1277 #endif
1278                 v86int();
1279             }
1280             
1281 #ifdef PC98
1282             v86.ctl = V86_FLAGS;
1283             v86.addr = 0x1b;
1284             if (od->od_flags & BD_FLOPPY) {
1285                 v86.eax = 0xd500 | od->od_unit;
1286                 v86.ecx = 0x0200 | (cyl & 0xff);
1287             } else {
1288                 v86.eax = 0x0500 | od->od_unit;
1289                 v86.ecx = cyl;
1290             }
1291             v86.edx = (hd << 8) | sec;
1292             v86.ebx = x * BIOSDISK_SECSIZE;
1293             v86.es = VTOPSEG(xp);
1294             v86.ebp = VTOPOFF(xp);
1295             v86int();
1296             result = (v86.efl & 0x1);
1297             if (result == 0)
1298                 break;
1299 #else
1300             if(cyl > 1023) {
1301                 /* use EDD if the disk supports it, otherwise, return error */
1302                 if(od->od_flags & BD_MODEEDD1) {
1303                     static unsigned short packet[8];
1304
1305                     packet[0] = 0x10;
1306                     packet[1] = x;
1307                     packet[2] = VTOPOFF(xp);
1308                     packet[3] = VTOPSEG(xp);
1309                     packet[4] = dblk & 0xffff;
1310                     packet[5] = dblk >> 16;
1311                     packet[6] = 0;
1312                     packet[7] = 0;
1313                     v86.ctl = V86_FLAGS;
1314                     v86.addr = 0x13;
1315                         /* Should we Write with verify ?? 0x4302 ? */
1316                     v86.eax = 0x4300;
1317                     v86.edx = od->od_unit;
1318                     v86.ds = VTOPSEG(packet);
1319                     v86.esi = VTOPOFF(packet);
1320                     v86int();
1321                     result = (v86.efl & 0x1);
1322                     if(result == 0)
1323                       break;
1324                 } else {
1325                     result = 1;
1326                     break;
1327                 }
1328             } else {
1329                 /* Use normal CHS addressing */
1330                 v86.ctl = V86_FLAGS;
1331                 v86.addr = 0x13;
1332                 v86.eax = 0x300 | x;
1333                 v86.ecx = ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec;
1334                 v86.edx = (hd << 8) | od->od_unit;
1335                 v86.es = VTOPSEG(xp);
1336                 v86.ebx = VTOPOFF(xp);
1337                 v86int();
1338                 result = (v86.efl & 0x1);
1339                 if (result == 0)
1340                   break;
1341             }
1342 #endif
1343         }
1344         
1345 #ifdef PC98
1346         DEBUG("%d sectors from %d/%d/%d to %p (0x%x) %s", x, cyl, hd,
1347             od->od_flags & BD_FLOPPY ? sec - 1 : sec, p, VTOP(p),
1348             result ? "failed" : "ok");
1349         /* BUG here, cannot use v86 in printf because putchar uses it too */
1350         DEBUG("ax = 0x%04x cx = 0x%04x dx = 0x%04x status 0x%x", 
1351             od->od_flags & BD_FLOPPY ? 0xd600 | od->od_unit : 0x0600 | od->od_unit,
1352             od->od_flags & BD_FLOPPY ? 0x0200 | cyl : cyl, (hd << 8) | sec,
1353             (v86.eax >> 8) & 0xff);
1354 #else
1355         DEBUG("%d sectors from %d/%d/%d to %p (0x%x) %s", x, cyl, hd, sec - 1, p, VTOP(p), result ? "failed" : "ok");
1356         /* BUG here, cannot use v86 in printf because putchar uses it too */
1357         DEBUG("ax = 0x%04x cx = 0x%04x dx = 0x%04x status 0x%x", 
1358               0x200 | x, ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec, (hd << 8) | od->od_unit, (v86.eax >> 8) & 0xff);
1359 #endif
1360         if (result) {
1361             if (bbuf != NULL)
1362                 free(bbuf);
1363             return(-1);
1364         }
1365     }
1366         
1367 /*    hexdump(dest, (blks * BIOSDISK_SECSIZE)); */
1368     if (bbuf != NULL)
1369         free(bbuf);
1370     return(0);
1371 }
1372 static int
1373 bd_getgeom(struct open_disk *od)
1374 {
1375
1376 #ifdef PC98
1377     if (od->od_flags & BD_FLOPPY) {
1378         od->od_cyl = 79;
1379         od->od_hds = 2;
1380         od->od_sec = (od->od_unit & 0xf0) == 0x30 ? 18 : 15;
1381     } else if (od->od_flags & BD_OPTICAL) {
1382         od->od_cyl = 0xFFFE;
1383         od->od_hds = 8;
1384         od->od_sec = 32;
1385     } else {
1386         v86.ctl = V86_FLAGS;
1387         v86.addr = 0x1b;
1388         v86.eax = 0x8400 | od->od_unit;
1389         v86int();
1390       
1391         od->od_cyl = v86.ecx;
1392         od->od_hds = (v86.edx >> 8) & 0xff;
1393         od->od_sec = v86.edx & 0xff;
1394         if (v86.efl & 0x1)
1395             return(1);
1396     }
1397 #else
1398     v86.ctl = V86_FLAGS;
1399     v86.addr = 0x13;
1400     v86.eax = 0x800;
1401     v86.edx = od->od_unit;
1402     v86int();
1403
1404     if ((v86.efl & 0x1) ||                              /* carry set */
1405         ((v86.edx & 0xff) <= (unsigned)(od->od_unit & 0x7f)))   /* unit # bad */
1406         return(1);
1407     
1408     /* convert max cyl # -> # of cylinders */
1409     od->od_cyl = ((v86.ecx & 0xc0) << 2) + ((v86.ecx & 0xff00) >> 8) + 1;
1410     /* convert max head # -> # of heads */
1411     od->od_hds = ((v86.edx & 0xff00) >> 8) + 1;
1412     od->od_sec = v86.ecx & 0x3f;
1413 #endif
1414
1415     DEBUG("unit 0x%x geometry %d/%d/%d", od->od_unit, od->od_cyl, od->od_hds, od->od_sec);
1416     return(0);
1417 }
1418
1419 /*
1420  * Return the BIOS geometry of a given "fixed drive" in a format
1421  * suitable for the legacy bootinfo structure.  Since the kernel is
1422  * expecting raw int 0x13/0x8 values for N_BIOS_GEOM drives, we
1423  * prefer to get the information directly, rather than rely on being
1424  * able to put it together from information already maintained for
1425  * different purposes and for a probably different number of drives.
1426  *
1427  * For valid drives, the geometry is expected in the format (31..0)
1428  * "000000cc cccccccc hhhhhhhh 00ssssss"; and invalid drives are
1429  * indicated by returning the geometry of a "1.2M" PC-format floppy
1430  * disk.  And, incidentally, what is returned is not the geometry as
1431  * such but the highest valid cylinder, head, and sector numbers.
1432  */
1433 u_int32_t
1434 bd_getbigeom(int bunit)
1435 {
1436
1437 #ifdef PC98
1438     int hds = 0;
1439     int unit = 0x80;            /* IDE HDD */
1440     u_int addr = 0xA155d;
1441
1442     while (unit < 0xa7) {
1443         if (*(u_char *)PTOV(addr) & (1 << (unit & 0x0f)))
1444             if (hds++ == bunit)
1445                 break;
1446
1447         if (unit >= 0xA0) {
1448             int  media = ((unsigned *)PTOV(0xA1460))[unit & 0x0F] & 0x1F;
1449
1450             if (media == 7 && hds++ == bunit)   /* SCSI MO */
1451                 return(0xFFFE0820); /* C:65535 H:8 S:32 */
1452         }
1453         if (++unit == 0x84) {
1454             unit = 0xA0;        /* SCSI HDD */
1455             addr = 0xA1482;
1456         }
1457     }
1458     if (unit == 0xa7)
1459         return 0x4F020F;        /* 1200KB FD C:80 H:2 S:15 */
1460     v86.ctl = V86_FLAGS;
1461     v86.addr = 0x1b;
1462     v86.eax = 0x8400 | unit;
1463     v86int();
1464     if (v86.efl & 0x1)
1465         return 0x4F020F;        /* 1200KB FD C:80 H:2 S:15 */
1466     return ((v86.ecx & 0xffff) << 16) | (v86.edx & 0xffff);
1467 #else
1468     v86.ctl = V86_FLAGS;
1469     v86.addr = 0x13;
1470     v86.eax = 0x800;
1471     v86.edx = 0x80 + bunit;
1472     v86int();
1473     if (v86.efl & 0x1)
1474         return 0x4f010f;
1475     return ((v86.ecx & 0xc0) << 18) | ((v86.ecx & 0xff00) << 8) |
1476            (v86.edx & 0xff00) | (v86.ecx & 0x3f);
1477 #endif
1478 }
1479
1480 /*
1481  * Return a suitable dev_t value for (dev).
1482  *
1483  * In the case where it looks like (dev) is a SCSI disk, we allow the number of
1484  * IDE disks to be specified in $num_ide_disks.  There should be a Better Way.
1485  */
1486 int
1487 bd_getdev(struct i386_devdesc *dev)
1488 {
1489     struct open_disk            *od;
1490     int                         biosdev;
1491     int                         major;
1492     int                         rootdev;
1493     char                        *nip, *cp;
1494     int                         unitofs = 0, i, unit;
1495
1496     biosdev = bd_unit2bios(dev->d_kind.biosdisk.unit);
1497     DEBUG("unit %d BIOS device %d", dev->d_kind.biosdisk.unit, biosdev);
1498     if (biosdev == -1)                          /* not a BIOS device */
1499         return(-1);
1500     if (bd_opendisk(&od, dev) != 0)             /* oops, not a viable device */
1501         return(-1);
1502
1503 #ifdef PC98
1504     if ((biosdev & 0xf0) == 0x90 || (biosdev & 0xf0) == 0x30) {
1505 #else
1506     if (biosdev < 0x80) {
1507 #endif
1508         /* floppy (or emulated floppy) or ATAPI device */
1509         if (bdinfo[dev->d_kind.biosdisk.unit].bd_type == DT_ATAPI) {
1510             /* is an ATAPI disk */
1511             major = WFDMAJOR;
1512         } else {
1513             /* is a floppy disk */
1514             major = FDMAJOR;
1515         }
1516     } else {
1517         /* harddisk */
1518         if ((od->od_flags & BD_LABELOK) && (od->od_disklabel.d_type == DTYPE_SCSI)) {
1519             /* label OK, disk labelled as SCSI */
1520             major = DAMAJOR;
1521             /* check for unit number correction hint, now deprecated */
1522             if ((nip = getenv("num_ide_disks")) != NULL) {
1523                 i = strtol(nip, &cp, 0);
1524                 /* check for parse error */
1525                 if ((cp != nip) && (*cp == 0))
1526                     unitofs = i;
1527             }
1528         } else {
1529             /* assume an IDE disk */
1530             major = WDMAJOR;
1531         }
1532     }
1533     /* default root disk unit number */
1534 #ifdef PC98
1535     if ((biosdev & 0xf0) == 0xa0)
1536         unit = bdinfo[dev->d_kind.biosdisk.unit].bd_da_unit;
1537     else
1538         unit = biosdev & 0xf;
1539 #else
1540     unit = (biosdev & 0x7f) - unitofs;
1541 #endif
1542
1543     /* XXX a better kludge to set the root disk unit number */
1544     if ((nip = getenv("root_disk_unit")) != NULL) {
1545         i = strtol(nip, &cp, 0);
1546         /* check for parse error */
1547         if ((cp != nip) && (*cp == 0))
1548             unit = i;
1549     }
1550
1551     rootdev = MAKEBOOTDEV(major,
1552                           (dev->d_kind.biosdisk.slice + 1) >> 4,        /* XXX slices may be wrong here */
1553                           (dev->d_kind.biosdisk.slice + 1) & 0xf, 
1554                           unit,
1555                           dev->d_kind.biosdisk.partition);
1556     DEBUG("dev is 0x%x\n", rootdev);
1557     return(rootdev);
1558 }