main.c revision 1.20 1 /* $NetBSD: main.c,v 1.20 2012/04/27 00:35:43 nisimura 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 /* default PATA drive configuration is "10": single master on first channel */
62 static char *drive_config = "10";
63
64 void *bootinfo; /* low memory reserved to pass bootinfo structures */
65 int bi_size; /* BOOTINFO_MAXSIZE */
66 char *bi_next;
67
68 void bi_init(void *);
69 void bi_add(void *, int, int);
70
71 struct btinfo_memory bi_mem;
72 struct btinfo_console bi_cons;
73 struct btinfo_clock bi_clk;
74 struct btinfo_prodfamily bi_fam;
75 struct btinfo_bootpath bi_path;
76 struct btinfo_rootdevice bi_rdev;
77 struct btinfo_net bi_net;
78 struct btinfo_modulelist *btinfo_modulelist;
79 size_t btinfo_modulelist_size;
80
81 struct boot_module {
82 char *bm_kmod;
83 ssize_t bm_len;
84 struct boot_module *bm_next;
85 };
86 struct boot_module *boot_modules;
87 char module_base[80];
88 uint32_t kmodloadp;
89 int modules_enabled = 0;
90
91 void module_add(char *);
92 void module_load(char *);
93 int module_open(struct boot_module *);
94
95 void main(int, char **, char *, char *);
96
97 extern char bootprog_name[], bootprog_rev[];
98 extern char newaltboot[], newaltboot_end[];
99
100 struct pcidev lata[2];
101 struct pcidev lnif[2];
102 struct pcidev lusb[3];
103 int nata, nnif, nusb;
104
105 int brdtype;
106 uint32_t busclock, cpuclock;
107
108 static int check_bootname(char *);
109 static int input_cmdline(char **, int);
110 static int parse_cmdline(char **, int, char *, char *);
111 static int is_space(char);
112 #ifdef DEBUG
113 static void sat_test(void);
114 #endif
115
116 #define BNAME_DEFAULT "wd0:"
117 #define MAX_ARGS 10
118
119 void
120 main(int argc, char *argv[], char *bootargs_start, char *bootargs_end)
121 {
122 unsigned long marks[MARK_MAX];
123 struct brdprop *brdprop;
124 char *new_argv[MAX_ARGS];
125 char *bname;
126 ssize_t len;
127 int err, fd, howto, i, n;
128
129 printf("\n>> %s altboot, revision %s\n", bootprog_name, bootprog_rev);
130
131 brdprop = brd_lookup(brdtype);
132 printf(">> %s, cpu %u MHz, bus %u MHz, %dMB SDRAM\n", brdprop->verbose,
133 cpuclock / 1000000, busclock / 1000000, bi_mem.memsize >> 20);
134
135 nata = pcilookup(PCI_CLASS_IDE, lata, 2);
136 if (nata == 0)
137 nata = pcilookup(PCI_CLASS_RAID, lata, 2);
138 if (nata == 0)
139 nata = pcilookup(PCI_CLASS_MISCSTORAGE, lata, 2);
140 if (nata == 0)
141 nata = pcilookup(PCI_CLASS_SCSI, lata, 2);
142 nnif = pcilookup(PCI_CLASS_ETH, lnif, 2);
143 nusb = pcilookup(PCI_CLASS_USB, lusb, 3);
144
145 #ifdef DEBUG
146 if (nata == 0)
147 printf("No IDE/SATA found\n");
148 else for (n = 0; n < nata; n++) {
149 int b, d, f, bdf, pvd;
150 bdf = lata[n].bdf;
151 pvd = lata[n].pvd;
152 pcidecomposetag(bdf, &b, &d, &f);
153 printf("%04x.%04x DSK %02d:%02d:%02d\n",
154 PCI_VENDOR(pvd), PCI_PRODUCT(pvd), b, d, f);
155 }
156 if (nnif == 0)
157 printf("no NET found\n");
158 else for (n = 0; n < nnif; n++) {
159 int b, d, f, bdf, pvd;
160 bdf = lnif[n].bdf;
161 pvd = lnif[n].pvd;
162 pcidecomposetag(bdf, &b, &d, &f);
163 printf("%04x.%04x NET %02d:%02d:%02d\n",
164 PCI_VENDOR(pvd), PCI_PRODUCT(pvd), b, d, f);
165 }
166 if (nusb == 0)
167 printf("no USB found\n");
168 else for (n = 0; n < nusb; n++) {
169 int b, d, f, bdf, pvd;
170 bdf = lusb[0].bdf;
171 pvd = lusb[0].pvd;
172 pcidecomposetag(bdf, &b, &d, &f);
173 printf("%04x.%04x USB %02d:%02d:%02d\n",
174 PCI_VENDOR(pvd), PCI_PRODUCT(pvd), b, d, f);
175 }
176 #endif
177
178 pcisetup();
179 pcifixup();
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:
190 * check if it contains a valid altboot command line
191 */
192 char *p = (char *)argv;
193
194 if (strncmp(p, "altboot:", 8) == 0) {
195 *p = 0;
196 for (p = p + 8; *p >= ' '; p++);
197 argc = parse_cmdline(new_argv, MAX_ARGS,
198 ((char *)argv) + 8, p);
199 argv = new_argv;
200 } else
201 argc = 0; /* boot default */
202 } else {
203 /* parse standard Linux bootargs */
204 argc = parse_cmdline(new_argv, MAX_ARGS,
205 bootargs_start, bootargs_end);
206 argv = new_argv;
207 }
208 }
209
210 /* look for a PATA drive configuration string under the arguments */
211 for (n = 1; n < argc; n++) {
212 if (strncmp(argv[n], "ide:", 4) == 0 &&
213 argv[n][4] >= '0' && argv[n][4] <= '2') {
214 drive_config = &argv[n][4];
215 break;
216 }
217 }
218
219 /* intialize a disk driver */
220 for (i = 0, n = 0; i < nata; i++)
221 n += dskdv_init(&lata[i]);
222 if (n == 0)
223 printf("IDE/SATA device driver was not found\n");
224
225 /* initialize a network interface */
226 for (n = 0; n < nnif; n++)
227 if (netif_init(&lnif[n]) != 0)
228 break;
229 if (n >= nnif)
230 printf("no NET device driver was found\n");
231
232 /* wait 2s for user to enter interactive mode */
233 for (n = 200; n >= 0; n--) {
234 if (n % 100 == 0)
235 printf("\rHit any key to enter interactive mode: %d",
236 n / 100);
237 if (tstchar()) {
238 #ifdef DEBUG
239 if (toupper(getchar()) == 'C') {
240 /* controller test terminal */
241 sat_test();
242 n = 200;
243 continue;
244 }
245 #else
246 (void)getchar();
247 #endif
248 /* enter command line */
249 argv = new_argv;
250 argc = input_cmdline(argv, MAX_ARGS);
251 break;
252 }
253 delay(10000);
254 }
255 putchar('\n');
256
257 howto = RB_AUTOBOOT; /* default is autoboot = 0 */
258
259 /* get boot options and determine bootname */
260 for (n = 1; n < argc; n++) {
261 if (strncmp(argv[n], "ide:", 4) == 0)
262 continue; /* ignore drive configuration argument */
263
264 for (i = 0; i < sizeof(bootargs) / sizeof(bootargs[0]); i++) {
265 if (strncasecmp(argv[n], bootargs[i].name,
266 strlen(bootargs[i].name)) == 0) {
267 howto |= bootargs[i].value;
268 break;
269 }
270 }
271 if (i >= sizeof(bootargs) / sizeof(bootargs[0]))
272 break; /* break on first unknown string */
273 }
274
275 /*
276 * If no device name is given, we construct a list of drives
277 * which have valid disklabels.
278 */
279 if (n >= argc) {
280 n = 0;
281 argc = 0;
282 argv = alloc(MAX_UNITS * (sizeof(char *) + sizeof("wdN:")));
283 bname = (char *)(argv + MAX_UNITS);
284 for (i = 0; i < MAX_UNITS; i++) {
285 if (!dlabel_valid(i))
286 continue;
287 sprintf(bname, "wd%d:", i);
288 argv[argc++] = bname;
289 bname += sizeof("wdN:");
290 }
291 /* use default drive if no valid disklabel is found */
292 if (argc == 0) {
293 argc = 1;
294 argv[0] = BNAME_DEFAULT;
295 }
296 }
297
298 /* try to boot off kernel from the drive list */
299 while (n < argc) {
300 bname = argv[n++];
301
302 if (check_bootname(bname) == 0) {
303 printf("%s not a valid bootname\n", bname);
304 continue;
305 }
306
307 if ((fd = open(bname, 0)) < 0) {
308 if (errno == ENOENT)
309 printf("\"%s\" not found\n", bi_path.bootpath);
310 continue;
311 }
312 printf("loading \"%s\" ", bi_path.bootpath);
313 marks[MARK_START] = 0;
314
315 if (howto == -1) {
316 /* load another altboot binary and replace ourselves */
317 len = read(fd, (void *)0x100000, 0x1000000 - 0x100000);
318 if (len == -1)
319 goto loadfail;
320 close(fd);
321 netif_shutdown_all();
322
323 memcpy((void *)0xf0000, newaltboot,
324 newaltboot_end - newaltboot);
325 __syncicache((void *)0xf0000,
326 newaltboot_end - newaltboot);
327 printf("Restarting...\n");
328 run((void *)1, argv, (void *)0x100000, (void *)len,
329 (void *)0xf0000);
330 }
331
332 err = fdloadfile(fd, marks, LOAD_KERNEL);
333 close(fd);
334 if (err < 0)
335 continue;
336
337 printf("entry=%p, ssym=%p, esym=%p\n",
338 (void *)marks[MARK_ENTRY],
339 (void *)marks[MARK_SYM],
340 (void *)marks[MARK_END]);
341
342 bootinfo = (void *)0x4000;
343 bi_init(bootinfo);
344 bi_add(&bi_cons, BTINFO_CONSOLE, sizeof(bi_cons));
345 bi_add(&bi_mem, BTINFO_MEMORY, sizeof(bi_mem));
346 bi_add(&bi_clk, BTINFO_CLOCK, sizeof(bi_clk));
347 bi_add(&bi_path, BTINFO_BOOTPATH, sizeof(bi_path));
348 bi_add(&bi_rdev, BTINFO_ROOTDEVICE, sizeof(bi_rdev));
349 bi_add(&bi_fam, BTINFO_PRODFAMILY, sizeof(bi_fam));
350 if (brdtype == BRD_SYNOLOGY || brdtype == BRD_DLINKDSM) {
351 /* need to pass this MAC address to kernel */
352 bi_add(&bi_net, BTINFO_NET, sizeof(bi_net));
353 }
354
355 if (modules_enabled) {
356 module_add(fsmod);
357 if (fsmod2 != NULL && strcmp(fsmod, fsmod2) != 0)
358 module_add(fsmod2);
359 kmodloadp = marks[MARK_END];
360 btinfo_modulelist = NULL;
361 module_load(bname);
362 if (btinfo_modulelist != NULL &&
363 btinfo_modulelist->num > 0)
364 bi_add(btinfo_modulelist, BTINFO_MODULELIST,
365 btinfo_modulelist_size);
366 }
367
368 launchfixup();
369 netif_shutdown_all();
370
371 __syncicache((void *)marks[MARK_ENTRY],
372 (u_int)marks[MARK_SYM] - (u_int)marks[MARK_ENTRY]);
373
374 run((void *)marks[MARK_SYM], (void *)marks[MARK_END],
375 (void *)howto, bootinfo, (void *)marks[MARK_ENTRY]);
376
377 /* should never come here */
378 printf("exec returned. Restarting...\n");
379 _rtt();
380 }
381 loadfail:
382 printf("load failed. Restarting...\n");
383 _rtt();
384 }
385
386 void
387 bi_init(void *addr)
388 {
389 struct btinfo_magic bi_magic;
390
391 memset(addr, 0, BOOTINFO_MAXSIZE);
392 bi_next = (char *)addr;
393 bi_size = 0;
394
395 bi_magic.magic = BOOTINFO_MAGIC;
396 bi_add(&bi_magic, BTINFO_MAGIC, sizeof(bi_magic));
397 }
398
399 void
400 bi_add(void *new, int type, int size)
401 {
402 struct btinfo_common *bi;
403
404 if (bi_size + size > BOOTINFO_MAXSIZE)
405 return; /* XXX error? */
406
407 bi = new;
408 bi->next = size;
409 bi->type = type;
410 memcpy(bi_next, new, size);
411 bi_next += size;
412 }
413
414 void
415 module_add(char *name)
416 {
417 struct boot_module *bm, *bmp;
418
419 while (*name == ' ' || *name == '\t')
420 ++name;
421
422 bm = alloc(sizeof(struct boot_module) + strlen(name) + 1);
423 if (bm == NULL) {
424 printf("couldn't allocate module %s\n", name);
425 return;
426 }
427
428 bm->bm_kmod = (char *)(bm + 1);
429 bm->bm_len = -1;
430 bm->bm_next = NULL;
431 strcpy(bm->bm_kmod, name);
432 if ((bmp = boot_modules) == NULL)
433 boot_modules = bm;
434 else {
435 while (bmp->bm_next != NULL)
436 bmp = bmp->bm_next;
437 bmp->bm_next = bm;
438 }
439 }
440
441 #define PAGE_SIZE 4096
442 #define alignpg(x) (((x)+PAGE_SIZE-1) & ~(PAGE_SIZE-1))
443
444 void
445 module_load(char *kernel_path)
446 {
447 struct boot_module *bm;
448 struct bi_modulelist_entry *bi;
449 struct stat st;
450 char *p;
451 int size, fd;
452
453 strcpy(module_base, kernel_path);
454 if ((p = strchr(module_base, ':')) == NULL)
455 return; /* eeh?! */
456 p += 1;
457 size = sizeof(module_base) - (p - module_base);
458
459 if (netbsd_version / 1000000 % 100 == 99) {
460 /* -current */
461 snprintf(p, size,
462 "/stand/sandpoint/%d.%d.%d/modules",
463 netbsd_version / 100000000,
464 netbsd_version / 1000000 % 100,
465 netbsd_version / 100 % 100);
466 }
467 else if (netbsd_version != 0) {
468 /* release */
469 snprintf(p, size,
470 "/stand/sandpoint/%d.%d/modules",
471 netbsd_version / 100000000,
472 netbsd_version / 1000000 % 100);
473 }
474
475 /*
476 * 1st pass; determine module existence
477 */
478 size = 0;
479 for (bm = boot_modules; bm != NULL; bm = bm->bm_next) {
480 fd = module_open(bm);
481 if (fd == -1)
482 continue;
483 if (fstat(fd, &st) == -1 || st.st_size == -1) {
484 printf("WARNING: couldn't stat %s\n", bm->bm_kmod);
485 close(fd);
486 continue;
487 }
488 bm->bm_len = (int)st.st_size;
489 close(fd);
490 size += sizeof(struct bi_modulelist_entry);
491 }
492 if (size == 0)
493 return;
494
495 size += sizeof(struct btinfo_modulelist);
496 btinfo_modulelist = alloc(size);
497 if (btinfo_modulelist == NULL) {
498 printf("WARNING: couldn't allocate module list\n");
499 return;
500 }
501 btinfo_modulelist_size = size;
502 btinfo_modulelist->num = 0;
503
504 /*
505 * 2nd pass; load modules into memory
506 */
507 kmodloadp = alignpg(kmodloadp);
508 bi = (struct bi_modulelist_entry *)(btinfo_modulelist + 1);
509 for (bm = boot_modules; bm != NULL; bm = bm->bm_next) {
510 if (bm->bm_len == -1)
511 continue; /* already found unavailable */
512 fd = module_open(bm);
513 printf("module \"%s\" ", bm->bm_kmod);
514 size = read(fd, (char *)kmodloadp, SSIZE_MAX);
515 if (size < bm->bm_len)
516 printf("WARNING: couldn't load");
517 else {
518 snprintf(bi->kmod, sizeof(bi->kmod), bm->bm_kmod);
519 bi->type = BI_MODULE_ELF;
520 bi->len = size;
521 bi->base = kmodloadp;
522 btinfo_modulelist->num += 1;
523 printf("loaded at 0x%08x size 0x%x", kmodloadp, size);
524 kmodloadp += alignpg(size);
525 bi += 1;
526 }
527 printf("\n");
528 close(fd);
529 }
530 btinfo_modulelist->endpa = kmodloadp;
531 }
532
533 int
534 module_open(struct boot_module *bm)
535 {
536 char path[80];
537 int fd;
538
539 snprintf(path, sizeof(path),
540 "%s/%s/%s.kmod", module_base, bm->bm_kmod, bm->bm_kmod);
541 fd = open(path, 0);
542 return fd;
543 }
544
545 /*
546 * Return the drive configuration for the requested channel 'ch'.
547 * Channel 2 is the first channel of the next IDE controller.
548 * 0: for no drive present on channel
549 * 1: for master drive present on channel, no slave
550 * 2: for master and slave drive present
551 */
552 int
553 get_drive_config(int ch)
554 {
555 if (drive_config != NULL) {
556 if (strlen(drive_config) <= ch)
557 return 0; /* an unspecified channel is unused */
558 if (drive_config[ch] >= '0' && drive_config[ch] <= '2')
559 return drive_config[ch] - '0';
560 }
561 return -1;
562 }
563
564 void *
565 allocaligned(size_t size, size_t align)
566 {
567 uint32_t p;
568
569 if (align-- < 2)
570 return alloc(size);
571 p = (uint32_t)alloc(size + align);
572 return (void *)((p + align) & ~align);
573 }
574
575 static int hex2nibble(char c)
576 {
577
578 if (c >= 'a')
579 c &= ~0x20;
580 if (c >= 'A' && c <= 'F')
581 c -= 'A' - ('9' + 1);
582 else if (c < '0' || c > '9')
583 return -1;
584
585 return c - '0';
586 }
587
588 uint32_t
589 read_hex(const char *s)
590 {
591 int n;
592 uint32_t val;
593
594 val = 0;
595 while ((n = hex2nibble(*s++)) >= 0)
596 val = (val << 4) | n;
597 return val;
598 }
599
600 static int
601 check_bootname(char *s)
602 {
603 /*
604 * nfs:
605 * nfs:<bootfile>
606 * tftp:
607 * tftp:<bootfile>
608 * wd[N[P]]:<bootfile>
609 * mem:<address>
610 *
611 * net is a synonym of nfs.
612 */
613 if (strncmp(s, "nfs:", 4) == 0 || strncmp(s, "net:", 4) == 0 ||
614 strncmp(s, "tftp:", 5) == 0 || strncmp(s, "mem:", 4) == 0)
615 return 1;
616 if (s[0] == 'w' && s[1] == 'd') {
617 s += 2;
618 if (*s != ':' && *s >= '0' && *s <= '3') {
619 ++s;
620 if (*s != ':' && *s >= 'a' && *s <= 'p')
621 ++s;
622 }
623 return *s == ':';
624 }
625 return 0;
626 }
627
628 static int input_cmdline(char **argv, int maxargc)
629 {
630 char *cmdline;
631
632 printf("\nbootargs> ");
633 cmdline = alloc(256);
634 gets(cmdline);
635
636 return parse_cmdline(argv, maxargc, cmdline,
637 cmdline + strlen(cmdline));
638 }
639
640 static int
641 parse_cmdline(char **argv, int maxargc, char *p, char *end)
642 {
643 int argc;
644
645 argv[0] = "";
646 for (argc = 1; argc < maxargc && p < end; argc++) {
647 while (is_space(*p))
648 p++;
649 if (p >= end)
650 break;
651 argv[argc] = p;
652 while (!is_space(*p) && p < end)
653 p++;
654 *p++ = '\0';
655 }
656
657 return argc;
658 }
659
660 static int
661 is_space(char c)
662 {
663
664 return c > '\0' && c <= ' ';
665 }
666
667 #ifdef DEBUG
668 static void
669 sat_test(void)
670 {
671 char buf[1024];
672 int i, j, n, pos;
673 unsigned char c;
674
675 putchar('\n');
676 for (;;) {
677 do {
678 for (pos = 0; pos < 1024 && sat_tstch() != 0; pos++)
679 buf[pos] = sat_getch();
680 if (pos > 1023)
681 break;
682 delay(100000);
683 } while (sat_tstch());
684
685 for (i = 0; i < pos; i += 16) {
686 if ((n = i + 16) > pos)
687 n = pos;
688 for (j = 0; j < n; j++)
689 printf("%02x ", (unsigned)buf[i + j]);
690 for (; j < 16; j++)
691 printf(" ");
692 putchar('\"');
693 for (j = 0; j < n; j++) {
694 c = buf[i + j];
695 putchar((c >= 0x20 && c <= 0x7e) ? c : '.');
696 }
697 printf("\"\n");
698 }
699
700 printf("controller> ");
701 gets(buf);
702 if (buf[0] == '*' && buf[1] == 'X')
703 break;
704
705 if (buf[0] == '0' && tolower((unsigned)buf[1]) == 'x') {
706 for (i = 2, n = 0, c = 0; buf[i]; i++) {
707 c <<= 4;
708 c |= hex2nibble(buf[i]);
709 if (i & 1)
710 buf[n++] = c;
711 }
712 } else
713 n = strlen(buf);
714
715 if (n > 0)
716 sat_write(buf, n);
717 }
718 }
719 #endif
720