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