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