]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/DxeRuntimeDebugLibSerialPort/DebugLib.c
BaseTools/BinToPcd: Fix Python 2.7.x compatibility issue
[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
7 Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>\r
8 Copyright (c) 2018, Linaro, Ltd. All rights reserved.<BR>\r
9\r
10 This program and the accompanying materials\r
11 are licensed and made available under the terms and conditions of the BSD License\r
12 which accompanies this distribution. The full text of the license may be found at\r
13 http://opensource.org/licenses/bsd-license.php.\r
14\r
15 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
16 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
17\r
18**/\r
19\r
20#include <Base.h>\r
21#include <Library/DebugLib.h>\r
22#include <Library/DebugPrintErrorLevelLib.h>\r
23#include <Library/BaseLib.h>\r
24#include <Library/PrintLib.h>\r
25#include <Library/PcdLib.h>\r
26#include <Library/BaseMemoryLib.h>\r
27#include <Library/SerialPortLib.h>\r
28\r
29STATIC EFI_EVENT mEfiExitBootServicesEvent;\r
30STATIC BOOLEAN mEfiAtRuntime = FALSE;\r
31\r
32//\r
33// Define the maximum debug and assert message length that this library supports\r
34//\r
35#define MAX_DEBUG_MESSAGE_LENGTH 0x100\r
36\r
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
128{\r
129 CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];\r
130 VA_LIST Marker;\r
131\r
132 if (mEfiAtRuntime) {\r
133 return;\r
134 }\r
135\r
136 //\r
137 // If Format is NULL, then ASSERT().\r
138 //\r
139 ASSERT (Format != NULL);\r
140\r
141 //\r
142 // Check driver debug mask value and global mask\r
143 //\r
144 if ((ErrorLevel & GetDebugPrintErrorLevel ()) == 0) {\r
145 return;\r
146 }\r
147\r
148 //\r
149 // Convert the DEBUG() message to an ASCII String\r
150 //\r
151 VA_START (Marker, Format);\r
152 AsciiVSPrint (Buffer, sizeof (Buffer), Format, Marker);\r
153 VA_END (Marker);\r
154\r
155 //\r
156 // Send the print string to a Serial Port\r
157 //\r
158 SerialPortWrite ((UINT8 *)Buffer, AsciiStrLen (Buffer));\r
159}\r
160\r
161\r
162/**\r
163 Prints an assert message containing a filename, line number, and description.\r
164 This may be followed by a breakpoint or a dead loop.\r
165\r
166 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"\r
167 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of\r
168 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if\r
169 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then\r
170 CpuDeadLoop() is called. If neither of these bits are set, then this function\r
171 returns immediately after the message is printed to the debug output device.\r
172 DebugAssert() must actively prevent recursion. If DebugAssert() is called while\r
173 processing another DebugAssert(), then DebugAssert() must return immediately.\r
174\r
175 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.\r
176 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.\r
177\r
178 @param FileName The pointer to the name of the source file that generated the assert condition.\r
179 @param LineNumber The line number in the source file that generated the assert condition\r
180 @param Description The pointer to the description of the assert condition.\r
181\r
182**/\r
183VOID\r
184EFIAPI\r
185DebugAssert (\r
186 IN CONST CHAR8 *FileName,\r
187 IN UINTN LineNumber,\r
188 IN CONST CHAR8 *Description\r
189 )\r
190{\r
191 CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];\r
192\r
193 //\r
194 // Generate the ASSERT() message in Ascii format\r
195 //\r
196 AsciiSPrint (Buffer, sizeof (Buffer), "ASSERT [%a] %a(%d): %a\n",\r
197 gEfiCallerBaseName, FileName, LineNumber, Description);\r
198\r
199 if (!mEfiAtRuntime) {\r
200 //\r
201 // Send the print string to the Console Output device\r
202 //\r
203 SerialPortWrite ((UINT8 *)Buffer, AsciiStrLen (Buffer));\r
204 }\r
205\r
206 //\r
207 // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings\r
208 //\r
209 if ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED) != 0) {\r
210 CpuBreakpoint ();\r
211 } else if ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED) != 0) {\r
212 CpuDeadLoop ();\r
213 }\r
214}\r
215\r
216\r
217/**\r
218 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.\r
219\r
220 This function fills Length bytes of Buffer with the value specified by\r
221 PcdDebugClearMemoryValue, and returns Buffer.\r
222\r
223 If Buffer is NULL, then ASSERT().\r
224 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
225\r
226 @param Buffer The pointer to the target buffer to be filled with PcdDebugClearMemoryValue.\r
227 @param Length The number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.\r
228\r
229 @return Buffer The pointer to the target buffer filled with PcdDebugClearMemoryValue.\r
230\r
231**/\r
232VOID *\r
233EFIAPI\r
234DebugClearMemory (\r
235 OUT VOID *Buffer,\r
236 IN UINTN Length\r
237 )\r
238{\r
239 //\r
240 // If Buffer is NULL, then ASSERT().\r
241 //\r
242 ASSERT (Buffer != NULL);\r
243\r
244 //\r
245 // SetMem() checks for the the ASSERT() condition on Length and returns Buffer\r
246 //\r
247 return SetMem (Buffer, Length, PcdGet8(PcdDebugClearMemoryValue));\r
248}\r
249\r
250\r
251/**\r
252 Returns TRUE if ASSERT() macros are enabled.\r
253\r
254 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of\r
255 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
256\r
257 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.\r
258 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.\r
259\r
260**/\r
261BOOLEAN\r
262EFIAPI\r
263DebugAssertEnabled (\r
264 VOID\r
265 )\r
266{\r
267 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) != 0);\r
268}\r
269\r
270\r
271/**\r
272 Returns TRUE if DEBUG() macros are enabled.\r
273\r
274 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of\r
275 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
276\r
277 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.\r
278 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.\r
279\r
280**/\r
281BOOLEAN\r
282EFIAPI\r
283DebugPrintEnabled (\r
284 VOID\r
285 )\r
286{\r
287 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED) != 0);\r
288}\r
289\r
290\r
291/**\r
292 Returns TRUE if DEBUG_CODE() macros are enabled.\r
293\r
294 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of\r
295 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
296\r
297 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.\r
298 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.\r
299\r
300**/\r
301BOOLEAN\r
302EFIAPI\r
303DebugCodeEnabled (\r
304 VOID\r
305 )\r
306{\r
307 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED) != 0);\r
308}\r
309\r
310\r
311/**\r
312 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.\r
313\r
314 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of\r
315 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
316\r
317 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.\r
318 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.\r
319\r
320**/\r
321BOOLEAN\r
322EFIAPI\r
323DebugClearMemoryEnabled (\r
324 VOID\r
325 )\r
326{\r
327 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED) != 0);\r
328}\r
329\r
330/**\r
331 Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.\r
332\r
333 This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.\r
334\r
335 @retval TRUE Current ErrorLevel is supported.\r
336 @retval FALSE Current ErrorLevel is not supported.\r
337\r
338**/\r
339BOOLEAN\r
340EFIAPI\r
341DebugPrintLevelEnabled (\r
342 IN CONST UINTN ErrorLevel\r
343 )\r
344{\r
345 return (BOOLEAN) ((ErrorLevel & PcdGet32(PcdFixedDebugPrintErrorLevel)) != 0);\r
346}\r