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