1/*	$NetBSD: boot.c,v 1.4 2007/01/24 13:08:12 hubertf Exp $	*/
2
3/*-
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Steve C. Woodford.
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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/*
33 * Copyright (c) 1982, 1986, 1990, 1993
34 *	The Regents of the University of California.  All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 *    notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 *    notice, this list of conditions and the following disclaimer in the
43 *    documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the University nor the names of its contributors
45 *    may be used to endorse or promote products derived from this software
46 *    without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * 	@(#)boot.c	8.1 (Berkeley) 6/10/93
61 */
62
63#include <sys/param.h>
64#include <sys/reboot.h>
65#include <sys/termios.h>
66#include <sys/ttydefaults.h>
67#include <machine/bootinfo.h>
68
69#include <lib/libkern/libkern.h>
70
71#include "stand.h"
72#include "libsa.h"
73#include "bugsyscalls.h"
74
75struct bug_bootinfo	bug_bootinfo;
76struct mvmeppc_bootinfo	bootinfo;
77
78static u_int32_t ioctrl2cflag(u_int32_t);
79
80void
81main(void)
82{
83	struct bug_buginfo *bbi;
84	struct bug_boardid *bid;
85	struct bug_ioinquiry *ioi, ioinq;
86	struct bug_ioctrl ioctrl;
87	char consname[CONSOLEDEV_LEN];
88	char line[80];
89	const char *file;
90	int ask = 0, howto, part;
91
92	if (bug_bootinfo.bbi_bugmode == 0)
93		panic("mvmeppc-boot: PReP boot mode not supported!");
94
95	bbi = &bug_bootinfo.bbi_bi.bbi;
96
97	if ((bid = bugsys_brdid()) == NULL)
98		panic("mvmeppc-boot: bugsys_brdid() failed!");
99
100	ioinq.ii_boardname = consname;
101	ioinq.ii_ioctrl = &ioctrl;
102	ioinq.ii_portnum = BUG_IOINQ_PORT_CONSOLE;
103	if ((ioi = bugsys_ioinq(&ioinq)) == NULL)
104		panic("mvmeppc-boot: bugsys_ioinq() failed!");
105
106	if (bid->bi_devtype > 9)
107		panic("mvmeppc-boot: Bogus boot device type (%d)",
108		    bid->bi_devtype);
109
110	printf(">> MVMEPPC boot on MVME%x\n", bid->bi_bnumber);
111
112	parse_args(bbi->bbi_argstart, bbi->bbi_argend, &file, &howto, &part);
113
114	for (;;) {
115		if (ask) {
116			printf("boot: ");
117			gets(line);
118			if (strcmp(line, "halt") == 0)
119				break;
120
121			if (line[0]) {
122				char *cp = line;
123
124				while (cp < (line + sizeof(line) - 1) && *cp)
125					cp++;
126
127				bbi->bbi_argstart = line;
128				bbi->bbi_argend = cp;
129				parse_args(bbi->bbi_argstart, bbi->bbi_argend,
130				    &file, &howto, &part);
131			}
132		}
133
134		bootinfo.bi_boothowto = howto;
135		bootinfo.bi_bootaddr = bbi->bbi_devaddr;
136		bootinfo.bi_bootclun = bbi->bbi_clun;
137		bootinfo.bi_bootclun = bbi->bbi_dlun;
138		strncpy(bootinfo.bi_bootline, bbi->bbi_argstart,
139		    MIN(BOOTLINE_LEN, bbi->bbi_argend - bbi->bbi_argstart));
140		strncpy(bootinfo.bi_consoledev, consname, CONSOLEDEV_LEN);
141		bootinfo.bi_consoleaddr = ioi->ii_devaddr;
142		bootinfo.bi_consolechan = ioi->ii_channel;
143		bootinfo.bi_consolespeed = ioctrl.ic_baud;
144		bootinfo.bi_consolecflag = ioctrl2cflag(ioctrl.ic_ctrlbits);
145		bootinfo.bi_modelnumber = bid->bi_bnumber;
146
147		exec_mvme(file, howto, part);
148		printf("boot: %s: %s\n", file, strerror(errno));
149		ask = 1;
150	}
151}
152
153static u_int32_t
154ioctrl2cflag(u_int32_t ctrlbits)
155{
156	u_int32_t rv;
157
158	rv = TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB | PARODD);
159
160	/* Convert parity */
161	if (ctrlbits & IOCTRL_PARITY_ODD)
162		rv |= PARENB | PARODD;
163	else
164	if (ctrlbits & IOCTRL_PARITY_EVEN)
165		rv |= PARENB;
166
167	/* Convert character length */
168	if (ctrlbits & IOCTRL_BITS_8)
169		rv |= CS8;
170	else
171	if (ctrlbits & IOCTRL_BITS_7)
172		rv |= CS7;
173	else
174	if (ctrlbits & IOCTRL_BITS_6)
175		rv |= CS6;
176	else
177	if (ctrlbits & IOCTRL_BITS_5)
178		rv |= CS5;
179	else
180		panic("ioctrl2cflag: Bad character length: 0x%x", ctrlbits);
181
182	/* Convert number of stop bits */
183	if (ctrlbits & IOCTRL_STOP_2)
184		rv |= CSTOPB;
185	else
186	if ((ctrlbits & IOCTRL_STOP_1) == 0)
187		panic("ioctrl2cflag: Bad number of stop bits: 0x%x", ctrlbits);
188
189	return (rv);
190}
191