fdisk.c revision 65054
1106266Sjulian/* 2106266Sjulian * Mach Operating System 3139823Simp * Copyright (c) 1992 Carnegie Mellon University 4139823Simp * All Rights Reserved. 5139823Simp * 6144674Sglebius * Permission to use, copy, modify and distribute this software and its 7106266Sjulian * documentation is hereby granted, provided that both the copyright 8106266Sjulian * notice and this permission notice appear in all copies of the 9106266Sjulian * software, derivative works or modified versions, and any portions 10106266Sjulian * thereof, and that both notices appear in supporting documentation. 11106266Sjulian * 12106319Sjulian * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 13106266Sjulian * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR 14106319Sjulian * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 15106319Sjulian * 16106266Sjulian * Carnegie Mellon requests users of this software to return to 17106319Sjulian * 18106319Sjulian * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 19106266Sjulian * School of Computer Science 20106266Sjulian * Carnegie Mellon University 21106266Sjulian * Pittsburgh PA 15213-3890 22106319Sjulian * 23106319Sjulian * any improvements or extensions that they make and grant Carnegie Mellon 24106319Sjulian * the rights to redistribute these changes. 25106266Sjulian */ 26106266Sjulian 27106266Sjulian#ifndef lint 28106266Sjulianstatic const char rcsid[] = 29106266Sjulian "$FreeBSD: head/sbin/fdisk/fdisk.c 65054 2000-08-24 17:54:45Z jhb $"; 30106319Sjulian#endif /* not lint */ 31106266Sjulian 32106266Sjulian#include <sys/disklabel.h> 33106266Sjulian#include <sys/stat.h> 34106266Sjulian#include <ctype.h> 35106266Sjulian#include <fcntl.h> 36106266Sjulian#include <err.h> 37106266Sjulian#include <errno.h> 38209728Semaste#include <stdio.h> 39106266Sjulian#include <stdlib.h> 40106266Sjulian#include <string.h> 41125077Sharti#include <unistd.h> 42125077Sharti 43125077Shartiint iotest; 44106266Sjulian 45106266Sjulian#define LBUF 100 46143387Sbmilekicstatic char lbuf[LBUF]; 47143387Sbmilekic 48143387Sbmilekic#define MBRSIGOFF 510 49143387Sbmilekic 50106266Sjulian/* 51106266Sjulian * 52106266Sjulian * Ported to 386bsd by Julian Elischer Thu Oct 15 20:26:46 PDT 1992 53106266Sjulian * 54106266Sjulian * 14-Dec-89 Robert Baron (rvb) at Carnegie-Mellon University 55106266Sjulian * Copyright (c) 1989 Robert. V. Baron 56143387Sbmilekic * Created. 57106266Sjulian */ 58106266Sjulian 59106266Sjulian#define Decimal(str, ans, tmp) if (decimal(str, &tmp, ans)) ans = tmp 60106266Sjulian#define Hex(str, ans, tmp) if (hex(str, &tmp, ans)) ans = tmp 61106266Sjulian#define String(str, ans, len) {char *z = ans; char **dflt = &z; if (string(str, dflt)) strncpy(ans, *dflt, len); } 62106266Sjulian 63106266Sjulian#define RoundCyl(x) ((((x) + cylsecs - 1) / cylsecs) * cylsecs) 64106266Sjulian 65106266Sjulian#define MAX_SEC_SIZE 2048 /* maximum section size that is supported */ 66106266Sjulian#define MIN_SEC_SIZE 512 /* the sector size to start sensing at */ 67106266Sjulianint secsize = 0; /* the sensed sector size */ 68144674Sglebius 69106266Sjulianconst char *disk; 70106266Sjulianconst char *disks[] = 71196019Srwatson{ 72106266Sjulian "/dev/ad0", "/dev/wd0", "/dev/da0", "/dev/od0", 0 73106266Sjulian}; 74106266Sjulian 75106266Sjulianstruct disklabel disklabel; /* disk parameters */ 76106266Sjulian 77106266Sjulianint cyls, sectors, heads, cylsecs, disksecs; 78106266Sjulian 79106266Sjulianstruct mboot 80106266Sjulian{ 81167156Semaste unsigned char padding[2]; /* force the longs to be long aligned */ 82167156Semaste unsigned char *bootinst; /* boot code */ 83106266Sjulian off_t bootinst_size; 84106266Sjulian struct dos_partition parts[4]; 85106266Sjulian}; 86144674Sglebiusstruct mboot mboot = {{0}, NULL, 0}; 87144674Sglebius 88106266Sjulian#define ACTIVE 0x80 89106319Sjulian#define BOOT_MAGIC 0xAA55 90167160Semaste 91106266Sjulianint dos_cyls; 92137138Sglebiusint dos_heads; 93144674Sglebiusint dos_sectors; 94144674Sglebiusint dos_cylsecs; 95167156Semaste 96167160Semaste#define DOSSECT(s,c) ((s & 0x3f) | ((c >> 2) & 0xc0)) 97106266Sjulian#define DOSCYL(c) (c & 0xff) 98106266Sjulianstatic int partition = -1; 99106266Sjulian 100106266Sjulian 101106266Sjulian#define MAX_ARGS 10 102106266Sjulian 103106266Sjulianstatic int current_line_number; 104106266Sjulian 105106266Sjulianstatic int geom_processed = 0; 106106266Sjulianstatic int part_processed = 0; 107106266Sjulianstatic int active_processed = 0; 108144674Sglebius 109106266Sjulian 110106266Sjuliantypedef struct cmd { 111106266Sjulian char cmd; 112106266Sjulian int n_args; 113125243Sharti struct arg { 114106266Sjulian char argtype; 115144674Sglebius int arg_val; 116106266Sjulian } args[MAX_ARGS]; 117106266Sjulian} CMD; 118144674Sglebius 119167156Semaste 120167156Semastestatic int B_flag = 0; /* replace boot code */ 121167160Semastestatic int I_flag = 0; /* use entire disk for FreeBSD */ 122167160Semastestatic int a_flag = 0; /* set active partition */ 123167160Semastestatic char *b_flag = NULL; /* path to boot code */ 124167156Semastestatic int i_flag = 0; /* replace partition data */ 125167156Semastestatic int u_flag = 0; /* update partition data */ 126106266Sjulianstatic int s_flag = 0; /* Print a summary and exit */ 127106266Sjulianstatic int t_flag = 0; /* test only, if f_flag is given */ 128125077Shartistatic char *f_flag = NULL; /* Read config info from file */ 129106266Sjulianstatic int v_flag = 0; /* Be verbose */ 130106266Sjulian 131106266Sjulianstruct part_type 132106266Sjulian{ 133106266Sjulian unsigned char type; 134106266Sjulian char *name; 135106266Sjulian}part_types[] = 136106266Sjulian{ 137106266Sjulian {0x00, "unused"} 138106266Sjulian ,{0x01, "Primary DOS with 12 bit FAT"} 139106266Sjulian ,{0x02, "XENIX / filesystem"} 140106266Sjulian ,{0x03, "XENIX /usr filesystem"} 141106266Sjulian ,{0x04, "Primary DOS with 16 bit FAT (<= 32MB)"} 142106266Sjulian ,{0x05, "Extended DOS"} 143106266Sjulian ,{0x06, "Primary 'big' DOS (> 32MB)"} 144106266Sjulian ,{0x07, "OS/2 HPFS, NTFS, QNX-2 (16 bit) or Advanced UNIX"} 145106266Sjulian ,{0x08, "AIX filesystem"} 146167156Semaste ,{0x09, "AIX boot partition or Coherent"} 147167156Semaste ,{0x0A, "OS/2 Boot Manager or OPUS"} 148167156Semaste ,{0x0B, "DOS or Windows 95 with 32 bit FAT"} 149167156Semaste ,{0x0C, "DOS or Windows 95 with 32 bit FAT, LBA"} 150167156Semaste ,{0x0E, "Primary 'big' DOS (> 32MB, LBA)"} 151167156Semaste ,{0x0F, "Extended DOS, LBA"} 152167156Semaste ,{0x10, "OPUS"} 153167156Semaste ,{0x39, "plan9"} 154167160Semaste ,{0x40, "VENIX 286"} 155167160Semaste ,{0x4D, "QNX 4.2 Primary"} 156167160Semaste ,{0x4E, "QNX 4.2 Secondary"} 157167160Semaste ,{0x4F, "QNX 4.2 Tertiary"} 158167160Semaste ,{0x50, "DM"} 159167160Semaste ,{0x51, "DM"} 160167160Semaste ,{0x52, "CP/M or Microport SysV/AT"} 161167160Semaste ,{0x56, "GB"} 162106266Sjulian ,{0x61, "Speed"} 163106266Sjulian ,{0x63, "ISC UNIX, other System V/386, GNU HURD or Mach"} 164106266Sjulian ,{0x64, "Novell Netware 2.xx"} 165106266Sjulian ,{0x65, "Novell Netware 3.xx"} 166106266Sjulian ,{0x75, "PCIX"} 167106266Sjulian ,{0x80, "Minix 1.1 ... 1.4a"} 168106266Sjulian ,{0x81, "Minix 1.4b ... 1.5.10"} 169106266Sjulian ,{0x82, "Linux swap or Solaris x86"} 170106266Sjulian ,{0x83, "Linux filesystem"} 171106266Sjulian ,{0x93, "Amoeba filesystem"} 172106266Sjulian ,{0x94, "Amoeba bad block table"} 173106266Sjulian ,{0x9F, "BSD/OS"} 174106266Sjulian ,{0xA0, "Suspend to Disk"} 175106266Sjulian ,{0xA5, "FreeBSD/NetBSD/386BSD"} 176106266Sjulian ,{0xA6, "OpenBSD"} 177106266Sjulian ,{0xA7, "NEXTSTEP"} 178106266Sjulian ,{0xA9, "NetBSD"} 179106266Sjulian ,{0xB7, "BSDI BSD/386 filesystem"} 180106266Sjulian ,{0xB8, "BSDI BSD/386 swap"} 181106266Sjulian ,{0xDB, "Concurrent CPM or C.DOS or CTOS"} 182106266Sjulian ,{0xE1, "Speed"} 183106266Sjulian ,{0xE3, "Speed"} 184106266Sjulian ,{0xE4, "Speed"} 185106266Sjulian ,{0xF1, "Speed"} 186106266Sjulian ,{0xF2, "DOS 3.3+ Secondary"} 187106266Sjulian ,{0xF4, "Speed"} 188106266Sjulian ,{0xFF, "BBT (Bad Blocks Table)"} 189106266Sjulian}; 190106266Sjulian 191106266Sjulianstatic void print_s0(int which); 192106266Sjulianstatic void print_part(int i); 193106266Sjulianstatic void init_sector0(unsigned long start); 194106266Sjulianstatic void init_boot(void); 195106266Sjulianstatic void change_part(int i); 196106266Sjulianstatic void print_params(); 197106266Sjulianstatic void change_active(int which); 198106266Sjulianstatic void change_code(); 199106266Sjulianstatic void get_params_to_use(); 200106266Sjulianstatic void dos(int sec, int size, unsigned char *c, unsigned char *s, 201106266Sjulian unsigned char *h); 202106266Sjulianstatic int open_disk(int u_flag); 203106266Sjulianstatic ssize_t read_disk(off_t sector, void *buf); 204106266Sjulianstatic ssize_t write_disk(off_t sector, void *buf); 205106266Sjulianstatic int get_params(); 206125033Shartistatic int read_s0(); 207125033Shartistatic int write_s0(); 208144674Sglebiusstatic int ok(char *str); 209144674Sglebiusstatic int decimal(char *str, int *num, int deflt); 210144674Sglebiusstatic char *get_type(int type); 211125033Shartistatic int read_config(char *config_file); 212125033Shartistatic void reset_boot(void); 213153690Sglebiusstatic int sanitize_partition(struct dos_partition *); 214153690Sglebiusstatic void usage(void); 215153690Sglebius#if 0 216153690Sglebiusstatic int hex(char *str, int *num, int deflt); 217153690Sglebiusstatic int string(char *str, char **ans); 218153690Sglebius#endif 219153690Sglebius 220167156Semaste 221167156Semasteint 222167156Semastemain(int argc, char *argv[]) 223167156Semaste{ 224167156Semaste int c, i; 225167156Semaste 226167156Semaste while ((c = getopt(argc, argv, "BIab:f:istuv1234")) != -1) 227167156Semaste switch (c) { 228167156Semaste case 'B': 229167156Semaste B_flag = 1; 230167156Semaste break; 231167156Semaste case 'I': 232167156Semaste I_flag = 1; 233167156Semaste break; 234167160Semaste case 'a': 235167160Semaste a_flag = 1; 236167160Semaste break; 237167160Semaste case 'b': 238167160Semaste b_flag = optarg; 239167160Semaste break; 240167160Semaste case 'f': 241167160Semaste f_flag = optarg; 242167160Semaste break; 243167160Semaste case 'i': 244167160Semaste i_flag = 1; 245167160Semaste break; 246167160Semaste case 's': 247167160Semaste s_flag = 1; 248106266Sjulian break; 249106266Sjulian case 't': 250106266Sjulian t_flag = 1; 251106266Sjulian break; 252106266Sjulian case 'u': 253129823Sjulian u_flag = 1; 254129823Sjulian break; 255129823Sjulian case 'v': 256129823Sjulian v_flag = 1; 257129823Sjulian break; 258129823Sjulian case '1': 259144674Sglebius case '2': 260129823Sjulian case '3': 261129823Sjulian case '4': 262129823Sjulian partition = c - '0'; 263106266Sjulian break; 264106266Sjulian default: 265106266Sjulian usage(); 266144674Sglebius } 267125032Sharti if (f_flag || i_flag) 268106266Sjulian u_flag = 1; 269106266Sjulian if (t_flag) 270106266Sjulian v_flag = 1; 271106266Sjulian argc -= optind; 272106321Sjulian argv += optind; 273106266Sjulian 274106266Sjulian if (argc > 0) 275106266Sjulian { 276125030Sharti static char realname[12]; 277106266Sjulian 278106266Sjulian if(strncmp(argv[0], "/dev", 4) == 0) 279106266Sjulian disk = argv[0]; 280106321Sjulian else 281106321Sjulian { 282106266Sjulian snprintf(realname, 12, "/dev/%s", argv[0]); 283137138Sglebius disk = realname; 284137138Sglebius } 285106266Sjulian 286106266Sjulian if (open_disk(u_flag) < 0) 287106266Sjulian err(1, "cannot open disk %s", disk); 288106266Sjulian } 289106266Sjulian else 290106266Sjulian { 291106266Sjulian int rv = 0; 292106266Sjulian 293106266Sjulian for(i = 0; disks[i]; i++) 294144674Sglebius { 295106266Sjulian disk = disks[i]; 296106266Sjulian rv = open_disk(u_flag); 297144674Sglebius if(rv != -2) break; 298106266Sjulian } 299144674Sglebius if(rv < 0) 300106266Sjulian err(1, "cannot open any disk"); 301106266Sjulian } 302106266Sjulian 303106266Sjulian /* (abu)use mboot.bootinst to probe for the sector size */ 304144674Sglebius if ((mboot.bootinst = malloc(MAX_SEC_SIZE)) == NULL) 305106266Sjulian err(1, "cannot allocate buffer to determine disk sector size"); 306106266Sjulian read_disk(0, mboot.bootinst); 307106266Sjulian free(mboot.bootinst); 308106266Sjulian mboot.bootinst = NULL; 309144674Sglebius 310144674Sglebius if (s_flag) 311144674Sglebius { 312144674Sglebius int i; 313144674Sglebius struct dos_partition *partp; 314144674Sglebius 315144674Sglebius if (read_s0()) 316144674Sglebius err(1, "read_s0"); 317144674Sglebius printf("%s: %d cyl %d hd %d sec\n", disk, dos_cyls, dos_heads, 318144674Sglebius dos_sectors); 319144674Sglebius printf("Part %11s %11s Type Flags\n", "Start", "Size"); 320144674Sglebius for (i = 0; i < NDOSPART; i++) { 321144674Sglebius partp = ((struct dos_partition *) &mboot.parts) + i; 322144674Sglebius if (partp->dp_start == 0 && partp->dp_size == 0) 323144674Sglebius continue; 324144674Sglebius printf("%4d: %11lu %11lu 0x%02x 0x%02x\n", i + 1, 325144674Sglebius (u_long) partp->dp_start, 326144674Sglebius (u_long) partp->dp_size, partp->dp_typ, 327144674Sglebius partp->dp_flag); 328144674Sglebius } 329144674Sglebius exit(0); 330144674Sglebius } 331144674Sglebius 332144674Sglebius printf("******* Working on device %s *******\n",disk); 333144674Sglebius 334144674Sglebius if (I_flag) 335144674Sglebius { 336144674Sglebius struct dos_partition *partp; 337144674Sglebius 338144674Sglebius read_s0(); 339144674Sglebius reset_boot(); 340106266Sjulian partp = (struct dos_partition *) (&mboot.parts[0]); 341106266Sjulian partp->dp_typ = DOSPTYP_386BSD; 342106266Sjulian partp->dp_flag = ACTIVE; 343106321Sjulian partp->dp_start = dos_sectors; 344106266Sjulian partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs - 345144674Sglebius dos_sectors; 346144674Sglebius 347106266Sjulian dos(partp->dp_start, partp->dp_size, 348106266Sjulian &partp->dp_scyl, &partp->dp_ssect, &partp->dp_shd); 349106321Sjulian dos(partp->dp_start + partp->dp_size - 1, partp->dp_size, 350144674Sglebius &partp->dp_ecyl, &partp->dp_esect, &partp->dp_ehd); 351106266Sjulian if (v_flag) 352106266Sjulian print_s0(-1); 353106435Sjulian write_s0(); 354106435Sjulian exit(0); 355106435Sjulian } 356106435Sjulian if (f_flag) 357106266Sjulian { 358106266Sjulian if (read_s0() || i_flag) 359106266Sjulian { 360106266Sjulian reset_boot(); 361106266Sjulian } 362106266Sjulian 363106266Sjulian if (!read_config(f_flag)) 364106266Sjulian { 365106266Sjulian exit(1); 366106266Sjulian } 367106266Sjulian if (v_flag) 368106266Sjulian { 369106266Sjulian print_s0(-1); 370106266Sjulian } 371106266Sjulian if (!t_flag) 372106319Sjulian { 373106321Sjulian write_s0(); 374106266Sjulian } 375106319Sjulian } 376106266Sjulian else 377106319Sjulian { 378106266Sjulian if(u_flag) 379106319Sjulian { 380106266Sjulian get_params_to_use(); 381106266Sjulian } 382106266Sjulian else 383106266Sjulian { 384106266Sjulian print_params(); 385106266Sjulian } 386106266Sjulian 387106266Sjulian if (read_s0()) 388144674Sglebius init_sector0(dos_sectors); 389144674Sglebius 390144674Sglebius printf("Media sector size is %d\n", secsize); 391106266Sjulian printf("Warning: BIOS sector numbering starts with sector 1\n"); 392106266Sjulian printf("Information from DOS bootblock is:\n"); 393106266Sjulian if (partition == -1) 394125033Sharti for (i = 1; i <= NDOSPART; i++) 395144674Sglebius change_part(i); 396144674Sglebius else 397144674Sglebius change_part(partition); 398144674Sglebius 399144674Sglebius if (u_flag || a_flag) 400125033Sharti change_active(partition); 401106266Sjulian 402106266Sjulian if (B_flag) 403106266Sjulian change_code(); 404106266Sjulian 405106266Sjulian if (u_flag || a_flag || B_flag) { 406106266Sjulian if (!t_flag) 407144674Sglebius { 408144674Sglebius printf("\nWe haven't changed the partition table yet. "); 409144674Sglebius printf("This is your last chance.\n"); 410144674Sglebius } 411144674Sglebius print_s0(-1); 412144674Sglebius if (!t_flag) 413144674Sglebius { 414144674Sglebius if (ok("Should we write new partition table?")) 415144674Sglebius write_s0(); 416144674Sglebius } 417144674Sglebius else 418144674Sglebius { 419153690Sglebius printf("\n-t flag specified -- partition table not written.\n"); 420153690Sglebius } 421153690Sglebius } 422153690Sglebius } 423153690Sglebius 424153690Sglebius exit(0); 425153690Sglebius} 426153690Sglebius 427153690Sglebiusstatic void 428153690Sglebiususage() 429153690Sglebius{ 430153690Sglebius fprintf(stderr, "%s%s", 431153690Sglebius "usage: fdisk [-BIaistu] [-b bootcode] [-1234] [disk]\n", 432153690Sglebius " fdisk -f configfile [-itv] [disk]\n"); 433153690Sglebius exit(1); 434167156Semaste} 435167156Semaste 436167156Semastestatic void 437167156Semasteprint_s0(int which) 438167168Semaste{ 439167168Semasteint i; 440167168Semaste 441167168Semaste print_params(); 442167156Semaste printf("Information from DOS bootblock is:\n"); 443167156Semaste if (which == -1) 444167156Semaste for (i = 1; i <= NDOSPART; i++) 445167156Semaste printf("%d: ", i), print_part(i); 446167156Semaste else 447167156Semaste print_part(which); 448167156Semaste} 449167156Semaste 450167156Semastestatic struct dos_partition mtpart = { 0 }; 451167156Semaste 452167156Semastestatic void 453167156Semasteprint_part(int i) 454167156Semaste{ 455167156Semaste struct dos_partition *partp; 456167156Semaste u_int64_t part_mb; 457167156Semaste 458167156Semaste partp = ((struct dos_partition *) &mboot.parts) + i - 1; 459167156Semaste 460167156Semaste if (!bcmp(partp, &mtpart, sizeof (struct dos_partition))) { 461167160Semaste printf("<UNUSED>\n"); 462167160Semaste return; 463167160Semaste } 464167160Semaste /* 465167168Semaste * Be careful not to overflow. 466167168Semaste */ 467167168Semaste part_mb = partp->dp_size; 468167168Semaste part_mb *= secsize; 469167160Semaste part_mb /= (1024 * 1024); 470167160Semaste printf("sysid %d,(%s)\n", partp->dp_typ, get_type(partp->dp_typ)); 471167160Semaste printf(" start %lu, size %lu (%qd Meg), flag %x%s\n", 472167160Semaste (u_long)partp->dp_start, 473167160Semaste (u_long)partp->dp_size, 474167160Semaste part_mb, 475167160Semaste partp->dp_flag, 476167160Semaste partp->dp_flag == ACTIVE ? " (active)" : ""); 477167160Semaste printf("\tbeg: cyl %d/ sector %d/ head %d;\n\tend: cyl %d/ sector %d/ head %d\n" 478167160Semaste ,DPCYL(partp->dp_scyl, partp->dp_ssect) 479167160Semaste ,DPSECT(partp->dp_ssect) 480167160Semaste ,partp->dp_shd 481167160Semaste ,DPCYL(partp->dp_ecyl, partp->dp_esect) 482167160Semaste ,DPSECT(partp->dp_esect) 483167160Semaste ,partp->dp_ehd); 484167160Semaste} 485167160Semaste 486167160Semaste 487167160Semastestatic void 488167160Semasteinit_boot(void) 489167160Semaste{ 490167160Semaste const char *fname; 491167160Semaste int fd, n; 492167160Semaste struct stat sb; 493167160Semaste 494167160Semaste fname = b_flag ? b_flag : "/boot/mbr"; 495167160Semaste if ((fd = open(fname, O_RDONLY)) == -1 || 496167160Semaste fstat(fd, &sb) == -1) 497167160Semaste err(1, "%s", fname); 498167160Semaste if ((mboot.bootinst_size = sb.st_size) % secsize != 0) 499167160Semaste errx(1, "%s: length must be a multiple of sector size", fname); 500106266Sjulian if (mboot.bootinst != NULL) 501106266Sjulian free(mboot.bootinst); 502106266Sjulian if ((mboot.bootinst = malloc(mboot.bootinst_size = sb.st_size)) == NULL) 503106266Sjulian errx(1, "%s: unable to allocate read buffer", fname); 504106266Sjulian if ((n = read(fd, mboot.bootinst, mboot.bootinst_size)) == -1 || 505106435Sjulian close(fd)) 506106435Sjulian err(1, "%s", fname); 507106435Sjulian if (n != mboot.bootinst_size) 508106435Sjulian errx(1, "%s: short read", fname); 509106435Sjulian} 510106435Sjulian 511144674Sglebius 512144674Sglebiusstatic void 513144674Sglebiusinit_sector0(unsigned long start) 514144674Sglebius{ 515144674Sglebiusstruct dos_partition *partp = (struct dos_partition *) (&mboot.parts[3]); 516144674Sglebius 517144674Sglebius init_boot(); 518144674Sglebius 519144674Sglebius partp->dp_typ = DOSPTYP_386BSD; 520144674Sglebius partp->dp_flag = ACTIVE; 521106435Sjulian start = ((start + dos_sectors - 1) / dos_sectors) * dos_sectors; 522106435Sjulian if(start == 0) 523144674Sglebius start = dos_sectors; 524106435Sjulian partp->dp_start = start; 525106435Sjulian partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs - start; 526106435Sjulian 527106435Sjulian dos(partp->dp_start, partp->dp_size, 528106266Sjulian &partp->dp_scyl, &partp->dp_ssect, &partp->dp_shd); 529106266Sjulian dos(partp->dp_start + partp->dp_size - 1, partp->dp_size, 530106266Sjulian &partp->dp_ecyl, &partp->dp_esect, &partp->dp_ehd); 531106266Sjulian} 532106266Sjulian 533106266Sjulianstatic void 534144674Sglebiuschange_part(int i) 535106321Sjulian{ 536144674Sglebiusstruct dos_partition *partp = ((struct dos_partition *) &mboot.parts) + i - 1; 537106321Sjulian 538106266Sjulian printf("The data for partition %d is:\n", i); 539106266Sjulian print_part(i); 540106266Sjulian 541106266Sjulian if (u_flag && ok("Do you want to change it?")) { 542106266Sjulian int tmp; 543106266Sjulian 544106266Sjulian if (i_flag) { 545106266Sjulian bzero((char *)partp, sizeof (struct dos_partition)); 546106266Sjulian if (i == 4) { 547106266Sjulian init_sector0(1); 548106321Sjulian printf("\nThe static data for the DOS partition 4 has been reinitialized to:\n"); 549106266Sjulian print_part(i); 550144674Sglebius } 551144674Sglebius } 552106266Sjulian 553106266Sjulian do { 554106321Sjulian Decimal("sysid (165=FreeBSD)", partp->dp_typ, tmp); 555106321Sjulian Decimal("start", partp->dp_start, tmp); 556106266Sjulian Decimal("size", partp->dp_size, tmp); 557106266Sjulian 558144674Sglebius if (ok("Explicitly specify beg/end address ?")) 559106266Sjulian { 560106321Sjulian int tsec,tcyl,thd; 561106266Sjulian tcyl = DPCYL(partp->dp_scyl,partp->dp_ssect); 562106266Sjulian thd = partp->dp_shd; 563144674Sglebius tsec = DPSECT(partp->dp_ssect); 564106266Sjulian Decimal("beginning cylinder", tcyl, tmp); 565144674Sglebius Decimal("beginning head", thd, tmp); 566106266Sjulian Decimal("beginning sector", tsec, tmp); 567125031Sharti partp->dp_scyl = DOSCYL(tcyl); 568106266Sjulian partp->dp_ssect = DOSSECT(tsec,tcyl); 569167160Semaste partp->dp_shd = thd; 570106266Sjulian 571106266Sjulian tcyl = DPCYL(partp->dp_ecyl,partp->dp_esect); 572106266Sjulian thd = partp->dp_ehd; 573106266Sjulian tsec = DPSECT(partp->dp_esect); 574106266Sjulian Decimal("ending cylinder", tcyl, tmp); 575106266Sjulian Decimal("ending head", thd, tmp); 576106266Sjulian Decimal("ending sector", tsec, tmp); 577106266Sjulian partp->dp_ecyl = DOSCYL(tcyl); 578106266Sjulian partp->dp_esect = DOSSECT(tsec,tcyl); 579106266Sjulian partp->dp_ehd = thd; 580144674Sglebius } else { 581106266Sjulian if (!sanitize_partition(partp)) 582106266Sjulian partp->dp_typ = 0; 583106266Sjulian dos(partp->dp_start, partp->dp_size, 584106321Sjulian &partp->dp_scyl, &partp->dp_ssect, &partp->dp_shd); 585106321Sjulian dos(partp->dp_start + partp->dp_size - 1, partp->dp_size, 586125077Sharti &partp->dp_ecyl, &partp->dp_esect, &partp->dp_ehd); 587144674Sglebius } 588106266Sjulian 589106266Sjulian print_part(i); 590106266Sjulian } while (!ok("Are we happy with this entry?")); 591106266Sjulian } 592106266Sjulian} 593106266Sjulian 594106266Sjulianstatic void 595106266Sjulianprint_params() 596106266Sjulian{ 597106319Sjulian printf("parameters extracted from in-core disklabel are:\n"); 598106266Sjulian printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n" 599106321Sjulian ,cyls,heads,sectors,cylsecs); 600125077Sharti if((dos_sectors > 63) || (dos_cyls > 1023) || (dos_heads > 255)) 601144674Sglebius printf("Figures below won't work with BIOS for partitions not in cyl 1\n"); 602106321Sjulian printf("parameters to be used for BIOS calculations are:\n"); 603106266Sjulian printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n" 604106266Sjulian ,dos_cyls,dos_heads,dos_sectors,dos_cylsecs); 605106266Sjulian} 606106266Sjulian 607218909Sbrucecstatic void 608106435Sjulianchange_active(int which) 609106435Sjulian{ 610106435Sjulianint i; 611144674Sglebiusint active = 4, tmp; 612106435Sjulianstruct dos_partition *partp = ((struct dos_partition *) &mboot.parts); 613106435Sjulian 614106435Sjulian if (a_flag && which != -1) 615106266Sjulian active = which; 616144674Sglebius if (!ok("Do you want to change the active partition?")) 617106266Sjulian return; 618106266Sjuliansetactive: 619183225Szec active = 4; 620106266Sjulian do { 621106266Sjulian Decimal("active partition", active, tmp); 622106266Sjulian if (active < 1 || 4 < active) { 623106266Sjulian printf("Active partition number must be in range 1-4." 624106266Sjulian " Try again.\n"); 625106266Sjulian goto setactive; 626106266Sjulian } 627106266Sjulian } while (!ok("Are you happy with this choice")); 628106266Sjulian for (i = 0; i < NDOSPART; i++) 629106266Sjulian partp[i].dp_flag = 0; 630106266Sjulian if (active > 0 && active <= NDOSPART) 631125077Sharti partp[active-1].dp_flag = ACTIVE; 632106266Sjulian} 633106319Sjulian 634106266Sjulianstatic void 635106266Sjulianchange_code() 636106266Sjulian{ 637106266Sjulian if (ok("Do you want to change the boot code?")) 638106266Sjulian init_boot(); 639106266Sjulian} 640106266Sjulian 641106266Sjulianvoid 642106266Sjulianget_params_to_use() 643106266Sjulian{ 644106266Sjulian int tmp; 645144674Sglebius print_params(); 646106266Sjulian if (ok("Do you want to change our idea of what BIOS thinks ?")) 647106266Sjulian { 648106266Sjulian do 649106266Sjulian { 650106266Sjulian Decimal("BIOS's idea of #cylinders", dos_cyls, tmp); 651144674Sglebius Decimal("BIOS's idea of #heads", dos_heads, tmp); 652106266Sjulian Decimal("BIOS's idea of #sectors", dos_sectors, tmp); 653106266Sjulian dos_cylsecs = dos_heads * dos_sectors; 654106266Sjulian print_params(); 655144674Sglebius } 656144674Sglebius while(!ok("Are you happy with this choice")); 657106266Sjulian } 658106266Sjulian} 659106266Sjulian 660106266Sjulian 661106266Sjulian/***********************************************\ 662106266Sjulian* Change real numbers into strange dos numbers * 663106266Sjulian\***********************************************/ 664106266Sjulianstatic void 665106266Sjuliandos(sec, size, c, s, h) 666106266Sjulianint sec, size; 667106266Sjulianunsigned char *c, *s, *h; 668106266Sjulian{ 669125031Shartiint cy; 670106266Sjulianint hd; 671106266Sjulian 672106266Sjulian if (sec == 0 && size == 0) { 673106266Sjulian *s = *c = *h = 0; 674106266Sjulian return; 675167168Semaste } 676106266Sjulian 677106266Sjulian cy = sec / ( dos_cylsecs ); 678106266Sjulian sec = sec - cy * ( dos_cylsecs ); 679106266Sjulian 680106266Sjulian hd = sec / dos_sectors; 681144674Sglebius sec = (sec - hd * dos_sectors) + 1; 682144674Sglebius 683106266Sjulian *h = hd; 684144674Sglebius *c = cy & 0xff; 685144674Sglebius *s = (sec & 0x3f) | ( (cy & 0x300) >> 2); 686144674Sglebius} 687144674Sglebius 688144674Sglebiusint fd; 689144674Sglebius 690144674Sglebius /* Getting device status */ 691144674Sglebius 692144674Sglebiusstatic int 693144674Sglebiusopen_disk(int u_flag) 694144674Sglebius{ 695144674Sglebiusstruct stat st; 696144674Sglebius 697144674Sglebius if (stat(disk, &st) == -1) { 698153690Sglebius warnx("can't get file status of %s", disk); 699144674Sglebius return -1; 700144674Sglebius } 701144674Sglebius if ( !(st.st_mode & S_IFCHR) ) 702144674Sglebius warnx("device %s is not character special", disk); 703106266Sjulian if ((fd = open(disk, 704106266Sjulian a_flag || I_flag || B_flag || u_flag ? O_RDWR : O_RDONLY)) == -1) { 705106266Sjulian if(errno == ENXIO) 706106266Sjulian return -2; 707106266Sjulian warnx("can't open device %s", disk); 708106266Sjulian return -1; 709144674Sglebius } 710106266Sjulian if (get_params(0) == -1) { 711144674Sglebius warnx("can't get disk parameters on %s", disk); 712144674Sglebius return -1; 713144674Sglebius } 714144674Sglebius return fd; 715144674Sglebius} 716106266Sjulian 717106266Sjulianstatic ssize_t 718106266Sjulianread_disk(off_t sector, void *buf) 719106266Sjulian{ 720106266Sjulian lseek(fd,(sector * 512), 0); 721106266Sjulian if( secsize == 0 ) 722106266Sjulian for( secsize = MIN_SEC_SIZE; secsize <= MAX_SEC_SIZE; secsize *= 2 ) 723106266Sjulian { 724125243Sharti /* try the read */ 725106266Sjulian int size = read(fd, buf, secsize); 726125243Sharti if( size == secsize ) 727106266Sjulian /* it worked so return */ 728106266Sjulian return secsize; 729106266Sjulian } 730125077Sharti else 731106266Sjulian return read( fd, buf, secsize ); 732144674Sglebius 733106321Sjulian /* we failed to read at any of the sizes */ 734106266Sjulian return -1; 735106266Sjulian} 736106266Sjulian 737106266Sjulianstatic ssize_t 738125033Shartiwrite_disk(off_t sector, void *buf) 739141745Sru{ 740125033Sharti lseek(fd,(sector * 512), 0); 741125033Sharti /* write out in the size that the read_disk found worked */ 742125033Sharti return write(fd, buf, secsize); 743125033Sharti} 744153690Sglebius 745153690Sglebiusstatic int 746153690Sglebiusget_params() 747153690Sglebius{ 748153690Sglebius 749153690Sglebius if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) { 750153690Sglebius warnx("can't get disk parameters on %s; supplying dummy ones", disk); 751153690Sglebius dos_cyls = cyls = 1; 752153690Sglebius dos_heads = heads = 1; 753153690Sglebius dos_sectors = sectors = 1; 754153690Sglebius dos_cylsecs = cylsecs = heads * sectors; 755153690Sglebius disksecs = cyls * heads * sectors; 756153690Sglebius return disksecs; 757153690Sglebius } 758153690Sglebius 759106266Sjulian dos_cyls = cyls = disklabel.d_ncylinders; 760125243Sharti dos_heads = heads = disklabel.d_ntracks; 761106266Sjulian dos_sectors = sectors = disklabel.d_nsectors; 762125243Sharti dos_cylsecs = cylsecs = heads * sectors; 763138268Sglebius disksecs = cyls * heads * sectors; 764137136Sglebius 765106266Sjulian return (disksecs); 766106266Sjulian} 767106266Sjulian 768154707Sglebius 769106266Sjulianstatic int 770106266Sjulianread_s0() 771154707Sglebius{ 772106266Sjulian mboot.bootinst_size = secsize; 773106266Sjulian if (mboot.bootinst != NULL) 774154707Sglebius free(mboot.bootinst); 775106266Sjulian if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL) { 776106266Sjulian warnx("unable to allocate buffer to read fdisk " 777125077Sharti "partition table"); 778106321Sjulian return -1; 779125077Sharti } 780106266Sjulian if (read_disk(0, mboot.bootinst) == -1) { 781144674Sglebius warnx("can't read fdisk partition table"); 782106266Sjulian return -1; 783106266Sjulian } 784154707Sglebius if (*(uint16_t *)&mboot.bootinst[MBRSIGOFF] != BOOT_MAGIC) { 785106266Sjulian warnx("invalid fdisk partition table found"); 786125031Sharti /* So should we initialize things */ 787106266Sjulian return -1; 788106266Sjulian } 789106266Sjulian memcpy(mboot.parts, &mboot.bootinst[DOSPARTOFF], sizeof(mboot.parts)); 790167156Semaste return 0; 791167156Semaste} 792167156Semaste 793167156Semastestatic int 794167156Semastewrite_s0() 795167156Semaste{ 796167156Semaste#ifdef NOT_NOW 797106266Sjulian int flag; 798106266Sjulian#endif 799106266Sjulian int sector; 800144674Sglebius 801125031Sharti if (iotest) { 802106266Sjulian print_s0(-1); 803154707Sglebius return 0; 804154707Sglebius } 805154707Sglebius memcpy(&mboot.bootinst[DOSPARTOFF], mboot.parts, sizeof(mboot.parts)); 806154707Sglebius /* 807106266Sjulian * write enable label sector before write (if necessary), 808106266Sjulian * disable after writing. 809106266Sjulian * needed if the disklabel protected area also protects 810106266Sjulian * sector 0. (e.g. empty disk) 811106266Sjulian */ 812106266Sjulian#ifdef NOT_NOW 813106266Sjulian flag = 1; 814106266Sjulian if (ioctl(fd, DIOCWLABEL, &flag) < 0) 815167156Semaste warn("ioctl DIOCWLABEL"); 816167156Semaste#endif 817167156Semaste for(sector = 0; sector < mboot.bootinst_size / secsize; sector++) 818167156Semaste if (write_disk(sector, 819167156Semaste &mboot.bootinst[sector * secsize]) == -1) { 820167156Semaste warn("can't write fdisk partition table"); 821167156Semaste return -1; 822167156Semaste#ifdef NOT_NOW 823167156Semaste flag = 0; 824167156Semaste (void) ioctl(fd, DIOCWLABEL, &flag); 825167156Semaste#endif 826167156Semaste } 827167156Semaste#ifdef NOT_NOW 828167156Semaste flag = 0; 829167156Semaste (void) ioctl(fd, DIOCWLABEL, &flag); 830167156Semaste#endif 831167156Semaste return(0); 832167156Semaste} 833167156Semaste 834167156Semaste 835167156Semastestatic int 836167156Semasteok(str) 837167160Semastechar *str; 838167160Semaste{ 839167160Semaste printf("%s [n] ", str); 840167160Semaste fgets(lbuf, LBUF, stdin); 841167160Semaste lbuf[strlen(lbuf)-1] = 0; 842167160Semaste 843167160Semaste if (*lbuf && 844167160Semaste (!strcmp(lbuf, "yes") || !strcmp(lbuf, "YES") || 845167160Semaste !strcmp(lbuf, "y") || !strcmp(lbuf, "Y"))) 846167160Semaste return 1; 847167160Semaste else 848167160Semaste return 0; 849167160Semaste} 850167160Semaste 851167160Semastestatic int 852167160Semastedecimal(char *str, int *num, int deflt) 853167160Semaste{ 854167160Semasteint acc = 0, c; 855167160Semastechar *cp; 856167160Semaste 857167160Semaste while (1) { 858167160Semaste printf("Supply a decimal value for \"%s\" [%d] ", str, deflt); 859167160Semaste fgets(lbuf, LBUF, stdin); 860167160Semaste lbuf[strlen(lbuf)-1] = 0; 861167160Semaste 862167160Semaste if (!*lbuf) 863167160Semaste return 0; 864167160Semaste 865167156Semaste cp = lbuf; 866167156Semaste while ((c = *cp) && (c == ' ' || c == '\t')) cp++; 867167156Semaste if (!c) 868167156Semaste return 0; 869167160Semaste while ((c = *cp++)) { 870167156Semaste if (c <= '9' && c >= '0') 871167156Semaste acc = acc * 10 + c - '0'; 872167156Semaste else 873167160Semaste break; 874167156Semaste } 875167156Semaste if (c == ' ' || c == '\t') 876167156Semaste while ((c = *cp) && (c == ' ' || c == '\t')) cp++; 877167160Semaste if (!c) { 878167160Semaste *num = acc; 879167156Semaste return 1; 880167156Semaste } else 881167156Semaste printf("%s is an invalid decimal number. Try again.\n", 882167156Semaste lbuf); 883167156Semaste } 884167156Semaste 885167156Semaste} 886167156Semaste 887167156Semaste#if 0 888167156Semastestatic int 889167156Semastehex(char *str, int *num, int deflt) 890167156Semaste{ 891167156Semasteint acc = 0, c; 892167156Semastechar *cp; 893167156Semaste 894167156Semaste while (1) { 895167156Semaste printf("Supply a hex value for \"%s\" [%x] ", str, deflt); 896167156Semaste fgets(lbuf, LBUF, stdin); 897167160Semaste lbuf[strlen(lbuf)-1] = 0; 898167160Semaste 899167160Semaste if (!*lbuf) 900167160Semaste return 0; 901167160Semaste 902167160Semaste cp = lbuf; 903167160Semaste while ((c = *cp) && (c == ' ' || c == '\t')) cp++; 904167160Semaste if (!c) 905167160Semaste return 0; 906167160Semaste while ((c = *cp++)) { 907167160Semaste if (c <= '9' && c >= '0') 908167160Semaste acc = (acc << 4) + c - '0'; 909167156Semaste else if (c <= 'f' && c >= 'a') 910167156Semaste acc = (acc << 4) + c - 'a' + 10; 911167156Semaste else if (c <= 'F' && c >= 'A') 912167156Semaste acc = (acc << 4) + c - 'A' + 10; 913167156Semaste else 914167156Semaste break; 915167156Semaste } 916167156Semaste if (c == ' ' || c == '\t') 917167156Semaste while ((c = *cp) && (c == ' ' || c == '\t')) cp++; 918167156Semaste if (!c) { 919167156Semaste *num = acc; 920167156Semaste return 1; 921167156Semaste } else 922 printf("%s is an invalid hex number. Try again.\n", 923 lbuf); 924 } 925 926} 927 928static int 929string(char *str, char **ans) 930{ 931int c; 932char *cp = lbuf; 933 934 while (1) { 935 printf("Supply a string value for \"%s\" [%s] ", str, *ans); 936 fgets(lbuf, LBUF, stdin); 937 lbuf[strlen(lbuf)-1] = 0; 938 939 if (!*lbuf) 940 return 0; 941 942 while ((c = *cp) && (c == ' ' || c == '\t')) cp++; 943 if (c == '"') { 944 c = *++cp; 945 *ans = cp; 946 while ((c = *cp) && c != '"') cp++; 947 } else { 948 *ans = cp; 949 while ((c = *cp) && c != ' ' && c != '\t') cp++; 950 } 951 952 if (c) 953 *cp = 0; 954 return 1; 955 } 956} 957#endif 958 959static char * 960get_type(int type) 961{ 962 int numentries = (sizeof(part_types)/sizeof(struct part_type)); 963 int counter = 0; 964 struct part_type *ptr = part_types; 965 966 967 while(counter < numentries) 968 { 969 if(ptr->type == type) 970 { 971 return(ptr->name); 972 } 973 ptr++; 974 counter++; 975 } 976 return("unknown"); 977} 978 979 980static void 981parse_config_line(line, command) 982 char *line; 983 CMD *command; 984{ 985 char *cp, *end; 986 987 cp = line; 988 while (1) /* dirty trick used to insure one exit point for this 989 function */ 990 { 991 memset(command, 0, sizeof(*command)); 992 993 while (isspace(*cp)) ++cp; 994 if (*cp == '\0' || *cp == '#') 995 { 996 break; 997 } 998 command->cmd = *cp++; 999 1000 /* 1001 * Parse args 1002 */ 1003 while (1) 1004 { 1005 while (isspace(*cp)) ++cp; 1006 if (*cp == '#') 1007 { 1008 break; /* found comment */ 1009 } 1010 if (isalpha(*cp)) 1011 { 1012 command->args[command->n_args].argtype = *cp++; 1013 } 1014 if (!isdigit(*cp)) 1015 { 1016 break; /* assume end of line */ 1017 } 1018 end = NULL; 1019 command->args[command->n_args].arg_val = strtol(cp, &end, 0); 1020 if (cp == end) 1021 { 1022 break; /* couldn't parse number */ 1023 } 1024 cp = end; 1025 command->n_args++; 1026 } 1027 break; 1028 } 1029} 1030 1031 1032static int 1033process_geometry(command) 1034 CMD *command; 1035{ 1036 int status = 1, i; 1037 1038 while (1) 1039 { 1040 geom_processed = 1; 1041 if (part_processed) 1042 { 1043 warnx( 1044 "ERROR line %d: the geometry specification line must occur before\n\ 1045 all partition specifications", 1046 current_line_number); 1047 status = 0; 1048 break; 1049 } 1050 if (command->n_args != 3) 1051 { 1052 warnx("ERROR line %d: incorrect number of geometry args", 1053 current_line_number); 1054 status = 0; 1055 break; 1056 } 1057 dos_cyls = -1; 1058 dos_heads = -1; 1059 dos_sectors = -1; 1060 for (i = 0; i < 3; ++i) 1061 { 1062 switch (command->args[i].argtype) 1063 { 1064 case 'c': 1065 dos_cyls = command->args[i].arg_val; 1066 break; 1067 case 'h': 1068 dos_heads = command->args[i].arg_val; 1069 break; 1070 case 's': 1071 dos_sectors = command->args[i].arg_val; 1072 break; 1073 default: 1074 warnx( 1075 "ERROR line %d: unknown geometry arg type: '%c' (0x%02x)", 1076 current_line_number, command->args[i].argtype, 1077 command->args[i].argtype); 1078 status = 0; 1079 break; 1080 } 1081 } 1082 if (status == 0) 1083 { 1084 break; 1085 } 1086 1087 dos_cylsecs = dos_heads * dos_sectors; 1088 1089 /* 1090 * Do sanity checks on parameter values 1091 */ 1092 if (dos_cyls < 0) 1093 { 1094 warnx("ERROR line %d: number of cylinders not specified", 1095 current_line_number); 1096 status = 0; 1097 } 1098 if (dos_cyls == 0 || dos_cyls > 1024) 1099 { 1100 warnx( 1101 "WARNING line %d: number of cylinders (%d) may be out-of-range\n\ 1102 (must be within 1-1024 for normal BIOS operation, unless the entire disk\n\ 1103 is dedicated to FreeBSD)", 1104 current_line_number, dos_cyls); 1105 } 1106 1107 if (dos_heads < 0) 1108 { 1109 warnx("ERROR line %d: number of heads not specified", 1110 current_line_number); 1111 status = 0; 1112 } 1113 else if (dos_heads < 1 || dos_heads > 256) 1114 { 1115 warnx("ERROR line %d: number of heads must be within (1-256)", 1116 current_line_number); 1117 status = 0; 1118 } 1119 1120 if (dos_sectors < 0) 1121 { 1122 warnx("ERROR line %d: number of sectors not specified", 1123 current_line_number); 1124 status = 0; 1125 } 1126 else if (dos_sectors < 1 || dos_sectors > 63) 1127 { 1128 warnx("ERROR line %d: number of sectors must be within (1-63)", 1129 current_line_number); 1130 status = 0; 1131 } 1132 1133 break; 1134 } 1135 return (status); 1136} 1137 1138 1139static int 1140process_partition(command) 1141 CMD *command; 1142{ 1143 int status = 0, partition; 1144 u_int32_t prev_head_boundary, prev_cyl_boundary; 1145 u_int32_t adj_size, max_end; 1146 struct dos_partition *partp; 1147 1148 while (1) 1149 { 1150 part_processed = 1; 1151 if (command->n_args != 4) 1152 { 1153 warnx("ERROR line %d: incorrect number of partition args", 1154 current_line_number); 1155 break; 1156 } 1157 partition = command->args[0].arg_val; 1158 if (partition < 1 || partition > 4) 1159 { 1160 warnx("ERROR line %d: invalid partition number %d", 1161 current_line_number, partition); 1162 break; 1163 } 1164 partp = ((struct dos_partition *) &mboot.parts) + partition - 1; 1165 bzero((char *)partp, sizeof (struct dos_partition)); 1166 partp->dp_typ = command->args[1].arg_val; 1167 partp->dp_start = command->args[2].arg_val; 1168 partp->dp_size = command->args[3].arg_val; 1169 max_end = partp->dp_start + partp->dp_size; 1170 1171 if (partp->dp_typ == 0) 1172 { 1173 /* 1174 * Get out, the partition is marked as unused. 1175 */ 1176 /* 1177 * Insure that it's unused. 1178 */ 1179 bzero((char *)partp, sizeof (struct dos_partition)); 1180 status = 1; 1181 break; 1182 } 1183 1184 /* 1185 * Adjust start upwards, if necessary, to fall on an head boundary. 1186 */ 1187 if (partp->dp_start % dos_sectors != 0) 1188 { 1189 prev_head_boundary = partp->dp_start / dos_sectors * dos_sectors; 1190 if (max_end < dos_sectors || 1191 prev_head_boundary > max_end - dos_sectors) 1192 { 1193 /* 1194 * Can't go past end of partition 1195 */ 1196 warnx( 1197 "ERROR line %d: unable to adjust start of partition %d to fall on\n\ 1198 a head boundary", 1199 current_line_number, partition); 1200 break; 1201 } 1202 warnx( 1203 "WARNING: adjusting start offset of partition %d\n\ 1204 from %u to %u, to fall on a head boundary", 1205 partition, (u_int)partp->dp_start, 1206 (u_int)(prev_head_boundary + dos_sectors)); 1207 partp->dp_start = prev_head_boundary + dos_sectors; 1208 } 1209 1210 /* 1211 * Adjust size downwards, if necessary, to fall on a cylinder 1212 * boundary. 1213 */ 1214 prev_cyl_boundary = 1215 ((partp->dp_start + partp->dp_size) / dos_cylsecs) * dos_cylsecs; 1216 if (prev_cyl_boundary > partp->dp_start) 1217 adj_size = prev_cyl_boundary - partp->dp_start; 1218 else 1219 { 1220 warnx( 1221 "ERROR: could not adjust partition to start on a head boundary\n\ 1222 and end on a cylinder boundary."); 1223 return (0); 1224 } 1225 if (adj_size != partp->dp_size) 1226 { 1227 warnx( 1228 "WARNING: adjusting size of partition %d from %u to %u\n\ 1229 to end on a cylinder boundary", 1230 partition, (u_int)partp->dp_size, (u_int)adj_size); 1231 partp->dp_size = adj_size; 1232 } 1233 if (partp->dp_size == 0) 1234 { 1235 warnx("ERROR line %d: size for partition %d is zero", 1236 current_line_number, partition); 1237 break; 1238 } 1239 1240 dos(partp->dp_start, partp->dp_size, 1241 &partp->dp_scyl, &partp->dp_ssect, &partp->dp_shd); 1242 dos(partp->dp_start+partp->dp_size - 1, partp->dp_size, 1243 &partp->dp_ecyl, &partp->dp_esect, &partp->dp_ehd); 1244 status = 1; 1245 break; 1246 } 1247 return (status); 1248} 1249 1250 1251static int 1252process_active(command) 1253 CMD *command; 1254{ 1255 int status = 0, partition, i; 1256 struct dos_partition *partp; 1257 1258 while (1) 1259 { 1260 active_processed = 1; 1261 if (command->n_args != 1) 1262 { 1263 warnx("ERROR line %d: incorrect number of active args", 1264 current_line_number); 1265 status = 0; 1266 break; 1267 } 1268 partition = command->args[0].arg_val; 1269 if (partition < 1 || partition > 4) 1270 { 1271 warnx("ERROR line %d: invalid partition number %d", 1272 current_line_number, partition); 1273 break; 1274 } 1275 /* 1276 * Reset active partition 1277 */ 1278 partp = ((struct dos_partition *) &mboot.parts); 1279 for (i = 0; i < NDOSPART; i++) 1280 partp[i].dp_flag = 0; 1281 partp[partition-1].dp_flag = ACTIVE; 1282 1283 status = 1; 1284 break; 1285 } 1286 return (status); 1287} 1288 1289 1290static int 1291process_line(line) 1292 char *line; 1293{ 1294 CMD command; 1295 int status = 1; 1296 1297 while (1) 1298 { 1299 parse_config_line(line, &command); 1300 switch (command.cmd) 1301 { 1302 case 0: 1303 /* 1304 * Comment or blank line 1305 */ 1306 break; 1307 case 'g': 1308 /* 1309 * Set geometry 1310 */ 1311 status = process_geometry(&command); 1312 break; 1313 case 'p': 1314 status = process_partition(&command); 1315 break; 1316 case 'a': 1317 status = process_active(&command); 1318 break; 1319 default: 1320 status = 0; 1321 break; 1322 } 1323 break; 1324 } 1325 return (status); 1326} 1327 1328 1329static int 1330read_config(config_file) 1331 char *config_file; 1332{ 1333 FILE *fp = NULL; 1334 int status = 1; 1335 char buf[1010]; 1336 1337 while (1) /* dirty trick used to insure one exit point for this 1338 function */ 1339 { 1340 if (strcmp(config_file, "-") != 0) 1341 { 1342 /* 1343 * We're not reading from stdin 1344 */ 1345 if ((fp = fopen(config_file, "r")) == NULL) 1346 { 1347 status = 0; 1348 break; 1349 } 1350 } 1351 else 1352 { 1353 fp = stdin; 1354 } 1355 current_line_number = 0; 1356 while (!feof(fp)) 1357 { 1358 if (fgets(buf, sizeof(buf), fp) == NULL) 1359 { 1360 break; 1361 } 1362 ++current_line_number; 1363 status = process_line(buf); 1364 if (status == 0) 1365 { 1366 break; 1367 } 1368 } 1369 break; 1370 } 1371 if (fp) 1372 { 1373 /* 1374 * It doesn't matter if we're reading from stdin, as we've reached EOF 1375 */ 1376 fclose(fp); 1377 } 1378 return (status); 1379} 1380 1381 1382static void 1383reset_boot(void) 1384{ 1385 int i; 1386 struct dos_partition *partp; 1387 1388 init_boot(); 1389 for (i = 0; i < 4; ++i) 1390 { 1391 partp = ((struct dos_partition *) &mboot.parts) + i; 1392 bzero((char *)partp, sizeof (struct dos_partition)); 1393 } 1394} 1395 1396static int 1397sanitize_partition(partp) 1398 struct dos_partition *partp; 1399{ 1400 u_int32_t prev_head_boundary, prev_cyl_boundary; 1401 u_int32_t adj_size, max_end; 1402 1403 max_end = partp->dp_start + partp->dp_size; 1404 1405 /* 1406 * Adjust start upwards, if necessary, to fall on an head boundary. 1407 */ 1408 if (partp->dp_start % dos_sectors != 0) { 1409 prev_head_boundary = partp->dp_start / dos_sectors * dos_sectors; 1410 if (max_end < dos_sectors || 1411 prev_head_boundary > max_end - dos_sectors) { 1412 /* 1413 * Can't go past end of partition 1414 */ 1415 warnx( 1416 "ERROR: unable to adjust start of partition to fall on a head boundary"); 1417 return (0); 1418 } 1419 warnx( 1420 "WARNING: adjusting start offset of partition\n\ 1421 to %u to fall on a head boundary", 1422 (u_int)(prev_head_boundary + dos_sectors)); 1423 partp->dp_start = prev_head_boundary + dos_sectors; 1424 } 1425 1426 /* 1427 * Adjust size downwards, if necessary, to fall on a cylinder 1428 * boundary. 1429 */ 1430 prev_cyl_boundary = ((partp->dp_start + partp->dp_size) / dos_cylsecs) * 1431 dos_cylsecs; 1432 if (prev_cyl_boundary > partp->dp_start) 1433 adj_size = prev_cyl_boundary - partp->dp_start; 1434 else 1435 { 1436 warnx("ERROR: could not adjust partition to start on a head boundary\n\ 1437 and end on a cylinder boundary."); 1438 return (0); 1439 } 1440 if (adj_size != partp->dp_size) { 1441 warnx( 1442 "WARNING: adjusting size of partition to %u to end on a\n\ 1443 cylinder boundary", 1444 (u_int)adj_size); 1445 partp->dp_size = adj_size; 1446 } 1447 if (partp->dp_size == 0) { 1448 warnx("ERROR: size for partition is zero"); 1449 return (0); 1450 } 1451 1452 return (1); 1453} 1454