2001-07-08 02:57:57 +04:00
|
|
|
/* $NetBSD: biosdisk.c,v 1.15 2001/07/07 22:57:57 perry Exp $ */
|
1997-03-14 05:40:32 +03:00
|
|
|
|
|
|
|
/*
|
1998-02-19 17:12:48 +03:00
|
|
|
* Copyright (c) 1996, 1998
|
1997-03-14 05:40:32 +03:00
|
|
|
* Matthias Drochner. 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
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed for the NetBSD Project
|
|
|
|
* by Matthias Drochner.
|
|
|
|
* 4. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
1997-03-22 04:41:34 +03:00
|
|
|
/*
|
1997-06-13 17:36:04 +04:00
|
|
|
* raw BIOS disk device for libsa.
|
|
|
|
* needs lowlevel parts from bios_disk.S and biosdisk_ll.c
|
|
|
|
* partly from netbsd:sys/arch/i386/boot/disk.c
|
|
|
|
* no bad144 handling!
|
1997-03-14 05:40:32 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ported to boot 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
|
|
|
|
*
|
|
|
|
* Mach Operating System
|
|
|
|
* Copyright (c) 1992, 1991 Carnegie Mellon University
|
|
|
|
* All Rights Reserved.
|
1997-03-22 04:41:34 +03:00
|
|
|
*
|
1997-03-14 05:40:32 +03:00
|
|
|
* Permission to use, copy, modify and distribute this software and its
|
|
|
|
* documentation is hereby granted, provided that both the copyright
|
|
|
|
* notice and this permission notice appear in all copies of the
|
|
|
|
* software, derivative works or modified versions, and any portions
|
|
|
|
* thereof, and that both notices appear in supporting documentation.
|
1997-03-22 04:41:34 +03:00
|
|
|
*
|
1997-03-14 05:40:32 +03:00
|
|
|
* CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
|
|
|
|
* CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
|
|
|
|
* ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
|
1997-03-22 04:41:34 +03:00
|
|
|
*
|
1997-03-14 05:40:32 +03:00
|
|
|
* Carnegie Mellon requests users of this software to return to
|
1997-03-22 04:41:34 +03:00
|
|
|
*
|
1997-03-14 05:40:32 +03:00
|
|
|
* Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
|
|
|
|
* School of Computer Science
|
|
|
|
* Carnegie Mellon University
|
|
|
|
* Pittsburgh PA 15213-3890
|
1997-03-22 04:41:34 +03:00
|
|
|
*
|
1997-03-14 05:40:32 +03:00
|
|
|
* any improvements or extensions that they make and grant Carnegie Mellon
|
|
|
|
* the rights to redistribute these changes.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/disklabel.h>
|
|
|
|
|
|
|
|
#include <lib/libsa/stand.h>
|
|
|
|
#include <lib/libsa/saerrno.h>
|
1997-09-17 22:08:13 +04:00
|
|
|
#include <machine/stdarg.h>
|
1997-03-14 05:40:32 +03:00
|
|
|
|
|
|
|
#include "libi386.h"
|
|
|
|
#include "biosdisk_ll.h"
|
1997-09-17 22:08:13 +04:00
|
|
|
#include "biosdisk.h"
|
1998-05-15 20:38:53 +04:00
|
|
|
#ifdef _STANDALONE
|
1997-09-17 22:08:13 +04:00
|
|
|
#include "bootinfo.h"
|
1998-05-15 20:38:53 +04:00
|
|
|
#endif
|
1997-03-14 05:40:32 +03:00
|
|
|
|
|
|
|
#define BUFSIZE (1 * BIOSDISK_SECSIZE)
|
|
|
|
|
1997-03-22 04:41:34 +03:00
|
|
|
struct biosdisk {
|
|
|
|
struct biosdisk_ll ll;
|
|
|
|
int boff;
|
|
|
|
char buf[BUFSIZE];
|
1997-03-14 05:40:32 +03:00
|
|
|
};
|
|
|
|
|
1998-05-15 20:38:53 +04:00
|
|
|
#ifdef _STANDALONE
|
1997-09-17 22:08:13 +04:00
|
|
|
static struct btinfo_bootdisk bi_disk;
|
1998-05-15 20:38:53 +04:00
|
|
|
#endif
|
1997-09-17 22:08:13 +04:00
|
|
|
|
2000-10-30 10:30:59 +03:00
|
|
|
#define RF_PROTECTED_SECTORS 64 /* XXX refer to <.../rf_optnames.h> */
|
|
|
|
|
1997-03-22 04:41:34 +03:00
|
|
|
int
|
|
|
|
biosdiskstrategy(devdata, flag, dblk, size, buf, rsize)
|
|
|
|
void *devdata;
|
|
|
|
int flag;
|
|
|
|
daddr_t dblk;
|
|
|
|
size_t size;
|
|
|
|
void *buf;
|
|
|
|
size_t *rsize;
|
1997-03-14 05:40:32 +03:00
|
|
|
{
|
1997-03-22 04:41:34 +03:00
|
|
|
struct biosdisk *d;
|
|
|
|
int blks, frag;
|
1997-03-14 05:40:32 +03:00
|
|
|
|
1997-03-22 04:41:34 +03:00
|
|
|
if (flag != F_READ)
|
|
|
|
return (EROFS);
|
1997-03-14 05:40:32 +03:00
|
|
|
|
1997-03-22 04:41:34 +03:00
|
|
|
d = (struct biosdisk *) devdata;
|
1997-03-14 05:40:32 +03:00
|
|
|
|
1997-03-22 04:41:34 +03:00
|
|
|
dblk += d->boff;
|
1997-03-14 05:40:32 +03:00
|
|
|
|
1997-03-22 04:41:34 +03:00
|
|
|
blks = size / BIOSDISK_SECSIZE;
|
|
|
|
if (blks && readsects(&d->ll, dblk, blks, buf, 0)) {
|
|
|
|
if (rsize)
|
|
|
|
*rsize = 0;
|
|
|
|
return (EIO);
|
|
|
|
}
|
|
|
|
/* do we really need this? */
|
|
|
|
frag = size % BIOSDISK_SECSIZE;
|
|
|
|
if (frag) {
|
|
|
|
if (readsects(&d->ll, dblk + blks, 1, d->buf, 0)) {
|
|
|
|
if (rsize)
|
|
|
|
*rsize = blks * BIOSDISK_SECSIZE;
|
|
|
|
return (EIO);
|
|
|
|
}
|
2001-07-08 02:57:57 +04:00
|
|
|
memcpy(buf + blks * BIOSDISK_SECSIZE, d->buf, frag);
|
1997-03-22 04:41:34 +03:00
|
|
|
}
|
|
|
|
if (rsize)
|
|
|
|
*rsize = size;
|
|
|
|
return (0);
|
1997-03-14 05:40:32 +03:00
|
|
|
}
|
|
|
|
|
1997-03-22 04:41:34 +03:00
|
|
|
int
|
1997-09-17 22:08:13 +04:00
|
|
|
biosdiskopen(struct open_file *f, ...)
|
|
|
|
/* file, biosdev, partition */
|
1997-03-14 05:40:32 +03:00
|
|
|
{
|
1997-09-17 22:08:13 +04:00
|
|
|
va_list ap;
|
1997-03-22 04:41:34 +03:00
|
|
|
struct biosdisk *d;
|
1997-10-13 13:26:29 +04:00
|
|
|
int partition;
|
1997-03-14 05:40:32 +03:00
|
|
|
#ifndef NO_DISKLABEL
|
1999-01-27 23:54:56 +03:00
|
|
|
struct mbr_partition *dptr;
|
1997-10-13 13:26:29 +04:00
|
|
|
int sector, i;
|
1997-03-22 04:41:34 +03:00
|
|
|
struct disklabel *lp;
|
1997-03-14 05:40:32 +03:00
|
|
|
#endif
|
1997-10-13 13:26:29 +04:00
|
|
|
int error = 0;
|
1997-03-14 05:40:32 +03:00
|
|
|
|
1997-03-22 04:41:34 +03:00
|
|
|
d = (struct biosdisk *) alloc(sizeof(struct biosdisk));
|
|
|
|
if (!d) {
|
1997-03-14 05:40:32 +03:00
|
|
|
#ifdef DEBUG
|
1997-03-22 04:41:34 +03:00
|
|
|
printf("biosdiskopen: no memory\n");
|
1997-03-14 05:40:32 +03:00
|
|
|
#endif
|
1997-03-22 04:41:34 +03:00
|
|
|
return (ENOMEM);
|
|
|
|
}
|
1997-09-17 22:08:13 +04:00
|
|
|
va_start(ap, f);
|
1998-05-15 20:38:53 +04:00
|
|
|
d->ll.dev = va_arg(ap, int);
|
1999-03-08 03:09:24 +03:00
|
|
|
if (set_geometry(&d->ll, NULL)) {
|
1997-03-14 05:40:32 +03:00
|
|
|
#ifdef DISK_DEBUG
|
1997-03-22 04:41:34 +03:00
|
|
|
printf("no geometry information\n");
|
1997-03-14 05:40:32 +03:00
|
|
|
#endif
|
1997-03-22 04:41:34 +03:00
|
|
|
error = ENXIO;
|
|
|
|
goto out;
|
|
|
|
}
|
1997-09-17 22:08:13 +04:00
|
|
|
|
1997-10-13 13:26:29 +04:00
|
|
|
d->boff = 0;
|
1998-05-15 20:38:53 +04:00
|
|
|
partition = va_arg(ap, int);
|
|
|
|
#ifdef _STANDALONE
|
|
|
|
bi_disk.biosdev = d->ll.dev;
|
|
|
|
bi_disk.partition = partition;
|
1997-10-13 13:26:29 +04:00
|
|
|
bi_disk.labelsector = -1;
|
1998-05-15 20:38:53 +04:00
|
|
|
#endif
|
1997-10-13 13:26:29 +04:00
|
|
|
|
|
|
|
#ifndef NO_DISKLABEL
|
|
|
|
if (!(d->ll.dev & 0x80) /* floppy */
|
|
|
|
|| partition == RAW_PART)
|
|
|
|
goto nolabel;
|
|
|
|
|
1997-03-22 04:41:34 +03:00
|
|
|
/*
|
1997-10-13 13:26:29 +04:00
|
|
|
* find NetBSD Partition in DOS partition table
|
|
|
|
* XXX check magic???
|
1997-03-22 04:41:34 +03:00
|
|
|
*/
|
|
|
|
if (readsects(&d->ll, 0, 1, d->buf, 0)) {
|
1997-03-14 05:40:32 +03:00
|
|
|
#ifdef DISK_DEBUG
|
1997-10-13 13:26:29 +04:00
|
|
|
printf("error reading MBR\n");
|
1997-03-14 05:40:32 +03:00
|
|
|
#endif
|
1997-03-22 04:41:34 +03:00
|
|
|
error = EIO;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
sector = -1;
|
1999-01-27 23:54:56 +03:00
|
|
|
dptr = (struct mbr_partition *) & d->buf[MBR_PARTOFF];
|
1998-02-19 17:12:48 +03:00
|
|
|
/* Look for NetBSD partition ID */
|
1999-01-27 23:54:56 +03:00
|
|
|
for (i = 0; i < NMBRPART; i++, dptr++)
|
|
|
|
if (dptr->mbrp_typ == MBR_PTYPE_NETBSD) {
|
|
|
|
sector = dptr->mbrp_start;
|
1997-03-22 04:41:34 +03:00
|
|
|
break;
|
|
|
|
}
|
1998-02-19 17:12:48 +03:00
|
|
|
#ifdef COMPAT_386BSD_MBRPART
|
|
|
|
if (sector == -1) {
|
|
|
|
/* If we didn't find one, look for 386BSD partition ID */
|
1999-01-27 23:54:56 +03:00
|
|
|
dptr = (struct mbr_partition *) & d->buf[MBR_PARTOFF];
|
|
|
|
for (i = 0; i < NMBRPART; i++, dptr++)
|
|
|
|
if (dptr->mbrp_typ == MBR_PTYPE_386BSD) {
|
1998-02-19 17:12:48 +03:00
|
|
|
printf("old BSD partition ID!\n");
|
1999-01-27 23:54:56 +03:00
|
|
|
sector = dptr->mbrp_start;
|
1998-02-19 17:12:48 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
1997-03-22 04:41:34 +03:00
|
|
|
if (sector == -1) {
|
|
|
|
/*
|
|
|
|
* One of two things:
|
|
|
|
* 1. no MBR
|
|
|
|
* 2. no NetBSD partition in MBR
|
|
|
|
*
|
|
|
|
* We simply default to "start of disk" in this case and
|
|
|
|
* press on.
|
|
|
|
*/
|
|
|
|
sector = 0;
|
|
|
|
}
|
1997-10-13 13:26:29 +04:00
|
|
|
|
1997-03-22 04:41:34 +03:00
|
|
|
/* find partition in NetBSD disklabel */
|
|
|
|
if (readsects(&d->ll, sector + LABELSECTOR, 1, d->buf, 0)) {
|
1997-03-14 05:40:32 +03:00
|
|
|
#ifdef DISK_DEBUG
|
1997-03-22 04:41:34 +03:00
|
|
|
printf("Error reading disklabel\n");
|
1997-03-14 05:40:32 +03:00
|
|
|
#endif
|
1997-03-22 04:41:34 +03:00
|
|
|
error = EIO;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
lp = (struct disklabel *) (d->buf + LABELOFFSET);
|
|
|
|
if (lp->d_magic != DISKMAGIC) {
|
1997-03-14 05:40:32 +03:00
|
|
|
#ifdef DISK_DEBUG
|
1997-03-22 04:41:34 +03:00
|
|
|
printf("warning: no disklabel\n");
|
1997-03-14 05:40:32 +03:00
|
|
|
#endif
|
1997-03-22 04:41:34 +03:00
|
|
|
d->boff = sector;
|
|
|
|
} else if (partition >= lp->d_npartitions ||
|
|
|
|
lp->d_partitions[partition].p_fstype == FS_UNUSED) {
|
1997-03-14 05:40:32 +03:00
|
|
|
#ifdef DISK_DEBUG
|
1997-03-22 04:41:34 +03:00
|
|
|
printf("illegal partition\n");
|
1997-03-14 05:40:32 +03:00
|
|
|
#endif
|
1997-03-22 04:41:34 +03:00
|
|
|
error = EPART;
|
|
|
|
goto out;
|
|
|
|
} else {
|
|
|
|
d->boff = lp->d_partitions[partition].p_offset;
|
2000-10-30 10:30:59 +03:00
|
|
|
if (lp->d_partitions[partition].p_fstype == FS_RAID)
|
|
|
|
d->boff += RF_PROTECTED_SECTORS;
|
1998-05-15 20:38:53 +04:00
|
|
|
#ifdef _STANDALONE
|
|
|
|
bi_disk.labelsector = sector + LABELSECTOR;
|
|
|
|
bi_disk.label.type = lp->d_type;
|
2001-07-08 02:57:57 +04:00
|
|
|
memcpy(bi_disk.label.packname, lp->d_packname, 16);
|
1997-09-17 22:08:13 +04:00
|
|
|
bi_disk.label.checksum = lp->d_checksum;
|
1998-05-15 20:38:53 +04:00
|
|
|
#endif
|
1997-03-22 04:41:34 +03:00
|
|
|
}
|
1997-10-13 13:26:29 +04:00
|
|
|
nolabel:
|
|
|
|
#endif /* NO_DISKLABEL */
|
1997-03-14 05:40:32 +03:00
|
|
|
|
|
|
|
#ifdef DISK_DEBUG
|
1997-03-22 04:41:34 +03:00
|
|
|
printf("partition @%d\n", d->boff);
|
1997-03-14 05:40:32 +03:00
|
|
|
#endif
|
|
|
|
|
1998-05-15 20:38:53 +04:00
|
|
|
#ifdef _STANDALONE
|
1997-09-17 22:08:13 +04:00
|
|
|
BI_ADD(&bi_disk, BTINFO_BOOTDISK, sizeof(bi_disk));
|
1998-05-15 20:38:53 +04:00
|
|
|
#endif
|
1997-09-17 22:08:13 +04:00
|
|
|
|
1997-03-22 04:41:34 +03:00
|
|
|
f->f_devdata = d;
|
1997-03-14 05:40:32 +03:00
|
|
|
out:
|
1997-09-17 22:08:13 +04:00
|
|
|
va_end(ap);
|
1997-03-22 04:41:34 +03:00
|
|
|
if (error)
|
|
|
|
free(d, sizeof(struct biosdisk));
|
|
|
|
return (error);
|
1997-03-14 05:40:32 +03:00
|
|
|
}
|
|
|
|
|
1999-08-03 23:46:22 +04:00
|
|
|
#ifndef LIBSA_NO_FS_CLOSE
|
1997-03-22 04:41:34 +03:00
|
|
|
int
|
|
|
|
biosdiskclose(f)
|
|
|
|
struct open_file *f;
|
1997-03-14 05:40:32 +03:00
|
|
|
{
|
1997-03-22 04:41:34 +03:00
|
|
|
struct biosdisk *d = f->f_devdata;
|
1997-03-14 05:40:32 +03:00
|
|
|
|
1997-03-22 04:41:34 +03:00
|
|
|
if (!(d->ll.dev & 0x80))/* let the floppy drive go off */
|
|
|
|
delay(3000000); /* 2s is enough on all PCs I found */
|
1997-03-14 05:40:32 +03:00
|
|
|
|
1997-03-22 04:41:34 +03:00
|
|
|
free(d, sizeof(struct biosdisk));
|
|
|
|
f->f_devdata = NULL;
|
|
|
|
return (0);
|
1997-03-14 05:40:32 +03:00
|
|
|
}
|
1999-08-03 23:46:22 +04:00
|
|
|
#endif
|
1997-03-14 05:40:32 +03:00
|
|
|
|
1997-03-22 04:41:34 +03:00
|
|
|
int
|
|
|
|
biosdiskioctl(f, cmd, arg)
|
|
|
|
struct open_file *f;
|
|
|
|
u_long cmd;
|
|
|
|
void *arg;
|
1997-03-14 05:40:32 +03:00
|
|
|
{
|
1997-03-22 04:41:34 +03:00
|
|
|
return EIO;
|
1997-03-14 05:40:32 +03:00
|
|
|
}
|