loadbsd.c revision 1.3 1 1.3 leo /* $NetBSD: loadbsd.c,v 1.3 1995/04/08 21:01:39 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.3 leo char version[] = "$VER: LoadBSD 1.0 (08/04/95)";
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.3 leo while ((ch = getopt(argc, argv, "abdhstvDS:")) != 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.1 leo case 'v':
118 1.3 leo fprintf(stderr,"%s\r\n", version);
119 1.1 leo break;
120 1.1 leo case 'h':
121 1.1 leo help();
122 1.1 leo default:
123 1.1 leo usage();
124 1.1 leo }
125 1.1 leo }
126 1.1 leo argc -= optind;
127 1.1 leo argv += optind;
128 1.1 leo if(argc == 1)
129 1.1 leo strcpy(kname, argv[0]);
130 1.1 leo
131 1.1 leo /*
132 1.2 leo * Get system info to pass to NetBSD
133 1.1 leo */
134 1.1 leo get_sys_info();
135 1.1 leo
136 1.1 leo /*
137 1.1 leo * Find the kernel to boot and read it's exec-header
138 1.1 leo */
139 1.1 leo if((fd = open(kname, O_RDONLY)) < 0)
140 1.1 leo error("Cannot open kernel '%s'", kname);
141 1.1 leo if(read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr))
142 1.1 leo error("Cannot read exec-header of '%s'", kname);
143 1.1 leo if((ehdr.a_magic & 0xffff) != NMAGIC) /* XXX */
144 1.1 leo error("Not an NMAGIC file '%s'", kname);
145 1.1 leo
146 1.1 leo /*
147 1.1 leo * Extract various sizes from the kernel executable
148 1.1 leo */
149 1.1 leo textsz = (ehdr.a_text + __LDPGSZ - 1) & ~(__LDPGSZ - 1);
150 1.1 leo kparam.esym_loc = 0;
151 1.1 leo kparam.ksize = textsz + ehdr.a_data + ehdr.a_bss;
152 1.1 leo kparam.entry = ehdr.a_entry;
153 1.1 leo
154 1.1 leo if(ehdr.a_syms) {
155 1.1 leo if(lseek(fd,ehdr.a_text+ehdr.a_data+ehdr.a_syms+sizeof(ehdr), 0) <= 0)
156 1.1 leo error("Cannot seek to string table in '%s'", kname);
157 1.1 leo if(read(fd, &stringsz, sizeof(long)) != sizeof(long))
158 1.1 leo error("Cannot read string-table size");
159 1.1 leo if(lseek(fd, sizeof(ehdr), 0) <= 0)
160 1.1 leo error("Cannot seek back to text start");
161 1.1 leo kparam.ksize += ehdr.a_syms + sizeof(long) + stringsz;
162 1.1 leo }
163 1.1 leo
164 1.1 leo if((kparam.kp = (u_char *)malloc(kparam.ksize)) == NULL)
165 1.1 leo error("Cannot malloc kernel image space");
166 1.1 leo
167 1.1 leo /*
168 1.1 leo * Read text & data, clear bss
169 1.1 leo */
170 1.1 leo if((read(fd, kparam.kp, ehdr.a_text) != ehdr.a_text)
171 1.1 leo || (read(fd, kparam.kp + textsz, ehdr.a_data) != ehdr.a_data))
172 1.1 leo error("Unable to read kernel image\n");
173 1.1 leo memset(kparam.kp + textsz + ehdr.a_data, 0, ehdr.a_bss);
174 1.1 leo
175 1.1 leo /*
176 1.1 leo * Read symbol and string table
177 1.1 leo */
178 1.1 leo if(ehdr.a_syms) {
179 1.1 leo long *p;
180 1.1 leo
181 1.1 leo p = (long *)(kparam.kp + textsz + ehdr.a_data + ehdr.a_bss);
182 1.1 leo *p++ = ehdr.a_syms;
183 1.1 leo if(read(fd, (char *)p, ehdr.a_syms) != ehdr.a_syms)
184 1.1 leo error("Cannot read symbol table\n");
185 1.1 leo p = (long *)((char *)p + ehdr.a_syms);
186 1.1 leo if(read(fd, (char *)p, stringsz) != stringsz)
187 1.1 leo error("Cannot read string table\n");
188 1.1 leo kparam.esym_loc = (long)((char *)p-(char *)kparam.kp +stringsz);
189 1.1 leo }
190 1.1 leo
191 1.1 leo if(!t_flag)
192 1.1 leo start_kernel();
193 1.1 leo /* NOT REACHED */
194 1.1 leo
195 1.3 leo fprintf(stderr, "Kernel '%s' was loaded OK\r\n", kname);
196 1.3 leo do_exit(0);
197 1.1 leo }
198 1.1 leo
199 1.1 leo /*
200 1.1 leo * Extract memory and cpu/fpu info from system.
201 1.1 leo */
202 1.1 leo void get_sys_info()
203 1.1 leo {
204 1.1 leo long stck;
205 1.1 leo long *jar;
206 1.1 leo
207 1.1 leo kparam.cputype = 0;
208 1.1 leo
209 1.1 leo stck = Super(0);
210 1.1 leo
211 1.3 leo if(kparam.stmem_size <= 0)
212 1.3 leo kparam.stmem_size = *ADDR_PHYSTOP;
213 1.3 leo
214 1.1 leo kparam.ttmem_start = TTRAM_BASE;
215 1.3 leo if(!s_flag)
216 1.3 leo kparam.ttmem_size = *ADDR_RAMTOP - TTRAM_BASE;
217 1.1 leo
218 1.1 leo /*
219 1.1 leo * Scan cookiejar for cpu/fpu types
220 1.1 leo */
221 1.1 leo jar = *ADDR_P_COOKIE;
222 1.1 leo if(jar != NULL) {
223 1.1 leo do {
224 1.1 leo if(jar[0] == 0x5f435055) { /* _CPU */
225 1.1 leo switch(jar[1]) {
226 1.1 leo case 0:
227 1.1 leo kparam.cputype |= ATARI_68000;
228 1.1 leo break;
229 1.1 leo case 10:
230 1.1 leo kparam.cputype |= ATARI_68010;
231 1.1 leo break;
232 1.1 leo case 20:
233 1.1 leo kparam.cputype |= ATARI_68020;
234 1.1 leo break;
235 1.1 leo case 30:
236 1.1 leo kparam.cputype |= ATARI_68030;
237 1.1 leo break;
238 1.1 leo case 40:
239 1.1 leo kparam.cputype |= ATARI_68040;
240 1.1 leo break;
241 1.1 leo default:
242 1.1 leo error("Unknown CPU-type");
243 1.1 leo }
244 1.1 leo }
245 1.1 leo if(jar[0] == 0x5f465055) { /* _FPU */
246 1.1 leo switch(jar[1]) {
247 1.1 leo case 0x10000:
248 1.1 leo case 0x20000:
249 1.1 leo kparam.cputype |= ATARI_68881;
250 1.1 leo break;
251 1.1 leo case 0x30000:
252 1.1 leo kparam.cputype |= ATARI_68882;
253 1.1 leo break;
254 1.1 leo case 0x40000:
255 1.1 leo kparam.cputype |= ATARI_FPU40;
256 1.1 leo break;
257 1.1 leo default:
258 1.1 leo error("Unknown FPU-type");
259 1.1 leo }
260 1.1 leo }
261 1.1 leo jar = &jar[2];
262 1.1 leo } while(jar[-2]);
263 1.1 leo }
264 1.1 leo if(!(kparam.cputype & ATARI_ANYCPU))
265 1.1 leo error("Cannot determine CPU-type");
266 1.1 leo if(!(kparam.cputype & ATARI_ANYFPU))
267 1.1 leo error("Cannot determine FPU-type");
268 1.1 leo
269 1.1 leo Super(stck);
270 1.3 leo
271 1.3 leo if(d_flag) {
272 1.3 leo fprintf(stderr, "Machine info:\r\n");
273 1.3 leo fprintf(stderr, "ST-RAM size\t: %10d bytes\r\n", kparam.stmem_size);
274 1.3 leo fprintf(stderr, "TT-RAM size\t: %10d bytes\r\n", kparam.ttmem_size);
275 1.3 leo fprintf(stderr, "TT-RAM start\t: 0x%08x\r\n", kparam.ttmem_start);
276 1.3 leo fprintf(stderr, "Cpu-type\t: 0x%08x\r\n", kparam.cputype);
277 1.3 leo }
278 1.1 leo }
279 1.1 leo
280 1.1 leo void error(char *fmt, ...)
281 1.1 leo {
282 1.1 leo va_list ap;
283 1.1 leo
284 1.1 leo va_start(ap, fmt);
285 1.1 leo
286 1.1 leo fprintf(stderr, "%s: ", Progname);
287 1.1 leo vfprintf(stderr, fmt, ap);
288 1.3 leo fprintf(stderr, "\r\n");
289 1.3 leo do_exit(1);
290 1.1 leo /*NOTREACHED*/
291 1.1 leo }
292 1.1 leo
293 1.1 leo void help()
294 1.1 leo {
295 1.3 leo fprintf(stderr, "\r
296 1.3 leo NetBSD loader for the Atari-TT\r
297 1.3 leo \r
298 1.3 leo Usage: %s [-abdhstvD] [-S <stram-size>] [kernel]\r
299 1.3 leo \r
300 1.3 leo Description of options:\r
301 1.3 leo \r
302 1.3 leo \t-a Boot up to multi-user mode.\r
303 1.3 leo \t-b Ask for root device to use.\r
304 1.3 leo \t-d Enter kernel debugger.\r
305 1.3 leo \t-h What your getting right now.\r
306 1.3 leo \t-s Use only ST-compatible RAM\r
307 1.3 leo \t-S Set amount of ST-compatible RAM\r
308 1.3 leo \t-t Test the loader. It will do everything except executing the\r
309 1.3 leo \t loaded kernel.\r
310 1.3 leo \t-D printout debugging information while loading\r
311 1.3 leo \t-v Print loader version.\r
312 1.1 leo ", Progname);
313 1.3 leo do_exit(0);
314 1.1 leo }
315 1.1 leo
316 1.1 leo void usage()
317 1.1 leo {
318 1.3 leo fprintf(stderr, "Usage: %s [-abdhtv] [kernel]\r\n", Progname);
319 1.3 leo do_exit(1);
320 1.3 leo }
321 1.3 leo
322 1.3 leo void do_exit(code)
323 1.3 leo int code;
324 1.3 leo {
325 1.3 leo fprintf(stderr, "\r\nHit any key to continue...");
326 1.3 leo (void)getchar();
327 1.3 leo fprintf(stderr, "\r\n");
328 1.3 leo exit(code);
329 1.1 leo }
330 1.1 leo
331 1.1 leo void start_kernel()
332 1.1 leo {
333 1.1 leo long stck;
334 1.1 leo
335 1.1 leo stck = Super(0);
336 1.1 leo startit();
337 1.1 leo /* NOT REACHED */
338 1.1 leo
339 1.1 leo Super(stck);
340 1.1 leo }
341 1.1 leo
342 1.1 leo asm("
343 1.1 leo .text
344 1.1 leo .globl _startit
345 1.1 leo
346 1.1 leo _startit:
347 1.1 leo move.w #0x2700,sr
348 1.1 leo
349 1.1 leo | the BSD kernel wants values into the following registers:
350 1.1 leo | d0: ttmem-size
351 1.1 leo | d1: stmem-size
352 1.1 leo | d2: cputype
353 1.1 leo | d3: boothowto
354 1.1 leo | d4: length of loaded kernel
355 1.1 leo | a0: start of loaded kernel
356 1.1 leo | a1: end of symbols (esym)
357 1.1 leo | All other registers zeroed for possible future requirements.
358 1.1 leo
359 1.1 leo lea _kparam, a3 | a3 points to parameter block
360 1.1 leo lea _startit,sp | make sure we have a good stack ***
361 1.1 leo move.l (a3),a0 | loaded kernel
362 1.1 leo move.l 8(a3),-(sp) | push entry point ***
363 1.1 leo move.l a0,d0 | offset of loaded kernel
364 1.1 leo add.l d0,(sp) | add offset
365 1.1 leo move.l 12(a3),d1 | stmem-size
366 1.1 leo move.l 16(a3),d0 | ttmem-size
367 1.1 leo move.l 20(a3),d2 | cputype
368 1.1 leo move.l 24(a3),d3 | boothowto
369 1.1 leo move.l 4(a3),d4 | length of loaded kernel
370 1.1 leo move.l 28(a3),d5 | start of fastram
371 1.1 leo move.l 32(a3),a1 | end of symbols
372 1.1 leo sub.l a5,a5 | target, load to 0
373 1.1 leo btst #4, d2 | Is this an 68040?
374 1.1 leo beq not040
375 1.1 leo
376 1.1 leo | Turn off 68040 MMU
377 1.1 leo .word 0x4e7b,0xd003 | movec a5,tc
378 1.1 leo .word 0x4e7b,0xd806 | movec a5,urp
379 1.1 leo .word 0x4e7b,0xd807 | movec a5,srp
380 1.1 leo .word 0x4e7b,0xd004 | movec a5,itt0
381 1.1 leo .word 0x4e7b,0xd005 | movec a5,itt1
382 1.1 leo .word 0x4e7b,0xd006 | movec a5,dtt0
383 1.1 leo .word 0x4e7b,0xd007 | movec a5,dtt1
384 1.1 leo bra nott
385 1.1 leo
386 1.1 leo not040:
387 1.1 leo lea zero,a3
388 1.1 leo pmove (a3),tcr | Turn off MMU
389 1.1 leo lea nullrp,a3
390 1.1 leo pmove (a3),crp | Turn off MMU some more
391 1.1 leo pmove (a3),srp | Really, really, turn off MMU
392 1.1 leo
393 1.1 leo | Turn off 68030 TT registers
394 1.1 leo btst #3, d2 | Is this an 68030?
395 1.1 leo beq.b nott
396 1.1 leo lea zero,a3
397 1.1 leo pmove (a3),tt0
398 1.1 leo pmove (a3),tt1
399 1.1 leo
400 1.1 leo nott:
401 1.1 leo moveq.l #0,d6 | would have known contents)
402 1.1 leo moveq.l #0,d7
403 1.1 leo movea.l d6,a2
404 1.1 leo movea.l d6,a3
405 1.1 leo movea.l d6,a4
406 1.1 leo movea.l d6,a5
407 1.1 leo movea.l d6,a6
408 1.1 leo rts | enter kernel at address on stack ***
409 1.1 leo
410 1.1 leo
411 1.1 leo | A do-nothing MMU root pointer (includes the following long as well)
412 1.1 leo
413 1.1 leo nullrp: .long 0x80000202
414 1.1 leo zero: .long 0
415 1.1 leo svsp: .long 0
416 1.1 leo
417 1.1 leo
418 1.1 leo ");
419