WARNS6 cleanups.
[dragonfly.git] / sbin / disklabel / disklabel.c
blobddfe5361ef5b9b1004c2bd63ea8f66a715228686
1 /*
2 * Copyright (c) 1987, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Symmetric Computer Systems.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
36 * @(#) Copyright (c) 1987, 1993 The Regents of the University of California. All rights reserved.
37 * @(#)disklabel.c 1.2 (Symmetric) 11/28/85
38 * @(#)disklabel.c 8.2 (Berkeley) 1/7/94
39 * $FreeBSD: src/sbin/disklabel/disklabel.c,v 1.28.2.15 2003/01/24 16:18:16 des Exp $
40 * $DragonFly: src/sbin/disklabel/disklabel.c,v 1.12 2006/10/17 02:18:51 pavalos Exp $
43 #include <sys/param.h>
44 #include <sys/file.h>
45 #include <sys/stat.h>
46 #include <sys/wait.h>
47 #define DKTYPENAMES
48 #include <sys/disklabel.h>
49 #include <sys/diskmbr.h>
51 #include <vfs/ufs/dinode.h>
52 #include <vfs/ufs/fs.h>
54 #include <unistd.h>
55 #include <string.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <signal.h>
59 #include <stdarg.h>
60 #include <ctype.h>
61 #include <err.h>
62 #include <errno.h>
63 #include "pathnames.h"
66 * Disklabel: read and write disklabels.
67 * The label is usually placed on one of the first sectors of the disk.
68 * Many machines also place a bootstrap in the same area,
69 * in which case the label is embedded in the bootstrap.
70 * The bootstrap source must leave space at the proper offset
71 * for the label on such machines.
74 #ifndef BBSIZE
75 #define BBSIZE 8192 /* size of boot area, with label */
76 #endif
78 /* FIX! These are too low, but are traditional */
79 #define DEFAULT_NEWFS_BLOCK 8192U
80 #define DEFAULT_NEWFS_FRAG 1024U
81 #define DEFAULT_NEWFS_CPG 16U
83 #define BIG_NEWFS_BLOCK 16384U
84 #define BIG_NEWFS_FRAG 2048U
85 #define BIG_NEWFS_CPG 64U
87 #define NUMBOOT 2
89 void makelabel(const char *, const char *, struct disklabel *);
90 int writelabel(int, const char *, struct disklabel *);
91 void l_perror(const char *);
92 struct disklabel *readlabel(int);
93 struct disklabel *makebootarea(char *, struct disklabel *, int);
94 void display(FILE *, const struct disklabel *);
95 int edit(struct disklabel *, int);
96 int editit(void);
97 char *skip(char *);
98 char *word(char *);
99 int getasciilabel(FILE *, struct disklabel *);
100 int getasciipartspec(char *, struct disklabel *, int, int);
101 int checklabel(struct disklabel *);
102 void setbootflag(struct disklabel *);
103 void Warning(const char *, ...) __printflike(1, 2);
104 void usage(void);
105 int checkoldboot(int, const char *);
106 struct disklabel *getvirginlabel(void);
108 #define DEFEDITOR _PATH_VI
109 #define streq(a,b) (strcmp(a,b) == 0)
111 char *dkname;
112 char *specname;
113 char tmpfil[] = PATH_TMPFILE;
115 char namebuf[BBSIZE], *np = namebuf;
116 struct disklabel lab;
117 char bootarea[BBSIZE];
119 #define MAX_PART ('z')
120 #define MAX_NUM_PARTS (1 + MAX_PART - 'a')
121 char part_size_type[MAX_NUM_PARTS];
122 char part_offset_type[MAX_NUM_PARTS];
123 int part_set[MAX_NUM_PARTS];
125 #if NUMBOOT > 0
126 int installboot; /* non-zero if we should install a boot program */
127 char *bootbuf; /* pointer to buffer with remainder of boot prog */
128 int bootsize; /* size of remaining boot program */
129 char *xxboot; /* primary boot */
130 char *bootxx; /* secondary boot */
131 char boot0[MAXPATHLEN];
132 char boot1[MAXPATHLEN];
133 #endif
135 enum {
136 UNSPEC, EDIT, NOWRITE, READ, RESTORE, WRITE, WRITEABLE, WRITEBOOT
137 } op = UNSPEC;
139 int rflag;
140 int disable_write; /* set to disable writing to disk label */
142 #ifdef DEBUG
143 int debug;
144 #define OPTIONS "BNRWb:denrs:w"
145 #else
146 #define OPTIONS "BNRWb:enrs:w"
147 #endif
150 main(int argc, char *argv[])
152 struct disklabel *lp;
153 FILE *t;
154 int ch, f = 0, flag, error = 0;
155 char *name = 0;
157 while ((ch = getopt(argc, argv, OPTIONS)) != -1)
158 switch (ch) {
159 #if NUMBOOT > 0
160 case 'B':
161 ++installboot;
162 break;
163 case 'b':
164 xxboot = optarg;
165 break;
166 #if NUMBOOT > 1
167 case 's':
168 bootxx = optarg;
169 break;
170 #endif
171 #endif
172 case 'N':
173 if (op != UNSPEC)
174 usage();
175 op = NOWRITE;
176 break;
177 case 'n':
178 disable_write = 1;
179 break;
180 case 'R':
181 if (op != UNSPEC)
182 usage();
183 op = RESTORE;
184 break;
185 case 'W':
186 if (op != UNSPEC)
187 usage();
188 op = WRITEABLE;
189 break;
190 case 'e':
191 if (op != UNSPEC)
192 usage();
193 op = EDIT;
194 break;
195 case 'r':
196 ++rflag;
197 break;
198 case 'w':
199 if (op != UNSPEC)
200 usage();
201 op = WRITE;
202 break;
203 #ifdef DEBUG
204 case 'd':
205 debug++;
206 break;
207 #endif
208 case '?':
209 default:
210 usage();
212 argc -= optind;
213 argv += optind;
214 #if NUMBOOT > 0
215 if (installboot) {
216 rflag++;
217 if (op == UNSPEC)
218 op = WRITEBOOT;
219 } else {
220 if (op == UNSPEC)
221 op = READ;
222 xxboot = bootxx = 0;
224 #else
225 if (op == UNSPEC)
226 op = READ;
227 #endif
228 if (argc < 1)
229 usage();
231 dkname = argv[0];
232 if (dkname[0] != '/') {
233 sprintf(np, "%s%s%c", _PATH_DEV, dkname, 'a' + RAW_PART);
234 specname = np;
235 np += strlen(specname) + 1;
236 } else
237 specname = dkname;
238 f = open(specname, op == READ ? O_RDONLY : O_RDWR);
239 if (f < 0 && errno == ENOENT && dkname[0] != '/') {
240 sprintf(specname, "%s%s", _PATH_DEV, dkname);
241 np = namebuf + strlen(specname) + 1;
242 f = open(specname, op == READ ? O_RDONLY : O_RDWR);
244 if (f < 0)
245 err(4, "%s", specname);
247 switch(op) {
249 case UNSPEC:
250 break;
252 case EDIT:
253 if (argc != 1)
254 usage();
255 lp = readlabel(f);
256 error = edit(lp, f);
257 break;
259 case NOWRITE:
260 flag = 0;
261 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
262 err(4, "ioctl DIOCWLABEL");
263 break;
265 case READ:
266 if (argc != 1)
267 usage();
268 lp = readlabel(f);
269 display(stdout, lp);
270 error = checklabel(lp);
271 if (checkoldboot(f, NULL))
272 warnx("Warning, old bootblocks detected, install new bootblocks & reinstall the disklabel");
273 break;
275 case RESTORE:
276 #if NUMBOOT > 0
277 if (installboot && argc == 3) {
278 makelabel(argv[2], 0, &lab);
279 argc--;
282 * We only called makelabel() for its side effect
283 * of setting the bootstrap file names. Discard
284 * all changes to `lab' so that all values in the
285 * final label come from the ASCII label.
287 bzero((char *)&lab, sizeof(lab));
289 #endif
290 if (argc != 2)
291 usage();
292 if (!(t = fopen(argv[1], "r")))
293 err(4, "%s", argv[1]);
294 if (!getasciilabel(t, &lab))
295 exit(1);
296 lp = makebootarea(bootarea, &lab, f);
297 *lp = lab;
298 error = writelabel(f, bootarea, lp);
299 break;
301 case WRITE:
302 if (argc == 3) {
303 name = argv[2];
304 argc--;
306 if (argc != 2)
307 usage();
308 makelabel(argv[1], name, &lab);
309 lp = makebootarea(bootarea, &lab, f);
310 *lp = lab;
311 if (checklabel(lp) == 0)
312 error = writelabel(f, bootarea, lp);
313 break;
315 case WRITEABLE:
316 flag = 1;
317 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
318 err(4, "ioctl DIOCWLABEL");
319 break;
321 #if NUMBOOT > 0
322 case WRITEBOOT:
324 struct disklabel tlab;
326 lp = readlabel(f);
327 tlab = *lp;
328 if (argc == 2)
329 makelabel(argv[1], 0, &lab);
330 lp = makebootarea(bootarea, &lab, f);
331 *lp = tlab;
332 if (checklabel(lp) == 0)
333 error = writelabel(f, bootarea, lp);
334 break;
336 #endif
338 exit(error);
342 * Construct a prototype disklabel from /etc/disktab. As a side
343 * effect, set the names of the primary and secondary boot files
344 * if specified.
346 void
347 makelabel(const char *type, const char *name, struct disklabel *lp)
349 struct disklabel *dp;
351 if (strcmp(type, "auto") == 0)
352 dp = getvirginlabel();
353 else
354 dp = getdiskbyname(type);
355 if (dp == NULL)
356 errx(1, "%s: unknown disk type", type);
357 *lp = *dp;
358 #if NUMBOOT > 0
360 * Set bootstrap name(s).
361 * 1. If set from command line, use those,
362 * 2. otherwise, check if disktab specifies them (b0 or b1),
363 * 3. otherwise, makebootarea() will choose ones based on the name
364 * of the disk special file. E.g. /dev/ra0 -> raboot, bootra
366 if (!xxboot && lp->d_boot0) {
367 if (*lp->d_boot0 != '/')
368 sprintf(boot0, "%s/%s", _PATH_BOOTDIR, lp->d_boot0);
369 else
370 strcpy(boot0, lp->d_boot0);
371 xxboot = boot0;
373 #if NUMBOOT > 1
374 if (!bootxx && lp->d_boot1) {
375 if (*lp->d_boot1 != '/')
376 sprintf(boot1, "%s/%s", _PATH_BOOTDIR, lp->d_boot1);
377 else
378 strcpy(boot1, lp->d_boot1);
379 bootxx = boot1;
381 #endif
382 #endif
383 /* d_packname is union d_boot[01], so zero */
384 bzero(lp->d_packname, sizeof(lp->d_packname));
385 if (name)
386 strncpy(lp->d_packname, name, sizeof(lp->d_packname));
390 writelabel(int f, const char *boot, struct disklabel *lp)
392 int flag;
394 if (disable_write) {
395 Warning("write to disk label supressed - label was as follows:");
396 display(stdout, lp);
397 return (0);
398 } else {
399 /* make sure we are not overwriting our boot code */
400 if (checkoldboot(f, boot))
401 errx(4, "Will not overwrite old bootblocks w/ label, install new boot blocks first!");
402 setbootflag(lp);
403 lp->d_magic = DISKMAGIC;
404 lp->d_magic2 = DISKMAGIC;
405 lp->d_checksum = 0;
406 lp->d_checksum = dkcksum(lp);
407 if (rflag) {
409 * First set the kernel disk label,
410 * then write a label to the raw disk.
411 * If the SDINFO ioctl fails because it is unimplemented,
412 * keep going; otherwise, the kernel consistency checks
413 * may prevent us from changing the current (in-core)
414 * label.
416 if (ioctl(f, DIOCSDINFO, lp) < 0 &&
417 errno != ENODEV && errno != ENOTTY) {
418 l_perror("ioctl DIOCSDINFO");
419 return (1);
421 lseek(f, (off_t)0, SEEK_SET);
424 * write enable label sector before write (if necessary),
425 * disable after writing.
427 flag = 1;
428 if (ioctl(f, DIOCWLABEL, &flag) < 0)
429 warn("ioctl DIOCWLABEL");
430 if (write(f, boot, lp->d_bbsize) != (ssize_t)lp->d_bbsize) {
431 warn("write");
432 return (1);
434 #if NUMBOOT > 0
436 * Output the remainder of the disklabel
438 if (bootbuf && write(f, bootbuf, bootsize) != bootsize) {
439 warn("write");
440 return(1);
442 #endif
443 flag = 0;
444 ioctl(f, DIOCWLABEL, &flag);
445 } else if (ioctl(f, DIOCWDINFO, lp) < 0) {
446 l_perror("ioctl DIOCWDINFO");
447 return (1);
450 return (0);
453 void
454 l_perror(const char *s)
456 switch (errno) {
458 case ESRCH:
459 warnx("%s: no disk label on disk;", s);
460 fprintf(stderr, "add \"-r\" to install initial label\n");
461 break;
463 case EINVAL:
464 warnx("%s: label magic number or checksum is wrong!", s);
465 fprintf(stderr, "(disklabel or kernel is out of date?)\n");
466 break;
468 case EBUSY:
469 warnx("%s: open partition would move or shrink", s);
470 break;
472 case EXDEV:
473 warnx("%s: '%c' partition must start at beginning of disk",
474 s, 'a' + RAW_PART);
475 break;
477 default:
478 warn((char *)NULL);
479 break;
484 * Fetch disklabel for disk.
485 * Use ioctl to get label unless -r flag is given.
487 struct disklabel *
488 readlabel(int f)
490 struct disklabel *lp;
492 if (rflag) {
493 if (read(f, bootarea, BBSIZE) < BBSIZE)
494 err(4, "%s", specname);
495 for (lp = (struct disklabel *)bootarea;
496 lp <= (struct disklabel *)(bootarea + BBSIZE - sizeof(*lp));
497 lp = (struct disklabel *)((char *)lp + 16))
498 if (lp->d_magic == DISKMAGIC &&
499 lp->d_magic2 == DISKMAGIC)
500 break;
501 if (lp > (struct disklabel *)(bootarea+BBSIZE-sizeof(*lp)) ||
502 lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC ||
503 dkcksum(lp) != 0)
504 errx(1,
505 "bad pack magic number (label is damaged, or pack is unlabeled)");
506 } else {
507 lp = &lab;
508 if (ioctl(f, DIOCGDINFO, lp) < 0)
509 err(4, "ioctl DIOCGDINFO");
511 return (lp);
515 * Construct a bootarea (d_bbsize bytes) in the specified buffer ``boot''
516 * Returns a pointer to the disklabel portion of the bootarea.
518 struct disklabel *
519 makebootarea(char *boot, struct disklabel *dp, int f)
521 struct disklabel *lp;
522 char *p;
523 int b;
524 #if NUMBOOT > 0
525 char *dkbasename;
526 struct stat sb;
527 #endif
528 #ifdef __i386__
529 char *tmpbuf;
530 unsigned int i, found;
531 #endif
533 /* XXX */
534 if (dp->d_secsize == 0) {
535 dp->d_secsize = DEV_BSIZE;
536 dp->d_bbsize = BBSIZE;
538 lp = (struct disklabel *)
539 (boot + (LABELSECTOR * dp->d_secsize) + LABELOFFSET);
540 bzero((char *)lp, sizeof *lp);
541 #if NUMBOOT > 0
543 * If we are not installing a boot program but we are installing a
544 * label on disk then we must read the current bootarea so we don't
545 * clobber the existing boot.
547 if (!installboot) {
548 if (rflag) {
549 if (read(f, boot, BBSIZE) < BBSIZE)
550 err(4, "%s", specname);
551 bzero((char *)lp, sizeof *lp);
553 return (lp);
556 * We are installing a boot program. Determine the name(s) and
557 * read them into the appropriate places in the boot area.
559 if (!xxboot || !bootxx) {
560 dkbasename = np;
561 if ((p = strrchr(dkname, '/')) == NULL)
562 p = dkname;
563 else
564 p++;
565 while (*p && !isdigit(*p))
566 *np++ = *p++;
567 *np++ = '\0';
569 if (!xxboot) {
570 sprintf(boot0, "%s/boot1", _PATH_BOOTDIR);
571 xxboot = boot0;
573 #if NUMBOOT > 1
574 if (!bootxx) {
575 sprintf(boot1, "%s/boot2", _PATH_BOOTDIR);
576 bootxx = boot1;
578 #endif
580 #ifdef DEBUG
581 if (debug)
582 fprintf(stderr, "bootstraps: xxboot = %s, bootxx = %s\n",
583 xxboot, bootxx ? bootxx : "NONE");
584 #endif
587 * Strange rules:
588 * 1. One-piece bootstrap (hp300/hp800)
589 * up to d_bbsize bytes of ``xxboot'' go in bootarea, the rest
590 * is remembered and written later following the bootarea.
591 * 2. Two-piece bootstraps (vax/i386?/mips?)
592 * up to d_secsize bytes of ``xxboot'' go in first d_secsize
593 * bytes of bootarea, remaining d_bbsize-d_secsize filled
594 * from ``bootxx''.
596 b = open(xxboot, O_RDONLY);
597 if (b < 0)
598 err(4, "%s", xxboot);
599 #if NUMBOOT > 1
600 #ifdef __i386__
602 * XXX Botch alert.
603 * The i386 has the so-called fdisk table embedded into the
604 * primary bootstrap. We take care to not clobber it, but
605 * only if it does already contain some data. (Otherwise,
606 * the xxboot provides a template.)
608 if ((tmpbuf = (char *)malloc((int)dp->d_secsize)) == 0)
609 err(4, "%s", xxboot);
610 memcpy((void *)tmpbuf, (void *)boot, (int)dp->d_secsize);
611 #endif /* i386 */
612 if (read(b, boot, (int)dp->d_secsize) < 0)
613 err(4, "%s", xxboot);
614 close(b);
615 #ifdef __i386__
616 for (i = DOSPARTOFF, found = 0;
617 !found && i < DOSPARTOFF + NDOSPART*sizeof(struct dos_partition);
618 i++)
619 found = tmpbuf[i] != 0;
620 if (found)
621 memcpy((void *)&boot[DOSPARTOFF],
622 (void *)&tmpbuf[DOSPARTOFF],
623 NDOSPART * sizeof(struct dos_partition));
624 free(tmpbuf);
625 #endif /* i386 */
626 b = open(bootxx, O_RDONLY);
627 if (b < 0)
628 err(4, "%s", bootxx);
629 if (fstat(b, &sb) != 0)
630 err(4, "%s", bootxx);
631 if (dp->d_secsize + sb.st_size > dp->d_bbsize)
632 errx(4, "%s too large", bootxx);
633 if (read(b, &boot[dp->d_secsize],
634 (int)(dp->d_bbsize-dp->d_secsize)) < 0)
635 err(4, "%s", bootxx);
636 #else /* !(NUMBOOT > 1) */
637 if (read(b, boot, (int)dp->d_bbsize) < 0)
638 err(4, "%s", xxboot);
639 if (fstat(b, &sb) != 0)
640 err(4, "%s", xxboot);
641 bootsize = (int)sb.st_size - dp->d_bbsize;
642 if (bootsize > 0) {
643 /* XXX assume d_secsize is a power of two */
644 bootsize = (bootsize + dp->d_secsize-1) & ~(dp->d_secsize-1);
645 bootbuf = (char *)malloc((size_t)bootsize);
646 if (bootbuf == 0)
647 err(4, "%s", xxboot);
648 if (read(b, bootbuf, bootsize) < 0) {
649 free(bootbuf);
650 err(4, "%s", xxboot);
653 #endif /* NUMBOOT > 1 */
654 close(b);
655 #endif /* NUMBOOT > 0 */
657 * Make sure no part of the bootstrap is written in the area
658 * reserved for the label.
660 for (p = (char *)lp; p < (char *)lp + sizeof(struct disklabel); p++)
661 if (*p)
662 errx(2, "bootstrap doesn't leave room for disk label");
663 return (lp);
666 void
667 display(FILE *f, const struct disklabel *lp)
669 int i, j;
670 const struct partition *pp;
672 fprintf(f, "# %s:\n", specname);
673 if (lp->d_type < DKMAXTYPES)
674 fprintf(f, "type: %s\n", dktypenames[lp->d_type]);
675 else
676 fprintf(f, "type: %u\n", lp->d_type);
677 fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename),
678 lp->d_typename);
679 fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname),
680 lp->d_packname);
681 fprintf(f, "flags:");
682 if (lp->d_flags & D_REMOVABLE)
683 fprintf(f, " removeable");
684 if (lp->d_flags & D_ECC)
685 fprintf(f, " ecc");
686 if (lp->d_flags & D_BADSECT)
687 fprintf(f, " badsect");
688 fprintf(f, "\n");
689 fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize);
690 fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors);
691 fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks);
692 fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl);
693 fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders);
694 fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit);
695 fprintf(f, "rpm: %u\n", lp->d_rpm);
696 fprintf(f, "interleave: %u\n", lp->d_interleave);
697 fprintf(f, "trackskew: %u\n", lp->d_trackskew);
698 fprintf(f, "cylinderskew: %u\n", lp->d_cylskew);
699 fprintf(f, "headswitch: %lu\t\t# milliseconds\n",
700 (u_long)lp->d_headswitch);
701 fprintf(f, "track-to-track seek: %ld\t# milliseconds\n",
702 (u_long)lp->d_trkseek);
703 fprintf(f, "drivedata: ");
704 for (i = NDDATA - 1; i >= 0; i--)
705 if (lp->d_drivedata[i])
706 break;
707 if (i < 0)
708 i = 0;
709 for (j = 0; j <= i; j++)
710 fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]);
711 fprintf(f, "\n\n%u partitions:\n", lp->d_npartitions);
712 fprintf(f,
713 "# size offset fstype [fsize bsize bps/cpg]\n");
714 pp = lp->d_partitions;
715 for (i = 0; i < lp->d_npartitions; i++, pp++) {
716 if (pp->p_size) {
717 fprintf(f, " %c: %8lu %8lu ", 'a' + i,
718 (u_long)pp->p_size, (u_long)pp->p_offset);
719 if (pp->p_fstype < FSMAXTYPES)
720 fprintf(f, "%8.8s", fstypenames[pp->p_fstype]);
721 else
722 fprintf(f, "%8d", pp->p_fstype);
723 switch (pp->p_fstype) {
725 case FS_UNUSED: /* XXX */
726 fprintf(f, " %5lu %5lu %5.5s ",
727 (u_long)pp->p_fsize,
728 (u_long)(pp->p_fsize * pp->p_frag), "");
729 break;
731 case FS_BSDFFS:
732 fprintf(f, " %5lu %5lu %5u ",
733 (u_long)pp->p_fsize,
734 (u_long)(pp->p_fsize * pp->p_frag),
735 pp->p_cpg);
736 break;
738 case FS_BSDLFS:
739 fprintf(f, " %5lu %5lu %5d",
740 (u_long)pp->p_fsize,
741 (u_long)(pp->p_fsize * pp->p_frag),
742 pp->p_cpg);
743 break;
745 default:
746 fprintf(f, "%20.20s", "");
747 break;
749 fprintf(f, "\t# (Cyl. %4lu",
750 (u_long)(pp->p_offset / lp->d_secpercyl));
751 if (pp->p_offset % lp->d_secpercyl)
752 putc('*', f);
753 else
754 putc(' ', f);
755 fprintf(f, "- %lu",
756 (u_long)((pp->p_offset + pp->p_size +
757 lp->d_secpercyl - 1) /
758 lp->d_secpercyl - 1));
759 if (pp->p_size % lp->d_secpercyl)
760 putc('*', f);
761 fprintf(f, ")\n");
764 fflush(f);
768 edit(struct disklabel *lp, int f)
770 int c, fd;
771 struct disklabel label;
772 FILE *fp;
774 if ((fd = mkstemp(tmpfil)) == -1 ||
775 (fp = fdopen(fd, "w")) == NULL) {
776 warnx("can't create %s", tmpfil);
777 return (1);
779 display(fp, lp);
780 fclose(fp);
781 for (;;) {
782 if (!editit())
783 break;
784 fp = fopen(tmpfil, "r");
785 if (fp == NULL) {
786 warnx("can't reopen %s for reading", tmpfil);
787 break;
789 bzero((char *)&label, sizeof(label));
790 if (getasciilabel(fp, &label)) {
791 *lp = label;
792 if (writelabel(f, bootarea, lp) == 0) {
793 fclose(fp);
794 unlink(tmpfil);
795 return (0);
798 fclose(fp);
799 printf("re-edit the label? [y]: "); fflush(stdout);
800 c = getchar();
801 if (c != EOF && c != (int)'\n')
802 while (getchar() != (int)'\n')
804 if (c == (int)'n')
805 break;
807 unlink(tmpfil);
808 return (1);
812 editit(void)
814 int pid, xpid;
815 int status, omask;
816 const char *ed;
818 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
819 while ((pid = fork()) < 0) {
820 if (errno == EPROCLIM) {
821 warnx("you have too many processes");
822 return(0);
824 if (errno != EAGAIN) {
825 warn("fork");
826 return(0);
828 sleep(1);
830 if (pid == 0) {
831 sigsetmask(omask);
832 setgid(getgid());
833 setuid(getuid());
834 if ((ed = getenv("EDITOR")) == (char *)0)
835 ed = DEFEDITOR;
836 execlp(ed, ed, tmpfil, (char *)0);
837 err(1, "%s", ed);
839 while ((xpid = wait(&status)) >= 0)
840 if (xpid == pid)
841 break;
842 sigsetmask(omask);
843 return(!status);
846 char *
847 skip(char *cp)
850 while (*cp != '\0' && isspace(*cp))
851 cp++;
852 if (*cp == '\0' || *cp == '#')
853 return (NULL);
854 return (cp);
857 char *
858 word(char *cp)
860 char c;
862 while (*cp != '\0' && !isspace(*cp) && *cp != '#')
863 cp++;
864 if ((c = *cp) != '\0') {
865 *cp++ = '\0';
866 if (c != '#')
867 return (skip(cp));
869 return (NULL);
873 * Read an ascii label in from fd f,
874 * in the same format as that put out by display(),
875 * and fill in lp.
878 getasciilabel(FILE *f, struct disklabel *lp)
880 char *cp;
881 const char **cpp;
882 u_int part;
883 char *tp, line[BUFSIZ];
884 u_long v;
885 int lineno = 0, errors = 0;
886 int i;
887 char empty[] = "";
888 char unknown[] = "unknown";
890 bzero(&part_set, sizeof(part_set));
891 bzero(&part_size_type, sizeof(part_size_type));
892 bzero(&part_offset_type, sizeof(part_offset_type));
893 lp->d_bbsize = BBSIZE; /* XXX */
894 lp->d_sbsize = SBSIZE; /* XXX */
895 while (fgets(line, sizeof(line) - 1, f)) {
896 lineno++;
897 if ((cp = strchr(line,'\n')) != 0)
898 *cp = '\0';
899 cp = skip(line);
900 if (cp == NULL)
901 continue;
902 tp = strchr(cp, ':');
903 if (tp == NULL) {
904 fprintf(stderr, "line %d: syntax error\n", lineno);
905 errors++;
906 continue;
908 *tp++ = '\0', tp = skip(tp);
909 if (streq(cp, "type")) {
910 if (tp == NULL)
911 tp = unknown;
912 cpp = dktypenames;
913 for (; cpp < &dktypenames[DKMAXTYPES]; cpp++)
914 if (*cpp && streq(*cpp, tp)) {
915 lp->d_type = cpp - dktypenames;
916 break;
918 if (cpp < &dktypenames[DKMAXTYPES])
919 continue;
920 v = strtoul(tp, NULL, 10);
921 if (v >= DKMAXTYPES)
922 fprintf(stderr, "line %d:%s %lu\n", lineno,
923 "Warning, unknown disk type", v);
924 lp->d_type = v;
925 continue;
927 if (streq(cp, "flags")) {
928 for (v = 0; (cp = tp) && *cp != '\0';) {
929 tp = word(cp);
930 if (streq(cp, "removeable"))
931 v |= D_REMOVABLE;
932 else if (streq(cp, "ecc"))
933 v |= D_ECC;
934 else if (streq(cp, "badsect"))
935 v |= D_BADSECT;
936 else {
937 fprintf(stderr,
938 "line %d: %s: bad flag\n",
939 lineno, cp);
940 errors++;
943 lp->d_flags = v;
944 continue;
946 if (streq(cp, "drivedata")) {
947 for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) {
948 lp->d_drivedata[i++] = strtoul(cp, NULL, 10);
949 tp = word(cp);
951 continue;
953 if (sscanf(cp, "%lu partitions", &v) == 1) {
954 if (v == 0 || v > MAXPARTITIONS) {
955 fprintf(stderr,
956 "line %d: bad # of partitions\n", lineno);
957 lp->d_npartitions = MAXPARTITIONS;
958 errors++;
959 } else
960 lp->d_npartitions = v;
961 continue;
963 if (tp == NULL)
964 tp = empty;
965 if (streq(cp, "disk")) {
966 strncpy(lp->d_typename, tp, sizeof (lp->d_typename));
967 continue;
969 if (streq(cp, "label")) {
970 strncpy(lp->d_packname, tp, sizeof (lp->d_packname));
971 continue;
973 if (streq(cp, "bytes/sector")) {
974 v = strtoul(tp, NULL, 10);
975 if (v == 0 || (v % DEV_BSIZE) != 0) {
976 fprintf(stderr,
977 "line %d: %s: bad sector size\n",
978 lineno, tp);
979 errors++;
980 } else
981 lp->d_secsize = v;
982 continue;
984 if (streq(cp, "sectors/track")) {
985 v = strtoul(tp, NULL, 10);
986 #if (ULONG_MAX != 0xffffffffUL)
987 if (v == 0 || v > 0xffffffff) {
988 #else
989 if (v == 0) {
990 #endif
991 fprintf(stderr, "line %d: %s: bad %s\n",
992 lineno, tp, cp);
993 errors++;
994 } else
995 lp->d_nsectors = v;
996 continue;
998 if (streq(cp, "sectors/cylinder")) {
999 v = strtoul(tp, NULL, 10);
1000 if (v == 0) {
1001 fprintf(stderr, "line %d: %s: bad %s\n",
1002 lineno, tp, cp);
1003 errors++;
1004 } else
1005 lp->d_secpercyl = v;
1006 continue;
1008 if (streq(cp, "tracks/cylinder")) {
1009 v = strtoul(tp, NULL, 10);
1010 if (v == 0) {
1011 fprintf(stderr, "line %d: %s: bad %s\n",
1012 lineno, tp, cp);
1013 errors++;
1014 } else
1015 lp->d_ntracks = v;
1016 continue;
1018 if (streq(cp, "cylinders")) {
1019 v = strtoul(tp, NULL, 10);
1020 if (v == 0) {
1021 fprintf(stderr, "line %d: %s: bad %s\n",
1022 lineno, tp, cp);
1023 errors++;
1024 } else
1025 lp->d_ncylinders = v;
1026 continue;
1028 if (streq(cp, "sectors/unit")) {
1029 v = strtoul(tp, NULL, 10);
1030 if (v == 0) {
1031 fprintf(stderr, "line %d: %s: bad %s\n",
1032 lineno, tp, cp);
1033 errors++;
1034 } else
1035 lp->d_secperunit = v;
1036 continue;
1038 if (streq(cp, "rpm")) {
1039 v = strtoul(tp, NULL, 10);
1040 if (v == 0 || v > USHRT_MAX) {
1041 fprintf(stderr, "line %d: %s: bad %s\n",
1042 lineno, tp, cp);
1043 errors++;
1044 } else
1045 lp->d_rpm = v;
1046 continue;
1048 if (streq(cp, "interleave")) {
1049 v = strtoul(tp, NULL, 10);
1050 if (v == 0 || v > USHRT_MAX) {
1051 fprintf(stderr, "line %d: %s: bad %s\n",
1052 lineno, tp, cp);
1053 errors++;
1054 } else
1055 lp->d_interleave = v;
1056 continue;
1058 if (streq(cp, "trackskew")) {
1059 v = strtoul(tp, NULL, 10);
1060 if (v > USHRT_MAX) {
1061 fprintf(stderr, "line %d: %s: bad %s\n",
1062 lineno, tp, cp);
1063 errors++;
1064 } else
1065 lp->d_trackskew = v;
1066 continue;
1068 if (streq(cp, "cylinderskew")) {
1069 v = strtoul(tp, NULL, 10);
1070 if (v > USHRT_MAX) {
1071 fprintf(stderr, "line %d: %s: bad %s\n",
1072 lineno, tp, cp);
1073 errors++;
1074 } else
1075 lp->d_cylskew = v;
1076 continue;
1078 if (streq(cp, "headswitch")) {
1079 v = strtoul(tp, NULL, 10);
1080 lp->d_headswitch = v;
1081 continue;
1083 if (streq(cp, "track-to-track seek")) {
1084 v = strtoul(tp, NULL, 10);
1085 lp->d_trkseek = v;
1086 continue;
1088 /* the ':' was removed above */
1089 if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') {
1090 fprintf(stderr,
1091 "line %d: %s: Unknown disklabel field\n", lineno,
1092 cp);
1093 errors++;
1094 continue;
1097 /* Process a partition specification line. */
1098 part = *cp - 'a';
1099 if (part >= lp->d_npartitions) {
1100 fprintf(stderr,
1101 "line %d: partition name out of range a-%c: %s\n",
1102 lineno, 'a' + lp->d_npartitions - 1, cp);
1103 errors++;
1104 continue;
1106 part_set[part] = 1;
1108 if (getasciipartspec(tp, lp, part, lineno) != 0) {
1109 errors++;
1110 break;
1113 errors += checklabel(lp);
1114 return (errors == 0);
1117 #define NXTNUM(n) do { \
1118 if (tp == NULL) { \
1119 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1120 return (1); \
1121 } else { \
1122 cp = tp, tp = word(cp); \
1123 (n) = strtoul(cp, NULL, 10); \
1125 } while (0)
1127 /* retain 1 character following number */
1128 #define NXTWORD(w,n) do { \
1129 if (tp == NULL) { \
1130 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1131 return (1); \
1132 } else { \
1133 char *tmp; \
1134 cp = tp, tp = word(cp); \
1135 (n) = strtoul(cp, &tmp, 10); \
1136 if (tmp) (w) = *tmp; \
1138 } while (0)
1141 * Read a partition line into partition `part' in the specified disklabel.
1142 * Return 0 on success, 1 on failure.
1145 getasciipartspec(char *tp, struct disklabel *lp, int part, int lineno)
1147 struct partition *pp;
1148 char *cp;
1149 const char **cpp;
1150 u_long v;
1152 pp = &lp->d_partitions[part];
1153 cp = NULL;
1155 v = 0;
1156 NXTWORD(part_size_type[part],v);
1157 if (v == 0 && part_size_type[part] != '*') {
1158 fprintf(stderr,
1159 "line %d: %s: bad partition size\n", lineno, cp);
1160 return (1);
1162 pp->p_size = v;
1164 v = 0;
1165 NXTWORD(part_offset_type[part],v);
1166 if (v == 0 && part_offset_type[part] != '*' &&
1167 part_offset_type[part] != '\0') {
1168 fprintf(stderr,
1169 "line %d: %s: bad partition offset\n", lineno, cp);
1170 return (1);
1172 pp->p_offset = v;
1173 cp = tp, tp = word(cp);
1174 for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++)
1175 if (*cpp && streq(*cpp, cp))
1176 break;
1177 if (*cpp != NULL) {
1178 pp->p_fstype = cpp - fstypenames;
1179 } else {
1180 if (isdigit(*cp))
1181 v = strtoul(cp, NULL, 10);
1182 else
1183 v = FSMAXTYPES;
1184 if (v >= FSMAXTYPES) {
1185 fprintf(stderr,
1186 "line %d: Warning, unknown filesystem type %s\n",
1187 lineno, cp);
1188 v = FS_UNUSED;
1190 pp->p_fstype = v;
1193 switch (pp->p_fstype) {
1194 case FS_UNUSED:
1196 * allow us to accept defaults for
1197 * fsize/frag/cpg
1199 if (tp) {
1200 NXTNUM(pp->p_fsize);
1201 if (pp->p_fsize == 0)
1202 break;
1203 NXTNUM(v);
1204 pp->p_frag = v / pp->p_fsize;
1206 /* else default to 0's */
1207 break;
1209 /* These happen to be the same */
1210 case FS_BSDFFS:
1211 case FS_BSDLFS:
1212 if (tp) {
1213 NXTNUM(pp->p_fsize);
1214 if (pp->p_fsize == 0)
1215 break;
1216 NXTNUM(v);
1217 pp->p_frag = v / pp->p_fsize;
1218 NXTNUM(pp->p_cpg);
1219 } else {
1221 * FIX! poor attempt at adaptive
1223 /* 1 GB */
1224 if (pp->p_size < 1024*1024*1024 / lp->d_secsize) {
1226 * FIX! These are too low, but are traditional
1228 pp->p_fsize = DEFAULT_NEWFS_FRAG;
1229 pp->p_frag = DEFAULT_NEWFS_BLOCK /
1230 DEFAULT_NEWFS_FRAG;
1231 pp->p_cpg = DEFAULT_NEWFS_CPG;
1232 } else {
1233 pp->p_fsize = BIG_NEWFS_FRAG;
1234 pp->p_frag = BIG_NEWFS_BLOCK /
1235 BIG_NEWFS_FRAG;
1236 pp->p_cpg = BIG_NEWFS_CPG;
1239 default:
1240 break;
1242 return (0);
1246 * Check disklabel for errors and fill in
1247 * derived fields according to supplied values.
1250 checklabel(struct disklabel *lp)
1252 struct partition *pp;
1253 int i, errors = 0;
1254 char part;
1255 u_long total_size, total_percent, current_offset;
1256 int seen_default_offset;
1257 int hog_part;
1258 int j;
1259 struct partition *pp2;
1261 if (lp->d_secsize == 0) {
1262 fprintf(stderr, "sector size 0\n");
1263 return (1);
1265 if (lp->d_nsectors == 0) {
1266 fprintf(stderr, "sectors/track 0\n");
1267 return (1);
1269 if (lp->d_ntracks == 0) {
1270 fprintf(stderr, "tracks/cylinder 0\n");
1271 return (1);
1273 if (lp->d_ncylinders == 0) {
1274 fprintf(stderr, "cylinders/unit 0\n");
1275 errors++;
1277 if (lp->d_rpm == 0)
1278 Warning("revolutions/minute 0");
1279 if (lp->d_secpercyl == 0)
1280 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
1281 if (lp->d_secperunit == 0)
1282 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
1283 if (lp->d_bbsize == 0) {
1284 fprintf(stderr, "boot block size 0\n");
1285 errors++;
1286 } else if (lp->d_bbsize % lp->d_secsize)
1287 Warning("boot block size %% sector-size != 0");
1288 if (lp->d_sbsize == 0) {
1289 fprintf(stderr, "super block size 0\n");
1290 errors++;
1291 } else if (lp->d_sbsize % lp->d_secsize)
1292 Warning("super block size %% sector-size != 0");
1293 if (lp->d_npartitions > MAXPARTITIONS)
1294 Warning("number of partitions (%lu) > MAXPARTITIONS (%d)",
1295 (u_long)lp->d_npartitions, MAXPARTITIONS);
1297 /* first allocate space to the partitions, then offsets */
1298 total_size = 0; /* in sectors */
1299 total_percent = 0; /* in percent */
1300 hog_part = -1;
1301 /* find all fixed partitions */
1302 for (i = 0; i < lp->d_npartitions; i++) {
1303 pp = &lp->d_partitions[i];
1304 if (part_set[i]) {
1305 if (part_size_type[i] == '*') {
1306 if (i == RAW_PART) {
1307 pp->p_size = lp->d_secperunit;
1308 } else {
1309 if (hog_part != -1)
1310 Warning("Too many '*' partitions (%c and %c)",
1311 hog_part + 'a',i + 'a');
1312 else
1313 hog_part = i;
1315 } else {
1316 off_t size;
1318 size = pp->p_size;
1319 switch (part_size_type[i]) {
1320 case '%':
1321 total_percent += size;
1322 break;
1323 case 'k':
1324 case 'K':
1325 size *= 1024ULL;
1326 break;
1327 case 'm':
1328 case 'M':
1329 size *= 1024ULL * 1024ULL;
1330 break;
1331 case 'g':
1332 case 'G':
1333 size *= 1024ULL * 1024ULL * 1024ULL;
1334 break;
1335 case '\0':
1336 break;
1337 default:
1338 Warning("unknown size specifier '%c' (K/M/G are valid)",part_size_type[i]);
1339 break;
1341 /* don't count %'s yet */
1342 if (part_size_type[i] != '%') {
1344 * for all not in sectors, convert to
1345 * sectors
1347 if (part_size_type[i] != '\0') {
1348 if (size % lp->d_secsize != 0)
1349 Warning("partition %c not an integer number of sectors",
1350 i + 'a');
1351 size /= lp->d_secsize;
1352 pp->p_size = size;
1354 /* else already in sectors */
1355 if (i != RAW_PART)
1356 total_size += size;
1361 /* handle % partitions - note %'s don't need to add up to 100! */
1362 if (total_percent != 0) {
1363 long free_space = lp->d_secperunit - total_size;
1364 if (total_percent > 100) {
1365 fprintf(stderr,"total percentage %lu is greater than 100\n",
1366 total_percent);
1367 errors++;
1370 if (free_space > 0) {
1371 for (i = 0; i < lp->d_npartitions; i++) {
1372 pp = &lp->d_partitions[i];
1373 if (part_set[i] && part_size_type[i] == '%') {
1374 /* careful of overflows! and integer roundoff */
1375 pp->p_size = ((double)pp->p_size/100) * free_space;
1376 total_size += pp->p_size;
1378 /* FIX we can lose a sector or so due to roundoff per
1379 partition. A more complex algorithm could avoid that */
1382 } else {
1383 fprintf(stderr,
1384 "%ld sectors available to give to '*' and '%%' partitions\n",
1385 free_space);
1386 errors++;
1387 /* fix? set all % partitions to size 0? */
1390 /* give anything remaining to the hog partition */
1391 if (hog_part != -1) {
1392 lp->d_partitions[hog_part].p_size = lp->d_secperunit - total_size;
1393 total_size = lp->d_secperunit;
1396 /* Now set the offsets for each partition */
1397 current_offset = 0; /* in sectors */
1398 seen_default_offset = 0;
1399 for (i = 0; i < lp->d_npartitions; i++) {
1400 part = 'a' + i;
1401 pp = &lp->d_partitions[i];
1402 if (part_set[i]) {
1403 if (part_offset_type[i] == '*') {
1404 if (i == RAW_PART) {
1405 pp->p_offset = 0;
1406 } else {
1407 pp->p_offset = current_offset;
1408 seen_default_offset = 1;
1410 } else {
1411 /* allow them to be out of order for old-style tables */
1412 if (pp->p_offset < current_offset &&
1413 seen_default_offset && i != RAW_PART &&
1414 pp->p_fstype != FS_VINUM) {
1415 fprintf(stderr,
1416 "Offset %ld for partition %c overlaps previous partition which ends at %lu\n",
1417 (long)pp->p_offset,i+'a',current_offset);
1418 fprintf(stderr,
1419 "Labels with any *'s for offset must be in ascending order by sector\n");
1420 errors++;
1421 } else if (pp->p_offset != current_offset &&
1422 i != RAW_PART && seen_default_offset) {
1424 * this may give unneeded warnings if
1425 * partitions are out-of-order
1427 Warning(
1428 "Offset %ld for partition %c doesn't match expected value %ld",
1429 (long)pp->p_offset, i + 'a', current_offset);
1432 if (i != RAW_PART)
1433 current_offset = pp->p_offset + pp->p_size;
1437 for (i = 0; i < lp->d_npartitions; i++) {
1438 part = 'a' + i;
1439 pp = &lp->d_partitions[i];
1440 if (pp->p_size == 0 && pp->p_offset != 0)
1441 Warning("partition %c: size 0, but offset %lu",
1442 part, (u_long)pp->p_offset);
1443 #ifdef notdef
1444 if (pp->p_size % lp->d_secpercyl)
1445 Warning("partition %c: size %% cylinder-size != 0",
1446 part);
1447 if (pp->p_offset % lp->d_secpercyl)
1448 Warning("partition %c: offset %% cylinder-size != 0",
1449 part);
1450 #endif
1451 if (pp->p_offset > lp->d_secperunit) {
1452 fprintf(stderr,
1453 "partition %c: offset past end of unit\n", part);
1454 errors++;
1456 if (pp->p_offset + pp->p_size > lp->d_secperunit) {
1457 fprintf(stderr,
1458 "partition %c: partition extends past end of unit\n",
1459 part);
1460 errors++;
1462 if (i == RAW_PART)
1464 if (pp->p_fstype != FS_UNUSED)
1465 Warning("partition %c is not marked as unused!",part);
1466 if (pp->p_offset != 0)
1467 Warning("partition %c doesn't start at 0!",part);
1468 if (pp->p_size != lp->d_secperunit)
1469 Warning("partition %c doesn't cover the whole unit!",part);
1471 if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) ||
1472 (pp->p_size != lp->d_secperunit)) {
1473 Warning("An incorrect partition %c may cause problems for "
1474 "standard system utilities",part);
1478 /* check for overlaps */
1479 /* this will check for all possible overlaps once and only once */
1480 for (j = 0; j < i; j++) {
1481 pp2 = &lp->d_partitions[j];
1482 if (j != RAW_PART && i != RAW_PART &&
1483 pp->p_fstype != FS_VINUM &&
1484 pp2->p_fstype != FS_VINUM &&
1485 part_set[i] && part_set[j]) {
1486 if (pp2->p_offset < pp->p_offset + pp->p_size &&
1487 (pp2->p_offset + pp2->p_size > pp->p_offset ||
1488 pp2->p_offset >= pp->p_offset)) {
1489 fprintf(stderr,"partitions %c and %c overlap!\n",
1490 j + 'a', i + 'a');
1491 errors++;
1496 for (; i < 8 || i < lp->d_npartitions; i++) {
1497 part = 'a' + i;
1498 pp = &lp->d_partitions[i];
1499 if (pp->p_size || pp->p_offset)
1500 Warning("unused partition %c: size %d offset %lu",
1501 'a' + i, pp->p_size, (u_long)pp->p_offset);
1503 return (errors);
1507 * When operating on a "virgin" disk, try getting an initial label
1508 * from the associated device driver. This might work for all device
1509 * drivers that are able to fetch some initial device parameters
1510 * without even having access to a (BSD) disklabel, like SCSI disks,
1511 * most IDE drives, or vn devices.
1513 * The device name must be given in its "canonical" form.
1515 struct disklabel *
1516 getvirginlabel(void)
1518 static struct disklabel dlab;
1519 char nambuf[BBSIZE];
1520 int f;
1522 if (dkname[0] == '/') {
1523 warnx("\"auto\" requires the usage of a canonical disk name");
1524 return (NULL);
1526 snprintf(nambuf, BBSIZE, "%s%s", _PATH_DEV, dkname);
1527 if ((f = open(nambuf, O_RDONLY)) == -1) {
1528 warn("cannot open %s", nambuf);
1529 return (NULL);
1533 * Try to use the new get-virgin-label ioctl. If it fails,
1534 * fallback to the old get-disdk-info ioctl.
1536 if (ioctl(f, DIOCGDVIRGIN, &dlab) < 0) {
1537 if (ioctl(f, DIOCGDINFO, &dlab) < 0) {
1538 warn("ioctl DIOCGDINFO");
1539 close(f);
1540 return (NULL);
1543 close(f);
1544 dlab.d_boot0 = NULL;
1545 dlab.d_boot1 = NULL;
1546 return (&dlab);
1550 * If we are installing a boot program that doesn't fit in d_bbsize
1551 * we need to mark those partitions that the boot overflows into.
1552 * This allows newfs to prevent creation of a filesystem where it might
1553 * clobber bootstrap code.
1555 void
1556 setbootflag(struct disklabel *lp)
1558 struct partition *pp;
1559 int i, errors = 0;
1560 char part;
1561 u_long boffset;
1563 if (bootbuf == 0)
1564 return;
1565 boffset = bootsize / lp->d_secsize;
1566 for (i = 0; i < lp->d_npartitions; i++) {
1567 part = 'a' + i;
1568 pp = &lp->d_partitions[i];
1569 if (pp->p_size == 0)
1570 continue;
1571 if (boffset <= pp->p_offset) {
1572 if (pp->p_fstype == FS_BOOT)
1573 pp->p_fstype = FS_UNUSED;
1574 } else if (pp->p_fstype != FS_BOOT) {
1575 if (pp->p_fstype != FS_UNUSED) {
1576 fprintf(stderr,
1577 "boot overlaps used partition %c\n",
1578 part);
1579 errors++;
1580 } else {
1581 pp->p_fstype = FS_BOOT;
1582 Warning("boot overlaps partition %c, %s",
1583 part, "marked as FS_BOOT");
1587 if (errors)
1588 errx(4, "cannot install boot program");
1591 /*VARARGS1*/
1592 void
1593 Warning(const char *fmt, ...)
1595 va_list ap;
1597 fprintf(stderr, "Warning, ");
1598 va_start(ap, fmt);
1599 vfprintf(stderr, fmt, ap);
1600 fprintf(stderr, "\n");
1601 va_end(ap);
1605 * Check to see if the bootblocks are in the wrong place. FBsd5 bootblocks
1606 * and earlier DFly bb's are packed against the old disklabel and a new
1607 * disklabel would blow them up. This is a hack that should be removed
1608 * in 2006 sometime (if ever).
1612 checkoldboot(int f, const char *bootbuffer)
1614 char buf[BBSIZE];
1616 if (bootbuffer && strncmp(bootbuffer + 0x402, "BTX", 3) == 0)
1617 return(0);
1618 lseek(f, (off_t)0, SEEK_SET);
1619 if (read(f, buf, sizeof(buf)) != sizeof(buf))
1620 return(0);
1621 if (strncmp(buf + 0x402, "BTX", 3) == 0) /* new location */
1622 return(0);
1623 if (strncmp(buf + 0x316, "BTX", 3) == 0) /* old location */
1624 return(1);
1625 return(0);
1628 void
1629 usage(void)
1631 #if NUMBOOT > 0
1632 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
1633 "usage: disklabel [-r] disk",
1634 "\t\t(to read label)",
1635 " disklabel -w [-r] [-n] disk type [ packid ]",
1636 "\t\t(to write label with existing boot program)",
1637 " disklabel -e [-r] [-n] disk",
1638 "\t\t(to edit label)",
1639 " disklabel -R [-r] [-n] disk protofile",
1640 "\t\t(to restore label with existing boot program)",
1641 #if NUMBOOT > 1
1642 " disklabel -B [-n] [ -b boot1 [ -s boot2 ] ] disk [ type ]",
1643 "\t\t(to install boot program with existing label)",
1644 " disklabel -w -B [-n] [ -b boot1 [ -s boot2 ] ] disk type [ packid ]",
1645 "\t\t(to write label and boot program)",
1646 " disklabel -R -B [-n] [ -b boot1 [ -s boot2 ] ] disk protofile [ type ]",
1647 "\t\t(to restore label and boot program)",
1648 #else
1649 " disklabel -B [-n] [ -b bootprog ] disk [ type ]",
1650 "\t\t(to install boot program with existing on-disk label)",
1651 " disklabel -w -B [-n] [ -b bootprog ] disk type [ packid ]",
1652 "\t\t(to write label and install boot program)",
1653 " disklabel -R -B [-n] [ -b bootprog ] disk protofile [ type ]",
1654 "\t\t(to restore label and install boot program)",
1655 #endif
1656 " disklabel [-NW] disk",
1657 "\t\t(to write disable/enable label)");
1658 #else
1659 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
1660 "usage: disklabel [-r] disk", "(to read label)",
1661 " disklabel -w [-r] [-n] disk type [ packid ]",
1662 "\t\t(to write label)",
1663 " disklabel -e [-r] [-n] disk",
1664 "\t\t(to edit label)",
1665 " disklabel -R [-r] [-n] disk protofile",
1666 "\t\t(to restore label)",
1667 " disklabel [-NW] disk",
1668 "\t\t(to write disable/enable label)");
1669 #endif
1670 exit(1);