]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Library/PeiDxeDebugLibReportStatusCode/DebugLib.c
3f3778d84a07d117e5714bc2a50dd14b8e56a16f
[mirror_edk2.git] / IntelFrameworkModulePkg / Library / PeiDxeDebugLibReportStatusCode / DebugLib.c
1 /** @file
2 Debug Library based on report status code library.
3
4 Note that if the debug message length is larger than the maximum allowable
5 record length, then the debug message will be ignored directly.
6
7 Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
8 This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
12
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15
16 **/
17
18 #include <PiPei.h>
19
20 #include <Guid/StatusCodeDataTypeId.h>
21 #include <Guid/StatusCodeDataTypeDebug.h>
22
23 #include <Library/DebugLib.h>
24 #include <Library/BaseLib.h>
25 #include <Library/BaseMemoryLib.h>
26 #include <Library/ReportStatusCodeLib.h>
27 #include <Library/PcdLib.h>
28 #include <Library/DebugPrintErrorLevelLib.h>
29
30 //
31 // VA_LIST can not initialize to NULL for all compiler, so we use this to
32 // indicate a null VA_LIST
33 //
34 VA_LIST mVaListNull;
35
36 /**
37 Prints a debug message to the debug output device if the specified error level is enabled.
38
39 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
40 GetDebugPrintErrorLevel (), then print the message specified by Format and the
41 associated variable argument list to the debug output device.
42
43 If Format is NULL, then ASSERT().
44
45 If the length of the message string specificed by Format is larger than the maximum allowable
46 record length, then directly return and not print it.
47
48 @param ErrorLevel The error level of the debug message.
49 @param Format Format string for the debug message to print.
50 @param ... Variable argument list whose contents are accessed
51 based on the format string specified by Format.
52
53 **/
54 VOID
55 EFIAPI
56 DebugPrint (
57 IN UINTN ErrorLevel,
58 IN CONST CHAR8 *Format,
59 ...
60 )
61 {
62 VA_LIST Marker;
63
64 VA_START (Marker, Format);
65 DebugVPrint (ErrorLevel, Format, Marker);
66 VA_END (Marker);
67 }
68
69 /**
70 Prints a debug message to the debug output device if the specified
71 error level is enabled base on Null-terminated format string and a
72 VA_LIST argument list or a BASE_LIST argument list.
73
74 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
75 GetDebugPrintErrorLevel (), then print the message specified by Format and
76 the associated variable argument list to the debug output device.
77
78 Only one list type is used.
79 If BaseListMarker == NULL, then use VaListMarker.
80 Otherwise use BaseListMarker and the VaListMarker should be initilized as
81 mVaListNull.
82
83 If Format is NULL, then ASSERT().
84
85 @param ErrorLevel The error level of the debug message.
86 @param Format Format string for the debug message to print.
87 @param VaListMarker VA_LIST marker for the variable argument list.
88 @param BaseListMarker BASE_LIST marker for the variable argument list.
89
90 **/
91 VOID
92 DebugPrintMarker (
93 IN UINTN ErrorLevel,
94 IN CONST CHAR8 *Format,
95 IN VA_LIST VaListMarker,
96 IN BASE_LIST BaseListMarker
97 )
98 {
99 UINT64 Buffer[(EFI_STATUS_CODE_DATA_MAX_SIZE / sizeof (UINT64)) + 1];
100 EFI_DEBUG_INFO *DebugInfo;
101 UINTN TotalSize;
102 UINTN DestBufferSize;
103 BASE_LIST BaseListMarkerPointer;
104 CHAR8 *FormatString;
105 BOOLEAN Long;
106
107 //
108 // If Format is NULL, then ASSERT().
109 //
110 ASSERT (Format != NULL);
111
112 //
113 // Check driver Debug Level value and global debug level
114 //
115 if ((ErrorLevel & GetDebugPrintErrorLevel ()) == 0) {
116 return;
117 }
118
119 //
120 // Compute the total size of the record.
121 // Note that the passing-in format string and variable parameters will be constructed to
122 // the following layout:
123 //
124 // Buffer->|------------------------|
125 // | Padding | 4 bytes
126 // DebugInfo->|------------------------|
127 // | EFI_DEBUG_INFO | sizeof(EFI_DEBUG_INFO)
128 // BaseListMarkerPointer->|------------------------|
129 // | ... |
130 // | variable arguments | 12 * sizeof (UINT64)
131 // | ... |
132 // |------------------------|
133 // | Format String |
134 // |------------------------|<- (UINT8 *)Buffer + sizeof(Buffer)
135 //
136 TotalSize = 4 + sizeof (EFI_DEBUG_INFO) + 12 * sizeof (UINT64) + AsciiStrSize (Format);
137
138 //
139 // If the TotalSize is larger than the maximum record size, then truncate it.
140 //
141 if (TotalSize > sizeof (Buffer)) {
142 TotalSize = sizeof (Buffer);
143 }
144
145 //
146 // Fill in EFI_DEBUG_INFO
147 //
148 // Here we skip the first 4 bytes of Buffer, because we must ensure BaseListMarkerPointer is
149 // 64-bit aligned, otherwise retrieving 64-bit parameter from BaseListMarkerPointer will cause
150 // exception on IPF. Buffer starts at 64-bit aligned address, so skipping 4 types (sizeof(EFI_DEBUG_INFO))
151 // just makes address of BaseListMarkerPointer, which follows DebugInfo, 64-bit aligned.
152 //
153 DebugInfo = (EFI_DEBUG_INFO *)(Buffer) + 1;
154 DebugInfo->ErrorLevel = (UINT32)ErrorLevel;
155 BaseListMarkerPointer = (BASE_LIST)(DebugInfo + 1);
156 FormatString = (CHAR8 *)((UINT64 *)(DebugInfo + 1) + 12);
157
158 //
159 // Copy the Format string into the record
160 //
161 // According to the content structure of Buffer shown above, the size of
162 // the FormatString buffer is the size of Buffer minus the Padding
163 // (4 bytes), minus the size of EFI_DEBUG_INFO, minus the size of
164 // variable arguments (12 * sizeof (UINT64)).
165 //
166 DestBufferSize = sizeof (Buffer) - 4 - sizeof (EFI_DEBUG_INFO) - 12 * sizeof (UINT64);
167 AsciiStrCpyS (FormatString, DestBufferSize / sizeof (CHAR8), Format);
168
169 //
170 // The first 12 * sizeof (UINT64) bytes following EFI_DEBUG_INFO are for variable arguments
171 // of format in DEBUG string, which is followed by the DEBUG format string.
172 // Here we will process the variable arguments and pack them in this area.
173 //
174 for (; *Format != '\0'; Format++) {
175 //
176 // Only format with prefix % is processed.
177 //
178 if (*Format != '%') {
179 continue;
180 }
181 Long = FALSE;
182 //
183 // Parse Flags and Width
184 //
185 for (Format++; TRUE; Format++) {
186 if (*Format == '.' || *Format == '-' || *Format == '+' || *Format == ' ') {
187 //
188 // These characters in format field are omitted.
189 //
190 continue;
191 }
192 if (*Format >= '0' && *Format <= '9') {
193 //
194 // These characters in format field are omitted.
195 //
196 continue;
197 }
198 if (*Format == 'L' || *Format == 'l') {
199 //
200 // 'L" or "l" in format field means the number being printed is a UINT64
201 //
202 Long = TRUE;
203 continue;
204 }
205 if (*Format == '*') {
206 //
207 // '*' in format field means the precision of the field is specified by
208 // a UINTN argument in the argument list.
209 //
210 if (BaseListMarker == NULL) {
211 BASE_ARG (BaseListMarkerPointer, UINTN) = VA_ARG (VaListMarker, UINTN);
212 } else {
213 BASE_ARG (BaseListMarkerPointer, UINTN) = BASE_ARG (BaseListMarker, UINTN);
214 }
215 continue;
216 }
217 if (*Format == '\0') {
218 //
219 // Make no output if Format string terminates unexpectedly when
220 // looking up for flag, width, precision and type.
221 //
222 Format--;
223 }
224 //
225 // When valid argument type detected or format string terminates unexpectedly,
226 // the inner loop is done.
227 //
228 break;
229 }
230
231 //
232 // Pack variable arguments into the storage area following EFI_DEBUG_INFO.
233 //
234 if ((*Format == 'p') && (sizeof (VOID *) > 4)) {
235 Long = TRUE;
236 }
237 if (*Format == 'p' || *Format == 'X' || *Format == 'x' || *Format == 'd' || *Format == 'u') {
238 if (Long) {
239 if (BaseListMarker == NULL) {
240 BASE_ARG (BaseListMarkerPointer, INT64) = VA_ARG (VaListMarker, INT64);
241 } else {
242 BASE_ARG (BaseListMarkerPointer, INT64) = BASE_ARG (BaseListMarker, INT64);
243 }
244 } else {
245 if (BaseListMarker == NULL) {
246 BASE_ARG (BaseListMarkerPointer, int) = VA_ARG (VaListMarker, int);
247 } else {
248 BASE_ARG (BaseListMarkerPointer, int) = BASE_ARG (BaseListMarker, int);
249 }
250 }
251 } else if (*Format == 's' || *Format == 'S' || *Format == 'a' || *Format == 'g' || *Format == 't') {
252 if (BaseListMarker == NULL) {
253 BASE_ARG (BaseListMarkerPointer, VOID *) = VA_ARG (VaListMarker, VOID *);
254 } else {
255 BASE_ARG (BaseListMarkerPointer, VOID *) = BASE_ARG (BaseListMarker, VOID *);
256 }
257 } else if (*Format == 'c') {
258 if (BaseListMarker == NULL) {
259 BASE_ARG (BaseListMarkerPointer, UINTN) = VA_ARG (VaListMarker, UINTN);
260 } else {
261 BASE_ARG (BaseListMarkerPointer, UINTN) = BASE_ARG (BaseListMarker, UINTN);
262 }
263 } else if (*Format == 'r') {
264 if (BaseListMarker == NULL) {
265 BASE_ARG (BaseListMarkerPointer, RETURN_STATUS) = VA_ARG (VaListMarker, RETURN_STATUS);
266 } else {
267 BASE_ARG (BaseListMarkerPointer, RETURN_STATUS) = BASE_ARG (BaseListMarker, RETURN_STATUS);
268 }
269 }
270
271 //
272 // If the converted BASE_LIST is larger than the 12 * sizeof (UINT64) allocated bytes, then ASSERT()
273 // This indicates that the DEBUG() macro is passing in more argument than can be handled by
274 // the EFI_DEBUG_INFO record
275 //
276 ASSERT ((CHAR8 *)BaseListMarkerPointer <= FormatString);
277
278 //
279 // If the converted BASE_LIST is larger than the 12 * sizeof (UINT64) allocated bytes, then return
280 //
281 if ((CHAR8 *)BaseListMarkerPointer > FormatString) {
282 return;
283 }
284 }
285
286 //
287 // Send the DebugInfo record
288 //
289 REPORT_STATUS_CODE_EX (
290 EFI_DEBUG_CODE,
291 (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_DC_UNSPECIFIED),
292 0,
293 NULL,
294 &gEfiStatusCodeDataTypeDebugGuid,
295 DebugInfo,
296 TotalSize
297 );
298 }
299
300 /**
301 Prints a debug message to the debug output device if the specified
302 error level is enabled.
303
304 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
305 GetDebugPrintErrorLevel (), then print the message specified by Format and
306 the associated variable argument list to the debug output device.
307
308 If Format is NULL, then ASSERT().
309
310 @param ErrorLevel The error level of the debug message.
311 @param Format Format string for the debug message to print.
312 @param VaListMarker VA_LIST marker for the variable argument list.
313
314 **/
315 VOID
316 EFIAPI
317 DebugVPrint (
318 IN UINTN ErrorLevel,
319 IN CONST CHAR8 *Format,
320 IN VA_LIST VaListMarker
321 )
322 {
323 DebugPrintMarker (ErrorLevel, Format, VaListMarker, NULL);
324 }
325
326 /**
327 Prints a debug message to the debug output device if the specified
328 error level is enabled.
329 This function use BASE_LIST which would provide a more compatible
330 service than VA_LIST.
331
332 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
333 GetDebugPrintErrorLevel (), then print the message specified by Format and
334 the associated variable argument list to the debug output device.
335
336 If Format is NULL, then ASSERT().
337
338 @param ErrorLevel The error level of the debug message.
339 @param Format Format string for the debug message to print.
340 @param BaseListMarker BASE_LIST marker for the variable argument list.
341
342 **/
343 VOID
344 EFIAPI
345 DebugBPrint (
346 IN UINTN ErrorLevel,
347 IN CONST CHAR8 *Format,
348 IN BASE_LIST BaseListMarker
349 )
350 {
351 DebugPrintMarker (ErrorLevel, Format, mVaListNull, BaseListMarker);
352 }
353
354 /**
355 Prints an assert message containing a filename, line number, and description.
356 This may be followed by a breakpoint or a dead loop.
357
358 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"
359 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of
360 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if
361 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then
362 CpuDeadLoop() is called. If neither of these bits are set, then this function
363 returns immediately after the message is printed to the debug output device.
364 DebugAssert() must actively prevent recursion. If DebugAssert() is called while
365 processing another DebugAssert(), then DebugAssert() must return immediately.
366
367 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.
368 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.
369
370 @param FileName Pointer to the name of the source file that generated the assert condition.
371 @param LineNumber The line number in the source file that generated the assert condition
372 @param Description Pointer to the description of the assert condition.
373
374 **/
375 VOID
376 EFIAPI
377 DebugAssert (
378 IN CONST CHAR8 *FileName,
379 IN UINTN LineNumber,
380 IN CONST CHAR8 *Description
381 )
382 {
383 UINT64 Buffer[EFI_STATUS_CODE_DATA_MAX_SIZE / sizeof(UINT64)];
384 EFI_DEBUG_ASSERT_DATA *AssertData;
385 UINTN HeaderSize;
386 UINTN TotalSize;
387 CHAR8 *Temp;
388 UINTN ModuleNameSize;
389 UINTN FileNameSize;
390 UINTN DescriptionSize;
391
392 //
393 // Get string size
394 //
395 HeaderSize = sizeof (EFI_DEBUG_ASSERT_DATA);
396 //
397 // Compute string size of module name enclosed by []
398 //
399 ModuleNameSize = 2 + AsciiStrSize (gEfiCallerBaseName);
400 FileNameSize = AsciiStrSize (FileName);
401 DescriptionSize = AsciiStrSize (Description);
402
403 //
404 // Make sure it will all fit in the passed in buffer.
405 //
406 if (HeaderSize + ModuleNameSize + FileNameSize + DescriptionSize > sizeof (Buffer)) {
407 //
408 // remove module name if it's too long to be filled into buffer
409 //
410 ModuleNameSize = 0;
411 if (HeaderSize + FileNameSize + DescriptionSize > sizeof (Buffer)) {
412 //
413 // FileName + Description is too long to be filled into buffer.
414 //
415 if (HeaderSize + FileNameSize < sizeof (Buffer)) {
416 //
417 // Description has enough buffer to be truncated.
418 //
419 DescriptionSize = sizeof (Buffer) - HeaderSize - FileNameSize;
420 } else {
421 //
422 // FileName is too long to be filled into buffer.
423 // FileName will be truncated. Reserved one byte for Description NULL terminator.
424 //
425 DescriptionSize = 1;
426 FileNameSize = sizeof (Buffer) - HeaderSize - DescriptionSize;
427 }
428 }
429 }
430 //
431 // Fill in EFI_DEBUG_ASSERT_DATA
432 //
433 AssertData = (EFI_DEBUG_ASSERT_DATA *)Buffer;
434 AssertData->LineNumber = (UINT32)LineNumber;
435 TotalSize = sizeof (EFI_DEBUG_ASSERT_DATA);
436
437 Temp = (CHAR8 *)(AssertData + 1);
438
439 //
440 // Copy Ascii [ModuleName].
441 //
442 if (ModuleNameSize != 0) {
443 CopyMem(Temp, "[", 1);
444 CopyMem(Temp + 1, gEfiCallerBaseName, ModuleNameSize - 3);
445 CopyMem(Temp + ModuleNameSize - 2, "] ", 2);
446 }
447
448 //
449 // Copy Ascii FileName including NULL terminator.
450 //
451 Temp = CopyMem (Temp + ModuleNameSize, FileName, FileNameSize);
452 Temp[FileNameSize - 1] = 0;
453 TotalSize += (ModuleNameSize + FileNameSize);
454
455 //
456 // Copy Ascii Description include NULL terminator.
457 //
458 Temp = CopyMem (Temp + FileNameSize, Description, DescriptionSize);
459 Temp[DescriptionSize - 1] = 0;
460 TotalSize += DescriptionSize;
461
462 REPORT_STATUS_CODE_EX (
463 (EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED),
464 (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_EC_ILLEGAL_SOFTWARE_STATE),
465 0,
466 NULL,
467 NULL,
468 AssertData,
469 TotalSize
470 );
471
472 //
473 // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings
474 //
475 if ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED) != 0) {
476 CpuBreakpoint ();
477 } else if ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED) != 0) {
478 CpuDeadLoop ();
479 }
480 }
481
482
483 /**
484 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.
485
486 This function fills Length bytes of Buffer with the value specified by
487 PcdDebugClearMemoryValue, and returns Buffer.
488
489 If Buffer is NULL, then ASSERT().
490 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
491
492 @param Buffer Pointer to the target buffer to be filled with PcdDebugClearMemoryValue.
493 @param Length Number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.
494
495 @return Buffer Pointer to the target buffer filled with PcdDebugClearMemoryValue.
496
497 **/
498 VOID *
499 EFIAPI
500 DebugClearMemory (
501 OUT VOID *Buffer,
502 IN UINTN Length
503 )
504 {
505 ASSERT (Buffer != NULL);
506
507 return SetMem (Buffer, Length, PcdGet8 (PcdDebugClearMemoryValue));
508 }
509
510
511 /**
512 Returns TRUE if ASSERT() macros are enabled.
513
514 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of
515 PcdDebugProperyMask is set. Otherwise FALSE is returned.
516
517 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.
518 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.
519
520 **/
521 BOOLEAN
522 EFIAPI
523 DebugAssertEnabled (
524 VOID
525 )
526 {
527 return (BOOLEAN) ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) != 0);
528 }
529
530
531 /**
532 Returns TRUE if DEBUG() macros are enabled.
533
534 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of
535 PcdDebugProperyMask is set. Otherwise FALSE is returned.
536
537 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.
538 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.
539
540 **/
541 BOOLEAN
542 EFIAPI
543 DebugPrintEnabled (
544 VOID
545 )
546 {
547 return (BOOLEAN) ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED) != 0);
548 }
549
550
551 /**
552 Returns TRUE if DEBUG_CODE() macros are enabled.
553
554 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of
555 PcdDebugProperyMask is set. Otherwise FALSE is returned.
556
557 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.
558 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.
559
560 **/
561 BOOLEAN
562 EFIAPI
563 DebugCodeEnabled (
564 VOID
565 )
566 {
567 return (BOOLEAN) ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED) != 0);
568 }
569
570
571 /**
572 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.
573
574 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of
575 PcdDebugProperyMask is set. Otherwise FALSE is returned.
576
577 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.
578 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.
579
580 **/
581 BOOLEAN
582 EFIAPI
583 DebugClearMemoryEnabled (
584 VOID
585 )
586 {
587 return (BOOLEAN) ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED) != 0);
588 }
589
590 /**
591 Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.
592
593 This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.
594
595 @retval TRUE Current ErrorLevel is supported.
596 @retval FALSE Current ErrorLevel is not supported.
597
598 **/
599 BOOLEAN
600 EFIAPI
601 DebugPrintLevelEnabled (
602 IN CONST UINTN ErrorLevel
603 )
604 {
605 return (BOOLEAN) ((ErrorLevel & PcdGet32(PcdFixedDebugPrintErrorLevel)) != 0);
606 }