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 - 2011, 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 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
->CreateEventEx (EVT_NOTIFY_SIGNAL
,
81 TPL_NOTIFY
, ExitBootServicesEvent
, NULL
,
82 &gEfiEventExitBootServicesGuid
,
83 &mEfiExitBootServicesEvent
);
87 If a runtime driver exits with an error, it must call this routine
88 to free the allocated resource before the exiting.
90 @param[in] ImageHandle The firmware allocated handle for the EFI image.
91 @param[in] SystemTable A pointer to the EFI System Table.
93 @retval EFI_SUCCESS The Runtime Driver Lib shutdown successfully.
94 @retval EFI_UNSUPPORTED Runtime Driver lib was not initialized.
98 DxeRuntimeDebugLibSerialPortDestructor (
99 IN EFI_HANDLE ImageHandle
,
100 IN EFI_SYSTEM_TABLE
*SystemTable
103 return SystemTable
->BootServices
->CloseEvent (mEfiExitBootServicesEvent
);
107 Prints a debug message to the debug output device if the specified error level is enabled.
109 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
110 GetDebugPrintErrorLevel (), then print the message specified by Format and the
111 associated variable argument list to the debug output device.
113 If Format is NULL, then ASSERT().
115 @param ErrorLevel The error level of the debug message.
116 @param Format Format string for the debug message to print.
117 @param ... Variable argument list whose contents are accessed
118 based on the format string specified by Format.
125 IN CONST CHAR8
*Format
,
129 CHAR8 Buffer
[MAX_DEBUG_MESSAGE_LENGTH
];
137 // If Format is NULL, then ASSERT().
139 ASSERT (Format
!= NULL
);
142 // Check driver debug mask value and global mask
144 if ((ErrorLevel
& GetDebugPrintErrorLevel ()) == 0) {
149 // Convert the DEBUG() message to an ASCII String
151 VA_START (Marker
, Format
);
152 AsciiVSPrint (Buffer
, sizeof (Buffer
), Format
, Marker
);
156 // Send the print string to a Serial Port
158 SerialPortWrite ((UINT8
*)Buffer
, AsciiStrLen (Buffer
));
163 Prints an assert message containing a filename, line number, and description.
164 This may be followed by a breakpoint or a dead loop.
166 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"
167 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of
168 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if
169 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then
170 CpuDeadLoop() is called. If neither of these bits are set, then this function
171 returns immediately after the message is printed to the debug output device.
172 DebugAssert() must actively prevent recursion. If DebugAssert() is called while
173 processing another DebugAssert(), then DebugAssert() must return immediately.
175 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.
176 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.
178 @param FileName The pointer to the name of the source file that generated the assert condition.
179 @param LineNumber The line number in the source file that generated the assert condition
180 @param Description The pointer to the description of the assert condition.
186 IN CONST CHAR8
*FileName
,
188 IN CONST CHAR8
*Description
191 CHAR8 Buffer
[MAX_DEBUG_MESSAGE_LENGTH
];
194 // Generate the ASSERT() message in Ascii format
196 AsciiSPrint (Buffer
, sizeof (Buffer
), "ASSERT [%a] %a(%d): %a\n",
197 gEfiCallerBaseName
, FileName
, LineNumber
, Description
);
199 if (!mEfiAtRuntime
) {
201 // Send the print string to the Console Output device
203 SerialPortWrite ((UINT8
*)Buffer
, AsciiStrLen (Buffer
));
207 // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings
209 if ((PcdGet8(PcdDebugPropertyMask
) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED
) != 0) {
211 } else if ((PcdGet8(PcdDebugPropertyMask
) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED
) != 0) {
218 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.
220 This function fills Length bytes of Buffer with the value specified by
221 PcdDebugClearMemoryValue, and returns Buffer.
223 If Buffer is NULL, then ASSERT().
224 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
226 @param Buffer The pointer to the target buffer to be filled with PcdDebugClearMemoryValue.
227 @param Length The number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.
229 @return Buffer The pointer to the target buffer filled with PcdDebugClearMemoryValue.
240 // If Buffer is NULL, then ASSERT().
242 ASSERT (Buffer
!= NULL
);
245 // SetMem() checks for the the ASSERT() condition on Length and returns Buffer
247 return SetMem (Buffer
, Length
, PcdGet8(PcdDebugClearMemoryValue
));
252 Returns TRUE if ASSERT() macros are enabled.
254 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of
255 PcdDebugProperyMask is set. Otherwise FALSE is returned.
257 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.
258 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.
267 return (BOOLEAN
) ((PcdGet8(PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED
) != 0);
272 Returns TRUE if DEBUG() macros are enabled.
274 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of
275 PcdDebugProperyMask is set. Otherwise FALSE is returned.
277 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.
278 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.
287 return (BOOLEAN
) ((PcdGet8(PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED
) != 0);
292 Returns TRUE if DEBUG_CODE() macros are enabled.
294 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of
295 PcdDebugProperyMask is set. Otherwise FALSE is returned.
297 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.
298 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.
307 return (BOOLEAN
) ((PcdGet8(PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED
) != 0);
312 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.
314 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of
315 PcdDebugProperyMask is set. Otherwise FALSE is returned.
317 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.
318 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.
323 DebugClearMemoryEnabled (
327 return (BOOLEAN
) ((PcdGet8(PcdDebugPropertyMask
) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED
) != 0);
331 Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.
333 This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.
335 @retval TRUE Current ErrorLevel is supported.
336 @retval FALSE Current ErrorLevel is not supported.
341 DebugPrintLevelEnabled (
342 IN CONST UINTN ErrorLevel
345 return (BOOLEAN
) ((ErrorLevel
& PcdGet32(PcdFixedDebugPrintErrorLevel
)) != 0);