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