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