]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
MdeModulePkg: Variable drivers robustly handle crashes during Reclaim().
[mirror_edk2.git] / MdeModulePkg / Universal / Variable / RuntimeDxe / Variable.h
CommitLineData
504214c4 1/** @file\r
8d3a5c82 2\r
504214c4 3 The internal header file includes the common header files, defines\r
8a2d4996 4 internal structure and functions used by Variable modules.\r
504214c4 5\r
23b06935 6Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>\r
e5eed7d3 7This program and the accompanying materials\r
8d3a5c82 8are licensed and made available under the terms and conditions of the BSD License\r
9which accompanies this distribution. The full text of the license may be found at\r
10http://opensource.org/licenses/bsd-license.php\r
11\r
12THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
13WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14\r
504214c4 15**/\r
8d3a5c82 16\r
7c80e839 17#ifndef _VARIABLE_H_\r
18#define _VARIABLE_H_\r
8d3a5c82 19\r
39099938 20#include <PiDxe.h>\r
8d3a5c82 21#include <Protocol/VariableWrite.h>\r
88a5561c 22#include <Protocol/FaultTolerantWrite.h>\r
8d3a5c82 23#include <Protocol/FirmwareVolumeBlock.h>\r
24#include <Protocol/Variable.h>\r
ff843847 25#include <Protocol/VariableLock.h>\r
8d3a5c82 26#include <Library/PcdLib.h>\r
0f7aff72 27#include <Library/HobLib.h>\r
8d3a5c82 28#include <Library/UefiDriverEntryPoint.h>\r
29#include <Library/DxeServicesTableLib.h>\r
30#include <Library/UefiRuntimeLib.h>\r
31#include <Library/DebugLib.h>\r
32#include <Library/BaseMemoryLib.h>\r
8d3a5c82 33#include <Library/UefiBootServicesTableLib.h>\r
34#include <Library/UefiLib.h>\r
35#include <Library/BaseLib.h>\r
4cf894eb 36#include <Library/SynchronizationLib.h>\r
8d3a5c82 37#include <Library/MemoryAllocationLib.h>\r
052ad7e1 38#include <Guid/GlobalVariable.h>\r
01a5c994 39#include <Guid/EventGroup.h>\r
3709c4cd 40#include <Guid/VariableFormat.h>\r
d6550260 41#include <Guid/SystemNvDataGuid.h>\r
3e02ebb2 42#include <Guid/FaultTolerantWrite.h>\r
a5f15e30 43#include <Guid/HardwareErrorVariable.h>\r
8d3a5c82 44\r
45#define VARIABLE_RECLAIM_THRESHOLD (1024)\r
46\r
255a3f33
RN
47///\r
48/// The size of a 3 character ISO639 language code.\r
49///\r
50#define ISO_639_2_ENTRY_SIZE 3\r
51\r
0f7aff72
RN
52typedef enum {\r
53 VariableStoreTypeVolatile,\r
54 VariableStoreTypeHob,\r
55 VariableStoreTypeNv,\r
56 VariableStoreTypeMax\r
57} VARIABLE_STORE_TYPE;\r
58\r
8d3a5c82 59typedef struct {\r
60 VARIABLE_HEADER *CurrPtr;\r
23b06935
SZ
61 //\r
62 // If both ADDED and IN_DELETED_TRANSITION variable are present,\r
63 // InDeletedTransitionPtr will point to the IN_DELETED_TRANSITION one.\r
64 // Otherwise, CurrPtr will point to the ADDED or IN_DELETED_TRANSITION one,\r
65 // and InDeletedTransitionPtr will be NULL at the same time.\r
66 //\r
67 VARIABLE_HEADER *InDeletedTransitionPtr;\r
8d3a5c82 68 VARIABLE_HEADER *EndPtr;\r
69 VARIABLE_HEADER *StartPtr;\r
70 BOOLEAN Volatile;\r
71} VARIABLE_POINTER_TRACK;\r
72\r
73typedef struct {\r
0f7aff72 74 EFI_PHYSICAL_ADDRESS HobVariableBase;\r
8d3a5c82 75 EFI_PHYSICAL_ADDRESS VolatileVariableBase;\r
76 EFI_PHYSICAL_ADDRESS NonVolatileVariableBase;\r
77 EFI_LOCK VariableServicesLock;\r
fdb7765f 78 UINT32 ReentrantState;\r
8d3a5c82 79} VARIABLE_GLOBAL;\r
80\r
81typedef struct {\r
052ad7e1 82 VARIABLE_GLOBAL VariableGlobal;\r
8d3a5c82 83 UINTN VolatileLastVariableOffset;\r
84 UINTN NonVolatileLastVariableOffset;\r
2fcdca1d 85 UINTN CommonVariableTotalSize;\r
86 UINTN HwErrVariableTotalSize;\r
255a3f33
RN
87 CHAR8 *PlatformLangCodes;\r
88 CHAR8 *LangCodes;\r
89 CHAR8 *PlatformLang;\r
90 CHAR8 Lang[ISO_639_2_ENTRY_SIZE + 1];\r
8a9e0b72 91 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvbInstance;\r
052ad7e1
A
92} VARIABLE_MODULE_GLOBAL;\r
93\r
94typedef struct {\r
95 EFI_GUID *Guid;\r
96 CHAR16 *Name;\r
97 UINT32 Attributes;\r
98 UINTN DataSize;\r
99 VOID *Data;\r
100} VARIABLE_CACHE_ENTRY;\r
8d3a5c82 101\r
ff843847
RN
102typedef struct {\r
103 EFI_GUID Guid;\r
104 CHAR16 *Name;\r
105 LIST_ENTRY Link;\r
106} VARIABLE_ENTRY;\r
107\r
335e2681
SZ
108/**\r
109 Flush the HOB variable to flash.\r
110\r
111 @param[in] VariableName Name of variable has been updated or deleted.\r
112 @param[in] VendorGuid Guid of variable has been updated or deleted.\r
113\r
114**/\r
115VOID\r
116FlushHobVariableToFlash (\r
117 IN CHAR16 *VariableName,\r
118 IN EFI_GUID *VendorGuid\r
119 );\r
120\r
c24b392c 121/**\r
122 Writes a buffer to variable storage space, in the working block.\r
123\r
8a2d4996 124 This function writes a buffer to variable storage space into a firmware\r
125 volume block device. The destination is specified by the parameter\r
c24b392c 126 VariableBase. Fault Tolerant Write protocol is used for writing.\r
127\r
8a2d4996 128 @param VariableBase Base address of the variable to write.\r
129 @param Buffer Point to the data buffer.\r
130 @param BufferSize The number of bytes of the data Buffer.\r
c24b392c 131\r
8a2d4996 132 @retval EFI_SUCCESS The function completed successfully.\r
133 @retval EFI_NOT_FOUND Fail to locate Fault Tolerant Write protocol.\r
134 @retval EFI_ABORTED The function could not complete successfully.\r
c24b392c 135\r
136**/\r
137EFI_STATUS\r
138FtwVariableSpace (\r
139 IN EFI_PHYSICAL_ADDRESS VariableBase,\r
140 IN UINT8 *Buffer,\r
141 IN UINTN BufferSize\r
142 );\r
143\r
144\r
8a2d4996 145/**\r
146 Update the variable region with Variable information. These are the same \r
147 arguments as the EFI Variable services.\r
148\r
149 @param[in] VariableName Name of variable.\r
150\r
151 @param[in] VendorGuid Guid of variable.\r
152\r
153 @param[in] Data Variable data.\r
154\r
155 @param[in] DataSize Size of data. 0 means delete.\r
156\r
157 @param[in] Attributes Attribues of the variable.\r
158\r
23b06935 159 @param[in, out] Variable The variable information that is used to keep track of variable usage.\r
8a2d4996 160\r
161 @retval EFI_SUCCESS The update operation is success.\r
162\r
163 @retval EFI_OUT_OF_RESOURCES Variable region is full, cannot write other data into this region.\r
164\r
165**/\r
166EFI_STATUS\r
167UpdateVariable (\r
168 IN CHAR16 *VariableName,\r
169 IN EFI_GUID *VendorGuid,\r
170 IN VOID *Data,\r
171 IN UINTN DataSize,\r
172 IN UINT32 Attributes OPTIONAL,\r
23b06935 173 IN OUT VARIABLE_POINTER_TRACK *Variable\r
8a2d4996 174 );\r
175\r
176\r
177/**\r
178 Return TRUE if ExitBootServices () has been called.\r
179 \r
180 @retval TRUE If ExitBootServices () has been called.\r
181**/\r
182BOOLEAN\r
183AtRuntime (\r
184 VOID\r
185 );\r
186\r
187/**\r
188 Initializes a basic mutual exclusion lock.\r
189\r
190 This function initializes a basic mutual exclusion lock to the released state \r
191 and returns the lock. Each lock provides mutual exclusion access at its task \r
192 priority level. Since there is no preemption or multiprocessor support in EFI,\r
193 acquiring the lock only consists of raising to the locks TPL.\r
194 If Lock is NULL, then ASSERT().\r
195 If Priority is not a valid TPL value, then ASSERT().\r
196\r
197 @param Lock A pointer to the lock data structure to initialize.\r
198 @param Priority EFI TPL is associated with the lock.\r
199\r
200 @return The lock.\r
201\r
202**/\r
203EFI_LOCK *\r
204InitializeLock (\r
205 IN OUT EFI_LOCK *Lock,\r
206 IN EFI_TPL Priority\r
207 );\r
208\r
209 \r
210/**\r
211 Acquires lock only at boot time. Simply returns at runtime.\r
212\r
213 This is a temperary function that will be removed when\r
214 EfiAcquireLock() in UefiLib can handle the call in UEFI\r
215 Runtimer driver in RT phase.\r
216 It calls EfiAcquireLock() at boot time, and simply returns\r
217 at runtime.\r
218\r
219 @param Lock A pointer to the lock to acquire.\r
220\r
221**/\r
222VOID\r
223AcquireLockOnlyAtBootTime (\r
224 IN EFI_LOCK *Lock\r
225 );\r
226\r
227\r
228/**\r
229 Releases lock only at boot time. Simply returns at runtime.\r
230\r
231 This is a temperary function which will be removed when\r
232 EfiReleaseLock() in UefiLib can handle the call in UEFI\r
233 Runtimer driver in RT phase.\r
234 It calls EfiReleaseLock() at boot time and simply returns\r
235 at runtime.\r
236\r
237 @param Lock A pointer to the lock to release.\r
238\r
239**/\r
240VOID\r
241ReleaseLockOnlyAtBootTime (\r
242 IN EFI_LOCK *Lock\r
243 ); \r
244\r
245/**\r
246 Retrive the FVB protocol interface by HANDLE.\r
247\r
248 @param[in] FvBlockHandle The handle of FVB protocol that provides services for\r
249 reading, writing, and erasing the target block.\r
250 @param[out] FvBlock The interface of FVB protocol\r
251\r
252 @retval EFI_SUCCESS The interface information for the specified protocol was returned.\r
253 @retval EFI_UNSUPPORTED The device does not support the FVB protocol.\r
254 @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.\r
255 \r
256**/\r
257EFI_STATUS\r
258GetFvbByHandle (\r
259 IN EFI_HANDLE FvBlockHandle,\r
260 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvBlock\r
261 );\r
262\r
263\r
264/**\r
265 Retrive the Swap Address Range protocol interface.\r
266\r
267 @param[out] SarProtocol The interface of SAR protocol\r
268\r
269 @retval EFI_SUCCESS The SAR protocol instance was found and returned in SarProtocol.\r
270 @retval EFI_NOT_FOUND The SAR protocol instance was not found.\r
271 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.\r
272\r
273**/\r
274EFI_STATUS\r
275GetSarProtocol (\r
276 OUT VOID **SarProtocol\r
277 );\r
278\r
279/**\r
280 Function returns an array of handles that support the FVB protocol\r
281 in a buffer allocated from pool. \r
282\r
283 @param[out] NumberHandles The number of handles returned in Buffer.\r
284 @param[out] Buffer A pointer to the buffer to return the requested\r
285 array of handles that support FVB protocol.\r
286\r
287 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of\r
288 handles in Buffer was returned in NumberHandles.\r
289 @retval EFI_NOT_FOUND No FVB handle was found.\r
290 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.\r
291 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.\r
292 \r
293**/\r
294EFI_STATUS\r
295GetFvbCountAndBuffer (\r
296 OUT UINTN *NumberHandles,\r
297 OUT EFI_HANDLE **Buffer\r
298 );\r
299\r
300/**\r
301 Initializes variable store area for non-volatile and volatile variable.\r
302\r
303 @retval EFI_SUCCESS Function successfully executed.\r
304 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.\r
305\r
306**/\r
307EFI_STATUS\r
308VariableCommonInitialize (\r
309 VOID\r
310 );\r
311\r
312/**\r
313 This function reclaims variable storage if free size is below the threshold.\r
314 \r
315**/\r
316VOID\r
317ReclaimForOS(\r
318 VOID\r
319 ); \r
320\r
321\r
322/**\r
323 Initializes variable write service after FVB was ready.\r
324\r
325 @retval EFI_SUCCESS Function successfully executed.\r
326 @retval Others Fail to initialize the variable service.\r
327\r
328**/\r
329EFI_STATUS\r
330VariableWriteServiceInitialize (\r
331 VOID\r
332 );\r
333 \r
334/**\r
335 Retrive the SMM Fault Tolerent Write protocol interface.\r
336\r
337 @param[out] FtwProtocol The interface of SMM Ftw protocol\r
338\r
339 @retval EFI_SUCCESS The SMM SAR protocol instance was found and returned in SarProtocol.\r
340 @retval EFI_NOT_FOUND The SMM SAR protocol instance was not found.\r
341 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.\r
342\r
343**/\r
344EFI_STATUS\r
345GetFtwProtocol (\r
346 OUT VOID **FtwProtocol\r
347 );\r
348\r
349/**\r
350 Get the proper fvb handle and/or fvb protocol by the given Flash address.\r
351\r
352 @param[in] Address The Flash address.\r
353 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.\r
354 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.\r
355\r
356**/\r
357EFI_STATUS\r
358GetFvbInfoByAddress (\r
359 IN EFI_PHYSICAL_ADDRESS Address,\r
360 OUT EFI_HANDLE *FvbHandle OPTIONAL,\r
361 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvbProtocol OPTIONAL\r
362 );\r
363\r
364/**\r
365\r
366 This code finds variable in storage blocks (Volatile or Non-Volatile).\r
367\r
368 @param VariableName Name of Variable to be found.\r
369 @param VendorGuid Variable vendor GUID.\r
370 @param Attributes Attribute value of the variable found.\r
371 @param DataSize Size of Data found. If size is less than the\r
372 data, this value contains the required size.\r
373 @param Data Data pointer.\r
374 \r
375 @return EFI_INVALID_PARAMETER Invalid parameter.\r
376 @return EFI_SUCCESS Find the specified variable.\r
377 @return EFI_NOT_FOUND Not found.\r
378 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.\r
379\r
380**/\r
381EFI_STATUS\r
382EFIAPI\r
383VariableServiceGetVariable (\r
384 IN CHAR16 *VariableName,\r
385 IN EFI_GUID *VendorGuid,\r
386 OUT UINT32 *Attributes OPTIONAL,\r
387 IN OUT UINTN *DataSize,\r
388 OUT VOID *Data\r
389 );\r
390\r
391/**\r
392\r
393 This code Finds the Next available variable.\r
394\r
395 @param VariableNameSize Size of the variable name.\r
396 @param VariableName Pointer to variable name.\r
397 @param VendorGuid Variable Vendor Guid.\r
398\r
399 @return EFI_INVALID_PARAMETER Invalid parameter.\r
400 @return EFI_SUCCESS Find the specified variable.\r
401 @return EFI_NOT_FOUND Not found.\r
402 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.\r
403\r
404**/\r
405EFI_STATUS\r
406EFIAPI\r
407VariableServiceGetNextVariableName (\r
408 IN OUT UINTN *VariableNameSize,\r
409 IN OUT CHAR16 *VariableName,\r
410 IN OUT EFI_GUID *VendorGuid\r
411 );\r
412\r
413/**\r
414\r
415 This code sets variable in storage blocks (Volatile or Non-Volatile).\r
416\r
417 @param VariableName Name of Variable to be found.\r
418 @param VendorGuid Variable vendor GUID.\r
419 @param Attributes Attribute value of the variable found\r
420 @param DataSize Size of Data found. If size is less than the\r
421 data, this value contains the required size.\r
422 @param Data Data pointer.\r
423\r
424 @return EFI_INVALID_PARAMETER Invalid parameter.\r
425 @return EFI_SUCCESS Set successfully.\r
426 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.\r
427 @return EFI_NOT_FOUND Not found.\r
428 @return EFI_WRITE_PROTECTED Variable is read-only.\r
429\r
430**/\r
431EFI_STATUS\r
432EFIAPI\r
433VariableServiceSetVariable (\r
434 IN CHAR16 *VariableName,\r
435 IN EFI_GUID *VendorGuid,\r
436 IN UINT32 Attributes,\r
437 IN UINTN DataSize,\r
438 IN VOID *Data\r
439 );\r
440\r
441/**\r
442\r
443 This code returns information about the EFI variables.\r
444\r
445 @param Attributes Attributes bitmask to specify the type of variables\r
446 on which to return information.\r
447 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available\r
448 for the EFI variables associated with the attributes specified.\r
449 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available\r
450 for EFI variables associated with the attributes specified.\r
451 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables\r
452 associated with the attributes specified.\r
453\r
454 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.\r
455 @return EFI_SUCCESS Query successfully.\r
456 @return EFI_UNSUPPORTED The attribute is not supported on this platform.\r
457\r
458**/\r
459EFI_STATUS\r
460EFIAPI\r
461VariableServiceQueryVariableInfo (\r
462 IN UINT32 Attributes,\r
463 OUT UINT64 *MaximumVariableStorageSize,\r
464 OUT UINT64 *RemainingVariableStorageSize,\r
465 OUT UINT64 *MaximumVariableSize\r
466 ); \r
ff843847
RN
467\r
468/**\r
469 Mark a variable that will become read-only after leaving the DXE phase of execution.\r
470\r
471 @param[in] This The VARIABLE_LOCK_PROTOCOL instance.\r
472 @param[in] VariableName A pointer to the variable name that will be made read-only subsequently.\r
473 @param[in] VendorGuid A pointer to the vendor GUID that will be made read-only subsequently.\r
474\r
475 @retval EFI_SUCCESS The variable specified by the VariableName and the VendorGuid was marked\r
476 as pending to be read-only.\r
477 @retval EFI_INVALID_PARAMETER VariableName or VendorGuid is NULL.\r
478 Or VariableName is an empty string.\r
479 @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has\r
480 already been signaled.\r
481 @retval EFI_OUT_OF_RESOURCES There is not enough resource to hold the lock request.\r
482**/\r
483EFI_STATUS\r
484EFIAPI\r
485VariableLockRequestToLock (\r
486 IN CONST EDKII_VARIABLE_LOCK_PROTOCOL *This,\r
487 IN CHAR16 *VariableName,\r
488 IN EFI_GUID *VendorGuid\r
489 );\r
490\r
8a2d4996 491extern VARIABLE_MODULE_GLOBAL *mVariableModuleGlobal;\r
492\r
8d3a5c82 493#endif\r