biosdisk.c revision 200631
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 200631 2009-12-17 13:14:11Z nyan $"); 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#define MAXBDDEV MAXDEV 5643561Skato 5743561Skato#define DT_ATAPI 0x10 /* disk type for ATAPI floppies */ 5843561Skato#define WDMAJOR 0 /* major numbers for devices we frontend for */ 5943561Skato#define WFDMAJOR 1 6043561Skato#define FDMAJOR 2 6143561Skato#define DAMAJOR 4 6243561Skato 6343561Skato#ifdef DISK_DEBUG 6487599Sobrien# define DEBUG(fmt, args...) printf("%s: " fmt "\n" , __func__ , ## args) 6543561Skato#else 6643561Skato# define DEBUG(fmt, args...) 6743561Skato#endif 6843561Skato 6943561Skatostruct open_disk { 7043561Skato int od_dkunit; /* disk unit number */ 7143561Skato int od_unit; /* BIOS unit number */ 7243561Skato int od_cyl; /* BIOS geometry */ 7343561Skato int od_hds; 7443561Skato int od_sec; 7543561Skato int od_boff; /* block offset from beginning of BIOS disk */ 7643561Skato int od_flags; 7759777Snyan#define BD_MODEINT13 0x0000 7859777Snyan#define BD_MODEEDD1 0x0001 7959777Snyan#define BD_MODEEDD3 0x0002 8063101Snyan#define BD_MODEMASK 0x0003 8159777Snyan#define BD_FLOPPY 0x0004 8259777Snyan#define BD_LABELOK 0x0008 8359777Snyan#define BD_PARTTABOK 0x0010 84108791Snyan#define BD_OPTICAL 0x0020 8543561Skato struct disklabel od_disklabel; 8659777Snyan int od_nslices; /* slice count */ 87109638Snyan struct pc98_partition od_slicetab[NDOSPART]; 8843561Skato}; 8943561Skato 9043561Skato/* 9143561Skato * List of BIOS devices, translation from disk unit number to 9243561Skato * BIOS unit number. 9343561Skato */ 9443561Skatostatic struct bdinfo 9543561Skato{ 9643561Skato int bd_unit; /* BIOS unit number */ 9743561Skato int bd_flags; 9843561Skato int bd_type; /* BIOS 'drive type' (floppy only) */ 9953207Snyan int bd_da_unit; /* kernel unit number for da */ 10043561Skato} bdinfo [MAXBDDEV]; 10143561Skatostatic int nbdinfo = 0; 10243561Skato 10343561Skatostatic int bd_getgeom(struct open_disk *od); 10459777Snyanstatic int bd_read(struct open_disk *od, daddr_t dblk, int blks, 10559777Snyan caddr_t dest); 10687734Snyanstatic int bd_write(struct open_disk *od, daddr_t dblk, int blks, 10787734Snyan caddr_t dest); 10843561Skato 10943561Skatostatic int bd_int13probe(struct bdinfo *bd); 11043561Skato 111108650Snyanstatic void bd_printslice(struct open_disk *od, struct pc98_partition *dp, 11268358Snyan char *prefix, int verbose); 11368358Snyanstatic void bd_printbsdslice(struct open_disk *od, daddr_t offset, 11468358Snyan char *prefix, int verbose); 11543561Skato 11643561Skatostatic int bd_init(void); 11759777Snyanstatic int bd_strategy(void *devdata, int flag, daddr_t dblk, 11868358Snyan size_t size, char *buf, size_t *rsize); 11959777Snyanstatic int bd_realstrategy(void *devdata, int flag, daddr_t dblk, 12068358Snyan size_t size, char *buf, size_t *rsize); 12143561Skatostatic int bd_open(struct open_file *f, ...); 12243561Skatostatic int bd_close(struct open_file *f); 12343561Skatostatic void bd_print(int verbose); 12443561Skato 12543561Skatostruct devsw biosdisk = { 12643561Skato "disk", 12743561Skato DEVT_DISK, 12843561Skato bd_init, 12943561Skato bd_strategy, 13043561Skato bd_open, 13143561Skato bd_close, 13243561Skato noioctl, 13368358Snyan bd_print, 13468358Snyan NULL 13543561Skato}; 13643561Skato 13743561Skatostatic int bd_opendisk(struct open_disk **odp, struct i386_devdesc *dev); 13843561Skatostatic void bd_closedisk(struct open_disk *od); 139172968Snyanstatic int bd_open_pc98(struct open_disk *od, struct i386_devdesc *dev); 14059777Snyanstatic int bd_bestslice(struct open_disk *od); 14159777Snyanstatic void bd_checkextended(struct open_disk *od, int slicenum); 14243561Skato 14343561Skato/* 14443561Skato * Translate between BIOS device numbers and our private unit numbers. 14543561Skato */ 14643561Skatoint 14743561Skatobd_bios2unit(int biosdev) 14843561Skato{ 14943561Skato int i; 15043561Skato 15143561Skato DEBUG("looking for bios device 0x%x", biosdev); 15243561Skato for (i = 0; i < nbdinfo; i++) { 15343561Skato DEBUG("bd unit %d is BIOS device 0x%x", i, bdinfo[i].bd_unit); 15443561Skato if (bdinfo[i].bd_unit == biosdev) 15543561Skato return(i); 15643561Skato } 15743561Skato return(-1); 15843561Skato} 15943561Skato 16043561Skatoint 16143561Skatobd_unit2bios(int unit) 16243561Skato{ 16343561Skato if ((unit >= 0) && (unit < nbdinfo)) 16443561Skato return(bdinfo[unit].bd_unit); 16543561Skato return(-1); 16643561Skato} 16743561Skato 16843561Skato/* 16943561Skato * Quiz the BIOS for disk devices, save a little info about them. 17043561Skato */ 17143561Skatostatic int 17243561Skatobd_init(void) 17343561Skato{ 17443561Skato int base, unit; 17563101Snyan int da_drive=0, n=-0x10; 17643561Skato 17743561Skato /* sequence 0x90, 0x80, 0xa0 */ 17843561Skato for (base = 0x90; base <= 0xa0; base += n, n += 0x30) { 17943561Skato for (unit = base; (nbdinfo < MAXBDDEV) || ((unit & 0x0f) < 4); unit++) { 18043561Skato bdinfo[nbdinfo].bd_unit = unit; 18143561Skato bdinfo[nbdinfo].bd_flags = (unit & 0xf0) == 0x90 ? BD_FLOPPY : 0; 18243561Skato 18344463Skato if (!bd_int13probe(&bdinfo[nbdinfo])){ 18449425Skato if (((unit & 0xf0) == 0x90 && (unit & 0x0f) < 4) || 18549425Skato ((unit & 0xf0) == 0xa0 && (unit & 0x0f) < 6)) 18644463Skato continue; /* Target IDs are not contiguous. */ 18744463Skato else 18844463Skato break; 18944463Skato } 19043561Skato 19143561Skato if (bdinfo[nbdinfo].bd_flags & BD_FLOPPY){ 19244467Skato /* available 1.44MB access? */ 19359777Snyan if (*(u_char *)PTOV(0xA15AE) & (1<<(unit & 0xf))) { 19444467Skato /* boot media 1.2MB FD? */ 19544467Skato if ((*(u_char *)PTOV(0xA1584) & 0xf0) != 0x90) 19644467Skato bdinfo[nbdinfo].bd_unit = 0x30 + (unit & 0xf); 19744467Skato } 19843561Skato } 19949426Skato else { 200108791Snyan if ((unit & 0xF0) == 0xA0) /* SCSI HD or MO */ 20149426Skato bdinfo[nbdinfo].bd_da_unit = da_drive++; 20249426Skato } 20343561Skato /* XXX we need "disk aliases" to make this simpler */ 20443561Skato printf("BIOS drive %c: is disk%d\n", 20549426Skato 'A' + nbdinfo, nbdinfo); 20643561Skato nbdinfo++; 20743561Skato } 20843561Skato } 20943561Skato return(0); 21043561Skato} 21143561Skato 21243561Skato/* 21343561Skato * Try to detect a device supported by the legacy int13 BIOS 21443561Skato */ 21543561Skatostatic int 21643561Skatobd_int13probe(struct bdinfo *bd) 21743561Skato{ 21843561Skato int addr; 21963101Snyan 22059777Snyan if (bd->bd_flags & BD_FLOPPY) { 22143561Skato addr = 0xa155c; 22259777Snyan } else { 22343561Skato if ((bd->bd_unit & 0xf0) == 0x80) 22443561Skato addr = 0xa155d; 22543561Skato else 22643561Skato addr = 0xa1482; 22743561Skato } 22843561Skato if ( *(u_char *)PTOV(addr) & (1<<(bd->bd_unit & 0x0f))) { 22943561Skato bd->bd_flags |= BD_MODEINT13; 23043561Skato return(1); 23143561Skato } 232108791Snyan if ((bd->bd_unit & 0xF0) == 0xA0) { 233108791Snyan int media = ((unsigned *)PTOV(0xA1460))[bd->bd_unit & 0x0F] & 0x1F; 234108791Snyan 235108791Snyan if (media == 7) { /* MO */ 236108791Snyan bd->bd_flags |= BD_MODEINT13 | BD_OPTICAL; 237108791Snyan return(1); 238108791Snyan } 239108791Snyan } 24043561Skato return(0); 24143561Skato} 24243561Skato 24343561Skato/* 24443561Skato * Print information about disks 24543561Skato */ 24643561Skatostatic void 24743561Skatobd_print(int verbose) 24843561Skato{ 24943561Skato int i, j; 25043561Skato char line[80]; 25143561Skato struct i386_devdesc dev; 25243561Skato struct open_disk *od; 253108650Snyan struct pc98_partition *dptr; 25443561Skato 25543561Skato for (i = 0; i < nbdinfo; i++) { 25649426Skato sprintf(line, " disk%d: BIOS drive %c:\n", i, 'A' + i); 25743561Skato pager_output(line); 25843561Skato 25943561Skato /* try to open the whole disk */ 260163897Smarcel dev.d_unit = i; 26143561Skato dev.d_kind.biosdisk.slice = -1; 26243561Skato dev.d_kind.biosdisk.partition = -1; 26343561Skato 26443561Skato if (!bd_opendisk(&od, &dev)) { 26543561Skato 26643561Skato /* Do we have a partition table? */ 26743561Skato if (od->od_flags & BD_PARTTABOK) { 26859777Snyan dptr = &od->od_slicetab[0]; 26943561Skato 27068358Snyan /* Check for a "dedicated" disk */ 27159777Snyan for (j = 0; j < od->od_nslices; j++) { 272190127Snyan sprintf(line, " disk%ds%d", i, j + 1); 273190127Snyan bd_printslice(od, &dptr[j], line, verbose); 27443561Skato } 27543561Skato } 27643561Skato bd_closedisk(od); 27743561Skato } 27843561Skato } 27943561Skato} 28043561Skato 281176654Snyan/* Given a size in 512 byte sectors, convert it to a human-readable number. */ 282176654Snyanstatic char * 283176654Snyandisplay_size(uint64_t size) 284176654Snyan{ 285176654Snyan static char buf[80]; 286176654Snyan char unit; 287176654Snyan 288176654Snyan size /= 2; 289176654Snyan unit = 'K'; 290176654Snyan if (size >= 10485760000LL) { 291176654Snyan size /= 1073741824; 292176654Snyan unit = 'T'; 293176654Snyan } else if (size >= 10240000) { 294176654Snyan size /= 1048576; 295176654Snyan unit = 'G'; 296176654Snyan } else if (size >= 10000) { 297176654Snyan size /= 1024; 298176654Snyan unit = 'M'; 299176654Snyan } 300176654Snyan sprintf(buf, "%.6ld%cB", (long)size, unit); 301176654Snyan return (buf); 302176654Snyan} 303176654Snyan 30459777Snyan/* 305190127Snyan * Print information about slices on a disk. For the size calculations we 306190127Snyan * assume a 512 byte sector. 307190127Snyan */ 308190127Snyanstatic void 309190127Snyanbd_printslice(struct open_disk *od, struct pc98_partition *dp, char *prefix, 310190127Snyan int verbose) 311190127Snyan{ 312190127Snyan int cylsecs, start, size; 313190127Snyan char stats[80]; 314190127Snyan char line[80]; 315190127Snyan 316190127Snyan cylsecs = od->od_hds * od->od_sec; 317190127Snyan start = dp->dp_scyl * cylsecs + dp->dp_shd * od->od_sec + dp->dp_ssect; 318190127Snyan size = (dp->dp_ecyl - dp->dp_scyl + 1) * cylsecs; 319190127Snyan 320190127Snyan if (verbose) 321190127Snyan sprintf(stats, " %s (%d - %d)", display_size(size), 322190127Snyan start, start + size); 323190127Snyan else 324190127Snyan stats[0] = '\0'; 325190127Snyan 326190127Snyan switch(dp->dp_mid & PC98_MID_MASK) { 327190127Snyan case PC98_MID_386BSD: 328190127Snyan bd_printbsdslice(od, start, prefix, verbose); 329190127Snyan return; 330190127Snyan case 0x00: /* unused partition */ 331190127Snyan return; 332190127Snyan case 0x01: 333190127Snyan sprintf(line, "%s: FAT-12%s\n", prefix, stats); 334190127Snyan break; 335190127Snyan case 0x11: 336190127Snyan case 0x20: 337190127Snyan case 0x21: 338190127Snyan case 0x22: 339190127Snyan case 0x23: 340190127Snyan case 0x24: 341190127Snyan sprintf(line, "%s: FAT-16%s\n", prefix, stats); 342190127Snyan break; 343190127Snyan default: 344190127Snyan sprintf(line, "%s: Unknown fs: 0x%x %s\n", prefix, dp->dp_mid, 345190127Snyan stats); 346190127Snyan } 347190127Snyan pager_output(line); 348190127Snyan} 349190127Snyan 350190127Snyan/* 35168358Snyan * Print out each valid partition in the disklabel of a FreeBSD slice. 35268358Snyan * For size calculations, we assume a 512 byte sector size. 35368358Snyan */ 35459777Snyanstatic void 35568358Snyanbd_printbsdslice(struct open_disk *od, daddr_t offset, char *prefix, 35668358Snyan int verbose) 35759777Snyan{ 35843561Skato char line[80]; 35968358Snyan char buf[BIOSDISK_SECSIZE]; 36043561Skato struct disklabel *lp; 36143561Skato int i; 36243561Skato 36343561Skato /* read disklabel */ 36443561Skato if (bd_read(od, offset + LABELSECTOR, 1, buf)) 36543561Skato return; 36643561Skato lp =(struct disklabel *)(&buf[0]); 36743561Skato if (lp->d_magic != DISKMAGIC) { 36863101Snyan sprintf(line, "%s: FFS bad disklabel\n", prefix); 36943561Skato pager_output(line); 37043561Skato return; 37143561Skato } 37243561Skato 37343561Skato /* Print partitions */ 37443561Skato for (i = 0; i < lp->d_npartitions; i++) { 37568358Snyan /* 37668358Snyan * For each partition, make sure we know what type of fs it is. If 37768358Snyan * not, then skip it. However, since floppies often have bogus 37868358Snyan * fstypes, print the 'a' partition on a floppy even if it is marked 37968358Snyan * unused. 38068358Snyan */ 38163101Snyan if ((lp->d_partitions[i].p_fstype == FS_BSDFFS) || 38263101Snyan (lp->d_partitions[i].p_fstype == FS_SWAP) || 38363101Snyan (lp->d_partitions[i].p_fstype == FS_VINUM) || 38443561Skato ((lp->d_partitions[i].p_fstype == FS_UNUSED) && 38568358Snyan (od->od_flags & BD_FLOPPY) && (i == 0))) { 38668358Snyan 38768358Snyan /* Only print out statistics in verbose mode */ 38868358Snyan if (verbose) 389190127Snyan sprintf(line, " %s%c: %s %s (%d - %d)\n", prefix, 'a' + i, 390176654Snyan (lp->d_partitions[i].p_fstype == FS_SWAP) ? "swap " : 39168358Snyan (lp->d_partitions[i].p_fstype == FS_VINUM) ? "vinum" : 392176654Snyan "FFS ", 393176654Snyan display_size(lp->d_partitions[i].p_size), 39468358Snyan lp->d_partitions[i].p_offset, 39568358Snyan lp->d_partitions[i].p_offset + lp->d_partitions[i].p_size); 39668358Snyan else 39768358Snyan sprintf(line, " %s%c: %s\n", prefix, 'a' + i, 39868358Snyan (lp->d_partitions[i].p_fstype == FS_SWAP) ? "swap" : 39968358Snyan (lp->d_partitions[i].p_fstype == FS_VINUM) ? "vinum" : 40068358Snyan "FFS"); 40143561Skato pager_output(line); 40243561Skato } 40343561Skato } 40443561Skato} 40543561Skato 40643561Skato 40743561Skato/* 40843561Skato * Attempt to open the disk described by (dev) for use by (f). 40943561Skato * 41043561Skato * Note that the philosophy here is "give them exactly what 41143561Skato * they ask for". This is necessary because being too "smart" 41243561Skato * about what the user might want leads to complications. 41343561Skato * (eg. given no slice or partition value, with a disk that is 41443561Skato * sliced - are they after the first BSD slice, or the DOS 41543561Skato * slice before it?) 41643561Skato */ 41743561Skatostatic int 41843561Skatobd_open(struct open_file *f, ...) 41943561Skato{ 42043561Skato va_list ap; 42143561Skato struct i386_devdesc *dev; 42243561Skato struct open_disk *od; 42343561Skato int error; 42443561Skato 42543561Skato va_start(ap, f); 42643561Skato dev = va_arg(ap, struct i386_devdesc *); 42743561Skato va_end(ap); 42843561Skato if ((error = bd_opendisk(&od, dev))) 42943561Skato return(error); 43043561Skato 43143561Skato /* 43243561Skato * Save our context 43343561Skato */ 43443561Skato ((struct i386_devdesc *)(f->f_devdata))->d_kind.biosdisk.data = od; 43543561Skato DEBUG("open_disk %p, partition at 0x%x", od, od->od_boff); 43643561Skato return(0); 43743561Skato} 43843561Skato 43943561Skatostatic int 44043561Skatobd_opendisk(struct open_disk **odp, struct i386_devdesc *dev) 44143561Skato{ 44243561Skato struct open_disk *od; 44343561Skato int error; 44443561Skato 445163897Smarcel if (dev->d_unit >= nbdinfo) { 44643561Skato DEBUG("attempt to open nonexistent disk"); 44743561Skato return(ENXIO); 44843561Skato } 44943561Skato 45043561Skato od = (struct open_disk *)malloc(sizeof(struct open_disk)); 45143561Skato if (!od) { 45243561Skato DEBUG("no memory"); 45343561Skato return (ENOMEM); 45443561Skato } 45543561Skato 45643561Skato /* Look up BIOS unit number, intialise open_disk structure */ 457163897Smarcel od->od_dkunit = dev->d_unit; 45843561Skato od->od_unit = bdinfo[od->od_dkunit].bd_unit; 45943561Skato od->od_flags = bdinfo[od->od_dkunit].bd_flags; 46043561Skato od->od_boff = 0; 46143561Skato error = 0; 462172968Snyan DEBUG("open '%s', unit 0x%x slice %d partition %d", 463163897Smarcel i386_fmtdev(dev), dev->d_unit, 464172968Snyan dev->d_kind.biosdisk.slice, dev->d_kind.biosdisk.partition); 46543561Skato 46643561Skato /* Get geometry for this open (removable device may have changed) */ 46743561Skato if (bd_getgeom(od)) { 46843561Skato DEBUG("can't get geometry"); 46943561Skato error = ENXIO; 47043561Skato goto out; 47143561Skato } 47243561Skato 473172968Snyan /* Determine disk layout. */ 474172968Snyan error = bd_open_pc98(od, dev); 475172968Snyan 476172968Snyan out: 477172968Snyan if (error) { 478172968Snyan free(od); 479172968Snyan } else { 480172968Snyan *odp = od; /* return the open disk */ 481172968Snyan } 482172968Snyan return(error); 483172968Snyan} 484172968Snyan 485172968Snyanstatic int 486172968Snyanbd_open_pc98(struct open_disk *od, struct i386_devdesc *dev) 487172968Snyan{ 488172968Snyan struct pc98_partition *dptr; 489172968Snyan struct disklabel *lp; 490172968Snyan int sector, slice, i; 491172968Snyan char buf[BUFSIZE]; 492172968Snyan 49343561Skato /* 49443561Skato * Following calculations attempt to determine the correct value 49543561Skato * for d->od_boff by looking for the slice and partition specified, 49643561Skato * or searching for reasonable defaults. 49743561Skato */ 49843561Skato 49943561Skato /* 50043561Skato * Find the slice in the DOS slice table. 50143561Skato */ 502172968Snyan od->od_nslices = 0; 50343561Skato if (od->od_flags & BD_FLOPPY) { 50443561Skato sector = 0; 50543561Skato goto unsliced; 50643561Skato } 50743561Skato if (bd_read(od, 0, 1, buf)) { 50843561Skato DEBUG("error reading MBR"); 509172968Snyan return (EIO); 51043561Skato } 51143561Skato 51243561Skato /* 51343561Skato * Check the slice table magic. 51443561Skato */ 51568358Snyan if (((u_char)buf[0x1fe] != 0x55) || ((u_char)buf[0x1ff] != 0xaa)) { 51643561Skato /* If a slice number was explicitly supplied, this is an error */ 51743561Skato if (dev->d_kind.biosdisk.slice > 0) { 51843561Skato DEBUG("no slice table/MBR (no magic)"); 519172968Snyan return (ENOENT); 52043561Skato } 52143561Skato sector = 0; 52243561Skato goto unsliced; /* may be a floppy */ 52343561Skato } 52443561Skato if (bd_read(od, 1, 1, buf)) { 52543561Skato DEBUG("error reading MBR"); 526172968Snyan return (EIO); 52743561Skato } 52859777Snyan 52959777Snyan /* 53059777Snyan * copy the partition table, then pick up any extended partitions. 53159777Snyan */ 53259777Snyan bcopy(buf + DOSPARTOFF, &od->od_slicetab, 533108650Snyan sizeof(struct pc98_partition) * NDOSPART); 53459777Snyan od->od_nslices = NDOSPART; /* extended slices start here */ 53543561Skato od->od_flags |= BD_PARTTABOK; 53659777Snyan dptr = &od->od_slicetab[0]; 53743561Skato 53843561Skato /* Is this a request for the whole disk? */ 53943561Skato if (dev->d_kind.biosdisk.slice == -1) { 54043561Skato sector = 0; 54143561Skato goto unsliced; 54243561Skato } 54343561Skato 54459777Snyan /* 54559777Snyan * if a slice number was supplied but not found, this is an error. 54659777Snyan */ 54759777Snyan if (dev->d_kind.biosdisk.slice > 0) { 54859777Snyan slice = dev->d_kind.biosdisk.slice - 1; 54959777Snyan if (slice >= od->od_nslices) { 55059777Snyan DEBUG("slice %d not found", slice); 551172968Snyan return (ENOENT); 55259777Snyan } 55359777Snyan } 55443561Skato 55559777Snyan /* Try to auto-detect the best slice; this should always give a slice number */ 55659777Snyan if (dev->d_kind.biosdisk.slice == 0) { 55759777Snyan slice = bd_bestslice(od); 55859777Snyan if (slice == -1) { 559172968Snyan return (ENOENT); 56059777Snyan } 56159777Snyan dev->d_kind.biosdisk.slice = slice; 56259777Snyan } 56359777Snyan 56459777Snyan dptr = &od->od_slicetab[0]; 56543561Skato /* 56643561Skato * Accept the supplied slice number unequivocally (we may be looking 56743561Skato * at a DOS partition). 56843561Skato */ 56943561Skato dptr += (dev->d_kind.biosdisk.slice - 1); /* we number 1-4, offsets are 0-3 */ 57059777Snyan sector = dptr->dp_scyl * od->od_hds * od->od_sec + 57159777Snyan dptr->dp_shd * od->od_sec + dptr->dp_ssect; 57243561Skato { 57359777Snyan int end = dptr->dp_ecyl * od->od_hds * od->od_sec + 57459777Snyan dptr->dp_ehd * od->od_sec + dptr->dp_esect; 57559777Snyan DEBUG("slice entry %d at %d, %d sectors", 57659777Snyan dev->d_kind.biosdisk.slice - 1, sector, end-sector); 57743561Skato } 57843561Skato 57943561Skato /* 58043561Skato * If we are looking at a BSD slice, and the partition is < 0, assume the 'a' partition 58143561Skato */ 58243561Skato if ((dptr->dp_mid == DOSMID_386BSD) && (dev->d_kind.biosdisk.partition < 0)) 58343561Skato dev->d_kind.biosdisk.partition = 0; 58443561Skato 58543561Skato unsliced: 58643561Skato /* 58743561Skato * Now we have the slice offset, look for the partition in the disklabel if we have 58843561Skato * a partition to start with. 58943561Skato * 59043561Skato * XXX we might want to check the label checksum. 59143561Skato */ 59243561Skato if (dev->d_kind.biosdisk.partition < 0) { 59343561Skato od->od_boff = sector; /* no partition, must be after the slice */ 59443561Skato DEBUG("opening raw slice"); 59543561Skato } else { 59653207Snyan 59743561Skato if (bd_read(od, sector + LABELSECTOR, 1, buf)) { 59843561Skato DEBUG("error reading disklabel"); 599172968Snyan return (EIO); 60043561Skato } 60143561Skato DEBUG("copy %d bytes of label from %p to %p", sizeof(struct disklabel), buf + LABELOFFSET, &od->od_disklabel); 60243561Skato bcopy(buf + LABELOFFSET, &od->od_disklabel, sizeof(struct disklabel)); 60343561Skato lp = &od->od_disklabel; 60443561Skato od->od_flags |= BD_LABELOK; 60543561Skato 60643561Skato if (lp->d_magic != DISKMAGIC) { 60743561Skato DEBUG("no disklabel"); 608172968Snyan return (ENOENT); 60943561Skato } 61043561Skato if (dev->d_kind.biosdisk.partition >= lp->d_npartitions) { 61143561Skato DEBUG("partition '%c' exceeds partitions in table (a-'%c')", 61243561Skato 'a' + dev->d_kind.biosdisk.partition, 'a' + lp->d_npartitions); 613172968Snyan return (EPART); 61443561Skato } 61543561Skato 61668358Snyan#ifdef DISK_DEBUG 61768358Snyan /* Complain if the partition is unused unless this is a floppy. */ 61843561Skato if ((lp->d_partitions[dev->d_kind.biosdisk.partition].p_fstype == FS_UNUSED) && 61968358Snyan !(od->od_flags & BD_FLOPPY)) 62043561Skato DEBUG("warning, partition marked as unused"); 62168358Snyan#endif 62243561Skato 623146010Snyan od->od_boff = 624146010Snyan lp->d_partitions[dev->d_kind.biosdisk.partition].p_offset - 625146010Snyan lp->d_partitions[RAW_PART].p_offset + 626146010Snyan sector; 62743561Skato } 628172968Snyan return (0); 62943561Skato} 63043561Skato 63143561Skato/* 63243561Skato * Search for a slice with the following preferences: 63343561Skato * 63443561Skato * 1: Active FreeBSD slice 63543561Skato * 2: Non-active FreeBSD slice 63659777Snyan * 3: Active Linux slice 63759777Snyan * 4: non-active Linux slice 63859777Snyan * 5: Active FAT/FAT32 slice 63959777Snyan * 6: non-active FAT/FAT32 slice 64043561Skato */ 64159777Snyan#define PREF_RAWDISK 0 64259777Snyan#define PREF_FBSD_ACT 1 64359777Snyan#define PREF_FBSD 2 64459777Snyan#define PREF_LINUX_ACT 3 64559777Snyan#define PREF_LINUX 4 64659777Snyan#define PREF_DOS_ACT 5 64759777Snyan#define PREF_DOS 6 64859777Snyan#define PREF_NONE 7 64943561Skato 65059777Snyan/* 65159777Snyan * slicelimit is in the range 0 .. NDOSPART 65259777Snyan */ 65343561Skatostatic int 65459777Snyanbd_bestslice(struct open_disk *od) 65543561Skato{ 656108650Snyan struct pc98_partition *dp; 65759777Snyan int pref, preflevel; 65859777Snyan int i, prefslice; 65953207Snyan 66059777Snyan prefslice = 0; 66159777Snyan preflevel = PREF_NONE; 66243561Skato 66359777Snyan dp = &od->od_slicetab[0]; 66459777Snyan for (i = 0; i < od->od_nslices; i++, dp++) { 665190029Snyan switch(dp->dp_mid & PC98_MID_MASK) { 666190029Snyan case PC98_MID_386BSD: /* FreeBSD */ 667190029Snyan if ((dp->dp_mid & PC98_MID_BOOTABLE) && 66859777Snyan (preflevel > PREF_FBSD_ACT)) { 66959777Snyan pref = i; 67059777Snyan preflevel = PREF_FBSD_ACT; 67159777Snyan } else if (preflevel > PREF_FBSD) { 67259777Snyan pref = i; 67359777Snyan preflevel = PREF_FBSD; 67459777Snyan } 67559777Snyan break; 67659777Snyan 67759777Snyan case 0x11: /* DOS/Windows */ 67859777Snyan case 0x20: 67959777Snyan case 0x21: 68059777Snyan case 0x22: 68159777Snyan case 0x23: 68259777Snyan case 0x63: 683190029Snyan if ((dp->dp_mid & PC98_MID_BOOTABLE) && 68459777Snyan (preflevel > PREF_DOS_ACT)) { 68559777Snyan pref = i; 68659777Snyan preflevel = PREF_DOS_ACT; 68759777Snyan } else if (preflevel > PREF_DOS) { 68859777Snyan pref = i; 68959777Snyan preflevel = PREF_DOS; 69059777Snyan } 69159777Snyan break; 69259777Snyan } 69343561Skato } 69459777Snyan return (prefslice); 69543561Skato} 69687734Snyan 69743561Skatostatic int 69843561Skatobd_close(struct open_file *f) 69943561Skato{ 70043561Skato struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)(f->f_devdata))->d_kind.biosdisk.data); 70143561Skato 70243561Skato bd_closedisk(od); 70343561Skato return(0); 70443561Skato} 70543561Skato 70643561Skatostatic void 70743561Skatobd_closedisk(struct open_disk *od) 70843561Skato{ 70943561Skato DEBUG("open_disk %p", od); 71043561Skato#if 0 71143561Skato /* XXX is this required? (especially if disk already open...) */ 71243561Skato if (od->od_flags & BD_FLOPPY) 71343561Skato delay(3000000); 71443561Skato#endif 71543561Skato free(od); 71643561Skato} 71743561Skato 71843561Skatostatic int 71968358Snyanbd_strategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize) 72043561Skato{ 72143561Skato struct bcache_devdata bcd; 72258165Snyan struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)devdata)->d_kind.biosdisk.data); 72358165Snyan 72443561Skato bcd.dv_strategy = bd_realstrategy; 72543561Skato bcd.dv_devdata = devdata; 72658165Snyan return(bcache_strategy(&bcd, od->od_unit, rw, dblk+od->od_boff, size, buf, rsize)); 72743561Skato} 72843561Skato 72943561Skatostatic int 73068358Snyanbd_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize) 73143561Skato{ 73243561Skato struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)devdata)->d_kind.biosdisk.data); 73343561Skato int blks; 73443561Skato#ifdef BD_SUPPORT_FRAGS 73543561Skato char fragbuf[BIOSDISK_SECSIZE]; 73643561Skato size_t fragsize; 73743561Skato 73843561Skato fragsize = size % BIOSDISK_SECSIZE; 73943561Skato#else 74043561Skato if (size % BIOSDISK_SECSIZE) 74143561Skato panic("bd_strategy: %d bytes I/O not multiple of block size", size); 74243561Skato#endif 74343561Skato 74443561Skato DEBUG("open_disk %p", od); 74543561Skato blks = size / BIOSDISK_SECSIZE; 74643561Skato if (rsize) 74743561Skato *rsize = 0; 748172925Snyan 749172925Snyan switch(rw){ 750172925Snyan case F_READ: 751172925Snyan DEBUG("read %d from %d to %p", blks, dblk, buf); 752172925Snyan 753172925Snyan if (blks && bd_read(od, dblk, blks, buf)) { 754172925Snyan DEBUG("read error"); 755172925Snyan return (EIO); 756172925Snyan } 75743561Skato#ifdef BD_SUPPORT_FRAGS 758172925Snyan DEBUG("bd_strategy: frag read %d from %d+%d to %p", 759172925Snyan fragsize, dblk, blks, buf + (blks * BIOSDISK_SECSIZE)); 760172925Snyan if (fragsize && bd_read(od, dblk + blks, 1, fragsize)) { 761172925Snyan DEBUG("frag read error"); 762172925Snyan return(EIO); 763172925Snyan } 764172925Snyan bcopy(fragbuf, buf + (blks * BIOSDISK_SECSIZE), fragsize); 76543561Skato#endif 766172925Snyan break; 767172925Snyan case F_WRITE : 768172925Snyan DEBUG("write %d from %d to %p", blks, dblk, buf); 76987734Snyan 770172925Snyan if (blks && bd_write(od, dblk, blks, buf)) { 771172925Snyan DEBUG("write error"); 772172925Snyan return (EIO); 773172925Snyan } 77487734Snyan#ifdef BD_SUPPORT_FRAGS 77587734Snyan if(fragsize) { 776172925Snyan DEBUG("Attempted to write a frag"); 777172925Snyan return (EIO); 77887734Snyan } 77987734Snyan#endif 780172925Snyan break; 781172925Snyan default: 782172925Snyan /* DO NOTHING */ 783172925Snyan return (EROFS); 784172925Snyan } 78587734Snyan 78687734Snyan if (rsize) 78787734Snyan *rsize = size; 78887734Snyan return (0); 78943561Skato} 79043561Skato 79143561Skato/* Max number of sectors to bounce-buffer if the request crosses a 64k boundary */ 79243561Skato#define FLOPPY_BOUNCEBUF 18 79343561Skato 79443561Skatostatic int 795172965Snyanbd_chs_io(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest, int write) 79643561Skato{ 797172965Snyan u_int x, bpc, cyl, hd, sec; 798172965Snyan 799172965Snyan bpc = (od->od_sec * od->od_hds); /* blocks per cylinder */ 800172965Snyan x = dblk; 801172965Snyan cyl = x / bpc; /* block # / blocks per cylinder */ 802172965Snyan x %= bpc; /* block offset into cylinder */ 803172965Snyan hd = x / od->od_sec; /* offset / blocks per track */ 804172965Snyan sec = x % od->od_sec; /* offset into track */ 805172965Snyan 806172965Snyan v86.ctl = V86_FLAGS; 807172965Snyan v86.addr = 0x1b; 808172965Snyan if (write) 809172965Snyan v86.eax = 0x0500 | od->od_unit; 810172965Snyan else 811172965Snyan v86.eax = 0x0600 | od->od_unit; 812172965Snyan if (od->od_flags & BD_FLOPPY) { 813172965Snyan v86.eax |= 0xd000; 814172965Snyan v86.ecx = 0x0200 | (cyl & 0xff); 815172965Snyan v86.edx = (hd << 8) | (sec + 1); 816172965Snyan } else if (od->od_flags & BD_OPTICAL) { 817172965Snyan v86.eax &= 0xFF7F; 818172965Snyan v86.ecx = dblk & 0xFFFF; 819172965Snyan v86.edx = dblk >> 16; 820172965Snyan } else { 821172965Snyan v86.ecx = cyl; 822172965Snyan v86.edx = (hd << 8) | sec; 823172965Snyan } 824172965Snyan v86.ebx = blks * BIOSDISK_SECSIZE; 825172965Snyan v86.es = VTOPSEG(dest); 826172965Snyan v86.ebp = VTOPOFF(dest); 827172965Snyan v86int(); 828172965Snyan return (v86.efl & 0x1); 829172965Snyan} 830172965Snyan 831172965Snyanstatic int 832172965Snyanbd_io(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest, int write) 833172965Snyan{ 834172965Snyan u_int x, sec, result, resid, retry, maxfer; 83543561Skato caddr_t p, xp, bbuf, breg; 83643561Skato 837172965Snyan /* Just in case some idiot actually tries to read/write -1 blocks... */ 83868358Snyan if (blks < 0) 83968358Snyan return (-1); 84068358Snyan 84143561Skato resid = blks; 84243561Skato p = dest; 84343561Skato 84443561Skato /* Decide whether we have to bounce */ 845153598Snyan if (VTOP(dest) >> 20 != 0 || 846153598Snyan ((VTOP(dest) >> 16) != (VTOP(dest + blks * BIOSDISK_SECSIZE) >> 16))) { 84743561Skato 84843561Skato /* 849172965Snyan * There is a 64k physical boundary somewhere in the 850172965Snyan * destination buffer, or the destination buffer is above 851172965Snyan * first 1MB of physical memory so we have to arrange a 852172965Snyan * suitable bounce buffer. Allocate a buffer twice as large 853172965Snyan * as we need to. Use the bottom half unless there is a break 854172965Snyan * there, in which case we use the top half. 85543561Skato */ 85668358Snyan x = min(od->od_sec, (unsigned)blks); 857153598Snyan bbuf = alloca(x * 2 * BIOSDISK_SECSIZE); 858172965Snyan if (((u_int32_t)VTOP(bbuf) & 0xffff0000) == 859172965Snyan ((u_int32_t)VTOP(bbuf + x * BIOSDISK_SECSIZE) & 0xffff0000)) { 86043561Skato breg = bbuf; 86143561Skato } else { 86243561Skato breg = bbuf + x * BIOSDISK_SECSIZE; 86343561Skato } 864172965Snyan maxfer = x; /* limit transfers to bounce region size */ 86543561Skato } else { 86668358Snyan breg = bbuf = NULL; 86743561Skato maxfer = 0; 86843561Skato } 86943561Skato 87043561Skato while (resid > 0) { 871172965Snyan /* 872172965Snyan * Play it safe and don't cross track boundaries. 873172965Snyan * (XXX this is probably unnecessary) 874172965Snyan */ 875172965Snyan sec = dblk % od->od_sec; /* offset into track */ 87643561Skato x = min(od->od_sec - sec, resid); 87743561Skato if (maxfer > 0) 87843561Skato x = min(x, maxfer); /* fit bounce buffer */ 87943561Skato 88043561Skato /* where do we transfer to? */ 88143561Skato xp = bbuf == NULL ? p : breg; 88243561Skato 883172965Snyan /* 884172965Snyan * Put your Data In, Put your Data out, 885172965Snyan * Put your Data In, and shake it all about 886172965Snyan */ 887172965Snyan if (write && bbuf != NULL) 888172965Snyan bcopy(p, breg, x * BIOSDISK_SECSIZE); 88943561Skato 890172965Snyan /* 891172965Snyan * Loop retrying the operation a couple of times. The BIOS 892172965Snyan * may also retry. 893172965Snyan */ 89443561Skato for (retry = 0; retry < 3; retry++) { 89543561Skato /* if retrying, reset the drive */ 89643561Skato if (retry > 0) { 89751586Skato v86.ctl = V86_FLAGS; 89851586Skato v86.addr = 0x1b; 89951586Skato v86.eax = 0x0300 | od->od_unit; 90043561Skato v86int(); 90143561Skato } 902172965Snyan 903172965Snyan result = bd_chs_io(od, dblk, x, xp, write); 90443561Skato if (result == 0) 90543561Skato break; 90643561Skato } 907172965Snyan 908172965Snyan if (write) 909200631Snyan DEBUG("Write %d sector(s) from %p (0x%x) to %lld %s", x, 910200631Snyan p, VTOP(p), dblk, result ? "failed" : "ok"); 911172965Snyan else 912200631Snyan DEBUG("Read %d sector(s) from %lld to %p (0x%x) %s", x, 913200631Snyan dblk, p, VTOP(p), result ? "failed" : "ok"); 91443561Skato if (result) { 91543561Skato return(-1); 91643561Skato } 917172965Snyan if (!write && bbuf != NULL) 91843561Skato bcopy(breg, p, x * BIOSDISK_SECSIZE); 91943561Skato p += (x * BIOSDISK_SECSIZE); 92043561Skato dblk += x; 92143561Skato resid -= x; 92243561Skato } 923172965Snyan 92443561Skato/* hexdump(dest, (blks * BIOSDISK_SECSIZE)); */ 92543561Skato return(0); 92643561Skato} 92743561Skato 928172965Snyanstatic int 929172965Snyanbd_read(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest) 930172965Snyan{ 93187734Snyan 932172965Snyan return (bd_io(od, dblk, blks, dest, 0)); 933172965Snyan} 934172965Snyan 93543561Skatostatic int 93687734Snyanbd_write(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest) 93787734Snyan{ 93887734Snyan 939172965Snyan return (bd_io(od, dblk, blks, dest, 1)); 940172965Snyan} 94187734Snyan 94287734Snyanstatic int 94343561Skatobd_getgeom(struct open_disk *od) 94443561Skato{ 94543561Skato 94643561Skato if (od->od_flags & BD_FLOPPY) { 94754819Snyan od->od_cyl = 79; 94843561Skato od->od_hds = 2; 94943561Skato od->od_sec = (od->od_unit & 0xf0) == 0x30 ? 18 : 15; 950108791Snyan } else if (od->od_flags & BD_OPTICAL) { 951108791Snyan od->od_cyl = 0xFFFE; 952108791Snyan od->od_hds = 8; 953108791Snyan od->od_sec = 32; 95459777Snyan } else { 95554819Snyan v86.ctl = V86_FLAGS; 95643561Skato v86.addr = 0x1b; 95743561Skato v86.eax = 0x8400 | od->od_unit; 95843561Skato v86int(); 95943561Skato 96043561Skato od->od_cyl = v86.ecx; 96143561Skato od->od_hds = (v86.edx >> 8) & 0xff; 96243561Skato od->od_sec = v86.edx & 0xff; 96351586Skato if (v86.efl & 0x1) 96451586Skato return(1); 96543561Skato } 96643561Skato 96743561Skato DEBUG("unit 0x%x geometry %d/%d/%d", od->od_unit, od->od_cyl, od->od_hds, od->od_sec); 96843561Skato return(0); 96943561Skato} 97043561Skato 97143561Skato/* 97253207Snyan * Return the BIOS geometry of a given "fixed drive" in a format 97353207Snyan * suitable for the legacy bootinfo structure. Since the kernel is 97453207Snyan * expecting raw int 0x13/0x8 values for N_BIOS_GEOM drives, we 97553207Snyan * prefer to get the information directly, rather than rely on being 97653207Snyan * able to put it together from information already maintained for 97753207Snyan * different purposes and for a probably different number of drives. 97853207Snyan * 97953207Snyan * For valid drives, the geometry is expected in the format (31..0) 98053207Snyan * "000000cc cccccccc hhhhhhhh 00ssssss"; and invalid drives are 98153207Snyan * indicated by returning the geometry of a "1.2M" PC-format floppy 98253207Snyan * disk. And, incidentally, what is returned is not the geometry as 98353207Snyan * such but the highest valid cylinder, head, and sector numbers. 98453207Snyan */ 98553207Snyanu_int32_t 98653207Snyanbd_getbigeom(int bunit) 98753207Snyan{ 98854819Snyan int hds = 0; 98955339Snyan int unit = 0x80; /* IDE HDD */ 99054819Snyan u_int addr = 0xA155d; 99155339Snyan 99254819Snyan while (unit < 0xa7) { 99355339Snyan if (*(u_char *)PTOV(addr) & (1 << (unit & 0x0f))) 99455339Snyan if (hds++ == bunit) 99554819Snyan break; 996108791Snyan 997108791Snyan if (unit >= 0xA0) { 998108791Snyan int media = ((unsigned *)PTOV(0xA1460))[unit & 0x0F] & 0x1F; 999108791Snyan 1000108791Snyan if (media == 7 && hds++ == bunit) /* SCSI MO */ 1001108791Snyan return(0xFFFE0820); /* C:65535 H:8 S:32 */ 1002108791Snyan } 100354819Snyan if (++unit == 0x84) { 1004108791Snyan unit = 0xA0; /* SCSI HDD */ 100554819Snyan addr = 0xA1482; 100654819Snyan } 100754819Snyan } 100854819Snyan if (unit == 0xa7) 1009108791Snyan return 0x4F020F; /* 1200KB FD C:80 H:2 S:15 */ 101053207Snyan v86.ctl = V86_FLAGS; 101154819Snyan v86.addr = 0x1b; 101254819Snyan v86.eax = 0x8400 | unit; 101354819Snyan v86int(); 101454819Snyan if (v86.efl & 0x1) 1015108791Snyan return 0x4F020F; /* 1200KB FD C:80 H:2 S:15 */ 101654819Snyan return ((v86.ecx & 0xffff) << 16) | (v86.edx & 0xffff); 101753207Snyan} 101853207Snyan 101953207Snyan/* 1020130603Sphk * Return a suitable dev_t value for (dev). 102143561Skato * 102243561Skato * In the case where it looks like (dev) is a SCSI disk, we allow the number of 102343561Skato * IDE disks to be specified in $num_ide_disks. There should be a Better Way. 102443561Skato */ 102543561Skatoint 102643561Skatobd_getdev(struct i386_devdesc *dev) 102743561Skato{ 102843561Skato struct open_disk *od; 102943561Skato int biosdev; 103043561Skato int major; 103143561Skato int rootdev; 103243561Skato char *nip, *cp; 103343561Skato int unitofs = 0, i, unit; 103443561Skato 1035163897Smarcel biosdev = bd_unit2bios(dev->d_unit); 1036163897Smarcel DEBUG("unit %d BIOS device %d", dev->d_unit, biosdev); 103743561Skato if (biosdev == -1) /* not a BIOS device */ 103843561Skato return(-1); 103943561Skato if (bd_opendisk(&od, dev) != 0) /* oops, not a viable device */ 104043561Skato return(-1); 104143561Skato 104243561Skato if ((biosdev & 0xf0) == 0x90 || (biosdev & 0xf0) == 0x30) { 104343561Skato /* floppy (or emulated floppy) or ATAPI device */ 1044163897Smarcel if (bdinfo[dev->d_unit].bd_type == DT_ATAPI) { 104543561Skato /* is an ATAPI disk */ 104643561Skato major = WFDMAJOR; 104743561Skato } else { 104843561Skato /* is a floppy disk */ 104943561Skato major = FDMAJOR; 105043561Skato } 105143561Skato } else { 105243561Skato /* harddisk */ 105343561Skato if ((od->od_flags & BD_LABELOK) && (od->od_disklabel.d_type == DTYPE_SCSI)) { 105443561Skato /* label OK, disk labelled as SCSI */ 105543561Skato major = DAMAJOR; 105643561Skato /* check for unit number correction hint, now deprecated */ 105743561Skato if ((nip = getenv("num_ide_disks")) != NULL) { 105843561Skato i = strtol(nip, &cp, 0); 105943561Skato /* check for parse error */ 106043561Skato if ((cp != nip) && (*cp == 0)) 106143561Skato unitofs = i; 106243561Skato } 106343561Skato } else { 106443561Skato /* assume an IDE disk */ 106543561Skato major = WDMAJOR; 106643561Skato } 106743561Skato } 106868358Snyan /* default root disk unit number */ 106968358Snyan if ((biosdev & 0xf0) == 0xa0) 1070163897Smarcel unit = bdinfo[dev->d_unit].bd_da_unit; 107168358Snyan else 107268358Snyan unit = biosdev & 0xf; 107368358Snyan 107443561Skato /* XXX a better kludge to set the root disk unit number */ 107543561Skato if ((nip = getenv("root_disk_unit")) != NULL) { 107643561Skato i = strtol(nip, &cp, 0); 107743561Skato /* check for parse error */ 107843561Skato if ((cp != nip) && (*cp == 0)) 107943561Skato unit = i; 108043561Skato } 108143561Skato 1082172921Sjhb rootdev = MAKEBOOTDEV(major, dev->d_kind.biosdisk.slice + 1, unit, 1083172921Sjhb dev->d_kind.biosdisk.partition); 108443561Skato DEBUG("dev is 0x%x\n", rootdev); 108543561Skato return(rootdev); 108643561Skato} 1087