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