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 typedef int putc_func_t(char c, void *arg);
30 typedef int32_t ofwh_t;
38 static const char digits[] = "0123456789abcdef";
40 static char bootpath[128];
41 static char bootargs[128];
43 static ofwh_t bootdev;
45 static uint32_t fs_off;
47 int main(int ac, char **av);
49 static void exit(int) __dead2;
50 static void load(const char *);
51 static int dskread(void *, u_int64_t, int);
53 static void usage(void);
55 static void bcopy(const void *src, void *dst, size_t len);
56 static void bzero(void *b, size_t len);
58 static int mount(const char *device);
60 static void panic(const char *fmt, ...) __dead2;
61 static int printf(const char *fmt, ...);
62 static int putchar(char c, void *arg);
63 static int vprintf(const char *fmt, va_list ap);
64 static int vsnprintf(char *str, size_t sz, const char *fmt, va_list ap);
66 static int __printf(const char *fmt, putc_func_t *putc, void *arg, va_list ap);
67 static int __puts(const char *s, putc_func_t *putc, void *arg);
68 static int __sputc(char c, void *arg);
69 static char *__uitoa(char *buf, u_int val, int base);
70 static char *__ultoa(char *buf, u_long val, int base);
73 * Open Firmware interface functions
75 typedef u_int64_t ofwcell_t;
76 typedef u_int32_t u_ofwh_t;
77 typedef int (*ofwfp_t)(ofwcell_t []);
78 static ofwfp_t ofw; /* the PROM Open Firmware entry */
80 void ofw_init(int, int, int, int, ofwfp_t);
81 static ofwh_t ofw_finddevice(const char *);
82 static ofwh_t ofw_open(const char *);
83 static int ofw_getprop(ofwh_t, const char *, void *, size_t);
84 static int ofw_read(ofwh_t, void *, size_t);
85 static int ofw_write(ofwh_t, const void *, size_t);
86 static int ofw_seek(ofwh_t, u_int64_t);
87 static void ofw_exit(void) __dead2;
89 static ofwh_t stdinh, stdouth;
92 * This has to stay here, as the PROM seems to ignore the
93 * entry point specified in the a.out header. (or elftoaout is broken)
97 ofw_init(int d, int d1, int d2, int d3, ofwfp_t ofwaddr)
106 chosenh = ofw_finddevice("/chosen");
107 ofw_getprop(chosenh, "stdin", &stdinh, sizeof(stdinh));
108 ofw_getprop(chosenh, "stdout", &stdouth, sizeof(stdouth));
109 ofw_getprop(chosenh, "bootargs", bootargs, sizeof(bootargs));
110 ofw_getprop(chosenh, "bootpath", bootpath, sizeof(bootpath));
112 bootargs[sizeof(bootargs) - 1] = '\0';
113 bootpath[sizeof(bootpath) - 1] = '\0';
118 while (*p == ' ' && *p != '\0')
120 if (*p == '\0' || ac >= 16)
123 while (*p != ' ' && *p != '\0')
133 ofw_finddevice(const char *name)
136 (ofwcell_t)"finddevice",
144 printf("ofw_finddevice: name=\"%s\"\n", name);
151 ofw_getprop(ofwh_t ofwh, const char *name, void *buf, size_t len)
154 (ofwcell_t)"getprop",
165 printf("ofw_getprop: ofwh=0x%x buf=%p len=%u\n",
173 ofw_open(const char *path)
184 printf("ofw_open: path=\"%s\"\n", path);
191 ofw_close(ofwh_t devh)
201 printf("ofw_close: devh=0x%x\n", devh);
208 ofw_read(ofwh_t devh, void *buf, size_t len)
221 printf("ofw_read: devh=0x%x buf=%p len=%u\n", devh, buf, len);
228 ofw_write(ofwh_t devh, const void *buf, size_t len)
241 printf("ofw_write: devh=0x%x buf=%p len=%u\n", devh, buf, len);
248 ofw_seek(ofwh_t devh, u_int64_t off)
261 printf("ofw_seek: devh=0x%x off=0x%lx\n", devh, off);
272 args[0] = (ofwcell_t)"exit";
281 bcopy(const void *src, void *dst, size_t len)
291 memcpy(void *dst, const void *src, size_t len)
294 bcopy(src, dst, len);
298 bzero(void *b, size_t len)
307 strcmp(const char *s1, const char *s2)
310 for (; *s1 == *s2 && *s1; s1++, s2++)
312 return ((u_char)*s1 - (u_char)*s2);
318 main(int ac, char **av)
324 for (i = 0; i < ac; i++) {
338 printf(" \n>> FreeBSD/sparc64 boot block\n"
340 " Boot loader: %s\n", bootpath, path);
342 if (mount(bootpath) == -1)
353 printf("usage: boot device [/path/to/loader]\n");
364 static struct dmadat __dmadat;
367 mount(const char *device)
371 if ((bootdev = ofw_open(device)) == -1) {
372 printf("mount: can't open device\n");
375 if (fsread(0, NULL, 0)) {
376 printf("mount: can't read superblock\n");
383 load(const char *fname)
391 if ((ino = lookup(fname)) == 0) {
392 printf("File %s not found\n", fname);
395 if (fsread(ino, &eh, sizeof(eh)) != sizeof(eh)) {
396 printf("Can't read elf header\n");
400 printf("Not an ELF file\n");
403 for (i = 0; i < eh.e_phnum; i++) {
404 fs_off = eh.e_phoff + i * eh.e_phentsize;
405 if (fsread(ino, &ph, sizeof(ph)) != sizeof(ph)) {
406 printf("Can't read program header %d\n", i);
409 if (ph.p_type != PT_LOAD)
411 fs_off = ph.p_offset;
412 p = (caddr_t)ph.p_vaddr;
413 if (fsread(ino, p, ph.p_filesz) != ph.p_filesz) {
414 printf("Can't read content of section %d\n", i);
417 if (ph.p_filesz != ph.p_memsz)
418 bzero(p + ph.p_filesz, ph.p_memsz - ph.p_filesz);
421 (*(void (*)(int, int, int, int, ofwfp_t))eh.e_entry)(0, 0, 0, 0, ofw);
425 dskread(void *buf, u_int64_t lba, int nblk)
429 * The Open Firmware should open the correct partition for us.
430 * That means, if we read from offset zero on an open instance handle,
431 * we should read from offset zero of that partition.
433 ofw_seek(bootdev, lba * DEV_BSIZE);
434 ofw_read(bootdev, buf, nblk * DEV_BSIZE);
439 panic(const char *fmt, ...)
445 vsnprintf(buf, sizeof buf, fmt, ap);
446 printf("panic: %s\n", buf);
453 printf(const char *fmt, ...)
459 ret = vprintf(fmt, ap);
465 putchar(char c, void *arg)
471 ofw_write(stdouth, &buf, 1);
474 ofw_write(stdouth, &buf, 1);
479 vprintf(const char *fmt, va_list ap)
483 ret = __printf(fmt, putchar, 0, ap);
488 vsnprintf(char *str, size_t sz, const char *fmt, va_list ap)
496 ret = __printf(fmt, __sputc, &sp, ap);
501 __printf(const char *fmt, putc_func_t *putc, void *arg, va_list ap)
503 char buf[(sizeof(long) * 8) + 1];
514 nbuf = &buf[sizeof buf - 1];
516 while ((c = *fmt++) != 0) {
524 reswitch: c = *fmt++;
530 ret += putc('%', arg);
538 ui = (u_int)va_arg(ap, int);
541 ret += putc('-', arg);
543 s = __uitoa(nbuf, ui, 10);
545 ul = (u_long)va_arg(ap, long);
548 ret += putc('-', arg);
550 s = __ultoa(nbuf, ul, 10);
552 ret += __puts(s, putc, arg);
559 ui = (u_int)va_arg(ap, u_int);
560 s = __uitoa(nbuf, ui, 8);
562 ul = (u_long)va_arg(ap, u_long);
563 s = __ultoa(nbuf, ul, 8);
565 ret += __puts(s, putc, arg);
568 ul = (u_long)va_arg(ap, void *);
569 s = __ultoa(nbuf, ul, 16);
570 ret += __puts("0x", putc, arg);
571 ret += __puts(s, putc, arg);
574 s = va_arg(ap, char *);
575 ret += __puts(s, putc, arg);
579 ui = va_arg(ap, u_int);
580 s = __uitoa(nbuf, ui, 10);
582 ul = va_arg(ap, u_long);
583 s = __ultoa(nbuf, ul, 10);
585 ret += __puts(s, putc, arg);
589 ui = va_arg(ap, u_int);
590 s = __uitoa(nbuf, ui, 16);
592 ul = va_arg(ap, u_long);
593 s = __ultoa(nbuf, ul, 16);
596 ret += __puts("0x", putc, arg);
597 ret += __puts(s, putc, arg);
599 case '0': case '1': case '2': case '3': case '4':
600 case '5': case '6': case '7': case '8': case '9':
601 pad = pad * 10 + c - '0';
611 __sputc(char c, void *arg)
616 if (sp->sp_len < sp->sp_size)
617 sp->sp_buf[sp->sp_len++] = c;
618 sp->sp_buf[sp->sp_len] = '\0';
623 __puts(const char *s, putc_func_t *putc, void *arg)
629 for (p = s; *p != '\0'; p++)
630 ret += putc(*p, arg);
635 __uitoa(char *buf, u_int ui, int base)
642 *--p = digits[ui % base];
643 while ((ui /= base) != 0);
648 __ultoa(char *buf, u_long ul, int base)
655 *--p = digits[ul % base];
656 while ((ul /= base) != 0);