]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
1) Replace MACRO with C functions.
[mirror_edk2.git] / MdeModulePkg / Universal / Variable / RuntimeDxe / Variable.c
1 /** @file
2 EFI Runtime Variable services.
3
4 Copyright (c) 2006 - 2007, Intel Corporation
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15
16 #include "Variable.h"
17
18
19 VARIABLE_MODULE_GLOBAL mRuntimeData;
20 VARIABLE_MODULE_GLOBAL *mVariableModuleGlobal = &mRuntimeData;
21 EFI_EVENT mVirtualAddressChangeEvent = NULL;
22 EFI_HANDLE mHandle = NULL;
23
24
25 //
26 // This is a temperary function which will be removed
27 // when EfiAcquireLock in UefiLib can handle the
28 // the call in UEFI Runtimer driver in RT phase.
29 //
30 VOID
31 AcquireLockOnlyAtBootTime (
32 IN EFI_LOCK *Lock
33 )
34 {
35 if (!EfiAtRuntime ()) {
36 EfiAcquireLock (Lock);
37 }
38 }
39
40 //
41 // This is a temperary function which will be removed
42 // when EfiAcquireLock in UefiLib can handle the
43 // the call in UEFI Runtimer driver in RT phase.
44 //
45 VOID
46 ReleaseLockOnlyAtBootTime (
47 IN EFI_LOCK *Lock
48 )
49 {
50 if (!EfiAtRuntime ()) {
51 EfiReleaseLock (Lock);
52 }
53 }
54
55
56 GLOBAL_REMOVE_IF_UNREFERENCED VARIABLE_INFO_ENTRY *gVariableInfo = NULL;
57
58
59 /**
60 Routine used to track statistical information about variable usage.
61 The data is stored in the EFI system table so it can be accessed later.
62 VariableInfo.efi can dump out the table. Only Boot Services variable
63 accesses are tracked by this code. The PcdVariableCollectStatistics
64 build flag controls if this feature is enabled.
65
66 A read that hits in the cache will have Read and Cache true for
67 the transaction. Data is allocated by this routine, but never
68 freed.
69
70 @param[in] VariableName Name of the Variable to track
71 @param[in] VendorGuid Guid of the Variable to track
72 @param[in] Volatile TRUE if volatile FALSE if non-volatile
73 @param[in] Read TRUE if GetVariable() was called
74 @param[in] Write TRUE if SetVariable() was called
75 @param[in] Delete TRUE if deleted via SetVariable()
76 @param[in] Cache TRUE for a cache hit.
77
78 **/
79 VOID
80 UpdateVariableInfo (
81 IN CHAR16 *VariableName,
82 IN EFI_GUID *VendorGuid,
83 IN BOOLEAN Volatile,
84 IN BOOLEAN Read,
85 IN BOOLEAN Write,
86 IN BOOLEAN Delete,
87 IN BOOLEAN Cache
88 )
89 {
90 VARIABLE_INFO_ENTRY *Entry;
91
92 if (FeaturePcdGet (PcdVariableCollectStatistics)) {
93
94 if (EfiAtRuntime ()) {
95 // Don't collect statistics at runtime
96 return;
97 }
98
99 if (gVariableInfo == NULL) {
100 //
101 // on the first call allocate a entry and place a pointer to it in
102 // the EFI System Table
103 //
104 gVariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
105 ASSERT (gVariableInfo != NULL);
106
107 CopyGuid (&gVariableInfo->VendorGuid, VendorGuid);
108 gVariableInfo->Name = AllocatePool (StrLen (VariableName));
109 StrCpy (gVariableInfo->Name, VariableName);
110 gVariableInfo->Volatile = Volatile;
111
112 gBS->InstallConfigurationTable (&gEfiVariableInfoGuid, gVariableInfo);
113 }
114
115
116 for (Entry = gVariableInfo; Entry != NULL; Entry = Entry->Next) {
117 if (CompareGuid (VendorGuid, &Entry->VendorGuid)) {
118 if (StrCmp (VariableName, Entry->Name) == 0) {
119 if (Read) {
120 Entry->ReadCount++;
121 }
122 if (Write) {
123 Entry->WriteCount++;
124 }
125 if (Delete) {
126 Entry->DeleteCount++;
127 }
128 if (Cache) {
129 Entry->CacheCount++;
130 }
131
132 return;
133 }
134 }
135
136 if (Entry->Next == NULL) {
137 //
138 // If the entry is not in the table add it.
139 // Next iteration of the loop will fill in the data
140 //
141 Entry->Next = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
142 ASSERT (Entry->Next != NULL);
143
144 CopyGuid (&Entry->Next->VendorGuid, VendorGuid);
145 Entry->Next->Name = AllocatePool (StrLen (VariableName));
146 StrCpy (Entry->Next->Name, VariableName);
147 Entry->Next->Volatile = Volatile;
148 }
149
150 }
151 }
152 }
153
154
155 BOOLEAN
156 IsValidVariableHeader (
157 IN VARIABLE_HEADER *Variable
158 )
159 /*++
160
161 Routine Description:
162
163 This code checks if variable header is valid or not.
164
165 Arguments:
166 Variable Pointer to the Variable Header.
167
168 Returns:
169 TRUE Variable header is valid.
170 FALSE Variable header is not valid.
171
172 --*/
173 {
174 if (Variable == NULL || Variable->StartId != VARIABLE_DATA) {
175 return FALSE;
176 }
177
178 return TRUE;
179 }
180
181
182 EFI_STATUS
183 UpdateVariableStore (
184 IN VARIABLE_GLOBAL *Global,
185 IN BOOLEAN Volatile,
186 IN BOOLEAN SetByIndex,
187 IN UINTN Instance,
188 IN UINTN DataPtrIndex,
189 IN UINT32 DataSize,
190 IN UINT8 *Buffer
191 )
192 /*++
193
194 Routine Description:
195
196 This function writes data to the FWH at the correct LBA even if the LBAs
197 are fragmented.
198
199 Arguments:
200
201 Global - Pointer to VARAIBLE_GLOBAL structure
202 Volatile - If the Variable is Volatile or Non-Volatile
203 SetByIndex - TRUE: Target pointer is given as index
204 FALSE: Target pointer is absolute
205 Instance - Instance of FV Block services
206 DataPtrIndex - Pointer to the Data from the end of VARIABLE_STORE_HEADER
207 structure
208 DataSize - Size of data to be written.
209 Buffer - Pointer to the buffer from which data is written
210
211 Returns:
212
213 EFI_INVALID_PARAMETER - Parameters not valid
214 EFI_SUCCESS - Variable store successfully updated
215
216 --*/
217 {
218 EFI_FV_BLOCK_MAP_ENTRY *PtrBlockMapEntry;
219 UINTN BlockIndex2;
220 UINTN LinearOffset;
221 UINTN CurrWriteSize;
222 UINTN CurrWritePtr;
223 UINT8 *CurrBuffer;
224 EFI_LBA LbaNumber;
225 UINTN Size;
226 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
227 VARIABLE_STORE_HEADER *VolatileBase;
228 EFI_PHYSICAL_ADDRESS FvVolHdr;
229 EFI_PHYSICAL_ADDRESS DataPtr;
230 EFI_STATUS Status;
231
232 FwVolHeader = NULL;
233 DataPtr = DataPtrIndex;
234
235 //
236 // Check if the Data is Volatile
237 //
238 if (!Volatile) {
239 EfiFvbGetPhysicalAddress (Instance, &FvVolHdr);
240 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvVolHdr);
241 //
242 // Data Pointer should point to the actual Address where data is to be
243 // written
244 //
245 if (SetByIndex) {
246 DataPtr += mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;
247 }
248
249 if ((DataPtr + DataSize) >= ((EFI_PHYSICAL_ADDRESS) (UINTN) ((UINT8 *) FwVolHeader + FwVolHeader->FvLength))) {
250 return EFI_INVALID_PARAMETER;
251 }
252 } else {
253 //
254 // Data Pointer should point to the actual Address where data is to be
255 // written
256 //
257 VolatileBase = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);
258 if (SetByIndex) {
259 DataPtr += mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;
260 }
261
262 if ((DataPtr + DataSize) >= ((UINTN) ((UINT8 *) VolatileBase + VolatileBase->Size))) {
263 return EFI_INVALID_PARAMETER;
264 }
265
266 //
267 // If Volatile Variable just do a simple mem copy.
268 //
269 CopyMem ((UINT8 *)(UINTN)DataPtr, Buffer, DataSize);
270 return EFI_SUCCESS;
271 }
272
273 //
274 // If we are here we are dealing with Non-Volatile Variables
275 //
276 LinearOffset = (UINTN) FwVolHeader;
277 CurrWritePtr = (UINTN) DataPtr;
278 CurrWriteSize = DataSize;
279 CurrBuffer = Buffer;
280 LbaNumber = 0;
281
282 if (CurrWritePtr < LinearOffset) {
283 return EFI_INVALID_PARAMETER;
284 }
285
286 for (PtrBlockMapEntry = FwVolHeader->BlockMap; PtrBlockMapEntry->NumBlocks != 0; PtrBlockMapEntry++) {
287 for (BlockIndex2 = 0; BlockIndex2 < PtrBlockMapEntry->NumBlocks; BlockIndex2++) {
288 //
289 // Check to see if the Variable Writes are spanning through multiple
290 // blocks.
291 //
292 if ((CurrWritePtr >= LinearOffset) && (CurrWritePtr < LinearOffset + PtrBlockMapEntry->Length)) {
293 if ((CurrWritePtr + CurrWriteSize) <= (LinearOffset + PtrBlockMapEntry->Length)) {
294 Status = EfiFvbWriteBlock (
295 Instance,
296 LbaNumber,
297 (UINTN) (CurrWritePtr - LinearOffset),
298 &CurrWriteSize,
299 CurrBuffer
300 );
301 return Status;
302 } else {
303 Size = (UINT32) (LinearOffset + PtrBlockMapEntry->Length - CurrWritePtr);
304 Status = EfiFvbWriteBlock (
305 Instance,
306 LbaNumber,
307 (UINTN) (CurrWritePtr - LinearOffset),
308 &Size,
309 CurrBuffer
310 );
311 if (EFI_ERROR (Status)) {
312 return Status;
313 }
314
315 CurrWritePtr = LinearOffset + PtrBlockMapEntry->Length;
316 CurrBuffer = CurrBuffer + Size;
317 CurrWriteSize = CurrWriteSize - Size;
318 }
319 }
320
321 LinearOffset += PtrBlockMapEntry->Length;
322 LbaNumber++;
323 }
324 }
325
326 return EFI_SUCCESS;
327 }
328
329
330 VARIABLE_STORE_STATUS
331 GetVariableStoreStatus (
332 IN VARIABLE_STORE_HEADER *VarStoreHeader
333 )
334 /*++
335
336 Routine Description:
337
338 This code gets the current status of Variable Store.
339
340 Arguments:
341
342 VarStoreHeader Pointer to the Variable Store Header.
343
344 Returns:
345
346 EfiRaw Variable store status is raw
347 EfiValid Variable store status is valid
348 EfiInvalid Variable store status is invalid
349
350 --*/
351 {
352 if (VarStoreHeader->Signature == VARIABLE_STORE_SIGNATURE &&
353 VarStoreHeader->Format == VARIABLE_STORE_FORMATTED &&
354 VarStoreHeader->State == VARIABLE_STORE_HEALTHY
355 ) {
356
357 return EfiValid;
358 } else if (VarStoreHeader->Signature == 0xffffffff &&
359 VarStoreHeader->Size == 0xffffffff &&
360 VarStoreHeader->Format == 0xff &&
361 VarStoreHeader->State == 0xff
362 ) {
363
364 return EfiRaw;
365 } else {
366 return EfiInvalid;
367 }
368 }
369
370
371 UINTN
372 NameSizeOfVariable (
373 IN VARIABLE_HEADER *Variable
374 )
375 /*++
376
377 Routine Description:
378
379 This code gets the size of name of variable.
380
381 Arguments:
382
383 Variable Pointer to the Variable Header.
384
385 Returns:
386
387 UINTN Size of variable in bytes
388
389 --*/
390 {
391 if (Variable->State == (UINT8) (-1) ||
392 Variable->DataSize == (UINT32) -1 ||
393 Variable->NameSize == (UINT32) -1 ||
394 Variable->Attributes == (UINT32) -1) {
395 return 0;
396 }
397 return (UINTN) Variable->NameSize;
398 }
399
400 UINTN
401 DataSizeOfVariable (
402 IN VARIABLE_HEADER *Variable
403 )
404 /*++
405
406 Routine Description:
407
408 This code gets the size of name of variable.
409
410 Arguments:
411
412 Variable Pointer to the Variable Header.
413
414 Returns:
415
416 UINTN Size of variable in bytes
417
418 --*/
419 {
420 if (Variable->State == (UINT8) -1 ||
421 Variable->DataSize == (UINT32) -1 ||
422 Variable->NameSize == (UINT32) -1 ||
423 Variable->Attributes == (UINT32) -1) {
424 return 0;
425 }
426 return (UINTN) Variable->DataSize;
427 }
428
429 CHAR16 *
430 GetVariableNamePtr (
431 IN VARIABLE_HEADER *Variable
432 )
433 /*++
434
435 Routine Description:
436
437 This code gets the pointer to the variable name.
438
439 Arguments:
440
441 Variable Pointer to the Variable Header.
442
443 Returns:
444
445 CHAR16* Pointer to Variable Name
446
447 --*/
448 {
449
450 return (CHAR16 *) (Variable + 1);
451 }
452
453 UINT8 *
454 GetVariableDataPtr (
455 IN VARIABLE_HEADER *Variable
456 )
457 /*++
458
459 Routine Description:
460
461 This code gets the pointer to the variable data.
462
463 Arguments:
464
465 Variable Pointer to the Variable Header.
466
467 Returns:
468
469 UINT8* Pointer to Variable Data
470
471 --*/
472 {
473 UINTN Value;
474
475 //
476 // Be careful about pad size for alignment
477 //
478 Value = (UINTN) GetVariableNamePtr (Variable);
479 Value += NameSizeOfVariable (Variable);
480 Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));
481
482 return (UINT8 *) Value;
483 }
484
485
486 VARIABLE_HEADER *
487 GetNextVariablePtr (
488 IN VARIABLE_HEADER *Variable
489 )
490 /*++
491
492 Routine Description:
493
494 This code gets the pointer to the next variable header.
495
496 Arguments:
497
498 Variable Pointer to the Variable Header.
499
500 Returns:
501
502 VARIABLE_HEADER* Pointer to next variable header.
503
504 --*/
505 {
506 UINTN Value;
507
508 if (!IsValidVariableHeader (Variable)) {
509 return NULL;
510 }
511
512 Value = (UINTN) GetVariableDataPtr (Variable);
513 Value += DataSizeOfVariable (Variable);
514 Value += GET_PAD_SIZE (DataSizeOfVariable (Variable));
515
516 //
517 // Be careful about pad size for alignment
518 //
519 return (VARIABLE_HEADER *) HEADER_ALIGN (Value);
520 }
521
522 VARIABLE_HEADER *
523 GetStartPointer (
524 IN VARIABLE_STORE_HEADER *VarStoreHeader
525 )
526 /*++
527
528 Routine Description:
529
530 This code gets the pointer to the first variable memory pointer byte
531
532 Arguments:
533
534 VarStoreHeader Pointer to the Variable Store Header.
535
536 Returns:
537
538 VARIABLE_HEADER* Pointer to last unavailable Variable Header
539
540 --*/
541 {
542 //
543 // The end of variable store
544 //
545 return (VARIABLE_HEADER *) HEADER_ALIGN (VarStoreHeader + 1);
546 }
547
548 VARIABLE_HEADER *
549 GetEndPointer (
550 IN VARIABLE_STORE_HEADER *VarStoreHeader
551 )
552 /*++
553
554 Routine Description:
555
556 This code gets the pointer to the last variable memory pointer byte
557
558 Arguments:
559
560 VarStoreHeader Pointer to the Variable Store Header.
561
562 Returns:
563
564 VARIABLE_HEADER* Pointer to last unavailable Variable Header
565
566 --*/
567 {
568 //
569 // The end of variable store
570 //
571 return (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) VarStoreHeader + VarStoreHeader->Size);
572 }
573
574
575 EFI_STATUS
576 Reclaim (
577 IN EFI_PHYSICAL_ADDRESS VariableBase,
578 OUT UINTN *LastVariableOffset,
579 IN BOOLEAN IsVolatile
580 )
581 /*++
582
583 Routine Description:
584
585 Variable store garbage collection and reclaim operation
586
587 Arguments:
588
589 VariableBase Base address of variable store
590 LastVariableOffset Offset of last variable
591 IsVolatile The variable store is volatile or not,
592 if it is non-volatile, need FTW
593
594 Returns:
595
596 EFI STATUS
597
598 --*/
599 {
600 VARIABLE_HEADER *Variable;
601 VARIABLE_HEADER *NextVariable;
602 VARIABLE_STORE_HEADER *VariableStoreHeader;
603 UINT8 *ValidBuffer;
604 UINTN ValidBufferSize;
605 UINTN VariableSize;
606 UINT8 *CurrPtr;
607 EFI_STATUS Status;
608
609 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) VariableBase);
610
611 //
612 // Start Pointers for the variable.
613 //
614 Variable = GetStartPointer (VariableStoreHeader);
615 ValidBufferSize = sizeof (VARIABLE_STORE_HEADER);
616
617 while (IsValidVariableHeader (Variable)) {
618 NextVariable = GetNextVariablePtr (Variable);
619 if (Variable->State == VAR_ADDED) {
620 VariableSize = (UINTN) NextVariable - (UINTN) Variable;
621 ValidBufferSize += VariableSize;
622 }
623
624 Variable = NextVariable;
625 }
626
627 ValidBuffer = AllocatePool (ValidBufferSize);
628 if (ValidBuffer == NULL) {
629 return EFI_OUT_OF_RESOURCES;
630 }
631
632 SetMem (ValidBuffer, ValidBufferSize, 0xff);
633
634 CurrPtr = ValidBuffer;
635
636 //
637 // Copy variable store header
638 //
639 CopyMem (CurrPtr, VariableStoreHeader, sizeof (VARIABLE_STORE_HEADER));
640 CurrPtr = (UINT8 *) GetStartPointer (VariableStoreHeader);
641
642 //
643 // Start Pointers for the variable.
644 //
645 Variable = GetStartPointer (VariableStoreHeader);
646
647 while (IsValidVariableHeader (Variable)) {
648 NextVariable = GetNextVariablePtr (Variable);
649 if (Variable->State == VAR_ADDED) {
650 VariableSize = (UINTN) NextVariable - (UINTN) Variable;
651 CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);
652 CurrPtr += VariableSize;
653 }
654
655 Variable = NextVariable;
656 }
657
658 if (IsVolatile) {
659 //
660 // If volatile variable store, just copy valid buffer
661 //
662 SetMem ((UINT8 *) (UINTN) VariableBase, VariableStoreHeader->Size, 0xff);
663 CopyMem ((UINT8 *) (UINTN) VariableBase, ValidBuffer, ValidBufferSize);
664 *LastVariableOffset = ValidBufferSize;
665 Status = EFI_SUCCESS;
666 } else {
667 //
668 // If non-volatile variable store, perform FTW here.
669 //
670 Status = FtwVariableSpace (
671 VariableBase,
672 ValidBuffer,
673 ValidBufferSize
674 );
675 if (!EFI_ERROR (Status)) {
676 *LastVariableOffset = ValidBufferSize;
677 }
678 }
679
680 FreePool (ValidBuffer);
681
682 if (EFI_ERROR (Status)) {
683 *LastVariableOffset = 0;
684 }
685
686 return Status;
687 }
688
689
690 //
691 // The current Hii implementation accesses this variable a larg # of times on every boot.
692 // Other common variables are only accessed a single time. This is why this cache algorithm
693 // only targets a single variable. Probably to get an performance improvement out of
694 // a Cache you would need a cache that improves the search performance for a variable.
695 //
696 VARIABLE_CACHE_ENTRY mVariableCache[] = {
697 {
698 &gEfiGlobalVariableGuid,
699 L"Lang",
700 0x00000000,
701 0x00,
702 NULL
703 }
704 };
705
706
707 /**
708 Update the Cache with Variable information. These are the same
709 arguments as the EFI Variable services.
710
711 @param[in] VariableName Name of variable
712 @param[in] VendorGuid Guid of variable
713 @param[in] Attribute Attribue of the variable
714 @param[in] DataSize Size of data. 0 means delete
715 @param[in] Data Variable data
716
717 **/
718 VOID
719 UpdateVariableCache (
720 IN CHAR16 *VariableName,
721 IN EFI_GUID *VendorGuid,
722 IN UINT32 Attributes,
723 IN UINTN DataSize,
724 IN VOID *Data
725 )
726 {
727 VARIABLE_CACHE_ENTRY *Entry;
728 UINTN Index;
729
730 if (EfiAtRuntime ()) {
731 // Don't use the cache at runtime
732 return;
733 }
734
735 for (Index = 0, Entry = mVariableCache; Index < sizeof (mVariableCache)/sizeof (VARIABLE_CACHE_ENTRY); Index++, Entry++) {
736 if (CompareGuid (VendorGuid, Entry->Guid)) {
737 if (StrCmp (VariableName, Entry->Name) == 0) {
738 Entry->Attributes = Attributes;
739 if (DataSize == 0) {
740 // Delete Case
741 if (Entry->DataSize != 0) {
742 FreePool (Entry->Data);
743 }
744 Entry->DataSize = DataSize;
745 } else if (DataSize == Entry->DataSize) {
746 CopyMem (Entry->Data, Data, DataSize);
747 } else {
748 Entry->Data = AllocatePool (DataSize);
749 Entry->DataSize = DataSize;
750 CopyMem (Entry->Data, Data, DataSize);
751 }
752 }
753 }
754 }
755 }
756
757
758 /**
759 Search the cache to see if the variable is in the cache.
760
761 @param[in] VariableName Name of variable
762 @param[in] VendorGuid Guid of variable
763 @param[in] Attribute Attribue returned
764 @param[in] DataSize Size of data returned
765 @param[in] Data Variable data returned
766
767 @retval EFI_SUCCESS VariableGuid & VariableName data was returned.
768 @retval other Not found.
769
770 **/
771 EFI_STATUS
772 FindVariableInCache (
773 IN CHAR16 *VariableName,
774 IN EFI_GUID *VendorGuid,
775 OUT UINT32 *Attributes OPTIONAL,
776 IN OUT UINTN *DataSize,
777 OUT VOID *Data
778 )
779 {
780 VARIABLE_CACHE_ENTRY *Entry;
781 UINTN Index;
782
783 if (EfiAtRuntime ()) {
784 // Don't use the cache at runtime
785 return EFI_NOT_FOUND;
786 }
787
788 for (Index = 0, Entry = mVariableCache; Index < sizeof (mVariableCache)/sizeof (VARIABLE_CACHE_ENTRY); Index++, Entry++) {
789 if (CompareGuid (VendorGuid, Entry->Guid)) {
790 if (StrCmp (VariableName, Entry->Name) == 0) {
791 if (Entry->DataSize == 0) {
792 // Variable was deleted so return not found
793 return EFI_NOT_FOUND;
794 } else if (Entry->DataSize > *DataSize) {
795 // If the buffer is too small return correct size
796 *DataSize = Entry->DataSize;
797 return EFI_BUFFER_TOO_SMALL;
798 } else {
799 *DataSize = Entry->DataSize;
800 // Return the data
801 CopyMem (Data, Entry->Data, Entry->DataSize);
802 if (Attributes != NULL) {
803 *Attributes = Entry->Attributes;
804 }
805 return EFI_SUCCESS;
806 }
807 }
808 }
809 }
810
811 return EFI_NOT_FOUND;
812 }
813
814
815 EFI_STATUS
816 FindVariable (
817 IN CHAR16 *VariableName,
818 IN EFI_GUID *VendorGuid,
819 OUT VARIABLE_POINTER_TRACK *PtrTrack,
820 IN VARIABLE_GLOBAL *Global
821 )
822 /*++
823
824 Routine Description:
825
826 This code finds variable in storage blocks (Volatile or Non-Volatile)
827
828 Arguments:
829
830 VariableName Name of the variable to be found
831 VendorGuid Vendor GUID to be found.
832 PtrTrack Variable Track Pointer structure that contains
833 Variable Information.
834 Contains the pointer of Variable header.
835 Global VARIABLE_GLOBAL pointer
836
837 Returns:
838
839 EFI STATUS
840
841 --*/
842 {
843 VARIABLE_HEADER *Variable[2];
844 VARIABLE_STORE_HEADER *VariableStoreHeader[2];
845 UINTN Index;
846 VOID *Point;
847
848 //
849 // 0: Volatile, 1: Non-Volatile
850 // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName
851 // make use of this mapping to implement search algorithme.
852 //
853 VariableStoreHeader[0] = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);
854 VariableStoreHeader[1] = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);
855
856 //
857 // Start Pointers for the variable.
858 // Actual Data Pointer where data can be written.
859 //
860 Variable[0] = GetStartPointer (VariableStoreHeader[0]);
861 Variable[1] = GetStartPointer (VariableStoreHeader[1]);
862
863 if (VariableName[0] != 0 && VendorGuid == NULL) {
864 return EFI_INVALID_PARAMETER;
865 }
866 //
867 // Find the variable by walk through volatile and then non-volatile variable store
868 //
869 for (Index = 0; Index < 2; Index++) {
870 PtrTrack->StartPtr = GetStartPointer (VariableStoreHeader[Index]);
871 PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader[Index]);
872
873 while (IsValidVariableHeader (Variable[Index]) && (Variable[Index] <= GetEndPointer (VariableStoreHeader[Index]))) {
874 if (Variable[Index]->State == VAR_ADDED) {
875 if (!EfiAtRuntime () || (Variable[Index]->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {
876 if (VariableName[0] == 0) {
877 PtrTrack->CurrPtr = Variable[Index];
878 PtrTrack->Volatile = (BOOLEAN)(Index == 0);
879 return EFI_SUCCESS;
880 } else {
881 if (CompareGuid (VendorGuid, &Variable[Index]->VendorGuid)) {
882 Point = (VOID *) GetVariableNamePtr (Variable[Index]);
883
884 ASSERT (NameSizeOfVariable (Variable[Index]) != 0);
885 if (!CompareMem (VariableName, Point, NameSizeOfVariable (Variable[Index]))) {
886 PtrTrack->CurrPtr = Variable[Index];
887 PtrTrack->Volatile = (BOOLEAN)(Index == 0);
888 return EFI_SUCCESS;
889 }
890 }
891 }
892 }
893 }
894
895 Variable[Index] = GetNextVariablePtr (Variable[Index]);
896 }
897 }
898 PtrTrack->CurrPtr = NULL;
899 return EFI_NOT_FOUND;
900 }
901
902
903
904 /*++
905
906 Routine Description:
907
908 This code finds variable in storage blocks (Volatile or Non-Volatile)
909
910 Arguments:
911
912 VariableName Name of Variable to be found
913 VendorGuid Variable vendor GUID
914 Attributes OPTIONAL Attribute value of the variable found
915 DataSize Size of Data found. If size is less than the
916 data, this value contains the required size.
917 Data Data pointer
918 Global Pointer to VARIABLE_GLOBAL structure
919 Instance Instance of the Firmware Volume.
920
921 Returns:
922
923 EFI_INVALID_PARAMETER - Invalid parameter
924 EFI_SUCCESS - Find the specified variable
925 EFI_NOT_FOUND - Not found
926 EFI_BUFFER_TO_SMALL - DataSize is too small for the result
927
928
929 --*/
930 EFI_STATUS
931 EFIAPI
932 RuntimeServiceGetVariable (
933 IN CHAR16 *VariableName,
934 IN EFI_GUID *VendorGuid,
935 OUT UINT32 *Attributes OPTIONAL,
936 IN OUT UINTN *DataSize,
937 OUT VOID *Data
938 )
939 {
940 EFI_STATUS Status;
941 VARIABLE_POINTER_TRACK Variable;
942 UINTN VarDataSize;
943
944 if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {
945 return EFI_INVALID_PARAMETER;
946 }
947
948 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
949
950 //
951 // Find existing variable
952 //
953 Status = FindVariableInCache (VariableName, VendorGuid, Attributes, DataSize, Data);
954 if ((Status == EFI_BUFFER_TOO_SMALL) || (Status == EFI_SUCCESS)){
955 // Hit in the Cache
956 UpdateVariableInfo (VariableName, VendorGuid, FALSE, TRUE, FALSE, FALSE, TRUE);
957 goto Done;
958 }
959
960 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal);
961 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {
962 goto Done;
963 }
964
965 //
966 // Get data size
967 //
968 VarDataSize = DataSizeOfVariable (Variable.CurrPtr);
969 ASSERT (VarDataSize != 0);
970
971 if (*DataSize >= VarDataSize) {
972 if (Data == NULL) {
973 Status = EFI_INVALID_PARAMETER;
974 goto Done;
975 }
976
977 CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);
978 if (Attributes != NULL) {
979 *Attributes = Variable.CurrPtr->Attributes;
980 }
981
982 *DataSize = VarDataSize;
983 UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, TRUE, FALSE, FALSE, FALSE);
984 UpdateVariableCache (VariableName, VendorGuid, Variable.CurrPtr->Attributes, VarDataSize, Data);
985
986 Status = EFI_SUCCESS;
987 goto Done;
988 } else {
989 *DataSize = VarDataSize;
990 Status = EFI_BUFFER_TOO_SMALL;
991 goto Done;
992 }
993
994 Done:
995 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
996 return Status;
997 }
998
999
1000
1001 /*++
1002
1003 Routine Description:
1004
1005 This code Finds the Next available variable
1006
1007 Arguments:
1008
1009 VariableNameSize Size of the variable
1010 VariableName Pointer to variable name
1011 VendorGuid Variable Vendor Guid
1012 Global VARIABLE_GLOBAL structure pointer.
1013 Instance FV instance
1014
1015 Returns:
1016
1017 EFI STATUS
1018
1019 --*/
1020 EFI_STATUS
1021 EFIAPI
1022 RuntimeServiceGetNextVariableName (
1023 IN OUT UINTN *VariableNameSize,
1024 IN OUT CHAR16 *VariableName,
1025 IN OUT EFI_GUID *VendorGuid
1026 )
1027 {
1028 VARIABLE_POINTER_TRACK Variable;
1029 UINTN VarNameSize;
1030 EFI_STATUS Status;
1031
1032 if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {
1033 return EFI_INVALID_PARAMETER;
1034 }
1035
1036 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
1037
1038 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal);
1039 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {
1040 goto Done;
1041 }
1042
1043 if (VariableName[0] != 0) {
1044 //
1045 // If variable name is not NULL, get next variable
1046 //
1047 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
1048 }
1049
1050 while (TRUE) {
1051 //
1052 // If both volatile and non-volatile variable store are parsed,
1053 // return not found
1054 //
1055 if (Variable.CurrPtr >= Variable.EndPtr || Variable.CurrPtr == NULL) {
1056 Variable.Volatile = (BOOLEAN) (Variable.Volatile ^ ((BOOLEAN) 0x1));
1057 if (!Variable.Volatile) {
1058 Variable.StartPtr = GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);
1059 Variable.EndPtr = GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase));
1060 } else {
1061 Status = EFI_NOT_FOUND;
1062 goto Done;
1063 }
1064
1065 Variable.CurrPtr = Variable.StartPtr;
1066 if (!IsValidVariableHeader (Variable.CurrPtr)) {
1067 continue;
1068 }
1069 }
1070 //
1071 // Variable is found
1072 //
1073 if (IsValidVariableHeader (Variable.CurrPtr) && Variable.CurrPtr->State == VAR_ADDED) {
1074 if (!(EfiAtRuntime () && !(Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS))) {
1075 VarNameSize = NameSizeOfVariable (Variable.CurrPtr);
1076 ASSERT (VarNameSize != 0);
1077
1078 if (VarNameSize <= *VariableNameSize) {
1079 CopyMem (
1080 VariableName,
1081 GetVariableNamePtr (Variable.CurrPtr),
1082 VarNameSize
1083 );
1084 CopyMem (
1085 VendorGuid,
1086 &Variable.CurrPtr->VendorGuid,
1087 sizeof (EFI_GUID)
1088 );
1089 Status = EFI_SUCCESS;
1090 } else {
1091 Status = EFI_BUFFER_TOO_SMALL;
1092 }
1093
1094 *VariableNameSize = VarNameSize;
1095 goto Done;
1096 }
1097 }
1098
1099 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
1100 }
1101
1102 Done:
1103 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
1104 return Status;
1105 }
1106
1107
1108 /*++
1109
1110 Routine Description:
1111
1112 This code sets variable in storage blocks (Volatile or Non-Volatile)
1113
1114 Arguments:
1115
1116 VariableName Name of Variable to be found
1117 VendorGuid Variable vendor GUID
1118 Attributes Attribute value of the variable found
1119 DataSize Size of Data found. If size is less than the
1120 data, this value contains the required size.
1121 Data Data pointer
1122 Global Pointer to VARIABLE_GLOBAL structure
1123 VolatileOffset The offset of last volatile variable
1124 NonVolatileOffset The offset of last non-volatile variable
1125 Instance Instance of the Firmware Volume.
1126
1127 Returns:
1128
1129 EFI_INVALID_PARAMETER - Invalid parameter
1130 EFI_SUCCESS - Set successfully
1131 EFI_OUT_OF_RESOURCES - Resource not enough to set variable
1132 EFI_NOT_FOUND - Not found
1133 EFI_DEVICE_ERROR - Variable can not be saved due to hardware failure
1134 EFI_WRITE_PROTECTED - Variable is read-only
1135
1136 --*/
1137 EFI_STATUS
1138 EFIAPI
1139 RuntimeServiceSetVariable (
1140 IN CHAR16 *VariableName,
1141 IN EFI_GUID *VendorGuid,
1142 IN UINT32 Attributes,
1143 IN UINTN DataSize,
1144 IN VOID *Data
1145 )
1146 {
1147 VARIABLE_POINTER_TRACK Variable;
1148 EFI_STATUS Status;
1149 VARIABLE_HEADER *NextVariable;
1150 UINTN VarNameSize;
1151 UINTN VarNameOffset;
1152 UINTN VarDataOffset;
1153 UINTN VarSize;
1154 UINT8 State;
1155 BOOLEAN Reclaimed;
1156 UINTN *VolatileOffset;
1157 UINTN *NonVolatileOffset;
1158 UINT32 Instance;
1159 BOOLEAN Volatile;
1160 EFI_PHYSICAL_ADDRESS Point;
1161
1162 //
1163 // Check input parameters
1164 //
1165 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {
1166 return EFI_INVALID_PARAMETER;
1167 }
1168 //
1169 // Make sure if runtime bit is set, boot service bit is set also
1170 //
1171 if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {
1172 return EFI_INVALID_PARAMETER;
1173 }
1174
1175 //
1176 // The size of the VariableName, including the Unicode Null in bytes plus
1177 // the DataSize is limited to maximum size of MAX_HARDWARE_ERROR_VARIABLE_SIZE (32K)
1178 // bytes for HwErrRec, and MAX_VARIABLE_SIZE (1024) bytes for the others.
1179 //
1180 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
1181 if ((DataSize > MAX_HARDWARE_ERROR_VARIABLE_SIZE) ||
1182 (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > MAX_HARDWARE_ERROR_VARIABLE_SIZE)) {
1183 return EFI_INVALID_PARAMETER;
1184 }
1185 } else {
1186 //
1187 // The size of the VariableName, including the Unicode Null in bytes plus
1188 // the DataSize is limited to maximum size of MAX_VARIABLE_SIZE (1024) bytes.
1189 //
1190 if ((DataSize > MAX_VARIABLE_SIZE) ||
1191 (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > MAX_VARIABLE_SIZE)) {
1192 return EFI_INVALID_PARAMETER;
1193 }
1194 }
1195
1196 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
1197
1198 Reclaimed = FALSE;
1199 Instance = mVariableModuleGlobal->FvbInstance;
1200 VolatileOffset = &mVariableModuleGlobal->VolatileLastVariableOffset;
1201
1202 //
1203 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated;
1204 //
1205 if (1 < InterlockedIncrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState)) {
1206 Point = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;;
1207 //
1208 // Parse non-volatile variable data and get last variable offset
1209 //
1210 NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point);
1211 while (IsValidVariableHeader (NextVariable)) {
1212 NextVariable = GetNextVariablePtr (NextVariable);
1213 }
1214 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) Point;
1215 }
1216
1217 NonVolatileOffset = &mVariableModuleGlobal->NonVolatileLastVariableOffset;
1218
1219
1220 //
1221 // Check whether the input variable is already existed
1222 //
1223
1224 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal);
1225 if (Status == EFI_SUCCESS && Variable.CurrPtr != NULL) {
1226 //
1227 // Update/Delete existing variable
1228 //
1229 Volatile = Variable.Volatile;
1230
1231 if (EfiAtRuntime ()) {
1232 //
1233 // If EfiAtRuntime and the variable is Volatile and Runtime Access,
1234 // the volatile is ReadOnly, and SetVariable should be aborted and
1235 // return EFI_WRITE_PROTECTED.
1236 //
1237 if (Variable.Volatile) {
1238 Status = EFI_WRITE_PROTECTED;
1239 goto Done;
1240 }
1241 //
1242 // Only variable have NV attribute can be updated/deleted in Runtime
1243 //
1244 if (!(Variable.CurrPtr->Attributes & EFI_VARIABLE_NON_VOLATILE)) {
1245 Status = EFI_INVALID_PARAMETER;
1246 goto Done;
1247 }
1248 }
1249 //
1250 // Setting a data variable with no access, or zero DataSize attributes
1251 // specified causes it to be deleted.
1252 //
1253 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {
1254 State = Variable.CurrPtr->State;
1255 State &= VAR_DELETED;
1256
1257 Status = UpdateVariableStore (
1258 &mVariableModuleGlobal->VariableGlobal,
1259 Variable.Volatile,
1260 FALSE,
1261 Instance,
1262 (UINTN) &Variable.CurrPtr->State,
1263 sizeof (UINT8),
1264 &State
1265 );
1266 if (!EFI_ERROR (Status)) {
1267 UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, FALSE, TRUE, FALSE);
1268 UpdateVariableCache (VariableName, VendorGuid, Attributes, DataSize, Data);
1269 }
1270 goto Done;
1271 }
1272 //
1273 // If the variable is marked valid and the same data has been passed in
1274 // then return to the caller immediately.
1275 //
1276 if (DataSizeOfVariable (Variable.CurrPtr) == DataSize &&
1277 (CompareMem (Data, GetVariableDataPtr (Variable.CurrPtr), DataSize) == 0)) {
1278
1279 UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);
1280 Status = EFI_SUCCESS;
1281 goto Done;
1282 } else if ((Variable.CurrPtr->State == VAR_ADDED) ||
1283 (Variable.CurrPtr->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {
1284 //
1285 // Mark the old variable as in delete transition
1286 //
1287 State = Variable.CurrPtr->State;
1288 State &= VAR_IN_DELETED_TRANSITION;
1289
1290 Status = UpdateVariableStore (
1291 &mVariableModuleGlobal->VariableGlobal,
1292 Variable.Volatile,
1293 FALSE,
1294 Instance,
1295 (UINTN) &Variable.CurrPtr->State,
1296 sizeof (UINT8),
1297 &State
1298 );
1299 if (EFI_ERROR (Status)) {
1300 goto Done;
1301 }
1302 }
1303 } else if (Status == EFI_NOT_FOUND) {
1304 //
1305 // Create a new variable
1306 //
1307
1308 //
1309 // Make sure we are trying to create a new variable.
1310 // Setting a data variable with no access, or zero DataSize attributes means to delete it.
1311 //
1312 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {
1313 Status = EFI_NOT_FOUND;
1314 goto Done;
1315 }
1316
1317 //
1318 // Only variable have NV|RT attribute can be created in Runtime
1319 //
1320 if (EfiAtRuntime () &&
1321 (!(Attributes & EFI_VARIABLE_RUNTIME_ACCESS) || !(Attributes & EFI_VARIABLE_NON_VOLATILE))) {
1322 Status = EFI_INVALID_PARAMETER;
1323 goto Done;
1324 }
1325 } else {
1326 //
1327 // Status should be EFI_INVALID_PARAMETER here according to return status of FindVariable().
1328 //
1329 ASSERT (Status == EFI_INVALID_PARAMETER);
1330 goto Done;
1331 }
1332
1333 //
1334 // Function part - create a new variable and copy the data.
1335 // Both update a variable and create a variable will come here.
1336 //
1337 // Tricky part: Use scratch data area at the end of volatile variable store
1338 // as a temporary storage.
1339 //
1340 NextVariable = GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase));
1341
1342 SetMem (NextVariable, SCRATCH_SIZE, 0xff);
1343
1344 NextVariable->StartId = VARIABLE_DATA;
1345 NextVariable->Attributes = Attributes;
1346 //
1347 // NextVariable->State = VAR_ADDED;
1348 //
1349 NextVariable->Reserved = 0;
1350 VarNameOffset = sizeof (VARIABLE_HEADER);
1351 VarNameSize = StrSize (VariableName);
1352 CopyMem (
1353 (UINT8 *) ((UINTN) NextVariable + VarNameOffset),
1354 VariableName,
1355 VarNameSize
1356 );
1357 VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);
1358 CopyMem (
1359 (UINT8 *) ((UINTN) NextVariable + VarDataOffset),
1360 Data,
1361 DataSize
1362 );
1363 CopyMem (&NextVariable->VendorGuid, VendorGuid, sizeof (EFI_GUID));
1364 //
1365 // There will be pad bytes after Data, the NextVariable->NameSize and
1366 // NextVariable->DataSize should not include pad size so that variable
1367 // service can get actual size in GetVariable
1368 //
1369 NextVariable->NameSize = (UINT32)VarNameSize;
1370 NextVariable->DataSize = (UINT32)DataSize;
1371
1372 //
1373 // The actual size of the variable that stores in storage should
1374 // include pad size.
1375 //
1376 VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);
1377 if (Attributes & EFI_VARIABLE_NON_VOLATILE) {
1378 //
1379 // Create a nonvolatile variable
1380 //
1381 Volatile = FALSE;
1382
1383 if ((UINT32) (VarSize +*NonVolatileOffset) >
1384 ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase)))->Size
1385 ) {
1386 if (EfiAtRuntime ()) {
1387 Status = EFI_OUT_OF_RESOURCES;
1388 goto Done;
1389 }
1390 //
1391 // Perform garbage collection & reclaim operation
1392 //
1393 Status = Reclaim (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase, NonVolatileOffset, FALSE);
1394 if (EFI_ERROR (Status)) {
1395 goto Done;
1396 }
1397 //
1398 // If still no enough space, return out of resources
1399 //
1400 if ((UINT32) (VarSize +*NonVolatileOffset) >
1401 ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase)))->Size
1402 ) {
1403 Status = EFI_OUT_OF_RESOURCES;
1404 goto Done;
1405 }
1406
1407 Reclaimed = TRUE;
1408 }
1409 //
1410 // Three steps
1411 // 1. Write variable header
1412 // 2. Set variable state to header valid
1413 // 3. Write variable data
1414 // 4. Set variable state to valid
1415 //
1416 //
1417 // Step 1:
1418 //
1419 Status = UpdateVariableStore (
1420 &mVariableModuleGlobal->VariableGlobal,
1421 FALSE,
1422 TRUE,
1423 Instance,
1424 *NonVolatileOffset,
1425 sizeof (VARIABLE_HEADER),
1426 (UINT8 *) NextVariable
1427 );
1428
1429 if (EFI_ERROR (Status)) {
1430 goto Done;
1431 }
1432
1433 //
1434 // Step 2:
1435 //
1436 NextVariable->State = VAR_HEADER_VALID_ONLY;
1437 Status = UpdateVariableStore (
1438 &mVariableModuleGlobal->VariableGlobal,
1439 FALSE,
1440 TRUE,
1441 Instance,
1442 *NonVolatileOffset,
1443 sizeof (VARIABLE_HEADER),
1444 (UINT8 *) NextVariable
1445 );
1446
1447 if (EFI_ERROR (Status)) {
1448 goto Done;
1449 }
1450 //
1451 // Step 3:
1452 //
1453 Status = UpdateVariableStore (
1454 &mVariableModuleGlobal->VariableGlobal,
1455 FALSE,
1456 TRUE,
1457 Instance,
1458 *NonVolatileOffset + sizeof (VARIABLE_HEADER),
1459 (UINT32) VarSize - sizeof (VARIABLE_HEADER),
1460 (UINT8 *) NextVariable + sizeof (VARIABLE_HEADER)
1461 );
1462
1463 if (EFI_ERROR (Status)) {
1464 goto Done;
1465 }
1466 //
1467 // Step 4:
1468 //
1469 NextVariable->State = VAR_ADDED;
1470 Status = UpdateVariableStore (
1471 &mVariableModuleGlobal->VariableGlobal,
1472 FALSE,
1473 TRUE,
1474 Instance,
1475 *NonVolatileOffset,
1476 sizeof (VARIABLE_HEADER),
1477 (UINT8 *) NextVariable
1478 );
1479
1480 if (EFI_ERROR (Status)) {
1481 goto Done;
1482 }
1483
1484 *NonVolatileOffset = HEADER_ALIGN (*NonVolatileOffset + VarSize);
1485
1486 } else {
1487 //
1488 // Create a volatile variable
1489 //
1490 Volatile = TRUE;
1491
1492 if ((UINT32) (VarSize +*VolatileOffset) >
1493 ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size) {
1494 //
1495 // Perform garbage collection & reclaim operation
1496 //
1497 Status = Reclaim (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase, VolatileOffset, TRUE);
1498 if (EFI_ERROR (Status)) {
1499 goto Done;
1500 }
1501 //
1502 // If still no enough space, return out of resources
1503 //
1504 if ((UINT32) (VarSize +*VolatileOffset) >
1505 ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size
1506 ) {
1507 Status = EFI_OUT_OF_RESOURCES;
1508 goto Done;
1509 }
1510
1511 Reclaimed = TRUE;
1512 }
1513
1514 NextVariable->State = VAR_ADDED;
1515 Status = UpdateVariableStore (
1516 &mVariableModuleGlobal->VariableGlobal,
1517 TRUE,
1518 TRUE,
1519 Instance,
1520 *VolatileOffset,
1521 (UINT32) VarSize,
1522 (UINT8 *) NextVariable
1523 );
1524
1525 if (EFI_ERROR (Status)) {
1526 goto Done;
1527 }
1528
1529 *VolatileOffset = HEADER_ALIGN (*VolatileOffset + VarSize);
1530 }
1531 //
1532 // Mark the old variable as deleted
1533 //
1534 if (!Reclaimed && !EFI_ERROR (Status) && Variable.CurrPtr != NULL) {
1535 State = Variable.CurrPtr->State;
1536 State &= VAR_DELETED;
1537
1538 Status = UpdateVariableStore (
1539 &mVariableModuleGlobal->VariableGlobal,
1540 Variable.Volatile,
1541 FALSE,
1542 Instance,
1543 (UINTN) &Variable.CurrPtr->State,
1544 sizeof (UINT8),
1545 &State
1546 );
1547
1548 if (!EFI_ERROR (Status)) {
1549 UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);
1550 UpdateVariableCache (VariableName, VendorGuid, Attributes, DataSize, Data);
1551 }
1552 goto Done;
1553 }
1554
1555 Status = EFI_SUCCESS;
1556 UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);
1557 UpdateVariableCache (VariableName, VendorGuid, Attributes, DataSize, Data);
1558
1559 Done:
1560 InterlockedDecrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState);
1561 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
1562
1563 return Status;
1564 }
1565
1566
1567 /*++
1568
1569 Routine Description:
1570
1571 This code returns information about the EFI variables.
1572
1573 Arguments:
1574
1575 Attributes Attributes bitmask to specify the type of variables
1576 on which to return information.
1577 MaximumVariableStorageSize Pointer to the maximum size of the storage space available
1578 for the EFI variables associated with the attributes specified.
1579 RemainingVariableStorageSize Pointer to the remaining size of the storage space available
1580 for EFI variables associated with the attributes specified.
1581 MaximumVariableSize Pointer to the maximum size of an individual EFI variables
1582 associated with the attributes specified.
1583 Global Pointer to VARIABLE_GLOBAL structure.
1584 Instance Instance of the Firmware Volume.
1585
1586 Returns:
1587
1588 EFI STATUS
1589 EFI_INVALID_PARAMETER - An invalid combination of attribute bits was supplied.
1590 EFI_SUCCESS - Query successfully.
1591 EFI_UNSUPPORTED - The attribute is not supported on this platform.
1592
1593 --*/
1594 EFI_STATUS
1595 EFIAPI
1596 RuntimeServiceQueryVariableInfo (
1597 IN UINT32 Attributes,
1598 OUT UINT64 *MaximumVariableStorageSize,
1599 OUT UINT64 *RemainingVariableStorageSize,
1600 OUT UINT64 *MaximumVariableSize
1601 )
1602 {
1603 VARIABLE_HEADER *Variable;
1604 VARIABLE_HEADER *NextVariable;
1605 UINT64 VariableSize;
1606 VARIABLE_STORE_HEADER *VariableStoreHeader;
1607
1608 if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {
1609 return EFI_INVALID_PARAMETER;
1610 }
1611
1612 if((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == 0) {
1613 //
1614 // Make sure the Attributes combination is supported by the platform.
1615 //
1616 return EFI_UNSUPPORTED;
1617 } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {
1618 //
1619 // Make sure if runtime bit is set, boot service bit is set also.
1620 //
1621 return EFI_INVALID_PARAMETER;
1622 } else if (EfiAtRuntime () && !(Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {
1623 //
1624 // Make sure RT Attribute is set if we are in Runtime phase.
1625 //
1626 return EFI_INVALID_PARAMETER;
1627 }
1628
1629 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
1630
1631 if((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {
1632 //
1633 // Query is Volatile related.
1634 //
1635 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);
1636 } else {
1637 //
1638 // Query is Non-Volatile related.
1639 //
1640 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);
1641 }
1642
1643 //
1644 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
1645 // with the storage size (excluding the storage header size).
1646 //
1647 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);
1648 *RemainingVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);
1649
1650 //
1651 // Let *MaximumVariableSize be MAX_VARIABLE_SIZE with the exception of the variable header size.
1652 //
1653 *MaximumVariableSize = MAX_VARIABLE_SIZE - sizeof (VARIABLE_HEADER);
1654
1655 //
1656 // Harware error record variable needs larger size.
1657 //
1658 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
1659 *MaximumVariableSize = MAX_HARDWARE_ERROR_VARIABLE_SIZE - sizeof (VARIABLE_HEADER);
1660 }
1661
1662 //
1663 // Point to the starting address of the variables.
1664 //
1665 Variable = GetStartPointer (VariableStoreHeader);
1666
1667 //
1668 // Now walk through the related variable store.
1669 //
1670 while (IsValidVariableHeader (Variable) && (Variable < GetEndPointer (VariableStoreHeader))) {
1671 NextVariable = GetNextVariablePtr (Variable);
1672 VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;
1673
1674 if (EfiAtRuntime ()) {
1675 //
1676 // we don't take the state of the variables in mind
1677 // when calculating RemainingVariableStorageSize,
1678 // since the space occupied by variables not marked with
1679 // VAR_ADDED is not allowed to be reclaimed in Runtime.
1680 //
1681 *RemainingVariableStorageSize -= VariableSize;
1682 } else {
1683 //
1684 // Only care about Variables with State VAR_ADDED,because
1685 // the space not marked as VAR_ADDED is reclaimable now.
1686 //
1687 if (Variable->State == VAR_ADDED) {
1688 *RemainingVariableStorageSize -= VariableSize;
1689 }
1690 }
1691
1692 //
1693 // Go to the next one
1694 //
1695 Variable = NextVariable;
1696 }
1697
1698 if (*RemainingVariableStorageSize < sizeof (VARIABLE_HEADER)) {
1699 *MaximumVariableSize = 0;
1700 } else if ((*RemainingVariableStorageSize - sizeof (VARIABLE_HEADER)) < *MaximumVariableSize) {
1701 *MaximumVariableSize = *RemainingVariableStorageSize - sizeof (VARIABLE_HEADER);
1702 }
1703
1704 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
1705 return EFI_SUCCESS;
1706 }
1707
1708 EFI_STATUS
1709 VariableCommonInitialize (
1710 IN EFI_HANDLE ImageHandle,
1711 IN EFI_SYSTEM_TABLE *SystemTable
1712 )
1713 /*++
1714
1715 Routine Description:
1716 This function does common initialization for variable services
1717
1718 Arguments:
1719
1720 ImageHandle - The firmware allocated handle for the EFI image.
1721 SystemTable - A pointer to the EFI System Table.
1722
1723 Returns:
1724
1725 Status code.
1726
1727 EFI_NOT_FOUND - Variable store area not found.
1728 EFI_UNSUPPORTED - Currently only one non-volatile variable store is supported.
1729 EFI_SUCCESS - Variable services successfully initialized.
1730
1731 --*/
1732 {
1733 EFI_STATUS Status;
1734 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
1735 CHAR8 *CurrPtr;
1736 VARIABLE_STORE_HEADER *VolatileVariableStore;
1737 VARIABLE_STORE_HEADER *VariableStoreHeader;
1738 VARIABLE_HEADER *NextVariable;
1739 UINT32 Instance;
1740 EFI_PHYSICAL_ADDRESS FvVolHdr;
1741 UINT64 TempVariableStoreHeader;
1742 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;
1743 UINT64 BaseAddress;
1744 UINT64 Length;
1745 UINTN Index;
1746 UINT8 Data;
1747 UINT64 VariableStoreBase;
1748 UINT64 VariableStoreLength;
1749
1750
1751 EfiInitializeLock(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock, TPL_NOTIFY);
1752 mVariableModuleGlobal->VariableGlobal.ReentrantState = 0;
1753
1754 //
1755 // Allocate memory for volatile variable store
1756 //
1757 VolatileVariableStore = AllocateRuntimePool (VARIABLE_STORE_SIZE + SCRATCH_SIZE);
1758 if (VolatileVariableStore == NULL) {
1759 FreePool (mVariableModuleGlobal);
1760 return EFI_OUT_OF_RESOURCES;
1761 }
1762
1763 SetMem (VolatileVariableStore, VARIABLE_STORE_SIZE + SCRATCH_SIZE, 0xff);
1764
1765 //
1766 // Variable Specific Data
1767 //
1768 mVariableModuleGlobal->VariableGlobal.VolatileVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VolatileVariableStore;
1769 mVariableModuleGlobal->VolatileLastVariableOffset = (UINTN) GetStartPointer (VolatileVariableStore) - (UINTN) VolatileVariableStore;
1770
1771 VolatileVariableStore->Signature = VARIABLE_STORE_SIGNATURE;
1772 VolatileVariableStore->Size = VARIABLE_STORE_SIZE;
1773 VolatileVariableStore->Format = VARIABLE_STORE_FORMATTED;
1774 VolatileVariableStore->State = VARIABLE_STORE_HEALTHY;
1775 VolatileVariableStore->Reserved = 0;
1776 VolatileVariableStore->Reserved1 = 0;
1777
1778 //
1779 // Get non volatile varaible store
1780 //
1781
1782 TempVariableStoreHeader = (UINT64) PcdGet32 (PcdFlashNvStorageVariableBase);
1783 VariableStoreBase = TempVariableStoreHeader + \
1784 (((EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) (TempVariableStoreHeader)) -> HeaderLength);
1785 VariableStoreLength = (UINT64) PcdGet32 (PcdFlashNvStorageVariableSize) - \
1786 (((EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) (TempVariableStoreHeader)) -> HeaderLength);
1787 //
1788 // Mark the variable storage region of the FLASH as RUNTIME
1789 //
1790 BaseAddress = VariableStoreBase & (~EFI_PAGE_MASK);
1791 Length = VariableStoreLength + (VariableStoreBase - BaseAddress);
1792 Length = (Length + EFI_PAGE_SIZE - 1) & (~EFI_PAGE_MASK);
1793
1794 Status = gDS->GetMemorySpaceDescriptor (BaseAddress, &GcdDescriptor);
1795 if (EFI_ERROR (Status)) {
1796 FreePool (mVariableModuleGlobal);
1797 FreePool (VolatileVariableStore);
1798 return EFI_UNSUPPORTED;
1799 }
1800
1801 Status = gDS->SetMemorySpaceAttributes (
1802 BaseAddress,
1803 Length,
1804 GcdDescriptor.Attributes | EFI_MEMORY_RUNTIME
1805 );
1806 if (EFI_ERROR (Status)) {
1807 FreePool (mVariableModuleGlobal);
1808 FreePool (VolatileVariableStore);
1809 return EFI_UNSUPPORTED;
1810 }
1811 //
1812 // Get address of non volatile variable store base
1813 //
1814 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase;
1815
1816 //
1817 // Check Integrity
1818 //
1819 //
1820 // Find the Correct Instance of the FV Block Service.
1821 //
1822 Instance = 0;
1823 CurrPtr = (CHAR8 *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);
1824 while (EfiFvbGetPhysicalAddress (Instance, &FvVolHdr) == EFI_SUCCESS) {
1825 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvVolHdr);
1826 if (CurrPtr >= (CHAR8 *) FwVolHeader && CurrPtr < (((CHAR8 *) FwVolHeader) + FwVolHeader->FvLength)) {
1827 mVariableModuleGlobal->FvbInstance = Instance;
1828 break;
1829 }
1830
1831 Instance++;
1832 }
1833
1834 VariableStoreHeader = (VARIABLE_STORE_HEADER *) CurrPtr;
1835 if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) {
1836 if (~VariableStoreHeader->Size == 0) {
1837 Status = UpdateVariableStore (
1838 &mVariableModuleGlobal->VariableGlobal,
1839 FALSE,
1840 FALSE,
1841 mVariableModuleGlobal->FvbInstance,
1842 (UINTN) &VariableStoreHeader->Size,
1843 sizeof (UINT32),
1844 (UINT8 *) &VariableStoreLength
1845 );
1846 //
1847 // As Variables are stored in NV storage, which are slow devices,such as flash.
1848 // Variable operation may skip checking variable program result to improve performance,
1849 // We can assume Variable program is OK through some check point.
1850 // Variable Store Size Setting should be the first Variable write operation,
1851 // We can assume all Read/Write is OK if we can set Variable store size successfully.
1852 // If write fail, we will assert here
1853 //
1854 ASSERT(VariableStoreHeader->Size == VariableStoreLength);
1855
1856 if (EFI_ERROR (Status)) {
1857 return Status;
1858 }
1859 }
1860
1861 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = (EFI_PHYSICAL_ADDRESS) ((UINTN) CurrPtr);
1862 //
1863 // Parse non-volatile variable data and get last variable offset
1864 //
1865 NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) CurrPtr);
1866 Status = EFI_SUCCESS;
1867
1868 while (IsValidVariableHeader (NextVariable)) {
1869 NextVariable = GetNextVariablePtr (NextVariable);
1870 }
1871
1872 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) CurrPtr;
1873
1874 //
1875 // Check if the free area is blow a threshold
1876 //
1877 if ((((VARIABLE_STORE_HEADER *)((UINTN) CurrPtr))->Size - mVariableModuleGlobal->NonVolatileLastVariableOffset) < VARIABLE_RECLAIM_THRESHOLD) {
1878 Status = Reclaim (
1879 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,
1880 &mVariableModuleGlobal->NonVolatileLastVariableOffset,
1881 FALSE
1882 );
1883 }
1884
1885 if (EFI_ERROR (Status)) {
1886 FreePool (mVariableModuleGlobal);
1887 FreePool (VolatileVariableStore);
1888 return Status;
1889 }
1890
1891 //
1892 // Check if the free area is really free.
1893 //
1894 for (Index = mVariableModuleGlobal->NonVolatileLastVariableOffset; Index < VariableStoreHeader->Size; Index++) {
1895 Data = ((UINT8 *) (UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase)[Index];
1896 if (Data != 0xff) {
1897 //
1898 // There must be something wrong in variable store, do reclaim operation.
1899 //
1900 Status = Reclaim (
1901 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,
1902 &mVariableModuleGlobal->NonVolatileLastVariableOffset,
1903 FALSE
1904 );
1905 break;
1906 }
1907 }
1908 }
1909
1910 if (EFI_ERROR (Status)) {
1911 FreePool (mVariableModuleGlobal);
1912 FreePool (VolatileVariableStore);
1913 }
1914
1915 return Status;
1916 }
1917
1918
1919
1920
1921 VOID
1922 EFIAPI
1923 VariableClassAddressChangeEvent (
1924 IN EFI_EVENT Event,
1925 IN VOID *Context
1926 )
1927 {
1928 EfiConvertPointer (
1929 0x0,
1930 (VOID **) &mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase
1931 );
1932 EfiConvertPointer (
1933 0x0,
1934 (VOID **) &mVariableModuleGlobal->VariableGlobal.VolatileVariableBase
1935 );
1936 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal);
1937 }
1938
1939
1940 /**
1941 Variable Driver main entry point. The Variable driver places the 4 EFI
1942 runtime services in the EFI System Table and installs arch protocols
1943 for variable read and write services being availible.
1944
1945 @param[in] ImageHandle The firmware allocated handle for the EFI image.
1946 @param[in] SystemTable A pointer to the EFI System Table.
1947
1948 @retval EFI_SUCCESS The entry point is executed successfully.
1949 @retval other Some error occurs when executing this entry point.
1950
1951 **/
1952 EFI_STATUS
1953 EFIAPI
1954 VariableServiceInitialize (
1955 IN EFI_HANDLE ImageHandle,
1956 IN EFI_SYSTEM_TABLE *SystemTable
1957 )
1958 {
1959 EFI_STATUS Status;
1960
1961 Status = VariableCommonInitialize (ImageHandle, SystemTable);
1962 ASSERT_EFI_ERROR (Status);
1963
1964 SystemTable->RuntimeServices->GetVariable = RuntimeServiceGetVariable;
1965 SystemTable->RuntimeServices->GetNextVariableName = RuntimeServiceGetNextVariableName;
1966 SystemTable->RuntimeServices->SetVariable = RuntimeServiceSetVariable;
1967 SystemTable->RuntimeServices->QueryVariableInfo = RuntimeServiceQueryVariableInfo;
1968
1969 //
1970 // Now install the Variable Runtime Architectural Protocol on a new handle
1971 //
1972 Status = gBS->InstallMultipleProtocolInterfaces (
1973 &mHandle,
1974 &gEfiVariableArchProtocolGuid, NULL,
1975 &gEfiVariableWriteArchProtocolGuid, NULL,
1976 NULL
1977 );
1978 ASSERT_EFI_ERROR (Status);
1979
1980 Status = gBS->CreateEvent (
1981 EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE,
1982 TPL_NOTIFY,
1983 VariableClassAddressChangeEvent,
1984 NULL,
1985 &mVirtualAddressChangeEvent
1986 );
1987 ASSERT_EFI_ERROR (Status);
1988
1989 return EFI_SUCCESS;
1990 }
1991