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