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