]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - sys/boot/sparc64/boot1/boot1.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / sys / boot / sparc64 / boot1 / boot1.c
1 /*-
2  * Copyright (c) 1998 Robert Nordier
3  * All rights reserved.
4  * Copyright (c) 2001 Robert Drehmel
5  * All rights reserved.
6  *
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
10  * such forms.
11  *
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
15  * purpose.
16  */
17
18 #include <sys/cdefs.h>
19 __FBSDID("$FreeBSD$");
20
21 #include <sys/param.h>
22 #include <sys/dirent.h>
23 #include <machine/elf.h>
24 #include <machine/stdarg.h>
25
26 #define _PATH_LOADER    "/boot/loader"
27 #define _PATH_KERNEL    "/boot/kernel/kernel"
28
29 typedef int putc_func_t(char c, void *arg);
30 typedef int32_t ofwh_t;
31
32 struct sp_data {
33         char    *sp_buf;
34         u_int   sp_len;
35         u_int   sp_size;
36 };
37
38 static const char digits[] = "0123456789abcdef";
39
40 static char bootpath[128];
41 static char bootargs[128];
42
43 static ofwh_t bootdev;
44
45 static uint32_t fs_off;
46
47 int main(int ac, char **av);
48
49 static void exit(int) __dead2;
50 static void load(const char *);
51 static int dskread(void *, u_int64_t, int);
52
53 static void usage(void);
54
55 static void bcopy(const void *src, void *dst, size_t len);
56 static void bzero(void *b, size_t len);
57
58 static int mount(const char *device);
59
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);
65
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);
71
72 /*
73  * Open Firmware interface functions
74  */
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 */
79
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;
88
89 static ofwh_t stdinh, stdouth;
90
91 /*
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)
94  */
95
96 void
97 ofw_init(int d, int d1, int d2, int d3, ofwfp_t ofwaddr)
98 {
99         ofwh_t chosenh;
100         char *av[16];
101         char *p;
102         int ac;
103
104         ofw = ofwaddr;
105
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));
111
112         bootargs[sizeof(bootargs) - 1] = '\0';
113         bootpath[sizeof(bootpath) - 1] = '\0';
114
115         ac = 0;
116         p = bootargs;
117         for (;;) {
118                 while (*p == ' ' && *p != '\0')
119                         p++;
120                 if (*p == '\0' || ac >= 16)
121                         break;
122                 av[ac++] = p;
123                 while (*p != ' ' && *p != '\0')
124                         p++;
125                 if (*p != '\0')
126                         *p++ = '\0';
127         }
128
129         exit(main(ac, av));
130 }
131
132 static ofwh_t
133 ofw_finddevice(const char *name)
134 {
135         ofwcell_t args[] = {
136                 (ofwcell_t)"finddevice",
137                 1,
138                 1,
139                 (ofwcell_t)name,
140                 0
141         };
142
143         if ((*ofw)(args)) {
144                 printf("ofw_finddevice: name=\"%s\"\n", name);
145                 return (1);
146         }
147         return (args[4]);
148 }
149
150 static int
151 ofw_getprop(ofwh_t ofwh, const char *name, void *buf, size_t len)
152 {
153         ofwcell_t args[] = {
154                 (ofwcell_t)"getprop",
155                 4,
156                 1,
157                 (u_ofwh_t)ofwh,
158                 (ofwcell_t)name,
159                 (ofwcell_t)buf,
160                 len,
161         0
162         };
163
164         if ((*ofw)(args)) {
165                 printf("ofw_getprop: ofwh=0x%x buf=%p len=%u\n",
166                         ofwh, buf, len);
167                 return (1);
168         }
169         return (0);
170 }
171
172 static ofwh_t
173 ofw_open(const char *path)
174 {
175         ofwcell_t args[] = {
176                 (ofwcell_t)"open",
177                 1,
178                 1,
179                 (ofwcell_t)path,
180                 0
181         };
182
183         if ((*ofw)(args)) {
184                 printf("ofw_open: path=\"%s\"\n", path);
185                 return (-1);
186         }
187         return (args[4]);
188 }
189
190 static int
191 ofw_close(ofwh_t devh)
192 {
193         ofwcell_t args[] = {
194                 (ofwcell_t)"close",
195                 1,
196                 0,
197                 (u_ofwh_t)devh
198         };
199
200         if ((*ofw)(args)) {
201                 printf("ofw_close: devh=0x%x\n", devh);
202                 return (1);
203         }
204         return (0);
205 }
206
207 static int
208 ofw_read(ofwh_t devh, void *buf, size_t len)
209 {
210         ofwcell_t args[] = {
211                 (ofwcell_t)"read",
212                 3,
213                 1,
214                 (u_ofwh_t)devh,
215                 (ofwcell_t)buf,
216                 len,
217                 0
218         };
219
220         if ((*ofw)(args)) {
221                 printf("ofw_read: devh=0x%x buf=%p len=%u\n", devh, buf, len);
222                 return (1);
223         }
224         return (0);
225 }
226
227 static int
228 ofw_write(ofwh_t devh, const void *buf, size_t len)
229 {
230         ofwcell_t args[] = {
231                 (ofwcell_t)"write",
232                 3,
233                 1,
234                 (u_ofwh_t)devh,
235                 (ofwcell_t)buf,
236                 len,
237                 0
238         };
239
240         if ((*ofw)(args)) {
241                 printf("ofw_write: devh=0x%x buf=%p len=%u\n", devh, buf, len);
242                 return (1);
243         }
244         return (0);
245 }
246
247 static int
248 ofw_seek(ofwh_t devh, u_int64_t off)
249 {
250         ofwcell_t args[] = {
251                 (ofwcell_t)"seek",
252                 3,
253                 1,
254                 (u_ofwh_t)devh,
255                 off >> 32,
256                 off,
257                 0
258         };
259
260         if ((*ofw)(args)) {
261                 printf("ofw_seek: devh=0x%x off=0x%lx\n", devh, off);
262                 return (1);
263         }
264         return (0);
265 }
266
267 static void
268 ofw_exit(void)
269 {
270         ofwcell_t args[3];
271
272         args[0] = (ofwcell_t)"exit";
273         args[1] = 0;
274         args[2] = 0;
275
276         for (;;)
277                 (*ofw)(args);
278 }
279
280 static void
281 bcopy(const void *src, void *dst, size_t len)
282 {
283         const char *s = src;
284         char *d = dst;
285
286         while (len-- != 0)
287                 *d++ = *s++;
288 }
289
290 static void
291 memcpy(void *dst, const void *src, size_t len)
292 {
293
294         bcopy(src, dst, len);
295 }
296
297 static void
298 bzero(void *b, size_t len)
299 {
300         char *p = b;
301
302         while (len-- != 0)
303                 *p++ = 0;
304 }
305
306 static int
307 strcmp(const char *s1, const char *s2)
308 {
309
310         for (; *s1 == *s2 && *s1; s1++, s2++)
311                 ;
312         return ((u_char)*s1 - (u_char)*s2);
313 }
314
315 #include "ufsread.c"
316
317 int
318 main(int ac, char **av)
319 {
320         const char *path;
321         int i;
322
323         path = _PATH_LOADER;
324         for (i = 0; i < ac; i++) {
325                 switch (av[i][0]) {
326                 case '-':
327                         switch (av[i][1]) {
328                         default:
329                                 usage();
330                         }
331                         break;
332                 default:
333                         path = av[i];
334                         break;
335                 }
336         }
337
338         printf(" \n>> FreeBSD/sparc64 boot block\n"
339         "   Boot path:   %s\n"
340         "   Boot loader: %s\n", bootpath, path);
341
342         if (mount(bootpath) == -1)
343                 panic("mount");
344
345         load(path);
346         return (1);
347 }
348
349 static void
350 usage(void)
351 {
352
353         printf("usage: boot device [/path/to/loader]\n");
354         exit(1);
355 }
356
357 static void
358 exit(int code)
359 {
360
361         ofw_exit();
362 }
363
364 static struct dmadat __dmadat;
365
366 static int
367 mount(const char *device)
368 {
369
370         dmadat = &__dmadat;
371         if ((bootdev = ofw_open(device)) == -1) {
372                 printf("mount: can't open device\n");
373                 return (-1);
374         }
375         if (fsread(0, NULL, 0)) {
376                 printf("mount: can't read superblock\n");
377                 return (-1);
378         }
379         return (0);
380 }
381
382 static void
383 load(const char *fname)
384 {
385         Elf64_Ehdr eh;
386         Elf64_Phdr ph;
387         caddr_t p;
388         ino_t ino;
389         int i;
390
391         if ((ino = lookup(fname)) == 0) {
392                 printf("File %s not found\n", fname);
393                 return;
394         }
395         if (fsread(ino, &eh, sizeof(eh)) != sizeof(eh)) {
396                 printf("Can't read elf header\n");
397                 return;
398         }
399         if (!IS_ELF(eh)) {
400                 printf("Not an ELF file\n");
401                 return;
402         }
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);
407                         return;
408                 }
409                 if (ph.p_type != PT_LOAD)
410                         continue;
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);
415                         return;
416                 }
417                 if (ph.p_filesz != ph.p_memsz)
418                         bzero(p + ph.p_filesz, ph.p_memsz - ph.p_filesz);
419         }
420         ofw_close(bootdev);
421         (*(void (*)(int, int, int, int, ofwfp_t))eh.e_entry)(0, 0, 0, 0, ofw);
422 }
423
424 static int
425 dskread(void *buf, u_int64_t lba, int nblk)
426 {
427
428         /*
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.
432          */
433         ofw_seek(bootdev, lba * DEV_BSIZE);
434         ofw_read(bootdev, buf, nblk * DEV_BSIZE);
435         return (0);
436 }
437
438 static void
439 panic(const char *fmt, ...)
440 {
441         char buf[128];
442         va_list ap;
443
444         va_start(ap, fmt);
445         vsnprintf(buf, sizeof buf, fmt, ap);
446         printf("panic: %s\n", buf);
447         va_end(ap);
448
449         exit(1);
450 }
451
452 static int
453 printf(const char *fmt, ...)
454 {
455         va_list ap;
456         int ret;
457
458         va_start(ap, fmt);
459         ret = vprintf(fmt, ap);
460         va_end(ap);
461         return (ret);
462 }
463
464 static int
465 putchar(char c, void *arg)
466 {
467         char buf;
468
469         if (c == '\n') {
470                 buf = '\r';
471                 ofw_write(stdouth, &buf, 1);
472         }
473         buf = c;
474         ofw_write(stdouth, &buf, 1);
475         return (1);
476 }
477
478 static int
479 vprintf(const char *fmt, va_list ap)
480 {
481         int ret;
482
483         ret = __printf(fmt, putchar, 0, ap);
484         return (ret);
485 }
486
487 static int
488 vsnprintf(char *str, size_t sz, const char *fmt, va_list ap)
489 {
490         struct sp_data sp;
491         int ret;
492
493         sp.sp_buf = str;
494         sp.sp_len = 0;
495         sp.sp_size = sz;
496         ret = __printf(fmt, __sputc, &sp, ap);
497         return (ret);
498 }
499
500 static int
501 __printf(const char *fmt, putc_func_t *putc, void *arg, va_list ap)
502 {
503         char buf[(sizeof(long) * 8) + 1];
504         char *nbuf;
505         u_long ul;
506         u_int ui;
507         int lflag;
508         int sflag;
509         char *s;
510         int pad;
511         int ret;
512         int c;
513
514         nbuf = &buf[sizeof buf - 1];
515         ret = 0;
516         while ((c = *fmt++) != 0) {
517                 if (c != '%') {
518                         ret += putc(c, arg);
519                         continue;
520                 }
521                 lflag = 0;
522                 sflag = 0;
523                 pad = 0;
524 reswitch:       c = *fmt++;
525                 switch (c) {
526                 case '#':
527                         sflag = 1;
528                         goto reswitch;
529                 case '%':
530                         ret += putc('%', arg);
531                         break;
532                 case 'c':
533                         c = va_arg(ap, int);
534                         ret += putc(c, arg);
535                         break;
536                 case 'd':
537                         if (lflag == 0) {
538                                 ui = (u_int)va_arg(ap, int);
539                                 if (ui < (int)ui) {
540                                         ui = -ui;
541                                         ret += putc('-', arg);
542                                 }
543                                 s = __uitoa(nbuf, ui, 10);
544                         } else {
545                                 ul = (u_long)va_arg(ap, long);
546                                 if (ul < (long)ul) {
547                                         ul = -ul;
548                                         ret += putc('-', arg);
549                                 }
550                                 s = __ultoa(nbuf, ul, 10);
551                         }
552                         ret += __puts(s, putc, arg);
553                         break;
554                 case 'l':
555                         lflag = 1;
556                         goto reswitch;
557                 case 'o':
558                         if (lflag == 0) {
559                                 ui = (u_int)va_arg(ap, u_int);
560                                 s = __uitoa(nbuf, ui, 8);
561                         } else {
562                                 ul = (u_long)va_arg(ap, u_long);
563                                 s = __ultoa(nbuf, ul, 8);
564                         }
565                         ret += __puts(s, putc, arg);
566                         break;
567                 case 'p':
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);
572                         break;
573                 case 's':
574                         s = va_arg(ap, char *);
575                         ret += __puts(s, putc, arg);
576                         break;
577                 case 'u':
578                         if (lflag == 0) {
579                                 ui = va_arg(ap, u_int);
580                                 s = __uitoa(nbuf, ui, 10);
581                         } else {
582                                 ul = va_arg(ap, u_long);
583                                 s = __ultoa(nbuf, ul, 10);
584                         }
585                         ret += __puts(s, putc, arg);
586                         break;
587                 case 'x':
588                         if (lflag == 0) {
589                                 ui = va_arg(ap, u_int);
590                                 s = __uitoa(nbuf, ui, 16);
591                         } else {
592                                 ul = va_arg(ap, u_long);
593                                 s = __ultoa(nbuf, ul, 16);
594                         }
595                         if (sflag)
596                                 ret += __puts("0x", putc, arg);
597                         ret += __puts(s, putc, arg);
598                         break;
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';
602                         goto reswitch;
603                 default:
604                         break;
605                 }
606         }
607         return (ret);
608 }
609
610 static int
611 __sputc(char c, void *arg)
612 {
613         struct sp_data *sp;
614
615         sp = 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';
619         return (1);
620 }
621
622 static int
623 __puts(const char *s, putc_func_t *putc, void *arg)
624 {
625         const char *p;
626         int ret;
627
628         ret = 0;
629         for (p = s; *p != '\0'; p++)
630                 ret += putc(*p, arg);
631         return (ret);
632 }
633
634 static char *
635 __uitoa(char *buf, u_int ui, int base)
636 {
637         char *p;
638
639         p = buf;
640         *p = '\0';
641         do
642                 *--p = digits[ui % base];
643         while ((ui /= base) != 0);
644         return (p);
645 }
646
647 static char *
648 __ultoa(char *buf, u_long ul, int base)
649 {
650         char *p;
651
652         p = buf;
653         *p = '\0';
654         do
655                 *--p = digits[ul % base];
656         while ((ul /= base) != 0);
657         return (p);
658 }