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