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