]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFrameworkModulePkg/Library/PeiDxeDebugLibReportStatusCode/DebugLib.c
Remove extra #includes where possible to make build more efficient
[mirror_edk2.git] / IntelFrameworkModulePkg / Library / PeiDxeDebugLibReportStatusCode / DebugLib.c
CommitLineData
2287f237 1/** @file\r
9ba6cd30 2 Debug Library based on report status code library.\r
2287f237 3\r
9ba6cd30 4 Copyright (c) 2006 - 2009, Intel Corporation<BR>\r
2287f237 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
9\r
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
12\r
13**/\r
14\r
2287f237 15#include <FrameworkPei.h>\r
79bc7a89 16\r
2287f237 17#include <Guid/StatusCodeDataTypeId.h>\r
3a6064fa 18#include <Guid/StatusCodeDataTypeDebug.h>\r
2287f237 19\r
20#include <Library/DebugLib.h>\r
21#include <Library/BaseLib.h>\r
22#include <Library/BaseMemoryLib.h>\r
23#include <Library/ReportStatusCodeLib.h>\r
24#include <Library/PcdLib.h>\r
25\r
2287f237 26/**\r
2287f237 27 Prints a debug message to the debug output device if the specified error level is enabled.\r
28\r
9ba6cd30 29 If any bit in ErrorLevel is also set in PcdDebugPrintErrorLevel, then print \r
30 the message specified by Format and the associated variable argument list to \r
2287f237 31 the debug output device.\r
32\r
33 If Format is NULL, then ASSERT().\r
34\r
35 @param ErrorLevel The error level of the debug message.\r
36 @param Format Format string for the debug message to print.\r
967c09fa 37 @param ... Variable argument list whose contents are accessed \r
38 based on the format string specified by Format.\r
2287f237 39\r
40**/\r
41VOID\r
42EFIAPI\r
43DebugPrint (\r
44 IN UINTN ErrorLevel,\r
45 IN CONST CHAR8 *Format,\r
46 ...\r
47 )\r
48{\r
6916d99c 49 UINT64 Buffer[(EFI_STATUS_CODE_DATA_MAX_SIZE / sizeof (UINT64)) + 1];\r
2287f237 50 EFI_DEBUG_INFO *DebugInfo;\r
51 UINTN TotalSize;\r
ca9938b8 52 VA_LIST VaListMarker;\r
53 BASE_LIST BaseListMarker;\r
54 CHAR8 *FormatString;\r
55 BOOLEAN Long;\r
56 BOOLEAN Done;\r
2287f237 57\r
58 //\r
59 // If Format is NULL, then ASSERT().\r
60 //\r
61 ASSERT (Format != NULL);\r
62\r
63 //\r
64 // Check driver Debug Level value and global debug level\r
65 //\r
9ba6cd30 66 if ((ErrorLevel & PcdGet32 (PcdDebugPrintErrorLevel)) == 0) {\r
2287f237 67 return;\r
68 }\r
69\r
ca9938b8 70 //\r
71 // Compute the total size of the record\r
72 //\r
9ba6cd30 73 TotalSize = sizeof (EFI_DEBUG_INFO) + 12 * sizeof (UINT64) + AsciiStrSize (Format);\r
ca9938b8 74\r
ca9938b8 75 //\r
76 // If the TotalSize is larger than the maximum record size, then return\r
77 //\r
1ca88083 78 if (TotalSize > sizeof (Buffer)) {\r
2287f237 79 return;\r
80 }\r
81\r
82 //\r
ca9938b8 83 // Fill in EFI_DEBUG_INFO\r
2287f237 84 //\r
9ba6cd30 85 // Here we skip the first 4 bytes of Buffer, because we must ensure BaseListMarker is\r
86 // 64-bit aligned, otherwise retrieving 64-bit parameter from BaseListMarker will cause\r
87 // exception on IPF. Buffer starts at 64-bit aligned address, so skipping 4 types (sizeof(EFI_DEBUG_INFO))\r
88 // just makes addess of BaseListMarker, which follows DebugInfo, 64-bit aligned.\r
89 //\r
6916d99c 90 DebugInfo = (EFI_DEBUG_INFO *)(Buffer) + 1;\r
2287f237 91 DebugInfo->ErrorLevel = (UINT32)ErrorLevel;\r
ca9938b8 92 BaseListMarker = (BASE_LIST)(DebugInfo + 1);\r
3d7dfb38 93 FormatString = (CHAR8 *)((UINT64 *)(DebugInfo + 1) + 12);\r
ca9938b8 94\r
95 //\r
96 // Copy the Format string into the record\r
97 //\r
98 AsciiStrCpy (FormatString, Format);\r
2287f237 99\r
100 //\r
9ba6cd30 101 // The first 12 * sizeof (UINT64) bytes following EFI_DEBUG_INFO are for variable arguments\r
102 // of format in DEBUG string, which is followed by the DEBUG format string.\r
103 // Here we will process the variable arguments and pack them in this area.\r
2287f237 104 //\r
ca9938b8 105 VA_START (VaListMarker, Format);\r
106 for (; *Format != '\0'; Format++) {\r
9ba6cd30 107 //\r
108 // Only format with prefix % is processed.\r
109 //\r
ca9938b8 110 if (*Format != '%') {\r
111 continue;\r
112 }\r
113 Long = FALSE;\r
114 //\r
115 // Parse Flags and Width\r
116 //\r
117 for (Done = FALSE; !Done; ) {\r
118 Format++;\r
119 switch (*Format) {\r
120 case '.': \r
121 case '-': \r
122 case '+': \r
123 case ' ': \r
124 case ',': \r
125 case '0':\r
126 case '1':\r
127 case '2':\r
128 case '3':\r
129 case '4':\r
130 case '5':\r
131 case '6':\r
132 case '7':\r
133 case '8':\r
134 case '9':\r
9ba6cd30 135 //\r
136 // These characters in format field are omitted.\r
137 //\r
ca9938b8 138 break;\r
139 case 'L':\r
140 case 'l': \r
9ba6cd30 141 //\r
142 // 'L" or "l" in format field means the number being printed is a UINT64\r
143 //\r
ca9938b8 144 Long = TRUE;\r
145 break;\r
146 case '*':\r
9ba6cd30 147 //\r
148 // '*' in format field means the precision of the field is specified by\r
149 // a UINTN argument in the argument list.\r
150 //\r
ca9938b8 151 BASE_ARG (BaseListMarker, UINTN) = VA_ARG (VaListMarker, UINTN);\r
152 break;\r
153 case '\0':\r
154 //\r
155 // Make no output if Format string terminates unexpectedly when\r
156 // looking up for flag, width, precision and type. \r
157 //\r
158 Format--;\r
159 //\r
160 // break skipped on purpose.\r
161 //\r
162 default:\r
9ba6cd30 163 //\r
164 // When valid argument type detected or format string terminates unexpectedly,\r
165 // the inner loop is done.\r
166 //\r
ca9938b8 167 Done = TRUE;\r
168 break;\r
169 }\r
170 } \r
171 \r
172 //\r
9ba6cd30 173 // Pack variable arguments into the storage area following EFI_DEBUG_INFO.\r
ca9938b8 174 //\r
175 switch (*Format) {\r
176 case 'p':\r
177 if (sizeof (VOID *) > 4) {\r
178 Long = TRUE;\r
179 }\r
180 case 'X':\r
181 case 'x':\r
182 case 'd':\r
183 if (Long) {\r
184 BASE_ARG (BaseListMarker, INT64) = VA_ARG (VaListMarker, INT64);\r
185 } else {\r
186 BASE_ARG (BaseListMarker, int) = VA_ARG (VaListMarker, int);\r
187 }\r
188 break;\r
189 case 's':\r
190 case 'S':\r
191 case 'a':\r
192 case 'g':\r
193 case 't':\r
194 BASE_ARG (BaseListMarker, VOID *) = VA_ARG (VaListMarker, VOID *);\r
195 break;\r
196 case 'c':\r
197 BASE_ARG (BaseListMarker, UINTN) = VA_ARG (VaListMarker, UINTN);\r
198 break;\r
199 case 'r':\r
200 BASE_ARG (BaseListMarker, RETURN_STATUS) = VA_ARG (VaListMarker, RETURN_STATUS);\r
201 break;\r
202 }\r
203\r
204 //\r
205 // If the converted BASE_LIST is larger than the 12 * sizeof (UINT64) allocated bytes, then ASSERT()\r
206 // This indicates that the DEBUG() macro is passing in more argument than can be handled by \r
207 // the EFI_DEBUG_INFO record\r
208 //\r
3d7dfb38 209 ASSERT ((CHAR8 *)BaseListMarker <= FormatString);\r
ca9938b8 210\r
211 //\r
212 // If the converted BASE_LIST is larger than the 12 * sizeof (UINT64) allocated bytes, then return\r
213 //\r
3d7dfb38 214 if ((CHAR8 *)BaseListMarker > FormatString) {\r
ca9938b8 215 return;\r
216 }\r
2287f237 217 }\r
ca9938b8 218 VA_END (VaListMarker);\r
2287f237 219\r
ca9938b8 220 //\r
221 // Send the DebugInfo record\r
222 //\r
2287f237 223 REPORT_STATUS_CODE_EX (\r
224 EFI_DEBUG_CODE,\r
225 (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_DC_UNSPECIFIED),\r
226 0,\r
227 NULL,\r
228 &gEfiStatusCodeDataTypeDebugGuid,\r
229 DebugInfo,\r
230 TotalSize\r
231 );\r
232}\r
233\r
2287f237 234/**\r
9ba6cd30 235 Prints an assert message containing a filename, line number, and description. \r
2287f237 236 This may be followed by a breakpoint or a dead loop.\r
237\r
238 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"\r
9ba6cd30 239 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of \r
240 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if \r
241 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then \r
242 CpuDeadLoop() is called. If neither of these bits are set, then this function \r
2287f237 243 returns immediately after the message is printed to the debug output device.\r
584125bc 244 DebugAssert() must actively prevent recursion. If DebugAssert() is called while\r
2287f237 245 processing another DebugAssert(), then DebugAssert() must return immediately.\r
246\r
247 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.\r
2287f237 248 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.\r
249\r
250 @param FileName Pointer to the name of the source file that generated the assert condition.\r
251 @param LineNumber The line number in the source file that generated the assert condition\r
252 @param Description Pointer to the description of the assert condition.\r
253\r
254**/\r
255VOID\r
256EFIAPI\r
257DebugAssert (\r
258 IN CONST CHAR8 *FileName,\r
259 IN UINTN LineNumber,\r
260 IN CONST CHAR8 *Description\r
261 )\r
262{\r
263 UINT64 Buffer[EFI_STATUS_CODE_DATA_MAX_SIZE / sizeof(UINT64)];\r
264 EFI_DEBUG_ASSERT_DATA *AssertData;\r
265 UINTN TotalSize;\r
266 CHAR8 *Temp;\r
9ba6cd30 267 UINTN FileNameSize;\r
268 UINTN DescriptionSize;\r
2287f237 269\r
270 //\r
271 // Make sure it will all fit in the passed in buffer\r
272 //\r
9ba6cd30 273 FileNameSize = AsciiStrSize (FileName);\r
274 DescriptionSize = AsciiStrSize (Description);\r
275 TotalSize = sizeof (EFI_DEBUG_ASSERT_DATA) + FileNameSize + DescriptionSize;\r
1ca88083 276 if (TotalSize <= sizeof (Buffer)) {\r
2287f237 277 //\r
278 // Fill in EFI_DEBUG_ASSERT_DATA\r
279 //\r
280 AssertData = (EFI_DEBUG_ASSERT_DATA *)Buffer;\r
281 AssertData->LineNumber = (UINT32)LineNumber;\r
282\r
283 //\r
284 // Copy Ascii FileName including NULL.\r
285 //\r
286 Temp = AsciiStrCpy ((CHAR8 *)(AssertData + 1), FileName);\r
287\r
288 //\r
289 // Copy Ascii Description\r
290 //\r
9ba6cd30 291 AsciiStrCpy (Temp + FileNameSize, Description);\r
2287f237 292\r
8191cd5e 293 REPORT_STATUS_CODE_EX (\r
2287f237 294 (EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED),\r
295 (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_EC_ILLEGAL_SOFTWARE_STATE),\r
8191cd5e
LG
296 0,\r
297 NULL,\r
1872ccab 298 NULL,\r
2287f237 299 AssertData,\r
300 TotalSize\r
301 );\r
302 }\r
303\r
304 //\r
305 // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings\r
306 //\r
9ba6cd30 307 if ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED) != 0) {\r
2287f237 308 CpuBreakpoint ();\r
9ba6cd30 309 } else if ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED) != 0) {\r
2287f237 310 CpuDeadLoop ();\r
311 }\r
312}\r
313\r
314\r
315/**\r
2287f237 316 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.\r
317\r
9ba6cd30 318 This function fills Length bytes of Buffer with the value specified by \r
2287f237 319 PcdDebugClearMemoryValue, and returns Buffer.\r
320\r
321 If Buffer is NULL, then ASSERT().\r
9ba6cd30 322 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r
2287f237 323\r
9ba6cd30 324 @param Buffer Pointer to the target buffer to be filled with PcdDebugClearMemoryValue.\r
325 @param Length Number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue. \r
2287f237 326\r
9ba6cd30 327 @return Buffer Pointer to the target buffer filled with PcdDebugClearMemoryValue.\r
2287f237 328\r
329**/\r
330VOID *\r
331EFIAPI\r
332DebugClearMemory (\r
333 OUT VOID *Buffer,\r
334 IN UINTN Length\r
335 )\r
336{\r
2287f237 337 ASSERT (Buffer != NULL);\r
338\r
9ba6cd30 339 return SetMem (Buffer, Length, PcdGet8 (PcdDebugClearMemoryValue));\r
2287f237 340}\r
341\r
342\r
343/**\r
2287f237 344 Returns TRUE if ASSERT() macros are enabled.\r
345\r
9ba6cd30 346 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of \r
2287f237 347 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
348\r
349 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.\r
350 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.\r
351\r
352**/\r
353BOOLEAN\r
354EFIAPI\r
355DebugAssertEnabled (\r
356 VOID\r
357 )\r
358{\r
9ba6cd30 359 return (BOOLEAN) ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) != 0);\r
2287f237 360}\r
361\r
362\r
9ba6cd30 363/** \r
364 Returns TRUE if DEBUG() macros are enabled.\r
2287f237 365\r
9ba6cd30 366 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of \r
2287f237 367 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
368\r
369 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.\r
370 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.\r
371\r
372**/\r
373BOOLEAN\r
374EFIAPI\r
375DebugPrintEnabled (\r
376 VOID\r
377 )\r
378{\r
9ba6cd30 379 return (BOOLEAN) ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED) != 0);\r
2287f237 380}\r
381\r
382\r
9ba6cd30 383/** \r
384 Returns TRUE if DEBUG_CODE() macros are enabled.\r
2287f237 385\r
9ba6cd30 386 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of \r
2287f237 387 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
388\r
389 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.\r
390 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.\r
391\r
392**/\r
393BOOLEAN\r
394EFIAPI\r
395DebugCodeEnabled (\r
396 VOID\r
397 )\r
398{\r
9ba6cd30 399 return (BOOLEAN) ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED) != 0);\r
2287f237 400}\r
401\r
402\r
9ba6cd30 403/** \r
404 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.\r
2287f237 405\r
9ba6cd30 406 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of \r
2287f237 407 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
408\r
9ba6cd30 409 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.\r
410 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.\r
2287f237 411\r
412**/\r
413BOOLEAN\r
414EFIAPI\r
415DebugClearMemoryEnabled (\r
416 VOID\r
417 )\r
418{\r
9ba6cd30 419 return (BOOLEAN) ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED) != 0);\r
2287f237 420}\r