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