1 1.19 nonaka /* -*-C++-*- $NetBSD: hpcmenu.cpp,v 1.19 2010/04/06 16:20:27 nonaka Exp $ */ 2 1.1 uch 3 1.1 uch /*- 4 1.13 uch * Copyright (c) 2001, 2004 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 * 19 1.1 uch * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.1 uch * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.1 uch * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.1 uch * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.1 uch * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.1 uch * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.1 uch * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.1 uch * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.1 uch * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.1 uch * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.1 uch * POSSIBILITY OF SUCH DAMAGE. 30 1.1 uch */ 31 1.1 uch 32 1.1 uch #include <hpcmenu.h> 33 1.1 uch #include <hpcboot.h> 34 1.1 uch #include <res/resource.h> 35 1.1 uch #include <menu/window.h> 36 1.1 uch #include <menu/tabwindow.h> 37 1.1 uch #include <menu/rootwindow.h> 38 1.6 uch #include <menu/menu.h> 39 1.1 uch #include <machine/bootinfo.h> 40 1.1 uch #include <framebuffer.h> 41 1.1 uch #include <console.h> 42 1.9 uch #include <string.h> 43 1.1 uch 44 1.1 uch HpcMenuInterface *HpcMenuInterface::_instance = 0; 45 1.1 uch 46 1.1 uch HpcMenuInterface & 47 1.4 uch HpcMenuInterface::Instance() 48 1.1 uch { 49 1.14 uch 50 1.1 uch if (!_instance) 51 1.1 uch _instance = new HpcMenuInterface(); 52 1.1 uch return *_instance; 53 1.1 uch } 54 1.1 uch 55 1.1 uch void 56 1.4 uch HpcMenuInterface::Destroy() 57 1.1 uch { 58 1.14 uch 59 1.1 uch if (_instance) 60 1.1 uch delete _instance; 61 1.1 uch } 62 1.1 uch 63 1.6 uch HpcMenuInterface::HpcMenuInterface() 64 1.6 uch { 65 1.14 uch 66 1.6 uch if (!load()) 67 1.6 uch _set_default_pref(); 68 1.6 uch _pref._version = HPCBOOT_VERSION; 69 1.6 uch _pref._size = sizeof(HpcMenuPreferences); 70 1.14 uch 71 1.6 uch _cons_parameter = 0; 72 1.6 uch memset(_cons_hook, 0, sizeof(struct cons_hook_args) * 4); 73 1.6 uch memset(&_boot_hook, 0, sizeof(struct boot_hook_args)); 74 1.6 uch } 75 1.6 uch 76 1.1 uch void 77 1.1 uch HpcMenuInterface::print(TCHAR *buf) 78 1.1 uch { 79 1.14 uch 80 1.1 uch if (_console) 81 1.1 uch _console->print(buf); 82 1.1 uch } 83 1.1 uch 84 1.1 uch void 85 1.4 uch HpcMenuInterface::get_options() 86 1.1 uch { 87 1.14 uch 88 1.1 uch _main->get(); 89 1.1 uch _option->get(); 90 1.1 uch } 91 1.1 uch 92 1.1 uch TCHAR * 93 1.1 uch HpcMenuInterface::dir(int i) 94 1.1 uch { 95 1.1 uch int res = IDS_DIR_RES(i); 96 1.14 uch 97 1.1 uch if (!IDS_DIR_RES_VALID(res)) 98 1.1 uch return 0; 99 1.14 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.7 uch (LoadString(_root->_app._instance, res, 0, 0)); 106 1.14 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.14 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.14 uch 121 1.6 uch _pref._magic = HPCBOOT_MAGIC; 122 1.6 uch _pref.dir = 0; 123 1.6 uch _pref.dir_user = FALSE; 124 1.6 uch _pref.kernel_user = FALSE; 125 1.6 uch _pref.platid_hi = 0; 126 1.6 uch _pref.platid_lo = 0; 127 1.6 uch _pref.rootfs = 0; 128 1.6 uch wsprintf(_pref.rootfs_file, TEXT("miniroot.fs")); 129 1.6 uch _pref.boot_serial = FALSE; 130 1.6 uch _pref.boot_verbose = FALSE; 131 1.6 uch _pref.boot_single_user = FALSE; 132 1.6 uch _pref.boot_ask_for_name = FALSE; 133 1.8 enami _pref.boot_debugger = FALSE; 134 1.9 uch wsprintf(_pref.boot_extra, TEXT("")); 135 1.6 uch _pref.auto_boot = 0; 136 1.6 uch _pref.reverse_video = FALSE; 137 1.6 uch _pref.pause_before_boot = TRUE; 138 1.6 uch _pref.safety_message = TRUE; 139 1.6 uch #ifdef MIPS 140 1.6 uch _pref.serial_speed = 9600; // historical reason. 141 1.6 uch #else 142 1.6 uch _pref.serial_speed = 19200; 143 1.6 uch #endif 144 1.6 uch } 145 1.6 uch 146 1.6 uch // 147 1.6 uch // load and save current menu status. 148 1.6 uch // 149 1.1 uch BOOL 150 1.4 uch HpcMenuInterface::load() 151 1.1 uch { 152 1.1 uch TCHAR path[MAX_PATH]; 153 1.1 uch 154 1.1 uch if (!_find_pref_dir(path)) 155 1.1 uch return FALSE; 156 1.1 uch 157 1.1 uch TCHAR filename[MAX_PATH]; 158 1.1 uch wsprintf(filename, TEXT("\\%s\\hpcboot.cnf"), path); 159 1.1 uch HANDLE file = CreateFile(filename, GENERIC_READ, 0, 0, OPEN_EXISTING, 160 1.7 uch FILE_ATTRIBUTE_NORMAL, 0); 161 1.1 uch if (file == INVALID_HANDLE_VALUE) 162 1.1 uch return FALSE; 163 1.14 uch 164 1.1 uch DWORD cnt; 165 1.1 uch // read header. 166 1.1 uch if (!ReadFile(file, &_pref, 12, &cnt, 0)) 167 1.1 uch goto bad; 168 1.1 uch if (_pref._magic != HPCBOOT_MAGIC) 169 1.1 uch goto bad; 170 1.1 uch // read all. 171 1.1 uch SetFilePointer(file, 0, 0, FILE_BEGIN); 172 1.1 uch if (!ReadFile(file, &_pref, _pref._size, &cnt, 0)) 173 1.1 uch goto bad; 174 1.1 uch CloseHandle(file); 175 1.1 uch 176 1.1 uch return TRUE; 177 1.1 uch bad: 178 1.1 uch CloseHandle(file); 179 1.1 uch return FALSE; 180 1.1 uch } 181 1.1 uch 182 1.1 uch BOOL 183 1.4 uch HpcMenuInterface::save() 184 1.1 uch { 185 1.1 uch TCHAR path[MAX_PATH]; 186 1.14 uch 187 1.1 uch if (_find_pref_dir(path)) { 188 1.1 uch TCHAR filename[MAX_PATH]; 189 1.1 uch wsprintf(filename, TEXT("\\%s\\hpcboot.cnf"), path); 190 1.1 uch HANDLE file = CreateFile(filename, GENERIC_WRITE, 0, 0, 191 1.7 uch CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); 192 1.1 uch DWORD cnt; 193 1.1 uch WriteFile(file, &_pref, _pref._size, &cnt, 0); 194 1.1 uch CloseHandle(file); 195 1.1 uch return cnt == _pref._size; 196 1.1 uch } 197 1.1 uch 198 1.1 uch return FALSE; 199 1.1 uch } 200 1.1 uch 201 1.1 uch // arguments for kernel. 202 1.1 uch int 203 1.9 uch HpcMenuInterface::setup_kernel_args(vaddr_t v, paddr_t p, size_t sz) 204 1.1 uch { 205 1.1 uch int argc = 0; 206 1.1 uch kaddr_t *argv = reinterpret_cast <kaddr_t *>(v); 207 1.1 uch char *loc = reinterpret_cast <char *> 208 1.7 uch (v + sizeof(char **) * MAX_KERNEL_ARGS); 209 1.1 uch paddr_t locp = p + sizeof(char **) * MAX_KERNEL_ARGS; 210 1.1 uch size_t len; 211 1.1 uch TCHAR *w; 212 1.9 uch char *ptr; 213 1.1 uch 214 1.14 uch #define SETOPT(c) \ 215 1.1 uch __BEGIN_MACRO \ 216 1.1 uch argv[argc++] = ptokv(locp); \ 217 1.1 uch *loc++ =(c); \ 218 1.1 uch *loc++ = '\0'; \ 219 1.1 uch locp += 2; \ 220 1.1 uch __END_MACRO 221 1.1 uch // 1st arg is kernel name. 222 1.9 uch // DPRINTF_SETUP(); //if you want to use debug print, enable this line. 223 1.1 uch 224 1.1 uch w = _pref.kernel_user_file; 225 1.1 uch argv[argc++] = ptokv(locp); 226 1.1 uch len = WideCharToMultiByte(CP_ACP, 0, w, wcslen(w), 0, 0, 0, 0); 227 1.14 uch WideCharToMultiByte(CP_ACP, 0, w, len, loc, len, 0, 0); 228 1.1 uch loc[len] = '\0'; 229 1.1 uch loc += len + 1; 230 1.1 uch locp += len + 1; 231 1.1 uch 232 1.1 uch if (_pref.boot_serial) // serial console 233 1.1 uch SETOPT('h'); 234 1.1 uch if (_pref.boot_verbose) // boot verbosely 235 1.1 uch SETOPT('v'); 236 1.1 uch if (_pref.boot_single_user) // boot to single user 237 1.1 uch SETOPT('s'); 238 1.1 uch if (_pref.boot_ask_for_name) // ask for file name to boot from 239 1.1 uch SETOPT('a'); 240 1.8 enami if (_pref.boot_debugger) // break into the kernel debugger 241 1.8 enami SETOPT('d'); 242 1.1 uch 243 1.1 uch // boot from 244 1.1 uch switch(_pref.rootfs) { 245 1.1 uch case 0: // wd0 246 1.1 uch break; 247 1.1 uch case 1: // sd0 248 1.2 uch argv[argc++] = ptokv(locp); 249 1.2 uch strncpy(loc, "b=sd0", 6); 250 1.2 uch loc += 6; 251 1.2 uch locp += 6; 252 1.1 uch break; 253 1.1 uch case 2: // memory disk 254 1.1 uch w = _pref.rootfs_file; 255 1.1 uch argv[argc++] = ptokv(locp); 256 1.1 uch strncpy(loc, "m=", 2); 257 1.1 uch loc += 2; 258 1.1 uch len = WideCharToMultiByte(CP_ACP, 0, w, wcslen(w), 0, 0, 0, 0); 259 1.1 uch WideCharToMultiByte(CP_ACP, 0, w, len, loc, len, 0, 0); 260 1.1 uch loc[len] = '\0'; 261 1.1 uch loc += len + 1; 262 1.1 uch locp += 2 + len + 1; 263 1.1 uch break; 264 1.1 uch case 3: // nfs 265 1.1 uch argv[argc++] = ptokv(locp); 266 1.1 uch strncpy(loc, "b=nfs", 6); 267 1.1 uch loc += 6; 268 1.1 uch locp += 6; 269 1.1 uch break; 270 1.19 nonaka case 4: // dk0 271 1.19 nonaka argv[argc++] = ptokv(locp); 272 1.19 nonaka strncpy(loc, "b=dk0", 6); 273 1.19 nonaka loc += 6; 274 1.19 nonaka locp += 6; 275 1.19 nonaka break; 276 1.19 nonaka case 5: // ld0 277 1.19 nonaka argv[argc++] = ptokv(locp); 278 1.19 nonaka strncpy(loc, "b=ld0", 6); 279 1.19 nonaka loc += 6; 280 1.19 nonaka locp += 6; 281 1.19 nonaka break; 282 1.9 uch } 283 1.9 uch 284 1.9 uch // Extra kernel options. (Option tab window) 285 1.9 uch w = _pref.boot_extra; 286 1.9 uch len = WideCharToMultiByte(CP_ACP, 0, w, wcslen(w), 0, 0, 0, 0); 287 1.9 uch 288 1.9 uch if ((ptr = (char *)malloc(len)) == NULL) { 289 1.9 uch MessageBox(_root->_window, 290 1.10 uwe L"Can't allocate memory for extra kernel options.", 291 1.10 uwe TEXT("WARNING"), 292 1.10 uwe MB_ICONWARNING | MB_OK); 293 1.12 uwe UpdateWindow(_root->_window); 294 1.9 uch 295 1.14 uch return argc; 296 1.9 uch } 297 1.14 uch WideCharToMultiByte(CP_ACP, 0, w, len, ptr, len, 0, 0); 298 1.9 uch ptr[len]='\0'; 299 1.9 uch 300 1.9 uch while (*ptr == ' ' || *ptr == '\t') 301 1.9 uch ptr++; 302 1.9 uch while (*ptr != '\0') { 303 1.9 uch len = strcspn(ptr, " \t"); 304 1.9 uch if (len == 0) 305 1.9 uch len = strlen(ptr); 306 1.9 uch 307 1.9 uch if (argc == MAX_KERNEL_ARGS || locp + len + 1 > p + sz) { 308 1.9 uch MessageBox(_root->_window, 309 1.9 uch L"Too many extra kernel options.", 310 1.10 uwe TEXT("WARNING"), 311 1.10 uwe MB_ICONWARNING | MB_OK); 312 1.12 uwe UpdateWindow(_root->_window); 313 1.9 uch break; 314 1.9 uch } 315 1.9 uch argv[argc++] = ptokv(locp); 316 1.9 uch strncpy(loc, ptr, len); 317 1.9 uch loc[len] = '\0'; 318 1.9 uch loc += len + 1; 319 1.9 uch locp += len + 1; 320 1.14 uch 321 1.9 uch ptr += len; 322 1.9 uch while (*ptr == ' ' || *ptr == '\t') 323 1.9 uch ptr++; 324 1.1 uch } 325 1.1 uch 326 1.1 uch return argc; 327 1.1 uch } 328 1.1 uch 329 1.1 uch // kernel bootinfo. 330 1.1 uch void 331 1.1 uch HpcMenuInterface::setup_bootinfo(struct bootinfo &bi) 332 1.1 uch { 333 1.1 uch FrameBufferInfo fb(_pref.platid_hi, _pref.platid_lo); 334 1.1 uch TIME_ZONE_INFORMATION tz; 335 1.15 uwe DWORD tzid = GetTimeZoneInformation(&tz); 336 1.1 uch 337 1.1 uch memset(&bi, 0, sizeof(struct bootinfo)); 338 1.1 uch bi.length = sizeof(struct bootinfo); 339 1.1 uch bi.reserved = 0; 340 1.1 uch bi.magic = BOOTINFO_MAGIC; 341 1.1 uch bi.fb_addr = fb.addr(); 342 1.1 uch bi.fb_type = fb.type(); 343 1.1 uch bi.fb_line_bytes = fb.linebytes(); 344 1.1 uch bi.fb_width = fb.width(); 345 1.1 uch bi.fb_height = fb.height(); 346 1.1 uch bi.platid_cpu = _pref.platid_hi; 347 1.1 uch bi.platid_machine = _pref.platid_lo; 348 1.1 uch bi.timezone = tz.Bias; 349 1.15 uwe if (tzid == TIME_ZONE_ID_DAYLIGHT) 350 1.15 uwe bi.timezone += tz.DaylightBias; 351 1.1 uch } 352 1.1 uch 353 1.1 uch // Progress bar 354 1.1 uch void 355 1.13 uch HpcMenuInterface::progress(const char *msg) 356 1.1 uch { 357 1.14 uch 358 1.13 uch _root->progress(msg); 359 1.11 uwe } 360 1.11 uwe 361 1.11 uwe void 362 1.11 uwe HpcMenuInterface::unprogress() 363 1.11 uwe { 364 1.14 uch 365 1.11 uwe _root->unprogress(); 366 1.4 uch } 367 1.4 uch 368 1.6 uch // Boot kernel. 369 1.4 uch void 370 1.4 uch HpcMenuInterface::boot() 371 1.4 uch { 372 1.4 uch struct support_status *tab = _unsupported; 373 1.17 uwe uint32_t cpu = _pref.platid_hi; 374 1.17 uwe uint32_t machine = _pref.platid_lo; 375 1.4 uch 376 1.4 uch if (_pref.safety_message) 377 1.4 uch for (; tab->cpu; tab++) { 378 1.4 uch if (tab->cpu == cpu && tab->machine == machine) { 379 1.4 uch MessageBox(_root->_window, 380 1.7 uch tab->cause ? tab->cause : 381 1.10 uwe L"Not supported yet.", 382 1.10 uwe TEXT("BOOT FAILED"), 383 1.10 uwe MB_ICONERROR | MB_OK); 384 1.4 uch return; 385 1.4 uch } 386 1.4 uch } 387 1.4 uch 388 1.4 uch if (_boot_hook.func) 389 1.6 uch _boot_hook.func(_boot_hook.arg); 390 1.1 uch } 391