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