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