1 /* $NetBSD: bootxx.c,v 1.27 2003/08/07 16:29:15 agc 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 #include <machine/dec_prom.h> 78 79 typedef void (*entrypt) __P((int, char **, int, const void *)); 80 81 int main __P((int, char **)); 82 entrypt loadfile __P((char *path, char *name)); 83 84 extern int clear_cache __P((char *addr, int len)); 85 86 /* 87 * This gets arguments from the PROM, calls other routines to open 88 * and load the secondary boot loader called boot, and then transfers 89 * execution to that program. 90 * 91 * Argv[0] should be something like "rz(0,0,0)netbsd" on a DECstation 3100. 92 * Argv[0,1] should be something like "boot 5/rz0/netbsd" on a DECstation 5000. 93 * The argument "-a" means netbsd should do an automatic reboot. 94 */ 95 int 96 main(argc, argv) 97 int argc; 98 char **argv; 99 { 100 char *cp; 101 entrypt entry; 102 103 /* check for DS5000 boot */ 104 if (strcmp(argv[0], "boot") == 0) { 105 argc--; 106 argv++; 107 } 108 cp = *argv; 109 110 printf("\nNetBSD/pmax " NETBSD_VERS " " BOOTXX_FS_NAME " Primary Bootstrap\n"); 111 112 entry = loadfile(cp, "/boot.pmax"); 113 if ((int)entry != -1) 114 goto goodload; 115 116 /* Give old /boot a go... */ 117 entry = loadfile(cp, "/boot"); 118 if ((int)entry != -1) 119 goto goodload; 120 121 /* Booting off an 8.3 filesystem? */ 122 entry = loadfile(cp, "/boot.pma"); 123 if ((int)entry != -1) 124 goto goodload; 125 126 goto bad; 127 goodload: 128 129 clear_cache((char *)PRIMARY_LOAD_ADDRESS, 1024 * 1024); 130 if (callv == &callvec) 131 entry(argc, argv, 0, 0); 132 else 133 entry(argc, argv, DEC_PROM_MAGIC, callv); 134 bad: 135 /* XXX would calling prom_halt here be cleaner? */ 136 return (1); 137 } 138 139 /* 140 * Open 'filename', read in program and return the entry point or -1 if error. 141 */ 142 entrypt 143 loadfile(path, name) 144 char *path, *name; 145 { 146 int fd, i; 147 char c, *buf, bootfname[64]; 148 Elf32_Ehdr ehdr; 149 Elf32_Phdr phdr; 150 151 strcpy(bootfname, path); 152 buf = bootfname; 153 while ((c = *buf++) != '\0') { 154 if (c == ')') 155 break; 156 if (c != '/') 157 continue; 158 while ((c = *buf++) != '\0') 159 if (c == '/') 160 break; 161 /* 162 * Make "N/rzY" with no trailing '/' valid by adding 163 * the extra '/' before appending 'bootpmax' to the path. 164 */ 165 if (c != '/') { 166 buf--; 167 *buf++ = '/'; 168 *buf = '\0'; 169 } 170 break; 171 } 172 strcpy(buf, name); 173 if ((fd = open(bootfname, 0)) < 0) { 174 printf("open %s: %d\n", bootfname, errno); 175 goto err; 176 } 177 178 /* read the exec header */ 179 i = read(fd, (char *)&ehdr, sizeof(ehdr)); 180 if ((i != sizeof(ehdr)) || 181 (bcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) || 182 (ehdr.e_ident[EI_CLASS] != ELFCLASS32)) { 183 printf("%s: No ELF header\n", bootfname); 184 goto cerr; 185 } 186 187 for (i = 0; i < ehdr.e_phnum; i++) { 188 if (lseek(fd, (off_t) ehdr.e_phoff + i * sizeof(phdr), 0) < 0) 189 goto cerr; 190 if (read(fd, &phdr, sizeof(phdr)) != sizeof(phdr)) 191 goto cerr; 192 if (phdr.p_type != PT_LOAD) 193 continue; 194 if (lseek(fd, (off_t)phdr.p_offset, 0) < 0) 195 goto cerr; 196 if (read(fd, (char *)phdr.p_paddr, phdr.p_filesz) != phdr.p_filesz) 197 goto cerr; 198 } 199 return ((entrypt)ehdr.e_entry); 200 201 cerr: 202 #ifndef LIBSA_NO_FS_CLOSE 203 (void) close(fd); 204 #endif 205 err: 206 printf("Can't load '%s'\n", bootfname); 207 return ((entrypt)-1); 208 } 209