xref: /netbsd/sys/arch/sgimips/stand/common/boot.c (revision 6550d01e)
1 /*	$NetBSD: boot.c,v 1.18 2011/01/22 19:19:23 joerg 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 extern const struct arcbios_fv *ARCBIOS;
102 static int debug = 0;
103 
104 int main(int, char **);
105 
106 /* Storage must be static. */
107 struct btinfo_symtab bi_syms;
108 struct btinfo_bootpath bi_bpath;
109 
110 static uint8_t bootinfo[BOOTINFO_SIZE];
111 
112 /*
113  * This gets arguments from the ARCS monitor, calls ARCS routines to open
114  * and load the program to boot, then transfers execution to the new program.
115  *
116  * argv[0] will be the ARCS path to the bootloader (i.e.,
117  * "pci(0)scsi(0)disk(2)rdisk(0)partition(8)/boot.ip3").
118  *
119  * argv[1] through argv[n] will contain arguments passed from the PROM, if any.
120  */
121 
122 int
123 main(int argc, char **argv)
124 {
125 	const char      *kernel = NULL;
126 	const char      *bootpath = NULL;
127 	char            bootfile[PATH_MAX];
128 	void            (*entry) (int, char *[], int, void *);
129 	u_long          marks[MARK_MAX];
130 	int             win = 0;
131 	int             i;
132 	int             ch;
133 
134 	/* print a banner */
135 	printf("\n");
136 	printf("%s " NETBSD_VERS " Bootstrap, Revision %s\n",
137 	    bootprog_name, bootprog_rev);
138 	printf("\n");
139 
140 	memset(marks, 0, sizeof marks);
141 
142 	/* initialise bootinfo structure early */
143 	bi_init(bootinfo);
144 
145 	/* Parse arguments, if present.  */
146 	while ((ch = getopt(argc, argv, "v")) != -1) {
147 		switch (ch) {
148 		case 'v':
149 			debug = 1;
150 			break;
151 		}
152 	}
153 
154 	/*
155 	 * How to find partition and file to load?
156 	 *
157 	 * If argv[0] contains the string "cdrom(", we're probably doing an
158 	 * install.  The bootpath will therefore be partition 0 of whatever
159 	 * device we've booted from.  Derive the install kernel name from
160 	 * the bootloader name ("ip3xboot", "ip2xboot", or "aoutboot").
161 	 */
162 
163 	if (strstr(argv[0], "cdrom(")) {
164 		strcpy(bootfile, argv[0]);
165 		i = (strrchr(bootfile, ')') - bootfile);
166 		bootfile[i - 1] = '0';
167 		if (strstr(bootfile, "ip3x"))
168 			kernel = "ip3x";
169 		else
170 			kernel = "ip2x";
171 		sprintf((strrchr(bootfile, ')') + 1), kernel);
172 		if ((loadfile(bootfile, marks, LOAD_KERNEL)) >= 0)
173 			goto finish;
174 	}
175 
176 	bootpath = ARCBIOS->GetEnvironmentVariable("OSLoadPartition");
177 
178 	if (bootpath == NULL) {
179 		/* XXX need to actually do the fixup */
180 		printf("\nPlease set the OSLoadPartition "
181 		    "environment variable.\n");
182 		return 0;
183 	}
184 
185 	/*
186 	 * Grab OSLoadFilename from ARCS.
187 	 */
188 
189 	kernel = ARCBIOS->GetEnvironmentVariable("OSLoadFilename");
190 
191 	/*
192 	 * argv[1] 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 	if (((strchr(argv[1], '=')) == NULL) && (argv[1][0] != '-'))
198 		kernel = argv[1];
199 
200 	if (kernel != NULL) {
201 		/*
202 		 * if the name contains parenthesis, we assume that it
203 		 * contains the bootpath and ignore anything passed through
204 		 * the environment
205 		 */
206 		if (strchr(kernel, '('))
207 			win = loadfile(kernel, marks, LOAD_KERNEL);
208 		else {
209 			strcpy(bootfile, bootpath);
210 			strcat(bootfile, kernel);
211 			win = loadfile(bootfile, marks, LOAD_KERNEL);
212 		}
213 
214 	} else {
215 		i = 1;
216 		while (kernelnames[i] != NULL) {
217 			strcpy(bootfile, bootpath);
218 			strcat(bootfile, kernelnames[i]);
219 			kernel = kernelnames[i];
220 			win = loadfile(bootfile, marks, LOAD_KERNEL);
221 			if (win != -1)
222 				break;
223 			i++;
224 		}
225 	}
226 
227 	if (win < 0) {
228 		printf("Boot failed!  Halting...\n");
229 		return 0;
230 	}
231 
232 finish:
233 	strlcpy(bi_bpath.bootpath, bootfile, BTINFO_BOOTPATH_LEN);
234 	bi_add(&bi_bpath, BTINFO_BOOTPATH, sizeof(bi_bpath));
235 
236 	bi_syms.nsym = marks[MARK_NSYM];
237 	bi_syms.ssym = marks[MARK_SYM];
238 	bi_syms.esym = marks[MARK_END];
239 	bi_add(&bi_syms, BTINFO_SYMTAB, sizeof(bi_syms));
240 	entry = (void *)marks[MARK_ENTRY];
241 
242 	if (debug) {
243 		printf("Starting at %p\n\n", entry);
244 		printf("nsym 0x%lx ssym 0x%lx esym 0x%lx\n", marks[MARK_NSYM],
245 		       marks[MARK_SYM], marks[MARK_END]);
246 	}
247 	(*entry)(argc, argv, BOOTINFO_MAGIC, bootinfo);
248 
249 	printf("Kernel returned!  Halting...\n");
250 	return 0;
251 }
252