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