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 #ifndef lint 28 static const char rcsid[] = 29 "$FreeBSD$"; 30 #endif /* not lint */ 31 32 #include <sys/param.h> 33 #include <sys/disklabel.h> 34 #include <sys/stat.h> 35 36 #include <err.h> 37 #include <errno.h> 38 #include <fcntl.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_DRIVE 0x1ba /* offset: setdrv drive */ 49 #define OFF_FLAGS 0x1bb /* offset: option flags */ 50 #define OFF_TICKS 0x1bc /* offset: clock ticks */ 51 #define OFF_PTBL 0x1be /* offset: partition table */ 52 #define OFF_MAGIC 0x1fe /* offset: magic number */ 53 54 #define cv2(p) ((p)[0] | (p)[1] << 010) 55 56 #define mk2(p, x) \ 57 (p)[0] = (u_int8_t)(x), \ 58 (p)[1] = (u_int8_t)((x) >> 010) 59 60 static const struct { 61 const char *tok; 62 int def; 63 } opttbl[] = { 64 {"packet", 0}, 65 {"update", 1}, 66 {"setdrv", 0} 67 }; 68 static const int nopt = sizeof(opttbl) / sizeof(opttbl[0]); 69 70 static const char fmt0[] = "# flag start chs type" 71 " end chs offset size\n"; 72 73 static const char fmt1[] = "%d 0x%02x %4u:%3u:%2u 0x%02x" 74 " %4u:%3u:%2u %10u %10u\n"; 75 76 static int read_mbr(const char *, u_int8_t **, int); 77 static void write_mbr(const char *, int, u_int8_t *, int); 78 static void display_mbr(u_int8_t *); 79 static int boot0version(const u_int8_t *); 80 static int boot0bs(const u_int8_t *); 81 static void stropt(const char *, int *, int *); 82 static char *mkrdev(const char *); 83 static int argtoi(const char *, int, int, int); 84 static void usage(void); 85 86 /* 87 * Boot manager installation/configuration utility. 88 */ 89 int 90 main(int argc, char *argv[]) 91 { 92 u_int8_t *mbr, *boot0; 93 int boot0_size, mbr_size; 94 const char *bpath, *fpath, *disk; 95 int B_flag, v_flag, o_flag; 96 int d_arg, m_arg, t_arg; 97 int o_and, o_or; 98 int up, c; 99 100 bpath = "/boot/boot0"; 101 fpath = NULL; 102 B_flag = v_flag = o_flag = 0; 103 d_arg = m_arg = t_arg = -1; 104 o_and = 0xff; 105 o_or = 0; 106 while ((c = getopt(argc, argv, "Bvb:d:f:m:o:t:")) != -1) 107 switch (c) { 108 case 'B': 109 B_flag = 1; 110 break; 111 case 'v': 112 v_flag = 1; 113 break; 114 case 'b': 115 bpath = optarg; 116 break; 117 case 'd': 118 d_arg = argtoi(optarg, 0, 0xff, 'd'); 119 break; 120 case 'f': 121 fpath = optarg; 122 break; 123 case 'm': 124 m_arg = argtoi(optarg, 0, 0xf, 'm'); 125 break; 126 case 'o': 127 stropt(optarg, &o_and, &o_or); 128 o_flag = 1; 129 break; 130 case 't': 131 t_arg = argtoi(optarg, 1, 0xffff, 't'); 132 break; 133 default: 134 usage(); 135 } 136 argc -= optind; 137 argv += optind; 138 if (argc != 1) 139 usage(); 140 disk = mkrdev(*argv); 141 up = B_flag || d_arg != -1 || m_arg != -1 || o_flag || t_arg != -1; 142 143 /* open the disk and read in the existing mbr */ 144 mbr_size = read_mbr(disk, &mbr, !B_flag); 145 146 /* save the existing MBR if we are asked to do so */ 147 if (fpath) 148 write_mbr(fpath, O_CREAT | O_TRUNC, mbr, mbr_size); 149 150 /* 151 * If we are installing the boot loader, read it from disk and copy the 152 * slice table over from the existing MBR. If not, then point boot0 153 * back at the MBR we just read in. After this, boot0 is the data to 154 * write back to disk if we are going to do a write. 155 */ 156 if (B_flag) { 157 boot0_size = read_mbr(bpath, &boot0, 1); 158 memcpy(boot0 + OFF_PTBL, mbr + OFF_PTBL, 159 sizeof(struct dos_partition) * NDOSPART); 160 } else { 161 boot0 = mbr; 162 boot0_size = mbr_size; 163 } 164 165 /* set the drive */ 166 if (d_arg != -1) 167 boot0[OFF_DRIVE] = d_arg; 168 169 /* set various flags */ 170 if (m_arg != -1) { 171 boot0[OFF_FLAGS] &= 0xf0; 172 boot0[OFF_FLAGS] |= m_arg; 173 } 174 if (o_flag) { 175 boot0[OFF_FLAGS] &= o_and; 176 boot0[OFF_FLAGS] |= o_or; 177 } 178 179 /* set the timeout */ 180 if (t_arg != -1) 181 mk2(boot0 + OFF_TICKS, t_arg); 182 183 /* write the MBR back to disk */ 184 if (up) 185 write_mbr(disk, 0, boot0, boot0_size); 186 187 /* display the MBR */ 188 if (v_flag) 189 display_mbr(boot0); 190 191 /* clean up */ 192 if (mbr != boot0) 193 free(boot0); 194 free(mbr); 195 196 return 0; 197 } 198 199 /* 200 * Read in the MBR of the disk. If it is boot0, then use the version to 201 * read in all of it if necessary. Use pointers to return a malloc'd 202 * buffer containing the MBR and then return its size. 203 */ 204 static int 205 read_mbr(const char *disk, u_int8_t **mbr, int check_version) 206 { 207 u_int8_t buf[MBRSIZE]; 208 int mbr_size, fd; 209 ssize_t n; 210 211 if ((fd = open(disk, O_RDONLY)) == -1) 212 err(1, "%s", disk); 213 if ((n = read(fd, buf, MBRSIZE)) == -1) 214 err(1, "%s", disk); 215 if (n != MBRSIZE) 216 errx(1, "%s: short read", disk); 217 if (cv2(buf + OFF_MAGIC) != 0xaa55) 218 errx(1, "%s: bad magic", disk); 219 220 if (!boot0bs(buf)) { 221 if (check_version) 222 errx(1, "%s: unknown or incompatible boot code", disk); 223 } else if (boot0version(buf) == 0x101) { 224 mbr_size = 1024; 225 if ((*mbr = malloc(mbr_size)) == NULL) 226 errx(1, "%s: unable to allocate read buffer", disk); 227 if (lseek(fd, 0, SEEK_SET) == -1 || 228 (n = read(fd, *mbr, mbr_size)) == -1) 229 err(1, "%s", disk); 230 if (n != mbr_size) 231 errx(1, "%s: short read", disk); 232 return (mbr_size); 233 } 234 *mbr = malloc(sizeof(buf)); 235 memcpy(*mbr, buf, sizeof(buf)); 236 237 return sizeof(buf); 238 } 239 240 /* 241 * Write out the mbr to the specified file. 242 */ 243 static void 244 write_mbr(const char *fname, int flags, u_int8_t *mbr, int mbr_size) 245 { 246 int fd; 247 ssize_t n; 248 249 if ((fd = open(fname, O_WRONLY | flags, 0666)) == -1 || 250 (n = write(fd, mbr, mbr_size)) == -1 || close(fd)) 251 err(1, "%s", fname); 252 if (n != mbr_size) 253 errx(1, "%s: short write", fname); 254 } 255 256 /* 257 * Outputs an informative dump of the data in the MBR to stdout. 258 */ 259 static void 260 display_mbr(u_int8_t *mbr) 261 { 262 struct dos_partition *part; 263 int i, version; 264 265 part = (struct dos_partition *)(mbr + DOSPARTOFF); 266 printf(fmt0); 267 for (i = 0; i < NDOSPART; i++) 268 if (part[i].dp_typ) 269 printf(fmt1, 1 + i, part[i].dp_flag, 270 part[i].dp_scyl + ((part[i].dp_ssect & 0xc0) << 2), 271 part[i].dp_shd, part[i].dp_ssect & 0x3f, part[i].dp_typ, 272 part[i].dp_ecyl + ((part[i].dp_esect & 0xc0) << 2), 273 part[i].dp_ehd, part[i].dp_esect & 0x3f, part[i].dp_start, 274 part[i].dp_size); 275 printf("\n"); 276 version = boot0version(mbr); 277 printf("version=%d.%d drive=0x%x mask=0x%x ticks=%u\noptions=", 278 version >> 8, version & 0xff, mbr[OFF_DRIVE], 279 mbr[OFF_FLAGS] & 0xf, cv2(mbr + OFF_TICKS)); 280 for (i = 0; i < nopt; i++) { 281 if (i) 282 printf(","); 283 if (!(mbr[OFF_FLAGS] & 1 << (7 - i)) ^ opttbl[i].def) 284 printf("no"); 285 printf("%s", opttbl[i].tok); 286 } 287 printf("\n"); 288 } 289 290 /* 291 * Return the boot0 version with the minor revision in the low byte, and 292 * the major revision in the next higher byte. 293 */ 294 static int 295 boot0version(const u_int8_t *bs) 296 { 297 static u_int8_t idold[] = {0xfe, 0x45, 0xf2, 0xe9, 0x00, 0x8a}; 298 299 /* Check for old version, and return 0x100 if found. */ 300 if (memcmp(bs + 0x1c, idold, sizeof(idold)) == 0) 301 return 0x100; 302 303 /* We have a newer boot0, so extract the version number and return it. */ 304 return *(int *)(bs + OFF_VERSION) & 0xffff; 305 } 306 307 /* 308 * Decide if we have valid boot0 boot code by looking for 309 * characteristic byte sequences at fixed offsets. 310 */ 311 static int 312 boot0bs(const u_int8_t *bs) 313 { 314 static u_int8_t id0[] = {0xfc, 0x31, 0xc0, 0x8e, 0xc0, 0x8e, 0xd8, 315 0x8e, 0xd0, 0xbc, 0x00, 0x7c }; 316 static u_int8_t id1[] = {'D', 'r', 'i', 'v', 'e', ' '}; 317 static struct { 318 unsigned off; 319 unsigned len; 320 u_int8_t *key; 321 } ident[2] = { 322 {0x0, sizeof(id0), id0}, 323 {0x1b2, sizeof(id1), id1} 324 }; 325 int i; 326 327 for (i = 0; i < sizeof(ident) / sizeof(ident[0]); i++) 328 if (memcmp(bs + ident[i].off, ident[i].key, ident[i].len)) 329 return 0; 330 return 1; 331 }; 332 333 /* 334 * Adjust "and" and "or" masks for a -o option argument. 335 */ 336 static void 337 stropt(const char *arg, int *xa, int *xo) 338 { 339 const char *q; 340 char *s, *s1; 341 int inv, i, x; 342 343 if (!(s = strdup(arg))) 344 err(1, NULL); 345 for (s1 = s; (q = strtok(s1, ",")); s1 = NULL) { 346 if ((inv = !strncmp(q, "no", 2))) 347 q += 2; 348 for (i = 0; i < nopt; i++) 349 if (!strcmp(q, opttbl[i].tok)) 350 break; 351 if (i == nopt) 352 errx(1, "%s: Unknown -o option", q); 353 if (opttbl[i].def) 354 inv ^= 1; 355 x = 1 << (7 - i); 356 if (inv) 357 *xa &= ~x; 358 else 359 *xo |= x; 360 } 361 free(s); 362 } 363 364 /* 365 * Produce a device path for a "canonical" name, where appropriate. 366 */ 367 static char * 368 mkrdev(const char *fname) 369 { 370 char buf[MAXPATHLEN]; 371 char *s; 372 373 s = (char *)fname; 374 if (!strchr(fname, '/')) { 375 snprintf(buf, sizeof(buf), "%s%s", _PATH_DEV, fname); 376 if (!(s = strdup(buf))) 377 err(1, NULL); 378 } 379 return s; 380 } 381 382 /* 383 * Convert and check an option argument. 384 */ 385 static int 386 argtoi(const char *arg, int lo, int hi, int opt) 387 { 388 char *s; 389 long x; 390 391 errno = 0; 392 x = strtol(arg, &s, 0); 393 if (errno || !*arg || *s || x < lo || x > hi) 394 errx(1, "%s: Bad argument to -%c option", arg, opt); 395 return x; 396 } 397 398 /* 399 * Display usage information. 400 */ 401 static void 402 usage(void) 403 { 404 fprintf(stderr, "%s\n%s\n", 405 "usage: boot0cfg [-Bv] [-b boot0] [-d drive] [-f file] [-m mask]", 406 " [-o options] [-t ticks] disk"); 407 exit(1); 408 } 409