]> git.proxmox.com Git - mirror_edk2.git/blame - EdkModulePkg/Library/EdkDxeDebugLibReportStatusCode/DebugLib.c
Sync with PeiDxeDebugLibReportStatusCodeLib to fix IPF alignment issue.
[mirror_edk2.git] / EdkModulePkg / Library / EdkDxeDebugLibReportStatusCode / DebugLib.c
CommitLineData
add13dc2 1/** @file\r
2 EFI Debug Library that installs Debug Level Protocol.\r
878ddf1f 3\r
add13dc2 4 Copyright (c) 2006, Intel Corporation<BR>\r
5 All rights reserved. This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
878ddf1f 9\r
add13dc2 10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
878ddf1f 12\r
add13dc2 13**/\r
878ddf1f 14\r
add13dc2 15STATIC BOOLEAN mDebugLevelInstalled = FALSE;\r
16STATIC EFI_DEBUG_LEVEL_PROTOCOL mDebugLevel = { 0 };\r
878ddf1f 17\r
add13dc2 18/**\r
19 Installs Debug Level Protocol.\r
20 \r
21 The constructor function installs Debug Level Protocol on the ImageHandle.\r
22 It will ASSERT() if the installation fails and will always return EFI_SUCCESS. \r
878ddf1f 23\r
add13dc2 24 @param ImageHandle The firmware allocated handle for the EFI image.\r
25 @param SystemTable A pointer to the EFI System Table.\r
26 \r
27 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.\r
878ddf1f 28\r
add13dc2 29**/\r
878ddf1f 30EFI_STATUS\r
add13dc2 31EFIAPI\r
878ddf1f 32DebugLibConstructor (\r
33 IN EFI_HANDLE ImageHandle,\r
34 IN EFI_SYSTEM_TABLE *SystemTable\r
35 )\r
878ddf1f 36{\r
add13dc2 37 EFI_STATUS Status;\r
878ddf1f 38\r
39 //\r
add13dc2 40 // Initialize Debug Level Protocol.\r
878ddf1f 41 //\r
42 mDebugLevel.DebugLevel = PcdGet32(PcdDebugPrintErrorLevel);\r
43\r
44 //\r
add13dc2 45 // Install Debug Level Protocol. \r
878ddf1f 46 //\r
47 Status = gBS->InstallMultipleProtocolInterfaces (\r
48 &ImageHandle,\r
add13dc2 49 &gEfiDebugLevelProtocolGuid,\r
50 &mDebugLevel,\r
878ddf1f 51 NULL\r
52 );\r
53 ASSERT_EFI_ERROR (Status);\r
54\r
55 //\r
add13dc2 56 // Set flag to show that the Debug Level Protocol has been installed.\r
878ddf1f 57 //\r
58 mDebugLevelInstalled = TRUE;\r
59\r
add13dc2 60 return Status;\r
878ddf1f 61}\r
62\r
add13dc2 63/**\r
878ddf1f 64\r
add13dc2 65 Prints a debug message to the debug output device if the specified error level is enabled.\r
878ddf1f 66\r
add13dc2 67 If any bit in ErrorLevel is also set in PcdDebugPrintErrorLevel, then print \r
68 the message specified by Format and the associated variable argument list to \r
69 the debug output device.\r
878ddf1f 70\r
add13dc2 71 If Format is NULL, then ASSERT().\r
878ddf1f 72\r
add13dc2 73 @param ErrorLevel The error level of the debug message.\r
74 @param Format Format string for the debug message to print.\r
878ddf1f 75\r
add13dc2 76**/\r
878ddf1f 77VOID\r
add13dc2 78EFIAPI\r
79DebugPrint (\r
80 IN UINTN ErrorLevel,\r
81 IN CONST CHAR8 *Format,\r
82 ...\r
878ddf1f 83 )\r
878ddf1f 84{\r
85 UINT64 Buffer[EFI_STATUS_CODE_DATA_MAX_SIZE / sizeof (UINT64)];\r
86 EFI_DEBUG_INFO *DebugInfo;\r
87 UINTN TotalSize;\r
88 UINTN Index;\r
add13dc2 89 VA_LIST Marker;\r
878ddf1f 90 UINT64 *ArgumentPointer;\r
91\r
add13dc2 92 //\r
93 // If Format is NULL, then ASSERT().\r
94 //\r
95 ASSERT (Format != NULL);\r
96\r
878ddf1f 97 //\r
98 // Check driver Debug Level value and global debug level\r
99 //\r
100 if (mDebugLevelInstalled) {\r
101 if ((ErrorLevel & mDebugLevel.DebugLevel) == 0) {\r
102 return;\r
103 }\r
104 } else {\r
105 if ((ErrorLevel & PcdGet32(PcdDebugPrintErrorLevel)) == 0) {\r
106 return;\r
107 }\r
108 }\r
109\r
add13dc2 110 TotalSize = sizeof (EFI_DEBUG_INFO) + 12 * sizeof (UINT64) + AsciiStrLen (Format) + 1;\r
878ddf1f 111 if (TotalSize > EFI_STATUS_CODE_DATA_MAX_SIZE) {\r
112 return;\r
113 }\r
114\r
115 //\r
116 // Then EFI_DEBUG_INFO\r
117 //\r
118 DebugInfo = (EFI_DEBUG_INFO *)Buffer;\r
119 DebugInfo->ErrorLevel = (UINT32)ErrorLevel;\r
120\r
121 //\r
122 // 256 byte mini Var Arg stack. That is followed by the format string.\r
123 //\r
add13dc2 124 VA_START (Marker, Format);\r
878ddf1f 125 for (Index = 0, ArgumentPointer = (UINT64 *)(DebugInfo + 1); Index < 12; Index++, ArgumentPointer++) {\r
b672349a 126 WriteUnaligned64(ArgumentPointer, VA_ARG (Marker, UINT64));\r
878ddf1f 127 }\r
add13dc2 128 VA_END (Marker);\r
878ddf1f 129 AsciiStrCpy ((CHAR8 *)ArgumentPointer, Format);\r
130\r
add13dc2 131 REPORT_STATUS_CODE_EX (\r
878ddf1f 132 EFI_DEBUG_CODE,\r
133 (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_DC_UNSPECIFIED),\r
add13dc2 134 0,\r
135 NULL,\r
136 &gEfiStatusCodeDataTypeDebugGuid,\r
878ddf1f 137 DebugInfo,\r
138 TotalSize\r
139 );\r
140}\r
141\r
878ddf1f 142\r
add13dc2 143/**\r
878ddf1f 144\r
add13dc2 145 Prints an assert message containing a filename, line number, and description. \r
146 This may be followed by a breakpoint or a dead loop.\r
878ddf1f 147\r
add13dc2 148 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n" \r
149 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of \r
150 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if \r
151 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then \r
152 CpuDeadLoop() is called. If neither of these bits are set, then this function \r
153 returns immediately after the message is printed to the debug output device.\r
154 DebugAssert() must actively prevent recusrsion. If DebugAssert() is called while\r
155 processing another DebugAssert(), then DebugAssert() must return immediately.\r
878ddf1f 156\r
add13dc2 157 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.\r
878ddf1f 158\r
add13dc2 159 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.\r
878ddf1f 160\r
add13dc2 161 @param FileName Pointer to the name of the source file that generated the assert condition.\r
162 @param LineNumber The line number in the source file that generated the assert condition\r
163 @param Description Pointer to the description of the assert condition.\r
878ddf1f 164\r
add13dc2 165**/\r
166VOID\r
167EFIAPI\r
168DebugAssert (\r
169 IN CONST CHAR8 *FileName,\r
170 IN UINTN LineNumber,\r
171 IN CONST CHAR8 *Description\r
172 )\r
878ddf1f 173{\r
add13dc2 174 UINT64 Buffer[EFI_STATUS_CODE_DATA_MAX_SIZE / sizeof(UINT64)];\r
175 EFI_DEBUG_ASSERT_DATA *AssertData;\r
176 UINTN TotalSize;\r
177 CHAR8 *Temp;\r
878ddf1f 178\r
add13dc2 179 //\r
180 // Make sure it will all fit in the passed in buffer.\r
181 //\r
182 TotalSize = sizeof (EFI_DEBUG_ASSERT_DATA) + AsciiStrLen (FileName) + 1 + AsciiStrLen (Description) + 1;\r
183 if (TotalSize <= EFI_STATUS_CODE_DATA_MAX_SIZE) {\r
184 //\r
185 // Fill in EFI_DEBUG_ASSERT_DATA\r
186 //\r
187 AssertData = (EFI_DEBUG_ASSERT_DATA *)Buffer;\r
188 AssertData->LineNumber = (UINT32)LineNumber;\r
189\r
190 //\r
191 // Copy Ascii FileName including NULL.\r
192 //\r
193 Temp = AsciiStrCpy ((CHAR8 *)(AssertData + 1), FileName);\r
194\r
195 //\r
196 // Copy Ascii Description. \r
197 //\r
198 AsciiStrCpy (Temp + AsciiStrLen (FileName) + 1, Description);\r
199\r
200 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (\r
201 (EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED),\r
202 (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_EC_ILLEGAL_SOFTWARE_STATE),\r
203 AssertData,\r
204 TotalSize\r
205 );\r
206 }\r
207\r
208 //\r
209 // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings\r
210 //\r
211 if ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED) != 0) {\r
212 CpuBreakpoint ();\r
213 } else if ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED) != 0) {\r
214 CpuDeadLoop ();\r
215 }\r
878ddf1f 216}\r
217\r
add13dc2 218\r
878ddf1f 219/**\r
add13dc2 220\r
878ddf1f 221 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.\r
222\r
223 This function fills Length bytes of Buffer with the value specified by \r
224 PcdDebugClearMemoryValue, and returns Buffer.\r
225\r
226 If Buffer is NULL, then ASSERT().\r
227\r
511710d6 228 If Length is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT(). \r
878ddf1f 229\r
add13dc2 230 @param Buffer Pointer to the target buffer to fill with PcdDebugClearMemoryValue.\r
231 @param Length Number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue. \r
878ddf1f 232\r
233 @return Buffer\r
234\r
235**/\r
236VOID *\r
237EFIAPI\r
238DebugClearMemory (\r
239 OUT VOID *Buffer,\r
240 IN UINTN Length\r
241 )\r
242{\r
add13dc2 243 //\r
244 // If Buffer is NULL, then ASSERT().\r
245 //\r
246 ASSERT (Buffer != NULL);\r
247\r
248 //\r
249 // SetMem() checks for the the ASSERT() condition on Length and returns Buffer\r
250 //\r
251 return SetMem (Buffer, Length, PcdGet8(PcdDebugClearMemoryValue));\r
878ddf1f 252}\r
253\r
add13dc2 254\r
255/**\r
256 \r
257 Returns TRUE if ASSERT() macros are enabled.\r
258\r
259 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of \r
260 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
261\r
262 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.\r
263 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.\r
264\r
265**/\r
878ddf1f 266BOOLEAN\r
267EFIAPI\r
268DebugAssertEnabled (\r
269 VOID\r
270 )\r
271{\r
272 return ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) != 0);\r
273}\r
274\r
add13dc2 275\r
276/**\r
277 \r
278 Returns TRUE if DEBUG()macros are enabled.\r
279\r
280 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of \r
281 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
282\r
283 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.\r
284 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.\r
285\r
286**/\r
878ddf1f 287BOOLEAN\r
288EFIAPI\r
289DebugPrintEnabled (\r
290 VOID\r
291 )\r
292{\r
293 return ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED) != 0);\r
294}\r
295\r
add13dc2 296\r
297/**\r
298 \r
299 Returns TRUE if DEBUG_CODE()macros are enabled.\r
300\r
301 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of \r
302 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
303\r
304 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.\r
305 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.\r
306\r
307**/\r
878ddf1f 308BOOLEAN\r
309EFIAPI\r
310DebugCodeEnabled (\r
311 VOID\r
312 )\r
313{\r
314 return ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED) != 0);\r
315}\r
316\r
add13dc2 317\r
318/**\r
319 \r
320 Returns TRUE if DEBUG_CLEAR_MEMORY()macro is enabled.\r
321\r
322 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CLEAR_MEMORY_ENABLED bit of \r
323 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
324\r
325 @retval TRUE The DEBUG_PROPERTY_DEBUG_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.\r
326 @retval FALSE The DEBUG_PROPERTY_DEBUG_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.\r
327\r
328**/\r
878ddf1f 329BOOLEAN\r
330EFIAPI\r
331DebugClearMemoryEnabled (\r
332 VOID\r
333 )\r
334{\r
335 return ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED) != 0);\r
336}\r