]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFsp2Pkg/Library/BaseFspDebugLibSerialPort/DebugLib.c
cb2317bfb2409eafa2f09e5bf4e059e1b09eeddb
[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 UINTN *
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 UINTN Value,
197 CHAR8 *Buffer
198 )
199 {
200 INTN Idx;
201
202 for (Idx = (sizeof (UINTN) * 2) - 1; 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 UINTN *Frame;
232
233 Frame = (UINTN *)GetStackFramePointer ();
234
235 //
236 // Generate the ASSERT() message in Ascii format
237 //
238 if (sizeof (UINTN) == sizeof (UINT32)) {
239 AsciiStrnCpyS (
240 Buffer,
241 sizeof (Buffer) / sizeof (CHAR8),
242 "-> EBP:0x00000000 EIP:0x00000000\n",
243 sizeof (Buffer) / sizeof (CHAR8) - 1
244 );
245 } else {
246 AsciiStrnCpyS (
247 Buffer,
248 sizeof (Buffer) / sizeof (CHAR8),
249 "-> RBP:0x0000000000000000 RIP:0x0000000000000000\n",
250 sizeof (Buffer) / sizeof (CHAR8) - 1
251 );
252 }
253 SerialPortWrite ((UINT8 *)"ASSERT DUMP:\n", 13);
254 while (Frame != NULL) {
255 FillHex ((UINTN)Frame, Buffer + 9);
256 FillHex (Frame[1], Buffer + 9 + (sizeof (UINTN) * 2) + 8);
257 SerialPortWrite ((UINT8 *)Buffer, AsciiStrLen (Buffer));
258 if ((Frame[0] > (UINTN)Frame) && (Frame[0] < (UINTN)Frame + 0x00100000)) {
259 Frame = (UINTN *)Frame[0];
260 } else {
261 Frame = NULL;
262 }
263 }
264
265 //
266 // Dead loop
267 //
268 CpuDeadLoop ();
269 }
270
271 /**
272 Prints an assert message containing a filename, line number, and description.
273 This may be followed by a breakpoint or a dead loop.
274
275 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"
276 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of
277 PcdDebugPropertyMask is set then CpuBreakpoint() is called. Otherwise, if
278 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugPropertyMask is set then
279 CpuDeadLoop() is called. If neither of these bits are set, then this function
280 returns immediately after the message is printed to the debug output device.
281 DebugAssert() must actively prevent recursion. If DebugAssert() is called while
282 processing another DebugAssert(), then DebugAssert() must return immediately.
283
284 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.
285 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.
286
287 @param FileName The pointer to the name of the source file that generated the assert condition.
288 @param LineNumber The line number in the source file that generated the assert condition
289 @param Description The pointer to the description of the assert condition.
290
291 **/
292 VOID
293 EFIAPI
294 DebugAssert (
295 IN CONST CHAR8 *FileName,
296 IN UINTN LineNumber,
297 IN CONST CHAR8 *Description
298 )
299 {
300 DebugAssertInternal ();
301 }
302
303 /**
304 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.
305
306 This function fills Length bytes of Buffer with the value specified by
307 PcdDebugClearMemoryValue, and returns Buffer.
308
309 If Buffer is NULL, then ASSERT().
310 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
311
312 @param Buffer The pointer to the target buffer to be filled with PcdDebugClearMemoryValue.
313 @param Length The number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.
314
315 @return Buffer The pointer to the target buffer filled with PcdDebugClearMemoryValue.
316
317 **/
318 VOID *
319 EFIAPI
320 DebugClearMemory (
321 OUT VOID *Buffer,
322 IN UINTN Length
323 )
324 {
325 return Buffer;
326 }
327
328 /**
329 Returns TRUE if ASSERT() macros are enabled.
330
331 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of
332 PcdDebugPropertyMask is set. Otherwise FALSE is returned.
333
334 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugPropertyMask is set.
335 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugPropertyMask is clear.
336
337 **/
338 BOOLEAN
339 EFIAPI
340 DebugAssertEnabled (
341 VOID
342 )
343 {
344 return (BOOLEAN)((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) != 0);
345 }
346
347 /**
348 Returns TRUE if DEBUG() macros are enabled.
349
350 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of
351 PcdDebugPropertyMask is set. Otherwise FALSE is returned.
352
353 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugPropertyMask is set.
354 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugPropertyMask is clear.
355
356 **/
357 BOOLEAN
358 EFIAPI
359 DebugPrintEnabled (
360 VOID
361 )
362 {
363 return (BOOLEAN)((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED) != 0);
364 }
365
366 /**
367 Returns TRUE if DEBUG_CODE() macros are enabled.
368
369 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of
370 PcdDebugPropertyMask is set. Otherwise FALSE is returned.
371
372 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugPropertyMask is set.
373 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugPropertyMask is clear.
374
375 **/
376 BOOLEAN
377 EFIAPI
378 DebugCodeEnabled (
379 VOID
380 )
381 {
382 return (BOOLEAN)((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED) != 0);
383 }
384
385 /**
386 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.
387
388 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of
389 PcdDebugPropertyMask is set. Otherwise FALSE is returned.
390
391 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugPropertyMask is set.
392 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugPropertyMask is clear.
393
394 **/
395 BOOLEAN
396 EFIAPI
397 DebugClearMemoryEnabled (
398 VOID
399 )
400 {
401 return (BOOLEAN)((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED) != 0);
402 }
403
404 /**
405 Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.
406
407 This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.
408
409 @retval TRUE Current ErrorLevel is supported.
410 @retval FALSE Current ErrorLevel is not supported.
411
412 **/
413 BOOLEAN
414 EFIAPI
415 DebugPrintLevelEnabled (
416 IN CONST UINTN ErrorLevel
417 )
418 {
419 return (BOOLEAN)((ErrorLevel & PcdGet32 (PcdFixedDebugPrintErrorLevel)) != 0);
420 }