]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/PeiDxeDebugLibReportStatusCode/DebugLib.c
MdeModulePkg/FaultTolerantWriteDxe: implement standalone MM version
[mirror_edk2.git] / MdeModulePkg / Library / PeiDxeDebugLibReportStatusCode / DebugLib.c
CommitLineData
11ff2c71
LG
1/** @file\r
2 Debug Library based on report status code library.\r
3\r
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
137ed155 7 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
11ff2c71
LG
8 This program and the accompanying materials\r
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
18#include <PiPei.h>\r
19\r
20#include <Guid/StatusCodeDataTypeId.h>\r
21#include <Guid/StatusCodeDataTypeDebug.h>\r
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
28#include <Library/DebugPrintErrorLevelLib.h>\r
29\r
30/**\r
31 Prints a debug message to the debug output device if the specified error level is enabled.\r
32\r
d1102dba
LG
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
11ff2c71
LG
35 associated variable argument list to the debug output device.\r
36\r
37 If Format is NULL, then ASSERT().\r
38\r
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
42 @param ErrorLevel The error level of the debug message.\r
43 @param Format Format string for the debug message to print.\r
d1102dba 44 @param ... Variable argument list whose contents are accessed\r
11ff2c71
LG
45 based on the format string specified by Format.\r
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
56 UINT64 Buffer[(EFI_STATUS_CODE_DATA_MAX_SIZE / sizeof (UINT64)) + 1];\r
57 EFI_DEBUG_INFO *DebugInfo;\r
58 UINTN TotalSize;\r
59 VA_LIST VaListMarker;\r
60 BASE_LIST BaseListMarker;\r
61 CHAR8 *FormatString;\r
62 BOOLEAN Long;\r
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
72 if ((ErrorLevel & GetDebugPrintErrorLevel ()) == 0) {\r
73 return;\r
74 }\r
75\r
76 //\r
77 // Compute the total size of the record.\r
d1102dba 78 // Note that the passing-in format string and variable parameters will be constructed to\r
11ff2c71
LG
79 // the following layout:\r
80 //\r
81 // Buffer->|------------------------|\r
82 // | Padding | 4 bytes\r
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
94\r
95 //\r
96 // If the TotalSize is larger than the maximum record size, then return\r
97 //\r
98 if (TotalSize > sizeof (Buffer)) {\r
137ed155 99 TotalSize = sizeof (Buffer);\r
11ff2c71
LG
100 }\r
101\r
102 //\r
103 // Fill in EFI_DEBUG_INFO\r
104 //\r
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
108 // just makes address of BaseListMarker, which follows DebugInfo, 64-bit aligned.\r
109 //\r
110 DebugInfo = (EFI_DEBUG_INFO *)(Buffer) + 1;\r
111 DebugInfo->ErrorLevel = (UINT32)ErrorLevel;\r
112 BaseListMarker = (BASE_LIST)(DebugInfo + 1);\r
113 FormatString = (CHAR8 *)((UINT64 *)(DebugInfo + 1) + 12);\r
114\r
115 //\r
137ed155 116 // Copy the Format string into the record. It will be truncated if it's too long.\r
11ff2c71 117 //\r
137ed155
RN
118 AsciiStrnCpyS (\r
119 FormatString, sizeof(Buffer) - (4 + sizeof(EFI_DEBUG_INFO) + 12 * sizeof(UINT64)),\r
120 Format, sizeof(Buffer) - (4 + sizeof(EFI_DEBUG_INFO) + 12 * sizeof(UINT64)) - 1\r
121 );\r
11ff2c71
LG
122\r
123 //\r
124 // The first 12 * sizeof (UINT64) bytes following EFI_DEBUG_INFO are for variable arguments\r
125 // of format in DEBUG string, which is followed by the DEBUG format string.\r
126 // Here we will process the variable arguments and pack them in this area.\r
127 //\r
128 VA_START (VaListMarker, Format);\r
a7119c81
RN
129\r
130 //\r
131 // Use the actual format string.\r
132 //\r
133 Format = FormatString;\r
11ff2c71
LG
134 for (; *Format != '\0'; Format++) {\r
135 //\r
136 // Only format with prefix % is processed.\r
137 //\r
138 if (*Format != '%') {\r
139 continue;\r
140 }\r
141 Long = FALSE;\r
142 //\r
143 // Parse Flags and Width\r
144 //\r
145 for (Format++; TRUE; Format++) {\r
146 if (*Format == '.' || *Format == '-' || *Format == '+' || *Format == ' ') {\r
147 //\r
148 // These characters in format field are omitted.\r
149 //\r
150 continue;\r
151 }\r
152 if (*Format >= '0' && *Format <= '9') {\r
153 //\r
154 // These characters in format field are omitted.\r
155 //\r
156 continue;\r
157 }\r
158 if (*Format == 'L' || *Format == 'l') {\r
159 //\r
160 // 'L" or "l" in format field means the number being printed is a UINT64\r
161 //\r
162 Long = TRUE;\r
163 continue;\r
164 }\r
165 if (*Format == '*') {\r
166 //\r
167 // '*' in format field means the precision of the field is specified by\r
168 // a UINTN argument in the argument list.\r
169 //\r
170 BASE_ARG (BaseListMarker, UINTN) = VA_ARG (VaListMarker, UINTN);\r
171 continue;\r
172 }\r
173 if (*Format == '\0') {\r
174 //\r
175 // Make no output if Format string terminates unexpectedly when\r
d1102dba 176 // looking up for flag, width, precision and type.\r
11ff2c71
LG
177 //\r
178 Format--;\r
179 }\r
180 //\r
181 // When valid argument type detected or format string terminates unexpectedly,\r
182 // the inner loop is done.\r
183 //\r
184 break;\r
185 }\r
d1102dba 186\r
11ff2c71
LG
187 //\r
188 // Pack variable arguments into the storage area following EFI_DEBUG_INFO.\r
189 //\r
190 if ((*Format == 'p') && (sizeof (VOID *) > 4)) {\r
191 Long = TRUE;\r
192 }\r
796fecd8 193 if (*Format == 'p' || *Format == 'X' || *Format == 'x' || *Format == 'd' || *Format == 'u') {\r
11ff2c71
LG
194 if (Long) {\r
195 BASE_ARG (BaseListMarker, INT64) = VA_ARG (VaListMarker, INT64);\r
196 } else {\r
197 BASE_ARG (BaseListMarker, int) = VA_ARG (VaListMarker, int);\r
198 }\r
199 } else if (*Format == 's' || *Format == 'S' || *Format == 'a' || *Format == 'g' || *Format == 't') {\r
200 BASE_ARG (BaseListMarker, VOID *) = VA_ARG (VaListMarker, VOID *);\r
201 } else if (*Format == 'c') {\r
202 BASE_ARG (BaseListMarker, UINTN) = VA_ARG (VaListMarker, UINTN);\r
203 } else if (*Format == 'r') {\r
204 BASE_ARG (BaseListMarker, RETURN_STATUS) = VA_ARG (VaListMarker, RETURN_STATUS);\r
205 }\r
206\r
207 //\r
208 // If the converted BASE_LIST is larger than the 12 * sizeof (UINT64) allocated bytes, then ASSERT()\r
d1102dba 209 // This indicates that the DEBUG() macro is passing in more argument than can be handled by\r
11ff2c71
LG
210 // the EFI_DEBUG_INFO record\r
211 //\r
212 ASSERT ((CHAR8 *)BaseListMarker <= FormatString);\r
213\r
214 //\r
215 // If the converted BASE_LIST is larger than the 12 * sizeof (UINT64) allocated bytes, then return\r
216 //\r
217 if ((CHAR8 *)BaseListMarker > FormatString) {\r
218 VA_END (VaListMarker);\r
219 return;\r
220 }\r
221 }\r
222 VA_END (VaListMarker);\r
223\r
224 //\r
225 // Send the DebugInfo record\r
226 //\r
227 REPORT_STATUS_CODE_EX (\r
228 EFI_DEBUG_CODE,\r
229 (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_DC_UNSPECIFIED),\r
230 0,\r
231 NULL,\r
232 &gEfiStatusCodeDataTypeDebugGuid,\r
233 DebugInfo,\r
234 TotalSize\r
235 );\r
236}\r
237\r
238/**\r
d1102dba 239 Prints an assert message containing a filename, line number, and description.\r
11ff2c71
LG
240 This may be followed by a breakpoint or a dead loop.\r
241\r
242 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"\r
d1102dba
LG
243 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of\r
244 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if\r
245 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then\r
246 CpuDeadLoop() is called. If neither of these bits are set, then this function\r
11ff2c71
LG
247 returns immediately after the message is printed to the debug output device.\r
248 DebugAssert() must actively prevent recursion. If DebugAssert() is called while\r
249 processing another DebugAssert(), then DebugAssert() must return immediately.\r
250\r
251 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.\r
252 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.\r
253\r
254 @param FileName Pointer to the name of the source file that generated the assert condition.\r
255 @param LineNumber The line number in the source file that generated the assert condition\r
256 @param Description Pointer to the description of the assert condition.\r
257\r
258**/\r
259VOID\r
260EFIAPI\r
261DebugAssert (\r
262 IN CONST CHAR8 *FileName,\r
263 IN UINTN LineNumber,\r
264 IN CONST CHAR8 *Description\r
265 )\r
266{\r
267 UINT64 Buffer[EFI_STATUS_CODE_DATA_MAX_SIZE / sizeof(UINT64)];\r
268 EFI_DEBUG_ASSERT_DATA *AssertData;\r
269 UINTN HeaderSize;\r
270 UINTN TotalSize;\r
271 CHAR8 *Temp;\r
39eccb9c 272 UINTN ModuleNameSize;\r
11ff2c71
LG
273 UINTN FileNameSize;\r
274 UINTN DescriptionSize;\r
275\r
276 //\r
277 // Get string size\r
278 //\r
279 HeaderSize = sizeof (EFI_DEBUG_ASSERT_DATA);\r
39eccb9c
BA
280 //\r
281 // Compute string size of module name enclosed by []\r
282 //\r
283 ModuleNameSize = 2 + AsciiStrSize (gEfiCallerBaseName);\r
11ff2c71
LG
284 FileNameSize = AsciiStrSize (FileName);\r
285 DescriptionSize = AsciiStrSize (Description);\r
286\r
287 //\r
288 // Make sure it will all fit in the passed in buffer.\r
289 //\r
39eccb9c 290 if (HeaderSize + ModuleNameSize + FileNameSize + DescriptionSize > sizeof (Buffer)) {\r
11ff2c71 291 //\r
39eccb9c 292 // remove module name if it's too long to be filled into buffer\r
11ff2c71 293 //\r
39eccb9c
BA
294 ModuleNameSize = 0;\r
295 if (HeaderSize + FileNameSize + DescriptionSize > sizeof (Buffer)) {\r
11ff2c71 296 //\r
39eccb9c 297 // FileName + Description is too long to be filled into buffer.\r
11ff2c71 298 //\r
39eccb9c
BA
299 if (HeaderSize + FileNameSize < sizeof (Buffer)) {\r
300 //\r
301 // Description has enough buffer to be truncated.\r
302 //\r
303 DescriptionSize = sizeof (Buffer) - HeaderSize - FileNameSize;\r
304 } else {\r
305 //\r
306 // FileName is too long to be filled into buffer.\r
307 // FileName will be truncated. Reserved one byte for Description NULL terminator.\r
308 //\r
309 DescriptionSize = 1;\r
310 FileNameSize = sizeof (Buffer) - HeaderSize - DescriptionSize;\r
311 }\r
11ff2c71
LG
312 }\r
313 }\r
11ff2c71
LG
314 //\r
315 // Fill in EFI_DEBUG_ASSERT_DATA\r
316 //\r
317 AssertData = (EFI_DEBUG_ASSERT_DATA *)Buffer;\r
318 AssertData->LineNumber = (UINT32)LineNumber;\r
319 TotalSize = sizeof (EFI_DEBUG_ASSERT_DATA);\r
320\r
39eccb9c
BA
321 Temp = (CHAR8 *)(AssertData + 1);\r
322\r
323 //\r
324 // Copy Ascii [ModuleName].\r
325 //\r
326 if (ModuleNameSize != 0) {\r
327 CopyMem(Temp, "[", 1);\r
328 CopyMem(Temp + 1, gEfiCallerBaseName, ModuleNameSize - 3);\r
329 CopyMem(Temp + ModuleNameSize - 2, "] ", 2);\r
330 }\r
331\r
11ff2c71
LG
332 //\r
333 // Copy Ascii FileName including NULL terminator.\r
334 //\r
39eccb9c 335 Temp = CopyMem (Temp + ModuleNameSize, FileName, FileNameSize);\r
11ff2c71 336 Temp[FileNameSize - 1] = 0;\r
39eccb9c 337 TotalSize += (ModuleNameSize + FileNameSize);\r
11ff2c71
LG
338\r
339 //\r
340 // Copy Ascii Description include NULL terminator.\r
341 //\r
342 Temp = CopyMem (Temp + FileNameSize, Description, DescriptionSize);\r
343 Temp[DescriptionSize - 1] = 0;\r
344 TotalSize += DescriptionSize;\r
345\r
346 REPORT_STATUS_CODE_EX (\r
347 (EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED),\r
348 (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_EC_ILLEGAL_SOFTWARE_STATE),\r
349 0,\r
350 NULL,\r
351 NULL,\r
352 AssertData,\r
353 TotalSize\r
354 );\r
355\r
356 //\r
357 // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings\r
358 //\r
359 if ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED) != 0) {\r
360 CpuBreakpoint ();\r
361 } else if ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED) != 0) {\r
362 CpuDeadLoop ();\r
363 }\r
364}\r
365\r
366\r
367/**\r
368 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.\r
369\r
d1102dba 370 This function fills Length bytes of Buffer with the value specified by\r
11ff2c71
LG
371 PcdDebugClearMemoryValue, and returns Buffer.\r
372\r
373 If Buffer is NULL, then ASSERT().\r
d1102dba 374 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
11ff2c71
LG
375\r
376 @param Buffer Pointer to the target buffer to be filled with PcdDebugClearMemoryValue.\r
d1102dba 377 @param Length Number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.\r
11ff2c71
LG
378\r
379 @return Buffer Pointer to the target buffer filled with PcdDebugClearMemoryValue.\r
380\r
381**/\r
382VOID *\r
383EFIAPI\r
384DebugClearMemory (\r
385 OUT VOID *Buffer,\r
386 IN UINTN Length\r
387 )\r
388{\r
389 ASSERT (Buffer != NULL);\r
390\r
391 return SetMem (Buffer, Length, PcdGet8 (PcdDebugClearMemoryValue));\r
392}\r
393\r
394\r
395/**\r
396 Returns TRUE if ASSERT() macros are enabled.\r
397\r
d1102dba 398 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of\r
11ff2c71
LG
399 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
400\r
401 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.\r
402 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.\r
403\r
404**/\r
405BOOLEAN\r
406EFIAPI\r
407DebugAssertEnabled (\r
408 VOID\r
409 )\r
410{\r
411 return (BOOLEAN) ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) != 0);\r
412}\r
413\r
414\r
d1102dba 415/**\r
11ff2c71
LG
416 Returns TRUE if DEBUG() macros are enabled.\r
417\r
d1102dba 418 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of\r
11ff2c71
LG
419 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
420\r
421 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.\r
422 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.\r
423\r
424**/\r
425BOOLEAN\r
426EFIAPI\r
427DebugPrintEnabled (\r
428 VOID\r
429 )\r
430{\r
431 return (BOOLEAN) ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED) != 0);\r
432}\r
433\r
434\r
d1102dba 435/**\r
11ff2c71
LG
436 Returns TRUE if DEBUG_CODE() macros are enabled.\r
437\r
d1102dba 438 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of\r
11ff2c71
LG
439 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
440\r
441 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.\r
442 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.\r
443\r
444**/\r
445BOOLEAN\r
446EFIAPI\r
447DebugCodeEnabled (\r
448 VOID\r
449 )\r
450{\r
451 return (BOOLEAN) ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED) != 0);\r
452}\r
453\r
454\r
d1102dba 455/**\r
11ff2c71
LG
456 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.\r
457\r
d1102dba 458 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of\r
11ff2c71
LG
459 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
460\r
461 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.\r
462 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.\r
463\r
464**/\r
465BOOLEAN\r
466EFIAPI\r
467DebugClearMemoryEnabled (\r
468 VOID\r
469 )\r
470{\r
471 return (BOOLEAN) ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED) != 0);\r
472}\r
473\r
474/**\r
475 Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.\r
476\r
477 This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.\r
478\r
479 @retval TRUE Current ErrorLevel is supported.\r
480 @retval FALSE Current ErrorLevel is not supported.\r
481\r
482**/\r
483BOOLEAN\r
484EFIAPI\r
485DebugPrintLevelEnabled (\r
486 IN CONST UINTN ErrorLevel\r
487 )\r
488{\r
489 return (BOOLEAN) ((ErrorLevel & PcdGet32(PcdFixedDebugPrintErrorLevel)) != 0);\r
490}\r