Home | History | Annotate | Line # | Download | only in boot
main.c revision 1.1
      1 /*
      2  * $NetBSD: main.c,v 1.1 1996/11/29 23:36:29 is 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, 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 -Sn2";
     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, 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 	/*
    130 	 * we need V36 for: EClock, RDB Bootblocks, CacheClearU
    131 	 */
    132 
    133 	if (SysBase->LibNode.Version < EXECMIN) {
    134 		printf("Exec V%ld, need V%ld\n",
    135 		    (long)SysBase->LibNode.Version, (long)EXECMIN);
    136 		goto out;
    137 	}
    138 
    139 	printf("\2337mNetBSD/Amiga bootblock %s\2330m\n%s :- ",
    140 		version, kernel_name);
    141 
    142 	timelimit = 3;
    143 	gets(linebuf);
    144 
    145 	if (*linebuf == 'q')
    146 		return 1;
    147 
    148 	if (*linebuf)
    149 		path = linebuf;
    150 
    151 	/*
    152 	 * parse boot command for path name and process any options
    153 	 */
    154 	while ((c = *path)) {
    155 		while (c == ' ')
    156 			c = *++path;
    157 		if (c == '-') {
    158 			while ((c = *++path) && c != ' ') {
    159 				switch (c) {
    160 				case 'a':	/* multi-user state */
    161 					boothowto &= ~RB_SINGLE;
    162 					break;
    163 				case 'b':	/* ask for root device */
    164 					boothowto |= RB_ASKNAME;
    165 					break;
    166 				case 'c':	/* force machine model */
    167 					cpuid = get_number(&path) << 16;
    168 					break;
    169 				case 'k':	/* Reserve first 4M fastmem */
    170 					k_flag++;
    171 					break;
    172 				case 'm':	/* Force fastmem size */
    173 					m_value = get_number(&path) * 1024;
    174 					break;
    175 				case 'n':	/* non-contiguous memory */
    176 					amiga_flags |=
    177 					    (get_number(&path) & 3) << 1;
    178 					break;
    179 				case 'p':	/* Select fastmem by priority */
    180 					p_flag++;
    181 					break;
    182 				case 's':	/* single-user state */
    183 					boothowto |= RB_SINGLE;
    184 					break;
    185 				case 't':	/* test flag */
    186 					t_flag = 1;
    187 					break;
    188 				case 'A':	/* enable AGA modes */
    189 					amiga_flags |= 1;
    190 					break;
    191 				case 'D':	/* enter Debugger */
    192 					boothowto |= RB_KDB;
    193 					break;
    194 				case 'I':	/* inhibit sync negotiation */
    195 					I_flag = get_number(&path);
    196 					break;
    197 				case 'K':	/* remove 1st 4MB fastmem */
    198 					break;
    199 				case 'S':	/* include debug symbols */
    200 					S_flag = 1;
    201 					break;
    202 				case 'Z':	/* force chip memory load */
    203 					Z_flag = 1;
    204 					break;
    205 				}
    206 			}
    207 		} else {
    208 			kernel_name = path;
    209 			while ((c = *++path) && c != ' ')
    210 				;
    211 			if (c)
    212 				*path++ = 0;
    213 		}
    214 	}
    215 	while ((c = *kernel_name) && c == ' ')
    216 		++kernel_name;
    217 	path = kernel_name;
    218 	while ((c = *path) && c != ' ')
    219 		++path;
    220 	if (c)
    221 		*path = 0;
    222 
    223 	if (get_cpuid(&cpuid))
    224 		goto out;
    225 
    226 	ExpansionBase = OpenLibrary("expansion.library", 0);
    227 	if (!ExpansionBase) {
    228 		printf("can't open %s\n", "expansion.library");
    229 		return 1;
    230 	}
    231 
    232 	for (ncd=0, cd=0; (cd = FindConfigDev(cd, -1, -1)); ncd++)
    233 		/* nothing */;
    234 
    235 	/* find memory list */
    236 
    237 	memseg = (struct boot_memseg *)alloc(16*sizeof(struct boot_memseg));
    238 
    239 	/* Forbid(); */
    240 
    241 	nseg = 0;
    242 	mh = SysBase->MemLst;
    243 	vfrom = mh->Lower & -__PGSZ;
    244 	vsize = (mh->Upper - vfrom) & -__PGSZ;
    245 	contflag = mapped1to1 = 0;
    246 
    247 	do {
    248 		size = vsize;
    249 
    250 		if (SysBase->LibNode.Version > 36) {
    251 			from = CachePreDMA(vfrom, &size, contflag);
    252 			contflag = DMAF_Continue;
    253 			mapped1to1 = (from == vfrom);
    254 			vsize -= size;
    255 			vfrom += size;
    256 		} else {
    257 			from = vfrom;
    258 			vsize = 0;
    259 		}
    260 
    261 #if 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 	printf("Loading %s: ", kernel_name);
    312 	io = open(kernel_name, 0);
    313 	if (io < 0)
    314 		goto err;
    315 
    316 	eh = alloc(sizeof(*eh));
    317 	if (!eh) {
    318 		errno = ENOMEM;
    319 		goto err;
    320 	}
    321 	if (read(io, eh, sizeof(*eh)) != sizeof(*eh)) {
    322 		errno = ENOEXEC;
    323 		goto err;
    324 	}
    325 
    326 	if ((N_GETMAGIC(*eh) != NMAGIC) || (N_GETMID(*eh) != MID_M68K)) {
    327 		errno = ENOEXEC;
    328 		goto err;
    329 	}
    330 
    331 	textsz = (eh->a_text + __LDPGSZ - 1) & (-__LDPGSZ);
    332 	esym = 0;
    333 
    334 	ksize = textsz + eh->a_data + eh->a_bss
    335 	    + sizeof(*nkcd) + ncd*sizeof(*cd)
    336 	    + sizeof(*nkcd) + nseg * sizeof(struct boot_memseg);
    337 
    338 	if (S_flag && eh->a_syms) {
    339 		if (lseek(io, eh->a_text+ eh->a_data+ eh->a_syms, SEEK_CUR)
    340 		    <= 0
    341 		    || read(io, &stringsz, 4) != 4
    342 		    || lseek(io, sizeof(*eh), SEEK_SET) < 0)
    343 			goto err;
    344 		ksize += eh->a_syms + 4 + ((stringsz + 3) & ~3);
    345 	}
    346 
    347 	kp = alloc(ksize + 256 + ((u_char *)startit_end - (u_char *)startit));
    348 	if (kp == 0) {
    349 		errno = ENOMEM;
    350 		goto err;
    351 	}
    352 
    353 	printf("%ld", eh->a_text);
    354 	if (read(io, kp, eh->a_text) != eh->a_text)
    355 		goto err;
    356 
    357 	printf("+%ld", eh->a_data);
    358 	if (read(io, kp + textsz, eh->a_data) != eh->a_data)
    359 		goto err;
    360 
    361 	printf("+%ld", eh->a_bss);
    362 
    363 	kvers = (u_short *)(kp + eh->a_entry - 2);
    364 
    365 	if (*kvers > KERNEL_STARTUP_VERSION_MAX && *kvers != 0x4e73) {
    366                 printf("\nnewer bootblock required: %ld\n", (long)*kvers);
    367 		goto freeall;
    368 	}
    369 #if 0
    370         if (*kvers > KERNEL_STARTUP_VERSION)
    371 		printf("\nKernel V%ld newer than bootblock V%ld\n",
    372 		    (long)*kvers, (long)KERNEL_STARTUP_VERSION);
    373 #endif
    374         nkcd = (int *)(kp + textsz + eh->a_data + eh->a_bss);
    375         if (*kvers != 0x4e73 && *kvers > 1 && S_flag && eh->a_syms) {
    376                 *nkcd++ = eh->a_syms;
    377 		printf("+[%ld", eh->a_syms);
    378                 if (read(io, (char *)nkcd, eh->a_syms) != eh->a_syms)
    379 			goto err;
    380                 nkcd = (int *)((char *)nkcd + eh->a_syms);
    381 		printf("+%ld]", stringsz);
    382                 if (read(io, (char *)nkcd, stringsz) != stringsz)
    383 			goto err;
    384                 nkcd = (int*)((char *)nkcd + ((stringsz + 3) & ~3));
    385                 esym = (char *)(textsz + eh->a_data + eh->a_bss
    386                     + eh->a_syms + 4 + ((stringsz + 3) & ~3));
    387         }
    388 	putchar('\n');
    389 
    390 	*nkcd = ncd;
    391 	kcd = (struct cfdev *)(nkcd + 1);
    392 
    393 	while ((cd = FindConfigDev(cd, -1, -1))) {
    394 		*kcd = *cd;
    395 		if (((cpuid >> 24) == 0x7D) &&
    396 		    ((u_long)kcd->addr < 0x1000000)) {
    397 			kcd->addr += 0x3000000;
    398 		}
    399 		++kcd;
    400 	}
    401 
    402 	nkcd = (u_int32_t *)kcd;
    403 	*nkcd = nseg;
    404 
    405 	kmemseg = (struct boot_memseg *)(nkcd + 1);
    406 
    407 	while (nseg-- > 0)
    408 		*kmemseg++ = *memseg++;
    409 
    410 	if (*kvers > 2 && Z_flag == 0) {
    411 		/*
    412 		 * Kernel supports direct load to fastmem, and the -Z
    413 		 * option was not specified.  Copy startup code to end
    414 		 * of kernel image and set start_it.
    415 		 */
    416 		if ((u_int32_t)kp < fmem) {
    417 			errno = EFBIG;
    418 			goto err;
    419 		}
    420 		memcpy(kp + ksize + 256, (char *)startit,
    421 		    (char *)startit_end - (char *)startit);
    422 		CacheClearU();
    423 		(caddr_t)start_it = kp + ksize + 256;
    424 		printf("*** Loading from %08lx to Fastmem %08lx ***\n",
    425 		    (u_long)kp, (u_long)fmem);
    426 		/* sleep(2); */
    427 	} else {
    428 		/*
    429 		 * Either the kernel doesn't suppport loading directly to
    430 		 * fastmem or the -Z flag was given.  Verify kernel image
    431 		 * fits into chipmem.
    432 		 */
    433 		if (ksize >= cmemsz) {
    434 			printf("Kernel size %d exceeds Chip Memory of %d\n",
    435 			    ksize, cmemsz);
    436 			return 20;
    437 		}
    438 		Z_flag = 1;
    439 		printf("*** Loading from %08lx to Chipmem ***\n", (u_long)kp);
    440 	}
    441 
    442 #if 0
    443 	printf("would start(kp=0x%lx, ksize=%ld, entry=0x%lx,\n"
    444 		"fmem=0x%lx, fmemsz=%ld, cmemsz=%ld\n"
    445 		"boothow=0x%lx, esym=0x%lx, cpuid=0x%lx, eclock=%ld\n"
    446 		"amigaflags=0x%lx, I_flags=0x%lx, Zflag=%ld, ok?\n",
    447 	    (u_long)kp, (u_long)ksize, eh->a_entry,
    448 	    (u_long)fmem, (u_long)fmemsz, (u_long)cmemsz,
    449 	    (u_long)boothowto, (u_long)esym, (u_long)cpuid, (u_long)eclock,
    450 	    (u_long)amiga_flags, (u_long)I_flag, (u_long)(Z_flag == 0));
    451 #endif
    452 	timelimit = 2;
    453 	(void)getchar();
    454 
    455 	start_it(kp, ksize, eh->a_entry, (void *)fmem, fmemsz, cmemsz,
    456 	    boothowto, esym, cpuid, eclock, amiga_flags, I_flag, Z_flag == 0);
    457 	/*NOTREACHED*/
    458 
    459 freeall:
    460 	free(kp, ksize);
    461 	free(eh, sizeof(*eh));
    462 err:
    463 	printf("\nError %ld\n", (long)errno);
    464 	close(io);
    465 out:
    466 	timelimit = 10;
    467 	(void)getchar();
    468 	return 1;
    469 }
    470 
    471 static
    472 long get_number(ptr)
    473 char **ptr;
    474 {
    475 	long value = 0;
    476 	int base = 10;
    477 	char *p = *ptr;
    478 	char c;
    479 	char sign = 0;
    480 
    481 	c = *++p;
    482 	while (c == ' ')
    483 		c = *++p;
    484 	if (c == '-') {
    485 		sign = -1;
    486 		c = *++p;
    487 	}
    488 	if (c == '$') {
    489 		base = 16;
    490 		c = *++p;
    491 	} else if (c == '0') {
    492 		c = *++p;
    493 		if ((c & 0xdf) == 'X') {
    494 			base = 16;
    495 			c = *++p;
    496 		}
    497 	}
    498 	while (c) {
    499 		if (c >= '0' && c <= '9')
    500 			c -= '0';
    501 		else {
    502 			c = (c & 0xdf) - 'A' + 10;
    503 			if (base != 16 || c < 10 || c > 15)
    504 				break;
    505 		}
    506 		value = value * base + c;
    507 		c = *++p;
    508 	}
    509 	*ptr = p - 1;
    510 #ifdef TEST
    511 	fprintf(stderr, "get_number: got %c0x%x",
    512 	    sign ? '-' : '+', value);
    513 #endif
    514 	return (sign ? -value : value);
    515 }
    516 
    517 /*
    518  * Try to determine the machine ID by searching the resident module list
    519  * for modules only present on specific machines.  (Thanks, Bill!)
    520  */
    521 
    522 int
    523 get_cpuid(cpuid)
    524 	u_int32_t *cpuid;
    525 {
    526 	*cpuid |= SysBase->AttnFlags;	/* get FPU and CPU flags */
    527 	if (*cpuid & 0xffff0000) {
    528 		if ((*cpuid >> 24) == 0x7D)
    529 			return 0;
    530 
    531 		switch (*cpuid >> 16) {
    532 		case 500:
    533 		case 600:
    534 		case 1000:
    535 		case 1200:
    536 		case 2000:
    537 		case 3000:
    538 		case 4000:
    539 			return 0;
    540 		default:
    541 			printf("Amiga %ld ???\n",
    542 			    (long)(*cpuid >> 16));
    543 			return(1);
    544 		}
    545 	}
    546 	if (FindResident("A4000 Bonus") || FindResident("A4000 bonus")
    547 	    || FindResident("A1000 Bonus"))
    548 		*cpuid |= 4000 << 16;
    549 	else if (FindResident("A3000 Bonus") || FindResident("A3000 bonus")
    550 	    || (SysBase->LibNode.Version == 36))
    551 		*cpuid |= 3000 << 16;
    552 	else if (OpenResource("card.resource")) {
    553 		/* Test for AGA? */
    554 		*cpuid |= 1200 << 16;
    555 	} else if (OpenResource("draco.resource")) {
    556 		*cpuid |= (32000 | DRACOREVISION) << 16;
    557 	}
    558 	/*
    559 	 * Nothing found, it's probably an A2000 or A500
    560 	 */
    561 	if ((*cpuid >> 16) == 0)
    562 		*cpuid |= 2000 << 16;
    563 
    564 	return 0;
    565 }
    566