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