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