exec.c revision 1.61.2.2 1 /* $NetBSD: exec.c,v 1.61.2.2 2017/03/20 06:57:15 pgoyette 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 memset(marks, 0, sizeof(marks));
479 if ((fd = loadfile(file, marks, COUNT_KERNEL)) == -1)
480 return -1;
481 close(fd);
482 marks[MARK_END] = (((u_long) marks[MARK_END] + sizeof(int) - 1)) &
483 (-sizeof(int));
484 sz = marks[MARK_END];
485
486 /* The modules must be allocated after the kernel */
487 if (boot_modules_enabled) {
488 extract_device(file, kdev, sizeof(kdev));
489 module_base_path(base_path, sizeof(base_path));
490
491 /* If the root fs type is unusual, load its module. */
492 if (fsmod != NULL)
493 module_add(__UNCONST(fsmod));
494
495 for (bm = boot_modules; bm; bm = bm->bm_next) {
496 fd = module_open(bm, 0, kdev, base_path, false);
497 if (fd == -1)
498 continue;
499 sz = (image_end + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
500 err = fstat(fd, &st);
501 if (err == -1 || st.st_size == -1) {
502 close(fd);
503 continue;
504 }
505 sz += st.st_size;
506 close(fd);
507 }
508 }
509
510 *rsz = sz;
511 return 0;
512 }
513
514 static void
515 extract_device(const char *path, char *buf, size_t buflen)
516 {
517 size_t i;
518
519 if (strchr(path, ':') != NULL) {
520 for (i = 0; i < buflen - 2 && path[i] != ':'; i++)
521 buf[i] = path[i];
522 buf[i++] = ':';
523 buf[i] = '\0';
524 } else
525 buf[0] = '\0';
526 }
527
528 static const char *
529 module_path(boot_module_t *bm, const char *kdev, const char *base_path)
530 {
531 static char buf[256];
532 char name_buf[256], dev_buf[64];
533 const char *name, *name2, *p;
534
535 name = bm->bm_path;
536 for (name2 = name; *name2; ++name2) {
537 if (*name2 == ' ' || *name2 == '\t') {
538 strlcpy(name_buf, name, sizeof(name_buf));
539 if ((uintptr_t)name2 - (uintptr_t)name < sizeof(name_buf))
540 name_buf[name2 - name] = '\0';
541 name = name_buf;
542 break;
543 }
544 }
545 if ((p = strchr(name, ':')) != NULL) {
546 /* device specified, use it */
547 if (p[1] == '/')
548 snprintf(buf, sizeof(buf), "%s", name);
549 else {
550 p++;
551 extract_device(name, dev_buf, sizeof(dev_buf));
552 snprintf(buf, sizeof(buf), "%s%s/%s/%s.kmod",
553 dev_buf, base_path, p, p);
554 }
555 } else {
556 /* device not specified; load from kernel device if known */
557 if (name[0] == '/')
558 snprintf(buf, sizeof(buf), "%s%s", kdev, name);
559 else
560 snprintf(buf, sizeof(buf), "%s%s/%s/%s.kmod",
561 kdev, base_path, name, name);
562 }
563
564 return buf;
565 }
566
567 static int
568 module_open(boot_module_t *bm, int mode, const char *kdev,
569 const char *base_path, bool doload)
570 {
571 int fd;
572 const char *path;
573
574 /* check the expanded path first */
575 path = module_path(bm, kdev, base_path);
576 fd = open(path, mode);
577 if (fd != -1) {
578 if ((howto & AB_SILENT) == 0 && doload)
579 printf("Loading %s ", path);
580 } else {
581 /* now attempt the raw path provided */
582 fd = open(bm->bm_path, mode);
583 if (fd != -1 && (howto & AB_SILENT) == 0 && doload)
584 printf("Loading %s ", bm->bm_path);
585 }
586 if (!doload && fd == -1) {
587 printf("WARNING: couldn't open %s", bm->bm_path);
588 if (strcmp(bm->bm_path, path) != 0)
589 printf(" (%s)", path);
590 printf("\n");
591 }
592 return fd;
593 }
594
595 static void
596 module_base_path(char *buf, size_t bufsize)
597 {
598 const char *machine;
599
600 switch (netbsd_elf_class) {
601 case ELFCLASS32:
602 machine = "i386";
603 break;
604 case ELFCLASS64:
605 machine = "amd64";
606 break;
607 default:
608 machine = "generic";
609 break;
610 }
611 if (netbsd_version / 1000000 % 100 == 99) {
612 /* -current */
613 snprintf(buf, bufsize,
614 "/stand/%s/%d.%d.%d/modules", machine,
615 netbsd_version / 100000000,
616 netbsd_version / 1000000 % 100,
617 netbsd_version / 100 % 100);
618 } else if (netbsd_version != 0) {
619 /* release */
620 snprintf(buf, bufsize,
621 "/stand/%s/%d.%d/modules", machine,
622 netbsd_version / 100000000,
623 netbsd_version / 1000000 % 100);
624 }
625 }
626
627 static void
628 module_init(const char *kernel_path)
629 {
630 struct bi_modulelist_entry *bi;
631 struct stat st;
632 char kdev[64];
633 char *buf;
634 boot_module_t *bm;
635 ssize_t len;
636 off_t off;
637 int err, fd, nfail = 0;
638
639 extract_device(kernel_path, kdev, sizeof(kdev));
640 module_base_path(module_base, sizeof(module_base));
641
642 /* First, see which modules are valid and calculate btinfo size */
643 len = sizeof(struct btinfo_modulelist);
644 for (bm = boot_modules; bm; bm = bm->bm_next) {
645 fd = module_open(bm, 0, kdev, module_base, false);
646 if (fd == -1) {
647 bm->bm_len = -1;
648 ++nfail;
649 continue;
650 }
651 err = fstat(fd, &st);
652 if (err == -1 || st.st_size == -1) {
653 printf("WARNING: couldn't stat %s\n", bm->bm_path);
654 close(fd);
655 bm->bm_len = -1;
656 ++nfail;
657 continue;
658 }
659 bm->bm_len = st.st_size;
660 close(fd);
661 len += sizeof(struct bi_modulelist_entry);
662 }
663
664 /* Allocate the module list */
665 btinfo_modulelist = alloc(len);
666 if (btinfo_modulelist == NULL) {
667 printf("WARNING: couldn't allocate module list\n");
668 wait_sec(MODULE_WARNING_SEC);
669 return;
670 }
671 memset(btinfo_modulelist, 0, len);
672 btinfo_modulelist_size = len;
673
674 /* Fill in btinfo structure */
675 buf = (char *)btinfo_modulelist;
676 btinfo_modulelist->num = 0;
677 off = sizeof(struct btinfo_modulelist);
678
679 for (bm = boot_modules; bm; bm = bm->bm_next) {
680 if (bm->bm_len == -1)
681 continue;
682 fd = module_open(bm, 0, kdev, module_base, true);
683 if (fd == -1)
684 continue;
685 image_end = (image_end + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
686 len = pread(fd, (void *)(uintptr_t)image_end, SSIZE_MAX);
687 if (len < bm->bm_len) {
688 if ((howto & AB_SILENT) != 0)
689 printf("Loading %s ", bm->bm_path);
690 printf(" FAILED\n");
691 } else {
692 btinfo_modulelist->num++;
693 bi = (struct bi_modulelist_entry *)(buf + off);
694 off += sizeof(struct bi_modulelist_entry);
695 strncpy(bi->path, bm->bm_path, sizeof(bi->path) - 1);
696 bi->base = image_end;
697 bi->len = len;
698 switch (bm->bm_type) {
699 case BM_TYPE_KMOD:
700 bi->type = BI_MODULE_ELF;
701 break;
702 case BM_TYPE_IMAGE:
703 bi->type = BI_MODULE_IMAGE;
704 break;
705 case BM_TYPE_FS:
706 bi->type = BI_MODULE_FS;
707 break;
708 case BM_TYPE_RND:
709 default:
710 /* safest -- rnd checks the sha1 */
711 bi->type = BI_MODULE_RND;
712 break;
713 }
714 if ((howto & AB_SILENT) == 0)
715 printf(" \n");
716 }
717 if (len > 0)
718 image_end += len;
719 close(fd);
720 }
721 btinfo_modulelist->endpa = image_end;
722
723 if (nfail > 0) {
724 printf("WARNING: %d module%s failed to load\n",
725 nfail, nfail == 1 ? "" : "s");
726 #if notyet
727 wait_sec(MODULE_WARNING_SEC);
728 #endif
729 }
730 }
731
732 static void
733 userconf_init(void)
734 {
735 size_t count, len;
736 userconf_command_t *uc;
737 char *buf;
738 off_t off;
739
740 /* Calculate the userconf commands list size */
741 count = 0;
742 for (uc = userconf_commands; uc != NULL; uc = uc->uc_next)
743 count++;
744 len = sizeof(*btinfo_userconfcommands) +
745 count * sizeof(struct bi_userconfcommand);
746
747 /* Allocate the userconf commands list */
748 btinfo_userconfcommands = alloc(len);
749 if (btinfo_userconfcommands == NULL) {
750 printf("WARNING: couldn't allocate userconf commands list\n");
751 return;
752 }
753 memset(btinfo_userconfcommands, 0, len);
754 btinfo_userconfcommands_size = len;
755
756 /* Fill in btinfo structure */
757 buf = (char *)btinfo_userconfcommands;
758 off = sizeof(*btinfo_userconfcommands);
759 btinfo_userconfcommands->num = 0;
760 for (uc = userconf_commands; uc != NULL; uc = uc->uc_next) {
761 struct bi_userconfcommand *bi;
762 bi = (struct bi_userconfcommand *)(buf + off);
763 strncpy(bi->text, uc->uc_text, sizeof(bi->text) - 1);
764
765 off += sizeof(*bi);
766 btinfo_userconfcommands->num++;
767 }
768 }
769
770 int
771 exec_multiboot(const char *file, char *args)
772 {
773 struct multiboot_info *mbi;
774 struct multiboot_module *mbm;
775 struct bi_modulelist_entry *bim;
776 int i, len;
777 u_long marks[MARK_MAX];
778 u_long extmem;
779 u_long basemem;
780 char *cmdline;
781
782 mbi = alloc(sizeof(struct multiboot_info));
783 mbi->mi_flags = MULTIBOOT_INFO_HAS_MEMORY;
784
785 if (common_load_kernel(file, &basemem, &extmem, 0, 0, marks))
786 goto out;
787
788 mbi->mi_mem_upper = extmem;
789 mbi->mi_mem_lower = basemem;
790
791 if (args) {
792 mbi->mi_flags |= MULTIBOOT_INFO_HAS_CMDLINE;
793 len = strlen(file) + 1 + strlen(args) + 1;
794 cmdline = alloc(len);
795 snprintf(cmdline, len, "%s %s", file, args);
796 mbi->mi_cmdline = (char *) vtophys(cmdline);
797 }
798
799 /* pull in any modules if necessary */
800 if (boot_modules_enabled) {
801 module_init(file);
802 if (btinfo_modulelist) {
803 mbm = alloc(sizeof(struct multiboot_module) *
804 btinfo_modulelist->num);
805
806 bim = (struct bi_modulelist_entry *)
807 (((char *) btinfo_modulelist) +
808 sizeof(struct btinfo_modulelist));
809 for (i = 0; i < btinfo_modulelist->num; i++) {
810 mbm[i].mmo_start = bim->base;
811 mbm[i].mmo_end = bim->base + bim->len;
812 mbm[i].mmo_string = (char *)vtophys(bim->path);
813 mbm[i].mmo_reserved = 0;
814 bim++;
815 }
816 mbi->mi_flags |= MULTIBOOT_INFO_HAS_MODS;
817 mbi->mi_mods_count = btinfo_modulelist->num;
818 mbi->mi_mods_addr = vtophys(mbm);
819 }
820 }
821
822 #ifdef DEBUG
823 printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", marks[MARK_ENTRY],
824 marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]);
825 #endif
826
827 #if 0
828 if (btinfo_symtab.nsym) {
829 mbi->mi_flags |= MULTIBOOT_INFO_HAS_ELF_SYMS;
830 mbi->mi_elfshdr_addr = marks[MARK_SYM];
831 btinfo_symtab.nsym = marks[MARK_NSYM];
832 btinfo_symtab.ssym = marks[MARK_SYM];
833 btinfo_symtab.esym = marks[MARK_END];
834 #endif
835
836 multiboot(marks[MARK_ENTRY], vtophys(mbi),
837 x86_trunc_page(mbi->mi_mem_lower * 1024));
838 panic("exec returned");
839
840 out:
841 dealloc(mbi, 0);
842 return -1;
843 }
844
845 void
846 x86_progress(const char *fmt, ...)
847 {
848 va_list ap;
849
850 if ((howto & AB_SILENT) != 0)
851 return;
852 va_start(ap, fmt);
853 vprintf(fmt, ap);
854 va_end(ap);
855 }
856