xref: /netbsd-src/sys/arch/amiga/stand/bootblock/bootxx_ffs/main.c (revision 9084173a5d6e1167781424012990806c24982425)
1e1d8eca6Smhitch /*
2*9084173aSrin  * $NetBSD: main.c,v 1.10 2022/04/29 07:18:17 rin Exp $
3e1d8eca6Smhitch  *
4e1d8eca6Smhitch  *
5e1d8eca6Smhitch  * Copyright (c) 1996,1999 Ignatios Souvatzis
6e1d8eca6Smhitch  * Copyright (c) 1994 Michael L. Hitch
7e1d8eca6Smhitch  * All rights reserved.
8e1d8eca6Smhitch  *
9e1d8eca6Smhitch  * Redistribution and use in source and binary forms, with or without
10e1d8eca6Smhitch  * modification, are permitted provided that the following conditions
11e1d8eca6Smhitch  * are met:
12e1d8eca6Smhitch  * 1. Redistributions of source code must retain the above copyright
13e1d8eca6Smhitch  *    notice, this list of conditions and the following disclaimer.
14e1d8eca6Smhitch  * 2. Redistributions in binary form must reproduce the above copyright
15e1d8eca6Smhitch  *    notice, this list of conditions and the following disclaimer in the
16e1d8eca6Smhitch  *    documentation and/or other materials provided with the distribution.
17e1d8eca6Smhitch  *
18e1d8eca6Smhitch  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19e1d8eca6Smhitch  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20e1d8eca6Smhitch  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21e1d8eca6Smhitch  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22e1d8eca6Smhitch  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23e1d8eca6Smhitch  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24e1d8eca6Smhitch  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25e1d8eca6Smhitch  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26e1d8eca6Smhitch  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27e1d8eca6Smhitch  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28e1d8eca6Smhitch  *
29e1d8eca6Smhitch  */
30e1d8eca6Smhitch 
31e1d8eca6Smhitch #include <sys/cdefs.h>
32e1d8eca6Smhitch #include <sys/reboot.h>
33e1d8eca6Smhitch #include <sys/types.h>
34e1d8eca6Smhitch 
35e1d8eca6Smhitch #include <sys/exec_aout.h>
36e1d8eca6Smhitch 
37e1d8eca6Smhitch #include <amiga/cfdev.h>
38e1d8eca6Smhitch #include <amiga/memlist.h>
39e1d8eca6Smhitch #include <include/cpu.h>
40e1d8eca6Smhitch 
41e1d8eca6Smhitch #include <saerrno.h>
42e63501d2Sjunyoung #include <lib/libsa/stand.h>
439eb4d007Stsutsui #include <lib/libkern/libkern.h>
44e1d8eca6Smhitch 
45e1d8eca6Smhitch #include "libstubs.h"
46e1d8eca6Smhitch #include "samachdep.h"
47e1d8eca6Smhitch 
48e8cc3884Sthorpej #undef AOUT_LDPGSZ
49e8cc3884Sthorpej #define AOUT_LDPGSZ 8192
50e1d8eca6Smhitch #define __PGSZ 8192
51e1d8eca6Smhitch 
52e1d8eca6Smhitch #define DRACOREVISION	(*(u_int8_t *)0x02000009)
53e1d8eca6Smhitch #define DRACOMMUMARGIN	0x200000
54e1d8eca6Smhitch #define DRACOZ2OFFSET	0x3000000
55e1d8eca6Smhitch #define DRACOZ2MAX	0x1000000
56e1d8eca6Smhitch 
57e1d8eca6Smhitch #define EXECMIN 36
58e1d8eca6Smhitch 
59e1d8eca6Smhitch /*
60e1d8eca6Smhitch  * vers.c (generated by newvers.sh)
61e1d8eca6Smhitch  */
62e1d8eca6Smhitch extern const char bootprog_rev[];
63e1d8eca6Smhitch 
64c54298e6Saymeric void startit(void *, void *, void *);
65e1d8eca6Smhitch 
66e1d8eca6Smhitch int consclose(void);
67e1d8eca6Smhitch 
68e1d8eca6Smhitch extern void *ConsoleBase;
69e1d8eca6Smhitch 
70e1d8eca6Smhitch int
pain(void * aio)71c54298e6Saymeric pain(void *aio)
72e1d8eca6Smhitch {
73e1d8eca6Smhitch 	long int io = 0;
7453524e44Schristos 	void *kp;
75e1d8eca6Smhitch 	int ksize;
76e1d8eca6Smhitch 	struct stat sb;
77e1d8eca6Smhitch 
78e1d8eca6Smhitch 	extern u_int16_t timelimit;
79e1d8eca6Smhitch 
80e1d8eca6Smhitch 	xdinit(aio);
81e1d8eca6Smhitch 
82e1d8eca6Smhitch 	if (consinit(NULL))		/* Initialize fresh console */
83e1d8eca6Smhitch 		return(1);
84e1d8eca6Smhitch 
85e1d8eca6Smhitch #ifdef PPCBOOTER
86e1d8eca6Smhitch 	printf("NetBSD/AmigaPPC " NETBSD_VERS " Primary Bootstrap %s\n", bootprog_rev);
87e1d8eca6Smhitch #else
88e1d8eca6Smhitch 	printf("NetBSD/Amiga " NETBSD_VERS " Primary Bootstrap %s\n", bootprog_rev);
89e1d8eca6Smhitch #endif
90e1d8eca6Smhitch 	io = open("/boot.amiga", 0);	/* Try /boot.amiga first */
91e1d8eca6Smhitch 	if (io < 0) {
92e1d8eca6Smhitch 		io = open("/boot", 0);	/* Fallback to /boot */
93e1d8eca6Smhitch 		if (io < 0) {
94e1d8eca6Smhitch 			io = open("/boot.ami", 0);	/* 8.3 name? */
95e1d8eca6Smhitch 			if (io < 0) {
96e1d8eca6Smhitch 				goto err;
97e1d8eca6Smhitch 			}
98e1d8eca6Smhitch 		}
99e1d8eca6Smhitch 	}
100e1d8eca6Smhitch 
101e1d8eca6Smhitch 	/* get size of file? */
102e1d8eca6Smhitch 	if (fstat(io, &sb))
103e1d8eca6Smhitch 		goto err;
104e1d8eca6Smhitch 	/* allocate memory for file */
105e1d8eca6Smhitch 	ksize = sb.st_size;
106e1d8eca6Smhitch 	if (ksize == 0) {
107e1d8eca6Smhitch 		printf("Bad size, using 32K\n");	/* XXX debug? */
108e1d8eca6Smhitch 		ksize = 32 * 1024;
109e1d8eca6Smhitch 	}
110e1d8eca6Smhitch 	kp = alloc(ksize);
111e1d8eca6Smhitch 	if (kp == 0) {
112e1d8eca6Smhitch 		errno = ENOMEM;
113e1d8eca6Smhitch 		goto err;
114e1d8eca6Smhitch 	}
115e1d8eca6Smhitch 	/* read file into memory */
116e1d8eca6Smhitch 	if (read(io, kp, ksize) != ksize) {
117e1d8eca6Smhitch 		errno = ENOEXEC;
118e1d8eca6Smhitch 		goto freeall;
119e1d8eca6Smhitch 	}
120e1d8eca6Smhitch 	/* validate boot: DOS\0 and checksum? */
121e1d8eca6Smhitch 	if (strcmp(kp, "DOS") != 0 &&
122e1d8eca6Smhitch 	    (*(u_int32_t *)kp) != 0x424f4f54) {
123e1d8eca6Smhitch 		errno = ENOEXEC;
124e1d8eca6Smhitch 		goto freeall;
125e1d8eca6Smhitch 	}
126e1d8eca6Smhitch 	/* call boot+12(aio, sysbase); */
127e1d8eca6Smhitch 	close(io);
128e1d8eca6Smhitch 	startit(kp, aio, ConsoleBase);
129e1d8eca6Smhitch 	errno = -1;
130e1d8eca6Smhitch 
131e1d8eca6Smhitch freeall:
132606bb2caSchristos 	dealloc(kp, ksize);
133e1d8eca6Smhitch err:
134e1d8eca6Smhitch 	printf("Error %ld\n", (long)errno);
135e1d8eca6Smhitch 	close(io);
136e1d8eca6Smhitch 
137e1d8eca6Smhitch 	timelimit = 10;
138e1d8eca6Smhitch 	(void)getchar();
139e1d8eca6Smhitch 	consclose();
140e1d8eca6Smhitch 	return 1;
141e1d8eca6Smhitch }
142*9084173aSrin 
143*9084173aSrin void
twiddle(void)144*9084173aSrin twiddle(void)
145*9084173aSrin {
146*9084173aSrin 
147*9084173aSrin 	/* Keep silence; boot.amiga should be small enough. */
148*9084173aSrin }
149