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