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