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