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