xref: /netbsd-src/sys/arch/i386/stand/lib/exec.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: exec.c,v 1.22 2005/12/11 12:17:48 christos 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 
93 extern struct btinfo_console btinfo_console;
94 
95 int
96 exec_netbsd(const char *file, physaddr_t loadaddr, int boothowto)
97 {
98 	u_long          boot_argv[BOOT_NARGS];
99 	int		fd;
100 	u_long		marks[MARK_MAX];
101 	struct btinfo_symtab btinfo_symtab;
102 	u_long		extmem;
103 	u_long		basemem;
104 #ifdef XMS
105 	u_long		xmsmem;
106 	physaddr_t	origaddr = loadaddr;
107 #endif
108 
109 #ifdef	DEBUG
110 	printf("exec: file=%s loadaddr=0x%lx\n",
111 	       file ? file : "NULL", loadaddr);
112 #endif
113 
114 	BI_ALLOC(6); /* ??? */
115 
116 	BI_ADD(&btinfo_console, BTINFO_CONSOLE, sizeof(struct btinfo_console));
117 
118 	extmem = getextmem();
119 	basemem = getbasemem();
120 
121 #ifdef XMS
122 	if ((getextmem1() == 0) && (xmsmem = checkxms())) {
123 	        u_long kernsize;
124 
125 		/*
126 		 * With "CONSERVATIVE_MEMDETECT", extmem is 0 because
127 		 *  getextmem() is getextmem1(). Without, the "smart"
128 		 *  methods could fail to report all memory as well.
129 		 * xmsmem is a few kB less than the actual size, but
130 		 *  better than nothing.
131 		 */
132 		if (xmsmem > extmem)
133 			extmem = xmsmem;
134 		/*
135 		 * Get the size of the kernel
136 		 */
137 		marks[MARK_START] = loadaddr;
138 		if ((fd = loadfile(file, marks, COUNT_KERNEL)) == -1)
139 			goto out;
140 		close(fd);
141 
142 		kernsize = marks[MARK_END];
143 		kernsize = (kernsize + 1023) / 1024;
144 
145 		loadaddr = xmsalloc(kernsize);
146 		if (!loadaddr)
147 			return ENOMEM;
148 	}
149 #endif
150 	marks[MARK_START] = loadaddr;
151 	if ((fd = loadfile(file, marks, LOAD_KERNEL)) == -1)
152 		goto out;
153 
154 	boot_argv[0] = boothowto;
155 	boot_argv[1] = 0;
156 	boot_argv[2] = vtophys(bootinfo);	/* old cyl offset */
157 	/* argv[3] below */
158 	boot_argv[4] = extmem;
159 	boot_argv[5] = basemem;
160 
161 	close(fd);
162 
163 	/*
164 	 * Gather some information for the kernel. Do this after the
165 	 * "point of no return" to avoid memory leaks.
166 	 * (but before DOS might be trashed in the XMS case)
167 	 */
168 #ifdef PASS_BIOSGEOM
169 	bi_getbiosgeom();
170 #endif
171 #ifdef PASS_MEMMAP
172 	bi_getmemmap();
173 #endif
174 
175 #ifdef XMS
176 	if (loadaddr != origaddr) {
177 		/*
178 		 * We now have done our last DOS IO, so we may
179 		 * trash the OS. Copy the data from the temporary
180 		 * buffer to its real address.
181 		 */
182 		marks[MARK_START] -= loadaddr;
183 		marks[MARK_END] -= loadaddr;
184 		marks[MARK_SYM] -= loadaddr;
185 		marks[MARK_END] -= loadaddr;
186 		ppbcopy(loadaddr, origaddr, marks[MARK_END]);
187 	}
188 #endif
189 	marks[MARK_END] = (((u_long) marks[MARK_END] + sizeof(int) - 1)) &
190 	    (-sizeof(int));
191 
192 	boot_argv[3] = marks[MARK_END];
193 
194 
195 #ifdef DEBUG
196 	printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", marks[MARK_ENTRY],
197 	    marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]);
198 #endif
199 
200 	btinfo_symtab.nsym = marks[MARK_NSYM];
201 	btinfo_symtab.ssym = marks[MARK_SYM];
202 	btinfo_symtab.esym = marks[MARK_END];
203 	BI_ADD(&btinfo_symtab, BTINFO_SYMTAB, sizeof(struct btinfo_symtab));
204 
205 	startprog(marks[MARK_ENTRY], BOOT_NARGS, boot_argv,
206 		  x86_trunc_page(basemem*1024));
207 	panic("exec returned");
208 
209 out:
210 	BI_FREE();
211 	bootinfo = 0;
212 	return -1;
213 }
214