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