xref: /netbsd-src/sys/arch/hpc/stand/hpcboot/hpcboot.cpp (revision 0dd5877adce57db949b16ae963e5a6831cccdfb6)
1 /*	$NetBSD: hpcboot.cpp,v 1.6 2002/02/11 17:05:45 uch Exp $	*/
2 
3 /*-
4  * Copyright (c) 2001, 2002 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by UCHIYAMA Yasushi.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <hpcmenu.h>
40 #include <hpcboot.h>
41 
42 #include <menu/window.h>
43 #include <menu/rootwindow.h>
44 
45 #include <console.h>
46 #include <arch.h>
47 #include <memory.h>
48 #include <file.h>
49 #include <load.h>
50 
51 #include <boot.h>
52 
53 int WINAPI
54 WinMain(HINSTANCE instance, HINSTANCE prev_instance,
55     LPTSTR cmd_line, int window_show)
56 {
57 	HpcMenuInterface::Instance();	// Menu System
58 	HpcBootApp *app = 0;		// Application body.
59 	int ret = 0;
60 
61 	InitCommonControls();
62 
63 	app = new HpcBootApp(instance);
64 	app->_cons = Console::Instance();
65 	app->_root = new RootWindow(*app);
66 
67 	if (!app->registerClass(reinterpret_cast <WNDPROC>(Window::_wnd_proc)))
68 		goto failed;
69 
70 	if (!app->_root->create(0))
71 		goto failed;
72 
73 	Boot::Instance();	// Boot loader
74 
75 	ret = app->run();	// Main loop.
76 	// NOTREACHED
77 
78  failed:
79 
80 	Boot::Destroy();
81 	if (app->_root)
82 		delete app->_root;
83 	delete app;
84 	Console::Destroy();
85 	HpcMenuInterface::Destroy();
86 
87 	return ret;
88 }
89 
90 //
91 // boot sequence.
92 //
93 void
94 hpcboot(void *arg)
95 {
96 	size_t sz = 0;
97 	paddr_t p = 0;
98 	TCHAR *error_message = 0;
99 
100 	HpcMenuInterface &menu = HPC_MENU;
101 	Boot &f = Boot::Instance();
102 
103 	menu.progress();
104 	if (!f.setup()) {
105 		error_message = TEXT("architecture not supported.\n");
106 		goto failed_exit;
107 	}
108 
109 	menu.progress();
110 	if (!f.create()) {
111 		error_message = TEXT("architexture ops. not found.\n");
112 		goto failed_exit;
113 	}
114 
115 	menu.progress();
116 	if (!f._arch->init()) {
117 		error_message = TEXT("architecture initialize failed.\n");
118 		goto failed_exit;
119 	}
120 
121 	f._arch->systemInfo();
122 
123 	menu.progress();
124 	// kernel / file system image directory.
125 	if (!f._file->setRoot(f.args.fileRoot)) {
126 		error_message = TEXT("couldn't set root directory.\n");
127 		goto failed_exit;
128 	}
129 
130 	// open file system image.
131 	if (f.args.loadmfs)
132 	{
133 		if (!f._file->open(f.args.mfsName)) {
134 			error_message =
135 			    TEXT("couldn't open file system image.\n");
136 			goto failed_exit;
137 		}
138 		sz = f._file->size();
139 		sz = f._mem->roundPage(sz);
140 		f._file->close();
141 	}
142 
143 	menu.progress();
144 	if (!f._file->open(f.args.fileName)) {
145 		error_message = TEXT("couldn't open kernel image.\n");
146 		goto failed;
147 	}
148 
149 	menu.progress();
150 	// put kernel to loader.
151 	if (!f.attachLoader())
152 		goto failed;
153 
154 	if (!f._loader->setFile(f._file)) {
155 		error_message = TEXT("couldn't initialize loader.\n");
156 		goto failed;
157 	}
158 
159 	menu.progress();
160 	sz += f._mem->roundPage(f._loader->memorySize());
161 
162 	// allocate required memory.
163 	if (!f._arch->allocateMemory(sz)) {
164 		error_message = TEXT("couldn't allocate memory.\n");
165 		goto failed;
166 	}
167 
168 	menu.progress();
169 	// load kernel to memory.
170 	if (!f._arch->setupLoader()) {
171 		error_message = TEXT("couldn't set up loader.\n");
172 		goto failed;
173 	}
174 
175 	menu.progress();
176 	if (!f._loader->load()) {
177 		error_message = TEXT("couldn't load kernel image to memory.\n");
178 		goto failed;
179 	}
180 	menu.progress();
181 	f._file->close();
182 
183 	// load file system image to memory
184 	if (f.args.loadmfs) {
185 		f._file->open(f.args.mfsName);
186 		if (!f._loader->loadExtData()) {
187 			error_message =
188 			    TEXT("couldn't load filesystem image to memory.\n");
189 			goto failed;
190 		}
191 		f._file->close();
192 	}
193 	f._loader->loadEnd();
194 
195 	// setup arguments for kernel.
196 	p = f._arch->setupBootInfo(*f._loader);
197 
198 	menu.progress();
199 
200 	f._loader->tagDump(3); // dump page chain.(print first 3 links)
201 
202 	// jump to kernel entry.
203 	if (HPC_PREFERENCE.pause_before_boot) {
204 		if (MessageBox(menu._root->_window, TEXT("Push OK to boot."),
205 		    TEXT("Last chance..."), MB_YESNO) != IDYES)
206 			goto failed;
207 	}
208 
209 	f._arch->jump(p, f._loader->tagStart());
210 	// NOTREACHED
211 
212  failed:
213 	if (error_message == 0)
214 		error_message = TEXT("can't jump to kernel.\n");
215 	f._file->close();
216  failed_exit:
217 	MessageBox(menu._root->_window, error_message,
218 	    TEXT("BOOT FAILED"), 0);
219 }
220 
221 //
222 // HPCBOOT main loop
223 //
224 int
225 HpcBootApp::run(void)
226 {
227 	MSG msg;
228 
229 	while (GetMessage(&msg, 0, 0, 0)) {
230 		// cancel auto-boot.
231 		if (HPC_PREFERENCE.auto_boot > 0 && _root &&
232 		    (msg.message == WM_KEYDOWN ||
233 			msg.message == WM_LBUTTONDOWN)) {
234 			_root->disableTimer();
235 		}
236 		if (!_root->isDialogMessage(msg)) {
237 			TranslateMessage(&msg);
238 			DispatchMessage(&msg);
239 		}
240 	}
241 	return msg.wParam;
242 }
243 
244 BOOL
245 HpcBootApp::registerClass(WNDPROC proc)
246 {
247 	TCHAR *wc_name;
248 	WNDCLASS wc;
249 
250 	memset(&wc, 0, sizeof(WNDCLASS));
251 	wc_name		= reinterpret_cast <TCHAR *>
252 	    (LoadString(_instance, IDS_HPCMENU, 0, 0));
253 	wc.lpfnWndProc	= proc;
254 	wc.hInstance	= _instance;
255 	wc.hIcon	= LoadIcon(_instance, MAKEINTRESOURCE(IDI_ICON));
256 	wc.cbWndExtra	= 4;		// pointer of `this`
257 	wc.hbrBackground= static_cast <HBRUSH>(GetStockObject(LTGRAY_BRUSH));
258 	wc.lpszClassName= wc_name;
259 
260 	return RegisterClass(&wc);
261 }
262 
263 
264 //
265 // Debug support.
266 //
267 void
268 _bitdisp(u_int32_t a, int s, int e, int m, int c)
269 {
270 	u_int32_t j, j1;
271 	int i, n;
272 
273 	DPRINTF_SETUP();
274 
275 	n = 31;	// 32bit only.
276 	j1 = 1 << n;
277 	e = e ? e : n;
278 	for (j = j1, i = n; j > 0; j >>=1, i--) {
279 		if (i > e || i < s) {
280 			DPRINTF((TEXT("%c"), a & j ? '+' : '-'));
281 		} else {
282 			DPRINTF((TEXT("%c"), a & j ? '|' : '.'));
283 		}
284 	}
285 	if (m) {
286 		DPRINTF((TEXT("[%s]"),(char*)m));
287 	}
288 
289 	DPRINTF((TEXT(" [0x%08x]"), a));
290 
291 	if (c) {
292 		for (j = j1, i = n; j > 0; j >>=1, i--) {
293 			if (!(i > e || i < s) &&(a & j)) {
294 				DPRINTF((TEXT(" %d"), i));
295 			}
296 		}
297 	}
298 
299 	DPRINTF((TEXT(" %d\n"), a));
300 }
301 
302 void
303 _dbg_bit_print(u_int32_t reg, u_int32_t mask, const char *name)
304 {
305 	static const char onoff[3] = "_x";
306 
307 	DPRINTF_SETUP();
308 
309 	DPRINTF((TEXT("%S[%c] "), name, onoff[reg & mask ? 1 : 0]));
310 }
311