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