2 * Copyright (c) 1998 Robert Nordier
5 * Redistribution and use in source and binary forms are freely
6 * permitted provided that the above copyright notice and this
7 * paragraph and the following disclaimer are duplicated in all
10 * This software is provided "AS IS" and without any express or
11 * implied warranties, including, without limitation, the implied
12 * warranties of merchantability and fitness for a particular
16 #include <sys/cdefs.h>
17 __FBSDID("$FreeBSD$");
19 #include <sys/param.h>
20 #include <sys/disklabel.h>
21 #include <sys/diskmbr.h>
22 #include <sys/dirent.h>
23 #include <sys/reboot.h>
25 #include <machine/elf.h>
32 #define RBX_ASKNAME 0x0 /* -a */
33 #define RBX_SINGLE 0x1 /* -s */
34 /* 0x2 is reserved for log2(RB_NOSYNC). */
35 /* 0x3 is reserved for log2(RB_HALT). */
36 /* 0x4 is reserved for log2(RB_INITNAME). */
37 #define RBX_DFLTROOT 0x5 /* -r */
38 /* #define RBX_KDB 0x6 -d */
39 /* 0x7 is reserved for log2(RB_RDONLY). */
40 /* 0x8 is reserved for log2(RB_DUMP). */
41 /* 0x9 is reserved for log2(RB_MINIROOT). */
42 #define RBX_CONFIG 0xa /* -c */
43 #define RBX_VERBOSE 0xb /* -v */
44 /* #define RBX_SERIAL 0xc -h */
45 /* #define RBX_CDROM 0xd -C */
46 /* 0xe is reserved for log2(RB_POWEROFF). */
47 #define RBX_GDB 0xf /* -g */
48 /* #define RBX_MUTE 0x10 -m */
49 /* 0x11 is reserved for log2(RB_SELFTEST). */
50 /* 0x12 is reserved for boot programs. */
51 /* 0x13 is reserved for boot programs. */
52 /* #define RBX_PAUSE 0x14 -p */
53 /* #define RBX_QUIET 0x15 -q */
54 /* #define RBX_NOINTR 0x1c -n */
55 /* 0x1d is reserved for log2(RB_MULTIPLE) and is just misnamed here. */
56 /* #define RBX_DUAL 0x1d -D */
57 /* 0x1f is reserved for log2(RB_BOOTINFO). */
59 /* pass: -a, -s, -r, -v, -g */
60 #define RBX_MASK (OPT_SET(RBX_ASKNAME) | OPT_SET(RBX_SINGLE) | \
61 OPT_SET(RBX_DFLTROOT) | \
62 OPT_SET(RBX_VERBOSE) | \
65 #define PATH_CONFIG "/boot.config"
66 //#define PATH_KERNEL "/boot/kernel/kernel"
67 #define PATH_KERNEL "/boot/kernel/kernel.gz.tramp"
71 #define OPT_SET(opt) (1 << (opt))
72 #define OPT_CHECK(opt) ((opts) & OPT_SET(opt))
76 static const char optstr[NOPT] = "agrsv";
77 static const unsigned char flags[NOPT] = {
87 static char kname[1024];
91 static void load(void);
92 static int parse(void);
93 static int xfsread(ino_t, void *, size_t);
94 static int dskread(void *, unsigned, unsigned);
96 #define UFS_SMALL_CGBASE
100 xfsread(ino_t inode, void *buf, size_t nbyte)
102 if ((size_t)fsread(inode, buf, nbyte) != nbyte)
131 if (s - cmd < sizeof(cmd) - 1)
147 dmadat = (void *)(0x20000000 + (16 << 20));
148 /* Process configuration file */
152 if ((ino = lookup(PATH_CONFIG)))
153 fsread(ino, cmd, sizeof(cmd));
158 printf("%s: %s", PATH_CONFIG, cmd);
159 /* Do not process this command twice */
163 /* Present the user with the boot2 prompt. */
166 strcpy(kname, PATH_KERNEL);
168 printf("\nDefault: %s\nboot: ", kname);
169 if (!autoboot || (c = getc(2)) != -1)
177 else if (*cmd == '*')
189 static Elf32_Phdr ep[2];
195 if (!(ino = lookup(kname))) {
197 printf("No %s\n", kname);
200 if (xfsread(ino, &eh, sizeof(eh)))
203 printf("Invalid %s\n", "format");
207 for (j = i = 0; i < eh.e_phnum && j < 2; i++) {
208 if (xfsread(ino, ep + j, sizeof(ep[0])))
210 if (ep[j].p_type == PT_LOAD)
213 for (i = 0; i < 2; i++) {
214 p = (caddr_t)ep[i].p_paddr;
215 fs_off = ep[i].p_offset;
216 if (xfsread(ino, p, ep[i].p_filesz))
220 ((void(*)(int))addr)(opts & RBX_MASK);
230 while ((c = *arg++)) {
231 if (c == ' ' || c == '\t' || c == '\n')
233 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
238 while ((c = *arg++)) {
239 for (i = 0; c != optstr[i]; i++)
242 opts ^= OPT_SET(flags[i]);
246 if ((i = ep - arg)) {
247 if ((size_t)i >= sizeof(kname))
249 memcpy(kname, arg, i + 1);
258 dskread(void *buf, unsigned lba, unsigned nblk)
260 struct dos_partition *dp;
266 sec = dmadat->secbuf;
268 if (drvread(sec, DOSBBSECTOR, 1))
270 dp = (void *)(sec + DOSPARTOFF);
271 for (i = 0; i < NDOSPART; i++) {
272 if (dp[i].dp_typ == DOSPTYP_386BSD)
277 // Although dp_start is aligned within the disk partition structure,
278 // DOSPARTOFF is 446, which is only word (2) aligned, not longword (4)
279 // aligned. Cope by using memcpy to fetch the start of this partition.
280 memcpy(&dsk_start, &dp[1].dp_start, 4);
281 if (drvread(sec, dsk_start + LABELSECTOR, 1))
283 d = (void *)(sec + LABELOFFSET);
284 if (d->d_magic != DISKMAGIC || d->d_magic2 != DISKMAGIC) {
285 printf("Invalid %s\n", "label");
288 if (!d->d_partitions[0].p_size) {
289 printf("Invalid %s\n", "partition");
292 dsk_start += d->d_partitions[0].p_offset;
293 dsk_start -= d->d_partitions[RAW_PART].p_offset;
296 return drvread(buf, dsk_start + lba, nblk);