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>
24 #include <machine/elf.h>
25 #include <machine/stdarg.h>
27 #define _PATH_LOADER "/boot/loader"
28 #define _PATH_KERNEL "/boot/kernel/kernel"
29 #define READ_BUF_SIZE 8192
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;
47 static uint32_t fs_off;
49 int main(int ac, char **av);
50 static void exit(int) __dead2;
51 static void usage(void);
54 static void loadzfs(void);
55 static int zbread(char *buf, off_t off, size_t bytes);
57 static void load(const char *);
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);
64 static int dskread(void *buf, u_int64_t lba, int nblk);
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 __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);
79 * Open Firmware interface functions
81 typedef u_int64_t ofwcell_t;
82 typedef u_int32_t u_ofwh_t;
83 typedef int (*ofwfp_t)(ofwcell_t []);
84 static ofwfp_t ofw; /* the PROM Open Firmware entry */
86 void ofw_init(int, int, int, int, ofwfp_t);
87 static ofwh_t ofw_finddevice(const char *);
88 static ofwh_t ofw_open(const char *);
89 static int ofw_getprop(ofwh_t, const char *, void *, size_t);
90 static int ofw_read(ofwh_t, void *, size_t);
91 static int ofw_write(ofwh_t, const void *, size_t);
92 static int ofw_seek(ofwh_t, u_int64_t);
93 static void ofw_exit(void) __dead2;
95 static ofwh_t stdinh, stdouth;
98 * This has to stay here, as the PROM seems to ignore the
99 * entry point specified in the a.out header. (or elftoaout is broken)
103 ofw_init(int d, int d1, int d2, int d3, ofwfp_t ofwaddr)
112 chosenh = ofw_finddevice("/chosen");
113 ofw_getprop(chosenh, "stdin", &stdinh, sizeof(stdinh));
114 ofw_getprop(chosenh, "stdout", &stdouth, sizeof(stdouth));
115 ofw_getprop(chosenh, "bootargs", bootargs, sizeof(bootargs));
116 ofw_getprop(chosenh, "bootpath", bootpath, sizeof(bootpath));
118 bootargs[sizeof(bootargs) - 1] = '\0';
119 bootpath[sizeof(bootpath) - 1] = '\0';
124 while (*p == ' ' && *p != '\0')
126 if (*p == '\0' || ac >= 16)
129 while (*p != ' ' && *p != '\0')
139 ofw_finddevice(const char *name)
142 (ofwcell_t)"finddevice",
150 printf("ofw_finddevice: name=\"%s\"\n", name);
157 ofw_getprop(ofwh_t ofwh, const char *name, void *buf, size_t len)
160 (ofwcell_t)"getprop",
171 printf("ofw_getprop: ofwh=0x%x buf=%p len=%u\n",
179 ofw_open(const char *path)
190 printf("ofw_open: path=\"%s\"\n", path);
197 ofw_close(ofwh_t devh)
207 printf("ofw_close: devh=0x%x\n", devh);
214 ofw_read(ofwh_t devh, void *buf, size_t len)
227 printf("ofw_read: devh=0x%x buf=%p len=%u\n", devh, buf, len);
234 ofw_write(ofwh_t devh, const void *buf, size_t len)
247 printf("ofw_write: devh=0x%x buf=%p len=%u\n", devh, buf, len);
254 ofw_seek(ofwh_t devh, u_int64_t off)
267 printf("ofw_seek: devh=0x%x off=0x%lx\n", devh, off);
278 args[0] = (ofwcell_t)"exit";
287 bcopy(const void *src, void *dst, size_t len)
297 memcpy(void *dst, const void *src, size_t len)
300 bcopy(src, dst, len);
304 bzero(void *b, size_t len)
313 strcmp(const char *s1, const char *s2)
316 for (; *s1 == *s2 && *s1; s1++, s2++)
318 return ((u_char)*s1 - (u_char)*s2);
322 main(int ac, char **av)
328 for (i = 0; i < ac; i++) {
343 printf(" \n>> FreeBSD/sparc64 ZFS boot block\n Boot path: %s\n",
346 printf(" \n>> FreeBSD/sparc64 boot block\n Boot path: %s\n"
347 " Boot loader: %s\n", bootpath, path);
350 if (domount(bootpath) == -1)
365 printf("usage: boot device [/path/to/loader]\n");
378 #define VDEV_BOOT_OFFSET (2 * 256 * 1024)
379 static char zbuf[READ_BUF_SIZE];
382 zbread(char *buf, off_t off, size_t bytes)
392 soff = VDEV_BOOT_OFFSET + off;
393 lb = (soff + bytes + DEV_BSIZE - 1) / DEV_BSIZE;
395 while (poff < soff + bytes) {
396 nb = lb - poff / DEV_BSIZE;
397 if (nb > READ_BUF_SIZE / DEV_BSIZE)
398 nb = READ_BUF_SIZE / DEV_BSIZE;
399 if (dskread(zbuf, poff / DEV_BSIZE, nb))
401 if ((poff / DEV_BSIZE + nb) * DEV_BSIZE > soff + bytes)
402 len = soff + bytes - poff;
404 len = (poff / DEV_BSIZE + nb) * DEV_BSIZE - poff;
405 memcpy(p, zbuf + poff % DEV_BSIZE, len);
409 return (poff - soff);
420 if (zbread((char *)&eh, 0, sizeof(eh)) != sizeof(eh)) {
421 printf("Can't read elf header\n");
425 printf("Not an ELF file\n");
428 for (i = 0; i < eh.e_phnum; i++) {
429 fs_off = eh.e_phoff + i * eh.e_phentsize;
430 if (zbread((char *)&ph, fs_off, sizeof(ph)) != sizeof(ph)) {
431 printf("Can't read program header %d\n", i);
434 if (ph.p_type != PT_LOAD)
436 fs_off = ph.p_offset;
437 p = (caddr_t)ph.p_vaddr;
438 if (zbread(p, fs_off, ph.p_filesz) != ph.p_filesz) {
439 printf("Can't read content of section %d\n", i);
442 if (ph.p_filesz != ph.p_memsz)
443 bzero(p + ph.p_filesz, ph.p_memsz - ph.p_filesz);
446 (*(void (*)(int, int, int, int, ofwfp_t))eh.e_entry)(0, 0, 0, 0, ofw);
453 static struct dmadat __dmadat;
456 load(const char *fname)
464 if ((ino = lookup(fname)) == 0) {
465 printf("File %s not found\n", fname);
468 if (fsread(ino, &eh, sizeof(eh)) != sizeof(eh)) {
469 printf("Can't read elf header\n");
473 printf("Not an ELF file\n");
476 for (i = 0; i < eh.e_phnum; i++) {
477 fs_off = eh.e_phoff + i * eh.e_phentsize;
478 if (fsread(ino, &ph, sizeof(ph)) != sizeof(ph)) {
479 printf("Can't read program header %d\n", i);
482 if (ph.p_type != PT_LOAD)
484 fs_off = ph.p_offset;
485 p = (caddr_t)ph.p_vaddr;
486 if (fsread(ino, p, ph.p_filesz) != ph.p_filesz) {
487 printf("Can't read content of section %d\n", i);
490 if (ph.p_filesz != ph.p_memsz)
491 bzero(p + ph.p_filesz, ph.p_memsz - ph.p_filesz);
494 (*(void (*)(int, int, int, int, ofwfp_t))eh.e_entry)(0, 0, 0, 0, ofw);
500 domount(const char *device)
503 if ((bootdev = ofw_open(device)) == -1) {
504 printf("domount: can't open device\n");
509 if (fsread(0, NULL, 0)) {
510 printf("domount: can't read superblock\n");
518 dskread(void *buf, u_int64_t lba, int nblk)
522 * The Open Firmware should open the correct partition for us.
523 * That means, if we read from offset zero on an open instance handle,
524 * we should read from offset zero of that partition.
526 ofw_seek(bootdev, lba * DEV_BSIZE);
527 ofw_read(bootdev, buf, nblk * DEV_BSIZE);
532 panic(const char *fmt, ...)
538 vsnprintf(buf, sizeof buf, fmt, ap);
539 printf("panic: %s\n", buf);
546 printf(const char *fmt, ...)
552 ret = vprintf(fmt, ap);
558 putchar(char c, void *arg)
564 ofw_write(stdouth, &buf, 1);
567 ofw_write(stdouth, &buf, 1);
572 vprintf(const char *fmt, va_list ap)
576 ret = __printf(fmt, putchar, 0, ap);
581 vsnprintf(char *str, size_t sz, const char *fmt, va_list ap)
589 ret = __printf(fmt, __sputc, &sp, ap);
594 __printf(const char *fmt, putc_func_t *putc, void *arg, va_list ap)
596 char buf[(sizeof(long) * 8) + 1];
607 nbuf = &buf[sizeof buf - 1];
609 while ((c = *fmt++) != 0) {
617 reswitch: c = *fmt++;
623 ret += putc('%', arg);
631 ui = (u_int)va_arg(ap, int);
634 ret += putc('-', arg);
636 s = __uitoa(nbuf, ui, 10);
638 ul = (u_long)va_arg(ap, long);
641 ret += putc('-', arg);
643 s = __ultoa(nbuf, ul, 10);
645 ret += __puts(s, putc, arg);
652 ui = (u_int)va_arg(ap, u_int);
653 s = __uitoa(nbuf, ui, 8);
655 ul = (u_long)va_arg(ap, u_long);
656 s = __ultoa(nbuf, ul, 8);
658 ret += __puts(s, putc, arg);
661 ul = (u_long)va_arg(ap, void *);
662 s = __ultoa(nbuf, ul, 16);
663 ret += __puts("0x", putc, arg);
664 ret += __puts(s, putc, arg);
667 s = va_arg(ap, char *);
668 ret += __puts(s, putc, arg);
672 ui = va_arg(ap, u_int);
673 s = __uitoa(nbuf, ui, 10);
675 ul = va_arg(ap, u_long);
676 s = __ultoa(nbuf, ul, 10);
678 ret += __puts(s, putc, arg);
682 ui = va_arg(ap, u_int);
683 s = __uitoa(nbuf, ui, 16);
685 ul = va_arg(ap, u_long);
686 s = __ultoa(nbuf, ul, 16);
689 ret += __puts("0x", putc, arg);
690 ret += __puts(s, putc, arg);
692 case '0': case '1': case '2': case '3': case '4':
693 case '5': case '6': case '7': case '8': case '9':
694 pad = pad * 10 + c - '0';
704 __sputc(char c, void *arg)
709 if (sp->sp_len < sp->sp_size)
710 sp->sp_buf[sp->sp_len++] = c;
711 sp->sp_buf[sp->sp_len] = '\0';
716 __puts(const char *s, putc_func_t *putc, void *arg)
722 for (p = s; *p != '\0'; p++)
723 ret += putc(*p, arg);
728 __uitoa(char *buf, u_int ui, int base)
735 *--p = digits[ui % base];
736 while ((ui /= base) != 0);
741 __ultoa(char *buf, u_long ul, int base)
748 *--p = digits[ul % base];
749 while ((ul /= base) != 0);