]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdePkg/Library/UefiDebugLibDebugPortProtocol/DebugLib.c
MdePkg: Apply uncrustify changes
[mirror_edk2.git] / MdePkg / Library / UefiDebugLibDebugPortProtocol / DebugLib.c
... / ...
CommitLineData
1/** @file\r
2 UEFI Debug Library that sends messages to EFI_DEBUGPORT_PROTOCOL.Write.\r
3\r
4 Copyright (c) 2015 - 2019, Intel Corporation. All rights reserved.<BR>\r
5 SPDX-License-Identifier: BSD-2-Clause-Patent\r
6\r
7**/\r
8\r
9#include <Uefi.h>\r
10\r
11#include <Library/DebugLib.h>\r
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
21// Define the maximum debug and assert message length that this library supports\r
22//\r
23#define MAX_DEBUG_MESSAGE_LENGTH 0x100\r
24\r
25//\r
26// Define the timeout for EFI_DEBUGPORT_PROTOCOL.Write\r
27//\r
28#define WRITE_TIMEOUT 1000\r
29\r
30EFI_DEBUGPORT_PROTOCOL *mDebugPort = NULL;\r
31\r
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
36VA_LIST mVaListNull;\r
37\r
38extern BOOLEAN mPostEBS;\r
39extern EFI_BOOT_SERVICES *mDebugBS;\r
40\r
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
57 UINTN Length;\r
58 EFI_STATUS Status;\r
59\r
60 if (!mPostEBS) {\r
61 //\r
62 // If mDebugPort is NULL, initialize first.\r
63 //\r
64 if (mDebugPort == NULL) {\r
65 Status = mDebugBS->LocateProtocol (&gEfiDebugPortProtocolGuid, NULL, (VOID **)&mDebugPort);\r
66 if (EFI_ERROR (Status)) {\r
67 return;\r
68 }\r
69\r
70 mDebugPort->Reset (mDebugPort);\r
71 }\r
72\r
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
78\r
79 Status = mDebugPort->Write (mDebugPort, WRITE_TIMEOUT, &Length, (VOID *)Buffer);\r
80 if (EFI_ERROR (Status) || (BufferLength < Length)) {\r
81 break;\r
82 }\r
83\r
84 Buffer += Length;\r
85 BufferLength -= Length;\r
86 }\r
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
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
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
101 @param ... A variable argument list whose contents are accessed\r
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
112{\r
113 VA_LIST Marker;\r
114\r
115 VA_START (Marker, Format);\r
116 DebugVPrint (ErrorLevel, Format, Marker);\r
117 VA_END (Marker);\r
118}\r
119\r
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
139 IN UINTN ErrorLevel,\r
140 IN CONST CHAR8 *Format,\r
141 IN VA_LIST VaListMarker,\r
142 IN BASE_LIST BaseListMarker\r
143 )\r
144{\r
145 CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];\r
146\r
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
159\r
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
168\r
169 //\r
170 // Send the print string to EFI_DEBUGPORT_PROTOCOL.Write.\r
171 //\r
172 UefiDebugLibDebugPortProtocolWrite (Buffer, AsciiStrLen (Buffer));\r
173 }\r
174}\r
175\r
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
194 IN UINTN ErrorLevel,\r
195 IN CONST CHAR8 *Format,\r
196 IN VA_LIST VaListMarker\r
197 )\r
198{\r
199 DebugPrintMarker (ErrorLevel, Format, VaListMarker, NULL);\r
200}\r
201\r
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
222 IN UINTN ErrorLevel,\r
223 IN CONST CHAR8 *Format,\r
224 IN BASE_LIST BaseListMarker\r
225 )\r
226{\r
227 DebugPrintMarker (ErrorLevel, Format, mVaListNull, BaseListMarker);\r
228}\r
229\r
230/**\r
231 Prints an assert message containing a filename, line number, and description.\r
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
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
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
246 @param FileName The pointer to the name of the source file that generated\r
247 the assert condition.\r
248 @param LineNumber The line number in the source file that generated the\r
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
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
285 if ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED) != 0) {\r
286 CpuBreakpoint ();\r
287 } else if ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED) != 0) {\r
288 CpuDeadLoop ();\r
289 }\r
290 }\r
291}\r
292\r
293/**\r
294 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.\r
295\r
296 This function fills Length bytes of Buffer with the value specified by\r
297 PcdDebugClearMemoryValue, and returns Buffer.\r
298\r
299 If Buffer is NULL, then ASSERT().\r
300 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
301\r
302 @param Buffer The pointer to the target buffer to be filled with PcdDebugClearMemoryValue.\r
303 @param Length The number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.\r
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
323 return SetMem (Buffer, Length, PcdGet8 (PcdDebugClearMemoryValue));\r
324}\r
325\r
326/**\r
327 Returns TRUE if ASSERT() macros are enabled.\r
328\r
329 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of\r
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
342 return (BOOLEAN)((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) != 0);\r
343}\r
344\r
345/**\r
346 Returns TRUE if DEBUG() macros are enabled.\r
347\r
348 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of\r
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
361 return (BOOLEAN)((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED) != 0);\r
362}\r
363\r
364/**\r
365 Returns TRUE if DEBUG_CODE() macros are enabled.\r
366\r
367 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of\r
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
380 return (BOOLEAN)((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED) != 0);\r
381}\r
382\r
383/**\r
384 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.\r
385\r
386 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of\r
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
399 return (BOOLEAN)((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED) != 0);\r
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
414 IN CONST UINTN ErrorLevel\r
415 )\r
416{\r
417 return (BOOLEAN)((ErrorLevel & PcdGet32 (PcdFixedDebugPrintErrorLevel)) != 0);\r
418}\r