]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
If setting variable in Runtime and there has been a same GUID and name variable exist...
[mirror_edk2.git] / MdeModulePkg / Universal / Variable / RuntimeDxe / Variable.c
1 /** @file
2
3 The common variable operation routines shared by DXE_RINTIME variable
4 module and DXE_SMM variable module.
5
6 Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14
15 **/
16
17 #include "Variable.h"
18
19 VARIABLE_MODULE_GLOBAL *mVariableModuleGlobal;
20
21 ///
22 /// Define a memory cache that improves the search performance for a variable.
23 ///
24 VARIABLE_STORE_HEADER *mNvVariableCache = NULL;
25
26 ///
27 /// The memory entry used for variable statistics data.
28 ///
29 VARIABLE_INFO_ENTRY *gVariableInfo = NULL;
30
31
32 /**
33 Routine used to track statistical information about variable usage.
34 The data is stored in the EFI system table so it can be accessed later.
35 VariableInfo.efi can dump out the table. Only Boot Services variable
36 accesses are tracked by this code. The PcdVariableCollectStatistics
37 build flag controls if this feature is enabled.
38
39 A read that hits in the cache will have Read and Cache true for
40 the transaction. Data is allocated by this routine, but never
41 freed.
42
43 @param[in] VariableName Name of the Variable to track.
44 @param[in] VendorGuid Guid of the Variable to track.
45 @param[in] Volatile TRUE if volatile FALSE if non-volatile.
46 @param[in] Read TRUE if GetVariable() was called.
47 @param[in] Write TRUE if SetVariable() was called.
48 @param[in] Delete TRUE if deleted via SetVariable().
49 @param[in] Cache TRUE for a cache hit.
50
51 **/
52 VOID
53 UpdateVariableInfo (
54 IN CHAR16 *VariableName,
55 IN EFI_GUID *VendorGuid,
56 IN BOOLEAN Volatile,
57 IN BOOLEAN Read,
58 IN BOOLEAN Write,
59 IN BOOLEAN Delete,
60 IN BOOLEAN Cache
61 )
62 {
63 VARIABLE_INFO_ENTRY *Entry;
64
65 if (FeaturePcdGet (PcdVariableCollectStatistics)) {
66
67 if (AtRuntime ()) {
68 // Don't collect statistics at runtime.
69 return;
70 }
71
72 if (gVariableInfo == NULL) {
73 //
74 // On the first call allocate a entry and place a pointer to it in
75 // the EFI System Table.
76 //
77 gVariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
78 ASSERT (gVariableInfo != NULL);
79
80 CopyGuid (&gVariableInfo->VendorGuid, VendorGuid);
81 gVariableInfo->Name = AllocatePool (StrSize (VariableName));
82 ASSERT (gVariableInfo->Name != NULL);
83 StrCpy (gVariableInfo->Name, VariableName);
84 gVariableInfo->Volatile = Volatile;
85 }
86
87
88 for (Entry = gVariableInfo; Entry != NULL; Entry = Entry->Next) {
89 if (CompareGuid (VendorGuid, &Entry->VendorGuid)) {
90 if (StrCmp (VariableName, Entry->Name) == 0) {
91 if (Read) {
92 Entry->ReadCount++;
93 }
94 if (Write) {
95 Entry->WriteCount++;
96 }
97 if (Delete) {
98 Entry->DeleteCount++;
99 }
100 if (Cache) {
101 Entry->CacheCount++;
102 }
103
104 return;
105 }
106 }
107
108 if (Entry->Next == NULL) {
109 //
110 // If the entry is not in the table add it.
111 // Next iteration of the loop will fill in the data.
112 //
113 Entry->Next = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
114 ASSERT (Entry->Next != NULL);
115
116 CopyGuid (&Entry->Next->VendorGuid, VendorGuid);
117 Entry->Next->Name = AllocatePool (StrSize (VariableName));
118 ASSERT (Entry->Next->Name != NULL);
119 StrCpy (Entry->Next->Name, VariableName);
120 Entry->Next->Volatile = Volatile;
121 }
122
123 }
124 }
125 }
126
127
128 /**
129
130 This code checks if variable header is valid or not.
131
132 @param Variable Pointer to the Variable Header.
133
134 @retval TRUE Variable header is valid.
135 @retval FALSE Variable header is not valid.
136
137 **/
138 BOOLEAN
139 IsValidVariableHeader (
140 IN VARIABLE_HEADER *Variable
141 )
142 {
143 if (Variable == NULL || Variable->StartId != VARIABLE_DATA) {
144 return FALSE;
145 }
146
147 return TRUE;
148 }
149
150
151 /**
152
153 This function writes data to the FWH at the correct LBA even if the LBAs
154 are fragmented.
155
156 @param Global Pointer to VARAIBLE_GLOBAL structure.
157 @param Volatile Point out the Variable is Volatile or Non-Volatile.
158 @param SetByIndex TRUE if target pointer is given as index.
159 FALSE if target pointer is absolute.
160 @param Fvb Pointer to the writable FVB protocol.
161 @param DataPtrIndex Pointer to the Data from the end of VARIABLE_STORE_HEADER
162 structure.
163 @param DataSize Size of data to be written.
164 @param Buffer Pointer to the buffer from which data is written.
165
166 @retval EFI_INVALID_PARAMETER Parameters not valid.
167 @retval EFI_SUCCESS Variable store successfully updated.
168
169 **/
170 EFI_STATUS
171 UpdateVariableStore (
172 IN VARIABLE_GLOBAL *Global,
173 IN BOOLEAN Volatile,
174 IN BOOLEAN SetByIndex,
175 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb,
176 IN UINTN DataPtrIndex,
177 IN UINT32 DataSize,
178 IN UINT8 *Buffer
179 )
180 {
181 EFI_FV_BLOCK_MAP_ENTRY *PtrBlockMapEntry;
182 UINTN BlockIndex2;
183 UINTN LinearOffset;
184 UINTN CurrWriteSize;
185 UINTN CurrWritePtr;
186 UINT8 *CurrBuffer;
187 EFI_LBA LbaNumber;
188 UINTN Size;
189 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
190 VARIABLE_STORE_HEADER *VolatileBase;
191 EFI_PHYSICAL_ADDRESS FvVolHdr;
192 EFI_PHYSICAL_ADDRESS DataPtr;
193 EFI_STATUS Status;
194
195 FwVolHeader = NULL;
196 DataPtr = DataPtrIndex;
197
198 //
199 // Check if the Data is Volatile.
200 //
201 if (!Volatile) {
202 ASSERT (Fvb != NULL);
203 Status = Fvb->GetPhysicalAddress(Fvb, &FvVolHdr);
204 ASSERT_EFI_ERROR (Status);
205
206 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvVolHdr);
207 //
208 // Data Pointer should point to the actual Address where data is to be
209 // written.
210 //
211 if (SetByIndex) {
212 DataPtr += mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;
213 }
214
215 if ((DataPtr + DataSize) >= ((EFI_PHYSICAL_ADDRESS) (UINTN) ((UINT8 *) FwVolHeader + FwVolHeader->FvLength))) {
216 return EFI_INVALID_PARAMETER;
217 }
218 } else {
219 //
220 // Data Pointer should point to the actual Address where data is to be
221 // written.
222 //
223 VolatileBase = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);
224 if (SetByIndex) {
225 DataPtr += mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;
226 }
227
228 if ((DataPtr + DataSize) >= ((UINTN) ((UINT8 *) VolatileBase + VolatileBase->Size))) {
229 return EFI_INVALID_PARAMETER;
230 }
231
232 //
233 // If Volatile Variable just do a simple mem copy.
234 //
235 CopyMem ((UINT8 *)(UINTN)DataPtr, Buffer, DataSize);
236 return EFI_SUCCESS;
237 }
238
239 //
240 // If we are here we are dealing with Non-Volatile Variables.
241 //
242 LinearOffset = (UINTN) FwVolHeader;
243 CurrWritePtr = (UINTN) DataPtr;
244 CurrWriteSize = DataSize;
245 CurrBuffer = Buffer;
246 LbaNumber = 0;
247
248 if (CurrWritePtr < LinearOffset) {
249 return EFI_INVALID_PARAMETER;
250 }
251
252 for (PtrBlockMapEntry = FwVolHeader->BlockMap; PtrBlockMapEntry->NumBlocks != 0; PtrBlockMapEntry++) {
253 for (BlockIndex2 = 0; BlockIndex2 < PtrBlockMapEntry->NumBlocks; BlockIndex2++) {
254 //
255 // Check to see if the Variable Writes are spanning through multiple
256 // blocks.
257 //
258 if ((CurrWritePtr >= LinearOffset) && (CurrWritePtr < LinearOffset + PtrBlockMapEntry->Length)) {
259 if ((CurrWritePtr + CurrWriteSize) <= (LinearOffset + PtrBlockMapEntry->Length)) {
260 Status = Fvb->Write (
261 Fvb,
262 LbaNumber,
263 (UINTN) (CurrWritePtr - LinearOffset),
264 &CurrWriteSize,
265 CurrBuffer
266 );
267 return Status;
268 } else {
269 Size = (UINT32) (LinearOffset + PtrBlockMapEntry->Length - CurrWritePtr);
270 Status = Fvb->Write (
271 Fvb,
272 LbaNumber,
273 (UINTN) (CurrWritePtr - LinearOffset),
274 &Size,
275 CurrBuffer
276 );
277 if (EFI_ERROR (Status)) {
278 return Status;
279 }
280
281 CurrWritePtr = LinearOffset + PtrBlockMapEntry->Length;
282 CurrBuffer = CurrBuffer + Size;
283 CurrWriteSize = CurrWriteSize - Size;
284 }
285 }
286
287 LinearOffset += PtrBlockMapEntry->Length;
288 LbaNumber++;
289 }
290 }
291
292 return EFI_SUCCESS;
293 }
294
295
296 /**
297
298 This code gets the current status of Variable Store.
299
300 @param VarStoreHeader Pointer to the Variable Store Header.
301
302 @retval EfiRaw Variable store status is raw.
303 @retval EfiValid Variable store status is valid.
304 @retval EfiInvalid Variable store status is invalid.
305
306 **/
307 VARIABLE_STORE_STATUS
308 GetVariableStoreStatus (
309 IN VARIABLE_STORE_HEADER *VarStoreHeader
310 )
311 {
312 if (CompareGuid (&VarStoreHeader->Signature, &gEfiVariableGuid) &&
313 VarStoreHeader->Format == VARIABLE_STORE_FORMATTED &&
314 VarStoreHeader->State == VARIABLE_STORE_HEALTHY
315 ) {
316
317 return EfiValid;
318 } else if (((UINT32 *)(&VarStoreHeader->Signature))[0] == 0xffffffff &&
319 ((UINT32 *)(&VarStoreHeader->Signature))[1] == 0xffffffff &&
320 ((UINT32 *)(&VarStoreHeader->Signature))[2] == 0xffffffff &&
321 ((UINT32 *)(&VarStoreHeader->Signature))[3] == 0xffffffff &&
322 VarStoreHeader->Size == 0xffffffff &&
323 VarStoreHeader->Format == 0xff &&
324 VarStoreHeader->State == 0xff
325 ) {
326
327 return EfiRaw;
328 } else {
329 return EfiInvalid;
330 }
331 }
332
333
334 /**
335
336 This code gets the size of name of variable.
337
338 @param Variable Pointer to the Variable Header.
339
340 @return UINTN Size of variable in bytes.
341
342 **/
343 UINTN
344 NameSizeOfVariable (
345 IN VARIABLE_HEADER *Variable
346 )
347 {
348 if (Variable->State == (UINT8) (-1) ||
349 Variable->DataSize == (UINT32) (-1) ||
350 Variable->NameSize == (UINT32) (-1) ||
351 Variable->Attributes == (UINT32) (-1)) {
352 return 0;
353 }
354 return (UINTN) Variable->NameSize;
355 }
356
357 /**
358
359 This code gets the size of variable data.
360
361 @param Variable Pointer to the Variable Header.
362
363 @return Size of variable in bytes.
364
365 **/
366 UINTN
367 DataSizeOfVariable (
368 IN VARIABLE_HEADER *Variable
369 )
370 {
371 if (Variable->State == (UINT8) (-1) ||
372 Variable->DataSize == (UINT32) (-1) ||
373 Variable->NameSize == (UINT32) (-1) ||
374 Variable->Attributes == (UINT32) (-1)) {
375 return 0;
376 }
377 return (UINTN) Variable->DataSize;
378 }
379
380 /**
381
382 This code gets the pointer to the variable name.
383
384 @param Variable Pointer to the Variable Header.
385
386 @return Pointer to Variable Name which is Unicode encoding.
387
388 **/
389 CHAR16 *
390 GetVariableNamePtr (
391 IN VARIABLE_HEADER *Variable
392 )
393 {
394
395 return (CHAR16 *) (Variable + 1);
396 }
397
398 /**
399
400 This code gets the pointer to the variable data.
401
402 @param Variable Pointer to the Variable Header.
403
404 @return Pointer to Variable Data.
405
406 **/
407 UINT8 *
408 GetVariableDataPtr (
409 IN VARIABLE_HEADER *Variable
410 )
411 {
412 UINTN Value;
413
414 //
415 // Be careful about pad size for alignment.
416 //
417 Value = (UINTN) GetVariableNamePtr (Variable);
418 Value += NameSizeOfVariable (Variable);
419 Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));
420
421 return (UINT8 *) Value;
422 }
423
424
425 /**
426
427 This code gets the pointer to the next variable header.
428
429 @param Variable Pointer to the Variable Header.
430
431 @return Pointer to next variable header.
432
433 **/
434 VARIABLE_HEADER *
435 GetNextVariablePtr (
436 IN VARIABLE_HEADER *Variable
437 )
438 {
439 UINTN Value;
440
441 if (!IsValidVariableHeader (Variable)) {
442 return NULL;
443 }
444
445 Value = (UINTN) GetVariableDataPtr (Variable);
446 Value += DataSizeOfVariable (Variable);
447 Value += GET_PAD_SIZE (DataSizeOfVariable (Variable));
448
449 //
450 // Be careful about pad size for alignment.
451 //
452 return (VARIABLE_HEADER *) HEADER_ALIGN (Value);
453 }
454
455 /**
456
457 Gets the pointer to the first variable header in given variable store area.
458
459 @param VarStoreHeader Pointer to the Variable Store Header.
460
461 @return Pointer to the first variable header.
462
463 **/
464 VARIABLE_HEADER *
465 GetStartPointer (
466 IN VARIABLE_STORE_HEADER *VarStoreHeader
467 )
468 {
469 //
470 // The end of variable store.
471 //
472 return (VARIABLE_HEADER *) HEADER_ALIGN (VarStoreHeader + 1);
473 }
474
475 /**
476
477 Gets the pointer to the end of the variable storage area.
478
479 This function gets pointer to the end of the variable storage
480 area, according to the input variable store header.
481
482 @param VarStoreHeader Pointer to the Variable Store Header.
483
484 @return Pointer to the end of the variable storage area.
485
486 **/
487 VARIABLE_HEADER *
488 GetEndPointer (
489 IN VARIABLE_STORE_HEADER *VarStoreHeader
490 )
491 {
492 //
493 // The end of variable store
494 //
495 return (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) VarStoreHeader + VarStoreHeader->Size);
496 }
497
498
499 /**
500
501 Variable store garbage collection and reclaim operation.
502
503 @param VariableBase Base address of variable store.
504 @param LastVariableOffset Offset of last variable.
505 @param IsVolatile The variable store is volatile or not;
506 if it is non-volatile, need FTW.
507 @param UpdatingVariable Pointer to updating variable.
508
509 @return EFI_OUT_OF_RESOURCES
510 @return EFI_SUCCESS
511 @return Others
512
513 **/
514 EFI_STATUS
515 Reclaim (
516 IN EFI_PHYSICAL_ADDRESS VariableBase,
517 OUT UINTN *LastVariableOffset,
518 IN BOOLEAN IsVolatile,
519 IN VARIABLE_HEADER *UpdatingVariable
520 )
521 {
522 VARIABLE_HEADER *Variable;
523 VARIABLE_HEADER *AddedVariable;
524 VARIABLE_HEADER *NextVariable;
525 VARIABLE_HEADER *NextAddedVariable;
526 VARIABLE_STORE_HEADER *VariableStoreHeader;
527 UINT8 *ValidBuffer;
528 UINTN MaximumBufferSize;
529 UINTN VariableSize;
530 UINTN VariableNameSize;
531 UINTN UpdatingVariableNameSize;
532 UINTN NameSize;
533 UINT8 *CurrPtr;
534 VOID *Point0;
535 VOID *Point1;
536 BOOLEAN FoundAdded;
537 EFI_STATUS Status;
538 CHAR16 *VariableNamePtr;
539 CHAR16 *UpdatingVariableNamePtr;
540
541 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) VariableBase);
542 //
543 // Recalculate the total size of Common/HwErr type variables in non-volatile area.
544 //
545 if (!IsVolatile) {
546 mVariableModuleGlobal->CommonVariableTotalSize = 0;
547 mVariableModuleGlobal->HwErrVariableTotalSize = 0;
548 }
549
550 //
551 // Start Pointers for the variable.
552 //
553 Variable = GetStartPointer (VariableStoreHeader);
554 MaximumBufferSize = sizeof (VARIABLE_STORE_HEADER);
555
556 while (IsValidVariableHeader (Variable)) {
557 NextVariable = GetNextVariablePtr (Variable);
558 if (Variable->State == VAR_ADDED ||
559 Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)
560 ) {
561 VariableSize = (UINTN) NextVariable - (UINTN) Variable;
562 MaximumBufferSize += VariableSize;
563 }
564
565 Variable = NextVariable;
566 }
567
568 //
569 // Reserve the 1 Bytes with Oxff to identify the
570 // end of the variable buffer.
571 //
572 MaximumBufferSize += 1;
573 ValidBuffer = AllocatePool (MaximumBufferSize);
574 if (ValidBuffer == NULL) {
575 return EFI_OUT_OF_RESOURCES;
576 }
577
578 SetMem (ValidBuffer, MaximumBufferSize, 0xff);
579
580 //
581 // Copy variable store header.
582 //
583 CopyMem (ValidBuffer, VariableStoreHeader, sizeof (VARIABLE_STORE_HEADER));
584 CurrPtr = (UINT8 *) GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer);
585
586 //
587 // Reinstall all ADDED variables as long as they are not identical to Updating Variable.
588 //
589 Variable = GetStartPointer (VariableStoreHeader);
590 while (IsValidVariableHeader (Variable)) {
591 NextVariable = GetNextVariablePtr (Variable);
592 if (Variable->State == VAR_ADDED) {
593 if (UpdatingVariable != NULL) {
594 if (UpdatingVariable == Variable) {
595 Variable = NextVariable;
596 continue;
597 }
598
599 VariableNameSize = NameSizeOfVariable(Variable);
600 UpdatingVariableNameSize = NameSizeOfVariable(UpdatingVariable);
601
602 VariableNamePtr = GetVariableNamePtr (Variable);
603 UpdatingVariableNamePtr = GetVariableNamePtr (UpdatingVariable);
604 if (CompareGuid (&Variable->VendorGuid, &UpdatingVariable->VendorGuid) &&
605 VariableNameSize == UpdatingVariableNameSize &&
606 CompareMem (VariableNamePtr, UpdatingVariableNamePtr, VariableNameSize) == 0 ) {
607 Variable = NextVariable;
608 continue;
609 }
610 }
611 VariableSize = (UINTN) NextVariable - (UINTN) Variable;
612 CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);
613 CurrPtr += VariableSize;
614 if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {
615 mVariableModuleGlobal->HwErrVariableTotalSize += VariableSize;
616 } else if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {
617 mVariableModuleGlobal->CommonVariableTotalSize += VariableSize;
618 }
619 }
620 Variable = NextVariable;
621 }
622
623 //
624 // Reinstall the variable being updated if it is not NULL.
625 //
626 if (UpdatingVariable != NULL) {
627 VariableSize = (UINTN)(GetNextVariablePtr (UpdatingVariable)) - (UINTN)UpdatingVariable;
628 CopyMem (CurrPtr, (UINT8 *) UpdatingVariable, VariableSize);
629 CurrPtr += VariableSize;
630 if ((!IsVolatile) && ((UpdatingVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {
631 mVariableModuleGlobal->HwErrVariableTotalSize += VariableSize;
632 } else if ((!IsVolatile) && ((UpdatingVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {
633 mVariableModuleGlobal->CommonVariableTotalSize += VariableSize;
634 }
635 }
636
637 //
638 // Reinstall all in delete transition variables.
639 //
640 Variable = GetStartPointer (VariableStoreHeader);
641 while (IsValidVariableHeader (Variable)) {
642 NextVariable = GetNextVariablePtr (Variable);
643 if (Variable != UpdatingVariable && Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
644
645 //
646 // Buffer has cached all ADDED variable.
647 // Per IN_DELETED variable, we have to guarantee that
648 // no ADDED one in previous buffer.
649 //
650
651 FoundAdded = FALSE;
652 AddedVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer);
653 while (IsValidVariableHeader (AddedVariable)) {
654 NextAddedVariable = GetNextVariablePtr (AddedVariable);
655 NameSize = NameSizeOfVariable (AddedVariable);
656 if (CompareGuid (&AddedVariable->VendorGuid, &Variable->VendorGuid) &&
657 NameSize == NameSizeOfVariable (Variable)
658 ) {
659 Point0 = (VOID *) GetVariableNamePtr (AddedVariable);
660 Point1 = (VOID *) GetVariableNamePtr (Variable);
661 if (CompareMem (Point0, Point1, NameSizeOfVariable (AddedVariable)) == 0) {
662 FoundAdded = TRUE;
663 break;
664 }
665 }
666 AddedVariable = NextAddedVariable;
667 }
668 if (!FoundAdded) {
669 //
670 // Promote VAR_IN_DELETED_TRANSITION to VAR_ADDED.
671 //
672 VariableSize = (UINTN) NextVariable - (UINTN) Variable;
673 CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);
674 ((VARIABLE_HEADER *) CurrPtr)->State = VAR_ADDED;
675 CurrPtr += VariableSize;
676 if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {
677 mVariableModuleGlobal->HwErrVariableTotalSize += VariableSize;
678 } else if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {
679 mVariableModuleGlobal->CommonVariableTotalSize += VariableSize;
680 }
681 }
682 }
683
684 Variable = NextVariable;
685 }
686
687 if (IsVolatile) {
688 //
689 // If volatile variable store, just copy valid buffer.
690 //
691 SetMem ((UINT8 *) (UINTN) VariableBase, VariableStoreHeader->Size, 0xff);
692 CopyMem ((UINT8 *) (UINTN) VariableBase, ValidBuffer, (UINTN) (CurrPtr - (UINT8 *) ValidBuffer));
693 Status = EFI_SUCCESS;
694 } else {
695 //
696 // If non-volatile variable store, perform FTW here.
697 //
698 Status = FtwVariableSpace (
699 VariableBase,
700 ValidBuffer,
701 (UINTN) (CurrPtr - (UINT8 *) ValidBuffer)
702 );
703 CopyMem (mNvVariableCache, (CHAR8 *)(UINTN)VariableBase, VariableStoreHeader->Size);
704 }
705 if (!EFI_ERROR (Status)) {
706 *LastVariableOffset = (UINTN) (CurrPtr - (UINT8 *) ValidBuffer);
707 } else {
708 *LastVariableOffset = 0;
709 }
710
711 FreePool (ValidBuffer);
712
713 return Status;
714 }
715
716 /**
717 Find the variable in the specified variable store.
718
719 @param VariableName Name of the variable to be found
720 @param VendorGuid Vendor GUID to be found.
721 @param IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
722 check at runtime when searching variable.
723 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
724
725 @retval EFI_SUCCESS Variable found successfully
726 @retval EFI_NOT_FOUND Variable not found
727 **/
728 EFI_STATUS
729 FindVariableEx (
730 IN CHAR16 *VariableName,
731 IN EFI_GUID *VendorGuid,
732 IN BOOLEAN IgnoreRtCheck,
733 IN OUT VARIABLE_POINTER_TRACK *PtrTrack
734 )
735 {
736 VARIABLE_HEADER *InDeletedVariable;
737 VOID *Point;
738
739 //
740 // Find the variable by walk through HOB, volatile and non-volatile variable store.
741 //
742 InDeletedVariable = NULL;
743
744 for ( PtrTrack->CurrPtr = PtrTrack->StartPtr
745 ; (PtrTrack->CurrPtr < PtrTrack->EndPtr) && IsValidVariableHeader (PtrTrack->CurrPtr)
746 ; PtrTrack->CurrPtr = GetNextVariablePtr (PtrTrack->CurrPtr)
747 ) {
748 if (PtrTrack->CurrPtr->State == VAR_ADDED ||
749 PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)
750 ) {
751 if (IgnoreRtCheck || !AtRuntime () || ((PtrTrack->CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {
752 if (VariableName[0] == 0) {
753 if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
754 InDeletedVariable = PtrTrack->CurrPtr;
755 } else {
756 return EFI_SUCCESS;
757 }
758 } else {
759 if (CompareGuid (VendorGuid, &PtrTrack->CurrPtr->VendorGuid)) {
760 Point = (VOID *) GetVariableNamePtr (PtrTrack->CurrPtr);
761
762 ASSERT (NameSizeOfVariable (PtrTrack->CurrPtr) != 0);
763 if (CompareMem (VariableName, Point, NameSizeOfVariable (PtrTrack->CurrPtr)) == 0) {
764 if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
765 InDeletedVariable = PtrTrack->CurrPtr;
766 } else {
767 return EFI_SUCCESS;
768 }
769 }
770 }
771 }
772 }
773 }
774 }
775
776 PtrTrack->CurrPtr = InDeletedVariable;
777 return (PtrTrack->CurrPtr == NULL) ? EFI_NOT_FOUND : EFI_SUCCESS;
778 }
779
780
781 /**
782 Finds variable in storage blocks of volatile and non-volatile storage areas.
783
784 This code finds variable in storage blocks of volatile and non-volatile storage areas.
785 If VariableName is an empty string, then we just return the first
786 qualified variable without comparing VariableName and VendorGuid.
787 If IgnoreRtCheck is TRUE, then we ignore the EFI_VARIABLE_RUNTIME_ACCESS attribute check
788 at runtime when searching existing variable, only VariableName and VendorGuid are compared.
789 Otherwise, variables without EFI_VARIABLE_RUNTIME_ACCESS are not visible at runtime.
790
791 @param VariableName Name of the variable to be found.
792 @param VendorGuid Vendor GUID to be found.
793 @param PtrTrack VARIABLE_POINTER_TRACK structure for output,
794 including the range searched and the target position.
795 @param Global Pointer to VARIABLE_GLOBAL structure, including
796 base of volatile variable storage area, base of
797 NV variable storage area, and a lock.
798 @param IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
799 check at runtime when searching variable.
800
801 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while
802 VendorGuid is NULL.
803 @retval EFI_SUCCESS Variable successfully found.
804 @retval EFI_NOT_FOUND Variable not found
805
806 **/
807 EFI_STATUS
808 FindVariable (
809 IN CHAR16 *VariableName,
810 IN EFI_GUID *VendorGuid,
811 OUT VARIABLE_POINTER_TRACK *PtrTrack,
812 IN VARIABLE_GLOBAL *Global,
813 IN BOOLEAN IgnoreRtCheck
814 )
815 {
816 EFI_STATUS Status;
817 VARIABLE_STORE_HEADER *VariableStoreHeader[VariableStoreTypeMax];
818 VARIABLE_STORE_TYPE Type;
819
820 if (VariableName[0] != 0 && VendorGuid == NULL) {
821 return EFI_INVALID_PARAMETER;
822 }
823
824 //
825 // 0: Volatile, 1: HOB, 2: Non-Volatile.
826 // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName
827 // make use of this mapping to implement search algorithm.
828 //
829 VariableStoreHeader[VariableStoreTypeVolatile] = (VARIABLE_STORE_HEADER *) (UINTN) Global->VolatileVariableBase;
830 VariableStoreHeader[VariableStoreTypeHob] = (VARIABLE_STORE_HEADER *) (UINTN) Global->HobVariableBase;
831 VariableStoreHeader[VariableStoreTypeNv] = mNvVariableCache;
832
833 //
834 // Find the variable by walk through HOB, volatile and non-volatile variable store.
835 //
836 for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {
837 if (VariableStoreHeader[Type] == NULL) {
838 continue;
839 }
840
841 PtrTrack->StartPtr = GetStartPointer (VariableStoreHeader[Type]);
842 PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader[Type]);
843 PtrTrack->Volatile = (BOOLEAN) (Type == VariableStoreTypeVolatile);
844
845 Status = FindVariableEx (VariableName, VendorGuid, IgnoreRtCheck, PtrTrack);
846 if (!EFI_ERROR (Status)) {
847 return Status;
848 }
849 }
850 return EFI_NOT_FOUND;
851 }
852
853 /**
854 Get index from supported language codes according to language string.
855
856 This code is used to get corresponding index in supported language codes. It can handle
857 RFC4646 and ISO639 language tags.
858 In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.
859 In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.
860
861 For example:
862 SupportedLang = "engfraengfra"
863 Lang = "eng"
864 Iso639Language = TRUE
865 The return value is "0".
866 Another example:
867 SupportedLang = "en;fr;en-US;fr-FR"
868 Lang = "fr-FR"
869 Iso639Language = FALSE
870 The return value is "3".
871
872 @param SupportedLang Platform supported language codes.
873 @param Lang Configured language.
874 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
875
876 @retval The index of language in the language codes.
877
878 **/
879 UINTN
880 GetIndexFromSupportedLangCodes(
881 IN CHAR8 *SupportedLang,
882 IN CHAR8 *Lang,
883 IN BOOLEAN Iso639Language
884 )
885 {
886 UINTN Index;
887 UINTN CompareLength;
888 UINTN LanguageLength;
889
890 if (Iso639Language) {
891 CompareLength = ISO_639_2_ENTRY_SIZE;
892 for (Index = 0; Index < AsciiStrLen (SupportedLang); Index += CompareLength) {
893 if (AsciiStrnCmp (Lang, SupportedLang + Index, CompareLength) == 0) {
894 //
895 // Successfully find the index of Lang string in SupportedLang string.
896 //
897 Index = Index / CompareLength;
898 return Index;
899 }
900 }
901 ASSERT (FALSE);
902 return 0;
903 } else {
904 //
905 // Compare RFC4646 language code
906 //
907 Index = 0;
908 for (LanguageLength = 0; Lang[LanguageLength] != '\0'; LanguageLength++);
909
910 for (Index = 0; *SupportedLang != '\0'; Index++, SupportedLang += CompareLength) {
911 //
912 // Skip ';' characters in SupportedLang
913 //
914 for (; *SupportedLang != '\0' && *SupportedLang == ';'; SupportedLang++);
915 //
916 // Determine the length of the next language code in SupportedLang
917 //
918 for (CompareLength = 0; SupportedLang[CompareLength] != '\0' && SupportedLang[CompareLength] != ';'; CompareLength++);
919
920 if ((CompareLength == LanguageLength) &&
921 (AsciiStrnCmp (Lang, SupportedLang, CompareLength) == 0)) {
922 //
923 // Successfully find the index of Lang string in SupportedLang string.
924 //
925 return Index;
926 }
927 }
928 ASSERT (FALSE);
929 return 0;
930 }
931 }
932
933 /**
934 Get language string from supported language codes according to index.
935
936 This code is used to get corresponding language strings in supported language codes. It can handle
937 RFC4646 and ISO639 language tags.
938 In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.
939 In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.
940
941 For example:
942 SupportedLang = "engfraengfra"
943 Index = "1"
944 Iso639Language = TRUE
945 The return value is "fra".
946 Another example:
947 SupportedLang = "en;fr;en-US;fr-FR"
948 Index = "1"
949 Iso639Language = FALSE
950 The return value is "fr".
951
952 @param SupportedLang Platform supported language codes.
953 @param Index The index in supported language codes.
954 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
955
956 @retval The language string in the language codes.
957
958 **/
959 CHAR8 *
960 GetLangFromSupportedLangCodes (
961 IN CHAR8 *SupportedLang,
962 IN UINTN Index,
963 IN BOOLEAN Iso639Language
964 )
965 {
966 UINTN SubIndex;
967 UINTN CompareLength;
968 CHAR8 *Supported;
969
970 SubIndex = 0;
971 Supported = SupportedLang;
972 if (Iso639Language) {
973 //
974 // According to the index of Lang string in SupportedLang string to get the language.
975 // This code will be invoked in RUNTIME, therefore there is not a memory allocate/free operation.
976 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
977 //
978 CompareLength = ISO_639_2_ENTRY_SIZE;
979 mVariableModuleGlobal->Lang[CompareLength] = '\0';
980 return CopyMem (mVariableModuleGlobal->Lang, SupportedLang + Index * CompareLength, CompareLength);
981
982 } else {
983 while (TRUE) {
984 //
985 // Take semicolon as delimitation, sequentially traverse supported language codes.
986 //
987 for (CompareLength = 0; *Supported != ';' && *Supported != '\0'; CompareLength++) {
988 Supported++;
989 }
990 if ((*Supported == '\0') && (SubIndex != Index)) {
991 //
992 // Have completed the traverse, but not find corrsponding string.
993 // This case is not allowed to happen.
994 //
995 ASSERT(FALSE);
996 return NULL;
997 }
998 if (SubIndex == Index) {
999 //
1000 // According to the index of Lang string in SupportedLang string to get the language.
1001 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.
1002 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
1003 //
1004 mVariableModuleGlobal->PlatformLang[CompareLength] = '\0';
1005 return CopyMem (mVariableModuleGlobal->PlatformLang, Supported - CompareLength, CompareLength);
1006 }
1007 SubIndex++;
1008
1009 //
1010 // Skip ';' characters in Supported
1011 //
1012 for (; *Supported != '\0' && *Supported == ';'; Supported++);
1013 }
1014 }
1015 }
1016
1017 /**
1018 Returns a pointer to an allocated buffer that contains the best matching language
1019 from a set of supported languages.
1020
1021 This function supports both ISO 639-2 and RFC 4646 language codes, but language
1022 code types may not be mixed in a single call to this function. This function
1023 supports a variable argument list that allows the caller to pass in a prioritized
1024 list of language codes to test against all the language codes in SupportedLanguages.
1025
1026 If SupportedLanguages is NULL, then ASSERT().
1027
1028 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that
1029 contains a set of language codes in the format
1030 specified by Iso639Language.
1031 @param[in] Iso639Language If TRUE, then all language codes are assumed to be
1032 in ISO 639-2 format. If FALSE, then all language
1033 codes are assumed to be in RFC 4646 language format
1034 @param[in] ... A variable argument list that contains pointers to
1035 Null-terminated ASCII strings that contain one or more
1036 language codes in the format specified by Iso639Language.
1037 The first language code from each of these language
1038 code lists is used to determine if it is an exact or
1039 close match to any of the language codes in
1040 SupportedLanguages. Close matches only apply to RFC 4646
1041 language codes, and the matching algorithm from RFC 4647
1042 is used to determine if a close match is present. If
1043 an exact or close match is found, then the matching
1044 language code from SupportedLanguages is returned. If
1045 no matches are found, then the next variable argument
1046 parameter is evaluated. The variable argument list
1047 is terminated by a NULL.
1048
1049 @retval NULL The best matching language could not be found in SupportedLanguages.
1050 @retval NULL There are not enough resources available to return the best matching
1051 language.
1052 @retval Other A pointer to a Null-terminated ASCII string that is the best matching
1053 language in SupportedLanguages.
1054
1055 **/
1056 CHAR8 *
1057 EFIAPI
1058 VariableGetBestLanguage (
1059 IN CONST CHAR8 *SupportedLanguages,
1060 IN BOOLEAN Iso639Language,
1061 ...
1062 )
1063 {
1064 VA_LIST Args;
1065 CHAR8 *Language;
1066 UINTN CompareLength;
1067 UINTN LanguageLength;
1068 CONST CHAR8 *Supported;
1069 CHAR8 *Buffer;
1070
1071 ASSERT (SupportedLanguages != NULL);
1072
1073 VA_START (Args, Iso639Language);
1074 while ((Language = VA_ARG (Args, CHAR8 *)) != NULL) {
1075 //
1076 // Default to ISO 639-2 mode
1077 //
1078 CompareLength = 3;
1079 LanguageLength = MIN (3, AsciiStrLen (Language));
1080
1081 //
1082 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language
1083 //
1084 if (!Iso639Language) {
1085 for (LanguageLength = 0; Language[LanguageLength] != 0 && Language[LanguageLength] != ';'; LanguageLength++);
1086 }
1087
1088 //
1089 // Trim back the length of Language used until it is empty
1090 //
1091 while (LanguageLength > 0) {
1092 //
1093 // Loop through all language codes in SupportedLanguages
1094 //
1095 for (Supported = SupportedLanguages; *Supported != '\0'; Supported += CompareLength) {
1096 //
1097 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages
1098 //
1099 if (!Iso639Language) {
1100 //
1101 // Skip ';' characters in Supported
1102 //
1103 for (; *Supported != '\0' && *Supported == ';'; Supported++);
1104 //
1105 // Determine the length of the next language code in Supported
1106 //
1107 for (CompareLength = 0; Supported[CompareLength] != 0 && Supported[CompareLength] != ';'; CompareLength++);
1108 //
1109 // If Language is longer than the Supported, then skip to the next language
1110 //
1111 if (LanguageLength > CompareLength) {
1112 continue;
1113 }
1114 }
1115 //
1116 // See if the first LanguageLength characters in Supported match Language
1117 //
1118 if (AsciiStrnCmp (Supported, Language, LanguageLength) == 0) {
1119 VA_END (Args);
1120
1121 Buffer = Iso639Language ? mVariableModuleGlobal->Lang : mVariableModuleGlobal->PlatformLang;
1122 Buffer[CompareLength] = '\0';
1123 return CopyMem (Buffer, Supported, CompareLength);
1124 }
1125 }
1126
1127 if (Iso639Language) {
1128 //
1129 // If ISO 639 mode, then each language can only be tested once
1130 //
1131 LanguageLength = 0;
1132 } else {
1133 //
1134 // If RFC 4646 mode, then trim Language from the right to the next '-' character
1135 //
1136 for (LanguageLength--; LanguageLength > 0 && Language[LanguageLength] != '-'; LanguageLength--);
1137 }
1138 }
1139 }
1140 VA_END (Args);
1141
1142 //
1143 // No matches were found
1144 //
1145 return NULL;
1146 }
1147
1148 /**
1149 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.
1150
1151 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.
1152
1153 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,
1154 and are read-only. Therefore, in variable driver, only store the original value for other use.
1155
1156 @param[in] VariableName Name of variable.
1157
1158 @param[in] Data Variable data.
1159
1160 @param[in] DataSize Size of data. 0 means delete.
1161
1162 **/
1163 VOID
1164 AutoUpdateLangVariable (
1165 IN CHAR16 *VariableName,
1166 IN VOID *Data,
1167 IN UINTN DataSize
1168 )
1169 {
1170 EFI_STATUS Status;
1171 CHAR8 *BestPlatformLang;
1172 CHAR8 *BestLang;
1173 UINTN Index;
1174 UINT32 Attributes;
1175 VARIABLE_POINTER_TRACK Variable;
1176 BOOLEAN SetLanguageCodes;
1177
1178 //
1179 // Don't do updates for delete operation
1180 //
1181 if (DataSize == 0) {
1182 return;
1183 }
1184
1185 SetLanguageCodes = FALSE;
1186
1187 if (StrCmp (VariableName, L"PlatformLangCodes") == 0) {
1188 //
1189 // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.
1190 //
1191 if (AtRuntime ()) {
1192 return;
1193 }
1194
1195 SetLanguageCodes = TRUE;
1196
1197 //
1198 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only
1199 // Therefore, in variable driver, only store the original value for other use.
1200 //
1201 if (mVariableModuleGlobal->PlatformLangCodes != NULL) {
1202 FreePool (mVariableModuleGlobal->PlatformLangCodes);
1203 }
1204 mVariableModuleGlobal->PlatformLangCodes = AllocateRuntimeCopyPool (DataSize, Data);
1205 ASSERT (mVariableModuleGlobal->PlatformLangCodes != NULL);
1206
1207 //
1208 // PlatformLang holds a single language from PlatformLangCodes,
1209 // so the size of PlatformLangCodes is enough for the PlatformLang.
1210 //
1211 if (mVariableModuleGlobal->PlatformLang != NULL) {
1212 FreePool (mVariableModuleGlobal->PlatformLang);
1213 }
1214 mVariableModuleGlobal->PlatformLang = AllocateRuntimePool (DataSize);
1215 ASSERT (mVariableModuleGlobal->PlatformLang != NULL);
1216
1217 } else if (StrCmp (VariableName, L"LangCodes") == 0) {
1218 //
1219 // LangCodes is a volatile variable, so it can not be updated at runtime.
1220 //
1221 if (AtRuntime ()) {
1222 return;
1223 }
1224
1225 SetLanguageCodes = TRUE;
1226
1227 //
1228 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only
1229 // Therefore, in variable driver, only store the original value for other use.
1230 //
1231 if (mVariableModuleGlobal->LangCodes != NULL) {
1232 FreePool (mVariableModuleGlobal->LangCodes);
1233 }
1234 mVariableModuleGlobal->LangCodes = AllocateRuntimeCopyPool (DataSize, Data);
1235 ASSERT (mVariableModuleGlobal->LangCodes != NULL);
1236 }
1237
1238 if (SetLanguageCodes
1239 && (mVariableModuleGlobal->PlatformLangCodes != NULL)
1240 && (mVariableModuleGlobal->LangCodes != NULL)) {
1241 //
1242 // Update Lang if PlatformLang is already set
1243 // Update PlatformLang if Lang is already set
1244 //
1245 Status = FindVariable (L"PlatformLang", &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);
1246 if (!EFI_ERROR (Status)) {
1247 //
1248 // Update Lang
1249 //
1250 VariableName = L"PlatformLang";
1251 Data = GetVariableDataPtr (Variable.CurrPtr);
1252 DataSize = Variable.CurrPtr->DataSize;
1253 } else {
1254 Status = FindVariable (L"Lang", &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);
1255 if (!EFI_ERROR (Status)) {
1256 //
1257 // Update PlatformLang
1258 //
1259 VariableName = L"Lang";
1260 Data = GetVariableDataPtr (Variable.CurrPtr);
1261 DataSize = Variable.CurrPtr->DataSize;
1262 } else {
1263 //
1264 // Neither PlatformLang nor Lang is set, directly return
1265 //
1266 return;
1267 }
1268 }
1269 }
1270
1271 //
1272 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.
1273 //
1274 Attributes = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;
1275
1276 if (StrCmp (VariableName, L"PlatformLang") == 0) {
1277 //
1278 // Update Lang when PlatformLangCodes/LangCodes were set.
1279 //
1280 if ((mVariableModuleGlobal->PlatformLangCodes != NULL) && (mVariableModuleGlobal->LangCodes != NULL)) {
1281 //
1282 // When setting PlatformLang, firstly get most matched language string from supported language codes.
1283 //
1284 BestPlatformLang = VariableGetBestLanguage (mVariableModuleGlobal->PlatformLangCodes, FALSE, Data, NULL);
1285 if (BestPlatformLang != NULL) {
1286 //
1287 // Get the corresponding index in language codes.
1288 //
1289 Index = GetIndexFromSupportedLangCodes (mVariableModuleGlobal->PlatformLangCodes, BestPlatformLang, FALSE);
1290
1291 //
1292 // Get the corresponding ISO639 language tag according to RFC4646 language tag.
1293 //
1294 BestLang = GetLangFromSupportedLangCodes (mVariableModuleGlobal->LangCodes, Index, TRUE);
1295
1296 //
1297 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.
1298 //
1299 FindVariable (L"Lang", &gEfiGlobalVariableGuid, &Variable, (VARIABLE_GLOBAL *)mVariableModuleGlobal, FALSE);
1300
1301 Status = UpdateVariable (L"Lang", &gEfiGlobalVariableGuid, BestLang,
1302 ISO_639_2_ENTRY_SIZE + 1, Attributes, &Variable);
1303
1304 DEBUG ((EFI_D_INFO, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a\n", BestPlatformLang, BestLang));
1305
1306 ASSERT_EFI_ERROR(Status);
1307 }
1308 }
1309
1310 } else if (StrCmp (VariableName, L"Lang") == 0) {
1311 //
1312 // Update PlatformLang when PlatformLangCodes/LangCodes were set.
1313 //
1314 if ((mVariableModuleGlobal->PlatformLangCodes != NULL) && (mVariableModuleGlobal->LangCodes != NULL)) {
1315 //
1316 // When setting Lang, firstly get most matched language string from supported language codes.
1317 //
1318 BestLang = VariableGetBestLanguage (mVariableModuleGlobal->LangCodes, TRUE, Data, NULL);
1319 if (BestLang != NULL) {
1320 //
1321 // Get the corresponding index in language codes.
1322 //
1323 Index = GetIndexFromSupportedLangCodes (mVariableModuleGlobal->LangCodes, BestLang, TRUE);
1324
1325 //
1326 // Get the corresponding RFC4646 language tag according to ISO639 language tag.
1327 //
1328 BestPlatformLang = GetLangFromSupportedLangCodes (mVariableModuleGlobal->PlatformLangCodes, Index, FALSE);
1329
1330 //
1331 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.
1332 //
1333 FindVariable (L"PlatformLang", &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);
1334
1335 Status = UpdateVariable (L"PlatformLang", &gEfiGlobalVariableGuid, BestPlatformLang,
1336 AsciiStrSize (BestPlatformLang), Attributes, &Variable);
1337
1338 DEBUG ((EFI_D_INFO, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a\n", BestLang, BestPlatformLang));
1339 ASSERT_EFI_ERROR (Status);
1340 }
1341 }
1342 }
1343 }
1344
1345 /**
1346 Update the variable region with Variable information. These are the same
1347 arguments as the EFI Variable services.
1348
1349 @param[in] VariableName Name of variable.
1350 @param[in] VendorGuid Guid of variable.
1351 @param[in] Data Variable data.
1352 @param[in] DataSize Size of data. 0 means delete.
1353 @param[in] Attributes Attribues of the variable.
1354 @param[in] CacheVariable The variable information which is used to keep track of variable usage.
1355
1356 @retval EFI_SUCCESS The update operation is success.
1357 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.
1358
1359 **/
1360 EFI_STATUS
1361 UpdateVariable (
1362 IN CHAR16 *VariableName,
1363 IN EFI_GUID *VendorGuid,
1364 IN VOID *Data,
1365 IN UINTN DataSize,
1366 IN UINT32 Attributes OPTIONAL,
1367 IN VARIABLE_POINTER_TRACK *CacheVariable
1368 )
1369 {
1370 EFI_STATUS Status;
1371 VARIABLE_HEADER *NextVariable;
1372 UINTN ScratchSize;
1373 UINTN NonVolatileVarableStoreSize;
1374 UINTN VarNameOffset;
1375 UINTN VarDataOffset;
1376 UINTN VarNameSize;
1377 UINTN VarSize;
1378 BOOLEAN Volatile;
1379 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;
1380 UINT8 State;
1381 BOOLEAN Reclaimed;
1382 VARIABLE_POINTER_TRACK *Variable;
1383 VARIABLE_POINTER_TRACK NvVariable;
1384 VARIABLE_STORE_HEADER *VariableStoreHeader;
1385 UINTN CacheOffset;
1386
1387 if ((mVariableModuleGlobal->FvbInstance == NULL) && ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0)) {
1388 //
1389 // The FVB protocol is not ready. Trying to update NV variable prior to the installation
1390 // of EFI_VARIABLE_WRITE_ARCH_PROTOCOL.
1391 //
1392 return EFI_NOT_AVAILABLE_YET;
1393 }
1394
1395 if ((CacheVariable->CurrPtr == NULL) || CacheVariable->Volatile) {
1396 Variable = CacheVariable;
1397 } else {
1398 //
1399 // Update/Delete existing NV variable.
1400 // CacheVariable points to the variable in the memory copy of Flash area
1401 // Now let Variable points to the same variable in Flash area.
1402 //
1403 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);
1404 Variable = &NvVariable;
1405 Variable->StartPtr = GetStartPointer (VariableStoreHeader);
1406 Variable->EndPtr = GetEndPointer (VariableStoreHeader);
1407 Variable->CurrPtr = (VARIABLE_HEADER *)((UINTN)Variable->StartPtr + ((UINTN)CacheVariable->CurrPtr - (UINTN)CacheVariable->StartPtr));
1408 Variable->Volatile = FALSE;
1409 }
1410
1411 Fvb = mVariableModuleGlobal->FvbInstance;
1412 Reclaimed = FALSE;
1413
1414 if (Variable->CurrPtr != NULL) {
1415 //
1416 // Update/Delete existing variable.
1417 //
1418 if (AtRuntime ()) {
1419 //
1420 // If AtRuntime and the variable is Volatile and Runtime Access,
1421 // the volatile is ReadOnly, and SetVariable should be aborted and
1422 // return EFI_WRITE_PROTECTED.
1423 //
1424 if (Variable->Volatile) {
1425 Status = EFI_WRITE_PROTECTED;
1426 goto Done;
1427 }
1428 //
1429 // Only variable that have NV|RT attributes can be updated/deleted in Runtime.
1430 //
1431 if (((Variable->CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) || ((Variable->CurrPtr->Attributes & EFI_VARIABLE_NON_VOLATILE) == 0)) {
1432 Status = EFI_INVALID_PARAMETER;
1433 goto Done;
1434 }
1435 }
1436
1437 //
1438 // Setting a data variable with no access, or zero DataSize attributes
1439 // causes it to be deleted.
1440 //
1441 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {
1442 State = Variable->CurrPtr->State;
1443 State &= VAR_DELETED;
1444
1445 Status = UpdateVariableStore (
1446 &mVariableModuleGlobal->VariableGlobal,
1447 Variable->Volatile,
1448 FALSE,
1449 Fvb,
1450 (UINTN) &Variable->CurrPtr->State,
1451 sizeof (UINT8),
1452 &State
1453 );
1454 if (!EFI_ERROR (Status)) {
1455 UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, FALSE, TRUE, FALSE);
1456 if (!Variable->Volatile) {
1457 CacheVariable->CurrPtr->State = State;
1458 }
1459 }
1460 goto Done;
1461 }
1462 //
1463 // If the variable is marked valid, and the same data has been passed in,
1464 // then return to the caller immediately.
1465 //
1466 if (DataSizeOfVariable (Variable->CurrPtr) == DataSize &&
1467 (CompareMem (Data, GetVariableDataPtr (Variable->CurrPtr), DataSize) == 0)) {
1468
1469 UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, TRUE, FALSE, FALSE);
1470 Status = EFI_SUCCESS;
1471 goto Done;
1472 } else if ((Variable->CurrPtr->State == VAR_ADDED) ||
1473 (Variable->CurrPtr->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {
1474
1475 //
1476 // Mark the old variable as in delete transition.
1477 //
1478 State = Variable->CurrPtr->State;
1479 State &= VAR_IN_DELETED_TRANSITION;
1480
1481 Status = UpdateVariableStore (
1482 &mVariableModuleGlobal->VariableGlobal,
1483 Variable->Volatile,
1484 FALSE,
1485 Fvb,
1486 (UINTN) &Variable->CurrPtr->State,
1487 sizeof (UINT8),
1488 &State
1489 );
1490 if (EFI_ERROR (Status)) {
1491 goto Done;
1492 }
1493 if (!Variable->Volatile) {
1494 CacheVariable->CurrPtr->State = State;
1495 }
1496 }
1497 } else {
1498 //
1499 // Not found existing variable. Create a new variable.
1500 //
1501
1502 //
1503 // Make sure we are trying to create a new variable.
1504 // Setting a data variable with zero DataSize or no access attributes means to delete it.
1505 //
1506 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {
1507 Status = EFI_NOT_FOUND;
1508 goto Done;
1509 }
1510
1511 //
1512 // Only variable have NV|RT attribute can be created in Runtime.
1513 //
1514 if (AtRuntime () &&
1515 (((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) || ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0))) {
1516 Status = EFI_INVALID_PARAMETER;
1517 goto Done;
1518 }
1519 }
1520
1521 //
1522 // Function part - create a new variable and copy the data.
1523 // Both update a variable and create a variable will come here.
1524
1525 //
1526 // Tricky part: Use scratch data area at the end of volatile variable store
1527 // as a temporary storage.
1528 //
1529 NextVariable = GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase));
1530 ScratchSize = MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize));
1531
1532 SetMem (NextVariable, ScratchSize, 0xff);
1533
1534 NextVariable->StartId = VARIABLE_DATA;
1535 NextVariable->Attributes = Attributes;
1536 //
1537 // NextVariable->State = VAR_ADDED;
1538 //
1539 NextVariable->Reserved = 0;
1540 VarNameOffset = sizeof (VARIABLE_HEADER);
1541 VarNameSize = StrSize (VariableName);
1542 CopyMem (
1543 (UINT8 *) ((UINTN) NextVariable + VarNameOffset),
1544 VariableName,
1545 VarNameSize
1546 );
1547 VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);
1548 CopyMem (
1549 (UINT8 *) ((UINTN) NextVariable + VarDataOffset),
1550 Data,
1551 DataSize
1552 );
1553 CopyMem (&NextVariable->VendorGuid, VendorGuid, sizeof (EFI_GUID));
1554 //
1555 // There will be pad bytes after Data, the NextVariable->NameSize and
1556 // NextVariable->DataSize should not include pad size so that variable
1557 // service can get actual size in GetVariable.
1558 //
1559 NextVariable->NameSize = (UINT32)VarNameSize;
1560 NextVariable->DataSize = (UINT32)DataSize;
1561
1562 //
1563 // The actual size of the variable that stores in storage should
1564 // include pad size.
1565 //
1566 VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);
1567 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {
1568 //
1569 // Create a nonvolatile variable.
1570 //
1571 Volatile = FALSE;
1572 NonVolatileVarableStoreSize = ((VARIABLE_STORE_HEADER *)(UINTN)(mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase))->Size;
1573 if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0)
1574 && ((VarSize + mVariableModuleGlobal->HwErrVariableTotalSize) > PcdGet32 (PcdHwErrStorageSize)))
1575 || (((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0)
1576 && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > NonVolatileVarableStoreSize - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize)))) {
1577 if (AtRuntime ()) {
1578 Status = EFI_OUT_OF_RESOURCES;
1579 goto Done;
1580 }
1581 //
1582 // Perform garbage collection & reclaim operation.
1583 //
1584 Status = Reclaim (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,
1585 &mVariableModuleGlobal->NonVolatileLastVariableOffset, FALSE, Variable->CurrPtr);
1586 if (EFI_ERROR (Status)) {
1587 goto Done;
1588 }
1589 //
1590 // If still no enough space, return out of resources.
1591 //
1592 if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0)
1593 && ((VarSize + mVariableModuleGlobal->HwErrVariableTotalSize) > PcdGet32 (PcdHwErrStorageSize)))
1594 || (((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0)
1595 && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > NonVolatileVarableStoreSize - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize)))) {
1596 Status = EFI_OUT_OF_RESOURCES;
1597 goto Done;
1598 }
1599 Reclaimed = TRUE;
1600 }
1601 //
1602 // Four steps
1603 // 1. Write variable header
1604 // 2. Set variable state to header valid
1605 // 3. Write variable data
1606 // 4. Set variable state to valid
1607 //
1608 //
1609 // Step 1:
1610 //
1611 CacheOffset = mVariableModuleGlobal->NonVolatileLastVariableOffset;
1612 Status = UpdateVariableStore (
1613 &mVariableModuleGlobal->VariableGlobal,
1614 FALSE,
1615 TRUE,
1616 Fvb,
1617 mVariableModuleGlobal->NonVolatileLastVariableOffset,
1618 sizeof (VARIABLE_HEADER),
1619 (UINT8 *) NextVariable
1620 );
1621
1622 if (EFI_ERROR (Status)) {
1623 goto Done;
1624 }
1625
1626 //
1627 // Step 2:
1628 //
1629 NextVariable->State = VAR_HEADER_VALID_ONLY;
1630 Status = UpdateVariableStore (
1631 &mVariableModuleGlobal->VariableGlobal,
1632 FALSE,
1633 TRUE,
1634 Fvb,
1635 mVariableModuleGlobal->NonVolatileLastVariableOffset + OFFSET_OF (VARIABLE_HEADER, State),
1636 sizeof (UINT8),
1637 &NextVariable->State
1638 );
1639
1640 if (EFI_ERROR (Status)) {
1641 goto Done;
1642 }
1643 //
1644 // Step 3:
1645 //
1646 Status = UpdateVariableStore (
1647 &mVariableModuleGlobal->VariableGlobal,
1648 FALSE,
1649 TRUE,
1650 Fvb,
1651 mVariableModuleGlobal->NonVolatileLastVariableOffset + sizeof (VARIABLE_HEADER),
1652 (UINT32) VarSize - sizeof (VARIABLE_HEADER),
1653 (UINT8 *) NextVariable + sizeof (VARIABLE_HEADER)
1654 );
1655
1656 if (EFI_ERROR (Status)) {
1657 goto Done;
1658 }
1659 //
1660 // Step 4:
1661 //
1662 NextVariable->State = VAR_ADDED;
1663 Status = UpdateVariableStore (
1664 &mVariableModuleGlobal->VariableGlobal,
1665 FALSE,
1666 TRUE,
1667 Fvb,
1668 mVariableModuleGlobal->NonVolatileLastVariableOffset + OFFSET_OF (VARIABLE_HEADER, State),
1669 sizeof (UINT8),
1670 &NextVariable->State
1671 );
1672
1673 if (EFI_ERROR (Status)) {
1674 goto Done;
1675 }
1676
1677 mVariableModuleGlobal->NonVolatileLastVariableOffset += HEADER_ALIGN (VarSize);
1678
1679 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {
1680 mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VarSize);
1681 } else {
1682 mVariableModuleGlobal->CommonVariableTotalSize += HEADER_ALIGN (VarSize);
1683 }
1684 //
1685 // update the memory copy of Flash region.
1686 //
1687 CopyMem ((UINT8 *)mNvVariableCache + CacheOffset, (UINT8 *)NextVariable, VarSize);
1688 } else {
1689 //
1690 // Create a volatile variable.
1691 //
1692 Volatile = TRUE;
1693
1694 if ((UINT32) (VarSize + mVariableModuleGlobal->VolatileLastVariableOffset) >
1695 ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size) {
1696 //
1697 // Perform garbage collection & reclaim operation.
1698 //
1699 Status = Reclaim (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase,
1700 &mVariableModuleGlobal->VolatileLastVariableOffset, TRUE, Variable->CurrPtr);
1701 if (EFI_ERROR (Status)) {
1702 goto Done;
1703 }
1704 //
1705 // If still no enough space, return out of resources.
1706 //
1707 if ((UINT32) (VarSize + mVariableModuleGlobal->VolatileLastVariableOffset) >
1708 ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size
1709 ) {
1710 Status = EFI_OUT_OF_RESOURCES;
1711 goto Done;
1712 }
1713 Reclaimed = TRUE;
1714 }
1715
1716 NextVariable->State = VAR_ADDED;
1717 Status = UpdateVariableStore (
1718 &mVariableModuleGlobal->VariableGlobal,
1719 TRUE,
1720 TRUE,
1721 Fvb,
1722 mVariableModuleGlobal->VolatileLastVariableOffset,
1723 (UINT32) VarSize,
1724 (UINT8 *) NextVariable
1725 );
1726
1727 if (EFI_ERROR (Status)) {
1728 goto Done;
1729 }
1730
1731 mVariableModuleGlobal->VolatileLastVariableOffset += HEADER_ALIGN (VarSize);
1732 }
1733
1734 //
1735 // Mark the old variable as deleted.
1736 //
1737 if (!Reclaimed && !EFI_ERROR (Status) && Variable->CurrPtr != NULL) {
1738 State = Variable->CurrPtr->State;
1739 State &= VAR_DELETED;
1740
1741 Status = UpdateVariableStore (
1742 &mVariableModuleGlobal->VariableGlobal,
1743 Variable->Volatile,
1744 FALSE,
1745 Fvb,
1746 (UINTN) &Variable->CurrPtr->State,
1747 sizeof (UINT8),
1748 &State
1749 );
1750 if (!EFI_ERROR (Status) && !Variable->Volatile) {
1751 CacheVariable->CurrPtr->State = State;
1752 }
1753 }
1754
1755 if (!EFI_ERROR (Status)) {
1756 UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);
1757 }
1758
1759 Done:
1760 return Status;
1761 }
1762
1763 /**
1764
1765 This code finds variable in storage blocks (Volatile or Non-Volatile).
1766
1767 @param VariableName Name of Variable to be found.
1768 @param VendorGuid Variable vendor GUID.
1769 @param Attributes Attribute value of the variable found.
1770 @param DataSize Size of Data found. If size is less than the
1771 data, this value contains the required size.
1772 @param Data Data pointer.
1773
1774 @return EFI_INVALID_PARAMETER Invalid parameter.
1775 @return EFI_SUCCESS Find the specified variable.
1776 @return EFI_NOT_FOUND Not found.
1777 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
1778
1779 **/
1780 EFI_STATUS
1781 EFIAPI
1782 VariableServiceGetVariable (
1783 IN CHAR16 *VariableName,
1784 IN EFI_GUID *VendorGuid,
1785 OUT UINT32 *Attributes OPTIONAL,
1786 IN OUT UINTN *DataSize,
1787 OUT VOID *Data
1788 )
1789 {
1790 EFI_STATUS Status;
1791 VARIABLE_POINTER_TRACK Variable;
1792 UINTN VarDataSize;
1793
1794 if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {
1795 return EFI_INVALID_PARAMETER;
1796 }
1797
1798 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
1799
1800 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);
1801 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {
1802 goto Done;
1803 }
1804
1805 //
1806 // Get data size
1807 //
1808 VarDataSize = DataSizeOfVariable (Variable.CurrPtr);
1809 ASSERT (VarDataSize != 0);
1810
1811 if (*DataSize >= VarDataSize) {
1812 if (Data == NULL) {
1813 Status = EFI_INVALID_PARAMETER;
1814 goto Done;
1815 }
1816
1817 CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);
1818 if (Attributes != NULL) {
1819 *Attributes = Variable.CurrPtr->Attributes;
1820 }
1821
1822 *DataSize = VarDataSize;
1823 UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, TRUE, FALSE, FALSE, FALSE);
1824
1825 Status = EFI_SUCCESS;
1826 goto Done;
1827 } else {
1828 *DataSize = VarDataSize;
1829 Status = EFI_BUFFER_TOO_SMALL;
1830 goto Done;
1831 }
1832
1833 Done:
1834 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
1835 return Status;
1836 }
1837
1838
1839
1840 /**
1841
1842 This code Finds the Next available variable.
1843
1844 @param VariableNameSize Size of the variable name.
1845 @param VariableName Pointer to variable name.
1846 @param VendorGuid Variable Vendor Guid.
1847
1848 @return EFI_INVALID_PARAMETER Invalid parameter.
1849 @return EFI_SUCCESS Find the specified variable.
1850 @return EFI_NOT_FOUND Not found.
1851 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
1852
1853 **/
1854 EFI_STATUS
1855 EFIAPI
1856 VariableServiceGetNextVariableName (
1857 IN OUT UINTN *VariableNameSize,
1858 IN OUT CHAR16 *VariableName,
1859 IN OUT EFI_GUID *VendorGuid
1860 )
1861 {
1862 VARIABLE_STORE_TYPE Type;
1863 VARIABLE_POINTER_TRACK Variable;
1864 VARIABLE_POINTER_TRACK VariableInHob;
1865 UINTN VarNameSize;
1866 EFI_STATUS Status;
1867 VARIABLE_STORE_HEADER *VariableStoreHeader[VariableStoreTypeMax];
1868
1869 if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {
1870 return EFI_INVALID_PARAMETER;
1871 }
1872
1873 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
1874
1875 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);
1876 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {
1877 goto Done;
1878 }
1879
1880 if (VariableName[0] != 0) {
1881 //
1882 // If variable name is not NULL, get next variable.
1883 //
1884 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
1885 }
1886
1887 //
1888 // 0: Volatile, 1: HOB, 2: Non-Volatile.
1889 // The index and attributes mapping must be kept in this order as FindVariable
1890 // makes use of this mapping to implement search algorithm.
1891 //
1892 VariableStoreHeader[VariableStoreTypeVolatile] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;
1893 VariableStoreHeader[VariableStoreTypeHob] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;
1894 VariableStoreHeader[VariableStoreTypeNv] = mNvVariableCache;
1895
1896 while (TRUE) {
1897 //
1898 // Switch from Volatile to HOB, to Non-Volatile.
1899 //
1900 while ((Variable.CurrPtr >= Variable.EndPtr) ||
1901 (Variable.CurrPtr == NULL) ||
1902 !IsValidVariableHeader (Variable.CurrPtr)
1903 ) {
1904 //
1905 // Find current storage index
1906 //
1907 for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {
1908 if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr == GetStartPointer (VariableStoreHeader[Type]))) {
1909 break;
1910 }
1911 }
1912 ASSERT (Type < VariableStoreTypeMax);
1913 //
1914 // Switch to next storage
1915 //
1916 for (Type++; Type < VariableStoreTypeMax; Type++) {
1917 if (VariableStoreHeader[Type] != NULL) {
1918 break;
1919 }
1920 }
1921 //
1922 // Capture the case that
1923 // 1. current storage is the last one, or
1924 // 2. no further storage
1925 //
1926 if (Type == VariableStoreTypeMax) {
1927 Status = EFI_NOT_FOUND;
1928 goto Done;
1929 }
1930 Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);
1931 Variable.EndPtr = GetEndPointer (VariableStoreHeader[Type]);
1932 Variable.CurrPtr = Variable.StartPtr;
1933 }
1934
1935 //
1936 // Variable is found
1937 //
1938 if (Variable.CurrPtr->State == VAR_ADDED) {
1939 if ((AtRuntime () && ((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0)) == 0) {
1940
1941 //
1942 // Don't return NV variable when HOB overrides it
1943 //
1944 if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) && (VariableStoreHeader[VariableStoreTypeNv] != NULL) &&
1945 (Variable.StartPtr == GetStartPointer (VariableStoreHeader[VariableStoreTypeNv]))
1946 ) {
1947 VariableInHob.StartPtr = GetStartPointer (VariableStoreHeader[VariableStoreTypeHob]);
1948 VariableInHob.EndPtr = GetEndPointer (VariableStoreHeader[VariableStoreTypeHob]);
1949 Status = FindVariableEx (
1950 GetVariableNamePtr (Variable.CurrPtr),
1951 &Variable.CurrPtr->VendorGuid,
1952 FALSE,
1953 &VariableInHob
1954 );
1955 if (!EFI_ERROR (Status)) {
1956 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
1957 continue;
1958 }
1959 }
1960
1961 VarNameSize = NameSizeOfVariable (Variable.CurrPtr);
1962 ASSERT (VarNameSize != 0);
1963
1964 if (VarNameSize <= *VariableNameSize) {
1965 CopyMem (VariableName, GetVariableNamePtr (Variable.CurrPtr), VarNameSize);
1966 CopyMem (VendorGuid, &Variable.CurrPtr->VendorGuid, sizeof (EFI_GUID));
1967 Status = EFI_SUCCESS;
1968 } else {
1969 Status = EFI_BUFFER_TOO_SMALL;
1970 }
1971
1972 *VariableNameSize = VarNameSize;
1973 goto Done;
1974 }
1975 }
1976
1977 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
1978 }
1979
1980 Done:
1981 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
1982 return Status;
1983 }
1984
1985 /**
1986
1987 This code sets variable in storage blocks (Volatile or Non-Volatile).
1988
1989 @param VariableName Name of Variable to be found.
1990 @param VendorGuid Variable vendor GUID.
1991 @param Attributes Attribute value of the variable found
1992 @param DataSize Size of Data found. If size is less than the
1993 data, this value contains the required size.
1994 @param Data Data pointer.
1995
1996 @return EFI_INVALID_PARAMETER Invalid parameter.
1997 @return EFI_SUCCESS Set successfully.
1998 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.
1999 @return EFI_NOT_FOUND Not found.
2000 @return EFI_WRITE_PROTECTED Variable is read-only.
2001
2002 **/
2003 EFI_STATUS
2004 EFIAPI
2005 VariableServiceSetVariable (
2006 IN CHAR16 *VariableName,
2007 IN EFI_GUID *VendorGuid,
2008 IN UINT32 Attributes,
2009 IN UINTN DataSize,
2010 IN VOID *Data
2011 )
2012 {
2013 VARIABLE_POINTER_TRACK Variable;
2014 EFI_STATUS Status;
2015 VARIABLE_HEADER *NextVariable;
2016 EFI_PHYSICAL_ADDRESS Point;
2017
2018 //
2019 // Check input parameters.
2020 //
2021 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {
2022 return EFI_INVALID_PARAMETER;
2023 }
2024
2025 if (DataSize != 0 && Data == NULL) {
2026 return EFI_INVALID_PARAMETER;
2027 }
2028
2029 //
2030 // Not support authenticated variable write yet.
2031 //
2032 if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {
2033 return EFI_INVALID_PARAMETER;
2034 }
2035
2036 //
2037 // Make sure if runtime bit is set, boot service bit is set also.
2038 //
2039 if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {
2040 return EFI_INVALID_PARAMETER;
2041 }
2042
2043 //
2044 // The size of the VariableName, including the Unicode Null in bytes plus
2045 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)
2046 // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.
2047 //
2048 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
2049 if ((DataSize > PcdGet32 (PcdMaxHardwareErrorVariableSize)) ||
2050 (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > PcdGet32 (PcdMaxHardwareErrorVariableSize))) {
2051 return EFI_INVALID_PARAMETER;
2052 }
2053 //
2054 // According to UEFI spec, HARDWARE_ERROR_RECORD variable name convention should be L"HwErrRecXXXX".
2055 //
2056 if (StrnCmp(VariableName, L"HwErrRec", StrLen(L"HwErrRec")) != 0) {
2057 return EFI_INVALID_PARAMETER;
2058 }
2059 } else {
2060 //
2061 // The size of the VariableName, including the Unicode Null in bytes plus
2062 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxVariableSize) bytes.
2063 //
2064 if ((DataSize > PcdGet32 (PcdMaxVariableSize)) ||
2065 (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > PcdGet32 (PcdMaxVariableSize))) {
2066 return EFI_INVALID_PARAMETER;
2067 }
2068 }
2069
2070 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
2071
2072 //
2073 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.
2074 //
2075 if (1 < InterlockedIncrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState)) {
2076 Point = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;
2077 //
2078 // Parse non-volatile variable data and get last variable offset.
2079 //
2080 NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point);
2081 while ((NextVariable < GetEndPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point))
2082 && IsValidVariableHeader (NextVariable)) {
2083 NextVariable = GetNextVariablePtr (NextVariable);
2084 }
2085 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) Point;
2086 }
2087
2088 //
2089 // Check whether the input variable is already existed.
2090 //
2091 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, TRUE);
2092 if (!EFI_ERROR (Status)) {
2093 if (((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) && AtRuntime ()) {
2094 return EFI_WRITE_PROTECTED;
2095 }
2096 }
2097
2098 //
2099 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.
2100 //
2101 AutoUpdateLangVariable (VariableName, Data, DataSize);
2102
2103 Status = UpdateVariable (VariableName, VendorGuid, Data, DataSize, Attributes, &Variable);
2104
2105 InterlockedDecrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState);
2106 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
2107
2108 return Status;
2109 }
2110
2111 /**
2112
2113 This code returns information about the EFI variables.
2114
2115 @param Attributes Attributes bitmask to specify the type of variables
2116 on which to return information.
2117 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
2118 for the EFI variables associated with the attributes specified.
2119 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
2120 for EFI variables associated with the attributes specified.
2121 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
2122 associated with the attributes specified.
2123
2124 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
2125 @return EFI_SUCCESS Query successfully.
2126 @return EFI_UNSUPPORTED The attribute is not supported on this platform.
2127
2128 **/
2129 EFI_STATUS
2130 EFIAPI
2131 VariableServiceQueryVariableInfo (
2132 IN UINT32 Attributes,
2133 OUT UINT64 *MaximumVariableStorageSize,
2134 OUT UINT64 *RemainingVariableStorageSize,
2135 OUT UINT64 *MaximumVariableSize
2136 )
2137 {
2138 VARIABLE_HEADER *Variable;
2139 VARIABLE_HEADER *NextVariable;
2140 UINT64 VariableSize;
2141 VARIABLE_STORE_HEADER *VariableStoreHeader;
2142 UINT64 CommonVariableTotalSize;
2143 UINT64 HwErrVariableTotalSize;
2144
2145 CommonVariableTotalSize = 0;
2146 HwErrVariableTotalSize = 0;
2147
2148 if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {
2149 return EFI_INVALID_PARAMETER;
2150 }
2151
2152 if((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == 0) {
2153 //
2154 // Make sure the Attributes combination is supported by the platform.
2155 //
2156 return EFI_UNSUPPORTED;
2157 } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {
2158 //
2159 // Make sure if runtime bit is set, boot service bit is set also.
2160 //
2161 return EFI_INVALID_PARAMETER;
2162 } else if (AtRuntime () && ((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0)) {
2163 //
2164 // Make sure RT Attribute is set if we are in Runtime phase.
2165 //
2166 return EFI_INVALID_PARAMETER;
2167 } else if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
2168 //
2169 // Make sure Hw Attribute is set with NV.
2170 //
2171 return EFI_INVALID_PARAMETER;
2172 } else if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {
2173 //
2174 // Not support authentiated variable write yet.
2175 //
2176 return EFI_UNSUPPORTED;
2177 }
2178
2179 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
2180
2181 if((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {
2182 //
2183 // Query is Volatile related.
2184 //
2185 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);
2186 } else {
2187 //
2188 // Query is Non-Volatile related.
2189 //
2190 VariableStoreHeader = mNvVariableCache;
2191 }
2192
2193 //
2194 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
2195 // with the storage size (excluding the storage header size).
2196 //
2197 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);
2198
2199 //
2200 // Harware error record variable needs larger size.
2201 //
2202 if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {
2203 *MaximumVariableStorageSize = PcdGet32 (PcdHwErrStorageSize);
2204 *MaximumVariableSize = PcdGet32 (PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER);
2205 } else {
2206 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {
2207 ASSERT (PcdGet32 (PcdHwErrStorageSize) < VariableStoreHeader->Size);
2208 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize);
2209 }
2210
2211 //
2212 // Let *MaximumVariableSize be PcdGet32 (PcdMaxVariableSize) with the exception of the variable header size.
2213 //
2214 *MaximumVariableSize = PcdGet32 (PcdMaxVariableSize) - sizeof (VARIABLE_HEADER);
2215 }
2216
2217 //
2218 // Point to the starting address of the variables.
2219 //
2220 Variable = GetStartPointer (VariableStoreHeader);
2221
2222 //
2223 // Now walk through the related variable store.
2224 //
2225 while ((Variable < GetEndPointer (VariableStoreHeader)) && IsValidVariableHeader (Variable)) {
2226 NextVariable = GetNextVariablePtr (Variable);
2227 VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;
2228
2229 if (AtRuntime ()) {
2230 //
2231 // We don't take the state of the variables in mind
2232 // when calculating RemainingVariableStorageSize,
2233 // since the space occupied by variables not marked with
2234 // VAR_ADDED is not allowed to be reclaimed in Runtime.
2235 //
2236 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
2237 HwErrVariableTotalSize += VariableSize;
2238 } else {
2239 CommonVariableTotalSize += VariableSize;
2240 }
2241 } else {
2242 //
2243 // Only care about Variables with State VAR_ADDED, because
2244 // the space not marked as VAR_ADDED is reclaimable now.
2245 //
2246 if (Variable->State == VAR_ADDED) {
2247 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
2248 HwErrVariableTotalSize += VariableSize;
2249 } else {
2250 CommonVariableTotalSize += VariableSize;
2251 }
2252 }
2253 }
2254
2255 //
2256 // Go to the next one.
2257 //
2258 Variable = NextVariable;
2259 }
2260
2261 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD){
2262 *RemainingVariableStorageSize = *MaximumVariableStorageSize - HwErrVariableTotalSize;
2263 }else {
2264 *RemainingVariableStorageSize = *MaximumVariableStorageSize - CommonVariableTotalSize;
2265 }
2266
2267 if (*RemainingVariableStorageSize < sizeof (VARIABLE_HEADER)) {
2268 *MaximumVariableSize = 0;
2269 } else if ((*RemainingVariableStorageSize - sizeof (VARIABLE_HEADER)) < *MaximumVariableSize) {
2270 *MaximumVariableSize = *RemainingVariableStorageSize - sizeof (VARIABLE_HEADER);
2271 }
2272
2273 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
2274 return EFI_SUCCESS;
2275 }
2276
2277
2278 /**
2279 This function reclaims variable storage if free size is below the threshold.
2280
2281 **/
2282 VOID
2283 ReclaimForOS(
2284 VOID
2285 )
2286 {
2287 EFI_STATUS Status;
2288 UINTN CommonVariableSpace;
2289 UINTN RemainingCommonVariableSpace;
2290 UINTN RemainingHwErrVariableSpace;
2291
2292 Status = EFI_SUCCESS;
2293
2294 CommonVariableSpace = ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase)))->Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32(PcdHwErrStorageSize); //Allowable max size of common variable storage space
2295
2296 RemainingCommonVariableSpace = CommonVariableSpace - mVariableModuleGlobal->CommonVariableTotalSize;
2297
2298 RemainingHwErrVariableSpace = PcdGet32 (PcdHwErrStorageSize) - mVariableModuleGlobal->HwErrVariableTotalSize;
2299 //
2300 // Check if the free area is blow a threshold.
2301 //
2302 if ((RemainingCommonVariableSpace < PcdGet32 (PcdMaxVariableSize))
2303 || ((PcdGet32 (PcdHwErrStorageSize) != 0) &&
2304 (RemainingHwErrVariableSpace < PcdGet32 (PcdMaxHardwareErrorVariableSize)))){
2305 Status = Reclaim (
2306 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,
2307 &mVariableModuleGlobal->NonVolatileLastVariableOffset,
2308 FALSE,
2309 NULL
2310 );
2311 ASSERT_EFI_ERROR (Status);
2312 }
2313 }
2314
2315
2316 /**
2317 Initializes variable write service after FVB was ready.
2318
2319 @retval EFI_SUCCESS Function successfully executed.
2320 @retval Others Fail to initialize the variable service.
2321
2322 **/
2323 EFI_STATUS
2324 VariableWriteServiceInitialize (
2325 VOID
2326 )
2327 {
2328 EFI_STATUS Status;
2329 VARIABLE_STORE_HEADER *VariableStoreHeader;
2330 UINTN Index;
2331 UINT8 Data;
2332 EFI_PHYSICAL_ADDRESS VariableStoreBase;
2333 VARIABLE_HEADER *Variable;
2334 VOID *VariableData;
2335
2336 VariableStoreBase = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;
2337 VariableStoreHeader = (VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase;
2338
2339 //
2340 // Check if the free area is really free.
2341 //
2342 for (Index = mVariableModuleGlobal->NonVolatileLastVariableOffset; Index < VariableStoreHeader->Size; Index++) {
2343 Data = ((UINT8 *) mNvVariableCache)[Index];
2344 if (Data != 0xff) {
2345 //
2346 // There must be something wrong in variable store, do reclaim operation.
2347 //
2348 Status = Reclaim (
2349 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,
2350 &mVariableModuleGlobal->NonVolatileLastVariableOffset,
2351 FALSE,
2352 NULL
2353 );
2354 if (EFI_ERROR (Status)) {
2355 return Status;
2356 }
2357 break;
2358 }
2359 }
2360
2361 //
2362 // Flush the HOB variable to flash and invalidate HOB variable.
2363 //
2364 if (mVariableModuleGlobal->VariableGlobal.HobVariableBase != 0) {
2365 //
2366 // Clear the HobVariableBase to avoid SetVariable() updating the variable in HOB
2367 //
2368 VariableStoreHeader = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;
2369 mVariableModuleGlobal->VariableGlobal.HobVariableBase = 0;
2370
2371 for ( Variable = GetStartPointer (VariableStoreHeader)
2372 ; (Variable < GetEndPointer (VariableStoreHeader) && IsValidVariableHeader (Variable))
2373 ; Variable = GetNextVariablePtr (Variable)
2374 ) {
2375 ASSERT (Variable->State == VAR_ADDED);
2376 ASSERT ((Variable->Attributes & EFI_VARIABLE_NON_VOLATILE) != 0);
2377 VariableData = GetVariableDataPtr (Variable);
2378 Status = VariableServiceSetVariable (
2379 GetVariableNamePtr (Variable),
2380 &Variable->VendorGuid,
2381 Variable->Attributes,
2382 Variable->DataSize,
2383 VariableData
2384 );
2385 ASSERT_EFI_ERROR (Status);
2386 }
2387 }
2388 return EFI_SUCCESS;
2389 }
2390
2391
2392 /**
2393 Initializes variable store area for non-volatile and volatile variable.
2394
2395 @retval EFI_SUCCESS Function successfully executed.
2396 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
2397
2398 **/
2399 EFI_STATUS
2400 VariableCommonInitialize (
2401 VOID
2402 )
2403 {
2404 EFI_STATUS Status;
2405 VARIABLE_STORE_HEADER *VolatileVariableStore;
2406 VARIABLE_STORE_HEADER *VariableStoreHeader;
2407 VARIABLE_HEADER *NextVariable;
2408 EFI_PHYSICAL_ADDRESS TempVariableStoreHeader;
2409 EFI_PHYSICAL_ADDRESS VariableStoreBase;
2410 UINT64 VariableStoreLength;
2411 UINTN ScratchSize;
2412 UINTN VariableSize;
2413 EFI_HOB_GUID_TYPE *GuidHob;
2414
2415 //
2416 // Allocate runtime memory for variable driver global structure.
2417 //
2418 mVariableModuleGlobal = AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL));
2419 if (mVariableModuleGlobal == NULL) {
2420 return EFI_OUT_OF_RESOURCES;
2421 }
2422
2423 InitializeLock (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock, TPL_NOTIFY);
2424
2425 //
2426 // Note that in EdkII variable driver implementation, Hardware Error Record type variable
2427 // is stored with common variable in the same NV region. So the platform integrator should
2428 // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of
2429 // PcdFlashNvStorageVariableSize.
2430 //
2431 ASSERT (PcdGet32 (PcdHwErrStorageSize) <= PcdGet32 (PcdFlashNvStorageVariableSize));
2432
2433 //
2434 // Get HOB variable store.
2435 //
2436 GuidHob = GetFirstGuidHob (&gEfiVariableGuid);
2437 if (GuidHob != NULL) {
2438 VariableStoreHeader = GET_GUID_HOB_DATA (GuidHob);
2439 if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) {
2440 mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VariableStoreHeader;
2441 } else {
2442 DEBUG ((EFI_D_ERROR, "HOB Variable Store header is corrupted!\n"));
2443 }
2444 }
2445
2446 //
2447 // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.
2448 //
2449 ScratchSize = MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize));
2450 VolatileVariableStore = AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize) + ScratchSize);
2451 if (VolatileVariableStore == NULL) {
2452 FreePool (mVariableModuleGlobal);
2453 return EFI_OUT_OF_RESOURCES;
2454 }
2455
2456 SetMem (VolatileVariableStore, PcdGet32 (PcdVariableStoreSize) + ScratchSize, 0xff);
2457
2458 //
2459 // Initialize Variable Specific Data.
2460 //
2461 mVariableModuleGlobal->VariableGlobal.VolatileVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VolatileVariableStore;
2462 mVariableModuleGlobal->VolatileLastVariableOffset = (UINTN) GetStartPointer (VolatileVariableStore) - (UINTN) VolatileVariableStore;
2463 mVariableModuleGlobal->FvbInstance = NULL;
2464
2465 CopyGuid (&VolatileVariableStore->Signature, &gEfiVariableGuid);
2466 VolatileVariableStore->Size = PcdGet32 (PcdVariableStoreSize);
2467 VolatileVariableStore->Format = VARIABLE_STORE_FORMATTED;
2468 VolatileVariableStore->State = VARIABLE_STORE_HEALTHY;
2469 VolatileVariableStore->Reserved = 0;
2470 VolatileVariableStore->Reserved1 = 0;
2471
2472 //
2473 // Get non-volatile variable store.
2474 //
2475
2476 TempVariableStoreHeader = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);
2477 if (TempVariableStoreHeader == 0) {
2478 TempVariableStoreHeader = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);
2479 }
2480
2481 //
2482 // Check if the Firmware Volume is not corrupted
2483 //
2484 if ((((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(TempVariableStoreHeader))->Signature != EFI_FVH_SIGNATURE) ||
2485 (!CompareGuid (&gEfiSystemNvDataFvGuid, &((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(TempVariableStoreHeader))->FileSystemGuid))) {
2486 Status = EFI_VOLUME_CORRUPTED;
2487 DEBUG ((EFI_D_ERROR, "Firmware Volume for Variable Store is corrupted\n"));
2488 goto Done;
2489 }
2490
2491 VariableStoreBase = TempVariableStoreHeader + \
2492 (((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(TempVariableStoreHeader)) -> HeaderLength);
2493 VariableStoreLength = (UINT64) PcdGet32 (PcdFlashNvStorageVariableSize) - \
2494 (((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(TempVariableStoreHeader)) -> HeaderLength);
2495
2496 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase;
2497 VariableStoreHeader = (VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase;
2498 if (GetVariableStoreStatus (VariableStoreHeader) != EfiValid) {
2499 Status = EFI_VOLUME_CORRUPTED;
2500 DEBUG((EFI_D_INFO, "Variable Store header is corrupted\n"));
2501 goto Done;
2502 }
2503 ASSERT(VariableStoreHeader->Size == VariableStoreLength);
2504
2505 //
2506 // Parse non-volatile variable data and get last variable offset.
2507 //
2508 NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase);
2509 while (IsValidVariableHeader (NextVariable)) {
2510 VariableSize = NextVariable->NameSize + NextVariable->DataSize + sizeof (VARIABLE_HEADER);
2511 if ((NextVariable->Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {
2512 mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VariableSize);
2513 } else {
2514 mVariableModuleGlobal->CommonVariableTotalSize += HEADER_ALIGN (VariableSize);
2515 }
2516
2517 NextVariable = GetNextVariablePtr (NextVariable);
2518 }
2519
2520 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) VariableStoreBase;
2521
2522 //
2523 // Allocate runtime memory used for a memory copy of the FLASH region.
2524 // Keep the memory and the FLASH in sync as updates occur
2525 //
2526 mNvVariableCache = AllocateRuntimeZeroPool ((UINTN)VariableStoreLength);
2527 if (mNvVariableCache == NULL) {
2528 Status = EFI_OUT_OF_RESOURCES;
2529 goto Done;
2530 }
2531 CopyMem (mNvVariableCache, (CHAR8 *)(UINTN)VariableStoreBase, (UINTN)VariableStoreLength);
2532 Status = EFI_SUCCESS;
2533
2534 Done:
2535 if (EFI_ERROR (Status)) {
2536 FreePool (mVariableModuleGlobal);
2537 FreePool (VolatileVariableStore);
2538 }
2539
2540 return Status;
2541 }
2542
2543
2544 /**
2545 Get the proper fvb handle and/or fvb protocol by the given Flash address.
2546
2547 @param[in] Address The Flash address.
2548 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.
2549 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.
2550
2551 **/
2552 EFI_STATUS
2553 GetFvbInfoByAddress (
2554 IN EFI_PHYSICAL_ADDRESS Address,
2555 OUT EFI_HANDLE *FvbHandle OPTIONAL,
2556 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvbProtocol OPTIONAL
2557 )
2558 {
2559 EFI_STATUS Status;
2560 EFI_HANDLE *HandleBuffer;
2561 UINTN HandleCount;
2562 UINTN Index;
2563 EFI_PHYSICAL_ADDRESS FvbBaseAddress;
2564 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;
2565 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
2566 EFI_FVB_ATTRIBUTES_2 Attributes;
2567
2568 //
2569 // Get all FVB handles.
2570 //
2571 Status = GetFvbCountAndBuffer (&HandleCount, &HandleBuffer);
2572 if (EFI_ERROR (Status)) {
2573 return EFI_NOT_FOUND;
2574 }
2575
2576 //
2577 // Get the FVB to access variable store.
2578 //
2579 Fvb = NULL;
2580 for (Index = 0; Index < HandleCount; Index += 1, Status = EFI_NOT_FOUND, Fvb = NULL) {
2581 Status = GetFvbByHandle (HandleBuffer[Index], &Fvb);
2582 if (EFI_ERROR (Status)) {
2583 Status = EFI_NOT_FOUND;
2584 break;
2585 }
2586
2587 //
2588 // Ensure this FVB protocol supported Write operation.
2589 //
2590 Status = Fvb->GetAttributes (Fvb, &Attributes);
2591 if (EFI_ERROR (Status) || ((Attributes & EFI_FVB2_WRITE_STATUS) == 0)) {
2592 continue;
2593 }
2594
2595 //
2596 // Compare the address and select the right one.
2597 //
2598 Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);
2599 if (EFI_ERROR (Status)) {
2600 continue;
2601 }
2602
2603 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvbBaseAddress);
2604 if ((Address >= FvbBaseAddress) && (Address < (FvbBaseAddress + FwVolHeader->FvLength))) {
2605 if (FvbHandle != NULL) {
2606 *FvbHandle = HandleBuffer[Index];
2607 }
2608 if (FvbProtocol != NULL) {
2609 *FvbProtocol = Fvb;
2610 }
2611 Status = EFI_SUCCESS;
2612 break;
2613 }
2614 }
2615 FreePool (HandleBuffer);
2616
2617 if (Fvb == NULL) {
2618 Status = EFI_NOT_FOUND;
2619 }
2620
2621 return Status;
2622 }
2623