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