2 * Copyright (c) 1998 Robert Nordier
4 * Copyright (c) 2001 Robert Drehmel
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/dirent.h>
23 #include <machine/elf.h>
24 #include <machine/stdarg.h>
25 #include <machine/md_var.h>
29 #define BSIZEMAX 16384
31 typedef int putc_func_t(char c, void *arg);
32 typedef int32_t ofwh_t;
40 static const char digits[] = "0123456789abcdef";
42 static char bootpath[128];
43 static char bootargs[128];
45 static ofwh_t bootdev;
48 static char blkbuf[BSIZEMAX];
49 static unsigned int fsblks;
51 static uint32_t fs_off;
53 int main(int ac, char **av);
55 static void exit(int) __dead2;
56 static void load(const char *);
57 static int dskread(void *, uint64_t, int);
59 static void usage(void);
61 static void bcopy(const void *src, void *dst, size_t len);
62 static void bzero(void *b, size_t len);
64 static int domount(const char *device, int quiet);
66 static void panic(const char *fmt, ...) __dead2;
67 static int printf(const char *fmt, ...);
68 static int putchar(char c, void *arg);
69 static int vprintf(const char *fmt, va_list ap);
70 static int vsnprintf(char *str, size_t sz, const char *fmt, va_list ap);
72 static int __printf(const char *fmt, putc_func_t *putc, void *arg, va_list ap);
73 static int __putc(char c, void *arg);
74 static int __puts(const char *s, putc_func_t *putc, void *arg);
75 static int __sputc(char c, void *arg);
76 static char *__uitoa(char *buf, u_int val, int base);
77 static char *__ultoa(char *buf, u_long val, int base);
80 * Open Firmware interface functions
82 typedef uint32_t ofwcell_t;
83 typedef uint32_t u_ofwh_t;
84 typedef int (*ofwfp_t)(void *);
85 ofwfp_t ofw; /* the prom Open Firmware entry */
88 void ofw_init(void *, int, int (*)(void *), char *, int);
89 static ofwh_t ofw_finddevice(const char *);
90 static ofwh_t ofw_open(const char *);
91 static int ofw_close(ofwh_t);
92 static int ofw_getprop(ofwh_t, const char *, void *, size_t);
93 static int ofw_setprop(ofwh_t, const char *, void *, size_t);
94 static int ofw_read(ofwh_t, void *, size_t);
95 static int ofw_write(ofwh_t, const void *, size_t);
96 static int ofw_claim(void *virt, size_t len, u_int align);
97 static int ofw_seek(ofwh_t, uint64_t);
98 static void ofw_exit(void) __dead2;
101 ofwh_t stdinh, stdouth;
113 addi %r1,%r1,stack@l \n\
114 addi %r1,%r1,8192 \n\
120 ofw_init(void *vpd, int res, int (*openfirm)(void *), char *arg, int argl)
128 chosenh = ofw_finddevice("/chosen");
129 ofw_getprop(chosenh, "stdin", &stdinh, sizeof(stdinh));
130 ofw_getprop(chosenh, "stdout", &stdouth, sizeof(stdouth));
131 ofw_getprop(chosenh, "bootargs", bootargs, sizeof(bootargs));
132 ofw_getprop(chosenh, "bootpath", bootpath, sizeof(bootpath));
134 bootargs[sizeof(bootargs) - 1] = '\0';
135 bootpath[sizeof(bootpath) - 1] = '\0';
139 /* Truncate partition ID */
151 while (*p == ' ' && *p != '\0')
153 if (*p == '\0' || ac >= 16)
156 while (*p != ' ' && *p != '\0')
166 ofw_finddevice(const char *name)
169 (ofwcell_t)"finddevice",
177 printf("ofw_finddevice: name=\"%s\"\n", name);
184 ofw_getprop(ofwh_t ofwh, const char *name, void *buf, size_t len)
187 (ofwcell_t)"getprop",
198 printf("ofw_getprop: ofwh=0x%x buf=%p len=%u\n",
206 ofw_setprop(ofwh_t ofwh, const char *name, void *buf, size_t len)
209 (ofwcell_t)"setprop",
220 printf("ofw_setprop: ofwh=0x%x buf=%p len=%u\n",
228 ofw_open(const char *path)
239 printf("ofw_open: path=\"%s\"\n", path);
246 ofw_close(ofwh_t devh)
256 printf("ofw_close: devh=0x%x\n", devh);
263 ofw_claim(void *virt, size_t len, u_int align)
277 printf("ofw_claim: virt=%p len=%u\n", virt, len);
285 ofw_read(ofwh_t devh, void *buf, size_t len)
298 printf("ofw_read: devh=0x%x buf=%p len=%u\n", devh, buf, len);
305 ofw_write(ofwh_t devh, const void *buf, size_t len)
318 printf("ofw_write: devh=0x%x buf=%p len=%u\n", devh, buf, len);
325 ofw_seek(ofwh_t devh, uint64_t off)
338 printf("ofw_seek: devh=0x%x off=0x%lx\n", devh, off);
349 args[0] = (ofwcell_t)"exit";
358 bcopy(const void *src, void *dst, size_t len)
368 memcpy(void *dst, const void *src, size_t len)
370 bcopy(src, dst, len);
374 bzero(void *b, size_t len)
383 strcmp(const char *s1, const char *s2)
385 for (; *s1 == *s2 && *s1; s1++, s2++)
387 return ((u_char)*s1 - (u_char)*s2);
393 main(int ac, char **av)
396 char bootpath_full[255];
400 for (i = 0; i < ac; i++) {
414 printf(" \n>> FreeBSD/powerpc Open Firmware boot block\n"
416 " Boot loader: %s\n", bootpath, path);
419 while (bootpath[len] != '\0') len++;
421 memcpy(bootpath_full,bootpath,len+1);
423 if (bootpath_full[len-1] != ':') {
424 /* First try full volume */
425 if (domount(bootpath_full,1) == 0)
428 /* Add a : so that we try partitions if that fails */
431 bootpath_full[len] = ':';
435 /* Loop through first 16 partitions to find a UFS one */
436 for (i = 0; i < 16; i++) {
438 bootpath_full[len] = i + '0';
439 bootpath_full[len+1] = '\0';
441 bootpath_full[len] = '1';
442 bootpath_full[len+1] = i - 10 + '0';
443 bootpath_full[len+2] = '\0';
446 if (domount(bootpath_full,1) >= 0)
457 printf(" Boot volume: %s\n",bootpath_full);
458 ofw_setprop(chosenh, "bootargs", bootpath_full, len+2);
467 printf("usage: boot device [/path/to/loader]\n");
478 static struct dmadat __dmadat;
481 domount(const char *device, int quiet)
485 if ((bootdev = ofw_open(device)) == -1) {
486 printf("domount: can't open device\n");
489 if (fsread(0, NULL, 0)) {
491 printf("domount: can't read superblock\n");
498 load(const char *fname)
506 if ((ino = lookup(fname)) == 0) {
507 printf("File %s not found\n", fname);
510 if (fsread(ino, &eh, sizeof(eh)) != sizeof(eh)) {
511 printf("Can't read elf header\n");
515 printf("Not an ELF file\n");
518 for (i = 0; i < eh.e_phnum; i++) {
519 fs_off = eh.e_phoff + i * eh.e_phentsize;
520 if (fsread(ino, &ph, sizeof(ph)) != sizeof(ph)) {
521 printf("Can't read program header %d\n", i);
524 if (ph.p_type != PT_LOAD)
526 fs_off = ph.p_offset;
527 p = (caddr_t)ph.p_vaddr;
528 ofw_claim(p,(ph.p_filesz > ph.p_memsz) ?
529 ph.p_filesz : ph.p_memsz,0);
530 if (fsread(ino, p, ph.p_filesz) != ph.p_filesz) {
531 printf("Can't read content of section %d\n", i);
534 if (ph.p_filesz != ph.p_memsz)
535 bzero(p + ph.p_filesz, ph.p_memsz - ph.p_filesz);
536 __syncicache(p, ph.p_memsz);
539 (*(void (*)(void *, int, ofwfp_t, char *, int))eh.e_entry)(NULL, 0,
544 dskread(void *buf, uint64_t lba, int nblk)
547 * The Open Firmware should open the correct partition for us.
548 * That means, if we read from offset zero on an open instance handle,
549 * we should read from offset zero of that partition.
551 ofw_seek(bootdev, lba * DEV_BSIZE);
552 ofw_read(bootdev, buf, nblk * DEV_BSIZE);
557 panic(const char *fmt, ...)
563 vsnprintf(buf, sizeof buf, fmt, ap);
564 printf("panic: %s\n", buf);
571 printf(const char *fmt, ...)
577 ret = vprintf(fmt, ap);
583 putchar(char c, void *arg)
589 ofw_write(stdouth, &buf, 1);
592 ofw_write(stdouth, &buf, 1);
597 vprintf(const char *fmt, va_list ap)
601 ret = __printf(fmt, putchar, 0, ap);
606 vsnprintf(char *str, size_t sz, const char *fmt, va_list ap)
614 ret = __printf(fmt, __sputc, &sp, ap);
619 __printf(const char *fmt, putc_func_t *putc, void *arg, va_list ap)
621 char buf[(sizeof(long) * 8) + 1];
632 nbuf = &buf[sizeof buf - 1];
634 while ((c = *fmt++) != 0) {
642 reswitch: c = *fmt++;
648 ret += putc('%', arg);
656 ui = (u_int)va_arg(ap, int);
659 ret += putc('-', arg);
661 s = __uitoa(nbuf, ui, 10);
663 ul = (u_long)va_arg(ap, long);
666 ret += putc('-', arg);
668 s = __ultoa(nbuf, ul, 10);
670 ret += __puts(s, putc, arg);
677 ui = (u_int)va_arg(ap, u_int);
678 s = __uitoa(nbuf, ui, 8);
680 ul = (u_long)va_arg(ap, u_long);
681 s = __ultoa(nbuf, ul, 8);
683 ret += __puts(s, putc, arg);
686 ul = (u_long)va_arg(ap, void *);
687 s = __ultoa(nbuf, ul, 16);
688 ret += __puts("0x", putc, arg);
689 ret += __puts(s, putc, arg);
692 s = va_arg(ap, char *);
693 ret += __puts(s, putc, arg);
697 ui = va_arg(ap, u_int);
698 s = __uitoa(nbuf, ui, 10);
700 ul = va_arg(ap, u_long);
701 s = __ultoa(nbuf, ul, 10);
703 ret += __puts(s, putc, arg);
707 ui = va_arg(ap, u_int);
708 s = __uitoa(nbuf, ui, 16);
710 ul = va_arg(ap, u_long);
711 s = __ultoa(nbuf, ul, 16);
714 ret += __puts("0x", putc, arg);
715 ret += __puts(s, putc, arg);
717 case '0': case '1': case '2': case '3': case '4':
718 case '5': case '6': case '7': case '8': case '9':
719 pad = pad * 10 + c - '0';
729 __sputc(char c, void *arg)
734 if (sp->sp_len < sp->sp_size)
735 sp->sp_buf[sp->sp_len++] = c;
736 sp->sp_buf[sp->sp_len] = '\0';
741 __puts(const char *s, putc_func_t *putc, void *arg)
747 for (p = s; *p != '\0'; p++)
748 ret += putc(*p, arg);
753 __uitoa(char *buf, u_int ui, int base)
760 *--p = digits[ui % base];
761 while ((ui /= base) != 0);
766 __ultoa(char *buf, u_long ul, int base)
773 *--p = digits[ul % base];
774 while ((ul /= base) != 0);