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