xref: /netbsd-src/sys/kern/subr_disk_mbr.c (revision c0179c282a5968435315a82f4128c61372c68fc3)
1 /*	$NetBSD: subr_disk_mbr.c,v 1.22 2006/11/25 11:59:58 scw 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.22 2006/11/25 11:59:58 scw 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 /*
71  * We allocate a buffer 2 sectors large, and look in both....
72  * That means we find labels written by other ports with different offsets.
73  * LABELSECTOR and LABELOFFSET are only used if the disk doesn't have a label.
74  */
75 #if LABELSECTOR > 1 || LABELOFFSET > 512
76 #error Invalid LABELSECTOR or LABELOFFSET
77 #endif
78 
79 #define MBR_LABELSECTOR	1
80 
81 #define SCAN_CONTINUE	0
82 #define SCAN_FOUND	1
83 #define SCAN_ERROR	2
84 
85 typedef struct mbr_args {
86 	struct disklabel *lp;
87 	void		(*strat)(struct buf *);
88 	struct buf	*bp;
89 	const char	*msg;
90 	int		error;
91 	int		written;	/* number of times we wrote label */
92 	int		found_mbr;	/* set if disk has a valid mbr */
93 	uint		label_sector;	/* where we found the label */
94 	int		action;
95 #define READ_LABEL	1
96 #define UPDATE_LABEL	2
97 #define WRITE_LABEL	3
98 } mbr_args_t;
99 
100 static int validate_label(mbr_args_t *, uint);
101 static int look_netbsd_part(mbr_args_t *, mbr_partition_t *, int, uint);
102 static int write_netbsd_label(mbr_args_t *, mbr_partition_t *, int, uint);
103 
104 static int
105 read_sector(mbr_args_t *a, uint sector, int count)
106 {
107 	struct buf *bp = a->bp;
108 	int error;
109 
110 	if (a->lp->d_secpercyl == 0)
111 		return EINVAL;
112 
113 	bp->b_blkno = sector;
114 	bp->b_bcount = count * a->lp->d_secsize;
115 	bp->b_flags = (bp->b_flags & ~(B_WRITE | B_DONE)) | B_READ;
116 	bp->b_cylinder = sector / a->lp->d_secpercyl;
117 	(*a->strat)(bp);
118 	error = biowait(bp);
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 			boolean_t 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  * Attempt to read a disk label from a device
238  * using the indicated strategy routine.
239  * The label must be partly set up before this:
240  * secpercyl, secsize and anything required for a block i/o read
241  * operation in the driver's strategy/start routines
242  * must be filled in before calling us.
243  *
244  * If dos partition table requested, attempt to load it and
245  * find disklabel inside a DOS partition. Also, if bad block
246  * table needed, attempt to extract it as well. Return buffer
247  * for use in signalling errors if requested.
248  *
249  * Returns null on success and an error string on failure.
250  */
251 const char *
252 readdisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp,
253     struct cpu_disklabel *osdep)
254 {
255 	struct dkbad *bdp;
256 	int rval;
257 	int i;
258 	mbr_args_t a;
259 
260 	memset(&a, 0, sizeof a);
261 	a.lp = lp;
262 	a.strat = strat;
263 	a.action = READ_LABEL;
264 
265 	/* minimal requirements for architypal disk label */
266 	if (lp->d_secsize == 0)
267 		lp->d_secsize = DEV_BSIZE;
268 	if (lp->d_secperunit == 0)
269 		lp->d_secperunit = 0x1fffffff;
270 	lp->d_npartitions = RAW_PART + 1;
271 	for (i = 0; i < RAW_PART; i++) {
272 		lp->d_partitions[i].p_size = 0;
273 		lp->d_partitions[i].p_offset = 0;
274 	}
275 	if (lp->d_partitions[RAW_PART].p_size == 0)
276 		lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
277 	lp->d_partitions[RAW_PART].p_offset = 0;
278 
279 	/*
280 	 * Set partition 'a' to be the whole disk.
281 	 * Cleared if we find an mbr or a netbsd label.
282 	 */
283 	lp->d_partitions[0].p_size = lp->d_partitions[RAW_PART].p_size;
284 	lp->d_partitions[0].p_fstype = FS_BSDFFS;
285 
286 	/* get a buffer and initialize it */
287 	a.bp = geteblk(2 * (int)lp->d_secsize);
288 	a.bp->b_dev = dev;
289 
290 	if (osdep)
291 		/*
292 		 * Scan mbr searching for netbsd partition and saving
293 		 * bios partition information to use if the netbsd one
294 		 * is absent.
295 		 */
296 		rval = scan_mbr(&a, look_netbsd_part);
297 	else
298 		rval = SCAN_CONTINUE;
299 
300 	if (rval == SCAN_CONTINUE) {
301 		/* Look at start of disk */
302 		rval = validate_label(&a, 0);
303 	}
304 
305 #if 0
306 	/*
307 	 * Save sector where we found the label for the 'don't overwrite
308 	 * the label' check in bounds_check_with_label.
309 	 */
310 	if (rval == SCAN_FOUND)
311 		xxx->label_sector = a.label_sector;
312 #endif
313 
314 	/* Obtain bad sector table if requested and present */
315 	if (rval == SCAN_FOUND && osdep && (lp->d_flags & D_BADSECT)) {
316 		struct dkbad *db;
317 		int blkno;
318 
319 		bdp = &osdep->bad;
320 		i = 0;
321 		rval = SCAN_ERROR;
322 		do {
323 			/* read a bad sector table */
324 			blkno = lp->d_secperunit - lp->d_nsectors + i;
325 			if (lp->d_secsize > DEV_BSIZE)
326 				blkno *= lp->d_secsize / DEV_BSIZE;
327 			else
328 				blkno /= DEV_BSIZE / lp->d_secsize;
329 			/* if successful, validate, otherwise try another */
330 			if (read_sector(&a, blkno, 1)) {
331 				a.msg = "bad sector table I/O error";
332 				continue;
333 			}
334 			db = (struct dkbad *)(a.bp->b_data);
335 #define DKBAD_MAGIC 0x4321
336 			if (db->bt_mbz != 0 || db->bt_flag != DKBAD_MAGIC) {
337 				a.msg = "bad sector table corrupted";
338 				continue;
339 			}
340 			rval = SCAN_FOUND;
341 			*bdp = *db;
342 			break;
343 		} while ((a.bp->b_flags & B_ERROR) && (i += 2) < 10 &&
344 			i < lp->d_nsectors);
345 	}
346 
347 	brelse(a.bp);
348 	if (rval == SCAN_ERROR || rval == SCAN_CONTINUE)
349 		return a.msg;
350 	return NULL;
351 }
352 
353 static int
354 look_netbsd_part(mbr_args_t *a, mbr_partition_t *dp, int slot, uint ext_base)
355 {
356 	struct partition *pp;
357 	int ptn_base = ext_base + le32toh(dp->mbrp_start);
358 	int rval;
359 
360 	if (
361 #ifdef COMPAT_386BSD_MBRPART
362 	    dp->mbrp_type == MBR_PTYPE_386BSD ||
363 #endif
364 	    dp->mbrp_type == MBR_PTYPE_NETBSD) {
365 		rval = validate_label(a, ptn_base);
366 
367 #if RAW_PART == 3
368 		/* Put actual location where we found the label into ptn 2 */
369 		if (rval == SCAN_FOUND || a->lp->d_partitions[2].p_size == 0) {
370 			a->lp->d_partitions[2].p_size = le32toh(dp->mbrp_size);
371 			a->lp->d_partitions[2].p_offset = ptn_base;
372 		}
373 #endif
374 
375 		/* If we got a netbsd label look no further */
376 		if (rval == SCAN_FOUND)
377 			return rval;
378 	}
379 
380 	/* Install main partitions into e..h and extended into i+ */
381 	if (ext_base == 0)
382 		slot += 4;
383 	else {
384 		slot = 4 + MBR_PART_COUNT;
385 		pp = &a->lp->d_partitions[slot];
386 		for (; slot < MAXPARTITIONS; pp++, slot++) {
387 			/* This gets called twice - avoid duplicates */
388 			if (pp->p_offset == ptn_base &&
389 			    pp->p_size == le32toh(dp->mbrp_size))
390 				break;
391 			if (pp->p_size == 0)
392 				break;
393 		}
394 	}
395 
396 	if (slot < MAXPARTITIONS) {
397 		/* Stop 'a' being the entire disk */
398 		a->lp->d_partitions[0].p_size = 0;
399 		a->lp->d_partitions[0].p_fstype = 0;
400 
401 		/* save partition info */
402 		pp = &a->lp->d_partitions[slot];
403 		pp->p_offset = ptn_base;
404 		pp->p_size = le32toh(dp->mbrp_size);
405 		pp->p_fstype = xlat_mbr_fstype(dp->mbrp_type);
406 
407 		if (slot >= a->lp->d_npartitions)
408 			a->lp->d_npartitions = slot + 1;
409 	}
410 
411 	return SCAN_CONTINUE;
412 }
413 
414 
415 static int
416 validate_label(mbr_args_t *a, uint label_sector)
417 {
418 	struct disklabel *dlp;
419 	char *dlp_lim, *dlp_byte;
420 	int error;
421 
422 	/* Next, dig out disk label */
423 	if (read_sector(a, label_sector, 2)) {
424 		a->msg = "disk label read failed";
425 		return SCAN_ERROR;
426 	}
427 
428 	/* Locate disk label within block and validate */
429 	/*
430 	 * XXX (dsl) This search may be a waste of time, a lot of other i386
431 	 * code assumes the label is at offset LABELOFFSET (=0) in the sector.
432 	 *
433 	 * If we want to support disks from other netbsd ports, then the
434 	 * code should also allow for a shorter label nearer the end of
435 	 * the disk sector, and (IIRC) labels within 8k of the disk start.
436 	 */
437 	dlp = (void *)a->bp->b_data;
438 	dlp_lim = a->bp->b_data + a->bp->b_bcount - sizeof *dlp;
439 	for (;; dlp = (void *)((char *)dlp + sizeof(long))) {
440 		if ((char *)dlp > dlp_lim) {
441 			if (a->action != WRITE_LABEL)
442 				return SCAN_CONTINUE;
443 			/* Write at arch. dependant default location */
444 			dlp_byte = a->bp->b_data + LABELOFFSET;
445 			if (label_sector)
446 				dlp_byte += MBR_LABELSECTOR * a->lp->d_secsize;
447 			else
448 				dlp_byte += LABELSECTOR * a->lp->d_secsize;
449 			dlp = (void *)dlp_byte;
450 			break;
451 		}
452 		if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC)
453 			continue;
454 		if (dlp->d_npartitions > MAXPARTITIONS || dkcksum(dlp) != 0) {
455 			a->msg = "disk label corrupted";
456 			continue;
457 		}
458 		break;
459 	}
460 
461 	switch (a->action) {
462 	case READ_LABEL:
463 		*a->lp = *dlp;
464 		a->label_sector = label_sector;
465 		return SCAN_FOUND;
466 	case UPDATE_LABEL:
467 	case WRITE_LABEL:
468 		*dlp = *a->lp;
469 		a->bp->b_flags &= ~(B_READ|B_DONE);
470 		a->bp->b_flags |= B_WRITE;
471 		(*a->strat)(a->bp);
472 		error = biowait(a->bp);
473 		if (error != 0) {
474 			a->error = error;
475 			a->msg = "disk label write failed";
476 			return SCAN_ERROR;
477 		}
478 		a->written++;
479 		/* Write label to all mbr partitions */
480 		return SCAN_CONTINUE;
481 	default:
482 		return SCAN_ERROR;
483 	}
484 }
485 
486 /*
487  * Check new disk label for sensibility
488  * before setting it.
489  */
490 int
491 setdisklabel(struct disklabel *olp, struct disklabel *nlp, u_long openmask,
492     struct cpu_disklabel *osdep)
493 {
494 	int i;
495 	struct partition *opp, *npp;
496 
497 	/* sanity clause */
498 	if (nlp->d_secpercyl == 0 || nlp->d_secsize == 0
499 		|| (nlp->d_secsize % DEV_BSIZE) != 0)
500 			return (EINVAL);
501 
502 	/* special case to allow disklabel to be invalidated */
503 	if (nlp->d_magic == 0xffffffff) {
504 		*olp = *nlp;
505 		return (0);
506 	}
507 
508 	if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC ||
509 	    dkcksum(nlp) != 0)
510 		return (EINVAL);
511 
512 	/* XXX missing check if other dos partitions will be overwritten */
513 
514 	while (openmask != 0) {
515 		i = ffs(openmask) - 1;
516 		openmask &= ~(1 << i);
517 		if (i > nlp->d_npartitions)
518 			return (EBUSY);
519 		opp = &olp->d_partitions[i];
520 		npp = &nlp->d_partitions[i];
521 		/*
522 		 * Copy internally-set partition information
523 		 * if new label doesn't include it.		XXX
524 		 */
525 		if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) {
526 			*npp = *opp;
527 			continue;
528 		}
529 		if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size)
530 			return (EBUSY);
531 	}
532  	nlp->d_checksum = 0;
533  	nlp->d_checksum = dkcksum(nlp);
534 	*olp = *nlp;
535 	return (0);
536 }
537 
538 
539 /*
540  * Write disk label back to device after modification.
541  */
542 int
543 writedisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp,
544     struct cpu_disklabel *osdep)
545 {
546 	mbr_args_t a;
547 
548 	memset(&a, 0, sizeof a);
549 	a.lp = lp;
550 	a.strat = strat;
551 
552 	/* get a buffer and initialize it */
553 	a.bp = geteblk(2 * (int)lp->d_secsize);
554 	a.bp->b_dev = dev;
555 
556 	/* osdep => we expect an mbr with label in netbsd ptn */
557 	a.action = osdep != NULL ? WRITE_LABEL : UPDATE_LABEL;
558 
559 	/* Write/update the label to every netbsd mbr partition */
560 	scan_mbr(&a, write_netbsd_label);
561 
562 	/* Old write the label at the start of the volume on disks that
563 	 * don't have a valid mbr (always update an existing one) */
564 	a.action = a.found_mbr ? UPDATE_LABEL : WRITE_LABEL;
565 	validate_label(&a, 0);
566 
567 	if (a.written == 0 && a.error == 0)
568 		a.error = ESRCH;
569 
570 	brelse(a.bp);
571 	return a.error;
572 }
573 
574 static int
575 write_netbsd_label(mbr_args_t *a, mbr_partition_t *dp, int slot, uint ext_base)
576 {
577 	int ptn_base = ext_base + le32toh(dp->mbrp_start);
578 
579 	if (dp->mbrp_type != MBR_PTYPE_NETBSD)
580 		return SCAN_CONTINUE;
581 
582 	return validate_label(a, ptn_base);
583 }
584