xref: /netbsd-src/sys/arch/sgimips/stand/common/boot.c (revision 5c95c9b7a37efa101e86f9452047eb8999be1d13)
1*5c95c9b7Schristos /*	$NetBSD: boot.c,v 1.20 2014/03/26 16:16:06 christos Exp $	*/
24e63f44fSthorpej 
34e63f44fSthorpej /*-
44e63f44fSthorpej  * Copyright (c) 1999 The NetBSD Foundation, Inc.
54e63f44fSthorpej  * All rights reserved.
64e63f44fSthorpej  *
74e63f44fSthorpej  * This code is derived from software contributed to The NetBSD Foundation
84e63f44fSthorpej  * by Jonathan Stone, Michael Hitch and Simon Burge.
94e63f44fSthorpej  *
104e63f44fSthorpej  * Redistribution and use in source and binary forms, with or without
114e63f44fSthorpej  * modification, are permitted provided that the following conditions
124e63f44fSthorpej  * are met:
134e63f44fSthorpej  * 1. Redistributions of source code must retain the above copyright
144e63f44fSthorpej  *    notice, this list of conditions and the following disclaimer.
154e63f44fSthorpej  * 2. Redistributions in binary form must reproduce the above copyright
164e63f44fSthorpej  *    notice, this list of conditions and the following disclaimer in the
174e63f44fSthorpej  *    documentation and/or other materials provided with the distribution.
184e63f44fSthorpej  *
194e63f44fSthorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
204e63f44fSthorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
214e63f44fSthorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
224e63f44fSthorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
234e63f44fSthorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
244e63f44fSthorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
254e63f44fSthorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
264e63f44fSthorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
274e63f44fSthorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
284e63f44fSthorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
294e63f44fSthorpej  * POSSIBILITY OF SUCH DAMAGE.
304e63f44fSthorpej  */
314e63f44fSthorpej 
324e63f44fSthorpej /*
334e63f44fSthorpej  * Copyright (c) 1992, 1993
344e63f44fSthorpej  *	The Regents of the University of California.  All rights reserved.
354e63f44fSthorpej  *
364e63f44fSthorpej  * This code is derived from software contributed to Berkeley by
374e63f44fSthorpej  * Ralph Campbell.
384e63f44fSthorpej  *
394e63f44fSthorpej  * Redistribution and use in source and binary forms, with or without
404e63f44fSthorpej  * modification, are permitted provided that the following conditions
414e63f44fSthorpej  * are met:
424e63f44fSthorpej  * 1. Redistributions of source code must retain the above copyright
434e63f44fSthorpej  *    notice, this list of conditions and the following disclaimer.
444e63f44fSthorpej  * 2. Redistributions in binary form must reproduce the above copyright
454e63f44fSthorpej  *    notice, this list of conditions and the following disclaimer in the
464e63f44fSthorpej  *    documentation and/or other materials provided with the distribution.
47aad01611Sagc  * 3. Neither the name of the University nor the names of its contributors
484e63f44fSthorpej  *    may be used to endorse or promote products derived from this software
494e63f44fSthorpej  *    without specific prior written permission.
504e63f44fSthorpej  *
514e63f44fSthorpej  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
524e63f44fSthorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
534e63f44fSthorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
544e63f44fSthorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
554e63f44fSthorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
564e63f44fSthorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
574e63f44fSthorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
584e63f44fSthorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
594e63f44fSthorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
604e63f44fSthorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
614e63f44fSthorpej  * SUCH DAMAGE.
624e63f44fSthorpej  *
634e63f44fSthorpej  *	@(#)boot.c	8.1 (Berkeley) 6/10/93
644e63f44fSthorpej  */
654e63f44fSthorpej 
664e63f44fSthorpej #include <lib/libsa/stand.h>
674e63f44fSthorpej #include <lib/libsa/loadfile.h>
684e63f44fSthorpej #include <lib/libkern/libkern.h>
694e63f44fSthorpej 
704e63f44fSthorpej #include <sys/param.h>
714e63f44fSthorpej #include <sys/exec.h>
724e63f44fSthorpej #include <sys/exec_elf.h>
73b3023f06Ssekiya #include <sys/boot_flag.h>
744e63f44fSthorpej 
754e63f44fSthorpej #include <dev/arcbios/arcbios.h>
764e63f44fSthorpej 
774e63f44fSthorpej #include "common.h"
784e63f44fSthorpej #include "bootinfo.h"
794e63f44fSthorpej 
804e63f44fSthorpej /*
814e63f44fSthorpej  * We won't go overboard with gzip'd kernel names.  After all we can
8257bd30a9Ssoren  * still boot a gzip'd kernel called "netbsd.sgimips" - it doesn't need
834e63f44fSthorpej  * the .gz suffix.
84b3023f06Ssekiya  *
85b3023f06Ssekiya  * For arcane reasons, the first byte of the first element of this struct will
86b3023f06Ssekiya  * contain a zero.  We therefore start from one.
874e63f44fSthorpej  */
88b3023f06Ssekiya 
894ad6bc9eStsutsui const char *kernelnames[] = {
90b3023f06Ssekiya 	"placekeeper",
914e63f44fSthorpej 	"netbsd.sgimips",
92b3023f06Ssekiya 	"netbsd",
93b3023f06Ssekiya 	"netbsd.gz",
944e63f44fSthorpej 	"netbsd.bak",
954e63f44fSthorpej 	"netbsd.old",
964e63f44fSthorpej 	"onetbsd",
974e63f44fSthorpej 	"gennetbsd",
984e63f44fSthorpej 	NULL
994e63f44fSthorpej };
1004e63f44fSthorpej 
101b3023f06Ssekiya static int debug = 0;
1024e63f44fSthorpej 
103b3023f06Ssekiya int main(int, char **);
1044e63f44fSthorpej 
1053975ceceSthorpej /* Storage must be static. */
1063975ceceSthorpej struct btinfo_symtab bi_syms;
1073975ceceSthorpej struct btinfo_bootpath bi_bpath;
1083975ceceSthorpej 
109de92c0b2Stsutsui static uint8_t bootinfo[BOOTINFO_SIZE];
110de92c0b2Stsutsui 
1114e63f44fSthorpej /*
112b3023f06Ssekiya  * This gets arguments from the ARCS monitor, calls ARCS routines to open
113b3023f06Ssekiya  * and load the program to boot, then transfers execution to the new program.
1144e63f44fSthorpej  *
115b3023f06Ssekiya  * argv[0] will be the ARCS path to the bootloader (i.e.,
116b3023f06Ssekiya  * "pci(0)scsi(0)disk(2)rdisk(0)partition(8)/boot.ip3").
117b3023f06Ssekiya  *
118b3023f06Ssekiya  * argv[1] through argv[n] will contain arguments passed from the PROM, if any.
1194e63f44fSthorpej  */
120b3023f06Ssekiya 
1214e63f44fSthorpej int
main(int argc,char ** argv)122b3023f06Ssekiya main(int argc, char **argv)
1234e63f44fSthorpej {
124bf739d01Smartin 	const char      *kernel = NULL;
125bf739d01Smartin 	const char      *bootpath = NULL;
126b3023f06Ssekiya 	char            bootfile[PATH_MAX];
1273975ceceSthorpej 	void            (*entry) (int, char *[], int, void *);
1284e63f44fSthorpej 	u_long          marks[MARK_MAX];
129b3023f06Ssekiya 	int             win = 0;
130b3023f06Ssekiya 	int             i;
131b3023f06Ssekiya 	int             ch;
1324e63f44fSthorpej 
1334e63f44fSthorpej 	/* print a banner */
1344e63f44fSthorpej 	printf("\n");
1354ad6bc9eStsutsui 	printf("%s " NETBSD_VERS " Bootstrap, Revision %s\n",
1364ad6bc9eStsutsui 	    bootprog_name, bootprog_rev);
1374e63f44fSthorpej 	printf("\n");
1384e63f44fSthorpej 
139b3023f06Ssekiya 	memset(marks, 0, sizeof marks);
1404e63f44fSthorpej 
1414e63f44fSthorpej 	/* initialise bootinfo structure early */
142de92c0b2Stsutsui 	bi_init(bootinfo);
1434e63f44fSthorpej 
144b3023f06Ssekiya 	/* Parse arguments, if present.  */
145b3023f06Ssekiya 	while ((ch = getopt(argc, argv, "v")) != -1) {
146b3023f06Ssekiya 		switch (ch) {
147b3023f06Ssekiya 		case 'v':
148b3023f06Ssekiya 			debug = 1;
149b3023f06Ssekiya 			break;
150b3023f06Ssekiya 		}
151b3023f06Ssekiya 	}
152b3023f06Ssekiya 
1534e63f44fSthorpej 	/*
1544e63f44fSthorpej 	 * How to find partition and file to load?
155b3023f06Ssekiya 	 *
1561ba2b99eSsekiya 	 * If argv[0] contains the string "cdrom(", we're probably doing an
1571ba2b99eSsekiya 	 * install.  The bootpath will therefore be partition 0 of whatever
1581ba2b99eSsekiya 	 * device we've booted from.  Derive the install kernel name from
1594ad6bc9eStsutsui 	 * the bootloader name ("ip3xboot", "ip2xboot", or "aoutboot").
1604e63f44fSthorpej 	 */
1614e63f44fSthorpej 
1624ad6bc9eStsutsui 	if (strstr(argv[0], "cdrom(")) {
163*5c95c9b7Schristos 		char *ep =
1641ba2b99eSsekiya 		strcpy(bootfile, argv[0]);
165*5c95c9b7Schristos 		ep = strrchr(bootfile, ')');
166*5c95c9b7Schristos 		i =  ep - bootfile;
1671ba2b99eSsekiya 		bootfile[i - 1] = '0';
1681ba2b99eSsekiya 		if (strstr(bootfile, "ip3x"))
169548d0c80Stsutsui 			kernel = "ip3x";
1701ba2b99eSsekiya 		else
171548d0c80Stsutsui 			kernel = "ip2x";
172*5c95c9b7Schristos 		strcpy(ep + 1, kernel);
17340c0a4c3Ssekiya 		if ((loadfile(bootfile, marks, LOAD_KERNEL)) >= 0)
1741ba2b99eSsekiya 			goto finish;
1751ba2b99eSsekiya 	}
1761ba2b99eSsekiya 
17723347d39Smatt 	bootpath = arcbios_GetEnvironmentVariable("OSLoadPartition");
178b3023f06Ssekiya 
179b3023f06Ssekiya 	if (bootpath == NULL) {
180b3023f06Ssekiya 		/* XXX need to actually do the fixup */
1814ad6bc9eStsutsui 		printf("\nPlease set the OSLoadPartition "
1824ad6bc9eStsutsui 		    "environment variable.\n");
183b3023f06Ssekiya 		return 0;
184b3023f06Ssekiya 	}
185b3023f06Ssekiya 
186b3023f06Ssekiya 	/*
187b3023f06Ssekiya 	 * Grab OSLoadFilename from ARCS.
188b3023f06Ssekiya 	 */
189b3023f06Ssekiya 
19023347d39Smatt 	kernel = arcbios_GetEnvironmentVariable("OSLoadFilename");
191b3023f06Ssekiya 
192b3023f06Ssekiya 	/*
193b3023f06Ssekiya 	 * argv[1] is assumed to contain the name of the kernel to boot,
194b3023f06Ssekiya 	 * if it a) does not start with a hyphen and b) does not contain
195b3023f06Ssekiya 	 * an equals sign.
196b3023f06Ssekiya 	 */
197b3023f06Ssekiya 
198b3023f06Ssekiya 	if (((strchr(argv[1], '=')) == NULL) && (argv[1][0] != '-'))
199b3023f06Ssekiya 		kernel = argv[1];
200b3023f06Ssekiya 
201b3023f06Ssekiya 	if (kernel != NULL) {
202b3023f06Ssekiya 		/*
203b3023f06Ssekiya 		 * if the name contains parenthesis, we assume that it
204b3023f06Ssekiya 		 * contains the bootpath and ignore anything passed through
205b3023f06Ssekiya 		 * the environment
206b3023f06Ssekiya 		 */
207b3023f06Ssekiya 		if (strchr(kernel, '('))
208b3023f06Ssekiya 			win = loadfile(kernel, marks, LOAD_KERNEL);
2094e63f44fSthorpej 		else {
210b3023f06Ssekiya 			strcpy(bootfile, bootpath);
211b3023f06Ssekiya 			strcat(bootfile, kernel);
212b3023f06Ssekiya 			win = loadfile(bootfile, marks, LOAD_KERNEL);
2134e63f44fSthorpej 		}
2144e63f44fSthorpej 
215b3023f06Ssekiya 	} else {
216b3023f06Ssekiya 		i = 1;
217b3023f06Ssekiya 		while (kernelnames[i] != NULL) {
218b3023f06Ssekiya 			strcpy(bootfile, bootpath);
219b3023f06Ssekiya 			strcat(bootfile, kernelnames[i]);
220b3023f06Ssekiya 			kernel = kernelnames[i];
221b3023f06Ssekiya 			win = loadfile(bootfile, marks, LOAD_KERNEL);
222b3023f06Ssekiya 			if (win != -1)
223b3023f06Ssekiya 				break;
224b3023f06Ssekiya 			i++;
225b3023f06Ssekiya 		}
226b3023f06Ssekiya 	}
227b3023f06Ssekiya 
228b3023f06Ssekiya 	if (win < 0) {
229b3023f06Ssekiya 		printf("Boot failed!  Halting...\n");
230b3023f06Ssekiya 		return 0;
231b3023f06Ssekiya 	}
2321ba2b99eSsekiya 
2331ba2b99eSsekiya finish:
23474d089c2Stsutsui 	strlcpy(bi_bpath.bootpath, bootfile, BTINFO_BOOTPATH_LEN);
235de92c0b2Stsutsui 	bi_add(&bi_bpath, BTINFO_BOOTPATH, sizeof(bi_bpath));
2364e63f44fSthorpej 
2374e63f44fSthorpej 	bi_syms.nsym = marks[MARK_NSYM];
2384e63f44fSthorpej 	bi_syms.ssym = marks[MARK_SYM];
2394e63f44fSthorpej 	bi_syms.esym = marks[MARK_END];
240de92c0b2Stsutsui 	bi_add(&bi_syms, BTINFO_SYMTAB, sizeof(bi_syms));
241b8166ca3Ssekiya 	entry = (void *)marks[MARK_ENTRY];
2424e63f44fSthorpej 
243b3023f06Ssekiya 	if (debug) {
2443975ceceSthorpej 		printf("Starting at %p\n\n", entry);
2453975ceceSthorpej 		printf("nsym 0x%lx ssym 0x%lx esym 0x%lx\n", marks[MARK_NSYM],
2463975ceceSthorpej 		       marks[MARK_SYM], marks[MARK_END]);
247b3023f06Ssekiya 	}
248de92c0b2Stsutsui 	(*entry)(argc, argv, BOOTINFO_MAGIC, bootinfo);
2493975ceceSthorpej 
2503975ceceSthorpej 	printf("Kernel returned!  Halting...\n");
2514ad6bc9eStsutsui 	return 0;
2524e63f44fSthorpej }
253