exec.c revision 1.16 1 1.16 jdolecek /* $NetBSD: exec.c,v 1.16 2001/05/19 18:15:14 jdolecek Exp $ */
2 1.1 perry
3 1.1 perry /*
4 1.1 perry * Copyright (c) 1982, 1986, 1990, 1993
5 1.1 perry * The Regents of the University of California. All rights reserved.
6 1.1 perry * Copyright (c) 1996
7 1.1 perry * Matthias Drochner. All rights reserved.
8 1.1 perry * Copyright (c) 1996
9 1.1 perry * Perry E. Metzger. All rights reserved.
10 1.8 christos * Copyright (c) 1997
11 1.8 christos * Martin Husemann. All rights reserved.
12 1.1 perry *
13 1.1 perry * Redistribution and use in source and binary forms, with or without
14 1.1 perry * modification, are permitted provided that the following conditions
15 1.1 perry * are met:
16 1.1 perry * 1. Redistributions of source code must retain the above copyright
17 1.1 perry * notice, this list of conditions and the following disclaimer.
18 1.1 perry * 2. Redistributions in binary form must reproduce the above copyright
19 1.1 perry * notice, this list of conditions and the following disclaimer in the
20 1.1 perry * documentation and/or other materials provided with the distribution.
21 1.1 perry * 3. All advertising materials mentioning features or use of this software
22 1.1 perry * must display the following acknowledgement:
23 1.1 perry * This product includes software developed by the University of
24 1.1 perry * California, Berkeley and its contributors.
25 1.1 perry * 4. Neither the name of the University nor the names of its contributors
26 1.1 perry * may be used to endorse or promote products derived from this software
27 1.1 perry * without specific prior written permission.
28 1.1 perry *
29 1.1 perry * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 1.1 perry * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 1.1 perry * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 1.1 perry * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 1.1 perry * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 1.1 perry * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 1.1 perry * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 1.1 perry * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 1.1 perry * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 1.1 perry * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 1.1 perry * SUCH DAMAGE.
40 1.1 perry *
41 1.1 perry * @(#)boot.c 8.1 (Berkeley) 6/10/93
42 1.1 perry */
43 1.1 perry
44 1.2 thorpej /*
45 1.8 christos * starts NetBSD a.out kernel
46 1.8 christos * needs lowlevel startup from startprog.S
47 1.8 christos * This is a special version of exec.c to support use of XMS.
48 1.1 perry */
49 1.8 christos
50 1.1 perry #include <sys/param.h>
51 1.1 perry #include <sys/reboot.h>
52 1.1 perry #ifdef COMPAT_OLDBOOT
53 1.1 perry #include <sys/disklabel.h>
54 1.1 perry #endif
55 1.1 perry
56 1.1 perry #include <lib/libsa/stand.h>
57 1.4 drochner
58 1.6 christos #include "loadfile.h"
59 1.1 perry #include "libi386.h"
60 1.4 drochner #include "bootinfo.h"
61 1.16 jdolecek #ifdef SUPPORT_PS2
62 1.16 jdolecek #include "biosmca.h"
63 1.16 jdolecek #endif
64 1.1 perry
65 1.1 perry #ifdef COMPAT_OLDBOOT
66 1.11 drochner static int dev2major __P((char *, int *));
67 1.11 drochner
68 1.1 perry static int
69 1.1 perry dev2major(devname, major)
70 1.2 thorpej char *devname;
71 1.2 thorpej int *major;
72 1.1 perry {
73 1.16 jdolecek static const struct {
74 1.16 jdolecek const char *name;
75 1.16 jdolecek int maj;
76 1.16 jdolecek } devices[] = {
77 1.16 jdolecek { "wd", 0 },
78 1.16 jdolecek { "fd", 2 },
79 1.16 jdolecek { "sd", 4 },
80 1.16 jdolecek #ifdef SUPPORT_PS2
81 1.16 jdolecek { "ed", 20 },
82 1.16 jdolecek #endif
83 1.16 jdolecek };
84 1.16 jdolecek #define NUMDEVICES (sizeof(devices)/sizeof(devices[0]))
85 1.2 thorpej int i;
86 1.1 perry
87 1.2 thorpej for (i = 0; i < NUMDEVICES; i++)
88 1.16 jdolecek if (!strcmp(devname, devices[i].name)) {
89 1.16 jdolecek *major = devices[i].maj;
90 1.2 thorpej return (0);
91 1.2 thorpej }
92 1.2 thorpej return (-1);
93 1.1 perry }
94 1.1 perry #endif
95 1.6 christos #define BOOT_NARGS 6
96 1.1 perry
97 1.4 drochner extern struct btinfo_console btinfo_console;
98 1.4 drochner
99 1.2 thorpej int
100 1.4 drochner exec_netbsd(file, loadaddr, boothowto)
101 1.2 thorpej const char *file;
102 1.2 thorpej physaddr_t loadaddr;
103 1.2 thorpej int boothowto;
104 1.1 perry {
105 1.6 christos u_long boot_argv[BOOT_NARGS];
106 1.6 christos int fd;
107 1.8 christos u_long marks[MARK_MAX];
108 1.8 christos struct btinfo_symtab btinfo_symtab;
109 1.8 christos u_long extmem;
110 1.15 dbj u_long basemem;
111 1.8 christos #ifdef XMS
112 1.8 christos u_long xmsmem;
113 1.8 christos physaddr_t origaddr = loadaddr;
114 1.8 christos #endif
115 1.8 christos
116 1.1 perry #ifdef COMPAT_OLDBOOT
117 1.2 thorpej char *fsname, *devname;
118 1.2 thorpej int unit, part;
119 1.2 thorpej const char *filename;
120 1.2 thorpej int bootdevnr;
121 1.1 perry #endif
122 1.1 perry
123 1.1 perry #ifdef DEBUG
124 1.13 drochner printf("exec: file=%s loadaddr=0x%lx\n",
125 1.13 drochner file ? file : "NULL", loadaddr);
126 1.1 perry #endif
127 1.4 drochner
128 1.6 christos BI_ALLOC(6); /* ??? */
129 1.4 drochner
130 1.4 drochner BI_ADD(&btinfo_console, BTINFO_CONSOLE, sizeof(struct btinfo_console));
131 1.4 drochner
132 1.8 christos extmem = getextmem();
133 1.15 dbj basemem = getbasemem();
134 1.8 christos
135 1.8 christos #ifdef XMS
136 1.8 christos if ((getextmem1() == 0) && (xmsmem = checkxms())) {
137 1.8 christos u_long kernsize;
138 1.8 christos
139 1.8 christos /*
140 1.8 christos * With "CONSERVATIVE_MEMDETECT", extmem is 0 because
141 1.8 christos * getextmem() is getextmem1(). Without, the "smart"
142 1.8 christos * methods could fail to report all memory as well.
143 1.8 christos * xmsmem is a few kB less than the actual size, but
144 1.8 christos * better than nothing.
145 1.8 christos */
146 1.8 christos if (xmsmem > extmem)
147 1.8 christos extmem = xmsmem;
148 1.8 christos /*
149 1.8 christos * Get the size of the kernel
150 1.8 christos */
151 1.8 christos marks[MARK_START] = loadaddr;
152 1.12 christos if ((fd = loadfile(file, marks, COUNT_KERNEL)) == -1)
153 1.8 christos goto out;
154 1.8 christos close(fd);
155 1.8 christos
156 1.8 christos kernsize = marks[MARK_END];
157 1.8 christos kernsize = (kernsize + 1023) / 1024;
158 1.8 christos
159 1.8 christos loadaddr = xmsalloc(kernsize);
160 1.8 christos if (!loadaddr)
161 1.8 christos return(ENOMEM);
162 1.8 christos }
163 1.8 christos #endif
164 1.7 christos marks[MARK_START] = loadaddr;
165 1.12 christos if ((fd = loadfile(file, marks, LOAD_KERNEL)) == -1)
166 1.4 drochner goto out;
167 1.1 perry
168 1.1 perry boot_argv[0] = boothowto;
169 1.1 perry
170 1.1 perry #ifdef COMPAT_OLDBOOT
171 1.1 perry /* prepare boot device information for kernel */
172 1.2 thorpej if (parsebootfile(file, &fsname, &devname, &unit, &part, &filename)
173 1.2 thorpej || strcmp(fsname, "ufs"))
174 1.2 thorpej bootdevnr = 0; /* XXX error out if parse error??? */
175 1.1 perry else {
176 1.2 thorpej int major;
177 1.1 perry
178 1.3 thorpej if (strcmp(devname, "hd") == 0) {
179 1.2 thorpej /* generic BIOS disk, have to guess type */
180 1.6 christos struct open_file *f = &files[fd]; /* XXX */
181 1.2 thorpej
182 1.16 jdolecek switch (biosdisk_gettype(f)) {
183 1.16 jdolecek case DTYPE_SCSI:
184 1.2 thorpej devname = "sd";
185 1.16 jdolecek break;
186 1.16 jdolecek #ifdef SUPPORT_PS2
187 1.16 jdolecek case DTYPE_ESDI:
188 1.16 jdolecek if (biosmca_ps2model) {
189 1.16 jdolecek devname = "ed";
190 1.16 jdolecek break;
191 1.16 jdolecek }
192 1.16 jdolecek #endif
193 1.16 jdolecek default:
194 1.2 thorpej devname = "wd";
195 1.16 jdolecek break;
196 1.16 jdolecek }
197 1.3 thorpej
198 1.3 thorpej /*
199 1.3 thorpej * The old boot block performed the following
200 1.3 thorpej * conversion:
201 1.3 thorpej *
202 1.3 thorpej * hdN -> Xd0
203 1.3 thorpej *
204 1.3 thorpej * where X is the type specified by the label.
205 1.3 thorpej * We mimmick that here, for lack of any better
206 1.3 thorpej * way of doing things.
207 1.3 thorpej */
208 1.3 thorpej unit = 0;
209 1.2 thorpej }
210 1.3 thorpej
211 1.2 thorpej if (dev2major(devname, &major))
212 1.2 thorpej bootdevnr = 0; /* XXX error out??? */
213 1.1 perry else
214 1.2 thorpej bootdevnr = MAKEBOOTDEV(major, 0, 0, unit, part);
215 1.1 perry }
216 1.1 perry
217 1.1 perry boot_argv[1] = bootdevnr;
218 1.5 drochner #else
219 1.5 drochner boot_argv[1] = 0;
220 1.5 drochner #endif
221 1.8 christos boot_argv[2] = vtophys(bootinfo); /* old cyl offset */
222 1.8 christos /* argv[3] below */
223 1.8 christos boot_argv[4] = extmem;
224 1.15 dbj boot_argv[5] = basemem;
225 1.8 christos
226 1.8 christos close(fd);
227 1.10 drochner
228 1.10 drochner /*
229 1.10 drochner * Gather some information for the kernel. Do this after the
230 1.10 drochner * "point of no return" to avoid memory leaks.
231 1.10 drochner * (but before DOS might be trashed in the XMS case)
232 1.10 drochner */
233 1.10 drochner #ifdef PASS_BIOSGEOM
234 1.10 drochner bi_getbiosgeom();
235 1.10 drochner #endif
236 1.10 drochner #ifdef PASS_MEMMAP
237 1.10 drochner bi_getmemmap();
238 1.10 drochner #endif
239 1.8 christos
240 1.8 christos #ifdef XMS
241 1.8 christos if (loadaddr != origaddr) {
242 1.8 christos /*
243 1.14 ross * We now have done our last DOS IO, so we may
244 1.8 christos * trash the OS. Copy the data from the temporary
245 1.8 christos * buffer to its real adress.
246 1.8 christos */
247 1.8 christos marks[MARK_START] -= loadaddr;
248 1.8 christos marks[MARK_END] -= loadaddr;
249 1.8 christos marks[MARK_SYM] -= loadaddr;
250 1.8 christos marks[MARK_END] -= loadaddr;
251 1.8 christos ppbcopy(loadaddr, origaddr, marks[MARK_END]);
252 1.8 christos }
253 1.8 christos #endif
254 1.8 christos marks[MARK_END] = (((u_long) marks[MARK_END] + sizeof(int) - 1)) &
255 1.8 christos (-sizeof(int));
256 1.8 christos
257 1.7 christos boot_argv[3] = marks[MARK_END];
258 1.1 perry
259 1.1 perry
260 1.1 perry #ifdef DEBUG
261 1.8 christos printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", marks[MARK_ENTRY],
262 1.7 christos marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]);
263 1.1 perry #endif
264 1.1 perry
265 1.8 christos btinfo_symtab.nsym = marks[MARK_NSYM];
266 1.8 christos btinfo_symtab.ssym = marks[MARK_SYM];
267 1.8 christos btinfo_symtab.esym = marks[MARK_END];
268 1.8 christos BI_ADD(&btinfo_symtab, BTINFO_SYMTAB, sizeof(struct btinfo_symtab));
269 1.8 christos
270 1.15 dbj startprog(marks[MARK_ENTRY], BOOT_NARGS, boot_argv,
271 1.15 dbj i386_trunc_page(basemem*1024));
272 1.1 perry panic("exec returned");
273 1.1 perry
274 1.4 drochner out:
275 1.4 drochner BI_FREE();
276 1.4 drochner bootinfo = 0;
277 1.2 thorpej return (-1);
278 1.1 perry }
279