main.c revision 1.49 1 /* $NetBSD: main.c,v 1.49 2018/04/01 04:35:02 ryo Exp $ */
2
3 /*
4 * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Julio M. Merino Vidal.
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 /*
33 * Copyright (c) 1987, 1993
34 * The Regents of the University of California. All rights reserved.
35 *
36 * This code is derived from software contributed to Berkeley by
37 * Symmetric Computer Systems.
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * SUCH DAMAGE.
62 */
63
64 #if HAVE_NBTOOL_CONFIG_H
65 #include "nbtool_config.h"
66 #endif
67
68 #include <sys/cdefs.h>
69 #ifndef lint
70 __COPYRIGHT("@(#) Copyright (c) 1987, 1993\
71 The Regents of the University of California. All rights reserved.");
72 #endif /* not lint */
73
74 #ifndef lint
75 #if 0
76 static char sccsid[] = "@(#)disklabel.c 8.4 (Berkeley) 5/4/95";
77 /* from static char sccsid[] = "@(#)disklabel.c 1.2 (Symmetric) 11/28/85"; */
78 #else
79 __RCSID("$NetBSD: main.c,v 1.49 2018/04/01 04:35:02 ryo Exp $");
80 #endif
81 #endif /* not lint */
82
83 #include <sys/param.h>
84 #include <sys/file.h>
85 #include <sys/stat.h>
86 #include <sys/wait.h>
87 #define DKTYPENAMES
88 #define FSTYPENAMES
89
90 #include <ctype.h>
91 #include <err.h>
92 #include <errno.h>
93 #include <signal.h>
94 #include <string.h>
95 #include <stdio.h>
96 #include <stdlib.h>
97 #include <limits.h>
98 #include <unistd.h>
99
100 #include <ufs/ufs/dinode.h>
101 #include <ufs/ffs/fs.h>
102
103 #if HAVE_NBTOOL_CONFIG_H
104 #include <nbinclude/sys/disklabel.h>
105 #include <nbinclude/sys/disklabel_acorn.h>
106 #include <nbinclude/sys/bootblock.h>
107 #include "../../include/disktab.h"
108 #else
109 #include <sys/ioctl.h>
110 #include <sys/disklabel.h>
111 #include <sys/disklabel_acorn.h>
112 #include <sys/bootblock.h>
113 #include <util.h>
114 #include <disktab.h>
115 #endif /* HAVE_NBTOOL_CONFIG_H */
116
117 #include "pathnames.h"
118 #include "extern.h"
119 #include "dkcksum.h"
120 #include "bswap.h"
121
122 /*
123 * Disklabel: read and write disklabels.
124 * The label is usually placed on one of the first sectors of the disk.
125 * Many machines also place a bootstrap in the same area,
126 * in which case the label is embedded in the bootstrap.
127 * The bootstrap source must leave space at the proper offset
128 * for the label on such machines.
129 */
130
131 #ifndef BBSIZE
132 #define BBSIZE 8192 /* size of boot area, with label */
133 #endif
134
135 #define DISKMAGIC_REV bswap32(DISKMAGIC)
136 /* To delete a label, we just invert the magic numbers */
137 #define DISKMAGIC_DELETED (~DISKMAGIC)
138 #define DISKMAGIC_DELETED_REV bswap32(~DISKMAGIC)
139
140 #define DEFEDITOR _PATH_VI
141
142 char specname[MAXPATHLEN];
143
144 /* Some global data, all too hard to pass about */
145 char bootarea[BBSIZE]; /* Buffer matching part of disk */
146 int bootarea_len; /* Number of bytes we actually read */
147 static struct disklabel lab; /* The label we have updated */
148
149 static int Aflag; /* Action all labels */
150 static int Fflag; /* Read/write from file */
151 static int rflag; /* Read/write direct from disk */
152 static int tflag; /* Format output as disktab */
153 int Cflag; /* CHS format output */
154 static int Dflag; /* Delete old labels (use with write) */
155 static int Iflag; /* Read/write direct, but default if absent */
156 static int lflag; /* List all known file system types and exit */
157 static int mflag; /* Expect disk to contain an MBR */
158 static int verbose;
159 static int read_all; /* set if op = READ && Aflag */
160
161 static int write_label(int);
162 static int readlabel_direct(int);
163 static void writelabel_direct(int);
164 static int update_label(int, u_int, u_int);
165 static struct disklabel *find_label(int, u_int);
166 #if !defined(NATIVELABEL_ONLY)
167 static void getmachineparams(const char *);
168 #endif
169
170 static void makedisktab(FILE *, struct disklabel *);
171 static void makelabel(const char *, const char *);
172 static void l_perror(const char *);
173 static void readlabel(int);
174 static int edit(int);
175 static int editit(const char *);
176 static char *skip(char *);
177 static char *word(char *);
178 static int getasciilabel(FILE *, struct disklabel *);
179 __dead static void usage(void);
180 static int qsort_strcmp(const void *, const void *);
181 static int getulong(const char *, char, char **,
182 unsigned long *, unsigned long);
183 #define GETNUM32(a, v) getulong(a, '\0', NULL, v, UINT32_MAX)
184 #define GETNUM16(a, v) getulong(a, '\0', NULL, v, UINT16_MAX)
185 #define GETNUM8(a, v) getulong(a, '\0', NULL, v, UINT8_MAX)
186
187 static int set_writable_fd = -1;
188
189 #if !defined(NATIVELABEL_ONLY)
190 static u_int labeloffset;
191 static u_int labelsector;
192 static int labelusesmbr;
193 u_int maxpartitions;
194 static int byteorder;
195
196 static int biendian_p;
197 #ifndef HAVE_NBTOOL_CONFIG_H
198 static int native_p = 1;
199 #endif
200 int bswap_p;
201
202 static const struct disklabel_params {
203 const char *machine;
204 u_char labelusesmbr : 1;
205 u_char labelsector : 7;
206 u_char maxpartitions;
207 u_char raw_part;
208 u_char oldmaxpartitions;
209 u_short labeloffset;
210 u_short byteorder;
211 } disklabel_params[] = {
212 { "mvme68k", 0, 0, 8, 2, 0, 0, BIG_ENDIAN }, /* m68k */
213 { "next68k", 0, 0, 8, 2, 0, 0, BIG_ENDIAN }, /* m68k */
214
215 { "algor", 0, 0, 8, 2, 0, 64, LITTLE_ENDIAN }, /* mips */
216 { "alpha", 0, 0, 8, 2, 0, 64, LITTLE_ENDIAN }, /* alpha */
217 { "luna68k", 0, 0, 8, 2, 0, 64, BIG_ENDIAN }, /* m68k */
218 { "mac68k", 0, 0, 8, 2, 0, 64, BIG_ENDIAN }, /* m68k */
219 { "news68k", 0, 0, 8, 2, 0, 64, BIG_ENDIAN }, /* m68k */
220 { "newsmips", 0, 0, 8, 2, 0, 64, BIG_ENDIAN }, /* mips */
221 { "pmax", 0, 0, 8, 2, 0, 64, LITTLE_ENDIAN }, /* mips */
222 { "sun2", 0, 0, 8, 2, 0, 64, BIG_ENDIAN }, /* m68k */
223 { "sun68k", 0, 0, 8, 2, 0, 64, BIG_ENDIAN }, /* m68010 */
224 { "x68k", 0, 0, 8, 2, 0, 64, BIG_ENDIAN }, /* m68010 */
225
226 { "vax", 0, 0, 12, 2, 8, 64, LITTLE_ENDIAN }, /* vax */
227
228 { "amiga", 0, 0, 16, 2, 0, 64, BIG_ENDIAN }, /* m68k */
229 { "amigappc", 0, 0, 16, 2, 0, 64, BIG_ENDIAN }, /* powerpc */
230 { "evbmips", 0, 0, 16, 2, 0, 64, 0 }, /* mips */
231 { "evbppc", 0, 0, 16, 2, 0, 64, BIG_ENDIAN }, /* powerpc */
232
233 { "sparc", 0, 0, 8, 2, 0, 128, BIG_ENDIAN }, /* sun */
234 { "sparc64", 0, 0, 8, 2, 0, 128, BIG_ENDIAN }, /* sun */
235 { "sun3", 0, 0, 8, 2, 0, 128, BIG_ENDIAN }, /* sun */
236
237 { "atari", 0, 0, 16, 2, 0, 516, BIG_ENDIAN }, /* m68k */
238
239 { "mipsco", 0, 1, 8, 2, 0, 0, BIG_ENDIAN }, /* mips */
240 { "mvmeppc", 0, 1, 8, 3, 0, 0, BIG_ENDIAN }, /* powerpc */
241
242 { "bebox", 0, 1, 8, 3, 0, 0, BIG_ENDIAN }, /* powerpc */
243
244 { "emips", 0, 1, 16, 2, 0, 0, BIG_ENDIAN }, /* mips */
245 { "hppa", 0, 1, 16, 2, 0, 0, BIG_ENDIAN }, /* hppa */
246 { "ibmnws", 0, 1, 16, 2, 0, 0, BIG_ENDIAN }, /* powerpc */
247 { "ofppc", 0, 1, 16, 2, 0, 0, BIG_ENDIAN }, /* powerpc */
248 { "rs6000", 0, 1, 16, 2, 0, 0, BIG_ENDIAN }, /* powerpc */
249 { "sandpoint", 0, 1, 16, 2, 0, 0, BIG_ENDIAN }, /* powerpc */
250 { "sgimips", 0, 1, 16, 2, 0, 0, BIG_ENDIAN }, /* mips */
251
252 { "sbmips", 0, 1, 16, 3, 0, 0, 0 }, /* mips */
253
254 { "cesfic", 0, 2, 8, 2, 0, 0, BIG_ENDIAN }, /* m68k */
255 { "hp300", 0, 2, 8, 2, 0, 0, BIG_ENDIAN }, /* m68k */
256
257 { "ews4800mips",0, 9, 16, 15, 0, 0, BIG_ENDIAN }, /* mips */
258
259 { "macppc", 1, 0, 16, 2, 0, 64, BIG_ENDIAN }, /* powerpc */
260 { "pmon", 1, 0, 16, 2, 0, 64, 0 }, /* evbmips */
261
262 { "prep", 1, 1, 8, 2, 0, 0, BIG_ENDIAN }, /* powerpc */
263
264 { "dreamcast", 1, 1, 16, 2, 0, 0, LITTLE_ENDIAN }, /* sh3 */
265 { "evbcf", 1, 1, 16, 2, 0, 0, BIG_ENDIAN }, /* coldfire */
266 { "evbppc-mbr", 1, 1, 16, 2, 0, 0, BIG_ENDIAN }, /* powerpc */
267 { "evbsh3", 1, 1, 16, 2, 0, 0, 0 }, /* sh3 */
268 { "hpcsh", 1, 1, 16, 2, 0, 0, LITTLE_ENDIAN }, /* sh3 */
269 { "mmeye", 1, 1, 16, 2, 0, 0, 0 }, /* sh3 */
270 { "or1k", 1, 1, 16, 2, 0, 0, BIG_ENDIAN }, /* or1k */
271 { "riscv", 1, 1, 16, 2, 0, 0, LITTLE_ENDIAN }, /* riscv */
272
273 { "acorn32", 1, 1, 16, 2, 8, 0, LITTLE_ENDIAN }, /* arm */
274 { "cats", 1, 1, 16, 2, 8, 0, LITTLE_ENDIAN }, /* arm */
275 { "evbarm", 1, 1, 16, 2, 8, 0, 0 }, /* arm */
276 { "iyonix", 1, 1, 16, 2, 8, 0, LITTLE_ENDIAN }, /* arm */
277 { "netwinder", 1, 1, 16, 2, 8, 0, LITTLE_ENDIAN }, /* arm */
278 { "shark", 1, 1, 16, 2, 8, 0, LITTLE_ENDIAN }, /* arm */
279
280 { "amd64", 1, 1, 16, 3, 0, 0, LITTLE_ENDIAN }, /* x86 */
281 { "arc", 1, 1, 16, 3, 0, 0, LITTLE_ENDIAN }, /* mips */
282 { "cobalt", 1, 1, 16, 3, 0, 0, LITTLE_ENDIAN }, /* mips */
283 { "landisk", 1, 1, 16, 3, 0, 0, LITTLE_ENDIAN }, /* sh3 */
284
285 { "epoc32", 1, 1, 16, 3, 8, 0, LITTLE_ENDIAN }, /* arm */
286 { "hpcarm", 1, 1, 16, 3, 8, 0, LITTLE_ENDIAN }, /* arm */
287 { "hpcmips", 1, 1, 16, 3, 8, 0, LITTLE_ENDIAN }, /* mips */
288 { "i386", 1, 1, 16, 3, 8, 0, LITTLE_ENDIAN }, /* x86 */
289 { "ia64", 1, 1, 16, 3, 8, 0, LITTLE_ENDIAN }, /* x86 */
290 { "zaurus", 1, 1, 16, 3, 8, 0, LITTLE_ENDIAN }, /* arm */
291
292 { NULL, 0, 0, 0, 0, 0, 0, 0 }, /* must be last */
293 };
294
295 #ifndef HAVE_NBTOOL_CONFIG_H
296 static struct disklabel_params native_params;
297 #endif
298
299 static const struct arch_endian {
300 int byteorder;
301 const char *arch;
302 } arch_endians[] = {
303 { LITTLE_ENDIAN, "aarch64" },
304 { LITTLE_ENDIAN, "alpha" },
305 { LITTLE_ENDIAN, "arm" },
306 { LITTLE_ENDIAN, "earm" },
307 { LITTLE_ENDIAN, "earmhf" },
308 { LITTLE_ENDIAN, "earmv4" },
309 { LITTLE_ENDIAN, "earmv5" },
310 { LITTLE_ENDIAN, "earmv6" },
311 { LITTLE_ENDIAN, "earmv6hf" },
312 { LITTLE_ENDIAN, "earmv7" },
313 { LITTLE_ENDIAN, "earmv7hf" },
314 { LITTLE_ENDIAN, "i386" },
315 { LITTLE_ENDIAN, "ia64" },
316 { LITTLE_ENDIAN, "mipsel" },
317 { LITTLE_ENDIAN, "mips64el" },
318 { LITTLE_ENDIAN, "riscv32" },
319 { LITTLE_ENDIAN, "riscv64" },
320 { LITTLE_ENDIAN, "sh3el" },
321 { LITTLE_ENDIAN, "vax" },
322 { LITTLE_ENDIAN, "x86_64" },
323
324 { BIG_ENDIAN, "aarch64eb" },
325 { BIG_ENDIAN, "armeb" },
326 { BIG_ENDIAN, "coldfire" },
327 { BIG_ENDIAN, "earmeb" },
328 { BIG_ENDIAN, "earmhfeb" },
329 { BIG_ENDIAN, "earmv4eb" },
330 { BIG_ENDIAN, "earmv5eb" },
331 { BIG_ENDIAN, "earmv6eb" },
332 { BIG_ENDIAN, "earmv6hfeb" },
333 { BIG_ENDIAN, "earmv7eb" },
334 { BIG_ENDIAN, "earmv7hfeb" },
335 { BIG_ENDIAN, "hppa" },
336 { BIG_ENDIAN, "m68000" },
337 { BIG_ENDIAN, "m68k" },
338 { BIG_ENDIAN, "mipseb" },
339 { BIG_ENDIAN, "mips64eb" },
340 { BIG_ENDIAN, "or1k" },
341 { BIG_ENDIAN, "powerpc" },
342 { BIG_ENDIAN, "sh3eb" },
343 { BIG_ENDIAN, "sparc" },
344 { BIG_ENDIAN, "sparc64" },
345
346 { 0, NULL },
347 };
348
349 /* Default location for label - only used if we don't find one to update */
350 #define LABEL_OFFSET (dklabel_getlabelsector() * DEV_BSIZE + dklabel_getlabeloffset())
351 #else
352 #define labeloffset LABELOFFSET
353 #define labelsector LABELSECTOR
354 #define labelusesmbr LABELUSESMBR
355 #define maxpartitions MAXPARTITIONS
356 #define LABEL_OFFSET LABELOFFSET
357 #endif /* !NATIVELABEL_ONLY */
358
359 /*
360 * For portability it doesn't make sense to use any other value....
361 * Except, maybe, the size of a physical sector.
362 * This value is used if we have to write a label to the start of an mbr ptn.
363 */
364 #ifndef LABELOFFSET_MBR
365 #define LABELOFFSET_MBR 512
366 #endif
367
368 #if HAVE_NBTOOL_CONFIG_H
369 static int
370 opendisk(const char *path, int flags, char *buf, int buflen, int cooked)
371 {
372 int f;
373 f = open(path, flags, 0);
374 strlcpy(buf, path, buflen);
375 return f;
376 }
377 #endif /* HAVE_NBTOOL_CONFIG_H */
378
379 #if !defined(NATIVELABEL_ONLY)
380 static void
381 setbyteorder(int new_byteorder)
382 {
383 static int set_p;
384
385 if ((!biendian_p || set_p)
386 && byteorder != 0
387 && byteorder != new_byteorder) {
388 warnx("changing %s byteorder to %s",
389 byteorder == LITTLE_ENDIAN ? "le" : "be",
390 new_byteorder == LITTLE_ENDIAN ? "le" : "be");
391 }
392 byteorder = new_byteorder;
393 biendian_p = 0;
394 set_p = 1;
395 }
396
397 static void
398 getmachineparams(const char *mach)
399 {
400 const struct disklabel_params *dp = disklabel_params;
401 for (; dp->machine != NULL; dp++) {
402 if (!strcmp(mach, dp->machine)) {
403 labelusesmbr = dp->labelusesmbr;
404 labelsector = dp->labelsector;
405 labeloffset = dp->labeloffset;
406 maxpartitions = dp->maxpartitions;
407 biendian_p = (dp->byteorder == 0);
408 if (!biendian_p)
409 setbyteorder(dp->byteorder);
410 return;
411 }
412 }
413 errx(1, "%s: unknown machine type", mach);
414 }
415
416 static void
417 getarchbyteorder(const char *arch)
418 {
419 const struct arch_endian *p = arch_endians;
420 for (; p->arch != NULL; p++) {
421 if (!strcmp(arch, p->arch)) {
422 setbyteorder(p->byteorder);
423 return;
424 }
425 }
426 errx(1, "%s: unknown arch", arch);
427 }
428
429 static daddr_t
430 dklabel_getlabelsector(void)
431 {
432 unsigned long int nval;
433 char *end;
434 const char *val;
435
436 if ((val = getenv("DISKLABELSECTOR")) == NULL)
437 return labelsector;
438 if ((nval = strtoul(val, &end, 10)) == ULONG_MAX && errno == ERANGE)
439 err(EXIT_FAILURE, "DISKLABELSECTOR in environment");
440 return nval;
441 }
442
443 static off_t
444 dklabel_getlabeloffset(void)
445 {
446 unsigned long int nval;
447 char *end;
448 const char *val;
449
450 if ((val = getenv("DISKLABELOFFSET")) == NULL)
451 return labeloffset;
452 if ((nval = strtoul(val, &end, 10)) == ULONG_MAX && errno == ERANGE)
453 err(EXIT_FAILURE, "DISKLABELOFFSET in environment");
454 return nval;
455 }
456 #endif /* !NATIVELABEL_ONLY */
457
458 static void
459 clear_writable(void)
460 {
461 static int zero = 0;
462 dk_ioctl(set_writable_fd, DIOCWLABEL, &zero);
463 }
464
465 int
466 main(int argc, char *argv[])
467 {
468 FILE *t;
469 int ch, f, error;
470 char *dkname;
471 #if !defined(NATIVELABEL_ONLY)
472 char *cp;
473 #endif
474 struct stat sb;
475 int writable;
476 enum {
477 UNSPEC, EDIT, READ, RESTORE, SETWRITABLE, SETREADONLY,
478 WRITE,
479 #if !defined(NO_INTERACT)
480 INTERACT,
481 #endif
482 DELETE
483 } op = UNSPEC, old_op;
484
485 #ifndef HAVE_NBTOOL_CONFIG_H
486 #if !defined(NATIVELABEL_ONLY)
487 labeloffset = native_params.labeloffset = getlabeloffset();
488 labelsector = native_params.labelsector = getlabelsector();
489 labelusesmbr = native_params.labelusesmbr = getlabelusesmbr();
490 maxpartitions = native_params.maxpartitions = getmaxpartitions();
491 byteorder = native_params.byteorder = BYTE_ORDER;
492 #endif
493 #endif
494
495 #if !defined(NATIVELABEL_ONLY)
496 if ((cp = getenv("MACHINE")) != NULL) {
497 getmachineparams(cp);
498 }
499
500 if ((cp = getenv("MACHINE_ARCH")) != NULL) {
501 getarchbyteorder(cp);
502 }
503 #endif
504
505 mflag = labelusesmbr;
506 if (mflag < 0) {
507 #if HAVE_NBTOOL_CONFIG_H
508 warn("getlabelusesmbr() failed");
509 #else
510 warn("getlabelusesmbr() failed");
511 mflag = LABELUSESMBR;
512 #endif
513 }
514 #if HAVE_NBTOOL_CONFIG_H
515 /* We must avoid doing any ioctl requests */
516 Fflag = rflag = 1;
517 #endif
518
519 error = 0;
520 while ((ch = getopt(argc, argv, "AB:CDFIM:NRWef:ilmrtvw")) != -1) {
521 old_op = op;
522 switch (ch) {
523 case 'A': /* Action all labels */
524 Aflag = 1;
525 rflag = 1;
526 break;
527 case 'C': /* Display in CHS format */
528 Cflag = 1;
529 break;
530 case 'D': /* Delete all existing labels */
531 Dflag = 1;
532 rflag = 1;
533 break;
534 case 'F': /* Treat 'disk' as a regular file */
535 Fflag = 1;
536 rflag = 1; /* Force direct access */
537 break;
538 case 'I': /* Use default label if none found */
539 Iflag = 1;
540 rflag = 1; /* Implies direct access */
541 break;
542 case 'R': /* Restore label from text file */
543 op = RESTORE;
544 break;
545 #if !defined(NATIVELABEL_ONLY)
546 case 'B': /* byteorder */
547 if (!strcmp(optarg, "be")) {
548 setbyteorder(BIG_ENDIAN);
549 } else if (!strcmp(optarg, "le")) {
550 setbyteorder(LITTLE_ENDIAN);
551 } else {
552 errx(1, "%s: not be or le", optarg);
553 }
554 break;
555 case 'M': /* machine type */
556 getmachineparams(optarg);
557 break;
558 #endif
559 case 'N': /* Disallow writes to label sector */
560 op = SETREADONLY;
561 break;
562 case 'W': /* Allow writes to label sector */
563 op = SETWRITABLE;
564 break;
565 case 'e': /* Edit label with $EDITOR */
566 op = EDIT;
567 break;
568 case 'f': /* Name of disktab file */
569 if (setdisktab(optarg) == -1)
570 usage();
571 break;
572 #if !defined(NO_INTERACT)
573 case 'i': /* Edit using built-in editor */
574 op = INTERACT;
575 break;
576 #endif /* !NO_INTERACT */
577 case 'l': /* List all known file system types and exit */
578 lflag = 1;
579 break;
580 case 'm': /* Expect disk to have an MBR */
581 mflag ^= 1;
582 break;
583 case 'r': /* Read/write label directly from disk */
584 rflag = 1;
585 break;
586 case 't': /* Format output as a disktab entry */
587 tflag = 1;
588 break;
589 case 'v': /* verbose/diag output */
590 verbose++;
591 break;
592 case 'w': /* Write label based on disktab entry */
593 op = WRITE;
594 break;
595 case '?':
596 default:
597 usage();
598 }
599 if (old_op != UNSPEC && old_op != op)
600 usage();
601 }
602
603 #if !defined(NATIVELABEL_ONLY)
604 if (maxpartitions == 0) {
605 errx(1, "unknown label: use -M/-B and $MACHINE/$MACHINE_ARCH");
606 }
607 if (byteorder != BIG_ENDIAN && byteorder != LITTLE_ENDIAN) {
608 errx(1, "unknown byteorder");
609 }
610 bswap_p = (byteorder != BYTE_ORDER);
611 #ifdef DEBUG
612 printf("labelusesmbr=%d labelsector=%u labeloffset=%u maxpartitions=%u\n",
613 labelusesmbr, labelsector, labeloffset, maxpartitions);
614 printf("byteorder=%d bswap_p=%d\n", byteorder, bswap_p);
615 #endif
616 #ifndef HAVE_NBTOOL_CONFIG_H
617 /*
618 * If the disklabel has the same location as the native disklabel and
619 * fewer or equal paritions, we can use the native ioctls. Otherwise
620 * force file/raw access.
621 */
622 native_p = native_params.labelusesmbr == labelusesmbr
623 && native_params.labelsector == labelsector
624 && native_params.labeloffset == labeloffset
625 && maxpartitions <= native_params.maxpartitions
626 && !bswap_p;
627 if (!native_p)
628 Fflag = rflag = 1;
629 #endif
630 #endif /* !NATIVELABEL_ONLY */
631
632 argc -= optind;
633 argv += optind;
634
635 if (lflag)
636 exit(list_fs_types() ? EXIT_SUCCESS : EXIT_FAILURE);
637
638 if (op == UNSPEC)
639 op = Dflag ? DELETE : READ;
640
641 if (argc < 1)
642 usage();
643
644 if (Iflag && op != EDIT
645 #if !defined(NO_INTERACT)
646 && op != INTERACT
647 #endif
648 )
649 usage();
650
651 dkname = argv[0];
652 f = opendisk(dkname, op == READ ? O_RDONLY : O_RDWR,
653 specname, sizeof specname, 0);
654 if (f < 0)
655 err(4, "%s", specname);
656
657 if (!Fflag && fstat(f, &sb) == 0 && S_ISREG(sb.st_mode))
658 Fflag = rflag = 1;
659
660 switch (op) {
661
662 case DELETE: /* Remove all existing labels */
663 if (argc != 1)
664 usage();
665 Dflag = 2;
666 writelabel_direct(f);
667 break;
668
669 case EDIT:
670 if (argc != 1)
671 usage();
672 readlabel(f);
673 error = edit(f);
674 break;
675
676 #if !defined(NO_INTERACT)
677 case INTERACT:
678 if (argc != 1)
679 usage();
680 readlabel(f);
681 /*
682 * XXX: Fill some default values so checklabel does not fail
683 */
684 if (lab.d_bbsize == 0)
685 lab.d_bbsize = BBSIZE;
686 if (lab.d_sbsize == 0)
687 lab.d_sbsize = SBLOCKSIZE;
688 interact(&lab, f);
689 break;
690 #endif /* !NO_INTERACT */
691
692 case READ:
693 if (argc != 1)
694 usage();
695 read_all = Aflag;
696 readlabel(f);
697 if (read_all)
698 /* Label got printed in the bowels of readlabel */
699 break;
700 if (tflag)
701 makedisktab(stdout, &lab);
702 else {
703 showinfo(stdout, &lab, specname);
704 showpartitions(stdout, &lab, Cflag);
705 }
706 error = checklabel(&lab);
707 if (error)
708 error += 100;
709 break;
710
711 case RESTORE:
712 if (argc != 2)
713 usage();
714 if (!(t = fopen(argv[1], "r")))
715 err(4, "%s", argv[1]);
716 if (getasciilabel(t, &lab))
717 error = write_label(f);
718 else
719 error = 1;
720 break;
721
722 case SETREADONLY:
723 writable = 0;
724 goto do_diocwlabel;
725 case SETWRITABLE:
726 writable = 1;
727 do_diocwlabel:
728 if (argc != 1)
729 usage();
730 if (dk_ioctl(f, DIOCWLABEL, &writable) < 0)
731 err(4, "ioctl DIOCWLABEL");
732 break;
733
734 case WRITE: /* Create label from /etc/disktab entry & write */
735 if (argc < 2 || argc > 3)
736 usage();
737 makelabel(argv[1], argv[2]);
738 if (checklabel(&lab) == 0)
739 error = write_label(f);
740 else
741 error = 1;
742 break;
743
744 case UNSPEC:
745 usage();
746
747 }
748 exit(error);
749 }
750
751 /*
752 * Construct a prototype disklabel from /etc/disktab.
753 */
754 static void
755 makelabel(const char *type, const char *name)
756 {
757 struct disklabel *dp;
758
759 dp = getdiskbyname(type);
760 if (dp == NULL)
761 errx(1, "unknown disk type: %s", type);
762 lab = *dp;
763
764 /* d_packname is union d_boot[01], so zero */
765 (void)memset(lab.d_packname, 0, sizeof(lab.d_packname));
766 if (name)
767 (void)strncpy(lab.d_packname, name, sizeof(lab.d_packname));
768 }
769
770 static int
771 write_label(int f)
772 {
773 int writable;
774
775 lab.d_magic = DISKMAGIC;
776 lab.d_magic2 = DISKMAGIC;
777 lab.d_checksum = 0;
778 lab.d_checksum = dkcksum(&lab);
779
780 if (rflag) {
781 /* Write the label directly to the disk */
782
783 /*
784 * First set the kernel disk label,
785 * then write a label to the raw disk.
786 * If the SDINFO ioctl fails because it is unimplemented,
787 * keep going; otherwise, the kernel consistency checks
788 * may prevent us from changing the current (in-core)
789 * label.
790 */
791 if (!Fflag && dk_ioctl(f, DIOCSDINFO, &lab) < 0 &&
792 errno != ENODEV && errno != ENOTTY) {
793 l_perror("ioctl DIOCSDINFO");
794 return (1);
795 }
796 /*
797 * write enable label sector before write (if necessary),
798 * disable after writing.
799 */
800 writable = 1;
801 if (!Fflag) {
802 if (dk_ioctl(f, DIOCWLABEL, &writable) < 0)
803 perror("ioctl DIOCWLABEL");
804 set_writable_fd = f;
805 atexit(clear_writable);
806 }
807
808 writelabel_direct(f);
809
810 /*
811 * Now issue a DIOCWDINFO. This will let the kernel convert the
812 * disklabel to some machdep format if needed.
813 */
814 /* XXX: This is stupid! */
815 if (!Fflag && dk_ioctl(f, DIOCWDINFO, &lab) < 0) {
816 l_perror("ioctl DIOCWDINFO");
817 return (1);
818 }
819 } else {
820 /* Get the kernel to write the label */
821 if (dk_ioctl(f, DIOCWDINFO, &lab) < 0) {
822 l_perror("ioctl DIOCWDINFO");
823 return (1);
824 }
825 }
826
827 #ifdef VAX_ALTLABELS
828 if (lab.d_type == DKTYPE_SMD && lab.d_flags & D_BADSECT &&
829 lab.d_secsize == 512) {
830 /* Write the label to the odd sectors of the last track! */
831 daddr_t alt;
832 int i;
833 uint8_t sec0[512];
834
835 if (pread(f, sec0, 512, 0) < 512) {
836 warn("read master label to write alternates");
837 return 0;
838 }
839
840 alt = lab.d_ncylinders * lab.d_secpercyl - lab.d_nsectors;
841 for (i = 1; i < 11 && (uint32_t)i < lab.d_nsectors; i += 2) {
842 if (pwrite(f, sec0, 512, (off_t)(alt + i) * 512) < 512)
843 warn("alternate label %d write", i/2);
844 }
845 }
846 #endif /* VAX_ALTLABELS */
847
848 return 0;
849 }
850
851 int
852 writelabel(int f, struct disklabel *lp)
853 {
854 if (lp != &lab)
855 lab = *lp;
856 return write_label(f);
857 }
858
859 static void
860 l_perror(const char *s)
861 {
862
863 switch (errno) {
864
865 case ESRCH:
866 warnx("%s: No disk label on disk;\n"
867 "use \"disklabel -I\" to install initial label", s);
868 break;
869
870 case EINVAL:
871 warnx("%s: Label magic number or checksum is wrong!\n"
872 "(disklabel or kernel is out of date?)", s);
873 break;
874
875 case EBUSY:
876 warnx("%s: Open partition would move or shrink", s);
877 break;
878
879 case EXDEV:
880 warnx("%s: Labeled partition or 'a' partition must start"
881 " at beginning of disk", s);
882 break;
883
884 default:
885 warn("%s", s);
886 break;
887 }
888 }
889
890 #ifdef NO_MBR_SUPPORT
891 #define process_mbr(f, action) 1
892 #else
893 /*
894 * Scan DOS/MBR partition table and extended partition list for NetBSD ptns.
895 */
896 static int
897 process_mbr(int f, int (*action)(int, u_int))
898 {
899 struct mbr_partition *dp;
900 struct mbr_sector mbr;
901 int rval = 1, res;
902 int part;
903 u_int ext_base, next_ext, this_ext, start;
904
905 ext_base = 0;
906 next_ext = 0;
907 for (;;) {
908 this_ext = next_ext;
909 next_ext = 0;
910 if (verbose > 1)
911 warnx("reading mbr sector %u", this_ext);
912 if (pread(f, &mbr, sizeof mbr, this_ext * (off_t)DEV_BSIZE)
913 != sizeof(mbr)) {
914 if (verbose)
915 warn("Can't read master boot record %u",
916 this_ext);
917 break;
918 }
919
920 /* Check if table is valid. */
921 if (mbr.mbr_magic != htole16(MBR_MAGIC)) {
922 if (verbose)
923 warnx("Invalid signature in mbr record %u",
924 this_ext);
925 break;
926 }
927
928 dp = &mbr.mbr_parts[0];
929
930 /* Find NetBSD partition(s). */
931 for (part = 0; part < MBR_PART_COUNT; dp++, part++) {
932 start = le32toh(dp->mbrp_start);
933 switch (dp->mbrp_type) {
934 #ifdef COMPAT_386BSD_MBRPART
935 case MBR_PTYPE_386BSD:
936 if (ext_base != 0)
937 break;
938 /* FALLTHROUGH */
939 #endif
940 case MBR_PTYPE_NETBSD:
941 res = action(f, this_ext + start);
942 if (res <= 0)
943 /* Found or failure */
944 return res;
945 if (res > rval)
946 /* Keep largest value */
947 rval = res;
948 break;
949 case MBR_PTYPE_EXT:
950 case MBR_PTYPE_EXT_LBA:
951 case MBR_PTYPE_EXT_LNX:
952 next_ext = start;
953 break;
954 default:
955 break;
956 }
957 }
958 if (next_ext == 0)
959 /* No more extended partitions */
960 break;
961 next_ext += ext_base;
962 if (ext_base == 0)
963 ext_base = next_ext;
964
965 if (next_ext <= this_ext) {
966 if (verbose)
967 warnx("Invalid extended chain %x <= %x",
968 next_ext, this_ext);
969 break;
970 }
971 /* Maybe we should check against the disk size... */
972 }
973
974 return rval;
975 }
976
977 static int
978 readlabel_mbr(int f, u_int sector)
979 {
980 struct disklabel *disk_lp;
981
982 disk_lp = find_label(f, sector);
983 if (disk_lp == NULL)
984 return 1;
985 targettohlabel(&lab, disk_lp);
986 return 0;
987 }
988
989 static int
990 writelabel_mbr(int f, u_int sector)
991 {
992 return update_label(f, sector, mflag ? LABELOFFSET_MBR : ~0U) ? 2 : 0;
993 }
994
995 #endif /* !NO_MBR_SUPPORT */
996
997 #ifndef USE_ACORN
998 #define get_filecore_partition(f) 0
999 #else
1000 /*
1001 * static int filecore_checksum(u_char *bootblock)
1002 *
1003 * Calculates the filecore boot block checksum. This is used to validate
1004 * a filecore boot block on the disk. If a boot block is validated then
1005 * it is used to locate the partition table. If the boot block is not
1006 * validated, it is assumed that the whole disk is NetBSD.
1007 *
1008 * The basic algorithm is:
1009 *
1010 * for (each byte in block, excluding checksum) {
1011 * sum += byte;
1012 * if (sum > 255)
1013 * sum -= 255;
1014 * }
1015 *
1016 * That's equivalent to summing all of the bytes in the block
1017 * (excluding the checksum byte, of course), then calculating the
1018 * checksum as "cksum = sum - ((sum - 1) / 255) * 255)". That
1019 * expression may or may not yield a faster checksum function,
1020 * but it's easier to reason about.
1021 *
1022 * Note that if you have a block filled with bytes of a single
1023 * value "X" (regardless of that value!) and calculate the cksum
1024 * of the block (excluding the checksum byte), you will _always_
1025 * end up with a checksum of X. (Do the math; that can be derived
1026 * from the checksum calculation function!) That means that
1027 * blocks which contain bytes which all have the same value will
1028 * always checksum properly. That's a _very_ unlikely occurence
1029 * (probably impossible, actually) for a valid filecore boot block,
1030 * so we treat such blocks as invalid.
1031 */
1032 static int
1033 filecore_checksum(u_char *bootblock)
1034 {
1035 u_char byte0, accum_diff;
1036 u_int sum;
1037 int i;
1038
1039 sum = 0;
1040 accum_diff = 0;
1041 byte0 = bootblock[0];
1042
1043 /*
1044 * Sum the contents of the block, keeping track of whether
1045 * or not all bytes are the same. If 'accum_diff' ends up
1046 * being zero, all of the bytes are, in fact, the same.
1047 */
1048 for (i = 0; i < 511; ++i) {
1049 sum += bootblock[i];
1050 accum_diff |= bootblock[i] ^ byte0;
1051 }
1052
1053 /*
1054 * Check to see if the checksum byte is the same as the
1055 * rest of the bytes, too. (Note that if all of the bytes
1056 * are the same except the checksum, a checksum compare
1057 * won't succeed, but that's not our problem.)
1058 */
1059 accum_diff |= bootblock[i] ^ byte0;
1060
1061 /* All bytes in block are the same; call it invalid. */
1062 if (accum_diff == 0)
1063 return (-1);
1064
1065 return (sum - ((sum - 1) / 255) * 255);
1066 }
1067
1068 /*
1069 * Check for the presence of a RiscOS filecore boot block
1070 * indicating an ADFS file system on the disc.
1071 * Return the offset to the NetBSD part of the disc if
1072 * this can be determined.
1073 * This routine will terminate disklabel if the disc
1074 * is found to be ADFS only.
1075 */
1076 static u_int
1077 get_filecore_partition(int f)
1078 {
1079 struct filecore_bootblock *fcbb;
1080 static u_char bb[DEV_BSIZE];
1081 u_int offset;
1082 struct riscix_partition_table *riscix_part;
1083 int loop;
1084
1085 if (pread(f, bb, sizeof(bb), (off_t)FILECORE_BOOT_SECTOR * DEV_BSIZE) != sizeof(bb))
1086 err(4, "can't read filecore boot block");
1087 fcbb = (struct filecore_bootblock *)bb;
1088
1089 /* Check if table is valid. */
1090 if (filecore_checksum(bb) != fcbb->checksum)
1091 return (0);
1092
1093 /*
1094 * Check for NetBSD/arm32 (RiscBSD) partition marker.
1095 * If found the NetBSD disklabel location is easy.
1096 */
1097 offset = (fcbb->partition_cyl_low + (fcbb->partition_cyl_high << 8))
1098 * fcbb->heads * fcbb->secspertrack;
1099
1100 switch (fcbb->partition_type) {
1101
1102 case PARTITION_FORMAT_RISCBSD:
1103 return (offset);
1104
1105 case PARTITION_FORMAT_RISCIX:
1106 /*
1107 * Read the RISCiX partition table and search for the
1108 * first partition named "RiscBSD", "NetBSD", or "Empty:"
1109 *
1110 * XXX is use of 'Empty:' really desirable?! -- cgd
1111 */
1112
1113 if (pread(f, bb, sizeof(bb), (off_t)offset * DEV_BSIZE) != sizeof(bb))
1114 err(4, "can't read riscix partition table");
1115 riscix_part = (struct riscix_partition_table *)bb;
1116
1117 for (loop = 0; loop < NRISCIX_PARTITIONS; ++loop) {
1118 if (strcmp((char *)riscix_part->partitions[loop].rp_name,
1119 "RiscBSD") == 0 ||
1120 strcmp((char *)riscix_part->partitions[loop].rp_name,
1121 "NetBSD") == 0 ||
1122 strcmp((char *)riscix_part->partitions[loop].rp_name,
1123 "Empty:") == 0) {
1124 return riscix_part->partitions[loop].rp_start;
1125 break;
1126 }
1127 }
1128 /*
1129 * Valid filecore boot block, RISCiX partition table
1130 * but no NetBSD partition. We should leave this
1131 * disc alone.
1132 */
1133 errx(4, "cannot label: no NetBSD partition found"
1134 " in RISCiX partition table");
1135
1136 default:
1137 /*
1138 * Valid filecore boot block and no non-ADFS partition.
1139 * This means that the whole disc is allocated for ADFS
1140 * so do not trash ! If the user really wants to put a
1141 * NetBSD disklabel on the disc then they should remove
1142 * the filecore boot block first with dd.
1143 */
1144 errx(4, "cannot label: filecore-only disk"
1145 " (no non-ADFS partition)");
1146 }
1147 return (0);
1148 }
1149 #endif /* USE_ACORN */
1150
1151 /*
1152 * Fetch disklabel for disk to 'lab'.
1153 * Use ioctl to get label unless -r flag is given.
1154 */
1155 static void
1156 readlabel(int f)
1157 {
1158 if (rflag) {
1159 /* Get label directly from disk */
1160 if (readlabel_direct(f) == 0)
1161 return;
1162 /*
1163 * There was no label on the disk. Get the fictious one
1164 * as a basis for initialisation.
1165 */
1166 if (!Fflag && Iflag && (dk_ioctl(f, DIOCGDINFO, &lab) == 0 ||
1167 dk_ioctl(f, DIOCGDEFLABEL, &lab) == 0))
1168 return;
1169 } else {
1170 /* Get label from kernel. */
1171 if (dk_ioctl(f, DIOCGDINFO, &lab) < 0)
1172 err(4, "ioctl DIOCGDINFO");
1173 return;
1174 }
1175
1176 if (read_all == 2)
1177 /* We actually found one, and printed it... */
1178 exit(0);
1179 errx(1, "could not read existing label");
1180 }
1181
1182 /*
1183 * Reading the label from the disk is largely a case of 'hunt the label'.
1184 * and since different architectures default to different places there
1185 * could even be more than one label that contradict each other!
1186 * For now we look in the expected place, then search through likely
1187 * other locations.
1188 */
1189 static struct disklabel *
1190 find_label(int f, u_int sector)
1191 {
1192 struct disklabel *disk_lp, hlp;
1193 int i;
1194 off_t offset;
1195 const char *is_deleted;
1196
1197 bootarea_len = pread(f, bootarea, sizeof bootarea,
1198 sector * (off_t)DEV_BSIZE);
1199 if (bootarea_len <= 0) {
1200 if (verbose)
1201 warn("failed to read bootarea from sector %u", sector);
1202 return NULL;
1203 }
1204
1205 if (verbose > 2)
1206 warnx("read sector %u len %d looking for label",
1207 sector, bootarea_len);
1208
1209 /* Check expected offset first */
1210 for (offset = LABEL_OFFSET, i = -4;; offset = i += 4) {
1211 is_deleted = "";
1212 disk_lp = (void *)(bootarea + offset);
1213 if (i == LABEL_OFFSET)
1214 continue;
1215 if ((char *)(disk_lp + 1) > bootarea + bootarea_len)
1216 break;
1217 if (disk_lp->d_magic2 != disk_lp->d_magic)
1218 continue;
1219 if (read_all && (disk_lp->d_magic == DISKMAGIC_DELETED ||
1220 disk_lp->d_magic == DISKMAGIC_DELETED_REV)) {
1221 disk_lp->d_magic ^= ~0u;
1222 disk_lp->d_magic2 ^= ~0u;
1223 is_deleted = "deleted ";
1224 }
1225 if (target32toh(disk_lp->d_magic) != DISKMAGIC) {
1226 /* XXX: Do something about byte-swapped labels ? */
1227 if (target32toh(disk_lp->d_magic) == DISKMAGIC_REV &&
1228 target32toh(disk_lp->d_magic2) == DISKMAGIC_REV)
1229 warnx("ignoring %sbyteswapped label"
1230 " at offset %jd from sector %u",
1231 is_deleted, (intmax_t)offset, sector);
1232 continue;
1233 }
1234 if (target16toh(disk_lp->d_npartitions) > maxpartitions ||
1235 dkcksum_target(disk_lp) != 0) {
1236 if (verbose > 0)
1237 warnx("corrupt label found at offset %jd in "
1238 "sector %u", (intmax_t)offset, sector);
1239 continue;
1240 }
1241 if (verbose > 1)
1242 warnx("%slabel found at offset %jd from sector %u",
1243 is_deleted, (intmax_t)offset, sector);
1244 if (!read_all)
1245 return disk_lp;
1246
1247 /* To print all the labels we have to do it here */
1248 /* XXX: maybe we should compare them? */
1249 targettohlabel(&hlp, disk_lp);
1250 printf("# %ssector %u offset %jd bytes\n",
1251 is_deleted, sector, (intmax_t)offset);
1252 if (tflag)
1253 makedisktab(stdout, &hlp);
1254 else {
1255 showinfo(stdout, &hlp, specname);
1256 showpartitions(stdout, &hlp, Cflag);
1257 }
1258 checklabel(&hlp);
1259 htotargetlabel(disk_lp, &hlp);
1260 /* Remember we've found a label */
1261 read_all = 2;
1262 }
1263 return NULL;
1264 }
1265
1266 static void
1267 write_bootarea(int f, u_int sector)
1268 {
1269 int wlen;
1270
1271 if (bootarea_len <= 0)
1272 errx(1, "attempting to write after failed read");
1273
1274 #ifdef ALPHA_BOOTBLOCK_CKSUM
1275 /*
1276 * The Alpha requires that the boot block be checksummed.
1277 * <sys/bootblock.h> provides a macro to do it.
1278 */
1279 if (sector == 0) {
1280 struct alpha_boot_block *bb;
1281
1282 bb = (struct alpha_boot_block *)(void *)bootarea;
1283 bb->bb_cksum = 0;
1284 ALPHA_BOOT_BLOCK_CKSUM(bb, &bb->bb_cksum);
1285 }
1286 #endif /* ALPHA_BOOTBLOCK_CKSUM */
1287
1288 wlen = pwrite(f, bootarea, bootarea_len, sector * (off_t)DEV_BSIZE);
1289 if (wlen == bootarea_len)
1290 return;
1291 if (wlen == -1)
1292 err(1, "disklabel write (sector %u) size %d failed",
1293 sector, bootarea_len);
1294 errx(1, "disklabel write (sector %u) size %d truncated to %d",
1295 sector, bootarea_len, wlen);
1296 }
1297
1298 static int
1299 update_label(int f, u_int label_sector, u_int label_offset)
1300 {
1301 struct disklabel *disk_lp;
1302
1303 disk_lp = find_label(f, label_sector);
1304
1305 if (disk_lp && Dflag) {
1306 /* Invalidate the existing label */
1307 disk_lp->d_magic ^= ~0u;
1308 disk_lp->d_magic2 ^= ~0u;
1309 if (Dflag == 2)
1310 write_bootarea(f, label_sector);
1311 /* Force label to default location */
1312 disk_lp = NULL;
1313 }
1314
1315 if (Dflag == 2)
1316 /* We are just deleting the label */
1317 return 0;
1318
1319 if (disk_lp == NULL) {
1320 if (label_offset == ~0u)
1321 return 0;
1322 /* Nothing on the disk - we need to add it */
1323 disk_lp = (void *)(bootarea + label_offset);
1324 if ((char *)(disk_lp + 1) > bootarea + bootarea_len)
1325 errx(1, "no space in bootarea (sector %u) "
1326 "to create label", label_sector);
1327 }
1328
1329 htotargetlabel(disk_lp, &lab);
1330 write_bootarea(f, label_sector);
1331 return 1;
1332 }
1333
1334 static void
1335 writelabel_direct(int f)
1336 {
1337 u_int label_sector;
1338 int written = 0;
1339 int rval;
1340
1341 label_sector = get_filecore_partition(f);
1342 if (label_sector != 0)
1343 /* The offset needs to be that from the acorn ports... */
1344 written = update_label(f, label_sector, DEV_BSIZE);
1345
1346 rval = process_mbr(f, writelabel_mbr);
1347
1348 if (rval == 2 || written)
1349 /* Don't add a label to sector 0, but update one if there */
1350 update_label(f, 0, ~0u);
1351 else
1352 update_label(f, 0, LABEL_OFFSET);
1353 }
1354
1355 static int
1356 readlabel_direct(int f)
1357 {
1358 struct disklabel *disk_lp;
1359 u_int filecore_partition_offset;
1360
1361 filecore_partition_offset = get_filecore_partition(f);
1362 if (filecore_partition_offset != 0) {
1363 disk_lp = find_label(f, filecore_partition_offset);
1364 if (disk_lp != NULL) {
1365 targettohlabel(&lab, disk_lp);
1366 return 0;
1367 }
1368 }
1369
1370 if (mflag && process_mbr(f, readlabel_mbr) == 0)
1371 return 0;
1372
1373 disk_lp = find_label(f, 0);
1374 if (disk_lp != NULL) {
1375 targettohlabel(&lab, disk_lp);
1376 return 0;
1377 }
1378
1379 if (!mflag && process_mbr(f, readlabel_mbr) == 0)
1380 return 0;
1381
1382 return 1;
1383 }
1384
1385 static void
1386 makedisktab(FILE *f, struct disklabel *lp)
1387 {
1388 int i;
1389 const char *did;
1390 struct partition *pp;
1391
1392 did = "\\\n\t:";
1393 (void) fprintf(f, "%.*s|Automatically generated label:\\\n\t:dt=",
1394 (int) sizeof(lp->d_typename), lp->d_typename);
1395 if ((unsigned) lp->d_type < DKMAXTYPES)
1396 (void) fprintf(f, "%s:", dktypenames[lp->d_type]);
1397 else
1398 (void) fprintf(f, "unknown%" PRIu16 ":", lp->d_type);
1399
1400 (void) fprintf(f, "se#%" PRIu32 ":", lp->d_secsize);
1401 (void) fprintf(f, "ns#%" PRIu32 ":", lp->d_nsectors);
1402 (void) fprintf(f, "nt#%" PRIu32 ":", lp->d_ntracks);
1403 (void) fprintf(f, "sc#%" PRIu32 ":", lp->d_secpercyl);
1404 (void) fprintf(f, "nc#%" PRIu32 ":", lp->d_ncylinders);
1405
1406 if ((lp->d_secpercyl * lp->d_ncylinders) != lp->d_secperunit) {
1407 (void) fprintf(f, "%ssu#%" PRIu32 ":", did, lp->d_secperunit);
1408 did = "";
1409 }
1410 if (lp->d_rpm != 3600) {
1411 (void) fprintf(f, "%srm#%" PRIu16 ":", did, lp->d_rpm);
1412 did = "";
1413 }
1414 if (lp->d_interleave != 1) {
1415 (void) fprintf(f, "%sil#%" PRIu16 ":", did, lp->d_interleave);
1416 did = "";
1417 }
1418 if (lp->d_trackskew != 0) {
1419 (void) fprintf(f, "%ssk#%" PRIu16 ":", did, lp->d_trackskew);
1420 did = "";
1421 }
1422 if (lp->d_cylskew != 0) {
1423 (void) fprintf(f, "%scs#%" PRIu16 ":", did, lp->d_cylskew);
1424 did = "";
1425 }
1426 if (lp->d_headswitch != 0) {
1427 (void) fprintf(f, "%shs#%" PRIu32 ":", did, lp->d_headswitch);
1428 did = "";
1429 }
1430 if (lp->d_trkseek != 0) {
1431 (void) fprintf(f, "%sts#%" PRIu32 ":", did, lp->d_trkseek);
1432 did = "";
1433 }
1434 #ifdef notyet
1435 (void) fprintf(f, "drivedata: ");
1436 for (i = NDDATA - 1; i >= 0; i--)
1437 if (lp->d_drivedata[i])
1438 break;
1439 if (i < 0)
1440 i = 0;
1441 for (j = 0; j <= i; j++)
1442 (void) fprintf(f, "%" PRIu32 " ", lp->d_drivedata[j]);
1443 #endif /* notyet */
1444 pp = lp->d_partitions;
1445 for (i = 0; i < lp->d_npartitions; i++, pp++) {
1446 if (pp->p_size) {
1447 char c = 'a' + i;
1448 (void) fprintf(f, "\\\n\t:");
1449 (void) fprintf(f, "p%c#%" PRIu32 ":", c, pp->p_size);
1450 (void) fprintf(f, "o%c#%" PRIu32 ":", c, pp->p_offset);
1451 if (pp->p_fstype != FS_UNUSED) {
1452 if ((unsigned) pp->p_fstype < FSMAXTYPES)
1453 (void) fprintf(f, "t%c=%s:", c,
1454 fstypenames[pp->p_fstype]);
1455 else
1456 (void) fprintf(f,
1457 "t%c=unknown%" PRIu8 ":",
1458 c, pp->p_fstype);
1459 }
1460 switch (pp->p_fstype) {
1461
1462 case FS_UNUSED:
1463 break;
1464
1465 case FS_BSDFFS:
1466 case FS_BSDLFS:
1467 case FS_EX2FS:
1468 case FS_ADOS:
1469 case FS_APPLEUFS:
1470 (void) fprintf(f, "b%c#%" PRIu64 ":", c,
1471 (uint64_t)pp->p_fsize * pp->p_frag);
1472 (void) fprintf(f, "f%c#%" PRIu32 ":", c,
1473 pp->p_fsize);
1474 break;
1475 default:
1476 break;
1477 }
1478 }
1479 }
1480 (void) fprintf(f, "\n");
1481 (void) fflush(f);
1482 }
1483
1484 static int
1485 edit(int f)
1486 {
1487 const char *tmpdir;
1488 char tmpfil[MAXPATHLEN];
1489 int first, ch, fd;
1490 int get_ok;
1491 FILE *fp;
1492
1493 if ((tmpdir = getenv("TMPDIR")) == NULL)
1494 tmpdir = _PATH_TMP;
1495 (void)snprintf(tmpfil, sizeof(tmpfil), "%s/%s", tmpdir, TMPFILE);
1496 if ((fd = mkstemp(tmpfil)) == -1 || (fp = fdopen(fd, "w")) == NULL) {
1497 warn("%s", tmpfil);
1498 return (1);
1499 }
1500 (void)fchmod(fd, 0600);
1501 showinfo(fp, &lab, specname);
1502 showpartitions(fp, &lab, Cflag);
1503 (void) fclose(fp);
1504 for (;;) {
1505 if (!editit(tmpfil))
1506 break;
1507 fp = fopen(tmpfil, "r");
1508 if (fp == NULL) {
1509 warn("%s", tmpfil);
1510 break;
1511 }
1512 (void) memset(&lab, 0, sizeof(lab));
1513 get_ok = getasciilabel(fp, &lab);
1514 fclose(fp);
1515 if (get_ok && write_label(f) == 0) {
1516 (void) unlink(tmpfil);
1517 return (0);
1518 }
1519 (void) printf("re-edit the label? [y]: ");
1520 (void) fflush(stdout);
1521 first = ch = getchar();
1522 while (ch != '\n' && ch != EOF)
1523 ch = getchar();
1524 if (first == 'n' || first == 'N')
1525 break;
1526 }
1527 (void)unlink(tmpfil);
1528 return (1);
1529 }
1530
1531 static int
1532 editit(const char *tmpfil)
1533 {
1534 int pid, xpid;
1535 int status;
1536 sigset_t nsigset, osigset;
1537
1538 sigemptyset(&nsigset);
1539 sigaddset(&nsigset, SIGINT);
1540 sigaddset(&nsigset, SIGQUIT);
1541 sigaddset(&nsigset, SIGHUP);
1542 sigprocmask(SIG_BLOCK, &nsigset, &osigset);
1543 while ((pid = fork()) < 0) {
1544 if (errno != EAGAIN) {
1545 sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
1546 warn("fork");
1547 return (0);
1548 }
1549 sleep(1);
1550 }
1551 if (pid == 0) {
1552 const char *ed;
1553 char *buf;
1554 int retval;
1555
1556 sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
1557 setgid(getgid());
1558 setuid(getuid());
1559 if ((ed = getenv("EDITOR")) == (char *)0)
1560 ed = DEFEDITOR;
1561 /*
1562 * Jump through a few extra hoops in case someone's editor
1563 * is "editor arg1 arg2".
1564 */
1565 asprintf(&buf, "%s %s", ed, tmpfil);
1566 if (!buf)
1567 err(1, "malloc");
1568 retval = execlp(_PATH_BSHELL, _PATH_BSHELL, "-c", buf, NULL);
1569 if (retval == -1)
1570 perror(ed);
1571 exit(retval);
1572 }
1573 while ((xpid = wait(&status)) >= 0)
1574 if (xpid == pid)
1575 break;
1576 sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
1577 return (!status);
1578 }
1579
1580 static char *
1581 skip(char *cp)
1582 {
1583
1584 cp += strspn(cp, " \t");
1585 if (*cp == '\0')
1586 return (NULL);
1587 return (cp);
1588 }
1589
1590 static char *
1591 word(char *cp)
1592 {
1593
1594 if (cp == NULL || *cp == '\0')
1595 return (NULL);
1596
1597 cp += strcspn(cp, " \t");
1598 if (*cp == '\0')
1599 return (NULL);
1600 *cp++ = '\0';
1601 cp += strspn(cp, " \t");
1602 if (*cp == '\0')
1603 return (NULL);
1604 return (cp);
1605 }
1606
1607 #define _CHECKLINE \
1608 if (tp == NULL || *tp == '\0') { \
1609 warnx("line %d: too few fields", lineno); \
1610 errors++; \
1611 break; \
1612 }
1613
1614 #define __CHECKLINE \
1615 if (*tp == NULL || **tp == '\0') { \
1616 warnx("line %d: too few fields", lineno); \
1617 *tp = _error_; \
1618 return 0; \
1619 }
1620
1621 static char _error_[] = "";
1622 #define NXTNUM(n) if ((n = nxtnum(&tp, lineno),0) + tp != _error_) \
1623 ; else goto error
1624 #define NXTXNUM(n) if ((n = nxtxnum(&tp, lp, lineno),0) + tp != _error_) \
1625 ; else goto error
1626
1627 static unsigned long
1628 nxtnum(char **tp, int lineno)
1629 {
1630 char *cp;
1631 unsigned long v;
1632
1633 __CHECKLINE
1634 if (getulong(*tp, '\0', &cp, &v, UINT32_MAX) != 0) {
1635 warnx("line %d: syntax error", lineno);
1636 *tp = _error_;
1637 return 0;
1638 }
1639 *tp = cp;
1640 return v;
1641 }
1642
1643 static unsigned long
1644 nxtxnum(char **tp, struct disklabel *lp, int lineno)
1645 {
1646 char *cp, *ncp;
1647 unsigned long n, v;
1648
1649 __CHECKLINE
1650 cp = *tp;
1651 if (getulong(cp, '/', &ncp, &n, UINT32_MAX) != 0)
1652 goto bad;
1653
1654 if (*ncp == '/') {
1655 n *= lp->d_secpercyl;
1656 cp = ncp + 1;
1657 if (getulong(cp, '/', &ncp, &v, UINT32_MAX) != 0)
1658 goto bad;
1659 n += v * lp->d_nsectors;
1660 cp = ncp + 1;
1661 if (getulong(cp, '\0', &ncp, &v, UINT32_MAX) != 0)
1662 goto bad;
1663 n += v;
1664 }
1665 *tp = ncp;
1666 return n;
1667 bad:
1668 warnx("line %d: invalid format", lineno);
1669 *tp = _error_;
1670 return 0;
1671 }
1672
1673 /*
1674 * Read an ascii label in from fd f,
1675 * in the same format as that put out by showinfo() and showpartitions(),
1676 * and fill in lp.
1677 */
1678 static int
1679 getasciilabel(FILE *f, struct disklabel *lp)
1680 {
1681 const char *const *cpp, *s;
1682 struct partition *pp;
1683 char *cp, *tp, line[BUFSIZ], tbuf[15];
1684 int lineno, errors;
1685 unsigned long v;
1686 unsigned int part;
1687
1688 lineno = 0;
1689 errors = 0;
1690 lp->d_bbsize = BBSIZE; /* XXX */
1691 lp->d_sbsize = SBLOCKSIZE; /* XXX */
1692 while (fgets(line, sizeof(line) - 1, f)) {
1693 lineno++;
1694 if ((cp = strpbrk(line, "#\r\n")) != NULL)
1695 *cp = '\0';
1696 cp = skip(line);
1697 if (cp == NULL) /* blank line or comment line */
1698 continue;
1699 tp = strchr(cp, ':'); /* everything has a colon in it */
1700 if (tp == NULL) {
1701 warnx("line %d: syntax error", lineno);
1702 errors++;
1703 continue;
1704 }
1705 *tp++ = '\0', tp = skip(tp);
1706 if (!strcmp(cp, "type")) {
1707 if (tp == NULL) {
1708 strlcpy(tbuf, "unknown", sizeof(tbuf));
1709 tp = tbuf;
1710 }
1711 cpp = dktypenames;
1712 for (; cpp < &dktypenames[DKMAXTYPES]; cpp++)
1713 if ((s = *cpp) && !strcasecmp(s, tp)) {
1714 lp->d_type = cpp - dktypenames;
1715 goto next;
1716 }
1717 if (GETNUM16(tp, &v) != 0) {
1718 warnx("line %d: syntax error", lineno);
1719 errors++;
1720 continue;
1721 }
1722 if (v >= DKMAXTYPES)
1723 warnx("line %d: warning, unknown disk type: %s",
1724 lineno, tp);
1725 lp->d_type = v;
1726 continue;
1727 }
1728 if (!strcmp(cp, "flags")) {
1729 for (v = 0; (cp = tp) && *cp != '\0';) {
1730 tp = word(cp);
1731 if (!strcasecmp(cp, "removable"))
1732 v |= D_REMOVABLE;
1733 else if (!strcasecmp(cp, "ecc"))
1734 v |= D_ECC;
1735 else if (!strcasecmp(cp, "badsect"))
1736 v |= D_BADSECT;
1737 else {
1738 warnx("line %d: bad flag: %s",
1739 lineno, cp);
1740 errors++;
1741 }
1742 }
1743 lp->d_flags = v;
1744 continue;
1745 }
1746 if (!strcmp(cp, "drivedata")) {
1747 int i;
1748
1749 for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) {
1750 if (GETNUM32(cp, &v) != 0) {
1751 warnx("line %d: bad drive data",
1752 lineno);
1753 errors++;
1754 } else
1755 lp->d_drivedata[i] = v;
1756 i++;
1757 tp = word(cp);
1758 }
1759 continue;
1760 }
1761 if (sscanf(cp, "%lu partitions", &v) == 1) {
1762 if (v == 0 || v > maxpartitions) {
1763 warnx("line %d: bad # of partitions", lineno);
1764 lp->d_npartitions = maxpartitions;
1765 errors++;
1766 } else
1767 lp->d_npartitions = v;
1768 continue;
1769 }
1770 if (tp == NULL) {
1771 tbuf[0] = '\0';
1772 tp = tbuf;
1773 }
1774 if (!strcmp(cp, "disk")) {
1775 strncpy(lp->d_typename, tp, sizeof(lp->d_typename));
1776 continue;
1777 }
1778 if (!strcmp(cp, "label")) {
1779 strncpy(lp->d_packname, tp, sizeof(lp->d_packname));
1780 continue;
1781 }
1782 if (!strcmp(cp, "bytes/sector")) {
1783 if (GETNUM32(tp, &v) != 0 || v <= 0 || (v % 512) != 0) {
1784 warnx("line %d: bad %s: %s", lineno, cp, tp);
1785 errors++;
1786 } else
1787 lp->d_secsize = v;
1788 continue;
1789 }
1790 if (!strcmp(cp, "sectors/track")) {
1791 if (GETNUM32(tp, &v) != 0) {
1792 warnx("line %d: bad %s: %s", lineno, cp, tp);
1793 errors++;
1794 } else
1795 lp->d_nsectors = v;
1796 continue;
1797 }
1798 if (!strcmp(cp, "sectors/cylinder")) {
1799 if (GETNUM32(tp, &v) != 0) {
1800 warnx("line %d: bad %s: %s", lineno, cp, tp);
1801 errors++;
1802 } else
1803 lp->d_secpercyl = v;
1804 continue;
1805 }
1806 if (!strcmp(cp, "tracks/cylinder")) {
1807 if (GETNUM32(tp, &v) != 0) {
1808 warnx("line %d: bad %s: %s", lineno, cp, tp);
1809 errors++;
1810 } else
1811 lp->d_ntracks = v;
1812 continue;
1813 }
1814 if (!strcmp(cp, "cylinders")) {
1815 if (GETNUM32(tp, &v) != 0) {
1816 warnx("line %d: bad %s: %s", lineno, cp, tp);
1817 errors++;
1818 } else
1819 lp->d_ncylinders = v;
1820 continue;
1821 }
1822 if (!strcmp(cp, "total sectors") ||
1823 !strcmp(cp, "sectors/unit")) {
1824 if (GETNUM32(tp, &v) != 0) {
1825 warnx("line %d: bad %s: %s", lineno, cp, tp);
1826 errors++;
1827 } else
1828 lp->d_secperunit = v;
1829 continue;
1830 }
1831 if (!strcmp(cp, "rpm")) {
1832 if (GETNUM16(tp, &v) != 0) {
1833 warnx("line %d: bad %s: %s", lineno, cp, tp);
1834 errors++;
1835 } else
1836 lp->d_rpm = v;
1837 continue;
1838 }
1839 if (!strcmp(cp, "interleave")) {
1840 if (GETNUM16(tp, &v) != 0) {
1841 warnx("line %d: bad %s: %s", lineno, cp, tp);
1842 errors++;
1843 } else
1844 lp->d_interleave = v;
1845 continue;
1846 }
1847 if (!strcmp(cp, "trackskew")) {
1848 if (GETNUM16(tp, &v) != 0) {
1849 warnx("line %d: bad %s: %s", lineno, cp, tp);
1850 errors++;
1851 } else
1852 lp->d_trackskew = v;
1853 continue;
1854 }
1855 if (!strcmp(cp, "cylinderskew")) {
1856 if (GETNUM16(tp, &v) != 0) {
1857 warnx("line %d: bad %s: %s", lineno, cp, tp);
1858 errors++;
1859 } else
1860 lp->d_cylskew = v;
1861 continue;
1862 }
1863 if (!strcmp(cp, "headswitch")) {
1864 if (GETNUM32(tp, &v) != 0) {
1865 warnx("line %d: bad %s: %s", lineno, cp, tp);
1866 errors++;
1867 } else
1868 lp->d_headswitch = v;
1869 continue;
1870 }
1871 if (!strcmp(cp, "track-to-track seek")) {
1872 if (GETNUM32(tp, &v) != 0) {
1873 warnx("line %d: bad %s: %s", lineno, cp, tp);
1874 errors++;
1875 } else
1876 lp->d_trkseek = v;
1877 continue;
1878 }
1879 if ('a' > *cp || *cp > 'z' || cp[1] != '\0') {
1880 warnx("line %d: unknown field: %s", lineno, cp);
1881 errors++;
1882 continue;
1883 }
1884
1885 /* We have a partition entry */
1886 part = *cp - 'a';
1887
1888 if (part >= maxpartitions) {
1889 warnx("line %d: bad partition name: %s", lineno, cp);
1890 errors++;
1891 continue;
1892 }
1893 if (part >= __arraycount(lp->d_partitions)) {
1894 warnx("line %d: partition id %s, >= %zu", lineno,
1895 cp, __arraycount(lp->d_partitions));
1896 errors++;
1897 continue;
1898 }
1899 pp = &lp->d_partitions[part];
1900
1901 NXTXNUM(pp->p_size);
1902 NXTXNUM(pp->p_offset);
1903 /* can't use word() here because of blanks in fstypenames[] */
1904 tp += strspn(tp, " \t");
1905 _CHECKLINE
1906 cp = tp;
1907 cpp = fstypenames;
1908 for (; cpp < &fstypenames[FSMAXTYPES]; cpp++) {
1909 s = *cpp;
1910 if (s == NULL ||
1911 (cp[strlen(s)] != ' ' &&
1912 cp[strlen(s)] != '\t' &&
1913 cp[strlen(s)] != '\0'))
1914 continue;
1915 if (!memcmp(s, cp, strlen(s))) {
1916 pp->p_fstype = cpp - fstypenames;
1917 tp += strlen(s);
1918 if (*tp == '\0')
1919 tp = NULL;
1920 else {
1921 tp += strspn(tp, " \t");
1922 if (*tp == '\0')
1923 tp = NULL;
1924 }
1925 goto gottype;
1926 }
1927 }
1928 tp = word(cp);
1929 if (isdigit(*cp & 0xff)) {
1930 if (GETNUM8(cp, &v) != 0) {
1931 warnx("line %d: syntax error", lineno);
1932 errors++;
1933 }
1934 } else
1935 v = FSMAXTYPES;
1936 if ((unsigned)v >= FSMAXTYPES) {
1937 warnx("line %d: warning, unknown file system type: %s",
1938 lineno, cp);
1939 warnx("tip: use -l to see all valid file system "
1940 "types");
1941 v = FS_UNUSED;
1942 }
1943 pp->p_fstype = v;
1944 gottype:
1945 switch (pp->p_fstype) {
1946
1947 case FS_UNUSED: /* XXX */
1948 NXTNUM(pp->p_fsize);
1949 if (pp->p_fsize == 0)
1950 break;
1951 NXTNUM(v);
1952 pp->p_frag = v / pp->p_fsize;
1953 break;
1954
1955 case FS_BSDFFS:
1956 case FS_ADOS:
1957 case FS_APPLEUFS:
1958 NXTNUM(pp->p_fsize);
1959 if (pp->p_fsize == 0)
1960 break;
1961 NXTNUM(v);
1962 pp->p_frag = v / pp->p_fsize;
1963 NXTNUM(pp->p_cpg);
1964 break;
1965 case FS_BSDLFS:
1966 NXTNUM(pp->p_fsize);
1967 if (pp->p_fsize == 0)
1968 break;
1969 NXTNUM(v);
1970 pp->p_frag = v / pp->p_fsize;
1971 NXTNUM(pp->p_sgs);
1972 break;
1973 case FS_EX2FS:
1974 NXTNUM(pp->p_fsize);
1975 if (pp->p_fsize == 0)
1976 break;
1977 NXTNUM(v);
1978 pp->p_frag = v / pp->p_fsize;
1979 break;
1980 case FS_ISO9660:
1981 NXTNUM(pp->p_cdsession);
1982 break;
1983 default:
1984 break;
1985 }
1986 continue;
1987 error:
1988 errors++;
1989 next:
1990 ;
1991 }
1992 errors += checklabel(lp);
1993 return (errors == 0);
1994 }
1995
1996 /*
1997 * Check disklabel for errors and fill in
1998 * derived fields according to supplied values.
1999 */
2000 int
2001 checklabel(struct disklabel *lp)
2002 {
2003 struct partition *pp, *qp;
2004 int i, j, errors;
2005 char part;
2006
2007 errors = 0;
2008 if (lp->d_secsize == 0) {
2009 warnx("sector size %" PRIu32, lp->d_secsize);
2010 return (1);
2011 }
2012 if (lp->d_nsectors == 0) {
2013 warnx("sectors/track %" PRIu32, lp->d_nsectors);
2014 return (1);
2015 }
2016 if (lp->d_ntracks == 0) {
2017 warnx("tracks/cylinder %" PRIu32, lp->d_ntracks);
2018 return (1);
2019 }
2020 if (lp->d_ncylinders == 0) {
2021 warnx("cylinders/unit %" PRIu32, lp->d_ncylinders);
2022 errors++;
2023 }
2024 if (lp->d_rpm == 0)
2025 warnx("warning, revolutions/minute %" PRIu16, lp->d_rpm);
2026 if (lp->d_secpercyl == 0)
2027 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
2028 if (lp->d_secperunit == 0)
2029 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
2030 if (lp->d_bbsize == 0) {
2031 warnx("boot block size %" PRIu32, lp->d_bbsize);
2032 errors++;
2033 } else if (lp->d_bbsize % lp->d_secsize)
2034 warnx("warning, boot block size %% sector-size != 0");
2035 if (lp->d_sbsize == 0) {
2036 warnx("super block size %" PRIu32, lp->d_sbsize);
2037 errors++;
2038 } else if (lp->d_sbsize % lp->d_secsize)
2039 warnx("warning, super block size %% sector-size != 0");
2040 if (lp->d_npartitions > maxpartitions)
2041 warnx("warning, number of partitions (%" PRIu16 ") > "
2042 "MAXPARTITIONS (%d)",
2043 lp->d_npartitions, maxpartitions);
2044 else
2045 for (i = maxpartitions - 1; i >= lp->d_npartitions; i--) {
2046 part = 'a' + i;
2047 pp = &lp->d_partitions[i];
2048 if (pp->p_size || pp->p_offset) {
2049 warnx("warning, partition %c increased "
2050 "number of partitions from %" PRIu16
2051 " to %d",
2052 part, lp->d_npartitions, i + 1);
2053 lp->d_npartitions = i + 1;
2054 break;
2055 }
2056 }
2057 for (i = 0; i < lp->d_npartitions; i++) {
2058 part = 'a' + i;
2059 pp = &lp->d_partitions[i];
2060 if (pp->p_size == 0 && pp->p_offset != 0)
2061 warnx("warning, partition %c: size 0, but "
2062 "offset %" PRIu32,
2063 part, pp->p_offset);
2064 #ifdef STRICT_CYLINDER_ALIGNMENT
2065 if (pp->p_offset % lp->d_secpercyl) {
2066 warnx("warning, partition %c:"
2067 " not starting on cylinder boundary",
2068 part);
2069 errors++;
2070 }
2071 #endif /* STRICT_CYLINDER_ALIGNMENT */
2072 if (pp->p_offset > lp->d_secperunit) {
2073 warnx("partition %c: offset past end of unit", part);
2074 errors++;
2075 }
2076 if (pp->p_offset + pp->p_size > lp->d_secperunit) {
2077 warnx("partition %c: partition extends"
2078 " past end of unit",
2079 part);
2080 errors++;
2081 }
2082 if (pp->p_fstype != FS_UNUSED)
2083 for (j = i + 1; j < lp->d_npartitions; j++) {
2084 qp = &lp->d_partitions[j];
2085 if (qp->p_fstype == FS_UNUSED)
2086 continue;
2087 if (pp->p_offset < qp->p_offset + qp->p_size &&
2088 qp->p_offset < pp->p_offset + pp->p_size)
2089 warnx("partitions %c and %c overlap",
2090 part, 'a' + j);
2091 }
2092 }
2093 return (errors);
2094 }
2095
2096 static void
2097 usage(void)
2098 {
2099 static const struct {
2100 const char *name;
2101 const char *expn;
2102 } usages[] = {
2103 { "[-ABCFMrtv] disk", "(to read label)" },
2104 { "-w [-BDFMrv] [-f disktab] disk disktype [packid]", "(to write label)" },
2105 { "-e [-BCDFMIrv] disk", "(to edit label)" },
2106 #if !defined(NO_INTERACT)
2107 { "-i [-BDFMIrv] disk", "(to create a label interactively)" },
2108 #endif
2109 { "-D [-v] disk", "(to delete existing label(s))" },
2110 { "-R [-BDFMrv] disk protofile", "(to restore label)" },
2111 { "[-NW] disk", "(to write disable/enable label)" },
2112 { "-l", "(to show all known file system types)" },
2113 { NULL, NULL }
2114 };
2115 int i;
2116 const char *pn = getprogname();
2117 const char *t = "usage:";
2118
2119 for (i = 0; usages[i].name != NULL; i++) {
2120 (void)fprintf(stderr, "%s %s %s\n\t%s\n",
2121 t, pn, usages[i].name, usages[i].expn);
2122 t = "or";
2123 }
2124 exit(1);
2125 }
2126
2127 static int
2128 getulong(const char *str, char sep, char **epp, unsigned long *ul,
2129 unsigned long max)
2130 {
2131 char *ep;
2132
2133 if (epp == NULL)
2134 epp = &ep;
2135
2136 *ul = strtoul(str, epp, 10);
2137
2138 if ((*ul == ULONG_MAX && errno == ERANGE) || *ul > max)
2139 return ERANGE;
2140
2141 if (*str == '\0' || (**epp != '\0' && **epp != sep &&
2142 !isspace((unsigned char)**epp)))
2143 return EFTYPE;
2144
2145 return 0;
2146 }
2147
2148 /*
2149 * This is a wrapper over the standard strcmp function to be used with
2150 * qsort on an array of pointers to strings.
2151 */
2152 static int
2153 qsort_strcmp(const void *v1, const void *v2)
2154 {
2155 const char *const *sp1 = (const char *const *)v1;
2156 const char *const *sp2 = (const char *const *)v2;
2157
2158 return strcmp(*sp1, *sp2);
2159 }
2160
2161 /*
2162 * Prints all know file system types for a partition.
2163 * Returns 1 on success, 0 on failure.
2164 */
2165 int
2166 list_fs_types(void)
2167 {
2168 int ret;
2169 size_t nelems;
2170
2171 nelems = 0;
2172 {
2173 const char *const *namep;
2174
2175 namep = fstypenames;
2176 while (*namep++ != NULL)
2177 nelems++;
2178 }
2179
2180 ret = 1;
2181 if (nelems > 0) {
2182 const char **list;
2183 size_t i;
2184
2185 list = (const char **)malloc(sizeof(char *) * nelems);
2186 if (list == NULL) {
2187 warnx("sorry, could not allocate memory for list");
2188 ret = 0;
2189 } else {
2190 for (i = 0; i < nelems; i++)
2191 list[i] = fstypenames[i];
2192
2193 qsort(list, nelems, sizeof(char *), qsort_strcmp);
2194
2195 for (i = 0; i < nelems; i++)
2196 (void)printf("%s\n", list[i]);
2197
2198 free(list);
2199 }
2200 }
2201
2202 return ret;
2203 }
2204
2205 #ifndef HAVE_NBTOOL_CONFIG_H
2206 int
2207 dk_ioctl(int f, u_long cmd, void *arg)
2208 {
2209 #if !defined(NATIVELABEL_ONLY)
2210 if (!native_p) {
2211 errno = ENOTTY;
2212 return -1;
2213 }
2214 #endif
2215 return ioctl(f, cmd, arg);
2216 }
2217 #endif
2218