fsck_msdosfs: check next cluster index for validity before using it
[dragonfly.git] / usr.sbin / boot0cfg / boot0cfg.c
blob53e7e95ea5d552012355bd07f9bb52caf0cea818
1 /*
2 * Copyright (c) 1999 Robert Nordier
3 * All rights reserved.
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.
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.
26 * $FreeBSD: src/usr.sbin/boot0cfg/boot0cfg.c,v 1.7.2.4 2002/03/16 01:06:51 mikeh Exp $
27 * $DragonFly: src/usr.sbin/boot0cfg/boot0cfg.c,v 1.6 2008/03/24 23:04:19 swildner Exp $
30 #include <sys/param.h>
31 #include <sys/diskmbr.h>
32 #include <sys/stat.h>
34 #include <err.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <paths.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
43 #define MBRSIZE 512 /* master boot record size */
45 #define OFF_VERSION 0x1b0 /* offset: version number */
46 #define OFF_OPT 0x1b9 /* offset: default boot option */
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 */
53 #define cv2(p) ((p)[0] | (p)[1] << 010)
55 #define mk2(p, x) \
56 (p)[0] = (u_int8_t)(x), \
57 (p)[1] = (u_int8_t)((x) >> 010)
59 static const struct {
60 const char *tok;
61 int def;
62 } opttbl[] = {
63 {"packet", 0},
64 {"update", 1},
65 {"setdrv", 0}
67 static const int nopt = sizeof(opttbl) / sizeof(opttbl[0]);
69 static const char fmt0[] = "# flag start chs type"
70 " end chs offset size\n";
72 static const char fmt1[] = "%d 0x%02x %4u:%3u:%2u 0x%02x"
73 " %4u:%3u:%2u %10u %10u\n";
75 static int read_mbr(const char *, u_int8_t **, int);
76 static void write_mbr(const char *, int, u_int8_t *, int);
77 static void display_mbr(u_int8_t *);
78 static int boot0version(const u_int8_t *);
79 static int boot0bs(const u_int8_t *);
80 static void stropt(const char *, int *, int *);
81 static char *mkrdev(const char *);
82 static int argtoi(const char *, int, int, int);
83 static void usage(void);
86 * Boot manager installation/configuration utility.
88 int
89 main(int argc, char *argv[])
91 u_int8_t *mbr, *boot0;
92 int boot0_size, mbr_size;
93 const char *bpath, *fpath;
94 char *disk;
95 int B_flag, v_flag, o_flag;
96 int d_arg, m_arg, s_arg, t_arg;
97 int o_and, o_or;
98 int up, c;
100 bpath = "/boot/boot0";
101 fpath = NULL;
102 B_flag = v_flag = o_flag = 0;
103 d_arg = m_arg = s_arg = t_arg = -1;
104 o_and = 0xff;
105 o_or = 0;
106 while ((c = getopt(argc, argv, "Bvb:d:f:m:o:s:t:")) != -1)
107 switch (c) {
108 case 'B':
109 B_flag = 1;
110 break;
111 case 'v':
112 v_flag = 1;
113 break;
114 case 'b':
115 bpath = optarg;
116 break;
117 case 'd':
118 d_arg = argtoi(optarg, 0, 0xff, 'd');
119 break;
120 case 'f':
121 fpath = optarg;
122 break;
123 case 'm':
124 m_arg = argtoi(optarg, 0, 0xf, 'm');
125 break;
126 case 'o':
127 stropt(optarg, &o_and, &o_or);
128 o_flag = 1;
129 break;
130 case 's':
131 s_arg = argtoi(optarg, 1, 5, 's');
132 break;
133 case 't':
134 t_arg = argtoi(optarg, 1, 0xffff, 't');
135 break;
136 default:
137 usage();
139 argc -= optind;
140 argv += optind;
141 if (argc != 1)
142 usage();
143 disk = mkrdev(*argv);
144 up = B_flag || d_arg != -1 || m_arg != -1 || o_flag || s_arg != -1
145 || t_arg != -1;
147 /* open the disk and read in the existing mbr */
148 mbr_size = read_mbr(disk, &mbr, !B_flag);
150 /* save the existing MBR if we are asked to do so */
151 if (fpath)
152 write_mbr(fpath, O_CREAT | O_TRUNC, mbr, mbr_size);
155 * If we are installing the boot loader, read it from disk and copy the
156 * slice table over from the existing MBR. If not, then point boot0
157 * back at the MBR we just read in. After this, boot0 is the data to
158 * write back to disk if we are going to do a write.
160 if (B_flag) {
161 boot0_size = read_mbr(bpath, &boot0, 1);
162 memcpy(boot0 + OFF_PTBL, mbr + OFF_PTBL,
163 sizeof(struct dos_partition) * NDOSPART);
164 } else {
165 boot0 = mbr;
166 boot0_size = mbr_size;
169 /* set the drive */
170 if (d_arg != -1)
171 boot0[OFF_DRIVE] = d_arg;
173 /* set various flags */
174 if (m_arg != -1) {
175 boot0[OFF_FLAGS] &= 0xf0;
176 boot0[OFF_FLAGS] |= m_arg;
178 if (o_flag) {
179 boot0[OFF_FLAGS] &= o_and;
180 boot0[OFF_FLAGS] |= o_or;
183 /* set the default boot selection */
184 if (s_arg != -1)
185 boot0[OFF_OPT] = s_arg - 1;
187 /* set the timeout */
188 if (t_arg != -1)
189 mk2(boot0 + OFF_TICKS, t_arg);
191 /* write the MBR back to disk */
192 if (up)
193 write_mbr(disk, 0, boot0, boot0_size);
195 /* display the MBR */
196 if (v_flag)
197 display_mbr(boot0);
199 /* clean up */
200 if (mbr != boot0)
201 free(boot0);
202 free(mbr);
203 free(disk);
205 return 0;
209 * Read in the MBR of the disk. If it is boot0, then use the version to
210 * read in all of it if necessary. Use pointers to return a malloc'd
211 * buffer containing the MBR and then return its size.
213 static int
214 read_mbr(const char *disk, u_int8_t **mbr, int check_version)
216 u_int8_t buf[MBRSIZE];
217 int mbr_size, fd;
218 ssize_t n;
220 if ((fd = open(disk, O_RDONLY)) == -1)
221 err(1, "open %s", disk);
222 if ((n = read(fd, buf, MBRSIZE)) == -1)
223 err(1, "read %s", disk);
224 if (n != MBRSIZE)
225 errx(1, "%s: short read", disk);
226 if (cv2(buf + OFF_MAGIC) != 0xaa55)
227 errx(1, "%s: bad magic", disk);
229 if (!boot0bs(buf)) {
230 if (check_version)
231 errx(1, "%s: unknown or incompatible boot code", disk);
232 } else if (boot0version(buf) == 0x101) {
233 mbr_size = 1024;
234 if ((*mbr = malloc(mbr_size)) == NULL)
235 errx(1, "%s: unable to allocate read buffer", disk);
236 if (lseek(fd, 0, SEEK_SET) == -1 ||
237 (n = read(fd, *mbr, mbr_size)) == -1)
238 err(1, "%s", disk);
239 if (n != mbr_size)
240 errx(1, "%s: short read", disk);
241 return (mbr_size);
243 *mbr = malloc(sizeof(buf));
244 memcpy(*mbr, buf, sizeof(buf));
246 return sizeof(buf);
250 * Write out the mbr to the specified file.
252 static void
253 write_mbr(const char *fname, int flags, u_int8_t *mbr, int mbr_size)
255 int fd;
256 ssize_t n;
258 fd = open(fname, O_WRONLY | flags, 0666);
259 if (fd != -1) {
260 n = write(fd, mbr, mbr_size);
261 close(fd);
262 if (n != mbr_size)
263 errx(1, "%s: short write", fname);
264 return;
266 if (flags != 0)
267 err(1, "%s", fname);
268 err(1, "write_mbr: %s", fname);
272 * Outputs an informative dump of the data in the MBR to stdout.
274 static void
275 display_mbr(u_int8_t *mbr)
277 struct dos_partition *part;
278 int i, version;
280 part = (struct dos_partition *)(mbr + DOSPARTOFF);
281 printf(fmt0);
282 for (i = 0; i < NDOSPART; i++)
283 if (part[i].dp_typ)
284 printf(fmt1, 1 + i, part[i].dp_flag,
285 part[i].dp_scyl + ((part[i].dp_ssect & 0xc0) << 2),
286 part[i].dp_shd, part[i].dp_ssect & 0x3f, part[i].dp_typ,
287 part[i].dp_ecyl + ((part[i].dp_esect & 0xc0) << 2),
288 part[i].dp_ehd, part[i].dp_esect & 0x3f, part[i].dp_start,
289 part[i].dp_size);
290 printf("\n");
291 version = boot0version(mbr);
292 printf("version=%d.%d drive=0x%x mask=0x%x ticks=%u\noptions=",
293 version >> 8, version & 0xff, mbr[OFF_DRIVE],
294 mbr[OFF_FLAGS] & 0xf, cv2(mbr + OFF_TICKS));
295 for (i = 0; i < nopt; i++) {
296 if (i)
297 printf(",");
298 if (!(mbr[OFF_FLAGS] & 1 << (7 - i)) ^ opttbl[i].def)
299 printf("no");
300 printf("%s", opttbl[i].tok);
302 printf("\n");
303 printf("default_selection=F%d (", mbr[OFF_OPT] + 1);
304 if (mbr[OFF_OPT] < 4)
305 printf("Slice %d", mbr[OFF_OPT] + 1);
306 else
307 printf("Drive 1");
308 printf(")\n");
312 * Return the boot0 version with the minor revision in the low byte, and
313 * the major revision in the next higher byte.
315 static int
316 boot0version(const u_int8_t *bs)
318 static u_int8_t idold[] = {0xfe, 0x45, 0xf2, 0xe9, 0x00, 0x8a};
320 /* Check for old version, and return 0x100 if found. */
321 if (memcmp(bs + 0x1c, idold, sizeof(idold)) == 0)
322 return 0x100;
324 /* We have a newer boot0, so extract the version number and return it. */
325 return *(const int *)(bs + OFF_VERSION) & 0xffff;
329 * Decide if we have valid boot0 boot code by looking for
330 * characteristic byte sequences at fixed offsets.
332 static int
333 boot0bs(const u_int8_t *bs)
335 static u_int8_t id0[] = {0xfc, 0x31, 0xc0, 0x8e, 0xc0, 0x8e, 0xd8,
336 0x8e, 0xd0, 0xbc, 0x00, 0x7c };
337 static u_int8_t id1[] = {0xfc, 0xb8, 0x00, 0x09, 0x8e, 0xc0, 0x8e,
338 0xd0, 0xbc, 0x00, 0x10 ,0x31 };
339 static u_int8_t id2[] = {'D', 'r', 'i', 'v', 'e', ' '};
340 static struct {
341 unsigned off;
342 unsigned len;
343 u_int8_t *key;
344 } ident[] = {
345 {0x0, sizeof(id0), id0},
346 {0x0, sizeof(id1), id1},
347 {0x1b2, sizeof(id2), id2}
349 unsigned int i;
350 int count = 0;
352 for (i = 0; i < sizeof(ident) / sizeof(ident[0]); i++) {
353 if (memcmp(bs + ident[i].off, ident[i].key, ident[i].len) == 0)
354 ++count;
356 if (count >= 2)
357 return 1;
358 return 0;
362 * Adjust "and" and "or" masks for a -o option argument.
364 static void
365 stropt(const char *arg, int *xa, int *xo)
367 const char *q;
368 char *s, *s1;
369 int inv, i, x;
371 if (!(s = strdup(arg)))
372 err(1, NULL);
373 for (s1 = s; (q = strtok(s1, ",")); s1 = NULL) {
374 if ((inv = !strncmp(q, "no", 2)))
375 q += 2;
376 for (i = 0; i < nopt; i++)
377 if (!strcmp(q, opttbl[i].tok))
378 break;
379 if (i == nopt)
380 errx(1, "%s: Unknown -o option", q);
381 if (opttbl[i].def)
382 inv ^= 1;
383 x = 1 << (7 - i);
384 if (inv)
385 *xa &= ~x;
386 else
387 *xo |= x;
389 free(s);
393 * Produce a device path for a "canonical" name, where appropriate.
395 static char *
396 mkrdev(const char *fname)
398 char buf[MAXPATHLEN];
399 char *s;
401 if (!strchr(fname, '/')) {
402 snprintf(buf, sizeof(buf), "%s%s", _PATH_DEV, fname);
403 s = strdup(buf);
404 } else
405 s = strdup(fname);
407 if (s == NULL)
408 errx(1, "No more memory");
409 return s;
413 * Convert and check an option argument.
415 static int
416 argtoi(const char *arg, int lo, int hi, int opt)
418 char *s;
419 long x;
421 errno = 0;
422 x = strtol(arg, &s, 0);
423 if (errno || !*arg || *s || x < lo || x > hi)
424 errx(1, "%s: Bad argument to -%c option", arg, opt);
425 return x;
429 * Display usage information.
431 static void
432 usage(void)
434 fprintf(stderr, "%s\n%s\n",
435 "usage: boot0cfg [-Bv] [-b boot0] [-d drive] [-f file] [-m mask]",
436 " [-o options] [-s slice] [-t ticks] disk");
437 exit(1);