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