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