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