main.c revision 1.7 1 /*
2 * $NetBSD: main.c,v 1.7 1998/05/08 19:08:18 chopps Exp $
3 *
4 *
5 * Copyright (c) 1996 Ignatios Souvatzis
6 * Copyright (c) 1994 Michael L. Hitch
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by Michael L. Hitch.
20 * 4. The name of the authors may not be used to endorse or promote products
21 * derived from this software without specific prior written permission
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 */
35
36 #include <sys/cdefs.h>
37 #include <sys/reboot.h>
38 #include <sys/types.h>
39
40 #include <sys/exec_aout.h>
41
42 #include <amiga/cfdev.h>
43 #include <amiga/memlist.h>
44 #include <include/cpu.h>
45
46 #include <saerrno.h>
47 #include <stand.h>
48
49 #include "libstubs.h"
50 #include "samachdep.h"
51
52 #undef __LDPGSZ
53 #define __LDPGSZ 8192
54 #define __PGSZ 8192
55
56 #define DRACOREVISION (*(u_int8_t *)0x02000009)
57 #define DRACOMMUMARGIN 0x200000
58 #define DRACOZ2OFFSET 0x3000000
59 #define DRACOZ2MAX 0x1000000
60
61 #define EXECMIN 36
62
63 void startit __P((void *, u_long, u_long, void *, u_long, u_long, int, void *,
64 int, int, u_long, u_long, u_long, int));
65 void startit_end __P((void));
66 int get_cpuid __P((u_int32_t *));
67
68 /*
69 * Kernel startup interface version
70 * 1: first version of loadbsd
71 * 2: needs esym location passed in a4
72 * 3: load kernel image into fastmem rather than chipmem
73 * MAX: highest version with backward compatibility.
74 */
75
76 #define KERNEL_STARTUP_VERSION 3
77 #define KERNEL_STARTUP_VERSION_MAX 9
78
79 static long get_number(char **);
80
81 const char version[] = "2.0";
82 char default_command[] = "netbsd -AS";
83
84 int
85 pain()
86 {
87 long int io = 0;
88 char linebuf[128];
89 char *kernel_name = default_command;
90 char *path = default_command;
91 int boothowto = RB_AUTOBOOT;
92 u_int32_t cpuid = 0;
93 int amiga_flags = 0;
94 u_int32_t I_flag = 0;
95 int k_flag = 0;
96 int p_flag = 0;
97 int m_value = 0;
98 int S_flag = 0;
99 int t_flag = 0;
100 long stringsz;
101
102 u_int32_t fmem = 0x0;
103 int fmemsz = 0x0;
104 int cmemsz = 0x0;
105 int eclock = SysBase->EClockFreq;
106 /* int skip_chipmem = 0; */
107
108 void (*start_it)(void *, u_long, u_long, void *, u_long, u_long, int,
109 void *, int, int, u_long, u_long, u_long, int) = startit;
110
111 caddr_t kp;
112 u_int16_t *kvers;
113 struct exec *eh;
114 int textsz, ksize;
115 void *esym = 0;
116 int32_t *nkcd;
117 struct cfdev *cd, *kcd;
118 struct boot_memseg *kmemseg;
119 struct boot_memseg *memseg;
120 struct MemHead *mh;
121 u_int32_t from, size, vfrom, vsize;
122 int contflag, mapped1to1;
123
124 int ncd, nseg;
125 char c;
126
127 extern u_int16_t timelimit;
128
129 extern u_int32_t aio_base;
130
131 /*
132 * we need V36 for: EClock, RDB Bootblocks, CacheClearU
133 */
134
135 if (SysBase->LibNode.Version < EXECMIN) {
136 printf("Exec V%ld, need V%ld\n",
137 (long)SysBase->LibNode.Version, (long)EXECMIN);
138 goto out;
139 }
140
141 printf("\2337mNetBSD/Amiga bootblock %s\2330m\n%s :- ",
142 version, kernel_name);
143
144 timelimit = 3;
145 gets(linebuf);
146
147 if (*linebuf == 'q')
148 return 1;
149
150 if (*linebuf)
151 path = linebuf;
152
153 /*
154 * parse boot command for path name and process any options
155 */
156 while ((c = *path)) {
157 while (c == ' ')
158 c = *++path;
159 if (c == '-') {
160 while ((c = *++path) && c != ' ') {
161 switch (c) {
162 case 'a': /* multi-user state */
163 boothowto &= ~RB_SINGLE;
164 break;
165 case 'b': /* ask for root device */
166 boothowto |= RB_ASKNAME;
167 break;
168 case 'c': /* force machine model */
169 cpuid = get_number(&path) << 16;
170 break;
171 case 'k': /* Reserve first 4M fastmem */
172 k_flag++;
173 break;
174 case 'm': /* Force fastmem size */
175 m_value = get_number(&path) * 1024;
176 break;
177 case 'n': /* non-contiguous memory */
178 amiga_flags |=
179 (get_number(&path) & 3) << 1;
180 break;
181 case 'p': /* Select fastmem by priority */
182 p_flag++;
183 break;
184 case 's': /* single-user state */
185 boothowto |= RB_SINGLE;
186 break;
187 case 't': /* test flag */
188 t_flag = 1;
189 break;
190 case 'A': /* enable AGA modes */
191 amiga_flags |= 1;
192 break;
193 case 'D': /* enter Debugger */
194 boothowto |= RB_KDB;
195 break;
196 case 'I': /* inhibit sync negotiation */
197 I_flag = get_number(&path);
198 break;
199 case 'K': /* remove 1st 4MB fastmem */
200 break;
201 case 'S': /* include debug symbols */
202 S_flag = 1;
203 break;
204 }
205 }
206 } else {
207 kernel_name = path;
208 while ((c = *++path) && c != ' ')
209 ;
210 if (c)
211 *path++ = 0;
212 }
213 }
214 while ((c = *kernel_name) && c == ' ')
215 ++kernel_name;
216 path = kernel_name;
217 while ((c = *path) && c != ' ')
218 ++path;
219 if (c)
220 *path = 0;
221
222 if (get_cpuid(&cpuid))
223 goto out;
224
225 ExpansionBase = OpenLibrary("expansion.library", 0);
226 if (!ExpansionBase) {
227 printf("can't open %s\n", "expansion.library");
228 return 1;
229 }
230
231 for (ncd=0, cd=0; (cd = FindConfigDev(cd, -1, -1)); ncd++)
232 /* nothing */;
233
234 /* find memory list */
235
236 memseg = (struct boot_memseg *)alloc(16*sizeof(struct boot_memseg));
237
238 /* Forbid(); */
239
240 nseg = 0;
241 mh = SysBase->MemLst;
242 vfrom = mh->Lower & -__PGSZ;
243 vsize = (mh->Upper & -__PGSZ) - vfrom;
244 contflag = mapped1to1 = 0;
245
246 do {
247 size = vsize;
248
249 if (SysBase->LibNode.Version > 36) {
250 from = CachePreDMA(vfrom, &size, contflag);
251 contflag = DMAF_Continue;
252 mapped1to1 = (from == vfrom);
253 vsize -= size;
254 vfrom += size;
255 } else {
256 from = vfrom;
257 mapped1to1 = 1;
258 vsize = 0;
259 }
260
261 #ifdef DEBUG_MEMORY_LIST
262 printf("%lx %lx %lx %ld/%lx %lx\n",
263 (long)from, (long)size,
264 (long)mh->Attribs, (long)mh->Pri,
265 (long)vfrom, (long)vsize);
266 #endif
267 /* Insert The Evergrowing Kludge List Here: */
268
269 /* a) dont load kernel over DraCo MMU table */
270
271 if (((cpuid >> 24) == 0x7D) &&
272 ((from & -DRACOMMUMARGIN) == 0x40000000) &&
273 (size >= DRACOMMUMARGIN)) {
274
275 memseg[nseg].ms_start = from & -DRACOMMUMARGIN;
276 memseg[nseg].ms_size = DRACOMMUMARGIN;
277 memseg[nseg].ms_attrib = mh->Attribs;
278 memseg[nseg].ms_pri = mh->Pri;
279
280 size -= DRACOMMUMARGIN - (from & (DRACOMMUMARGIN - 1));
281 from += DRACOMMUMARGIN - (from & (DRACOMMUMARGIN - 1));
282 ++nseg;
283 }
284
285 if ((mh->Attribs & (MEMF_CHIP|MEMF_FAST)) == MEMF_CHIP) {
286 size += from;
287 cmemsz = size;;
288 from = 0;
289 } else if ((fmemsz < size) && mapped1to1) {
290 fmem = from;
291 fmemsz = size;
292 }
293
294 memseg[nseg].ms_start = from;
295 memseg[nseg].ms_size = size;
296 memseg[nseg].ms_attrib = mh->Attribs;
297 memseg[nseg].ms_pri = mh->Pri;
298
299 if (vsize == 0) {
300 mh = mh->next;
301 contflag = 0;
302 if (mh->next) {
303 vfrom = mh->Lower & -__PGSZ;
304 vsize = (mh->Upper & -__PGSZ) - vfrom;
305 }
306 }
307 } while ((++nseg <= 16) && vsize);
308
309 /* Permit(); */
310
311 printf("Loading %s: ", kernel_name);
312 io = open(kernel_name, 0);
313 if (io < 0)
314 goto err;
315
316 eh = alloc(sizeof(*eh));
317 if (!eh) {
318 errno = ENOMEM;
319 goto err;
320 }
321 if (read(io, eh, sizeof(*eh)) != sizeof(*eh)) {
322 errno = ENOEXEC;
323 goto err;
324 }
325
326 if ((N_GETMAGIC(*eh) != NMAGIC) || (N_GETMID(*eh) != MID_M68K)) {
327 errno = ENOEXEC;
328 goto err;
329 }
330
331 textsz = (eh->a_text + __LDPGSZ - 1) & (-__LDPGSZ);
332 esym = 0;
333
334 ksize = textsz + eh->a_data + eh->a_bss
335 + sizeof(*nkcd) + ncd*sizeof(*cd)
336 + sizeof(*nkcd) + nseg * sizeof(struct boot_memseg);
337
338 if (S_flag && eh->a_syms) {
339 if (lseek(io, eh->a_text+ eh->a_data+ eh->a_syms, SEEK_CUR)
340 <= 0
341 || read(io, &stringsz, 4) != 4
342 || lseek(io, sizeof(*eh), SEEK_SET) < 0)
343 goto err;
344 ksize += eh->a_syms + 4 + ((stringsz + 3) & ~3);
345 }
346
347 kp = alloc(ksize + 256 + ((u_char *)startit_end - (u_char *)startit));
348 if (kp == 0) {
349 errno = ENOMEM;
350 goto err;
351 }
352
353 printf("%ld", eh->a_text);
354 if (read(io, kp, eh->a_text) != eh->a_text)
355 goto err;
356
357 printf("+%ld", eh->a_data);
358 if (read(io, kp + textsz, eh->a_data) != eh->a_data)
359 goto err;
360
361 printf("+%ld", eh->a_bss);
362
363 kvers = (u_short *)(kp + eh->a_entry - 2);
364
365 if (*kvers > KERNEL_STARTUP_VERSION_MAX && *kvers != 0x4e73) {
366 printf("\nnewer bootblock required: %ld\n", (long)*kvers);
367 goto freeall;
368 }
369 if (*kvers < KERNEL_STARTUP_VERSION || *kvers == 0x4e73) {
370 printf("\nkernel too old for bootblock\n");
371 goto freeall;
372 }
373 #if 0
374 if (*kvers > KERNEL_STARTUP_VERSION)
375 printf("\nKernel V%ld newer than bootblock V%ld\n",
376 (long)*kvers, (long)KERNEL_STARTUP_VERSION);
377 #endif
378 nkcd = (int *)(kp + textsz + eh->a_data + eh->a_bss);
379 if (*kvers != 0x4e73 && *kvers > 1 && S_flag && eh->a_syms) {
380 *nkcd++ = eh->a_syms;
381 printf("+[%ld", eh->a_syms);
382 if (read(io, (char *)nkcd, eh->a_syms) != eh->a_syms)
383 goto err;
384 nkcd = (int *)((char *)nkcd + eh->a_syms);
385 printf("+%ld]", stringsz);
386 if (read(io, (char *)nkcd, stringsz) != stringsz)
387 goto err;
388 nkcd = (int*)((char *)nkcd + ((stringsz + 3) & ~3));
389 esym = (char *)(textsz + eh->a_data + eh->a_bss
390 + eh->a_syms + 4 + ((stringsz + 3) & ~3));
391 }
392 putchar('\n');
393
394 *nkcd = ncd;
395 kcd = (struct cfdev *)(nkcd + 1);
396
397 while ((cd = FindConfigDev(cd, -1, -1))) {
398 *kcd = *cd;
399 if (((cpuid >> 24) == 0x7D) &&
400 ((u_long)kcd->addr < 0x1000000)) {
401 kcd->addr += 0x3000000;
402 }
403 ++kcd;
404 }
405
406 nkcd = (u_int32_t *)kcd;
407 *nkcd = nseg;
408
409 kmemseg = (struct boot_memseg *)(nkcd + 1);
410
411 while (nseg-- > 0)
412 *kmemseg++ = *memseg++;
413
414 /*
415 * Kernel supports direct load to fastmem, and the -Z
416 * option was not specified. Copy startup code to end
417 * of kernel image and set start_it.
418 */
419 #if 0
420 if ((u_int32_t)kp < fmem) {
421 errno = EFBIG;
422 goto err;
423 }
424 #endif
425 memcpy(kp + ksize + 256, (char *)startit,
426 (char *)startit_end - (char *)startit);
427 CacheClearU();
428 (caddr_t)start_it = kp + ksize + 256;
429 printf("*** Loading from %08lx to Fastmem %08lx ***\n",
430 (u_long)kp, (u_long)fmem);
431 /* sleep(2); */
432
433 #if 0
434 printf("would start(kp=0x%lx, ksize=%ld, entry=0x%lx,\n"
435 "fmem=0x%lx, fmemsz=%ld, cmemsz=%ld\n"
436 "boothow=0x%lx, esym=0x%lx, cpuid=0x%lx, eclock=%ld\n"
437 "amigaflags=0x%lx, I_flags=0x%lx, ok?\n",
438 (u_long)kp, (u_long)ksize, eh->a_entry,
439 (u_long)fmem, (u_long)fmemsz, (u_long)cmemsz,
440 (u_long)boothowto, (u_long)esym, (u_long)cpuid, (u_long)eclock,
441 (u_long)amiga_flags, (u_long)I_flag);
442 #endif
443 #ifdef DEBUG_MEMORY_LIST
444 timelimit = 0;
445 #else
446 timelimit = 2;
447 #endif
448 (void)getchar();
449
450 start_it(kp, ksize, eh->a_entry, (void *)fmem, fmemsz, cmemsz,
451 boothowto, esym, cpuid, eclock, amiga_flags, I_flag,
452 aio_base >> 9, 1);
453 /*NOTREACHED*/
454
455 freeall:
456 free(kp, ksize);
457 free(eh, sizeof(*eh));
458 err:
459 printf("\nError %ld\n", (long)errno);
460 close(io);
461 out:
462 timelimit = 10;
463 (void)getchar();
464 return 1;
465 }
466
467 static
468 long get_number(ptr)
469 char **ptr;
470 {
471 long value = 0;
472 int base = 10;
473 char *p = *ptr;
474 char c;
475 char sign = 0;
476
477 c = *++p;
478 while (c == ' ')
479 c = *++p;
480 if (c == '-') {
481 sign = -1;
482 c = *++p;
483 }
484 if (c == '$') {
485 base = 16;
486 c = *++p;
487 } else if (c == '0') {
488 c = *++p;
489 if ((c & 0xdf) == 'X') {
490 base = 16;
491 c = *++p;
492 }
493 }
494 while (c) {
495 if (c >= '0' && c <= '9')
496 c -= '0';
497 else {
498 c = (c & 0xdf) - 'A' + 10;
499 if (base != 16 || c < 10 || c > 15)
500 break;
501 }
502 value = value * base + c;
503 c = *++p;
504 }
505 *ptr = p - 1;
506 #ifdef TEST
507 fprintf(stderr, "get_number: got %c0x%x",
508 sign ? '-' : '+', value);
509 #endif
510 return (sign ? -value : value);
511 }
512
513 /*
514 * Try to determine the machine ID by searching the resident module list
515 * for modules only present on specific machines. (Thanks, Bill!)
516 */
517
518 int
519 get_cpuid(cpuid)
520 u_int32_t *cpuid;
521 {
522 *cpuid |= SysBase->AttnFlags; /* get FPU and CPU flags */
523 if (*cpuid & 0xffff0000) {
524 if ((*cpuid >> 24) == 0x7D)
525 return 0;
526
527 switch (*cpuid >> 16) {
528 case 500:
529 case 600:
530 case 1000:
531 case 1200:
532 case 2000:
533 case 3000:
534 case 4000:
535 return 0;
536 default:
537 printf("Amiga %ld ???\n",
538 (long)(*cpuid >> 16));
539 return(1);
540 }
541 }
542 if (FindResident("A4000 Bonus") || FindResident("A4000 bonus")
543 || FindResident("A1000 Bonus"))
544 *cpuid |= 4000 << 16;
545 else if (FindResident("A3000 Bonus") || FindResident("A3000 bonus")
546 || (SysBase->LibNode.Version == 36))
547 *cpuid |= 3000 << 16;
548 else if (OpenResource("card.resource")) {
549 /* Test for AGA? */
550 *cpuid |= 1200 << 16;
551 } else if (OpenResource("draco.resource")) {
552 *cpuid |= (32000 | DRACOREVISION) << 16;
553 }
554 /*
555 * Nothing found, it's probably an A2000 or A500
556 */
557 if ((*cpuid >> 16) == 0)
558 *cpuid |= 2000 << 16;
559
560 return 0;
561 }
562