]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFsp2Pkg/Library/BaseFspDebugLibSerialPort/DebugLib.c
Revert "FmpDevicePkg: Fix various typos"
[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
29VA_LIST mVaListNull;\r
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
42\r
43/**\r
44 Prints a debug message to the debug output device if the specified error level is enabled.\r
45\r
46 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function\r
47 GetDebugPrintErrorLevel (), then print the message specified by Format and the\r
48 associated variable argument list to the debug output device.\r
49\r
50 If Format is NULL, then ASSERT().\r
51\r
52 @param ErrorLevel The error level of the debug message.\r
53 @param Format Format string for the debug message to print.\r
54 @param ... Variable argument list whose contents are accessed\r
55 based on the format string specified by Format.\r
56\r
57**/\r
58VOID\r
59EFIAPI\r
60DebugPrint (\r
61 IN UINTN ErrorLevel,\r
62 IN CONST CHAR8 *Format,\r
63 ...\r
64 )\r
446be24d
BB
65{\r
66 VA_LIST Marker;\r
67\r
68 VA_START (Marker, Format);\r
69 DebugVPrint (ErrorLevel, Format, Marker);\r
70 VA_END (Marker);\r
71}\r
72\r
73/**\r
74 Prints a debug message to the debug output device if the specified\r
75 error level is enabled base on Null-terminated format string and a\r
76 VA_LIST argument list or a BASE_LIST argument list.\r
77\r
78 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function\r
79 GetDebugPrintErrorLevel (), then print the message specified by Format and\r
80 the associated variable argument list to the debug output device.\r
81\r
82 If Format is NULL, then ASSERT().\r
83\r
84 @param ErrorLevel The error level of the debug message.\r
85 @param Format Format string for the debug message to print.\r
86 @param VaListMarker VA_LIST marker for the variable argument list.\r
87 @param BaseListMarker BASE_LIST marker for the variable argument list.\r
88\r
89**/\r
90VOID\r
91DebugPrintMarker (\r
92 IN UINTN ErrorLevel,\r
93 IN CONST CHAR8 *Format,\r
94 IN VA_LIST VaListMarker,\r
95 IN BASE_LIST BaseListMarker\r
96 )\r
cf1d4549
JY
97{\r
98 CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];\r
cf1d4549
JY
99\r
100 //\r
101 // If Format is NULL, then ASSERT().\r
102 //\r
103 if (!GetDebugPrintDeviceEnable ()) {\r
104 return;\r
105 }\r
106\r
107 //\r
108 // Check driver debug mask value and global mask\r
109 //\r
110 if ((ErrorLevel & GetDebugPrintErrorLevel ()) == 0) {\r
111 return;\r
112 }\r
113\r
114 //\r
115 // If Format is NULL, then ASSERT().\r
116 //\r
117 ASSERT (Format != NULL);\r
118\r
119 //\r
120 // Convert the DEBUG() message to an ASCII String\r
121 //\r
446be24d
BB
122 if (BaseListMarker == NULL) {\r
123 AsciiVSPrint (Buffer, sizeof (Buffer), Format, VaListMarker);\r
124 } else {\r
125 AsciiBSPrint (Buffer, sizeof (Buffer), Format, BaseListMarker);\r
126 }\r
cf1d4549
JY
127\r
128 //\r
129 // Send the print string to a Serial Port\r
130 //\r
131 SerialPortWrite ((UINT8 *)Buffer, AsciiStrLen (Buffer));\r
132}\r
133\r
446be24d
BB
134/**\r
135 Prints a debug message to the debug output device if the specified\r
136 error level is enabled.\r
137\r
138 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function\r
139 GetDebugPrintErrorLevel (), then print the message specified by Format and\r
140 the associated variable argument list to the debug output device.\r
141\r
142 If Format is NULL, then ASSERT().\r
143\r
144 @param ErrorLevel The error level of the debug message.\r
145 @param Format Format string for the debug message to print.\r
146 @param VaListMarker VA_LIST marker for the variable argument list.\r
147\r
148**/\r
149VOID\r
150EFIAPI\r
151DebugVPrint (\r
152 IN UINTN ErrorLevel,\r
153 IN CONST CHAR8 *Format,\r
154 IN VA_LIST VaListMarker\r
155 )\r
156{\r
157 DebugPrintMarker (ErrorLevel, Format, VaListMarker, NULL);\r
158}\r
159\r
160/**\r
161 Prints a debug message to the debug output device if the specified\r
162 error level is enabled.\r
163 This function use BASE_LIST which would provide a more compatible\r
164 service than VA_LIST.\r
165\r
166 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function\r
167 GetDebugPrintErrorLevel (), then print the message specified by Format and\r
168 the associated variable argument list to the debug output device.\r
169\r
170 If Format is NULL, then ASSERT().\r
171\r
172 @param ErrorLevel The error level of the debug message.\r
173 @param Format Format string for the debug message to print.\r
174 @param BaseListMarker BASE_LIST marker for the variable argument list.\r
175\r
176**/\r
177VOID\r
178EFIAPI\r
179DebugBPrint (\r
180 IN UINTN ErrorLevel,\r
181 IN CONST CHAR8 *Format,\r
182 IN BASE_LIST BaseListMarker\r
183 )\r
184{\r
185 DebugPrintMarker (ErrorLevel, Format, mVaListNull, BaseListMarker);\r
186}\r
187\r
cf1d4549 188/**\r
efa12a3f 189 Convert an UINT32 value into HEX string sepcified by Buffer.\r
cf1d4549
JY
190\r
191 @param Value The HEX value to convert to string\r
192 @param Buffer The pointer to the target buffer to be filled with HEX string\r
193\r
194**/\r
195VOID\r
196FillHex (\r
197 UINT32 Value,\r
198 CHAR8 *Buffer\r
199 )\r
200{\r
201 INTN Idx;\r
202 for (Idx = 7; Idx >= 0; Idx--) {\r
203 Buffer[Idx] = mHexTable[Value & 0x0F];\r
204 Value >>= 4;\r
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
efa12a3f
AC
214 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if\r
215 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask 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
230 CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];\r
231 UINT32 *Frame;\r
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
240 sizeof(Buffer) / sizeof(CHAR8),\r
241 "-> EBP:0x00000000 EIP:0x00000000\n",\r
242 sizeof(Buffer) / sizeof(CHAR8) - 1\r
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
efa12a3f
AC
268 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if\r
269 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask 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
294\r
295/**\r
296 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.\r
297\r
298 This function fills Length bytes of Buffer with the value specified by\r
299 PcdDebugClearMemoryValue, and returns Buffer.\r
300\r
301 If Buffer is NULL, then ASSERT().\r
302 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
303\r
304 @param Buffer The pointer to the target buffer to be filled with PcdDebugClearMemoryValue.\r
305 @param Length The number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.\r
306\r
307 @return Buffer The pointer to the target buffer filled with PcdDebugClearMemoryValue.\r
308\r
309**/\r
310VOID *\r
311EFIAPI\r
312DebugClearMemory (\r
313 OUT VOID *Buffer,\r
314 IN UINTN Length\r
315 )\r
316{\r
317 return Buffer;\r
318}\r
319\r
320\r
321/**\r
322 Returns TRUE if ASSERT() macros are enabled.\r
323\r
324 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of\r
efa12a3f 325 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
cf1d4549 326\r
efa12a3f
AC
327 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.\r
328 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.\r
cf1d4549
JY
329\r
330**/\r
331BOOLEAN\r
332EFIAPI\r
333DebugAssertEnabled (\r
334 VOID\r
335 )\r
336{\r
337 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) != 0);\r
338}\r
339\r
340\r
341/**\r
342 Returns TRUE if DEBUG() macros are enabled.\r
343\r
344 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of\r
efa12a3f 345 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
cf1d4549 346\r
efa12a3f
AC
347 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.\r
348 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.\r
cf1d4549
JY
349\r
350**/\r
351BOOLEAN\r
352EFIAPI\r
353DebugPrintEnabled (\r
354 VOID\r
355 )\r
356{\r
357 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED) != 0);\r
358}\r
359\r
360/**\r
361 Returns TRUE if DEBUG_CODE() macros are enabled.\r
362\r
363 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of\r
efa12a3f 364 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
cf1d4549 365\r
efa12a3f
AC
366 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.\r
367 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.\r
cf1d4549
JY
368\r
369**/\r
370BOOLEAN\r
371EFIAPI\r
372DebugCodeEnabled (\r
373 VOID\r
374 )\r
375{\r
376 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED) != 0);\r
377}\r
378\r
379\r
380/**\r
381 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.\r
382\r
383 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of\r
efa12a3f 384 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
cf1d4549 385\r
efa12a3f
AC
386 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.\r
387 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.\r
cf1d4549
JY
388\r
389**/\r
390BOOLEAN\r
391EFIAPI\r
392DebugClearMemoryEnabled (\r
393 VOID\r
394 )\r
395{\r
396 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED) != 0);\r
397}\r
398\r
399/**\r
400 Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.\r
401\r
402 This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.\r
403\r
404 @retval TRUE Current ErrorLevel is supported.\r
405 @retval FALSE Current ErrorLevel is not supported.\r
406\r
407**/\r
408BOOLEAN\r
409EFIAPI\r
410DebugPrintLevelEnabled (\r
411 IN CONST UINTN ErrorLevel\r
412 )\r
413{\r
414 return (BOOLEAN) ((ErrorLevel & PcdGet32(PcdFixedDebugPrintErrorLevel)) != 0);\r
415}\r