1 | /* $Id: logcom.cpp 2981 2007-06-01 16:01:28Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * innotek Portable Runtime - Logging to Serial Port.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * If you received this file as part of a commercial VirtualBox
|
---|
18 | * distribution, then only the terms of your commercial VirtualBox
|
---|
19 | * license agreement apply instead of the previous paragraph.
|
---|
20 | */
|
---|
21 |
|
---|
22 | /*******************************************************************************
|
---|
23 | * Defined Constants And Macros *
|
---|
24 | *******************************************************************************/
|
---|
25 | /** Comport to log to (COM2).
|
---|
26 | * This is also defined in VBox/nasm.mac. */
|
---|
27 | //#define UART_BASE 0x2f8 /* COM2 */
|
---|
28 | #define UART_BASE 0x3f8 /* COM1 */
|
---|
29 |
|
---|
30 |
|
---|
31 | /*******************************************************************************
|
---|
32 | * Header Files *
|
---|
33 | *******************************************************************************/
|
---|
34 | #include <iprt/log.h>
|
---|
35 | #include <iprt/asm.h>
|
---|
36 | #include <iprt/stdarg.h>
|
---|
37 | #include <iprt/string.h>
|
---|
38 |
|
---|
39 |
|
---|
40 | /*******************************************************************************
|
---|
41 | * Internal Functions *
|
---|
42 | *******************************************************************************/
|
---|
43 | static DECLCALLBACK(size_t) rtLogComOutput(void *pv, const char *pachChars, size_t cbChars);
|
---|
44 |
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Prints a formatted string to the serial port used for logging.
|
---|
48 | *
|
---|
49 | * @returns Number of bytes written.
|
---|
50 | * @param pszFormat Format string.
|
---|
51 | * @param ... Optional arguments specified in the format string.
|
---|
52 | */
|
---|
53 | RTDECL(size_t) RTLogComPrintf(const char *pszFormat, ...)
|
---|
54 | {
|
---|
55 | va_list args;
|
---|
56 | size_t cb;
|
---|
57 | va_start(args, pszFormat);
|
---|
58 | cb = RTLogComPrintfV(pszFormat, args);
|
---|
59 | va_end(args);
|
---|
60 |
|
---|
61 | return cb;
|
---|
62 | }
|
---|
63 |
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Prints a formatted string to the serial port used for logging.
|
---|
67 | *
|
---|
68 | * @returns Number of bytes written.
|
---|
69 | * @param pszFormat Format string.
|
---|
70 | * @param args Optional arguments specified in the format string.
|
---|
71 | */
|
---|
72 | RTDECL(size_t) RTLogComPrintfV(const char *pszFormat, va_list args)
|
---|
73 | {
|
---|
74 | return RTLogFormatV(rtLogComOutput, NULL, pszFormat, args);
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Callback for RTLogFormatV which writes to the com port.
|
---|
80 | * See PFNLOGOUTPUT() for details.
|
---|
81 | */
|
---|
82 | static DECLCALLBACK(size_t) rtLogComOutput(void *pv, const char *pachChars, size_t cbChars)
|
---|
83 | {
|
---|
84 | if (cbChars)
|
---|
85 | RTLogWriteCom(pachChars, cbChars);
|
---|
86 | return cbChars;
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Write log buffer to COM port.
|
---|
92 | *
|
---|
93 | * @param pach Pointer to the buffer to write.
|
---|
94 | * @param cb Number of bytes to write.
|
---|
95 | */
|
---|
96 | RTDECL(void) RTLogWriteCom(const char *pach, size_t cb)
|
---|
97 | {
|
---|
98 | for (const uint8_t *pu8 = (const uint8_t *)pach; cb-- > 0; pu8++)
|
---|
99 | {
|
---|
100 | /* expand \n -> \r\n */
|
---|
101 | if (*pu8 == '\n')
|
---|
102 | RTLogWriteCom("\r", 1);
|
---|
103 |
|
---|
104 | /* Check if port is ready. */
|
---|
105 | register unsigned cMaxWait = ~0;
|
---|
106 | register uint8_t u8;
|
---|
107 | do
|
---|
108 | {
|
---|
109 | u8 = ASMInU8(UART_BASE + 5);
|
---|
110 | cMaxWait--;
|
---|
111 | } while (!(u8 & 0x20) && u8 != 0xff && cMaxWait);
|
---|
112 |
|
---|
113 | /* write */
|
---|
114 | ASMOutU8(UART_BASE, *pu8);
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|