2 * Copyright (c) 2008 Luigi Rizzo
3 * Copyright (c) 1999 Robert Nordier
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
19 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
20 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
22 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
24 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
25 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/disklabel.h>
33 #include <sys/diskmbr.h>
46 #define MBRSIZE 512 /* master boot record size */
48 #define OFF_VERSION 0x1b0 /* offset: version number, only boot0version */
49 #define OFF_SERIAL 0x1b8 /* offset: volume serial number */
50 #define OFF_PTBL 0x1be /* offset: partition table */
51 #define OFF_MAGIC 0x1fe /* offset: magic number */
53 * Offsets to the parameters of the 512-byte boot block.
54 * For historical reasons they are set as macros
63 struct opt_offsets b0_ofs[] = {
64 { 0x0, 0x0, 0x0, 0x0 }, /* no boot block */
65 { 0x1b9, 0x1ba, 0x1bb, 0x1bc }, /* original block */
66 { 0x1b5, 0x1b6, 0x1b7, 0x1bc }, /* NT_SERIAL block */
69 int b0_ver; /* boot block version set by boot0bs */
71 #define OFF_OPT (b0_ofs[b0_ver].opt) /* default boot option */
72 #define OFF_DRIVE (b0_ofs[b0_ver].drive) /* setdrv drive */
73 #define OFF_FLAGS (b0_ofs[b0_ver].flags) /* option flags */
74 #define OFF_TICKS (b0_ofs[b0_ver].ticks) /* clock ticks */
77 #define cv2(p) ((p)[0] | (p)[1] << 010)
80 (p)[0] = (u_int8_t)(x), \
81 (p)[1] = (u_int8_t)((x) >> 010)
91 static const int nopt = sizeof(opttbl) / sizeof(opttbl[0]);
93 static const char fmt0[] = "# flag start chs type"
94 " end chs offset size\n";
96 static const char fmt1[] = "%d 0x%02x %4u:%3u:%2u 0x%02x"
97 " %4u:%3u:%2u %10u %10u\n";
99 static int read_mbr(const char *, u_int8_t **, int);
100 static void write_mbr(const char *, int, u_int8_t *, int);
101 static void display_mbr(u_int8_t *);
102 static int boot0version(const u_int8_t *);
103 static int boot0bs(const u_int8_t *);
104 static void stropt(const char *, int *, int *);
105 static char *mkrdev(const char *);
106 static int argtoi(const char *, int, int, int);
107 static int set_bell(u_int8_t *, int, int);
108 static void usage(void);
110 unsigned vol_id[5]; /* 4 plus 1 for flag */
114 * Boot manager installation/configuration utility.
117 main(int argc, char *argv[])
119 u_int8_t *mbr, *boot0;
120 int boot0_size, mbr_size;
121 const char *bpath, *fpath;
124 int d_arg, m_arg, s_arg, t_arg;
125 int o_and, o_or, o_e = -1;
128 bpath = "/boot/boot0";
130 B_flag = v_flag = o_flag = 0;
131 d_arg = m_arg = s_arg = t_arg = -1;
134 while ((c = getopt(argc, argv, "Bvb:d:e:f:i:m:o:s:t:")) != -1)
146 d_arg = argtoi(optarg, 0, 0xff, 'd');
149 if (optarg[0] == '0' && optarg[1] == 'x')
150 sscanf(optarg, "0x%02x", &o_e);
158 if (sscanf(optarg, "%02x%02x-%02x%02x",
159 vol_id, vol_id+1, vol_id+2, vol_id+3) == 4)
162 errx(1, "bad argument %s", optarg);
165 m_arg = argtoi(optarg, 0, 0xf, 'm');
168 stropt(optarg, &o_and, &o_or);
172 s_arg = argtoi(optarg, 1, 5, 's');
175 t_arg = argtoi(optarg, 1, 0xffff, 't');
184 disk = mkrdev(*argv);
185 up = B_flag || d_arg != -1 || m_arg != -1 || o_flag || s_arg != -1
188 /* open the disk and read in the existing mbr. Either here or
189 * when reading the block from disk, we do check for the version
190 * and abort if a suitable block is not found.
192 mbr_size = read_mbr(disk, &mbr, !B_flag);
194 /* save the existing MBR if we are asked to do so */
196 write_mbr(fpath, O_CREAT | O_TRUNC, mbr, mbr_size);
199 * If we are installing the boot loader, read it from disk and copy the
200 * slice table over from the existing MBR. If not, then point boot0
201 * back at the MBR we just read in. After this, boot0 is the data to
202 * write back to disk if we are going to do a write.
205 boot0_size = read_mbr(bpath, &boot0, 1);
206 memcpy(boot0 + OFF_PTBL, mbr + OFF_PTBL,
207 sizeof(struct dos_partition) * NDOSPART);
208 if (b0_ver == 2) /* volume serial number support */
209 memcpy(boot0 + OFF_SERIAL, mbr + OFF_SERIAL, 4);
212 boot0_size = mbr_size;
217 boot0[OFF_DRIVE] = d_arg;
219 /* set various flags */
221 boot0[OFF_FLAGS] &= 0xf0;
222 boot0[OFF_FLAGS] |= m_arg;
225 boot0[OFF_FLAGS] &= o_and;
226 boot0[OFF_FLAGS] |= o_or;
229 /* set the default boot selection */
231 boot0[OFF_OPT] = s_arg - 1;
233 /* set the timeout */
235 mk2(boot0 + OFF_TICKS, t_arg);
237 /* set the bell char */
238 if (o_e != -1 && set_bell(boot0, o_e, 0) != -1)
243 errx(1, "incompatible boot block, cannot set volume ID");
244 boot0[OFF_SERIAL] = vol_id[0];
245 boot0[OFF_SERIAL+1] = vol_id[1];
246 boot0[OFF_SERIAL+2] = vol_id[2];
247 boot0[OFF_SERIAL+3] = vol_id[3];
248 up = 1; /* force update */
250 /* write the MBR back to disk */
252 write_mbr(disk, 0, boot0, boot0_size);
254 /* display the MBR */
267 /* get or set the 'bell' character to be used in case of errors.
268 * Lookup for a certain code sequence, return -1 if not found.
271 set_bell(u_int8_t *mbr, int new_bell, int report)
273 /* lookup sequence: 0x100 means skip, 0x200 means done */
274 static unsigned seq[] =
275 { 0xb0, 0x100, 0xe8, 0x100, 0x100, 0x30, 0xe4, 0x200 };
277 for (ofs = 0x60; ofs < 0x180; ofs++) { /* search range */
278 if (mbr[ofs] != seq[0]) /* search initial pattern */
281 if (seq[i] == 0x200) { /* found */
284 mbr[ofs+1] = c = new_bell;
286 printf(" bell=%c (0x%x)",
287 (c >= ' ' && c < 0x7f) ? c : ' ', c);
290 if (seq[i] != 0x100 && seq[i] != mbr[ofs+i])
294 warn("bell not found");
298 * Read in the MBR of the disk. If it is boot0, then use the version to
299 * read in all of it if necessary. Use pointers to return a malloc'd
300 * buffer containing the MBR and then return its size.
303 read_mbr(const char *disk, u_int8_t **mbr, int check_version)
305 u_int8_t buf[MBRSIZE];
310 if ((fd = open(disk, O_RDONLY)) == -1)
311 err(1, "open %s", disk);
312 if ((n = read(fd, buf, MBRSIZE)) == -1)
313 err(1, "read %s", disk);
315 errx(1, "%s: short read", disk);
316 if (cv2(buf + OFF_MAGIC) != 0xaa55)
317 errx(1, "%s: bad magic", disk);
319 if (! (ver = boot0bs(buf))) {
321 errx(1, "%s: unknown or incompatible boot code", disk);
322 } else if (boot0version(buf) == 0x101) {
324 if ((*mbr = malloc(mbr_size)) == NULL)
325 errx(1, "%s: unable to allocate read buffer", disk);
326 if (lseek(fd, 0, SEEK_SET) == -1 ||
327 (n = read(fd, *mbr, mbr_size)) == -1)
330 errx(1, "%s: short read", disk);
333 *mbr = malloc(sizeof(buf));
334 memcpy(*mbr, buf, sizeof(buf));
340 * Write out the mbr to the specified file.
343 write_mbr(const char *fname, int flags, u_int8_t *mbr, int mbr_size)
349 struct gctl_req *grq;
351 fd = open(fname, O_WRONLY | flags, 0666);
353 n = write(fd, mbr, mbr_size);
356 errx(1, "%s: short write", fname);
362 grq = gctl_get_handle();
363 gctl_ro_param(grq, "verb", -1, "write MBR");
364 gctl_ro_param(grq, "class", -1, "MBR");
365 q = strrchr(fname, '/');
370 gctl_ro_param(grq, "geom", -1, q);
371 gctl_ro_param(grq, "data", mbr_size, mbr);
376 warnx("%s: %s", fname, q);
380 for (p = 1; p < 5; p++) {
381 asprintf(&s, "%ss%d", fname, p);
382 fd = open(s, O_RDONLY);
387 n = ioctl(fd, DIOCSMBR, (mbr));
389 err(1, "%s: ioctl DIOCSMBR", fname);
395 err(1, "write_mbr: %s", fname);
399 * Outputs an informative dump of the data in the MBR to stdout.
402 display_mbr(u_int8_t *mbr)
404 struct dos_partition *part;
407 part = (struct dos_partition *)(mbr + DOSPARTOFF);
409 for (i = 0; i < NDOSPART; i++)
411 printf(fmt1, 1 + i, part[i].dp_flag,
412 part[i].dp_scyl + ((part[i].dp_ssect & 0xc0) << 2),
413 part[i].dp_shd, part[i].dp_ssect & 0x3f, part[i].dp_typ,
414 part[i].dp_ecyl + ((part[i].dp_esect & 0xc0) << 2),
415 part[i].dp_ehd, part[i].dp_esect & 0x3f, part[i].dp_start,
418 version = boot0version(mbr);
419 printf("version=%d.%d drive=0x%x mask=0x%x ticks=%u",
420 version >> 8, version & 0xff, mbr[OFF_DRIVE],
421 mbr[OFF_FLAGS] & 0xf, cv2(mbr + OFF_TICKS));
423 printf("\noptions=");
424 for (i = 0; i < nopt; i++) {
427 if (!(mbr[OFF_FLAGS] & 1 << (7 - i)) ^ opttbl[i].def)
429 printf("%s", opttbl[i].tok);
433 printf("volume serial ID %02x%02x-%02x%02x\n",
434 mbr[OFF_SERIAL], mbr[OFF_SERIAL+1],
435 mbr[OFF_SERIAL+2], mbr[OFF_SERIAL+3]);
436 printf("default_selection=F%d (", mbr[OFF_OPT] + 1);
437 if (mbr[OFF_OPT] < 4)
438 printf("Slice %d", mbr[OFF_OPT] + 1);
445 * Return the boot0 version with the minor revision in the low byte, and
446 * the major revision in the next higher byte.
449 boot0version(const u_int8_t *bs)
451 /* Check for old version, and return 0x100 if found. */
456 /* We have a newer boot0, so extract the version number and return it. */
457 return *(const int *)(bs + OFF_VERSION) & 0xffff;
460 /* descriptor of a pattern to match.
461 * Start from the first entry trying to match the chunk of bytes,
462 * if you hit an entry with len=0 terminate the search and report
463 * off as the version. Otherwise skip to the next block after len=0
464 * An entry with len=0, off=0 is the end marker.
466 struct byte_pattern {
473 * Decide if we have valid boot0 boot code by looking for
474 * characteristic byte sequences at fixed offsets.
477 boot0bs(const u_int8_t *bs)
479 /* the initial code sequence */
480 static u_int8_t id0[] = {0xfc, 0x31, 0xc0, 0x8e, 0xc0, 0x8e, 0xd8,
481 0x8e, 0xd0, 0xbc, 0x00, 0x7c };
483 static u_int8_t id1[] = {'D', 'r', 'i', 'v', 'e', ' '};
484 static struct byte_pattern patterns[] = {
485 {0x0, sizeof(id0), id0},
486 {0x1b2, sizeof(id1), id1},
488 {0x0, sizeof(id0), id0}, /* version with NT support */
489 {0x1ae, sizeof(id1), id1},
493 struct byte_pattern *p = patterns;
495 for (; p->off || p->len; p++) {
498 if (!memcmp(bs + p->off, p->key, p->len)) /* match */
500 while (p->len) /* skip to next block */
503 b0_ver = p->off; /* XXX ugly side effect */
508 * Adjust "and" and "or" masks for a -o option argument.
511 stropt(const char *arg, int *xa, int *xo)
517 if (!(s = strdup(arg)))
519 for (s1 = s; (q = strtok(s1, ",")); s1 = NULL) {
520 if ((inv = !strncmp(q, "no", 2)))
522 for (i = 0; i < nopt; i++)
523 if (!strcmp(q, opttbl[i].tok))
526 errx(1, "%s: Unknown -o option", q);
539 * Produce a device path for a "canonical" name, where appropriate.
542 mkrdev(const char *fname)
544 char buf[MAXPATHLEN];
547 if (!strchr(fname, '/')) {
548 snprintf(buf, sizeof(buf), "%s%s", _PATH_DEV, fname);
554 errx(1, "No more memory");
559 * Convert and check an option argument.
562 argtoi(const char *arg, int lo, int hi, int opt)
568 x = strtol(arg, &s, 0);
569 if (errno || !*arg || *s || x < lo || x > hi)
570 errx(1, "%s: Bad argument to -%c option", arg, opt);
575 * Display usage information.
580 fprintf(stderr, "%s\n%s\n",
581 "usage: boot0cfg [-Bv] [-b boot0] [-d drive] [-f file] [-m mask]",
582 " [-o options] [-s slice] [-t ticks] disk");