zone.9: Adjust for the removal of the 'zalloc' arg to zinit/zinitna.
[dragonfly.git] / sbin / disklabel32 / disklabel.c
blobeb3d90d770d04663e772b2b2595c54203b4e3ee0
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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
32 * @(#) Copyright (c) 1987, 1993 The Regents of the University of California. All rights reserved.
33 * @(#)disklabel.c 1.2 (Symmetric) 11/28/85
34 * @(#)disklabel.c 8.2 (Berkeley) 1/7/94
35 * $FreeBSD: src/sbin/disklabel/disklabel.c,v 1.28.2.15 2003/01/24 16:18:16 des Exp $
38 #include <sys/param.h>
39 #include <sys/file.h>
40 #include <sys/stat.h>
41 #include <sys/wait.h>
42 #define DKTYPENAMES
43 #include <sys/disklabel32.h>
44 #include <sys/disklabel64.h>
45 #include <sys/diskslice.h>
46 #include <sys/diskmbr.h>
47 #include <sys/dtype.h>
48 #include <sys/sysctl.h>
49 #include <disktab.h>
50 #include <fstab.h>
52 #include <vfs/ufs/dinode.h>
53 #include <vfs/ufs/fs.h>
55 #include <unistd.h>
56 #include <string.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <signal.h>
60 #include <stdarg.h>
61 #include <ctype.h>
62 #include <err.h>
63 #include <errno.h>
64 #include "pathnames.h"
67 * Disklabel32: read and write 32 bit disklabels.
68 * The label is usually placed on one of the first sectors of the disk.
69 * Many machines also place a bootstrap in the same area,
70 * in which case the label is embedded in the bootstrap.
71 * The bootstrap source must leave space at the proper offset
72 * for the label on such machines.
75 #ifndef BBSIZE
76 #define BBSIZE 8192 /* size of boot area, with label */
77 #endif
79 /* FIX! These are too low, but are traditional */
80 #define DEFAULT_NEWFS_BLOCK 8192U
81 #define DEFAULT_NEWFS_FRAG 1024U
82 #define DEFAULT_NEWFS_CPG 16U
84 #define BIG_NEWFS_BLOCK 16384U
85 #define BIG_NEWFS_FRAG 2048U
86 #define BIG_NEWFS_CPG 64U
88 #define NUMBOOT 2
90 void makelabel(const char *, const char *, struct disklabel32 *);
91 int writelabel(int, const char *, struct disklabel32 *);
92 void l_perror(const char *);
93 struct disklabel32 *readlabel(int);
94 struct disklabel32 *makebootarea(char *, struct disklabel32 *, int);
95 void display(FILE *, const struct disklabel32 *);
96 int edit(struct disklabel32 *, int);
97 int editit(void);
98 char *skip(char *);
99 char *word(char *);
100 int getasciilabel(FILE *, struct disklabel32 *);
101 int getasciipartspec(char *, struct disklabel32 *, int, int);
102 int checklabel(struct disklabel32 *);
103 void setbootflag(struct disklabel32 *);
104 void Warning(const char *, ...) __printflike(1, 2);
105 void usage(void);
106 int checkoldboot(int, const char *);
107 const char *fixlabel(int, struct disklabel32 *, int);
108 struct disklabel32 *getvirginlabel(void);
109 struct disklabel32 *getdisklabelfromdisktab(const char *name);
111 #define DEFEDITOR _PATH_VI
112 #define streq(a,b) (strcmp(a,b) == 0)
114 char *dkname;
115 char *specname;
116 char tmpfil[] = PATH_TMPFILE;
118 char namebuf[BBSIZE];
119 struct disklabel32 lab;
120 char bootarea[BBSIZE];
122 #define MAX_PART ('z')
123 #define MAX_NUM_PARTS (1 + MAX_PART - 'a')
124 char part_size_type[MAX_NUM_PARTS];
125 char part_offset_type[MAX_NUM_PARTS];
126 int part_set[MAX_NUM_PARTS];
128 #if NUMBOOT > 0
129 int installboot; /* non-zero if we should install a boot program */
130 char *bootbuf; /* pointer to buffer with remainder of boot prog */
131 int bootsize; /* size of remaining boot program */
132 char *xxboot; /* primary boot */
133 char *bootxx; /* secondary boot */
134 char boot0[MAXPATHLEN];
135 char boot1[MAXPATHLEN];
136 #endif
138 enum {
139 UNSPEC, EDIT, NOWRITE, READ, RESTORE, WRITE, WRITEABLE, WRITEBOOT
140 } op = UNSPEC;
142 int rflag;
143 int disable_write; /* set to disable writing to disk label */
144 int forceflag;
145 u_int32_t slice_start_lba;
147 #ifdef DEBUG
148 int debug;
149 #define OPTIONS "BNRWb:def:nrs:w"
150 #else
151 #define OPTIONS "BNRWb:ef:nrs:w"
152 #endif
155 main(int argc, char *argv[])
157 struct disklabel32 *lp;
158 FILE *t;
159 int ch, f = 0, flag, error = 0;
160 char *name = NULL;
162 while ((ch = getopt(argc, argv, OPTIONS)) != -1)
163 switch (ch) {
164 #if NUMBOOT > 0
165 case 'B':
166 ++installboot;
167 break;
168 case 'b':
169 xxboot = optarg;
170 break;
172 case 'f':
173 forceflag = 1;
174 slice_start_lba = strtoul(optarg, NULL, 0);
175 break;
176 #if NUMBOOT > 1
177 case 's':
178 bootxx = optarg;
179 break;
180 #endif
181 #endif
182 case 'N':
183 if (op != UNSPEC)
184 usage();
185 op = NOWRITE;
186 break;
187 case 'n':
188 disable_write = 1;
189 break;
190 case 'R':
191 if (op != UNSPEC)
192 usage();
193 op = RESTORE;
194 break;
195 case 'W':
196 if (op != UNSPEC)
197 usage();
198 op = WRITEABLE;
199 break;
200 case 'e':
201 if (op != UNSPEC)
202 usage();
203 op = EDIT;
204 break;
205 case 'r':
206 ++rflag;
207 break;
208 case 'w':
209 if (op != UNSPEC)
210 usage();
211 op = WRITE;
212 break;
213 #ifdef DEBUG
214 case 'd':
215 debug++;
216 break;
217 #endif
218 case '?':
219 default:
220 usage();
222 argc -= optind;
223 argv += optind;
224 #if NUMBOOT > 0
225 if (installboot) {
226 rflag++;
227 if (op == UNSPEC)
228 op = WRITEBOOT;
229 } else {
230 if (op == UNSPEC)
231 op = READ;
232 xxboot = bootxx = NULL;
234 #else
235 if (op == UNSPEC)
236 op = READ;
237 #endif
238 if (argc < 1)
239 usage();
241 dkname = getdevpath(argv[0], 0);
242 specname = dkname;
243 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 disklabel32 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 disklabel32 *lp)
349 struct disklabel32 *dp;
351 if (strcmp(type, "auto") == 0)
352 dp = getvirginlabel();
353 else
354 dp = getdisklabelfromdisktab(type);
355 if (dp == NULL)
356 errx(1, "%s: unknown disk type", type);
357 *lp = *dp;
360 * NOTE: boot control files may no longer be specified in disktab.
362 if (name)
363 strncpy(lp->d_packname, name, sizeof(lp->d_packname));
367 writelabel(int f, const char *boot, struct disklabel32 *lp)
369 const char *msg;
370 int flag;
371 int r;
373 if (disable_write) {
374 Warning("write to disk label suppressed - label was as follows:");
375 display(stdout, lp);
376 return (0);
377 } else {
378 /* make sure we are not overwriting our boot code */
379 if (checkoldboot(f, boot))
380 errx(4, "Will not overwrite old bootblocks w/ label, install new boot blocks first!");
381 setbootflag(lp);
382 lp->d_magic = DISKMAGIC32;
383 lp->d_magic2 = DISKMAGIC32;
384 lp->d_checksum = 0;
385 lp->d_checksum = dkcksum32(lp);
386 if (rflag) {
388 * First set the kernel disk label,
389 * then write a label to the raw disk.
390 * If the SDINFO ioctl fails because it is unimplemented,
391 * keep going; otherwise, the kernel consistency checks
392 * may prevent us from changing the current (in-core)
393 * label.
395 if (ioctl(f, DIOCSDINFO32, lp) < 0 &&
396 errno != ENODEV && errno != ENOTTY) {
397 l_perror("ioctl DIOCSDINFO32");
398 return (1);
400 lseek(f, (off_t)0, SEEK_SET);
403 * write enable label sector before write
404 * (if necessary), disable after writing.
406 flag = 1;
407 if (ioctl(f, DIOCWLABEL, &flag) < 0)
408 warn("ioctl DIOCWLABEL");
409 msg = fixlabel(f, lp, 1);
410 if (msg) {
411 warn("%s", msg);
412 return (1);
414 r = write(f, boot, lp->d_bbsize);
415 fixlabel(f, lp, 0);
416 if (r != ((ssize_t)lp->d_bbsize)) {
417 warn("write");
418 return (1);
420 #if NUMBOOT > 0
422 * Output the remainder of the disklabel
424 if (bootbuf) {
425 fixlabel(f, lp, 1);
426 r = write(f, bootbuf, bootsize);
427 fixlabel(f, lp, 0);
428 if (r != bootsize) {
429 warn("write");
430 return(1);
433 #endif
434 flag = 0;
435 ioctl(f, DIOCWLABEL, &flag);
436 } else if (ioctl(f, DIOCWDINFO32, lp) < 0) {
437 l_perror("ioctl DIOCWDINFO32");
438 return (1);
441 return (0);
444 void
445 l_perror(const char *s)
447 switch (errno) {
449 case ESRCH:
450 warnx("%s: no disk label on disk;", s);
451 fprintf(stderr, "add \"-r\" to install initial label\n");
452 break;
454 case EINVAL:
455 warnx("%s: label magic number or checksum is wrong!", s);
456 fprintf(stderr, "(disklabel or kernel is out of date?)\n");
457 break;
459 case EBUSY:
460 warnx("%s: open partition would move or shrink", s);
461 break;
463 case EXDEV:
464 warnx("%s: '%c' partition must start at beginning of disk",
465 s, 'a' + RAW_PART);
466 break;
468 case ENOATTR:
469 warnx("%s: the disk already has a label of a different type,\n"
470 "probably a 64 bit disklabel. It must be cleaned out "
471 "first.\n", s);
472 break;
474 default:
475 warn(NULL);
476 break;
481 * Fetch disklabel for disk.
482 * Use ioctl to get label unless -r flag is given.
484 struct disklabel32 *
485 readlabel(int f)
487 const char *msg;
488 struct disklabel32 *lp;
489 int r;
491 if (rflag) {
492 r = read(f, bootarea, BBSIZE);
493 if (r < BBSIZE)
494 err(4, "%s", specname);
495 for (lp = (struct disklabel32 *)bootarea;
496 lp <= (struct disklabel32 *)(bootarea + BBSIZE - sizeof(*lp));
497 lp = (struct disklabel32 *)((char *)lp + 16)) {
498 if (lp->d_magic == DISKMAGIC32 &&
499 lp->d_magic2 == DISKMAGIC32)
500 break;
502 if (lp > (struct disklabel32 *)(bootarea+BBSIZE-sizeof(*lp)) ||
503 lp->d_magic != DISKMAGIC32 || lp->d_magic2 != DISKMAGIC32 ||
504 dkcksum32(lp) != 0) {
505 errx(1, "bad pack magic number (label is damaged, "
506 "or pack is unlabeled)");
508 if ((msg = fixlabel(f, lp, 0)) != NULL)
509 errx(1, "%s", msg);
510 } else {
511 lp = &lab;
512 if (ioctl(f, DIOCGDINFO32, lp) < 0) {
513 l_perror("ioctl DIOCGDINFO32");
514 exit(4);
517 return (lp);
521 * Construct a bootarea (d_bbsize bytes) in the specified buffer ``boot''
522 * Returns a pointer to the disklabel portion of the bootarea.
524 struct disklabel32 *
525 makebootarea(char *boot, struct disklabel32 *dp, int f)
527 struct disklabel32 *lp;
528 char *p;
529 int b;
530 #if NUMBOOT > 0
531 struct stat sb;
532 #endif
533 #ifdef __i386__
534 char *tmpbuf;
535 unsigned int i, found;
536 #endif
538 /* XXX */
539 if (dp->d_secsize == 0) {
540 dp->d_secsize = DEV_BSIZE;
541 dp->d_bbsize = BBSIZE;
543 lp = (struct disklabel32 *)
544 (boot + (LABELSECTOR32 * dp->d_secsize) + LABELOFFSET32);
545 bzero((char *)lp, sizeof *lp);
546 #if NUMBOOT > 0
548 * If we are not installing a boot program but we are installing a
549 * label on disk then we must read the current bootarea so we don't
550 * clobber the existing boot.
552 if (!installboot) {
553 if (rflag) {
554 if (read(f, boot, BBSIZE) < BBSIZE)
555 err(4, "%s", specname);
556 bzero((char *)lp, sizeof *lp);
558 return (lp);
561 * We are installing a boot program. Determine the name(s) and
562 * read them into the appropriate places in the boot area.
564 if (!xxboot || !bootxx) {
565 if (!xxboot) {
566 sprintf(boot0, "%s/boot1", _PATH_BOOTDIR);
567 xxboot = boot0;
569 #if NUMBOOT > 1
570 if (!bootxx) {
571 sprintf(boot1, "%s/boot2", _PATH_BOOTDIR);
572 bootxx = boot1;
574 #endif
576 #ifdef DEBUG
577 if (debug)
578 fprintf(stderr, "bootstraps: xxboot = %s, bootxx = %s\n",
579 xxboot, bootxx ? bootxx : "NONE");
580 #endif
583 * Strange rules:
584 * 1. One-piece bootstrap (hp300/hp800)
585 * up to d_bbsize bytes of ``xxboot'' go in bootarea, the rest
586 * is remembered and written later following the bootarea.
587 * 2. Two-piece bootstraps (vax/i386?/mips?)
588 * up to d_secsize bytes of ``xxboot'' go in first d_secsize
589 * bytes of bootarea, remaining d_bbsize-d_secsize filled
590 * from ``bootxx''.
592 b = open(xxboot, O_RDONLY);
593 if (b < 0)
594 err(4, "%s", xxboot);
595 #if NUMBOOT > 1
596 #ifdef __i386__
598 * XXX Botch alert.
599 * The i386 has the so-called fdisk table embedded into the
600 * primary bootstrap. We take care to not clobber it, but
601 * only if it does already contain some data. (Otherwise,
602 * the xxboot provides a template.)
604 if ((tmpbuf = (char *)malloc((int)dp->d_secsize)) == NULL)
605 err(4, "%s", xxboot);
606 memcpy((void *)tmpbuf, (void *)boot, (int)dp->d_secsize);
607 #endif /* i386 */
608 if (read(b, boot, (int)dp->d_secsize) < 0)
609 err(4, "%s", xxboot);
610 close(b);
611 #ifdef __i386__
612 for (i = DOSPARTOFF, found = 0;
613 !found && i < DOSPARTOFF + NDOSPART*sizeof(struct dos_partition);
614 i++)
615 found = tmpbuf[i] != 0;
616 if (found)
617 memcpy((void *)&boot[DOSPARTOFF],
618 (void *)&tmpbuf[DOSPARTOFF],
619 NDOSPART * sizeof(struct dos_partition));
620 free(tmpbuf);
621 #endif /* i386 */
622 b = open(bootxx, O_RDONLY);
623 if (b < 0)
624 err(4, "%s", bootxx);
625 if (fstat(b, &sb) != 0)
626 err(4, "%s", bootxx);
627 if (dp->d_secsize + sb.st_size > dp->d_bbsize)
628 errx(4, "%s too large", bootxx);
629 if (read(b, &boot[dp->d_secsize],
630 (int)(dp->d_bbsize-dp->d_secsize)) < 0)
631 err(4, "%s", bootxx);
632 #else /* !(NUMBOOT > 1) */
633 if (read(b, boot, (int)dp->d_bbsize) < 0)
634 err(4, "%s", xxboot);
635 if (fstat(b, &sb) != 0)
636 err(4, "%s", xxboot);
637 bootsize = (int)sb.st_size - dp->d_bbsize;
638 if (bootsize > 0) {
639 /* XXX assume d_secsize is a power of two */
640 bootsize = roundup2(bootsize, dp->d_secsize);
641 bootbuf = (char *)malloc((size_t)bootsize);
642 if (bootbuf == NULL)
643 err(4, "%s", xxboot);
644 if (read(b, bootbuf, bootsize) < 0) {
645 free(bootbuf);
646 err(4, "%s", xxboot);
649 #endif /* NUMBOOT > 1 */
650 close(b);
651 #endif /* NUMBOOT > 0 */
653 * Make sure no part of the bootstrap is written in the area
654 * reserved for the label.
656 for (p = (char *)lp; p < (char *)lp + sizeof(struct disklabel32); p++)
657 if (*p)
658 errx(2, "bootstrap doesn't leave room for disk label");
659 return (lp);
662 void
663 display(FILE *f, const struct disklabel32 *lp)
665 int i, j;
666 const struct partition32 *pp;
668 fprintf(f, "# %s:\n", specname);
669 if (lp->d_type < DKMAXTYPES)
670 fprintf(f, "type: %s\n", dktypenames[lp->d_type]);
671 else
672 fprintf(f, "type: %u\n", lp->d_type);
673 fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename),
674 lp->d_typename);
675 fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname),
676 lp->d_packname);
677 fprintf(f, "flags:");
678 fprintf(f, "\n");
679 fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize);
680 fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors);
681 fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks);
682 fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl);
683 fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders);
684 fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit);
685 fprintf(f, "rpm: %u\n", lp->d_rpm);
686 fprintf(f, "interleave: %u\n", lp->d_interleave);
687 fprintf(f, "trackskew: %u\n", lp->d_trackskew);
688 fprintf(f, "cylinderskew: %u\n", lp->d_cylskew);
689 fprintf(f, "headswitch: %lu\t\t# milliseconds\n",
690 (u_long)lp->d_headswitch);
691 fprintf(f, "track-to-track seek: %ld\t# milliseconds\n",
692 (u_long)lp->d_trkseek);
693 fprintf(f, "drivedata: ");
694 for (i = NDDATA32 - 1; i >= 0; i--) {
695 if (lp->d_drivedata[i])
696 break;
698 if (i < 0)
699 i = 0;
700 for (j = 0; j <= i; j++)
701 fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]);
702 fprintf(f, "\n\n%u partitions:\n", lp->d_npartitions);
703 fprintf(f,
704 "# size offset fstype\n");
705 pp = lp->d_partitions;
706 for (i = 0; i < lp->d_npartitions; i++, pp++) {
707 if (pp->p_size) {
708 u_long onemeg = 1024 * 1024 / lp->d_secsize;
709 fprintf(f, " %c: ", 'a' + i);
711 fprintf(f, "%10lu ", (u_long)pp->p_size);
712 fprintf(f, "%10lu ", (u_long)pp->p_offset);
713 if (pp->p_fstype < FSMAXTYPES)
714 fprintf(f, "%8.8s", fstypenames[pp->p_fstype]);
715 else
716 fprintf(f, "%8d", pp->p_fstype);
718 fprintf(f, "\t# %11.3fMB", (double)pp->p_size / onemeg);
719 fprintf(f, "\n");
722 fflush(f);
726 edit(struct disklabel32 *lp, int f)
728 int c, fd;
729 struct disklabel32 label;
730 FILE *fp;
732 if ((fd = mkstemp(tmpfil)) == -1 ||
733 (fp = fdopen(fd, "w")) == NULL) {
734 warnx("can't create %s", tmpfil);
735 return (1);
737 display(fp, lp);
738 fclose(fp);
739 for (;;) {
740 if (!editit())
741 break;
742 fp = fopen(tmpfil, "r");
743 if (fp == NULL) {
744 warnx("can't reopen %s for reading", tmpfil);
745 break;
747 bzero((char *)&label, sizeof(label));
748 if (getasciilabel(fp, &label)) {
749 *lp = label;
750 if (writelabel(f, bootarea, lp) == 0) {
751 fclose(fp);
752 unlink(tmpfil);
753 return (0);
756 fclose(fp);
757 printf("re-edit the label? [y]: "); fflush(stdout);
758 c = getchar();
759 if (c != EOF && c != (int)'\n')
760 while (getchar() != (int)'\n')
762 if (c == (int)'n')
763 break;
765 unlink(tmpfil);
766 return (1);
770 editit(void)
772 int pid, xpid;
773 int status, omask;
774 const char *ed;
776 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
777 while ((pid = fork()) < 0) {
778 if (errno == EPROCLIM) {
779 warnx("you have too many processes");
780 return(0);
782 if (errno != EAGAIN) {
783 warn("fork");
784 return(0);
786 sleep(1);
788 if (pid == 0) {
789 sigsetmask(omask);
790 setgid(getgid());
791 setuid(getuid());
792 if ((ed = getenv("EDITOR")) == NULL)
793 ed = DEFEDITOR;
794 execlp(ed, ed, tmpfil, NULL);
795 err(1, "%s", ed);
797 while ((xpid = wait(&status)) >= 0)
798 if (xpid == pid)
799 break;
800 sigsetmask(omask);
801 return(!status);
804 char *
805 skip(char *cp)
808 while (*cp != '\0' && isspace(*cp))
809 cp++;
810 if (*cp == '\0' || *cp == '#')
811 return (NULL);
812 return (cp);
815 char *
816 word(char *cp)
818 char c;
820 while (*cp != '\0' && !isspace(*cp) && *cp != '#')
821 cp++;
822 if ((c = *cp) != '\0') {
823 *cp++ = '\0';
824 if (c != '#')
825 return (skip(cp));
827 return (NULL);
831 * Read an ascii label in from fd f,
832 * in the same format as that put out by display(),
833 * and fill in lp.
836 getasciilabel(FILE *f, struct disklabel32 *lp)
838 char *cp;
839 const char **cpp;
840 u_int part;
841 char *tp, line[BUFSIZ];
842 u_long v;
843 int lineno = 0, errors = 0;
844 int i;
845 char empty[] = "";
846 char unknown[] = "unknown";
848 bzero(&part_set, sizeof(part_set));
849 bzero(&part_size_type, sizeof(part_size_type));
850 bzero(&part_offset_type, sizeof(part_offset_type));
851 lp->d_bbsize = BBSIZE; /* XXX */
852 lp->d_sbsize = SBSIZE; /* XXX */
853 while (fgets(line, sizeof(line) - 1, f)) {
854 lineno++;
855 if ((cp = strchr(line,'\n')) != NULL)
856 *cp = '\0';
857 cp = skip(line);
858 if (cp == NULL)
859 continue;
860 tp = strchr(cp, ':');
861 if (tp == NULL) {
862 fprintf(stderr, "line %d: syntax error\n", lineno);
863 errors++;
864 continue;
866 *tp++ = '\0', tp = skip(tp);
867 if (streq(cp, "type")) {
868 if (tp == NULL)
869 tp = unknown;
870 cpp = dktypenames;
871 for (; cpp < &dktypenames[DKMAXTYPES]; cpp++) {
872 if (*cpp && strcasecmp(*cpp, tp) == 0) {
873 lp->d_type = cpp - dktypenames;
874 break;
877 if (cpp < &dktypenames[DKMAXTYPES])
878 continue;
879 v = strtoul(tp, NULL, 10);
880 if (v >= DKMAXTYPES) {
881 fprintf(stderr, "line %d:%s %lu\n", lineno,
882 "Warning, unknown disk type", v);
884 lp->d_type = v;
885 continue;
887 if (streq(cp, "flags")) {
888 for (v = 0; (cp = tp) && *cp != '\0';) {
889 tp = word(cp);
890 if (streq(cp, "removeable"))
891 v |= 0; /* obsolete */
892 else if (streq(cp, "ecc"))
893 v |= 0; /* obsolete */
894 else if (streq(cp, "badsect"))
895 v |= 0; /* obsolete */
896 else {
897 fprintf(stderr,
898 "line %d: %s: bad flag\n",
899 lineno, cp);
900 errors++;
903 lp->d_flags = v;
904 continue;
906 if (streq(cp, "drivedata")) {
907 for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA32;) {
908 lp->d_drivedata[i++] = strtoul(cp, NULL, 10);
909 tp = word(cp);
911 continue;
913 if (sscanf(cp, "%lu partitions", &v) == 1) {
914 if (v == 0 || v > MAXPARTITIONS32) {
915 fprintf(stderr,
916 "line %d: bad # of partitions\n", lineno);
917 lp->d_npartitions = MAXPARTITIONS32;
918 errors++;
919 } else
920 lp->d_npartitions = v;
921 continue;
923 if (tp == NULL)
924 tp = empty;
925 if (streq(cp, "disk")) {
926 strncpy(lp->d_typename, tp, sizeof (lp->d_typename));
927 continue;
929 if (streq(cp, "label")) {
930 strncpy(lp->d_packname, tp, sizeof (lp->d_packname));
931 continue;
933 if (streq(cp, "bytes/sector")) {
934 v = strtoul(tp, NULL, 10);
935 if (v == 0 || (v % DEV_BSIZE) != 0) {
936 fprintf(stderr,
937 "line %d: %s: bad sector size\n",
938 lineno, tp);
939 errors++;
940 } else
941 lp->d_secsize = v;
942 continue;
944 if (streq(cp, "sectors/track")) {
945 v = strtoul(tp, NULL, 10);
946 #if (ULONG_MAX != 0xffffffffUL)
947 if (v == 0 || v > 0xffffffff) {
948 #else
949 if (v == 0) {
950 #endif
951 fprintf(stderr, "line %d: %s: bad %s\n",
952 lineno, tp, cp);
953 errors++;
954 } else
955 lp->d_nsectors = v;
956 continue;
958 if (streq(cp, "sectors/cylinder")) {
959 v = strtoul(tp, NULL, 10);
960 if (v == 0) {
961 fprintf(stderr, "line %d: %s: bad %s\n",
962 lineno, tp, cp);
963 errors++;
964 } else
965 lp->d_secpercyl = v;
966 continue;
968 if (streq(cp, "tracks/cylinder")) {
969 v = strtoul(tp, NULL, 10);
970 if (v == 0) {
971 fprintf(stderr, "line %d: %s: bad %s\n",
972 lineno, tp, cp);
973 errors++;
974 } else
975 lp->d_ntracks = v;
976 continue;
978 if (streq(cp, "cylinders")) {
979 v = strtoul(tp, NULL, 10);
980 if (v == 0) {
981 fprintf(stderr, "line %d: %s: bad %s\n",
982 lineno, tp, cp);
983 errors++;
984 } else
985 lp->d_ncylinders = v;
986 continue;
988 if (streq(cp, "sectors/unit")) {
989 v = strtoul(tp, NULL, 10);
990 if (v == 0) {
991 fprintf(stderr, "line %d: %s: bad %s\n",
992 lineno, tp, cp);
993 errors++;
994 } else
995 lp->d_secperunit = v;
996 continue;
998 if (streq(cp, "rpm")) {
999 v = strtoul(tp, NULL, 10);
1000 if (v == 0 || v > USHRT_MAX) {
1001 fprintf(stderr, "line %d: %s: bad %s\n",
1002 lineno, tp, cp);
1003 errors++;
1004 } else
1005 lp->d_rpm = v;
1006 continue;
1008 if (streq(cp, "interleave")) {
1009 v = strtoul(tp, NULL, 10);
1010 if (v == 0 || v > USHRT_MAX) {
1011 fprintf(stderr, "line %d: %s: bad %s\n",
1012 lineno, tp, cp);
1013 errors++;
1014 } else
1015 lp->d_interleave = v;
1016 continue;
1018 if (streq(cp, "trackskew")) {
1019 v = strtoul(tp, NULL, 10);
1020 if (v > USHRT_MAX) {
1021 fprintf(stderr, "line %d: %s: bad %s\n",
1022 lineno, tp, cp);
1023 errors++;
1024 } else
1025 lp->d_trackskew = v;
1026 continue;
1028 if (streq(cp, "cylinderskew")) {
1029 v = strtoul(tp, NULL, 10);
1030 if (v > USHRT_MAX) {
1031 fprintf(stderr, "line %d: %s: bad %s\n",
1032 lineno, tp, cp);
1033 errors++;
1034 } else
1035 lp->d_cylskew = v;
1036 continue;
1038 if (streq(cp, "headswitch")) {
1039 v = strtoul(tp, NULL, 10);
1040 lp->d_headswitch = v;
1041 continue;
1043 if (streq(cp, "track-to-track seek")) {
1044 v = strtoul(tp, NULL, 10);
1045 lp->d_trkseek = v;
1046 continue;
1048 /* the ':' was removed above */
1049 if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') {
1050 fprintf(stderr,
1051 "line %d: %s: Unknown disklabel field\n", lineno,
1052 cp);
1053 errors++;
1054 continue;
1057 /* Process a partition specification line. */
1058 part = *cp - 'a';
1059 if (part >= lp->d_npartitions) {
1060 fprintf(stderr,
1061 "line %d: partition name out of range a-%c: %s\n",
1062 lineno, 'a' + lp->d_npartitions - 1, cp);
1063 errors++;
1064 continue;
1066 part_set[part] = 1;
1068 if (getasciipartspec(tp, lp, part, lineno) != 0) {
1069 errors++;
1070 break;
1073 errors += checklabel(lp);
1074 return (errors == 0);
1077 #define NXTNUM(n) do { \
1078 if (tp == NULL) { \
1079 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1080 return (1); \
1081 } else { \
1082 cp = tp, tp = word(cp); \
1083 (n) = strtoul(cp, NULL, 10); \
1085 } while (0)
1087 /* retain 1 character following number */
1088 #define NXTWORD(w,n) do { \
1089 if (tp == NULL) { \
1090 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1091 return (1); \
1092 } else { \
1093 char *tmp; \
1094 cp = tp, tp = word(cp); \
1095 (n) = strtoul(cp, &tmp, 10); \
1096 if (tmp) (w) = *tmp; \
1098 } while (0)
1101 * Read a partition line into partition `part' in the specified disklabel.
1102 * Return 0 on success, 1 on failure.
1105 getasciipartspec(char *tp, struct disklabel32 *lp, int part, int lineno)
1107 struct partition32 *pp;
1108 char *cp;
1109 const char **cpp;
1110 u_long v;
1112 pp = &lp->d_partitions[part];
1113 cp = NULL;
1116 * size
1118 v = 0;
1119 NXTWORD(part_size_type[part],v);
1120 if (v == 0 && part_size_type[part] != '*') {
1121 fprintf(stderr,
1122 "line %d: %s: bad partition size\n", lineno, cp);
1123 return (1);
1125 pp->p_size = v;
1128 * offset
1130 v = 0;
1131 NXTWORD(part_offset_type[part],v);
1132 if (v == 0 && part_offset_type[part] != '*' &&
1133 part_offset_type[part] != '\0') {
1134 fprintf(stderr,
1135 "line %d: %s: bad partition offset\n", lineno, cp);
1136 return (1);
1138 pp->p_offset = v;
1141 * fstype
1143 if (tp == NULL) {
1144 fprintf(stderr,
1145 "line %d: no filesystem type was specified\n", lineno);
1146 return(1);
1148 cp = tp;
1149 tp = word(cp);
1150 for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++) {
1151 if (*cpp && strcasecmp(*cpp, cp) == 0)
1152 break;
1154 if (*cpp != NULL) {
1155 pp->p_fstype = cpp - fstypenames;
1156 } else {
1157 if (isdigit(*cp))
1158 v = strtoul(cp, NULL, 10);
1159 else
1160 v = FSMAXTYPES;
1161 if (v >= FSMAXTYPES) {
1162 fprintf(stderr,
1163 "line %d: Warning, unknown filesystem type %s\n",
1164 lineno, cp);
1165 v = FS_UNUSED;
1167 pp->p_fstype = v;
1170 pp->p_fsize = 0;
1171 pp->p_frag = 0;
1172 pp->p_cpg = 0;
1174 cp = tp;
1175 if (tp) {
1176 fprintf(stderr, "line %d: Warning, fragment, block, "
1177 "and bps/cpg fields are no\n"
1178 "longer supported and must be specified "
1179 "via newfs options instead.\n",
1180 lineno);
1182 return(0);
1186 * Check disklabel for errors and fill in
1187 * derived fields according to supplied values.
1190 checklabel(struct disklabel32 *lp)
1192 struct partition32 *pp;
1193 int i, errors = 0;
1194 char part;
1195 u_long base_offset, needed, total_size, total_percent, current_offset;
1196 long free_space;
1197 int seen_default_offset;
1198 int hog_part;
1199 int j;
1200 struct partition32 *pp2;
1202 if (lp->d_secsize == 0) {
1203 fprintf(stderr, "sector size 0\n");
1204 return (1);
1206 if (lp->d_nsectors == 0) {
1207 fprintf(stderr, "sectors/track 0\n");
1208 return (1);
1210 if (lp->d_ntracks == 0) {
1211 fprintf(stderr, "tracks/cylinder 0\n");
1212 return (1);
1214 if (lp->d_ncylinders == 0) {
1215 fprintf(stderr, "cylinders/unit 0\n");
1216 errors++;
1218 if (lp->d_rpm == 0)
1219 Warning("revolutions/minute 0");
1220 if (lp->d_secpercyl == 0)
1221 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
1222 if (lp->d_secperunit == 0)
1223 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
1224 if (lp->d_bbsize == 0) {
1225 fprintf(stderr, "boot block size 0\n");
1226 errors++;
1227 } else if (lp->d_bbsize % lp->d_secsize)
1228 Warning("boot block size %% sector-size != 0");
1229 if (lp->d_sbsize == 0) {
1230 fprintf(stderr, "super block size 0\n");
1231 errors++;
1232 } else if (lp->d_sbsize % lp->d_secsize)
1233 Warning("super block size %% sector-size != 0");
1234 if (lp->d_npartitions > MAXPARTITIONS32)
1235 Warning("number of partitions (%lu) > MAXPARTITIONS (%d)",
1236 (u_long)lp->d_npartitions, MAXPARTITIONS32);
1238 /* first allocate space to the partitions, then offsets */
1239 total_size = 0; /* in sectors */
1240 total_percent = 0; /* in percent */
1241 hog_part = -1;
1242 /* find all fixed partitions */
1243 for (i = 0; i < lp->d_npartitions; i++) {
1244 pp = &lp->d_partitions[i];
1245 if (part_set[i]) {
1247 if (part_size_type[i] == '*') {
1248 if (i == RAW_PART) {
1249 pp->p_size = lp->d_secperunit;
1250 } else {
1251 if (part_offset_type[i] != '*') {
1252 if (total_size < pp->p_offset)
1253 total_size = pp->p_offset;
1255 if (hog_part != -1)
1256 Warning("Too many '*' partitions (%c and %c)",
1257 hog_part + 'a',i + 'a');
1258 else
1259 hog_part = i;
1261 } else {
1262 off_t size;
1264 size = pp->p_size;
1265 switch (part_size_type[i]) {
1266 case '%':
1267 total_percent += size;
1268 break;
1269 case 't':
1270 case 'T':
1271 size *= 1024ULL;
1272 /* FALLTHROUGH */
1273 case 'g':
1274 case 'G':
1275 size *= 1024ULL;
1276 /* FALLTHROUGH */
1277 case 'm':
1278 case 'M':
1279 size *= 1024ULL;
1280 /* FALLTHROUGH */
1281 case 'k':
1282 case 'K':
1283 size *= 1024ULL;
1284 break;
1285 case '\0':
1286 break;
1287 default:
1288 Warning("unknown size specifier '%c' (K/M/G/T are valid)",part_size_type[i]);
1289 break;
1291 /* don't count %'s yet */
1292 if (part_size_type[i] != '%') {
1294 * for all not in sectors, convert to
1295 * sectors
1297 if (part_size_type[i] != '\0') {
1298 if (size % lp->d_secsize != 0)
1299 Warning("partition %c not an integer number of sectors",
1300 i + 'a');
1301 size /= lp->d_secsize;
1302 pp->p_size = size;
1304 /* else already in sectors */
1305 if (i != RAW_PART)
1306 total_size += size;
1312 /* Find out the total free space, excluding the boot block area. */
1313 base_offset = BBSIZE / lp->d_secsize;
1314 free_space = 0;
1315 for (i = 0; i < lp->d_npartitions; i++) {
1316 pp = &lp->d_partitions[i];
1317 if (!part_set[i] || i == RAW_PART ||
1318 part_size_type[i] == '%' || part_size_type[i] == '*')
1319 continue;
1320 if (pp->p_offset > base_offset)
1321 free_space += pp->p_offset - base_offset;
1322 if (pp->p_offset + pp->p_size > base_offset)
1323 base_offset = pp->p_offset + pp->p_size;
1325 if (base_offset < lp->d_secperunit)
1326 free_space += lp->d_secperunit - base_offset;
1328 /* handle % partitions - note %'s don't need to add up to 100! */
1329 if (total_percent != 0) {
1330 if (total_percent > 100) {
1331 fprintf(stderr,"total percentage %lu is greater than 100\n",
1332 total_percent);
1333 errors++;
1336 if (free_space > 0) {
1337 for (i = 0; i < lp->d_npartitions; i++) {
1338 pp = &lp->d_partitions[i];
1339 if (part_set[i] && part_size_type[i] == '%') {
1340 /* careful of overflows! and integer roundoff */
1341 pp->p_size = ((double)pp->p_size/100) * free_space;
1342 total_size += pp->p_size;
1344 /* FIX we can lose a sector or so due to roundoff per
1345 partition. A more complex algorithm could avoid that */
1348 } else {
1349 fprintf(stderr,
1350 "%ld sectors available to give to '*' and '%%' partitions\n",
1351 free_space);
1352 errors++;
1353 /* fix? set all % partitions to size 0? */
1356 /* give anything remaining to the hog partition */
1357 if (hog_part != -1) {
1359 * Find the range of offsets usable by '*' partitions around
1360 * the hog partition and how much space they need.
1362 needed = 0;
1363 base_offset = BBSIZE / lp->d_secsize;
1364 for (i = hog_part - 1; i >= 0; i--) {
1365 pp = &lp->d_partitions[i];
1366 if (!part_set[i] || i == RAW_PART)
1367 continue;
1368 if (part_offset_type[i] == '*') {
1369 needed += pp->p_size;
1370 continue;
1372 base_offset = pp->p_offset + pp->p_size;
1373 break;
1375 current_offset = lp->d_secperunit;
1376 for (i = lp->d_npartitions - 1; i > hog_part; i--) {
1377 pp = &lp->d_partitions[i];
1378 if (!part_set[i] || i == RAW_PART)
1379 continue;
1380 if (part_offset_type[i] == '*') {
1381 needed += pp->p_size;
1382 continue;
1384 current_offset = pp->p_offset;
1387 if (current_offset - base_offset <= needed) {
1388 fprintf(stderr, "Cannot find space for partition %c\n",
1389 hog_part + 'a');
1390 fprintf(stderr,
1391 "Need more than %lu sectors between %lu and %lu\n",
1392 needed, base_offset, current_offset);
1393 errors++;
1394 lp->d_partitions[hog_part].p_size = 0;
1395 } else {
1396 lp->d_partitions[hog_part].p_size = current_offset -
1397 base_offset - needed;
1398 total_size += lp->d_partitions[hog_part].p_size;
1402 /* Now set the offsets for each partition */
1403 current_offset = BBSIZE / lp->d_secsize; /* in sectors */
1404 seen_default_offset = 0;
1405 for (i = 0; i < lp->d_npartitions; i++) {
1406 part = 'a' + i;
1407 pp = &lp->d_partitions[i];
1408 if (part_set[i]) {
1409 if (part_offset_type[i] == '*') {
1410 if (i == RAW_PART) {
1411 pp->p_offset = 0;
1412 } else {
1413 pp->p_offset = current_offset;
1414 seen_default_offset = 1;
1416 } else {
1417 /* allow them to be out of order for old-style tables */
1418 if (pp->p_offset < current_offset &&
1419 seen_default_offset && i != RAW_PART &&
1420 pp->p_fstype != FS_VINUM) {
1421 fprintf(stderr,
1422 "Offset %ld for partition %c overlaps previous partition which ends at %lu\n",
1423 (long)pp->p_offset,i+'a',current_offset);
1424 fprintf(stderr,
1425 "Labels with any *'s for offset must be in ascending order by sector\n");
1426 errors++;
1427 } else if (pp->p_offset != current_offset &&
1428 i != RAW_PART && seen_default_offset) {
1430 * this may give unneeded warnings if
1431 * partitions are out-of-order
1433 Warning(
1434 "Offset %ld for partition %c doesn't match expected value %ld",
1435 (long)pp->p_offset, i + 'a', current_offset);
1438 if (i != RAW_PART)
1439 current_offset = pp->p_offset + pp->p_size;
1443 for (i = 0; i < lp->d_npartitions; i++) {
1444 part = 'a' + i;
1445 pp = &lp->d_partitions[i];
1446 if (pp->p_size == 0 && pp->p_offset != 0)
1447 Warning("partition %c: size 0, but offset %lu",
1448 part, (u_long)pp->p_offset);
1449 #ifdef notdef
1450 if (pp->p_size % lp->d_secpercyl)
1451 Warning("partition %c: size %% cylinder-size != 0",
1452 part);
1453 if (pp->p_offset % lp->d_secpercyl)
1454 Warning("partition %c: offset %% cylinder-size != 0",
1455 part);
1456 #endif
1457 if (pp->p_offset > lp->d_secperunit) {
1458 fprintf(stderr,
1459 "partition %c: offset past end of unit\n", part);
1460 errors++;
1462 if (pp->p_offset + pp->p_size > lp->d_secperunit) {
1463 fprintf(stderr,
1464 "partition %c: partition extends past end of unit\n",
1465 part);
1466 errors++;
1468 if (i == RAW_PART)
1470 if (pp->p_fstype != FS_UNUSED)
1471 Warning("partition %c is not marked as unused!",part);
1472 if (pp->p_offset != 0)
1473 Warning("partition %c doesn't start at 0!",part);
1474 if (pp->p_size != lp->d_secperunit)
1475 Warning("partition %c doesn't cover the whole unit!",part);
1477 if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) ||
1478 (pp->p_size != lp->d_secperunit)) {
1479 Warning("An incorrect partition %c may cause problems for "
1480 "standard system utilities",part);
1484 /* check for overlaps */
1485 /* this will check for all possible overlaps once and only once */
1486 for (j = 0; j < i; j++) {
1487 pp2 = &lp->d_partitions[j];
1488 if (j != RAW_PART && i != RAW_PART &&
1489 pp->p_fstype != FS_VINUM &&
1490 pp2->p_fstype != FS_VINUM &&
1491 part_set[i] && part_set[j]) {
1492 if (pp2->p_offset < pp->p_offset + pp->p_size &&
1493 (pp2->p_offset + pp2->p_size > pp->p_offset ||
1494 pp2->p_offset >= pp->p_offset)) {
1495 fprintf(stderr,"partitions %c and %c overlap!\n",
1496 j + 'a', i + 'a');
1497 errors++;
1502 for (; i < 8 || i < lp->d_npartitions; i++) {
1503 part = 'a' + i;
1504 pp = &lp->d_partitions[i];
1505 if (pp->p_size || pp->p_offset)
1506 Warning("unused partition %c: size %d offset %lu",
1507 'a' + i, pp->p_size, (u_long)pp->p_offset);
1509 return (errors);
1513 * When operating on a "virgin" disk, try getting an initial label
1514 * from the associated device driver. This might work for all device
1515 * drivers that are able to fetch some initial device parameters
1516 * without even having access to a (BSD) disklabel, like SCSI disks,
1517 * most IDE drives, or vn devices.
1519 static struct disklabel32 dlab;
1521 struct disklabel32 *
1522 getvirginlabel(void)
1524 struct partinfo info;
1525 struct disklabel32 *dl = &dlab;
1526 int f;
1528 if ((f = open(dkname, O_RDONLY)) == -1) {
1529 warn("cannot open %s", dkname);
1530 return (NULL);
1534 * Check to see if the media is too big for a 32 bit disklabel.
1536 if (ioctl(f, DIOCGPART, &info) == 0) {
1537 if (info.media_size >= 0x100000000ULL * 512) {
1538 warnx("The media is too large for a 32 bit disklabel,"
1539 " please use disklabel64.");
1540 return (NULL);
1545 * Generate a virgin disklabel via ioctl
1547 if (ioctl(f, DIOCGDVIRGIN32, dl) < 0) {
1548 l_perror("ioctl DIOCGDVIRGIN32");
1549 close(f);
1550 return(NULL);
1552 close(f);
1553 return (dl);
1556 struct disklabel32 *
1557 getdisklabelfromdisktab(const char *name)
1559 struct disktab *dt;
1560 struct disklabel32 *dl = &dlab;
1561 int i;
1563 if ((dt = getdisktabbyname(name)) == NULL)
1564 return(NULL);
1565 dl->d_magic = DISKMAGIC32;
1566 dl->d_type = dt->d_typeid;
1567 dl->d_subtype = 0;
1568 dl->d_secsize = dt->d_media_blksize;
1569 dl->d_nsectors = dt->d_secpertrack;
1570 dl->d_ntracks = dt->d_nheads;
1571 dl->d_ncylinders = dt->d_ncylinders;
1572 dl->d_secpercyl = dt->d_secpercyl;
1573 dl->d_secperunit = dt->d_media_blocks;
1574 dl->d_rpm = dt->d_rpm;
1575 dl->d_interleave = dt->d_interleave;
1576 dl->d_trackskew = dt->d_trackskew;
1577 dl->d_cylskew = dt->d_cylskew;
1578 dl->d_headswitch = dt->d_headswitch;
1579 dl->d_trkseek = dt->d_trkseek;
1580 dl->d_magic2 = DISKMAGIC32;
1581 dl->d_npartitions = dt->d_npartitions;
1582 dl->d_bbsize = dt->d_bbsize;
1583 dl->d_sbsize = dt->d_sbsize;
1584 for (i = 0; i < dt->d_npartitions; ++i) {
1585 struct partition32 *dlp = &dl->d_partitions[i];
1586 struct dt_partition *dtp = &dt->d_partitions[i];
1588 dlp->p_size = dtp->p_size;
1589 dlp->p_offset = dtp->p_offset;
1590 dlp->p_fsize = dtp->p_fsize;
1591 dlp->p_fstype = dtp->p_fstype;
1592 dlp->p_frag = dtp->p_fsize;
1594 return(dl);
1598 * If we are installing a boot program that doesn't fit in d_bbsize
1599 * we need to mark those partitions that the boot overflows into.
1600 * This allows newfs to prevent creation of a filesystem where it might
1601 * clobber bootstrap code.
1603 void
1604 setbootflag(struct disklabel32 *lp)
1606 struct partition32 *pp;
1607 int i, errors = 0;
1608 char part;
1609 u_long boffset;
1611 if (bootbuf == NULL)
1612 return;
1613 boffset = bootsize / lp->d_secsize;
1614 for (i = 0; i < lp->d_npartitions; i++) {
1615 part = 'a' + i;
1616 pp = &lp->d_partitions[i];
1617 if (pp->p_size == 0)
1618 continue;
1619 if (boffset <= pp->p_offset) {
1620 if (pp->p_fstype == FS_BOOT)
1621 pp->p_fstype = FS_UNUSED;
1622 } else if (pp->p_fstype != FS_BOOT) {
1623 if (pp->p_fstype != FS_UNUSED) {
1624 fprintf(stderr,
1625 "boot overlaps used partition %c\n",
1626 part);
1627 errors++;
1628 } else {
1629 pp->p_fstype = FS_BOOT;
1630 Warning("boot overlaps partition %c, %s",
1631 part, "marked as FS_BOOT");
1635 if (errors)
1636 errx(4, "cannot install boot program");
1639 /*VARARGS1*/
1640 void
1641 Warning(const char *fmt, ...)
1643 va_list ap;
1645 fprintf(stderr, "Warning, ");
1646 va_start(ap, fmt);
1647 vfprintf(stderr, fmt, ap);
1648 fprintf(stderr, "\n");
1649 va_end(ap);
1653 * Check to see if the bootblocks are in the wrong place. FBsd5 bootblocks
1654 * and earlier DFly bb's are packed against the old disklabel and a new
1655 * disklabel would blow them up. This is a hack that should be removed
1656 * in 2006 sometime (if ever).
1660 checkoldboot(int f, const char *bootbuffer)
1662 char buf[BBSIZE];
1664 if (bootbuffer && strncmp(bootbuffer + 0x402, "BTX", 3) == 0)
1665 return(0);
1666 lseek(f, (off_t)0, SEEK_SET);
1667 if (read(f, buf, sizeof(buf)) != sizeof(buf))
1668 return(0);
1669 if (strncmp(buf + 0x402, "BTX", 3) == 0) /* new location */
1670 return(0);
1671 if (strncmp(buf + 0x316, "BTX", 3) == 0) /* old location */
1672 return(1);
1673 return(0);
1677 * Traditional 32 bit disklabels actually use absolute sector numbers on
1678 * disk, NOT slice relative sector numbres. The OS hides this from us
1679 * when we use DIOC ioctls to access the label, but newer versions of
1680 * Dragonfly no longer adjusts the disklabel when snooping reads or writes
1681 * so we have to figure it out ourselves.
1683 const char *
1684 fixlabel(int f, struct disklabel32 *lp, int writeadj)
1686 const char *msg = NULL;
1687 struct partinfo info;
1688 struct partition32 *pp;
1689 u_int64_t start;
1690 u_int64_t end;
1691 u_int64_t offset;
1692 int part;
1693 int rev;
1694 size_t rev_len = sizeof(rev);
1696 if (sysctlbyname("kern.osrevision", &rev, &rev_len, NULL, 0) < 0) {
1697 errx(1, "Cannot use raw mode on non-DragonFly systems\n");
1699 if (rev < 200701) {
1700 warnx("Warning running new disklabel on old DragonFly systems,\n"
1701 "assuming the disk layer will fixup the label.\n");
1702 sleep(3);
1703 return(NULL);
1706 pp = &lp->d_partitions[RAW_PART];
1708 if (forceflag) {
1709 info.media_offset = slice_start_lba * lp->d_secsize;
1710 info.media_blocks = pp->p_size;
1711 info.media_blksize = lp->d_secsize;
1712 } else if (ioctl(f, DIOCGPART, &info) < 0) {
1713 msg = "Unable to extract the slice starting LBA, "
1714 "you must use the -f <slice_start_lba> option\n"
1715 "to specify it manually, or perhaps try without "
1716 "using -r and let the kernel deal with it\n";
1717 return(msg);
1720 if (lp->d_magic != DISKMAGIC32 || lp->d_magic2 != DISKMAGIC32)
1721 return ("fixlabel: invalid magic");
1722 if (dkcksum32(lp) != 0)
1723 return ("fixlabel: invalid checksum");
1726 * What a mess. For ages old backwards compatibility the disklabel
1727 * on-disk stores absolute offsets instead of slice-relative offsets.
1728 * So fix it up when reading, writing, or snooping.
1730 * The in-core label is always slice-relative.
1732 if (writeadj) {
1734 * incore -> disk
1736 start = 0;
1737 offset = info.media_offset / info.media_blksize;
1738 } else {
1740 * disk -> incore
1742 start = info.media_offset / info.media_blksize;
1743 offset = -info.media_offset / info.media_blksize;
1745 if (pp->p_offset != start)
1746 return ("fixlabel: raw partition offset != slice offset");
1747 if (pp->p_size != info.media_blocks) {
1748 if (pp->p_size > info.media_blocks)
1749 return ("fixlabel: raw partition size > slice size");
1751 end = start + info.media_blocks;
1752 if (start > end)
1753 return ("fixlabel: slice wraps");
1754 if (lp->d_secpercyl <= 0)
1755 return ("fixlabel: d_secpercyl <= 0");
1756 pp -= RAW_PART;
1757 for (part = 0; part < lp->d_npartitions; part++, pp++) {
1758 if (pp->p_offset != 0 || pp->p_size != 0) {
1759 if (pp->p_offset < start
1760 || pp->p_offset + pp->p_size > end
1761 || pp->p_offset + pp->p_size < pp->p_offset) {
1762 /* XXX else silently discard junk. */
1763 bzero(pp, sizeof *pp);
1764 } else {
1765 pp->p_offset += offset;
1769 lp->d_checksum = 0;
1770 lp->d_checksum = dkcksum32(lp);
1771 return (NULL);
1774 void
1775 usage(void)
1777 #if NUMBOOT > 0
1778 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",
1779 "usage: disklabel32 [-r] disk",
1780 "\t\t(to read label)",
1781 " disklabel32 -w [-r] [-n] disk type [packid]",
1782 "\t\t(to write label with existing boot program)",
1783 " disklabel32 -e [-r] [-n] disk",
1784 "\t\t(to edit label)",
1785 " disklabel32 -R [-r] [-n] disk protofile",
1786 "\t\t(to restore label with existing boot program)",
1787 #if NUMBOOT > 1
1788 " disklabel32 -B [-n] [-b boot1 -s boot2] disk [type]",
1789 "\t\t(to install boot program with existing label)",
1790 " disklabel32 -w -B [-n] [-b boot1 -s boot2] disk type [packid]",
1791 "\t\t(to write label and boot program)",
1792 " disklabel32 -R -B [-n] [-b boot1 -s boot2] disk protofile [type]",
1793 "\t\t(to restore label and boot program)",
1794 #else
1795 " disklabel32 -B [-n] [-b bootprog] disk [type]",
1796 "\t\t(to install boot program with existing on-disk label)",
1797 " disklabel32 -w -B [-n] [-b bootprog] disk type [packid]",
1798 "\t\t(to write label and install boot program)",
1799 " disklabel32 -R -B [-n] [-b bootprog] disk protofile [type]",
1800 "\t\t(to restore label and install boot program)",
1801 #endif
1802 " disklabel32 [-NW] disk",
1803 "\t\t(to write disable/enable label)");
1804 #else
1805 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
1806 "usage: disklabel32 [-r] disk", "(to read label)",
1807 " disklabel32 -w [-r] [-n] disk type [packid]",
1808 "\t\t(to write label)",
1809 " disklabel32 -e [-r] [-n] disk",
1810 "\t\t(to edit label)",
1811 " disklabel32 -R [-r] [-n] disk protofile",
1812 "\t\t(to restore label)",
1813 " disklabel32 [-NW] disk",
1814 "\t\t(to write disable/enable label)");
1815 #endif
1816 fprintf(stderr, "%s\n%s\n",
1817 " disklabel32 [-f slice_start_lba] [options]",
1818 "\t\t(to force using manual offset)");
1819 exit(1);