exec.c revision 1.30
1/* $NetBSD: exec.c,v 1.30 2008/10/08 22:42:38 joerg Exp $ */ 2 3/*- 4 * Copyright (c) 2008 The NetBSD Foundation, Inc. 5 * 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 * 16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29/* 30 * Copyright (c) 1982, 1986, 1990, 1993 31 * The Regents of the University of California. All rights reserved. 32 * 33 * Redistribution and use in source and binary forms, with or without 34 * modification, are permitted provided that the following conditions 35 * are met: 36 * 1. Redistributions of source code must retain the above copyright 37 * notice, this list of conditions and the following disclaimer. 38 * 2. Redistributions in binary form must reproduce the above copyright 39 * notice, this list of conditions and the following disclaimer in the 40 * documentation and/or other materials provided with the distribution. 41 * 3. Neither the name of the University nor the names of its contributors 42 * may be used to endorse or promote products derived from this software 43 * without specific prior written permission. 44 * 45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 55 * SUCH DAMAGE. 56 * 57 * @(#)boot.c 8.1 (Berkeley) 6/10/93 58 */ 59 60/* 61 * Copyright (c) 1996 62 * Matthias Drochner. All rights reserved. 63 * Copyright (c) 1996 64 * Perry E. Metzger. All rights reserved. 65 * 66 * Redistribution and use in source and binary forms, with or without 67 * modification, are permitted provided that the following conditions 68 * are met: 69 * 1. Redistributions of source code must retain the above copyright 70 * notice, this list of conditions and the following disclaimer. 71 * 2. Redistributions in binary form must reproduce the above copyright 72 * notice, this list of conditions and the following disclaimer in the 73 * documentation and/or other materials provided with the distribution. 74 * 3. All advertising materials mentioning features or use of this software 75 * must display the following acknowledgement: 76 * This product includes software developed by the University of 77 * California, Berkeley and its contributors. 78 * 4. Neither the name of the University nor the names of its contributors 79 * may be used to endorse or promote products derived from this software 80 * without specific prior written permission. 81 * 82 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 83 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 84 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 85 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 86 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 87 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 88 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 89 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 90 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 91 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 92 * SUCH DAMAGE. 93 * 94 * @(#)boot.c 8.1 (Berkeley) 6/10/93 95 */ 96 97/* 98 * starts NetBSD a.out kernel 99 * needs lowlevel startup from startprog.S 100 * This is a special version of exec.c to support use of XMS. 101 */ 102 103#include <sys/param.h> 104#include <sys/reboot.h> 105 106#include <lib/libsa/stand.h> 107#include <lib/libkern/libkern.h> 108 109#include "loadfile.h" 110#include "libi386.h" 111#include "bootinfo.h" 112#include "bootmod.h" 113#ifdef SUPPORT_PS2 114#include "biosmca.h" 115#endif 116 117#define BOOT_NARGS 6 118 119#ifndef PAGE_SIZE 120#define PAGE_SIZE 4096 121#endif 122 123extern struct btinfo_console btinfo_console; 124 125boot_module_t *boot_modules; 126bool boot_modules_enabled = true; 127bool kernel_loaded; 128 129static struct btinfo_modulelist *btinfo_modulelist; 130static size_t btinfo_modulelist_size; 131static uint32_t image_end; 132static char module_base[64] = "/"; 133 134static void module_init(void); 135 136int 137exec_netbsd(const char *file, physaddr_t loadaddr, int boothowto, int floppy) 138{ 139 u_long boot_argv[BOOT_NARGS]; 140 int fd; 141 u_long marks[MARK_MAX]; 142 struct btinfo_symtab btinfo_symtab; 143 u_long extmem; 144 u_long basemem; 145#ifdef XMS 146 u_long xmsmem; 147 physaddr_t origaddr = loadaddr; 148#endif 149 150#ifdef DEBUG 151 printf("exec: file=%s loadaddr=0x%lx\n", 152 file ? file : "NULL", loadaddr); 153#endif 154 155 BI_ALLOC(32); /* ??? */ 156 157 BI_ADD(&btinfo_console, BTINFO_CONSOLE, sizeof(struct btinfo_console)); 158 159 extmem = getextmem(); 160 basemem = getbasemem(); 161 162#ifdef XMS 163 if ((getextmem1() == 0) && (xmsmem = checkxms())) { 164 u_long kernsize; 165 166 /* 167 * With "CONSERVATIVE_MEMDETECT", extmem is 0 because 168 * getextmem() is getextmem1(). Without, the "smart" 169 * methods could fail to report all memory as well. 170 * xmsmem is a few kB less than the actual size, but 171 * better than nothing. 172 */ 173 if (xmsmem > extmem) 174 extmem = xmsmem; 175 /* 176 * Get the size of the kernel 177 */ 178 marks[MARK_START] = loadaddr; 179 if ((fd = loadfile(file, marks, COUNT_KERNEL)) == -1) 180 goto out; 181 close(fd); 182 183 kernsize = marks[MARK_END]; 184 kernsize = (kernsize + 1023) / 1024; 185 186 loadaddr = xmsalloc(kernsize); 187 if (!loadaddr) 188 return ENOMEM; 189 } 190#endif 191 marks[MARK_START] = loadaddr; 192 if ((fd = loadfile(file, marks, 193 LOAD_KERNEL & ~(floppy ? LOAD_NOTE : 0))) == -1) 194 goto out; 195 196 boot_argv[0] = boothowto; 197 boot_argv[1] = 0; 198 boot_argv[2] = vtophys(bootinfo); /* old cyl offset */ 199 /* argv[3] below */ 200 boot_argv[4] = extmem; 201 boot_argv[5] = basemem; 202 203 close(fd); 204 205 /* 206 * Gather some information for the kernel. Do this after the 207 * "point of no return" to avoid memory leaks. 208 * (but before DOS might be trashed in the XMS case) 209 */ 210#ifdef PASS_BIOSGEOM 211 bi_getbiosgeom(); 212#endif 213#ifdef PASS_MEMMAP 214 bi_getmemmap(); 215#endif 216 217#ifdef XMS 218 if (loadaddr != origaddr) { 219 /* 220 * We now have done our last DOS IO, so we may 221 * trash the OS. Copy the data from the temporary 222 * buffer to its real address. 223 */ 224 marks[MARK_START] -= loadaddr; 225 marks[MARK_END] -= loadaddr; 226 marks[MARK_SYM] -= loadaddr; 227 marks[MARK_END] -= loadaddr; 228 ppbcopy(loadaddr, origaddr, marks[MARK_END]); 229 } 230#endif 231 marks[MARK_END] = (((u_long) marks[MARK_END] + sizeof(int) - 1)) & 232 (-sizeof(int)); 233 boot_argv[3] = marks[MARK_END]; 234 image_end = marks[MARK_END]; 235 kernel_loaded = true; 236 237 /* pull in any modules if necessary */ 238 if (boot_modules_enabled) { 239 module_init(); 240 if (btinfo_modulelist) { 241 BI_ADD(btinfo_modulelist, BTINFO_MODULELIST, 242 btinfo_modulelist_size); 243 } 244 } 245 246#ifdef DEBUG 247 printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", marks[MARK_ENTRY], 248 marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]); 249#endif 250 251 btinfo_symtab.nsym = marks[MARK_NSYM]; 252 btinfo_symtab.ssym = marks[MARK_SYM]; 253 btinfo_symtab.esym = marks[MARK_END]; 254 BI_ADD(&btinfo_symtab, BTINFO_SYMTAB, sizeof(struct btinfo_symtab)); 255 256 startprog(marks[MARK_ENTRY], BOOT_NARGS, boot_argv, 257 x86_trunc_page(basemem*1024)); 258 panic("exec returned"); 259 260out: 261 BI_FREE(); 262 bootinfo = 0; 263 return -1; 264} 265 266static const char * 267module_path(boot_module_t *bm) 268{ 269 static char buf[256]; 270 const char *name; 271 272 name = bm->bm_path; 273 if (name[0] == '/') 274 return name; 275 snprintf(buf, sizeof(buf), "%s/%s/%s.kmod", module_base, bm->bm_path, 276 bm->bm_path); 277 return buf; 278} 279 280static int 281module_open(boot_module_t *bm, int mode) 282{ 283 int fd; 284 const char *path; 285 286 /* check the expanded path first */ 287 path = module_path(bm); 288 fd = open(path, mode); 289 if (fd == -1) { 290 printf("WARNING: couldn't open %s\n", path); 291 /* now attempt the raw path provided */ 292 fd = open(bm->bm_path, mode); 293 if (fd == -1) 294 printf("WARNING: couldn't open %s\n", bm->bm_path); 295 } 296 return fd; 297} 298 299static void 300module_init(void) 301{ 302 struct bi_modulelist_entry *bi; 303 struct stat st; 304 const char *machine; 305 char *buf; 306 boot_module_t *bm; 307 size_t len; 308 off_t off; 309 int err, fd; 310 311 switch (netbsd_elf_class) { 312 case ELFCLASS32: 313 machine = "i386"; 314 break; 315 case ELFCLASS64: 316 machine = "amd64"; 317 break; 318 default: 319 machine = "generic"; 320 break; 321 } 322 if (netbsd_version / 1000000 % 100 == 99) { 323 /* -current */ 324 snprintf(module_base, sizeof(module_base), 325 "/stand/%s/%d.%d.%d/modules", machine, 326 netbsd_version / 100000000, 327 netbsd_version / 1000000 % 100, 328 netbsd_version / 100 % 100); 329 } else if (netbsd_version != 0) { 330 /* release */ 331 snprintf(module_base, sizeof(module_base), 332 "/stand/%s/%d.%d/modules", machine, 333 netbsd_version / 100000000, 334 netbsd_version / 1000000 % 100); 335 } 336 337 /* First, see which modules are valid and calculate btinfo size */ 338 len = sizeof(struct btinfo_modulelist); 339 for (bm = boot_modules; bm; bm = bm->bm_next) { 340 fd = module_open(bm, 0); 341 if (fd == -1) { 342 bm->bm_len = -1; 343 continue; 344 } 345 err = fstat(fd, &st); 346 if (err == -1 || st.st_size == -1) { 347 printf("WARNING: couldn't stat %s\n", bm->bm_path); 348 close(fd); 349 bm->bm_len = -1; 350 continue; 351 } 352 bm->bm_len = st.st_size; 353 close(fd); 354 len += sizeof(struct bi_modulelist_entry); 355 } 356 357 /* Allocate the module list */ 358 btinfo_modulelist = alloc(len); 359 if (btinfo_modulelist == NULL) { 360 printf("WARNING: couldn't allocate module list\n"); 361 return; 362 } 363 memset(btinfo_modulelist, 0, len); 364 btinfo_modulelist_size = len; 365 366 /* Fill in btinfo structure */ 367 buf = (char *)btinfo_modulelist; 368 btinfo_modulelist->num = 0; 369 off = sizeof(struct btinfo_modulelist); 370 371 for (bm = boot_modules; bm; bm = bm->bm_next) { 372 if (bm->bm_len == -1) 373 continue; 374 printf("Loading %s ", bm->bm_path); 375 fd = module_open(bm, 0); 376 if (fd == -1) { 377 printf("ERROR: couldn't open %s\n", bm->bm_path); 378 continue; 379 } 380 image_end = (image_end + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1); 381 len = pread(fd, (void *)image_end, SSIZE_MAX); 382 if (len < bm->bm_len) { 383 printf(" FAILED\n"); 384 } else { 385 btinfo_modulelist->num++; 386 bi = (struct bi_modulelist_entry *)(buf + off); 387 off += sizeof(struct bi_modulelist_entry); 388 strncpy(bi->path, bm->bm_path, sizeof(bi->path) - 1); 389 bi->base = image_end; 390 bi->len = len; 391 bi->type = BI_MODULE_ELF; 392 printf(" \n"); 393 } 394 if (len > 0) 395 image_end += len; 396 close(fd); 397 } 398 btinfo_modulelist->endpa = image_end; 399} 400