]> git.proxmox.com Git - mirror_edk2.git/blob - EdkModulePkg/Universal/Variable/RuntimeDxe/Variable.c
f59d2a396981102b7fecde6faad535e41e028c2a
[mirror_edk2.git] / EdkModulePkg / Universal / Variable / RuntimeDxe / Variable.c
1 /*++
2
3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 Variable.c
15
16 Abstract:
17
18 Revision History
19
20 --*/
21
22 #include "Variable.h"
23 #include "reclaim.h"
24
25 //
26 // Don't use module globals after the SetVirtualAddress map is signaled
27 //
28 ESAL_VARIABLE_GLOBAL *mVariableModuleGlobal;
29
30 UINT32
31 EFIAPI
32 ArrayLength (
33 IN CHAR16 *String
34 )
35 /*++
36
37 Routine Description:
38
39 Determine the length of null terminated char16 array.
40
41 Arguments:
42
43 String Null-terminated CHAR16 array pointer.
44
45 Returns:
46
47 UINT32 Number of bytes in the string, including the double NULL at the end;
48
49 --*/
50 {
51 UINT32 Count;
52
53 if (NULL == String) {
54 return 0;
55 }
56
57 Count = 0;
58
59 while (0 != String[Count]) {
60 Count++;
61 }
62
63 return (Count * 2) + 2;
64 }
65
66 BOOLEAN
67 EFIAPI
68 IsValidVariableHeader (
69 IN VARIABLE_HEADER *Variable
70 )
71 /*++
72
73 Routine Description:
74
75 This code checks if variable header is valid or not.
76
77 Arguments:
78 Variable Pointer to the Variable Header.
79
80 Returns:
81 TRUE Variable header is valid.
82 FALSE Variable header is not valid.
83
84 --*/
85 {
86 if (Variable == NULL ||
87 Variable->StartId != VARIABLE_DATA ||
88 (sizeof (VARIABLE_HEADER) + Variable->NameSize + Variable->DataSize) > MAX_VARIABLE_SIZE
89 ) {
90 return FALSE;
91 }
92
93 return TRUE;
94 }
95
96 EFI_STATUS
97 EFIAPI
98 UpdateVariableStore (
99 IN VARIABLE_GLOBAL *Global,
100 IN BOOLEAN Volatile,
101 IN BOOLEAN SetByIndex,
102 IN UINTN Instance,
103 IN UINTN DataPtrIndex,
104 IN UINT32 DataSize,
105 IN UINT8 *Buffer
106 )
107 /*++
108
109 Routine Description:
110
111 This function writes data to the FWH at the correct LBA even if the LBAs
112 are fragmented.
113
114 Arguments:
115
116 Global Pointer to VARAIBLE_GLOBAL structure
117 Volatile If the Variable is Volatile or Non-Volatile
118 SetByIndex TRUE: Target pointer is given as index
119 FALSE: Target pointer is absolute
120 Instance Instance of FV Block services
121 DataPtrIndex Pointer to the Data from the end of VARIABLE_STORE_HEADER
122 structure
123 DataSize Size of data to be written.
124 Buffer Pointer to the buffer from which data is written
125
126 Returns:
127
128 EFI STATUS
129
130 --*/
131 {
132 EFI_FV_BLOCK_MAP_ENTRY *PtrBlockMapEntry;
133 UINTN BlockIndex2;
134 UINTN LinearOffset;
135 UINTN CurrWriteSize;
136 UINTN CurrWritePtr;
137 UINT8 *CurrBuffer;
138 EFI_LBA LbaNumber;
139 UINTN Size;
140 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
141 VARIABLE_STORE_HEADER *VolatileBase;
142 EFI_PHYSICAL_ADDRESS FvVolHdr;
143 EFI_PHYSICAL_ADDRESS DataPtr;
144 EFI_STATUS Status;
145
146 FwVolHeader = NULL;
147 DataPtr = DataPtrIndex;
148
149 //
150 // Check if the Data is Volatile
151 //
152 if (!Volatile) {
153 EfiFvbGetPhysicalAddress (Instance, &FvVolHdr);
154 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvVolHdr);
155 //
156 // Data Pointer should point to the actual Address where data is to be
157 // written
158 //
159 if (SetByIndex) {
160 DataPtr += Global->NonVolatileVariableBase;
161 }
162
163 if ((DataPtr + DataSize) >= ((EFI_PHYSICAL_ADDRESS) (UINTN) ((UINT8 *) FwVolHeader + FwVolHeader->FvLength))) {
164 return EFI_INVALID_PARAMETER;
165 }
166 } else {
167 //
168 // Data Pointer should point to the actual Address where data is to be
169 // written
170 //
171 VolatileBase = (VARIABLE_STORE_HEADER *) ((UINTN) Global->VolatileVariableBase);
172 if (SetByIndex) {
173 DataPtr += Global->VolatileVariableBase;
174 }
175
176 if ((DataPtr + DataSize) >= ((UINTN) ((UINT8 *) VolatileBase + VolatileBase->Size))) {
177 return EFI_INVALID_PARAMETER;
178 }
179 }
180 //
181 // If Volatile Variable just do a simple mem copy.
182 //
183 if (Volatile) {
184 CopyMem ((UINT8 *) ((UINTN) DataPtr), Buffer, DataSize);
185 return EFI_SUCCESS;
186 }
187 //
188 // If we are here we are dealing with Non-Volatile Variables
189 //
190 LinearOffset = (UINTN) FwVolHeader;
191 CurrWritePtr = (UINTN) DataPtr;
192 CurrWriteSize = DataSize;
193 CurrBuffer = Buffer;
194 LbaNumber = 0;
195
196 if (CurrWritePtr < LinearOffset) {
197 return EFI_INVALID_PARAMETER;
198 }
199
200 for (PtrBlockMapEntry = FwVolHeader->FvBlockMap; PtrBlockMapEntry->NumBlocks != 0; PtrBlockMapEntry++) {
201 for (BlockIndex2 = 0; BlockIndex2 < PtrBlockMapEntry->NumBlocks; BlockIndex2++) {
202 //
203 // Check to see if the Variable Writes are spanning through multiple
204 // blocks.
205 //
206 if ((CurrWritePtr >= LinearOffset) && (CurrWritePtr < LinearOffset + PtrBlockMapEntry->BlockLength)) {
207 if ((CurrWritePtr + CurrWriteSize) <= (LinearOffset + PtrBlockMapEntry->BlockLength)) {
208 Status = EfiFvbWriteBlock (
209 Instance,
210 LbaNumber,
211 (UINTN) (CurrWritePtr - LinearOffset),
212 &CurrWriteSize,
213 CurrBuffer
214 );
215 if (EFI_ERROR (Status)) {
216 return Status;
217 }
218 } else {
219 Size = (UINT32) (LinearOffset + PtrBlockMapEntry->BlockLength - CurrWritePtr);
220 Status = EfiFvbWriteBlock (
221 Instance,
222 LbaNumber,
223 (UINTN) (CurrWritePtr - LinearOffset),
224 &Size,
225 CurrBuffer
226 );
227 if (EFI_ERROR (Status)) {
228 return Status;
229 }
230
231 CurrWritePtr = LinearOffset + PtrBlockMapEntry->BlockLength;
232 CurrBuffer = CurrBuffer + Size;
233 CurrWriteSize = CurrWriteSize - Size;
234 }
235 }
236
237 LinearOffset += PtrBlockMapEntry->BlockLength;
238 LbaNumber++;
239 }
240 }
241
242 return EFI_SUCCESS;
243 }
244
245 VARIABLE_STORE_STATUS
246 EFIAPI
247 GetVariableStoreStatus (
248 IN VARIABLE_STORE_HEADER *VarStoreHeader
249 )
250 /*++
251
252 Routine Description:
253
254 This code gets the current status of Variable Store.
255
256 Arguments:
257
258 VarStoreHeader Pointer to the Variable Store Header.
259
260 Returns:
261
262 EfiRaw Variable store status is raw
263 EfiValid Variable store status is valid
264 EfiInvalid Variable store status is invalid
265
266 --*/
267 {
268 if (VarStoreHeader->Signature == VARIABLE_STORE_SIGNATURE &&
269 VarStoreHeader->Format == VARIABLE_STORE_FORMATTED &&
270 VarStoreHeader->State == VARIABLE_STORE_HEALTHY
271 ) {
272
273 return EfiValid;
274 } else if (VarStoreHeader->Signature == 0xffffffff &&
275 VarStoreHeader->Size == 0xffffffff &&
276 VarStoreHeader->Format == 0xff &&
277 VarStoreHeader->State == 0xff
278 ) {
279
280 return EfiRaw;
281 } else {
282 return EfiInvalid;
283 }
284 }
285
286 UINT8 *
287 EFIAPI
288 GetVariableDataPtr (
289 IN VARIABLE_HEADER *Variable
290 )
291 /*++
292
293 Routine Description:
294
295 This code gets the pointer to the variable data.
296
297 Arguments:
298
299 Variable Pointer to the Variable Header.
300
301 Returns:
302
303 UINT8* Pointer to Variable Data
304
305 --*/
306 {
307 //
308 // Be careful about pad size for alignment
309 //
310 return (UINT8 *) ((UINTN) GET_VARIABLE_NAME_PTR (Variable) + Variable->NameSize + GET_PAD_SIZE (Variable->NameSize));
311 }
312
313 VARIABLE_HEADER *
314 EFIAPI
315 GetNextVariablePtr (
316 IN VARIABLE_HEADER *Variable
317 )
318 /*++
319
320 Routine Description:
321
322 This code gets the pointer to the next variable header.
323
324 Arguments:
325
326 Variable Pointer to the Variable Header.
327
328 Returns:
329
330 VARIABLE_HEADER* Pointer to next variable header.
331
332 --*/
333 {
334 if (!IsValidVariableHeader (Variable)) {
335 return NULL;
336 }
337 //
338 // Be careful about pad size for alignment
339 //
340 return (VARIABLE_HEADER *) ((UINTN) GetVariableDataPtr (Variable) + Variable->DataSize + GET_PAD_SIZE (Variable->DataSize));
341 }
342
343 VARIABLE_HEADER *
344 EFIAPI
345 GetEndPointer (
346 IN VARIABLE_STORE_HEADER *VarStoreHeader
347 )
348 /*++
349
350 Routine Description:
351
352 This code gets the pointer to the last variable memory pointer byte
353
354 Arguments:
355
356 VarStoreHeader Pointer to the Variable Store Header.
357
358 Returns:
359
360 VARIABLE_HEADER* Pointer to last unavailable Variable Header
361
362 --*/
363 {
364 //
365 // The end of variable store
366 //
367 return (VARIABLE_HEADER *) ((UINTN) VarStoreHeader + VarStoreHeader->Size);
368 }
369
370 EFI_STATUS
371 EFIAPI
372 Reclaim (
373 IN EFI_PHYSICAL_ADDRESS VariableBase,
374 OUT UINTN *LastVariableOffset,
375 IN BOOLEAN IsVolatile
376 )
377 /*++
378
379 Routine Description:
380
381 Variable store garbage collection and reclaim operation
382
383 Arguments:
384
385 VariableBase Base address of variable store
386 LastVariableOffset Offset of last variable
387 IsVolatile The variable store is volatile or not,
388 if it is non-volatile, need FTW
389
390 Returns:
391
392 EFI STATUS
393
394 --*/
395 {
396 VARIABLE_HEADER *Variable;
397 VARIABLE_HEADER *NextVariable;
398 VARIABLE_STORE_HEADER *VariableStoreHeader;
399 UINT8 *ValidBuffer;
400 UINTN ValidBufferSize;
401 UINTN VariableSize;
402 UINT8 *CurrPtr;
403 EFI_STATUS Status;
404
405 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) VariableBase);
406
407 //
408 // Start Pointers for the variable.
409 //
410 Variable = (VARIABLE_HEADER *) (VariableStoreHeader + 1);
411
412 ValidBufferSize = sizeof (VARIABLE_STORE_HEADER);
413
414 while (IsValidVariableHeader (Variable)) {
415 NextVariable = GetNextVariablePtr (Variable);
416 if (Variable->State == VAR_ADDED) {
417 VariableSize = (UINTN) NextVariable - (UINTN) Variable;
418 ValidBufferSize += VariableSize;
419 }
420
421 Variable = NextVariable;
422 }
423
424 Status = gBS->AllocatePool (
425 EfiBootServicesData,
426 ValidBufferSize,
427 (VOID **) &ValidBuffer
428 );
429 if (EFI_ERROR (Status)) {
430 return Status;
431 }
432
433 SetMem (ValidBuffer, ValidBufferSize, 0xff);
434
435 CurrPtr = ValidBuffer;
436
437 //
438 // Copy variable store header
439 //
440 CopyMem (CurrPtr, VariableStoreHeader, sizeof (VARIABLE_STORE_HEADER));
441 CurrPtr += sizeof (VARIABLE_STORE_HEADER);
442
443 //
444 // Start Pointers for the variable.
445 //
446 Variable = (VARIABLE_HEADER *) (VariableStoreHeader + 1);
447
448 while (IsValidVariableHeader (Variable)) {
449 NextVariable = GetNextVariablePtr (Variable);
450 if (Variable->State == VAR_ADDED) {
451 VariableSize = (UINTN) NextVariable - (UINTN) Variable;
452 CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);
453 CurrPtr += VariableSize;
454 }
455
456 Variable = NextVariable;
457 }
458
459 if (IsVolatile) {
460 //
461 // If volatile variable store, just copy valid buffer
462 //
463 SetMem ((UINT8 *) (UINTN) VariableBase, VariableStoreHeader->Size, 0xff);
464 CopyMem ((UINT8 *) (UINTN) VariableBase, ValidBuffer, ValidBufferSize);
465 *LastVariableOffset = ValidBufferSize;
466 Status = EFI_SUCCESS;
467 } else {
468 //
469 // If non-volatile variable store, perform FTW here.
470 //
471 Status = FtwVariableSpace (
472 VariableBase,
473 ValidBuffer,
474 ValidBufferSize
475 );
476 if (!EFI_ERROR (Status)) {
477 *LastVariableOffset = ValidBufferSize;
478 }
479 }
480
481 gBS->FreePool (ValidBuffer);
482
483 if (EFI_ERROR (Status)) {
484 *LastVariableOffset = 0;
485 }
486
487 return Status;
488 }
489
490 EFI_STATUS
491 EFIAPI
492 FindVariable (
493 IN CHAR16 *VariableName,
494 IN EFI_GUID *VendorGuid,
495 OUT VARIABLE_POINTER_TRACK *PtrTrack,
496 IN VARIABLE_GLOBAL *Global
497 )
498 /*++
499
500 Routine Description:
501
502 This code finds variable in storage blocks (Volatile or Non-Volatile)
503
504 Arguments:
505
506 VariableName Name of the variable to be found
507 VendorGuid Vendor GUID to be found.
508 PtrTrack Variable Track Pointer structure that contains
509 Variable Information.
510 Contains the pointer of Variable header.
511 Global VARIABLE_GLOBAL pointer
512
513 Returns:
514
515 EFI STATUS
516
517 --*/
518 {
519 VARIABLE_HEADER *Variable[2];
520 VARIABLE_STORE_HEADER *VariableStoreHeader[2];
521 UINTN Index;
522
523 //
524 // 0: Non-Volatile, 1: Volatile
525 //
526 VariableStoreHeader[0] = (VARIABLE_STORE_HEADER *) ((UINTN) Global->NonVolatileVariableBase);
527 VariableStoreHeader[1] = (VARIABLE_STORE_HEADER *) ((UINTN) Global->VolatileVariableBase);
528
529 //
530 // Start Pointers for the variable.
531 // Actual Data Pointer where data can be written.
532 //
533 Variable[0] = (VARIABLE_HEADER *) (VariableStoreHeader[0] + 1);
534 Variable[1] = (VARIABLE_HEADER *) (VariableStoreHeader[1] + 1);
535
536 if (VariableName[0] != 0 && VendorGuid == NULL) {
537 return EFI_INVALID_PARAMETER;
538 }
539 //
540 // Find the variable by walk through non-volatile and volatile variable store
541 //
542 for (Index = 0; Index < 2; Index++) {
543 PtrTrack->StartPtr = (VARIABLE_HEADER *) (VariableStoreHeader[Index] + 1);
544 PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader[Index]);
545
546 while (IsValidVariableHeader (Variable[Index]) && (Variable[Index] <= GetEndPointer (VariableStoreHeader[Index]))) {
547 if (Variable[Index]->State == VAR_ADDED) {
548 if (!(EfiAtRuntime () && !(Variable[Index]->Attributes & EFI_VARIABLE_RUNTIME_ACCESS))) {
549 if (VariableName[0] == 0) {
550 PtrTrack->CurrPtr = Variable[Index];
551 PtrTrack->Volatile = (BOOLEAN) Index;
552 return EFI_SUCCESS;
553 } else {
554 if (CompareGuid (VendorGuid, &Variable[Index]->VendorGuid)) {
555 if (!CompareMem (VariableName, GET_VARIABLE_NAME_PTR (Variable[Index]), ArrayLength (VariableName))) {
556 PtrTrack->CurrPtr = Variable[Index];
557 PtrTrack->Volatile = (BOOLEAN) Index;
558 return EFI_SUCCESS;
559 }
560 }
561 }
562 }
563 }
564
565 Variable[Index] = GetNextVariablePtr (Variable[Index]);
566 }
567 //
568 // While (...)
569 //
570 }
571 //
572 // for (...)
573 //
574 PtrTrack->CurrPtr = NULL;
575 return EFI_NOT_FOUND;
576 }
577
578 EFI_STATUS
579 EFIAPI
580 GetVariable (
581 IN CHAR16 *VariableName,
582 IN EFI_GUID * VendorGuid,
583 OUT UINT32 *Attributes OPTIONAL,
584 IN OUT UINTN *DataSize,
585 OUT VOID *Data,
586 IN VARIABLE_GLOBAL * Global,
587 IN UINT32 Instance
588 )
589 /*++
590
591 Routine Description:
592
593 This code finds variable in storage blocks (Volatile or Non-Volatile)
594
595 Arguments:
596
597 VariableName Name of Variable to be found
598 VendorGuid Variable vendor GUID
599 Attributes OPTIONAL Attribute value of the variable found
600 DataSize Size of Data found. If size is less than the
601 data, this value contains the required size.
602 Data Data pointer
603 Global Pointer to VARIABLE_GLOBAL structure
604 Instance Instance of the Firmware Volume.
605
606 Returns:
607
608 EFI STATUS
609
610 --*/
611 {
612 VARIABLE_POINTER_TRACK Variable;
613 UINTN VarDataSize;
614 EFI_STATUS Status;
615
616 if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {
617 return EFI_INVALID_PARAMETER;
618 }
619 //
620 // Find existing variable
621 //
622 Status = FindVariable (VariableName, VendorGuid, &Variable, Global);
623
624 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {
625 return Status;
626 }
627 //
628 // Get data size
629 //
630 VarDataSize = Variable.CurrPtr->DataSize;
631 if (*DataSize >= VarDataSize) {
632 if (Data == NULL) {
633 return EFI_INVALID_PARAMETER;
634 }
635
636 CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);
637 if (Attributes != NULL) {
638 *Attributes = Variable.CurrPtr->Attributes;
639 }
640
641 *DataSize = VarDataSize;
642 return EFI_SUCCESS;
643 } else {
644 *DataSize = VarDataSize;
645 return EFI_BUFFER_TOO_SMALL;
646 }
647 }
648
649 EFI_STATUS
650 EFIAPI
651 GetNextVariableName (
652 IN OUT UINTN *VariableNameSize,
653 IN OUT CHAR16 *VariableName,
654 IN OUT EFI_GUID *VendorGuid,
655 IN VARIABLE_GLOBAL *Global,
656 IN UINT32 Instance
657 )
658 /*++
659
660 Routine Description:
661
662 This code Finds the Next available variable
663
664 Arguments:
665
666 VariableNameSize Size of the variable
667 VariableName Pointer to variable name
668 VendorGuid Variable Vendor Guid
669 Global VARIABLE_GLOBAL structure pointer.
670 Instance FV instance
671
672 Returns:
673
674 EFI STATUS
675
676 --*/
677 {
678 VARIABLE_POINTER_TRACK Variable;
679 UINTN VarNameSize;
680 EFI_STATUS Status;
681
682 if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {
683 return EFI_INVALID_PARAMETER;
684 }
685
686 Status = FindVariable (VariableName, VendorGuid, &Variable, Global);
687
688 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {
689 return Status;
690 }
691
692 if (VariableName[0] != 0) {
693 //
694 // If variable name is not NULL, get next variable
695 //
696 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
697 }
698
699 while (TRUE) {
700 //
701 // If both volatile and non-volatile variable store are parsed,
702 // return not found
703 //
704 if (Variable.CurrPtr >= Variable.EndPtr || Variable.CurrPtr == NULL) {
705 Variable.Volatile = (BOOLEAN) (Variable.Volatile ^ ((BOOLEAN) 0x1));
706 if (Variable.Volatile) {
707 Variable.StartPtr = (VARIABLE_HEADER *) ((UINTN) (Global->VolatileVariableBase + sizeof (VARIABLE_STORE_HEADER)));
708 Variable.EndPtr = (VARIABLE_HEADER *) GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) Global->VolatileVariableBase));
709 } else {
710 return EFI_NOT_FOUND;
711 }
712
713 Variable.CurrPtr = Variable.StartPtr;
714 if (!IsValidVariableHeader (Variable.CurrPtr)) {
715 continue;
716 }
717 }
718 //
719 // Variable is found
720 //
721 if (IsValidVariableHeader (Variable.CurrPtr) && Variable.CurrPtr->State == VAR_ADDED) {
722 if (!(EfiAtRuntime () && !(Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS))) {
723 VarNameSize = Variable.CurrPtr->NameSize;
724 if (VarNameSize <= *VariableNameSize) {
725 CopyMem (
726 VariableName,
727 GET_VARIABLE_NAME_PTR (Variable.CurrPtr),
728 VarNameSize
729 );
730 CopyMem (
731 VendorGuid,
732 &Variable.CurrPtr->VendorGuid,
733 sizeof (EFI_GUID)
734 );
735 Status = EFI_SUCCESS;
736 } else {
737 Status = EFI_BUFFER_TOO_SMALL;
738 }
739
740 *VariableNameSize = VarNameSize;
741 return Status;
742 }
743 }
744
745 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
746 }
747
748 return EFI_NOT_FOUND;
749 }
750
751 EFI_STATUS
752 EFIAPI
753 SetVariable (
754 IN CHAR16 *VariableName,
755 IN EFI_GUID *VendorGuid,
756 IN UINT32 Attributes,
757 IN UINTN DataSize,
758 IN VOID *Data,
759 IN VARIABLE_GLOBAL *Global,
760 IN UINTN *VolatileOffset,
761 IN UINTN *NonVolatileOffset,
762 IN UINT32 Instance
763 )
764 /*++
765
766 Routine Description:
767
768 This code sets variable in storage blocks (Volatile or Non-Volatile)
769
770 Arguments:
771
772 VariableName Name of Variable to be found
773 VendorGuid Variable vendor GUID
774 Attributes Attribute value of the variable found
775 DataSize Size of Data found. If size is less than the
776 data, this value contains the required size.
777 Data Data pointer
778 Global Pointer to VARIABLE_GLOBAL structure
779 VolatileOffset The offset of last volatile variable
780 NonVolatileOffset The offset of last non-volatile variable
781 Instance Instance of the Firmware Volume.
782
783 Returns:
784
785 EFI STATUS
786 EFI_INVALID_PARAMETER - Invalid parameter
787 EFI_SUCCESS - Set successfully
788 EFI_OUT_OF_RESOURCES - Resource not enough to set variable
789 EFI_NOT_FOUND - Not found
790
791 --*/
792 {
793 VARIABLE_POINTER_TRACK Variable;
794 EFI_STATUS Status;
795 VARIABLE_HEADER *NextVariable;
796 UINTN VarNameSize;
797 UINTN VarNameOffset;
798 UINTN VarDataOffset;
799 UINTN VarSize;
800 UINT8 State;
801 BOOLEAN Reclaimed;
802
803 Reclaimed = FALSE;
804
805 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {
806 return EFI_INVALID_PARAMETER;
807 }
808
809 Status = FindVariable (VariableName, VendorGuid, &Variable, Global);
810
811 if (Status == EFI_INVALID_PARAMETER) {
812 return Status;
813 } else if (!EFI_ERROR (Status) && Variable.Volatile && EfiAtRuntime()) {
814 //
815 // If EfiAtRuntime and the variable is Volatile and Runtime Access,
816 // the volatile is ReadOnly, and SetVariable should be aborted and
817 // return EFI_WRITE_PROTECTED.
818 //
819 return EFI_WRITE_PROTECTED;
820 } else if (sizeof (VARIABLE_HEADER) + ArrayLength (VariableName) + DataSize > MAX_VARIABLE_SIZE) {
821 //
822 // The size of the VariableName, including the Unicode Null in bytes plus
823 // the DataSize is limited to maximum size of MAX_VARIABLE_SIZE (1024) bytes.
824 //
825 return EFI_INVALID_PARAMETER;
826 } else if (Attributes == EFI_VARIABLE_NON_VOLATILE) {
827 //
828 // Make sure not only EFI_VARIABLE_NON_VOLATILE is set
829 //
830 return EFI_INVALID_PARAMETER;
831 } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) ==
832 EFI_VARIABLE_RUNTIME_ACCESS) {
833 //
834 // Make sure if runtime bit is set, boot service bit is set also
835 //
836 return EFI_INVALID_PARAMETER;
837 } else if (EfiAtRuntime () && Attributes && !(Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {
838 //
839 // Runtime but Attribute is not Runtime
840 //
841 return EFI_INVALID_PARAMETER;
842 } else if (EfiAtRuntime () && Attributes && !(Attributes & EFI_VARIABLE_NON_VOLATILE)) {
843 //
844 // Cannot set volatile variable in Runtime
845 //
846 return EFI_INVALID_PARAMETER;
847 } else if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {
848 //
849 // Setting a data variable with no access, or zero DataSize attributes
850 // specified causes it to be deleted.
851 //
852 if (!EFI_ERROR (Status)) {
853 State = Variable.CurrPtr->State;
854 State &= VAR_DELETED;
855
856 Status = UpdateVariableStore (
857 Global,
858 Variable.Volatile,
859 FALSE,
860 Instance,
861 (UINTN) &Variable.CurrPtr->State,
862 sizeof (UINT8),
863 &State
864 );
865 if (EFI_ERROR (Status)) {
866 return Status;
867 }
868
869 return EFI_SUCCESS;
870 }
871
872 return EFI_NOT_FOUND;
873 } else {
874 if (!EFI_ERROR (Status)) {
875 //
876 // If the variable is marked valid and the same data has been passed in
877 // then return to the caller immediately.
878 //
879 if (Variable.CurrPtr->DataSize == DataSize &&
880 !CompareMem (Data, GetVariableDataPtr (Variable.CurrPtr), DataSize)
881 ) {
882 return EFI_SUCCESS;
883 } else if (Variable.CurrPtr->State == VAR_ADDED) {
884 //
885 // Mark the old variable as in delete transition
886 //
887 State = Variable.CurrPtr->State;
888 State &= VAR_IN_DELETED_TRANSITION;
889
890 Status = UpdateVariableStore (
891 Global,
892 Variable.Volatile,
893 FALSE,
894 Instance,
895 (UINTN) &Variable.CurrPtr->State,
896 sizeof (UINT8),
897 &State
898 );
899 if (EFI_ERROR (Status)) {
900 return Status;
901 }
902 }
903 }
904 //
905 // Create a new variable and copy the data.
906 //
907 // Tricky part: Use scratch data area at the end of volatile variable store
908 // as a temporary storage.
909 //
910 NextVariable = GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) Global->VolatileVariableBase));
911
912 SetMem (NextVariable, SCRATCH_SIZE, 0xff);
913
914 NextVariable->StartId = VARIABLE_DATA;
915 NextVariable->Attributes = Attributes;
916 //
917 // NextVariable->State = VAR_ADDED;
918 //
919 NextVariable->Reserved = 0;
920 VarNameOffset = sizeof (VARIABLE_HEADER);
921 VarNameSize = ArrayLength (VariableName);
922 CopyMem (
923 (UINT8 *) ((UINTN) NextVariable + VarNameOffset),
924 VariableName,
925 VarNameSize
926 );
927 VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);
928 CopyMem (
929 (UINT8 *) ((UINTN) NextVariable + VarDataOffset),
930 Data,
931 DataSize
932 );
933 CopyMem (&NextVariable->VendorGuid, VendorGuid, sizeof (EFI_GUID));
934 //
935 // There will be pad bytes after Data, the NextVariable->NameSize and
936 // NextVariable->DataSize should not include pad size so that variable
937 // service can get actual size in GetVariable
938 //
939 NextVariable->NameSize = (UINT32)VarNameSize;
940 NextVariable->DataSize = (UINT32)DataSize;
941
942 //
943 // The actual size of the variable that stores in storage should
944 // include pad size.
945 //
946 VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);
947 if (Attributes & EFI_VARIABLE_NON_VOLATILE) {
948 if ((UINT32) (VarSize +*NonVolatileOffset) >
949 ((VARIABLE_STORE_HEADER *) ((UINTN) (Global->NonVolatileVariableBase)))->Size
950 ) {
951 if (EfiAtRuntime ()) {
952 return EFI_OUT_OF_RESOURCES;
953 }
954 //
955 // Perform garbage collection & reclaim operation
956 //
957 Status = Reclaim (Global->NonVolatileVariableBase, NonVolatileOffset, FALSE);
958 if (EFI_ERROR (Status)) {
959 return Status;
960 }
961 //
962 // If still no enough space, return out of resources
963 //
964 if ((UINT32) (VarSize +*NonVolatileOffset) >
965 ((VARIABLE_STORE_HEADER *) ((UINTN) (Global->NonVolatileVariableBase)))->Size
966 ) {
967 return EFI_OUT_OF_RESOURCES;
968 }
969
970 Reclaimed = TRUE;
971 }
972 //
973 // Three steps
974 // 1. Write variable header
975 // 2. Write variable data
976 // 3. Set variable state to valid
977 //
978 //
979 // Step 1:
980 //
981 Status = UpdateVariableStore (
982 Global,
983 FALSE,
984 TRUE,
985 Instance,
986 *NonVolatileOffset,
987 sizeof (VARIABLE_HEADER),
988 (UINT8 *) NextVariable
989 );
990
991 if (EFI_ERROR (Status)) {
992 return Status;
993 }
994 //
995 // Step 2:
996 //
997 Status = UpdateVariableStore (
998 Global,
999 FALSE,
1000 TRUE,
1001 Instance,
1002 *NonVolatileOffset + sizeof (VARIABLE_HEADER),
1003 (UINT32) VarSize - sizeof (VARIABLE_HEADER),
1004 (UINT8 *) NextVariable + sizeof (VARIABLE_HEADER)
1005 );
1006
1007 if (EFI_ERROR (Status)) {
1008 return Status;
1009 }
1010 //
1011 // Step 3:
1012 //
1013 NextVariable->State = VAR_ADDED;
1014 Status = UpdateVariableStore (
1015 Global,
1016 FALSE,
1017 TRUE,
1018 Instance,
1019 *NonVolatileOffset,
1020 sizeof (VARIABLE_HEADER),
1021 (UINT8 *) NextVariable
1022 );
1023
1024 if (EFI_ERROR (Status)) {
1025 return Status;
1026 }
1027
1028 *NonVolatileOffset = *NonVolatileOffset + VarSize;
1029
1030 } else {
1031 if (EfiAtRuntime ()) {
1032 return EFI_INVALID_PARAMETER;
1033 }
1034
1035 if ((UINT32) (VarSize +*VolatileOffset) >
1036 ((VARIABLE_STORE_HEADER *) ((UINTN) (Global->VolatileVariableBase)))->Size
1037 ) {
1038 //
1039 // Perform garbage collection & reclaim operation
1040 //
1041 Status = Reclaim (Global->VolatileVariableBase, VolatileOffset, TRUE);
1042 if (EFI_ERROR (Status)) {
1043 return Status;
1044 }
1045 //
1046 // If still no enough space, return out of resources
1047 //
1048 if ((UINT32) (VarSize +*VolatileOffset) >
1049 ((VARIABLE_STORE_HEADER *) ((UINTN) (Global->VolatileVariableBase)))->Size
1050 ) {
1051 return EFI_OUT_OF_RESOURCES;
1052 }
1053
1054 Reclaimed = TRUE;
1055 }
1056
1057 NextVariable->State = VAR_ADDED;
1058 Status = UpdateVariableStore (
1059 Global,
1060 TRUE,
1061 TRUE,
1062 Instance,
1063 *VolatileOffset,
1064 (UINT32) VarSize,
1065 (UINT8 *) NextVariable
1066 );
1067
1068 if (EFI_ERROR (Status)) {
1069 return Status;
1070 }
1071
1072 *VolatileOffset = *VolatileOffset + VarSize;
1073 }
1074 //
1075 // Mark the old variable as deleted
1076 //
1077 if (!Reclaimed && !EFI_ERROR (Status) && Variable.CurrPtr != NULL) {
1078 State = Variable.CurrPtr->State;
1079 State &= VAR_DELETED;
1080
1081 Status = UpdateVariableStore (
1082 Global,
1083 Variable.Volatile,
1084 FALSE,
1085 Instance,
1086 (UINTN) &Variable.CurrPtr->State,
1087 sizeof (UINT8),
1088 &State
1089 );
1090
1091 if (EFI_ERROR (Status)) {
1092 return Status;
1093 }
1094 }
1095 }
1096
1097 return EFI_SUCCESS;
1098 }
1099
1100 #if (EFI_SPECIFICATION_VERSION >= 0x00020000)
1101 EFI_STATUS
1102 EFIAPI
1103 QueryVariableInfo (
1104 IN UINT32 Attributes,
1105 OUT UINT64 *MaximumVariableStorageSize,
1106 OUT UINT64 *RemainingVariableStorageSize,
1107 OUT UINT64 *MaximumVariableSize,
1108 IN VARIABLE_GLOBAL *Global,
1109 IN UINT32 Instance
1110 )
1111 /*++
1112
1113 Routine Description:
1114
1115 This code returns information about the EFI variables.
1116
1117 Arguments:
1118
1119 Attributes Attributes bitmask to specify the type of variables
1120 on which to return information.
1121 MaximumVariableStorageSize Pointer to the maximum size of the storage space available
1122 for the EFI variables associated with the attributes specified.
1123 RemainingVariableStorageSize Pointer to the remaining size of the storage space available
1124 for the EFI variables associated with the attributes specified.
1125 MaximumVariableSize Pointer to the maximum size of the individual EFI variables
1126 associated with the attributes specified.
1127 Global Pointer to VARIABLE_GLOBAL structure.
1128 Instance Instance of the Firmware Volume.
1129
1130 Returns:
1131
1132 EFI STATUS
1133 EFI_INVALID_PARAMETER - An invalid combination of attribute bits was supplied.
1134 EFI_SUCCESS - Query successfully.
1135 EFI_UNSUPPORTED - The attribute is not supported on this platform.
1136
1137 --*/
1138 {
1139 VARIABLE_HEADER *Variable;
1140 VARIABLE_HEADER *NextVariable;
1141 UINT64 VariableSize;
1142 VARIABLE_STORE_HEADER *VariableStoreHeader;
1143
1144 if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL) {
1145 return EFI_INVALID_PARAMETER;
1146 }
1147
1148 if((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS)) == 0) {
1149 //
1150 // Make sure the Attributes combination is supported by the platform.
1151 //
1152 return EFI_UNSUPPORTED;
1153 } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {
1154 //
1155 // Make sure if runtime bit is set, boot service bit is set also.
1156 //
1157 return EFI_INVALID_PARAMETER;
1158 } else if (EfiAtRuntime () && !(Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {
1159 //
1160 // Make sure RT Attribute is set if we are in Runtime phase.
1161 //
1162 return EFI_INVALID_PARAMETER;
1163 }
1164
1165 if((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {
1166 //
1167 // Query is Volatile related.
1168 //
1169 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) Global->VolatileVariableBase);
1170 } else {
1171 //
1172 // Query is Non-Volatile related.
1173 //
1174 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) Global->NonVolatileVariableBase);
1175 }
1176
1177 //
1178 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
1179 // with the storage size (excluding the storage header size).
1180 //
1181 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);
1182 *RemainingVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);
1183
1184 //
1185 // Let *MaximumVariableSize be MAX_VARIABLE_SIZE.
1186 //
1187 *MaximumVariableSize = MAX_VARIABLE_SIZE;
1188
1189 //
1190 // Point to the starting address of the variables.
1191 //
1192 Variable = (VARIABLE_HEADER *) (VariableStoreHeader + 1);
1193
1194 //
1195 // Now walk through the related variable store.
1196 //
1197 while (IsValidVariableHeader (Variable) && (Variable < GetEndPointer (VariableStoreHeader))) {
1198 NextVariable = GetNextVariablePtr (Variable);
1199 VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;
1200
1201 if (EfiAtRuntime ()) {
1202 //
1203 // we don't take the state of the variables in mind
1204 // when calculating RemainingVariableStorageSize,
1205 // since the space occupied by variables not marked with
1206 // VAR_ADDED is not allowed to be reclaimed in Runtime.
1207 //
1208 *RemainingVariableStorageSize -= VariableSize;
1209 } else {
1210 //
1211 // Only care about Variables with State VAR_ADDED,because
1212 // the space not marked as VAR_ADDED is reclaimable now.
1213 //
1214 if (Variable->State == VAR_ADDED) {
1215 *RemainingVariableStorageSize -= VariableSize;
1216 }
1217 }
1218
1219 //
1220 // Go to the next one
1221 //
1222 Variable = NextVariable;
1223 }
1224
1225 return EFI_SUCCESS;
1226 }
1227 #endif
1228
1229 EFI_STATUS
1230 EFIAPI
1231 VariableCommonInitialize (
1232 IN EFI_HANDLE ImageHandle,
1233 IN EFI_SYSTEM_TABLE *SystemTable
1234 )
1235 /*++
1236
1237 Routine Description:
1238 This function does common initialization for variable services
1239
1240 Arguments:
1241
1242 ImageHandle - The firmware allocated handle for the EFI image.
1243 SystemTable - A pointer to the EFI System Table.
1244
1245 Returns:
1246
1247 Status code.
1248
1249 EFI_NOT_FOUND - Variable store area not found.
1250 EFI_UNSUPPORTED - Currently only one non-volatile variable store is supported.
1251 EFI_SUCCESS - Variable services successfully initialized.
1252
1253 --*/
1254 {
1255 EFI_STATUS Status;
1256 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
1257 CHAR8 *CurrPtr;
1258 VARIABLE_STORE_HEADER *VolatileVariableStore;
1259 VARIABLE_STORE_HEADER *VariableStoreHeader;
1260 VARIABLE_HEADER *NextVariable;
1261 UINT32 Instance;
1262 EFI_PHYSICAL_ADDRESS FvVolHdr;
1263
1264 UINT64 TempVariableStoreHeader;
1265
1266 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;
1267 EFI_FLASH_SUBAREA_ENTRY VariableStoreEntry;
1268 UINT64 BaseAddress;
1269 UINT64 Length;
1270 UINTN Index;
1271 UINT8 Data;
1272
1273 Status = gBS->AllocatePool (
1274 EfiRuntimeServicesData,
1275 sizeof (ESAL_VARIABLE_GLOBAL),
1276 (VOID **) &mVariableModuleGlobal
1277 );
1278
1279 if (EFI_ERROR (Status)) {
1280 return Status;
1281 }
1282 //
1283 // Allocate memory for volatile variable store
1284 //
1285 Status = gBS->AllocatePool (
1286 EfiRuntimeServicesData,
1287 VARIABLE_STORE_SIZE + SCRATCH_SIZE,
1288 (VOID **) &VolatileVariableStore
1289 );
1290
1291 if (EFI_ERROR (Status)) {
1292 gBS->FreePool (mVariableModuleGlobal);
1293 return Status;
1294 }
1295
1296 SetMem (VolatileVariableStore, VARIABLE_STORE_SIZE + SCRATCH_SIZE, 0xff);
1297
1298 //
1299 // Variable Specific Data
1300 //
1301 mVariableModuleGlobal->VariableBase[Physical].VolatileVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VolatileVariableStore;
1302 mVariableModuleGlobal->VolatileLastVariableOffset = sizeof (VARIABLE_STORE_HEADER);
1303
1304 VolatileVariableStore->Signature = VARIABLE_STORE_SIGNATURE;
1305 VolatileVariableStore->Size = VARIABLE_STORE_SIZE;
1306 VolatileVariableStore->Format = VARIABLE_STORE_FORMATTED;
1307 VolatileVariableStore->State = VARIABLE_STORE_HEALTHY;
1308 VolatileVariableStore->Reserved = 0;
1309 VolatileVariableStore->Reserved1 = 0;
1310
1311 //
1312 // Get non volatile varaible store
1313 //
1314
1315 TempVariableStoreHeader = (UINT64) PcdGet32 (PcdFlashNvStorageVariableBase);
1316 VariableStoreEntry.Base = TempVariableStoreHeader + \
1317 (((EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) (TempVariableStoreHeader)) -> HeaderLength);
1318 VariableStoreEntry.Length = (UINT64) PcdGet32 (PcdFlashNvStorageVariableSize) - \
1319 (((EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) (TempVariableStoreHeader)) -> HeaderLength);
1320 //
1321 // Mark the variable storage region of the FLASH as RUNTIME
1322 //
1323 BaseAddress = VariableStoreEntry.Base & (~EFI_PAGE_MASK);
1324 Length = VariableStoreEntry.Length + (VariableStoreEntry.Base - BaseAddress);
1325 Length = (Length + EFI_PAGE_SIZE - 1) & (~EFI_PAGE_MASK);
1326
1327 Status = gDS->GetMemorySpaceDescriptor (BaseAddress, &GcdDescriptor);
1328 if (EFI_ERROR (Status)) {
1329 gBS->FreePool (mVariableModuleGlobal);
1330 gBS->FreePool (VolatileVariableStore);
1331 return EFI_UNSUPPORTED;
1332 }
1333
1334 Status = gDS->SetMemorySpaceAttributes (
1335 BaseAddress,
1336 Length,
1337 GcdDescriptor.Attributes | EFI_MEMORY_RUNTIME
1338 );
1339 if (EFI_ERROR (Status)) {
1340 gBS->FreePool (mVariableModuleGlobal);
1341 gBS->FreePool (VolatileVariableStore);
1342 return EFI_UNSUPPORTED;
1343 }
1344 //
1345 // Get address of non volatile variable store base
1346 //
1347 mVariableModuleGlobal->VariableBase[Physical].NonVolatileVariableBase = VariableStoreEntry.Base;
1348
1349 //
1350 // Check Integrity
1351 //
1352 //
1353 // Find the Correct Instance of the FV Block Service.
1354 //
1355 Instance = 0;
1356 CurrPtr = (CHAR8 *) ((UINTN) mVariableModuleGlobal->VariableBase[Physical].NonVolatileVariableBase);
1357 while (EfiFvbGetPhysicalAddress (Instance, &FvVolHdr) == EFI_SUCCESS) {
1358 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvVolHdr);
1359 if (CurrPtr >= (CHAR8 *) FwVolHeader && CurrPtr < (((CHAR8 *) FwVolHeader) + FwVolHeader->FvLength)) {
1360 mVariableModuleGlobal->FvbInstance = Instance;
1361 break;
1362 }
1363
1364 Instance++;
1365 }
1366
1367 VariableStoreHeader = (VARIABLE_STORE_HEADER *) CurrPtr;
1368 if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) {
1369 if (~VariableStoreHeader->Size == 0) {
1370 Status = UpdateVariableStore (
1371 &mVariableModuleGlobal->VariableBase[Physical],
1372 FALSE,
1373 FALSE,
1374 mVariableModuleGlobal->FvbInstance,
1375 (UINTN) &VariableStoreHeader->Size,
1376 sizeof (UINT32),
1377 (UINT8 *) &VariableStoreEntry.Length
1378 );
1379
1380 if (EFI_ERROR (Status)) {
1381 return Status;
1382 }
1383 }
1384
1385 mVariableModuleGlobal->VariableBase[Physical].NonVolatileVariableBase = (EFI_PHYSICAL_ADDRESS) ((UINTN) CurrPtr);
1386 //
1387 // Parse non-volatile variable data and get last variable offset
1388 //
1389 NextVariable = (VARIABLE_HEADER *) (CurrPtr + sizeof (VARIABLE_STORE_HEADER));
1390 Status = EFI_SUCCESS;
1391
1392 while (IsValidVariableHeader (NextVariable)) {
1393 NextVariable = GetNextVariablePtr (NextVariable);
1394 }
1395
1396 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) CurrPtr;
1397
1398 //
1399 // Check if the free area is blow a threshold
1400 //
1401 if ((((VARIABLE_STORE_HEADER *)((UINTN) CurrPtr))->Size - mVariableModuleGlobal->NonVolatileLastVariableOffset) < VARIABLE_RECLAIM_THRESHOLD) {
1402 Status = Reclaim (
1403 mVariableModuleGlobal->VariableBase[Physical].NonVolatileVariableBase,
1404 &mVariableModuleGlobal->NonVolatileLastVariableOffset,
1405 FALSE
1406 );
1407 }
1408
1409 if (EFI_ERROR (Status)) {
1410 gBS->FreePool (mVariableModuleGlobal);
1411 gBS->FreePool (VolatileVariableStore);
1412 return Status;
1413 }
1414
1415 //
1416 // Check if the free area is really free.
1417 //
1418 for (Index = mVariableModuleGlobal->NonVolatileLastVariableOffset; Index < VariableStoreHeader->Size; Index++) {
1419 Data = ((UINT8 *) (UINTN) mVariableModuleGlobal->VariableBase[Physical].NonVolatileVariableBase)[Index];
1420 if (Data != 0xff) {
1421 //
1422 // There must be something wrong in variable store, do reclaim operation.
1423 //
1424 Status = Reclaim (
1425 mVariableModuleGlobal->VariableBase[Physical].NonVolatileVariableBase,
1426 &mVariableModuleGlobal->NonVolatileLastVariableOffset,
1427 FALSE
1428 );
1429 break;
1430 }
1431 }
1432 }
1433
1434 if (EFI_ERROR (Status)) {
1435 gBS->FreePool (mVariableModuleGlobal);
1436 gBS->FreePool (VolatileVariableStore);
1437 }
1438
1439 return Status;
1440 }