hpcboot.cpp revision 1.15 1 /* $NetBSD: hpcboot.cpp,v 1.15 2004/08/06 17:21:28 uch Exp $ */
2
3 /*-
4 * Copyright (c) 2001, 2002, 2004 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 #if _WIN32_WCE <= 200
54 OSVERSIONINFO WinCEVersion;
55 #else
56 OSVERSIONINFOW WinCEVersion;
57 #endif
58
59 int WINAPI
60 WinMain(HINSTANCE instance, HINSTANCE prev_instance,
61 LPTSTR cmd_line, int window_show)
62 {
63 HpcMenuInterface::Instance(); // Menu System
64 HpcBootApp *app = 0; // Application body.
65 int ret = 0;
66
67 InitCommonControls();
68
69 app = new HpcBootApp(instance);
70 app->_cons = Console::Instance();
71 app->_root = new RootWindow(*app);
72
73 if (!app->registerClass(reinterpret_cast <WNDPROC>(Window::_wnd_proc)))
74 goto failed;
75
76 if (!app->_root->create(0))
77 goto failed;
78
79 Boot::Instance(); // Boot loader
80
81 ret = app->run(); // Main loop.
82 // NOTREACHED
83
84 failed:
85
86 Boot::Destroy();
87 if (app->_root)
88 delete app->_root;
89 delete app;
90 Console::Destroy();
91 HpcMenuInterface::Destroy();
92
93 return ret;
94 }
95
96 //
97 // boot sequence.
98 //
99 void
100 hpcboot(void *arg)
101 {
102 size_t sz = 0;
103 paddr_t p = 0;
104 TCHAR *error_message = 0;
105
106 HpcMenuInterface &menu = HPC_MENU;
107 Boot &f = Boot::Instance();
108
109 // Open serial port for kernel KGDB.
110 SerialConsole::OpenCOM1();
111
112 menu.progress("0");
113 if (!f.setup()) {
114 error_message = TEXT("Architecture not supported.\n");
115 goto failed_exit;
116 }
117
118 menu.progress("1");
119 if (!f.create()) {
120 error_message = TEXT("Architecture ops. not found.\n");
121 goto failed_exit;
122 }
123
124 menu.progress("2");
125 if (!f._arch->init()) {
126 error_message = TEXT("Architecture initialize failed.\n");
127 goto failed_exit;
128 }
129
130 f._arch->systemInfo();
131
132 menu.progress("3");
133 // kernel / file system image directory.
134 if (!f._file->setRoot(f.args.fileRoot)) {
135 error_message = TEXT("Can't set root directory.\n");
136 goto failed_exit;
137 }
138
139 // determine the size of file system image.
140 if (f.args.loadmfs)
141 {
142 if (!f._file->open(f.args.mfsName)) {
143 error_message =
144 TEXT("Can't open file system image.\n");
145 goto failed_exit;
146 }
147 sz = f._file->realsize();
148 sz = f._mem->roundPage(sz);
149 f._file->close();
150 }
151
152 menu.progress("4");
153 if (!f._file->open(f.args.fileName)) {
154 error_message = TEXT("Can't open kernel image.\n");
155 goto failed_exit;
156 }
157
158 menu.progress("5");
159 // put kernel to loader.
160 if (!f.attachLoader()) {
161 error_message = TEXT("Can't attach loader.\n");
162 goto file_close_exit;
163 }
164
165 if (!f._loader->setFile(f._file)) {
166 error_message = TEXT("Can't initialize loader.\n");
167 goto file_close_exit;
168 }
169
170 menu.progress("6");
171 sz += f._mem->roundPage(f._loader->memorySize());
172
173 // allocate required memory.
174 if (!f._arch->allocateMemory(sz)) {
175 error_message = TEXT("Can't allocate memory.\n");
176 goto file_close_exit;
177 }
178
179 menu.progress("7");
180 // load kernel to memory.
181 if (!f._arch->setupLoader()) {
182 error_message = TEXT("Can't set up loader.\n");
183 goto file_close_exit;
184 }
185
186 menu.progress("8");
187 if (!f._loader->load()) {
188 error_message = TEXT("Can't load kernel image to memory.\n");
189 goto file_close_exit;
190 }
191 menu.progress("9");
192 f._file->close();
193
194 // load file system image to memory
195 if (f.args.loadmfs) {
196 if (!f._file->open(f.args.mfsName)) {
197 error_message =
198 TEXT("Can't open file system image.\n");
199 goto failed_exit;
200 }
201 if (!f._loader->loadExtData()) {
202 error_message =
203 TEXT("Can't load file system image to memory.\n");
204 goto file_close_exit;
205 }
206 f._file->close();
207 }
208 f._loader->loadEnd();
209
210 // setup arguments for kernel.
211 p = f._arch->setupBootInfo(*f._loader);
212
213 menu.progress("10");
214
215 f._loader->tagDump(3); // dump page chain.(print first 3 links)
216
217 // jump to kernel entry.
218 if (HPC_PREFERENCE.pause_before_boot) {
219 if (MessageBox(menu._root->_window, TEXT("Push YES to boot."),
220 TEXT("Last chance..."),
221 MB_ICONWARNING | MB_YESNO) != IDYES)
222 {
223 error_message = TEXT("Canceled by user.\n");
224 goto failed_exit;
225 }
226 // redraw areas damaged by the dialog
227 UpdateWindow(menu._root->_window);
228 }
229
230 f._arch->jump(p, f._loader->tagStart());
231 // NOTREACHED
232 error_message = TEXT("Can't jump to the kernel.\n");
233
234 file_close_exit:
235 f._file->close();
236
237 failed_exit:
238 if (error_message == 0)
239 error_message = TEXT("Unknown error?\n");
240 MessageBox(menu._root->_window, error_message,
241 TEXT("BOOT FAILED"), MB_ICONERROR | MB_OK);
242
243 menu.unprogress(); // rewind progress bar
244 }
245
246 //
247 // HPCBOOT main loop
248 //
249 int
250 HpcBootApp::run(void)
251 {
252 MSG msg;
253
254 while (GetMessage(&msg, 0, 0, 0)) {
255 // cancel auto-boot.
256 if (HPC_PREFERENCE.auto_boot > 0 && _root &&
257 (msg.message == WM_KEYDOWN ||
258 msg.message == WM_LBUTTONDOWN)) {
259 _root->disableTimer();
260 }
261 if (!_root->isDialogMessage(msg)) {
262 TranslateMessage(&msg);
263 DispatchMessage(&msg);
264 }
265 }
266 return msg.wParam;
267 }
268
269 BOOL
270 HpcBootApp::registerClass(WNDPROC proc)
271 {
272 TCHAR *wc_name;
273 WNDCLASS wc;
274
275 memset(&wc, 0, sizeof(WNDCLASS));
276 wc_name = reinterpret_cast <TCHAR *>
277 (LoadString(_instance, IDS_HPCMENU, 0, 0));
278 wc.lpfnWndProc = proc;
279 wc.hInstance = _instance;
280 wc.hIcon = LoadIcon(_instance, MAKEINTRESOURCE(IDI_ICON));
281 wc.cbWndExtra = 4; // pointer of `this`
282 wc.hbrBackground= static_cast <HBRUSH>(GetStockObject(LTGRAY_BRUSH));
283 wc.lpszClassName= wc_name;
284
285 return RegisterClass(&wc);
286 }
287
288
289 //
290 // Debug support.
291 //
292 void
293 _bitdisp(u_int32_t a, int s, int e, int m, int c)
294 {
295 u_int32_t j, j1;
296 int i, n;
297
298 DPRINTF_SETUP();
299
300 n = 31; // 32bit only.
301 j1 = 1 << n;
302 e = e ? e : n;
303 for (j = j1, i = n; j > 0; j >>=1, i--) {
304 if (i > e || i < s) {
305 DPRINTF((TEXT("%c"), a & j ? '+' : '-'));
306 } else {
307 DPRINTF((TEXT("%c"), a & j ? '|' : '.'));
308 }
309 }
310 if (m) {
311 DPRINTF((TEXT("[%s]"),(char*)m));
312 }
313
314 DPRINTF((TEXT(" [0x%08x]"), a));
315
316 if (c) {
317 for (j = j1, i = n; j > 0; j >>=1, i--) {
318 if (!(i > e || i < s) &&(a & j)) {
319 DPRINTF((TEXT(" %d"), i));
320 }
321 }
322 }
323
324 DPRINTF((TEXT(" %d\n"), a));
325 }
326
327 void
328 _dbg_bit_print(u_int32_t reg, u_int32_t mask, const char *name)
329 {
330 static const char onoff[3] = "_x";
331
332 DPRINTF_SETUP();
333
334 DPRINTF((TEXT("%S[%c] "), name, onoff[reg & mask ? 1 : 0]));
335 }
336