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