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