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