exec.c revision 1.68 1 /* $NetBSD: exec.c,v 1.68 2017/03/24 08:50:17 nonaka 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 a NetBSD ELF kernel. The low level startup is done in startprog.S.
92 * This is a special version of exec.c to support use of XMS.
93 */
94
95 #include <sys/param.h>
96 #include <sys/reboot.h>
97 #include <sys/reboot.h>
98
99 #include <i386/multiboot.h>
100
101 #include <lib/libsa/stand.h>
102 #include <lib/libkern/libkern.h>
103
104 #include "loadfile.h"
105 #include "libi386.h"
106 #include "bootinfo.h"
107 #include "bootmod.h"
108 #include "vbe.h"
109 #ifdef SUPPORT_PS2
110 #include "biosmca.h"
111 #endif
112 #ifdef EFIBOOT
113 #include "efiboot.h"
114 #undef DEBUG /* XXX */
115 #endif
116
117 #define BOOT_NARGS 6
118
119 #ifndef PAGE_SIZE
120 #define PAGE_SIZE 4096
121 #endif
122
123 #define MODULE_WARNING_SEC 5
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 typedef struct userconf_command {
132 char *uc_text;
133 size_t uc_len;
134 struct userconf_command *uc_next;
135 } userconf_command_t;
136 userconf_command_t *userconf_commands = NULL;
137
138 static struct btinfo_framebuffer btinfo_framebuffer;
139
140 static struct btinfo_modulelist *btinfo_modulelist;
141 static size_t btinfo_modulelist_size;
142 static uint32_t image_end;
143 static char module_base[64] = "/";
144 static int howto;
145
146 static struct btinfo_userconfcommands *btinfo_userconfcommands = NULL;
147 static size_t btinfo_userconfcommands_size = 0;
148
149 static void module_init(const char *);
150 static void module_add_common(const char *, uint8_t);
151
152 static void userconf_init(void);
153
154 static void extract_device(const char *, char *, size_t);
155 static void module_base_path(char *, size_t);
156 static int module_open(boot_module_t *, int, const char *, const char *,
157 bool);
158
159 void
160 framebuffer_configure(struct btinfo_framebuffer *fb)
161 {
162 if (fb)
163 btinfo_framebuffer = *fb;
164 else {
165 btinfo_framebuffer.physaddr = 0;
166 btinfo_framebuffer.flags = 0;
167 }
168 }
169
170 void
171 module_add(char *name)
172 {
173 return module_add_common(name, BM_TYPE_KMOD);
174 }
175
176 void
177 splash_add(char *name)
178 {
179 return module_add_common(name, BM_TYPE_IMAGE);
180 }
181
182 void
183 rnd_add(char *name)
184 {
185 return module_add_common(name, BM_TYPE_RND);
186 }
187
188 void
189 fs_add(char *name)
190 {
191 return module_add_common(name, BM_TYPE_FS);
192 }
193
194 static void
195 module_add_common(const char *name, uint8_t type)
196 {
197 boot_module_t *bm, *bmp;
198 size_t len;
199 char *str;
200
201 while (*name == ' ' || *name == '\t')
202 ++name;
203
204 for (bm = boot_modules; bm != NULL; bm = bm->bm_next)
205 if (bm->bm_type == type && strcmp(bm->bm_path, name) == 0)
206 return;
207
208 bm = alloc(sizeof(boot_module_t));
209 len = strlen(name) + 1;
210 str = alloc(len);
211 if (bm == NULL || str == NULL) {
212 printf("couldn't allocate module\n");
213 return;
214 }
215 memcpy(str, name, len);
216 bm->bm_path = str;
217 bm->bm_next = NULL;
218 bm->bm_type = type;
219 if (boot_modules == NULL)
220 boot_modules = bm;
221 else {
222 for (bmp = boot_modules; bmp->bm_next;
223 bmp = bmp->bm_next)
224 ;
225 bmp->bm_next = bm;
226 }
227 }
228
229 void
230 userconf_add(char *cmd)
231 {
232 userconf_command_t *uc;
233 size_t len;
234 char *text;
235
236 while (*cmd == ' ' || *cmd == '\t')
237 ++cmd;
238
239 uc = alloc(sizeof(*uc));
240 if (uc == NULL) {
241 printf("couldn't allocate command\n");
242 return;
243 }
244
245 len = strlen(cmd) + 1;
246 text = alloc(len);
247 if (text == NULL) {
248 dealloc(uc, sizeof(*uc));
249 printf("couldn't allocate command\n");
250 return;
251 }
252 memcpy(text, cmd, len);
253
254 uc->uc_text = text;
255 uc->uc_len = len;
256 uc->uc_next = NULL;
257
258 if (userconf_commands == NULL)
259 userconf_commands = uc;
260 else {
261 userconf_command_t *ucp;
262 for (ucp = userconf_commands; ucp->uc_next != NULL;
263 ucp = ucp->uc_next)
264 ;
265 ucp->uc_next = uc;
266 }
267 }
268
269 static int
270 common_load_kernel(const char *file, u_long *basemem, u_long *extmem,
271 physaddr_t loadaddr, int floppy, u_long marks[MARK_MAX])
272 {
273 int fd;
274 #ifdef XMS
275 u_long xmsmem;
276 physaddr_t origaddr = loadaddr;
277 #endif
278
279 *extmem = getextmem();
280 *basemem = getbasemem();
281
282 #ifdef XMS
283 if ((getextmem1() == 0) && (xmsmem = checkxms())) {
284 u_long kernsize;
285
286 /*
287 * With "CONSERVATIVE_MEMDETECT", extmem is 0 because
288 * getextmem() is getextmem1(). Without, the "smart"
289 * methods could fail to report all memory as well.
290 * xmsmem is a few kB less than the actual size, but
291 * better than nothing.
292 */
293 if (xmsmem > *extmem)
294 *extmem = xmsmem;
295 /*
296 * Get the size of the kernel
297 */
298 marks[MARK_START] = loadaddr;
299 if ((fd = loadfile(file, marks, COUNT_KERNEL)) == -1)
300 return EIO;
301 close(fd);
302
303 kernsize = marks[MARK_END];
304 kernsize = (kernsize + 1023) / 1024;
305
306 loadaddr = xmsalloc(kernsize);
307 if (!loadaddr)
308 return ENOMEM;
309 }
310 #endif
311 marks[MARK_START] = loadaddr;
312 if ((fd = loadfile(file, marks,
313 LOAD_KERNEL & ~(floppy ? LOAD_BACKWARDS : 0))) == -1)
314 return EIO;
315
316 close(fd);
317
318 /* If the root fs type is unusual, load its module. */
319 if (fsmod != NULL)
320 module_add_common(fsmod, BM_TYPE_KMOD);
321
322 /*
323 * Gather some information for the kernel. Do this after the
324 * "point of no return" to avoid memory leaks.
325 * (but before DOS might be trashed in the XMS case)
326 */
327 #ifdef PASS_BIOSGEOM
328 bi_getbiosgeom();
329 #endif
330 #ifdef PASS_MEMMAP
331 bi_getmemmap();
332 #endif
333
334 #ifdef XMS
335 if (loadaddr != origaddr) {
336 /*
337 * We now have done our last DOS IO, so we may
338 * trash the OS. Copy the data from the temporary
339 * buffer to its real address.
340 */
341 marks[MARK_START] -= loadaddr;
342 marks[MARK_END] -= loadaddr;
343 marks[MARK_SYM] -= loadaddr;
344 marks[MARK_END] -= loadaddr;
345 ppbcopy(loadaddr, origaddr, marks[MARK_END]);
346 }
347 #endif
348 marks[MARK_END] = (((u_long) marks[MARK_END] + sizeof(int) - 1)) &
349 (-sizeof(int));
350 image_end = marks[MARK_END];
351 kernel_loaded = true;
352
353 return 0;
354 }
355
356 int
357 exec_netbsd(const char *file, physaddr_t loadaddr, int boothowto, int floppy,
358 void (*callback)(void))
359 {
360 uint32_t boot_argv[BOOT_NARGS];
361 u_long marks[MARK_MAX];
362 struct btinfo_symtab btinfo_symtab;
363 u_long extmem;
364 u_long basemem;
365 int error;
366 #ifdef EFIBOOT
367 int i;
368 #endif
369
370 #ifdef DEBUG
371 printf("exec: file=%s loadaddr=0x%lx\n", file ? file : "NULL",
372 loadaddr);
373 #endif
374
375 BI_ALLOC(BTINFO_MAX);
376
377 BI_ADD(&btinfo_console, BTINFO_CONSOLE, sizeof(struct btinfo_console));
378
379 howto = boothowto;
380
381 memset(marks, 0, sizeof(marks));
382
383 error = common_load_kernel(file, &basemem, &extmem, loadaddr, floppy,
384 marks);
385 if (error) {
386 errno = error;
387 goto out;
388 }
389 #ifdef EFIBOOT
390 /* adjust to the real load address */
391 marks[MARK_START] -= efi_loadaddr;
392 marks[MARK_ENTRY] -= efi_loadaddr;
393 marks[MARK_DATA] -= efi_loadaddr;
394 /* MARK_NSYM */
395 marks[MARK_SYM] -= efi_loadaddr;
396 marks[MARK_END] -= efi_loadaddr;
397 #endif
398
399 boot_argv[0] = boothowto;
400 boot_argv[1] = 0;
401 boot_argv[2] = vtophys(bootinfo); /* old cyl offset */
402 boot_argv[3] = marks[MARK_END];
403 boot_argv[4] = extmem;
404 boot_argv[5] = basemem;
405
406 /* pull in any modules if necessary */
407 if (boot_modules_enabled) {
408 module_init(file);
409 if (btinfo_modulelist) {
410 #ifdef EFIBOOT
411 /* convert module loaded address to paddr */
412 struct bi_modulelist_entry *bim;
413 bim = (void *)(btinfo_modulelist + 1);
414 for (i = 0; i < btinfo_modulelist->num; i++, bim++)
415 bim->base -= efi_loadaddr;
416 btinfo_modulelist->endpa -= efi_loadaddr;
417 #endif
418 BI_ADD(btinfo_modulelist, BTINFO_MODULELIST,
419 btinfo_modulelist_size);
420 }
421 }
422
423 userconf_init();
424 if (btinfo_userconfcommands != NULL)
425 BI_ADD(btinfo_userconfcommands, BTINFO_USERCONFCOMMANDS,
426 btinfo_userconfcommands_size);
427
428 #ifdef DEBUG
429 printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", marks[MARK_ENTRY],
430 marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]);
431 #endif
432
433 btinfo_symtab.nsym = marks[MARK_NSYM];
434 btinfo_symtab.ssym = marks[MARK_SYM];
435 btinfo_symtab.esym = marks[MARK_END];
436 BI_ADD(&btinfo_symtab, BTINFO_SYMTAB, sizeof(struct btinfo_symtab));
437
438 /* set new video mode if necessary */
439 vbe_commit();
440 BI_ADD(&btinfo_framebuffer, BTINFO_FRAMEBUFFER,
441 sizeof(struct btinfo_framebuffer));
442
443 if (callback != NULL)
444 (*callback)();
445 #ifdef EFIBOOT
446 /* Copy bootinfo to safe arena. */
447 for (i = 0; i < bootinfo->nentries; i++) {
448 struct btinfo_common *bi = (void *)(u_long)bootinfo->entry[i];
449 char *p = alloc(bi->len);
450 memcpy(p, bi, bi->len);
451 bootinfo->entry[i] = vtophys(p);
452 }
453
454 efi_kernel_start = marks[MARK_START];
455 efi_kernel_size = image_end - efi_loadaddr - efi_kernel_start;
456 #endif
457 startprog(marks[MARK_ENTRY], BOOT_NARGS, boot_argv,
458 x86_trunc_page(basemem * 1024));
459 panic("exec returned");
460
461 out:
462 BI_FREE();
463 bootinfo = NULL;
464 return -1;
465 }
466
467 int
468 count_netbsd(const char *file, u_long *rsz)
469 {
470 u_long marks[MARK_MAX];
471 char kdev[64];
472 char base_path[64] = "/";
473 struct stat st;
474 boot_module_t *bm;
475 u_long sz;
476 int err, fd;
477
478 howto = AB_SILENT;
479
480 memset(marks, 0, sizeof(marks));
481 if ((fd = loadfile(file, marks, COUNT_KERNEL | LOAD_NOTE)) == -1)
482 return -1;
483 close(fd);
484 marks[MARK_END] = (((u_long) marks[MARK_END] + sizeof(int) - 1)) &
485 (-sizeof(int));
486 sz = marks[MARK_END];
487
488 /* The modules must be allocated after the kernel */
489 if (boot_modules_enabled) {
490 extract_device(file, kdev, sizeof(kdev));
491 module_base_path(base_path, sizeof(base_path));
492
493 /* If the root fs type is unusual, load its module. */
494 if (fsmod != NULL)
495 module_add_common(fsmod, BM_TYPE_KMOD);
496
497 for (bm = boot_modules; bm; bm = bm->bm_next) {
498 fd = module_open(bm, 0, kdev, base_path, false);
499 if (fd == -1)
500 continue;
501 sz = (sz + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
502 err = fstat(fd, &st);
503 if (err == -1 || st.st_size == -1) {
504 close(fd);
505 continue;
506 }
507 sz += st.st_size;
508 close(fd);
509 }
510 }
511
512 *rsz = sz;
513 return 0;
514 }
515
516 static void
517 extract_device(const char *path, char *buf, size_t buflen)
518 {
519 size_t i;
520
521 if (strchr(path, ':') != NULL) {
522 for (i = 0; i < buflen - 2 && path[i] != ':'; i++)
523 buf[i] = path[i];
524 buf[i++] = ':';
525 buf[i] = '\0';
526 } else
527 buf[0] = '\0';
528 }
529
530 static const char *
531 module_path(boot_module_t *bm, const char *kdev, const char *base_path)
532 {
533 static char buf[256];
534 char name_buf[256], dev_buf[64];
535 const char *name, *name2, *p;
536
537 name = bm->bm_path;
538 for (name2 = name; *name2; ++name2) {
539 if (*name2 == ' ' || *name2 == '\t') {
540 strlcpy(name_buf, name, sizeof(name_buf));
541 if ((uintptr_t)name2 - (uintptr_t)name < sizeof(name_buf))
542 name_buf[name2 - name] = '\0';
543 name = name_buf;
544 break;
545 }
546 }
547 if ((p = strchr(name, ':')) != NULL) {
548 /* device specified, use it */
549 if (p[1] == '/')
550 snprintf(buf, sizeof(buf), "%s", name);
551 else {
552 p++;
553 extract_device(name, dev_buf, sizeof(dev_buf));
554 snprintf(buf, sizeof(buf), "%s%s/%s/%s.kmod",
555 dev_buf, base_path, p, p);
556 }
557 } else {
558 /* device not specified; load from kernel device if known */
559 if (name[0] == '/')
560 snprintf(buf, sizeof(buf), "%s%s", kdev, name);
561 else
562 snprintf(buf, sizeof(buf), "%s%s/%s/%s.kmod",
563 kdev, base_path, name, name);
564 }
565
566 return buf;
567 }
568
569 static int
570 module_open(boot_module_t *bm, int mode, const char *kdev,
571 const char *base_path, bool doload)
572 {
573 int fd;
574 const char *path;
575
576 /* check the expanded path first */
577 path = module_path(bm, kdev, base_path);
578 fd = open(path, mode);
579 if (fd != -1) {
580 if ((howto & AB_SILENT) == 0 && doload)
581 printf("Loading %s ", path);
582 } else {
583 /* now attempt the raw path provided */
584 fd = open(bm->bm_path, mode);
585 if (fd != -1 && (howto & AB_SILENT) == 0 && doload)
586 printf("Loading %s ", bm->bm_path);
587 }
588 if (!doload && fd == -1) {
589 printf("WARNING: couldn't open %s", bm->bm_path);
590 if (strcmp(bm->bm_path, path) != 0)
591 printf(" (%s)", path);
592 printf("\n");
593 }
594 return fd;
595 }
596
597 static void
598 module_base_path(char *buf, size_t bufsize)
599 {
600 const char *machine;
601
602 switch (netbsd_elf_class) {
603 case ELFCLASS32:
604 machine = "i386";
605 break;
606 case ELFCLASS64:
607 machine = "amd64";
608 break;
609 default:
610 machine = "generic";
611 break;
612 }
613 if (netbsd_version / 1000000 % 100 == 99) {
614 /* -current */
615 snprintf(buf, bufsize,
616 "/stand/%s/%d.%d.%d/modules", machine,
617 netbsd_version / 100000000,
618 netbsd_version / 1000000 % 100,
619 netbsd_version / 100 % 100);
620 } else if (netbsd_version != 0) {
621 /* release */
622 snprintf(buf, bufsize,
623 "/stand/%s/%d.%d/modules", machine,
624 netbsd_version / 100000000,
625 netbsd_version / 1000000 % 100);
626 }
627 }
628
629 static void
630 module_init(const char *kernel_path)
631 {
632 struct bi_modulelist_entry *bi;
633 struct stat st;
634 char kdev[64];
635 char *buf;
636 boot_module_t *bm;
637 ssize_t len;
638 off_t off;
639 int err, fd, nfail = 0;
640
641 extract_device(kernel_path, kdev, sizeof(kdev));
642 module_base_path(module_base, sizeof(module_base));
643
644 /* First, see which modules are valid and calculate btinfo size */
645 len = sizeof(struct btinfo_modulelist);
646 for (bm = boot_modules; bm; bm = bm->bm_next) {
647 fd = module_open(bm, 0, kdev, module_base, false);
648 if (fd == -1) {
649 bm->bm_len = -1;
650 ++nfail;
651 continue;
652 }
653 err = fstat(fd, &st);
654 if (err == -1 || st.st_size == -1) {
655 printf("WARNING: couldn't stat %s\n", bm->bm_path);
656 close(fd);
657 bm->bm_len = -1;
658 ++nfail;
659 continue;
660 }
661 bm->bm_len = st.st_size;
662 close(fd);
663 len += sizeof(struct bi_modulelist_entry);
664 }
665
666 /* Allocate the module list */
667 btinfo_modulelist = alloc(len);
668 if (btinfo_modulelist == NULL) {
669 printf("WARNING: couldn't allocate module list\n");
670 wait_sec(MODULE_WARNING_SEC);
671 return;
672 }
673 memset(btinfo_modulelist, 0, len);
674 btinfo_modulelist_size = len;
675
676 /* Fill in btinfo structure */
677 buf = (char *)btinfo_modulelist;
678 btinfo_modulelist->num = 0;
679 off = sizeof(struct btinfo_modulelist);
680
681 for (bm = boot_modules; bm; bm = bm->bm_next) {
682 if (bm->bm_len == -1)
683 continue;
684 fd = module_open(bm, 0, kdev, module_base, true);
685 if (fd == -1)
686 continue;
687 image_end = (image_end + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
688 len = pread(fd, (void *)(uintptr_t)image_end, SSIZE_MAX);
689 if (len < bm->bm_len) {
690 if ((howto & AB_SILENT) != 0)
691 printf("Loading %s ", bm->bm_path);
692 printf(" FAILED\n");
693 } else {
694 btinfo_modulelist->num++;
695 bi = (struct bi_modulelist_entry *)(buf + off);
696 off += sizeof(struct bi_modulelist_entry);
697 strncpy(bi->path, bm->bm_path, sizeof(bi->path) - 1);
698 bi->base = image_end;
699 bi->len = len;
700 switch (bm->bm_type) {
701 case BM_TYPE_KMOD:
702 bi->type = BI_MODULE_ELF;
703 break;
704 case BM_TYPE_IMAGE:
705 bi->type = BI_MODULE_IMAGE;
706 break;
707 case BM_TYPE_FS:
708 bi->type = BI_MODULE_FS;
709 break;
710 case BM_TYPE_RND:
711 default:
712 /* safest -- rnd checks the sha1 */
713 bi->type = BI_MODULE_RND;
714 break;
715 }
716 if ((howto & AB_SILENT) == 0)
717 printf(" \n");
718 }
719 if (len > 0)
720 image_end += len;
721 close(fd);
722 }
723 btinfo_modulelist->endpa = image_end;
724
725 if (nfail > 0) {
726 printf("WARNING: %d module%s failed to load\n",
727 nfail, nfail == 1 ? "" : "s");
728 #if notyet
729 wait_sec(MODULE_WARNING_SEC);
730 #endif
731 }
732 }
733
734 static void
735 userconf_init(void)
736 {
737 size_t count, len;
738 userconf_command_t *uc;
739 char *buf;
740 off_t off;
741
742 /* Calculate the userconf commands list size */
743 count = 0;
744 for (uc = userconf_commands; uc != NULL; uc = uc->uc_next)
745 count++;
746 len = sizeof(*btinfo_userconfcommands) +
747 count * sizeof(struct bi_userconfcommand);
748
749 /* Allocate the userconf commands list */
750 btinfo_userconfcommands = alloc(len);
751 if (btinfo_userconfcommands == NULL) {
752 printf("WARNING: couldn't allocate userconf commands list\n");
753 return;
754 }
755 memset(btinfo_userconfcommands, 0, len);
756 btinfo_userconfcommands_size = len;
757
758 /* Fill in btinfo structure */
759 buf = (char *)btinfo_userconfcommands;
760 off = sizeof(*btinfo_userconfcommands);
761 btinfo_userconfcommands->num = 0;
762 for (uc = userconf_commands; uc != NULL; uc = uc->uc_next) {
763 struct bi_userconfcommand *bi;
764 bi = (struct bi_userconfcommand *)(buf + off);
765 strncpy(bi->text, uc->uc_text, sizeof(bi->text) - 1);
766
767 off += sizeof(*bi);
768 btinfo_userconfcommands->num++;
769 }
770 }
771
772 int
773 exec_multiboot(const char *file, char *args)
774 {
775 struct multiboot_info *mbi;
776 struct multiboot_module *mbm;
777 struct bi_modulelist_entry *bim;
778 int i, len;
779 u_long marks[MARK_MAX];
780 u_long extmem;
781 u_long basemem;
782 char *cmdline;
783
784 mbi = alloc(sizeof(struct multiboot_info));
785 mbi->mi_flags = MULTIBOOT_INFO_HAS_MEMORY;
786
787 if (common_load_kernel(file, &basemem, &extmem, 0, 0, marks))
788 goto out;
789
790 mbi->mi_mem_upper = extmem;
791 mbi->mi_mem_lower = basemem;
792
793 if (args) {
794 mbi->mi_flags |= MULTIBOOT_INFO_HAS_CMDLINE;
795 len = strlen(file) + 1 + strlen(args) + 1;
796 cmdline = alloc(len);
797 snprintf(cmdline, len, "%s %s", file, args);
798 mbi->mi_cmdline = (char *) vtophys(cmdline);
799 }
800
801 /* pull in any modules if necessary */
802 if (boot_modules_enabled) {
803 module_init(file);
804 if (btinfo_modulelist) {
805 mbm = alloc(sizeof(struct multiboot_module) *
806 btinfo_modulelist->num);
807
808 bim = (struct bi_modulelist_entry *)
809 (((char *) btinfo_modulelist) +
810 sizeof(struct btinfo_modulelist));
811 for (i = 0; i < btinfo_modulelist->num; i++) {
812 mbm[i].mmo_start = bim->base;
813 mbm[i].mmo_end = bim->base + bim->len;
814 mbm[i].mmo_string = (char *)vtophys(bim->path);
815 mbm[i].mmo_reserved = 0;
816 bim++;
817 }
818 mbi->mi_flags |= MULTIBOOT_INFO_HAS_MODS;
819 mbi->mi_mods_count = btinfo_modulelist->num;
820 mbi->mi_mods_addr = vtophys(mbm);
821 }
822 }
823
824 #ifdef DEBUG
825 printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", marks[MARK_ENTRY],
826 marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]);
827 #endif
828
829 #if 0
830 if (btinfo_symtab.nsym) {
831 mbi->mi_flags |= MULTIBOOT_INFO_HAS_ELF_SYMS;
832 mbi->mi_elfshdr_addr = marks[MARK_SYM];
833 btinfo_symtab.nsym = marks[MARK_NSYM];
834 btinfo_symtab.ssym = marks[MARK_SYM];
835 btinfo_symtab.esym = marks[MARK_END];
836 #endif
837
838 multiboot(marks[MARK_ENTRY], vtophys(mbi),
839 x86_trunc_page(mbi->mi_mem_lower * 1024));
840 panic("exec returned");
841
842 out:
843 dealloc(mbi, 0);
844 return -1;
845 }
846
847 void
848 x86_progress(const char *fmt, ...)
849 {
850 va_list ap;
851
852 if ((howto & AB_SILENT) != 0)
853 return;
854 va_start(ap, fmt);
855 vprintf(fmt, ap);
856 va_end(ap);
857 }
858