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