2 UEFI Debug Library that sends messages to EFI_DEBUGPORT_PROTOCOL.Write.
4 Copyright (c) 2015, 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 Send message to DebugPort Protocol.
43 If mDebugPort is NULL, i.e. EFI_DEBUGPORT_PROTOCOL is not located,
44 EFI_DEBUGPORT_PROTOCOL is located first.
45 Then, Buffer is sent via EFI_DEBUGPORT_PROTOCOL.Write.
47 @param Buffer The message to be sent.
48 @param BufferLength The byte length of Buffer.
51 UefiDebugLibDebugPortProtocolWrite (
52 IN CONST CHAR8
*Buffer
,
60 // If mDebugPort is NULL, initialize first.
62 if (mDebugPort
== NULL
) {
63 Status
= gBS
->LocateProtocol (&gEfiDebugPortProtocolGuid
, NULL
, (VOID
**)&mDebugPort
);
64 if (EFI_ERROR (Status
)) {
68 mDebugPort
->Reset (mDebugPort
);
72 // EFI_DEBUGPORT_PROTOCOL.Write is called until all message is sent.
74 while (BufferLength
> 0) {
75 Length
= BufferLength
;
77 Status
= mDebugPort
->Write (mDebugPort
, WRITE_TIMEOUT
, &Length
, (VOID
*) Buffer
);
78 if (EFI_ERROR (Status
) || BufferLength
< Length
) {
83 BufferLength
-= Length
;
88 Prints a debug message to the debug output device if the specified error level is enabled.
90 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
91 GetDebugPrintErrorLevel (), then print the message specified by Format and the
92 associated variable argument list to the debug output device.
94 If Format is NULL, then ASSERT().
96 @param ErrorLevel The error level of the debug message.
97 @param Format Format string for the debug message to print.
98 @param ... A variable argument list whose contents are accessed
99 based on the format string specified by Format.
106 IN CONST CHAR8
*Format
,
110 CHAR8 Buffer
[MAX_DEBUG_MESSAGE_LENGTH
];
114 // If Format is NULL, then ASSERT().
116 ASSERT (Format
!= NULL
);
119 // Check driver debug mask value and global mask
121 if ((ErrorLevel
& GetDebugPrintErrorLevel ()) == 0) {
126 // Convert the DEBUG() message to an ASCII String
128 VA_START (Marker
, Format
);
129 AsciiVSPrint (Buffer
, sizeof (Buffer
), Format
, Marker
);
133 // Send the print string to EFI_DEBUGPORT_PROTOCOL.Write.
135 UefiDebugLibDebugPortProtocolWrite (Buffer
, AsciiStrLen (Buffer
));
140 Prints an assert message containing a filename, line number, and description.
141 This may be followed by a breakpoint or a dead loop.
143 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"
144 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of
145 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if
146 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then
147 CpuDeadLoop() is called. If neither of these bits are set, then this function
148 returns immediately after the message is printed to the debug output device.
149 DebugAssert() must actively prevent recursion. If DebugAssert() is called while
150 processing another DebugAssert(), then DebugAssert() must return immediately.
152 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.
153 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.
155 @param FileName The pointer to the name of the source file that generated
156 the assert condition.
157 @param LineNumber The line number in the source file that generated the
159 @param Description The pointer to the description of the assert condition.
165 IN CONST CHAR8
*FileName
,
167 IN CONST CHAR8
*Description
170 CHAR8 Buffer
[MAX_DEBUG_MESSAGE_LENGTH
];
173 // Generate the ASSERT() message in ASCII format
178 "ASSERT %a(%d): %a\n",
185 // Send the print string to EFI_DEBUGPORT_PROTOCOL.Write.
187 UefiDebugLibDebugPortProtocolWrite (Buffer
, AsciiStrLen (Buffer
));
190 // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings
192 if ((PcdGet8(PcdDebugPropertyMask
) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED
) != 0) {
194 } else if ((PcdGet8(PcdDebugPropertyMask
) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED
) != 0) {
201 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.
203 This function fills Length bytes of Buffer with the value specified by
204 PcdDebugClearMemoryValue, and returns Buffer.
206 If Buffer is NULL, then ASSERT().
207 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
209 @param Buffer The pointer to the target buffer to be filled with PcdDebugClearMemoryValue.
210 @param Length The number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.
212 @return Buffer The pointer to the target buffer filled with PcdDebugClearMemoryValue.
223 // If Buffer is NULL, then ASSERT().
225 ASSERT (Buffer
!= NULL
);
228 // SetMem() checks for the the ASSERT() condition on Length and returns Buffer
230 return SetMem (Buffer
, Length
, PcdGet8(PcdDebugClearMemoryValue
));
235 Returns TRUE if ASSERT() macros are enabled.
237 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of
238 PcdDebugProperyMask is set. Otherwise FALSE is returned.
240 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.
241 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.
250 return (BOOLEAN
) ((PcdGet8(PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED
) != 0);
255 Returns TRUE if DEBUG() macros are enabled.
257 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of
258 PcdDebugProperyMask is set. Otherwise FALSE is returned.
260 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.
261 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.
270 return (BOOLEAN
) ((PcdGet8(PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED
) != 0);
275 Returns TRUE if DEBUG_CODE() macros are enabled.
277 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of
278 PcdDebugProperyMask is set. Otherwise FALSE is returned.
280 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.
281 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.
290 return (BOOLEAN
) ((PcdGet8(PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED
) != 0);
295 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.
297 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of
298 PcdDebugProperyMask is set. Otherwise FALSE is returned.
300 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.
301 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.
306 DebugClearMemoryEnabled (
310 return (BOOLEAN
) ((PcdGet8(PcdDebugPropertyMask
) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED
) != 0);
314 Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.
316 This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.
318 @retval TRUE Current ErrorLevel is supported.
319 @retval FALSE Current ErrorLevel is not supported.
324 DebugPrintLevelEnabled (
325 IN CONST UINTN ErrorLevel
328 return (BOOLEAN
) ((ErrorLevel
& PcdGet32(PcdFixedDebugPrintErrorLevel
)) != 0);