Home | History | Annotate | Line # | Download | only in kern
subr_disk_mbr.c revision 1.42.2.1
      1 /*	$NetBSD: subr_disk_mbr.c,v 1.42.2.1 2012/10/30 17:22:33 yamt 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 the disc is searched
     42  * for ecma-167/iso9660/udf style partition indicators.
     43  * Useful for media or 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.42.2.1 2012/10/30 17:22:33 yamt Exp $");
     58 
     59 #include <sys/param.h>
     60 #include <sys/systm.h>
     61 #include <sys/buf.h>
     62 #include <sys/bootblock.h>
     63 #include <sys/disklabel.h>
     64 #include <sys/disk.h>
     65 #include <sys/syslog.h>
     66 #include <sys/vnode.h>
     67 #include <sys/fcntl.h>
     68 #include <sys/conf.h>
     69 #include <sys/cdio.h>
     70 #include <sys/dkbad.h>
     71 #include <fs/udf/ecma167-udf.h>
     72 
     73 #include <sys/kauth.h>
     74 
     75 #ifdef _KERNEL_OPT
     76 #include "opt_mbr.h"
     77 #endif /* _KERNEL_OPT */
     78 
     79 typedef struct mbr_partition mbr_partition_t;
     80 
     81 /*
     82  * We allocate a buffer 3 sectors large, and look in all....
     83  * That means we find labels written by other ports with different offsets.
     84  * LABELSECTOR and LABELOFFSET are only used if the disk doesn't have a label.
     85  */
     86 #define SCANBLOCKS 3
     87 #define DISKLABEL_SIZE 404
     88 #if LABELSECTOR*DEV_BSIZE + LABELOFFSET > SCANBLOCKS*DEV_BSIZE - DISKLABEL_SIZE
     89 #if _MACHINE != ews4800mips /* XXX: fail silently, ews4800mips LABELSECTOR */
     90 #error Invalid LABELSECTOR or LABELOFFSET
     91 #endif
     92 #endif
     93 
     94 #define MBR_LABELSECTOR	1
     95 
     96 #define SCAN_CONTINUE	0
     97 #define SCAN_FOUND	1
     98 #define SCAN_ERROR	2
     99 
    100 typedef struct mbr_args {
    101 	struct disklabel *lp;
    102 	void		(*strat)(struct buf *);
    103 	struct buf	*bp;
    104 	const char	*msg;
    105 	int		error;
    106 	int		written;	/* number of times we wrote label */
    107 	int		found_mbr;	/* set if disk has a valid mbr */
    108 	uint		label_sector;	/* where we found the label */
    109 	int		action;
    110 	uint32_t	secperunit;
    111 #define READ_LABEL	1
    112 #define UPDATE_LABEL	2
    113 #define WRITE_LABEL	3
    114 } mbr_args_t;
    115 
    116 static int validate_label(mbr_args_t *, uint);
    117 static int look_netbsd_part(mbr_args_t *, mbr_partition_t *, int, uint);
    118 static int write_netbsd_label(mbr_args_t *, mbr_partition_t *, int, uint);
    119 
    120 static int
    121 read_sector(mbr_args_t *a, uint sector, int count)
    122 {
    123 	int error;
    124 
    125 	error = disk_read_sectors(a->strat, a->lp, a->bp, sector, count);
    126 	if (error != 0)
    127 		a->error = error;
    128 	return error;
    129 }
    130 
    131 /*
    132  * Scan MBR for partitions, call 'action' routine for each.
    133  */
    134 
    135 static int
    136 scan_mbr(mbr_args_t *a, int (*actn)(mbr_args_t *, mbr_partition_t *, int, uint))
    137 {
    138 	mbr_partition_t ptns[MBR_PART_COUNT];
    139 	mbr_partition_t *dp;
    140 	struct mbr_sector *mbr;
    141 	uint ext_base, this_ext, next_ext;
    142 	int rval;
    143 	int i;
    144 	int j;
    145 #ifdef COMPAT_386BSD_MBRPART
    146 	int dp_386bsd = -1;
    147 	int ap_386bsd = -1;
    148 #endif
    149 
    150 	ext_base = 0;
    151 	this_ext = 0;
    152 	for (;;) {
    153 		if (read_sector(a, this_ext, 1)) {
    154 			a->msg = "dos partition I/O error";
    155 			return SCAN_ERROR;
    156 		}
    157 
    158 		/* Note: Magic number is little-endian. */
    159 		mbr = (void *)a->bp->b_data;
    160 		if (mbr->mbr_magic != htole16(MBR_MAGIC))
    161 			return SCAN_CONTINUE;
    162 
    163 		/* Copy data out of buffer so action can use bp */
    164 		memcpy(ptns, &mbr->mbr_parts, sizeof ptns);
    165 
    166 		/* Look for drivers and skip them */
    167 		if (ext_base == 0 && ptns[0].mbrp_type == MBR_PTYPE_DM6_DDO) {
    168 			/* We've found a DM6 DDO partition type (used by
    169 			 * the Ontrack Disk Manager drivers).
    170 			 *
    171 			 * Ensure that there are no other partitions in the
    172 			 * MBR and jump to the real partition table (stored
    173 			 * in the first sector of the second track). */
    174 			bool ok = true;
    175 
    176 			for (i = 1; i < MBR_PART_COUNT; i++)
    177 				if (ptns[i].mbrp_type != MBR_PTYPE_UNUSED)
    178 					ok = false;
    179 
    180 			if (ok) {
    181 				this_ext = le32toh(a->lp->d_secpercyl /
    182 				    a->lp->d_ntracks);
    183 				continue;
    184 			}
    185 		}
    186 
    187 		/* look for NetBSD partition */
    188 		next_ext = 0;
    189 		dp = ptns;
    190 		j = 0;
    191 		for (i = 0; i < MBR_PART_COUNT; i++, dp++) {
    192 			if (dp->mbrp_type == MBR_PTYPE_UNUSED)
    193 				continue;
    194 			/* Check end of partition is inside disk limits */
    195 			if ((uint64_t)ext_base + le32toh(dp->mbrp_start) +
    196 			    le32toh(dp->mbrp_size) > a->lp->d_secperunit) {
    197 				/* This mbr doesn't look good.... */
    198 				a->msg = "mbr partition exceeds disk size";
    199 				/* ...but don't report this as an error (yet) */
    200 				return SCAN_CONTINUE;
    201 			}
    202 			a->found_mbr = 1;
    203 			if (MBR_IS_EXTENDED(dp->mbrp_type)) {
    204 				next_ext = le32toh(dp->mbrp_start);
    205 				continue;
    206 			}
    207 #ifdef COMPAT_386BSD_MBRPART
    208 			if (dp->mbrp_type == MBR_PTYPE_386BSD) {
    209 				/*
    210 				 * If more than one matches, take last,
    211 				 * as NetBSD install tool does.
    212 				 */
    213 				if (this_ext == 0) {
    214 					dp_386bsd = i;
    215 					ap_386bsd = j;
    216 				}
    217 				continue;
    218 			}
    219 #endif
    220 			rval = (*actn)(a, dp, j, this_ext);
    221 			if (rval != SCAN_CONTINUE)
    222 				return rval;
    223 			j++;
    224 		}
    225 		if (next_ext == 0)
    226 			break;
    227 		if (ext_base == 0) {
    228 			ext_base = next_ext;
    229 			next_ext = 0;
    230 		}
    231 		next_ext += ext_base;
    232 		if (next_ext <= this_ext)
    233 			break;
    234 		this_ext = next_ext;
    235 	}
    236 #ifdef COMPAT_386BSD_MBRPART
    237 	if (this_ext == 0 && dp_386bsd != -1)
    238 		return (*actn)(a, &ptns[dp_386bsd], ap_386bsd, 0);
    239 #endif
    240 	return SCAN_CONTINUE;
    241 }
    242 
    243 
    244 static void
    245 scan_iso_vrs_session(mbr_args_t *a, uint32_t first_sector,
    246 	int *is_iso9660, int *is_udf)
    247 {
    248 	struct vrs_desc *vrsd;
    249 	uint64_t vrs;
    250 	int sector_size;
    251 	int blks, inc;
    252 
    253 	sector_size = a->lp->d_secsize;
    254 	blks = sector_size / DEV_BSIZE;
    255 	inc  = MAX(1, 2048 / sector_size);
    256 
    257 	/* by definition */
    258 	vrs = ((32*1024 + sector_size - 1) / sector_size)
    259 	        + first_sector;
    260 
    261 	/* read first vrs sector */
    262 	if (read_sector(a, vrs * blks, 1))
    263 		return;
    264 
    265 	/* skip all CD001 records */
    266 	vrsd = a->bp->b_data;
    267 	/* printf("vrsd->identifier = `%s`\n", vrsd->identifier); */
    268 	while (memcmp(vrsd->identifier, "CD001", 5) == 0) {
    269 		/* for sure */
    270 		*is_iso9660 = first_sector;
    271 
    272 		vrs += inc;
    273 		if (read_sector(a, vrs * blks, 1))
    274 			return;
    275 	}
    276 
    277 	/* search for BEA01 */
    278 	vrsd = a->bp->b_data;
    279 	/* printf("vrsd->identifier = `%s`\n", vrsd->identifier); */
    280 	if (memcmp(vrsd->identifier, "BEA01", 5))
    281 		return;
    282 
    283 	/* read successor */
    284 	vrs += inc;
    285 	if (read_sector(a, vrs * blks, 1))
    286 		return;
    287 
    288 	/* check for NSR[23] */
    289 	vrsd = a->bp->b_data;
    290 	/* printf("vrsd->identifier = `%s`\n", vrsd->identifier); */
    291 	if (memcmp(vrsd->identifier, "NSR0", 4))
    292 		return;
    293 
    294 	*is_udf = first_sector;
    295 }
    296 
    297 
    298 /*
    299  * Scan for ISO Volume Recognition Sequences
    300  */
    301 
    302 static int
    303 scan_iso_vrs(mbr_args_t *a)
    304 {
    305 	struct mmc_discinfo  di;
    306 	struct mmc_trackinfo ti;
    307 	dev_t dev;
    308 	uint64_t sector;
    309 	int is_iso9660, is_udf;
    310 	int tracknr, sessionnr;
    311 	int new_session, error;
    312 
    313 	is_iso9660 = is_udf = -1;
    314 
    315 	/* parse all sessions of disc if we're on a SCSI MMC device */
    316 	if (a->lp->d_flags & D_SCSI_MMC) {
    317 		/* get disc info */
    318 		dev = a->bp->b_dev;
    319 		error = bdev_ioctl(dev, MMCGETDISCINFO, &di, FKIOCTL, curlwp);
    320 		if (error)
    321 			return SCAN_CONTINUE;
    322 
    323 		/* go trough all (data) tracks */
    324 		sessionnr = -1;
    325 		for (tracknr = di.first_track;
    326 		    tracknr <= di.first_track_last_session; tracknr++)
    327 		{
    328 			ti.tracknr = tracknr;
    329 			error = bdev_ioctl(dev, MMCGETTRACKINFO, &ti,
    330 					FKIOCTL, curlwp);
    331 			if (error)
    332 				return SCAN_CONTINUE;
    333 			new_session = (ti.sessionnr != sessionnr);
    334 			sessionnr = ti.sessionnr;
    335 			if (new_session) {
    336 				if (ti.flags & MMC_TRACKINFO_BLANK)
    337 					continue;
    338 				if (!(ti.flags & MMC_TRACKINFO_DATA))
    339 					continue;
    340 				sector = ti.track_start;
    341 				scan_iso_vrs_session(a, sector,
    342 					&is_iso9660, &is_udf);
    343 			}
    344 		}
    345 	} else {
    346 		/* try start of disc */
    347 		sector = 0;
    348 		scan_iso_vrs_session(a, sector, &is_iso9660, &is_udf);
    349 	}
    350 
    351 	if ((is_iso9660 < 0) && (is_udf < 0))
    352 		return SCAN_CONTINUE;
    353 
    354 	strncpy(a->lp->d_typename, "iso partition", 16);
    355 
    356 	/* adjust session information for iso9660 partition */
    357 	if (is_iso9660 >= 0) {
    358 		/* set 'a' partition to iso9660 */
    359 		a->lp->d_partitions[0].p_offset = 0;
    360 		a->lp->d_partitions[0].p_size   = a->lp->d_secperunit;
    361 		a->lp->d_partitions[0].p_cdsession = is_iso9660;
    362 		a->lp->d_partitions[0].p_fstype = FS_ISO9660;
    363 	}
    364 
    365 	/* UDF doesn't care about the cd session specified here */
    366 
    367 	return SCAN_FOUND;
    368 }
    369 
    370 
    371 /*
    372  * Attempt to read a disk label from a device
    373  * using the indicated strategy routine.
    374  * The label must be partly set up before this:
    375  * secpercyl, secsize and anything required for a block i/o read
    376  * operation in the driver's strategy/start routines
    377  * must be filled in before calling us.
    378  *
    379  * If dos partition table requested, attempt to load it and
    380  * find disklabel inside a DOS partition. Also, if bad block
    381  * table needed, attempt to extract it as well. Return buffer
    382  * for use in signalling errors if requested.
    383  *
    384  * Returns null on success and an error string on failure.
    385  */
    386 const char *
    387 readdisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp,
    388     struct cpu_disklabel *osdep)
    389 {
    390 	int rval;
    391 	int i;
    392 	mbr_args_t a;
    393 
    394 	memset(&a, 0, sizeof a);
    395 	a.lp = lp;
    396 	a.strat = strat;
    397 	a.action = READ_LABEL;
    398 
    399 	/* minimal requirements for architypal disk label */
    400 	if (lp->d_secsize == 0)
    401 		lp->d_secsize = DEV_BSIZE;
    402 	if (lp->d_secperunit == 0)
    403 		lp->d_secperunit = 0x1fffffff;
    404 	a.secperunit = lp->d_secperunit;
    405 	lp->d_npartitions = RAW_PART + 1;
    406 	for (i = 0; i < RAW_PART; i++) {
    407 		lp->d_partitions[i].p_size = 0;
    408 		lp->d_partitions[i].p_offset = 0;
    409 	}
    410 	if (lp->d_partitions[RAW_PART].p_size == 0)
    411 		lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
    412 	lp->d_partitions[RAW_PART].p_offset = 0;
    413 
    414 	/*
    415 	 * Set partition 'a' to be the whole disk.
    416 	 * Cleared if we find an mbr or a netbsd label.
    417 	 */
    418 	lp->d_partitions[0].p_size = lp->d_partitions[RAW_PART].p_size;
    419 	lp->d_partitions[0].p_fstype = FS_BSDFFS;
    420 
    421 	/*
    422 	 * Get a buffer big enough to read a disklabel in and initialize it
    423 	 * make it three sectors long for the validate_label(); see comment at
    424 	 * start of file.
    425 	 */
    426 	a.bp = geteblk(SCANBLOCKS * (int)lp->d_secsize);
    427 	a.bp->b_dev = dev;
    428 
    429 	if (osdep)
    430 		/*
    431 		 * Scan mbr searching for netbsd partition and saving
    432 		 * bios partition information to use if the netbsd one
    433 		 * is absent.
    434 		 */
    435 		rval = scan_mbr(&a, look_netbsd_part);
    436 	else
    437 		rval = SCAN_CONTINUE;
    438 
    439 	if (rval == SCAN_CONTINUE) {
    440 		/* Look at start of disk */
    441 		rval = validate_label(&a, 0);
    442 	}
    443 
    444 	if (rval == SCAN_CONTINUE) {
    445 		rval = scan_iso_vrs(&a);
    446 	}
    447 #if 0
    448 	/*
    449 	 * Save sector where we found the label for the 'don't overwrite
    450 	 * the label' check in bounds_check_with_label.
    451 	 */
    452 	if (rval == SCAN_FOUND)
    453 		xxx->label_sector = a.label_sector;
    454 #endif
    455 
    456 	/* Obtain bad sector table if requested and present */
    457 #ifdef __HAVE_DISKLABEL_DKBAD
    458 	if (rval == SCAN_FOUND && osdep && (lp->d_flags & D_BADSECT)) {
    459 		struct dkbad *bdp, *db;
    460 		int blkno;
    461 
    462 		bdp = &osdep->bad;
    463 		i = 0;
    464 		rval = SCAN_ERROR;
    465 		do {
    466 			/* read a bad sector table */
    467 			blkno = lp->d_secperunit - lp->d_nsectors + i;
    468 			if (lp->d_secsize > DEV_BSIZE)
    469 				blkno *= lp->d_secsize / DEV_BSIZE;
    470 			else
    471 				blkno /= DEV_BSIZE / lp->d_secsize;
    472 			/* if successful, validate, otherwise try another */
    473 			if (read_sector(&a, blkno, 1)) {
    474 				a.msg = "bad sector table I/O error";
    475 				continue;
    476 			}
    477 			db = (struct dkbad *)(a.bp->b_data);
    478 #define DKBAD_MAGIC 0x4321
    479 			if (db->bt_mbz != 0 || db->bt_flag != DKBAD_MAGIC) {
    480 				a.msg = "bad sector table corrupted";
    481 				continue;
    482 			}
    483 			rval = SCAN_FOUND;
    484 			*bdp = *db;
    485 			break;
    486 		} while (a.bp->b_error && (i += 2) < 10 &&
    487 			i < lp->d_nsectors);
    488 	}
    489 #endif /* __HAVE_DISKLABEL_DKBAD */
    490 
    491 	brelse(a.bp, 0);
    492 	if (rval == SCAN_ERROR || rval == SCAN_CONTINUE)
    493 		return a.msg;
    494 	return NULL;
    495 }
    496 
    497 static int
    498 look_netbsd_part(mbr_args_t *a, mbr_partition_t *dp, int slot, uint ext_base)
    499 {
    500 	struct partition *pp;
    501 	int ptn_base = ext_base + le32toh(dp->mbrp_start);
    502 	int rval;
    503 
    504 	if (
    505 #ifdef COMPAT_386BSD_MBRPART
    506 	    dp->mbrp_type == MBR_PTYPE_386BSD ||
    507 #endif
    508 	    dp->mbrp_type == MBR_PTYPE_NETBSD) {
    509 		rval = validate_label(a, ptn_base);
    510 
    511 #if RAW_PART == 3
    512 		/* Put actual location where we found the label into ptn 2 */
    513 		if (rval == SCAN_FOUND || a->lp->d_partitions[2].p_size == 0) {
    514 			a->lp->d_partitions[2].p_size = le32toh(dp->mbrp_size);
    515 			a->lp->d_partitions[2].p_offset = ptn_base;
    516 		}
    517 #endif
    518 
    519 		/* If we got a netbsd label look no further */
    520 		if (rval == SCAN_FOUND)
    521 			return rval;
    522 	}
    523 
    524 	/* Install main partitions into e..h and extended into i+ */
    525 	if (ext_base == 0)
    526 		slot += 4;
    527 	else {
    528 		slot = 4 + MBR_PART_COUNT;
    529 		pp = &a->lp->d_partitions[slot];
    530 		for (; slot < MAXPARTITIONS; pp++, slot++) {
    531 			/* This gets called twice - avoid duplicates */
    532 			if (pp->p_offset == ptn_base &&
    533 			    pp->p_size == le32toh(dp->mbrp_size))
    534 				break;
    535 			if (pp->p_size == 0)
    536 				break;
    537 		}
    538 	}
    539 
    540 	if (slot < MAXPARTITIONS) {
    541 		/* Stop 'a' being the entire disk */
    542 		a->lp->d_partitions[0].p_size = 0;
    543 		a->lp->d_partitions[0].p_fstype = 0;
    544 
    545 		/* save partition info */
    546 		pp = &a->lp->d_partitions[slot];
    547 		pp->p_offset = ptn_base;
    548 		pp->p_size = le32toh(dp->mbrp_size);
    549 		pp->p_fstype = xlat_mbr_fstype(dp->mbrp_type);
    550 
    551 		if (slot >= a->lp->d_npartitions)
    552 			a->lp->d_npartitions = slot + 1;
    553 	}
    554 
    555 	return SCAN_CONTINUE;
    556 }
    557 
    558 
    559 static int
    560 validate_label(mbr_args_t *a, uint label_sector)
    561 {
    562 	struct disklabel *dlp;
    563 	char *dlp_lim, *dlp_byte;
    564 	int error;
    565 
    566 	/* Next, dig out disk label */
    567 	if (read_sector(a, label_sector, SCANBLOCKS)) {
    568 		a->msg = "disk label read failed";
    569 		return SCAN_ERROR;
    570 	}
    571 
    572 	/* Locate disk label within block and validate */
    573 	/*
    574 	 * XXX (dsl) This search may be a waste of time, a lot of other i386
    575 	 * code assumes the label is at offset LABELOFFSET (=0) in the sector.
    576 	 *
    577 	 * If we want to support disks from other netbsd ports, then the
    578 	 * code should also allow for a shorter label nearer the end of
    579 	 * the disk sector, and (IIRC) labels within 8k of the disk start.
    580 	 */
    581 	dlp = (void *)a->bp->b_data;
    582 	dlp_lim = (char *)a->bp->b_data + a->bp->b_bcount - sizeof *dlp;
    583 	for (;; dlp = (void *)((char *)dlp + sizeof(long))) {
    584 		if ((char *)dlp > dlp_lim) {
    585 			if (a->action != WRITE_LABEL)
    586 				return SCAN_CONTINUE;
    587 			/* Write at arch. dependent default location */
    588 			dlp_byte = (char *)a->bp->b_data + LABELOFFSET;
    589 			if (label_sector)
    590 				dlp_byte += MBR_LABELSECTOR * a->lp->d_secsize;
    591 			else
    592 				dlp_byte += LABELSECTOR * a->lp->d_secsize;
    593 			dlp = (void *)dlp_byte;
    594 			break;
    595 		}
    596 		if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC)
    597 			continue;
    598 		if (dlp->d_npartitions > MAXPARTITIONS || dkcksum(dlp) != 0) {
    599 			a->msg = "disk label corrupted";
    600 			continue;
    601 		}
    602 		break;
    603 	}
    604 
    605 	switch (a->action) {
    606 	case READ_LABEL:
    607 		*a->lp = *dlp;
    608 		if ((a->msg = convertdisklabel(a->lp, a->strat, a->bp,
    609 		                              a->secperunit)) != NULL)
    610 			return SCAN_ERROR;
    611 		a->label_sector = label_sector;
    612 		return SCAN_FOUND;
    613 	case UPDATE_LABEL:
    614 	case WRITE_LABEL:
    615 		*dlp = *a->lp;
    616 		a->bp->b_oflags &= ~BO_DONE;
    617 		a->bp->b_flags &= ~B_READ;
    618 		a->bp->b_flags |= B_WRITE;
    619 		(*a->strat)(a->bp);
    620 		error = biowait(a->bp);
    621 		if (error != 0) {
    622 			a->error = error;
    623 			a->msg = "disk label write failed";
    624 			return SCAN_ERROR;
    625 		}
    626 		a->written++;
    627 		/* Write label to all mbr partitions */
    628 		return SCAN_CONTINUE;
    629 	default:
    630 		return SCAN_ERROR;
    631 	}
    632 }
    633 
    634 /*
    635  * Check new disk label for sensibility
    636  * before setting it.
    637  */
    638 int
    639 setdisklabel(struct disklabel *olp, struct disklabel *nlp, u_long openmask,
    640     struct cpu_disklabel *osdep)
    641 {
    642 	int i;
    643 	struct partition *opp, *npp;
    644 
    645 	/* sanity clause */
    646 	if (nlp->d_secpercyl == 0 || nlp->d_secsize == 0
    647 		|| (nlp->d_secsize % DEV_BSIZE) != 0)
    648 			return (EINVAL);
    649 
    650 	/* special case to allow disklabel to be invalidated */
    651 	if (nlp->d_magic == 0xffffffff) {
    652 		*olp = *nlp;
    653 		return (0);
    654 	}
    655 
    656 	if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC ||
    657 	    dkcksum(nlp) != 0)
    658 		return (EINVAL);
    659 
    660 	/* XXX missing check if other dos partitions will be overwritten */
    661 
    662 	while (openmask != 0) {
    663 		i = ffs(openmask) - 1;
    664 		openmask &= ~(1 << i);
    665 		if (i > nlp->d_npartitions)
    666 			return (EBUSY);
    667 		opp = &olp->d_partitions[i];
    668 		npp = &nlp->d_partitions[i];
    669 		/*
    670 		 * Copy internally-set partition information
    671 		 * if new label doesn't include it.		XXX
    672 		 */
    673 		if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) {
    674 			*npp = *opp;
    675 			continue;
    676 		}
    677 		if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size)
    678 			return (EBUSY);
    679 	}
    680  	nlp->d_checksum = 0;
    681  	nlp->d_checksum = dkcksum(nlp);
    682 	*olp = *nlp;
    683 	return (0);
    684 }
    685 
    686 
    687 /*
    688  * Write disk label back to device after modification.
    689  */
    690 int
    691 writedisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp,
    692     struct cpu_disklabel *osdep)
    693 {
    694 	mbr_args_t a;
    695 
    696 	memset(&a, 0, sizeof a);
    697 	a.lp = lp;
    698 	a.strat = strat;
    699 
    700 	/* get a buffer and initialize it */
    701 	a.bp = geteblk(SCANBLOCKS * (int)lp->d_secsize);
    702 	a.bp->b_dev = dev;
    703 
    704 	/* osdep => we expect an mbr with label in netbsd ptn */
    705 	a.action = osdep != NULL ? WRITE_LABEL : UPDATE_LABEL;
    706 
    707 	/* Write/update the label to every netbsd mbr partition */
    708 	scan_mbr(&a, write_netbsd_label);
    709 
    710 	/* Old write the label at the start of the volume on disks that
    711 	 * don't have a valid mbr (always update an existing one) */
    712 	a.action = a.found_mbr ? UPDATE_LABEL : WRITE_LABEL;
    713 	validate_label(&a, 0);
    714 
    715 	if (a.written == 0 && a.error == 0)
    716 		a.error = ESRCH;
    717 
    718 	brelse(a.bp, 0);
    719 	return a.error;
    720 }
    721 
    722 static int
    723 write_netbsd_label(mbr_args_t *a, mbr_partition_t *dp, int slot, uint ext_base)
    724 {
    725 	int ptn_base = ext_base + le32toh(dp->mbrp_start);
    726 
    727 	if (dp->mbrp_type != MBR_PTYPE_NETBSD)
    728 		return SCAN_CONTINUE;
    729 
    730 	return validate_label(a, ptn_base);
    731 }
    732