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(int 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);
67 static void panic(const char *fmt, ...) __dead2;
68 static int printf(const char *fmt, ...);
69 static int putchar(int 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(int c, void *arg);
75 static int __puts(const char *s, putc_func_t *putc, void *arg);
76 static int __sputc(int 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_int64_t ofwcell_t;
84 typedef u_int32_t u_ofwh_t;
85 typedef int (*ofwfp_t)(ofwcell_t []);
86 ofwfp_t ofw; /* the prom Open Firmware entry */
88 void ofw_init(int, int, int, int, ofwfp_t);
89 ofwh_t ofw_finddevice(const char *);
90 ofwh_t ofw_open(const char *);
91 int ofw_getprop(ofwh_t, const char *, void *, size_t);
92 int ofw_read(ofwh_t, void *, size_t);
93 int ofw_write(ofwh_t, const void *, size_t);
94 int ofw_seek(ofwh_t, u_int64_t);
95 void ofw_exit(void) __dead2;
98 ofwh_t stdinh, stdouth;
101 * This has to stay here, as the PROM seems to ignore the
102 * entry point specified in the a.out header. (or elftoaout is broken)
106 ofw_init(int d, int d1, int d2, int d3, ofwfp_t ofwaddr)
115 chosenh = ofw_finddevice("/chosen");
116 ofw_getprop(chosenh, "stdin", &stdinh, sizeof(stdinh));
117 ofw_getprop(chosenh, "stdout", &stdouth, sizeof(stdouth));
118 ofw_getprop(chosenh, "bootargs", bootargs, sizeof(bootargs));
119 ofw_getprop(chosenh, "bootpath", bootpath, sizeof(bootpath));
121 bootargs[sizeof(bootargs) - 1] = '\0';
122 bootpath[sizeof(bootpath) - 1] = '\0';
127 while (*p == ' ' && *p != '\0')
129 if (*p == '\0' || ac >= 16)
132 while (*p != ' ' && *p != '\0')
142 ofw_finddevice(const char *name)
145 (ofwcell_t)"finddevice",
153 printf("ofw_finddevice: name=\"%s\"\n", name);
160 ofw_getprop(ofwh_t ofwh, const char *name, void *buf, size_t len)
163 (ofwcell_t)"getprop",
174 printf("ofw_getprop: ofwh=0x%x buf=%p len=%u\n",
182 ofw_open(const char *path)
193 printf("ofw_open: path=\"%s\"\n", path);
200 ofw_close(ofwh_t devh)
210 printf("ofw_close: devh=0x%x\n", devh);
217 ofw_read(ofwh_t devh, void *buf, size_t len)
230 printf("ofw_read: devh=0x%x buf=%p len=%u\n", devh, buf, len);
237 ofw_write(ofwh_t devh, const void *buf, size_t len)
250 printf("ofw_write: devh=0x%x buf=%p len=%u\n", devh, buf, len);
257 ofw_seek(ofwh_t devh, u_int64_t off)
270 printf("ofw_seek: devh=0x%x off=0x%lx\n", devh, off);
281 args[0] = (ofwcell_t)"exit";
290 bcopy(const void *src, void *dst, size_t len)
300 memcpy(void *dst, const void *src, size_t len)
302 bcopy(src, dst, len);
306 bzero(void *b, size_t len)
315 strcmp(const char *s1, const char *s2)
317 for (; *s1 == *s2 && *s1; s1++, s2++)
319 return ((u_char)*s1 - (u_char)*s2);
325 main(int ac, char **av)
331 for (i = 0; i < ac; i++) {
345 printf(" \n>> FreeBSD/sparc64 boot block\n"
347 " Boot loader: %s\n", bootpath, path);
349 if (mount(bootpath) == -1)
360 printf("usage: boot device [/path/to/loader]\n");
371 static struct dmadat __dmadat;
374 mount(const char *device)
378 if ((bootdev = ofw_open(device)) == -1) {
379 printf("mount: can't open device\n");
382 if (fsread(0, NULL, 0)) {
383 printf("mount: can't read superblock\n");
390 load(const char *fname)
398 if ((ino = lookup(fname)) == 0) {
399 printf("File %s not found\n", fname);
402 if (fsread(ino, &eh, sizeof(eh)) != sizeof(eh)) {
403 printf("Can't read elf header\n");
407 printf("Not an ELF file\n");
410 for (i = 0; i < eh.e_phnum; i++) {
411 fs_off = eh.e_phoff + i * eh.e_phentsize;
412 if (fsread(ino, &ph, sizeof(ph)) != sizeof(ph)) {
413 printf("Can't read program header %d\n", i);
416 if (ph.p_type != PT_LOAD)
418 fs_off = ph.p_offset;
419 p = (caddr_t)ph.p_vaddr;
420 if (fsread(ino, p, ph.p_filesz) != ph.p_filesz) {
421 printf("Can't read content of section %d\n", i);
424 if (ph.p_filesz != ph.p_memsz)
425 bzero(p + ph.p_filesz, ph.p_memsz - ph.p_filesz);
428 (*(void (*)(int, int, int, int, ofwfp_t))eh.e_entry)(0, 0, 0, 0, ofw);
432 dskread(void *buf, u_int64_t lba, int nblk)
435 * The Open Firmware should open the correct partition for us.
436 * That means, if we read from offset zero on an open instance handle,
437 * we should read from offset zero of that partition.
439 ofw_seek(bootdev, lba * DEV_BSIZE);
440 ofw_read(bootdev, buf, nblk * DEV_BSIZE);
445 panic(const char *fmt, ...)
451 vsnprintf(buf, sizeof buf, fmt, ap);
452 printf("panic: %s\n", buf);
459 printf(const char *fmt, ...)
465 ret = vprintf(fmt, ap);
471 putchar(int c, void *arg)
477 ofw_write(stdouth, &buf, 1);
480 ofw_write(stdouth, &buf, 1);
485 vprintf(const char *fmt, va_list ap)
489 ret = __printf(fmt, putchar, 0, ap);
494 vsnprintf(char *str, size_t sz, const char *fmt, va_list ap)
502 ret = __printf(fmt, __sputc, &sp, ap);
507 __printf(const char *fmt, putc_func_t *putc, void *arg, va_list ap)
509 char buf[(sizeof(long) * 8) + 1];
520 nbuf = &buf[sizeof buf - 1];
522 while ((c = *fmt++) != 0) {
530 reswitch: c = *fmt++;
536 ret += putc('%', arg);
544 ui = (u_int)va_arg(ap, int);
547 ret += putc('-', arg);
549 s = __uitoa(nbuf, ui, 10);
551 ul = (u_long)va_arg(ap, long);
554 ret += putc('-', arg);
556 s = __ultoa(nbuf, ul, 10);
558 ret += __puts(s, putc, arg);
565 ui = (u_int)va_arg(ap, u_int);
566 s = __uitoa(nbuf, ui, 8);
568 ul = (u_long)va_arg(ap, u_long);
569 s = __ultoa(nbuf, ul, 8);
571 ret += __puts(s, putc, arg);
574 ul = (u_long)va_arg(ap, void *);
575 s = __ultoa(nbuf, ul, 16);
576 ret += __puts("0x", putc, arg);
577 ret += __puts(s, putc, arg);
580 s = va_arg(ap, char *);
581 ret += __puts(s, putc, arg);
585 ui = va_arg(ap, u_int);
586 s = __uitoa(nbuf, ui, 10);
588 ul = va_arg(ap, u_long);
589 s = __ultoa(nbuf, ul, 10);
591 ret += __puts(s, putc, arg);
595 ui = va_arg(ap, u_int);
596 s = __uitoa(nbuf, ui, 16);
598 ul = va_arg(ap, u_long);
599 s = __ultoa(nbuf, ul, 16);
602 ret += __puts("0x", putc, arg);
603 ret += __puts(s, putc, arg);
605 case '0': case '1': case '2': case '3': case '4':
606 case '5': case '6': case '7': case '8': case '9':
607 pad = pad * 10 + c - '0';
617 __sputc(int c, void *arg)
622 if (sp->sp_len < sp->sp_size)
623 sp->sp_buf[sp->sp_len++] = c;
624 sp->sp_buf[sp->sp_len] = '\0';
629 __puts(const char *s, putc_func_t *putc, void *arg)
635 for (p = s; *p != '\0'; p++)
636 ret += putc(*p, arg);
641 __uitoa(char *buf, u_int ui, int base)
648 *--p = digits[ui % base];
649 while ((ui /= base) != 0);
654 __ultoa(char *buf, u_long ul, int base)
661 *--p = digits[ul % base];
662 while ((ul /= base) != 0);