exec.c revision 1.33 1 /* $NetBSD: exec.c,v 1.33 2008/10/11 11:06:20 joerg Exp $ */
2
3 /*-
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * Copyright (c) 1982, 1986, 1990, 1993
31 * The Regents of the University of California. All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
56 *
57 * @(#)boot.c 8.1 (Berkeley) 6/10/93
58 */
59
60 /*
61 * Copyright (c) 1996
62 * Matthias Drochner. All rights reserved.
63 * Copyright (c) 1996
64 * Perry E. Metzger. All rights reserved.
65 *
66 * Redistribution and use in source and binary forms, with or without
67 * modification, are permitted provided that the following conditions
68 * are met:
69 * 1. Redistributions of source code must retain the above copyright
70 * notice, this list of conditions and the following disclaimer.
71 * 2. Redistributions in binary form must reproduce the above copyright
72 * notice, this list of conditions and the following disclaimer in the
73 * documentation and/or other materials provided with the distribution.
74 * 3. All advertising materials mentioning features or use of this software
75 * must display the following acknowledgement:
76 * This product includes software developed by the University of
77 * California, Berkeley and its contributors.
78 * 4. Neither the name of the University nor the names of its contributors
79 * may be used to endorse or promote products derived from this software
80 * without specific prior written permission.
81 *
82 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
83 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
84 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
85 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
86 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
87 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
88 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
89 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
90 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
91 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
92 * SUCH DAMAGE.
93 *
94 * @(#)boot.c 8.1 (Berkeley) 6/10/93
95 */
96
97 /*
98 * starts NetBSD a.out kernel
99 * needs lowlevel startup from startprog.S
100 * This is a special version of exec.c to support use of XMS.
101 */
102
103 #include <sys/param.h>
104 #include <sys/reboot.h>
105
106 #include <machine/multiboot.h>
107
108 #include <lib/libsa/stand.h>
109 #include <lib/libkern/libkern.h>
110
111 #include "loadfile.h"
112 #include "libi386.h"
113 #include "bootinfo.h"
114 #include "bootmod.h"
115 #ifdef SUPPORT_PS2
116 #include "biosmca.h"
117 #endif
118
119 #define BOOT_NARGS 6
120
121 #ifndef PAGE_SIZE
122 #define PAGE_SIZE 4096
123 #endif
124
125 extern struct btinfo_console btinfo_console;
126
127 boot_module_t *boot_modules;
128 bool boot_modules_enabled = true;
129 bool kernel_loaded;
130
131 static struct btinfo_modulelist *btinfo_modulelist;
132 static size_t btinfo_modulelist_size;
133 static uint32_t image_end;
134 static char module_base[64] = "/";
135
136 static void module_init(void);
137
138 static int
139 common_load_kernel(const char *file, u_long *basemem, u_long *extmem,
140 physaddr_t loadaddr, int floppy, u_long marks[MARK_MAX])
141 {
142 int fd;
143 #ifdef XMS
144 u_long xmsmem;
145 physaddr_t origaddr = loadaddr;
146 #endif
147
148 *extmem = getextmem();
149 *basemem = getbasemem();
150
151 #ifdef XMS
152 if ((getextmem1() == 0) && (xmsmem = checkxms())) {
153 u_long kernsize;
154
155 /*
156 * With "CONSERVATIVE_MEMDETECT", extmem is 0 because
157 * getextmem() is getextmem1(). Without, the "smart"
158 * methods could fail to report all memory as well.
159 * xmsmem is a few kB less than the actual size, but
160 * better than nothing.
161 */
162 if (xmsmem > *extmem)
163 *extmem = xmsmem;
164 /*
165 * Get the size of the kernel
166 */
167 marks[MARK_START] = loadaddr;
168 if ((fd = loadfile(file, marks, COUNT_KERNEL)) == -1)
169 return EIO;
170 close(fd);
171
172 kernsize = marks[MARK_END];
173 kernsize = (kernsize + 1023) / 1024;
174
175 loadaddr = xmsalloc(kernsize);
176 if (!loadaddr)
177 return ENOMEM;
178 }
179 #endif
180 marks[MARK_START] = loadaddr;
181 if ((fd = loadfile(file, marks,
182 LOAD_KERNEL & ~(floppy ? LOAD_NOTE : 0))) == -1)
183 return EIO;
184
185 close(fd);
186
187 /*
188 * Gather some information for the kernel. Do this after the
189 * "point of no return" to avoid memory leaks.
190 * (but before DOS might be trashed in the XMS case)
191 */
192 #ifdef PASS_BIOSGEOM
193 bi_getbiosgeom();
194 #endif
195 #ifdef PASS_MEMMAP
196 bi_getmemmap();
197 #endif
198
199 #ifdef XMS
200 if (loadaddr != origaddr) {
201 /*
202 * We now have done our last DOS IO, so we may
203 * trash the OS. Copy the data from the temporary
204 * buffer to its real address.
205 */
206 marks[MARK_START] -= loadaddr;
207 marks[MARK_END] -= loadaddr;
208 marks[MARK_SYM] -= loadaddr;
209 marks[MARK_END] -= loadaddr;
210 ppbcopy(loadaddr, origaddr, marks[MARK_END]);
211 }
212 #endif
213 marks[MARK_END] = (((u_long) marks[MARK_END] + sizeof(int) - 1)) &
214 (-sizeof(int));
215 image_end = marks[MARK_END];
216 kernel_loaded = true;
217
218 return 0;
219 }
220
221 int
222 exec_netbsd(const char *file, physaddr_t loadaddr, int boothowto, int floppy)
223 {
224 u_long boot_argv[BOOT_NARGS];
225 u_long marks[MARK_MAX];
226 struct btinfo_symtab btinfo_symtab;
227 u_long extmem;
228 u_long basemem;
229
230 #ifdef DEBUG
231 printf("exec: file=%s loadaddr=0x%lx\n",
232 file ? file : "NULL", loadaddr);
233 #endif
234
235 BI_ALLOC(32); /* ??? */
236
237 BI_ADD(&btinfo_console, BTINFO_CONSOLE, sizeof(struct btinfo_console));
238
239 if (common_load_kernel(file, &basemem, &extmem, loadaddr, floppy, marks))
240 goto out;
241
242 boot_argv[0] = boothowto;
243 boot_argv[1] = 0;
244 boot_argv[2] = vtophys(bootinfo); /* old cyl offset */
245 boot_argv[3] = marks[MARK_END];
246 boot_argv[4] = extmem;
247 boot_argv[5] = basemem;
248
249 /* pull in any modules if necessary */
250 if (boot_modules_enabled) {
251 module_init();
252 if (btinfo_modulelist) {
253 BI_ADD(btinfo_modulelist, BTINFO_MODULELIST,
254 btinfo_modulelist_size);
255 }
256 }
257
258 #ifdef DEBUG
259 printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", marks[MARK_ENTRY],
260 marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]);
261 #endif
262
263 btinfo_symtab.nsym = marks[MARK_NSYM];
264 btinfo_symtab.ssym = marks[MARK_SYM];
265 btinfo_symtab.esym = marks[MARK_END];
266 BI_ADD(&btinfo_symtab, BTINFO_SYMTAB, sizeof(struct btinfo_symtab));
267
268 startprog(marks[MARK_ENTRY], BOOT_NARGS, boot_argv,
269 x86_trunc_page(basemem*1024));
270 panic("exec returned");
271
272 out:
273 BI_FREE();
274 bootinfo = 0;
275 return -1;
276 }
277
278 static const char *
279 module_path(boot_module_t *bm)
280 {
281 static char buf[256];
282 char name_buf[256];
283 const char *name, *name2;
284
285 name = bm->bm_path;
286 for (name2 = name; *name2; ++name2) {
287 if (*name2 == ' ' || *name2 == '\t') {
288 strlcpy(name_buf, name, sizeof(name_buf));
289 if (name2 - name < sizeof(name_buf))
290 name_buf[name2 - name] = '\0';
291 name = name_buf;
292 break;
293 }
294 }
295 if (name[0] == '/')
296 snprintf(buf, sizeof(buf), "%s", name);
297 else
298 snprintf(buf, sizeof(buf), "%s/%s/%s.kmod",
299 module_base, name, name);
300
301 return buf;
302 }
303
304 static int
305 module_open(boot_module_t *bm, int mode)
306 {
307 int fd;
308 const char *path;
309
310 /* check the expanded path first */
311 path = module_path(bm);
312 fd = open(path, mode);
313 if (fd == -1) {
314 printf("WARNING: couldn't open %s\n", path);
315 /* now attempt the raw path provided */
316 fd = open(bm->bm_path, mode);
317 if (fd == -1)
318 printf("WARNING: couldn't open %s\n", bm->bm_path);
319 }
320 return fd;
321 }
322
323 static void
324 module_init(void)
325 {
326 struct bi_modulelist_entry *bi;
327 struct stat st;
328 const char *machine;
329 char *buf;
330 boot_module_t *bm;
331 size_t len;
332 off_t off;
333 int err, fd;
334
335 switch (netbsd_elf_class) {
336 case ELFCLASS32:
337 machine = "i386";
338 break;
339 case ELFCLASS64:
340 machine = "amd64";
341 break;
342 default:
343 machine = "generic";
344 break;
345 }
346 if (netbsd_version / 1000000 % 100 == 99) {
347 /* -current */
348 snprintf(module_base, sizeof(module_base),
349 "/stand/%s/%d.%d.%d/modules", machine,
350 netbsd_version / 100000000,
351 netbsd_version / 1000000 % 100,
352 netbsd_version / 100 % 100);
353 } else if (netbsd_version != 0) {
354 /* release */
355 snprintf(module_base, sizeof(module_base),
356 "/stand/%s/%d.%d/modules", machine,
357 netbsd_version / 100000000,
358 netbsd_version / 1000000 % 100);
359 }
360
361 /* First, see which modules are valid and calculate btinfo size */
362 len = sizeof(struct btinfo_modulelist);
363 for (bm = boot_modules; bm; bm = bm->bm_next) {
364 fd = module_open(bm, 0);
365 if (fd == -1) {
366 bm->bm_len = -1;
367 continue;
368 }
369 err = fstat(fd, &st);
370 if (err == -1 || st.st_size == -1) {
371 printf("WARNING: couldn't stat %s\n", bm->bm_path);
372 close(fd);
373 bm->bm_len = -1;
374 continue;
375 }
376 bm->bm_len = st.st_size;
377 close(fd);
378 len += sizeof(struct bi_modulelist_entry);
379 }
380
381 /* Allocate the module list */
382 btinfo_modulelist = alloc(len);
383 if (btinfo_modulelist == NULL) {
384 printf("WARNING: couldn't allocate module list\n");
385 return;
386 }
387 memset(btinfo_modulelist, 0, len);
388 btinfo_modulelist_size = len;
389
390 /* Fill in btinfo structure */
391 buf = (char *)btinfo_modulelist;
392 btinfo_modulelist->num = 0;
393 off = sizeof(struct btinfo_modulelist);
394
395 for (bm = boot_modules; bm; bm = bm->bm_next) {
396 if (bm->bm_len == -1)
397 continue;
398 printf("Loading %s ", bm->bm_path);
399 fd = module_open(bm, 0);
400 if (fd == -1) {
401 printf("ERROR: couldn't open %s\n", bm->bm_path);
402 continue;
403 }
404 image_end = (image_end + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
405 len = pread(fd, (void *)image_end, SSIZE_MAX);
406 if (len < bm->bm_len) {
407 printf(" FAILED\n");
408 } else {
409 btinfo_modulelist->num++;
410 bi = (struct bi_modulelist_entry *)(buf + off);
411 off += sizeof(struct bi_modulelist_entry);
412 strncpy(bi->path, bm->bm_path, sizeof(bi->path) - 1);
413 bi->base = image_end;
414 bi->len = len;
415 bi->type = BI_MODULE_ELF;
416 printf(" \n");
417 }
418 if (len > 0)
419 image_end += len;
420 close(fd);
421 }
422 btinfo_modulelist->endpa = image_end;
423 }
424
425 int
426 exec_multiboot(const char *file, char *args)
427 {
428 struct multiboot_info *mbi;
429 struct multiboot_module *mbm;
430 struct bi_modulelist_entry *bim;
431 int i, len;
432 u_long marks[MARK_MAX];
433 u_long extmem;
434 u_long basemem;
435 char *cmdline;
436
437 mbi = alloc(sizeof(struct multiboot_info));
438 mbi->mi_flags = MULTIBOOT_INFO_HAS_MEMORY;
439
440 if (common_load_kernel(file, &basemem, &extmem, 0, 0, marks))
441 goto out;
442
443 mbi->mi_mem_upper = extmem;
444 mbi->mi_mem_lower = basemem;
445
446 if (args) {
447 mbi->mi_flags |= MULTIBOOT_INFO_HAS_CMDLINE;
448 len = strlen(file) + 1 + strlen(args) + 1;
449 cmdline = alloc(len);
450 snprintf(cmdline, len, "%s %s", file, args);
451 mbi->mi_cmdline = (char *) vtophys(cmdline);
452 }
453
454 /* pull in any modules if necessary */
455 if (boot_modules_enabled) {
456 module_init();
457 if (btinfo_modulelist) {
458 mbm = alloc(sizeof(struct multiboot_module) *
459 btinfo_modulelist->num);
460
461 bim = (struct bi_modulelist_entry *)
462 (((char *) btinfo_modulelist) +
463 sizeof(struct btinfo_modulelist));
464 for (i = 0; i < btinfo_modulelist->num; i++) {
465 mbm[i].mmo_start = bim->base;
466 mbm[i].mmo_end = bim->base + bim->len;
467 mbm[i].mmo_string = (char *)vtophys(bim->path);
468 mbm[i].mmo_reserved = 0;
469 bim++;
470 }
471 mbi->mi_flags |= MULTIBOOT_INFO_HAS_MODS;
472 mbi->mi_mods_count = btinfo_modulelist->num;
473 mbi->mi_mods_addr = vtophys(mbm);
474 }
475 }
476
477 #ifdef DEBUG
478 printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", marks[MARK_ENTRY],
479 marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]);
480 #endif
481
482
483 #if 0
484 if (btinfo_symtab.nsym) {
485 mbi->mi_flags |= MULTIBOOT_INFO_HAS_ELF_SYMS;
486 mbi->mi_elfshdr_addr = marks[MARK_SYM];
487 btinfo_symtab.nsym = marks[MARK_NSYM];
488 btinfo_symtab.ssym = marks[MARK_SYM];
489 btinfo_symtab.esym = marks[MARK_END];
490 #endif
491
492 multiboot(marks[MARK_ENTRY], vtophys(mbi),
493 x86_trunc_page(mbi->mi_mem_lower*1024));
494 panic("exec returned");
495
496 out:
497 dealloc(mbi, 0);
498 return -1;
499 }
500