Home | History | Annotate | Line # | Download | only in lib
biosdisk.c revision 1.17
      1  1.17       dsl /*	$NetBSD: biosdisk.c,v 1.17 2003/07/07 13:32:42 dsl Exp $	*/
      2   1.1     perry 
      3   1.1     perry /*
      4   1.8  drochner  * Copyright (c) 1996, 1998
      5   1.1     perry  *	Matthias Drochner.  All rights reserved.
      6   1.1     perry  *
      7   1.1     perry  * Redistribution and use in source and binary forms, with or without
      8   1.1     perry  * modification, are permitted provided that the following conditions
      9   1.1     perry  * are met:
     10   1.1     perry  * 1. Redistributions of source code must retain the above copyright
     11   1.1     perry  *    notice, this list of conditions and the following disclaimer.
     12   1.1     perry  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1     perry  *    notice, this list of conditions and the following disclaimer in the
     14   1.1     perry  *    documentation and/or other materials provided with the distribution.
     15   1.1     perry  * 3. All advertising materials mentioning features or use of this software
     16   1.1     perry  *    must display the following acknowledgement:
     17   1.1     perry  *	This product includes software developed for the NetBSD Project
     18   1.1     perry  *	by Matthias Drochner.
     19   1.1     perry  * 4. The name of the author may not be used to endorse or promote products
     20   1.1     perry  *    derived from this software without specific prior written permission.
     21   1.1     perry  *
     22   1.1     perry  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23   1.1     perry  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24   1.1     perry  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25   1.1     perry  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26   1.1     perry  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27   1.1     perry  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28   1.1     perry  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29   1.1     perry  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30   1.1     perry  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31   1.1     perry  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32   1.1     perry  *
     33   1.1     perry  */
     34   1.1     perry 
     35   1.3   thorpej /*
     36   1.4  drochner  * raw BIOS disk device for libsa.
     37   1.4  drochner  * needs lowlevel parts from bios_disk.S and biosdisk_ll.c
     38   1.4  drochner  * partly from netbsd:sys/arch/i386/boot/disk.c
     39   1.4  drochner  * no bad144 handling!
     40  1.17       dsl  *
     41  1.17       dsl  * A lot of this must match sys/kern/subr_disk_mbr.c
     42   1.1     perry  */
     43   1.1     perry 
     44   1.1     perry /*
     45   1.1     perry  * Ported to boot 386BSD by Julian Elischer (julian (at) tfs.com) Sept 1992
     46   1.1     perry  *
     47   1.1     perry  * Mach Operating System
     48   1.1     perry  * Copyright (c) 1992, 1991 Carnegie Mellon University
     49   1.1     perry  * All Rights Reserved.
     50   1.3   thorpej  *
     51   1.1     perry  * Permission to use, copy, modify and distribute this software and its
     52   1.1     perry  * documentation is hereby granted, provided that both the copyright
     53   1.1     perry  * notice and this permission notice appear in all copies of the
     54   1.1     perry  * software, derivative works or modified versions, and any portions
     55   1.1     perry  * thereof, and that both notices appear in supporting documentation.
     56   1.3   thorpej  *
     57   1.1     perry  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     58   1.1     perry  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
     59   1.1     perry  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     60   1.3   thorpej  *
     61   1.1     perry  * Carnegie Mellon requests users of this software to return to
     62   1.3   thorpej  *
     63   1.1     perry  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     64   1.1     perry  *  School of Computer Science
     65   1.1     perry  *  Carnegie Mellon University
     66   1.1     perry  *  Pittsburgh PA 15213-3890
     67   1.3   thorpej  *
     68   1.1     perry  * any improvements or extensions that they make and grant Carnegie Mellon
     69   1.1     perry  * the rights to redistribute these changes.
     70   1.1     perry  */
     71   1.1     perry 
     72   1.1     perry #include <sys/types.h>
     73   1.1     perry #include <sys/disklabel.h>
     74   1.1     perry 
     75   1.1     perry #include <lib/libsa/stand.h>
     76   1.1     perry #include <lib/libsa/saerrno.h>
     77   1.5  drochner #include <machine/stdarg.h>
     78   1.1     perry 
     79   1.1     perry #include "libi386.h"
     80   1.1     perry #include "biosdisk_ll.h"
     81   1.5  drochner #include "biosdisk.h"
     82   1.9  drochner #ifdef _STANDALONE
     83   1.5  drochner #include "bootinfo.h"
     84   1.9  drochner #endif
     85   1.1     perry 
     86  1.16       dsl extern void memset(void *, int, size_t);
     87  1.16       dsl 
     88   1.1     perry #define BUFSIZE (1 * BIOSDISK_SECSIZE)
     89   1.1     perry 
     90   1.3   thorpej struct biosdisk {
     91   1.3   thorpej 	struct biosdisk_ll ll;
     92   1.3   thorpej 	int             boff;
     93   1.3   thorpej 	char            buf[BUFSIZE];
     94   1.1     perry };
     95   1.1     perry 
     96   1.9  drochner #ifdef _STANDALONE
     97   1.5  drochner static struct btinfo_bootdisk bi_disk;
     98   1.9  drochner #endif
     99   1.5  drochner 
    100  1.13     lukem #define	RF_PROTECTED_SECTORS	64	/* XXX refer to <.../rf_optnames.h> */
    101  1.13     lukem 
    102  1.16       dsl int boot_biossector;	/* disk sector partition might have started in */
    103  1.16       dsl 
    104   1.3   thorpej int
    105   1.3   thorpej biosdiskstrategy(devdata, flag, dblk, size, buf, rsize)
    106   1.3   thorpej 	void           *devdata;
    107   1.3   thorpej 	int             flag;
    108   1.3   thorpej 	daddr_t         dblk;
    109   1.3   thorpej 	size_t          size;
    110   1.3   thorpej 	void           *buf;
    111   1.3   thorpej 	size_t         *rsize;
    112   1.1     perry {
    113   1.3   thorpej 	struct biosdisk *d;
    114   1.3   thorpej 	int             blks, frag;
    115   1.1     perry 
    116   1.3   thorpej 	if (flag != F_READ)
    117   1.3   thorpej 		return (EROFS);
    118   1.1     perry 
    119   1.3   thorpej 	d = (struct biosdisk *) devdata;
    120   1.1     perry 
    121   1.3   thorpej 	dblk += d->boff;
    122   1.3   thorpej 
    123   1.3   thorpej 	blks = size / BIOSDISK_SECSIZE;
    124   1.3   thorpej 	if (blks && readsects(&d->ll, dblk, blks, buf, 0)) {
    125   1.3   thorpej 		if (rsize)
    126   1.3   thorpej 			*rsize = 0;
    127   1.3   thorpej 		return (EIO);
    128   1.3   thorpej 	}
    129   1.3   thorpej 	/* do we really need this? */
    130   1.3   thorpej 	frag = size % BIOSDISK_SECSIZE;
    131   1.3   thorpej 	if (frag) {
    132   1.3   thorpej 		if (readsects(&d->ll, dblk + blks, 1, d->buf, 0)) {
    133   1.3   thorpej 			if (rsize)
    134   1.3   thorpej 				*rsize = blks * BIOSDISK_SECSIZE;
    135   1.3   thorpej 			return (EIO);
    136   1.3   thorpej 		}
    137  1.15     perry 		memcpy(buf + blks * BIOSDISK_SECSIZE, d->buf, frag);
    138   1.3   thorpej 	}
    139   1.3   thorpej 	if (rsize)
    140   1.3   thorpej 		*rsize = size;
    141   1.3   thorpej 	return (0);
    142   1.1     perry }
    143   1.1     perry 
    144  1.16       dsl static struct biosdisk *
    145  1.16       dsl alloc_biosdisk(int dev)
    146   1.1     perry {
    147   1.3   thorpej 	struct biosdisk *d;
    148   1.1     perry 
    149  1.16       dsl 	d = (struct biosdisk *)alloc(sizeof *d);
    150  1.16       dsl 	if (!d)
    151  1.16       dsl 		return NULL;
    152  1.16       dsl 	memset(d, 0, sizeof *d);
    153  1.16       dsl 
    154  1.16       dsl 	d->ll.dev = dev;;
    155  1.11      fvdl 	if (set_geometry(&d->ll, NULL)) {
    156   1.1     perry #ifdef DISK_DEBUG
    157   1.3   thorpej 		printf("no geometry information\n");
    158   1.1     perry #endif
    159  1.16       dsl 		free(d, sizeof *d);
    160  1.16       dsl 		return NULL;
    161   1.3   thorpej 	}
    162  1.16       dsl 	return d;
    163  1.16       dsl }
    164  1.16       dsl 
    165  1.16       dsl #ifndef NO_DISKLABEL
    166  1.16       dsl static int
    167  1.17       dsl check_label(struct biosdisk *d, int sector)
    168  1.17       dsl {
    169  1.17       dsl 	struct disklabel *lp;
    170  1.17       dsl 
    171  1.17       dsl 	/* find partition in NetBSD disklabel */
    172  1.17       dsl 	if (readsects(&d->ll, sector + LABELSECTOR, 1, d->buf, 0)) {
    173  1.17       dsl #ifdef DISK_DEBUG
    174  1.17       dsl 		printf("Error reading disklabel\n");
    175  1.17       dsl #endif
    176  1.17       dsl 		return EIO;
    177  1.17       dsl 	}
    178  1.17       dsl 	lp = (struct disklabel *) (d->buf + LABELOFFSET);
    179  1.17       dsl 	if (lp->d_magic != DISKMAGIC || dkcksum(lp)) {
    180  1.17       dsl #ifdef DISK_DEBUG
    181  1.17       dsl 		printf("warning: no disklabel\n");
    182  1.17       dsl #endif
    183  1.17       dsl 		return -1;
    184  1.17       dsl 	}
    185  1.17       dsl 
    186  1.17       dsl 	d->boff = sector;
    187  1.17       dsl 	return 0;
    188  1.17       dsl }
    189  1.17       dsl 
    190  1.17       dsl static int
    191  1.16       dsl read_label(struct biosdisk *d)
    192  1.16       dsl {
    193  1.17       dsl 	struct disklabel dflt_lbl;
    194  1.17       dsl 	struct mbr_partition mbr[NMBRPART];
    195  1.17       dsl 	struct partition *p;
    196  1.16       dsl 	int sector, i;
    197  1.17       dsl 	int error;
    198  1.17       dsl 	int typ;
    199  1.17       dsl 	int ext_base, this_ext, next_ext;
    200  1.17       dsl #ifdef COMPAT_386BSD_MBRPART
    201  1.17       dsl 	int sector_386bsd = -1;
    202  1.17       dsl #endif
    203  1.17       dsl 
    204  1.17       dsl 	memset(&dflt_lbl, 0, sizeof dflt_lbl);
    205  1.17       dsl 	dflt_lbl.d_npartitions = 8;
    206   1.5  drochner 
    207   1.7  drochner 	d->boff = 0;
    208   1.7  drochner 
    209  1.16       dsl 	if (!(d->ll.dev & 0x80)) /* floppy */
    210  1.16       dsl 		/* No label on floppy */
    211  1.16       dsl 		return -1;
    212   1.7  drochner 
    213   1.3   thorpej 	/*
    214   1.7  drochner 	 * find NetBSD Partition in DOS partition table
    215   1.7  drochner 	 * XXX check magic???
    216   1.3   thorpej 	 */
    217  1.17       dsl 	ext_base = 0;
    218  1.17       dsl 	next_ext = 0;
    219  1.17       dsl 	for (;;) {
    220  1.17       dsl 		this_ext = ext_base + next_ext;
    221  1.17       dsl 		next_ext = 0;
    222  1.17       dsl 		if (readsects(&d->ll, this_ext, 1, d->buf, 0)) {
    223   1.1     perry #ifdef DISK_DEBUG
    224  1.17       dsl 			printf("error reading MBR sector %d\n", this_ext);
    225  1.17       dsl #endif
    226  1.17       dsl 			return EIO;
    227  1.17       dsl 		}
    228  1.17       dsl 		memcpy(&mbr, ((mbr_sector_t *)d->buf)->mbr_parts, sizeof mbr);
    229  1.17       dsl 		/* Look for NetBSD partition ID */
    230  1.17       dsl 		for (i = 0; i < NMBRPART; i++) {
    231  1.17       dsl 			typ = mbr[i].mbrp_typ;
    232  1.17       dsl 			if (typ == 0)
    233  1.17       dsl 				continue;
    234  1.17       dsl 			sector = this_ext + mbr[i].mbrp_start;
    235  1.17       dsl 			if (typ == MBR_PTYPE_NETBSD) {
    236  1.17       dsl 				error = check_label(d, sector);
    237  1.17       dsl 				if (error >= 0)
    238  1.17       dsl 					return error;
    239  1.17       dsl 			}
    240  1.17       dsl 			if (MBR_IS_EXTENDED(typ)) {
    241  1.17       dsl 				next_ext = mbr[i].mbrp_start;
    242  1.17       dsl 				continue;
    243  1.17       dsl 			}
    244  1.17       dsl #ifdef COMPAT_386BSD_MBRPART
    245  1.17       dsl 			if (this_ext == 0 && typ == MBR_PTYPE_386BSD)
    246  1.17       dsl 				sector_386bsd = sector;
    247   1.1     perry #endif
    248  1.17       dsl 			if (this_ext != 0) {
    249  1.17       dsl 				if (dflt_lbl.d_npartitions >= MAXPARTITIONS)
    250  1.17       dsl 					continue;
    251  1.17       dsl 				p = &dflt_lbl.d_partitions[dflt_lbl.d_npartitions++];
    252  1.17       dsl 			} else
    253  1.17       dsl 				p = &dflt_lbl.d_partitions[i];
    254  1.17       dsl 			p->p_offset = sector;
    255  1.17       dsl 			p->p_size = mbr[i].mbrp_size;
    256  1.17       dsl 			p->p_fstype = xlat_mbr_fstype(typ);
    257  1.17       dsl 		}
    258  1.17       dsl 		if (next_ext == 0)
    259   1.3   thorpej 			break;
    260  1.17       dsl 		if (ext_base == 0) {
    261  1.17       dsl 			ext_base = next_ext;
    262  1.17       dsl 			next_ext = 0;
    263   1.3   thorpej 		}
    264  1.17       dsl 	}
    265  1.17       dsl 
    266  1.17       dsl 	sector = 0;
    267   1.8  drochner #ifdef COMPAT_386BSD_MBRPART
    268  1.17       dsl 	if (sector_386bsd != -1) {
    269  1.17       dsl 		printf("old BSD partition ID!\n");
    270  1.17       dsl 		sector = sector_386bsd;
    271   1.8  drochner 	}
    272   1.8  drochner #endif
    273   1.7  drochner 
    274  1.17       dsl 	/*
    275  1.17       dsl 	 * One of two things:
    276  1.17       dsl 	 * 	1. no MBR
    277  1.17       dsl 	 *	2. no NetBSD partition in MBR
    278  1.17       dsl 	 *
    279  1.17       dsl 	 * We simply default to "start of disk" in this case and
    280  1.17       dsl 	 * press on.
    281  1.17       dsl 	 */
    282  1.17       dsl 	error = check_label(d, sector);
    283  1.17       dsl 	if (error >= 0)
    284  1.17       dsl 		return error;
    285  1.16       dsl 
    286  1.17       dsl 	/*
    287  1.17       dsl 	 * Nothing at start of disk, return info from mbr partitions.
    288  1.17       dsl 	 */
    289  1.17       dsl 	/* XXX fill it to make checksum match kernel one */
    290  1.17       dsl 	dflt_lbl.d_checksum = dkcksum(&dflt_lbl);
    291  1.17       dsl 	memcpy(d->buf, &dflt_lbl, sizeof dflt_lbl);
    292  1.17       dsl 	return -1;
    293  1.16       dsl }
    294  1.16       dsl #endif /* NO_DISKLABEL */
    295  1.16       dsl 
    296  1.16       dsl /* Determine likely partition for possible sector number of dos
    297  1.17       dsl  * partition.
    298  1.17       dsl  */
    299  1.16       dsl 
    300  1.16       dsl u_int
    301  1.16       dsl biosdiskfindptn(int biosdev, u_int sector)
    302  1.16       dsl {
    303  1.16       dsl #ifdef NO_DISKLABEL
    304  1.16       dsl 	return 0;
    305  1.16       dsl #else
    306  1.16       dsl 	struct biosdisk *d;
    307  1.16       dsl 	u_int partition = 0;
    308  1.16       dsl 	struct disklabel *lp;
    309  1.16       dsl 
    310  1.16       dsl 	/* Look for netbsd partition that is the dos boot one */
    311  1.16       dsl 	d = alloc_biosdisk(biosdev);
    312  1.17       dsl 	if (d == NULL)
    313  1.17       dsl 		return 0;
    314  1.17       dsl 
    315  1.17       dsl 	if (read_label(d) == 0) {
    316  1.16       dsl 		lp = (struct disklabel *)(d->buf + LABELOFFSET);
    317  1.16       dsl 		for (partition = lp->d_npartitions; --partition;){
    318  1.16       dsl 			if (lp->d_partitions[partition].p_fstype == FS_UNUSED)
    319  1.16       dsl 				continue;
    320  1.16       dsl 			if (lp->d_partitions[partition].p_offset == sector)
    321  1.16       dsl 				break;
    322  1.16       dsl 		}
    323  1.16       dsl 	}
    324  1.16       dsl 
    325  1.17       dsl 	free(d, sizeof *d);
    326  1.16       dsl 	return partition;
    327  1.16       dsl }
    328  1.16       dsl #endif /* NO_DISKLABEL */
    329  1.16       dsl 
    330  1.16       dsl int
    331  1.16       dsl biosdiskopen(struct open_file *f, ...)
    332  1.16       dsl /* file, biosdev, partition */
    333  1.16       dsl {
    334  1.16       dsl 	va_list ap;
    335  1.16       dsl 	struct biosdisk *d;
    336  1.16       dsl 	int partition;
    337  1.16       dsl #ifndef NO_DISKLABEL
    338  1.16       dsl 	struct disklabel *lp;
    339  1.16       dsl #endif
    340  1.16       dsl 	int error = 0;
    341  1.16       dsl 
    342  1.16       dsl 	va_start(ap, f);
    343  1.16       dsl 	d = alloc_biosdisk(va_arg(ap, int));
    344  1.16       dsl 	if (!d) {
    345  1.16       dsl 		error = ENXIO;
    346  1.16       dsl 		goto out;
    347  1.16       dsl 	}
    348  1.16       dsl 
    349  1.16       dsl 	partition = va_arg(ap, int);
    350  1.16       dsl #ifdef _STANDALONE
    351  1.16       dsl 	bi_disk.biosdev = d->ll.dev;
    352  1.16       dsl 	bi_disk.partition = partition;
    353  1.16       dsl 	bi_disk.labelsector = -1;
    354  1.16       dsl #endif
    355  1.16       dsl 
    356  1.16       dsl #ifndef NO_DISKLABEL
    357  1.16       dsl 	if (partition == RAW_PART)
    358  1.16       dsl 		goto nolabel;
    359  1.16       dsl 	error = read_label(d);
    360  1.16       dsl 	if (error == -1) {
    361  1.16       dsl 		error = 0;
    362  1.16       dsl 		goto nolabel;
    363  1.16       dsl 	}
    364  1.16       dsl 	if (error)
    365  1.16       dsl 		goto out;
    366  1.16       dsl 
    367  1.16       dsl 	lp = (struct disklabel *) (d->buf + LABELOFFSET);
    368  1.16       dsl 	if (partition >= lp->d_npartitions ||
    369  1.16       dsl 	   lp->d_partitions[partition].p_fstype == FS_UNUSED) {
    370   1.1     perry #ifdef DISK_DEBUG
    371   1.3   thorpej 		printf("illegal partition\n");
    372   1.1     perry #endif
    373   1.3   thorpej 		error = EPART;
    374   1.3   thorpej 		goto out;
    375  1.16       dsl 	}
    376   1.9  drochner #ifdef _STANDALONE
    377  1.16       dsl 	bi_disk.labelsector = d->boff + LABELSECTOR;
    378  1.16       dsl 	bi_disk.label.type = lp->d_type;
    379  1.16       dsl 	memcpy(bi_disk.label.packname, lp->d_packname, 16);
    380  1.16       dsl 	bi_disk.label.checksum = lp->d_checksum;
    381  1.16       dsl #endif
    382  1.16       dsl 	d->boff = lp->d_partitions[partition].p_offset;
    383  1.16       dsl 	if (lp->d_partitions[partition].p_fstype == FS_RAID)
    384  1.16       dsl 		d->boff += RF_PROTECTED_SECTORS;
    385   1.7  drochner nolabel:
    386   1.7  drochner #endif /* NO_DISKLABEL */
    387   1.1     perry 
    388   1.1     perry #ifdef DISK_DEBUG
    389   1.3   thorpej 	printf("partition @%d\n", d->boff);
    390   1.1     perry #endif
    391   1.1     perry 
    392   1.9  drochner #ifdef _STANDALONE
    393   1.5  drochner 	BI_ADD(&bi_disk, BTINFO_BOOTDISK, sizeof(bi_disk));
    394   1.9  drochner #endif
    395   1.5  drochner 
    396   1.3   thorpej 	f->f_devdata = d;
    397   1.1     perry out:
    398   1.5  drochner         va_end(ap);
    399   1.3   thorpej 	if (error)
    400   1.3   thorpej 		free(d, sizeof(struct biosdisk));
    401   1.3   thorpej 	return (error);
    402   1.1     perry }
    403   1.1     perry 
    404  1.12  drochner #ifndef LIBSA_NO_FS_CLOSE
    405   1.3   thorpej int
    406   1.3   thorpej biosdiskclose(f)
    407   1.3   thorpej 	struct open_file *f;
    408   1.1     perry {
    409   1.3   thorpej 	struct biosdisk *d = f->f_devdata;
    410   1.1     perry 
    411   1.3   thorpej 	if (!(d->ll.dev & 0x80))/* let the floppy drive go off */
    412   1.3   thorpej 		delay(3000000);	/* 2s is enough on all PCs I found */
    413   1.1     perry 
    414   1.3   thorpej 	free(d, sizeof(struct biosdisk));
    415   1.3   thorpej 	f->f_devdata = NULL;
    416   1.3   thorpej 	return (0);
    417   1.1     perry }
    418  1.12  drochner #endif
    419   1.1     perry 
    420   1.3   thorpej int
    421   1.3   thorpej biosdiskioctl(f, cmd, arg)
    422   1.3   thorpej 	struct open_file *f;
    423   1.3   thorpej 	u_long          cmd;
    424   1.3   thorpej 	void           *arg;
    425   1.1     perry {
    426   1.3   thorpej 	return EIO;
    427   1.1     perry }
    428