]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/boot/powerpc/boot1.chrp/boot1.c
MFC r293461:
[FreeBSD/stable/10.git] / sys / boot / powerpc / boot1.chrp / 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 #define BSIZEMAX        16384
30
31 typedef int putc_func_t(char c, void *arg);
32 typedef int32_t ofwh_t;
33
34 struct sp_data {
35         char    *sp_buf;
36         u_int   sp_len;
37         u_int   sp_size;
38 };
39
40 static const char digits[] = "0123456789abcdef";
41
42 static char bootpath[128];
43 static char bootargs[128];
44
45 static ofwh_t bootdev;
46
47 static struct fs fs;
48 static char blkbuf[BSIZEMAX];
49 static unsigned int fsblks;
50
51 static uint32_t fs_off;
52
53 int main(int ac, char **av);
54
55 static void exit(int) __dead2;
56 static void load(const char *);
57 static int dskread(void *, u_int64_t, int);
58
59 static void usage(void);
60
61 static void bcopy(const void *src, void *dst, size_t len);
62 static void bzero(void *b, size_t len);
63
64 static int domount(const char *device, int quiet);
65
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);
71
72 static int __printf(const char *fmt, putc_func_t *putc, void *arg, va_list ap);
73 static int __putc(char c, void *arg);
74 static int __puts(const char *s, putc_func_t *putc, void *arg);
75 static int __sputc(char c, void *arg);
76 static char *__uitoa(char *buf, u_int val, int base);
77 static char *__ultoa(char *buf, u_long val, int base);
78
79 void __syncicache(void *, int);
80
81 /*
82  * Open Firmware interface functions
83  */
84 typedef u_int32_t       ofwcell_t;
85 typedef u_int32_t       u_ofwh_t;
86 typedef int (*ofwfp_t)(void *);
87 ofwfp_t ofw;                    /* the prom Open Firmware entry */
88 ofwh_t chosenh;
89
90 void ofw_init(void *, int, int (*)(void *), char *, int);
91 static ofwh_t ofw_finddevice(const char *);
92 static ofwh_t ofw_open(const char *);
93 static int ofw_close(ofwh_t);
94 static int ofw_getprop(ofwh_t, const char *, void *, size_t);
95 static int ofw_setprop(ofwh_t, const char *, void *, size_t);
96 static int ofw_read(ofwh_t, void *, size_t);
97 static int ofw_write(ofwh_t, const void *, size_t);
98 static int ofw_claim(void *virt, size_t len, u_int align);
99 static int ofw_seek(ofwh_t, u_int64_t);
100 static void ofw_exit(void) __dead2;
101
102 ofwh_t bootdevh;
103 ofwh_t stdinh, stdouth;
104
105 __asm("                         \n\
106         .data                   \n\
107         .align 4                \n\
108 stack:                          \n\
109         .space  16384           \n\
110                                 \n\
111         .text                   \n\
112         .globl  _start          \n\
113 _start:                         \n\
114         lis     %r1,stack@ha    \n\
115         addi    %r1,%r1,stack@l \n\
116         addi    %r1,%r1,8192    \n\
117                                 \n\
118         b       ofw_init        \n\
119 ");
120
121 void
122 ofw_init(void *vpd, int res, int (*openfirm)(void *), char *arg, int argl)
123 {
124         char *av[16];
125         char *p;
126         int ac;
127
128         ofw = openfirm;
129
130         chosenh = ofw_finddevice("/chosen");
131         ofw_getprop(chosenh, "stdin", &stdinh, sizeof(stdinh));
132         ofw_getprop(chosenh, "stdout", &stdouth, sizeof(stdouth));
133         ofw_getprop(chosenh, "bootargs", bootargs, sizeof(bootargs));
134         ofw_getprop(chosenh, "bootpath", bootpath, sizeof(bootpath));
135
136         bootargs[sizeof(bootargs) - 1] = '\0';
137         bootpath[sizeof(bootpath) - 1] = '\0';
138
139         p = bootpath;
140         while (*p != '\0') {
141                 if (*p == ':') {
142                         *(++p) = '\0';
143                         break;
144                 }
145                 p++;
146         }
147
148         ac = 0;
149         p = bootargs;
150         for (;;) {
151                 while (*p == ' ' && *p != '\0')
152                         p++;
153                 if (*p == '\0' || ac >= 16)
154                         break;
155                 av[ac++] = p;
156                 while (*p != ' ' && *p != '\0')
157                         p++;
158                 if (*p != '\0')
159                         *p++ = '\0';
160         }
161
162         exit(main(ac, av));
163 }
164
165 static ofwh_t
166 ofw_finddevice(const char *name)
167 {
168         ofwcell_t args[] = {
169                 (ofwcell_t)"finddevice",
170                 1,
171                 1,
172                 (ofwcell_t)name,
173                 0
174         };
175
176         if ((*ofw)(args)) {
177                 printf("ofw_finddevice: name=\"%s\"\n", name);
178                 return (1);
179         }
180         return (args[4]);
181 }
182
183 static int
184 ofw_getprop(ofwh_t ofwh, const char *name, void *buf, size_t len)
185 {
186         ofwcell_t args[] = {
187                 (ofwcell_t)"getprop",
188                 4,
189                 1,
190                 (u_ofwh_t)ofwh,
191                 (ofwcell_t)name,
192                 (ofwcell_t)buf,
193                 len,
194         0
195         };
196
197         if ((*ofw)(args)) {
198                 printf("ofw_getprop: ofwh=0x%x buf=%p len=%u\n",
199                         ofwh, buf, len);
200                 return (1);
201         }
202         return (0);
203 }
204
205 static int
206 ofw_setprop(ofwh_t ofwh, const char *name, void *buf, size_t len)
207 {
208         ofwcell_t args[] = {
209                 (ofwcell_t)"setprop",
210                 4,
211                 1,
212                 (u_ofwh_t)ofwh,
213                 (ofwcell_t)name,
214                 (ofwcell_t)buf,
215                 len,
216         0
217         };
218
219         if ((*ofw)(args)) {
220                 printf("ofw_setprop: ofwh=0x%x buf=%p len=%u\n",
221                         ofwh, buf, len);
222                 return (1);
223         }
224         return (0);
225 }
226
227 static ofwh_t
228 ofw_open(const char *path)
229 {
230         ofwcell_t args[] = {
231                 (ofwcell_t)"open",
232                 1,
233                 1,
234                 (ofwcell_t)path,
235                 0
236         };
237
238         if ((*ofw)(args)) {
239                 printf("ofw_open: path=\"%s\"\n", path);
240                 return (-1);
241         }
242         return (args[4]);
243 }
244
245 static int
246 ofw_close(ofwh_t devh)
247 {
248         ofwcell_t args[] = {
249                 (ofwcell_t)"close",
250                 1,
251                 0,
252                 (u_ofwh_t)devh
253         };
254
255         if ((*ofw)(args)) {
256                 printf("ofw_close: devh=0x%x\n", devh);
257                 return (1);
258         }
259         return (0);
260 }
261
262 static int
263 ofw_claim(void *virt, size_t len, u_int align)
264 {
265         ofwcell_t args[] = {
266                 (ofwcell_t)"claim",
267                 3,
268                 1,
269                 (ofwcell_t)virt,
270                 len,
271                 align,
272                 0,
273                 0
274         };
275
276         if ((*ofw)(args)) {
277                 printf("ofw_claim: virt=%p len=%u\n", virt, len);
278                 return (1);
279         }
280
281         return (0);
282 }
283
284 static int
285 ofw_read(ofwh_t devh, void *buf, size_t len)
286 {
287         ofwcell_t args[] = {
288                 (ofwcell_t)"read",
289                 3,
290                 1,
291                 (u_ofwh_t)devh,
292                 (ofwcell_t)buf,
293                 len,
294                 0
295         };
296
297         if ((*ofw)(args)) {
298                 printf("ofw_read: devh=0x%x buf=%p len=%u\n", devh, buf, len);
299                 return (1);
300         }
301         return (0);
302 }
303
304 static int
305 ofw_write(ofwh_t devh, const void *buf, size_t len)
306 {
307         ofwcell_t args[] = {
308                 (ofwcell_t)"write",
309                 3,
310                 1,
311                 (u_ofwh_t)devh,
312                 (ofwcell_t)buf,
313                 len,
314                 0
315         };
316
317         if ((*ofw)(args)) {
318                 printf("ofw_write: devh=0x%x buf=%p len=%u\n", devh, buf, len);
319                 return (1);
320         }
321         return (0);
322 }
323
324 static int
325 ofw_seek(ofwh_t devh, u_int64_t off)
326 {
327         ofwcell_t args[] = {
328                 (ofwcell_t)"seek",
329                 3,
330                 1,
331                 (u_ofwh_t)devh,
332                 off >> 32,
333                 off,
334                 0
335         };
336
337         if ((*ofw)(args)) {
338                 printf("ofw_seek: devh=0x%x off=0x%lx\n", devh, off);
339                 return (1);
340         }
341         return (0);
342 }
343
344 static void
345 ofw_exit(void)
346 {
347         ofwcell_t args[3];
348
349         args[0] = (ofwcell_t)"exit";
350         args[1] = 0;
351         args[2] = 0;
352
353         for (;;)
354                 (*ofw)(args);
355 }
356
357 static void
358 bcopy(const void *src, void *dst, size_t len)
359 {
360         const char *s = src;
361         char *d = dst;
362
363         while (len-- != 0)
364                 *d++ = *s++;
365 }
366
367 static void
368 memcpy(void *dst, const void *src, size_t len)
369 {
370         bcopy(src, dst, len);
371 }
372
373 static void
374 bzero(void *b, size_t len)
375 {
376         char *p = b;
377
378         while (len-- != 0)
379                 *p++ = 0;
380 }
381
382 static int
383 strcmp(const char *s1, const char *s2)
384 {
385         for (; *s1 == *s2 && *s1; s1++, s2++)
386                 ;
387         return ((u_char)*s1 - (u_char)*s2);
388 }
389
390 #include "ufsread.c"
391
392 int
393 main(int ac, char **av)
394 {
395         const char *path;
396         char bootpath_full[255];
397         int i, len;
398
399         path = _PATH_LOADER;
400         for (i = 0; i < ac; i++) {
401                 switch (av[i][0]) {
402                 case '-':
403                         switch (av[i][1]) {
404                         default:
405                                 usage();
406                         }
407                         break;
408                 default:
409                         path = av[i];
410                         break;
411                 }
412         }
413
414         printf(" \n>> FreeBSD/powerpc Open Firmware boot block\n"
415         "   Boot path:   %s\n"
416         "   Boot loader: %s\n", bootpath, path);
417
418         len = 0;
419         while (bootpath[len] != '\0') len++;
420
421         memcpy(bootpath_full,bootpath,len+1);
422
423         if (bootpath_full[len-1] == ':') {
424                 for (i = 0; i < 16; i++) {
425                         if (i < 10) {
426                                 bootpath_full[len] = i + '0';
427                                 bootpath_full[len+1] = '\0';
428                         } else {
429                                 bootpath_full[len] = '1';
430                                 bootpath_full[len+1] = i - 10 + '0';
431                                 bootpath_full[len+2] = '\0';
432                         }
433                                 
434                         if (domount(bootpath_full,1) >= 0)
435                                 break;
436
437                         if (bootdev > 0)
438                                 ofw_close(bootdev);
439                 }
440
441                 if (i >= 16)
442                         panic("domount");
443         } else {
444                 if (domount(bootpath_full,0) == -1)
445                         panic("domount");
446         }
447
448         printf("   Boot volume:   %s\n",bootpath_full);
449         ofw_setprop(chosenh, "bootargs", bootpath_full, len+2);
450         load(path);
451         return (1);
452 }
453
454 static void
455 usage(void)
456 {
457
458         printf("usage: boot device [/path/to/loader]\n");
459         exit(1);
460 }
461
462 static void
463 exit(int code)
464 {
465
466         ofw_exit();
467 }
468
469 static struct dmadat __dmadat;
470
471 static int
472 domount(const char *device, int quiet)
473 {
474
475         dmadat = &__dmadat;
476         if ((bootdev = ofw_open(device)) == -1) {
477                 printf("domount: can't open device\n");
478                 return (-1);
479         }
480         if (fsread(0, NULL, 0)) {
481                 if (!quiet)
482                         printf("domount: can't read superblock\n");
483                 return (-1);
484         }
485         return (0);
486 }
487
488 static void
489 load(const char *fname)
490 {
491         Elf32_Ehdr eh;
492         Elf32_Phdr ph;
493         caddr_t p;
494         ufs_ino_t ino;
495         int i;
496
497         if ((ino = lookup(fname)) == 0) {
498                 printf("File %s not found\n", fname);
499                 return;
500         }
501         if (fsread(ino, &eh, sizeof(eh)) != sizeof(eh)) {
502                 printf("Can't read elf header\n");
503                 return;
504         }
505         if (!IS_ELF(eh)) {
506                 printf("Not an ELF file\n");
507                 return;
508         }
509         for (i = 0; i < eh.e_phnum; i++) {
510                 fs_off = eh.e_phoff + i * eh.e_phentsize;
511                 if (fsread(ino, &ph, sizeof(ph)) != sizeof(ph)) {
512                         printf("Can't read program header %d\n", i);
513                         return;
514                 }
515                 if (ph.p_type != PT_LOAD)
516                         continue;
517                 fs_off = ph.p_offset;
518                 p = (caddr_t)ph.p_vaddr;
519                 ofw_claim(p,(ph.p_filesz > ph.p_memsz) ? 
520                     ph.p_filesz : ph.p_memsz,0);
521                 if (fsread(ino, p, ph.p_filesz) != ph.p_filesz) {
522                         printf("Can't read content of section %d\n", i);
523                         return;
524                 }
525                 if (ph.p_filesz != ph.p_memsz)
526                         bzero(p + ph.p_filesz, ph.p_memsz - ph.p_filesz);
527                 __syncicache(p, ph.p_memsz);
528         }
529         ofw_close(bootdev);
530         (*(void (*)(void *, int, ofwfp_t, char *, int))eh.e_entry)(NULL, 0, 
531             ofw,NULL,0);
532 }
533
534 static int
535 dskread(void *buf, u_int64_t lba, int nblk)
536 {
537         /*
538          * The Open Firmware should open the correct partition for us.
539          * That means, if we read from offset zero on an open instance handle,
540          * we should read from offset zero of that partition.
541          */
542         ofw_seek(bootdev, lba * DEV_BSIZE);
543         ofw_read(bootdev, buf, nblk * DEV_BSIZE);
544         return (0);
545 }
546
547 static void
548 panic(const char *fmt, ...)
549 {
550         char buf[128];
551         va_list ap;
552
553         va_start(ap, fmt);
554         vsnprintf(buf, sizeof buf, fmt, ap);
555         printf("panic: %s\n", buf);
556         va_end(ap);
557
558         exit(1);
559 }
560
561 static int
562 printf(const char *fmt, ...)
563 {
564         va_list ap;
565         int ret;
566
567         va_start(ap, fmt);
568         ret = vprintf(fmt, ap);
569         va_end(ap);
570         return (ret);
571 }
572
573 static int
574 putchar(char c, void *arg)
575 {
576         char buf;
577
578         if (c == '\n') {
579                 buf = '\r';
580                 ofw_write(stdouth, &buf, 1);
581         }
582         buf = c;
583         ofw_write(stdouth, &buf, 1);
584         return (1);
585 }
586
587 static int
588 vprintf(const char *fmt, va_list ap)
589 {
590         int ret;
591
592         ret = __printf(fmt, putchar, 0, ap);
593         return (ret);
594 }
595
596 static int
597 vsnprintf(char *str, size_t sz, const char *fmt, va_list ap)
598 {
599         struct sp_data sp;
600         int ret;
601
602         sp.sp_buf = str;
603         sp.sp_len = 0;
604         sp.sp_size = sz;
605         ret = __printf(fmt, __sputc, &sp, ap);
606         return (ret);
607 }
608
609 static int
610 __printf(const char *fmt, putc_func_t *putc, void *arg, va_list ap)
611 {
612         char buf[(sizeof(long) * 8) + 1];
613         char *nbuf;
614         u_long ul;
615         u_int ui;
616         int lflag;
617         int sflag;
618         char *s;
619         int pad;
620         int ret;
621         int c;
622
623         nbuf = &buf[sizeof buf - 1];
624         ret = 0;
625         while ((c = *fmt++) != 0) {
626                 if (c != '%') {
627                         ret += putc(c, arg);
628                         continue;
629                 }
630                 lflag = 0;
631                 sflag = 0;
632                 pad = 0;
633 reswitch:       c = *fmt++;
634                 switch (c) {
635                 case '#':
636                         sflag = 1;
637                         goto reswitch;
638                 case '%':
639                         ret += putc('%', arg);
640                         break;
641                 case 'c':
642                         c = va_arg(ap, int);
643                         ret += putc(c, arg);
644                         break;
645                 case 'd':
646                         if (lflag == 0) {
647                                 ui = (u_int)va_arg(ap, int);
648                                 if (ui < (int)ui) {
649                                         ui = -ui;
650                                         ret += putc('-', arg);
651                                 }
652                                 s = __uitoa(nbuf, ui, 10);
653                         } else {
654                                 ul = (u_long)va_arg(ap, long);
655                                 if (ul < (long)ul) {
656                                         ul = -ul;
657                                         ret += putc('-', arg);
658                                 }
659                                 s = __ultoa(nbuf, ul, 10);
660                         }
661                         ret += __puts(s, putc, arg);
662                         break;
663                 case 'l':
664                         lflag = 1;
665                         goto reswitch;
666                 case 'o':
667                         if (lflag == 0) {
668                                 ui = (u_int)va_arg(ap, u_int);
669                                 s = __uitoa(nbuf, ui, 8);
670                         } else {
671                                 ul = (u_long)va_arg(ap, u_long);
672                                 s = __ultoa(nbuf, ul, 8);
673                         }
674                         ret += __puts(s, putc, arg);
675                         break;
676                 case 'p':
677                         ul = (u_long)va_arg(ap, void *);
678                         s = __ultoa(nbuf, ul, 16);
679                         ret += __puts("0x", putc, arg);
680                         ret += __puts(s, putc, arg);
681                         break;
682                 case 's':
683                         s = va_arg(ap, char *);
684                         ret += __puts(s, putc, arg);
685                         break;
686                 case 'u':
687                         if (lflag == 0) {
688                                 ui = va_arg(ap, u_int);
689                                 s = __uitoa(nbuf, ui, 10);
690                         } else {
691                                 ul = va_arg(ap, u_long);
692                                 s = __ultoa(nbuf, ul, 10);
693                         }
694                         ret += __puts(s, putc, arg);
695                         break;
696                 case 'x':
697                         if (lflag == 0) {
698                                 ui = va_arg(ap, u_int);
699                                 s = __uitoa(nbuf, ui, 16);
700                         } else {
701                                 ul = va_arg(ap, u_long);
702                                 s = __ultoa(nbuf, ul, 16);
703                         }
704                         if (sflag)
705                                 ret += __puts("0x", putc, arg);
706                         ret += __puts(s, putc, arg);
707                         break;
708                 case '0': case '1': case '2': case '3': case '4':
709                 case '5': case '6': case '7': case '8': case '9':
710                         pad = pad * 10 + c - '0';
711                         goto reswitch;
712                 default:
713                         break;
714                 }
715         }
716         return (ret);
717 }
718
719 static int
720 __sputc(char c, void *arg)
721 {
722         struct sp_data *sp;
723
724         sp = arg;
725         if (sp->sp_len < sp->sp_size)
726                 sp->sp_buf[sp->sp_len++] = c;
727         sp->sp_buf[sp->sp_len] = '\0';
728         return (1);
729 }
730
731 static int
732 __puts(const char *s, putc_func_t *putc, void *arg)
733 {
734         const char *p;
735         int ret;
736
737         ret = 0;
738         for (p = s; *p != '\0'; p++)
739                 ret += putc(*p, arg);
740         return (ret);
741 }
742
743 static char *
744 __uitoa(char *buf, u_int ui, int base)
745 {
746         char *p;
747
748         p = buf;
749         *p = '\0';
750         do
751                 *--p = digits[ui % base];
752         while ((ui /= base) != 0);
753         return (p);
754 }
755
756 static char *
757 __ultoa(char *buf, u_long ul, int base)
758 {
759         char *p;
760
761         p = buf;
762         *p = '\0';
763         do
764                 *--p = digits[ul % base];
765         while ((ul /= base) != 0);
766         return (p);
767 }