]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseDebugLibSerialPort/DebugLib.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdePkg / Library / BaseDebugLibSerialPort / DebugLib.c
CommitLineData
7bc232b2 1/** @file\r
eceb3a4c
LG
2 Base Debug library instance base on Serial Port library.\r
3 It uses PrintLib to send debug messages to serial port device.\r
9095d37b
LG
4\r
5 NOTE: If the Serial Port library enables hardware flow control, then a call\r
6 to DebugPrint() or DebugAssert() may hang if writes to the serial port are\r
ccc96db9 7 being blocked. This may occur if a key(s) are pressed in a terminal emulator\r
9095d37b 8 used to monitor the DEBUG() and ASSERT() messages.\r
7bc232b2 9\r
f1320287 10 Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>\r
9344f092 11 SPDX-License-Identifier: BSD-2-Clause-Patent\r
7bc232b2
LG
12\r
13**/\r
14\r
0eeb6d1f 15#include <Base.h>\r
859b72fa
A
16#include <Library/DebugLib.h>\r
17#include <Library/BaseLib.h>\r
18#include <Library/PrintLib.h>\r
19#include <Library/PcdLib.h>\r
20#include <Library/BaseMemoryLib.h>\r
21#include <Library/SerialPortLib.h>\r
2891fc8b 22#include <Library/DebugPrintErrorLevelLib.h>\r
859b72fa 23\r
7bc232b2 24//\r
9095d37b 25// Define the maximum debug and assert message length that this library supports\r
7bc232b2
LG
26//\r
27#define MAX_DEBUG_MESSAGE_LENGTH 0x100\r
28\r
f1320287
BB
29//\r
30// VA_LIST can not initialize to NULL for all compiler, so we use this to\r
31// indicate a null VA_LIST\r
32//\r
2f88bd3a 33VA_LIST mVaListNull;\r
f1320287 34\r
ccc96db9 35/**\r
36 The constructor function initialize the Serial Port Library\r
37\r
38 @retval EFI_SUCCESS The constructor always returns RETURN_SUCCESS.\r
39\r
40**/\r
41RETURN_STATUS\r
42EFIAPI\r
43BaseDebugLibSerialPortConstructor (\r
44 VOID\r
45 )\r
46{\r
47 return SerialPortInitialize ();\r
48}\r
49\r
7bc232b2 50/**\r
7bc232b2
LG
51 Prints a debug message to the debug output device if the specified error level is enabled.\r
52\r
9095d37b
LG
53 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function\r
54 GetDebugPrintErrorLevel (), then print the message specified by Format and the\r
2891fc8b 55 associated variable argument list to the debug output device.\r
7bc232b2
LG
56\r
57 If Format is NULL, then ASSERT().\r
58\r
59 @param ErrorLevel The error level of the debug message.\r
60 @param Format Format string for the debug message to print.\r
9095d37b 61 @param ... Variable argument list whose contents are accessed\r
285010e7 62 based on the format string specified by Format.\r
7bc232b2
LG
63\r
64**/\r
65VOID\r
66EFIAPI\r
67DebugPrint (\r
68 IN UINTN ErrorLevel,\r
69 IN CONST CHAR8 *Format,\r
70 ...\r
71 )\r
72{\r
7bc232b2
LG
73 VA_LIST Marker;\r
74\r
f1320287
BB
75 VA_START (Marker, Format);\r
76 DebugVPrint (ErrorLevel, Format, Marker);\r
77 VA_END (Marker);\r
78}\r
79\r
f1320287
BB
80/**\r
81 Prints a debug message to the debug output device if the specified\r
82 error level is enabled base on Null-terminated format string and a\r
83 VA_LIST argument list or a BASE_LIST argument list.\r
84\r
85 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function\r
86 GetDebugPrintErrorLevel (), then print the message specified by Format and\r
87 the associated variable argument list to the debug output device.\r
88\r
89 If Format is NULL, then ASSERT().\r
90\r
91 @param ErrorLevel The error level of the debug message.\r
92 @param Format Format string for the debug message to print.\r
93 @param VaListMarker VA_LIST marker for the variable argument list.\r
94 @param BaseListMarker BASE_LIST marker for the variable argument list.\r
95\r
96**/\r
97VOID\r
98DebugPrintMarker (\r
2f88bd3a
MK
99 IN UINTN ErrorLevel,\r
100 IN CONST CHAR8 *Format,\r
101 IN VA_LIST VaListMarker,\r
102 IN BASE_LIST BaseListMarker\r
f1320287
BB
103 )\r
104{\r
2f88bd3a 105 CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];\r
f1320287 106\r
7bc232b2
LG
107 //\r
108 // If Format is NULL, then ASSERT().\r
109 //\r
110 ASSERT (Format != NULL);\r
111\r
112 //\r
113 // Check driver debug mask value and global mask\r
114 //\r
2891fc8b 115 if ((ErrorLevel & GetDebugPrintErrorLevel ()) == 0) {\r
7bc232b2
LG
116 return;\r
117 }\r
118\r
119 //\r
120 // Convert the DEBUG() message to an ASCII String\r
121 //\r
f1320287
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
7bc232b2
LG
127\r
128 //\r
9095d37b 129 // Send the print string to a Serial Port\r
7bc232b2 130 //\r
ccc96db9 131 SerialPortWrite ((UINT8 *)Buffer, AsciiStrLen (Buffer));\r
7bc232b2
LG
132}\r
133\r
f1320287
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
2f88bd3a
MK
152 IN UINTN ErrorLevel,\r
153 IN CONST CHAR8 *Format,\r
154 IN VA_LIST VaListMarker\r
f1320287
BB
155 )\r
156{\r
157 DebugPrintMarker (ErrorLevel, Format, VaListMarker, NULL);\r
158}\r
159\r
f1320287
BB
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
2f88bd3a
MK
180 IN UINTN ErrorLevel,\r
181 IN CONST CHAR8 *Format,\r
182 IN BASE_LIST BaseListMarker\r
f1320287
BB
183 )\r
184{\r
185 DebugPrintMarker (ErrorLevel, Format, mVaListNull, BaseListMarker);\r
186}\r
187\r
7bc232b2 188/**\r
9095d37b 189 Prints an assert message containing a filename, line number, and description.\r
7bc232b2
LG
190 This may be followed by a breakpoint or a dead loop.\r
191\r
3e5c3238 192 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"\r
9095d37b
LG
193 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of\r
194 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if\r
195 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then\r
196 CpuDeadLoop() is called. If neither of these bits are set, then this function\r
7bc232b2 197 returns immediately after the message is printed to the debug output device.\r
3e5c3238 198 DebugAssert() must actively prevent recursion. If DebugAssert() is called while\r
7bc232b2
LG
199 processing another DebugAssert(), then DebugAssert() must return immediately.\r
200\r
201 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.\r
7bc232b2
LG
202 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.\r
203\r
2fc59a00 204 @param FileName The pointer to the name of the source file that generated the assert condition.\r
7bc232b2 205 @param LineNumber The line number in the source file that generated the assert condition\r
2fc59a00 206 @param Description The pointer to the description of the assert condition.\r
7bc232b2
LG
207\r
208**/\r
209VOID\r
210EFIAPI\r
211DebugAssert (\r
212 IN CONST CHAR8 *FileName,\r
213 IN UINTN LineNumber,\r
214 IN CONST CHAR8 *Description\r
215 )\r
216{\r
217 CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];\r
218\r
219 //\r
7c366ec9 220 // Generate the ASSERT() message in Ascii format\r
7bc232b2 221 //\r
aa0a5149 222 AsciiSPrint (Buffer, sizeof (Buffer), "ASSERT [%a] %a(%d): %a\n", gEfiCallerBaseName, FileName, LineNumber, Description);\r
7bc232b2
LG
223\r
224 //\r
225 // Send the print string to the Console Output device\r
226 //\r
ccc96db9 227 SerialPortWrite ((UINT8 *)Buffer, AsciiStrLen (Buffer));\r
7bc232b2
LG
228\r
229 //\r
230 // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings\r
231 //\r
2f88bd3a 232 if ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED) != 0) {\r
7bc232b2 233 CpuBreakpoint ();\r
2f88bd3a 234 } else if ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED) != 0) {\r
7bc232b2
LG
235 CpuDeadLoop ();\r
236 }\r
237}\r
238\r
7bc232b2 239/**\r
7bc232b2
LG
240 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.\r
241\r
9095d37b 242 This function fills Length bytes of Buffer with the value specified by\r
7bc232b2
LG
243 PcdDebugClearMemoryValue, and returns Buffer.\r
244\r
245 If Buffer is NULL, then ASSERT().\r
9095d37b 246 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
7bc232b2 247\r
2fc59a00 248 @param Buffer The pointer to the target buffer to be filled with PcdDebugClearMemoryValue.\r
9095d37b 249 @param Length The number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.\r
7bc232b2 250\r
2fc59a00 251 @return Buffer The pointer to the target buffer filled with PcdDebugClearMemoryValue.\r
7bc232b2
LG
252\r
253**/\r
254VOID *\r
255EFIAPI\r
256DebugClearMemory (\r
257 OUT VOID *Buffer,\r
258 IN UINTN Length\r
259 )\r
260{\r
261 //\r
262 // If Buffer is NULL, then ASSERT().\r
263 //\r
264 ASSERT (Buffer != NULL);\r
265\r
266 //\r
267 // SetMem() checks for the the ASSERT() condition on Length and returns Buffer\r
268 //\r
2f88bd3a 269 return SetMem (Buffer, Length, PcdGet8 (PcdDebugClearMemoryValue));\r
7bc232b2
LG
270}\r
271\r
7bc232b2 272/**\r
7bc232b2
LG
273 Returns TRUE if ASSERT() macros are enabled.\r
274\r
9095d37b 275 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of\r
7bc232b2
LG
276 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
277\r
278 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.\r
279 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.\r
280\r
281**/\r
282BOOLEAN\r
283EFIAPI\r
284DebugAssertEnabled (\r
285 VOID\r
286 )\r
287{\r
2f88bd3a 288 return (BOOLEAN)((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) != 0);\r
7bc232b2
LG
289}\r
290\r
9095d37b 291/**\r
3e5c3238 292 Returns TRUE if DEBUG() macros are enabled.\r
7bc232b2 293\r
9095d37b 294 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of\r
7bc232b2
LG
295 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
296\r
297 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.\r
298 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.\r
299\r
300**/\r
301BOOLEAN\r
302EFIAPI\r
303DebugPrintEnabled (\r
304 VOID\r
305 )\r
306{\r
2f88bd3a 307 return (BOOLEAN)((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED) != 0);\r
7bc232b2
LG
308}\r
309\r
9095d37b 310/**\r
3e5c3238 311 Returns TRUE if DEBUG_CODE() macros are enabled.\r
7bc232b2 312\r
9095d37b 313 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of\r
7bc232b2
LG
314 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
315\r
316 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.\r
317 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.\r
318\r
319**/\r
320BOOLEAN\r
321EFIAPI\r
322DebugCodeEnabled (\r
323 VOID\r
324 )\r
325{\r
2f88bd3a 326 return (BOOLEAN)((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED) != 0);\r
7bc232b2
LG
327}\r
328\r
9095d37b 329/**\r
3e5c3238 330 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.\r
7bc232b2 331\r
9095d37b 332 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of\r
7bc232b2
LG
333 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
334\r
eceb3a4c
LG
335 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.\r
336 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.\r
7bc232b2
LG
337\r
338**/\r
339BOOLEAN\r
340EFIAPI\r
341DebugClearMemoryEnabled (\r
342 VOID\r
343 )\r
344{\r
2f88bd3a 345 return (BOOLEAN)((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED) != 0);\r
7bc232b2 346}\r
243dfd85
LG
347\r
348/**\r
349 Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.\r
350\r
351 This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.\r
352\r
353 @retval TRUE Current ErrorLevel is supported.\r
354 @retval FALSE Current ErrorLevel is not supported.\r
355\r
356**/\r
357BOOLEAN\r
358EFIAPI\r
359DebugPrintLevelEnabled (\r
2f88bd3a 360 IN CONST UINTN ErrorLevel\r
243dfd85
LG
361 )\r
362{\r
2f88bd3a 363 return (BOOLEAN)((ErrorLevel & PcdGet32 (PcdFixedDebugPrintErrorLevel)) != 0);\r
243dfd85 364}\r