10SN/A/*
25323SN/A * SPDX-License-Identifier: BSD-2-Clause
30SN/A *
40SN/A * Copyright (c) 2008 Luigi Rizzo
50SN/A * Copyright (c) 1999 Robert Nordier
60SN/A * All rights reserved.
70SN/A *
80SN/A * Redistribution and use in source and binary forms, with or without
90SN/A * modification, are permitted provided that the following conditions
100SN/A * are met:
110SN/A * 1. Redistributions of source code must retain the above copyright
120SN/A *    notice, this list of conditions and the following disclaimer.
130SN/A * 2. Redistributions in binary form must reproduce the above copyright
140SN/A *    notice, this list of conditions and the following disclaimer in the
150SN/A *    documentation and/or other materials provided with the distribution.
160SN/A *
170SN/A * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS``AS IS'' AND
180SN/A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191472SN/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
201472SN/A * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
211472SN/A * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
220SN/A * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
230SN/A * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
240SN/A * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
250SN/A * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
260SN/A * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
270SN/A * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
280SN/A */
290SN/A
300SN/A#include <sys/param.h>
310SN/A#include <sys/disklabel.h>
320SN/A#include <sys/diskmbr.h>
330SN/A#include <sys/stat.h>
340SN/A
350SN/A#include <err.h>
360SN/A#include <errno.h>
370SN/A#include <fcntl.h>
380SN/A#include <libgeom.h>
390SN/A#include <paths.h>
400SN/A#include <stdio.h>
410SN/A#include <stdlib.h>
420SN/A#include <string.h>
430SN/A#include <unistd.h>
445397SN/A
455397SN/A#define MBRSIZE         512     /* master boot record size */
460SN/A
475397SN/A#define OFF_VERSION	0x1b0	/* offset: version number, only boot0version */
485397SN/A#define OFF_SERIAL	0x1b8	/* offset: volume serial number */
495397SN/A#define OFF_PTBL        0x1be   /* offset: partition table */
500SN/A#define OFF_MAGIC       0x1fe   /* offset: magic number */
515397SN/A/*
525397SN/A * Offsets to the parameters of the 512-byte boot block.
535397SN/A * For historical reasons they are set as macros
540SN/A */
555397SN/Astruct opt_offsets {
565397SN/A	int opt;
575397SN/A	int drive;
580SN/A	int flags;
595397SN/A	int ticks;
605397SN/A};
615397SN/A
620SN/Astatic struct opt_offsets b0_ofs[] = {
635397SN/A	{ 0x0, 0x0, 0x0, 0x0 },		/* no boot block */
645397SN/A	{ 0x1b9, 0x1ba, 0x1bb, 0x1bc },	/* original block */
655397SN/A	{ 0x1b5, 0x1b6, 0x1b7, 0x1bc },	/* NT_SERIAL block */
665397SN/A};
675397SN/A
685397SN/Astatic int b0_ver;	/* boot block version set by boot0bs */
695397SN/A
705397SN/A#define OFF_OPT		(b0_ofs[b0_ver].opt)	/* default boot option */
715397SN/A#define OFF_DRIVE	(b0_ofs[b0_ver].drive)	/* setdrv drive */
720SN/A#define OFF_FLAGS       (b0_ofs[b0_ver].flags)	/* option flags */
730SN/A#define OFF_TICKS       (b0_ofs[b0_ver].ticks)	/* clock ticks */
740SN/A
750SN/A
760SN/A#define cv2(p)  ((p)[0] | (p)[1] << 010)
770SN/A
785397SN/A#define mk2(p, x)                               \
795397SN/A    (p)[0] = (u_int8_t)(x),                     \
805397SN/A    (p)[1] = (u_int8_t)((x) >> 010)
810SN/A
820SN/Astatic const struct {
830SN/A    const char *tok;
845397SN/A    int def;
850SN/A} opttbl[] = {
860SN/A    {"packet", 0},
870SN/A    {"update", 1},
880SN/A    {"setdrv", 0}
890SN/A};
905397SN/Astatic const int nopt = nitems(opttbl);
915397SN/A
925397SN/Astatic const char fmt0[] = "#   flag     start chs   type"
930SN/A    "       end chs       offset         size\n";
940SN/A
950SN/Astatic const char fmt1[] = "%d   0x%02x   %4u:%3u:%2u   0x%02x"
960SN/A    "   %4u:%3u:%2u   %10u   %10u\n";
970SN/A
985397SN/Astatic int geom_class_available(const char *);
995397SN/Astatic int read_mbr(const char *, u_int8_t **, int);
1005397SN/Astatic void write_mbr(const char *, int, u_int8_t *, int, int);
1015397SN/Astatic void display_mbr(u_int8_t *);
1025397SN/Astatic int boot0version(const u_int8_t *);
1030SN/Astatic int boot0bs(const u_int8_t *);
1040SN/Astatic void stropt(const char *, int *, int *);
1050SN/Astatic int argtoi(const char *, int, int, int);
1065397SN/Astatic int set_bell(u_int8_t *, int, int);
1075397SN/Astatic void usage(void) __dead2;
1085397SN/A
1095397SN/Astatic unsigned vol_id[5];	/* 4 plus 1 for flag */
1105397SN/A
1110SN/Astatic int v_flag;
1125397SN/A/*
1135397SN/A * Boot manager installation/configuration utility.
1145397SN/A */
1155397SN/Aint
1165397SN/Amain(int argc, char *argv[])
1175397SN/A{
1180SN/A    u_int8_t *mbr, *boot0;
1190SN/A    int boot0_size, mbr_size;
1200SN/A    const char *bpath, *fpath;
1210SN/A    char *disk;
1220SN/A    int B_flag, o_flag;
1235397SN/A    int d_arg, m_arg, s_arg, t_arg;
1245397SN/A    int o_and, o_or, o_e = -1;
1255397SN/A    int up, c;
1265397SN/A
1270SN/A    bpath = "/boot/boot0";
1285397SN/A    fpath = NULL;
1295397SN/A    B_flag = v_flag = o_flag = 0;
1305397SN/A    d_arg = m_arg = s_arg = t_arg = -1;
1315397SN/A    o_and = 0xff;
1320SN/A    o_or = 0;
1335397SN/A    while ((c = getopt(argc, argv, "Bvb:d:e:f:i:m:o:s:t:")) != -1)
1340SN/A        switch (c) {
1350SN/A        case 'B':
1360SN/A            B_flag = 1;
1375397SN/A            break;
1380SN/A        case 'v':
1395397SN/A            v_flag = 1;
1405397SN/A            break;
1415397SN/A        case 'b':
1425397SN/A            bpath = optarg;
1435397SN/A            break;
1440SN/A        case 'd':
1455397SN/A            d_arg = argtoi(optarg, 0, 0xff, 'd');
1465397SN/A            break;
1475397SN/A        case 'e':
1480SN/A	    if (optarg[0] == '0' && optarg[1] == 'x')
1490SN/A		sscanf(optarg, "0x%02x", &o_e);
1500SN/A	    else
1510SN/A		o_e = optarg[0];
1525397SN/A            break;
1535397SN/A        case 'f':
1545397SN/A            fpath = optarg;
1550SN/A            break;
1565397SN/A        case 'i':
1575397SN/A            if (sscanf(optarg, "%02x%02x-%02x%02x",
1585397SN/A		vol_id, vol_id+1, vol_id+2, vol_id+3) == 4)
1595397SN/A			vol_id[4] = 1;
1605397SN/A	    else
1615397SN/A		errx(1, "bad argument %s", optarg);
1625397SN/A            break;
1635397SN/A        case 'm':
1640SN/A            m_arg = argtoi(optarg, 0, 0xf, 'm');
1655397SN/A            break;
1665397SN/A        case 'o':
1675397SN/A            stropt(optarg, &o_and, &o_or);
1685397SN/A            o_flag = 1;
1695397SN/A            break;
1700SN/A        case 's':
1715397SN/A	    if (strcasecmp(optarg, "pxe") == 0)
1725397SN/A		s_arg = 6;
1735397SN/A	    else
1740SN/A		s_arg = argtoi(optarg, 1, 6, 's');
1750SN/A            break;
1765397SN/A        case 't':
1775397SN/A            t_arg = argtoi(optarg, 1, 0xffff, 't');
1785397SN/A            break;
1795397SN/A        default:
1805397SN/A            usage();
1815397SN/A        }
1825397SN/A    argc -= optind;
1835397SN/A    argv += optind;
1845397SN/A    if (argc != 1)
1855397SN/A        usage();
1865397SN/A    disk = g_device_path(*argv);
1875397SN/A    if (disk == NULL)
1885397SN/A        errx(1, "Unable to get providername for %s\n", *argv);
1895397SN/A    up = B_flag || d_arg != -1 || m_arg != -1 || o_flag || s_arg != -1
1905397SN/A	|| t_arg != -1;
1915397SN/A
1925397SN/A    /* open the disk and read in the existing mbr. Either here or
1930SN/A     * when reading the block from disk, we do check for the version
1945397SN/A     * and abort if a suitable block is not found.
1955397SN/A     */
1960SN/A    mbr_size = read_mbr(disk, &mbr, !B_flag);
1970SN/A
1980SN/A    /* save the existing MBR if we are asked to do so */
1990SN/A    if (fpath)
2000SN/A	write_mbr(fpath, O_CREAT | O_TRUNC, mbr, mbr_size, 0);
2014626SN/A
2020SN/A    /*
2030SN/A     * If we are installing the boot loader, read it from disk and copy the
2044626SN/A     * slice table over from the existing MBR.  If not, then point boot0
2050SN/A     * back at the MBR we just read in.  After this, boot0 is the data to
2060SN/A     * write back to disk if we are going to do a write.
2070SN/A     */
2080SN/A    if (B_flag) {
2090SN/A	boot0_size = read_mbr(bpath, &boot0, 1);
2100SN/A        memcpy(boot0 + OFF_PTBL, mbr + OFF_PTBL,
2110SN/A	    sizeof(struct dos_partition) * NDOSPART);
2120SN/A	if (b0_ver == 2)	/* volume serial number support */
2130SN/A	    memcpy(boot0 + OFF_SERIAL, mbr + OFF_SERIAL, 4);
2140SN/A    } else {
2150SN/A	boot0 = mbr;
2160SN/A	boot0_size = mbr_size;
2170SN/A    }
2180SN/A
2190SN/A    /* set the drive */
2200SN/A    if (d_arg != -1)
2210SN/A	boot0[OFF_DRIVE] = d_arg;
2220SN/A
2230SN/A    /* set various flags */
2240SN/A    if (m_arg != -1) {
2250SN/A	boot0[OFF_FLAGS] &= 0xf0;
2260SN/A	boot0[OFF_FLAGS] |= m_arg;
2270SN/A    }
2280SN/A    if (o_flag) {
2290SN/A        boot0[OFF_FLAGS] &= o_and;
2300SN/A        boot0[OFF_FLAGS] |= o_or;
2310SN/A    }
2325397SN/A
2335397SN/A    /* set the default boot selection */
2345397SN/A    if (s_arg != -1)
2350SN/A        boot0[OFF_OPT] = s_arg - 1;
2360SN/A
2370SN/A    /* set the timeout */
2383602SN/A    if (t_arg != -1)
2390SN/A        mk2(boot0 + OFF_TICKS, t_arg);
2400SN/A
2410SN/A    /* set the bell char */
2420SN/A    if (o_e != -1 && set_bell(boot0, o_e, 0) != -1)
243529SN/A	up = 1;
2445397SN/A
2455397SN/A    if (vol_id[4]) {
2465397SN/A	if (b0_ver != 2)
2475397SN/A	    errx(1, "incompatible boot block, cannot set volume ID");
2485397SN/A	boot0[OFF_SERIAL] = vol_id[0];
2495397SN/A	boot0[OFF_SERIAL+1] = vol_id[1];
2505397SN/A	boot0[OFF_SERIAL+2] = vol_id[2];
2510SN/A	boot0[OFF_SERIAL+3] = vol_id[3];
2520SN/A	up = 1;	/* force update */
2530SN/A    }
2545397SN/A    /* write the MBR back to disk */
2555397SN/A    if (up)
2565397SN/A	write_mbr(disk, 0, boot0, boot0_size, vol_id[4] || b0_ver == 1);
2575397SN/A
2585397SN/A    /* display the MBR */
2595397SN/A    if (v_flag)
2605397SN/A	display_mbr(boot0);
2610SN/A
2620SN/A    /* clean up */
2630SN/A    if (mbr != boot0)
2640SN/A	free(boot0);
2655397SN/A    free(mbr);
2665397SN/A    free(disk);
2670SN/A
2685397SN/A    return 0;
2695397SN/A}
2705397SN/A
2715397SN/A/* get or set the 'bell' character to be used in case of errors.
2725397SN/A * Lookup for a certain code sequence, return -1 if not found.
2735397SN/A */
2745397SN/Astatic int
2755397SN/Aset_bell(u_int8_t *mbr, int new_bell, int report)
2765397SN/A{
2775397SN/A    /* lookup sequence: 0x100 means skip, 0x200 means done */
2785397SN/A    static unsigned seq[] =
2795397SN/A		{ 0xb0, 0x100, 0xe8, 0x100, 0x100, 0x30, 0xe4, 0x200 };
2800SN/A    int ofs, i, c;
2815397SN/A    for (ofs = 0x60; ofs < 0x180; ofs++) { /* search range */
2825397SN/A	if (mbr[ofs] != seq[0])	/* search initial pattern */
2830SN/A	    continue;
2840SN/A	for (i=0;; i++) {
2850SN/A	    if (seq[i] == 0x200) {	/* found */
2860SN/A		c = mbr[ofs+1];
2870SN/A		if (!report)
2885397SN/A		    mbr[ofs+1] = c = new_bell;
2890SN/A		else
2900SN/A		    printf("  bell=%c (0x%x)",
2915397SN/A			(c >= ' ' && c < 0x7f) ? c : ' ', c);
2925397SN/A		return c;
2935397SN/A	    }
2945397SN/A	    if (seq[i] != 0x100 && seq[i] != mbr[ofs+i])
2955397SN/A		break;
2965397SN/A	}
2975397SN/A    }
2985397SN/A    warn("bell not found");
2995397SN/A    return -1;
3005397SN/A}
3015397SN/A/*
3025397SN/A * Read in the MBR of the disk.  If it is boot0, then use the version to
3035397SN/A * read in all of it if necessary.  Use pointers to return a malloc'd
3045397SN/A * buffer containing the MBR and then return its size.
3055397SN/A */
3060SN/Astatic int
3075397SN/Aread_mbr(const char *disk, u_int8_t **mbr, int check_version)
3085397SN/A{
3095397SN/A    u_int8_t buf[MBRSIZE];
3105397SN/A    int mbr_size, fd;
3115397SN/A    int ver;
3125397SN/A    ssize_t n;
3135397SN/A
3140SN/A    if ((fd = open(disk, O_RDONLY)) == -1)
3155397SN/A        err(1, "open %s", disk);
3165397SN/A    if ((n = read(fd, buf, MBRSIZE)) == -1)
3175397SN/A        err(1, "read %s", disk);
3185397SN/A    if (n != MBRSIZE)
3195397SN/A        errx(1, "%s: short read", disk);
3205397SN/A    if (cv2(buf + OFF_MAGIC) != 0xaa55)
3215397SN/A        errx(1, "%s: bad magic", disk);
3220SN/A
3235397SN/A    if (! (ver = boot0bs(buf))) {
3245397SN/A	if (check_version)
3255397SN/A	    errx(1, "%s: unknown or incompatible boot code", disk);
3265397SN/A    } else if (boot0version(buf) == 0x101) {
3270SN/A	mbr_size = 1024;
3285397SN/A	if ((*mbr = malloc(mbr_size)) == NULL)
3295397SN/A	    errx(1, "%s: unable to allocate read buffer", disk);
3305397SN/A	if (lseek(fd, 0, SEEK_SET) == -1 ||
3315397SN/A	    (n = read(fd, *mbr, mbr_size)) == -1)
3325397SN/A	    err(1, "%s", disk);
3330SN/A	if (n != mbr_size)
3345397SN/A	    errx(1, "%s: short read", disk);
3355397SN/A	close(fd);
3365397SN/A	return (mbr_size);
3375397SN/A    }
3380SN/A    if ((*mbr = malloc(sizeof(buf))) == NULL)
3395397SN/A	errx(1, "%s: unable to allocate MBR buffer", disk);
3405397SN/A    memcpy(*mbr, buf, sizeof(buf));
3415397SN/A    close(fd);
3425397SN/A
3430SN/A    return sizeof(buf);
3445397SN/A}
3455397SN/A
3465397SN/Astatic int
3475397SN/Ageom_class_available(const char *name)
3485397SN/A{
3495397SN/A	struct gclass *class;
3505397SN/A	struct gmesh mesh;
3515397SN/A	int error;
3525397SN/A
3535397SN/A	error = geom_gettree(&mesh);
3540SN/A	if (error != 0)
3555397SN/A		errc(1, error, "Cannot get GEOM tree");
3565397SN/A
3575397SN/A	LIST_FOREACH(class, &mesh.lg_class, lg_class) {
3585397SN/A		if (strcmp(class->lg_name, name) == 0) {
3595397SN/A			geom_deletetree(&mesh);
3605397SN/A			return (1);
3615397SN/A		}
3625397SN/A	}
3630SN/A
3645397SN/A	geom_deletetree(&mesh);
3655397SN/A	return (0);
3665397SN/A}
3675397SN/A
3685397SN/A/*
3695397SN/A * Write out the mbr to the specified file.
3705397SN/A */
3710SN/Astatic void
3725397SN/Awrite_mbr(const char *fname, int flags, u_int8_t *mbr, int mbr_size,
3735397SN/A    int disable_dsn)
3745397SN/A{
3755397SN/A	struct gctl_req *grq;
3765397SN/A	const char *errmsg;
3775397SN/A	char *pname;
3785397SN/A	ssize_t n;
3790SN/A	int fd;
3805397SN/A
3815397SN/A	fd = open(fname, O_WRONLY | flags, 0666);
3825397SN/A	if (fd != -1) {
3835397SN/A		n = write(fd, mbr, mbr_size);
3845397SN/A		close(fd);
3855397SN/A		if (n != mbr_size)
3865397SN/A			errx(1, "%s: short write", fname);
3875397SN/A		return;
3885397SN/A	}
3895397SN/A
3905397SN/A	/*
3910SN/A	 * If we're called to write to a backup file, don't try to
3925397SN/A	 * write through GEOM.
3930SN/A	 */
3945397SN/A	if (flags != 0)
3955397SN/A		err(1, "can't open file %s to write backup", fname);
3965397SN/A
3970SN/A	/* Try open it read only. */
3980SN/A	fd = open(fname, O_RDONLY);
3990SN/A	if (fd == -1) {
4000SN/A		warn("error opening %s", fname);
4010SN/A		return;
4020SN/A	}
4030SN/A
4040SN/A	pname = g_providername(fd);
4050SN/A	if (pname == NULL) {
4060SN/A		warn("error getting providername for %s", fname);
4075397SN/A		return;
4085397SN/A	}
4090SN/A
4105397SN/A	/* First check that GEOM_PART is available */
4115397SN/A	if (geom_class_available("PART") != 0) {
4125397SN/A		grq = gctl_get_handle();
4130SN/A		gctl_ro_param(grq, "class", -1, "PART");
4145397SN/A		gctl_ro_param(grq, "arg0", -1, pname);
4150SN/A		gctl_ro_param(grq, "verb", -1, "bootcode");
4160SN/A		gctl_ro_param(grq, "bootcode", mbr_size, mbr);
4170SN/A		gctl_ro_param(grq, "flags", -1, "C");
4180SN/A		if (disable_dsn)
4190SN/A			gctl_ro_param(grq, "skip_dsn", sizeof(int),
4205397SN/A			    &disable_dsn);
4215397SN/A		errmsg = gctl_issue(grq);
4225397SN/A		if (errmsg != NULL && errmsg[0] != '\0')
4230SN/A			errx(1, "GEOM_PART: write bootcode to %s failed: %s",
4245397SN/A			    fname, errmsg);
4255397SN/A		gctl_free(grq);
4265397SN/A	} else
4275397SN/A		errx(1, "can't write MBR to %s", fname);
4285397SN/A	free(pname);
4295397SN/A}
4300SN/A
4315397SN/A/*
4325397SN/A * Outputs an informative dump of the data in the MBR to stdout.
4335397SN/A */
4345397SN/Astatic void
4355397SN/Adisplay_mbr(u_int8_t *mbr)
4365397SN/A{
4370SN/A    struct dos_partition *part;
4385397SN/A    int i, version;
4395397SN/A
4405397SN/A    part = (struct dos_partition *)(mbr + DOSPARTOFF);
4415397SN/A    printf(fmt0);
4425397SN/A    for (i = 0; i < NDOSPART; i++)
4435397SN/A	if (part[i].dp_typ)
4445397SN/A	    printf(fmt1, 1 + i, part[i].dp_flag,
4455397SN/A		part[i].dp_scyl + ((part[i].dp_ssect & 0xc0) << 2),
4465397SN/A		part[i].dp_shd, part[i].dp_ssect & 0x3f, part[i].dp_typ,
4470SN/A                part[i].dp_ecyl + ((part[i].dp_esect & 0xc0) << 2),
4485397SN/A                part[i].dp_ehd, part[i].dp_esect & 0x3f, part[i].dp_start,
4495397SN/A                part[i].dp_size);
4505397SN/A    printf("\n");
4515397SN/A    version = boot0version(mbr);
4525397SN/A    printf("version=%d.%d  drive=0x%x  mask=0x%x  ticks=%u",
4535397SN/A	version >> 8, version & 0xff, mbr[OFF_DRIVE],
4545397SN/A	mbr[OFF_FLAGS] & 0xf, cv2(mbr + OFF_TICKS));
4555397SN/A    set_bell(mbr, 0, 1);
4565397SN/A    printf("\noptions=");
4570SN/A    for (i = 0; i < nopt; i++) {
4585397SN/A	if (i)
4590SN/A	    printf(",");
4600SN/A	if (!(mbr[OFF_FLAGS] & 1 << (7 - i)) ^ opttbl[i].def)
4610SN/A	    printf("no");
4620SN/A	printf("%s", opttbl[i].tok);
4630SN/A    }
4640SN/A    printf("\n");
4650SN/A    if (b0_ver == 2)
4660SN/A	printf("volume serial ID %02x%02x-%02x%02x\n",
4670SN/A		mbr[OFF_SERIAL], mbr[OFF_SERIAL+1],
4680SN/A		mbr[OFF_SERIAL+2], mbr[OFF_SERIAL+3]);
4690SN/A    printf("default_selection=F%d (", mbr[OFF_OPT] + 1);
4700SN/A    if (mbr[OFF_OPT] < 4)
4710SN/A	printf("Slice %d", mbr[OFF_OPT] + 1);
4720SN/A    else if (mbr[OFF_OPT] == 4)
4730SN/A	printf("Drive 1");
4740SN/A    else
4755397SN/A	printf("PXE");
4760SN/A    printf(")\n");
4775397SN/A}
4780SN/A
4790SN/A/*
4800SN/A * Return the boot0 version with the minor revision in the low byte, and
4810SN/A * the major revision in the next higher byte.
4820SN/A */
4830SN/Astatic int
4845397SN/Aboot0version(const u_int8_t *bs)
4850SN/A{
4865397SN/A    /* Check for old version, and return 0x100 if found. */
4870SN/A    int v = boot0bs(bs);
4880SN/A    if (v != 0)
4890SN/A        return v << 8;
4900SN/A
4910SN/A    /* We have a newer boot0, so extract the version number and return it. */
4920SN/A    return *(const int *)(bs + OFF_VERSION) & 0xffff;
4930SN/A}
4940SN/A
4950SN/A/* descriptor of a pattern to match.
4960SN/A * Start from the first entry trying to match the chunk of bytes,
4970SN/A * if you hit an entry with len=0 terminate the search and report
4980SN/A * off as the version. Otherwise skip to the next block after len=0
4990SN/A * An entry with len=0, off=0 is the end marker.
5000SN/A  */
5010SN/Astruct byte_pattern {
5020SN/A    unsigned off;
5030SN/A    unsigned len;
5040SN/A    u_int8_t *key;
5050SN/A};
5060SN/A
5070SN/A/*
5080SN/A * Decide if we have valid boot0 boot code by looking for
5090SN/A * characteristic byte sequences at fixed offsets.
5100SN/A */
5110SN/Astatic int
5120SN/Aboot0bs(const u_int8_t *bs)
5130SN/A{
5140SN/A    /* the initial code sequence */
5150SN/A    static u_int8_t id0[] = {0xfc, 0x31, 0xc0, 0x8e, 0xc0, 0x8e, 0xd8,
5160SN/A			     0x8e, 0xd0, 0xbc, 0x00, 0x7c };
5170SN/A    /* the drive id */
5180SN/A    static u_int8_t id1[] = {'D', 'r', 'i', 'v', 'e', ' '};
5190SN/A    static struct byte_pattern patterns[] = {
5200SN/A        {0x0,   sizeof(id0), id0},
5210SN/A        {0x1b2, sizeof(id1), id1},
5220SN/A        {1, 0, NULL},
5230SN/A        {0x0,   sizeof(id0), id0},	/* version with NT support */
5240SN/A        {0x1ae, sizeof(id1), id1},
5250SN/A        {2, 0, NULL},
5260SN/A        {0, 0, NULL},
5270SN/A    };
5280SN/A    struct byte_pattern *p = patterns;
5290SN/A
5300SN/A    for (;  p->off || p->len; p++) {
5310SN/A	if (p->len == 0)
5320SN/A	    break;
5330SN/A	if (!memcmp(bs + p->off, p->key, p->len))	/* match */
5340SN/A	    continue;
5350SN/A	while (p->len)	/* skip to next block */
53650SN/A	    p++;
53750SN/A    }
53850SN/A    b0_ver = p->off;	/* XXX ugly side effect */
53950SN/A    return p->off;
5400SN/A}
5410SN/A
5420SN/A/*
5430SN/A * Adjust "and" and "or" masks for a -o option argument.
5440SN/A */
5450SN/Astatic void
5460SN/Astropt(const char *arg, int *xa, int *xo)
5470SN/A{
5480SN/A    const char *q;
5490SN/A    char *s, *s1;
5500SN/A    int inv, i, x;
5510SN/A
5520SN/A    if (!(s = strdup(arg)))
5530SN/A        err(1, NULL);
5540SN/A    for (s1 = s; (q = strtok(s1, ",")); s1 = NULL) {
5550SN/A        if ((inv = !strncmp(q, "no", 2)))
5560SN/A            q += 2;
5570SN/A        for (i = 0; i < nopt; i++)
5580SN/A            if (!strcmp(q, opttbl[i].tok))
5590SN/A                break;
5600SN/A        if (i == nopt)
5610SN/A            errx(1, "%s: Unknown -o option", q);
5620SN/A        if (opttbl[i].def)
5630SN/A            inv ^= 1;
5640SN/A        x = 1 << (7 - i);
5650SN/A        if (inv)
5660SN/A            *xa &= ~x;
5670SN/A        else
5680SN/A            *xo |= x;
5690SN/A    }
5700SN/A    free(s);
5710SN/A}
5720SN/A
5730SN/A/*
5740SN/A * Convert and check an option argument.
5750SN/A */
5760SN/Astatic int
5770SN/Aargtoi(const char *arg, int lo, int hi, int opt)
5780SN/A{
5790SN/A    char *s;
5800SN/A    long x;
5810SN/A
5820SN/A    errno = 0;
5830SN/A    x = strtol(arg, &s, 0);
5840SN/A    if (errno || !*arg || *s || x < lo || x > hi)
5850SN/A        errx(1, "%s: Bad argument to -%c option", arg, opt);
5860SN/A    return x;
5870SN/A}
5880SN/A
5890SN/A/*
5900SN/A * Display usage information.
5910SN/A */
5920SN/Astatic void
5930SN/Ausage(void)
5940SN/A{
5950SN/A    fprintf(stderr, "%s\n%s\n",
5960SN/A    "usage: boot0cfg [-Bv] [-b boot0] [-d drive] [-f file] [-m mask]",
5970SN/A    "                [-o options] [-s slice] [-t ticks] disk");
5980SN/A    exit(1);
5990SN/A}
6000SN/A