]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFspPkg/Library/BaseFspDebugLibSerialPort/DebugLib.c
17688c7fcb9869616ed5c5fdd123a882f8435fb3
[mirror_edk2.git] / IntelFspPkg / 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 /**
44 Prints a debug message to the debug output device if the specified error level is enabled.
45
46 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
47 GetDebugPrintErrorLevel (), then print the message specified by Format and the
48 associated variable argument list to the debug output device.
49
50 If Format is NULL, then ASSERT().
51
52 @param ErrorLevel The error level of the debug message.
53 @param Format Format string for the debug message to print.
54 @param ... Variable argument list whose contents are accessed
55 based on the format string specified by Format.
56
57 **/
58 VOID
59 EFIAPI
60 DebugPrint (
61 IN UINTN ErrorLevel,
62 IN CONST CHAR8 *Format,
63 ...
64 )
65 {
66 VA_LIST Marker;
67
68 VA_START (Marker, Format);
69 DebugVPrint (ErrorLevel, Format, Marker);
70 VA_END (Marker);
71 }
72
73 /**
74 Prints a debug message to the debug output device if the specified
75 error level is enabled base on Null-terminated format string and a
76 VA_LIST argument list or a BASE_LIST argument list.
77
78 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
79 GetDebugPrintErrorLevel (), then print the message specified by Format and
80 the associated variable argument list to the debug output device.
81
82 If Format is NULL, then ASSERT().
83
84 @param ErrorLevel The error level of the debug message.
85 @param Format Format string for the debug message to print.
86 @param VaListMarker VA_LIST marker for the variable argument list.
87 @param BaseListMarker BASE_LIST marker for the variable argument list.
88
89 **/
90 VOID
91 DebugPrintMarker (
92 IN UINTN ErrorLevel,
93 IN CONST CHAR8 *Format,
94 IN VA_LIST VaListMarker,
95 IN BASE_LIST BaseListMarker
96 )
97 {
98 CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];
99
100 //
101 // If Format is NULL, then ASSERT().
102 //
103 if (!GetDebugPrintDeviceEnable ()) {
104 return;
105 }
106
107 //
108 // Check driver debug mask value and global mask
109 //
110 if ((ErrorLevel & GetDebugPrintErrorLevel ()) == 0) {
111 return;
112 }
113
114 //
115 // If Format is NULL, then ASSERT().
116 //
117 ASSERT (Format != NULL);
118
119 //
120 // Convert the DEBUG() message to an ASCII String
121 //
122 if (BaseListMarker == NULL) {
123 AsciiVSPrint (Buffer, sizeof (Buffer), Format, VaListMarker);
124 } else {
125 AsciiBSPrint (Buffer, sizeof (Buffer), Format, BaseListMarker);
126 }
127
128 //
129 // Send the print string to a Serial Port
130 //
131 SerialPortWrite ((UINT8 *)Buffer, AsciiStrLen (Buffer));
132 }
133
134 /**
135 Prints a debug message to the debug output device if the specified
136 error level is enabled.
137
138 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
139 GetDebugPrintErrorLevel (), then print the message specified by Format and
140 the associated variable argument list to the debug output device.
141
142 If Format is NULL, then ASSERT().
143
144 @param ErrorLevel The error level of the debug message.
145 @param Format Format string for the debug message to print.
146 @param VaListMarker VA_LIST marker for the variable argument list.
147
148 **/
149 VOID
150 EFIAPI
151 DebugVPrint (
152 IN UINTN ErrorLevel,
153 IN CONST CHAR8 *Format,
154 IN VA_LIST VaListMarker
155 )
156 {
157 DebugPrintMarker (ErrorLevel, Format, VaListMarker, NULL);
158 }
159
160 /**
161 Prints a debug message to the debug output device if the specified
162 error level is enabled.
163 This function use BASE_LIST which would provide a more compatible
164 service than VA_LIST.
165
166 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
167 GetDebugPrintErrorLevel (), then print the message specified by Format and
168 the associated variable argument list to the debug output device.
169
170 If Format is NULL, then ASSERT().
171
172 @param ErrorLevel The error level of the debug message.
173 @param Format Format string for the debug message to print.
174 @param BaseListMarker BASE_LIST marker for the variable argument list.
175
176 **/
177 VOID
178 EFIAPI
179 DebugBPrint (
180 IN UINTN ErrorLevel,
181 IN CONST CHAR8 *Format,
182 IN BASE_LIST BaseListMarker
183 )
184 {
185 DebugPrintMarker (ErrorLevel, Format, mVaListNull, BaseListMarker);
186 }
187
188 /**
189 Convert an UINT32 value into HEX string sepcified by Buffer.
190
191 @param Value The HEX value to convert to string
192 @param Buffer The pointer to the target buffer to be filled with HEX string
193
194 **/
195 VOID
196 FillHex (
197 UINT32 Value,
198 CHAR8 *Buffer
199 )
200 {
201 INTN Idx;
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 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if
215 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask 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 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if
269 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask 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 /**
296 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.
297
298 This function fills Length bytes of Buffer with the value specified by
299 PcdDebugClearMemoryValue, and returns Buffer.
300
301 If Buffer is NULL, then ASSERT().
302 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
303
304 @param Buffer The pointer to the target buffer to be filled with PcdDebugClearMemoryValue.
305 @param Length The number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.
306
307 @return Buffer The pointer to the target buffer filled with PcdDebugClearMemoryValue.
308
309 **/
310 VOID *
311 EFIAPI
312 DebugClearMemory (
313 OUT VOID *Buffer,
314 IN UINTN Length
315 )
316 {
317 return Buffer;
318 }
319
320
321 /**
322 Returns TRUE if ASSERT() macros are enabled.
323
324 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of
325 PcdDebugProperyMask is set. Otherwise FALSE is returned.
326
327 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.
328 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.
329
330 **/
331 BOOLEAN
332 EFIAPI
333 DebugAssertEnabled (
334 VOID
335 )
336 {
337 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) != 0);
338 }
339
340
341 /**
342 Returns TRUE if DEBUG() macros are enabled.
343
344 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of
345 PcdDebugProperyMask is set. Otherwise FALSE is returned.
346
347 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.
348 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.
349
350 **/
351 BOOLEAN
352 EFIAPI
353 DebugPrintEnabled (
354 VOID
355 )
356 {
357 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED) != 0);
358 }
359
360 /**
361 Returns TRUE if DEBUG_CODE() macros are enabled.
362
363 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of
364 PcdDebugProperyMask is set. Otherwise FALSE is returned.
365
366 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.
367 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.
368
369 **/
370 BOOLEAN
371 EFIAPI
372 DebugCodeEnabled (
373 VOID
374 )
375 {
376 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED) != 0);
377 }
378
379
380 /**
381 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.
382
383 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of
384 PcdDebugProperyMask is set. Otherwise FALSE is returned.
385
386 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.
387 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.
388
389 **/
390 BOOLEAN
391 EFIAPI
392 DebugClearMemoryEnabled (
393 VOID
394 )
395 {
396 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED) != 0);
397 }
398
399 /**
400 Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.
401
402 This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.
403
404 @retval TRUE Current ErrorLevel is supported.
405 @retval FALSE Current ErrorLevel is not supported.
406
407 **/
408 BOOLEAN
409 EFIAPI
410 DebugPrintLevelEnabled (
411 IN CONST UINTN ErrorLevel
412 )
413 {
414 return (BOOLEAN) ((ErrorLevel & PcdGet32(PcdFixedDebugPrintErrorLevel)) != 0);
415 }