xref: /netbsd-src/sys/arch/mipsco/stand/common/boot.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: boot.c,v 1.3 2003/08/07 16:28:38 agc 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, Simon Burge and Wayne Knowles.
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 
81 #include <machine/prom.h>
82 
83 #include "common.h"
84 #include "bootinfo.h"
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.mipsco" - it doesn't need
89  * the .gz suffix.
90  */
91 char *kernelnames[] = {
92 	"netbsd",
93 	"netbsd.gz",
94 	"netbsd.bak",
95 	"netbsd.old",
96 	"netbsd.mipsco",
97 	"netbsd.elf",
98 	NULL
99 };
100 
101 
102 static char *devsplit __P((char *, char *));
103 int main __P((int, char **));
104 
105 /*
106  * This gets arguments from the first stage boot lader, calls PROM routines
107  * to open and load the program to boot, and then transfers execution to
108  * that new program.
109  */
110 int
111 main(argc, argv)
112 	int argc;
113 	char **argv;
114 {
115 	char *name, **namep, *dev, *kernel;
116 	char bootname[PATH_MAX], bootpath[PATH_MAX];
117 	int win;
118 	u_long marks[MARK_MAX];
119 	struct btinfo_symtab bi_syms;
120 	struct btinfo_bootpath bi_bpath;
121 	extern void prom_init __P((void));
122 	void (*entry) __P((int, char **, char **, u_int, char *));
123 
124 	prom_init();
125 
126 	/* print a banner */
127 	printf("\n");
128 	printf("NetBSD/mipsco " NETBSD_VERS " " BOOT_TYPE_NAME
129 	       " Bootstrap, Revision %s\n", bootprog_rev);
130 	printf("(%s, %s)\n\n", bootprog_maker, bootprog_date);
131 
132 	/* initialise bootinfo structure early */
133 	bi_init(BOOTINFO_ADDR);
134 
135 	dev = name = NULL;
136 	if (argc > 1) {
137 		kernel = devsplit(argv[1], bootname);
138 		if (*bootname) {
139 			dev = bootname;
140 			if (*kernel)
141 				name = argv[1];
142 			++argv;
143 			--argc;
144 		}
145 
146 	}
147 	if (dev == NULL) {
148 		(void) devsplit(argv[0], bootname);
149 		dev = bootname;
150 	}
151 
152 	memset(marks, 0, sizeof marks);
153 	if (name != NULL)
154 		win = (loadfile(name, marks, LOAD_KERNEL) == 0);
155 	else {
156 		win = 0;
157 		for (namep = kernelnames, win = 0; *namep != NULL && !win;
158 		    namep++) {
159 			kernel = *namep;
160 			strcpy(bootpath, dev);
161 			strcat(bootpath, kernel);
162 			printf("Loading: %s\n", bootpath);
163 			win = (loadfile(bootpath, marks, LOAD_ALL) != -1);
164 			if (win) {
165 				name = bootpath;
166 			}
167 		}
168 	}
169 	if (!win)
170 		goto fail;
171 
172 	strncpy(bi_bpath.bootpath, kernel, BTINFO_BOOTPATH_LEN);
173 	bi_add(&bi_bpath, BTINFO_BOOTPATH, sizeof(bi_bpath));
174 
175 	entry = (void *) marks[MARK_ENTRY];
176 	bi_syms.nsym = marks[MARK_NSYM];
177 	bi_syms.ssym = marks[MARK_SYM];
178 	bi_syms.esym = marks[MARK_END];
179 	bi_add(&bi_syms, BTINFO_SYMTAB, sizeof(bi_syms));
180 
181 	printf("Starting at 0x%x\n\n", (u_int)entry);
182 
183 	(*entry)(argc, argv, NULL, BOOTINFO_MAGIC, (char *)BOOTINFO_ADDR);
184 
185 	(void)printf("KERNEL RETURNED!\n");
186 
187 fail:
188 	(void)printf("Boot failed!  Halting...\n");
189 	return (0);
190 }
191 
192 /*
193  * strip out device name and kernel name
194  */
195 static char *
196 devsplit(fname, devname)
197 	char *fname, *devname;
198 {
199 	char *src, *dst;
200 
201 	dst = devname;
202 	for (src = fname; *src;/**/)
203 		if ((*dst++ = *src++) == ')') {
204 			*dst = (char) 0;
205 			return src;
206 		}
207 	*devname = (char) 0;
208 	return fname;
209 }
210