xref: /freebsd/sbin/bsdlabel/bsdlabel.c (revision 390e8cc2974df1888369c06339ef8e0e92b312b6)
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 #include <sys/disklabel.h>
67 
68 #include <unistd.h>
69 #include <string.h>
70 #include <stdio.h>
71 #include <libgeom.h>
72 #include <stdlib.h>
73 #include <signal.h>
74 #include <stdarg.h>
75 #include <ctype.h>
76 #include <err.h>
77 #include <errno.h>
78 
79 #include "pathnames.h"
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 static void	makelabel(const char *, struct disklabel *);
91 static int	writelabel(void);
92 static int readlabel(int flag);
93 static void	display(FILE *, const struct disklabel *);
94 static int edit(void);
95 static int	editit(void);
96 static char	*skip(char *);
97 static char	*word(char *);
98 static int	getasciilabel(FILE *, struct disklabel *);
99 static int	getasciipartspec(char *, struct disklabel *, int, int);
100 static int	checklabel(struct disklabel *);
101 static void	usage(void);
102 static struct disklabel *getvirginlabel(void);
103 
104 #define	DEFEDITOR	_PATH_VI
105 
106 static char	*dkname;
107 static char	*specname;
108 static char	tmpfil[] = PATH_TMPFILE;
109 
110 static struct	disklabel lab;
111 static u_char	bootarea[BBSIZE];
112 static off_t	mediasize;
113 static u_int	secsize;
114 static char	blank[] = "";
115 static char	unknown[] = "unknown";
116 
117 #define MAX_PART ('z')
118 #define MAX_NUM_PARTS (1 + MAX_PART - 'a')
119 static char    part_size_type[MAX_NUM_PARTS];
120 static char    part_offset_type[MAX_NUM_PARTS];
121 static int     part_set[MAX_NUM_PARTS];
122 
123 static int	installboot;	/* non-zero if we should install a boot program */
124 static int	allfields;	/* present all fields in edit */
125 static char const *xxboot;	/* primary boot */
126 
127 static off_t mbroffset;
128 static int labelsoffset = LABELSECTOR;
129 static int labeloffset = LABELOFFSET;
130 static int bbsize = BBSIZE;
131 static int alphacksum =
132 #if defined(__alpha__)
133 	1;
134 #else
135 	0;
136 #endif
137 
138 enum	{
139 	UNSPEC, EDIT, READ, RESTORE, WRITE, WRITEBOOT
140 } op = UNSPEC;
141 
142 
143 static int	disable_write;   /* set to disable writing to disk label */
144 
145 int
146 main(int argc, char *argv[])
147 {
148 	FILE *t;
149 	int ch, error = 0;
150 	char const *name = 0;
151 
152 	while ((ch = getopt(argc, argv, "ABb:em:nRrs:w")) != -1)
153 		switch (ch) {
154 			case 'A':
155 				allfields = 1;
156 				break;
157 			case 'B':
158 				++installboot;
159 				break;
160 			case 'b':
161 				xxboot = optarg;
162 				break;
163 			case 'm':
164 				if (!strcmp(optarg, "i386")) {
165 					labelsoffset = 1;
166 					labeloffset = 0;
167 					bbsize = 8192;
168 					alphacksum = 0;
169 				} else if (!strcmp(optarg, "pc98")) {
170 					labelsoffset = 1;
171 					labeloffset = 0;
172 					bbsize = 8192;
173 					alphacksum = 0;
174 				} else if (!strcmp(optarg, "alpha")) {
175 					labelsoffset = 0;
176 					labeloffset = 64;
177 					bbsize = 8192;
178 					alphacksum = 1;
179 				} else if (!strcmp(optarg, "ia64")) {
180 					labelsoffset = 1;
181 					labeloffset = 0;
182 					bbsize = 8192;
183 					alphacksum = 0;
184 				} else {
185 					errx(1, "Unsupported architecture");
186 				}
187 				break;
188 			case 'n':
189 				disable_write = 1;
190 				break;
191 			case 'R':
192 				if (op != UNSPEC)
193 					usage();
194 				op = RESTORE;
195 				break;
196 			case 'e':
197 				if (op != UNSPEC)
198 					usage();
199 				op = EDIT;
200 				break;
201 			case 'r':
202 				/*
203 				 * We accept and ignode -r for compatibility with
204 				 * historically disklabel usage.
205 				 */
206 				break;
207 			case 'w':
208 				if (op != UNSPEC)
209 					usage();
210 				op = WRITE;
211 				break;
212 			case '?':
213 			default:
214 				usage();
215 		}
216 	argc -= optind;
217 	argv += optind;
218 
219 	if (argc < 1)
220 		usage();
221 
222 	/* Figure out the names of the thing we're working on */
223 	if (argv[0][0] != '/') {
224 		dkname = argv[0];
225 		asprintf(&specname, "%s%s", _PATH_DEV, argv[0]);
226 	} else {
227 		dkname = strrchr(argv[0], '/');
228 		dkname++;
229 		specname = argv[0];
230 	}
231 
232 	if (installboot && op == UNSPEC)
233 		op = WRITEBOOT;
234 	else if (op == UNSPEC)
235 		op = READ;
236 
237 	switch(op) {
238 
239 	case UNSPEC:
240 		break;
241 
242 	case EDIT:
243 		if (argc != 1)
244 			usage();
245 		readlabel(1);
246 		error = edit();
247 		break;
248 
249 	case READ:
250 		if (argc != 1)
251 			usage();
252 		readlabel(1);
253 		display(stdout, NULL);
254 		error = checklabel(NULL);
255 		break;
256 
257 	case RESTORE:
258 		if (argc != 2)
259 			usage();
260 		if (!(t = fopen(argv[1], "r")))
261 			err(4, "fopen %s", argv[1]);
262 		readlabel(0);
263 		if (!getasciilabel(t, &lab))
264 			exit(1);
265 		error = writelabel();
266 		break;
267 
268 	case WRITE:
269 		if (argc == 2)
270 			name = argv[1];
271 		else if (argc == 1)
272 			name = "auto";
273 		else
274 			usage();
275 		readlabel(0);
276 		makelabel(name, &lab);
277 		if (checklabel(NULL) == 0)
278 			error = writelabel();
279 		break;
280 
281 	case WRITEBOOT:
282 
283 		readlabel(1);
284 		if (argc == 2)
285 			makelabel(argv[1], &lab);
286 		if (checklabel(NULL) == 0)
287 			error = writelabel();
288 		break;
289 	}
290 	exit(error);
291 }
292 
293 /*
294  * Construct a prototype disklabel from /etc/disktab.
295  */
296 static void
297 makelabel(const char *type, struct disklabel *lp)
298 {
299 	struct disklabel *dp;
300 
301 	if (strcmp(type, "auto") == 0)
302 		dp = getvirginlabel();
303 	else
304 		dp = getdiskbyname(type);
305 	if (dp == NULL)
306 		errx(1, "%s: unknown disk type", type);
307 	*lp = *dp;
308 	bzero(lp->d_packname, sizeof(lp->d_packname));
309 }
310 
311 static void
312 readboot(void)
313 {
314 	int fd, i;
315 	struct stat st;
316 
317 	if (xxboot == NULL)
318 		xxboot = "/boot/boot";
319 	fd = open(xxboot, O_RDONLY);
320 	if (fd < 0)
321 		err(1, "cannot open %s", xxboot);
322 	fstat(fd, &st);
323 	if (alphacksum && st.st_size <= BBSIZE - 512) {
324 		i = read(fd, bootarea + 512, st.st_size);
325 		if (i != st.st_size)
326 			err(1, "read error %s", xxboot);
327 		return;
328 	} else if ((!alphacksum) && st.st_size <= BBSIZE) {
329 		i = read(fd, bootarea, st.st_size);
330 		if (i != st.st_size)
331 			err(1, "read error %s", xxboot);
332 		return;
333 	}
334 	errx(1, "boot code %s is wrong size", xxboot);
335 }
336 
337 static int
338 writelabel(void)
339 {
340 	uint64_t *p, sum;
341 	int i, fd;
342 	struct gctl_req *grq;
343 	char const *errstr;
344 	struct disklabel *lp = &lab;
345 
346 	if (disable_write) {
347 		warnx("write to disk label supressed - label was as follows:");
348 		display(stdout, NULL);
349 		return (0);
350 	}
351 
352 	lp->d_magic = DISKMAGIC;
353 	lp->d_magic2 = DISKMAGIC;
354 	lp->d_checksum = 0;
355 	lp->d_checksum = dkcksum(lp);
356 	if (installboot)
357 		readboot();
358 	for (i = 0; i < lab.d_npartitions; i++)
359 		if (lab.d_partitions[i].p_size)
360 			lab.d_partitions[i].p_offset += mbroffset;
361 	bsd_disklabel_le_enc(bootarea + labeloffset + labelsoffset * secsize,
362 	    lp);
363 	if (alphacksum) {
364 		/* Generate the bootblock checksum for the SRM console.  */
365 		for (p = (uint64_t *)bootarea, i = 0, sum = 0; i < 63; i++)
366 			sum += p[i];
367 		p[63] = sum;
368 	}
369 
370 	fd = open(specname, O_RDWR);
371 	if (fd < 0) {
372 		grq = gctl_get_handle();
373 		gctl_ro_param(grq, "verb", -1, "write label");
374 		gctl_ro_param(grq, "class", -1, "BSD");
375 		gctl_ro_param(grq, "geom", -1, dkname);
376 		gctl_ro_param(grq, "label", 148+16*8,
377 			bootarea + labeloffset + labelsoffset * secsize);
378 		errstr = gctl_issue(grq);
379 		if (errstr != NULL) {
380 			warnx("%s", errstr);
381 			gctl_free(grq);
382 			return(1);
383 		}
384 		gctl_free(grq);
385 		if (installboot) {
386 			grq = gctl_get_handle();
387 			gctl_ro_param(grq, "verb", -1, "write bootcode");
388 			gctl_ro_param(grq, "class", -1, "BSD");
389 			gctl_ro_param(grq, "geom", -1, dkname);
390 			gctl_ro_param(grq, "bootcode", BBSIZE, bootarea);
391 			errstr = gctl_issue(grq);
392 			if (errstr != NULL) {
393 				warnx("%s", errstr);
394 				gctl_free(grq);
395 				return (1);
396 			}
397 			gctl_free(grq);
398 		}
399 	} else {
400 		if (write(fd, bootarea, bbsize) != bbsize) {
401 			warn("write %s", specname);
402 			close (fd);
403 			return (1);
404 		}
405 		close (fd);
406 	}
407 	return (0);
408 }
409 
410 /*
411  * Fetch disklabel for disk.
412  * Use ioctl to get label unless -r flag is given.
413  */
414 static int
415 readlabel(int flag)
416 {
417 	int f, i;
418 	int error;
419 	struct gctl_req *grq;
420 	char const *errstr;
421 
422 	f = open(specname, O_RDONLY);
423 	if (f < 0)
424 		err(1, specname);
425 	/* New world order */
426 	if ((ioctl(f, DIOCGMEDIASIZE, &mediasize) != 0) ||
427 	    (ioctl(f, DIOCGSECTORSIZE, &secsize) != 0)) {
428 		err(4, "cannot get disk geometry");
429 	}
430 	(void)lseek(f, (off_t)0, SEEK_SET);
431 	if (read(f, bootarea, BBSIZE) != BBSIZE)
432 		err(4, "%s read", specname);
433 	close (f);
434 	error = bsd_disklabel_le_dec(
435 	    bootarea + (labeloffset + labelsoffset * secsize),
436 	    &lab, MAXPARTITIONS);
437 	if (flag && error)
438 		errx(1, "%s: no valid label found", specname);
439 
440 	grq = gctl_get_handle();
441 	gctl_ro_param(grq, "verb", -1, "read mbroffset");
442 	gctl_ro_param(grq, "class", -1, "BSD");
443 	gctl_ro_param(grq, "geom", -1, dkname);
444 	gctl_rw_param(grq, "mbroffset", sizeof(mbroffset), &mbroffset);
445 	errstr = gctl_issue(grq);
446 	if (errstr != NULL) {
447 		mbroffset = 0;
448 		gctl_free(grq);
449 		return (error);
450 	}
451 	mbroffset /= lab.d_secsize;
452 	if (lab.d_partitions[RAW_PART].p_offset == mbroffset)
453 		for (i = 0; i < lab.d_npartitions; i++)
454 			if (lab.d_partitions[i].p_size)
455 				lab.d_partitions[i].p_offset -= mbroffset;
456 	return (error);
457 }
458 
459 
460 static void
461 display(FILE *f, const struct disklabel *lp)
462 {
463 	int i, j;
464 	const struct partition *pp;
465 
466 	if (lp == NULL)
467 		lp = &lab;
468 
469 	fprintf(f, "# %s:\n", specname);
470 	if (allfields) {
471 		if (lp->d_type < DKMAXTYPES)
472 			fprintf(f, "type: %s\n", dktypenames[lp->d_type]);
473 		else
474 			fprintf(f, "type: %u\n", lp->d_type);
475 		fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename),
476 			lp->d_typename);
477 		fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname),
478 			lp->d_packname);
479 		fprintf(f, "flags:");
480 		if (lp->d_flags & D_REMOVABLE)
481 			fprintf(f, " removeable");
482 		if (lp->d_flags & D_ECC)
483 			fprintf(f, " ecc");
484 		if (lp->d_flags & D_BADSECT)
485 			fprintf(f, " badsect");
486 		fprintf(f, "\n");
487 		fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize);
488 		fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors);
489 		fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks);
490 		fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl);
491 		fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders);
492 		fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit);
493 		fprintf(f, "rpm: %u\n", lp->d_rpm);
494 		fprintf(f, "interleave: %u\n", lp->d_interleave);
495 		fprintf(f, "trackskew: %u\n", lp->d_trackskew);
496 		fprintf(f, "cylinderskew: %u\n", lp->d_cylskew);
497 		fprintf(f, "headswitch: %lu\t\t# milliseconds\n",
498 		    (u_long)lp->d_headswitch);
499 		fprintf(f, "track-to-track seek: %ld\t# milliseconds\n",
500 		    (u_long)lp->d_trkseek);
501 		fprintf(f, "drivedata: ");
502 		for (i = NDDATA - 1; i >= 0; i--)
503 			if (lp->d_drivedata[i])
504 				break;
505 		if (i < 0)
506 			i = 0;
507 		for (j = 0; j <= i; j++)
508 			fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]);
509 		fprintf(f, "\n\n");
510 	}
511 	fprintf(f, "%u partitions:\n", lp->d_npartitions);
512 	fprintf(f,
513 	    "#        size   offset    fstype   [fsize bsize bps/cpg]\n");
514 	pp = lp->d_partitions;
515 	for (i = 0; i < lp->d_npartitions; i++, pp++) {
516 		if (pp->p_size) {
517 			fprintf(f, "  %c: %8lu %8lu  ", 'a' + i,
518 			   (u_long)pp->p_size, (u_long)pp->p_offset);
519 			if (pp->p_fstype < FSMAXTYPES)
520 				fprintf(f, "%8.8s", fstypenames[pp->p_fstype]);
521 			else
522 				fprintf(f, "%8d", pp->p_fstype);
523 			switch (pp->p_fstype) {
524 
525 			case FS_UNUSED:				/* XXX */
526 				fprintf(f, "    %5lu %5lu %5.5s ",
527 				    (u_long)pp->p_fsize,
528 				    (u_long)(pp->p_fsize * pp->p_frag), "");
529 				break;
530 
531 			case FS_BSDFFS:
532 				fprintf(f, "    %5lu %5lu %5u ",
533 				    (u_long)pp->p_fsize,
534 				    (u_long)(pp->p_fsize * pp->p_frag),
535 				    pp->p_cpg);
536 				break;
537 
538 			case FS_BSDLFS:
539 				fprintf(f, "    %5lu %5lu %5d",
540 				    (u_long)pp->p_fsize,
541 				    (u_long)(pp->p_fsize * pp->p_frag),
542 				    pp->p_cpg);
543 				break;
544 
545 			default:
546 				fprintf(f, "%20.20s", "");
547 				break;
548 			}
549 			if (i == RAW_PART) {
550 				fprintf(f, "  # \"raw\" part, don't edit");
551 			}
552 			fprintf(f, "\n");
553 		}
554 	}
555 	fflush(f);
556 }
557 
558 static int
559 edit(void)
560 {
561 	int c, fd;
562 	struct disklabel label;
563 	FILE *fp;
564 
565 	if ((fd = mkstemp(tmpfil)) == -1 ||
566 	    (fp = fdopen(fd, "w")) == NULL) {
567 		warnx("can't create %s", tmpfil);
568 		return (1);
569 	}
570 	display(fp, NULL);
571 	fclose(fp);
572 	for (;;) {
573 		if (!editit())
574 			break;
575 		fp = fopen(tmpfil, "r");
576 		if (fp == NULL) {
577 			warnx("can't reopen %s for reading", tmpfil);
578 			break;
579 		}
580 		bzero((char *)&label, sizeof(label));
581 		c = getasciilabel(fp, &label);
582 		fclose(fp);
583 		if (c) {
584 			lab = label;
585 			if (writelabel() == 0) {
586 				(void) unlink(tmpfil);
587 				return (0);
588 			}
589 		}
590 		printf("re-edit the label? [y]: ");
591 		fflush(stdout);
592 		c = getchar();
593 		if (c != EOF && c != (int)'\n')
594 			while (getchar() != (int)'\n')
595 				;
596 		if  (c == (int)'n')
597 			break;
598 	}
599 	(void) unlink(tmpfil);
600 	return (1);
601 }
602 
603 static int
604 editit(void)
605 {
606 	int pid, xpid;
607 	int locstat, omask;
608 	const char *ed;
609 
610 	omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
611 	while ((pid = fork()) < 0) {
612 		if (errno == EPROCLIM) {
613 			warnx("you have too many processes");
614 			return(0);
615 		}
616 		if (errno != EAGAIN) {
617 			warn("fork");
618 			return(0);
619 		}
620 		sleep(1);
621 	}
622 	if (pid == 0) {
623 		sigsetmask(omask);
624 		setgid(getgid());
625 		setuid(getuid());
626 		if ((ed = getenv("EDITOR")) == (char *)0)
627 			ed = DEFEDITOR;
628 		execlp(ed, ed, tmpfil, (char *)0);
629 		err(1, "%s", ed);
630 	}
631 	while ((xpid = wait(&locstat)) >= 0)
632 		if (xpid == pid)
633 			break;
634 	sigsetmask(omask);
635 	return(!locstat);
636 }
637 
638 static char *
639 skip(char *cp)
640 {
641 
642 	while (*cp != '\0' && isspace(*cp))
643 		cp++;
644 	if (*cp == '\0' || *cp == '#')
645 		return (NULL);
646 	return (cp);
647 }
648 
649 static char *
650 word(char *cp)
651 {
652 	char c;
653 
654 	while (*cp != '\0' && !isspace(*cp) && *cp != '#')
655 		cp++;
656 	if ((c = *cp) != '\0') {
657 		*cp++ = '\0';
658 		if (c != '#')
659 			return (skip(cp));
660 	}
661 	return (NULL);
662 }
663 
664 /*
665  * Read an ascii label in from fd f,
666  * in the same format as that put out by display(),
667  * and fill in lp.
668  */
669 static int
670 getasciilabel(FILE *f, struct disklabel *lp)
671 {
672 	char *cp;
673 	const char **cpp;
674 	u_int part;
675 	char *tp, line[BUFSIZ];
676 	u_long v;
677 	int lineno = 0, errors = 0;
678 	int i;
679 
680 	makelabel("auto", lp);
681 	bzero(&part_set, sizeof(part_set));
682 	bzero(&part_size_type, sizeof(part_size_type));
683 	bzero(&part_offset_type, sizeof(part_offset_type));
684 	lp->d_bbsize = BBSIZE;				/* XXX */
685 	lp->d_sbsize = 0;				/* XXX */
686 	while (fgets(line, sizeof(line) - 1, f)) {
687 		lineno++;
688 		if ((cp = index(line,'\n')) != 0)
689 			*cp = '\0';
690 		cp = skip(line);
691 		if (cp == NULL)
692 			continue;
693 		tp = index(cp, ':');
694 		if (tp == NULL) {
695 			fprintf(stderr, "line %d: syntax error\n", lineno);
696 			errors++;
697 			continue;
698 		}
699 		*tp++ = '\0', tp = skip(tp);
700 		if (!strcmp(cp, "type")) {
701 			if (tp == NULL)
702 				tp = unknown;
703 			cpp = dktypenames;
704 			for (; cpp < &dktypenames[DKMAXTYPES]; cpp++)
705 				if (*cpp && !strcmp(*cpp, tp)) {
706 					lp->d_type = cpp - dktypenames;
707 					break;
708 				}
709 			if (cpp < &dktypenames[DKMAXTYPES])
710 				continue;
711 			v = strtoul(tp, NULL, 10);
712 			if (v >= DKMAXTYPES)
713 				fprintf(stderr, "line %d:%s %lu\n", lineno,
714 				    "Warning, unknown disk type", v);
715 			lp->d_type = v;
716 			continue;
717 		}
718 		if (!strcmp(cp, "flags")) {
719 			for (v = 0; (cp = tp) && *cp != '\0';) {
720 				tp = word(cp);
721 				if (!strcmp(cp, "removeable"))
722 					v |= D_REMOVABLE;
723 				else if (!strcmp(cp, "ecc"))
724 					v |= D_ECC;
725 				else if (!strcmp(cp, "badsect"))
726 					v |= D_BADSECT;
727 				else {
728 					fprintf(stderr,
729 					    "line %d: %s: bad flag\n",
730 					    lineno, cp);
731 					errors++;
732 				}
733 			}
734 			lp->d_flags = v;
735 			continue;
736 		}
737 		if (!strcmp(cp, "drivedata")) {
738 			for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) {
739 				lp->d_drivedata[i++] = strtoul(cp, NULL, 10);
740 				tp = word(cp);
741 			}
742 			continue;
743 		}
744 		if (sscanf(cp, "%lu partitions", &v) == 1) {
745 			if (v == 0 || v > MAXPARTITIONS) {
746 				fprintf(stderr,
747 				    "line %d: bad # of partitions\n", lineno);
748 				lp->d_npartitions = MAXPARTITIONS;
749 				errors++;
750 			} else
751 				lp->d_npartitions = v;
752 			continue;
753 		}
754 		if (tp == NULL)
755 			tp = blank;
756 		if (!strcmp(cp, "disk")) {
757 			strncpy(lp->d_typename, tp, sizeof (lp->d_typename));
758 			continue;
759 		}
760 		if (!strcmp(cp, "label")) {
761 			strncpy(lp->d_packname, tp, sizeof (lp->d_packname));
762 			continue;
763 		}
764 		if (!strcmp(cp, "bytes/sector")) {
765 			v = strtoul(tp, NULL, 10);
766 			if (v == 0 || (v % DEV_BSIZE) != 0) {
767 				fprintf(stderr,
768 				    "line %d: %s: bad sector size\n",
769 				    lineno, tp);
770 				errors++;
771 			} else
772 				lp->d_secsize = v;
773 			continue;
774 		}
775 		if (!strcmp(cp, "sectors/track")) {
776 			v = strtoul(tp, NULL, 10);
777 #if (ULONG_MAX != 0xffffffffUL)
778 			if (v == 0 || v > 0xffffffff) {
779 #else
780 			if (v == 0) {
781 #endif
782 				fprintf(stderr, "line %d: %s: bad %s\n",
783 				    lineno, tp, cp);
784 				errors++;
785 			} else
786 				lp->d_nsectors = v;
787 			continue;
788 		}
789 		if (!strcmp(cp, "sectors/cylinder")) {
790 			v = strtoul(tp, NULL, 10);
791 			if (v == 0) {
792 				fprintf(stderr, "line %d: %s: bad %s\n",
793 				    lineno, tp, cp);
794 				errors++;
795 			} else
796 				lp->d_secpercyl = v;
797 			continue;
798 		}
799 		if (!strcmp(cp, "tracks/cylinder")) {
800 			v = strtoul(tp, NULL, 10);
801 			if (v == 0) {
802 				fprintf(stderr, "line %d: %s: bad %s\n",
803 				    lineno, tp, cp);
804 				errors++;
805 			} else
806 				lp->d_ntracks = v;
807 			continue;
808 		}
809 		if (!strcmp(cp, "cylinders")) {
810 			v = strtoul(tp, NULL, 10);
811 			if (v == 0) {
812 				fprintf(stderr, "line %d: %s: bad %s\n",
813 				    lineno, tp, cp);
814 				errors++;
815 			} else
816 				lp->d_ncylinders = v;
817 			continue;
818 		}
819 		if (!strcmp(cp, "sectors/unit")) {
820 			v = strtoul(tp, NULL, 10);
821 			if (v == 0) {
822 				fprintf(stderr, "line %d: %s: bad %s\n",
823 				    lineno, tp, cp);
824 				errors++;
825 			} else
826 				lp->d_secperunit = v;
827 			continue;
828 		}
829 		if (!strcmp(cp, "rpm")) {
830 			v = strtoul(tp, NULL, 10);
831 			if (v == 0 || v > USHRT_MAX) {
832 				fprintf(stderr, "line %d: %s: bad %s\n",
833 				    lineno, tp, cp);
834 				errors++;
835 			} else
836 				lp->d_rpm = v;
837 			continue;
838 		}
839 		if (!strcmp(cp, "interleave")) {
840 			v = strtoul(tp, NULL, 10);
841 			if (v == 0 || v > USHRT_MAX) {
842 				fprintf(stderr, "line %d: %s: bad %s\n",
843 				    lineno, tp, cp);
844 				errors++;
845 			} else
846 				lp->d_interleave = v;
847 			continue;
848 		}
849 		if (!strcmp(cp, "trackskew")) {
850 			v = strtoul(tp, NULL, 10);
851 			if (v > USHRT_MAX) {
852 				fprintf(stderr, "line %d: %s: bad %s\n",
853 				    lineno, tp, cp);
854 				errors++;
855 			} else
856 				lp->d_trackskew = v;
857 			continue;
858 		}
859 		if (!strcmp(cp, "cylinderskew")) {
860 			v = strtoul(tp, NULL, 10);
861 			if (v > USHRT_MAX) {
862 				fprintf(stderr, "line %d: %s: bad %s\n",
863 				    lineno, tp, cp);
864 				errors++;
865 			} else
866 				lp->d_cylskew = v;
867 			continue;
868 		}
869 		if (!strcmp(cp, "headswitch")) {
870 			v = strtoul(tp, NULL, 10);
871 			lp->d_headswitch = v;
872 			continue;
873 		}
874 		if (!strcmp(cp, "track-to-track seek")) {
875 			v = strtoul(tp, NULL, 10);
876 			lp->d_trkseek = v;
877 			continue;
878 		}
879 		/* the ':' was removed above */
880 		if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') {
881 			fprintf(stderr,
882 			    "line %d: %s: Unknown disklabel field\n", lineno,
883 			    cp);
884 			errors++;
885 			continue;
886 		}
887 
888 		/* Process a partition specification line. */
889 		part = *cp - 'a';
890 		if (part >= lp->d_npartitions) {
891 			fprintf(stderr,
892 			    "line %d: partition name out of range a-%c: %s\n",
893 			    lineno, 'a' + lp->d_npartitions - 1, cp);
894 			errors++;
895 			continue;
896 		}
897 		part_set[part] = 1;
898 
899 		if (getasciipartspec(tp, lp, part, lineno) != 0) {
900 			errors++;
901 			break;
902 		}
903 	}
904 	errors += checklabel(lp);
905 	return (errors == 0);
906 }
907 
908 #define NXTNUM(n) do { \
909 	if (tp == NULL) { \
910 		fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
911 		return (1); \
912 	} else { \
913 		cp = tp, tp = word(cp); \
914 		(n) = strtoul(cp, NULL, 10); \
915 	} \
916 } while (0)
917 
918 /* retain 1 character following number */
919 #define NXTWORD(w,n) do { \
920 	if (tp == NULL) { \
921 		fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
922 		return (1); \
923 	} else { \
924 	        char *tmp; \
925 		cp = tp, tp = word(cp); \
926 	        (n) = strtoul(cp, &tmp, 10); \
927 		if (tmp) (w) = *tmp; \
928 	} \
929 } while (0)
930 
931 /*
932  * Read a partition line into partition `part' in the specified disklabel.
933  * Return 0 on success, 1 on failure.
934  */
935 static int
936 getasciipartspec(char *tp, struct disklabel *lp, int part, int lineno)
937 {
938 	struct partition *pp;
939 	char *cp;
940 	const char **cpp;
941 	u_long v;
942 
943 	pp = &lp->d_partitions[part];
944 	cp = NULL;
945 
946 	v = 0;
947 	NXTWORD(part_size_type[part],v);
948 	if (v == 0 && part_size_type[part] != '*') {
949 		fprintf(stderr,
950 		    "line %d: %s: bad partition size\n", lineno, cp);
951 		return (1);
952 	}
953 	pp->p_size = v;
954 
955 	v = 0;
956 	NXTWORD(part_offset_type[part],v);
957 	if (v == 0 && part_offset_type[part] != '*' &&
958 	    part_offset_type[part] != '\0') {
959 		fprintf(stderr,
960 		    "line %d: %s: bad partition offset\n", lineno, cp);
961 		return (1);
962 	}
963 	pp->p_offset = v;
964 	if (tp == NULL) {
965 		fprintf(stderr, "line %d: missing file system type\n", lineno);
966 		return (1);
967 	}
968 	cp = tp, tp = word(cp);
969 	for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++)
970 		if (*cpp && !strcmp(*cpp, cp))
971 			break;
972 	if (*cpp != NULL) {
973 		pp->p_fstype = cpp - fstypenames;
974 	} else {
975 		if (isdigit(*cp))
976 			v = strtoul(cp, NULL, 10);
977 		else
978 			v = FSMAXTYPES;
979 		if (v >= FSMAXTYPES) {
980 			fprintf(stderr,
981 			    "line %d: Warning, unknown file system type %s\n",
982 			    lineno, cp);
983 			v = FS_UNUSED;
984 		}
985 		pp->p_fstype = v;
986 	}
987 
988 	switch (pp->p_fstype) {
989 	case FS_UNUSED:
990 		/*
991 		 * allow us to accept defaults for
992 		 * fsize/frag/cpg
993 		 */
994 		if (tp) {
995 			NXTNUM(pp->p_fsize);
996 			if (pp->p_fsize == 0)
997 				break;
998 			NXTNUM(v);
999 			pp->p_frag = v / pp->p_fsize;
1000 		}
1001 		/* else default to 0's */
1002 		break;
1003 
1004 	/* These happen to be the same */
1005 	case FS_BSDFFS:
1006 	case FS_BSDLFS:
1007 		if (tp) {
1008 			NXTNUM(pp->p_fsize);
1009 			if (pp->p_fsize == 0)
1010 				break;
1011 			NXTNUM(v);
1012 			pp->p_frag = v / pp->p_fsize;
1013 			NXTNUM(pp->p_cpg);
1014 		} else {
1015 			/*
1016 			 * FIX! poor attempt at adaptive
1017 			 */
1018 			/* 1 GB */
1019 			if (pp->p_size < 1024*1024*1024 / lp->d_secsize) {
1020 				/*
1021 				 * FIX! These are too low, but are traditional
1022 				 */
1023 				pp->p_fsize = DEFAULT_NEWFS_FRAG;
1024 				pp->p_frag = DEFAULT_NEWFS_BLOCK /
1025 				    DEFAULT_NEWFS_FRAG;
1026 				pp->p_cpg = DEFAULT_NEWFS_CPG;
1027 			} else {
1028 				pp->p_fsize = BIG_NEWFS_FRAG;
1029 				pp->p_frag = BIG_NEWFS_BLOCK /
1030 				    BIG_NEWFS_FRAG;
1031 				pp->p_cpg = BIG_NEWFS_CPG;
1032 			}
1033 		}
1034 	default:
1035 		break;
1036 	}
1037 	return (0);
1038 }
1039 
1040 /*
1041  * Check disklabel for errors and fill in
1042  * derived fields according to supplied values.
1043  */
1044 static int
1045 checklabel(struct disklabel *lp)
1046 {
1047 	struct partition *pp;
1048 	int i, errors = 0;
1049 	char part;
1050 	u_long total_size, total_percent, current_offset;
1051 	int seen_default_offset;
1052 	int hog_part;
1053 	int j;
1054 	struct partition *pp2;
1055 
1056 	if (lp == NULL)
1057 		lp = &lab;
1058 
1059 	if (allfields) {
1060 
1061 		if (lp->d_secsize == 0) {
1062 			fprintf(stderr, "sector size 0\n");
1063 			return (1);
1064 		}
1065 		if (lp->d_nsectors == 0) {
1066 			fprintf(stderr, "sectors/track 0\n");
1067 			return (1);
1068 		}
1069 		if (lp->d_ntracks == 0) {
1070 			fprintf(stderr, "tracks/cylinder 0\n");
1071 			return (1);
1072 		}
1073 		if  (lp->d_ncylinders == 0) {
1074 			fprintf(stderr, "cylinders/unit 0\n");
1075 			errors++;
1076 		}
1077 		if (lp->d_rpm == 0)
1078 			warnx("revolutions/minute 0");
1079 		if (lp->d_secpercyl == 0)
1080 			lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
1081 		if (lp->d_secperunit == 0)
1082 			lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
1083 		if (lp->d_bbsize == 0) {
1084 			fprintf(stderr, "boot block size 0\n");
1085 			errors++;
1086 		} else if (lp->d_bbsize % lp->d_secsize)
1087 			warnx("boot block size %% sector-size != 0");
1088 		if (lp->d_npartitions > MAXPARTITIONS)
1089 			warnx("number of partitions (%lu) > MAXPARTITIONS (%d)",
1090 			    (u_long)lp->d_npartitions, MAXPARTITIONS);
1091 	} else {
1092 		struct disklabel *vl;
1093 
1094 		vl = getvirginlabel();
1095 		lp->d_secsize = vl->d_secsize;
1096 		lp->d_nsectors = vl->d_nsectors;
1097 		lp->d_ntracks = vl->d_ntracks;
1098 		lp->d_ncylinders = vl->d_ncylinders;
1099 		lp->d_rpm = vl->d_rpm;
1100 		lp->d_interleave = vl->d_interleave;
1101 		lp->d_secpercyl = vl->d_secpercyl;
1102 		lp->d_secperunit = vl->d_secperunit;
1103 		lp->d_bbsize = vl->d_bbsize;
1104 		lp->d_npartitions = vl->d_npartitions;
1105 	}
1106 
1107 
1108 	/* first allocate space to the partitions, then offsets */
1109 	total_size = 0; /* in sectors */
1110 	total_percent = 0; /* in percent */
1111 	hog_part = -1;
1112 	/* find all fixed partitions */
1113 	for (i = 0; i < lp->d_npartitions; i++) {
1114 		pp = &lp->d_partitions[i];
1115 		if (part_set[i]) {
1116 			if (part_size_type[i] == '*') {
1117 				if (i == RAW_PART) {
1118 					pp->p_size = lp->d_secperunit;
1119 				} else {
1120 					if (hog_part != -1)
1121 						warnx("Too many '*' partitions (%c and %c)",
1122 						    hog_part + 'a',i + 'a');
1123 					else
1124 						hog_part = i;
1125 				}
1126 			} else {
1127 				off_t size;
1128 
1129 				size = pp->p_size;
1130 				switch (part_size_type[i]) {
1131 				case '%':
1132 					total_percent += size;
1133 					break;
1134 				case 'k':
1135 				case 'K':
1136 					size *= 1024ULL;
1137 					break;
1138 				case 'm':
1139 				case 'M':
1140 					size *= 1024ULL * 1024ULL;
1141 					break;
1142 				case 'g':
1143 				case 'G':
1144 					size *= 1024ULL * 1024ULL * 1024ULL;
1145 					break;
1146 				case '\0':
1147 					break;
1148 				default:
1149 					warnx("unknown size specifier '%c' (K/M/G are valid)",part_size_type[i]);
1150 					break;
1151 				}
1152 				/* don't count %'s yet */
1153 				if (part_size_type[i] != '%') {
1154 					/*
1155 					 * for all not in sectors, convert to
1156 					 * sectors
1157 					 */
1158 					if (part_size_type[i] != '\0') {
1159 						if (size % lp->d_secsize != 0)
1160 							warnx("partition %c not an integer number of sectors",
1161 							    i + 'a');
1162 						size /= lp->d_secsize;
1163 						pp->p_size = size;
1164 					}
1165 					/* else already in sectors */
1166 					if (i != RAW_PART)
1167 						total_size += size;
1168 				}
1169 			}
1170 		}
1171 	}
1172 	/* handle % partitions - note %'s don't need to add up to 100! */
1173 	if (total_percent != 0) {
1174 		long free_space = lp->d_secperunit - total_size;
1175 		if (total_percent > 100) {
1176 			fprintf(stderr,"total percentage %lu is greater than 100\n",
1177 			    total_percent);
1178 			errors++;
1179 		}
1180 
1181 		if (free_space > 0) {
1182 			for (i = 0; i < lp->d_npartitions; i++) {
1183 				pp = &lp->d_partitions[i];
1184 				if (part_set[i] && part_size_type[i] == '%') {
1185 					/* careful of overflows! and integer roundoff */
1186 					pp->p_size = ((double)pp->p_size/100) * free_space;
1187 					total_size += pp->p_size;
1188 
1189 					/* FIX we can lose a sector or so due to roundoff per
1190 					   partition.  A more complex algorithm could avoid that */
1191 				}
1192 			}
1193 		} else {
1194 			fprintf(stderr,
1195 			    "%ld sectors available to give to '*' and '%%' partitions\n",
1196 			    free_space);
1197 			errors++;
1198 			/* fix?  set all % partitions to size 0? */
1199 		}
1200 	}
1201 	/* give anything remaining to the hog partition */
1202 	if (hog_part != -1) {
1203 		lp->d_partitions[hog_part].p_size = lp->d_secperunit - total_size;
1204 		total_size = lp->d_secperunit;
1205 	}
1206 
1207 	/* Now set the offsets for each partition */
1208 	current_offset = 0; /* in sectors */
1209 	seen_default_offset = 0;
1210 	for (i = 0; i < lp->d_npartitions; i++) {
1211 		part = 'a' + i;
1212 		pp = &lp->d_partitions[i];
1213 		if (part_set[i]) {
1214 			if (part_offset_type[i] == '*') {
1215 				if (i == RAW_PART) {
1216 					pp->p_offset = 0;
1217 				} else {
1218 					pp->p_offset = current_offset;
1219 					seen_default_offset = 1;
1220 				}
1221 			} else {
1222 				/* allow them to be out of order for old-style tables */
1223 				if (pp->p_offset < current_offset &&
1224 				    seen_default_offset && i != RAW_PART &&
1225 				    pp->p_fstype != FS_VINUM) {
1226 					fprintf(stderr,
1227 "Offset %ld for partition %c overlaps previous partition which ends at %lu\n",
1228 					    (long)pp->p_offset,i+'a',current_offset);
1229 					fprintf(stderr,
1230 "Labels with any *'s for offset must be in ascending order by sector\n");
1231 					errors++;
1232 				} else if (pp->p_offset != current_offset &&
1233 				    i != RAW_PART && seen_default_offset) {
1234 					/*
1235 					 * this may give unneeded warnings if
1236 					 * partitions are out-of-order
1237 					 */
1238 					warnx(
1239 "Offset %ld for partition %c doesn't match expected value %ld",
1240 					    (long)pp->p_offset, i + 'a', current_offset);
1241 				}
1242 			}
1243 			if (i != RAW_PART)
1244 				current_offset = pp->p_offset + pp->p_size;
1245 		}
1246 	}
1247 
1248 	for (i = 0; i < lp->d_npartitions; i++) {
1249 		part = 'a' + i;
1250 		pp = &lp->d_partitions[i];
1251 		if (pp->p_size == 0 && pp->p_offset != 0)
1252 			warnx("partition %c: size 0, but offset %lu",
1253 			    part, (u_long)pp->p_offset);
1254 #ifdef notdef
1255 		if (pp->p_size % lp->d_secpercyl)
1256 			warnx("partition %c: size %% cylinder-size != 0",
1257 			    part);
1258 		if (pp->p_offset % lp->d_secpercyl)
1259 			warnx("partition %c: offset %% cylinder-size != 0",
1260 			    part);
1261 #endif
1262 		if (pp->p_offset > lp->d_secperunit) {
1263 			fprintf(stderr,
1264 			    "partition %c: offset past end of unit\n", part);
1265 			errors++;
1266 		}
1267 		if (pp->p_offset + pp->p_size > lp->d_secperunit) {
1268 			fprintf(stderr,
1269 			"partition %c: partition extends past end of unit\n",
1270 			    part);
1271 			errors++;
1272 		}
1273 		if (i == RAW_PART) {
1274 			if (pp->p_fstype != FS_UNUSED)
1275 				warnx("partition %c is not marked as unused!",part);
1276 			if (pp->p_offset != 0)
1277 				warnx("partition %c doesn't start at 0!",part);
1278 			if (pp->p_size != lp->d_secperunit)
1279 				warnx("partition %c doesn't cover the whole unit!",part);
1280 
1281 			if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) ||
1282 			    (pp->p_size != lp->d_secperunit)) {
1283 				warnx("An incorrect partition %c may cause problems for "
1284 				    "standard system utilities",part);
1285 			}
1286 		}
1287 
1288 		/* check for overlaps */
1289 		/* this will check for all possible overlaps once and only once */
1290 		for (j = 0; j < i; j++) {
1291 			pp2 = &lp->d_partitions[j];
1292 			if (j != RAW_PART && i != RAW_PART &&
1293 			    pp->p_fstype != FS_VINUM &&
1294 			    pp2->p_fstype != FS_VINUM &&
1295 			    part_set[i] && part_set[j]) {
1296 				if (pp2->p_offset < pp->p_offset + pp->p_size &&
1297 				    (pp2->p_offset + pp2->p_size > pp->p_offset ||
1298 					pp2->p_offset >= pp->p_offset)) {
1299 					fprintf(stderr,"partitions %c and %c overlap!\n",
1300 					    j + 'a', i + 'a');
1301 					errors++;
1302 				}
1303 			}
1304 		}
1305 	}
1306 	for (; i < MAXPARTITIONS; i++) {
1307 		part = 'a' + i;
1308 		pp = &lp->d_partitions[i];
1309 		if (pp->p_size || pp->p_offset)
1310 			warnx("unused partition %c: size %d offset %lu",
1311 			    'a' + i, pp->p_size, (u_long)pp->p_offset);
1312 	}
1313 	return (errors);
1314 }
1315 
1316 /*
1317  * When operating on a "virgin" disk, try getting an initial label
1318  * from the associated device driver.  This might work for all device
1319  * drivers that are able to fetch some initial device parameters
1320  * without even having access to a (BSD) disklabel, like SCSI disks,
1321  * most IDE drives, or vn devices.
1322  *
1323  * The device name must be given in its "canonical" form.
1324  */
1325 static struct disklabel *
1326 getvirginlabel(void)
1327 {
1328 	static struct disklabel loclab;
1329 	struct partition *dp;
1330 	int f;
1331 	u_int u;
1332 
1333 	if ((f = open(specname, O_RDONLY)) == -1) {
1334 		warn("cannot open %s", specname);
1335 		return (NULL);
1336 	}
1337 
1338 	/* New world order */
1339 	if ((ioctl(f, DIOCGMEDIASIZE, &mediasize) != 0) ||
1340 	    (ioctl(f, DIOCGSECTORSIZE, &secsize) != 0)) {
1341 		close (f);
1342 		return (NULL);
1343 	}
1344 	memset(&loclab, 0, sizeof loclab);
1345 	loclab.d_magic = DISKMAGIC;
1346 	loclab.d_magic2 = DISKMAGIC;
1347 	loclab.d_secsize = secsize;
1348 	loclab.d_secperunit = mediasize / secsize;
1349 
1350 	/*
1351 	 * Nobody in these enligthened days uses the CHS geometry for
1352 	 * anything, but nontheless try to get it right.  If we fail
1353 	 * to get any good ideas from the device, construct something
1354 	 * which is IBM-PC friendly.
1355 	 */
1356 	if (ioctl(f, DIOCGFWSECTORS, &u) == 0)
1357 		loclab.d_nsectors = u;
1358 	else
1359 		loclab.d_nsectors = 63;
1360 	if (ioctl(f, DIOCGFWHEADS, &u) == 0)
1361 		loclab.d_ntracks = u;
1362 	else if (loclab.d_secperunit <= 63*1*1024)
1363 		loclab.d_ntracks = 1;
1364 	else if (loclab.d_secperunit <= 63*16*1024)
1365 		loclab.d_ntracks = 16;
1366 	else
1367 		loclab.d_ntracks = 255;
1368 	loclab.d_secpercyl = loclab.d_ntracks * loclab.d_nsectors;
1369 	loclab.d_ncylinders = loclab.d_secperunit / loclab.d_secpercyl;
1370 	loclab.d_npartitions = MAXPARTITIONS;
1371 
1372 	/* Various (unneeded) compat stuff */
1373 	loclab.d_rpm = 3600;
1374 	loclab.d_bbsize = BBSIZE;
1375 	loclab.d_interleave = 1;
1376 	strncpy(loclab.d_typename, "amnesiac",
1377 	    sizeof(loclab.d_typename));
1378 
1379 	dp = &loclab.d_partitions[RAW_PART];
1380 	dp->p_size = loclab.d_secperunit;
1381 	loclab.d_checksum = dkcksum(&loclab);
1382 	close (f);
1383 	return (&loclab);
1384 }
1385 
1386 static void
1387 usage(void)
1388 {
1389 
1390 	fprintf(stderr,
1391 	"%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",
1392 	"usage: bsdlabel disk",
1393 	"\t\t(to read label)",
1394 	"	bsdlabel -w [-n] [-m machine] disk [type]",
1395 	"\t\t(to write label with existing boot program)",
1396 	"	bsdlabel -e [-n] [-m machine] disk",
1397 	"\t\t(to edit label)",
1398 	"	bsdlabel -R [-n] [-m machine] disk protofile",
1399 	"\t\t(to restore label with existing boot program)",
1400 	"	bsdlabel -B [-b boot] [-m machine] disk",
1401 	"\t\t(to install boot program with existing on-disk label)",
1402 	"	bsdlabel -w -B [-n] [-b boot] [-m machine] disk [type]",
1403 	"\t\t(to write label and install boot program)",
1404 	"	bsdlabel -R -B [-n] [-b boot] [-m machine] disk protofile",
1405 		"\t\t(to restore label and install boot program)"
1406 	);
1407 	exit(1);
1408 }
1409