main.c revision 1.19 1 /* $NetBSD: main.c,v 1.19 2012/04/26 19:59:37 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 /* 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 if (n >= argc) {
276 /*
277 * If no device name is given we construct a list of drives
278 * which have valid disklabels.
279 */
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 while (n < argc) {
299 bname = argv[n++];
300
301 if (check_bootname(bname) == 0) {
302 printf("%s not a valid bootname\n", bname);
303 continue;
304 }
305
306 if ((fd = open(bname, 0)) < 0) {
307 if (errno == ENOENT)
308 printf("\"%s\" not found\n", bi_path.bootpath);
309 continue;
310 }
311 printf("loading \"%s\" ", bi_path.bootpath);
312 marks[MARK_START] = 0;
313
314 if (howto == -1) {
315 /* load another altboot binary and replace ourselves */
316 len = read(fd, (void *)0x100000, 0x1000000 - 0x100000);
317 if (len == -1)
318 goto loadfail;
319 close(fd);
320 netif_shutdown_all();
321
322 memcpy((void *)0xf0000, newaltboot,
323 newaltboot_end - newaltboot);
324 __syncicache((void *)0xf0000,
325 newaltboot_end - newaltboot);
326 printf("Restarting...\n");
327 run((void *)1, argv, (void *)0x100000, (void *)len,
328 (void *)0xf0000);
329 }
330
331 err = fdloadfile(fd, marks, LOAD_KERNEL);
332 close(fd);
333 if (err < 0)
334 continue;
335
336 printf("entry=%p, ssym=%p, esym=%p\n",
337 (void *)marks[MARK_ENTRY],
338 (void *)marks[MARK_SYM],
339 (void *)marks[MARK_END]);
340
341 bootinfo = (void *)0x4000;
342 bi_init(bootinfo);
343 bi_add(&bi_cons, BTINFO_CONSOLE, sizeof(bi_cons));
344 bi_add(&bi_mem, BTINFO_MEMORY, sizeof(bi_mem));
345 bi_add(&bi_clk, BTINFO_CLOCK, sizeof(bi_clk));
346 bi_add(&bi_path, BTINFO_BOOTPATH, sizeof(bi_path));
347 bi_add(&bi_rdev, BTINFO_ROOTDEVICE, sizeof(bi_rdev));
348 bi_add(&bi_fam, BTINFO_PRODFAMILY, sizeof(bi_fam));
349 if (brdtype == BRD_SYNOLOGY || brdtype == BRD_DLINKDSM) {
350 /* need to pass this MAC address to kernel */
351 bi_add(&bi_net, BTINFO_NET, sizeof(bi_net));
352 }
353
354 if (modules_enabled) {
355 module_add(fsmod);
356 if (fsmod2 != NULL && strcmp(fsmod, fsmod2) != 0)
357 module_add(fsmod2);
358 kmodloadp = marks[MARK_END];
359 btinfo_modulelist = NULL;
360 module_load(bname);
361 if (btinfo_modulelist != NULL &&
362 btinfo_modulelist->num > 0)
363 bi_add(btinfo_modulelist, BTINFO_MODULELIST,
364 btinfo_modulelist_size);
365 }
366
367 launchfixup();
368 netif_shutdown_all();
369
370 __syncicache((void *)marks[MARK_ENTRY],
371 (u_int)marks[MARK_SYM] - (u_int)marks[MARK_ENTRY]);
372
373 run((void *)marks[MARK_SYM], (void *)marks[MARK_END],
374 (void *)howto, bootinfo, (void *)marks[MARK_ENTRY]);
375
376 /* should never come here */
377 printf("exec returned. Restarting...\n");
378 _rtt();
379 }
380 loadfail:
381 printf("load failed. Restarting...\n");
382 _rtt();
383 }
384
385 void
386 bi_init(void *addr)
387 {
388 struct btinfo_magic bi_magic;
389
390 memset(addr, 0, BOOTINFO_MAXSIZE);
391 bi_next = (char *)addr;
392 bi_size = 0;
393
394 bi_magic.magic = BOOTINFO_MAGIC;
395 bi_add(&bi_magic, BTINFO_MAGIC, sizeof(bi_magic));
396 }
397
398 void
399 bi_add(void *new, int type, int size)
400 {
401 struct btinfo_common *bi;
402
403 if (bi_size + size > BOOTINFO_MAXSIZE)
404 return; /* XXX error? */
405
406 bi = new;
407 bi->next = size;
408 bi->type = type;
409 memcpy(bi_next, new, size);
410 bi_next += size;
411 }
412
413 void
414 module_add(char *name)
415 {
416 struct boot_module *bm, *bmp;
417
418 while (*name == ' ' || *name == '\t')
419 ++name;
420
421 bm = alloc(sizeof(struct boot_module) + strlen(name) + 1);
422 if (bm == NULL) {
423 printf("couldn't allocate module %s\n", name);
424 return;
425 }
426
427 bm->bm_kmod = (char *)(bm + 1);
428 bm->bm_len = -1;
429 bm->bm_next = NULL;
430 strcpy(bm->bm_kmod, name);
431 if ((bmp = boot_modules) == NULL)
432 boot_modules = bm;
433 else {
434 while (bmp->bm_next != NULL)
435 bmp = bmp->bm_next;
436 bmp->bm_next = bm;
437 }
438 }
439
440 #define PAGE_SIZE 4096
441 #define alignpg(x) (((x)+PAGE_SIZE-1) & ~(PAGE_SIZE-1))
442
443 void
444 module_load(char *kernel_path)
445 {
446 struct boot_module *bm;
447 struct bi_modulelist_entry *bi;
448 struct stat st;
449 char *p;
450 int size, fd;
451
452 strcpy(module_base, kernel_path);
453 if ((p = strchr(module_base, ':')) == NULL)
454 return; /* eeh?! */
455 p += 1;
456 size = sizeof(module_base) - (p - module_base);
457
458 if (netbsd_version / 1000000 % 100 == 99) {
459 /* -current */
460 snprintf(p, size,
461 "/stand/sandpoint/%d.%d.%d/modules",
462 netbsd_version / 100000000,
463 netbsd_version / 1000000 % 100,
464 netbsd_version / 100 % 100);
465 }
466 else if (netbsd_version != 0) {
467 /* release */
468 snprintf(p, size,
469 "/stand/sandpoint/%d.%d/modules",
470 netbsd_version / 100000000,
471 netbsd_version / 1000000 % 100);
472 }
473
474 /*
475 * 1st pass; determine module existence
476 */
477 size = 0;
478 for (bm = boot_modules; bm != NULL; bm = bm->bm_next) {
479 fd = module_open(bm);
480 if (fd == -1)
481 continue;
482 if (fstat(fd, &st) == -1 || st.st_size == -1) {
483 printf("WARNING: couldn't stat %s\n", bm->bm_kmod);
484 close(fd);
485 continue;
486 }
487 bm->bm_len = (int)st.st_size;
488 close(fd);
489 size += sizeof(struct bi_modulelist_entry);
490 }
491 if (size == 0)
492 return;
493
494 size += sizeof(struct btinfo_modulelist);
495 btinfo_modulelist = alloc(size);
496 if (btinfo_modulelist == NULL) {
497 printf("WARNING: couldn't allocate module list\n");
498 return;
499 }
500 btinfo_modulelist_size = size;
501 btinfo_modulelist->num = 0;
502
503 /*
504 * 2nd pass; load modules into memory
505 */
506 kmodloadp = alignpg(kmodloadp);
507 bi = (struct bi_modulelist_entry *)(btinfo_modulelist + 1);
508 for (bm = boot_modules; bm != NULL; bm = bm->bm_next) {
509 if (bm->bm_len == -1)
510 continue; /* already found unavailable */
511 fd = module_open(bm);
512 printf("module \"%s\" ", bm->bm_kmod);
513 size = read(fd, (char *)kmodloadp, SSIZE_MAX);
514 if (size < bm->bm_len)
515 printf("WARNING: couldn't load");
516 else {
517 snprintf(bi->kmod, sizeof(bi->kmod), bm->bm_kmod);
518 bi->type = BI_MODULE_ELF;
519 bi->len = size;
520 bi->base = kmodloadp;
521 btinfo_modulelist->num += 1;
522 printf("loaded at 0x%08x size 0x%x", kmodloadp, size);
523 kmodloadp += alignpg(size);
524 bi += 1;
525 }
526 printf("\n");
527 close(fd);
528 }
529 btinfo_modulelist->endpa = kmodloadp;
530 }
531
532 int
533 module_open(struct boot_module *bm)
534 {
535 char path[80];
536 int fd;
537
538 snprintf(path, sizeof(path),
539 "%s/%s/%s.kmod", module_base, bm->bm_kmod, bm->bm_kmod);
540 fd = open(path, 0);
541 return fd;
542 }
543
544 /*
545 * Return the drive configuration for the requested channel 'ch'.
546 * Channel 2 is the first channel of the next IDE controller.
547 * 0: for no drive present on channel
548 * 1: for master drive present on channel, no slave
549 * 2: for master and slave drive present
550 */
551 int
552 get_drive_config(int ch)
553 {
554 if (drive_config != NULL) {
555 if (strlen(drive_config) <= ch)
556 return 0; /* an unspecified channel is unused */
557 if (drive_config[ch] >= '0' && drive_config[ch] <= '2')
558 return drive_config[ch] - '0';
559 }
560 return -1;
561 }
562
563 void *
564 allocaligned(size_t size, size_t align)
565 {
566 uint32_t p;
567
568 if (align-- < 2)
569 return alloc(size);
570 p = (uint32_t)alloc(size + align);
571 return (void *)((p + align) & ~align);
572 }
573
574 static int hex2nibble(char c)
575 {
576
577 if (c >= 'a')
578 c &= ~0x20;
579 if (c >= 'A' && c <= 'F')
580 c -= 'A' - ('9' + 1);
581 else if (c < '0' || c > '9')
582 return -1;
583
584 return c - '0';
585 }
586
587 uint32_t
588 read_hex(const char *s)
589 {
590 int n;
591 uint32_t val;
592
593 val = 0;
594 while ((n = hex2nibble(*s++)) >= 0)
595 val = (val << 4) | n;
596 return val;
597 }
598
599 static int
600 check_bootname(char *s)
601 {
602 /*
603 * nfs:
604 * nfs:<bootfile>
605 * tftp:
606 * tftp:<bootfile>
607 * wd[N[P]]:<bootfile>
608 * mem:<address>
609 *
610 * net is a synonym of nfs.
611 */
612 if (strncmp(s, "nfs:", 4) == 0 || strncmp(s, "net:", 4) == 0 ||
613 strncmp(s, "tftp:", 5) == 0 || strncmp(s, "mem:", 4) == 0)
614 return 1;
615 if (s[0] == 'w' && s[1] == 'd') {
616 s += 2;
617 if (*s != ':' && *s >= '0' && *s <= '3') {
618 ++s;
619 if (*s != ':' && *s >= 'a' && *s <= 'p')
620 ++s;
621 }
622 return *s == ':';
623 }
624 return 0;
625 }
626
627 static int input_cmdline(char **argv, int maxargc)
628 {
629 char *cmdline;
630
631 printf("\nbootargs> ");
632 cmdline = alloc(256);
633 gets(cmdline);
634
635 return parse_cmdline(argv, maxargc, cmdline,
636 cmdline + strlen(cmdline));
637 }
638
639 static int
640 parse_cmdline(char **argv, int maxargc, char *p, char *end)
641 {
642 int argc;
643
644 argv[0] = "";
645 for (argc = 1; argc < maxargc && p < end; argc++) {
646 while (is_space(*p))
647 p++;
648 if (p >= end)
649 break;
650 argv[argc] = p;
651 while (!is_space(*p) && p < end)
652 p++;
653 *p++ = '\0';
654 }
655
656 return argc;
657 }
658
659 static int
660 is_space(char c)
661 {
662
663 return c > '\0' && c <= ' ';
664 }
665
666 #ifdef DEBUG
667 static void
668 sat_test(void)
669 {
670 char buf[1024];
671 int i, j, n, pos;
672 unsigned char c;
673
674 putchar('\n');
675 for (;;) {
676 do {
677 for (pos = 0; pos < 1024 && sat_tstch() != 0; pos++)
678 buf[pos] = sat_getch();
679 if (pos > 1023)
680 break;
681 delay(100000);
682 } while (sat_tstch());
683
684 for (i = 0; i < pos; i += 16) {
685 if ((n = i + 16) > pos)
686 n = pos;
687 for (j = 0; j < n; j++)
688 printf("%02x ", (unsigned)buf[i + j]);
689 for (; j < 16; j++)
690 printf(" ");
691 putchar('\"');
692 for (j = 0; j < n; j++) {
693 c = buf[i + j];
694 putchar((c >= 0x20 && c <= 0x7e) ? c : '.');
695 }
696 printf("\"\n");
697 }
698
699 printf("controller> ");
700 gets(buf);
701 if (buf[0] == '*' && buf[1] == 'X')
702 break;
703
704 if (buf[0] == '0' && tolower((unsigned)buf[1]) == 'x') {
705 for (i = 2, n = 0, c = 0; buf[i]; i++) {
706 c <<= 4;
707 c |= hex2nibble(buf[i]);
708 if (i & 1)
709 buf[n++] = c;
710 }
711 } else
712 n = strlen(buf);
713
714 if (n > 0)
715 sat_write(buf, n);
716 }
717 }
718 #endif
719