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