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