1 /*
2  * $NetBSD: main.c,v 1.2 2002/01/26 13:21:10 aymeric Exp $
3  *
4  *
5  * Copyright (c) 1996,1999 Ignatios Souvatzis
6  * Copyright (c) 1994 Michael L. Hitch
7  * All rights reserved.
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 Michael L. Hitch.
20  * 4. The name of the authors may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  */
35 
36 #include <sys/cdefs.h>
37 #include <sys/reboot.h>
38 #include <sys/types.h>
39 
40 #include <sys/exec_aout.h>
41 
42 #include <amiga/cfdev.h>
43 #include <amiga/memlist.h>
44 #include <include/cpu.h>
45 
46 #include <saerrno.h>
47 #include <stand.h>
48 
49 #include "libstubs.h"
50 #include "samachdep.h"
51 
52 #undef __LDPGSZ
53 #define __LDPGSZ 8192
54 #define __PGSZ 8192
55 
56 #define DRACOREVISION	(*(u_int8_t *)0x02000009)
57 #define DRACOMMUMARGIN	0x200000
58 #define DRACOZ2OFFSET	0x3000000
59 #define DRACOZ2MAX	0x1000000
60 
61 #define EXECMIN 36
62 
63 /*
64  * vers.c (generated by newvers.sh)
65  */
66 extern const char bootprog_rev[];
67 
68 void startit(void *, void *, void *);
69 
70 int consclose(void);
71 
72 extern void *ConsoleBase;
73 
74 int
75 pain(void *aio)
76 {
77 	long int io = 0;
78 	caddr_t kp;
79 	int ksize;
80 	struct stat sb;
81 
82 	extern u_int16_t timelimit;
83 
84 	xdinit(aio);
85 
86 	if (consinit(NULL))		/* Initialize fresh console */
87 		return(1);
88 
89 #ifdef PPCBOOTER
90 	printf("NetBSD/AmigaPPC " NETBSD_VERS " Primary Bootstrap %s\n", bootprog_rev);
91 #else
92 	printf("NetBSD/Amiga " NETBSD_VERS " Primary Bootstrap %s\n", bootprog_rev);
93 #endif
94 	io = open("/boot.amiga", 0);	/* Try /boot.amiga first */
95 	if (io < 0) {
96 		io = open("/boot", 0);	/* Fallback to /boot */
97 		if (io < 0) {
98 			io = open("/boot.ami", 0);	/* 8.3 name? */
99 			if (io < 0) {
100 				goto err;
101 			}
102 		}
103 	}
104 
105 	/* get size of file? */
106 	if (fstat(io, &sb))
107 		goto err;
108 	/* allocate memory for file */
109 	ksize = sb.st_size;
110 	if (ksize == 0) {
111 		printf("Bad size, using 32K\n");	/* XXX debug? */
112 		ksize = 32 * 1024;
113 	}
114 	kp = alloc(ksize);
115 	if (kp == 0) {
116 		errno = ENOMEM;
117 		goto err;
118 	}
119 	/* read file into memory */
120 	if (read(io, kp, ksize) != ksize) {
121 		errno = ENOEXEC;
122 		goto freeall;
123 	}
124 	/* validate boot: DOS\0 and checksum? */
125 	if (strcmp(kp, "DOS") != 0 &&
126 	    (*(u_int32_t *)kp) != 0x424f4f54) {
127 		errno = ENOEXEC;
128 		goto freeall;
129 	}
130 	/* call boot+12(aio, sysbase); */
131 	close(io);
132 	startit(kp, aio, ConsoleBase);
133 	errno = -1;
134 
135 freeall:
136 	free(kp, ksize);
137 err:
138 	printf("Error %ld\n", (long)errno);
139 	close(io);
140 
141 	timelimit = 10;
142 	(void)getchar();
143 	consclose();
144 	return 1;
145 }
146