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