xref: /netbsd-src/sys/arch/luna68k/stand/boot/init_main.c (revision 07e9cd7be8b88db0a863c974e90e7604d41aea42)
1 /*	$NetBSD: init_main.c,v 1.17 2024/09/24 11:17:54 rin Exp $	*/
2 
3 /*
4  * Copyright (c) 1992 OMRON Corporation.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * OMRON Corporation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by the University of
20  *	California, Berkeley and its contributors.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *	@(#)init_main.c	8.2 (Berkeley) 8/15/93
38  */
39 /*
40  * Copyright (c) 1992, 1993
41  *	The Regents of the University of California.  All rights reserved.
42  *
43  * This code is derived from software contributed to Berkeley by
44  * OMRON Corporation.
45  *
46  * Redistribution and use in source and binary forms, with or without
47  * modification, are permitted provided that the following conditions
48  * are met:
49  * 1. Redistributions of source code must retain the above copyright
50  *    notice, this list of conditions and the following disclaimer.
51  * 2. Redistributions in binary form must reproduce the above copyright
52  *    notice, this list of conditions and the following disclaimer in the
53  *    documentation and/or other materials provided with the distribution.
54  * 3. Neither the name of the University nor the names of its contributors
55  *    may be used to endorse or promote products derived from this software
56  *    without specific prior written permission.
57  *
58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68  * SUCH DAMAGE.
69  *
70  *	@(#)init_main.c	8.2 (Berkeley) 8/15/93
71  */
72 
73 #include <sys/param.h>
74 #include <sys/boot_flag.h>
75 #include <machine/cpu.h>
76 #include <luna68k/stand/boot/samachdep.h>
77 #include <luna68k/stand/boot/romvec.h>
78 #include <luna68k/stand/boot/status.h>
79 #include <lib/libsa/loadfile.h>
80 #ifdef SUPPORT_ETHERNET
81 #include <lib/libsa/dev_net.h>
82 #endif
83 
84 static int get_plane_numbers(void);
85 static int reorder_dipsw(int);
86 
87 int cpuspeed;	/* for DELAY() macro */
88 int hz = 60;
89 int machtype;
90 char default_file[64];
91 const char *default_bootdev;
92 int default_unit;
93 
94 #define	VERS_LOCAL	"Phase-31"
95 
96 int nplane;
97 
98 /* for command parser */
99 
100 #define BUFFSIZE 100
101 #define MAXARGS  30
102 
103 char buffer[BUFFSIZE];
104 
105 int   argc;
106 char *argv[MAXARGS];
107 
108 #define BOOT_TIMEOUT 10
109 int boot_timeout = BOOT_TIMEOUT;
110 
111 static const char prompt[] = "boot> ";
112 
113 /*
114  * PROM monitor's boot device info
115  */
116 
117 /* LUNA-I monitor's bootinfo structure */
118 /* This bootinfo data address is investigated on ROM Ver4.22 and Ver4.25 */
119 #define	LUNA1_BOOTINFOADDR	0x000008c0
120 struct luna1_bootinfo {
121 	uint8_t	bi_xxx1[3];	/* 0x08c0: ??? */
122 	uint8_t	bi_device;	/* 0x08c3: boot device */
123 #define	LUNA1_BTDEV_DK	0		/* Hard-Disk */
124 #define	LUNA1_BTDEV_FB	1		/* Floppy-Disk */
125 #define	LUNA1_BTDEV_SD	2		/* Streamer-Tape */
126 #define	LUNA1_BTDEV_P0	3		/* RS232c */
127 #define	LUNA1_BTDEV_ET	4		/* Ether-net */
128 #define	LUNA1_NBTDEV	5
129 
130 	struct {
131 		uint8_t	bd_xxx1;	/*  0: ??? */
132 		uint8_t	bd_boot;	/*  1: 1 == booted */
133 		char	bd_name[2];	/*  2: device name (dk, fb, sd ... ) */
134 		uint8_t	bd_drv;		/*  4: drive number (not ID) */
135 		uint8_t	bd_xxx2;	/*  5: ??? */
136 		uint8_t	bd_xxx3;	/*  6: ??? */
137 		uint8_t	bd_part;	/*  7: dk partition / st record # */
138 		uint8_t	bd_xxx4[4];	/*  8: ??? */
139 		uint8_t	bd_xxx5[4];	/* 12: ??? */
140 	} bi_devinfo[LUNA1_NBTDEV];
141 } __packed;
142 
143 /* LUNA-II monitor's bootinfo structure */
144 /* This bootinfo data address is investigated on ROM Version 1.11 */
145 #define	LUNA2_BOOTINFOADDR	0x00001d80
146 struct luna2_bootinfo {
147 	uint8_t	bi_xxx1[13];	/* 0x1d80: ??? */
148 	uint8_t	bi_device;	/* 0x1d8d: boot device */
149 #define	LUNA2_BTDEV_DK	0		/* Hard-Disk */
150 #define	LUNA2_BTDEV_FT	1		/* Floppy-Disk */
151 #define	LUNA2_BTDEV_SD	2		/* Streamer-Tape */
152 #define	LUNA2_BTDEV_P0	3		/* RS232c */
153 #define	LUNA2_NBTDEV	4
154 
155 	struct {
156 		uint8_t	bd_xxx1;	/*  0: ??? */
157 		uint8_t	bd_boot;	/*  1: 1 == booted */
158 		char	bd_name[4];	/*  2: device name (dk, ft, sd ... ) */
159 		uint8_t	bd_xxx2;	/*  6: ??? */
160 		uint8_t	bd_ctlr;	/*  7: SCSI controller number */
161 		uint8_t	bd_id;		/*  8: SCSI ID number */
162 		uint8_t	bd_xxx3;	/*  9: device number index? */
163 		uint8_t	bd_xxx4;	/* 10: ??? */
164 		uint8_t	bd_part;	/* 11: dk partition / st record # */
165 		uint8_t	bd_xxx5[4];	/* 12: ??? */
166 		uint8_t	bd_xxx6[4];	/* 16: ??? */
167 	} bi_devinfo[LUNA2_NBTDEV];
168 } __packed;
169 
170 /* #define BTINFO_DEBUG */
171 
172 void
173 main(void)
174 {
175 	int i, status = ST_NORMAL;
176 	const char *machstr;
177 	const char *bootdev;
178 	uint32_t howto;
179 	int unit, part;
180 	int bdev, ctlr, id;
181 
182 	/*
183 	 * Initialize the console before we print anything out.
184 	 */
185 	if (cputype == CPU_68030) {
186 		machtype = LUNA_I;
187 		machstr  = "LUNA-I";
188 		cpuspeed = MHZ_25;
189 		hz = 60;
190 	} else {
191 		machtype = LUNA_II;
192 		machstr  = "LUNA-II";
193 		cpuspeed = MHZ_25 * 2;	/* XXX */
194 		hz = 100;
195 	}
196 
197 	nplane = get_plane_numbers();
198 
199 	cninit();
200 
201 	printf("\n");
202 	printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev);
203 	printf(">> (based on Stinger ver 0.0 [%s])\n", VERS_LOCAL);
204 	printf("\n");
205 
206 	i = ROM_memsize;
207 	printf("Machine model   = %s\n", machstr);
208 	printf("Physical Memory = 0x%x  ", i);
209 	i >>= 20;
210 	printf("(%d MB)\n", i);
211 	printf("\n");
212 
213 	/*
214 	 * IO configuration
215 	 */
216 
217 #ifdef SUPPORT_ETHERNET
218 	try_bootp = 1;
219 #endif
220 
221 	find_devs();
222 	printf("\n");
223 
224 	/* use sd(0,0) for the default boot device */
225 	bootdev = "sd";
226 	unit = 0;
227 	part = 0;
228 
229 /*
230  * - LUNA1_BOOTINFOADDR is in the 0-th page [0, 0x1000), which causes
231  *   -Warray-bounds for GCC12 and later.
232  * - LUNA2_BOOTINFOADDR is also in the 0-th page, if we switch to
233  *   8KB page.
234  */
235 #pragma GCC diagnostic push
236 #pragma GCC diagnostic ignored "-Warray-bounds"
237 
238 	if (machtype == LUNA_I) {
239 		const struct luna1_bootinfo *bi1 = (void *)LUNA1_BOOTINFOADDR;
240 
241 		bdev = bi1->bi_device;
242 		switch (bdev) {
243 		case LUNA1_BTDEV_DK:
244 			/* note: bd_drv is not SCSI ID */
245 			ctlr = 0;
246 			id   = 6 - bi1->bi_devinfo[bdev].bd_drv;
247 			unit = UNIT(ctlr, id);
248 			break;
249 		case LUNA1_BTDEV_ET:
250 			bootdev = "le";
251 			unit = 0;
252 			break;
253 		default:
254 			/* not supported */
255 			break;
256 		}
257 #ifdef BTINFO_DEBUG
258 		printf("bi1->bi_device = 0x%02x\n", bi1->bi_device);
259 		printf("bi1->bi_devinfo[bdev].bd_boot = 0x%02x\n",
260 		    bi1->bi_devinfo[bdev].bd_boot);
261 		printf("bi1->bi_devinfo[bdev].bd_name = %c%c\n",
262 		    bi1->bi_devinfo[bdev].bd_name[0],
263 		    bi1->bi_devinfo[bdev].bd_name[1]);
264 		printf("bi1->bi_devinfo[bdev].bd_drv = 0x%02x\n",
265 		    bi1->bi_devinfo[bdev].bd_drv);
266 		printf("bi1->bi_devinfo[bdev].bd_part = 0x%02x\n",
267 		    bi1->bi_devinfo[bdev].bd_part);
268 #endif
269 	} else {
270 		const struct luna2_bootinfo *bi2 = (void *)LUNA2_BOOTINFOADDR;
271 
272 		bdev = bi2->bi_device;
273 		switch (bdev) {
274 		case LUNA2_BTDEV_DK:
275 			ctlr = bi2->bi_devinfo[bdev].bd_ctlr;
276 			id   = bi2->bi_devinfo[bdev].bd_id;
277 			unit = UNIT(ctlr, id);
278 			break;
279 		default:
280 			/* not supported */
281 			break;
282 		}
283 #ifdef BTINFO_DEBUG
284 		printf("bi2->bi_device = 0x%02x\n", bi2->bi_device);
285 		printf("bi2->bi_devinfo[bdev].bd_boot = 0x%02x\n",
286 		    bi2->bi_devinfo[bdev].bd_boot);
287 		printf("bi2->bi_devinfo[bdev].bd_name = %s\n",
288 		    bi2->bi_devinfo[bdev].bd_name);
289 		printf("bi2->bi_devinfo[bdev].bd_ctlr = 0x%02x\n",
290 		    bi2->bi_devinfo[bdev].bd_ctlr);
291 		printf("bi2->bi_devinfo[bdev].bd_id = 0x%02x\n",
292 		    bi2->bi_devinfo[bdev].bd_id);
293 		printf("bi2->bi_devinfo[bdev].bd_part = 0x%02x\n",
294 		    bi2->bi_devinfo[bdev].bd_part);
295 #endif
296 	}
297 
298 #pragma GCC diagnostic pop
299 
300 	snprintf(default_file, sizeof(default_file),
301 	    "%s(%d,%d)%s", bootdev, unit, part, "netbsd");
302 	default_bootdev = bootdev;
303 	default_unit = unit;
304 
305 	howto = reorder_dipsw(dipsw2);
306 
307 	if ((howto & 0xFE) == 0) {
308 		char c;
309 
310 		printf("Press return to boot now,"
311 		    " any other key for boot menu\n");
312 		printf("booting %s - starting in ", default_file);
313 		c = awaitkey("%d seconds. ", boot_timeout, true);
314 		if (c == '\r' || c == '\n' || c == 0) {
315 			printf("auto-boot %s\n", default_file);
316 			bootnetbsd(default_file, 0);
317 		}
318 	}
319 
320 	/*
321 	 * Main Loop
322 	 */
323 
324 	printf("type \"help\" for help.\n");
325 
326 	do {
327 		memset(buffer, 0, BUFFSIZE);
328 		if (getline(prompt, buffer) > 0) {
329 			argc = getargs(buffer, argv,
330 			    sizeof(argv) / sizeof(char *));
331 
332 			status = parse(argc, argv);
333 			if (status == ST_NOTFOUND)
334 				printf("Command \"%s\" is not found !!\n",
335 				    argv[0]);
336 		}
337 	} while (status != ST_EXIT);
338 
339 	exit(0);
340 }
341 
342 static int
343 get_plane_numbers(void)
344 {
345 	int r = ROM_plane;
346 	int n = 0;
347 
348 	for (; r ; r >>= 1)
349 		if (r & 0x1)
350 			n++;
351 
352 	return n;
353 }
354 
355 static int
356 reorder_dipsw(int dipsw)
357 {
358 	int i, sw = 0;
359 
360 	for (i = 0; i < 8; i++) {
361 		if ((dipsw & 0x01) == 0)
362 			sw += 1;
363 
364 		if (i == 7)
365 			break;
366 
367 		sw <<= 1;
368 		dipsw >>= 1;
369 	}
370 
371 	return sw;
372 }
373