1 /* -*-C++-*- $NetBSD: console.cpp,v 1.7 2001/06/19 16:48:49 uch Exp $ */ 2 3 /*- 4 * Copyright (c) 2001 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 if (_instance == 0) 51 _instance = new Console; 52 return _instance; 53 } 54 55 Console::Console() 56 { 57 // set default builtin console. (bicons) 58 setBootConsole(BI_CNUSE_BUILTIN); 59 } 60 61 void 62 Console::Destroy() 63 { 64 if (_instance) 65 delete _instance; 66 _instance = 0; 67 } 68 69 void 70 Console::print(const TCHAR *fmt, ...) 71 { 72 va_list ap; 73 va_start(ap, fmt); 74 wvsprintf(_bufw, fmt, ap); 75 va_end(ap); 76 77 // print to `Console Tab Window' 78 HPC_MENU.print(_bufw); 79 } 80 81 // 82 // Serial console. 83 // 84 SerialConsole::SerialConsole() 85 { 86 _handle = INVALID_HANDLE_VALUE; 87 // set default serial console. 88 setBootConsole(BI_CNUSE_SERIAL); 89 } 90 91 BOOL 92 SerialConsole::init() 93 { 94 // always open COM1 to supply clock and power for the 95 // sake of kernel serial driver 96 return openCOM1(); 97 } 98 99 BOOL 100 SerialConsole::setupMultibyteBuffer() 101 { 102 size_t len = WideCharToMultiByte(CP_ACP, 0, _bufw, wcslen(_bufw), 103 0, 0, 0, 0); 104 if (len + 1 > CONSOLE_BUFSIZE) 105 return FALSE; 106 if (!WideCharToMultiByte(CP_ACP, 0, _bufw, len, _bufm, len, 0, 0)) 107 return FALSE; 108 _bufm[len] = '\0'; 109 110 return TRUE; 111 } 112 113 void 114 SerialConsole::print(const TCHAR *fmt, ...) 115 { 116 SETUP_WIDECHAR_BUFFER(); 117 118 if (!setupMultibyteBuffer()) 119 return; 120 121 genericPrint(_bufm); 122 } 123 124 BOOL 125 SerialConsole::openCOM1() 126 { 127 int speed = HPC_PREFERENCE.serial_speed; 128 129 if (_handle == INVALID_HANDLE_VALUE) { 130 _handle = CreateFile(TEXT("COM1:"), 131 GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 132 NULL); 133 if (_handle == INVALID_HANDLE_VALUE) { 134 Console::print(TEXT("couldn't open COM1\n")); 135 return FALSE; 136 } 137 138 DCB dcb; 139 if (!GetCommState(_handle, &dcb)) { 140 Console::print(TEXT("couldn't get COM port status.\n")); 141 goto bad; 142 } 143 144 dcb.BaudRate = speed; 145 if (!SetCommState(_handle, &dcb)) { 146 Console::print(TEXT("couldn't set baud rate to %s.\n"), 147 speed); 148 goto bad; 149 } 150 151 Console::print(TEXT("BaudRate %d, ByteSize %#x, Parity %#x, StopBits %#x\n"), 152 dcb.BaudRate, dcb.ByteSize, dcb.Parity, dcb.StopBits); 153 const char msg[] = "--------HPCBOOT--------\r\n"; 154 unsigned long wrote; 155 WriteFile(_handle, msg, sizeof msg, &wrote, 0); 156 } 157 158 return TRUE; 159 bad: 160 CloseHandle(_handle); 161 _handle = INVALID_HANDLE_VALUE; 162 return FALSE; 163 } 164 165 void 166 SerialConsole::genericPrint(const char *buf) 167 { 168 unsigned long wrote; 169 int i; 170 171 for (i = 0; *buf != '\0'; buf++) { 172 char c = *buf; 173 if (c == '\n') 174 WriteFile(_handle, "\r", 1, &wrote, 0); 175 WriteFile(_handle, &c, 1, &wrote, 0); 176 } 177 } 178