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