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