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 (EVT_SIGNAL_EXIT_BOOT_SERVICES
,
81 TPL_NOTIFY
, ExitBootServicesEvent
, NULL
,
82 &mEfiExitBootServicesEvent
);
86 If a runtime driver exits with an error, it must call this routine
87 to free the allocated resource before the exiting.
89 @param[in] ImageHandle The firmware allocated handle for the EFI image.
90 @param[in] SystemTable A pointer to the EFI System Table.
92 @retval EFI_SUCCESS The Runtime Driver Lib shutdown successfully.
93 @retval EFI_UNSUPPORTED Runtime Driver lib was not initialized.
97 DxeRuntimeDebugLibSerialPortDestructor (
98 IN EFI_HANDLE ImageHandle
,
99 IN EFI_SYSTEM_TABLE
*SystemTable
102 return SystemTable
->BootServices
->CloseEvent (mEfiExitBootServicesEvent
);
106 Prints a debug message to the debug output device if the specified error level is enabled.
108 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
109 GetDebugPrintErrorLevel (), then print the message specified by Format and the
110 associated variable argument list to the debug output device.
112 If Format is NULL, then ASSERT().
114 @param ErrorLevel The error level of the debug message.
115 @param Format Format string for the debug message to print.
116 @param ... Variable argument list whose contents are accessed
117 based on the format string specified by Format.
124 IN CONST CHAR8
*Format
,
130 VA_START (Marker
, Format
);
131 DebugVPrint (ErrorLevel
, Format
, Marker
);
137 Prints a debug message to the debug output device if the specified
138 error level is enabled base on Null-terminated format string and a
139 VA_LIST argument list or a BASE_LIST argument list.
141 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
142 GetDebugPrintErrorLevel (), then print the message specified by Format and
143 the associated variable argument list to the debug output device.
145 If Format is NULL, then ASSERT().
147 @param ErrorLevel The error level of the debug message.
148 @param Format Format string for the debug message to print.
149 @param VaListMarker VA_LIST marker for the variable argument list.
150 @param BaseListMarker BASE_LIST marker for the variable argument list.
156 IN CONST CHAR8
*Format
,
157 IN VA_LIST VaListMarker
,
158 IN BASE_LIST BaseListMarker
161 CHAR8 Buffer
[MAX_DEBUG_MESSAGE_LENGTH
];
168 // If Format is NULL, then ASSERT().
170 ASSERT (Format
!= NULL
);
173 // Check driver debug mask value and global mask
175 if ((ErrorLevel
& GetDebugPrintErrorLevel ()) == 0) {
180 // Convert the DEBUG() message to an ASCII String
182 if (BaseListMarker
== NULL
) {
183 AsciiVSPrint (Buffer
, sizeof (Buffer
), Format
, VaListMarker
);
185 AsciiBSPrint (Buffer
, sizeof (Buffer
), Format
, BaseListMarker
);
189 // Send the print string to a Serial Port
191 SerialPortWrite ((UINT8
*)Buffer
, AsciiStrLen (Buffer
));
196 Prints a debug message to the debug output device if the specified
197 error level is enabled.
199 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
200 GetDebugPrintErrorLevel (), then print the message specified by Format and
201 the associated variable argument list to the debug output device.
203 If Format is NULL, then ASSERT().
205 @param ErrorLevel The error level of the debug message.
206 @param Format Format string for the debug message to print.
207 @param VaListMarker VA_LIST marker for the variable argument list.
214 IN CONST CHAR8
*Format
,
215 IN VA_LIST VaListMarker
218 DebugPrintMarker (ErrorLevel
, Format
, VaListMarker
, NULL
);
223 Prints a debug message to the debug output device if the specified
224 error level is enabled.
225 This function use BASE_LIST which would provide a more compatible
226 service than VA_LIST.
228 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
229 GetDebugPrintErrorLevel (), then print the message specified by Format and
230 the associated variable argument list to the debug output device.
232 If Format is NULL, then ASSERT().
234 @param ErrorLevel The error level of the debug message.
235 @param Format Format string for the debug message to print.
236 @param BaseListMarker BASE_LIST marker for the variable argument list.
243 IN CONST CHAR8
*Format
,
244 IN BASE_LIST BaseListMarker
247 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
285 AsciiSPrint (Buffer
, sizeof (Buffer
), "ASSERT [%a] %a(%d): %a\n",
286 gEfiCallerBaseName
, FileName
, LineNumber
, Description
);
288 if (!mEfiAtRuntime
) {
290 // Send the print string to the Console Output device
292 SerialPortWrite ((UINT8
*)Buffer
, AsciiStrLen (Buffer
));
296 // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings
298 if ((PcdGet8(PcdDebugPropertyMask
) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED
) != 0) {
300 } else if ((PcdGet8(PcdDebugPropertyMask
) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED
) != 0) {
307 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.
309 This function fills Length bytes of Buffer with the value specified by
310 PcdDebugClearMemoryValue, and returns Buffer.
312 If Buffer is NULL, then ASSERT().
313 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
315 @param Buffer The pointer to the target buffer to be filled with PcdDebugClearMemoryValue.
316 @param Length The number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.
318 @return Buffer The pointer to the target buffer filled with PcdDebugClearMemoryValue.
329 // If Buffer is NULL, then ASSERT().
331 ASSERT (Buffer
!= NULL
);
334 // SetMem() checks for the the ASSERT() condition on Length and returns Buffer
336 return SetMem (Buffer
, Length
, PcdGet8(PcdDebugClearMemoryValue
));
341 Returns TRUE if ASSERT() macros are enabled.
343 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of
344 PcdDebugProperyMask is set. Otherwise FALSE is returned.
346 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.
347 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.
356 return (BOOLEAN
) ((PcdGet8(PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED
) != 0);
361 Returns TRUE if DEBUG() macros are enabled.
363 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of
364 PcdDebugProperyMask is set. Otherwise FALSE is returned.
366 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.
367 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.
376 return (BOOLEAN
) ((PcdGet8(PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED
) != 0);
381 Returns TRUE if DEBUG_CODE() macros are enabled.
383 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of
384 PcdDebugProperyMask is set. Otherwise FALSE is returned.
386 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.
387 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.
396 return (BOOLEAN
) ((PcdGet8(PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED
) != 0);
401 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.
403 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of
404 PcdDebugProperyMask is set. Otherwise FALSE is returned.
406 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.
407 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.
412 DebugClearMemoryEnabled (
416 return (BOOLEAN
) ((PcdGet8(PcdDebugPropertyMask
) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED
) != 0);
420 Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.
422 This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.
424 @retval TRUE Current ErrorLevel is supported.
425 @retval FALSE Current ErrorLevel is not supported.
430 DebugPrintLevelEnabled (
431 IN CONST UINTN ErrorLevel
434 return (BOOLEAN
) ((ErrorLevel
& PcdGet32(PcdFixedDebugPrintErrorLevel
)) != 0);