1 /* $NetBSD: loadbsd.c,v 1.15 1999/06/23 19:26:13 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 <a_out.h> 38 #include <fcntl.h> 39 #include <stdio.h> 40 #include <osbind.h> 41 #include <stdarg.h> 42 #include <stdlib.h> 43 #include <string.h> 44 #include <unistd.h> 45 #include "libtos.h" 46 #include "loader.h" 47 48 #ifdef COMPRESSED_READ 49 #define open copen 50 #define read cread 51 #define lseek clseek 52 #define close cclose 53 #endif /* COMPRESSED_READ */ 54 55 char *Progname; /* How are we called */ 56 int d_flag = 0; /* Output debugging output? */ 57 int h_flag = 0; /* show help */ 58 int s_flag = 0; /* St-ram only */ 59 int t_flag = 0; /* Just test, do not execute */ 60 int v_flag = 0; /* show version */ 61 62 const char version[] = "$Revision: 1.15 $"; 63 64 /* 65 * Default name of kernel to boot, large enough to patch 66 */ 67 char kname[80] = "n:/netbsd"; 68 69 static struct kparamb kparam; 70 71 void help PROTO((void)); 72 void usage PROTO((void)); 73 void get_sys_info PROTO((void)); 74 void start_kernel PROTO((void)); 75 76 int 77 main(argc, argv) 78 int argc; 79 char **argv; 80 { 81 /* 82 * Option parsing 83 */ 84 extern int optind; 85 extern char *optarg; 86 int ch; 87 int fd; 88 long textsz, stringsz; 89 struct exec ehdr; 90 91 init_toslib(argv[0]); 92 Progname = argv[0]; 93 94 kparam.boothowto = RB_SINGLE; 95 96 while ((ch = getopt(argc, argv, "abdhstVwDo:S:T:")) != -1) { 97 switch (ch) { 98 case 'a': 99 kparam.boothowto &= ~(RB_SINGLE); 100 kparam.boothowto |= RB_AUTOBOOT; 101 break; 102 case 'b': 103 kparam.boothowto |= RB_ASKNAME; 104 break; 105 case 'd': 106 kparam.boothowto |= RB_KDB; 107 break; 108 case 'D': 109 d_flag = 1; 110 break; 111 case 'h': 112 h_flag = 1; 113 break; 114 case 'o': 115 redirect_output(optarg); 116 break; 117 case 's': 118 s_flag = 1; 119 break; 120 case 'S': 121 kparam.stmem_size = atoi(optarg); 122 break; 123 case 't': 124 t_flag = 1; 125 break; 126 case 'T': 127 kparam.ttmem_size = atoi(optarg); 128 break; 129 case 'V': 130 v_flag = 1; 131 break; 132 case 'w': 133 set_wait_for_key(); 134 break; 135 default: 136 usage(); 137 } 138 } 139 argc -= optind; 140 argv += optind; 141 if (argc == 1) 142 strcpy(kname, argv[0]); 143 144 if (h_flag) 145 help(); 146 if (v_flag) 147 eprintf("%s\r\n", version); 148 149 /* 150 * Get system info to pass to NetBSD 151 */ 152 get_sys_info(); 153 154 /* 155 * Find the kernel to boot and read it's exec-header 156 */ 157 if ((fd = open(kname, O_RDONLY)) < 0) 158 fatal(-1, "Cannot open kernel '%s'", kname); 159 if (read(fd, (char *)&ehdr, sizeof(ehdr)) != sizeof(ehdr)) 160 fatal(-1, "Cannot read exec-header of '%s'", kname); 161 162 if (N_MAGIC(ehdr) != NMAGIC) 163 fatal(-1, "Not an NMAGIC file '%s'", kname); 164 165 /* 166 * Extract various sizes from the kernel executable 167 */ 168 textsz = (ehdr.a_text + __LDPGSZ - 1) & ~(__LDPGSZ - 1); 169 kparam.esym_loc = 0; 170 kparam.ksize = textsz + ehdr.a_data + ehdr.a_bss; 171 kparam.entry = ehdr.a_entry; 172 173 if (ehdr.a_syms) { 174 if (lseek(fd,ehdr.a_text+ehdr.a_data+ehdr.a_syms+sizeof(ehdr),0) 175 <= 0) 176 fatal(-1, "Cannot seek to string table in '%s'", kname); 177 if (read(fd, (char *)&stringsz, sizeof(long)) != sizeof(long)) 178 fatal(-1, "Cannot read string-table size"); 179 if (lseek(fd, sizeof(ehdr), 0) <= 0) 180 fatal(-1, "Cannot seek back to text start"); 181 kparam.ksize += ehdr.a_syms + sizeof(long) + stringsz; 182 } 183 184 if ((kparam.kp = (u_char *)malloc(kparam.ksize)) == NULL) 185 fatal(-1, "Cannot malloc kernel image space"); 186 187 /* 188 * Read text & data, clear bss 189 */ 190 if ((read(fd, (char *)kparam.kp, ehdr.a_text) != ehdr.a_text) 191 ||(read(fd,(char *)(kparam.kp+textsz),ehdr.a_data) != ehdr.a_data)) 192 fatal(-1, "Unable to read kernel image\n"); 193 memset(kparam.kp + textsz + ehdr.a_data, 0, ehdr.a_bss); 194 195 /* 196 * Read symbol and string table 197 */ 198 if (ehdr.a_syms) { 199 long *p; 200 201 p = (long *)(kparam.kp + textsz + ehdr.a_data + ehdr.a_bss); 202 *p++ = ehdr.a_syms; 203 if (read(fd, (char *)p, ehdr.a_syms) != ehdr.a_syms) 204 fatal(-1, "Cannot read symbol table\n"); 205 p = (long *)((char *)p + ehdr.a_syms); 206 if (read(fd, (char *)p, stringsz) != stringsz) 207 fatal(-1, "Cannot read string table\n"); 208 kparam.esym_loc = (long)((char *)p-(char *)kparam.kp +stringsz); 209 } 210 close(fd); 211 212 if (d_flag) { 213 eprintf("\r\nKernel info:\r\n"); 214 eprintf("Kernel loadaddr\t: 0x%08x\r\n", kparam.kp); 215 eprintf("Kernel size\t: %10d bytes\r\n", kparam.ksize); 216 eprintf("Kernel entry\t: 0x%08x\r\n", kparam.entry); 217 eprintf("Kernel esym\t: 0x%08x\r\n", kparam.esym_loc); 218 } 219 220 if (!t_flag) 221 start_kernel(); 222 /* NOT REACHED */ 223 224 eprintf("Kernel '%s' was loaded OK\r\n", kname); 225 xexit(0); 226 } 227 228 /* 229 * Extract memory and cpu/fpu info from system. 230 */ 231 void 232 get_sys_info() 233 { 234 long stck; 235 long *jar; 236 OSH *oshdr; 237 238 kparam.bootflags = 0; 239 240 stck = Super(0); 241 242 /* 243 * Some GEMDOS versions use a different year-base in the RTC. 244 */ 245 oshdr = *ADDR_OSHEAD; 246 oshdr = oshdr->os_beg; 247 if ((oshdr->os_version > 0x0300) && (oshdr->os_version < 0x0306)) 248 kparam.bootflags |= ATARI_CLKBROKEN; 249 250 if (kparam.stmem_size <= 0) 251 kparam.stmem_size = *ADDR_PHYSTOP; 252 253 if (kparam.ttmem_size) 254 kparam.ttmem_start = TTRAM_BASE; 255 else { 256 if (!s_flag && (*ADDR_CHKRAMTOP == RAM_TOP_MAGIC)) { 257 kparam.ttmem_size = *ADDR_RAMTOP; 258 if (kparam.ttmem_size > TTRAM_BASE) { 259 kparam.ttmem_size -= TTRAM_BASE; 260 kparam.ttmem_start = TTRAM_BASE; 261 } 262 else kparam.ttmem_size = 0; 263 } 264 } 265 266 /* 267 * Scan cookiejar for cpu types 268 */ 269 jar = *ADDR_P_COOKIE; 270 if (jar != NULL) { 271 do { 272 if (jar[0] == 0x5f435055) { /* _CPU */ 273 switch (jar[1]) { 274 case 0: 275 kparam.bootflags |= ATARI_68000; 276 break; 277 case 10: 278 kparam.bootflags |= ATARI_68010; 279 break; 280 case 20: 281 kparam.bootflags |= ATARI_68020; 282 break; 283 case 30: 284 kparam.bootflags |= ATARI_68030; 285 break; 286 case 40: 287 kparam.bootflags |= ATARI_68040; 288 break; 289 case 60: 290 kparam.bootflags |= ATARI_68060; 291 break; 292 default: 293 fatal(-1, "Unknown CPU-type"); 294 } 295 } 296 if (jar[0] == 0x42504658) { /* BPFX */ 297 unsigned long *p; 298 299 p = (unsigned long*)jar[1]; 300 301 kparam.ttmem_start = p[1]; 302 kparam.ttmem_size = p[2]; 303 } 304 if (jar[0] == 0x5f435432) { /* _CT2 */ 305 /* 306 * The CT2 board has a different physical base address! 307 */ 308 kparam.ttmem_start = CTRAM_BASE; 309 } 310 jar = &jar[2]; 311 } while (jar[-2]); 312 } 313 if (!(kparam.bootflags & ATARI_ANYCPU)) 314 fatal(-1, "Cannot determine CPU-type"); 315 316 (void)Super(stck); 317 318 if (d_flag) { 319 eprintf("Machine info:\r\n"); 320 eprintf("ST-RAM size\t: %10d bytes\r\n",kparam.stmem_size); 321 eprintf("TT-RAM size\t: %10d bytes\r\n",kparam.ttmem_size); 322 eprintf("TT-RAM start\t: 0x%08x\r\n", kparam.ttmem_start); 323 eprintf("Cpu-type\t: 0x%08x\r\n", kparam.bootflags); 324 } 325 } 326 327 void 328 help() 329 { 330 eprintf("\r 331 NetBSD loader for the Atari-TT\r 332 \r 333 Usage: %s [-abdhstVD] [-S <stram-size>] [-T <ttram-size>] [kernel]\r 334 \r 335 Description of options:\r 336 \r 337 \t-a Boot up to multi-user mode.\r 338 \t-b Ask for root device to use.\r 339 \t-d Enter kernel debugger.\r 340 \t-D printout debug information while loading\r 341 \t-h What you're getting right now.\r 342 \t-o Write output to both <output file> and stdout.\r 343 \t-s Use only ST-compatible RAM\r 344 \t-S Set amount of ST-compatible RAM\r 345 \t-T Set amount of TT-compatible RAM\r 346 \t-t Test the loader. It will do everything except executing the\r 347 \t loaded kernel.\r 348 \t-V Print loader version.\r 349 \t-w Wait for a keypress before exiting.\r 350 ", Progname); 351 xexit(0); 352 } 353 354 void 355 usage() 356 { 357 eprintf("Usage: %s [-abdhstVD] [-S <stram-size>] " 358 "[-T <ttram-size>] [kernel]\r\n", Progname); 359 xexit(1); 360 } 361 362 void 363 start_kernel() 364 { 365 long stck; 366 367 stck = Super(0); 368 bsd_startup(&kparam); 369 /* NOT REACHED */ 370 371 (void)Super(stck); 372 } 373