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.
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.
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.
20 #include <Library/DebugPrintErrorLevelLib.h>
21 #include <Library/PcdLib.h>
22 #include <Library/HobLib.h>
24 #include <Guid/DebugMask.h>
27 /// Debug Mask Protocol function prototypes
31 Retrieves the current debug print error level mask for a module are returns
32 it in CurrentDebugMask.
34 @param This The protocol instance pointer.
35 @param CurrentDebugMask Pointer to the debug print error level mask that
38 @retval EFI_SUCCESS The current debug print error level mask was
39 returned in CurrentDebugMask.
40 @retval EFI_INVALID_PARAMETER CurrentDebugMask is NULL.
41 @retval EFI_DEVICE_ERROR The current debug print error level mask could
48 IN EFI_DEBUG_MASK_PROTOCOL
*This
,
49 IN OUT UINTN
*CurrentDebugMask
53 Sets the current debug print error level mask for a module to the value
54 specified by NewDebugMask.
56 @param This The protocol instance pointer.
57 @param NewDebugMask The new debug print error level mask for this module.
59 @retval EFI_SUCCESS The current debug print error level mask was
60 set to the value specified by NewDebugMask.
61 @retval EFI_DEVICE_ERROR The current debug print error level mask could
62 not be set to the value specified by NewDebugMask.
68 IN EFI_DEBUG_MASK_PROTOCOL
*This
,
73 /// Debug Mask Protocol instance
75 EFI_DEBUG_MASK_PROTOCOL mDebugMaskProtocol
= {
76 EFI_DEBUG_MASK_REVISION
,
82 /// Global variable that is set to TRUE after the first attempt is made to
83 /// retrieve the global error level mask through the EFI Varibale Services.
84 /// This variable prevents the EFI Variable Services from being called fort
85 /// every DEBUG() macro.
87 BOOLEAN mGlobalErrorLevelInitialized
= FALSE
;
90 /// Global variable that contains the current debug error level mask for the
91 /// module that is using this library instance. This variable is initially
92 /// set to the PcdDebugPrintErrorLevel value. If the EFI Variable exists that
93 /// contains the global debug print error level mask, then that overrides the
94 /// PcdDebugPrintErrorLevel value. The EFI Variable can optionally be
95 /// discovered via a HOB so early DXE drivers can access the variable. If the
96 /// Debug Mask Protocol SetDebugMask() service is called, then that overrides
97 /// the PcdDebugPrintErrorLevel and the EFI Variable setting.
99 UINT32 mDebugPrintErrorLevel
= 0;
102 /// Global variable that is used to cache a pointer to the EFI System Table
103 /// that is required to access the EFI Variable Services to get and set
104 /// the global debug print error level mask value. The UefiBootServicesTableLib
105 /// is not used to prevent a circular dependency between these libraries.
107 EFI_SYSTEM_TABLE
*mSystemTable
= NULL
;
110 The constructor function caches the PCI Express Base Address and creates a
111 Set Virtual Address Map event to convert physical address to virtual addresses.
113 @param ImageHandle The firmware allocated handle for the EFI image.
114 @param SystemTable A pointer to the EFI System Table.
116 @retval EFI_SUCCESS The constructor completed successfully.
117 @retval Other value The constructor did not complete successfully.
122 DxeDebugPrintErrorLevelLibConstructor (
123 IN EFI_HANDLE ImageHandle
,
124 IN EFI_SYSTEM_TABLE
*SystemTable
130 // Initialize the error level mask from PCD setting.
132 mDebugPrintErrorLevel
= PcdGet32 (PcdDebugPrintErrorLevel
);
135 // Install Debug Mask Protocol onto ImageHandle
137 mSystemTable
= SystemTable
;
138 Status
= SystemTable
->BootServices
->InstallMultipleProtocolInterfaces (
140 &gEfiDebugMaskProtocolGuid
, &mDebugMaskProtocol
,
145 // Attempt to retrieve the global debug print error level mask from the EFI Variable
146 // If the EFI Variable can not be accessed when this module's library constructors are
147 // executed a HOB can be used to set the global debug print error level. If no value
148 // was found then the EFI Variable access will be reattempted on every DEBUG() print
149 // from this module until the EFI Variable services are available.
151 GetDebugPrintErrorLevel ();
157 The destructor function frees any allocated buffers and closes the Set Virtual
160 @param ImageHandle The firmware allocated handle for the EFI image.
161 @param SystemTable A pointer to the EFI System Table.
163 @retval EFI_SUCCESS The destructor completed successfully.
164 @retval Other value The destructor did not complete successfully.
169 DxeDebugPrintErrorLevelLibDestructor (
170 IN EFI_HANDLE ImageHandle
,
171 IN EFI_SYSTEM_TABLE
*SystemTable
175 // Uninstall the Debug Mask Protocol from ImageHandle
177 return SystemTable
->BootServices
->UninstallMultipleProtocolInterfaces (
179 &gEfiDebugMaskProtocolGuid
, &mDebugMaskProtocol
,
185 Returns the debug print error level mask for the current module.
187 @return Debug print error level mask for the current module.
192 GetDebugPrintErrorLevel (
199 UINTN GlobalErrorLevel
;
203 // If the constructor has not been executed yet, then just return the PCD value.
204 // This case should only occur if debug print is generated by a library
205 // constructor for this module
207 if (mSystemTable
== NULL
) {
208 return PcdGet32 (PcdDebugPrintErrorLevel
);
212 // Check to see if an attempt has been made to retrieve the global debug print
213 // error level mask. Since this library instance stores the global debug print
214 // error level mask in an EFI Variable, the EFI Variable should only be accessed
215 // once to reduce the overhead of reading the EFI Variable on every debug print
217 if (!mGlobalErrorLevelInitialized
) {
219 // Make sure the TPL Level is low enough for EFI Variable Services to be called
221 CurrentTpl
= mSystemTable
->BootServices
->RaiseTPL (TPL_HIGH_LEVEL
);
222 mSystemTable
->BootServices
->RestoreTPL (CurrentTpl
);
223 if (CurrentTpl
<= TPL_CALLBACK
) {
225 // Attempt to retrieve the global debug print error level mask from the
228 Size
= sizeof (GlobalErrorLevel
);
229 Status
= mSystemTable
->RuntimeServices
->GetVariable (
230 DEBUG_MASK_VARIABLE_NAME
,
231 &gEfiGenericVariableGuid
,
236 if (Status
!= EFI_NOT_AVAILABLE_YET
) {
238 // If EFI Variable Services are available, then set a flag so the EFI
239 // Variable will not be read again by this module.
241 mGlobalErrorLevelInitialized
= TRUE
;
242 if (!EFI_ERROR (Status
)) {
244 // If the EFI Varible exists, then set this module's module's mask to
245 // the global debug print error level mask value.
247 mDebugPrintErrorLevel
= (UINT32
)GlobalErrorLevel
;
251 // If variable services are not yet available optionally get the global
252 // debug print error level mask from a HOB.
254 Hob
= GetFirstGuidHob (&gEfiGenericVariableGuid
);
256 if (GET_GUID_HOB_DATA_SIZE (Hob
) == sizeof (UINT32
)) {
257 mDebugPrintErrorLevel
= *(UINT32
*)GET_GUID_HOB_DATA (Hob
);
258 mGlobalErrorLevelInitialized
= TRUE
;
266 // Return the current mask value for this module.
268 return mDebugPrintErrorLevel
;
272 Sets the global debug print error level mask fpr the entire platform.
274 @param ErrorLevel Global debug print error level
276 @retval TRUE The debug print error level mask was sucessfully set.
277 @retval FALSE The debug print error level mask could not be set.
282 SetDebugPrintErrorLevel (
289 UINTN GlobalErrorLevel
;
292 // Make sure the constructor has been executed
294 if (mSystemTable
!= NULL
) {
296 // Make sure the TPL Level is low enough for EFI Variable Services
298 CurrentTpl
= mSystemTable
->BootServices
->RaiseTPL (TPL_HIGH_LEVEL
);
299 mSystemTable
->BootServices
->RestoreTPL (CurrentTpl
);
300 if (CurrentTpl
<= TPL_CALLBACK
) {
302 // Attempt to store the global debug print error level mask in an EFI Variable
304 GlobalErrorLevel
= (UINTN
)ErrorLevel
;
305 Size
= sizeof (GlobalErrorLevel
);
306 Status
= mSystemTable
->RuntimeServices
->SetVariable (
307 DEBUG_MASK_VARIABLE_NAME
,
308 &gEfiGenericVariableGuid
,
309 (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
),
313 if (!EFI_ERROR (Status
)) {
315 // If the EFI Variable was updated, then update the mask value for this
316 // module and return TRUE.
318 mGlobalErrorLevelInitialized
= TRUE
;
319 mDebugPrintErrorLevel
= ErrorLevel
;
325 // Return FALSE since the EFI Variable could not be updated.
331 Retrieves the current debug print error level mask for a module are returns
332 it in CurrentDebugMask.
334 @param This The protocol instance pointer.
335 @param CurrentDebugMask Pointer to the debug print error level mask that
338 @retval EFI_SUCCESS The current debug print error level mask was
339 returned in CurrentDebugMask.
340 @retval EFI_INVALID_PARAMETER CurrentDebugMask is NULL.
341 @retval EFI_DEVICE_ERROR The current debug print error level mask could
348 IN EFI_DEBUG_MASK_PROTOCOL
*This
,
349 IN OUT UINTN
*CurrentDebugMask
352 if (CurrentDebugMask
== NULL
) {
353 return EFI_INVALID_PARAMETER
;
357 // Retrieve the current debug mask from mDebugPrintErrorLevel
359 *CurrentDebugMask
= (UINTN
)mDebugPrintErrorLevel
;
364 Sets the current debug print error level mask for a module to the value
365 specified by NewDebugMask.
367 @param This The protocol instance pointer.
368 @param NewDebugMask The new debug print error level mask for this module.
370 @retval EFI_SUCCESS The current debug print error level mask was
371 set to the value specified by NewDebugMask.
372 @retval EFI_DEVICE_ERROR The current debug print error level mask could
373 not be set to the value specified by NewDebugMask.
379 IN EFI_DEBUG_MASK_PROTOCOL
*This
,
380 IN UINTN NewDebugMask
384 // Store the new debug mask into mDebugPrintErrorLevel
386 mDebugPrintErrorLevel
= (UINT32
)NewDebugMask
;