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