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>
28 #define BSIZEMAX 16384
30 typedef int putc_func_t(char c, void *arg);
31 typedef int32_t ofwh_t;
39 static const char digits[] = "0123456789abcdef";
41 static char bootpath[128];
42 static char bootargs[128];
44 static ofwh_t bootdev;
47 static char blkbuf[BSIZEMAX];
48 static unsigned int fsblks;
50 static uint32_t fs_off;
52 int main(int ac, char **av);
54 static void exit(int) __dead2;
55 static void load(const char *);
56 static int dskread(void *, u_int64_t, int);
58 static void usage(void);
60 static void bcopy(const void *src, void *dst, size_t len);
61 static void bzero(void *b, size_t len);
63 static int domount(const char *device, int quiet);
65 static void panic(const char *fmt, ...) __dead2;
66 static int printf(const char *fmt, ...);
67 static int putchar(char c, void *arg);
68 static int vprintf(const char *fmt, va_list ap);
69 static int vsnprintf(char *str, size_t sz, const char *fmt, va_list ap);
71 static int __printf(const char *fmt, putc_func_t *putc, void *arg, va_list ap);
72 static int __putc(char c, void *arg);
73 static int __puts(const char *s, putc_func_t *putc, void *arg);
74 static int __sputc(char c, void *arg);
75 static char *__uitoa(char *buf, u_int val, int base);
76 static char *__ultoa(char *buf, u_long val, int base);
78 void __syncicache(void *, int);
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';
140 /* Truncate partition ID */
152 while (*p == ' ' && *p != '\0')
154 if (*p == '\0' || ac >= 16)
157 while (*p != ' ' && *p != '\0')
167 ofw_finddevice(const char *name)
170 (ofwcell_t)"finddevice",
178 printf("ofw_finddevice: name=\"%s\"\n", name);
185 ofw_getprop(ofwh_t ofwh, const char *name, void *buf, size_t len)
188 (ofwcell_t)"getprop",
199 printf("ofw_getprop: ofwh=0x%x buf=%p len=%u\n",
207 ofw_setprop(ofwh_t ofwh, const char *name, void *buf, size_t len)
210 (ofwcell_t)"setprop",
221 printf("ofw_setprop: ofwh=0x%x buf=%p len=%u\n",
229 ofw_open(const char *path)
240 printf("ofw_open: path=\"%s\"\n", path);
247 ofw_close(ofwh_t devh)
257 printf("ofw_close: devh=0x%x\n", devh);
264 ofw_claim(void *virt, size_t len, u_int align)
278 printf("ofw_claim: virt=%p len=%u\n", virt, len);
286 ofw_read(ofwh_t devh, void *buf, size_t len)
299 printf("ofw_read: devh=0x%x buf=%p len=%u\n", devh, buf, len);
306 ofw_write(ofwh_t devh, const void *buf, size_t len)
319 printf("ofw_write: devh=0x%x buf=%p len=%u\n", devh, buf, len);
326 ofw_seek(ofwh_t devh, u_int64_t off)
339 printf("ofw_seek: devh=0x%x off=0x%lx\n", devh, off);
350 args[0] = (ofwcell_t)"exit";
359 bcopy(const void *src, void *dst, size_t len)
369 memcpy(void *dst, const void *src, size_t len)
371 bcopy(src, dst, len);
375 bzero(void *b, size_t len)
384 strcmp(const char *s1, const char *s2)
386 for (; *s1 == *s2 && *s1; s1++, s2++)
388 return ((u_char)*s1 - (u_char)*s2);
394 main(int ac, char **av)
397 char bootpath_full[255];
401 for (i = 0; i < ac; i++) {
415 printf(" \n>> FreeBSD/powerpc Open Firmware boot block\n"
417 " Boot loader: %s\n", bootpath, path);
420 while (bootpath[len] != '\0') len++;
422 memcpy(bootpath_full,bootpath,len+1);
424 if (bootpath_full[len-1] != ':') {
425 /* First try full volume */
426 if (domount(bootpath_full,1) == 0)
429 /* Add a : so that we try partitions if that fails */
432 bootpath_full[len] = ':';
436 /* Loop through first 16 partitions to find a UFS one */
437 for (i = 0; i < 16; i++) {
439 bootpath_full[len] = i + '0';
440 bootpath_full[len+1] = '\0';
442 bootpath_full[len] = '1';
443 bootpath_full[len+1] = i - 10 + '0';
444 bootpath_full[len+2] = '\0';
447 if (domount(bootpath_full,1) >= 0)
458 printf(" Boot volume: %s\n",bootpath_full);
459 ofw_setprop(chosenh, "bootargs", bootpath_full, len+2);
468 printf("usage: boot device [/path/to/loader]\n");
479 static struct dmadat __dmadat;
482 domount(const char *device, int quiet)
486 if ((bootdev = ofw_open(device)) == -1) {
487 printf("domount: can't open device\n");
490 if (fsread(0, NULL, 0)) {
492 printf("domount: can't read superblock\n");
499 load(const char *fname)
507 if ((ino = lookup(fname)) == 0) {
508 printf("File %s not found\n", fname);
511 if (fsread(ino, &eh, sizeof(eh)) != sizeof(eh)) {
512 printf("Can't read elf header\n");
516 printf("Not an ELF file\n");
519 for (i = 0; i < eh.e_phnum; i++) {
520 fs_off = eh.e_phoff + i * eh.e_phentsize;
521 if (fsread(ino, &ph, sizeof(ph)) != sizeof(ph)) {
522 printf("Can't read program header %d\n", i);
525 if (ph.p_type != PT_LOAD)
527 fs_off = ph.p_offset;
528 p = (caddr_t)ph.p_vaddr;
529 ofw_claim(p,(ph.p_filesz > ph.p_memsz) ?
530 ph.p_filesz : ph.p_memsz,0);
531 if (fsread(ino, p, ph.p_filesz) != ph.p_filesz) {
532 printf("Can't read content of section %d\n", i);
535 if (ph.p_filesz != ph.p_memsz)
536 bzero(p + ph.p_filesz, ph.p_memsz - ph.p_filesz);
537 __syncicache(p, ph.p_memsz);
540 (*(void (*)(void *, int, ofwfp_t, char *, int))eh.e_entry)(NULL, 0,
545 dskread(void *buf, u_int64_t lba, int nblk)
548 * The Open Firmware should open the correct partition for us.
549 * That means, if we read from offset zero on an open instance handle,
550 * we should read from offset zero of that partition.
552 ofw_seek(bootdev, lba * DEV_BSIZE);
553 ofw_read(bootdev, buf, nblk * DEV_BSIZE);
558 panic(const char *fmt, ...)
564 vsnprintf(buf, sizeof buf, fmt, ap);
565 printf("panic: %s\n", buf);
572 printf(const char *fmt, ...)
578 ret = vprintf(fmt, ap);
584 putchar(char c, void *arg)
590 ofw_write(stdouth, &buf, 1);
593 ofw_write(stdouth, &buf, 1);
598 vprintf(const char *fmt, va_list ap)
602 ret = __printf(fmt, putchar, 0, ap);
607 vsnprintf(char *str, size_t sz, const char *fmt, va_list ap)
615 ret = __printf(fmt, __sputc, &sp, ap);
620 __printf(const char *fmt, putc_func_t *putc, void *arg, va_list ap)
622 char buf[(sizeof(long) * 8) + 1];
633 nbuf = &buf[sizeof buf - 1];
635 while ((c = *fmt++) != 0) {
643 reswitch: c = *fmt++;
649 ret += putc('%', arg);
657 ui = (u_int)va_arg(ap, int);
660 ret += putc('-', arg);
662 s = __uitoa(nbuf, ui, 10);
664 ul = (u_long)va_arg(ap, long);
667 ret += putc('-', arg);
669 s = __ultoa(nbuf, ul, 10);
671 ret += __puts(s, putc, arg);
678 ui = (u_int)va_arg(ap, u_int);
679 s = __uitoa(nbuf, ui, 8);
681 ul = (u_long)va_arg(ap, u_long);
682 s = __ultoa(nbuf, ul, 8);
684 ret += __puts(s, putc, arg);
687 ul = (u_long)va_arg(ap, void *);
688 s = __ultoa(nbuf, ul, 16);
689 ret += __puts("0x", putc, arg);
690 ret += __puts(s, putc, arg);
693 s = va_arg(ap, char *);
694 ret += __puts(s, putc, arg);
698 ui = va_arg(ap, u_int);
699 s = __uitoa(nbuf, ui, 10);
701 ul = va_arg(ap, u_long);
702 s = __ultoa(nbuf, ul, 10);
704 ret += __puts(s, putc, arg);
708 ui = va_arg(ap, u_int);
709 s = __uitoa(nbuf, ui, 16);
711 ul = va_arg(ap, u_long);
712 s = __ultoa(nbuf, ul, 16);
715 ret += __puts("0x", putc, arg);
716 ret += __puts(s, putc, arg);
718 case '0': case '1': case '2': case '3': case '4':
719 case '5': case '6': case '7': case '8': case '9':
720 pad = pad * 10 + c - '0';
730 __sputc(char c, void *arg)
735 if (sp->sp_len < sp->sp_size)
736 sp->sp_buf[sp->sp_len++] = c;
737 sp->sp_buf[sp->sp_len] = '\0';
742 __puts(const char *s, putc_func_t *putc, void *arg)
748 for (p = s; *p != '\0'; p++)
749 ret += putc(*p, arg);
754 __uitoa(char *buf, u_int ui, int base)
761 *--p = digits[ui % base];
762 while ((ui /= base) != 0);
767 __ultoa(char *buf, u_long ul, int base)
774 *--p = digits[ul % base];
775 while ((ul /= base) != 0);