Continue untangling the disklabel.
[dragonfly.git] / sbin / disklabel / disklabel.c
1 /*
2  * Copyright (c) 1987, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Symmetric Computer Systems.
7  *
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.
23  *
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.
35  *
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.18 2007/05/20 23:21:34 dillon Exp $
41  */
42
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/diskslice.h>
50 #include <sys/diskmbr.h>
51 #include <disktab.h>
52
53 #include <vfs/ufs/dinode.h>
54 #include <vfs/ufs/fs.h>
55
56 #include <unistd.h>
57 #include <string.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <signal.h>
61 #include <stdarg.h>
62 #include <ctype.h>
63 #include <err.h>
64 #include <errno.h>
65 #include <disktab.h>
66 #include "pathnames.h"
67
68 /*
69  * Disklabel: read and write disklabels.
70  * The label is usually placed on one of the first sectors of the disk.
71  * Many machines also place a bootstrap in the same area,
72  * in which case the label is embedded in the bootstrap.
73  * The bootstrap source must leave space at the proper offset
74  * for the label on such machines.
75  */
76
77 #ifndef BBSIZE
78 #define BBSIZE  8192                    /* size of boot area, with label */
79 #endif
80
81 /* FIX!  These are too low, but are traditional */
82 #define DEFAULT_NEWFS_BLOCK  8192U
83 #define DEFAULT_NEWFS_FRAG   1024U
84 #define DEFAULT_NEWFS_CPG    16U
85
86 #define BIG_NEWFS_BLOCK  16384U
87 #define BIG_NEWFS_FRAG   2048U
88 #define BIG_NEWFS_CPG    64U
89
90 #define NUMBOOT 2
91
92 void    makelabel(const char *, const char *, struct disklabel *);
93 int     writelabel(int, const char *, struct disklabel *);
94 void    l_perror(const char *);
95 struct disklabel *readlabel(int);
96 struct disklabel *makebootarea(char *, struct disklabel *, int);
97 void    display(FILE *, const struct disklabel *);
98 int     edit(struct disklabel *, int);
99 int     editit(void);
100 char    *skip(char *);
101 char    *word(char *);
102 int     getasciilabel(FILE *, struct disklabel *);
103 int     getasciipartspec(char *, struct disklabel *, int, int);
104 int     checklabel(struct disklabel *);
105 void    setbootflag(struct disklabel *);
106 void    Warning(const char *, ...) __printflike(1, 2);
107 void    usage(void);
108 int     checkoldboot(int, const char *);
109 struct disklabel *getvirginlabel(void);
110 struct disklabel *getdisklabelfromdisktab(const char *name);
111
112 #define DEFEDITOR       _PATH_VI
113 #define streq(a,b)      (strcmp(a,b) == 0)
114
115 char    *dkname;
116 char    *specname;
117 char    tmpfil[] = PATH_TMPFILE;
118
119 char    namebuf[BBSIZE], *np = namebuf;
120 struct  disklabel lab;
121 char    bootarea[BBSIZE];
122
123 #define MAX_PART ('z')
124 #define MAX_NUM_PARTS (1 + MAX_PART - 'a')
125 char    part_size_type[MAX_NUM_PARTS];
126 char    part_offset_type[MAX_NUM_PARTS];
127 int     part_set[MAX_NUM_PARTS];
128
129 #if NUMBOOT > 0
130 int     installboot;    /* non-zero if we should install a boot program */
131 char    *bootbuf;       /* pointer to buffer with remainder of boot prog */
132 int     bootsize;       /* size of remaining boot program */
133 char    *xxboot;        /* primary boot */
134 char    *bootxx;        /* secondary boot */
135 char    boot0[MAXPATHLEN];
136 char    boot1[MAXPATHLEN];
137 #endif
138
139 enum    {
140         UNSPEC, EDIT, NOWRITE, READ, RESTORE, WRITE, WRITEABLE, WRITEBOOT
141 } op = UNSPEC;
142
143 int     rflag;
144 int     disable_write;   /* set to disable writing to disk label */
145
146 #ifdef DEBUG
147 int     debug;
148 #define OPTIONS "BNRWb:denrs:w"
149 #else
150 #define OPTIONS "BNRWb:enrs:w"
151 #endif
152
153 int
154 main(int argc, char *argv[])
155 {
156         struct disklabel *lp;
157         FILE *t;
158         int ch, f = 0, flag, error = 0;
159         char *name = 0;
160
161         while ((ch = getopt(argc, argv, OPTIONS)) != -1)
162                 switch (ch) {
163 #if NUMBOOT > 0
164                         case 'B':
165                                 ++installboot;
166                                 break;
167                         case 'b':
168                                 xxboot = optarg;
169                                 break;
170 #if NUMBOOT > 1
171                         case 's':
172                                 bootxx = optarg;
173                                 break;
174 #endif
175 #endif
176                         case 'N':
177                                 if (op != UNSPEC)
178                                         usage();
179                                 op = NOWRITE;
180                                 break;
181                         case 'n':
182                                 disable_write = 1;
183                                 break;
184                         case 'R':
185                                 if (op != UNSPEC)
186                                         usage();
187                                 op = RESTORE;
188                                 break;
189                         case 'W':
190                                 if (op != UNSPEC)
191                                         usage();
192                                 op = WRITEABLE;
193                                 break;
194                         case 'e':
195                                 if (op != UNSPEC)
196                                         usage();
197                                 op = EDIT;
198                                 break;
199                         case 'r':
200                                 ++rflag;
201                                 break;
202                         case 'w':
203                                 if (op != UNSPEC)
204                                         usage();
205                                 op = WRITE;
206                                 break;
207 #ifdef DEBUG
208                         case 'd':
209                                 debug++;
210                                 break;
211 #endif
212                         case '?':
213                         default:
214                                 usage();
215                 }
216         argc -= optind;
217         argv += optind;
218 #if NUMBOOT > 0
219         if (installboot) {
220                 rflag++;
221                 if (op == UNSPEC)
222                         op = WRITEBOOT;
223         } else {
224                 if (op == UNSPEC)
225                         op = READ;
226                 xxboot = bootxx = 0;
227         }
228 #else
229         if (op == UNSPEC)
230                 op = READ;
231 #endif
232         if (argc < 1)
233                 usage();
234
235         dkname = argv[0];
236         if (dkname[0] != '/') {
237                 sprintf(np, "%s%s", _PATH_DEV, dkname);
238                 specname = np;
239                 np += strlen(specname) + 1;
240         } else {
241                 specname = dkname;
242         }
243         f = open(specname, op == READ ? O_RDONLY : O_RDWR);
244         if (f < 0 && errno == ENOENT && dkname[0] != '/') {
245                 sprintf(specname, "%s%s", _PATH_DEV, dkname);
246                 np = namebuf + strlen(specname) + 1;
247                 f = open(specname, op == READ ? O_RDONLY : O_RDWR);
248         }
249         if (f < 0)
250                 err(4, "%s", specname);
251
252         switch(op) {
253
254         case UNSPEC:
255                 break;
256
257         case EDIT:
258                 if (argc != 1)
259                         usage();
260                 lp = readlabel(f);
261                 error = edit(lp, f);
262                 break;
263
264         case NOWRITE:
265                 flag = 0;
266                 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
267                         err(4, "ioctl DIOCWLABEL");
268                 break;
269
270         case READ:
271                 if (argc != 1)
272                         usage();
273                 lp = readlabel(f);
274                 display(stdout, lp);
275                 error = checklabel(lp);
276                 if (checkoldboot(f, NULL))
277                         warnx("Warning, old bootblocks detected, install new bootblocks & reinstall the disklabel");
278                 break;
279
280         case RESTORE:
281 #if NUMBOOT > 0
282                 if (installboot && argc == 3) {
283                         makelabel(argv[2], 0, &lab);
284                         argc--;
285
286                         /*
287                          * We only called makelabel() for its side effect
288                          * of setting the bootstrap file names.  Discard
289                          * all changes to `lab' so that all values in the
290                          * final label come from the ASCII label.
291                          */
292                         bzero((char *)&lab, sizeof(lab));
293                 }
294 #endif
295                 if (argc != 2)
296                         usage();
297                 if (!(t = fopen(argv[1], "r")))
298                         err(4, "%s", argv[1]);
299                 if (!getasciilabel(t, &lab))
300                         exit(1);
301                 lp = makebootarea(bootarea, &lab, f);
302                 *lp = lab;
303                 error = writelabel(f, bootarea, lp);
304                 break;
305
306         case WRITE:
307                 if (argc == 3) {
308                         name = argv[2];
309                         argc--;
310                 }
311                 if (argc != 2)
312                         usage();
313                 makelabel(argv[1], name, &lab);
314                 lp = makebootarea(bootarea, &lab, f);
315                 *lp = lab;
316                 if (checklabel(lp) == 0)
317                         error = writelabel(f, bootarea, lp);
318                 break;
319
320         case WRITEABLE:
321                 flag = 1;
322                 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
323                         err(4, "ioctl DIOCWLABEL");
324                 break;
325
326 #if NUMBOOT > 0
327         case WRITEBOOT:
328         {
329                 struct disklabel tlab;
330
331                 lp = readlabel(f);
332                 tlab = *lp;
333                 if (argc == 2)
334                         makelabel(argv[1], 0, &lab);
335                 lp = makebootarea(bootarea, &lab, f);
336                 *lp = tlab;
337                 if (checklabel(lp) == 0)
338                         error = writelabel(f, bootarea, lp);
339                 break;
340         }
341 #endif
342         }
343         exit(error);
344 }
345
346 /*
347  * Construct a prototype disklabel from /etc/disktab.  As a side
348  * effect, set the names of the primary and secondary boot files
349  * if specified.
350  */
351 void
352 makelabel(const char *type, const char *name, struct disklabel *lp)
353 {
354         struct disklabel *dp;
355
356         if (strcmp(type, "auto") == 0)
357                 dp = getvirginlabel();
358         else
359                 dp = getdisklabelfromdisktab(type);
360         if (dp == NULL)
361                 errx(1, "%s: unknown disk type", type);
362         *lp = *dp;
363
364         /*
365          * NOTE: boot control files may no longer be specified in disktab.
366          */
367         if (name)
368                 strncpy(lp->d_packname, name, sizeof(lp->d_packname));
369 }
370
371 int
372 writelabel(int f, const char *boot, struct disklabel *lp)
373 {
374         int flag;
375
376         if (disable_write) {
377                 Warning("write to disk label supressed - label was as follows:");
378                 display(stdout, lp);
379                 return (0);
380         } else {
381                 /* make sure we are not overwriting our boot code */
382                 if (checkoldboot(f, boot))
383                         errx(4, "Will not overwrite old bootblocks w/ label, install new boot blocks first!");
384                 setbootflag(lp);
385                 lp->d_magic = DISKMAGIC;
386                 lp->d_magic2 = DISKMAGIC;
387                 lp->d_checksum = 0;
388                 lp->d_checksum = dkcksum(lp);
389                 if (rflag) {
390                         /*
391                          * First set the kernel disk label,
392                          * then write a label to the raw disk.
393                          * If the SDINFO ioctl fails because it is unimplemented,
394                          * keep going; otherwise, the kernel consistency checks
395                          * may prevent us from changing the current (in-core)
396                          * label.
397                          */
398                         if (ioctl(f, DIOCSDINFO, lp) < 0 &&
399                                 errno != ENODEV && errno != ENOTTY) {
400                                 l_perror("ioctl DIOCSDINFO");
401                                 return (1);
402                         }
403                         lseek(f, (off_t)0, SEEK_SET);
404                         
405                         /*
406                          * write enable label sector before write (if necessary),
407                          * disable after writing.
408                          */
409                         flag = 1;
410                         if (ioctl(f, DIOCWLABEL, &flag) < 0)
411                                 warn("ioctl DIOCWLABEL");
412                         if (write(f, boot, lp->d_bbsize) != (ssize_t)lp->d_bbsize) {
413                                 warn("write");
414                                 return (1);
415                         }
416 #if NUMBOOT > 0
417                         /*
418                          * Output the remainder of the disklabel
419                          */
420                         if (bootbuf && write(f, bootbuf, bootsize) != bootsize) {
421                                 warn("write");
422                                 return(1);
423                         }
424 #endif
425                         flag = 0;
426                         ioctl(f, DIOCWLABEL, &flag);
427                 } else if (ioctl(f, DIOCWDINFO, lp) < 0) {
428                         l_perror("ioctl DIOCWDINFO");
429                         return (1);
430                 }
431         }
432         return (0);
433 }
434
435 void
436 l_perror(const char *s)
437 {
438         switch (errno) {
439
440         case ESRCH:
441                 warnx("%s: no disk label on disk;", s);
442                 fprintf(stderr, "add \"-r\" to install initial label\n");
443                 break;
444
445         case EINVAL:
446                 warnx("%s: label magic number or checksum is wrong!", s);
447                 fprintf(stderr, "(disklabel or kernel is out of date?)\n");
448                 break;
449
450         case EBUSY:
451                 warnx("%s: open partition would move or shrink", s);
452                 break;
453
454         case EXDEV:
455                 warnx("%s: '%c' partition must start at beginning of disk",
456                     s, 'a' + RAW_PART);
457                 break;
458
459         default:
460                 warn((char *)NULL);
461                 break;
462         }
463 }
464
465 /*
466  * Fetch disklabel for disk.
467  * Use ioctl to get label unless -r flag is given.
468  */
469 struct disklabel *
470 readlabel(int f)
471 {
472         struct disklabel *lp;
473
474         if (rflag) {
475                 if (read(f, bootarea, BBSIZE) < BBSIZE)
476                         err(4, "%s", specname);
477                 for (lp = (struct disklabel *)bootarea;
478                     lp <= (struct disklabel *)(bootarea + BBSIZE - sizeof(*lp));
479                     lp = (struct disklabel *)((char *)lp + 16))
480                         if (lp->d_magic == DISKMAGIC &&
481                             lp->d_magic2 == DISKMAGIC)
482                                 break;
483                 if (lp > (struct disklabel *)(bootarea+BBSIZE-sizeof(*lp)) ||
484                     lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC ||
485                     dkcksum(lp) != 0)
486                         errx(1,
487             "bad pack magic number (label is damaged, or pack is unlabeled)");
488         } else {
489                 lp = &lab;
490                 if (ioctl(f, DIOCGDINFO, lp) < 0)
491                         err(4, "ioctl DIOCGDINFO");
492         }
493         return (lp);
494 }
495
496 /*
497  * Construct a bootarea (d_bbsize bytes) in the specified buffer ``boot''
498  * Returns a pointer to the disklabel portion of the bootarea.
499  */
500 struct disklabel *
501 makebootarea(char *boot, struct disklabel *dp, int f)
502 {
503         struct disklabel *lp;
504         char *p;
505         int b;
506 #if NUMBOOT > 0
507         char *dkbasename;
508         struct stat sb;
509 #endif
510 #ifdef __i386__
511         char *tmpbuf;
512         unsigned int i, found;
513 #endif
514
515         /* XXX */
516         if (dp->d_secsize == 0) {
517                 dp->d_secsize = DEV_BSIZE;
518                 dp->d_bbsize = BBSIZE;
519         }
520         lp = (struct disklabel *)
521                 (boot + (LABELSECTOR * dp->d_secsize) + LABELOFFSET);
522         bzero((char *)lp, sizeof *lp);
523 #if NUMBOOT > 0
524         /*
525          * If we are not installing a boot program but we are installing a
526          * label on disk then we must read the current bootarea so we don't
527          * clobber the existing boot.
528          */
529         if (!installboot) {
530                 if (rflag) {
531                         if (read(f, boot, BBSIZE) < BBSIZE)
532                                 err(4, "%s", specname);
533                         bzero((char *)lp, sizeof *lp);
534                 }
535                 return (lp);
536         }
537         /*
538          * We are installing a boot program.  Determine the name(s) and
539          * read them into the appropriate places in the boot area.
540          */
541         if (!xxboot || !bootxx) {
542                 dkbasename = np;
543                 if ((p = strrchr(dkname, '/')) == NULL)
544                         p = dkname;
545                 else
546                         p++;
547                 while (*p && !isdigit(*p))
548                         *np++ = *p++;
549                 *np++ = '\0';
550
551                 if (!xxboot) {
552                         sprintf(boot0, "%s/boot1", _PATH_BOOTDIR);
553                         xxboot = boot0;
554                 }
555 #if NUMBOOT > 1
556                 if (!bootxx) {
557                         sprintf(boot1, "%s/boot2", _PATH_BOOTDIR);
558                         bootxx = boot1;
559                 }
560 #endif
561         }
562 #ifdef DEBUG
563         if (debug)
564                 fprintf(stderr, "bootstraps: xxboot = %s, bootxx = %s\n",
565                         xxboot, bootxx ? bootxx : "NONE");
566 #endif
567
568         /*
569          * Strange rules:
570          * 1. One-piece bootstrap (hp300/hp800)
571          *      up to d_bbsize bytes of ``xxboot'' go in bootarea, the rest
572          *      is remembered and written later following the bootarea.
573          * 2. Two-piece bootstraps (vax/i386?/mips?)
574          *      up to d_secsize bytes of ``xxboot'' go in first d_secsize
575          *      bytes of bootarea, remaining d_bbsize-d_secsize filled
576          *      from ``bootxx''.
577          */
578         b = open(xxboot, O_RDONLY);
579         if (b < 0)
580                 err(4, "%s", xxboot);
581 #if NUMBOOT > 1
582 #ifdef __i386__
583         /*
584          * XXX Botch alert.
585          * The i386 has the so-called fdisk table embedded into the
586          * primary bootstrap.  We take care to not clobber it, but
587          * only if it does already contain some data.  (Otherwise,
588          * the xxboot provides a template.)
589          */
590         if ((tmpbuf = (char *)malloc((int)dp->d_secsize)) == 0)
591                 err(4, "%s", xxboot);
592         memcpy((void *)tmpbuf, (void *)boot, (int)dp->d_secsize);
593 #endif /* i386 */
594         if (read(b, boot, (int)dp->d_secsize) < 0)
595                 err(4, "%s", xxboot);
596         close(b);
597 #ifdef __i386__
598         for (i = DOSPARTOFF, found = 0;
599              !found && i < DOSPARTOFF + NDOSPART*sizeof(struct dos_partition);
600              i++)
601                 found = tmpbuf[i] != 0;
602         if (found)
603                 memcpy((void *)&boot[DOSPARTOFF],
604                        (void *)&tmpbuf[DOSPARTOFF],
605                        NDOSPART * sizeof(struct dos_partition));
606         free(tmpbuf);
607 #endif /* i386 */
608         b = open(bootxx, O_RDONLY);
609         if (b < 0)
610                 err(4, "%s", bootxx);
611         if (fstat(b, &sb) != 0)
612                 err(4, "%s", bootxx);
613         if (dp->d_secsize + sb.st_size > dp->d_bbsize)
614                 errx(4, "%s too large", bootxx);
615         if (read(b, &boot[dp->d_secsize],
616                  (int)(dp->d_bbsize-dp->d_secsize)) < 0)
617                 err(4, "%s", bootxx);
618 #else /* !(NUMBOOT > 1) */
619         if (read(b, boot, (int)dp->d_bbsize) < 0)
620                 err(4, "%s", xxboot);
621         if (fstat(b, &sb) != 0)
622                 err(4, "%s", xxboot);
623         bootsize = (int)sb.st_size - dp->d_bbsize;
624         if (bootsize > 0) {
625                 /* XXX assume d_secsize is a power of two */
626                 bootsize = (bootsize + dp->d_secsize-1) & ~(dp->d_secsize-1);
627                 bootbuf = (char *)malloc((size_t)bootsize);
628                 if (bootbuf == 0)
629                         err(4, "%s", xxboot);
630                 if (read(b, bootbuf, bootsize) < 0) {
631                         free(bootbuf);
632                         err(4, "%s", xxboot);
633                 }
634         }
635 #endif /* NUMBOOT > 1 */
636         close(b);
637 #endif /* NUMBOOT > 0 */
638         /*
639          * Make sure no part of the bootstrap is written in the area
640          * reserved for the label.
641          */
642         for (p = (char *)lp; p < (char *)lp + sizeof(struct disklabel); p++)
643                 if (*p)
644                         errx(2, "bootstrap doesn't leave room for disk label");
645         return (lp);
646 }
647
648 void
649 display(FILE *f, const struct disklabel *lp)
650 {
651         int i, j;
652         const struct partition *pp;
653
654         fprintf(f, "# %s:\n", specname);
655         if (lp->d_type < DKMAXTYPES)
656                 fprintf(f, "type: %s\n", dktypenames[lp->d_type]);
657         else
658                 fprintf(f, "type: %u\n", lp->d_type);
659         fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename),
660                 lp->d_typename);
661         fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname),
662                 lp->d_packname);
663         fprintf(f, "flags:");
664         fprintf(f, "\n");
665         fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize);
666         fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors);
667         fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks);
668         fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl);
669         fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders);
670         fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit);
671         fprintf(f, "rpm: %u\n", lp->d_rpm);
672         fprintf(f, "interleave: %u\n", lp->d_interleave);
673         fprintf(f, "trackskew: %u\n", lp->d_trackskew);
674         fprintf(f, "cylinderskew: %u\n", lp->d_cylskew);
675         fprintf(f, "headswitch: %lu\t\t# milliseconds\n",
676             (u_long)lp->d_headswitch);
677         fprintf(f, "track-to-track seek: %ld\t# milliseconds\n",
678             (u_long)lp->d_trkseek);
679         fprintf(f, "drivedata: ");
680         for (i = NDDATA - 1; i >= 0; i--) {
681                 if (lp->d_drivedata[i])
682                         break;
683         }
684         if (i < 0)
685                 i = 0;
686         for (j = 0; j <= i; j++)
687                 fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]);
688         fprintf(f, "\n\n%u partitions:\n", lp->d_npartitions);
689         fprintf(f,
690             "#          size     offset    fstype   \n");
691         pp = lp->d_partitions;
692         for (i = 0; i < lp->d_npartitions; i++, pp++) {
693                 if (pp->p_size) {
694                         u_long onemeg = 1024 * 1024 / lp->d_secsize;
695                         fprintf(f, "  %c: ", 'a' + i);
696
697                         fprintf(f, "%10lu ", (u_long)pp->p_size);
698                         fprintf(f, "%10lu  ", (u_long)pp->p_offset);
699                         if (pp->p_fstype < FSMAXTYPES)
700                                 fprintf(f, "%8.8s", fstypenames[pp->p_fstype]);
701                         else
702                                 fprintf(f, "%8d", pp->p_fstype);
703
704                         fprintf(f, "\t# %11.3fM", (double)pp->p_size / onemeg);
705                         fprintf(f, "\n");
706                 }
707         }
708         fflush(f);
709 }
710
711 int
712 edit(struct disklabel *lp, int f)
713 {
714         int c, fd;
715         struct disklabel label;
716         FILE *fp;
717
718         if ((fd = mkstemp(tmpfil)) == -1 ||
719             (fp = fdopen(fd, "w")) == NULL) {
720                 warnx("can't create %s", tmpfil);
721                 return (1);
722         }
723         display(fp, lp);
724         fclose(fp);
725         for (;;) {
726                 if (!editit())
727                         break;
728                 fp = fopen(tmpfil, "r");
729                 if (fp == NULL) {
730                         warnx("can't reopen %s for reading", tmpfil);
731                         break;
732                 }
733                 bzero((char *)&label, sizeof(label));
734                 if (getasciilabel(fp, &label)) {
735                         *lp = label;
736                         if (writelabel(f, bootarea, lp) == 0) {
737                                 fclose(fp);
738                                 unlink(tmpfil);
739                                 return (0);
740                         }
741                 }
742                 fclose(fp);
743                 printf("re-edit the label? [y]: "); fflush(stdout);
744                 c = getchar();
745                 if (c != EOF && c != (int)'\n')
746                         while (getchar() != (int)'\n')
747                                 ;
748                 if  (c == (int)'n')
749                         break;
750         }
751         unlink(tmpfil);
752         return (1);
753 }
754
755 int
756 editit(void)
757 {
758         int pid, xpid;
759         int status, omask;
760         const char *ed;
761
762         omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
763         while ((pid = fork()) < 0) {
764                 if (errno == EPROCLIM) {
765                         warnx("you have too many processes");
766                         return(0);
767                 }
768                 if (errno != EAGAIN) {
769                         warn("fork");
770                         return(0);
771                 }
772                 sleep(1);
773         }
774         if (pid == 0) {
775                 sigsetmask(omask);
776                 setgid(getgid());
777                 setuid(getuid());
778                 if ((ed = getenv("EDITOR")) == (char *)0)
779                         ed = DEFEDITOR;
780                 execlp(ed, ed, tmpfil, (char *)0);
781                 err(1, "%s", ed);
782         }
783         while ((xpid = wait(&status)) >= 0)
784                 if (xpid == pid)
785                         break;
786         sigsetmask(omask);
787         return(!status);
788 }
789
790 char *
791 skip(char *cp)
792 {
793
794         while (*cp != '\0' && isspace(*cp))
795                 cp++;
796         if (*cp == '\0' || *cp == '#')
797                 return (NULL);
798         return (cp);
799 }
800
801 char *
802 word(char *cp)
803 {
804         char c;
805
806         while (*cp != '\0' && !isspace(*cp) && *cp != '#')
807                 cp++;
808         if ((c = *cp) != '\0') {
809                 *cp++ = '\0';
810                 if (c != '#')
811                         return (skip(cp));
812         }
813         return (NULL);
814 }
815
816 /*
817  * Read an ascii label in from fd f,
818  * in the same format as that put out by display(),
819  * and fill in lp.
820  */
821 int
822 getasciilabel(FILE *f, struct disklabel *lp)
823 {
824         char *cp;
825         const char **cpp;
826         u_int part;
827         char *tp, line[BUFSIZ];
828         u_long v;
829         int lineno = 0, errors = 0;
830         int i;
831         char empty[] = "";
832         char unknown[] = "unknown";
833
834         bzero(&part_set, sizeof(part_set));
835         bzero(&part_size_type, sizeof(part_size_type));
836         bzero(&part_offset_type, sizeof(part_offset_type));
837         lp->d_bbsize = BBSIZE;                          /* XXX */
838         lp->d_sbsize = SBSIZE;                          /* XXX */
839         while (fgets(line, sizeof(line) - 1, f)) {
840                 lineno++;
841                 if ((cp = strchr(line,'\n')) != 0)
842                         *cp = '\0';
843                 cp = skip(line);
844                 if (cp == NULL)
845                         continue;
846                 tp = strchr(cp, ':');
847                 if (tp == NULL) {
848                         fprintf(stderr, "line %d: syntax error\n", lineno);
849                         errors++;
850                         continue;
851                 }
852                 *tp++ = '\0', tp = skip(tp);
853                 if (streq(cp, "type")) {
854                         if (tp == NULL)
855                                 tp = unknown;
856                         cpp = dktypenames;
857                         for (; cpp < &dktypenames[DKMAXTYPES]; cpp++) {
858                                 if (*cpp && streq(*cpp, tp)) {
859                                         lp->d_type = cpp - dktypenames;
860                                         break;
861                                 }
862                         }
863                         if (cpp < &dktypenames[DKMAXTYPES])
864                                 continue;
865                         v = strtoul(tp, NULL, 10);
866                         if (v >= DKMAXTYPES) {
867                                 fprintf(stderr, "line %d:%s %lu\n", lineno,
868                                     "Warning, unknown disk type", v);
869                         }
870                         lp->d_type = v;
871                         continue;
872                 }
873                 if (streq(cp, "flags")) {
874                         for (v = 0; (cp = tp) && *cp != '\0';) {
875                                 tp = word(cp);
876                                 if (streq(cp, "removeable"))
877                                         v |= 0; /* obsolete */
878                                 else if (streq(cp, "ecc"))
879                                         v |= 0; /* obsolete */
880                                 else if (streq(cp, "badsect"))
881                                         v |= 0; /* obsolete */
882                                 else {
883                                         fprintf(stderr,
884                                             "line %d: %s: bad flag\n",
885                                             lineno, cp);
886                                         errors++;
887                                 }
888                         }
889                         lp->d_flags = v;
890                         continue;
891                 }
892                 if (streq(cp, "drivedata")) {
893                         for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) {
894                                 lp->d_drivedata[i++] = strtoul(cp, NULL, 10);
895                                 tp = word(cp);
896                         }
897                         continue;
898                 }
899                 if (sscanf(cp, "%lu partitions", &v) == 1) {
900                         if (v == 0 || v > MAXPARTITIONS) {
901                                 fprintf(stderr,
902                                     "line %d: bad # of partitions\n", lineno);
903                                 lp->d_npartitions = MAXPARTITIONS;
904                                 errors++;
905                         } else
906                                 lp->d_npartitions = v;
907                         continue;
908                 }
909                 if (tp == NULL)
910                         tp = empty;
911                 if (streq(cp, "disk")) {
912                         strncpy(lp->d_typename, tp, sizeof (lp->d_typename));
913                         continue;
914                 }
915                 if (streq(cp, "label")) {
916                         strncpy(lp->d_packname, tp, sizeof (lp->d_packname));
917                         continue;
918                 }
919                 if (streq(cp, "bytes/sector")) {
920                         v = strtoul(tp, NULL, 10);
921                         if (v == 0 || (v % DEV_BSIZE) != 0) {
922                                 fprintf(stderr,
923                                     "line %d: %s: bad sector size\n",
924                                     lineno, tp);
925                                 errors++;
926                         } else
927                                 lp->d_secsize = v;
928                         continue;
929                 }
930                 if (streq(cp, "sectors/track")) {
931                         v = strtoul(tp, NULL, 10);
932 #if (ULONG_MAX != 0xffffffffUL)
933                         if (v == 0 || v > 0xffffffff) {
934 #else
935                         if (v == 0) {
936 #endif
937                                 fprintf(stderr, "line %d: %s: bad %s\n",
938                                     lineno, tp, cp);
939                                 errors++;
940                         } else
941                                 lp->d_nsectors = v;
942                         continue;
943                 }
944                 if (streq(cp, "sectors/cylinder")) {
945                         v = strtoul(tp, NULL, 10);
946                         if (v == 0) {
947                                 fprintf(stderr, "line %d: %s: bad %s\n",
948                                     lineno, tp, cp);
949                                 errors++;
950                         } else
951                                 lp->d_secpercyl = v;
952                         continue;
953                 }
954                 if (streq(cp, "tracks/cylinder")) {
955                         v = strtoul(tp, NULL, 10);
956                         if (v == 0) {
957                                 fprintf(stderr, "line %d: %s: bad %s\n",
958                                     lineno, tp, cp);
959                                 errors++;
960                         } else
961                                 lp->d_ntracks = v;
962                         continue;
963                 }
964                 if (streq(cp, "cylinders")) {
965                         v = strtoul(tp, NULL, 10);
966                         if (v == 0) {
967                                 fprintf(stderr, "line %d: %s: bad %s\n",
968                                     lineno, tp, cp);
969                                 errors++;
970                         } else
971                                 lp->d_ncylinders = v;
972                         continue;
973                 }
974                 if (streq(cp, "sectors/unit")) {
975                         v = strtoul(tp, NULL, 10);
976                         if (v == 0) {
977                                 fprintf(stderr, "line %d: %s: bad %s\n",
978                                     lineno, tp, cp);
979                                 errors++;
980                         } else
981                                 lp->d_secperunit = v;
982                         continue;
983                 }
984                 if (streq(cp, "rpm")) {
985                         v = strtoul(tp, NULL, 10);
986                         if (v == 0 || v > USHRT_MAX) {
987                                 fprintf(stderr, "line %d: %s: bad %s\n",
988                                     lineno, tp, cp);
989                                 errors++;
990                         } else
991                                 lp->d_rpm = v;
992                         continue;
993                 }
994                 if (streq(cp, "interleave")) {
995                         v = strtoul(tp, NULL, 10);
996                         if (v == 0 || v > USHRT_MAX) {
997                                 fprintf(stderr, "line %d: %s: bad %s\n",
998                                     lineno, tp, cp);
999                                 errors++;
1000                         } else
1001                                 lp->d_interleave = v;
1002                         continue;
1003                 }
1004                 if (streq(cp, "trackskew")) {
1005                         v = strtoul(tp, NULL, 10);
1006                         if (v > USHRT_MAX) {
1007                                 fprintf(stderr, "line %d: %s: bad %s\n",
1008                                     lineno, tp, cp);
1009                                 errors++;
1010                         } else
1011                                 lp->d_trackskew = v;
1012                         continue;
1013                 }
1014                 if (streq(cp, "cylinderskew")) {
1015                         v = strtoul(tp, NULL, 10);
1016                         if (v > USHRT_MAX) {
1017                                 fprintf(stderr, "line %d: %s: bad %s\n",
1018                                     lineno, tp, cp);
1019                                 errors++;
1020                         } else
1021                                 lp->d_cylskew = v;
1022                         continue;
1023                 }
1024                 if (streq(cp, "headswitch")) {
1025                         v = strtoul(tp, NULL, 10);
1026                         lp->d_headswitch = v;
1027                         continue;
1028                 }
1029                 if (streq(cp, "track-to-track seek")) {
1030                         v = strtoul(tp, NULL, 10);
1031                         lp->d_trkseek = v;
1032                         continue;
1033                 }
1034                 /* the ':' was removed above */
1035                 if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') {
1036                         fprintf(stderr,
1037                             "line %d: %s: Unknown disklabel field\n", lineno,
1038                             cp);
1039                         errors++;
1040                         continue;
1041                 }
1042
1043                 /* Process a partition specification line. */
1044                 part = *cp - 'a';
1045                 if (part >= lp->d_npartitions) {
1046                         fprintf(stderr,
1047                             "line %d: partition name out of range a-%c: %s\n",
1048                             lineno, 'a' + lp->d_npartitions - 1, cp);
1049                         errors++;
1050                         continue;
1051                 }
1052                 part_set[part] = 1;
1053
1054                 if (getasciipartspec(tp, lp, part, lineno) != 0) {
1055                         errors++;
1056                         break;
1057                 }
1058         }
1059         errors += checklabel(lp);
1060         return (errors == 0);
1061 }
1062
1063 #define NXTNUM(n) do { \
1064         if (tp == NULL) { \
1065                 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1066                 return (1); \
1067         } else { \
1068                 cp = tp, tp = word(cp); \
1069                 (n) = strtoul(cp, NULL, 10); \
1070         } \
1071 } while (0)
1072
1073 /* retain 1 character following number */
1074 #define NXTWORD(w,n) do { \
1075         if (tp == NULL) { \
1076                 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1077                 return (1); \
1078         } else { \
1079                 char *tmp; \
1080                 cp = tp, tp = word(cp); \
1081                 (n) = strtoul(cp, &tmp, 10); \
1082                 if (tmp) (w) = *tmp; \
1083         } \
1084 } while (0)
1085
1086 /*
1087  * Read a partition line into partition `part' in the specified disklabel.
1088  * Return 0 on success, 1 on failure.
1089  */
1090 int
1091 getasciipartspec(char *tp, struct disklabel *lp, int part, int lineno)
1092 {
1093         struct partition *pp;
1094         char *cp;
1095         const char **cpp;
1096         u_long v;
1097
1098         pp = &lp->d_partitions[part];
1099         cp = NULL;
1100
1101         /*
1102          * size
1103          */
1104         v = 0;
1105         NXTWORD(part_size_type[part],v);
1106         if (v == 0 && part_size_type[part] != '*') {
1107                 fprintf(stderr,
1108                     "line %d: %s: bad partition size\n", lineno, cp);
1109                 return (1);
1110         }
1111         pp->p_size = v;
1112
1113         /*
1114          * offset
1115          */
1116         v = 0;
1117         NXTWORD(part_offset_type[part],v);
1118         if (v == 0 && part_offset_type[part] != '*' &&
1119             part_offset_type[part] != '\0') {
1120                 fprintf(stderr,
1121                     "line %d: %s: bad partition offset\n", lineno, cp);
1122                 return (1);
1123         }
1124         pp->p_offset = v;
1125
1126         /*
1127          * fstype
1128          */
1129         cp = tp;
1130         tp = word(cp);
1131         for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++)
1132                 if (*cpp && streq(*cpp, cp))
1133                         break;
1134         if (*cpp != NULL) {
1135                 pp->p_fstype = cpp - fstypenames;
1136         } else {
1137                 if (isdigit(*cp))
1138                         v = strtoul(cp, NULL, 10);
1139                 else
1140                         v = FSMAXTYPES;
1141                 if (v >= FSMAXTYPES) {
1142                         fprintf(stderr,
1143                             "line %d: Warning, unknown filesystem type %s\n",
1144                             lineno, cp);
1145                         v = FS_UNUSED;
1146                 }
1147                 pp->p_fstype = v;
1148         }
1149
1150         pp->p_fsize = 0;
1151         pp->p_frag = 0;
1152         pp->p_cpg = 0;
1153
1154         cp = tp;
1155         if (tp) {
1156                 fprintf(stderr, "line %d: Warning, fragment, block, "
1157                                 "and bps/cpg fields are no\n"
1158                                 "longer supported and must be specified "
1159                                 "via newfs options instead.\n",
1160                         lineno);
1161         }
1162         return(0);
1163 }
1164
1165 /*
1166  * Check disklabel for errors and fill in
1167  * derived fields according to supplied values.
1168  */
1169 int
1170 checklabel(struct disklabel *lp)
1171 {
1172         struct partition *pp;
1173         int i, errors = 0;
1174         char part;
1175         u_long total_size, total_percent, current_offset;
1176         int seen_default_offset;
1177         int hog_part;
1178         int j;
1179         struct partition *pp2;
1180
1181         if (lp->d_secsize == 0) {
1182                 fprintf(stderr, "sector size 0\n");
1183                 return (1);
1184         }
1185         if (lp->d_nsectors == 0) {
1186                 fprintf(stderr, "sectors/track 0\n");
1187                 return (1);
1188         }
1189         if (lp->d_ntracks == 0) {
1190                 fprintf(stderr, "tracks/cylinder 0\n");
1191                 return (1);
1192         }
1193         if  (lp->d_ncylinders == 0) {
1194                 fprintf(stderr, "cylinders/unit 0\n");
1195                 errors++;
1196         }
1197         if (lp->d_rpm == 0)
1198                 Warning("revolutions/minute 0");
1199         if (lp->d_secpercyl == 0)
1200                 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
1201         if (lp->d_secperunit == 0)
1202                 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
1203         if (lp->d_bbsize == 0) {
1204                 fprintf(stderr, "boot block size 0\n");
1205                 errors++;
1206         } else if (lp->d_bbsize % lp->d_secsize)
1207                 Warning("boot block size %% sector-size != 0");
1208         if (lp->d_sbsize == 0) {
1209                 fprintf(stderr, "super block size 0\n");
1210                 errors++;
1211         } else if (lp->d_sbsize % lp->d_secsize)
1212                 Warning("super block size %% sector-size != 0");
1213         if (lp->d_npartitions > MAXPARTITIONS)
1214                 Warning("number of partitions (%lu) > MAXPARTITIONS (%d)",
1215                     (u_long)lp->d_npartitions, MAXPARTITIONS);
1216
1217         /* first allocate space to the partitions, then offsets */
1218         total_size = 0; /* in sectors */
1219         total_percent = 0; /* in percent */
1220         hog_part = -1;
1221         /* find all fixed partitions */
1222         for (i = 0; i < lp->d_npartitions; i++) {
1223                 pp = &lp->d_partitions[i];
1224                 if (part_set[i]) {
1225                         if (part_size_type[i] == '*') {
1226                                 if (i == RAW_PART) {
1227                                         pp->p_size = lp->d_secperunit;
1228                                 } else {
1229                                         if (hog_part != -1)
1230                                                 Warning("Too many '*' partitions (%c and %c)",
1231                                                     hog_part + 'a',i + 'a');
1232                                         else
1233                                                 hog_part = i;
1234                                 }
1235                         } else {
1236                                 off_t size;
1237
1238                                 size = pp->p_size;
1239                                 switch (part_size_type[i]) {
1240                                 case '%':
1241                                         total_percent += size;
1242                                         break;
1243                                 case 'k':
1244                                 case 'K':
1245                                         size *= 1024ULL;
1246                                         break;
1247                                 case 'm':
1248                                 case 'M':
1249                                         size *= 1024ULL * 1024ULL;
1250                                         break;
1251                                 case 'g':
1252                                 case 'G':
1253                                         size *= 1024ULL * 1024ULL * 1024ULL;
1254                                         break;
1255                                 case '\0':
1256                                         break;
1257                                 default:
1258                                         Warning("unknown size specifier '%c' (K/M/G are valid)",part_size_type[i]);
1259                                         break;
1260                                 }
1261                                 /* don't count %'s yet */
1262                                 if (part_size_type[i] != '%') {
1263                                         /*
1264                                          * for all not in sectors, convert to
1265                                          * sectors
1266                                          */
1267                                         if (part_size_type[i] != '\0') {
1268                                                 if (size % lp->d_secsize != 0)
1269                                                         Warning("partition %c not an integer number of sectors",
1270                                                             i + 'a');
1271                                                 size /= lp->d_secsize;
1272                                                 pp->p_size = size;
1273                                         }
1274                                         /* else already in sectors */
1275                                         if (i != RAW_PART)
1276                                                 total_size += size;
1277                                 }
1278                         }
1279                 }
1280         }
1281         /* handle % partitions - note %'s don't need to add up to 100! */
1282         if (total_percent != 0) {
1283                 long free_space = lp->d_secperunit - total_size;
1284                 if (total_percent > 100) {
1285                         fprintf(stderr,"total percentage %lu is greater than 100\n",
1286                             total_percent);
1287                         errors++;
1288                 }
1289
1290                 if (free_space > 0) {
1291                         for (i = 0; i < lp->d_npartitions; i++) {
1292                                 pp = &lp->d_partitions[i];
1293                                 if (part_set[i] && part_size_type[i] == '%') {
1294                                         /* careful of overflows! and integer roundoff */
1295                                         pp->p_size = ((double)pp->p_size/100) * free_space;
1296                                         total_size += pp->p_size;
1297
1298                                         /* FIX we can lose a sector or so due to roundoff per
1299                                            partition.  A more complex algorithm could avoid that */
1300                                 }
1301                         }
1302                 } else {
1303                         fprintf(stderr,
1304                             "%ld sectors available to give to '*' and '%%' partitions\n",
1305                             free_space);
1306                         errors++;
1307                         /* fix?  set all % partitions to size 0? */
1308                 }
1309         }
1310         /* give anything remaining to the hog partition */
1311         if (hog_part != -1) {
1312                 lp->d_partitions[hog_part].p_size = lp->d_secperunit - total_size;
1313                 total_size = lp->d_secperunit;
1314         }
1315
1316         /* Now set the offsets for each partition */
1317         current_offset = 0; /* in sectors */
1318         seen_default_offset = 0;
1319         for (i = 0; i < lp->d_npartitions; i++) {
1320                 part = 'a' + i;
1321                 pp = &lp->d_partitions[i];
1322                 if (part_set[i]) {
1323                         if (part_offset_type[i] == '*') {
1324                                 if (i == RAW_PART) {
1325                                         pp->p_offset = 0;
1326                                 } else {
1327                                         pp->p_offset = current_offset;
1328                                         seen_default_offset = 1;
1329                                 }
1330                         } else {
1331                                 /* allow them to be out of order for old-style tables */
1332                                 if (pp->p_offset < current_offset && 
1333                                     seen_default_offset && i != RAW_PART &&
1334                                     pp->p_fstype != FS_VINUM) {
1335                                         fprintf(stderr,
1336 "Offset %ld for partition %c overlaps previous partition which ends at %lu\n",
1337                                             (long)pp->p_offset,i+'a',current_offset);
1338                                         fprintf(stderr,
1339 "Labels with any *'s for offset must be in ascending order by sector\n");
1340                                         errors++;
1341                                 } else if (pp->p_offset != current_offset &&
1342                                     i != RAW_PART && seen_default_offset) {
1343                                         /* 
1344                                          * this may give unneeded warnings if 
1345                                          * partitions are out-of-order
1346                                          */
1347                                         Warning(
1348 "Offset %ld for partition %c doesn't match expected value %ld",
1349                                             (long)pp->p_offset, i + 'a', current_offset);
1350                                 }
1351                         }
1352                         if (i != RAW_PART)
1353                                 current_offset = pp->p_offset + pp->p_size; 
1354                 }
1355         }
1356
1357         for (i = 0; i < lp->d_npartitions; i++) {
1358                 part = 'a' + i;
1359                 pp = &lp->d_partitions[i];
1360                 if (pp->p_size == 0 && pp->p_offset != 0)
1361                         Warning("partition %c: size 0, but offset %lu",
1362                             part, (u_long)pp->p_offset);
1363 #ifdef notdef
1364                 if (pp->p_size % lp->d_secpercyl)
1365                         Warning("partition %c: size %% cylinder-size != 0",
1366                             part);
1367                 if (pp->p_offset % lp->d_secpercyl)
1368                         Warning("partition %c: offset %% cylinder-size != 0",
1369                             part);
1370 #endif
1371                 if (pp->p_offset > lp->d_secperunit) {
1372                         fprintf(stderr,
1373                             "partition %c: offset past end of unit\n", part);
1374                         errors++;
1375                 }
1376                 if (pp->p_offset + pp->p_size > lp->d_secperunit) {
1377                         fprintf(stderr,
1378                         "partition %c: partition extends past end of unit\n",
1379                             part);
1380                         errors++;
1381                 }
1382                 if (i == RAW_PART)
1383                 {
1384                         if (pp->p_fstype != FS_UNUSED)
1385                                 Warning("partition %c is not marked as unused!",part);
1386                         if (pp->p_offset != 0)
1387                                 Warning("partition %c doesn't start at 0!",part);
1388                         if (pp->p_size != lp->d_secperunit)
1389                                 Warning("partition %c doesn't cover the whole unit!",part);
1390
1391                         if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) ||
1392                             (pp->p_size != lp->d_secperunit)) {
1393                                 Warning("An incorrect partition %c may cause problems for "
1394                                     "standard system utilities",part);
1395                         }
1396                 }
1397
1398                 /* check for overlaps */
1399                 /* this will check for all possible overlaps once and only once */
1400                 for (j = 0; j < i; j++) {
1401                         pp2 = &lp->d_partitions[j];
1402                         if (j != RAW_PART && i != RAW_PART &&   
1403                             pp->p_fstype != FS_VINUM &&
1404                             pp2->p_fstype != FS_VINUM &&
1405                             part_set[i] && part_set[j]) {
1406                                 if (pp2->p_offset < pp->p_offset + pp->p_size &&
1407                                     (pp2->p_offset + pp2->p_size > pp->p_offset ||
1408                                         pp2->p_offset >= pp->p_offset)) {
1409                                         fprintf(stderr,"partitions %c and %c overlap!\n",
1410                                             j + 'a', i + 'a');
1411                                         errors++;
1412                                 }
1413                         }
1414                 }
1415         }
1416         for (; i < 8 || i < lp->d_npartitions; i++) {
1417                 part = 'a' + i;
1418                 pp = &lp->d_partitions[i];
1419                 if (pp->p_size || pp->p_offset)
1420                         Warning("unused partition %c: size %d offset %lu",
1421                             'a' + i, pp->p_size, (u_long)pp->p_offset);
1422         }
1423         return (errors);
1424 }
1425
1426 /*
1427  * When operating on a "virgin" disk, try getting an initial label
1428  * from the associated device driver.  This might work for all device
1429  * drivers that are able to fetch some initial device parameters
1430  * without even having access to a (BSD) disklabel, like SCSI disks,
1431  * most IDE drives, or vn devices.
1432  *
1433  * The device name must be given in its "canonical" form.
1434  */
1435 static struct disklabel dlab;
1436
1437 struct disklabel *
1438 getvirginlabel(void)
1439 {
1440         struct disklabel *dl = &dlab;
1441         char nambuf[BBSIZE];
1442         int f;
1443
1444         if (dkname[0] == '/') {
1445                 warnx("\"auto\" requires the usage of a canonical disk name");
1446                 return (NULL);
1447         }
1448         snprintf(nambuf, BBSIZE, "%s%s", _PATH_DEV, dkname);
1449         if ((f = open(nambuf, O_RDONLY)) == -1) {
1450                 warn("cannot open %s", nambuf);
1451                 return (NULL);
1452         }
1453
1454         /*
1455          * Try to use the new get-virgin-label ioctl.  If it fails,
1456          * fallback to the old get-disdk-info ioctl.
1457          */
1458         if (ioctl(f, DIOCGDVIRGIN, dl) < 0) {
1459                 if (ioctl(f, DIOCGDINFO, dl) < 0) {
1460                         warn("ioctl DIOCGDINFO");
1461                         close(f);
1462                         return (NULL);
1463                 }
1464         }
1465         close(f);
1466         return (dl);
1467 }
1468
1469 struct disklabel *
1470 getdisklabelfromdisktab(const char *name)
1471 {
1472         struct disktab *dt;
1473         struct disklabel *dl = &dlab;
1474         int i;
1475
1476         if ((dt = getdisktabbyname(name)) == NULL)
1477                 return(NULL);
1478         dl->d_magic = DISKMAGIC;
1479         dl->d_type = dt->d_typeid;
1480         dl->d_subtype = 0;
1481         dl->d_secsize = dt->d_media_blksize;
1482         dl->d_nsectors = dt->d_secpertrack;
1483         dl->d_ntracks = dt->d_nheads;
1484         dl->d_ncylinders = dt->d_ncylinders;
1485         dl->d_secpercyl = dt->d_secpercyl;
1486         dl->d_secperunit = dt->d_media_blocks;
1487         dl->d_rpm = dt->d_rpm;
1488         dl->d_interleave = dt->d_interleave;
1489         dl->d_trackskew = dt->d_trackskew;
1490         dl->d_cylskew = dt->d_cylskew;
1491         dl->d_headswitch = dt->d_headswitch;
1492         dl->d_trkseek = dt->d_trkseek;
1493         dl->d_magic2 = DISKMAGIC;
1494         dl->d_npartitions = dt->d_npartitions;
1495         dl->d_bbsize = dt->d_bbsize;
1496         dl->d_sbsize = dt->d_sbsize;
1497         for (i = 0; i < dt->d_npartitions; ++i) {
1498                 struct partition *dlp = &dl->d_partitions[i];
1499                 struct dt_partition *dtp = &dt->d_partitions[i];
1500
1501                 dlp->p_size     = dtp->p_size;
1502                 dlp->p_offset   = dtp->p_offset;
1503                 dlp->p_fsize    = dtp->p_fsize;
1504                 dlp->p_fstype   = dtp->p_fstype;
1505                 dlp->p_frag     = dtp->p_fsize;
1506         }
1507         return(dl);
1508 }
1509
1510 /*
1511  * If we are installing a boot program that doesn't fit in d_bbsize
1512  * we need to mark those partitions that the boot overflows into.
1513  * This allows newfs to prevent creation of a filesystem where it might
1514  * clobber bootstrap code.
1515  */
1516 void
1517 setbootflag(struct disklabel *lp)
1518 {
1519         struct partition *pp;
1520         int i, errors = 0;
1521         char part;
1522         u_long boffset;
1523
1524         if (bootbuf == 0)
1525                 return;
1526         boffset = bootsize / lp->d_secsize;
1527         for (i = 0; i < lp->d_npartitions; i++) {
1528                 part = 'a' + i;
1529                 pp = &lp->d_partitions[i];
1530                 if (pp->p_size == 0)
1531                         continue;
1532                 if (boffset <= pp->p_offset) {
1533                         if (pp->p_fstype == FS_BOOT)
1534                                 pp->p_fstype = FS_UNUSED;
1535                 } else if (pp->p_fstype != FS_BOOT) {
1536                         if (pp->p_fstype != FS_UNUSED) {
1537                                 fprintf(stderr,
1538                                         "boot overlaps used partition %c\n",
1539                                         part);
1540                                 errors++;
1541                         } else {
1542                                 pp->p_fstype = FS_BOOT;
1543                                 Warning("boot overlaps partition %c, %s",
1544                                         part, "marked as FS_BOOT");
1545                         }
1546                 }
1547         }
1548         if (errors)
1549                 errx(4, "cannot install boot program");
1550 }
1551
1552 /*VARARGS1*/
1553 void
1554 Warning(const char *fmt, ...)
1555 {
1556         va_list ap;
1557
1558         fprintf(stderr, "Warning, ");
1559         va_start(ap, fmt);
1560         vfprintf(stderr, fmt, ap);
1561         fprintf(stderr, "\n");
1562         va_end(ap);
1563 }
1564
1565 /*
1566  * Check to see if the bootblocks are in the wrong place.  FBsd5 bootblocks
1567  * and earlier DFly bb's are packed against the old disklabel and a new
1568  * disklabel would blow them up.  This is a hack that should be removed
1569  * in 2006 sometime (if ever).
1570  */
1571
1572 int
1573 checkoldboot(int f, const char *bootbuffer)
1574 {
1575         char buf[BBSIZE];
1576
1577         if (bootbuffer && strncmp(bootbuffer + 0x402, "BTX", 3) == 0)
1578                 return(0);
1579         lseek(f, (off_t)0, SEEK_SET);
1580         if (read(f, buf, sizeof(buf)) != sizeof(buf))
1581                 return(0);
1582         if (strncmp(buf + 0x402, "BTX", 3) == 0)  /* new location */
1583                 return(0);
1584         if (strncmp(buf + 0x316, "BTX", 3) == 0)  /* old location */
1585                 return(1);
1586         return(0);
1587 }
1588
1589 void
1590 usage(void)
1591 {
1592 #if NUMBOOT > 0
1593         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",
1594                 "usage: disklabel [-r] disk",
1595                 "\t\t(to read label)",
1596                 "       disklabel -w [-r] [-n] disk type [ packid ]",
1597                 "\t\t(to write label with existing boot program)",
1598                 "       disklabel -e [-r] [-n] disk",
1599                 "\t\t(to edit label)",
1600                 "       disklabel -R [-r] [-n] disk protofile",
1601                 "\t\t(to restore label with existing boot program)",
1602 #if NUMBOOT > 1
1603                 "       disklabel -B [-n] [ -b boot1 [ -s boot2 ] ] disk [ type ]",
1604                 "\t\t(to install boot program with existing label)",
1605                 "       disklabel -w -B [-n] [ -b boot1 [ -s boot2 ] ] disk type [ packid ]",
1606                 "\t\t(to write label and boot program)",
1607                 "       disklabel -R -B [-n] [ -b boot1 [ -s boot2 ] ] disk protofile [ type ]",
1608                 "\t\t(to restore label and boot program)",
1609 #else
1610                 "       disklabel -B [-n] [ -b bootprog ] disk [ type ]",
1611                 "\t\t(to install boot program with existing on-disk label)",
1612                 "       disklabel -w -B [-n] [ -b bootprog ] disk type [ packid ]",
1613                 "\t\t(to write label and install boot program)",
1614                 "       disklabel -R -B [-n] [ -b bootprog ] disk protofile [ type ]",
1615                 "\t\t(to restore label and install boot program)",
1616 #endif
1617                 "       disklabel [-NW] disk",
1618                 "\t\t(to write disable/enable label)");
1619 #else
1620         fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
1621                 "usage: disklabel [-r] disk", "(to read label)",
1622                 "       disklabel -w [-r] [-n] disk type [ packid ]",
1623                 "\t\t(to write label)",
1624                 "       disklabel -e [-r] [-n] disk",
1625                 "\t\t(to edit label)",
1626                 "       disklabel -R [-r] [-n] disk protofile",
1627                 "\t\t(to restore label)",
1628                 "       disklabel [-NW] disk",
1629                 "\t\t(to write disable/enable label)");
1630 #endif
1631         exit(1);
1632 }