xref: /netbsd-src/sys/arch/i386/stand/lib/exec.c (revision b5677b36047b601b9addaaa494a58ceae82c2a6c)
1 /*	$NetBSD: exec.c,v 1.40 2009/03/21 15:01:56 ad 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  * 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 #include <sys/reboot.h>
106 
107 #include <machine/multiboot.h>
108 #include <machine/stdarg.h>
109 
110 #include <lib/libsa/stand.h>
111 #include <lib/libkern/libkern.h>
112 
113 #include "loadfile.h"
114 #include "libi386.h"
115 #include "bootinfo.h"
116 #include "bootmod.h"
117 #ifdef SUPPORT_PS2
118 #include "biosmca.h"
119 #endif
120 
121 #define BOOT_NARGS	6
122 
123 #ifndef	PAGE_SIZE
124 #define	PAGE_SIZE	4096
125 #endif
126 
127 extern struct btinfo_console btinfo_console;
128 
129 boot_module_t *boot_modules;
130 bool boot_modules_enabled = true;
131 bool kernel_loaded;
132 
133 static struct btinfo_framebuffer btinfo_framebuffer;
134 
135 static struct btinfo_modulelist *btinfo_modulelist;
136 static size_t btinfo_modulelist_size;
137 static uint32_t image_end;
138 static char module_base[64] = "/";
139 static int howto;
140 
141 static void	module_init(void);
142 
143 void
144 framebuffer_configure(struct btinfo_framebuffer *fb)
145 {
146 	if (fb)
147 		btinfo_framebuffer = *fb;
148 	else {
149 		btinfo_framebuffer.physaddr = 0;
150 		btinfo_framebuffer.flags = 0;
151 	}
152 }
153 
154 void
155 module_add(char *name)
156 {
157 	boot_module_t *bm, *bmp;
158 	size_t len;
159 	char *str;
160 
161 	while (*name == ' ' || *name == '\t')
162 		++name;
163 
164 	bm = alloc(sizeof(boot_module_t));
165 	len = strlen(name) + 1;
166 	str = alloc(len);
167 	if (bm == NULL || str == NULL) {
168 		printf("couldn't allocate module\n");
169 		return;
170 	}
171 	memcpy(str, name, len);
172 	bm->bm_path = str;
173 	bm->bm_next = NULL;
174 	if (boot_modules == NULL)
175 		boot_modules = bm;
176 	else {
177 		for (bmp = boot_modules; bmp->bm_next;
178 		    bmp = bmp->bm_next)
179 			;
180 		bmp->bm_next = bm;
181 	}
182 }
183 
184 static int
185 common_load_kernel(const char *file, u_long *basemem, u_long *extmem,
186     physaddr_t loadaddr, int floppy, u_long marks[MARK_MAX])
187 {
188 	int fd;
189 #ifdef XMS
190 	u_long		xmsmem;
191 	physaddr_t	origaddr = loadaddr;
192 #endif
193 
194 	*extmem = getextmem();
195 	*basemem = getbasemem();
196 
197 #ifdef XMS
198 	if ((getextmem1() == 0) && (xmsmem = checkxms())) {
199 	        u_long kernsize;
200 
201 		/*
202 		 * With "CONSERVATIVE_MEMDETECT", extmem is 0 because
203 		 *  getextmem() is getextmem1(). Without, the "smart"
204 		 *  methods could fail to report all memory as well.
205 		 * xmsmem is a few kB less than the actual size, but
206 		 *  better than nothing.
207 		 */
208 		if (xmsmem > *extmem)
209 			*extmem = xmsmem;
210 		/*
211 		 * Get the size of the kernel
212 		 */
213 		marks[MARK_START] = loadaddr;
214 		if ((fd = loadfile(file, marks, COUNT_KERNEL)) == -1)
215 			return EIO;
216 		close(fd);
217 
218 		kernsize = marks[MARK_END];
219 		kernsize = (kernsize + 1023) / 1024;
220 
221 		loadaddr = xmsalloc(kernsize);
222 		if (!loadaddr)
223 			return ENOMEM;
224 	}
225 #endif
226 	marks[MARK_START] = loadaddr;
227 	if ((fd = loadfile(file, marks,
228 	    LOAD_KERNEL & ~(floppy ? LOAD_NOTE : 0))) == -1)
229 		return EIO;
230 
231 	close(fd);
232 
233 	/* Now we know the root fs type, load modules for it. */
234 	module_add(fsmod);
235 	if (fsmod2 != NULL && strcmp(fsmod, fsmod2) != 0)
236 		module_add(fsmod2);
237 
238 	/*
239 	 * Gather some information for the kernel. Do this after the
240 	 * "point of no return" to avoid memory leaks.
241 	 * (but before DOS might be trashed in the XMS case)
242 	 */
243 #ifdef PASS_BIOSGEOM
244 	bi_getbiosgeom();
245 #endif
246 #ifdef PASS_MEMMAP
247 	bi_getmemmap();
248 #endif
249 
250 #ifdef XMS
251 	if (loadaddr != origaddr) {
252 		/*
253 		 * We now have done our last DOS IO, so we may
254 		 * trash the OS. Copy the data from the temporary
255 		 * buffer to its real address.
256 		 */
257 		marks[MARK_START] -= loadaddr;
258 		marks[MARK_END] -= loadaddr;
259 		marks[MARK_SYM] -= loadaddr;
260 		marks[MARK_END] -= loadaddr;
261 		ppbcopy(loadaddr, origaddr, marks[MARK_END]);
262 	}
263 #endif
264 	marks[MARK_END] = (((u_long) marks[MARK_END] + sizeof(int) - 1)) &
265 	    (-sizeof(int));
266 	image_end = marks[MARK_END];
267 	kernel_loaded = true;
268 
269 	return 0;
270 }
271 
272 int
273 exec_netbsd(const char *file, physaddr_t loadaddr, int boothowto, int floppy,
274 	    void (*callback)(void))
275 {
276 	u_long          boot_argv[BOOT_NARGS];
277 	u_long		marks[MARK_MAX];
278 	struct btinfo_symtab btinfo_symtab;
279 	u_long		extmem;
280 	u_long		basemem;
281 
282 #ifdef	DEBUG
283 	printf("exec: file=%s loadaddr=0x%lx\n",
284 	       file ? file : "NULL", loadaddr);
285 #endif
286 
287 	BI_ALLOC(32); /* ??? */
288 
289 	BI_ADD(&btinfo_console, BTINFO_CONSOLE, sizeof(struct btinfo_console));
290 	BI_ADD(&btinfo_framebuffer, BTINFO_FRAMEBUFFER,
291 	    sizeof(struct btinfo_framebuffer));
292 
293 	howto = boothowto;
294 
295 	if (common_load_kernel(file, &basemem, &extmem, loadaddr, floppy, marks))
296 		goto out;
297 
298 	boot_argv[0] = boothowto;
299 	boot_argv[1] = 0;
300 	boot_argv[2] = vtophys(bootinfo);	/* old cyl offset */
301 	boot_argv[3] = marks[MARK_END];
302 	boot_argv[4] = extmem;
303 	boot_argv[5] = basemem;
304 
305 	/* pull in any modules if necessary */
306 	if (boot_modules_enabled) {
307 		module_init();
308 		if (btinfo_modulelist) {
309 			BI_ADD(btinfo_modulelist, BTINFO_MODULELIST,
310 			    btinfo_modulelist_size);
311 		}
312 	}
313 
314 #ifdef DEBUG
315 	printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", marks[MARK_ENTRY],
316 	    marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]);
317 #endif
318 
319 	btinfo_symtab.nsym = marks[MARK_NSYM];
320 	btinfo_symtab.ssym = marks[MARK_SYM];
321 	btinfo_symtab.esym = marks[MARK_END];
322 	BI_ADD(&btinfo_symtab, BTINFO_SYMTAB, sizeof(struct btinfo_symtab));
323 
324 	if (callback != NULL)
325 		(*callback)();
326 	startprog(marks[MARK_ENTRY], BOOT_NARGS, boot_argv,
327 		  x86_trunc_page(basemem*1024));
328 	panic("exec returned");
329 
330 out:
331 	BI_FREE();
332 	bootinfo = 0;
333 	return -1;
334 }
335 
336 static const char *
337 module_path(boot_module_t *bm)
338 {
339 	static char buf[256];
340 	char name_buf[256];
341 	const char *name, *name2;
342 
343 	name = bm->bm_path;
344 	for (name2 = name; *name2; ++name2) {
345 		if (*name2 == ' ' || *name2 == '\t') {
346 			strlcpy(name_buf, name, sizeof(name_buf));
347 			if (name2 - name < sizeof(name_buf))
348 				name_buf[name2 - name] = '\0';
349 			name = name_buf;
350 			break;
351 		}
352 	}
353  	if (name[0] == '/')
354 		snprintf(buf, sizeof(buf), "%s", name);
355 	else
356 		snprintf(buf, sizeof(buf), "%s/%s/%s.kmod",
357 		    module_base, name, name);
358 
359 	return buf;
360 }
361 
362 static int
363 module_open(boot_module_t *bm, int mode)
364 {
365 	int fd;
366 	const char *path;
367 
368 	/* check the expanded path first */
369 	path = module_path(bm);
370 	fd = open(path, mode);
371 	if (fd == -1) {
372 		printf("WARNING: couldn't open %s\n", path);
373 		/* now attempt the raw path provided */
374 		fd = open(bm->bm_path, mode);
375 		if (fd == -1)
376 			printf("WARNING: couldn't open %s\n", bm->bm_path);
377 	}
378 	return fd;
379 }
380 
381 static void
382 module_init(void)
383 {
384 	struct bi_modulelist_entry *bi;
385 	struct stat st;
386 	const char *machine;
387 	char *buf;
388 	boot_module_t *bm;
389 	size_t len;
390 	off_t off;
391 	int err, fd;
392 
393 	switch (netbsd_elf_class) {
394 	case ELFCLASS32:
395 		machine = "i386";
396 		break;
397 	case ELFCLASS64:
398 		machine = "amd64";
399 		break;
400 	default:
401 		machine = "generic";
402 		break;
403 	}
404 	if (netbsd_version / 1000000 % 100 == 99) {
405 		/* -current */
406 		snprintf(module_base, sizeof(module_base),
407 		    "/stand/%s/%d.%d.%d/modules", machine,
408 		    netbsd_version / 100000000,
409 		    netbsd_version / 1000000 % 100,
410 		    netbsd_version / 100 % 100);
411 	} else if (netbsd_version != 0) {
412 		/* release */
413 		snprintf(module_base, sizeof(module_base),
414 		    "/stand/%s/%d.%d/modules", machine,
415 		    netbsd_version / 100000000,
416 		    netbsd_version / 1000000 % 100);
417 	}
418 
419 	/* First, see which modules are valid and calculate btinfo size */
420 	len = sizeof(struct btinfo_modulelist);
421 	for (bm = boot_modules; bm; bm = bm->bm_next) {
422 		fd = module_open(bm, 0);
423 		if (fd == -1) {
424 			bm->bm_len = -1;
425 			continue;
426 		}
427 		err = fstat(fd, &st);
428 		if (err == -1 || st.st_size == -1) {
429 			printf("WARNING: couldn't stat %s\n", bm->bm_path);
430 			close(fd);
431 			bm->bm_len = -1;
432 			continue;
433 		}
434 		bm->bm_len = st.st_size;
435 		close(fd);
436 		len += sizeof(struct bi_modulelist_entry);
437 	}
438 
439 	/* Allocate the module list */
440 	btinfo_modulelist = alloc(len);
441 	if (btinfo_modulelist == NULL) {
442 		printf("WARNING: couldn't allocate module list\n");
443 		return;
444 	}
445 	memset(btinfo_modulelist, 0, len);
446 	btinfo_modulelist_size = len;
447 
448 	/* Fill in btinfo structure */
449 	buf = (char *)btinfo_modulelist;
450 	btinfo_modulelist->num = 0;
451 	off = sizeof(struct btinfo_modulelist);
452 
453 	for (bm = boot_modules; bm; bm = bm->bm_next) {
454 		if (bm->bm_len == -1)
455 			continue;
456 		if ((howto & AB_SILENT) == 0)
457 			printf("Loading %s ", bm->bm_path);
458 		fd = module_open(bm, 0);
459 		if (fd == -1) {
460 			printf("ERROR: couldn't open %s\n", bm->bm_path);
461 			continue;
462 		}
463 		image_end = (image_end + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
464 		len = pread(fd, (void *)image_end, SSIZE_MAX);
465 		if (len < bm->bm_len) {
466 			if ((howto & AB_SILENT) != 0)
467 				printf("Loading %s ", bm->bm_path);
468 			printf(" FAILED\n");
469 		} else {
470 			btinfo_modulelist->num++;
471 			bi = (struct bi_modulelist_entry *)(buf + off);
472 			off += sizeof(struct bi_modulelist_entry);
473 			strncpy(bi->path, bm->bm_path, sizeof(bi->path) - 1);
474 			bi->base = image_end;
475 			bi->len = len;
476 			bi->type = BI_MODULE_ELF;
477 			if ((howto & AB_SILENT) == 0)
478 				printf(" \n");
479 		}
480 		if (len > 0)
481 			image_end += len;
482 		close(fd);
483 	}
484 	btinfo_modulelist->endpa = image_end;
485 }
486 
487 int
488 exec_multiboot(const char *file, char *args)
489 {
490 	struct multiboot_info *mbi;
491 	struct multiboot_module *mbm;
492 	struct bi_modulelist_entry *bim;
493 	int		i, len;
494 	u_long		marks[MARK_MAX];
495 	u_long		extmem;
496 	u_long		basemem;
497 	char		*cmdline;
498 
499 	mbi = alloc(sizeof(struct multiboot_info));
500 	mbi->mi_flags = MULTIBOOT_INFO_HAS_MEMORY;
501 
502 	if (common_load_kernel(file, &basemem, &extmem, 0, 0, marks))
503 		goto out;
504 
505 	mbi->mi_mem_upper = extmem;
506 	mbi->mi_mem_lower = basemem;
507 
508 	if (args) {
509 		mbi->mi_flags |= MULTIBOOT_INFO_HAS_CMDLINE;
510 		len = strlen(file) + 1 + strlen(args) + 1;
511 		cmdline = alloc(len);
512 		snprintf(cmdline, len, "%s %s", file, args);
513 		mbi->mi_cmdline = (char *) vtophys(cmdline);
514 	}
515 
516 	/* pull in any modules if necessary */
517 	if (boot_modules_enabled) {
518 		module_init();
519 		if (btinfo_modulelist) {
520 			mbm = alloc(sizeof(struct multiboot_module) *
521 					   btinfo_modulelist->num);
522 
523 			bim = (struct bi_modulelist_entry *)
524 			  (((char *) btinfo_modulelist) +
525 			   sizeof(struct btinfo_modulelist));
526 			for (i = 0; i < btinfo_modulelist->num; i++) {
527 				mbm[i].mmo_start = bim->base;
528 				mbm[i].mmo_end = bim->base + bim->len;
529 				mbm[i].mmo_string = (char *)vtophys(bim->path);
530 				mbm[i].mmo_reserved = 0;
531 				bim++;
532 			}
533 			mbi->mi_flags |= MULTIBOOT_INFO_HAS_MODS;
534 			mbi->mi_mods_count = btinfo_modulelist->num;
535 			mbi->mi_mods_addr = vtophys(mbm);
536 		}
537 	}
538 
539 #ifdef DEBUG
540 	printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", marks[MARK_ENTRY],
541 	    marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]);
542 #endif
543 
544 
545 #if 0
546 	if (btinfo_symtab.nsym) {
547 		mbi->mi_flags |= MULTIBOOT_INFO_HAS_ELF_SYMS;
548 		mbi->mi_elfshdr_addr = marks[MARK_SYM];
549 	btinfo_symtab.nsym = marks[MARK_NSYM];
550 	btinfo_symtab.ssym = marks[MARK_SYM];
551 	btinfo_symtab.esym = marks[MARK_END];
552 #endif
553 
554 	multiboot(marks[MARK_ENTRY], vtophys(mbi),
555 		  x86_trunc_page(mbi->mi_mem_lower*1024));
556 	panic("exec returned");
557 
558 out:
559         dealloc(mbi, 0);
560 	return -1;
561 }
562 
563 void
564 x86_progress(const char *fmt, ...)
565 {
566 	va_list ap;
567 
568 	if ((howto & AB_SILENT) != 0)
569 		return;
570 	va_start(ap, fmt);
571 	vprintf(fmt, ap);
572 	va_end(ap);
573 }
574