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