bootxx.c revision 1.1 1 1.1 wdk /* $NetBSD: bootxx.c,v 1.1 2000/09/18 11:40:48 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.1 wdk if (argc > 1)
106 1.1 wdk cp = argv[1];
107 1.1 wdk else
108 1.1 wdk cp = "dksd()";
109 1.1 wdk
110 1.1 wdk printf("\nNetBSD/mipsco " NETBSD_VERS " " BOOTXX_FS_NAME " Primary Bootstrap\n");
111 1.1 wdk
112 1.1 wdk entry = loadfile(cp, "/boot");
113 1.1 wdk if ((int)entry != -1)
114 1.1 wdk goto goodload;
115 1.1 wdk
116 1.1 wdk entry = loadfile(cp, "/boot.mipsco");
117 1.1 wdk if ((int)entry != -1)
118 1.1 wdk goto goodload;
119 1.1 wdk
120 1.1 wdk goto bad;
121 1.1 wdk
122 1.1 wdk goodload:
123 1.1 wdk MIPS_PROM(flushcache)();
124 1.1 wdk entry(argc, argv, 0, 0);
125 1.1 wdk
126 1.1 wdk bad:
127 1.1 wdk return (1);
128 1.1 wdk }
129 1.1 wdk
130 1.1 wdk /*
131 1.1 wdk * Open 'filename', read in program and return the entry point or -1 if error.
132 1.1 wdk */
133 1.1 wdk entrypt
134 1.1 wdk loadfile(path, name)
135 1.1 wdk char *path, *name;
136 1.1 wdk {
137 1.1 wdk int fd, i;
138 1.1 wdk char *src, *dst, bootfname[64];
139 1.1 wdk Elf32_Ehdr ehdr;
140 1.1 wdk Elf32_Phdr phdr;
141 1.1 wdk
142 1.1 wdk src = bootfname;
143 1.1 wdk for (dst = path; *dst;/**/)
144 1.1 wdk *src++ = *dst++;
145 1.1 wdk for (dst = name; *dst;/**/)
146 1.1 wdk *src++ = *dst++;
147 1.1 wdk *src = (char) 0;
148 1.1 wdk
149 1.1 wdk if ((fd = open(bootfname, 0)) < 0) {
150 1.1 wdk printf("open %s: %d\n", bootfname, errno);
151 1.1 wdk goto err;
152 1.1 wdk }
153 1.1 wdk
154 1.1 wdk /* read the exec header */
155 1.1 wdk i = read(fd, (char *)&ehdr, sizeof(ehdr));
156 1.1 wdk if ((i != sizeof(ehdr)) ||
157 1.1 wdk (bcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) ||
158 1.1 wdk (ehdr.e_ident[EI_CLASS] != ELFCLASS32)) {
159 1.1 wdk printf("%s: No ELF header\n", bootfname);
160 1.1 wdk goto cerr;
161 1.1 wdk }
162 1.1 wdk
163 1.1 wdk for (i = 0; i < ehdr.e_phnum; i++) {
164 1.1 wdk if (lseek(fd, (off_t) ehdr.e_phoff + i * sizeof(phdr), 0) < 0)
165 1.1 wdk goto cerr;
166 1.1 wdk if (read(fd, &phdr, sizeof(phdr)) != sizeof(phdr))
167 1.1 wdk goto cerr;
168 1.1 wdk if (phdr.p_type != PT_LOAD)
169 1.1 wdk continue;
170 1.1 wdk if (lseek(fd, (off_t)phdr.p_offset, 0) < 0)
171 1.1 wdk goto cerr;
172 1.1 wdk if (read(fd, (char *)phdr.p_paddr, phdr.p_filesz) != phdr.p_filesz)
173 1.1 wdk goto cerr;
174 1.1 wdk }
175 1.1 wdk return ((entrypt)ehdr.e_entry);
176 1.1 wdk
177 1.1 wdk cerr:
178 1.1 wdk #ifndef LIBSA_NO_FS_CLOSE
179 1.1 wdk (void) close(fd);
180 1.1 wdk #endif
181 1.1 wdk err:
182 1.1 wdk printf("Can't load '%s'\n", bootfname);
183 1.1 wdk return ((entrypt)-1);
184 1.1 wdk }
185