exec.c revision 1.20
1/*	$NetBSD: exec.c,v 1.20 2003/11/10 08:51:52 wiz Exp $	 */
2
3/*
4 * Copyright (c) 1982, 1986, 1990, 1993
5 *	The Regents of the University of California.  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 * 3. Neither the name of the University nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * 	@(#)boot.c	8.1 (Berkeley) 6/10/93
32 */
33
34/*
35 * Copyright (c) 1996
36 *	Matthias Drochner.  All rights reserved.
37 * Copyright (c) 1996
38 * 	Perry E. Metzger.  All rights reserved.
39 * Copyright (c) 1997
40 * 	Martin Husemann.  All rights reserved.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 *    notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 *    notice, this list of conditions and the following disclaimer in the
49 *    documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 *    must display the following acknowledgement:
52 *	This product includes software developed by the University of
53 *	California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 *    may be used to endorse or promote products derived from this software
56 *    without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 *
70 * 	@(#)boot.c	8.1 (Berkeley) 6/10/93
71 */
72
73/*
74 * starts NetBSD a.out kernel
75 * needs lowlevel startup from startprog.S
76 * This is a special version of exec.c to support use of XMS.
77 */
78
79#include <sys/param.h>
80#include <sys/reboot.h>
81
82#include <lib/libsa/stand.h>
83
84#include "loadfile.h"
85#include "libi386.h"
86#include "bootinfo.h"
87#ifdef SUPPORT_PS2
88#include "biosmca.h"
89#endif
90
91#define BOOT_NARGS	6
92
93extern struct btinfo_console btinfo_console;
94
95int
96exec_netbsd(file, loadaddr, boothowto)
97	const char     *file;
98	physaddr_t      loadaddr;
99	int             boothowto;
100{
101	u_long          boot_argv[BOOT_NARGS];
102	int		fd;
103	u_long		marks[MARK_MAX];
104	struct btinfo_symtab btinfo_symtab;
105	u_long		extmem;
106	u_long		basemem;
107#ifdef XMS
108	u_long		xmsmem;
109	physaddr_t	origaddr = loadaddr;
110#endif
111
112#ifdef	DEBUG
113	printf("exec: file=%s loadaddr=0x%lx\n",
114	       file ? file : "NULL", loadaddr);
115#endif
116
117	BI_ALLOC(6); /* ??? */
118
119	BI_ADD(&btinfo_console, BTINFO_CONSOLE, sizeof(struct btinfo_console));
120
121	extmem = getextmem();
122	basemem = getbasemem();
123
124#ifdef XMS
125	if ((getextmem1() == 0) && (xmsmem = checkxms())) {
126	        u_long kernsize;
127
128		/*
129		 * With "CONSERVATIVE_MEMDETECT", extmem is 0 because
130		 *  getextmem() is getextmem1(). Without, the "smart"
131		 *  methods could fail to report all memory as well.
132		 * xmsmem is a few kB less than the actual size, but
133		 *  better than nothing.
134		 */
135		if (xmsmem > extmem)
136			extmem = xmsmem;
137		/*
138		 * Get the size of the kernel
139		 */
140		marks[MARK_START] = loadaddr;
141		if ((fd = loadfile(file, marks, COUNT_KERNEL)) == -1)
142			goto out;
143		close(fd);
144
145		kernsize = marks[MARK_END];
146		kernsize = (kernsize + 1023) / 1024;
147
148		loadaddr = xmsalloc(kernsize);
149		if (!loadaddr)
150			return(ENOMEM);
151	}
152#endif
153	marks[MARK_START] = loadaddr;
154	if ((fd = loadfile(file, marks, LOAD_KERNEL)) == -1)
155		goto out;
156
157	boot_argv[0] = boothowto;
158	boot_argv[1] = 0;
159	boot_argv[2] = vtophys(bootinfo);	/* old cyl offset */
160	/* argv[3] below */
161	boot_argv[4] = extmem;
162	boot_argv[5] = basemem;
163
164	close(fd);
165
166	/*
167	 * Gather some information for the kernel. Do this after the
168	 * "point of no return" to avoid memory leaks.
169	 * (but before DOS might be trashed in the XMS case)
170	 */
171#ifdef PASS_BIOSGEOM
172	bi_getbiosgeom();
173#endif
174#ifdef PASS_MEMMAP
175	bi_getmemmap();
176#endif
177
178#ifdef XMS
179	if (loadaddr != origaddr) {
180		/*
181		 * We now have done our last DOS IO, so we may
182		 * trash the OS. Copy the data from the temporary
183		 * buffer to its real address.
184		 */
185		marks[MARK_START] -= loadaddr;
186		marks[MARK_END] -= loadaddr;
187		marks[MARK_SYM] -= loadaddr;
188		marks[MARK_END] -= loadaddr;
189		ppbcopy(loadaddr, origaddr, marks[MARK_END]);
190	}
191#endif
192	marks[MARK_END] = (((u_long) marks[MARK_END] + sizeof(int) - 1)) &
193	    (-sizeof(int));
194
195	boot_argv[3] = marks[MARK_END];
196
197
198#ifdef DEBUG
199	printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", marks[MARK_ENTRY],
200	    marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]);
201#endif
202
203	btinfo_symtab.nsym = marks[MARK_NSYM];
204	btinfo_symtab.ssym = marks[MARK_SYM];
205	btinfo_symtab.esym = marks[MARK_END];
206	BI_ADD(&btinfo_symtab, BTINFO_SYMTAB, sizeof(struct btinfo_symtab));
207
208	startprog(marks[MARK_ENTRY], BOOT_NARGS, boot_argv,
209		x86_trunc_page(basemem*1024));
210	panic("exec returned");
211
212out:
213	BI_FREE();
214	bootinfo = 0;
215	return (-1);
216}
217