exec.c revision 1.39 1 /* $NetBSD: exec.c,v 1.39 2009/02/16 22:39:30 jmcneill 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_framebuffer btinfo_framebuffer;
132
133 static struct btinfo_modulelist *btinfo_modulelist;
134 static size_t btinfo_modulelist_size;
135 static uint32_t image_end;
136 static char module_base[64] = "/";
137
138 static void module_init(void);
139
140 void
141 framebuffer_configure(struct btinfo_framebuffer *fb)
142 {
143 if (fb)
144 btinfo_framebuffer = *fb;
145 else {
146 btinfo_framebuffer.physaddr = 0;
147 btinfo_framebuffer.flags = 0;
148 }
149 }
150
151 void
152 module_add(char *name)
153 {
154 boot_module_t *bm, *bmp;
155 size_t len;
156 char *str;
157
158 while (*name == ' ' || *name == '\t')
159 ++name;
160
161 bm = alloc(sizeof(boot_module_t));
162 len = strlen(name) + 1;
163 str = alloc(len);
164 if (bm == NULL || str == NULL) {
165 printf("couldn't allocate module\n");
166 return;
167 }
168 memcpy(str, name, len);
169 bm->bm_path = str;
170 bm->bm_next = NULL;
171 if (boot_modules == NULL)
172 boot_modules = bm;
173 else {
174 for (bmp = boot_modules; bmp->bm_next;
175 bmp = bmp->bm_next)
176 ;
177 bmp->bm_next = bm;
178 }
179 }
180
181 static int
182 common_load_kernel(const char *file, u_long *basemem, u_long *extmem,
183 physaddr_t loadaddr, int floppy, u_long marks[MARK_MAX])
184 {
185 int fd;
186 #ifdef XMS
187 u_long xmsmem;
188 physaddr_t origaddr = loadaddr;
189 #endif
190
191 *extmem = getextmem();
192 *basemem = getbasemem();
193
194 #ifdef XMS
195 if ((getextmem1() == 0) && (xmsmem = checkxms())) {
196 u_long kernsize;
197
198 /*
199 * With "CONSERVATIVE_MEMDETECT", extmem is 0 because
200 * getextmem() is getextmem1(). Without, the "smart"
201 * methods could fail to report all memory as well.
202 * xmsmem is a few kB less than the actual size, but
203 * better than nothing.
204 */
205 if (xmsmem > *extmem)
206 *extmem = xmsmem;
207 /*
208 * Get the size of the kernel
209 */
210 marks[MARK_START] = loadaddr;
211 if ((fd = loadfile(file, marks, COUNT_KERNEL)) == -1)
212 return EIO;
213 close(fd);
214
215 kernsize = marks[MARK_END];
216 kernsize = (kernsize + 1023) / 1024;
217
218 loadaddr = xmsalloc(kernsize);
219 if (!loadaddr)
220 return ENOMEM;
221 }
222 #endif
223 marks[MARK_START] = loadaddr;
224 if ((fd = loadfile(file, marks,
225 LOAD_KERNEL & ~(floppy ? LOAD_NOTE : 0))) == -1)
226 return EIO;
227
228 close(fd);
229
230 /* Now we know the root fs type, load modules for it. */
231 module_add(fsmod);
232 if (fsmod2 != NULL && strcmp(fsmod, fsmod2) != 0)
233 module_add(fsmod2);
234
235 /*
236 * Gather some information for the kernel. Do this after the
237 * "point of no return" to avoid memory leaks.
238 * (but before DOS might be trashed in the XMS case)
239 */
240 #ifdef PASS_BIOSGEOM
241 bi_getbiosgeom();
242 #endif
243 #ifdef PASS_MEMMAP
244 bi_getmemmap();
245 #endif
246
247 #ifdef XMS
248 if (loadaddr != origaddr) {
249 /*
250 * We now have done our last DOS IO, so we may
251 * trash the OS. Copy the data from the temporary
252 * buffer to its real address.
253 */
254 marks[MARK_START] -= loadaddr;
255 marks[MARK_END] -= loadaddr;
256 marks[MARK_SYM] -= loadaddr;
257 marks[MARK_END] -= loadaddr;
258 ppbcopy(loadaddr, origaddr, marks[MARK_END]);
259 }
260 #endif
261 marks[MARK_END] = (((u_long) marks[MARK_END] + sizeof(int) - 1)) &
262 (-sizeof(int));
263 image_end = marks[MARK_END];
264 kernel_loaded = true;
265
266 return 0;
267 }
268
269 int
270 exec_netbsd(const char *file, physaddr_t loadaddr, int boothowto, int floppy)
271 {
272 u_long boot_argv[BOOT_NARGS];
273 u_long marks[MARK_MAX];
274 struct btinfo_symtab btinfo_symtab;
275 u_long extmem;
276 u_long basemem;
277
278 #ifdef DEBUG
279 printf("exec: file=%s loadaddr=0x%lx\n",
280 file ? file : "NULL", loadaddr);
281 #endif
282
283 BI_ALLOC(32); /* ??? */
284
285 BI_ADD(&btinfo_console, BTINFO_CONSOLE, sizeof(struct btinfo_console));
286 BI_ADD(&btinfo_framebuffer, BTINFO_FRAMEBUFFER,
287 sizeof(struct btinfo_framebuffer));
288
289 if (common_load_kernel(file, &basemem, &extmem, loadaddr, floppy, marks))
290 goto out;
291
292 boot_argv[0] = boothowto;
293 boot_argv[1] = 0;
294 boot_argv[2] = vtophys(bootinfo); /* old cyl offset */
295 boot_argv[3] = marks[MARK_END];
296 boot_argv[4] = extmem;
297 boot_argv[5] = basemem;
298
299 /* pull in any modules if necessary */
300 if (boot_modules_enabled) {
301 module_init();
302 if (btinfo_modulelist) {
303 BI_ADD(btinfo_modulelist, BTINFO_MODULELIST,
304 btinfo_modulelist_size);
305 }
306 }
307
308 #ifdef DEBUG
309 printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", marks[MARK_ENTRY],
310 marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]);
311 #endif
312
313 btinfo_symtab.nsym = marks[MARK_NSYM];
314 btinfo_symtab.ssym = marks[MARK_SYM];
315 btinfo_symtab.esym = marks[MARK_END];
316 BI_ADD(&btinfo_symtab, BTINFO_SYMTAB, sizeof(struct btinfo_symtab));
317
318 startprog(marks[MARK_ENTRY], BOOT_NARGS, boot_argv,
319 x86_trunc_page(basemem*1024));
320 panic("exec returned");
321
322 out:
323 BI_FREE();
324 bootinfo = 0;
325 return -1;
326 }
327
328 static const char *
329 module_path(boot_module_t *bm)
330 {
331 static char buf[256];
332 char name_buf[256];
333 const char *name, *name2;
334
335 name = bm->bm_path;
336 for (name2 = name; *name2; ++name2) {
337 if (*name2 == ' ' || *name2 == '\t') {
338 strlcpy(name_buf, name, sizeof(name_buf));
339 if (name2 - name < sizeof(name_buf))
340 name_buf[name2 - name] = '\0';
341 name = name_buf;
342 break;
343 }
344 }
345 if (name[0] == '/')
346 snprintf(buf, sizeof(buf), "%s", name);
347 else
348 snprintf(buf, sizeof(buf), "%s/%s/%s.kmod",
349 module_base, name, name);
350
351 return buf;
352 }
353
354 static int
355 module_open(boot_module_t *bm, int mode)
356 {
357 int fd;
358 const char *path;
359
360 /* check the expanded path first */
361 path = module_path(bm);
362 fd = open(path, mode);
363 if (fd == -1) {
364 printf("WARNING: couldn't open %s\n", path);
365 /* now attempt the raw path provided */
366 fd = open(bm->bm_path, mode);
367 if (fd == -1)
368 printf("WARNING: couldn't open %s\n", bm->bm_path);
369 }
370 return fd;
371 }
372
373 static void
374 module_init(void)
375 {
376 struct bi_modulelist_entry *bi;
377 struct stat st;
378 const char *machine;
379 char *buf;
380 boot_module_t *bm;
381 size_t len;
382 off_t off;
383 int err, fd;
384
385 switch (netbsd_elf_class) {
386 case ELFCLASS32:
387 machine = "i386";
388 break;
389 case ELFCLASS64:
390 machine = "amd64";
391 break;
392 default:
393 machine = "generic";
394 break;
395 }
396 if (netbsd_version / 1000000 % 100 == 99) {
397 /* -current */
398 snprintf(module_base, sizeof(module_base),
399 "/stand/%s/%d.%d.%d/modules", machine,
400 netbsd_version / 100000000,
401 netbsd_version / 1000000 % 100,
402 netbsd_version / 100 % 100);
403 } else if (netbsd_version != 0) {
404 /* release */
405 snprintf(module_base, sizeof(module_base),
406 "/stand/%s/%d.%d/modules", machine,
407 netbsd_version / 100000000,
408 netbsd_version / 1000000 % 100);
409 }
410
411 /* First, see which modules are valid and calculate btinfo size */
412 len = sizeof(struct btinfo_modulelist);
413 for (bm = boot_modules; bm; bm = bm->bm_next) {
414 fd = module_open(bm, 0);
415 if (fd == -1) {
416 bm->bm_len = -1;
417 continue;
418 }
419 err = fstat(fd, &st);
420 if (err == -1 || st.st_size == -1) {
421 printf("WARNING: couldn't stat %s\n", bm->bm_path);
422 close(fd);
423 bm->bm_len = -1;
424 continue;
425 }
426 bm->bm_len = st.st_size;
427 close(fd);
428 len += sizeof(struct bi_modulelist_entry);
429 }
430
431 /* Allocate the module list */
432 btinfo_modulelist = alloc(len);
433 if (btinfo_modulelist == NULL) {
434 printf("WARNING: couldn't allocate module list\n");
435 return;
436 }
437 memset(btinfo_modulelist, 0, len);
438 btinfo_modulelist_size = len;
439
440 /* Fill in btinfo structure */
441 buf = (char *)btinfo_modulelist;
442 btinfo_modulelist->num = 0;
443 off = sizeof(struct btinfo_modulelist);
444
445 for (bm = boot_modules; bm; bm = bm->bm_next) {
446 if (bm->bm_len == -1)
447 continue;
448 printf("Loading %s ", bm->bm_path);
449 fd = module_open(bm, 0);
450 if (fd == -1) {
451 printf("ERROR: couldn't open %s\n", bm->bm_path);
452 continue;
453 }
454 image_end = (image_end + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
455 len = pread(fd, (void *)image_end, SSIZE_MAX);
456 if (len < bm->bm_len) {
457 printf(" FAILED\n");
458 } else {
459 btinfo_modulelist->num++;
460 bi = (struct bi_modulelist_entry *)(buf + off);
461 off += sizeof(struct bi_modulelist_entry);
462 strncpy(bi->path, bm->bm_path, sizeof(bi->path) - 1);
463 bi->base = image_end;
464 bi->len = len;
465 bi->type = BI_MODULE_ELF;
466 printf(" \n");
467 }
468 if (len > 0)
469 image_end += len;
470 close(fd);
471 }
472 btinfo_modulelist->endpa = image_end;
473 }
474
475 int
476 exec_multiboot(const char *file, char *args)
477 {
478 struct multiboot_info *mbi;
479 struct multiboot_module *mbm;
480 struct bi_modulelist_entry *bim;
481 int i, len;
482 u_long marks[MARK_MAX];
483 u_long extmem;
484 u_long basemem;
485 char *cmdline;
486
487 mbi = alloc(sizeof(struct multiboot_info));
488 mbi->mi_flags = MULTIBOOT_INFO_HAS_MEMORY;
489
490 if (common_load_kernel(file, &basemem, &extmem, 0, 0, marks))
491 goto out;
492
493 mbi->mi_mem_upper = extmem;
494 mbi->mi_mem_lower = basemem;
495
496 if (args) {
497 mbi->mi_flags |= MULTIBOOT_INFO_HAS_CMDLINE;
498 len = strlen(file) + 1 + strlen(args) + 1;
499 cmdline = alloc(len);
500 snprintf(cmdline, len, "%s %s", file, args);
501 mbi->mi_cmdline = (char *) vtophys(cmdline);
502 }
503
504 /* pull in any modules if necessary */
505 if (boot_modules_enabled) {
506 module_init();
507 if (btinfo_modulelist) {
508 mbm = alloc(sizeof(struct multiboot_module) *
509 btinfo_modulelist->num);
510
511 bim = (struct bi_modulelist_entry *)
512 (((char *) btinfo_modulelist) +
513 sizeof(struct btinfo_modulelist));
514 for (i = 0; i < btinfo_modulelist->num; i++) {
515 mbm[i].mmo_start = bim->base;
516 mbm[i].mmo_end = bim->base + bim->len;
517 mbm[i].mmo_string = (char *)vtophys(bim->path);
518 mbm[i].mmo_reserved = 0;
519 bim++;
520 }
521 mbi->mi_flags |= MULTIBOOT_INFO_HAS_MODS;
522 mbi->mi_mods_count = btinfo_modulelist->num;
523 mbi->mi_mods_addr = vtophys(mbm);
524 }
525 }
526
527 #ifdef DEBUG
528 printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", marks[MARK_ENTRY],
529 marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]);
530 #endif
531
532
533 #if 0
534 if (btinfo_symtab.nsym) {
535 mbi->mi_flags |= MULTIBOOT_INFO_HAS_ELF_SYMS;
536 mbi->mi_elfshdr_addr = marks[MARK_SYM];
537 btinfo_symtab.nsym = marks[MARK_NSYM];
538 btinfo_symtab.ssym = marks[MARK_SYM];
539 btinfo_symtab.esym = marks[MARK_END];
540 #endif
541
542 multiboot(marks[MARK_ENTRY], vtophys(mbi),
543 x86_trunc_page(mbi->mi_mem_lower*1024));
544 panic("exec returned");
545
546 out:
547 dealloc(mbi, 0);
548 return -1;
549 }
550