hpcboot.cpp revision 1.11 1 /* $NetBSD: hpcboot.cpp,v 1.11 2003/12/23 03:18:13 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 goto file_close_exit;
158
159 if (!f._loader->setFile(f._file)) {
160 error_message = TEXT("Can't initialize loader.\n");
161 goto file_close_exit;
162 }
163
164 menu.progress();
165 sz += f._mem->roundPage(f._loader->memorySize());
166
167 // allocate required memory.
168 if (!f._arch->allocateMemory(sz)) {
169 error_message = TEXT("Can't allocate memory.\n");
170 goto file_close_exit;
171 }
172
173 menu.progress();
174 // load kernel to memory.
175 if (!f._arch->setupLoader()) {
176 error_message = TEXT("Can't set up loader.\n");
177 goto file_close_exit;
178 }
179
180 menu.progress();
181 if (!f._loader->load()) {
182 error_message = TEXT("Can't load kernel image to memory.\n");
183 goto file_close_exit;
184 }
185 menu.progress();
186 f._file->close();
187
188 // load file system image to memory
189 if (f.args.loadmfs) {
190 if (!f._file->open(f.args.mfsName)) {
191 error_message =
192 TEXT("Can't open file system image.\n");
193 goto failed_exit;
194 }
195 if (!f._loader->loadExtData()) {
196 error_message =
197 TEXT("Can't load file system image to memory.\n");
198 goto file_close_exit;
199 }
200 f._file->close();
201 }
202 f._loader->loadEnd();
203
204 // setup arguments for kernel.
205 p = f._arch->setupBootInfo(*f._loader);
206
207 menu.progress();
208
209 f._loader->tagDump(3); // dump page chain.(print first 3 links)
210
211 // jump to kernel entry.
212 if (HPC_PREFERENCE.pause_before_boot) {
213 if (MessageBox(menu._root->_window, TEXT("Push YES to boot."),
214 TEXT("Last chance..."),
215 MB_ICONWARNING | MB_YESNO) != IDYES)
216 {
217 error_message = TEXT("Canceled by user.\n");
218 goto failed_exit;
219 }
220 }
221
222 f._arch->jump(p, f._loader->tagStart());
223 // NOTREACHED
224
225 file_close_exit:
226 if (error_message == 0)
227 error_message = TEXT("Can't jump to the kernel.\n");
228 f._file->close();
229 failed_exit:
230 MessageBox(menu._root->_window, error_message,
231 TEXT("BOOT FAILED"), MB_ICONERROR | MB_OK);
232
233 menu.unprogress(); // rewind progress bar
234 }
235
236 //
237 // HPCBOOT main loop
238 //
239 int
240 HpcBootApp::run(void)
241 {
242 MSG msg;
243
244 while (GetMessage(&msg, 0, 0, 0)) {
245 // cancel auto-boot.
246 if (HPC_PREFERENCE.auto_boot > 0 && _root &&
247 (msg.message == WM_KEYDOWN ||
248 msg.message == WM_LBUTTONDOWN)) {
249 _root->disableTimer();
250 }
251 if (!_root->isDialogMessage(msg)) {
252 TranslateMessage(&msg);
253 DispatchMessage(&msg);
254 }
255 }
256 return msg.wParam;
257 }
258
259 BOOL
260 HpcBootApp::registerClass(WNDPROC proc)
261 {
262 TCHAR *wc_name;
263 WNDCLASS wc;
264
265 memset(&wc, 0, sizeof(WNDCLASS));
266 wc_name = reinterpret_cast <TCHAR *>
267 (LoadString(_instance, IDS_HPCMENU, 0, 0));
268 wc.lpfnWndProc = proc;
269 wc.hInstance = _instance;
270 wc.hIcon = LoadIcon(_instance, MAKEINTRESOURCE(IDI_ICON));
271 wc.cbWndExtra = 4; // pointer of `this`
272 wc.hbrBackground= static_cast <HBRUSH>(GetStockObject(LTGRAY_BRUSH));
273 wc.lpszClassName= wc_name;
274
275 return RegisterClass(&wc);
276 }
277
278
279 //
280 // Debug support.
281 //
282 void
283 _bitdisp(u_int32_t a, int s, int e, int m, int c)
284 {
285 u_int32_t j, j1;
286 int i, n;
287
288 DPRINTF_SETUP();
289
290 n = 31; // 32bit only.
291 j1 = 1 << n;
292 e = e ? e : n;
293 for (j = j1, i = n; j > 0; j >>=1, i--) {
294 if (i > e || i < s) {
295 DPRINTF((TEXT("%c"), a & j ? '+' : '-'));
296 } else {
297 DPRINTF((TEXT("%c"), a & j ? '|' : '.'));
298 }
299 }
300 if (m) {
301 DPRINTF((TEXT("[%s]"),(char*)m));
302 }
303
304 DPRINTF((TEXT(" [0x%08x]"), a));
305
306 if (c) {
307 for (j = j1, i = n; j > 0; j >>=1, i--) {
308 if (!(i > e || i < s) &&(a & j)) {
309 DPRINTF((TEXT(" %d"), i));
310 }
311 }
312 }
313
314 DPRINTF((TEXT(" %d\n"), a));
315 }
316
317 void
318 _dbg_bit_print(u_int32_t reg, u_int32_t mask, const char *name)
319 {
320 static const char onoff[3] = "_x";
321
322 DPRINTF_SETUP();
323
324 DPRINTF((TEXT("%S[%c] "), name, onoff[reg & mask ? 1 : 0]));
325 }
326