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