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