xref: /netbsd-src/sys/arch/sgimips/stand/common/boot.c (revision 267197ec1eebfcb9810ea27a89625b6ddf68e3e7)
1 /*	$NetBSD: boot.c,v 1.13 2008/01/26 14:35:24 tsutsui 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 /*
88  * We won't go overboard with gzip'd kernel names.  After all we can
89  * still boot a gzip'd kernel called "netbsd.sgimips" - it doesn't need
90  * the .gz suffix.
91  *
92  * For arcane reasons, the first byte of the first element of this struct will
93  * contain a zero.  We therefore start from one.
94  */
95 
96 char           *kernelnames[] = {
97 	"placekeeper",
98 	"netbsd.sgimips",
99 	"netbsd",
100 	"netbsd.gz",
101 	"netbsd.bak",
102 	"netbsd.old",
103 	"onetbsd",
104 	"gennetbsd",
105 	NULL
106 };
107 
108 extern const struct arcbios_fv *ARCBIOS;
109 static int      debug = 0;
110 
111 int             main(int, char **);
112 
113 /* Storage must be static. */
114 struct btinfo_symtab bi_syms;
115 struct btinfo_bootpath bi_bpath;
116 
117 static uint8_t bootinfo[BOOTINFO_SIZE];
118 
119 /*
120  * This gets arguments from the ARCS monitor, calls ARCS routines to open
121  * and load the program to boot, then transfers execution to the new program.
122  *
123  * argv[0] will be the ARCS path to the bootloader (i.e.,
124  * "pci(0)scsi(0)disk(2)rdisk(0)partition(8)/boot.ip3").
125  *
126  * argv[1] through argv[n] will contain arguments passed from the PROM, if any.
127  */
128 
129 int
130 main(int argc, char **argv)
131 {
132 	const char      *kernel = NULL;
133 	const char      *bootpath = NULL;
134 	char            bootfile[PATH_MAX];
135 	void            (*entry) (int, char *[], int, void *);
136 	u_long          marks[MARK_MAX];
137 	int             win = 0;
138 	int             i;
139 	int             ch;
140 
141 	/* print a banner */
142 	printf("\n");
143 	printf("NetBSD/sgimips " NETBSD_VERS " Bootstrap, Revision %s\n",
144 	       bootprog_rev);
145 	printf("(%s, %s)\n", bootprog_maker, bootprog_date);
146 	printf("\n");
147 
148 	memset(marks, 0, sizeof marks);
149 
150 	/* initialise bootinfo structure early */
151 	bi_init(bootinfo);
152 
153 	/* Parse arguments, if present.  */
154 
155 	while ((ch = getopt(argc, argv, "v")) != -1) {
156 		switch (ch) {
157 		case 'v':
158 			debug = 1;
159 			break;
160 		}
161 	}
162 
163 	/*
164 	 * How to find partition and file to load?
165 	 *
166 	 * If argv[0] contains the string "cdrom(", we're probably doing an
167 	 * install.  The bootpath will therefore be partition 0 of whatever
168 	 * device we've booted from.  Derive the install kernel name from
169 	 * the bootloader name ("ip32boot", "ip22boot", or "aoutboot").
170 	 */
171 
172 	if (strstr(argv[0], "cdrom("))
173 	{
174 		strcpy(bootfile, argv[0]);
175 		i = (strrchr(bootfile, ')') - bootfile);
176 		bootfile[i-1] = '0';
177 		if (strstr(bootfile, "ip3x"))
178 			sprintf( (strrchr(bootfile, ')') + 1), "ip3x");
179 		else
180 			sprintf( (strrchr(bootfile, ')') + 1), "ip2x");
181 		if ( (loadfile(bootfile, marks, LOAD_KERNEL)) >= 0 )
182 			goto finish;
183 	}
184 
185 	bootpath = ARCBIOS->GetEnvironmentVariable("OSLoadPartition");
186 
187 	if (bootpath == NULL) {
188 		/* XXX need to actually do the fixup */
189 		printf("\nPlease set the OSLoadPartition environment variable.\n");
190 		return 0;
191 	}
192 
193 	/*
194 	 * Grab OSLoadFilename from ARCS.
195 	 */
196 
197 	kernel = ARCBIOS->GetEnvironmentVariable("OSLoadFilename");
198 
199 	/*
200 	 * argv[1] is assumed to contain the name of the kernel to boot,
201 	 * if it a) does not start with a hyphen and b) does not contain
202 	 * an equals sign.
203 	 */
204 
205 	if (((strchr(argv[1], '=')) == NULL) && (argv[1][0] != '-'))
206 		kernel = argv[1];
207 
208 	if (kernel != NULL) {
209 		/*
210 		 * if the name contains parenthesis, we assume that it
211 		 * contains the bootpath and ignore anything passed through
212 		 * the environment
213 		 */
214 		if (strchr(kernel, '('))
215 			win = loadfile(kernel, marks, LOAD_KERNEL);
216 		else {
217 			strcpy(bootfile, bootpath);
218 			strcat(bootfile, kernel);
219 			win = loadfile(bootfile, marks, LOAD_KERNEL);
220 		}
221 
222 	} else {
223 		i = 1;
224 		while (kernelnames[i] != NULL) {
225 			strcpy(bootfile, bootpath);
226 			strcat(bootfile, kernelnames[i]);
227 			kernel = kernelnames[i];
228 			win = loadfile(bootfile, marks, LOAD_KERNEL);
229 			if (win != -1)
230 				break;
231 			i++;
232 		}
233 
234 	}
235 
236 	if (win < 0) {
237 		printf("Boot failed!  Halting...\n");
238 		return 0;
239 	}
240 
241 finish:
242 	strlcpy(bi_bpath.bootpath, kernel, BTINFO_BOOTPATH_LEN);
243 	bi_add(&bi_bpath, BTINFO_BOOTPATH, sizeof(bi_bpath));
244 
245 	bi_syms.nsym = marks[MARK_NSYM];
246 	bi_syms.ssym = marks[MARK_SYM];
247 	bi_syms.esym = marks[MARK_END];
248 	bi_add(&bi_syms, BTINFO_SYMTAB, sizeof(bi_syms));
249 	entry = (void *)marks[MARK_ENTRY];
250 
251 	if (debug) {
252 		printf("Starting at %p\n\n", entry);
253 		printf("nsym 0x%lx ssym 0x%lx esym 0x%lx\n", marks[MARK_NSYM],
254 		       marks[MARK_SYM], marks[MARK_END]);
255 	}
256 	(*entry)(argc, argv, BOOTINFO_MAGIC, bootinfo);
257 
258 	printf("Kernel returned!  Halting...\n");
259 	return (0);
260 }
261