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