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