Home | History | Annotate | Line # | Download | only in lib
exec.c revision 1.63
      1  1.63    nonaka /*	$NetBSD: exec.c,v 1.63 2017/01/24 11:09:14 nonaka Exp $	 */
      2  1.23        ad 
      3  1.62      maxv /*
      4  1.40        ad  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
      5  1.23        ad  * All rights reserved.
      6  1.23        ad  *
      7  1.23        ad  * Redistribution and use in source and binary forms, with or without
      8  1.23        ad  * modification, are permitted provided that the following conditions
      9  1.23        ad  * are met:
     10  1.23        ad  * 1. Redistributions of source code must retain the above copyright
     11  1.23        ad  *    notice, this list of conditions and the following disclaimer.
     12  1.23        ad  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.23        ad  *    notice, this list of conditions and the following disclaimer in the
     14  1.23        ad  *    documentation and/or other materials provided with the distribution.
     15  1.23        ad  *
     16  1.23        ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.23        ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.23        ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.23        ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.23        ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.23        ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.23        ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.23        ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.23        ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.23        ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.23        ad  * POSSIBILITY OF SUCH DAMAGE.
     27  1.23        ad  */
     28   1.1     perry 
     29   1.1     perry /*
     30   1.1     perry  * Copyright (c) 1982, 1986, 1990, 1993
     31   1.1     perry  *	The Regents of the University of California.  All rights reserved.
     32  1.19       agc  *
     33  1.19       agc  * Redistribution and use in source and binary forms, with or without
     34  1.19       agc  * modification, are permitted provided that the following conditions
     35  1.19       agc  * are met:
     36  1.19       agc  * 1. Redistributions of source code must retain the above copyright
     37  1.19       agc  *    notice, this list of conditions and the following disclaimer.
     38  1.19       agc  * 2. Redistributions in binary form must reproduce the above copyright
     39  1.19       agc  *    notice, this list of conditions and the following disclaimer in the
     40  1.19       agc  *    documentation and/or other materials provided with the distribution.
     41  1.19       agc  * 3. Neither the name of the University nor the names of its contributors
     42  1.19       agc  *    may be used to endorse or promote products derived from this software
     43  1.19       agc  *    without specific prior written permission.
     44  1.19       agc  *
     45  1.19       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     46  1.19       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     47  1.19       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     48  1.19       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     49  1.19       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     50  1.19       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     51  1.19       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     52  1.19       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     53  1.19       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     54  1.19       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     55  1.19       agc  * SUCH DAMAGE.
     56  1.19       agc  *
     57  1.19       agc  * 	@(#)boot.c	8.1 (Berkeley) 6/10/93
     58  1.19       agc  */
     59  1.19       agc 
     60  1.19       agc /*
     61   1.1     perry  * Copyright (c) 1996
     62   1.1     perry  *	Matthias Drochner.  All rights reserved.
     63   1.1     perry  * Copyright (c) 1996
     64   1.1     perry  * 	Perry E. Metzger.  All rights reserved.
     65   1.1     perry  *
     66   1.1     perry  * Redistribution and use in source and binary forms, with or without
     67   1.1     perry  * modification, are permitted provided that the following conditions
     68   1.1     perry  * are met:
     69   1.1     perry  * 1. Redistributions of source code must retain the above copyright
     70   1.1     perry  *    notice, this list of conditions and the following disclaimer.
     71   1.1     perry  * 2. Redistributions in binary form must reproduce the above copyright
     72   1.1     perry  *    notice, this list of conditions and the following disclaimer in the
     73   1.1     perry  *    documentation and/or other materials provided with the distribution.
     74   1.1     perry  *
     75   1.1     perry  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     76   1.1     perry  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     77   1.1     perry  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     78   1.1     perry  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     79   1.1     perry  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     80   1.1     perry  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     81   1.1     perry  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     82   1.1     perry  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     83   1.1     perry  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     84   1.1     perry  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     85   1.1     perry  * SUCH DAMAGE.
     86   1.1     perry  *
     87   1.1     perry  * 	@(#)boot.c	8.1 (Berkeley) 6/10/93
     88   1.1     perry  */
     89   1.1     perry 
     90   1.2   thorpej /*
     91  1.62      maxv  * Starts a NetBSD ELF kernel. The low level startup is done in startprog.S.
     92   1.8  christos  * This is a special version of exec.c to support use of XMS.
     93   1.1     perry  */
     94   1.8  christos 
     95   1.1     perry #include <sys/param.h>
     96   1.1     perry #include <sys/reboot.h>
     97  1.40        ad #include <sys/reboot.h>
     98   1.1     perry 
     99  1.53  jakllsch #include <i386/multiboot.h>
    100  1.33     joerg 
    101   1.1     perry #include <lib/libsa/stand.h>
    102  1.23        ad #include <lib/libkern/libkern.h>
    103   1.4  drochner 
    104   1.6  christos #include "loadfile.h"
    105   1.1     perry #include "libi386.h"
    106   1.4  drochner #include "bootinfo.h"
    107  1.23        ad #include "bootmod.h"
    108  1.42  jmcneill #include "vbe.h"
    109  1.16  jdolecek #ifdef SUPPORT_PS2
    110  1.16  jdolecek #include "biosmca.h"
    111  1.16  jdolecek #endif
    112  1.63    nonaka #ifdef EFIBOOT
    113  1.63    nonaka #include "efiboot.h"
    114  1.63    nonaka #undef DEBUG	/* XXX */
    115  1.63    nonaka static u_long efi_loadaddr;
    116  1.63    nonaka #endif
    117   1.1     perry 
    118   1.6  christos #define BOOT_NARGS	6
    119   1.1     perry 
    120  1.23        ad #ifndef	PAGE_SIZE
    121  1.23        ad #define	PAGE_SIZE	4096
    122  1.23        ad #endif
    123  1.23        ad 
    124  1.43   tsutsui #define MODULE_WARNING_SEC	5
    125  1.41  jmcneill 
    126   1.4  drochner extern struct btinfo_console btinfo_console;
    127   1.4  drochner 
    128  1.23        ad boot_module_t *boot_modules;
    129  1.23        ad bool boot_modules_enabled = true;
    130  1.23        ad bool kernel_loaded;
    131  1.23        ad 
    132  1.47  uebayasi typedef struct userconf_command {
    133  1.47  uebayasi 	char *uc_text;
    134  1.47  uebayasi 	size_t uc_len;
    135  1.47  uebayasi 	struct userconf_command *uc_next;
    136  1.47  uebayasi } userconf_command_t;
    137  1.47  uebayasi userconf_command_t *userconf_commands = NULL;
    138  1.47  uebayasi 
    139  1.39  jmcneill static struct btinfo_framebuffer btinfo_framebuffer;
    140  1.39  jmcneill 
    141  1.23        ad static struct btinfo_modulelist *btinfo_modulelist;
    142  1.23        ad static size_t btinfo_modulelist_size;
    143  1.23        ad static uint32_t image_end;
    144  1.26        ad static char module_base[64] = "/";
    145  1.40        ad static int howto;
    146  1.23        ad 
    147  1.47  uebayasi static struct btinfo_userconfcommands *btinfo_userconfcommands = NULL;
    148  1.47  uebayasi static size_t btinfo_userconfcommands_size = 0;
    149  1.47  uebayasi 
    150  1.41  jmcneill static void	module_init(const char *);
    151  1.57  jakllsch static void	module_add_common(const char *, uint8_t);
    152  1.23        ad 
    153  1.47  uebayasi static void	userconf_init(void);
    154  1.47  uebayasi 
    155  1.34        ad void
    156  1.39  jmcneill framebuffer_configure(struct btinfo_framebuffer *fb)
    157  1.39  jmcneill {
    158  1.39  jmcneill 	if (fb)
    159  1.39  jmcneill 		btinfo_framebuffer = *fb;
    160  1.39  jmcneill 	else {
    161  1.39  jmcneill 		btinfo_framebuffer.physaddr = 0;
    162  1.39  jmcneill 		btinfo_framebuffer.flags = 0;
    163  1.39  jmcneill 	}
    164  1.39  jmcneill }
    165  1.39  jmcneill 
    166  1.39  jmcneill void
    167  1.34        ad module_add(char *name)
    168  1.34        ad {
    169  1.46  jmcneill 	return module_add_common(name, BM_TYPE_KMOD);
    170  1.46  jmcneill }
    171  1.46  jmcneill 
    172  1.46  jmcneill void
    173  1.46  jmcneill splash_add(char *name)
    174  1.46  jmcneill {
    175  1.46  jmcneill 	return module_add_common(name, BM_TYPE_IMAGE);
    176  1.46  jmcneill }
    177  1.46  jmcneill 
    178  1.49       tls void
    179  1.49       tls rnd_add(char *name)
    180  1.49       tls {
    181  1.49       tls 	return module_add_common(name, BM_TYPE_RND);
    182  1.49       tls }
    183  1.49       tls 
    184  1.51  jmcneill void
    185  1.51  jmcneill fs_add(char *name)
    186  1.51  jmcneill {
    187  1.51  jmcneill 	return module_add_common(name, BM_TYPE_FS);
    188  1.51  jmcneill }
    189  1.51  jmcneill 
    190  1.46  jmcneill static void
    191  1.57  jakllsch module_add_common(const char *name, uint8_t type)
    192  1.46  jmcneill {
    193  1.34        ad 	boot_module_t *bm, *bmp;
    194  1.34        ad 	size_t len;
    195  1.34        ad 	char *str;
    196  1.34        ad 
    197  1.35        ad 	while (*name == ' ' || *name == '\t')
    198  1.35        ad 		++name;
    199  1.35        ad 
    200  1.34        ad 	bm = alloc(sizeof(boot_module_t));
    201  1.34        ad 	len = strlen(name) + 1;
    202  1.34        ad 	str = alloc(len);
    203  1.34        ad 	if (bm == NULL || str == NULL) {
    204  1.34        ad 		printf("couldn't allocate module\n");
    205  1.34        ad 		return;
    206  1.34        ad 	}
    207  1.34        ad 	memcpy(str, name, len);
    208  1.34        ad 	bm->bm_path = str;
    209  1.34        ad 	bm->bm_next = NULL;
    210  1.46  jmcneill 	bm->bm_type = type;
    211  1.34        ad 	if (boot_modules == NULL)
    212  1.34        ad 		boot_modules = bm;
    213  1.34        ad 	else {
    214  1.34        ad 		for (bmp = boot_modules; bmp->bm_next;
    215  1.34        ad 		    bmp = bmp->bm_next)
    216  1.34        ad 			;
    217  1.34        ad 		bmp->bm_next = bm;
    218  1.34        ad 	}
    219  1.34        ad }
    220  1.34        ad 
    221  1.47  uebayasi void
    222  1.47  uebayasi userconf_add(char *cmd)
    223  1.47  uebayasi {
    224  1.47  uebayasi 	userconf_command_t *uc;
    225  1.47  uebayasi 	size_t len;
    226  1.47  uebayasi 	char *text;
    227  1.47  uebayasi 
    228  1.47  uebayasi 	while (*cmd == ' ' || *cmd == '\t')
    229  1.47  uebayasi 		++cmd;
    230  1.47  uebayasi 
    231  1.47  uebayasi 	uc = alloc(sizeof(*uc));
    232  1.47  uebayasi 	if (uc == NULL) {
    233  1.47  uebayasi 		printf("couldn't allocate command\n");
    234  1.47  uebayasi 		return;
    235  1.47  uebayasi 	}
    236  1.47  uebayasi 
    237  1.47  uebayasi 	len = strlen(cmd) + 1;
    238  1.47  uebayasi 	text = alloc(len);
    239  1.47  uebayasi 	if (text == NULL) {
    240  1.47  uebayasi 		dealloc(uc, sizeof(*uc));
    241  1.47  uebayasi 		printf("couldn't allocate command\n");
    242  1.47  uebayasi 		return;
    243  1.47  uebayasi 	}
    244  1.47  uebayasi 	memcpy(text, cmd, len);
    245  1.47  uebayasi 
    246  1.47  uebayasi 	uc->uc_text = text;
    247  1.47  uebayasi 	uc->uc_len = len;
    248  1.47  uebayasi 	uc->uc_next = NULL;
    249  1.47  uebayasi 
    250  1.47  uebayasi 	if (userconf_commands == NULL)
    251  1.47  uebayasi 		userconf_commands = uc;
    252  1.47  uebayasi 	else {
    253  1.47  uebayasi 		userconf_command_t *ucp;
    254  1.47  uebayasi 		for (ucp = userconf_commands; ucp->uc_next != NULL;
    255  1.47  uebayasi 		     ucp = ucp->uc_next)
    256  1.47  uebayasi 			;
    257  1.47  uebayasi 		ucp->uc_next = uc;
    258  1.47  uebayasi 	}
    259  1.47  uebayasi }
    260  1.47  uebayasi 
    261  1.32     joerg static int
    262  1.32     joerg common_load_kernel(const char *file, u_long *basemem, u_long *extmem,
    263  1.32     joerg     physaddr_t loadaddr, int floppy, u_long marks[MARK_MAX])
    264   1.1     perry {
    265  1.32     joerg 	int fd;
    266   1.8  christos #ifdef XMS
    267  1.62      maxv 	u_long xmsmem;
    268  1.62      maxv 	physaddr_t origaddr = loadaddr;
    269   1.8  christos #endif
    270   1.8  christos 
    271  1.32     joerg 	*extmem = getextmem();
    272  1.32     joerg 	*basemem = getbasemem();
    273   1.8  christos 
    274   1.8  christos #ifdef XMS
    275   1.8  christos 	if ((getextmem1() == 0) && (xmsmem = checkxms())) {
    276  1.62      maxv 		u_long kernsize;
    277   1.8  christos 
    278   1.8  christos 		/*
    279   1.8  christos 		 * With "CONSERVATIVE_MEMDETECT", extmem is 0 because
    280  1.62      maxv 		 * getextmem() is getextmem1(). Without, the "smart"
    281  1.62      maxv 		 * methods could fail to report all memory as well.
    282   1.8  christos 		 * xmsmem is a few kB less than the actual size, but
    283  1.62      maxv 		 * better than nothing.
    284   1.8  christos 		 */
    285  1.32     joerg 		if (xmsmem > *extmem)
    286  1.32     joerg 			*extmem = xmsmem;
    287  1.21  junyoung 		/*
    288   1.8  christos 		 * Get the size of the kernel
    289   1.8  christos 		 */
    290   1.8  christos 		marks[MARK_START] = loadaddr;
    291  1.12  christos 		if ((fd = loadfile(file, marks, COUNT_KERNEL)) == -1)
    292  1.32     joerg 			return EIO;
    293   1.8  christos 		close(fd);
    294   1.8  christos 
    295   1.8  christos 		kernsize = marks[MARK_END];
    296   1.8  christos 		kernsize = (kernsize + 1023) / 1024;
    297   1.8  christos 
    298   1.8  christos 		loadaddr = xmsalloc(kernsize);
    299   1.8  christos 		if (!loadaddr)
    300  1.21  junyoung 			return ENOMEM;
    301   1.8  christos 	}
    302   1.8  christos #endif
    303  1.63    nonaka #ifdef EFIBOOT
    304  1.63    nonaka 	{
    305  1.63    nonaka 		EFI_STATUS status;
    306  1.63    nonaka 		EFI_PHYSICAL_ADDRESS addr;
    307  1.63    nonaka 		UINTN kernsize;
    308  1.63    nonaka 
    309  1.63    nonaka 		marks[MARK_START] = loadaddr;
    310  1.63    nonaka 		if ((fd = loadfile(file, marks, COUNT_KERNEL)) == -1)
    311  1.63    nonaka 			return EIO;
    312  1.63    nonaka 		close(fd);
    313  1.63    nonaka 
    314  1.63    nonaka 		/* Allocate temporary arena. */
    315  1.63    nonaka 		addr = EFI_ALLOCATE_MAX_ADDRESS;
    316  1.63    nonaka 		kernsize = marks[MARK_END] - loadaddr;
    317  1.63    nonaka 		kernsize += 1 * 1024 * 1024;	/* XXX: kernel size COUNT_KERNEL vs LOAD_KERNL (lacked some SYMTAB?) */
    318  1.63    nonaka 		kernsize = EFI_SIZE_TO_PAGES(kernsize);
    319  1.63    nonaka 		status = uefi_call_wrapper(BS->AllocatePages, 4,
    320  1.63    nonaka 		    AllocateMaxAddress, EfiLoaderData, kernsize, &addr);
    321  1.63    nonaka 		if (EFI_ERROR(status))
    322  1.63    nonaka 			return ENOMEM;
    323  1.63    nonaka 		efi_loadaddr = loadaddr = addr;
    324  1.63    nonaka 
    325  1.63    nonaka 		memset(marks, 0, sizeof(marks[0]) * MARK_MAX);
    326  1.63    nonaka 	}
    327  1.63    nonaka #endif
    328   1.7  christos 	marks[MARK_START] = loadaddr;
    329  1.29  christos 	if ((fd = loadfile(file, marks,
    330  1.44  christos 	    LOAD_KERNEL & ~(floppy ? LOAD_BACKWARDS : 0))) == -1)
    331  1.32     joerg 		return EIO;
    332   1.1     perry 
    333   1.8  christos 	close(fd);
    334  1.10  drochner 
    335  1.50       dsl 	/* If the root fs type is unusual, load its module. */
    336  1.50       dsl 	if (fsmod != NULL)
    337  1.57  jakllsch 		module_add_common(fsmod, BM_TYPE_KMOD);
    338  1.34        ad 
    339  1.10  drochner 	/*
    340  1.10  drochner 	 * Gather some information for the kernel. Do this after the
    341  1.10  drochner 	 * "point of no return" to avoid memory leaks.
    342  1.10  drochner 	 * (but before DOS might be trashed in the XMS case)
    343  1.10  drochner 	 */
    344  1.10  drochner #ifdef PASS_BIOSGEOM
    345  1.10  drochner 	bi_getbiosgeom();
    346  1.10  drochner #endif
    347  1.10  drochner #ifdef PASS_MEMMAP
    348  1.10  drochner 	bi_getmemmap();
    349  1.10  drochner #endif
    350   1.8  christos 
    351   1.8  christos #ifdef XMS
    352   1.8  christos 	if (loadaddr != origaddr) {
    353   1.8  christos 		/*
    354  1.14      ross 		 * We now have done our last DOS IO, so we may
    355   1.8  christos 		 * trash the OS. Copy the data from the temporary
    356  1.20       wiz 		 * buffer to its real address.
    357   1.8  christos 		 */
    358   1.8  christos 		marks[MARK_START] -= loadaddr;
    359   1.8  christos 		marks[MARK_END] -= loadaddr;
    360   1.8  christos 		marks[MARK_SYM] -= loadaddr;
    361   1.8  christos 		marks[MARK_END] -= loadaddr;
    362   1.8  christos 		ppbcopy(loadaddr, origaddr, marks[MARK_END]);
    363   1.8  christos 	}
    364   1.8  christos #endif
    365  1.63    nonaka #ifdef EFIBOOT
    366  1.63    nonaka 	marks[MARK_START] -= loadaddr;
    367  1.63    nonaka 	marks[MARK_ENTRY] -= loadaddr;
    368  1.63    nonaka 	marks[MARK_DATA] -= loadaddr;
    369  1.63    nonaka 	/* MARK_NSYM */
    370  1.63    nonaka 	marks[MARK_SYM] -= loadaddr;
    371  1.63    nonaka 	marks[MARK_END] -= loadaddr;
    372  1.63    nonaka 	/* Copy the kernel to original load address later. */
    373  1.63    nonaka #endif
    374   1.8  christos 	marks[MARK_END] = (((u_long) marks[MARK_END] + sizeof(int) - 1)) &
    375   1.8  christos 	    (-sizeof(int));
    376  1.23        ad 	image_end = marks[MARK_END];
    377  1.23        ad 	kernel_loaded = true;
    378   1.1     perry 
    379  1.32     joerg 	return 0;
    380  1.32     joerg }
    381  1.32     joerg 
    382  1.32     joerg int
    383  1.40        ad exec_netbsd(const char *file, physaddr_t loadaddr, int boothowto, int floppy,
    384  1.62      maxv     void (*callback)(void))
    385  1.32     joerg {
    386  1.62      maxv 	uint32_t boot_argv[BOOT_NARGS];
    387  1.62      maxv 	u_long marks[MARK_MAX];
    388  1.32     joerg 	struct btinfo_symtab btinfo_symtab;
    389  1.62      maxv 	u_long extmem;
    390  1.62      maxv 	u_long basemem;
    391  1.63    nonaka 	int error;
    392  1.32     joerg 
    393  1.32     joerg #ifdef	DEBUG
    394  1.62      maxv 	printf("exec: file=%s loadaddr=0x%lx\n", file ? file : "NULL",
    395  1.62      maxv 	    loadaddr);
    396  1.32     joerg #endif
    397  1.32     joerg 
    398  1.61      maxv 	BI_ALLOC(BTINFO_MAX);
    399  1.32     joerg 
    400  1.32     joerg 	BI_ADD(&btinfo_console, BTINFO_CONSOLE, sizeof(struct btinfo_console));
    401  1.32     joerg 
    402  1.40        ad 	howto = boothowto;
    403  1.40        ad 
    404  1.56  jakllsch 	memset(marks, 0, sizeof(marks));
    405  1.56  jakllsch 
    406  1.63    nonaka 	error = common_load_kernel(file, &basemem, &extmem, loadaddr, floppy,
    407  1.63    nonaka 	    marks);
    408  1.63    nonaka 	if (error) {
    409  1.63    nonaka 		errno = error;
    410  1.32     joerg 		goto out;
    411  1.63    nonaka 	}
    412  1.32     joerg 
    413  1.31     joerg 	boot_argv[0] = boothowto;
    414  1.31     joerg 	boot_argv[1] = 0;
    415  1.31     joerg 	boot_argv[2] = vtophys(bootinfo);	/* old cyl offset */
    416  1.31     joerg 	boot_argv[3] = marks[MARK_END];
    417  1.31     joerg 	boot_argv[4] = extmem;
    418  1.31     joerg 	boot_argv[5] = basemem;
    419  1.31     joerg 
    420  1.23        ad 	/* pull in any modules if necessary */
    421  1.23        ad 	if (boot_modules_enabled) {
    422  1.41  jmcneill 		module_init(file);
    423  1.23        ad 		if (btinfo_modulelist) {
    424  1.23        ad 			BI_ADD(btinfo_modulelist, BTINFO_MODULELIST,
    425  1.23        ad 			    btinfo_modulelist_size);
    426  1.23        ad 		}
    427  1.23        ad 	}
    428   1.1     perry 
    429  1.47  uebayasi 	userconf_init();
    430  1.47  uebayasi 	if (btinfo_userconfcommands != NULL)
    431  1.47  uebayasi 		BI_ADD(btinfo_userconfcommands, BTINFO_USERCONFCOMMANDS,
    432  1.58  jakllsch 		    btinfo_userconfcommands_size);
    433  1.47  uebayasi 
    434   1.1     perry #ifdef DEBUG
    435   1.8  christos 	printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", marks[MARK_ENTRY],
    436   1.7  christos 	    marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]);
    437   1.1     perry #endif
    438   1.1     perry 
    439   1.8  christos 	btinfo_symtab.nsym = marks[MARK_NSYM];
    440   1.8  christos 	btinfo_symtab.ssym = marks[MARK_SYM];
    441   1.8  christos 	btinfo_symtab.esym = marks[MARK_END];
    442   1.8  christos 	BI_ADD(&btinfo_symtab, BTINFO_SYMTAB, sizeof(struct btinfo_symtab));
    443   1.8  christos 
    444  1.42  jmcneill 	/* set new video mode if necessary */
    445  1.42  jmcneill 	vbe_commit();
    446  1.42  jmcneill 	BI_ADD(&btinfo_framebuffer, BTINFO_FRAMEBUFFER,
    447  1.42  jmcneill 	    sizeof(struct btinfo_framebuffer));
    448  1.42  jmcneill 
    449  1.40        ad 	if (callback != NULL)
    450  1.40        ad 		(*callback)();
    451  1.63    nonaka #ifdef EFIBOOT
    452  1.63    nonaka 	/* Copy the kernel to original load address. */
    453  1.63    nonaka 	memmove((void *)marks[MARK_START],
    454  1.63    nonaka 	    (void *)(efi_loadaddr + marks[MARK_START]),
    455  1.63    nonaka 	    marks[MARK_END] - marks[MARK_START]);
    456  1.63    nonaka #endif
    457  1.15       dbj 	startprog(marks[MARK_ENTRY], BOOT_NARGS, boot_argv,
    458  1.62      maxv 	    x86_trunc_page(basemem * 1024));
    459   1.1     perry 	panic("exec returned");
    460   1.1     perry 
    461   1.4  drochner out:
    462   1.4  drochner 	BI_FREE();
    463  1.60      maxv 	bootinfo = NULL;
    464  1.21  junyoung 	return -1;
    465   1.1     perry }
    466  1.23        ad 
    467  1.41  jmcneill static void
    468  1.41  jmcneill extract_device(const char *path, char *buf, size_t buflen)
    469  1.41  jmcneill {
    470  1.59  jakllsch 	size_t i;
    471  1.41  jmcneill 
    472  1.41  jmcneill 	if (strchr(path, ':') != NULL) {
    473  1.41  jmcneill 		for (i = 0; i < buflen - 2 && path[i] != ':'; i++)
    474  1.41  jmcneill 			buf[i] = path[i];
    475  1.41  jmcneill 		buf[i++] = ':';
    476  1.41  jmcneill 		buf[i] = '\0';
    477  1.41  jmcneill 	} else
    478  1.41  jmcneill 		buf[0] = '\0';
    479  1.41  jmcneill }
    480  1.41  jmcneill 
    481  1.26        ad static const char *
    482  1.41  jmcneill module_path(boot_module_t *bm, const char *kdev)
    483  1.26        ad {
    484  1.26        ad 	static char buf[256];
    485  1.41  jmcneill 	char name_buf[256], dev_buf[64];
    486  1.41  jmcneill 	const char *name, *name2, *p;
    487  1.26        ad 
    488  1.26        ad 	name = bm->bm_path;
    489  1.33     joerg 	for (name2 = name; *name2; ++name2) {
    490  1.33     joerg 		if (*name2 == ' ' || *name2 == '\t') {
    491  1.33     joerg 			strlcpy(name_buf, name, sizeof(name_buf));
    492  1.59  jakllsch 			if ((uintptr_t)name2 - (uintptr_t)name < sizeof(name_buf))
    493  1.33     joerg 				name_buf[name2 - name] = '\0';
    494  1.33     joerg 			name = name_buf;
    495  1.33     joerg 			break;
    496  1.33     joerg 		}
    497  1.33     joerg 	}
    498  1.41  jmcneill 	if ((p = strchr(name, ':')) != NULL) {
    499  1.41  jmcneill 		/* device specified, use it */
    500  1.41  jmcneill 		if (p[1] == '/')
    501  1.41  jmcneill 			snprintf(buf, sizeof(buf), "%s", name);
    502  1.41  jmcneill 		else {
    503  1.41  jmcneill 			p++;
    504  1.41  jmcneill 			extract_device(name, dev_buf, sizeof(dev_buf));
    505  1.41  jmcneill 			snprintf(buf, sizeof(buf), "%s%s/%s/%s.kmod",
    506  1.41  jmcneill 			    dev_buf, module_base, p, p);
    507  1.41  jmcneill 		}
    508  1.41  jmcneill 	} else {
    509  1.41  jmcneill 		/* device not specified; load from kernel device if known */
    510  1.58  jakllsch 		if (name[0] == '/')
    511  1.41  jmcneill 			snprintf(buf, sizeof(buf), "%s%s", kdev, name);
    512  1.41  jmcneill 		else
    513  1.41  jmcneill 			snprintf(buf, sizeof(buf), "%s%s/%s/%s.kmod",
    514  1.41  jmcneill 			    kdev, module_base, name, name);
    515  1.41  jmcneill 	}
    516  1.33     joerg 
    517  1.26        ad 	return buf;
    518  1.26        ad }
    519  1.26        ad 
    520  1.36  christos static int
    521  1.41  jmcneill module_open(boot_module_t *bm, int mode, const char *kdev, bool doload)
    522  1.28     chris {
    523  1.28     chris 	int fd;
    524  1.28     chris 	const char *path;
    525  1.36  christos 
    526  1.28     chris 	/* check the expanded path first */
    527  1.41  jmcneill 	path = module_path(bm, kdev);
    528  1.28     chris 	fd = open(path, mode);
    529  1.41  jmcneill 	if (fd != -1) {
    530  1.41  jmcneill 		if ((howto & AB_SILENT) == 0 && doload)
    531  1.41  jmcneill 			printf("Loading %s ", path);
    532  1.41  jmcneill 	} else {
    533  1.28     chris 		/* now attempt the raw path provided */
    534  1.28     chris 		fd = open(bm->bm_path, mode);
    535  1.41  jmcneill 		if (fd != -1 && (howto & AB_SILENT) == 0 && doload)
    536  1.41  jmcneill 			printf("Loading %s ", bm->bm_path);
    537  1.41  jmcneill 	}
    538  1.41  jmcneill 	if (!doload && fd == -1) {
    539  1.41  jmcneill 		printf("WARNING: couldn't open %s", bm->bm_path);
    540  1.41  jmcneill 		if (strcmp(bm->bm_path, path) != 0)
    541  1.41  jmcneill 			printf(" (%s)", path);
    542  1.41  jmcneill 		printf("\n");
    543  1.28     chris 	}
    544  1.28     chris 	return fd;
    545  1.28     chris }
    546  1.28     chris 
    547  1.23        ad static void
    548  1.41  jmcneill module_init(const char *kernel_path)
    549  1.23        ad {
    550  1.23        ad 	struct bi_modulelist_entry *bi;
    551  1.23        ad 	struct stat st;
    552  1.30     joerg 	const char *machine;
    553  1.41  jmcneill 	char kdev[64];
    554  1.23        ad 	char *buf;
    555  1.23        ad 	boot_module_t *bm;
    556  1.59  jakllsch 	ssize_t len;
    557  1.23        ad 	off_t off;
    558  1.41  jmcneill 	int err, fd, nfail = 0;
    559  1.41  jmcneill 
    560  1.41  jmcneill 	extract_device(kernel_path, kdev, sizeof(kdev));
    561  1.23        ad 
    562  1.30     joerg 	switch (netbsd_elf_class) {
    563  1.30     joerg 	case ELFCLASS32:
    564  1.30     joerg 		machine = "i386";
    565  1.30     joerg 		break;
    566  1.30     joerg 	case ELFCLASS64:
    567  1.30     joerg 		machine = "amd64";
    568  1.30     joerg 		break;
    569  1.30     joerg 	default:
    570  1.30     joerg 		machine = "generic";
    571  1.30     joerg 		break;
    572  1.30     joerg 	}
    573  1.30     joerg 	if (netbsd_version / 1000000 % 100 == 99) {
    574  1.30     joerg 		/* -current */
    575  1.30     joerg 		snprintf(module_base, sizeof(module_base),
    576  1.38     rmind 		    "/stand/%s/%d.%d.%d/modules", machine,
    577  1.30     joerg 		    netbsd_version / 100000000,
    578  1.30     joerg 		    netbsd_version / 1000000 % 100,
    579  1.30     joerg 		    netbsd_version / 100 % 100);
    580  1.30     joerg 	} else if (netbsd_version != 0) {
    581  1.30     joerg 		/* release */
    582  1.30     joerg 		snprintf(module_base, sizeof(module_base),
    583  1.38     rmind 		    "/stand/%s/%d.%d/modules", machine,
    584  1.30     joerg 		    netbsd_version / 100000000,
    585  1.30     joerg 		    netbsd_version / 1000000 % 100);
    586  1.30     joerg 	}
    587  1.30     joerg 
    588  1.23        ad 	/* First, see which modules are valid and calculate btinfo size */
    589  1.23        ad 	len = sizeof(struct btinfo_modulelist);
    590  1.23        ad 	for (bm = boot_modules; bm; bm = bm->bm_next) {
    591  1.41  jmcneill 		fd = module_open(bm, 0, kdev, false);
    592  1.23        ad 		if (fd == -1) {
    593  1.23        ad 			bm->bm_len = -1;
    594  1.41  jmcneill 			++nfail;
    595  1.23        ad 			continue;
    596  1.23        ad 		}
    597  1.23        ad 		err = fstat(fd, &st);
    598  1.24        ad 		if (err == -1 || st.st_size == -1) {
    599  1.28     chris 			printf("WARNING: couldn't stat %s\n", bm->bm_path);
    600  1.23        ad 			close(fd);
    601  1.23        ad 			bm->bm_len = -1;
    602  1.41  jmcneill 			++nfail;
    603  1.23        ad 			continue;
    604  1.23        ad 		}
    605  1.23        ad 		bm->bm_len = st.st_size;
    606  1.23        ad 		close(fd);
    607  1.23        ad 		len += sizeof(struct bi_modulelist_entry);
    608  1.23        ad 	}
    609  1.23        ad 
    610  1.23        ad 	/* Allocate the module list */
    611  1.23        ad 	btinfo_modulelist = alloc(len);
    612  1.23        ad 	if (btinfo_modulelist == NULL) {
    613  1.23        ad 		printf("WARNING: couldn't allocate module list\n");
    614  1.43   tsutsui 		wait_sec(MODULE_WARNING_SEC);
    615  1.23        ad 		return;
    616  1.23        ad 	}
    617  1.23        ad 	memset(btinfo_modulelist, 0, len);
    618  1.23        ad 	btinfo_modulelist_size = len;
    619  1.23        ad 
    620  1.23        ad 	/* Fill in btinfo structure */
    621  1.23        ad 	buf = (char *)btinfo_modulelist;
    622  1.23        ad 	btinfo_modulelist->num = 0;
    623  1.23        ad 	off = sizeof(struct btinfo_modulelist);
    624  1.23        ad 
    625  1.23        ad 	for (bm = boot_modules; bm; bm = bm->bm_next) {
    626  1.23        ad 		if (bm->bm_len == -1)
    627  1.23        ad 			continue;
    628  1.41  jmcneill 		fd = module_open(bm, 0, kdev, true);
    629  1.41  jmcneill 		if (fd == -1)
    630  1.23        ad 			continue;
    631  1.24        ad 		image_end = (image_end + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
    632  1.52  jakllsch 		len = pread(fd, (void *)(uintptr_t)image_end, SSIZE_MAX);
    633  1.24        ad 		if (len < bm->bm_len) {
    634  1.40        ad 			if ((howto & AB_SILENT) != 0)
    635  1.40        ad 				printf("Loading %s ", bm->bm_path);
    636  1.23        ad 			printf(" FAILED\n");
    637  1.23        ad 		} else {
    638  1.23        ad 			btinfo_modulelist->num++;
    639  1.23        ad 			bi = (struct bi_modulelist_entry *)(buf + off);
    640  1.23        ad 			off += sizeof(struct bi_modulelist_entry);
    641  1.23        ad 			strncpy(bi->path, bm->bm_path, sizeof(bi->path) - 1);
    642  1.24        ad 			bi->base = image_end;
    643  1.24        ad 			bi->len = len;
    644  1.49       tls 			switch (bm->bm_type) {
    645  1.49       tls 			    case BM_TYPE_KMOD:
    646  1.49       tls 				bi->type = BI_MODULE_ELF;
    647  1.49       tls 				break;
    648  1.49       tls 			    case BM_TYPE_IMAGE:
    649  1.49       tls 				bi->type = BI_MODULE_IMAGE;
    650  1.49       tls 				break;
    651  1.51  jmcneill 			    case BM_TYPE_FS:
    652  1.51  jmcneill 				bi->type = BI_MODULE_FS;
    653  1.51  jmcneill 				break;
    654  1.49       tls 			    case BM_TYPE_RND:
    655  1.49       tls 			    default:
    656  1.49       tls 				/* safest -- rnd checks the sha1 */
    657  1.49       tls 				bi->type = BI_MODULE_RND;
    658  1.49       tls 				break;
    659  1.49       tls 			}
    660  1.40        ad 			if ((howto & AB_SILENT) == 0)
    661  1.40        ad 				printf(" \n");
    662  1.23        ad 		}
    663  1.24        ad 		if (len > 0)
    664  1.24        ad 			image_end += len;
    665  1.23        ad 		close(fd);
    666  1.23        ad 	}
    667  1.23        ad 	btinfo_modulelist->endpa = image_end;
    668  1.41  jmcneill 
    669  1.41  jmcneill 	if (nfail > 0) {
    670  1.41  jmcneill 		printf("WARNING: %d module%s failed to load\n",
    671  1.41  jmcneill 		    nfail, nfail == 1 ? "" : "s");
    672  1.41  jmcneill #if notyet
    673  1.43   tsutsui 		wait_sec(MODULE_WARNING_SEC);
    674  1.41  jmcneill #endif
    675  1.41  jmcneill 	}
    676  1.23        ad }
    677  1.33     joerg 
    678  1.47  uebayasi static void
    679  1.47  uebayasi userconf_init(void)
    680  1.47  uebayasi {
    681  1.47  uebayasi 	size_t count, len;
    682  1.47  uebayasi 	userconf_command_t *uc;
    683  1.47  uebayasi 	char *buf;
    684  1.47  uebayasi 	off_t off;
    685  1.47  uebayasi 
    686  1.47  uebayasi 	/* Calculate the userconf commands list size */
    687  1.47  uebayasi 	count = 0;
    688  1.47  uebayasi 	for (uc = userconf_commands; uc != NULL; uc = uc->uc_next)
    689  1.47  uebayasi 		count++;
    690  1.55  jakllsch 	len = sizeof(*btinfo_userconfcommands) +
    691  1.47  uebayasi 	      count * sizeof(struct bi_userconfcommand);
    692  1.47  uebayasi 
    693  1.47  uebayasi 	/* Allocate the userconf commands list */
    694  1.47  uebayasi 	btinfo_userconfcommands = alloc(len);
    695  1.47  uebayasi 	if (btinfo_userconfcommands == NULL) {
    696  1.47  uebayasi 		printf("WARNING: couldn't allocate userconf commands list\n");
    697  1.47  uebayasi 		return;
    698  1.47  uebayasi 	}
    699  1.47  uebayasi 	memset(btinfo_userconfcommands, 0, len);
    700  1.47  uebayasi 	btinfo_userconfcommands_size = len;
    701  1.47  uebayasi 
    702  1.47  uebayasi 	/* Fill in btinfo structure */
    703  1.47  uebayasi 	buf = (char *)btinfo_userconfcommands;
    704  1.47  uebayasi 	off = sizeof(*btinfo_userconfcommands);
    705  1.47  uebayasi 	btinfo_userconfcommands->num = 0;
    706  1.47  uebayasi 	for (uc = userconf_commands; uc != NULL; uc = uc->uc_next) {
    707  1.47  uebayasi 		struct bi_userconfcommand *bi;
    708  1.47  uebayasi 		bi = (struct bi_userconfcommand *)(buf + off);
    709  1.47  uebayasi 		strncpy(bi->text, uc->uc_text, sizeof(bi->text) - 1);
    710  1.47  uebayasi 
    711  1.47  uebayasi 		off += sizeof(*bi);
    712  1.47  uebayasi 		btinfo_userconfcommands->num++;
    713  1.47  uebayasi 	}
    714  1.47  uebayasi }
    715  1.47  uebayasi 
    716  1.33     joerg int
    717  1.33     joerg exec_multiboot(const char *file, char *args)
    718  1.33     joerg {
    719  1.33     joerg 	struct multiboot_info *mbi;
    720  1.33     joerg 	struct multiboot_module *mbm;
    721  1.33     joerg 	struct bi_modulelist_entry *bim;
    722  1.62      maxv 	int i, len;
    723  1.62      maxv 	u_long marks[MARK_MAX];
    724  1.62      maxv 	u_long extmem;
    725  1.62      maxv 	u_long basemem;
    726  1.62      maxv 	char *cmdline;
    727  1.33     joerg 
    728  1.33     joerg 	mbi = alloc(sizeof(struct multiboot_info));
    729  1.33     joerg 	mbi->mi_flags = MULTIBOOT_INFO_HAS_MEMORY;
    730  1.33     joerg 
    731  1.33     joerg 	if (common_load_kernel(file, &basemem, &extmem, 0, 0, marks))
    732  1.33     joerg 		goto out;
    733  1.33     joerg 
    734  1.33     joerg 	mbi->mi_mem_upper = extmem;
    735  1.33     joerg 	mbi->mi_mem_lower = basemem;
    736  1.33     joerg 
    737  1.33     joerg 	if (args) {
    738  1.33     joerg 		mbi->mi_flags |= MULTIBOOT_INFO_HAS_CMDLINE;
    739  1.33     joerg 		len = strlen(file) + 1 + strlen(args) + 1;
    740  1.33     joerg 		cmdline = alloc(len);
    741  1.33     joerg 		snprintf(cmdline, len, "%s %s", file, args);
    742  1.33     joerg 		mbi->mi_cmdline = (char *) vtophys(cmdline);
    743  1.33     joerg 	}
    744  1.33     joerg 
    745  1.33     joerg 	/* pull in any modules if necessary */
    746  1.33     joerg 	if (boot_modules_enabled) {
    747  1.41  jmcneill 		module_init(file);
    748  1.33     joerg 		if (btinfo_modulelist) {
    749  1.33     joerg 			mbm = alloc(sizeof(struct multiboot_module) *
    750  1.33     joerg 					   btinfo_modulelist->num);
    751  1.33     joerg 
    752  1.33     joerg 			bim = (struct bi_modulelist_entry *)
    753  1.33     joerg 			  (((char *) btinfo_modulelist) +
    754  1.33     joerg 			   sizeof(struct btinfo_modulelist));
    755  1.33     joerg 			for (i = 0; i < btinfo_modulelist->num; i++) {
    756  1.33     joerg 				mbm[i].mmo_start = bim->base;
    757  1.33     joerg 				mbm[i].mmo_end = bim->base + bim->len;
    758  1.33     joerg 				mbm[i].mmo_string = (char *)vtophys(bim->path);
    759  1.33     joerg 				mbm[i].mmo_reserved = 0;
    760  1.33     joerg 				bim++;
    761  1.33     joerg 			}
    762  1.33     joerg 			mbi->mi_flags |= MULTIBOOT_INFO_HAS_MODS;
    763  1.33     joerg 			mbi->mi_mods_count = btinfo_modulelist->num;
    764  1.33     joerg 			mbi->mi_mods_addr = vtophys(mbm);
    765  1.33     joerg 		}
    766  1.33     joerg 	}
    767  1.33     joerg 
    768  1.33     joerg #ifdef DEBUG
    769  1.33     joerg 	printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", marks[MARK_ENTRY],
    770  1.33     joerg 	    marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]);
    771  1.33     joerg #endif
    772  1.33     joerg 
    773  1.33     joerg #if 0
    774  1.33     joerg 	if (btinfo_symtab.nsym) {
    775  1.33     joerg 		mbi->mi_flags |= MULTIBOOT_INFO_HAS_ELF_SYMS;
    776  1.33     joerg 		mbi->mi_elfshdr_addr = marks[MARK_SYM];
    777  1.33     joerg 	btinfo_symtab.nsym = marks[MARK_NSYM];
    778  1.33     joerg 	btinfo_symtab.ssym = marks[MARK_SYM];
    779  1.33     joerg 	btinfo_symtab.esym = marks[MARK_END];
    780  1.33     joerg #endif
    781  1.33     joerg 
    782  1.33     joerg 	multiboot(marks[MARK_ENTRY], vtophys(mbi),
    783  1.62      maxv 	    x86_trunc_page(mbi->mi_mem_lower * 1024));
    784  1.33     joerg 	panic("exec returned");
    785  1.33     joerg 
    786  1.33     joerg out:
    787  1.62      maxv 	dealloc(mbi, 0);
    788  1.33     joerg 	return -1;
    789  1.33     joerg }
    790  1.40        ad 
    791  1.40        ad void
    792  1.40        ad x86_progress(const char *fmt, ...)
    793  1.40        ad {
    794  1.40        ad 	va_list ap;
    795  1.40        ad 
    796  1.40        ad 	if ((howto & AB_SILENT) != 0)
    797  1.40        ad 		return;
    798  1.40        ad 	va_start(ap, fmt);
    799  1.40        ad 	vprintf(fmt, ap);
    800  1.40        ad 	va_end(ap);
    801  1.40        ad }
    802