Home | History | Annotate | Line # | Download | only in boot
main.c revision 1.9
      1 /*
      2  * $NetBSD: main.c,v 1.9 1998/10/31 22:40:27 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 #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 -ASn2";
     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 ehs;
    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 	if (read(io, &ehs, sizeof(ehs)) != sizeof(ehs)) {
    324 		errno = ENOEXEC;
    325 		goto err;
    326 	}
    327 
    328 	if ((N_GETMAGIC(ehs) != NMAGIC) || (N_GETMID(ehs) != MID_M68K)) {
    329 		errno = ENOEXEC;
    330 		goto err;
    331 	}
    332 
    333 	textsz = (ehs.a_text + __LDPGSZ - 1) & (-__LDPGSZ);
    334 	esym = 0;
    335 
    336 	ksize = textsz + ehs.a_data + ehs.a_bss
    337 	    + sizeof(*nkcd) + ncd*sizeof(*cd)
    338 	    + sizeof(*nkcd) + nseg * sizeof(struct boot_memseg);
    339 
    340 	if (S_flag && ehs.a_syms) {
    341 		if (lseek(io, ehs.a_text+ ehs.a_data+ ehs.a_syms, SEEK_CUR)
    342 		    <= 0
    343 		    || read(io, &stringsz, 4) != 4
    344 		    || lseek(io, sizeof(ehs), SEEK_SET) < 0)
    345 			goto err;
    346 		ksize += ehs.a_syms + 4 + ((stringsz + 3) & ~3);
    347 	}
    348 
    349 	kp = alloc(ksize + 256 + ((u_char *)startit_end - (u_char *)startit));
    350 	if (kp == 0) {
    351 		errno = ENOMEM;
    352 		goto err;
    353 	}
    354 
    355 	printf("%ld", ehs.a_text);
    356 	if (read(io, kp, ehs.a_text) != ehs.a_text)
    357 		goto err;
    358 
    359 	printf("+%ld", ehs.a_data);
    360 	if (read(io, kp + textsz, ehs.a_data) != ehs.a_data)
    361 		goto err;
    362 
    363 	printf("+%ld", ehs.a_bss);
    364 
    365 	kvers = (u_short *)(kp + ehs.a_entry - 2);
    366 
    367 	if (*kvers > KERNEL_STARTUP_VERSION_MAX && *kvers != 0x4e73) {
    368                 printf("\nnewer bootblock required: %ld\n", (long)*kvers);
    369 		goto freeall;
    370 	}
    371 	if (*kvers < KERNEL_STARTUP_VERSION || *kvers == 0x4e73) {
    372                 printf("\nkernel too old for bootblock\n");
    373 		goto freeall;
    374 	}
    375 #if 0
    376         if (*kvers > KERNEL_STARTUP_VERSION)
    377 		printf("\nKernel V%ld newer than bootblock V%ld\n",
    378 		    (long)*kvers, (long)KERNEL_STARTUP_VERSION);
    379 #endif
    380         nkcd = (int *)(kp + textsz + ehs.a_data + ehs.a_bss);
    381         if (*kvers != 0x4e73 && *kvers > 1 && S_flag && ehs.a_syms) {
    382                 *nkcd++ = ehs.a_syms;
    383 		printf("+[%ld", ehs.a_syms);
    384                 if (read(io, (char *)nkcd, ehs.a_syms) != ehs.a_syms)
    385 			goto err;
    386                 nkcd = (int *)((char *)nkcd + ehs.a_syms);
    387 		printf("+%ld]", stringsz);
    388                 if (read(io, (char *)nkcd, stringsz) != stringsz)
    389 			goto err;
    390                 nkcd = (int*)((char *)nkcd + ((stringsz + 3) & ~3));
    391                 esym = (char *)(textsz + ehs.a_data + ehs.a_bss
    392                     + ehs.a_syms + 4 + ((stringsz + 3) & ~3));
    393         }
    394 	putchar('\n');
    395 
    396 	*nkcd = ncd;
    397 	kcd = (struct cfdev *)(nkcd + 1);
    398 
    399 	while ((cd = FindConfigDev(cd, -1, -1))) {
    400 		*kcd = *cd;
    401 		if (((cpuid >> 24) == 0x7D) &&
    402 		    ((u_long)kcd->addr < 0x1000000)) {
    403 			kcd->addr += 0x3000000;
    404 		}
    405 		++kcd;
    406 	}
    407 
    408 	nkcd = (u_int32_t *)kcd;
    409 	*nkcd = nseg;
    410 
    411 	kmemseg = (struct boot_memseg *)(nkcd + 1);
    412 
    413 	while (nseg-- > 0)
    414 		*kmemseg++ = *memseg++;
    415 
    416 	/*
    417 	 * Copy startup code to end of kernel image and set start_it.
    418 	 */
    419 	memcpy(kp + ksize + 256, (char *)startit,
    420 	    (char *)startit_end - (char *)startit);
    421 	CacheClearU();
    422 	(caddr_t)start_it = kp + ksize + 256;
    423 	printf("*** Loading from %08lx to Fastmem %08lx ***\n",
    424 	    (u_long)kp, (u_long)fmem);
    425 	/* sleep(2); */
    426 
    427 #if 0
    428 	printf("would start(kp=0x%lx, ksize=%ld, entry=0x%lx,\n"
    429 		"fmem=0x%lx, fmemsz=%ld, cmemsz=%ld\n"
    430 		"boothow=0x%lx, esym=0x%lx, cpuid=0x%lx, eclock=%ld\n"
    431 		"amigaflags=0x%lx, I_flags=0x%lx, ok?\n",
    432 	    (u_long)kp, (u_long)ksize, ehs.a_entry,
    433 	    (u_long)fmem, (u_long)fmemsz, (u_long)cmemsz,
    434 	    (u_long)boothowto, (u_long)esym, (u_long)cpuid, (u_long)eclock,
    435 	    (u_long)amiga_flags, (u_long)I_flag);
    436 #endif
    437 #ifdef DEBUG_MEMORY_LIST
    438 	timelimit = 0;
    439 #else
    440 	timelimit = 2;
    441 #endif
    442 	(void)getchar();
    443 
    444 	start_it(kp, ksize, ehs.a_entry, (void *)fmem, fmemsz, cmemsz,
    445 	    boothowto, esym, cpuid, eclock, amiga_flags, I_flag,
    446 	    aio_base >> 9, 1);
    447 	/*NOTREACHED*/
    448 
    449 freeall:
    450 	free(kp, ksize);
    451 err:
    452 	printf("\nError %ld\n", (long)errno);
    453 	close(io);
    454 out:
    455 	timelimit = 10;
    456 	(void)getchar();
    457 	return 1;
    458 }
    459 
    460 static
    461 long get_number(ptr)
    462 char **ptr;
    463 {
    464 	long value = 0;
    465 	int base = 10;
    466 	char *p = *ptr;
    467 	char c;
    468 	char sign = 0;
    469 
    470 	c = *++p;
    471 	while (c == ' ')
    472 		c = *++p;
    473 	if (c == '-') {
    474 		sign = -1;
    475 		c = *++p;
    476 	}
    477 	if (c == '$') {
    478 		base = 16;
    479 		c = *++p;
    480 	} else if (c == '0') {
    481 		c = *++p;
    482 		if ((c & 0xdf) == 'X') {
    483 			base = 16;
    484 			c = *++p;
    485 		}
    486 	}
    487 	while (c) {
    488 		if (c >= '0' && c <= '9')
    489 			c -= '0';
    490 		else {
    491 			c = (c & 0xdf) - 'A' + 10;
    492 			if (base != 16 || c < 10 || c > 15)
    493 				break;
    494 		}
    495 		value = value * base + c;
    496 		c = *++p;
    497 	}
    498 	*ptr = p - 1;
    499 #ifdef TEST
    500 	fprintf(stderr, "get_number: got %c0x%x",
    501 	    sign ? '-' : '+', value);
    502 #endif
    503 	return (sign ? -value : value);
    504 }
    505 
    506 /*
    507  * Try to determine the machine ID by searching the resident module list
    508  * for modules only present on specific machines.  (Thanks, Bill!)
    509  */
    510 
    511 int
    512 get_cpuid(cpuid)
    513 	u_int32_t *cpuid;
    514 {
    515 	*cpuid |= SysBase->AttnFlags;	/* get FPU and CPU flags */
    516 	if (*cpuid & 0xffff0000) {
    517 		if ((*cpuid >> 24) == 0x7D)
    518 			return 0;
    519 
    520 		switch (*cpuid >> 16) {
    521 		case 500:
    522 		case 600:
    523 		case 1000:
    524 		case 1200:
    525 		case 2000:
    526 		case 3000:
    527 		case 4000:
    528 			return 0;
    529 		default:
    530 			printf("Amiga %ld ???\n",
    531 			    (long)(*cpuid >> 16));
    532 			return(1);
    533 		}
    534 	}
    535 	if (FindResident("A4000 Bonus") || FindResident("A4000 bonus")
    536 	    || FindResident("A1000 Bonus"))
    537 		*cpuid |= 4000 << 16;
    538 	else if (FindResident("A3000 Bonus") || FindResident("A3000 bonus")
    539 	    || (SysBase->LibNode.Version == 36))
    540 		*cpuid |= 3000 << 16;
    541 	else if (OpenResource("card.resource")) {
    542 		/* Test for AGA? */
    543 		*cpuid |= 1200 << 16;
    544 	} else if (OpenResource("draco.resource")) {
    545 		*cpuid |= (32000 | DRACOREVISION) << 16;
    546 	}
    547 	/*
    548 	 * Nothing found, it's probably an A2000 or A500
    549 	 */
    550 	if ((*cpuid >> 16) == 0)
    551 		*cpuid |= 2000 << 16;
    552 
    553 	return 0;
    554 }
    555