exec.c revision 1.47 1 1.47 uebayasi /* $NetBSD: exec.c,v 1.47 2011/05/26 04:25:27 uebayasi Exp $ */
2 1.23 ad
3 1.23 ad /*-
4 1.40 ad * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
5 1.23 ad * All rights reserved.
6 1.23 ad *
7 1.23 ad * Redistribution and use in source and binary forms, with or without
8 1.23 ad * modification, are permitted provided that the following conditions
9 1.23 ad * are met:
10 1.23 ad * 1. Redistributions of source code must retain the above copyright
11 1.23 ad * notice, this list of conditions and the following disclaimer.
12 1.23 ad * 2. Redistributions in binary form must reproduce the above copyright
13 1.23 ad * notice, this list of conditions and the following disclaimer in the
14 1.23 ad * documentation and/or other materials provided with the distribution.
15 1.23 ad *
16 1.23 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.23 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.23 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.23 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.23 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.23 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.23 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.23 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.23 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.23 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.23 ad * POSSIBILITY OF SUCH DAMAGE.
27 1.23 ad */
28 1.1 perry
29 1.1 perry /*
30 1.1 perry * Copyright (c) 1982, 1986, 1990, 1993
31 1.1 perry * The Regents of the University of California. All rights reserved.
32 1.19 agc *
33 1.19 agc * Redistribution and use in source and binary forms, with or without
34 1.19 agc * modification, are permitted provided that the following conditions
35 1.19 agc * are met:
36 1.19 agc * 1. Redistributions of source code must retain the above copyright
37 1.19 agc * notice, this list of conditions and the following disclaimer.
38 1.19 agc * 2. Redistributions in binary form must reproduce the above copyright
39 1.19 agc * notice, this list of conditions and the following disclaimer in the
40 1.19 agc * documentation and/or other materials provided with the distribution.
41 1.19 agc * 3. Neither the name of the University nor the names of its contributors
42 1.19 agc * may be used to endorse or promote products derived from this software
43 1.19 agc * without specific prior written permission.
44 1.19 agc *
45 1.19 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 1.19 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 1.19 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 1.19 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 1.19 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 1.19 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 1.19 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 1.19 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 1.19 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 1.19 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 1.19 agc * SUCH DAMAGE.
56 1.19 agc *
57 1.19 agc * @(#)boot.c 8.1 (Berkeley) 6/10/93
58 1.19 agc */
59 1.19 agc
60 1.19 agc /*
61 1.1 perry * Copyright (c) 1996
62 1.1 perry * Matthias Drochner. All rights reserved.
63 1.1 perry * Copyright (c) 1996
64 1.1 perry * Perry E. Metzger. All rights reserved.
65 1.1 perry *
66 1.1 perry * Redistribution and use in source and binary forms, with or without
67 1.1 perry * modification, are permitted provided that the following conditions
68 1.1 perry * are met:
69 1.1 perry * 1. Redistributions of source code must retain the above copyright
70 1.1 perry * notice, this list of conditions and the following disclaimer.
71 1.1 perry * 2. Redistributions in binary form must reproduce the above copyright
72 1.1 perry * notice, this list of conditions and the following disclaimer in the
73 1.1 perry * documentation and/or other materials provided with the distribution.
74 1.1 perry *
75 1.1 perry * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
76 1.1 perry * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
77 1.1 perry * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
78 1.1 perry * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
79 1.1 perry * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
80 1.1 perry * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
81 1.1 perry * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
82 1.1 perry * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
83 1.1 perry * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
84 1.1 perry * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
85 1.1 perry * SUCH DAMAGE.
86 1.1 perry *
87 1.1 perry * @(#)boot.c 8.1 (Berkeley) 6/10/93
88 1.1 perry */
89 1.1 perry
90 1.2 thorpej /*
91 1.8 christos * starts NetBSD a.out kernel
92 1.8 christos * needs lowlevel startup from startprog.S
93 1.8 christos * This is a special version of exec.c to support use of XMS.
94 1.1 perry */
95 1.8 christos
96 1.1 perry #include <sys/param.h>
97 1.1 perry #include <sys/reboot.h>
98 1.40 ad #include <sys/reboot.h>
99 1.1 perry
100 1.33 joerg #include <machine/multiboot.h>
101 1.40 ad #include <machine/stdarg.h>
102 1.33 joerg
103 1.1 perry #include <lib/libsa/stand.h>
104 1.23 ad #include <lib/libkern/libkern.h>
105 1.4 drochner
106 1.6 christos #include "loadfile.h"
107 1.1 perry #include "libi386.h"
108 1.4 drochner #include "bootinfo.h"
109 1.23 ad #include "bootmod.h"
110 1.42 jmcneill #include "vbe.h"
111 1.16 jdolecek #ifdef SUPPORT_PS2
112 1.16 jdolecek #include "biosmca.h"
113 1.16 jdolecek #endif
114 1.1 perry
115 1.6 christos #define BOOT_NARGS 6
116 1.1 perry
117 1.23 ad #ifndef PAGE_SIZE
118 1.23 ad #define PAGE_SIZE 4096
119 1.23 ad #endif
120 1.23 ad
121 1.43 tsutsui #define MODULE_WARNING_SEC 5
122 1.41 jmcneill
123 1.4 drochner extern struct btinfo_console btinfo_console;
124 1.4 drochner
125 1.23 ad boot_module_t *boot_modules;
126 1.23 ad bool boot_modules_enabled = true;
127 1.23 ad bool kernel_loaded;
128 1.23 ad
129 1.47 uebayasi typedef struct userconf_command {
130 1.47 uebayasi char *uc_text;
131 1.47 uebayasi size_t uc_len;
132 1.47 uebayasi struct userconf_command *uc_next;
133 1.47 uebayasi } userconf_command_t;
134 1.47 uebayasi userconf_command_t *userconf_commands = NULL;
135 1.47 uebayasi
136 1.39 jmcneill static struct btinfo_framebuffer btinfo_framebuffer;
137 1.39 jmcneill
138 1.23 ad static struct btinfo_modulelist *btinfo_modulelist;
139 1.23 ad static size_t btinfo_modulelist_size;
140 1.23 ad static uint32_t image_end;
141 1.26 ad static char module_base[64] = "/";
142 1.40 ad static int howto;
143 1.23 ad
144 1.47 uebayasi static struct btinfo_userconfcommands *btinfo_userconfcommands = NULL;
145 1.47 uebayasi static size_t btinfo_userconfcommands_size = 0;
146 1.47 uebayasi
147 1.41 jmcneill static void module_init(const char *);
148 1.46 jmcneill static void module_add_common(char *, uint8_t);
149 1.23 ad
150 1.47 uebayasi static void userconf_init(void);
151 1.47 uebayasi
152 1.34 ad void
153 1.39 jmcneill framebuffer_configure(struct btinfo_framebuffer *fb)
154 1.39 jmcneill {
155 1.39 jmcneill if (fb)
156 1.39 jmcneill btinfo_framebuffer = *fb;
157 1.39 jmcneill else {
158 1.39 jmcneill btinfo_framebuffer.physaddr = 0;
159 1.39 jmcneill btinfo_framebuffer.flags = 0;
160 1.39 jmcneill }
161 1.39 jmcneill }
162 1.39 jmcneill
163 1.39 jmcneill void
164 1.34 ad module_add(char *name)
165 1.34 ad {
166 1.46 jmcneill return module_add_common(name, BM_TYPE_KMOD);
167 1.46 jmcneill }
168 1.46 jmcneill
169 1.46 jmcneill void
170 1.46 jmcneill splash_add(char *name)
171 1.46 jmcneill {
172 1.46 jmcneill return module_add_common(name, BM_TYPE_IMAGE);
173 1.46 jmcneill }
174 1.46 jmcneill
175 1.46 jmcneill static void
176 1.46 jmcneill module_add_common(char *name, uint8_t type)
177 1.46 jmcneill {
178 1.34 ad boot_module_t *bm, *bmp;
179 1.34 ad size_t len;
180 1.34 ad char *str;
181 1.34 ad
182 1.35 ad while (*name == ' ' || *name == '\t')
183 1.35 ad ++name;
184 1.35 ad
185 1.34 ad bm = alloc(sizeof(boot_module_t));
186 1.34 ad len = strlen(name) + 1;
187 1.34 ad str = alloc(len);
188 1.34 ad if (bm == NULL || str == NULL) {
189 1.34 ad printf("couldn't allocate module\n");
190 1.34 ad return;
191 1.34 ad }
192 1.34 ad memcpy(str, name, len);
193 1.34 ad bm->bm_path = str;
194 1.34 ad bm->bm_next = NULL;
195 1.46 jmcneill bm->bm_type = type;
196 1.34 ad if (boot_modules == NULL)
197 1.34 ad boot_modules = bm;
198 1.34 ad else {
199 1.34 ad for (bmp = boot_modules; bmp->bm_next;
200 1.34 ad bmp = bmp->bm_next)
201 1.34 ad ;
202 1.34 ad bmp->bm_next = bm;
203 1.34 ad }
204 1.34 ad }
205 1.34 ad
206 1.47 uebayasi void
207 1.47 uebayasi userconf_add(char *cmd)
208 1.47 uebayasi {
209 1.47 uebayasi userconf_command_t *uc;
210 1.47 uebayasi size_t len;
211 1.47 uebayasi char *text;
212 1.47 uebayasi
213 1.47 uebayasi while (*cmd == ' ' || *cmd == '\t')
214 1.47 uebayasi ++cmd;
215 1.47 uebayasi
216 1.47 uebayasi uc = alloc(sizeof(*uc));
217 1.47 uebayasi if (uc == NULL) {
218 1.47 uebayasi printf("couldn't allocate command\n");
219 1.47 uebayasi return;
220 1.47 uebayasi }
221 1.47 uebayasi
222 1.47 uebayasi len = strlen(cmd) + 1;
223 1.47 uebayasi text = alloc(len);
224 1.47 uebayasi if (text == NULL) {
225 1.47 uebayasi dealloc(uc, sizeof(*uc));
226 1.47 uebayasi printf("couldn't allocate command\n");
227 1.47 uebayasi return;
228 1.47 uebayasi }
229 1.47 uebayasi memcpy(text, cmd, len);
230 1.47 uebayasi
231 1.47 uebayasi uc->uc_text = text;
232 1.47 uebayasi uc->uc_len = len;
233 1.47 uebayasi uc->uc_next = NULL;
234 1.47 uebayasi
235 1.47 uebayasi if (userconf_commands == NULL)
236 1.47 uebayasi userconf_commands = uc;
237 1.47 uebayasi else {
238 1.47 uebayasi userconf_command_t *ucp;
239 1.47 uebayasi for (ucp = userconf_commands; ucp->uc_next != NULL;
240 1.47 uebayasi ucp = ucp->uc_next)
241 1.47 uebayasi ;
242 1.47 uebayasi ucp->uc_next = uc;
243 1.47 uebayasi }
244 1.47 uebayasi }
245 1.47 uebayasi
246 1.32 joerg static int
247 1.32 joerg common_load_kernel(const char *file, u_long *basemem, u_long *extmem,
248 1.32 joerg physaddr_t loadaddr, int floppy, u_long marks[MARK_MAX])
249 1.1 perry {
250 1.32 joerg int fd;
251 1.8 christos #ifdef XMS
252 1.8 christos u_long xmsmem;
253 1.8 christos physaddr_t origaddr = loadaddr;
254 1.8 christos #endif
255 1.8 christos
256 1.32 joerg *extmem = getextmem();
257 1.32 joerg *basemem = getbasemem();
258 1.8 christos
259 1.8 christos #ifdef XMS
260 1.8 christos if ((getextmem1() == 0) && (xmsmem = checkxms())) {
261 1.8 christos u_long kernsize;
262 1.8 christos
263 1.8 christos /*
264 1.8 christos * With "CONSERVATIVE_MEMDETECT", extmem is 0 because
265 1.8 christos * getextmem() is getextmem1(). Without, the "smart"
266 1.8 christos * methods could fail to report all memory as well.
267 1.8 christos * xmsmem is a few kB less than the actual size, but
268 1.8 christos * better than nothing.
269 1.8 christos */
270 1.32 joerg if (xmsmem > *extmem)
271 1.32 joerg *extmem = xmsmem;
272 1.21 junyoung /*
273 1.8 christos * Get the size of the kernel
274 1.8 christos */
275 1.8 christos marks[MARK_START] = loadaddr;
276 1.12 christos if ((fd = loadfile(file, marks, COUNT_KERNEL)) == -1)
277 1.32 joerg return EIO;
278 1.8 christos close(fd);
279 1.8 christos
280 1.8 christos kernsize = marks[MARK_END];
281 1.8 christos kernsize = (kernsize + 1023) / 1024;
282 1.8 christos
283 1.8 christos loadaddr = xmsalloc(kernsize);
284 1.8 christos if (!loadaddr)
285 1.21 junyoung return ENOMEM;
286 1.8 christos }
287 1.8 christos #endif
288 1.7 christos marks[MARK_START] = loadaddr;
289 1.29 christos if ((fd = loadfile(file, marks,
290 1.44 christos LOAD_KERNEL & ~(floppy ? LOAD_BACKWARDS : 0))) == -1)
291 1.32 joerg return EIO;
292 1.1 perry
293 1.8 christos close(fd);
294 1.10 drochner
295 1.34 ad /* Now we know the root fs type, load modules for it. */
296 1.34 ad module_add(fsmod);
297 1.34 ad if (fsmod2 != NULL && strcmp(fsmod, fsmod2) != 0)
298 1.34 ad module_add(fsmod2);
299 1.34 ad
300 1.10 drochner /*
301 1.10 drochner * Gather some information for the kernel. Do this after the
302 1.10 drochner * "point of no return" to avoid memory leaks.
303 1.10 drochner * (but before DOS might be trashed in the XMS case)
304 1.10 drochner */
305 1.10 drochner #ifdef PASS_BIOSGEOM
306 1.10 drochner bi_getbiosgeom();
307 1.10 drochner #endif
308 1.10 drochner #ifdef PASS_MEMMAP
309 1.10 drochner bi_getmemmap();
310 1.10 drochner #endif
311 1.8 christos
312 1.8 christos #ifdef XMS
313 1.8 christos if (loadaddr != origaddr) {
314 1.8 christos /*
315 1.14 ross * We now have done our last DOS IO, so we may
316 1.8 christos * trash the OS. Copy the data from the temporary
317 1.20 wiz * buffer to its real address.
318 1.8 christos */
319 1.8 christos marks[MARK_START] -= loadaddr;
320 1.8 christos marks[MARK_END] -= loadaddr;
321 1.8 christos marks[MARK_SYM] -= loadaddr;
322 1.8 christos marks[MARK_END] -= loadaddr;
323 1.8 christos ppbcopy(loadaddr, origaddr, marks[MARK_END]);
324 1.8 christos }
325 1.8 christos #endif
326 1.8 christos marks[MARK_END] = (((u_long) marks[MARK_END] + sizeof(int) - 1)) &
327 1.8 christos (-sizeof(int));
328 1.23 ad image_end = marks[MARK_END];
329 1.23 ad kernel_loaded = true;
330 1.1 perry
331 1.32 joerg return 0;
332 1.32 joerg }
333 1.32 joerg
334 1.32 joerg int
335 1.40 ad exec_netbsd(const char *file, physaddr_t loadaddr, int boothowto, int floppy,
336 1.40 ad void (*callback)(void))
337 1.32 joerg {
338 1.32 joerg u_long boot_argv[BOOT_NARGS];
339 1.32 joerg u_long marks[MARK_MAX];
340 1.32 joerg struct btinfo_symtab btinfo_symtab;
341 1.32 joerg u_long extmem;
342 1.32 joerg u_long basemem;
343 1.32 joerg
344 1.32 joerg #ifdef DEBUG
345 1.32 joerg printf("exec: file=%s loadaddr=0x%lx\n",
346 1.32 joerg file ? file : "NULL", loadaddr);
347 1.32 joerg #endif
348 1.32 joerg
349 1.32 joerg BI_ALLOC(32); /* ??? */
350 1.32 joerg
351 1.32 joerg BI_ADD(&btinfo_console, BTINFO_CONSOLE, sizeof(struct btinfo_console));
352 1.32 joerg
353 1.40 ad howto = boothowto;
354 1.40 ad
355 1.32 joerg if (common_load_kernel(file, &basemem, &extmem, loadaddr, floppy, marks))
356 1.32 joerg goto out;
357 1.32 joerg
358 1.31 joerg boot_argv[0] = boothowto;
359 1.31 joerg boot_argv[1] = 0;
360 1.31 joerg boot_argv[2] = vtophys(bootinfo); /* old cyl offset */
361 1.31 joerg boot_argv[3] = marks[MARK_END];
362 1.31 joerg boot_argv[4] = extmem;
363 1.31 joerg boot_argv[5] = basemem;
364 1.31 joerg
365 1.23 ad /* pull in any modules if necessary */
366 1.23 ad if (boot_modules_enabled) {
367 1.41 jmcneill module_init(file);
368 1.23 ad if (btinfo_modulelist) {
369 1.23 ad BI_ADD(btinfo_modulelist, BTINFO_MODULELIST,
370 1.23 ad btinfo_modulelist_size);
371 1.23 ad }
372 1.23 ad }
373 1.1 perry
374 1.47 uebayasi userconf_init();
375 1.47 uebayasi if (btinfo_userconfcommands != NULL)
376 1.47 uebayasi BI_ADD(btinfo_userconfcommands, BTINFO_USERCONFCOMMANDS,
377 1.47 uebayasi btinfo_userconfcommands_size);
378 1.47 uebayasi
379 1.1 perry #ifdef DEBUG
380 1.8 christos printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", marks[MARK_ENTRY],
381 1.7 christos marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]);
382 1.1 perry #endif
383 1.1 perry
384 1.8 christos btinfo_symtab.nsym = marks[MARK_NSYM];
385 1.8 christos btinfo_symtab.ssym = marks[MARK_SYM];
386 1.8 christos btinfo_symtab.esym = marks[MARK_END];
387 1.8 christos BI_ADD(&btinfo_symtab, BTINFO_SYMTAB, sizeof(struct btinfo_symtab));
388 1.8 christos
389 1.42 jmcneill /* set new video mode if necessary */
390 1.42 jmcneill vbe_commit();
391 1.42 jmcneill BI_ADD(&btinfo_framebuffer, BTINFO_FRAMEBUFFER,
392 1.42 jmcneill sizeof(struct btinfo_framebuffer));
393 1.42 jmcneill
394 1.40 ad if (callback != NULL)
395 1.40 ad (*callback)();
396 1.15 dbj startprog(marks[MARK_ENTRY], BOOT_NARGS, boot_argv,
397 1.21 junyoung x86_trunc_page(basemem*1024));
398 1.1 perry panic("exec returned");
399 1.1 perry
400 1.4 drochner out:
401 1.4 drochner BI_FREE();
402 1.4 drochner bootinfo = 0;
403 1.21 junyoung return -1;
404 1.1 perry }
405 1.23 ad
406 1.41 jmcneill static void
407 1.41 jmcneill extract_device(const char *path, char *buf, size_t buflen)
408 1.41 jmcneill {
409 1.41 jmcneill int i;
410 1.41 jmcneill
411 1.41 jmcneill if (strchr(path, ':') != NULL) {
412 1.41 jmcneill for (i = 0; i < buflen - 2 && path[i] != ':'; i++)
413 1.41 jmcneill buf[i] = path[i];
414 1.41 jmcneill buf[i++] = ':';
415 1.41 jmcneill buf[i] = '\0';
416 1.41 jmcneill } else
417 1.41 jmcneill buf[0] = '\0';
418 1.41 jmcneill }
419 1.41 jmcneill
420 1.26 ad static const char *
421 1.41 jmcneill module_path(boot_module_t *bm, const char *kdev)
422 1.26 ad {
423 1.26 ad static char buf[256];
424 1.41 jmcneill char name_buf[256], dev_buf[64];
425 1.41 jmcneill const char *name, *name2, *p;
426 1.26 ad
427 1.26 ad name = bm->bm_path;
428 1.33 joerg for (name2 = name; *name2; ++name2) {
429 1.33 joerg if (*name2 == ' ' || *name2 == '\t') {
430 1.33 joerg strlcpy(name_buf, name, sizeof(name_buf));
431 1.33 joerg if (name2 - name < sizeof(name_buf))
432 1.33 joerg name_buf[name2 - name] = '\0';
433 1.33 joerg name = name_buf;
434 1.33 joerg break;
435 1.33 joerg }
436 1.33 joerg }
437 1.41 jmcneill if ((p = strchr(name, ':')) != NULL) {
438 1.41 jmcneill /* device specified, use it */
439 1.41 jmcneill if (p[1] == '/')
440 1.41 jmcneill snprintf(buf, sizeof(buf), "%s", name);
441 1.41 jmcneill else {
442 1.41 jmcneill p++;
443 1.41 jmcneill extract_device(name, dev_buf, sizeof(dev_buf));
444 1.41 jmcneill snprintf(buf, sizeof(buf), "%s%s/%s/%s.kmod",
445 1.41 jmcneill dev_buf, module_base, p, p);
446 1.41 jmcneill }
447 1.41 jmcneill } else {
448 1.41 jmcneill /* device not specified; load from kernel device if known */
449 1.41 jmcneill if (name[0] == '/')
450 1.41 jmcneill snprintf(buf, sizeof(buf), "%s%s", kdev, name);
451 1.41 jmcneill else
452 1.41 jmcneill snprintf(buf, sizeof(buf), "%s%s/%s/%s.kmod",
453 1.41 jmcneill kdev, module_base, name, name);
454 1.41 jmcneill }
455 1.33 joerg
456 1.26 ad return buf;
457 1.26 ad }
458 1.26 ad
459 1.36 christos static int
460 1.41 jmcneill module_open(boot_module_t *bm, int mode, const char *kdev, bool doload)
461 1.28 chris {
462 1.28 chris int fd;
463 1.28 chris const char *path;
464 1.36 christos
465 1.28 chris /* check the expanded path first */
466 1.41 jmcneill path = module_path(bm, kdev);
467 1.28 chris fd = open(path, mode);
468 1.41 jmcneill if (fd != -1) {
469 1.41 jmcneill if ((howto & AB_SILENT) == 0 && doload)
470 1.41 jmcneill printf("Loading %s ", path);
471 1.41 jmcneill } else {
472 1.28 chris /* now attempt the raw path provided */
473 1.28 chris fd = open(bm->bm_path, mode);
474 1.41 jmcneill if (fd != -1 && (howto & AB_SILENT) == 0 && doload)
475 1.41 jmcneill printf("Loading %s ", bm->bm_path);
476 1.41 jmcneill }
477 1.41 jmcneill if (!doload && fd == -1) {
478 1.41 jmcneill printf("WARNING: couldn't open %s", bm->bm_path);
479 1.41 jmcneill if (strcmp(bm->bm_path, path) != 0)
480 1.41 jmcneill printf(" (%s)", path);
481 1.41 jmcneill printf("\n");
482 1.28 chris }
483 1.28 chris return fd;
484 1.28 chris }
485 1.28 chris
486 1.23 ad static void
487 1.41 jmcneill module_init(const char *kernel_path)
488 1.23 ad {
489 1.23 ad struct bi_modulelist_entry *bi;
490 1.23 ad struct stat st;
491 1.30 joerg const char *machine;
492 1.41 jmcneill char kdev[64];
493 1.23 ad char *buf;
494 1.23 ad boot_module_t *bm;
495 1.23 ad size_t len;
496 1.23 ad off_t off;
497 1.41 jmcneill int err, fd, nfail = 0;
498 1.41 jmcneill
499 1.41 jmcneill extract_device(kernel_path, kdev, sizeof(kdev));
500 1.23 ad
501 1.30 joerg switch (netbsd_elf_class) {
502 1.30 joerg case ELFCLASS32:
503 1.30 joerg machine = "i386";
504 1.30 joerg break;
505 1.30 joerg case ELFCLASS64:
506 1.30 joerg machine = "amd64";
507 1.30 joerg break;
508 1.30 joerg default:
509 1.30 joerg machine = "generic";
510 1.30 joerg break;
511 1.30 joerg }
512 1.30 joerg if (netbsd_version / 1000000 % 100 == 99) {
513 1.30 joerg /* -current */
514 1.30 joerg snprintf(module_base, sizeof(module_base),
515 1.38 rmind "/stand/%s/%d.%d.%d/modules", machine,
516 1.30 joerg netbsd_version / 100000000,
517 1.30 joerg netbsd_version / 1000000 % 100,
518 1.30 joerg netbsd_version / 100 % 100);
519 1.30 joerg } else if (netbsd_version != 0) {
520 1.30 joerg /* release */
521 1.30 joerg snprintf(module_base, sizeof(module_base),
522 1.38 rmind "/stand/%s/%d.%d/modules", machine,
523 1.30 joerg netbsd_version / 100000000,
524 1.30 joerg netbsd_version / 1000000 % 100);
525 1.30 joerg }
526 1.30 joerg
527 1.23 ad /* First, see which modules are valid and calculate btinfo size */
528 1.23 ad len = sizeof(struct btinfo_modulelist);
529 1.23 ad for (bm = boot_modules; bm; bm = bm->bm_next) {
530 1.41 jmcneill fd = module_open(bm, 0, kdev, false);
531 1.23 ad if (fd == -1) {
532 1.23 ad bm->bm_len = -1;
533 1.41 jmcneill ++nfail;
534 1.23 ad continue;
535 1.23 ad }
536 1.23 ad err = fstat(fd, &st);
537 1.24 ad if (err == -1 || st.st_size == -1) {
538 1.28 chris printf("WARNING: couldn't stat %s\n", bm->bm_path);
539 1.23 ad close(fd);
540 1.23 ad bm->bm_len = -1;
541 1.41 jmcneill ++nfail;
542 1.23 ad continue;
543 1.23 ad }
544 1.23 ad bm->bm_len = st.st_size;
545 1.23 ad close(fd);
546 1.23 ad len += sizeof(struct bi_modulelist_entry);
547 1.23 ad }
548 1.23 ad
549 1.23 ad /* Allocate the module list */
550 1.23 ad btinfo_modulelist = alloc(len);
551 1.23 ad if (btinfo_modulelist == NULL) {
552 1.23 ad printf("WARNING: couldn't allocate module list\n");
553 1.43 tsutsui wait_sec(MODULE_WARNING_SEC);
554 1.23 ad return;
555 1.23 ad }
556 1.23 ad memset(btinfo_modulelist, 0, len);
557 1.23 ad btinfo_modulelist_size = len;
558 1.23 ad
559 1.23 ad /* Fill in btinfo structure */
560 1.23 ad buf = (char *)btinfo_modulelist;
561 1.23 ad btinfo_modulelist->num = 0;
562 1.23 ad off = sizeof(struct btinfo_modulelist);
563 1.23 ad
564 1.23 ad for (bm = boot_modules; bm; bm = bm->bm_next) {
565 1.23 ad if (bm->bm_len == -1)
566 1.23 ad continue;
567 1.41 jmcneill fd = module_open(bm, 0, kdev, true);
568 1.41 jmcneill if (fd == -1)
569 1.23 ad continue;
570 1.24 ad image_end = (image_end + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
571 1.24 ad len = pread(fd, (void *)image_end, SSIZE_MAX);
572 1.24 ad if (len < bm->bm_len) {
573 1.40 ad if ((howto & AB_SILENT) != 0)
574 1.40 ad printf("Loading %s ", bm->bm_path);
575 1.23 ad printf(" FAILED\n");
576 1.23 ad } else {
577 1.23 ad btinfo_modulelist->num++;
578 1.23 ad bi = (struct bi_modulelist_entry *)(buf + off);
579 1.23 ad off += sizeof(struct bi_modulelist_entry);
580 1.23 ad strncpy(bi->path, bm->bm_path, sizeof(bi->path) - 1);
581 1.24 ad bi->base = image_end;
582 1.24 ad bi->len = len;
583 1.46 jmcneill bi->type = bm->bm_type == BM_TYPE_KMOD ?
584 1.46 jmcneill BI_MODULE_ELF : BI_MODULE_IMAGE;
585 1.40 ad if ((howto & AB_SILENT) == 0)
586 1.40 ad printf(" \n");
587 1.23 ad }
588 1.24 ad if (len > 0)
589 1.24 ad image_end += len;
590 1.23 ad close(fd);
591 1.23 ad }
592 1.23 ad btinfo_modulelist->endpa = image_end;
593 1.41 jmcneill
594 1.41 jmcneill if (nfail > 0) {
595 1.41 jmcneill printf("WARNING: %d module%s failed to load\n",
596 1.41 jmcneill nfail, nfail == 1 ? "" : "s");
597 1.41 jmcneill #if notyet
598 1.43 tsutsui wait_sec(MODULE_WARNING_SEC);
599 1.41 jmcneill #endif
600 1.41 jmcneill }
601 1.23 ad }
602 1.33 joerg
603 1.47 uebayasi static void
604 1.47 uebayasi userconf_init(void)
605 1.47 uebayasi {
606 1.47 uebayasi size_t count, len;
607 1.47 uebayasi userconf_command_t *uc;
608 1.47 uebayasi char *buf;
609 1.47 uebayasi off_t off;
610 1.47 uebayasi
611 1.47 uebayasi /* Calculate the userconf commands list size */
612 1.47 uebayasi count = 0;
613 1.47 uebayasi for (uc = userconf_commands; uc != NULL; uc = uc->uc_next)
614 1.47 uebayasi count++;
615 1.47 uebayasi len = sizeof(btinfo_userconfcommands) +
616 1.47 uebayasi count * sizeof(struct bi_userconfcommand);
617 1.47 uebayasi
618 1.47 uebayasi /* Allocate the userconf commands list */
619 1.47 uebayasi btinfo_userconfcommands = alloc(len);
620 1.47 uebayasi if (btinfo_userconfcommands == NULL) {
621 1.47 uebayasi printf("WARNING: couldn't allocate userconf commands list\n");
622 1.47 uebayasi return;
623 1.47 uebayasi }
624 1.47 uebayasi memset(btinfo_userconfcommands, 0, len);
625 1.47 uebayasi btinfo_userconfcommands_size = len;
626 1.47 uebayasi
627 1.47 uebayasi /* Fill in btinfo structure */
628 1.47 uebayasi buf = (char *)btinfo_userconfcommands;
629 1.47 uebayasi off = sizeof(*btinfo_userconfcommands);
630 1.47 uebayasi btinfo_userconfcommands->num = 0;
631 1.47 uebayasi for (uc = userconf_commands; uc != NULL; uc = uc->uc_next) {
632 1.47 uebayasi struct bi_userconfcommand *bi;
633 1.47 uebayasi bi = (struct bi_userconfcommand *)(buf + off);
634 1.47 uebayasi strncpy(bi->text, uc->uc_text, sizeof(bi->text) - 1);
635 1.47 uebayasi
636 1.47 uebayasi off += sizeof(*bi);
637 1.47 uebayasi btinfo_userconfcommands->num++;
638 1.47 uebayasi }
639 1.47 uebayasi }
640 1.47 uebayasi
641 1.33 joerg int
642 1.33 joerg exec_multiboot(const char *file, char *args)
643 1.33 joerg {
644 1.33 joerg struct multiboot_info *mbi;
645 1.33 joerg struct multiboot_module *mbm;
646 1.33 joerg struct bi_modulelist_entry *bim;
647 1.33 joerg int i, len;
648 1.33 joerg u_long marks[MARK_MAX];
649 1.33 joerg u_long extmem;
650 1.33 joerg u_long basemem;
651 1.33 joerg char *cmdline;
652 1.33 joerg
653 1.33 joerg mbi = alloc(sizeof(struct multiboot_info));
654 1.33 joerg mbi->mi_flags = MULTIBOOT_INFO_HAS_MEMORY;
655 1.33 joerg
656 1.33 joerg if (common_load_kernel(file, &basemem, &extmem, 0, 0, marks))
657 1.33 joerg goto out;
658 1.33 joerg
659 1.33 joerg mbi->mi_mem_upper = extmem;
660 1.33 joerg mbi->mi_mem_lower = basemem;
661 1.33 joerg
662 1.33 joerg if (args) {
663 1.33 joerg mbi->mi_flags |= MULTIBOOT_INFO_HAS_CMDLINE;
664 1.33 joerg len = strlen(file) + 1 + strlen(args) + 1;
665 1.33 joerg cmdline = alloc(len);
666 1.33 joerg snprintf(cmdline, len, "%s %s", file, args);
667 1.33 joerg mbi->mi_cmdline = (char *) vtophys(cmdline);
668 1.33 joerg }
669 1.33 joerg
670 1.33 joerg /* pull in any modules if necessary */
671 1.33 joerg if (boot_modules_enabled) {
672 1.41 jmcneill module_init(file);
673 1.33 joerg if (btinfo_modulelist) {
674 1.33 joerg mbm = alloc(sizeof(struct multiboot_module) *
675 1.33 joerg btinfo_modulelist->num);
676 1.33 joerg
677 1.33 joerg bim = (struct bi_modulelist_entry *)
678 1.33 joerg (((char *) btinfo_modulelist) +
679 1.33 joerg sizeof(struct btinfo_modulelist));
680 1.33 joerg for (i = 0; i < btinfo_modulelist->num; i++) {
681 1.33 joerg mbm[i].mmo_start = bim->base;
682 1.33 joerg mbm[i].mmo_end = bim->base + bim->len;
683 1.33 joerg mbm[i].mmo_string = (char *)vtophys(bim->path);
684 1.33 joerg mbm[i].mmo_reserved = 0;
685 1.33 joerg bim++;
686 1.33 joerg }
687 1.33 joerg mbi->mi_flags |= MULTIBOOT_INFO_HAS_MODS;
688 1.33 joerg mbi->mi_mods_count = btinfo_modulelist->num;
689 1.33 joerg mbi->mi_mods_addr = vtophys(mbm);
690 1.33 joerg }
691 1.33 joerg }
692 1.33 joerg
693 1.33 joerg #ifdef DEBUG
694 1.33 joerg printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", marks[MARK_ENTRY],
695 1.33 joerg marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]);
696 1.33 joerg #endif
697 1.33 joerg
698 1.33 joerg
699 1.33 joerg #if 0
700 1.33 joerg if (btinfo_symtab.nsym) {
701 1.33 joerg mbi->mi_flags |= MULTIBOOT_INFO_HAS_ELF_SYMS;
702 1.33 joerg mbi->mi_elfshdr_addr = marks[MARK_SYM];
703 1.33 joerg btinfo_symtab.nsym = marks[MARK_NSYM];
704 1.33 joerg btinfo_symtab.ssym = marks[MARK_SYM];
705 1.33 joerg btinfo_symtab.esym = marks[MARK_END];
706 1.33 joerg #endif
707 1.33 joerg
708 1.33 joerg multiboot(marks[MARK_ENTRY], vtophys(mbi),
709 1.33 joerg x86_trunc_page(mbi->mi_mem_lower*1024));
710 1.33 joerg panic("exec returned");
711 1.33 joerg
712 1.33 joerg out:
713 1.33 joerg dealloc(mbi, 0);
714 1.33 joerg return -1;
715 1.33 joerg }
716 1.40 ad
717 1.40 ad void
718 1.40 ad x86_progress(const char *fmt, ...)
719 1.40 ad {
720 1.40 ad va_list ap;
721 1.40 ad
722 1.40 ad if ((howto & AB_SILENT) != 0)
723 1.40 ad return;
724 1.40 ad va_start(ap, fmt);
725 1.40 ad vprintf(fmt, ap);
726 1.40 ad va_end(ap);
727 1.40 ad }
728