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