Home | History | Annotate | Line # | Download | only in hpcboot
hpcboot.cpp revision 1.1
      1 /*	$NetBSD: hpcboot.cpp,v 1.1 2001/02/09 18:34:41 uch Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001 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 int WINAPI
     54 WinMain(HINSTANCE instance, HINSTANCE prev_instance,
     55 	LPTSTR cmd_line, int window_show)
     56 {
     57 	HpcMenuInterface::Instance();
     58 	HpcBootApp *app = 0;	// Application body.
     59 	int ret = 0;
     60 
     61 	InitCommonControls();
     62 
     63 	app = new HpcBootApp(instance);
     64 	app->_cons = Console::Instance();
     65 	app->_root = new RootWindow(*app);
     66 
     67 	if (!app->registerClass(reinterpret_cast <WNDPROC>(Window::_wnd_proc)))
     68 		goto failed;
     69 
     70 	if (!app->_root->create(0))
     71 		goto failed;
     72 
     73 	Boot::Instance();
     74 
     75 	ret = app->run();
     76 	// NOTREACHED
     77 
     78  failed:
     79 
     80 	Boot::Destroy();
     81 	if (app->_root)
     82 		delete app->_root;
     83 	delete app;
     84 	Console::Destroy();
     85 	HpcMenuInterface::Destroy();
     86 
     87 	return ret;
     88 }
     89 
     90 void
     91 hpcboot(void *arg, struct HpcMenuInterface::HpcMenuPreferences &pref)
     92 {
     93 	size_t sz = 0;
     94 	paddr_t p = 0;
     95 	Boot &f = Boot::Instance();
     96 	HpcMenuInterface &menu = HpcMenuInterface::Instance();
     97 	Console *_cons = Console::Instance();
     98 	TCHAR *error_message = 0;
     99 
    100 	menu.progress();
    101 	if (!f.setup(pref))
    102 		return;
    103 
    104 	menu.progress();
    105 	if (!f.create())
    106 		return;
    107 
    108 	menu.progress();
    109 	if (!f._arch->init()) {
    110 		error_message = TEXT("architecture initialize failed.\n");
    111 		goto failed;
    112 	}
    113 
    114 	f._arch->systemInfo();
    115 
    116 	menu.progress();
    117 	// kernel / file system image directory.
    118 	if (!f._file->setRoot(f.args.fileRoot)) {
    119 		error_message = TEXT("couldn't set root directory.\n");
    120 		goto failed;
    121 	}
    122 
    123 	// open file system image.
    124 	if (f.args.loadmfs)
    125 	{
    126 		if (!f._file->open(f.args.mfsName)) {
    127 			error_message =
    128 				TEXT("couldn't open file system image.\n");
    129 			goto failed;
    130 		}
    131 		sz = f._file->size();
    132 		f._file->close();
    133 	}
    134 
    135 	menu.progress();
    136 	if (!f._file->open(f.args.fileName)) {
    137 		error_message = TEXT("couldn't open kernel image.\n");
    138 		goto failed;
    139 	}
    140 
    141 	menu.progress();
    142 	// put kernel to loader.
    143 	if (!f.attachLoader())
    144 		goto failed;
    145 
    146 	if (!f._loader->setFile(f._file)) {
    147 		error_message = TEXT("couldn't initialize loader.\n");
    148 		goto failed;
    149 	}
    150 
    151 	menu.progress();
    152 	sz += f._loader->memorySize();
    153 
    154 	// allocate required memory.
    155 	if (!f._arch->allocateMemory(sz)) {
    156 		error_message = TEXT("couldn't allocate memory.\n");
    157 		goto failed;
    158 	}
    159 
    160 	menu.progress();
    161 	// load kernel to memory.
    162 	if (!f._arch->setupLoader()) {
    163 		error_message = TEXT("couldn't set up loader.\n");
    164 		goto failed;
    165 	}
    166 
    167 	menu.progress();
    168 	if (!f._loader->load()) {
    169 		error_message =
    170 			TEXT("couldn't load kernel image to memory.\n");
    171 		goto failed;
    172 	}
    173 	menu.progress();
    174 
    175 	// load file system image to memory
    176 	if (f.args.loadmfs) {
    177 		f._file->open(f.args.mfsName);
    178 		f._loader->loadExtData();
    179 		f._file->close();
    180 	}
    181 	f._loader->loadEnd();
    182 
    183 	// setup arguments for kernel.
    184 	p = f._arch->setupBootInfo(*f._loader);
    185 
    186 	menu.progress();
    187 
    188 	f._loader->tagDump(3); // dump page chain.(print first 3 links)
    189 
    190 	// jump to kernel entry.
    191 	if (menu._pref.pause_before_boot) {
    192 		if (MessageBox(menu._root->_window, TEXT("Push OK to boot."),
    193 			       TEXT("Last chance..."), MB_YESNO) != IDYES)
    194 			goto failed;
    195 	}
    196 
    197 	f._arch->jump(p, f._loader->tagStart());
    198 	// NOTREACHED
    199 
    200  failed:
    201 	if (error_message == 0)
    202 		error_message = TEXT("can't jump to kernel.\n");
    203 	MessageBox(menu._root->_window, error_message,
    204 		   TEXT("BOOT FAILED"), 0);
    205 }
    206 
    207 //
    208 // HPCBOOT main loop
    209 //
    210 int
    211 HpcBootApp::run(void)
    212 {
    213 	MSG msg;
    214 	HpcMenuInterface &menu = HpcMenuInterface::Instance();
    215 
    216 	while (GetMessage(&msg, 0, 0, 0)) {
    217 		// cancel auto-boot.
    218 		if (menu._pref.auto_boot > 0 && _root &&
    219 		    (msg.message == WM_KEYDOWN ||
    220 		     msg.message == WM_LBUTTONDOWN)) {
    221 			_root->disableTimer();
    222 		}
    223 		if (!_root->isDialogMessage(msg)) {
    224 			TranslateMessage(&msg);
    225 			DispatchMessage(&msg);
    226 		}
    227 	}
    228 	return msg.wParam;
    229 }
    230 
    231 BOOL
    232 HpcBootApp::registerClass(WNDPROC proc)
    233 {
    234 	TCHAR *wc_name;
    235 	WNDCLASS wc;
    236 
    237 	memset(&wc, 0, sizeof(WNDCLASS));
    238 	wc_name		= reinterpret_cast <TCHAR *>
    239 		(LoadString(_instance, IDS_HPCMENU, 0, 0));
    240 	wc.lpfnWndProc	= proc;
    241 	wc.hInstance	= _instance;
    242 	wc.hIcon	= LoadIcon(_instance, MAKEINTRESOURCE(IDI_ICON));
    243 	wc.cbWndExtra	= 4;		// pointer of `this`
    244 	wc.hbrBackground= static_cast <HBRUSH>(GetStockObject(LTGRAY_BRUSH));
    245 	wc.lpszClassName= wc_name;
    246 
    247 	return RegisterClass(&wc);
    248 }
    249