]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/DxeRuntimeDebugLibSerialPort/DebugLib.c
MdePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdePkg / Library / DxeRuntimeDebugLibSerialPort / DebugLib.c
CommitLineData
7f029e1b
AB
1/** @file\r
2 DXE runtime Debug library instance based on Serial Port library.\r
3 It takes care not to call into SerialPortLib after ExitBootServices() has\r
4 been called, to prevent touching hardware that is no longer owned by the\r
5 firmware.\r
6\r
b22b4f5a 7 Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>\r
7f029e1b
AB
8 Copyright (c) 2018, Linaro, Ltd. All rights reserved.<BR>\r
9\r
9344f092 10 SPDX-License-Identifier: BSD-2-Clause-Patent\r
7f029e1b
AB
11\r
12**/\r
13\r
14#include <Base.h>\r
15#include <Library/DebugLib.h>\r
16#include <Library/DebugPrintErrorLevelLib.h>\r
17#include <Library/BaseLib.h>\r
18#include <Library/PrintLib.h>\r
19#include <Library/PcdLib.h>\r
20#include <Library/BaseMemoryLib.h>\r
21#include <Library/SerialPortLib.h>\r
22\r
23STATIC EFI_EVENT mEfiExitBootServicesEvent;\r
24STATIC BOOLEAN mEfiAtRuntime = FALSE;\r
25\r
26//\r
27// Define the maximum debug and assert message length that this library supports\r
28//\r
29#define MAX_DEBUG_MESSAGE_LENGTH 0x100\r
30\r
b22b4f5a
BB
31//\r
32// VA_LIST can not initialize to NULL for all compiler, so we use this to\r
33// indicate a null VA_LIST\r
34//\r
35VA_LIST mVaListNull;\r
36\r
7f029e1b
AB
37/**\r
38 Set AtRuntime flag as TRUE after ExitBootServices.\r
39\r
40 @param[in] Event The Event that is being processed.\r
41 @param[in] Context The Event Context.\r
42\r
43**/\r
44STATIC\r
45VOID\r
46EFIAPI\r
47ExitBootServicesEvent (\r
48 IN EFI_EVENT Event,\r
49 IN VOID *Context\r
50 )\r
51{\r
52 mEfiAtRuntime = TRUE;\r
53}\r
54\r
55/**\r
56 The constructor function to initialize the Serial Port library and\r
57 register a callback for the ExitBootServices event.\r
58\r
59 @param[in] ImageHandle The firmware allocated handle for the EFI image.\r
60 @param[in] SystemTable A pointer to the EFI System Table.\r
61\r
62 @retval EFI_SUCCESS The operation completed successfully.\r
63 @retval other Either the serial port failed to initialize or the\r
64 ExitBootServices event callback registration failed.\r
65**/\r
66EFI_STATUS\r
67EFIAPI\r
68DxeRuntimeDebugLibSerialPortConstructor (\r
69 IN EFI_HANDLE ImageHandle,\r
70 IN EFI_SYSTEM_TABLE *SystemTable\r
71 )\r
72{\r
73 EFI_STATUS Status;\r
74\r
75 Status = SerialPortInitialize ();\r
76 if (EFI_ERROR (Status)) {\r
77 return Status;\r
78 }\r
79\r
80 return SystemTable->BootServices->CreateEventEx (EVT_NOTIFY_SIGNAL,\r
81 TPL_NOTIFY, ExitBootServicesEvent, NULL,\r
82 &gEfiEventExitBootServicesGuid,\r
83 &mEfiExitBootServicesEvent);\r
84}\r
85\r
86/**\r
87 If a runtime driver exits with an error, it must call this routine\r
88 to free the allocated resource before the exiting.\r
89\r
90 @param[in] ImageHandle The firmware allocated handle for the EFI image.\r
91 @param[in] SystemTable A pointer to the EFI System Table.\r
92\r
93 @retval EFI_SUCCESS The Runtime Driver Lib shutdown successfully.\r
94 @retval EFI_UNSUPPORTED Runtime Driver lib was not initialized.\r
95**/\r
96EFI_STATUS\r
97EFIAPI\r
98DxeRuntimeDebugLibSerialPortDestructor (\r
99 IN EFI_HANDLE ImageHandle,\r
100 IN EFI_SYSTEM_TABLE *SystemTable\r
101 )\r
102{\r
103 return SystemTable->BootServices->CloseEvent (mEfiExitBootServicesEvent);\r
104}\r
105\r
106/**\r
107 Prints a debug message to the debug output device if the specified error level is enabled.\r
108\r
109 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function\r
110 GetDebugPrintErrorLevel (), then print the message specified by Format and the\r
111 associated variable argument list to the debug output device.\r
112\r
113 If Format is NULL, then ASSERT().\r
114\r
115 @param ErrorLevel The error level of the debug message.\r
116 @param Format Format string for the debug message to print.\r
117 @param ... Variable argument list whose contents are accessed\r
118 based on the format string specified by Format.\r
119\r
120**/\r
121VOID\r
122EFIAPI\r
123DebugPrint (\r
124 IN UINTN ErrorLevel,\r
125 IN CONST CHAR8 *Format,\r
126 ...\r
127 )\r
b22b4f5a
BB
128{\r
129 VA_LIST Marker;\r
130\r
131 VA_START (Marker, Format);\r
132 DebugVPrint (ErrorLevel, Format, Marker);\r
133 VA_END (Marker);\r
134}\r
135\r
136\r
137/**\r
138 Prints a debug message to the debug output device if the specified\r
139 error level is enabled base on Null-terminated format string and a\r
140 VA_LIST argument list or a BASE_LIST argument list.\r
141\r
142 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function\r
143 GetDebugPrintErrorLevel (), then print the message specified by Format and\r
144 the associated variable argument list to the debug output device.\r
145\r
146 If Format is NULL, then ASSERT().\r
147\r
148 @param ErrorLevel The error level of the debug message.\r
149 @param Format Format string for the debug message to print.\r
150 @param VaListMarker VA_LIST marker for the variable argument list.\r
151 @param BaseListMarker BASE_LIST marker for the variable argument list.\r
152\r
153**/\r
154VOID\r
155DebugPrintMarker (\r
156 IN UINTN ErrorLevel,\r
157 IN CONST CHAR8 *Format,\r
158 IN VA_LIST VaListMarker,\r
159 IN BASE_LIST BaseListMarker\r
160 )\r
7f029e1b
AB
161{\r
162 CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];\r
7f029e1b
AB
163\r
164 if (mEfiAtRuntime) {\r
165 return;\r
166 }\r
167\r
168 //\r
169 // If Format is NULL, then ASSERT().\r
170 //\r
171 ASSERT (Format != NULL);\r
172\r
173 //\r
174 // Check driver debug mask value and global mask\r
175 //\r
176 if ((ErrorLevel & GetDebugPrintErrorLevel ()) == 0) {\r
177 return;\r
178 }\r
179\r
180 //\r
181 // Convert the DEBUG() message to an ASCII String\r
182 //\r
b22b4f5a
BB
183 if (BaseListMarker == NULL) {\r
184 AsciiVSPrint (Buffer, sizeof (Buffer), Format, VaListMarker);\r
185 } else {\r
186 AsciiBSPrint (Buffer, sizeof (Buffer), Format, BaseListMarker);\r
187 }\r
7f029e1b
AB
188\r
189 //\r
190 // Send the print string to a Serial Port\r
191 //\r
192 SerialPortWrite ((UINT8 *)Buffer, AsciiStrLen (Buffer));\r
193}\r
194\r
195\r
b22b4f5a
BB
196/**\r
197 Prints a debug message to the debug output device if the specified\r
198 error level is enabled.\r
199\r
200 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function\r
201 GetDebugPrintErrorLevel (), then print the message specified by Format and\r
202 the associated variable argument list to the debug output device.\r
203\r
204 If Format is NULL, then ASSERT().\r
205\r
206 @param ErrorLevel The error level of the debug message.\r
207 @param Format Format string for the debug message to print.\r
208 @param VaListMarker VA_LIST marker for the variable argument list.\r
209\r
210**/\r
211VOID\r
212EFIAPI\r
213DebugVPrint (\r
214 IN UINTN ErrorLevel,\r
215 IN CONST CHAR8 *Format,\r
216 IN VA_LIST VaListMarker\r
217 )\r
218{\r
219 DebugPrintMarker (ErrorLevel, Format, VaListMarker, NULL);\r
220}\r
221\r
222\r
223/**\r
224 Prints a debug message to the debug output device if the specified\r
225 error level is enabled.\r
226 This function use BASE_LIST which would provide a more compatible\r
227 service than VA_LIST.\r
228\r
229 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function\r
230 GetDebugPrintErrorLevel (), then print the message specified by Format and\r
231 the associated variable argument list to the debug output device.\r
232\r
233 If Format is NULL, then ASSERT().\r
234\r
235 @param ErrorLevel The error level of the debug message.\r
236 @param Format Format string for the debug message to print.\r
237 @param BaseListMarker BASE_LIST marker for the variable argument list.\r
238\r
239**/\r
240VOID\r
241EFIAPI\r
242DebugBPrint (\r
243 IN UINTN ErrorLevel,\r
244 IN CONST CHAR8 *Format,\r
245 IN BASE_LIST BaseListMarker\r
246 )\r
247{\r
248 DebugPrintMarker (ErrorLevel, Format, mVaListNull, BaseListMarker);\r
249}\r
250\r
251\r
7f029e1b
AB
252/**\r
253 Prints an assert message containing a filename, line number, and description.\r
254 This may be followed by a breakpoint or a dead loop.\r
255\r
256 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"\r
257 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of\r
258 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if\r
259 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then\r
260 CpuDeadLoop() is called. If neither of these bits are set, then this function\r
261 returns immediately after the message is printed to the debug output device.\r
262 DebugAssert() must actively prevent recursion. If DebugAssert() is called while\r
263 processing another DebugAssert(), then DebugAssert() must return immediately.\r
264\r
265 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.\r
266 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.\r
267\r
268 @param FileName The pointer to the name of the source file that generated the assert condition.\r
269 @param LineNumber The line number in the source file that generated the assert condition\r
270 @param Description The pointer to the description of the assert condition.\r
271\r
272**/\r
273VOID\r
274EFIAPI\r
275DebugAssert (\r
276 IN CONST CHAR8 *FileName,\r
277 IN UINTN LineNumber,\r
278 IN CONST CHAR8 *Description\r
279 )\r
280{\r
281 CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];\r
282\r
283 //\r
284 // Generate the ASSERT() message in Ascii format\r
285 //\r
286 AsciiSPrint (Buffer, sizeof (Buffer), "ASSERT [%a] %a(%d): %a\n",\r
287 gEfiCallerBaseName, FileName, LineNumber, Description);\r
288\r
289 if (!mEfiAtRuntime) {\r
290 //\r
291 // Send the print string to the Console Output device\r
292 //\r
293 SerialPortWrite ((UINT8 *)Buffer, AsciiStrLen (Buffer));\r
294 }\r
295\r
296 //\r
297 // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings\r
298 //\r
299 if ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED) != 0) {\r
300 CpuBreakpoint ();\r
301 } else if ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED) != 0) {\r
302 CpuDeadLoop ();\r
303 }\r
304}\r
305\r
306\r
307/**\r
308 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.\r
309\r
310 This function fills Length bytes of Buffer with the value specified by\r
311 PcdDebugClearMemoryValue, and returns Buffer.\r
312\r
313 If Buffer is NULL, then ASSERT().\r
314 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
315\r
316 @param Buffer The pointer to the target buffer to be filled with PcdDebugClearMemoryValue.\r
317 @param Length The number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.\r
318\r
319 @return Buffer The pointer to the target buffer filled with PcdDebugClearMemoryValue.\r
320\r
321**/\r
322VOID *\r
323EFIAPI\r
324DebugClearMemory (\r
325 OUT VOID *Buffer,\r
326 IN UINTN Length\r
327 )\r
328{\r
329 //\r
330 // If Buffer is NULL, then ASSERT().\r
331 //\r
332 ASSERT (Buffer != NULL);\r
333\r
334 //\r
335 // SetMem() checks for the the ASSERT() condition on Length and returns Buffer\r
336 //\r
337 return SetMem (Buffer, Length, PcdGet8(PcdDebugClearMemoryValue));\r
338}\r
339\r
340\r
341/**\r
342 Returns TRUE if ASSERT() macros are enabled.\r
343\r
344 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of\r
345 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
346\r
347 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.\r
348 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.\r
349\r
350**/\r
351BOOLEAN\r
352EFIAPI\r
353DebugAssertEnabled (\r
354 VOID\r
355 )\r
356{\r
357 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) != 0);\r
358}\r
359\r
360\r
361/**\r
362 Returns TRUE if DEBUG() macros are enabled.\r
363\r
364 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of\r
365 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
366\r
367 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.\r
368 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.\r
369\r
370**/\r
371BOOLEAN\r
372EFIAPI\r
373DebugPrintEnabled (\r
374 VOID\r
375 )\r
376{\r
377 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED) != 0);\r
378}\r
379\r
380\r
381/**\r
382 Returns TRUE if DEBUG_CODE() macros are enabled.\r
383\r
384 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of\r
385 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
386\r
387 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.\r
388 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.\r
389\r
390**/\r
391BOOLEAN\r
392EFIAPI\r
393DebugCodeEnabled (\r
394 VOID\r
395 )\r
396{\r
397 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED) != 0);\r
398}\r
399\r
400\r
401/**\r
402 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.\r
403\r
404 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of\r
405 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
406\r
407 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.\r
408 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.\r
409\r
410**/\r
411BOOLEAN\r
412EFIAPI\r
413DebugClearMemoryEnabled (\r
414 VOID\r
415 )\r
416{\r
417 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED) != 0);\r
418}\r
419\r
420/**\r
421 Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.\r
422\r
423 This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.\r
424\r
425 @retval TRUE Current ErrorLevel is supported.\r
426 @retval FALSE Current ErrorLevel is not supported.\r
427\r
428**/\r
429BOOLEAN\r
430EFIAPI\r
431DebugPrintLevelEnabled (\r
432 IN CONST UINTN ErrorLevel\r
433 )\r
434{\r
435 return (BOOLEAN) ((ErrorLevel & PcdGet32(PcdFixedDebugPrintErrorLevel)) != 0);\r
436}\r