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