loadbsd.c revision 1.18 1 1.18 leo /* $NetBSD: loadbsd.c,v 1.18 2001/10/11 07:07:43 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.1 leo #include <fcntl.h>
38 1.10 leo #include <stdio.h>
39 1.1 leo #include <osbind.h>
40 1.1 leo #include <stdarg.h>
41 1.10 leo #include <stdlib.h>
42 1.10 leo #include <string.h>
43 1.10 leo #include <unistd.h>
44 1.10 leo #include "libtos.h"
45 1.17 leo #include "tosdefs.h"
46 1.18 leo #include "cread.h"
47 1.15 leo
48 1.1 leo char *Progname; /* How are we called */
49 1.10 leo int d_flag = 0; /* Output debugging output? */
50 1.10 leo int h_flag = 0; /* show help */
51 1.16 leo int N_flag = 0; /* No symbols? */
52 1.10 leo int s_flag = 0; /* St-ram only */
53 1.10 leo int t_flag = 0; /* Just test, do not execute */
54 1.10 leo int v_flag = 0; /* show version */
55 1.1 leo
56 1.18 leo const char version[] = "$Revision: 1.18 $";
57 1.1 leo
58 1.1 leo /*
59 1.1 leo * Default name of kernel to boot, large enough to patch
60 1.1 leo */
61 1.10 leo char kname[80] = "n:/netbsd";
62 1.10 leo
63 1.17 leo static osdsc_t kernelparms;
64 1.1 leo
65 1.10 leo void help PROTO((void));
66 1.10 leo void usage PROTO((void));
67 1.17 leo void get_sys_info PROTO((osdsc_t *));
68 1.17 leo void start_kernel PROTO((osdsc_t *));
69 1.1 leo
70 1.10 leo int
71 1.10 leo main(argc, argv)
72 1.1 leo int argc;
73 1.1 leo char **argv;
74 1.1 leo {
75 1.1 leo /*
76 1.1 leo * Option parsing
77 1.1 leo */
78 1.1 leo extern int optind;
79 1.1 leo extern char *optarg;
80 1.17 leo int ch, err;
81 1.17 leo char *errmsg;
82 1.1 leo int fd;
83 1.17 leo osdsc_t *od;
84 1.1 leo
85 1.10 leo init_toslib(argv[0]);
86 1.1 leo Progname = argv[0];
87 1.1 leo
88 1.17 leo od = &kernelparms;
89 1.17 leo od->boothowto = RB_SINGLE;
90 1.1 leo
91 1.16 leo while ((ch = getopt(argc, argv, "abdDhNstVwo:S:T:")) != -1) {
92 1.10 leo switch (ch) {
93 1.1 leo case 'a':
94 1.17 leo od->boothowto &= ~(RB_SINGLE);
95 1.17 leo od->boothowto |= RB_AUTOBOOT;
96 1.1 leo break;
97 1.1 leo case 'b':
98 1.17 leo od->boothowto |= RB_ASKNAME;
99 1.1 leo break;
100 1.1 leo case 'd':
101 1.17 leo od->boothowto |= RB_KDB;
102 1.1 leo break;
103 1.3 leo case 'D':
104 1.3 leo d_flag = 1;
105 1.3 leo break;
106 1.10 leo case 'h':
107 1.10 leo h_flag = 1;
108 1.10 leo break;
109 1.16 leo case 'N':
110 1.16 leo N_flag = 1;
111 1.16 leo break;
112 1.10 leo case 'o':
113 1.10 leo redirect_output(optarg);
114 1.10 leo break;
115 1.3 leo case 's':
116 1.3 leo s_flag = 1;
117 1.3 leo break;
118 1.3 leo case 'S':
119 1.17 leo od->stmem_size = atoi(optarg);
120 1.3 leo break;
121 1.1 leo case 't':
122 1.1 leo t_flag = 1;
123 1.1 leo break;
124 1.5 leo case 'T':
125 1.17 leo od->ttmem_size = atoi(optarg);
126 1.5 leo break;
127 1.10 leo case 'V':
128 1.10 leo v_flag = 1;
129 1.10 leo break;
130 1.10 leo case 'w':
131 1.10 leo set_wait_for_key();
132 1.1 leo break;
133 1.1 leo default:
134 1.1 leo usage();
135 1.1 leo }
136 1.1 leo }
137 1.1 leo argc -= optind;
138 1.1 leo argv += optind;
139 1.10 leo if (argc == 1)
140 1.1 leo strcpy(kname, argv[0]);
141 1.1 leo
142 1.10 leo if (h_flag)
143 1.10 leo help();
144 1.10 leo if (v_flag)
145 1.10 leo eprintf("%s\r\n", version);
146 1.10 leo
147 1.1 leo /*
148 1.2 leo * Get system info to pass to NetBSD
149 1.1 leo */
150 1.17 leo get_sys_info(od);
151 1.17 leo if (d_flag) {
152 1.17 leo eprintf("Machine info:\r\n");
153 1.17 leo eprintf("ST-RAM size\t: %10d bytes\r\n",od->stmem_size);
154 1.17 leo eprintf("TT-RAM size\t: %10d bytes\r\n",od->ttmem_size);
155 1.17 leo eprintf("TT-RAM start\t: 0x%08x\r\n", od->ttmem_start);
156 1.17 leo eprintf("Cpu-type\t: 0x%08x\r\n", od->cputype);
157 1.17 leo }
158 1.1 leo
159 1.1 leo /*
160 1.1 leo * Find the kernel to boot and read it's exec-header
161 1.1 leo */
162 1.10 leo if ((fd = open(kname, O_RDONLY)) < 0)
163 1.10 leo fatal(-1, "Cannot open kernel '%s'", kname);
164 1.17 leo if ((err = elf_load(fd, od, &errmsg, !N_flag)) == -1) {
165 1.17 leo /*
166 1.17 leo * Not ELF, try a.out
167 1.17 leo */
168 1.17 leo if (err = aout_load(fd, od, &errmsg, !N_flag)) {
169 1.17 leo if (err == -1)
170 1.17 leo errmsg = "Not an ELF or NMAGIC file '%s'";
171 1.17 leo fatal(-1, errmsg, kname);
172 1.17 leo }
173 1.17 leo }
174 1.17 leo else {
175 1.17 leo if (err)
176 1.17 leo fatal(-1, errmsg);
177 1.17 leo }
178 1.17 leo
179 1.16 leo close(fd);
180 1.16 leo
181 1.16 leo if (d_flag) {
182 1.16 leo eprintf("\r\nKernel info:\r\n");
183 1.17 leo eprintf("Kernel loadaddr\t: 0x%08x\r\n", od->kstart);
184 1.17 leo eprintf("Kernel size\t: %10d bytes\r\n", od->ksize);
185 1.17 leo eprintf("Kernel entry\t: 0x%08x\r\n", od->kentry);
186 1.17 leo eprintf("Kernel esym\t: 0x%08x\r\n", od->k_esym);
187 1.16 leo }
188 1.16 leo
189 1.16 leo if (!t_flag)
190 1.17 leo start_kernel(od);
191 1.16 leo /* NOT REACHED */
192 1.16 leo
193 1.16 leo eprintf("Kernel '%s' was loaded OK\r\n", kname);
194 1.16 leo xexit(0);
195 1.16 leo return 0;
196 1.16 leo }
197 1.16 leo
198 1.10 leo void
199 1.17 leo get_sys_info(od)
200 1.17 leo osdsc_t *od;
201 1.1 leo {
202 1.1 leo long stck;
203 1.1 leo
204 1.1 leo stck = Super(0);
205 1.7 leo
206 1.17 leo sys_info(od);
207 1.1 leo
208 1.17 leo if (!(od->cputype & ATARI_ANYCPU))
209 1.10 leo fatal(-1, "Cannot determine CPU-type");
210 1.1 leo
211 1.10 leo (void)Super(stck);
212 1.17 leo if (s_flag)
213 1.17 leo od->ttmem_size = od->ttmem_start = 0;
214 1.1 leo }
215 1.1 leo
216 1.10 leo void
217 1.10 leo help()
218 1.1 leo {
219 1.10 leo eprintf("\r
220 1.3 leo NetBSD loader for the Atari-TT\r
221 1.3 leo \r
222 1.10 leo Usage: %s [-abdhstVD] [-S <stram-size>] [-T <ttram-size>] [kernel]\r
223 1.3 leo \r
224 1.3 leo Description of options:\r
225 1.3 leo \r
226 1.3 leo \t-a Boot up to multi-user mode.\r
227 1.3 leo \t-b Ask for root device to use.\r
228 1.3 leo \t-d Enter kernel debugger.\r
229 1.10 leo \t-D printout debug information while loading\r
230 1.11 leo \t-h What you're getting right now.\r
231 1.16 leo `t-N No symbols must be loaded.\r
232 1.10 leo \t-o Write output to both <output file> and stdout.\r
233 1.3 leo \t-s Use only ST-compatible RAM\r
234 1.3 leo \t-S Set amount of ST-compatible RAM\r
235 1.5 leo \t-T Set amount of TT-compatible RAM\r
236 1.3 leo \t-t Test the loader. It will do everything except executing the\r
237 1.3 leo \t loaded kernel.\r
238 1.10 leo \t-V Print loader version.\r
239 1.10 leo \t-w Wait for a keypress before exiting.\r
240 1.1 leo ", Progname);
241 1.10 leo xexit(0);
242 1.1 leo }
243 1.1 leo
244 1.10 leo void
245 1.10 leo usage()
246 1.1 leo {
247 1.10 leo eprintf("Usage: %s [-abdhstVD] [-S <stram-size>] "
248 1.10 leo "[-T <ttram-size>] [kernel]\r\n", Progname);
249 1.10 leo xexit(1);
250 1.3 leo }
251 1.3 leo
252 1.10 leo void
253 1.17 leo start_kernel(od)
254 1.17 leo osdsc_t *od;
255 1.1 leo {
256 1.1 leo long stck;
257 1.1 leo
258 1.1 leo stck = Super(0);
259 1.17 leo bsd_startup(&(od->kp));
260 1.1 leo /* NOT REACHED */
261 1.1 leo
262 1.10 leo (void)Super(stck);
263 1.1 leo }
264