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