]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Library/PeiDxeDebugLibReportStatusCode/DebugLib.c
Code scrub for PeiDxeDebugLibReportStatusCode.
[mirror_edk2.git] / IntelFrameworkModulePkg / Library / PeiDxeDebugLibReportStatusCode / DebugLib.c
1 /** @file
2 Debug Library based on report status code library.
3
4 Copyright (c) 2006 - 2009, Intel Corporation<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15
16
17 #include <FrameworkPei.h>
18 #include <Guid/StatusCodeDataTypeId.h>
19 #include <Guid/StatusCodeDataTypeDebug.h>
20
21 #include <Library/DebugLib.h>
22 #include <Library/BaseLib.h>
23 #include <Library/BaseMemoryLib.h>
24 #include <Library/ReportStatusCodeLib.h>
25 #include <Library/PcdLib.h>
26
27 /**
28 Prints a debug message to the debug output device if the specified error level is enabled.
29
30 If any bit in ErrorLevel is also set in PcdDebugPrintErrorLevel, then print
31 the message specified by Format and the associated variable argument list to
32 the debug output device.
33
34 If Format is NULL, then ASSERT().
35
36 @param ErrorLevel The error level of the debug message.
37 @param Format Format string for the debug message to print.
38 @param ... Variable argument list whose contents are accessed
39 based on the format string specified by Format.
40
41 **/
42 VOID
43 EFIAPI
44 DebugPrint (
45 IN UINTN ErrorLevel,
46 IN CONST CHAR8 *Format,
47 ...
48 )
49 {
50 UINT64 Buffer[(EFI_STATUS_CODE_DATA_MAX_SIZE / sizeof (UINT64)) + 1];
51 EFI_DEBUG_INFO *DebugInfo;
52 UINTN TotalSize;
53 VA_LIST VaListMarker;
54 BASE_LIST BaseListMarker;
55 CHAR8 *FormatString;
56 BOOLEAN Long;
57 BOOLEAN Done;
58
59 //
60 // If Format is NULL, then ASSERT().
61 //
62 ASSERT (Format != NULL);
63
64 //
65 // Check driver Debug Level value and global debug level
66 //
67 if ((ErrorLevel & PcdGet32 (PcdDebugPrintErrorLevel)) == 0) {
68 return;
69 }
70
71 //
72 // Compute the total size of the record
73 //
74 TotalSize = sizeof (EFI_DEBUG_INFO) + 12 * sizeof (UINT64) + AsciiStrSize (Format);
75
76 //
77 // If the TotalSize is larger than the maximum record size, then return
78 //
79 if (TotalSize > sizeof (Buffer)) {
80 return;
81 }
82
83 //
84 // Fill in EFI_DEBUG_INFO
85 //
86 // Here we skip the first 4 bytes of Buffer, because we must ensure BaseListMarker is
87 // 64-bit aligned, otherwise retrieving 64-bit parameter from BaseListMarker will cause
88 // exception on IPF. Buffer starts at 64-bit aligned address, so skipping 4 types (sizeof(EFI_DEBUG_INFO))
89 // just makes addess of BaseListMarker, which follows DebugInfo, 64-bit aligned.
90 //
91 DebugInfo = (EFI_DEBUG_INFO *)(Buffer) + 1;
92 DebugInfo->ErrorLevel = (UINT32)ErrorLevel;
93 BaseListMarker = (BASE_LIST)(DebugInfo + 1);
94 FormatString = (CHAR8 *)((UINT64 *)(DebugInfo + 1) + 12);
95
96 //
97 // Copy the Format string into the record
98 //
99 AsciiStrCpy (FormatString, Format);
100
101 //
102 // The first 12 * sizeof (UINT64) bytes following EFI_DEBUG_INFO are for variable arguments
103 // of format in DEBUG string, which is followed by the DEBUG format string.
104 // Here we will process the variable arguments and pack them in this area.
105 //
106 VA_START (VaListMarker, Format);
107 for (; *Format != '\0'; Format++) {
108 //
109 // Only format with prefix % is processed.
110 //
111 if (*Format != '%') {
112 continue;
113 }
114 Long = FALSE;
115 //
116 // Parse Flags and Width
117 //
118 for (Done = FALSE; !Done; ) {
119 Format++;
120 switch (*Format) {
121 case '.':
122 case '-':
123 case '+':
124 case ' ':
125 case ',':
126 case '0':
127 case '1':
128 case '2':
129 case '3':
130 case '4':
131 case '5':
132 case '6':
133 case '7':
134 case '8':
135 case '9':
136 //
137 // These characters in format field are omitted.
138 //
139 break;
140 case 'L':
141 case 'l':
142 //
143 // 'L" or "l" in format field means the number being printed is a UINT64
144 //
145 Long = TRUE;
146 break;
147 case '*':
148 //
149 // '*' in format field means the precision of the field is specified by
150 // a UINTN argument in the argument list.
151 //
152 BASE_ARG (BaseListMarker, UINTN) = VA_ARG (VaListMarker, UINTN);
153 break;
154 case '\0':
155 //
156 // Make no output if Format string terminates unexpectedly when
157 // looking up for flag, width, precision and type.
158 //
159 Format--;
160 //
161 // break skipped on purpose.
162 //
163 default:
164 //
165 // When valid argument type detected or format string terminates unexpectedly,
166 // the inner loop is done.
167 //
168 Done = TRUE;
169 break;
170 }
171 }
172
173 //
174 // Pack variable arguments into the storage area following EFI_DEBUG_INFO.
175 //
176 switch (*Format) {
177 case 'p':
178 if (sizeof (VOID *) > 4) {
179 Long = TRUE;
180 }
181 case 'X':
182 case 'x':
183 case 'd':
184 if (Long) {
185 BASE_ARG (BaseListMarker, INT64) = VA_ARG (VaListMarker, INT64);
186 } else {
187 BASE_ARG (BaseListMarker, int) = VA_ARG (VaListMarker, int);
188 }
189 break;
190 case 's':
191 case 'S':
192 case 'a':
193 case 'g':
194 case 't':
195 BASE_ARG (BaseListMarker, VOID *) = VA_ARG (VaListMarker, VOID *);
196 break;
197 case 'c':
198 BASE_ARG (BaseListMarker, UINTN) = VA_ARG (VaListMarker, UINTN);
199 break;
200 case 'r':
201 BASE_ARG (BaseListMarker, RETURN_STATUS) = VA_ARG (VaListMarker, RETURN_STATUS);
202 break;
203 }
204
205 //
206 // If the converted BASE_LIST is larger than the 12 * sizeof (UINT64) allocated bytes, then ASSERT()
207 // This indicates that the DEBUG() macro is passing in more argument than can be handled by
208 // the EFI_DEBUG_INFO record
209 //
210 ASSERT ((CHAR8 *)BaseListMarker <= FormatString);
211
212 //
213 // If the converted BASE_LIST is larger than the 12 * sizeof (UINT64) allocated bytes, then return
214 //
215 if ((CHAR8 *)BaseListMarker > FormatString) {
216 return;
217 }
218 }
219 VA_END (VaListMarker);
220
221 //
222 // Send the DebugInfo record
223 //
224 REPORT_STATUS_CODE_EX (
225 EFI_DEBUG_CODE,
226 (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_DC_UNSPECIFIED),
227 0,
228 NULL,
229 &gEfiStatusCodeDataTypeDebugGuid,
230 DebugInfo,
231 TotalSize
232 );
233 }
234
235 /**
236 Prints an assert message containing a filename, line number, and description.
237 This may be followed by a breakpoint or a dead loop.
238
239 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"
240 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of
241 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if
242 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then
243 CpuDeadLoop() is called. If neither of these bits are set, then this function
244 returns immediately after the message is printed to the debug output device.
245 DebugAssert() must actively prevent recursion. If DebugAssert() is called while
246 processing another DebugAssert(), then DebugAssert() must return immediately.
247
248 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.
249 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.
250
251 @param FileName Pointer to the name of the source file that generated the assert condition.
252 @param LineNumber The line number in the source file that generated the assert condition
253 @param Description Pointer to the description of the assert condition.
254
255 **/
256 VOID
257 EFIAPI
258 DebugAssert (
259 IN CONST CHAR8 *FileName,
260 IN UINTN LineNumber,
261 IN CONST CHAR8 *Description
262 )
263 {
264 UINT64 Buffer[EFI_STATUS_CODE_DATA_MAX_SIZE / sizeof(UINT64)];
265 EFI_DEBUG_ASSERT_DATA *AssertData;
266 UINTN TotalSize;
267 CHAR8 *Temp;
268 UINTN FileNameSize;
269 UINTN DescriptionSize;
270
271 //
272 // Make sure it will all fit in the passed in buffer
273 //
274 FileNameSize = AsciiStrSize (FileName);
275 DescriptionSize = AsciiStrSize (Description);
276 TotalSize = sizeof (EFI_DEBUG_ASSERT_DATA) + FileNameSize + DescriptionSize;
277 if (TotalSize <= sizeof (Buffer)) {
278 //
279 // Fill in EFI_DEBUG_ASSERT_DATA
280 //
281 AssertData = (EFI_DEBUG_ASSERT_DATA *)Buffer;
282 AssertData->LineNumber = (UINT32)LineNumber;
283
284 //
285 // Copy Ascii FileName including NULL.
286 //
287 Temp = AsciiStrCpy ((CHAR8 *)(AssertData + 1), FileName);
288
289 //
290 // Copy Ascii Description
291 //
292 AsciiStrCpy (Temp + FileNameSize, Description);
293
294 REPORT_STATUS_CODE_EX (
295 (EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED),
296 (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_EC_ILLEGAL_SOFTWARE_STATE),
297 0,
298 NULL,
299 NULL,
300 AssertData,
301 TotalSize
302 );
303 }
304
305 //
306 // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings
307 //
308 if ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED) != 0) {
309 CpuBreakpoint ();
310 } else if ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED) != 0) {
311 CpuDeadLoop ();
312 }
313 }
314
315
316 /**
317 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.
318
319 This function fills Length bytes of Buffer with the value specified by
320 PcdDebugClearMemoryValue, and returns Buffer.
321
322 If Buffer is NULL, then ASSERT().
323 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
324
325 @param Buffer Pointer to the target buffer to be filled with PcdDebugClearMemoryValue.
326 @param Length Number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.
327
328 @return Buffer Pointer to the target buffer filled with PcdDebugClearMemoryValue.
329
330 **/
331 VOID *
332 EFIAPI
333 DebugClearMemory (
334 OUT VOID *Buffer,
335 IN UINTN Length
336 )
337 {
338 ASSERT (Buffer != NULL);
339
340 return SetMem (Buffer, Length, PcdGet8 (PcdDebugClearMemoryValue));
341 }
342
343
344 /**
345 Returns TRUE if ASSERT() macros are enabled.
346
347 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of
348 PcdDebugProperyMask is set. Otherwise FALSE is returned.
349
350 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.
351 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.
352
353 **/
354 BOOLEAN
355 EFIAPI
356 DebugAssertEnabled (
357 VOID
358 )
359 {
360 __asm int 3
361 return (BOOLEAN) ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) != 0);
362 }
363
364
365 /**
366 Returns TRUE if DEBUG() macros are enabled.
367
368 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of
369 PcdDebugProperyMask is set. Otherwise FALSE is returned.
370
371 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.
372 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.
373
374 **/
375 BOOLEAN
376 EFIAPI
377 DebugPrintEnabled (
378 VOID
379 )
380 {
381 return (BOOLEAN) ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED) != 0);
382 }
383
384
385 /**
386 Returns TRUE if DEBUG_CODE() macros are enabled.
387
388 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of
389 PcdDebugProperyMask is set. Otherwise FALSE is returned.
390
391 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.
392 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.
393
394 **/
395 BOOLEAN
396 EFIAPI
397 DebugCodeEnabled (
398 VOID
399 )
400 {
401 return (BOOLEAN) ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED) != 0);
402 }
403
404
405 /**
406 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.
407
408 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of
409 PcdDebugProperyMask is set. Otherwise FALSE is returned.
410
411 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.
412 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.
413
414 **/
415 BOOLEAN
416 EFIAPI
417 DebugClearMemoryEnabled (
418 VOID
419 )
420 {
421 return (BOOLEAN) ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED) != 0);
422 }