Home | History | Annotate | Line # | Download | only in lib
exec.c revision 1.22.74.3
      1 /*	$NetBSD: exec.c,v 1.22.74.3 2009/01/17 13:28:05 mjf Exp $	 */
      2 
      3 /*-
      4  * Copyright (c) 2008 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  * 3. All advertising materials mentioning features or use of this software
     75  *    must display the following acknowledgement:
     76  *	This product includes software developed by the University of
     77  *	California, Berkeley and its contributors.
     78  * 4. Neither the name of the University nor the names of its contributors
     79  *    may be used to endorse or promote products derived from this software
     80  *    without specific prior written permission.
     81  *
     82  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     83  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     84  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     85  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     86  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     87  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     88  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     89  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     90  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     91  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     92  * SUCH DAMAGE.
     93  *
     94  * 	@(#)boot.c	8.1 (Berkeley) 6/10/93
     95  */
     96 
     97 /*
     98  * starts NetBSD a.out kernel
     99  * needs lowlevel startup from startprog.S
    100  * This is a special version of exec.c to support use of XMS.
    101  */
    102 
    103 #include <sys/param.h>
    104 #include <sys/reboot.h>
    105 
    106 #include <machine/multiboot.h>
    107 
    108 #include <lib/libsa/stand.h>
    109 #include <lib/libkern/libkern.h>
    110 
    111 #include "loadfile.h"
    112 #include "libi386.h"
    113 #include "bootinfo.h"
    114 #include "bootmod.h"
    115 #ifdef SUPPORT_PS2
    116 #include "biosmca.h"
    117 #endif
    118 
    119 #define BOOT_NARGS	6
    120 
    121 #ifndef	PAGE_SIZE
    122 #define	PAGE_SIZE	4096
    123 #endif
    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 static struct btinfo_modulelist *btinfo_modulelist;
    132 static size_t btinfo_modulelist_size;
    133 static uint32_t image_end;
    134 static char module_base[64] = "/";
    135 
    136 static void	module_init(void);
    137 
    138 void
    139 module_add(char *name)
    140 {
    141 	boot_module_t *bm, *bmp;
    142 	size_t len;
    143 	char *str;
    144 
    145 	while (*name == ' ' || *name == '\t')
    146 		++name;
    147 
    148 	bm = alloc(sizeof(boot_module_t));
    149 	len = strlen(name) + 1;
    150 	str = alloc(len);
    151 	if (bm == NULL || str == NULL) {
    152 		printf("couldn't allocate module\n");
    153 		return;
    154 	}
    155 	memcpy(str, name, len);
    156 	bm->bm_path = str;
    157 	bm->bm_next = NULL;
    158 	if (boot_modules == NULL)
    159 		boot_modules = bm;
    160 	else {
    161 		for (bmp = boot_modules; bmp->bm_next;
    162 		    bmp = bmp->bm_next)
    163 			;
    164 		bmp->bm_next = bm;
    165 	}
    166 }
    167 
    168 static int
    169 common_load_kernel(const char *file, u_long *basemem, u_long *extmem,
    170     physaddr_t loadaddr, int floppy, u_long marks[MARK_MAX])
    171 {
    172 	int fd;
    173 #ifdef XMS
    174 	u_long		xmsmem;
    175 	physaddr_t	origaddr = loadaddr;
    176 #endif
    177 
    178 	*extmem = getextmem();
    179 	*basemem = getbasemem();
    180 
    181 #ifdef XMS
    182 	if ((getextmem1() == 0) && (xmsmem = checkxms())) {
    183 	        u_long kernsize;
    184 
    185 		/*
    186 		 * With "CONSERVATIVE_MEMDETECT", extmem is 0 because
    187 		 *  getextmem() is getextmem1(). Without, the "smart"
    188 		 *  methods could fail to report all memory as well.
    189 		 * xmsmem is a few kB less than the actual size, but
    190 		 *  better than nothing.
    191 		 */
    192 		if (xmsmem > *extmem)
    193 			*extmem = xmsmem;
    194 		/*
    195 		 * Get the size of the kernel
    196 		 */
    197 		marks[MARK_START] = loadaddr;
    198 		if ((fd = loadfile(file, marks, COUNT_KERNEL)) == -1)
    199 			return EIO;
    200 		close(fd);
    201 
    202 		kernsize = marks[MARK_END];
    203 		kernsize = (kernsize + 1023) / 1024;
    204 
    205 		loadaddr = xmsalloc(kernsize);
    206 		if (!loadaddr)
    207 			return ENOMEM;
    208 	}
    209 #endif
    210 	marks[MARK_START] = loadaddr;
    211 	if ((fd = loadfile(file, marks,
    212 	    LOAD_KERNEL & ~(floppy ? LOAD_NOTE : 0))) == -1)
    213 		return EIO;
    214 
    215 	close(fd);
    216 
    217 	/* Now we know the root fs type, load modules for it. */
    218 	module_add(fsmod);
    219 	if (fsmod2 != NULL && strcmp(fsmod, fsmod2) != 0)
    220 		module_add(fsmod2);
    221 
    222 	/*
    223 	 * Gather some information for the kernel. Do this after the
    224 	 * "point of no return" to avoid memory leaks.
    225 	 * (but before DOS might be trashed in the XMS case)
    226 	 */
    227 #ifdef PASS_BIOSGEOM
    228 	bi_getbiosgeom();
    229 #endif
    230 #ifdef PASS_MEMMAP
    231 	bi_getmemmap();
    232 #endif
    233 
    234 #ifdef XMS
    235 	if (loadaddr != origaddr) {
    236 		/*
    237 		 * We now have done our last DOS IO, so we may
    238 		 * trash the OS. Copy the data from the temporary
    239 		 * buffer to its real address.
    240 		 */
    241 		marks[MARK_START] -= loadaddr;
    242 		marks[MARK_END] -= loadaddr;
    243 		marks[MARK_SYM] -= loadaddr;
    244 		marks[MARK_END] -= loadaddr;
    245 		ppbcopy(loadaddr, origaddr, marks[MARK_END]);
    246 	}
    247 #endif
    248 	marks[MARK_END] = (((u_long) marks[MARK_END] + sizeof(int) - 1)) &
    249 	    (-sizeof(int));
    250 	image_end = marks[MARK_END];
    251 	kernel_loaded = true;
    252 
    253 	return 0;
    254 }
    255 
    256 int
    257 exec_netbsd(const char *file, physaddr_t loadaddr, int boothowto, int floppy)
    258 {
    259 	u_long          boot_argv[BOOT_NARGS];
    260 	u_long		marks[MARK_MAX];
    261 	struct btinfo_symtab btinfo_symtab;
    262 	u_long		extmem;
    263 	u_long		basemem;
    264 
    265 #ifdef	DEBUG
    266 	printf("exec: file=%s loadaddr=0x%lx\n",
    267 	       file ? file : "NULL", loadaddr);
    268 #endif
    269 
    270 	BI_ALLOC(32); /* ??? */
    271 
    272 	BI_ADD(&btinfo_console, BTINFO_CONSOLE, sizeof(struct btinfo_console));
    273 
    274 	if (common_load_kernel(file, &basemem, &extmem, loadaddr, floppy, marks))
    275 		goto out;
    276 
    277 	boot_argv[0] = boothowto;
    278 	boot_argv[1] = 0;
    279 	boot_argv[2] = vtophys(bootinfo);	/* old cyl offset */
    280 	boot_argv[3] = marks[MARK_END];
    281 	boot_argv[4] = extmem;
    282 	boot_argv[5] = basemem;
    283 
    284 	/* pull in any modules if necessary */
    285 	if (boot_modules_enabled) {
    286 		module_init();
    287 		if (btinfo_modulelist) {
    288 			BI_ADD(btinfo_modulelist, BTINFO_MODULELIST,
    289 			    btinfo_modulelist_size);
    290 		}
    291 	}
    292 
    293 #ifdef DEBUG
    294 	printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", marks[MARK_ENTRY],
    295 	    marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]);
    296 #endif
    297 
    298 	btinfo_symtab.nsym = marks[MARK_NSYM];
    299 	btinfo_symtab.ssym = marks[MARK_SYM];
    300 	btinfo_symtab.esym = marks[MARK_END];
    301 	BI_ADD(&btinfo_symtab, BTINFO_SYMTAB, sizeof(struct btinfo_symtab));
    302 
    303 	startprog(marks[MARK_ENTRY], BOOT_NARGS, boot_argv,
    304 		  x86_trunc_page(basemem*1024));
    305 	panic("exec returned");
    306 
    307 out:
    308 	BI_FREE();
    309 	bootinfo = 0;
    310 	return -1;
    311 }
    312 
    313 static const char *
    314 module_path(boot_module_t *bm)
    315 {
    316 	static char buf[256];
    317 	char name_buf[256];
    318 	const char *name, *name2;
    319 
    320 	name = bm->bm_path;
    321 	for (name2 = name; *name2; ++name2) {
    322 		if (*name2 == ' ' || *name2 == '\t') {
    323 			strlcpy(name_buf, name, sizeof(name_buf));
    324 			if (name2 - name < sizeof(name_buf))
    325 				name_buf[name2 - name] = '\0';
    326 			name = name_buf;
    327 			break;
    328 		}
    329 	}
    330  	if (name[0] == '/')
    331 		snprintf(buf, sizeof(buf), "%s", name);
    332 	else
    333 		snprintf(buf, sizeof(buf), "%s/%s/%s.kmod",
    334 		    module_base, name, name);
    335 
    336 	return buf;
    337 }
    338 
    339 static int
    340 module_open(boot_module_t *bm, int mode)
    341 {
    342 	int fd;
    343 	const char *path;
    344 
    345 	/* check the expanded path first */
    346 	path = module_path(bm);
    347 	fd = open(path, mode);
    348 	if (fd == -1) {
    349 		printf("WARNING: couldn't open %s\n", path);
    350 		/* now attempt the raw path provided */
    351 		fd = open(bm->bm_path, mode);
    352 		if (fd == -1)
    353 			printf("WARNING: couldn't open %s\n", bm->bm_path);
    354 	}
    355 	return fd;
    356 }
    357 
    358 static void
    359 module_init(void)
    360 {
    361 	struct bi_modulelist_entry *bi;
    362 	struct stat st;
    363 	const char *machine;
    364 	char *buf;
    365 	boot_module_t *bm;
    366 	size_t len;
    367 	off_t off;
    368 	int err, fd;
    369 
    370 	switch (netbsd_elf_class) {
    371 	case ELFCLASS32:
    372 		machine = "i386";
    373 		break;
    374 	case ELFCLASS64:
    375 		machine = "amd64";
    376 		break;
    377 	default:
    378 		machine = "generic";
    379 		break;
    380 	}
    381 	if (netbsd_version / 1000000 % 100 == 99) {
    382 		/* -current */
    383 		snprintf(module_base, sizeof(module_base),
    384 		    "/stand/%s/%d.%d.%d/modules", machine,
    385 		    netbsd_version / 100000000,
    386 		    netbsd_version / 1000000 % 100,
    387 		    netbsd_version / 100 % 100);
    388 	} else if (netbsd_version != 0) {
    389 		/* release */
    390 		snprintf(module_base, sizeof(module_base),
    391 		    "/stand/%s/%d.%d/modules", machine,
    392 		    netbsd_version / 100000000,
    393 		    netbsd_version / 1000000 % 100);
    394 	}
    395 
    396 	/* First, see which modules are valid and calculate btinfo size */
    397 	len = sizeof(struct btinfo_modulelist);
    398 	for (bm = boot_modules; bm; bm = bm->bm_next) {
    399 		fd = module_open(bm, 0);
    400 		if (fd == -1) {
    401 			bm->bm_len = -1;
    402 			continue;
    403 		}
    404 		err = fstat(fd, &st);
    405 		if (err == -1 || st.st_size == -1) {
    406 			printf("WARNING: couldn't stat %s\n", bm->bm_path);
    407 			close(fd);
    408 			bm->bm_len = -1;
    409 			continue;
    410 		}
    411 		bm->bm_len = st.st_size;
    412 		close(fd);
    413 		len += sizeof(struct bi_modulelist_entry);
    414 	}
    415 
    416 	/* Allocate the module list */
    417 	btinfo_modulelist = alloc(len);
    418 	if (btinfo_modulelist == NULL) {
    419 		printf("WARNING: couldn't allocate module list\n");
    420 		return;
    421 	}
    422 	memset(btinfo_modulelist, 0, len);
    423 	btinfo_modulelist_size = len;
    424 
    425 	/* Fill in btinfo structure */
    426 	buf = (char *)btinfo_modulelist;
    427 	btinfo_modulelist->num = 0;
    428 	off = sizeof(struct btinfo_modulelist);
    429 
    430 	for (bm = boot_modules; bm; bm = bm->bm_next) {
    431 		if (bm->bm_len == -1)
    432 			continue;
    433 		printf("Loading %s ", bm->bm_path);
    434 		fd = module_open(bm, 0);
    435 		if (fd == -1) {
    436 			printf("ERROR: couldn't open %s\n", bm->bm_path);
    437 			continue;
    438 		}
    439 		image_end = (image_end + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
    440 		len = pread(fd, (void *)image_end, SSIZE_MAX);
    441 		if (len < bm->bm_len) {
    442 			printf(" FAILED\n");
    443 		} else {
    444 			btinfo_modulelist->num++;
    445 			bi = (struct bi_modulelist_entry *)(buf + off);
    446 			off += sizeof(struct bi_modulelist_entry);
    447 			strncpy(bi->path, bm->bm_path, sizeof(bi->path) - 1);
    448 			bi->base = image_end;
    449 			bi->len = len;
    450 			bi->type = BI_MODULE_ELF;
    451 			printf(" \n");
    452 		}
    453 		if (len > 0)
    454 			image_end += len;
    455 		close(fd);
    456 	}
    457 	btinfo_modulelist->endpa = image_end;
    458 }
    459 
    460 int
    461 exec_multiboot(const char *file, char *args)
    462 {
    463 	struct multiboot_info *mbi;
    464 	struct multiboot_module *mbm;
    465 	struct bi_modulelist_entry *bim;
    466 	int		i, len;
    467 	u_long		marks[MARK_MAX];
    468 	u_long		extmem;
    469 	u_long		basemem;
    470 	char		*cmdline;
    471 
    472 	mbi = alloc(sizeof(struct multiboot_info));
    473 	mbi->mi_flags = MULTIBOOT_INFO_HAS_MEMORY;
    474 
    475 	if (common_load_kernel(file, &basemem, &extmem, 0, 0, marks))
    476 		goto out;
    477 
    478 	mbi->mi_mem_upper = extmem;
    479 	mbi->mi_mem_lower = basemem;
    480 
    481 	if (args) {
    482 		mbi->mi_flags |= MULTIBOOT_INFO_HAS_CMDLINE;
    483 		len = strlen(file) + 1 + strlen(args) + 1;
    484 		cmdline = alloc(len);
    485 		snprintf(cmdline, len, "%s %s", file, args);
    486 		mbi->mi_cmdline = (char *) vtophys(cmdline);
    487 	}
    488 
    489 	/* pull in any modules if necessary */
    490 	if (boot_modules_enabled) {
    491 		module_init();
    492 		if (btinfo_modulelist) {
    493 			mbm = alloc(sizeof(struct multiboot_module) *
    494 					   btinfo_modulelist->num);
    495 
    496 			bim = (struct bi_modulelist_entry *)
    497 			  (((char *) btinfo_modulelist) +
    498 			   sizeof(struct btinfo_modulelist));
    499 			for (i = 0; i < btinfo_modulelist->num; i++) {
    500 				mbm[i].mmo_start = bim->base;
    501 				mbm[i].mmo_end = bim->base + bim->len;
    502 				mbm[i].mmo_string = (char *)vtophys(bim->path);
    503 				mbm[i].mmo_reserved = 0;
    504 				bim++;
    505 			}
    506 			mbi->mi_flags |= MULTIBOOT_INFO_HAS_MODS;
    507 			mbi->mi_mods_count = btinfo_modulelist->num;
    508 			mbi->mi_mods_addr = vtophys(mbm);
    509 		}
    510 	}
    511 
    512 #ifdef DEBUG
    513 	printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", marks[MARK_ENTRY],
    514 	    marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]);
    515 #endif
    516 
    517 
    518 #if 0
    519 	if (btinfo_symtab.nsym) {
    520 		mbi->mi_flags |= MULTIBOOT_INFO_HAS_ELF_SYMS;
    521 		mbi->mi_elfshdr_addr = marks[MARK_SYM];
    522 	btinfo_symtab.nsym = marks[MARK_NSYM];
    523 	btinfo_symtab.ssym = marks[MARK_SYM];
    524 	btinfo_symtab.esym = marks[MARK_END];
    525 #endif
    526 
    527 	multiboot(marks[MARK_ENTRY], vtophys(mbi),
    528 		  x86_trunc_page(mbi->mi_mem_lower*1024));
    529 	panic("exec returned");
    530 
    531 out:
    532         dealloc(mbi, 0);
    533 	return -1;
    534 }
    535