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