1 1.33 dsl /* $NetBSD: bootxx.c,v 1.33 2009/03/14 21:04:14 dsl Exp $ */ 2 1.15 simonb 3 1.15 simonb /*- 4 1.15 simonb * Copyright (c) 1999 The NetBSD Foundation, Inc. 5 1.15 simonb * All rights reserved. 6 1.15 simonb * 7 1.15 simonb * This code is derived from software contributed to The NetBSD Foundation 8 1.15 simonb * by Jonathan Stone, Michael Hitch and Simon Burge. 9 1.15 simonb * 10 1.15 simonb * Redistribution and use in source and binary forms, with or without 11 1.15 simonb * modification, are permitted provided that the following conditions 12 1.15 simonb * are met: 13 1.15 simonb * 1. Redistributions of source code must retain the above copyright 14 1.15 simonb * notice, this list of conditions and the following disclaimer. 15 1.15 simonb * 2. Redistributions in binary form must reproduce the above copyright 16 1.15 simonb * notice, this list of conditions and the following disclaimer in the 17 1.15 simonb * documentation and/or other materials provided with the distribution. 18 1.15 simonb * 19 1.15 simonb * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.15 simonb * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.15 simonb * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.15 simonb * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.15 simonb * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.15 simonb * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.15 simonb * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.15 simonb * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.15 simonb * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.15 simonb * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.15 simonb * POSSIBILITY OF SUCH DAMAGE. 30 1.15 simonb */ 31 1.4 cgd 32 1.1 deraadt /* 33 1.2 glass * Copyright (c) 1992, 1993 34 1.2 glass * The Regents of the University of California. All rights reserved. 35 1.1 deraadt * 36 1.1 deraadt * This code is derived from software contributed to Berkeley by 37 1.1 deraadt * Ralph Campbell. 38 1.1 deraadt * 39 1.1 deraadt * Redistribution and use in source and binary forms, with or without 40 1.1 deraadt * modification, are permitted provided that the following conditions 41 1.1 deraadt * are met: 42 1.1 deraadt * 1. Redistributions of source code must retain the above copyright 43 1.1 deraadt * notice, this list of conditions and the following disclaimer. 44 1.1 deraadt * 2. Redistributions in binary form must reproduce the above copyright 45 1.1 deraadt * notice, this list of conditions and the following disclaimer in the 46 1.1 deraadt * documentation and/or other materials provided with the distribution. 47 1.27 agc * 3. Neither the name of the University nor the names of its contributors 48 1.1 deraadt * may be used to endorse or promote products derived from this software 49 1.1 deraadt * without specific prior written permission. 50 1.1 deraadt * 51 1.1 deraadt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 52 1.1 deraadt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 53 1.1 deraadt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 54 1.1 deraadt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 55 1.1 deraadt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 56 1.1 deraadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 57 1.1 deraadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 58 1.1 deraadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 59 1.1 deraadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 60 1.1 deraadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 61 1.1 deraadt * SUCH DAMAGE. 62 1.1 deraadt * 63 1.4 cgd * @(#)boot.c 8.1 (Berkeley) 6/10/93 64 1.1 deraadt */ 65 1.1 deraadt 66 1.1 deraadt #include <sys/param.h> 67 1.14 simonb #include <sys/exec_elf.h> 68 1.23 simonb #include <lib/libsa/stand.h> 69 1.25 jdolecek #include <lib/libkern/libkern.h> 70 1.9 simonb #include <machine/dec_prom.h> 71 1.1 deraadt 72 1.31 dsl typedef void (*entrypt)(int, char **, int, const void *); 73 1.18 simonb 74 1.31 dsl int main(int, char **); 75 1.31 dsl entrypt loadfile(char *path, char *name); 76 1.18 simonb 77 1.31 dsl extern int clear_cache(char *addr, int len); 78 1.1 deraadt 79 1.1 deraadt /* 80 1.1 deraadt * This gets arguments from the PROM, calls other routines to open 81 1.18 simonb * and load the secondary boot loader called boot, and then transfers 82 1.18 simonb * execution to that program. 83 1.18 simonb * 84 1.18 simonb * Argv[0] should be something like "rz(0,0,0)netbsd" on a DECstation 3100. 85 1.18 simonb * Argv[0,1] should be something like "boot 5/rz0/netbsd" on a DECstation 5000. 86 1.18 simonb * The argument "-a" means netbsd should do an automatic reboot. 87 1.1 deraadt */ 88 1.5 mellon int 89 1.32 dsl main(int argc, char **argv) 90 1.1 deraadt { 91 1.18 simonb char *cp; 92 1.18 simonb entrypt entry; 93 1.6 jonathan 94 1.2 glass /* check for DS5000 boot */ 95 1.2 glass if (strcmp(argv[0], "boot") == 0) { 96 1.1 deraadt argc--; 97 1.1 deraadt argv++; 98 1.1 deraadt } 99 1.2 glass cp = *argv; 100 1.13 simonb 101 1.23 simonb printf("\nNetBSD/pmax " NETBSD_VERS " " BOOTXX_FS_NAME " Primary Bootstrap\n"); 102 1.23 simonb 103 1.23 simonb entry = loadfile(cp, "/boot.pmax"); 104 1.23 simonb if ((int)entry != -1) 105 1.23 simonb goto goodload; 106 1.23 simonb 107 1.23 simonb /* Give old /boot a go... */ 108 1.23 simonb entry = loadfile(cp, "/boot"); 109 1.23 simonb if ((int)entry != -1) 110 1.23 simonb goto goodload; 111 1.23 simonb 112 1.23 simonb /* Booting off an 8.3 filesystem? */ 113 1.23 simonb entry = loadfile(cp, "/boot.pma"); 114 1.23 simonb if ((int)entry != -1) 115 1.23 simonb goto goodload; 116 1.23 simonb 117 1.23 simonb goto bad; 118 1.23 simonb goodload: 119 1.5 mellon 120 1.23 simonb clear_cache((char *)PRIMARY_LOAD_ADDRESS, 1024 * 1024); 121 1.2 glass if (callv == &callvec) 122 1.18 simonb entry(argc, argv, 0, 0); 123 1.2 glass else 124 1.18 simonb entry(argc, argv, DEC_PROM_MAGIC, callv); 125 1.23 simonb bad: 126 1.23 simonb /* XXX would calling prom_halt here be cleaner? */ 127 1.13 simonb return (1); 128 1.1 deraadt } 129 1.1 deraadt 130 1.1 deraadt /* 131 1.1 deraadt * Open 'filename', read in program and return the entry point or -1 if error. 132 1.1 deraadt */ 133 1.18 simonb entrypt 134 1.33 dsl loadfile(char *path, char *name) 135 1.1 deraadt { 136 1.19 simonb int fd, i; 137 1.19 simonb char c, *buf, bootfname[64]; 138 1.13 simonb Elf32_Ehdr ehdr; 139 1.13 simonb Elf32_Phdr phdr; 140 1.13 simonb 141 1.23 simonb strcpy(bootfname, path); 142 1.13 simonb buf = bootfname; 143 1.19 simonb while ((c = *buf++) != '\0') { 144 1.19 simonb if (c == ')') 145 1.13 simonb break; 146 1.19 simonb if (c != '/') 147 1.13 simonb continue; 148 1.19 simonb while ((c = *buf++) != '\0') 149 1.19 simonb if (c == '/') 150 1.13 simonb break; 151 1.19 simonb /* 152 1.19 simonb * Make "N/rzY" with no trailing '/' valid by adding 153 1.23 simonb * the extra '/' before appending 'bootpmax' to the path. 154 1.19 simonb */ 155 1.19 simonb if (c != '/') { 156 1.19 simonb buf--; 157 1.19 simonb *buf++ = '/'; 158 1.19 simonb *buf = '\0'; 159 1.19 simonb } 160 1.13 simonb break; 161 1.13 simonb } 162 1.23 simonb strcpy(buf, name); 163 1.13 simonb if ((fd = open(bootfname, 0)) < 0) { 164 1.13 simonb printf("open %s: %d\n", bootfname, errno); 165 1.1 deraadt goto err; 166 1.1 deraadt } 167 1.1 deraadt 168 1.1 deraadt /* read the exec header */ 169 1.13 simonb i = read(fd, (char *)&ehdr, sizeof(ehdr)); 170 1.13 simonb if ((i != sizeof(ehdr)) || 171 1.29 he (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) || 172 1.21 simonb (ehdr.e_ident[EI_CLASS] != ELFCLASS32)) { 173 1.13 simonb printf("%s: No ELF header\n", bootfname); 174 1.1 deraadt goto cerr; 175 1.1 deraadt } 176 1.1 deraadt 177 1.13 simonb for (i = 0; i < ehdr.e_phnum; i++) { 178 1.13 simonb if (lseek(fd, (off_t) ehdr.e_phoff + i * sizeof(phdr), 0) < 0) 179 1.13 simonb goto cerr; 180 1.13 simonb if (read(fd, &phdr, sizeof(phdr)) != sizeof(phdr)) 181 1.13 simonb goto cerr; 182 1.20 kleink if (phdr.p_type != PT_LOAD) 183 1.13 simonb continue; 184 1.13 simonb if (lseek(fd, (off_t)phdr.p_offset, 0) < 0) 185 1.13 simonb goto cerr; 186 1.13 simonb if (read(fd, (char *)phdr.p_paddr, phdr.p_filesz) != phdr.p_filesz) 187 1.13 simonb goto cerr; 188 1.1 deraadt } 189 1.18 simonb return ((entrypt)ehdr.e_entry); 190 1.1 deraadt 191 1.1 deraadt cerr: 192 1.17 simonb #ifndef LIBSA_NO_FS_CLOSE 193 1.1 deraadt (void) close(fd); 194 1.2 glass #endif 195 1.1 deraadt err: 196 1.13 simonb printf("Can't load '%s'\n", bootfname); 197 1.18 simonb return ((entrypt)-1); 198 1.1 deraadt } 199