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