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