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