]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseDebugLibSerialPort/DebugLib.c
MdePkg: Replace BSD License with BSD+Patent License
[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
33VA_LIST mVaListNull;\r
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
80\r
81/**\r
82 Prints a debug message to the debug output device if the specified\r
83 error level is enabled base on Null-terminated format string and a\r
84 VA_LIST argument list or a BASE_LIST argument list.\r
85\r
86 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function\r
87 GetDebugPrintErrorLevel (), then print the message specified by Format and\r
88 the associated variable argument list to the debug output device.\r
89\r
90 If Format is NULL, then ASSERT().\r
91\r
92 @param ErrorLevel The error level of the debug message.\r
93 @param Format Format string for the debug message to print.\r
94 @param VaListMarker VA_LIST marker for the variable argument list.\r
95 @param BaseListMarker BASE_LIST marker for the variable argument list.\r
96\r
97**/\r
98VOID\r
99DebugPrintMarker (\r
100 IN UINTN ErrorLevel,\r
101 IN CONST CHAR8 *Format,\r
102 IN VA_LIST VaListMarker,\r
103 IN BASE_LIST BaseListMarker\r
104 )\r
105{\r
106 CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];\r
107\r
7bc232b2
LG
108 //\r
109 // If Format is NULL, then ASSERT().\r
110 //\r
111 ASSERT (Format != NULL);\r
112\r
113 //\r
114 // Check driver debug mask value and global mask\r
115 //\r
2891fc8b 116 if ((ErrorLevel & GetDebugPrintErrorLevel ()) == 0) {\r
7bc232b2
LG
117 return;\r
118 }\r
119\r
120 //\r
121 // Convert the DEBUG() message to an ASCII String\r
122 //\r
f1320287
BB
123 if (BaseListMarker == NULL) {\r
124 AsciiVSPrint (Buffer, sizeof (Buffer), Format, VaListMarker);\r
125 } else {\r
126 AsciiBSPrint (Buffer, sizeof (Buffer), Format, BaseListMarker);\r
127 }\r
7bc232b2
LG
128\r
129 //\r
9095d37b 130 // Send the print string to a Serial Port\r
7bc232b2 131 //\r
ccc96db9 132 SerialPortWrite ((UINT8 *)Buffer, AsciiStrLen (Buffer));\r
7bc232b2
LG
133}\r
134\r
135\r
f1320287
BB
136/**\r
137 Prints a debug message to the debug output device if the specified\r
138 error level is enabled.\r
139\r
140 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function\r
141 GetDebugPrintErrorLevel (), then print the message specified by Format and\r
142 the associated variable argument list to the debug output device.\r
143\r
144 If Format is NULL, then ASSERT().\r
145\r
146 @param ErrorLevel The error level of the debug message.\r
147 @param Format Format string for the debug message to print.\r
148 @param VaListMarker VA_LIST marker for the variable argument list.\r
149\r
150**/\r
151VOID\r
152EFIAPI\r
153DebugVPrint (\r
154 IN UINTN ErrorLevel,\r
155 IN CONST CHAR8 *Format,\r
156 IN VA_LIST VaListMarker\r
157 )\r
158{\r
159 DebugPrintMarker (ErrorLevel, Format, VaListMarker, NULL);\r
160}\r
161\r
162\r
163/**\r
164 Prints a debug message to the debug output device if the specified\r
165 error level is enabled.\r
166 This function use BASE_LIST which would provide a more compatible\r
167 service than VA_LIST.\r
168\r
169 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function\r
170 GetDebugPrintErrorLevel (), then print the message specified by Format and\r
171 the associated variable argument list to the debug output device.\r
172\r
173 If Format is NULL, then ASSERT().\r
174\r
175 @param ErrorLevel The error level of the debug message.\r
176 @param Format Format string for the debug message to print.\r
177 @param BaseListMarker BASE_LIST marker for the variable argument list.\r
178\r
179**/\r
180VOID\r
181EFIAPI\r
182DebugBPrint (\r
183 IN UINTN ErrorLevel,\r
184 IN CONST CHAR8 *Format,\r
185 IN BASE_LIST BaseListMarker\r
186 )\r
187{\r
188 DebugPrintMarker (ErrorLevel, Format, mVaListNull, BaseListMarker);\r
189}\r
190\r
191\r
7bc232b2 192/**\r
9095d37b 193 Prints an assert message containing a filename, line number, and description.\r
7bc232b2
LG
194 This may be followed by a breakpoint or a dead loop.\r
195\r
3e5c3238 196 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"\r
9095d37b
LG
197 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of\r
198 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if\r
199 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then\r
200 CpuDeadLoop() is called. If neither of these bits are set, then this function\r
7bc232b2 201 returns immediately after the message is printed to the debug output device.\r
3e5c3238 202 DebugAssert() must actively prevent recursion. If DebugAssert() is called while\r
7bc232b2
LG
203 processing another DebugAssert(), then DebugAssert() must return immediately.\r
204\r
205 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.\r
7bc232b2
LG
206 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.\r
207\r
2fc59a00 208 @param FileName The pointer to the name of the source file that generated the assert condition.\r
7bc232b2 209 @param LineNumber The line number in the source file that generated the assert condition\r
2fc59a00 210 @param Description The pointer to the description of the assert condition.\r
7bc232b2
LG
211\r
212**/\r
213VOID\r
214EFIAPI\r
215DebugAssert (\r
216 IN CONST CHAR8 *FileName,\r
217 IN UINTN LineNumber,\r
218 IN CONST CHAR8 *Description\r
219 )\r
220{\r
221 CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];\r
222\r
223 //\r
7c366ec9 224 // Generate the ASSERT() message in Ascii format\r
7bc232b2 225 //\r
aa0a5149 226 AsciiSPrint (Buffer, sizeof (Buffer), "ASSERT [%a] %a(%d): %a\n", gEfiCallerBaseName, FileName, LineNumber, Description);\r
7bc232b2
LG
227\r
228 //\r
229 // Send the print string to the Console Output device\r
230 //\r
ccc96db9 231 SerialPortWrite ((UINT8 *)Buffer, AsciiStrLen (Buffer));\r
7bc232b2
LG
232\r
233 //\r
234 // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings\r
235 //\r
236 if ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED) != 0) {\r
237 CpuBreakpoint ();\r
238 } else if ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED) != 0) {\r
239 CpuDeadLoop ();\r
240 }\r
241}\r
242\r
243\r
244/**\r
7bc232b2
LG
245 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.\r
246\r
9095d37b 247 This function fills Length bytes of Buffer with the value specified by\r
7bc232b2
LG
248 PcdDebugClearMemoryValue, and returns Buffer.\r
249\r
250 If Buffer is NULL, then ASSERT().\r
9095d37b 251 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
7bc232b2 252\r
2fc59a00 253 @param Buffer The pointer to the target buffer to be filled with PcdDebugClearMemoryValue.\r
9095d37b 254 @param Length The number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.\r
7bc232b2 255\r
2fc59a00 256 @return Buffer The pointer to the target buffer filled with PcdDebugClearMemoryValue.\r
7bc232b2
LG
257\r
258**/\r
259VOID *\r
260EFIAPI\r
261DebugClearMemory (\r
262 OUT VOID *Buffer,\r
263 IN UINTN Length\r
264 )\r
265{\r
266 //\r
267 // If Buffer is NULL, then ASSERT().\r
268 //\r
269 ASSERT (Buffer != NULL);\r
270\r
271 //\r
272 // SetMem() checks for the the ASSERT() condition on Length and returns Buffer\r
273 //\r
274 return SetMem (Buffer, Length, PcdGet8(PcdDebugClearMemoryValue));\r
275}\r
276\r
277\r
278/**\r
7bc232b2
LG
279 Returns TRUE if ASSERT() macros are enabled.\r
280\r
9095d37b 281 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of\r
7bc232b2
LG
282 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
283\r
284 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.\r
285 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.\r
286\r
287**/\r
288BOOLEAN\r
289EFIAPI\r
290DebugAssertEnabled (\r
291 VOID\r
292 )\r
293{\r
294 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) != 0);\r
295}\r
296\r
297\r
9095d37b 298/**\r
3e5c3238 299 Returns TRUE if DEBUG() macros are enabled.\r
7bc232b2 300\r
9095d37b 301 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of\r
7bc232b2
LG
302 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
303\r
304 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.\r
305 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.\r
306\r
307**/\r
308BOOLEAN\r
309EFIAPI\r
310DebugPrintEnabled (\r
311 VOID\r
312 )\r
313{\r
314 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED) != 0);\r
315}\r
316\r
317\r
9095d37b 318/**\r
3e5c3238 319 Returns TRUE if DEBUG_CODE() macros are enabled.\r
7bc232b2 320\r
9095d37b 321 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of\r
7bc232b2
LG
322 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
323\r
324 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.\r
325 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.\r
326\r
327**/\r
328BOOLEAN\r
329EFIAPI\r
330DebugCodeEnabled (\r
331 VOID\r
332 )\r
333{\r
334 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED) != 0);\r
335}\r
336\r
337\r
9095d37b 338/**\r
3e5c3238 339 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.\r
7bc232b2 340\r
9095d37b 341 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of\r
7bc232b2
LG
342 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
343\r
eceb3a4c
LG
344 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.\r
345 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.\r
7bc232b2
LG
346\r
347**/\r
348BOOLEAN\r
349EFIAPI\r
350DebugClearMemoryEnabled (\r
351 VOID\r
352 )\r
353{\r
354 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED) != 0);\r
355}\r
243dfd85
LG
356\r
357/**\r
358 Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.\r
359\r
360 This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.\r
361\r
362 @retval TRUE Current ErrorLevel is supported.\r
363 @retval FALSE Current ErrorLevel is not supported.\r
364\r
365**/\r
366BOOLEAN\r
367EFIAPI\r
368DebugPrintLevelEnabled (\r
369 IN CONST UINTN ErrorLevel\r
370 )\r
371{\r
372 return (BOOLEAN) ((ErrorLevel & PcdGet32(PcdFixedDebugPrintErrorLevel)) != 0);\r
373}\r
374\r