1 /* $NetBSD: bootxx.c,v 1.1 2011/01/26 01:18:54 pooka Exp $ */ 2 3 /*- 4 * Copyright (c) 1999 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jonathan Stone, Michael Hitch and Simon Burge. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 /* 40 * Copyright (c) 1992, 1993 41 * The Regents of the University of California. All rights reserved. 42 * 43 * This code is derived from software contributed to Berkeley by 44 * Ralph Campbell. 45 * 46 * Redistribution and use in source and binary forms, with or without 47 * modification, are permitted provided that the following conditions 48 * are met: 49 * 1. Redistributions of source code must retain the above copyright 50 * notice, this list of conditions and the following disclaimer. 51 * 2. Redistributions in binary form must reproduce the above copyright 52 * notice, this list of conditions and the following disclaimer in the 53 * documentation and/or other materials provided with the distribution. 54 * 3. 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 #include <sys/param.h> 74 #include <sys/exec_elf.h> 75 #include <lib/libsa/stand.h> 76 #include <lib/libkern/libkern.h> 77 78 typedef void (entrypt) (int, char **, int, const void *, int, char *); 79 80 entrypt main; 81 entrypt *loadfile (char *path, char *name); 82 83 /* 84 * This gets arguments from the PROM, calls other routines to open 85 * and load the secondary boot loader called boot, and then transfers 86 * execution to that program. 87 */ 88 void 89 main(int argc, char **argv, int code, const void *cv, int bim, char *bip) 90 { 91 char *cp; 92 entrypt *entry; 93 94 cp = *argv; 95 96 printf("\nNetBSD/emips " NETBSD_VERS " " BOOTXX_FS_NAME " Bootstrap\n"); 97 98 entry = loadfile(cp, "netbsd"); 99 if ((int)entry != -1) 100 goto goodload; 101 102 /* Give old /boot a go... */ 103 entry = loadfile(cp, "/boot"); 104 if ((int)entry != -1) 105 goto goodload; 106 107 goto bad; 108 goodload: 109 110 entry(argc, argv, code, cv, bim, bip); 111 bad: 112 printf("Boot failed.\n"); 113 } 114 115 /* 116 * Open 'filename', read in program and return the entry point or -1 if error. 117 */ 118 entrypt * 119 loadfile(char *path, char *name) 120 { 121 int fd, i; 122 char c, *buf, bootfname[64]; 123 Elf32_Ehdr ehdr; 124 Elf32_Phdr phdr; 125 126 strcpy(bootfname, path); 127 buf = bootfname; 128 while ((c = *buf++) != '\0') { 129 if (c == ')') 130 break; 131 if (c != '/') 132 continue; 133 while ((c = *buf++) != '\0') 134 if (c == '/') 135 break; 136 /* 137 * Make "N/rzY" with no trailing '/' valid by adding 138 * the extra '/' before appending 'bootemips' to the path. 139 */ 140 if (c != '/') { 141 buf--; 142 *buf++ = '/'; 143 *buf = '\0'; 144 } 145 break; 146 } 147 strcpy(buf, name); 148 if ((fd = open(bootfname, 0)) < 0) { 149 printf("open %s: %d\n", bootfname, errno); 150 goto err; 151 } 152 153 /* read the exec header */ 154 i = read(fd, (char *)&ehdr, sizeof(ehdr)); 155 if ((i != sizeof(ehdr)) || 156 (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) || 157 (ehdr.e_ident[EI_CLASS] != ELFCLASS32)) { 158 printf("%s: No ELF header\n", bootfname); 159 goto cerr; 160 } 161 162 for (i = 0; i < ehdr.e_phnum; i++) { 163 if (lseek(fd, (off_t) ehdr.e_phoff + i * sizeof(phdr), 0) < 0) 164 goto cerr; 165 if (read(fd, &phdr, sizeof(phdr)) != sizeof(phdr)) 166 goto cerr; 167 if (phdr.p_type != PT_LOAD) 168 continue; 169 if (lseek(fd, (off_t)phdr.p_offset, 0) < 0) 170 goto cerr; 171 if (read(fd, (char *)phdr.p_paddr, phdr.p_filesz) != phdr.p_filesz) 172 goto cerr; 173 } 174 return ((entrypt*)ehdr.e_entry); 175 176 cerr: 177 #ifndef LIBSA_NO_FS_CLOSE 178 (void) close(fd); 179 #endif 180 err: 181 printf("Can't load '%s'\n", bootfname); 182 return ((entrypt*)-1); 183 } 184