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