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