]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/DxeDebugPrintErrorLevelLib/DxeDebugPrintErrorLevelLib.c
Update DebugLib to provide support for "err" command in the EFI Shell to adjust the...
[mirror_edk2.git] / MdeModulePkg / Library / DxeDebugPrintErrorLevelLib / DxeDebugPrintErrorLevelLib.c
1 /** @file
2 Debug Print Error Level library instance that provide compatibility with the
3 "err" shell command. This includes support for the Debug Mask Protocol
4 supports for global debug print error level mask stored in an EFI Variable.
5 This library instance only support DXE Phase modules.
6
7 Copyright (c) 2011, 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 <PiDxe.h>
19
20 #include <Library/DebugPrintErrorLevelLib.h>
21 #include <Library/PcdLib.h>
22
23 #include <Guid/DebugMask.h>
24
25 ///
26 /// Debug Mask Protocol function prototypes
27 ///
28 EFI_STATUS
29 EFIAPI
30 GetDebugMask (
31 IN EFI_DEBUG_MASK_PROTOCOL *This,
32 IN OUT UINTN *CurrentDebugMask
33 );
34
35 EFI_STATUS
36 EFIAPI
37 SetDebugMask (
38 IN EFI_DEBUG_MASK_PROTOCOL *This,
39 IN UINTN NewDebugMask
40 );
41
42 ///
43 /// Debug Mask Protocol instance
44 ///
45 EFI_DEBUG_MASK_PROTOCOL mDebugMaskProtocol = {
46 EFI_DEBUG_MASK_REVISION,
47 GetDebugMask,
48 SetDebugMask
49 };
50
51 ///
52 /// Global variable that is set to TRUE after the first attempt is made to
53 /// retrieve the global error level mask through the EFI Varibale Services.
54 /// This variable prevents the EFI Variable Services from being called fort
55 /// every DEBUG() macro.
56 ///
57 BOOLEAN mGlobalErrorLevelInitialized = FALSE;
58
59 ///
60 /// Global variable that contains the current debug error level mask for the
61 /// module that is using this library instance. This variable is initially
62 /// set to the PcdDebugPrintErrorLevel value. If the EFI Variable exists that
63 /// contains the global debug print error level mask, then that override the
64 /// PcdDebugPrintErrorLevel value. If the Debug Mask Protocol SetDebugMask()
65 /// service is called, then that overrides bit the PcdDebugPrintErrorLevel and
66 /// the EFI Variable setting.
67 ///
68 UINT32 mDebugPrintErrorLevel = 0;
69
70 ///
71 /// Global variable that is used to cache a pointer to the EFI System Table
72 /// that is required to access the EFI Variable Services to get and set
73 /// the global debug print error level mask value. The UefiBootServicesTableLib
74 /// is not used to prevent a circular dependency between these libraries.
75 ///
76 EFI_SYSTEM_TABLE *mST = NULL;
77
78 /**
79 The constructor function caches the PCI Express Base Address and creates a
80 Set Virtual Address Map event to convert physical address to virtual addresses.
81
82 @param ImageHandle The firmware allocated handle for the EFI image.
83 @param SystemTable A pointer to the EFI System Table.
84
85 @retval EFI_SUCCESS The constructor completed successfully.
86 @retval Other value The constructor did not complete successfully.
87
88 **/
89 EFI_STATUS
90 EFIAPI
91 DxeDebugPrintErrorLevelLibConstructor (
92 IN EFI_HANDLE ImageHandle,
93 IN EFI_SYSTEM_TABLE *SystemTable
94 )
95 {
96 EFI_STATUS Status;
97
98 //
99 // Initialize the error level mask from PCD setting.
100 //
101 mDebugPrintErrorLevel = PcdGet32 (PcdDebugPrintErrorLevel);
102
103 //
104 // Install Debug Mask Protocol onto ImageHandle
105 //
106 mST = SystemTable;
107 Status = SystemTable->BootServices->InstallMultipleProtocolInterfaces (
108 &ImageHandle,
109 &gEfiDebugMaskProtocolGuid, &mDebugMaskProtocol,
110 NULL
111 );
112
113 //
114 // Attempt to retrieve the global debug print error level mask from the EFI Variable
115 // If the EFI Variable can not be accessed when this module's library constructors are
116 // executed, then the EFI Variable access will be reattempted on every DEBUG() print
117 // from this module until the EFI Variable services are available.
118 //
119 GetDebugPrintErrorLevel ();
120
121 return Status;
122 }
123
124 /**
125 The destructor function frees any allocated buffers and closes the Set Virtual
126 Address Map event.
127
128 @param ImageHandle The firmware allocated handle for the EFI image.
129 @param SystemTable A pointer to the EFI System Table.
130
131 @retval EFI_SUCCESS The destructor completed successfully.
132 @retval Other value The destructor did not complete successfully.
133
134 **/
135 EFI_STATUS
136 EFIAPI
137 DxeDebugPrintErrorLevelLibDestructor (
138 IN EFI_HANDLE ImageHandle,
139 IN EFI_SYSTEM_TABLE *SystemTable
140 )
141 {
142 //
143 // Uninstall the Debug Mask Protocol from ImageHandle
144 //
145 return SystemTable->BootServices->UninstallMultipleProtocolInterfaces (
146 ImageHandle,
147 &gEfiDebugMaskProtocolGuid, &mDebugMaskProtocol,
148 NULL
149 );
150 }
151
152 /**
153 Returns the debug print error level mask for the current module.
154
155 @return Debug print error level mask for the current module.
156
157 **/
158 UINT32
159 EFIAPI
160 GetDebugPrintErrorLevel (
161 VOID
162 )
163 {
164 EFI_STATUS Status;
165 EFI_TPL CurrentTpl;
166 UINTN Size;
167 UINTN GlobalErrorLevel;
168
169 //
170 // If the constructor has not been executed yet, then just return the PCD value.
171 // This case should only occur if debug print is generated by a library
172 // constructor for this module
173 //
174 if (mST == NULL) {
175 return PcdGet32 (PcdDebugPrintErrorLevel);
176 }
177
178 //
179 // Check to see if an attempt has been mde to retrieve the global debug print
180 // error level mask. Since this libtrary instance stores the global debug print
181 // error level mask in an EFI Variable, the EFI Variable should only be accessed
182 // once to reduce the overhead of reading the EFI Variable on every debug print
183 //
184 if (!mGlobalErrorLevelInitialized) {
185 //
186 // Make sure the TPL Level is low enough for EFI Variable Services to be called
187 //
188 CurrentTpl = mST->BootServices->RaiseTPL (TPL_HIGH_LEVEL);
189 mST->BootServices->RestoreTPL (CurrentTpl);
190 if (CurrentTpl <= TPL_CALLBACK) {
191 //
192 // Attempt to retrieve the global debug print error level mask from the
193 // EFI Variable
194 //
195 Size = sizeof (GlobalErrorLevel);
196 Status = mST->RuntimeServices->GetVariable (
197 DEBUG_MASK_VARIABLE_NAME,
198 &gEfiGenericVariableGuid,
199 NULL,
200 &Size,
201 &GlobalErrorLevel
202 );
203 if (Status != EFI_NOT_AVAILABLE_YET) {
204 //
205 // If EFI Variable Services are available, then set a flag so the EFI
206 // Variable will not be read again by this module.
207 //
208 mGlobalErrorLevelInitialized = TRUE;
209 if (!EFI_ERROR (Status)) {
210 //
211 // If the EFI Varible exists, then set this module's module's mask to
212 // the global debug print error level mask value.
213 //
214 mDebugPrintErrorLevel = (UINT32)GlobalErrorLevel;
215 }
216 }
217 }
218 }
219
220 //
221 // Return the current mask value for this module.
222 //
223 return mDebugPrintErrorLevel;
224 }
225
226 /**
227 Sets the global debug print error level mask fpr the entire platform.
228
229 @retval TRUE The debug print error level mask was sucessfully set.
230 @retval FALSE The debug print error level mask could not be set.
231
232 **/
233 BOOLEAN
234 EFIAPI
235 SetDebugPrintErrorLevel (
236 UINT32 ErrorLevel
237 )
238 {
239 EFI_STATUS Status;
240 EFI_TPL CurrentTpl;
241 UINTN Size;
242 UINTN GlobalErrorLevel;
243
244 //
245 // Make sure the constructor has been executed
246 //
247 if (mST != NULL) {
248 //
249 // Make sure the TPL Level is low enough for EFI Variable Services
250 //
251 CurrentTpl = mST->BootServices->RaiseTPL (TPL_HIGH_LEVEL);
252 mST->BootServices->RestoreTPL (CurrentTpl);
253 if (CurrentTpl <= TPL_CALLBACK) {
254 //
255 // Attempt to store the global debug print error level mask in an EFI Variable
256 //
257 GlobalErrorLevel = (UINTN)ErrorLevel;
258 Size = sizeof (GlobalErrorLevel);
259 Status = mST->RuntimeServices->SetVariable (
260 DEBUG_MASK_VARIABLE_NAME,
261 &gEfiGenericVariableGuid,
262 (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS),
263 Size,
264 &GlobalErrorLevel
265 );
266 if (!EFI_ERROR (Status)) {
267 //
268 // If the EFI Variable was updated, then update the mask value for this
269 // module and return TRUE.
270 //
271 mGlobalErrorLevelInitialized = TRUE;
272 mDebugPrintErrorLevel = ErrorLevel;
273 return TRUE;
274 }
275 }
276 }
277 //
278 // Return FALSE since the EFI Variable could not be updated.
279 //
280 return FALSE;
281 }
282
283 /**
284 Retrieves the current debug print error level mask for a module are returns
285 it in CurrentDebugMask.
286
287 @param This The protocol instance pointer.
288 @param CurrentDebugMask Pointer to the debug print error level mask that
289 is returned.
290
291 @retval EFI_SUCCESS The current debug print error level mask was
292 returned in CurrentDebugMask.
293 @retval EFI_INVALID_PARAMETER CurrentDebugMask is NULL.
294 @retval EFI_DEVICE_ERROR The current debug print error level mask could
295 not be retrieved.
296
297 **/
298 EFI_STATUS
299 EFIAPI
300 GetDebugMask (
301 IN EFI_DEBUG_MASK_PROTOCOL *This,
302 IN OUT UINTN *CurrentDebugMask
303 )
304 {
305 if (CurrentDebugMask == NULL) {
306 return EFI_INVALID_PARAMETER;
307 }
308
309 //
310 // Retrieve the current debug mask from mDebugPrintErrorLevel
311 //
312 *CurrentDebugMask = (UINTN)mDebugPrintErrorLevel;
313 return EFI_SUCCESS;
314 }
315
316 /**
317 Sets the current debug print error level mask for a module to the value
318 specified by NewDebugMask.
319
320 @param This The protocol instance pointer.
321 @param NewDebugMask The new debug print error level mask for this module.
322
323 @retval EFI_SUCCESS The current debug print error level mask was
324 set to the value specified by NewDebugMask.
325 @retval EFI_DEVICE_ERROR The current debug print error level mask could
326 not be set to the value specified by NewDebugMask.
327
328 **/
329 EFI_STATUS
330 EFIAPI
331 SetDebugMask (
332 IN EFI_DEBUG_MASK_PROTOCOL *This,
333 IN UINTN NewDebugMask
334 )
335 {
336 //
337 // Store the new debug mask into mDebugPrintErrorLevel
338 //
339 mDebugPrintErrorLevel = (UINT32)NewDebugMask;
340 return EFI_SUCCESS;
341 }