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