xref: /netbsd-src/sys/arch/newsmips/stand/boot/boot.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: boot.c,v 1.20 2014/03/26 17:43:11 christos Exp $	*/
2 
3 /*-
4  * Copyright (C) 1999 Tsubai Masanari.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <lib/libkern/libkern.h>
30 #include <lib/libsa/stand.h>
31 #include <lib/libsa/loadfile.h>
32 
33 #include <machine/apcall.h>
34 #include <machine/bootinfo.h>
35 #include <machine/cpu.h>
36 #include <machine/romcall.h>
37 
38 void boot(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t);
39 
40 void mips1_flushicache(void *, int);
41 extern char _edata[], _end[];
42 
43 /* version strings in vers.c (generated by newvers.sh) */
44 extern const char bootprog_name[];
45 extern const char bootprog_rev[];
46 
47 struct apbus_sysinfo *_sip;
48 int apbus;
49 
50 char *devs[] = { "sd", "fh", "fd", NULL, NULL, "rd", "st" };
51 char *kernels[] = { "/netbsd", "/netbsd.gz", NULL };
52 
53 #ifdef BOOT_DEBUG
54 # define DPRINTF printf
55 #else
56 # define DPRINTF while (0) printf
57 #endif
58 
59 void
60 boot(uint32_t a0, uint32_t a1, uint32_t a2, uint32_t a3, uint32_t a4,
61     uint32_t a5)
62 {
63 	int fd, i;
64 	char *netbsd = "";
65 	int maxmem;
66 	u_long marks[MARK_MAX];
67 	char devname[32], file[32];
68 	void (*entry)(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t,
69 	    uint32_t);
70 	struct btinfo_symtab bi_sym;
71 	struct btinfo_bootarg bi_arg;
72 	struct btinfo_bootpath bi_bpath;
73 	struct btinfo_systype bi_sys;
74 	int loadflag;
75 
76 	/* Clear BSS. */
77 	memset(_edata, 0, _end - _edata);
78 
79 	/*
80 	 * XXX a3 contains:
81 	 *     maxmem (nws-3xxx)
82 	 *     argv   (apbus-based machine)
83 	 */
84 	if (a3 >= 0x80000000)
85 		apbus = 1;
86 	else
87 		apbus = 0;
88 
89 	if (apbus)
90 		_sip = (void *)a4;
91 
92 	printf("%s Secondary Boot, Revision %s\n",
93 	    bootprog_name, bootprog_rev);
94 
95 	if (apbus) {
96 		char *bootdev = (char *)a1;
97 		int argc = a2;
98 		char **argv = (char **)a3;
99 
100 		DPRINTF("APbus-based system\n");
101 
102 		DPRINTF("argc = %d\n", argc);
103 		for (i = 0; i < argc; i++) {
104 			DPRINTF("argv[%d] = %s\n", i, argv[i]);
105 			if (argv[i][0] != '-' && *netbsd == 0)
106 				netbsd = argv[i];
107 		}
108 		maxmem = _sip->apbsi_memsize;
109 		maxmem -= 0x100000;	/* reserve 1MB for ROM monitor */
110 
111 		DPRINTF("howto = 0x%x\n", a0);
112 		DPRINTF("bootdev = %s\n", (char *)a1);
113 		DPRINTF("bootname = %s\n", netbsd);
114 		DPRINTF("maxmem = 0x%x\n", maxmem);
115 
116 		/* XXX use "sonic()" instead of "tftp()" */
117 		if (strncmp(bootdev, "tftp", 4) == 0)
118 			bootdev = "sonic";
119 
120 		strcpy(devname, bootdev);
121 		if (strchr(devname, '(') == NULL)
122 			strcat(devname, "()");
123 	} else {
124 		int bootdev = a1;
125 		char *bootname = (char *)a2;
126 		int ctlr, unit, part, type;
127 
128 		DPRINTF("HB system.\n");
129 
130 		/* bootname is "/boot" by default on HB system. */
131 		if (bootname && strcmp(bootname, "/boot") != 0)
132 			netbsd = bootname;
133 		maxmem = a3;
134 
135 		DPRINTF("howto = 0x%x\n", a0);
136 		DPRINTF("bootdev = 0x%x\n", a1);
137 		DPRINTF("bootname = %s\n", netbsd);
138 		DPRINTF("maxmem = 0x%x\n", maxmem);
139 
140 		ctlr = BOOTDEV_CTLR(bootdev);
141 		unit = BOOTDEV_UNIT(bootdev);
142 		part = BOOTDEV_PART(bootdev);
143 		type = BOOTDEV_TYPE(bootdev);
144 
145 		if (devs[type] == NULL) {
146 			printf("unknown bootdev (0x%x)\n", bootdev);
147 			_rtt();
148 		}
149 
150 		snprintf(devname, sizeof(devname), "%s(%d,%d,%d)",
151 		    devs[type], ctlr, unit, part);
152 	}
153 
154 	printf("Booting %s%s\n", devname, netbsd);
155 
156 	/* use user specified kernel name if exists */
157 	if (*netbsd) {
158 		kernels[0] = netbsd;
159 		kernels[1] = NULL;
160 	}
161 
162 	loadflag = LOAD_KERNEL;
163 	if (devname[0] == 'f')	/* XXX */
164 		loadflag &= ~LOAD_BACKWARDS;
165 
166 	marks[MARK_START] = 0;
167 
168 	for (i = 0; kernels[i]; i++) {
169 		snprintf(file, sizeof(file), "%s%s", devname, kernels[i]);
170 		DPRINTF("trying %s...\n", file);
171 		fd = loadfile(file, marks, loadflag);
172 		if (fd != -1)
173 			break;
174 	}
175 	if (kernels[i] == NULL)
176 		_rtt();
177 
178 	DPRINTF("entry = 0x%x\n", (int)marks[MARK_ENTRY]);
179 	DPRINTF("ssym = 0x%x\n", (int)marks[MARK_SYM]);
180 	DPRINTF("esym = 0x%x\n", (int)marks[MARK_END]);
181 
182 	bi_init(BOOTINFO_ADDR);
183 
184 	bi_sym.nsym = marks[MARK_NSYM];
185 	bi_sym.ssym = marks[MARK_SYM];
186 	bi_sym.esym = marks[MARK_END];
187 	bi_add(&bi_sym, BTINFO_SYMTAB, sizeof(bi_sym));
188 
189 	bi_arg.howto = a0;
190 	bi_arg.bootdev = a1;
191 	bi_arg.maxmem = maxmem;
192 	bi_arg.sip = (int)_sip;
193 	bi_add(&bi_arg, BTINFO_BOOTARG, sizeof(bi_arg));
194 
195 	strcpy(bi_bpath.bootpath, file);
196 	bi_add(&bi_bpath, BTINFO_BOOTPATH, sizeof(bi_bpath));
197 
198 	bi_sys.type = apbus ? NEWS5000 : NEWS3400;		/* XXX */
199 	bi_add(&bi_sys, BTINFO_SYSTYPE, sizeof(bi_sys));
200 
201 	entry = (void *)marks[MARK_ENTRY];
202 
203 	if (apbus)
204 		apcall_flushcache();
205 	else
206 		mips1_flushicache(entry, marks[MARK_SYM] - marks[MARK_ENTRY]);
207 
208 	printf("\n");
209 	(*entry)(a0, a1, a2, a3, a4, a5);
210 }
211 
212 void
213 putchar(int x)
214 {
215 	char c = x;
216 
217 	if (apbus)
218 		apcall_write(1, &c, 1);
219 	else
220 		rom_write(1, &c, 1);
221 }
222 
223 int
224 getchar(void)
225 {
226 	unsigned char c = '\0';
227 	int i;
228 
229 	for (;;) {
230 		i = apbus ? apcall_read(1, &c, 1) : rom_read(1, &c, 1);
231 		if (i == 1)
232 			break;
233 		if (i != -2 && i != 0)
234 			return -1;
235 	}
236 	return c;
237 }
238 
239 void
240 _rtt(void)
241 {
242 	if (apbus)
243 		apcall_exit(8);
244 	else
245 		rom_halt();
246 
247 	for (;;);
248 }
249