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