xref: /netbsd-src/usr.sbin/sunlabel/sunlabel.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /* $NetBSD: sunlabel.c,v 1.23 2009/04/18 14:06:58 lukem Exp $ */
2 
3 /*-
4  * Copyright (c) 2002 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by der Mouse.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #endif
35 
36 #include <sys/cdefs.h>
37 #if defined(__RCSID) && !defined(lint)
38 __RCSID("$NetBSD: sunlabel.c,v 1.23 2009/04/18 14:06:58 lukem Exp $");
39 #endif
40 
41 #include <stdio.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <ctype.h>
45 #include <stdlib.h>
46 #include <unistd.h>
47 #ifndef NO_TERMCAP_WIDTH
48 #include <termcap.h>
49 #endif
50 #include <string.h>
51 #include <strings.h>
52 #include <inttypes.h>
53 #include <err.h>
54 
55 #include <sys/ioctl.h>
56 
57 /* If neither S_COMMAND nor NO_S_COMMAND is defined, guess. */
58 #if !defined(S_COMMAND) && !defined(NO_S_COMMAND)
59 #define S_COMMAND
60 #include <util.h>
61 #include <sys/disklabel.h>
62 #endif
63 
64 /*
65  * NPART is the total number of partitions.  This must be <= 43, given the
66  * amount of space available to store extended partitions. It also must be
67  * <=26, given the use of single letters to name partitions.  The 8 is the
68  * number of `standard' partitions; this arguably should be a #define, since
69  * it occurs not only here but scattered throughout the code.
70  */
71 #define NPART 16
72 #define NXPART (NPART - 8)
73 #define PARTLETTER(i) ((i) + 'a')
74 #define LETTERPART(i) ((i) - 'a')
75 
76 /*
77  * A partition.  We keep redundant information around, making sure
78  * that whenever we change one, we keep another constant and update
79  * the third.  Which one is which depends.  Arguably a partition
80  * should also know its partition number; here, if we need that we
81  * cheat, using (effectively) ptr-&label.partitions[0].
82  */
83 struct part {
84 	uint32_t    startcyl;
85 	uint32_t    nblk;
86 	uint32_t    endcyl;
87 };
88 
89 /*
90  * A label.  As the embedded comments indicate, much of this structure
91  * corresponds directly to Sun's struct dk_label.  Some of the values
92  * here are historical holdovers.  Apparently really old Suns did
93  * their own sparing in software, so a sector or two per cylinder,
94  * plus a whole cylinder or two at the end, got set aside as spares.
95  * acyl and apc count those spares, and this is also why ncyl and pcyl
96  * both exist.  These days the spares generally are hidden from the
97  * host by the disk, and there's no reason not to set
98  * ncyl=pcyl=ceil(device size/spc) and acyl=apc=0.
99  *
100  * Note also that the geometry assumptions behind having nhead and
101  * nsect assume that the sect/trk and trk/cyl values are constant
102  * across the whole drive.  The latter is still usually true; the
103  * former isn't.  In my experience, you can just put fixed values
104  * here; the basis for software knowing the drive geometry is also
105  * mostly invalid these days anyway.  (I just use nhead=32 nsect=64,
106  * which gives me 1M "cylinders", a convenient size.)
107  */
108 struct label {
109 	/* BEGIN fields taken directly from struct dk_label */
110 	char asciilabel[128];
111 	uint32_t rpm;	/* Spindle rotation speed - useless now */
112 	uint32_t pcyl;	/* Physical cylinders */
113 	uint32_t apc;	/* Alternative sectors per cylinder */
114 	uint32_t obs1;	/* Obsolete? */
115 	uint32_t obs2;	/* Obsolete? */
116 	uint32_t intrlv;	/* Interleave - never anything but 1 IME */
117 	uint32_t ncyl;	/* Number of usable cylinders */
118 	uint32_t acyl;	/* Alternative cylinders - pcyl minus ncyl */
119 	uint32_t nhead;	/* Tracks-per-cylinder (usually # of heads) */
120 	uint32_t nsect;	/* Sectors-per-track */
121 	uint32_t obs3;	/* Obsolete? */
122 	uint32_t obs4;	/* Obsolete? */
123 	/* END fields taken directly from struct dk_label */
124 	uint32_t spc;	/* Sectors per cylinder - nhead*nsect */
125 	uint32_t dirty:1;/* Modified since last read */
126 	struct part partitions[NPART];/* The partitions themselves */
127 };
128 
129 /*
130  * Describes a field in the label.
131  *
132  * tag is a short name for the field, like "apc" or "nsect".  loc is a
133  * pointer to the place in the label where it's stored.  print is a
134  * function to print the value; the second argument is the current
135  * column number, and the return value is the new current column
136  * number.  (This allows print functions to do proper line wrapping.)
137  * chval is called to change a field; the first argument is the
138  * command line portion that contains the new value (in text form).
139  * The chval function is responsible for parsing and error-checking as
140  * well as doing the modification.  changed is a function which does
141  * field-specific actions necessary when the field has been changed.
142  * This could be rolled into the chval function, but I believe this
143  * way provides better code sharing.
144  *
145  * Note that while the fields in the label vary in size (8, 16, or 32
146  * bits), we store everything as ints in the label struct, above, and
147  * convert when packing and unpacking.  This allows us to have only
148  * one numeric chval function.
149  */
150 struct field {
151 	const char *tag;
152 	void *loc;
153 	int (*print)(struct field *, int);
154 	void (*chval)(const char *, struct field *);
155 	void (*changed)(void);
156 	int taglen;
157 };
158 
159 /* LABEL_MAGIC was chosen by Sun and cannot be trivially changed. */
160 #define LABEL_MAGIC 0xdabe
161 /*
162  * LABEL_XMAGIC needs to agree between here and any other code that uses
163  * extended partitions (mainly the kernel).
164  */
165 #define LABEL_XMAGIC (0x199d1fe2+8)
166 
167 static int diskfd;			/* fd on the disk */
168 static const char *diskname;		/* name of the disk, for messages */
169 static int readonly;			/* true iff it's open RO */
170 static unsigned char labelbuf[512];	/* Buffer holding the label sector */
171 static struct label label;		/* The label itself. */
172 static int fixmagic;			/* -m, ignore bad magic #s */
173 static int fixcksum;			/* -s, ignore bad cksums */
174 static int newlabel;			/* -n, ignore all on-disk values */
175 static int quiet;			/* -q, don't print chatter */
176 
177 /*
178  * The various functions that go in the field function pointers.  The
179  * _ascii functions are for 128-byte string fields (the ASCII label);
180  * the _int functions are for int-valued fields (everything else).
181  * update_spc is a `changed' function for updating the spc value when
182  * changing one of the two values that make it up.
183  */
184 static int print_ascii(struct field *, int);
185 static void chval_ascii(const char *, struct field *);
186 static int print_int(struct field *, int);
187 static void chval_int(const char *, struct field *);
188 static void update_spc(void);
189 
190 int  main(int, char **);
191 
192 /* The fields themselves. */
193 static struct field fields[] =
194 {
195 	{"ascii", &label.asciilabel[0], print_ascii, chval_ascii, 0, 0 },
196 	{"rpm", &label.rpm, print_int, chval_int, 0, 0 },
197 	{"pcyl", &label.pcyl, print_int, chval_int, 0, 0 },
198 	{"apc", &label.apc, print_int, chval_int, 0, 0 },
199 	{"obs1", &label.obs1, print_int, chval_int, 0, 0 },
200 	{"obs2", &label.obs2, print_int, chval_int, 0, 0 },
201 	{"intrlv", &label.intrlv, print_int, chval_int, 0, 0 },
202 	{"ncyl", &label.ncyl, print_int, chval_int, 0, 0 },
203 	{"acyl", &label.acyl, print_int, chval_int, 0, 0 },
204 	{"nhead", &label.nhead, print_int, chval_int, update_spc, 0 },
205 	{"nsect", &label.nsect, print_int, chval_int, update_spc, 0 },
206 	{"obs3", &label.obs3, print_int, chval_int, 0, 0 },
207 	{"obs4", &label.obs4, print_int, chval_int, 0, 0 },
208 	{NULL, NULL, NULL, NULL, 0, 0 }
209 };
210 
211 /*
212  * We'd _like_ to use howmany() from the include files, but can't count
213  *  on its being present or working.
214  */
215 static inline uint32_t how_many(uint32_t amt, uint32_t unit)
216     __attribute__((const));
217 static inline uint32_t
218 how_many(uint32_t amt, uint32_t unit)
219 {
220 	return ((amt + unit - 1) / unit);
221 }
222 
223 /*
224  * Try opening the disk, given a name.  If mustsucceed is true, we
225  *  "cannot fail"; failures produce gripe-and-exit, and if we return,
226  *  our return value is 1.  Otherwise, we return 1 on success and 0 on
227  *  failure.
228  */
229 static int
230 trydisk(const char *s, int mustsucceed)
231 {
232 	int ro = 0;
233 
234 	diskname = s;
235 	if ((diskfd = open(s, O_RDWR)) == -1 ||
236 	    (diskfd = open(s, O_RDWR | O_NONBLOCK)) == -1) {
237 		if ((diskfd = open(s, O_RDONLY)) == -1) {
238 			if (mustsucceed)
239 				err(1, "Cannot open `%s'", s);
240 			else
241 				return 0;
242 		}
243 		ro = 1;
244 	}
245 	if (ro && !quiet)
246 		warnx("No write access, label is readonly");
247 	readonly = ro;
248 	return 1;
249 }
250 
251 /*
252  * Set the disk device, given the user-supplied string.  Note that even
253  * if we malloc, we never free, because either trydisk eventually
254  * succeeds, in which case the string is saved in diskname, or it
255  * fails, in which case we exit and freeing is irrelevant.
256  */
257 static void
258 setdisk(const char *s)
259 {
260 	char *tmp;
261 
262 	if (strchr(s, '/')) {
263 		trydisk(s, 1);
264 		return;
265 	}
266 	if (trydisk(s, 0))
267 		return;
268 #ifndef DISTRIB /* native tool: search in /dev */
269 	asprintf(&tmp, "/dev/%s", s);
270 	if (!tmp)
271 		err(1, "malloc");
272 	if (trydisk(tmp, 0)) {
273 		free(tmp);
274 		return;
275 	}
276 	free(tmp);
277 	asprintf(&tmp, "/dev/%s%c", s, getrawpartition() + 'a');
278 	if (!tmp)
279 		err(1, "malloc");
280 	if (trydisk(tmp, 0)) {
281 		free(tmp);
282 		return;
283 	}
284 #endif
285 	errx(1, "Can't find device for disk `%s'", s);
286 }
287 
288 static void usage(void) __dead;
289 static void
290 usage(void)
291 {
292 	(void)fprintf(stderr, "usage: %s [-mnqs] disk\n", getprogname());
293 	exit(1);
294 }
295 
296 /*
297  * Command-line arguments.  We can have at most one non-flag
298  *  argument, which is the disk name; we can also have flags
299  *
300  *	-m
301  *		Turns on fixmagic, which causes bad magic numbers to be
302  *		ignored (though a complaint is still printed), rather
303  *		than being fatal errors.
304  *
305  *	-s
306  *		Turns on fixcksum, which causes bad checksums to be
307  *		ignored (though a complaint is still printed), rather
308  *		than being fatal errors.
309  *
310  *	-n
311  *		Turns on newlabel, which means we're creating a new
312  *		label and anything in the label sector should be
313  *		ignored.  This is a bit like -m -s, except that it
314  *		doesn't print complaints and it ignores possible
315  *		garbage on-disk.
316  *
317  *	-q
318  *		Turns on quiet, which suppresses printing of prompts
319  *		and other irrelevant chatter.  If you're trying to use
320  *		sunlabel in an automated way, you probably want this.
321  */
322 static void
323 handleargs(int ac, char **av)
324 {
325 	int c;
326 
327 	while ((c = getopt(ac, av, "mnqs")) != -1) {
328 		switch (c) {
329 		case 'm':
330 			fixmagic++;
331 			break;
332 		case 'n':
333 			newlabel++;
334 			break;
335 		case 'q':
336 			quiet++;
337 			break;
338 		case 's':
339 			fixcksum++;
340 			break;
341 		case '?':
342 			warnx("Illegal option `%c'", c);
343 			usage();
344 		}
345 	}
346 	ac -= optind;
347 	av += optind;
348 	if (ac != 1)
349 		usage();
350 	setdisk(av[0]);
351 }
352 
353 /*
354  * Sets the ending cylinder for a partition.  This exists mainly to
355  * centralize the check.  (If spc is zero, cylinder numbers make
356  * little sense, and the code would otherwise die on divide-by-0 if we
357  * barged blindly ahead.)  We need to call this on a partition
358  * whenever we change it; we need to call it on all partitions
359  * whenever we change spc.
360  */
361 static void
362 set_endcyl(struct part *p)
363 {
364 	if (label.spc == 0) {
365 		p->endcyl = p->startcyl;
366 	} else {
367 		p->endcyl = p->startcyl + how_many(p->nblk, label.spc);
368 	}
369 }
370 
371 /*
372  * Unpack a label from disk into the in-core label structure.  If
373  * newlabel is set, we don't actually do so; we just synthesize a
374  * blank label instead.  This is where knowledge of the Sun label
375  * format is kept for read; pack_label is the corresponding routine
376  * for write.  We are careful to use labelbuf, l_s, or l_l as
377  * appropriate to avoid byte-sex issues, so we can work on
378  * little-endian machines.
379  *
380  * Note that a bad magic number for the extended partition information
381  * is not considered an error; it simply indicates there is no
382  * extended partition information.  Arguably this is the Wrong Thing,
383  * and we should take zero as meaning no info, and anything other than
384  * zero or LABEL_XMAGIC as reason to gripe.
385  */
386 static const char *
387 unpack_label(void)
388 {
389 	unsigned short int l_s[256];
390 	unsigned long int l_l[128];
391 	int i;
392 	unsigned long int sum;
393 	int have_x;
394 
395 	if (newlabel) {
396 		bzero(&label.asciilabel[0], 128);
397 		label.rpm = 0;
398 		label.pcyl = 0;
399 		label.apc = 0;
400 		label.obs1 = 0;
401 		label.obs2 = 0;
402 		label.intrlv = 0;
403 		label.ncyl = 0;
404 		label.acyl = 0;
405 		label.nhead = 0;
406 		label.nsect = 0;
407 		label.obs3 = 0;
408 		label.obs4 = 0;
409 		for (i = 0; i < NPART; i++) {
410 			label.partitions[i].startcyl = 0;
411 			label.partitions[i].nblk = 0;
412 			set_endcyl(&label.partitions[i]);
413 		}
414 		label.spc = 0;
415 		label.dirty = 1;
416 		return (0);
417 	}
418 	for (i = 0; i < 256; i++)
419 		l_s[i] = (labelbuf[i + i] << 8) | labelbuf[i + i + 1];
420 	for (i = 0; i < 128; i++)
421 		l_l[i] = (l_s[i + i] << 16) | l_s[i + i + 1];
422 	if (l_s[254] != LABEL_MAGIC) {
423 		if (fixmagic) {
424 			label.dirty = 1;
425 			warnx("ignoring incorrect magic number.");
426 		} else {
427 			return "bad magic number";
428 		}
429 	}
430 	sum = 0;
431 	for (i = 0; i < 256; i++)
432 		sum ^= l_s[i];
433 	label.dirty = 0;
434 	if (sum != 0) {
435 		if (fixcksum) {
436 			label.dirty = 1;
437 			warnx("ignoring incorrect checksum.");
438 		} else {
439 			return "checksum wrong";
440 		}
441 	}
442 	(void)memcpy(&label.asciilabel[0], &labelbuf[0], 128);
443 	label.rpm = l_s[210];
444 	label.pcyl = l_s[211];
445 	label.apc = l_s[212];
446 	label.obs1 = l_s[213];
447 	label.obs2 = l_s[214];
448 	label.intrlv = l_s[215];
449 	label.ncyl = l_s[216];
450 	label.acyl = l_s[217];
451 	label.nhead = l_s[218];
452 	label.nsect = l_s[219];
453 	label.obs3 = l_s[220];
454 	label.obs4 = l_s[221];
455 	label.spc = label.nhead * label.nsect;
456 	for (i = 0; i < 8; i++) {
457 		label.partitions[i].startcyl = (uint32_t)l_l[i + i + 111];
458 		label.partitions[i].nblk = (uint32_t)l_l[i + i + 112];
459 		set_endcyl(&label.partitions[i]);
460 	}
461 	have_x = 0;
462 	if (l_l[33] == LABEL_XMAGIC) {
463 		sum = 0;
464 		for (i = 0; i < ((NXPART * 2) + 1); i++)
465 			sum += l_l[33 + i];
466 		if (sum != l_l[32]) {
467 			if (fixcksum) {
468 				label.dirty = 1;
469 				warnx("Ignoring incorrect extended-partition checksum.");
470 				have_x = 1;
471 			} else {
472 				warnx("Extended-partition magic right but checksum wrong.");
473 			}
474 		} else {
475 			have_x = 1;
476 		}
477 	}
478 	if (have_x) {
479 		for (i = 0; i < NXPART; i++) {
480 			int j = i + i + 34;
481 			label.partitions[i + 8].startcyl = (uint32_t)l_l[j++];
482 			label.partitions[i + 8].nblk = (uint32_t)l_l[j++];
483 			set_endcyl(&label.partitions[i + 8]);
484 		}
485 	} else {
486 		for (i = 0; i < NXPART; i++) {
487 			label.partitions[i + 8].startcyl = 0;
488 			label.partitions[i + 8].nblk = 0;
489 			set_endcyl(&label.partitions[i + 8]);
490 		}
491 	}
492 	return 0;
493 }
494 
495 /*
496  * Pack a label from the in-core label structure into on-disk format.
497  * This is where knowledge of the Sun label format is kept for write;
498  * unpack_label is the corresponding routine for read.  If all
499  * partitions past the first 8 are size=0 cyl=0, we store all-0s in
500  * the extended partition space, to be fully compatible with Sun
501  * labels.  Since AFIAK nothing works in that case that would break if
502  * we put extended partition info there in the same format we'd use if
503  * there were real info there, this is arguably unnecessary, but it's
504  * easy to do.
505  *
506  * We are careful to avoid endianness issues by constructing everything
507  * in an array of shorts.  We do this rather than using chars or longs
508  * because the checksum is defined in terms of shorts; using chars or
509  * longs would simplify small amounts of code at the price of
510  * complicating more.
511  */
512 static void
513 pack_label(void)
514 {
515 	unsigned short int l_s[256];
516 	int i;
517 	unsigned short int sum;
518 
519 	memset(&l_s[0], 0, 512);
520 	memcpy(&labelbuf[0], &label.asciilabel[0], 128);
521 	for (i = 0; i < 64; i++)
522 		l_s[i] = (labelbuf[i + i] << 8) | labelbuf[i + i + 1];
523 	l_s[210] = label.rpm;
524 	l_s[211] = label.pcyl;
525 	l_s[212] = label.apc;
526 	l_s[213] = label.obs1;
527 	l_s[214] = label.obs2;
528 	l_s[215] = label.intrlv;
529 	l_s[216] = label.ncyl;
530 	l_s[217] = label.acyl;
531 	l_s[218] = label.nhead;
532 	l_s[219] = label.nsect;
533 	l_s[220] = label.obs3;
534 	l_s[221] = label.obs4;
535 	for (i = 0; i < 8; i++) {
536 		l_s[(i * 4) + 222] = label.partitions[i].startcyl >> 16;
537 		l_s[(i * 4) + 223] = label.partitions[i].startcyl & 0xffff;
538 		l_s[(i * 4) + 224] = label.partitions[i].nblk >> 16;
539 		l_s[(i * 4) + 225] = label.partitions[i].nblk & 0xffff;
540 	}
541 	for (i = 0; i < NXPART; i++) {
542 		if (label.partitions[i + 8].startcyl ||
543 		    label.partitions[i + 8].nblk)
544 			break;
545 	}
546 	if (i < NXPART) {
547 		unsigned long int xsum;
548 		l_s[66] = LABEL_XMAGIC >> 16;
549 		l_s[67] = LABEL_XMAGIC & 0xffff;
550 		for (i = 0; i < NXPART; i++) {
551 			int j = (i * 4) + 68;
552 			l_s[j++] = label.partitions[i + 8].startcyl >> 16;
553 			l_s[j++] = label.partitions[i + 8].startcyl & 0xffff;
554 			l_s[j++] = label.partitions[i + 8].nblk >> 16;
555 			l_s[j++] = label.partitions[i + 8].nblk & 0xffff;
556 		}
557 		xsum = 0;
558 		for (i = 0; i < ((NXPART * 2) + 1); i++)
559 			xsum += (l_s[i + i + 66] << 16) | l_s[i + i + 67];
560 		l_s[64] = (int32_t)(xsum >> 16);
561 		l_s[65] = (int32_t)(xsum & 0xffff);
562 	}
563 	l_s[254] = LABEL_MAGIC;
564 	sum = 0;
565 	for (i = 0; i < 255; i++)
566 		sum ^= l_s[i];
567 	l_s[255] = sum;
568 	for (i = 0; i < 256; i++) {
569 		labelbuf[i + i] = ((uint32_t)l_s[i]) >> 8;
570 		labelbuf[i + i + 1] = l_s[i] & 0xff;
571 	}
572 }
573 
574 /*
575  * Get the label.  Read it off the disk and unpack it.  This function
576  *  is nothing but lseek, read, unpack_label, and error checking.
577  */
578 static void
579 getlabel(void)
580 {
581 	int rv;
582 	const char *lerr;
583 
584 	if (lseek(diskfd, (off_t)0, SEEK_SET) == (off_t)-1)
585 		err(1, "lseek to 0 on `%s' failed", diskname);
586 
587 	if ((rv = read(diskfd, &labelbuf[0], 512)) == -1)
588 		err(1, "read label from `%s' failed", diskname);
589 
590 	if (rv != 512)
591 		errx(1, "short read from `%s' wanted %d, got %d.", diskname,
592 		    512, rv);
593 
594 	lerr = unpack_label();
595 	if (lerr)
596 		errx(1, "bogus label on `%s' (%s)", diskname, lerr);
597 }
598 
599 /*
600  * Put the label.  Pack it and write it to the disk.  This function is
601  *  little more than pack_label, lseek, write, and error checking.
602  */
603 static void
604 putlabel(void)
605 {
606 	int rv;
607 
608 	if (readonly) {
609 		warnx("No write access to `%s'", diskname);
610 		return;
611 	}
612 
613 	if (lseek(diskfd, (off_t)0, SEEK_SET) < (off_t)-1)
614 		err(1, "lseek to 0 on `%s' failed", diskname);
615 
616 	pack_label();
617 
618 	if ((rv = write(diskfd, &labelbuf[0], 512)) == -1) {
619 		err(1, "write label to `%s' failed", diskname);
620 		exit(1);
621 	}
622 
623 	if (rv != 512)
624 		errx(1, "short write to `%s': wanted %d, got %d",
625 		    diskname, 512, rv);
626 
627 	label.dirty = 0;
628 }
629 
630 /*
631  * Skip whitespace.  Used several places in the command-line parsing
632  * code.
633  */
634 static void
635 skipspaces(const char **cpp)
636 {
637 	const char *cp = *cpp;
638 	while (*cp && isspace((unsigned char)*cp))
639 		cp++;
640 	*cpp = cp;
641 }
642 
643 /*
644  * Scan a number.  The first arg points to the char * that's moving
645  *  along the string.  The second arg points to where we should store
646  *  the result.  The third arg says what we're scanning, for errors.
647  *  The return value is 0 on error, or nonzero if all goes well.
648  */
649 static int
650 scannum(const char **cpp, uint32_t *np, const char *tag)
651 {
652 	uint32_t v;
653 	int nd;
654 	const char *cp;
655 
656 	skipspaces(cpp);
657 	v = 0;
658 	nd = 0;
659 
660 	cp = *cpp;
661 	while (*cp && isdigit((unsigned char)*cp)) {
662 		v = (10 * v) + (*cp++ - '0');
663 		nd++;
664 	}
665 	*cpp = cp;
666 
667 	if (nd == 0) {
668 		printf("Missing/invalid %s: %s\n", tag, cp);
669 		return (0);
670 	}
671 	*np = v;
672 	return (1);
673 }
674 
675 /*
676  * Change a partition.  pno is the number of the partition to change;
677  *  numbers is a pointer to the string containing the specification for
678  *  the new start and size.  This always takes the form "start size",
679  *  where start can be
680  *
681  *	a number
682  *		The partition starts at the beginning of that cylinder.
683  *
684  *	start-X
685  *		The partition starts at the same place partition X does.
686  *
687  *	end-X
688  *		The partition starts at the place partition X ends.  If
689  *		partition X does not exactly on a cylinder boundary, it
690  *		is effectively rounded up.
691  *
692  *  and size can be
693  *
694  *	a number
695  *		The partition is that many sectors long.
696  *
697  *	num/num/num
698  *		The three numbers are cyl/trk/sect counts.  n1/n2/n3 is
699  *		equivalent to specifying a single number
700  *		((n1*label.nhead)+n2)*label.nsect)+n3.  In particular,
701  *		if label.nhead or label.nsect is zero, this has limited
702  *		usefulness.
703  *
704  *	end-X
705  *		The partition ends where partition X ends.  It is an
706  *		error for partition X to end before the specified start
707  *		point.  This always goes to exactly where partition X
708  *		ends, even if that's partway through a cylinder.
709  *
710  *	start-X
711  *		The partition extends to end exactly where partition X
712  *		begins.  It is an error for partition X to begin before
713  *		the specified start point.
714  *
715  *	size-X
716  *		The partition has the same size as partition X.
717  *
718  * If label.spc is nonzero but the partition size is not a multiple of
719  *  it, a warning is printed, since you usually don't want this.  Most
720  *  often, in my experience, this comes from specifying a cylinder
721  *  count as a single number N instead of N/0/0.
722  */
723 static void
724 chpart(int pno, const char *numbers)
725 {
726 	uint32_t cyl0;
727 	uint32_t size;
728 	uint32_t sizec;
729 	uint32_t sizet;
730 	uint32_t sizes;
731 
732 	skipspaces(&numbers);
733 	if (!memcmp(numbers, "end-", 4) && numbers[4]) {
734 		int epno = LETTERPART(numbers[4]);
735 		if ((epno >= 0) && (epno < NPART)) {
736 			cyl0 = label.partitions[epno].endcyl;
737 			numbers += 5;
738 		} else {
739 			if (!scannum(&numbers, &cyl0, "starting cylinder"))
740 				return;
741 		}
742 	} else if (!memcmp(numbers, "start-", 6) && numbers[6]) {
743 		int spno = LETTERPART(numbers[6]);
744 		if ((spno >= 0) && (spno < NPART)) {
745 			cyl0 = label.partitions[spno].startcyl;
746 			numbers += 7;
747 		} else {
748 			if (!scannum(&numbers, &cyl0, "starting cylinder"))
749 				return;
750 		}
751 	} else {
752 		if (!scannum(&numbers, &cyl0, "starting cylinder"))
753 			return;
754 	}
755 	skipspaces(&numbers);
756 	if (!memcmp(numbers, "end-", 4) && numbers[4]) {
757 		int epno = LETTERPART(numbers[4]);
758 		if ((epno >= 0) && (epno < NPART)) {
759 			if (label.partitions[epno].endcyl <= cyl0) {
760 				warnx("Partition %c ends before cylinder %u",
761 				    PARTLETTER(epno), cyl0);
762 				return;
763 			}
764 			size = label.partitions[epno].nblk;
765 			/* Be careful of unsigned arithmetic */
766 			if (cyl0 > label.partitions[epno].startcyl) {
767 				size -= (cyl0 - label.partitions[epno].startcyl)
768 				    * label.spc;
769 			} else if (cyl0 < label.partitions[epno].startcyl) {
770 				size += (label.partitions[epno].startcyl - cyl0)
771 				    * label.spc;
772 			}
773 			numbers += 5;
774 		} else {
775 			if (!scannum(&numbers, &size, "partition size"))
776 				return;
777 		}
778 	} else if (!memcmp(numbers, "start-", 6) && numbers[6]) {
779 		int  spno = LETTERPART(numbers[6]);
780 		if ((spno >= 0) && (spno < NPART)) {
781 			if (label.partitions[spno].startcyl <= cyl0) {
782 				warnx("Partition %c starts before cylinder %u",
783 				    PARTLETTER(spno), cyl0);
784 				return;
785 			}
786 			size = (label.partitions[spno].startcyl - cyl0)
787 			    * label.spc;
788 			numbers += 7;
789 		} else {
790 			if (!scannum(&numbers, &size, "partition size"))
791 				return;
792 		}
793 	} else if (!memcmp(numbers, "size-", 5) && numbers[5]) {
794 		int spno = LETTERPART(numbers[5]);
795 		if ((spno >= 0) && (spno < NPART)) {
796 			size = label.partitions[spno].nblk;
797 			numbers += 6;
798 		} else {
799 			if (!scannum(&numbers, &size, "partition size"))
800 				return;
801 		}
802 	} else {
803 		if (!scannum(&numbers, &size, "partition size"))
804 			return;
805 		skipspaces(&numbers);
806 		if (*numbers == '/') {
807 			sizec = size;
808 			numbers++;
809 			if (!scannum(&numbers, &sizet,
810 			    "partition size track value"))
811 				return;
812 			skipspaces(&numbers);
813 			if (*numbers != '/') {
814 				warnx("Invalid c/t/s syntax - no second slash");
815 				return;
816 			}
817 			numbers++;
818 			if (!scannum(&numbers, &sizes,
819 			    "partition size sector value"))
820 				return;
821 			size = sizes + (label.nsect * (sizet
822 			    + (label.nhead * sizec)));
823 		}
824 	}
825 	if (label.spc && (size % label.spc)) {
826 		warnx("Size is not a multiple of cylinder size (is %u/%u/%u)",
827 		    size / label.spc,
828 		    (size % label.spc) / label.nsect, size % label.nsect);
829 	}
830 	label.partitions[pno].startcyl = cyl0;
831 	label.partitions[pno].nblk = size;
832 	set_endcyl(&label.partitions[pno]);
833 	if ((label.partitions[pno].startcyl * label.spc)
834 	    + label.partitions[pno].nblk > label.spc * label.ncyl) {
835 		warnx("Partition extends beyond end of disk");
836 	}
837 	label.dirty = 1;
838 }
839 
840 /*
841  * Change a 128-byte-string field.  There's currently only one such,
842  *  the ASCII label field.
843  */
844 static void
845 chval_ascii(const char *cp, struct field *f)
846 {
847 	const char *nl;
848 
849 	skipspaces(&cp);
850 	if ((nl = strchr(cp, '\n')) == NULL)
851 		nl = cp + strlen(cp);
852 	if (nl - cp > 128) {
853 		warnx("Ascii label string too long - max 128 characters");
854 	} else {
855 		memset(f->loc, 0, 128);
856 		memcpy(f->loc, cp, (size_t)(nl - cp));
857 		label.dirty = 1;
858 	}
859 }
860 /*
861  * Change an int-valued field.  As noted above, there's only one
862  *  function, regardless of the field size in the on-disk label.
863  */
864 static void
865 chval_int(const char *cp, struct field *f)
866 {
867 	uint32_t v;
868 
869 	if (!scannum(&cp, &v, "value"))
870 		return;
871 	*(uint32_t *)f->loc = v;
872 	label.dirty = 1;
873 }
874 /*
875  * Change a field's value.  The string argument contains the field name
876  *  and the new value in text form.  Look up the field and call its
877  *  chval and changed functions.
878  */
879 static void
880 chvalue(const char *str)
881 {
882 	const char *cp;
883 	int i;
884 	size_t n;
885 
886 	if (fields[0].taglen < 1) {
887 		for (i = 0; fields[i].tag; i++)
888 			fields[i].taglen = strlen(fields[i].tag);
889 	}
890 	skipspaces(&str);
891 	cp = str;
892 	while (*cp && !isspace((unsigned char)*cp))
893 		cp++;
894 	n = cp - str;
895 	for (i = 0; fields[i].tag; i++) {
896 		if (((int)n == fields[i].taglen) && !memcmp(str, fields[i].tag, n)) {
897 			(*fields[i].chval) (cp, &fields[i]);
898 			if (fields[i].changed)
899 				(*fields[i].changed)();
900 			break;
901 		}
902 	}
903 	if (!fields[i].tag)
904 		warnx("Bad name %.*s - see L output for names", (int)n, str);
905 }
906 
907 /*
908  * `changed' function for the ntrack and nsect fields; update label.spc
909  *  and call set_endcyl on all partitions.
910  */
911 static void
912 update_spc(void)
913 {
914 	int i;
915 
916 	label.spc = label.nhead * label.nsect;
917 	for (i = 0; i < NPART; i++)
918 		set_endcyl(&label.partitions[i]);
919 }
920 
921 /*
922  * Print function for 128-byte-string fields.  Currently only the ASCII
923  *  label, but we don't depend on that.
924  */
925 static int
926 print_ascii(struct field *f, int sofar)
927 {
928 	printf("%s: %.128s\n", f->tag, (char *)f->loc);
929 	sofar = 0;
930 	return 0;
931 }
932 
933 /*
934  * Print an int-valued field.  We are careful to do proper line wrap,
935  *  making each value occupy 16 columns.
936  */
937 static int
938 print_int(struct field *f, int sofar)
939 {
940 	if (sofar >= 60) {
941 		printf("\n");
942 		sofar = 0;
943 	}
944 	printf("%s: %-*u", f->tag, 14 - (int)strlen(f->tag),
945 	    *(uint32_t *)f->loc);
946 	return sofar + 16;
947 }
948 
949 /*
950  * Print the whole label.  Just call the print function for each field,
951  *  then append a newline if necessary.
952  */
953 static void
954 print_label(void)
955 {
956 	int i;
957 	int c;
958 
959 	c = 0;
960 	for (i = 0; fields[i].tag; i++)
961 		c = (*fields[i].print) (&fields[i], c);
962 	if (c > 0)
963 		printf("\n");
964 }
965 
966 /*
967  * Figure out how many columns wide the screen is.  We impose a minimum
968  *  width of 20 columns; I suspect the output code has some issues if
969  *  we have fewer columns than partitions.
970  */
971 static int
972 screen_columns(void)
973 {
974 	int ncols;
975 #ifndef NO_TERMCAP_WIDTH
976 	char *term;
977 	char tbuf[1024];
978 #endif
979 #if defined(TIOCGWINSZ)
980 	struct winsize wsz;
981 #elif defined(TIOCGSIZE)
982 	struct ttysize tsz;
983 #endif
984 
985 	ncols = 80;
986 #ifndef NO_TERMCAP_WIDTH
987 	term = getenv("TERM");
988 	if (term && (tgetent(&tbuf[0], term) == 1)) {
989 		int n = tgetnum("co");
990 		if (n > 1)
991 			ncols = n;
992 	}
993 #endif
994 #if defined(TIOCGWINSZ)
995 	if ((ioctl(1, TIOCGWINSZ, &wsz) == 0) && (wsz.ws_col > 0)) {
996 		ncols = wsz.ws_col;
997 	}
998 #elif defined(TIOCGSIZE)
999 	if ((ioctl(1, TIOCGSIZE, &tsz) == 0) && (tsz.ts_cols > 0)) {
1000 		ncols = tsz.ts_cols;
1001 	}
1002 #endif
1003 	if (ncols < 20)
1004 		ncols = 20;
1005 	return ncols;
1006 }
1007 
1008 /*
1009  * Print the partitions.  The argument is true iff we should print all
1010  * partitions, even those set start=0 size=0.  We generate one line
1011  * per partition (or, if all==0, per `interesting' partition), plus a
1012  * visually graphic map of partition letters.  Most of the hair in the
1013  * visual display lies in ensuring that nothing takes up less than one
1014  * character column, that if two boundaries appear visually identical,
1015  * they _are_ identical.  Within that constraint, we try to make the
1016  * number of character columns proportional to the size....
1017  */
1018 static void
1019 print_part(int all)
1020 {
1021 	int i, j, k, n, r, c;
1022 	size_t ncols;
1023 	uint32_t edges[2 * NPART];
1024 	int ce[2 * NPART];
1025 	int row[NPART];
1026 	unsigned char table[2 * NPART][NPART];
1027 	char *line;
1028 	struct part *p = label.partitions;
1029 
1030 	for (i = 0; i < NPART; i++) {
1031 		if (all || p[i].startcyl || p[i].nblk) {
1032 			printf("%c: start cyl = %6u, size = %8u (",
1033 			    PARTLETTER(i), p[i].startcyl, p[i].nblk);
1034 			if (label.spc) {
1035 				printf("%u/%u/%u - ", p[i].nblk / label.spc,
1036 				    (p[i].nblk % label.spc) / label.nsect,
1037 				    p[i].nblk % label.nsect);
1038 			}
1039 			printf("%gMb)\n", p[i].nblk / 2048.0);
1040 		}
1041 	}
1042 
1043 	j = 0;
1044 	for (i = 0; i < NPART; i++) {
1045 		if (p[i].nblk > 0) {
1046 			edges[j++] = p[i].startcyl;
1047 			edges[j++] = p[i].endcyl;
1048 		}
1049 	}
1050 
1051 	do {
1052 		n = 0;
1053 		for (i = 1; i < j; i++) {
1054 			if (edges[i] < edges[i - 1]) {
1055 				uint32_t    t;
1056 				t = edges[i];
1057 				edges[i] = edges[i - 1];
1058 				edges[i - 1] = t;
1059 				n++;
1060 			}
1061 		}
1062 	} while (n > 0);
1063 
1064 	for (i = 1; i < j; i++) {
1065 		if (edges[i] != edges[n]) {
1066 			n++;
1067 			if (n != i)
1068 				edges[n] = edges[i];
1069 		}
1070 	}
1071 
1072 	n++;
1073 	for (i = 0; i < NPART; i++) {
1074 		if (p[i].nblk > 0) {
1075 			for (j = 0; j < n; j++) {
1076 				if ((p[i].startcyl <= edges[j]) &&
1077 				    (p[i].endcyl > edges[j])) {
1078 					table[j][i] = 1;
1079 				} else {
1080 					table[j][i] = 0;
1081 				}
1082 			}
1083 		}
1084 	}
1085 
1086 	ncols = screen_columns() - 2;
1087 	for (i = 0; i < n; i++)
1088 		ce[i] = (edges[i] * ncols) / (double) edges[n - 1];
1089 
1090 	for (i = 1; i < n; i++)
1091 		if (ce[i] <= ce[i - 1])
1092 			ce[i] = ce[i - 1] + 1;
1093 
1094 	if ((size_t)ce[n - 1] > ncols) {
1095 		ce[n - 1] = ncols;
1096 		for (i = n - 1; (i > 0) && (ce[i] <= ce[i - 1]); i--)
1097 			ce[i - 1] = ce[i] - 1;
1098 		if (ce[0] < 0)
1099 			for (i = 0; i < n; i++)
1100 				ce[i] = i;
1101 	}
1102 
1103 	printf("\n");
1104 	for (i = 0; i < NPART; i++) {
1105 		if (p[i].nblk > 0) {
1106 			r = -1;
1107 			do {
1108 				r++;
1109 				for (j = i - 1; j >= 0; j--) {
1110 					if (row[j] != r)
1111 						continue;
1112 					for (k = 0; k < n; k++)
1113 						if (table[k][i] && table[k][j])
1114 							break;
1115 					if (k < n)
1116 						break;
1117 				}
1118 			} while (j >= 0);
1119 			row[i] = r;
1120 		} else {
1121 			row[i] = -1;
1122 		}
1123 	}
1124 	r = row[0];
1125 	for (i = 1; i < NPART; i++)
1126 		if (row[i] > r)
1127 			r = row[i];
1128 
1129 	if ((line = malloc(ncols + 1)) == NULL)
1130 		err(1, "Can't allocate memory");
1131 
1132 	for (i = 0; i <= r; i++) {
1133 		for (j = 0; (size_t)j < ncols; j++)
1134 			line[j] = ' ';
1135 		for (j = 0; j < NPART; j++) {
1136 			if (row[j] != i)
1137 				continue;
1138 			k = 0;
1139 			for (k = 0; k < n; k++) {
1140 				if (table[k][j]) {
1141 					for (c = ce[k]; c < ce[k + 1]; c++)
1142 						line[c] = 'a' + j;
1143 				}
1144 			}
1145 		}
1146 		for (j = ncols - 1; (j >= 0) && (line[j] == ' '); j--);
1147 		printf("%.*s\n", j + 1, line);
1148 	}
1149 	free(line);
1150 }
1151 
1152 #ifdef S_COMMAND
1153 /*
1154  * This computes an appropriate checksum for an in-core label.  It's
1155  * not really related to the S command, except that it's needed only
1156  * by setlabel(), which is #ifdef S_COMMAND.
1157  */
1158 static unsigned short int
1159 dkcksum(const struct disklabel *lp)
1160 {
1161 	const unsigned short int *start;
1162 	const unsigned short int *end;
1163 	unsigned short int sum;
1164 	const unsigned short int *p;
1165 
1166 	start = (const void *)lp;
1167 	end = (const void *)&lp->d_partitions[lp->d_npartitions];
1168 	sum = 0;
1169 	for (p = start; p < end; p++)
1170 		sum ^= *p;
1171 	return (sum);
1172 }
1173 
1174 /*
1175  * Set the in-core label.  This is basically putlabel, except it builds
1176  * a struct disklabel instead of a Sun label buffer, and uses
1177  * DIOCSDINFO instead of lseek-and-write.
1178  */
1179 static void
1180 setlabel(void)
1181 {
1182 	union {
1183 		struct disklabel l;
1184 		char pad[sizeof(struct disklabel) -
1185 		     (MAXPARTITIONS * sizeof(struct partition)) +
1186 		      (16 * sizeof(struct partition))];
1187 	} u;
1188 	int i;
1189 	struct part *p = label.partitions;
1190 
1191 	if (ioctl(diskfd, DIOCGDINFO, &u.l) == -1) {
1192 		warn("ioctl DIOCGDINFO failed");
1193 		return;
1194 	}
1195 	if (u.l.d_secsize != 512) {
1196 		warnx("Disk claims %d-byte sectors", (int)u.l.d_secsize);
1197 	}
1198 	u.l.d_nsectors = label.nsect;
1199 	u.l.d_ntracks = label.nhead;
1200 	u.l.d_ncylinders = label.ncyl;
1201 	u.l.d_secpercyl = label.nsect * label.nhead;
1202 	u.l.d_rpm = label.rpm;
1203 	u.l.d_interleave = label.intrlv;
1204 	u.l.d_npartitions = getmaxpartitions();
1205 	memset(&u.l.d_partitions[0], 0,
1206 	    u.l.d_npartitions * sizeof(struct partition));
1207 	for (i = 0; i < u.l.d_npartitions; i++) {
1208 		u.l.d_partitions[i].p_size = p[i].nblk;
1209 		u.l.d_partitions[i].p_offset = p[i].startcyl
1210 		    * label.nsect * label.nhead;
1211 		u.l.d_partitions[i].p_fsize = 0;
1212 		u.l.d_partitions[i].p_fstype = (i == 1) ? FS_SWAP :
1213 		    (i == 2) ? FS_UNUSED : FS_BSDFFS;
1214 		u.l.d_partitions[i].p_frag = 0;
1215 		u.l.d_partitions[i].p_cpg = 0;
1216 	}
1217 	u.l.d_checksum = 0;
1218 	u.l.d_checksum = dkcksum(&u.l);
1219 	if (ioctl(diskfd, DIOCSDINFO, &u.l) == -1) {
1220 		warn("ioctl DIOCSDINFO failed");
1221 		return;
1222 	}
1223 }
1224 #endif
1225 
1226 static const char *help[] = {
1227 	"?\t- print this help",
1228 	"L\t- print label, except for partition table",
1229 	"P\t- print partition table",
1230 	"PP\t- print partition table including size=0 offset=0 entries",
1231 	"[abcdefghijklmnop] <cylno> <size> - change partition",
1232 	"V <name> <value> - change a non-partition label value",
1233 	"W\t- write (possibly modified) label out",
1234 #ifdef S_COMMAND
1235 	"S\t- set label in the kernel (orthogonal to W)",
1236 #endif
1237 	"Q\t- quit program (error if no write since last change)",
1238 	"Q!\t- quit program (unconditionally) [EOF also quits]",
1239 	NULL
1240 };
1241 
1242 /*
1243  * Read and execute one command line from the user.
1244  */
1245 static void
1246 docmd(void)
1247 {
1248 	char cmdline[512];
1249 	int i;
1250 
1251 	if (!quiet)
1252 		printf("sunlabel> ");
1253 	if (fgets(&cmdline[0], sizeof(cmdline), stdin) != &cmdline[0])
1254 		exit(0);
1255 	switch (cmdline[0]) {
1256 	case '?':
1257 		for (i = 0; help[i]; i++)
1258 			printf("%s\n", help[i]);
1259 		break;
1260 	case 'L':
1261 		print_label();
1262 		break;
1263 	case 'P':
1264 		print_part(cmdline[1] == 'P');
1265 		break;
1266 	case 'W':
1267 		putlabel();
1268 		break;
1269 	case 'S':
1270 #ifdef S_COMMAND
1271 		setlabel();
1272 #else
1273 		printf("This compilation doesn't support S.\n");
1274 #endif
1275 		break;
1276 	case 'Q':
1277 		if ((cmdline[1] == '!') || !label.dirty)
1278 			exit(0);
1279 		printf("Label is dirty - use w to write it\n");
1280 		printf("Use Q! to quit anyway.\n");
1281 		break;
1282 	case 'a':
1283 	case 'b':
1284 	case 'c':
1285 	case 'd':
1286 	case 'e':
1287 	case 'f':
1288 	case 'g':
1289 	case 'h':
1290 	case 'i':
1291 	case 'j':
1292 	case 'k':
1293 	case 'l':
1294 	case 'm':
1295 	case 'n':
1296 	case 'o':
1297 	case 'p':
1298 		chpart(LETTERPART(cmdline[0]), &cmdline[1]);
1299 		break;
1300 	case 'V':
1301 		chvalue(&cmdline[1]);
1302 		break;
1303 	case '\n':
1304 		break;
1305 	default:
1306 		printf("(Unrecognized command character %c ignored.)\n",
1307 		    cmdline[0]);
1308 		break;
1309 	}
1310 }
1311 
1312 /*
1313  * main() (duh!).  Pretty boring.
1314  */
1315 int
1316 main(int ac, char **av)
1317 {
1318 	handleargs(ac, av);
1319 	getlabel();
1320 	for (;;)
1321 		docmd();
1322 }
1323