2 DXE runtime Debug library instance based on Serial Port library.
3 It takes care not to call into SerialPortLib after ExitBootServices() has
4 been called, to prevent touching hardware that is no longer owned by the
7 Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
8 Copyright (c) 2018, Linaro, Ltd. All rights reserved.<BR>
10 SPDX-License-Identifier: BSD-2-Clause-Patent
15 #include <Library/DebugLib.h>
16 #include <Library/DebugPrintErrorLevelLib.h>
17 #include <Library/BaseLib.h>
18 #include <Library/PrintLib.h>
19 #include <Library/PcdLib.h>
20 #include <Library/BaseMemoryLib.h>
21 #include <Library/SerialPortLib.h>
23 STATIC EFI_EVENT mEfiExitBootServicesEvent
;
24 STATIC BOOLEAN mEfiAtRuntime
= FALSE
;
27 // Define the maximum debug and assert message length that this library supports
29 #define MAX_DEBUG_MESSAGE_LENGTH 0x100
32 // VA_LIST can not initialize to NULL for all compiler, so we use this to
33 // indicate a null VA_LIST
38 Set AtRuntime flag as TRUE after ExitBootServices.
40 @param[in] Event The Event that is being processed.
41 @param[in] Context The Event Context.
47 ExitBootServicesEvent (
56 The constructor function to initialize the Serial Port library and
57 register a callback for the ExitBootServices event.
59 @param[in] ImageHandle The firmware allocated handle for the EFI image.
60 @param[in] SystemTable A pointer to the EFI System Table.
62 @retval EFI_SUCCESS The operation completed successfully.
63 @retval other Either the serial port failed to initialize or the
64 ExitBootServices event callback registration failed.
68 DxeRuntimeDebugLibSerialPortConstructor (
69 IN EFI_HANDLE ImageHandle
,
70 IN EFI_SYSTEM_TABLE
*SystemTable
75 Status
= SerialPortInitialize ();
76 if (EFI_ERROR (Status
)) {
80 return SystemTable
->BootServices
->CreateEvent (
81 EVT_SIGNAL_EXIT_BOOT_SERVICES
,
83 ExitBootServicesEvent
,
85 &mEfiExitBootServicesEvent
90 If a runtime driver exits with an error, it must call this routine
91 to free the allocated resource before the exiting.
93 @param[in] ImageHandle The firmware allocated handle for the EFI image.
94 @param[in] SystemTable A pointer to the EFI System Table.
96 @retval EFI_SUCCESS The Runtime Driver Lib shutdown successfully.
97 @retval EFI_UNSUPPORTED Runtime Driver lib was not initialized.
101 DxeRuntimeDebugLibSerialPortDestructor (
102 IN EFI_HANDLE ImageHandle
,
103 IN EFI_SYSTEM_TABLE
*SystemTable
106 return SystemTable
->BootServices
->CloseEvent (mEfiExitBootServicesEvent
);
110 Prints a debug message to the debug output device if the specified error level is enabled.
112 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
113 GetDebugPrintErrorLevel (), then print the message specified by Format and the
114 associated variable argument list to the debug output device.
116 If Format is NULL, then ASSERT().
118 @param ErrorLevel The error level of the debug message.
119 @param Format Format string for the debug message to print.
120 @param ... Variable argument list whose contents are accessed
121 based on the format string specified by Format.
128 IN CONST CHAR8
*Format
,
134 VA_START (Marker
, Format
);
135 DebugVPrint (ErrorLevel
, Format
, Marker
);
140 Prints a debug message to the debug output device if the specified
141 error level is enabled base on Null-terminated format string and a
142 VA_LIST argument list or a BASE_LIST argument list.
144 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
145 GetDebugPrintErrorLevel (), then print the message specified by Format and
146 the associated variable argument list to the debug output device.
148 If Format is NULL, then ASSERT().
150 @param ErrorLevel The error level of the debug message.
151 @param Format Format string for the debug message to print.
152 @param VaListMarker VA_LIST marker for the variable argument list.
153 @param BaseListMarker BASE_LIST marker for the variable argument list.
159 IN CONST CHAR8
*Format
,
160 IN VA_LIST VaListMarker
,
161 IN BASE_LIST BaseListMarker
164 CHAR8 Buffer
[MAX_DEBUG_MESSAGE_LENGTH
];
171 // If Format is NULL, then ASSERT().
173 ASSERT (Format
!= NULL
);
176 // Check driver debug mask value and global mask
178 if ((ErrorLevel
& GetDebugPrintErrorLevel ()) == 0) {
183 // Convert the DEBUG() message to an ASCII String
185 if (BaseListMarker
== NULL
) {
186 AsciiVSPrint (Buffer
, sizeof (Buffer
), Format
, VaListMarker
);
188 AsciiBSPrint (Buffer
, sizeof (Buffer
), Format
, BaseListMarker
);
192 // Send the print string to a Serial Port
194 SerialPortWrite ((UINT8
*)Buffer
, AsciiStrLen (Buffer
));
198 Prints a debug message to the debug output device if the specified
199 error level is enabled.
201 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
202 GetDebugPrintErrorLevel (), then print the message specified by Format and
203 the associated variable argument list to the debug output device.
205 If Format is NULL, then ASSERT().
207 @param ErrorLevel The error level of the debug message.
208 @param Format Format string for the debug message to print.
209 @param VaListMarker VA_LIST marker for the variable argument list.
216 IN CONST CHAR8
*Format
,
217 IN VA_LIST VaListMarker
220 DebugPrintMarker (ErrorLevel
, Format
, VaListMarker
, NULL
);
224 Prints a debug message to the debug output device if the specified
225 error level is enabled.
226 This function use BASE_LIST which would provide a more compatible
227 service than VA_LIST.
229 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
230 GetDebugPrintErrorLevel (), then print the message specified by Format and
231 the associated variable argument list to the debug output device.
233 If Format is NULL, then ASSERT().
235 @param ErrorLevel The error level of the debug message.
236 @param Format Format string for the debug message to print.
237 @param BaseListMarker BASE_LIST marker for the variable argument list.
244 IN CONST CHAR8
*Format
,
245 IN BASE_LIST BaseListMarker
248 DebugPrintMarker (ErrorLevel
, Format
, mVaListNull
, BaseListMarker
);
252 Prints an assert message containing a filename, line number, and description.
253 This may be followed by a breakpoint or a dead loop.
255 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"
256 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of
257 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if
258 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then
259 CpuDeadLoop() is called. If neither of these bits are set, then this function
260 returns immediately after the message is printed to the debug output device.
261 DebugAssert() must actively prevent recursion. If DebugAssert() is called while
262 processing another DebugAssert(), then DebugAssert() must return immediately.
264 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.
265 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.
267 @param FileName The pointer to the name of the source file that generated the assert condition.
268 @param LineNumber The line number in the source file that generated the assert condition
269 @param Description The pointer to the description of the assert condition.
275 IN CONST CHAR8
*FileName
,
277 IN CONST CHAR8
*Description
280 CHAR8 Buffer
[MAX_DEBUG_MESSAGE_LENGTH
];
283 // Generate the ASSERT() message in Ascii format
288 "ASSERT [%a] %a(%d): %a\n",
295 if (!mEfiAtRuntime
) {
297 // Send the print string to the Console Output device
299 SerialPortWrite ((UINT8
*)Buffer
, AsciiStrLen (Buffer
));
303 // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings
305 if ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED
) != 0) {
307 } else if ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED
) != 0) {
313 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.
315 This function fills Length bytes of Buffer with the value specified by
316 PcdDebugClearMemoryValue, and returns Buffer.
318 If Buffer is NULL, then ASSERT().
319 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
321 @param Buffer The pointer to the target buffer to be filled with PcdDebugClearMemoryValue.
322 @param Length The number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.
324 @return Buffer The pointer to the target buffer filled with PcdDebugClearMemoryValue.
335 // If Buffer is NULL, then ASSERT().
337 ASSERT (Buffer
!= NULL
);
340 // SetMem() checks for the the ASSERT() condition on Length and returns Buffer
342 return SetMem (Buffer
, Length
, PcdGet8 (PcdDebugClearMemoryValue
));
346 Returns TRUE if ASSERT() macros are enabled.
348 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of
349 PcdDebugProperyMask is set. Otherwise FALSE is returned.
351 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.
352 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.
361 return (BOOLEAN
)((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED
) != 0);
365 Returns TRUE if DEBUG() macros are enabled.
367 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of
368 PcdDebugProperyMask is set. Otherwise FALSE is returned.
370 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.
371 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.
380 return (BOOLEAN
)((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED
) != 0);
384 Returns TRUE if DEBUG_CODE() macros are enabled.
386 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of
387 PcdDebugProperyMask is set. Otherwise FALSE is returned.
389 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.
390 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.
399 return (BOOLEAN
)((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED
) != 0);
403 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.
405 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of
406 PcdDebugProperyMask is set. Otherwise FALSE is returned.
408 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.
409 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.
414 DebugClearMemoryEnabled (
418 return (BOOLEAN
)((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED
) != 0);
422 Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.
424 This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.
426 @retval TRUE Current ErrorLevel is supported.
427 @retval FALSE Current ErrorLevel is not supported.
432 DebugPrintLevelEnabled (
433 IN CONST UINTN ErrorLevel
436 return (BOOLEAN
)((ErrorLevel
& PcdGet32 (PcdFixedDebugPrintErrorLevel
)) != 0);