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