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