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