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 This program and the accompanying materials
11 are licensed and made available under the terms and conditions of the BSD License
12 which accompanies this distribution. The full text of the license may be found at
13 http://opensource.org/licenses/bsd-license.php.
15 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
16 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
21 #include <Library/DebugLib.h>
22 #include <Library/DebugPrintErrorLevelLib.h>
23 #include <Library/BaseLib.h>
24 #include <Library/PrintLib.h>
25 #include <Library/PcdLib.h>
26 #include <Library/BaseMemoryLib.h>
27 #include <Library/SerialPortLib.h>
29 STATIC EFI_EVENT mEfiExitBootServicesEvent
;
30 STATIC BOOLEAN mEfiAtRuntime
= FALSE
;
33 // Define the maximum debug and assert message length that this library supports
35 #define MAX_DEBUG_MESSAGE_LENGTH 0x100
38 // VA_LIST can not initialize to NULL for all compiler, so we use this to
39 // indicate a null VA_LIST
44 Set AtRuntime flag as TRUE after ExitBootServices.
46 @param[in] Event The Event that is being processed.
47 @param[in] Context The Event Context.
53 ExitBootServicesEvent (
62 The constructor function to initialize the Serial Port library and
63 register a callback for the ExitBootServices event.
65 @param[in] ImageHandle The firmware allocated handle for the EFI image.
66 @param[in] SystemTable A pointer to the EFI System Table.
68 @retval EFI_SUCCESS The operation completed successfully.
69 @retval other Either the serial port failed to initialize or the
70 ExitBootServices event callback registration failed.
74 DxeRuntimeDebugLibSerialPortConstructor (
75 IN EFI_HANDLE ImageHandle
,
76 IN EFI_SYSTEM_TABLE
*SystemTable
81 Status
= SerialPortInitialize ();
82 if (EFI_ERROR (Status
)) {
86 return SystemTable
->BootServices
->CreateEventEx (EVT_NOTIFY_SIGNAL
,
87 TPL_NOTIFY
, ExitBootServicesEvent
, NULL
,
88 &gEfiEventExitBootServicesGuid
,
89 &mEfiExitBootServicesEvent
);
93 If a runtime driver exits with an error, it must call this routine
94 to free the allocated resource before the exiting.
96 @param[in] ImageHandle The firmware allocated handle for the EFI image.
97 @param[in] SystemTable A pointer to the EFI System Table.
99 @retval EFI_SUCCESS The Runtime Driver Lib shutdown successfully.
100 @retval EFI_UNSUPPORTED Runtime Driver lib was not initialized.
104 DxeRuntimeDebugLibSerialPortDestructor (
105 IN EFI_HANDLE ImageHandle
,
106 IN EFI_SYSTEM_TABLE
*SystemTable
109 return SystemTable
->BootServices
->CloseEvent (mEfiExitBootServicesEvent
);
113 Prints a debug message to the debug output device if the specified error level is enabled.
115 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
116 GetDebugPrintErrorLevel (), then print the message specified by Format and the
117 associated variable argument list to the debug output device.
119 If Format is NULL, then ASSERT().
121 @param ErrorLevel The error level of the debug message.
122 @param Format Format string for the debug message to print.
123 @param ... Variable argument list whose contents are accessed
124 based on the format string specified by Format.
131 IN CONST CHAR8
*Format
,
137 VA_START (Marker
, Format
);
138 DebugVPrint (ErrorLevel
, Format
, Marker
);
144 Prints a debug message to the debug output device if the specified
145 error level is enabled base on Null-terminated format string and a
146 VA_LIST argument list or a BASE_LIST argument list.
148 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
149 GetDebugPrintErrorLevel (), then print the message specified by Format and
150 the associated variable argument list to the debug output device.
152 If Format is NULL, then ASSERT().
154 @param ErrorLevel The error level of the debug message.
155 @param Format Format string for the debug message to print.
156 @param VaListMarker VA_LIST marker for the variable argument list.
157 @param BaseListMarker BASE_LIST marker for the variable argument list.
163 IN CONST CHAR8
*Format
,
164 IN VA_LIST VaListMarker
,
165 IN BASE_LIST BaseListMarker
168 CHAR8 Buffer
[MAX_DEBUG_MESSAGE_LENGTH
];
175 // If Format is NULL, then ASSERT().
177 ASSERT (Format
!= NULL
);
180 // Check driver debug mask value and global mask
182 if ((ErrorLevel
& GetDebugPrintErrorLevel ()) == 0) {
187 // Convert the DEBUG() message to an ASCII String
189 if (BaseListMarker
== NULL
) {
190 AsciiVSPrint (Buffer
, sizeof (Buffer
), Format
, VaListMarker
);
192 AsciiBSPrint (Buffer
, sizeof (Buffer
), Format
, BaseListMarker
);
196 // Send the print string to a Serial Port
198 SerialPortWrite ((UINT8
*)Buffer
, AsciiStrLen (Buffer
));
203 Prints a debug message to the debug output device if the specified
204 error level is enabled.
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 VaListMarker VA_LIST marker for the variable argument list.
221 IN CONST CHAR8
*Format
,
222 IN VA_LIST VaListMarker
225 DebugPrintMarker (ErrorLevel
, Format
, VaListMarker
, NULL
);
230 Prints a debug message to the debug output device if the specified
231 error level is enabled.
232 This function use BASE_LIST which would provide a more compatible
233 service than VA_LIST.
235 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
236 GetDebugPrintErrorLevel (), then print the message specified by Format and
237 the associated variable argument list to the debug output device.
239 If Format is NULL, then ASSERT().
241 @param ErrorLevel The error level of the debug message.
242 @param Format Format string for the debug message to print.
243 @param BaseListMarker BASE_LIST marker for the variable argument list.
250 IN CONST CHAR8
*Format
,
251 IN BASE_LIST BaseListMarker
254 DebugPrintMarker (ErrorLevel
, Format
, mVaListNull
, BaseListMarker
);
259 Prints an assert message containing a filename, line number, and description.
260 This may be followed by a breakpoint or a dead loop.
262 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"
263 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of
264 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if
265 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then
266 CpuDeadLoop() is called. If neither of these bits are set, then this function
267 returns immediately after the message is printed to the debug output device.
268 DebugAssert() must actively prevent recursion. If DebugAssert() is called while
269 processing another DebugAssert(), then DebugAssert() must return immediately.
271 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.
272 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.
274 @param FileName The pointer to the name of the source file that generated the assert condition.
275 @param LineNumber The line number in the source file that generated the assert condition
276 @param Description The pointer to the description of the assert condition.
282 IN CONST CHAR8
*FileName
,
284 IN CONST CHAR8
*Description
287 CHAR8 Buffer
[MAX_DEBUG_MESSAGE_LENGTH
];
290 // Generate the ASSERT() message in Ascii format
292 AsciiSPrint (Buffer
, sizeof (Buffer
), "ASSERT [%a] %a(%d): %a\n",
293 gEfiCallerBaseName
, FileName
, LineNumber
, Description
);
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) {
314 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.
316 This function fills Length bytes of Buffer with the value specified by
317 PcdDebugClearMemoryValue, and returns Buffer.
319 If Buffer is NULL, then ASSERT().
320 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
322 @param Buffer The pointer to the target buffer to be filled with PcdDebugClearMemoryValue.
323 @param Length The number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.
325 @return Buffer The pointer to the target buffer filled with PcdDebugClearMemoryValue.
336 // If Buffer is NULL, then ASSERT().
338 ASSERT (Buffer
!= NULL
);
341 // SetMem() checks for the the ASSERT() condition on Length and returns Buffer
343 return SetMem (Buffer
, Length
, PcdGet8(PcdDebugClearMemoryValue
));
348 Returns TRUE if ASSERT() macros are enabled.
350 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of
351 PcdDebugProperyMask is set. Otherwise FALSE is returned.
353 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.
354 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.
363 return (BOOLEAN
) ((PcdGet8(PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED
) != 0);
368 Returns TRUE if DEBUG() macros are enabled.
370 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of
371 PcdDebugProperyMask is set. Otherwise FALSE is returned.
373 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.
374 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.
383 return (BOOLEAN
) ((PcdGet8(PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED
) != 0);
388 Returns TRUE if DEBUG_CODE() macros are enabled.
390 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of
391 PcdDebugProperyMask is set. Otherwise FALSE is returned.
393 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.
394 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.
403 return (BOOLEAN
) ((PcdGet8(PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED
) != 0);
408 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.
410 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of
411 PcdDebugProperyMask is set. Otherwise FALSE is returned.
413 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.
414 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.
419 DebugClearMemoryEnabled (
423 return (BOOLEAN
) ((PcdGet8(PcdDebugPropertyMask
) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED
) != 0);
427 Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.
429 This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.
431 @retval TRUE Current ErrorLevel is supported.
432 @retval FALSE Current ErrorLevel is not supported.
437 DebugPrintLevelEnabled (
438 IN CONST UINTN ErrorLevel
441 return (BOOLEAN
) ((ErrorLevel
& PcdGet32(PcdFixedDebugPrintErrorLevel
)) != 0);