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_DOTCONFIG "/boot.config"
68 #define PATH_CONFIG "/boot/config"
69 //#define PATH_KERNEL "/boot/kernel/kernel"
70 #define PATH_KERNEL "/boot/kernel/kernel.gz.tramp"
76 #define OPT_SET(opt) (1 << (opt))
77 #define OPT_CHECK(opt) ((opts) & OPT_SET(opt))
79 static const char optstr[NOPT] = "agnrsv";
80 static const unsigned char flags[NOPT] = {
89 unsigned board_id; /* board type to pass to kernel, if set by board_* code */
92 static char kname[1024];
94 static uint8_t dsk_meta;
96 static void load(void);
97 static int parse(void);
98 static int dskread(void *, unsigned, unsigned);
100 static void fixup_boot_drv(caddr_t, int, int, int);
103 #define UFS_SMALL_CGBASE
107 #define DPRINTF(fmt, ...) printf(fmt, __VA_ARGS__)
109 #define DPRINTF(fmt, ...)
113 xfsread(ufs_ino_t inode, void *buf, size_t nbyte)
115 if ((size_t)fsread(inode, buf, nbyte) != nbyte)
144 if (s - cmd < sizeof(cmd) - 1)
158 dmadat = (void *)(0x20000000 + (16 << 20));
163 /* Process configuration file */
164 if ((ino = lookup(PATH_CONFIG)) ||
165 (ino = lookup(PATH_DOTCONFIG)))
166 fsread(ino, cmd, sizeof(cmd));
171 printf("%s: %s\n", PATH_CONFIG, cmd);
172 /* Do not process this command twice */
177 strcpy(kname, PATH_KERNEL);
179 /* Present the user with the boot2 prompt. */
181 printf("\nDefault: %s\nboot: ", kname);
183 (OPT_CHECK(RBX_NOINTR) == 0 && (c = getc(2)) != 0))
199 static Elf32_Phdr ep[2];
204 #ifdef FIXUP_BOOT_DRV
208 staddr = (caddr_t)0xffffffff;
211 if (!(ino = lookup(kname))) {
213 printf("No %s\n", kname);
216 if (xfsread(ino, &eh, sizeof(eh)))
219 printf("Invalid %s\n", "format");
223 for (j = i = 0; i < eh.e_phnum && j < 2; i++) {
224 if (xfsread(ino, ep + j, sizeof(ep[0])))
226 if (ep[j].p_type == PT_LOAD)
229 for (i = 0; i < 2; i++) {
230 p = (caddr_t)ep[i].p_paddr;
231 fs_off = ep[i].p_offset;
232 #ifdef FIXUP_BOOT_DRV
233 if (staddr == (caddr_t)0xffffffff)
235 klen += ep[i].p_filesz;
237 if (xfsread(ino, p, ep[i].p_filesz))
241 #ifdef FIXUP_BOOT_DRV
242 fixup_boot_drv(staddr, klen, bootslice, bootpart);
244 ((void(*)(int, int, int, int))addr)(opts & RBX_MASK, board_id, 0, 0);
254 while ((c = *arg++)) {
255 if (c == ' ' || c == '\t' || c == '\n')
257 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
262 while ((c = *arg++)) {
263 for (i = 0; c != optstr[i]; i++)
266 opts ^= OPT_SET(flags[i]);
270 if ((i = ep - arg)) {
271 if ((size_t)i >= sizeof(kname))
273 memcpy(kname, arg, i + 1);
282 dskread(void *buf, unsigned lba, unsigned nblk)
284 struct dos_partition *dp;
290 sec = dmadat->secbuf;
292 if (drvread(sec, DOSBBSECTOR, 1))
294 dp = (void *)(sec + DOSPARTOFF);
295 for (i = 0; i < NDOSPART; i++) {
296 if (dp[i].dp_typ == DOSPTYP_386BSD)
302 * Although dp_start is aligned within the disk
303 * partition structure, DOSPARTOFF is 446, which is
304 * only word (2) aligned, not longword (4) aligned.
305 * Cope by using memcpy to fetch the start of this
308 memcpy(&dsk_start, &dp[1].dp_start, 4);
309 if (drvread(sec, dsk_start + LABELSECTOR, 1))
311 d = (void *)(sec + LABELOFFSET);
312 if (d->d_magic != DISKMAGIC || d->d_magic2 != DISKMAGIC) {
313 printf("Invalid %s\n", "label");
316 if (!d->d_partitions[0].p_size) {
317 printf("Invalid %s\n", "partition");
320 dsk_start += d->d_partitions[0].p_offset;
321 dsk_start -= d->d_partitions[RAW_PART].p_offset;
324 return drvread(buf, dsk_start + lba, nblk);
327 #ifdef FIXUP_BOOT_DRV
329 * fixup_boot_drv() will try to find the ROOTDEVNAME spec in the kernel
330 * and change it to what was specified on the comandline or /boot.conf
331 * file or to what was encountered on the disk. It will try to handle 3
332 * different disk layouts, raw (dangerously dedicated), slice only and
333 * slice + partition. It will look for the following strings in the
334 * kernel, but if it is one of the first three, the string in the kernel
335 * must use the correct form to match the actual disk layout:
340 * In the case of the first three strings, only the "a" at the end and
341 * the "1" after the "s" will be modified, if they exist. The string
342 * length will not be changed. In the case of the last string, the
343 * whole string will be built up and nul, '\0' terminated.
346 fixup_boot_drv(caddr_t addr, int klen, int bs, int bp)
348 const u_int8_t op[] = "ufs:ROOTDEVNAME";
349 const u_int8_t op2[] = "ufs:ad0";
352 DPRINTF("fixup_boot_drv: 0x%x, %d, slice %d, partition %d\n",
353 (int)addr, klen, bs, bp);
358 ps = memmem(addr, klen, op, sizeof(op));
360 p = ps + 4; /* past ufs: */
361 DPRINTF("Found it at 0x%x\n", (int)ps);
362 p[0] = 'a'; p[1] = 'd'; p[2] = '0'; /* ad0 */
369 if (disk_layout != DL_SLICE) {
370 /* append partition */
375 ps = memmem(addr, klen, op2, sizeof(op2) - 1);
377 p = ps + sizeof(op2) - 1;
378 DPRINTF("Found it at 0x%x\n", (int)ps);
389 printf("Could not locate \"%s\" to fix kernel boot device, "
390 "check ROOTDEVNAME is set\n", op);
393 DPRINTF("Changed boot device to %s\n", ps);