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