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