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