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