exec.c revision 1.22.76.1 1 1.22.76.1 yamt /* $NetBSD: exec.c,v 1.22.76.1 2008/05/18 12:32:17 yamt Exp $ */
2 1.22.76.1 yamt
3 1.22.76.1 yamt /*-
4 1.22.76.1 yamt * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 1.22.76.1 yamt * All rights reserved.
6 1.22.76.1 yamt *
7 1.22.76.1 yamt * Redistribution and use in source and binary forms, with or without
8 1.22.76.1 yamt * modification, are permitted provided that the following conditions
9 1.22.76.1 yamt * are met:
10 1.22.76.1 yamt * 1. Redistributions of source code must retain the above copyright
11 1.22.76.1 yamt * notice, this list of conditions and the following disclaimer.
12 1.22.76.1 yamt * 2. Redistributions in binary form must reproduce the above copyright
13 1.22.76.1 yamt * notice, this list of conditions and the following disclaimer in the
14 1.22.76.1 yamt * documentation and/or other materials provided with the distribution.
15 1.22.76.1 yamt *
16 1.22.76.1 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.22.76.1 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.22.76.1 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.22.76.1 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.22.76.1 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.22.76.1 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.22.76.1 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.22.76.1 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.22.76.1 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.22.76.1 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.22.76.1 yamt * POSSIBILITY OF SUCH DAMAGE.
27 1.22.76.1 yamt */
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 * 3. All advertising materials mentioning features or use of this software
75 1.1 perry * must display the following acknowledgement:
76 1.1 perry * This product includes software developed by the University of
77 1.1 perry * California, Berkeley and its contributors.
78 1.1 perry * 4. Neither the name of the University nor the names of its contributors
79 1.1 perry * may be used to endorse or promote products derived from this software
80 1.1 perry * without specific prior written permission.
81 1.1 perry *
82 1.1 perry * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
83 1.1 perry * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
84 1.1 perry * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
85 1.1 perry * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
86 1.1 perry * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
87 1.1 perry * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
88 1.1 perry * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
89 1.1 perry * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
90 1.1 perry * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
91 1.1 perry * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
92 1.1 perry * SUCH DAMAGE.
93 1.1 perry *
94 1.1 perry * @(#)boot.c 8.1 (Berkeley) 6/10/93
95 1.1 perry */
96 1.1 perry
97 1.2 thorpej /*
98 1.8 christos * starts NetBSD a.out kernel
99 1.8 christos * needs lowlevel startup from startprog.S
100 1.8 christos * This is a special version of exec.c to support use of XMS.
101 1.1 perry */
102 1.8 christos
103 1.1 perry #include <sys/param.h>
104 1.1 perry #include <sys/reboot.h>
105 1.1 perry
106 1.1 perry #include <lib/libsa/stand.h>
107 1.22.76.1 yamt #include <lib/libkern/libkern.h>
108 1.4 drochner
109 1.6 christos #include "loadfile.h"
110 1.1 perry #include "libi386.h"
111 1.4 drochner #include "bootinfo.h"
112 1.22.76.1 yamt #include "bootmod.h"
113 1.16 jdolecek #ifdef SUPPORT_PS2
114 1.16 jdolecek #include "biosmca.h"
115 1.16 jdolecek #endif
116 1.1 perry
117 1.6 christos #define BOOT_NARGS 6
118 1.1 perry
119 1.22.76.1 yamt #ifndef PAGE_SIZE
120 1.22.76.1 yamt #define PAGE_SIZE 4096
121 1.22.76.1 yamt #endif
122 1.22.76.1 yamt
123 1.4 drochner extern struct btinfo_console btinfo_console;
124 1.4 drochner
125 1.22.76.1 yamt boot_module_t *boot_modules;
126 1.22.76.1 yamt bool boot_modules_enabled = true;
127 1.22.76.1 yamt bool kernel_loaded;
128 1.22.76.1 yamt
129 1.22.76.1 yamt static struct btinfo_modulelist *btinfo_modulelist;
130 1.22.76.1 yamt static size_t btinfo_modulelist_size;
131 1.22.76.1 yamt static uint32_t image_end;
132 1.22.76.1 yamt
133 1.22.76.1 yamt static void module_init(void);
134 1.22.76.1 yamt
135 1.21 junyoung int
136 1.21 junyoung exec_netbsd(const char *file, physaddr_t loadaddr, int boothowto)
137 1.1 perry {
138 1.6 christos u_long boot_argv[BOOT_NARGS];
139 1.6 christos int fd;
140 1.8 christos u_long marks[MARK_MAX];
141 1.8 christos struct btinfo_symtab btinfo_symtab;
142 1.8 christos u_long extmem;
143 1.15 dbj u_long basemem;
144 1.8 christos #ifdef XMS
145 1.8 christos u_long xmsmem;
146 1.8 christos physaddr_t origaddr = loadaddr;
147 1.8 christos #endif
148 1.8 christos
149 1.1 perry #ifdef DEBUG
150 1.13 drochner printf("exec: file=%s loadaddr=0x%lx\n",
151 1.13 drochner file ? file : "NULL", loadaddr);
152 1.1 perry #endif
153 1.4 drochner
154 1.22.76.1 yamt BI_ALLOC(32); /* ??? */
155 1.4 drochner
156 1.4 drochner BI_ADD(&btinfo_console, BTINFO_CONSOLE, sizeof(struct btinfo_console));
157 1.4 drochner
158 1.8 christos extmem = getextmem();
159 1.15 dbj basemem = getbasemem();
160 1.8 christos
161 1.8 christos #ifdef XMS
162 1.8 christos if ((getextmem1() == 0) && (xmsmem = checkxms())) {
163 1.8 christos u_long kernsize;
164 1.8 christos
165 1.8 christos /*
166 1.8 christos * With "CONSERVATIVE_MEMDETECT", extmem is 0 because
167 1.8 christos * getextmem() is getextmem1(). Without, the "smart"
168 1.8 christos * methods could fail to report all memory as well.
169 1.8 christos * xmsmem is a few kB less than the actual size, but
170 1.8 christos * better than nothing.
171 1.8 christos */
172 1.8 christos if (xmsmem > extmem)
173 1.8 christos extmem = xmsmem;
174 1.21 junyoung /*
175 1.8 christos * Get the size of the kernel
176 1.8 christos */
177 1.8 christos marks[MARK_START] = loadaddr;
178 1.12 christos if ((fd = loadfile(file, marks, COUNT_KERNEL)) == -1)
179 1.8 christos goto out;
180 1.8 christos close(fd);
181 1.8 christos
182 1.8 christos kernsize = marks[MARK_END];
183 1.8 christos kernsize = (kernsize + 1023) / 1024;
184 1.8 christos
185 1.8 christos loadaddr = xmsalloc(kernsize);
186 1.8 christos if (!loadaddr)
187 1.21 junyoung return ENOMEM;
188 1.8 christos }
189 1.8 christos #endif
190 1.7 christos marks[MARK_START] = loadaddr;
191 1.12 christos if ((fd = loadfile(file, marks, LOAD_KERNEL)) == -1)
192 1.4 drochner goto out;
193 1.1 perry
194 1.1 perry boot_argv[0] = boothowto;
195 1.5 drochner boot_argv[1] = 0;
196 1.8 christos boot_argv[2] = vtophys(bootinfo); /* old cyl offset */
197 1.8 christos /* argv[3] below */
198 1.8 christos boot_argv[4] = extmem;
199 1.15 dbj boot_argv[5] = basemem;
200 1.8 christos
201 1.8 christos close(fd);
202 1.10 drochner
203 1.10 drochner /*
204 1.10 drochner * Gather some information for the kernel. Do this after the
205 1.10 drochner * "point of no return" to avoid memory leaks.
206 1.10 drochner * (but before DOS might be trashed in the XMS case)
207 1.10 drochner */
208 1.10 drochner #ifdef PASS_BIOSGEOM
209 1.10 drochner bi_getbiosgeom();
210 1.10 drochner #endif
211 1.10 drochner #ifdef PASS_MEMMAP
212 1.10 drochner bi_getmemmap();
213 1.10 drochner #endif
214 1.8 christos
215 1.8 christos #ifdef XMS
216 1.8 christos if (loadaddr != origaddr) {
217 1.8 christos /*
218 1.14 ross * We now have done our last DOS IO, so we may
219 1.8 christos * trash the OS. Copy the data from the temporary
220 1.20 wiz * buffer to its real address.
221 1.8 christos */
222 1.8 christos marks[MARK_START] -= loadaddr;
223 1.8 christos marks[MARK_END] -= loadaddr;
224 1.8 christos marks[MARK_SYM] -= loadaddr;
225 1.8 christos marks[MARK_END] -= loadaddr;
226 1.8 christos ppbcopy(loadaddr, origaddr, marks[MARK_END]);
227 1.8 christos }
228 1.8 christos #endif
229 1.8 christos marks[MARK_END] = (((u_long) marks[MARK_END] + sizeof(int) - 1)) &
230 1.8 christos (-sizeof(int));
231 1.7 christos boot_argv[3] = marks[MARK_END];
232 1.22.76.1 yamt image_end = marks[MARK_END];
233 1.22.76.1 yamt kernel_loaded = true;
234 1.1 perry
235 1.22.76.1 yamt /* pull in any modules if necessary */
236 1.22.76.1 yamt if (boot_modules_enabled) {
237 1.22.76.1 yamt module_init();
238 1.22.76.1 yamt if (btinfo_modulelist) {
239 1.22.76.1 yamt BI_ADD(btinfo_modulelist, BTINFO_MODULELIST,
240 1.22.76.1 yamt btinfo_modulelist_size);
241 1.22.76.1 yamt }
242 1.22.76.1 yamt }
243 1.1 perry
244 1.1 perry #ifdef DEBUG
245 1.8 christos printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", marks[MARK_ENTRY],
246 1.7 christos marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]);
247 1.1 perry #endif
248 1.1 perry
249 1.8 christos btinfo_symtab.nsym = marks[MARK_NSYM];
250 1.8 christos btinfo_symtab.ssym = marks[MARK_SYM];
251 1.8 christos btinfo_symtab.esym = marks[MARK_END];
252 1.8 christos BI_ADD(&btinfo_symtab, BTINFO_SYMTAB, sizeof(struct btinfo_symtab));
253 1.8 christos
254 1.15 dbj startprog(marks[MARK_ENTRY], BOOT_NARGS, boot_argv,
255 1.21 junyoung x86_trunc_page(basemem*1024));
256 1.1 perry panic("exec returned");
257 1.1 perry
258 1.4 drochner out:
259 1.4 drochner BI_FREE();
260 1.4 drochner bootinfo = 0;
261 1.21 junyoung return -1;
262 1.1 perry }
263 1.22.76.1 yamt
264 1.22.76.1 yamt static void
265 1.22.76.1 yamt module_init(void)
266 1.22.76.1 yamt {
267 1.22.76.1 yamt struct bi_modulelist_entry *bi;
268 1.22.76.1 yamt struct stat st;
269 1.22.76.1 yamt char *buf;
270 1.22.76.1 yamt boot_module_t *bm;
271 1.22.76.1 yamt size_t len;
272 1.22.76.1 yamt off_t off;
273 1.22.76.1 yamt int err, fd;
274 1.22.76.1 yamt
275 1.22.76.1 yamt /* First, see which modules are valid and calculate btinfo size */
276 1.22.76.1 yamt len = sizeof(struct btinfo_modulelist);
277 1.22.76.1 yamt for (bm = boot_modules; bm; bm = bm->bm_next) {
278 1.22.76.1 yamt fd = open(bm->bm_path, 0);
279 1.22.76.1 yamt if (fd == -1) {
280 1.22.76.1 yamt printf("WARNING: couldn't open %s\n", bm->bm_path);
281 1.22.76.1 yamt bm->bm_len = -1;
282 1.22.76.1 yamt continue;
283 1.22.76.1 yamt }
284 1.22.76.1 yamt err = fstat(fd, &st);
285 1.22.76.1 yamt if (err == -1 || st.st_size == -1) {
286 1.22.76.1 yamt printf("WARNING: couldn't stat %s\n", bm->bm_path);
287 1.22.76.1 yamt close(fd);
288 1.22.76.1 yamt bm->bm_len = -1;
289 1.22.76.1 yamt continue;
290 1.22.76.1 yamt }
291 1.22.76.1 yamt bm->bm_len = st.st_size;
292 1.22.76.1 yamt close(fd);
293 1.22.76.1 yamt len += sizeof(struct bi_modulelist_entry);
294 1.22.76.1 yamt }
295 1.22.76.1 yamt
296 1.22.76.1 yamt /* Allocate the module list */
297 1.22.76.1 yamt btinfo_modulelist = alloc(len);
298 1.22.76.1 yamt if (btinfo_modulelist == NULL) {
299 1.22.76.1 yamt printf("WARNING: couldn't allocate module list\n");
300 1.22.76.1 yamt return;
301 1.22.76.1 yamt }
302 1.22.76.1 yamt memset(btinfo_modulelist, 0, len);
303 1.22.76.1 yamt btinfo_modulelist_size = len;
304 1.22.76.1 yamt
305 1.22.76.1 yamt /* Fill in btinfo structure */
306 1.22.76.1 yamt buf = (char *)btinfo_modulelist;
307 1.22.76.1 yamt btinfo_modulelist->num = 0;
308 1.22.76.1 yamt off = sizeof(struct btinfo_modulelist);
309 1.22.76.1 yamt
310 1.22.76.1 yamt for (bm = boot_modules; bm; bm = bm->bm_next) {
311 1.22.76.1 yamt if (bm->bm_len == -1)
312 1.22.76.1 yamt continue;
313 1.22.76.1 yamt printf("Loading %s ", bm->bm_path);
314 1.22.76.1 yamt fd = open(bm->bm_path, 0);
315 1.22.76.1 yamt if (fd == -1) {
316 1.22.76.1 yamt printf("ERROR: couldn't open %s\n", bm->bm_path);
317 1.22.76.1 yamt continue;
318 1.22.76.1 yamt }
319 1.22.76.1 yamt image_end = (image_end + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
320 1.22.76.1 yamt len = pread(fd, (void *)image_end, SSIZE_MAX);
321 1.22.76.1 yamt if (len < bm->bm_len) {
322 1.22.76.1 yamt printf(" FAILED\n");
323 1.22.76.1 yamt } else {
324 1.22.76.1 yamt btinfo_modulelist->num++;
325 1.22.76.1 yamt bi = (struct bi_modulelist_entry *)(buf + off);
326 1.22.76.1 yamt off += sizeof(struct bi_modulelist_entry);
327 1.22.76.1 yamt strncpy(bi->path, bm->bm_path, sizeof(bi->path) - 1);
328 1.22.76.1 yamt bi->base = image_end;
329 1.22.76.1 yamt bi->len = len;
330 1.22.76.1 yamt bi->type = BI_MODULE_ELF;
331 1.22.76.1 yamt printf(" \n");
332 1.22.76.1 yamt }
333 1.22.76.1 yamt if (len > 0)
334 1.22.76.1 yamt image_end += len;
335 1.22.76.1 yamt close(fd);
336 1.22.76.1 yamt }
337 1.22.76.1 yamt btinfo_modulelist->endpa = image_end;
338 1.22.76.1 yamt }
339