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