Home | History | Annotate | Line # | Download | only in hpcboot
hpcmenu.cpp revision 1.3.2.2
      1  1.3.2.2  nathanw /* -*-C++-*-	$NetBSD: hpcmenu.cpp,v 1.3.2.2 2001/06/21 19:22:44 nathanw 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.3.2.2  nathanw #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.3.2.1  nathanw 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.3.2.1  nathanw 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.3.2.2  nathanw HpcMenuInterface::HpcMenuInterface()
     68  1.3.2.2  nathanw {
     69  1.3.2.2  nathanw 	if (!load())
     70  1.3.2.2  nathanw 		_set_default_pref();
     71  1.3.2.2  nathanw 	_pref._version	= HPCBOOT_VERSION;
     72  1.3.2.2  nathanw 	_pref._size	= sizeof(HpcMenuPreferences);
     73  1.3.2.2  nathanw 
     74  1.3.2.2  nathanw 	_cons_parameter = 0;
     75  1.3.2.2  nathanw 	memset(_cons_hook, 0, sizeof(struct cons_hook_args) * 4);
     76  1.3.2.2  nathanw 	memset(&_boot_hook, 0, sizeof(struct boot_hook_args));
     77  1.3.2.2  nathanw }
     78  1.3.2.2  nathanw 
     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.3.2.1  nathanw 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.3.2.2  nathanw 	    (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.3.2.1  nathanw 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.3.2.2  nathanw void
    117  1.3.2.2  nathanw HpcMenuInterface::_set_default_pref()
    118  1.3.2.2  nathanw {
    119  1.3.2.2  nathanw 	_pref._magic		= HPCBOOT_MAGIC;
    120  1.3.2.2  nathanw 	_pref.dir		= 0;
    121  1.3.2.2  nathanw 	_pref.dir_user		= FALSE;
    122  1.3.2.2  nathanw 	_pref.kernel_user	= FALSE;
    123  1.3.2.2  nathanw 	_pref.platid_hi		= 0;
    124  1.3.2.2  nathanw 	_pref.platid_lo		= 0;
    125  1.3.2.2  nathanw 	_pref.rootfs		= 0;
    126  1.3.2.2  nathanw 	wsprintf(_pref.rootfs_file, TEXT("miniroot.fs"));
    127  1.3.2.2  nathanw 	_pref.boot_serial	= FALSE;
    128  1.3.2.2  nathanw 	_pref.boot_verbose	= FALSE;
    129  1.3.2.2  nathanw 	_pref.boot_single_user	= FALSE;
    130  1.3.2.2  nathanw 	_pref.boot_ask_for_name	= FALSE;
    131  1.3.2.2  nathanw 	_pref.boot_debugger	= FALSE;
    132  1.3.2.2  nathanw 	_pref.auto_boot		= 0;
    133  1.3.2.2  nathanw 	_pref.reverse_video	= FALSE;
    134  1.3.2.2  nathanw 	_pref.pause_before_boot	= TRUE;
    135  1.3.2.2  nathanw 	_pref.safety_message	= TRUE;
    136  1.3.2.2  nathanw #ifdef MIPS
    137  1.3.2.2  nathanw 	_pref.serial_speed	= 9600; // historical reason.
    138  1.3.2.2  nathanw #else
    139  1.3.2.2  nathanw 	_pref.serial_speed	= 19200;
    140  1.3.2.2  nathanw #endif
    141  1.3.2.2  nathanw }
    142  1.3.2.2  nathanw 
    143  1.3.2.2  nathanw //
    144  1.3.2.2  nathanw // load and save current menu status.
    145  1.3.2.2  nathanw //
    146      1.1      uch BOOL
    147  1.3.2.1  nathanw HpcMenuInterface::load()
    148      1.1      uch {
    149      1.1      uch 	TCHAR path[MAX_PATH];
    150      1.1      uch 
    151      1.1      uch 	if (!_find_pref_dir(path))
    152      1.1      uch 		return FALSE;
    153      1.1      uch 
    154      1.1      uch 	TCHAR filename[MAX_PATH];
    155      1.1      uch 	wsprintf(filename, TEXT("\\%s\\hpcboot.cnf"), path);
    156      1.1      uch 	HANDLE file = CreateFile(filename, GENERIC_READ, 0, 0, OPEN_EXISTING,
    157  1.3.2.2  nathanw 	    FILE_ATTRIBUTE_NORMAL, 0);
    158      1.1      uch 	if (file == INVALID_HANDLE_VALUE)
    159      1.1      uch 		return FALSE;
    160      1.1      uch 
    161      1.1      uch 	DWORD cnt;
    162      1.1      uch 	// read header.
    163      1.1      uch 	if (!ReadFile(file, &_pref, 12, &cnt, 0))
    164      1.1      uch 		goto bad;
    165      1.1      uch 	if (_pref._magic != HPCBOOT_MAGIC)
    166      1.1      uch 		goto bad;
    167      1.1      uch 	// read all.
    168      1.1      uch 	SetFilePointer(file, 0, 0, FILE_BEGIN);
    169      1.1      uch 	if (!ReadFile(file, &_pref, _pref._size, &cnt, 0))
    170      1.1      uch 		goto bad;
    171      1.1      uch 	CloseHandle(file);
    172      1.1      uch 
    173      1.1      uch 	return TRUE;
    174      1.1      uch  bad:
    175      1.1      uch 	CloseHandle(file);
    176      1.1      uch 	return FALSE;
    177      1.1      uch }
    178      1.1      uch 
    179      1.1      uch BOOL
    180  1.3.2.1  nathanw HpcMenuInterface::save()
    181      1.1      uch {
    182      1.1      uch 	TCHAR path[MAX_PATH];
    183      1.1      uch 
    184      1.1      uch 	if (_find_pref_dir(path)) {
    185      1.1      uch 		TCHAR filename[MAX_PATH];
    186      1.1      uch 		wsprintf(filename, TEXT("\\%s\\hpcboot.cnf"), path);
    187      1.1      uch 		HANDLE file = CreateFile(filename, GENERIC_WRITE, 0, 0,
    188  1.3.2.2  nathanw 		    CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 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.3.2.2  nathanw 	    (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.3.2.2  nathanw 	if (_pref.boot_debugger)	// break into the kernel debugger
    237  1.3.2.2  nathanw 		SETOPT('d');
    238      1.1      uch 
    239      1.1      uch 	// boot from
    240      1.1      uch 	switch(_pref.rootfs) {
    241      1.1      uch 	case 0:	// wd0
    242      1.1      uch 		break;
    243      1.1      uch 	case 1:	// sd0
    244      1.2      uch 		argv[argc++] = ptokv(locp);
    245      1.2      uch 		strncpy(loc, "b=sd0", 6);
    246      1.2      uch 		loc += 6;
    247      1.2      uch 		locp += 6;
    248      1.1      uch 		break;
    249      1.1      uch 	case 2:	// memory disk
    250      1.1      uch 		w = _pref.rootfs_file;
    251      1.1      uch 		argv[argc++] = ptokv(locp);
    252      1.1      uch 		strncpy(loc, "m=", 2);
    253      1.1      uch 		loc += 2;
    254      1.1      uch 		len = WideCharToMultiByte(CP_ACP, 0, w, wcslen(w), 0, 0, 0, 0);
    255      1.1      uch 		WideCharToMultiByte(CP_ACP, 0, w, len, loc, len, 0, 0);
    256      1.1      uch 		loc[len] = '\0';
    257      1.1      uch 		loc += len + 1;
    258      1.1      uch 		locp += 2 + len + 1;
    259      1.1      uch 		break;
    260      1.1      uch 	case 3:	// nfs
    261      1.1      uch 		argv[argc++] = ptokv(locp);
    262      1.1      uch 		strncpy(loc, "b=nfs", 6);
    263      1.1      uch 		loc += 6;
    264      1.1      uch 		locp += 6;
    265      1.1      uch 		break;
    266      1.1      uch 	}
    267      1.1      uch 
    268      1.1      uch 	return argc;
    269      1.1      uch }
    270      1.1      uch 
    271      1.1      uch // kernel bootinfo.
    272      1.1      uch void
    273      1.1      uch HpcMenuInterface::setup_bootinfo(struct bootinfo &bi)
    274      1.1      uch {
    275      1.1      uch 	FrameBufferInfo fb(_pref.platid_hi, _pref.platid_lo);
    276      1.1      uch 	TIME_ZONE_INFORMATION tz;
    277      1.1      uch 	GetTimeZoneInformation(&tz);
    278      1.1      uch 
    279      1.1      uch 	memset(&bi, 0, sizeof(struct bootinfo));
    280      1.1      uch 	bi.length		= sizeof(struct bootinfo);
    281      1.1      uch 	bi.reserved		= 0;
    282      1.1      uch 	bi.magic		= BOOTINFO_MAGIC;
    283      1.1      uch 	bi.fb_addr		= fb.addr();
    284      1.1      uch 	bi.fb_type		= fb.type();
    285      1.1      uch 	bi.fb_line_bytes	= fb.linebytes();
    286      1.1      uch 	bi.fb_width		= fb.width();
    287      1.1      uch 	bi.fb_height		= fb.height();
    288      1.1      uch 	bi.platid_cpu		= _pref.platid_hi;
    289      1.1      uch 	bi.platid_machine	= _pref.platid_lo;
    290      1.1      uch 	bi.timezone		= tz.Bias;
    291      1.1      uch }
    292      1.1      uch 
    293      1.1      uch // Progress bar
    294      1.1      uch void
    295  1.3.2.1  nathanw HpcMenuInterface::progress()
    296      1.1      uch {
    297      1.1      uch 	SendMessage(_root->_progress_bar->_window, PBM_STEPIT, 0, 0);
    298  1.3.2.1  nathanw }
    299  1.3.2.1  nathanw 
    300  1.3.2.2  nathanw // Boot kernel.
    301  1.3.2.1  nathanw void
    302  1.3.2.1  nathanw HpcMenuInterface::boot()
    303  1.3.2.1  nathanw {
    304  1.3.2.1  nathanw 	struct support_status *tab = _unsupported;
    305  1.3.2.1  nathanw 	u_int32_t cpu = _pref.platid_hi;
    306  1.3.2.1  nathanw 	u_int32_t machine = _pref.platid_lo;
    307  1.3.2.1  nathanw 
    308  1.3.2.1  nathanw 	if (_pref.safety_message)
    309  1.3.2.1  nathanw 		for (; tab->cpu; tab++) {
    310  1.3.2.1  nathanw 			if (tab->cpu == cpu && tab->machine == machine) {
    311  1.3.2.1  nathanw 				MessageBox(_root->_window,
    312  1.3.2.2  nathanw 				    tab->cause ? tab->cause :
    313  1.3.2.2  nathanw 				    L"not supported yet.",
    314  1.3.2.2  nathanw 				    TEXT("BOOT FAILED"), 0);
    315  1.3.2.1  nathanw 				return;
    316  1.3.2.1  nathanw 			}
    317  1.3.2.1  nathanw 		}
    318  1.3.2.1  nathanw 
    319  1.3.2.1  nathanw 	if (_boot_hook.func)
    320  1.3.2.2  nathanw 		_boot_hook.func(_boot_hook.arg);
    321      1.1      uch }
    322