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