xref: /netbsd-src/sys/arch/atari/stand/tostools/loadbsd/loadbsd.c (revision df7f595ecd6efe54ea7c11083e2dbf711cad4b31)
1 /*	$NetBSD: loadbsd.c,v 1.20 2009/03/18 10:22:26 cegger 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.20 $";
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(int argc, char **argv)
72 {
73 	/*
74 	 * Option parsing
75 	 */
76 	extern	int	optind;
77 	extern	char	*optarg;
78 	int		ch, err;
79 	char		*errmsg;
80 	int		fd;
81 	osdsc_t		*od;
82 
83 	init_toslib(argv[0]);
84 	Progname = argv[0];
85 
86 	od = &kernelparms;
87 	od->boothowto = RB_SINGLE;
88 
89 	while ((ch = getopt(argc, argv, "abdDhNstVwo:S:T:")) != -1) {
90 		switch (ch) {
91 		case 'a':
92 			od->boothowto &= ~(RB_SINGLE);
93 			od->boothowto |= RB_AUTOBOOT;
94 			break;
95 		case 'b':
96 			od->boothowto |= RB_ASKNAME;
97 			break;
98 		case 'd':
99 			od->boothowto |= RB_KDB;
100 			break;
101 		case 'D':
102 			d_flag = 1;
103 			break;
104 		case 'h':
105 			h_flag = 1;
106 			break;
107 		case 'N':
108 			N_flag = 1;
109 			break;
110 		case 'o':
111 			redirect_output(optarg);
112 			break;
113 		case 's':
114 			s_flag = 1;
115 			break;
116 		case 'S':
117 			od->stmem_size = atoi(optarg);
118 			break;
119 		case 't':
120 			t_flag = 1;
121 			break;
122 		case 'T':
123 			od->ttmem_size = atoi(optarg);
124 			break;
125 		case 'V':
126 			v_flag = 1;
127 			break;
128 		case 'w':
129 			set_wait_for_key();
130 			break;
131 		default:
132 			usage();
133 		}
134 	}
135 	argc -= optind;
136 	argv += optind;
137 	if (argc == 1)
138 		strcpy(kname, argv[0]);
139 
140 	if (h_flag)
141 		help();
142 	if (v_flag)
143 		eprintf("%s\r\n", version);
144 
145 	/*
146 	 * Get system info to pass to NetBSD
147 	 */
148 	get_sys_info(od);
149 	if (d_flag) {
150 	    eprintf("Machine info:\r\n");
151 	    eprintf("ST-RAM size\t: %10d bytes\r\n",od->stmem_size);
152 	    eprintf("TT-RAM size\t: %10d bytes\r\n",od->ttmem_size);
153 	    eprintf("TT-RAM start\t: 0x%08x\r\n", od->ttmem_start);
154 	    eprintf("Cpu-type\t: 0x%08x\r\n", od->cputype);
155 	}
156 
157 	/*
158 	 * Find the kernel to boot and read it's exec-header
159 	 */
160 	if ((fd = open(kname, O_RDONLY)) < 0)
161 		fatal(-1, "Cannot open kernel '%s'", kname);
162 	if ((err = elf_load(fd, od, &errmsg, !N_flag)) == -1) {
163 		/*
164 		 * Not ELF, try a.out
165 		 */
166 		if (err = aout_load(fd, od, &errmsg, !N_flag)) {
167 			if (err == -1)
168 				errmsg = "Not an ELF or NMAGIC file '%s'";
169 			fatal(-1, errmsg, kname);
170 		}
171 	}
172 	else {
173 		if (err)
174 			fatal(-1, errmsg);
175 	}
176 
177 	close(fd);
178 
179 	if (d_flag) {
180 	    eprintf("\r\nKernel info:\r\n");
181 	    eprintf("Kernel loadaddr\t: 0x%08x\r\n", od->kstart);
182 	    eprintf("Kernel size\t: %10d bytes\r\n", od->ksize);
183 	    eprintf("Kernel entry\t: 0x%08x\r\n", od->kentry);
184 	    eprintf("Kernel esym\t: 0x%08x\r\n", od->k_esym);
185 	}
186 
187 	if (!t_flag)
188 		start_kernel(od);
189 		/* NOT REACHED */
190 
191 	eprintf("Kernel '%s' was loaded OK\r\n", kname);
192 	xexit(0);
193 	return 0;
194 }
195 
196 void
197 get_sys_info(osdsc_t *od)
198 {
199 	long	stck;
200 
201 	stck = Super(0);
202 
203 	sys_info(od);
204 
205 	if (!(od->cputype & ATARI_ANYCPU))
206 		fatal(-1, "Cannot determine CPU-type");
207 
208 	(void)Super(stck);
209 	if (s_flag)
210  		od->ttmem_size = od->ttmem_start = 0;
211 }
212 
213 void
214 help(void)
215 {
216 	eprintf("\r
217 NetBSD loader for the Atari-TT\r
218 \r
219 Usage: %s [-abdhstVD] [-S <stram-size>] [-T <ttram-size>] [kernel]\r
220 \r
221 Description of options:\r
222 \r
223 \t-a  Boot up to multi-user mode.\r
224 \t-b  Ask for root device to use.\r
225 \t-d  Enter kernel debugger.\r
226 \t-D  printout debug information while loading\r
227 \t-h  What you're getting right now.\r
228 `t-N  No symbols must be loaded.\r
229 \t-o  Write output to both <output file> and stdout.\r
230 \t-s  Use only ST-compatible RAM\r
231 \t-S  Set amount of ST-compatible RAM\r
232 \t-T  Set amount of TT-compatible RAM\r
233 \t-t  Test the loader. It will do everything except executing the\r
234 \t    loaded kernel.\r
235 \t-V  Print loader version.\r
236 \t-w  Wait for a keypress before exiting.\r
237 ", Progname);
238 	xexit(0);
239 }
240 
241 void
242 usage(void)
243 {
244 	eprintf("Usage: %s [-abdhstVD] [-S <stram-size>] "
245 		"[-T <ttram-size>] [kernel]\r\n", Progname);
246 	xexit(1);
247 }
248 
249 void
250 start_kernel(osdsc_t *od)
251 {
252 	long	stck;
253 
254 	stck = Super(0);
255 	bsd_startup(&(od->kp));
256 	/* NOT REACHED */
257 
258 	(void)Super(stck);
259 }
260