Home | History | Annotate | Line # | Download | only in hpcboot
hpcboot.cpp revision 1.9
      1 /*	$NetBSD: hpcboot.cpp,v 1.9 2003/12/18 12:15:40 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 			error_message = TEXT("Canceled by user.\n");
    217 			goto failed_exit;
    218 	}
    219 
    220 	f._arch->jump(p, f._loader->tagStart());
    221 	// NOTREACHED
    222 
    223  file_close_exit:
    224 	if (error_message == 0)
    225 		error_message = TEXT("Can't jump to the kernel.\n");
    226 	f._file->close();
    227  failed_exit:
    228 	MessageBox(menu._root->_window, error_message,
    229 	    TEXT("BOOT FAILED"), MB_ICONERROR | MB_OK);
    230 }
    231 
    232 //
    233 // HPCBOOT main loop
    234 //
    235 int
    236 HpcBootApp::run(void)
    237 {
    238 	MSG msg;
    239 
    240 	while (GetMessage(&msg, 0, 0, 0)) {
    241 		// cancel auto-boot.
    242 		if (HPC_PREFERENCE.auto_boot > 0 && _root &&
    243 		    (msg.message == WM_KEYDOWN ||
    244 			msg.message == WM_LBUTTONDOWN)) {
    245 			_root->disableTimer();
    246 		}
    247 		if (!_root->isDialogMessage(msg)) {
    248 			TranslateMessage(&msg);
    249 			DispatchMessage(&msg);
    250 		}
    251 	}
    252 	return msg.wParam;
    253 }
    254 
    255 BOOL
    256 HpcBootApp::registerClass(WNDPROC proc)
    257 {
    258 	TCHAR *wc_name;
    259 	WNDCLASS wc;
    260 
    261 	memset(&wc, 0, sizeof(WNDCLASS));
    262 	wc_name		= reinterpret_cast <TCHAR *>
    263 	    (LoadString(_instance, IDS_HPCMENU, 0, 0));
    264 	wc.lpfnWndProc	= proc;
    265 	wc.hInstance	= _instance;
    266 	wc.hIcon	= LoadIcon(_instance, MAKEINTRESOURCE(IDI_ICON));
    267 	wc.cbWndExtra	= 4;		// pointer of `this`
    268 	wc.hbrBackground= static_cast <HBRUSH>(GetStockObject(LTGRAY_BRUSH));
    269 	wc.lpszClassName= wc_name;
    270 
    271 	return RegisterClass(&wc);
    272 }
    273 
    274 
    275 //
    276 // Debug support.
    277 //
    278 void
    279 _bitdisp(u_int32_t a, int s, int e, int m, int c)
    280 {
    281 	u_int32_t j, j1;
    282 	int i, n;
    283 
    284 	DPRINTF_SETUP();
    285 
    286 	n = 31;	// 32bit only.
    287 	j1 = 1 << n;
    288 	e = e ? e : n;
    289 	for (j = j1, i = n; j > 0; j >>=1, i--) {
    290 		if (i > e || i < s) {
    291 			DPRINTF((TEXT("%c"), a & j ? '+' : '-'));
    292 		} else {
    293 			DPRINTF((TEXT("%c"), a & j ? '|' : '.'));
    294 		}
    295 	}
    296 	if (m) {
    297 		DPRINTF((TEXT("[%s]"),(char*)m));
    298 	}
    299 
    300 	DPRINTF((TEXT(" [0x%08x]"), a));
    301 
    302 	if (c) {
    303 		for (j = j1, i = n; j > 0; j >>=1, i--) {
    304 			if (!(i > e || i < s) &&(a & j)) {
    305 				DPRINTF((TEXT(" %d"), i));
    306 			}
    307 		}
    308 	}
    309 
    310 	DPRINTF((TEXT(" %d\n"), a));
    311 }
    312 
    313 void
    314 _dbg_bit_print(u_int32_t reg, u_int32_t mask, const char *name)
    315 {
    316 	static const char onoff[3] = "_x";
    317 
    318 	DPRINTF_SETUP();
    319 
    320 	DPRINTF((TEXT("%S[%c] "), name, onoff[reg & mask ? 1 : 0]));
    321 }
    322