exec.c revision 1.38.2.3 1 /* $NetBSD: exec.c,v 1.38.2.3 2011/03/28 23:04:46 jym Exp $ */
2
3 /*-
4 * Copyright (c) 2008, 2009 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 #include <sys/reboot.h>
106
107 #include <machine/multiboot.h>
108 #include <machine/stdarg.h>
109
110 #include <lib/libsa/stand.h>
111 #include <lib/libkern/libkern.h>
112
113 #include "loadfile.h"
114 #include "libi386.h"
115 #include "bootinfo.h"
116 #include "bootmod.h"
117 #include "vbe.h"
118 #ifdef SUPPORT_PS2
119 #include "biosmca.h"
120 #endif
121
122 #define BOOT_NARGS 6
123
124 #ifndef PAGE_SIZE
125 #define PAGE_SIZE 4096
126 #endif
127
128 #define MODULE_WARNING_DELAY 5000000
129
130 extern struct btinfo_console btinfo_console;
131
132 boot_module_t *boot_modules;
133 bool boot_modules_enabled = true;
134 bool kernel_loaded;
135
136 static struct btinfo_framebuffer btinfo_framebuffer;
137
138 static struct btinfo_modulelist *btinfo_modulelist;
139 static size_t btinfo_modulelist_size;
140 static uint32_t image_end;
141 static char module_base[64] = "/";
142 static int howto;
143
144 static void module_init(const char *);
145 static void module_add_common(char *, uint8_t);
146
147 void
148 framebuffer_configure(struct btinfo_framebuffer *fb)
149 {
150 if (fb)
151 btinfo_framebuffer = *fb;
152 else {
153 btinfo_framebuffer.physaddr = 0;
154 btinfo_framebuffer.flags = 0;
155 }
156 }
157
158 void
159 module_add(char *name)
160 {
161 return module_add_common(name, BM_TYPE_KMOD);
162 }
163
164 void
165 splash_add(char *name)
166 {
167 return module_add_common(name, BM_TYPE_IMAGE);
168 }
169
170 static void
171 module_add_common(char *name, uint8_t type)
172 {
173 boot_module_t *bm, *bmp;
174 size_t len;
175 char *str;
176
177 while (*name == ' ' || *name == '\t')
178 ++name;
179
180 bm = alloc(sizeof(boot_module_t));
181 len = strlen(name) + 1;
182 str = alloc(len);
183 if (bm == NULL || str == NULL) {
184 printf("couldn't allocate module\n");
185 return;
186 }
187 memcpy(str, name, len);
188 bm->bm_path = str;
189 bm->bm_next = NULL;
190 bm->bm_type = type;
191 if (boot_modules == NULL)
192 boot_modules = bm;
193 else {
194 for (bmp = boot_modules; bmp->bm_next;
195 bmp = bmp->bm_next)
196 ;
197 bmp->bm_next = bm;
198 }
199 }
200
201 static int
202 common_load_kernel(const char *file, u_long *basemem, u_long *extmem,
203 physaddr_t loadaddr, int floppy, u_long marks[MARK_MAX])
204 {
205 int fd;
206 #ifdef XMS
207 u_long xmsmem;
208 physaddr_t origaddr = loadaddr;
209 #endif
210
211 *extmem = getextmem();
212 *basemem = getbasemem();
213
214 #ifdef XMS
215 if ((getextmem1() == 0) && (xmsmem = checkxms())) {
216 u_long kernsize;
217
218 /*
219 * With "CONSERVATIVE_MEMDETECT", extmem is 0 because
220 * getextmem() is getextmem1(). Without, the "smart"
221 * methods could fail to report all memory as well.
222 * xmsmem is a few kB less than the actual size, but
223 * better than nothing.
224 */
225 if (xmsmem > *extmem)
226 *extmem = xmsmem;
227 /*
228 * Get the size of the kernel
229 */
230 marks[MARK_START] = loadaddr;
231 if ((fd = loadfile(file, marks, COUNT_KERNEL)) == -1)
232 return EIO;
233 close(fd);
234
235 kernsize = marks[MARK_END];
236 kernsize = (kernsize + 1023) / 1024;
237
238 loadaddr = xmsalloc(kernsize);
239 if (!loadaddr)
240 return ENOMEM;
241 }
242 #endif
243 marks[MARK_START] = loadaddr;
244 if ((fd = loadfile(file, marks,
245 LOAD_KERNEL & ~(floppy ? LOAD_NOTE : 0))) == -1)
246 return EIO;
247
248 close(fd);
249
250 /* Now we know the root fs type, load modules for it. */
251 module_add(fsmod);
252 if (fsmod2 != NULL && strcmp(fsmod, fsmod2) != 0)
253 module_add(fsmod2);
254
255 /*
256 * Gather some information for the kernel. Do this after the
257 * "point of no return" to avoid memory leaks.
258 * (but before DOS might be trashed in the XMS case)
259 */
260 #ifdef PASS_BIOSGEOM
261 bi_getbiosgeom();
262 #endif
263 #ifdef PASS_MEMMAP
264 bi_getmemmap();
265 #endif
266
267 #ifdef XMS
268 if (loadaddr != origaddr) {
269 /*
270 * We now have done our last DOS IO, so we may
271 * trash the OS. Copy the data from the temporary
272 * buffer to its real address.
273 */
274 marks[MARK_START] -= loadaddr;
275 marks[MARK_END] -= loadaddr;
276 marks[MARK_SYM] -= loadaddr;
277 marks[MARK_END] -= loadaddr;
278 ppbcopy(loadaddr, origaddr, marks[MARK_END]);
279 }
280 #endif
281 marks[MARK_END] = (((u_long) marks[MARK_END] + sizeof(int) - 1)) &
282 (-sizeof(int));
283 image_end = marks[MARK_END];
284 kernel_loaded = true;
285
286 return 0;
287 }
288
289 int
290 exec_netbsd(const char *file, physaddr_t loadaddr, int boothowto, int floppy,
291 void (*callback)(void))
292 {
293 u_long boot_argv[BOOT_NARGS];
294 u_long marks[MARK_MAX];
295 struct btinfo_symtab btinfo_symtab;
296 u_long extmem;
297 u_long basemem;
298
299 #ifdef DEBUG
300 printf("exec: file=%s loadaddr=0x%lx\n",
301 file ? file : "NULL", loadaddr);
302 #endif
303
304 BI_ALLOC(32); /* ??? */
305
306 BI_ADD(&btinfo_console, BTINFO_CONSOLE, sizeof(struct btinfo_console));
307
308 howto = boothowto;
309
310 if (common_load_kernel(file, &basemem, &extmem, loadaddr, floppy, marks))
311 goto out;
312
313 boot_argv[0] = boothowto;
314 boot_argv[1] = 0;
315 boot_argv[2] = vtophys(bootinfo); /* old cyl offset */
316 boot_argv[3] = marks[MARK_END];
317 boot_argv[4] = extmem;
318 boot_argv[5] = basemem;
319
320 /* pull in any modules if necessary */
321 if (boot_modules_enabled) {
322 module_init(file);
323 if (btinfo_modulelist) {
324 BI_ADD(btinfo_modulelist, BTINFO_MODULELIST,
325 btinfo_modulelist_size);
326 }
327 }
328
329 #ifdef DEBUG
330 printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", marks[MARK_ENTRY],
331 marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]);
332 #endif
333
334 btinfo_symtab.nsym = marks[MARK_NSYM];
335 btinfo_symtab.ssym = marks[MARK_SYM];
336 btinfo_symtab.esym = marks[MARK_END];
337 BI_ADD(&btinfo_symtab, BTINFO_SYMTAB, sizeof(struct btinfo_symtab));
338
339 /* set new video mode if necessary */
340 vbe_commit();
341 BI_ADD(&btinfo_framebuffer, BTINFO_FRAMEBUFFER,
342 sizeof(struct btinfo_framebuffer));
343
344 if (callback != NULL)
345 (*callback)();
346 startprog(marks[MARK_ENTRY], BOOT_NARGS, boot_argv,
347 x86_trunc_page(basemem*1024));
348 panic("exec returned");
349
350 out:
351 BI_FREE();
352 bootinfo = 0;
353 return -1;
354 }
355
356 static void
357 extract_device(const char *path, char *buf, size_t buflen)
358 {
359 int i;
360
361 if (strchr(path, ':') != NULL) {
362 for (i = 0; i < buflen - 2 && path[i] != ':'; i++)
363 buf[i] = path[i];
364 buf[i++] = ':';
365 buf[i] = '\0';
366 } else
367 buf[0] = '\0';
368 }
369
370 static const char *
371 module_path(boot_module_t *bm, const char *kdev)
372 {
373 static char buf[256];
374 char name_buf[256], dev_buf[64];
375 const char *name, *name2, *p;
376
377 name = bm->bm_path;
378 for (name2 = name; *name2; ++name2) {
379 if (*name2 == ' ' || *name2 == '\t') {
380 strlcpy(name_buf, name, sizeof(name_buf));
381 if (name2 - name < sizeof(name_buf))
382 name_buf[name2 - name] = '\0';
383 name = name_buf;
384 break;
385 }
386 }
387 if ((p = strchr(name, ':')) != NULL) {
388 /* device specified, use it */
389 if (p[1] == '/')
390 snprintf(buf, sizeof(buf), "%s", name);
391 else {
392 p++;
393 extract_device(name, dev_buf, sizeof(dev_buf));
394 snprintf(buf, sizeof(buf), "%s%s/%s/%s.kmod",
395 dev_buf, module_base, p, p);
396 }
397 } else {
398 /* device not specified; load from kernel device if known */
399 if (name[0] == '/')
400 snprintf(buf, sizeof(buf), "%s%s", kdev, name);
401 else
402 snprintf(buf, sizeof(buf), "%s%s/%s/%s.kmod",
403 kdev, module_base, name, name);
404 }
405
406 return buf;
407 }
408
409 static int
410 module_open(boot_module_t *bm, int mode, const char *kdev, bool doload)
411 {
412 int fd;
413 const char *path;
414
415 /* check the expanded path first */
416 path = module_path(bm, kdev);
417 fd = open(path, mode);
418 if (fd != -1) {
419 if ((howto & AB_SILENT) == 0 && doload)
420 printf("Loading %s ", path);
421 } else {
422 /* now attempt the raw path provided */
423 fd = open(bm->bm_path, mode);
424 if (fd != -1 && (howto & AB_SILENT) == 0 && doload)
425 printf("Loading %s ", bm->bm_path);
426 }
427 if (!doload && fd == -1) {
428 printf("WARNING: couldn't open %s", bm->bm_path);
429 if (strcmp(bm->bm_path, path) != 0)
430 printf(" (%s)", path);
431 printf("\n");
432 }
433 return fd;
434 }
435
436 static void
437 module_init(const char *kernel_path)
438 {
439 struct bi_modulelist_entry *bi;
440 struct stat st;
441 const char *machine;
442 char kdev[64];
443 char *buf;
444 boot_module_t *bm;
445 size_t len;
446 off_t off;
447 int err, fd, nfail = 0;
448
449 extract_device(kernel_path, kdev, sizeof(kdev));
450
451 switch (netbsd_elf_class) {
452 case ELFCLASS32:
453 machine = "i386";
454 break;
455 case ELFCLASS64:
456 machine = "amd64";
457 break;
458 default:
459 machine = "generic";
460 break;
461 }
462 if (netbsd_version / 1000000 % 100 == 99) {
463 /* -current */
464 snprintf(module_base, sizeof(module_base),
465 "/stand/%s/%d.%d.%d/modules", machine,
466 netbsd_version / 100000000,
467 netbsd_version / 1000000 % 100,
468 netbsd_version / 100 % 100);
469 } else if (netbsd_version != 0) {
470 /* release */
471 snprintf(module_base, sizeof(module_base),
472 "/stand/%s/%d.%d/modules", machine,
473 netbsd_version / 100000000,
474 netbsd_version / 1000000 % 100);
475 }
476
477 /* First, see which modules are valid and calculate btinfo size */
478 len = sizeof(struct btinfo_modulelist);
479 for (bm = boot_modules; bm; bm = bm->bm_next) {
480 fd = module_open(bm, 0, kdev, false);
481 if (fd == -1) {
482 bm->bm_len = -1;
483 ++nfail;
484 continue;
485 }
486 err = fstat(fd, &st);
487 if (err == -1 || st.st_size == -1) {
488 printf("WARNING: couldn't stat %s\n", bm->bm_path);
489 close(fd);
490 bm->bm_len = -1;
491 ++nfail;
492 continue;
493 }
494 bm->bm_len = st.st_size;
495 close(fd);
496 len += sizeof(struct bi_modulelist_entry);
497 }
498
499 /* Allocate the module list */
500 btinfo_modulelist = alloc(len);
501 if (btinfo_modulelist == NULL) {
502 printf("WARNING: couldn't allocate module list\n");
503 delay(MODULE_WARNING_DELAY);
504 return;
505 }
506 memset(btinfo_modulelist, 0, len);
507 btinfo_modulelist_size = len;
508
509 /* Fill in btinfo structure */
510 buf = (char *)btinfo_modulelist;
511 btinfo_modulelist->num = 0;
512 off = sizeof(struct btinfo_modulelist);
513
514 for (bm = boot_modules; bm; bm = bm->bm_next) {
515 if (bm->bm_len == -1)
516 continue;
517 fd = module_open(bm, 0, kdev, true);
518 if (fd == -1)
519 continue;
520 image_end = (image_end + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
521 len = pread(fd, (void *)image_end, SSIZE_MAX);
522 if (len < bm->bm_len) {
523 if ((howto & AB_SILENT) != 0)
524 printf("Loading %s ", bm->bm_path);
525 printf(" FAILED\n");
526 } else {
527 btinfo_modulelist->num++;
528 bi = (struct bi_modulelist_entry *)(buf + off);
529 off += sizeof(struct bi_modulelist_entry);
530 strncpy(bi->path, bm->bm_path, sizeof(bi->path) - 1);
531 bi->base = image_end;
532 bi->len = len;
533 bi->type = bm->bm_type == BM_TYPE_KMOD ?
534 BI_MODULE_ELF : BI_MODULE_IMAGE;
535 if ((howto & AB_SILENT) == 0)
536 printf(" \n");
537 }
538 if (len > 0)
539 image_end += len;
540 close(fd);
541 }
542 btinfo_modulelist->endpa = image_end;
543
544 if (nfail > 0) {
545 printf("WARNING: %d module%s failed to load\n",
546 nfail, nfail == 1 ? "" : "s");
547 #if notyet
548 delay(MODULE_WARNING_DELAY);
549 #endif
550 }
551 }
552
553 int
554 exec_multiboot(const char *file, char *args)
555 {
556 struct multiboot_info *mbi;
557 struct multiboot_module *mbm;
558 struct bi_modulelist_entry *bim;
559 int i, len;
560 u_long marks[MARK_MAX];
561 u_long extmem;
562 u_long basemem;
563 char *cmdline;
564
565 mbi = alloc(sizeof(struct multiboot_info));
566 mbi->mi_flags = MULTIBOOT_INFO_HAS_MEMORY;
567
568 if (common_load_kernel(file, &basemem, &extmem, 0, 0, marks))
569 goto out;
570
571 mbi->mi_mem_upper = extmem;
572 mbi->mi_mem_lower = basemem;
573
574 if (args) {
575 mbi->mi_flags |= MULTIBOOT_INFO_HAS_CMDLINE;
576 len = strlen(file) + 1 + strlen(args) + 1;
577 cmdline = alloc(len);
578 snprintf(cmdline, len, "%s %s", file, args);
579 mbi->mi_cmdline = (char *) vtophys(cmdline);
580 }
581
582 /* pull in any modules if necessary */
583 if (boot_modules_enabled) {
584 module_init(file);
585 if (btinfo_modulelist) {
586 mbm = alloc(sizeof(struct multiboot_module) *
587 btinfo_modulelist->num);
588
589 bim = (struct bi_modulelist_entry *)
590 (((char *) btinfo_modulelist) +
591 sizeof(struct btinfo_modulelist));
592 for (i = 0; i < btinfo_modulelist->num; i++) {
593 mbm[i].mmo_start = bim->base;
594 mbm[i].mmo_end = bim->base + bim->len;
595 mbm[i].mmo_string = (char *)vtophys(bim->path);
596 mbm[i].mmo_reserved = 0;
597 bim++;
598 }
599 mbi->mi_flags |= MULTIBOOT_INFO_HAS_MODS;
600 mbi->mi_mods_count = btinfo_modulelist->num;
601 mbi->mi_mods_addr = vtophys(mbm);
602 }
603 }
604
605 #ifdef DEBUG
606 printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", marks[MARK_ENTRY],
607 marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]);
608 #endif
609
610
611 #if 0
612 if (btinfo_symtab.nsym) {
613 mbi->mi_flags |= MULTIBOOT_INFO_HAS_ELF_SYMS;
614 mbi->mi_elfshdr_addr = marks[MARK_SYM];
615 btinfo_symtab.nsym = marks[MARK_NSYM];
616 btinfo_symtab.ssym = marks[MARK_SYM];
617 btinfo_symtab.esym = marks[MARK_END];
618 #endif
619
620 multiboot(marks[MARK_ENTRY], vtophys(mbi),
621 x86_trunc_page(mbi->mi_mem_lower*1024));
622 panic("exec returned");
623
624 out:
625 dealloc(mbi, 0);
626 return -1;
627 }
628
629 void
630 x86_progress(const char *fmt, ...)
631 {
632 va_list ap;
633
634 if ((howto & AB_SILENT) != 0)
635 return;
636 va_start(ap, fmt);
637 vprintf(fmt, ap);
638 va_end(ap);
639 }
640