Home | History | Annotate | Line # | Download | only in pbsdboot
main.c revision 1.13
      1 /*	$NetBSD: main.c,v 1.13 1999/12/09 02:51:30 jun Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999 Shin Takemura.
      5  * All rights reserved.
      6  *
      7  * This software is part of the PocketBSD.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed by the PocketBSD project
     20  *	and its contributors.
     21  * 4. Neither the name of the project nor the names of its contributors
     22  *    may be used to endorse or promote products derived from this software
     23  *    without specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  * SUCH DAMAGE.
     36  *
     37  */
     38 #include <pbsdboot.h>
     39 #include <commctrl.h>
     40 #include <res/resource.h>
     41 
     42 /*-----------------------------------------------------------------------------
     43 
     44   type difinitions
     45 
     46 -----------------------------------------------------------------------------*/
     47 enum {
     48 	UPDATE_DLGBOX,
     49 	UPDATE_DATA,
     50 };
     51 
     52 struct fb_type {
     53 	int type;
     54 	TCHAR *name;
     55 };
     56 
     57 struct fb_setting {
     58 	TCHAR *name;
     59 	int type;
     60 	int width, height, linebytes;
     61 	long addr;
     62 	unsigned long platid_cpu, platid_machine;
     63 };
     64 
     65 /*-----------------------------------------------------------------------------
     66 
     67   variable declarations
     68 
     69 -----------------------------------------------------------------------------*/
     70 HINSTANCE  hInst = NULL;
     71 HWND		hWndMain;
     72 HWND		hWndCB = NULL;
     73 HWND		hDlgLoad = NULL;
     74 unsigned int	dlgStatus;
     75 int		user_define_idx;
     76 
     77 /*-----------------------------------------------------------------------------
     78 
     79   data
     80 
     81 -----------------------------------------------------------------------------*/
     82 TCHAR szAppName[ ] = TEXT("PocketBSD boot");
     83 TCHAR szTitle[ ]   = TEXT("Welcome to PocketBSD!");
     84 
     85 struct fb_type fb_types[] = {
     86 	{ BIFB_D2_M2L_3,	TEXT(BIFBN_D2_M2L_3)	},
     87 	{ BIFB_D2_M2L_3x2,	TEXT(BIFBN_D2_M2L_3x2)	},
     88 	{ BIFB_D2_M2L_0,	TEXT(BIFBN_D2_M2L_0)	},
     89 	{ BIFB_D8_00,		TEXT(BIFBN_D8_00)	},
     90 	{ BIFB_D8_FF,		TEXT(BIFBN_D8_FF)	},
     91 	{ BIFB_D16_0000,	TEXT(BIFBN_D16_0000)	},
     92 	{ BIFB_D16_FFFF,	TEXT(BIFBN_D16_FFFF)	},
     93 };
     94 
     95 int fb_size[] = {
     96 	160, 240, 320, 400, 480, 600, 640,
     97 	768, 800, 1024, 1150, 1280, 1600
     98 };
     99 
    100 int fb_bpl[] = {
    101 	40, 80, 128, 160, 240, 256, 320,
    102 	384, 400, 480, 512, 600, 640, 768, 800, 1024, 1150, 1280, 1600
    103 };
    104 
    105 struct fb_setting fb_settings[] = {
    106 	{ NULL, BIFB_D2_M2L_3,
    107 		320, 240, 80, 0xa000000,
    108 		PLATID_UNKNOWN, PLATID_UNKNOWN },
    109 	{ TEXT("FreeStyle"), BIFB_D2_M2L_3,
    110 		320, 240, 80, 0xa000000,
    111 		PLATID_CPU_MIPS_VR_41XX, PLATID_MACH_EVEREX_FREESTYLE_AXX },
    112 	{ TEXT("FreeStyle(Small Font)"), BIFB_D2_M2L_3x2,
    113 		640, 240, 80, 0xa000000,
    114 		PLATID_CPU_MIPS_VR_41XX, PLATID_MACH_EVEREX_FREESTYLE_AXX },
    115 	{ TEXT("MobileGear MC-CS1X"), BIFB_D2_M2L_0,
    116 		480, 240, 256, 0xa000000,
    117 		PLATID_CPU_MIPS_VR_4102, PLATID_MACH_NEC_MCCS_1X },
    118 	{ TEXT("MobileGearII MC-R300"), BIFB_D2_M2L_0,
    119 		640, 240, 256, 0xa000000,
    120 		PLATID_CPU_MIPS_VR_4111, PLATID_MACH_NEC_MCR_300 },
    121 	{ TEXT("MobileGearII for DoCoMo"), BIFB_D2_M2L_0,
    122 		640, 240, 256, 0xa000000,
    123 		PLATID_CPU_MIPS_VR_4111, PLATID_MACH_NEC_MCR_FORDOCOMO },
    124 	{ TEXT("MobileGearII MC-R320"), BIFB_D2_M2L_0,
    125 		640, 240, 160, 0xa000000,
    126 		PLATID_CPU_MIPS_VR_4121, PLATID_MACH_NEC_MCR_320 },
    127 	{ TEXT("MobileGearII MC-R500"), BIFB_D8_FF,
    128 		640, 240, 1024, 0x13000000,
    129 		PLATID_CPU_MIPS_VR_4111, PLATID_MACH_NEC_MCR_500 },
    130 	{ TEXT("Mobile Pro 750c"), BIFB_D8_FF,
    131 		640, 240, 1024, 0x13000000,
    132 		PLATID_CPU_MIPS_VR_4111, PLATID_MACH_NEC_MCR_500A },
    133 	{ TEXT("MobileGearII MC-R510"), BIFB_D8_00,
    134 		640, 240, 1024, 0xa000000,
    135 		PLATID_CPU_MIPS_VR_4121, PLATID_MACH_NEC_MCR_510 },
    136 	{ TEXT("NEC MC-R510(15bit color)"), BIFB_D16_0000,
    137 		640, 240, 1600, 0xa000000,
    138 		PLATID_CPU_MIPS_VR_4121, PLATID_MACH_NEC_MCR_510 },
    139 	{ TEXT("MobileGearII MC-R520"), BIFB_D16_0000,
    140 		640, 240, 1600, 0xa000000,
    141 		PLATID_CPU_MIPS_VR_4121, PLATID_MACH_NEC_MCR_520 },
    142 	{ TEXT("Mobile Pro 770"), BIFB_D16_0000,
    143 		640, 240, 1600, 0xa000000,
    144 		PLATID_CPU_MIPS_VR_4121, PLATID_MACH_NEC_MCR_520A },
    145 	{ TEXT("MobileGearII MC-R700"), BIFB_D16_0000,
    146 		800, 600, 1600, 0xa000000,
    147 		PLATID_CPU_MIPS_VR_4121, PLATID_MACH_NEC_MCR_700 },
    148 	{ TEXT("Mobile Pro 800"), BIFB_D16_0000,
    149 		800, 600, 1600, 0xa000000,
    150 		PLATID_CPU_MIPS_VR_4121, PLATID_MACH_NEC_MCR_700A },
    151 	{ TEXT("Tripad PV-6000"), BIFB_D8_FF,
    152 		640, 480, 640, 0xa000000,
    153 		PLATID_CPU_MIPS_VR_4111, PLATID_MACH_SHARP_TRIPAD_PV6000 },
    154 	{ TEXT("Vadem Clio"), BIFB_D8_FF,
    155 		640, 480, 640, 0xa000000,
    156 		PLATID_CPU_MIPS_VR_4111, PLATID_MACH_SHARP_TRIPAD_PV6000 },
    157 	{ TEXT("E-55"), BIFB_D2_M2L_0,
    158 		240, 320, 256, 0xa000000,
    159 		PLATID_CPU_MIPS_VR_4111, PLATID_MACH_CASIO_CASSIOPEIAE_E55 },
    160 	{ TEXT("E-55(Small Font)"), BIFB_D2_M2L_0x2,
    161 		480, 320, 256, 0xa000000,
    162 		PLATID_CPU_MIPS_VR_4111, PLATID_MACH_CASIO_CASSIOPEIAE_E55 },
    163 	{ TEXT("E-100"), BIFB_D16_FFFF,
    164 		240, 320, 512, 0xa200000,
    165 		PLATID_CPU_MIPS_VR_4121, PLATID_MACH_CASIO_CASSIOPEIAE_E100 },
    166 	{ TEXT("E-500"), BIFB_D16_FFFF,
    167 		240, 320, 512, 0xa200000,
    168 		PLATID_CPU_MIPS_VR_4121, PLATID_MACH_CASIO_CASSIOPEIAE_E500 },
    169 	{ TEXT("INTERTOP CX300"), BIFB_D8_FF,
    170 		640, 480, 640, 0xa000000,
    171 		PLATID_CPU_MIPS_VR_4121, PLATID_MACH_FUJITSU_INTERTOP_IT300 },
    172 	{ TEXT("IBM WorkPad z50"), BIFB_D16_0000,
    173 		640, 480, 1280, 0xa000000,
    174 		PLATID_CPU_MIPS_VR_4121, PLATID_MACH_IBM_WORKPAD_Z50 },
    175 	{ TEXT("Philips Nino 312"), BIFB_D2_M2L_0,
    176 		240, 320, 0, 0,
    177 		PLATID_CPU_MIPS_TX_3912, PLATID_MACH_PHILIPS_NINO_312 },
    178 	{ TEXT("Compaq C-series 810"), BIFB_D2_M2L_0,
    179 		640, 240, 0, 0,
    180 		PLATID_CPU_MIPS_TX_3912, PLATID_MACH_COMPAQ_C_810 },
    181 	{ TEXT("Compaq C-series 2010c"), BIFB_D8_FF,
    182 		640, 240, 0, 0,
    183 		PLATID_CPU_MIPS_TX_3912, PLATID_MACH_COMPAQ_C_2010 },
    184 	{ TEXT("Compaq C-series 2015c"), BIFB_D8_FF,
    185 		640, 240, 0, 0,
    186 		PLATID_CPU_MIPS_TX_3912, PLATID_MACH_COMPAQ_C_2015 },
    187 	{ TEXT("Compaq PRESARIO 213"), BIFB_D8_00,
    188 		320, 240, 0, 0,
    189 		PLATID_CPU_MIPS_VR_4111, PLATID_MACH_COMPAQ_PRESARIO_213 },
    190 	{ TEXT("Victor InterLink MP-C101"), BIFB_D16_0000,
    191 		640, 480, 0, 0,
    192 		PLATID_CPU_MIPS_TX_3922, PLATID_MACH_VICTOR_INTERLINK_MPC101},
    193 	{ TEXT("Sharp Telios HC-AJ1"), BIFB_D16_0000,
    194 		800, 600, 0, 0,
    195 		PLATID_CPU_MIPS_TX_3922, PLATID_MACH_SHARP_TELIOS_HCAJ1},
    196 };
    197 
    198 #define ARRAYSIZEOF(a)	(sizeof(a)/sizeof(*(a)))
    199 
    200 #ifdef UNDER_CE
    201 	/* 'memory card' in HANKAKU KANA */
    202 #define UNICODE_MEMORY_CARD \
    203 	TEXT('\\'), 0xff92, 0xff93, 0xff98, TEXT(' '), 0xff76, 0xff70, \
    204 	0xff84, 0xff9e
    205 TCHAR unicode_memory_card[] = { UNICODE_MEMORY_CARD,  TEXT('\\') };
    206 TCHAR unicode_memory_card2[] = { UNICODE_MEMORY_CARD,  TEXT('2'), TEXT('\\') };
    207 #endif
    208 
    209 TCHAR* path_list[] = {
    210 	TEXT("\\Storage Card\\"),
    211 	TEXT("/"),
    212 	TEXT("\\"),
    213 	TEXT("\\Storage Card2\\"),
    214 #ifdef UNDER_CE
    215 	unicode_memory_card,
    216 	unicode_memory_card2,
    217 #endif
    218 };
    219 int path_list_items = ARRAYSIZEOF(path_list);
    220 
    221 #ifdef ADDITIONAL_KERNELS
    222 TCHAR* kernel_list[] = {
    223 
    224 };
    225 int kernel_list_items = ARRAYSIZEOF(kernel_list);
    226 #endif
    227 
    228 /*-----------------------------------------------------------------------------
    229 
    230   function prototypes
    231 
    232 -----------------------------------------------------------------------------*/
    233 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    234 void SetBootInfo(struct bootinfo *bi, struct fb_setting *fbs);
    235 void wstrcpy(TCHAR* dst, TCHAR* src);
    236 
    237 /*-----------------------------------------------------------------------------
    238 
    239   function definitions
    240 
    241 -----------------------------------------------------------------------------*/
    242 void wstrcpy(TCHAR* dst, TCHAR* src)
    243 {
    244 	while (*src) {
    245 		*dst++ = *src++;
    246 	}
    247 	*dst = *src;
    248 }
    249 
    250 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    251                     LPTSTR lpCmdLine, int nCmdShow )
    252 {
    253 	MSG          msg;
    254 	WNDCLASS     wc;
    255 	int i, idx;
    256 	RECT rect;
    257 
    258 	wc.style          = (UINT)NULL;
    259 	wc.lpfnWndProc    = (WNDPROC) WndProc;
    260 	wc.cbClsExtra     = 0;
    261 	wc.cbWndExtra     = 0;
    262 	wc.hInstance      = hInstance;
    263 	wc.hIcon          = NULL;
    264 	wc.hCursor        = NULL;
    265 	wc.hbrBackground  = (HBRUSH) GetStockObject(WHITE_BRUSH);
    266 	wc.lpszMenuName   = NULL;
    267 	wc.lpszClassName  = whoami;
    268 
    269 	RegisterClass(&wc);
    270 
    271 	InitCommonControls();   // Initialize common controls - command bar
    272 	hInst = hInstance;      // Save handle to create command bar
    273 
    274 	hardware_test();
    275 
    276 	/*
    277 	 *	Main Window
    278          */
    279 	hWndMain = CreateWindow(szAppName,		// Class
    280 				szTitle,		// Title
    281 				WS_VISIBLE,		// Style
    282 				CW_USEDEFAULT,		// x-position
    283 				CW_USEDEFAULT,		// y-position
    284 				CW_USEDEFAULT,		// x-size
    285 				CW_USEDEFAULT,		// y-size
    286 				NULL,			// Parent handle
    287 				NULL,			// Menu handle
    288 				hInstance,		// Instance handle
    289 				NULL);			// Creation
    290 
    291 	GetClientRect(hWndMain, &rect);
    292 	if (rect.right < rect.bottom) {
    293 		/*
    294 		 *	portrait
    295 		 */
    296 		CreateMainWindow(hInst, hWndMain,
    297 				 MAKEINTRESOURCE(IDD_MAIN_240X320),
    298 				 CommandBar_Height(hWndCB));
    299 	} else {
    300 		/*
    301 		 *	landscape
    302 		 */
    303 		CreateMainWindow(hInst, hWndMain,
    304 				 MAKEINTRESOURCE(IDD_MAIN_640X240),
    305 				 CommandBar_Height(hWndCB));
    306 	}
    307 
    308 	/*
    309 	 *  load preferences
    310 	 */
    311 	pref_init(&pref);
    312 	if (pref_load(path_list, path_list_items) == 0) {
    313 		TCHAR tmp[256];
    314 		wsprintf(tmp, TEXT("%s is loaded."), where_pref_load_from);
    315 		SetDlgItemText(hWndMain, IDC_STATUS, tmp);
    316 
    317 		fb_settings[0].type = pref.fb_type;
    318 		fb_settings[0].width = pref.fb_width;
    319 		fb_settings[0].height = pref.fb_height;
    320 		fb_settings[0].linebytes = pref.fb_linebytes;
    321 		fb_settings[0].addr = pref.fb_addr;
    322 		fb_settings[0].platid_cpu = pref.platid_cpu;
    323 		fb_settings[0].platid_machine = pref.platid_machine;
    324 	} else {
    325 		TCHAR tmpbuf[PATHBUFLEN];
    326 		wsprintf(tmpbuf, TEXT("%s%S"), path_list[0], "netbsd");
    327 		SetDlgItemText(hWndMain, IDC_STATUS,
    328 			       TEXT("preferences not loaded."));
    329 
    330 		pref.setting_idx = 1;
    331 		pref.fb_type = fb_settings[0].type;
    332 		pref.fb_width = fb_settings[0].width;
    333 		pref.fb_height = fb_settings[0].height;
    334 		pref.fb_linebytes = fb_settings[0].linebytes;
    335 		pref.fb_addr = fb_settings[0].addr;
    336 		pref.platid_cpu = fb_settings[0].platid_cpu;
    337 		pref.platid_machine = fb_settings[0].platid_machine;
    338 		wstrcpy(pref.setting_name, TEXT("User defined"));
    339 		wstrcpy(pref.kernel_name, tmpbuf);
    340 		wstrcpy(pref.options, TEXT(""));
    341 		pref.check_last_chance = FALSE;
    342 		pref.load_debug_info = FALSE;
    343 		pref.serial_port = FALSE;
    344 	}
    345 	fb_settings[0].name = pref.setting_name;
    346 
    347 	/*
    348 	 *  initialize kernel file name list.
    349 	 */
    350 	for (i = 0; i < path_list_items; i++) {
    351 		TCHAR tmpbuf[1024];
    352 		wsprintf(tmpbuf, TEXT("%s%S"), path_list[i], "netbsd");
    353 		SendDlgItemMessage(hWndMain, IDC_KERNEL, CB_ADDSTRING, 0,
    354 				   (LPARAM)tmpbuf);
    355 	}
    356 #ifdef ADDITIONAL_KERNELS
    357 	for (i = 0; i < kernel_list_items; i++) {
    358 		SendDlgItemMessage(hWndMain, IDC_KERNEL, CB_ADDSTRING, 0,
    359 				   (LPARAM)kernel_list[i]);
    360 	}
    361 #endif
    362 	/*
    363 	SendDlgItemMessage(hWndMain, IDC_KERNEL, CB_SETCURSEL, 0,
    364 			   (LPARAM)NULL);
    365 	*/
    366 	SetDlgItemText(hWndMain, IDC_KERNEL, pref.kernel_name);
    367 	SetDlgItemText(hWndMain, IDC_OPTIONS, pref.options);
    368 
    369 	/*
    370 	 *  Frame Buffer setting names.
    371 	 */
    372 	for (i = 0; i < ARRAYSIZEOF(fb_settings); i++) {
    373 		idx = SendDlgItemMessage(hWndMain, IDC_FBSELECT, CB_ADDSTRING,
    374 					 0, (LPARAM)fb_settings[i].name);
    375 		SendDlgItemMessage(hWndMain, IDC_FBSELECT,
    376 				   CB_SETITEMDATA, idx, (LPARAM)i);
    377 		if (i == 0) {
    378 			user_define_idx = idx;
    379 		}
    380 	}
    381 	SendDlgItemMessage(hWndMain, IDC_FBSELECT, CB_SETCURSEL,
    382 			   pref.setting_idx, (LPARAM)NULL);
    383 
    384 	/*
    385 	 *  Check box, 'Pause before boot'
    386 	 */
    387 	SendDlgItemMessage(hWndMain, IDC_PAUSE, BM_SETCHECK,
    388 			   pref.check_last_chance, 0);
    389 	SendDlgItemMessage(hWndMain, IDC_DEBUG, BM_SETCHECK,
    390 			   pref.load_debug_info, 0);
    391 	SendDlgItemMessage(hWndMain, IDC_COMM, BM_SETCHECK,
    392 			   pref.serial_port, 0);
    393 
    394 	/*
    395 	 *  Map window and message loop
    396 	 */
    397 	ShowWindow(hWndMain, SW_SHOW);
    398 	UpdateWindow(hWndMain);
    399 	while ( GetMessage(&msg, NULL, 0, 0) != FALSE ) {
    400 		TranslateMessage(&msg);
    401 		DispatchMessage(&msg);
    402 	}
    403 
    404 	return(msg.wParam);
    405 }
    406 
    407 BOOL CALLBACK DlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    408 {
    409 	switch (message) {
    410 	case WM_INITDIALOG:
    411 		return (1);
    412 
    413 	case WM_COMMAND:
    414 		switch (LOWORD(wParam)) {
    415 		case IDCANCEL:
    416 			dlgStatus = IDCANCEL;
    417 			break;
    418 		}
    419 		break;
    420 	default:
    421 		return (0);
    422 	}
    423 }
    424 
    425 BOOL CALLBACK DlgProc2(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    426 {
    427 	switch (message) {
    428 	case WM_INITDIALOG:
    429 		SetDlgItemText(hWnd, IDC_ABOUT_EDIT,
    430 			       TEXT("PocketBSD boot loader\r\n")
    431 			       TEXT("Version 1.7.4 1999.11.27\r\n")
    432 			       TEXT("\r\n")
    433 			       TEXT("Copyright(C) 1999 Shin Takemura,\r\n")
    434 			       TEXT("All rights reserved.\r\n")
    435 			       TEXT("\r\n")
    436 			       TEXT("http://www02.u-page.so-net.ne.jp/ca2/takemura/\r\n")
    437 			       );
    438 		return (1);
    439 
    440 	case WM_COMMAND:
    441 		switch (LOWORD(wParam)) {
    442 		case IDC_ABOUT_EDIT:
    443 			switch (HIWORD(wParam)) {
    444 			case EN_SETFOCUS:
    445 				//SendDlgItemMessage(hWnd, IDC_ABOUT_EDIT, EM_SETSEL, -1, 0);
    446 				SetFocus(GetDlgItem(hWnd, IDC_ABOUT_BITMAP));
    447 				break;
    448 			}
    449 			break;
    450 
    451 		case IDCANCEL:
    452 			EndDialog(hWnd, LOWORD(wParam));
    453 			return (1);
    454 		}
    455 		break;
    456 	default:
    457 		return (0);
    458 	}
    459 }
    460 
    461 void
    462 SetBootInfo(struct bootinfo *bi, struct fb_setting *fbs)
    463 {
    464 	TIME_ZONE_INFORMATION tz;
    465 
    466 	GetTimeZoneInformation(&tz);
    467 	memset(bi, 0, sizeof(struct bootinfo));
    468 	bi->length = sizeof(struct bootinfo);
    469 	bi->reserved = 0;
    470 	bi->magic = BOOTINFO_MAGIC;
    471 	bi->fb_addr = (unsigned char*)(fbs->addr + 0xA0000000);
    472 	bi->fb_type = fbs->type;
    473 	bi->fb_line_bytes = fbs->linebytes;
    474 	bi->fb_width = fbs->width;
    475 	bi->fb_height = fbs->height;
    476 	bi->platid_cpu = fbs->platid_cpu;
    477 	bi->platid_machine = fbs->platid_machine;
    478 	bi->timezone = tz.Bias;
    479 
    480 	debug_printf(TEXT("fb setting: %s fb_type=%d 0x%X %dx%d %d\n"),
    481 		     fbs->name,
    482 		     bi->fb_type, bi->fb_addr,
    483 		     bi->fb_width, bi->fb_height, bi->fb_line_bytes);
    484 	debug_printf(TEXT("timezone: %02ld:00\n"), (bi->timezone / 60));
    485 }
    486 
    487 
    488 void
    489 UpdateFbDlg(HWND hWnd, struct fb_setting *fbs, int direction)
    490 {
    491 	int i;
    492 	TCHAR tmpbuf[PATHBUFLEN];
    493 	int type, width, height, linebytes;
    494 	long addr;
    495 
    496 	switch (direction) {
    497 	case UPDATE_DLGBOX:
    498 		SetDlgItemText(hWnd, IDC_FB_NAME, fbs->name);
    499 
    500 		for (i = 0; i < ARRAYSIZEOF(fb_types); i++) {
    501 			if (fb_types[i].type == fbs->type) break;
    502 		}
    503 		if (ARRAYSIZEOF(fb_types) <= i) {
    504 			MessageBox(NULL, TEXT("Unknown FrameBuffer type."),
    505 				   szAppName, MB_OK);
    506 			return;
    507 		}
    508 		debug_printf(TEXT("UpdateFbDlg(%s)\n"), fbs->name);
    509 		i = SendDlgItemMessage(hWnd, IDC_FB_TYPE, CB_FINDSTRINGEXACT,
    510 				       0, (LPARAM)fb_types[i].name);
    511 		SendDlgItemMessage(hWnd, IDC_FB_TYPE, CB_SETCURSEL, i, 0);
    512 
    513 		wsprintf(tmpbuf, TEXT("%X"), fbs->addr);
    514 		SetDlgItemText(hWnd, IDC_FB_ADDR, tmpbuf);
    515 		wsprintf(tmpbuf, TEXT("%d"), fbs->width);
    516 		SetDlgItemText(hWnd, IDC_FB_WIDTH, tmpbuf);
    517 		wsprintf(tmpbuf, TEXT("%d"), fbs->height);
    518 		SetDlgItemText(hWnd, IDC_FB_HEIGHT, tmpbuf);
    519 		wsprintf(tmpbuf, TEXT("%d"), fbs->linebytes);
    520 		SetDlgItemText(hWnd, IDC_FB_LINEBYTES, tmpbuf);
    521 		wsprintf(tmpbuf, TEXT("%08X"), fbs->platid_cpu);
    522 		SetDlgItemText(hWnd, IDC_FB_CPU, tmpbuf);
    523 		wsprintf(tmpbuf, TEXT("%08X"), fbs->platid_machine);
    524 		SetDlgItemText(hWnd, IDC_FB_MACHINE, tmpbuf);
    525 		break;
    526 	case UPDATE_DATA:
    527 		GetDlgItemText(hWnd, IDC_FB_NAME, fbs->name, PATHBUFLEN);
    528 		type = SendDlgItemMessage(hWnd, IDC_FB_TYPE,
    529 					  CB_GETCURSEL, 0, 0);
    530 		type = SendDlgItemMessage(hWnd, IDC_FB_TYPE,
    531 					  CB_GETITEMDATA, type, 0);
    532 		GetDlgItemText(hWnd, IDC_FB_WIDTH, tmpbuf, sizeof(tmpbuf));
    533 		width = _tcstol(tmpbuf, NULL, 10);
    534 		GetDlgItemText(hWnd, IDC_FB_HEIGHT, tmpbuf, sizeof(tmpbuf));
    535 		height = _tcstol(tmpbuf, NULL, 10);
    536 		GetDlgItemText(hWnd, IDC_FB_LINEBYTES, tmpbuf, sizeof(tmpbuf));
    537 		linebytes = _tcstol(tmpbuf, NULL, 10);
    538 		GetDlgItemText(hWnd, IDC_FB_ADDR, tmpbuf, sizeof(tmpbuf));
    539 		addr = _tcstoul(tmpbuf, NULL, 16);
    540 		GetDlgItemText(hWnd, IDC_FB_CPU, tmpbuf, sizeof(tmpbuf));
    541 		fbs->platid_cpu = _tcstoul(tmpbuf, NULL, 16);
    542 		GetDlgItemText(hWnd, IDC_FB_MACHINE, tmpbuf, sizeof(tmpbuf));
    543 		fbs->platid_machine = _tcstoul(tmpbuf, NULL, 16);
    544 		fbs->type = type;
    545 		fbs->addr = addr;
    546 		fbs->width = width;
    547 		fbs->height = height;
    548 		fbs->linebytes = linebytes;
    549 
    550 		debug_printf(TEXT("type=%d  %dx%d  %d bytes/line %08x %08x\n"),
    551 			     type, width, height, linebytes,
    552 			     fbs->platid_cpu,
    553 			     fbs->platid_machine);
    554 		break;
    555 	default:
    556 		debug_printf(TEXT("UpdateFbDlg(): internal error!\n"));
    557 		break;
    558 	}
    559 }
    560 
    561 BOOL CALLBACK FbDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    562 {
    563 	int idx, i;
    564 	TCHAR tmpbuf[100];
    565 
    566 	switch (message) {
    567 	case WM_INITDIALOG:
    568 		{
    569 		UDACCEL uda;
    570 		for (i = 0; i < ARRAYSIZEOF(fb_settings); i++) {
    571 			idx = SendDlgItemMessage(hWnd, IDC_FB_NAME,
    572 						 CB_ADDSTRING, 0,
    573 						 (LPARAM)fb_settings[i].name);
    574 			SendDlgItemMessage(hWnd, IDC_FB_NAME,
    575 					   CB_SETITEMDATA, idx, (LPARAM)i);
    576 		}
    577 		for (i = 0; i < ARRAYSIZEOF(fb_size); i++) {
    578 			wsprintf(tmpbuf, TEXT("%d"), fb_size[i]);
    579 			SendDlgItemMessage(hWnd, IDC_FB_WIDTH, CB_ADDSTRING,
    580 					   0, (LPARAM)tmpbuf);
    581 			SendDlgItemMessage(hWnd, IDC_FB_HEIGHT, CB_ADDSTRING,
    582 					   0, (LPARAM)tmpbuf);
    583 		}
    584 		for (i = 0; i < ARRAYSIZEOF(fb_bpl); i++) {
    585 			wsprintf(tmpbuf, TEXT("%d"), fb_bpl[i]);
    586 			SendDlgItemMessage(hWnd, IDC_FB_LINEBYTES,
    587 					   CB_ADDSTRING, 0,
    588 					   (LPARAM)tmpbuf);
    589 		}
    590 		for (i = 0; i < ARRAYSIZEOF(fb_types); i++) {
    591 			idx = SendDlgItemMessage(hWnd, IDC_FB_TYPE,
    592 						 CB_ADDSTRING, 0,
    593 						 (LPARAM)fb_types[i].name);
    594 			SendDlgItemMessage(hWnd, IDC_FB_TYPE, CB_SETITEMDATA,
    595 					   idx, (LPARAM)fb_types[i].type);
    596 		}
    597 		UpdateFbDlg(hWnd, &fb_settings[0], UPDATE_DLGBOX);
    598 
    599 		uda.nSec = 1;
    600 		uda.nInc = 0x100;
    601 		/*
    602 		SendDlgItemMessage(hWnd, IDC_FB_ADDRSPIN, UDM_SETACCEL,
    603 				   0, (LPARAM)&uda);
    604 		*/
    605 		/*
    606 		SendDlgItemMessage(hWnd, IDC_FB_ADDRSPIN, UDM_SETRANGE,
    607 		                   0, MAKELPARAM(UD_MAXVAL, UD_MINVAL));
    608 		*/
    609 		}
    610 		return (1);
    611 
    612 	case WM_VSCROLL:
    613 		if ((HWND)lParam == GetDlgItem(hWnd, IDC_FB_ADDRSPIN)) {
    614 			long addr;
    615 			switch (LOWORD(wParam)) {
    616 			case SB_THUMBPOSITION:
    617 			case SB_THUMBTRACK:
    618 				GetDlgItemText(hWnd, IDC_FB_ADDR, tmpbuf, 100);
    619 				addr = _tcstoul(tmpbuf, NULL, 16);
    620 				if (50 < HIWORD(wParam)) {
    621 					addr -= 0x400;
    622 				} else {
    623 					addr += 0x400;
    624 				}
    625 				SendDlgItemMessage(hWnd, IDC_FB_ADDRSPIN,
    626 						   UDM_SETPOS, 0,
    627 						   MAKELPARAM(50, 0));
    628 				wsprintf(tmpbuf, TEXT("%X"), addr);
    629 				SetDlgItemText(hWnd, IDC_FB_ADDR, tmpbuf);
    630 				return (1);
    631 			}
    632 		}
    633 		break;
    634 
    635 	case WM_COMMAND:
    636 		switch (LOWORD(wParam)) {
    637 		case IDC_FB_NAME:
    638 			switch (HIWORD(wParam)) {
    639 			case CBN_SELCHANGE:
    640 				idx = SendDlgItemMessage(hWnd, IDC_FB_NAME,
    641 							 CB_GETCURSEL, 0, 0);
    642 				i = SendDlgItemMessage(hWnd, IDC_FB_NAME,
    643 						       CB_GETITEMDATA, idx, 0);
    644 				if (0 <= i && i < ARRAYSIZEOF(fb_settings)) {
    645 					fb_settings[0] = fb_settings[i];
    646 					UpdateFbDlg(hWnd, &fb_settings[0],
    647 						    UPDATE_DLGBOX);
    648 				}
    649 				return (1);
    650 			}
    651 			break;
    652 		case IDOK:
    653 			UpdateFbDlg(hWnd, &fb_settings[0], UPDATE_DATA);
    654 
    655 			EndDialog(hWnd, IDOK);
    656 			return (1);
    657 
    658 		case IDCANCEL:
    659 			EndDialog(hWnd, IDCANCEL);
    660 			return (1);
    661 		}
    662 		break;
    663 	}
    664 	return (0);
    665 }
    666 
    667 
    668 BOOL SerialPort(BOOL on)
    669 {
    670 	static HANDLE hPort = INVALID_HANDLE_VALUE;
    671 	BOOL res = (hPort != INVALID_HANDLE_VALUE);
    672 
    673 	if (on != res) {
    674 		if (on) {
    675 			hPort = CreateFile(TEXT("COM1:"),
    676 					   GENERIC_READ | GENERIC_WRITE,
    677 					   0, NULL, OPEN_EXISTING,
    678 					   0,
    679 					   NULL);
    680 			debug_printf(TEXT("serial port ON\n"));
    681 			if ( hPort == INVALID_HANDLE_VALUE ) {
    682 				debug_printf(TEXT("open failed\n"));
    683 			} else {
    684 #if 0
    685 				DWORD Len;
    686 				BYTE x = 'X';
    687 				WriteFile (hPort, &x, 1, &Len, 0);
    688 				WriteFile (hPort, &x, 1, &Len, 0);
    689 				WriteFile (hPort, &x, 1, &Len, 0);
    690 				WriteFile (hPort, &x, 1, &Len, 0);
    691 #endif
    692 			}
    693 		} else {
    694 			debug_printf(TEXT("serial port OFF\n"));
    695 			CloseHandle(hPort);
    696 			hPort = INVALID_HANDLE_VALUE;
    697 		}
    698 	}
    699 
    700 	return (res);
    701 }
    702 
    703 
    704 BOOL CheckCancel(int progress)
    705 {
    706 	MSG msg;
    707 
    708 	if (0 <= progress) {
    709 		SendDlgItemMessage(hDlgLoad, IDC_PROGRESS,
    710 				   PBM_SETPOS, (WPARAM)progress, (LPARAM)NULL);
    711 	} else
    712 	if (pref.check_last_chance) {
    713 		if (msg_printf(MB_YESNO | MB_ICONHAND,
    714 			       TEXT("Last chance..."),
    715 			       TEXT("Push OK to boot.")) != IDYES) {
    716 			dlgStatus = IDCANCEL;
    717 		}
    718 	}
    719 
    720 	/*
    721 	 *  Put WM_TIMER in my message queue.
    722 	 *  (WM_TIMER has lowest priority.)
    723 	 */
    724 	SetTimer(hDlgLoad, 1, 1, NULL);
    725 
    726 	/*
    727 	 *  I tried PeekMessage() but it does not work.
    728 	 */
    729 	while (GetMessage(&msg, NULL, 0, 0)) {
    730 		if (msg.hwnd == hDlgLoad && msg.message == WM_TIMER) {
    731 			break;
    732 		}
    733 		TranslateMessage(&msg);
    734 		DispatchMessage(&msg);
    735 	}
    736 
    737 	return (dlgStatus != 0);
    738 }
    739 
    740 LRESULT CALLBACK WndProc(HWND hWnd, UINT message,
    741                           WPARAM wParam, LPARAM lParam )
    742 {
    743 	int i, idx;
    744 
    745 	switch (message) {
    746 	case WM_CREATE:
    747 		sndPlaySound(TEXT("OpenProg"), SND_NODEFAULT | SND_ASYNC);
    748 
    749 		hWndCB = CommandBar_Create(hInst, hWnd, 1);
    750 		CommandBar_AddAdornments(hWndCB, STD_HELP, (DWORD)NULL);
    751 
    752 		break;
    753 
    754 	case WM_PAINT:
    755 		{
    756 		HDC          hdc;
    757 		PAINTSTRUCT  ps;
    758 
    759 		hdc = BeginPaint(hWnd, &ps);
    760 		EndPaint(hWnd, &ps);
    761 		}
    762 		break;
    763 
    764 	case WM_HELP:
    765 		/*
    766 		MessageBox (NULL, TEXT("HELP NOT AVAILABLE"),
    767 			    szAppName, MB_OK);
    768 		*/
    769 		DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUT), hWnd, DlgProc2);
    770         break;
    771 
    772 	case WM_COMMAND:
    773 		switch (LOWORD(wParam)) {
    774 		case IDC_BOOT:
    775 			{
    776 		       	int argc;
    777 		       	TCHAR wkernel_name[PATHBUFLEN];
    778 			TCHAR woptions[PATHBUFLEN];
    779 			char options[PATHBUFLEN*2], kernel_name[PATHBUFLEN*2];
    780 			platid_t platid;
    781 
    782 			char *p, *argv[32];
    783 			struct bootinfo bi;
    784 
    785 			if (SendDlgItemMessage(hWndMain, IDC_PAUSE,
    786 					       BM_GETCHECK, 0, 0) ==
    787 								BST_CHECKED) {
    788 				pref.check_last_chance = TRUE;
    789 			} else {
    790 				pref.check_last_chance = FALSE;
    791 			}
    792 
    793 			if (SendDlgItemMessage(hWndMain, IDC_DEBUG,
    794 					       BM_GETCHECK, 0, 0) ==
    795 								BST_CHECKED) {
    796 				pref.load_debug_info = TRUE;
    797 			} else {
    798 				pref.load_debug_info = FALSE;
    799 			}
    800 
    801 			if (SendDlgItemMessage(hWndMain, IDC_COMM,
    802 					       BM_GETCHECK, 0, 0) ==
    803 								BST_CHECKED) {
    804 				pref.serial_port = TRUE;
    805 			} else {
    806 				pref.serial_port = FALSE;
    807 			}
    808 
    809 			if (GetDlgItemText(hWndMain, IDC_KERNEL, wkernel_name,
    810 					   sizeof(wkernel_name)) == 0) {
    811 				MessageBox (NULL, TEXT("Kernel name required"),
    812 					    szAppName, MB_OK);
    813 				break;
    814 			}
    815 			GetDlgItemText(hWndMain, IDC_OPTIONS,
    816 				       woptions, sizeof(woptions));
    817 			if (wcstombs(options, woptions, sizeof(options)) < 0 ||
    818 			    wcstombs(kernel_name, wkernel_name,
    819 				     sizeof(kernel_name)) < 0) {
    820 				MessageBox (NULL, TEXT("invalid character"),
    821 					    szAppName, MB_OK);
    822 				break;
    823 			}
    824 
    825 			argc = 0;
    826 			argv[argc++] = kernel_name;
    827 			p = options;
    828 			while (*p) {
    829 				while (*p == ' ' || *p == '\t') {
    830 					p++;
    831 				}
    832 				if (*p == '\0')
    833 					break;
    834 				if (ARRAYSIZEOF(argv) <= argc) {
    835 					MessageBox (NULL,
    836 						    TEXT("too many options"),
    837 						    szAppName, MB_OK);
    838 					argc++;
    839 					break;
    840 				} else {
    841 					argv[argc++] = p;
    842 				}
    843 				while (*p != ' ' && *p != '\t' && *p != '\0') {
    844 					p++;
    845 				}
    846 				if (*p == '\0') {
    847 					break;
    848 				} else {
    849 					*p++ = '\0';
    850 				}
    851 			}
    852 			if (ARRAYSIZEOF(argv) < argc) {
    853 				break;
    854 			}
    855 
    856 			EnableWindow(hWndMain, FALSE);
    857 			if (MessageBox (hWndMain,
    858 					TEXT("Data in memory will be lost.\nAre you sure?"),
    859 					szAppName,
    860 					MB_YESNO | MB_DEFBUTTON2 | MB_ICONHAND) == IDYES) {
    861 				dlgStatus = 0;
    862 				hDlgLoad =
    863 				  CreateDialog(hInst,
    864 					       MAKEINTRESOURCE(IDD_LOAD),
    865 					       hWndMain, DlgProc);
    866 				ShowWindow(hDlgLoad, SW_SHOWNORMAL);
    867 				BringWindowToTop(hDlgLoad);
    868 
    869 				/*
    870 				 *  save settings.
    871 				 */
    872 				pref.fb_type		= fb_settings[0].type;
    873 				pref.fb_width		= fb_settings[0].width;
    874 				pref.fb_height		= fb_settings[0].height;
    875 				pref.fb_linebytes	= fb_settings[0].linebytes;
    876 				pref.fb_addr		= fb_settings[0].addr;
    877 				pref.platid_cpu		= fb_settings[0].platid_cpu;
    878 				pref.platid_machine	= fb_settings[0].platid_machine;
    879 				wstrcpy(pref.kernel_name, wkernel_name);
    880 				wstrcpy(pref.options, woptions);
    881 
    882 				if (where_pref_load_from) {
    883 					pref_write(where_pref_load_from, &pref);
    884 				} else {
    885 					TCHAR *p, t;
    886 					TCHAR *last_separator = NULL;
    887 					TCHAR tmpbuf[PATHBUFLEN];
    888 					for (p = wkernel_name; *p != 0; p++) {
    889 						if (*p == TEXT('\\')) {
    890 							last_separator = p;
    891 						}
    892 					}
    893 					if (last_separator != NULL) {
    894 						p = last_separator + 1;
    895 					}
    896 					t = *p;
    897 					*p = 0;
    898 					wsprintf(tmpbuf,
    899 						 TEXT("%s%s"),
    900 						 wkernel_name, PREFNAME);
    901 					*p = t;
    902 					pref_write(tmpbuf, &pref);
    903 				}
    904 
    905 				SetBootInfo(&bi, &fb_settings[pref.setting_idx]);
    906 				debug_printf(TEXT("Args: "));
    907 				for (i = 0; i < argc; i++) {
    908 					debug_printf(TEXT("'%S' "), argv[i]);
    909 				}
    910 				debug_printf(TEXT("\n"));
    911 				debug_printf(TEXT("Bootinfo: fb_type=%d 0x%X %dx%d %d\n"),
    912 					     bi.fb_type, bi.fb_addr,
    913 					     bi.fb_width, bi.fb_height,
    914 					     bi.fb_line_bytes);
    915 
    916 				if (pref.serial_port) {
    917 					SerialPort(TRUE);
    918 				}
    919 				/*
    920 				 * Set system infomation
    921 				 */
    922 				platid.dw.dw0 = bi.platid_cpu;
    923 				platid.dw.dw1 = bi.platid_machine;
    924 				if (set_system_info(&platid)) {
    925 					/*
    926 					*  boot !
    927 					*/
    928 					pbsdboot(wkernel_name, argc, argv, &bi);
    929 				}
    930 				/*
    931 				 *  Not return.
    932 				 */
    933 
    934 				if (pref.serial_port) {
    935 					SerialPort(FALSE);
    936 				}
    937 
    938 				DestroyWindow(hDlgLoad);
    939 			}
    940 			EnableWindow(hWndMain, TRUE);
    941 			}
    942 			break;
    943 		case IDC_FBSETTING:
    944 			if (DialogBox(hInst, MAKEINTRESOURCE(IDD_FB),
    945 				      hWndMain, FbDlgProc) == IDOK) {
    946 				/* User defined */
    947 				pref.setting_idx = 0;
    948 				SendDlgItemMessage(hWndMain, IDC_FBSELECT,
    949 						   CB_DELETESTRING,
    950 						   (WPARAM)user_define_idx, 0);
    951 				SendDlgItemMessage(hWndMain, IDC_FBSELECT,
    952 						   CB_INSERTSTRING,
    953 						   (WPARAM)user_define_idx,
    954 						   (LPARAM)fb_settings[0].name);
    955 				SendDlgItemMessage(hWnd, IDC_FBSELECT,
    956 						   CB_SETCURSEL, 0, 0);
    957 			}
    958 			break;
    959 		case IDC_FBSELECT:
    960 			switch (HIWORD(wParam)) {
    961 			case CBN_SELCHANGE:
    962 				idx = SendDlgItemMessage(hWnd, IDC_FBSELECT,
    963 							 CB_GETCURSEL, 0, 0);
    964 				i = SendDlgItemMessage(hWnd, IDC_FBSELECT,
    965 						       CB_GETITEMDATA, idx, 0);
    966 				if (0 <= i && i < ARRAYSIZEOF(fb_settings)) {
    967 					debug_printf(TEXT("fb_setting=%d\n"), i);
    968 					pref.setting_idx = i;
    969 				}
    970 				break;
    971 			}
    972 			break;
    973 		}
    974 		break;
    975 
    976 	case WM_HIBERNATE:
    977 		MessageBox(NULL, TEXT("MEMORY IS LOW"), szAppName, MB_OK);
    978 		//Additional code to handle a low memory situation
    979 
    980 	case WM_CLOSE:
    981 	        sndPlaySound(TEXT("Close"), SND_NODEFAULT | SND_ASYNC);
    982 
    983 		DestroyWindow(hWnd);
    984 		break;
    985 
    986 	case WM_DESTROY:
    987 	        PostQuitMessage(0);
    988 		break;
    989 
    990 	default:
    991         	return (DefWindowProc(hWnd, message, wParam, lParam));
    992 	}
    993 
    994 	return (0);
    995 }
    996 
    997 
    998