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