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