1 | /* $Id: logcom.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Logging to Serial Port.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Defined Constants And Macros *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #ifndef IPRT_UART_BASE
|
---|
42 | /** The port address of the COM port to log to.
|
---|
43 | *
|
---|
44 | * To override the default (COM1) append IPRT_UART_BASE=0xWXYZ to DEFS in your
|
---|
45 | * LocalConfig.kmk. Alternatively you can edit this file, but the don't forget
|
---|
46 | * to also update the default found in VBox/asmdefs.h.
|
---|
47 | *
|
---|
48 | * Standard port assignments are: COM1=0x3f8, COM2=0x2f8, COM3=0x3e8, COM4=0x2e8.
|
---|
49 | */
|
---|
50 | # define IPRT_UART_BASE 0x3f8
|
---|
51 | #endif
|
---|
52 |
|
---|
53 |
|
---|
54 | /*********************************************************************************************************************************
|
---|
55 | * Header Files *
|
---|
56 | *********************************************************************************************************************************/
|
---|
57 | #include <iprt/log.h>
|
---|
58 | #include "internal/iprt.h"
|
---|
59 |
|
---|
60 | #include <iprt/asm.h>
|
---|
61 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86) /** @todo consider fixing the config instead. */
|
---|
62 | # include <iprt/asm-amd64-x86.h>
|
---|
63 | #endif
|
---|
64 | #include <iprt/stdarg.h>
|
---|
65 | #include <iprt/string.h>
|
---|
66 |
|
---|
67 |
|
---|
68 | /*********************************************************************************************************************************
|
---|
69 | * Internal Functions *
|
---|
70 | *********************************************************************************************************************************/
|
---|
71 | static DECLCALLBACK(size_t) rtLogComOutput(void *pv, const char *pachChars, size_t cbChars);
|
---|
72 |
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Prints a formatted string to the serial port used for logging.
|
---|
76 | *
|
---|
77 | * @returns Number of bytes written.
|
---|
78 | * @param pszFormat Format string.
|
---|
79 | * @param ... Optional arguments specified in the format string.
|
---|
80 | */
|
---|
81 | RTDECL(size_t) RTLogComPrintf(const char *pszFormat, ...)
|
---|
82 | {
|
---|
83 | va_list args;
|
---|
84 | size_t cb;
|
---|
85 | va_start(args, pszFormat);
|
---|
86 | cb = RTLogComPrintfV(pszFormat, args);
|
---|
87 | va_end(args);
|
---|
88 |
|
---|
89 | return cb;
|
---|
90 | }
|
---|
91 | RT_EXPORT_SYMBOL(RTLogComPrintf);
|
---|
92 |
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Prints a formatted string to the serial port used for logging.
|
---|
96 | *
|
---|
97 | * @returns Number of bytes written.
|
---|
98 | * @param pszFormat Format string.
|
---|
99 | * @param args Optional arguments specified in the format string.
|
---|
100 | */
|
---|
101 | RTDECL(size_t) RTLogComPrintfV(const char *pszFormat, va_list args)
|
---|
102 | {
|
---|
103 | return RTLogFormatV(rtLogComOutput, NULL, pszFormat, args);
|
---|
104 | }
|
---|
105 | RT_EXPORT_SYMBOL(RTLogComPrintfV);
|
---|
106 |
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Callback for RTLogFormatV which writes to the com port.
|
---|
110 | * See PFNLOGOUTPUT() for details.
|
---|
111 | */
|
---|
112 | static DECLCALLBACK(size_t) rtLogComOutput(void *pv, const char *pachChars, size_t cbChars)
|
---|
113 | {
|
---|
114 | NOREF(pv);
|
---|
115 | if (cbChars)
|
---|
116 | RTLogWriteCom(pachChars, cbChars);
|
---|
117 | return cbChars;
|
---|
118 | }
|
---|
119 |
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Write log buffer to COM port.
|
---|
123 | *
|
---|
124 | * @param pach Pointer to the buffer to write.
|
---|
125 | * @param cb Number of bytes to write.
|
---|
126 | */
|
---|
127 | RTDECL(void) RTLogWriteCom(const char *pach, size_t cb)
|
---|
128 | {
|
---|
129 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
130 | const uint8_t *pu8;
|
---|
131 | for (pu8 = (const uint8_t *)pach; cb-- > 0; pu8++)
|
---|
132 | {
|
---|
133 | unsigned cMaxWait;
|
---|
134 | uint8_t u8;
|
---|
135 |
|
---|
136 | /* expand \n -> \r\n */
|
---|
137 | if (*pu8 == '\n')
|
---|
138 | RTLogWriteCom("\r", 1);
|
---|
139 |
|
---|
140 | /* Check if port is ready. */
|
---|
141 | cMaxWait = ~0U;
|
---|
142 | do
|
---|
143 | {
|
---|
144 | u8 = ASMInU8(IPRT_UART_BASE + 5);
|
---|
145 | cMaxWait--;
|
---|
146 | } while (!(u8 & 0x20) && u8 != 0xff && cMaxWait);
|
---|
147 |
|
---|
148 | /* write */
|
---|
149 | ASMOutU8(IPRT_UART_BASE, *pu8);
|
---|
150 | }
|
---|
151 | #else
|
---|
152 | /* PORTME? */
|
---|
153 | #endif
|
---|
154 | }
|
---|
155 | RT_EXPORT_SYMBOL(RTLogWriteCom);
|
---|
156 |
|
---|