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