main.c revision 1.7.2.2 1 1.7.2.2 rmind /* $NetBSD: main.c,v 1.7.2.2 2011/03/05 20:51:47 rmind Exp $ */
2 1.7.2.2 rmind
3 1.7.2.2 rmind /*-
4 1.7.2.2 rmind * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 1.7.2.2 rmind * All rights reserved.
6 1.7.2.2 rmind *
7 1.7.2.2 rmind * This code is derived from software contributed to The NetBSD Foundation
8 1.7.2.2 rmind * by Tohru Nishimura.
9 1.7.2.2 rmind *
10 1.7.2.2 rmind * Redistribution and use in source and binary forms, with or without
11 1.7.2.2 rmind * modification, are permitted provided that the following conditions
12 1.7.2.2 rmind * are met:
13 1.7.2.2 rmind * 1. Redistributions of source code must retain the above copyright
14 1.7.2.2 rmind * notice, this list of conditions and the following disclaimer.
15 1.7.2.2 rmind * 2. Redistributions in binary form must reproduce the above copyright
16 1.7.2.2 rmind * notice, this list of conditions and the following disclaimer in the
17 1.7.2.2 rmind * documentation and/or other materials provided with the distribution.
18 1.7.2.2 rmind *
19 1.7.2.2 rmind * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.7.2.2 rmind * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.7.2.2 rmind * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.7.2.2 rmind * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.7.2.2 rmind * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.7.2.2 rmind * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.7.2.2 rmind * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.7.2.2 rmind * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.7.2.2 rmind * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.7.2.2 rmind * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.7.2.2 rmind * POSSIBILITY OF SUCH DAMAGE.
30 1.7.2.2 rmind */
31 1.7.2.2 rmind
32 1.7.2.2 rmind #include <sys/param.h>
33 1.7.2.2 rmind #include <sys/reboot.h>
34 1.7.2.2 rmind
35 1.7.2.2 rmind #include <lib/libsa/stand.h>
36 1.7.2.2 rmind #include <lib/libsa/loadfile.h>
37 1.7.2.2 rmind #include <lib/libkern/libkern.h>
38 1.7.2.2 rmind
39 1.7.2.2 rmind #include <machine/bootinfo.h>
40 1.7.2.2 rmind
41 1.7.2.2 rmind #include "globals.h"
42 1.7.2.2 rmind
43 1.7.2.2 rmind static const struct bootarg {
44 1.7.2.2 rmind const char *name;
45 1.7.2.2 rmind int value;
46 1.7.2.2 rmind } bootargs[] = {
47 1.7.2.2 rmind { "multi", RB_AUTOBOOT },
48 1.7.2.2 rmind { "auto", RB_AUTOBOOT },
49 1.7.2.2 rmind { "ask", RB_ASKNAME },
50 1.7.2.2 rmind { "single", RB_SINGLE },
51 1.7.2.2 rmind { "ddb", RB_KDB },
52 1.7.2.2 rmind { "userconf", RB_USERCONF },
53 1.7.2.2 rmind { "norm", AB_NORMAL },
54 1.7.2.2 rmind { "quiet", AB_QUIET },
55 1.7.2.2 rmind { "verb", AB_VERBOSE },
56 1.7.2.2 rmind { "silent", AB_SILENT },
57 1.7.2.2 rmind { "debug", AB_DEBUG }
58 1.7.2.2 rmind };
59 1.7.2.2 rmind
60 1.7.2.2 rmind void *bootinfo; /* low memory reserved to pass bootinfo structures */
61 1.7.2.2 rmind int bi_size; /* BOOTINFO_MAXSIZE */
62 1.7.2.2 rmind char *bi_next;
63 1.7.2.2 rmind
64 1.7.2.2 rmind void bi_init(void *);
65 1.7.2.2 rmind void bi_add(void *, int, int);
66 1.7.2.2 rmind
67 1.7.2.2 rmind struct btinfo_memory bi_mem;
68 1.7.2.2 rmind struct btinfo_console bi_cons;
69 1.7.2.2 rmind struct btinfo_clock bi_clk;
70 1.7.2.2 rmind struct btinfo_prodfamily bi_fam;
71 1.7.2.2 rmind struct btinfo_bootpath bi_path;
72 1.7.2.2 rmind struct btinfo_rootdevice bi_rdev;
73 1.7.2.2 rmind struct btinfo_net bi_net;
74 1.7.2.2 rmind struct btinfo_modulelist *btinfo_modulelist;
75 1.7.2.2 rmind size_t btinfo_modulelist_size;
76 1.7.2.2 rmind
77 1.7.2.2 rmind struct boot_module {
78 1.7.2.2 rmind char *bm_kmod;
79 1.7.2.2 rmind ssize_t bm_len;
80 1.7.2.2 rmind struct boot_module *bm_next;
81 1.7.2.2 rmind };
82 1.7.2.2 rmind struct boot_module *boot_modules;
83 1.7.2.2 rmind char module_base[80];
84 1.7.2.2 rmind uint32_t kmodloadp;
85 1.7.2.2 rmind int modules_enabled = 0;
86 1.7.2.2 rmind
87 1.7.2.2 rmind void module_add(char *);
88 1.7.2.2 rmind void module_load(char *);
89 1.7.2.2 rmind int module_open(struct boot_module *);
90 1.7.2.2 rmind
91 1.7.2.2 rmind void main(int, char **, char *, char *);
92 1.7.2.2 rmind extern char bootprog_name[], bootprog_rev[];
93 1.7.2.2 rmind
94 1.7.2.2 rmind struct pcidev lata[2];
95 1.7.2.2 rmind struct pcidev lnif[1];
96 1.7.2.2 rmind struct pcidev lusb[3];
97 1.7.2.2 rmind int nata, nnif, nusb;
98 1.7.2.2 rmind
99 1.7.2.2 rmind int brdtype;
100 1.7.2.2 rmind uint32_t busclock, cpuclock;
101 1.7.2.2 rmind
102 1.7.2.2 rmind static int check_bootname(char *);
103 1.7.2.2 rmind static int parse_cmdline(char **, int, char *, char *);
104 1.7.2.2 rmind static int is_space(char);
105 1.7.2.2 rmind
106 1.7.2.2 rmind #define BNAME_DEFAULT "nfs:"
107 1.7.2.2 rmind #define MAX_ARGS 10
108 1.7.2.2 rmind
109 1.7.2.2 rmind void
110 1.7.2.2 rmind main(int argc, char *argv[], char *bootargs_start, char *bootargs_end)
111 1.7.2.2 rmind {
112 1.7.2.2 rmind struct brdprop *brdprop;
113 1.7.2.2 rmind unsigned long marks[MARK_MAX];
114 1.7.2.2 rmind char *new_argv[MAX_ARGS];
115 1.7.2.2 rmind int n, i, fd, howto;
116 1.7.2.2 rmind char *bname;
117 1.7.2.2 rmind
118 1.7.2.2 rmind printf("\n");
119 1.7.2.2 rmind printf(">> %s altboot, revision %s\n", bootprog_name, bootprog_rev);
120 1.7.2.2 rmind
121 1.7.2.2 rmind brdprop = brd_lookup(brdtype);
122 1.7.2.2 rmind printf(">> %s, cpu %u MHz, bus %u MHz, %dMB SDRAM\n", brdprop->verbose,
123 1.7.2.2 rmind cpuclock / 1000000, busclock / 1000000, bi_mem.memsize >> 20);
124 1.7.2.2 rmind
125 1.7.2.2 rmind nata = pcilookup(PCI_CLASS_IDE, lata, 2);
126 1.7.2.2 rmind if (nata == 0)
127 1.7.2.2 rmind nata = pcilookup(PCI_CLASS_MISCSTORAGE, lata, 2);
128 1.7.2.2 rmind nnif = pcilookup(PCI_CLASS_ETH, lnif, 1);
129 1.7.2.2 rmind nusb = pcilookup(PCI_CLASS_USB, lusb, 3);
130 1.7.2.2 rmind
131 1.7.2.2 rmind #ifdef DEBUG
132 1.7.2.2 rmind if (nata == 0)
133 1.7.2.2 rmind printf("No IDE/SATA found\n");
134 1.7.2.2 rmind else for (n = 0; n < nata; n++) {
135 1.7.2.2 rmind int b, d, f, bdf, pvd;
136 1.7.2.2 rmind bdf = lata[n].bdf;
137 1.7.2.2 rmind pvd = lata[n].pvd;
138 1.7.2.2 rmind pcidecomposetag(bdf, &b, &d, &f);
139 1.7.2.2 rmind printf("%04x.%04x DSK %02d:%02d:%02d\n",
140 1.7.2.2 rmind PCI_VENDOR(pvd), PCI_PRODUCT(pvd), b, d, f);
141 1.7.2.2 rmind }
142 1.7.2.2 rmind if (nnif == 0)
143 1.7.2.2 rmind printf("no NET found\n");
144 1.7.2.2 rmind else {
145 1.7.2.2 rmind int b, d, f, bdf, pvd;
146 1.7.2.2 rmind bdf = lnif[0].bdf;
147 1.7.2.2 rmind pvd = lnif[0].pvd;
148 1.7.2.2 rmind pcidecomposetag(bdf, &b, &d, &f);
149 1.7.2.2 rmind printf("%04x.%04x NET %02d:%02d:%02d\n",
150 1.7.2.2 rmind PCI_VENDOR(pvd), PCI_PRODUCT(pvd), b, d, f);
151 1.7.2.2 rmind }
152 1.7.2.2 rmind if (nusb == 0)
153 1.7.2.2 rmind printf("no USB found\n");
154 1.7.2.2 rmind else for (n = 0; n < nusb; n++) {
155 1.7.2.2 rmind int b, d, f, bdf, pvd;
156 1.7.2.2 rmind bdf = lusb[0].bdf;
157 1.7.2.2 rmind pvd = lusb[0].pvd;
158 1.7.2.2 rmind pcidecomposetag(bdf, &b, &d, &f);
159 1.7.2.2 rmind printf("%04x.%04x USB %02d:%02d:%02d\n",
160 1.7.2.2 rmind PCI_VENDOR(pvd), PCI_PRODUCT(pvd), b, d, f);
161 1.7.2.2 rmind }
162 1.7.2.2 rmind #endif
163 1.7.2.2 rmind
164 1.7.2.2 rmind pcisetup();
165 1.7.2.2 rmind pcifixup();
166 1.7.2.2 rmind
167 1.7.2.2 rmind if (dskdv_init(&lata[0]) == 0
168 1.7.2.2 rmind || (nata == 2 && dskdv_init(&lata[1]) == 0))
169 1.7.2.2 rmind printf("IDE/SATA device driver was not found\n");
170 1.7.2.2 rmind
171 1.7.2.2 rmind if (netif_init(&lnif[0]) == 0)
172 1.7.2.2 rmind printf("no NET device driver was found\n");
173 1.7.2.2 rmind
174 1.7.2.2 rmind /*
175 1.7.2.2 rmind * When argc is too big then it is probably a pointer, which could
176 1.7.2.2 rmind * indicate that we were launched as a Linux kernel module using
177 1.7.2.2 rmind * "bootm".
178 1.7.2.2 rmind */
179 1.7.2.2 rmind if (argc > MAX_ARGS) {
180 1.7.2.2 rmind /* parse Linux bootargs */
181 1.7.2.2 rmind argv = new_argv;
182 1.7.2.2 rmind argc = parse_cmdline(argv, MAX_ARGS, bootargs_start,
183 1.7.2.2 rmind bootargs_end);
184 1.7.2.2 rmind }
185 1.7.2.2 rmind
186 1.7.2.2 rmind howto = RB_AUTOBOOT; /* default is autoboot = 0 */
187 1.7.2.2 rmind
188 1.7.2.2 rmind /* get boot options and determine bootname */
189 1.7.2.2 rmind for (n = 1; n < argc; n++) {
190 1.7.2.2 rmind for (i = 0; i < sizeof(bootargs) / sizeof(bootargs[0]); i++) {
191 1.7.2.2 rmind if (strncasecmp(argv[n], bootargs[i].name,
192 1.7.2.2 rmind strlen(bootargs[i].name)) == 0) {
193 1.7.2.2 rmind howto |= bootargs[i].value;
194 1.7.2.2 rmind break;
195 1.7.2.2 rmind }
196 1.7.2.2 rmind }
197 1.7.2.2 rmind if (i >= sizeof(bootargs) / sizeof(bootargs[0]))
198 1.7.2.2 rmind break; /* break on first unknown string */
199 1.7.2.2 rmind }
200 1.7.2.2 rmind if (n >= argc)
201 1.7.2.2 rmind bname = BNAME_DEFAULT;
202 1.7.2.2 rmind else {
203 1.7.2.2 rmind bname = argv[n];
204 1.7.2.2 rmind if (check_bootname(bname) == 0) {
205 1.7.2.2 rmind printf("%s not a valid bootname\n", bname);
206 1.7.2.2 rmind goto loadfail;
207 1.7.2.2 rmind }
208 1.7.2.2 rmind }
209 1.7.2.2 rmind
210 1.7.2.2 rmind if ((fd = open(bname, 0)) < 0) {
211 1.7.2.2 rmind if (errno == ENOENT)
212 1.7.2.2 rmind printf("\"%s\" not found\n", bi_path.bootpath);
213 1.7.2.2 rmind goto loadfail;
214 1.7.2.2 rmind }
215 1.7.2.2 rmind printf("loading \"%s\" ", bi_path.bootpath);
216 1.7.2.2 rmind marks[MARK_START] = 0;
217 1.7.2.2 rmind if (fdloadfile(fd, marks, LOAD_KERNEL) < 0)
218 1.7.2.2 rmind goto loadfail;
219 1.7.2.2 rmind close(fd);
220 1.7.2.2 rmind
221 1.7.2.2 rmind printf("entry=%p, ssym=%p, esym=%p\n",
222 1.7.2.2 rmind (void *)marks[MARK_ENTRY],
223 1.7.2.2 rmind (void *)marks[MARK_SYM],
224 1.7.2.2 rmind (void *)marks[MARK_END]);
225 1.7.2.2 rmind
226 1.7.2.2 rmind bootinfo = (void *)0x4000;
227 1.7.2.2 rmind bi_init(bootinfo);
228 1.7.2.2 rmind bi_add(&bi_cons, BTINFO_CONSOLE, sizeof(bi_cons));
229 1.7.2.2 rmind bi_add(&bi_mem, BTINFO_MEMORY, sizeof(bi_mem));
230 1.7.2.2 rmind bi_add(&bi_clk, BTINFO_CLOCK, sizeof(bi_clk));
231 1.7.2.2 rmind bi_add(&bi_path, BTINFO_BOOTPATH, sizeof(bi_path));
232 1.7.2.2 rmind bi_add(&bi_rdev, BTINFO_ROOTDEVICE, sizeof(bi_rdev));
233 1.7.2.2 rmind bi_add(&bi_fam, BTINFO_PRODFAMILY, sizeof(bi_fam));
234 1.7.2.2 rmind if (brdtype == BRD_SYNOLOGY) {
235 1.7.2.2 rmind /* need to set MAC address for Marvell-SKnet */
236 1.7.2.2 rmind bi_add(&bi_net, BTINFO_NET, sizeof(bi_net));
237 1.7.2.2 rmind }
238 1.7.2.2 rmind
239 1.7.2.2 rmind if (modules_enabled) {
240 1.7.2.2 rmind module_add(fsmod);
241 1.7.2.2 rmind if (fsmod2 != NULL && strcmp(fsmod, fsmod2) != 0)
242 1.7.2.2 rmind module_add(fsmod2);
243 1.7.2.2 rmind kmodloadp = marks[MARK_END];
244 1.7.2.2 rmind btinfo_modulelist = NULL;
245 1.7.2.2 rmind module_load(bname);
246 1.7.2.2 rmind if (btinfo_modulelist != NULL && btinfo_modulelist->num > 0)
247 1.7.2.2 rmind bi_add(btinfo_modulelist, BTINFO_MODULELIST,
248 1.7.2.2 rmind btinfo_modulelist_size);
249 1.7.2.2 rmind }
250 1.7.2.2 rmind
251 1.7.2.2 rmind __syncicache((void *)marks[MARK_ENTRY],
252 1.7.2.2 rmind (u_int)marks[MARK_SYM] - (u_int)marks[MARK_ENTRY]);
253 1.7.2.2 rmind
254 1.7.2.2 rmind run((void *)marks[MARK_SYM], (void *)marks[MARK_END],
255 1.7.2.2 rmind (void *)howto, bootinfo, (void *)marks[MARK_ENTRY]);
256 1.7.2.2 rmind
257 1.7.2.2 rmind /* should never come here */
258 1.7.2.2 rmind printf("exec returned. Restarting...\n");
259 1.7.2.2 rmind _rtt();
260 1.7.2.2 rmind
261 1.7.2.2 rmind loadfail:
262 1.7.2.2 rmind printf("load failed. Restarting...\n");
263 1.7.2.2 rmind _rtt();
264 1.7.2.2 rmind }
265 1.7.2.2 rmind
266 1.7.2.2 rmind void
267 1.7.2.2 rmind bi_init(void *addr)
268 1.7.2.2 rmind {
269 1.7.2.2 rmind struct btinfo_magic bi_magic;
270 1.7.2.2 rmind
271 1.7.2.2 rmind memset(addr, 0, BOOTINFO_MAXSIZE);
272 1.7.2.2 rmind bi_next = (char *)addr;
273 1.7.2.2 rmind bi_size = 0;
274 1.7.2.2 rmind
275 1.7.2.2 rmind bi_magic.magic = BOOTINFO_MAGIC;
276 1.7.2.2 rmind bi_add(&bi_magic, BTINFO_MAGIC, sizeof(bi_magic));
277 1.7.2.2 rmind }
278 1.7.2.2 rmind
279 1.7.2.2 rmind void
280 1.7.2.2 rmind bi_add(void *new, int type, int size)
281 1.7.2.2 rmind {
282 1.7.2.2 rmind struct btinfo_common *bi;
283 1.7.2.2 rmind
284 1.7.2.2 rmind if (bi_size + size > BOOTINFO_MAXSIZE)
285 1.7.2.2 rmind return; /* XXX error? */
286 1.7.2.2 rmind
287 1.7.2.2 rmind bi = new;
288 1.7.2.2 rmind bi->next = size;
289 1.7.2.2 rmind bi->type = type;
290 1.7.2.2 rmind memcpy(bi_next, new, size);
291 1.7.2.2 rmind bi_next += size;
292 1.7.2.2 rmind }
293 1.7.2.2 rmind
294 1.7.2.2 rmind void
295 1.7.2.2 rmind module_add(char *name)
296 1.7.2.2 rmind {
297 1.7.2.2 rmind struct boot_module *bm, *bmp;
298 1.7.2.2 rmind
299 1.7.2.2 rmind while (*name == ' ' || *name == '\t')
300 1.7.2.2 rmind ++name;
301 1.7.2.2 rmind
302 1.7.2.2 rmind bm = alloc(sizeof(struct boot_module) + strlen(name) + 1);
303 1.7.2.2 rmind if (bm == NULL) {
304 1.7.2.2 rmind printf("couldn't allocate module %s\n", name);
305 1.7.2.2 rmind return;
306 1.7.2.2 rmind }
307 1.7.2.2 rmind
308 1.7.2.2 rmind bm->bm_kmod = (char *)(bm + 1);
309 1.7.2.2 rmind bm->bm_len = -1;
310 1.7.2.2 rmind bm->bm_next = NULL;
311 1.7.2.2 rmind strcpy(bm->bm_kmod, name);
312 1.7.2.2 rmind if ((bmp = boot_modules) == NULL)
313 1.7.2.2 rmind boot_modules = bm;
314 1.7.2.2 rmind else {
315 1.7.2.2 rmind while (bmp->bm_next != NULL)
316 1.7.2.2 rmind bmp = bmp->bm_next;
317 1.7.2.2 rmind bmp->bm_next = bm;
318 1.7.2.2 rmind }
319 1.7.2.2 rmind }
320 1.7.2.2 rmind
321 1.7.2.2 rmind #define PAGE_SIZE 4096
322 1.7.2.2 rmind #define alignpg(x) (((x)+PAGE_SIZE-1) & ~(PAGE_SIZE-1))
323 1.7.2.2 rmind
324 1.7.2.2 rmind void
325 1.7.2.2 rmind module_load(char *kernel_path)
326 1.7.2.2 rmind {
327 1.7.2.2 rmind struct boot_module *bm;
328 1.7.2.2 rmind struct bi_modulelist_entry *bi;
329 1.7.2.2 rmind struct stat st;
330 1.7.2.2 rmind char *p;
331 1.7.2.2 rmind int size, fd;
332 1.7.2.2 rmind
333 1.7.2.2 rmind strcpy(module_base, kernel_path);
334 1.7.2.2 rmind if ((p = strchr(module_base, ':')) == NULL)
335 1.7.2.2 rmind return; /* eeh?! */
336 1.7.2.2 rmind p += 1;
337 1.7.2.2 rmind size = sizeof(module_base) - (p - module_base);
338 1.7.2.2 rmind
339 1.7.2.2 rmind if (netbsd_version / 1000000 % 100 == 99) {
340 1.7.2.2 rmind /* -current */
341 1.7.2.2 rmind snprintf(p, size,
342 1.7.2.2 rmind "/stand/sandpoint/%d.%d.%d/modules",
343 1.7.2.2 rmind netbsd_version / 100000000,
344 1.7.2.2 rmind netbsd_version / 1000000 % 100,
345 1.7.2.2 rmind netbsd_version / 100 % 100);
346 1.7.2.2 rmind }
347 1.7.2.2 rmind else if (netbsd_version != 0) {
348 1.7.2.2 rmind /* release */
349 1.7.2.2 rmind snprintf(p, size,
350 1.7.2.2 rmind "/stand/sandpoint/%d.%d/modules",
351 1.7.2.2 rmind netbsd_version / 100000000,
352 1.7.2.2 rmind netbsd_version / 1000000 % 100);
353 1.7.2.2 rmind }
354 1.7.2.2 rmind
355 1.7.2.2 rmind /*
356 1.7.2.2 rmind * 1st pass; determine module existence
357 1.7.2.2 rmind */
358 1.7.2.2 rmind size = 0;
359 1.7.2.2 rmind for (bm = boot_modules; bm != NULL; bm = bm->bm_next) {
360 1.7.2.2 rmind fd = module_open(bm);
361 1.7.2.2 rmind if (fd == -1)
362 1.7.2.2 rmind continue;
363 1.7.2.2 rmind if (fstat(fd, &st) == -1 || st.st_size == -1) {
364 1.7.2.2 rmind printf("WARNING: couldn't stat %s\n", bm->bm_kmod);
365 1.7.2.2 rmind close(fd);
366 1.7.2.2 rmind continue;
367 1.7.2.2 rmind }
368 1.7.2.2 rmind bm->bm_len = (int)st.st_size;
369 1.7.2.2 rmind close(fd);
370 1.7.2.2 rmind size += sizeof(struct bi_modulelist_entry);
371 1.7.2.2 rmind }
372 1.7.2.2 rmind if (size == 0)
373 1.7.2.2 rmind return;
374 1.7.2.2 rmind
375 1.7.2.2 rmind size += sizeof(struct btinfo_modulelist);
376 1.7.2.2 rmind btinfo_modulelist = alloc(size);
377 1.7.2.2 rmind if (btinfo_modulelist == NULL) {
378 1.7.2.2 rmind printf("WARNING: couldn't allocate module list\n");
379 1.7.2.2 rmind return;
380 1.7.2.2 rmind }
381 1.7.2.2 rmind btinfo_modulelist_size = size;
382 1.7.2.2 rmind btinfo_modulelist->num = 0;
383 1.7.2.2 rmind
384 1.7.2.2 rmind /*
385 1.7.2.2 rmind * 2nd pass; load modules into memory
386 1.7.2.2 rmind */
387 1.7.2.2 rmind kmodloadp = alignpg(kmodloadp);
388 1.7.2.2 rmind bi = (struct bi_modulelist_entry *)(btinfo_modulelist + 1);
389 1.7.2.2 rmind for (bm = boot_modules; bm != NULL; bm = bm->bm_next) {
390 1.7.2.2 rmind if (bm->bm_len == -1)
391 1.7.2.2 rmind continue; /* already found unavailable */
392 1.7.2.2 rmind fd = module_open(bm);
393 1.7.2.2 rmind printf("module \"%s\" ", bm->bm_kmod);
394 1.7.2.2 rmind size = read(fd, (char *)kmodloadp, SSIZE_MAX);
395 1.7.2.2 rmind if (size < bm->bm_len)
396 1.7.2.2 rmind printf("WARNING: couldn't load");
397 1.7.2.2 rmind else {
398 1.7.2.2 rmind snprintf(bi->kmod, sizeof(bi->kmod), bm->bm_kmod);
399 1.7.2.2 rmind bi->type = BI_MODULE_ELF;
400 1.7.2.2 rmind bi->len = size;
401 1.7.2.2 rmind bi->base = kmodloadp;
402 1.7.2.2 rmind btinfo_modulelist->num += 1;
403 1.7.2.2 rmind printf("loaded at 0x%08x size 0x%x", kmodloadp, size);
404 1.7.2.2 rmind kmodloadp += alignpg(size);
405 1.7.2.2 rmind bi += 1;
406 1.7.2.2 rmind }
407 1.7.2.2 rmind printf("\n");
408 1.7.2.2 rmind close(fd);
409 1.7.2.2 rmind }
410 1.7.2.2 rmind btinfo_modulelist->endpa = kmodloadp;
411 1.7.2.2 rmind }
412 1.7.2.2 rmind
413 1.7.2.2 rmind int
414 1.7.2.2 rmind module_open(struct boot_module *bm)
415 1.7.2.2 rmind {
416 1.7.2.2 rmind char path[80];
417 1.7.2.2 rmind int fd;
418 1.7.2.2 rmind
419 1.7.2.2 rmind snprintf(path, sizeof(path),
420 1.7.2.2 rmind "%s/%s/%s.kmod", module_base, bm->bm_kmod, bm->bm_kmod);
421 1.7.2.2 rmind fd = open(path, 0);
422 1.7.2.2 rmind return fd;
423 1.7.2.2 rmind }
424 1.7.2.2 rmind
425 1.7.2.2 rmind #if 0
426 1.7.2.2 rmind static const char *cmdln[] = {
427 1.7.2.2 rmind "console=ttyS0,115200 root=/dev/sda1 rw initrd=0x200000,2M",
428 1.7.2.2 rmind "console=ttyS0,115200 root=/dev/nfs ip=dhcp"
429 1.7.2.2 rmind };
430 1.7.2.2 rmind
431 1.7.2.2 rmind void
432 1.7.2.2 rmind mkatagparams(unsigned addr, char *kcmd)
433 1.7.2.2 rmind {
434 1.7.2.2 rmind struct tag {
435 1.7.2.2 rmind unsigned siz;
436 1.7.2.2 rmind unsigned tag;
437 1.7.2.2 rmind unsigned val[1];
438 1.7.2.2 rmind };
439 1.7.2.2 rmind struct tag *p;
440 1.7.2.2 rmind #define ATAG_CORE 0x54410001
441 1.7.2.2 rmind #define ATAG_MEM 0x54410002
442 1.7.2.2 rmind #define ATAG_INITRD 0x54410005
443 1.7.2.2 rmind #define ATAG_CMDLINE 0x54410009
444 1.7.2.2 rmind #define ATAG_NONE 0x00000000
445 1.7.2.2 rmind #define tagnext(p) (struct tag *)((unsigned *)(p) + (p)->siz)
446 1.7.2.2 rmind #define tagsize(n) (2 + (n))
447 1.7.2.2 rmind
448 1.7.2.2 rmind p = (struct tag *)addr;
449 1.7.2.2 rmind p->tag = ATAG_CORE;
450 1.7.2.2 rmind p->siz = tagsize(3);
451 1.7.2.2 rmind p->val[0] = 0; /* flags */
452 1.7.2.2 rmind p->val[1] = 0; /* pagesize */
453 1.7.2.2 rmind p->val[2] = 0; /* rootdev */
454 1.7.2.2 rmind p = tagnext(p);
455 1.7.2.2 rmind p->tag = ATAG_MEM;
456 1.7.2.2 rmind p->siz = tagsize(2);
457 1.7.2.2 rmind p->val[0] = 64 * 1024 * 1024;
458 1.7.2.2 rmind p->val[1] = 0; /* start */
459 1.7.2.2 rmind p = tagnext(p);
460 1.7.2.2 rmind if (kcmd != NULL) {
461 1.7.2.2 rmind p = tagnext(p);
462 1.7.2.2 rmind p->tag = ATAG_CMDLINE;
463 1.7.2.2 rmind p->siz = tagsize((strlen(kcmd) + 1 + 3) >> 2);
464 1.7.2.2 rmind strcpy((void *)p->val, kcmd);
465 1.7.2.2 rmind }
466 1.7.2.2 rmind p = tagnext(p);
467 1.7.2.2 rmind p->tag = ATAG_NONE;
468 1.7.2.2 rmind p->siz = 0;
469 1.7.2.2 rmind }
470 1.7.2.2 rmind #endif
471 1.7.2.2 rmind
472 1.7.2.2 rmind void *
473 1.7.2.2 rmind allocaligned(size_t size, size_t align)
474 1.7.2.2 rmind {
475 1.7.2.2 rmind uint32_t p;
476 1.7.2.2 rmind
477 1.7.2.2 rmind if (align-- < 2)
478 1.7.2.2 rmind return alloc(size);
479 1.7.2.2 rmind p = (uint32_t)alloc(size + align);
480 1.7.2.2 rmind return (void *)((p + align) & ~align);
481 1.7.2.2 rmind }
482 1.7.2.2 rmind
483 1.7.2.2 rmind static int
484 1.7.2.2 rmind check_bootname(char *s)
485 1.7.2.2 rmind {
486 1.7.2.2 rmind /*
487 1.7.2.2 rmind * nfs:
488 1.7.2.2 rmind * nfs:<bootfile>
489 1.7.2.2 rmind * tftp:
490 1.7.2.2 rmind * tftp:<bootfile>
491 1.7.2.2 rmind * wd[N[P]]:<bootfile>
492 1.7.2.2 rmind *
493 1.7.2.2 rmind * net is a synonym of nfs.
494 1.7.2.2 rmind */
495 1.7.2.2 rmind if (strncmp(s, "nfs:", 4) == 0 || strncmp(s, "net:", 4) == 0)
496 1.7.2.2 rmind return 1;
497 1.7.2.2 rmind if (strncmp(s, "tftp:", 5) == 0)
498 1.7.2.2 rmind return 1;
499 1.7.2.2 rmind if (s[0] == 'w' && s[1] == 'd') {
500 1.7.2.2 rmind s += 2;
501 1.7.2.2 rmind if (*s != ':' && *s >= '0' && *s <= '3') {
502 1.7.2.2 rmind ++s;
503 1.7.2.2 rmind if (*s != ':' && *s >= 'a' && *s <= 'p')
504 1.7.2.2 rmind ++s;
505 1.7.2.2 rmind }
506 1.7.2.2 rmind return *s == ':';
507 1.7.2.2 rmind }
508 1.7.2.2 rmind return 0;
509 1.7.2.2 rmind }
510 1.7.2.2 rmind
511 1.7.2.2 rmind static int
512 1.7.2.2 rmind parse_cmdline(char **argv, int maxargc, char *p, char *end)
513 1.7.2.2 rmind {
514 1.7.2.2 rmind int argc;
515 1.7.2.2 rmind
516 1.7.2.2 rmind argv[0] = "";
517 1.7.2.2 rmind for (argc = 1; argc < maxargc && p < end; argc++) {
518 1.7.2.2 rmind while (is_space(*p))
519 1.7.2.2 rmind p++;
520 1.7.2.2 rmind if (p >= end)
521 1.7.2.2 rmind break;
522 1.7.2.2 rmind argv[argc] = p;
523 1.7.2.2 rmind while (!is_space(*p) && p < end)
524 1.7.2.2 rmind p++;
525 1.7.2.2 rmind *p++ = '\0';
526 1.7.2.2 rmind }
527 1.7.2.2 rmind
528 1.7.2.2 rmind return argc;
529 1.7.2.2 rmind }
530 1.7.2.2 rmind
531 1.7.2.2 rmind static int
532 1.7.2.2 rmind is_space(char c)
533 1.7.2.2 rmind {
534 1.7.2.2 rmind return c > '\0' && c <= ' ';
535 1.7.2.2 rmind }
536