1 /** @file
2   UEFI Debug Library that sends messages to the Console Output Device in the EFI System Table.
3 
4   Copyright (c) 2006 - 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.
9 
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.
12 
13 **/
14 
15 #include <Uefi.h>
16 
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>
24 
25 //
26 // Define the maximum debug and assert message length that this library supports
27 //
28 #define MAX_DEBUG_MESSAGE_LENGTH  0x100
29 
30 /**
31   Prints a debug message to the debug output device if the specified error level is enabled.
32 
33   If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
34   GetDebugPrintErrorLevel (), then print the message specified by Format and the
35   associated variable argument list to the debug output device.
36 
37   If Format is NULL, then ASSERT().
38 
39   @param  ErrorLevel  The error level of the debug message.
40   @param  Format      Format string for the debug message to print.
41   @param  ...         A variable argument list whose contents are accessed
42                       based on the format string specified by Format.
43 
44 **/
45 VOID
46 EFIAPI
DebugPrint(IN UINTN ErrorLevel,IN CONST CHAR8 * Format,...)47 DebugPrint (
48   IN  UINTN        ErrorLevel,
49   IN  CONST CHAR8  *Format,
50   ...
51   )
52 {
53   CHAR16   Buffer[MAX_DEBUG_MESSAGE_LENGTH];
54   VA_LIST  Marker;
55 
56   //
57   // If Format is NULL, then ASSERT().
58   //
59   ASSERT (Format != NULL);
60 
61   //
62   // Check driver debug mask value and global mask
63   //
64   if ((ErrorLevel & GetDebugPrintErrorLevel ()) == 0) {
65     return;
66   }
67 
68   //
69   // Convert the DEBUG() message to a Unicode String
70   //
71   VA_START (Marker, Format);
72   UnicodeVSPrintAsciiFormat (Buffer, MAX_DEBUG_MESSAGE_LENGTH,  Format, Marker);
73   VA_END (Marker);
74 
75 
76   //
77   // Send the print string to the Console Output device
78   //
79   if ((gST != NULL) && (gST->ConOut != NULL)) {
80     gST->ConOut->OutputString (gST->ConOut, Buffer);
81   }
82 }
83 
84 
85 /**
86   Prints an assert message containing a filename, line number, and description.
87   This may be followed by a breakpoint or a dead loop.
88 
89   Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"
90   to the debug output device.  If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of
91   PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if
92   DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then
93   CpuDeadLoop() is called.  If neither of these bits are set, then this function
94   returns immediately after the message is printed to the debug output device.
95   DebugAssert() must actively prevent recursion.  If DebugAssert() is called while
96   processing another DebugAssert(), then DebugAssert() must return immediately.
97 
98   If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.
99   If Description is NULL, then a <Description> string of "(NULL) Description" is printed.
100 
101   @param  FileName     The pointer to the name of the source file that generated
102                        the assert condition.
103   @param  LineNumber   The line number in the source file that generated the
104                        assert condition
105   @param  Description  The pointer to the description of the assert condition.
106 
107 **/
108 VOID
109 EFIAPI
DebugAssert(IN CONST CHAR8 * FileName,IN UINTN LineNumber,IN CONST CHAR8 * Description)110 DebugAssert (
111   IN CONST CHAR8  *FileName,
112   IN UINTN        LineNumber,
113   IN CONST CHAR8  *Description
114   )
115 {
116   CHAR16  Buffer[MAX_DEBUG_MESSAGE_LENGTH];
117 
118   //
119   // Generate the ASSERT() message in Unicode format
120   //
121   UnicodeSPrintAsciiFormat (
122     Buffer,
123     sizeof (Buffer),
124     "ASSERT %a(%d): %a\n",
125     FileName,
126     LineNumber,
127     Description
128     );
129 
130   //
131   // Send the print string to the Console Output device
132   //
133   if ((gST != NULL) && (gST->ConOut != NULL)) {
134     gST->ConOut->OutputString (gST->ConOut, Buffer);
135   }
136 
137   //
138   // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings
139   //
140   if ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED) != 0) {
141     CpuBreakpoint ();
142   } else if ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED) != 0) {
143     CpuDeadLoop ();
144   }
145 }
146 
147 
148 /**
149   Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.
150 
151   This function fills Length bytes of Buffer with the value specified by
152   PcdDebugClearMemoryValue, and returns Buffer.
153 
154   If Buffer is NULL, then ASSERT().
155   If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
156 
157   @param   Buffer  The pointer to the target buffer to be filled with PcdDebugClearMemoryValue.
158   @param   Length  The number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.
159 
160   @return  Buffer  The pointer to the target buffer filled with PcdDebugClearMemoryValue.
161 
162 **/
163 VOID *
164 EFIAPI
DebugClearMemory(OUT VOID * Buffer,IN UINTN Length)165 DebugClearMemory (
166   OUT VOID  *Buffer,
167   IN UINTN  Length
168   )
169 {
170   //
171   // If Buffer is NULL, then ASSERT().
172   //
173   ASSERT (Buffer != NULL);
174 
175   //
176   // SetMem() checks for the the ASSERT() condition on Length and returns Buffer
177   //
178   return SetMem (Buffer, Length, PcdGet8(PcdDebugClearMemoryValue));
179 }
180 
181 
182 /**
183   Returns TRUE if ASSERT() macros are enabled.
184 
185   This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of
186   PcdDebugProperyMask is set.  Otherwise FALSE is returned.
187 
188   @retval  TRUE    The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.
189   @retval  FALSE   The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.
190 
191 **/
192 BOOLEAN
193 EFIAPI
DebugAssertEnabled(VOID)194 DebugAssertEnabled (
195   VOID
196   )
197 {
198   return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) != 0);
199 }
200 
201 
202 /**
203   Returns TRUE if DEBUG() macros are enabled.
204 
205   This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of
206   PcdDebugProperyMask is set.  Otherwise FALSE is returned.
207 
208   @retval  TRUE    The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.
209   @retval  FALSE   The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.
210 
211 **/
212 BOOLEAN
213 EFIAPI
DebugPrintEnabled(VOID)214 DebugPrintEnabled (
215   VOID
216   )
217 {
218   return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED) != 0);
219 }
220 
221 
222 /**
223   Returns TRUE if DEBUG_CODE() macros are enabled.
224 
225   This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of
226   PcdDebugProperyMask is set.  Otherwise FALSE is returned.
227 
228   @retval  TRUE    The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.
229   @retval  FALSE   The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.
230 
231 **/
232 BOOLEAN
233 EFIAPI
DebugCodeEnabled(VOID)234 DebugCodeEnabled (
235   VOID
236   )
237 {
238   return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED) != 0);
239 }
240 
241 
242 /**
243   Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.
244 
245   This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of
246   PcdDebugProperyMask is set.  Otherwise FALSE is returned.
247 
248   @retval  TRUE    The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.
249   @retval  FALSE   The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.
250 
251 **/
252 BOOLEAN
253 EFIAPI
DebugClearMemoryEnabled(VOID)254 DebugClearMemoryEnabled (
255   VOID
256   )
257 {
258   return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED) != 0);
259 }
260 
261 /**
262   Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.
263 
264   This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.
265 
266   @retval  TRUE    Current ErrorLevel is supported.
267   @retval  FALSE   Current ErrorLevel is not supported.
268 
269 **/
270 BOOLEAN
271 EFIAPI
DebugPrintLevelEnabled(IN CONST UINTN ErrorLevel)272 DebugPrintLevelEnabled (
273   IN  CONST UINTN        ErrorLevel
274   )
275 {
276   return (BOOLEAN) ((ErrorLevel & PcdGet32(PcdFixedDebugPrintErrorLevel)) != 0);
277 }
278