]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/Library/PlatformDebugLibIoPort/DebugLib.c
36cde54976d8a78816044cc32c9c732ef180bc18
[mirror_edk2.git] / OvmfPkg / Library / PlatformDebugLibIoPort / DebugLib.c
1 /** @file
2 Base Debug library instance for QEMU debug port.
3 It uses PrintLib to send debug messages to a fixed I/O port.
4
5 Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>
6 Copyright (c) 2012, Red Hat, Inc.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php.
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14
15 **/
16
17 #include <Base.h>
18 #include <Library/DebugLib.h>
19 #include <Library/BaseLib.h>
20 #include <Library/IoLib.h>
21 #include <Library/PrintLib.h>
22 #include <Library/PcdLib.h>
23 #include <Library/BaseMemoryLib.h>
24 #include <Library/DebugPrintErrorLevelLib.h>
25 #include "DebugLibDetect.h"
26
27 //
28 // Define the maximum debug and assert message length that this library supports
29 //
30 #define MAX_DEBUG_MESSAGE_LENGTH 0x100
31
32 /**
33 Prints a debug message to the debug output device if the specified error level is enabled.
34
35 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
36 GetDebugPrintErrorLevel (), then print the message specified by Format and the
37 associated variable argument list to the debug output device.
38
39 If Format is NULL, then ASSERT().
40
41 @param ErrorLevel The error level of the debug message.
42 @param Format Format string for the debug message to print.
43 @param ... Variable argument list whose contents are accessed
44 based on the format string specified by Format.
45
46 **/
47 VOID
48 EFIAPI
49 DebugPrint (
50 IN UINTN ErrorLevel,
51 IN CONST CHAR8 *Format,
52 ...
53 )
54 {
55 CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];
56 VA_LIST Marker;
57 UINTN Length;
58
59 //
60 // If Format is NULL, then ASSERT().
61 //
62 ASSERT (Format != NULL);
63
64 //
65 // Check if the global mask disables this message or the device is inactive
66 //
67 if ((ErrorLevel & GetDebugPrintErrorLevel ()) == 0 ||
68 !PlatformDebugLibIoPortFound ()) {
69 return;
70 }
71
72 //
73 // Convert the DEBUG() message to an ASCII String
74 //
75 VA_START (Marker, Format);
76 Length = AsciiVSPrint (Buffer, sizeof (Buffer), Format, Marker);
77 VA_END (Marker);
78
79 //
80 // Send the print string to the debug I/O port
81 //
82 IoWriteFifo8 (PcdGet16 (PcdDebugIoPort), Length, Buffer);
83 }
84
85
86 /**
87 Prints an assert message containing a filename, line number, and description.
88 This may be followed by a breakpoint or a dead loop.
89
90 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"
91 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of
92 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if
93 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then
94 CpuDeadLoop() is called. If neither of these bits are set, then this function
95 returns immediately after the message is printed to the debug output device.
96 DebugAssert() must actively prevent recursion. If DebugAssert() is called while
97 processing another DebugAssert(), then DebugAssert() must return immediately.
98
99 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.
100 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.
101
102 @param FileName The pointer to the name of the source file that generated the assert condition.
103 @param LineNumber The line number in the source file that generated the assert condition
104 @param Description The pointer to the description of the assert condition.
105
106 **/
107 VOID
108 EFIAPI
109 DebugAssert (
110 IN CONST CHAR8 *FileName,
111 IN UINTN LineNumber,
112 IN CONST CHAR8 *Description
113 )
114 {
115 CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];
116 UINTN Length;
117
118 //
119 // Generate the ASSERT() message in Ascii format
120 //
121 Length = AsciiSPrint (Buffer, sizeof Buffer, "ASSERT %a(%Lu): %a\n",
122 FileName, (UINT64)LineNumber, Description);
123
124 //
125 // Send the print string to the debug I/O port, if present
126 //
127 if (PlatformDebugLibIoPortFound ()) {
128 IoWriteFifo8 (PcdGet16 (PcdDebugIoPort), Length, Buffer);
129 }
130
131 //
132 // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings
133 //
134 if ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED) != 0) {
135 CpuBreakpoint ();
136 } else if ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED) != 0) {
137 CpuDeadLoop ();
138 }
139 }
140
141
142 /**
143 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.
144
145 This function fills Length bytes of Buffer with the value specified by
146 PcdDebugClearMemoryValue, and returns Buffer.
147
148 If Buffer is NULL, then ASSERT().
149 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
150
151 @param Buffer The pointer to the target buffer to be filled with PcdDebugClearMemoryValue.
152 @param Length The number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.
153
154 @return Buffer The pointer to the target buffer filled with PcdDebugClearMemoryValue.
155
156 **/
157 VOID *
158 EFIAPI
159 DebugClearMemory (
160 OUT VOID *Buffer,
161 IN UINTN Length
162 )
163 {
164 //
165 // If Buffer is NULL, then ASSERT().
166 //
167 ASSERT (Buffer != NULL);
168
169 //
170 // SetMem() checks for the the ASSERT() condition on Length and returns Buffer
171 //
172 return SetMem (Buffer, Length, PcdGet8(PcdDebugClearMemoryValue));
173 }
174
175
176 /**
177 Returns TRUE if ASSERT() macros are enabled.
178
179 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of
180 PcdDebugProperyMask is set. Otherwise FALSE is returned.
181
182 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.
183 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.
184
185 **/
186 BOOLEAN
187 EFIAPI
188 DebugAssertEnabled (
189 VOID
190 )
191 {
192 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) != 0);
193 }
194
195
196 /**
197 Returns TRUE if DEBUG() macros are enabled.
198
199 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of
200 PcdDebugProperyMask is set. Otherwise FALSE is returned.
201
202 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.
203 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.
204
205 **/
206 BOOLEAN
207 EFIAPI
208 DebugPrintEnabled (
209 VOID
210 )
211 {
212 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED) != 0);
213 }
214
215
216 /**
217 Returns TRUE if DEBUG_CODE() macros are enabled.
218
219 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of
220 PcdDebugProperyMask is set. Otherwise FALSE is returned.
221
222 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.
223 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.
224
225 **/
226 BOOLEAN
227 EFIAPI
228 DebugCodeEnabled (
229 VOID
230 )
231 {
232 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED) != 0);
233 }
234
235
236 /**
237 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.
238
239 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of
240 PcdDebugProperyMask is set. Otherwise FALSE is returned.
241
242 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.
243 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.
244
245 **/
246 BOOLEAN
247 EFIAPI
248 DebugClearMemoryEnabled (
249 VOID
250 )
251 {
252 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED) != 0);
253 }
254
255 /**
256 Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.
257
258 This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.
259
260 @retval TRUE Current ErrorLevel is supported.
261 @retval FALSE Current ErrorLevel is not supported.
262
263 **/
264 BOOLEAN
265 EFIAPI
266 DebugPrintLevelEnabled (
267 IN CONST UINTN ErrorLevel
268 )
269 {
270 return (BOOLEAN) ((ErrorLevel & PcdGet32(PcdFixedDebugPrintErrorLevel)) != 0);
271 }
272
273 /**
274 Return the result of detecting the debug I/O port device.
275
276 @retval TRUE if the debug I/O port device was detected.
277 @retval FALSE otherwise
278
279 **/
280 BOOLEAN
281 EFIAPI
282 PlatformDebugLibIoPortDetect (
283 VOID
284 )
285 {
286 return IoRead8 (PcdGet16 (PcdDebugIoPort)) == BOCHS_DEBUG_PORT_MAGIC;
287 }