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>
38 static const char optstr[NOPT] = "agnrsv";
39 static const unsigned char flags[NOPT] = {
48 static unsigned dsk_start;
50 static char kname[1024];
52 static uint8_t dsk_meta;
55 static int disk_layout;
57 #define DL_RAW 1 /* Dangerously dedicated */
58 #define DL_SLICE 2 /* Use only slices (DOS partitions) */
59 #define DL_SLICEPART 3 /* Use slices and partitions */
61 static void load(void);
62 static int parse(void);
63 static int dskread(void *, unsigned, unsigned);
64 static int drvread(void *, unsigned, unsigned);
66 static void fixup_boot_drv(caddr_t, int, int, int);
72 #define DPRINTF(fmt, ...) printf(fmt, __VA_ARGS__)
74 #define DPRINTF(fmt, ...)
78 xfsread(ufs_ino_t inode, void *buf, size_t nbyte)
80 if ((size_t)fsread(inode, buf, nbyte) != nbyte)
109 if (s - cmd < sizeof(cmd) - 1)
124 dmadat = (void *)(0x1c0000);
125 p_memset((char *)dmadat, 0, 32 * 1024);
128 printf("FreeBSD ARM (%s) boot2 v%d.%d\n", bt, 0, 4);
132 /* Process configuration file */
133 if ((ino = lookup(PATH_CONFIG)) ||
134 (ino = lookup(PATH_DOTCONFIG)))
135 fsread(ino, cmd, sizeof(cmd));
140 printf("%s: %s\n", PATH_CONFIG, cmd);
141 /* Do not process this command twice */
146 strcpy(kname, PATH_KERNEL);
148 /* Present the user with the boot2 prompt. */
150 printf("\nDefault: %s\nboot: ", kname);
152 (OPT_CHECK(RBX_NOINTR) == 0 && (c = getc(2)) != 0))
157 DPRINTF("cmd is '%s'\n", cmd);
169 static Elf32_Phdr ep[2];
174 #ifdef FIXUP_BOOT_DRV
178 staddr = (caddr_t)0xffffffff;
181 if (!(ino = lookup(kname))) {
183 printf("No %s\n", kname);
186 DPRINTF("Found %s\n", kname);
187 if (xfsread(ino, &eh, sizeof(eh)))
190 printf("Invalid %s\n", "format");
194 for (j = i = 0; i < eh.e_phnum && j < 2; i++) {
195 if (xfsread(ino, ep + j, sizeof(ep[0])))
197 if (ep[j].p_type == PT_LOAD)
200 for (i = 0; i < 2; i++) {
201 p = (caddr_t)(ep[i].p_paddr & 0x0fffffff);
202 fs_off = ep[i].p_offset;
203 #ifdef FIXUP_BOOT_DRV
204 if (staddr == (caddr_t)0xffffffff)
206 klen += ep[i].p_filesz;
208 if (xfsread(ino, p, ep[i].p_filesz))
211 addr = eh.e_entry & 0x0fffffff;
212 DPRINTF("Entry point %x for %s\n", addr, kname);
214 #ifdef FIXUP_BOOT_DRV
215 fixup_boot_drv(staddr, klen, bootslice, bootpart);
217 ((void(*)(int))addr)(RB_BOOTINFO /* XXX | (opts & RBX_MASK) */);
227 while ((c = *arg++)) {
228 if (c == ' ' || c == '\t' || c == '\n')
230 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
235 while ((c = *arg++)) {
236 for (i = 0; c != optstr[i]; i++)
239 opts ^= OPT_SET(flags[i]);
243 /* look for ad0s1a:... | ad0s1:... */
244 if (strlen(arg) > 6 && arg[0] == 'a' &&
245 arg[1] == 'd' && arg[3] == 's' &&
246 (arg[5] == ':' || arg[6] == ':')) {
247 /* XXX Should also handle disk. */
248 bootslice = arg[4] - '0';
249 if (bootslice < 1 || bootslice > 4)
253 bootpart = arg[5] - 'a';
254 if (bootpart < 0 || bootpart > 7)
261 /* look for ad0a:... */
262 } else if (strlen(arg) > 4 && arg[0] == 'a' &&
263 arg[1] == 'd' && arg[2] == '0' && arg[4] == ':') {
265 bootpart = arg[3] - 'a';
266 if (bootpart < 0 || bootpart > 7)
271 if ((i = ep - arg)) {
272 if ((size_t)i >= sizeof(kname))
274 memcpy(kname, arg, i + 1);
283 * dskread() will try to handle the disk layouts that are typically
285 * - raw or "Dangerously Dedicated" mode. No real slice table, just the
286 * default one that is included with bsdlabel -B. Typically this is
287 * used with ROOTDEVNAME=\"ufs:ad0a\".
288 * - slice only. Only a slice table is installed with no bsd label or
289 * bsd partition table. This is typically used with
290 * ROOTDEVNAME=\"ufs:ad0s1\".
291 * - slice + bsd label + partition table. This is typically done with
292 * with fdisk + bsdlabel and is used with ROOTDEVNAME=\"ufs:ad0s1a\".
295 dskread(void *buf, unsigned lba, unsigned nblk)
297 struct dos_partition *dp;
303 sec = dmadat->secbuf;
305 if (drvread(sec, DOSBBSECTOR, 1))
307 dp = (void *)(sec + DOSPARTOFF);
308 if (bootslice != 0) {
310 if (dp[i].dp_typ != DOSPTYP_386BSD)
313 for (i = 0; i < NDOSPART; i++) {
314 if ((dp[i].dp_typ == DOSPTYP_386BSD) &&
315 (dp[i].dp_flag == 0x80))
321 DPRINTF("Found an active fbsd slice. (%d)\n", i + 1);
323 * Although dp_start is aligned within the disk
324 * partition structure, DOSPARTOFF is 446, which
325 * is only word (2) aligned, not longword (4)
326 * aligned. Cope by using memcpy to fetch the
327 * start of this partition.
329 memcpy(&dsk_start, &dp[i].dp_start, 4);
330 dsk_start = swap32(dsk_start);
331 DPRINTF("dsk_start %x\n", dsk_start);
332 if ((bootslice == 4) && (dsk_start == 0)) {
333 disk_layout = DL_RAW;
337 if (drvread(sec, dsk_start + LABELSECTOR, 1))
339 d = (void *)(sec + LABELOFFSET);
340 if ((d->d_magic == DISKMAGIC && d->d_magic2 == DISKMAGIC) ||
341 (swap32(d->d_magic) == DISKMAGIC &&
342 swap32(d->d_magic2) == DISKMAGIC)) {
343 DPRINTF("p_size = %x\n",
344 !d->d_partitions[bootpart].p_size);
345 if (!d->d_partitions[bootpart].p_size) {
346 printf("Invalid partition\n");
349 DPRINTF("p_offset %x, RAW %x\n",
350 swap32(d->d_partitions[bootpart].p_offset),
351 swap32(d->d_partitions[RAW_PART].p_offset));
352 dsk_start += swap32(d->d_partitions[bootpart].p_offset);
353 dsk_start -= swap32(d->d_partitions[RAW_PART].p_offset);
354 if ((disk_layout == DL_UNKNOWN) && (bootslice == 0))
355 disk_layout = DL_RAW;
356 else if (disk_layout == DL_UNKNOWN)
357 disk_layout = DL_SLICEPART;
359 disk_layout = DL_SLICE;
360 DPRINTF("Invalid %s\n", "label");
362 DPRINTF("bootslice %d, bootpart %d, dsk_start %u\n", bootslice,
363 bootpart, dsk_start);
366 return drvread(buf, dsk_start + lba, nblk);
370 drvread(void *buf, unsigned lba, unsigned nblk)
372 static unsigned c = 0x2d5c7c2f;
374 printf("%c\b", c = c << 8 | c >> 24);
375 return (avila_read((char *)buf, lba, nblk));
378 #ifdef FIXUP_BOOT_DRV
380 * fixup_boot_drv() will try to find the ROOTDEVNAME spec in the kernel
381 * and change it to what was specified on the comandline or /boot.conf
382 * file or to what was encountered on the disk. It will try to handle 3
383 * different disk layouts, raw (dangerously dedicated), slice only and
384 * slice + partition. It will look for the following strings in the
385 * kernel, but if it is one of the first three, the string in the kernel
386 * must use the correct form to match the actual disk layout:
391 * In the case of the first three strings, only the "a" at the end and
392 * the "1" after the "s" will be modified, if they exist. The string
393 * length will not be changed. In the case of the last string, the
394 * whole string will be built up and nul, '\0' terminated.
397 fixup_boot_drv(caddr_t addr, int klen, int bs, int bp)
399 const u_int8_t op[] = "ufs:ROOTDEVNAME";
400 const u_int8_t op2[] = "ufs:ad0";
403 DPRINTF("fixup_boot_drv: 0x%x, %d, slice %d, partition %d\n",
404 (int)addr, klen, bs, bp);
409 ps = memmem(addr, klen, op, sizeof(op));
411 p = ps + 4; /* past ufs: */
412 DPRINTF("Found it at 0x%x\n", (int)ps);
413 p[0] = 'a'; p[1] = 'd'; p[2] = '0'; /* ad0 */
420 if (disk_layout != DL_SLICE) {
421 /* append partition */
426 ps = memmem(addr, klen, op2, sizeof(op2) - 1);
428 p = ps + sizeof(op2) - 1;
429 DPRINTF("Found it at 0x%x\n", (int)ps);
440 printf("Could not locate \"%s\" to fix kernel boot device, "
441 "check ROOTDEVNAME is set\n", op);
444 DPRINTF("Changed boot device to %s\n", ps);