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$"); 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 63185579Sluigistruct 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 69185579Sluigiint 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 9963092Sjhbstatic int read_mbr(const char *, u_int8_t **, int); 10063092Sjhbstatic void write_mbr(const char *, int, u_int8_t *, int); 10163092Sjhbstatic void display_mbr(u_int8_t *); 10263034Sjhbstatic int boot0version(const u_int8_t *); 10348113Srnordierstatic int boot0bs(const u_int8_t *); 10444196Srnordierstatic void stropt(const char *, int *, int *); 10544196Srnordierstatic int argtoi(const char *, int, int, int); 106185579Sluigistatic int set_bell(u_int8_t *, int, int); 10744196Srnordierstatic void usage(void); 10844196Srnordier 109185579Sluigiunsigned vol_id[5]; /* 4 plus 1 for flag */ 110185579Sluigi 111185579Sluigiint v_flag; 11248113Srnordier/* 11348113Srnordier * Boot manager installation/configuration utility. 11448113Srnordier */ 11544196Srnordierint 11644196Srnordiermain(int argc, char *argv[]) 11744196Srnordier{ 11863092Sjhb u_int8_t *mbr, *boot0; 11963092Sjhb int boot0_size, mbr_size; 12087643Smikeh const char *bpath, *fpath; 12187643Smikeh char *disk; 122185579Sluigi int B_flag, o_flag; 12364797Sdwmalone int d_arg, m_arg, s_arg, t_arg; 124185579Sluigi int o_and, o_or, o_e = -1; 12563092Sjhb int up, c; 12644196Srnordier 12744196Srnordier bpath = "/boot/boot0"; 12844196Srnordier fpath = NULL; 12944196Srnordier B_flag = v_flag = o_flag = 0; 13064797Sdwmalone d_arg = m_arg = s_arg = t_arg = -1; 13144196Srnordier o_and = 0xff; 13244196Srnordier o_or = 0; 133185579Sluigi while ((c = getopt(argc, argv, "Bvb:d:e:f:i:m:o:s:t:")) != -1) 13444196Srnordier switch (c) { 13544196Srnordier case 'B': 13644196Srnordier B_flag = 1; 13744196Srnordier break; 13844196Srnordier case 'v': 13944196Srnordier v_flag = 1; 14044196Srnordier break; 14144196Srnordier case 'b': 14244196Srnordier bpath = optarg; 14344196Srnordier break; 14444196Srnordier case 'd': 14544196Srnordier d_arg = argtoi(optarg, 0, 0xff, 'd'); 14644196Srnordier break; 147185579Sluigi case 'e': 148185579Sluigi if (optarg[0] == '0' && optarg[1] == 'x') 149185579Sluigi sscanf(optarg, "0x%02x", &o_e); 150185579Sluigi else 151185579Sluigi o_e = optarg[0]; 152185579Sluigi break; 15344196Srnordier case 'f': 15444196Srnordier fpath = optarg; 15544196Srnordier break; 156185579Sluigi case 'i': 157185579Sluigi if (sscanf(optarg, "%02x%02x-%02x%02x", 158185579Sluigi vol_id, vol_id+1, vol_id+2, vol_id+3) == 4) 159185579Sluigi vol_id[4] = 1; 160185579Sluigi else 161185579Sluigi errx(1, "bad argument %s", optarg); 162185579Sluigi break; 16348038Srnordier case 'm': 16448038Srnordier m_arg = argtoi(optarg, 0, 0xf, 'm'); 16548038Srnordier break; 16644196Srnordier case 'o': 16744196Srnordier stropt(optarg, &o_and, &o_or); 16844196Srnordier o_flag = 1; 16944196Srnordier break; 17064797Sdwmalone case 's': 171230065Sjhb if (strcasecmp(optarg, "pxe") == 0) 172230065Sjhb s_arg = 6; 173230065Sjhb else 174230065Sjhb s_arg = argtoi(optarg, 1, 6, 's'); 17564797Sdwmalone break; 17644196Srnordier case 't': 17744196Srnordier t_arg = argtoi(optarg, 1, 0xffff, 't'); 17844196Srnordier break; 17944196Srnordier default: 18044196Srnordier usage(); 18144196Srnordier } 18244196Srnordier argc -= optind; 18344196Srnordier argv += optind; 18444196Srnordier if (argc != 1) 18544196Srnordier usage(); 186182844Slulf disk = g_device_path(*argv); 187182844Slulf if (disk == NULL) 188182844Slulf errx(1, "Unable to get providername for %s\n", *argv); 18964797Sdwmalone up = B_flag || d_arg != -1 || m_arg != -1 || o_flag || s_arg != -1 19064797Sdwmalone || t_arg != -1; 19163092Sjhb 192185579Sluigi /* open the disk and read in the existing mbr. Either here or 193185579Sluigi * when reading the block from disk, we do check for the version 194185579Sluigi * and abort if a suitable block is not found. 195185579Sluigi */ 19663092Sjhb mbr_size = read_mbr(disk, &mbr, !B_flag); 19763092Sjhb 19863092Sjhb /* save the existing MBR if we are asked to do so */ 19963092Sjhb if (fpath) 20063092Sjhb write_mbr(fpath, O_CREAT | O_TRUNC, mbr, mbr_size); 20163092Sjhb 20263092Sjhb /* 20363092Sjhb * If we are installing the boot loader, read it from disk and copy the 20463092Sjhb * slice table over from the existing MBR. If not, then point boot0 20563092Sjhb * back at the MBR we just read in. After this, boot0 is the data to 20663092Sjhb * write back to disk if we are going to do a write. 20763092Sjhb */ 20844196Srnordier if (B_flag) { 20963092Sjhb boot0_size = read_mbr(bpath, &boot0, 1); 21063092Sjhb memcpy(boot0 + OFF_PTBL, mbr + OFF_PTBL, 21163092Sjhb sizeof(struct dos_partition) * NDOSPART); 212185579Sluigi if (b0_ver == 2) /* volume serial number support */ 213185579Sluigi memcpy(boot0 + OFF_SERIAL, mbr + OFF_SERIAL, 4); 21463092Sjhb } else { 21563092Sjhb boot0 = mbr; 21663092Sjhb boot0_size = mbr_size; 21744196Srnordier } 21863092Sjhb 21963092Sjhb /* set the drive */ 22044297Srnordier if (d_arg != -1) 22163034Sjhb boot0[OFF_DRIVE] = d_arg; 22263092Sjhb 22363092Sjhb /* set various flags */ 22448038Srnordier if (m_arg != -1) { 22563034Sjhb boot0[OFF_FLAGS] &= 0xf0; 22663034Sjhb boot0[OFF_FLAGS] |= m_arg; 22748038Srnordier } 22844196Srnordier if (o_flag) { 22963034Sjhb boot0[OFF_FLAGS] &= o_and; 23063034Sjhb boot0[OFF_FLAGS] |= o_or; 23144196Srnordier } 23263092Sjhb 23364797Sdwmalone /* set the default boot selection */ 23464797Sdwmalone if (s_arg != -1) 23564797Sdwmalone boot0[OFF_OPT] = s_arg - 1; 23664797Sdwmalone 23763092Sjhb /* set the timeout */ 23844196Srnordier if (t_arg != -1) 23963034Sjhb mk2(boot0 + OFF_TICKS, t_arg); 24063092Sjhb 241185579Sluigi /* set the bell char */ 242185579Sluigi if (o_e != -1 && set_bell(boot0, o_e, 0) != -1) 243185579Sluigi up = 1; 244185579Sluigi 245185579Sluigi if (vol_id[4]) { 246185579Sluigi if (b0_ver != 2) 247185579Sluigi errx(1, "incompatible boot block, cannot set volume ID"); 248185579Sluigi boot0[OFF_SERIAL] = vol_id[0]; 249185579Sluigi boot0[OFF_SERIAL+1] = vol_id[1]; 250185579Sluigi boot0[OFF_SERIAL+2] = vol_id[2]; 251185579Sluigi boot0[OFF_SERIAL+3] = vol_id[3]; 252185579Sluigi up = 1; /* force update */ 253185579Sluigi } 25463092Sjhb /* write the MBR back to disk */ 25563092Sjhb if (up) 25663092Sjhb write_mbr(disk, 0, boot0, boot0_size); 25763092Sjhb 25863092Sjhb /* display the MBR */ 25963092Sjhb if (v_flag) 26063092Sjhb display_mbr(boot0); 26163092Sjhb 26263092Sjhb /* clean up */ 26363092Sjhb if (mbr != boot0) 26463092Sjhb free(boot0); 26563092Sjhb free(mbr); 26687643Smikeh free(disk); 26763092Sjhb 26863092Sjhb return 0; 26963092Sjhb} 27063092Sjhb 271185579Sluigi/* get or set the 'bell' character to be used in case of errors. 272185579Sluigi * Lookup for a certain code sequence, return -1 if not found. 273185579Sluigi */ 274185579Sluigistatic int 275185579Sluigiset_bell(u_int8_t *mbr, int new_bell, int report) 276185579Sluigi{ 277185579Sluigi /* lookup sequence: 0x100 means skip, 0x200 means done */ 278185579Sluigi static unsigned seq[] = 279185579Sluigi { 0xb0, 0x100, 0xe8, 0x100, 0x100, 0x30, 0xe4, 0x200 }; 280185579Sluigi int ofs, i, c; 281185579Sluigi for (ofs = 0x60; ofs < 0x180; ofs++) { /* search range */ 282185579Sluigi if (mbr[ofs] != seq[0]) /* search initial pattern */ 283185579Sluigi continue; 284185579Sluigi for (i=0;; i++) { 285185579Sluigi if (seq[i] == 0x200) { /* found */ 286185579Sluigi c = mbr[ofs+1]; 287185579Sluigi if (!report) 288185579Sluigi mbr[ofs+1] = c = new_bell; 289185579Sluigi else 290185579Sluigi printf(" bell=%c (0x%x)", 291185579Sluigi (c >= ' ' && c < 0x7f) ? c : ' ', c); 292185579Sluigi return c; 293185579Sluigi } 294185579Sluigi if (seq[i] != 0x100 && seq[i] != mbr[ofs+i]) 295185579Sluigi break; 296185579Sluigi } 297185579Sluigi } 298185579Sluigi warn("bell not found"); 299185579Sluigi return -1; 300185579Sluigi} 30163092Sjhb/* 30263092Sjhb * Read in the MBR of the disk. If it is boot0, then use the version to 30363092Sjhb * read in all of it if necessary. Use pointers to return a malloc'd 30463092Sjhb * buffer containing the MBR and then return its size. 30563092Sjhb */ 30663092Sjhbstatic int 30763092Sjhbread_mbr(const char *disk, u_int8_t **mbr, int check_version) 30863092Sjhb{ 30963092Sjhb u_int8_t buf[MBRSIZE]; 31063092Sjhb int mbr_size, fd; 311185579Sluigi int ver; 31263092Sjhb ssize_t n; 31363092Sjhb 31463092Sjhb if ((fd = open(disk, O_RDONLY)) == -1) 315108394Sphk err(1, "open %s", disk); 31663092Sjhb if ((n = read(fd, buf, MBRSIZE)) == -1) 317108394Sphk err(1, "read %s", disk); 31863092Sjhb if (n != MBRSIZE) 31963092Sjhb errx(1, "%s: short read", disk); 32063092Sjhb if (cv2(buf + OFF_MAGIC) != 0xaa55) 32163092Sjhb errx(1, "%s: bad magic", disk); 32263092Sjhb 323185579Sluigi if (! (ver = boot0bs(buf))) { 32463092Sjhb if (check_version) 32563092Sjhb errx(1, "%s: unknown or incompatible boot code", disk); 32663092Sjhb } else if (boot0version(buf) == 0x101) { 32763092Sjhb mbr_size = 1024; 32863092Sjhb if ((*mbr = malloc(mbr_size)) == NULL) 32963092Sjhb errx(1, "%s: unable to allocate read buffer", disk); 33063092Sjhb if (lseek(fd, 0, SEEK_SET) == -1 || 33163092Sjhb (n = read(fd, *mbr, mbr_size)) == -1) 33263092Sjhb err(1, "%s", disk); 33363092Sjhb if (n != mbr_size) 33463092Sjhb errx(1, "%s: short read", disk); 33563092Sjhb return (mbr_size); 33644196Srnordier } 33763092Sjhb *mbr = malloc(sizeof(buf)); 33863092Sjhb memcpy(*mbr, buf, sizeof(buf)); 33963092Sjhb 34063092Sjhb return sizeof(buf); 34163092Sjhb} 34263092Sjhb 34363092Sjhb/* 34463092Sjhb * Write out the mbr to the specified file. 34563092Sjhb */ 34663092Sjhbstatic void 34763092Sjhbwrite_mbr(const char *fname, int flags, u_int8_t *mbr, int mbr_size) 34863092Sjhb{ 349189273Smarcel int fd; 35063092Sjhb ssize_t n; 351182844Slulf const char *errmsg; 352182844Slulf char *pname; 353148036Sphk struct gctl_req *grq; 354108394Sphk 355108394Sphk fd = open(fname, O_WRONLY | flags, 0666); 356108394Sphk if (fd != -1) { 357108394Sphk n = write(fd, mbr, mbr_size); 358108394Sphk close(fd); 359108394Sphk if (n != mbr_size) 360108394Sphk errx(1, "%s: short write", fname); 361108394Sphk return; 362108394Sphk } 363148036Sphk 364189273Smarcel /* 365189273Smarcel * If we're called to write to a backup file, don't try to 366189273Smarcel * write through GEOM. It only generates additional errors. 367189273Smarcel */ 368189273Smarcel if (flags != 0) 369189273Smarcel return; 370189273Smarcel 371182844Slulf /* Try open it read only. */ 372182844Slulf fd = open(fname, O_RDONLY); 373182844Slulf if (fd == -1) { 374183487Slulf warn("error opening %s", fname); 375182844Slulf return; 376182844Slulf } 377182844Slulf pname = g_providername(fd); 378182844Slulf if (pname == NULL) { 379183487Slulf warn("error getting providername for %s", fname); 380182844Slulf return; 381182844Slulf } 382148036Sphk grq = gctl_get_handle(); 383189273Smarcel gctl_ro_param(grq, "class", -1, "PART"); 384226858Sae gctl_ro_param(grq, "arg0", -1, pname); 385189273Smarcel gctl_ro_param(grq, "verb", -1, "bootcode"); 386189273Smarcel gctl_ro_param(grq, "bootcode", mbr_size, mbr); 387189273Smarcel gctl_ro_param(grq, "flags", -1, "C"); 388189273Smarcel errmsg = gctl_issue(grq); 389189273Smarcel if (errmsg == NULL) 390189273Smarcel goto out; 391189273Smarcel 392189273Smarcel grq = gctl_get_handle(); 393148036Sphk gctl_ro_param(grq, "verb", -1, "write MBR"); 394148036Sphk gctl_ro_param(grq, "class", -1, "MBR"); 395182844Slulf gctl_ro_param(grq, "geom", -1, pname); 396148036Sphk gctl_ro_param(grq, "data", mbr_size, mbr); 397182844Slulf errmsg = gctl_issue(grq); 398189273Smarcel if (errmsg != NULL) 399189273Smarcel err(1, "write_mbr: %s", fname); 400189273Smarcel 401189273Smarcelout: 402182844Slulf free(pname); 403148036Sphk gctl_free(grq); 40463092Sjhb} 40563092Sjhb 40663092Sjhb/* 40763092Sjhb * Outputs an informative dump of the data in the MBR to stdout. 40863092Sjhb */ 40963092Sjhbstatic void 41063092Sjhbdisplay_mbr(u_int8_t *mbr) 41163092Sjhb{ 41263092Sjhb struct dos_partition *part; 41363092Sjhb int i, version; 41463092Sjhb 41563092Sjhb part = (struct dos_partition *)(mbr + DOSPARTOFF); 41663092Sjhb printf(fmt0); 41763092Sjhb for (i = 0; i < NDOSPART; i++) 41863092Sjhb if (part[i].dp_typ) 41963092Sjhb printf(fmt1, 1 + i, part[i].dp_flag, 42063092Sjhb part[i].dp_scyl + ((part[i].dp_ssect & 0xc0) << 2), 42163092Sjhb part[i].dp_shd, part[i].dp_ssect & 0x3f, part[i].dp_typ, 42263092Sjhb part[i].dp_ecyl + ((part[i].dp_esect & 0xc0) << 2), 42363092Sjhb part[i].dp_ehd, part[i].dp_esect & 0x3f, part[i].dp_start, 42463092Sjhb part[i].dp_size); 42563092Sjhb printf("\n"); 42663092Sjhb version = boot0version(mbr); 427185579Sluigi printf("version=%d.%d drive=0x%x mask=0x%x ticks=%u", 42863092Sjhb version >> 8, version & 0xff, mbr[OFF_DRIVE], 42963092Sjhb mbr[OFF_FLAGS] & 0xf, cv2(mbr + OFF_TICKS)); 430185579Sluigi set_bell(mbr, 0, 1); 431185579Sluigi printf("\noptions="); 43263092Sjhb for (i = 0; i < nopt; i++) { 43363092Sjhb if (i) 43463092Sjhb printf(","); 43563092Sjhb if (!(mbr[OFF_FLAGS] & 1 << (7 - i)) ^ opttbl[i].def) 43663092Sjhb printf("no"); 43763092Sjhb printf("%s", opttbl[i].tok); 43844196Srnordier } 43963092Sjhb printf("\n"); 440185579Sluigi if (b0_ver == 2) 441185579Sluigi printf("volume serial ID %02x%02x-%02x%02x\n", 442185579Sluigi mbr[OFF_SERIAL], mbr[OFF_SERIAL+1], 443185579Sluigi mbr[OFF_SERIAL+2], mbr[OFF_SERIAL+3]); 44464797Sdwmalone printf("default_selection=F%d (", mbr[OFF_OPT] + 1); 44564797Sdwmalone if (mbr[OFF_OPT] < 4) 44664797Sdwmalone printf("Slice %d", mbr[OFF_OPT] + 1); 447230065Sjhb else if (mbr[OFF_OPT] == 4) 448230065Sjhb printf("Drive 1"); 44964797Sdwmalone else 450230065Sjhb printf("PXE"); 45164797Sdwmalone printf(")\n"); 45244196Srnordier} 45344196Srnordier 45448113Srnordier/* 45563034Sjhb * Return the boot0 version with the minor revision in the low byte, and 45663034Sjhb * the major revision in the next higher byte. 45763034Sjhb */ 45863034Sjhbstatic int 45963034Sjhbboot0version(const u_int8_t *bs) 46063034Sjhb{ 46163034Sjhb /* Check for old version, and return 0x100 if found. */ 462185579Sluigi int v = boot0bs(bs); 463185579Sluigi if (v != 0) 464185579Sluigi return v << 8; 46563034Sjhb 46663034Sjhb /* We have a newer boot0, so extract the version number and return it. */ 46787643Smikeh return *(const int *)(bs + OFF_VERSION) & 0xffff; 46863034Sjhb} 46963034Sjhb 470185579Sluigi/* descriptor of a pattern to match. 471185579Sluigi * Start from the first entry trying to match the chunk of bytes, 472185579Sluigi * if you hit an entry with len=0 terminate the search and report 473185579Sluigi * off as the version. Otherwise skip to the next block after len=0 474185579Sluigi * An entry with len=0, off=0 is the end marker. 475185579Sluigi */ 476185579Sluigistruct byte_pattern { 477185579Sluigi unsigned off; 478185579Sluigi unsigned len; 479185579Sluigi u_int8_t *key; 480185579Sluigi}; 481185579Sluigi 48263034Sjhb/* 48348113Srnordier * Decide if we have valid boot0 boot code by looking for 48448113Srnordier * characteristic byte sequences at fixed offsets. 48548113Srnordier */ 48648113Srnordierstatic int 48748113Srnordierboot0bs(const u_int8_t *bs) 48848113Srnordier{ 489185579Sluigi /* the initial code sequence */ 49063034Sjhb static u_int8_t id0[] = {0xfc, 0x31, 0xc0, 0x8e, 0xc0, 0x8e, 0xd8, 49163034Sjhb 0x8e, 0xd0, 0xbc, 0x00, 0x7c }; 492185579Sluigi /* the drive id */ 49348113Srnordier static u_int8_t id1[] = {'D', 'r', 'i', 'v', 'e', ' '}; 494185579Sluigi static struct byte_pattern patterns[] = { 49563034Sjhb {0x0, sizeof(id0), id0}, 496185579Sluigi {0x1b2, sizeof(id1), id1}, 497185579Sluigi {1, 0, NULL}, 498185579Sluigi {0x0, sizeof(id0), id0}, /* version with NT support */ 499185579Sluigi {0x1ae, sizeof(id1), id1}, 500185579Sluigi {2, 0, NULL}, 501185579Sluigi {0, 0, NULL}, 50248113Srnordier }; 503185579Sluigi struct byte_pattern *p = patterns; 50448113Srnordier 505185579Sluigi for (; p->off || p->len; p++) { 506185579Sluigi if (p->len == 0) 507185579Sluigi break; 508185579Sluigi if (!memcmp(bs + p->off, p->key, p->len)) /* match */ 509185579Sluigi continue; 510185579Sluigi while (p->len) /* skip to next block */ 511185579Sluigi p++; 512185579Sluigi } 513185579Sluigi b0_ver = p->off; /* XXX ugly side effect */ 514185579Sluigi return p->off; 515129302Sstefanf} 51648113Srnordier 51748113Srnordier/* 51848113Srnordier * Adjust "and" and "or" masks for a -o option argument. 51948113Srnordier */ 52044196Srnordierstatic void 52144196Srnordierstropt(const char *arg, int *xa, int *xo) 52244196Srnordier{ 52344196Srnordier const char *q; 52444196Srnordier char *s, *s1; 52544196Srnordier int inv, i, x; 52644196Srnordier 52744196Srnordier if (!(s = strdup(arg))) 52844196Srnordier err(1, NULL); 52944196Srnordier for (s1 = s; (q = strtok(s1, ",")); s1 = NULL) { 53044196Srnordier if ((inv = !strncmp(q, "no", 2))) 53144196Srnordier q += 2; 53244196Srnordier for (i = 0; i < nopt; i++) 53344196Srnordier if (!strcmp(q, opttbl[i].tok)) 53444196Srnordier break; 53544196Srnordier if (i == nopt) 53644196Srnordier errx(1, "%s: Unknown -o option", q); 53744196Srnordier if (opttbl[i].def) 53844196Srnordier inv ^= 1; 53944196Srnordier x = 1 << (7 - i); 54044196Srnordier if (inv) 54144196Srnordier *xa &= ~x; 54244196Srnordier else 54344196Srnordier *xo |= x; 54444196Srnordier } 54544196Srnordier free(s); 54644196Srnordier} 54744196Srnordier 54848113Srnordier/* 54948113Srnordier * Convert and check an option argument. 55048113Srnordier */ 55144196Srnordierstatic int 55244196Srnordierargtoi(const char *arg, int lo, int hi, int opt) 55344196Srnordier{ 55444196Srnordier char *s; 55544196Srnordier long x; 55644196Srnordier 55744196Srnordier errno = 0; 55844196Srnordier x = strtol(arg, &s, 0); 55944196Srnordier if (errno || !*arg || *s || x < lo || x > hi) 56044196Srnordier errx(1, "%s: Bad argument to -%c option", arg, opt); 56144196Srnordier return x; 56244196Srnordier} 56344196Srnordier 56448113Srnordier/* 56548113Srnordier * Display usage information. 56648113Srnordier */ 56744196Srnordierstatic void 56844196Srnordierusage(void) 56944196Srnordier{ 57044196Srnordier fprintf(stderr, "%s\n%s\n", 57148038Srnordier "usage: boot0cfg [-Bv] [-b boot0] [-d drive] [-f file] [-m mask]", 57274668Siedowse " [-o options] [-s slice] [-t ticks] disk"); 57344196Srnordier exit(1); 57444196Srnordier} 575