xref: /netbsd-src/sys/arch/hpc/stand/hpcboot/hpcboot.cpp (revision 17dd36da8292193180754d5047c0926dbb56818c)
1 /*	$NetBSD: hpcboot.cpp,v 1.2 2001/03/22 18:21:02 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 	f._file->close();
175 
176 	// load file system image to memory
177 	if (f.args.loadmfs) {
178 		f._file->open(f.args.mfsName);
179 		f._loader->loadExtData();
180 		f._file->close();
181 	}
182 	f._loader->loadEnd();
183 
184 	// setup arguments for kernel.
185 	p = f._arch->setupBootInfo(*f._loader);
186 
187 	menu.progress();
188 
189 	f._loader->tagDump(3); // dump page chain.(print first 3 links)
190 
191 	// jump to kernel entry.
192 	if (menu._pref.pause_before_boot) {
193 		if (MessageBox(menu._root->_window, TEXT("Push OK to boot."),
194 			       TEXT("Last chance..."), MB_YESNO) != IDYES)
195 			goto failed;
196 	}
197 
198 	f._arch->jump(p, f._loader->tagStart());
199 	// NOTREACHED
200 
201  failed:
202 	if (error_message == 0)
203 		error_message = TEXT("can't jump to kernel.\n");
204 	f._file->close();
205 	MessageBox(menu._root->_window, error_message,
206 		   TEXT("BOOT FAILED"), 0);
207 }
208 
209 //
210 // HPCBOOT main loop
211 //
212 int
213 HpcBootApp::run(void)
214 {
215 	MSG msg;
216 	HpcMenuInterface &menu = HpcMenuInterface::Instance();
217 
218 	while (GetMessage(&msg, 0, 0, 0)) {
219 		// cancel auto-boot.
220 		if (menu._pref.auto_boot > 0 && _root &&
221 		    (msg.message == WM_KEYDOWN ||
222 		     msg.message == WM_LBUTTONDOWN)) {
223 			_root->disableTimer();
224 		}
225 		if (!_root->isDialogMessage(msg)) {
226 			TranslateMessage(&msg);
227 			DispatchMessage(&msg);
228 		}
229 	}
230 	return msg.wParam;
231 }
232 
233 BOOL
234 HpcBootApp::registerClass(WNDPROC proc)
235 {
236 	TCHAR *wc_name;
237 	WNDCLASS wc;
238 
239 	memset(&wc, 0, sizeof(WNDCLASS));
240 	wc_name		= reinterpret_cast <TCHAR *>
241 		(LoadString(_instance, IDS_HPCMENU, 0, 0));
242 	wc.lpfnWndProc	= proc;
243 	wc.hInstance	= _instance;
244 	wc.hIcon	= LoadIcon(_instance, MAKEINTRESOURCE(IDI_ICON));
245 	wc.cbWndExtra	= 4;		// pointer of `this`
246 	wc.hbrBackground= static_cast <HBRUSH>(GetStockObject(LTGRAY_BRUSH));
247 	wc.lpszClassName= wc_name;
248 
249 	return RegisterClass(&wc);
250 }
251