Home | History | Annotate | Line # | Download | only in hpcboot
console.cpp revision 1.10.78.1
      1  1.10.78.1  yamt /* -*-C++-*-	$NetBSD: console.cpp,v 1.10.78.1 2008/05/16 02:22:24 yamt Exp $ */
      2        1.1   uch 
      3        1.1   uch /*-
      4        1.8   uch  * Copyright (c) 2001, 2002 The NetBSD Foundation, Inc.
      5        1.1   uch  * All rights reserved.
      6        1.1   uch  *
      7        1.1   uch  * This code is derived from software contributed to The NetBSD Foundation
      8        1.1   uch  * by UCHIYAMA Yasushi.
      9        1.1   uch  *
     10        1.1   uch  * Redistribution and use in source and binary forms, with or without
     11        1.1   uch  * modification, are permitted provided that the following conditions
     12        1.1   uch  * are met:
     13        1.1   uch  * 1. Redistributions of source code must retain the above copyright
     14        1.1   uch  *    notice, this list of conditions and the following disclaimer.
     15        1.1   uch  * 2. Redistributions in binary form must reproduce the above copyright
     16        1.1   uch  *    notice, this list of conditions and the following disclaimer in the
     17        1.1   uch  *    documentation and/or other materials provided with the distribution.
     18        1.1   uch  *
     19        1.1   uch  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20        1.1   uch  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21        1.1   uch  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22        1.1   uch  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23        1.1   uch  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24        1.1   uch  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25        1.1   uch  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26        1.1   uch  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27        1.1   uch  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28        1.1   uch  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29        1.1   uch  * POSSIBILITY OF SUCH DAMAGE.
     30        1.1   uch  */
     31        1.1   uch 
     32        1.1   uch #include <hpcmenu.h>
     33        1.1   uch #include <console.h>
     34        1.1   uch 
     35        1.1   uch Console *Console::_instance = 0;
     36        1.1   uch 
     37        1.4   uch //
     38        1.4   uch // Display console
     39        1.4   uch //
     40        1.1   uch Console *
     41        1.1   uch Console::Instance()
     42        1.1   uch {
     43        1.8   uch 
     44        1.8   uch 	if (_instance == 0) {
     45        1.1   uch 		_instance = new Console;
     46        1.8   uch 	}
     47        1.8   uch 
     48        1.8   uch 	return (_instance);
     49        1.6   uch }
     50        1.6   uch 
     51        1.6   uch Console::Console()
     52        1.6   uch {
     53        1.6   uch 	// set default builtin console. (bicons)
     54        1.6   uch 	setBootConsole(BI_CNUSE_BUILTIN);
     55        1.1   uch }
     56        1.1   uch 
     57        1.1   uch void
     58        1.1   uch Console::Destroy()
     59        1.1   uch {
     60        1.8   uch 
     61        1.1   uch 	if (_instance)
     62        1.1   uch 		delete _instance;
     63        1.1   uch 	_instance = 0;
     64        1.1   uch }
     65        1.1   uch 
     66        1.1   uch void
     67        1.1   uch Console::print(const TCHAR *fmt, ...)
     68        1.1   uch {
     69        1.1   uch 	va_list ap;
     70        1.8   uch 
     71        1.1   uch 	va_start(ap, fmt);
     72        1.4   uch 	wvsprintf(_bufw, fmt, ap);
     73        1.1   uch 	va_end(ap);
     74        1.4   uch 
     75        1.4   uch 	// print to `Console Tab Window'
     76        1.4   uch 	HPC_MENU.print(_bufw);
     77        1.4   uch }
     78        1.4   uch 
     79        1.4   uch //
     80        1.4   uch // Serial console.
     81        1.4   uch //
     82        1.7   uch SerialConsole::SerialConsole()
     83        1.7   uch {
     84        1.8   uch 
     85        1.7   uch 	_handle = INVALID_HANDLE_VALUE;
     86        1.7   uch 	// set default serial console.
     87        1.7   uch 	setBootConsole(BI_CNUSE_SERIAL);
     88        1.7   uch }
     89        1.7   uch 
     90        1.4   uch BOOL
     91        1.4   uch SerialConsole::init()
     92        1.4   uch {
     93        1.4   uch 	// always open COM1 to supply clock and power for the
     94        1.9   uch 	// sake of kernel serial driver
     95        1.8   uch 	if (_handle == INVALID_HANDLE_VALUE)
     96        1.9   uch 		_handle = OpenCOM1();
     97        1.8   uch 
     98        1.8   uch 	if (_handle == INVALID_HANDLE_VALUE) {
     99        1.8   uch 		Console::print(TEXT("couldn't open COM1\n"));
    100        1.8   uch 		return (FALSE);
    101        1.8   uch 	}
    102        1.8   uch 
    103        1.8   uch 	// Print serial console status on LCD.
    104        1.8   uch 	DCB dcb;
    105        1.8   uch 	GetCommState(_handle, &dcb);
    106        1.8   uch 	Console::print(
    107        1.8   uch 		TEXT("BaudRate %d, ByteSize %#x, Parity %#x, StopBits %#x\n"),
    108        1.8   uch 		dcb.BaudRate, dcb.ByteSize, dcb.Parity, dcb.StopBits);
    109        1.8   uch 
    110        1.8   uch 	return (TRUE);
    111        1.1   uch }
    112        1.1   uch 
    113        1.1   uch BOOL
    114        1.4   uch SerialConsole::setupMultibyteBuffer()
    115        1.1   uch {
    116        1.1   uch 	size_t len = WideCharToMultiByte(CP_ACP, 0, _bufw, wcslen(_bufw),
    117        1.5   uch 	    0, 0, 0, 0);
    118        1.8   uch 
    119        1.1   uch 	if (len + 1 > CONSOLE_BUFSIZE)
    120        1.1   uch 		return FALSE;
    121        1.1   uch 	if (!WideCharToMultiByte(CP_ACP, 0, _bufw, len, _bufm, len, 0, 0))
    122        1.1   uch 		return FALSE;
    123        1.1   uch 	_bufm[len] = '\0';
    124        1.1   uch 
    125        1.1   uch 	return TRUE;
    126        1.1   uch }
    127        1.1   uch 
    128        1.4   uch void
    129        1.4   uch SerialConsole::print(const TCHAR *fmt, ...)
    130        1.4   uch {
    131        1.8   uch 
    132        1.4   uch 	SETUP_WIDECHAR_BUFFER();
    133        1.4   uch 
    134        1.4   uch 	if (!setupMultibyteBuffer())
    135        1.4   uch 		return;
    136        1.4   uch 
    137        1.4   uch 	genericPrint(_bufm);
    138        1.4   uch }
    139        1.4   uch 
    140        1.8   uch HANDLE
    141        1.8   uch SerialConsole::OpenCOM1()
    142        1.1   uch {
    143        1.8   uch 	static HANDLE COM1handle = INVALID_HANDLE_VALUE;
    144        1.8   uch 	const char msg[] = "\r\n--------HPCBOOT--------\r\n";
    145        1.8   uch 	unsigned long wrote;
    146        1.4   uch 	int speed = HPC_PREFERENCE.serial_speed;
    147        1.8   uch 	HANDLE h;
    148        1.3   uch 
    149        1.8   uch 	if (COM1handle != INVALID_HANDLE_VALUE)
    150        1.8   uch 		return (COM1handle);
    151        1.8   uch 
    152        1.9   uch 	h = CreateFile(TEXT("COM1:"),
    153        1.8   uch 	    GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0,
    154        1.8   uch 	    NULL);
    155        1.8   uch 	if (h == INVALID_HANDLE_VALUE)
    156        1.8   uch 		return (h);
    157        1.8   uch 
    158        1.8   uch 	DCB dcb;
    159        1.8   uch 	if (!GetCommState(h, &dcb))
    160        1.8   uch 		goto bad;
    161        1.9   uch 
    162        1.8   uch 	dcb.BaudRate = speed;
    163        1.8   uch 	if (!SetCommState(h, &dcb))
    164        1.8   uch 		goto bad;
    165        1.8   uch 
    166        1.8   uch 	// Print banner on serial console.
    167        1.8   uch 	WriteFile(h, msg, sizeof msg, &wrote, 0);
    168        1.8   uch 
    169        1.8   uch 	COM1handle = h;
    170        1.1   uch 
    171        1.8   uch 	return (h);
    172        1.1   uch  bad:
    173        1.8   uch 	CloseHandle(h);
    174        1.8   uch 	return (INVALID_HANDLE_VALUE);
    175        1.2   uch }
    176        1.2   uch 
    177        1.2   uch void
    178        1.2   uch SerialConsole::genericPrint(const char *buf)
    179        1.2   uch {
    180        1.2   uch 	unsigned long wrote;
    181        1.2   uch 	int i;
    182        1.9   uch 
    183        1.2   uch 	for (i = 0; *buf != '\0'; buf++) {
    184        1.2   uch 		char c = *buf;
    185        1.2   uch 		if (c == '\n')
    186        1.2   uch 			WriteFile(_handle, "\r", 1, &wrote, 0);
    187        1.2   uch 		WriteFile(_handle, &c, 1, &wrote, 0);
    188        1.2   uch 	}
    189        1.1   uch }
    190