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 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); 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 read_s0(); 384 reset_boot(); 385 partp = &mboot.parts[0]; 386 partp->dp_typ = DOSPTYP_386BSD; 387 partp->dp_flag = ACTIVE; 388 partp->dp_start = dos_sectors; 389 partp->dp_size = rounddown(disksecs, dos_cylsecs) - 390 dos_sectors; 391 dos(partp); 392 if (v_flag) 393 print_s0(); 394 if (!t_flag) 395 write_s0(); 396 exit(0); 397 } 398 if (f_flag) { 399 if (read_s0() || i_flag) 400 reset_boot(); 401 if (!read_config(f_flag)) 402 exit(1); 403 if (v_flag) 404 print_s0(); 405 if (!t_flag) 406 write_s0(); 407 } else { 408 if(u_flag) 409 get_params_to_use(); 410 else 411 print_params(); 412 413 if (read_s0()) 414 init_sector0(dos_sectors); 415 416 printf("Media sector size is %d\n", secsize); 417 printf("Warning: BIOS sector numbering starts with sector 1\n"); 418 printf("Information from DOS bootblock is:\n"); 419 if (partition == -1) 420 for (i = 1; i <= NDOSPART; i++) 421 change_part(i); 422 else 423 change_part(partition); 424 425 if (u_flag || a_flag) 426 change_active(partition); 427 428 if (B_flag) 429 change_code(); 430 431 if (u_flag || a_flag || B_flag) { 432 if (!t_flag) { 433 printf("\nWe haven't changed the partition table yet. "); 434 printf("This is your last chance.\n"); 435 } 436 print_s0(); 437 if (!t_flag) { 438 if (ok("Should we write new partition table?")) 439 write_s0(); 440 } else { 441 printf("\n-t flag specified -- partition table not written.\n"); 442 } 443 } 444 } 445 446 exit(0); 447 } 448 449 static void 450 usage() 451 { 452 fprintf(stderr, "%s%s", 453 "usage: fdisk [-BIaipqstu] [-b bootcode] [-1234] [disk]\n", 454 " fdisk -f configfile [-itv] [disk]\n"); 455 exit(1); 456 } 457 458 static void 459 print_s0(void) 460 { 461 int i; 462 463 print_params(); 464 printf("Information from DOS bootblock is:\n"); 465 for (i = 1; i <= NDOSPART; i++) { 466 printf("%d: ", i); 467 print_part(&mboot.parts[i - 1]); 468 } 469 } 470 471 static struct dos_partition mtpart; 472 473 static void 474 print_part(const struct dos_partition *partp) 475 { 476 u_int64_t part_mb; 477 478 if (!bcmp(partp, &mtpart, sizeof (struct dos_partition))) { 479 printf("<UNUSED>\n"); 480 return; 481 } 482 /* 483 * Be careful not to overflow. 484 */ 485 part_mb = partp->dp_size; 486 part_mb *= secsize; 487 part_mb /= (1024 * 1024); 488 printf("sysid %d (%#04x),(%s)\n", partp->dp_typ, partp->dp_typ, 489 get_type(partp->dp_typ)); 490 printf(" start %lu, size %lu (%ju Meg), flag %x%s\n", 491 (u_long)partp->dp_start, 492 (u_long)partp->dp_size, 493 (uintmax_t)part_mb, 494 partp->dp_flag, 495 partp->dp_flag == ACTIVE ? " (active)" : ""); 496 printf("\tbeg: cyl %d/ head %d/ sector %d;\n\tend: cyl %d/ head %d/ sector %d\n" 497 ,DPCYL(partp->dp_scyl, partp->dp_ssect) 498 ,partp->dp_shd 499 ,DPSECT(partp->dp_ssect) 500 ,DPCYL(partp->dp_ecyl, partp->dp_esect) 501 ,partp->dp_ehd 502 ,DPSECT(partp->dp_esect)); 503 } 504 505 506 static void 507 init_boot(void) 508 { 509 const char *fname; 510 int fdesc, n; 511 struct stat sb; 512 513 fname = b_flag ? b_flag : "/boot/mbr"; 514 if ((fdesc = open(fname, O_RDONLY)) == -1 || 515 fstat(fdesc, &sb) == -1) 516 err(1, "%s", fname); 517 if (sb.st_size == 0) 518 errx(1, "%s is empty, must not be.", fname); 519 if ((mboot.bootinst_size = sb.st_size) % secsize != 0) 520 errx(1, "%s: length must be a multiple of sector size", fname); 521 if (mboot.bootinst != NULL) 522 free(mboot.bootinst); 523 if ((mboot.bootinst = malloc(mboot.bootinst_size = sb.st_size)) == NULL) 524 errx(1, "%s: unable to allocate read buffer", fname); 525 if ((n = read(fdesc, mboot.bootinst, mboot.bootinst_size)) == -1 || 526 close(fdesc)) 527 err(1, "%s", fname); 528 if (n != mboot.bootinst_size) 529 errx(1, "%s: short read", fname); 530 } 531 532 533 static void 534 init_sector0(unsigned long start) 535 { 536 struct dos_partition *partp = &mboot.parts[0]; 537 538 init_boot(); 539 540 partp->dp_typ = DOSPTYP_386BSD; 541 partp->dp_flag = ACTIVE; 542 start = roundup(start, dos_sectors); 543 if(start == 0) 544 start = dos_sectors; 545 partp->dp_start = start; 546 partp->dp_size = rounddown(disksecs, dos_cylsecs) - start; 547 548 dos(partp); 549 } 550 551 static void 552 change_part(int i) 553 { 554 struct dos_partition *partp = &mboot.parts[i - 1]; 555 556 printf("The data for partition %d is:\n", i); 557 print_part(partp); 558 559 if (u_flag && ok("Do you want to change it?")) { 560 int tmp; 561 562 if (i_flag) { 563 bzero(partp, sizeof (*partp)); 564 if (i == 1) { 565 init_sector0(1); 566 printf("\nThe static data for the slice 1 has been reinitialized to:\n"); 567 print_part(partp); 568 } 569 } 570 571 do { 572 Decimal("sysid (165=FreeBSD)", partp->dp_typ, tmp, 255); 573 Decimal("start", partp->dp_start, tmp, NO_DISK_SECTORS); 574 Decimal("size", partp->dp_size, tmp, NO_DISK_SECTORS); 575 if (!sanitize_partition(partp)) { 576 warnx("ERROR: failed to adjust; setting sysid to 0"); 577 partp->dp_typ = 0; 578 } 579 580 if (ok("Explicitly specify beg/end address ?")) 581 { 582 int tsec,tcyl,thd; 583 tcyl = DPCYL(partp->dp_scyl,partp->dp_ssect); 584 thd = partp->dp_shd; 585 tsec = DPSECT(partp->dp_ssect); 586 Decimal("beginning cylinder", tcyl, tmp, NO_TRACK_CYLINDERS); 587 Decimal("beginning head", thd, tmp, NO_TRACK_HEADS); 588 Decimal("beginning sector", tsec, tmp, NO_TRACK_SECTORS); 589 partp->dp_scyl = DOSCYL(tcyl); 590 partp->dp_ssect = DOSSECT(tsec,tcyl); 591 partp->dp_shd = thd; 592 593 tcyl = DPCYL(partp->dp_ecyl,partp->dp_esect); 594 thd = partp->dp_ehd; 595 tsec = DPSECT(partp->dp_esect); 596 Decimal("ending cylinder", tcyl, tmp, NO_TRACK_CYLINDERS); 597 Decimal("ending head", thd, tmp, NO_TRACK_HEADS); 598 Decimal("ending sector", tsec, tmp, NO_TRACK_SECTORS); 599 partp->dp_ecyl = DOSCYL(tcyl); 600 partp->dp_esect = DOSSECT(tsec,tcyl); 601 partp->dp_ehd = thd; 602 } else 603 dos(partp); 604 605 print_part(partp); 606 } while (!ok("Are we happy with this entry?")); 607 } 608 } 609 610 static void 611 print_params() 612 { 613 printf("parameters extracted from in-core disklabel are:\n"); 614 printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n" 615 ,cyls,heads,sectors,cylsecs); 616 if (dos_cyls > 1023 || dos_heads > 255 || dos_sectors > 63) 617 printf("Figures below won't work with BIOS for partitions not in cyl 1\n"); 618 printf("parameters to be used for BIOS calculations are:\n"); 619 printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n" 620 ,dos_cyls,dos_heads,dos_sectors,dos_cylsecs); 621 } 622 623 static void 624 change_active(int which) 625 { 626 struct dos_partition *partp = &mboot.parts[0]; 627 int active, i, new, tmp; 628 629 active = -1; 630 for (i = 0; i < NDOSPART; i++) { 631 if ((partp[i].dp_flag & ACTIVE) == 0) 632 continue; 633 printf("Partition %d is marked active\n", i + 1); 634 if (active == -1) 635 active = i + 1; 636 } 637 if (a_flag && which != -1) 638 active = which; 639 else if (active == -1) 640 active = 1; 641 642 if (!ok("Do you want to change the active partition?")) 643 return; 644 setactive: 645 do { 646 new = active; 647 Decimal("active partition", new, tmp, 0); 648 if (new < 1 || new > 4) { 649 printf("Active partition number must be in range 1-4." 650 " Try again.\n"); 651 goto setactive; 652 } 653 active = new; 654 } while (!ok("Are you happy with this choice")); 655 for (i = 0; i < NDOSPART; i++) 656 partp[i].dp_flag = 0; 657 if (active > 0 && active <= NDOSPART) 658 partp[active-1].dp_flag = ACTIVE; 659 } 660 661 static void 662 change_code() 663 { 664 if (ok("Do you want to change the boot code?")) 665 init_boot(); 666 } 667 668 void 669 get_params_to_use() 670 { 671 int tmp; 672 print_params(); 673 if (ok("Do you want to change our idea of what BIOS thinks ?")) 674 { 675 do 676 { 677 Decimal("BIOS's idea of #cylinders", dos_cyls, tmp, 0); 678 Decimal("BIOS's idea of #heads", dos_heads, tmp, 0); 679 Decimal("BIOS's idea of #sectors", dos_sectors, tmp, 0); 680 dos_cylsecs = dos_heads * dos_sectors; 681 print_params(); 682 } 683 while(!ok("Are you happy with this choice")); 684 } 685 } 686 687 688 /***********************************************\ 689 * Change real numbers into strange dos numbers * 690 \***********************************************/ 691 static void 692 dos(struct dos_partition *partp) 693 { 694 int cy, sec; 695 u_int32_t end; 696 697 if (partp->dp_typ == 0 && partp->dp_start == 0 && partp->dp_size == 0) { 698 memcpy(partp, &mtpart, sizeof(*partp)); 699 return; 700 } 701 702 /* Start c/h/s. */ 703 partp->dp_shd = partp->dp_start % dos_cylsecs / dos_sectors; 704 cy = partp->dp_start / dos_cylsecs; 705 sec = partp->dp_start % dos_sectors + 1; 706 partp->dp_scyl = DOSCYL(cy); 707 partp->dp_ssect = DOSSECT(sec, cy); 708 709 /* End c/h/s. */ 710 end = partp->dp_start + partp->dp_size - 1; 711 partp->dp_ehd = end % dos_cylsecs / dos_sectors; 712 cy = end / dos_cylsecs; 713 sec = end % dos_sectors + 1; 714 partp->dp_ecyl = DOSCYL(cy); 715 partp->dp_esect = DOSSECT(sec, cy); 716 } 717 718 static int 719 open_disk(int flag) 720 { 721 int rwmode; 722 723 /* Write mode if one of these flags are set. */ 724 rwmode = (a_flag || I_flag || B_flag || flag); 725 fd = g_open(disk, rwmode); 726 /* If the mode fails, try read-only if we didn't. */ 727 if (fd == -1 && errno == EPERM && rwmode) 728 fd = g_open(disk, 0); 729 if (fd == -1 && errno == ENXIO) 730 return -2; 731 if (fd == -1) { 732 warnx("can't open device %s", disk); 733 return -1; 734 } 735 if (get_params() == -1) { 736 warnx("can't get disk parameters on %s", disk); 737 return -1; 738 } 739 return fd; 740 } 741 742 static ssize_t 743 read_disk(off_t sector, void *buf) 744 { 745 746 lseek(fd, (sector * 512), 0); 747 if (secsize == 0) 748 for (secsize = MIN_SEC_SIZE; secsize <= MAX_SEC_SIZE; 749 secsize *= 2) { 750 /* try the read */ 751 int size = read(fd, buf, secsize); 752 if (size == secsize) 753 /* it worked so return */ 754 return secsize; 755 } 756 else 757 return read(fd, buf, secsize); 758 759 /* we failed to read at any of the sizes */ 760 return -1; 761 } 762 763 static int 764 geom_class_available(const char *name) 765 { 766 struct gclass *class; 767 struct gmesh mesh; 768 int error; 769 770 error = geom_gettree(&mesh); 771 if (error != 0) 772 errc(1, error, "Cannot get GEOM tree"); 773 774 LIST_FOREACH(class, &mesh.lg_class, lg_class) { 775 if (strcmp(class->lg_name, name) == 0) { 776 geom_deletetree(&mesh); 777 return (1); 778 } 779 } 780 781 geom_deletetree(&mesh); 782 783 return (0); 784 } 785 786 static int 787 write_disk(off_t sector, void *buf) 788 { 789 struct gctl_req *grq; 790 const char *errmsg; 791 char *pname; 792 int error; 793 794 /* Check that GEOM_MBR is available */ 795 if (geom_class_available("MBR") != 0) { 796 grq = gctl_get_handle(); 797 gctl_ro_param(grq, "verb", -1, "write MBR"); 798 gctl_ro_param(grq, "class", -1, "MBR"); 799 pname = g_providername(fd); 800 if (pname == NULL) { 801 warn("Error getting providername for %s", disk); 802 return (-1); 803 } 804 gctl_ro_param(grq, "geom", -1, pname); 805 gctl_ro_param(grq, "data", secsize, buf); 806 errmsg = gctl_issue(grq); 807 free(pname); 808 if (errmsg == NULL) { 809 gctl_free(grq); 810 return(0); 811 } 812 if (!q_flag) 813 warnx("GEOM_MBR: %s", errmsg); 814 gctl_free(grq); 815 } else { 816 /* 817 * Try to write MBR directly. This may help when disk 818 * is not in use. 819 * XXX: hardcoded sectorsize 820 */ 821 error = pwrite(fd, buf, secsize, (sector * 512)); 822 if (error == secsize) 823 return (0); 824 } 825 826 /* 827 * GEOM_MBR is not available or failed to write MBR. 828 * Now check that we have GEOM_PART and recommend to use gpart (8). 829 */ 830 if (geom_class_available("PART") != 0) 831 warnx("Failed to write MBR. Try to use gpart(8)."); 832 else 833 warnx("Failed to write sector zero"); 834 return(EINVAL); 835 } 836 837 static int 838 get_params() 839 { 840 int error; 841 u_int u; 842 off_t o; 843 844 error = ioctl(fd, DIOCGFWSECTORS, &u); 845 if (error == 0) 846 sectors = dos_sectors = u; 847 else 848 sectors = dos_sectors = 63; 849 850 error = ioctl(fd, DIOCGFWHEADS, &u); 851 if (error == 0) 852 heads = dos_heads = u; 853 else 854 heads = dos_heads = 255; 855 856 dos_cylsecs = cylsecs = heads * sectors; 857 disksecs = cyls * heads * sectors; 858 859 u = g_sectorsize(fd); 860 if (u <= 0) 861 return (-1); 862 863 o = g_mediasize(fd); 864 if (o < 0) 865 return (-1); 866 if (o / u <= NO_DISK_SECTORS) 867 disksecs = o / u; 868 else 869 disksecs = NO_DISK_SECTORS; 870 cyls = dos_cyls = o / (u * dos_heads * dos_sectors); 871 872 return (0); 873 } 874 875 static int 876 read_s0() 877 { 878 int i; 879 880 mboot.bootinst_size = secsize; 881 if (mboot.bootinst != NULL) 882 free(mboot.bootinst); 883 if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL) { 884 warnx("unable to allocate buffer to read fdisk " 885 "partition table"); 886 return -1; 887 } 888 if (read_disk(0, mboot.bootinst) == -1) { 889 warnx("can't read fdisk partition table"); 890 return -1; 891 } 892 if (le16dec(&mboot.bootinst[DOSMAGICOFFSET]) != DOSMAGIC) { 893 warnx("invalid fdisk partition table found"); 894 /* So should we initialize things */ 895 return -1; 896 } 897 for (i = 0; i < NDOSPART; i++) 898 dos_partition_dec( 899 &mboot.bootinst[DOSPARTOFF + i * DOSPARTSIZE], 900 &mboot.parts[i]); 901 return 0; 902 } 903 904 static int 905 write_s0() 906 { 907 int sector, i; 908 909 if (iotest) { 910 print_s0(); 911 return 0; 912 } 913 for(i = 0; i < NDOSPART; i++) 914 dos_partition_enc(&mboot.bootinst[DOSPARTOFF + i * DOSPARTSIZE], 915 &mboot.parts[i]); 916 le16enc(&mboot.bootinst[DOSMAGICOFFSET], DOSMAGIC); 917 for(sector = 0; sector < mboot.bootinst_size / secsize; sector++) 918 if (write_disk(sector, 919 &mboot.bootinst[sector * secsize]) == -1) { 920 warn("can't write fdisk partition table"); 921 return -1; 922 } 923 return(0); 924 } 925 926 927 static int 928 ok(const char *str) 929 { 930 printf("%s [n] ", str); 931 fflush(stdout); 932 if (fgets(lbuf, LBUF, stdin) == NULL) 933 exit(1); 934 lbuf[strlen(lbuf)-1] = 0; 935 936 if (*lbuf && 937 (!strcmp(lbuf, "yes") || !strcmp(lbuf, "YES") || 938 !strcmp(lbuf, "y") || !strcmp(lbuf, "Y"))) 939 return 1; 940 else 941 return 0; 942 } 943 944 static int 945 decimal(const char *str, int *num, int deflt, uint32_t maxval) 946 { 947 long long acc; 948 int c; 949 char *cp; 950 951 while (1) { 952 acc = 0; 953 printf("Supply a decimal value for \"%s\" [%d] ", str, deflt); 954 fflush(stdout); 955 if (fgets(lbuf, LBUF, stdin) == NULL) 956 exit(1); 957 lbuf[strlen(lbuf)-1] = 0; 958 959 if (!*lbuf) 960 return 0; 961 962 cp = lbuf; 963 while ((c = *cp) && (c == ' ' || c == '\t')) cp++; 964 if (!c) 965 return 0; 966 while ((c = *cp++)) { 967 if (c <= '9' && c >= '0') { 968 if (acc <= maxval || maxval == 0) 969 acc = acc * 10 + c - '0'; 970 } else 971 break; 972 } 973 if (c == ' ' || c == '\t') 974 while ((c = *cp) && (c == ' ' || c == '\t')) cp++; 975 if (!c) { 976 if (maxval > 0 && acc > maxval) { 977 acc = maxval; 978 printf("%s exceeds maximum value allowed for " 979 "this field. The value has been reduced " 980 "to %lld\n", lbuf, acc); 981 } 982 *num = acc; 983 return 1; 984 } else 985 printf("%s is an invalid decimal number. Try again.\n", 986 lbuf); 987 } 988 } 989 990 991 static void 992 parse_config_line(char *line, CMD *command) 993 { 994 char *cp, *end; 995 996 cp = line; 997 while (1) { 998 memset(command, 0, sizeof(*command)); 999 1000 while (isspace(*cp)) ++cp; 1001 if (*cp == '\0' || *cp == '#') 1002 break; 1003 command->cmd = *cp++; 1004 1005 /* 1006 * Parse args 1007 */ 1008 while (1) { 1009 while (isspace(*cp)) ++cp; 1010 if (*cp == '\0') 1011 break; /* eol */ 1012 if (*cp == '#') 1013 break; /* found comment */ 1014 if (isalpha(*cp)) 1015 command->args[command->n_args].argtype = *cp++; 1016 end = NULL; 1017 command->args[command->n_args].arg_val = strtoul(cp, &end, 0); 1018 if (cp == end || (!isspace(*end) && *end != '\0')) { 1019 char ch; 1020 end = cp; 1021 while (!isspace(*end) && *end != '\0') ++end; 1022 ch = *end; *end = '\0'; 1023 command->args[command->n_args].arg_str = strdup(cp); 1024 *end = ch; 1025 } else 1026 command->args[command->n_args].arg_str = NULL; 1027 cp = end; 1028 command->n_args++; 1029 } 1030 break; 1031 } 1032 } 1033 1034 1035 static int 1036 process_geometry(CMD *command) 1037 { 1038 int status = 1, i; 1039 1040 while (1) { 1041 geom_processed = 1; 1042 if (part_processed) { 1043 warnx( 1044 "ERROR line %d: the geometry specification line must occur before\n\ 1045 all partition specifications", 1046 current_line_number); 1047 status = 0; 1048 break; 1049 } 1050 if (command->n_args != 3) { 1051 warnx("ERROR line %d: incorrect number of geometry args", 1052 current_line_number); 1053 status = 0; 1054 break; 1055 } 1056 dos_cyls = 0; 1057 dos_heads = 0; 1058 dos_sectors = 0; 1059 for (i = 0; i < 3; ++i) { 1060 switch (command->args[i].argtype) { 1061 case 'c': 1062 dos_cyls = command->args[i].arg_val; 1063 break; 1064 case 'h': 1065 dos_heads = command->args[i].arg_val; 1066 break; 1067 case 's': 1068 dos_sectors = command->args[i].arg_val; 1069 break; 1070 default: 1071 warnx( 1072 "ERROR line %d: unknown geometry arg type: '%c' (0x%02x)", 1073 current_line_number, command->args[i].argtype, 1074 command->args[i].argtype); 1075 status = 0; 1076 break; 1077 } 1078 } 1079 if (status == 0) 1080 break; 1081 1082 dos_cylsecs = dos_heads * dos_sectors; 1083 1084 /* 1085 * Do sanity checks on parameter values 1086 */ 1087 if (dos_cyls == 0) { 1088 warnx("ERROR line %d: number of cylinders not specified", 1089 current_line_number); 1090 status = 0; 1091 } 1092 if (dos_cyls > 1024) { 1093 warnx( 1094 "WARNING line %d: number of cylinders (%d) may be out-of-range\n\ 1095 (must be within 1-1024 for normal BIOS operation, unless the entire disk\n\ 1096 is dedicated to FreeBSD)", 1097 current_line_number, dos_cyls); 1098 } 1099 1100 if (dos_heads == 0) { 1101 warnx("ERROR line %d: number of heads not specified", 1102 current_line_number); 1103 status = 0; 1104 } else if (dos_heads > 256) { 1105 warnx("ERROR line %d: number of heads must be within (1-256)", 1106 current_line_number); 1107 status = 0; 1108 } 1109 1110 if (dos_sectors == 0) { 1111 warnx("ERROR line %d: number of sectors not specified", 1112 current_line_number); 1113 status = 0; 1114 } else if (dos_sectors > 63) { 1115 warnx("ERROR line %d: number of sectors must be within (1-63)", 1116 current_line_number); 1117 status = 0; 1118 } 1119 1120 break; 1121 } 1122 return (status); 1123 } 1124 1125 static u_int32_t 1126 str2sectors(const char *str) 1127 { 1128 char *end; 1129 unsigned long val; 1130 1131 val = strtoul(str, &end, 0); 1132 if (str == end || *end == '\0') { 1133 warnx("ERROR line %d: unexpected size: \'%s\'", 1134 current_line_number, str); 1135 return NO_DISK_SECTORS; 1136 } 1137 1138 if (*end == 'K') 1139 val *= 1024UL / secsize; 1140 else if (*end == 'M') 1141 val *= 1024UL * 1024UL / secsize; 1142 else if (*end == 'G') 1143 val *= 1024UL * 1024UL * 1024UL / secsize; 1144 else { 1145 warnx("ERROR line %d: unexpected modifier: %c " 1146 "(not K/M/G)", current_line_number, *end); 1147 return NO_DISK_SECTORS; 1148 } 1149 1150 return val; 1151 } 1152 1153 static int 1154 process_partition(CMD *command) 1155 { 1156 int status = 0, partition; 1157 u_int32_t prev_head_boundary, prev_cyl_boundary; 1158 u_int32_t adj_size, max_end; 1159 struct dos_partition *partp; 1160 1161 while (1) { 1162 part_processed = 1; 1163 if (command->n_args != 4) { 1164 warnx("ERROR line %d: incorrect number of partition args", 1165 current_line_number); 1166 break; 1167 } 1168 partition = command->args[0].arg_val; 1169 if (partition < 1 || partition > 4) { 1170 warnx("ERROR line %d: invalid partition number %d", 1171 current_line_number, partition); 1172 break; 1173 } 1174 partp = &mboot.parts[partition - 1]; 1175 bzero(partp, sizeof (*partp)); 1176 partp->dp_typ = command->args[1].arg_val; 1177 if (command->args[2].arg_str != NULL) { 1178 if (strcmp(command->args[2].arg_str, "*") == 0) { 1179 int i; 1180 partp->dp_start = dos_sectors; 1181 for (i = 1; i < partition; i++) { 1182 struct dos_partition *prev_partp; 1183 prev_partp = ((struct dos_partition *) 1184 &mboot.parts) + i - 1; 1185 if (prev_partp->dp_typ != 0) 1186 partp->dp_start = prev_partp->dp_start + 1187 prev_partp->dp_size; 1188 } 1189 if (partp->dp_start % dos_sectors != 0) { 1190 prev_head_boundary = rounddown(partp->dp_start, 1191 dos_sectors); 1192 partp->dp_start = prev_head_boundary + 1193 dos_sectors; 1194 } 1195 } else { 1196 partp->dp_start = str2sectors(command->args[2].arg_str); 1197 if (partp->dp_start == NO_DISK_SECTORS) 1198 break; 1199 } 1200 } else 1201 partp->dp_start = command->args[2].arg_val; 1202 1203 if (command->args[3].arg_str != NULL) { 1204 if (strcmp(command->args[3].arg_str, "*") == 0) 1205 partp->dp_size = rounddown(disksecs, dos_cylsecs) - 1206 partp->dp_start; 1207 else { 1208 partp->dp_size = str2sectors(command->args[3].arg_str); 1209 if (partp->dp_size == NO_DISK_SECTORS) 1210 break; 1211 } 1212 prev_cyl_boundary = rounddown(partp->dp_start + partp->dp_size, 1213 dos_cylsecs); 1214 if (prev_cyl_boundary > partp->dp_start) 1215 partp->dp_size = prev_cyl_boundary - partp->dp_start; 1216 } else 1217 partp->dp_size = command->args[3].arg_val; 1218 1219 max_end = partp->dp_start + partp->dp_size; 1220 1221 if (partp->dp_typ == 0) { 1222 /* 1223 * Get out, the partition is marked as unused. 1224 */ 1225 /* 1226 * Insure that it's unused. 1227 */ 1228 bzero(partp, sizeof(*partp)); 1229 status = 1; 1230 break; 1231 } 1232 1233 /* 1234 * Adjust start upwards, if necessary, to fall on a head boundary. 1235 */ 1236 if (partp->dp_start % dos_sectors != 0) { 1237 prev_head_boundary = rounddown(partp->dp_start, dos_sectors); 1238 if (max_end < dos_sectors || 1239 prev_head_boundary > max_end - dos_sectors) { 1240 /* 1241 * Can't go past end of partition 1242 */ 1243 warnx( 1244 "ERROR line %d: unable to adjust start of partition %d to fall on\n\ 1245 a head boundary", 1246 current_line_number, partition); 1247 break; 1248 } 1249 warnx( 1250 "WARNING: adjusting start offset of partition %d\n\ 1251 from %u to %u, to fall on a head boundary", 1252 partition, (u_int)partp->dp_start, 1253 (u_int)(prev_head_boundary + dos_sectors)); 1254 partp->dp_start = prev_head_boundary + dos_sectors; 1255 } 1256 1257 /* 1258 * Adjust size downwards, if necessary, to fall on a cylinder 1259 * boundary. 1260 */ 1261 prev_cyl_boundary = rounddown(partp->dp_start + partp->dp_size, 1262 dos_cylsecs); 1263 if (prev_cyl_boundary > partp->dp_start) 1264 adj_size = prev_cyl_boundary - partp->dp_start; 1265 else { 1266 warnx( 1267 "ERROR: could not adjust partition to start on a head boundary\n\ 1268 and end on a cylinder boundary."); 1269 return (0); 1270 } 1271 if (adj_size != partp->dp_size) { 1272 warnx( 1273 "WARNING: adjusting size of partition %d from %u to %u\n\ 1274 to end on a cylinder boundary", 1275 partition, (u_int)partp->dp_size, (u_int)adj_size); 1276 partp->dp_size = adj_size; 1277 } 1278 if (partp->dp_size == 0) { 1279 warnx("ERROR line %d: size of partition %d is zero", 1280 current_line_number, partition); 1281 break; 1282 } 1283 1284 dos(partp); 1285 status = 1; 1286 break; 1287 } 1288 return (status); 1289 } 1290 1291 1292 static int 1293 process_active(CMD *command) 1294 { 1295 int status = 0, partition, i; 1296 struct dos_partition *partp; 1297 1298 while (1) { 1299 active_processed = 1; 1300 if (command->n_args != 1) { 1301 warnx("ERROR line %d: incorrect number of active args", 1302 current_line_number); 1303 status = 0; 1304 break; 1305 } 1306 partition = command->args[0].arg_val; 1307 if (partition < 1 || partition > 4) { 1308 warnx("ERROR line %d: invalid partition number %d", 1309 current_line_number, partition); 1310 break; 1311 } 1312 /* 1313 * Reset active partition 1314 */ 1315 partp = mboot.parts; 1316 for (i = 0; i < NDOSPART; i++) 1317 partp[i].dp_flag = 0; 1318 partp[partition-1].dp_flag = ACTIVE; 1319 1320 status = 1; 1321 break; 1322 } 1323 return (status); 1324 } 1325 1326 1327 static int 1328 process_line(char *line) 1329 { 1330 CMD command; 1331 int status = 1; 1332 1333 while (1) { 1334 parse_config_line(line, &command); 1335 switch (command.cmd) { 1336 case 0: 1337 /* 1338 * Comment or blank line 1339 */ 1340 break; 1341 case 'g': 1342 /* 1343 * Set geometry 1344 */ 1345 status = process_geometry(&command); 1346 break; 1347 case 'p': 1348 status = process_partition(&command); 1349 break; 1350 case 'a': 1351 status = process_active(&command); 1352 break; 1353 default: 1354 status = 0; 1355 break; 1356 } 1357 break; 1358 } 1359 return (status); 1360 } 1361 1362 1363 static int 1364 read_config(char *config_file) 1365 { 1366 FILE *fp = NULL; 1367 int status = 1; 1368 char buf[1010]; 1369 1370 while (1) { 1371 if (strcmp(config_file, "-") != 0) { 1372 /* 1373 * We're not reading from stdin 1374 */ 1375 if ((fp = fopen(config_file, "r")) == NULL) { 1376 status = 0; 1377 break; 1378 } 1379 } else { 1380 fp = stdin; 1381 } 1382 current_line_number = 0; 1383 while (!feof(fp)) { 1384 if (fgets(buf, sizeof(buf), fp) == NULL) 1385 break; 1386 ++current_line_number; 1387 status = process_line(buf); 1388 if (status == 0) 1389 break; 1390 } 1391 break; 1392 } 1393 if (fp) { 1394 /* 1395 * It doesn't matter if we're reading from stdin, as we've reached EOF 1396 */ 1397 fclose(fp); 1398 } 1399 return (status); 1400 } 1401 1402 1403 static void 1404 reset_boot(void) 1405 { 1406 int i; 1407 struct dos_partition *partp; 1408 1409 init_boot(); 1410 for (i = 0; i < 4; ++i) { 1411 partp = &mboot.parts[i]; 1412 bzero(partp, sizeof(*partp)); 1413 } 1414 } 1415 1416 static int 1417 sanitize_partition(struct dos_partition *partp) 1418 { 1419 u_int32_t prev_head_boundary, prev_cyl_boundary; 1420 u_int32_t max_end, size, start; 1421 1422 start = partp->dp_start; 1423 size = partp->dp_size; 1424 max_end = start + size; 1425 /* Only allow a zero size if the partition is being marked unused. */ 1426 if (size == 0) { 1427 if (start == 0 && partp->dp_typ == 0) 1428 return (1); 1429 warnx("ERROR: size of partition is zero"); 1430 return (0); 1431 } 1432 /* Return if no adjustment is necessary. */ 1433 if (start % dos_sectors == 0 && (start + size) % dos_sectors == 0) 1434 return (1); 1435 1436 if (start == 0) { 1437 warnx("WARNING: partition overlaps with partition table"); 1438 if (ok("Correct this automatically?")) 1439 start = dos_sectors; 1440 } 1441 if (start % dos_sectors != 0) 1442 warnx("WARNING: partition does not start on a head boundary"); 1443 if ((start +size) % dos_sectors != 0) 1444 warnx("WARNING: partition does not end on a cylinder boundary"); 1445 warnx("WARNING: this may confuse the BIOS or some operating systems"); 1446 if (!ok("Correct this automatically?")) 1447 return (1); 1448 1449 /* 1450 * Adjust start upwards, if necessary, to fall on a head boundary. 1451 */ 1452 if (start % dos_sectors != 0) { 1453 prev_head_boundary = rounddown(start, dos_sectors); 1454 if (max_end < dos_sectors || 1455 prev_head_boundary >= max_end - dos_sectors) { 1456 /* 1457 * Can't go past end of partition 1458 */ 1459 warnx( 1460 "ERROR: unable to adjust start of partition to fall on a head boundary"); 1461 return (0); 1462 } 1463 start = prev_head_boundary + dos_sectors; 1464 } 1465 1466 /* 1467 * Adjust size downwards, if necessary, to fall on a cylinder 1468 * boundary. 1469 */ 1470 prev_cyl_boundary = rounddown(start + size, dos_cylsecs); 1471 if (prev_cyl_boundary > start) 1472 size = prev_cyl_boundary - start; 1473 else { 1474 warnx("ERROR: could not adjust partition to start on a head boundary\n\ 1475 and end on a cylinder boundary."); 1476 return (0); 1477 } 1478 1479 /* Finally, commit any changes to partp and return. */ 1480 if (start != partp->dp_start) { 1481 warnx("WARNING: adjusting start offset of partition to %u", 1482 (u_int)start); 1483 partp->dp_start = start; 1484 } 1485 if (size != partp->dp_size) { 1486 warnx("WARNING: adjusting size of partition to %u", (u_int)size); 1487 partp->dp_size = size; 1488 } 1489 1490 return (1); 1491 } 1492 1493 /* 1494 * Try figuring out the root device's canonical disk name. 1495 * The following choices are considered: 1496 * /dev/ad0s1a => /dev/ad0 1497 * /dev/da0a => /dev/da0 1498 * /dev/vinum/root => /dev/vinum/root 1499 * A ".eli" part is removed if it exists (see geli(8)). 1500 * A ".journal" ending is removed if it exists (see gjournal(8)). 1501 */ 1502 static char * 1503 get_rootdisk(void) 1504 { 1505 struct statfs rootfs; 1506 regex_t re; 1507 #define NMATCHES 2 1508 regmatch_t rm[NMATCHES]; 1509 char dev[PATH_MAX], *s; 1510 int rv; 1511 1512 if (statfs("/", &rootfs) == -1) 1513 err(1, "statfs(\"/\")"); 1514 1515 if ((rv = regcomp(&re, "^(/dev/[a-z/]+[0-9]*)([sp][0-9]+)?[a-h]?(\\.journal)?$", 1516 REG_EXTENDED)) != 0) 1517 errx(1, "regcomp() failed (%d)", rv); 1518 strlcpy(dev, rootfs.f_mntfromname, sizeof (dev)); 1519 if ((s = strstr(dev, ".eli")) != NULL) 1520 memmove(s, s+4, strlen(s + 4) + 1); 1521 1522 if ((rv = regexec(&re, dev, NMATCHES, rm, 0)) != 0) 1523 errx(1, 1524 "mounted root fs resource doesn't match expectations (regexec returned %d)", 1525 rv); 1526 if ((s = malloc(rm[1].rm_eo - rm[1].rm_so + 1)) == NULL) 1527 errx(1, "out of memory"); 1528 memcpy(s, rootfs.f_mntfromname + rm[1].rm_so, 1529 rm[1].rm_eo - rm[1].rm_so); 1530 s[rm[1].rm_eo - rm[1].rm_so] = 0; 1531 1532 return s; 1533 } 1534