1*ce099b40Smartin /* $NetBSD: hpcboot.cpp,v 1.20 2008/04/28 20:23:20 martin Exp $ */
29173eae7Such
39173eae7Such /*-
4a28cfcb5Such * Copyright (c) 2001, 2002, 2004 The NetBSD Foundation, Inc.
59173eae7Such * All rights reserved.
69173eae7Such *
79173eae7Such * This code is derived from software contributed to The NetBSD Foundation
89173eae7Such * by UCHIYAMA Yasushi.
99173eae7Such *
109173eae7Such * Redistribution and use in source and binary forms, with or without
119173eae7Such * modification, are permitted provided that the following conditions
129173eae7Such * are met:
139173eae7Such * 1. Redistributions of source code must retain the above copyright
149173eae7Such * notice, this list of conditions and the following disclaimer.
159173eae7Such * 2. Redistributions in binary form must reproduce the above copyright
169173eae7Such * notice, this list of conditions and the following disclaimer in the
179173eae7Such * documentation and/or other materials provided with the distribution.
189173eae7Such *
199173eae7Such * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
209173eae7Such * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
219173eae7Such * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
229173eae7Such * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
239173eae7Such * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
249173eae7Such * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
259173eae7Such * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
269173eae7Such * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
279173eae7Such * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
289173eae7Such * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
299173eae7Such * POSSIBILITY OF SUCH DAMAGE.
309173eae7Such */
319173eae7Such
329173eae7Such #include <hpcmenu.h>
339173eae7Such #include <hpcboot.h>
349173eae7Such
359173eae7Such #include <menu/window.h>
369173eae7Such #include <menu/rootwindow.h>
379173eae7Such
389173eae7Such #include <console.h>
399173eae7Such #include <arch.h>
409173eae7Such #include <memory.h>
419173eae7Such #include <file.h>
429173eae7Such #include <load.h>
439173eae7Such
449173eae7Such #include <boot.h>
459173eae7Such
4600539717Such #include "../binary/build_number.h"
4700539717Such
48a28cfcb5Such #if _WIN32_WCE <= 200
49a28cfcb5Such OSVERSIONINFO WinCEVersion;
50a28cfcb5Such #else
512a2cc9f4Such OSVERSIONINFOW WinCEVersion;
52a28cfcb5Such #endif
532a2cc9f4Such
549173eae7Such int WINAPI
WinMain(HINSTANCE instance,HINSTANCE prev_instance,LPTSTR cmd_line,int window_show)559173eae7Such WinMain(HINSTANCE instance, HINSTANCE prev_instance,
569173eae7Such LPTSTR cmd_line, int window_show)
579173eae7Such {
58bd926f64Such HpcMenuInterface::Instance(); // Menu System
599173eae7Such HpcBootApp *app = 0; // Application body.
609173eae7Such int ret = 0;
619173eae7Such
629173eae7Such InitCommonControls();
639173eae7Such
649173eae7Such app = new HpcBootApp(instance);
659173eae7Such app->_cons = Console::Instance();
669173eae7Such app->_root = new RootWindow(*app);
679173eae7Such
689173eae7Such if (!app->registerClass(reinterpret_cast <WNDPROC>(Window::_wnd_proc)))
699173eae7Such goto failed;
709173eae7Such
719173eae7Such if (!app->_root->create(0))
729173eae7Such goto failed;
739173eae7Such
74bd926f64Such Boot::Instance(); // Boot loader
759173eae7Such
76bd926f64Such ret = app->run(); // Main loop.
779173eae7Such // NOTREACHED
789173eae7Such
799173eae7Such failed:
809173eae7Such
819173eae7Such Boot::Destroy();
829173eae7Such if (app->_root)
839173eae7Such delete app->_root;
849173eae7Such delete app;
859173eae7Such Console::Destroy();
869173eae7Such HpcMenuInterface::Destroy();
879173eae7Such
889173eae7Such return ret;
899173eae7Such }
909173eae7Such
91bd926f64Such //
92bd926f64Such // boot sequence.
93bd926f64Such //
949173eae7Such void
hpcboot(void * arg)95bd926f64Such hpcboot(void *arg)
969173eae7Such {
979173eae7Such size_t sz = 0;
989173eae7Such paddr_t p = 0;
999173eae7Such TCHAR *error_message = 0;
1009173eae7Such
101bd926f64Such HpcMenuInterface &menu = HPC_MENU;
102bd926f64Such Boot &f = Boot::Instance();
103bd926f64Such
1042a2cc9f4Such // Open serial port for kernel KGDB.
1052a2cc9f4Such SerialConsole::OpenCOM1();
1062a2cc9f4Such
107a28cfcb5Such menu.progress("0");
1087758e172Such if (!f.setup()) {
109f578e43dSuwe error_message = TEXT("Architecture not supported.\n");
1107758e172Such goto failed_exit;
1117758e172Such }
1129173eae7Such
113a28cfcb5Such menu.progress("1");
1147758e172Such if (!f.create()) {
115f578e43dSuwe error_message = TEXT("Architecture ops. not found.\n");
1167758e172Such goto failed_exit;
1177758e172Such }
1189173eae7Such
11900539717Such // Now we can write console which user specified.
12000539717Such {
12100539717Such DPRINTF_SETUP();
12200539717Such DPRINTF((TEXT("hpcboot build number: %d\n"),
12300539717Such HPCBOOT_BUILD_NUMBER));
12400539717Such DPRINTF((TEXT("%s (cpu=0x%08x machine=0x%08x)\n"),
12500539717Such HPC_MENU.platform_get(HPC_MENU.platform_default()),
12600539717Such HPC_PREFERENCE.platid_hi, HPC_PREFERENCE.platid_lo));
12700539717Such }
12800539717Such
129a28cfcb5Such menu.progress("2");
1309173eae7Such if (!f._arch->init()) {
131f578e43dSuwe error_message = TEXT("Architecture initialize failed.\n");
1327758e172Such goto failed_exit;
1339173eae7Such }
1349173eae7Such
1359173eae7Such f._arch->systemInfo();
1369173eae7Such
137a28cfcb5Such menu.progress("3");
1389173eae7Such // kernel / file system image directory.
1399173eae7Such if (!f._file->setRoot(f.args.fileRoot)) {
140f578e43dSuwe error_message = TEXT("Can't set root directory.\n");
1417758e172Such goto failed_exit;
1429173eae7Such }
1439173eae7Such
144d6be11a5Suwe // determine the size of file system image.
1459173eae7Such if (f.args.loadmfs)
1469173eae7Such {
1479173eae7Such if (!f._file->open(f.args.mfsName)) {
1489173eae7Such error_message =
149f578e43dSuwe TEXT("Can't open file system image.\n");
1507758e172Such goto failed_exit;
1519173eae7Such }
152d6be11a5Suwe sz = f._file->realsize();
153984e0193Such sz = f._mem->roundPage(sz);
1549173eae7Such f._file->close();
1559173eae7Such }
1569173eae7Such
157a28cfcb5Such menu.progress("4");
1589173eae7Such if (!f._file->open(f.args.fileName)) {
159f578e43dSuwe error_message = TEXT("Can't open kernel image.\n");
1602a2cc9f4Such goto failed_exit;
1619173eae7Such }
1629173eae7Such
163a28cfcb5Such menu.progress("5");
1649173eae7Such // put kernel to loader.
1650c9676c9Suwe if (!f.attachLoader()) {
1660c9676c9Suwe error_message = TEXT("Can't attach loader.\n");
1672a2cc9f4Such goto file_close_exit;
1680c9676c9Suwe }
1699173eae7Such
1709173eae7Such if (!f._loader->setFile(f._file)) {
171f578e43dSuwe error_message = TEXT("Can't initialize loader.\n");
1722a2cc9f4Such goto file_close_exit;
1739173eae7Such }
1749173eae7Such
175a28cfcb5Such menu.progress("6");
176984e0193Such sz += f._mem->roundPage(f._loader->memorySize());
1779173eae7Such
1789173eae7Such // allocate required memory.
1799173eae7Such if (!f._arch->allocateMemory(sz)) {
180f578e43dSuwe error_message = TEXT("Can't allocate memory.\n");
1812a2cc9f4Such goto file_close_exit;
1829173eae7Such }
1839173eae7Such
184a28cfcb5Such menu.progress("7");
1859173eae7Such // load kernel to memory.
1869173eae7Such if (!f._arch->setupLoader()) {
187f578e43dSuwe error_message = TEXT("Can't set up loader.\n");
1882a2cc9f4Such goto file_close_exit;
1899173eae7Such }
1909173eae7Such
191a28cfcb5Such menu.progress("8");
1929173eae7Such if (!f._loader->load()) {
193f578e43dSuwe error_message = TEXT("Can't load kernel image to memory.\n");
1942a2cc9f4Such goto file_close_exit;
1959173eae7Such }
196a28cfcb5Such menu.progress("9");
19787aeed61Such f._file->close();
1989173eae7Such
1999173eae7Such // load file system image to memory
2009173eae7Such if (f.args.loadmfs) {
2012a2cc9f4Such if (!f._file->open(f.args.mfsName)) {
2022a2cc9f4Such error_message =
203f578e43dSuwe TEXT("Can't open file system image.\n");
2042a2cc9f4Such goto failed_exit;
2052a2cc9f4Such }
206984e0193Such if (!f._loader->loadExtData()) {
207984e0193Such error_message =
208f578e43dSuwe TEXT("Can't load file system image to memory.\n");
2092a2cc9f4Such goto file_close_exit;
210984e0193Such }
2119173eae7Such f._file->close();
2129173eae7Such }
2139173eae7Such f._loader->loadEnd();
2149173eae7Such
2159173eae7Such // setup arguments for kernel.
2169173eae7Such p = f._arch->setupBootInfo(*f._loader);
2179173eae7Such
218a28cfcb5Such menu.progress("10");
2199173eae7Such
2209173eae7Such f._loader->tagDump(3); // dump page chain.(print first 3 links)
2219173eae7Such
2229173eae7Such // jump to kernel entry.
223bd926f64Such if (HPC_PREFERENCE.pause_before_boot) {
2248b377aadSuwe if (MessageBox(menu._root->_window, TEXT("Push YES to boot."),
2258b377aadSuwe TEXT("Last chance..."),
226f578e43dSuwe MB_ICONWARNING | MB_YESNO) != IDYES)
227816df014Suwe {
228f578e43dSuwe error_message = TEXT("Canceled by user.\n");
2292a2cc9f4Such goto failed_exit;
2309173eae7Such }
2319f2e9e23Suwe // redraw areas damaged by the dialog
2329f2e9e23Suwe UpdateWindow(menu._root->_window);
233816df014Suwe }
2349173eae7Such
2359173eae7Such f._arch->jump(p, f._loader->tagStart());
2369173eae7Such // NOTREACHED
2370c9676c9Suwe error_message = TEXT("Can't jump to the kernel.\n");
2389173eae7Such
2392a2cc9f4Such file_close_exit:
24087aeed61Such f._file->close();
2410c9676c9Suwe
2427758e172Such failed_exit:
2430c9676c9Suwe if (error_message == 0)
2440c9676c9Suwe error_message = TEXT("Unknown error?\n");
2459173eae7Such MessageBox(menu._root->_window, error_message,
2468b377aadSuwe TEXT("BOOT FAILED"), MB_ICONERROR | MB_OK);
247f94c4399Suwe
248f94c4399Suwe menu.unprogress(); // rewind progress bar
2499173eae7Such }
2509173eae7Such
2519173eae7Such //
2529173eae7Such // HPCBOOT main loop
2539173eae7Such //
2549173eae7Such int
run(void)2559173eae7Such HpcBootApp::run(void)
2569173eae7Such {
2579173eae7Such MSG msg;
2589173eae7Such
2599173eae7Such while (GetMessage(&msg, 0, 0, 0)) {
2609173eae7Such // cancel auto-boot.
261bd926f64Such if (HPC_PREFERENCE.auto_boot > 0 && _root &&
2629173eae7Such (msg.message == WM_KEYDOWN ||
2639173eae7Such msg.message == WM_LBUTTONDOWN)) {
2649173eae7Such _root->disableTimer();
2659173eae7Such }
2669173eae7Such if (!_root->isDialogMessage(msg)) {
2679173eae7Such TranslateMessage(&msg);
2689173eae7Such DispatchMessage(&msg);
2699173eae7Such }
2709173eae7Such }
2719173eae7Such return msg.wParam;
2729173eae7Such }
2739173eae7Such
2749173eae7Such BOOL
registerClass(WNDPROC proc)2759173eae7Such HpcBootApp::registerClass(WNDPROC proc)
2769173eae7Such {
2779173eae7Such TCHAR *wc_name;
2789173eae7Such WNDCLASS wc;
2799173eae7Such
2809173eae7Such memset(&wc, 0, sizeof(WNDCLASS));
2819173eae7Such wc_name = reinterpret_cast <TCHAR *>
2829173eae7Such (LoadString(_instance, IDS_HPCMENU, 0, 0));
2839173eae7Such wc.lpfnWndProc = proc;
2849173eae7Such wc.hInstance = _instance;
2859173eae7Such wc.hIcon = LoadIcon(_instance, MAKEINTRESOURCE(IDI_ICON));
2869173eae7Such wc.cbWndExtra = 4; // pointer of `this`
2879173eae7Such wc.hbrBackground= static_cast <HBRUSH>(GetStockObject(LTGRAY_BRUSH));
2889173eae7Such wc.lpszClassName= wc_name;
2899173eae7Such
2909173eae7Such return RegisterClass(&wc);
2919173eae7Such }
292984e0193Such
293984e0193Such
294984e0193Such //
295984e0193Such // Debug support.
296984e0193Such //
297984e0193Such void
_bitdisp(uint32_t a,int s,int e,int m,int c)29824c8a902Suwe _bitdisp(uint32_t a, int s, int e, int m, int c)
299984e0193Such {
30024c8a902Suwe uint32_t j, j1;
301984e0193Such int i, n;
302984e0193Such
303984e0193Such DPRINTF_SETUP();
304984e0193Such
305984e0193Such n = 31; // 32bit only.
306984e0193Such j1 = 1 << n;
307984e0193Such e = e ? e : n;
308984e0193Such for (j = j1, i = n; j > 0; j >>=1, i--) {
309984e0193Such if (i > e || i < s) {
310984e0193Such DPRINTF((TEXT("%c"), a & j ? '+' : '-'));
311984e0193Such } else {
312984e0193Such DPRINTF((TEXT("%c"), a & j ? '|' : '.'));
313984e0193Such }
314984e0193Such }
315984e0193Such if (m) {
316984e0193Such DPRINTF((TEXT("[%s]"),(char*)m));
317984e0193Such }
318984e0193Such
319984e0193Such DPRINTF((TEXT(" [0x%08x]"), a));
320984e0193Such
321984e0193Such if (c) {
322984e0193Such for (j = j1, i = n; j > 0; j >>=1, i--) {
323984e0193Such if (!(i > e || i < s) &&(a & j)) {
324984e0193Such DPRINTF((TEXT(" %d"), i));
325984e0193Such }
326984e0193Such }
327984e0193Such }
328984e0193Such
329984e0193Such DPRINTF((TEXT(" %d\n"), a));
330984e0193Such }
331984e0193Such
332984e0193Such void
_dbg_bit_print(uint32_t reg,uint32_t mask,const char * name)33324c8a902Suwe _dbg_bit_print(uint32_t reg, uint32_t mask, const char *name)
334984e0193Such {
335984e0193Such static const char onoff[3] = "_x";
336984e0193Such
337984e0193Such DPRINTF_SETUP();
338984e0193Such
339984e0193Such DPRINTF((TEXT("%S[%c] "), name, onoff[reg & mask ? 1 : 0]));
340984e0193Such }
341