2 UEFI Debug Library that sends messages to EFI_DEBUGPORT_PROTOCOL.Write.
4 Copyright (c) 2015 - 2019, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php.
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include <Library/DebugLib.h>
18 #include <Library/UefiBootServicesTableLib.h>
19 #include <Library/PrintLib.h>
20 #include <Library/PcdLib.h>
21 #include <Library/BaseLib.h>
22 #include <Library/BaseMemoryLib.h>
23 #include <Library/DebugPrintErrorLevelLib.h>
25 #include <Protocol/DebugPort.h>
28 // Define the maximum debug and assert message length that this library supports
30 #define MAX_DEBUG_MESSAGE_LENGTH 0x100
33 // Define the timeout for EFI_DEBUGPORT_PROTOCOL.Write
35 #define WRITE_TIMEOUT 1000
38 EFI_DEBUGPORT_PROTOCOL
*mDebugPort
= NULL
;
41 // VA_LIST can not initialize to NULL for all compiler, so we use this to
42 // indicate a null VA_LIST
47 Send message to DebugPort Protocol.
49 If mDebugPort is NULL, i.e. EFI_DEBUGPORT_PROTOCOL is not located,
50 EFI_DEBUGPORT_PROTOCOL is located first.
51 Then, Buffer is sent via EFI_DEBUGPORT_PROTOCOL.Write.
53 @param Buffer The message to be sent.
54 @param BufferLength The byte length of Buffer.
57 UefiDebugLibDebugPortProtocolWrite (
58 IN CONST CHAR8
*Buffer
,
66 // If mDebugPort is NULL, initialize first.
68 if (mDebugPort
== NULL
) {
69 Status
= gBS
->LocateProtocol (&gEfiDebugPortProtocolGuid
, NULL
, (VOID
**)&mDebugPort
);
70 if (EFI_ERROR (Status
)) {
74 mDebugPort
->Reset (mDebugPort
);
78 // EFI_DEBUGPORT_PROTOCOL.Write is called until all message is sent.
80 while (BufferLength
> 0) {
81 Length
= BufferLength
;
83 Status
= mDebugPort
->Write (mDebugPort
, WRITE_TIMEOUT
, &Length
, (VOID
*) Buffer
);
84 if (EFI_ERROR (Status
) || BufferLength
< Length
) {
89 BufferLength
-= Length
;
94 Prints a debug message to the debug output device if the specified error level is enabled.
96 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
97 GetDebugPrintErrorLevel (), then print the message specified by Format and the
98 associated variable argument list to the debug output device.
100 If Format is NULL, then ASSERT().
102 @param ErrorLevel The error level of the debug message.
103 @param Format Format string for the debug message to print.
104 @param ... A variable argument list whose contents are accessed
105 based on the format string specified by Format.
112 IN CONST CHAR8
*Format
,
118 VA_START (Marker
, Format
);
119 DebugVPrint (ErrorLevel
, Format
, Marker
);
125 Prints a debug message to the debug output device if the specified
126 error level is enabled base on Null-terminated format string and a
127 VA_LIST argument list or a BASE_LIST argument list.
129 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
130 GetDebugPrintErrorLevel (), then print the message specified by Format and
131 the associated variable argument list to the debug output device.
133 If Format is NULL, then ASSERT().
135 @param ErrorLevel The error level of the debug message.
136 @param Format Format string for the debug message to print.
137 @param VaListMarker VA_LIST marker for the variable argument list.
138 @param BaseListMarker BASE_LIST marker for the variable argument list.
144 IN CONST CHAR8
*Format
,
145 IN VA_LIST VaListMarker
,
146 IN BASE_LIST BaseListMarker
149 CHAR8 Buffer
[MAX_DEBUG_MESSAGE_LENGTH
];
152 // If Format is NULL, then ASSERT().
154 ASSERT (Format
!= NULL
);
157 // Check driver debug mask value and global mask
159 if ((ErrorLevel
& GetDebugPrintErrorLevel ()) == 0) {
164 // Convert the DEBUG() message to an ASCII String
166 if (BaseListMarker
== NULL
) {
167 AsciiVSPrint (Buffer
, sizeof (Buffer
), Format
, VaListMarker
);
169 AsciiBSPrint (Buffer
, sizeof (Buffer
), Format
, BaseListMarker
);
173 // Send the print string to EFI_DEBUGPORT_PROTOCOL.Write.
175 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
];
269 // Generate the ASSERT() message in ASCII format
274 "ASSERT [%a] %a(%d): %a\n",
282 // Send the print string to EFI_DEBUGPORT_PROTOCOL.Write.
284 UefiDebugLibDebugPortProtocolWrite (Buffer
, AsciiStrLen (Buffer
));
287 // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings
289 if ((PcdGet8(PcdDebugPropertyMask
) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED
) != 0) {
291 } else if ((PcdGet8(PcdDebugPropertyMask
) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED
) != 0) {
298 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.
300 This function fills Length bytes of Buffer with the value specified by
301 PcdDebugClearMemoryValue, and returns Buffer.
303 If Buffer is NULL, then ASSERT().
304 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
306 @param Buffer The pointer to the target buffer to be filled with PcdDebugClearMemoryValue.
307 @param Length The number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.
309 @return Buffer The pointer to the target buffer filled with PcdDebugClearMemoryValue.
320 // If Buffer is NULL, then ASSERT().
322 ASSERT (Buffer
!= NULL
);
325 // SetMem() checks for the the ASSERT() condition on Length and returns Buffer
327 return SetMem (Buffer
, Length
, PcdGet8(PcdDebugClearMemoryValue
));
332 Returns TRUE if ASSERT() macros are enabled.
334 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of
335 PcdDebugProperyMask is set. Otherwise FALSE is returned.
337 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.
338 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.
347 return (BOOLEAN
) ((PcdGet8(PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED
) != 0);
352 Returns TRUE if DEBUG() macros are enabled.
354 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of
355 PcdDebugProperyMask is set. Otherwise FALSE is returned.
357 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.
358 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.
367 return (BOOLEAN
) ((PcdGet8(PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED
) != 0);
372 Returns TRUE if DEBUG_CODE() macros are enabled.
374 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of
375 PcdDebugProperyMask is set. Otherwise FALSE is returned.
377 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.
378 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.
387 return (BOOLEAN
) ((PcdGet8(PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED
) != 0);
392 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.
394 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of
395 PcdDebugProperyMask is set. Otherwise FALSE is returned.
397 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.
398 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.
403 DebugClearMemoryEnabled (
407 return (BOOLEAN
) ((PcdGet8(PcdDebugPropertyMask
) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED
) != 0);
411 Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.
413 This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.
415 @retval TRUE Current ErrorLevel is supported.
416 @retval FALSE Current ErrorLevel is not supported.
421 DebugPrintLevelEnabled (
422 IN CONST UINTN ErrorLevel
425 return (BOOLEAN
) ((ErrorLevel
& PcdGet32(PcdFixedDebugPrintErrorLevel
)) != 0);