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