]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/DxeRuntimeDebugLibSerialPort/DebugLib.c
MdePkg: Do not use CreateEventEx unless required
[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
df851da3 80 return SystemTable->BootServices->CreateEvent (EVT_SIGNAL_EXIT_BOOT_SERVICES,\r
7f029e1b 81 TPL_NOTIFY, ExitBootServicesEvent, NULL,\r
7f029e1b
AB
82 &mEfiExitBootServicesEvent);\r
83}\r
84\r
85/**\r
86 If a runtime driver exits with an error, it must call this routine\r
87 to free the allocated resource before the exiting.\r
88\r
89 @param[in] ImageHandle The firmware allocated handle for the EFI image.\r
90 @param[in] SystemTable A pointer to the EFI System Table.\r
91\r
92 @retval EFI_SUCCESS The Runtime Driver Lib shutdown successfully.\r
93 @retval EFI_UNSUPPORTED Runtime Driver lib was not initialized.\r
94**/\r
95EFI_STATUS\r
96EFIAPI\r
97DxeRuntimeDebugLibSerialPortDestructor (\r
98 IN EFI_HANDLE ImageHandle,\r
99 IN EFI_SYSTEM_TABLE *SystemTable\r
100 )\r
101{\r
102 return SystemTable->BootServices->CloseEvent (mEfiExitBootServicesEvent);\r
103}\r
104\r
105/**\r
106 Prints a debug message to the debug output device if the specified error level is enabled.\r
107\r
108 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function\r
109 GetDebugPrintErrorLevel (), then print the message specified by Format and the\r
110 associated variable argument list to the debug output device.\r
111\r
112 If Format is NULL, then ASSERT().\r
113\r
114 @param ErrorLevel The error level of the debug message.\r
115 @param Format Format string for the debug message to print.\r
116 @param ... Variable argument list whose contents are accessed\r
117 based on the format string specified by Format.\r
118\r
119**/\r
120VOID\r
121EFIAPI\r
122DebugPrint (\r
123 IN UINTN ErrorLevel,\r
124 IN CONST CHAR8 *Format,\r
125 ...\r
126 )\r
b22b4f5a
BB
127{\r
128 VA_LIST Marker;\r
129\r
130 VA_START (Marker, Format);\r
131 DebugVPrint (ErrorLevel, Format, Marker);\r
132 VA_END (Marker);\r
133}\r
134\r
135\r
136/**\r
137 Prints a debug message to the debug output device if the specified\r
138 error level is enabled base on Null-terminated format string and a\r
139 VA_LIST argument list or a BASE_LIST argument list.\r
140\r
141 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function\r
142 GetDebugPrintErrorLevel (), then print the message specified by Format and\r
143 the associated variable argument list to the debug output device.\r
144\r
145 If Format is NULL, then ASSERT().\r
146\r
147 @param ErrorLevel The error level of the debug message.\r
148 @param Format Format string for the debug message to print.\r
149 @param VaListMarker VA_LIST marker for the variable argument list.\r
150 @param BaseListMarker BASE_LIST marker for the variable argument list.\r
151\r
152**/\r
153VOID\r
154DebugPrintMarker (\r
155 IN UINTN ErrorLevel,\r
156 IN CONST CHAR8 *Format,\r
157 IN VA_LIST VaListMarker,\r
158 IN BASE_LIST BaseListMarker\r
159 )\r
7f029e1b
AB
160{\r
161 CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];\r
7f029e1b
AB
162\r
163 if (mEfiAtRuntime) {\r
164 return;\r
165 }\r
166\r
167 //\r
168 // If Format is NULL, then ASSERT().\r
169 //\r
170 ASSERT (Format != NULL);\r
171\r
172 //\r
173 // Check driver debug mask value and global mask\r
174 //\r
175 if ((ErrorLevel & GetDebugPrintErrorLevel ()) == 0) {\r
176 return;\r
177 }\r
178\r
179 //\r
180 // Convert the DEBUG() message to an ASCII String\r
181 //\r
b22b4f5a
BB
182 if (BaseListMarker == NULL) {\r
183 AsciiVSPrint (Buffer, sizeof (Buffer), Format, VaListMarker);\r
184 } else {\r
185 AsciiBSPrint (Buffer, sizeof (Buffer), Format, BaseListMarker);\r
186 }\r
7f029e1b
AB
187\r
188 //\r
189 // Send the print string to a Serial Port\r
190 //\r
191 SerialPortWrite ((UINT8 *)Buffer, AsciiStrLen (Buffer));\r
192}\r
193\r
194\r
b22b4f5a
BB
195/**\r
196 Prints a debug message to the debug output device if the specified\r
197 error level is enabled.\r
198\r
199 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function\r
200 GetDebugPrintErrorLevel (), then print the message specified by Format and\r
201 the associated variable argument list to the debug output device.\r
202\r
203 If Format is NULL, then ASSERT().\r
204\r
205 @param ErrorLevel The error level of the debug message.\r
206 @param Format Format string for the debug message to print.\r
207 @param VaListMarker VA_LIST marker for the variable argument list.\r
208\r
209**/\r
210VOID\r
211EFIAPI\r
212DebugVPrint (\r
213 IN UINTN ErrorLevel,\r
214 IN CONST CHAR8 *Format,\r
215 IN VA_LIST VaListMarker\r
216 )\r
217{\r
218 DebugPrintMarker (ErrorLevel, Format, VaListMarker, NULL);\r
219}\r
220\r
221\r
222/**\r
223 Prints a debug message to the debug output device if the specified\r
224 error level is enabled.\r
225 This function use BASE_LIST which would provide a more compatible\r
226 service than VA_LIST.\r
227\r
228 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function\r
229 GetDebugPrintErrorLevel (), then print the message specified by Format and\r
230 the associated variable argument list to the debug output device.\r
231\r
232 If Format is NULL, then ASSERT().\r
233\r
234 @param ErrorLevel The error level of the debug message.\r
235 @param Format Format string for the debug message to print.\r
236 @param BaseListMarker BASE_LIST marker for the variable argument list.\r
237\r
238**/\r
239VOID\r
240EFIAPI\r
241DebugBPrint (\r
242 IN UINTN ErrorLevel,\r
243 IN CONST CHAR8 *Format,\r
244 IN BASE_LIST BaseListMarker\r
245 )\r
246{\r
247 DebugPrintMarker (ErrorLevel, Format, mVaListNull, BaseListMarker);\r
248}\r
249\r
250\r
7f029e1b
AB
251/**\r
252 Prints an assert message containing a filename, line number, and description.\r
253 This may be followed by a breakpoint or a dead loop.\r
254\r
255 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"\r
256 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of\r
257 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if\r
258 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then\r
259 CpuDeadLoop() is called. If neither of these bits are set, then this function\r
260 returns immediately after the message is printed to the debug output device.\r
261 DebugAssert() must actively prevent recursion. If DebugAssert() is called while\r
262 processing another DebugAssert(), then DebugAssert() must return immediately.\r
263\r
264 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.\r
265 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.\r
266\r
267 @param FileName The pointer to the name of the source file that generated the assert condition.\r
268 @param LineNumber The line number in the source file that generated the assert condition\r
269 @param Description The pointer to the description of the assert condition.\r
270\r
271**/\r
272VOID\r
273EFIAPI\r
274DebugAssert (\r
275 IN CONST CHAR8 *FileName,\r
276 IN UINTN LineNumber,\r
277 IN CONST CHAR8 *Description\r
278 )\r
279{\r
280 CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];\r
281\r
282 //\r
283 // Generate the ASSERT() message in Ascii format\r
284 //\r
285 AsciiSPrint (Buffer, sizeof (Buffer), "ASSERT [%a] %a(%d): %a\n",\r
286 gEfiCallerBaseName, FileName, LineNumber, Description);\r
287\r
288 if (!mEfiAtRuntime) {\r
289 //\r
290 // Send the print string to the Console Output device\r
291 //\r
292 SerialPortWrite ((UINT8 *)Buffer, AsciiStrLen (Buffer));\r
293 }\r
294\r
295 //\r
296 // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings\r
297 //\r
298 if ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED) != 0) {\r
299 CpuBreakpoint ();\r
300 } else if ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED) != 0) {\r
301 CpuDeadLoop ();\r
302 }\r
303}\r
304\r
305\r
306/**\r
307 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.\r
308\r
309 This function fills Length bytes of Buffer with the value specified by\r
310 PcdDebugClearMemoryValue, and returns Buffer.\r
311\r
312 If Buffer is NULL, then ASSERT().\r
313 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
314\r
315 @param Buffer The pointer to the target buffer to be filled with PcdDebugClearMemoryValue.\r
316 @param Length The number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.\r
317\r
318 @return Buffer The pointer to the target buffer filled with PcdDebugClearMemoryValue.\r
319\r
320**/\r
321VOID *\r
322EFIAPI\r
323DebugClearMemory (\r
324 OUT VOID *Buffer,\r
325 IN UINTN Length\r
326 )\r
327{\r
328 //\r
329 // If Buffer is NULL, then ASSERT().\r
330 //\r
331 ASSERT (Buffer != NULL);\r
332\r
333 //\r
334 // SetMem() checks for the the ASSERT() condition on Length and returns Buffer\r
335 //\r
336 return SetMem (Buffer, Length, PcdGet8(PcdDebugClearMemoryValue));\r
337}\r
338\r
339\r
340/**\r
341 Returns TRUE if ASSERT() macros are enabled.\r
342\r
343 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of\r
344 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
345\r
346 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.\r
347 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.\r
348\r
349**/\r
350BOOLEAN\r
351EFIAPI\r
352DebugAssertEnabled (\r
353 VOID\r
354 )\r
355{\r
356 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) != 0);\r
357}\r
358\r
359\r
360/**\r
361 Returns TRUE if DEBUG() macros are enabled.\r
362\r
363 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of\r
364 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
365\r
366 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.\r
367 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.\r
368\r
369**/\r
370BOOLEAN\r
371EFIAPI\r
372DebugPrintEnabled (\r
373 VOID\r
374 )\r
375{\r
376 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED) != 0);\r
377}\r
378\r
379\r
380/**\r
381 Returns TRUE if DEBUG_CODE() macros are enabled.\r
382\r
383 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of\r
384 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
385\r
386 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.\r
387 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.\r
388\r
389**/\r
390BOOLEAN\r
391EFIAPI\r
392DebugCodeEnabled (\r
393 VOID\r
394 )\r
395{\r
396 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED) != 0);\r
397}\r
398\r
399\r
400/**\r
401 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.\r
402\r
403 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of\r
404 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
405\r
406 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.\r
407 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.\r
408\r
409**/\r
410BOOLEAN\r
411EFIAPI\r
412DebugClearMemoryEnabled (\r
413 VOID\r
414 )\r
415{\r
416 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED) != 0);\r
417}\r
418\r
419/**\r
420 Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.\r
421\r
422 This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.\r
423\r
424 @retval TRUE Current ErrorLevel is supported.\r
425 @retval FALSE Current ErrorLevel is not supported.\r
426\r
427**/\r
428BOOLEAN\r
429EFIAPI\r
430DebugPrintLevelEnabled (\r
431 IN CONST UINTN ErrorLevel\r
432 )\r
433{\r
434 return (BOOLEAN) ((ErrorLevel & PcdGet32(PcdFixedDebugPrintErrorLevel)) != 0);\r
435}\r