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