hpcmenu.cpp revision 1.8.8.2 1 1.8.8.2 nathanw /* -*-C++-*- $NetBSD: hpcmenu.cpp,v 1.8.8.2 2002/04/01 07:40:14 nathanw Exp $ */
2 1.8.8.2 nathanw
3 1.8.8.2 nathanw /*-
4 1.8.8.2 nathanw * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 1.8.8.2 nathanw * All rights reserved.
6 1.8.8.2 nathanw *
7 1.8.8.2 nathanw * This code is derived from software contributed to The NetBSD Foundation
8 1.8.8.2 nathanw * by UCHIYAMA Yasushi.
9 1.8.8.2 nathanw *
10 1.8.8.2 nathanw * Redistribution and use in source and binary forms, with or without
11 1.8.8.2 nathanw * modification, are permitted provided that the following conditions
12 1.8.8.2 nathanw * are met:
13 1.8.8.2 nathanw * 1. Redistributions of source code must retain the above copyright
14 1.8.8.2 nathanw * notice, this list of conditions and the following disclaimer.
15 1.8.8.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
16 1.8.8.2 nathanw * notice, this list of conditions and the following disclaimer in the
17 1.8.8.2 nathanw * documentation and/or other materials provided with the distribution.
18 1.8.8.2 nathanw * 3. All advertising materials mentioning features or use of this software
19 1.8.8.2 nathanw * must display the following acknowledgement:
20 1.8.8.2 nathanw * This product includes software developed by the NetBSD
21 1.8.8.2 nathanw * Foundation, Inc. and its contributors.
22 1.8.8.2 nathanw * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.8.8.2 nathanw * contributors may be used to endorse or promote products derived
24 1.8.8.2 nathanw * from this software without specific prior written permission.
25 1.8.8.2 nathanw *
26 1.8.8.2 nathanw * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.8.8.2 nathanw * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.8.8.2 nathanw * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.8.8.2 nathanw * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.8.8.2 nathanw * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.8.8.2 nathanw * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.8.8.2 nathanw * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.8.8.2 nathanw * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.8.8.2 nathanw * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.8.8.2 nathanw * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.8.8.2 nathanw * POSSIBILITY OF SUCH DAMAGE.
37 1.8.8.2 nathanw */
38 1.8.8.2 nathanw
39 1.8.8.2 nathanw #include <hpcmenu.h>
40 1.8.8.2 nathanw #include <hpcboot.h>
41 1.8.8.2 nathanw #include <res/resource.h>
42 1.8.8.2 nathanw #include <menu/window.h>
43 1.8.8.2 nathanw #include <menu/tabwindow.h>
44 1.8.8.2 nathanw #include <menu/rootwindow.h>
45 1.8.8.2 nathanw #include <menu/menu.h>
46 1.8.8.2 nathanw #include <machine/bootinfo.h>
47 1.8.8.2 nathanw #include <framebuffer.h>
48 1.8.8.2 nathanw #include <console.h>
49 1.8.8.2 nathanw #include <string.h>
50 1.8.8.2 nathanw
51 1.8.8.2 nathanw HpcMenuInterface *HpcMenuInterface::_instance = 0;
52 1.8.8.2 nathanw
53 1.8.8.2 nathanw HpcMenuInterface &
54 1.8.8.2 nathanw HpcMenuInterface::Instance()
55 1.8.8.2 nathanw {
56 1.8.8.2 nathanw if (!_instance)
57 1.8.8.2 nathanw _instance = new HpcMenuInterface();
58 1.8.8.2 nathanw return *_instance;
59 1.8.8.2 nathanw }
60 1.8.8.2 nathanw
61 1.8.8.2 nathanw void
62 1.8.8.2 nathanw HpcMenuInterface::Destroy()
63 1.8.8.2 nathanw {
64 1.8.8.2 nathanw if (_instance)
65 1.8.8.2 nathanw delete _instance;
66 1.8.8.2 nathanw }
67 1.8.8.2 nathanw
68 1.8.8.2 nathanw HpcMenuInterface::HpcMenuInterface()
69 1.8.8.2 nathanw {
70 1.8.8.2 nathanw if (!load())
71 1.8.8.2 nathanw _set_default_pref();
72 1.8.8.2 nathanw _pref._version = HPCBOOT_VERSION;
73 1.8.8.2 nathanw _pref._size = sizeof(HpcMenuPreferences);
74 1.8.8.2 nathanw
75 1.8.8.2 nathanw _cons_parameter = 0;
76 1.8.8.2 nathanw memset(_cons_hook, 0, sizeof(struct cons_hook_args) * 4);
77 1.8.8.2 nathanw memset(&_boot_hook, 0, sizeof(struct boot_hook_args));
78 1.8.8.2 nathanw }
79 1.8.8.2 nathanw
80 1.8.8.2 nathanw void
81 1.8.8.2 nathanw HpcMenuInterface::print(TCHAR *buf)
82 1.8.8.2 nathanw {
83 1.8.8.2 nathanw if (_console)
84 1.8.8.2 nathanw _console->print(buf);
85 1.8.8.2 nathanw }
86 1.8.8.2 nathanw
87 1.8.8.2 nathanw void
88 1.8.8.2 nathanw HpcMenuInterface::get_options()
89 1.8.8.2 nathanw {
90 1.8.8.2 nathanw _main->get();
91 1.8.8.2 nathanw _option->get();
92 1.8.8.2 nathanw }
93 1.8.8.2 nathanw
94 1.8.8.2 nathanw TCHAR *
95 1.8.8.2 nathanw HpcMenuInterface::dir(int i)
96 1.8.8.2 nathanw {
97 1.8.8.2 nathanw int res = IDS_DIR_RES(i);
98 1.8.8.2 nathanw if (!IDS_DIR_RES_VALID(res))
99 1.8.8.2 nathanw return 0;
100 1.8.8.2 nathanw
101 1.8.8.2 nathanw if (_pref.dir_user && res == IDS_DIR_USER_DEFINED) {
102 1.8.8.2 nathanw return _pref.dir_user_path;
103 1.8.8.2 nathanw }
104 1.8.8.2 nathanw
105 1.8.8.2 nathanw TCHAR *s = reinterpret_cast <TCHAR *>
106 1.8.8.2 nathanw (LoadString(_root->_app._instance, res, 0, 0));
107 1.8.8.2 nathanw
108 1.8.8.2 nathanw return s;
109 1.8.8.2 nathanw }
110 1.8.8.2 nathanw
111 1.8.8.2 nathanw int
112 1.8.8.2 nathanw HpcMenuInterface::dir_default()
113 1.8.8.2 nathanw {
114 1.8.8.2 nathanw return _pref.dir_user ? IDS_DIR_SEQ(IDS_DIR_USER_DEFINED) : 0;
115 1.8.8.2 nathanw }
116 1.8.8.2 nathanw
117 1.8.8.2 nathanw void
118 1.8.8.2 nathanw HpcMenuInterface::_set_default_pref()
119 1.8.8.2 nathanw {
120 1.8.8.2 nathanw _pref._magic = HPCBOOT_MAGIC;
121 1.8.8.2 nathanw _pref.dir = 0;
122 1.8.8.2 nathanw _pref.dir_user = FALSE;
123 1.8.8.2 nathanw _pref.kernel_user = FALSE;
124 1.8.8.2 nathanw _pref.platid_hi = 0;
125 1.8.8.2 nathanw _pref.platid_lo = 0;
126 1.8.8.2 nathanw _pref.rootfs = 0;
127 1.8.8.2 nathanw wsprintf(_pref.rootfs_file, TEXT("miniroot.fs"));
128 1.8.8.2 nathanw _pref.boot_serial = FALSE;
129 1.8.8.2 nathanw _pref.boot_verbose = FALSE;
130 1.8.8.2 nathanw _pref.boot_single_user = FALSE;
131 1.8.8.2 nathanw _pref.boot_ask_for_name = FALSE;
132 1.8.8.2 nathanw _pref.boot_debugger = FALSE;
133 1.8.8.2 nathanw wsprintf(_pref.boot_extra, TEXT(""));
134 1.8.8.2 nathanw _pref.auto_boot = 0;
135 1.8.8.2 nathanw _pref.reverse_video = FALSE;
136 1.8.8.2 nathanw _pref.pause_before_boot = TRUE;
137 1.8.8.2 nathanw _pref.safety_message = TRUE;
138 1.8.8.2 nathanw #ifdef MIPS
139 1.8.8.2 nathanw _pref.serial_speed = 9600; // historical reason.
140 1.8.8.2 nathanw #else
141 1.8.8.2 nathanw _pref.serial_speed = 19200;
142 1.8.8.2 nathanw #endif
143 1.8.8.2 nathanw }
144 1.8.8.2 nathanw
145 1.8.8.2 nathanw //
146 1.8.8.2 nathanw // load and save current menu status.
147 1.8.8.2 nathanw //
148 1.8.8.2 nathanw BOOL
149 1.8.8.2 nathanw HpcMenuInterface::load()
150 1.8.8.2 nathanw {
151 1.8.8.2 nathanw TCHAR path[MAX_PATH];
152 1.8.8.2 nathanw
153 1.8.8.2 nathanw if (!_find_pref_dir(path))
154 1.8.8.2 nathanw return FALSE;
155 1.8.8.2 nathanw
156 1.8.8.2 nathanw TCHAR filename[MAX_PATH];
157 1.8.8.2 nathanw wsprintf(filename, TEXT("\\%s\\hpcboot.cnf"), path);
158 1.8.8.2 nathanw HANDLE file = CreateFile(filename, GENERIC_READ, 0, 0, OPEN_EXISTING,
159 1.8.8.2 nathanw FILE_ATTRIBUTE_NORMAL, 0);
160 1.8.8.2 nathanw if (file == INVALID_HANDLE_VALUE)
161 1.8.8.2 nathanw return FALSE;
162 1.8.8.2 nathanw
163 1.8.8.2 nathanw DWORD cnt;
164 1.8.8.2 nathanw // read header.
165 1.8.8.2 nathanw if (!ReadFile(file, &_pref, 12, &cnt, 0))
166 1.8.8.2 nathanw goto bad;
167 1.8.8.2 nathanw if (_pref._magic != HPCBOOT_MAGIC)
168 1.8.8.2 nathanw goto bad;
169 1.8.8.2 nathanw // read all.
170 1.8.8.2 nathanw SetFilePointer(file, 0, 0, FILE_BEGIN);
171 1.8.8.2 nathanw if (!ReadFile(file, &_pref, _pref._size, &cnt, 0))
172 1.8.8.2 nathanw goto bad;
173 1.8.8.2 nathanw CloseHandle(file);
174 1.8.8.2 nathanw
175 1.8.8.2 nathanw return TRUE;
176 1.8.8.2 nathanw bad:
177 1.8.8.2 nathanw CloseHandle(file);
178 1.8.8.2 nathanw return FALSE;
179 1.8.8.2 nathanw }
180 1.8.8.2 nathanw
181 1.8.8.2 nathanw BOOL
182 1.8.8.2 nathanw HpcMenuInterface::save()
183 1.8.8.2 nathanw {
184 1.8.8.2 nathanw TCHAR path[MAX_PATH];
185 1.8.8.2 nathanw
186 1.8.8.2 nathanw if (_find_pref_dir(path)) {
187 1.8.8.2 nathanw TCHAR filename[MAX_PATH];
188 1.8.8.2 nathanw wsprintf(filename, TEXT("\\%s\\hpcboot.cnf"), path);
189 1.8.8.2 nathanw HANDLE file = CreateFile(filename, GENERIC_WRITE, 0, 0,
190 1.8.8.2 nathanw CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
191 1.8.8.2 nathanw DWORD cnt;
192 1.8.8.2 nathanw WriteFile(file, &_pref, _pref._size, &cnt, 0);
193 1.8.8.2 nathanw CloseHandle(file);
194 1.8.8.2 nathanw return cnt == _pref._size;
195 1.8.8.2 nathanw }
196 1.8.8.2 nathanw
197 1.8.8.2 nathanw return FALSE;
198 1.8.8.2 nathanw }
199 1.8.8.2 nathanw
200 1.8.8.2 nathanw // arguments for kernel.
201 1.8.8.2 nathanw int
202 1.8.8.2 nathanw HpcMenuInterface::setup_kernel_args(vaddr_t v, paddr_t p, size_t sz)
203 1.8.8.2 nathanw {
204 1.8.8.2 nathanw int argc = 0;
205 1.8.8.2 nathanw kaddr_t *argv = reinterpret_cast <kaddr_t *>(v);
206 1.8.8.2 nathanw char *loc = reinterpret_cast <char *>
207 1.8.8.2 nathanw (v + sizeof(char **) * MAX_KERNEL_ARGS);
208 1.8.8.2 nathanw paddr_t locp = p + sizeof(char **) * MAX_KERNEL_ARGS;
209 1.8.8.2 nathanw size_t len;
210 1.8.8.2 nathanw TCHAR *w;
211 1.8.8.2 nathanw char *ptr;
212 1.8.8.2 nathanw
213 1.8.8.2 nathanw #define SETOPT(c) \
214 1.8.8.2 nathanw __BEGIN_MACRO \
215 1.8.8.2 nathanw argv[argc++] = ptokv(locp); \
216 1.8.8.2 nathanw *loc++ =(c); \
217 1.8.8.2 nathanw *loc++ = '\0'; \
218 1.8.8.2 nathanw locp += 2; \
219 1.8.8.2 nathanw __END_MACRO
220 1.8.8.2 nathanw // 1st arg is kernel name.
221 1.8.8.2 nathanw // DPRINTF_SETUP(); //if you want to use debug print, enable this line.
222 1.8.8.2 nathanw
223 1.8.8.2 nathanw w = _pref.kernel_user_file;
224 1.8.8.2 nathanw argv[argc++] = ptokv(locp);
225 1.8.8.2 nathanw len = WideCharToMultiByte(CP_ACP, 0, w, wcslen(w), 0, 0, 0, 0);
226 1.8.8.2 nathanw WideCharToMultiByte(CP_ACP, 0, w, len, loc, len, 0, 0);
227 1.8.8.2 nathanw loc[len] = '\0';
228 1.8.8.2 nathanw loc += len + 1;
229 1.8.8.2 nathanw locp += len + 1;
230 1.8.8.2 nathanw
231 1.8.8.2 nathanw if (_pref.boot_serial) // serial console
232 1.8.8.2 nathanw SETOPT('h');
233 1.8.8.2 nathanw if (_pref.boot_verbose) // boot verbosely
234 1.8.8.2 nathanw SETOPT('v');
235 1.8.8.2 nathanw if (_pref.boot_single_user) // boot to single user
236 1.8.8.2 nathanw SETOPT('s');
237 1.8.8.2 nathanw if (_pref.boot_ask_for_name) // ask for file name to boot from
238 1.8.8.2 nathanw SETOPT('a');
239 1.8.8.2 nathanw if (_pref.boot_debugger) // break into the kernel debugger
240 1.8.8.2 nathanw SETOPT('d');
241 1.8.8.2 nathanw
242 1.8.8.2 nathanw // boot from
243 1.8.8.2 nathanw switch(_pref.rootfs) {
244 1.8.8.2 nathanw case 0: // wd0
245 1.8.8.2 nathanw break;
246 1.8.8.2 nathanw case 1: // sd0
247 1.8.8.2 nathanw argv[argc++] = ptokv(locp);
248 1.8.8.2 nathanw strncpy(loc, "b=sd0", 6);
249 1.8.8.2 nathanw loc += 6;
250 1.8.8.2 nathanw locp += 6;
251 1.8.8.2 nathanw break;
252 1.8.8.2 nathanw case 2: // memory disk
253 1.8.8.2 nathanw w = _pref.rootfs_file;
254 1.8.8.2 nathanw argv[argc++] = ptokv(locp);
255 1.8.8.2 nathanw strncpy(loc, "m=", 2);
256 1.8.8.2 nathanw loc += 2;
257 1.8.8.2 nathanw len = WideCharToMultiByte(CP_ACP, 0, w, wcslen(w), 0, 0, 0, 0);
258 1.8.8.2 nathanw WideCharToMultiByte(CP_ACP, 0, w, len, loc, len, 0, 0);
259 1.8.8.2 nathanw loc[len] = '\0';
260 1.8.8.2 nathanw loc += len + 1;
261 1.8.8.2 nathanw locp += 2 + len + 1;
262 1.8.8.2 nathanw break;
263 1.8.8.2 nathanw case 3: // nfs
264 1.8.8.2 nathanw argv[argc++] = ptokv(locp);
265 1.8.8.2 nathanw strncpy(loc, "b=nfs", 6);
266 1.8.8.2 nathanw loc += 6;
267 1.8.8.2 nathanw locp += 6;
268 1.8.8.2 nathanw break;
269 1.8.8.2 nathanw }
270 1.8.8.2 nathanw
271 1.8.8.2 nathanw // Extra kernel options. (Option tab window)
272 1.8.8.2 nathanw w = _pref.boot_extra;
273 1.8.8.2 nathanw len = WideCharToMultiByte(CP_ACP, 0, w, wcslen(w), 0, 0, 0, 0);
274 1.8.8.2 nathanw
275 1.8.8.2 nathanw if ((ptr = (char *)malloc(len)) == NULL) {
276 1.8.8.2 nathanw MessageBox(_root->_window,
277 1.8.8.2 nathanw L"Couldn't allocate memory for extra kernel options.",
278 1.8.8.2 nathanw TEXT("WARNING"), 0);
279 1.8.8.2 nathanw
280 1.8.8.2 nathanw return argc;
281 1.8.8.2 nathanw }
282 1.8.8.2 nathanw WideCharToMultiByte(CP_ACP, 0, w, len, ptr, len, 0, 0);
283 1.8.8.2 nathanw ptr[len]='\0';
284 1.8.8.2 nathanw
285 1.8.8.2 nathanw while (*ptr == ' ' || *ptr == '\t')
286 1.8.8.2 nathanw ptr++;
287 1.8.8.2 nathanw while (*ptr != '\0') {
288 1.8.8.2 nathanw len = strcspn(ptr, " \t");
289 1.8.8.2 nathanw if (len == 0)
290 1.8.8.2 nathanw len = strlen(ptr);
291 1.8.8.2 nathanw
292 1.8.8.2 nathanw if (argc == MAX_KERNEL_ARGS || locp + len + 1 > p + sz) {
293 1.8.8.2 nathanw MessageBox(_root->_window,
294 1.8.8.2 nathanw L"Too many extra kernel options.",
295 1.8.8.2 nathanw TEXT("WARNING"), 0);
296 1.8.8.2 nathanw break;
297 1.8.8.2 nathanw }
298 1.8.8.2 nathanw argv[argc++] = ptokv(locp);
299 1.8.8.2 nathanw strncpy(loc, ptr, len);
300 1.8.8.2 nathanw loc[len] = '\0';
301 1.8.8.2 nathanw loc += len + 1;
302 1.8.8.2 nathanw locp += len + 1;
303 1.8.8.2 nathanw
304 1.8.8.2 nathanw ptr += len;
305 1.8.8.2 nathanw while (*ptr == ' ' || *ptr == '\t')
306 1.8.8.2 nathanw ptr++;
307 1.8.8.2 nathanw }
308 1.8.8.2 nathanw
309 1.8.8.2 nathanw return argc;
310 1.8.8.2 nathanw }
311 1.8.8.2 nathanw
312 1.8.8.2 nathanw // kernel bootinfo.
313 1.8.8.2 nathanw void
314 1.8.8.2 nathanw HpcMenuInterface::setup_bootinfo(struct bootinfo &bi)
315 1.8.8.2 nathanw {
316 1.8.8.2 nathanw FrameBufferInfo fb(_pref.platid_hi, _pref.platid_lo);
317 1.8.8.2 nathanw TIME_ZONE_INFORMATION tz;
318 1.8.8.2 nathanw GetTimeZoneInformation(&tz);
319 1.8.8.2 nathanw
320 1.8.8.2 nathanw memset(&bi, 0, sizeof(struct bootinfo));
321 1.8.8.2 nathanw bi.length = sizeof(struct bootinfo);
322 1.8.8.2 nathanw bi.reserved = 0;
323 1.8.8.2 nathanw bi.magic = BOOTINFO_MAGIC;
324 1.8.8.2 nathanw bi.fb_addr = fb.addr();
325 1.8.8.2 nathanw bi.fb_type = fb.type();
326 1.8.8.2 nathanw bi.fb_line_bytes = fb.linebytes();
327 1.8.8.2 nathanw bi.fb_width = fb.width();
328 1.8.8.2 nathanw bi.fb_height = fb.height();
329 1.8.8.2 nathanw bi.platid_cpu = _pref.platid_hi;
330 1.8.8.2 nathanw bi.platid_machine = _pref.platid_lo;
331 1.8.8.2 nathanw bi.timezone = tz.Bias;
332 1.8.8.2 nathanw }
333 1.8.8.2 nathanw
334 1.8.8.2 nathanw // Progress bar
335 1.8.8.2 nathanw void
336 1.8.8.2 nathanw HpcMenuInterface::progress()
337 1.8.8.2 nathanw {
338 1.8.8.2 nathanw SendMessage(_root->_progress_bar->_window, PBM_STEPIT, 0, 0);
339 1.8.8.2 nathanw }
340 1.8.8.2 nathanw
341 1.8.8.2 nathanw // Boot kernel.
342 1.8.8.2 nathanw void
343 1.8.8.2 nathanw HpcMenuInterface::boot()
344 1.8.8.2 nathanw {
345 1.8.8.2 nathanw struct support_status *tab = _unsupported;
346 1.8.8.2 nathanw u_int32_t cpu = _pref.platid_hi;
347 1.8.8.2 nathanw u_int32_t machine = _pref.platid_lo;
348 1.8.8.2 nathanw
349 1.8.8.2 nathanw if (_pref.safety_message)
350 1.8.8.2 nathanw for (; tab->cpu; tab++) {
351 1.8.8.2 nathanw if (tab->cpu == cpu && tab->machine == machine) {
352 1.8.8.2 nathanw MessageBox(_root->_window,
353 1.8.8.2 nathanw tab->cause ? tab->cause :
354 1.8.8.2 nathanw L"not supported yet.",
355 1.8.8.2 nathanw TEXT("BOOT FAILED"), 0);
356 1.8.8.2 nathanw return;
357 1.8.8.2 nathanw }
358 1.8.8.2 nathanw }
359 1.8.8.2 nathanw
360 1.8.8.2 nathanw if (_boot_hook.func)
361 1.8.8.2 nathanw _boot_hook.func(_boot_hook.arg);
362 1.8.8.2 nathanw }
363