1 /* 2 * Copyright (c) 1999 Robert Nordier 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS 18 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 19 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 20 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 21 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 22 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 23 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 24 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/disklabel.h> 32 #include <sys/diskmbr.h> 33 #include <sys/stat.h> 34 35 #include <err.h> 36 #include <errno.h> 37 #include <fcntl.h> 38 #include <libgeom.h> 39 #include <paths.h> 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <string.h> 43 #include <unistd.h> 44 45 #define MBRSIZE 512 /* master boot record size */ 46 47 #define OFF_VERSION 0x1b0 /* offset: version number */ 48 #define OFF_OPT 0x1b9 /* offset: default boot option */ 49 #define OFF_DRIVE 0x1ba /* offset: setdrv drive */ 50 #define OFF_FLAGS 0x1bb /* offset: option flags */ 51 #define OFF_TICKS 0x1bc /* offset: clock ticks */ 52 #define OFF_PTBL 0x1be /* offset: partition table */ 53 #define OFF_MAGIC 0x1fe /* offset: magic number */ 54 55 #define cv2(p) ((p)[0] | (p)[1] << 010) 56 57 #define mk2(p, x) \ 58 (p)[0] = (u_int8_t)(x), \ 59 (p)[1] = (u_int8_t)((x) >> 010) 60 61 static const struct { 62 const char *tok; 63 int def; 64 } opttbl[] = { 65 {"packet", 0}, 66 {"update", 1}, 67 {"setdrv", 0} 68 }; 69 static const int nopt = sizeof(opttbl) / sizeof(opttbl[0]); 70 71 static const char fmt0[] = "# flag start chs type" 72 " end chs offset size\n"; 73 74 static const char fmt1[] = "%d 0x%02x %4u:%3u:%2u 0x%02x" 75 " %4u:%3u:%2u %10u %10u\n"; 76 77 static int read_mbr(const char *, u_int8_t **, int); 78 static void write_mbr(const char *, int, u_int8_t *, int); 79 static void display_mbr(u_int8_t *); 80 static int boot0version(const u_int8_t *); 81 static int boot0bs(const u_int8_t *); 82 static void stropt(const char *, int *, int *); 83 static char *mkrdev(const char *); 84 static int argtoi(const char *, int, int, int); 85 static void usage(void); 86 87 /* 88 * Boot manager installation/configuration utility. 89 */ 90 int 91 main(int argc, char *argv[]) 92 { 93 u_int8_t *mbr, *boot0; 94 int boot0_size, mbr_size; 95 const char *bpath, *fpath; 96 char *disk; 97 int B_flag, v_flag, o_flag; 98 int d_arg, m_arg, s_arg, t_arg; 99 int o_and, o_or; 100 int up, c; 101 102 bpath = "/boot/boot0"; 103 fpath = NULL; 104 B_flag = v_flag = o_flag = 0; 105 d_arg = m_arg = s_arg = t_arg = -1; 106 o_and = 0xff; 107 o_or = 0; 108 while ((c = getopt(argc, argv, "Bvb:d:f:m:o:s:t:")) != -1) 109 switch (c) { 110 case 'B': 111 B_flag = 1; 112 break; 113 case 'v': 114 v_flag = 1; 115 break; 116 case 'b': 117 bpath = optarg; 118 break; 119 case 'd': 120 d_arg = argtoi(optarg, 0, 0xff, 'd'); 121 break; 122 case 'f': 123 fpath = optarg; 124 break; 125 case 'm': 126 m_arg = argtoi(optarg, 0, 0xf, 'm'); 127 break; 128 case 'o': 129 stropt(optarg, &o_and, &o_or); 130 o_flag = 1; 131 break; 132 case 's': 133 s_arg = argtoi(optarg, 1, 5, 's'); 134 break; 135 case 't': 136 t_arg = argtoi(optarg, 1, 0xffff, 't'); 137 break; 138 default: 139 usage(); 140 } 141 argc -= optind; 142 argv += optind; 143 if (argc != 1) 144 usage(); 145 disk = mkrdev(*argv); 146 up = B_flag || d_arg != -1 || m_arg != -1 || o_flag || s_arg != -1 147 || t_arg != -1; 148 149 /* open the disk and read in the existing mbr */ 150 mbr_size = read_mbr(disk, &mbr, !B_flag); 151 152 /* save the existing MBR if we are asked to do so */ 153 if (fpath) 154 write_mbr(fpath, O_CREAT | O_TRUNC, mbr, mbr_size); 155 156 /* 157 * If we are installing the boot loader, read it from disk and copy the 158 * slice table over from the existing MBR. If not, then point boot0 159 * back at the MBR we just read in. After this, boot0 is the data to 160 * write back to disk if we are going to do a write. 161 */ 162 if (B_flag) { 163 boot0_size = read_mbr(bpath, &boot0, 1); 164 memcpy(boot0 + OFF_PTBL, mbr + OFF_PTBL, 165 sizeof(struct dos_partition) * NDOSPART); 166 } else { 167 boot0 = mbr; 168 boot0_size = mbr_size; 169 } 170 171 /* set the drive */ 172 if (d_arg != -1) 173 boot0[OFF_DRIVE] = d_arg; 174 175 /* set various flags */ 176 if (m_arg != -1) { 177 boot0[OFF_FLAGS] &= 0xf0; 178 boot0[OFF_FLAGS] |= m_arg; 179 } 180 if (o_flag) { 181 boot0[OFF_FLAGS] &= o_and; 182 boot0[OFF_FLAGS] |= o_or; 183 } 184 185 /* set the default boot selection */ 186 if (s_arg != -1) 187 boot0[OFF_OPT] = s_arg - 1; 188 189 /* set the timeout */ 190 if (t_arg != -1) 191 mk2(boot0 + OFF_TICKS, t_arg); 192 193 /* write the MBR back to disk */ 194 if (up) 195 write_mbr(disk, 0, boot0, boot0_size); 196 197 /* display the MBR */ 198 if (v_flag) 199 display_mbr(boot0); 200 201 /* clean up */ 202 if (mbr != boot0) 203 free(boot0); 204 free(mbr); 205 free(disk); 206 207 return 0; 208 } 209 210 /* 211 * Read in the MBR of the disk. If it is boot0, then use the version to 212 * read in all of it if necessary. Use pointers to return a malloc'd 213 * buffer containing the MBR and then return its size. 214 */ 215 static int 216 read_mbr(const char *disk, u_int8_t **mbr, int check_version) 217 { 218 u_int8_t buf[MBRSIZE]; 219 int mbr_size, fd; 220 ssize_t n; 221 222 if ((fd = open(disk, O_RDONLY)) == -1) 223 err(1, "open %s", disk); 224 if ((n = read(fd, buf, MBRSIZE)) == -1) 225 err(1, "read %s", disk); 226 if (n != MBRSIZE) 227 errx(1, "%s: short read", disk); 228 if (cv2(buf + OFF_MAGIC) != 0xaa55) 229 errx(1, "%s: bad magic", disk); 230 231 if (!boot0bs(buf)) { 232 if (check_version) 233 errx(1, "%s: unknown or incompatible boot code", disk); 234 } else if (boot0version(buf) == 0x101) { 235 mbr_size = 1024; 236 if ((*mbr = malloc(mbr_size)) == NULL) 237 errx(1, "%s: unable to allocate read buffer", disk); 238 if (lseek(fd, 0, SEEK_SET) == -1 || 239 (n = read(fd, *mbr, mbr_size)) == -1) 240 err(1, "%s", disk); 241 if (n != mbr_size) 242 errx(1, "%s: short read", disk); 243 return (mbr_size); 244 } 245 *mbr = malloc(sizeof(buf)); 246 memcpy(*mbr, buf, sizeof(buf)); 247 248 return sizeof(buf); 249 } 250 251 /* 252 * Write out the mbr to the specified file. 253 */ 254 static void 255 write_mbr(const char *fname, int flags, u_int8_t *mbr, int mbr_size) 256 { 257 int fd, p; 258 ssize_t n; 259 char *s; 260 const char *q; 261 struct gctl_req *grq; 262 263 fd = open(fname, O_WRONLY | flags, 0666); 264 if (fd != -1) { 265 n = write(fd, mbr, mbr_size); 266 close(fd); 267 if (n != mbr_size) 268 errx(1, "%s: short write", fname); 269 return; 270 } 271 272 if (flags != 0) 273 err(1, "%s", fname); 274 grq = gctl_get_handle(); 275 gctl_ro_param(grq, "verb", -1, "write MBR"); 276 gctl_ro_param(grq, "class", -1, "MBR"); 277 q = strrchr(fname, '/'); 278 if (q == NULL) 279 q = fname; 280 else 281 q++; 282 gctl_ro_param(grq, "geom", -1, q); 283 gctl_ro_param(grq, "data", mbr_size, mbr); 284 q = gctl_issue(grq); 285 if (q == NULL) 286 return; 287 288 warnx("%s: %s", fname, q); 289 gctl_free(grq); 290 291 #ifdef DIOCSMBR 292 for (p = 1; p < 5; p++) { 293 asprintf(&s, "%ss%d", fname, p); 294 fd = open(s, O_RDONLY); 295 if (fd < 0) { 296 free(s); 297 continue; 298 } 299 n = ioctl(fd, DIOCSMBR, (mbr)); 300 if (n != 0) 301 err(1, "%s: ioctl DIOCSMBR", fname); 302 close(fd); 303 free(s); 304 return; 305 } 306 #endif 307 err(1, "write_mbr: %s", fname); 308 } 309 310 /* 311 * Outputs an informative dump of the data in the MBR to stdout. 312 */ 313 static void 314 display_mbr(u_int8_t *mbr) 315 { 316 struct dos_partition *part; 317 int i, version; 318 319 part = (struct dos_partition *)(mbr + DOSPARTOFF); 320 printf(fmt0); 321 for (i = 0; i < NDOSPART; i++) 322 if (part[i].dp_typ) 323 printf(fmt1, 1 + i, part[i].dp_flag, 324 part[i].dp_scyl + ((part[i].dp_ssect & 0xc0) << 2), 325 part[i].dp_shd, part[i].dp_ssect & 0x3f, part[i].dp_typ, 326 part[i].dp_ecyl + ((part[i].dp_esect & 0xc0) << 2), 327 part[i].dp_ehd, part[i].dp_esect & 0x3f, part[i].dp_start, 328 part[i].dp_size); 329 printf("\n"); 330 version = boot0version(mbr); 331 printf("version=%d.%d drive=0x%x mask=0x%x ticks=%u\noptions=", 332 version >> 8, version & 0xff, mbr[OFF_DRIVE], 333 mbr[OFF_FLAGS] & 0xf, cv2(mbr + OFF_TICKS)); 334 for (i = 0; i < nopt; i++) { 335 if (i) 336 printf(","); 337 if (!(mbr[OFF_FLAGS] & 1 << (7 - i)) ^ opttbl[i].def) 338 printf("no"); 339 printf("%s", opttbl[i].tok); 340 } 341 printf("\n"); 342 printf("default_selection=F%d (", mbr[OFF_OPT] + 1); 343 if (mbr[OFF_OPT] < 4) 344 printf("Slice %d", mbr[OFF_OPT] + 1); 345 else 346 printf("Drive 1"); 347 printf(")\n"); 348 } 349 350 /* 351 * Return the boot0 version with the minor revision in the low byte, and 352 * the major revision in the next higher byte. 353 */ 354 static int 355 boot0version(const u_int8_t *bs) 356 { 357 static u_int8_t idold[] = {0xfe, 0x45, 0xf2, 0xe9, 0x00, 0x8a}; 358 359 /* Check for old version, and return 0x100 if found. */ 360 if (memcmp(bs + 0x1c, idold, sizeof(idold)) == 0) 361 return 0x100; 362 363 /* We have a newer boot0, so extract the version number and return it. */ 364 return *(const int *)(bs + OFF_VERSION) & 0xffff; 365 } 366 367 /* 368 * Decide if we have valid boot0 boot code by looking for 369 * characteristic byte sequences at fixed offsets. 370 */ 371 static int 372 boot0bs(const u_int8_t *bs) 373 { 374 static u_int8_t id0[] = {0xfc, 0x31, 0xc0, 0x8e, 0xc0, 0x8e, 0xd8, 375 0x8e, 0xd0, 0xbc, 0x00, 0x7c }; 376 static u_int8_t id1[] = {'D', 'r', 'i', 'v', 'e', ' '}; 377 static struct { 378 unsigned off; 379 unsigned len; 380 u_int8_t *key; 381 } ident[2] = { 382 {0x0, sizeof(id0), id0}, 383 {0x1b2, sizeof(id1), id1} 384 }; 385 unsigned int i; 386 387 for (i = 0; i < sizeof(ident) / sizeof(ident[0]); i++) 388 if (memcmp(bs + ident[i].off, ident[i].key, ident[i].len)) 389 return 0; 390 return 1; 391 } 392 393 /* 394 * Adjust "and" and "or" masks for a -o option argument. 395 */ 396 static void 397 stropt(const char *arg, int *xa, int *xo) 398 { 399 const char *q; 400 char *s, *s1; 401 int inv, i, x; 402 403 if (!(s = strdup(arg))) 404 err(1, NULL); 405 for (s1 = s; (q = strtok(s1, ",")); s1 = NULL) { 406 if ((inv = !strncmp(q, "no", 2))) 407 q += 2; 408 for (i = 0; i < nopt; i++) 409 if (!strcmp(q, opttbl[i].tok)) 410 break; 411 if (i == nopt) 412 errx(1, "%s: Unknown -o option", q); 413 if (opttbl[i].def) 414 inv ^= 1; 415 x = 1 << (7 - i); 416 if (inv) 417 *xa &= ~x; 418 else 419 *xo |= x; 420 } 421 free(s); 422 } 423 424 /* 425 * Produce a device path for a "canonical" name, where appropriate. 426 */ 427 static char * 428 mkrdev(const char *fname) 429 { 430 char buf[MAXPATHLEN]; 431 char *s; 432 433 if (!strchr(fname, '/')) { 434 snprintf(buf, sizeof(buf), "%s%s", _PATH_DEV, fname); 435 s = strdup(buf); 436 } else 437 s = strdup(fname); 438 439 if (s == NULL) 440 errx(1, "No more memory"); 441 return s; 442 } 443 444 /* 445 * Convert and check an option argument. 446 */ 447 static int 448 argtoi(const char *arg, int lo, int hi, int opt) 449 { 450 char *s; 451 long x; 452 453 errno = 0; 454 x = strtol(arg, &s, 0); 455 if (errno || !*arg || *s || x < lo || x > hi) 456 errx(1, "%s: Bad argument to -%c option", arg, opt); 457 return x; 458 } 459 460 /* 461 * Display usage information. 462 */ 463 static void 464 usage(void) 465 { 466 fprintf(stderr, "%s\n%s\n", 467 "usage: boot0cfg [-Bv] [-b boot0] [-d drive] [-f file] [-m mask]", 468 " [-o options] [-s slice] [-t ticks] disk"); 469 exit(1); 470 } 471