Home | History | Annotate | Line # | Download | only in loadbsd
loadbsd.c revision 1.19
      1  1.19  dsl /*	$NetBSD: loadbsd.c,v 1.19 2009/03/14 15:36:04 dsl 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 <fcntl.h>
     38  1.10  leo #include <stdio.h>
     39   1.1  leo #include <osbind.h>
     40   1.1  leo #include <stdarg.h>
     41  1.10  leo #include <stdlib.h>
     42  1.10  leo #include <string.h>
     43  1.10  leo #include <unistd.h>
     44  1.10  leo #include "libtos.h"
     45  1.17  leo #include "tosdefs.h"
     46  1.18  leo #include "cread.h"
     47  1.15  leo 
     48   1.1  leo char	*Progname;		/* How are we called		*/
     49  1.10  leo int	d_flag  = 0;		/* Output debugging output?	*/
     50  1.10  leo int	h_flag  = 0;		/* show help			*/
     51  1.16  leo int	N_flag  = 0;		/* No symbols?			*/
     52  1.10  leo int	s_flag  = 0;		/* St-ram only			*/
     53  1.10  leo int	t_flag  = 0;		/* Just test, do not execute	*/
     54  1.10  leo int	v_flag  = 0;		/* show version			*/
     55   1.1  leo 
     56  1.19  dsl const char version[] = "$Revision: 1.19 $";
     57   1.1  leo 
     58   1.1  leo /*
     59   1.1  leo  * Default name of kernel to boot, large enough to patch
     60   1.1  leo  */
     61  1.10  leo char	kname[80] = "n:/netbsd";
     62  1.10  leo 
     63  1.17  leo static osdsc_t	kernelparms;
     64   1.1  leo 
     65  1.10  leo void help  PROTO((void));
     66  1.10  leo void usage PROTO((void));
     67  1.17  leo void get_sys_info PROTO((osdsc_t *));
     68  1.17  leo void start_kernel PROTO((osdsc_t *));
     69   1.1  leo 
     70  1.10  leo int
     71  1.19  dsl main(int argc, char **argv)
     72   1.1  leo {
     73   1.1  leo 	/*
     74   1.1  leo 	 * Option parsing
     75   1.1  leo 	 */
     76   1.1  leo 	extern	int	optind;
     77   1.1  leo 	extern	char	*optarg;
     78  1.17  leo 	int		ch, err;
     79  1.17  leo 	char		*errmsg;
     80   1.1  leo 	int		fd;
     81  1.17  leo 	osdsc_t		*od;
     82   1.1  leo 
     83  1.10  leo 	init_toslib(argv[0]);
     84   1.1  leo 	Progname = argv[0];
     85   1.1  leo 
     86  1.17  leo 	od = &kernelparms;
     87  1.17  leo 	od->boothowto = RB_SINGLE;
     88   1.1  leo 
     89  1.16  leo 	while ((ch = getopt(argc, argv, "abdDhNstVwo:S:T:")) != -1) {
     90  1.10  leo 		switch (ch) {
     91   1.1  leo 		case 'a':
     92  1.17  leo 			od->boothowto &= ~(RB_SINGLE);
     93  1.17  leo 			od->boothowto |= RB_AUTOBOOT;
     94   1.1  leo 			break;
     95   1.1  leo 		case 'b':
     96  1.17  leo 			od->boothowto |= RB_ASKNAME;
     97   1.1  leo 			break;
     98   1.1  leo 		case 'd':
     99  1.17  leo 			od->boothowto |= RB_KDB;
    100   1.1  leo 			break;
    101   1.3  leo 		case 'D':
    102   1.3  leo 			d_flag = 1;
    103   1.3  leo 			break;
    104  1.10  leo 		case 'h':
    105  1.10  leo 			h_flag = 1;
    106  1.10  leo 			break;
    107  1.16  leo 		case 'N':
    108  1.16  leo 			N_flag = 1;
    109  1.16  leo 			break;
    110  1.10  leo 		case 'o':
    111  1.10  leo 			redirect_output(optarg);
    112  1.10  leo 			break;
    113   1.3  leo 		case 's':
    114   1.3  leo 			s_flag = 1;
    115   1.3  leo 			break;
    116   1.3  leo 		case 'S':
    117  1.17  leo 			od->stmem_size = atoi(optarg);
    118   1.3  leo 			break;
    119   1.1  leo 		case 't':
    120   1.1  leo 			t_flag = 1;
    121   1.1  leo 			break;
    122   1.5  leo 		case 'T':
    123  1.17  leo 			od->ttmem_size = atoi(optarg);
    124   1.5  leo 			break;
    125  1.10  leo 		case 'V':
    126  1.10  leo 			v_flag = 1;
    127  1.10  leo 			break;
    128  1.10  leo 		case 'w':
    129  1.10  leo 			set_wait_for_key();
    130   1.1  leo 			break;
    131   1.1  leo 		default:
    132   1.1  leo 			usage();
    133   1.1  leo 		}
    134   1.1  leo 	}
    135   1.1  leo 	argc -= optind;
    136   1.1  leo 	argv += optind;
    137  1.10  leo 	if (argc == 1)
    138   1.1  leo 		strcpy(kname, argv[0]);
    139   1.1  leo 
    140  1.10  leo 	if (h_flag)
    141  1.10  leo 		help();
    142  1.10  leo 	if (v_flag)
    143  1.10  leo 		eprintf("%s\r\n", version);
    144  1.10  leo 
    145   1.1  leo 	/*
    146   1.2  leo 	 * Get system info to pass to NetBSD
    147   1.1  leo 	 */
    148  1.17  leo 	get_sys_info(od);
    149  1.17  leo 	if (d_flag) {
    150  1.17  leo 	    eprintf("Machine info:\r\n");
    151  1.17  leo 	    eprintf("ST-RAM size\t: %10d bytes\r\n",od->stmem_size);
    152  1.17  leo 	    eprintf("TT-RAM size\t: %10d bytes\r\n",od->ttmem_size);
    153  1.17  leo 	    eprintf("TT-RAM start\t: 0x%08x\r\n", od->ttmem_start);
    154  1.17  leo 	    eprintf("Cpu-type\t: 0x%08x\r\n", od->cputype);
    155  1.17  leo 	}
    156   1.1  leo 
    157   1.1  leo 	/*
    158   1.1  leo 	 * Find the kernel to boot and read it's exec-header
    159   1.1  leo 	 */
    160  1.10  leo 	if ((fd = open(kname, O_RDONLY)) < 0)
    161  1.10  leo 		fatal(-1, "Cannot open kernel '%s'", kname);
    162  1.17  leo 	if ((err = elf_load(fd, od, &errmsg, !N_flag)) == -1) {
    163  1.17  leo 		/*
    164  1.17  leo 		 * Not ELF, try a.out
    165  1.17  leo 		 */
    166  1.17  leo 		if (err = aout_load(fd, od, &errmsg, !N_flag)) {
    167  1.17  leo 			if (err == -1)
    168  1.17  leo 				errmsg = "Not an ELF or NMAGIC file '%s'";
    169  1.17  leo 			fatal(-1, errmsg, kname);
    170  1.17  leo 		}
    171  1.17  leo 	}
    172  1.17  leo 	else {
    173  1.17  leo 		if (err)
    174  1.17  leo 			fatal(-1, errmsg);
    175  1.17  leo 	}
    176  1.17  leo 
    177  1.16  leo 	close(fd);
    178  1.16  leo 
    179  1.16  leo 	if (d_flag) {
    180  1.16  leo 	    eprintf("\r\nKernel info:\r\n");
    181  1.17  leo 	    eprintf("Kernel loadaddr\t: 0x%08x\r\n", od->kstart);
    182  1.17  leo 	    eprintf("Kernel size\t: %10d bytes\r\n", od->ksize);
    183  1.17  leo 	    eprintf("Kernel entry\t: 0x%08x\r\n", od->kentry);
    184  1.17  leo 	    eprintf("Kernel esym\t: 0x%08x\r\n", od->k_esym);
    185  1.16  leo 	}
    186  1.16  leo 
    187  1.16  leo 	if (!t_flag)
    188  1.17  leo 		start_kernel(od);
    189  1.16  leo 		/* NOT REACHED */
    190  1.16  leo 
    191  1.16  leo 	eprintf("Kernel '%s' was loaded OK\r\n", kname);
    192  1.16  leo 	xexit(0);
    193  1.16  leo 	return 0;
    194  1.16  leo }
    195  1.16  leo 
    196  1.10  leo void
    197  1.19  dsl get_sys_info(osdsc_t *od)
    198   1.1  leo {
    199   1.1  leo 	long	stck;
    200   1.1  leo 
    201   1.1  leo 	stck = Super(0);
    202   1.7  leo 
    203  1.17  leo 	sys_info(od);
    204   1.1  leo 
    205  1.17  leo 	if (!(od->cputype & ATARI_ANYCPU))
    206  1.10  leo 		fatal(-1, "Cannot determine CPU-type");
    207   1.1  leo 
    208  1.10  leo 	(void)Super(stck);
    209  1.17  leo 	if (s_flag)
    210  1.17  leo  		od->ttmem_size = od->ttmem_start = 0;
    211   1.1  leo }
    212   1.1  leo 
    213  1.10  leo void
    214  1.10  leo help()
    215   1.1  leo {
    216  1.10  leo 	eprintf("\r
    217   1.3  leo NetBSD loader for the Atari-TT\r
    218   1.3  leo \r
    219  1.10  leo Usage: %s [-abdhstVD] [-S <stram-size>] [-T <ttram-size>] [kernel]\r
    220   1.3  leo \r
    221   1.3  leo Description of options:\r
    222   1.3  leo \r
    223   1.3  leo \t-a  Boot up to multi-user mode.\r
    224   1.3  leo \t-b  Ask for root device to use.\r
    225   1.3  leo \t-d  Enter kernel debugger.\r
    226  1.10  leo \t-D  printout debug information while loading\r
    227  1.11  leo \t-h  What you're getting right now.\r
    228  1.16  leo `t-N  No symbols must be loaded.\r
    229  1.10  leo \t-o  Write output to both <output file> and stdout.\r
    230   1.3  leo \t-s  Use only ST-compatible RAM\r
    231   1.3  leo \t-S  Set amount of ST-compatible RAM\r
    232   1.5  leo \t-T  Set amount of TT-compatible RAM\r
    233   1.3  leo \t-t  Test the loader. It will do everything except executing the\r
    234   1.3  leo \t    loaded kernel.\r
    235  1.10  leo \t-V  Print loader version.\r
    236  1.10  leo \t-w  Wait for a keypress before exiting.\r
    237   1.1  leo ", Progname);
    238  1.10  leo 	xexit(0);
    239   1.1  leo }
    240   1.1  leo 
    241  1.10  leo void
    242  1.10  leo usage()
    243   1.1  leo {
    244  1.10  leo 	eprintf("Usage: %s [-abdhstVD] [-S <stram-size>] "
    245  1.10  leo 		"[-T <ttram-size>] [kernel]\r\n", Progname);
    246  1.10  leo 	xexit(1);
    247   1.3  leo }
    248   1.3  leo 
    249  1.10  leo void
    250  1.19  dsl start_kernel(osdsc_t *od)
    251   1.1  leo {
    252   1.1  leo 	long	stck;
    253   1.1  leo 
    254   1.1  leo 	stck = Super(0);
    255  1.17  leo 	bsd_startup(&(od->kp));
    256   1.1  leo 	/* NOT REACHED */
    257   1.1  leo 
    258  1.10  leo 	(void)Super(stck);
    259   1.1  leo }
    260