| 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 | * $FreeBSD: /repoman/r/ncvs/src/sbin/i386/fdisk/fdisk.c,v 1.36.2.14 2004/01/30 14:40:47 harti Exp $ |
| 27 | * $DragonFly: src/sbin/i386/fdisk/fdisk.c,v 1.16 2008/04/23 22:09:07 thomas Exp $ |
| 28 | */ |
| 29 | |
| 30 | #include <sys/types.h> |
| 31 | #include <sys/diskslice.h> |
| 32 | #include <sys/diskmbr.h> |
| 33 | #include <sys/ioctl_compat.h> |
| 34 | #include <sys/sysctl.h> |
| 35 | #include <sys/stat.h> |
| 36 | #include <ctype.h> |
| 37 | #include <fcntl.h> |
| 38 | #include <err.h> |
| 39 | #include <fstab.h> |
| 40 | #include <errno.h> |
| 41 | #include <stdio.h> |
| 42 | #include <stdlib.h> |
| 43 | #include <string.h> |
| 44 | #include <unistd.h> |
| 45 | |
| 46 | int iotest; |
| 47 | |
| 48 | #define LBUF 100 |
| 49 | static char lbuf[LBUF]; |
| 50 | |
| 51 | #define MBRSIGOFF 510 |
| 52 | |
| 53 | /* |
| 54 | * |
| 55 | * Ported to 386bsd by Julian Elischer Thu Oct 15 20:26:46 PDT 1992 |
| 56 | * |
| 57 | * 14-Dec-89 Robert Baron (rvb) at Carnegie-Mellon University |
| 58 | * Copyright (c) 1989 Robert. V. Baron |
| 59 | * Created. |
| 60 | */ |
| 61 | |
| 62 | #define Decimal(str, ans, tmp) if (decimal(str, &tmp, ans)) ans = tmp |
| 63 | #define Hex(str, ans, tmp) if (hex(str, &tmp, ans)) ans = tmp |
| 64 | #define String(str, ans, len) {char *z = ans; char **dflt = &z; if (string(str, dflt)) strncpy(ans, *dflt, len); } |
| 65 | |
| 66 | #define RoundCyl(x) ((((x) + cylsecs - 1) / cylsecs) * cylsecs) |
| 67 | |
| 68 | #define MAX_SEC_SIZE 2048 /* maximum section size that is supported */ |
| 69 | #define MIN_SEC_SIZE 512 /* the sector size to start sensing at */ |
| 70 | int secsize = 0; /* the sensed sector size */ |
| 71 | |
| 72 | const char *disk; |
| 73 | const char *disks[] = |
| 74 | { |
| 75 | "/dev/ad0", "/dev/da0", "/dev/vkd0", 0 |
| 76 | }; |
| 77 | |
| 78 | int cyls, sectors, heads, cylsecs; |
| 79 | int64_t disksecs; |
| 80 | |
| 81 | struct mboot |
| 82 | { |
| 83 | unsigned char padding[2]; /* force the longs to be long aligned */ |
| 84 | unsigned char *bootinst; /* boot code */ |
| 85 | off_t bootinst_size; |
| 86 | struct dos_partition parts[4]; |
| 87 | }; |
| 88 | struct mboot mboot = {{0}, NULL, 0}; |
| 89 | |
| 90 | #define ACTIVE 0x80 |
| 91 | #define BOOT_MAGIC 0xAA55 |
| 92 | |
| 93 | int dos_cyls; |
| 94 | int dos_heads; |
| 95 | int dos_sectors; |
| 96 | int dos_cylsecs; |
| 97 | |
| 98 | #define DOSSECT(s,c) ((s & 0x3f) | ((c >> 2) & 0xc0)) |
| 99 | #define DOSCYL(c) (c & 0xff) |
| 100 | #define MAXCYL 1023 |
| 101 | static int partition = -1; |
| 102 | |
| 103 | |
| 104 | #define MAX_ARGS 10 |
| 105 | |
| 106 | static int current_line_number; |
| 107 | |
| 108 | static int geom_processed = 0; |
| 109 | static int part_processed = 0; |
| 110 | static int active_processed = 0; |
| 111 | |
| 112 | |
| 113 | typedef struct cmd { |
| 114 | char cmd; |
| 115 | int n_args; |
| 116 | struct arg { |
| 117 | char argtype; |
| 118 | long long arg_val; |
| 119 | } args[MAX_ARGS]; |
| 120 | } CMD; |
| 121 | |
| 122 | |
| 123 | static int B_flag = 0; /* replace boot code */ |
| 124 | static int C_flag = 0; /* use wrapped values for CHS */ |
| 125 | static int E_flag = 0; /* Erase through TRIM */ |
| 126 | static int I_flag = 0; /* use entire disk for DragonFly */ |
| 127 | static int a_flag = 0; /* set active partition */ |
| 128 | static char *b_flag = NULL; /* path to boot code */ |
| 129 | static int i_flag = 0; /* replace partition data */ |
| 130 | static int u_flag = 0; /* update partition data */ |
| 131 | static int p_flag = 0; /* operate on a disk image file */ |
| 132 | static int s_flag = 0; /* Print a summary and exit */ |
| 133 | static int t_flag = 0; /* test only */ |
| 134 | static char *f_flag = NULL; /* Read config info from file */ |
| 135 | static int v_flag = 0; /* Be verbose */ |
| 136 | |
| 137 | struct part_type |
| 138 | { |
| 139 | unsigned char type; |
| 140 | char *name; |
| 141 | }part_types[] = |
| 142 | { |
| 143 | {0x00, "unused"} |
| 144 | ,{0x01, "Primary DOS with 12 bit FAT"} |
| 145 | ,{0x02, "XENIX / filesystem"} |
| 146 | ,{0x03, "XENIX /usr filesystem"} |
| 147 | ,{0x04, "Primary DOS with 16 bit FAT (<= 32MB)"} |
| 148 | ,{0x05, "Extended DOS"} |
| 149 | ,{0x06, "Primary 'big' DOS (> 32MB)"} |
| 150 | ,{0x07, "OS/2 HPFS, NTFS, QNX-2 (16 bit) or Advanced UNIX"} |
| 151 | ,{0x08, "AIX filesystem"} |
| 152 | ,{0x09, "AIX boot partition or Coherent"} |
| 153 | ,{0x0A, "OS/2 Boot Manager or OPUS"} |
| 154 | ,{0x0B, "DOS or Windows 95 with 32 bit FAT"} |
| 155 | ,{0x0C, "DOS or Windows 95 with 32 bit FAT, LBA"} |
| 156 | ,{0x0E, "Primary 'big' DOS (> 32MB, LBA)"} |
| 157 | ,{0x0F, "Extended DOS, LBA"} |
| 158 | ,{0x10, "OPUS"} |
| 159 | ,{0x11, "OS/2 BM: hidden DOS with 12-bit FAT"} |
| 160 | ,{0x12, "Compaq diagnostics"} |
| 161 | ,{0x14, "OS/2 BM: hidden DOS with 16-bit FAT (< 32MB)"} |
| 162 | ,{0x16, "OS/2 BM: hidden DOS with 16-bit FAT (>= 32MB)"} |
| 163 | ,{0x17, "OS/2 BM: hidden IFS (e.g. HPFS)"} |
| 164 | ,{0x18, "AST Windows swapfile"} |
| 165 | ,{0x24, "NEC DOS"} |
| 166 | ,{0x39, "plan9"} |
| 167 | ,{0x3C, "PartitionMagic recovery"} |
| 168 | ,{0x40, "VENIX 286"} |
| 169 | ,{0x41, "Linux/MINIX (sharing disk with DRDOS)"} |
| 170 | ,{0x42, "SFS or Linux swap (sharing disk with DRDOS)"} |
| 171 | ,{0x43, "Linux native (sharing disk with DRDOS)"} |
| 172 | ,{0x4D, "QNX 4.2 Primary"} |
| 173 | ,{0x4E, "QNX 4.2 Secondary"} |
| 174 | ,{0x4F, "QNX 4.2 Tertiary"} |
| 175 | ,{0x50, "DM"} |
| 176 | ,{0x51, "DM"} |
| 177 | ,{0x52, "CP/M or Microport SysV/AT"} |
| 178 | ,{0x53, "DM6 Aux3"} |
| 179 | ,{0x54, "DM6"} |
| 180 | ,{0x55, "EZ-Drive (disk manager)"} |
| 181 | ,{0x56, "GB"} |
| 182 | ,{0x5C, "Priam Edisk (disk manager)"} /* according to S. Widlake */ |
| 183 | ,{0x61, "Speed"} |
| 184 | ,{0x63, "ISC UNIX, other System V/386, GNU HURD or Mach"} |
| 185 | ,{0x64, "Novell Netware 2.xx"} |
| 186 | ,{0x65, "Novell Netware 3.xx"} |
| 187 | ,{0x70, "DiskSecure Multi-Boot"} |
| 188 | ,{0x75, "PCIX"} |
| 189 | ,{0x77, "QNX4.x"} |
| 190 | ,{0x78, "QNX4.x 2nd part"} |
| 191 | ,{0x79, "QNX4.x 3rd part"} |
| 192 | ,{0x80, "Minix 1.1 ... 1.4a"} |
| 193 | ,{0x81, "Minix 1.4b ... 1.5.10"} |
| 194 | ,{0x82, "Linux swap or Solaris x86"} |
| 195 | ,{0x83, "Linux filesystem"} |
| 196 | ,{0x84, "OS/2 hidden C: drive"} |
| 197 | ,{0x85, "Linux extended"} |
| 198 | ,{0x86, "NTFS volume set??"} |
| 199 | ,{0x87, "NTFS volume set??"} |
| 200 | ,{0x93, "Amoeba filesystem"} |
| 201 | ,{0x94, "Amoeba bad block table"} |
| 202 | ,{0x9F, "BSD/OS"} |
| 203 | ,{0xA0, "Suspend to Disk"} |
| 204 | ,{0xA5, "DragonFly/FreeBSD/NetBSD/386BSD"} |
| 205 | ,{0xA6, "OpenBSD"} |
| 206 | ,{0xA7, "NEXTSTEP"} |
| 207 | ,{0xA9, "NetBSD"} |
| 208 | ,{0xAC, "IBM JFS"} |
| 209 | ,{0xB7, "BSDI BSD/386 filesystem"} |
| 210 | ,{0xB8, "BSDI BSD/386 swap"} |
| 211 | ,{0xBE, "Solaris x86 boot"} |
| 212 | ,{0xC1, "DRDOS/sec with 12-bit FAT"} |
| 213 | ,{0xC4, "DRDOS/sec with 16-bit FAT (< 32MB)"} |
| 214 | ,{0xC6, "DRDOS/sec with 16-bit FAT (>= 32MB)"} |
| 215 | ,{0xC7, "Syrinx"} |
| 216 | ,{0xDB, "Concurrent CPM or C.DOS or CTOS"} |
| 217 | ,{0xE1, "Speed"} |
| 218 | ,{0xE3, "Speed"} |
| 219 | ,{0xE4, "Speed"} |
| 220 | ,{0xEB, "BeOS file system"} |
| 221 | ,{0xEE, "EFI GPT"} |
| 222 | ,{0xEF, "EFI System Partition"} |
| 223 | ,{0xF1, "Speed"} |
| 224 | ,{0xF2, "DOS 3.3+ Secondary"} |
| 225 | ,{0xF4, "Speed"} |
| 226 | ,{0xFE, "SpeedStor >1024 cyl. or LANstep"} |
| 227 | ,{0xFF, "BBT (Bad Blocks Table)"} |
| 228 | }; |
| 229 | |
| 230 | static void print_s0(int which); |
| 231 | static void print_part(int i); |
| 232 | static void init_sector0(unsigned long start); |
| 233 | static void init_boot(void); |
| 234 | static void change_part(int i); |
| 235 | static void print_params(); |
| 236 | static void change_active(int which); |
| 237 | static void change_code(); |
| 238 | static void get_params_to_use(); |
| 239 | static void dos(struct dos_partition *partp); |
| 240 | static int open_disk(int u_flag); |
| 241 | static void erase_partition(int i); |
| 242 | static ssize_t read_disk(off_t sector, void *buf); |
| 243 | static ssize_t write_disk(off_t sector, void *buf); |
| 244 | static int get_params(); |
| 245 | static int read_s0(); |
| 246 | static int write_s0(); |
| 247 | static int ok(char *str); |
| 248 | static int decimal(char *str, int *num, int deflt); |
| 249 | static char *get_type(int type); |
| 250 | static int read_config(char *config_file); |
| 251 | static void reset_boot(void); |
| 252 | static int sanitize_partition(struct dos_partition *); |
| 253 | static void usage(void); |
| 254 | #if 0 |
| 255 | static int hex(char *str, int *num, int deflt); |
| 256 | static int string(char *str, char **ans); |
| 257 | #endif |
| 258 | |
| 259 | |
| 260 | int |
| 261 | main(int argc, char *argv[]) |
| 262 | { |
| 263 | int c, i; |
| 264 | |
| 265 | while ((c = getopt(argc, argv, "BCEIab:f:p:istuv1234")) != -1) |
| 266 | switch (c) { |
| 267 | case 'B': |
| 268 | B_flag = 1; |
| 269 | break; |
| 270 | case 'C': |
| 271 | C_flag = 1; |
| 272 | break; |
| 273 | case 'E': |
| 274 | E_flag = 1; |
| 275 | break; |
| 276 | case 'I': |
| 277 | I_flag = 1; |
| 278 | break; |
| 279 | case 'a': |
| 280 | a_flag = 1; |
| 281 | break; |
| 282 | case 'b': |
| 283 | b_flag = optarg; |
| 284 | break; |
| 285 | case 'f': |
| 286 | f_flag = optarg; |
| 287 | break; |
| 288 | case 'p': |
| 289 | disk = optarg; |
| 290 | p_flag = 1; |
| 291 | break; |
| 292 | case 'i': |
| 293 | i_flag = 1; |
| 294 | break; |
| 295 | case 's': |
| 296 | s_flag = 1; |
| 297 | break; |
| 298 | case 't': |
| 299 | t_flag = 1; |
| 300 | break; |
| 301 | case 'u': |
| 302 | u_flag = 1; |
| 303 | break; |
| 304 | case 'v': |
| 305 | v_flag = 1; |
| 306 | break; |
| 307 | case '1': |
| 308 | case '2': |
| 309 | case '3': |
| 310 | case '4': |
| 311 | partition = c - '0'; |
| 312 | break; |
| 313 | default: |
| 314 | usage(); |
| 315 | } |
| 316 | if (f_flag || i_flag) |
| 317 | u_flag = 1; |
| 318 | if (t_flag) |
| 319 | v_flag = 1; |
| 320 | argc -= optind; |
| 321 | argv += optind; |
| 322 | |
| 323 | if (argc > 0) { |
| 324 | disk = getdevpath(argv[0], 0); |
| 325 | if (open_disk(u_flag) < 0) |
| 326 | err(1, "cannot open disk %s", disk); |
| 327 | } else if (disk == NULL) { |
| 328 | int rv = 0; |
| 329 | |
| 330 | for(i = 0; disks[i]; i++) |
| 331 | { |
| 332 | disk = disks[i]; |
| 333 | rv = open_disk(u_flag); |
| 334 | if(rv != -2) break; |
| 335 | } |
| 336 | if(rv < 0) |
| 337 | err(1, "cannot open any disk"); |
| 338 | } else { |
| 339 | if (open_disk(u_flag) < 0) |
| 340 | err(1, "cannot open disk %s", disk); |
| 341 | } |
| 342 | |
| 343 | /* (abu)use mboot.bootinst to probe for the sector size */ |
| 344 | if ((mboot.bootinst = malloc(MAX_SEC_SIZE)) == NULL) |
| 345 | err(1, "cannot allocate buffer to determine disk sector size"); |
| 346 | read_disk(0, mboot.bootinst); |
| 347 | free(mboot.bootinst); |
| 348 | mboot.bootinst = NULL; |
| 349 | |
| 350 | if (s_flag) |
| 351 | { |
| 352 | int i; |
| 353 | struct dos_partition *partp; |
| 354 | |
| 355 | if (read_s0()) |
| 356 | err(1, "read_s0"); |
| 357 | printf("%s: %d cyl %d hd %d sec\n", disk, dos_cyls, dos_heads, |
| 358 | dos_sectors); |
| 359 | printf("Part %11s %11s Type Flags\n", "Start", "Size"); |
| 360 | for (i = 0; i < NDOSPART; i++) { |
| 361 | partp = ((struct dos_partition *) &mboot.parts) + i; |
| 362 | if (partp->dp_start == 0 && partp->dp_size == 0) |
| 363 | continue; |
| 364 | printf("%4d: %11lu %11lu 0x%02x 0x%02x\n", i + 1, |
| 365 | (u_long) partp->dp_start, |
| 366 | (u_long) partp->dp_size, partp->dp_typ, |
| 367 | partp->dp_flag); |
| 368 | } |
| 369 | exit(0); |
| 370 | } |
| 371 | |
| 372 | printf("******* Working on device %s *******\n",disk); |
| 373 | |
| 374 | if (I_flag) |
| 375 | { |
| 376 | struct dos_partition *partp; |
| 377 | |
| 378 | read_s0(); |
| 379 | reset_boot(); |
| 380 | partp = (struct dos_partition *) (&mboot.parts[0]); |
| 381 | partp->dp_typ = DOSPTYP_386BSD; |
| 382 | partp->dp_flag = ACTIVE; |
| 383 | partp->dp_start = dos_sectors; |
| 384 | if (disksecs - dos_sectors > 0xFFFFFFFFU) { |
| 385 | printf("Warning: Ending logical block > 2TB, using max value\n"); |
| 386 | partp->dp_size = 0xFFFFFFFFU; |
| 387 | } else { |
| 388 | partp->dp_size = (disksecs / dos_cylsecs) * |
| 389 | dos_cylsecs - dos_sectors; |
| 390 | } |
| 391 | dos(partp); |
| 392 | if (v_flag) |
| 393 | print_s0(-1); |
| 394 | |
| 395 | if (E_flag) { |
| 396 | /* Trim now if we're using the entire device */ |
| 397 | erase_partition(0); |
| 398 | } |
| 399 | |
| 400 | if (!t_flag) |
| 401 | write_s0(); |
| 402 | exit(0); |
| 403 | } |
| 404 | if (f_flag) |
| 405 | { |
| 406 | if (read_s0() || i_flag) |
| 407 | { |
| 408 | reset_boot(); |
| 409 | } |
| 410 | |
| 411 | if (!read_config(f_flag)) |
| 412 | { |
| 413 | exit(1); |
| 414 | } |
| 415 | if (v_flag) |
| 416 | { |
| 417 | print_s0(-1); |
| 418 | } |
| 419 | if (!t_flag) |
| 420 | { |
| 421 | write_s0(); |
| 422 | } |
| 423 | } |
| 424 | else |
| 425 | { |
| 426 | if(u_flag) |
| 427 | { |
| 428 | get_params_to_use(); |
| 429 | } |
| 430 | else |
| 431 | { |
| 432 | print_params(); |
| 433 | } |
| 434 | |
| 435 | if (read_s0()) |
| 436 | init_sector0(dos_sectors); |
| 437 | |
| 438 | printf("Media sector size is %d\n", secsize); |
| 439 | printf("Warning: BIOS sector numbering starts with sector 1\n"); |
| 440 | printf("Information from DOS bootblock is:\n"); |
| 441 | if (partition == -1) |
| 442 | for (i = 1; i <= NDOSPART; i++) |
| 443 | change_part(i); |
| 444 | else |
| 445 | change_part(partition); |
| 446 | |
| 447 | if (u_flag || a_flag) |
| 448 | change_active(partition); |
| 449 | |
| 450 | if (B_flag) |
| 451 | change_code(); |
| 452 | |
| 453 | if (u_flag || a_flag || B_flag) { |
| 454 | if (!t_flag) { |
| 455 | printf("\nWe haven't changed the partition table yet. "); |
| 456 | printf("This is your last chance.\n"); |
| 457 | } |
| 458 | print_s0(-1); |
| 459 | if (!t_flag) { |
| 460 | if (ok("Should we write new partition table?")) { |
| 461 | if (E_flag && u_flag) { |
| 462 | /* |
| 463 | * Trim now because we've committed to |
| 464 | * updating the partition. |
| 465 | */ |
| 466 | if (partition == -1) |
| 467 | for (i = 0; i < NDOSPART; i++) |
| 468 | erase_partition(i); |
| 469 | else |
| 470 | erase_partition(partition); |
| 471 | } |
| 472 | write_s0(); |
| 473 | } |
| 474 | } |
| 475 | else |
| 476 | { |
| 477 | printf("\n-t flag specified -- partition table not written.\n"); |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | exit(0); |
| 483 | } |
| 484 | |
| 485 | static void |
| 486 | usage(void) |
| 487 | { |
| 488 | fprintf(stderr, "%s%s", |
| 489 | "usage: fdisk [-BCIaistu] [-b bootcode] [-p diskimage] [-1234] [disk]\n", |
| 490 | " fdisk -f configfile [-itv] [disk]\n"); |
| 491 | exit(1); |
| 492 | } |
| 493 | |
| 494 | static void |
| 495 | print_s0(int which) |
| 496 | { |
| 497 | int i; |
| 498 | |
| 499 | print_params(); |
| 500 | printf("Information from DOS bootblock is:\n"); |
| 501 | if (which == -1) |
| 502 | for (i = 1; i <= NDOSPART; i++) |
| 503 | printf("%d: ", i), print_part(i); |
| 504 | else |
| 505 | print_part(which); |
| 506 | } |
| 507 | |
| 508 | static struct dos_partition mtpart = { 0 }; |
| 509 | |
| 510 | static void |
| 511 | print_part(int i) |
| 512 | { |
| 513 | struct dos_partition *partp; |
| 514 | u_int64_t part_mb; |
| 515 | |
| 516 | partp = ((struct dos_partition *) &mboot.parts) + i - 1; |
| 517 | |
| 518 | if (!bcmp(partp, &mtpart, sizeof (struct dos_partition))) { |
| 519 | printf("<UNUSED>\n"); |
| 520 | return; |
| 521 | } |
| 522 | /* |
| 523 | * Be careful not to overflow. |
| 524 | */ |
| 525 | part_mb = partp->dp_size; |
| 526 | part_mb *= secsize; |
| 527 | part_mb /= (1024 * 1024); |
| 528 | printf("sysid %d,(%s)\n", partp->dp_typ, get_type(partp->dp_typ)); |
| 529 | printf(" start %lu, size %lu (%jd Meg), flag %x%s\n", |
| 530 | (u_long)partp->dp_start, |
| 531 | (u_long)partp->dp_size, |
| 532 | (intmax_t)part_mb, |
| 533 | partp->dp_flag, |
| 534 | partp->dp_flag == ACTIVE ? " (active)" : ""); |
| 535 | printf("\tbeg: cyl %d/ head %d/ sector %d;\n\tend: cyl %d/ head %d/ sector %d\n" |
| 536 | ,DPCYL(partp->dp_scyl, partp->dp_ssect) |
| 537 | ,partp->dp_shd |
| 538 | ,DPSECT(partp->dp_ssect) |
| 539 | ,DPCYL(partp->dp_ecyl, partp->dp_esect) |
| 540 | ,partp->dp_ehd |
| 541 | ,DPSECT(partp->dp_esect)); |
| 542 | } |
| 543 | |
| 544 | |
| 545 | static void |
| 546 | init_boot(void) |
| 547 | { |
| 548 | const char *fname; |
| 549 | int fd, n; |
| 550 | struct stat sb; |
| 551 | |
| 552 | fname = b_flag ? b_flag : "/boot/mbr"; |
| 553 | if ((fd = open(fname, O_RDONLY)) == -1 || |
| 554 | fstat(fd, &sb) == -1) |
| 555 | err(1, "%s", fname); |
| 556 | if ((mboot.bootinst_size = sb.st_size) % secsize != 0) |
| 557 | errx(1, "%s: length must be a multiple of sector size", fname); |
| 558 | if (mboot.bootinst != NULL) |
| 559 | free(mboot.bootinst); |
| 560 | if ((mboot.bootinst = malloc(mboot.bootinst_size = sb.st_size)) == NULL) |
| 561 | errx(1, "%s: unable to allocate read buffer", fname); |
| 562 | if ((n = read(fd, mboot.bootinst, mboot.bootinst_size)) == -1 || |
| 563 | close(fd)) |
| 564 | err(1, "%s", fname); |
| 565 | if (n != mboot.bootinst_size) |
| 566 | errx(1, "%s: short read", fname); |
| 567 | } |
| 568 | |
| 569 | |
| 570 | static void |
| 571 | init_sector0(unsigned long start) |
| 572 | { |
| 573 | struct dos_partition *partp = (struct dos_partition *) (&mboot.parts[3]); |
| 574 | |
| 575 | init_boot(); |
| 576 | |
| 577 | partp->dp_typ = DOSPTYP_386BSD; |
| 578 | partp->dp_flag = ACTIVE; |
| 579 | start = ((start + dos_sectors - 1) / dos_sectors) * dos_sectors; |
| 580 | if(start == 0) |
| 581 | start = dos_sectors; |
| 582 | partp->dp_start = start; |
| 583 | if (disksecs - start > 0xFFFFFFFFU) { |
| 584 | printf("Warning: Ending logical block > 2TB, using max value\n"); |
| 585 | partp->dp_size = 0xFFFFFFFFU; |
| 586 | } else { |
| 587 | partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs - start; |
| 588 | } |
| 589 | |
| 590 | dos(partp); |
| 591 | } |
| 592 | |
| 593 | static void |
| 594 | change_part(int i) |
| 595 | { |
| 596 | struct dos_partition *partp = ((struct dos_partition *) &mboot.parts) + i - 1; |
| 597 | |
| 598 | printf("The data for partition %d is:\n", i); |
| 599 | print_part(i); |
| 600 | |
| 601 | if (u_flag && ok("Do you want to change it?")) { |
| 602 | int tmp; |
| 603 | |
| 604 | if (i_flag) { |
| 605 | bzero((char *)partp, sizeof (struct dos_partition)); |
| 606 | if (i == 4) { |
| 607 | init_sector0(1); |
| 608 | printf("\nThe static data for the DOS partition 4 has been reinitialized to:\n"); |
| 609 | print_part(i); |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | do { |
| 614 | Decimal("sysid (165=DragonFly)", partp->dp_typ, tmp); |
| 615 | Decimal("start", partp->dp_start, tmp); |
| 616 | Decimal("size", partp->dp_size, tmp); |
| 617 | if (!sanitize_partition(partp)) { |
| 618 | warnx("ERROR: failed to adjust; setting sysid to 0"); |
| 619 | partp->dp_typ = 0; |
| 620 | } |
| 621 | |
| 622 | if (ok("Explicitly specify beg/end address ?")) |
| 623 | { |
| 624 | int tsec,tcyl,thd; |
| 625 | tcyl = DPCYL(partp->dp_scyl,partp->dp_ssect); |
| 626 | thd = partp->dp_shd; |
| 627 | tsec = DPSECT(partp->dp_ssect); |
| 628 | Decimal("beginning cylinder", tcyl, tmp); |
| 629 | Decimal("beginning head", thd, tmp); |
| 630 | Decimal("beginning sector", tsec, tmp); |
| 631 | if (tcyl > MAXCYL && C_flag == 0) { |
| 632 | printf("Warning: starting cylinder wraps, using all 1's\n"); |
| 633 | partp->dp_scyl = -1; |
| 634 | partp->dp_ssect = -1; |
| 635 | partp->dp_shd = -1; |
| 636 | } else { |
| 637 | partp->dp_scyl = DOSCYL(tcyl); |
| 638 | partp->dp_ssect = DOSSECT(tsec,tcyl); |
| 639 | partp->dp_shd = thd; |
| 640 | } |
| 641 | |
| 642 | tcyl = DPCYL(partp->dp_ecyl,partp->dp_esect); |
| 643 | thd = partp->dp_ehd; |
| 644 | tsec = DPSECT(partp->dp_esect); |
| 645 | Decimal("ending cylinder", tcyl, tmp); |
| 646 | Decimal("ending head", thd, tmp); |
| 647 | Decimal("ending sector", tsec, tmp); |
| 648 | if (tcyl > MAXCYL && C_flag == 0) { |
| 649 | printf("Warning: ending cylinder wraps, using all 1's\n"); |
| 650 | partp->dp_ecyl = -1; |
| 651 | partp->dp_esect = -1; |
| 652 | partp->dp_ehd = -1; |
| 653 | } else { |
| 654 | partp->dp_ecyl = DOSCYL(tcyl); |
| 655 | partp->dp_esect = DOSSECT(tsec,tcyl); |
| 656 | partp->dp_ehd = thd; |
| 657 | } |
| 658 | } else |
| 659 | dos(partp); |
| 660 | |
| 661 | print_part(i); |
| 662 | } while (!ok("Are we happy with this entry?")); |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | static void |
| 667 | print_params(void) |
| 668 | { |
| 669 | printf("parameters extracted from device are:\n"); |
| 670 | printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n" |
| 671 | ,cyls,heads,sectors,cylsecs); |
| 672 | if((dos_sectors > 63) || (dos_cyls > 1023) || (dos_heads > 255)) |
| 673 | printf("Figures below won't work with BIOS for partitions not in cyl 1\n"); |
| 674 | printf("parameters to be used for BIOS calculations are:\n"); |
| 675 | printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n" |
| 676 | ,dos_cyls,dos_heads,dos_sectors,dos_cylsecs); |
| 677 | } |
| 678 | |
| 679 | static void |
| 680 | change_active(int which) |
| 681 | { |
| 682 | struct dos_partition *partp = &mboot.parts[0]; |
| 683 | int active, i, new, tmp; |
| 684 | |
| 685 | active = -1; |
| 686 | for (i = 0; i < NDOSPART; i++) { |
| 687 | if ((partp[i].dp_flag & ACTIVE) == 0) |
| 688 | continue; |
| 689 | printf("Partition %d is marked active\n", i + 1); |
| 690 | if (active == -1) |
| 691 | active = i + 1; |
| 692 | } |
| 693 | if (a_flag && which != -1) |
| 694 | active = which; |
| 695 | else if (active == -1) |
| 696 | active = 1; |
| 697 | |
| 698 | if (!ok("Do you want to change the active partition?")) |
| 699 | return; |
| 700 | setactive: |
| 701 | do { |
| 702 | new = active; |
| 703 | Decimal("active partition", new, tmp); |
| 704 | if (new < 1 || new > 4) { |
| 705 | printf("Active partition number must be in range 1-4." |
| 706 | " Try again.\n"); |
| 707 | goto setactive; |
| 708 | } |
| 709 | active = new; |
| 710 | } while (!ok("Are you happy with this choice")); |
| 711 | for (i = 0; i < NDOSPART; i++) |
| 712 | partp[i].dp_flag = 0; |
| 713 | if (active > 0 && active <= NDOSPART) |
| 714 | partp[active-1].dp_flag = ACTIVE; |
| 715 | } |
| 716 | |
| 717 | static void |
| 718 | change_code(void) |
| 719 | { |
| 720 | if (ok("Do you want to change the boot code?")) |
| 721 | init_boot(); |
| 722 | } |
| 723 | |
| 724 | void |
| 725 | get_params_to_use(void) |
| 726 | { |
| 727 | int tmp; |
| 728 | print_params(); |
| 729 | if (ok("Do you want to change our idea of what BIOS thinks ?")) |
| 730 | { |
| 731 | do |
| 732 | { |
| 733 | Decimal("BIOS's idea of #cylinders", dos_cyls, tmp); |
| 734 | Decimal("BIOS's idea of #heads", dos_heads, tmp); |
| 735 | Decimal("BIOS's idea of #sectors", dos_sectors, tmp); |
| 736 | dos_cylsecs = dos_heads * dos_sectors; |
| 737 | print_params(); |
| 738 | } |
| 739 | while(!ok("Are you happy with this choice")); |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | |
| 744 | /***********************************************\ |
| 745 | * Change real numbers into strange dos numbers * |
| 746 | \***********************************************/ |
| 747 | static void |
| 748 | dos(struct dos_partition *partp) |
| 749 | { |
| 750 | int cy, sec; |
| 751 | u_int32_t end; |
| 752 | |
| 753 | if (partp->dp_typ == 0 && partp->dp_start == 0 && partp->dp_size == 0) { |
| 754 | memcpy(partp, &mtpart, sizeof(*partp)); |
| 755 | return; |
| 756 | } |
| 757 | |
| 758 | /* Start c/h/s. */ |
| 759 | cy = partp->dp_start / dos_cylsecs; |
| 760 | sec = partp->dp_start % dos_sectors + 1; |
| 761 | if (cy > MAXCYL && C_flag == 0) { |
| 762 | printf("Warning: starting cylinder wraps, using all 1's\n"); |
| 763 | partp->dp_shd = -1; |
| 764 | partp->dp_scyl = -1; |
| 765 | partp->dp_ssect = -1; |
| 766 | } else { |
| 767 | partp->dp_shd = partp->dp_start % dos_cylsecs / dos_sectors; |
| 768 | partp->dp_scyl = DOSCYL(cy); |
| 769 | partp->dp_ssect = DOSSECT(sec, cy); |
| 770 | } |
| 771 | |
| 772 | /* End c/h/s. */ |
| 773 | end = partp->dp_start + partp->dp_size - 1; |
| 774 | cy = end / dos_cylsecs; |
| 775 | sec = end % dos_sectors + 1; |
| 776 | if (cy > MAXCYL && C_flag == 0) { |
| 777 | printf("Warning: ending cylinder wraps, using all 1's\n"); |
| 778 | partp->dp_ehd = -1; |
| 779 | partp->dp_ecyl = -1; |
| 780 | partp->dp_esect = -1; |
| 781 | } else { |
| 782 | partp->dp_ehd = end % dos_cylsecs / dos_sectors; |
| 783 | partp->dp_ecyl = DOSCYL(cy); |
| 784 | partp->dp_esect = DOSSECT(sec, cy); |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | int fd; |
| 789 | |
| 790 | static void |
| 791 | erase_partition(int i) |
| 792 | { |
| 793 | struct dos_partition *partp; |
| 794 | off_t ioarg[2]; |
| 795 | |
| 796 | char sysctl_name[64]; |
| 797 | int trim_enabled = 0; |
| 798 | size_t olen = sizeof(trim_enabled); |
| 799 | char *dev_name = strdup(disk); |
| 800 | |
| 801 | dev_name = strtok(dev_name + strlen("/dev/da"),"s"); |
| 802 | sprintf(sysctl_name, "kern.cam.da.%s.trim_enabled", dev_name); |
| 803 | sysctlbyname(sysctl_name, &trim_enabled, &olen, NULL, 0); |
| 804 | if(errno == ENOENT) { |
| 805 | printf("Device:%s does not support the TRIM command\n", disk); |
| 806 | usage(); |
| 807 | } |
| 808 | if(!trim_enabled) { |
| 809 | printf("Erase device option selected, but sysctl (%s) " |
| 810 | "is not enabled\n",sysctl_name); |
| 811 | usage(); |
| 812 | } |
| 813 | partp = ((struct dos_partition *) &mboot.parts) + i; |
| 814 | printf("erase sectors:%u %u\n", |
| 815 | partp->dp_start, |
| 816 | partp->dp_size); |
| 817 | |
| 818 | /* Trim the Device */ |
| 819 | ioarg[0] = partp->dp_start; |
| 820 | ioarg[0] *=secsize; |
| 821 | ioarg[1] = partp->dp_size; |
| 822 | ioarg[1] *=secsize; |
| 823 | |
| 824 | if (ioctl(fd, IOCTLTRIM, ioarg) < 0) { |
| 825 | printf("Device trim failed\n"); |
| 826 | usage (); |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | /* Getting device status */ |
| 831 | |
| 832 | static int |
| 833 | open_disk(int u_flag) |
| 834 | { |
| 835 | struct stat st; |
| 836 | |
| 837 | if (stat(disk, &st) == -1) { |
| 838 | if (errno == ENOENT) |
| 839 | return -2; |
| 840 | warnx("can't get file status of %s", disk); |
| 841 | return -1; |
| 842 | } |
| 843 | if ( !(st.st_mode & S_IFCHR) && p_flag == 0 ) |
| 844 | warnx("device %s is not character special", disk); |
| 845 | if ((fd = open(disk, |
| 846 | a_flag || I_flag || B_flag || u_flag ? O_RDWR : O_RDONLY)) == -1) { |
| 847 | if(errno == ENXIO) |
| 848 | return -2; |
| 849 | warnx("can't open device %s", disk); |
| 850 | return -1; |
| 851 | } |
| 852 | if (get_params() == -1) { |
| 853 | warnx("can't get disk parameters on %s", disk); |
| 854 | return -1; |
| 855 | } |
| 856 | return fd; |
| 857 | } |
| 858 | |
| 859 | static ssize_t |
| 860 | read_disk(off_t sector, void *buf) |
| 861 | { |
| 862 | lseek(fd,(sector * 512), 0); |
| 863 | if( secsize == 0 ) |
| 864 | for( secsize = MIN_SEC_SIZE; secsize <= MAX_SEC_SIZE; secsize *= 2 ) |
| 865 | { |
| 866 | /* try the read */ |
| 867 | int size = read(fd, buf, secsize); |
| 868 | if( size == secsize ) |
| 869 | /* it worked so return */ |
| 870 | return secsize; |
| 871 | } |
| 872 | else |
| 873 | return read( fd, buf, secsize ); |
| 874 | |
| 875 | /* we failed to read at any of the sizes */ |
| 876 | return -1; |
| 877 | } |
| 878 | |
| 879 | static ssize_t |
| 880 | write_disk(off_t sector, void *buf) |
| 881 | { |
| 882 | lseek(fd,(sector * 512), 0); |
| 883 | /* write out in the size that the read_disk found worked */ |
| 884 | return write(fd, buf, secsize); |
| 885 | } |
| 886 | |
| 887 | static int |
| 888 | get_params(void) |
| 889 | { |
| 890 | struct partinfo partinfo; /* disk parameters */ |
| 891 | struct stat st; |
| 892 | |
| 893 | /* |
| 894 | * NOTE: When faking up the CHS for a file image (e.g. for USB), |
| 895 | * we must use max values. If we do not then an overflowed |
| 896 | * cylinder count will, by convention, set the CHS fields to |
| 897 | * all 1's. The heads and sectors in the CHS fields will then |
| 898 | * exceed the basic geometry which can cause BIOSes to brick. |
| 899 | */ |
| 900 | if (ioctl(fd, DIOCGPART, &partinfo) == -1) { |
| 901 | if (p_flag && fstat(fd, &st) == 0 && st.st_size) { |
| 902 | sectors = 63; |
| 903 | heads = 255; |
| 904 | cylsecs = heads * sectors; |
| 905 | cyls = st.st_size / 512 / cylsecs; |
| 906 | } else { |
| 907 | warnx("can't get disk parameters on %s; supplying dummy ones", |
| 908 | disk); |
| 909 | heads = 1; |
| 910 | cylsecs = heads * sectors; |
| 911 | } |
| 912 | } else { |
| 913 | cyls = partinfo.d_ncylinders; |
| 914 | heads = partinfo.d_nheads; |
| 915 | sectors = partinfo.d_secpertrack; |
| 916 | cylsecs = heads * sectors; |
| 917 | secsize = partinfo.media_blksize; |
| 918 | } |
| 919 | dos_cyls = cyls; |
| 920 | dos_heads = heads; |
| 921 | dos_sectors = sectors; |
| 922 | dos_cylsecs = cylsecs; |
| 923 | disksecs = (int64_t)cyls * heads * sectors; |
| 924 | return (disksecs); |
| 925 | } |
| 926 | \f |
| 927 | |
| 928 | static int |
| 929 | read_s0(void) |
| 930 | { |
| 931 | mboot.bootinst_size = secsize; |
| 932 | if (mboot.bootinst != NULL) |
| 933 | free(mboot.bootinst); |
| 934 | if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL) { |
| 935 | warnx("unable to allocate buffer to read fdisk " |
| 936 | "partition table"); |
| 937 | return -1; |
| 938 | } |
| 939 | if (read_disk(0, mboot.bootinst) == -1) { |
| 940 | warnx("can't read fdisk partition table"); |
| 941 | return -1; |
| 942 | } |
| 943 | if (*(uint16_t *)&mboot.bootinst[MBRSIGOFF] != BOOT_MAGIC) { |
| 944 | warnx("invalid fdisk partition table found"); |
| 945 | /* So should we initialize things */ |
| 946 | return -1; |
| 947 | } |
| 948 | memcpy(mboot.parts, &mboot.bootinst[DOSPARTOFF], sizeof(mboot.parts)); |
| 949 | return 0; |
| 950 | } |
| 951 | |
| 952 | static int |
| 953 | write_s0(void) |
| 954 | { |
| 955 | #ifdef NOT_NOW |
| 956 | int flag; |
| 957 | #endif |
| 958 | int sector; |
| 959 | |
| 960 | if (iotest) { |
| 961 | print_s0(-1); |
| 962 | return 0; |
| 963 | } |
| 964 | memcpy(&mboot.bootinst[DOSPARTOFF], mboot.parts, sizeof(mboot.parts)); |
| 965 | /* |
| 966 | * write enable label sector before write (if necessary), |
| 967 | * disable after writing. |
| 968 | * needed if the disklabel protected area also protects |
| 969 | * sector 0. (e.g. empty disk) |
| 970 | */ |
| 971 | #ifdef NOT_NOW |
| 972 | flag = 1; |
| 973 | if (ioctl(fd, DIOCWLABEL, &flag) < 0) |
| 974 | warn("ioctl DIOCWLABEL"); |
| 975 | #endif |
| 976 | for(sector = 0; sector < mboot.bootinst_size / secsize; sector++) |
| 977 | if (write_disk(sector, |
| 978 | &mboot.bootinst[sector * secsize]) == -1) { |
| 979 | warn("can't write fdisk partition table"); |
| 980 | return -1; |
| 981 | #ifdef NOT_NOW |
| 982 | flag = 0; |
| 983 | ioctl(fd, DIOCWLABEL, &flag); |
| 984 | #endif |
| 985 | } |
| 986 | #ifdef NOT_NOW |
| 987 | flag = 0; |
| 988 | ioctl(fd, DIOCWLABEL, &flag); |
| 989 | #endif |
| 990 | return(0); |
| 991 | } |
| 992 | |
| 993 | |
| 994 | static int |
| 995 | ok(char *str) |
| 996 | { |
| 997 | printf("%s [n] ", str); |
| 998 | fflush(stdout); |
| 999 | if (fgets(lbuf, LBUF, stdin) == NULL) |
| 1000 | exit(1); |
| 1001 | lbuf[strlen(lbuf)-1] = 0; |
| 1002 | |
| 1003 | if (*lbuf && |
| 1004 | (!strcmp(lbuf, "yes") || !strcmp(lbuf, "YES") || |
| 1005 | !strcmp(lbuf, "y") || !strcmp(lbuf, "Y"))) |
| 1006 | return 1; |
| 1007 | else |
| 1008 | return 0; |
| 1009 | } |
| 1010 | |
| 1011 | static int |
| 1012 | decimal(char *str, int *num, int deflt) |
| 1013 | { |
| 1014 | int acc = 0, c; |
| 1015 | char *cp; |
| 1016 | |
| 1017 | while (1) { |
| 1018 | printf("Supply a decimal value for \"%s\" [%d] ", str, deflt); |
| 1019 | fflush(stdout); |
| 1020 | if (fgets(lbuf, LBUF, stdin) == NULL) |
| 1021 | exit(1); |
| 1022 | lbuf[strlen(lbuf)-1] = 0; |
| 1023 | |
| 1024 | if (!*lbuf) |
| 1025 | return 0; |
| 1026 | |
| 1027 | cp = lbuf; |
| 1028 | while ((c = *cp) && (c == ' ' || c == '\t')) cp++; |
| 1029 | if (!c) |
| 1030 | return 0; |
| 1031 | while ((c = *cp++)) { |
| 1032 | if (c <= '9' && c >= '0') |
| 1033 | acc = acc * 10 + c - '0'; |
| 1034 | else |
| 1035 | break; |
| 1036 | } |
| 1037 | if (c == ' ' || c == '\t') |
| 1038 | while ((c = *cp) && (c == ' ' || c == '\t')) cp++; |
| 1039 | if (!c) { |
| 1040 | *num = acc; |
| 1041 | return 1; |
| 1042 | } else |
| 1043 | printf("%s is an invalid decimal number. Try again.\n", |
| 1044 | lbuf); |
| 1045 | } |
| 1046 | |
| 1047 | } |
| 1048 | |
| 1049 | #if 0 |
| 1050 | static int |
| 1051 | hex(char *str, int *num, int deflt) |
| 1052 | { |
| 1053 | int acc = 0, c; |
| 1054 | char *cp; |
| 1055 | |
| 1056 | while (1) { |
| 1057 | printf("Supply a hex value for \"%s\" [%x] ", str, deflt); |
| 1058 | fgets(lbuf, LBUF, stdin); |
| 1059 | lbuf[strlen(lbuf)-1] = 0; |
| 1060 | |
| 1061 | if (!*lbuf) |
| 1062 | return 0; |
| 1063 | |
| 1064 | cp = lbuf; |
| 1065 | while ((c = *cp) && (c == ' ' || c == '\t')) cp++; |
| 1066 | if (!c) |
| 1067 | return 0; |
| 1068 | while ((c = *cp++)) { |
| 1069 | if (c <= '9' && c >= '0') |
| 1070 | acc = (acc << 4) + c - '0'; |
| 1071 | else if (c <= 'f' && c >= 'a') |
| 1072 | acc = (acc << 4) + c - 'a' + 10; |
| 1073 | else if (c <= 'F' && c >= 'A') |
| 1074 | acc = (acc << 4) + c - 'A' + 10; |
| 1075 | else |
| 1076 | break; |
| 1077 | } |
| 1078 | if (c == ' ' || c == '\t') |
| 1079 | while ((c = *cp) && (c == ' ' || c == '\t')) cp++; |
| 1080 | if (!c) { |
| 1081 | *num = acc; |
| 1082 | return 1; |
| 1083 | } else |
| 1084 | printf("%s is an invalid hex number. Try again.\n", |
| 1085 | lbuf); |
| 1086 | } |
| 1087 | |
| 1088 | } |
| 1089 | |
| 1090 | static int |
| 1091 | string(char *str, char **ans) |
| 1092 | { |
| 1093 | int c; |
| 1094 | char *cp = lbuf; |
| 1095 | |
| 1096 | while (1) { |
| 1097 | printf("Supply a string value for \"%s\" [%s] ", str, *ans); |
| 1098 | fgets(lbuf, LBUF, stdin); |
| 1099 | lbuf[strlen(lbuf)-1] = 0; |
| 1100 | |
| 1101 | if (!*lbuf) |
| 1102 | return 0; |
| 1103 | |
| 1104 | while ((c = *cp) && (c == ' ' || c == '\t')) cp++; |
| 1105 | if (c == '"') { |
| 1106 | c = *++cp; |
| 1107 | *ans = cp; |
| 1108 | while ((c = *cp) && c != '"') cp++; |
| 1109 | } else { |
| 1110 | *ans = cp; |
| 1111 | while ((c = *cp) && c != ' ' && c != '\t') cp++; |
| 1112 | } |
| 1113 | |
| 1114 | if (c) |
| 1115 | *cp = 0; |
| 1116 | return 1; |
| 1117 | } |
| 1118 | } |
| 1119 | #endif |
| 1120 | |
| 1121 | static char * |
| 1122 | get_type(int type) |
| 1123 | { |
| 1124 | int numentries = (sizeof(part_types)/sizeof(struct part_type)); |
| 1125 | int counter = 0; |
| 1126 | struct part_type *ptr = part_types; |
| 1127 | |
| 1128 | |
| 1129 | while(counter < numentries) |
| 1130 | { |
| 1131 | if(ptr->type == type) |
| 1132 | { |
| 1133 | return(ptr->name); |
| 1134 | } |
| 1135 | ptr++; |
| 1136 | counter++; |
| 1137 | } |
| 1138 | return("unknown"); |
| 1139 | } |
| 1140 | |
| 1141 | |
| 1142 | static void |
| 1143 | parse_config_line(char *line, CMD *command) |
| 1144 | { |
| 1145 | char *cp, *end; |
| 1146 | |
| 1147 | cp = line; |
| 1148 | while (1) /* dirty trick used to insure one exit point for this |
| 1149 | function */ |
| 1150 | { |
| 1151 | memset(command, 0, sizeof(*command)); |
| 1152 | |
| 1153 | while (isspace(*cp)) ++cp; |
| 1154 | if (*cp == '\0' || *cp == '#') |
| 1155 | { |
| 1156 | break; |
| 1157 | } |
| 1158 | command->cmd = *cp++; |
| 1159 | |
| 1160 | /* |
| 1161 | * Parse args |
| 1162 | */ |
| 1163 | while (1) |
| 1164 | { |
| 1165 | while (isspace(*cp)) ++cp; |
| 1166 | if (*cp == '#') |
| 1167 | { |
| 1168 | break; /* found comment */ |
| 1169 | } |
| 1170 | if (isalpha(*cp)) |
| 1171 | { |
| 1172 | command->args[command->n_args].argtype = *cp++; |
| 1173 | } |
| 1174 | if (!isdigit(*cp)) |
| 1175 | { |
| 1176 | break; /* assume end of line */ |
| 1177 | } |
| 1178 | end = NULL; |
| 1179 | command->args[command->n_args].arg_val = strtoll(cp, &end, 0); |
| 1180 | if (cp == end) |
| 1181 | { |
| 1182 | break; /* couldn't parse number */ |
| 1183 | } |
| 1184 | cp = end; |
| 1185 | command->n_args++; |
| 1186 | } |
| 1187 | break; |
| 1188 | } |
| 1189 | } |
| 1190 | |
| 1191 | |
| 1192 | static int |
| 1193 | process_geometry(CMD *command) |
| 1194 | { |
| 1195 | int status = 1, i; |
| 1196 | |
| 1197 | while (1) |
| 1198 | { |
| 1199 | geom_processed = 1; |
| 1200 | if (part_processed) |
| 1201 | { |
| 1202 | warnx( |
| 1203 | "ERROR line %d: the geometry specification line must occur before\n\ |
| 1204 | all partition specifications", |
| 1205 | current_line_number); |
| 1206 | status = 0; |
| 1207 | break; |
| 1208 | } |
| 1209 | if (command->n_args != 3) |
| 1210 | { |
| 1211 | warnx("ERROR line %d: incorrect number of geometry args", |
| 1212 | current_line_number); |
| 1213 | status = 0; |
| 1214 | break; |
| 1215 | } |
| 1216 | dos_cyls = -1; |
| 1217 | dos_heads = -1; |
| 1218 | dos_sectors = -1; |
| 1219 | for (i = 0; i < 3; ++i) |
| 1220 | { |
| 1221 | switch (command->args[i].argtype) |
| 1222 | { |
| 1223 | case 'c': |
| 1224 | dos_cyls = command->args[i].arg_val; |
| 1225 | break; |
| 1226 | case 'h': |
| 1227 | dos_heads = command->args[i].arg_val; |
| 1228 | break; |
| 1229 | case 's': |
| 1230 | dos_sectors = command->args[i].arg_val; |
| 1231 | break; |
| 1232 | default: |
| 1233 | warnx( |
| 1234 | "ERROR line %d: unknown geometry arg type: '%c' (0x%02x)", |
| 1235 | current_line_number, command->args[i].argtype, |
| 1236 | command->args[i].argtype); |
| 1237 | status = 0; |
| 1238 | break; |
| 1239 | } |
| 1240 | } |
| 1241 | if (status == 0) |
| 1242 | { |
| 1243 | break; |
| 1244 | } |
| 1245 | |
| 1246 | dos_cylsecs = dos_heads * dos_sectors; |
| 1247 | |
| 1248 | /* |
| 1249 | * Do sanity checks on parameter values |
| 1250 | */ |
| 1251 | if (dos_cyls < 0) |
| 1252 | { |
| 1253 | warnx("ERROR line %d: number of cylinders not specified", |
| 1254 | current_line_number); |
| 1255 | status = 0; |
| 1256 | } |
| 1257 | if (dos_cyls == 0 || dos_cyls > 1024) |
| 1258 | { |
| 1259 | warnx( |
| 1260 | "WARNING line %d: number of cylinders (%d) may be out-of-range\n\ |
| 1261 | (must be within 1-1024 for normal BIOS operation, unless the entire disk\n\ |
| 1262 | is dedicated to DragonFly)", |
| 1263 | current_line_number, dos_cyls); |
| 1264 | } |
| 1265 | |
| 1266 | if (dos_heads < 0) |
| 1267 | { |
| 1268 | warnx("ERROR line %d: number of heads not specified", |
| 1269 | current_line_number); |
| 1270 | status = 0; |
| 1271 | } |
| 1272 | else if (dos_heads < 1 || dos_heads > 256) |
| 1273 | { |
| 1274 | warnx("ERROR line %d: number of heads must be within (1-256)", |
| 1275 | current_line_number); |
| 1276 | status = 0; |
| 1277 | } |
| 1278 | |
| 1279 | if (dos_sectors < 0) |
| 1280 | { |
| 1281 | warnx("ERROR line %d: number of sectors not specified", |
| 1282 | current_line_number); |
| 1283 | status = 0; |
| 1284 | } |
| 1285 | else if (dos_sectors < 1 || dos_sectors > 63) |
| 1286 | { |
| 1287 | warnx("ERROR line %d: number of sectors must be within (1-63)", |
| 1288 | current_line_number); |
| 1289 | status = 0; |
| 1290 | } |
| 1291 | |
| 1292 | break; |
| 1293 | } |
| 1294 | return (status); |
| 1295 | } |
| 1296 | |
| 1297 | |
| 1298 | static int |
| 1299 | process_partition(CMD *command) |
| 1300 | { |
| 1301 | int status = 0, partition; |
| 1302 | u_int32_t prev_head_boundary, prev_cyl_boundary; |
| 1303 | u_int32_t adj_size, max_end; |
| 1304 | struct dos_partition *partp; |
| 1305 | |
| 1306 | while (1) |
| 1307 | { |
| 1308 | part_processed = 1; |
| 1309 | if (command->n_args != 4) |
| 1310 | { |
| 1311 | warnx("ERROR line %d: incorrect number of partition args", |
| 1312 | current_line_number); |
| 1313 | break; |
| 1314 | } |
| 1315 | partition = command->args[0].arg_val; |
| 1316 | if (partition < 1 || partition > 4) |
| 1317 | { |
| 1318 | warnx("ERROR line %d: invalid partition number %d", |
| 1319 | current_line_number, partition); |
| 1320 | break; |
| 1321 | } |
| 1322 | partp = ((struct dos_partition *) &mboot.parts) + partition - 1; |
| 1323 | bzero((char *)partp, sizeof (struct dos_partition)); |
| 1324 | partp->dp_typ = command->args[1].arg_val; |
| 1325 | partp->dp_start = command->args[2].arg_val; |
| 1326 | partp->dp_size = command->args[3].arg_val; |
| 1327 | max_end = partp->dp_start + partp->dp_size; |
| 1328 | |
| 1329 | if (partp->dp_typ == 0) |
| 1330 | { |
| 1331 | /* |
| 1332 | * Get out, the partition is marked as unused. |
| 1333 | */ |
| 1334 | /* |
| 1335 | * Insure that it's unused. |
| 1336 | */ |
| 1337 | bzero((char *)partp, sizeof (struct dos_partition)); |
| 1338 | status = 1; |
| 1339 | break; |
| 1340 | } |
| 1341 | |
| 1342 | /* |
| 1343 | * Adjust start upwards, if necessary, to fall on an head boundary. |
| 1344 | */ |
| 1345 | if (partp->dp_start % dos_sectors != 0) |
| 1346 | { |
| 1347 | prev_head_boundary = partp->dp_start / dos_sectors * dos_sectors; |
| 1348 | if (max_end < dos_sectors || |
| 1349 | prev_head_boundary > max_end - dos_sectors) |
| 1350 | { |
| 1351 | /* |
| 1352 | * Can't go past end of partition |
| 1353 | */ |
| 1354 | warnx( |
| 1355 | "ERROR line %d: unable to adjust start of partition %d to fall on\n\ |
| 1356 | a head boundary", |
| 1357 | current_line_number, partition); |
| 1358 | break; |
| 1359 | } |
| 1360 | warnx( |
| 1361 | "WARNING: adjusting start offset of partition %d\n\ |
| 1362 | from %u to %u, to fall on a head boundary", |
| 1363 | partition, (u_int)partp->dp_start, |
| 1364 | (u_int)(prev_head_boundary + dos_sectors)); |
| 1365 | partp->dp_start = prev_head_boundary + dos_sectors; |
| 1366 | } |
| 1367 | |
| 1368 | /* |
| 1369 | * Adjust size downwards, if necessary, to fall on a cylinder |
| 1370 | * boundary. |
| 1371 | */ |
| 1372 | prev_cyl_boundary = |
| 1373 | ((partp->dp_start + partp->dp_size) / dos_cylsecs) * dos_cylsecs; |
| 1374 | if (prev_cyl_boundary > partp->dp_start) |
| 1375 | adj_size = prev_cyl_boundary - partp->dp_start; |
| 1376 | else |
| 1377 | { |
| 1378 | warnx( |
| 1379 | "ERROR: could not adjust partition to start on a head boundary\n\ |
| 1380 | and end on a cylinder boundary."); |
| 1381 | return (0); |
| 1382 | } |
| 1383 | if (adj_size != partp->dp_size) |
| 1384 | { |
| 1385 | warnx( |
| 1386 | "WARNING: adjusting size of partition %d from %u to %u\n\ |
| 1387 | to end on a cylinder boundary", |
| 1388 | partition, (u_int)partp->dp_size, (u_int)adj_size); |
| 1389 | partp->dp_size = adj_size; |
| 1390 | } |
| 1391 | if (partp->dp_size == 0) |
| 1392 | { |
| 1393 | warnx("ERROR line %d: size of partition %d is zero", |
| 1394 | current_line_number, partition); |
| 1395 | break; |
| 1396 | } |
| 1397 | |
| 1398 | dos(partp); |
| 1399 | status = 1; |
| 1400 | break; |
| 1401 | } |
| 1402 | return (status); |
| 1403 | } |
| 1404 | |
| 1405 | |
| 1406 | static int |
| 1407 | process_active(CMD *command) |
| 1408 | { |
| 1409 | int status = 0, partition, i; |
| 1410 | struct dos_partition *partp; |
| 1411 | |
| 1412 | while (1) |
| 1413 | { |
| 1414 | active_processed = 1; |
| 1415 | if (command->n_args != 1) |
| 1416 | { |
| 1417 | warnx("ERROR line %d: incorrect number of active args", |
| 1418 | current_line_number); |
| 1419 | status = 0; |
| 1420 | break; |
| 1421 | } |
| 1422 | partition = command->args[0].arg_val; |
| 1423 | if (partition < 1 || partition > 4) |
| 1424 | { |
| 1425 | warnx("ERROR line %d: invalid partition number %d", |
| 1426 | current_line_number, partition); |
| 1427 | break; |
| 1428 | } |
| 1429 | /* |
| 1430 | * Reset active partition |
| 1431 | */ |
| 1432 | partp = ((struct dos_partition *) &mboot.parts); |
| 1433 | for (i = 0; i < NDOSPART; i++) |
| 1434 | partp[i].dp_flag = 0; |
| 1435 | partp[partition-1].dp_flag = ACTIVE; |
| 1436 | |
| 1437 | status = 1; |
| 1438 | break; |
| 1439 | } |
| 1440 | return (status); |
| 1441 | } |
| 1442 | |
| 1443 | |
| 1444 | static int |
| 1445 | process_line(char *line) |
| 1446 | { |
| 1447 | CMD command; |
| 1448 | int status = 1; |
| 1449 | |
| 1450 | while (1) |
| 1451 | { |
| 1452 | parse_config_line(line, &command); |
| 1453 | switch (command.cmd) |
| 1454 | { |
| 1455 | case 0: |
| 1456 | /* |
| 1457 | * Comment or blank line |
| 1458 | */ |
| 1459 | break; |
| 1460 | case 'g': |
| 1461 | /* |
| 1462 | * Set geometry |
| 1463 | */ |
| 1464 | status = process_geometry(&command); |
| 1465 | break; |
| 1466 | case 'p': |
| 1467 | status = process_partition(&command); |
| 1468 | break; |
| 1469 | case 'a': |
| 1470 | status = process_active(&command); |
| 1471 | break; |
| 1472 | default: |
| 1473 | status = 0; |
| 1474 | break; |
| 1475 | } |
| 1476 | break; |
| 1477 | } |
| 1478 | return (status); |
| 1479 | } |
| 1480 | |
| 1481 | |
| 1482 | static int |
| 1483 | read_config(char *config_file) |
| 1484 | { |
| 1485 | FILE *fp = NULL; |
| 1486 | int status = 1; |
| 1487 | char buf[1010]; |
| 1488 | |
| 1489 | while (1) /* dirty trick used to insure one exit point for this |
| 1490 | function */ |
| 1491 | { |
| 1492 | if (strcmp(config_file, "-") != 0) |
| 1493 | { |
| 1494 | /* |
| 1495 | * We're not reading from stdin |
| 1496 | */ |
| 1497 | if ((fp = fopen(config_file, "r")) == NULL) |
| 1498 | { |
| 1499 | status = 0; |
| 1500 | break; |
| 1501 | } |
| 1502 | } |
| 1503 | else |
| 1504 | { |
| 1505 | fp = stdin; |
| 1506 | } |
| 1507 | current_line_number = 0; |
| 1508 | while (!feof(fp)) |
| 1509 | { |
| 1510 | if (fgets(buf, sizeof(buf), fp) == NULL) |
| 1511 | { |
| 1512 | break; |
| 1513 | } |
| 1514 | ++current_line_number; |
| 1515 | status = process_line(buf); |
| 1516 | if (status == 0) |
| 1517 | { |
| 1518 | break; |
| 1519 | } |
| 1520 | } |
| 1521 | break; |
| 1522 | } |
| 1523 | if (fp) |
| 1524 | { |
| 1525 | /* |
| 1526 | * It doesn't matter if we're reading from stdin, as we've reached EOF |
| 1527 | */ |
| 1528 | fclose(fp); |
| 1529 | } |
| 1530 | return (status); |
| 1531 | } |
| 1532 | |
| 1533 | |
| 1534 | static void |
| 1535 | reset_boot(void) |
| 1536 | { |
| 1537 | int i; |
| 1538 | struct dos_partition *partp; |
| 1539 | |
| 1540 | init_boot(); |
| 1541 | for (i = 0; i < 4; ++i) |
| 1542 | { |
| 1543 | partp = ((struct dos_partition *) &mboot.parts) + i; |
| 1544 | bzero((char *)partp, sizeof (struct dos_partition)); |
| 1545 | } |
| 1546 | } |
| 1547 | |
| 1548 | static int |
| 1549 | sanitize_partition(struct dos_partition *partp) |
| 1550 | { |
| 1551 | u_int32_t prev_head_boundary, prev_cyl_boundary; |
| 1552 | u_int32_t max_end, size, start; |
| 1553 | |
| 1554 | start = partp->dp_start; |
| 1555 | size = partp->dp_size; |
| 1556 | max_end = start + size; |
| 1557 | /* Only allow a zero size if the partition is being marked unused. */ |
| 1558 | if (size == 0) { |
| 1559 | if (start == 0 && partp->dp_typ == 0) |
| 1560 | return (1); |
| 1561 | warnx("ERROR: size of partition is zero"); |
| 1562 | return (0); |
| 1563 | } |
| 1564 | /* Return if no adjustment is necessary. */ |
| 1565 | if (start % dos_sectors == 0 && (start + size) % dos_sectors == 0) |
| 1566 | return (1); |
| 1567 | |
| 1568 | if (start == 0) { |
| 1569 | warnx("WARNING: partition overlaps with partition table"); |
| 1570 | if (ok("Correct this automatically?")) |
| 1571 | start = dos_sectors; |
| 1572 | } |
| 1573 | if (start % dos_sectors != 0) |
| 1574 | warnx("WARNING: partition does not start on a head boundary"); |
| 1575 | if ((start +size) % dos_sectors != 0) |
| 1576 | warnx("WARNING: partition does not end on a cylinder boundary"); |
| 1577 | warnx("WARNING: this may confuse the BIOS or some operating systems"); |
| 1578 | if (!ok("Correct this automatically?")) |
| 1579 | return (1); |
| 1580 | |
| 1581 | /* |
| 1582 | * Adjust start upwards, if necessary, to fall on an head boundary. |
| 1583 | */ |
| 1584 | if (start % dos_sectors != 0) { |
| 1585 | prev_head_boundary = start / dos_sectors * dos_sectors; |
| 1586 | if (max_end < dos_sectors || |
| 1587 | prev_head_boundary >= max_end - dos_sectors) { |
| 1588 | /* |
| 1589 | * Can't go past end of partition |
| 1590 | */ |
| 1591 | warnx( |
| 1592 | "ERROR: unable to adjust start of partition to fall on a head boundary"); |
| 1593 | return (0); |
| 1594 | } |
| 1595 | start = prev_head_boundary + dos_sectors; |
| 1596 | } |
| 1597 | |
| 1598 | /* |
| 1599 | * Adjust size downwards, if necessary, to fall on a cylinder |
| 1600 | * boundary. |
| 1601 | */ |
| 1602 | prev_cyl_boundary = ((start + size) / dos_cylsecs) * dos_cylsecs; |
| 1603 | if (prev_cyl_boundary > start) |
| 1604 | size = prev_cyl_boundary - start; |
| 1605 | else { |
| 1606 | warnx("ERROR: could not adjust partition to start on a head boundary\n\ |
| 1607 | and end on a cylinder boundary."); |
| 1608 | return (0); |
| 1609 | } |
| 1610 | |
| 1611 | /* Finally, commit any changes to partp and return. */ |
| 1612 | if (start != partp->dp_start) { |
| 1613 | warnx("WARNING: adjusting start offset of partition to %u", |
| 1614 | (u_int)start); |
| 1615 | partp->dp_start = start; |
| 1616 | } |
| 1617 | if (size != partp->dp_size) { |
| 1618 | warnx("WARNING: adjusting size of partition to %u", (u_int)size); |
| 1619 | partp->dp_size = size; |
| 1620 | } |
| 1621 | |
| 1622 | return (1); |
| 1623 | } |