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