]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/UefiDebugLibDebugPortProtocol/DebugLib.c
MdePkg/DxeRuntimeDebugLibSerialPort: Add new APIs
[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
9095d37b
LG
4 Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>\r
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
40/**\r
41 Send message to DebugPort Protocol.\r
42\r
43 If mDebugPort is NULL, i.e. EFI_DEBUGPORT_PROTOCOL is not located,\r
44 EFI_DEBUGPORT_PROTOCOL is located first.\r
45 Then, Buffer is sent via EFI_DEBUGPORT_PROTOCOL.Write.\r
46\r
47 @param Buffer The message to be sent.\r
48 @param BufferLength The byte length of Buffer.\r
49**/\r
50VOID\r
51UefiDebugLibDebugPortProtocolWrite (\r
52 IN CONST CHAR8 *Buffer,\r
53 IN UINTN BufferLength\r
54 )\r
55{\r
56 UINTN Length;\r
57 EFI_STATUS Status;\r
58\r
59 //\r
60 // If mDebugPort is NULL, initialize first.\r
61 //\r
62 if (mDebugPort == NULL) {\r
63 Status = gBS->LocateProtocol (&gEfiDebugPortProtocolGuid, NULL, (VOID **)&mDebugPort);\r
64 if (EFI_ERROR (Status)) {\r
65 return;\r
66 }\r
67\r
68 mDebugPort->Reset (mDebugPort);\r
69 }\r
70\r
71 //\r
72 // EFI_DEBUGPORT_PROTOCOL.Write is called until all message is sent.\r
73 //\r
74 while (BufferLength > 0) {\r
75 Length = BufferLength;\r
76\r
77 Status = mDebugPort->Write (mDebugPort, WRITE_TIMEOUT, &Length, (VOID *) Buffer);\r
78 if (EFI_ERROR (Status) || BufferLength < Length) {\r
79 break;\r
80 }\r
81\r
82 Buffer += Length;\r
83 BufferLength -= Length;\r
84 }\r
85}\r
86\r
87/**\r
88 Prints a debug message to the debug output device if the specified error level is enabled.\r
89\r
9095d37b
LG
90 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function\r
91 GetDebugPrintErrorLevel (), then print the message specified by Format and the\r
b29910d6
MM
92 associated variable argument list to the debug output device.\r
93\r
94 If Format is NULL, then ASSERT().\r
95\r
96 @param ErrorLevel The error level of the debug message.\r
97 @param Format Format string for the debug message to print.\r
9095d37b 98 @param ... A variable argument list whose contents are accessed\r
b29910d6
MM
99 based on the format string specified by Format.\r
100\r
101**/\r
102VOID\r
103EFIAPI\r
104DebugPrint (\r
105 IN UINTN ErrorLevel,\r
106 IN CONST CHAR8 *Format,\r
107 ...\r
108 )\r
109{\r
110 CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];\r
111 VA_LIST Marker;\r
112\r
113 //\r
114 // If Format is NULL, then ASSERT().\r
115 //\r
116 ASSERT (Format != NULL);\r
117\r
118 //\r
119 // Check driver debug mask value and global mask\r
120 //\r
121 if ((ErrorLevel & GetDebugPrintErrorLevel ()) == 0) {\r
122 return;\r
123 }\r
124\r
125 //\r
126 // Convert the DEBUG() message to an ASCII String\r
127 //\r
128 VA_START (Marker, Format);\r
129 AsciiVSPrint (Buffer, sizeof (Buffer), Format, Marker);\r
130 VA_END (Marker);\r
131\r
132 //\r
133 // Send the print string to EFI_DEBUGPORT_PROTOCOL.Write.\r
134 //\r
135 UefiDebugLibDebugPortProtocolWrite (Buffer, AsciiStrLen (Buffer));\r
136}\r
137\r
138\r
139/**\r
9095d37b 140 Prints an assert message containing a filename, line number, and description.\r
b29910d6
MM
141 This may be followed by a breakpoint or a dead loop.\r
142\r
143 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"\r
9095d37b
LG
144 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of\r
145 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if\r
146 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then\r
147 CpuDeadLoop() is called. If neither of these bits are set, then this function\r
b29910d6
MM
148 returns immediately after the message is printed to the debug output device.\r
149 DebugAssert() must actively prevent recursion. If DebugAssert() is called while\r
150 processing another DebugAssert(), then DebugAssert() must return immediately.\r
151\r
152 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.\r
153 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.\r
154\r
9095d37b 155 @param FileName The pointer to the name of the source file that generated\r
b29910d6 156 the assert condition.\r
9095d37b 157 @param LineNumber The line number in the source file that generated the\r
b29910d6
MM
158 assert condition\r
159 @param Description The pointer to the description of the assert condition.\r
160\r
161**/\r
162VOID\r
163EFIAPI\r
164DebugAssert (\r
165 IN CONST CHAR8 *FileName,\r
166 IN UINTN LineNumber,\r
167 IN CONST CHAR8 *Description\r
168 )\r
169{\r
170 CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];\r
171\r
172 //\r
173 // Generate the ASSERT() message in ASCII format\r
174 //\r
175 AsciiSPrint (\r
9095d37b
LG
176 Buffer,\r
177 sizeof (Buffer),\r
aa0a5149
BA
178 "ASSERT [%a] %a(%d): %a\n",\r
179 gEfiCallerBaseName,\r
9095d37b
LG
180 FileName,\r
181 LineNumber,\r
b29910d6
MM
182 Description\r
183 );\r
184\r
185 //\r
186 // Send the print string to EFI_DEBUGPORT_PROTOCOL.Write.\r
187 //\r
188 UefiDebugLibDebugPortProtocolWrite (Buffer, AsciiStrLen (Buffer));\r
189\r
190 //\r
191 // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings\r
192 //\r
193 if ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED) != 0) {\r
194 CpuBreakpoint ();\r
195 } else if ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED) != 0) {\r
196 CpuDeadLoop ();\r
197 }\r
198}\r
199\r
200\r
201/**\r
202 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.\r
203\r
9095d37b 204 This function fills Length bytes of Buffer with the value specified by\r
b29910d6
MM
205 PcdDebugClearMemoryValue, and returns Buffer.\r
206\r
207 If Buffer is NULL, then ASSERT().\r
9095d37b 208 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
b29910d6
MM
209\r
210 @param Buffer The pointer to the target buffer to be filled with PcdDebugClearMemoryValue.\r
9095d37b 211 @param Length The number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.\r
b29910d6
MM
212\r
213 @return Buffer The pointer to the target buffer filled with PcdDebugClearMemoryValue.\r
214\r
215**/\r
216VOID *\r
217EFIAPI\r
218DebugClearMemory (\r
219 OUT VOID *Buffer,\r
220 IN UINTN Length\r
221 )\r
222{\r
223 //\r
224 // If Buffer is NULL, then ASSERT().\r
225 //\r
226 ASSERT (Buffer != NULL);\r
227\r
228 //\r
229 // SetMem() checks for the the ASSERT() condition on Length and returns Buffer\r
230 //\r
231 return SetMem (Buffer, Length, PcdGet8(PcdDebugClearMemoryValue));\r
232}\r
233\r
234\r
235/**\r
236 Returns TRUE if ASSERT() macros are enabled.\r
237\r
9095d37b 238 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of\r
b29910d6
MM
239 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
240\r
241 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.\r
242 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.\r
243\r
244**/\r
245BOOLEAN\r
246EFIAPI\r
247DebugAssertEnabled (\r
248 VOID\r
249 )\r
250{\r
251 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) != 0);\r
252}\r
253\r
254\r
9095d37b 255/**\r
b29910d6
MM
256 Returns TRUE if DEBUG() macros are enabled.\r
257\r
9095d37b 258 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of\r
b29910d6
MM
259 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
260\r
261 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.\r
262 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.\r
263\r
264**/\r
265BOOLEAN\r
266EFIAPI\r
267DebugPrintEnabled (\r
268 VOID\r
269 )\r
270{\r
271 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED) != 0);\r
272}\r
273\r
274\r
9095d37b 275/**\r
b29910d6
MM
276 Returns TRUE if DEBUG_CODE() macros are enabled.\r
277\r
9095d37b 278 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of\r
b29910d6
MM
279 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
280\r
281 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.\r
282 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.\r
283\r
284**/\r
285BOOLEAN\r
286EFIAPI\r
287DebugCodeEnabled (\r
288 VOID\r
289 )\r
290{\r
291 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED) != 0);\r
292}\r
293\r
294\r
9095d37b 295/**\r
b29910d6
MM
296 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.\r
297\r
9095d37b 298 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of\r
b29910d6
MM
299 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
300\r
301 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.\r
302 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.\r
303\r
304**/\r
305BOOLEAN\r
306EFIAPI\r
307DebugClearMemoryEnabled (\r
308 VOID\r
309 )\r
310{\r
311 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED) != 0);\r
312}\r
313\r
314/**\r
315 Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.\r
316\r
317 This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.\r
318\r
319 @retval TRUE Current ErrorLevel is supported.\r
320 @retval FALSE Current ErrorLevel is not supported.\r
321\r
322**/\r
323BOOLEAN\r
324EFIAPI\r
325DebugPrintLevelEnabled (\r
326 IN CONST UINTN ErrorLevel\r
327 )\r
328{\r
329 return (BOOLEAN) ((ErrorLevel & PcdGet32(PcdFixedDebugPrintErrorLevel)) != 0);\r
330}\r
50e7d3d9 331\r