loadbsd.c revision 1.17 1 1.17 leo /* $NetBSD: loadbsd.c,v 1.17 2001/10/10 14:24:48 leo Exp $ */
2 1.1 leo
3 1.1 leo /*
4 1.1 leo * Copyright (c) 1995 L. Weppelman
5 1.1 leo * All rights reserved.
6 1.1 leo *
7 1.1 leo * Redistribution and use in source and binary forms, with or without
8 1.1 leo * modification, are permitted provided that the following conditions
9 1.1 leo * are met:
10 1.1 leo * 1. Redistributions of source code must retain the above copyright
11 1.1 leo * notice, this list of conditions and the following disclaimer.
12 1.1 leo * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 leo * notice, this list of conditions and the following disclaimer in the
14 1.1 leo * documentation and/or other materials provided with the distribution.
15 1.1 leo * 3. All advertising materials mentioning features or use of this software
16 1.1 leo * must display the following acknowledgement:
17 1.1 leo * This product includes software developed by Leo Weppelman.
18 1.1 leo * 4. The name of the author may not be used to endorse or promote products
19 1.1 leo * derived from this software without specific prior written permission
20 1.1 leo *
21 1.1 leo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 leo * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 leo * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 leo * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 leo * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 leo * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 leo * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 leo * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 leo * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.1 leo * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 leo */
32 1.1 leo
33 1.1 leo /*
34 1.1 leo * NetBSD loader for the Atari-TT.
35 1.1 leo */
36 1.1 leo
37 1.16 leo #include "exec_elf.h"
38 1.1 leo #include <a_out.h>
39 1.1 leo #include <fcntl.h>
40 1.10 leo #include <stdio.h>
41 1.1 leo #include <osbind.h>
42 1.1 leo #include <stdarg.h>
43 1.10 leo #include <stdlib.h>
44 1.10 leo #include <string.h>
45 1.10 leo #include <unistd.h>
46 1.10 leo #include "libtos.h"
47 1.17 leo #include "tosdefs.h"
48 1.1 leo
49 1.15 leo #ifdef COMPRESSED_READ
50 1.15 leo #define open copen
51 1.15 leo #define read cread
52 1.15 leo #define lseek clseek
53 1.15 leo #define close cclose
54 1.15 leo #endif /* COMPRESSED_READ */
55 1.15 leo
56 1.1 leo char *Progname; /* How are we called */
57 1.10 leo int d_flag = 0; /* Output debugging output? */
58 1.10 leo int h_flag = 0; /* show help */
59 1.16 leo int N_flag = 0; /* No symbols? */
60 1.10 leo int s_flag = 0; /* St-ram only */
61 1.10 leo int t_flag = 0; /* Just test, do not execute */
62 1.10 leo int v_flag = 0; /* show version */
63 1.1 leo
64 1.17 leo const char version[] = "$Revision: 1.17 $";
65 1.1 leo
66 1.1 leo /*
67 1.1 leo * Default name of kernel to boot, large enough to patch
68 1.1 leo */
69 1.10 leo char kname[80] = "n:/netbsd";
70 1.10 leo
71 1.17 leo static osdsc_t kernelparms;
72 1.1 leo
73 1.10 leo void help PROTO((void));
74 1.10 leo void usage PROTO((void));
75 1.17 leo void get_sys_info PROTO((osdsc_t *));
76 1.17 leo void start_kernel PROTO((osdsc_t *));
77 1.1 leo
78 1.16 leo #define ELFMAGIC ((ELFMAG0 << 24) | (ELFMAG1 << 16) | \
79 1.16 leo (ELFMAG2 << 8) | ELFMAG3)
80 1.10 leo int
81 1.10 leo main(argc, argv)
82 1.1 leo int argc;
83 1.1 leo char **argv;
84 1.1 leo {
85 1.1 leo /*
86 1.1 leo * Option parsing
87 1.1 leo */
88 1.1 leo extern int optind;
89 1.1 leo extern char *optarg;
90 1.17 leo int ch, err;
91 1.17 leo char *errmsg;
92 1.1 leo int fd;
93 1.17 leo osdsc_t *od;
94 1.1 leo
95 1.10 leo init_toslib(argv[0]);
96 1.1 leo Progname = argv[0];
97 1.1 leo
98 1.17 leo od = &kernelparms;
99 1.17 leo od->boothowto = RB_SINGLE;
100 1.1 leo
101 1.16 leo while ((ch = getopt(argc, argv, "abdDhNstVwo:S:T:")) != -1) {
102 1.10 leo switch (ch) {
103 1.1 leo case 'a':
104 1.17 leo od->boothowto &= ~(RB_SINGLE);
105 1.17 leo od->boothowto |= RB_AUTOBOOT;
106 1.1 leo break;
107 1.1 leo case 'b':
108 1.17 leo od->boothowto |= RB_ASKNAME;
109 1.1 leo break;
110 1.1 leo case 'd':
111 1.17 leo od->boothowto |= RB_KDB;
112 1.1 leo break;
113 1.3 leo case 'D':
114 1.3 leo d_flag = 1;
115 1.3 leo break;
116 1.10 leo case 'h':
117 1.10 leo h_flag = 1;
118 1.10 leo break;
119 1.16 leo case 'N':
120 1.16 leo N_flag = 1;
121 1.16 leo break;
122 1.10 leo case 'o':
123 1.10 leo redirect_output(optarg);
124 1.10 leo break;
125 1.3 leo case 's':
126 1.3 leo s_flag = 1;
127 1.3 leo break;
128 1.3 leo case 'S':
129 1.17 leo od->stmem_size = atoi(optarg);
130 1.3 leo break;
131 1.1 leo case 't':
132 1.1 leo t_flag = 1;
133 1.1 leo break;
134 1.5 leo case 'T':
135 1.17 leo od->ttmem_size = atoi(optarg);
136 1.5 leo break;
137 1.10 leo case 'V':
138 1.10 leo v_flag = 1;
139 1.10 leo break;
140 1.10 leo case 'w':
141 1.10 leo set_wait_for_key();
142 1.1 leo break;
143 1.1 leo default:
144 1.1 leo usage();
145 1.1 leo }
146 1.1 leo }
147 1.1 leo argc -= optind;
148 1.1 leo argv += optind;
149 1.10 leo if (argc == 1)
150 1.1 leo strcpy(kname, argv[0]);
151 1.1 leo
152 1.10 leo if (h_flag)
153 1.10 leo help();
154 1.10 leo if (v_flag)
155 1.10 leo eprintf("%s\r\n", version);
156 1.10 leo
157 1.1 leo /*
158 1.2 leo * Get system info to pass to NetBSD
159 1.1 leo */
160 1.17 leo get_sys_info(od);
161 1.17 leo if (d_flag) {
162 1.17 leo eprintf("Machine info:\r\n");
163 1.17 leo eprintf("ST-RAM size\t: %10d bytes\r\n",od->stmem_size);
164 1.17 leo eprintf("TT-RAM size\t: %10d bytes\r\n",od->ttmem_size);
165 1.17 leo eprintf("TT-RAM start\t: 0x%08x\r\n", od->ttmem_start);
166 1.17 leo eprintf("Cpu-type\t: 0x%08x\r\n", od->cputype);
167 1.17 leo }
168 1.1 leo
169 1.1 leo /*
170 1.1 leo * Find the kernel to boot and read it's exec-header
171 1.1 leo */
172 1.10 leo if ((fd = open(kname, O_RDONLY)) < 0)
173 1.10 leo fatal(-1, "Cannot open kernel '%s'", kname);
174 1.17 leo if ((err = elf_load(fd, od, &errmsg, !N_flag)) == -1) {
175 1.17 leo /*
176 1.17 leo * Not ELF, try a.out
177 1.17 leo */
178 1.17 leo if (err = aout_load(fd, od, &errmsg, !N_flag)) {
179 1.17 leo if (err == -1)
180 1.17 leo errmsg = "Not an ELF or NMAGIC file '%s'";
181 1.17 leo fatal(-1, errmsg, kname);
182 1.17 leo }
183 1.17 leo }
184 1.17 leo else {
185 1.17 leo if (err)
186 1.17 leo fatal(-1, errmsg);
187 1.17 leo }
188 1.17 leo
189 1.16 leo close(fd);
190 1.16 leo
191 1.16 leo if (d_flag) {
192 1.16 leo eprintf("\r\nKernel info:\r\n");
193 1.17 leo eprintf("Kernel loadaddr\t: 0x%08x\r\n", od->kstart);
194 1.17 leo eprintf("Kernel size\t: %10d bytes\r\n", od->ksize);
195 1.17 leo eprintf("Kernel entry\t: 0x%08x\r\n", od->kentry);
196 1.17 leo eprintf("Kernel esym\t: 0x%08x\r\n", od->k_esym);
197 1.16 leo }
198 1.16 leo
199 1.16 leo if (!t_flag)
200 1.17 leo start_kernel(od);
201 1.16 leo /* NOT REACHED */
202 1.16 leo
203 1.16 leo eprintf("Kernel '%s' was loaded OK\r\n", kname);
204 1.16 leo xexit(0);
205 1.16 leo return 0;
206 1.16 leo }
207 1.16 leo
208 1.10 leo void
209 1.17 leo get_sys_info(od)
210 1.17 leo osdsc_t *od;
211 1.1 leo {
212 1.1 leo long stck;
213 1.1 leo
214 1.1 leo stck = Super(0);
215 1.7 leo
216 1.17 leo sys_info(od);
217 1.1 leo
218 1.17 leo if (!(od->cputype & ATARI_ANYCPU))
219 1.10 leo fatal(-1, "Cannot determine CPU-type");
220 1.1 leo
221 1.10 leo (void)Super(stck);
222 1.17 leo if (s_flag)
223 1.17 leo od->ttmem_size = od->ttmem_start = 0;
224 1.1 leo }
225 1.1 leo
226 1.10 leo void
227 1.10 leo help()
228 1.1 leo {
229 1.10 leo eprintf("\r
230 1.3 leo NetBSD loader for the Atari-TT\r
231 1.3 leo \r
232 1.10 leo Usage: %s [-abdhstVD] [-S <stram-size>] [-T <ttram-size>] [kernel]\r
233 1.3 leo \r
234 1.3 leo Description of options:\r
235 1.3 leo \r
236 1.3 leo \t-a Boot up to multi-user mode.\r
237 1.3 leo \t-b Ask for root device to use.\r
238 1.3 leo \t-d Enter kernel debugger.\r
239 1.10 leo \t-D printout debug information while loading\r
240 1.11 leo \t-h What you're getting right now.\r
241 1.16 leo `t-N No symbols must be loaded.\r
242 1.10 leo \t-o Write output to both <output file> and stdout.\r
243 1.3 leo \t-s Use only ST-compatible RAM\r
244 1.3 leo \t-S Set amount of ST-compatible RAM\r
245 1.5 leo \t-T Set amount of TT-compatible RAM\r
246 1.3 leo \t-t Test the loader. It will do everything except executing the\r
247 1.3 leo \t loaded kernel.\r
248 1.10 leo \t-V Print loader version.\r
249 1.10 leo \t-w Wait for a keypress before exiting.\r
250 1.1 leo ", Progname);
251 1.10 leo xexit(0);
252 1.1 leo }
253 1.1 leo
254 1.10 leo void
255 1.10 leo usage()
256 1.1 leo {
257 1.10 leo eprintf("Usage: %s [-abdhstVD] [-S <stram-size>] "
258 1.10 leo "[-T <ttram-size>] [kernel]\r\n", Progname);
259 1.10 leo xexit(1);
260 1.3 leo }
261 1.3 leo
262 1.10 leo void
263 1.17 leo start_kernel(od)
264 1.17 leo osdsc_t *od;
265 1.1 leo {
266 1.1 leo long stck;
267 1.1 leo
268 1.1 leo stck = Super(0);
269 1.17 leo bsd_startup(&(od->kp));
270 1.1 leo /* NOT REACHED */
271 1.1 leo
272 1.10 leo (void)Super(stck);
273 1.1 leo }
274