1 /* $NetBSD: main.c,v 1.50 2018/06/27 01:14:48 kamil 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.50 2018/06/27 01:14:48 kamil Exp $"); 80 #endif 81 #endif /* not lint */ 82 83 #include <sys/param.h> 84 #include <sys/file.h> 85 #include <sys/stat.h> 86 #include <sys/wait.h> 87 #define DKTYPENAMES 88 #define FSTYPENAMES 89 90 #include <ctype.h> 91 #include <err.h> 92 #include <errno.h> 93 #include <signal.h> 94 #include <string.h> 95 #include <stdio.h> 96 #include <stdlib.h> 97 #include <limits.h> 98 #include <unistd.h> 99 100 #include <ufs/ufs/dinode.h> 101 #include <ufs/ffs/fs.h> 102 103 #if HAVE_NBTOOL_CONFIG_H 104 #include <nbinclude/sys/disklabel.h> 105 #include <nbinclude/sys/disklabel_acorn.h> 106 #include <nbinclude/sys/bootblock.h> 107 #include "../../include/disktab.h" 108 #else 109 #include <sys/ioctl.h> 110 #include <sys/disklabel.h> 111 #include <sys/disklabel_acorn.h> 112 #include <sys/bootblock.h> 113 #include <util.h> 114 #include <disktab.h> 115 #endif /* HAVE_NBTOOL_CONFIG_H */ 116 117 #include "pathnames.h" 118 #include "extern.h" 119 #include "dkcksum.h" 120 #include "bswap.h" 121 122 /* 123 * Disklabel: read and write disklabels. 124 * The label is usually placed on one of the first sectors of the disk. 125 * Many machines also place a bootstrap in the same area, 126 * in which case the label is embedded in the bootstrap. 127 * The bootstrap source must leave space at the proper offset 128 * for the label on such machines. 129 */ 130 131 #ifndef BBSIZE 132 #define BBSIZE 8192 /* size of boot area, with label */ 133 #endif 134 135 #define DISKMAGIC_REV bswap32(DISKMAGIC) 136 /* To delete a label, we just invert the magic numbers */ 137 #define DISKMAGIC_DELETED (~DISKMAGIC) 138 #define DISKMAGIC_DELETED_REV bswap32(~DISKMAGIC) 139 140 #define DEFEDITOR _PATH_VI 141 142 char specname[MAXPATHLEN]; 143 144 /* Some global data, all too hard to pass about */ 145 char bootarea[BBSIZE]; /* Buffer matching part of disk */ 146 int bootarea_len; /* Number of bytes we actually read */ 147 static struct disklabel lab; /* The label we have updated */ 148 149 static int Aflag; /* Action all labels */ 150 static int Fflag; /* Read/write from file */ 151 static int rflag; /* Read/write direct from disk */ 152 static int tflag; /* Format output as disktab */ 153 int Cflag; /* CHS format output */ 154 static int Dflag; /* Delete old labels (use with write) */ 155 static int Iflag; /* Read/write direct, but default if absent */ 156 static int lflag; /* List all known file system types and exit */ 157 static int mflag; /* Expect disk to contain an MBR */ 158 static int verbose; 159 static int read_all; /* set if op = READ && Aflag */ 160 161 static int write_label(int); 162 static int readlabel_direct(int); 163 static void writelabel_direct(int); 164 static int update_label(int, u_int, u_int); 165 static struct disklabel *find_label(int, u_int); 166 #if !defined(NATIVELABEL_ONLY) 167 static void getmachineparams(const char *); 168 #endif 169 170 static void makedisktab(FILE *, struct disklabel *); 171 static void makelabel(const char *, const char *); 172 static void l_perror(const char *); 173 static void readlabel(int); 174 static int edit(int); 175 static int editit(const char *); 176 static char *skip(char *); 177 static char *word(char *); 178 static int getasciilabel(FILE *, struct disklabel *); 179 __dead static void usage(void); 180 static int qsort_strcmp(const void *, const void *); 181 static int getulong(const char *, char, char **, 182 unsigned long *, unsigned long); 183 #define GETNUM32(a, v) getulong(a, '\0', NULL, v, UINT32_MAX) 184 #define GETNUM16(a, v) getulong(a, '\0', NULL, v, UINT16_MAX) 185 #define GETNUM8(a, v) getulong(a, '\0', NULL, v, UINT8_MAX) 186 187 static int set_writable_fd = -1; 188 189 #if !defined(NATIVELABEL_ONLY) 190 static u_int labeloffset; 191 static u_int labelsector; 192 static int labelusesmbr; 193 u_int maxpartitions; 194 static int byteorder; 195 196 static int biendian_p; 197 #ifndef HAVE_NBTOOL_CONFIG_H 198 static int native_p = 1; 199 #endif 200 int bswap_p; 201 202 static const struct disklabel_params { 203 const char *machine; 204 u_char labelusesmbr : 1; 205 u_char labelsector : 7; 206 u_char maxpartitions; 207 u_char raw_part; 208 u_char oldmaxpartitions; 209 u_short labeloffset; 210 u_short byteorder; 211 } disklabel_params[] = { 212 { "mvme68k", 0, 0, 8, 2, 0, 0, BIG_ENDIAN }, /* m68k */ 213 { "next68k", 0, 0, 8, 2, 0, 0, BIG_ENDIAN }, /* m68k */ 214 215 { "algor", 0, 0, 8, 2, 0, 64, LITTLE_ENDIAN }, /* mips */ 216 { "alpha", 0, 0, 8, 2, 0, 64, LITTLE_ENDIAN }, /* alpha */ 217 { "luna68k", 0, 0, 8, 2, 0, 64, BIG_ENDIAN }, /* m68k */ 218 { "mac68k", 0, 0, 8, 2, 0, 64, BIG_ENDIAN }, /* m68k */ 219 { "news68k", 0, 0, 8, 2, 0, 64, BIG_ENDIAN }, /* m68k */ 220 { "newsmips", 0, 0, 8, 2, 0, 64, BIG_ENDIAN }, /* mips */ 221 { "pmax", 0, 0, 8, 2, 0, 64, LITTLE_ENDIAN }, /* mips */ 222 { "sun2", 0, 0, 8, 2, 0, 64, BIG_ENDIAN }, /* m68k */ 223 { "sun68k", 0, 0, 8, 2, 0, 64, BIG_ENDIAN }, /* m68010 */ 224 { "x68k", 0, 0, 8, 2, 0, 64, BIG_ENDIAN }, /* m68010 */ 225 226 { "vax", 0, 0, 12, 2, 8, 64, LITTLE_ENDIAN }, /* vax */ 227 228 { "amiga", 0, 0, 16, 2, 0, 64, BIG_ENDIAN }, /* m68k */ 229 { "amigappc", 0, 0, 16, 2, 0, 64, BIG_ENDIAN }, /* powerpc */ 230 { "evbmips", 0, 0, 16, 2, 0, 64, 0 }, /* mips */ 231 { "evbppc", 0, 0, 16, 2, 0, 64, BIG_ENDIAN }, /* powerpc */ 232 233 { "sparc", 0, 0, 8, 2, 0, 128, BIG_ENDIAN }, /* sun */ 234 { "sparc64", 0, 0, 8, 2, 0, 128, BIG_ENDIAN }, /* sun */ 235 { "sun3", 0, 0, 8, 2, 0, 128, BIG_ENDIAN }, /* sun */ 236 237 { "atari", 0, 0, 16, 2, 0, 516, BIG_ENDIAN }, /* m68k */ 238 239 { "mipsco", 0, 1, 8, 2, 0, 0, BIG_ENDIAN }, /* mips */ 240 { "mvmeppc", 0, 1, 8, 3, 0, 0, BIG_ENDIAN }, /* powerpc */ 241 242 { "bebox", 0, 1, 8, 3, 0, 0, BIG_ENDIAN }, /* powerpc */ 243 244 { "emips", 0, 1, 16, 2, 0, 0, BIG_ENDIAN }, /* mips */ 245 { "hppa", 0, 1, 16, 2, 0, 0, BIG_ENDIAN }, /* hppa */ 246 { "ibmnws", 0, 1, 16, 2, 0, 0, BIG_ENDIAN }, /* powerpc */ 247 { "ofppc", 0, 1, 16, 2, 0, 0, BIG_ENDIAN }, /* powerpc */ 248 { "rs6000", 0, 1, 16, 2, 0, 0, BIG_ENDIAN }, /* powerpc */ 249 { "sandpoint", 0, 1, 16, 2, 0, 0, BIG_ENDIAN }, /* powerpc */ 250 { "sgimips", 0, 1, 16, 2, 0, 0, BIG_ENDIAN }, /* mips */ 251 252 { "sbmips", 0, 1, 16, 3, 0, 0, 0 }, /* mips */ 253 254 { "cesfic", 0, 2, 8, 2, 0, 0, BIG_ENDIAN }, /* m68k */ 255 { "hp300", 0, 2, 8, 2, 0, 0, BIG_ENDIAN }, /* m68k */ 256 257 { "ews4800mips",0, 9, 16, 15, 0, 0, BIG_ENDIAN }, /* mips */ 258 259 { "macppc", 1, 0, 16, 2, 0, 64, BIG_ENDIAN }, /* powerpc */ 260 { "pmon", 1, 0, 16, 2, 0, 64, 0 }, /* evbmips */ 261 262 { "prep", 1, 1, 8, 2, 0, 0, BIG_ENDIAN }, /* powerpc */ 263 264 { "dreamcast", 1, 1, 16, 2, 0, 0, LITTLE_ENDIAN }, /* sh3 */ 265 { "evbcf", 1, 1, 16, 2, 0, 0, BIG_ENDIAN }, /* coldfire */ 266 { "evbppc-mbr", 1, 1, 16, 2, 0, 0, BIG_ENDIAN }, /* powerpc */ 267 { "evbsh3", 1, 1, 16, 2, 0, 0, 0 }, /* sh3 */ 268 { "hpcsh", 1, 1, 16, 2, 0, 0, LITTLE_ENDIAN }, /* sh3 */ 269 { "mmeye", 1, 1, 16, 2, 0, 0, 0 }, /* sh3 */ 270 { "or1k", 1, 1, 16, 2, 0, 0, BIG_ENDIAN }, /* or1k */ 271 { "riscv", 1, 1, 16, 2, 0, 0, LITTLE_ENDIAN }, /* riscv */ 272 273 { "acorn32", 1, 1, 16, 2, 8, 0, LITTLE_ENDIAN }, /* arm */ 274 { "cats", 1, 1, 16, 2, 8, 0, LITTLE_ENDIAN }, /* arm */ 275 { "evbarm", 1, 1, 16, 2, 8, 0, 0 }, /* arm */ 276 { "iyonix", 1, 1, 16, 2, 8, 0, LITTLE_ENDIAN }, /* arm */ 277 { "netwinder", 1, 1, 16, 2, 8, 0, LITTLE_ENDIAN }, /* arm */ 278 { "shark", 1, 1, 16, 2, 8, 0, LITTLE_ENDIAN }, /* arm */ 279 280 { "amd64", 1, 1, 16, 3, 0, 0, LITTLE_ENDIAN }, /* x86 */ 281 { "arc", 1, 1, 16, 3, 0, 0, LITTLE_ENDIAN }, /* mips */ 282 { "cobalt", 1, 1, 16, 3, 0, 0, LITTLE_ENDIAN }, /* mips */ 283 { "landisk", 1, 1, 16, 3, 0, 0, LITTLE_ENDIAN }, /* sh3 */ 284 285 { "epoc32", 1, 1, 16, 3, 8, 0, LITTLE_ENDIAN }, /* arm */ 286 { "hpcarm", 1, 1, 16, 3, 8, 0, LITTLE_ENDIAN }, /* arm */ 287 { "hpcmips", 1, 1, 16, 3, 8, 0, LITTLE_ENDIAN }, /* mips */ 288 { "i386", 1, 1, 16, 3, 8, 0, LITTLE_ENDIAN }, /* x86 */ 289 { "ia64", 1, 1, 16, 3, 8, 0, LITTLE_ENDIAN }, /* x86 */ 290 { "zaurus", 1, 1, 16, 3, 8, 0, LITTLE_ENDIAN }, /* arm */ 291 292 { NULL, 0, 0, 0, 0, 0, 0, 0 }, /* must be last */ 293 }; 294 295 #ifndef HAVE_NBTOOL_CONFIG_H 296 static struct disklabel_params native_params; 297 #endif 298 299 static const struct arch_endian { 300 int byteorder; 301 const char *arch; 302 } arch_endians[] = { 303 { LITTLE_ENDIAN, "aarch64" }, 304 { LITTLE_ENDIAN, "alpha" }, 305 { LITTLE_ENDIAN, "arm" }, 306 { LITTLE_ENDIAN, "earm" }, 307 { LITTLE_ENDIAN, "earmhf" }, 308 { LITTLE_ENDIAN, "earmv4" }, 309 { LITTLE_ENDIAN, "earmv5" }, 310 { LITTLE_ENDIAN, "earmv6" }, 311 { LITTLE_ENDIAN, "earmv6hf" }, 312 { LITTLE_ENDIAN, "earmv7" }, 313 { LITTLE_ENDIAN, "earmv7hf" }, 314 { LITTLE_ENDIAN, "i386" }, 315 { LITTLE_ENDIAN, "ia64" }, 316 { LITTLE_ENDIAN, "mipsel" }, 317 { LITTLE_ENDIAN, "mips64el" }, 318 { LITTLE_ENDIAN, "riscv32" }, 319 { LITTLE_ENDIAN, "riscv64" }, 320 { LITTLE_ENDIAN, "sh3el" }, 321 { LITTLE_ENDIAN, "vax" }, 322 { LITTLE_ENDIAN, "x86_64" }, 323 324 { BIG_ENDIAN, "aarch64eb" }, 325 { BIG_ENDIAN, "armeb" }, 326 { BIG_ENDIAN, "coldfire" }, 327 { BIG_ENDIAN, "earmeb" }, 328 { BIG_ENDIAN, "earmhfeb" }, 329 { BIG_ENDIAN, "earmv4eb" }, 330 { BIG_ENDIAN, "earmv5eb" }, 331 { BIG_ENDIAN, "earmv6eb" }, 332 { BIG_ENDIAN, "earmv6hfeb" }, 333 { BIG_ENDIAN, "earmv7eb" }, 334 { BIG_ENDIAN, "earmv7hfeb" }, 335 { BIG_ENDIAN, "hppa" }, 336 { BIG_ENDIAN, "m68000" }, 337 { BIG_ENDIAN, "m68k" }, 338 { BIG_ENDIAN, "mipseb" }, 339 { BIG_ENDIAN, "mips64eb" }, 340 { BIG_ENDIAN, "or1k" }, 341 { BIG_ENDIAN, "powerpc" }, 342 { BIG_ENDIAN, "sh3eb" }, 343 { BIG_ENDIAN, "sparc" }, 344 { BIG_ENDIAN, "sparc64" }, 345 346 { 0, NULL }, 347 }; 348 349 /* Default location for label - only used if we don't find one to update */ 350 #define LABEL_OFFSET (dklabel_getlabelsector() * DEV_BSIZE + dklabel_getlabeloffset()) 351 #else 352 #define labeloffset LABELOFFSET 353 #define labelsector LABELSECTOR 354 #define labelusesmbr LABELUSESMBR 355 #define maxpartitions MAXPARTITIONS 356 #define LABEL_OFFSET LABELOFFSET 357 #endif /* !NATIVELABEL_ONLY */ 358 359 /* 360 * For portability it doesn't make sense to use any other value.... 361 * Except, maybe, the size of a physical sector. 362 * This value is used if we have to write a label to the start of an mbr ptn. 363 */ 364 #ifndef LABELOFFSET_MBR 365 #define LABELOFFSET_MBR 512 366 #endif 367 368 #if HAVE_NBTOOL_CONFIG_H 369 static int 370 opendisk(const char *path, int flags, char *buf, int buflen, int cooked) 371 { 372 int f; 373 f = open(path, flags, 0); 374 strlcpy(buf, path, buflen); 375 return f; 376 } 377 #endif /* HAVE_NBTOOL_CONFIG_H */ 378 379 #if !defined(NATIVELABEL_ONLY) 380 static void 381 setbyteorder(int new_byteorder) 382 { 383 static int set_p; 384 385 if ((!biendian_p || set_p) 386 && byteorder != 0 387 && byteorder != new_byteorder) { 388 warnx("changing %s byteorder to %s", 389 byteorder == LITTLE_ENDIAN ? "le" : "be", 390 new_byteorder == LITTLE_ENDIAN ? "le" : "be"); 391 } 392 byteorder = new_byteorder; 393 biendian_p = 0; 394 set_p = 1; 395 } 396 397 static void 398 getmachineparams(const char *mach) 399 { 400 const struct disklabel_params *dp = disklabel_params; 401 for (; dp->machine != NULL; dp++) { 402 if (!strcmp(mach, dp->machine)) { 403 labelusesmbr = dp->labelusesmbr; 404 labelsector = dp->labelsector; 405 labeloffset = dp->labeloffset; 406 maxpartitions = dp->maxpartitions; 407 biendian_p = (dp->byteorder == 0); 408 if (!biendian_p) 409 setbyteorder(dp->byteorder); 410 return; 411 } 412 } 413 errx(1, "%s: unknown machine type", mach); 414 } 415 416 static void 417 getarchbyteorder(const char *arch) 418 { 419 const struct arch_endian *p = arch_endians; 420 for (; p->arch != NULL; p++) { 421 if (!strcmp(arch, p->arch)) { 422 setbyteorder(p->byteorder); 423 return; 424 } 425 } 426 errx(1, "%s: unknown arch", arch); 427 } 428 429 static daddr_t 430 dklabel_getlabelsector(void) 431 { 432 unsigned long int nval; 433 char *end; 434 const char *val; 435 436 if ((val = getenv("DISKLABELSECTOR")) == NULL) 437 return labelsector; 438 if ((nval = strtoul(val, &end, 10)) == ULONG_MAX && errno == ERANGE) 439 err(EXIT_FAILURE, "DISKLABELSECTOR in environment"); 440 return nval; 441 } 442 443 static off_t 444 dklabel_getlabeloffset(void) 445 { 446 unsigned long int nval; 447 char *end; 448 const char *val; 449 450 if ((val = getenv("DISKLABELOFFSET")) == NULL) 451 return labeloffset; 452 if ((nval = strtoul(val, &end, 10)) == ULONG_MAX && errno == ERANGE) 453 err(EXIT_FAILURE, "DISKLABELOFFSET in environment"); 454 return nval; 455 } 456 #endif /* !NATIVELABEL_ONLY */ 457 458 static void 459 clear_writable(void) 460 { 461 static int zero = 0; 462 dk_ioctl(set_writable_fd, DIOCWLABEL, &zero); 463 } 464 465 int 466 main(int argc, char *argv[]) 467 { 468 FILE *t; 469 int ch, f, error; 470 char *dkname; 471 #if !defined(NATIVELABEL_ONLY) 472 char *cp; 473 #endif 474 struct stat sb; 475 int writable; 476 enum { 477 UNSPEC, EDIT, READ, RESTORE, SETWRITABLE, SETREADONLY, 478 WRITE, 479 #if !defined(NO_INTERACT) 480 INTERACT, 481 #endif 482 DELETE 483 } op = UNSPEC, old_op; 484 485 #ifndef HAVE_NBTOOL_CONFIG_H 486 #if !defined(NATIVELABEL_ONLY) 487 labeloffset = native_params.labeloffset = getlabeloffset(); 488 labelsector = native_params.labelsector = getlabelsector(); 489 labelusesmbr = native_params.labelusesmbr = getlabelusesmbr(); 490 maxpartitions = native_params.maxpartitions = getmaxpartitions(); 491 byteorder = native_params.byteorder = BYTE_ORDER; 492 #endif 493 #endif 494 495 #if !defined(NATIVELABEL_ONLY) 496 if ((cp = getenv("MACHINE")) != NULL) { 497 getmachineparams(cp); 498 } 499 500 if ((cp = getenv("MACHINE_ARCH")) != NULL) { 501 getarchbyteorder(cp); 502 } 503 #endif 504 505 mflag = labelusesmbr; 506 if (mflag < 0) { 507 #if HAVE_NBTOOL_CONFIG_H 508 warn("getlabelusesmbr() failed"); 509 #else 510 warn("getlabelusesmbr() failed"); 511 mflag = LABELUSESMBR; 512 #endif 513 } 514 #if HAVE_NBTOOL_CONFIG_H 515 /* We must avoid doing any ioctl requests */ 516 Fflag = rflag = 1; 517 #endif 518 519 error = 0; 520 while ((ch = getopt(argc, argv, "AB:CDFIM:NRWef:ilmrtvw")) != -1) { 521 old_op = op; 522 switch (ch) { 523 case 'A': /* Action all labels */ 524 Aflag = 1; 525 rflag = 1; 526 break; 527 case 'C': /* Display in CHS format */ 528 Cflag = 1; 529 break; 530 case 'D': /* Delete all existing labels */ 531 Dflag = 1; 532 rflag = 1; 533 break; 534 case 'F': /* Treat 'disk' as a regular file */ 535 Fflag = 1; 536 rflag = 1; /* Force direct access */ 537 break; 538 case 'I': /* Use default label if none found */ 539 Iflag = 1; 540 rflag = 1; /* Implies direct access */ 541 break; 542 case 'R': /* Restore label from text file */ 543 op = RESTORE; 544 break; 545 #if !defined(NATIVELABEL_ONLY) 546 case 'B': /* byteorder */ 547 if (!strcmp(optarg, "be")) { 548 setbyteorder(BIG_ENDIAN); 549 } else if (!strcmp(optarg, "le")) { 550 setbyteorder(LITTLE_ENDIAN); 551 } else { 552 errx(1, "%s: not be or le", optarg); 553 } 554 break; 555 case 'M': /* machine type */ 556 getmachineparams(optarg); 557 break; 558 #endif 559 case 'N': /* Disallow writes to label sector */ 560 op = SETREADONLY; 561 break; 562 case 'W': /* Allow writes to label sector */ 563 op = SETWRITABLE; 564 break; 565 case 'e': /* Edit label with $EDITOR */ 566 op = EDIT; 567 break; 568 case 'f': /* Name of disktab file */ 569 if (setdisktab(optarg) == -1) 570 usage(); 571 break; 572 #if !defined(NO_INTERACT) 573 case 'i': /* Edit using built-in editor */ 574 op = INTERACT; 575 break; 576 #endif /* !NO_INTERACT */ 577 case 'l': /* List all known file system types and exit */ 578 lflag = 1; 579 break; 580 case 'm': /* Expect disk to have an MBR */ 581 mflag ^= 1; 582 break; 583 case 'r': /* Read/write label directly from disk */ 584 rflag = 1; 585 break; 586 case 't': /* Format output as a disktab entry */ 587 tflag = 1; 588 break; 589 case 'v': /* verbose/diag output */ 590 verbose++; 591 break; 592 case 'w': /* Write label based on disktab entry */ 593 op = WRITE; 594 break; 595 case '?': 596 default: 597 usage(); 598 } 599 if (old_op != UNSPEC && old_op != op) 600 usage(); 601 } 602 603 #if !defined(NATIVELABEL_ONLY) 604 if (maxpartitions == 0) { 605 errx(1, "unknown label: use -M/-B and $MACHINE/$MACHINE_ARCH"); 606 } 607 if (byteorder != BIG_ENDIAN && byteorder != LITTLE_ENDIAN) { 608 errx(1, "unknown byteorder"); 609 } 610 bswap_p = (byteorder != BYTE_ORDER); 611 #ifdef DEBUG 612 printf("labelusesmbr=%d labelsector=%u labeloffset=%u maxpartitions=%u\n", 613 labelusesmbr, labelsector, labeloffset, maxpartitions); 614 printf("byteorder=%d bswap_p=%d\n", byteorder, bswap_p); 615 #endif 616 #ifndef HAVE_NBTOOL_CONFIG_H 617 /* 618 * If the disklabel has the same location as the native disklabel and 619 * fewer or equal paritions, we can use the native ioctls. Otherwise 620 * force file/raw access. 621 */ 622 native_p = native_params.labelusesmbr == labelusesmbr 623 && native_params.labelsector == labelsector 624 && native_params.labeloffset == labeloffset 625 && maxpartitions <= native_params.maxpartitions 626 && !bswap_p; 627 if (!native_p) 628 Fflag = rflag = 1; 629 #endif 630 #endif /* !NATIVELABEL_ONLY */ 631 632 argc -= optind; 633 argv += optind; 634 635 if (lflag) 636 exit(list_fs_types() ? EXIT_SUCCESS : EXIT_FAILURE); 637 638 if (op == UNSPEC) 639 op = Dflag ? DELETE : READ; 640 641 if (argc < 1) 642 usage(); 643 644 if (Iflag && op != EDIT 645 #if !defined(NO_INTERACT) 646 && op != INTERACT 647 #endif 648 ) 649 usage(); 650 651 dkname = argv[0]; 652 f = opendisk(dkname, op == READ ? O_RDONLY : O_RDWR, 653 specname, sizeof specname, 0); 654 if (f < 0) 655 err(4, "%s", specname); 656 657 if (!Fflag && fstat(f, &sb) == 0 && S_ISREG(sb.st_mode)) 658 Fflag = rflag = 1; 659 660 switch (op) { 661 662 case DELETE: /* Remove all existing labels */ 663 if (argc != 1) 664 usage(); 665 Dflag = 2; 666 writelabel_direct(f); 667 break; 668 669 case EDIT: 670 if (argc != 1) 671 usage(); 672 readlabel(f); 673 error = edit(f); 674 break; 675 676 #if !defined(NO_INTERACT) 677 case INTERACT: 678 if (argc != 1) 679 usage(); 680 readlabel(f); 681 /* 682 * XXX: Fill some default values so checklabel does not fail 683 */ 684 if (lab.d_bbsize == 0) 685 lab.d_bbsize = BBSIZE; 686 if (lab.d_sbsize == 0) 687 lab.d_sbsize = SBLOCKSIZE; 688 interact(&lab, f); 689 break; 690 #endif /* !NO_INTERACT */ 691 692 case READ: 693 if (argc != 1) 694 usage(); 695 read_all = Aflag; 696 readlabel(f); 697 if (read_all) 698 /* Label got printed in the bowels of readlabel */ 699 break; 700 if (tflag) 701 makedisktab(stdout, &lab); 702 else { 703 showinfo(stdout, &lab, specname); 704 showpartitions(stdout, &lab, Cflag); 705 } 706 error = checklabel(&lab); 707 if (error) 708 error += 100; 709 break; 710 711 case RESTORE: 712 if (argc != 2) 713 usage(); 714 if (!(t = fopen(argv[1], "r"))) 715 err(4, "%s", argv[1]); 716 if (getasciilabel(t, &lab)) 717 error = write_label(f); 718 else 719 error = 1; 720 break; 721 722 case SETREADONLY: 723 writable = 0; 724 goto do_diocwlabel; 725 case SETWRITABLE: 726 writable = 1; 727 do_diocwlabel: 728 if (argc != 1) 729 usage(); 730 if (dk_ioctl(f, DIOCWLABEL, &writable) < 0) 731 err(4, "ioctl DIOCWLABEL"); 732 break; 733 734 case WRITE: /* Create label from /etc/disktab entry & write */ 735 if (argc < 2 || argc > 3) 736 usage(); 737 makelabel(argv[1], argv[2]); 738 if (checklabel(&lab) == 0) 739 error = write_label(f); 740 else 741 error = 1; 742 break; 743 744 case UNSPEC: 745 usage(); 746 747 } 748 exit(error); 749 } 750 751 /* 752 * Construct a prototype disklabel from /etc/disktab. 753 */ 754 static void 755 makelabel(const char *type, const char *name) 756 { 757 struct disklabel *dp; 758 759 dp = getdiskbyname(type); 760 if (dp == NULL) 761 errx(1, "unknown disk type: %s", type); 762 lab = *dp; 763 764 /* d_packname is union d_boot[01], so zero */ 765 (void)memset(lab.d_packname, 0, sizeof(lab.d_packname)); 766 if (name) 767 (void)strncpy(lab.d_packname, name, sizeof(lab.d_packname)); 768 } 769 770 static int 771 write_label(int f) 772 { 773 int writable; 774 775 lab.d_magic = DISKMAGIC; 776 lab.d_magic2 = DISKMAGIC; 777 lab.d_checksum = 0; 778 lab.d_checksum = dkcksum(&lab); 779 780 if (rflag) { 781 /* Write the label directly to the disk */ 782 783 /* 784 * First set the kernel disk label, 785 * then write a label to the raw disk. 786 * If the SDINFO ioctl fails because it is unimplemented, 787 * keep going; otherwise, the kernel consistency checks 788 * may prevent us from changing the current (in-core) 789 * label. 790 */ 791 if (!Fflag && dk_ioctl(f, DIOCSDINFO, &lab) < 0 && 792 errno != ENODEV && errno != ENOTTY) { 793 l_perror("ioctl DIOCSDINFO"); 794 return (1); 795 } 796 /* 797 * write enable label sector before write (if necessary), 798 * disable after writing. 799 */ 800 writable = 1; 801 if (!Fflag) { 802 if (dk_ioctl(f, DIOCWLABEL, &writable) < 0) 803 perror("ioctl DIOCWLABEL"); 804 set_writable_fd = f; 805 atexit(clear_writable); 806 } 807 808 writelabel_direct(f); 809 810 /* 811 * Now issue a DIOCWDINFO. This will let the kernel convert the 812 * disklabel to some machdep format if needed. 813 */ 814 /* XXX: This is stupid! */ 815 if (!Fflag && dk_ioctl(f, DIOCWDINFO, &lab) < 0) { 816 l_perror("ioctl DIOCWDINFO"); 817 return (1); 818 } 819 } else { 820 /* Get the kernel to write the label */ 821 if (dk_ioctl(f, DIOCWDINFO, &lab) < 0) { 822 l_perror("ioctl DIOCWDINFO"); 823 return (1); 824 } 825 } 826 827 #ifdef VAX_ALTLABELS 828 if (lab.d_type == DKTYPE_SMD && lab.d_flags & D_BADSECT && 829 lab.d_secsize == 512) { 830 /* Write the label to the odd sectors of the last track! */ 831 daddr_t alt; 832 int i; 833 uint8_t sec0[512]; 834 835 if (pread(f, sec0, 512, 0) < 512) { 836 warn("read master label to write alternates"); 837 return 0; 838 } 839 840 alt = lab.d_ncylinders * lab.d_secpercyl - lab.d_nsectors; 841 for (i = 1; i < 11 && (uint32_t)i < lab.d_nsectors; i += 2) { 842 if (pwrite(f, sec0, 512, (off_t)(alt + i) * 512) < 512) 843 warn("alternate label %d write", i/2); 844 } 845 } 846 #endif /* VAX_ALTLABELS */ 847 848 return 0; 849 } 850 851 int 852 writelabel(int f, struct disklabel *lp) 853 { 854 if (lp != &lab) 855 lab = *lp; 856 return write_label(f); 857 } 858 859 static void 860 l_perror(const char *s) 861 { 862 863 switch (errno) { 864 865 case ESRCH: 866 warnx("%s: No disk label on disk;\n" 867 "use \"disklabel -I\" to install initial label", s); 868 break; 869 870 case EINVAL: 871 warnx("%s: Label magic number or checksum is wrong!\n" 872 "(disklabel or kernel is out of date?)", s); 873 break; 874 875 case EBUSY: 876 warnx("%s: Open partition would move or shrink", s); 877 break; 878 879 case EXDEV: 880 warnx("%s: Labeled partition or 'a' partition must start" 881 " at beginning of disk", s); 882 break; 883 884 default: 885 warn("%s", s); 886 break; 887 } 888 } 889 890 #ifdef NO_MBR_SUPPORT 891 #define process_mbr(f, action) 1 892 #else 893 /* 894 * Scan DOS/MBR partition table and extended partition list for NetBSD ptns. 895 */ 896 static int 897 process_mbr(int f, int (*action)(int, u_int)) 898 { 899 struct mbr_partition *dp; 900 struct mbr_sector mbr; 901 int rval = 1, res; 902 int part; 903 u_int ext_base, next_ext, this_ext, start; 904 905 ext_base = 0; 906 next_ext = 0; 907 for (;;) { 908 this_ext = next_ext; 909 next_ext = 0; 910 if (verbose > 1) 911 warnx("reading mbr sector %u", this_ext); 912 if (pread(f, &mbr, sizeof mbr, this_ext * (off_t)DEV_BSIZE) 913 != sizeof(mbr)) { 914 if (verbose) 915 warn("Can't read master boot record %u", 916 this_ext); 917 break; 918 } 919 920 /* Check if table is valid. */ 921 if (mbr.mbr_magic != htole16(MBR_MAGIC)) { 922 if (verbose) 923 warnx("Invalid signature in mbr record %u", 924 this_ext); 925 break; 926 } 927 928 dp = &mbr.mbr_parts[0]; 929 930 /* Find NetBSD partition(s). */ 931 for (part = 0; part < MBR_PART_COUNT; dp++, part++) { 932 start = le32toh(dp->mbrp_start); 933 switch (dp->mbrp_type) { 934 #ifdef COMPAT_386BSD_MBRPART 935 case MBR_PTYPE_386BSD: 936 if (ext_base != 0) 937 break; 938 /* FALLTHROUGH */ 939 #endif 940 case MBR_PTYPE_NETBSD: 941 res = action(f, this_ext + start); 942 if (res <= 0) 943 /* Found or failure */ 944 return res; 945 if (res > rval) 946 /* Keep largest value */ 947 rval = res; 948 break; 949 case MBR_PTYPE_EXT: 950 case MBR_PTYPE_EXT_LBA: 951 case MBR_PTYPE_EXT_LNX: 952 next_ext = start; 953 break; 954 default: 955 break; 956 } 957 } 958 if (next_ext == 0) 959 /* No more extended partitions */ 960 break; 961 next_ext += ext_base; 962 if (ext_base == 0) 963 ext_base = next_ext; 964 965 if (next_ext <= this_ext) { 966 if (verbose) 967 warnx("Invalid extended chain %x <= %x", 968 next_ext, this_ext); 969 break; 970 } 971 /* Maybe we should check against the disk size... */ 972 } 973 974 return rval; 975 } 976 977 static int 978 readlabel_mbr(int f, u_int sector) 979 { 980 struct disklabel *disk_lp; 981 982 disk_lp = find_label(f, sector); 983 if (disk_lp == NULL) 984 return 1; 985 targettohlabel(&lab, disk_lp); 986 return 0; 987 } 988 989 static int 990 writelabel_mbr(int f, u_int sector) 991 { 992 return update_label(f, sector, mflag ? LABELOFFSET_MBR : ~0U) ? 2 : 0; 993 } 994 995 #endif /* !NO_MBR_SUPPORT */ 996 997 #ifndef USE_ACORN 998 #define get_filecore_partition(f) 0 999 #else 1000 /* 1001 * static int filecore_checksum(u_char *bootblock) 1002 * 1003 * Calculates the filecore boot block checksum. This is used to validate 1004 * a filecore boot block on the disk. If a boot block is validated then 1005 * it is used to locate the partition table. If the boot block is not 1006 * validated, it is assumed that the whole disk is NetBSD. 1007 * 1008 * The basic algorithm is: 1009 * 1010 * for (each byte in block, excluding checksum) { 1011 * sum += byte; 1012 * if (sum > 255) 1013 * sum -= 255; 1014 * } 1015 * 1016 * That's equivalent to summing all of the bytes in the block 1017 * (excluding the checksum byte, of course), then calculating the 1018 * checksum as "cksum = sum - ((sum - 1) / 255) * 255)". That 1019 * expression may or may not yield a faster checksum function, 1020 * but it's easier to reason about. 1021 * 1022 * Note that if you have a block filled with bytes of a single 1023 * value "X" (regardless of that value!) and calculate the cksum 1024 * of the block (excluding the checksum byte), you will _always_ 1025 * end up with a checksum of X. (Do the math; that can be derived 1026 * from the checksum calculation function!) That means that 1027 * blocks which contain bytes which all have the same value will 1028 * always checksum properly. That's a _very_ unlikely occurence 1029 * (probably impossible, actually) for a valid filecore boot block, 1030 * so we treat such blocks as invalid. 1031 */ 1032 static int 1033 filecore_checksum(u_char *bootblock) 1034 { 1035 u_char byte0, accum_diff; 1036 u_int sum; 1037 int i; 1038 1039 sum = 0; 1040 accum_diff = 0; 1041 byte0 = bootblock[0]; 1042 1043 /* 1044 * Sum the contents of the block, keeping track of whether 1045 * or not all bytes are the same. If 'accum_diff' ends up 1046 * being zero, all of the bytes are, in fact, the same. 1047 */ 1048 for (i = 0; i < 511; ++i) { 1049 sum += bootblock[i]; 1050 accum_diff |= bootblock[i] ^ byte0; 1051 } 1052 1053 /* 1054 * Check to see if the checksum byte is the same as the 1055 * rest of the bytes, too. (Note that if all of the bytes 1056 * are the same except the checksum, a checksum compare 1057 * won't succeed, but that's not our problem.) 1058 */ 1059 accum_diff |= bootblock[i] ^ byte0; 1060 1061 /* All bytes in block are the same; call it invalid. */ 1062 if (accum_diff == 0) 1063 return (-1); 1064 1065 return (sum - ((sum - 1) / 255) * 255); 1066 } 1067 1068 /* 1069 * Check for the presence of a RiscOS filecore boot block 1070 * indicating an ADFS file system on the disc. 1071 * Return the offset to the NetBSD part of the disc if 1072 * this can be determined. 1073 * This routine will terminate disklabel if the disc 1074 * is found to be ADFS only. 1075 */ 1076 static u_int 1077 get_filecore_partition(int f) 1078 { 1079 struct filecore_bootblock *fcbb; 1080 static u_char bb[DEV_BSIZE]; 1081 u_int offset; 1082 struct riscix_partition_table *riscix_part; 1083 int loop; 1084 1085 if (pread(f, bb, sizeof(bb), (off_t)FILECORE_BOOT_SECTOR * DEV_BSIZE) != sizeof(bb)) 1086 err(4, "can't read filecore boot block"); 1087 fcbb = (struct filecore_bootblock *)bb; 1088 1089 /* Check if table is valid. */ 1090 if (filecore_checksum(bb) != fcbb->checksum) 1091 return (0); 1092 1093 /* 1094 * Check for NetBSD/arm32 (RiscBSD) partition marker. 1095 * If found the NetBSD disklabel location is easy. 1096 */ 1097 offset = (fcbb->partition_cyl_low + (fcbb->partition_cyl_high << 8)) 1098 * fcbb->heads * fcbb->secspertrack; 1099 1100 switch (fcbb->partition_type) { 1101 1102 case PARTITION_FORMAT_RISCBSD: 1103 return (offset); 1104 1105 case PARTITION_FORMAT_RISCIX: 1106 /* 1107 * Read the RISCiX partition table and search for the 1108 * first partition named "RiscBSD", "NetBSD", or "Empty:" 1109 * 1110 * XXX is use of 'Empty:' really desirable?! -- cgd 1111 */ 1112 1113 if (pread(f, bb, sizeof(bb), (off_t)offset * DEV_BSIZE) != sizeof(bb)) 1114 err(4, "can't read riscix partition table"); 1115 riscix_part = (struct riscix_partition_table *)bb; 1116 1117 for (loop = 0; loop < NRISCIX_PARTITIONS; ++loop) { 1118 if (strcmp((char *)riscix_part->partitions[loop].rp_name, 1119 "RiscBSD") == 0 || 1120 strcmp((char *)riscix_part->partitions[loop].rp_name, 1121 "NetBSD") == 0 || 1122 strcmp((char *)riscix_part->partitions[loop].rp_name, 1123 "Empty:") == 0) { 1124 return riscix_part->partitions[loop].rp_start; 1125 break; 1126 } 1127 } 1128 /* 1129 * Valid filecore boot block, RISCiX partition table 1130 * but no NetBSD partition. We should leave this 1131 * disc alone. 1132 */ 1133 errx(4, "cannot label: no NetBSD partition found" 1134 " in RISCiX partition table"); 1135 1136 default: 1137 /* 1138 * Valid filecore boot block and no non-ADFS partition. 1139 * This means that the whole disc is allocated for ADFS 1140 * so do not trash ! If the user really wants to put a 1141 * NetBSD disklabel on the disc then they should remove 1142 * the filecore boot block first with dd. 1143 */ 1144 errx(4, "cannot label: filecore-only disk" 1145 " (no non-ADFS partition)"); 1146 } 1147 return (0); 1148 } 1149 #endif /* USE_ACORN */ 1150 1151 /* 1152 * Fetch disklabel for disk to 'lab'. 1153 * Use ioctl to get label unless -r flag is given. 1154 */ 1155 static void 1156 readlabel(int f) 1157 { 1158 if (rflag) { 1159 /* Get label directly from disk */ 1160 if (readlabel_direct(f) == 0) 1161 return; 1162 /* 1163 * There was no label on the disk. Get the fictious one 1164 * as a basis for initialisation. 1165 */ 1166 if (!Fflag && Iflag && (dk_ioctl(f, DIOCGDINFO, &lab) == 0 || 1167 dk_ioctl(f, DIOCGDEFLABEL, &lab) == 0)) 1168 return; 1169 } else { 1170 /* Get label from kernel. */ 1171 if (dk_ioctl(f, DIOCGDINFO, &lab) < 0) 1172 err(4, "ioctl DIOCGDINFO"); 1173 return; 1174 } 1175 1176 if (read_all == 2) 1177 /* We actually found one, and printed it... */ 1178 exit(0); 1179 errx(1, "could not read existing label"); 1180 } 1181 1182 /* 1183 * Reading the label from the disk is largely a case of 'hunt the label'. 1184 * and since different architectures default to different places there 1185 * could even be more than one label that contradict each other! 1186 * For now we look in the expected place, then search through likely 1187 * other locations. 1188 */ 1189 static struct disklabel * 1190 find_label(int f, u_int sector) 1191 { 1192 struct disklabel *disk_lp, hlp, tlp; 1193 int i; 1194 off_t offset; 1195 const char *is_deleted; 1196 1197 bootarea_len = pread(f, bootarea, sizeof bootarea, 1198 sector * (off_t)DEV_BSIZE); 1199 if (bootarea_len <= 0) { 1200 if (verbose) 1201 warn("failed to read bootarea from sector %u", sector); 1202 return NULL; 1203 } 1204 1205 if (verbose > 2) 1206 warnx("read sector %u len %d looking for label", 1207 sector, bootarea_len); 1208 1209 /* Check expected offset first */ 1210 for (offset = LABEL_OFFSET, i = -4;; offset = i += 4) { 1211 is_deleted = ""; 1212 if (i == LABEL_OFFSET) 1213 continue; 1214 disk_lp = (void *)(bootarea + offset); 1215 memcpy(&tlp, disk_lp, sizeof(tlp)); 1216 if ((char *)(disk_lp + 1) > bootarea + bootarea_len) 1217 break; 1218 if (tlp.d_magic2 != tlp.d_magic) 1219 continue; 1220 if (read_all && (tlp.d_magic == DISKMAGIC_DELETED || 1221 tlp.d_magic == DISKMAGIC_DELETED_REV)) { 1222 tlp.d_magic ^= ~0u; 1223 tlp.d_magic2 ^= ~0u; 1224 is_deleted = "deleted "; 1225 } 1226 if (target32toh(tlp.d_magic) != DISKMAGIC) { 1227 /* XXX: Do something about byte-swapped labels ? */ 1228 if (target32toh(tlp.d_magic) == DISKMAGIC_REV && 1229 target32toh(tlp.d_magic2) == DISKMAGIC_REV) 1230 warnx("ignoring %sbyteswapped label" 1231 " at offset %jd from sector %u", 1232 is_deleted, (intmax_t)offset, sector); 1233 continue; 1234 } 1235 if (target16toh(tlp.d_npartitions) > maxpartitions || 1236 dkcksum_target(&tlp) != 0) { 1237 if (verbose > 0) 1238 warnx("corrupt label found at offset %jd in " 1239 "sector %u", (intmax_t)offset, sector); 1240 continue; 1241 } 1242 if (verbose > 1) 1243 warnx("%slabel found at offset %jd from sector %u", 1244 is_deleted, (intmax_t)offset, sector); 1245 if (!read_all) 1246 return disk_lp; 1247 1248 /* To print all the labels we have to do it here */ 1249 /* XXX: maybe we should compare them? */ 1250 targettohlabel(&hlp, &tlp); 1251 printf("# %ssector %u offset %jd bytes\n", 1252 is_deleted, sector, (intmax_t)offset); 1253 if (tflag) 1254 makedisktab(stdout, &hlp); 1255 else { 1256 showinfo(stdout, &hlp, specname); 1257 showpartitions(stdout, &hlp, Cflag); 1258 } 1259 checklabel(&hlp); 1260 htotargetlabel(&tlp, &hlp); 1261 memcpy(disk_lp, &tlp, sizeof(tlp)); 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