xref: /netbsd-src/sys/arch/atari/stand/bootxx/bootxx.c (revision fdecd6a253f999ae92b139670d9e15cc9df4497c)
1 /*	$NetBSD: bootxx.c,v 1.2 1997/06/28 21:36:28 leo Exp $	*/
2 
3 /*
4  * Copyright (c) 1995 Waldi Ravens.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *        This product includes software developed by Waldi Ravens.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 asm("	.text
34 	.even
35 start:
36 	bras	_bootxx
37 ");
38 #define	boot_BSD	bsd_startup
39 
40 #include <stand.h>
41 #include <atari_stand.h>
42 #include <string.h>
43 #include <libkern.h>
44 #include <kparamb.h>
45 #include <sys/exec.h>
46 #include <sys/reboot.h>
47 #include <machine/cpu.h>
48 
49 #include "bootxx.h"
50 
51 void	boot_BSD __P((kparb *)__attribute__((noreturn)));
52 int	load_BSD __P((osdsc *));
53 int	sys_info __P((osdsc *));
54 int	usr_info __P((osdsc *));
55 
56 int
57 bootxx(readsector, disklabel, autoboot)
58 	void	*readsector,
59 		*disklabel;
60 	int	autoboot;
61 {
62 	static osdsc	os_desc;
63 	extern char	end[], edata[];
64 	osdsc		*od = &os_desc;
65 
66 	bzero(edata, end - edata);
67 
68 	printf("\033v\nNetBSD/Atari boot loader ($Revision: 1.2 $)\n\n");
69 
70 	if (init_dskio(readsector, disklabel, -1))
71 		return(-1);
72 
73 	if (sys_info(od))
74 		return(-2);
75 
76 	for (;;) {
77 		od->rootfs = 0;			/* partition a */
78 		od->osname = "/netbsd";
79 		od->ostype = &od->osname[1];
80 		od->boothowto = (RB_RDONLY);
81 
82 		if (!autoboot) {
83 			int	pref;
84 
85 			od->boothowto = (RB_RDONLY|RB_SINGLE);
86 			pref = usr_info(od);
87 			if (pref < 0)
88 				continue;
89 			if (pref > 0)
90 				return(pref);
91 		}
92 		autoboot = 0;			/* in case auto boot fails */
93 
94 		if (init_dskio(readsector, disklabel, od->rootfs))
95 			continue;
96 
97 		if (load_BSD(od))
98 			continue;
99 
100 		boot_BSD(&od->kp);
101 	}
102 	/* NOTREACHED */
103 }
104 
105 int
106 sys_info(od)
107 	osdsc	*od;
108 {
109 	long	*jar;
110 	int	tos;
111 
112 	od->stmem_size = *ADDR_PHYSTOP;
113 
114 	if (*ADDR_CHKRAMTOP == RAMTOP_MAGIC) {
115 		od->ttmem_size  = *ADDR_RAMTOP;
116 		if (od->ttmem_size > TTRAM_BASE) {
117 			od->ttmem_size  -= TTRAM_BASE;
118 			od->ttmem_start  = TTRAM_BASE;
119 		}
120 	}
121 
122 	tos = (*ADDR_SYSBASE)->os_version;
123 	if ((tos > 0x300) && (tos < 0x306))
124 		od->cputype = ATARI_CLKBROKEN;
125 
126 	if ((jar = *ADDR_P_COOKIE)) {
127 		while (jar[0]) {
128 			if (jar[0] == 0x5f435055L) { /* _CPU	*/
129 				switch(jar[1]) {
130 					case 0:
131 						od->cputype |= ATARI_68000;
132 						break;
133 					case 10:
134 						od->cputype |= ATARI_68010;
135 						break;
136 					case 20:
137 						od->cputype |= ATARI_68020;
138 						break;
139 					case 30:
140 						od->cputype |= ATARI_68030;
141 						break;
142 					case 40:
143 						od->cputype |= ATARI_68040;
144 						break;
145 				}
146 			}
147 			jar += 2;
148 		}
149 	}
150 	if (!(od->cputype & ATARI_ANYCPU)) {
151 		printf("Unknown CPU-type.\n");
152 		return(-1);
153 	}
154 
155 	return(0);
156 }
157 
158 int
159 usr_info(od)
160 	osdsc	*od;
161 {
162 	static char	line[800];
163 	char		c, *p = line;
164 
165 	printf("\nEnter os-type [.%s] root-fs [:a] kernel [%s]"
166 	       " options [none]:\n\033e", od->ostype, od->osname);
167 	gets(p);
168 	printf("\033f");
169 
170 	for (;;) {
171 		while (isspace(*p))
172 			*p++ = '\0';
173 
174 		switch (*p++) {
175 		  case '\0':
176 			goto done;
177 		  case ':':
178 			if ((c = *p) >= 'a' && c <= 'z')
179 				od->rootfs = c - 'a';
180 			else if (c >= 'A' && c <= 'Z')
181 				od->rootfs = c - ('A' - 27);
182 			else return(-1);
183 
184 			if (!od->rootfs)
185 				break;
186 			*p = 'b';
187 			/* FALLTHROUGH */
188 		  case '-':
189 			if ((c = *p) == 'a')
190 				od->boothowto &= ~RB_SINGLE;
191 			else if (c == 'b')
192 				od->boothowto |= RB_ASKNAME;
193 			else if (c == 'd')
194 				od->boothowto |= RB_KDB;
195 			else return(-1);
196 			break;
197 		  case '.':
198 			od->ostype = p;
199 			break;
200 		  case '/':
201 			od->osname = --p;
202 			break;
203 		  default:
204 			return(-1);
205 		}
206 
207 		while ((c = *p) && !isspace(c))
208 			p += 1;
209 	}
210 
211 done:
212 	c = od->ostype[0];
213 	if (isupper(c))
214 		c = tolower(c);
215 
216 	switch (c) {
217 	  case 'n':		/* NetBSD */
218 		return(0);
219 	  case 'l':		/* Linux  */
220 		return(0x10);
221 	  case 'a':		/* ASV    */
222 		return(0x40);
223 	  case 't':		/* TOS    */
224 		return(0x80);
225 	  default:
226 		return(-1);
227 	}
228 }
229 
230 int
231 load_BSD(od)
232 	osdsc		*od;
233 {
234 	struct exec	hdr;
235 	int		err, fd;
236 	u_int		textsz, strsz;
237 
238 	/*
239 	 * Read kernel's exec-header.
240 	 */
241 	err = 1;
242 	if ((fd = open(od->osname, 0)) < 0)
243 		goto error;
244 	err = 2;
245 	if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr))
246 		goto error;
247 	err = 3;
248 	if (N_GETMAGIC(hdr) != NMAGIC)
249 		goto error;
250 
251 	/*
252 	 * Extract sizes from kernel executable.
253 	 */
254 	textsz     = (hdr.a_text + __LDPGSZ - 1) & ~(__LDPGSZ - 1);
255 	od->ksize  = textsz + hdr.a_data + hdr.a_bss;
256 	od->kentry = hdr.a_entry;
257 	od->kstart = NULL;
258 	od->k_esym = 0;
259 
260 	if (hdr.a_syms) {
261 	    u_int x = sizeof(hdr) + hdr.a_text + hdr.a_data + hdr.a_syms;
262 	    err = 8;
263 	    if (lseek(fd, (off_t)x, SEEK_SET) != x)
264 		goto error;
265 	    err = 9;
266 	    if (read(fd, &strsz, sizeof(strsz)) != sizeof(strsz))
267 		goto error;
268 	    err = 10;
269 	    if (lseek(fd, (off_t)sizeof(hdr), SEEK_SET) != sizeof(hdr))
270 		goto error;
271 	    od->ksize += sizeof(strsz) + hdr.a_syms + strsz;
272 	}
273 
274 	/*
275 	 * Read text & data, clear bss
276 	 */
277 	err = 16;
278 	if ((od->kstart = alloc(od->ksize)) == NULL)
279 		goto error;
280 	err = 17;
281 	if (read(fd, od->kstart, hdr.a_text) != hdr.a_text)
282 		goto error;
283 	err = 18;
284 	if (read(fd, od->kstart + textsz, hdr.a_data) != hdr.a_data)
285 		goto error;
286 	bzero(od->kstart + textsz + hdr.a_data, hdr.a_bss);
287 
288 	/*
289 	 * Read symbol and string table
290 	 */
291 	if (hdr.a_syms) {
292 		char *p = od->kstart + textsz + hdr.a_data + hdr.a_bss;
293 		*((u_int32_t *)p)++ = hdr.a_syms;
294 		err = 24;
295 		if (read(fd, p, hdr.a_syms) != hdr.a_syms)
296 			goto error;
297 		p += hdr.a_syms;
298 		err = 25;
299 		if (read(fd, p, strsz) != strsz)
300 			goto error;
301 		od->k_esym = (p - (char *)od->kstart) + strsz;
302 	}
303 
304 	return(0);
305 
306 error:
307 	if (fd >= 0) {
308 		if (od->kstart)
309 			free(od->kstart, od->ksize);
310 		close(fd);
311 	}
312 	printf("Error %d in load_BSD.\n", err);
313 	return(-1);
314 }
315