xref: /netbsd-src/sys/arch/atari/stand/tostools/loadbsd/loadbsd.c (revision 176d4dd6e59f742148dff65b11413fc95453cf99)
1 /*	$NetBSD: loadbsd.c,v 1.5 1995/05/02 05:54:28 leo Exp $	*/
2 
3 /*
4  * Copyright (c) 1995 L. Weppelman
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 Leo Weppelman.
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 /*
34  * NetBSD loader for the Atari-TT.
35  */
36 
37 #include <stdio.h>
38 #include <a_out.h>
39 #include <fcntl.h>
40 #include <osbind.h>
41 #include <stdarg.h>
42 #include "loader.h"
43 
44 char	*Progname;		/* How are we called		*/
45 int	t_flag = 0;		/* Just test, do not execute	*/
46 int	d_flag = 0;		/* Output debugging output?	*/
47 int	s_flag = 0;		/* St-ram only			*/
48 
49 char version[] = "$Revision: 1.5 $";
50 
51 /*
52  * Default name of kernel to boot, large enough to patch
53  */
54 char		kname[80] = "n:/netbsd";
55 
56 static struct {
57 	u_char	*kp;		/* 00: Kernel load address		*/
58 	long	ksize;		/* 04: Size of loaded kernel		*/
59 	u_long	entry;		/* 08: Kernel entry point		*/
60 	long	stmem_size;	/* 12: Size of st-ram			*/
61 	long	ttmem_size;	/* 16: Size of tt-ram			*/
62 	long	cputype;	/* 20: Type of cpu			*/
63 	long	boothowto;	/* 24: How to boot			*/
64 	long	ttmem_start;	/* 28: Start of tt-ram			*/
65 	long	esym_loc;	/* 32: End of symbol table		*/
66 } kparam;
67 
68 void get_sys_info(void);
69 void error(char *fmt, ...);
70 void help(void);
71 void usage(void);
72 void start_kernel(void);
73 void do_exit(int);
74 
75 int main(argc, argv)
76 int	argc;
77 char	**argv;
78 {
79 	/*
80 	 * Option parsing
81 	 */
82 	extern	int	optind;
83 	extern	char	*optarg;
84 	int		ch;
85 	int		fd;
86 	long		textsz, stringsz;
87 	struct exec	ehdr;
88 
89 	Progname = argv[0];
90 
91 	kparam.boothowto = RB_SINGLE;
92 
93 	while ((ch = getopt(argc, argv, "abdhstvDS:T:")) != EOF) {
94 		switch(ch) {
95 		case 'a':
96 			kparam.boothowto &= ~(RB_SINGLE);
97 			kparam.boothowto |= RB_AUTOBOOT;
98 			break;
99 		case 'b':
100 			kparam.boothowto |= RB_ASKNAME;
101 			break;
102 		case 'd':
103 			kparam.boothowto |= RB_KDB;
104 			break;
105 		case 'D':
106 			d_flag = 1;
107 			break;
108 		case 's':
109 			s_flag = 1;
110 			break;
111 		case 'S':
112 			kparam.stmem_size = atoi(optarg);
113 			break;
114 		case 't':
115 			t_flag = 1;
116 			break;
117 		case 'T':
118 			kparam.ttmem_size = atoi(optarg);
119 			break;
120 		case 'v':
121 			fprintf(stderr,"%s\r\n", version);
122 			break;
123 		case 'h':
124 			help();
125 		default:
126 			usage();
127 		}
128 	}
129 	argc -= optind;
130 	argv += optind;
131 	if(argc == 1)
132 		strcpy(kname, argv[0]);
133 
134 	/*
135 	 * Get system info to pass to NetBSD
136 	 */
137 	get_sys_info();
138 
139 	/*
140 	 * Find the kernel to boot and read it's exec-header
141 	 */
142 	if((fd = open(kname, O_RDONLY)) < 0)
143 		error("Cannot open kernel '%s'", kname);
144 	if(read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr))
145 		error("Cannot read exec-header of '%s'", kname);
146 	if((ehdr.a_magic & 0xffff) != NMAGIC) /* XXX */
147 		error("Not an NMAGIC file '%s'", kname);
148 
149 	/*
150 	 * Extract various sizes from the kernel executable
151 	 */
152 	textsz          = (ehdr.a_text + __LDPGSZ - 1) & ~(__LDPGSZ - 1);
153 	kparam.esym_loc = 0;
154 	kparam.ksize    = textsz + ehdr.a_data + ehdr.a_bss;
155 	kparam.entry    = ehdr.a_entry;
156 
157 	if(ehdr.a_syms) {
158 	  if(lseek(fd,ehdr.a_text+ehdr.a_data+ehdr.a_syms+sizeof(ehdr), 0) <= 0)
159 		error("Cannot seek to string table in '%s'", kname);
160 	  if(read(fd, &stringsz, sizeof(long)) != sizeof(long))
161 		error("Cannot read string-table size");
162 	  if(lseek(fd, sizeof(ehdr), 0) <= 0)
163 		error("Cannot seek back to text start");
164 	  kparam.ksize += ehdr.a_syms + sizeof(long) + stringsz;
165 	}
166 
167 	if((kparam.kp = (u_char *)malloc(kparam.ksize)) == NULL)
168 		error("Cannot malloc kernel image space");
169 
170 	/*
171 	 * Read text & data, clear bss
172 	 */
173 	if((read(fd, kparam.kp, ehdr.a_text) != ehdr.a_text)
174 	    || (read(fd, kparam.kp + textsz, ehdr.a_data) != ehdr.a_data))
175 		error("Unable to read kernel image\n");
176 	memset(kparam.kp + textsz + ehdr.a_data, 0, ehdr.a_bss);
177 
178 	/*
179 	 * Read symbol and string table
180 	 */
181 	if(ehdr.a_syms) {
182 		long	*p;
183 
184 		p = (long *)(kparam.kp + textsz + ehdr.a_data + ehdr.a_bss);
185 		*p++ = ehdr.a_syms;
186 		if(read(fd, (char *)p, ehdr.a_syms) != ehdr.a_syms)
187 			error("Cannot read symbol table\n");
188 		p = (long *)((char *)p + ehdr.a_syms);
189 		if(read(fd, (char *)p, stringsz) != stringsz)
190 			error("Cannot read string table\n");
191 		kparam.esym_loc = (long)((char *)p-(char *)kparam.kp +stringsz);
192 	}
193 
194 	if(d_flag) {
195 	    fprintf(stderr, "\r\nKernel info:\r\n");
196 	    fprintf(stderr, "Kernel loadaddr\t: 0x%08x\r\n", kparam.kp);
197 	    fprintf(stderr, "Kernel size\t: %10d bytes\r\n", kparam.ksize);
198 	    fprintf(stderr, "Kernel entry\t: 0x%08x\r\n", kparam.entry);
199 	    fprintf(stderr, "Kernel esym\t: 0x%08x\r\n", kparam.esym_loc);
200 	}
201 
202 	if(!t_flag)
203 		start_kernel();
204 		/* NOT REACHED */
205 
206 	fprintf(stderr, "Kernel '%s' was loaded OK\r\n", kname);
207 	do_exit(0);
208 }
209 
210 /*
211  * Extract memory and cpu/fpu info from system.
212  */
213 void get_sys_info()
214 {
215 	long	stck;
216 	long	*jar;
217 
218 	kparam.cputype = 0;
219 
220 	stck = Super(0);
221 
222 	if(kparam.stmem_size <= 0)
223 		kparam.stmem_size  = *ADDR_PHYSTOP;
224 
225 	if(kparam.ttmem_size)
226 		kparam.ttmem_start  = TTRAM_BASE;
227 	else {
228 		if(!s_flag && (*ADDR_CHKRAMTOP == RAM_TOP_MAGIC)) {
229 			kparam.ttmem_size  = *ADDR_RAMTOP;
230 			if(kparam.ttmem_size > TTRAM_BASE) {
231 				kparam.ttmem_size  -= TTRAM_BASE;
232 				kparam.ttmem_start  = TTRAM_BASE;
233 			}
234 			else kparam.ttmem_size = 0;
235 		}
236 	}
237 
238 	/*
239 	 * Scan cookiejar for cpu/fpu types
240 	 */
241 	jar = *ADDR_P_COOKIE;
242 	if(jar != NULL) {
243 		do {
244 			if(jar[0] == 0x5f435055) { /* _CPU	*/
245 				switch(jar[1]) {
246 					case 0:
247 						kparam.cputype |= ATARI_68000;
248 						break;
249 					case 10:
250 						kparam.cputype |= ATARI_68010;
251 						break;
252 					case 20:
253 						kparam.cputype |= ATARI_68020;
254 						break;
255 					case 30:
256 						kparam.cputype |= ATARI_68030;
257 						break;
258 					case 40:
259 						kparam.cputype |= ATARI_68040;
260 						break;
261 					default:
262 						error("Unknown CPU-type");
263 				}
264 			}
265 			if(jar[0] == 0x5f465055) { /* _FPU	*/
266 				switch(jar[1]) {
267 					case 0x10000:
268 					case 0x20000:
269 						kparam.cputype |= ATARI_68881;
270 						break;
271 					case 0x30000:
272 						kparam.cputype |= ATARI_68882;
273 						break;
274 					case 0x40000:
275 						kparam.cputype |= ATARI_FPU40;
276 						break;
277 					default:
278 						error("Unknown FPU-type");
279 				}
280 			}
281 			jar = &jar[2];
282 		} while(jar[-2]);
283 	}
284 	if(!(kparam.cputype & ATARI_ANYCPU))
285 		error("Cannot determine CPU-type");
286 	if(!(kparam.cputype & ATARI_ANYFPU))
287 		error("Cannot determine FPU-type");
288 
289 	Super(stck);
290 
291 	if(d_flag) {
292 	    fprintf(stderr, "Machine info:\r\n");
293 	    fprintf(stderr, "ST-RAM size\t: %10d bytes\r\n", kparam.stmem_size);
294 	    fprintf(stderr, "TT-RAM size\t: %10d bytes\r\n", kparam.ttmem_size);
295 	    fprintf(stderr, "TT-RAM start\t: 0x%08x\r\n", kparam.ttmem_start);
296 	    fprintf(stderr, "Cpu-type\t: 0x%08x\r\n", kparam.cputype);
297 	}
298 }
299 
300 void error(char *fmt, ...)
301 {
302 	va_list	ap;
303 
304 	va_start(ap, fmt);
305 
306 	fprintf(stderr, "%s: ", Progname);
307 	vfprintf(stderr, fmt, ap);
308 	fprintf(stderr, "\r\n");
309 	do_exit(1);
310 	/*NOTREACHED*/
311 }
312 
313 void help()
314 {
315 	fprintf(stderr, "\r
316 NetBSD loader for the Atari-TT\r
317 \r
318 Usage: %s [-abdhstvD] [-S <stram-size>] [kernel]\r
319 \r
320 Description of options:\r
321 \r
322 \t-a  Boot up to multi-user mode.\r
323 \t-b  Ask for root device to use.\r
324 \t-d  Enter kernel debugger.\r
325 \t-h  What your getting right now.\r
326 \t-s  Use only ST-compatible RAM\r
327 \t-S  Set amount of ST-compatible RAM\r
328 \t-T  Set amount of TT-compatible RAM\r
329 \t-t  Test the loader. It will do everything except executing the\r
330 \t    loaded kernel.\r
331 \t-D  printout debugging information while loading\r
332 \t-v  Print loader version.\r
333 ", Progname);
334 	do_exit(0);
335 }
336 
337 void usage()
338 {
339 	fprintf(stderr, "Usage: %s [-abdhtv] [kernel]\r\n", Progname);
340 	do_exit(1);
341 }
342 
343 void do_exit(code)
344 int	code;
345 {
346 	fprintf(stderr, "\r\nHit <return> to continue...");
347 	(void)getchar();
348 	fprintf(stderr, "\r\n");
349 	exit(code);
350 }
351 
352 void start_kernel()
353 {
354 	long	stck;
355 
356 	stck = Super(0);
357 	startit();
358 	/* NOT REACHED */
359 
360 	Super(stck);
361 }
362 
363 asm("
364 	.text
365 	.globl	_startit
366 
367 _startit:
368 	move.w	#0x2700,sr
369 
370 	| the BSD kernel wants values into the following registers:
371 	| d0:  ttmem-size
372 	| d1:  stmem-size
373 	| d2:  cputype
374 	| d3:  boothowto
375 	| d4:  length of loaded kernel
376 	| d5:  start of fastram
377 	| a0:  start of loaded kernel
378 	| a1:  end of symbols (esym)
379 	| All other registers zeroed for possible future requirements.
380 
381 	lea	_kparam, a3		| a3 points to parameter block
382 	lea	_startit,sp		| make sure we have a good stack ***
383 	move.l	(a3),a0			| loaded kernel
384 	move.l	8(a3),-(sp)		| push entry point		***
385 	move.l	a0,d0			| offset of loaded kernel
386 	add.l	d0,(sp)			| add offset
387 	move.l	12(a3),d1		| stmem-size
388 	move.l	16(a3),d0		| ttmem-size
389 	move.l	20(a3),d2		| cputype
390 	move.l	24(a3),d3		| boothowto
391 	move.l	4(a3),d4		| length of loaded kernel
392 	move.l	28(a3),d5		| start of fastram
393 	move.l	32(a3),a1		| end of symbols
394 	sub.l	a5,a5			| target, load to 0
395 	btst	#4, d2			| Is this an 68040?
396 	beq	not040
397 
398 	| Turn off 68040 MMU
399 	.word 0x4e7b,0xd003		| movec a5,tc
400 	.word 0x4e7b,0xd806		| movec a5,urp
401 	.word 0x4e7b,0xd807		| movec a5,srp
402 	.word 0x4e7b,0xd004		| movec a5,itt0
403 	.word 0x4e7b,0xd005		| movec a5,itt1
404 	.word 0x4e7b,0xd006		| movec a5,dtt0
405 	.word 0x4e7b,0xd007		| movec a5,dtt1
406 	bra	nott
407 
408 not040:
409 	lea	zero,a3
410 	pmove	(a3),tcr		| Turn off MMU
411 	lea	nullrp,a3
412 	pmove	(a3),crp		| Turn off MMU some more
413 	pmove	(a3),srp		| Really, really, turn off MMU
414 
415 	| Turn off 68030 TT registers
416 	btst	#3, d2			| Is this an 68030?
417 	beq.b	nott
418 	lea	zero,a3
419 	pmove	(a3),tt0
420 	pmove	(a3),tt1
421 
422 nott:
423 	moveq.l	#0,d6			|  would have known contents)
424 	moveq.l	#0,d7
425 	movea.l	d6,a2
426 	movea.l	d6,a3
427 	movea.l	d6,a4
428 	movea.l	d6,a5
429 	movea.l	d6,a6
430 	rts				| enter kernel at address on stack ***
431 
432 
433 | A do-nothing MMU root pointer (includes the following long as well)
434 
435 nullrp:	.long	0x80000202
436 zero:	.long	0
437 svsp:	.long   0
438 
439 ");
440