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
31 IN EFI_DEBUG_MASK_PROTOCOL
*This
,
32 IN OUT UINTN
*CurrentDebugMask
38 IN EFI_DEBUG_MASK_PROTOCOL
*This
,
43 /// Debug Mask Protocol instance
45 EFI_DEBUG_MASK_PROTOCOL mDebugMaskProtocol
= {
46 EFI_DEBUG_MASK_REVISION
,
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.
57 BOOLEAN mGlobalErrorLevelInitialized
= FALSE
;
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.
68 UINT32 mDebugPrintErrorLevel
= 0;
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.
76 EFI_SYSTEM_TABLE
*mST
= NULL
;
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.
82 @param ImageHandle The firmware allocated handle for the EFI image.
83 @param SystemTable A pointer to the EFI System Table.
85 @retval EFI_SUCCESS The constructor completed successfully.
86 @retval Other value The constructor did not complete successfully.
91 DxeDebugPrintErrorLevelLibConstructor (
92 IN EFI_HANDLE ImageHandle
,
93 IN EFI_SYSTEM_TABLE
*SystemTable
99 // Initialize the error level mask from PCD setting.
101 mDebugPrintErrorLevel
= PcdGet32 (PcdDebugPrintErrorLevel
);
104 // Install Debug Mask Protocol onto ImageHandle
107 Status
= SystemTable
->BootServices
->InstallMultipleProtocolInterfaces (
109 &gEfiDebugMaskProtocolGuid
, &mDebugMaskProtocol
,
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.
119 GetDebugPrintErrorLevel ();
125 The destructor function frees any allocated buffers and closes the Set Virtual
128 @param ImageHandle The firmware allocated handle for the EFI image.
129 @param SystemTable A pointer to the EFI System Table.
131 @retval EFI_SUCCESS The destructor completed successfully.
132 @retval Other value The destructor did not complete successfully.
137 DxeDebugPrintErrorLevelLibDestructor (
138 IN EFI_HANDLE ImageHandle
,
139 IN EFI_SYSTEM_TABLE
*SystemTable
143 // Uninstall the Debug Mask Protocol from ImageHandle
145 return SystemTable
->BootServices
->UninstallMultipleProtocolInterfaces (
147 &gEfiDebugMaskProtocolGuid
, &mDebugMaskProtocol
,
153 Returns the debug print error level mask for the current module.
155 @return Debug print error level mask for the current module.
160 GetDebugPrintErrorLevel (
167 UINTN GlobalErrorLevel
;
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
175 return PcdGet32 (PcdDebugPrintErrorLevel
);
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
184 if (!mGlobalErrorLevelInitialized
) {
186 // Make sure the TPL Level is low enough for EFI Variable Services to be called
188 CurrentTpl
= mST
->BootServices
->RaiseTPL (TPL_HIGH_LEVEL
);
189 mST
->BootServices
->RestoreTPL (CurrentTpl
);
190 if (CurrentTpl
<= TPL_CALLBACK
) {
192 // Attempt to retrieve the global debug print error level mask from the
195 Size
= sizeof (GlobalErrorLevel
);
196 Status
= mST
->RuntimeServices
->GetVariable (
197 DEBUG_MASK_VARIABLE_NAME
,
198 &gEfiGenericVariableGuid
,
203 if (Status
!= EFI_NOT_AVAILABLE_YET
) {
205 // If EFI Variable Services are available, then set a flag so the EFI
206 // Variable will not be read again by this module.
208 mGlobalErrorLevelInitialized
= TRUE
;
209 if (!EFI_ERROR (Status
)) {
211 // If the EFI Varible exists, then set this module's module's mask to
212 // the global debug print error level mask value.
214 mDebugPrintErrorLevel
= (UINT32
)GlobalErrorLevel
;
221 // Return the current mask value for this module.
223 return mDebugPrintErrorLevel
;
227 Sets the global debug print error level mask fpr the entire platform.
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.
235 SetDebugPrintErrorLevel (
242 UINTN GlobalErrorLevel
;
245 // Make sure the constructor has been executed
249 // Make sure the TPL Level is low enough for EFI Variable Services
251 CurrentTpl
= mST
->BootServices
->RaiseTPL (TPL_HIGH_LEVEL
);
252 mST
->BootServices
->RestoreTPL (CurrentTpl
);
253 if (CurrentTpl
<= TPL_CALLBACK
) {
255 // Attempt to store the global debug print error level mask in an EFI Variable
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
),
266 if (!EFI_ERROR (Status
)) {
268 // If the EFI Variable was updated, then update the mask value for this
269 // module and return TRUE.
271 mGlobalErrorLevelInitialized
= TRUE
;
272 mDebugPrintErrorLevel
= ErrorLevel
;
278 // Return FALSE since the EFI Variable could not be updated.
284 Retrieves the current debug print error level mask for a module are returns
285 it in CurrentDebugMask.
287 @param This The protocol instance pointer.
288 @param CurrentDebugMask Pointer to the debug print error level mask that
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
301 IN EFI_DEBUG_MASK_PROTOCOL
*This
,
302 IN OUT UINTN
*CurrentDebugMask
305 if (CurrentDebugMask
== NULL
) {
306 return EFI_INVALID_PARAMETER
;
310 // Retrieve the current debug mask from mDebugPrintErrorLevel
312 *CurrentDebugMask
= (UINTN
)mDebugPrintErrorLevel
;
317 Sets the current debug print error level mask for a module to the value
318 specified by NewDebugMask.
320 @param This The protocol instance pointer.
321 @param NewDebugMask The new debug print error level mask for this module.
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.
332 IN EFI_DEBUG_MASK_PROTOCOL
*This
,
333 IN UINTN NewDebugMask
337 // Store the new debug mask into mDebugPrintErrorLevel
339 mDebugPrintErrorLevel
= (UINT32
)NewDebugMask
;