Home | History | Annotate | Line # | Download | only in kern
subr_disk_mbr.c revision 1.2
      1 /*	$NetBSD: subr_disk_mbr.c,v 1.2 2003/08/07 16:31:53 agc Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
      5  * 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  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  *
     31  *	@(#)ufs_disksubr.c	7.16 (Berkeley) 5/4/91
     32  */
     33 
     34 /*
     35  * Code to find a NetBSD label on a disk that contains an i386 style MBR.
     36  * The first NetBSD label found in the 2nd sector of a NetBSD partition
     37  * is used.  If there isn't a NetBSD partition then a label is faked up
     38  * from the MBR.
     39  *
     40  * If there isn't a disklabel or anything in the MBR then partition a
     41  * is set to cover the whole disk.
     42  * Useful for files that contain single filesystems (etc).
     43  *
     44  * This code will read host endian netbsd labels from little endian MBR.
     45  *
     46  * Based on the i386 disksubr.c
     47  *
     48  * XXX There are potential problems writing labels to disks where there
     49  * is only space for 8 netbsd partitions but this code has been compiled
     50  * with MAXPARTITIONS=16.
     51  */
     52 
     53 #include <sys/cdefs.h>
     54 __KERNEL_RCSID(0, "$NetBSD: subr_disk_mbr.c,v 1.2 2003/08/07 16:31:53 agc Exp $");
     55 
     56 #include <sys/param.h>
     57 #include <sys/systm.h>
     58 #include <sys/buf.h>
     59 #include <sys/disklabel.h>
     60 #include <sys/disk.h>
     61 #include <sys/syslog.h>
     62 
     63 #include "opt_mbr.h"
     64 
     65 #define MBR_LABELSECTOR	1
     66 
     67 #define SCAN_CONTINUE	0
     68 #define SCAN_FOUND	1
     69 #define SCAN_ERROR	2
     70 
     71 typedef struct mbr_args {
     72 	struct disklabel *lp;
     73 	void		(*strat)(struct buf *);
     74 	struct buf	*bp;
     75 	const char	*msg;
     76 	int		error;
     77 } mbr_args_t;
     78 
     79 #define READ_LABEL	1
     80 #define WRITE_LABEL	2
     81 static int validate_label(mbr_args_t *, uint, int);
     82 static int look_netbsd_part(mbr_args_t *, mbr_partition_t *, int, uint);
     83 static int write_netbsd_label(mbr_args_t *, mbr_partition_t *, int, uint);
     84 
     85 static int
     86 read_sector(mbr_args_t *a, uint sector)
     87 {
     88 	struct buf *bp = a->bp;
     89 	int error;
     90 
     91 	bp->b_blkno = sector;
     92 	bp->b_bcount = a->lp->d_secsize;
     93 	bp->b_flags = (bp->b_flags & ~(B_WRITE | B_DONE)) | B_READ;
     94 	bp->b_cylinder = sector / a->lp->d_secpercyl;
     95 	(*a->strat)(bp);
     96 	error = biowait(bp);
     97 	if (error != 0)
     98 		a->error = error;
     99 	return error;
    100 }
    101 
    102 /*
    103  * Scan MBR for partitions, call 'action' routine for each.
    104  */
    105 
    106 static int
    107 scan_mbr(mbr_args_t *a, int (*actn)(mbr_args_t *, mbr_partition_t *, int, uint))
    108 {
    109 	mbr_partition_t ptns[NMBRPART];
    110 	mbr_partition_t *dp;
    111 	mbr_sector_t *mbr;
    112 	uint ext_base, this_ext, next_ext;
    113 	int rval;
    114 	int i;
    115 #ifdef COMPAT_386BSD_MBRPART
    116 	int dp_386bsd = -1;
    117 #endif
    118 
    119 	ext_base = 0;
    120 	this_ext = 0;
    121 	for (;;) {
    122 		if (read_sector(a, this_ext)) {
    123 			a->msg = "dos partition I/O error";
    124 			return SCAN_ERROR;
    125 		}
    126 
    127 		/* Note: Magic number is little-endian. */
    128 		mbr = (void *)a->bp->b_data;
    129 		if (mbr->mbr_signature != htole16(MBR_MAGIC)) {
    130 			/* Don't set an error message here.... */
    131 			return SCAN_ERROR;
    132 		}
    133 
    134 		/* Copy data out of buffer so action can use bp */
    135 		memcpy(ptns, &mbr->mbr_parts, sizeof ptns);
    136 
    137 		/* look for NetBSD partition */
    138 		next_ext = 0;
    139 		dp = ptns;
    140 		for (i = 0; i < NMBRPART; i++, dp++) {
    141 			if (dp->mbrp_typ == 0)
    142 				continue;
    143 			if (MBR_IS_EXTENDED(dp->mbrp_typ)) {
    144 				next_ext = le32toh(dp->mbrp_start);
    145 				continue;
    146 			}
    147 #ifdef COMPAT_386BSD_MBRPART
    148 			if (dp->mbrp_typ == MBR_PTYPE_386BSD) {
    149 				/*
    150 				 * If more than one matches, take last,
    151 				 * as NetBSD install tool does.
    152 				 */
    153 				if (this_ext == 0)
    154 					dp_386bsd = i;
    155 				continue;
    156 			}
    157 #endif
    158 			rval = (*actn)(a, dp, i, this_ext);
    159 			if (rval != SCAN_CONTINUE)
    160 				return rval;
    161 		}
    162 		if (next_ext == 0)
    163 			break;
    164 		if (ext_base == 0) {
    165 			ext_base = next_ext;
    166 			next_ext = 0;
    167 		}
    168 		next_ext += ext_base;
    169 		if (next_ext <= this_ext)
    170 			break;
    171 		this_ext = next_ext;
    172 	}
    173 #ifdef COMPAT_386BSD_MBRPART
    174 	if (this_ext == 0 && dp_386bsd != -1)
    175 		return (*actn)(a, &ptns[dp_386bsd], dp_386bsd, 0);
    176 #endif
    177 	return SCAN_CONTINUE;
    178 }
    179 
    180 /*
    181  * Attempt to read a disk label from a device
    182  * using the indicated strategy routine.
    183  * The label must be partly set up before this:
    184  * secpercyl, secsize and anything required for a block i/o read
    185  * operation in the driver's strategy/start routines
    186  * must be filled in before calling us.
    187  *
    188  * If dos partition table requested, attempt to load it and
    189  * find disklabel inside a DOS partition. Also, if bad block
    190  * table needed, attempt to extract it as well. Return buffer
    191  * for use in signalling errors if requested.
    192  *
    193  * Returns null on success and an error string on failure.
    194  */
    195 const char *
    196 readdisklabel(dev, strat, lp, osdep)
    197 	dev_t dev;
    198 	void (*strat)(struct buf *);
    199 	struct disklabel *lp;
    200 	struct cpu_disklabel *osdep;
    201 {
    202 	struct dkbad *bdp;
    203 	int rval;
    204 	int i;
    205 	mbr_args_t a;
    206 
    207 	a.lp = lp;
    208 	a.strat = strat;
    209 	a.msg = NULL;
    210 	a.error = 0;
    211 
    212 	/* minimal requirements for architypal disk label */
    213 	if (lp->d_secsize == 0)
    214 		lp->d_secsize = DEV_BSIZE;
    215 	if (lp->d_secperunit == 0)
    216 		lp->d_secperunit = 0x1fffffff;
    217 	lp->d_npartitions = RAW_PART + 1;
    218 	for (i = 0; i < RAW_PART; i++) {
    219 		lp->d_partitions[i].p_size = 0;
    220 		lp->d_partitions[i].p_offset = 0;
    221 	}
    222 	if (lp->d_partitions[RAW_PART].p_size == 0)
    223 		lp->d_partitions[RAW_PART].p_size = 0x1fffffff;
    224 	lp->d_partitions[RAW_PART].p_offset = 0;
    225 
    226 	/*
    227 	 * Set partition 'a' to be the whole disk.
    228 	 * Cleared if we find an mbr or a netbsd label.
    229 	 */
    230 	lp->d_partitions[0].p_size = lp->d_partitions[RAW_PART].p_size;
    231 	lp->d_partitions[0].p_fstype = FS_BSDFFS;
    232 
    233 	/* get a buffer and initialize it */
    234 	a.bp = geteblk((int)lp->d_secsize);
    235 	a.bp->b_dev = dev;
    236 
    237 	if (osdep)
    238 		/*
    239 		 * Scan mbr searching for netbsd partition and saving
    240 		 * bios partition information to use if the netbsd one
    241 		 * is absent.
    242 		 */
    243 		rval = scan_mbr(&a, look_netbsd_part);
    244 	else
    245 		rval = SCAN_CONTINUE;
    246 
    247 	if (rval == SCAN_CONTINUE || (rval == SCAN_ERROR && a.msg == NULL)) {
    248 		/* Look at start of disk */
    249 		rval = validate_label(&a, LABELSECTOR, READ_LABEL);
    250 		if (LABELSECTOR != 0 && rval == SCAN_CONTINUE)
    251 			rval = validate_label(&a, 0, READ_LABEL);
    252 	}
    253 
    254 	/* Obtain bad sector table if requested and present */
    255 	if (rval == SCAN_FOUND && osdep && (lp->d_flags & D_BADSECT)) {
    256 		struct dkbad *db;
    257 		int blkno;
    258 
    259 		bdp = &osdep->bad;
    260 		i = 0;
    261 		rval = SCAN_ERROR;
    262 		do {
    263 			/* read a bad sector table */
    264 			blkno = lp->d_secperunit - lp->d_nsectors + i;
    265 			if (lp->d_secsize > DEV_BSIZE)
    266 				blkno *= lp->d_secsize / DEV_BSIZE;
    267 			else
    268 				blkno /= DEV_BSIZE / lp->d_secsize;
    269 			/* if successful, validate, otherwise try another */
    270 			if (read_sector(&a, blkno)) {
    271 				a.msg = "bad sector table I/O error";
    272 				continue;
    273 			}
    274 			db = (struct dkbad *)(a.bp->b_data);
    275 #define DKBAD_MAGIC 0x4321
    276 			if (db->bt_mbz != 0 || db->bt_flag != DKBAD_MAGIC) {
    277 				a.msg = "bad sector table corrupted";
    278 				continue;
    279 			}
    280 			rval = SCAN_FOUND;
    281 			*bdp = *db;
    282 			break;
    283 		} while ((a.bp->b_flags & B_ERROR) && (i += 2) < 10 &&
    284 			i < lp->d_nsectors);
    285 	}
    286 
    287 	brelse(a.bp);
    288 	if (rval != SCAN_ERROR)
    289 		return NULL;
    290 	return a.msg;
    291 }
    292 
    293 static int
    294 look_netbsd_part(mbr_args_t *a, mbr_partition_t *dp, int slot, uint ext_base)
    295 {
    296 	struct partition *pp;
    297 	int ptn_base = ext_base + le32toh(dp->mbrp_start);
    298 	int rval;
    299 
    300 	if (
    301 #ifdef COMPAT_386BSD_MBRPART
    302 	    dp->mbrp_typ == MBR_PTYPE_386BSD ||
    303 #endif
    304 	    dp->mbrp_typ == MBR_PTYPE_NETBSD) {
    305 		rval = validate_label(a, ptn_base + MBR_LABELSECTOR, READ_LABEL);
    306 
    307 		/* Put actual location where we found the label into ptn 2 */
    308 		if (RAW_PART != 2 && (rval == SCAN_FOUND ||
    309 					a->lp->d_partitions[2].p_size == 0)) {
    310 			a->lp->d_partitions[2].p_size = le32toh(dp->mbrp_size);
    311 			a->lp->d_partitions[2].p_offset = ptn_base;
    312 		}
    313 
    314 		/* If we got a netbsd label look no further */
    315 		if (rval == SCAN_FOUND)
    316 			return rval;
    317 	}
    318 
    319 	/* Install main partitions into e..h and extended into i+ */
    320 	if (ext_base == 0)
    321 		slot += 4;
    322 	else {
    323 		slot = 4 + NMBRPART;
    324 		pp = &a->lp->d_partitions[slot];
    325 		for (; slot < MAXPARTITIONS; pp++, slot++) {
    326 			/* This gets called twice - avoid duplicates */
    327 			if (pp->p_offset == ptn_base &&
    328 			    pp->p_size == le32toh(dp->mbrp_size))
    329 				break;
    330 			if (pp->p_size == 0)
    331 				break;
    332 		}
    333 	}
    334 
    335 	if (slot < MAXPARTITIONS) {
    336 		/* Stop 'a' being the entire disk */
    337 		a->lp->d_partitions[0].p_size = 0;
    338 		a->lp->d_partitions[0].p_fstype = 0;
    339 
    340 		/* save partition info */
    341 		pp = &a->lp->d_partitions[slot];
    342 		pp->p_offset = ptn_base;
    343 		pp->p_size = le32toh(dp->mbrp_size);
    344 		pp->p_fstype = xlat_mbr_fstype(dp->mbrp_typ);
    345 
    346 		if (slot >= a->lp->d_npartitions)
    347 			a->lp->d_npartitions = slot + 1;
    348 	}
    349 
    350 	return SCAN_CONTINUE;
    351 }
    352 
    353 
    354 static int
    355 validate_label(mbr_args_t *a, uint label_sector, int action)
    356 {
    357 	struct disklabel *dlp, *dlp_lim;
    358 	int error;
    359 
    360 	/* Next, dig out disk label */
    361 	if (read_sector(a, label_sector)) {
    362 		a->msg =  "disk label read failed";
    363 		return SCAN_ERROR;
    364 	}
    365 
    366 	/* Locate disk label within block and validate */
    367 	/*
    368 	 * XXX (dsl) This search may be a waste of time, a lot of other i386
    369 	 * code assumes the label is at offset LABELOFFSET (=0) in the sector.
    370 	 *
    371 	 * If we want to support diska from other netbsd ports, then the
    372 	 * code should also allow for a shorter label nearer the end of
    373 	 * the disk sector, and (IIRC) labels within 8k of the disk start.
    374 	 */
    375 	dlp_lim = (void *)(a->bp->b_data + a->lp->d_secsize - sizeof(*dlp));
    376 	for (dlp = (void *)a->bp->b_data; dlp <= dlp_lim;
    377 	    dlp = (void *)((char *)dlp + sizeof(long))) {
    378 		if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC)
    379 			continue;
    380 		if (dlp->d_npartitions > MAXPARTITIONS || dkcksum(dlp) != 0) {
    381 			a->msg = "disk label corrupted";
    382 			continue;
    383 		}
    384 		switch (action) {
    385 		case READ_LABEL:
    386 			*a->lp = *dlp;
    387 			break;
    388 		case WRITE_LABEL:
    389 			*dlp = *a->lp;
    390 			a->bp->b_flags &= ~(B_READ|B_DONE);
    391 			a->bp->b_flags |= B_WRITE;
    392 			(*a->strat)(a->bp);
    393 			error = biowait(a->bp);
    394 			if (error != 0) {
    395 				a->error = error;
    396 				a->msg = "disk label write failed";
    397 				return SCAN_ERROR;
    398 			}
    399 			break;
    400 		}
    401 		return SCAN_FOUND;
    402 	}
    403 
    404 	return SCAN_CONTINUE;
    405 }
    406 
    407 /*
    408  * Check new disk label for sensibility
    409  * before setting it.
    410  */
    411 int
    412 setdisklabel(olp, nlp, openmask, osdep)
    413 	struct disklabel *olp, *nlp;
    414 	u_long openmask;
    415 	struct cpu_disklabel *osdep;
    416 {
    417 	int i;
    418 	struct partition *opp, *npp;
    419 
    420 	/* sanity clause */
    421 	if (nlp->d_secpercyl == 0 || nlp->d_secsize == 0
    422 		|| (nlp->d_secsize % DEV_BSIZE) != 0)
    423 			return (EINVAL);
    424 
    425 	/* special case to allow disklabel to be invalidated */
    426 	if (nlp->d_magic == 0xffffffff) {
    427 		*olp = *nlp;
    428 		return (0);
    429 	}
    430 
    431 	if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC ||
    432 	    dkcksum(nlp) != 0)
    433 		return (EINVAL);
    434 
    435 	/* XXX missing check if other dos partitions will be overwritten */
    436 
    437 	while (openmask != 0) {
    438 		i = ffs(openmask) - 1;
    439 		openmask &= ~(1 << i);
    440 		if (i > nlp->d_npartitions)
    441 			return (EBUSY);
    442 		opp = &olp->d_partitions[i];
    443 		npp = &nlp->d_partitions[i];
    444 		/*
    445 		 * Copy internally-set partition information
    446 		 * if new label doesn't include it.		XXX
    447 		 */
    448 		if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) {
    449 			*npp = *opp;
    450 			continue;
    451 		}
    452 		if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size)
    453 			return (EBUSY);
    454 	}
    455  	nlp->d_checksum = 0;
    456  	nlp->d_checksum = dkcksum(nlp);
    457 	*olp = *nlp;
    458 	return (0);
    459 }
    460 
    461 
    462 /*
    463  * Write disk label back to device after modification.
    464  */
    465 int
    466 writedisklabel(dev, strat, lp, osdep)
    467 	dev_t dev;
    468 	void (*strat)(struct buf *);
    469 	struct disklabel *lp;
    470 	struct cpu_disklabel *osdep;
    471 {
    472 	mbr_args_t a;
    473 	int rval;
    474 
    475 	a.lp = lp;
    476 	a.strat = strat;
    477 	a.msg = NULL;
    478 	a.error = 0;
    479 
    480 	/* get a buffer and initialize it */
    481 	a.bp = geteblk((int)lp->d_secsize);
    482 	a.bp->b_dev = dev;
    483 
    484 	if (osdep)
    485 		rval = scan_mbr(&a, write_netbsd_label);
    486 	else
    487 		rval = validate_label(&a, LABELSECTOR, WRITE_LABEL);
    488 
    489 	if (rval != SCAN_FOUND && a.error == 0)
    490 		a.error = ESRCH;
    491 
    492 	brelse(a.bp);
    493 	return a.error;
    494 }
    495 
    496 static int
    497 write_netbsd_label(mbr_args_t *a, mbr_partition_t *dp, int slot, uint ext_base)
    498 {
    499 	int ptn_base = ext_base + le32toh(dp->mbrp_start);
    500 	int rval;
    501 
    502 	if (dp->mbrp_typ != MBR_PTYPE_NETBSD)
    503 		return SCAN_CONTINUE;
    504 
    505 	rval = validate_label(a, ptn_base + MBR_LABELSECTOR, WRITE_LABEL);
    506 #ifdef notyet 	/* because the error reporting is wrong */
    507 	/* write label to all netbsd partitions */
    508 	if (rval == SCAN_FOUND)
    509 		return SCAN_CONTINUE;
    510 #endif
    511 	return rval;
    512 }
    513 
    514 
    515 /*
    516  * Determine the size of the transfer, and make sure it is
    517  * within the boundaries of the partition. Adjust transfer
    518  * if needed, and signal errors or early completion.
    519  */
    520 int
    521 bounds_check_with_label(dk, bp, wlabel)
    522 	struct disk *dk;
    523 	struct buf *bp;
    524 	int wlabel;
    525 {
    526 	struct disklabel *lp = dk->dk_label;
    527 	struct partition *p = lp->d_partitions + DISKPART(bp->b_dev);
    528 	int labelsector = lp->d_partitions[2].p_offset + LABELSECTOR;
    529 	int sz;
    530 
    531 	sz = howmany(bp->b_bcount, lp->d_secsize);
    532 
    533 	if (bp->b_blkno + sz > p->p_size) {
    534 		sz = p->p_size - bp->b_blkno;
    535 		if (sz == 0) {
    536 			/* If exactly at end of disk, return EOF. */
    537 			bp->b_resid = bp->b_bcount;
    538 			return (0);
    539 		}
    540 		if (sz < 0) {
    541 			/* If past end of disk, return EINVAL. */
    542 			bp->b_error = EINVAL;
    543 			goto bad;
    544 		}
    545 		/* Otherwise, truncate request. */
    546 		bp->b_bcount = sz << DEV_BSHIFT;
    547 	}
    548 
    549 	/* Overwriting disk label? */
    550 	if (bp->b_blkno + p->p_offset <= labelsector &&
    551 	    bp->b_blkno + p->p_offset + sz > labelsector &&
    552 	    (bp->b_flags & B_READ) == 0 && !wlabel) {
    553 		bp->b_error = EROFS;
    554 		goto bad;
    555 	}
    556 
    557 	/* calculate cylinder for disksort to order transfers with */
    558 	bp->b_cylinder = (bp->b_blkno + p->p_offset) /
    559 	    (lp->d_secsize / DEV_BSIZE) / lp->d_secpercyl;
    560 	return (1);
    561 
    562 bad:
    563 	bp->b_flags |= B_ERROR;
    564 	return (-1);
    565 }
    566