No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / hpc / stand / hpcboot / console.cpp
blobdce148debec6b1e6dafb96e195f1c20525e5b241
1 /* -*-C++-*- $NetBSD: console.cpp,v 1.10 2005/12/11 12:17:28 christos Exp $ */
3 /*-
4 * Copyright (c) 2001, 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by UCHIYAMA Yasushi.
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.
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.
32 #include <hpcmenu.h>
33 #include <console.h>
35 Console *Console::_instance = 0;
38 // Display console
40 Console *
41 Console::Instance()
44 if (_instance == 0) {
45 _instance = new Console;
48 return (_instance);
51 Console::Console()
53 // set default builtin console. (bicons)
54 setBootConsole(BI_CNUSE_BUILTIN);
57 void
58 Console::Destroy()
61 if (_instance)
62 delete _instance;
63 _instance = 0;
66 void
67 Console::print(const TCHAR *fmt, ...)
69 va_list ap;
71 va_start(ap, fmt);
72 wvsprintf(_bufw, fmt, ap);
73 va_end(ap);
75 // print to `Console Tab Window'
76 HPC_MENU.print(_bufw);
80 // Serial console.
82 SerialConsole::SerialConsole()
85 _handle = INVALID_HANDLE_VALUE;
86 // set default serial console.
87 setBootConsole(BI_CNUSE_SERIAL);
90 BOOL
91 SerialConsole::init()
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();
98 if (_handle == INVALID_HANDLE_VALUE) {
99 Console::print(TEXT("couldn't open COM1\n"));
100 return (FALSE);
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);
110 return (TRUE);
113 BOOL
114 SerialConsole::setupMultibyteBuffer()
116 size_t len = WideCharToMultiByte(CP_ACP, 0, _bufw, wcslen(_bufw),
117 0, 0, 0, 0);
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';
125 return TRUE;
128 void
129 SerialConsole::print(const TCHAR *fmt, ...)
132 SETUP_WIDECHAR_BUFFER();
134 if (!setupMultibyteBuffer())
135 return;
137 genericPrint(_bufm);
140 HANDLE
141 SerialConsole::OpenCOM1()
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;
149 if (COM1handle != INVALID_HANDLE_VALUE)
150 return (COM1handle);
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);
158 DCB dcb;
159 if (!GetCommState(h, &dcb))
160 goto bad;
162 dcb.BaudRate = speed;
163 if (!SetCommState(h, &dcb))
164 goto bad;
166 // Print banner on serial console.
167 WriteFile(h, msg, sizeof msg, &wrote, 0);
169 COM1handle = h;
171 return (h);
172 bad:
173 CloseHandle(h);
174 return (INVALID_HANDLE_VALUE);
177 void
178 SerialConsole::genericPrint(const char *buf)
180 unsigned long wrote;
181 int i;
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);