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