xref: /netbsd/sys/arch/hpc/stand/hpcboot/console.cpp (revision ce099b40)
1 /* -*-C++-*-	$NetBSD: console.cpp,v 1.11 2008/04/28 20:23:20 martin 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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <hpcmenu.h>
33 #include <console.h>
34 
35 Console *Console::_instance = 0;
36 
37 //
38 // Display console
39 //
40 Console *
Instance()41 Console::Instance()
42 {
43 
44 	if (_instance == 0) {
45 		_instance = new Console;
46 	}
47 
48 	return (_instance);
49 }
50 
Console()51 Console::Console()
52 {
53 	// set default builtin console. (bicons)
54 	setBootConsole(BI_CNUSE_BUILTIN);
55 }
56 
57 void
Destroy()58 Console::Destroy()
59 {
60 
61 	if (_instance)
62 		delete _instance;
63 	_instance = 0;
64 }
65 
66 void
print(const TCHAR * fmt,...)67 Console::print(const TCHAR *fmt, ...)
68 {
69 	va_list ap;
70 
71 	va_start(ap, fmt);
72 	wvsprintf(_bufw, fmt, ap);
73 	va_end(ap);
74 
75 	// print to `Console Tab Window'
76 	HPC_MENU.print(_bufw);
77 }
78 
79 //
80 // Serial console.
81 //
SerialConsole()82 SerialConsole::SerialConsole()
83 {
84 
85 	_handle = INVALID_HANDLE_VALUE;
86 	// set default serial console.
87 	setBootConsole(BI_CNUSE_SERIAL);
88 }
89 
90 BOOL
init()91 SerialConsole::init()
92 {
93 	// always open COM1 to supply clock and power for the
94 	// sake of kernel serial driver
95 	if (_handle == INVALID_HANDLE_VALUE)
96 		_handle = OpenCOM1();
97 
98 	if (_handle == INVALID_HANDLE_VALUE) {
99 		Console::print(TEXT("couldn't open COM1\n"));
100 		return (FALSE);
101 	}
102 
103 	// Print serial console status on LCD.
104 	DCB dcb;
105 	GetCommState(_handle, &dcb);
106 	Console::print(
107 		TEXT("BaudRate %d, ByteSize %#x, Parity %#x, StopBits %#x\n"),
108 		dcb.BaudRate, dcb.ByteSize, dcb.Parity, dcb.StopBits);
109 
110 	return (TRUE);
111 }
112 
113 BOOL
setupMultibyteBuffer()114 SerialConsole::setupMultibyteBuffer()
115 {
116 	size_t len = WideCharToMultiByte(CP_ACP, 0, _bufw, wcslen(_bufw),
117 	    0, 0, 0, 0);
118 
119 	if (len + 1 > CONSOLE_BUFSIZE)
120 		return FALSE;
121 	if (!WideCharToMultiByte(CP_ACP, 0, _bufw, len, _bufm, len, 0, 0))
122 		return FALSE;
123 	_bufm[len] = '\0';
124 
125 	return TRUE;
126 }
127 
128 void
print(const TCHAR * fmt,...)129 SerialConsole::print(const TCHAR *fmt, ...)
130 {
131 
132 	SETUP_WIDECHAR_BUFFER();
133 
134 	if (!setupMultibyteBuffer())
135 		return;
136 
137 	genericPrint(_bufm);
138 }
139 
140 HANDLE
OpenCOM1()141 SerialConsole::OpenCOM1()
142 {
143 	static HANDLE COM1handle = INVALID_HANDLE_VALUE;
144 	const char msg[] = "\r\n--------HPCBOOT--------\r\n";
145 	unsigned long wrote;
146 	int speed = HPC_PREFERENCE.serial_speed;
147 	HANDLE h;
148 
149 	if (COM1handle != INVALID_HANDLE_VALUE)
150 		return (COM1handle);
151 
152 	h = CreateFile(TEXT("COM1:"),
153 	    GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0,
154 	    NULL);
155 	if (h == INVALID_HANDLE_VALUE)
156 		return (h);
157 
158 	DCB dcb;
159 	if (!GetCommState(h, &dcb))
160 		goto bad;
161 
162 	dcb.BaudRate = speed;
163 	if (!SetCommState(h, &dcb))
164 		goto bad;
165 
166 	// Print banner on serial console.
167 	WriteFile(h, msg, sizeof msg, &wrote, 0);
168 
169 	COM1handle = h;
170 
171 	return (h);
172  bad:
173 	CloseHandle(h);
174 	return (INVALID_HANDLE_VALUE);
175 }
176 
177 void
genericPrint(const char * buf)178 SerialConsole::genericPrint(const char *buf)
179 {
180 	unsigned long wrote;
181 	int i;
182 
183 	for (i = 0; *buf != '\0'; buf++) {
184 		char c = *buf;
185 		if (c == '\n')
186 			WriteFile(_handle, "\r", 1, &wrote, 0);
187 		WriteFile(_handle, &c, 1, &wrote, 0);
188 	}
189 }
190