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/UefiBootServicesTableLib.h>
13 #include <Library/PrintLib.h>
14 #include <Library/PcdLib.h>
15 #include <Library/BaseLib.h>
16 #include <Library/BaseMemoryLib.h>
17 #include <Library/DebugPrintErrorLevelLib.h>
19 #include <Protocol/DebugPort.h>
22 // Define the maximum debug and assert message length that this library supports
24 #define MAX_DEBUG_MESSAGE_LENGTH 0x100
27 // Define the timeout for EFI_DEBUGPORT_PROTOCOL.Write
29 #define WRITE_TIMEOUT 1000
32 EFI_DEBUGPORT_PROTOCOL
*mDebugPort
= NULL
;
35 // VA_LIST can not initialize to NULL for all compiler, so we use this to
36 // indicate a null VA_LIST
41 Send message to DebugPort Protocol.
43 If mDebugPort is NULL, i.e. EFI_DEBUGPORT_PROTOCOL is not located,
44 EFI_DEBUGPORT_PROTOCOL is located first.
45 Then, Buffer is sent via EFI_DEBUGPORT_PROTOCOL.Write.
47 @param Buffer The message to be sent.
48 @param BufferLength The byte length of Buffer.
51 UefiDebugLibDebugPortProtocolWrite (
52 IN CONST CHAR8
*Buffer
,
60 // If mDebugPort is NULL, initialize first.
62 if (mDebugPort
== NULL
) {
63 Status
= gBS
->LocateProtocol (&gEfiDebugPortProtocolGuid
, NULL
, (VOID
**)&mDebugPort
);
64 if (EFI_ERROR (Status
)) {
68 mDebugPort
->Reset (mDebugPort
);
72 // EFI_DEBUGPORT_PROTOCOL.Write is called until all message is sent.
74 while (BufferLength
> 0) {
75 Length
= BufferLength
;
77 Status
= mDebugPort
->Write (mDebugPort
, WRITE_TIMEOUT
, &Length
, (VOID
*) Buffer
);
78 if (EFI_ERROR (Status
) || BufferLength
< Length
) {
83 BufferLength
-= Length
;
88 Prints a debug message to the debug output device if the specified error level is enabled.
90 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
91 GetDebugPrintErrorLevel (), then print the message specified by Format and the
92 associated variable argument list to the debug output device.
94 If Format is NULL, then ASSERT().
96 @param ErrorLevel The error level of the debug message.
97 @param Format Format string for the debug message to print.
98 @param ... A variable argument list whose contents are accessed
99 based on the format string specified by Format.
106 IN CONST CHAR8
*Format
,
112 VA_START (Marker
, Format
);
113 DebugVPrint (ErrorLevel
, Format
, Marker
);
119 Prints a debug message to the debug output device if the specified
120 error level is enabled base on Null-terminated format string and a
121 VA_LIST argument list or a BASE_LIST argument list.
123 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
124 GetDebugPrintErrorLevel (), then print the message specified by Format and
125 the associated variable argument list to the debug output device.
127 If Format is NULL, then ASSERT().
129 @param ErrorLevel The error level of the debug message.
130 @param Format Format string for the debug message to print.
131 @param VaListMarker VA_LIST marker for the variable argument list.
132 @param BaseListMarker BASE_LIST marker for the variable argument list.
138 IN CONST CHAR8
*Format
,
139 IN VA_LIST VaListMarker
,
140 IN BASE_LIST BaseListMarker
143 CHAR8 Buffer
[MAX_DEBUG_MESSAGE_LENGTH
];
146 // If Format is NULL, then ASSERT().
148 ASSERT (Format
!= NULL
);
151 // Check driver debug mask value and global mask
153 if ((ErrorLevel
& GetDebugPrintErrorLevel ()) == 0) {
158 // Convert the DEBUG() message to an ASCII String
160 if (BaseListMarker
== NULL
) {
161 AsciiVSPrint (Buffer
, sizeof (Buffer
), Format
, VaListMarker
);
163 AsciiBSPrint (Buffer
, sizeof (Buffer
), Format
, BaseListMarker
);
167 // Send the print string to EFI_DEBUGPORT_PROTOCOL.Write.
169 UefiDebugLibDebugPortProtocolWrite (Buffer
, AsciiStrLen (Buffer
));
174 Prints a debug message to the debug output device if the specified
175 error level is enabled.
177 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
178 GetDebugPrintErrorLevel (), then print the message specified by Format and
179 the associated variable argument list to the debug output device.
181 If Format is NULL, then ASSERT().
183 @param ErrorLevel The error level of the debug message.
184 @param Format Format string for the debug message to print.
185 @param VaListMarker VA_LIST marker for the variable argument list.
192 IN CONST CHAR8
*Format
,
193 IN VA_LIST VaListMarker
196 DebugPrintMarker (ErrorLevel
, Format
, VaListMarker
, NULL
);
201 Prints a debug message to the debug output device if the specified
202 error level is enabled.
203 This function use BASE_LIST which would provide a more compatible
204 service than VA_LIST.
206 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
207 GetDebugPrintErrorLevel (), then print the message specified by Format and
208 the associated variable argument list to the debug output device.
210 If Format is NULL, then ASSERT().
212 @param ErrorLevel The error level of the debug message.
213 @param Format Format string for the debug message to print.
214 @param BaseListMarker BASE_LIST marker for the variable argument list.
221 IN CONST CHAR8
*Format
,
222 IN BASE_LIST BaseListMarker
225 DebugPrintMarker (ErrorLevel
, Format
, mVaListNull
, BaseListMarker
);
230 Prints an assert message containing a filename, line number, and description.
231 This may be followed by a breakpoint or a dead loop.
233 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"
234 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of
235 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if
236 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then
237 CpuDeadLoop() is called. If neither of these bits are set, then this function
238 returns immediately after the message is printed to the debug output device.
239 DebugAssert() must actively prevent recursion. If DebugAssert() is called while
240 processing another DebugAssert(), then DebugAssert() must return immediately.
242 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.
243 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.
245 @param FileName The pointer to the name of the source file that generated
246 the assert condition.
247 @param LineNumber The line number in the source file that generated the
249 @param Description The pointer to the description of the assert condition.
255 IN CONST CHAR8
*FileName
,
257 IN CONST CHAR8
*Description
260 CHAR8 Buffer
[MAX_DEBUG_MESSAGE_LENGTH
];
263 // Generate the ASSERT() message in ASCII format
268 "ASSERT [%a] %a(%d): %a\n",
276 // Send the print string to EFI_DEBUGPORT_PROTOCOL.Write.
278 UefiDebugLibDebugPortProtocolWrite (Buffer
, AsciiStrLen (Buffer
));
281 // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings
283 if ((PcdGet8(PcdDebugPropertyMask
) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED
) != 0) {
285 } else if ((PcdGet8(PcdDebugPropertyMask
) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED
) != 0) {
292 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.
294 This function fills Length bytes of Buffer with the value specified by
295 PcdDebugClearMemoryValue, and returns Buffer.
297 If Buffer is NULL, then ASSERT().
298 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
300 @param Buffer The pointer to the target buffer to be filled with PcdDebugClearMemoryValue.
301 @param Length The number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.
303 @return Buffer The pointer to the target buffer filled with PcdDebugClearMemoryValue.
314 // If Buffer is NULL, then ASSERT().
316 ASSERT (Buffer
!= NULL
);
319 // SetMem() checks for the the ASSERT() condition on Length and returns Buffer
321 return SetMem (Buffer
, Length
, PcdGet8(PcdDebugClearMemoryValue
));
326 Returns TRUE if ASSERT() macros are enabled.
328 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of
329 PcdDebugProperyMask is set. Otherwise FALSE is returned.
331 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.
332 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.
341 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);
366 Returns TRUE if DEBUG_CODE() macros are enabled.
368 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of
369 PcdDebugProperyMask is set. Otherwise FALSE is returned.
371 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.
372 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.
381 return (BOOLEAN
) ((PcdGet8(PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED
) != 0);
386 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.
388 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of
389 PcdDebugProperyMask is set. Otherwise FALSE is returned.
391 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.
392 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.
397 DebugClearMemoryEnabled (
401 return (BOOLEAN
) ((PcdGet8(PcdDebugPropertyMask
) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED
) != 0);
405 Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.
407 This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.
409 @retval TRUE Current ErrorLevel is supported.
410 @retval FALSE Current ErrorLevel is not supported.
415 DebugPrintLevelEnabled (
416 IN CONST UINTN ErrorLevel
419 return (BOOLEAN
) ((ErrorLevel
& PcdGet32(PcdFixedDebugPrintErrorLevel
)) != 0);