biosdisk.c revision 298230
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: head/sys/boot/pc98/libpc98/biosdisk.c 298230 2016-04-18 23:09:22Z allanjude $"); 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 104298230Sallanjude#define BD(dev) (bdinfo[(dev)->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 114108650Snyanstatic void bd_printslice(struct open_disk *od, struct pc98_partition *dp, 11568358Snyan char *prefix, int verbose); 11668358Snyanstatic void 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, 12168358Snyan size_t size, char *buf, size_t *rsize); 12259777Snyanstatic int bd_realstrategy(void *devdata, int flag, daddr_t dblk, 12368358Snyan size_t size, char *buf, size_t *rsize); 12443561Skatostatic int bd_open(struct open_file *f, ...); 12543561Skatostatic int bd_close(struct open_file *f); 12643561Skatostatic void 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 */ 25243561Skatostatic void 25343561Skatobd_print(int verbose) 25443561Skato{ 25543561Skato int i, j; 25643561Skato char line[80]; 25743561Skato struct i386_devdesc dev; 25843561Skato struct open_disk *od; 259108650Snyan struct pc98_partition *dptr; 26043561Skato 26143561Skato for (i = 0; i < nbdinfo; i++) { 26249426Skato sprintf(line, " disk%d: BIOS drive %c:\n", i, 'A' + i); 26343561Skato pager_output(line); 26443561Skato 26543561Skato /* try to open the whole disk */ 266163897Smarcel dev.d_unit = i; 26743561Skato dev.d_kind.biosdisk.slice = -1; 26843561Skato dev.d_kind.biosdisk.partition = -1; 26943561Skato 27043561Skato if (!bd_opendisk(&od, &dev)) { 27143561Skato 27243561Skato /* Do we have a partition table? */ 27343561Skato if (od->od_flags & BD_PARTTABOK) { 27459777Snyan dptr = &od->od_slicetab[0]; 27543561Skato 27668358Snyan /* Check for a "dedicated" disk */ 27759777Snyan for (j = 0; j < od->od_nslices; j++) { 278190127Snyan sprintf(line, " disk%ds%d", i, j + 1); 279190127Snyan bd_printslice(od, &dptr[j], line, verbose); 28043561Skato } 28143561Skato } 28243561Skato bd_closedisk(od); 28343561Skato } 28443561Skato } 28543561Skato} 28643561Skato 287176654Snyan/* Given a size in 512 byte sectors, convert it to a human-readable number. */ 288176654Snyanstatic char * 289176654Snyandisplay_size(uint64_t size) 290176654Snyan{ 291176654Snyan static char buf[80]; 292176654Snyan char unit; 293176654Snyan 294176654Snyan size /= 2; 295176654Snyan unit = 'K'; 296176654Snyan if (size >= 10485760000LL) { 297176654Snyan size /= 1073741824; 298176654Snyan unit = 'T'; 299176654Snyan } else if (size >= 10240000) { 300176654Snyan size /= 1048576; 301176654Snyan unit = 'G'; 302176654Snyan } else if (size >= 10000) { 303176654Snyan size /= 1024; 304176654Snyan unit = 'M'; 305176654Snyan } 306250333Sjhb sprintf(buf, "%6ld%cB", (long)size, unit); 307176654Snyan return (buf); 308176654Snyan} 309176654Snyan 31059777Snyan/* 311190127Snyan * Print information about slices on a disk. For the size calculations we 312190127Snyan * assume a 512 byte sector. 313190127Snyan */ 314190127Snyanstatic void 315190127Snyanbd_printslice(struct open_disk *od, struct pc98_partition *dp, char *prefix, 316190127Snyan int verbose) 317190127Snyan{ 318190127Snyan int cylsecs, start, size; 319190127Snyan char stats[80]; 320190127Snyan char line[80]; 321190127Snyan 322190127Snyan cylsecs = od->od_hds * od->od_sec; 323190127Snyan start = dp->dp_scyl * cylsecs + dp->dp_shd * od->od_sec + dp->dp_ssect; 324190127Snyan size = (dp->dp_ecyl - dp->dp_scyl + 1) * cylsecs; 325190127Snyan 326190127Snyan if (verbose) 327190127Snyan sprintf(stats, " %s (%d - %d)", display_size(size), 328190127Snyan start, start + size); 329190127Snyan else 330190127Snyan stats[0] = '\0'; 331190127Snyan 332190127Snyan switch(dp->dp_mid & PC98_MID_MASK) { 333190127Snyan case PC98_MID_386BSD: 334190127Snyan bd_printbsdslice(od, start, prefix, verbose); 335190127Snyan return; 336190127Snyan case 0x00: /* unused partition */ 337190127Snyan return; 338190127Snyan case 0x01: 339190127Snyan sprintf(line, "%s: FAT-12%s\n", prefix, stats); 340190127Snyan break; 341190127Snyan case 0x11: 342190127Snyan case 0x20: 343190127Snyan case 0x21: 344190127Snyan case 0x22: 345190127Snyan case 0x23: 346190127Snyan case 0x24: 347190127Snyan sprintf(line, "%s: FAT-16%s\n", prefix, stats); 348190127Snyan break; 349190127Snyan default: 350190127Snyan sprintf(line, "%s: Unknown fs: 0x%x %s\n", prefix, dp->dp_mid, 351190127Snyan stats); 352190127Snyan } 353190127Snyan pager_output(line); 354190127Snyan} 355190127Snyan 356190127Snyan/* 35768358Snyan * Print out each valid partition in the disklabel of a FreeBSD slice. 35868358Snyan * For size calculations, we assume a 512 byte sector size. 35968358Snyan */ 36059777Snyanstatic void 36168358Snyanbd_printbsdslice(struct open_disk *od, daddr_t offset, char *prefix, 36268358Snyan int verbose) 36359777Snyan{ 36443561Skato char line[80]; 36568358Snyan char buf[BIOSDISK_SECSIZE]; 36643561Skato struct disklabel *lp; 36743561Skato int i; 36843561Skato 36943561Skato /* read disklabel */ 37043561Skato if (bd_read(od, offset + LABELSECTOR, 1, buf)) 37143561Skato return; 37243561Skato lp =(struct disklabel *)(&buf[0]); 37343561Skato if (lp->d_magic != DISKMAGIC) { 37463101Snyan sprintf(line, "%s: FFS bad disklabel\n", prefix); 37543561Skato pager_output(line); 37643561Skato return; 37743561Skato } 37843561Skato 37943561Skato /* Print partitions */ 38043561Skato for (i = 0; i < lp->d_npartitions; i++) { 38168358Snyan /* 38268358Snyan * For each partition, make sure we know what type of fs it is. If 38368358Snyan * not, then skip it. However, since floppies often have bogus 38468358Snyan * fstypes, print the 'a' partition on a floppy even if it is marked 38568358Snyan * unused. 38668358Snyan */ 38763101Snyan if ((lp->d_partitions[i].p_fstype == FS_BSDFFS) || 38863101Snyan (lp->d_partitions[i].p_fstype == FS_SWAP) || 38963101Snyan (lp->d_partitions[i].p_fstype == FS_VINUM) || 39043561Skato ((lp->d_partitions[i].p_fstype == FS_UNUSED) && 39168358Snyan (od->od_flags & BD_FLOPPY) && (i == 0))) { 39268358Snyan 39368358Snyan /* Only print out statistics in verbose mode */ 39468358Snyan if (verbose) 395190127Snyan sprintf(line, " %s%c: %s %s (%d - %d)\n", prefix, 'a' + i, 396176654Snyan (lp->d_partitions[i].p_fstype == FS_SWAP) ? "swap " : 39768358Snyan (lp->d_partitions[i].p_fstype == FS_VINUM) ? "vinum" : 398176654Snyan "FFS ", 399176654Snyan display_size(lp->d_partitions[i].p_size), 40068358Snyan lp->d_partitions[i].p_offset, 40168358Snyan lp->d_partitions[i].p_offset + lp->d_partitions[i].p_size); 40268358Snyan else 40368358Snyan sprintf(line, " %s%c: %s\n", prefix, 'a' + i, 40468358Snyan (lp->d_partitions[i].p_fstype == FS_SWAP) ? "swap" : 40568358Snyan (lp->d_partitions[i].p_fstype == FS_VINUM) ? "vinum" : 40668358Snyan "FFS"); 40743561Skato pager_output(line); 40843561Skato } 40943561Skato } 41043561Skato} 41143561Skato 41243561Skato 41343561Skato/* 41443561Skato * Attempt to open the disk described by (dev) for use by (f). 41543561Skato * 41643561Skato * Note that the philosophy here is "give them exactly what 41743561Skato * they ask for". This is necessary because being too "smart" 41843561Skato * about what the user might want leads to complications. 41943561Skato * (eg. given no slice or partition value, with a disk that is 42043561Skato * sliced - are they after the first BSD slice, or the DOS 42143561Skato * slice before it?) 42243561Skato */ 42343561Skatostatic int 42443561Skatobd_open(struct open_file *f, ...) 42543561Skato{ 42643561Skato va_list ap; 42743561Skato struct i386_devdesc *dev; 42843561Skato struct open_disk *od; 42943561Skato int error; 43043561Skato 43143561Skato va_start(ap, f); 43243561Skato dev = va_arg(ap, struct i386_devdesc *); 43343561Skato va_end(ap); 43443561Skato if ((error = bd_opendisk(&od, dev))) 43543561Skato return(error); 43643561Skato 437298230Sallanjude BD(dev).bd_open++; 438298230Sallanjude if (BD(dev).bd_bcache == NULL) 439298230Sallanjude BD(dev).bd_bcache = bcache_allocate(); 440298230Sallanjude 44143561Skato /* 44243561Skato * Save our context 44343561Skato */ 44443561Skato ((struct i386_devdesc *)(f->f_devdata))->d_kind.biosdisk.data = od; 44543561Skato DEBUG("open_disk %p, partition at 0x%x", od, od->od_boff); 44643561Skato return(0); 44743561Skato} 44843561Skato 44943561Skatostatic int 45043561Skatobd_opendisk(struct open_disk **odp, struct i386_devdesc *dev) 45143561Skato{ 45243561Skato struct open_disk *od; 45343561Skato int error; 45443561Skato 455163897Smarcel if (dev->d_unit >= nbdinfo) { 45643561Skato DEBUG("attempt to open nonexistent disk"); 45743561Skato return(ENXIO); 45843561Skato } 45943561Skato 46043561Skato od = (struct open_disk *)malloc(sizeof(struct open_disk)); 46143561Skato if (!od) { 46243561Skato DEBUG("no memory"); 46343561Skato return (ENOMEM); 46443561Skato } 46543561Skato 46643561Skato /* Look up BIOS unit number, intialise open_disk structure */ 467163897Smarcel od->od_dkunit = dev->d_unit; 46843561Skato od->od_unit = bdinfo[od->od_dkunit].bd_unit; 46943561Skato od->od_flags = bdinfo[od->od_dkunit].bd_flags; 47043561Skato od->od_boff = 0; 47143561Skato error = 0; 472172968Snyan DEBUG("open '%s', unit 0x%x slice %d partition %d", 473163897Smarcel i386_fmtdev(dev), dev->d_unit, 474172968Snyan dev->d_kind.biosdisk.slice, dev->d_kind.biosdisk.partition); 47543561Skato 47643561Skato /* Get geometry for this open (removable device may have changed) */ 47743561Skato if (bd_getgeom(od)) { 47843561Skato DEBUG("can't get geometry"); 47943561Skato error = ENXIO; 48043561Skato goto out; 48143561Skato } 48243561Skato 483172968Snyan /* Determine disk layout. */ 484172968Snyan error = bd_open_pc98(od, dev); 485172968Snyan 486172968Snyan out: 487172968Snyan if (error) { 488172968Snyan free(od); 489172968Snyan } else { 490172968Snyan *odp = od; /* return the open disk */ 491172968Snyan } 492172968Snyan return(error); 493172968Snyan} 494172968Snyan 495172968Snyanstatic int 496172968Snyanbd_open_pc98(struct open_disk *od, struct i386_devdesc *dev) 497172968Snyan{ 498172968Snyan struct pc98_partition *dptr; 499172968Snyan struct disklabel *lp; 500172968Snyan int sector, slice, i; 501172968Snyan char buf[BUFSIZE]; 502172968Snyan 50343561Skato /* 50443561Skato * Following calculations attempt to determine the correct value 50543561Skato * for d->od_boff by looking for the slice and partition specified, 50643561Skato * or searching for reasonable defaults. 50743561Skato */ 50843561Skato 50943561Skato /* 51043561Skato * Find the slice in the DOS slice table. 51143561Skato */ 512172968Snyan od->od_nslices = 0; 51343561Skato if (od->od_flags & BD_FLOPPY) { 51443561Skato sector = 0; 51543561Skato goto unsliced; 51643561Skato } 51743561Skato if (bd_read(od, 0, 1, buf)) { 51843561Skato DEBUG("error reading MBR"); 519172968Snyan return (EIO); 52043561Skato } 52143561Skato 52243561Skato /* 52343561Skato * Check the slice table magic. 52443561Skato */ 52568358Snyan if (((u_char)buf[0x1fe] != 0x55) || ((u_char)buf[0x1ff] != 0xaa)) { 52643561Skato /* If a slice number was explicitly supplied, this is an error */ 52743561Skato if (dev->d_kind.biosdisk.slice > 0) { 52843561Skato DEBUG("no slice table/MBR (no magic)"); 529172968Snyan return (ENOENT); 53043561Skato } 53143561Skato sector = 0; 53243561Skato goto unsliced; /* may be a floppy */ 53343561Skato } 53443561Skato if (bd_read(od, 1, 1, buf)) { 53543561Skato DEBUG("error reading MBR"); 536172968Snyan return (EIO); 53743561Skato } 53859777Snyan 53959777Snyan /* 54059777Snyan * copy the partition table, then pick up any extended partitions. 54159777Snyan */ 542254015Smarcel bcopy(buf + PC98_PARTOFF, &od->od_slicetab, 543254015Smarcel sizeof(struct pc98_partition) * PC98_NPARTS); 544254015Smarcel od->od_nslices = PC98_NPARTS; /* extended slices start here */ 54543561Skato od->od_flags |= BD_PARTTABOK; 54659777Snyan dptr = &od->od_slicetab[0]; 54743561Skato 54843561Skato /* Is this a request for the whole disk? */ 54943561Skato if (dev->d_kind.biosdisk.slice == -1) { 55043561Skato sector = 0; 55143561Skato goto unsliced; 55243561Skato } 55343561Skato 55459777Snyan /* 55559777Snyan * if a slice number was supplied but not found, this is an error. 55659777Snyan */ 55759777Snyan if (dev->d_kind.biosdisk.slice > 0) { 55859777Snyan slice = dev->d_kind.biosdisk.slice - 1; 55959777Snyan if (slice >= od->od_nslices) { 56059777Snyan DEBUG("slice %d not found", slice); 561172968Snyan return (ENOENT); 56259777Snyan } 56359777Snyan } 56443561Skato 56559777Snyan /* Try to auto-detect the best slice; this should always give a slice number */ 56659777Snyan if (dev->d_kind.biosdisk.slice == 0) { 56759777Snyan slice = bd_bestslice(od); 56859777Snyan if (slice == -1) { 569172968Snyan return (ENOENT); 57059777Snyan } 57159777Snyan dev->d_kind.biosdisk.slice = slice; 57259777Snyan } 57359777Snyan 57459777Snyan dptr = &od->od_slicetab[0]; 57543561Skato /* 57643561Skato * Accept the supplied slice number unequivocally (we may be looking 57743561Skato * at a DOS partition). 57843561Skato */ 57943561Skato dptr += (dev->d_kind.biosdisk.slice - 1); /* we number 1-4, offsets are 0-3 */ 58059777Snyan sector = dptr->dp_scyl * od->od_hds * od->od_sec + 58159777Snyan dptr->dp_shd * od->od_sec + dptr->dp_ssect; 58243561Skato { 58359777Snyan int end = dptr->dp_ecyl * od->od_hds * od->od_sec + 58459777Snyan dptr->dp_ehd * od->od_sec + dptr->dp_esect; 58559777Snyan DEBUG("slice entry %d at %d, %d sectors", 58659777Snyan dev->d_kind.biosdisk.slice - 1, sector, end-sector); 58743561Skato } 58843561Skato 58943561Skato /* 59043561Skato * If we are looking at a BSD slice, and the partition is < 0, assume the 'a' partition 59143561Skato */ 59243561Skato if ((dptr->dp_mid == DOSMID_386BSD) && (dev->d_kind.biosdisk.partition < 0)) 59343561Skato dev->d_kind.biosdisk.partition = 0; 59443561Skato 59543561Skato unsliced: 59643561Skato /* 59743561Skato * Now we have the slice offset, look for the partition in the disklabel if we have 59843561Skato * a partition to start with. 59943561Skato * 60043561Skato * XXX we might want to check the label checksum. 60143561Skato */ 60243561Skato if (dev->d_kind.biosdisk.partition < 0) { 60343561Skato od->od_boff = sector; /* no partition, must be after the slice */ 60443561Skato DEBUG("opening raw slice"); 60543561Skato } else { 60653207Snyan 60743561Skato if (bd_read(od, sector + LABELSECTOR, 1, buf)) { 60843561Skato DEBUG("error reading disklabel"); 609172968Snyan return (EIO); 61043561Skato } 61143561Skato DEBUG("copy %d bytes of label from %p to %p", sizeof(struct disklabel), buf + LABELOFFSET, &od->od_disklabel); 61243561Skato bcopy(buf + LABELOFFSET, &od->od_disklabel, sizeof(struct disklabel)); 61343561Skato lp = &od->od_disklabel; 61443561Skato od->od_flags |= BD_LABELOK; 61543561Skato 61643561Skato if (lp->d_magic != DISKMAGIC) { 61743561Skato DEBUG("no disklabel"); 618172968Snyan return (ENOENT); 61943561Skato } 62043561Skato if (dev->d_kind.biosdisk.partition >= lp->d_npartitions) { 62143561Skato DEBUG("partition '%c' exceeds partitions in table (a-'%c')", 62243561Skato 'a' + dev->d_kind.biosdisk.partition, 'a' + lp->d_npartitions); 623172968Snyan return (EPART); 62443561Skato } 62543561Skato 62668358Snyan#ifdef DISK_DEBUG 62768358Snyan /* Complain if the partition is unused unless this is a floppy. */ 62843561Skato if ((lp->d_partitions[dev->d_kind.biosdisk.partition].p_fstype == FS_UNUSED) && 62968358Snyan !(od->od_flags & BD_FLOPPY)) 63043561Skato DEBUG("warning, partition marked as unused"); 63168358Snyan#endif 63243561Skato 633146010Snyan od->od_boff = 634146010Snyan lp->d_partitions[dev->d_kind.biosdisk.partition].p_offset - 635146010Snyan lp->d_partitions[RAW_PART].p_offset + 636146010Snyan sector; 63743561Skato } 638172968Snyan return (0); 63943561Skato} 64043561Skato 64143561Skato/* 64243561Skato * Search for a slice with the following preferences: 64343561Skato * 64443561Skato * 1: Active FreeBSD slice 64543561Skato * 2: Non-active FreeBSD slice 64659777Snyan * 3: Active Linux slice 64759777Snyan * 4: non-active Linux slice 64859777Snyan * 5: Active FAT/FAT32 slice 64959777Snyan * 6: non-active FAT/FAT32 slice 65043561Skato */ 65159777Snyan#define PREF_RAWDISK 0 65259777Snyan#define PREF_FBSD_ACT 1 65359777Snyan#define PREF_FBSD 2 65459777Snyan#define PREF_LINUX_ACT 3 65559777Snyan#define PREF_LINUX 4 65659777Snyan#define PREF_DOS_ACT 5 65759777Snyan#define PREF_DOS 6 65859777Snyan#define PREF_NONE 7 65943561Skato 66059777Snyan/* 661254015Smarcel * slicelimit is in the range 0 .. PC98_NPARTS 66259777Snyan */ 66343561Skatostatic int 66459777Snyanbd_bestslice(struct open_disk *od) 66543561Skato{ 666108650Snyan struct pc98_partition *dp; 66759777Snyan int pref, preflevel; 66859777Snyan int i, prefslice; 66953207Snyan 67059777Snyan prefslice = 0; 67159777Snyan preflevel = PREF_NONE; 67243561Skato 67359777Snyan dp = &od->od_slicetab[0]; 67459777Snyan for (i = 0; i < od->od_nslices; i++, dp++) { 675190029Snyan switch(dp->dp_mid & PC98_MID_MASK) { 676190029Snyan case PC98_MID_386BSD: /* FreeBSD */ 677190029Snyan if ((dp->dp_mid & PC98_MID_BOOTABLE) && 67859777Snyan (preflevel > PREF_FBSD_ACT)) { 67959777Snyan pref = i; 68059777Snyan preflevel = PREF_FBSD_ACT; 68159777Snyan } else if (preflevel > PREF_FBSD) { 68259777Snyan pref = i; 68359777Snyan preflevel = PREF_FBSD; 68459777Snyan } 68559777Snyan break; 68659777Snyan 68759777Snyan case 0x11: /* DOS/Windows */ 68859777Snyan case 0x20: 68959777Snyan case 0x21: 69059777Snyan case 0x22: 69159777Snyan case 0x23: 69259777Snyan case 0x63: 693190029Snyan if ((dp->dp_mid & PC98_MID_BOOTABLE) && 69459777Snyan (preflevel > PREF_DOS_ACT)) { 69559777Snyan pref = i; 69659777Snyan preflevel = PREF_DOS_ACT; 69759777Snyan } else if (preflevel > PREF_DOS) { 69859777Snyan pref = i; 69959777Snyan preflevel = PREF_DOS; 70059777Snyan } 70159777Snyan break; 70259777Snyan } 70343561Skato } 70459777Snyan return (prefslice); 70543561Skato} 70687734Snyan 70743561Skatostatic int 70843561Skatobd_close(struct open_file *f) 70943561Skato{ 710298230Sallanjude struct i386_devdesc *dev = f->f_devdata; 711298230Sallanjude struct open_disk *od = (struct open_disk *)(dev->d_kind.biosdisk.data); 71243561Skato 713298230Sallanjude BD(dev).bd_open--; 714298230Sallanjude if (BD(dev).bd_open == 0) { 715298230Sallanjude bcache_free(BD(dev).bd_bcache); 716298230Sallanjude BD(dev).bd_bcache = NULL; 717298230Sallanjude } 718298230Sallanjude 71943561Skato bd_closedisk(od); 72043561Skato return(0); 72143561Skato} 72243561Skato 72343561Skatostatic void 72443561Skatobd_closedisk(struct open_disk *od) 72543561Skato{ 72643561Skato DEBUG("open_disk %p", od); 72743561Skato#if 0 72843561Skato /* XXX is this required? (especially if disk already open...) */ 72943561Skato if (od->od_flags & BD_FLOPPY) 73043561Skato delay(3000000); 73143561Skato#endif 73243561Skato free(od); 73343561Skato} 73443561Skato 73543561Skatostatic int 736298230Sallanjudebd_strategy(void *devdata, int rw, daddr_t dblk, size_t offset, size_t size, 737298230Sallanjude char *buf, size_t *rsize) 73843561Skato{ 73943561Skato struct bcache_devdata bcd; 740298230Sallanjude struct i386_devdesc *dev = f->f_devdata; 741298230Sallanjude struct open_disk *od = (struct open_disk *)(dev->d_kind.biosdisk.data); 74258165Snyan 74343561Skato bcd.dv_strategy = bd_realstrategy; 74443561Skato bcd.dv_devdata = devdata; 745298230Sallanjude bcd.dv_cache = BD(dev).bd_bcache; 746298230Sallanjude return(bcache_strategy(&bcd, od->od_unit, rw, dblk+od->od_boff, offset, 747298230Sallanjude size, buf, rsize)); 74843561Skato} 74943561Skato 75043561Skatostatic int 751298230Sallanjudebd_realstrategy(void *devdata, int rw, daddr_t dblk, size_t offset, 752298230Sallanjude size_t size, char *buf, size_t *rsize) 75343561Skato{ 75443561Skato struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)devdata)->d_kind.biosdisk.data); 75543561Skato int blks; 75643561Skato#ifdef BD_SUPPORT_FRAGS 75743561Skato char fragbuf[BIOSDISK_SECSIZE]; 75843561Skato size_t fragsize; 75943561Skato 76043561Skato fragsize = size % BIOSDISK_SECSIZE; 76143561Skato#else 76243561Skato if (size % BIOSDISK_SECSIZE) 76343561Skato panic("bd_strategy: %d bytes I/O not multiple of block size", size); 76443561Skato#endif 76543561Skato 76643561Skato DEBUG("open_disk %p", od); 76743561Skato blks = size / BIOSDISK_SECSIZE; 76843561Skato if (rsize) 76943561Skato *rsize = 0; 770172925Snyan 771172925Snyan switch(rw){ 772172925Snyan case F_READ: 773172925Snyan DEBUG("read %d from %d to %p", blks, dblk, buf); 774172925Snyan 775172925Snyan if (blks && bd_read(od, dblk, blks, buf)) { 776172925Snyan DEBUG("read error"); 777172925Snyan return (EIO); 778172925Snyan } 77943561Skato#ifdef BD_SUPPORT_FRAGS 780172925Snyan DEBUG("bd_strategy: frag read %d from %d+%d to %p", 781172925Snyan fragsize, dblk, blks, buf + (blks * BIOSDISK_SECSIZE)); 782172925Snyan if (fragsize && bd_read(od, dblk + blks, 1, fragsize)) { 783172925Snyan DEBUG("frag read error"); 784172925Snyan return(EIO); 785172925Snyan } 786172925Snyan bcopy(fragbuf, buf + (blks * BIOSDISK_SECSIZE), fragsize); 78743561Skato#endif 788172925Snyan break; 789172925Snyan case F_WRITE : 790172925Snyan DEBUG("write %d from %d to %p", blks, dblk, buf); 79187734Snyan 792172925Snyan if (blks && bd_write(od, dblk, blks, buf)) { 793172925Snyan DEBUG("write error"); 794172925Snyan return (EIO); 795172925Snyan } 79687734Snyan#ifdef BD_SUPPORT_FRAGS 79787734Snyan if(fragsize) { 798172925Snyan DEBUG("Attempted to write a frag"); 799172925Snyan return (EIO); 80087734Snyan } 80187734Snyan#endif 802172925Snyan break; 803172925Snyan default: 804172925Snyan /* DO NOTHING */ 805172925Snyan return (EROFS); 806172925Snyan } 80787734Snyan 80887734Snyan if (rsize) 80987734Snyan *rsize = size; 81087734Snyan return (0); 81143561Skato} 81243561Skato 81343561Skato/* Max number of sectors to bounce-buffer if the request crosses a 64k boundary */ 81443561Skato#define FLOPPY_BOUNCEBUF 18 81543561Skato 81643561Skatostatic int 817172965Snyanbd_chs_io(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest, int write) 81843561Skato{ 819172965Snyan u_int x, bpc, cyl, hd, sec; 820172965Snyan 821172965Snyan bpc = (od->od_sec * od->od_hds); /* blocks per cylinder */ 822172965Snyan x = dblk; 823172965Snyan cyl = x / bpc; /* block # / blocks per cylinder */ 824172965Snyan x %= bpc; /* block offset into cylinder */ 825172965Snyan hd = x / od->od_sec; /* offset / blocks per track */ 826172965Snyan sec = x % od->od_sec; /* offset into track */ 827172965Snyan 828172965Snyan v86.ctl = V86_FLAGS; 829172965Snyan v86.addr = 0x1b; 830172965Snyan if (write) 831172965Snyan v86.eax = 0x0500 | od->od_unit; 832172965Snyan else 833172965Snyan v86.eax = 0x0600 | od->od_unit; 834172965Snyan if (od->od_flags & BD_FLOPPY) { 835172965Snyan v86.eax |= 0xd000; 836172965Snyan v86.ecx = 0x0200 | (cyl & 0xff); 837172965Snyan v86.edx = (hd << 8) | (sec + 1); 838172965Snyan } else if (od->od_flags & BD_OPTICAL) { 839172965Snyan v86.eax &= 0xFF7F; 840172965Snyan v86.ecx = dblk & 0xFFFF; 841172965Snyan v86.edx = dblk >> 16; 842172965Snyan } else { 843172965Snyan v86.ecx = cyl; 844172965Snyan v86.edx = (hd << 8) | sec; 845172965Snyan } 846172965Snyan v86.ebx = blks * BIOSDISK_SECSIZE; 847172965Snyan v86.es = VTOPSEG(dest); 848172965Snyan v86.ebp = VTOPOFF(dest); 849172965Snyan v86int(); 850292682Sjhb return (V86_CY(v86.efl)); 851172965Snyan} 852172965Snyan 853172965Snyanstatic int 854172965Snyanbd_io(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest, int write) 855172965Snyan{ 856172965Snyan u_int x, sec, result, resid, retry, maxfer; 85743561Skato caddr_t p, xp, bbuf, breg; 85843561Skato 859172965Snyan /* Just in case some idiot actually tries to read/write -1 blocks... */ 86068358Snyan if (blks < 0) 86168358Snyan return (-1); 86268358Snyan 86343561Skato resid = blks; 86443561Skato p = dest; 86543561Skato 86643561Skato /* Decide whether we have to bounce */ 867153598Snyan if (VTOP(dest) >> 20 != 0 || 868153598Snyan ((VTOP(dest) >> 16) != (VTOP(dest + blks * BIOSDISK_SECSIZE) >> 16))) { 86943561Skato 87043561Skato /* 871172965Snyan * There is a 64k physical boundary somewhere in the 872172965Snyan * destination buffer, or the destination buffer is above 873172965Snyan * first 1MB of physical memory so we have to arrange a 874172965Snyan * suitable bounce buffer. Allocate a buffer twice as large 875172965Snyan * as we need to. Use the bottom half unless there is a break 876172965Snyan * there, in which case we use the top half. 87743561Skato */ 87868358Snyan x = min(od->od_sec, (unsigned)blks); 879153598Snyan bbuf = alloca(x * 2 * BIOSDISK_SECSIZE); 880172965Snyan if (((u_int32_t)VTOP(bbuf) & 0xffff0000) == 881172965Snyan ((u_int32_t)VTOP(bbuf + x * BIOSDISK_SECSIZE) & 0xffff0000)) { 88243561Skato breg = bbuf; 88343561Skato } else { 88443561Skato breg = bbuf + x * BIOSDISK_SECSIZE; 88543561Skato } 886172965Snyan maxfer = x; /* limit transfers to bounce region size */ 88743561Skato } else { 88868358Snyan breg = bbuf = NULL; 88943561Skato maxfer = 0; 89043561Skato } 89143561Skato 89243561Skato while (resid > 0) { 893172965Snyan /* 894172965Snyan * Play it safe and don't cross track boundaries. 895172965Snyan * (XXX this is probably unnecessary) 896172965Snyan */ 897172965Snyan sec = dblk % od->od_sec; /* offset into track */ 89843561Skato x = min(od->od_sec - sec, resid); 89943561Skato if (maxfer > 0) 90043561Skato x = min(x, maxfer); /* fit bounce buffer */ 90143561Skato 90243561Skato /* where do we transfer to? */ 90343561Skato xp = bbuf == NULL ? p : breg; 90443561Skato 905172965Snyan /* 906172965Snyan * Put your Data In, Put your Data out, 907172965Snyan * Put your Data In, and shake it all about 908172965Snyan */ 909172965Snyan if (write && bbuf != NULL) 910172965Snyan bcopy(p, breg, x * BIOSDISK_SECSIZE); 91143561Skato 912172965Snyan /* 913172965Snyan * Loop retrying the operation a couple of times. The BIOS 914172965Snyan * may also retry. 915172965Snyan */ 91643561Skato for (retry = 0; retry < 3; retry++) { 91743561Skato /* if retrying, reset the drive */ 91843561Skato if (retry > 0) { 91951586Skato v86.ctl = V86_FLAGS; 92051586Skato v86.addr = 0x1b; 92151586Skato v86.eax = 0x0300 | od->od_unit; 92243561Skato v86int(); 92343561Skato } 924172965Snyan 925172965Snyan result = bd_chs_io(od, dblk, x, xp, write); 92643561Skato if (result == 0) 92743561Skato break; 92843561Skato } 929172965Snyan 930172965Snyan if (write) 931200631Snyan DEBUG("Write %d sector(s) from %p (0x%x) to %lld %s", x, 932200631Snyan p, VTOP(p), dblk, result ? "failed" : "ok"); 933172965Snyan else 934200631Snyan DEBUG("Read %d sector(s) from %lld to %p (0x%x) %s", x, 935200631Snyan dblk, p, VTOP(p), result ? "failed" : "ok"); 93643561Skato if (result) { 93743561Skato return(-1); 93843561Skato } 939172965Snyan if (!write && bbuf != NULL) 94043561Skato bcopy(breg, p, x * BIOSDISK_SECSIZE); 94143561Skato p += (x * BIOSDISK_SECSIZE); 94243561Skato dblk += x; 94343561Skato resid -= x; 94443561Skato } 945172965Snyan 94643561Skato/* hexdump(dest, (blks * BIOSDISK_SECSIZE)); */ 94743561Skato return(0); 94843561Skato} 94943561Skato 950172965Snyanstatic int 951172965Snyanbd_read(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest) 952172965Snyan{ 95387734Snyan 954172965Snyan return (bd_io(od, dblk, blks, dest, 0)); 955172965Snyan} 956172965Snyan 95743561Skatostatic int 95887734Snyanbd_write(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest) 95987734Snyan{ 96087734Snyan 961172965Snyan return (bd_io(od, dblk, blks, dest, 1)); 962172965Snyan} 96387734Snyan 96487734Snyanstatic int 96543561Skatobd_getgeom(struct open_disk *od) 96643561Skato{ 96743561Skato 96843561Skato if (od->od_flags & BD_FLOPPY) { 96954819Snyan od->od_cyl = 79; 97043561Skato od->od_hds = 2; 97143561Skato od->od_sec = (od->od_unit & 0xf0) == 0x30 ? 18 : 15; 972108791Snyan } else if (od->od_flags & BD_OPTICAL) { 973108791Snyan od->od_cyl = 0xFFFE; 974108791Snyan od->od_hds = 8; 975108791Snyan od->od_sec = 32; 97659777Snyan } else { 97754819Snyan v86.ctl = V86_FLAGS; 97843561Skato v86.addr = 0x1b; 97943561Skato v86.eax = 0x8400 | od->od_unit; 98043561Skato v86int(); 98143561Skato 98243561Skato od->od_cyl = v86.ecx; 98343561Skato od->od_hds = (v86.edx >> 8) & 0xff; 98443561Skato od->od_sec = v86.edx & 0xff; 985292682Sjhb if (V86_CY(v86.efl)) 98651586Skato return(1); 98743561Skato } 98843561Skato 98943561Skato DEBUG("unit 0x%x geometry %d/%d/%d", od->od_unit, od->od_cyl, od->od_hds, od->od_sec); 99043561Skato return(0); 99143561Skato} 99243561Skato 99343561Skato/* 99453207Snyan * Return the BIOS geometry of a given "fixed drive" in a format 99553207Snyan * suitable for the legacy bootinfo structure. Since the kernel is 99653207Snyan * expecting raw int 0x13/0x8 values for N_BIOS_GEOM drives, we 99753207Snyan * prefer to get the information directly, rather than rely on being 99853207Snyan * able to put it together from information already maintained for 99953207Snyan * different purposes and for a probably different number of drives. 100053207Snyan * 100153207Snyan * For valid drives, the geometry is expected in the format (31..0) 100253207Snyan * "000000cc cccccccc hhhhhhhh 00ssssss"; and invalid drives are 100353207Snyan * indicated by returning the geometry of a "1.2M" PC-format floppy 100453207Snyan * disk. And, incidentally, what is returned is not the geometry as 100553207Snyan * such but the highest valid cylinder, head, and sector numbers. 100653207Snyan */ 100753207Snyanu_int32_t 100853207Snyanbd_getbigeom(int bunit) 100953207Snyan{ 101054819Snyan int hds = 0; 101155339Snyan int unit = 0x80; /* IDE HDD */ 101254819Snyan u_int addr = 0xA155d; 101355339Snyan 101454819Snyan while (unit < 0xa7) { 101555339Snyan if (*(u_char *)PTOV(addr) & (1 << (unit & 0x0f))) 101655339Snyan if (hds++ == bunit) 101754819Snyan break; 1018108791Snyan 1019108791Snyan if (unit >= 0xA0) { 1020108791Snyan int media = ((unsigned *)PTOV(0xA1460))[unit & 0x0F] & 0x1F; 1021108791Snyan 1022108791Snyan if (media == 7 && hds++ == bunit) /* SCSI MO */ 1023108791Snyan return(0xFFFE0820); /* C:65535 H:8 S:32 */ 1024108791Snyan } 102554819Snyan if (++unit == 0x84) { 1026108791Snyan unit = 0xA0; /* SCSI HDD */ 102754819Snyan addr = 0xA1482; 102854819Snyan } 102954819Snyan } 103054819Snyan if (unit == 0xa7) 1031108791Snyan return 0x4F020F; /* 1200KB FD C:80 H:2 S:15 */ 103253207Snyan v86.ctl = V86_FLAGS; 103354819Snyan v86.addr = 0x1b; 103454819Snyan v86.eax = 0x8400 | unit; 103554819Snyan v86int(); 1036292682Sjhb if (V86_CY(v86.efl)) 1037108791Snyan return 0x4F020F; /* 1200KB FD C:80 H:2 S:15 */ 103854819Snyan return ((v86.ecx & 0xffff) << 16) | (v86.edx & 0xffff); 103953207Snyan} 104053207Snyan 104153207Snyan/* 1042130603Sphk * Return a suitable dev_t value for (dev). 104343561Skato * 104443561Skato * In the case where it looks like (dev) is a SCSI disk, we allow the number of 104543561Skato * IDE disks to be specified in $num_ide_disks. There should be a Better Way. 104643561Skato */ 104743561Skatoint 104843561Skatobd_getdev(struct i386_devdesc *dev) 104943561Skato{ 105043561Skato struct open_disk *od; 105143561Skato int biosdev; 105243561Skato int major; 105343561Skato int rootdev; 105443561Skato char *nip, *cp; 105543561Skato int unitofs = 0, i, unit; 105643561Skato 1057163897Smarcel biosdev = bd_unit2bios(dev->d_unit); 1058163897Smarcel DEBUG("unit %d BIOS device %d", dev->d_unit, biosdev); 105943561Skato if (biosdev == -1) /* not a BIOS device */ 106043561Skato return(-1); 106143561Skato if (bd_opendisk(&od, dev) != 0) /* oops, not a viable device */ 106243561Skato return(-1); 106343561Skato 106443561Skato if ((biosdev & 0xf0) == 0x90 || (biosdev & 0xf0) == 0x30) { 106543561Skato /* floppy (or emulated floppy) or ATAPI device */ 1066163897Smarcel if (bdinfo[dev->d_unit].bd_type == DT_ATAPI) { 106743561Skato /* is an ATAPI disk */ 106843561Skato major = WFDMAJOR; 106943561Skato } else { 107043561Skato /* is a floppy disk */ 107143561Skato major = FDMAJOR; 107243561Skato } 107343561Skato } else { 107443561Skato /* harddisk */ 107543561Skato if ((od->od_flags & BD_LABELOK) && (od->od_disklabel.d_type == DTYPE_SCSI)) { 107643561Skato /* label OK, disk labelled as SCSI */ 107743561Skato major = DAMAJOR; 107843561Skato /* check for unit number correction hint, now deprecated */ 107943561Skato if ((nip = getenv("num_ide_disks")) != NULL) { 108043561Skato i = strtol(nip, &cp, 0); 108143561Skato /* check for parse error */ 108243561Skato if ((cp != nip) && (*cp == 0)) 108343561Skato unitofs = i; 108443561Skato } 108543561Skato } else { 108643561Skato /* assume an IDE disk */ 108743561Skato major = WDMAJOR; 108843561Skato } 108943561Skato } 109068358Snyan /* default root disk unit number */ 109168358Snyan if ((biosdev & 0xf0) == 0xa0) 1092163897Smarcel unit = bdinfo[dev->d_unit].bd_da_unit; 109368358Snyan else 109468358Snyan unit = biosdev & 0xf; 109568358Snyan 109643561Skato /* XXX a better kludge to set the root disk unit number */ 109743561Skato if ((nip = getenv("root_disk_unit")) != NULL) { 109843561Skato i = strtol(nip, &cp, 0); 109943561Skato /* check for parse error */ 110043561Skato if ((cp != nip) && (*cp == 0)) 110143561Skato unit = i; 110243561Skato } 110343561Skato 1104172921Sjhb rootdev = MAKEBOOTDEV(major, dev->d_kind.biosdisk.slice + 1, unit, 1105172921Sjhb dev->d_kind.biosdisk.partition); 110643561Skato DEBUG("dev is 0x%x\n", rootdev); 110743561Skato return(rootdev); 110843561Skato} 1109