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