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