main.c revision 1.6 1 /*
2 * $NetBSD: main.c,v 1.6 1997/03/24 19:51:28 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 & -__PGSZ) - vfrom;
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 mapped1to1 = 1;
262 vsize = 0;
263 }
264
265 #ifdef DEBUG_MEMORY_LIST
266 printf("%lx %lx %lx %ld/%lx %lx\n",
267 (long)from, (long)size,
268 (long)mh->Attribs, (long)mh->Pri,
269 (long)vfrom, (long)vsize);
270 #endif
271 /* Insert The Evergrowing Kludge List Here: */
272
273 /* a) dont load kernel over DraCo MMU table */
274
275 if (((cpuid >> 24) == 0x7D) &&
276 ((from & -DRACOMMUMARGIN) == 0x40000000) &&
277 (size >= DRACOMMUMARGIN)) {
278
279 memseg[nseg].ms_start = from & -DRACOMMUMARGIN;
280 memseg[nseg].ms_size = DRACOMMUMARGIN;
281 memseg[nseg].ms_attrib = mh->Attribs;
282 memseg[nseg].ms_pri = mh->Pri;
283
284 size -= DRACOMMUMARGIN - (from & (DRACOMMUMARGIN - 1));
285 from += DRACOMMUMARGIN - (from & (DRACOMMUMARGIN - 1));
286 ++nseg;
287 }
288
289 if ((mh->Attribs & (MEMF_CHIP|MEMF_FAST)) == MEMF_CHIP) {
290 size += from;
291 cmemsz = size;;
292 from = 0;
293 } else if ((fmemsz < size) && mapped1to1) {
294 fmem = from;
295 fmemsz = size;
296 }
297
298 memseg[nseg].ms_start = from;
299 memseg[nseg].ms_size = size;
300 memseg[nseg].ms_attrib = mh->Attribs;
301 memseg[nseg].ms_pri = mh->Pri;
302
303 if (vsize == 0) {
304 mh = mh->next;
305 contflag = 0;
306 if (mh->next) {
307 vfrom = mh->Lower & -__PGSZ;
308 vsize = (mh->Upper & -__PGSZ) - vfrom;
309 }
310 }
311 } while ((++nseg <= 16) && vsize);
312
313 /* Permit(); */
314
315 printf("Loading %s: ", kernel_name);
316 io = open(kernel_name, 0);
317 if (io < 0)
318 goto err;
319
320 eh = alloc(sizeof(*eh));
321 if (!eh) {
322 errno = ENOMEM;
323 goto err;
324 }
325 if (read(io, eh, sizeof(*eh)) != sizeof(*eh)) {
326 errno = ENOEXEC;
327 goto err;
328 }
329
330 if ((N_GETMAGIC(*eh) != NMAGIC) || (N_GETMID(*eh) != MID_M68K)) {
331 errno = ENOEXEC;
332 goto err;
333 }
334
335 textsz = (eh->a_text + __LDPGSZ - 1) & (-__LDPGSZ);
336 esym = 0;
337
338 ksize = textsz + eh->a_data + eh->a_bss
339 + sizeof(*nkcd) + ncd*sizeof(*cd)
340 + sizeof(*nkcd) + nseg * sizeof(struct boot_memseg);
341
342 if (S_flag && eh->a_syms) {
343 if (lseek(io, eh->a_text+ eh->a_data+ eh->a_syms, SEEK_CUR)
344 <= 0
345 || read(io, &stringsz, 4) != 4
346 || lseek(io, sizeof(*eh), SEEK_SET) < 0)
347 goto err;
348 ksize += eh->a_syms + 4 + ((stringsz + 3) & ~3);
349 }
350
351 kp = alloc(ksize + 256 + ((u_char *)startit_end - (u_char *)startit));
352 if (kp == 0) {
353 errno = ENOMEM;
354 goto err;
355 }
356
357 printf("%ld", eh->a_text);
358 if (read(io, kp, eh->a_text) != eh->a_text)
359 goto err;
360
361 printf("+%ld", eh->a_data);
362 if (read(io, kp + textsz, eh->a_data) != eh->a_data)
363 goto err;
364
365 printf("+%ld", eh->a_bss);
366
367 kvers = (u_short *)(kp + eh->a_entry - 2);
368
369 if (*kvers > KERNEL_STARTUP_VERSION_MAX && *kvers != 0x4e73) {
370 printf("\nnewer bootblock required: %ld\n", (long)*kvers);
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 if (*kvers > 2 && Z_flag == 0) {
415 /*
416 * Kernel supports direct load to fastmem, and the -Z
417 * option was not specified. Copy startup code to end
418 * of kernel image and set start_it.
419 */
420 if ((u_int32_t)kp < fmem) {
421 errno = EFBIG;
422 goto err;
423 }
424 memcpy(kp + ksize + 256, (char *)startit,
425 (char *)startit_end - (char *)startit);
426 CacheClearU();
427 (caddr_t)start_it = kp + ksize + 256;
428 printf("*** Loading from %08lx to Fastmem %08lx ***\n",
429 (u_long)kp, (u_long)fmem);
430 /* sleep(2); */
431 } else {
432 /*
433 * Either the kernel doesn't suppport loading directly to
434 * fastmem or the -Z flag was given. Verify kernel image
435 * fits into chipmem.
436 */
437 if (ksize >= cmemsz) {
438 printf("Kernel size %d exceeds Chip Memory of %d\n",
439 ksize, cmemsz);
440 return 20;
441 }
442 Z_flag = 1;
443 printf("*** Loading from %08lx to Chipmem ***\n", (u_long)kp);
444 }
445
446 #if 0
447 printf("would start(kp=0x%lx, ksize=%ld, entry=0x%lx,\n"
448 "fmem=0x%lx, fmemsz=%ld, cmemsz=%ld\n"
449 "boothow=0x%lx, esym=0x%lx, cpuid=0x%lx, eclock=%ld\n"
450 "amigaflags=0x%lx, I_flags=0x%lx, Zflag=%ld, ok?\n",
451 (u_long)kp, (u_long)ksize, eh->a_entry,
452 (u_long)fmem, (u_long)fmemsz, (u_long)cmemsz,
453 (u_long)boothowto, (u_long)esym, (u_long)cpuid, (u_long)eclock,
454 (u_long)amiga_flags, (u_long)I_flag, (u_long)(Z_flag == 0));
455 #endif
456 #ifdef DEBUG_MEMORY_LIST
457 timelimit = 0;
458 #else
459 timelimit = 2;
460 #endif
461 (void)getchar();
462
463 start_it(kp, ksize, eh->a_entry, (void *)fmem, fmemsz, cmemsz,
464 boothowto, esym, cpuid, eclock, amiga_flags, I_flag,
465 aio_base >> 9, Z_flag == 0);
466 /*NOTREACHED*/
467
468 freeall:
469 free(kp, ksize);
470 free(eh, sizeof(*eh));
471 err:
472 printf("\nError %ld\n", (long)errno);
473 close(io);
474 out:
475 timelimit = 10;
476 (void)getchar();
477 return 1;
478 }
479
480 static
481 long get_number(ptr)
482 char **ptr;
483 {
484 long value = 0;
485 int base = 10;
486 char *p = *ptr;
487 char c;
488 char sign = 0;
489
490 c = *++p;
491 while (c == ' ')
492 c = *++p;
493 if (c == '-') {
494 sign = -1;
495 c = *++p;
496 }
497 if (c == '$') {
498 base = 16;
499 c = *++p;
500 } else if (c == '0') {
501 c = *++p;
502 if ((c & 0xdf) == 'X') {
503 base = 16;
504 c = *++p;
505 }
506 }
507 while (c) {
508 if (c >= '0' && c <= '9')
509 c -= '0';
510 else {
511 c = (c & 0xdf) - 'A' + 10;
512 if (base != 16 || c < 10 || c > 15)
513 break;
514 }
515 value = value * base + c;
516 c = *++p;
517 }
518 *ptr = p - 1;
519 #ifdef TEST
520 fprintf(stderr, "get_number: got %c0x%x",
521 sign ? '-' : '+', value);
522 #endif
523 return (sign ? -value : value);
524 }
525
526 /*
527 * Try to determine the machine ID by searching the resident module list
528 * for modules only present on specific machines. (Thanks, Bill!)
529 */
530
531 int
532 get_cpuid(cpuid)
533 u_int32_t *cpuid;
534 {
535 *cpuid |= SysBase->AttnFlags; /* get FPU and CPU flags */
536 if (*cpuid & 0xffff0000) {
537 if ((*cpuid >> 24) == 0x7D)
538 return 0;
539
540 switch (*cpuid >> 16) {
541 case 500:
542 case 600:
543 case 1000:
544 case 1200:
545 case 2000:
546 case 3000:
547 case 4000:
548 return 0;
549 default:
550 printf("Amiga %ld ???\n",
551 (long)(*cpuid >> 16));
552 return(1);
553 }
554 }
555 if (FindResident("A4000 Bonus") || FindResident("A4000 bonus")
556 || FindResident("A1000 Bonus"))
557 *cpuid |= 4000 << 16;
558 else if (FindResident("A3000 Bonus") || FindResident("A3000 bonus")
559 || (SysBase->LibNode.Version == 36))
560 *cpuid |= 3000 << 16;
561 else if (OpenResource("card.resource")) {
562 /* Test for AGA? */
563 *cpuid |= 1200 << 16;
564 } else if (OpenResource("draco.resource")) {
565 *cpuid |= (32000 | DRACOREVISION) << 16;
566 }
567 /*
568 * Nothing found, it's probably an A2000 or A500
569 */
570 if ((*cpuid >> 16) == 0)
571 *cpuid |= 2000 << 16;
572
573 return 0;
574 }
575