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