1 /*++
2 
3 Copyright (c) 2004 - 2006, Intel Corporation. All rights reserved.<BR>
4 This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution.  The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8 
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11 
12 
13 Module Name:
14 
15   DebugLib.c
16 
17 Abstract:
18 
19   Debug Library that fowards all messages to ReportStatusCode()
20 
21 --*/
22 
23 #include "EdkIIGlueDxe.h"
24 
25 /**
26 
27   Prints a debug message to the debug output device if the specified error level is enabled.
28 
29   If any bit in ErrorLevel is also set in PcdDebugPrintErrorLevel, then print
30   the message specified by Format and the associated variable argument list to
31   the debug output device.
32 
33   If Format is NULL, then ASSERT().
34 
35   @param  ErrorLevel  The error level of the debug message.
36   @param  Format      Format string for the debug message to print.
37 
38 **/
39 VOID
40 EFIAPI
DebugPrint(IN UINTN ErrorLevel,IN CONST CHAR8 * Format,...)41 DebugPrint (
42   IN  UINTN        ErrorLevel,
43   IN  CONST CHAR8  *Format,
44   ...
45   )
46 {
47   UINT64          Buffer[EFI_STATUS_CODE_DATA_MAX_SIZE / sizeof (UINT64)];
48   EFI_DEBUG_INFO  *DebugInfo;
49   UINTN           TotalSize;
50   UINTN           Index;
51   VA_LIST         Marker;
52   UINT64          *ArgumentPointer;
53 
54   //
55   // If Format is NULL, then ASSERT().
56   //
57   ASSERT (Format != NULL);
58 
59   //
60   // Check driver Debug Level value and global debug level
61   //
62   if ((ErrorLevel & PcdGet32(PcdDebugPrintErrorLevel)) == 0) {
63     return;
64   }
65 
66   TotalSize = sizeof (EFI_DEBUG_INFO) + 12 * sizeof (UINT64) + AsciiStrLen (Format) + 1;
67   if (TotalSize > EFI_STATUS_CODE_DATA_MAX_SIZE) {
68     return;
69   }
70 
71   //
72   // Then EFI_DEBUG_INFO
73   //
74   DebugInfo = (EFI_DEBUG_INFO *)Buffer;
75   DebugInfo->ErrorLevel = (UINT32)ErrorLevel;
76 
77   //
78   // 256 byte mini Var Arg stack. That is followed by the format string.
79   //
80   VA_START (Marker, Format);
81   for (Index = 0, ArgumentPointer = (UINT64 *)(DebugInfo + 1); Index < 12; Index++, ArgumentPointer++) {
82     WriteUnaligned64(ArgumentPointer, VA_ARG (Marker, UINT64));
83   }
84   VA_END (Marker);
85   AsciiStrCpy ((CHAR8 *)ArgumentPointer, Format);
86 
87   REPORT_STATUS_CODE_EX (
88     EFI_DEBUG_CODE,
89     (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_DC_UNSPECIFIED),
90     0,
91     NULL,
92     &gEfiStatusCodeDataTypeDebugGuid,
93     DebugInfo,
94     TotalSize
95     );
96 }
97 
98 
99 /**
100 
101   Prints an assert message containing a filename, line number, and description.
102   This may be followed by a breakpoint or a dead loop.
103 
104   Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"
105   to the debug output device.  If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of
106   PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if
107   DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then
108   CpuDeadLoop() is called.  If neither of these bits are set, then this function
109   returns immediately after the message is printed to the debug output device.
110   DebugAssert() must actively prevent recusrsion.  If DebugAssert() is called while
111   processing another DebugAssert(), then DebugAssert() must return immediately.
112 
113   If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.
114 
115   If Description is NULL, then a <Description> string of "(NULL) Description" is printed.
116 
117   @param  FileName     Pointer to the name of the source file that generated the assert condition.
118   @param  LineNumber   The line number in the source file that generated the assert condition
119   @param  Description  Pointer to the description of the assert condition.
120 
121 **/
122 VOID
123 EFIAPI
DebugAssert(IN CONST CHAR8 * FileName,IN UINTN LineNumber,IN CONST CHAR8 * Description)124 DebugAssert (
125   IN CONST CHAR8  *FileName,
126   IN UINTN        LineNumber,
127   IN CONST CHAR8  *Description
128   )
129 {
130   UINT64                 Buffer[EFI_STATUS_CODE_DATA_MAX_SIZE / sizeof(UINT64)];
131   EFI_DEBUG_ASSERT_DATA  *AssertData;
132   UINTN                  TotalSize;
133   CHAR8                  *Temp;
134   UINTN                  FileNameLength;
135   UINTN                  DescriptionLength;
136 
137   //
138   // Make sure it will all fit in the passed in buffer
139   //
140   FileNameLength    = AsciiStrLen (FileName);
141   DescriptionLength = AsciiStrLen (Description);
142   TotalSize = sizeof (EFI_DEBUG_ASSERT_DATA) + FileNameLength + 1 + DescriptionLength + 1;
143   if (TotalSize <= EFI_STATUS_CODE_DATA_MAX_SIZE) {
144     //
145     // Fill in EFI_DEBUG_ASSERT_DATA
146     //
147     AssertData = (EFI_DEBUG_ASSERT_DATA *)Buffer;
148     AssertData->LineNumber = (UINT32)LineNumber;
149 
150     //
151     // Copy Ascii FileName including NULL.
152     //
153     Temp = AsciiStrCpy ((CHAR8 *)(AssertData + 1), FileName);
154 
155     //
156     // Copy Ascii Description
157     //
158     AsciiStrCpy (Temp + AsciiStrLen (FileName) + 1, Description);
159 
160     REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
161       (EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED),
162       (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_EC_ILLEGAL_SOFTWARE_STATE),
163       AssertData,
164       TotalSize
165       );
166   }
167 
168   //
169   // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings
170   //
171   if ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED) != 0) {
172     CpuBreakpoint ();
173   } else if ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED) != 0) {
174     CpuDeadLoop ();
175   }
176 }
177 
178 
179 /**
180 
181   Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.
182 
183   This function fills Length bytes of Buffer with the value specified by
184   PcdDebugClearMemoryValue, and returns Buffer.
185 
186   If Buffer is NULL, then ASSERT().
187 
188   If Length is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT().
189 
190   @param   Buffer  Pointer to the target buffer to fill with PcdDebugClearMemoryValue.
191   @param   Length  Number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.
192 
193   @return  Buffer
194 
195 **/
196 VOID *
197 EFIAPI
DebugClearMemory(OUT VOID * Buffer,IN UINTN Length)198 DebugClearMemory (
199   OUT VOID  *Buffer,
200   IN UINTN  Length
201   )
202 {
203   //
204   // If Buffer is NULL, then ASSERT().
205   //
206   ASSERT (Buffer != NULL);
207 
208   //
209   // SetMem() checks for the the ASSERT() condition on Length and returns Buffer
210   //
211   return SetMem (Buffer, Length, PcdGet8(PcdDebugClearMemoryValue));
212 }
213 
214