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