144196Srnordier/*
2185579Sluigi * Copyright (c) 2008 Luigi Rizzo
344196Srnordier * Copyright (c) 1999 Robert Nordier
444196Srnordier * All rights reserved.
544196Srnordier *
644196Srnordier * Redistribution and use in source and binary forms, with or without
744196Srnordier * modification, are permitted provided that the following conditions
844196Srnordier * are met:
944196Srnordier * 1. Redistributions of source code must retain the above copyright
1044196Srnordier *    notice, this list of conditions and the following disclaimer.
1144196Srnordier * 2. Redistributions in binary form must reproduce the above copyright
1244196Srnordier *    notice, this list of conditions and the following disclaimer in the
1344196Srnordier *    documentation and/or other materials provided with the distribution.
1444196Srnordier *
1544196Srnordier * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS``AS IS'' AND
1644196Srnordier * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1744196Srnordier * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1844196Srnordier * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
1944196Srnordier * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
2044196Srnordier * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
2144196Srnordier * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
2244196Srnordier * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2344196Srnordier * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
2444196Srnordier * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
2544196Srnordier * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2644196Srnordier */
2744196Srnordier
28114601Sobrien#include <sys/cdefs.h>
29114601Sobrien__FBSDID("$FreeBSD: releng/10.3/usr.sbin/boot0cfg/boot0cfg.c 294338 2016-01-19 19:04:56Z jhb $");
3044196Srnordier
3144196Srnordier#include <sys/param.h>
3244196Srnordier#include <sys/disklabel.h>
33104272Sphk#include <sys/diskmbr.h>
3444196Srnordier#include <sys/stat.h>
3544196Srnordier
3644196Srnordier#include <err.h>
3744196Srnordier#include <errno.h>
3844196Srnordier#include <fcntl.h>
39148036Sphk#include <libgeom.h>
4044196Srnordier#include <paths.h>
4144196Srnordier#include <stdio.h>
4244196Srnordier#include <stdlib.h>
4344196Srnordier#include <string.h>
4444196Srnordier#include <unistd.h>
4544196Srnordier
4644196Srnordier#define MBRSIZE         512     /* master boot record size */
4744196Srnordier
48185579Sluigi#define OFF_VERSION	0x1b0	/* offset: version number, only boot0version */
49185579Sluigi#define OFF_SERIAL	0x1b8	/* offset: volume serial number */
5044196Srnordier#define OFF_PTBL        0x1be   /* offset: partition table */
5144196Srnordier#define OFF_MAGIC       0x1fe   /* offset: magic number */
52185579Sluigi/*
53185579Sluigi * Offsets to the parameters of the 512-byte boot block.
54185579Sluigi * For historical reasons they are set as macros
55185579Sluigi */
56185579Sluigistruct opt_offsets {
57185579Sluigi	int opt;
58185579Sluigi	int drive;
59185579Sluigi	int flags;
60185579Sluigi	int ticks;
61185579Sluigi};
6244196Srnordier
63227250Sedstatic struct opt_offsets b0_ofs[] = {
64185579Sluigi	{ 0x0, 0x0, 0x0, 0x0 },		/* no boot block */
65185579Sluigi	{ 0x1b9, 0x1ba, 0x1bb, 0x1bc },	/* original block */
66185579Sluigi	{ 0x1b5, 0x1b6, 0x1b7, 0x1bc },	/* NT_SERIAL block */
67185579Sluigi};
68185579Sluigi
69227250Sedstatic int b0_ver;	/* boot block version set by boot0bs */
70185579Sluigi
71185579Sluigi#define OFF_OPT		(b0_ofs[b0_ver].opt)	/* default boot option */
72185579Sluigi#define OFF_DRIVE	(b0_ofs[b0_ver].drive)	/* setdrv drive */
73185579Sluigi#define OFF_FLAGS       (b0_ofs[b0_ver].flags)	/* option flags */
74185579Sluigi#define OFF_TICKS       (b0_ofs[b0_ver].ticks)	/* clock ticks */
75185579Sluigi
76185579Sluigi
7744196Srnordier#define cv2(p)  ((p)[0] | (p)[1] << 010)
7844196Srnordier
7944196Srnordier#define mk2(p, x)                               \
8044196Srnordier    (p)[0] = (u_int8_t)(x),                     \
8144196Srnordier    (p)[1] = (u_int8_t)((x) >> 010)
8244196Srnordier
8344196Srnordierstatic const struct {
8444196Srnordier    const char *tok;
8544196Srnordier    int def;
8644196Srnordier} opttbl[] = {
87135249Stegge    {"packet", 0},
8844196Srnordier    {"update", 1},
8944196Srnordier    {"setdrv", 0}
9044196Srnordier};
9144196Srnordierstatic const int nopt = sizeof(opttbl) / sizeof(opttbl[0]);
9244196Srnordier
9344196Srnordierstatic const char fmt0[] = "#   flag     start chs   type"
9444196Srnordier    "       end chs       offset         size\n";
9544196Srnordier
9644196Srnordierstatic const char fmt1[] = "%d   0x%02x   %4u:%3u:%2u   0x%02x"
9744196Srnordier    "   %4u:%3u:%2u   %10u   %10u\n";
9844196Srnordier
99227297Saestatic int geom_class_available(const char *);
10063092Sjhbstatic int read_mbr(const char *, u_int8_t **, int);
10163092Sjhbstatic void write_mbr(const char *, int, u_int8_t *, int);
10263092Sjhbstatic void display_mbr(u_int8_t *);
10363034Sjhbstatic int boot0version(const u_int8_t *);
10448113Srnordierstatic int boot0bs(const u_int8_t *);
10544196Srnordierstatic void stropt(const char *, int *, int *);
10644196Srnordierstatic int argtoi(const char *, int, int, int);
107185579Sluigistatic int set_bell(u_int8_t *, int, int);
10844196Srnordierstatic void usage(void);
10944196Srnordier
110227250Sedstatic unsigned vol_id[5];	/* 4 plus 1 for flag */
111185579Sluigi
112227250Sedstatic int v_flag;
11348113Srnordier/*
11448113Srnordier * Boot manager installation/configuration utility.
11548113Srnordier */
11644196Srnordierint
11744196Srnordiermain(int argc, char *argv[])
11844196Srnordier{
11963092Sjhb    u_int8_t *mbr, *boot0;
12063092Sjhb    int boot0_size, mbr_size;
12187643Smikeh    const char *bpath, *fpath;
12287643Smikeh    char *disk;
123185579Sluigi    int B_flag, o_flag;
12464797Sdwmalone    int d_arg, m_arg, s_arg, t_arg;
125185579Sluigi    int o_and, o_or, o_e = -1;
12663092Sjhb    int up, c;
12744196Srnordier
12844196Srnordier    bpath = "/boot/boot0";
12944196Srnordier    fpath = NULL;
13044196Srnordier    B_flag = v_flag = o_flag = 0;
13164797Sdwmalone    d_arg = m_arg = s_arg = t_arg = -1;
13244196Srnordier    o_and = 0xff;
13344196Srnordier    o_or = 0;
134185579Sluigi    while ((c = getopt(argc, argv, "Bvb:d:e:f:i:m:o:s:t:")) != -1)
13544196Srnordier        switch (c) {
13644196Srnordier        case 'B':
13744196Srnordier            B_flag = 1;
13844196Srnordier            break;
13944196Srnordier        case 'v':
14044196Srnordier            v_flag = 1;
14144196Srnordier            break;
14244196Srnordier        case 'b':
14344196Srnordier            bpath = optarg;
14444196Srnordier            break;
14544196Srnordier        case 'd':
14644196Srnordier            d_arg = argtoi(optarg, 0, 0xff, 'd');
14744196Srnordier            break;
148185579Sluigi        case 'e':
149185579Sluigi	    if (optarg[0] == '0' && optarg[1] == 'x')
150185579Sluigi		sscanf(optarg, "0x%02x", &o_e);
151185579Sluigi	    else
152185579Sluigi		o_e = optarg[0];
153185579Sluigi            break;
15444196Srnordier        case 'f':
15544196Srnordier            fpath = optarg;
15644196Srnordier            break;
157185579Sluigi        case 'i':
158185579Sluigi            if (sscanf(optarg, "%02x%02x-%02x%02x",
159185579Sluigi		vol_id, vol_id+1, vol_id+2, vol_id+3) == 4)
160185579Sluigi			vol_id[4] = 1;
161185579Sluigi	    else
162185579Sluigi		errx(1, "bad argument %s", optarg);
163185579Sluigi            break;
16448038Srnordier        case 'm':
16548038Srnordier            m_arg = argtoi(optarg, 0, 0xf, 'm');
16648038Srnordier            break;
16744196Srnordier        case 'o':
16844196Srnordier            stropt(optarg, &o_and, &o_or);
16944196Srnordier            o_flag = 1;
17044196Srnordier            break;
17164797Sdwmalone        case 's':
172228738Sjhb	    if (strcasecmp(optarg, "pxe") == 0)
173228738Sjhb		s_arg = 6;
174228738Sjhb	    else
175228738Sjhb		s_arg = argtoi(optarg, 1, 6, 's');
17664797Sdwmalone            break;
17744196Srnordier        case 't':
17844196Srnordier            t_arg = argtoi(optarg, 1, 0xffff, 't');
17944196Srnordier            break;
18044196Srnordier        default:
18144196Srnordier            usage();
18244196Srnordier        }
18344196Srnordier    argc -= optind;
18444196Srnordier    argv += optind;
18544196Srnordier    if (argc != 1)
18644196Srnordier        usage();
187182844Slulf    disk = g_device_path(*argv);
188182844Slulf    if (disk == NULL)
189182844Slulf        errx(1, "Unable to get providername for %s\n", *argv);
19064797Sdwmalone    up = B_flag || d_arg != -1 || m_arg != -1 || o_flag || s_arg != -1
19164797Sdwmalone	|| t_arg != -1;
19263092Sjhb
193185579Sluigi    /* open the disk and read in the existing mbr. Either here or
194185579Sluigi     * when reading the block from disk, we do check for the version
195185579Sluigi     * and abort if a suitable block is not found.
196185579Sluigi     */
19763092Sjhb    mbr_size = read_mbr(disk, &mbr, !B_flag);
19863092Sjhb
19963092Sjhb    /* save the existing MBR if we are asked to do so */
20063092Sjhb    if (fpath)
20163092Sjhb	write_mbr(fpath, O_CREAT | O_TRUNC, mbr, mbr_size);
20263092Sjhb
20363092Sjhb    /*
20463092Sjhb     * If we are installing the boot loader, read it from disk and copy the
20563092Sjhb     * slice table over from the existing MBR.  If not, then point boot0
20663092Sjhb     * back at the MBR we just read in.  After this, boot0 is the data to
20763092Sjhb     * write back to disk if we are going to do a write.
20863092Sjhb     */
20944196Srnordier    if (B_flag) {
21063092Sjhb	boot0_size = read_mbr(bpath, &boot0, 1);
21163092Sjhb        memcpy(boot0 + OFF_PTBL, mbr + OFF_PTBL,
21263092Sjhb	    sizeof(struct dos_partition) * NDOSPART);
213185579Sluigi	if (b0_ver == 2)	/* volume serial number support */
214185579Sluigi	    memcpy(boot0 + OFF_SERIAL, mbr + OFF_SERIAL, 4);
21563092Sjhb    } else {
21663092Sjhb	boot0 = mbr;
21763092Sjhb	boot0_size = mbr_size;
21844196Srnordier    }
21963092Sjhb
22063092Sjhb    /* set the drive */
22144297Srnordier    if (d_arg != -1)
22263034Sjhb	boot0[OFF_DRIVE] = d_arg;
22363092Sjhb
22463092Sjhb    /* set various flags */
22548038Srnordier    if (m_arg != -1) {
22663034Sjhb	boot0[OFF_FLAGS] &= 0xf0;
22763034Sjhb	boot0[OFF_FLAGS] |= m_arg;
22848038Srnordier    }
22944196Srnordier    if (o_flag) {
23063034Sjhb        boot0[OFF_FLAGS] &= o_and;
23163034Sjhb        boot0[OFF_FLAGS] |= o_or;
23244196Srnordier    }
23363092Sjhb
23464797Sdwmalone    /* set the default boot selection */
23564797Sdwmalone    if (s_arg != -1)
23664797Sdwmalone        boot0[OFF_OPT] = s_arg - 1;
23764797Sdwmalone
23863092Sjhb    /* set the timeout */
23944196Srnordier    if (t_arg != -1)
24063034Sjhb        mk2(boot0 + OFF_TICKS, t_arg);
24163092Sjhb
242185579Sluigi    /* set the bell char */
243185579Sluigi    if (o_e != -1 && set_bell(boot0, o_e, 0) != -1)
244185579Sluigi	up = 1;
245185579Sluigi
246185579Sluigi    if (vol_id[4]) {
247185579Sluigi	if (b0_ver != 2)
248185579Sluigi	    errx(1, "incompatible boot block, cannot set volume ID");
249185579Sluigi	boot0[OFF_SERIAL] = vol_id[0];
250185579Sluigi	boot0[OFF_SERIAL+1] = vol_id[1];
251185579Sluigi	boot0[OFF_SERIAL+2] = vol_id[2];
252185579Sluigi	boot0[OFF_SERIAL+3] = vol_id[3];
253185579Sluigi	up = 1;	/* force update */
254185579Sluigi    }
25563092Sjhb    /* write the MBR back to disk */
25663092Sjhb    if (up)
25763092Sjhb	write_mbr(disk, 0, boot0, boot0_size);
25863092Sjhb
25963092Sjhb    /* display the MBR */
26063092Sjhb    if (v_flag)
26163092Sjhb	display_mbr(boot0);
26263092Sjhb
26363092Sjhb    /* clean up */
26463092Sjhb    if (mbr != boot0)
26563092Sjhb	free(boot0);
26663092Sjhb    free(mbr);
26787643Smikeh    free(disk);
26863092Sjhb
26963092Sjhb    return 0;
27063092Sjhb}
27163092Sjhb
272185579Sluigi/* get or set the 'bell' character to be used in case of errors.
273185579Sluigi * Lookup for a certain code sequence, return -1 if not found.
274185579Sluigi */
275185579Sluigistatic int
276185579Sluigiset_bell(u_int8_t *mbr, int new_bell, int report)
277185579Sluigi{
278185579Sluigi    /* lookup sequence: 0x100 means skip, 0x200 means done */
279185579Sluigi    static unsigned seq[] =
280185579Sluigi		{ 0xb0, 0x100, 0xe8, 0x100, 0x100, 0x30, 0xe4, 0x200 };
281185579Sluigi    int ofs, i, c;
282185579Sluigi    for (ofs = 0x60; ofs < 0x180; ofs++) { /* search range */
283185579Sluigi	if (mbr[ofs] != seq[0])	/* search initial pattern */
284185579Sluigi	    continue;
285185579Sluigi	for (i=0;; i++) {
286185579Sluigi	    if (seq[i] == 0x200) {	/* found */
287185579Sluigi		c = mbr[ofs+1];
288185579Sluigi		if (!report)
289185579Sluigi		    mbr[ofs+1] = c = new_bell;
290185579Sluigi		else
291185579Sluigi		    printf("  bell=%c (0x%x)",
292185579Sluigi			(c >= ' ' && c < 0x7f) ? c : ' ', c);
293185579Sluigi		return c;
294185579Sluigi	    }
295185579Sluigi	    if (seq[i] != 0x100 && seq[i] != mbr[ofs+i])
296185579Sluigi		break;
297185579Sluigi	}
298185579Sluigi    }
299185579Sluigi    warn("bell not found");
300185579Sluigi    return -1;
301185579Sluigi}
30263092Sjhb/*
30363092Sjhb * Read in the MBR of the disk.  If it is boot0, then use the version to
30463092Sjhb * read in all of it if necessary.  Use pointers to return a malloc'd
30563092Sjhb * buffer containing the MBR and then return its size.
30663092Sjhb */
30763092Sjhbstatic int
30863092Sjhbread_mbr(const char *disk, u_int8_t **mbr, int check_version)
30963092Sjhb{
31063092Sjhb    u_int8_t buf[MBRSIZE];
31163092Sjhb    int mbr_size, fd;
312185579Sluigi    int ver;
31363092Sjhb    ssize_t n;
31463092Sjhb
31563092Sjhb    if ((fd = open(disk, O_RDONLY)) == -1)
316108394Sphk        err(1, "open %s", disk);
31763092Sjhb    if ((n = read(fd, buf, MBRSIZE)) == -1)
318108394Sphk        err(1, "read %s", disk);
31963092Sjhb    if (n != MBRSIZE)
32063092Sjhb        errx(1, "%s: short read", disk);
32163092Sjhb    if (cv2(buf + OFF_MAGIC) != 0xaa55)
32263092Sjhb        errx(1, "%s: bad magic", disk);
32363092Sjhb
324185579Sluigi    if (! (ver = boot0bs(buf))) {
32563092Sjhb	if (check_version)
32663092Sjhb	    errx(1, "%s: unknown or incompatible boot code", disk);
32763092Sjhb    } else if (boot0version(buf) == 0x101) {
32863092Sjhb	mbr_size = 1024;
32963092Sjhb	if ((*mbr = malloc(mbr_size)) == NULL)
33063092Sjhb	    errx(1, "%s: unable to allocate read buffer", disk);
33163092Sjhb	if (lseek(fd, 0, SEEK_SET) == -1 ||
33263092Sjhb	    (n = read(fd, *mbr, mbr_size)) == -1)
33363092Sjhb	    err(1, "%s", disk);
33463092Sjhb	if (n != mbr_size)
33563092Sjhb	    errx(1, "%s: short read", disk);
336227876Skevlo	close(fd);
33763092Sjhb	return (mbr_size);
33844196Srnordier    }
339294338Sjhb    if ((*mbr = malloc(sizeof(buf))) == NULL)
340294338Sjhb	errx(1, "%s: unable to allocate MBR buffer", disk);
34163092Sjhb    memcpy(*mbr, buf, sizeof(buf));
342227876Skevlo    close(fd);
34363092Sjhb
34463092Sjhb    return sizeof(buf);
34563092Sjhb}
34663092Sjhb
347227297Saestatic int
348227297Saegeom_class_available(const char *name)
349227297Sae{
350227297Sae	struct gclass *class;
351227297Sae	struct gmesh mesh;
352227297Sae	int error;
353227297Sae
354227297Sae	error = geom_gettree(&mesh);
355227297Sae	if (error != 0)
356227297Sae		errc(1, error, "Cannot get GEOM tree");
357227297Sae
358227297Sae	LIST_FOREACH(class, &mesh.lg_class, lg_class) {
359227297Sae		if (strcmp(class->lg_name, name) == 0) {
360227297Sae			geom_deletetree(&mesh);
361227297Sae			return (1);
362227297Sae		}
363227297Sae	}
364227297Sae
365227297Sae	geom_deletetree(&mesh);
366227297Sae	return (0);
367227297Sae}
368227297Sae
36963092Sjhb/*
37063092Sjhb * Write out the mbr to the specified file.
37163092Sjhb */
37263092Sjhbstatic void
37363092Sjhbwrite_mbr(const char *fname, int flags, u_int8_t *mbr, int mbr_size)
37463092Sjhb{
375227297Sae	struct gctl_req *grq;
376227297Sae	const char *errmsg;
377227297Sae	char *pname;
378227297Sae	ssize_t n;
379227297Sae	int fd;
380148036Sphk
381227297Sae	fd = open(fname, O_WRONLY | flags, 0666);
382227297Sae	if (fd != -1) {
383227297Sae		n = write(fd, mbr, mbr_size);
384227297Sae		close(fd);
385227297Sae		if (n != mbr_size)
386227297Sae			errx(1, "%s: short write", fname);
387227297Sae		return;
388227297Sae	}
389189273Smarcel
390227297Sae	/*
391227297Sae	 * If we're called to write to a backup file, don't try to
392227297Sae	 * write through GEOM.
393227297Sae	 */
394227297Sae	if (flags != 0)
395227297Sae		err(1, "can't open file %s to write backup", fname);
396189273Smarcel
397227297Sae	/* Try open it read only. */
398227297Sae	fd = open(fname, O_RDONLY);
399227297Sae	if (fd == -1) {
400227297Sae		warn("error opening %s", fname);
401227297Sae		return;
402227297Sae	}
403189273Smarcel
404227297Sae	pname = g_providername(fd);
405227297Sae	if (pname == NULL) {
406227297Sae		warn("error getting providername for %s", fname);
407227297Sae		return;
408227297Sae	}
409227297Sae
410227297Sae	/* First check that GEOM_PART is available */
411227297Sae	if (geom_class_available("PART") != 0) {
412227297Sae		grq = gctl_get_handle();
413227297Sae		gctl_ro_param(grq, "class", -1, "PART");
414227297Sae		gctl_ro_param(grq, "arg0", -1, pname);
415227297Sae		gctl_ro_param(grq, "verb", -1, "bootcode");
416227297Sae		gctl_ro_param(grq, "bootcode", mbr_size, mbr);
417227297Sae		gctl_ro_param(grq, "flags", -1, "C");
418227297Sae		errmsg = gctl_issue(grq);
419227297Sae		if (errmsg != NULL && errmsg[0] != '\0')
420227297Sae			errx(1, "GEOM_PART: write bootcode to %s failed: %s",
421227297Sae			    fname, errmsg);
422227297Sae		gctl_free(grq);
423227297Sae	} else if (geom_class_available("MBR") != 0) {
424227297Sae		grq = gctl_get_handle();
425227297Sae		gctl_ro_param(grq, "verb", -1, "write MBR");
426227297Sae		gctl_ro_param(grq, "class", -1, "MBR");
427227297Sae		gctl_ro_param(grq, "geom", -1, pname);
428227297Sae		gctl_ro_param(grq, "data", mbr_size, mbr);
429227297Sae		errmsg = gctl_issue(grq);
430227297Sae		if (errmsg != NULL)
431227297Sae			err(1, "GEOM_MBR: write MBR to %s failed", fname);
432227297Sae		gctl_free(grq);
433227297Sae	} else
434227297Sae		errx(1, "can't write MBR to %s", fname);
435227297Sae	free(pname);
43663092Sjhb}
43763092Sjhb
43863092Sjhb/*
43963092Sjhb * Outputs an informative dump of the data in the MBR to stdout.
44063092Sjhb */
44163092Sjhbstatic void
44263092Sjhbdisplay_mbr(u_int8_t *mbr)
44363092Sjhb{
44463092Sjhb    struct dos_partition *part;
44563092Sjhb    int i, version;
44663092Sjhb
44763092Sjhb    part = (struct dos_partition *)(mbr + DOSPARTOFF);
44863092Sjhb    printf(fmt0);
44963092Sjhb    for (i = 0; i < NDOSPART; i++)
45063092Sjhb	if (part[i].dp_typ)
45163092Sjhb	    printf(fmt1, 1 + i, part[i].dp_flag,
45263092Sjhb		part[i].dp_scyl + ((part[i].dp_ssect & 0xc0) << 2),
45363092Sjhb		part[i].dp_shd, part[i].dp_ssect & 0x3f, part[i].dp_typ,
45463092Sjhb                part[i].dp_ecyl + ((part[i].dp_esect & 0xc0) << 2),
45563092Sjhb                part[i].dp_ehd, part[i].dp_esect & 0x3f, part[i].dp_start,
45663092Sjhb                part[i].dp_size);
45763092Sjhb    printf("\n");
45863092Sjhb    version = boot0version(mbr);
459185579Sluigi    printf("version=%d.%d  drive=0x%x  mask=0x%x  ticks=%u",
46063092Sjhb	version >> 8, version & 0xff, mbr[OFF_DRIVE],
46163092Sjhb	mbr[OFF_FLAGS] & 0xf, cv2(mbr + OFF_TICKS));
462185579Sluigi    set_bell(mbr, 0, 1);
463185579Sluigi    printf("\noptions=");
46463092Sjhb    for (i = 0; i < nopt; i++) {
46563092Sjhb	if (i)
46663092Sjhb	    printf(",");
46763092Sjhb	if (!(mbr[OFF_FLAGS] & 1 << (7 - i)) ^ opttbl[i].def)
46863092Sjhb	    printf("no");
46963092Sjhb	printf("%s", opttbl[i].tok);
47044196Srnordier    }
47163092Sjhb    printf("\n");
472185579Sluigi    if (b0_ver == 2)
473185579Sluigi	printf("volume serial ID %02x%02x-%02x%02x\n",
474185579Sluigi		mbr[OFF_SERIAL], mbr[OFF_SERIAL+1],
475185579Sluigi		mbr[OFF_SERIAL+2], mbr[OFF_SERIAL+3]);
47664797Sdwmalone    printf("default_selection=F%d (", mbr[OFF_OPT] + 1);
47764797Sdwmalone    if (mbr[OFF_OPT] < 4)
47864797Sdwmalone	printf("Slice %d", mbr[OFF_OPT] + 1);
479228738Sjhb    else if (mbr[OFF_OPT] == 4)
480228738Sjhb	printf("Drive 1");
48164797Sdwmalone    else
482228738Sjhb	printf("PXE");
48364797Sdwmalone    printf(")\n");
48444196Srnordier}
48544196Srnordier
48648113Srnordier/*
48763034Sjhb * Return the boot0 version with the minor revision in the low byte, and
48863034Sjhb * the major revision in the next higher byte.
48963034Sjhb */
49063034Sjhbstatic int
49163034Sjhbboot0version(const u_int8_t *bs)
49263034Sjhb{
49363034Sjhb    /* Check for old version, and return 0x100 if found. */
494185579Sluigi    int v = boot0bs(bs);
495185579Sluigi    if (v != 0)
496185579Sluigi        return v << 8;
49763034Sjhb
49863034Sjhb    /* We have a newer boot0, so extract the version number and return it. */
49987643Smikeh    return *(const int *)(bs + OFF_VERSION) & 0xffff;
50063034Sjhb}
50163034Sjhb
502185579Sluigi/* descriptor of a pattern to match.
503185579Sluigi * Start from the first entry trying to match the chunk of bytes,
504185579Sluigi * if you hit an entry with len=0 terminate the search and report
505185579Sluigi * off as the version. Otherwise skip to the next block after len=0
506185579Sluigi * An entry with len=0, off=0 is the end marker.
507185579Sluigi  */
508185579Sluigistruct byte_pattern {
509185579Sluigi    unsigned off;
510185579Sluigi    unsigned len;
511185579Sluigi    u_int8_t *key;
512185579Sluigi};
513185579Sluigi
51463034Sjhb/*
51548113Srnordier * Decide if we have valid boot0 boot code by looking for
51648113Srnordier * characteristic byte sequences at fixed offsets.
51748113Srnordier */
51848113Srnordierstatic int
51948113Srnordierboot0bs(const u_int8_t *bs)
52048113Srnordier{
521185579Sluigi    /* the initial code sequence */
52263034Sjhb    static u_int8_t id0[] = {0xfc, 0x31, 0xc0, 0x8e, 0xc0, 0x8e, 0xd8,
52363034Sjhb			     0x8e, 0xd0, 0xbc, 0x00, 0x7c };
524185579Sluigi    /* the drive id */
52548113Srnordier    static u_int8_t id1[] = {'D', 'r', 'i', 'v', 'e', ' '};
526185579Sluigi    static struct byte_pattern patterns[] = {
52763034Sjhb        {0x0,   sizeof(id0), id0},
528185579Sluigi        {0x1b2, sizeof(id1), id1},
529185579Sluigi        {1, 0, NULL},
530185579Sluigi        {0x0,   sizeof(id0), id0},	/* version with NT support */
531185579Sluigi        {0x1ae, sizeof(id1), id1},
532185579Sluigi        {2, 0, NULL},
533185579Sluigi        {0, 0, NULL},
53448113Srnordier    };
535185579Sluigi    struct byte_pattern *p = patterns;
53648113Srnordier
537185579Sluigi    for (;  p->off || p->len; p++) {
538185579Sluigi	if (p->len == 0)
539185579Sluigi	    break;
540185579Sluigi	if (!memcmp(bs + p->off, p->key, p->len))	/* match */
541185579Sluigi	    continue;
542185579Sluigi	while (p->len)	/* skip to next block */
543185579Sluigi	    p++;
544185579Sluigi    }
545185579Sluigi    b0_ver = p->off;	/* XXX ugly side effect */
546185579Sluigi    return p->off;
547129302Sstefanf}
54848113Srnordier
54948113Srnordier/*
55048113Srnordier * Adjust "and" and "or" masks for a -o option argument.
55148113Srnordier */
55244196Srnordierstatic void
55344196Srnordierstropt(const char *arg, int *xa, int *xo)
55444196Srnordier{
55544196Srnordier    const char *q;
55644196Srnordier    char *s, *s1;
55744196Srnordier    int inv, i, x;
55844196Srnordier
55944196Srnordier    if (!(s = strdup(arg)))
56044196Srnordier        err(1, NULL);
56144196Srnordier    for (s1 = s; (q = strtok(s1, ",")); s1 = NULL) {
56244196Srnordier        if ((inv = !strncmp(q, "no", 2)))
56344196Srnordier            q += 2;
56444196Srnordier        for (i = 0; i < nopt; i++)
56544196Srnordier            if (!strcmp(q, opttbl[i].tok))
56644196Srnordier                break;
56744196Srnordier        if (i == nopt)
56844196Srnordier            errx(1, "%s: Unknown -o option", q);
56944196Srnordier        if (opttbl[i].def)
57044196Srnordier            inv ^= 1;
57144196Srnordier        x = 1 << (7 - i);
57244196Srnordier        if (inv)
57344196Srnordier            *xa &= ~x;
57444196Srnordier        else
57544196Srnordier            *xo |= x;
57644196Srnordier    }
57744196Srnordier    free(s);
57844196Srnordier}
57944196Srnordier
58048113Srnordier/*
58148113Srnordier * Convert and check an option argument.
58248113Srnordier */
58344196Srnordierstatic int
58444196Srnordierargtoi(const char *arg, int lo, int hi, int opt)
58544196Srnordier{
58644196Srnordier    char *s;
58744196Srnordier    long x;
58844196Srnordier
58944196Srnordier    errno = 0;
59044196Srnordier    x = strtol(arg, &s, 0);
59144196Srnordier    if (errno || !*arg || *s || x < lo || x > hi)
59244196Srnordier        errx(1, "%s: Bad argument to -%c option", arg, opt);
59344196Srnordier    return x;
59444196Srnordier}
59544196Srnordier
59648113Srnordier/*
59748113Srnordier * Display usage information.
59848113Srnordier */
59944196Srnordierstatic void
60044196Srnordierusage(void)
60144196Srnordier{
60244196Srnordier    fprintf(stderr, "%s\n%s\n",
60348038Srnordier    "usage: boot0cfg [-Bv] [-b boot0] [-d drive] [-f file] [-m mask]",
60474668Siedowse    "                [-o options] [-s slice] [-t ticks] disk");
60544196Srnordier    exit(1);
60644196Srnordier}
607