]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/bsdlabel/bsdlabel.c
Remove pc98 support completely.
[FreeBSD/FreeBSD.git] / sbin / bsdlabel / bsdlabel.c
1 /*
2  * Copyright (c) 1994, 1995 Gordon W. Ross
3  * Copyright (c) 1994 Theo de Raadt
4  * All rights reserved.
5  * Copyright (c) 1987, 1993
6  *      The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Symmetric Computer Systems.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *      This product includes software developed by the University of
22  *      California, Berkeley and its contributors.
23  *      This product includes software developed by Theo de Raadt.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  *      from: $NetBSD: disksubr.c,v 1.13 2000/12/17 22:39:18 pk $
41  */
42
43 #if 0
44 #ifndef lint
45 static const char copyright[] =
46 "@(#) Copyright (c) 1987, 1993\n\
47         The Regents of the University of California.  All rights reserved.\n";
48 #endif /* not lint */
49
50 #ifndef lint
51 static char sccsid[] = "@(#)disklabel.c 8.2 (Berkeley) 1/7/94";
52 /* from static char sccsid[] = "@(#)disklabel.c 1.2 (Symmetric) 11/28/85"; */
53 #endif /* not lint */
54 #endif
55 #include <sys/cdefs.h>
56 __FBSDID("$FreeBSD$");
57
58 #include <sys/param.h>
59 #include <stdint.h>
60 #include <sys/file.h>
61 #include <sys/stat.h>
62 #include <sys/wait.h>
63 #include <sys/disk.h>
64 #define DKTYPENAMES
65 #define FSTYPENAMES
66 #define MAXPARTITIONS   20
67 #include <sys/disklabel.h>
68
69 #include <unistd.h>
70 #include <string.h>
71 #include <stdio.h>
72 #include <libgeom.h>
73 #include <stdlib.h>
74 #include <signal.h>
75 #include <stdarg.h>
76 #include <ctype.h>
77 #include <err.h>
78 #include <errno.h>
79
80 #include "pathnames.h"
81
82 static void     makelabel(const char *, struct disklabel *);
83 static int      geom_class_available(const char *);
84 static int      writelabel(void);
85 static int      readlabel(int flag);
86 static void     display(FILE *, const struct disklabel *);
87 static int      edit(void);
88 static int      editit(void);
89 static void     fixlabel(struct disklabel *);
90 static char     *skip(char *);
91 static char     *word(char *);
92 static int      getasciilabel(FILE *, struct disklabel *);
93 static int      getasciipartspec(char *, struct disklabel *, int, int);
94 static int      checklabel(struct disklabel *);
95 static void     usage(void);
96 static struct disklabel *getvirginlabel(void);
97
98 #define DEFEDITOR       _PATH_VI
99 #define DEFPARTITIONS   8
100
101 static char     *specname;
102 static char     *pname;
103 static char     tmpfil[] = PATH_TMPFILE;
104
105 static struct   disklabel lab;
106 static u_char   bootarea[BBSIZE];
107 static off_t    mediasize;
108 static ssize_t  secsize;
109 static char     blank[] = "";
110 static char     unknown[] = "unknown";
111
112 #define MAX_PART ('z')
113 #define MAX_NUM_PARTS (1 + MAX_PART - 'a')
114 static char    part_size_type[MAX_NUM_PARTS];
115 static char    part_offset_type[MAX_NUM_PARTS];
116 static int     part_set[MAX_NUM_PARTS];
117
118 static int      installboot;    /* non-zero if we should install a boot program */
119 static int      allfields;      /* present all fields in edit */
120 static char const *xxboot;      /* primary boot */
121
122 static uint32_t lba_offset;
123 #ifndef LABELSECTOR
124 #define LABELSECTOR -1
125 #endif
126 #ifndef LABELOFFSET
127 #define LABELOFFSET -1
128 #endif
129 static int labelsoffset = LABELSECTOR;
130 static int labeloffset = LABELOFFSET;
131 static int bbsize = BBSIZE;
132
133 static enum {
134         UNSPEC, EDIT, READ, RESTORE, WRITE, WRITEBOOT
135 } op = UNSPEC;
136
137
138 static int      disable_write;   /* set to disable writing to disk label */
139 static int      is_file;        /* work on a file (abs. pathname), "-f" opt. */
140
141 int
142 main(int argc, char *argv[])
143 {
144         FILE *t;
145         int ch, error, fd;
146         const char *name;
147         
148         error = 0;
149         name = NULL;
150
151         while ((ch = getopt(argc, argv, "ABb:efm:nRrw")) != -1)
152                 switch (ch) {
153                         case 'A':
154                                 allfields = 1;
155                                 break;
156                         case 'B':
157                                 ++installboot;
158                                 break;
159                         case 'b':
160                                 xxboot = optarg;
161                                 break;
162                         case 'f':
163                                 is_file=1;
164                                 break;
165                         case 'm':
166                                 if (!strcmp(optarg, "i386") ||
167                                     !strcmp(optarg, "amd64")) {
168                                         labelsoffset = 1;
169                                         labeloffset = 0;
170                                         bbsize = 8192;
171                                 } else {
172                                         errx(1, "Unsupported architecture");
173                                 }
174                                 break;
175                         case 'n':
176                                 disable_write = 1;
177                                 break;
178                         case 'R':
179                                 if (op != UNSPEC)
180                                         usage();
181                                 op = RESTORE;
182                                 break;
183                         case 'e':
184                                 if (op != UNSPEC)
185                                         usage();
186                                 op = EDIT;
187                                 break;
188                         case 'r':
189                                 /*
190                                  * We accept and ignore -r for compatibility with
191                                  * historical disklabel usage.
192                                  */
193                                 break;
194                         case 'w':
195                                 if (op != UNSPEC)
196                                         usage();
197                                 op = WRITE;
198                                 break;
199                         case '?':
200                         default:
201                                 usage();
202                 }
203         argc -= optind;
204         argv += optind;
205
206         if (argc < 1)
207                 usage();
208         if (labelsoffset < 0 || labeloffset < 0)
209                 errx(1, "a -m <architecture> option must be specified");
210
211         /* Figure out the names of the thing we're working on */
212         if (is_file) {
213                 specname = argv[0];
214         } else {
215                 specname = g_device_path(argv[0]);
216                 if (specname == NULL) {
217                         warn("unable to get correct path for %s", argv[0]);
218                         return(1);
219                 }
220                 fd = open(specname, O_RDONLY);
221                 if (fd < 0) {
222                         warn("error opening %s", specname);
223                         return(1);
224                 }
225                 pname = g_providername(fd);
226                 if (pname == NULL) {
227                         warn("error getting providername for %s", specname);
228                         close(fd);
229                         return(1);
230                 }
231                 close(fd);
232         }
233
234         if (installboot && op == UNSPEC)
235                 op = WRITEBOOT;
236         else if (op == UNSPEC)
237                 op = READ;
238
239         switch(op) {
240
241         case UNSPEC:
242                 break;
243
244         case EDIT:
245                 if (argc != 1)
246                         usage();
247                 readlabel(1);
248                 fixlabel(&lab);
249                 error = edit();
250                 break;
251
252         case READ:
253                 if (argc != 1)
254                         usage();
255                 readlabel(1);
256                 display(stdout, NULL);
257                 error = checklabel(NULL);
258                 break;
259
260         case RESTORE:
261                 if (argc != 2)
262                         usage();
263                 if (!(t = fopen(argv[1], "r")))
264                         err(4, "fopen %s", argv[1]);
265                 readlabel(0);
266                 if (!getasciilabel(t, &lab))
267                         exit(1);
268                 error = writelabel();
269                 break;
270
271         case WRITE:
272                 if (argc == 2)
273                         name = argv[1];
274                 else if (argc == 1)
275                         name = "auto";
276                 else
277                         usage();
278                 readlabel(0);
279                 makelabel(name, &lab);
280                 fixlabel(&lab);
281                 if (checklabel(NULL) == 0)
282                         error = writelabel();
283                 break;
284
285         case WRITEBOOT:
286
287                 readlabel(1);
288                 fixlabel(&lab);
289                 if (argc == 2)
290                         makelabel(argv[1], &lab);
291                 if (checklabel(NULL) == 0)
292                         error = writelabel();
293                 break;
294         }
295         exit(error);
296 }
297
298 static void
299 fixlabel(struct disklabel *lp)
300 {
301         struct partition *dp;
302         int i;
303
304         for (i = 0; i < lp->d_npartitions; i++) {
305                 if (i == RAW_PART)
306                         continue;
307                 if (lp->d_partitions[i].p_size)
308                         return;
309         }
310
311         dp = &lp->d_partitions[0];
312         dp->p_offset = BBSIZE / secsize;
313         dp->p_size = lp->d_secperunit - dp->p_offset;
314 }
315
316 /*
317  * Construct a prototype disklabel from /etc/disktab.
318  */
319 static void
320 makelabel(const char *type, struct disklabel *lp)
321 {
322         struct disklabel *dp;
323
324         if (strcmp(type, "auto") == 0)
325                 dp = getvirginlabel();
326         else
327                 dp = getdiskbyname(type);
328         if (dp == NULL)
329                 errx(1, "%s: unknown disk type", type);
330         *lp = *dp;
331         bzero(lp->d_packname, sizeof(lp->d_packname));
332 }
333
334 static void
335 readboot(void)
336 {
337         int fd;
338         struct stat st;
339
340         if (xxboot == NULL)
341                 xxboot = "/boot/boot";
342         fd = open(xxboot, O_RDONLY);
343         if (fd < 0)
344                 err(1, "cannot open %s", xxboot);
345         fstat(fd, &st);
346         if (st.st_size <= BBSIZE) {
347                 if (read(fd, bootarea, st.st_size) != st.st_size)
348                         err(1, "read error %s", xxboot);
349                 close(fd);
350                 return;
351         }
352         errx(1, "boot code %s is wrong size", xxboot);
353 }
354
355 static int
356 geom_class_available(const char *name)
357 {
358         struct gclass *class;
359         struct gmesh mesh;
360         int error;
361
362         error = geom_gettree(&mesh);
363         if (error != 0)
364                 errc(1, error, "Cannot get GEOM tree");
365
366         LIST_FOREACH(class, &mesh.lg_class, lg_class) {
367                 if (strcmp(class->lg_name, name) == 0) {
368                         geom_deletetree(&mesh);
369                         return (1);
370                 }
371         }
372
373         geom_deletetree(&mesh);
374
375         return (0);
376 }
377
378 static int
379 writelabel(void)
380 {
381         int i, fd, serrno;
382         struct gctl_req *grq;
383         char const *errstr;
384         struct disklabel *lp = &lab;
385
386         if (disable_write) {
387                 warnx("write to disk label suppressed - label was as follows:");
388                 display(stdout, NULL);
389                 return (0);
390         }
391
392         lp->d_magic = DISKMAGIC;
393         lp->d_magic2 = DISKMAGIC;
394         lp->d_checksum = 0;
395         lp->d_checksum = dkcksum(lp);
396         if (installboot)
397                 readboot();
398         for (i = 0; i < lab.d_npartitions; i++)
399                 if (lab.d_partitions[i].p_size)
400                         lab.d_partitions[i].p_offset += lba_offset;
401         bsd_disklabel_le_enc(bootarea + labeloffset + labelsoffset * lab.d_secsize,
402             lp);
403
404         fd = open(specname, O_RDWR);
405         if (fd < 0) {
406                 if (is_file) {
407                         warn("cannot open file %s for writing label", specname);
408                         return(1);
409                 } else
410                         serrno = errno;
411
412                 if (geom_class_available("PART") != 0) {
413                         /*
414                          * Since we weren't able open provider for
415                          * writing, then recommend user to use gpart(8).
416                          */
417                         warnc(serrno,
418                             "cannot open provider %s for writing label",
419                             specname);
420                         warnx("Try to use gpart(8).");
421                         return (1);
422                 }
423
424                 /* Give up if GEOM_BSD is not available. */
425                 if (geom_class_available("BSD") == 0) {
426                         warnc(serrno, "%s", specname);
427                         return (1);
428                 }
429
430                 grq = gctl_get_handle();
431                 gctl_ro_param(grq, "verb", -1, "write label");
432                 gctl_ro_param(grq, "class", -1, "BSD");
433                 gctl_ro_param(grq, "geom", -1, pname);
434                 gctl_ro_param(grq, "label", 148+16*8,
435                         bootarea + labeloffset + labelsoffset * lab.d_secsize);
436                 errstr = gctl_issue(grq);
437                 if (errstr != NULL) {
438                         warnx("%s", errstr);
439                         gctl_free(grq);
440                         return(1);
441                 }
442                 gctl_free(grq);
443                 if (installboot) {
444                         grq = gctl_get_handle();
445                         gctl_ro_param(grq, "verb", -1, "write bootcode");
446                         gctl_ro_param(grq, "class", -1, "BSD");
447                         gctl_ro_param(grq, "geom", -1, pname);
448                         gctl_ro_param(grq, "bootcode", BBSIZE, bootarea);
449                         errstr = gctl_issue(grq);
450                         if (errstr != NULL) {
451                                 warnx("%s", errstr);
452                                 gctl_free(grq);
453                                 return (1);
454                         }
455                         gctl_free(grq);
456                 }
457         } else {
458                 if (write(fd, bootarea, bbsize) != bbsize) {
459                         warn("write %s", specname);
460                         close (fd);
461                         return (1);
462                 }
463                 close (fd);
464         }
465         return (0);
466 }
467
468 static void
469 get_file_parms(int f)
470 {
471         int i;
472         struct stat sb;
473
474         if (fstat(f, &sb) != 0)
475                 err(4, "fstat failed");
476         i = sb.st_mode & S_IFMT;
477         if (i != S_IFREG && i != S_IFLNK)
478                 errx(4, "%s is not a valid file or link", specname);
479         secsize = DEV_BSIZE;
480         mediasize = sb.st_size;
481 }
482
483 /*
484  * Fetch disklabel for disk.
485  */
486 static int
487 readlabel(int flag)
488 {
489         ssize_t nbytes;
490         uint32_t lba;
491         int f, i;
492         int error;
493
494         f = open(specname, O_RDONLY);
495         if (f < 0)
496                 err(1, "%s", specname);
497         if (is_file)
498                 get_file_parms(f);
499         else {
500                 mediasize = g_mediasize(f);
501                 secsize = g_sectorsize(f);
502                 if (secsize < 0 || mediasize < 0)
503                         err(4, "cannot get disk geometry");
504         }
505         if (mediasize > (off_t)0xffffffff * secsize)
506                 errx(1,
507                     "disks with more than 2^32-1 sectors are not supported");
508         (void)lseek(f, (off_t)0, SEEK_SET);
509         nbytes = read(f, bootarea, BBSIZE);
510         if (nbytes == -1)
511                 err(4, "%s read", specname);
512         if (nbytes != BBSIZE)
513                 errx(4, "couldn't read %d bytes from %s", BBSIZE, specname);
514         close (f);
515         error = bsd_disklabel_le_dec(
516             bootarea + (labeloffset + labelsoffset * secsize),
517             &lab, MAXPARTITIONS);
518         if (flag && error)
519                 errx(1, "%s: no valid label found", specname);
520
521         if (is_file)
522                 return(0);
523
524         /*
525          * Compensate for absolute block addressing by finding the
526          * smallest partition offset and if the offset of the 'c'
527          * partition is equal to that, subtract it from all offsets.
528          */
529         lba = ~0;
530         for (i = 0; i < lab.d_npartitions; i++) {
531                 if (lab.d_partitions[i].p_size)
532                         lba = MIN(lba, lab.d_partitions[i].p_offset);
533         }
534         if (lba != 0 && lab.d_partitions[RAW_PART].p_offset == lba) {
535                 for (i = 0; i < lab.d_npartitions; i++) {
536                         if (lab.d_partitions[i].p_size)
537                                 lab.d_partitions[i].p_offset -= lba;
538                 }
539                 /*
540                  * Save the offset so that we can write the label
541                  * back with absolute block addresses.
542                  */
543                 lba_offset = lba;
544         }
545         return (error);
546 }
547
548
549 static void
550 display(FILE *f, const struct disklabel *lp)
551 {
552         int i, j;
553         const struct partition *pp;
554
555         if (lp == NULL)
556                 lp = &lab;
557
558         fprintf(f, "# %s:\n", specname);
559         if (allfields) {
560                 if (lp->d_type < DKMAXTYPES)
561                         fprintf(f, "type: %s\n", dktypenames[lp->d_type]);
562                 else
563                         fprintf(f, "type: %u\n", lp->d_type);
564                 fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename),
565                         lp->d_typename);
566                 fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname),
567                         lp->d_packname);
568                 fprintf(f, "flags:");
569                 if (lp->d_flags & D_REMOVABLE)
570                         fprintf(f, " removeable");
571                 if (lp->d_flags & D_ECC)
572                         fprintf(f, " ecc");
573                 if (lp->d_flags & D_BADSECT)
574                         fprintf(f, " badsect");
575                 fprintf(f, "\n");
576                 fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize);
577                 fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors);
578                 fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks);
579                 fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl);
580                 fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders);
581                 fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit);
582                 fprintf(f, "rpm: %u\n", lp->d_rpm);
583                 fprintf(f, "interleave: %u\n", lp->d_interleave);
584                 fprintf(f, "trackskew: %u\n", lp->d_trackskew);
585                 fprintf(f, "cylinderskew: %u\n", lp->d_cylskew);
586                 fprintf(f, "headswitch: %lu\t\t# milliseconds\n",
587                     (u_long)lp->d_headswitch);
588                 fprintf(f, "track-to-track seek: %ld\t# milliseconds\n",
589                     (u_long)lp->d_trkseek);
590                 fprintf(f, "drivedata: ");
591                 for (i = NDDATA - 1; i >= 0; i--)
592                         if (lp->d_drivedata[i])
593                                 break;
594                 if (i < 0)
595                         i = 0;
596                 for (j = 0; j <= i; j++)
597                         fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]);
598                 fprintf(f, "\n\n");
599         }
600         fprintf(f, "%u partitions:\n", lp->d_npartitions);
601         fprintf(f,
602             "#          size     offset    fstype   [fsize bsize bps/cpg]\n");
603         pp = lp->d_partitions;
604         for (i = 0; i < lp->d_npartitions; i++, pp++) {
605                 if (pp->p_size) {
606                         fprintf(f, "  %c: %10lu %10lu  ", 'a' + i,
607                            (u_long)pp->p_size, (u_long)pp->p_offset);
608                         if (pp->p_fstype < FSMAXTYPES)
609                                 fprintf(f, "%8.8s", fstypenames[pp->p_fstype]);
610                         else
611                                 fprintf(f, "%8d", pp->p_fstype);
612                         switch (pp->p_fstype) {
613
614                         case FS_UNUSED:                         /* XXX */
615                                 fprintf(f, "    %5lu %5lu %2s",
616                                     (u_long)pp->p_fsize,
617                                     (u_long)(pp->p_fsize * pp->p_frag), "");
618                                 break;
619
620                         case FS_BSDFFS:
621                                 fprintf(f, "    %5lu %5lu %5u",
622                                     (u_long)pp->p_fsize,
623                                     (u_long)(pp->p_fsize * pp->p_frag),
624                                     pp->p_cpg);
625                                 break;
626
627                         case FS_BSDLFS:
628                                 fprintf(f, "    %5lu %5lu %5d",
629                                     (u_long)pp->p_fsize,
630                                     (u_long)(pp->p_fsize * pp->p_frag),
631                                     pp->p_cpg);
632                                 break;
633
634                         default:
635                                 fprintf(f, "%20.20s", "");
636                                 break;
637                         }
638                         if (i == RAW_PART) {
639                                 fprintf(f, "  # \"raw\" part, don't edit");
640                         }
641                         fprintf(f, "\n");
642                 }
643         }
644         fflush(f);
645 }
646
647 static int
648 edit(void)
649 {
650         int c, fd;
651         struct disklabel label;
652         FILE *fp;
653
654         if ((fd = mkstemp(tmpfil)) == -1 ||
655             (fp = fdopen(fd, "w")) == NULL) {
656                 warnx("can't create %s", tmpfil);
657                 return (1);
658         }
659         display(fp, NULL);
660         fclose(fp);
661         for (;;) {
662                 if (!editit())
663                         break;
664                 fp = fopen(tmpfil, "r");
665                 if (fp == NULL) {
666                         warnx("can't reopen %s for reading", tmpfil);
667                         break;
668                 }
669                 bzero((char *)&label, sizeof(label));
670                 c = getasciilabel(fp, &label);
671                 fclose(fp);
672                 if (c) {
673                         lab = label;
674                         if (writelabel() == 0) {
675                                 (void) unlink(tmpfil);
676                                 return (0);
677                         }
678                 }
679                 printf("re-edit the label? [y]: ");
680                 fflush(stdout);
681                 c = getchar();
682                 if (c != EOF && c != (int)'\n')
683                         while (getchar() != (int)'\n')
684                                 ;
685                 if  (c == (int)'n')
686                         break;
687         }
688         (void) unlink(tmpfil);
689         return (1);
690 }
691
692 static int
693 editit(void)
694 {
695         int pid, xpid;
696         int locstat, omask;
697         const char *ed;
698         uid_t uid;
699         gid_t gid;
700
701         omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
702         while ((pid = fork()) < 0) {
703                 if (errno == EPROCLIM) {
704                         warnx("you have too many processes");
705                         return(0);
706                 }
707                 if (errno != EAGAIN) {
708                         warn("fork");
709                         return(0);
710                 }
711                 sleep(1);
712         }
713         if (pid == 0) {
714                 sigsetmask(omask);
715                 gid = getgid();
716                 if (setresgid(gid, gid, gid) == -1)
717                         err(1, "setresgid");
718                 uid = getuid();
719                 if (setresuid(uid, uid, uid) == -1)
720                         err(1, "setresuid");
721                 if ((ed = getenv("EDITOR")) == (char *)0)
722                         ed = DEFEDITOR;
723                 execlp(ed, ed, tmpfil, (char *)0);
724                 err(1, "%s", ed);
725         }
726         while ((xpid = wait(&locstat)) >= 0)
727                 if (xpid == pid)
728                         break;
729         sigsetmask(omask);
730         return(!locstat);
731 }
732
733 static char *
734 skip(char *cp)
735 {
736
737         while (*cp != '\0' && isspace(*cp))
738                 cp++;
739         if (*cp == '\0' || *cp == '#')
740                 return (NULL);
741         return (cp);
742 }
743
744 static char *
745 word(char *cp)
746 {
747         char c;
748
749         while (*cp != '\0' && !isspace(*cp) && *cp != '#')
750                 cp++;
751         if ((c = *cp) != '\0') {
752                 *cp++ = '\0';
753                 if (c != '#')
754                         return (skip(cp));
755         }
756         return (NULL);
757 }
758
759 /*
760  * Read an ascii label in from fd f,
761  * in the same format as that put out by display(),
762  * and fill in lp.
763  */
764 static int
765 getasciilabel(FILE *f, struct disklabel *lp)
766 {
767         char *cp, *endp;
768         const char **cpp;
769         u_int part;
770         char *tp, line[BUFSIZ];
771         u_long v;
772         int lineno = 0, errors = 0;
773         int i;
774
775         makelabel("auto", lp);
776         bzero(&part_set, sizeof(part_set));
777         bzero(&part_size_type, sizeof(part_size_type));
778         bzero(&part_offset_type, sizeof(part_offset_type));
779         lp->d_bbsize = BBSIZE;                          /* XXX */
780         lp->d_sbsize = 0;                               /* XXX */
781         while (fgets(line, sizeof(line) - 1, f)) {
782                 lineno++;
783                 if ((cp = strchr(line,'\n')) != NULL)
784                         *cp = '\0';
785                 cp = skip(line);
786                 if (cp == NULL)
787                         continue;
788                 tp = strchr(cp, ':');
789                 if (tp == NULL) {
790                         fprintf(stderr, "line %d: syntax error\n", lineno);
791                         errors++;
792                         continue;
793                 }
794                 *tp++ = '\0', tp = skip(tp);
795                 if (!strcmp(cp, "type")) {
796                         if (tp == NULL)
797                                 tp = unknown;
798                         cpp = dktypenames;
799                         for (; cpp < &dktypenames[DKMAXTYPES]; cpp++)
800                                 if (*cpp && !strcmp(*cpp, tp)) {
801                                         lp->d_type = cpp - dktypenames;
802                                         break;
803                                 }
804                         if (cpp < &dktypenames[DKMAXTYPES])
805                                 continue;
806                         errno = 0;
807                         v = strtoul(tp, &endp, 10);
808                         if (errno != 0 || *endp != '\0')
809                                 v = DKMAXTYPES;
810                         if (v >= DKMAXTYPES)
811                                 fprintf(stderr, "line %d:%s %lu\n", lineno,
812                                     "Warning, unknown disk type", v);
813                         else
814                                 lp->d_type = v;
815                         continue;
816                 }
817                 if (!strcmp(cp, "flags")) {
818                         for (v = 0; (cp = tp) && *cp != '\0';) {
819                                 tp = word(cp);
820                                 if (!strcmp(cp, "removeable"))
821                                         v |= D_REMOVABLE;
822                                 else if (!strcmp(cp, "ecc"))
823                                         v |= D_ECC;
824                                 else if (!strcmp(cp, "badsect"))
825                                         v |= D_BADSECT;
826                                 else {
827                                         fprintf(stderr,
828                                             "line %d: %s: bad flag\n",
829                                             lineno, cp);
830                                         errors++;
831                                 }
832                         }
833                         lp->d_flags = v;
834                         continue;
835                 }
836                 if (!strcmp(cp, "drivedata")) {
837                         for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) {
838                                 lp->d_drivedata[i++] = strtoul(cp, NULL, 10);
839                                 tp = word(cp);
840                         }
841                         continue;
842                 }
843                 if (sscanf(cp, "%lu partitions", &v) == 1) {
844                         if (v > MAXPARTITIONS) {
845                                 fprintf(stderr,
846                                     "line %d: bad # of partitions\n", lineno);
847                                 lp->d_npartitions = MAXPARTITIONS;
848                                 errors++;
849                         } else if (v < DEFPARTITIONS) {
850                                 fprintf(stderr,
851                                     "line %d: bad # of partitions\n", lineno);
852                                 lp->d_npartitions = DEFPARTITIONS;
853                                 errors++;
854                         } else
855                                 lp->d_npartitions = v;
856                         continue;
857                 }
858                 if (tp == NULL)
859                         tp = blank;
860                 if (!strcmp(cp, "disk")) {
861                         strncpy(lp->d_typename, tp, sizeof (lp->d_typename));
862                         continue;
863                 }
864                 if (!strcmp(cp, "label")) {
865                         strncpy(lp->d_packname, tp, sizeof (lp->d_packname));
866                         continue;
867                 }
868                 if (!strcmp(cp, "bytes/sector")) {
869                         v = strtoul(tp, NULL, 10);
870                         if (v == 0 || (v % DEV_BSIZE) != 0) {
871                                 fprintf(stderr,
872                                     "line %d: %s: bad sector size\n",
873                                     lineno, tp);
874                                 errors++;
875                         } else
876                                 lp->d_secsize = v;
877                         continue;
878                 }
879                 if (!strcmp(cp, "sectors/track")) {
880                         v = strtoul(tp, NULL, 10);
881 #if (ULONG_MAX != 0xffffffffUL)
882                         if (v == 0 || v > 0xffffffff)
883 #else
884                         if (v == 0)
885 #endif
886                         {
887                                 fprintf(stderr, "line %d: %s: bad %s\n",
888                                     lineno, tp, cp);
889                                 errors++;
890                         } else
891                                 lp->d_nsectors = v;
892                         continue;
893                 }
894                 if (!strcmp(cp, "sectors/cylinder")) {
895                         v = strtoul(tp, NULL, 10);
896                         if (v == 0) {
897                                 fprintf(stderr, "line %d: %s: bad %s\n",
898                                     lineno, tp, cp);
899                                 errors++;
900                         } else
901                                 lp->d_secpercyl = v;
902                         continue;
903                 }
904                 if (!strcmp(cp, "tracks/cylinder")) {
905                         v = strtoul(tp, NULL, 10);
906                         if (v == 0) {
907                                 fprintf(stderr, "line %d: %s: bad %s\n",
908                                     lineno, tp, cp);
909                                 errors++;
910                         } else
911                                 lp->d_ntracks = v;
912                         continue;
913                 }
914                 if (!strcmp(cp, "cylinders")) {
915                         v = strtoul(tp, NULL, 10);
916                         if (v == 0) {
917                                 fprintf(stderr, "line %d: %s: bad %s\n",
918                                     lineno, tp, cp);
919                                 errors++;
920                         } else
921                                 lp->d_ncylinders = v;
922                         continue;
923                 }
924                 if (!strcmp(cp, "sectors/unit")) {
925                         v = strtoul(tp, NULL, 10);
926                         if (v == 0) {
927                                 fprintf(stderr, "line %d: %s: bad %s\n",
928                                     lineno, tp, cp);
929                                 errors++;
930                         } else
931                                 lp->d_secperunit = v;
932                         continue;
933                 }
934                 if (!strcmp(cp, "rpm")) {
935                         v = strtoul(tp, NULL, 10);
936                         if (v == 0 || v > USHRT_MAX) {
937                                 fprintf(stderr, "line %d: %s: bad %s\n",
938                                     lineno, tp, cp);
939                                 errors++;
940                         } else
941                                 lp->d_rpm = v;
942                         continue;
943                 }
944                 if (!strcmp(cp, "interleave")) {
945                         v = strtoul(tp, NULL, 10);
946                         if (v == 0 || v > USHRT_MAX) {
947                                 fprintf(stderr, "line %d: %s: bad %s\n",
948                                     lineno, tp, cp);
949                                 errors++;
950                         } else
951                                 lp->d_interleave = v;
952                         continue;
953                 }
954                 if (!strcmp(cp, "trackskew")) {
955                         v = strtoul(tp, NULL, 10);
956                         if (v > USHRT_MAX) {
957                                 fprintf(stderr, "line %d: %s: bad %s\n",
958                                     lineno, tp, cp);
959                                 errors++;
960                         } else
961                                 lp->d_trackskew = v;
962                         continue;
963                 }
964                 if (!strcmp(cp, "cylinderskew")) {
965                         v = strtoul(tp, NULL, 10);
966                         if (v > USHRT_MAX) {
967                                 fprintf(stderr, "line %d: %s: bad %s\n",
968                                     lineno, tp, cp);
969                                 errors++;
970                         } else
971                                 lp->d_cylskew = v;
972                         continue;
973                 }
974                 if (!strcmp(cp, "headswitch")) {
975                         v = strtoul(tp, NULL, 10);
976                         lp->d_headswitch = v;
977                         continue;
978                 }
979                 if (!strcmp(cp, "track-to-track seek")) {
980                         v = strtoul(tp, NULL, 10);
981                         lp->d_trkseek = v;
982                         continue;
983                 }
984                 /* the ':' was removed above */
985                 if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') {
986                         fprintf(stderr,
987                             "line %d: %s: Unknown disklabel field\n", lineno,
988                             cp);
989                         errors++;
990                         continue;
991                 }
992
993                 /* Process a partition specification line. */
994                 part = *cp - 'a';
995                 if (part >= lp->d_npartitions) {
996                         fprintf(stderr,
997                             "line %d: partition name out of range a-%c: %s\n",
998                             lineno, 'a' + lp->d_npartitions - 1, cp);
999                         errors++;
1000                         continue;
1001                 }
1002                 part_set[part] = 1;
1003
1004                 if (getasciipartspec(tp, lp, part, lineno) != 0) {
1005                         errors++;
1006                         break;
1007                 }
1008         }
1009         errors += checklabel(lp);
1010         return (errors == 0);
1011 }
1012
1013 #define NXTNUM(n) do { \
1014         if (tp == NULL) { \
1015                 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1016                 return (1); \
1017         } else { \
1018                 cp = tp, tp = word(cp); \
1019                 (n) = strtoul(cp, NULL, 10); \
1020         } \
1021 } while (0)
1022
1023 /* retain 1 character following number */
1024 #define NXTWORD(w,n) do { \
1025         if (tp == NULL) { \
1026                 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1027                 return (1); \
1028         } else { \
1029                 char *tmp; \
1030                 cp = tp, tp = word(cp); \
1031                 (n) = strtoul(cp, &tmp, 10); \
1032                 if (tmp) (w) = *tmp; \
1033         } \
1034 } while (0)
1035
1036 /*
1037  * Read a partition line into partition `part' in the specified disklabel.
1038  * Return 0 on success, 1 on failure.
1039  */
1040 static int
1041 getasciipartspec(char *tp, struct disklabel *lp, int part, int lineno)
1042 {
1043         struct partition *pp;
1044         char *cp, *endp;
1045         const char **cpp;
1046         u_long v;
1047
1048         pp = &lp->d_partitions[part];
1049         cp = NULL;
1050
1051         v = 0;
1052         NXTWORD(part_size_type[part],v);
1053         if (v == 0 && part_size_type[part] != '*') {
1054                 fprintf(stderr,
1055                     "line %d: %s: bad partition size\n", lineno, cp);
1056                 return (1);
1057         }
1058         pp->p_size = v;
1059
1060         v = 0;
1061         NXTWORD(part_offset_type[part],v);
1062         if (v == 0 && part_offset_type[part] != '*' &&
1063             part_offset_type[part] != '\0') {
1064                 fprintf(stderr,
1065                     "line %d: %s: bad partition offset\n", lineno, cp);
1066                 return (1);
1067         }
1068         pp->p_offset = v;
1069         if (tp == NULL) {
1070                 fprintf(stderr, "line %d: missing file system type\n", lineno);
1071                 return (1);
1072         }
1073         cp = tp, tp = word(cp);
1074         for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++)
1075                 if (*cpp && !strcmp(*cpp, cp))
1076                         break;
1077         if (*cpp != NULL) {
1078                 pp->p_fstype = cpp - fstypenames;
1079         } else {
1080                 if (isdigit(*cp)) {
1081                         errno = 0;
1082                         v = strtoul(cp, &endp, 10);
1083                         if (errno != 0 || *endp != '\0')
1084                                 v = FSMAXTYPES;
1085                 } else
1086                         v = FSMAXTYPES;
1087                 if (v >= FSMAXTYPES) {
1088                         fprintf(stderr,
1089                             "line %d: Warning, unknown file system type %s\n",
1090                             lineno, cp);
1091                         v = FS_UNUSED;
1092                 }
1093                 pp->p_fstype = v;
1094         }
1095
1096         switch (pp->p_fstype) {
1097         case FS_UNUSED:
1098         case FS_BSDFFS:
1099         case FS_BSDLFS:
1100                 /* accept defaults for fsize/frag/cpg */
1101                 if (tp) {
1102                         NXTNUM(pp->p_fsize);
1103                         if (pp->p_fsize == 0)
1104                                 break;
1105                         NXTNUM(v);
1106                         pp->p_frag = v / pp->p_fsize;
1107                         if (tp != NULL)
1108                                 NXTNUM(pp->p_cpg);
1109                 }
1110                 /* else default to 0's */
1111                 break;
1112         default:
1113                 break;
1114         }
1115         return (0);
1116 }
1117
1118 /*
1119  * Check disklabel for errors and fill in
1120  * derived fields according to supplied values.
1121  */
1122 static int
1123 checklabel(struct disklabel *lp)
1124 {
1125         struct partition *pp;
1126         int i, errors = 0;
1127         char part;
1128         u_long base_offset, needed, total_size, total_percent, current_offset;
1129         long free_space;
1130         int seen_default_offset;
1131         int hog_part;
1132         int j;
1133         struct partition *pp2;
1134
1135         if (lp == NULL)
1136                 lp = &lab;
1137
1138         if (allfields) {
1139
1140                 if (lp->d_secsize == 0) {
1141                         fprintf(stderr, "sector size 0\n");
1142                         return (1);
1143                 }
1144                 if (lp->d_nsectors == 0) {
1145                         fprintf(stderr, "sectors/track 0\n");
1146                         return (1);
1147                 }
1148                 if (lp->d_ntracks == 0) {
1149                         fprintf(stderr, "tracks/cylinder 0\n");
1150                         return (1);
1151                 }
1152                 if  (lp->d_ncylinders == 0) {
1153                         fprintf(stderr, "cylinders/unit 0\n");
1154                         errors++;
1155                 }
1156                 if (lp->d_rpm == 0)
1157                         warnx("revolutions/minute 0");
1158                 if (lp->d_secpercyl == 0)
1159                         lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
1160                 if (lp->d_secperunit == 0)
1161                         lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
1162                 if (lp->d_bbsize == 0) {
1163                         fprintf(stderr, "boot block size 0\n");
1164                         errors++;
1165                 } else if (lp->d_bbsize % lp->d_secsize)
1166                         warnx("boot block size %% sector-size != 0");
1167                 if (lp->d_npartitions > MAXPARTITIONS) {
1168                         warnx("number of partitions (%lu) > MAXPARTITIONS (%d)",
1169                             (u_long)lp->d_npartitions, MAXPARTITIONS);
1170                         errors++;
1171                 }
1172                 if (lp->d_npartitions < DEFPARTITIONS) {
1173                         warnx("number of partitions (%lu) < DEFPARTITIONS (%d)",
1174                             (u_long)lp->d_npartitions, DEFPARTITIONS);
1175                         errors++;
1176                 }
1177         } else {
1178                 struct disklabel *vl;
1179
1180                 vl = getvirginlabel();
1181                 if (lp->d_secsize == 0)
1182                         lp->d_secsize = vl->d_secsize;
1183                 if (lp->d_nsectors == 0)
1184                         lp->d_nsectors = vl->d_nsectors;
1185                 if (lp->d_ntracks == 0)
1186                         lp->d_ntracks = vl->d_ntracks;
1187                 if (lp->d_ncylinders == 0)
1188                         lp->d_ncylinders = vl->d_ncylinders;
1189                 if (lp->d_rpm == 0)
1190                         lp->d_rpm = vl->d_rpm;
1191                 if (lp->d_interleave == 0)
1192                         lp->d_interleave = vl->d_interleave;
1193                 if (lp->d_secpercyl == 0)
1194                         lp->d_secpercyl = vl->d_secpercyl;
1195                 if (lp->d_secperunit == 0 ||
1196                     lp->d_secperunit > vl->d_secperunit)
1197                         lp->d_secperunit = vl->d_secperunit;
1198                 if (lp->d_bbsize == 0)
1199                         lp->d_bbsize = vl->d_bbsize;
1200                 if (lp->d_npartitions < DEFPARTITIONS ||
1201                     lp->d_npartitions > MAXPARTITIONS)
1202                         lp->d_npartitions = vl->d_npartitions;
1203         }
1204
1205
1206         /* first allocate space to the partitions, then offsets */
1207         total_size = 0; /* in sectors */
1208         total_percent = 0; /* in percent */
1209         hog_part = -1;
1210         /* find all fixed partitions */
1211         for (i = 0; i < lp->d_npartitions; i++) {
1212                 pp = &lp->d_partitions[i];
1213                 if (part_set[i]) {
1214                         if (part_size_type[i] == '*') {
1215                                 if (i == RAW_PART) {
1216                                         pp->p_size = lp->d_secperunit;
1217                                 } else {
1218                                         if (hog_part != -1)
1219                                                 warnx("Too many '*' partitions (%c and %c)",
1220                                                     hog_part + 'a',i + 'a');
1221                                         else
1222                                                 hog_part = i;
1223                                 }
1224                         } else {
1225                                 off_t size;
1226
1227                                 size = pp->p_size;
1228                                 switch (part_size_type[i]) {
1229                                 case '%':
1230                                         total_percent += size;
1231                                         break;
1232                                 case 't':
1233                                 case 'T':
1234                                         size *= 1024ULL;
1235                                         /* FALLTHROUGH */
1236                                 case 'g':
1237                                 case 'G':
1238                                         size *= 1024ULL;
1239                                         /* FALLTHROUGH */
1240                                 case 'm':
1241                                 case 'M':
1242                                         size *= 1024ULL;
1243                                         /* FALLTHROUGH */
1244                                 case 'k':
1245                                 case 'K':
1246                                         size *= 1024ULL;
1247                                         break;
1248                                 case '\0':
1249                                         break;
1250                                 default:
1251                                         warnx("unknown multiplier suffix '%c' for partition %c (should be K, M, G or T)",
1252                                             part_size_type[i], i + 'a');
1253                                         break;
1254                                 }
1255                                 /* don't count %'s yet */
1256                                 if (part_size_type[i] != '%') {
1257                                         /*
1258                                          * for all not in sectors, convert to
1259                                          * sectors
1260                                          */
1261                                         if (part_size_type[i] != '\0') {
1262                                                 if (size % lp->d_secsize != 0)
1263                                                         warnx("partition %c not an integer number of sectors",
1264                                                             i + 'a');
1265                                                 size /= lp->d_secsize;
1266                                                 pp->p_size = size;
1267                                         }
1268                                         /* else already in sectors */
1269                                         if (i != RAW_PART)
1270                                                 total_size += size;
1271                                 }
1272                         }
1273                 }
1274         }
1275
1276         /* Find out the total free space, excluding the boot block area. */
1277         base_offset = BBSIZE / secsize;
1278         free_space = 0;
1279         for (i = 0; i < lp->d_npartitions; i++) {
1280                 pp = &lp->d_partitions[i];
1281                 if (!part_set[i] || i == RAW_PART ||
1282                     part_size_type[i] == '%' || part_size_type[i] == '*')
1283                         continue;
1284                 if (pp->p_offset > base_offset)
1285                         free_space += pp->p_offset - base_offset;
1286                 if (pp->p_offset + pp->p_size > base_offset)
1287                         base_offset = pp->p_offset + pp->p_size;
1288         }
1289         if (base_offset < lp->d_secperunit)
1290                 free_space += lp->d_secperunit - base_offset;
1291
1292         /* handle % partitions - note %'s don't need to add up to 100! */
1293         if (total_percent != 0) {
1294                 if (total_percent > 100) {
1295                         fprintf(stderr,"total percentage %lu is greater than 100\n",
1296                             total_percent);
1297                         errors++;
1298                 }
1299
1300                 if (free_space > 0) {
1301                         for (i = 0; i < lp->d_npartitions; i++) {
1302                                 pp = &lp->d_partitions[i];
1303                                 if (part_set[i] && part_size_type[i] == '%') {
1304                                         /* careful of overflows! and integer roundoff */
1305                                         pp->p_size = ((double)pp->p_size/100) * free_space;
1306                                         total_size += pp->p_size;
1307
1308                                         /* FIX we can lose a sector or so due to roundoff per
1309                                            partition.  A more complex algorithm could avoid that */
1310                                 }
1311                         }
1312                 } else {
1313                         fprintf(stderr,
1314                             "%ld sectors available to give to '*' and '%%' partitions\n",
1315                             free_space);
1316                         errors++;
1317                         /* fix?  set all % partitions to size 0? */
1318                 }
1319         }
1320         /* give anything remaining to the hog partition */
1321         if (hog_part != -1) {
1322                 /*
1323                  * Find the range of offsets usable by '*' partitions around
1324                  * the hog partition and how much space they need.
1325                  */
1326                 needed = 0;
1327                 base_offset = BBSIZE / secsize;
1328                 for (i = hog_part - 1; i >= 0; i--) {
1329                         pp = &lp->d_partitions[i];
1330                         if (!part_set[i] || i == RAW_PART)
1331                                 continue;
1332                         if (part_offset_type[i] == '*') {
1333                                 needed += pp->p_size;
1334                                 continue;
1335                         }
1336                         base_offset = pp->p_offset + pp->p_size;
1337                         break;
1338                 }
1339                 current_offset = lp->d_secperunit;
1340                 for (i = lp->d_npartitions - 1; i > hog_part; i--) {
1341                         pp = &lp->d_partitions[i];
1342                         if (!part_set[i] || i == RAW_PART)
1343                                 continue;
1344                         if (part_offset_type[i] == '*') {
1345                                 needed += pp->p_size;
1346                                 continue;
1347                         }
1348                         current_offset = pp->p_offset;
1349                 }
1350
1351                 if (current_offset - base_offset <= needed) {
1352                         fprintf(stderr, "Cannot find space for partition %c\n",
1353                             hog_part + 'a');
1354                         fprintf(stderr,
1355                             "Need more than %lu sectors between %lu and %lu\n",
1356                             needed, base_offset, current_offset);
1357                         errors++;
1358                         lp->d_partitions[hog_part].p_size = 0;
1359                 } else {
1360                         lp->d_partitions[hog_part].p_size = current_offset -
1361                             base_offset - needed;
1362                         total_size += lp->d_partitions[hog_part].p_size;
1363                 }
1364         }
1365
1366         /* Now set the offsets for each partition */
1367         current_offset = BBSIZE / secsize; /* in sectors */
1368         seen_default_offset = 0;
1369         for (i = 0; i < lp->d_npartitions; i++) {
1370                 part = 'a' + i;
1371                 pp = &lp->d_partitions[i];
1372                 if (part_set[i]) {
1373                         if (part_offset_type[i] == '*') {
1374                                 if (i == RAW_PART) {
1375                                         pp->p_offset = 0;
1376                                 } else {
1377                                         pp->p_offset = current_offset;
1378                                         seen_default_offset = 1;
1379                                 }
1380                         } else {
1381                                 /* allow them to be out of order for old-style tables */
1382                                 if (pp->p_offset < current_offset &&
1383                                     seen_default_offset && i != RAW_PART &&
1384                                     pp->p_fstype != FS_VINUM) {
1385                                         fprintf(stderr,
1386 "Offset %ld for partition %c overlaps previous partition which ends at %lu\n",
1387                                             (long)pp->p_offset,i+'a',current_offset);
1388                                         fprintf(stderr,
1389 "Labels with any *'s for offset must be in ascending order by sector\n");
1390                                         errors++;
1391                                 } else if (pp->p_offset != current_offset &&
1392                                     i != RAW_PART && seen_default_offset) {
1393                                         /*
1394                                          * this may give unneeded warnings if
1395                                          * partitions are out-of-order
1396                                          */
1397                                         warnx(
1398 "Offset %ld for partition %c doesn't match expected value %ld",
1399                                             (long)pp->p_offset, i + 'a', current_offset);
1400                                 }
1401                         }
1402                         if (i != RAW_PART)
1403                                 current_offset = pp->p_offset + pp->p_size;
1404                 }
1405         }
1406
1407         for (i = 0; i < lp->d_npartitions; i++) {
1408                 part = 'a' + i;
1409                 pp = &lp->d_partitions[i];
1410                 if (pp->p_size == 0 && pp->p_offset != 0)
1411                         warnx("partition %c: size 0, but offset %lu",
1412                             part, (u_long)pp->p_offset);
1413 #ifdef notdef
1414                 if (pp->p_size % lp->d_secpercyl)
1415                         warnx("partition %c: size %% cylinder-size != 0",
1416                             part);
1417                 if (pp->p_offset % lp->d_secpercyl)
1418                         warnx("partition %c: offset %% cylinder-size != 0",
1419                             part);
1420 #endif
1421                 if (pp->p_offset > lp->d_secperunit) {
1422                         fprintf(stderr,
1423                             "partition %c: offset past end of unit\n", part);
1424                         errors++;
1425                 }
1426                 if (pp->p_offset + pp->p_size > lp->d_secperunit) {
1427                         fprintf(stderr,
1428                         "partition %c: partition extends past end of unit\n",
1429                             part);
1430                         errors++;
1431                 }
1432                 if (i == RAW_PART) {
1433                         if (pp->p_fstype != FS_UNUSED)
1434                                 warnx("partition %c is not marked as unused!",part);
1435                         if (pp->p_offset != 0)
1436                                 warnx("partition %c doesn't start at 0!",part);
1437                         if (pp->p_size != lp->d_secperunit)
1438                                 warnx("partition %c doesn't cover the whole unit!",part);
1439
1440                         if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) ||
1441                             (pp->p_size != lp->d_secperunit)) {
1442                                 warnx("An incorrect partition %c may cause problems for "
1443                                     "standard system utilities",part);
1444                         }
1445                 }
1446
1447                 /* check for overlaps */
1448                 /* this will check for all possible overlaps once and only once */
1449                 for (j = 0; j < i; j++) {
1450                         pp2 = &lp->d_partitions[j];
1451                         if (j != RAW_PART && i != RAW_PART &&
1452                             pp->p_fstype != FS_VINUM &&
1453                             pp2->p_fstype != FS_VINUM &&
1454                             part_set[i] && part_set[j]) {
1455                                 if (pp2->p_offset < pp->p_offset + pp->p_size &&
1456                                     (pp2->p_offset + pp2->p_size > pp->p_offset ||
1457                                         pp2->p_offset >= pp->p_offset)) {
1458                                         fprintf(stderr,"partitions %c and %c overlap!\n",
1459                                             j + 'a', i + 'a');
1460                                         errors++;
1461                                 }
1462                         }
1463                 }
1464         }
1465         for (; i < lp->d_npartitions; i++) {
1466                 part = 'a' + i;
1467                 pp = &lp->d_partitions[i];
1468                 if (pp->p_size || pp->p_offset)
1469                         warnx("unused partition %c: size %d offset %lu",
1470                             'a' + i, pp->p_size, (u_long)pp->p_offset);
1471         }
1472         return (errors);
1473 }
1474
1475 /*
1476  * When operating on a "virgin" disk, try getting an initial label
1477  * from the associated device driver.  This might work for all device
1478  * drivers that are able to fetch some initial device parameters
1479  * without even having access to a (BSD) disklabel, like SCSI disks,
1480  * most IDE drives, or vn devices.
1481  *
1482  * The device name must be given in its "canonical" form.
1483  */
1484 static struct disklabel *
1485 getvirginlabel(void)
1486 {
1487         static struct disklabel loclab;
1488         struct partition *dp;
1489         int f;
1490         u_int u;
1491
1492         if ((f = open(specname, O_RDONLY)) == -1) {
1493                 warn("cannot open %s", specname);
1494                 return (NULL);
1495         }
1496
1497         if (is_file)
1498                 get_file_parms(f);
1499         else {
1500                 mediasize = g_mediasize(f);
1501                 secsize = g_sectorsize(f);
1502                 if (secsize < 0 || mediasize < 0) {
1503                         close (f);
1504                         return (NULL);
1505                 }
1506         }
1507         memset(&loclab, 0, sizeof loclab);
1508         loclab.d_magic = DISKMAGIC;
1509         loclab.d_magic2 = DISKMAGIC;
1510         loclab.d_secsize = secsize;
1511         loclab.d_secperunit = mediasize / secsize;
1512
1513         /*
1514          * Nobody in these enlightened days uses the CHS geometry for
1515          * anything, but nonetheless try to get it right.  If we fail
1516          * to get any good ideas from the device, construct something
1517          * which is IBM-PC friendly.
1518          */
1519         if (ioctl(f, DIOCGFWSECTORS, &u) == 0)
1520                 loclab.d_nsectors = u;
1521         else
1522                 loclab.d_nsectors = 63;
1523         if (ioctl(f, DIOCGFWHEADS, &u) == 0)
1524                 loclab.d_ntracks = u;
1525         else if (loclab.d_secperunit <= 63*1*1024)
1526                 loclab.d_ntracks = 1;
1527         else if (loclab.d_secperunit <= 63*16*1024)
1528                 loclab.d_ntracks = 16;
1529         else
1530                 loclab.d_ntracks = 255;
1531         loclab.d_secpercyl = loclab.d_ntracks * loclab.d_nsectors;
1532         loclab.d_ncylinders = loclab.d_secperunit / loclab.d_secpercyl;
1533         loclab.d_npartitions = DEFPARTITIONS;
1534
1535         /* Various (unneeded) compat stuff */
1536         loclab.d_rpm = 3600;
1537         loclab.d_bbsize = BBSIZE;
1538         loclab.d_interleave = 1;
1539         strncpy(loclab.d_typename, "amnesiac",
1540             sizeof(loclab.d_typename));
1541
1542         dp = &loclab.d_partitions[RAW_PART];
1543         dp->p_size = loclab.d_secperunit;
1544         loclab.d_checksum = dkcksum(&loclab);
1545         close (f);
1546         return (&loclab);
1547 }
1548
1549 static void
1550 usage(void)
1551 {
1552
1553         fprintf(stderr,
1554         "%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",
1555         "usage: bsdlabel disk",
1556         "\t\t(to read label)",
1557         "       bsdlabel -w [-n] [-m machine] disk [type]",
1558         "\t\t(to write label with existing boot program)",
1559         "       bsdlabel -e [-n] [-m machine] disk",
1560         "\t\t(to edit label)",
1561         "       bsdlabel -R [-n] [-m machine] disk protofile",
1562         "\t\t(to restore label with existing boot program)",
1563         "       bsdlabel -B [-b boot] [-m machine] disk",
1564         "\t\t(to install boot program with existing on-disk label)",
1565         "       bsdlabel -w -B [-n] [-b boot] [-m machine] disk [type]",
1566         "\t\t(to write label and install boot program)",
1567         "       bsdlabel -R -B [-n] [-b boot] [-m machine] disk protofile",
1568                 "\t\t(to restore label and install boot program)"
1569         );
1570         exit(1);
1571 }