biosdisk.c revision 172965
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 172965 2007-10-25 12:57:46Z 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); 13959777Snyanstatic int bd_bestslice(struct open_disk *od); 14059777Snyanstatic void bd_checkextended(struct open_disk *od, int slicenum); 14143561Skato 14243561Skato/* 14343561Skato * Translate between BIOS device numbers and our private unit numbers. 14443561Skato */ 14543561Skatoint 14643561Skatobd_bios2unit(int biosdev) 14743561Skato{ 14843561Skato int i; 14943561Skato 15043561Skato DEBUG("looking for bios device 0x%x", biosdev); 15143561Skato for (i = 0; i < nbdinfo; i++) { 15243561Skato DEBUG("bd unit %d is BIOS device 0x%x", i, bdinfo[i].bd_unit); 15343561Skato if (bdinfo[i].bd_unit == biosdev) 15443561Skato return(i); 15543561Skato } 15643561Skato return(-1); 15743561Skato} 15843561Skato 15943561Skatoint 16043561Skatobd_unit2bios(int unit) 16143561Skato{ 16243561Skato if ((unit >= 0) && (unit < nbdinfo)) 16343561Skato return(bdinfo[unit].bd_unit); 16443561Skato return(-1); 16543561Skato} 16643561Skato 16743561Skato/* 16843561Skato * Quiz the BIOS for disk devices, save a little info about them. 16943561Skato */ 17043561Skatostatic int 17143561Skatobd_init(void) 17243561Skato{ 17343561Skato int base, unit; 17463101Snyan int da_drive=0, n=-0x10; 17543561Skato 17643561Skato /* sequence 0x90, 0x80, 0xa0 */ 17743561Skato for (base = 0x90; base <= 0xa0; base += n, n += 0x30) { 17843561Skato for (unit = base; (nbdinfo < MAXBDDEV) || ((unit & 0x0f) < 4); unit++) { 17943561Skato bdinfo[nbdinfo].bd_unit = unit; 18043561Skato bdinfo[nbdinfo].bd_flags = (unit & 0xf0) == 0x90 ? BD_FLOPPY : 0; 18143561Skato 18244463Skato if (!bd_int13probe(&bdinfo[nbdinfo])){ 18349425Skato if (((unit & 0xf0) == 0x90 && (unit & 0x0f) < 4) || 18449425Skato ((unit & 0xf0) == 0xa0 && (unit & 0x0f) < 6)) 18544463Skato continue; /* Target IDs are not contiguous. */ 18644463Skato else 18744463Skato break; 18844463Skato } 18943561Skato 19043561Skato if (bdinfo[nbdinfo].bd_flags & BD_FLOPPY){ 19144467Skato /* available 1.44MB access? */ 19259777Snyan if (*(u_char *)PTOV(0xA15AE) & (1<<(unit & 0xf))) { 19344467Skato /* boot media 1.2MB FD? */ 19444467Skato if ((*(u_char *)PTOV(0xA1584) & 0xf0) != 0x90) 19544467Skato bdinfo[nbdinfo].bd_unit = 0x30 + (unit & 0xf); 19644467Skato } 19743561Skato } 19849426Skato else { 199108791Snyan if ((unit & 0xF0) == 0xA0) /* SCSI HD or MO */ 20049426Skato bdinfo[nbdinfo].bd_da_unit = da_drive++; 20149426Skato } 20243561Skato /* XXX we need "disk aliases" to make this simpler */ 20343561Skato printf("BIOS drive %c: is disk%d\n", 20449426Skato 'A' + nbdinfo, nbdinfo); 20543561Skato nbdinfo++; 20643561Skato } 20743561Skato } 20843561Skato return(0); 20943561Skato} 21043561Skato 21143561Skato/* 21243561Skato * Try to detect a device supported by the legacy int13 BIOS 21343561Skato */ 21443561Skatostatic int 21543561Skatobd_int13probe(struct bdinfo *bd) 21643561Skato{ 21743561Skato int addr; 21863101Snyan 21959777Snyan if (bd->bd_flags & BD_FLOPPY) { 22043561Skato addr = 0xa155c; 22159777Snyan } else { 22243561Skato if ((bd->bd_unit & 0xf0) == 0x80) 22343561Skato addr = 0xa155d; 22443561Skato else 22543561Skato addr = 0xa1482; 22643561Skato } 22743561Skato if ( *(u_char *)PTOV(addr) & (1<<(bd->bd_unit & 0x0f))) { 22843561Skato bd->bd_flags |= BD_MODEINT13; 22943561Skato return(1); 23043561Skato } 231108791Snyan if ((bd->bd_unit & 0xF0) == 0xA0) { 232108791Snyan int media = ((unsigned *)PTOV(0xA1460))[bd->bd_unit & 0x0F] & 0x1F; 233108791Snyan 234108791Snyan if (media == 7) { /* MO */ 235108791Snyan bd->bd_flags |= BD_MODEINT13 | BD_OPTICAL; 236108791Snyan return(1); 237108791Snyan } 238108791Snyan } 23943561Skato return(0); 24043561Skato} 24143561Skato 24243561Skato/* 24343561Skato * Print information about disks 24443561Skato */ 24543561Skatostatic void 24643561Skatobd_print(int verbose) 24743561Skato{ 24843561Skato int i, j; 24943561Skato char line[80]; 25043561Skato struct i386_devdesc dev; 25143561Skato struct open_disk *od; 252108650Snyan struct pc98_partition *dptr; 25343561Skato 25443561Skato for (i = 0; i < nbdinfo; i++) { 25549426Skato sprintf(line, " disk%d: BIOS drive %c:\n", i, 'A' + i); 25643561Skato pager_output(line); 25743561Skato 25843561Skato /* try to open the whole disk */ 259163897Smarcel dev.d_unit = i; 26043561Skato dev.d_kind.biosdisk.slice = -1; 26143561Skato dev.d_kind.biosdisk.partition = -1; 26243561Skato 26343561Skato if (!bd_opendisk(&od, &dev)) { 26443561Skato 26543561Skato /* Do we have a partition table? */ 26643561Skato if (od->od_flags & BD_PARTTABOK) { 26759777Snyan dptr = &od->od_slicetab[0]; 26843561Skato 26968358Snyan /* Check for a "dedicated" disk */ 27059777Snyan for (j = 0; j < od->od_nslices; j++) { 27143561Skato switch(dptr[j].dp_mid) { 27243561Skato case DOSMID_386BSD: 27343561Skato sprintf(line, " disk%ds%d", i, j + 1); 27459777Snyan bd_printbsdslice(od, 27559777Snyan dptr[j].dp_scyl * od->od_hds * od->od_sec + 27659777Snyan dptr[j].dp_shd * od->od_sec + dptr[j].dp_ssect, 27768358Snyan line, verbose); 27843561Skato break; 27943561Skato default: 280130601Sphk break; 28143561Skato } 28243561Skato } 28343561Skato } 28443561Skato bd_closedisk(od); 28543561Skato } 28643561Skato } 28743561Skato} 28843561Skato 28959777Snyan/* 29068358Snyan * Print out each valid partition in the disklabel of a FreeBSD slice. 29168358Snyan * For size calculations, we assume a 512 byte sector size. 29268358Snyan */ 29359777Snyanstatic void 29468358Snyanbd_printbsdslice(struct open_disk *od, daddr_t offset, char *prefix, 29568358Snyan int verbose) 29659777Snyan{ 29743561Skato char line[80]; 29868358Snyan char buf[BIOSDISK_SECSIZE]; 29943561Skato struct disklabel *lp; 30043561Skato int i; 30143561Skato 30243561Skato /* read disklabel */ 30343561Skato if (bd_read(od, offset + LABELSECTOR, 1, buf)) 30443561Skato return; 30543561Skato lp =(struct disklabel *)(&buf[0]); 30643561Skato if (lp->d_magic != DISKMAGIC) { 30763101Snyan sprintf(line, "%s: FFS bad disklabel\n", prefix); 30843561Skato pager_output(line); 30943561Skato return; 31043561Skato } 31143561Skato 31243561Skato /* Print partitions */ 31343561Skato for (i = 0; i < lp->d_npartitions; i++) { 31468358Snyan /* 31568358Snyan * For each partition, make sure we know what type of fs it is. If 31668358Snyan * not, then skip it. However, since floppies often have bogus 31768358Snyan * fstypes, print the 'a' partition on a floppy even if it is marked 31868358Snyan * unused. 31968358Snyan */ 32063101Snyan if ((lp->d_partitions[i].p_fstype == FS_BSDFFS) || 32163101Snyan (lp->d_partitions[i].p_fstype == FS_SWAP) || 32263101Snyan (lp->d_partitions[i].p_fstype == FS_VINUM) || 32343561Skato ((lp->d_partitions[i].p_fstype == FS_UNUSED) && 32468358Snyan (od->od_flags & BD_FLOPPY) && (i == 0))) { 32568358Snyan 32668358Snyan /* Only print out statistics in verbose mode */ 32768358Snyan if (verbose) 32868358Snyan sprintf(line, " %s%c: %s %.6dMB (%d - %d)\n", prefix, 'a' + i, 32963101Snyan (lp->d_partitions[i].p_fstype == FS_SWAP) ? "swap" : 33068358Snyan (lp->d_partitions[i].p_fstype == FS_VINUM) ? "vinum" : 33168358Snyan "FFS", 33268358Snyan lp->d_partitions[i].p_size / 2048, 33368358Snyan lp->d_partitions[i].p_offset, 33468358Snyan lp->d_partitions[i].p_offset + lp->d_partitions[i].p_size); 33568358Snyan else 33668358Snyan sprintf(line, " %s%c: %s\n", prefix, 'a' + i, 33768358Snyan (lp->d_partitions[i].p_fstype == FS_SWAP) ? "swap" : 33868358Snyan (lp->d_partitions[i].p_fstype == FS_VINUM) ? "vinum" : 33968358Snyan "FFS"); 34043561Skato pager_output(line); 34143561Skato } 34243561Skato } 34343561Skato} 34443561Skato 34543561Skato 34643561Skato/* 34743561Skato * Attempt to open the disk described by (dev) for use by (f). 34843561Skato * 34943561Skato * Note that the philosophy here is "give them exactly what 35043561Skato * they ask for". This is necessary because being too "smart" 35143561Skato * about what the user might want leads to complications. 35243561Skato * (eg. given no slice or partition value, with a disk that is 35343561Skato * sliced - are they after the first BSD slice, or the DOS 35443561Skato * slice before it?) 35543561Skato */ 35643561Skatostatic int 35743561Skatobd_open(struct open_file *f, ...) 35843561Skato{ 35943561Skato va_list ap; 36043561Skato struct i386_devdesc *dev; 36143561Skato struct open_disk *od; 36243561Skato int error; 36343561Skato 36443561Skato va_start(ap, f); 36543561Skato dev = va_arg(ap, struct i386_devdesc *); 36643561Skato va_end(ap); 36743561Skato if ((error = bd_opendisk(&od, dev))) 36843561Skato return(error); 36943561Skato 37043561Skato /* 37143561Skato * Save our context 37243561Skato */ 37343561Skato ((struct i386_devdesc *)(f->f_devdata))->d_kind.biosdisk.data = od; 37443561Skato DEBUG("open_disk %p, partition at 0x%x", od, od->od_boff); 37543561Skato return(0); 37643561Skato} 37743561Skato 37843561Skatostatic int 37943561Skatobd_opendisk(struct open_disk **odp, struct i386_devdesc *dev) 38043561Skato{ 381108650Snyan struct pc98_partition *dptr; 38243561Skato struct disklabel *lp; 38343561Skato struct open_disk *od; 38443561Skato int sector, slice, i; 38543561Skato int error; 38668358Snyan char buf[BUFSIZE]; 38743561Skato 388163897Smarcel if (dev->d_unit >= nbdinfo) { 38943561Skato DEBUG("attempt to open nonexistent disk"); 39043561Skato return(ENXIO); 39143561Skato } 39243561Skato 39343561Skato od = (struct open_disk *)malloc(sizeof(struct open_disk)); 39443561Skato if (!od) { 39543561Skato DEBUG("no memory"); 39643561Skato return (ENOMEM); 39743561Skato } 39843561Skato 39943561Skato /* Look up BIOS unit number, intialise open_disk structure */ 400163897Smarcel od->od_dkunit = dev->d_unit; 40143561Skato od->od_unit = bdinfo[od->od_dkunit].bd_unit; 40243561Skato od->od_flags = bdinfo[od->od_dkunit].bd_flags; 40343561Skato od->od_boff = 0; 40459777Snyan od->od_nslices = 0; 40543561Skato error = 0; 40643561Skato DEBUG("open '%s', unit 0x%x slice %d partition %c", 407163897Smarcel i386_fmtdev(dev), dev->d_unit, 40843561Skato dev->d_kind.biosdisk.slice, dev->d_kind.biosdisk.partition + 'a'); 40943561Skato 41043561Skato /* Get geometry for this open (removable device may have changed) */ 41143561Skato if (bd_getgeom(od)) { 41243561Skato DEBUG("can't get geometry"); 41343561Skato error = ENXIO; 41443561Skato goto out; 41543561Skato } 41643561Skato 41743561Skato /* 41843561Skato * Following calculations attempt to determine the correct value 41943561Skato * for d->od_boff by looking for the slice and partition specified, 42043561Skato * or searching for reasonable defaults. 42143561Skato */ 42243561Skato 42343561Skato /* 42443561Skato * Find the slice in the DOS slice table. 42543561Skato */ 42643561Skato if (od->od_flags & BD_FLOPPY) { 42743561Skato sector = 0; 42843561Skato goto unsliced; 42943561Skato } 43043561Skato if (bd_read(od, 0, 1, buf)) { 43143561Skato DEBUG("error reading MBR"); 43243561Skato error = EIO; 43343561Skato goto out; 43443561Skato } 43543561Skato 43643561Skato /* 43743561Skato * Check the slice table magic. 43843561Skato */ 43968358Snyan if (((u_char)buf[0x1fe] != 0x55) || ((u_char)buf[0x1ff] != 0xaa)) { 44043561Skato /* If a slice number was explicitly supplied, this is an error */ 44143561Skato if (dev->d_kind.biosdisk.slice > 0) { 44243561Skato DEBUG("no slice table/MBR (no magic)"); 44343561Skato error = ENOENT; 44443561Skato goto out; 44543561Skato } 44643561Skato sector = 0; 44743561Skato goto unsliced; /* may be a floppy */ 44843561Skato } 44943561Skato if (bd_read(od, 1, 1, buf)) { 45043561Skato DEBUG("error reading MBR"); 45143561Skato error = EIO; 45243561Skato goto out; 45343561Skato } 45459777Snyan 45559777Snyan /* 45659777Snyan * copy the partition table, then pick up any extended partitions. 45759777Snyan */ 45859777Snyan bcopy(buf + DOSPARTOFF, &od->od_slicetab, 459108650Snyan sizeof(struct pc98_partition) * NDOSPART); 46059777Snyan od->od_nslices = NDOSPART; /* extended slices start here */ 46143561Skato od->od_flags |= BD_PARTTABOK; 46259777Snyan dptr = &od->od_slicetab[0]; 46343561Skato 46443561Skato /* Is this a request for the whole disk? */ 46543561Skato if (dev->d_kind.biosdisk.slice == -1) { 46643561Skato sector = 0; 46743561Skato goto unsliced; 46843561Skato } 46943561Skato 47059777Snyan /* 47159777Snyan * if a slice number was supplied but not found, this is an error. 47259777Snyan */ 47359777Snyan if (dev->d_kind.biosdisk.slice > 0) { 47459777Snyan slice = dev->d_kind.biosdisk.slice - 1; 47559777Snyan if (slice >= od->od_nslices) { 47659777Snyan DEBUG("slice %d not found", slice); 47759777Snyan error = ENOENT; 47859777Snyan goto out; 47959777Snyan } 48059777Snyan } 48143561Skato 48259777Snyan /* Try to auto-detect the best slice; this should always give a slice number */ 48359777Snyan if (dev->d_kind.biosdisk.slice == 0) { 48459777Snyan slice = bd_bestslice(od); 48559777Snyan if (slice == -1) { 48659777Snyan error = ENOENT; 48759777Snyan goto out; 48859777Snyan } 48959777Snyan dev->d_kind.biosdisk.slice = slice; 49059777Snyan } 49159777Snyan 49259777Snyan dptr = &od->od_slicetab[0]; 49343561Skato /* 49443561Skato * Accept the supplied slice number unequivocally (we may be looking 49543561Skato * at a DOS partition). 49643561Skato */ 49743561Skato dptr += (dev->d_kind.biosdisk.slice - 1); /* we number 1-4, offsets are 0-3 */ 49859777Snyan sector = dptr->dp_scyl * od->od_hds * od->od_sec + 49959777Snyan dptr->dp_shd * od->od_sec + dptr->dp_ssect; 50043561Skato { 50159777Snyan int end = dptr->dp_ecyl * od->od_hds * od->od_sec + 50259777Snyan dptr->dp_ehd * od->od_sec + dptr->dp_esect; 50359777Snyan DEBUG("slice entry %d at %d, %d sectors", 50459777Snyan dev->d_kind.biosdisk.slice - 1, sector, end-sector); 50543561Skato } 50643561Skato 50743561Skato /* 50843561Skato * If we are looking at a BSD slice, and the partition is < 0, assume the 'a' partition 50943561Skato */ 51043561Skato if ((dptr->dp_mid == DOSMID_386BSD) && (dev->d_kind.biosdisk.partition < 0)) 51143561Skato dev->d_kind.biosdisk.partition = 0; 51243561Skato 51343561Skato unsliced: 51443561Skato /* 51543561Skato * Now we have the slice offset, look for the partition in the disklabel if we have 51643561Skato * a partition to start with. 51743561Skato * 51843561Skato * XXX we might want to check the label checksum. 51943561Skato */ 52043561Skato if (dev->d_kind.biosdisk.partition < 0) { 52143561Skato od->od_boff = sector; /* no partition, must be after the slice */ 52243561Skato DEBUG("opening raw slice"); 52343561Skato } else { 52453207Snyan 52543561Skato if (bd_read(od, sector + LABELSECTOR, 1, buf)) { 52643561Skato DEBUG("error reading disklabel"); 52743561Skato error = EIO; 52843561Skato goto out; 52943561Skato } 53043561Skato DEBUG("copy %d bytes of label from %p to %p", sizeof(struct disklabel), buf + LABELOFFSET, &od->od_disklabel); 53143561Skato bcopy(buf + LABELOFFSET, &od->od_disklabel, sizeof(struct disklabel)); 53243561Skato lp = &od->od_disklabel; 53343561Skato od->od_flags |= BD_LABELOK; 53443561Skato 53543561Skato if (lp->d_magic != DISKMAGIC) { 53643561Skato DEBUG("no disklabel"); 53743561Skato error = ENOENT; 53843561Skato goto out; 53943561Skato } 54043561Skato if (dev->d_kind.biosdisk.partition >= lp->d_npartitions) { 54143561Skato DEBUG("partition '%c' exceeds partitions in table (a-'%c')", 54243561Skato 'a' + dev->d_kind.biosdisk.partition, 'a' + lp->d_npartitions); 54343561Skato error = EPART; 54443561Skato goto out; 54543561Skato 54643561Skato } 54743561Skato 54868358Snyan#ifdef DISK_DEBUG 54968358Snyan /* Complain if the partition is unused unless this is a floppy. */ 55043561Skato if ((lp->d_partitions[dev->d_kind.biosdisk.partition].p_fstype == FS_UNUSED) && 55168358Snyan !(od->od_flags & BD_FLOPPY)) 55243561Skato DEBUG("warning, partition marked as unused"); 55368358Snyan#endif 55443561Skato 555146010Snyan od->od_boff = 556146010Snyan lp->d_partitions[dev->d_kind.biosdisk.partition].p_offset - 557146010Snyan lp->d_partitions[RAW_PART].p_offset + 558146010Snyan sector; 55943561Skato } 56043561Skato 56143561Skato out: 56243561Skato if (error) { 56343561Skato free(od); 56443561Skato } else { 56543561Skato *odp = od; /* return the open disk */ 56643561Skato } 56743561Skato return(error); 56843561Skato} 56943561Skato 57043561Skato/* 57143561Skato * Search for a slice with the following preferences: 57243561Skato * 57343561Skato * 1: Active FreeBSD slice 57443561Skato * 2: Non-active FreeBSD slice 57559777Snyan * 3: Active Linux slice 57659777Snyan * 4: non-active Linux slice 57759777Snyan * 5: Active FAT/FAT32 slice 57859777Snyan * 6: non-active FAT/FAT32 slice 57943561Skato */ 58059777Snyan#define PREF_RAWDISK 0 58159777Snyan#define PREF_FBSD_ACT 1 58259777Snyan#define PREF_FBSD 2 58359777Snyan#define PREF_LINUX_ACT 3 58459777Snyan#define PREF_LINUX 4 58559777Snyan#define PREF_DOS_ACT 5 58659777Snyan#define PREF_DOS 6 58759777Snyan#define PREF_NONE 7 58843561Skato 58959777Snyan/* 59059777Snyan * slicelimit is in the range 0 .. NDOSPART 59159777Snyan */ 59243561Skatostatic int 59359777Snyanbd_bestslice(struct open_disk *od) 59443561Skato{ 595108650Snyan struct pc98_partition *dp; 59659777Snyan int pref, preflevel; 59759777Snyan int i, prefslice; 59853207Snyan 59959777Snyan prefslice = 0; 60059777Snyan preflevel = PREF_NONE; 60143561Skato 60259777Snyan dp = &od->od_slicetab[0]; 60359777Snyan for (i = 0; i < od->od_nslices; i++, dp++) { 60459777Snyan switch(dp->dp_mid & 0x7f) { 60559777Snyan case DOSMID_386BSD & 0x7f: /* FreeBSD */ 60659777Snyan if ((dp->dp_mid & 0x80) && 60759777Snyan (preflevel > PREF_FBSD_ACT)) { 60859777Snyan pref = i; 60959777Snyan preflevel = PREF_FBSD_ACT; 61059777Snyan } else if (preflevel > PREF_FBSD) { 61159777Snyan pref = i; 61259777Snyan preflevel = PREF_FBSD; 61359777Snyan } 61459777Snyan break; 61559777Snyan 61659777Snyan case 0x11: /* DOS/Windows */ 61759777Snyan case 0x20: 61859777Snyan case 0x21: 61959777Snyan case 0x22: 62059777Snyan case 0x23: 62159777Snyan case 0x63: 62259777Snyan if ((dp->dp_mid & 0x80) && 62359777Snyan (preflevel > PREF_DOS_ACT)) { 62459777Snyan pref = i; 62559777Snyan preflevel = PREF_DOS_ACT; 62659777Snyan } else if (preflevel > PREF_DOS) { 62759777Snyan pref = i; 62859777Snyan preflevel = PREF_DOS; 62959777Snyan } 63059777Snyan break; 63159777Snyan } 63243561Skato } 63359777Snyan return (prefslice); 63443561Skato} 63587734Snyan 63643561Skatostatic int 63743561Skatobd_close(struct open_file *f) 63843561Skato{ 63943561Skato struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)(f->f_devdata))->d_kind.biosdisk.data); 64043561Skato 64143561Skato bd_closedisk(od); 64243561Skato return(0); 64343561Skato} 64443561Skato 64543561Skatostatic void 64643561Skatobd_closedisk(struct open_disk *od) 64743561Skato{ 64843561Skato DEBUG("open_disk %p", od); 64943561Skato#if 0 65043561Skato /* XXX is this required? (especially if disk already open...) */ 65143561Skato if (od->od_flags & BD_FLOPPY) 65243561Skato delay(3000000); 65343561Skato#endif 65443561Skato free(od); 65543561Skato} 65643561Skato 65743561Skatostatic int 65868358Snyanbd_strategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize) 65943561Skato{ 66043561Skato struct bcache_devdata bcd; 66158165Snyan struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)devdata)->d_kind.biosdisk.data); 66258165Snyan 66343561Skato bcd.dv_strategy = bd_realstrategy; 66443561Skato bcd.dv_devdata = devdata; 66558165Snyan return(bcache_strategy(&bcd, od->od_unit, rw, dblk+od->od_boff, size, buf, rsize)); 66643561Skato} 66743561Skato 66843561Skatostatic int 66968358Snyanbd_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize) 67043561Skato{ 67143561Skato struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)devdata)->d_kind.biosdisk.data); 67243561Skato int blks; 67343561Skato#ifdef BD_SUPPORT_FRAGS 67443561Skato char fragbuf[BIOSDISK_SECSIZE]; 67543561Skato size_t fragsize; 67643561Skato 67743561Skato fragsize = size % BIOSDISK_SECSIZE; 67843561Skato#else 67943561Skato if (size % BIOSDISK_SECSIZE) 68043561Skato panic("bd_strategy: %d bytes I/O not multiple of block size", size); 68143561Skato#endif 68243561Skato 68343561Skato DEBUG("open_disk %p", od); 68443561Skato blks = size / BIOSDISK_SECSIZE; 68543561Skato if (rsize) 68643561Skato *rsize = 0; 687172925Snyan 688172925Snyan switch(rw){ 689172925Snyan case F_READ: 690172925Snyan DEBUG("read %d from %d to %p", blks, dblk, buf); 691172925Snyan 692172925Snyan if (blks && bd_read(od, dblk, blks, buf)) { 693172925Snyan DEBUG("read error"); 694172925Snyan return (EIO); 695172925Snyan } 69643561Skato#ifdef BD_SUPPORT_FRAGS 697172925Snyan DEBUG("bd_strategy: frag read %d from %d+%d to %p", 698172925Snyan fragsize, dblk, blks, buf + (blks * BIOSDISK_SECSIZE)); 699172925Snyan if (fragsize && bd_read(od, dblk + blks, 1, fragsize)) { 700172925Snyan DEBUG("frag read error"); 701172925Snyan return(EIO); 702172925Snyan } 703172925Snyan bcopy(fragbuf, buf + (blks * BIOSDISK_SECSIZE), fragsize); 70443561Skato#endif 705172925Snyan break; 706172925Snyan case F_WRITE : 707172925Snyan DEBUG("write %d from %d to %p", blks, dblk, buf); 70887734Snyan 709172925Snyan if (blks && bd_write(od, dblk, blks, buf)) { 710172925Snyan DEBUG("write error"); 711172925Snyan return (EIO); 712172925Snyan } 71387734Snyan#ifdef BD_SUPPORT_FRAGS 71487734Snyan if(fragsize) { 715172925Snyan DEBUG("Attempted to write a frag"); 716172925Snyan return (EIO); 71787734Snyan } 71887734Snyan#endif 719172925Snyan break; 720172925Snyan default: 721172925Snyan /* DO NOTHING */ 722172925Snyan return (EROFS); 723172925Snyan } 72487734Snyan 72587734Snyan if (rsize) 72687734Snyan *rsize = size; 72787734Snyan return (0); 72843561Skato} 72943561Skato 73043561Skato/* Max number of sectors to bounce-buffer if the request crosses a 64k boundary */ 73143561Skato#define FLOPPY_BOUNCEBUF 18 73243561Skato 73343561Skatostatic int 734172965Snyanbd_chs_io(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest, int write) 73543561Skato{ 736172965Snyan u_int x, bpc, cyl, hd, sec; 737172965Snyan 738172965Snyan bpc = (od->od_sec * od->od_hds); /* blocks per cylinder */ 739172965Snyan x = dblk; 740172965Snyan cyl = x / bpc; /* block # / blocks per cylinder */ 741172965Snyan x %= bpc; /* block offset into cylinder */ 742172965Snyan hd = x / od->od_sec; /* offset / blocks per track */ 743172965Snyan sec = x % od->od_sec; /* offset into track */ 744172965Snyan 745172965Snyan v86.ctl = V86_FLAGS; 746172965Snyan v86.addr = 0x1b; 747172965Snyan if (write) 748172965Snyan v86.eax = 0x0500 | od->od_unit; 749172965Snyan else 750172965Snyan v86.eax = 0x0600 | od->od_unit; 751172965Snyan if (od->od_flags & BD_FLOPPY) { 752172965Snyan v86.eax |= 0xd000; 753172965Snyan v86.ecx = 0x0200 | (cyl & 0xff); 754172965Snyan v86.edx = (hd << 8) | (sec + 1); 755172965Snyan } else if (od->od_flags & BD_OPTICAL) { 756172965Snyan v86.eax &= 0xFF7F; 757172965Snyan v86.ecx = dblk & 0xFFFF; 758172965Snyan v86.edx = dblk >> 16; 759172965Snyan } else { 760172965Snyan v86.ecx = cyl; 761172965Snyan v86.edx = (hd << 8) | sec; 762172965Snyan } 763172965Snyan v86.ebx = blks * BIOSDISK_SECSIZE; 764172965Snyan v86.es = VTOPSEG(dest); 765172965Snyan v86.ebp = VTOPOFF(dest); 766172965Snyan v86int(); 767172965Snyan return (v86.efl & 0x1); 768172965Snyan} 769172965Snyan 770172965Snyanstatic int 771172965Snyanbd_io(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest, int write) 772172965Snyan{ 773172965Snyan u_int x, sec, result, resid, retry, maxfer; 77443561Skato caddr_t p, xp, bbuf, breg; 77543561Skato 776172965Snyan /* Just in case some idiot actually tries to read/write -1 blocks... */ 77768358Snyan if (blks < 0) 77868358Snyan return (-1); 77968358Snyan 78043561Skato resid = blks; 78143561Skato p = dest; 78243561Skato 78343561Skato /* Decide whether we have to bounce */ 784153598Snyan if (VTOP(dest) >> 20 != 0 || 785153598Snyan ((VTOP(dest) >> 16) != (VTOP(dest + blks * BIOSDISK_SECSIZE) >> 16))) { 78643561Skato 78743561Skato /* 788172965Snyan * There is a 64k physical boundary somewhere in the 789172965Snyan * destination buffer, or the destination buffer is above 790172965Snyan * first 1MB of physical memory so we have to arrange a 791172965Snyan * suitable bounce buffer. Allocate a buffer twice as large 792172965Snyan * as we need to. Use the bottom half unless there is a break 793172965Snyan * there, in which case we use the top half. 79443561Skato */ 79568358Snyan x = min(od->od_sec, (unsigned)blks); 796153598Snyan bbuf = alloca(x * 2 * BIOSDISK_SECSIZE); 797172965Snyan if (((u_int32_t)VTOP(bbuf) & 0xffff0000) == 798172965Snyan ((u_int32_t)VTOP(bbuf + x * BIOSDISK_SECSIZE) & 0xffff0000)) { 79943561Skato breg = bbuf; 80043561Skato } else { 80143561Skato breg = bbuf + x * BIOSDISK_SECSIZE; 80243561Skato } 803172965Snyan maxfer = x; /* limit transfers to bounce region size */ 80443561Skato } else { 80568358Snyan breg = bbuf = NULL; 80643561Skato maxfer = 0; 80743561Skato } 80843561Skato 80943561Skato while (resid > 0) { 810172965Snyan /* 811172965Snyan * Play it safe and don't cross track boundaries. 812172965Snyan * (XXX this is probably unnecessary) 813172965Snyan */ 814172965Snyan sec = dblk % od->od_sec; /* offset into track */ 81543561Skato x = min(od->od_sec - sec, resid); 81643561Skato if (maxfer > 0) 81743561Skato x = min(x, maxfer); /* fit bounce buffer */ 81843561Skato 81943561Skato /* where do we transfer to? */ 82043561Skato xp = bbuf == NULL ? p : breg; 82143561Skato 822172965Snyan /* 823172965Snyan * Put your Data In, Put your Data out, 824172965Snyan * Put your Data In, and shake it all about 825172965Snyan */ 826172965Snyan if (write && bbuf != NULL) 827172965Snyan bcopy(p, breg, x * BIOSDISK_SECSIZE); 82843561Skato 829172965Snyan /* 830172965Snyan * Loop retrying the operation a couple of times. The BIOS 831172965Snyan * may also retry. 832172965Snyan */ 83343561Skato for (retry = 0; retry < 3; retry++) { 83443561Skato /* if retrying, reset the drive */ 83543561Skato if (retry > 0) { 83651586Skato v86.ctl = V86_FLAGS; 83751586Skato v86.addr = 0x1b; 83851586Skato v86.eax = 0x0300 | od->od_unit; 83943561Skato v86int(); 84043561Skato } 841172965Snyan 842172965Snyan result = bd_chs_io(od, dblk, x, xp, write); 84343561Skato if (result == 0) 84443561Skato break; 84543561Skato } 846172965Snyan 847172965Snyan if (write) 848172965Snyan DEBUG("%d sectors from %lld to %p (0x%x) %s", x, dblk, p, VTOP(p), 849172965Snyan result ? "failed" : "ok"); 850172965Snyan else 851172965Snyan DEBUG("%d sectors from %p (0x%x) to %lld %s", x, p, VTOP(p), dblk, 852172965Snyan result ? "failed" : "ok"); 85343561Skato if (result) { 85443561Skato return(-1); 85543561Skato } 856172965Snyan if (!write && bbuf != NULL) 85743561Skato bcopy(breg, p, x * BIOSDISK_SECSIZE); 85843561Skato p += (x * BIOSDISK_SECSIZE); 85943561Skato dblk += x; 86043561Skato resid -= x; 86143561Skato } 862172965Snyan 86343561Skato/* hexdump(dest, (blks * BIOSDISK_SECSIZE)); */ 86443561Skato return(0); 86543561Skato} 86643561Skato 867172965Snyanstatic int 868172965Snyanbd_read(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest) 869172965Snyan{ 87087734Snyan 871172965Snyan return (bd_io(od, dblk, blks, dest, 0)); 872172965Snyan} 873172965Snyan 87443561Skatostatic int 87587734Snyanbd_write(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest) 87687734Snyan{ 87787734Snyan 878172965Snyan return (bd_io(od, dblk, blks, dest, 1)); 879172965Snyan} 88087734Snyan 88187734Snyanstatic int 88243561Skatobd_getgeom(struct open_disk *od) 88343561Skato{ 88443561Skato 88543561Skato if (od->od_flags & BD_FLOPPY) { 88654819Snyan od->od_cyl = 79; 88743561Skato od->od_hds = 2; 88843561Skato od->od_sec = (od->od_unit & 0xf0) == 0x30 ? 18 : 15; 889108791Snyan } else if (od->od_flags & BD_OPTICAL) { 890108791Snyan od->od_cyl = 0xFFFE; 891108791Snyan od->od_hds = 8; 892108791Snyan od->od_sec = 32; 89359777Snyan } else { 89454819Snyan v86.ctl = V86_FLAGS; 89543561Skato v86.addr = 0x1b; 89643561Skato v86.eax = 0x8400 | od->od_unit; 89743561Skato v86int(); 89843561Skato 89943561Skato od->od_cyl = v86.ecx; 90043561Skato od->od_hds = (v86.edx >> 8) & 0xff; 90143561Skato od->od_sec = v86.edx & 0xff; 90251586Skato if (v86.efl & 0x1) 90351586Skato return(1); 90443561Skato } 90543561Skato 90643561Skato DEBUG("unit 0x%x geometry %d/%d/%d", od->od_unit, od->od_cyl, od->od_hds, od->od_sec); 90743561Skato return(0); 90843561Skato} 90943561Skato 91043561Skato/* 91153207Snyan * Return the BIOS geometry of a given "fixed drive" in a format 91253207Snyan * suitable for the legacy bootinfo structure. Since the kernel is 91353207Snyan * expecting raw int 0x13/0x8 values for N_BIOS_GEOM drives, we 91453207Snyan * prefer to get the information directly, rather than rely on being 91553207Snyan * able to put it together from information already maintained for 91653207Snyan * different purposes and for a probably different number of drives. 91753207Snyan * 91853207Snyan * For valid drives, the geometry is expected in the format (31..0) 91953207Snyan * "000000cc cccccccc hhhhhhhh 00ssssss"; and invalid drives are 92053207Snyan * indicated by returning the geometry of a "1.2M" PC-format floppy 92153207Snyan * disk. And, incidentally, what is returned is not the geometry as 92253207Snyan * such but the highest valid cylinder, head, and sector numbers. 92353207Snyan */ 92453207Snyanu_int32_t 92553207Snyanbd_getbigeom(int bunit) 92653207Snyan{ 92754819Snyan int hds = 0; 92855339Snyan int unit = 0x80; /* IDE HDD */ 92954819Snyan u_int addr = 0xA155d; 93055339Snyan 93154819Snyan while (unit < 0xa7) { 93255339Snyan if (*(u_char *)PTOV(addr) & (1 << (unit & 0x0f))) 93355339Snyan if (hds++ == bunit) 93454819Snyan break; 935108791Snyan 936108791Snyan if (unit >= 0xA0) { 937108791Snyan int media = ((unsigned *)PTOV(0xA1460))[unit & 0x0F] & 0x1F; 938108791Snyan 939108791Snyan if (media == 7 && hds++ == bunit) /* SCSI MO */ 940108791Snyan return(0xFFFE0820); /* C:65535 H:8 S:32 */ 941108791Snyan } 94254819Snyan if (++unit == 0x84) { 943108791Snyan unit = 0xA0; /* SCSI HDD */ 94454819Snyan addr = 0xA1482; 94554819Snyan } 94654819Snyan } 94754819Snyan if (unit == 0xa7) 948108791Snyan return 0x4F020F; /* 1200KB FD C:80 H:2 S:15 */ 94953207Snyan v86.ctl = V86_FLAGS; 95054819Snyan v86.addr = 0x1b; 95154819Snyan v86.eax = 0x8400 | unit; 95254819Snyan v86int(); 95354819Snyan if (v86.efl & 0x1) 954108791Snyan return 0x4F020F; /* 1200KB FD C:80 H:2 S:15 */ 95554819Snyan return ((v86.ecx & 0xffff) << 16) | (v86.edx & 0xffff); 95653207Snyan} 95753207Snyan 95853207Snyan/* 959130603Sphk * Return a suitable dev_t value for (dev). 96043561Skato * 96143561Skato * In the case where it looks like (dev) is a SCSI disk, we allow the number of 96243561Skato * IDE disks to be specified in $num_ide_disks. There should be a Better Way. 96343561Skato */ 96443561Skatoint 96543561Skatobd_getdev(struct i386_devdesc *dev) 96643561Skato{ 96743561Skato struct open_disk *od; 96843561Skato int biosdev; 96943561Skato int major; 97043561Skato int rootdev; 97143561Skato char *nip, *cp; 97243561Skato int unitofs = 0, i, unit; 97343561Skato 974163897Smarcel biosdev = bd_unit2bios(dev->d_unit); 975163897Smarcel DEBUG("unit %d BIOS device %d", dev->d_unit, biosdev); 97643561Skato if (biosdev == -1) /* not a BIOS device */ 97743561Skato return(-1); 97843561Skato if (bd_opendisk(&od, dev) != 0) /* oops, not a viable device */ 97943561Skato return(-1); 98043561Skato 98143561Skato if ((biosdev & 0xf0) == 0x90 || (biosdev & 0xf0) == 0x30) { 98243561Skato /* floppy (or emulated floppy) or ATAPI device */ 983163897Smarcel if (bdinfo[dev->d_unit].bd_type == DT_ATAPI) { 98443561Skato /* is an ATAPI disk */ 98543561Skato major = WFDMAJOR; 98643561Skato } else { 98743561Skato /* is a floppy disk */ 98843561Skato major = FDMAJOR; 98943561Skato } 99043561Skato } else { 99143561Skato /* harddisk */ 99243561Skato if ((od->od_flags & BD_LABELOK) && (od->od_disklabel.d_type == DTYPE_SCSI)) { 99343561Skato /* label OK, disk labelled as SCSI */ 99443561Skato major = DAMAJOR; 99543561Skato /* check for unit number correction hint, now deprecated */ 99643561Skato if ((nip = getenv("num_ide_disks")) != NULL) { 99743561Skato i = strtol(nip, &cp, 0); 99843561Skato /* check for parse error */ 99943561Skato if ((cp != nip) && (*cp == 0)) 100043561Skato unitofs = i; 100143561Skato } 100243561Skato } else { 100343561Skato /* assume an IDE disk */ 100443561Skato major = WDMAJOR; 100543561Skato } 100643561Skato } 100768358Snyan /* default root disk unit number */ 100868358Snyan if ((biosdev & 0xf0) == 0xa0) 1009163897Smarcel unit = bdinfo[dev->d_unit].bd_da_unit; 101068358Snyan else 101168358Snyan unit = biosdev & 0xf; 101268358Snyan 101343561Skato /* XXX a better kludge to set the root disk unit number */ 101443561Skato if ((nip = getenv("root_disk_unit")) != NULL) { 101543561Skato i = strtol(nip, &cp, 0); 101643561Skato /* check for parse error */ 101743561Skato if ((cp != nip) && (*cp == 0)) 101843561Skato unit = i; 101943561Skato } 102043561Skato 1021172921Sjhb rootdev = MAKEBOOTDEV(major, dev->d_kind.biosdisk.slice + 1, unit, 1022172921Sjhb dev->d_kind.biosdisk.partition); 102343561Skato DEBUG("dev is 0x%x\n", rootdev); 102443561Skato return(rootdev); 102543561Skato} 1026