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