boot0cfg.c revision 48038
1/* 2 * Copyright (c) 1999 Robert Nordier 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS 18 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 19 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 20 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 21 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 22 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 23 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 24 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#ifndef lint 28static const char rcsid[] = 29 "$Id: boot0cfg.c,v 1.3 1999/02/26 14:57:17 rnordier Exp $"; 30#endif /* not lint */ 31 32#include <sys/param.h> 33#include <sys/disklabel.h> 34#include <sys/stat.h> 35 36#include <err.h> 37#include <errno.h> 38#include <fcntl.h> 39#include <paths.h> 40#include <stdio.h> 41#include <stdlib.h> 42#include <string.h> 43#include <unistd.h> 44 45#define MBRSIZE 512 /* master boot record size */ 46 47#define OFF_DRIVE 0x1ba /* offset: setdrv drive */ 48#define OFF_FLAGS 0x1bb /* offset: option flags */ 49#define OFF_TICKS 0x1bc /* offset: clock ticks */ 50#define OFF_PTBL 0x1be /* offset: partition table */ 51#define OFF_MAGIC 0x1fe /* offset: magic number */ 52 53#define cv2(p) ((p)[0] | (p)[1] << 010) 54 55#define mk2(p, x) \ 56 (p)[0] = (u_int8_t)(x), \ 57 (p)[1] = (u_int8_t)((x) >> 010) 58 59static const struct { 60 const char *tok; 61 int def; 62} opttbl[] = { 63 {"packet", 0}, 64 {"update", 1}, 65 {"setdrv", 0} 66}; 67static const int nopt = sizeof(opttbl) / sizeof(opttbl[0]); 68 69static const char fmt0[] = "# flag start chs type" 70 " end chs offset size\n"; 71 72static const char fmt1[] = "%d 0x%02x %4u:%3u:%2u 0x%02x" 73 " %4u:%3u:%2u %10u %10u\n"; 74 75static void stropt(const char *, int *, int *); 76static char *mkrdev(const char *); 77static int argtoi(const char *, int, int, int); 78static void usage(void); 79 80int 81main(int argc, char *argv[]) 82{ 83 u_int8_t buf[MBRSIZE]; 84 struct dos_partition part[4]; 85 const char *bpath, *fpath, *disk; 86 ssize_t n; 87 int B_flag, v_flag, o_flag; 88 int d_arg, m_arg, t_arg; 89 int o_and, o_or; 90 int fd, fd1, up, c, i; 91 92 bpath = "/boot/boot0"; 93 fpath = NULL; 94 B_flag = v_flag = o_flag = 0; 95 d_arg = m_arg = t_arg = -1; 96 o_and = 0xff; 97 o_or = 0; 98 while ((c = getopt(argc, argv, "Bvb:d:f:m:o:t:")) != -1) 99 switch (c) { 100 case 'B': 101 B_flag = 1; 102 break; 103 case 'v': 104 v_flag = 1; 105 break; 106 case 'b': 107 bpath = optarg; 108 break; 109 case 'd': 110 d_arg = argtoi(optarg, 0, 0xff, 'd'); 111 break; 112 case 'f': 113 fpath = optarg; 114 break; 115 case 'm': 116 m_arg = argtoi(optarg, 0, 0xf, 'm'); 117 break; 118 case 'o': 119 stropt(optarg, &o_and, &o_or); 120 o_flag = 1; 121 break; 122 case 't': 123 t_arg = argtoi(optarg, 1, 0xffff, 't'); 124 break; 125 default: 126 usage(); 127 } 128 argc -= optind; 129 argv += optind; 130 if (argc != 1) 131 usage(); 132 disk = mkrdev(*argv); 133 up = B_flag || d_arg != -1 || o_flag || t_arg != -1; 134 if ((fd = open(disk, up ? O_RDWR : O_RDONLY)) == -1) 135 err(1, "%s", disk); 136 if ((n = read(fd, buf, MBRSIZE)) == -1) 137 err(1, "%s", disk); 138 if (n != MBRSIZE) 139 errx(1, "%s: short read", disk); 140 if (cv2(buf + OFF_MAGIC) != 0xaa55) 141 errx(1, "%s: bad magic", disk); 142 if (fpath) { 143 if ((fd1 = open(fpath, O_WRONLY | O_CREAT | O_TRUNC, 144 0666)) == -1 || 145 (n = write(fd1, buf, MBRSIZE)) == -1 || close(fd1)) 146 err(1, "%s", fpath); 147 if (n != MBRSIZE) 148 errx(1, "%s: short write", fpath); 149 } 150 memcpy(part, buf + OFF_PTBL, sizeof(part)); 151 if (B_flag) { 152 if ((fd1 = open(bpath, O_RDONLY)) == -1 || 153 (n = read(fd1, buf, MBRSIZE)) == -1 || close(fd1)) 154 err(1, "%s", bpath); 155 if (n != MBRSIZE) 156 errx(1, "%s: short read", bpath); 157 if (cv2(buf + OFF_MAGIC) != 0xaa55) 158 errx(1, "%s: bad magic", bpath); 159 memcpy(buf + OFF_PTBL, part, sizeof(part)); 160 } 161 if (d_arg != -1) 162 buf[OFF_DRIVE] = d_arg; 163 if (m_arg != -1) { 164 buf[OFF_FLAGS] &= 0xf0; 165 buf[OFF_FLAGS] |= m_arg; 166 } 167 if (o_flag) { 168 buf[OFF_FLAGS] &= o_and; 169 buf[OFF_FLAGS] |= o_or; 170 } 171 if (t_arg != -1) 172 mk2(buf + OFF_TICKS, t_arg); 173 if (up) { 174 if (lseek(fd, 0, SEEK_SET) == -1 || 175 (n = write(fd, buf, MBRSIZE)) == -1 || close(fd)) 176 err(1, "%s", disk); 177 if (n != MBRSIZE) 178 errx(1, "%s: short write", disk); 179 } 180 if (v_flag) { 181 printf(fmt0); 182 for (i = 0; i < 4; i++) 183 if (part[i].dp_typ) { 184 printf(fmt1, 185 1 + i, 186 part[i].dp_flag, 187 part[i].dp_scyl + ((part[i].dp_ssect & 0xc0) << 2), 188 part[i].dp_shd, 189 part[i].dp_ssect & 0x3f, 190 part[i].dp_typ, 191 part[i].dp_ecyl + ((part[i].dp_esect & 0xc0) << 2), 192 part[i].dp_ehd, 193 part[i].dp_esect & 0x3f, 194 part[i].dp_start, 195 part[i].dp_size); 196 } 197 printf("\n"); 198 printf("drive=0x%x mask=0x%x options=", buf[OFF_DRIVE], 199 buf[OFF_FLAGS] & 0xf); 200 for (i = 0; i < nopt; i++) { 201 if (i) 202 printf(","); 203 if (!(buf[OFF_FLAGS] & 1 << (7 - i)) ^ opttbl[i].def) 204 printf("no"); 205 printf("%s", opttbl[i].tok); 206 } 207 printf(" ticks=%u\n", cv2(buf + OFF_TICKS)); 208 } 209 return 0; 210} 211 212static void 213stropt(const char *arg, int *xa, int *xo) 214{ 215 const char *q; 216 char *s, *s1; 217 int inv, i, x; 218 219 if (!(s = strdup(arg))) 220 err(1, NULL); 221 for (s1 = s; (q = strtok(s1, ",")); s1 = NULL) { 222 if ((inv = !strncmp(q, "no", 2))) 223 q += 2; 224 for (i = 0; i < nopt; i++) 225 if (!strcmp(q, opttbl[i].tok)) 226 break; 227 if (i == nopt) 228 errx(1, "%s: Unknown -o option", q); 229 if (opttbl[i].def) 230 inv ^= 1; 231 x = 1 << (7 - i); 232 if (inv) 233 *xa &= ~x; 234 else 235 *xo |= x; 236 } 237 free(s); 238} 239 240static char * 241mkrdev(const char *fname) 242{ 243 char buf[MAXPATHLEN]; 244 struct stat sb; 245 char *s; 246 247 s = (char *) fname; 248 if (!strchr(fname, '/')) { 249 snprintf(buf, sizeof(buf), "%sr%s", _PATH_DEV, fname); 250 if (stat(buf, &sb)) 251 snprintf(buf, sizeof(buf), "%s%s", _PATH_DEV, fname); 252 if (!(s = strdup(buf))) 253 err(1, NULL); 254 } 255 return s; 256} 257 258static int 259argtoi(const char *arg, int lo, int hi, int opt) 260{ 261 char *s; 262 long x; 263 264 errno = 0; 265 x = strtol(arg, &s, 0); 266 if (errno || !*arg || *s || x < lo || x > hi) 267 errx(1, "%s: Bad argument to -%c option", arg, opt); 268 return x; 269} 270 271static void 272usage(void) 273{ 274 fprintf(stderr, "%s\n%s\n", 275 "usage: boot0cfg [-Bv] [-b boot0] [-d drive] [-f file] [-m mask]", 276 " [-o options] [-t ticks] disk"); 277 exit(1); 278} 279