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