1/*	$NetBSD: src/sys/arch/hp700/stand/boot/boot.c,v 1.11 2009-11-03 05:07:25 snj Exp $	*/
2
3/*-
4 * Copyright (c) 1982, 1986, 1990, 1993
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 *	@(#)boot.c	8.1 (Berkeley) 6/10/93
32 */
33
34/*
35 * Copyright (c) 1998-2004 Michael Shalayeff
36 * All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 *    notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 *    notice, this list of conditions and the following disclaimer in the
45 *    documentation and/or other materials provided with the distribution.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
48 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
50 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
51 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
52 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
53 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
55 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
56 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
57 * THE POSSIBILITY OF SUCH DAMAGE.
58 *
59 */
60
61#include <sys/param.h>
62#include <sys/reboot.h>
63#include <sys/boot_flag.h>
64
65#include <arch/hp700/stand/common/libsa.h>
66#include <lib/libsa/loadfile.h>
67
68#include <machine/pdc.h>
69#include <machine/vmparam.h>
70
71#include <arch/hp700/stand/common/dev_hppa.h>
72
73#include "bootinfo.h"
74
75/*
76 * Boot program... bits in `howto' determine whether boot stops to
77 * ask for system name.	 Boot device is derived from ROM provided
78 * information.
79 */
80
81#define	MAXLEN	100
82
83char line[MAXLEN];
84char devname_buffer[16];
85
86extern	u_int opendev;
87extern	char *lowram;
88extern	int noconsole;
89
90/*
91 * XXX UFS accepts a /, NFS doesn't.
92 */
93char *name;
94char *names[] = {
95	"netbsd",		"netbsd.gz",
96	"netbsd.bak",		"netbsd.bak.gz",
97	"netbsd.old",		"netbsd.old.gz",
98	"onetbsd",		"onetbsd.gz",
99	NULL
100};
101#define NUMNAMES	(sizeof(names) / sizeof(char *))
102
103void boot(dev_t boot_dev);
104int main(void);
105void getbootdev(int *);
106void exec_hp700(char *, u_long, int);
107
108int tgets(char *);
109void _rtt(void);
110
111typedef void (*startfuncp)(int, int, int, int, int, void *)
112    __attribute__ ((noreturn));
113
114int howto;
115
116void
117boot(dev_t boot_dev)
118{
119	machdep();
120#ifdef	DEBUGBUG
121	debug = 1;
122#endif
123	devboot(boot_dev, devname_buffer);
124	main();
125}
126
127int
128main(void)
129{
130	int currname = 0;
131	char *filename, filename_buffer[MAXLEN];
132
133	printf("\n");
134	printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev);
135	printf(">> Enter \"reset\" to reset system.\n");
136
137	for (;;) {
138		size_t size;
139
140		/* reset bootinfo structure */
141		bi_init();
142
143		name = names[currname++];
144		if (currname == NUMNAMES)
145			currname = 0;
146
147		if (!noconsole) {
148			howto = 0;
149			getbootdev(&howto);
150		} else
151			printf(": %s\n", name);
152		if (strchr(name, ':') != 0) {
153			filename = name;
154		} else {
155			strcpy(filename_buffer, devname_buffer);
156			strcat(filename_buffer, ":");
157			strcat(filename_buffer, name);
158			filename = filename_buffer;
159		}
160
161		size = sizeof(struct btinfo_common) + strlen(name) + 1;
162	        /* Impose limit (somewhat arbitrary) */
163		if (size < BOOTINFO_MAXSIZE / 2) {
164			union {
165				struct btinfo_kernelfile bi_file;
166				char x[size];
167			} U;
168			strcpy(U.bi_file.name, name);
169			BI_ADD(&U.bi_file, BTINFO_KERNELFILE, size);
170		}
171
172		exec_hp700(filename, 0, howto);
173		printf("boot: %s\n", strerror(errno));
174	}
175}
176
177void
178getbootdev(int *boot_howto)
179{
180	char c, *ptr = line;
181	int bdev, badapt, bctlr, bunit, bpart;
182
183	bdev   = B_TYPE(bootdev);
184	badapt = B_ADAPTOR(bootdev);
185	bctlr  = B_CONTROLLER(bootdev);
186	bunit  = B_UNIT(bootdev);
187	bpart  = B_PARTITION(bootdev);
188
189	printf("Boot: [[[%s%d%c:]%s][-a][-c][-d][-s][-v][-q]] :- ",
190	    devsw[bdev].dv_name, badapt << 8 | bctlr << 4 | bunit,
191	    'a' + bpart, name);
192
193	if (tgets(line)) {
194		if (strcmp(line, "reset") == 0) {
195			_rtt();
196			printf("panic: can't reboot\n");
197			return;
198		}
199		while ((c = *ptr) != '\0') {
200			while (c == ' ')
201				c = *++ptr;
202			if (!c)
203				return;
204			if (c == '-')
205				while ((c = *++ptr) && c != ' ')
206					BOOT_FLAG(c, *boot_howto);
207			else {
208				name = ptr;
209				while ((c = *++ptr) && c != ' ');
210				if (c)
211					*ptr++ = 0;
212			}
213		}
214	} else
215		printf("\n");
216}
217
218#define	round_to_size(x) \
219	(((x) + sizeof(u_long) - 1) & ~(sizeof(u_long) - 1))
220
221void
222exec_hp700(char *file, u_long loadaddr, int boot_howto)
223{
224#ifdef EXEC_DEBUG
225	extern int debug;
226	int i;
227#endif
228	struct btinfo_symtab bi_syms;
229	u_long marks[MARK_MAX];
230	int fd;
231
232	marks[MARK_START] = loadaddr;
233#ifdef EXEC_DEBUG
234	printf("file=%s loadaddr=%x howto=%x\n",
235		file, loadaddr, boot_howto);
236#endif
237	if ((fd = loadfile(file, marks, LOAD_KERNEL)) == -1)
238		return;
239
240	marks[MARK_END] = round_to_size(marks[MARK_END] - loadaddr);
241	printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n",
242	    marks[MARK_ENTRY], marks[MARK_NSYM],
243	    marks[MARK_SYM], marks[MARK_END]);
244
245#ifdef EXEC_DEBUG
246	if (debug) {
247		printf("ep=0x%x [", marks[MARK_ENTRY]);
248		for (i = 0; i < 10240; i++) {
249			if (!(i % 8)) {
250				printf("\b\n%p:", &((u_int *)marks[MARK_ENTRY])[i]);
251				if (getchar() != ' ')
252					break;
253			}
254			printf("%x,", ((int *)marks[MARK_ENTRY])[i]);
255		}
256		printf("\b\b ]\n");
257	}
258#endif
259
260	bi_syms.nsym = marks[MARK_NSYM];
261	bi_syms.ssym = marks[MARK_SYM];
262	bi_syms.esym = marks[MARK_END];
263	BI_ADD(&bi_syms, BTINFO_SYMTAB, sizeof(bi_syms));
264
265	fcacheall();
266
267	__asm("mtctl %r0, %cr17");
268	__asm("mtctl %r0, %cr17");
269
270	/* stack and the gung is ok at this point, so, no need for asm setup */
271	(*(startfuncp)(marks[MARK_ENTRY])) ((int)pdc, boot_howto, bootdev,
272	     marks[MARK_END], BOOTARG_APIVER, &bootinfo);
273	/* not reached */
274}
275