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