Eliminate duplicated file GUID.
[mirror_edk2.git] / IntelFspPkg / Library / BaseFspDebugLibSerialPort / DebugLib.c
1 /** @file
2
3 Copyright (c) 2014, 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 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 CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];
67 VA_LIST Marker;
68
69 //
70 // If Format is NULL, then ASSERT().
71 //
72 if (!GetDebugPrintDeviceEnable ()) {
73 return;
74 }
75
76 //
77 // Check driver debug mask value and global mask
78 //
79 if ((ErrorLevel & GetDebugPrintErrorLevel ()) == 0) {
80 return;
81 }
82
83 //
84 // If Format is NULL, then ASSERT().
85 //
86 ASSERT (Format != NULL);
87
88 //
89 // Convert the DEBUG() message to an ASCII String
90 //
91 VA_START (Marker, Format);
92 AsciiVSPrint (Buffer, sizeof (Buffer), Format, Marker);
93 VA_END (Marker);
94
95 //
96 // Send the print string to a Serial Port
97 //
98 SerialPortWrite ((UINT8 *)Buffer, AsciiStrLen (Buffer));
99 }
100
101 /**
102 Convert an UINT32 value into HEX string sepcified by Buffer.
103
104 @param Value The HEX value to convert to string
105 @param Buffer The pointer to the target buffer to be filled with HEX string
106
107 **/
108 VOID
109 FillHex (
110 UINT32 Value,
111 CHAR8 *Buffer
112 )
113 {
114 INTN Idx;
115 for (Idx = 7; Idx >= 0; Idx--) {
116 Buffer[Idx] = mHexTable[Value & 0x0F];
117 Value >>= 4;
118 }
119 }
120
121 /**
122 Prints an assert message containing a filename, line number, and description.
123 This may be followed by a breakpoint or a dead loop.
124
125 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"
126 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of
127 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if
128 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then
129 CpuDeadLoop() is called. If neither of these bits are set, then this function
130 returns immediately after the message is printed to the debug output device.
131 DebugAssert() must actively prevent recursion. If DebugAssert() is called while
132 processing another DebugAssert(), then DebugAssert() must return immediately.
133
134 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.
135 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.
136
137 **/
138 VOID
139 DebugAssertInternal (
140 VOID
141 )
142 {
143 CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];
144 UINT32 *Frame;
145
146 Frame = (UINT32 *)GetStackFramePointer ();
147
148 //
149 // Generate the ASSERT() message in Ascii format
150 //
151 AsciiStrCpy (Buffer, "-> EBP:0x00000000 EIP:0x00000000\n");
152 SerialPortWrite ((UINT8 *)"ASSERT DUMP:\n", 13);
153 while (Frame != NULL) {
154 FillHex ((UINT32)Frame, Buffer + 9);
155 FillHex (Frame[1], Buffer + 9 + 8 + 8);
156 SerialPortWrite ((UINT8 *)Buffer, AsciiStrLen (Buffer));
157 if ((Frame[0] > (UINT32)Frame) && (Frame[0] < (UINT32)Frame + 0x00100000)) {
158 Frame = (UINT32 *)Frame[0];
159 } else {
160 Frame = NULL;
161 }
162 }
163
164 //
165 // Dead loop
166 //
167 CpuDeadLoop ();
168 }
169
170 /**
171 Prints an assert message containing a filename, line number, and description.
172 This may be followed by a breakpoint or a dead loop.
173
174 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"
175 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of
176 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if
177 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then
178 CpuDeadLoop() is called. If neither of these bits are set, then this function
179 returns immediately after the message is printed to the debug output device.
180 DebugAssert() must actively prevent recursion. If DebugAssert() is called while
181 processing another DebugAssert(), then DebugAssert() must return immediately.
182
183 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.
184 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.
185
186 @param FileName The pointer to the name of the source file that generated the assert condition.
187 @param LineNumber The line number in the source file that generated the assert condition
188 @param Description The pointer to the description of the assert condition.
189
190 **/
191 VOID
192 EFIAPI
193 DebugAssert (
194 IN CONST CHAR8 *FileName,
195 IN UINTN LineNumber,
196 IN CONST CHAR8 *Description
197 )
198 {
199 DebugAssertInternal ();
200 }
201
202
203 /**
204 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.
205
206 This function fills Length bytes of Buffer with the value specified by
207 PcdDebugClearMemoryValue, and returns Buffer.
208
209 If Buffer is NULL, then ASSERT().
210 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
211
212 @param Buffer The pointer to the target buffer to be filled with PcdDebugClearMemoryValue.
213 @param Length The number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.
214
215 @return Buffer The pointer to the target buffer filled with PcdDebugClearMemoryValue.
216
217 **/
218 VOID *
219 EFIAPI
220 DebugClearMemory (
221 OUT VOID *Buffer,
222 IN UINTN Length
223 )
224 {
225 return Buffer;
226 }
227
228
229 /**
230 Returns TRUE if ASSERT() macros are enabled.
231
232 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of
233 PcdDebugProperyMask is set. Otherwise FALSE is returned.
234
235 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.
236 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.
237
238 **/
239 BOOLEAN
240 EFIAPI
241 DebugAssertEnabled (
242 VOID
243 )
244 {
245 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) != 0);
246 }
247
248
249 /**
250 Returns TRUE if DEBUG() macros are enabled.
251
252 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of
253 PcdDebugProperyMask is set. Otherwise FALSE is returned.
254
255 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.
256 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.
257
258 **/
259 BOOLEAN
260 EFIAPI
261 DebugPrintEnabled (
262 VOID
263 )
264 {
265 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED) != 0);
266 }
267
268 /**
269 Returns TRUE if DEBUG_CODE() macros are enabled.
270
271 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of
272 PcdDebugProperyMask is set. Otherwise FALSE is returned.
273
274 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.
275 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.
276
277 **/
278 BOOLEAN
279 EFIAPI
280 DebugCodeEnabled (
281 VOID
282 )
283 {
284 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED) != 0);
285 }
286
287
288 /**
289 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.
290
291 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of
292 PcdDebugProperyMask is set. Otherwise FALSE is returned.
293
294 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.
295 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.
296
297 **/
298 BOOLEAN
299 EFIAPI
300 DebugClearMemoryEnabled (
301 VOID
302 )
303 {
304 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED) != 0);
305 }