fdisk.c revision 187239
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: head/sbin/fdisk/fdisk.c 187239 2009-01-14 20:53:46Z luigi $"); 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 50int iotest; 51 52#define LBUF 100 53static 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 */ 70static int secsize = 0; /* the sensed sector size */ 71 72static char *disk; 73 74static int cyls, sectors, heads, cylsecs, disksecs; 75 76struct mboot { 77 unsigned char *bootinst; /* boot code */ 78 off_t bootinst_size; 79 struct dos_partition parts[NDOSPART]; 80}; 81 82static struct mboot mboot; 83static int fd; 84 85#define ACTIVE 0x80 86 87static uint dos_cyls; 88static uint dos_heads; 89static uint dos_sectors; 90static 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 97static int current_line_number; 98 99static int geom_processed = 0; 100static int part_processed = 0; 101static int active_processed = 0; 102 103typedef 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 112static int B_flag = 0; /* replace boot code */ 113static int I_flag = 0; /* use entire disk for FreeBSD */ 114static int a_flag = 0; /* set active partition */ 115static char *b_flag = NULL; /* path to boot code */ 116static int i_flag = 0; /* replace partition data */ 117static int q_flag = 0; /* Be quiet */ 118static int u_flag = 0; /* update partition data */ 119static int s_flag = 0; /* Print a summary and exit */ 120static int t_flag = 0; /* test only */ 121static char *f_flag = NULL; /* Read config info from file */ 122static int v_flag = 0; /* Be verbose */ 123static int print_config_flag = 0; 124 125static 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 219static void print_s0(void); 220static void print_part(int i); 221static void init_sector0(unsigned long start); 222static void init_boot(void); 223static void change_part(int i); 224static void print_params(void); 225static void change_active(int which); 226static void change_code(void); 227static void get_params_to_use(void); 228static char *get_rootdisk(void); 229static void dos(struct dos_partition *partp); 230static int open_disk(int flag); 231static ssize_t read_disk(off_t sector, void *buf); 232static int write_disk(off_t sector, void *buf); 233static int get_params(void); 234static int read_s0(void); 235static int write_s0(void); 236static int ok(const char *str); 237static int decimal(const char *str, int *num, int deflt); 238static const char *get_type(int type); 239static int read_config(char *config_file); 240static void reset_boot(void); 241static int sanitize_partition(struct dos_partition *); 242static void usage(void); 243 244int 245main(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(); 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(); 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(); 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 432static void 433usage() 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 441static void 442print_s0(void) 443{ 444 int i; 445 446 print_params(); 447 printf("Information from DOS bootblock is:\n"); 448 for (i = 1; i <= NDOSPART; i++) 449 printf("%d: ", i), print_part(i); 450} 451 452static struct dos_partition mtpart; 453 454static void 455print_part(int i) 456{ 457 struct dos_partition *partp; 458 u_int64_t part_mb; 459 460 partp = &mboot.parts[i - 1]; 461 462 if (!bcmp(partp, &mtpart, sizeof (struct dos_partition))) { 463 printf("<UNUSED>\n"); 464 return; 465 } 466 /* 467 * Be careful not to overflow. 468 */ 469 part_mb = partp->dp_size; 470 part_mb *= secsize; 471 part_mb /= (1024 * 1024); 472 printf("sysid %d (%#04x),(%s)\n", partp->dp_typ, partp->dp_typ, 473 get_type(partp->dp_typ)); 474 printf(" start %lu, size %lu (%ju Meg), flag %x%s\n", 475 (u_long)partp->dp_start, 476 (u_long)partp->dp_size, 477 (uintmax_t)part_mb, 478 partp->dp_flag, 479 partp->dp_flag == ACTIVE ? " (active)" : ""); 480 printf("\tbeg: cyl %d/ head %d/ sector %d;\n\tend: cyl %d/ head %d/ sector %d\n" 481 ,DPCYL(partp->dp_scyl, partp->dp_ssect) 482 ,partp->dp_shd 483 ,DPSECT(partp->dp_ssect) 484 ,DPCYL(partp->dp_ecyl, partp->dp_esect) 485 ,partp->dp_ehd 486 ,DPSECT(partp->dp_esect)); 487} 488 489 490static void 491init_boot(void) 492{ 493#ifndef __ia64__ 494 const char *fname; 495 int fdesc, n; 496 struct stat sb; 497 498 fname = b_flag ? b_flag : "/boot/mbr"; 499 if ((fdesc = open(fname, O_RDONLY)) == -1 || 500 fstat(fdesc, &sb) == -1) 501 err(1, "%s", fname); 502 if ((mboot.bootinst_size = sb.st_size) % secsize != 0) 503 errx(1, "%s: length must be a multiple of sector size", fname); 504 if (mboot.bootinst != NULL) 505 free(mboot.bootinst); 506 if ((mboot.bootinst = malloc(mboot.bootinst_size = sb.st_size)) == NULL) 507 errx(1, "%s: unable to allocate read buffer", fname); 508 if ((n = read(fdesc, mboot.bootinst, mboot.bootinst_size)) == -1 || 509 close(fdesc)) 510 err(1, "%s", fname); 511 if (n != mboot.bootinst_size) 512 errx(1, "%s: short read", fname); 513#else 514 if (mboot.bootinst != NULL) 515 free(mboot.bootinst); 516 mboot.bootinst_size = secsize; 517 if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL) 518 errx(1, "unable to allocate boot block buffer"); 519 memset(mboot.bootinst, 0, mboot.bootinst_size); 520 le16enc(&mboot.bootinst[DOSMAGICOFFSET], DOSMAGIC); 521#endif 522} 523 524 525static void 526init_sector0(unsigned long start) 527{ 528 struct dos_partition *partp = &mboot.parts[0]; 529 530 init_boot(); 531 532 partp->dp_typ = DOSPTYP_386BSD; 533 partp->dp_flag = ACTIVE; 534 start = ((start + dos_sectors - 1) / dos_sectors) * dos_sectors; 535 if(start == 0) 536 start = dos_sectors; 537 partp->dp_start = start; 538 partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs - start; 539 540 dos(partp); 541} 542 543static void 544change_part(int i) 545{ 546 struct dos_partition *partp = &mboot.parts[i - 1]; 547 548 printf("The data for partition %d is:\n", i); 549 print_part(i); 550 551 if (u_flag && ok("Do you want to change it?")) { 552 int tmp; 553 554 if (i_flag) { 555 bzero(partp, sizeof (*partp)); 556 if (i == 1) { 557 init_sector0(1); 558 printf("\nThe static data for the slice 1 has been reinitialized to:\n"); 559 print_part(i); 560 } 561 } 562 563 do { 564 Decimal("sysid (165=FreeBSD)", partp->dp_typ, tmp); 565 Decimal("start", partp->dp_start, tmp); 566 Decimal("size", partp->dp_size, tmp); 567 if (!sanitize_partition(partp)) { 568 warnx("ERROR: failed to adjust; setting sysid to 0"); 569 partp->dp_typ = 0; 570 } 571 572 if (ok("Explicitly specify beg/end address ?")) 573 { 574 int tsec,tcyl,thd; 575 tcyl = DPCYL(partp->dp_scyl,partp->dp_ssect); 576 thd = partp->dp_shd; 577 tsec = DPSECT(partp->dp_ssect); 578 Decimal("beginning cylinder", tcyl, tmp); 579 Decimal("beginning head", thd, tmp); 580 Decimal("beginning sector", tsec, tmp); 581 partp->dp_scyl = DOSCYL(tcyl); 582 partp->dp_ssect = DOSSECT(tsec,tcyl); 583 partp->dp_shd = thd; 584 585 tcyl = DPCYL(partp->dp_ecyl,partp->dp_esect); 586 thd = partp->dp_ehd; 587 tsec = DPSECT(partp->dp_esect); 588 Decimal("ending cylinder", tcyl, tmp); 589 Decimal("ending head", thd, tmp); 590 Decimal("ending sector", tsec, tmp); 591 partp->dp_ecyl = DOSCYL(tcyl); 592 partp->dp_esect = DOSSECT(tsec,tcyl); 593 partp->dp_ehd = thd; 594 } else 595 dos(partp); 596 597 print_part(i); 598 } while (!ok("Are we happy with this entry?")); 599 } 600} 601 602static void 603print_params() 604{ 605 printf("parameters extracted from in-core disklabel are:\n"); 606 printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n" 607 ,cyls,heads,sectors,cylsecs); 608 if (dos_cyls > 1023 || dos_heads > 255 || dos_sectors > 63) 609 printf("Figures below won't work with BIOS for partitions not in cyl 1\n"); 610 printf("parameters to be used for BIOS calculations are:\n"); 611 printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n" 612 ,dos_cyls,dos_heads,dos_sectors,dos_cylsecs); 613} 614 615static void 616change_active(int which) 617{ 618 struct dos_partition *partp = &mboot.parts[0]; 619 int active, i, new, tmp; 620 621 active = -1; 622 for (i = 0; i < NDOSPART; i++) { 623 if ((partp[i].dp_flag & ACTIVE) == 0) 624 continue; 625 printf("Partition %d is marked active\n", i + 1); 626 if (active == -1) 627 active = i + 1; 628 } 629 if (a_flag && which != -1) 630 active = which; 631 else if (active == -1) 632 active = 1; 633 634 if (!ok("Do you want to change the active partition?")) 635 return; 636setactive: 637 do { 638 new = active; 639 Decimal("active partition", new, tmp); 640 if (new < 1 || new > 4) { 641 printf("Active partition number must be in range 1-4." 642 " Try again.\n"); 643 goto setactive; 644 } 645 active = new; 646 } while (!ok("Are you happy with this choice")); 647 for (i = 0; i < NDOSPART; i++) 648 partp[i].dp_flag = 0; 649 if (active > 0 && active <= NDOSPART) 650 partp[active-1].dp_flag = ACTIVE; 651} 652 653static void 654change_code() 655{ 656 if (ok("Do you want to change the boot code?")) 657 init_boot(); 658} 659 660void 661get_params_to_use() 662{ 663 int tmp; 664 print_params(); 665 if (ok("Do you want to change our idea of what BIOS thinks ?")) 666 { 667 do 668 { 669 Decimal("BIOS's idea of #cylinders", dos_cyls, tmp); 670 Decimal("BIOS's idea of #heads", dos_heads, tmp); 671 Decimal("BIOS's idea of #sectors", dos_sectors, tmp); 672 dos_cylsecs = dos_heads * dos_sectors; 673 print_params(); 674 } 675 while(!ok("Are you happy with this choice")); 676 } 677} 678 679 680/***********************************************\ 681* Change real numbers into strange dos numbers * 682\***********************************************/ 683static void 684dos(struct dos_partition *partp) 685{ 686 int cy, sec; 687 u_int32_t end; 688 689 if (partp->dp_typ == 0 && partp->dp_start == 0 && partp->dp_size == 0) { 690 memcpy(partp, &mtpart, sizeof(*partp)); 691 return; 692 } 693 694 /* Start c/h/s. */ 695 partp->dp_shd = partp->dp_start % dos_cylsecs / dos_sectors; 696 cy = partp->dp_start / dos_cylsecs; 697 sec = partp->dp_start % dos_sectors + 1; 698 partp->dp_scyl = DOSCYL(cy); 699 partp->dp_ssect = DOSSECT(sec, cy); 700 701 /* End c/h/s. */ 702 end = partp->dp_start + partp->dp_size - 1; 703 partp->dp_ehd = end % dos_cylsecs / dos_sectors; 704 cy = end / dos_cylsecs; 705 sec = end % dos_sectors + 1; 706 partp->dp_ecyl = DOSCYL(cy); 707 partp->dp_esect = DOSSECT(sec, cy); 708} 709 710static int 711open_disk(int flag) 712{ 713 int rwmode; 714 715 /* Write mode if one of these flags are set. */ 716 rwmode = (a_flag || I_flag || B_flag || flag); 717 fd = g_open(disk, rwmode); 718 /* If the mode fails, try read-only if we didn't. */ 719 if (fd == -1 && errno == EPERM && rwmode) 720 fd = g_open(disk, 0); 721 if (fd == -1 && errno == ENXIO) 722 return -2; 723 if (fd == -1) { 724 warnx("can't open device %s", disk); 725 return -1; 726 } 727 if (get_params() == -1) { 728 warnx("can't get disk parameters on %s", disk); 729 return -1; 730 } 731 return fd; 732} 733 734static ssize_t 735read_disk(off_t sector, void *buf) 736{ 737 738 lseek(fd, (sector * 512), 0); 739 if (secsize == 0) 740 for (secsize = MIN_SEC_SIZE; secsize <= MAX_SEC_SIZE; 741 secsize *= 2) { 742 /* try the read */ 743 int size = read(fd, buf, secsize); 744 if (size == secsize) 745 /* it worked so return */ 746 return secsize; 747 } 748 else 749 return read(fd, buf, secsize); 750 751 /* we failed to read at any of the sizes */ 752 return -1; 753} 754 755static int 756write_disk(off_t sector, void *buf) 757{ 758 int error; 759 struct gctl_req *grq; 760 const char *errmsg; 761 char fbuf[BUFSIZ], *pname; 762 int i, fdw; 763 764 grq = gctl_get_handle(); 765 gctl_ro_param(grq, "verb", -1, "write MBR"); 766 gctl_ro_param(grq, "class", -1, "MBR"); 767 pname = g_providername(fd); 768 if (pname == NULL) { 769 warn("Error getting providername for %s", disk); 770 return (-1); 771 } 772 gctl_ro_param(grq, "geom", -1, pname); 773 gctl_ro_param(grq, "data", secsize, buf); 774 errmsg = gctl_issue(grq); 775 free(pname); 776 if (errmsg == NULL) { 777 gctl_free(grq); 778 return(0); 779 } 780 if (!q_flag) /* GEOM errors are benign, not all devices supported */ 781 warnx("%s", errmsg); 782 gctl_free(grq); 783 784 error = pwrite(fd, buf, secsize, (sector * 512)); 785 if (error == secsize) 786 return (0); 787 788 for (i = 1; i < 5; i++) { 789 sprintf(fbuf, "%ss%d", disk, i); 790 fdw = open(fbuf, O_RDWR, 0); 791 if (fdw < 0) 792 continue; 793 error = ioctl(fdw, DIOCSMBR, buf); 794 close(fdw); 795 if (error == 0) 796 return (0); 797 } 798 warnx("Failed to write sector zero"); 799 return(EINVAL); 800} 801 802static int 803get_params() 804{ 805 int error; 806 u_int u; 807 off_t o; 808 809 error = ioctl(fd, DIOCGFWSECTORS, &u); 810 if (error == 0) 811 sectors = dos_sectors = u; 812 else 813 sectors = dos_sectors = 63; 814 815 error = ioctl(fd, DIOCGFWHEADS, &u); 816 if (error == 0) 817 heads = dos_heads = u; 818 else 819 heads = dos_heads = 255; 820 821 dos_cylsecs = cylsecs = heads * sectors; 822 disksecs = cyls * heads * sectors; 823 824 u = g_sectorsize(fd); 825 if (u <= 0) 826 return (-1); 827 828 o = g_mediasize(fd); 829 if (o < 0) 830 return (-1); 831 disksecs = o / u; 832 cyls = dos_cyls = o / (u * dos_heads * dos_sectors); 833 834 return (disksecs); 835} 836 837static int 838read_s0() 839{ 840 int i; 841 842 mboot.bootinst_size = secsize; 843 if (mboot.bootinst != NULL) 844 free(mboot.bootinst); 845 if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL) { 846 warnx("unable to allocate buffer to read fdisk " 847 "partition table"); 848 return -1; 849 } 850 if (read_disk(0, mboot.bootinst) == -1) { 851 warnx("can't read fdisk partition table"); 852 return -1; 853 } 854 if (le16dec(&mboot.bootinst[DOSMAGICOFFSET]) != DOSMAGIC) { 855 warnx("invalid fdisk partition table found"); 856 /* So should we initialize things */ 857 return -1; 858 } 859 for (i = 0; i < NDOSPART; i++) 860 dos_partition_dec( 861 &mboot.bootinst[DOSPARTOFF + i * DOSPARTSIZE], 862 &mboot.parts[i]); 863 return 0; 864} 865 866static int 867write_s0() 868{ 869 int sector, i; 870 871 if (iotest) { 872 print_s0(); 873 return 0; 874 } 875 for(i = 0; i < NDOSPART; i++) 876 dos_partition_enc(&mboot.bootinst[DOSPARTOFF + i * DOSPARTSIZE], 877 &mboot.parts[i]); 878 le16enc(&mboot.bootinst[DOSMAGICOFFSET], DOSMAGIC); 879 for(sector = 0; sector < mboot.bootinst_size / secsize; sector++) 880 if (write_disk(sector, 881 &mboot.bootinst[sector * secsize]) == -1) { 882 warn("can't write fdisk partition table"); 883 return -1; 884 } 885 return(0); 886} 887 888 889static int 890ok(const char *str) 891{ 892 printf("%s [n] ", str); 893 fflush(stdout); 894 if (fgets(lbuf, LBUF, stdin) == NULL) 895 exit(1); 896 lbuf[strlen(lbuf)-1] = 0; 897 898 if (*lbuf && 899 (!strcmp(lbuf, "yes") || !strcmp(lbuf, "YES") || 900 !strcmp(lbuf, "y") || !strcmp(lbuf, "Y"))) 901 return 1; 902 else 903 return 0; 904} 905 906static int 907decimal(const char *str, int *num, int deflt) 908{ 909 int acc = 0, c; 910 char *cp; 911 912 while (1) { 913 printf("Supply a decimal value for \"%s\" [%d] ", str, deflt); 914 fflush(stdout); 915 if (fgets(lbuf, LBUF, stdin) == NULL) 916 exit(1); 917 lbuf[strlen(lbuf)-1] = 0; 918 919 if (!*lbuf) 920 return 0; 921 922 cp = lbuf; 923 while ((c = *cp) && (c == ' ' || c == '\t')) cp++; 924 if (!c) 925 return 0; 926 while ((c = *cp++)) { 927 if (c <= '9' && c >= '0') 928 acc = acc * 10 + c - '0'; 929 else 930 break; 931 } 932 if (c == ' ' || c == '\t') 933 while ((c = *cp) && (c == ' ' || c == '\t')) cp++; 934 if (!c) { 935 *num = acc; 936 return 1; 937 } else 938 printf("%s is an invalid decimal number. Try again.\n", 939 lbuf); 940 } 941 942} 943 944static const char * 945get_type(int type) 946{ 947 int numentries = (sizeof(part_types)/sizeof(struct part_type)); 948 int counter = 0; 949 struct part_type *ptr = part_types; 950 951 952 while(counter < numentries) { 953 if(ptr->type == type) 954 return(ptr->name); 955 ptr++; 956 counter++; 957 } 958 return("unknown"); 959} 960 961 962static void 963parse_config_line(char *line, CMD *command) 964{ 965 char *cp, *end; 966 967 cp = line; 968 while (1) { 969 memset(command, 0, sizeof(*command)); 970 971 while (isspace(*cp)) ++cp; 972 if (*cp == '\0' || *cp == '#') 973 break; 974 command->cmd = *cp++; 975 976 /* 977 * Parse args 978 */ 979 while (1) { 980 while (isspace(*cp)) ++cp; 981 if (*cp == '#') 982 break; /* found comment */ 983 if (isalpha(*cp)) 984 command->args[command->n_args].argtype = *cp++; 985 if (!isdigit(*cp)) 986 break; /* assume end of line */ 987 end = NULL; 988 command->args[command->n_args].arg_val = strtol(cp, &end, 0); 989 if (cp == end) 990 break; /* couldn't parse number */ 991 cp = end; 992 command->n_args++; 993 } 994 break; 995 } 996} 997 998 999static int 1000process_geometry(CMD *command) 1001{ 1002 int status = 1, i; 1003 1004 while (1) { 1005 geom_processed = 1; 1006 if (part_processed) { 1007 warnx( 1008 "ERROR line %d: the geometry specification line must occur before\n\ 1009 all partition specifications", 1010 current_line_number); 1011 status = 0; 1012 break; 1013 } 1014 if (command->n_args != 3) { 1015 warnx("ERROR line %d: incorrect number of geometry args", 1016 current_line_number); 1017 status = 0; 1018 break; 1019 } 1020 dos_cyls = 0; 1021 dos_heads = 0; 1022 dos_sectors = 0; 1023 for (i = 0; i < 3; ++i) { 1024 switch (command->args[i].argtype) { 1025 case 'c': 1026 dos_cyls = command->args[i].arg_val; 1027 break; 1028 case 'h': 1029 dos_heads = command->args[i].arg_val; 1030 break; 1031 case 's': 1032 dos_sectors = command->args[i].arg_val; 1033 break; 1034 default: 1035 warnx( 1036 "ERROR line %d: unknown geometry arg type: '%c' (0x%02x)", 1037 current_line_number, command->args[i].argtype, 1038 command->args[i].argtype); 1039 status = 0; 1040 break; 1041 } 1042 } 1043 if (status == 0) 1044 break; 1045 1046 dos_cylsecs = dos_heads * dos_sectors; 1047 1048 /* 1049 * Do sanity checks on parameter values 1050 */ 1051 if (dos_cyls == 0) { 1052 warnx("ERROR line %d: number of cylinders not specified", 1053 current_line_number); 1054 status = 0; 1055 } 1056 if (dos_cyls > 1024) { 1057 warnx( 1058 "WARNING line %d: number of cylinders (%d) may be out-of-range\n\ 1059 (must be within 1-1024 for normal BIOS operation, unless the entire disk\n\ 1060 is dedicated to FreeBSD)", 1061 current_line_number, dos_cyls); 1062 } 1063 1064 if (dos_heads == 0) { 1065 warnx("ERROR line %d: number of heads not specified", 1066 current_line_number); 1067 status = 0; 1068 } else if (dos_heads > 256) { 1069 warnx("ERROR line %d: number of heads must be within (1-256)", 1070 current_line_number); 1071 status = 0; 1072 } 1073 1074 if (dos_sectors == 0) { 1075 warnx("ERROR line %d: number of sectors not specified", 1076 current_line_number); 1077 status = 0; 1078 } else if (dos_sectors > 63) { 1079 warnx("ERROR line %d: number of sectors must be within (1-63)", 1080 current_line_number); 1081 status = 0; 1082 } 1083 1084 break; 1085 } 1086 return (status); 1087} 1088 1089 1090static int 1091process_partition(CMD *command) 1092{ 1093 int status = 0, partition; 1094 u_int32_t prev_head_boundary, prev_cyl_boundary; 1095 u_int32_t adj_size, max_end; 1096 struct dos_partition *partp; 1097 1098 while (1) { 1099 part_processed = 1; 1100 if (command->n_args != 4) { 1101 warnx("ERROR line %d: incorrect number of partition args", 1102 current_line_number); 1103 break; 1104 } 1105 partition = command->args[0].arg_val; 1106 if (partition < 1 || partition > 4) { 1107 warnx("ERROR line %d: invalid partition number %d", 1108 current_line_number, partition); 1109 break; 1110 } 1111 partp = &mboot.parts[partition - 1]; 1112 bzero(partp, sizeof (*partp)); 1113 partp->dp_typ = command->args[1].arg_val; 1114 partp->dp_start = command->args[2].arg_val; 1115 partp->dp_size = command->args[3].arg_val; 1116 max_end = partp->dp_start + partp->dp_size; 1117 1118 if (partp->dp_typ == 0) { 1119 /* 1120 * Get out, the partition is marked as unused. 1121 */ 1122 /* 1123 * Insure that it's unused. 1124 */ 1125 bzero(partp, sizeof(*partp)); 1126 status = 1; 1127 break; 1128 } 1129 1130 /* 1131 * Adjust start upwards, if necessary, to fall on a head boundary. 1132 */ 1133 if (partp->dp_start % dos_sectors != 0) { 1134 prev_head_boundary = partp->dp_start / dos_sectors * dos_sectors; 1135 if (max_end < dos_sectors || 1136 prev_head_boundary > max_end - dos_sectors) { 1137 /* 1138 * Can't go past end of partition 1139 */ 1140 warnx( 1141 "ERROR line %d: unable to adjust start of partition %d to fall on\n\ 1142 a head boundary", 1143 current_line_number, partition); 1144 break; 1145 } 1146 warnx( 1147 "WARNING: adjusting start offset of partition %d\n\ 1148 from %u to %u, to fall on a head boundary", 1149 partition, (u_int)partp->dp_start, 1150 (u_int)(prev_head_boundary + dos_sectors)); 1151 partp->dp_start = prev_head_boundary + dos_sectors; 1152 } 1153 1154 /* 1155 * Adjust size downwards, if necessary, to fall on a cylinder 1156 * boundary. 1157 */ 1158 prev_cyl_boundary = 1159 ((partp->dp_start + partp->dp_size) / dos_cylsecs) * dos_cylsecs; 1160 if (prev_cyl_boundary > partp->dp_start) 1161 adj_size = prev_cyl_boundary - partp->dp_start; 1162 else { 1163 warnx( 1164 "ERROR: could not adjust partition to start on a head boundary\n\ 1165 and end on a cylinder boundary."); 1166 return (0); 1167 } 1168 if (adj_size != partp->dp_size) { 1169 warnx( 1170 "WARNING: adjusting size of partition %d from %u to %u\n\ 1171 to end on a cylinder boundary", 1172 partition, (u_int)partp->dp_size, (u_int)adj_size); 1173 partp->dp_size = adj_size; 1174 } 1175 if (partp->dp_size == 0) { 1176 warnx("ERROR line %d: size of partition %d is zero", 1177 current_line_number, partition); 1178 break; 1179 } 1180 1181 dos(partp); 1182 status = 1; 1183 break; 1184 } 1185 return (status); 1186} 1187 1188 1189static int 1190process_active(CMD *command) 1191{ 1192 int status = 0, partition, i; 1193 struct dos_partition *partp; 1194 1195 while (1) { 1196 active_processed = 1; 1197 if (command->n_args != 1) { 1198 warnx("ERROR line %d: incorrect number of active args", 1199 current_line_number); 1200 status = 0; 1201 break; 1202 } 1203 partition = command->args[0].arg_val; 1204 if (partition < 1 || partition > 4) { 1205 warnx("ERROR line %d: invalid partition number %d", 1206 current_line_number, partition); 1207 break; 1208 } 1209 /* 1210 * Reset active partition 1211 */ 1212 partp = mboot.parts; 1213 for (i = 0; i < NDOSPART; i++) 1214 partp[i].dp_flag = 0; 1215 partp[partition-1].dp_flag = ACTIVE; 1216 1217 status = 1; 1218 break; 1219 } 1220 return (status); 1221} 1222 1223 1224static int 1225process_line(char *line) 1226{ 1227 CMD command; 1228 int status = 1; 1229 1230 while (1) { 1231 parse_config_line(line, &command); 1232 switch (command.cmd) { 1233 case 0: 1234 /* 1235 * Comment or blank line 1236 */ 1237 break; 1238 case 'g': 1239 /* 1240 * Set geometry 1241 */ 1242 status = process_geometry(&command); 1243 break; 1244 case 'p': 1245 status = process_partition(&command); 1246 break; 1247 case 'a': 1248 status = process_active(&command); 1249 break; 1250 default: 1251 status = 0; 1252 break; 1253 } 1254 break; 1255 } 1256 return (status); 1257} 1258 1259 1260static int 1261read_config(char *config_file) 1262{ 1263 FILE *fp = NULL; 1264 int status = 1; 1265 char buf[1010]; 1266 1267 while (1) { 1268 if (strcmp(config_file, "-") != 0) { 1269 /* 1270 * We're not reading from stdin 1271 */ 1272 if ((fp = fopen(config_file, "r")) == NULL) { 1273 status = 0; 1274 break; 1275 } 1276 } else { 1277 fp = stdin; 1278 } 1279 current_line_number = 0; 1280 while (!feof(fp)) { 1281 if (fgets(buf, sizeof(buf), fp) == NULL) 1282 break; 1283 ++current_line_number; 1284 status = process_line(buf); 1285 if (status == 0) 1286 break; 1287 } 1288 break; 1289 } 1290 if (fp) { 1291 /* 1292 * It doesn't matter if we're reading from stdin, as we've reached EOF 1293 */ 1294 fclose(fp); 1295 } 1296 return (status); 1297} 1298 1299 1300static void 1301reset_boot(void) 1302{ 1303 int i; 1304 struct dos_partition *partp; 1305 1306 init_boot(); 1307 for (i = 0; i < 4; ++i) { 1308 partp = &mboot.parts[i]; 1309 bzero(partp, sizeof(*partp)); 1310 } 1311} 1312 1313static int 1314sanitize_partition(struct dos_partition *partp) 1315{ 1316 u_int32_t prev_head_boundary, prev_cyl_boundary; 1317 u_int32_t max_end, size, start; 1318 1319 start = partp->dp_start; 1320 size = partp->dp_size; 1321 max_end = start + size; 1322 /* Only allow a zero size if the partition is being marked unused. */ 1323 if (size == 0) { 1324 if (start == 0 && partp->dp_typ == 0) 1325 return (1); 1326 warnx("ERROR: size of partition is zero"); 1327 return (0); 1328 } 1329 /* Return if no adjustment is necessary. */ 1330 if (start % dos_sectors == 0 && (start + size) % dos_sectors == 0) 1331 return (1); 1332 1333 if (start == 0) { 1334 warnx("WARNING: partition overlaps with partition table"); 1335 if (ok("Correct this automatically?")) 1336 start = dos_sectors; 1337 } 1338 if (start % dos_sectors != 0) 1339 warnx("WARNING: partition does not start on a head boundary"); 1340 if ((start +size) % dos_sectors != 0) 1341 warnx("WARNING: partition does not end on a cylinder boundary"); 1342 warnx("WARNING: this may confuse the BIOS or some operating systems"); 1343 if (!ok("Correct this automatically?")) 1344 return (1); 1345 1346 /* 1347 * Adjust start upwards, if necessary, to fall on a head boundary. 1348 */ 1349 if (start % dos_sectors != 0) { 1350 prev_head_boundary = start / dos_sectors * dos_sectors; 1351 if (max_end < dos_sectors || 1352 prev_head_boundary >= max_end - dos_sectors) { 1353 /* 1354 * Can't go past end of partition 1355 */ 1356 warnx( 1357 "ERROR: unable to adjust start of partition to fall on a head boundary"); 1358 return (0); 1359 } 1360 start = prev_head_boundary + dos_sectors; 1361 } 1362 1363 /* 1364 * Adjust size downwards, if necessary, to fall on a cylinder 1365 * boundary. 1366 */ 1367 prev_cyl_boundary = ((start + size) / dos_cylsecs) * dos_cylsecs; 1368 if (prev_cyl_boundary > start) 1369 size = prev_cyl_boundary - start; 1370 else { 1371 warnx("ERROR: could not adjust partition to start on a head boundary\n\ 1372 and end on a cylinder boundary."); 1373 return (0); 1374 } 1375 1376 /* Finally, commit any changes to partp and return. */ 1377 if (start != partp->dp_start) { 1378 warnx("WARNING: adjusting start offset of partition to %u", 1379 (u_int)start); 1380 partp->dp_start = start; 1381 } 1382 if (size != partp->dp_size) { 1383 warnx("WARNING: adjusting size of partition to %u", (u_int)size); 1384 partp->dp_size = size; 1385 } 1386 1387 return (1); 1388} 1389 1390/* 1391 * Try figuring out the root device's canonical disk name. 1392 * The following choices are considered: 1393 * /dev/ad0s1a => /dev/ad0 1394 * /dev/da0a => /dev/da0 1395 * /dev/vinum/root => /dev/vinum/root 1396 */ 1397static char * 1398get_rootdisk(void) 1399{ 1400 struct statfs rootfs; 1401 regex_t re; 1402#define NMATCHES 2 1403 regmatch_t rm[NMATCHES]; 1404 char *s; 1405 int rv; 1406 1407 if (statfs("/", &rootfs) == -1) 1408 err(1, "statfs(\"/\")"); 1409 1410 if ((rv = regcomp(&re, "^(/dev/[a-z/]+[0-9]+)([sp][0-9]+)?[a-h]?$", 1411 REG_EXTENDED)) != 0) 1412 errx(1, "regcomp() failed (%d)", rv); 1413 if ((rv = regexec(&re, rootfs.f_mntfromname, NMATCHES, rm, 0)) != 0) 1414 errx(1, 1415"mounted root fs resource doesn't match expectations (regexec returned %d)", 1416 rv); 1417 if ((s = malloc(rm[1].rm_eo - rm[1].rm_so + 1)) == NULL) 1418 errx(1, "out of memory"); 1419 memcpy(s, rootfs.f_mntfromname + rm[1].rm_so, 1420 rm[1].rm_eo - rm[1].rm_so); 1421 s[rm[1].rm_eo - rm[1].rm_so] = 0; 1422 1423 return s; 1424} 1425