exec.c revision 1.42.2.3 1 /* $NetBSD: exec.c,v 1.42.2.3 2010/11/06 08:08:19 uebayasi 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 *
75 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
76 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
77 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
78 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
79 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
80 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
81 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
82 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
83 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
84 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
85 * SUCH DAMAGE.
86 *
87 * @(#)boot.c 8.1 (Berkeley) 6/10/93
88 */
89
90 /*
91 * starts NetBSD a.out kernel
92 * needs lowlevel startup from startprog.S
93 * This is a special version of exec.c to support use of XMS.
94 */
95
96 #include <sys/param.h>
97 #include <sys/reboot.h>
98 #include <sys/reboot.h>
99
100 #include <machine/multiboot.h>
101 #include <machine/stdarg.h>
102
103 #include <lib/libsa/stand.h>
104 #include <lib/libkern/libkern.h>
105
106 #include "loadfile.h"
107 #include "libi386.h"
108 #include "bootinfo.h"
109 #include "bootmod.h"
110 #include "vbe.h"
111 #ifdef SUPPORT_PS2
112 #include "biosmca.h"
113 #endif
114
115 #define BOOT_NARGS 6
116
117 #ifndef PAGE_SIZE
118 #define PAGE_SIZE 4096
119 #endif
120
121 #define MODULE_WARNING_SEC 5
122
123 extern struct btinfo_console btinfo_console;
124
125 boot_module_t *boot_modules;
126 bool boot_modules_enabled = true;
127 bool kernel_loaded;
128
129 static struct btinfo_framebuffer btinfo_framebuffer;
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 static int howto;
136
137 static void module_init(const char *);
138
139 void
140 framebuffer_configure(struct btinfo_framebuffer *fb)
141 {
142 if (fb)
143 btinfo_framebuffer = *fb;
144 else {
145 btinfo_framebuffer.physaddr = 0;
146 btinfo_framebuffer.flags = 0;
147 }
148 }
149
150 void
151 module_add(char *name)
152 {
153 boot_module_t *bm, *bmp;
154 size_t len;
155 char *str;
156
157 while (*name == ' ' || *name == '\t')
158 ++name;
159
160 bm = alloc(sizeof(boot_module_t));
161 len = strlen(name) + 1;
162 str = alloc(len);
163 if (bm == NULL || str == NULL) {
164 printf("couldn't allocate module\n");
165 return;
166 }
167 memcpy(str, name, len);
168 bm->bm_path = str;
169 bm->bm_next = NULL;
170 if (boot_modules == NULL)
171 boot_modules = bm;
172 else {
173 for (bmp = boot_modules; bmp->bm_next;
174 bmp = bmp->bm_next)
175 ;
176 bmp->bm_next = bm;
177 }
178 }
179
180 static int
181 common_load_kernel(const char *file, u_long *basemem, u_long *extmem,
182 physaddr_t loadaddr, int floppy, u_long marks[MARK_MAX])
183 {
184 int fd;
185 #ifdef XMS
186 u_long xmsmem;
187 physaddr_t origaddr = loadaddr;
188 #endif
189
190 *extmem = getextmem();
191 *basemem = getbasemem();
192
193 #ifdef XMS
194 if ((getextmem1() == 0) && (xmsmem = checkxms())) {
195 u_long kernsize;
196
197 /*
198 * With "CONSERVATIVE_MEMDETECT", extmem is 0 because
199 * getextmem() is getextmem1(). Without, the "smart"
200 * methods could fail to report all memory as well.
201 * xmsmem is a few kB less than the actual size, but
202 * better than nothing.
203 */
204 if (xmsmem > *extmem)
205 *extmem = xmsmem;
206 /*
207 * Get the size of the kernel
208 */
209 marks[MARK_START] = loadaddr;
210 if ((fd = loadfile(file, marks, COUNT_KERNEL)) == -1)
211 return EIO;
212 close(fd);
213
214 kernsize = marks[MARK_END];
215 kernsize = (kernsize + 1023) / 1024;
216
217 loadaddr = xmsalloc(kernsize);
218 if (!loadaddr)
219 return ENOMEM;
220 }
221 #endif
222 marks[MARK_START] = loadaddr;
223 if ((fd = loadfile(file, marks,
224 LOAD_KERNEL & ~(floppy ? LOAD_BACKWARDS : 0))) == -1)
225 return EIO;
226
227 close(fd);
228
229 /* Now we know the root fs type, load modules for it. */
230 module_add(fsmod);
231 if (fsmod2 != NULL && strcmp(fsmod, fsmod2) != 0)
232 module_add(fsmod2);
233
234 /*
235 * Gather some information for the kernel. Do this after the
236 * "point of no return" to avoid memory leaks.
237 * (but before DOS might be trashed in the XMS case)
238 */
239 #ifdef PASS_BIOSGEOM
240 bi_getbiosgeom();
241 #endif
242 #ifdef PASS_MEMMAP
243 bi_getmemmap();
244 #endif
245
246 #ifdef XMS
247 if (loadaddr != origaddr) {
248 /*
249 * We now have done our last DOS IO, so we may
250 * trash the OS. Copy the data from the temporary
251 * buffer to its real address.
252 */
253 marks[MARK_START] -= loadaddr;
254 marks[MARK_END] -= loadaddr;
255 marks[MARK_SYM] -= loadaddr;
256 marks[MARK_END] -= loadaddr;
257 ppbcopy(loadaddr, origaddr, marks[MARK_END]);
258 }
259 #endif
260 marks[MARK_END] = (((u_long) marks[MARK_END] + sizeof(int) - 1)) &
261 (-sizeof(int));
262 image_end = marks[MARK_END];
263 kernel_loaded = true;
264
265 return 0;
266 }
267
268 int
269 exec_netbsd(const char *file, physaddr_t loadaddr, int boothowto, int floppy,
270 void (*callback)(void))
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
287 howto = boothowto;
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(file);
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 /* set new video mode if necessary */
319 vbe_commit();
320 BI_ADD(&btinfo_framebuffer, BTINFO_FRAMEBUFFER,
321 sizeof(struct btinfo_framebuffer));
322
323 if (callback != NULL)
324 (*callback)();
325 startprog(marks[MARK_ENTRY], BOOT_NARGS, boot_argv,
326 x86_trunc_page(basemem*1024));
327 panic("exec returned");
328
329 out:
330 BI_FREE();
331 bootinfo = 0;
332 return -1;
333 }
334
335 static void
336 extract_device(const char *path, char *buf, size_t buflen)
337 {
338 int i;
339
340 if (strchr(path, ':') != NULL) {
341 for (i = 0; i < buflen - 2 && path[i] != ':'; i++)
342 buf[i] = path[i];
343 buf[i++] = ':';
344 buf[i] = '\0';
345 } else
346 buf[0] = '\0';
347 }
348
349 static const char *
350 module_path(boot_module_t *bm, const char *kdev)
351 {
352 static char buf[256];
353 char name_buf[256], dev_buf[64];
354 const char *name, *name2, *p;
355
356 name = bm->bm_path;
357 for (name2 = name; *name2; ++name2) {
358 if (*name2 == ' ' || *name2 == '\t') {
359 strlcpy(name_buf, name, sizeof(name_buf));
360 if (name2 - name < sizeof(name_buf))
361 name_buf[name2 - name] = '\0';
362 name = name_buf;
363 break;
364 }
365 }
366 if ((p = strchr(name, ':')) != NULL) {
367 /* device specified, use it */
368 if (p[1] == '/')
369 snprintf(buf, sizeof(buf), "%s", name);
370 else {
371 p++;
372 extract_device(name, dev_buf, sizeof(dev_buf));
373 snprintf(buf, sizeof(buf), "%s%s/%s/%s.kmod",
374 dev_buf, module_base, p, p);
375 }
376 } else {
377 /* device not specified; load from kernel device if known */
378 if (name[0] == '/')
379 snprintf(buf, sizeof(buf), "%s%s", kdev, name);
380 else
381 snprintf(buf, sizeof(buf), "%s%s/%s/%s.kmod",
382 kdev, module_base, name, name);
383 }
384
385 return buf;
386 }
387
388 static int
389 module_open(boot_module_t *bm, int mode, const char *kdev, bool doload)
390 {
391 int fd;
392 const char *path;
393
394 /* check the expanded path first */
395 path = module_path(bm, kdev);
396 fd = open(path, mode);
397 if (fd != -1) {
398 if ((howto & AB_SILENT) == 0 && doload)
399 printf("Loading %s ", path);
400 } else {
401 /* now attempt the raw path provided */
402 fd = open(bm->bm_path, mode);
403 if (fd != -1 && (howto & AB_SILENT) == 0 && doload)
404 printf("Loading %s ", bm->bm_path);
405 }
406 if (!doload && fd == -1) {
407 printf("WARNING: couldn't open %s", bm->bm_path);
408 if (strcmp(bm->bm_path, path) != 0)
409 printf(" (%s)", path);
410 printf("\n");
411 }
412 return fd;
413 }
414
415 static void
416 module_init(const char *kernel_path)
417 {
418 struct bi_modulelist_entry *bi;
419 struct stat st;
420 const char *machine;
421 char kdev[64];
422 char *buf;
423 boot_module_t *bm;
424 size_t len;
425 off_t off;
426 int err, fd, nfail = 0;
427
428 extract_device(kernel_path, kdev, sizeof(kdev));
429
430 switch (netbsd_elf_class) {
431 case ELFCLASS32:
432 machine = "i386";
433 break;
434 case ELFCLASS64:
435 machine = "amd64";
436 break;
437 default:
438 machine = "generic";
439 break;
440 }
441 if (netbsd_version / 1000000 % 100 == 99) {
442 /* -current */
443 snprintf(module_base, sizeof(module_base),
444 "/stand/%s/%d.%d.%d/modules", machine,
445 netbsd_version / 100000000,
446 netbsd_version / 1000000 % 100,
447 netbsd_version / 100 % 100);
448 } else if (netbsd_version != 0) {
449 /* release */
450 snprintf(module_base, sizeof(module_base),
451 "/stand/%s/%d.%d/modules", machine,
452 netbsd_version / 100000000,
453 netbsd_version / 1000000 % 100);
454 }
455
456 /* First, see which modules are valid and calculate btinfo size */
457 len = sizeof(struct btinfo_modulelist);
458 for (bm = boot_modules; bm; bm = bm->bm_next) {
459 fd = module_open(bm, 0, kdev, false);
460 if (fd == -1) {
461 bm->bm_len = -1;
462 ++nfail;
463 continue;
464 }
465 err = fstat(fd, &st);
466 if (err == -1 || st.st_size == -1) {
467 printf("WARNING: couldn't stat %s\n", bm->bm_path);
468 close(fd);
469 bm->bm_len = -1;
470 ++nfail;
471 continue;
472 }
473 bm->bm_len = st.st_size;
474 close(fd);
475 len += sizeof(struct bi_modulelist_entry);
476 }
477
478 /* Allocate the module list */
479 btinfo_modulelist = alloc(len);
480 if (btinfo_modulelist == NULL) {
481 printf("WARNING: couldn't allocate module list\n");
482 wait_sec(MODULE_WARNING_SEC);
483 return;
484 }
485 memset(btinfo_modulelist, 0, len);
486 btinfo_modulelist_size = len;
487
488 /* Fill in btinfo structure */
489 buf = (char *)btinfo_modulelist;
490 btinfo_modulelist->num = 0;
491 off = sizeof(struct btinfo_modulelist);
492
493 for (bm = boot_modules; bm; bm = bm->bm_next) {
494 if (bm->bm_len == -1)
495 continue;
496 fd = module_open(bm, 0, kdev, true);
497 if (fd == -1)
498 continue;
499 image_end = (image_end + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
500 len = pread(fd, (void *)image_end, SSIZE_MAX);
501 if (len < bm->bm_len) {
502 if ((howto & AB_SILENT) != 0)
503 printf("Loading %s ", bm->bm_path);
504 printf(" FAILED\n");
505 } else {
506 btinfo_modulelist->num++;
507 bi = (struct bi_modulelist_entry *)(buf + off);
508 off += sizeof(struct bi_modulelist_entry);
509 strncpy(bi->path, bm->bm_path, sizeof(bi->path) - 1);
510 bi->base = image_end;
511 bi->len = len;
512 bi->type = BI_MODULE_ELF;
513 if ((howto & AB_SILENT) == 0)
514 printf(" \n");
515 }
516 if (len > 0)
517 image_end += len;
518 close(fd);
519 }
520 btinfo_modulelist->endpa = image_end;
521
522 if (nfail > 0) {
523 printf("WARNING: %d module%s failed to load\n",
524 nfail, nfail == 1 ? "" : "s");
525 #if notyet
526 wait_sec(MODULE_WARNING_SEC);
527 #endif
528 }
529 }
530
531 int
532 exec_multiboot(const char *file, char *args)
533 {
534 struct multiboot_info *mbi;
535 struct multiboot_module *mbm;
536 struct bi_modulelist_entry *bim;
537 int i, len;
538 u_long marks[MARK_MAX];
539 u_long extmem;
540 u_long basemem;
541 char *cmdline;
542
543 mbi = alloc(sizeof(struct multiboot_info));
544 mbi->mi_flags = MULTIBOOT_INFO_HAS_MEMORY;
545
546 if (common_load_kernel(file, &basemem, &extmem, 0, 0, marks))
547 goto out;
548
549 mbi->mi_mem_upper = extmem;
550 mbi->mi_mem_lower = basemem;
551
552 if (args) {
553 mbi->mi_flags |= MULTIBOOT_INFO_HAS_CMDLINE;
554 len = strlen(file) + 1 + strlen(args) + 1;
555 cmdline = alloc(len);
556 snprintf(cmdline, len, "%s %s", file, args);
557 mbi->mi_cmdline = (char *) vtophys(cmdline);
558 }
559
560 /* pull in any modules if necessary */
561 if (boot_modules_enabled) {
562 module_init(file);
563 if (btinfo_modulelist) {
564 mbm = alloc(sizeof(struct multiboot_module) *
565 btinfo_modulelist->num);
566
567 bim = (struct bi_modulelist_entry *)
568 (((char *) btinfo_modulelist) +
569 sizeof(struct btinfo_modulelist));
570 for (i = 0; i < btinfo_modulelist->num; i++) {
571 mbm[i].mmo_start = bim->base;
572 mbm[i].mmo_end = bim->base + bim->len;
573 mbm[i].mmo_string = (char *)vtophys(bim->path);
574 mbm[i].mmo_reserved = 0;
575 bim++;
576 }
577 mbi->mi_flags |= MULTIBOOT_INFO_HAS_MODS;
578 mbi->mi_mods_count = btinfo_modulelist->num;
579 mbi->mi_mods_addr = vtophys(mbm);
580 }
581 }
582
583 #ifdef DEBUG
584 printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", marks[MARK_ENTRY],
585 marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]);
586 #endif
587
588
589 #if 0
590 if (btinfo_symtab.nsym) {
591 mbi->mi_flags |= MULTIBOOT_INFO_HAS_ELF_SYMS;
592 mbi->mi_elfshdr_addr = marks[MARK_SYM];
593 btinfo_symtab.nsym = marks[MARK_NSYM];
594 btinfo_symtab.ssym = marks[MARK_SYM];
595 btinfo_symtab.esym = marks[MARK_END];
596 #endif
597
598 multiboot(marks[MARK_ENTRY], vtophys(mbi),
599 x86_trunc_page(mbi->mi_mem_lower*1024));
600 panic("exec returned");
601
602 out:
603 dealloc(mbi, 0);
604 return -1;
605 }
606
607 void
608 x86_progress(const char *fmt, ...)
609 {
610 va_list ap;
611
612 if ((howto & AB_SILENT) != 0)
613 return;
614 va_start(ap, fmt);
615 vprintf(fmt, ap);
616 va_end(ap);
617 }
618