main.c revision 1.4 1 /*
2 * $NetBSD: main.c,v 1.4 1997/03/24 18:54:26 mycroft 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 Z_flag = 0;
98 int m_value = 0;
99 int S_flag = 0;
100 int t_flag = 0;
101 long stringsz;
102
103 u_int32_t fmem = 0x0;
104 int fmemsz = 0x0;
105 int cmemsz = 0x0;
106 int eclock = SysBase->EClockFreq;
107 /* int skip_chipmem = 0; */
108
109 void (*start_it)(void *, u_long, u_long, void *, u_long, u_long, int,
110 void *, int, int, u_long, u_long, u_long, int) = startit;
111
112 caddr_t kp;
113 u_int16_t *kvers;
114 struct exec *eh;
115 int textsz, ksize;
116 void *esym = 0;
117 int32_t *nkcd;
118 struct cfdev *cd, *kcd;
119 struct boot_memseg *kmemseg;
120 struct boot_memseg *memseg;
121 struct MemHead *mh;
122 u_int32_t from, size, vfrom, vsize;
123 int contflag, mapped1to1;
124
125 int ncd, nseg;
126 char c;
127
128 extern u_int16_t timelimit;
129
130 extern u_int32_t aio_base;
131
132 /*
133 * we need V36 for: EClock, RDB Bootblocks, CacheClearU
134 */
135
136 if (SysBase->LibNode.Version < EXECMIN) {
137 printf("Exec V%ld, need V%ld\n",
138 (long)SysBase->LibNode.Version, (long)EXECMIN);
139 goto out;
140 }
141
142 printf("\2337mNetBSD/Amiga bootblock %s\2330m\n%s :- ",
143 version, kernel_name);
144
145 timelimit = 3;
146 gets(linebuf);
147
148 if (*linebuf == 'q')
149 return 1;
150
151 if (*linebuf)
152 path = linebuf;
153
154 /*
155 * parse boot command for path name and process any options
156 */
157 while ((c = *path)) {
158 while (c == ' ')
159 c = *++path;
160 if (c == '-') {
161 while ((c = *++path) && c != ' ') {
162 switch (c) {
163 case 'a': /* multi-user state */
164 boothowto &= ~RB_SINGLE;
165 break;
166 case 'b': /* ask for root device */
167 boothowto |= RB_ASKNAME;
168 break;
169 case 'c': /* force machine model */
170 cpuid = get_number(&path) << 16;
171 break;
172 case 'k': /* Reserve first 4M fastmem */
173 k_flag++;
174 break;
175 case 'm': /* Force fastmem size */
176 m_value = get_number(&path) * 1024;
177 break;
178 case 'n': /* non-contiguous memory */
179 amiga_flags |=
180 (get_number(&path) & 3) << 1;
181 break;
182 case 'p': /* Select fastmem by priority */
183 p_flag++;
184 break;
185 case 's': /* single-user state */
186 boothowto |= RB_SINGLE;
187 break;
188 case 't': /* test flag */
189 t_flag = 1;
190 break;
191 case 'A': /* enable AGA modes */
192 amiga_flags |= 1;
193 break;
194 case 'D': /* enter Debugger */
195 boothowto |= RB_KDB;
196 break;
197 case 'I': /* inhibit sync negotiation */
198 I_flag = get_number(&path);
199 break;
200 case 'K': /* remove 1st 4MB fastmem */
201 break;
202 case 'S': /* include debug symbols */
203 S_flag = 1;
204 break;
205 case 'Z': /* force chip memory load */
206 Z_flag = 1;
207 break;
208 }
209 }
210 } else {
211 kernel_name = path;
212 while ((c = *++path) && c != ' ')
213 ;
214 if (c)
215 *path++ = 0;
216 }
217 }
218 while ((c = *kernel_name) && c == ' ')
219 ++kernel_name;
220 path = kernel_name;
221 while ((c = *path) && c != ' ')
222 ++path;
223 if (c)
224 *path = 0;
225
226 if (get_cpuid(&cpuid))
227 goto out;
228
229 ExpansionBase = OpenLibrary("expansion.library", 0);
230 if (!ExpansionBase) {
231 printf("can't open %s\n", "expansion.library");
232 return 1;
233 }
234
235 for (ncd=0, cd=0; (cd = FindConfigDev(cd, -1, -1)); ncd++)
236 /* nothing */;
237
238 /* find memory list */
239
240 memseg = (struct boot_memseg *)alloc(16*sizeof(struct boot_memseg));
241
242 /* Forbid(); */
243
244 nseg = 0;
245 mh = SysBase->MemLst;
246 vfrom = mh->Lower & -__PGSZ;
247 vsize = (mh->Upper - vfrom) & -__PGSZ;
248 contflag = mapped1to1 = 0;
249
250 do {
251 size = vsize;
252
253 if (SysBase->LibNode.Version > 36) {
254 from = CachePreDMA(vfrom, &size, contflag);
255 contflag = DMAF_Continue;
256 mapped1to1 = (from == vfrom);
257 vsize -= size;
258 vfrom += size;
259 } else {
260 from = vfrom;
261 vsize = 0;
262 }
263
264 #if DEBUG_MEMORY_LIST
265 printf("%lx %lx %lx %ld/%lx %lx\n",
266 (long)from, (long)size,
267 (long)mh->Attribs, (long)mh->Pri,
268 (long)vfrom, (long)vsize);
269 #endif
270 /* Insert The Evergrowing Kludge List Here: */
271
272 /* a) dont load kernel over DraCo MMU table */
273
274 if (((cpuid >> 24) == 0x7D) &&
275 ((from & -DRACOMMUMARGIN) == 0x40000000) &&
276 (size >= DRACOMMUMARGIN)) {
277
278 memseg[nseg].ms_start = from & -DRACOMMUMARGIN;
279 memseg[nseg].ms_size = DRACOMMUMARGIN;
280 memseg[nseg].ms_attrib = mh->Attribs;
281 memseg[nseg].ms_pri = mh->Pri;
282
283 size -= DRACOMMUMARGIN - (from & (DRACOMMUMARGIN - 1));
284 from += DRACOMMUMARGIN - (from & (DRACOMMUMARGIN - 1));
285 ++nseg;
286 }
287
288 if ((mh->Attribs & (MEMF_CHIP|MEMF_FAST)) == MEMF_CHIP) {
289 size += from;
290 cmemsz = size;;
291 from = 0;
292 } else if ((fmemsz < size) && mapped1to1) {
293 fmem = from;
294 fmemsz = size;
295 }
296
297 memseg[nseg].ms_start = from;
298 memseg[nseg].ms_size = size;
299 memseg[nseg].ms_attrib = mh->Attribs;
300 memseg[nseg].ms_pri = mh->Pri;
301
302 if (vsize == 0) {
303 mh = mh->next;
304 contflag = 0;
305 if (mh->next) {
306 vfrom = mh->Lower & -__PGSZ;
307 vsize = (mh->Upper & -__PGSZ) - vfrom;
308 }
309 }
310 } while ((++nseg <= 16) && vsize);
311
312 /* Permit(); */
313
314 printf("Loading %s: ", kernel_name);
315 io = open(kernel_name, 0);
316 if (io < 0)
317 goto err;
318
319 eh = alloc(sizeof(*eh));
320 if (!eh) {
321 errno = ENOMEM;
322 goto err;
323 }
324 if (read(io, eh, sizeof(*eh)) != sizeof(*eh)) {
325 errno = ENOEXEC;
326 goto err;
327 }
328
329 if ((N_GETMAGIC(*eh) != NMAGIC) || (N_GETMID(*eh) != MID_M68K)) {
330 errno = ENOEXEC;
331 goto err;
332 }
333
334 textsz = (eh->a_text + __LDPGSZ - 1) & (-__LDPGSZ);
335 esym = 0;
336
337 ksize = textsz + eh->a_data + eh->a_bss
338 + sizeof(*nkcd) + ncd*sizeof(*cd)
339 + sizeof(*nkcd) + nseg * sizeof(struct boot_memseg);
340
341 if (S_flag && eh->a_syms) {
342 if (lseek(io, eh->a_text+ eh->a_data+ eh->a_syms, SEEK_CUR)
343 <= 0
344 || read(io, &stringsz, 4) != 4
345 || lseek(io, sizeof(*eh), SEEK_SET) < 0)
346 goto err;
347 ksize += eh->a_syms + 4 + ((stringsz + 3) & ~3);
348 }
349
350 kp = alloc(ksize + 256 + ((u_char *)startit_end - (u_char *)startit));
351 if (kp == 0) {
352 errno = ENOMEM;
353 goto err;
354 }
355
356 printf("%ld", eh->a_text);
357 if (read(io, kp, eh->a_text) != eh->a_text)
358 goto err;
359
360 printf("+%ld", eh->a_data);
361 if (read(io, kp + textsz, eh->a_data) != eh->a_data)
362 goto err;
363
364 printf("+%ld", eh->a_bss);
365
366 kvers = (u_short *)(kp + eh->a_entry - 2);
367
368 if (*kvers > KERNEL_STARTUP_VERSION_MAX && *kvers != 0x4e73) {
369 printf("\nnewer bootblock required: %ld\n", (long)*kvers);
370 goto freeall;
371 }
372 #if 0
373 if (*kvers > KERNEL_STARTUP_VERSION)
374 printf("\nKernel V%ld newer than bootblock V%ld\n",
375 (long)*kvers, (long)KERNEL_STARTUP_VERSION);
376 #endif
377 nkcd = (int *)(kp + textsz + eh->a_data + eh->a_bss);
378 if (*kvers != 0x4e73 && *kvers > 1 && S_flag && eh->a_syms) {
379 *nkcd++ = eh->a_syms;
380 printf("+[%ld", eh->a_syms);
381 if (read(io, (char *)nkcd, eh->a_syms) != eh->a_syms)
382 goto err;
383 nkcd = (int *)((char *)nkcd + eh->a_syms);
384 printf("+%ld]", stringsz);
385 if (read(io, (char *)nkcd, stringsz) != stringsz)
386 goto err;
387 nkcd = (int*)((char *)nkcd + ((stringsz + 3) & ~3));
388 esym = (char *)(textsz + eh->a_data + eh->a_bss
389 + eh->a_syms + 4 + ((stringsz + 3) & ~3));
390 }
391 putchar('\n');
392
393 *nkcd = ncd;
394 kcd = (struct cfdev *)(nkcd + 1);
395
396 while ((cd = FindConfigDev(cd, -1, -1))) {
397 *kcd = *cd;
398 if (((cpuid >> 24) == 0x7D) &&
399 ((u_long)kcd->addr < 0x1000000)) {
400 kcd->addr += 0x3000000;
401 }
402 ++kcd;
403 }
404
405 nkcd = (u_int32_t *)kcd;
406 *nkcd = nseg;
407
408 kmemseg = (struct boot_memseg *)(nkcd + 1);
409
410 while (nseg-- > 0)
411 *kmemseg++ = *memseg++;
412
413 if (*kvers > 2 && Z_flag == 0) {
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 ((u_int32_t)kp < fmem) {
420 errno = EFBIG;
421 goto err;
422 }
423 memcpy(kp + ksize + 256, (char *)startit,
424 (char *)startit_end - (char *)startit);
425 CacheClearU();
426 (caddr_t)start_it = kp + ksize + 256;
427 printf("*** Loading from %08lx to Fastmem %08lx ***\n",
428 (u_long)kp, (u_long)fmem);
429 /* sleep(2); */
430 } else {
431 /*
432 * Either the kernel doesn't suppport loading directly to
433 * fastmem or the -Z flag was given. Verify kernel image
434 * fits into chipmem.
435 */
436 if (ksize >= cmemsz) {
437 printf("Kernel size %d exceeds Chip Memory of %d\n",
438 ksize, cmemsz);
439 return 20;
440 }
441 Z_flag = 1;
442 printf("*** Loading from %08lx to Chipmem ***\n", (u_long)kp);
443 }
444
445 #if 0
446 printf("would start(kp=0x%lx, ksize=%ld, entry=0x%lx,\n"
447 "fmem=0x%lx, fmemsz=%ld, cmemsz=%ld\n"
448 "boothow=0x%lx, esym=0x%lx, cpuid=0x%lx, eclock=%ld\n"
449 "amigaflags=0x%lx, I_flags=0x%lx, Zflag=%ld, ok?\n",
450 (u_long)kp, (u_long)ksize, eh->a_entry,
451 (u_long)fmem, (u_long)fmemsz, (u_long)cmemsz,
452 (u_long)boothowto, (u_long)esym, (u_long)cpuid, (u_long)eclock,
453 (u_long)amiga_flags, (u_long)I_flag, (u_long)(Z_flag == 0));
454 #endif
455 timelimit = 2;
456 (void)getchar();
457
458 start_it(kp, ksize, eh->a_entry, (void *)fmem, fmemsz, cmemsz,
459 boothowto, esym, cpuid, eclock, amiga_flags, I_flag,
460 aio_base >> 9, Z_flag == 0);
461 /*NOTREACHED*/
462
463 freeall:
464 free(kp, ksize);
465 free(eh, sizeof(*eh));
466 err:
467 printf("\nError %ld\n", (long)errno);
468 close(io);
469 out:
470 timelimit = 10;
471 (void)getchar();
472 return 1;
473 }
474
475 static
476 long get_number(ptr)
477 char **ptr;
478 {
479 long value = 0;
480 int base = 10;
481 char *p = *ptr;
482 char c;
483 char sign = 0;
484
485 c = *++p;
486 while (c == ' ')
487 c = *++p;
488 if (c == '-') {
489 sign = -1;
490 c = *++p;
491 }
492 if (c == '$') {
493 base = 16;
494 c = *++p;
495 } else if (c == '0') {
496 c = *++p;
497 if ((c & 0xdf) == 'X') {
498 base = 16;
499 c = *++p;
500 }
501 }
502 while (c) {
503 if (c >= '0' && c <= '9')
504 c -= '0';
505 else {
506 c = (c & 0xdf) - 'A' + 10;
507 if (base != 16 || c < 10 || c > 15)
508 break;
509 }
510 value = value * base + c;
511 c = *++p;
512 }
513 *ptr = p - 1;
514 #ifdef TEST
515 fprintf(stderr, "get_number: got %c0x%x",
516 sign ? '-' : '+', value);
517 #endif
518 return (sign ? -value : value);
519 }
520
521 /*
522 * Try to determine the machine ID by searching the resident module list
523 * for modules only present on specific machines. (Thanks, Bill!)
524 */
525
526 int
527 get_cpuid(cpuid)
528 u_int32_t *cpuid;
529 {
530 *cpuid |= SysBase->AttnFlags; /* get FPU and CPU flags */
531 if (*cpuid & 0xffff0000) {
532 if ((*cpuid >> 24) == 0x7D)
533 return 0;
534
535 switch (*cpuid >> 16) {
536 case 500:
537 case 600:
538 case 1000:
539 case 1200:
540 case 2000:
541 case 3000:
542 case 4000:
543 return 0;
544 default:
545 printf("Amiga %ld ???\n",
546 (long)(*cpuid >> 16));
547 return(1);
548 }
549 }
550 if (FindResident("A4000 Bonus") || FindResident("A4000 bonus")
551 || FindResident("A1000 Bonus"))
552 *cpuid |= 4000 << 16;
553 else if (FindResident("A3000 Bonus") || FindResident("A3000 bonus")
554 || (SysBase->LibNode.Version == 36))
555 *cpuid |= 3000 << 16;
556 else if (OpenResource("card.resource")) {
557 /* Test for AGA? */
558 *cpuid |= 1200 << 16;
559 } else if (OpenResource("draco.resource")) {
560 *cpuid |= (32000 | DRACOREVISION) << 16;
561 }
562 /*
563 * Nothing found, it's probably an A2000 or A500
564 */
565 if ((*cpuid >> 16) == 0)
566 *cpuid |= 2000 << 16;
567
568 return 0;
569 }
570