]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Library/PeiDxeDebugLibReportStatusCode/DebugLib.c
4c62def93a97ecfe135006bc787531587225a621
[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 UINTN Index;
57 VA_LIST Marker;
58 UINT64 *ArgumentPointer;
59
60 //
61 // If Format is NULL, then ASSERT().
62 //
63 ASSERT (Format != NULL);
64
65 //
66 // Check driver Debug Level value and global debug level
67 //
68 if ((ErrorLevel & PcdGet32(PcdDebugPrintErrorLevel)) == 0) {
69 return;
70 }
71
72 TotalSize = sizeof (EFI_DEBUG_INFO) + 12 * sizeof (UINT64) + AsciiStrLen (Format) + 1;
73 if (TotalSize > EFI_STATUS_CODE_DATA_MAX_SIZE) {
74 return;
75 }
76
77 //
78 // Then EFI_DEBUG_INFO
79 //
80 DebugInfo = (EFI_DEBUG_INFO *)Buffer;
81 DebugInfo->ErrorLevel = (UINT32)ErrorLevel;
82
83 //
84 // 256 byte mini Var Arg stack. That is followed by the format string.
85 //
86 VA_START (Marker, Format);
87 for (Index = 0, ArgumentPointer = (UINT64 *)(DebugInfo + 1); Index < 12; Index++, ArgumentPointer++) {
88 WriteUnaligned64(ArgumentPointer, VA_ARG (Marker, UINT64));
89 }
90 VA_END (Marker);
91 AsciiStrCpy ((CHAR8 *)ArgumentPointer, Format);
92
93 REPORT_STATUS_CODE_EX (
94 EFI_DEBUG_CODE,
95 (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_DC_UNSPECIFIED),
96 0,
97 NULL,
98 &gEfiStatusCodeDataTypeDebugGuid,
99 DebugInfo,
100 TotalSize
101 );
102 }
103
104
105 /**
106
107 Prints an assert message containing a filename, line number, and description.
108 This may be followed by a breakpoint or a dead loop.
109
110 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"
111 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of
112 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if
113 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then
114 CpuDeadLoop() is called. If neither of these bits are set, then this function
115 returns immediately after the message is printed to the debug output device.
116 DebugAssert() must actively prevent recursion. If DebugAssert() is called while
117 processing another DebugAssert(), then DebugAssert() must return immediately.
118
119 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.
120
121 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.
122
123 @param FileName Pointer to the name of the source file that generated the assert condition.
124 @param LineNumber The line number in the source file that generated the assert condition
125 @param Description Pointer to the description of the assert condition.
126
127 **/
128 VOID
129 EFIAPI
130 DebugAssert (
131 IN CONST CHAR8 *FileName,
132 IN UINTN LineNumber,
133 IN CONST CHAR8 *Description
134 )
135 {
136 UINT64 Buffer[EFI_STATUS_CODE_DATA_MAX_SIZE / sizeof(UINT64)];
137 EFI_DEBUG_ASSERT_DATA *AssertData;
138 UINTN TotalSize;
139 CHAR8 *Temp;
140 UINTN FileNameLength;
141 UINTN DescriptionLength;
142
143 //
144 // Make sure it will all fit in the passed in buffer
145 //
146 FileNameLength = AsciiStrLen (FileName);
147 DescriptionLength = AsciiStrLen (Description);
148 TotalSize = sizeof (EFI_DEBUG_ASSERT_DATA) + FileNameLength + 1 + DescriptionLength + 1;
149 if (TotalSize <= EFI_STATUS_CODE_DATA_MAX_SIZE) {
150 //
151 // Fill in EFI_DEBUG_ASSERT_DATA
152 //
153 AssertData = (EFI_DEBUG_ASSERT_DATA *)Buffer;
154 AssertData->LineNumber = (UINT32)LineNumber;
155
156 //
157 // Copy Ascii FileName including NULL.
158 //
159 Temp = AsciiStrCpy ((CHAR8 *)(AssertData + 1), FileName);
160
161 //
162 // Copy Ascii Description
163 //
164 AsciiStrCpy (Temp + AsciiStrLen (FileName) + 1, Description);
165
166 REPORT_STATUS_CODE_EX (
167 (EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED),
168 (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_EC_ILLEGAL_SOFTWARE_STATE),
169 0,
170 NULL,
171 &gEfiStatusCodeDataTypeAssertGuid,
172 AssertData,
173 TotalSize
174 );
175 }
176
177 //
178 // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings
179 //
180 if ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED) != 0) {
181 CpuBreakpoint ();
182 } else if ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED) != 0) {
183 CpuDeadLoop ();
184 }
185 }
186
187
188 /**
189
190 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.
191
192 This function fills Length bytes of Buffer with the value specified by
193 PcdDebugClearMemoryValue, and returns Buffer.
194
195 If Buffer is NULL, then ASSERT().
196
197 If Length is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT().
198
199 @param Buffer Pointer to the target buffer to fill with PcdDebugClearMemoryValue.
200 @param Length Number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.
201
202 @return Buffer
203
204 **/
205 VOID *
206 EFIAPI
207 DebugClearMemory (
208 OUT VOID *Buffer,
209 IN UINTN Length
210 )
211 {
212 //
213 // If Buffer is NULL, then ASSERT().
214 //
215 ASSERT (Buffer != NULL);
216
217 //
218 // SetMem() checks for the the ASSERT() condition on Length and returns Buffer
219 //
220 return SetMem (Buffer, Length, PcdGet8(PcdDebugClearMemoryValue));
221 }
222
223
224 /**
225
226 Returns TRUE if ASSERT() macros are enabled.
227
228 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of
229 PcdDebugProperyMask is set. Otherwise FALSE is returned.
230
231 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.
232 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.
233
234 **/
235 BOOLEAN
236 EFIAPI
237 DebugAssertEnabled (
238 VOID
239 )
240 {
241 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) != 0);
242 }
243
244
245 /**
246
247 Returns TRUE if DEBUG()macros are enabled.
248
249 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of
250 PcdDebugProperyMask is set. Otherwise FALSE is returned.
251
252 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.
253 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.
254
255 **/
256 BOOLEAN
257 EFIAPI
258 DebugPrintEnabled (
259 VOID
260 )
261 {
262 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED) != 0);
263 }
264
265
266 /**
267
268 Returns TRUE if DEBUG_CODE()macros are enabled.
269
270 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of
271 PcdDebugProperyMask is set. Otherwise FALSE is returned.
272
273 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.
274 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.
275
276 **/
277 BOOLEAN
278 EFIAPI
279 DebugCodeEnabled (
280 VOID
281 )
282 {
283 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED) != 0);
284 }
285
286
287 /**
288
289 Returns TRUE if DEBUG_CLEAR_MEMORY()macro is enabled.
290
291 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CLEAR_MEMORY_ENABLED bit of
292 PcdDebugProperyMask is set. Otherwise FALSE is returned.
293
294 @retval TRUE The DEBUG_PROPERTY_DEBUG_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.
295 @retval FALSE The DEBUG_PROPERTY_DEBUG_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.
296
297 **/
298 BOOLEAN
299 EFIAPI
300 DebugClearMemoryEnabled (
301 VOID
302 )
303 {
304 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED) != 0);
305 }