main.c revision 1.7.2.3 1 /* $NetBSD: main.c,v 1.7.2.3 2011/04/21 01:41:22 rmind 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
110 #define BNAME_DEFAULT "wd0:"
111 #define MAX_ARGS 10
112
113 void
114 main(int argc, char *argv[], char *bootargs_start, char *bootargs_end)
115 {
116 struct brdprop *brdprop;
117 unsigned long marks[MARK_MAX];
118 char *new_argv[MAX_ARGS];
119 ssize_t len;
120 int n, i, fd, howto;
121 char *bname;
122
123 printf("\n");
124 printf(">> %s altboot, revision %s\n", bootprog_name, bootprog_rev);
125
126 brdprop = brd_lookup(brdtype);
127 printf(">> %s, cpu %u MHz, bus %u MHz, %dMB SDRAM\n", brdprop->verbose,
128 cpuclock / 1000000, busclock / 1000000, bi_mem.memsize >> 20);
129
130 nata = pcilookup(PCI_CLASS_IDE, lata, 2);
131 if (nata == 0)
132 nata = pcilookup(PCI_CLASS_MISCSTORAGE, lata, 2);
133 if (nata == 0)
134 nata = pcilookup(PCI_CLASS_SCSI, lata, 2);
135 nnif = pcilookup(PCI_CLASS_ETH, lnif, 1);
136 nusb = pcilookup(PCI_CLASS_USB, lusb, 3);
137
138 #ifdef DEBUG
139 if (nata == 0)
140 printf("No IDE/SATA found\n");
141 else for (n = 0; n < nata; n++) {
142 int b, d, f, bdf, pvd;
143 bdf = lata[n].bdf;
144 pvd = lata[n].pvd;
145 pcidecomposetag(bdf, &b, &d, &f);
146 printf("%04x.%04x DSK %02d:%02d:%02d\n",
147 PCI_VENDOR(pvd), PCI_PRODUCT(pvd), b, d, f);
148 }
149 if (nnif == 0)
150 printf("no NET found\n");
151 else {
152 int b, d, f, bdf, pvd;
153 bdf = lnif[0].bdf;
154 pvd = lnif[0].pvd;
155 pcidecomposetag(bdf, &b, &d, &f);
156 printf("%04x.%04x NET %02d:%02d:%02d\n",
157 PCI_VENDOR(pvd), PCI_PRODUCT(pvd), b, d, f);
158 }
159 if (nusb == 0)
160 printf("no USB found\n");
161 else for (n = 0; n < nusb; n++) {
162 int b, d, f, bdf, pvd;
163 bdf = lusb[0].bdf;
164 pvd = lusb[0].pvd;
165 pcidecomposetag(bdf, &b, &d, &f);
166 printf("%04x.%04x USB %02d:%02d:%02d\n",
167 PCI_VENDOR(pvd), PCI_PRODUCT(pvd), b, d, f);
168 }
169 #endif
170
171 pcisetup();
172 pcifixup();
173
174 if (dskdv_init(&lata[0]) == 0
175 || (nata == 2 && dskdv_init(&lata[1]) == 0))
176 printf("IDE/SATA device driver was not found\n");
177
178 if (netif_init(&lnif[0]) == 0)
179 printf("no NET device driver was found\n");
180
181 /*
182 * When argc is too big then it is probably a pointer, which could
183 * indicate that we were launched as a Linux kernel module using
184 * "bootm".
185 */
186 if (argc > MAX_ARGS) {
187 if (argv != NULL) {
188 /*
189 * initrd image was loaded: assume extremely
190 * restricted firmware and boot default
191 */
192 argc = 0;
193 } else {
194 /* parse standard Linux bootargs */
195 argc = parse_cmdline(new_argv, MAX_ARGS,
196 bootargs_start, bootargs_end);
197 argv = new_argv;
198 }
199 }
200
201 /* wait 2s for user to enter interactive mode */
202 for (n = 200; n >= 0; n--) {
203 if (n % 100 == 0)
204 printf("Hit any key to enter interactive mode: %d\r",
205 n / 100);
206 if (tstchar()) {
207 (void)getchar();
208 argv = new_argv;
209 argc = input_cmdline(argv, MAX_ARGS);
210 break;
211 }
212 delay(10000);
213 }
214 putchar('\n');
215
216 howto = RB_AUTOBOOT; /* default is autoboot = 0 */
217
218 /* get boot options and determine bootname */
219 for (n = 1; n < argc; n++) {
220 for (i = 0; i < sizeof(bootargs) / sizeof(bootargs[0]); i++) {
221 if (strncasecmp(argv[n], bootargs[i].name,
222 strlen(bootargs[i].name)) == 0) {
223 howto |= bootargs[i].value;
224 break;
225 }
226 }
227 if (i >= sizeof(bootargs) / sizeof(bootargs[0]))
228 break; /* break on first unknown string */
229 }
230 if (n >= argc)
231 bname = BNAME_DEFAULT;
232 else {
233 bname = argv[n];
234 if (check_bootname(bname) == 0) {
235 printf("%s not a valid bootname\n", bname);
236 goto loadfail;
237 }
238 }
239
240 if ((fd = open(bname, 0)) < 0) {
241 if (errno == ENOENT)
242 printf("\"%s\" not found\n", bi_path.bootpath);
243 goto loadfail;
244 }
245 printf("loading \"%s\" ", bi_path.bootpath);
246 marks[MARK_START] = 0;
247
248 if (howto == -1) {
249 /* load another altboot binary and replace ourselves */
250 len = read(fd, (void *)0x100000, 0x1000000 - 0x100000);
251 if (len == -1)
252 goto loadfail;
253 close(fd);
254 netif_shutdown_all();
255
256 memcpy((void *)0xf0000, newaltboot,
257 newaltboot_end - newaltboot);
258 __syncicache((void *)0xf0000, newaltboot_end - newaltboot);
259 printf("Restarting...\n");
260 run((void *)1, argv, (void *)0x100000, (void *)len,
261 (void *)0xf0000);
262 } else if (fdloadfile(fd, marks, LOAD_KERNEL) < 0)
263 goto loadfail;
264 close(fd);
265
266 printf("entry=%p, ssym=%p, esym=%p\n",
267 (void *)marks[MARK_ENTRY],
268 (void *)marks[MARK_SYM],
269 (void *)marks[MARK_END]);
270
271 bootinfo = (void *)0x4000;
272 bi_init(bootinfo);
273 bi_add(&bi_cons, BTINFO_CONSOLE, sizeof(bi_cons));
274 bi_add(&bi_mem, BTINFO_MEMORY, sizeof(bi_mem));
275 bi_add(&bi_clk, BTINFO_CLOCK, sizeof(bi_clk));
276 bi_add(&bi_path, BTINFO_BOOTPATH, sizeof(bi_path));
277 bi_add(&bi_rdev, BTINFO_ROOTDEVICE, sizeof(bi_rdev));
278 bi_add(&bi_fam, BTINFO_PRODFAMILY, sizeof(bi_fam));
279 if (brdtype == BRD_SYNOLOGY || brdtype == BRD_DLINKDSM) {
280 /* need to set this MAC address in kernel driver later */
281 bi_add(&bi_net, BTINFO_NET, sizeof(bi_net));
282 }
283
284 if (modules_enabled) {
285 module_add(fsmod);
286 if (fsmod2 != NULL && strcmp(fsmod, fsmod2) != 0)
287 module_add(fsmod2);
288 kmodloadp = marks[MARK_END];
289 btinfo_modulelist = NULL;
290 module_load(bname);
291 if (btinfo_modulelist != NULL && btinfo_modulelist->num > 0)
292 bi_add(btinfo_modulelist, BTINFO_MODULELIST,
293 btinfo_modulelist_size);
294 }
295
296 netif_shutdown_all();
297
298 __syncicache((void *)marks[MARK_ENTRY],
299 (u_int)marks[MARK_SYM] - (u_int)marks[MARK_ENTRY]);
300
301 run((void *)marks[MARK_SYM], (void *)marks[MARK_END],
302 (void *)howto, bootinfo, (void *)marks[MARK_ENTRY]);
303
304 /* should never come here */
305 printf("exec returned. Restarting...\n");
306 _rtt();
307
308 loadfail:
309 printf("load failed. Restarting...\n");
310 _rtt();
311 }
312
313 void
314 bi_init(void *addr)
315 {
316 struct btinfo_magic bi_magic;
317
318 memset(addr, 0, BOOTINFO_MAXSIZE);
319 bi_next = (char *)addr;
320 bi_size = 0;
321
322 bi_magic.magic = BOOTINFO_MAGIC;
323 bi_add(&bi_magic, BTINFO_MAGIC, sizeof(bi_magic));
324 }
325
326 void
327 bi_add(void *new, int type, int size)
328 {
329 struct btinfo_common *bi;
330
331 if (bi_size + size > BOOTINFO_MAXSIZE)
332 return; /* XXX error? */
333
334 bi = new;
335 bi->next = size;
336 bi->type = type;
337 memcpy(bi_next, new, size);
338 bi_next += size;
339 }
340
341 void
342 module_add(char *name)
343 {
344 struct boot_module *bm, *bmp;
345
346 while (*name == ' ' || *name == '\t')
347 ++name;
348
349 bm = alloc(sizeof(struct boot_module) + strlen(name) + 1);
350 if (bm == NULL) {
351 printf("couldn't allocate module %s\n", name);
352 return;
353 }
354
355 bm->bm_kmod = (char *)(bm + 1);
356 bm->bm_len = -1;
357 bm->bm_next = NULL;
358 strcpy(bm->bm_kmod, name);
359 if ((bmp = boot_modules) == NULL)
360 boot_modules = bm;
361 else {
362 while (bmp->bm_next != NULL)
363 bmp = bmp->bm_next;
364 bmp->bm_next = bm;
365 }
366 }
367
368 #define PAGE_SIZE 4096
369 #define alignpg(x) (((x)+PAGE_SIZE-1) & ~(PAGE_SIZE-1))
370
371 void
372 module_load(char *kernel_path)
373 {
374 struct boot_module *bm;
375 struct bi_modulelist_entry *bi;
376 struct stat st;
377 char *p;
378 int size, fd;
379
380 strcpy(module_base, kernel_path);
381 if ((p = strchr(module_base, ':')) == NULL)
382 return; /* eeh?! */
383 p += 1;
384 size = sizeof(module_base) - (p - module_base);
385
386 if (netbsd_version / 1000000 % 100 == 99) {
387 /* -current */
388 snprintf(p, size,
389 "/stand/sandpoint/%d.%d.%d/modules",
390 netbsd_version / 100000000,
391 netbsd_version / 1000000 % 100,
392 netbsd_version / 100 % 100);
393 }
394 else if (netbsd_version != 0) {
395 /* release */
396 snprintf(p, size,
397 "/stand/sandpoint/%d.%d/modules",
398 netbsd_version / 100000000,
399 netbsd_version / 1000000 % 100);
400 }
401
402 /*
403 * 1st pass; determine module existence
404 */
405 size = 0;
406 for (bm = boot_modules; bm != NULL; bm = bm->bm_next) {
407 fd = module_open(bm);
408 if (fd == -1)
409 continue;
410 if (fstat(fd, &st) == -1 || st.st_size == -1) {
411 printf("WARNING: couldn't stat %s\n", bm->bm_kmod);
412 close(fd);
413 continue;
414 }
415 bm->bm_len = (int)st.st_size;
416 close(fd);
417 size += sizeof(struct bi_modulelist_entry);
418 }
419 if (size == 0)
420 return;
421
422 size += sizeof(struct btinfo_modulelist);
423 btinfo_modulelist = alloc(size);
424 if (btinfo_modulelist == NULL) {
425 printf("WARNING: couldn't allocate module list\n");
426 return;
427 }
428 btinfo_modulelist_size = size;
429 btinfo_modulelist->num = 0;
430
431 /*
432 * 2nd pass; load modules into memory
433 */
434 kmodloadp = alignpg(kmodloadp);
435 bi = (struct bi_modulelist_entry *)(btinfo_modulelist + 1);
436 for (bm = boot_modules; bm != NULL; bm = bm->bm_next) {
437 if (bm->bm_len == -1)
438 continue; /* already found unavailable */
439 fd = module_open(bm);
440 printf("module \"%s\" ", bm->bm_kmod);
441 size = read(fd, (char *)kmodloadp, SSIZE_MAX);
442 if (size < bm->bm_len)
443 printf("WARNING: couldn't load");
444 else {
445 snprintf(bi->kmod, sizeof(bi->kmod), bm->bm_kmod);
446 bi->type = BI_MODULE_ELF;
447 bi->len = size;
448 bi->base = kmodloadp;
449 btinfo_modulelist->num += 1;
450 printf("loaded at 0x%08x size 0x%x", kmodloadp, size);
451 kmodloadp += alignpg(size);
452 bi += 1;
453 }
454 printf("\n");
455 close(fd);
456 }
457 btinfo_modulelist->endpa = kmodloadp;
458 }
459
460 int
461 module_open(struct boot_module *bm)
462 {
463 char path[80];
464 int fd;
465
466 snprintf(path, sizeof(path),
467 "%s/%s/%s.kmod", module_base, bm->bm_kmod, bm->bm_kmod);
468 fd = open(path, 0);
469 return fd;
470 }
471
472 #if 0
473 static const char *cmdln[] = {
474 "console=ttyS0,115200 root=/dev/sda1 rw initrd=0x200000,2M",
475 "console=ttyS0,115200 root=/dev/nfs ip=dhcp"
476 };
477
478 void
479 mkatagparams(unsigned addr, char *kcmd)
480 {
481 struct tag {
482 unsigned siz;
483 unsigned tag;
484 unsigned val[1];
485 };
486 struct tag *p;
487 #define ATAG_CORE 0x54410001
488 #define ATAG_MEM 0x54410002
489 #define ATAG_INITRD 0x54410005
490 #define ATAG_CMDLINE 0x54410009
491 #define ATAG_NONE 0x00000000
492 #define tagnext(p) (struct tag *)((unsigned *)(p) + (p)->siz)
493 #define tagsize(n) (2 + (n))
494
495 p = (struct tag *)addr;
496 p->tag = ATAG_CORE;
497 p->siz = tagsize(3);
498 p->val[0] = 0; /* flags */
499 p->val[1] = 0; /* pagesize */
500 p->val[2] = 0; /* rootdev */
501 p = tagnext(p);
502 p->tag = ATAG_MEM;
503 p->siz = tagsize(2);
504 p->val[0] = 64 * 1024 * 1024;
505 p->val[1] = 0; /* start */
506 p = tagnext(p);
507 if (kcmd != NULL) {
508 p = tagnext(p);
509 p->tag = ATAG_CMDLINE;
510 p->siz = tagsize((strlen(kcmd) + 1 + 3) >> 2);
511 strcpy((void *)p->val, kcmd);
512 }
513 p = tagnext(p);
514 p->tag = ATAG_NONE;
515 p->siz = 0;
516 }
517 #endif
518
519 void *
520 allocaligned(size_t size, size_t align)
521 {
522 uint32_t p;
523
524 if (align-- < 2)
525 return alloc(size);
526 p = (uint32_t)alloc(size + align);
527 return (void *)((p + align) & ~align);
528 }
529
530 static int hex2nibble(char c)
531 {
532
533 if (c >= 'a')
534 c &= ~0x20;
535 if (c >= 'A' && c <= 'F')
536 c -= 'A' - ('9' + 1);
537 else if (c < '0' || c > '9')
538 return -1;
539
540 return c - '0';
541 }
542
543 uint32_t
544 read_hex(const char *s)
545 {
546 int n;
547 uint32_t val;
548
549 val = 0;
550 while ((n = hex2nibble(*s++)) >= 0)
551 val = (val << 4) | n;
552 return val;
553 }
554
555 static int
556 check_bootname(char *s)
557 {
558 /*
559 * nfs:
560 * nfs:<bootfile>
561 * tftp:
562 * tftp:<bootfile>
563 * wd[N[P]]:<bootfile>
564 * mem:<address>
565 *
566 * net is a synonym of nfs.
567 */
568 if (strncmp(s, "nfs:", 4) == 0 || strncmp(s, "net:", 4) == 0 ||
569 strncmp(s, "tftp:", 5) == 0 || strncmp(s, "mem:", 4) == 0)
570 return 1;
571 if (s[0] == 'w' && s[1] == 'd') {
572 s += 2;
573 if (*s != ':' && *s >= '0' && *s <= '3') {
574 ++s;
575 if (*s != ':' && *s >= 'a' && *s <= 'p')
576 ++s;
577 }
578 return *s == ':';
579 }
580 return 0;
581 }
582
583 static int input_cmdline(char **argv, int maxargc)
584 {
585 char *cmdline;
586
587 printf("\nbootargs> ");
588 cmdline = alloc(256);
589 gets(cmdline);
590
591 return parse_cmdline(argv, maxargc, cmdline,
592 cmdline + strlen(cmdline));
593 }
594
595 static int
596 parse_cmdline(char **argv, int maxargc, char *p, char *end)
597 {
598 int argc;
599
600 argv[0] = "";
601 for (argc = 1; argc < maxargc && p < end; argc++) {
602 while (is_space(*p))
603 p++;
604 if (p >= end)
605 break;
606 argv[argc] = p;
607 while (!is_space(*p) && p < end)
608 p++;
609 *p++ = '\0';
610 }
611
612 return argc;
613 }
614
615 static int
616 is_space(char c)
617 {
618 return c > '\0' && c <= ' ';
619 }
620