xref: /netbsd-src/sys/arch/sbmips/stand/common/boot.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /* $NetBSD: boot.c,v 1.5 2009/03/18 16:00:14 cegger Exp $ */
2 
3 /*
4  * Copyright (c) 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Ralph Campbell.
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. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)boot.c	8.1 (Berkeley) 6/10/93
35  */
36 
37 #include <lib/libsa/stand.h>
38 #include <lib/libkern/libkern.h>
39 #include <lib/libsa/loadfile.h>
40 
41 #include <sys/param.h>
42 #include <sys/exec.h>
43 #include <sys/exec_ecoff.h>
44 
45 #include "stand/common/common.h"
46 #include "stand/common/cfe_api.h"
47 
48 #include <machine/autoconf.h>
49 
50 #if !defined(UNIFIED_BOOTBLOCK) && !defined(SECONDARY_BOOTBLOCK)
51 #error not UNIFIED_BOOTBLOCK and not SECONDARY_BOOTBLOCK
52 #endif
53 
54 char boot_file[128];
55 char boot_flags[128];
56 extern int debug;
57 
58 struct bootinfo_v1 bootinfo;
59 
60 paddr_t ffp_save, ptbr_save;
61 
62 int debug;
63 
64 char *kernelnames[] = {
65 	"netbsd",		"netbsd.gz",
66 	"netbsd.bak",		"netbsd.bak.gz",
67 	"netbsd.old",		"netbsd.old.gz",
68 	"onetbsd",		"onetbsd.gz",
69 	"netbsd.mips",		"netbsd.mips.gz",
70 	NULL
71 };
72 
73 int
74 #if defined(UNIFIED_BOOTBLOCK)
75 main(long fwhandle,long evector,long fwentry,long fwseal)
76 #else /* defined(SECONDARY_BOOTBLOCK) */
77 main(long fwhandle,long fd,long fwentry)
78 #endif
79 {
80 	char *name, **namep;
81 	u_long marks[MARK_MAX];
82 	u_int32_t entry;
83 	int win;
84 
85 	/* Init prom callback vector. */
86 	cfe_init(fwhandle,fwentry);
87 
88 	/* Init the console device */
89 	init_console();
90 
91 	/* print a banner */
92 	printf("\n");
93 	printf("NetBSD/sbmips " NETBSD_VERS " " BOOT_TYPE_NAME " Bootstrap, Revision %s\n",
94 	    bootprog_rev);
95 	printf("(%s, %s)\n", bootprog_maker, bootprog_date);
96 	printf("\n");
97 
98 	/* set up the booted device descriptor */
99 #if defined(UNIFIED_BOOTBLOCK)
100         if (!booted_dev_open()) {
101                 printf("Boot device (%s) open failed.\n",
102 		    booted_dev_name[0] ? booted_dev_name : "unknown");
103                 goto fail;
104         }
105 #else /* defined(SECONDARY_BOOTBLOCK) */
106 	booted_dev_setfd(fd);
107 #endif
108 
109 	printf("\n");
110 
111 	boot_file[0] = 0;
112 	cfe_getenv("KERNEL_FILE",boot_file,sizeof(boot_file));
113 
114 	boot_flags[0] = 0;
115 	cfe_getenv("BOOT_FLAGS",boot_flags,sizeof(boot_flags));
116 
117 	if (boot_file[0] != 0)
118 		(void)printf("Boot file: %s\n", boot_file);
119 	(void)printf("Boot flags: %s\n", boot_flags);
120 
121 	if (strchr(boot_flags, 'i') || strchr(boot_flags, 'I')) {
122 		printf("Boot file: ");
123 		gets(boot_file);
124 	}
125 
126 	memset(marks, 0, sizeof marks);
127 	if (boot_file[0] != '\0') {
128 		name = boot_file;
129 		win = loadfile(name, marks, LOAD_KERNEL) == 0;
130 	} else {
131 		name = NULL;	/* XXX gcc -Wuninitialized */
132 		for (namep = kernelnames, win = 0; *namep != NULL && !win;
133 		    namep++)
134 			win = loadfile(name = *namep, marks, LOAD_KERNEL) == 0;
135 	}
136 
137 	entry = marks[MARK_ENTRY];
138 	booted_dev_close();
139 	printf("\n");
140 	if (!win)
141 		goto fail;
142 
143 	(void)printf("Entering %s at 0x%lx...\n", name, (long)entry);
144 
145 	cfe_flushcache(0);
146 
147 	memset(&bootinfo, 0,sizeof(bootinfo));
148 	bootinfo.version = BOOTINFO_VERSION;
149 	bootinfo.reserved = 0;
150 	bootinfo.ssym = marks[MARK_SYM];
151 	bootinfo.esym = marks[MARK_END];
152 	strncpy(bootinfo.boot_flags,boot_flags,sizeof(bootinfo.boot_flags));
153 	strncpy(bootinfo.booted_kernel,name,sizeof(bootinfo.booted_kernel));
154 	bootinfo.fwhandle = fwhandle;
155 	bootinfo.fwentry = fwentry;
156 
157 	(*(void (*)(long,long,long,long))entry)(fwhandle,
158 						BOOTINFO_MAGIC,
159 						(long)&bootinfo,
160 						fwentry);
161 
162 	(void)printf("KERNEL RETURNED!\n");
163 
164 fail:
165 	(void)printf("Boot failed!  Halting...\n");
166 	halt();
167 	return 1;
168 }
169 
170 
171 
172