bootxx.c revision 1.1 1 1.1 leo /* $NetBSD: bootxx.c,v 1.1 1996/02/29 11:35:05 leo Exp $ */
2 1.1 leo
3 1.1 leo /*
4 1.1 leo * Copyright (c) 1995 Waldi Ravens.
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 Waldi Ravens.
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 asm(" .text
34 1.1 leo .even
35 1.1 leo start:
36 1.1 leo bras _bootxx
37 1.1 leo ");
38 1.1 leo #define boot_BSD bsd_startup
39 1.1 leo
40 1.1 leo #include <stand.h>
41 1.1 leo #include <string.h>
42 1.1 leo #include <libkern.h>
43 1.1 leo #include <kparamb.h>
44 1.1 leo #include <sys/exec.h>
45 1.1 leo #include <sys/reboot.h>
46 1.1 leo #include <machine/cpu.h>
47 1.1 leo
48 1.1 leo #include "bootxx.h"
49 1.1 leo
50 1.1 leo void boot_BSD __P((kparb *)__attribute__((noreturn)));
51 1.1 leo int load_BSD __P((osdsc *));
52 1.1 leo int sys_info __P((osdsc *));
53 1.1 leo int usr_info __P((osdsc *));
54 1.1 leo
55 1.1 leo int
56 1.1 leo bootxx(readsector, disklabel, autoboot)
57 1.1 leo void *readsector,
58 1.1 leo *disklabel;
59 1.1 leo int autoboot;
60 1.1 leo {
61 1.1 leo static osdsc os_desc;
62 1.1 leo extern char end[], edata[];
63 1.1 leo osdsc *od = &os_desc;
64 1.1 leo
65 1.1 leo bzero(edata, end - edata);
66 1.1 leo
67 1.1 leo printf("\033v\nNetBSD/Atari boot loader ($Revision: 1.1 $)\n\n");
68 1.1 leo
69 1.1 leo if (init_dskio(readsector, disklabel, -1))
70 1.1 leo return(-1);
71 1.1 leo
72 1.1 leo if (sys_info(od))
73 1.1 leo return(-2);
74 1.1 leo
75 1.1 leo for (;;) {
76 1.1 leo od->rootfs = 0; /* partition a */
77 1.1 leo od->osname = "/netbsd";
78 1.1 leo od->ostype = &od->osname[1];
79 1.1 leo od->boothowto = (RB_RDONLY);
80 1.1 leo
81 1.1 leo if (!autoboot) {
82 1.1 leo int pref;
83 1.1 leo
84 1.1 leo od->boothowto = (RB_RDONLY|RB_SINGLE);
85 1.1 leo pref = usr_info(od);
86 1.1 leo if (pref < 0)
87 1.1 leo continue;
88 1.1 leo if (pref > 0)
89 1.1 leo return(pref);
90 1.1 leo }
91 1.1 leo autoboot = 0; /* in case auto boot fails */
92 1.1 leo
93 1.1 leo if (init_dskio(readsector, disklabel, od->rootfs))
94 1.1 leo continue;
95 1.1 leo
96 1.1 leo if (load_BSD(od))
97 1.1 leo continue;
98 1.1 leo
99 1.1 leo boot_BSD(&od->kp);
100 1.1 leo }
101 1.1 leo /* NOTREACHED */
102 1.1 leo }
103 1.1 leo
104 1.1 leo int
105 1.1 leo sys_info(od)
106 1.1 leo osdsc *od;
107 1.1 leo {
108 1.1 leo long *jar;
109 1.1 leo int tos;
110 1.1 leo
111 1.1 leo od->stmem_size = *ADDR_PHYSTOP;
112 1.1 leo
113 1.1 leo if (*ADDR_CHKRAMTOP == RAMTOP_MAGIC) {
114 1.1 leo od->ttmem_size = *ADDR_RAMTOP;
115 1.1 leo if (od->ttmem_size > TTRAM_BASE) {
116 1.1 leo od->ttmem_size -= TTRAM_BASE;
117 1.1 leo od->ttmem_start = TTRAM_BASE;
118 1.1 leo }
119 1.1 leo }
120 1.1 leo
121 1.1 leo tos = (*ADDR_SYSBASE)->os_version;
122 1.1 leo if ((tos > 0x300) && (tos < 0x306))
123 1.1 leo od->cputype = ATARI_CLKBROKEN;
124 1.1 leo
125 1.1 leo if ((jar = *ADDR_P_COOKIE)) {
126 1.1 leo while (jar[0]) {
127 1.1 leo if (jar[0] == 0x5f435055L) { /* _CPU */
128 1.1 leo switch(jar[1]) {
129 1.1 leo case 0:
130 1.1 leo od->cputype |= ATARI_68000;
131 1.1 leo break;
132 1.1 leo case 10:
133 1.1 leo od->cputype |= ATARI_68010;
134 1.1 leo break;
135 1.1 leo case 20:
136 1.1 leo od->cputype |= ATARI_68020;
137 1.1 leo break;
138 1.1 leo case 30:
139 1.1 leo od->cputype |= ATARI_68030;
140 1.1 leo break;
141 1.1 leo case 40:
142 1.1 leo od->cputype |= ATARI_68040;
143 1.1 leo break;
144 1.1 leo }
145 1.1 leo }
146 1.1 leo jar += 2;
147 1.1 leo }
148 1.1 leo }
149 1.1 leo if (!(od->cputype & ATARI_ANYCPU)) {
150 1.1 leo printf("Unknown CPU-type.\n");
151 1.1 leo return(-1);
152 1.1 leo }
153 1.1 leo
154 1.1 leo return(0);
155 1.1 leo }
156 1.1 leo
157 1.1 leo int
158 1.1 leo usr_info(od)
159 1.1 leo osdsc *od;
160 1.1 leo {
161 1.1 leo static char line[800];
162 1.1 leo char c, *p = line;
163 1.1 leo
164 1.1 leo printf("\nEnter os-type [.%s] root-fs [:a] kernel [%s]"
165 1.1 leo " options [none]:\n\033e", od->ostype, od->osname);
166 1.1 leo gets(p);
167 1.1 leo printf("\033f");
168 1.1 leo
169 1.1 leo for (;;) {
170 1.1 leo while (isspace(*p))
171 1.1 leo *p++ = '\0';
172 1.1 leo
173 1.1 leo switch (*p++) {
174 1.1 leo case '\0':
175 1.1 leo goto done;
176 1.1 leo case ':':
177 1.1 leo if ((c = *p) >= 'a' && c <= 'z')
178 1.1 leo od->rootfs = c - 'a';
179 1.1 leo else if (c >= 'A' && c <= 'Z')
180 1.1 leo od->rootfs = c - ('A' - 27);
181 1.1 leo else return(-1);
182 1.1 leo
183 1.1 leo if (!od->rootfs)
184 1.1 leo break;
185 1.1 leo *p = 'b';
186 1.1 leo /* FALLTHROUGH */
187 1.1 leo case '-':
188 1.1 leo if ((c = *p) == 'a')
189 1.1 leo od->boothowto &= ~RB_SINGLE;
190 1.1 leo else if (c == 'b')
191 1.1 leo od->boothowto |= RB_ASKNAME;
192 1.1 leo else if (c == 'd')
193 1.1 leo od->boothowto |= RB_KDB;
194 1.1 leo else return(-1);
195 1.1 leo break;
196 1.1 leo case '.':
197 1.1 leo od->ostype = p;
198 1.1 leo break;
199 1.1 leo case '/':
200 1.1 leo od->osname = --p;
201 1.1 leo break;
202 1.1 leo default:
203 1.1 leo return(-1);
204 1.1 leo }
205 1.1 leo
206 1.1 leo while ((c = *p) && !isspace(c))
207 1.1 leo p += 1;
208 1.1 leo }
209 1.1 leo
210 1.1 leo done:
211 1.1 leo c = od->ostype[0];
212 1.1 leo if (isupper(c))
213 1.1 leo c = tolower(c);
214 1.1 leo
215 1.1 leo switch (c) {
216 1.1 leo case 'n': /* NetBSD */
217 1.1 leo return(0);
218 1.1 leo case 'l': /* Linux */
219 1.1 leo return(0x10);
220 1.1 leo case 'a': /* ASV */
221 1.1 leo return(0x40);
222 1.1 leo case 't': /* TOS */
223 1.1 leo return(0x80);
224 1.1 leo default:
225 1.1 leo return(-1);
226 1.1 leo }
227 1.1 leo }
228 1.1 leo
229 1.1 leo int
230 1.1 leo load_BSD(od)
231 1.1 leo osdsc *od;
232 1.1 leo {
233 1.1 leo struct exec hdr;
234 1.1 leo int err, fd;
235 1.1 leo u_int textsz, strsz;
236 1.1 leo
237 1.1 leo /*
238 1.1 leo * Read kernel's exec-header.
239 1.1 leo */
240 1.1 leo err = 1;
241 1.1 leo if ((fd = open(od->osname, 0)) < 0)
242 1.1 leo goto error;
243 1.1 leo err = 2;
244 1.1 leo if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr))
245 1.1 leo goto error;
246 1.1 leo err = 3;
247 1.1 leo if (N_GETMAGIC(hdr) != NMAGIC)
248 1.1 leo goto error;
249 1.1 leo
250 1.1 leo /*
251 1.1 leo * Extract sizes from kernel executable.
252 1.1 leo */
253 1.1 leo textsz = (hdr.a_text + __LDPGSZ - 1) & ~(__LDPGSZ - 1);
254 1.1 leo od->ksize = textsz + hdr.a_data + hdr.a_bss;
255 1.1 leo od->kentry = hdr.a_entry;
256 1.1 leo od->kstart = NULL;
257 1.1 leo od->k_esym = 0;
258 1.1 leo
259 1.1 leo if (hdr.a_syms) {
260 1.1 leo u_int x = sizeof(hdr) + hdr.a_text + hdr.a_data + hdr.a_syms;
261 1.1 leo err = 8;
262 1.1 leo if (lseek(fd, (off_t)x, SEEK_SET) != x)
263 1.1 leo goto error;
264 1.1 leo err = 9;
265 1.1 leo if (read(fd, &strsz, sizeof(strsz)) != sizeof(strsz))
266 1.1 leo goto error;
267 1.1 leo err = 10;
268 1.1 leo if (lseek(fd, (off_t)sizeof(hdr), SEEK_SET) != sizeof(hdr))
269 1.1 leo goto error;
270 1.1 leo od->ksize += sizeof(strsz) + hdr.a_syms + strsz;
271 1.1 leo }
272 1.1 leo
273 1.1 leo /*
274 1.1 leo * Read text & data, clear bss
275 1.1 leo */
276 1.1 leo err = 16;
277 1.1 leo if ((od->kstart = alloc(od->ksize)) == NULL)
278 1.1 leo goto error;
279 1.1 leo err = 17;
280 1.1 leo if (read(fd, od->kstart, hdr.a_text) != hdr.a_text)
281 1.1 leo goto error;
282 1.1 leo err = 18;
283 1.1 leo if (read(fd, od->kstart + textsz, hdr.a_data) != hdr.a_data)
284 1.1 leo goto error;
285 1.1 leo bzero(od->kstart + textsz + hdr.a_data, hdr.a_bss);
286 1.1 leo
287 1.1 leo /*
288 1.1 leo * Read symbol and string table
289 1.1 leo */
290 1.1 leo if (hdr.a_syms) {
291 1.1 leo char *p = od->kstart + textsz + hdr.a_data + hdr.a_bss;
292 1.1 leo *((u_int32_t *)p)++ = hdr.a_syms;
293 1.1 leo err = 24;
294 1.1 leo if (read(fd, p, hdr.a_syms) != hdr.a_syms)
295 1.1 leo goto error;
296 1.1 leo p += hdr.a_syms;
297 1.1 leo err = 25;
298 1.1 leo if (read(fd, p, strsz) != strsz)
299 1.1 leo goto error;
300 1.1 leo od->k_esym = (p - (char *)od->kstart) + strsz;
301 1.1 leo }
302 1.1 leo
303 1.1 leo return(0);
304 1.1 leo
305 1.1 leo error:
306 1.1 leo if (fd >= 0) {
307 1.1 leo if (od->kstart)
308 1.1 leo free(od->kstart, od->ksize);
309 1.1 leo close(fd);
310 1.1 leo }
311 1.1 leo printf("Error %d in load_BSD.\n", err);
312 1.1 leo return(-1);
313 1.1 leo }
314