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