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