]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/UefiDebugLibDebugPortProtocol/DebugLib.c
MdePkg: Add UefiDebugLibDebugPortProtocol to output logs via DebugPort.
[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
4 Copyright (c) 2015, 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
9\r
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
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
28// Define the maximum debug and assert message length that this library supports \r
29//\r
30#define MAX_DEBUG_MESSAGE_LENGTH 0x100\r
31\r
32//\r
33// Define the timeout for EFI_DEBUGPORT_PROTOCOL.Write \r
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
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
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
98 @param ... A variable argument list whose contents are accessed \r
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
140 Prints an assert message containing a filename, line number, and description. \r
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
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
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
155 @param FileName The pointer to the name of the source file that generated \r
156 the assert condition.\r
157 @param LineNumber The line number in the source file that generated the \r
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
176 Buffer, \r
177 sizeof (Buffer), \r
178 "ASSERT %a(%d): %a\n", \r
179 FileName, \r
180 LineNumber, \r
181 Description\r
182 );\r
183\r
184 //\r
185 // Send the print string to EFI_DEBUGPORT_PROTOCOL.Write.\r
186 //\r
187 UefiDebugLibDebugPortProtocolWrite (Buffer, AsciiStrLen (Buffer));\r
188\r
189 //\r
190 // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings\r
191 //\r
192 if ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED) != 0) {\r
193 CpuBreakpoint ();\r
194 } else if ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED) != 0) {\r
195 CpuDeadLoop ();\r
196 }\r
197}\r
198\r
199\r
200/**\r
201 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.\r
202\r
203 This function fills Length bytes of Buffer with the value specified by \r
204 PcdDebugClearMemoryValue, and returns Buffer.\r
205\r
206 If Buffer is NULL, then ASSERT().\r
207 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r
208\r
209 @param Buffer The pointer to the target buffer to be filled with PcdDebugClearMemoryValue.\r
210 @param Length The number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue. \r
211\r
212 @return Buffer The pointer to the target buffer filled with PcdDebugClearMemoryValue.\r
213\r
214**/\r
215VOID *\r
216EFIAPI\r
217DebugClearMemory (\r
218 OUT VOID *Buffer,\r
219 IN UINTN Length\r
220 )\r
221{\r
222 //\r
223 // If Buffer is NULL, then ASSERT().\r
224 //\r
225 ASSERT (Buffer != NULL);\r
226\r
227 //\r
228 // SetMem() checks for the the ASSERT() condition on Length and returns Buffer\r
229 //\r
230 return SetMem (Buffer, Length, PcdGet8(PcdDebugClearMemoryValue));\r
231}\r
232\r
233\r
234/**\r
235 Returns TRUE if ASSERT() macros are enabled.\r
236\r
237 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of \r
238 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
239\r
240 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.\r
241 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.\r
242\r
243**/\r
244BOOLEAN\r
245EFIAPI\r
246DebugAssertEnabled (\r
247 VOID\r
248 )\r
249{\r
250 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) != 0);\r
251}\r
252\r
253\r
254/** \r
255 Returns TRUE if DEBUG() macros are enabled.\r
256\r
257 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of \r
258 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
259\r
260 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.\r
261 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.\r
262\r
263**/\r
264BOOLEAN\r
265EFIAPI\r
266DebugPrintEnabled (\r
267 VOID\r
268 )\r
269{\r
270 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED) != 0);\r
271}\r
272\r
273\r
274/** \r
275 Returns TRUE if DEBUG_CODE() macros are enabled.\r
276\r
277 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of \r
278 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
279\r
280 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.\r
281 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.\r
282\r
283**/\r
284BOOLEAN\r
285EFIAPI\r
286DebugCodeEnabled (\r
287 VOID\r
288 )\r
289{\r
290 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED) != 0);\r
291}\r
292\r
293\r
294/** \r
295 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.\r
296\r
297 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of \r
298 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
299\r
300 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.\r
301 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.\r
302\r
303**/\r
304BOOLEAN\r
305EFIAPI\r
306DebugClearMemoryEnabled (\r
307 VOID\r
308 )\r
309{\r
310 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED) != 0);\r
311}\r
312\r
313/**\r
314 Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.\r
315\r
316 This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.\r
317\r
318 @retval TRUE Current ErrorLevel is supported.\r
319 @retval FALSE Current ErrorLevel is not supported.\r
320\r
321**/\r
322BOOLEAN\r
323EFIAPI\r
324DebugPrintLevelEnabled (\r
325 IN CONST UINTN ErrorLevel\r
326 )\r
327{\r
328 return (BOOLEAN) ((ErrorLevel & PcdGet32(PcdFixedDebugPrintErrorLevel)) != 0);\r
329}\r