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