]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFsp2Pkg/Library/BaseFspDebugLibSerialPort/DebugLib.c
IntelFsp2Pkg: BaseFspDebugLibSerialPort Support for X64
[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
0531f613 36UINTN *\r
cf1d4549
JY
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
0531f613 196 UINTN Value,\r
cf1d4549
JY
197 CHAR8 *Buffer\r
198 )\r
199{\r
200 INTN Idx;\r
111f2228 201\r
0531f613 202 for (Idx = (sizeof (UINTN) * 2) - 1; Idx >= 0; Idx--) {\r
cf1d4549 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 230 CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];\r
0531f613 231 UINTN *Frame;\r
cf1d4549 232\r
0531f613 233 Frame = (UINTN *)GetStackFramePointer ();\r
cf1d4549
JY
234\r
235 //\r
236 // Generate the ASSERT() message in Ascii format\r
237 //\r
0531f613
KT
238 if (sizeof (UINTN) == sizeof (UINT32)) {\r
239 AsciiStrnCpyS (\r
240 Buffer,\r
241 sizeof (Buffer) / sizeof (CHAR8),\r
242 "-> EBP:0x00000000 EIP:0x00000000\n",\r
243 sizeof (Buffer) / sizeof (CHAR8) - 1\r
244 );\r
245 } else {\r
246 AsciiStrnCpyS (\r
247 Buffer,\r
248 sizeof (Buffer) / sizeof (CHAR8),\r
249 "-> RBP:0x0000000000000000 RIP:0x0000000000000000\n",\r
250 sizeof (Buffer) / sizeof (CHAR8) - 1\r
251 );\r
252 }\r
cf1d4549
JY
253 SerialPortWrite ((UINT8 *)"ASSERT DUMP:\n", 13);\r
254 while (Frame != NULL) {\r
0531f613
KT
255 FillHex ((UINTN)Frame, Buffer + 9);\r
256 FillHex (Frame[1], Buffer + 9 + (sizeof (UINTN) * 2) + 8);\r
cf1d4549 257 SerialPortWrite ((UINT8 *)Buffer, AsciiStrLen (Buffer));\r
0531f613
KT
258 if ((Frame[0] > (UINTN)Frame) && (Frame[0] < (UINTN)Frame + 0x00100000)) {\r
259 Frame = (UINTN *)Frame[0];\r
cf1d4549
JY
260 } else {\r
261 Frame = NULL;\r
262 }\r
263 }\r
264\r
265 //\r
266 // Dead loop\r
267 //\r
268 CpuDeadLoop ();\r
269}\r
270\r
271/**\r
272 Prints an assert message containing a filename, line number, and description.\r
273 This may be followed by a breakpoint or a dead loop.\r
274\r
275 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"\r
276 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of\r
91cc60ba
AC
277 PcdDebugPropertyMask is set then CpuBreakpoint() is called. Otherwise, if\r
278 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugPropertyMask is set then\r
cf1d4549
JY
279 CpuDeadLoop() is called. If neither of these bits are set, then this function\r
280 returns immediately after the message is printed to the debug output device.\r
281 DebugAssert() must actively prevent recursion. If DebugAssert() is called while\r
282 processing another DebugAssert(), then DebugAssert() must return immediately.\r
283\r
284 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.\r
285 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.\r
286\r
287 @param FileName The pointer to the name of the source file that generated the assert condition.\r
288 @param LineNumber The line number in the source file that generated the assert condition\r
289 @param Description The pointer to the description of the assert condition.\r
290\r
291**/\r
292VOID\r
293EFIAPI\r
294DebugAssert (\r
295 IN CONST CHAR8 *FileName,\r
296 IN UINTN LineNumber,\r
297 IN CONST CHAR8 *Description\r
298 )\r
299{\r
300 DebugAssertInternal ();\r
301}\r
302\r
cf1d4549
JY
303/**\r
304 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.\r
305\r
306 This function fills Length bytes of Buffer with the value specified by\r
307 PcdDebugClearMemoryValue, and returns Buffer.\r
308\r
309 If Buffer is NULL, then ASSERT().\r
310 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
311\r
312 @param Buffer The pointer to the target buffer to be filled with PcdDebugClearMemoryValue.\r
313 @param Length The number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.\r
314\r
315 @return Buffer The pointer to the target buffer filled with PcdDebugClearMemoryValue.\r
316\r
317**/\r
318VOID *\r
319EFIAPI\r
320DebugClearMemory (\r
321 OUT VOID *Buffer,\r
322 IN UINTN Length\r
323 )\r
324{\r
325 return Buffer;\r
326}\r
327\r
cf1d4549
JY
328/**\r
329 Returns TRUE if ASSERT() macros are enabled.\r
330\r
331 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of\r
91cc60ba 332 PcdDebugPropertyMask is set. Otherwise FALSE is returned.\r
cf1d4549 333\r
91cc60ba
AC
334 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugPropertyMask is set.\r
335 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugPropertyMask is clear.\r
cf1d4549
JY
336\r
337**/\r
338BOOLEAN\r
339EFIAPI\r
340DebugAssertEnabled (\r
341 VOID\r
342 )\r
343{\r
111f2228 344 return (BOOLEAN)((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) != 0);\r
cf1d4549
JY
345}\r
346\r
cf1d4549
JY
347/**\r
348 Returns TRUE if DEBUG() macros are enabled.\r
349\r
350 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of\r
91cc60ba 351 PcdDebugPropertyMask is set. Otherwise FALSE is returned.\r
cf1d4549 352\r
91cc60ba
AC
353 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugPropertyMask is set.\r
354 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugPropertyMask is clear.\r
cf1d4549
JY
355\r
356**/\r
357BOOLEAN\r
358EFIAPI\r
359DebugPrintEnabled (\r
360 VOID\r
361 )\r
362{\r
111f2228 363 return (BOOLEAN)((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED) != 0);\r
cf1d4549
JY
364}\r
365\r
366/**\r
367 Returns TRUE if DEBUG_CODE() macros are enabled.\r
368\r
369 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of\r
91cc60ba 370 PcdDebugPropertyMask is set. Otherwise FALSE is returned.\r
cf1d4549 371\r
91cc60ba
AC
372 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugPropertyMask is set.\r
373 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugPropertyMask is clear.\r
cf1d4549
JY
374\r
375**/\r
376BOOLEAN\r
377EFIAPI\r
378DebugCodeEnabled (\r
379 VOID\r
380 )\r
381{\r
111f2228 382 return (BOOLEAN)((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED) != 0);\r
cf1d4549
JY
383}\r
384\r
cf1d4549
JY
385/**\r
386 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.\r
387\r
388 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of\r
91cc60ba 389 PcdDebugPropertyMask is set. Otherwise FALSE is returned.\r
cf1d4549 390\r
91cc60ba
AC
391 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugPropertyMask is set.\r
392 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugPropertyMask is clear.\r
cf1d4549
JY
393\r
394**/\r
395BOOLEAN\r
396EFIAPI\r
397DebugClearMemoryEnabled (\r
398 VOID\r
399 )\r
400{\r
111f2228 401 return (BOOLEAN)((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED) != 0);\r
cf1d4549
JY
402}\r
403\r
404/**\r
405 Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.\r
406\r
407 This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.\r
408\r
409 @retval TRUE Current ErrorLevel is supported.\r
410 @retval FALSE Current ErrorLevel is not supported.\r
411\r
412**/\r
413BOOLEAN\r
414EFIAPI\r
415DebugPrintLevelEnabled (\r
111f2228 416 IN CONST UINTN ErrorLevel\r
cf1d4549
JY
417 )\r
418{\r
111f2228 419 return (BOOLEAN)((ErrorLevel & PcdGet32 (PcdFixedDebugPrintErrorLevel)) != 0);\r
cf1d4549 420}\r