]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/UefiDebugLibConOut/DebugLib.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdePkg / Library / UefiDebugLibConOut / DebugLib.c
1 /** @file
2 UEFI Debug Library that sends messages to the Console Output Device in the EFI System Table.
3
4 Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include <Uefi.h>
10
11 #include <Library/DebugLib.h>
12 #include <Library/PrintLib.h>
13 #include <Library/PcdLib.h>
14 #include <Library/BaseLib.h>
15 #include <Library/BaseMemoryLib.h>
16 #include <Library/DebugPrintErrorLevelLib.h>
17
18 //
19 // Define the maximum debug and assert message length that this library supports
20 //
21 #define MAX_DEBUG_MESSAGE_LENGTH 0x100
22
23 //
24 // VA_LIST can not initialize to NULL for all compiler, so we use this to
25 // indicate a null VA_LIST
26 //
27 VA_LIST mVaListNull;
28
29 extern BOOLEAN mPostEBS;
30 extern EFI_SYSTEM_TABLE *mDebugST;
31
32 /**
33 Prints a debug message to the debug output device if the specified error level is enabled.
34
35 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
36 GetDebugPrintErrorLevel (), then print the message specified by Format and the
37 associated variable argument list to the debug output device.
38
39 If Format is NULL, then ASSERT().
40
41 @param ErrorLevel The error level of the debug message.
42 @param Format Format string for the debug message to print.
43 @param ... A variable argument list whose contents are accessed
44 based on the format string specified by Format.
45
46 **/
47 VOID
48 EFIAPI
49 DebugPrint (
50 IN UINTN ErrorLevel,
51 IN CONST CHAR8 *Format,
52 ...
53 )
54 {
55 VA_LIST Marker;
56
57 VA_START (Marker, Format);
58 DebugVPrint (ErrorLevel, Format, Marker);
59 VA_END (Marker);
60 }
61
62 /**
63 Prints a debug message to the debug output device if the specified
64 error level is enabled base on Null-terminated format string and a
65 VA_LIST argument list or a BASE_LIST argument list.
66
67 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
68 GetDebugPrintErrorLevel (), then print the message specified by Format and
69 the associated variable argument list to the debug output device.
70
71 If Format is NULL, then ASSERT().
72
73 @param ErrorLevel The error level of the debug message.
74 @param Format Format string for the debug message to print.
75 @param VaListMarker VA_LIST marker for the variable argument list.
76 @param BaseListMarker BASE_LIST marker for the variable argument list.
77
78 **/
79 VOID
80 DebugPrintMarker (
81 IN UINTN ErrorLevel,
82 IN CONST CHAR8 *Format,
83 IN VA_LIST VaListMarker,
84 IN BASE_LIST BaseListMarker
85 )
86 {
87 CHAR16 Buffer[MAX_DEBUG_MESSAGE_LENGTH];
88
89 if (!mPostEBS) {
90 //
91 // If Format is NULL, then ASSERT().
92 //
93 ASSERT (Format != NULL);
94
95 //
96 // Check driver debug mask value and global mask
97 //
98 if ((ErrorLevel & GetDebugPrintErrorLevel ()) == 0) {
99 return;
100 }
101
102 //
103 // Convert the DEBUG() message to a Unicode String
104 //
105 if (BaseListMarker == NULL) {
106 UnicodeVSPrintAsciiFormat (Buffer, sizeof (Buffer), Format, VaListMarker);
107 } else {
108 UnicodeBSPrintAsciiFormat (Buffer, sizeof (Buffer), Format, BaseListMarker);
109 }
110
111 //
112 // Send the print string to the Console Output device
113 //
114 if ((mDebugST != NULL) && (mDebugST->ConOut != NULL)) {
115 mDebugST->ConOut->OutputString (mDebugST->ConOut, Buffer);
116 }
117 }
118 }
119
120 /**
121 Prints a debug message to the debug output device if the specified
122 error level is enabled.
123
124 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
125 GetDebugPrintErrorLevel (), then print the message specified by Format and
126 the associated variable argument list to the debug output device.
127
128 If Format is NULL, then ASSERT().
129
130 @param ErrorLevel The error level of the debug message.
131 @param Format Format string for the debug message to print.
132 @param VaListMarker VA_LIST marker for the variable argument list.
133
134 **/
135 VOID
136 EFIAPI
137 DebugVPrint (
138 IN UINTN ErrorLevel,
139 IN CONST CHAR8 *Format,
140 IN VA_LIST VaListMarker
141 )
142 {
143 DebugPrintMarker (ErrorLevel, Format, VaListMarker, NULL);
144 }
145
146 /**
147 Prints a debug message to the debug output device if the specified
148 error level is enabled.
149 This function use BASE_LIST which would provide a more compatible
150 service than VA_LIST.
151
152 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
153 GetDebugPrintErrorLevel (), then print the message specified by Format and
154 the associated variable argument list to the debug output device.
155
156 If Format is NULL, then ASSERT().
157
158 @param ErrorLevel The error level of the debug message.
159 @param Format Format string for the debug message to print.
160 @param BaseListMarker BASE_LIST marker for the variable argument list.
161
162 **/
163 VOID
164 EFIAPI
165 DebugBPrint (
166 IN UINTN ErrorLevel,
167 IN CONST CHAR8 *Format,
168 IN BASE_LIST BaseListMarker
169 )
170 {
171 DebugPrintMarker (ErrorLevel, Format, mVaListNull, BaseListMarker);
172 }
173
174 /**
175 Prints an assert message containing a filename, line number, and description.
176 This may be followed by a breakpoint or a dead loop.
177
178 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"
179 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of
180 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if
181 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then
182 CpuDeadLoop() is called. If neither of these bits are set, then this function
183 returns immediately after the message is printed to the debug output device.
184 DebugAssert() must actively prevent recursion. If DebugAssert() is called while
185 processing another DebugAssert(), then DebugAssert() must return immediately.
186
187 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.
188 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.
189
190 @param FileName The pointer to the name of the source file that generated
191 the assert condition.
192 @param LineNumber The line number in the source file that generated the
193 assert condition
194 @param Description The pointer to the description of the assert condition.
195
196 **/
197 VOID
198 EFIAPI
199 DebugAssert (
200 IN CONST CHAR8 *FileName,
201 IN UINTN LineNumber,
202 IN CONST CHAR8 *Description
203 )
204 {
205 CHAR16 Buffer[MAX_DEBUG_MESSAGE_LENGTH];
206
207 if (!mPostEBS) {
208 //
209 // Generate the ASSERT() message in Unicode format
210 //
211 UnicodeSPrintAsciiFormat (
212 Buffer,
213 sizeof (Buffer),
214 "ASSERT [%a] %a(%d): %a\n",
215 gEfiCallerBaseName,
216 FileName,
217 LineNumber,
218 Description
219 );
220
221 //
222 // Send the print string to the Console Output device
223 //
224 if ((mDebugST != NULL) && (mDebugST->ConOut != NULL)) {
225 mDebugST->ConOut->OutputString (mDebugST->ConOut, Buffer);
226 }
227
228 //
229 // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings
230 //
231 if ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED) != 0) {
232 CpuBreakpoint ();
233 } else if ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED) != 0) {
234 CpuDeadLoop ();
235 }
236 }
237 }
238
239 /**
240 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.
241
242 This function fills Length bytes of Buffer with the value specified by
243 PcdDebugClearMemoryValue, and returns Buffer.
244
245 If Buffer is NULL, then ASSERT().
246 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
247
248 @param Buffer The pointer to the target buffer to be filled with PcdDebugClearMemoryValue.
249 @param Length The number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.
250
251 @return Buffer The pointer to the target buffer filled with PcdDebugClearMemoryValue.
252
253 **/
254 VOID *
255 EFIAPI
256 DebugClearMemory (
257 OUT VOID *Buffer,
258 IN UINTN Length
259 )
260 {
261 //
262 // If Buffer is NULL, then ASSERT().
263 //
264 ASSERT (Buffer != NULL);
265
266 //
267 // SetMem() checks for the the ASSERT() condition on Length and returns Buffer
268 //
269 return SetMem (Buffer, Length, PcdGet8 (PcdDebugClearMemoryValue));
270 }
271
272 /**
273 Returns TRUE if ASSERT() macros are enabled.
274
275 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of
276 PcdDebugProperyMask is set. Otherwise FALSE is returned.
277
278 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.
279 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.
280
281 **/
282 BOOLEAN
283 EFIAPI
284 DebugAssertEnabled (
285 VOID
286 )
287 {
288 return (BOOLEAN)((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) != 0);
289 }
290
291 /**
292 Returns TRUE if DEBUG() macros are enabled.
293
294 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of
295 PcdDebugProperyMask is set. Otherwise FALSE is returned.
296
297 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.
298 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.
299
300 **/
301 BOOLEAN
302 EFIAPI
303 DebugPrintEnabled (
304 VOID
305 )
306 {
307 return (BOOLEAN)((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED) != 0);
308 }
309
310 /**
311 Returns TRUE if DEBUG_CODE() macros are enabled.
312
313 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of
314 PcdDebugProperyMask is set. Otherwise FALSE is returned.
315
316 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.
317 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.
318
319 **/
320 BOOLEAN
321 EFIAPI
322 DebugCodeEnabled (
323 VOID
324 )
325 {
326 return (BOOLEAN)((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED) != 0);
327 }
328
329 /**
330 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.
331
332 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of
333 PcdDebugProperyMask is set. Otherwise FALSE is returned.
334
335 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.
336 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.
337
338 **/
339 BOOLEAN
340 EFIAPI
341 DebugClearMemoryEnabled (
342 VOID
343 )
344 {
345 return (BOOLEAN)((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED) != 0);
346 }
347
348 /**
349 Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.
350
351 This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.
352
353 @retval TRUE Current ErrorLevel is supported.
354 @retval FALSE Current ErrorLevel is not supported.
355
356 **/
357 BOOLEAN
358 EFIAPI
359 DebugPrintLevelEnabled (
360 IN CONST UINTN ErrorLevel
361 )
362 {
363 return (BOOLEAN)((ErrorLevel & PcdGet32 (PcdFixedDebugPrintErrorLevel)) != 0);
364 }