boot.c revision 1.3 1 /* $NetBSD: boot.c,v 1.3 2005/06/09 16:02:19 he Exp $ */
2
3 /*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jonathan Stone, Michael Hitch and Simon Burge.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Copyright (c) 1992, 1993
41 * The Regents of the University of California. All rights reserved.
42 *
43 * This code is derived from software contributed to Berkeley by
44 * Ralph Campbell.
45 *
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
48 * are met:
49 * 1. Redistributions of source code must retain the above copyright
50 * notice, this list of conditions and the following disclaimer.
51 * 2. Redistributions in binary form must reproduce the above copyright
52 * notice, this list of conditions and the following disclaimer in the
53 * documentation and/or other materials provided with the distribution.
54 * 3. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 *
70 * @(#)boot.c 8.1 (Berkeley) 6/10/93
71 */
72
73 #include <lib/libsa/stand.h>
74 #include <lib/libsa/loadfile.h>
75 #include <lib/libkern/libkern.h>
76
77 #include <sys/param.h>
78 #include <sys/exec.h>
79 #include <sys/exec_elf.h>
80 #include <sys/boot_flag.h>
81
82 #include <dev/arcbios/arcbios.h>
83
84 #include "common.h"
85 #include "bootinfo.h"
86
87 #ifdef BOOT_DEBUG
88 #define DPRINTF if (debug) printf
89 #else
90 #define DPRINTF while (/*CONSTCOND*/0) printf
91 #endif
92
93 /*
94 * We won't go overboard with gzip'd kernel names. After all we can
95 * still boot a gzip'd kernel called "netbsd.arc" - it doesn't need
96 * the .gz suffix.
97 *
98 * For arcane reasons, the first byte of the first element of this struct will
99 * contain a zero. We therefore start from one.
100 */
101
102 char *kernelnames[] = {
103 "placekeeper",
104 "netbsd.arc",
105 "netbsd",
106 "netbsd.gz",
107 "netbsd.bak",
108 "netbsd.old",
109 "onetbsd",
110 "gennetbsd",
111 NULL
112 };
113
114 static int debug = 0;
115 static char **environment;
116
117 /* Storage must be static. */
118 struct btinfo_symtab bi_syms;
119 struct btinfo_bootpath bi_bpath;
120
121 extern const struct arcbios_fv *ARCBIOS;
122
123 int main(int, char **);
124 static char *firmware_getenv(char *);
125
126 /*
127 * This gets arguments from the ARCS monitor, calls ARCS routines to open
128 * and load the program to boot, then transfers execution to the new program.
129 *
130 * argv[0] will be the ARCS path to the bootloader (i.e.,
131 * "scsi(0)disk(2)rdisk(0)partition(1)\boot").
132 *
133 * argv[1] through argv[n] will contain arguments passed from the PROM, if any.
134 */
135
136 int
137 main(int argc, char **argv)
138 {
139 const char *kernel = NULL;
140 const char *bootpath = NULL;
141 char bootfile[PATH_MAX];
142 void (*entry)(int, char *[], int, void *);
143 u_long marks[MARK_MAX];
144 int win = 0;
145 int i;
146 int ch;
147
148 /* print a banner */
149 printf("\n");
150 printf("%s Bootstrap, Revision %s\n", bootprog_name, bootprog_rev);
151 printf("(%s, %s)\n", bootprog_maker, bootprog_date);
152
153 memset(marks, 0, sizeof marks);
154
155 /* initialise bootinfo structure early */
156 bi_init();
157
158 #ifdef BOOT_DEBUG
159 for (i = 0; i < argc; i++)
160 printf("argv[%d] = %s\n", i, argv[i]);
161 #endif
162
163 /* Parse arguments, if present. */
164 while ((ch = getopt(argc, argv, "v")) != -1) {
165 switch (ch) {
166 case 'v':
167 debug = 1;
168 break;
169 }
170 }
171
172 environment = &argv[1];
173
174 bootpath = firmware_getenv("OSLoadPartition");
175 if (bootpath == NULL)
176 bootpath =
177 (*ARCBIOS->GetEnvironmentVariable)("OSLoadPartition");
178
179 if (bootpath == NULL) {
180 /* XXX need to actually do the fixup */
181 printf("OSLoadPartition is not specified.\n");
182 return 0;
183 }
184 DPRINTF("bootpath = %s\n", bootpath);
185
186 /*
187 * Grab OSLoadFilename from ARCS.
188 */
189
190 kernel = firmware_getenv("OSLoadFilename");
191 if (kernel == NULL)
192 kernel = (*ARCBIOS->GetEnvironmentVariable)("OSLoadFilename");
193
194 DPRINTF("kernel = %s\n", kernel ? kernel : "<null>");
195
196 /*
197 * The first arg is assumed to contain the name of the kernel to boot,
198 * if it a) does not start with a hyphen and b) does not contain
199 * an equals sign.
200 */
201
202 for (i = 1; i < argc; i++) {
203 if (((strchr(argv[i], '=')) == NULL) && (argv[i][0] != '-')) {
204 kernel = argv[i];
205 break;
206 }
207 }
208
209 if (kernel != NULL) {
210 /*
211 * if the name contains parenthesis, we assume that it
212 * contains the bootpath and ignore anything passed through
213 * the environment
214 */
215 if (strchr(kernel, '('))
216 win = loadfile(kernel, marks, LOAD_KERNEL);
217 else {
218 strcpy(bootfile, bootpath);
219 strcat(bootfile, kernel);
220 win = loadfile(bootfile, marks, LOAD_KERNEL);
221 }
222
223 } else {
224 i = 1;
225 while (kernelnames[i] != NULL) {
226 strcpy(bootfile, bootpath);
227 strcat(bootfile, kernelnames[i]);
228 kernel = kernelnames[i];
229 win = loadfile(bootfile, marks, LOAD_KERNEL);
230 if (win != -1)
231 break;
232 i++;
233 }
234
235 }
236
237 if (win < 0) {
238 printf("Boot failed! Halting...\n");
239 (void)getchar();
240 return 0;
241 }
242
243 #if 0
244 strncpy(bi_bpath.bootpath, kernel, BTINFO_BOOTPATH_LEN);
245 bi_add(&bi_bpath, BTINFO_BOOTPATH);
246
247 bi_syms.nsym = marks[MARK_NSYM];
248 bi_syms.ssym = marks[MARK_SYM];
249 bi_syms.esym = marks[MARK_END];
250 bi_add(&bi_syms, BTINFO_SYMTAB);
251 #endif
252 entry = (void *)marks[MARK_ENTRY];
253
254 if (debug) {
255 printf("Starting at %p\n\n", entry);
256 printf("nsym 0x%lx ssym 0x%lx esym 0x%lx\n", marks[MARK_NSYM],
257 marks[MARK_SYM], marks[MARK_END]);
258 }
259 (*entry)(argc, argv, 0 /* BOOTINFO_MAGIC */, NULL);
260
261 printf("Kernel returned! Halting...\n");
262 return 0;
263 }
264
265 char *
266 firmware_getenv(char *envname)
267 {
268 char **env;
269 int len;
270
271 len = strlen(envname);
272
273 for (env = environment; env[0]; env++) {
274 if (strncasecmp(envname, env[0], len) == 0 &&
275 env[0][len] == '=') {
276 return &env[0][len + 1];
277 }
278 }
279 return NULL;
280 }
281
282 void
283 _rtt(void)
284 {
285
286 (*ARCBIOS->Halt)();
287 }
288