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