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