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