]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
eb21cd854b7a8b53b246116bfd2ed5b607161442
[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 - 2015, 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 <Protocol/VarCheck.h>
27 #include <Library/PcdLib.h>
28 #include <Library/HobLib.h>
29 #include <Library/UefiDriverEntryPoint.h>
30 #include <Library/DxeServicesTableLib.h>
31 #include <Library/UefiRuntimeLib.h>
32 #include <Library/DebugLib.h>
33 #include <Library/BaseMemoryLib.h>
34 #include <Library/UefiBootServicesTableLib.h>
35 #include <Library/UefiLib.h>
36 #include <Library/BaseLib.h>
37 #include <Library/SynchronizationLib.h>
38 #include <Library/MemoryAllocationLib.h>
39 #include <Guid/GlobalVariable.h>
40 #include <Guid/EventGroup.h>
41 #include <Guid/VariableFormat.h>
42 #include <Guid/SystemNvDataGuid.h>
43 #include <Guid/FaultTolerantWrite.h>
44 #include <Guid/HardwareErrorVariable.h>
45
46 #define VARIABLE_ATTRIBUTE_BS_RT (EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS)
47 #define VARIABLE_ATTRIBUTE_NV_BS_RT (VARIABLE_ATTRIBUTE_BS_RT | EFI_VARIABLE_NON_VOLATILE)
48 #define VARIABLE_ATTRIBUTE_NV_BS_RT_AT (VARIABLE_ATTRIBUTE_NV_BS_RT | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)
49
50 ///
51 /// The size of a 3 character ISO639 language code.
52 ///
53 #define ISO_639_2_ENTRY_SIZE 3
54
55 typedef enum {
56 VariableStoreTypeVolatile,
57 VariableStoreTypeHob,
58 VariableStoreTypeNv,
59 VariableStoreTypeMax
60 } VARIABLE_STORE_TYPE;
61
62 typedef struct {
63 VARIABLE_HEADER *CurrPtr;
64 //
65 // If both ADDED and IN_DELETED_TRANSITION variable are present,
66 // InDeletedTransitionPtr will point to the IN_DELETED_TRANSITION one.
67 // Otherwise, CurrPtr will point to the ADDED or IN_DELETED_TRANSITION one,
68 // and InDeletedTransitionPtr will be NULL at the same time.
69 //
70 VARIABLE_HEADER *InDeletedTransitionPtr;
71 VARIABLE_HEADER *EndPtr;
72 VARIABLE_HEADER *StartPtr;
73 BOOLEAN Volatile;
74 } VARIABLE_POINTER_TRACK;
75
76 typedef struct {
77 EFI_PHYSICAL_ADDRESS HobVariableBase;
78 EFI_PHYSICAL_ADDRESS VolatileVariableBase;
79 EFI_PHYSICAL_ADDRESS NonVolatileVariableBase;
80 EFI_LOCK VariableServicesLock;
81 UINT32 ReentrantState;
82 } VARIABLE_GLOBAL;
83
84 typedef struct {
85 VARIABLE_GLOBAL VariableGlobal;
86 UINTN VolatileLastVariableOffset;
87 UINTN NonVolatileLastVariableOffset;
88 UINTN CommonVariableTotalSize;
89 UINTN HwErrVariableTotalSize;
90 CHAR8 *PlatformLangCodes;
91 CHAR8 *LangCodes;
92 CHAR8 *PlatformLang;
93 CHAR8 Lang[ISO_639_2_ENTRY_SIZE + 1];
94 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvbInstance;
95 } VARIABLE_MODULE_GLOBAL;
96
97 typedef struct {
98 EFI_GUID *Guid;
99 CHAR16 *Name;
100 UINTN VariableSize;
101 } VARIABLE_ENTRY_CONSISTENCY;
102
103 typedef struct {
104 LIST_ENTRY Link;
105 EFI_GUID Guid;
106 //CHAR16 *Name;
107 } VARIABLE_ENTRY;
108
109 /**
110 Flush the HOB variable to flash.
111
112 @param[in] VariableName Name of variable has been updated or deleted.
113 @param[in] VendorGuid Guid of variable has been updated or deleted.
114
115 **/
116 VOID
117 FlushHobVariableToFlash (
118 IN CHAR16 *VariableName,
119 IN EFI_GUID *VendorGuid
120 );
121
122 /**
123 Writes a buffer to variable storage space, in the working block.
124
125 This function writes a buffer to variable storage space into a firmware
126 volume block device. The destination is specified by the parameter
127 VariableBase. Fault Tolerant Write protocol is used for writing.
128
129 @param VariableBase Base address of the variable to write.
130 @param VariableBuffer Point to the variable data buffer.
131
132 @retval EFI_SUCCESS The function completed successfully.
133 @retval EFI_NOT_FOUND Fail to locate Fault Tolerant Write protocol.
134 @retval EFI_ABORTED The function could not complete successfully.
135
136 **/
137 EFI_STATUS
138 FtwVariableSpace (
139 IN EFI_PHYSICAL_ADDRESS VariableBase,
140 IN VARIABLE_STORE_HEADER *VariableBuffer
141 );
142
143
144 /**
145 Update the variable region with Variable information. These are the same
146 arguments as the EFI Variable services.
147
148 @param[in] VariableName Name of variable.
149
150 @param[in] VendorGuid Guid of variable.
151
152 @param[in] Data Variable data.
153
154 @param[in] DataSize Size of data. 0 means delete.
155
156 @param[in] Attributes Attribues of the variable.
157
158 @param[in, out] Variable The variable information that is used to keep track of variable usage.
159
160 @retval EFI_SUCCESS The update operation is success.
161
162 @retval EFI_OUT_OF_RESOURCES Variable region is full, cannot write other data into this region.
163
164 **/
165 EFI_STATUS
166 UpdateVariable (
167 IN CHAR16 *VariableName,
168 IN EFI_GUID *VendorGuid,
169 IN VOID *Data,
170 IN UINTN DataSize,
171 IN UINT32 Attributes OPTIONAL,
172 IN OUT VARIABLE_POINTER_TRACK *Variable
173 );
174
175
176 /**
177 Return TRUE if ExitBootServices () has been called.
178
179 @retval TRUE If ExitBootServices () has been called.
180 **/
181 BOOLEAN
182 AtRuntime (
183 VOID
184 );
185
186 /**
187 Initializes a basic mutual exclusion lock.
188
189 This function initializes a basic mutual exclusion lock to the released state
190 and returns the lock. Each lock provides mutual exclusion access at its task
191 priority level. Since there is no preemption or multiprocessor support in EFI,
192 acquiring the lock only consists of raising to the locks TPL.
193 If Lock is NULL, then ASSERT().
194 If Priority is not a valid TPL value, then ASSERT().
195
196 @param Lock A pointer to the lock data structure to initialize.
197 @param Priority EFI TPL is associated with the lock.
198
199 @return The lock.
200
201 **/
202 EFI_LOCK *
203 InitializeLock (
204 IN OUT EFI_LOCK *Lock,
205 IN EFI_TPL Priority
206 );
207
208
209 /**
210 Acquires lock only at boot time. Simply returns at runtime.
211
212 This is a temperary function that will be removed when
213 EfiAcquireLock() in UefiLib can handle the call in UEFI
214 Runtimer driver in RT phase.
215 It calls EfiAcquireLock() at boot time, and simply returns
216 at runtime.
217
218 @param Lock A pointer to the lock to acquire.
219
220 **/
221 VOID
222 AcquireLockOnlyAtBootTime (
223 IN EFI_LOCK *Lock
224 );
225
226
227 /**
228 Releases lock only at boot time. Simply returns at runtime.
229
230 This is a temperary function which will be removed when
231 EfiReleaseLock() in UefiLib can handle the call in UEFI
232 Runtimer driver in RT phase.
233 It calls EfiReleaseLock() at boot time and simply returns
234 at runtime.
235
236 @param Lock A pointer to the lock to release.
237
238 **/
239 VOID
240 ReleaseLockOnlyAtBootTime (
241 IN EFI_LOCK *Lock
242 );
243
244 /**
245 Retrive the FVB protocol interface by HANDLE.
246
247 @param[in] FvBlockHandle The handle of FVB protocol that provides services for
248 reading, writing, and erasing the target block.
249 @param[out] FvBlock The interface of FVB protocol
250
251 @retval EFI_SUCCESS The interface information for the specified protocol was returned.
252 @retval EFI_UNSUPPORTED The device does not support the FVB protocol.
253 @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.
254
255 **/
256 EFI_STATUS
257 GetFvbByHandle (
258 IN EFI_HANDLE FvBlockHandle,
259 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvBlock
260 );
261
262
263 /**
264 Retrive the Swap Address Range protocol interface.
265
266 @param[out] SarProtocol The interface of SAR protocol
267
268 @retval EFI_SUCCESS The SAR protocol instance was found and returned in SarProtocol.
269 @retval EFI_NOT_FOUND The SAR protocol instance was not found.
270 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
271
272 **/
273 EFI_STATUS
274 GetSarProtocol (
275 OUT VOID **SarProtocol
276 );
277
278 /**
279 Function returns an array of handles that support the FVB protocol
280 in a buffer allocated from pool.
281
282 @param[out] NumberHandles The number of handles returned in Buffer.
283 @param[out] Buffer A pointer to the buffer to return the requested
284 array of handles that support FVB protocol.
285
286 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of
287 handles in Buffer was returned in NumberHandles.
288 @retval EFI_NOT_FOUND No FVB handle was found.
289 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.
290 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.
291
292 **/
293 EFI_STATUS
294 GetFvbCountAndBuffer (
295 OUT UINTN *NumberHandles,
296 OUT EFI_HANDLE **Buffer
297 );
298
299 /**
300 Initializes variable store area for non-volatile and volatile variable.
301
302 @retval EFI_SUCCESS Function successfully executed.
303 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
304
305 **/
306 EFI_STATUS
307 VariableCommonInitialize (
308 VOID
309 );
310
311 /**
312 This function reclaims variable storage if free size is below the threshold.
313
314 **/
315 VOID
316 ReclaimForOS(
317 VOID
318 );
319
320
321 /**
322 Initializes variable write service after FVB was ready.
323
324 @retval EFI_SUCCESS Function successfully executed.
325 @retval Others Fail to initialize the variable service.
326
327 **/
328 EFI_STATUS
329 VariableWriteServiceInitialize (
330 VOID
331 );
332
333 /**
334 Retrive the SMM Fault Tolerent Write protocol interface.
335
336 @param[out] FtwProtocol The interface of SMM Ftw protocol
337
338 @retval EFI_SUCCESS The SMM SAR protocol instance was found and returned in SarProtocol.
339 @retval EFI_NOT_FOUND The SMM SAR protocol instance was not found.
340 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
341
342 **/
343 EFI_STATUS
344 GetFtwProtocol (
345 OUT VOID **FtwProtocol
346 );
347
348 /**
349 Get the proper fvb handle and/or fvb protocol by the given Flash address.
350
351 @param[in] Address The Flash address.
352 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.
353 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.
354
355 **/
356 EFI_STATUS
357 GetFvbInfoByAddress (
358 IN EFI_PHYSICAL_ADDRESS Address,
359 OUT EFI_HANDLE *FvbHandle OPTIONAL,
360 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvbProtocol OPTIONAL
361 );
362
363 /**
364
365 This code finds variable in storage blocks (Volatile or Non-Volatile).
366
367 @param VariableName Name of Variable to be found.
368 @param VendorGuid Variable vendor GUID.
369 @param Attributes Attribute value of the variable found.
370 @param DataSize Size of Data found. If size is less than the
371 data, this value contains the required size.
372 @param Data Data pointer.
373
374 @return EFI_INVALID_PARAMETER Invalid parameter.
375 @return EFI_SUCCESS Find the specified variable.
376 @return EFI_NOT_FOUND Not found.
377 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
378
379 **/
380 EFI_STATUS
381 EFIAPI
382 VariableServiceGetVariable (
383 IN CHAR16 *VariableName,
384 IN EFI_GUID *VendorGuid,
385 OUT UINT32 *Attributes OPTIONAL,
386 IN OUT UINTN *DataSize,
387 OUT VOID *Data
388 );
389
390 /**
391
392 This code Finds the Next available variable.
393
394 Caution: This function may receive untrusted input.
395 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
396
397 @param VariableNameSize Size of the variable name.
398 @param VariableName Pointer to variable name.
399 @param VendorGuid Variable Vendor Guid.
400
401 @return EFI_INVALID_PARAMETER Invalid parameter.
402 @return EFI_SUCCESS Find the specified variable.
403 @return EFI_NOT_FOUND Not found.
404 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
405
406 **/
407 EFI_STATUS
408 EFIAPI
409 VariableServiceGetNextVariableName (
410 IN OUT UINTN *VariableNameSize,
411 IN OUT CHAR16 *VariableName,
412 IN OUT EFI_GUID *VendorGuid
413 );
414
415 /**
416
417 This code sets variable in storage blocks (Volatile or Non-Volatile).
418
419 Caution: This function may receive untrusted input.
420 This function may be invoked in SMM mode, and datasize and data are external input.
421 This function will do basic validation, before parse the data.
422
423 @param VariableName Name of Variable to be found.
424 @param VendorGuid Variable vendor GUID.
425 @param Attributes Attribute value of the variable found
426 @param DataSize Size of Data found. If size is less than the
427 data, this value contains the required size.
428 @param Data Data pointer.
429
430 @return EFI_INVALID_PARAMETER Invalid parameter.
431 @return EFI_SUCCESS Set successfully.
432 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.
433 @return EFI_NOT_FOUND Not found.
434 @return EFI_WRITE_PROTECTED Variable is read-only.
435
436 **/
437 EFI_STATUS
438 EFIAPI
439 VariableServiceSetVariable (
440 IN CHAR16 *VariableName,
441 IN EFI_GUID *VendorGuid,
442 IN UINT32 Attributes,
443 IN UINTN DataSize,
444 IN VOID *Data
445 );
446
447 /**
448
449 This code returns information about the EFI variables.
450
451 Caution: This function may receive untrusted input.
452 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
453
454 @param Attributes Attributes bitmask to specify the type of variables
455 on which to return information.
456 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
457 for the EFI variables associated with the attributes specified.
458 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
459 for EFI variables associated with the attributes specified.
460 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
461 associated with the attributes specified.
462
463 @return EFI_SUCCESS Query successfully.
464
465 **/
466 EFI_STATUS
467 EFIAPI
468 VariableServiceQueryVariableInfoInternal (
469 IN UINT32 Attributes,
470 OUT UINT64 *MaximumVariableStorageSize,
471 OUT UINT64 *RemainingVariableStorageSize,
472 OUT UINT64 *MaximumVariableSize
473 );
474
475 /**
476
477 This code returns information about the EFI variables.
478
479 Caution: This function may receive untrusted input.
480 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
481
482 @param Attributes Attributes bitmask to specify the type of variables
483 on which to return information.
484 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
485 for the EFI variables associated with the attributes specified.
486 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
487 for EFI variables associated with the attributes specified.
488 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
489 associated with the attributes specified.
490
491 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
492 @return EFI_SUCCESS Query successfully.
493 @return EFI_UNSUPPORTED The attribute is not supported on this platform.
494
495 **/
496 EFI_STATUS
497 EFIAPI
498 VariableServiceQueryVariableInfo (
499 IN UINT32 Attributes,
500 OUT UINT64 *MaximumVariableStorageSize,
501 OUT UINT64 *RemainingVariableStorageSize,
502 OUT UINT64 *MaximumVariableSize
503 );
504
505 /**
506 Mark a variable that will become read-only after leaving the DXE phase of execution.
507
508 @param[in] This The VARIABLE_LOCK_PROTOCOL instance.
509 @param[in] VariableName A pointer to the variable name that will be made read-only subsequently.
510 @param[in] VendorGuid A pointer to the vendor GUID that will be made read-only subsequently.
511
512 @retval EFI_SUCCESS The variable specified by the VariableName and the VendorGuid was marked
513 as pending to be read-only.
514 @retval EFI_INVALID_PARAMETER VariableName or VendorGuid is NULL.
515 Or VariableName is an empty string.
516 @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has
517 already been signaled.
518 @retval EFI_OUT_OF_RESOURCES There is not enough resource to hold the lock request.
519 **/
520 EFI_STATUS
521 EFIAPI
522 VariableLockRequestToLock (
523 IN CONST EDKII_VARIABLE_LOCK_PROTOCOL *This,
524 IN CHAR16 *VariableName,
525 IN EFI_GUID *VendorGuid
526 );
527
528 /**
529 Check if a Unicode character is a hexadecimal character.
530
531 This function checks if a Unicode character is a
532 hexadecimal character. The valid hexadecimal character is
533 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
534
535
536 @param Char The character to check against.
537
538 @retval TRUE If the Char is a hexadecmial character.
539 @retval FALSE If the Char is not a hexadecmial character.
540
541 **/
542 BOOLEAN
543 EFIAPI
544 IsHexaDecimalDigitCharacter (
545 IN CHAR16 Char
546 );
547
548 /**
549 Internal SetVariable check.
550
551 @param[in] VariableName Name of Variable to set.
552 @param[in] VendorGuid Variable vendor GUID.
553 @param[in] Attributes Attribute value of the variable.
554 @param[in] DataSize Size of Data to set.
555 @param[in] Data Data pointer.
556
557 @retval EFI_SUCCESS The SetVariable check result was success.
558 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits, name, and GUID were supplied,
559 or the DataSize exceeds the minimum or maximum allowed,
560 or the Data value is not following UEFI spec for UEFI defined variables.
561 @retval EFI_WRITE_PROTECTED The variable in question is read-only.
562 @retval Others The return status from check handler.
563
564 **/
565 EFI_STATUS
566 EFIAPI
567 InternalVarCheckSetVariableCheck (
568 IN CHAR16 *VariableName,
569 IN EFI_GUID *VendorGuid,
570 IN UINT32 Attributes,
571 IN UINTN DataSize,
572 IN VOID *Data
573 );
574
575 /**
576 Register SetVariable check handler.
577
578 @param[in] Handler Pointer to check handler.
579
580 @retval EFI_SUCCESS The SetVariable check handler was registered successfully.
581 @retval EFI_INVALID_PARAMETER Handler is NULL.
582 @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has
583 already been signaled.
584 @retval EFI_OUT_OF_RESOURCES There is not enough resource for the SetVariable check handler register request.
585 @retval EFI_UNSUPPORTED This interface is not implemented.
586 For example, it is unsupported in VarCheck protocol if both VarCheck and SmmVarCheck protocols are present.
587
588 **/
589 EFI_STATUS
590 EFIAPI
591 VarCheckRegisterSetVariableCheckHandler (
592 IN VAR_CHECK_SET_VARIABLE_CHECK_HANDLER Handler
593 );
594
595 /**
596 Variable property set.
597
598 @param[in] Name Pointer to the variable name.
599 @param[in] Guid Pointer to the vendor GUID.
600 @param[in] VariableProperty Pointer to the input variable property.
601
602 @retval EFI_SUCCESS The property of variable specified by the Name and Guid was set successfully.
603 @retval EFI_INVALID_PARAMETER Name, Guid or VariableProperty is NULL, or Name is an empty string,
604 or the fields of VariableProperty are not valid.
605 @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has
606 already been signaled.
607 @retval EFI_OUT_OF_RESOURCES There is not enough resource for the variable property set request.
608
609 **/
610 EFI_STATUS
611 EFIAPI
612 VarCheckVariablePropertySet (
613 IN CHAR16 *Name,
614 IN EFI_GUID *Guid,
615 IN VAR_CHECK_VARIABLE_PROPERTY *VariableProperty
616 );
617
618 /**
619 Variable property get.
620
621 @param[in] Name Pointer to the variable name.
622 @param[in] Guid Pointer to the vendor GUID.
623 @param[out] VariableProperty Pointer to the output variable property.
624
625 @retval EFI_SUCCESS The property of variable specified by the Name and Guid was got successfully.
626 @retval EFI_INVALID_PARAMETER Name, Guid or VariableProperty is NULL, or Name is an empty string.
627 @retval EFI_NOT_FOUND The property of variable specified by the Name and Guid was not found.
628
629 **/
630 EFI_STATUS
631 EFIAPI
632 VarCheckVariablePropertyGet (
633 IN CHAR16 *Name,
634 IN EFI_GUID *Guid,
635 OUT VAR_CHECK_VARIABLE_PROPERTY *VariableProperty
636 );
637
638 extern VARIABLE_MODULE_GLOBAL *mVariableModuleGlobal;
639
640 #endif