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