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