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