biosdisk.c revision 332154
143561Skato/*-
243561Skato * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
343561Skato * All rights reserved.
443561Skato *
543561Skato * Redistribution and use in source and binary forms, with or without
643561Skato * modification, are permitted provided that the following conditions
743561Skato * are met:
843561Skato * 1. Redistributions of source code must retain the above copyright
943561Skato *    notice, this list of conditions and the following disclaimer.
1043561Skato * 2. Redistributions in binary form must reproduce the above copyright
1143561Skato *    notice, this list of conditions and the following disclaimer in the
1243561Skato *    documentation and/or other materials provided with the distribution.
1343561Skato *
1443561Skato * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1543561Skato * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1643561Skato * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1743561Skato * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1843561Skato * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1943561Skato * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2043561Skato * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2143561Skato * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2243561Skato * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2343561Skato * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2443561Skato * SUCH DAMAGE.
2543561Skato */
2643561Skato
27119880Sobrien#include <sys/cdefs.h>
28119880Sobrien__FBSDID("$FreeBSD: stable/11/stand/pc98/libpc98/biosdisk.c 332154 2018-04-06 21:37:25Z kevans $");
29119880Sobrien
3043561Skato/*
3143561Skato * BIOS disk device handling.
3243561Skato *
3343561Skato * Ideas and algorithms from:
3443561Skato *
3543561Skato * - NetBSD libi386/biosdisk.c
3643561Skato * - FreeBSD biosboot/disk.c
3743561Skato *
3843561Skato */
3943561Skato
4043561Skato#include <stand.h>
4143561Skato
4243561Skato#include <sys/disklabel.h>
43104621Snyan#include <sys/diskpc98.h>
44113083Sphk#include <machine/bootinfo.h>
4543561Skato
4643561Skato#include <stdarg.h>
4743561Skato
4843561Skato#include <bootstrap.h>
4943561Skato#include <btxv86.h>
5043561Skato#include "libi386.h"
5143561Skato
5263101Snyan#define BIOS_NUMDRIVES		0x475
5343561Skato#define BIOSDISK_SECSIZE	512
5443561Skato#define BUFSIZE			(1 * BIOSDISK_SECSIZE)
5543561Skato
5643561Skato#define DT_ATAPI		0x10		/* disk type for ATAPI floppies */
5743561Skato#define WDMAJOR			0		/* major numbers for devices we frontend for */
5843561Skato#define WFDMAJOR		1
5943561Skato#define FDMAJOR			2
6043561Skato#define DAMAJOR			4
6143561Skato
6243561Skato#ifdef DISK_DEBUG
6387599Sobrien# define DEBUG(fmt, args...)	printf("%s: " fmt "\n" , __func__ , ## args)
6443561Skato#else
6543561Skato# define DEBUG(fmt, args...)
6643561Skato#endif
6743561Skato
6843561Skatostruct open_disk {
6943561Skato    int			od_dkunit;		/* disk unit number */
7043561Skato    int			od_unit;		/* BIOS unit number */
7143561Skato    int			od_cyl;			/* BIOS geometry */
7243561Skato    int			od_hds;
7343561Skato    int			od_sec;
7443561Skato    int			od_boff;		/* block offset from beginning of BIOS disk */
7543561Skato    int			od_flags;
7659777Snyan#define BD_MODEINT13		0x0000
7759777Snyan#define BD_MODEEDD1		0x0001
7859777Snyan#define BD_MODEEDD3		0x0002
7963101Snyan#define BD_MODEMASK		0x0003
8059777Snyan#define BD_FLOPPY		0x0004
8159777Snyan#define BD_LABELOK		0x0008
8259777Snyan#define BD_PARTTABOK		0x0010
83108791Snyan#define BD_OPTICAL		0x0020
8443561Skato    struct disklabel		od_disklabel;
8559777Snyan    int				od_nslices;	/* slice count */
86254015Smarcel    struct pc98_partition	od_slicetab[PC98_NPARTS];
8743561Skato};
8843561Skato
8943561Skato/*
9043561Skato * List of BIOS devices, translation from disk unit number to
9143561Skato * BIOS unit number.
9243561Skato */
9343561Skatostatic struct bdinfo
9443561Skato{
9543561Skato    int		bd_unit;		/* BIOS unit number */
9643561Skato    int		bd_flags;
9743561Skato    int		bd_type;		/* BIOS 'drive type' (floppy only) */
9853207Snyan    int		bd_da_unit;		/* kernel unit number for da */
99298230Sallanjude    int		bd_open;		/* reference counter */
100298230Sallanjude    void	*bd_bcache;		/* buffer cache data */
10143561Skato} bdinfo [MAXBDDEV];
10243561Skatostatic int nbdinfo = 0;
10343561Skato
104332154Skevans#define	BD(dev)	(bdinfo[(dev)->dd.d_unit])
105298230Sallanjude
10643561Skatostatic int	bd_getgeom(struct open_disk *od);
10759777Snyanstatic int	bd_read(struct open_disk *od, daddr_t dblk, int blks,
10859777Snyan		    caddr_t dest);
10987734Snyanstatic int	bd_write(struct open_disk *od, daddr_t dblk, int blks,
11087734Snyan		    caddr_t dest);
11143561Skato
11243561Skatostatic int	bd_int13probe(struct bdinfo *bd);
11343561Skato
114300117Simpstatic int	bd_printslice(struct open_disk *od, struct pc98_partition *dp,
11568358Snyan		    char *prefix, int verbose);
116300117Simpstatic int	bd_printbsdslice(struct open_disk *od, daddr_t offset,
11768358Snyan		    char *prefix, int verbose);
11843561Skato
11943561Skatostatic int	bd_init(void);
12059777Snyanstatic int	bd_strategy(void *devdata, int flag, daddr_t dblk,
121313355Stsoome		    size_t size, char *buf, size_t *rsize);
12259777Snyanstatic int	bd_realstrategy(void *devdata, int flag, daddr_t dblk,
123313355Stsoome		    size_t size, char *buf, size_t *rsize);
12443561Skatostatic int	bd_open(struct open_file *f, ...);
12543561Skatostatic int	bd_close(struct open_file *f);
126328889Skevansstatic int	bd_print(int verbose);
12743561Skato
12843561Skatostruct devsw biosdisk = {
12943561Skato    "disk",
13043561Skato    DEVT_DISK,
13143561Skato    bd_init,
13243561Skato    bd_strategy,
13343561Skato    bd_open,
13443561Skato    bd_close,
13543561Skato    noioctl,
13668358Snyan    bd_print,
13768358Snyan    NULL
13843561Skato};
13943561Skato
14043561Skatostatic int	bd_opendisk(struct open_disk **odp, struct i386_devdesc *dev);
14143561Skatostatic void	bd_closedisk(struct open_disk *od);
142172968Snyanstatic int	bd_open_pc98(struct open_disk *od, struct i386_devdesc *dev);
14359777Snyanstatic int	bd_bestslice(struct open_disk *od);
14459777Snyanstatic void	bd_checkextended(struct open_disk *od, int slicenum);
14543561Skato
14643561Skato/*
14743561Skato * Translate between BIOS device numbers and our private unit numbers.
14843561Skato */
14943561Skatoint
15043561Skatobd_bios2unit(int biosdev)
15143561Skato{
15243561Skato    int		i;
15343561Skato
15443561Skato    DEBUG("looking for bios device 0x%x", biosdev);
15543561Skato    for (i = 0; i < nbdinfo; i++) {
15643561Skato	DEBUG("bd unit %d is BIOS device 0x%x", i, bdinfo[i].bd_unit);
15743561Skato	if (bdinfo[i].bd_unit == biosdev)
15843561Skato	    return(i);
15943561Skato    }
16043561Skato    return(-1);
16143561Skato}
16243561Skato
16343561Skatoint
16443561Skatobd_unit2bios(int unit)
16543561Skato{
16643561Skato    if ((unit >= 0) && (unit < nbdinfo))
16743561Skato	return(bdinfo[unit].bd_unit);
16843561Skato    return(-1);
16943561Skato}
17043561Skato
17143561Skato/*
17243561Skato * Quiz the BIOS for disk devices, save a little info about them.
17343561Skato */
17443561Skatostatic int
17543561Skatobd_init(void)
17643561Skato{
17743561Skato    int		base, unit;
17863101Snyan    int		da_drive=0, n=-0x10;
17943561Skato
18043561Skato    /* sequence 0x90, 0x80, 0xa0 */
18143561Skato    for (base = 0x90; base <= 0xa0; base += n, n += 0x30) {
18243561Skato	for (unit = base; (nbdinfo < MAXBDDEV) || ((unit & 0x0f) < 4); unit++) {
183298230Sallanjude	    bdinfo[nbdinfo].bd_open = 0;
184298230Sallanjude	    bdinfo[nbdinfo].bd_bcache = NULL;
18543561Skato	    bdinfo[nbdinfo].bd_unit = unit;
18643561Skato	    bdinfo[nbdinfo].bd_flags = (unit & 0xf0) == 0x90 ? BD_FLOPPY : 0;
18743561Skato
18844463Skato	    if (!bd_int13probe(&bdinfo[nbdinfo])){
18949425Skato		if (((unit & 0xf0) == 0x90 && (unit & 0x0f) < 4) ||
19049425Skato		    ((unit & 0xf0) == 0xa0 && (unit & 0x0f) < 6))
19144463Skato		    continue;	/* Target IDs are not contiguous. */
19244463Skato		else
19344463Skato		    break;
19444463Skato	    }
19543561Skato
19643561Skato	    if (bdinfo[nbdinfo].bd_flags & BD_FLOPPY){
19744467Skato		/* available 1.44MB access? */
19859777Snyan		if (*(u_char *)PTOV(0xA15AE) & (1<<(unit & 0xf))) {
19944467Skato		    /* boot media 1.2MB FD? */
20044467Skato		    if ((*(u_char *)PTOV(0xA1584) & 0xf0) != 0x90)
20144467Skato		        bdinfo[nbdinfo].bd_unit = 0x30 + (unit & 0xf);
20244467Skato		}
20343561Skato	    }
20449426Skato	    else {
205108791Snyan		if ((unit & 0xF0) == 0xA0)	/* SCSI HD or MO */
20649426Skato		    bdinfo[nbdinfo].bd_da_unit = da_drive++;
20749426Skato	    }
20843561Skato	    /* XXX we need "disk aliases" to make this simpler */
20943561Skato	    printf("BIOS drive %c: is disk%d\n",
21049426Skato		   'A' + nbdinfo, nbdinfo);
21143561Skato	    nbdinfo++;
21243561Skato	}
21343561Skato    }
214298230Sallanjude    bcache_add_dev(nbdinfo);
21543561Skato    return(0);
21643561Skato}
21743561Skato
21843561Skato/*
21943561Skato * Try to detect a device supported by the legacy int13 BIOS
22043561Skato */
22143561Skatostatic int
22243561Skatobd_int13probe(struct bdinfo *bd)
22343561Skato{
22443561Skato    int addr;
22563101Snyan
22659777Snyan    if (bd->bd_flags & BD_FLOPPY) {
22743561Skato	addr = 0xa155c;
22859777Snyan    } else {
22943561Skato	if ((bd->bd_unit & 0xf0) == 0x80)
23043561Skato	    addr = 0xa155d;
23143561Skato	else
23243561Skato	    addr = 0xa1482;
23343561Skato    }
23443561Skato    if ( *(u_char *)PTOV(addr) & (1<<(bd->bd_unit & 0x0f))) {
23543561Skato	bd->bd_flags |= BD_MODEINT13;
23643561Skato	return(1);
23743561Skato    }
238108791Snyan    if ((bd->bd_unit & 0xF0) == 0xA0) {
239108791Snyan	int media = ((unsigned *)PTOV(0xA1460))[bd->bd_unit & 0x0F] & 0x1F;
240108791Snyan
241108791Snyan	if (media == 7) { /* MO */
242108791Snyan	    bd->bd_flags |= BD_MODEINT13 | BD_OPTICAL;
243108791Snyan	    return(1);
244108791Snyan	}
245108791Snyan    }
24643561Skato    return(0);
24743561Skato}
24843561Skato
24943561Skato/*
25043561Skato * Print information about disks
25143561Skato */
252328889Skevansstatic int
25343561Skatobd_print(int verbose)
25443561Skato{
255328889Skevans    int				i, j, ret = 0;
25643561Skato    char			line[80];
25743561Skato    struct i386_devdesc		dev;
25843561Skato    struct open_disk		*od;
259108650Snyan    struct pc98_partition	*dptr;
26043561Skato
261328889Skevans    if (nbdinfo == 0)
262328889Skevans	return (0);
26343561Skato
264328889Skevans    printf("%s devices:", biosdisk.dv_name);
265328889Skevans    if ((ret = pager_output("\n")) != 0)
266328889Skevans	return (ret);
267328889Skevans
268328889Skevans    for (i = 0; i < nbdinfo; i++) {
269328889Skevans	snprintf(line, sizeof(line), "    disk%d:   BIOS drive %c:\n",
270328889Skevans	    i, 'A' + i);
271328889Skevans	if ((ret = pager_output(line)) != 0)
272328889Skevans	    break;
273328889Skevans
27443561Skato	/* try to open the whole disk */
275332154Skevans	dev.dd.d_unit = i;
27643561Skato	dev.d_kind.biosdisk.slice = -1;
27743561Skato	dev.d_kind.biosdisk.partition = -1;
27843561Skato
27943561Skato	if (!bd_opendisk(&od, &dev)) {
28043561Skato
28143561Skato	    /* Do we have a partition table? */
28243561Skato	    if (od->od_flags & BD_PARTTABOK) {
28359777Snyan		dptr = &od->od_slicetab[0];
28443561Skato
28568358Snyan		/* Check for a "dedicated" disk */
28659777Snyan		for (j = 0; j < od->od_nslices; j++) {
287328889Skevans		    snprintf(line, sizeof(line), "      disk%ds%d", i, j + 1);
288328889Skevans		    if ((ret = bd_printslice(od, &dptr[j], line, verbose)) != 0)
289328889Skevans			break;
29043561Skato		}
29143561Skato	    }
29243561Skato	    bd_closedisk(od);
293328889Skevans	    if (ret != 0)
294328889Skevans		break;
29543561Skato	}
29643561Skato    }
297328889Skevans    return (ret);
29843561Skato}
29943561Skato
300176654Snyan/* Given a size in 512 byte sectors, convert it to a human-readable number. */
301176654Snyanstatic char *
302176654Snyandisplay_size(uint64_t size)
303176654Snyan{
304176654Snyan    static char buf[80];
305176654Snyan    char unit;
306176654Snyan
307176654Snyan    size /= 2;
308176654Snyan    unit = 'K';
309176654Snyan    if (size >= 10485760000LL) {
310176654Snyan	size /= 1073741824;
311176654Snyan	unit = 'T';
312176654Snyan    } else if (size >= 10240000) {
313176654Snyan	size /= 1048576;
314176654Snyan	unit = 'G';
315176654Snyan    } else if (size >= 10000) {
316176654Snyan	size /= 1024;
317176654Snyan	unit = 'M';
318176654Snyan    }
319250333Sjhb    sprintf(buf, "%6ld%cB", (long)size, unit);
320176654Snyan    return (buf);
321176654Snyan}
322176654Snyan
32359777Snyan/*
324190127Snyan * Print information about slices on a disk.  For the size calculations we
325190127Snyan * assume a 512 byte sector.
326190127Snyan */
327300117Simpstatic int
328190127Snyanbd_printslice(struct open_disk *od, struct pc98_partition *dp, char *prefix,
329190127Snyan	int verbose)
330190127Snyan{
331190127Snyan	int cylsecs, start, size;
332190127Snyan	char stats[80];
333190127Snyan	char line[80];
334190127Snyan
335190127Snyan	cylsecs = od->od_hds * od->od_sec;
336190127Snyan	start = dp->dp_scyl * cylsecs + dp->dp_shd * od->od_sec + dp->dp_ssect;
337190127Snyan	size = (dp->dp_ecyl - dp->dp_scyl + 1) * cylsecs;
338190127Snyan
339190127Snyan	if (verbose)
340190127Snyan		sprintf(stats, " %s (%d - %d)", display_size(size),
341190127Snyan		    start, start + size);
342190127Snyan	else
343190127Snyan		stats[0] = '\0';
344190127Snyan
345190127Snyan	switch(dp->dp_mid & PC98_MID_MASK) {
346190127Snyan	case PC98_MID_386BSD:
347300117Simp		return (bd_printbsdslice(od, start, prefix, verbose));
348190127Snyan	case 0x00:				/* unused partition */
349300117Simp		return (0);
350190127Snyan	case 0x01:
351190127Snyan		sprintf(line, "%s: FAT-12%s\n", prefix, stats);
352190127Snyan		break;
353190127Snyan	case 0x11:
354190127Snyan	case 0x20:
355190127Snyan	case 0x21:
356190127Snyan	case 0x22:
357190127Snyan	case 0x23:
358190127Snyan	case 0x24:
359190127Snyan		sprintf(line, "%s: FAT-16%s\n", prefix, stats);
360190127Snyan		break;
361190127Snyan	default:
362190127Snyan		sprintf(line, "%s: Unknown fs: 0x%x %s\n", prefix, dp->dp_mid,
363190127Snyan		    stats);
364190127Snyan	}
365300117Simp	return (pager_output(line));
366190127Snyan}
367190127Snyan
368190127Snyan/*
36968358Snyan * Print out each valid partition in the disklabel of a FreeBSD slice.
37068358Snyan * For size calculations, we assume a 512 byte sector size.
37168358Snyan */
372300117Simpstatic int
37368358Snyanbd_printbsdslice(struct open_disk *od, daddr_t offset, char *prefix,
37468358Snyan    int verbose)
37559777Snyan{
37643561Skato    char		line[80];
37768358Snyan    char		buf[BIOSDISK_SECSIZE];
37843561Skato    struct disklabel	*lp;
37943561Skato    int			i;
38043561Skato
38143561Skato    /* read disklabel */
38243561Skato    if (bd_read(od, offset + LABELSECTOR, 1, buf))
383300117Simp        return (0);
38443561Skato    lp =(struct disklabel *)(&buf[0]);
38543561Skato    if (lp->d_magic != DISKMAGIC) {
38663101Snyan	sprintf(line, "%s: FFS  bad disklabel\n", prefix);
387300117Simp	return (pager_output(line));
38843561Skato    }
38943561Skato
39043561Skato    /* Print partitions */
39143561Skato    for (i = 0; i < lp->d_npartitions; i++) {
39268358Snyan	/*
39368358Snyan	 * For each partition, make sure we know what type of fs it is.  If
39468358Snyan	 * not, then skip it.  However, since floppies often have bogus
39568358Snyan	 * fstypes, print the 'a' partition on a floppy even if it is marked
39668358Snyan	 * unused.
39768358Snyan	 */
39863101Snyan	if ((lp->d_partitions[i].p_fstype == FS_BSDFFS) ||
39963101Snyan            (lp->d_partitions[i].p_fstype == FS_SWAP) ||
40063101Snyan            (lp->d_partitions[i].p_fstype == FS_VINUM) ||
40143561Skato	    ((lp->d_partitions[i].p_fstype == FS_UNUSED) &&
40268358Snyan	     (od->od_flags & BD_FLOPPY) && (i == 0))) {
40368358Snyan
40468358Snyan	    /* Only print out statistics in verbose mode */
40568358Snyan	    if (verbose)
406190127Snyan	        sprintf(line, "  %s%c: %s %s (%d - %d)\n", prefix, 'a' + i,
407176654Snyan		    (lp->d_partitions[i].p_fstype == FS_SWAP) ? "swap " :
40868358Snyan		    (lp->d_partitions[i].p_fstype == FS_VINUM) ? "vinum" :
409176654Snyan		    "FFS  ",
410176654Snyan		    display_size(lp->d_partitions[i].p_size),
41168358Snyan		    lp->d_partitions[i].p_offset,
41268358Snyan		    lp->d_partitions[i].p_offset + lp->d_partitions[i].p_size);
41368358Snyan	    else
41468358Snyan	        sprintf(line, "  %s%c: %s\n", prefix, 'a' + i,
41568358Snyan		    (lp->d_partitions[i].p_fstype == FS_SWAP) ? "swap" :
41668358Snyan		    (lp->d_partitions[i].p_fstype == FS_VINUM) ? "vinum" :
41768358Snyan		    "FFS");
418300117Simp	    if (pager_output(line))
419300117Simp		    return (1);
42043561Skato	}
42143561Skato    }
422300117Simp    return (0);
42343561Skato}
42443561Skato
42543561Skato
42643561Skato/*
42743561Skato * Attempt to open the disk described by (dev) for use by (f).
42843561Skato *
42943561Skato * Note that the philosophy here is "give them exactly what
43043561Skato * they ask for".  This is necessary because being too "smart"
43143561Skato * about what the user might want leads to complications.
43243561Skato * (eg. given no slice or partition value, with a disk that is
43343561Skato *  sliced - are they after the first BSD slice, or the DOS
43443561Skato *  slice before it?)
43543561Skato */
43643561Skatostatic int
43743561Skatobd_open(struct open_file *f, ...)
43843561Skato{
43943561Skato    va_list			ap;
44043561Skato    struct i386_devdesc		*dev;
44143561Skato    struct open_disk		*od;
44243561Skato    int				error;
44343561Skato
44443561Skato    va_start(ap, f);
44543561Skato    dev = va_arg(ap, struct i386_devdesc *);
44643561Skato    va_end(ap);
44743561Skato    if ((error = bd_opendisk(&od, dev)))
44843561Skato	return(error);
44943561Skato
450298230Sallanjude    BD(dev).bd_open++;
451298230Sallanjude    if (BD(dev).bd_bcache == NULL)
452298230Sallanjude	BD(dev).bd_bcache = bcache_allocate();
453298230Sallanjude
45443561Skato    /*
45543561Skato     * Save our context
45643561Skato     */
45743561Skato    ((struct i386_devdesc *)(f->f_devdata))->d_kind.biosdisk.data = od;
45843561Skato    DEBUG("open_disk %p, partition at 0x%x", od, od->od_boff);
45943561Skato    return(0);
46043561Skato}
46143561Skato
46243561Skatostatic int
46343561Skatobd_opendisk(struct open_disk **odp, struct i386_devdesc *dev)
46443561Skato{
46543561Skato    struct open_disk		*od;
46643561Skato    int				error;
46743561Skato
468332154Skevans    if (dev->dd.d_unit >= nbdinfo) {
46943561Skato	DEBUG("attempt to open nonexistent disk");
47043561Skato	return(ENXIO);
47143561Skato    }
47243561Skato
47343561Skato    od = (struct open_disk *)malloc(sizeof(struct open_disk));
47443561Skato    if (!od) {
47543561Skato	DEBUG("no memory");
47643561Skato	return (ENOMEM);
47743561Skato    }
47843561Skato
47943561Skato    /* Look up BIOS unit number, intialise open_disk structure */
480332154Skevans    od->od_dkunit = dev->dd.d_unit;
48143561Skato    od->od_unit = bdinfo[od->od_dkunit].bd_unit;
48243561Skato    od->od_flags = bdinfo[od->od_dkunit].bd_flags;
48343561Skato    od->od_boff = 0;
48443561Skato    error = 0;
485172968Snyan    DEBUG("open '%s', unit 0x%x slice %d partition %d",
486332154Skevans	     i386_fmtdev(dev), dev->dd.d_unit,
487172968Snyan	     dev->d_kind.biosdisk.slice, dev->d_kind.biosdisk.partition);
48843561Skato
48943561Skato    /* Get geometry for this open (removable device may have changed) */
49043561Skato    if (bd_getgeom(od)) {
49143561Skato	DEBUG("can't get geometry");
49243561Skato	error = ENXIO;
49343561Skato	goto out;
49443561Skato    }
49543561Skato
496172968Snyan    /* Determine disk layout. */
497172968Snyan    error = bd_open_pc98(od, dev);
498172968Snyan
499172968Snyan out:
500172968Snyan    if (error) {
501172968Snyan	free(od);
502172968Snyan    } else {
503172968Snyan	*odp = od;	/* return the open disk */
504172968Snyan    }
505172968Snyan    return(error);
506172968Snyan}
507172968Snyan
508172968Snyanstatic int
509172968Snyanbd_open_pc98(struct open_disk *od, struct i386_devdesc *dev)
510172968Snyan{
511172968Snyan    struct pc98_partition	*dptr;
512172968Snyan    struct disklabel		*lp;
513172968Snyan    int				sector, slice, i;
514172968Snyan    char			buf[BUFSIZE];
515172968Snyan
51643561Skato    /*
51743561Skato     * Following calculations attempt to determine the correct value
51843561Skato     * for d->od_boff by looking for the slice and partition specified,
51943561Skato     * or searching for reasonable defaults.
52043561Skato     */
52143561Skato
52243561Skato    /*
52343561Skato     * Find the slice in the DOS slice table.
52443561Skato     */
525172968Snyan    od->od_nslices = 0;
52643561Skato    if (od->od_flags & BD_FLOPPY) {
52743561Skato	sector = 0;
52843561Skato	goto unsliced;
52943561Skato    }
53043561Skato    if (bd_read(od, 0, 1, buf)) {
53143561Skato	DEBUG("error reading MBR");
532172968Snyan	return (EIO);
53343561Skato    }
53443561Skato
53543561Skato    /*
53643561Skato     * Check the slice table magic.
53743561Skato     */
53868358Snyan    if (((u_char)buf[0x1fe] != 0x55) || ((u_char)buf[0x1ff] != 0xaa)) {
53943561Skato	/* If a slice number was explicitly supplied, this is an error */
54043561Skato	if (dev->d_kind.biosdisk.slice > 0) {
54143561Skato	    DEBUG("no slice table/MBR (no magic)");
542172968Snyan	    return (ENOENT);
54343561Skato	}
54443561Skato	sector = 0;
54543561Skato	goto unsliced;		/* may be a floppy */
54643561Skato    }
54743561Skato    if (bd_read(od, 1, 1, buf)) {
54843561Skato	DEBUG("error reading MBR");
549172968Snyan	return (EIO);
55043561Skato    }
55159777Snyan
55259777Snyan    /*
55359777Snyan     * copy the partition table, then pick up any extended partitions.
55459777Snyan     */
555254015Smarcel    bcopy(buf + PC98_PARTOFF, &od->od_slicetab,
556254015Smarcel      sizeof(struct pc98_partition) * PC98_NPARTS);
557254015Smarcel    od->od_nslices = PC98_NPARTS;	/* extended slices start here */
55843561Skato    od->od_flags |= BD_PARTTABOK;
55959777Snyan    dptr = &od->od_slicetab[0];
56043561Skato
56143561Skato    /* Is this a request for the whole disk? */
56243561Skato    if (dev->d_kind.biosdisk.slice == -1) {
56343561Skato	sector = 0;
56443561Skato	goto unsliced;
56543561Skato    }
56643561Skato
56759777Snyan    /*
56859777Snyan     * if a slice number was supplied but not found, this is an error.
56959777Snyan     */
57059777Snyan    if (dev->d_kind.biosdisk.slice > 0) {
57159777Snyan        slice = dev->d_kind.biosdisk.slice - 1;
57259777Snyan        if (slice >= od->od_nslices) {
57359777Snyan            DEBUG("slice %d not found", slice);
574172968Snyan	    return (ENOENT);
57559777Snyan        }
57659777Snyan    }
57743561Skato
57859777Snyan    /* Try to auto-detect the best slice; this should always give a slice number */
57959777Snyan    if (dev->d_kind.biosdisk.slice == 0) {
58059777Snyan	slice = bd_bestslice(od);
58159777Snyan        if (slice == -1) {
582172968Snyan	    return (ENOENT);
58359777Snyan        }
58459777Snyan        dev->d_kind.biosdisk.slice = slice;
58559777Snyan    }
58659777Snyan
58759777Snyan    dptr = &od->od_slicetab[0];
58843561Skato    /*
58943561Skato     * Accept the supplied slice number unequivocally (we may be looking
59043561Skato     * at a DOS partition).
59143561Skato     */
59243561Skato    dptr += (dev->d_kind.biosdisk.slice - 1);	/* we number 1-4, offsets are 0-3 */
59359777Snyan    sector = dptr->dp_scyl * od->od_hds * od->od_sec +
59459777Snyan	dptr->dp_shd * od->od_sec + dptr->dp_ssect;
59543561Skato    {
59659777Snyan	int end = dptr->dp_ecyl * od->od_hds * od->od_sec +
59759777Snyan	    dptr->dp_ehd * od->od_sec + dptr->dp_esect;
59859777Snyan	DEBUG("slice entry %d at %d, %d sectors",
59959777Snyan	      dev->d_kind.biosdisk.slice - 1, sector, end-sector);
60043561Skato    }
60143561Skato
60243561Skato    /*
60343561Skato     * If we are looking at a BSD slice, and the partition is < 0, assume the 'a' partition
60443561Skato     */
60543561Skato    if ((dptr->dp_mid == DOSMID_386BSD) && (dev->d_kind.biosdisk.partition < 0))
60643561Skato	dev->d_kind.biosdisk.partition = 0;
60743561Skato
60843561Skato unsliced:
60943561Skato    /*
61043561Skato     * Now we have the slice offset, look for the partition in the disklabel if we have
61143561Skato     * a partition to start with.
61243561Skato     *
61343561Skato     * XXX we might want to check the label checksum.
61443561Skato     */
61543561Skato    if (dev->d_kind.biosdisk.partition < 0) {
61643561Skato	od->od_boff = sector;		/* no partition, must be after the slice */
61743561Skato	DEBUG("opening raw slice");
61843561Skato    } else {
61953207Snyan
62043561Skato	if (bd_read(od, sector + LABELSECTOR, 1, buf)) {
62143561Skato	    DEBUG("error reading disklabel");
622172968Snyan	    return (EIO);
62343561Skato	}
62443561Skato	DEBUG("copy %d bytes of label from %p to %p", sizeof(struct disklabel), buf + LABELOFFSET, &od->od_disklabel);
62543561Skato	bcopy(buf + LABELOFFSET, &od->od_disklabel, sizeof(struct disklabel));
62643561Skato	lp = &od->od_disklabel;
62743561Skato	od->od_flags |= BD_LABELOK;
62843561Skato
62943561Skato	if (lp->d_magic != DISKMAGIC) {
63043561Skato	    DEBUG("no disklabel");
631172968Snyan	    return (ENOENT);
63243561Skato	}
63343561Skato	if (dev->d_kind.biosdisk.partition >= lp->d_npartitions) {
63443561Skato	    DEBUG("partition '%c' exceeds partitions in table (a-'%c')",
63543561Skato		  'a' + dev->d_kind.biosdisk.partition, 'a' + lp->d_npartitions);
636172968Snyan	    return (EPART);
63743561Skato	}
63843561Skato
63968358Snyan#ifdef DISK_DEBUG
64068358Snyan	/* Complain if the partition is unused unless this is a floppy. */
64143561Skato	if ((lp->d_partitions[dev->d_kind.biosdisk.partition].p_fstype == FS_UNUSED) &&
64268358Snyan	    !(od->od_flags & BD_FLOPPY))
64343561Skato	    DEBUG("warning, partition marked as unused");
64468358Snyan#endif
64543561Skato
646146010Snyan	od->od_boff =
647146010Snyan		lp->d_partitions[dev->d_kind.biosdisk.partition].p_offset -
648146010Snyan		lp->d_partitions[RAW_PART].p_offset +
649146010Snyan		sector;
65043561Skato    }
651172968Snyan    return (0);
65243561Skato}
65343561Skato
65443561Skato/*
65543561Skato * Search for a slice with the following preferences:
65643561Skato *
65743561Skato * 1: Active FreeBSD slice
65843561Skato * 2: Non-active FreeBSD slice
65959777Snyan * 3: Active Linux slice
66059777Snyan * 4: non-active Linux slice
66159777Snyan * 5: Active FAT/FAT32 slice
66259777Snyan * 6: non-active FAT/FAT32 slice
66343561Skato */
66459777Snyan#define PREF_RAWDISK	0
66559777Snyan#define PREF_FBSD_ACT	1
66659777Snyan#define PREF_FBSD	2
66759777Snyan#define PREF_LINUX_ACT	3
66859777Snyan#define PREF_LINUX	4
66959777Snyan#define PREF_DOS_ACT	5
67059777Snyan#define PREF_DOS	6
67159777Snyan#define PREF_NONE	7
67243561Skato
67359777Snyan/*
674254015Smarcel * slicelimit is in the range 0 .. PC98_NPARTS
67559777Snyan */
67643561Skatostatic int
67759777Snyanbd_bestslice(struct open_disk *od)
67843561Skato{
679108650Snyan	struct pc98_partition *dp;
68059777Snyan	int pref, preflevel;
68159777Snyan	int i, prefslice;
68253207Snyan
68359777Snyan	prefslice = 0;
68459777Snyan	preflevel = PREF_NONE;
68543561Skato
68659777Snyan	dp = &od->od_slicetab[0];
68759777Snyan	for (i = 0; i < od->od_nslices; i++, dp++) {
688190029Snyan		switch(dp->dp_mid & PC98_MID_MASK) {
689190029Snyan		case PC98_MID_386BSD:		/* FreeBSD */
690190029Snyan			if ((dp->dp_mid & PC98_MID_BOOTABLE) &&
69159777Snyan			    (preflevel > PREF_FBSD_ACT)) {
69259777Snyan				pref = i;
69359777Snyan				preflevel = PREF_FBSD_ACT;
69459777Snyan			} else if (preflevel > PREF_FBSD) {
69559777Snyan				pref = i;
69659777Snyan				preflevel = PREF_FBSD;
69759777Snyan			}
69859777Snyan			break;
69959777Snyan
70059777Snyan		case 0x11:				/* DOS/Windows */
70159777Snyan		case 0x20:
70259777Snyan		case 0x21:
70359777Snyan		case 0x22:
70459777Snyan		case 0x23:
70559777Snyan		case 0x63:
706190029Snyan			if ((dp->dp_mid & PC98_MID_BOOTABLE) &&
70759777Snyan			    (preflevel > PREF_DOS_ACT)) {
70859777Snyan				pref = i;
70959777Snyan				preflevel = PREF_DOS_ACT;
71059777Snyan			} else if (preflevel > PREF_DOS) {
71159777Snyan				pref = i;
71259777Snyan				preflevel = PREF_DOS;
71359777Snyan			}
71459777Snyan			break;
71559777Snyan		}
71643561Skato	}
71759777Snyan	return (prefslice);
71843561Skato}
71987734Snyan
72043561Skatostatic int
72143561Skatobd_close(struct open_file *f)
72243561Skato{
723298230Sallanjude    struct i386_devdesc		*dev = f->f_devdata;
724298230Sallanjude    struct open_disk	*od = (struct open_disk *)(dev->d_kind.biosdisk.data);
72543561Skato
726298230Sallanjude    BD(dev).bd_open--;
727298230Sallanjude    if (BD(dev).bd_open == 0) {
728298230Sallanjude	bcache_free(BD(dev).bd_bcache);
729298230Sallanjude	BD(dev).bd_bcache = NULL;
730298230Sallanjude    }
731298230Sallanjude
73243561Skato    bd_closedisk(od);
73343561Skato    return(0);
73443561Skato}
73543561Skato
73643561Skatostatic void
73743561Skatobd_closedisk(struct open_disk *od)
73843561Skato{
73943561Skato    DEBUG("open_disk %p", od);
74043561Skato#if 0
74143561Skato    /* XXX is this required? (especially if disk already open...) */
74243561Skato    if (od->od_flags & BD_FLOPPY)
74343561Skato	delay(3000000);
74443561Skato#endif
74543561Skato    free(od);
74643561Skato}
74743561Skato
74843561Skatostatic int
749313355Stsoomebd_strategy(void *devdata, int rw, daddr_t dblk, size_t size,
750298230Sallanjude    char *buf, size_t *rsize)
75143561Skato{
75243561Skato    struct bcache_devdata	bcd;
753298478Sallanjude    struct i386_devdesc		*dev = devdata;
754298230Sallanjude    struct open_disk	*od = (struct open_disk *)(dev->d_kind.biosdisk.data);
75558165Snyan
75643561Skato    bcd.dv_strategy = bd_realstrategy;
75743561Skato    bcd.dv_devdata = devdata;
758298230Sallanjude    bcd.dv_cache = BD(dev).bd_bcache;
759313355Stsoome    return(bcache_strategy(&bcd, rw, dblk+od->od_boff, size, buf, rsize));
76043561Skato}
76143561Skato
76243561Skatostatic int
763313355Stsoomebd_realstrategy(void *devdata, int rw, daddr_t dblk,
764298230Sallanjude    size_t size, char *buf, size_t *rsize)
76543561Skato{
76643561Skato    struct open_disk	*od = (struct open_disk *)(((struct i386_devdesc *)devdata)->d_kind.biosdisk.data);
76743561Skato    int			blks;
76843561Skato#ifdef BD_SUPPORT_FRAGS
76943561Skato    char		fragbuf[BIOSDISK_SECSIZE];
77043561Skato    size_t		fragsize;
77143561Skato
77243561Skato    fragsize = size % BIOSDISK_SECSIZE;
77343561Skato#else
77443561Skato    if (size % BIOSDISK_SECSIZE)
77543561Skato	panic("bd_strategy: %d bytes I/O not multiple of block size", size);
77643561Skato#endif
77743561Skato
77843561Skato    DEBUG("open_disk %p", od);
77943561Skato    blks = size / BIOSDISK_SECSIZE;
78043561Skato    if (rsize)
78143561Skato	*rsize = 0;
782172925Snyan
783172925Snyan    switch(rw){
784172925Snyan    case F_READ:
785172925Snyan	DEBUG("read %d from %d to %p", blks, dblk, buf);
786172925Snyan
787172925Snyan	if (blks && bd_read(od, dblk, blks, buf)) {
788172925Snyan	    DEBUG("read error");
789172925Snyan	    return (EIO);
790172925Snyan	}
79143561Skato#ifdef BD_SUPPORT_FRAGS
792172925Snyan	DEBUG("bd_strategy: frag read %d from %d+%d to %p",
793172925Snyan	    fragsize, dblk, blks, buf + (blks * BIOSDISK_SECSIZE));
794172925Snyan	if (fragsize && bd_read(od, dblk + blks, 1, fragsize)) {
795172925Snyan	    DEBUG("frag read error");
796172925Snyan	    return(EIO);
797172925Snyan	}
798172925Snyan	bcopy(fragbuf, buf + (blks * BIOSDISK_SECSIZE), fragsize);
79943561Skato#endif
800172925Snyan	break;
801172925Snyan    case F_WRITE :
802172925Snyan	DEBUG("write %d from %d to %p", blks, dblk, buf);
80387734Snyan
804172925Snyan	if (blks && bd_write(od, dblk, blks, buf)) {
805172925Snyan	    DEBUG("write error");
806172925Snyan	    return (EIO);
807172925Snyan	}
80887734Snyan#ifdef BD_SUPPORT_FRAGS
80987734Snyan	if(fragsize) {
810172925Snyan	    DEBUG("Attempted to write a frag");
811172925Snyan	    return (EIO);
81287734Snyan	}
81387734Snyan#endif
814172925Snyan	break;
815172925Snyan    default:
816172925Snyan	/* DO NOTHING */
817172925Snyan	return (EROFS);
818172925Snyan    }
81987734Snyan
82087734Snyan    if (rsize)
82187734Snyan	*rsize = size;
82287734Snyan    return (0);
82343561Skato}
82443561Skato
82543561Skato/* Max number of sectors to bounce-buffer if the request crosses a 64k boundary */
82643561Skato#define FLOPPY_BOUNCEBUF	18
82743561Skato
82843561Skatostatic int
829172965Snyanbd_chs_io(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest, int write)
83043561Skato{
831172965Snyan    u_int	x, bpc, cyl, hd, sec;
832172965Snyan
833172965Snyan    bpc = (od->od_sec * od->od_hds);	/* blocks per cylinder */
834172965Snyan    x = dblk;
835172965Snyan    cyl = x / bpc;			/* block # / blocks per cylinder */
836172965Snyan    x %= bpc;				/* block offset into cylinder */
837172965Snyan    hd = x / od->od_sec;		/* offset / blocks per track */
838172965Snyan    sec = x % od->od_sec;		/* offset into track */
839172965Snyan
840172965Snyan    v86.ctl = V86_FLAGS;
841172965Snyan    v86.addr = 0x1b;
842172965Snyan    if (write)
843172965Snyan	v86.eax = 0x0500 | od->od_unit;
844172965Snyan    else
845172965Snyan	v86.eax = 0x0600 | od->od_unit;
846172965Snyan    if (od->od_flags & BD_FLOPPY) {
847172965Snyan	v86.eax |= 0xd000;
848172965Snyan	v86.ecx = 0x0200 | (cyl & 0xff);
849172965Snyan	v86.edx = (hd << 8) | (sec + 1);
850172965Snyan    } else if (od->od_flags & BD_OPTICAL) {
851172965Snyan	v86.eax &= 0xFF7F;
852172965Snyan	v86.ecx = dblk & 0xFFFF;
853172965Snyan	v86.edx = dblk >> 16;
854172965Snyan    } else {
855172965Snyan	v86.ecx = cyl;
856172965Snyan	v86.edx = (hd << 8) | sec;
857172965Snyan    }
858172965Snyan    v86.ebx = blks * BIOSDISK_SECSIZE;
859172965Snyan    v86.es = VTOPSEG(dest);
860172965Snyan    v86.ebp = VTOPOFF(dest);
861172965Snyan    v86int();
862292682Sjhb    return (V86_CY(v86.efl));
863172965Snyan}
864172965Snyan
865172965Snyanstatic int
866172965Snyanbd_io(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest, int write)
867172965Snyan{
868172965Snyan    u_int	x, sec, result, resid, retry, maxfer;
86943561Skato    caddr_t	p, xp, bbuf, breg;
87043561Skato
871172965Snyan    /* Just in case some idiot actually tries to read/write -1 blocks... */
87268358Snyan    if (blks < 0)
87368358Snyan	return (-1);
87468358Snyan
87543561Skato    resid = blks;
87643561Skato    p = dest;
87743561Skato
87843561Skato    /* Decide whether we have to bounce */
879153598Snyan    if (VTOP(dest) >> 20 != 0 ||
880153598Snyan	((VTOP(dest) >> 16) != (VTOP(dest + blks * BIOSDISK_SECSIZE) >> 16))) {
88143561Skato
88243561Skato	/*
883172965Snyan	 * There is a 64k physical boundary somewhere in the
884172965Snyan	 * destination buffer, or the destination buffer is above
885172965Snyan	 * first 1MB of physical memory so we have to arrange a
886172965Snyan	 * suitable bounce buffer.  Allocate a buffer twice as large
887172965Snyan	 * as we need to.  Use the bottom half unless there is a break
888172965Snyan	 * there, in which case we use the top half.
88943561Skato	 */
89068358Snyan	x = min(od->od_sec, (unsigned)blks);
891153598Snyan	bbuf = alloca(x * 2 * BIOSDISK_SECSIZE);
892172965Snyan	if (((u_int32_t)VTOP(bbuf) & 0xffff0000) ==
893172965Snyan	    ((u_int32_t)VTOP(bbuf + x * BIOSDISK_SECSIZE) & 0xffff0000)) {
89443561Skato	    breg = bbuf;
89543561Skato	} else {
89643561Skato	    breg = bbuf + x * BIOSDISK_SECSIZE;
89743561Skato	}
898172965Snyan	maxfer = x;		/* limit transfers to bounce region size */
89943561Skato    } else {
90068358Snyan	breg = bbuf = NULL;
90143561Skato	maxfer = 0;
90243561Skato    }
90343561Skato
90443561Skato    while (resid > 0) {
905172965Snyan	/*
906172965Snyan	 * Play it safe and don't cross track boundaries.
907172965Snyan	 * (XXX this is probably unnecessary)
908172965Snyan	 */
909172965Snyan	sec = dblk % od->od_sec;	/* offset into track */
91043561Skato	x = min(od->od_sec - sec, resid);
91143561Skato	if (maxfer > 0)
91243561Skato	    x = min(x, maxfer);		/* fit bounce buffer */
91343561Skato
91443561Skato	/* where do we transfer to? */
91543561Skato	xp = bbuf == NULL ? p : breg;
91643561Skato
917172965Snyan	/*
918172965Snyan	 * Put your Data In, Put your Data out,
919172965Snyan	 * Put your Data In, and shake it all about
920172965Snyan	 */
921172965Snyan	if (write && bbuf != NULL)
922172965Snyan	    bcopy(p, breg, x * BIOSDISK_SECSIZE);
92343561Skato
924172965Snyan	/*
925172965Snyan	 * Loop retrying the operation a couple of times.  The BIOS
926172965Snyan	 * may also retry.
927172965Snyan	 */
92843561Skato	for (retry = 0; retry < 3; retry++) {
92943561Skato	    /* if retrying, reset the drive */
93043561Skato	    if (retry > 0) {
93151586Skato		v86.ctl = V86_FLAGS;
93251586Skato		v86.addr = 0x1b;
93351586Skato		v86.eax = 0x0300 | od->od_unit;
93443561Skato		v86int();
93543561Skato	    }
936172965Snyan
937172965Snyan	    result = bd_chs_io(od, dblk, x, xp, write);
93843561Skato	    if (result == 0)
93943561Skato		break;
94043561Skato	}
941172965Snyan
942172965Snyan	if (write)
943200631Snyan	    DEBUG("Write %d sector(s) from %p (0x%x) to %lld %s", x,
944200631Snyan		p, VTOP(p), dblk, result ? "failed" : "ok");
945172965Snyan	else
946200631Snyan	    DEBUG("Read %d sector(s) from %lld to %p (0x%x) %s", x,
947200631Snyan		dblk, p, VTOP(p), result ? "failed" : "ok");
94843561Skato	if (result) {
94943561Skato	    return(-1);
95043561Skato	}
951172965Snyan	if (!write && bbuf != NULL)
95243561Skato	    bcopy(breg, p, x * BIOSDISK_SECSIZE);
95343561Skato	p += (x * BIOSDISK_SECSIZE);
95443561Skato	dblk += x;
95543561Skato	resid -= x;
95643561Skato    }
957172965Snyan
95843561Skato/*    hexdump(dest, (blks * BIOSDISK_SECSIZE)); */
95943561Skato    return(0);
96043561Skato}
96143561Skato
962172965Snyanstatic int
963172965Snyanbd_read(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest)
964172965Snyan{
96587734Snyan
966172965Snyan    return (bd_io(od, dblk, blks, dest, 0));
967172965Snyan}
968172965Snyan
96943561Skatostatic int
97087734Snyanbd_write(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest)
97187734Snyan{
97287734Snyan
973172965Snyan    return (bd_io(od, dblk, blks, dest, 1));
974172965Snyan}
97587734Snyan
97687734Snyanstatic int
97743561Skatobd_getgeom(struct open_disk *od)
97843561Skato{
97943561Skato
98043561Skato    if (od->od_flags & BD_FLOPPY) {
98154819Snyan	od->od_cyl = 79;
98243561Skato	od->od_hds = 2;
98343561Skato	od->od_sec = (od->od_unit & 0xf0) == 0x30 ? 18 : 15;
984108791Snyan    } else if (od->od_flags & BD_OPTICAL) {
985108791Snyan	od->od_cyl = 0xFFFE;
986108791Snyan	od->od_hds = 8;
987108791Snyan	od->od_sec = 32;
98859777Snyan    } else {
98954819Snyan	v86.ctl = V86_FLAGS;
99043561Skato	v86.addr = 0x1b;
99143561Skato	v86.eax = 0x8400 | od->od_unit;
99243561Skato	v86int();
99343561Skato
99443561Skato	od->od_cyl = v86.ecx;
99543561Skato	od->od_hds = (v86.edx >> 8) & 0xff;
99643561Skato	od->od_sec = v86.edx & 0xff;
997292682Sjhb	if (V86_CY(v86.efl))
99851586Skato	    return(1);
99943561Skato    }
100043561Skato
100143561Skato    DEBUG("unit 0x%x geometry %d/%d/%d", od->od_unit, od->od_cyl, od->od_hds, od->od_sec);
100243561Skato    return(0);
100343561Skato}
100443561Skato
100543561Skato/*
100653207Snyan * Return the BIOS geometry of a given "fixed drive" in a format
100753207Snyan * suitable for the legacy bootinfo structure.  Since the kernel is
100853207Snyan * expecting raw int 0x13/0x8 values for N_BIOS_GEOM drives, we
100953207Snyan * prefer to get the information directly, rather than rely on being
101053207Snyan * able to put it together from information already maintained for
101153207Snyan * different purposes and for a probably different number of drives.
101253207Snyan *
101353207Snyan * For valid drives, the geometry is expected in the format (31..0)
101453207Snyan * "000000cc cccccccc hhhhhhhh 00ssssss"; and invalid drives are
101553207Snyan * indicated by returning the geometry of a "1.2M" PC-format floppy
101653207Snyan * disk.  And, incidentally, what is returned is not the geometry as
101753207Snyan * such but the highest valid cylinder, head, and sector numbers.
101853207Snyan */
101953207Snyanu_int32_t
102053207Snyanbd_getbigeom(int bunit)
102153207Snyan{
102254819Snyan    int hds = 0;
102355339Snyan    int unit = 0x80;		/* IDE HDD */
102454819Snyan    u_int addr = 0xA155d;
102555339Snyan
102654819Snyan    while (unit < 0xa7) {
102755339Snyan	if (*(u_char *)PTOV(addr) & (1 << (unit & 0x0f)))
102855339Snyan	    if (hds++ == bunit)
102954819Snyan		break;
1030108791Snyan
1031108791Snyan	if (unit >= 0xA0) {
1032108791Snyan	    int  media = ((unsigned *)PTOV(0xA1460))[unit & 0x0F] & 0x1F;
1033108791Snyan
1034108791Snyan	    if (media == 7 && hds++ == bunit)	/* SCSI MO */
1035108791Snyan		return(0xFFFE0820); /* C:65535 H:8 S:32 */
1036108791Snyan	}
103754819Snyan	if (++unit == 0x84) {
1038108791Snyan	    unit = 0xA0;	/* SCSI HDD */
103954819Snyan	    addr = 0xA1482;
104054819Snyan	}
104154819Snyan    }
104254819Snyan    if (unit == 0xa7)
1043108791Snyan	return 0x4F020F;	/* 1200KB FD C:80 H:2 S:15 */
104453207Snyan    v86.ctl = V86_FLAGS;
104554819Snyan    v86.addr = 0x1b;
104654819Snyan    v86.eax = 0x8400 | unit;
104754819Snyan    v86int();
1048292682Sjhb    if (V86_CY(v86.efl))
1049108791Snyan	return 0x4F020F;	/* 1200KB FD C:80 H:2 S:15 */
105054819Snyan    return ((v86.ecx & 0xffff) << 16) | (v86.edx & 0xffff);
105153207Snyan}
105253207Snyan
105353207Snyan/*
1054130603Sphk * Return a suitable dev_t value for (dev).
105543561Skato *
105643561Skato * In the case where it looks like (dev) is a SCSI disk, we allow the number of
105743561Skato * IDE disks to be specified in $num_ide_disks.  There should be a Better Way.
105843561Skato */
105943561Skatoint
106043561Skatobd_getdev(struct i386_devdesc *dev)
106143561Skato{
106243561Skato    struct open_disk		*od;
106343561Skato    int				biosdev;
106443561Skato    int 			major;
106543561Skato    int				rootdev;
106643561Skato    char			*nip, *cp;
106743561Skato    int				unitofs = 0, i, unit;
106843561Skato
1069332154Skevans    biosdev = bd_unit2bios(dev->dd.d_unit);
1070332154Skevans    DEBUG("unit %d BIOS device %d", dev->dd.d_unit, biosdev);
107143561Skato    if (biosdev == -1)				/* not a BIOS device */
107243561Skato	return(-1);
107343561Skato    if (bd_opendisk(&od, dev) != 0)		/* oops, not a viable device */
107443561Skato	return(-1);
107543561Skato
107643561Skato    if ((biosdev & 0xf0) == 0x90 || (biosdev & 0xf0) == 0x30) {
107743561Skato	/* floppy (or emulated floppy) or ATAPI device */
1078332154Skevans	if (bdinfo[dev->dd.d_unit].bd_type == DT_ATAPI) {
107943561Skato	    /* is an ATAPI disk */
108043561Skato	    major = WFDMAJOR;
108143561Skato	} else {
108243561Skato	    /* is a floppy disk */
108343561Skato	    major = FDMAJOR;
108443561Skato	}
108543561Skato    } else {
108643561Skato	/* harddisk */
108743561Skato	if ((od->od_flags & BD_LABELOK) && (od->od_disklabel.d_type == DTYPE_SCSI)) {
108843561Skato	    /* label OK, disk labelled as SCSI */
108943561Skato	    major = DAMAJOR;
109043561Skato	    /* check for unit number correction hint, now deprecated */
109143561Skato	    if ((nip = getenv("num_ide_disks")) != NULL) {
109243561Skato		i = strtol(nip, &cp, 0);
109343561Skato		/* check for parse error */
109443561Skato		if ((cp != nip) && (*cp == 0))
109543561Skato		    unitofs = i;
109643561Skato	    }
109743561Skato	} else {
109843561Skato	    /* assume an IDE disk */
109943561Skato	    major = WDMAJOR;
110043561Skato	}
110143561Skato    }
110268358Snyan    /* default root disk unit number */
110368358Snyan    if ((biosdev & 0xf0) == 0xa0)
1104332154Skevans	unit = bdinfo[dev->dd.d_unit].bd_da_unit;
110568358Snyan    else
110668358Snyan	unit = biosdev & 0xf;
110768358Snyan
110843561Skato    /* XXX a better kludge to set the root disk unit number */
110943561Skato    if ((nip = getenv("root_disk_unit")) != NULL) {
111043561Skato	i = strtol(nip, &cp, 0);
111143561Skato	/* check for parse error */
111243561Skato	if ((cp != nip) && (*cp == 0))
111343561Skato	    unit = i;
111443561Skato    }
111543561Skato
1116172921Sjhb    rootdev = MAKEBOOTDEV(major, dev->d_kind.biosdisk.slice + 1, unit,
1117172921Sjhb	dev->d_kind.biosdisk.partition);
111843561Skato    DEBUG("dev is 0x%x\n", rootdev);
111943561Skato    return(rootdev);
112043561Skato}
1121