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