]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/DxeDebugPrintErrorLevelLib/DxeDebugPrintErrorLevelLib.c
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@11399 6f19259b...
[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
29 /**
30 Retrieves the current debug print error level mask for a module are returns
31 it in CurrentDebugMask.
32
33 @param This The protocol instance pointer.
34 @param CurrentDebugMask Pointer to the debug print error level mask that
35 is returned.
36
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
41 not be retrieved.
42
43 **/
44 EFI_STATUS
45 EFIAPI
46 GetDebugMask (
47 IN EFI_DEBUG_MASK_PROTOCOL *This,
48 IN OUT UINTN *CurrentDebugMask
49 );
50
51 /**
52 Sets the current debug print error level mask for a module to the value
53 specified by NewDebugMask.
54
55 @param This The protocol instance pointer.
56 @param NewDebugMask The new debug print error level mask for this module.
57
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.
62
63 **/
64 EFI_STATUS
65 EFIAPI
66 SetDebugMask (
67 IN EFI_DEBUG_MASK_PROTOCOL *This,
68 IN UINTN NewDebugMask
69 );
70
71 ///
72 /// Debug Mask Protocol instance
73 ///
74 EFI_DEBUG_MASK_PROTOCOL mDebugMaskProtocol = {
75 EFI_DEBUG_MASK_REVISION,
76 GetDebugMask,
77 SetDebugMask
78 };
79
80 ///
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.
85 ///
86 BOOLEAN mGlobalErrorLevelInitialized = FALSE;
87
88 ///
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.
96 ///
97 UINT32 mDebugPrintErrorLevel = 0;
98
99 ///
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.
104 ///
105 EFI_SYSTEM_TABLE *mSystemTable = NULL;
106
107 /**
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.
110
111 @param ImageHandle The firmware allocated handle for the EFI image.
112 @param SystemTable A pointer to the EFI System Table.
113
114 @retval EFI_SUCCESS The constructor completed successfully.
115 @retval Other value The constructor did not complete successfully.
116
117 **/
118 EFI_STATUS
119 EFIAPI
120 DxeDebugPrintErrorLevelLibConstructor (
121 IN EFI_HANDLE ImageHandle,
122 IN EFI_SYSTEM_TABLE *SystemTable
123 )
124 {
125 EFI_STATUS Status;
126
127 //
128 // Initialize the error level mask from PCD setting.
129 //
130 mDebugPrintErrorLevel = PcdGet32 (PcdDebugPrintErrorLevel);
131
132 //
133 // Install Debug Mask Protocol onto ImageHandle
134 //
135 mSystemTable = SystemTable;
136 Status = SystemTable->BootServices->InstallMultipleProtocolInterfaces (
137 &ImageHandle,
138 &gEfiDebugMaskProtocolGuid, &mDebugMaskProtocol,
139 NULL
140 );
141
142 //
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.
147 //
148 GetDebugPrintErrorLevel ();
149
150 return Status;
151 }
152
153 /**
154 The destructor function frees any allocated buffers and closes the Set Virtual
155 Address Map event.
156
157 @param ImageHandle The firmware allocated handle for the EFI image.
158 @param SystemTable A pointer to the EFI System Table.
159
160 @retval EFI_SUCCESS The destructor completed successfully.
161 @retval Other value The destructor did not complete successfully.
162
163 **/
164 EFI_STATUS
165 EFIAPI
166 DxeDebugPrintErrorLevelLibDestructor (
167 IN EFI_HANDLE ImageHandle,
168 IN EFI_SYSTEM_TABLE *SystemTable
169 )
170 {
171 //
172 // Uninstall the Debug Mask Protocol from ImageHandle
173 //
174 return SystemTable->BootServices->UninstallMultipleProtocolInterfaces (
175 ImageHandle,
176 &gEfiDebugMaskProtocolGuid, &mDebugMaskProtocol,
177 NULL
178 );
179 }
180
181 /**
182 Returns the debug print error level mask for the current module.
183
184 @return Debug print error level mask for the current module.
185
186 **/
187 UINT32
188 EFIAPI
189 GetDebugPrintErrorLevel (
190 VOID
191 )
192 {
193 EFI_STATUS Status;
194 EFI_TPL CurrentTpl;
195 UINTN Size;
196 UINTN GlobalErrorLevel;
197
198 //
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
202 //
203 if (mSystemTable == NULL) {
204 return PcdGet32 (PcdDebugPrintErrorLevel);
205 }
206
207 //
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
212 //
213 if (!mGlobalErrorLevelInitialized) {
214 //
215 // Make sure the TPL Level is low enough for EFI Variable Services to be called
216 //
217 CurrentTpl = mSystemTable->BootServices->RaiseTPL (TPL_HIGH_LEVEL);
218 mSystemTable->BootServices->RestoreTPL (CurrentTpl);
219 if (CurrentTpl <= TPL_CALLBACK) {
220 //
221 // Attempt to retrieve the global debug print error level mask from the
222 // EFI Variable
223 //
224 Size = sizeof (GlobalErrorLevel);
225 Status = mSystemTable->RuntimeServices->GetVariable (
226 DEBUG_MASK_VARIABLE_NAME,
227 &gEfiGenericVariableGuid,
228 NULL,
229 &Size,
230 &GlobalErrorLevel
231 );
232 if (Status != EFI_NOT_AVAILABLE_YET) {
233 //
234 // If EFI Variable Services are available, then set a flag so the EFI
235 // Variable will not be read again by this module.
236 //
237 mGlobalErrorLevelInitialized = TRUE;
238 if (!EFI_ERROR (Status)) {
239 //
240 // If the EFI Varible exists, then set this module's module's mask to
241 // the global debug print error level mask value.
242 //
243 mDebugPrintErrorLevel = (UINT32)GlobalErrorLevel;
244 }
245 }
246 }
247 }
248
249 //
250 // Return the current mask value for this module.
251 //
252 return mDebugPrintErrorLevel;
253 }
254
255 /**
256 Sets the global debug print error level mask fpr the entire platform.
257
258 @param ErrorLevel Global debug print error level
259
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.
262
263 **/
264 BOOLEAN
265 EFIAPI
266 SetDebugPrintErrorLevel (
267 UINT32 ErrorLevel
268 )
269 {
270 EFI_STATUS Status;
271 EFI_TPL CurrentTpl;
272 UINTN Size;
273 UINTN GlobalErrorLevel;
274
275 //
276 // Make sure the constructor has been executed
277 //
278 if (mSystemTable != NULL) {
279 //
280 // Make sure the TPL Level is low enough for EFI Variable Services
281 //
282 CurrentTpl = mSystemTable->BootServices->RaiseTPL (TPL_HIGH_LEVEL);
283 mSystemTable->BootServices->RestoreTPL (CurrentTpl);
284 if (CurrentTpl <= TPL_CALLBACK) {
285 //
286 // Attempt to store the global debug print error level mask in an EFI Variable
287 //
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),
294 Size,
295 &GlobalErrorLevel
296 );
297 if (!EFI_ERROR (Status)) {
298 //
299 // If the EFI Variable was updated, then update the mask value for this
300 // module and return TRUE.
301 //
302 mGlobalErrorLevelInitialized = TRUE;
303 mDebugPrintErrorLevel = ErrorLevel;
304 return TRUE;
305 }
306 }
307 }
308 //
309 // Return FALSE since the EFI Variable could not be updated.
310 //
311 return FALSE;
312 }
313
314 /**
315 Retrieves the current debug print error level mask for a module are returns
316 it in CurrentDebugMask.
317
318 @param This The protocol instance pointer.
319 @param CurrentDebugMask Pointer to the debug print error level mask that
320 is returned.
321
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
326 not be retrieved.
327
328 **/
329 EFI_STATUS
330 EFIAPI
331 GetDebugMask (
332 IN EFI_DEBUG_MASK_PROTOCOL *This,
333 IN OUT UINTN *CurrentDebugMask
334 )
335 {
336 if (CurrentDebugMask == NULL) {
337 return EFI_INVALID_PARAMETER;
338 }
339
340 //
341 // Retrieve the current debug mask from mDebugPrintErrorLevel
342 //
343 *CurrentDebugMask = (UINTN)mDebugPrintErrorLevel;
344 return EFI_SUCCESS;
345 }
346
347 /**
348 Sets the current debug print error level mask for a module to the value
349 specified by NewDebugMask.
350
351 @param This The protocol instance pointer.
352 @param NewDebugMask The new debug print error level mask for this module.
353
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.
358
359 **/
360 EFI_STATUS
361 EFIAPI
362 SetDebugMask (
363 IN EFI_DEBUG_MASK_PROTOCOL *This,
364 IN UINTN NewDebugMask
365 )
366 {
367 //
368 // Store the new debug mask into mDebugPrintErrorLevel
369 //
370 mDebugPrintErrorLevel = (UINT32)NewDebugMask;
371 return EFI_SUCCESS;
372 }