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>
23 #include <Guid/DebugMask.h>
26 /// Debug Mask Protocol function prototypes
30 Retrieves the current debug print error level mask for a module are returns
31 it in CurrentDebugMask.
33 @param This The protocol instance pointer.
34 @param CurrentDebugMask Pointer to the debug print error level mask that
37 @retval EFI_SUCCESS The current debug print error level mask was
38 returned in CurrentDebugMask.
39 @retval EFI_INVALID_PARAMETER CurrentDebugMask is NULL.
40 @retval EFI_DEVICE_ERROR The current debug print error level mask could
47 IN EFI_DEBUG_MASK_PROTOCOL
*This
,
48 IN OUT UINTN
*CurrentDebugMask
52 Sets the current debug print error level mask for a module to the value
53 specified by NewDebugMask.
55 @param This The protocol instance pointer.
56 @param NewDebugMask The new debug print error level mask for this module.
58 @retval EFI_SUCCESS The current debug print error level mask was
59 set to the value specified by NewDebugMask.
60 @retval EFI_DEVICE_ERROR The current debug print error level mask could
61 not be set to the value specified by NewDebugMask.
67 IN EFI_DEBUG_MASK_PROTOCOL
*This
,
72 /// Debug Mask Protocol instance
74 EFI_DEBUG_MASK_PROTOCOL mDebugMaskProtocol
= {
75 EFI_DEBUG_MASK_REVISION
,
81 /// Global variable that is set to TRUE after the first attempt is made to
82 /// retrieve the global error level mask through the EFI Varibale Services.
83 /// This variable prevents the EFI Variable Services from being called fort
84 /// every DEBUG() macro.
86 BOOLEAN mGlobalErrorLevelInitialized
= FALSE
;
89 /// Global variable that contains the current debug error level mask for the
90 /// module that is using this library instance. This variable is initially
91 /// set to the PcdDebugPrintErrorLevel value. If the EFI Variable exists that
92 /// contains the global debug print error level mask, then that override the
93 /// PcdDebugPrintErrorLevel value. If the Debug Mask Protocol SetDebugMask()
94 /// service is called, then that overrides bit the PcdDebugPrintErrorLevel and
95 /// the EFI Variable setting.
97 UINT32 mDebugPrintErrorLevel
= 0;
100 /// Global variable that is used to cache a pointer to the EFI System Table
101 /// that is required to access the EFI Variable Services to get and set
102 /// the global debug print error level mask value. The UefiBootServicesTableLib
103 /// is not used to prevent a circular dependency between these libraries.
105 EFI_SYSTEM_TABLE
*mSystemTable
= NULL
;
108 The constructor function caches the PCI Express Base Address and creates a
109 Set Virtual Address Map event to convert physical address to virtual addresses.
111 @param ImageHandle The firmware allocated handle for the EFI image.
112 @param SystemTable A pointer to the EFI System Table.
114 @retval EFI_SUCCESS The constructor completed successfully.
115 @retval Other value The constructor did not complete successfully.
120 DxeDebugPrintErrorLevelLibConstructor (
121 IN EFI_HANDLE ImageHandle
,
122 IN EFI_SYSTEM_TABLE
*SystemTable
128 // Initialize the error level mask from PCD setting.
130 mDebugPrintErrorLevel
= PcdGet32 (PcdDebugPrintErrorLevel
);
133 // Install Debug Mask Protocol onto ImageHandle
135 mSystemTable
= SystemTable
;
136 Status
= SystemTable
->BootServices
->InstallMultipleProtocolInterfaces (
138 &gEfiDebugMaskProtocolGuid
, &mDebugMaskProtocol
,
143 // Attempt to retrieve the global debug print error level mask from the EFI Variable
144 // If the EFI Variable can not be accessed when this module's library constructors are
145 // executed, then the EFI Variable access will be reattempted on every DEBUG() print
146 // from this module until the EFI Variable services are available.
148 GetDebugPrintErrorLevel ();
154 The destructor function frees any allocated buffers and closes the Set Virtual
157 @param ImageHandle The firmware allocated handle for the EFI image.
158 @param SystemTable A pointer to the EFI System Table.
160 @retval EFI_SUCCESS The destructor completed successfully.
161 @retval Other value The destructor did not complete successfully.
166 DxeDebugPrintErrorLevelLibDestructor (
167 IN EFI_HANDLE ImageHandle
,
168 IN EFI_SYSTEM_TABLE
*SystemTable
172 // Uninstall the Debug Mask Protocol from ImageHandle
174 return SystemTable
->BootServices
->UninstallMultipleProtocolInterfaces (
176 &gEfiDebugMaskProtocolGuid
, &mDebugMaskProtocol
,
182 Returns the debug print error level mask for the current module.
184 @return Debug print error level mask for the current module.
189 GetDebugPrintErrorLevel (
196 UINTN GlobalErrorLevel
;
199 // If the constructor has not been executed yet, then just return the PCD value.
200 // This case should only occur if debug print is generated by a library
201 // constructor for this module
203 if (mSystemTable
== NULL
) {
204 return PcdGet32 (PcdDebugPrintErrorLevel
);
208 // Check to see if an attempt has been mde to retrieve the global debug print
209 // error level mask. Since this libtrary instance stores the global debug print
210 // error level mask in an EFI Variable, the EFI Variable should only be accessed
211 // once to reduce the overhead of reading the EFI Variable on every debug print
213 if (!mGlobalErrorLevelInitialized
) {
215 // Make sure the TPL Level is low enough for EFI Variable Services to be called
217 CurrentTpl
= mSystemTable
->BootServices
->RaiseTPL (TPL_HIGH_LEVEL
);
218 mSystemTable
->BootServices
->RestoreTPL (CurrentTpl
);
219 if (CurrentTpl
<= TPL_CALLBACK
) {
221 // Attempt to retrieve the global debug print error level mask from the
224 Size
= sizeof (GlobalErrorLevel
);
225 Status
= mSystemTable
->RuntimeServices
->GetVariable (
226 DEBUG_MASK_VARIABLE_NAME
,
227 &gEfiGenericVariableGuid
,
232 if (Status
!= EFI_NOT_AVAILABLE_YET
) {
234 // If EFI Variable Services are available, then set a flag so the EFI
235 // Variable will not be read again by this module.
237 mGlobalErrorLevelInitialized
= TRUE
;
238 if (!EFI_ERROR (Status
)) {
240 // If the EFI Varible exists, then set this module's module's mask to
241 // the global debug print error level mask value.
243 mDebugPrintErrorLevel
= (UINT32
)GlobalErrorLevel
;
250 // Return the current mask value for this module.
252 return mDebugPrintErrorLevel
;
256 Sets the global debug print error level mask fpr the entire platform.
258 @param ErrorLevel Global debug print error level
260 @retval TRUE The debug print error level mask was sucessfully set.
261 @retval FALSE The debug print error level mask could not be set.
266 SetDebugPrintErrorLevel (
273 UINTN GlobalErrorLevel
;
276 // Make sure the constructor has been executed
278 if (mSystemTable
!= NULL
) {
280 // Make sure the TPL Level is low enough for EFI Variable Services
282 CurrentTpl
= mSystemTable
->BootServices
->RaiseTPL (TPL_HIGH_LEVEL
);
283 mSystemTable
->BootServices
->RestoreTPL (CurrentTpl
);
284 if (CurrentTpl
<= TPL_CALLBACK
) {
286 // Attempt to store the global debug print error level mask in an EFI Variable
288 GlobalErrorLevel
= (UINTN
)ErrorLevel
;
289 Size
= sizeof (GlobalErrorLevel
);
290 Status
= mSystemTable
->RuntimeServices
->SetVariable (
291 DEBUG_MASK_VARIABLE_NAME
,
292 &gEfiGenericVariableGuid
,
293 (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
),
297 if (!EFI_ERROR (Status
)) {
299 // If the EFI Variable was updated, then update the mask value for this
300 // module and return TRUE.
302 mGlobalErrorLevelInitialized
= TRUE
;
303 mDebugPrintErrorLevel
= ErrorLevel
;
309 // Return FALSE since the EFI Variable could not be updated.
315 Retrieves the current debug print error level mask for a module are returns
316 it in CurrentDebugMask.
318 @param This The protocol instance pointer.
319 @param CurrentDebugMask Pointer to the debug print error level mask that
322 @retval EFI_SUCCESS The current debug print error level mask was
323 returned in CurrentDebugMask.
324 @retval EFI_INVALID_PARAMETER CurrentDebugMask is NULL.
325 @retval EFI_DEVICE_ERROR The current debug print error level mask could
332 IN EFI_DEBUG_MASK_PROTOCOL
*This
,
333 IN OUT UINTN
*CurrentDebugMask
336 if (CurrentDebugMask
== NULL
) {
337 return EFI_INVALID_PARAMETER
;
341 // Retrieve the current debug mask from mDebugPrintErrorLevel
343 *CurrentDebugMask
= (UINTN
)mDebugPrintErrorLevel
;
348 Sets the current debug print error level mask for a module to the value
349 specified by NewDebugMask.
351 @param This The protocol instance pointer.
352 @param NewDebugMask The new debug print error level mask for this module.
354 @retval EFI_SUCCESS The current debug print error level mask was
355 set to the value specified by NewDebugMask.
356 @retval EFI_DEVICE_ERROR The current debug print error level mask could
357 not be set to the value specified by NewDebugMask.
363 IN EFI_DEBUG_MASK_PROTOCOL
*This
,
364 IN UINTN NewDebugMask
368 // Store the new debug mask into mDebugPrintErrorLevel
370 mDebugPrintErrorLevel
= (UINT32
)NewDebugMask
;