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