Home | History | Annotate | Line # | Download | only in loadbsd
loadbsd.c revision 1.2
      1  1.2  leo /*	$NetBSD: loadbsd.c,v 1.2 1995/03/28 06:26:50 leo Exp $	*/
      2  1.1  leo 
      3  1.1  leo /*
      4  1.1  leo  * Copyright (c) 1995 L. Weppelman
      5  1.1  leo  * All rights reserved.
      6  1.1  leo  *
      7  1.1  leo  * Redistribution and use in source and binary forms, with or without
      8  1.1  leo  * modification, are permitted provided that the following conditions
      9  1.1  leo  * are met:
     10  1.1  leo  * 1. Redistributions of source code must retain the above copyright
     11  1.1  leo  *    notice, this list of conditions and the following disclaimer.
     12  1.1  leo  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  leo  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  leo  *    documentation and/or other materials provided with the distribution.
     15  1.1  leo  * 3. All advertising materials mentioning features or use of this software
     16  1.1  leo  *    must display the following acknowledgement:
     17  1.1  leo  *      This product includes software developed by Leo Weppelman.
     18  1.1  leo  * 4. The name of the author may not be used to endorse or promote products
     19  1.1  leo  *    derived from this software without specific prior written permission
     20  1.1  leo  *
     21  1.1  leo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  1.1  leo  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  1.1  leo  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  1.1  leo  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  1.1  leo  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  1.1  leo  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  1.1  leo  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  1.1  leo  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  1.1  leo  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  1.1  leo  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  1.1  leo  */
     32  1.1  leo 
     33  1.1  leo /*
     34  1.1  leo  * NetBSD loader for the Atari-TT.
     35  1.1  leo  */
     36  1.1  leo 
     37  1.1  leo #include <stdio.h>
     38  1.1  leo #include <a_out.h>
     39  1.1  leo #include <fcntl.h>
     40  1.1  leo #include <osbind.h>
     41  1.1  leo #include <stdarg.h>
     42  1.1  leo #include "loader.h"
     43  1.1  leo 
     44  1.1  leo char	*Progname;		/* How are we called		*/
     45  1.1  leo int	t_flag = 0;		/* Just test, do not execute	*/
     46  1.1  leo 
     47  1.1  leo char version[] = "$VER: LoadBSD 1.0 (11/01/95)";
     48  1.1  leo 
     49  1.1  leo /*
     50  1.1  leo  * Default name of kernel to boot, large enough to patch
     51  1.1  leo  */
     52  1.1  leo char		kname[80] = "n:/netbsd";
     53  1.1  leo 
     54  1.1  leo static struct {
     55  1.1  leo 	u_char	*kp;		/* 00: Kernel load address		*/
     56  1.1  leo 	long	ksize;		/* 04: Size of loaded kernel		*/
     57  1.1  leo 	u_long	entry;		/* 08: Kernel entry point		*/
     58  1.1  leo 	long	stmem_size;	/* 12: Size of st-ram			*/
     59  1.1  leo 	long	ttmem_size;	/* 16: Size of tt-ram			*/
     60  1.1  leo 	long	cputype;	/* 20: Type of cpu			*/
     61  1.1  leo 	long	boothowto;	/* 24: How to boot			*/
     62  1.1  leo 	long	ttmem_start;	/* 28: Start of tt-ram			*/
     63  1.1  leo 	long	esym_loc;	/* 32: End of symbol table		*/
     64  1.1  leo } kparam;
     65  1.1  leo 
     66  1.1  leo void get_sys_info(void);
     67  1.1  leo void error(char *fmt, ...);
     68  1.1  leo void help(void);
     69  1.1  leo void usage(void);
     70  1.1  leo void start_kernel(void);
     71  1.1  leo 
     72  1.1  leo int main(argc, argv)
     73  1.1  leo int	argc;
     74  1.1  leo char	**argv;
     75  1.1  leo {
     76  1.1  leo 	/*
     77  1.1  leo 	 * Option parsing
     78  1.1  leo 	 */
     79  1.1  leo 	extern	int	optind;
     80  1.1  leo 	extern	char	*optarg;
     81  1.1  leo 	int		ch;
     82  1.1  leo 	int		fd;
     83  1.1  leo 	long		textsz, stringsz;
     84  1.1  leo 	struct exec	ehdr;
     85  1.1  leo 
     86  1.1  leo 	Progname = argv[0];
     87  1.1  leo 
     88  1.1  leo 	kparam.boothowto = RB_SINGLE;
     89  1.1  leo 
     90  1.1  leo 	while ((ch = getopt(argc, argv, "abdhtv")) != EOF) {
     91  1.1  leo 		switch(ch) {
     92  1.1  leo 		case 'a':
     93  1.1  leo 			kparam.boothowto &= ~(RB_SINGLE);
     94  1.1  leo 			kparam.boothowto |= RB_AUTOBOOT;
     95  1.1  leo 			break;
     96  1.1  leo 		case 'b':
     97  1.1  leo 			kparam.boothowto |= RB_ASKNAME;
     98  1.1  leo 			break;
     99  1.1  leo 		case 'd':
    100  1.1  leo 			kparam.boothowto |= RB_KDB;
    101  1.1  leo 			break;
    102  1.1  leo 		case 't':
    103  1.1  leo 			t_flag = 1;
    104  1.1  leo 			break;
    105  1.1  leo 		case 'v':
    106  1.1  leo 			fprintf(stderr,"%s\n", version);
    107  1.1  leo 			break;
    108  1.1  leo 		case 'h':
    109  1.1  leo 			help();
    110  1.1  leo 		default:
    111  1.1  leo 			usage();
    112  1.1  leo 		}
    113  1.1  leo 	}
    114  1.1  leo 	argc -= optind;
    115  1.1  leo 	argv += optind;
    116  1.1  leo 	if(argc == 1)
    117  1.1  leo 		strcpy(kname, argv[0]);
    118  1.1  leo 
    119  1.1  leo 	/*
    120  1.2  leo 	 * Get system info to pass to NetBSD
    121  1.1  leo 	 */
    122  1.1  leo 	get_sys_info();
    123  1.1  leo 
    124  1.1  leo 	/*
    125  1.1  leo 	 * Find the kernel to boot and read it's exec-header
    126  1.1  leo 	 */
    127  1.1  leo 	if((fd = open(kname, O_RDONLY)) < 0)
    128  1.1  leo 		error("Cannot open kernel '%s'", kname);
    129  1.1  leo 	if(read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr))
    130  1.1  leo 		error("Cannot read exec-header of '%s'", kname);
    131  1.1  leo 	if((ehdr.a_magic & 0xffff) != NMAGIC) /* XXX */
    132  1.1  leo 		error("Not an NMAGIC file '%s'", kname);
    133  1.1  leo 
    134  1.1  leo 	/*
    135  1.1  leo 	 * Extract various sizes from the kernel executable
    136  1.1  leo 	 */
    137  1.1  leo 	textsz          = (ehdr.a_text + __LDPGSZ - 1) & ~(__LDPGSZ - 1);
    138  1.1  leo 	kparam.esym_loc = 0;
    139  1.1  leo 	kparam.ksize    = textsz + ehdr.a_data + ehdr.a_bss;
    140  1.1  leo 	kparam.entry    = ehdr.a_entry;
    141  1.1  leo 
    142  1.1  leo 	if(ehdr.a_syms) {
    143  1.1  leo 	  if(lseek(fd,ehdr.a_text+ehdr.a_data+ehdr.a_syms+sizeof(ehdr), 0) <= 0)
    144  1.1  leo 		error("Cannot seek to string table in '%s'", kname);
    145  1.1  leo 	  if(read(fd, &stringsz, sizeof(long)) != sizeof(long))
    146  1.1  leo 		error("Cannot read string-table size");
    147  1.1  leo 	  if(lseek(fd, sizeof(ehdr), 0) <= 0)
    148  1.1  leo 		error("Cannot seek back to text start");
    149  1.1  leo 	  kparam.ksize += ehdr.a_syms + sizeof(long) + stringsz;
    150  1.1  leo 	}
    151  1.1  leo 
    152  1.1  leo 	if((kparam.kp = (u_char *)malloc(kparam.ksize)) == NULL)
    153  1.1  leo 		error("Cannot malloc kernel image space");
    154  1.1  leo 
    155  1.1  leo 	/*
    156  1.1  leo 	 * Read text & data, clear bss
    157  1.1  leo 	 */
    158  1.1  leo 	if((read(fd, kparam.kp, ehdr.a_text) != ehdr.a_text)
    159  1.1  leo 	    || (read(fd, kparam.kp + textsz, ehdr.a_data) != ehdr.a_data))
    160  1.1  leo 		error("Unable to read kernel image\n");
    161  1.1  leo 	memset(kparam.kp + textsz + ehdr.a_data, 0, ehdr.a_bss);
    162  1.1  leo 
    163  1.1  leo 	/*
    164  1.1  leo 	 * Read symbol and string table
    165  1.1  leo 	 */
    166  1.1  leo 	if(ehdr.a_syms) {
    167  1.1  leo 		long	*p;
    168  1.1  leo 
    169  1.1  leo 		p = (long *)(kparam.kp + textsz + ehdr.a_data + ehdr.a_bss);
    170  1.1  leo 		*p++ = ehdr.a_syms;
    171  1.1  leo 		if(read(fd, (char *)p, ehdr.a_syms) != ehdr.a_syms)
    172  1.1  leo 			error("Cannot read symbol table\n");
    173  1.1  leo 		p = (long *)((char *)p + ehdr.a_syms);
    174  1.1  leo 		if(read(fd, (char *)p, stringsz) != stringsz)
    175  1.1  leo 			error("Cannot read string table\n");
    176  1.1  leo 		kparam.esym_loc = (long)((char *)p-(char *)kparam.kp +stringsz);
    177  1.1  leo 	}
    178  1.1  leo 
    179  1.1  leo 	if(!t_flag)
    180  1.1  leo 		start_kernel();
    181  1.1  leo 		/* NOT REACHED */
    182  1.1  leo 
    183  1.1  leo 	fprintf(stderr, "Kernel '%s' was loaded OK\n", kname);
    184  1.1  leo 	exit(0);
    185  1.1  leo }
    186  1.1  leo 
    187  1.1  leo /*
    188  1.1  leo  * Extract memory and cpu/fpu info from system.
    189  1.1  leo  */
    190  1.1  leo void get_sys_info()
    191  1.1  leo {
    192  1.1  leo 	long	stck;
    193  1.1  leo 	long	*jar;
    194  1.1  leo 
    195  1.1  leo 	kparam.cputype = 0;
    196  1.1  leo 
    197  1.1  leo 	stck = Super(0);
    198  1.1  leo 
    199  1.1  leo 	kparam.ttmem_start = TTRAM_BASE;
    200  1.1  leo 	kparam.ttmem_size  = *ADDR_RAMTOP - TTRAM_BASE;
    201  1.1  leo 	kparam.stmem_size  = *ADDR_PHYSTOP;
    202  1.1  leo 
    203  1.1  leo 	/*
    204  1.1  leo 	 * Scan cookiejar for cpu/fpu types
    205  1.1  leo 	 */
    206  1.1  leo 	jar = *ADDR_P_COOKIE;
    207  1.1  leo 	if(jar != NULL) {
    208  1.1  leo 		do {
    209  1.1  leo 			if(jar[0] == 0x5f435055) { /* _CPU	*/
    210  1.1  leo 				switch(jar[1]) {
    211  1.1  leo 					case 0:
    212  1.1  leo 						kparam.cputype |= ATARI_68000;
    213  1.1  leo 						break;
    214  1.1  leo 					case 10:
    215  1.1  leo 						kparam.cputype |= ATARI_68010;
    216  1.1  leo 						break;
    217  1.1  leo 					case 20:
    218  1.1  leo 						kparam.cputype |= ATARI_68020;
    219  1.1  leo 						break;
    220  1.1  leo 					case 30:
    221  1.1  leo 						kparam.cputype |= ATARI_68030;
    222  1.1  leo 						break;
    223  1.1  leo 					case 40:
    224  1.1  leo 						kparam.cputype |= ATARI_68040;
    225  1.1  leo 						break;
    226  1.1  leo 					default:
    227  1.1  leo 						error("Unknown CPU-type");
    228  1.1  leo 				}
    229  1.1  leo 			}
    230  1.1  leo 			if(jar[0] == 0x5f465055) { /* _FPU	*/
    231  1.1  leo 				switch(jar[1]) {
    232  1.1  leo 					case 0x10000:
    233  1.1  leo 					case 0x20000:
    234  1.1  leo 						kparam.cputype |= ATARI_68881;
    235  1.1  leo 						break;
    236  1.1  leo 					case 0x30000:
    237  1.1  leo 						kparam.cputype |= ATARI_68882;
    238  1.1  leo 						break;
    239  1.1  leo 					case 0x40000:
    240  1.1  leo 						kparam.cputype |= ATARI_FPU40;
    241  1.1  leo 						break;
    242  1.1  leo 					default:
    243  1.1  leo 						error("Unknown FPU-type");
    244  1.1  leo 				}
    245  1.1  leo 			}
    246  1.1  leo 			jar = &jar[2];
    247  1.1  leo 		} while(jar[-2]);
    248  1.1  leo 	}
    249  1.1  leo 	if(!(kparam.cputype & ATARI_ANYCPU))
    250  1.1  leo 		error("Cannot determine CPU-type");
    251  1.1  leo 	if(!(kparam.cputype & ATARI_ANYFPU))
    252  1.1  leo 		error("Cannot determine FPU-type");
    253  1.1  leo 
    254  1.1  leo 	Super(stck);
    255  1.1  leo }
    256  1.1  leo 
    257  1.1  leo void error(char *fmt, ...)
    258  1.1  leo {
    259  1.1  leo 	va_list	ap;
    260  1.1  leo 
    261  1.1  leo 	va_start(ap, fmt);
    262  1.1  leo 
    263  1.1  leo 	fprintf(stderr, "%s: ", Progname);
    264  1.1  leo 	vfprintf(stderr, fmt, ap);
    265  1.1  leo 	fprintf(stderr, "\n");
    266  1.1  leo 	exit(1);
    267  1.1  leo 	/*NOTREACHED*/
    268  1.1  leo }
    269  1.1  leo 
    270  1.1  leo void help()
    271  1.1  leo {
    272  1.1  leo 	fprintf(stderr, "
    273  1.1  leo NetBSD loader for the Atari-TT
    274  1.1  leo 
    275  1.1  leo Usage: %s [-abdhtv] [kernel]
    276  1.1  leo 
    277  1.1  leo Description of options:
    278  1.1  leo 
    279  1.1  leo \t-a  Boot up to multi-user mode.
    280  1.1  leo \t-b  Ask for root device to use.
    281  1.1  leo \t-d  Enter kernel debugger.
    282  1.1  leo \t-h  What your getting right now.
    283  1.1  leo \t-t  Test the loader. It will do everything except executing the
    284  1.1  leo \t    loaded kernel.
    285  1.1  leo \t-v  Print loader version.
    286  1.1  leo ", Progname);
    287  1.1  leo 	exit(1);
    288  1.1  leo }
    289  1.1  leo 
    290  1.1  leo void usage()
    291  1.1  leo {
    292  1.1  leo 	fprintf(stderr, "Usage: %s [-abdhtv] [kernel]", Progname);
    293  1.1  leo 	exit(1);
    294  1.1  leo }
    295  1.1  leo 
    296  1.1  leo void start_kernel()
    297  1.1  leo {
    298  1.1  leo 	long	stck;
    299  1.1  leo 
    300  1.1  leo 	stck = Super(0);
    301  1.1  leo 	startit();
    302  1.1  leo 	/* NOT REACHED */
    303  1.1  leo 
    304  1.1  leo 	Super(stck);
    305  1.1  leo }
    306  1.1  leo 
    307  1.1  leo asm("
    308  1.1  leo 	.text
    309  1.1  leo 	.globl	_startit
    310  1.1  leo 
    311  1.1  leo _startit:
    312  1.1  leo 	move.w	#0x2700,sr
    313  1.1  leo 
    314  1.1  leo 	| the BSD kernel wants values into the following registers:
    315  1.1  leo 	| d0:  ttmem-size
    316  1.1  leo 	| d1:  stmem-size
    317  1.1  leo 	| d2:  cputype
    318  1.1  leo 	| d3:  boothowto
    319  1.1  leo 	| d4:  length of loaded kernel
    320  1.1  leo 	| a0:  start of loaded kernel
    321  1.1  leo 	| a1:  end of symbols (esym)
    322  1.1  leo 	| All other registers zeroed for possible future requirements.
    323  1.1  leo 
    324  1.1  leo 	lea	_kparam, a3		| a3 points to parameter block
    325  1.1  leo 	lea	_startit,sp		| make sure we have a good stack ***
    326  1.1  leo 	move.l	(a3),a0			| loaded kernel
    327  1.1  leo 	move.l	8(a3),-(sp)		| push entry point		***
    328  1.1  leo 	move.l	a0,d0			| offset of loaded kernel
    329  1.1  leo 	add.l	d0,(sp)			| add offset
    330  1.1  leo 	move.l	12(a3),d1		| stmem-size
    331  1.1  leo 	move.l	16(a3),d0		| ttmem-size
    332  1.1  leo 	move.l	20(a3),d2		| cputype
    333  1.1  leo 	move.l	24(a3),d3		| boothowto
    334  1.1  leo 	move.l	4(a3),d4		| length of loaded kernel
    335  1.1  leo 	move.l	28(a3),d5		| start of fastram
    336  1.1  leo 	move.l	32(a3),a1		| end of symbols
    337  1.1  leo 	sub.l	a5,a5			| target, load to 0
    338  1.1  leo 	btst	#4, d2			| Is this an 68040?
    339  1.1  leo 	beq	not040
    340  1.1  leo 
    341  1.1  leo 	| Turn off 68040 MMU
    342  1.1  leo 	.word 0x4e7b,0xd003		| movec a5,tc
    343  1.1  leo 	.word 0x4e7b,0xd806		| movec a5,urp
    344  1.1  leo 	.word 0x4e7b,0xd807		| movec a5,srp
    345  1.1  leo 	.word 0x4e7b,0xd004		| movec a5,itt0
    346  1.1  leo 	.word 0x4e7b,0xd005		| movec a5,itt1
    347  1.1  leo 	.word 0x4e7b,0xd006		| movec a5,dtt0
    348  1.1  leo 	.word 0x4e7b,0xd007		| movec a5,dtt1
    349  1.1  leo 	bra	nott
    350  1.1  leo 
    351  1.1  leo not040:
    352  1.1  leo 	lea	zero,a3
    353  1.1  leo 	pmove	(a3),tcr		| Turn off MMU
    354  1.1  leo 	lea	nullrp,a3
    355  1.1  leo 	pmove	(a3),crp		| Turn off MMU some more
    356  1.1  leo 	pmove	(a3),srp		| Really, really, turn off MMU
    357  1.1  leo 
    358  1.1  leo 	| Turn off 68030 TT registers
    359  1.1  leo 	btst	#3, d2			| Is this an 68030?
    360  1.1  leo 	beq.b	nott
    361  1.1  leo 	lea	zero,a3
    362  1.1  leo 	pmove	(a3),tt0
    363  1.1  leo 	pmove	(a3),tt1
    364  1.1  leo 
    365  1.1  leo nott:
    366  1.1  leo 	moveq.l	#0,d6			|  would have known contents)
    367  1.1  leo 	moveq.l	#0,d7
    368  1.1  leo 	movea.l	d6,a2
    369  1.1  leo 	movea.l	d6,a3
    370  1.1  leo 	movea.l	d6,a4
    371  1.1  leo 	movea.l	d6,a5
    372  1.1  leo 	movea.l	d6,a6
    373  1.1  leo 	rts				| enter kernel at address on stack ***
    374  1.1  leo 
    375  1.1  leo 
    376  1.1  leo | A do-nothing MMU root pointer (includes the following long as well)
    377  1.1  leo 
    378  1.1  leo nullrp:	.long	0x80000202
    379  1.1  leo zero:	.long	0
    380  1.1  leo svsp:	.long   0
    381  1.1  leo 
    382  1.1  leo 
    383  1.1  leo ");
    384