main.c revision 1.3 1 /*
2 * $NetBSD: main.c,v 1.3 1997/02/01 01:46:26 mhitch 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 #define __LDPGSZ 8192
53 #define __PGSZ 8192
54
55 #define DRACOREVISION (*(u_int8_t *)0x02000009)
56 #define DRACOMMUMARGIN 0x200000
57 #define DRACOZ2OFFSET 0x3000000
58 #define DRACOZ2MAX 0x1000000
59
60 #define EXECMIN 36
61
62 void startit __P((void *, u_long, u_long, void *, u_long, u_long, int, void *,
63 int, int, u_long, u_long, u_long, int));
64 void startit_end __P((void));
65 int get_cpuid __P((u_int32_t *));
66
67 /*
68 * Kernel startup interface version
69 * 1: first version of loadbsd
70 * 2: needs esym location passed in a4
71 * 3: load kernel image into fastmem rather than chipmem
72 * MAX: highest version with backward compatibility.
73 */
74
75 #define KERNEL_STARTUP_VERSION 3
76 #define KERNEL_STARTUP_VERSION_MAX 9
77
78 static long get_number(char **);
79
80 const char version[] = "2.0";
81 char default_command[] = "netbsd -AS";
82
83 int
84 pain()
85 {
86 long int io = 0;
87 char linebuf[128];
88 char *kernel_name = default_command;
89 char *path = default_command;
90 int boothowto = RB_AUTOBOOT;
91 u_int32_t cpuid = 0;
92 int amiga_flags = 0;
93 u_int32_t I_flag = 0;
94 int k_flag = 0;
95 int p_flag = 0;
96 int Z_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 case 'Z': /* force chip memory load */
205 Z_flag = 1;
206 break;
207 }
208 }
209 } else {
210 kernel_name = path;
211 while ((c = *++path) && c != ' ')
212 ;
213 if (c)
214 *path++ = 0;
215 }
216 }
217 while ((c = *kernel_name) && c == ' ')
218 ++kernel_name;
219 path = kernel_name;
220 while ((c = *path) && c != ' ')
221 ++path;
222 if (c)
223 *path = 0;
224
225 if (get_cpuid(&cpuid))
226 goto out;
227
228 ExpansionBase = OpenLibrary("expansion.library", 0);
229 if (!ExpansionBase) {
230 printf("can't open %s\n", "expansion.library");
231 return 1;
232 }
233
234 for (ncd=0, cd=0; (cd = FindConfigDev(cd, -1, -1)); ncd++)
235 /* nothing */;
236
237 /* find memory list */
238
239 memseg = (struct boot_memseg *)alloc(16*sizeof(struct boot_memseg));
240
241 /* Forbid(); */
242
243 nseg = 0;
244 mh = SysBase->MemLst;
245 vfrom = mh->Lower & -__PGSZ;
246 vsize = (mh->Upper - vfrom) & -__PGSZ;
247 contflag = mapped1to1 = 0;
248
249 do {
250 size = vsize;
251
252 if (SysBase->LibNode.Version > 36) {
253 from = CachePreDMA(vfrom, &size, contflag);
254 contflag = DMAF_Continue;
255 mapped1to1 = (from == vfrom);
256 vsize -= size;
257 vfrom += size;
258 } else {
259 from = vfrom;
260 vsize = 0;
261 }
262
263 #if DEBUG_MEMORY_LIST
264 printf("%lx %lx %lx %ld/%lx %lx\n",
265 (long)from, (long)size,
266 (long)mh->Attribs, (long)mh->Pri,
267 (long)vfrom, (long)vsize);
268 #endif
269 /* Insert The Evergrowing Kludge List Here: */
270
271 /* a) dont load kernel over DraCo MMU table */
272
273 if (((cpuid >> 24) == 0x7D) &&
274 ((from & -DRACOMMUMARGIN) == 0x40000000) &&
275 (size >= DRACOMMUMARGIN)) {
276
277 memseg[nseg].ms_start = from & -DRACOMMUMARGIN;
278 memseg[nseg].ms_size = DRACOMMUMARGIN;
279 memseg[nseg].ms_attrib = mh->Attribs;
280 memseg[nseg].ms_pri = mh->Pri;
281
282 size -= DRACOMMUMARGIN - (from & (DRACOMMUMARGIN - 1));
283 from += DRACOMMUMARGIN - (from & (DRACOMMUMARGIN - 1));
284 ++nseg;
285 }
286
287 if ((mh->Attribs & (MEMF_CHIP|MEMF_FAST)) == MEMF_CHIP) {
288 size += from;
289 cmemsz = size;;
290 from = 0;
291 } else if ((fmemsz < size) && mapped1to1) {
292 fmem = from;
293 fmemsz = size;
294 }
295
296 memseg[nseg].ms_start = from;
297 memseg[nseg].ms_size = size;
298 memseg[nseg].ms_attrib = mh->Attribs;
299 memseg[nseg].ms_pri = mh->Pri;
300
301 if (vsize == 0) {
302 mh = mh->next;
303 contflag = 0;
304 if (mh->next) {
305 vfrom = mh->Lower & -__PGSZ;
306 vsize = (mh->Upper & -__PGSZ) - vfrom;
307 }
308 }
309 } while ((++nseg <= 16) && vsize);
310
311 /* Permit(); */
312
313 printf("Loading %s: ", kernel_name);
314 io = open(kernel_name, 0);
315 if (io < 0)
316 goto err;
317
318 eh = alloc(sizeof(*eh));
319 if (!eh) {
320 errno = ENOMEM;
321 goto err;
322 }
323 if (read(io, eh, sizeof(*eh)) != sizeof(*eh)) {
324 errno = ENOEXEC;
325 goto err;
326 }
327
328 if ((N_GETMAGIC(*eh) != NMAGIC) || (N_GETMID(*eh) != MID_M68K)) {
329 errno = ENOEXEC;
330 goto err;
331 }
332
333 textsz = (eh->a_text + __LDPGSZ - 1) & (-__LDPGSZ);
334 esym = 0;
335
336 ksize = textsz + eh->a_data + eh->a_bss
337 + sizeof(*nkcd) + ncd*sizeof(*cd)
338 + sizeof(*nkcd) + nseg * sizeof(struct boot_memseg);
339
340 if (S_flag && eh->a_syms) {
341 if (lseek(io, eh->a_text+ eh->a_data+ eh->a_syms, SEEK_CUR)
342 <= 0
343 || read(io, &stringsz, 4) != 4
344 || lseek(io, sizeof(*eh), SEEK_SET) < 0)
345 goto err;
346 ksize += eh->a_syms + 4 + ((stringsz + 3) & ~3);
347 }
348
349 kp = alloc(ksize + 256 + ((u_char *)startit_end - (u_char *)startit));
350 if (kp == 0) {
351 errno = ENOMEM;
352 goto err;
353 }
354
355 printf("%ld", eh->a_text);
356 if (read(io, kp, eh->a_text) != eh->a_text)
357 goto err;
358
359 printf("+%ld", eh->a_data);
360 if (read(io, kp + textsz, eh->a_data) != eh->a_data)
361 goto err;
362
363 printf("+%ld", eh->a_bss);
364
365 kvers = (u_short *)(kp + eh->a_entry - 2);
366
367 if (*kvers > KERNEL_STARTUP_VERSION_MAX && *kvers != 0x4e73) {
368 printf("\nnewer bootblock required: %ld\n", (long)*kvers);
369 goto freeall;
370 }
371 #if 0
372 if (*kvers > KERNEL_STARTUP_VERSION)
373 printf("\nKernel V%ld newer than bootblock V%ld\n",
374 (long)*kvers, (long)KERNEL_STARTUP_VERSION);
375 #endif
376 nkcd = (int *)(kp + textsz + eh->a_data + eh->a_bss);
377 if (*kvers != 0x4e73 && *kvers > 1 && S_flag && eh->a_syms) {
378 *nkcd++ = eh->a_syms;
379 printf("+[%ld", eh->a_syms);
380 if (read(io, (char *)nkcd, eh->a_syms) != eh->a_syms)
381 goto err;
382 nkcd = (int *)((char *)nkcd + eh->a_syms);
383 printf("+%ld]", stringsz);
384 if (read(io, (char *)nkcd, stringsz) != stringsz)
385 goto err;
386 nkcd = (int*)((char *)nkcd + ((stringsz + 3) & ~3));
387 esym = (char *)(textsz + eh->a_data + eh->a_bss
388 + eh->a_syms + 4 + ((stringsz + 3) & ~3));
389 }
390 putchar('\n');
391
392 *nkcd = ncd;
393 kcd = (struct cfdev *)(nkcd + 1);
394
395 while ((cd = FindConfigDev(cd, -1, -1))) {
396 *kcd = *cd;
397 if (((cpuid >> 24) == 0x7D) &&
398 ((u_long)kcd->addr < 0x1000000)) {
399 kcd->addr += 0x3000000;
400 }
401 ++kcd;
402 }
403
404 nkcd = (u_int32_t *)kcd;
405 *nkcd = nseg;
406
407 kmemseg = (struct boot_memseg *)(nkcd + 1);
408
409 while (nseg-- > 0)
410 *kmemseg++ = *memseg++;
411
412 if (*kvers > 2 && Z_flag == 0) {
413 /*
414 * Kernel supports direct load to fastmem, and the -Z
415 * option was not specified. Copy startup code to end
416 * of kernel image and set start_it.
417 */
418 if ((u_int32_t)kp < fmem) {
419 errno = EFBIG;
420 goto err;
421 }
422 memcpy(kp + ksize + 256, (char *)startit,
423 (char *)startit_end - (char *)startit);
424 CacheClearU();
425 (caddr_t)start_it = kp + ksize + 256;
426 printf("*** Loading from %08lx to Fastmem %08lx ***\n",
427 (u_long)kp, (u_long)fmem);
428 /* sleep(2); */
429 } else {
430 /*
431 * Either the kernel doesn't suppport loading directly to
432 * fastmem or the -Z flag was given. Verify kernel image
433 * fits into chipmem.
434 */
435 if (ksize >= cmemsz) {
436 printf("Kernel size %d exceeds Chip Memory of %d\n",
437 ksize, cmemsz);
438 return 20;
439 }
440 Z_flag = 1;
441 printf("*** Loading from %08lx to Chipmem ***\n", (u_long)kp);
442 }
443
444 #if 0
445 printf("would start(kp=0x%lx, ksize=%ld, entry=0x%lx,\n"
446 "fmem=0x%lx, fmemsz=%ld, cmemsz=%ld\n"
447 "boothow=0x%lx, esym=0x%lx, cpuid=0x%lx, eclock=%ld\n"
448 "amigaflags=0x%lx, I_flags=0x%lx, Zflag=%ld, ok?\n",
449 (u_long)kp, (u_long)ksize, eh->a_entry,
450 (u_long)fmem, (u_long)fmemsz, (u_long)cmemsz,
451 (u_long)boothowto, (u_long)esym, (u_long)cpuid, (u_long)eclock,
452 (u_long)amiga_flags, (u_long)I_flag, (u_long)(Z_flag == 0));
453 #endif
454 timelimit = 2;
455 (void)getchar();
456
457 start_it(kp, ksize, eh->a_entry, (void *)fmem, fmemsz, cmemsz,
458 boothowto, esym, cpuid, eclock, amiga_flags, I_flag,
459 aio_base >> 9, Z_flag == 0);
460 /*NOTREACHED*/
461
462 freeall:
463 free(kp, ksize);
464 free(eh, sizeof(*eh));
465 err:
466 printf("\nError %ld\n", (long)errno);
467 close(io);
468 out:
469 timelimit = 10;
470 (void)getchar();
471 return 1;
472 }
473
474 static
475 long get_number(ptr)
476 char **ptr;
477 {
478 long value = 0;
479 int base = 10;
480 char *p = *ptr;
481 char c;
482 char sign = 0;
483
484 c = *++p;
485 while (c == ' ')
486 c = *++p;
487 if (c == '-') {
488 sign = -1;
489 c = *++p;
490 }
491 if (c == '$') {
492 base = 16;
493 c = *++p;
494 } else if (c == '0') {
495 c = *++p;
496 if ((c & 0xdf) == 'X') {
497 base = 16;
498 c = *++p;
499 }
500 }
501 while (c) {
502 if (c >= '0' && c <= '9')
503 c -= '0';
504 else {
505 c = (c & 0xdf) - 'A' + 10;
506 if (base != 16 || c < 10 || c > 15)
507 break;
508 }
509 value = value * base + c;
510 c = *++p;
511 }
512 *ptr = p - 1;
513 #ifdef TEST
514 fprintf(stderr, "get_number: got %c0x%x",
515 sign ? '-' : '+', value);
516 #endif
517 return (sign ? -value : value);
518 }
519
520 /*
521 * Try to determine the machine ID by searching the resident module list
522 * for modules only present on specific machines. (Thanks, Bill!)
523 */
524
525 int
526 get_cpuid(cpuid)
527 u_int32_t *cpuid;
528 {
529 *cpuid |= SysBase->AttnFlags; /* get FPU and CPU flags */
530 if (*cpuid & 0xffff0000) {
531 if ((*cpuid >> 24) == 0x7D)
532 return 0;
533
534 switch (*cpuid >> 16) {
535 case 500:
536 case 600:
537 case 1000:
538 case 1200:
539 case 2000:
540 case 3000:
541 case 4000:
542 return 0;
543 default:
544 printf("Amiga %ld ???\n",
545 (long)(*cpuid >> 16));
546 return(1);
547 }
548 }
549 if (FindResident("A4000 Bonus") || FindResident("A4000 bonus")
550 || FindResident("A1000 Bonus"))
551 *cpuid |= 4000 << 16;
552 else if (FindResident("A3000 Bonus") || FindResident("A3000 bonus")
553 || (SysBase->LibNode.Version == 36))
554 *cpuid |= 3000 << 16;
555 else if (OpenResource("card.resource")) {
556 /* Test for AGA? */
557 *cpuid |= 1200 << 16;
558 } else if (OpenResource("draco.resource")) {
559 *cpuid |= (32000 | DRACOREVISION) << 16;
560 }
561 /*
562 * Nothing found, it's probably an A2000 or A500
563 */
564 if ((*cpuid >> 16) == 0)
565 *cpuid |= 2000 << 16;
566
567 return 0;
568 }
569