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