]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/UefiDebugLibDebugPortProtocol/DebugLib.c
MdeModulePkg: Replace BSD License with BSD+Patent License
[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
9095d37b
LG
5 This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php.\r
b29910d6 9\r
9095d37b
LG
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
b29910d6
MM
12\r
13**/\r
14\r
15#include <Uefi.h>\r
16\r
17#include <Library/DebugLib.h>\r
18#include <Library/UefiBootServicesTableLib.h>\r
19#include <Library/PrintLib.h>\r
20#include <Library/PcdLib.h>\r
21#include <Library/BaseLib.h>\r
22#include <Library/BaseMemoryLib.h>\r
23#include <Library/DebugPrintErrorLevelLib.h>\r
24\r
25#include <Protocol/DebugPort.h>\r
26\r
27//\r
9095d37b 28// Define the maximum debug and assert message length that this library supports\r
b29910d6
MM
29//\r
30#define MAX_DEBUG_MESSAGE_LENGTH 0x100\r
31\r
32//\r
9095d37b 33// Define the timeout for EFI_DEBUGPORT_PROTOCOL.Write\r
b29910d6
MM
34//\r
35#define WRITE_TIMEOUT 1000\r
36\r
37\r
38EFI_DEBUGPORT_PROTOCOL *mDebugPort = NULL;\r
39\r
27d86bc1
BB
40//\r
41// VA_LIST can not initialize to NULL for all compiler, so we use this to\r
42// indicate a null VA_LIST\r
43//\r
44VA_LIST mVaListNull;\r
45\r
b29910d6
MM
46/**\r
47 Send message to DebugPort Protocol.\r
48\r
49 If mDebugPort is NULL, i.e. EFI_DEBUGPORT_PROTOCOL is not located,\r
50 EFI_DEBUGPORT_PROTOCOL is located first.\r
51 Then, Buffer is sent via EFI_DEBUGPORT_PROTOCOL.Write.\r
52\r
53 @param Buffer The message to be sent.\r
54 @param BufferLength The byte length of Buffer.\r
55**/\r
56VOID\r
57UefiDebugLibDebugPortProtocolWrite (\r
58 IN CONST CHAR8 *Buffer,\r
59 IN UINTN BufferLength\r
60 )\r
61{\r
62 UINTN Length;\r
63 EFI_STATUS Status;\r
64\r
65 //\r
66 // If mDebugPort is NULL, initialize first.\r
67 //\r
68 if (mDebugPort == NULL) {\r
69 Status = gBS->LocateProtocol (&gEfiDebugPortProtocolGuid, NULL, (VOID **)&mDebugPort);\r
70 if (EFI_ERROR (Status)) {\r
71 return;\r
72 }\r
73\r
74 mDebugPort->Reset (mDebugPort);\r
75 }\r
76\r
77 //\r
78 // EFI_DEBUGPORT_PROTOCOL.Write is called until all message is sent.\r
79 //\r
80 while (BufferLength > 0) {\r
81 Length = BufferLength;\r
82\r
83 Status = mDebugPort->Write (mDebugPort, WRITE_TIMEOUT, &Length, (VOID *) Buffer);\r
84 if (EFI_ERROR (Status) || BufferLength < Length) {\r
85 break;\r
86 }\r
87\r
88 Buffer += Length;\r
89 BufferLength -= Length;\r
90 }\r
91}\r
92\r
93/**\r
94 Prints a debug message to the debug output device if the specified error level is enabled.\r
95\r
9095d37b
LG
96 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function\r
97 GetDebugPrintErrorLevel (), then print the message specified by Format and the\r
b29910d6
MM
98 associated variable argument list to the debug output device.\r
99\r
100 If Format is NULL, then ASSERT().\r
101\r
102 @param ErrorLevel The error level of the debug message.\r
103 @param Format Format string for the debug message to print.\r
9095d37b 104 @param ... A variable argument list whose contents are accessed\r
b29910d6
MM
105 based on the format string specified by Format.\r
106\r
107**/\r
108VOID\r
109EFIAPI\r
110DebugPrint (\r
111 IN UINTN ErrorLevel,\r
112 IN CONST CHAR8 *Format,\r
113 ...\r
114 )\r
27d86bc1
BB
115{\r
116 VA_LIST Marker;\r
117\r
118 VA_START (Marker, Format);\r
119 DebugVPrint (ErrorLevel, Format, Marker);\r
120 VA_END (Marker);\r
121}\r
122\r
123\r
124/**\r
125 Prints a debug message to the debug output device if the specified\r
126 error level is enabled base on Null-terminated format string and a\r
127 VA_LIST argument list or a BASE_LIST argument list.\r
128\r
129 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function\r
130 GetDebugPrintErrorLevel (), then print the message specified by Format and\r
131 the associated variable argument list to the debug output device.\r
132\r
133 If Format is NULL, then ASSERT().\r
134\r
135 @param ErrorLevel The error level of the debug message.\r
136 @param Format Format string for the debug message to print.\r
137 @param VaListMarker VA_LIST marker for the variable argument list.\r
138 @param BaseListMarker BASE_LIST marker for the variable argument list.\r
139\r
140**/\r
141VOID\r
142DebugPrintMarker (\r
143 IN UINTN ErrorLevel,\r
144 IN CONST CHAR8 *Format,\r
145 IN VA_LIST VaListMarker,\r
146 IN BASE_LIST BaseListMarker\r
147 )\r
b29910d6
MM
148{\r
149 CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];\r
b29910d6
MM
150\r
151 //\r
152 // If Format is NULL, then ASSERT().\r
153 //\r
154 ASSERT (Format != NULL);\r
155\r
156 //\r
157 // Check driver debug mask value and global mask\r
158 //\r
159 if ((ErrorLevel & GetDebugPrintErrorLevel ()) == 0) {\r
160 return;\r
161 }\r
162\r
163 //\r
164 // Convert the DEBUG() message to an ASCII String\r
165 //\r
27d86bc1
BB
166 if (BaseListMarker == NULL) {\r
167 AsciiVSPrint (Buffer, sizeof (Buffer), Format, VaListMarker);\r
168 } else {\r
169 AsciiBSPrint (Buffer, sizeof (Buffer), Format, BaseListMarker);\r
170 }\r
b29910d6
MM
171\r
172 //\r
173 // Send the print string to EFI_DEBUGPORT_PROTOCOL.Write.\r
174 //\r
175 UefiDebugLibDebugPortProtocolWrite (Buffer, AsciiStrLen (Buffer));\r
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
268 //\r
269 // Generate the ASSERT() message in ASCII format\r
270 //\r
271 AsciiSPrint (\r
9095d37b
LG
272 Buffer,\r
273 sizeof (Buffer),\r
aa0a5149
BA
274 "ASSERT [%a] %a(%d): %a\n",\r
275 gEfiCallerBaseName,\r
9095d37b
LG
276 FileName,\r
277 LineNumber,\r
b29910d6
MM
278 Description\r
279 );\r
280\r
281 //\r
282 // Send the print string to EFI_DEBUGPORT_PROTOCOL.Write.\r
283 //\r
284 UefiDebugLibDebugPortProtocolWrite (Buffer, AsciiStrLen (Buffer));\r
285\r
286 //\r
287 // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings\r
288 //\r
289 if ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED) != 0) {\r
290 CpuBreakpoint ();\r
291 } else if ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED) != 0) {\r
292 CpuDeadLoop ();\r
293 }\r
294}\r
295\r
296\r
297/**\r
298 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.\r
299\r
9095d37b 300 This function fills Length bytes of Buffer with the value specified by\r
b29910d6
MM
301 PcdDebugClearMemoryValue, and returns Buffer.\r
302\r
303 If Buffer is NULL, then ASSERT().\r
9095d37b 304 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
b29910d6
MM
305\r
306 @param Buffer The pointer to the target buffer to be filled with PcdDebugClearMemoryValue.\r
9095d37b 307 @param Length The number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.\r
b29910d6
MM
308\r
309 @return Buffer The pointer to the target buffer filled with PcdDebugClearMemoryValue.\r
310\r
311**/\r
312VOID *\r
313EFIAPI\r
314DebugClearMemory (\r
315 OUT VOID *Buffer,\r
316 IN UINTN Length\r
317 )\r
318{\r
319 //\r
320 // If Buffer is NULL, then ASSERT().\r
321 //\r
322 ASSERT (Buffer != NULL);\r
323\r
324 //\r
325 // SetMem() checks for the the ASSERT() condition on Length and returns Buffer\r
326 //\r
327 return SetMem (Buffer, Length, PcdGet8(PcdDebugClearMemoryValue));\r
328}\r
329\r
330\r
331/**\r
332 Returns TRUE if ASSERT() macros are enabled.\r
333\r
9095d37b 334 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of\r
b29910d6
MM
335 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
336\r
337 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.\r
338 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.\r
339\r
340**/\r
341BOOLEAN\r
342EFIAPI\r
343DebugAssertEnabled (\r
344 VOID\r
345 )\r
346{\r
347 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) != 0);\r
348}\r
349\r
350\r
9095d37b 351/**\r
b29910d6
MM
352 Returns TRUE if DEBUG() macros are enabled.\r
353\r
9095d37b 354 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of\r
b29910d6
MM
355 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
356\r
357 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.\r
358 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.\r
359\r
360**/\r
361BOOLEAN\r
362EFIAPI\r
363DebugPrintEnabled (\r
364 VOID\r
365 )\r
366{\r
367 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED) != 0);\r
368}\r
369\r
370\r
9095d37b 371/**\r
b29910d6
MM
372 Returns TRUE if DEBUG_CODE() macros are enabled.\r
373\r
9095d37b 374 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of\r
b29910d6
MM
375 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
376\r
377 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.\r
378 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.\r
379\r
380**/\r
381BOOLEAN\r
382EFIAPI\r
383DebugCodeEnabled (\r
384 VOID\r
385 )\r
386{\r
387 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED) != 0);\r
388}\r
389\r
390\r
9095d37b 391/**\r
b29910d6
MM
392 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.\r
393\r
9095d37b 394 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of\r
b29910d6
MM
395 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
396\r
397 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.\r
398 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.\r
399\r
400**/\r
401BOOLEAN\r
402EFIAPI\r
403DebugClearMemoryEnabled (\r
404 VOID\r
405 )\r
406{\r
407 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED) != 0);\r
408}\r
409\r
410/**\r
411 Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.\r
412\r
413 This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.\r
414\r
415 @retval TRUE Current ErrorLevel is supported.\r
416 @retval FALSE Current ErrorLevel is not supported.\r
417\r
418**/\r
419BOOLEAN\r
420EFIAPI\r
421DebugPrintLevelEnabled (\r
422 IN CONST UINTN ErrorLevel\r
423 )\r
424{\r
425 return (BOOLEAN) ((ErrorLevel & PcdGet32(PcdFixedDebugPrintErrorLevel)) != 0);\r
426}\r
50e7d3d9 427\r