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