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