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