2 UEFI Debug Library that sends messages to EFI_DEBUGPORT_PROTOCOL.Write.
4 Copyright (c) 2015 - 2019, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include <Library/DebugLib.h>
12 #include <Library/PrintLib.h>
13 #include <Library/PcdLib.h>
14 #include <Library/BaseLib.h>
15 #include <Library/BaseMemoryLib.h>
16 #include <Library/DebugPrintErrorLevelLib.h>
18 #include <Protocol/DebugPort.h>
21 // Define the maximum debug and assert message length that this library supports
23 #define MAX_DEBUG_MESSAGE_LENGTH 0x100
26 // Define the timeout for EFI_DEBUGPORT_PROTOCOL.Write
28 #define WRITE_TIMEOUT 1000
31 EFI_DEBUGPORT_PROTOCOL
*mDebugPort
= NULL
;
34 // VA_LIST can not initialize to NULL for all compiler, so we use this to
35 // indicate a null VA_LIST
39 extern BOOLEAN mPostEBS
;
40 extern EFI_BOOT_SERVICES
*mDebugBS
;
43 Send message to DebugPort Protocol.
45 If mDebugPort is NULL, i.e. EFI_DEBUGPORT_PROTOCOL is not located,
46 EFI_DEBUGPORT_PROTOCOL is located first.
47 Then, Buffer is sent via EFI_DEBUGPORT_PROTOCOL.Write.
49 @param Buffer The message to be sent.
50 @param BufferLength The byte length of Buffer.
53 UefiDebugLibDebugPortProtocolWrite (
54 IN CONST CHAR8
*Buffer
,
63 // If mDebugPort is NULL, initialize first.
65 if (mDebugPort
== NULL
) {
66 Status
= mDebugBS
->LocateProtocol (&gEfiDebugPortProtocolGuid
, NULL
, (VOID
**)&mDebugPort
);
67 if (EFI_ERROR (Status
)) {
71 mDebugPort
->Reset (mDebugPort
);
75 // EFI_DEBUGPORT_PROTOCOL.Write is called until all message is sent.
77 while (BufferLength
> 0) {
78 Length
= BufferLength
;
80 Status
= mDebugPort
->Write (mDebugPort
, WRITE_TIMEOUT
, &Length
, (VOID
*) Buffer
);
81 if (EFI_ERROR (Status
) || BufferLength
< Length
) {
86 BufferLength
-= Length
;
92 Prints a debug message to the debug output device if the specified error level is enabled.
94 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
95 GetDebugPrintErrorLevel (), then print the message specified by Format and the
96 associated variable argument list to the debug output device.
98 If Format is NULL, then ASSERT().
100 @param ErrorLevel The error level of the debug message.
101 @param Format Format string for the debug message to print.
102 @param ... A variable argument list whose contents are accessed
103 based on the format string specified by Format.
110 IN CONST CHAR8
*Format
,
116 VA_START (Marker
, Format
);
117 DebugVPrint (ErrorLevel
, Format
, Marker
);
123 Prints a debug message to the debug output device if the specified
124 error level is enabled base on Null-terminated format string and a
125 VA_LIST argument list or a BASE_LIST argument list.
127 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
128 GetDebugPrintErrorLevel (), then print the message specified by Format and
129 the associated variable argument list to the debug output device.
131 If Format is NULL, then ASSERT().
133 @param ErrorLevel The error level of the debug message.
134 @param Format Format string for the debug message to print.
135 @param VaListMarker VA_LIST marker for the variable argument list.
136 @param BaseListMarker BASE_LIST marker for the variable argument list.
142 IN CONST CHAR8
*Format
,
143 IN VA_LIST VaListMarker
,
144 IN BASE_LIST BaseListMarker
147 CHAR8 Buffer
[MAX_DEBUG_MESSAGE_LENGTH
];
151 // If Format is NULL, then ASSERT().
153 ASSERT (Format
!= NULL
);
156 // Check driver debug mask value and global mask
158 if ((ErrorLevel
& GetDebugPrintErrorLevel ()) == 0) {
163 // Convert the DEBUG() message to an ASCII String
165 if (BaseListMarker
== NULL
) {
166 AsciiVSPrint (Buffer
, sizeof (Buffer
), Format
, VaListMarker
);
168 AsciiBSPrint (Buffer
, sizeof (Buffer
), Format
, BaseListMarker
);
172 // Send the print string to EFI_DEBUGPORT_PROTOCOL.Write.
174 UefiDebugLibDebugPortProtocolWrite (Buffer
, AsciiStrLen (Buffer
));
180 Prints a debug message to the debug output device if the specified
181 error level is enabled.
183 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
184 GetDebugPrintErrorLevel (), then print the message specified by Format and
185 the associated variable argument list to the debug output device.
187 If Format is NULL, then ASSERT().
189 @param ErrorLevel The error level of the debug message.
190 @param Format Format string for the debug message to print.
191 @param VaListMarker VA_LIST marker for the variable argument list.
198 IN CONST CHAR8
*Format
,
199 IN VA_LIST VaListMarker
202 DebugPrintMarker (ErrorLevel
, Format
, VaListMarker
, NULL
);
207 Prints a debug message to the debug output device if the specified
208 error level is enabled.
209 This function use BASE_LIST which would provide a more compatible
210 service than VA_LIST.
212 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
213 GetDebugPrintErrorLevel (), then print the message specified by Format and
214 the associated variable argument list to the debug output device.
216 If Format is NULL, then ASSERT().
218 @param ErrorLevel The error level of the debug message.
219 @param Format Format string for the debug message to print.
220 @param BaseListMarker BASE_LIST marker for the variable argument list.
227 IN CONST CHAR8
*Format
,
228 IN BASE_LIST BaseListMarker
231 DebugPrintMarker (ErrorLevel
, Format
, mVaListNull
, BaseListMarker
);
236 Prints an assert message containing a filename, line number, and description.
237 This may be followed by a breakpoint or a dead loop.
239 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"
240 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of
241 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if
242 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then
243 CpuDeadLoop() is called. If neither of these bits are set, then this function
244 returns immediately after the message is printed to the debug output device.
245 DebugAssert() must actively prevent recursion. If DebugAssert() is called while
246 processing another DebugAssert(), then DebugAssert() must return immediately.
248 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.
249 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.
251 @param FileName The pointer to the name of the source file that generated
252 the assert condition.
253 @param LineNumber The line number in the source file that generated the
255 @param Description The pointer to the description of the assert condition.
261 IN CONST CHAR8
*FileName
,
263 IN CONST CHAR8
*Description
266 CHAR8 Buffer
[MAX_DEBUG_MESSAGE_LENGTH
];
270 // Generate the ASSERT() message in ASCII format
275 "ASSERT [%a] %a(%d): %a\n",
283 // Send the print string to EFI_DEBUGPORT_PROTOCOL.Write.
285 UefiDebugLibDebugPortProtocolWrite (Buffer
, AsciiStrLen (Buffer
));
288 // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings
290 if ((PcdGet8(PcdDebugPropertyMask
) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED
) != 0) {
292 } else if ((PcdGet8(PcdDebugPropertyMask
) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED
) != 0) {
300 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.
302 This function fills Length bytes of Buffer with the value specified by
303 PcdDebugClearMemoryValue, and returns Buffer.
305 If Buffer is NULL, then ASSERT().
306 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
308 @param Buffer The pointer to the target buffer to be filled with PcdDebugClearMemoryValue.
309 @param Length The number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.
311 @return Buffer The pointer to the target buffer filled with PcdDebugClearMemoryValue.
322 // If Buffer is NULL, then ASSERT().
324 ASSERT (Buffer
!= NULL
);
327 // SetMem() checks for the the ASSERT() condition on Length and returns Buffer
329 return SetMem (Buffer
, Length
, PcdGet8(PcdDebugClearMemoryValue
));
334 Returns TRUE if ASSERT() macros are enabled.
336 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of
337 PcdDebugProperyMask is set. Otherwise FALSE is returned.
339 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.
340 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.
349 return (BOOLEAN
) ((PcdGet8(PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED
) != 0);
354 Returns TRUE if DEBUG() macros are enabled.
356 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of
357 PcdDebugProperyMask is set. Otherwise FALSE is returned.
359 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.
360 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.
369 return (BOOLEAN
) ((PcdGet8(PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED
) != 0);
374 Returns TRUE if DEBUG_CODE() macros are enabled.
376 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of
377 PcdDebugProperyMask is set. Otherwise FALSE is returned.
379 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.
380 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.
389 return (BOOLEAN
) ((PcdGet8(PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED
) != 0);
394 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.
396 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of
397 PcdDebugProperyMask is set. Otherwise FALSE is returned.
399 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.
400 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.
405 DebugClearMemoryEnabled (
409 return (BOOLEAN
) ((PcdGet8(PcdDebugPropertyMask
) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED
) != 0);
413 Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.
415 This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.
417 @retval TRUE Current ErrorLevel is supported.
418 @retval FALSE Current ErrorLevel is not supported.
423 DebugPrintLevelEnabled (
424 IN CONST UINTN ErrorLevel
427 return (BOOLEAN
) ((ErrorLevel
& PcdGet32(PcdFixedDebugPrintErrorLevel
)) != 0);