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