hpcmenu.cpp revision 1.7 1 /* -*-C++-*- $NetBSD: hpcmenu.cpp,v 1.7 2001/05/08 18:51:22 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, 0);
188 DWORD cnt;
189 WriteFile(file, &_pref, _pref._size, &cnt, 0);
190 CloseHandle(file);
191 return cnt == _pref._size;
192 }
193
194 return FALSE;
195 }
196
197 // arguments for kernel.
198 int
199 HpcMenuInterface::setup_kernel_args(vaddr_t v, paddr_t p)
200 {
201 int argc = 0;
202 kaddr_t *argv = reinterpret_cast <kaddr_t *>(v);
203 char *loc = reinterpret_cast <char *>
204 (v + sizeof(char **) * MAX_KERNEL_ARGS);
205 paddr_t locp = p + sizeof(char **) * MAX_KERNEL_ARGS;
206 size_t len;
207 TCHAR *w;
208
209 #define SETOPT(c) \
210 __BEGIN_MACRO \
211 argv[argc++] = ptokv(locp); \
212 *loc++ =(c); \
213 *loc++ = '\0'; \
214 locp += 2; \
215 __END_MACRO
216 // 1st arg is kernel name.
217 // DPRINTF_SETUP(); if you want to use debug print, enable this line.
218
219 w = _pref.kernel_user_file;
220 argv[argc++] = ptokv(locp);
221 len = WideCharToMultiByte(CP_ACP, 0, w, wcslen(w), 0, 0, 0, 0);
222 WideCharToMultiByte(CP_ACP, 0, w, len, loc, len, 0, 0);
223 loc[len] = '\0';
224 loc += len + 1;
225 locp += len + 1;
226
227 if (_pref.boot_serial) // serial console
228 SETOPT('h');
229 if (_pref.boot_verbose) // boot verbosely
230 SETOPT('v');
231 if (_pref.boot_single_user) // boot to single user
232 SETOPT('s');
233 if (_pref.boot_ask_for_name) // ask for file name to boot from
234 SETOPT('a');
235
236 // boot from
237 switch(_pref.rootfs) {
238 case 0: // wd0
239 break;
240 case 1: // sd0
241 argv[argc++] = ptokv(locp);
242 strncpy(loc, "b=sd0", 6);
243 loc += 6;
244 locp += 6;
245 break;
246 case 2: // memory disk
247 w = _pref.rootfs_file;
248 argv[argc++] = ptokv(locp);
249 strncpy(loc, "m=", 2);
250 loc += 2;
251 len = WideCharToMultiByte(CP_ACP, 0, w, wcslen(w), 0, 0, 0, 0);
252 WideCharToMultiByte(CP_ACP, 0, w, len, loc, len, 0, 0);
253 loc[len] = '\0';
254 loc += len + 1;
255 locp += 2 + len + 1;
256 break;
257 case 3: // nfs
258 argv[argc++] = ptokv(locp);
259 strncpy(loc, "b=nfs", 6);
260 loc += 6;
261 locp += 6;
262 break;
263 }
264
265 return argc;
266 }
267
268 // kernel bootinfo.
269 void
270 HpcMenuInterface::setup_bootinfo(struct bootinfo &bi)
271 {
272 FrameBufferInfo fb(_pref.platid_hi, _pref.platid_lo);
273 TIME_ZONE_INFORMATION tz;
274 GetTimeZoneInformation(&tz);
275
276 memset(&bi, 0, sizeof(struct bootinfo));
277 bi.length = sizeof(struct bootinfo);
278 bi.reserved = 0;
279 bi.magic = BOOTINFO_MAGIC;
280 bi.fb_addr = fb.addr();
281 bi.fb_type = fb.type();
282 bi.fb_line_bytes = fb.linebytes();
283 bi.fb_width = fb.width();
284 bi.fb_height = fb.height();
285 bi.platid_cpu = _pref.platid_hi;
286 bi.platid_machine = _pref.platid_lo;
287 bi.timezone = tz.Bias;
288 }
289
290 // Progress bar
291 void
292 HpcMenuInterface::progress()
293 {
294 SendMessage(_root->_progress_bar->_window, PBM_STEPIT, 0, 0);
295 }
296
297 // Boot kernel.
298 void
299 HpcMenuInterface::boot()
300 {
301 struct support_status *tab = _unsupported;
302 u_int32_t cpu = _pref.platid_hi;
303 u_int32_t machine = _pref.platid_lo;
304
305 if (_pref.safety_message)
306 for (; tab->cpu; tab++) {
307 if (tab->cpu == cpu && tab->machine == machine) {
308 MessageBox(_root->_window,
309 tab->cause ? tab->cause :
310 L"not supported yet.",
311 TEXT("BOOT FAILED"), 0);
312 return;
313 }
314 }
315
316 if (_boot_hook.func)
317 _boot_hook.func(_boot_hook.arg);
318 }
319