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