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