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