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