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>
26 #define _PATH_LOADER "/boot/loader"
27 #define _PATH_KERNEL "/boot/kernel/kernel"
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;
49 static char blkbuf[BSIZEMAX];
50 static unsigned int fsblks;
52 static uint32_t fs_off;
54 int main(int ac, char **av);
56 static void exit(int) __dead2;
57 static void load(const char *);
58 static int dskread(void *, u_int64_t, int);
60 static void usage(void);
62 static void bcopy(const void *src, void *dst, size_t len);
63 static void bzero(void *b, size_t len);
65 static int mount(const char *device, int quiet);
67 static void panic(const char *fmt, ...) __dead2;
68 static int printf(const char *fmt, ...);
69 static int putchar(char c, void *arg);
70 static int vprintf(const char *fmt, va_list ap);
71 static int vsnprintf(char *str, size_t sz, const char *fmt, va_list ap);
73 static int __printf(const char *fmt, putc_func_t *putc, void *arg, va_list ap);
74 static int __putc(char c, void *arg);
75 static int __puts(const char *s, putc_func_t *putc, void *arg);
76 static int __sputc(char c, void *arg);
77 static char *__uitoa(char *buf, u_int val, int base);
78 static char *__ultoa(char *buf, u_long val, int base);
81 * Open Firmware interface functions
83 typedef u_int32_t ofwcell_t;
84 typedef u_int32_t u_ofwh_t;
85 typedef int (*ofwfp_t)(void *);
86 ofwfp_t ofw; /* the prom Open Firmware entry */
89 void ofw_init(void *, int, int (*)(void *), char *, int);
90 static ofwh_t ofw_finddevice(const char *);
91 static ofwh_t ofw_open(const char *);
92 static int ofw_close(ofwh_t);
93 static int ofw_getprop(ofwh_t, const char *, void *, size_t);
94 static int ofw_setprop(ofwh_t, const char *, void *, size_t);
95 static int ofw_read(ofwh_t, void *, size_t);
96 static int ofw_write(ofwh_t, const void *, size_t);
97 static int ofw_claim(void *virt, size_t len, u_int align);
98 static int ofw_seek(ofwh_t, u_int64_t);
99 static void ofw_exit(void) __dead2;
102 ofwh_t stdinh, stdouth;
114 addi %r1,%r1,stack@l \n\
115 addi %r1,%r1,8192 \n\
121 ofw_init(void *vpd, int res, int (*openfirm)(void *), char *arg, int argl)
129 chosenh = ofw_finddevice("/chosen");
130 ofw_getprop(chosenh, "stdin", &stdinh, sizeof(stdinh));
131 ofw_getprop(chosenh, "stdout", &stdouth, sizeof(stdouth));
132 ofw_getprop(chosenh, "bootargs", bootargs, sizeof(bootargs));
133 ofw_getprop(chosenh, "bootpath", bootpath, sizeof(bootpath));
135 bootargs[sizeof(bootargs) - 1] = '\0';
136 bootpath[sizeof(bootpath) - 1] = '\0';
150 while (*p == ' ' && *p != '\0')
152 if (*p == '\0' || ac >= 16)
155 while (*p != ' ' && *p != '\0')
165 ofw_finddevice(const char *name)
168 (ofwcell_t)"finddevice",
176 printf("ofw_finddevice: name=\"%s\"\n", name);
183 ofw_getprop(ofwh_t ofwh, const char *name, void *buf, size_t len)
186 (ofwcell_t)"getprop",
197 printf("ofw_getprop: ofwh=0x%x buf=%p len=%u\n",
205 ofw_setprop(ofwh_t ofwh, const char *name, void *buf, size_t len)
208 (ofwcell_t)"setprop",
219 printf("ofw_setprop: ofwh=0x%x buf=%p len=%u\n",
227 ofw_open(const char *path)
238 printf("ofw_open: path=\"%s\"\n", path);
245 ofw_close(ofwh_t devh)
255 printf("ofw_close: devh=0x%x\n", devh);
262 ofw_claim(void *virt, size_t len, u_int align)
276 printf("ofw_claim: virt=%p len=%u\n", virt, len);
284 ofw_read(ofwh_t devh, void *buf, size_t len)
297 printf("ofw_read: devh=0x%x buf=%p len=%u\n", devh, buf, len);
304 ofw_write(ofwh_t devh, const void *buf, size_t len)
317 printf("ofw_write: devh=0x%x buf=%p len=%u\n", devh, buf, len);
324 ofw_seek(ofwh_t devh, u_int64_t off)
337 printf("ofw_seek: devh=0x%x off=0x%lx\n", devh, off);
348 args[0] = (ofwcell_t)"exit";
357 bcopy(const void *src, void *dst, size_t len)
367 memcpy(void *dst, const void *src, size_t len)
369 bcopy(src, dst, len);
373 bzero(void *b, size_t len)
382 strcmp(const char *s1, const char *s2)
384 for (; *s1 == *s2 && *s1; s1++, s2++)
386 return ((u_char)*s1 - (u_char)*s2);
392 main(int ac, char **av)
395 char bootpath_full[255];
399 for (i = 0; i < ac; i++) {
413 printf(" \n>> FreeBSD/powerpc Open Firmware boot block\n"
415 " Boot loader: %s\n", bootpath, path);
418 while (bootpath[len] != '\0') len++;
420 memcpy(bootpath_full,bootpath,len+1);
422 if (bootpath_full[len-1] == ':') {
423 for (i = 0; i < 16; i++) {
425 bootpath_full[len] = i + '0';
426 bootpath_full[len+1] = '\0';
428 bootpath_full[len] = '1';
429 bootpath_full[len+1] = i - 10 + '0';
430 bootpath_full[len+2] = '\0';
433 if (mount(bootpath_full,1) >= 0)
443 if (mount(bootpath_full,0) == -1)
447 printf(" Boot volume: %s\n",bootpath_full);
448 ofw_setprop(chosenh, "bootargs", bootpath_full, len+2);
457 printf("usage: boot device [/path/to/loader]\n");
468 static struct dmadat __dmadat;
471 mount(const char *device, int quiet)
475 if ((bootdev = ofw_open(device)) == -1) {
476 printf("mount: can't open device\n");
479 if (fsread(0, NULL, 0)) {
481 printf("mount: can't read superblock\n");
488 load(const char *fname)
496 if ((ino = lookup(fname)) == 0) {
497 printf("File %s not found\n", fname);
500 if (fsread(ino, &eh, sizeof(eh)) != sizeof(eh)) {
501 printf("Can't read elf header\n");
505 printf("Not an ELF file\n");
508 for (i = 0; i < eh.e_phnum; i++) {
509 fs_off = eh.e_phoff + i * eh.e_phentsize;
510 if (fsread(ino, &ph, sizeof(ph)) != sizeof(ph)) {
511 printf("Can't read program header %d\n", i);
514 if (ph.p_type != PT_LOAD)
516 fs_off = ph.p_offset;
517 p = (caddr_t)ph.p_vaddr;
518 ofw_claim(p,(ph.p_filesz > ph.p_memsz) ?
519 ph.p_filesz : ph.p_memsz,0);
520 if (fsread(ino, p, ph.p_filesz) != ph.p_filesz) {
521 printf("Can't read content of section %d\n", i);
524 if (ph.p_filesz != ph.p_memsz)
525 bzero(p + ph.p_filesz, ph.p_memsz - ph.p_filesz);
528 (*(void (*)(void *, int, ofwfp_t, char *, int))eh.e_entry)(NULL, 0,
533 dskread(void *buf, u_int64_t lba, int nblk)
536 * The Open Firmware should open the correct partition for us.
537 * That means, if we read from offset zero on an open instance handle,
538 * we should read from offset zero of that partition.
540 ofw_seek(bootdev, lba * DEV_BSIZE);
541 ofw_read(bootdev, buf, nblk * DEV_BSIZE);
546 panic(const char *fmt, ...)
552 vsnprintf(buf, sizeof buf, fmt, ap);
553 printf("panic: %s\n", buf);
560 printf(const char *fmt, ...)
565 /* Don't annoy the user as we probe for partitions */
566 if (strcmp(fmt,"Not ufs\n") == 0)
570 ret = vprintf(fmt, ap);
576 putchar(char c, void *arg)
582 ofw_write(stdouth, &buf, 1);
585 ofw_write(stdouth, &buf, 1);
590 vprintf(const char *fmt, va_list ap)
594 ret = __printf(fmt, putchar, 0, ap);
599 vsnprintf(char *str, size_t sz, const char *fmt, va_list ap)
607 ret = __printf(fmt, __sputc, &sp, ap);
612 __printf(const char *fmt, putc_func_t *putc, void *arg, va_list ap)
614 char buf[(sizeof(long) * 8) + 1];
625 nbuf = &buf[sizeof buf - 1];
627 while ((c = *fmt++) != 0) {
635 reswitch: c = *fmt++;
641 ret += putc('%', arg);
649 ui = (u_int)va_arg(ap, int);
652 ret += putc('-', arg);
654 s = __uitoa(nbuf, ui, 10);
656 ul = (u_long)va_arg(ap, long);
659 ret += putc('-', arg);
661 s = __ultoa(nbuf, ul, 10);
663 ret += __puts(s, putc, arg);
670 ui = (u_int)va_arg(ap, u_int);
671 s = __uitoa(nbuf, ui, 8);
673 ul = (u_long)va_arg(ap, u_long);
674 s = __ultoa(nbuf, ul, 8);
676 ret += __puts(s, putc, arg);
679 ul = (u_long)va_arg(ap, void *);
680 s = __ultoa(nbuf, ul, 16);
681 ret += __puts("0x", putc, arg);
682 ret += __puts(s, putc, arg);
685 s = va_arg(ap, char *);
686 ret += __puts(s, putc, arg);
690 ui = va_arg(ap, u_int);
691 s = __uitoa(nbuf, ui, 10);
693 ul = va_arg(ap, u_long);
694 s = __ultoa(nbuf, ul, 10);
696 ret += __puts(s, putc, arg);
700 ui = va_arg(ap, u_int);
701 s = __uitoa(nbuf, ui, 16);
703 ul = va_arg(ap, u_long);
704 s = __ultoa(nbuf, ul, 16);
707 ret += __puts("0x", putc, arg);
708 ret += __puts(s, putc, arg);
710 case '0': case '1': case '2': case '3': case '4':
711 case '5': case '6': case '7': case '8': case '9':
712 pad = pad * 10 + c - '0';
722 __sputc(char c, void *arg)
727 if (sp->sp_len < sp->sp_size)
728 sp->sp_buf[sp->sp_len++] = c;
729 sp->sp_buf[sp->sp_len] = '\0';
734 __puts(const char *s, putc_func_t *putc, void *arg)
740 for (p = s; *p != '\0'; p++)
741 ret += putc(*p, arg);
746 __uitoa(char *buf, u_int ui, int base)
753 *--p = digits[ui % base];
754 while ((ui /= base) != 0);
759 __ultoa(char *buf, u_long ul, int base)
766 *--p = digits[ul % base];
767 while ((ul /= base) != 0);