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