2 * Copyright (c) 2008 John Hay
3 * Copyright (c) 1998 Robert Nordier
6 * Redistribution and use in source and binary forms are freely
7 * permitted provided that the above copyright notice and this
8 * paragraph and the following disclaimer are duplicated in all
11 * This software is provided "AS IS" and without any express or
12 * implied warranties, including, without limitation, the implied
13 * warranties of merchantability and fitness for a particular
17 #include <sys/cdefs.h>
18 __FBSDID("$FreeBSD$");
20 #include <sys/param.h>
21 #include <sys/disklabel.h>
22 #include <sys/diskmbr.h>
23 #include <sys/dirent.h>
24 #include <sys/reboot.h>
26 #include <machine/elf.h>
33 #define RBX_ASKNAME 0x0 /* -a */
34 #define RBX_SINGLE 0x1 /* -s */
35 /* 0x2 is reserved for log2(RB_NOSYNC). */
36 /* 0x3 is reserved for log2(RB_HALT). */
37 /* 0x4 is reserved for log2(RB_INITNAME). */
38 #define RBX_DFLTROOT 0x5 /* -r */
39 /* #define RBX_KDB 0x6 -d */
40 /* 0x7 is reserved for log2(RB_RDONLY). */
41 /* 0x8 is reserved for log2(RB_DUMP). */
42 /* 0x9 is reserved for log2(RB_MINIROOT). */
43 #define RBX_CONFIG 0xa /* -c */
44 #define RBX_VERBOSE 0xb /* -v */
45 /* #define RBX_SERIAL 0xc -h */
46 /* #define RBX_CDROM 0xd -C */
47 /* 0xe is reserved for log2(RB_POWEROFF). */
48 #define RBX_GDB 0xf /* -g */
49 /* #define RBX_MUTE 0x10 -m */
50 /* 0x11 is reserved for log2(RB_SELFTEST). */
51 /* 0x12 is reserved for boot programs. */
52 /* 0x13 is reserved for boot programs. */
53 /* #define RBX_PAUSE 0x14 -p */
54 /* #define RBX_QUIET 0x15 -q */
55 #define RBX_NOINTR 0x1c /* -n */
56 /* 0x1d is reserved for log2(RB_MULTIPLE) and is just misnamed here. */
57 /* #define RBX_DUAL 0x1d -D */
58 /* 0x1f is reserved for log2(RB_BOOTINFO). */
60 /* pass: -a, -s, -r, -v, -g */
61 #define RBX_MASK (OPT_SET(RBX_ASKNAME) | OPT_SET(RBX_SINGLE) | \
62 OPT_SET(RBX_DFLTROOT) | \
63 OPT_SET(RBX_VERBOSE) | \
70 #define OPT_SET(opt) (1 << (opt))
71 #define OPT_CHECK(opt) ((opts) & OPT_SET(opt))
73 static const char optstr[NOPT] = "agnrsv";
74 static const unsigned char flags[NOPT] = {
83 static unsigned dsk_start;
85 static char kname[1024];
87 static uint8_t dsk_meta;
90 static int disk_layout;
92 #define DL_RAW 1 /* Dangerously dedicated */
93 #define DL_SLICE 2 /* Use only slices (DOS partitions) */
94 #define DL_SLICEPART 3 /* Use slices and partitions */
96 static void load(void);
97 static int parse(void);
98 static int dskread(void *, unsigned, unsigned);
99 static int drvread(void *, unsigned, unsigned);
100 #ifdef FIXUP_BOOT_DRV
101 static void fixup_boot_drv(caddr_t, int, int, int);
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)
159 dmadat = (void *)(0x1c0000);
160 p_memset((char *)dmadat, 0, 32 * 1024);
163 printf("FreeBSD ARM (%s) boot2 v%d.%d\n", bt, 0, 4);
167 /* Process configuration file */
168 if ((ino = lookup(PATH_CONFIG)) ||
169 (ino = lookup(PATH_DOTCONFIG)))
170 fsread(ino, cmd, sizeof(cmd));
175 printf("%s: %s\n", PATH_CONFIG, cmd);
176 /* Do not process this command twice */
181 strcpy(kname, PATH_KERNEL);
183 /* Present the user with the boot2 prompt. */
185 printf("\nDefault: %s\nboot: ", kname);
187 (OPT_CHECK(RBX_NOINTR) == 0 && (c = getc(2)) != 0))
192 DPRINTF("cmd is '%s'\n", cmd);
204 static Elf32_Phdr ep[2];
209 #ifdef FIXUP_BOOT_DRV
213 staddr = (caddr_t)0xffffffff;
216 if (!(ino = lookup(kname))) {
218 printf("No %s\n", kname);
221 DPRINTF("Found %s\n", kname);
222 if (xfsread(ino, &eh, sizeof(eh)))
225 printf("Invalid %s\n", "format");
229 for (j = i = 0; i < eh.e_phnum && j < 2; i++) {
230 if (xfsread(ino, ep + j, sizeof(ep[0])))
232 if (ep[j].p_type == PT_LOAD)
235 for (i = 0; i < 2; i++) {
236 p = (caddr_t)(ep[i].p_paddr & 0x0fffffff);
237 fs_off = ep[i].p_offset;
238 #ifdef FIXUP_BOOT_DRV
239 if (staddr == (caddr_t)0xffffffff)
241 klen += ep[i].p_filesz;
243 if (xfsread(ino, p, ep[i].p_filesz))
246 addr = eh.e_entry & 0x0fffffff;
247 DPRINTF("Entry point %x for %s\n", addr, kname);
249 #ifdef FIXUP_BOOT_DRV
250 fixup_boot_drv(staddr, klen, bootslice, bootpart);
252 ((void(*)(int))addr)(RB_BOOTINFO /* XXX | (opts & RBX_MASK) */);
262 while ((c = *arg++)) {
263 if (c == ' ' || c == '\t' || c == '\n')
265 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
270 while ((c = *arg++)) {
271 for (i = 0; c != optstr[i]; i++)
274 opts ^= OPT_SET(flags[i]);
278 /* look for ad0s1a:... | ad0s1:... */
279 if (strlen(arg) > 6 && arg[0] == 'a' &&
280 arg[1] == 'd' && arg[3] == 's' &&
281 (arg[5] == ':' || arg[6] == ':')) {
282 /* XXX Should also handle disk. */
283 bootslice = arg[4] - '0';
284 if (bootslice < 1 || bootslice > 4)
288 bootpart = arg[5] - 'a';
289 if (bootpart < 0 || bootpart > 7)
296 /* look for ad0a:... */
297 } else if (strlen(arg) > 4 && arg[0] == 'a' &&
298 arg[1] == 'd' && arg[2] == '0' && arg[4] == ':') {
300 bootpart = arg[3] - 'a';
301 if (bootpart < 0 || bootpart > 7)
306 if ((i = ep - arg)) {
307 if ((size_t)i >= sizeof(kname))
309 memcpy(kname, arg, i + 1);
318 * dskread() will try to handle the disk layouts that are typically
320 * - raw or "Dangerously Dedicated" mode. No real slice table, just the
321 * default one that is included with bsdlabel -B. Typically this is
322 * used with ROOTDEVNAME=\"ufs:ad0a\".
323 * - slice only. Only a slice table is installed with no bsd label or
324 * bsd partition table. This is typically used with
325 * ROOTDEVNAME=\"ufs:ad0s1\".
326 * - slice + bsd label + partition table. This is typically done with
327 * with fdisk + bsdlabel and is used with ROOTDEVNAME=\"ufs:ad0s1a\".
330 dskread(void *buf, unsigned lba, unsigned nblk)
332 struct dos_partition *dp;
338 sec = dmadat->secbuf;
340 if (drvread(sec, DOSBBSECTOR, 1))
342 dp = (void *)(sec + DOSPARTOFF);
343 if (bootslice != 0) {
345 if (dp[i].dp_typ != DOSPTYP_386BSD)
348 for (i = 0; i < NDOSPART; i++) {
349 if ((dp[i].dp_typ == DOSPTYP_386BSD) &&
350 (dp[i].dp_flag == 0x80))
356 DPRINTF("Found an active fbsd slice. (%d)\n", i + 1);
358 * Although dp_start is aligned within the disk
359 * partition structure, DOSPARTOFF is 446, which
360 * is only word (2) aligned, not longword (4)
361 * aligned. Cope by using memcpy to fetch the
362 * start of this partition.
364 memcpy(&dsk_start, &dp[i].dp_start, 4);
365 dsk_start = swap32(dsk_start);
366 DPRINTF("dsk_start %x\n", dsk_start);
367 if ((bootslice == 4) && (dsk_start == 0)) {
368 disk_layout = DL_RAW;
372 if (drvread(sec, dsk_start + LABELSECTOR, 1))
374 d = (void *)(sec + LABELOFFSET);
375 if ((d->d_magic == DISKMAGIC && d->d_magic2 == DISKMAGIC) ||
376 (swap32(d->d_magic) == DISKMAGIC &&
377 swap32(d->d_magic2) == DISKMAGIC)) {
378 DPRINTF("p_size = %x\n",
379 !d->d_partitions[bootpart].p_size);
380 if (!d->d_partitions[bootpart].p_size) {
381 printf("Invalid partition\n");
384 DPRINTF("p_offset %x, RAW %x\n",
385 swap32(d->d_partitions[bootpart].p_offset),
386 swap32(d->d_partitions[RAW_PART].p_offset));
387 dsk_start += swap32(d->d_partitions[bootpart].p_offset);
388 dsk_start -= swap32(d->d_partitions[RAW_PART].p_offset);
389 if ((disk_layout == DL_UNKNOWN) && (bootslice == 0))
390 disk_layout = DL_RAW;
391 else if (disk_layout == DL_UNKNOWN)
392 disk_layout = DL_SLICEPART;
394 disk_layout = DL_SLICE;
395 DPRINTF("Invalid %s\n", "label");
397 DPRINTF("bootslice %d, bootpart %d, dsk_start %u\n", bootslice,
398 bootpart, dsk_start);
401 return drvread(buf, dsk_start + lba, nblk);
405 drvread(void *buf, unsigned lba, unsigned nblk)
407 static unsigned c = 0x2d5c7c2f;
409 printf("%c\b", c = c << 8 | c >> 24);
410 return (avila_read((char *)buf, lba, nblk));
413 #ifdef FIXUP_BOOT_DRV
415 * fixup_boot_drv() will try to find the ROOTDEVNAME spec in the kernel
416 * and change it to what was specified on the comandline or /boot.conf
417 * file or to what was encountered on the disk. It will try to handle 3
418 * different disk layouts, raw (dangerously dedicated), slice only and
419 * slice + partition. It will look for the following strings in the
420 * kernel, but if it is one of the first three, the string in the kernel
421 * must use the correct form to match the actual disk layout:
426 * In the case of the first three strings, only the "a" at the end and
427 * the "1" after the "s" will be modified, if they exist. The string
428 * length will not be changed. In the case of the last string, the
429 * whole string will be built up and nul, '\0' terminated.
432 fixup_boot_drv(caddr_t addr, int klen, int bs, int bp)
434 const u_int8_t op[] = "ufs:ROOTDEVNAME";
435 const u_int8_t op2[] = "ufs:ad0";
438 DPRINTF("fixup_boot_drv: 0x%x, %d, slice %d, partition %d\n",
439 (int)addr, klen, bs, bp);
444 ps = memmem(addr, klen, op, sizeof(op));
446 p = ps + 4; /* past ufs: */
447 DPRINTF("Found it at 0x%x\n", (int)ps);
448 p[0] = 'a'; p[1] = 'd'; p[2] = '0'; /* ad0 */
455 if (disk_layout != DL_SLICE) {
456 /* append partition */
461 ps = memmem(addr, klen, op2, sizeof(op2) - 1);
463 p = ps + sizeof(op2) - 1;
464 DPRINTF("Found it at 0x%x\n", (int)ps);
475 printf("Could not locate \"%s\" to fix kernel boot device, "
476 "check ROOTDEVNAME is set\n", op);
479 DPRINTF("Changed boot device to %s\n", ps);