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