6bc2c20dd9c17fb3ae9840dca62335d2427ef31a
[mirror_edk2.git] / EdkModulePkg / Library / EdkDxeDebugLibReportStatusCode / DebugLib.c
1 /** @file
2 EFI Debug Library that installs Debug Level Protocol.
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 STATIC BOOLEAN mDebugLevelInstalled = FALSE;
16 STATIC EFI_DEBUG_LEVEL_PROTOCOL mDebugLevel = { 0 };
17
18 /**
19 Installs Debug Level Protocol.
20
21 The constructor function installs Debug Level Protocol on the ImageHandle.
22 It will ASSERT() if the installation fails and will always return EFI_SUCCESS.
23
24 @param ImageHandle The firmware allocated handle for the EFI image.
25 @param SystemTable A pointer to the EFI System Table.
26
27 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
28
29 **/
30 EFI_STATUS
31 EFIAPI
32 DebugLibConstructor (
33 IN EFI_HANDLE ImageHandle,
34 IN EFI_SYSTEM_TABLE *SystemTable
35 )
36 {
37 EFI_STATUS Status;
38
39 //
40 // Initialize Debug Level Protocol.
41 //
42 mDebugLevel.DebugLevel = PcdGet32(PcdDebugPrintErrorLevel);
43
44 //
45 // Install Debug Level Protocol.
46 //
47 Status = gBS->InstallMultipleProtocolInterfaces (
48 &ImageHandle,
49 &gEfiDebugLevelProtocolGuid,
50 &mDebugLevel,
51 NULL
52 );
53 ASSERT_EFI_ERROR (Status);
54
55 //
56 // Set flag to show that the Debug Level Protocol has been installed.
57 //
58 mDebugLevelInstalled = TRUE;
59
60 return Status;
61 }
62
63 /**
64
65 Prints a debug message to the debug output device if the specified error level is enabled.
66
67 If any bit in ErrorLevel is also set in PcdDebugPrintErrorLevel, then print
68 the message specified by Format and the associated variable argument list to
69 the debug output device.
70
71 If Format is NULL, then ASSERT().
72
73 @param ErrorLevel The error level of the debug message.
74 @param Format Format string for the debug message to print.
75
76 **/
77 VOID
78 EFIAPI
79 DebugPrint (
80 IN UINTN ErrorLevel,
81 IN CONST CHAR8 *Format,
82 ...
83 )
84 {
85 UINT64 Buffer[EFI_STATUS_CODE_DATA_MAX_SIZE / sizeof (UINT64)];
86 EFI_DEBUG_INFO *DebugInfo;
87 UINTN TotalSize;
88 UINTN Index;
89 VA_LIST Marker;
90 UINT64 *ArgumentPointer;
91
92 //
93 // If Format is NULL, then ASSERT().
94 //
95 ASSERT (Format != NULL);
96
97 //
98 // Check driver Debug Level value and global debug level
99 //
100 if (mDebugLevelInstalled) {
101 if ((ErrorLevel & mDebugLevel.DebugLevel) == 0) {
102 return;
103 }
104 } else {
105 if ((ErrorLevel & PcdGet32(PcdDebugPrintErrorLevel)) == 0) {
106 return;
107 }
108 }
109
110 TotalSize = sizeof (EFI_DEBUG_INFO) + 12 * sizeof (UINT64) + AsciiStrLen (Format) + 1;
111 if (TotalSize > EFI_STATUS_CODE_DATA_MAX_SIZE) {
112 return;
113 }
114
115 //
116 // Then EFI_DEBUG_INFO
117 //
118 DebugInfo = (EFI_DEBUG_INFO *)Buffer;
119 DebugInfo->ErrorLevel = (UINT32)ErrorLevel;
120
121 //
122 // 256 byte mini Var Arg stack. That is followed by the format string.
123 //
124 VA_START (Marker, Format);
125 for (Index = 0, ArgumentPointer = (UINT64 *)(DebugInfo + 1); Index < 12; Index++, ArgumentPointer++) {
126 WriteUnaligned64(ArgumentPointer, VA_ARG (Marker, UINT64));
127 }
128 VA_END (Marker);
129 AsciiStrCpy ((CHAR8 *)ArgumentPointer, Format);
130
131 REPORT_STATUS_CODE_EX (
132 EFI_DEBUG_CODE,
133 (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_DC_UNSPECIFIED),
134 0,
135 NULL,
136 &gEfiStatusCodeDataTypeDebugGuid,
137 DebugInfo,
138 TotalSize
139 );
140 }
141
142
143 /**
144
145 Prints an assert message containing a filename, line number, and description.
146 This may be followed by a breakpoint or a dead loop.
147
148 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"
149 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of
150 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if
151 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then
152 CpuDeadLoop() is called. If neither of these bits are set, then this function
153 returns immediately after the message is printed to the debug output device.
154 DebugAssert() must actively prevent recusrsion. If DebugAssert() is called while
155 processing another DebugAssert(), then DebugAssert() must return immediately.
156
157 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.
158
159 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.
160
161 @param FileName Pointer to the name of the source file that generated the assert condition.
162 @param LineNumber The line number in the source file that generated the assert condition
163 @param Description Pointer to the description of the assert condition.
164
165 **/
166 VOID
167 EFIAPI
168 DebugAssert (
169 IN CONST CHAR8 *FileName,
170 IN UINTN LineNumber,
171 IN CONST CHAR8 *Description
172 )
173 {
174 UINT64 Buffer[EFI_STATUS_CODE_DATA_MAX_SIZE / sizeof(UINT64)];
175 EFI_DEBUG_ASSERT_DATA *AssertData;
176 UINTN TotalSize;
177 CHAR8 *Temp;
178
179 //
180 // Make sure it will all fit in the passed in buffer.
181 //
182 TotalSize = sizeof (EFI_DEBUG_ASSERT_DATA) + AsciiStrLen (FileName) + 1 + AsciiStrLen (Description) + 1;
183 if (TotalSize <= EFI_STATUS_CODE_DATA_MAX_SIZE) {
184 //
185 // Fill in EFI_DEBUG_ASSERT_DATA
186 //
187 AssertData = (EFI_DEBUG_ASSERT_DATA *)Buffer;
188 AssertData->LineNumber = (UINT32)LineNumber;
189
190 //
191 // Copy Ascii FileName including NULL.
192 //
193 Temp = AsciiStrCpy ((CHAR8 *)(AssertData + 1), FileName);
194
195 //
196 // Copy Ascii Description.
197 //
198 AsciiStrCpy (Temp + AsciiStrLen (FileName) + 1, Description);
199
200 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
201 (EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED),
202 (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_EC_ILLEGAL_SOFTWARE_STATE),
203 AssertData,
204 TotalSize
205 );
206 }
207
208 //
209 // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings
210 //
211 if ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED) != 0) {
212 CpuBreakpoint ();
213 } else if ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED) != 0) {
214 CpuDeadLoop ();
215 }
216 }
217
218
219 /**
220
221 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.
222
223 This function fills Length bytes of Buffer with the value specified by
224 PcdDebugClearMemoryValue, and returns Buffer.
225
226 If Buffer is NULL, then ASSERT().
227
228 If Length is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT().
229
230 @param Buffer Pointer to the target buffer to fill with PcdDebugClearMemoryValue.
231 @param Length Number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.
232
233 @return Buffer
234
235 **/
236 VOID *
237 EFIAPI
238 DebugClearMemory (
239 OUT VOID *Buffer,
240 IN UINTN Length
241 )
242 {
243 //
244 // If Buffer is NULL, then ASSERT().
245 //
246 ASSERT (Buffer != NULL);
247
248 //
249 // SetMem() checks for the the ASSERT() condition on Length and returns Buffer
250 //
251 return SetMem (Buffer, Length, PcdGet8(PcdDebugClearMemoryValue));
252 }
253
254
255 /**
256
257 Returns TRUE if ASSERT() macros are enabled.
258
259 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of
260 PcdDebugProperyMask is set. Otherwise FALSE is returned.
261
262 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.
263 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.
264
265 **/
266 BOOLEAN
267 EFIAPI
268 DebugAssertEnabled (
269 VOID
270 )
271 {
272 return ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) != 0);
273 }
274
275
276 /**
277
278 Returns TRUE if DEBUG()macros are enabled.
279
280 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of
281 PcdDebugProperyMask is set. Otherwise FALSE is returned.
282
283 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.
284 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.
285
286 **/
287 BOOLEAN
288 EFIAPI
289 DebugPrintEnabled (
290 VOID
291 )
292 {
293 return ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED) != 0);
294 }
295
296
297 /**
298
299 Returns TRUE if DEBUG_CODE()macros are enabled.
300
301 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of
302 PcdDebugProperyMask is set. Otherwise FALSE is returned.
303
304 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.
305 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.
306
307 **/
308 BOOLEAN
309 EFIAPI
310 DebugCodeEnabled (
311 VOID
312 )
313 {
314 return ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED) != 0);
315 }
316
317
318 /**
319
320 Returns TRUE if DEBUG_CLEAR_MEMORY()macro is enabled.
321
322 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CLEAR_MEMORY_ENABLED bit of
323 PcdDebugProperyMask is set. Otherwise FALSE is returned.
324
325 @retval TRUE The DEBUG_PROPERTY_DEBUG_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.
326 @retval FALSE The DEBUG_PROPERTY_DEBUG_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.
327
328 **/
329 BOOLEAN
330 EFIAPI
331 DebugClearMemoryEnabled (
332 VOID
333 )
334 {
335 return ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED) != 0);
336 }