1 /* $NetBSD: loadbsd.c,v 1.9 1995/09/23 20:31:21 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.9 $"; 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(stdout,"%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(stdout, "\r\nKernel info:\r\n"); 196 fprintf(stdout, "Kernel loadaddr\t: 0x%08x\r\n", kparam.kp); 197 fprintf(stdout, "Kernel size\t: %10d bytes\r\n", kparam.ksize); 198 fprintf(stdout, "Kernel entry\t: 0x%08x\r\n", kparam.entry); 199 fprintf(stdout, "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(stdout, "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 OSH *oshdr; 218 219 kparam.cputype = 0; 220 221 stck = Super(0); 222 223 /* 224 * Some GEMDOS versions use a different year-base in the RTC. 225 */ 226 oshdr = *ADDR_OSHEAD; 227 oshdr = oshdr->os_beg; 228 if((oshdr->os_version >= 0x0300) && (oshdr->os_version < 0x0306)) 229 kparam.cputype |= ATARI_CLKBROKEN; 230 231 if(kparam.stmem_size <= 0) 232 kparam.stmem_size = *ADDR_PHYSTOP; 233 234 if(kparam.ttmem_size) 235 kparam.ttmem_start = TTRAM_BASE; 236 else { 237 if(!s_flag && (*ADDR_CHKRAMTOP == RAM_TOP_MAGIC)) { 238 kparam.ttmem_size = *ADDR_RAMTOP; 239 if(kparam.ttmem_size > TTRAM_BASE) { 240 kparam.ttmem_size -= TTRAM_BASE; 241 kparam.ttmem_start = TTRAM_BASE; 242 } 243 else kparam.ttmem_size = 0; 244 } 245 } 246 247 /* 248 * Scan cookiejar for cpu types 249 */ 250 jar = *ADDR_P_COOKIE; 251 if(jar != NULL) { 252 do { 253 if(jar[0] == 0x5f435055) { /* _CPU */ 254 switch(jar[1]) { 255 case 0: 256 kparam.cputype |= ATARI_68000; 257 break; 258 case 10: 259 kparam.cputype |= ATARI_68010; 260 break; 261 case 20: 262 kparam.cputype |= ATARI_68020; 263 break; 264 case 30: 265 kparam.cputype |= ATARI_68030; 266 break; 267 case 40: 268 kparam.cputype |= ATARI_68040; 269 break; 270 default: 271 error("Unknown CPU-type"); 272 } 273 } 274 if(jar[0] == 0x42504658) { /* BPFX */ 275 unsigned long *p; 276 277 p = (unsigned long*)jar[1]; 278 279 kparam.ttmem_start = p[1]; 280 kparam.ttmem_size = p[2]; 281 } 282 jar = &jar[2]; 283 } while(jar[-2]); 284 } 285 if(!(kparam.cputype & ATARI_ANYCPU)) 286 error("Cannot determine CPU-type"); 287 288 Super(stck); 289 290 if(d_flag) { 291 fprintf(stdout, "Machine info:\r\n"); 292 fprintf(stdout, "ST-RAM size\t: %10d bytes\r\n", kparam.stmem_size); 293 fprintf(stdout, "TT-RAM size\t: %10d bytes\r\n", kparam.ttmem_size); 294 fprintf(stdout, "TT-RAM start\t: 0x%08x\r\n", kparam.ttmem_start); 295 fprintf(stdout, "Cpu-type\t: 0x%08x\r\n", kparam.cputype); 296 } 297 } 298 299 void error(char *fmt, ...) 300 { 301 va_list ap; 302 303 va_start(ap, fmt); 304 305 fprintf(stdout, "%s: ", Progname); 306 vfprintf(stdout, fmt, ap); 307 fprintf(stdout, "\r\n"); 308 do_exit(1); 309 /*NOTREACHED*/ 310 } 311 312 void help() 313 { 314 fprintf(stdout, "\r 315 NetBSD loader for the Atari-TT\r 316 \r 317 Usage: %s [-abdhstvD] [-S <stram-size>] [kernel]\r 318 \r 319 Description of options:\r 320 \r 321 \t-a Boot up to multi-user mode.\r 322 \t-b Ask for root device to use.\r 323 \t-d Enter kernel debugger.\r 324 \t-h What your getting right now.\r 325 \t-s Use only ST-compatible RAM\r 326 \t-S Set amount of ST-compatible RAM\r 327 \t-T Set amount of TT-compatible RAM\r 328 \t-t Test the loader. It will do everything except executing the\r 329 \t loaded kernel.\r 330 \t-D printout debugging information while loading\r 331 \t-v Print loader version.\r 332 ", Progname); 333 do_exit(0); 334 } 335 336 void usage() 337 { 338 fprintf(stdout, "Usage: %s [-abdhtv] [kernel]\r\n", Progname); 339 do_exit(1); 340 } 341 342 void do_exit(code) 343 int code; 344 { 345 fprintf(stdout, "\r\nHit <return> to continue..."); 346 (void)getchar(); 347 fprintf(stdout, "\r\n"); 348 exit(code); 349 } 350 351 void start_kernel() 352 { 353 long stck; 354 355 stck = Super(0); 356 startit(); 357 /* NOT REACHED */ 358 359 Super(stck); 360 } 361 362 asm(" 363 .text 364 .globl _startit 365 366 _startit: 367 move.w #0x2700,sr 368 369 | the BSD kernel wants values into the following registers: 370 | d0: ttmem-size 371 | d1: stmem-size 372 | d2: cputype 373 | d3: boothowto 374 | d4: length of loaded kernel 375 | d5: start of fastram 376 | a0: start of loaded kernel 377 | a1: end of symbols (esym) 378 | All other registers zeroed for possible future requirements. 379 380 lea _kparam, a3 | a3 points to parameter block 381 lea _startit,sp | make sure we have a good stack *** 382 move.l (a3),a0 | loaded kernel 383 move.l 8(a3),-(sp) | push entry point *** 384 move.l a0,d0 | offset of loaded kernel 385 add.l d0,(sp) | add offset 386 move.l 12(a3),d1 | stmem-size 387 move.l 16(a3),d0 | ttmem-size 388 move.l 20(a3),d2 | cputype 389 move.l 24(a3),d3 | boothowto 390 move.l 4(a3),d4 | length of loaded kernel 391 move.l 28(a3),d5 | start of fastram 392 move.l 32(a3),a1 | end of symbols 393 sub.l a5,a5 | target, load to 0 394 btst #4, d2 | Is this an 68040? 395 beq not040 396 397 | Turn off 68040 MMU 398 .word 0x4e7b,0xd003 | movec a5,tc 399 .word 0x4e7b,0xd806 | movec a5,urp 400 .word 0x4e7b,0xd807 | movec a5,srp 401 .word 0x4e7b,0xd004 | movec a5,itt0 402 .word 0x4e7b,0xd005 | movec a5,itt1 403 .word 0x4e7b,0xd006 | movec a5,dtt0 404 .word 0x4e7b,0xd007 | movec a5,dtt1 405 bra nott 406 407 not040: 408 lea zero,a3 409 pmove (a3),tcr | Turn off MMU 410 lea nullrp,a3 411 pmove (a3),crp | Turn off MMU some more 412 pmove (a3),srp | Really, really, turn off MMU 413 414 | Turn off 68030 TT registers 415 btst #3, d2 | Is this an 68030? 416 beq.b nott 417 lea zero,a3 418 pmove (a3),tt0 419 pmove (a3),tt1 420 421 nott: 422 moveq.l #0,d6 | would have known contents) 423 moveq.l #0,d7 424 movea.l d6,a2 425 movea.l d6,a3 426 movea.l d6,a4 427 movea.l d6,a5 428 movea.l d6,a6 429 rts | enter kernel at address on stack *** 430 431 432 | A do-nothing MMU root pointer (includes the following long as well) 433 434 nullrp: .long 0x80000202 435 zero: .long 0 436 svsp: .long 0 437 438 "); 439