biosdisk.c revision 199767f8919635c4928607450d9e0abb932109ce
/*-
* Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
* Copyright (c) 2012 Andrey V. Elsukov <ae@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
__FBSDID("$FreeBSD$");
/*
* BIOS disk device handling.
*
* Ideas and algorithms from:
*
* - NetBSD libi386/biosdisk.c
*
*/
#include <stand.h>
#include <machine/bootinfo.h>
#include <stdarg.h>
#include <bootstrap.h>
#include <btxv86.h>
#include <edd.h>
#include "disk.h"
#include "libi386.h"
#define BIOS_NUMDRIVES 0x475
#define BIOSDISK_SECSIZE 512
#define WDMAJOR 0 /* major numbers for devices we frontend for */
#define WFDMAJOR 1
#define FDMAJOR 2
#define DAMAJOR 4
#ifdef DISK_DEBUG
#else
#endif
/*
* List of BIOS devices, translation from disk unit number to
* BIOS unit number.
*/
static struct bdinfo
{
int bd_unit; /* BIOS unit number */
int bd_cyl; /* BIOS geometry */
int bd_hds;
int bd_sec;
int bd_flags;
#define BD_MODEINT13 0x0000
#define BD_MODEEDD1 0x0001
#define BD_MODEEDD3 0x0002
#define BD_MODEMASK 0x0003
#define BD_FLOPPY 0x0004
int bd_type; /* BIOS 'drive type' (floppy only) */
int bd_open; /* reference counter */
void *bd_bcache; /* buffer cache data */
static int nbdinfo = 0;
static int bd_init(void);
static void bd_cleanup(void);
"disk",
};
/*
* Translate between BIOS device numbers and our private unit numbers.
*/
int
bd_bios2unit(int biosdev)
{
int i;
for (i = 0; i < nbdinfo; i++) {
return (i);
}
return (-1);
}
int
bd_unit2bios(int unit)
{
return (-1);
}
/*
* Quiz the BIOS for disk devices, save a little info about them.
*/
static int
bd_init(void)
{
/* sequence 0, 0x80 */
#ifndef VIRTUALBOX
/*
* Check the BIOS equipment list for number
* of fixed disks.
*/
if(base == 0x80 &&
break;
#endif
break;
#ifndef BOOT2
/* XXX we need "disk aliases" to make this simpler */
#endif
nbdinfo++;
if (base == 0x80)
nfd++;
}
}
return(0);
}
static void
bd_cleanup(void)
{
}
/*
* Try to detect a device supported by the legacy int13 BIOS
*/
static int
{
struct edd_params params;
v86int();
/* Don't error out if we get bad sector number, try EDD as well */
return (0); /* skip device */
ret = 0; /* set error */
/* Convert max cyl # -> # of cylinders */
/* Convert max head # -> # of heads */
/* Calculate sectors count from the geometry */
/* Determine if we can use EDD with this device. */
v86int();
return (ret); /* return code from int13 AH=08 */
/* EDD supported */
/* Get disk params */
v86int();
ret = 1;
}
DEBUG("unit 0x%x flags %x, sectors %llu, sectorsize %u",
return (ret);
}
/*
* Print information about disks
*/
static int
{
static char line[80];
struct disk_devdesc dev;
int i, ret = 0;
for (i = 0; i < nbdinfo; i++) {
if (ret != 0)
return (ret);
bdinfo[i].bd_sectorsize,
DISK_F_NOCACHE: 0) == 0) {
disk_close(&dev);
if (ret != 0)
return (ret);
}
}
return (ret);
}
/*
* Attempt to open the disk described by (dev) for use by (f).
*
* Note that the philosophy here is "give them exactly what
* they ask for". This is necessary because being too "smart"
* about what the user might want leads to complications.
* (eg. given no slice or partition value, with a disk that is
* sliced - are they after the first BSD slice, or the DOS
* slice before it?)
*/
static int
{
struct disk_devdesc *dev;
return (EIO);
DISK_F_NOCACHE: 0));
}
static int
{
struct disk_devdesc *dev;
}
return (disk_close(dev));
}
static int
{
struct disk_devdesc *dev;
switch (cmd) {
case DIOCGSECTORSIZE:
break;
case DIOCGMEDIASIZE:
break;
default:
return (ENOTTY);
}
return (0);
}
static int
{
struct bcache_devdata bcd;
struct disk_devdesc *dev;
}
static int
{
#ifdef BD_SUPPORT_FRAGS /* XXX: sector size */
char fragbuf[BIOSDISK_SECSIZE];
#else
#endif
if (rsize)
*rsize = 0;
/*
* Perform partial read to prevent read-ahead crossing
* the end of disk - or any 32 bit aliases of the end.
* Signed arithmetic is used to handle wrap-around cases
* like we do for TCP sequence numbers.
*/
}
switch(rw){
case F_READ:
DEBUG("read error");
return (EIO);
}
#ifdef BD_SUPPORT_FRAGS /* XXX: sector size */
DEBUG("bd_strategy: frag read %d from %d+%d to %p",
DEBUG("frag read error");
return(EIO);
}
#endif
break;
case F_WRITE :
DEBUG("write error");
return (EIO);
}
#ifdef BD_SUPPORT_FRAGS
if(fragsize) {
DEBUG("Attempted to write a frag");
return (EIO);
}
#endif
break;
default:
/* DO NOTHING */
return (EROFS);
}
if (rsize)
return (0);
}
/* Max number of sectors to bounce-buffer if the request crosses a 64k boundary */
#define FLOPPY_BOUNCEBUF 18
static int
int dowrite)
{
static struct edd_packet packet;
if (dowrite)
/* Should we Write with verify ?? 0x4302 ? */
else
v86int();
}
static int
int dowrite)
{
x = dblk;
x %= bpc; /* block offset into cylinder */
/* correct sector number for 1-based BIOS numbering */
sec++;
if (cyl > 1023)
/* CHS doesn't support cylinders > 1023. */
return (1);
if (dowrite)
else
v86int();
}
static int
{
if (blks < 0)
return (-1);
p = dest;
/* Decide whether we have to bounce */
/*
* There is a 64k physical boundary somewhere in the
* destination buffer, or the destination buffer is above
* first 1MB of physical memory so we have to arrange a
* suitable bounce buffer. Allocate a buffer twice as large
* as we need to. Use the bottom half unless there is a break
* there, in which case we use the top half.
*/
} else {
}
maxfer = x; /* limit transfers to bounce region size */
} else {
maxfer = 0;
}
while (resid > 0) {
/*
* Play it safe and don't cross track boundaries.
* (XXX this is probably unnecessary)
*/
if (maxfer > 0)
/* where do we transfer to? */
/*
* Put your Data In, Put your Data out,
* Put your Data In, and shake it all about
*/
/*
* Loop retrying the operation a couple of times. The BIOS
* may also retry.
*/
/* if retrying, reset the drive */
if (retry > 0) {
v86int();
}
else
if (result == 0)
break;
}
if (dowrite)
DEBUG("Write %d sector(s) from %p (0x%x) to %lld %s", x,
else
DEBUG("Read %d sector(s) from %lld to %p (0x%x) %s", x,
if (result) {
return(-1);
}
dblk += x;
resid -= x;
}
/* hexdump(dest, (blks * BD(dev).bd_sectorsize)); */
return(0);
}
static int
{
}
static int
{
}
/*
* Return the BIOS geometry of a given "fixed drive" in a format
* suitable for the legacy bootinfo structure. Since the kernel is
* expecting raw int 0x13/0x8 values for N_BIOS_GEOM drives, we
* prefer to get the information directly, rather than rely on being
* able to put it together from information already maintained for
* different purposes and for a probably different number of drives.
*
* For valid drives, the geometry is expected in the format (31..0)
* "000000cc cccccccc hhhhhhhh 00ssssss"; and invalid drives are
* indicated by returning the geometry of a "1.2M" PC-format floppy
* disk. And, incidentally, what is returned is not the geometry as
* such but the highest valid cylinder, head, and sector numbers.
*/
bd_getbigeom(int bunit)
{
v86int();
return 0x4f010f;
}
/*
* Return a suitable dev_t value for (dev).
*
* In the case where it looks like (dev) is a SCSI disk, we allow the number of
* IDE disks to be specified in $num_ide_disks. There should be a Better Way.
*/
int
bd_getdev(struct i386_devdesc *d)
{
struct disk_devdesc *dev;
int biosdev;
int major;
int rootdev;
int i, unit;
dev = (struct disk_devdesc *)d;
return(-1);
DISK_F_NOCACHE: 0) != 0) /* oops, not a viable device */
return (-1);
else
if (biosdev < 0x80) {
/* floppy (or emulated floppy) or ATAPI device */
/* is an ATAPI disk */
} else {
/* is a floppy disk */
}
} else {
/* assume an IDE disk */
}
/* default root disk unit number */
/* XXX a better kludge to set the root disk unit number */
/* check for parse error */
unit = i;
}
return(rootdev);
}