bootxx.c revision 1.2 1 1.2 wdk /* $NetBSD: bootxx.c,v 1.2 2000/09/26 09:48:35 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 extern int bcmp __P((const void *, const void *, size_t)); /* XXX */
88 1.1 wdk
89 1.1 wdk /*
90 1.1 wdk * This gets arguments from the PROM, calls other routines to open
91 1.1 wdk * and load the secondary boot loader called boot, and then transfers
92 1.1 wdk * execution to that program.
93 1.1 wdk */
94 1.1 wdk int
95 1.1 wdk main(argc, argv)
96 1.1 wdk int argc;
97 1.1 wdk char **argv;
98 1.1 wdk {
99 1.1 wdk entrypt entry;
100 1.1 wdk char *cp;
101 1.1 wdk extern void prom_init __P((void));
102 1.1 wdk
103 1.1 wdk prom_init();
104 1.1 wdk
105 1.2 wdk cp = *argv;
106 1.1 wdk
107 1.1 wdk printf("\nNetBSD/mipsco " NETBSD_VERS " " BOOTXX_FS_NAME " Primary Bootstrap\n");
108 1.1 wdk
109 1.1 wdk entry = loadfile(cp, "/boot");
110 1.1 wdk if ((int)entry != -1)
111 1.1 wdk goto goodload;
112 1.1 wdk
113 1.1 wdk entry = loadfile(cp, "/boot.mipsco");
114 1.1 wdk if ((int)entry != -1)
115 1.1 wdk goto goodload;
116 1.1 wdk
117 1.1 wdk goto bad;
118 1.1 wdk
119 1.1 wdk goodload:
120 1.1 wdk MIPS_PROM(flushcache)();
121 1.1 wdk entry(argc, argv, 0, 0);
122 1.1 wdk
123 1.1 wdk bad:
124 1.1 wdk return (1);
125 1.1 wdk }
126 1.1 wdk
127 1.1 wdk /*
128 1.1 wdk * Open 'filename', read in program and return the entry point or -1 if error.
129 1.1 wdk */
130 1.1 wdk entrypt
131 1.1 wdk loadfile(path, name)
132 1.1 wdk char *path, *name;
133 1.1 wdk {
134 1.1 wdk int fd, i;
135 1.1 wdk char *src, *dst, bootfname[64];
136 1.1 wdk Elf32_Ehdr ehdr;
137 1.1 wdk Elf32_Phdr phdr;
138 1.1 wdk
139 1.2 wdk dst = bootfname;
140 1.2 wdk for (src = path; *src;/**/)
141 1.2 wdk if ((*dst++ = *src++) == ')')
142 1.2 wdk break;
143 1.2 wdk for (src = name; *src;/**/)
144 1.2 wdk *dst++ = *src++;
145 1.2 wdk *dst = (char) 0;
146 1.1 wdk
147 1.1 wdk if ((fd = open(bootfname, 0)) < 0) {
148 1.1 wdk printf("open %s: %d\n", bootfname, errno);
149 1.1 wdk goto err;
150 1.1 wdk }
151 1.1 wdk
152 1.1 wdk /* read the exec header */
153 1.1 wdk i = read(fd, (char *)&ehdr, sizeof(ehdr));
154 1.1 wdk if ((i != sizeof(ehdr)) ||
155 1.1 wdk (bcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) ||
156 1.1 wdk (ehdr.e_ident[EI_CLASS] != ELFCLASS32)) {
157 1.1 wdk printf("%s: No ELF header\n", bootfname);
158 1.1 wdk goto cerr;
159 1.1 wdk }
160 1.1 wdk
161 1.1 wdk for (i = 0; i < ehdr.e_phnum; i++) {
162 1.1 wdk if (lseek(fd, (off_t) ehdr.e_phoff + i * sizeof(phdr), 0) < 0)
163 1.1 wdk goto cerr;
164 1.1 wdk if (read(fd, &phdr, sizeof(phdr)) != sizeof(phdr))
165 1.1 wdk goto cerr;
166 1.1 wdk if (phdr.p_type != PT_LOAD)
167 1.1 wdk continue;
168 1.1 wdk if (lseek(fd, (off_t)phdr.p_offset, 0) < 0)
169 1.1 wdk goto cerr;
170 1.1 wdk if (read(fd, (char *)phdr.p_paddr, phdr.p_filesz) != phdr.p_filesz)
171 1.1 wdk goto cerr;
172 1.1 wdk }
173 1.1 wdk return ((entrypt)ehdr.e_entry);
174 1.1 wdk
175 1.1 wdk cerr:
176 1.1 wdk #ifndef LIBSA_NO_FS_CLOSE
177 1.1 wdk (void) close(fd);
178 1.1 wdk #endif
179 1.1 wdk err:
180 1.1 wdk printf("Can't load '%s'\n", bootfname);
181 1.1 wdk return ((entrypt)-1);
182 1.1 wdk }
183