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