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