main.c revision 1.15 1 /* $NetBSD: main.c,v 1.15 2011/11/06 20:20:57 phx Exp $ */
2
3 /*-
4 * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Tohru Nishimura.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/param.h>
33 #include <sys/reboot.h>
34
35 #include <lib/libsa/stand.h>
36 #include <lib/libsa/loadfile.h>
37 #include <lib/libkern/libkern.h>
38
39 #include <machine/bootinfo.h>
40
41 #include "globals.h"
42
43 static const struct bootarg {
44 const char *name;
45 int value;
46 } bootargs[] = {
47 { "multi", RB_AUTOBOOT },
48 { "auto", RB_AUTOBOOT },
49 { "ask", RB_ASKNAME },
50 { "single", RB_SINGLE },
51 { "ddb", RB_KDB },
52 { "userconf", RB_USERCONF },
53 { "norm", AB_NORMAL },
54 { "quiet", AB_QUIET },
55 { "verb", AB_VERBOSE },
56 { "silent", AB_SILENT },
57 { "debug", AB_DEBUG },
58 { "altboot", -1 }
59 };
60
61 void *bootinfo; /* low memory reserved to pass bootinfo structures */
62 int bi_size; /* BOOTINFO_MAXSIZE */
63 char *bi_next;
64
65 void bi_init(void *);
66 void bi_add(void *, int, int);
67
68 struct btinfo_memory bi_mem;
69 struct btinfo_console bi_cons;
70 struct btinfo_clock bi_clk;
71 struct btinfo_prodfamily bi_fam;
72 struct btinfo_bootpath bi_path;
73 struct btinfo_rootdevice bi_rdev;
74 struct btinfo_net bi_net;
75 struct btinfo_modulelist *btinfo_modulelist;
76 size_t btinfo_modulelist_size;
77
78 struct boot_module {
79 char *bm_kmod;
80 ssize_t bm_len;
81 struct boot_module *bm_next;
82 };
83 struct boot_module *boot_modules;
84 char module_base[80];
85 uint32_t kmodloadp;
86 int modules_enabled = 0;
87
88 void module_add(char *);
89 void module_load(char *);
90 int module_open(struct boot_module *);
91
92 void main(int, char **, char *, char *);
93
94 extern char bootprog_name[], bootprog_rev[];
95 extern char newaltboot[], newaltboot_end[];
96
97 struct pcidev lata[2];
98 struct pcidev lnif[1];
99 struct pcidev lusb[3];
100 int nata, nnif, nusb;
101
102 int brdtype;
103 uint32_t busclock, cpuclock;
104
105 static int check_bootname(char *);
106 static int input_cmdline(char **, int);
107 static int parse_cmdline(char **, int, char *, char *);
108 static int is_space(char);
109 #ifdef DEBUG
110 static void sat_test(void);
111 #endif
112
113 #define BNAME_DEFAULT "wd0:"
114 #define MAX_ARGS 10
115
116 void
117 main(int argc, char *argv[], char *bootargs_start, char *bootargs_end)
118 {
119 struct brdprop *brdprop;
120 unsigned long marks[MARK_MAX];
121 char *new_argv[MAX_ARGS];
122 ssize_t len;
123 int n, i, fd, howto;
124 char *bname;
125
126 printf("\n");
127 printf(">> %s altboot, revision %s\n", bootprog_name, bootprog_rev);
128
129 brdprop = brd_lookup(brdtype);
130 printf(">> %s, cpu %u MHz, bus %u MHz, %dMB SDRAM\n", brdprop->verbose,
131 cpuclock / 1000000, busclock / 1000000, bi_mem.memsize >> 20);
132
133 nata = pcilookup(PCI_CLASS_IDE, lata, 2);
134 if (nata == 0)
135 nata = pcilookup(PCI_CLASS_RAID, lata, 2);
136 if (nata == 0)
137 nata = pcilookup(PCI_CLASS_MISCSTORAGE, lata, 2);
138 if (nata == 0)
139 nata = pcilookup(PCI_CLASS_SCSI, lata, 2);
140 nnif = pcilookup(PCI_CLASS_ETH, lnif, 1);
141 nusb = pcilookup(PCI_CLASS_USB, lusb, 3);
142
143 #ifdef DEBUG
144 if (nata == 0)
145 printf("No IDE/SATA found\n");
146 else for (n = 0; n < nata; n++) {
147 int b, d, f, bdf, pvd;
148 bdf = lata[n].bdf;
149 pvd = lata[n].pvd;
150 pcidecomposetag(bdf, &b, &d, &f);
151 printf("%04x.%04x DSK %02d:%02d:%02d\n",
152 PCI_VENDOR(pvd), PCI_PRODUCT(pvd), b, d, f);
153 }
154 if (nnif == 0)
155 printf("no NET found\n");
156 else {
157 int b, d, f, bdf, pvd;
158 bdf = lnif[0].bdf;
159 pvd = lnif[0].pvd;
160 pcidecomposetag(bdf, &b, &d, &f);
161 printf("%04x.%04x NET %02d:%02d:%02d\n",
162 PCI_VENDOR(pvd), PCI_PRODUCT(pvd), b, d, f);
163 }
164 if (nusb == 0)
165 printf("no USB found\n");
166 else for (n = 0; n < nusb; n++) {
167 int b, d, f, bdf, pvd;
168 bdf = lusb[0].bdf;
169 pvd = lusb[0].pvd;
170 pcidecomposetag(bdf, &b, &d, &f);
171 printf("%04x.%04x USB %02d:%02d:%02d\n",
172 PCI_VENDOR(pvd), PCI_PRODUCT(pvd), b, d, f);
173 }
174 #endif
175
176 pcisetup();
177 pcifixup();
178
179 if (dskdv_init(&lata[0]) == 0
180 || (nata == 2 && dskdv_init(&lata[1]) == 0))
181 printf("IDE/SATA device driver was not found\n");
182
183 if (netif_init(&lnif[0]) == 0)
184 printf("no NET device driver was found\n");
185
186 /*
187 * When argc is too big then it is probably a pointer, which could
188 * indicate that we were launched as a Linux kernel module using
189 * "bootm".
190 */
191 if (argc > MAX_ARGS) {
192 if (argv != NULL) {
193 /*
194 * initrd image was loaded: assume extremely
195 * restricted firmware and boot default
196 */
197 argc = 0;
198 } else {
199 /* parse standard Linux bootargs */
200 argc = parse_cmdline(new_argv, MAX_ARGS,
201 bootargs_start, bootargs_end);
202 argv = new_argv;
203 }
204 }
205
206 /* wait 2s for user to enter interactive mode */
207 for (n = 200; n >= 0; n--) {
208 if (n % 100 == 0)
209 printf("Hit any key to enter interactive mode: %d\r",
210 n / 100);
211 if (tstchar()) {
212 #ifdef DEBUG
213 if (toupper(getchar()) == 'C') {
214 /* controller test terminal */
215 sat_test();
216 n = 200;
217 continue;
218 }
219 #else
220 (void)getchar();
221 #endif
222 /* enter command line */
223 argv = new_argv;
224 argc = input_cmdline(argv, MAX_ARGS);
225 break;
226 }
227 delay(10000);
228 }
229 putchar('\n');
230
231 howto = RB_AUTOBOOT; /* default is autoboot = 0 */
232
233 /* get boot options and determine bootname */
234 for (n = 1; n < argc; n++) {
235 for (i = 0; i < sizeof(bootargs) / sizeof(bootargs[0]); i++) {
236 if (strncasecmp(argv[n], bootargs[i].name,
237 strlen(bootargs[i].name)) == 0) {
238 howto |= bootargs[i].value;
239 break;
240 }
241 }
242 if (i >= sizeof(bootargs) / sizeof(bootargs[0]))
243 break; /* break on first unknown string */
244 }
245 if (n >= argc)
246 bname = BNAME_DEFAULT;
247 else {
248 bname = argv[n];
249 if (check_bootname(bname) == 0) {
250 printf("%s not a valid bootname\n", bname);
251 goto loadfail;
252 }
253 }
254
255 if ((fd = open(bname, 0)) < 0) {
256 if (errno == ENOENT)
257 printf("\"%s\" not found\n", bi_path.bootpath);
258 goto loadfail;
259 }
260 printf("loading \"%s\" ", bi_path.bootpath);
261 marks[MARK_START] = 0;
262
263 if (howto == -1) {
264 /* load another altboot binary and replace ourselves */
265 len = read(fd, (void *)0x100000, 0x1000000 - 0x100000);
266 if (len == -1)
267 goto loadfail;
268 close(fd);
269 netif_shutdown_all();
270
271 memcpy((void *)0xf0000, newaltboot,
272 newaltboot_end - newaltboot);
273 __syncicache((void *)0xf0000, newaltboot_end - newaltboot);
274 printf("Restarting...\n");
275 run((void *)1, argv, (void *)0x100000, (void *)len,
276 (void *)0xf0000);
277 } else if (fdloadfile(fd, marks, LOAD_KERNEL) < 0)
278 goto loadfail;
279 close(fd);
280
281 printf("entry=%p, ssym=%p, esym=%p\n",
282 (void *)marks[MARK_ENTRY],
283 (void *)marks[MARK_SYM],
284 (void *)marks[MARK_END]);
285
286 bootinfo = (void *)0x4000;
287 bi_init(bootinfo);
288 bi_add(&bi_cons, BTINFO_CONSOLE, sizeof(bi_cons));
289 bi_add(&bi_mem, BTINFO_MEMORY, sizeof(bi_mem));
290 bi_add(&bi_clk, BTINFO_CLOCK, sizeof(bi_clk));
291 bi_add(&bi_path, BTINFO_BOOTPATH, sizeof(bi_path));
292 bi_add(&bi_rdev, BTINFO_ROOTDEVICE, sizeof(bi_rdev));
293 bi_add(&bi_fam, BTINFO_PRODFAMILY, sizeof(bi_fam));
294 if (brdtype == BRD_SYNOLOGY || brdtype == BRD_DLINKDSM) {
295 /* need to set this MAC address in kernel driver later */
296 bi_add(&bi_net, BTINFO_NET, sizeof(bi_net));
297 }
298
299 if (modules_enabled) {
300 module_add(fsmod);
301 if (fsmod2 != NULL && strcmp(fsmod, fsmod2) != 0)
302 module_add(fsmod2);
303 kmodloadp = marks[MARK_END];
304 btinfo_modulelist = NULL;
305 module_load(bname);
306 if (btinfo_modulelist != NULL && btinfo_modulelist->num > 0)
307 bi_add(btinfo_modulelist, BTINFO_MODULELIST,
308 btinfo_modulelist_size);
309 }
310
311 netif_shutdown_all();
312
313 __syncicache((void *)marks[MARK_ENTRY],
314 (u_int)marks[MARK_SYM] - (u_int)marks[MARK_ENTRY]);
315
316 run((void *)marks[MARK_SYM], (void *)marks[MARK_END],
317 (void *)howto, bootinfo, (void *)marks[MARK_ENTRY]);
318
319 /* should never come here */
320 printf("exec returned. Restarting...\n");
321 _rtt();
322
323 loadfail:
324 printf("load failed. Restarting...\n");
325 _rtt();
326 }
327
328 void
329 bi_init(void *addr)
330 {
331 struct btinfo_magic bi_magic;
332
333 memset(addr, 0, BOOTINFO_MAXSIZE);
334 bi_next = (char *)addr;
335 bi_size = 0;
336
337 bi_magic.magic = BOOTINFO_MAGIC;
338 bi_add(&bi_magic, BTINFO_MAGIC, sizeof(bi_magic));
339 }
340
341 void
342 bi_add(void *new, int type, int size)
343 {
344 struct btinfo_common *bi;
345
346 if (bi_size + size > BOOTINFO_MAXSIZE)
347 return; /* XXX error? */
348
349 bi = new;
350 bi->next = size;
351 bi->type = type;
352 memcpy(bi_next, new, size);
353 bi_next += size;
354 }
355
356 void
357 module_add(char *name)
358 {
359 struct boot_module *bm, *bmp;
360
361 while (*name == ' ' || *name == '\t')
362 ++name;
363
364 bm = alloc(sizeof(struct boot_module) + strlen(name) + 1);
365 if (bm == NULL) {
366 printf("couldn't allocate module %s\n", name);
367 return;
368 }
369
370 bm->bm_kmod = (char *)(bm + 1);
371 bm->bm_len = -1;
372 bm->bm_next = NULL;
373 strcpy(bm->bm_kmod, name);
374 if ((bmp = boot_modules) == NULL)
375 boot_modules = bm;
376 else {
377 while (bmp->bm_next != NULL)
378 bmp = bmp->bm_next;
379 bmp->bm_next = bm;
380 }
381 }
382
383 #define PAGE_SIZE 4096
384 #define alignpg(x) (((x)+PAGE_SIZE-1) & ~(PAGE_SIZE-1))
385
386 void
387 module_load(char *kernel_path)
388 {
389 struct boot_module *bm;
390 struct bi_modulelist_entry *bi;
391 struct stat st;
392 char *p;
393 int size, fd;
394
395 strcpy(module_base, kernel_path);
396 if ((p = strchr(module_base, ':')) == NULL)
397 return; /* eeh?! */
398 p += 1;
399 size = sizeof(module_base) - (p - module_base);
400
401 if (netbsd_version / 1000000 % 100 == 99) {
402 /* -current */
403 snprintf(p, size,
404 "/stand/sandpoint/%d.%d.%d/modules",
405 netbsd_version / 100000000,
406 netbsd_version / 1000000 % 100,
407 netbsd_version / 100 % 100);
408 }
409 else if (netbsd_version != 0) {
410 /* release */
411 snprintf(p, size,
412 "/stand/sandpoint/%d.%d/modules",
413 netbsd_version / 100000000,
414 netbsd_version / 1000000 % 100);
415 }
416
417 /*
418 * 1st pass; determine module existence
419 */
420 size = 0;
421 for (bm = boot_modules; bm != NULL; bm = bm->bm_next) {
422 fd = module_open(bm);
423 if (fd == -1)
424 continue;
425 if (fstat(fd, &st) == -1 || st.st_size == -1) {
426 printf("WARNING: couldn't stat %s\n", bm->bm_kmod);
427 close(fd);
428 continue;
429 }
430 bm->bm_len = (int)st.st_size;
431 close(fd);
432 size += sizeof(struct bi_modulelist_entry);
433 }
434 if (size == 0)
435 return;
436
437 size += sizeof(struct btinfo_modulelist);
438 btinfo_modulelist = alloc(size);
439 if (btinfo_modulelist == NULL) {
440 printf("WARNING: couldn't allocate module list\n");
441 return;
442 }
443 btinfo_modulelist_size = size;
444 btinfo_modulelist->num = 0;
445
446 /*
447 * 2nd pass; load modules into memory
448 */
449 kmodloadp = alignpg(kmodloadp);
450 bi = (struct bi_modulelist_entry *)(btinfo_modulelist + 1);
451 for (bm = boot_modules; bm != NULL; bm = bm->bm_next) {
452 if (bm->bm_len == -1)
453 continue; /* already found unavailable */
454 fd = module_open(bm);
455 printf("module \"%s\" ", bm->bm_kmod);
456 size = read(fd, (char *)kmodloadp, SSIZE_MAX);
457 if (size < bm->bm_len)
458 printf("WARNING: couldn't load");
459 else {
460 snprintf(bi->kmod, sizeof(bi->kmod), bm->bm_kmod);
461 bi->type = BI_MODULE_ELF;
462 bi->len = size;
463 bi->base = kmodloadp;
464 btinfo_modulelist->num += 1;
465 printf("loaded at 0x%08x size 0x%x", kmodloadp, size);
466 kmodloadp += alignpg(size);
467 bi += 1;
468 }
469 printf("\n");
470 close(fd);
471 }
472 btinfo_modulelist->endpa = kmodloadp;
473 }
474
475 int
476 module_open(struct boot_module *bm)
477 {
478 char path[80];
479 int fd;
480
481 snprintf(path, sizeof(path),
482 "%s/%s/%s.kmod", module_base, bm->bm_kmod, bm->bm_kmod);
483 fd = open(path, 0);
484 return fd;
485 }
486
487 #if 0
488 static const char *cmdln[] = {
489 "console=ttyS0,115200 root=/dev/sda1 rw initrd=0x200000,2M",
490 "console=ttyS0,115200 root=/dev/nfs ip=dhcp"
491 };
492
493 void
494 mkatagparams(unsigned addr, char *kcmd)
495 {
496 struct tag {
497 unsigned siz;
498 unsigned tag;
499 unsigned val[1];
500 };
501 struct tag *p;
502 #define ATAG_CORE 0x54410001
503 #define ATAG_MEM 0x54410002
504 #define ATAG_INITRD 0x54410005
505 #define ATAG_CMDLINE 0x54410009
506 #define ATAG_NONE 0x00000000
507 #define tagnext(p) (struct tag *)((unsigned *)(p) + (p)->siz)
508 #define tagsize(n) (2 + (n))
509
510 p = (struct tag *)addr;
511 p->tag = ATAG_CORE;
512 p->siz = tagsize(3);
513 p->val[0] = 0; /* flags */
514 p->val[1] = 0; /* pagesize */
515 p->val[2] = 0; /* rootdev */
516 p = tagnext(p);
517 p->tag = ATAG_MEM;
518 p->siz = tagsize(2);
519 p->val[0] = 64 * 1024 * 1024;
520 p->val[1] = 0; /* start */
521 p = tagnext(p);
522 if (kcmd != NULL) {
523 p = tagnext(p);
524 p->tag = ATAG_CMDLINE;
525 p->siz = tagsize((strlen(kcmd) + 1 + 3) >> 2);
526 strcpy((void *)p->val, kcmd);
527 }
528 p = tagnext(p);
529 p->tag = ATAG_NONE;
530 p->siz = 0;
531 }
532 #endif
533
534 void *
535 allocaligned(size_t size, size_t align)
536 {
537 uint32_t p;
538
539 if (align-- < 2)
540 return alloc(size);
541 p = (uint32_t)alloc(size + align);
542 return (void *)((p + align) & ~align);
543 }
544
545 static int hex2nibble(char c)
546 {
547
548 if (c >= 'a')
549 c &= ~0x20;
550 if (c >= 'A' && c <= 'F')
551 c -= 'A' - ('9' + 1);
552 else if (c < '0' || c > '9')
553 return -1;
554
555 return c - '0';
556 }
557
558 uint32_t
559 read_hex(const char *s)
560 {
561 int n;
562 uint32_t val;
563
564 val = 0;
565 while ((n = hex2nibble(*s++)) >= 0)
566 val = (val << 4) | n;
567 return val;
568 }
569
570 static int
571 check_bootname(char *s)
572 {
573 /*
574 * nfs:
575 * nfs:<bootfile>
576 * tftp:
577 * tftp:<bootfile>
578 * wd[N[P]]:<bootfile>
579 * mem:<address>
580 *
581 * net is a synonym of nfs.
582 */
583 if (strncmp(s, "nfs:", 4) == 0 || strncmp(s, "net:", 4) == 0 ||
584 strncmp(s, "tftp:", 5) == 0 || strncmp(s, "mem:", 4) == 0)
585 return 1;
586 if (s[0] == 'w' && s[1] == 'd') {
587 s += 2;
588 if (*s != ':' && *s >= '0' && *s <= '3') {
589 ++s;
590 if (*s != ':' && *s >= 'a' && *s <= 'p')
591 ++s;
592 }
593 return *s == ':';
594 }
595 return 0;
596 }
597
598 static int input_cmdline(char **argv, int maxargc)
599 {
600 char *cmdline;
601
602 printf("\nbootargs> ");
603 cmdline = alloc(256);
604 gets(cmdline);
605
606 return parse_cmdline(argv, maxargc, cmdline,
607 cmdline + strlen(cmdline));
608 }
609
610 static int
611 parse_cmdline(char **argv, int maxargc, char *p, char *end)
612 {
613 int argc;
614
615 argv[0] = "";
616 for (argc = 1; argc < maxargc && p < end; argc++) {
617 while (is_space(*p))
618 p++;
619 if (p >= end)
620 break;
621 argv[argc] = p;
622 while (!is_space(*p) && p < end)
623 p++;
624 *p++ = '\0';
625 }
626
627 return argc;
628 }
629
630 static int
631 is_space(char c)
632 {
633
634 return c > '\0' && c <= ' ';
635 }
636
637 #ifdef DEBUG
638 static void
639 sat_test(void)
640 {
641 char buf[1024];
642 int i, j, n, pos;
643 unsigned char c;
644
645 putchar('\n');
646 for (;;) {
647 do {
648 for (pos = 0; pos < 1024 && sat_tstch() != 0; pos++)
649 buf[pos] = sat_getch();
650 if (pos > 1023)
651 break;
652 delay(100000);
653 } while (sat_tstch());
654
655 for (i = 0; i < pos; i += 16) {
656 if ((n = i + 16) > pos)
657 n = pos;
658 for (j = 0; j < n; j++)
659 printf("%02x ", (unsigned)buf[i + j]);
660 for (; j < 16; j++)
661 printf(" ");
662 putchar('\"');
663 for (j = 0; j < n; j++) {
664 c = buf[i + j];
665 putchar((c >= 0x20 && c <= 0x7e) ? c : '.');
666 }
667 printf("\"\n");
668 }
669
670 printf("controller> ");
671 gets(buf);
672 if (buf[0] == '*' && buf[1] == 'X')
673 break;
674
675 if (buf[0] == '0' && tolower((unsigned)buf[1]) == 'x') {
676 for (i = 2, n = 0, c = 0; buf[i]; i++) {
677 c <<= 4;
678 c |= hex2nibble(buf[i]);
679 if (i & 1)
680 buf[n++] = c;
681 }
682 } else
683 n = strlen(buf);
684
685 if (n > 0)
686 sat_write(buf, n);
687 }
688 }
689 #endif
690