hpcmenu.cpp revision 1.6 1 /* -*-C++-*- $NetBSD: hpcmenu.cpp,v 1.6 2001/04/24 19:27:59 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 #include <res/resource.h>
42 #include <menu/window.h>
43 #include <menu/tabwindow.h>
44 #include <menu/rootwindow.h>
45 #include <menu/menu.h>
46 #include <machine/bootinfo.h>
47 #include <framebuffer.h>
48 #include <console.h>
49
50 HpcMenuInterface *HpcMenuInterface::_instance = 0;
51
52 HpcMenuInterface &
53 HpcMenuInterface::Instance()
54 {
55 if (!_instance)
56 _instance = new HpcMenuInterface();
57 return *_instance;
58 }
59
60 void
61 HpcMenuInterface::Destroy()
62 {
63 if (_instance)
64 delete _instance;
65 }
66
67 HpcMenuInterface::HpcMenuInterface()
68 {
69 if (!load())
70 _set_default_pref();
71 _pref._version = HPCBOOT_VERSION;
72 _pref._size = sizeof(HpcMenuPreferences);
73
74 _cons_parameter = 0;
75 memset(_cons_hook, 0, sizeof(struct cons_hook_args) * 4);
76 memset(&_boot_hook, 0, sizeof(struct boot_hook_args));
77 }
78
79 void
80 HpcMenuInterface::print(TCHAR *buf)
81 {
82 if (_console)
83 _console->print(buf);
84 }
85
86 void
87 HpcMenuInterface::get_options()
88 {
89 _main->get();
90 _option->get();
91 }
92
93 TCHAR *
94 HpcMenuInterface::dir(int i)
95 {
96 int res = IDS_DIR_RES(i);
97 if (!IDS_DIR_RES_VALID(res))
98 return 0;
99
100 if (_pref.dir_user && res == IDS_DIR_USER_DEFINED) {
101 return _pref.dir_user_path;
102 }
103
104 TCHAR *s = reinterpret_cast <TCHAR *>
105 (LoadString(_root->_app._instance, res, 0, 0));
106
107 return s;
108 }
109
110 int
111 HpcMenuInterface::dir_default()
112 {
113 return _pref.dir_user ? IDS_DIR_SEQ(IDS_DIR_USER_DEFINED) : 0;
114 }
115
116 void
117 HpcMenuInterface::_set_default_pref()
118 {
119 _pref._magic = HPCBOOT_MAGIC;
120 _pref.dir = 0;
121 _pref.dir_user = FALSE;
122 _pref.kernel_user = FALSE;
123 _pref.platid_hi = 0;
124 _pref.platid_lo = 0;
125 _pref.rootfs = 0;
126 wsprintf(_pref.rootfs_file, TEXT("miniroot.fs"));
127 _pref.boot_serial = FALSE;
128 _pref.boot_verbose = FALSE;
129 _pref.boot_single_user = FALSE;
130 _pref.boot_ask_for_name = FALSE;
131 _pref.auto_boot = 0;
132 _pref.reverse_video = FALSE;
133 _pref.pause_before_boot = TRUE;
134 _pref.safety_message = TRUE;
135 #ifdef MIPS
136 _pref.serial_speed = 9600; // historical reason.
137 #else
138 _pref.serial_speed = 19200;
139 #endif
140 }
141
142 //
143 // load and save current menu status.
144 //
145 BOOL
146 HpcMenuInterface::load()
147 {
148 TCHAR path[MAX_PATH];
149
150 if (!_find_pref_dir(path))
151 return FALSE;
152
153 TCHAR filename[MAX_PATH];
154 wsprintf(filename, TEXT("\\%s\\hpcboot.cnf"), path);
155 HANDLE file = CreateFile(filename, GENERIC_READ, 0, 0, OPEN_EXISTING,
156 FILE_ATTRIBUTE_NORMAL, 0);
157 if (file == INVALID_HANDLE_VALUE)
158 return FALSE;
159
160 DWORD cnt;
161 // read header.
162 if (!ReadFile(file, &_pref, 12, &cnt, 0))
163 goto bad;
164 if (_pref._magic != HPCBOOT_MAGIC)
165 goto bad;
166 // read all.
167 SetFilePointer(file, 0, 0, FILE_BEGIN);
168 if (!ReadFile(file, &_pref, _pref._size, &cnt, 0))
169 goto bad;
170 CloseHandle(file);
171
172 return TRUE;
173 bad:
174 CloseHandle(file);
175 return FALSE;
176 }
177
178 BOOL
179 HpcMenuInterface::save()
180 {
181 TCHAR path[MAX_PATH];
182
183 if (_find_pref_dir(path)) {
184 TCHAR filename[MAX_PATH];
185 wsprintf(filename, TEXT("\\%s\\hpcboot.cnf"), path);
186 HANDLE file = CreateFile(filename, GENERIC_WRITE, 0, 0,
187 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,
188 0);
189 DWORD cnt;
190 WriteFile(file, &_pref, _pref._size, &cnt, 0);
191 CloseHandle(file);
192 return cnt == _pref._size;
193 }
194
195 return FALSE;
196 }
197
198 // arguments for kernel.
199 int
200 HpcMenuInterface::setup_kernel_args(vaddr_t v, paddr_t p)
201 {
202 int argc = 0;
203 kaddr_t *argv = reinterpret_cast <kaddr_t *>(v);
204 char *loc = reinterpret_cast <char *>
205 (v + sizeof(char **) * MAX_KERNEL_ARGS);
206 paddr_t locp = p + sizeof(char **) * MAX_KERNEL_ARGS;
207 size_t len;
208 TCHAR *w;
209
210 #define SETOPT(c) \
211 __BEGIN_MACRO \
212 argv[argc++] = ptokv(locp); \
213 *loc++ =(c); \
214 *loc++ = '\0'; \
215 locp += 2; \
216 __END_MACRO
217 // 1st arg is kernel name.
218 // DPRINTF_SETUP(); if you want to use debug print, enable this line.
219
220 w = _pref.kernel_user_file;
221 argv[argc++] = ptokv(locp);
222 len = WideCharToMultiByte(CP_ACP, 0, w, wcslen(w), 0, 0, 0, 0);
223 WideCharToMultiByte(CP_ACP, 0, w, len, loc, len, 0, 0);
224 loc[len] = '\0';
225 loc += len + 1;
226 locp += len + 1;
227
228 if (_pref.boot_serial) // serial console
229 SETOPT('h');
230 if (_pref.boot_verbose) // boot verbosely
231 SETOPT('v');
232 if (_pref.boot_single_user) // boot to single user
233 SETOPT('s');
234 if (_pref.boot_ask_for_name) // ask for file name to boot from
235 SETOPT('a');
236
237 // boot from
238 switch(_pref.rootfs) {
239 case 0: // wd0
240 break;
241 case 1: // sd0
242 argv[argc++] = ptokv(locp);
243 strncpy(loc, "b=sd0", 6);
244 loc += 6;
245 locp += 6;
246 break;
247 case 2: // memory disk
248 w = _pref.rootfs_file;
249 argv[argc++] = ptokv(locp);
250 strncpy(loc, "m=", 2);
251 loc += 2;
252 len = WideCharToMultiByte(CP_ACP, 0, w, wcslen(w), 0, 0, 0, 0);
253 WideCharToMultiByte(CP_ACP, 0, w, len, loc, len, 0, 0);
254 loc[len] = '\0';
255 loc += len + 1;
256 locp += 2 + len + 1;
257 break;
258 case 3: // nfs
259 argv[argc++] = ptokv(locp);
260 strncpy(loc, "b=nfs", 6);
261 loc += 6;
262 locp += 6;
263 break;
264 }
265
266 return argc;
267 }
268
269 // kernel bootinfo.
270 void
271 HpcMenuInterface::setup_bootinfo(struct bootinfo &bi)
272 {
273 FrameBufferInfo fb(_pref.platid_hi, _pref.platid_lo);
274 TIME_ZONE_INFORMATION tz;
275 GetTimeZoneInformation(&tz);
276
277 memset(&bi, 0, sizeof(struct bootinfo));
278 bi.length = sizeof(struct bootinfo);
279 bi.reserved = 0;
280 bi.magic = BOOTINFO_MAGIC;
281 bi.fb_addr = fb.addr();
282 bi.fb_type = fb.type();
283 bi.fb_line_bytes = fb.linebytes();
284 bi.fb_width = fb.width();
285 bi.fb_height = fb.height();
286 bi.platid_cpu = _pref.platid_hi;
287 bi.platid_machine = _pref.platid_lo;
288 bi.timezone = tz.Bias;
289 }
290
291 // Progress bar
292 void
293 HpcMenuInterface::progress()
294 {
295 SendMessage(_root->_progress_bar->_window, PBM_STEPIT, 0, 0);
296 }
297
298 // Boot kernel.
299 void
300 HpcMenuInterface::boot()
301 {
302 struct support_status *tab = _unsupported;
303 u_int32_t cpu = _pref.platid_hi;
304 u_int32_t machine = _pref.platid_lo;
305
306 if (_pref.safety_message)
307 for (; tab->cpu; tab++) {
308 if (tab->cpu == cpu && tab->machine == machine) {
309 MessageBox(_root->_window,
310 tab->cause ? tab->cause :
311 L"not supported yet.",
312 TEXT("BOOT FAILED"), 0);
313 return;
314 }
315 }
316
317 if (_boot_hook.func)
318 _boot_hook.func(_boot_hook.arg);
319 }
320