]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFsp2Pkg/Library/BaseFspDebugLibSerialPort/DebugLib.c
IntelFsp2Pkg: Apply uncrustify changes
[mirror_edk2.git] / IntelFsp2Pkg / Library / BaseFspDebugLibSerialPort / DebugLib.c
CommitLineData
cf1d4549
JY
1/** @file\r
2\r
446be24d 3 Copyright (c) 2014 - 2019, Intel Corporation. All rights reserved.<BR>\r
9672cd30 4 SPDX-License-Identifier: BSD-2-Clause-Patent\r
cf1d4549
JY
5\r
6**/\r
7\r
8#include <Base.h>\r
9#include <Library/DebugLib.h>\r
10#include <Library/BaseLib.h>\r
11#include <Library/PrintLib.h>\r
12#include <Library/PcdLib.h>\r
13#include <Library/BaseMemoryLib.h>\r
14#include <Library/SerialPortLib.h>\r
15#include <Library/DebugDeviceLib.h>\r
16#include <Library/DebugPrintErrorLevelLib.h>\r
17\r
18//\r
19// Define the maximum debug and assert message length that this library supports\r
20//\r
21#define MAX_DEBUG_MESSAGE_LENGTH 0x100\r
22\r
23CONST CHAR8 *mHexTable = "0123456789ABCDEF";\r
24\r
446be24d
BB
25//\r
26// VA_LIST can not initialize to NULL for all compiler, so we use this to\r
27// indicate a null VA_LIST\r
28//\r
111f2228 29VA_LIST mVaListNull;\r
446be24d 30\r
cf1d4549
JY
31/**\r
32 Get stack frame pointer of function call.\r
33\r
34 @return StackFramePointer stack frame pointer of function call.\r
35**/\r
36UINT32 *\r
37EFIAPI\r
38GetStackFramePointer (\r
39 VOID\r
40 );\r
41\r
cf1d4549
JY
42/**\r
43 Prints a debug message to the debug output device if the specified error level is enabled.\r
44\r
45 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function\r
46 GetDebugPrintErrorLevel (), then print the message specified by Format and the\r
47 associated variable argument list to the debug output device.\r
48\r
49 If Format is NULL, then ASSERT().\r
50\r
51 @param ErrorLevel The error level of the debug message.\r
52 @param Format Format string for the debug message to print.\r
53 @param ... Variable argument list whose contents are accessed\r
54 based on the format string specified by Format.\r
55\r
56**/\r
57VOID\r
58EFIAPI\r
59DebugPrint (\r
60 IN UINTN ErrorLevel,\r
61 IN CONST CHAR8 *Format,\r
62 ...\r
63 )\r
446be24d 64{\r
111f2228 65 VA_LIST Marker;\r
446be24d
BB
66\r
67 VA_START (Marker, Format);\r
68 DebugVPrint (ErrorLevel, Format, Marker);\r
69 VA_END (Marker);\r
70}\r
71\r
72/**\r
73 Prints a debug message to the debug output device if the specified\r
74 error level is enabled base on Null-terminated format string and a\r
75 VA_LIST argument list or a BASE_LIST argument list.\r
76\r
77 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function\r
78 GetDebugPrintErrorLevel (), then print the message specified by Format and\r
79 the associated variable argument list to the debug output device.\r
80\r
81 If Format is NULL, then ASSERT().\r
82\r
83 @param ErrorLevel The error level of the debug message.\r
84 @param Format Format string for the debug message to print.\r
85 @param VaListMarker VA_LIST marker for the variable argument list.\r
86 @param BaseListMarker BASE_LIST marker for the variable argument list.\r
87\r
88**/\r
89VOID\r
90DebugPrintMarker (\r
111f2228
MK
91 IN UINTN ErrorLevel,\r
92 IN CONST CHAR8 *Format,\r
93 IN VA_LIST VaListMarker,\r
94 IN BASE_LIST BaseListMarker\r
446be24d 95 )\r
cf1d4549 96{\r
111f2228 97 CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];\r
cf1d4549
JY
98\r
99 //\r
100 // If Format is NULL, then ASSERT().\r
101 //\r
102 if (!GetDebugPrintDeviceEnable ()) {\r
103 return;\r
104 }\r
105\r
106 //\r
107 // Check driver debug mask value and global mask\r
108 //\r
109 if ((ErrorLevel & GetDebugPrintErrorLevel ()) == 0) {\r
110 return;\r
111 }\r
112\r
113 //\r
114 // If Format is NULL, then ASSERT().\r
115 //\r
116 ASSERT (Format != NULL);\r
117\r
118 //\r
119 // Convert the DEBUG() message to an ASCII String\r
120 //\r
446be24d
BB
121 if (BaseListMarker == NULL) {\r
122 AsciiVSPrint (Buffer, sizeof (Buffer), Format, VaListMarker);\r
123 } else {\r
124 AsciiBSPrint (Buffer, sizeof (Buffer), Format, BaseListMarker);\r
125 }\r
cf1d4549
JY
126\r
127 //\r
128 // Send the print string to a Serial Port\r
129 //\r
130 SerialPortWrite ((UINT8 *)Buffer, AsciiStrLen (Buffer));\r
131}\r
132\r
446be24d
BB
133/**\r
134 Prints a debug message to the debug output device if the specified\r
135 error level is enabled.\r
136\r
137 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function\r
138 GetDebugPrintErrorLevel (), then print the message specified by Format and\r
139 the associated variable argument list to the debug output device.\r
140\r
141 If Format is NULL, then ASSERT().\r
142\r
143 @param ErrorLevel The error level of the debug message.\r
144 @param Format Format string for the debug message to print.\r
145 @param VaListMarker VA_LIST marker for the variable argument list.\r
146\r
147**/\r
148VOID\r
149EFIAPI\r
150DebugVPrint (\r
111f2228
MK
151 IN UINTN ErrorLevel,\r
152 IN CONST CHAR8 *Format,\r
153 IN VA_LIST VaListMarker\r
446be24d
BB
154 )\r
155{\r
156 DebugPrintMarker (ErrorLevel, Format, VaListMarker, NULL);\r
157}\r
158\r
159/**\r
160 Prints a debug message to the debug output device if the specified\r
161 error level is enabled.\r
162 This function use BASE_LIST which would provide a more compatible\r
163 service than VA_LIST.\r
164\r
165 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function\r
166 GetDebugPrintErrorLevel (), then print the message specified by Format and\r
167 the associated variable argument list to the debug output device.\r
168\r
169 If Format is NULL, then ASSERT().\r
170\r
171 @param ErrorLevel The error level of the debug message.\r
172 @param Format Format string for the debug message to print.\r
173 @param BaseListMarker BASE_LIST marker for the variable argument list.\r
174\r
175**/\r
176VOID\r
177EFIAPI\r
178DebugBPrint (\r
111f2228
MK
179 IN UINTN ErrorLevel,\r
180 IN CONST CHAR8 *Format,\r
181 IN BASE_LIST BaseListMarker\r
446be24d
BB
182 )\r
183{\r
184 DebugPrintMarker (ErrorLevel, Format, mVaListNull, BaseListMarker);\r
185}\r
186\r
cf1d4549 187/**\r
91cc60ba 188 Convert an UINT32 value into HEX string specified by Buffer.\r
cf1d4549
JY
189\r
190 @param Value The HEX value to convert to string\r
191 @param Buffer The pointer to the target buffer to be filled with HEX string\r
192\r
193**/\r
194VOID\r
195FillHex (\r
111f2228 196 UINT32 Value,\r
cf1d4549
JY
197 CHAR8 *Buffer\r
198 )\r
199{\r
200 INTN Idx;\r
111f2228 201\r
cf1d4549
JY
202 for (Idx = 7; Idx >= 0; Idx--) {\r
203 Buffer[Idx] = mHexTable[Value & 0x0F];\r
111f2228 204 Value >>= 4;\r
cf1d4549
JY
205 }\r
206}\r
207\r
208/**\r
209 Prints an assert message containing a filename, line number, and description.\r
210 This may be followed by a breakpoint or a dead loop.\r
211\r
212 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"\r
213 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of\r
91cc60ba
AC
214 PcdDebugPropertyMask is set then CpuBreakpoint() is called. Otherwise, if\r
215 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugPropertyMask is set then\r
cf1d4549
JY
216 CpuDeadLoop() is called. If neither of these bits are set, then this function\r
217 returns immediately after the message is printed to the debug output device.\r
218 DebugAssert() must actively prevent recursion. If DebugAssert() is called while\r
219 processing another DebugAssert(), then DebugAssert() must return immediately.\r
220\r
221 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.\r
222 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.\r
223\r
224**/\r
225VOID\r
226DebugAssertInternal (\r
227 VOID\r
228 )\r
229{\r
111f2228
MK
230 CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];\r
231 UINT32 *Frame;\r
cf1d4549
JY
232\r
233 Frame = (UINT32 *)GetStackFramePointer ();\r
234\r
235 //\r
236 // Generate the ASSERT() message in Ascii format\r
237 //\r
238 AsciiStrnCpyS (\r
239 Buffer,\r
111f2228 240 sizeof (Buffer) / sizeof (CHAR8),\r
cf1d4549 241 "-> EBP:0x00000000 EIP:0x00000000\n",\r
111f2228 242 sizeof (Buffer) / sizeof (CHAR8) - 1\r
cf1d4549
JY
243 );\r
244 SerialPortWrite ((UINT8 *)"ASSERT DUMP:\n", 13);\r
245 while (Frame != NULL) {\r
246 FillHex ((UINT32)Frame, Buffer + 9);\r
247 FillHex (Frame[1], Buffer + 9 + 8 + 8);\r
248 SerialPortWrite ((UINT8 *)Buffer, AsciiStrLen (Buffer));\r
249 if ((Frame[0] > (UINT32)Frame) && (Frame[0] < (UINT32)Frame + 0x00100000)) {\r
250 Frame = (UINT32 *)Frame[0];\r
251 } else {\r
252 Frame = NULL;\r
253 }\r
254 }\r
255\r
256 //\r
257 // Dead loop\r
258 //\r
259 CpuDeadLoop ();\r
260}\r
261\r
262/**\r
263 Prints an assert message containing a filename, line number, and description.\r
264 This may be followed by a breakpoint or a dead loop.\r
265\r
266 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"\r
267 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of\r
91cc60ba
AC
268 PcdDebugPropertyMask is set then CpuBreakpoint() is called. Otherwise, if\r
269 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugPropertyMask is set then\r
cf1d4549
JY
270 CpuDeadLoop() is called. If neither of these bits are set, then this function\r
271 returns immediately after the message is printed to the debug output device.\r
272 DebugAssert() must actively prevent recursion. If DebugAssert() is called while\r
273 processing another DebugAssert(), then DebugAssert() must return immediately.\r
274\r
275 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.\r
276 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.\r
277\r
278 @param FileName The pointer to the name of the source file that generated the assert condition.\r
279 @param LineNumber The line number in the source file that generated the assert condition\r
280 @param Description The pointer to the description of the assert condition.\r
281\r
282**/\r
283VOID\r
284EFIAPI\r
285DebugAssert (\r
286 IN CONST CHAR8 *FileName,\r
287 IN UINTN LineNumber,\r
288 IN CONST CHAR8 *Description\r
289 )\r
290{\r
291 DebugAssertInternal ();\r
292}\r
293\r
cf1d4549
JY
294/**\r
295 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.\r
296\r
297 This function fills Length bytes of Buffer with the value specified by\r
298 PcdDebugClearMemoryValue, and returns Buffer.\r
299\r
300 If Buffer is NULL, then ASSERT().\r
301 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
302\r
303 @param Buffer The pointer to the target buffer to be filled with PcdDebugClearMemoryValue.\r
304 @param Length The number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.\r
305\r
306 @return Buffer The pointer to the target buffer filled with PcdDebugClearMemoryValue.\r
307\r
308**/\r
309VOID *\r
310EFIAPI\r
311DebugClearMemory (\r
312 OUT VOID *Buffer,\r
313 IN UINTN Length\r
314 )\r
315{\r
316 return Buffer;\r
317}\r
318\r
cf1d4549
JY
319/**\r
320 Returns TRUE if ASSERT() macros are enabled.\r
321\r
322 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of\r
91cc60ba 323 PcdDebugPropertyMask is set. Otherwise FALSE is returned.\r
cf1d4549 324\r
91cc60ba
AC
325 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugPropertyMask is set.\r
326 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugPropertyMask is clear.\r
cf1d4549
JY
327\r
328**/\r
329BOOLEAN\r
330EFIAPI\r
331DebugAssertEnabled (\r
332 VOID\r
333 )\r
334{\r
111f2228 335 return (BOOLEAN)((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) != 0);\r
cf1d4549
JY
336}\r
337\r
cf1d4549
JY
338/**\r
339 Returns TRUE if DEBUG() macros are enabled.\r
340\r
341 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of\r
91cc60ba 342 PcdDebugPropertyMask is set. Otherwise FALSE is returned.\r
cf1d4549 343\r
91cc60ba
AC
344 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugPropertyMask is set.\r
345 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugPropertyMask is clear.\r
cf1d4549
JY
346\r
347**/\r
348BOOLEAN\r
349EFIAPI\r
350DebugPrintEnabled (\r
351 VOID\r
352 )\r
353{\r
111f2228 354 return (BOOLEAN)((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED) != 0);\r
cf1d4549
JY
355}\r
356\r
357/**\r
358 Returns TRUE if DEBUG_CODE() macros are enabled.\r
359\r
360 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of\r
91cc60ba 361 PcdDebugPropertyMask is set. Otherwise FALSE is returned.\r
cf1d4549 362\r
91cc60ba
AC
363 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugPropertyMask is set.\r
364 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugPropertyMask is clear.\r
cf1d4549
JY
365\r
366**/\r
367BOOLEAN\r
368EFIAPI\r
369DebugCodeEnabled (\r
370 VOID\r
371 )\r
372{\r
111f2228 373 return (BOOLEAN)((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED) != 0);\r
cf1d4549
JY
374}\r
375\r
cf1d4549
JY
376/**\r
377 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.\r
378\r
379 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of\r
91cc60ba 380 PcdDebugPropertyMask is set. Otherwise FALSE is returned.\r
cf1d4549 381\r
91cc60ba
AC
382 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugPropertyMask is set.\r
383 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugPropertyMask is clear.\r
cf1d4549
JY
384\r
385**/\r
386BOOLEAN\r
387EFIAPI\r
388DebugClearMemoryEnabled (\r
389 VOID\r
390 )\r
391{\r
111f2228 392 return (BOOLEAN)((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED) != 0);\r
cf1d4549
JY
393}\r
394\r
395/**\r
396 Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.\r
397\r
398 This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.\r
399\r
400 @retval TRUE Current ErrorLevel is supported.\r
401 @retval FALSE Current ErrorLevel is not supported.\r
402\r
403**/\r
404BOOLEAN\r
405EFIAPI\r
406DebugPrintLevelEnabled (\r
111f2228 407 IN CONST UINTN ErrorLevel\r
cf1d4549
JY
408 )\r
409{\r
111f2228 410 return (BOOLEAN)((ErrorLevel & PcdGet32 (PcdFixedDebugPrintErrorLevel)) != 0);\r
cf1d4549 411}\r