xref: /netbsd-src/sys/arch/vax/boot/boot/boot.c (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1 /*	$NetBSD: boot.c,v 1.11 2000/07/13 03:13:40 matt Exp $ */
2 /*-
3  * Copyright (c) 1982, 1986 The Regents of the University of California.
4  * 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. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by the University of
17  *	California, Berkeley and its contributors.
18  * 4. 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	7.15 (Berkeley) 5/4/91
35  */
36 
37 #include "sys/param.h"
38 #include "sys/reboot.h"
39 #include "lib/libsa/stand.h"
40 #include "lib/libkern/libkern.h"
41 
42 #define V750UCODE(x)    ((x>>8)&255)
43 
44 #include "machine/rpb.h"
45 
46 #include "vaxstand.h"
47 
48 /*
49  * Boot program... arguments passed in r10 and r11 determine
50  * whether boot stops to ask for system name and which device
51  * boot comes from.
52  */
53 
54 char line[100];
55 int	bootdev, debug;
56 extern	unsigned opendev;
57 
58 void	usage(char *), boot(char *), halt(char *);
59 void	Xmain(void);
60 void	autoconf(void);
61 int	getsecs(void);
62 int	setjmp(int *);
63 int	testkey(void);
64 void	loadpcs(void);
65 
66 const struct vals {
67 	char	*namn;
68 	void	(*func)(char *);
69 	char	*info;
70 } val[] = {
71 	{"?", usage, "Show this help menu"},
72 	{"help", usage, "Same as '?'"},
73 	{"boot", boot, "Load and execute file"},
74 	{"halt", halt, "Halts the system"},
75 	{0, 0},
76 };
77 
78 static struct {
79 	char name[12];
80 	int quiet;
81 } filelist[] = {
82 	{ "netbsd.vax", 1 },
83 	{ "netbsd", 0 },
84 	{ "netbsd.gz", 0 },
85 	{ "netbsd.old", 0 },
86 	{ "gennetbsd", 0 },
87 	{ "", 0 },
88 };
89 
90 int jbuf[10];
91 int sluttid, senast, skip, askname;
92 struct rpb bootrpb;
93 
94 void
95 Xmain(void)
96 {
97 	int io;
98 	int j, nu;
99 
100 	io = 0;
101 	skip = 1;
102 	autoconf();
103 
104 	askname = bootrpb.rpb_bootr5 & RB_ASKNAME;
105 	printf("\n\r>> NetBSD/vax boot [%s %s] <<\n", __DATE__, __TIME__);
106 	printf(">> Press any key to abort autoboot  ");
107 	sluttid = getsecs() + 5;
108 	senast = 0;
109 	skip = 0;
110 	setjmp(jbuf);
111 	for (;;) {
112 		nu = sluttid - getsecs();
113 		if (senast != nu)
114 			printf("%c%d", 8, nu);
115 		if (nu <= 0)
116 			break;
117 		senast = nu;
118 		if ((j = (testkey() & 0177))) {
119 			skip = 1;
120 			if (j != 10 && j != 13) {
121 				printf("\nPress '?' for help");
122 				askname = 1;
123 			}
124 			break;
125 		}
126 	}
127 	skip = 1;
128 	printf("\n");
129 
130 	/* First try to autoboot */
131 	if (askname == 0) {
132 		int fileindex;
133 		for (fileindex = 0; filelist[fileindex].name[0] != '\0';
134 		    fileindex++) {
135 			errno = 0;
136 			if (!filelist[fileindex].quiet)
137 				printf("> boot %s\n", filelist[fileindex].name);
138 			exec(filelist[fileindex].name, 0, 0);
139 			if (!filelist[fileindex].quiet)
140 				printf("%s: boot failed: %s\n",
141 				    filelist[fileindex].name, strerror(errno));
142 #if 0 /* Will hang VAX 4000 machines */
143 			if (testkey())
144 				break;
145 #endif
146 		}
147 	}
148 
149 	/* If any key pressed, go to conversational boot */
150 	for (;;) {
151 		const struct vals *v = &val[0];
152 		char *c, *d;
153 
154 		printf("> ");
155 		gets(line);
156 
157 		c = line;
158 		while (*c == ' ')
159 			c++;
160 
161 		if (c[0] == 0)
162 			continue;
163 
164 		if ((d = index(c, ' ')))
165 			*d++ = 0;
166 
167 		while (v->namn) {
168 			if (strcmp(v->namn, c) == 0)
169 				break;
170 			v++;
171 		}
172 		if (v->namn)
173 			(*v->func)(d);
174 		else
175 			printf("Unknown command: %s\n", c);
176 	}
177 }
178 
179 void
180 halt(char *hej)
181 {
182 	asm("halt");
183 }
184 
185 void
186 boot(char *arg)
187 {
188 	char *fn = "netbsd";
189 
190 	if (arg) {
191 		while (*arg == ' ')
192 			arg++;
193 
194 		if (*arg != '-') {
195 			fn = arg;
196 			if ((arg = index(arg, ' '))) {
197 				*arg++ = 0;
198 				while (*arg == ' ')
199 					arg++;
200 			} else
201 				goto load;
202 		}
203 		if (*arg != '-') {
204 fail:			printf("usage: boot [filename] [-asd]\n");
205 			return;
206 		}
207 
208 		while (*++arg) {
209 			if (*arg == 'a')
210 				bootrpb.rpb_bootr5 |= RB_ASKNAME;
211 			else if (*arg == 'd')
212 				bootrpb.rpb_bootr5 |= RB_KDB;
213 			else if (*arg == 's')
214 				bootrpb.rpb_bootr5 |= RB_SINGLE;
215 			else
216 				goto fail;
217 		}
218 	}
219 load:	exec(fn, 0, 0);
220 	printf("Boot failed: %s\n", strerror(errno));
221 }
222 
223 /* 750 Patchable Control Store magic */
224 
225 #include "../include/mtpr.h"
226 #include "../include/cpu.h"
227 #include "../include/sid.h"
228 #define	PCS_BITCNT	0x2000		/* number of patchbits */
229 #define	PCS_MICRONUM	0x400		/* number of ucode locs */
230 #define	PCS_PATCHADDR	0xf00000	/* start addr of patchbits */
231 #define	PCS_PCSADDR	(PCS_PATCHADDR+0x8000)	/* start addr of pcs */
232 #define	PCS_PATCHBIT	(PCS_PATCHADDR+0xc000)	/* patchbits enable reg */
233 #define	PCS_ENABLE	0xfff00000	/* enable bits for pcs */
234 
235 #define	extzv(one, two, three,four)	\
236 ({			\
237 	asm __volatile (" extzv %0,%3,(%1),(%2)+"	\
238 			:			\
239 			: "g"(one),"g"(two),"g"(three),"g"(four));	\
240 })
241 
242 
243 void
244 loadpcs()
245 {
246 	static int pcsdone = 0;
247 	int mid = mfpr(PR_SID);
248 	int i, j, *ip, *jp;
249 	char pcs[100];
250 	char *cp;
251 
252 	if ((mid >> 24) != VAX_750 || ((mid >> 8) & 255) < 95 || pcsdone)
253 		return;
254 	printf("Updating 11/750 microcode: ");
255 	for (cp = line; *cp; cp++)
256 		if (*cp == ')' || *cp == ':')
257 			break;
258 	if (*cp) {
259 		bcopy(line, pcs, 99);
260 		pcs[99] = 0;
261 		i = cp - line + 1;
262 	} else
263 		i = 0;
264 	strcpy(pcs + i, "pcs750.bin");
265 	i = open(pcs, 0);
266 	if (i < 0) {
267 		printf("bad luck - missing pcs750.bin :-(\n");
268 		return;
269 	}
270 	/*
271 	 * We ask for more than we need to be sure we get only what we expect.
272 	 * After read:
273 	 *	locs 0 - 1023	packed patchbits
274 	 *	 1024 - 11264	packed microcode
275 	 */
276 	if (read(i, (char *)0, 23*512) != 22*512) {
277 		printf("Error reading %s\n", pcs);
278 		close(i);
279 		return;
280 	}
281 	close(i);
282 
283 	/*
284 	 * Enable patchbit loading and load the bits one at a time.
285 	 */
286 	*((int *)PCS_PATCHBIT) = 1;
287 	ip = (int *)PCS_PATCHADDR;
288 	jp = (int *)0;
289 	for (i=0; i < PCS_BITCNT; i++) {
290 		extzv(i,jp,ip,1);
291 	}
292 	*((int *)PCS_PATCHBIT) = 0;
293 
294 	/*
295 	 * Load PCS microcode 20 bits at a time.
296 	 */
297 	ip = (int *)PCS_PCSADDR;
298 	jp = (int *)1024;
299 	for (i=j=0; j < PCS_MICRONUM * 4; i+=20, j++) {
300 		extzv(i,jp,ip,20);
301 	}
302 
303 	/*
304 	 * Enable PCS.
305 	 */
306 	i = *jp;		/* get 1st 20 bits of microcode again */
307 	i &= 0xfffff;
308 	i |= PCS_ENABLE;	/* reload these bits with PCS enable set */
309 	*((int *)PCS_PCSADDR) = i;
310 
311 	mid = mfpr(PR_SID);
312 	printf("new rev level=%d\n", V750UCODE(mid));
313 	pcsdone = 1;
314 }
315 
316 void
317 usage(char *hej)
318 {
319 	const struct vals *v = &val[0];
320 
321 	printf("Commands:\n");
322 	while (v->namn) {
323 		printf("%s\t%s\n", v->namn, v->info);
324 		v++;
325 	}
326 }
327