]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/DxeRuntimeDebugLibSerialPort/DebugLib.c
MdePkg: introduce DxeRuntimeDebugLibSerialPort
[mirror_edk2.git] / MdePkg / Library / DxeRuntimeDebugLibSerialPort / DebugLib.c
1 /** @file
2 DXE runtime Debug library instance based on Serial Port library.
3 It takes care not to call into SerialPortLib after ExitBootServices() has
4 been called, to prevent touching hardware that is no longer owned by the
5 firmware.
6
7 Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
8 Copyright (c) 2018, Linaro, Ltd. All rights reserved.<BR>
9
10 This program and the accompanying materials
11 are licensed and made available under the terms and conditions of the BSD License
12 which accompanies this distribution. The full text of the license may be found at
13 http://opensource.org/licenses/bsd-license.php.
14
15 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
16 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17
18 **/
19
20 #include <Base.h>
21 #include <Library/DebugLib.h>
22 #include <Library/DebugPrintErrorLevelLib.h>
23 #include <Library/BaseLib.h>
24 #include <Library/PrintLib.h>
25 #include <Library/PcdLib.h>
26 #include <Library/BaseMemoryLib.h>
27 #include <Library/SerialPortLib.h>
28
29 STATIC EFI_EVENT mEfiExitBootServicesEvent;
30 STATIC BOOLEAN mEfiAtRuntime = FALSE;
31
32 //
33 // Define the maximum debug and assert message length that this library supports
34 //
35 #define MAX_DEBUG_MESSAGE_LENGTH 0x100
36
37 /**
38 Set AtRuntime flag as TRUE after ExitBootServices.
39
40 @param[in] Event The Event that is being processed.
41 @param[in] Context The Event Context.
42
43 **/
44 STATIC
45 VOID
46 EFIAPI
47 ExitBootServicesEvent (
48 IN EFI_EVENT Event,
49 IN VOID *Context
50 )
51 {
52 mEfiAtRuntime = TRUE;
53 }
54
55 /**
56 The constructor function to initialize the Serial Port library and
57 register a callback for the ExitBootServices event.
58
59 @param[in] ImageHandle The firmware allocated handle for the EFI image.
60 @param[in] SystemTable A pointer to the EFI System Table.
61
62 @retval EFI_SUCCESS The operation completed successfully.
63 @retval other Either the serial port failed to initialize or the
64 ExitBootServices event callback registration failed.
65 **/
66 EFI_STATUS
67 EFIAPI
68 DxeRuntimeDebugLibSerialPortConstructor (
69 IN EFI_HANDLE ImageHandle,
70 IN EFI_SYSTEM_TABLE *SystemTable
71 )
72 {
73 EFI_STATUS Status;
74
75 Status = SerialPortInitialize ();
76 if (EFI_ERROR (Status)) {
77 return Status;
78 }
79
80 return SystemTable->BootServices->CreateEventEx (EVT_NOTIFY_SIGNAL,
81 TPL_NOTIFY, ExitBootServicesEvent, NULL,
82 &gEfiEventExitBootServicesGuid,
83 &mEfiExitBootServicesEvent);
84 }
85
86 /**
87 If a runtime driver exits with an error, it must call this routine
88 to free the allocated resource before the exiting.
89
90 @param[in] ImageHandle The firmware allocated handle for the EFI image.
91 @param[in] SystemTable A pointer to the EFI System Table.
92
93 @retval EFI_SUCCESS The Runtime Driver Lib shutdown successfully.
94 @retval EFI_UNSUPPORTED Runtime Driver lib was not initialized.
95 **/
96 EFI_STATUS
97 EFIAPI
98 DxeRuntimeDebugLibSerialPortDestructor (
99 IN EFI_HANDLE ImageHandle,
100 IN EFI_SYSTEM_TABLE *SystemTable
101 )
102 {
103 return SystemTable->BootServices->CloseEvent (mEfiExitBootServicesEvent);
104 }
105
106 /**
107 Prints a debug message to the debug output device if the specified error level is enabled.
108
109 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
110 GetDebugPrintErrorLevel (), then print the message specified by Format and the
111 associated variable argument list to the debug output device.
112
113 If Format is NULL, then ASSERT().
114
115 @param ErrorLevel The error level of the debug message.
116 @param Format Format string for the debug message to print.
117 @param ... Variable argument list whose contents are accessed
118 based on the format string specified by Format.
119
120 **/
121 VOID
122 EFIAPI
123 DebugPrint (
124 IN UINTN ErrorLevel,
125 IN CONST CHAR8 *Format,
126 ...
127 )
128 {
129 CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];
130 VA_LIST Marker;
131
132 if (mEfiAtRuntime) {
133 return;
134 }
135
136 //
137 // If Format is NULL, then ASSERT().
138 //
139 ASSERT (Format != NULL);
140
141 //
142 // Check driver debug mask value and global mask
143 //
144 if ((ErrorLevel & GetDebugPrintErrorLevel ()) == 0) {
145 return;
146 }
147
148 //
149 // Convert the DEBUG() message to an ASCII String
150 //
151 VA_START (Marker, Format);
152 AsciiVSPrint (Buffer, sizeof (Buffer), Format, Marker);
153 VA_END (Marker);
154
155 //
156 // Send the print string to a Serial Port
157 //
158 SerialPortWrite ((UINT8 *)Buffer, AsciiStrLen (Buffer));
159 }
160
161
162 /**
163 Prints an assert message containing a filename, line number, and description.
164 This may be followed by a breakpoint or a dead loop.
165
166 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"
167 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of
168 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if
169 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then
170 CpuDeadLoop() is called. If neither of these bits are set, then this function
171 returns immediately after the message is printed to the debug output device.
172 DebugAssert() must actively prevent recursion. If DebugAssert() is called while
173 processing another DebugAssert(), then DebugAssert() must return immediately.
174
175 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.
176 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.
177
178 @param FileName The pointer to the name of the source file that generated the assert condition.
179 @param LineNumber The line number in the source file that generated the assert condition
180 @param Description The pointer to the description of the assert condition.
181
182 **/
183 VOID
184 EFIAPI
185 DebugAssert (
186 IN CONST CHAR8 *FileName,
187 IN UINTN LineNumber,
188 IN CONST CHAR8 *Description
189 )
190 {
191 CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];
192
193 //
194 // Generate the ASSERT() message in Ascii format
195 //
196 AsciiSPrint (Buffer, sizeof (Buffer), "ASSERT [%a] %a(%d): %a\n",
197 gEfiCallerBaseName, FileName, LineNumber, Description);
198
199 if (!mEfiAtRuntime) {
200 //
201 // Send the print string to the Console Output device
202 //
203 SerialPortWrite ((UINT8 *)Buffer, AsciiStrLen (Buffer));
204 }
205
206 //
207 // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings
208 //
209 if ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED) != 0) {
210 CpuBreakpoint ();
211 } else if ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED) != 0) {
212 CpuDeadLoop ();
213 }
214 }
215
216
217 /**
218 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.
219
220 This function fills Length bytes of Buffer with the value specified by
221 PcdDebugClearMemoryValue, and returns Buffer.
222
223 If Buffer is NULL, then ASSERT().
224 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
225
226 @param Buffer The pointer to the target buffer to be filled with PcdDebugClearMemoryValue.
227 @param Length The number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.
228
229 @return Buffer The pointer to the target buffer filled with PcdDebugClearMemoryValue.
230
231 **/
232 VOID *
233 EFIAPI
234 DebugClearMemory (
235 OUT VOID *Buffer,
236 IN UINTN Length
237 )
238 {
239 //
240 // If Buffer is NULL, then ASSERT().
241 //
242 ASSERT (Buffer != NULL);
243
244 //
245 // SetMem() checks for the the ASSERT() condition on Length and returns Buffer
246 //
247 return SetMem (Buffer, Length, PcdGet8(PcdDebugClearMemoryValue));
248 }
249
250
251 /**
252 Returns TRUE if ASSERT() macros are enabled.
253
254 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of
255 PcdDebugProperyMask is set. Otherwise FALSE is returned.
256
257 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.
258 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.
259
260 **/
261 BOOLEAN
262 EFIAPI
263 DebugAssertEnabled (
264 VOID
265 )
266 {
267 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) != 0);
268 }
269
270
271 /**
272 Returns TRUE if DEBUG() macros are enabled.
273
274 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of
275 PcdDebugProperyMask is set. Otherwise FALSE is returned.
276
277 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.
278 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.
279
280 **/
281 BOOLEAN
282 EFIAPI
283 DebugPrintEnabled (
284 VOID
285 )
286 {
287 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED) != 0);
288 }
289
290
291 /**
292 Returns TRUE if DEBUG_CODE() macros are enabled.
293
294 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of
295 PcdDebugProperyMask is set. Otherwise FALSE is returned.
296
297 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.
298 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.
299
300 **/
301 BOOLEAN
302 EFIAPI
303 DebugCodeEnabled (
304 VOID
305 )
306 {
307 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED) != 0);
308 }
309
310
311 /**
312 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.
313
314 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of
315 PcdDebugProperyMask is set. Otherwise FALSE is returned.
316
317 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.
318 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.
319
320 **/
321 BOOLEAN
322 EFIAPI
323 DebugClearMemoryEnabled (
324 VOID
325 )
326 {
327 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED) != 0);
328 }
329
330 /**
331 Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.
332
333 This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.
334
335 @retval TRUE Current ErrorLevel is supported.
336 @retval FALSE Current ErrorLevel is not supported.
337
338 **/
339 BOOLEAN
340 EFIAPI
341 DebugPrintLevelEnabled (
342 IN CONST UINTN ErrorLevel
343 )
344 {
345 return (BOOLEAN) ((ErrorLevel & PcdGet32(PcdFixedDebugPrintErrorLevel)) != 0);
346 }