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