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
30 EFI_DEBUGPORT_PROTOCOL
*mDebugPort
= NULL
;
33 // VA_LIST can not initialize to NULL for all compiler, so we use this to
34 // indicate a null VA_LIST
38 extern BOOLEAN mPostEBS
;
39 extern EFI_BOOT_SERVICES
*mDebugBS
;
42 Send message to DebugPort Protocol.
44 If mDebugPort is NULL, i.e. EFI_DEBUGPORT_PROTOCOL is not located,
45 EFI_DEBUGPORT_PROTOCOL is located first.
46 Then, Buffer is sent via EFI_DEBUGPORT_PROTOCOL.Write.
48 @param Buffer The message to be sent.
49 @param BufferLength The byte length of Buffer.
52 UefiDebugLibDebugPortProtocolWrite (
53 IN CONST CHAR8
*Buffer
,
62 // If mDebugPort is NULL, initialize first.
64 if (mDebugPort
== NULL
) {
65 Status
= mDebugBS
->LocateProtocol (&gEfiDebugPortProtocolGuid
, NULL
, (VOID
**)&mDebugPort
);
66 if (EFI_ERROR (Status
)) {
70 mDebugPort
->Reset (mDebugPort
);
74 // EFI_DEBUGPORT_PROTOCOL.Write is called until all message is sent.
76 while (BufferLength
> 0) {
77 Length
= BufferLength
;
79 Status
= mDebugPort
->Write (mDebugPort
, WRITE_TIMEOUT
, &Length
, (VOID
*)Buffer
);
80 if (EFI_ERROR (Status
) || (BufferLength
< Length
)) {
85 BufferLength
-= Length
;
91 Prints a debug message to the debug output device if the specified error level is enabled.
93 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
94 GetDebugPrintErrorLevel (), then print the message specified by Format and the
95 associated variable argument list to the debug output device.
97 If Format is NULL, then ASSERT().
99 @param ErrorLevel The error level of the debug message.
100 @param Format Format string for the debug message to print.
101 @param ... A variable argument list whose contents are accessed
102 based on the format string specified by Format.
109 IN CONST CHAR8
*Format
,
115 VA_START (Marker
, Format
);
116 DebugVPrint (ErrorLevel
, Format
, Marker
);
121 Prints a debug message to the debug output device if the specified
122 error level is enabled base on Null-terminated format string and a
123 VA_LIST argument list or a BASE_LIST argument list.
125 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
126 GetDebugPrintErrorLevel (), then print the message specified by Format and
127 the associated variable argument list to the debug output device.
129 If Format is NULL, then ASSERT().
131 @param ErrorLevel The error level of the debug message.
132 @param Format Format string for the debug message to print.
133 @param VaListMarker VA_LIST marker for the variable argument list.
134 @param BaseListMarker BASE_LIST marker for the variable argument list.
140 IN CONST CHAR8
*Format
,
141 IN VA_LIST VaListMarker
,
142 IN BASE_LIST BaseListMarker
145 CHAR8 Buffer
[MAX_DEBUG_MESSAGE_LENGTH
];
149 // If Format is NULL, then ASSERT().
151 ASSERT (Format
!= NULL
);
154 // Check driver debug mask value and global mask
156 if ((ErrorLevel
& GetDebugPrintErrorLevel ()) == 0) {
161 // Convert the DEBUG() message to an ASCII String
163 if (BaseListMarker
== NULL
) {
164 AsciiVSPrint (Buffer
, sizeof (Buffer
), Format
, VaListMarker
);
166 AsciiBSPrint (Buffer
, sizeof (Buffer
), Format
, BaseListMarker
);
170 // Send the print string to EFI_DEBUGPORT_PROTOCOL.Write.
172 UefiDebugLibDebugPortProtocolWrite (Buffer
, AsciiStrLen (Buffer
));
177 Prints a debug message to the debug output device if the specified
178 error level is enabled.
180 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
181 GetDebugPrintErrorLevel (), then print the message specified by Format and
182 the associated variable argument list to the debug output device.
184 If Format is NULL, then ASSERT().
186 @param ErrorLevel The error level of the debug message.
187 @param Format Format string for the debug message to print.
188 @param VaListMarker VA_LIST marker for the variable argument list.
195 IN CONST CHAR8
*Format
,
196 IN VA_LIST VaListMarker
199 DebugPrintMarker (ErrorLevel
, Format
, VaListMarker
, NULL
);
203 Prints a debug message to the debug output device if the specified
204 error level is enabled.
205 This function use BASE_LIST which would provide a more compatible
206 service than VA_LIST.
208 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
209 GetDebugPrintErrorLevel (), then print the message specified by Format and
210 the associated variable argument list to the debug output device.
212 If Format is NULL, then ASSERT().
214 @param ErrorLevel The error level of the debug message.
215 @param Format Format string for the debug message to print.
216 @param BaseListMarker BASE_LIST marker for the variable argument list.
223 IN CONST CHAR8
*Format
,
224 IN BASE_LIST BaseListMarker
227 DebugPrintMarker (ErrorLevel
, Format
, mVaListNull
, BaseListMarker
);
231 Prints an assert message containing a filename, line number, and description.
232 This may be followed by a breakpoint or a dead loop.
234 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"
235 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of
236 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if
237 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then
238 CpuDeadLoop() is called. If neither of these bits are set, then this function
239 returns immediately after the message is printed to the debug output device.
240 DebugAssert() must actively prevent recursion. If DebugAssert() is called while
241 processing another DebugAssert(), then DebugAssert() must return immediately.
243 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.
244 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.
246 @param FileName The pointer to the name of the source file that generated
247 the assert condition.
248 @param LineNumber The line number in the source file that generated the
250 @param Description The pointer to the description of the assert condition.
256 IN CONST CHAR8
*FileName
,
258 IN CONST CHAR8
*Description
261 CHAR8 Buffer
[MAX_DEBUG_MESSAGE_LENGTH
];
265 // Generate the ASSERT() message in ASCII format
270 "ASSERT [%a] %a(%d): %a\n",
278 // Send the print string to EFI_DEBUGPORT_PROTOCOL.Write.
280 UefiDebugLibDebugPortProtocolWrite (Buffer
, AsciiStrLen (Buffer
));
283 // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings
285 if ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED
) != 0) {
287 } else if ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED
) != 0) {
294 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.
296 This function fills Length bytes of Buffer with the value specified by
297 PcdDebugClearMemoryValue, and returns Buffer.
299 If Buffer is NULL, then ASSERT().
300 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
302 @param Buffer The pointer to the target buffer to be filled with PcdDebugClearMemoryValue.
303 @param Length The number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.
305 @return Buffer The pointer to the target buffer filled with PcdDebugClearMemoryValue.
316 // If Buffer is NULL, then ASSERT().
318 ASSERT (Buffer
!= NULL
);
321 // SetMem() checks for the the ASSERT() condition on Length and returns Buffer
323 return SetMem (Buffer
, Length
, PcdGet8 (PcdDebugClearMemoryValue
));
327 Returns TRUE if ASSERT() macros are enabled.
329 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of
330 PcdDebugProperyMask is set. Otherwise FALSE is returned.
332 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.
333 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.
342 return (BOOLEAN
)((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED
) != 0);
346 Returns TRUE if DEBUG() macros are enabled.
348 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of
349 PcdDebugProperyMask is set. Otherwise FALSE is returned.
351 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.
352 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.
361 return (BOOLEAN
)((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED
) != 0);
365 Returns TRUE if DEBUG_CODE() macros are enabled.
367 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of
368 PcdDebugProperyMask is set. Otherwise FALSE is returned.
370 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.
371 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.
380 return (BOOLEAN
)((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED
) != 0);
384 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.
386 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of
387 PcdDebugProperyMask is set. Otherwise FALSE is returned.
389 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.
390 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.
395 DebugClearMemoryEnabled (
399 return (BOOLEAN
)((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED
) != 0);
403 Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.
405 This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.
407 @retval TRUE Current ErrorLevel is supported.
408 @retval FALSE Current ErrorLevel is not supported.
413 DebugPrintLevelEnabled (
414 IN CONST UINTN ErrorLevel
417 return (BOOLEAN
)((ErrorLevel
& PcdGet32 (PcdFixedDebugPrintErrorLevel
)) != 0);