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