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