]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
If there are variables in the VAR_ADDED state that are identical to the variable...
[mirror_edk2.git] / MdeModulePkg / Universal / Variable / RuntimeDxe / Variable.c
1 /** @file
2
3 Implement all four UEFI Runtime Variable services for the nonvolatile
4 and volatile storage space and install variable architecture protocol.
5
6 Copyright (c) 2006 - 2008, Intel Corporation
7 All rights reserved. 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
18 #include "Variable.h"
19
20 VARIABLE_MODULE_GLOBAL *mVariableModuleGlobal;
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 IN VARIABLE_HEADER *UpdatingVariable
581 )
582 /*++
583
584 Routine Description:
585
586 Variable store garbage collection and reclaim operation
587
588 Arguments:
589
590 VariableBase Base address of variable store
591 LastVariableOffset Offset of last variable
592 IsVolatile The variable store is volatile or not,
593 if it is non-volatile, need FTW
594
595 Returns:
596
597 EFI STATUS
598
599 --*/
600 {
601 VARIABLE_HEADER *Variable;
602 VARIABLE_HEADER *AddedVariable;
603 VARIABLE_HEADER *NextVariable;
604 VARIABLE_HEADER *NextAddedVariable;
605 VARIABLE_STORE_HEADER *VariableStoreHeader;
606 UINT8 *ValidBuffer;
607 UINTN MaximumBufferSize;
608 UINTN VariableSize;
609 UINTN NameSize;
610 UINT8 *CurrPtr;
611 VOID *Point0;
612 VOID *Point1;
613 BOOLEAN FoundAdded;
614 EFI_STATUS Status;
615
616 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) VariableBase);
617
618 //
619 // Start Pointers for the variable.
620 //
621 Variable = GetStartPointer (VariableStoreHeader);
622 MaximumBufferSize = sizeof (VARIABLE_STORE_HEADER);
623
624 while (IsValidVariableHeader (Variable)) {
625 NextVariable = GetNextVariablePtr (Variable);
626 if (Variable->State == VAR_ADDED ||
627 Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)
628 ) {
629 VariableSize = (UINTN) NextVariable - (UINTN) Variable;
630 MaximumBufferSize += VariableSize;
631 }
632
633 Variable = NextVariable;
634 }
635
636 //
637 // Reserve the 1 Bytes with Oxff to identify the
638 // end of the variable buffer.
639 //
640 MaximumBufferSize += 1;
641 ValidBuffer = AllocatePool (MaximumBufferSize);
642 if (ValidBuffer == NULL) {
643 return EFI_OUT_OF_RESOURCES;
644 }
645
646 SetMem (ValidBuffer, MaximumBufferSize, 0xff);
647
648 //
649 // Copy variable store header
650 //
651 CopyMem (ValidBuffer, VariableStoreHeader, sizeof (VARIABLE_STORE_HEADER));
652 CurrPtr = (UINT8 *) GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer);
653
654 //
655 // Start Pointers for the variable.
656 //
657
658 //
659 // Reinstall all ADDED variables as long as they are not identical to Updating Variable
660 //
661 Variable = GetStartPointer (VariableStoreHeader);
662 while (IsValidVariableHeader (Variable)) {
663 NextVariable = GetNextVariablePtr (Variable);
664 if (Variable->State == VAR_ADDED) {
665 if (UpdatingVariable != NULL) {
666 if (UpdatingVariable == Variable) {
667 Variable = NextVariable;
668 continue;
669 }
670 if (CompareGuid (&Variable->VendorGuid, &UpdatingVariable->VendorGuid) &&
671 NameSizeOfVariable(Variable) == NameSizeOfVariable (UpdatingVariable) &&
672 CompareMem (GetVariableNamePtr (Variable), GetVariableNamePtr (UpdatingVariable), NameSizeOfVariable (Variable)) == 0 ) {
673 Variable = NextVariable;
674 continue;
675 }
676 }
677 VariableSize = (UINTN) NextVariable - (UINTN) Variable;
678 CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);
679 CurrPtr += VariableSize;
680 }
681 Variable = NextVariable;
682 }
683
684 //
685 // Reinstall the variable being updated if it is not NULL
686 //
687 if (UpdatingVariable != NULL) {
688 VariableSize = (UINTN)(GetNextVariablePtr (UpdatingVariable)) - (UINTN)UpdatingVariable;
689 CopyMem (CurrPtr, (UINT8 *) UpdatingVariable, VariableSize);
690 CurrPtr += VariableSize;
691 }
692
693 //
694 // Reinstall all in delete transition variables
695 //
696 Variable = GetStartPointer (VariableStoreHeader);
697 while (IsValidVariableHeader (Variable)) {
698 NextVariable = GetNextVariablePtr (Variable);
699 if (Variable != UpdatingVariable && Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
700
701 //
702 // Buffer has cached all ADDED variable.
703 // Per IN_DELETED variable, we have to guarantee that
704 // no ADDED one in previous buffer.
705 //
706
707 FoundAdded = FALSE;
708 AddedVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer);
709 while (IsValidVariableHeader (AddedVariable)) {
710 NextAddedVariable = GetNextVariablePtr (AddedVariable);
711 NameSize = NameSizeOfVariable (AddedVariable);
712 if (CompareGuid (&AddedVariable->VendorGuid, &Variable->VendorGuid) &&
713 NameSize == NameSizeOfVariable (Variable)
714 ) {
715 Point0 = (VOID *) GetVariableNamePtr (AddedVariable);
716 Point1 = (VOID *) GetVariableNamePtr (Variable);
717 if (CompareMem (Point0, Point1, NameSizeOfVariable (AddedVariable)) == 0) {
718 FoundAdded = TRUE;
719 break;
720 }
721 }
722 AddedVariable = NextAddedVariable;
723 }
724 if (!FoundAdded) {
725 //
726 // Promote VAR_IN_DELETED_TRANSITION to VAR_ADDED
727 //
728 VariableSize = (UINTN) NextVariable - (UINTN) Variable;
729 CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);
730 ((VARIABLE_HEADER *) CurrPtr)->State = VAR_ADDED;
731 CurrPtr += VariableSize;
732 }
733 }
734
735 Variable = NextVariable;
736 }
737
738 if (IsVolatile) {
739 //
740 // If volatile variable store, just copy valid buffer
741 //
742 SetMem ((UINT8 *) (UINTN) VariableBase, VariableStoreHeader->Size, 0xff);
743 CopyMem ((UINT8 *) (UINTN) VariableBase, ValidBuffer, (UINTN) (CurrPtr - (UINT8 *) ValidBuffer));
744 Status = EFI_SUCCESS;
745 } else {
746 //
747 // If non-volatile variable store, perform FTW here.
748 //
749 Status = FtwVariableSpace (
750 VariableBase,
751 ValidBuffer,
752 (UINTN) (CurrPtr - (UINT8 *) ValidBuffer)
753 );
754 }
755 if (!EFI_ERROR (Status)) {
756 *LastVariableOffset = (UINTN) (CurrPtr - (UINT8 *) ValidBuffer);
757 } else {
758 *LastVariableOffset = 0;
759 }
760
761 FreePool (ValidBuffer);
762
763 return Status;
764 }
765
766
767 //
768 // The current Hii implementation accesses this variable a larg # of times on every boot.
769 // Other common variables are only accessed a single time. This is why this cache algorithm
770 // only targets a single variable. Probably to get an performance improvement out of
771 // a Cache you would need a cache that improves the search performance for a variable.
772 //
773 VARIABLE_CACHE_ENTRY mVariableCache[] = {
774 {
775 &gEfiGlobalVariableGuid,
776 L"Lang",
777 0x00000000,
778 0x00,
779 NULL
780 }
781 };
782
783
784 /**
785 Update the Cache with Variable information. These are the same
786 arguments as the EFI Variable services.
787
788 @param[in] VariableName Name of variable
789 @param[in] VendorGuid Guid of variable
790 @param[in] Attribute Attribue of the variable
791 @param[in] DataSize Size of data. 0 means delete
792 @param[in] Data Variable data
793
794 **/
795 VOID
796 UpdateVariableCache (
797 IN CHAR16 *VariableName,
798 IN EFI_GUID *VendorGuid,
799 IN UINT32 Attributes,
800 IN UINTN DataSize,
801 IN VOID *Data
802 )
803 {
804 VARIABLE_CACHE_ENTRY *Entry;
805 UINTN Index;
806
807 if (EfiAtRuntime ()) {
808 // Don't use the cache at runtime
809 return;
810 }
811
812 for (Index = 0, Entry = mVariableCache; Index < sizeof (mVariableCache)/sizeof (VARIABLE_CACHE_ENTRY); Index++, Entry++) {
813 if (CompareGuid (VendorGuid, Entry->Guid)) {
814 if (StrCmp (VariableName, Entry->Name) == 0) {
815 Entry->Attributes = Attributes;
816 if (DataSize == 0) {
817 // Delete Case
818 if (Entry->DataSize != 0) {
819 FreePool (Entry->Data);
820 }
821 Entry->DataSize = DataSize;
822 } else if (DataSize == Entry->DataSize) {
823 CopyMem (Entry->Data, Data, DataSize);
824 } else {
825 Entry->Data = AllocatePool (DataSize);
826 Entry->DataSize = DataSize;
827 CopyMem (Entry->Data, Data, DataSize);
828 }
829 }
830 }
831 }
832 }
833
834
835 /**
836 Search the cache to see if the variable is in the cache.
837
838 @param[in] VariableName Name of variable
839 @param[in] VendorGuid Guid of variable
840 @param[in] Attribute Attribue returned
841 @param[in] DataSize Size of data returned
842 @param[in] Data Variable data returned
843
844 @retval EFI_SUCCESS VariableGuid & VariableName data was returned.
845 @retval other Not found.
846
847 **/
848 EFI_STATUS
849 FindVariableInCache (
850 IN CHAR16 *VariableName,
851 IN EFI_GUID *VendorGuid,
852 OUT UINT32 *Attributes OPTIONAL,
853 IN OUT UINTN *DataSize,
854 OUT VOID *Data
855 )
856 {
857 VARIABLE_CACHE_ENTRY *Entry;
858 UINTN Index;
859
860 if (EfiAtRuntime ()) {
861 // Don't use the cache at runtime
862 return EFI_NOT_FOUND;
863 }
864
865 for (Index = 0, Entry = mVariableCache; Index < sizeof (mVariableCache)/sizeof (VARIABLE_CACHE_ENTRY); Index++, Entry++) {
866 if (CompareGuid (VendorGuid, Entry->Guid)) {
867 if (StrCmp (VariableName, Entry->Name) == 0) {
868 if (Entry->DataSize == 0) {
869 // Variable was deleted so return not found
870 return EFI_NOT_FOUND;
871 } else if (Entry->DataSize > *DataSize) {
872 // If the buffer is too small return correct size
873 *DataSize = Entry->DataSize;
874 return EFI_BUFFER_TOO_SMALL;
875 } else {
876 *DataSize = Entry->DataSize;
877 // Return the data
878 CopyMem (Data, Entry->Data, Entry->DataSize);
879 if (Attributes != NULL) {
880 *Attributes = Entry->Attributes;
881 }
882 return EFI_SUCCESS;
883 }
884 }
885 }
886 }
887
888 return EFI_NOT_FOUND;
889 }
890
891
892 EFI_STATUS
893 FindVariable (
894 IN CHAR16 *VariableName,
895 IN EFI_GUID *VendorGuid,
896 OUT VARIABLE_POINTER_TRACK *PtrTrack,
897 IN VARIABLE_GLOBAL *Global
898 )
899 /*++
900
901 Routine Description:
902
903 This code finds variable in storage blocks (Volatile or Non-Volatile)
904
905 Arguments:
906
907 VariableName Name of the variable to be found
908 VendorGuid Vendor GUID to be found.
909 PtrTrack Variable Track Pointer structure that contains
910 Variable Information.
911 Contains the pointer of Variable header.
912 Global VARIABLE_GLOBAL pointer
913
914 Returns:
915
916 EFI STATUS
917
918 --*/
919 {
920 VARIABLE_HEADER *Variable[2];
921 VARIABLE_HEADER *InDeletedVariable;
922 VARIABLE_STORE_HEADER *VariableStoreHeader[2];
923 UINTN InDeletedStorageIndex;
924 UINTN Index;
925 VOID *Point;
926
927 //
928 // 0: Volatile, 1: Non-Volatile
929 // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName
930 // make use of this mapping to implement search algorithme.
931 //
932 VariableStoreHeader[0] = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);
933 VariableStoreHeader[1] = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);
934
935 //
936 // Start Pointers for the variable.
937 // Actual Data Pointer where data can be written.
938 //
939 Variable[0] = GetStartPointer (VariableStoreHeader[0]);
940 Variable[1] = GetStartPointer (VariableStoreHeader[1]);
941
942 if (VariableName[0] != 0 && VendorGuid == NULL) {
943 return EFI_INVALID_PARAMETER;
944 }
945
946 //
947 // Find the variable by walk through volatile and then non-volatile variable store
948 //
949 InDeletedVariable = NULL;
950 InDeletedStorageIndex = 0;
951 for (Index = 0; Index < 2; Index++) {
952 while (IsValidVariableHeader (Variable[Index]) && (Variable[Index] <= GetEndPointer (VariableStoreHeader[Index]))) {
953 if (Variable[Index]->State == VAR_ADDED ||
954 Variable[Index]->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)
955 ) {
956 if (!EfiAtRuntime () || (Variable[Index]->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {
957 if (VariableName[0] == 0) {
958 if (Variable[Index]->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
959 InDeletedVariable = Variable[Index];
960 InDeletedStorageIndex = Index;
961 } else {
962 PtrTrack->StartPtr = GetStartPointer (VariableStoreHeader[Index]);
963 PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader[Index]);
964 PtrTrack->CurrPtr = Variable[Index];
965 PtrTrack->Volatile = (BOOLEAN)(Index == 0);
966
967 return EFI_SUCCESS;
968 }
969 } else {
970 if (CompareGuid (VendorGuid, &Variable[Index]->VendorGuid)) {
971 Point = (VOID *) GetVariableNamePtr (Variable[Index]);
972
973 ASSERT (NameSizeOfVariable (Variable[Index]) != 0);
974 if (!CompareMem (VariableName, Point, NameSizeOfVariable (Variable[Index]))) {
975 if (Variable[Index]->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
976 InDeletedVariable = Variable[Index];
977 InDeletedStorageIndex = Index;
978 } else {
979 PtrTrack->StartPtr = GetStartPointer (VariableStoreHeader[Index]);
980 PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader[Index]);
981 PtrTrack->CurrPtr = Variable[Index];
982 PtrTrack->Volatile = (BOOLEAN)(Index == 0);
983
984 return EFI_SUCCESS;
985 }
986 }
987 }
988 }
989 }
990 }
991
992 Variable[Index] = GetNextVariablePtr (Variable[Index]);
993 }
994 if (InDeletedVariable != NULL) {
995 PtrTrack->StartPtr = GetStartPointer (VariableStoreHeader[InDeletedStorageIndex]);
996 PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader[InDeletedStorageIndex]);
997 PtrTrack->CurrPtr = InDeletedVariable;
998 PtrTrack->Volatile = (BOOLEAN)(InDeletedStorageIndex == 0);
999 return EFI_SUCCESS;
1000 }
1001 }
1002 PtrTrack->CurrPtr = NULL;
1003 return EFI_NOT_FOUND;
1004 }
1005
1006
1007
1008 /*++
1009
1010 Routine Description:
1011
1012 This code finds variable in storage blocks (Volatile or Non-Volatile)
1013
1014 Arguments:
1015
1016 VariableName Name of Variable to be found
1017 VendorGuid Variable vendor GUID
1018 Attributes OPTIONAL Attribute value of the variable found
1019 DataSize Size of Data found. If size is less than the
1020 data, this value contains the required size.
1021 Data Data pointer
1022 Global Pointer to VARIABLE_GLOBAL structure
1023 Instance Instance of the Firmware Volume.
1024
1025 Returns:
1026
1027 EFI_INVALID_PARAMETER - Invalid parameter
1028 EFI_SUCCESS - Find the specified variable
1029 EFI_NOT_FOUND - Not found
1030 EFI_BUFFER_TO_SMALL - DataSize is too small for the result
1031
1032
1033 --*/
1034 EFI_STATUS
1035 EFIAPI
1036 RuntimeServiceGetVariable (
1037 IN CHAR16 *VariableName,
1038 IN EFI_GUID *VendorGuid,
1039 OUT UINT32 *Attributes OPTIONAL,
1040 IN OUT UINTN *DataSize,
1041 OUT VOID *Data
1042 )
1043 {
1044 EFI_STATUS Status;
1045 VARIABLE_POINTER_TRACK Variable;
1046 UINTN VarDataSize;
1047
1048 if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {
1049 return EFI_INVALID_PARAMETER;
1050 }
1051
1052 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
1053
1054 //
1055 // Find existing variable
1056 //
1057 Status = FindVariableInCache (VariableName, VendorGuid, Attributes, DataSize, Data);
1058 if ((Status == EFI_BUFFER_TOO_SMALL) || (Status == EFI_SUCCESS)){
1059 // Hit in the Cache
1060 UpdateVariableInfo (VariableName, VendorGuid, FALSE, TRUE, FALSE, FALSE, TRUE);
1061 goto Done;
1062 }
1063
1064 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal);
1065 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {
1066 goto Done;
1067 }
1068
1069 //
1070 // Get data size
1071 //
1072 VarDataSize = DataSizeOfVariable (Variable.CurrPtr);
1073 ASSERT (VarDataSize != 0);
1074
1075 if (*DataSize >= VarDataSize) {
1076 if (Data == NULL) {
1077 Status = EFI_INVALID_PARAMETER;
1078 goto Done;
1079 }
1080
1081 CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);
1082 if (Attributes != NULL) {
1083 *Attributes = Variable.CurrPtr->Attributes;
1084 }
1085
1086 *DataSize = VarDataSize;
1087 UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, TRUE, FALSE, FALSE, FALSE);
1088 UpdateVariableCache (VariableName, VendorGuid, Variable.CurrPtr->Attributes, VarDataSize, Data);
1089
1090 Status = EFI_SUCCESS;
1091 goto Done;
1092 } else {
1093 *DataSize = VarDataSize;
1094 Status = EFI_BUFFER_TOO_SMALL;
1095 goto Done;
1096 }
1097
1098 Done:
1099 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
1100 return Status;
1101 }
1102
1103
1104
1105 /*++
1106
1107 Routine Description:
1108
1109 This code Finds the Next available variable
1110
1111 Arguments:
1112
1113 VariableNameSize Size of the variable
1114 VariableName Pointer to variable name
1115 VendorGuid Variable Vendor Guid
1116 Global VARIABLE_GLOBAL structure pointer.
1117 Instance FV instance
1118
1119 Returns:
1120
1121 EFI STATUS
1122
1123 --*/
1124 EFI_STATUS
1125 EFIAPI
1126 RuntimeServiceGetNextVariableName (
1127 IN OUT UINTN *VariableNameSize,
1128 IN OUT CHAR16 *VariableName,
1129 IN OUT EFI_GUID *VendorGuid
1130 )
1131 {
1132 VARIABLE_POINTER_TRACK Variable;
1133 UINTN VarNameSize;
1134 EFI_STATUS Status;
1135
1136 if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {
1137 return EFI_INVALID_PARAMETER;
1138 }
1139
1140 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
1141
1142 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal);
1143 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {
1144 goto Done;
1145 }
1146
1147 if (VariableName[0] != 0) {
1148 //
1149 // If variable name is not NULL, get next variable
1150 //
1151 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
1152 }
1153
1154 while (TRUE) {
1155 //
1156 // If both volatile and non-volatile variable store are parsed,
1157 // return not found
1158 //
1159 if (Variable.CurrPtr >= Variable.EndPtr || Variable.CurrPtr == NULL) {
1160 Variable.Volatile = (BOOLEAN) (Variable.Volatile ^ ((BOOLEAN) 0x1));
1161 if (!Variable.Volatile) {
1162 Variable.StartPtr = GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);
1163 Variable.EndPtr = GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase));
1164 } else {
1165 Status = EFI_NOT_FOUND;
1166 goto Done;
1167 }
1168
1169 Variable.CurrPtr = Variable.StartPtr;
1170 if (!IsValidVariableHeader (Variable.CurrPtr)) {
1171 continue;
1172 }
1173 }
1174 //
1175 // Variable is found
1176 //
1177 if (IsValidVariableHeader (Variable.CurrPtr) && Variable.CurrPtr->State == VAR_ADDED) {
1178 if (!(EfiAtRuntime () && !(Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS))) {
1179 VarNameSize = NameSizeOfVariable (Variable.CurrPtr);
1180 ASSERT (VarNameSize != 0);
1181
1182 if (VarNameSize <= *VariableNameSize) {
1183 CopyMem (
1184 VariableName,
1185 GetVariableNamePtr (Variable.CurrPtr),
1186 VarNameSize
1187 );
1188 CopyMem (
1189 VendorGuid,
1190 &Variable.CurrPtr->VendorGuid,
1191 sizeof (EFI_GUID)
1192 );
1193 Status = EFI_SUCCESS;
1194 } else {
1195 Status = EFI_BUFFER_TOO_SMALL;
1196 }
1197
1198 *VariableNameSize = VarNameSize;
1199 goto Done;
1200 }
1201 }
1202
1203 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
1204 }
1205
1206 Done:
1207 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
1208 return Status;
1209 }
1210
1211
1212 /*++
1213
1214 Routine Description:
1215
1216 This code sets variable in storage blocks (Volatile or Non-Volatile)
1217
1218 Arguments:
1219
1220 VariableName Name of Variable to be found
1221 VendorGuid Variable vendor GUID
1222 Attributes Attribute value of the variable found
1223 DataSize Size of Data found. If size is less than the
1224 data, this value contains the required size.
1225 Data Data pointer
1226 Global Pointer to VARIABLE_GLOBAL structure
1227 VolatileOffset The offset of last volatile variable
1228 NonVolatileOffset The offset of last non-volatile variable
1229 Instance Instance of the Firmware Volume.
1230
1231 Returns:
1232
1233 EFI_INVALID_PARAMETER - Invalid parameter
1234 EFI_SUCCESS - Set successfully
1235 EFI_OUT_OF_RESOURCES - Resource not enough to set variable
1236 EFI_NOT_FOUND - Not found
1237 EFI_DEVICE_ERROR - Variable can not be saved due to hardware failure
1238 EFI_WRITE_PROTECTED - Variable is read-only
1239
1240 --*/
1241 EFI_STATUS
1242 EFIAPI
1243 RuntimeServiceSetVariable (
1244 IN CHAR16 *VariableName,
1245 IN EFI_GUID *VendorGuid,
1246 IN UINT32 Attributes,
1247 IN UINTN DataSize,
1248 IN VOID *Data
1249 )
1250 {
1251 VARIABLE_POINTER_TRACK Variable;
1252 EFI_STATUS Status;
1253 VARIABLE_HEADER *NextVariable;
1254 UINTN VarNameSize;
1255 UINTN VarNameOffset;
1256 UINTN VarDataOffset;
1257 UINTN VarSize;
1258 UINT8 State;
1259 BOOLEAN Reclaimed;
1260 UINTN *VolatileOffset;
1261 UINTN *NonVolatileOffset;
1262 UINT32 Instance;
1263 BOOLEAN Volatile;
1264 EFI_PHYSICAL_ADDRESS Point;
1265
1266 //
1267 // Check input parameters
1268 //
1269 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {
1270 return EFI_INVALID_PARAMETER;
1271 }
1272 //
1273 // Make sure if runtime bit is set, boot service bit is set also
1274 //
1275 if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {
1276 return EFI_INVALID_PARAMETER;
1277 }
1278
1279 //
1280 // The size of the VariableName, including the Unicode Null in bytes plus
1281 // the DataSize is limited to maximum size of MAX_HARDWARE_ERROR_VARIABLE_SIZE (32K)
1282 // bytes for HwErrRec, and MAX_VARIABLE_SIZE (1024) bytes for the others.
1283 //
1284 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
1285 if ((DataSize > MAX_HARDWARE_ERROR_VARIABLE_SIZE) ||
1286 (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > MAX_HARDWARE_ERROR_VARIABLE_SIZE)) {
1287 return EFI_INVALID_PARAMETER;
1288 }
1289 } else {
1290 //
1291 // The size of the VariableName, including the Unicode Null in bytes plus
1292 // the DataSize is limited to maximum size of MAX_VARIABLE_SIZE (1024) bytes.
1293 //
1294 if ((DataSize > MAX_VARIABLE_SIZE) ||
1295 (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > MAX_VARIABLE_SIZE)) {
1296 return EFI_INVALID_PARAMETER;
1297 }
1298 }
1299
1300 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
1301
1302 Reclaimed = FALSE;
1303 Instance = mVariableModuleGlobal->FvbInstance;
1304 VolatileOffset = &mVariableModuleGlobal->VolatileLastVariableOffset;
1305
1306 //
1307 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated;
1308 //
1309 if (1 < InterlockedIncrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState)) {
1310 Point = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;;
1311 //
1312 // Parse non-volatile variable data and get last variable offset
1313 //
1314 NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point);
1315 while (IsValidVariableHeader (NextVariable)) {
1316 NextVariable = GetNextVariablePtr (NextVariable);
1317 }
1318 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) Point;
1319 }
1320
1321 NonVolatileOffset = &mVariableModuleGlobal->NonVolatileLastVariableOffset;
1322
1323
1324 //
1325 // Check whether the input variable is already existed
1326 //
1327
1328 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal);
1329 if (Status == EFI_SUCCESS && Variable.CurrPtr != NULL) {
1330 //
1331 // Update/Delete existing variable
1332 //
1333 Volatile = Variable.Volatile;
1334
1335 if (EfiAtRuntime ()) {
1336 //
1337 // If EfiAtRuntime and the variable is Volatile and Runtime Access,
1338 // the volatile is ReadOnly, and SetVariable should be aborted and
1339 // return EFI_WRITE_PROTECTED.
1340 //
1341 if (Variable.Volatile) {
1342 Status = EFI_WRITE_PROTECTED;
1343 goto Done;
1344 }
1345 //
1346 // Only variable have NV attribute can be updated/deleted in Runtime
1347 //
1348 if (!(Variable.CurrPtr->Attributes & EFI_VARIABLE_NON_VOLATILE)) {
1349 Status = EFI_INVALID_PARAMETER;
1350 goto Done;
1351 }
1352 }
1353 //
1354 // Setting a data variable with no access, or zero DataSize attributes
1355 // specified causes it to be deleted.
1356 //
1357 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {
1358 State = Variable.CurrPtr->State;
1359 State &= VAR_DELETED;
1360
1361 Status = UpdateVariableStore (
1362 &mVariableModuleGlobal->VariableGlobal,
1363 Variable.Volatile,
1364 FALSE,
1365 Instance,
1366 (UINTN) &Variable.CurrPtr->State,
1367 sizeof (UINT8),
1368 &State
1369 );
1370 if (!EFI_ERROR (Status)) {
1371 UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, FALSE, TRUE, FALSE);
1372 UpdateVariableCache (VariableName, VendorGuid, Attributes, DataSize, Data);
1373 }
1374 goto Done;
1375 }
1376 //
1377 // If the variable is marked valid and the same data has been passed in
1378 // then return to the caller immediately.
1379 //
1380 if (DataSizeOfVariable (Variable.CurrPtr) == DataSize &&
1381 (CompareMem (Data, GetVariableDataPtr (Variable.CurrPtr), DataSize) == 0)) {
1382
1383 UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);
1384 Status = EFI_SUCCESS;
1385 goto Done;
1386 } else if ((Variable.CurrPtr->State == VAR_ADDED) ||
1387 (Variable.CurrPtr->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {
1388
1389 //
1390 // Mark the old variable as in delete transition
1391 //
1392 State = Variable.CurrPtr->State;
1393 State &= VAR_IN_DELETED_TRANSITION;
1394
1395 Status = UpdateVariableStore (
1396 &mVariableModuleGlobal->VariableGlobal,
1397 Variable.Volatile,
1398 FALSE,
1399 Instance,
1400 (UINTN) &Variable.CurrPtr->State,
1401 sizeof (UINT8),
1402 &State
1403 );
1404 if (EFI_ERROR (Status)) {
1405 goto Done;
1406 }
1407 }
1408 } else if (Status == EFI_NOT_FOUND) {
1409 //
1410 // Create a new variable
1411 //
1412
1413 //
1414 // Make sure we are trying to create a new variable.
1415 // Setting a data variable with no access, or zero DataSize attributes means to delete it.
1416 //
1417 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {
1418 Status = EFI_NOT_FOUND;
1419 goto Done;
1420 }
1421
1422 //
1423 // Only variable have NV|RT attribute can be created in Runtime
1424 //
1425 if (EfiAtRuntime () &&
1426 (!(Attributes & EFI_VARIABLE_RUNTIME_ACCESS) || !(Attributes & EFI_VARIABLE_NON_VOLATILE))) {
1427 Status = EFI_INVALID_PARAMETER;
1428 goto Done;
1429 }
1430 } else {
1431 //
1432 // Status should be EFI_INVALID_PARAMETER here according to return status of FindVariable().
1433 //
1434 ASSERT (Status == EFI_INVALID_PARAMETER);
1435 goto Done;
1436 }
1437
1438 //
1439 // Function part - create a new variable and copy the data.
1440 // Both update a variable and create a variable will come here.
1441 //
1442 // Tricky part: Use scratch data area at the end of volatile variable store
1443 // as a temporary storage.
1444 //
1445 NextVariable = GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase));
1446
1447 SetMem (NextVariable, SCRATCH_SIZE, 0xff);
1448
1449 NextVariable->StartId = VARIABLE_DATA;
1450 NextVariable->Attributes = Attributes;
1451 //
1452 // NextVariable->State = VAR_ADDED;
1453 //
1454 NextVariable->Reserved = 0;
1455 VarNameOffset = sizeof (VARIABLE_HEADER);
1456 VarNameSize = StrSize (VariableName);
1457 CopyMem (
1458 (UINT8 *) ((UINTN) NextVariable + VarNameOffset),
1459 VariableName,
1460 VarNameSize
1461 );
1462 VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);
1463 CopyMem (
1464 (UINT8 *) ((UINTN) NextVariable + VarDataOffset),
1465 Data,
1466 DataSize
1467 );
1468 CopyMem (&NextVariable->VendorGuid, VendorGuid, sizeof (EFI_GUID));
1469 //
1470 // There will be pad bytes after Data, the NextVariable->NameSize and
1471 // NextVariable->DataSize should not include pad size so that variable
1472 // service can get actual size in GetVariable
1473 //
1474 NextVariable->NameSize = (UINT32)VarNameSize;
1475 NextVariable->DataSize = (UINT32)DataSize;
1476
1477 //
1478 // The actual size of the variable that stores in storage should
1479 // include pad size.
1480 //
1481 VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);
1482 if (Attributes & EFI_VARIABLE_NON_VOLATILE) {
1483 //
1484 // Create a nonvolatile variable
1485 //
1486 Volatile = FALSE;
1487
1488 if ((UINT32) (VarSize +*NonVolatileOffset) >
1489 ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase)))->Size
1490 ) {
1491 if (EfiAtRuntime ()) {
1492 Status = EFI_OUT_OF_RESOURCES;
1493 goto Done;
1494 }
1495 //
1496 // Perform garbage collection & reclaim operation
1497 //
1498 Status = Reclaim (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase, NonVolatileOffset, FALSE, Variable.CurrPtr);
1499 if (EFI_ERROR (Status)) {
1500 goto Done;
1501 }
1502 //
1503 // If still no enough space, return out of resources
1504 //
1505 if ((UINT32) (VarSize +*NonVolatileOffset) >
1506 ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase)))->Size
1507 ) {
1508 Status = EFI_OUT_OF_RESOURCES;
1509 goto Done;
1510 }
1511
1512 Reclaimed = TRUE;
1513 }
1514 //
1515 // Three steps
1516 // 1. Write variable header
1517 // 2. Set variable state to header valid
1518 // 3. Write variable data
1519 // 4. Set variable state to valid
1520 //
1521 //
1522 // Step 1:
1523 //
1524 Status = UpdateVariableStore (
1525 &mVariableModuleGlobal->VariableGlobal,
1526 FALSE,
1527 TRUE,
1528 Instance,
1529 *NonVolatileOffset,
1530 sizeof (VARIABLE_HEADER),
1531 (UINT8 *) NextVariable
1532 );
1533
1534 if (EFI_ERROR (Status)) {
1535 goto Done;
1536 }
1537
1538 //
1539 // Step 2:
1540 //
1541 NextVariable->State = VAR_HEADER_VALID_ONLY;
1542 Status = UpdateVariableStore (
1543 &mVariableModuleGlobal->VariableGlobal,
1544 FALSE,
1545 TRUE,
1546 Instance,
1547 *NonVolatileOffset,
1548 sizeof (VARIABLE_HEADER),
1549 (UINT8 *) NextVariable
1550 );
1551
1552 if (EFI_ERROR (Status)) {
1553 goto Done;
1554 }
1555 //
1556 // Step 3:
1557 //
1558 Status = UpdateVariableStore (
1559 &mVariableModuleGlobal->VariableGlobal,
1560 FALSE,
1561 TRUE,
1562 Instance,
1563 *NonVolatileOffset + sizeof (VARIABLE_HEADER),
1564 (UINT32) VarSize - sizeof (VARIABLE_HEADER),
1565 (UINT8 *) NextVariable + sizeof (VARIABLE_HEADER)
1566 );
1567
1568 if (EFI_ERROR (Status)) {
1569 goto Done;
1570 }
1571 //
1572 // Step 4:
1573 //
1574 NextVariable->State = VAR_ADDED;
1575 Status = UpdateVariableStore (
1576 &mVariableModuleGlobal->VariableGlobal,
1577 FALSE,
1578 TRUE,
1579 Instance,
1580 *NonVolatileOffset,
1581 sizeof (VARIABLE_HEADER),
1582 (UINT8 *) NextVariable
1583 );
1584
1585 if (EFI_ERROR (Status)) {
1586 goto Done;
1587 }
1588
1589 *NonVolatileOffset = HEADER_ALIGN (*NonVolatileOffset + VarSize);
1590
1591 } else {
1592 //
1593 // Create a volatile variable
1594 //
1595 Volatile = TRUE;
1596
1597 if ((UINT32) (VarSize +*VolatileOffset) >
1598 ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size) {
1599 //
1600 // Perform garbage collection & reclaim operation
1601 //
1602 Status = Reclaim (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase, VolatileOffset, TRUE, Variable.CurrPtr);
1603 if (EFI_ERROR (Status)) {
1604 goto Done;
1605 }
1606 //
1607 // If still no enough space, return out of resources
1608 //
1609 if ((UINT32) (VarSize +*VolatileOffset) >
1610 ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size
1611 ) {
1612 Status = EFI_OUT_OF_RESOURCES;
1613 goto Done;
1614 }
1615
1616 Reclaimed = TRUE;
1617 }
1618
1619 NextVariable->State = VAR_ADDED;
1620 Status = UpdateVariableStore (
1621 &mVariableModuleGlobal->VariableGlobal,
1622 TRUE,
1623 TRUE,
1624 Instance,
1625 *VolatileOffset,
1626 (UINT32) VarSize,
1627 (UINT8 *) NextVariable
1628 );
1629
1630 if (EFI_ERROR (Status)) {
1631 goto Done;
1632 }
1633
1634 *VolatileOffset = HEADER_ALIGN (*VolatileOffset + VarSize);
1635 }
1636 //
1637 // Mark the old variable as deleted
1638 //
1639 if (!Reclaimed && !EFI_ERROR (Status) && Variable.CurrPtr != NULL) {
1640 State = Variable.CurrPtr->State;
1641 State &= VAR_DELETED;
1642
1643 Status = UpdateVariableStore (
1644 &mVariableModuleGlobal->VariableGlobal,
1645 Variable.Volatile,
1646 FALSE,
1647 Instance,
1648 (UINTN) &Variable.CurrPtr->State,
1649 sizeof (UINT8),
1650 &State
1651 );
1652
1653 if (!EFI_ERROR (Status)) {
1654 UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);
1655 UpdateVariableCache (VariableName, VendorGuid, Attributes, DataSize, Data);
1656 }
1657 goto Done;
1658 }
1659
1660 Status = EFI_SUCCESS;
1661 UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);
1662 UpdateVariableCache (VariableName, VendorGuid, Attributes, DataSize, Data);
1663
1664 Done:
1665 InterlockedDecrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState);
1666 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
1667
1668 return Status;
1669 }
1670
1671
1672 /*++
1673
1674 Routine Description:
1675
1676 This code returns information about the EFI variables.
1677
1678 Arguments:
1679
1680 Attributes Attributes bitmask to specify the type of variables
1681 on which to return information.
1682 MaximumVariableStorageSize Pointer to the maximum size of the storage space available
1683 for the EFI variables associated with the attributes specified.
1684 RemainingVariableStorageSize Pointer to the remaining size of the storage space available
1685 for EFI variables associated with the attributes specified.
1686 MaximumVariableSize Pointer to the maximum size of an individual EFI variables
1687 associated with the attributes specified.
1688 Global Pointer to VARIABLE_GLOBAL structure.
1689 Instance Instance of the Firmware Volume.
1690
1691 Returns:
1692
1693 EFI STATUS
1694 EFI_INVALID_PARAMETER - An invalid combination of attribute bits was supplied.
1695 EFI_SUCCESS - Query successfully.
1696 EFI_UNSUPPORTED - The attribute is not supported on this platform.
1697
1698 --*/
1699 EFI_STATUS
1700 EFIAPI
1701 RuntimeServiceQueryVariableInfo (
1702 IN UINT32 Attributes,
1703 OUT UINT64 *MaximumVariableStorageSize,
1704 OUT UINT64 *RemainingVariableStorageSize,
1705 OUT UINT64 *MaximumVariableSize
1706 )
1707 {
1708 VARIABLE_HEADER *Variable;
1709 VARIABLE_HEADER *NextVariable;
1710 UINT64 VariableSize;
1711 VARIABLE_STORE_HEADER *VariableStoreHeader;
1712
1713 if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {
1714 return EFI_INVALID_PARAMETER;
1715 }
1716
1717 if((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == 0) {
1718 //
1719 // Make sure the Attributes combination is supported by the platform.
1720 //
1721 return EFI_UNSUPPORTED;
1722 } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {
1723 //
1724 // Make sure if runtime bit is set, boot service bit is set also.
1725 //
1726 return EFI_INVALID_PARAMETER;
1727 } else if (EfiAtRuntime () && !(Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {
1728 //
1729 // Make sure RT Attribute is set if we are in Runtime phase.
1730 //
1731 return EFI_INVALID_PARAMETER;
1732 }
1733
1734 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
1735
1736 if((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {
1737 //
1738 // Query is Volatile related.
1739 //
1740 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);
1741 } else {
1742 //
1743 // Query is Non-Volatile related.
1744 //
1745 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);
1746 }
1747
1748 //
1749 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
1750 // with the storage size (excluding the storage header size).
1751 //
1752 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);
1753 *RemainingVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);
1754
1755 //
1756 // Let *MaximumVariableSize be MAX_VARIABLE_SIZE with the exception of the variable header size.
1757 //
1758 *MaximumVariableSize = MAX_VARIABLE_SIZE - sizeof (VARIABLE_HEADER);
1759
1760 //
1761 // Harware error record variable needs larger size.
1762 //
1763 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
1764 *MaximumVariableSize = MAX_HARDWARE_ERROR_VARIABLE_SIZE - sizeof (VARIABLE_HEADER);
1765 }
1766
1767 //
1768 // Point to the starting address of the variables.
1769 //
1770 Variable = GetStartPointer (VariableStoreHeader);
1771
1772 //
1773 // Now walk through the related variable store.
1774 //
1775 while (IsValidVariableHeader (Variable) && (Variable < GetEndPointer (VariableStoreHeader))) {
1776 NextVariable = GetNextVariablePtr (Variable);
1777 VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;
1778
1779 if (EfiAtRuntime ()) {
1780 //
1781 // we don't take the state of the variables in mind
1782 // when calculating RemainingVariableStorageSize,
1783 // since the space occupied by variables not marked with
1784 // VAR_ADDED is not allowed to be reclaimed in Runtime.
1785 //
1786 *RemainingVariableStorageSize -= VariableSize;
1787 } else {
1788 //
1789 // Only care about Variables with State VAR_ADDED,because
1790 // the space not marked as VAR_ADDED is reclaimable now.
1791 //
1792 if (Variable->State == VAR_ADDED) {
1793 *RemainingVariableStorageSize -= VariableSize;
1794 }
1795 }
1796
1797 //
1798 // Go to the next one
1799 //
1800 Variable = NextVariable;
1801 }
1802
1803 if (*RemainingVariableStorageSize < sizeof (VARIABLE_HEADER)) {
1804 *MaximumVariableSize = 0;
1805 } else if ((*RemainingVariableStorageSize - sizeof (VARIABLE_HEADER)) < *MaximumVariableSize) {
1806 *MaximumVariableSize = *RemainingVariableStorageSize - sizeof (VARIABLE_HEADER);
1807 }
1808
1809 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
1810 return EFI_SUCCESS;
1811 }
1812
1813 VOID
1814 EFIAPI
1815 ReclaimForOS(
1816 EFI_EVENT Event,
1817 VOID *Context
1818 )
1819 {
1820 UINT32 VarSize;
1821 EFI_STATUS Status;
1822
1823 VarSize = ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase))->Size;
1824 Status = EFI_SUCCESS;
1825
1826 //
1827 // Check if the free area is blow a threshold
1828 //
1829 if ((VarSize - mVariableModuleGlobal->NonVolatileLastVariableOffset) < VARIABLE_RECLAIM_THRESHOLD) {
1830 Status = Reclaim (
1831 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,
1832 &mVariableModuleGlobal->NonVolatileLastVariableOffset,
1833 FALSE,
1834 NULL
1835 );
1836 ASSERT_EFI_ERROR (Status);
1837 }
1838 }
1839
1840 EFI_STATUS
1841 VariableCommonInitialize (
1842 IN EFI_HANDLE ImageHandle,
1843 IN EFI_SYSTEM_TABLE *SystemTable
1844 )
1845 /*++
1846
1847 Routine Description:
1848 This function does common initialization for variable services
1849
1850 Arguments:
1851
1852 ImageHandle - The firmware allocated handle for the EFI image.
1853 SystemTable - A pointer to the EFI System Table.
1854
1855 Returns:
1856
1857 Status code.
1858
1859 EFI_NOT_FOUND - Variable store area not found.
1860 EFI_UNSUPPORTED - Currently only one non-volatile variable store is supported.
1861 EFI_SUCCESS - Variable services successfully initialized.
1862
1863 --*/
1864 {
1865 EFI_STATUS Status;
1866 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
1867 CHAR8 *CurrPtr;
1868 VARIABLE_STORE_HEADER *VolatileVariableStore;
1869 VARIABLE_STORE_HEADER *VariableStoreHeader;
1870 VARIABLE_HEADER *NextVariable;
1871 UINT32 Instance;
1872 EFI_PHYSICAL_ADDRESS FvVolHdr;
1873 UINT64 TempVariableStoreHeader;
1874 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;
1875 UINT64 BaseAddress;
1876 UINT64 Length;
1877 UINTN Index;
1878 UINT8 Data;
1879 UINT64 VariableStoreBase;
1880 UINT64 VariableStoreLength;
1881 EFI_EVENT ReadyToBootEvent;
1882
1883 Status = EFI_SUCCESS;
1884 //
1885 // Allocate runtime memory for variable driver global structure.
1886 //
1887 mVariableModuleGlobal = AllocateRuntimePool (sizeof (VARIABLE_MODULE_GLOBAL));
1888 if (mVariableModuleGlobal == NULL) {
1889 return EFI_OUT_OF_RESOURCES;
1890 }
1891
1892 EfiInitializeLock(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock, TPL_NOTIFY);
1893 mVariableModuleGlobal->VariableGlobal.ReentrantState = 0;
1894
1895 //
1896 // Allocate memory for volatile variable store
1897 //
1898 VolatileVariableStore = AllocateRuntimePool (VARIABLE_STORE_SIZE + SCRATCH_SIZE);
1899 if (VolatileVariableStore == NULL) {
1900 FreePool (mVariableModuleGlobal);
1901 return EFI_OUT_OF_RESOURCES;
1902 }
1903
1904 SetMem (VolatileVariableStore, VARIABLE_STORE_SIZE + SCRATCH_SIZE, 0xff);
1905
1906 //
1907 // Variable Specific Data
1908 //
1909 mVariableModuleGlobal->VariableGlobal.VolatileVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VolatileVariableStore;
1910 mVariableModuleGlobal->VolatileLastVariableOffset = (UINTN) GetStartPointer (VolatileVariableStore) - (UINTN) VolatileVariableStore;
1911
1912 VolatileVariableStore->Signature = VARIABLE_STORE_SIGNATURE;
1913 VolatileVariableStore->Size = VARIABLE_STORE_SIZE;
1914 VolatileVariableStore->Format = VARIABLE_STORE_FORMATTED;
1915 VolatileVariableStore->State = VARIABLE_STORE_HEALTHY;
1916 VolatileVariableStore->Reserved = 0;
1917 VolatileVariableStore->Reserved1 = 0;
1918
1919 //
1920 // Get non volatile varaible store
1921 //
1922
1923 TempVariableStoreHeader = (UINT64) PcdGet32 (PcdFlashNvStorageVariableBase);
1924 VariableStoreBase = TempVariableStoreHeader + \
1925 (((EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) (TempVariableStoreHeader)) -> HeaderLength);
1926 VariableStoreLength = (UINT64) PcdGet32 (PcdFlashNvStorageVariableSize) - \
1927 (((EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) (TempVariableStoreHeader)) -> HeaderLength);
1928 //
1929 // Mark the variable storage region of the FLASH as RUNTIME
1930 //
1931 BaseAddress = VariableStoreBase & (~EFI_PAGE_MASK);
1932 Length = VariableStoreLength + (VariableStoreBase - BaseAddress);
1933 Length = (Length + EFI_PAGE_SIZE - 1) & (~EFI_PAGE_MASK);
1934
1935 Status = gDS->GetMemorySpaceDescriptor (BaseAddress, &GcdDescriptor);
1936 if (EFI_ERROR (Status)) {
1937 goto Done;
1938 }
1939
1940 Status = gDS->SetMemorySpaceAttributes (
1941 BaseAddress,
1942 Length,
1943 GcdDescriptor.Attributes | EFI_MEMORY_RUNTIME
1944 );
1945 if (EFI_ERROR (Status)) {
1946 goto Done;
1947 }
1948 //
1949 // Get address of non volatile variable store base
1950 //
1951 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase;
1952
1953 //
1954 // Check Integrity
1955 //
1956 //
1957 // Find the Correct Instance of the FV Block Service.
1958 //
1959 Instance = 0;
1960 CurrPtr = (CHAR8 *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);
1961 while (EfiFvbGetPhysicalAddress (Instance, &FvVolHdr) == EFI_SUCCESS) {
1962 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvVolHdr);
1963 if (CurrPtr >= (CHAR8 *) FwVolHeader && CurrPtr < (((CHAR8 *) FwVolHeader) + FwVolHeader->FvLength)) {
1964 mVariableModuleGlobal->FvbInstance = Instance;
1965 break;
1966 }
1967
1968 Instance++;
1969 }
1970
1971 VariableStoreHeader = (VARIABLE_STORE_HEADER *) CurrPtr;
1972 if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) {
1973 if (~VariableStoreHeader->Size == 0) {
1974 Status = UpdateVariableStore (
1975 &mVariableModuleGlobal->VariableGlobal,
1976 FALSE,
1977 FALSE,
1978 mVariableModuleGlobal->FvbInstance,
1979 (UINTN) &VariableStoreHeader->Size,
1980 sizeof (UINT32),
1981 (UINT8 *) &VariableStoreLength
1982 );
1983 //
1984 // As Variables are stored in NV storage, which are slow devices,such as flash.
1985 // Variable operation may skip checking variable program result to improve performance,
1986 // We can assume Variable program is OK through some check point.
1987 // Variable Store Size Setting should be the first Variable write operation,
1988 // We can assume all Read/Write is OK if we can set Variable store size successfully.
1989 // If write fail, we will assert here
1990 //
1991 ASSERT(VariableStoreHeader->Size == VariableStoreLength);
1992
1993 if (EFI_ERROR (Status)) {
1994 goto Done;
1995 }
1996 }
1997
1998 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = (EFI_PHYSICAL_ADDRESS) ((UINTN) CurrPtr);
1999 //
2000 // Parse non-volatile variable data and get last variable offset
2001 //
2002 NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) CurrPtr);
2003 Status = EFI_SUCCESS;
2004
2005 while (IsValidVariableHeader (NextVariable)) {
2006 NextVariable = GetNextVariablePtr (NextVariable);
2007 }
2008
2009 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) CurrPtr;
2010
2011 //
2012 // Check if the free area is really free.
2013 //
2014 for (Index = mVariableModuleGlobal->NonVolatileLastVariableOffset; Index < VariableStoreHeader->Size; Index++) {
2015 Data = ((UINT8 *) (UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase)[Index];
2016 if (Data != 0xff) {
2017 //
2018 // There must be something wrong in variable store, do reclaim operation.
2019 //
2020 Status = Reclaim (
2021 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,
2022 &mVariableModuleGlobal->NonVolatileLastVariableOffset,
2023 FALSE,
2024 NULL
2025 );
2026
2027 if (EFI_ERROR (Status)) {
2028 goto Done;
2029 }
2030
2031 break;
2032 }
2033 }
2034
2035 //
2036 // Register the event handling function to reclaim variable for OS usage.
2037 //
2038 Status = EfiCreateEventReadyToBootEx (
2039 TPL_NOTIFY,
2040 ReclaimForOS,
2041 NULL,
2042 &ReadyToBootEvent
2043 );
2044 }
2045
2046 Done:
2047 if (EFI_ERROR (Status)) {
2048 FreePool (mVariableModuleGlobal);
2049 FreePool (VolatileVariableStore);
2050 }
2051
2052 return Status;
2053 }
2054
2055 VOID
2056 EFIAPI
2057 VariableClassAddressChangeEvent (
2058 IN EFI_EVENT Event,
2059 IN VOID *Context
2060 )
2061 {
2062 EfiConvertPointer (
2063 0x0,
2064 (VOID **) &mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase
2065 );
2066 EfiConvertPointer (
2067 0x0,
2068 (VOID **) &mVariableModuleGlobal->VariableGlobal.VolatileVariableBase
2069 );
2070 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal);
2071 }
2072
2073
2074 /**
2075 Variable Driver main entry point. The Variable driver places the 4 EFI
2076 runtime services in the EFI System Table and installs arch protocols
2077 for variable read and write services being availible.
2078
2079 @param[in] ImageHandle The firmware allocated handle for the EFI image.
2080 @param[in] SystemTable A pointer to the EFI System Table.
2081
2082 @retval EFI_SUCCESS The entry point is executed successfully.
2083 @retval other Some error occurs when executing this entry point.
2084
2085 **/
2086 EFI_STATUS
2087 EFIAPI
2088 VariableServiceInitialize (
2089 IN EFI_HANDLE ImageHandle,
2090 IN EFI_SYSTEM_TABLE *SystemTable
2091 )
2092 {
2093 EFI_STATUS Status;
2094
2095 Status = VariableCommonInitialize (ImageHandle, SystemTable);
2096 ASSERT_EFI_ERROR (Status);
2097
2098 SystemTable->RuntimeServices->GetVariable = RuntimeServiceGetVariable;
2099 SystemTable->RuntimeServices->GetNextVariableName = RuntimeServiceGetNextVariableName;
2100 SystemTable->RuntimeServices->SetVariable = RuntimeServiceSetVariable;
2101 SystemTable->RuntimeServices->QueryVariableInfo = RuntimeServiceQueryVariableInfo;
2102
2103 //
2104 // Now install the Variable Runtime Architectural Protocol on a new handle
2105 //
2106 Status = gBS->InstallMultipleProtocolInterfaces (
2107 &mHandle,
2108 &gEfiVariableArchProtocolGuid, NULL,
2109 &gEfiVariableWriteArchProtocolGuid, NULL,
2110 NULL
2111 );
2112 ASSERT_EFI_ERROR (Status);
2113
2114 Status = gBS->CreateEvent (
2115 EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE,
2116 TPL_NOTIFY,
2117 VariableClassAddressChangeEvent,
2118 NULL,
2119 &mVirtualAddressChangeEvent
2120 );
2121 ASSERT_EFI_ERROR (Status);
2122
2123 return EFI_SUCCESS;
2124 }
2125