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