144196Srnordier/*
2330449Seadler * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3330449Seadler *
4185579Sluigi * Copyright (c) 2008 Luigi Rizzo
544196Srnordier * Copyright (c) 1999 Robert Nordier
644196Srnordier * All rights reserved.
744196Srnordier *
844196Srnordier * Redistribution and use in source and binary forms, with or without
944196Srnordier * modification, are permitted provided that the following conditions
1044196Srnordier * are met:
1144196Srnordier * 1. Redistributions of source code must retain the above copyright
1244196Srnordier *    notice, this list of conditions and the following disclaimer.
1344196Srnordier * 2. Redistributions in binary form must reproduce the above copyright
1444196Srnordier *    notice, this list of conditions and the following disclaimer in the
1544196Srnordier *    documentation and/or other materials provided with the distribution.
1644196Srnordier *
1744196Srnordier * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS``AS IS'' AND
1844196Srnordier * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1944196Srnordier * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2044196Srnordier * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
2144196Srnordier * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
2244196Srnordier * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
2344196Srnordier * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
2444196Srnordier * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2544196Srnordier * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
2644196Srnordier * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
2744196Srnordier * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2844196Srnordier */
2944196Srnordier
30114601Sobrien#include <sys/cdefs.h>
31114601Sobrien__FBSDID("$FreeBSD: stable/11/usr.sbin/boot0cfg/boot0cfg.c 330449 2018-03-05 07:26:05Z eadler $");
3244196Srnordier
3344196Srnordier#include <sys/param.h>
3444196Srnordier#include <sys/disklabel.h>
35104272Sphk#include <sys/diskmbr.h>
3644196Srnordier#include <sys/stat.h>
3744196Srnordier
3844196Srnordier#include <err.h>
3944196Srnordier#include <errno.h>
4044196Srnordier#include <fcntl.h>
41148036Sphk#include <libgeom.h>
4244196Srnordier#include <paths.h>
4344196Srnordier#include <stdio.h>
4444196Srnordier#include <stdlib.h>
4544196Srnordier#include <string.h>
4644196Srnordier#include <unistd.h>
4744196Srnordier
4844196Srnordier#define MBRSIZE         512     /* master boot record size */
4944196Srnordier
50185579Sluigi#define OFF_VERSION	0x1b0	/* offset: version number, only boot0version */
51185579Sluigi#define OFF_SERIAL	0x1b8	/* offset: volume serial number */
5244196Srnordier#define OFF_PTBL        0x1be   /* offset: partition table */
5344196Srnordier#define OFF_MAGIC       0x1fe   /* offset: magic number */
54185579Sluigi/*
55185579Sluigi * Offsets to the parameters of the 512-byte boot block.
56185579Sluigi * For historical reasons they are set as macros
57185579Sluigi */
58185579Sluigistruct opt_offsets {
59185579Sluigi	int opt;
60185579Sluigi	int drive;
61185579Sluigi	int flags;
62185579Sluigi	int ticks;
63185579Sluigi};
6444196Srnordier
65227250Sedstatic struct opt_offsets b0_ofs[] = {
66185579Sluigi	{ 0x0, 0x0, 0x0, 0x0 },		/* no boot block */
67185579Sluigi	{ 0x1b9, 0x1ba, 0x1bb, 0x1bc },	/* original block */
68185579Sluigi	{ 0x1b5, 0x1b6, 0x1b7, 0x1bc },	/* NT_SERIAL block */
69185579Sluigi};
70185579Sluigi
71227250Sedstatic int b0_ver;	/* boot block version set by boot0bs */
72185579Sluigi
73185579Sluigi#define OFF_OPT		(b0_ofs[b0_ver].opt)	/* default boot option */
74185579Sluigi#define OFF_DRIVE	(b0_ofs[b0_ver].drive)	/* setdrv drive */
75185579Sluigi#define OFF_FLAGS       (b0_ofs[b0_ver].flags)	/* option flags */
76185579Sluigi#define OFF_TICKS       (b0_ofs[b0_ver].ticks)	/* clock ticks */
77185579Sluigi
78185579Sluigi
7944196Srnordier#define cv2(p)  ((p)[0] | (p)[1] << 010)
8044196Srnordier
8144196Srnordier#define mk2(p, x)                               \
8244196Srnordier    (p)[0] = (u_int8_t)(x),                     \
8344196Srnordier    (p)[1] = (u_int8_t)((x) >> 010)
8444196Srnordier
8544196Srnordierstatic const struct {
8644196Srnordier    const char *tok;
8744196Srnordier    int def;
8844196Srnordier} opttbl[] = {
89135249Stegge    {"packet", 0},
9044196Srnordier    {"update", 1},
9144196Srnordier    {"setdrv", 0}
9244196Srnordier};
93317062Saraujostatic const int nopt = nitems(opttbl);
9444196Srnordier
9544196Srnordierstatic const char fmt0[] = "#   flag     start chs   type"
9644196Srnordier    "       end chs       offset         size\n";
9744196Srnordier
9844196Srnordierstatic const char fmt1[] = "%d   0x%02x   %4u:%3u:%2u   0x%02x"
9944196Srnordier    "   %4u:%3u:%2u   %10u   %10u\n";
10044196Srnordier
101227297Saestatic int geom_class_available(const char *);
10263092Sjhbstatic int read_mbr(const char *, u_int8_t **, int);
10363092Sjhbstatic void write_mbr(const char *, int, u_int8_t *, int);
10463092Sjhbstatic void display_mbr(u_int8_t *);
10563034Sjhbstatic int boot0version(const u_int8_t *);
10648113Srnordierstatic int boot0bs(const u_int8_t *);
10744196Srnordierstatic void stropt(const char *, int *, int *);
10844196Srnordierstatic int argtoi(const char *, int, int, int);
109185579Sluigistatic int set_bell(u_int8_t *, int, int);
11044196Srnordierstatic void usage(void);
11144196Srnordier
112227250Sedstatic unsigned vol_id[5];	/* 4 plus 1 for flag */
113185579Sluigi
114227250Sedstatic int v_flag;
11548113Srnordier/*
11648113Srnordier * Boot manager installation/configuration utility.
11748113Srnordier */
11844196Srnordierint
11944196Srnordiermain(int argc, char *argv[])
12044196Srnordier{
12163092Sjhb    u_int8_t *mbr, *boot0;
12263092Sjhb    int boot0_size, mbr_size;
12387643Smikeh    const char *bpath, *fpath;
12487643Smikeh    char *disk;
125185579Sluigi    int B_flag, o_flag;
12664797Sdwmalone    int d_arg, m_arg, s_arg, t_arg;
127185579Sluigi    int o_and, o_or, o_e = -1;
12863092Sjhb    int up, c;
12944196Srnordier
13044196Srnordier    bpath = "/boot/boot0";
13144196Srnordier    fpath = NULL;
13244196Srnordier    B_flag = v_flag = o_flag = 0;
13364797Sdwmalone    d_arg = m_arg = s_arg = t_arg = -1;
13444196Srnordier    o_and = 0xff;
13544196Srnordier    o_or = 0;
136185579Sluigi    while ((c = getopt(argc, argv, "Bvb:d:e:f:i:m:o:s:t:")) != -1)
13744196Srnordier        switch (c) {
13844196Srnordier        case 'B':
13944196Srnordier            B_flag = 1;
14044196Srnordier            break;
14144196Srnordier        case 'v':
14244196Srnordier            v_flag = 1;
14344196Srnordier            break;
14444196Srnordier        case 'b':
14544196Srnordier            bpath = optarg;
14644196Srnordier            break;
14744196Srnordier        case 'd':
14844196Srnordier            d_arg = argtoi(optarg, 0, 0xff, 'd');
14944196Srnordier            break;
150185579Sluigi        case 'e':
151185579Sluigi	    if (optarg[0] == '0' && optarg[1] == 'x')
152185579Sluigi		sscanf(optarg, "0x%02x", &o_e);
153185579Sluigi	    else
154185579Sluigi		o_e = optarg[0];
155185579Sluigi            break;
15644196Srnordier        case 'f':
15744196Srnordier            fpath = optarg;
15844196Srnordier            break;
159185579Sluigi        case 'i':
160185579Sluigi            if (sscanf(optarg, "%02x%02x-%02x%02x",
161185579Sluigi		vol_id, vol_id+1, vol_id+2, vol_id+3) == 4)
162185579Sluigi			vol_id[4] = 1;
163185579Sluigi	    else
164185579Sluigi		errx(1, "bad argument %s", optarg);
165185579Sluigi            break;
16648038Srnordier        case 'm':
16748038Srnordier            m_arg = argtoi(optarg, 0, 0xf, 'm');
16848038Srnordier            break;
16944196Srnordier        case 'o':
17044196Srnordier            stropt(optarg, &o_and, &o_or);
17144196Srnordier            o_flag = 1;
17244196Srnordier            break;
17364797Sdwmalone        case 's':
174228738Sjhb	    if (strcasecmp(optarg, "pxe") == 0)
175228738Sjhb		s_arg = 6;
176228738Sjhb	    else
177228738Sjhb		s_arg = argtoi(optarg, 1, 6, 's');
17864797Sdwmalone            break;
17944196Srnordier        case 't':
18044196Srnordier            t_arg = argtoi(optarg, 1, 0xffff, 't');
18144196Srnordier            break;
18244196Srnordier        default:
18344196Srnordier            usage();
18444196Srnordier        }
18544196Srnordier    argc -= optind;
18644196Srnordier    argv += optind;
18744196Srnordier    if (argc != 1)
18844196Srnordier        usage();
189182844Slulf    disk = g_device_path(*argv);
190182844Slulf    if (disk == NULL)
191182844Slulf        errx(1, "Unable to get providername for %s\n", *argv);
19264797Sdwmalone    up = B_flag || d_arg != -1 || m_arg != -1 || o_flag || s_arg != -1
19364797Sdwmalone	|| t_arg != -1;
19463092Sjhb
195185579Sluigi    /* open the disk and read in the existing mbr. Either here or
196185579Sluigi     * when reading the block from disk, we do check for the version
197185579Sluigi     * and abort if a suitable block is not found.
198185579Sluigi     */
19963092Sjhb    mbr_size = read_mbr(disk, &mbr, !B_flag);
20063092Sjhb
20163092Sjhb    /* save the existing MBR if we are asked to do so */
20263092Sjhb    if (fpath)
20363092Sjhb	write_mbr(fpath, O_CREAT | O_TRUNC, mbr, mbr_size);
20463092Sjhb
20563092Sjhb    /*
20663092Sjhb     * If we are installing the boot loader, read it from disk and copy the
20763092Sjhb     * slice table over from the existing MBR.  If not, then point boot0
20863092Sjhb     * back at the MBR we just read in.  After this, boot0 is the data to
20963092Sjhb     * write back to disk if we are going to do a write.
21063092Sjhb     */
21144196Srnordier    if (B_flag) {
21263092Sjhb	boot0_size = read_mbr(bpath, &boot0, 1);
21363092Sjhb        memcpy(boot0 + OFF_PTBL, mbr + OFF_PTBL,
21463092Sjhb	    sizeof(struct dos_partition) * NDOSPART);
215185579Sluigi	if (b0_ver == 2)	/* volume serial number support */
216185579Sluigi	    memcpy(boot0 + OFF_SERIAL, mbr + OFF_SERIAL, 4);
21763092Sjhb    } else {
21863092Sjhb	boot0 = mbr;
21963092Sjhb	boot0_size = mbr_size;
22044196Srnordier    }
22163092Sjhb
22263092Sjhb    /* set the drive */
22344297Srnordier    if (d_arg != -1)
22463034Sjhb	boot0[OFF_DRIVE] = d_arg;
22563092Sjhb
22663092Sjhb    /* set various flags */
22748038Srnordier    if (m_arg != -1) {
22863034Sjhb	boot0[OFF_FLAGS] &= 0xf0;
22963034Sjhb	boot0[OFF_FLAGS] |= m_arg;
23048038Srnordier    }
23144196Srnordier    if (o_flag) {
23263034Sjhb        boot0[OFF_FLAGS] &= o_and;
23363034Sjhb        boot0[OFF_FLAGS] |= o_or;
23444196Srnordier    }
23563092Sjhb
23664797Sdwmalone    /* set the default boot selection */
23764797Sdwmalone    if (s_arg != -1)
23864797Sdwmalone        boot0[OFF_OPT] = s_arg - 1;
23964797Sdwmalone
24063092Sjhb    /* set the timeout */
24144196Srnordier    if (t_arg != -1)
24263034Sjhb        mk2(boot0 + OFF_TICKS, t_arg);
24363092Sjhb
244185579Sluigi    /* set the bell char */
245185579Sluigi    if (o_e != -1 && set_bell(boot0, o_e, 0) != -1)
246185579Sluigi	up = 1;
247185579Sluigi
248185579Sluigi    if (vol_id[4]) {
249185579Sluigi	if (b0_ver != 2)
250185579Sluigi	    errx(1, "incompatible boot block, cannot set volume ID");
251185579Sluigi	boot0[OFF_SERIAL] = vol_id[0];
252185579Sluigi	boot0[OFF_SERIAL+1] = vol_id[1];
253185579Sluigi	boot0[OFF_SERIAL+2] = vol_id[2];
254185579Sluigi	boot0[OFF_SERIAL+3] = vol_id[3];
255185579Sluigi	up = 1;	/* force update */
256185579Sluigi    }
25763092Sjhb    /* write the MBR back to disk */
25863092Sjhb    if (up)
25963092Sjhb	write_mbr(disk, 0, boot0, boot0_size);
26063092Sjhb
26163092Sjhb    /* display the MBR */
26263092Sjhb    if (v_flag)
26363092Sjhb	display_mbr(boot0);
26463092Sjhb
26563092Sjhb    /* clean up */
26663092Sjhb    if (mbr != boot0)
26763092Sjhb	free(boot0);
26863092Sjhb    free(mbr);
26987643Smikeh    free(disk);
27063092Sjhb
27163092Sjhb    return 0;
27263092Sjhb}
27363092Sjhb
274185579Sluigi/* get or set the 'bell' character to be used in case of errors.
275185579Sluigi * Lookup for a certain code sequence, return -1 if not found.
276185579Sluigi */
277185579Sluigistatic int
278185579Sluigiset_bell(u_int8_t *mbr, int new_bell, int report)
279185579Sluigi{
280185579Sluigi    /* lookup sequence: 0x100 means skip, 0x200 means done */
281185579Sluigi    static unsigned seq[] =
282185579Sluigi		{ 0xb0, 0x100, 0xe8, 0x100, 0x100, 0x30, 0xe4, 0x200 };
283185579Sluigi    int ofs, i, c;
284185579Sluigi    for (ofs = 0x60; ofs < 0x180; ofs++) { /* search range */
285185579Sluigi	if (mbr[ofs] != seq[0])	/* search initial pattern */
286185579Sluigi	    continue;
287185579Sluigi	for (i=0;; i++) {
288185579Sluigi	    if (seq[i] == 0x200) {	/* found */
289185579Sluigi		c = mbr[ofs+1];
290185579Sluigi		if (!report)
291185579Sluigi		    mbr[ofs+1] = c = new_bell;
292185579Sluigi		else
293185579Sluigi		    printf("  bell=%c (0x%x)",
294185579Sluigi			(c >= ' ' && c < 0x7f) ? c : ' ', c);
295185579Sluigi		return c;
296185579Sluigi	    }
297185579Sluigi	    if (seq[i] != 0x100 && seq[i] != mbr[ofs+i])
298185579Sluigi		break;
299185579Sluigi	}
300185579Sluigi    }
301185579Sluigi    warn("bell not found");
302185579Sluigi    return -1;
303185579Sluigi}
30463092Sjhb/*
30563092Sjhb * Read in the MBR of the disk.  If it is boot0, then use the version to
30663092Sjhb * read in all of it if necessary.  Use pointers to return a malloc'd
30763092Sjhb * buffer containing the MBR and then return its size.
30863092Sjhb */
30963092Sjhbstatic int
31063092Sjhbread_mbr(const char *disk, u_int8_t **mbr, int check_version)
31163092Sjhb{
31263092Sjhb    u_int8_t buf[MBRSIZE];
31363092Sjhb    int mbr_size, fd;
314185579Sluigi    int ver;
31563092Sjhb    ssize_t n;
31663092Sjhb
31763092Sjhb    if ((fd = open(disk, O_RDONLY)) == -1)
318108394Sphk        err(1, "open %s", disk);
31963092Sjhb    if ((n = read(fd, buf, MBRSIZE)) == -1)
320108394Sphk        err(1, "read %s", disk);
32163092Sjhb    if (n != MBRSIZE)
32263092Sjhb        errx(1, "%s: short read", disk);
32363092Sjhb    if (cv2(buf + OFF_MAGIC) != 0xaa55)
32463092Sjhb        errx(1, "%s: bad magic", disk);
32563092Sjhb
326185579Sluigi    if (! (ver = boot0bs(buf))) {
32763092Sjhb	if (check_version)
32863092Sjhb	    errx(1, "%s: unknown or incompatible boot code", disk);
32963092Sjhb    } else if (boot0version(buf) == 0x101) {
33063092Sjhb	mbr_size = 1024;
33163092Sjhb	if ((*mbr = malloc(mbr_size)) == NULL)
33263092Sjhb	    errx(1, "%s: unable to allocate read buffer", disk);
33363092Sjhb	if (lseek(fd, 0, SEEK_SET) == -1 ||
33463092Sjhb	    (n = read(fd, *mbr, mbr_size)) == -1)
33563092Sjhb	    err(1, "%s", disk);
33663092Sjhb	if (n != mbr_size)
33763092Sjhb	    errx(1, "%s: short read", disk);
338227876Skevlo	close(fd);
33963092Sjhb	return (mbr_size);
34044196Srnordier    }
341293977Sjhb    if ((*mbr = malloc(sizeof(buf))) == NULL)
342292410Sjhb	errx(1, "%s: unable to allocate MBR buffer", disk);
34363092Sjhb    memcpy(*mbr, buf, sizeof(buf));
344227876Skevlo    close(fd);
34563092Sjhb
34663092Sjhb    return sizeof(buf);
34763092Sjhb}
34863092Sjhb
349227297Saestatic int
350227297Saegeom_class_available(const char *name)
351227297Sae{
352227297Sae	struct gclass *class;
353227297Sae	struct gmesh mesh;
354227297Sae	int error;
355227297Sae
356227297Sae	error = geom_gettree(&mesh);
357227297Sae	if (error != 0)
358227297Sae		errc(1, error, "Cannot get GEOM tree");
359227297Sae
360227297Sae	LIST_FOREACH(class, &mesh.lg_class, lg_class) {
361227297Sae		if (strcmp(class->lg_name, name) == 0) {
362227297Sae			geom_deletetree(&mesh);
363227297Sae			return (1);
364227297Sae		}
365227297Sae	}
366227297Sae
367227297Sae	geom_deletetree(&mesh);
368227297Sae	return (0);
369227297Sae}
370227297Sae
37163092Sjhb/*
37263092Sjhb * Write out the mbr to the specified file.
37363092Sjhb */
37463092Sjhbstatic void
37563092Sjhbwrite_mbr(const char *fname, int flags, u_int8_t *mbr, int mbr_size)
37663092Sjhb{
377227297Sae	struct gctl_req *grq;
378227297Sae	const char *errmsg;
379227297Sae	char *pname;
380227297Sae	ssize_t n;
381227297Sae	int fd;
382148036Sphk
383227297Sae	fd = open(fname, O_WRONLY | flags, 0666);
384227297Sae	if (fd != -1) {
385227297Sae		n = write(fd, mbr, mbr_size);
386227297Sae		close(fd);
387227297Sae		if (n != mbr_size)
388227297Sae			errx(1, "%s: short write", fname);
389227297Sae		return;
390227297Sae	}
391189273Smarcel
392227297Sae	/*
393227297Sae	 * If we're called to write to a backup file, don't try to
394227297Sae	 * write through GEOM.
395227297Sae	 */
396227297Sae	if (flags != 0)
397227297Sae		err(1, "can't open file %s to write backup", fname);
398189273Smarcel
399227297Sae	/* Try open it read only. */
400227297Sae	fd = open(fname, O_RDONLY);
401227297Sae	if (fd == -1) {
402227297Sae		warn("error opening %s", fname);
403227297Sae		return;
404227297Sae	}
405189273Smarcel
406227297Sae	pname = g_providername(fd);
407227297Sae	if (pname == NULL) {
408227297Sae		warn("error getting providername for %s", fname);
409227297Sae		return;
410227297Sae	}
411227297Sae
412227297Sae	/* First check that GEOM_PART is available */
413227297Sae	if (geom_class_available("PART") != 0) {
414227297Sae		grq = gctl_get_handle();
415227297Sae		gctl_ro_param(grq, "class", -1, "PART");
416227297Sae		gctl_ro_param(grq, "arg0", -1, pname);
417227297Sae		gctl_ro_param(grq, "verb", -1, "bootcode");
418227297Sae		gctl_ro_param(grq, "bootcode", mbr_size, mbr);
419227297Sae		gctl_ro_param(grq, "flags", -1, "C");
420227297Sae		errmsg = gctl_issue(grq);
421227297Sae		if (errmsg != NULL && errmsg[0] != '\0')
422227297Sae			errx(1, "GEOM_PART: write bootcode to %s failed: %s",
423227297Sae			    fname, errmsg);
424227297Sae		gctl_free(grq);
425227297Sae	} else if (geom_class_available("MBR") != 0) {
426227297Sae		grq = gctl_get_handle();
427227297Sae		gctl_ro_param(grq, "verb", -1, "write MBR");
428227297Sae		gctl_ro_param(grq, "class", -1, "MBR");
429227297Sae		gctl_ro_param(grq, "geom", -1, pname);
430227297Sae		gctl_ro_param(grq, "data", mbr_size, mbr);
431227297Sae		errmsg = gctl_issue(grq);
432227297Sae		if (errmsg != NULL)
433227297Sae			err(1, "GEOM_MBR: write MBR to %s failed", fname);
434227297Sae		gctl_free(grq);
435227297Sae	} else
436227297Sae		errx(1, "can't write MBR to %s", fname);
437227297Sae	free(pname);
43863092Sjhb}
43963092Sjhb
44063092Sjhb/*
44163092Sjhb * Outputs an informative dump of the data in the MBR to stdout.
44263092Sjhb */
44363092Sjhbstatic void
44463092Sjhbdisplay_mbr(u_int8_t *mbr)
44563092Sjhb{
44663092Sjhb    struct dos_partition *part;
44763092Sjhb    int i, version;
44863092Sjhb
44963092Sjhb    part = (struct dos_partition *)(mbr + DOSPARTOFF);
45063092Sjhb    printf(fmt0);
45163092Sjhb    for (i = 0; i < NDOSPART; i++)
45263092Sjhb	if (part[i].dp_typ)
45363092Sjhb	    printf(fmt1, 1 + i, part[i].dp_flag,
45463092Sjhb		part[i].dp_scyl + ((part[i].dp_ssect & 0xc0) << 2),
45563092Sjhb		part[i].dp_shd, part[i].dp_ssect & 0x3f, part[i].dp_typ,
45663092Sjhb                part[i].dp_ecyl + ((part[i].dp_esect & 0xc0) << 2),
45763092Sjhb                part[i].dp_ehd, part[i].dp_esect & 0x3f, part[i].dp_start,
45863092Sjhb                part[i].dp_size);
45963092Sjhb    printf("\n");
46063092Sjhb    version = boot0version(mbr);
461185579Sluigi    printf("version=%d.%d  drive=0x%x  mask=0x%x  ticks=%u",
46263092Sjhb	version >> 8, version & 0xff, mbr[OFF_DRIVE],
46363092Sjhb	mbr[OFF_FLAGS] & 0xf, cv2(mbr + OFF_TICKS));
464185579Sluigi    set_bell(mbr, 0, 1);
465185579Sluigi    printf("\noptions=");
46663092Sjhb    for (i = 0; i < nopt; i++) {
46763092Sjhb	if (i)
46863092Sjhb	    printf(",");
46963092Sjhb	if (!(mbr[OFF_FLAGS] & 1 << (7 - i)) ^ opttbl[i].def)
47063092Sjhb	    printf("no");
47163092Sjhb	printf("%s", opttbl[i].tok);
47244196Srnordier    }
47363092Sjhb    printf("\n");
474185579Sluigi    if (b0_ver == 2)
475185579Sluigi	printf("volume serial ID %02x%02x-%02x%02x\n",
476185579Sluigi		mbr[OFF_SERIAL], mbr[OFF_SERIAL+1],
477185579Sluigi		mbr[OFF_SERIAL+2], mbr[OFF_SERIAL+3]);
47864797Sdwmalone    printf("default_selection=F%d (", mbr[OFF_OPT] + 1);
47964797Sdwmalone    if (mbr[OFF_OPT] < 4)
48064797Sdwmalone	printf("Slice %d", mbr[OFF_OPT] + 1);
481228738Sjhb    else if (mbr[OFF_OPT] == 4)
482228738Sjhb	printf("Drive 1");
48364797Sdwmalone    else
484228738Sjhb	printf("PXE");
48564797Sdwmalone    printf(")\n");
48644196Srnordier}
48744196Srnordier
48848113Srnordier/*
48963034Sjhb * Return the boot0 version with the minor revision in the low byte, and
49063034Sjhb * the major revision in the next higher byte.
49163034Sjhb */
49263034Sjhbstatic int
49363034Sjhbboot0version(const u_int8_t *bs)
49463034Sjhb{
49563034Sjhb    /* Check for old version, and return 0x100 if found. */
496185579Sluigi    int v = boot0bs(bs);
497185579Sluigi    if (v != 0)
498185579Sluigi        return v << 8;
49963034Sjhb
50063034Sjhb    /* We have a newer boot0, so extract the version number and return it. */
50187643Smikeh    return *(const int *)(bs + OFF_VERSION) & 0xffff;
50263034Sjhb}
50363034Sjhb
504185579Sluigi/* descriptor of a pattern to match.
505185579Sluigi * Start from the first entry trying to match the chunk of bytes,
506185579Sluigi * if you hit an entry with len=0 terminate the search and report
507185579Sluigi * off as the version. Otherwise skip to the next block after len=0
508185579Sluigi * An entry with len=0, off=0 is the end marker.
509185579Sluigi  */
510185579Sluigistruct byte_pattern {
511185579Sluigi    unsigned off;
512185579Sluigi    unsigned len;
513185579Sluigi    u_int8_t *key;
514185579Sluigi};
515185579Sluigi
51663034Sjhb/*
51748113Srnordier * Decide if we have valid boot0 boot code by looking for
51848113Srnordier * characteristic byte sequences at fixed offsets.
51948113Srnordier */
52048113Srnordierstatic int
52148113Srnordierboot0bs(const u_int8_t *bs)
52248113Srnordier{
523185579Sluigi    /* the initial code sequence */
52463034Sjhb    static u_int8_t id0[] = {0xfc, 0x31, 0xc0, 0x8e, 0xc0, 0x8e, 0xd8,
52563034Sjhb			     0x8e, 0xd0, 0xbc, 0x00, 0x7c };
526185579Sluigi    /* the drive id */
52748113Srnordier    static u_int8_t id1[] = {'D', 'r', 'i', 'v', 'e', ' '};
528185579Sluigi    static struct byte_pattern patterns[] = {
52963034Sjhb        {0x0,   sizeof(id0), id0},
530185579Sluigi        {0x1b2, sizeof(id1), id1},
531185579Sluigi        {1, 0, NULL},
532185579Sluigi        {0x0,   sizeof(id0), id0},	/* version with NT support */
533185579Sluigi        {0x1ae, sizeof(id1), id1},
534185579Sluigi        {2, 0, NULL},
535185579Sluigi        {0, 0, NULL},
53648113Srnordier    };
537185579Sluigi    struct byte_pattern *p = patterns;
53848113Srnordier
539185579Sluigi    for (;  p->off || p->len; p++) {
540185579Sluigi	if (p->len == 0)
541185579Sluigi	    break;
542185579Sluigi	if (!memcmp(bs + p->off, p->key, p->len))	/* match */
543185579Sluigi	    continue;
544185579Sluigi	while (p->len)	/* skip to next block */
545185579Sluigi	    p++;
546185579Sluigi    }
547185579Sluigi    b0_ver = p->off;	/* XXX ugly side effect */
548185579Sluigi    return p->off;
549129302Sstefanf}
55048113Srnordier
55148113Srnordier/*
55248113Srnordier * Adjust "and" and "or" masks for a -o option argument.
55348113Srnordier */
55444196Srnordierstatic void
55544196Srnordierstropt(const char *arg, int *xa, int *xo)
55644196Srnordier{
55744196Srnordier    const char *q;
55844196Srnordier    char *s, *s1;
55944196Srnordier    int inv, i, x;
56044196Srnordier
56144196Srnordier    if (!(s = strdup(arg)))
56244196Srnordier        err(1, NULL);
56344196Srnordier    for (s1 = s; (q = strtok(s1, ",")); s1 = NULL) {
56444196Srnordier        if ((inv = !strncmp(q, "no", 2)))
56544196Srnordier            q += 2;
56644196Srnordier        for (i = 0; i < nopt; i++)
56744196Srnordier            if (!strcmp(q, opttbl[i].tok))
56844196Srnordier                break;
56944196Srnordier        if (i == nopt)
57044196Srnordier            errx(1, "%s: Unknown -o option", q);
57144196Srnordier        if (opttbl[i].def)
57244196Srnordier            inv ^= 1;
57344196Srnordier        x = 1 << (7 - i);
57444196Srnordier        if (inv)
57544196Srnordier            *xa &= ~x;
57644196Srnordier        else
57744196Srnordier            *xo |= x;
57844196Srnordier    }
57944196Srnordier    free(s);
58044196Srnordier}
58144196Srnordier
58248113Srnordier/*
58348113Srnordier * Convert and check an option argument.
58448113Srnordier */
58544196Srnordierstatic int
58644196Srnordierargtoi(const char *arg, int lo, int hi, int opt)
58744196Srnordier{
58844196Srnordier    char *s;
58944196Srnordier    long x;
59044196Srnordier
59144196Srnordier    errno = 0;
59244196Srnordier    x = strtol(arg, &s, 0);
59344196Srnordier    if (errno || !*arg || *s || x < lo || x > hi)
59444196Srnordier        errx(1, "%s: Bad argument to -%c option", arg, opt);
59544196Srnordier    return x;
59644196Srnordier}
59744196Srnordier
59848113Srnordier/*
59948113Srnordier * Display usage information.
60048113Srnordier */
60144196Srnordierstatic void
60244196Srnordierusage(void)
60344196Srnordier{
60444196Srnordier    fprintf(stderr, "%s\n%s\n",
60548038Srnordier    "usage: boot0cfg [-Bv] [-b boot0] [-d drive] [-f file] [-m mask]",
60674668Siedowse    "                [-o options] [-s slice] [-t ticks] disk");
60744196Srnordier    exit(1);
60844196Srnordier}
609