1 /* 2 * Mach Operating System 3 * Copyright (c) 1992 Carnegie Mellon University 4 * All Rights Reserved. 5 * 6 * Permission to use, copy, modify and distribute this software and its 7 * documentation is hereby granted, provided that both the copyright 8 * notice and this permission notice appear in all copies of the 9 * software, derivative works or modified versions, and any portions 10 * thereof, and that both notices appear in supporting documentation. 11 * 12 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 13 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR 14 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 15 * 16 * Carnegie Mellon requests users of this software to return to 17 * 18 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 19 * School of Computer Science 20 * Carnegie Mellon University 21 * Pittsburgh PA 15213-3890 22 * 23 * any improvements or extensions that they make and grant Carnegie Mellon 24 * the rights to redistribute these changes. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/disk.h> 31 #include <sys/disklabel.h> 32 #include <sys/diskmbr.h> 33 #include <sys/endian.h> 34 #include <sys/param.h> 35 #include <sys/stat.h> 36 #include <sys/mount.h> 37 #include <ctype.h> 38 #include <fcntl.h> 39 #include <err.h> 40 #include <errno.h> 41 #include <libgeom.h> 42 #include <paths.h> 43 #include <regex.h> 44 #include <stdint.h> 45 #include <stdio.h> 46 #include <stdlib.h> 47 #include <string.h> 48 #include <unistd.h> 49 50 static int iotest; 51 52 #define NO_DISK_SECTORS ((u_int32_t)-1) 53 #define NO_TRACK_CYLINDERS 1023 54 #define NO_TRACK_HEADS 255 55 #define NO_TRACK_SECTORS 63 56 #define LBUF 100 57 static char lbuf[LBUF]; 58 59 /* 60 * 61 * Ported to 386bsd by Julian Elischer Thu Oct 15 20:26:46 PDT 1992 62 * 63 * 14-Dec-89 Robert Baron (rvb) at Carnegie-Mellon University 64 * Copyright (c) 1989 Robert. V. Baron 65 * Created. 66 */ 67 68 #define Decimal(str, ans, tmp, maxval) if (decimal(str, &tmp, ans, maxval)) ans = tmp 69 70 #define RoundCyl(x) ((((x) + cylsecs - 1) / cylsecs) * cylsecs) 71 72 #define MAX_SEC_SIZE 2048 /* maximum section size that is supported */ 73 #define MIN_SEC_SIZE 512 /* the sector size to start sensing at */ 74 static int secsize = 0; /* the sensed sector size */ 75 76 static char *disk; 77 78 static int cyls, sectors, heads, cylsecs; 79 static u_int32_t disksecs; 80 81 struct mboot { 82 unsigned char *bootinst; /* boot code */ 83 off_t bootinst_size; 84 struct dos_partition parts[NDOSPART]; 85 }; 86 87 static struct mboot mboot; 88 static int fd; 89 90 #define ACTIVE 0x80 91 92 static uint dos_cyls; 93 static uint dos_heads; 94 static uint dos_sectors; 95 static uint dos_cylsecs; 96 97 #define DOSSECT(s,c) ((s & 0x3f) | ((c >> 2) & 0xc0)) 98 #define DOSCYL(c) (c & 0xff) 99 100 #define MAX_ARGS 10 101 102 static int current_line_number; 103 104 static int geom_processed = 0; 105 static int part_processed = 0; 106 static int active_processed = 0; 107 108 typedef struct cmd { 109 char cmd; 110 int n_args; 111 struct arg { 112 char argtype; 113 unsigned long arg_val; 114 char * arg_str; 115 } args[MAX_ARGS]; 116 } CMD; 117 118 static int B_flag = 0; /* replace boot code */ 119 static int I_flag = 0; /* use entire disk for FreeBSD */ 120 static int a_flag = 0; /* set active partition */ 121 static char *b_flag = NULL; /* path to boot code */ 122 static int i_flag = 0; /* replace partition data */ 123 static int q_flag = 0; /* Be quiet */ 124 static int u_flag = 0; /* update partition data */ 125 static int s_flag = 0; /* Print a summary and exit */ 126 static int t_flag = 0; /* test only */ 127 static char *f_flag = NULL; /* Read config info from file */ 128 static int v_flag = 0; /* Be verbose */ 129 static int print_config_flag = 0; 130 131 /* 132 * A list of partition types, probably outdated. 133 */ 134 static const char *const part_types[256] = { 135 [0x00] = "unused", 136 [0x01] = "Primary DOS with 12 bit FAT", 137 [0x02] = "XENIX / file system", 138 [0x03] = "XENIX /usr file system", 139 [0x04] = "Primary DOS with 16 bit FAT (< 32MB)", 140 [0x05] = "Extended DOS", 141 [0x06] = "Primary DOS, 16 bit FAT (>= 32MB)", 142 [0x07] = "NTFS, OS/2 HPFS, QNX-2 (16 bit) or Advanced UNIX", 143 [0x08] = "AIX file system or SplitDrive", 144 [0x09] = "AIX boot partition or Coherent", 145 [0x0A] = "OS/2 Boot Manager, OPUS or Coherent swap", 146 [0x0B] = "DOS or Windows 95 with 32 bit FAT", 147 [0x0C] = "DOS or Windows 95 with 32 bit FAT (LBA)", 148 [0x0E] = "Primary 'big' DOS (>= 32MB, LBA)", 149 [0x0F] = "Extended DOS (LBA)", 150 [0x10] = "OPUS", 151 [0x11] = "OS/2 BM: hidden DOS with 12-bit FAT", 152 [0x12] = "Compaq diagnostics", 153 [0x14] = "OS/2 BM: hidden DOS with 16-bit FAT (< 32MB)", 154 [0x16] = "OS/2 BM: hidden DOS with 16-bit FAT (>= 32MB)", 155 [0x17] = "OS/2 BM: hidden IFS (e.g. HPFS)", 156 [0x18] = "AST Windows swapfile", 157 [0x1b] = "ASUS Recovery partition (NTFS)", 158 [0x24] = "NEC DOS", 159 [0x3C] = "PartitionMagic recovery", 160 [0x39] = "plan9", 161 [0x40] = "VENIX 286", 162 [0x41] = "Linux/MINIX (sharing disk with DRDOS)", 163 [0x42] = "SFS or Linux swap (sharing disk with DRDOS)", 164 [0x43] = "Linux native (sharing disk with DRDOS)", 165 [0x4D] = "QNX 4.2 Primary", 166 [0x4E] = "QNX 4.2 Secondary", 167 [0x4F] = "QNX 4.2 Tertiary", 168 [0x50] = "DM (disk manager)", 169 [0x51] = "DM6 Aux1 (or Novell)", 170 [0x52] = "CP/M or Microport SysV/AT", 171 [0x53] = "DM6 Aux3", 172 [0x54] = "DM6", 173 [0x55] = "EZ-Drive (disk manager)", 174 [0x56] = "Golden Bow (disk manager)", 175 [0x5c] = "Priam Edisk (disk manager)", /* according to S. Widlake */ 176 [0x61] = "SpeedStor", 177 [0x63] = "System V/386 (such as ISC UNIX), GNU HURD or Mach", 178 [0x64] = "Novell Netware/286 2.xx", 179 [0x65] = "Novell Netware/386 3.xx", 180 [0x70] = "DiskSecure Multi-Boot", 181 [0x75] = "PCIX", 182 [0x77] = "QNX4.x", 183 [0x78] = "QNX4.x 2nd part", 184 [0x79] = "QNX4.x 3rd part", 185 [0x80] = "Minix until 1.4a", 186 [0x81] = "Minix since 1.4b, early Linux partition or Mitac disk manager", 187 [0x82] = "Linux swap or Solaris x86", 188 [0x83] = "Linux native", 189 [0x84] = "OS/2 hidden C: drive", 190 [0x85] = "Linux extended", 191 [0x86] = "NTFS volume set??", 192 [0x87] = "NTFS volume set??", 193 [0x93] = "Amoeba file system", 194 [0x94] = "Amoeba bad block table", 195 [0x9F] = "BSD/OS", 196 [0xA0] = "Suspend to Disk", 197 [0xA5] = "FreeBSD/NetBSD/386BSD", 198 [0xA6] = "OpenBSD", 199 [0xA7] = "NeXTSTEP", 200 [0xA9] = "NetBSD", 201 [0xAC] = "IBM JFS", 202 [0xAF] = "HFS+", 203 [0xB7] = "BSDI BSD/386 file system", 204 [0xB8] = "BSDI BSD/386 swap", 205 [0xBE] = "Solaris x86 boot", 206 [0xBF] = "Solaris x86 (new)", 207 [0xC1] = "DRDOS/sec with 12-bit FAT", 208 [0xC4] = "DRDOS/sec with 16-bit FAT (< 32MB)", 209 [0xC6] = "DRDOS/sec with 16-bit FAT (>= 32MB)", 210 [0xC7] = "Syrinx", 211 [0xDB] = "CP/M, Concurrent CP/M, Concurrent DOS or CTOS", 212 [0xDE] = "DELL Utilities - FAT filesystem", 213 [0xE1] = "DOS access or SpeedStor with 12-bit FAT extended partition", 214 [0xE3] = "DOS R/O or SpeedStor", 215 [0xE4] = "SpeedStor with 16-bit FAT extended partition < 1024 cyl.", 216 [0xEB] = "BeOS file system", 217 [0xEE] = "EFI GPT", 218 [0xEF] = "EFI System Partition", 219 [0xF1] = "SpeedStor", 220 [0xF2] = "DOS 3.3+ Secondary", 221 [0xF4] = "SpeedStor large partition", 222 [0xFB] = "VMware VMFS", 223 [0xFE] = "SpeedStor >1024 cyl. or LANstep", 224 [0xFF] = "Xenix bad blocks table", 225 }; 226 227 static const char * 228 get_type(int t) 229 { 230 const char *ret; 231 232 ret = (t >= 0 && t <= 255) ? part_types[t] : NULL; 233 return ret ? ret : "unknown"; 234 } 235 236 237 static int geom_class_available(const char *); 238 static void print_s0(void); 239 static void print_part(const struct dos_partition *); 240 static void init_sector0(unsigned long start); 241 static void init_boot(void); 242 static void change_part(int i); 243 static void print_params(void); 244 static void change_active(int which); 245 static void change_code(void); 246 static void get_params_to_use(void); 247 static char *get_rootdisk(void); 248 static void dos(struct dos_partition *partp); 249 static int open_disk(int flag); 250 static ssize_t read_disk(off_t sector, void *buf); 251 static int write_disk(off_t sector, void *buf); 252 static int get_params(void); 253 static int read_s0(void); 254 static int write_s0(void); 255 static int ok(const char *str); 256 static int decimal(const char *str, int *num, int deflt, uint32_t maxval); 257 static int read_config(char *config_file); 258 static void reset_boot(void); 259 static int sanitize_partition(struct dos_partition *); 260 static void usage(void); 261 262 int 263 main(int argc, char *argv[]) 264 { 265 int c, i; 266 int partition = -1; 267 struct dos_partition *partp; 268 269 while ((c = getopt(argc, argv, "BIab:f:ipqstuv1234")) != -1) 270 switch (c) { 271 case 'B': 272 B_flag = 1; 273 break; 274 case 'I': 275 I_flag = 1; 276 break; 277 case 'a': 278 a_flag = 1; 279 break; 280 case 'b': 281 b_flag = optarg; 282 break; 283 case 'f': 284 f_flag = optarg; 285 break; 286 case 'i': 287 i_flag = 1; 288 break; 289 case 'p': 290 print_config_flag = 1; 291 break; 292 case 'q': 293 q_flag = 1; 294 break; 295 case 's': 296 s_flag = 1; 297 break; 298 case 't': 299 t_flag = 1; 300 break; 301 case 'u': 302 u_flag = 1; 303 break; 304 case 'v': 305 v_flag = 1; 306 break; 307 case '1': 308 case '2': 309 case '3': 310 case '4': 311 partition = c - '0'; 312 break; 313 default: 314 usage(); 315 } 316 if (f_flag || i_flag) 317 u_flag = 1; 318 if (t_flag) 319 v_flag = 1; 320 argc -= optind; 321 argv += optind; 322 323 if (argc == 0) { 324 disk = get_rootdisk(); 325 } else { 326 disk = g_device_path(argv[0]); 327 if (disk == NULL) 328 err(1, "unable to get correct path for %s", argv[0]); 329 } 330 if (open_disk(u_flag) < 0) 331 err(1, "cannot open disk %s", disk); 332 333 /* (abu)use mboot.bootinst to probe for the sector size */ 334 if ((mboot.bootinst = malloc(MAX_SEC_SIZE)) == NULL) 335 err(1, "cannot allocate buffer to determine disk sector size"); 336 if (read_disk(0, mboot.bootinst) == -1) 337 errx(1, "could not detect sector size"); 338 free(mboot.bootinst); 339 mboot.bootinst = NULL; 340 341 if (print_config_flag) { 342 if (read_s0()) 343 err(1, "read_s0"); 344 345 printf("# %s\n", disk); 346 printf("g c%d h%d s%d\n", dos_cyls, dos_heads, dos_sectors); 347 348 for (i = 0; i < NDOSPART; i++) { 349 partp = &mboot.parts[i]; 350 351 if (partp->dp_start == 0 && partp->dp_size == 0) 352 continue; 353 354 printf("p %d 0x%02x %lu %lu\n", i + 1, partp->dp_typ, 355 (u_long)partp->dp_start, (u_long)partp->dp_size); 356 357 /* Fill flags for the partition. */ 358 if (partp->dp_flag & 0x80) 359 printf("a %d\n", i + 1); 360 } 361 exit(0); 362 } 363 if (s_flag) { 364 if (read_s0()) 365 err(1, "read_s0"); 366 printf("%s: %d cyl %d hd %d sec\n", disk, dos_cyls, dos_heads, 367 dos_sectors); 368 printf("Part %11s %11s Type Flags\n", "Start", "Size"); 369 for (i = 0; i < NDOSPART; i++) { 370 partp = &mboot.parts[i]; 371 if (partp->dp_start == 0 && partp->dp_size == 0) 372 continue; 373 printf("%4d: %11lu %11lu 0x%02x 0x%02x\n", i + 1, 374 (u_long) partp->dp_start, 375 (u_long) partp->dp_size, partp->dp_typ, 376 partp->dp_flag); 377 } 378 exit(0); 379 } 380 381 printf("******* Working on device %s *******\n",disk); 382 383 if (I_flag) { 384 read_s0(); 385 reset_boot(); 386 partp = &mboot.parts[0]; 387 partp->dp_typ = DOSPTYP_386BSD; 388 partp->dp_flag = ACTIVE; 389 partp->dp_start = dos_sectors; 390 partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs - 391 dos_sectors; 392 dos(partp); 393 if (v_flag) 394 print_s0(); 395 if (!t_flag) 396 write_s0(); 397 exit(0); 398 } 399 if (f_flag) { 400 if (read_s0() || i_flag) 401 reset_boot(); 402 if (!read_config(f_flag)) 403 exit(1); 404 if (v_flag) 405 print_s0(); 406 if (!t_flag) 407 write_s0(); 408 } else { 409 if(u_flag) 410 get_params_to_use(); 411 else 412 print_params(); 413 414 if (read_s0()) 415 init_sector0(dos_sectors); 416 417 printf("Media sector size is %d\n", secsize); 418 printf("Warning: BIOS sector numbering starts with sector 1\n"); 419 printf("Information from DOS bootblock is:\n"); 420 if (partition == -1) 421 for (i = 1; i <= NDOSPART; i++) 422 change_part(i); 423 else 424 change_part(partition); 425 426 if (u_flag || a_flag) 427 change_active(partition); 428 429 if (B_flag) 430 change_code(); 431 432 if (u_flag || a_flag || B_flag) { 433 if (!t_flag) { 434 printf("\nWe haven't changed the partition table yet. "); 435 printf("This is your last chance.\n"); 436 } 437 print_s0(); 438 if (!t_flag) { 439 if (ok("Should we write new partition table?")) 440 write_s0(); 441 } else { 442 printf("\n-t flag specified -- partition table not written.\n"); 443 } 444 } 445 } 446 447 exit(0); 448 } 449 450 static void 451 usage() 452 { 453 fprintf(stderr, "%s%s", 454 "usage: fdisk [-BIaipqstu] [-b bootcode] [-1234] [disk]\n", 455 " fdisk -f configfile [-itv] [disk]\n"); 456 exit(1); 457 } 458 459 static void 460 print_s0(void) 461 { 462 int i; 463 464 print_params(); 465 printf("Information from DOS bootblock is:\n"); 466 for (i = 1; i <= NDOSPART; i++) { 467 printf("%d: ", i); 468 print_part(&mboot.parts[i - 1]); 469 } 470 } 471 472 static struct dos_partition mtpart; 473 474 static void 475 print_part(const struct dos_partition *partp) 476 { 477 u_int64_t part_mb; 478 479 if (!bcmp(partp, &mtpart, sizeof (struct dos_partition))) { 480 printf("<UNUSED>\n"); 481 return; 482 } 483 /* 484 * Be careful not to overflow. 485 */ 486 part_mb = partp->dp_size; 487 part_mb *= secsize; 488 part_mb /= (1024 * 1024); 489 printf("sysid %d (%#04x),(%s)\n", partp->dp_typ, partp->dp_typ, 490 get_type(partp->dp_typ)); 491 printf(" start %lu, size %lu (%ju Meg), flag %x%s\n", 492 (u_long)partp->dp_start, 493 (u_long)partp->dp_size, 494 (uintmax_t)part_mb, 495 partp->dp_flag, 496 partp->dp_flag == ACTIVE ? " (active)" : ""); 497 printf("\tbeg: cyl %d/ head %d/ sector %d;\n\tend: cyl %d/ head %d/ sector %d\n" 498 ,DPCYL(partp->dp_scyl, partp->dp_ssect) 499 ,partp->dp_shd 500 ,DPSECT(partp->dp_ssect) 501 ,DPCYL(partp->dp_ecyl, partp->dp_esect) 502 ,partp->dp_ehd 503 ,DPSECT(partp->dp_esect)); 504 } 505 506 507 static void 508 init_boot(void) 509 { 510 const char *fname; 511 int fdesc, n; 512 struct stat sb; 513 514 fname = b_flag ? b_flag : "/boot/mbr"; 515 if ((fdesc = open(fname, O_RDONLY)) == -1 || 516 fstat(fdesc, &sb) == -1) 517 err(1, "%s", fname); 518 if (sb.st_size == 0) 519 errx(1, "%s is empty, must not be.", fname); 520 if ((mboot.bootinst_size = sb.st_size) % secsize != 0) 521 errx(1, "%s: length must be a multiple of sector size", fname); 522 if (mboot.bootinst != NULL) 523 free(mboot.bootinst); 524 if ((mboot.bootinst = malloc(mboot.bootinst_size = sb.st_size)) == NULL) 525 errx(1, "%s: unable to allocate read buffer", fname); 526 if ((n = read(fdesc, mboot.bootinst, mboot.bootinst_size)) == -1 || 527 close(fdesc)) 528 err(1, "%s", fname); 529 if (n != mboot.bootinst_size) 530 errx(1, "%s: short read", fname); 531 } 532 533 534 static void 535 init_sector0(unsigned long start) 536 { 537 struct dos_partition *partp = &mboot.parts[0]; 538 539 init_boot(); 540 541 partp->dp_typ = DOSPTYP_386BSD; 542 partp->dp_flag = ACTIVE; 543 start = ((start + dos_sectors - 1) / dos_sectors) * dos_sectors; 544 if(start == 0) 545 start = dos_sectors; 546 partp->dp_start = start; 547 partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs - start; 548 549 dos(partp); 550 } 551 552 static void 553 change_part(int i) 554 { 555 struct dos_partition *partp = &mboot.parts[i - 1]; 556 557 printf("The data for partition %d is:\n", i); 558 print_part(partp); 559 560 if (u_flag && ok("Do you want to change it?")) { 561 int tmp; 562 563 if (i_flag) { 564 bzero(partp, sizeof (*partp)); 565 if (i == 1) { 566 init_sector0(1); 567 printf("\nThe static data for the slice 1 has been reinitialized to:\n"); 568 print_part(partp); 569 } 570 } 571 572 do { 573 Decimal("sysid (165=FreeBSD)", partp->dp_typ, tmp, 255); 574 Decimal("start", partp->dp_start, tmp, NO_DISK_SECTORS); 575 Decimal("size", partp->dp_size, tmp, NO_DISK_SECTORS); 576 if (!sanitize_partition(partp)) { 577 warnx("ERROR: failed to adjust; setting sysid to 0"); 578 partp->dp_typ = 0; 579 } 580 581 if (ok("Explicitly specify beg/end address ?")) 582 { 583 int tsec,tcyl,thd; 584 tcyl = DPCYL(partp->dp_scyl,partp->dp_ssect); 585 thd = partp->dp_shd; 586 tsec = DPSECT(partp->dp_ssect); 587 Decimal("beginning cylinder", tcyl, tmp, NO_TRACK_CYLINDERS); 588 Decimal("beginning head", thd, tmp, NO_TRACK_HEADS); 589 Decimal("beginning sector", tsec, tmp, NO_TRACK_SECTORS); 590 partp->dp_scyl = DOSCYL(tcyl); 591 partp->dp_ssect = DOSSECT(tsec,tcyl); 592 partp->dp_shd = thd; 593 594 tcyl = DPCYL(partp->dp_ecyl,partp->dp_esect); 595 thd = partp->dp_ehd; 596 tsec = DPSECT(partp->dp_esect); 597 Decimal("ending cylinder", tcyl, tmp, NO_TRACK_CYLINDERS); 598 Decimal("ending head", thd, tmp, NO_TRACK_HEADS); 599 Decimal("ending sector", tsec, tmp, NO_TRACK_SECTORS); 600 partp->dp_ecyl = DOSCYL(tcyl); 601 partp->dp_esect = DOSSECT(tsec,tcyl); 602 partp->dp_ehd = thd; 603 } else 604 dos(partp); 605 606 print_part(partp); 607 } while (!ok("Are we happy with this entry?")); 608 } 609 } 610 611 static void 612 print_params() 613 { 614 printf("parameters extracted from in-core disklabel are:\n"); 615 printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n" 616 ,cyls,heads,sectors,cylsecs); 617 if (dos_cyls > 1023 || dos_heads > 255 || dos_sectors > 63) 618 printf("Figures below won't work with BIOS for partitions not in cyl 1\n"); 619 printf("parameters to be used for BIOS calculations are:\n"); 620 printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n" 621 ,dos_cyls,dos_heads,dos_sectors,dos_cylsecs); 622 } 623 624 static void 625 change_active(int which) 626 { 627 struct dos_partition *partp = &mboot.parts[0]; 628 int active, i, new, tmp; 629 630 active = -1; 631 for (i = 0; i < NDOSPART; i++) { 632 if ((partp[i].dp_flag & ACTIVE) == 0) 633 continue; 634 printf("Partition %d is marked active\n", i + 1); 635 if (active == -1) 636 active = i + 1; 637 } 638 if (a_flag && which != -1) 639 active = which; 640 else if (active == -1) 641 active = 1; 642 643 if (!ok("Do you want to change the active partition?")) 644 return; 645 setactive: 646 do { 647 new = active; 648 Decimal("active partition", new, tmp, 0); 649 if (new < 1 || new > 4) { 650 printf("Active partition number must be in range 1-4." 651 " Try again.\n"); 652 goto setactive; 653 } 654 active = new; 655 } while (!ok("Are you happy with this choice")); 656 for (i = 0; i < NDOSPART; i++) 657 partp[i].dp_flag = 0; 658 if (active > 0 && active <= NDOSPART) 659 partp[active-1].dp_flag = ACTIVE; 660 } 661 662 static void 663 change_code() 664 { 665 if (ok("Do you want to change the boot code?")) 666 init_boot(); 667 } 668 669 void 670 get_params_to_use() 671 { 672 int tmp; 673 print_params(); 674 if (ok("Do you want to change our idea of what BIOS thinks ?")) 675 { 676 do 677 { 678 Decimal("BIOS's idea of #cylinders", dos_cyls, tmp, 0); 679 Decimal("BIOS's idea of #heads", dos_heads, tmp, 0); 680 Decimal("BIOS's idea of #sectors", dos_sectors, tmp, 0); 681 dos_cylsecs = dos_heads * dos_sectors; 682 print_params(); 683 } 684 while(!ok("Are you happy with this choice")); 685 } 686 } 687 688 689 /***********************************************\ 690 * Change real numbers into strange dos numbers * 691 \***********************************************/ 692 static void 693 dos(struct dos_partition *partp) 694 { 695 int cy, sec; 696 u_int32_t end; 697 698 if (partp->dp_typ == 0 && partp->dp_start == 0 && partp->dp_size == 0) { 699 memcpy(partp, &mtpart, sizeof(*partp)); 700 return; 701 } 702 703 /* Start c/h/s. */ 704 partp->dp_shd = partp->dp_start % dos_cylsecs / dos_sectors; 705 cy = partp->dp_start / dos_cylsecs; 706 sec = partp->dp_start % dos_sectors + 1; 707 partp->dp_scyl = DOSCYL(cy); 708 partp->dp_ssect = DOSSECT(sec, cy); 709 710 /* End c/h/s. */ 711 end = partp->dp_start + partp->dp_size - 1; 712 partp->dp_ehd = end % dos_cylsecs / dos_sectors; 713 cy = end / dos_cylsecs; 714 sec = end % dos_sectors + 1; 715 partp->dp_ecyl = DOSCYL(cy); 716 partp->dp_esect = DOSSECT(sec, cy); 717 } 718 719 static int 720 open_disk(int flag) 721 { 722 int rwmode; 723 724 /* Write mode if one of these flags are set. */ 725 rwmode = (a_flag || I_flag || B_flag || flag); 726 fd = g_open(disk, rwmode); 727 /* If the mode fails, try read-only if we didn't. */ 728 if (fd == -1 && errno == EPERM && rwmode) 729 fd = g_open(disk, 0); 730 if (fd == -1 && errno == ENXIO) 731 return -2; 732 if (fd == -1) { 733 warnx("can't open device %s", disk); 734 return -1; 735 } 736 if (get_params() == -1) { 737 warnx("can't get disk parameters on %s", disk); 738 return -1; 739 } 740 return fd; 741 } 742 743 static ssize_t 744 read_disk(off_t sector, void *buf) 745 { 746 747 lseek(fd, (sector * 512), 0); 748 if (secsize == 0) 749 for (secsize = MIN_SEC_SIZE; secsize <= MAX_SEC_SIZE; 750 secsize *= 2) { 751 /* try the read */ 752 int size = read(fd, buf, secsize); 753 if (size == secsize) 754 /* it worked so return */ 755 return secsize; 756 } 757 else 758 return read(fd, buf, secsize); 759 760 /* we failed to read at any of the sizes */ 761 return -1; 762 } 763 764 static int 765 geom_class_available(const char *name) 766 { 767 struct gclass *class; 768 struct gmesh mesh; 769 int error; 770 771 error = geom_gettree(&mesh); 772 if (error != 0) 773 errc(1, error, "Cannot get GEOM tree"); 774 775 LIST_FOREACH(class, &mesh.lg_class, lg_class) { 776 if (strcmp(class->lg_name, name) == 0) { 777 geom_deletetree(&mesh); 778 return (1); 779 } 780 } 781 782 geom_deletetree(&mesh); 783 784 return (0); 785 } 786 787 static int 788 write_disk(off_t sector, void *buf) 789 { 790 struct gctl_req *grq; 791 const char *errmsg; 792 char *pname; 793 int error; 794 795 /* Check that GEOM_MBR is available */ 796 if (geom_class_available("MBR") != 0) { 797 grq = gctl_get_handle(); 798 gctl_ro_param(grq, "verb", -1, "write MBR"); 799 gctl_ro_param(grq, "class", -1, "MBR"); 800 pname = g_providername(fd); 801 if (pname == NULL) { 802 warn("Error getting providername for %s", disk); 803 return (-1); 804 } 805 gctl_ro_param(grq, "geom", -1, pname); 806 gctl_ro_param(grq, "data", secsize, buf); 807 errmsg = gctl_issue(grq); 808 free(pname); 809 if (errmsg == NULL) { 810 gctl_free(grq); 811 return(0); 812 } 813 if (!q_flag) 814 warnx("GEOM_MBR: %s", errmsg); 815 gctl_free(grq); 816 } else { 817 /* 818 * Try to write MBR directly. This may help when disk 819 * is not in use. 820 * XXX: hardcoded sectorsize 821 */ 822 error = pwrite(fd, buf, secsize, (sector * 512)); 823 if (error == secsize) 824 return (0); 825 } 826 827 /* 828 * GEOM_MBR is not available or failed to write MBR. 829 * Now check that we have GEOM_PART and recommend to use gpart (8). 830 */ 831 if (geom_class_available("PART") != 0) 832 warnx("Failed to write MBR. Try to use gpart(8)."); 833 else 834 warnx("Failed to write sector zero"); 835 return(EINVAL); 836 } 837 838 static int 839 get_params() 840 { 841 int error; 842 u_int u; 843 off_t o; 844 845 error = ioctl(fd, DIOCGFWSECTORS, &u); 846 if (error == 0) 847 sectors = dos_sectors = u; 848 else 849 sectors = dos_sectors = 63; 850 851 error = ioctl(fd, DIOCGFWHEADS, &u); 852 if (error == 0) 853 heads = dos_heads = u; 854 else 855 heads = dos_heads = 255; 856 857 dos_cylsecs = cylsecs = heads * sectors; 858 disksecs = cyls * heads * sectors; 859 860 u = g_sectorsize(fd); 861 if (u <= 0) 862 return (-1); 863 864 o = g_mediasize(fd); 865 if (o < 0) 866 return (-1); 867 if (o / u <= NO_DISK_SECTORS) 868 disksecs = o / u; 869 else 870 disksecs = NO_DISK_SECTORS; 871 cyls = dos_cyls = o / (u * dos_heads * dos_sectors); 872 873 return (0); 874 } 875 876 static int 877 read_s0() 878 { 879 int i; 880 881 mboot.bootinst_size = secsize; 882 if (mboot.bootinst != NULL) 883 free(mboot.bootinst); 884 if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL) { 885 warnx("unable to allocate buffer to read fdisk " 886 "partition table"); 887 return -1; 888 } 889 if (read_disk(0, mboot.bootinst) == -1) { 890 warnx("can't read fdisk partition table"); 891 return -1; 892 } 893 if (le16dec(&mboot.bootinst[DOSMAGICOFFSET]) != DOSMAGIC) { 894 warnx("invalid fdisk partition table found"); 895 /* So should we initialize things */ 896 return -1; 897 } 898 for (i = 0; i < NDOSPART; i++) 899 dos_partition_dec( 900 &mboot.bootinst[DOSPARTOFF + i * DOSPARTSIZE], 901 &mboot.parts[i]); 902 return 0; 903 } 904 905 static int 906 write_s0() 907 { 908 int sector, i; 909 910 if (iotest) { 911 print_s0(); 912 return 0; 913 } 914 for(i = 0; i < NDOSPART; i++) 915 dos_partition_enc(&mboot.bootinst[DOSPARTOFF + i * DOSPARTSIZE], 916 &mboot.parts[i]); 917 le16enc(&mboot.bootinst[DOSMAGICOFFSET], DOSMAGIC); 918 for(sector = 0; sector < mboot.bootinst_size / secsize; sector++) 919 if (write_disk(sector, 920 &mboot.bootinst[sector * secsize]) == -1) { 921 warn("can't write fdisk partition table"); 922 return -1; 923 } 924 return(0); 925 } 926 927 928 static int 929 ok(const char *str) 930 { 931 printf("%s [n] ", str); 932 fflush(stdout); 933 if (fgets(lbuf, LBUF, stdin) == NULL) 934 exit(1); 935 lbuf[strlen(lbuf)-1] = 0; 936 937 if (*lbuf && 938 (!strcmp(lbuf, "yes") || !strcmp(lbuf, "YES") || 939 !strcmp(lbuf, "y") || !strcmp(lbuf, "Y"))) 940 return 1; 941 else 942 return 0; 943 } 944 945 static int 946 decimal(const char *str, int *num, int deflt, uint32_t maxval) 947 { 948 long long acc; 949 int c; 950 char *cp; 951 952 while (1) { 953 acc = 0; 954 printf("Supply a decimal value for \"%s\" [%d] ", str, deflt); 955 fflush(stdout); 956 if (fgets(lbuf, LBUF, stdin) == NULL) 957 exit(1); 958 lbuf[strlen(lbuf)-1] = 0; 959 960 if (!*lbuf) 961 return 0; 962 963 cp = lbuf; 964 while ((c = *cp) && (c == ' ' || c == '\t')) cp++; 965 if (!c) 966 return 0; 967 while ((c = *cp++)) { 968 if (c <= '9' && c >= '0') { 969 if (acc <= maxval || maxval == 0) 970 acc = acc * 10 + c - '0'; 971 } else 972 break; 973 } 974 if (c == ' ' || c == '\t') 975 while ((c = *cp) && (c == ' ' || c == '\t')) cp++; 976 if (!c) { 977 if (maxval > 0 && acc > maxval) { 978 acc = maxval; 979 printf("%s exceeds maximum value allowed for " 980 "this field. The value has been reduced " 981 "to %lld\n", lbuf, acc); 982 } 983 *num = acc; 984 return 1; 985 } else 986 printf("%s is an invalid decimal number. Try again.\n", 987 lbuf); 988 } 989 } 990 991 992 static void 993 parse_config_line(char *line, CMD *command) 994 { 995 char *cp, *end; 996 997 cp = line; 998 while (1) { 999 memset(command, 0, sizeof(*command)); 1000 1001 while (isspace(*cp)) ++cp; 1002 if (*cp == '\0' || *cp == '#') 1003 break; 1004 command->cmd = *cp++; 1005 1006 /* 1007 * Parse args 1008 */ 1009 while (1) { 1010 while (isspace(*cp)) ++cp; 1011 if (*cp == '\0') 1012 break; /* eol */ 1013 if (*cp == '#') 1014 break; /* found comment */ 1015 if (isalpha(*cp)) 1016 command->args[command->n_args].argtype = *cp++; 1017 end = NULL; 1018 command->args[command->n_args].arg_val = strtoul(cp, &end, 0); 1019 if (cp == end || (!isspace(*end) && *end != '\0')) { 1020 char ch; 1021 end = cp; 1022 while (!isspace(*end) && *end != '\0') ++end; 1023 ch = *end; *end = '\0'; 1024 command->args[command->n_args].arg_str = strdup(cp); 1025 *end = ch; 1026 } else 1027 command->args[command->n_args].arg_str = NULL; 1028 cp = end; 1029 command->n_args++; 1030 } 1031 break; 1032 } 1033 } 1034 1035 1036 static int 1037 process_geometry(CMD *command) 1038 { 1039 int status = 1, i; 1040 1041 while (1) { 1042 geom_processed = 1; 1043 if (part_processed) { 1044 warnx( 1045 "ERROR line %d: the geometry specification line must occur before\n\ 1046 all partition specifications", 1047 current_line_number); 1048 status = 0; 1049 break; 1050 } 1051 if (command->n_args != 3) { 1052 warnx("ERROR line %d: incorrect number of geometry args", 1053 current_line_number); 1054 status = 0; 1055 break; 1056 } 1057 dos_cyls = 0; 1058 dos_heads = 0; 1059 dos_sectors = 0; 1060 for (i = 0; i < 3; ++i) { 1061 switch (command->args[i].argtype) { 1062 case 'c': 1063 dos_cyls = command->args[i].arg_val; 1064 break; 1065 case 'h': 1066 dos_heads = command->args[i].arg_val; 1067 break; 1068 case 's': 1069 dos_sectors = command->args[i].arg_val; 1070 break; 1071 default: 1072 warnx( 1073 "ERROR line %d: unknown geometry arg type: '%c' (0x%02x)", 1074 current_line_number, command->args[i].argtype, 1075 command->args[i].argtype); 1076 status = 0; 1077 break; 1078 } 1079 } 1080 if (status == 0) 1081 break; 1082 1083 dos_cylsecs = dos_heads * dos_sectors; 1084 1085 /* 1086 * Do sanity checks on parameter values 1087 */ 1088 if (dos_cyls == 0) { 1089 warnx("ERROR line %d: number of cylinders not specified", 1090 current_line_number); 1091 status = 0; 1092 } 1093 if (dos_cyls > 1024) { 1094 warnx( 1095 "WARNING line %d: number of cylinders (%d) may be out-of-range\n\ 1096 (must be within 1-1024 for normal BIOS operation, unless the entire disk\n\ 1097 is dedicated to FreeBSD)", 1098 current_line_number, dos_cyls); 1099 } 1100 1101 if (dos_heads == 0) { 1102 warnx("ERROR line %d: number of heads not specified", 1103 current_line_number); 1104 status = 0; 1105 } else if (dos_heads > 256) { 1106 warnx("ERROR line %d: number of heads must be within (1-256)", 1107 current_line_number); 1108 status = 0; 1109 } 1110 1111 if (dos_sectors == 0) { 1112 warnx("ERROR line %d: number of sectors not specified", 1113 current_line_number); 1114 status = 0; 1115 } else if (dos_sectors > 63) { 1116 warnx("ERROR line %d: number of sectors must be within (1-63)", 1117 current_line_number); 1118 status = 0; 1119 } 1120 1121 break; 1122 } 1123 return (status); 1124 } 1125 1126 static u_int32_t 1127 str2sectors(const char *str) 1128 { 1129 char *end; 1130 unsigned long val; 1131 1132 val = strtoul(str, &end, 0); 1133 if (str == end || *end == '\0') { 1134 warnx("ERROR line %d: unexpected size: \'%s\'", 1135 current_line_number, str); 1136 return NO_DISK_SECTORS; 1137 } 1138 1139 if (*end == 'K') 1140 val *= 1024UL / secsize; 1141 else if (*end == 'M') 1142 val *= 1024UL * 1024UL / secsize; 1143 else if (*end == 'G') 1144 val *= 1024UL * 1024UL * 1024UL / secsize; 1145 else { 1146 warnx("ERROR line %d: unexpected modifier: %c " 1147 "(not K/M/G)", current_line_number, *end); 1148 return NO_DISK_SECTORS; 1149 } 1150 1151 return val; 1152 } 1153 1154 static int 1155 process_partition(CMD *command) 1156 { 1157 int status = 0, partition; 1158 u_int32_t prev_head_boundary, prev_cyl_boundary; 1159 u_int32_t adj_size, max_end; 1160 struct dos_partition *partp; 1161 1162 while (1) { 1163 part_processed = 1; 1164 if (command->n_args != 4) { 1165 warnx("ERROR line %d: incorrect number of partition args", 1166 current_line_number); 1167 break; 1168 } 1169 partition = command->args[0].arg_val; 1170 if (partition < 1 || partition > 4) { 1171 warnx("ERROR line %d: invalid partition number %d", 1172 current_line_number, partition); 1173 break; 1174 } 1175 partp = &mboot.parts[partition - 1]; 1176 bzero(partp, sizeof (*partp)); 1177 partp->dp_typ = command->args[1].arg_val; 1178 if (command->args[2].arg_str != NULL) { 1179 if (strcmp(command->args[2].arg_str, "*") == 0) { 1180 int i; 1181 partp->dp_start = dos_sectors; 1182 for (i = 1; i < partition; i++) { 1183 struct dos_partition *prev_partp; 1184 prev_partp = ((struct dos_partition *) 1185 &mboot.parts) + i - 1; 1186 if (prev_partp->dp_typ != 0) 1187 partp->dp_start = prev_partp->dp_start + 1188 prev_partp->dp_size; 1189 } 1190 if (partp->dp_start % dos_sectors != 0) { 1191 prev_head_boundary = partp->dp_start / 1192 dos_sectors * dos_sectors; 1193 partp->dp_start = prev_head_boundary + 1194 dos_sectors; 1195 } 1196 } else { 1197 partp->dp_start = str2sectors(command->args[2].arg_str); 1198 if (partp->dp_start == NO_DISK_SECTORS) 1199 break; 1200 } 1201 } else 1202 partp->dp_start = command->args[2].arg_val; 1203 1204 if (command->args[3].arg_str != NULL) { 1205 if (strcmp(command->args[3].arg_str, "*") == 0) 1206 partp->dp_size = ((disksecs / dos_cylsecs) * 1207 dos_cylsecs) - partp->dp_start; 1208 else { 1209 partp->dp_size = str2sectors(command->args[3].arg_str); 1210 if (partp->dp_size == NO_DISK_SECTORS) 1211 break; 1212 } 1213 prev_cyl_boundary = ((partp->dp_start + partp->dp_size) / 1214 dos_cylsecs) * dos_cylsecs; 1215 if (prev_cyl_boundary > partp->dp_start) 1216 partp->dp_size = prev_cyl_boundary - partp->dp_start; 1217 } else 1218 partp->dp_size = command->args[3].arg_val; 1219 1220 max_end = partp->dp_start + partp->dp_size; 1221 1222 if (partp->dp_typ == 0) { 1223 /* 1224 * Get out, the partition is marked as unused. 1225 */ 1226 /* 1227 * Insure that it's unused. 1228 */ 1229 bzero(partp, sizeof(*partp)); 1230 status = 1; 1231 break; 1232 } 1233 1234 /* 1235 * Adjust start upwards, if necessary, to fall on a head boundary. 1236 */ 1237 if (partp->dp_start % dos_sectors != 0) { 1238 prev_head_boundary = partp->dp_start / dos_sectors * dos_sectors; 1239 if (max_end < dos_sectors || 1240 prev_head_boundary > max_end - dos_sectors) { 1241 /* 1242 * Can't go past end of partition 1243 */ 1244 warnx( 1245 "ERROR line %d: unable to adjust start of partition %d to fall on\n\ 1246 a head boundary", 1247 current_line_number, partition); 1248 break; 1249 } 1250 warnx( 1251 "WARNING: adjusting start offset of partition %d\n\ 1252 from %u to %u, to fall on a head boundary", 1253 partition, (u_int)partp->dp_start, 1254 (u_int)(prev_head_boundary + dos_sectors)); 1255 partp->dp_start = prev_head_boundary + dos_sectors; 1256 } 1257 1258 /* 1259 * Adjust size downwards, if necessary, to fall on a cylinder 1260 * boundary. 1261 */ 1262 prev_cyl_boundary = 1263 ((partp->dp_start + partp->dp_size) / dos_cylsecs) * dos_cylsecs; 1264 if (prev_cyl_boundary > partp->dp_start) 1265 adj_size = prev_cyl_boundary - partp->dp_start; 1266 else { 1267 warnx( 1268 "ERROR: could not adjust partition to start on a head boundary\n\ 1269 and end on a cylinder boundary."); 1270 return (0); 1271 } 1272 if (adj_size != partp->dp_size) { 1273 warnx( 1274 "WARNING: adjusting size of partition %d from %u to %u\n\ 1275 to end on a cylinder boundary", 1276 partition, (u_int)partp->dp_size, (u_int)adj_size); 1277 partp->dp_size = adj_size; 1278 } 1279 if (partp->dp_size == 0) { 1280 warnx("ERROR line %d: size of partition %d is zero", 1281 current_line_number, partition); 1282 break; 1283 } 1284 1285 dos(partp); 1286 status = 1; 1287 break; 1288 } 1289 return (status); 1290 } 1291 1292 1293 static int 1294 process_active(CMD *command) 1295 { 1296 int status = 0, partition, i; 1297 struct dos_partition *partp; 1298 1299 while (1) { 1300 active_processed = 1; 1301 if (command->n_args != 1) { 1302 warnx("ERROR line %d: incorrect number of active args", 1303 current_line_number); 1304 status = 0; 1305 break; 1306 } 1307 partition = command->args[0].arg_val; 1308 if (partition < 1 || partition > 4) { 1309 warnx("ERROR line %d: invalid partition number %d", 1310 current_line_number, partition); 1311 break; 1312 } 1313 /* 1314 * Reset active partition 1315 */ 1316 partp = mboot.parts; 1317 for (i = 0; i < NDOSPART; i++) 1318 partp[i].dp_flag = 0; 1319 partp[partition-1].dp_flag = ACTIVE; 1320 1321 status = 1; 1322 break; 1323 } 1324 return (status); 1325 } 1326 1327 1328 static int 1329 process_line(char *line) 1330 { 1331 CMD command; 1332 int status = 1; 1333 1334 while (1) { 1335 parse_config_line(line, &command); 1336 switch (command.cmd) { 1337 case 0: 1338 /* 1339 * Comment or blank line 1340 */ 1341 break; 1342 case 'g': 1343 /* 1344 * Set geometry 1345 */ 1346 status = process_geometry(&command); 1347 break; 1348 case 'p': 1349 status = process_partition(&command); 1350 break; 1351 case 'a': 1352 status = process_active(&command); 1353 break; 1354 default: 1355 status = 0; 1356 break; 1357 } 1358 break; 1359 } 1360 return (status); 1361 } 1362 1363 1364 static int 1365 read_config(char *config_file) 1366 { 1367 FILE *fp = NULL; 1368 int status = 1; 1369 char buf[1010]; 1370 1371 while (1) { 1372 if (strcmp(config_file, "-") != 0) { 1373 /* 1374 * We're not reading from stdin 1375 */ 1376 if ((fp = fopen(config_file, "r")) == NULL) { 1377 status = 0; 1378 break; 1379 } 1380 } else { 1381 fp = stdin; 1382 } 1383 current_line_number = 0; 1384 while (!feof(fp)) { 1385 if (fgets(buf, sizeof(buf), fp) == NULL) 1386 break; 1387 ++current_line_number; 1388 status = process_line(buf); 1389 if (status == 0) 1390 break; 1391 } 1392 break; 1393 } 1394 if (fp) { 1395 /* 1396 * It doesn't matter if we're reading from stdin, as we've reached EOF 1397 */ 1398 fclose(fp); 1399 } 1400 return (status); 1401 } 1402 1403 1404 static void 1405 reset_boot(void) 1406 { 1407 int i; 1408 struct dos_partition *partp; 1409 1410 init_boot(); 1411 for (i = 0; i < 4; ++i) { 1412 partp = &mboot.parts[i]; 1413 bzero(partp, sizeof(*partp)); 1414 } 1415 } 1416 1417 static int 1418 sanitize_partition(struct dos_partition *partp) 1419 { 1420 u_int32_t prev_head_boundary, prev_cyl_boundary; 1421 u_int32_t max_end, size, start; 1422 1423 start = partp->dp_start; 1424 size = partp->dp_size; 1425 max_end = start + size; 1426 /* Only allow a zero size if the partition is being marked unused. */ 1427 if (size == 0) { 1428 if (start == 0 && partp->dp_typ == 0) 1429 return (1); 1430 warnx("ERROR: size of partition is zero"); 1431 return (0); 1432 } 1433 /* Return if no adjustment is necessary. */ 1434 if (start % dos_sectors == 0 && (start + size) % dos_sectors == 0) 1435 return (1); 1436 1437 if (start == 0) { 1438 warnx("WARNING: partition overlaps with partition table"); 1439 if (ok("Correct this automatically?")) 1440 start = dos_sectors; 1441 } 1442 if (start % dos_sectors != 0) 1443 warnx("WARNING: partition does not start on a head boundary"); 1444 if ((start +size) % dos_sectors != 0) 1445 warnx("WARNING: partition does not end on a cylinder boundary"); 1446 warnx("WARNING: this may confuse the BIOS or some operating systems"); 1447 if (!ok("Correct this automatically?")) 1448 return (1); 1449 1450 /* 1451 * Adjust start upwards, if necessary, to fall on a head boundary. 1452 */ 1453 if (start % dos_sectors != 0) { 1454 prev_head_boundary = start / dos_sectors * dos_sectors; 1455 if (max_end < dos_sectors || 1456 prev_head_boundary >= max_end - dos_sectors) { 1457 /* 1458 * Can't go past end of partition 1459 */ 1460 warnx( 1461 "ERROR: unable to adjust start of partition to fall on a head boundary"); 1462 return (0); 1463 } 1464 start = prev_head_boundary + dos_sectors; 1465 } 1466 1467 /* 1468 * Adjust size downwards, if necessary, to fall on a cylinder 1469 * boundary. 1470 */ 1471 prev_cyl_boundary = ((start + size) / dos_cylsecs) * dos_cylsecs; 1472 if (prev_cyl_boundary > start) 1473 size = prev_cyl_boundary - start; 1474 else { 1475 warnx("ERROR: could not adjust partition to start on a head boundary\n\ 1476 and end on a cylinder boundary."); 1477 return (0); 1478 } 1479 1480 /* Finally, commit any changes to partp and return. */ 1481 if (start != partp->dp_start) { 1482 warnx("WARNING: adjusting start offset of partition to %u", 1483 (u_int)start); 1484 partp->dp_start = start; 1485 } 1486 if (size != partp->dp_size) { 1487 warnx("WARNING: adjusting size of partition to %u", (u_int)size); 1488 partp->dp_size = size; 1489 } 1490 1491 return (1); 1492 } 1493 1494 /* 1495 * Try figuring out the root device's canonical disk name. 1496 * The following choices are considered: 1497 * /dev/ad0s1a => /dev/ad0 1498 * /dev/da0a => /dev/da0 1499 * /dev/vinum/root => /dev/vinum/root 1500 * A ".eli" part is removed if it exists (see geli(8)). 1501 * A ".journal" ending is removed if it exists (see gjournal(8)). 1502 */ 1503 static char * 1504 get_rootdisk(void) 1505 { 1506 struct statfs rootfs; 1507 regex_t re; 1508 #define NMATCHES 2 1509 regmatch_t rm[NMATCHES]; 1510 char dev[PATH_MAX], *s; 1511 int rv; 1512 1513 if (statfs("/", &rootfs) == -1) 1514 err(1, "statfs(\"/\")"); 1515 1516 if ((rv = regcomp(&re, "^(/dev/[a-z/]+[0-9]*)([sp][0-9]+)?[a-h]?(\\.journal)?$", 1517 REG_EXTENDED)) != 0) 1518 errx(1, "regcomp() failed (%d)", rv); 1519 strlcpy(dev, rootfs.f_mntfromname, sizeof (dev)); 1520 if ((s = strstr(dev, ".eli")) != NULL) 1521 memmove(s, s+4, strlen(s + 4) + 1); 1522 1523 if ((rv = regexec(&re, dev, NMATCHES, rm, 0)) != 0) 1524 errx(1, 1525 "mounted root fs resource doesn't match expectations (regexec returned %d)", 1526 rv); 1527 if ((s = malloc(rm[1].rm_eo - rm[1].rm_so + 1)) == NULL) 1528 errx(1, "out of memory"); 1529 memcpy(s, rootfs.f_mntfromname + rm[1].rm_so, 1530 rm[1].rm_eo - rm[1].rm_so); 1531 s[rm[1].rm_eo - rm[1].rm_so] = 0; 1532 1533 return s; 1534 } 1535