2 * Copyright (c) 2008 John Hay
3 * Copyright (c) 2006 Warner Losh
4 * Copyright (c) 1998 Robert Nordier
7 * Redistribution and use in source and binary forms are freely
8 * permitted provided that the above copyright notice and this
9 * paragraph and the following disclaimer are duplicated in all
12 * This software is provided "AS IS" and without any express or
13 * implied warranties, including, without limitation, the implied
14 * warranties of merchantability and fitness for a particular
18 #include <sys/cdefs.h>
19 __FBSDID("$FreeBSD$");
21 #include <sys/param.h>
22 #include <sys/disklabel.h>
23 #include <sys/diskmbr.h>
24 #include <sys/dirent.h>
25 #include <sys/reboot.h>
27 #include <machine/elf.h>
34 #define RBX_ASKNAME 0x0 /* -a */
35 #define RBX_SINGLE 0x1 /* -s */
36 /* 0x2 is reserved for log2(RB_NOSYNC). */
37 /* 0x3 is reserved for log2(RB_HALT). */
38 /* 0x4 is reserved for log2(RB_INITNAME). */
39 #define RBX_DFLTROOT 0x5 /* -r */
40 /* #define RBX_KDB 0x6 -d */
41 /* 0x7 is reserved for log2(RB_RDONLY). */
42 /* 0x8 is reserved for log2(RB_DUMP). */
43 /* 0x9 is reserved for log2(RB_MINIROOT). */
44 #define RBX_CONFIG 0xa /* -c */
45 #define RBX_VERBOSE 0xb /* -v */
46 /* #define RBX_SERIAL 0xc -h */
47 /* #define RBX_CDROM 0xd -C */
48 /* 0xe is reserved for log2(RB_POWEROFF). */
49 #define RBX_GDB 0xf /* -g */
50 /* #define RBX_MUTE 0x10 -m */
51 /* 0x11 is reserved for log2(RB_SELFTEST). */
52 /* 0x12 is reserved for boot programs. */
53 /* 0x13 is reserved for boot programs. */
54 /* #define RBX_PAUSE 0x14 -p */
55 /* #define RBX_QUIET 0x15 -q */
56 #define RBX_NOINTR 0x1c /* -n */
57 /* 0x1d is reserved for log2(RB_MULTIPLE) and is just misnamed here. */
58 /* #define RBX_DUAL 0x1d -D */
59 /* 0x1f is reserved for log2(RB_BOOTINFO). */
61 /* pass: -a, -s, -r, -v, -g */
62 #define RBX_MASK (OPT_SET(RBX_ASKNAME) | OPT_SET(RBX_SINGLE) | \
63 OPT_SET(RBX_DFLTROOT) | \
64 OPT_SET(RBX_VERBOSE) | \
67 #define PATH_CONFIG "/boot.config"
68 //#define PATH_KERNEL "/boot/kernel/kernel"
69 #define PATH_KERNEL "/boot/kernel/kernel.gz.tramp"
75 #define OPT_SET(opt) (1 << (opt))
76 #define OPT_CHECK(opt) ((opts) & OPT_SET(opt))
78 static const char optstr[NOPT] = "agnrsv";
79 static const unsigned char flags[NOPT] = {
90 static char kname[1024];
94 static void load(void);
95 static int parse(void);
96 static int xfsread(ino_t, void *, size_t);
97 static int dskread(void *, unsigned, unsigned);
99 static void fixup_boot_drv(caddr_t, int, int, int);
102 #define UFS_SMALL_CGBASE
106 #define DPRINTF(fmt, ...) printf(fmt, __VA_ARGS__)
108 #define DPRINTF(fmt, ...)
112 xfsread(ino_t inode, void *buf, size_t nbyte)
114 if ((size_t)fsread(inode, buf, nbyte) != nbyte)
143 if (s - cmd < sizeof(cmd) - 1)
157 dmadat = (void *)(0x20000000 + (16 << 20));
162 /* Process configuration file */
163 if ((ino = lookup(PATH_CONFIG)))
164 fsread(ino, cmd, sizeof(cmd));
169 printf("%s: %s\n", PATH_CONFIG, cmd);
170 /* Do not process this command twice */
175 strcpy(kname, PATH_KERNEL);
177 /* Present the user with the boot2 prompt. */
179 printf("\nDefault: %s\nboot: ", kname);
181 (OPT_CHECK(RBX_NOINTR) == 0 && (c = getc(2)) != 0))
197 static Elf32_Phdr ep[2];
202 #ifdef FIXUP_BOOT_DRV
206 staddr = (caddr_t)0xffffffff;
209 if (!(ino = lookup(kname))) {
211 printf("No %s\n", kname);
214 if (xfsread(ino, &eh, sizeof(eh)))
217 printf("Invalid %s\n", "format");
221 for (j = i = 0; i < eh.e_phnum && j < 2; i++) {
222 if (xfsread(ino, ep + j, sizeof(ep[0])))
224 if (ep[j].p_type == PT_LOAD)
227 for (i = 0; i < 2; i++) {
228 p = (caddr_t)ep[i].p_paddr;
229 fs_off = ep[i].p_offset;
230 #ifdef FIXUP_BOOT_DRV
231 if (staddr == (caddr_t)0xffffffff)
233 klen += ep[i].p_filesz;
235 if (xfsread(ino, p, ep[i].p_filesz))
239 #ifdef FIXUP_BOOT_DRV
240 fixup_boot_drv(staddr, klen, bootslice, bootpart);
242 ((void(*)(int))addr)(opts & RBX_MASK);
252 while ((c = *arg++)) {
253 if (c == ' ' || c == '\t' || c == '\n')
255 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
260 while ((c = *arg++)) {
261 for (i = 0; c != optstr[i]; i++)
264 opts ^= OPT_SET(flags[i]);
268 if ((i = ep - arg)) {
269 if ((size_t)i >= sizeof(kname))
271 memcpy(kname, arg, i + 1);
280 dskread(void *buf, unsigned lba, unsigned nblk)
282 struct dos_partition *dp;
288 sec = dmadat->secbuf;
290 if (drvread(sec, DOSBBSECTOR, 1))
292 dp = (void *)(sec + DOSPARTOFF);
293 for (i = 0; i < NDOSPART; i++) {
294 if (dp[i].dp_typ == DOSPTYP_386BSD)
300 * Although dp_start is aligned within the disk
301 * partition structure, DOSPARTOFF is 446, which is
302 * only word (2) aligned, not longword (4) aligned.
303 * Cope by using memcpy to fetch the start of this
306 memcpy(&dsk_start, &dp[1].dp_start, 4);
307 if (drvread(sec, dsk_start + LABELSECTOR, 1))
309 d = (void *)(sec + LABELOFFSET);
310 if (d->d_magic != DISKMAGIC || d->d_magic2 != DISKMAGIC) {
311 printf("Invalid %s\n", "label");
314 if (!d->d_partitions[0].p_size) {
315 printf("Invalid %s\n", "partition");
318 dsk_start += d->d_partitions[0].p_offset;
319 dsk_start -= d->d_partitions[RAW_PART].p_offset;
322 return drvread(buf, dsk_start + lba, nblk);
325 #ifdef FIXUP_BOOT_DRV
327 * fixup_boot_drv() will try to find the ROOTDEVNAME spec in the kernel
328 * and change it to what was specified on the comandline or /boot.conf
329 * file or to what was encountered on the disk. It will try to handle 3
330 * different disk layouts, raw (dangerously dedicated), slice only and
331 * slice + partition. It will look for the following strings in the
332 * kernel, but if it is one of the first three, the string in the kernel
333 * must use the correct form to match the actual disk layout:
338 * In the case of the first three strings, only the "a" at the end and
339 * the "1" after the "s" will be modified, if they exist. The string
340 * length will not be changed. In the case of the last string, the
341 * whole string will be built up and nul, '\0' terminated.
344 fixup_boot_drv(caddr_t addr, int klen, int bs, int bp)
346 const u_int8_t op[] = "ufs:ROOTDEVNAME";
347 const u_int8_t op2[] = "ufs:ad0";
350 DPRINTF("fixup_boot_drv: 0x%x, %d, slice %d, partition %d\n",
351 (int)addr, klen, bs, bp);
356 ps = memmem(addr, klen, op, sizeof(op));
358 p = ps + 4; /* past ufs: */
359 DPRINTF("Found it at 0x%x\n", (int)ps);
360 p[0] = 'a'; p[1] = 'd'; p[2] = '0'; /* ad0 */
367 if (disk_layout != DL_SLICE) {
368 /* append partition */
373 ps = memmem(addr, klen, op2, sizeof(op2) - 1);
375 p = ps + sizeof(op2) - 1;
376 DPRINTF("Found it at 0x%x\n", (int)ps);
387 printf("Could not locate \"%s\" to fix kernel boot device, "
388 "check ROOTDEVNAME is set\n", op);
391 DPRINTF("Changed boot device to %s\n", ps);