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