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