]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
MdeModulePkg Variable: Add some missing changes for 9b18845
[mirror_edk2.git] / MdeModulePkg / Universal / Variable / RuntimeDxe / Variable.c
1 /** @file
2 The common variable operation routines shared by DXE_RUNTIME variable
3 module and DXE_SMM variable module.
4
5 Caution: This module requires additional review when modified.
6 This driver will have external input - variable data. They may be input in SMM mode.
7 This external input must be validated carefully to avoid security issue like
8 buffer overflow, integer overflow.
9
10 VariableServiceGetNextVariableName () and VariableServiceQueryVariableInfo() are external API.
11 They need check input parameter.
12
13 VariableServiceGetVariable() and VariableServiceSetVariable() are external API
14 to receive datasize and data buffer. The size should be checked carefully.
15
16 VariableServiceSetVariable() should also check authenticate data to avoid buffer overflow,
17 integer overflow. It should also check attribute to avoid authentication bypass.
18
19 Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
20 (C) Copyright 2015-2018 Hewlett Packard Enterprise Development LP<BR>
21 This program and the accompanying materials
22 are licensed and made available under the terms and conditions of the BSD License
23 which accompanies this distribution. The full text of the license may be found at
24 http://opensource.org/licenses/bsd-license.php
25
26 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
27 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
28
29 **/
30
31 #include "Variable.h"
32
33 VARIABLE_MODULE_GLOBAL *mVariableModuleGlobal;
34
35 ///
36 /// Define a memory cache that improves the search performance for a variable.
37 ///
38 VARIABLE_STORE_HEADER *mNvVariableCache = NULL;
39
40 ///
41 /// Memory cache of Fv Header.
42 ///
43 EFI_FIRMWARE_VOLUME_HEADER *mNvFvHeaderCache = NULL;
44
45 ///
46 /// The memory entry used for variable statistics data.
47 ///
48 VARIABLE_INFO_ENTRY *gVariableInfo = NULL;
49
50 ///
51 /// The flag to indicate whether the platform has left the DXE phase of execution.
52 ///
53 BOOLEAN mEndOfDxe = FALSE;
54
55 ///
56 /// It indicates the var check request source.
57 /// In the implementation, DXE is regarded as untrusted, and SMM is trusted.
58 ///
59 VAR_CHECK_REQUEST_SOURCE mRequestSource = VarCheckFromUntrusted;
60
61 //
62 // It will record the current boot error flag before EndOfDxe.
63 //
64 VAR_ERROR_FLAG mCurrentBootVarErrFlag = VAR_ERROR_FLAG_NO_ERROR;
65
66 VARIABLE_ENTRY_PROPERTY mVariableEntryProperty[] = {
67 {
68 &gEdkiiVarErrorFlagGuid,
69 VAR_ERROR_FLAG_NAME,
70 {
71 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
72 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,
73 VARIABLE_ATTRIBUTE_NV_BS_RT,
74 sizeof (VAR_ERROR_FLAG),
75 sizeof (VAR_ERROR_FLAG)
76 }
77 },
78 };
79
80 AUTH_VAR_LIB_CONTEXT_IN mAuthContextIn = {
81 AUTH_VAR_LIB_CONTEXT_IN_STRUCT_VERSION,
82 //
83 // StructSize, TO BE FILLED
84 //
85 0,
86 //
87 // MaxAuthVariableSize, TO BE FILLED
88 //
89 0,
90 VariableExLibFindVariable,
91 VariableExLibFindNextVariable,
92 VariableExLibUpdateVariable,
93 VariableExLibGetScratchBuffer,
94 VariableExLibCheckRemainingSpaceForConsistency,
95 VariableExLibAtRuntime,
96 };
97
98 AUTH_VAR_LIB_CONTEXT_OUT mAuthContextOut;
99
100 /**
101 Routine used to track statistical information about variable usage.
102 The data is stored in the EFI system table so it can be accessed later.
103 VariableInfo.efi can dump out the table. Only Boot Services variable
104 accesses are tracked by this code. The PcdVariableCollectStatistics
105 build flag controls if this feature is enabled.
106
107 A read that hits in the cache will have Read and Cache true for
108 the transaction. Data is allocated by this routine, but never
109 freed.
110
111 @param[in] VariableName Name of the Variable to track.
112 @param[in] VendorGuid Guid of the Variable to track.
113 @param[in] Volatile TRUE if volatile FALSE if non-volatile.
114 @param[in] Read TRUE if GetVariable() was called.
115 @param[in] Write TRUE if SetVariable() was called.
116 @param[in] Delete TRUE if deleted via SetVariable().
117 @param[in] Cache TRUE for a cache hit.
118
119 **/
120 VOID
121 UpdateVariableInfo (
122 IN CHAR16 *VariableName,
123 IN EFI_GUID *VendorGuid,
124 IN BOOLEAN Volatile,
125 IN BOOLEAN Read,
126 IN BOOLEAN Write,
127 IN BOOLEAN Delete,
128 IN BOOLEAN Cache
129 )
130 {
131 VARIABLE_INFO_ENTRY *Entry;
132
133 if (FeaturePcdGet (PcdVariableCollectStatistics)) {
134
135 if (AtRuntime ()) {
136 // Don't collect statistics at runtime.
137 return;
138 }
139
140 if (gVariableInfo == NULL) {
141 //
142 // On the first call allocate a entry and place a pointer to it in
143 // the EFI System Table.
144 //
145 gVariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
146 ASSERT (gVariableInfo != NULL);
147
148 CopyGuid (&gVariableInfo->VendorGuid, VendorGuid);
149 gVariableInfo->Name = AllocateZeroPool (StrSize (VariableName));
150 ASSERT (gVariableInfo->Name != NULL);
151 StrCpyS (gVariableInfo->Name, StrSize(VariableName)/sizeof(CHAR16), VariableName);
152 gVariableInfo->Volatile = Volatile;
153 }
154
155
156 for (Entry = gVariableInfo; Entry != NULL; Entry = Entry->Next) {
157 if (CompareGuid (VendorGuid, &Entry->VendorGuid)) {
158 if (StrCmp (VariableName, Entry->Name) == 0) {
159 if (Read) {
160 Entry->ReadCount++;
161 }
162 if (Write) {
163 Entry->WriteCount++;
164 }
165 if (Delete) {
166 Entry->DeleteCount++;
167 }
168 if (Cache) {
169 Entry->CacheCount++;
170 }
171
172 return;
173 }
174 }
175
176 if (Entry->Next == NULL) {
177 //
178 // If the entry is not in the table add it.
179 // Next iteration of the loop will fill in the data.
180 //
181 Entry->Next = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
182 ASSERT (Entry->Next != NULL);
183
184 CopyGuid (&Entry->Next->VendorGuid, VendorGuid);
185 Entry->Next->Name = AllocateZeroPool (StrSize (VariableName));
186 ASSERT (Entry->Next->Name != NULL);
187 StrCpyS (Entry->Next->Name, StrSize(VariableName)/sizeof(CHAR16), VariableName);
188 Entry->Next->Volatile = Volatile;
189 }
190
191 }
192 }
193 }
194
195
196 /**
197
198 This code checks if variable header is valid or not.
199
200 @param Variable Pointer to the Variable Header.
201 @param VariableStoreEnd Pointer to the Variable Store End.
202
203 @retval TRUE Variable header is valid.
204 @retval FALSE Variable header is not valid.
205
206 **/
207 BOOLEAN
208 IsValidVariableHeader (
209 IN VARIABLE_HEADER *Variable,
210 IN VARIABLE_HEADER *VariableStoreEnd
211 )
212 {
213 if ((Variable == NULL) || (Variable >= VariableStoreEnd) || (Variable->StartId != VARIABLE_DATA)) {
214 //
215 // Variable is NULL or has reached the end of variable store,
216 // or the StartId is not correct.
217 //
218 return FALSE;
219 }
220
221 return TRUE;
222 }
223
224
225 /**
226
227 This function writes data to the FWH at the correct LBA even if the LBAs
228 are fragmented.
229
230 @param Global Pointer to VARAIBLE_GLOBAL structure.
231 @param Volatile Point out the Variable is Volatile or Non-Volatile.
232 @param SetByIndex TRUE if target pointer is given as index.
233 FALSE if target pointer is absolute.
234 @param Fvb Pointer to the writable FVB protocol.
235 @param DataPtrIndex Pointer to the Data from the end of VARIABLE_STORE_HEADER
236 structure.
237 @param DataSize Size of data to be written.
238 @param Buffer Pointer to the buffer from which data is written.
239
240 @retval EFI_INVALID_PARAMETER Parameters not valid.
241 @retval EFI_UNSUPPORTED Fvb is a NULL for Non-Volatile variable update.
242 @retval EFI_OUT_OF_RESOURCES The remaining size is not enough.
243 @retval EFI_SUCCESS Variable store successfully updated.
244
245 **/
246 EFI_STATUS
247 UpdateVariableStore (
248 IN VARIABLE_GLOBAL *Global,
249 IN BOOLEAN Volatile,
250 IN BOOLEAN SetByIndex,
251 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb,
252 IN UINTN DataPtrIndex,
253 IN UINT32 DataSize,
254 IN UINT8 *Buffer
255 )
256 {
257 EFI_FV_BLOCK_MAP_ENTRY *PtrBlockMapEntry;
258 UINTN BlockIndex2;
259 UINTN LinearOffset;
260 UINTN CurrWriteSize;
261 UINTN CurrWritePtr;
262 UINT8 *CurrBuffer;
263 EFI_LBA LbaNumber;
264 UINTN Size;
265 VARIABLE_STORE_HEADER *VolatileBase;
266 EFI_PHYSICAL_ADDRESS FvVolHdr;
267 EFI_PHYSICAL_ADDRESS DataPtr;
268 EFI_STATUS Status;
269
270 FvVolHdr = 0;
271 DataPtr = DataPtrIndex;
272
273 //
274 // Check if the Data is Volatile.
275 //
276 if (!Volatile) {
277 if (Fvb == NULL) {
278 return EFI_UNSUPPORTED;
279 }
280 Status = Fvb->GetPhysicalAddress(Fvb, &FvVolHdr);
281 ASSERT_EFI_ERROR (Status);
282
283 //
284 // Data Pointer should point to the actual Address where data is to be
285 // written.
286 //
287 if (SetByIndex) {
288 DataPtr += mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;
289 }
290
291 if ((DataPtr + DataSize) > (FvVolHdr + mNvFvHeaderCache->FvLength)) {
292 return EFI_OUT_OF_RESOURCES;
293 }
294 } else {
295 //
296 // Data Pointer should point to the actual Address where data is to be
297 // written.
298 //
299 VolatileBase = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);
300 if (SetByIndex) {
301 DataPtr += mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;
302 }
303
304 if ((DataPtr + DataSize) > ((UINTN) ((UINT8 *) VolatileBase + VolatileBase->Size))) {
305 return EFI_OUT_OF_RESOURCES;
306 }
307
308 //
309 // If Volatile Variable just do a simple mem copy.
310 //
311 CopyMem ((UINT8 *)(UINTN)DataPtr, Buffer, DataSize);
312 return EFI_SUCCESS;
313 }
314
315 //
316 // If we are here we are dealing with Non-Volatile Variables.
317 //
318 LinearOffset = (UINTN) FvVolHdr;
319 CurrWritePtr = (UINTN) DataPtr;
320 CurrWriteSize = DataSize;
321 CurrBuffer = Buffer;
322 LbaNumber = 0;
323
324 if (CurrWritePtr < LinearOffset) {
325 return EFI_INVALID_PARAMETER;
326 }
327
328 for (PtrBlockMapEntry = mNvFvHeaderCache->BlockMap; PtrBlockMapEntry->NumBlocks != 0; PtrBlockMapEntry++) {
329 for (BlockIndex2 = 0; BlockIndex2 < PtrBlockMapEntry->NumBlocks; BlockIndex2++) {
330 //
331 // Check to see if the Variable Writes are spanning through multiple
332 // blocks.
333 //
334 if ((CurrWritePtr >= LinearOffset) && (CurrWritePtr < LinearOffset + PtrBlockMapEntry->Length)) {
335 if ((CurrWritePtr + CurrWriteSize) <= (LinearOffset + PtrBlockMapEntry->Length)) {
336 Status = Fvb->Write (
337 Fvb,
338 LbaNumber,
339 (UINTN) (CurrWritePtr - LinearOffset),
340 &CurrWriteSize,
341 CurrBuffer
342 );
343 return Status;
344 } else {
345 Size = (UINT32) (LinearOffset + PtrBlockMapEntry->Length - CurrWritePtr);
346 Status = Fvb->Write (
347 Fvb,
348 LbaNumber,
349 (UINTN) (CurrWritePtr - LinearOffset),
350 &Size,
351 CurrBuffer
352 );
353 if (EFI_ERROR (Status)) {
354 return Status;
355 }
356
357 CurrWritePtr = LinearOffset + PtrBlockMapEntry->Length;
358 CurrBuffer = CurrBuffer + Size;
359 CurrWriteSize = CurrWriteSize - Size;
360 }
361 }
362
363 LinearOffset += PtrBlockMapEntry->Length;
364 LbaNumber++;
365 }
366 }
367
368 return EFI_SUCCESS;
369 }
370
371
372 /**
373
374 This code gets the current status of Variable Store.
375
376 @param VarStoreHeader Pointer to the Variable Store Header.
377
378 @retval EfiRaw Variable store status is raw.
379 @retval EfiValid Variable store status is valid.
380 @retval EfiInvalid Variable store status is invalid.
381
382 **/
383 VARIABLE_STORE_STATUS
384 GetVariableStoreStatus (
385 IN VARIABLE_STORE_HEADER *VarStoreHeader
386 )
387 {
388 if ((CompareGuid (&VarStoreHeader->Signature, &gEfiAuthenticatedVariableGuid) ||
389 CompareGuid (&VarStoreHeader->Signature, &gEfiVariableGuid)) &&
390 VarStoreHeader->Format == VARIABLE_STORE_FORMATTED &&
391 VarStoreHeader->State == VARIABLE_STORE_HEALTHY
392 ) {
393
394 return EfiValid;
395 } else if (((UINT32 *)(&VarStoreHeader->Signature))[0] == 0xffffffff &&
396 ((UINT32 *)(&VarStoreHeader->Signature))[1] == 0xffffffff &&
397 ((UINT32 *)(&VarStoreHeader->Signature))[2] == 0xffffffff &&
398 ((UINT32 *)(&VarStoreHeader->Signature))[3] == 0xffffffff &&
399 VarStoreHeader->Size == 0xffffffff &&
400 VarStoreHeader->Format == 0xff &&
401 VarStoreHeader->State == 0xff
402 ) {
403
404 return EfiRaw;
405 } else {
406 return EfiInvalid;
407 }
408 }
409
410 /**
411 This code gets the size of variable header.
412
413 @return Size of variable header in bytes in type UINTN.
414
415 **/
416 UINTN
417 GetVariableHeaderSize (
418 VOID
419 )
420 {
421 UINTN Value;
422
423 if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
424 Value = sizeof (AUTHENTICATED_VARIABLE_HEADER);
425 } else {
426 Value = sizeof (VARIABLE_HEADER);
427 }
428
429 return Value;
430 }
431
432 /**
433
434 This code gets the size of name of variable.
435
436 @param Variable Pointer to the Variable Header.
437
438 @return UINTN Size of variable in bytes.
439
440 **/
441 UINTN
442 NameSizeOfVariable (
443 IN VARIABLE_HEADER *Variable
444 )
445 {
446 AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
447
448 AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
449 if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
450 if (AuthVariable->State == (UINT8) (-1) ||
451 AuthVariable->DataSize == (UINT32) (-1) ||
452 AuthVariable->NameSize == (UINT32) (-1) ||
453 AuthVariable->Attributes == (UINT32) (-1)) {
454 return 0;
455 }
456 return (UINTN) AuthVariable->NameSize;
457 } else {
458 if (Variable->State == (UINT8) (-1) ||
459 Variable->DataSize == (UINT32) (-1) ||
460 Variable->NameSize == (UINT32) (-1) ||
461 Variable->Attributes == (UINT32) (-1)) {
462 return 0;
463 }
464 return (UINTN) Variable->NameSize;
465 }
466 }
467
468 /**
469 This code sets the size of name of variable.
470
471 @param[in] Variable Pointer to the Variable Header.
472 @param[in] NameSize Name size to set.
473
474 **/
475 VOID
476 SetNameSizeOfVariable (
477 IN VARIABLE_HEADER *Variable,
478 IN UINTN NameSize
479 )
480 {
481 AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
482
483 AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
484 if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
485 AuthVariable->NameSize = (UINT32) NameSize;
486 } else {
487 Variable->NameSize = (UINT32) NameSize;
488 }
489 }
490
491 /**
492
493 This code gets the size of variable data.
494
495 @param Variable Pointer to the Variable Header.
496
497 @return Size of variable in bytes.
498
499 **/
500 UINTN
501 DataSizeOfVariable (
502 IN VARIABLE_HEADER *Variable
503 )
504 {
505 AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
506
507 AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
508 if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
509 if (AuthVariable->State == (UINT8) (-1) ||
510 AuthVariable->DataSize == (UINT32) (-1) ||
511 AuthVariable->NameSize == (UINT32) (-1) ||
512 AuthVariable->Attributes == (UINT32) (-1)) {
513 return 0;
514 }
515 return (UINTN) AuthVariable->DataSize;
516 } else {
517 if (Variable->State == (UINT8) (-1) ||
518 Variable->DataSize == (UINT32) (-1) ||
519 Variable->NameSize == (UINT32) (-1) ||
520 Variable->Attributes == (UINT32) (-1)) {
521 return 0;
522 }
523 return (UINTN) Variable->DataSize;
524 }
525 }
526
527 /**
528 This code sets the size of variable data.
529
530 @param[in] Variable Pointer to the Variable Header.
531 @param[in] DataSize Data size to set.
532
533 **/
534 VOID
535 SetDataSizeOfVariable (
536 IN VARIABLE_HEADER *Variable,
537 IN UINTN DataSize
538 )
539 {
540 AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
541
542 AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
543 if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
544 AuthVariable->DataSize = (UINT32) DataSize;
545 } else {
546 Variable->DataSize = (UINT32) DataSize;
547 }
548 }
549
550 /**
551
552 This code gets the pointer to the variable name.
553
554 @param Variable Pointer to the Variable Header.
555
556 @return Pointer to Variable Name which is Unicode encoding.
557
558 **/
559 CHAR16 *
560 GetVariableNamePtr (
561 IN VARIABLE_HEADER *Variable
562 )
563 {
564 return (CHAR16 *) ((UINTN) Variable + GetVariableHeaderSize ());
565 }
566
567 /**
568 This code gets the pointer to the variable guid.
569
570 @param Variable Pointer to the Variable Header.
571
572 @return A EFI_GUID* pointer to Vendor Guid.
573
574 **/
575 EFI_GUID *
576 GetVendorGuidPtr (
577 IN VARIABLE_HEADER *Variable
578 )
579 {
580 AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
581
582 AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
583 if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
584 return &AuthVariable->VendorGuid;
585 } else {
586 return &Variable->VendorGuid;
587 }
588 }
589
590 /**
591
592 This code gets the pointer to the variable data.
593
594 @param Variable Pointer to the Variable Header.
595
596 @return Pointer to Variable Data.
597
598 **/
599 UINT8 *
600 GetVariableDataPtr (
601 IN VARIABLE_HEADER *Variable
602 )
603 {
604 UINTN Value;
605
606 //
607 // Be careful about pad size for alignment.
608 //
609 Value = (UINTN) GetVariableNamePtr (Variable);
610 Value += NameSizeOfVariable (Variable);
611 Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));
612
613 return (UINT8 *) Value;
614 }
615
616 /**
617 This code gets the variable data offset related to variable header.
618
619 @param Variable Pointer to the Variable Header.
620
621 @return Variable Data offset.
622
623 **/
624 UINTN
625 GetVariableDataOffset (
626 IN VARIABLE_HEADER *Variable
627 )
628 {
629 UINTN Value;
630
631 //
632 // Be careful about pad size for alignment
633 //
634 Value = GetVariableHeaderSize ();
635 Value += NameSizeOfVariable (Variable);
636 Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));
637
638 return Value;
639 }
640
641 /**
642
643 This code gets the pointer to the next variable header.
644
645 @param Variable Pointer to the Variable Header.
646
647 @return Pointer to next variable header.
648
649 **/
650 VARIABLE_HEADER *
651 GetNextVariablePtr (
652 IN VARIABLE_HEADER *Variable
653 )
654 {
655 UINTN Value;
656
657 Value = (UINTN) GetVariableDataPtr (Variable);
658 Value += DataSizeOfVariable (Variable);
659 Value += GET_PAD_SIZE (DataSizeOfVariable (Variable));
660
661 //
662 // Be careful about pad size for alignment.
663 //
664 return (VARIABLE_HEADER *) HEADER_ALIGN (Value);
665 }
666
667 /**
668
669 Gets the pointer to the first variable header in given variable store area.
670
671 @param VarStoreHeader Pointer to the Variable Store Header.
672
673 @return Pointer to the first variable header.
674
675 **/
676 VARIABLE_HEADER *
677 GetStartPointer (
678 IN VARIABLE_STORE_HEADER *VarStoreHeader
679 )
680 {
681 //
682 // The start of variable store.
683 //
684 return (VARIABLE_HEADER *) HEADER_ALIGN (VarStoreHeader + 1);
685 }
686
687 /**
688
689 Gets the pointer to the end of the variable storage area.
690
691 This function gets pointer to the end of the variable storage
692 area, according to the input variable store header.
693
694 @param VarStoreHeader Pointer to the Variable Store Header.
695
696 @return Pointer to the end of the variable storage area.
697
698 **/
699 VARIABLE_HEADER *
700 GetEndPointer (
701 IN VARIABLE_STORE_HEADER *VarStoreHeader
702 )
703 {
704 //
705 // The end of variable store
706 //
707 return (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) VarStoreHeader + VarStoreHeader->Size);
708 }
709
710 /**
711 Record variable error flag.
712
713 @param[in] Flag Variable error flag to record.
714 @param[in] VariableName Name of variable.
715 @param[in] VendorGuid Guid of variable.
716 @param[in] Attributes Attributes of the variable.
717 @param[in] VariableSize Size of the variable.
718
719 **/
720 VOID
721 RecordVarErrorFlag (
722 IN VAR_ERROR_FLAG Flag,
723 IN CHAR16 *VariableName,
724 IN EFI_GUID *VendorGuid,
725 IN UINT32 Attributes,
726 IN UINTN VariableSize
727 )
728 {
729 EFI_STATUS Status;
730 VARIABLE_POINTER_TRACK Variable;
731 VAR_ERROR_FLAG *VarErrFlag;
732 VAR_ERROR_FLAG TempFlag;
733
734 DEBUG_CODE (
735 DEBUG ((EFI_D_ERROR, "RecordVarErrorFlag (0x%02x) %s:%g - 0x%08x - 0x%x\n", Flag, VariableName, VendorGuid, Attributes, VariableSize));
736 if (Flag == VAR_ERROR_FLAG_SYSTEM_ERROR) {
737 if (AtRuntime ()) {
738 DEBUG ((EFI_D_ERROR, "CommonRuntimeVariableSpace = 0x%x - CommonVariableTotalSize = 0x%x\n", mVariableModuleGlobal->CommonRuntimeVariableSpace, mVariableModuleGlobal->CommonVariableTotalSize));
739 } else {
740 DEBUG ((EFI_D_ERROR, "CommonVariableSpace = 0x%x - CommonVariableTotalSize = 0x%x\n", mVariableModuleGlobal->CommonVariableSpace, mVariableModuleGlobal->CommonVariableTotalSize));
741 }
742 } else {
743 DEBUG ((EFI_D_ERROR, "CommonMaxUserVariableSpace = 0x%x - CommonUserVariableTotalSize = 0x%x\n", mVariableModuleGlobal->CommonMaxUserVariableSpace, mVariableModuleGlobal->CommonUserVariableTotalSize));
744 }
745 );
746
747 if (!mEndOfDxe) {
748 //
749 // Before EndOfDxe, just record the current boot variable error flag to local variable,
750 // and leave the variable error flag in NV flash as the last boot variable error flag.
751 // After EndOfDxe in InitializeVarErrorFlag (), the variable error flag in NV flash
752 // will be initialized to this local current boot variable error flag.
753 //
754 mCurrentBootVarErrFlag &= Flag;
755 return;
756 }
757
758 //
759 // Record error flag (it should have be initialized).
760 //
761 Status = FindVariable (
762 VAR_ERROR_FLAG_NAME,
763 &gEdkiiVarErrorFlagGuid,
764 &Variable,
765 &mVariableModuleGlobal->VariableGlobal,
766 FALSE
767 );
768 if (!EFI_ERROR (Status)) {
769 VarErrFlag = (VAR_ERROR_FLAG *) GetVariableDataPtr (Variable.CurrPtr);
770 TempFlag = *VarErrFlag;
771 TempFlag &= Flag;
772 if (TempFlag == *VarErrFlag) {
773 return;
774 }
775 Status = UpdateVariableStore (
776 &mVariableModuleGlobal->VariableGlobal,
777 FALSE,
778 FALSE,
779 mVariableModuleGlobal->FvbInstance,
780 (UINTN) VarErrFlag - (UINTN) mNvVariableCache + (UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,
781 sizeof (TempFlag),
782 &TempFlag
783 );
784 if (!EFI_ERROR (Status)) {
785 //
786 // Update the data in NV cache.
787 //
788 *VarErrFlag = TempFlag;
789 }
790 }
791 }
792
793 /**
794 Initialize variable error flag.
795
796 Before EndOfDxe, the variable indicates the last boot variable error flag,
797 then it means the last boot variable error flag must be got before EndOfDxe.
798 After EndOfDxe, the variable indicates the current boot variable error flag,
799 then it means the current boot variable error flag must be got after EndOfDxe.
800
801 **/
802 VOID
803 InitializeVarErrorFlag (
804 VOID
805 )
806 {
807 EFI_STATUS Status;
808 VARIABLE_POINTER_TRACK Variable;
809 VAR_ERROR_FLAG Flag;
810 VAR_ERROR_FLAG VarErrFlag;
811
812 if (!mEndOfDxe) {
813 return;
814 }
815
816 Flag = mCurrentBootVarErrFlag;
817 DEBUG ((EFI_D_INFO, "Initialize variable error flag (%02x)\n", Flag));
818
819 Status = FindVariable (
820 VAR_ERROR_FLAG_NAME,
821 &gEdkiiVarErrorFlagGuid,
822 &Variable,
823 &mVariableModuleGlobal->VariableGlobal,
824 FALSE
825 );
826 if (!EFI_ERROR (Status)) {
827 VarErrFlag = *((VAR_ERROR_FLAG *) GetVariableDataPtr (Variable.CurrPtr));
828 if (VarErrFlag == Flag) {
829 return;
830 }
831 }
832
833 UpdateVariable (
834 VAR_ERROR_FLAG_NAME,
835 &gEdkiiVarErrorFlagGuid,
836 &Flag,
837 sizeof (Flag),
838 VARIABLE_ATTRIBUTE_NV_BS_RT,
839 0,
840 0,
841 &Variable,
842 NULL
843 );
844 }
845
846 /**
847 Is user variable?
848
849 @param[in] Variable Pointer to variable header.
850
851 @retval TRUE User variable.
852 @retval FALSE System variable.
853
854 **/
855 BOOLEAN
856 IsUserVariable (
857 IN VARIABLE_HEADER *Variable
858 )
859 {
860 VAR_CHECK_VARIABLE_PROPERTY Property;
861
862 //
863 // Only after End Of Dxe, the variables belong to system variable are fixed.
864 // If PcdMaxUserNvStorageVariableSize is 0, it means user variable share the same NV storage with system variable,
865 // then no need to check if the variable is user variable or not specially.
866 //
867 if (mEndOfDxe && (mVariableModuleGlobal->CommonMaxUserVariableSpace != mVariableModuleGlobal->CommonVariableSpace)) {
868 if (VarCheckLibVariablePropertyGet (GetVariableNamePtr (Variable), GetVendorGuidPtr (Variable), &Property) == EFI_NOT_FOUND) {
869 return TRUE;
870 }
871 }
872 return FALSE;
873 }
874
875 /**
876 Calculate common user variable total size.
877
878 **/
879 VOID
880 CalculateCommonUserVariableTotalSize (
881 VOID
882 )
883 {
884 VARIABLE_HEADER *Variable;
885 VARIABLE_HEADER *NextVariable;
886 UINTN VariableSize;
887 VAR_CHECK_VARIABLE_PROPERTY Property;
888
889 //
890 // Only after End Of Dxe, the variables belong to system variable are fixed.
891 // If PcdMaxUserNvStorageVariableSize is 0, it means user variable share the same NV storage with system variable,
892 // then no need to calculate the common user variable total size specially.
893 //
894 if (mEndOfDxe && (mVariableModuleGlobal->CommonMaxUserVariableSpace != mVariableModuleGlobal->CommonVariableSpace)) {
895 Variable = GetStartPointer (mNvVariableCache);
896 while (IsValidVariableHeader (Variable, GetEndPointer (mNvVariableCache))) {
897 NextVariable = GetNextVariablePtr (Variable);
898 VariableSize = (UINTN) NextVariable - (UINTN) Variable;
899 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
900 if (VarCheckLibVariablePropertyGet (GetVariableNamePtr (Variable), GetVendorGuidPtr (Variable), &Property) == EFI_NOT_FOUND) {
901 //
902 // No property, it is user variable.
903 //
904 mVariableModuleGlobal->CommonUserVariableTotalSize += VariableSize;
905 }
906 }
907
908 Variable = NextVariable;
909 }
910 }
911 }
912
913 /**
914 Initialize variable quota.
915
916 **/
917 VOID
918 InitializeVariableQuota (
919 VOID
920 )
921 {
922 if (!mEndOfDxe) {
923 return;
924 }
925
926 InitializeVarErrorFlag ();
927 CalculateCommonUserVariableTotalSize ();
928 }
929
930 /**
931
932 Variable store garbage collection and reclaim operation.
933
934 @param[in] VariableBase Base address of variable store.
935 @param[out] LastVariableOffset Offset of last variable.
936 @param[in] IsVolatile The variable store is volatile or not;
937 if it is non-volatile, need FTW.
938 @param[in, out] UpdatingPtrTrack Pointer to updating variable pointer track structure.
939 @param[in] NewVariable Pointer to new variable.
940 @param[in] NewVariableSize New variable size.
941
942 @return EFI_SUCCESS Reclaim operation has finished successfully.
943 @return EFI_OUT_OF_RESOURCES No enough memory resources or variable space.
944 @return Others Unexpect error happened during reclaim operation.
945
946 **/
947 EFI_STATUS
948 Reclaim (
949 IN EFI_PHYSICAL_ADDRESS VariableBase,
950 OUT UINTN *LastVariableOffset,
951 IN BOOLEAN IsVolatile,
952 IN OUT VARIABLE_POINTER_TRACK *UpdatingPtrTrack,
953 IN VARIABLE_HEADER *NewVariable,
954 IN UINTN NewVariableSize
955 )
956 {
957 VARIABLE_HEADER *Variable;
958 VARIABLE_HEADER *AddedVariable;
959 VARIABLE_HEADER *NextVariable;
960 VARIABLE_HEADER *NextAddedVariable;
961 VARIABLE_STORE_HEADER *VariableStoreHeader;
962 UINT8 *ValidBuffer;
963 UINTN MaximumBufferSize;
964 UINTN VariableSize;
965 UINTN NameSize;
966 UINT8 *CurrPtr;
967 VOID *Point0;
968 VOID *Point1;
969 BOOLEAN FoundAdded;
970 EFI_STATUS Status;
971 UINTN CommonVariableTotalSize;
972 UINTN CommonUserVariableTotalSize;
973 UINTN HwErrVariableTotalSize;
974 VARIABLE_HEADER *UpdatingVariable;
975 VARIABLE_HEADER *UpdatingInDeletedTransition;
976
977 UpdatingVariable = NULL;
978 UpdatingInDeletedTransition = NULL;
979 if (UpdatingPtrTrack != NULL) {
980 UpdatingVariable = UpdatingPtrTrack->CurrPtr;
981 UpdatingInDeletedTransition = UpdatingPtrTrack->InDeletedTransitionPtr;
982 }
983
984 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) VariableBase);
985
986 CommonVariableTotalSize = 0;
987 CommonUserVariableTotalSize = 0;
988 HwErrVariableTotalSize = 0;
989
990 if (IsVolatile) {
991 //
992 // Start Pointers for the variable.
993 //
994 Variable = GetStartPointer (VariableStoreHeader);
995 MaximumBufferSize = sizeof (VARIABLE_STORE_HEADER);
996
997 while (IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))) {
998 NextVariable = GetNextVariablePtr (Variable);
999 if ((Variable->State == VAR_ADDED || Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) &&
1000 Variable != UpdatingVariable &&
1001 Variable != UpdatingInDeletedTransition
1002 ) {
1003 VariableSize = (UINTN) NextVariable - (UINTN) Variable;
1004 MaximumBufferSize += VariableSize;
1005 }
1006
1007 Variable = NextVariable;
1008 }
1009
1010 if (NewVariable != NULL) {
1011 //
1012 // Add the new variable size.
1013 //
1014 MaximumBufferSize += NewVariableSize;
1015 }
1016
1017 //
1018 // Reserve the 1 Bytes with Oxff to identify the
1019 // end of the variable buffer.
1020 //
1021 MaximumBufferSize += 1;
1022 ValidBuffer = AllocatePool (MaximumBufferSize);
1023 if (ValidBuffer == NULL) {
1024 return EFI_OUT_OF_RESOURCES;
1025 }
1026 } else {
1027 //
1028 // For NV variable reclaim, don't allocate pool here and just use mNvVariableCache
1029 // as the buffer to reduce SMRAM consumption for SMM variable driver.
1030 //
1031 MaximumBufferSize = mNvVariableCache->Size;
1032 ValidBuffer = (UINT8 *) mNvVariableCache;
1033 }
1034
1035 SetMem (ValidBuffer, MaximumBufferSize, 0xff);
1036
1037 //
1038 // Copy variable store header.
1039 //
1040 CopyMem (ValidBuffer, VariableStoreHeader, sizeof (VARIABLE_STORE_HEADER));
1041 CurrPtr = (UINT8 *) GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer);
1042
1043 //
1044 // Reinstall all ADDED variables as long as they are not identical to Updating Variable.
1045 //
1046 Variable = GetStartPointer (VariableStoreHeader);
1047 while (IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))) {
1048 NextVariable = GetNextVariablePtr (Variable);
1049 if (Variable != UpdatingVariable && Variable->State == VAR_ADDED) {
1050 VariableSize = (UINTN) NextVariable - (UINTN) Variable;
1051 CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);
1052 CurrPtr += VariableSize;
1053 if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {
1054 HwErrVariableTotalSize += VariableSize;
1055 } else if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {
1056 CommonVariableTotalSize += VariableSize;
1057 if (IsUserVariable (Variable)) {
1058 CommonUserVariableTotalSize += VariableSize;
1059 }
1060 }
1061 }
1062 Variable = NextVariable;
1063 }
1064
1065 //
1066 // Reinstall all in delete transition variables.
1067 //
1068 Variable = GetStartPointer (VariableStoreHeader);
1069 while (IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))) {
1070 NextVariable = GetNextVariablePtr (Variable);
1071 if (Variable != UpdatingVariable && Variable != UpdatingInDeletedTransition && Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
1072
1073 //
1074 // Buffer has cached all ADDED variable.
1075 // Per IN_DELETED variable, we have to guarantee that
1076 // no ADDED one in previous buffer.
1077 //
1078
1079 FoundAdded = FALSE;
1080 AddedVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer);
1081 while (IsValidVariableHeader (AddedVariable, GetEndPointer ((VARIABLE_STORE_HEADER *) ValidBuffer))) {
1082 NextAddedVariable = GetNextVariablePtr (AddedVariable);
1083 NameSize = NameSizeOfVariable (AddedVariable);
1084 if (CompareGuid (GetVendorGuidPtr (AddedVariable), GetVendorGuidPtr (Variable)) &&
1085 NameSize == NameSizeOfVariable (Variable)
1086 ) {
1087 Point0 = (VOID *) GetVariableNamePtr (AddedVariable);
1088 Point1 = (VOID *) GetVariableNamePtr (Variable);
1089 if (CompareMem (Point0, Point1, NameSize) == 0) {
1090 FoundAdded = TRUE;
1091 break;
1092 }
1093 }
1094 AddedVariable = NextAddedVariable;
1095 }
1096 if (!FoundAdded) {
1097 //
1098 // Promote VAR_IN_DELETED_TRANSITION to VAR_ADDED.
1099 //
1100 VariableSize = (UINTN) NextVariable - (UINTN) Variable;
1101 CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);
1102 ((VARIABLE_HEADER *) CurrPtr)->State = VAR_ADDED;
1103 CurrPtr += VariableSize;
1104 if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {
1105 HwErrVariableTotalSize += VariableSize;
1106 } else if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {
1107 CommonVariableTotalSize += VariableSize;
1108 if (IsUserVariable (Variable)) {
1109 CommonUserVariableTotalSize += VariableSize;
1110 }
1111 }
1112 }
1113 }
1114
1115 Variable = NextVariable;
1116 }
1117
1118 //
1119 // Install the new variable if it is not NULL.
1120 //
1121 if (NewVariable != NULL) {
1122 if (((UINTN) CurrPtr - (UINTN) ValidBuffer) + NewVariableSize > VariableStoreHeader->Size) {
1123 //
1124 // No enough space to store the new variable.
1125 //
1126 Status = EFI_OUT_OF_RESOURCES;
1127 goto Done;
1128 }
1129 if (!IsVolatile) {
1130 if ((NewVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
1131 HwErrVariableTotalSize += NewVariableSize;
1132 } else if ((NewVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
1133 CommonVariableTotalSize += NewVariableSize;
1134 if (IsUserVariable (NewVariable)) {
1135 CommonUserVariableTotalSize += NewVariableSize;
1136 }
1137 }
1138 if ((HwErrVariableTotalSize > PcdGet32 (PcdHwErrStorageSize)) ||
1139 (CommonVariableTotalSize > mVariableModuleGlobal->CommonVariableSpace) ||
1140 (CommonUserVariableTotalSize > mVariableModuleGlobal->CommonMaxUserVariableSpace)) {
1141 //
1142 // No enough space to store the new variable by NV or NV+HR attribute.
1143 //
1144 Status = EFI_OUT_OF_RESOURCES;
1145 goto Done;
1146 }
1147 }
1148
1149 CopyMem (CurrPtr, (UINT8 *) NewVariable, NewVariableSize);
1150 ((VARIABLE_HEADER *) CurrPtr)->State = VAR_ADDED;
1151 if (UpdatingVariable != NULL) {
1152 UpdatingPtrTrack->CurrPtr = (VARIABLE_HEADER *)((UINTN)UpdatingPtrTrack->StartPtr + ((UINTN)CurrPtr - (UINTN)GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer)));
1153 UpdatingPtrTrack->InDeletedTransitionPtr = NULL;
1154 }
1155 CurrPtr += NewVariableSize;
1156 }
1157
1158 if (IsVolatile) {
1159 //
1160 // If volatile variable store, just copy valid buffer.
1161 //
1162 SetMem ((UINT8 *) (UINTN) VariableBase, VariableStoreHeader->Size, 0xff);
1163 CopyMem ((UINT8 *) (UINTN) VariableBase, ValidBuffer, (UINTN) CurrPtr - (UINTN) ValidBuffer);
1164 *LastVariableOffset = (UINTN) CurrPtr - (UINTN) ValidBuffer;
1165 Status = EFI_SUCCESS;
1166 } else {
1167 //
1168 // If non-volatile variable store, perform FTW here.
1169 //
1170 Status = FtwVariableSpace (
1171 VariableBase,
1172 (VARIABLE_STORE_HEADER *) ValidBuffer
1173 );
1174 if (!EFI_ERROR (Status)) {
1175 *LastVariableOffset = (UINTN) CurrPtr - (UINTN) ValidBuffer;
1176 mVariableModuleGlobal->HwErrVariableTotalSize = HwErrVariableTotalSize;
1177 mVariableModuleGlobal->CommonVariableTotalSize = CommonVariableTotalSize;
1178 mVariableModuleGlobal->CommonUserVariableTotalSize = CommonUserVariableTotalSize;
1179 } else {
1180 mVariableModuleGlobal->HwErrVariableTotalSize = 0;
1181 mVariableModuleGlobal->CommonVariableTotalSize = 0;
1182 mVariableModuleGlobal->CommonUserVariableTotalSize = 0;
1183 Variable = GetStartPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableBase);
1184 while (IsValidVariableHeader (Variable, GetEndPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableBase))) {
1185 NextVariable = GetNextVariablePtr (Variable);
1186 VariableSize = (UINTN) NextVariable - (UINTN) Variable;
1187 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
1188 mVariableModuleGlobal->HwErrVariableTotalSize += VariableSize;
1189 } else if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
1190 mVariableModuleGlobal->CommonVariableTotalSize += VariableSize;
1191 if (IsUserVariable (Variable)) {
1192 mVariableModuleGlobal->CommonUserVariableTotalSize += VariableSize;
1193 }
1194 }
1195
1196 Variable = NextVariable;
1197 }
1198 *LastVariableOffset = (UINTN) Variable - (UINTN) VariableBase;
1199 }
1200 }
1201
1202 Done:
1203 if (IsVolatile) {
1204 FreePool (ValidBuffer);
1205 } else {
1206 //
1207 // For NV variable reclaim, we use mNvVariableCache as the buffer, so copy the data back.
1208 //
1209 CopyMem (mNvVariableCache, (UINT8 *)(UINTN)VariableBase, VariableStoreHeader->Size);
1210 }
1211
1212 return Status;
1213 }
1214
1215 /**
1216 Find the variable in the specified variable store.
1217
1218 @param[in] VariableName Name of the variable to be found
1219 @param[in] VendorGuid Vendor GUID to be found.
1220 @param[in] IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
1221 check at runtime when searching variable.
1222 @param[in, out] PtrTrack Variable Track Pointer structure that contains Variable Information.
1223
1224 @retval EFI_SUCCESS Variable found successfully
1225 @retval EFI_NOT_FOUND Variable not found
1226 **/
1227 EFI_STATUS
1228 FindVariableEx (
1229 IN CHAR16 *VariableName,
1230 IN EFI_GUID *VendorGuid,
1231 IN BOOLEAN IgnoreRtCheck,
1232 IN OUT VARIABLE_POINTER_TRACK *PtrTrack
1233 )
1234 {
1235 VARIABLE_HEADER *InDeletedVariable;
1236 VOID *Point;
1237
1238 PtrTrack->InDeletedTransitionPtr = NULL;
1239
1240 //
1241 // Find the variable by walk through HOB, volatile and non-volatile variable store.
1242 //
1243 InDeletedVariable = NULL;
1244
1245 for ( PtrTrack->CurrPtr = PtrTrack->StartPtr
1246 ; IsValidVariableHeader (PtrTrack->CurrPtr, PtrTrack->EndPtr)
1247 ; PtrTrack->CurrPtr = GetNextVariablePtr (PtrTrack->CurrPtr)
1248 ) {
1249 if (PtrTrack->CurrPtr->State == VAR_ADDED ||
1250 PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)
1251 ) {
1252 if (IgnoreRtCheck || !AtRuntime () || ((PtrTrack->CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {
1253 if (VariableName[0] == 0) {
1254 if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
1255 InDeletedVariable = PtrTrack->CurrPtr;
1256 } else {
1257 PtrTrack->InDeletedTransitionPtr = InDeletedVariable;
1258 return EFI_SUCCESS;
1259 }
1260 } else {
1261 if (CompareGuid (VendorGuid, GetVendorGuidPtr (PtrTrack->CurrPtr))) {
1262 Point = (VOID *) GetVariableNamePtr (PtrTrack->CurrPtr);
1263
1264 ASSERT (NameSizeOfVariable (PtrTrack->CurrPtr) != 0);
1265 if (CompareMem (VariableName, Point, NameSizeOfVariable (PtrTrack->CurrPtr)) == 0) {
1266 if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
1267 InDeletedVariable = PtrTrack->CurrPtr;
1268 } else {
1269 PtrTrack->InDeletedTransitionPtr = InDeletedVariable;
1270 return EFI_SUCCESS;
1271 }
1272 }
1273 }
1274 }
1275 }
1276 }
1277 }
1278
1279 PtrTrack->CurrPtr = InDeletedVariable;
1280 return (PtrTrack->CurrPtr == NULL) ? EFI_NOT_FOUND : EFI_SUCCESS;
1281 }
1282
1283
1284 /**
1285 Finds variable in storage blocks of volatile and non-volatile storage areas.
1286
1287 This code finds variable in storage blocks of volatile and non-volatile storage areas.
1288 If VariableName is an empty string, then we just return the first
1289 qualified variable without comparing VariableName and VendorGuid.
1290 If IgnoreRtCheck is TRUE, then we ignore the EFI_VARIABLE_RUNTIME_ACCESS attribute check
1291 at runtime when searching existing variable, only VariableName and VendorGuid are compared.
1292 Otherwise, variables without EFI_VARIABLE_RUNTIME_ACCESS are not visible at runtime.
1293
1294 @param[in] VariableName Name of the variable to be found.
1295 @param[in] VendorGuid Vendor GUID to be found.
1296 @param[out] PtrTrack VARIABLE_POINTER_TRACK structure for output,
1297 including the range searched and the target position.
1298 @param[in] Global Pointer to VARIABLE_GLOBAL structure, including
1299 base of volatile variable storage area, base of
1300 NV variable storage area, and a lock.
1301 @param[in] IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
1302 check at runtime when searching variable.
1303
1304 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while
1305 VendorGuid is NULL.
1306 @retval EFI_SUCCESS Variable successfully found.
1307 @retval EFI_NOT_FOUND Variable not found
1308
1309 **/
1310 EFI_STATUS
1311 FindVariable (
1312 IN CHAR16 *VariableName,
1313 IN EFI_GUID *VendorGuid,
1314 OUT VARIABLE_POINTER_TRACK *PtrTrack,
1315 IN VARIABLE_GLOBAL *Global,
1316 IN BOOLEAN IgnoreRtCheck
1317 )
1318 {
1319 EFI_STATUS Status;
1320 VARIABLE_STORE_HEADER *VariableStoreHeader[VariableStoreTypeMax];
1321 VARIABLE_STORE_TYPE Type;
1322
1323 if (VariableName[0] != 0 && VendorGuid == NULL) {
1324 return EFI_INVALID_PARAMETER;
1325 }
1326
1327 //
1328 // 0: Volatile, 1: HOB, 2: Non-Volatile.
1329 // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName
1330 // make use of this mapping to implement search algorithm.
1331 //
1332 VariableStoreHeader[VariableStoreTypeVolatile] = (VARIABLE_STORE_HEADER *) (UINTN) Global->VolatileVariableBase;
1333 VariableStoreHeader[VariableStoreTypeHob] = (VARIABLE_STORE_HEADER *) (UINTN) Global->HobVariableBase;
1334 VariableStoreHeader[VariableStoreTypeNv] = mNvVariableCache;
1335
1336 //
1337 // Find the variable by walk through HOB, volatile and non-volatile variable store.
1338 //
1339 for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {
1340 if (VariableStoreHeader[Type] == NULL) {
1341 continue;
1342 }
1343
1344 PtrTrack->StartPtr = GetStartPointer (VariableStoreHeader[Type]);
1345 PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader[Type]);
1346 PtrTrack->Volatile = (BOOLEAN) (Type == VariableStoreTypeVolatile);
1347
1348 Status = FindVariableEx (VariableName, VendorGuid, IgnoreRtCheck, PtrTrack);
1349 if (!EFI_ERROR (Status)) {
1350 return Status;
1351 }
1352 }
1353 return EFI_NOT_FOUND;
1354 }
1355
1356 /**
1357 Get index from supported language codes according to language string.
1358
1359 This code is used to get corresponding index in supported language codes. It can handle
1360 RFC4646 and ISO639 language tags.
1361 In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.
1362 In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.
1363
1364 For example:
1365 SupportedLang = "engfraengfra"
1366 Lang = "eng"
1367 Iso639Language = TRUE
1368 The return value is "0".
1369 Another example:
1370 SupportedLang = "en;fr;en-US;fr-FR"
1371 Lang = "fr-FR"
1372 Iso639Language = FALSE
1373 The return value is "3".
1374
1375 @param SupportedLang Platform supported language codes.
1376 @param Lang Configured language.
1377 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
1378
1379 @retval The index of language in the language codes.
1380
1381 **/
1382 UINTN
1383 GetIndexFromSupportedLangCodes(
1384 IN CHAR8 *SupportedLang,
1385 IN CHAR8 *Lang,
1386 IN BOOLEAN Iso639Language
1387 )
1388 {
1389 UINTN Index;
1390 UINTN CompareLength;
1391 UINTN LanguageLength;
1392
1393 if (Iso639Language) {
1394 CompareLength = ISO_639_2_ENTRY_SIZE;
1395 for (Index = 0; Index < AsciiStrLen (SupportedLang); Index += CompareLength) {
1396 if (AsciiStrnCmp (Lang, SupportedLang + Index, CompareLength) == 0) {
1397 //
1398 // Successfully find the index of Lang string in SupportedLang string.
1399 //
1400 Index = Index / CompareLength;
1401 return Index;
1402 }
1403 }
1404 ASSERT (FALSE);
1405 return 0;
1406 } else {
1407 //
1408 // Compare RFC4646 language code
1409 //
1410 Index = 0;
1411 for (LanguageLength = 0; Lang[LanguageLength] != '\0'; LanguageLength++);
1412
1413 for (Index = 0; *SupportedLang != '\0'; Index++, SupportedLang += CompareLength) {
1414 //
1415 // Skip ';' characters in SupportedLang
1416 //
1417 for (; *SupportedLang != '\0' && *SupportedLang == ';'; SupportedLang++);
1418 //
1419 // Determine the length of the next language code in SupportedLang
1420 //
1421 for (CompareLength = 0; SupportedLang[CompareLength] != '\0' && SupportedLang[CompareLength] != ';'; CompareLength++);
1422
1423 if ((CompareLength == LanguageLength) &&
1424 (AsciiStrnCmp (Lang, SupportedLang, CompareLength) == 0)) {
1425 //
1426 // Successfully find the index of Lang string in SupportedLang string.
1427 //
1428 return Index;
1429 }
1430 }
1431 ASSERT (FALSE);
1432 return 0;
1433 }
1434 }
1435
1436 /**
1437 Get language string from supported language codes according to index.
1438
1439 This code is used to get corresponding language strings in supported language codes. It can handle
1440 RFC4646 and ISO639 language tags.
1441 In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.
1442 In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.
1443
1444 For example:
1445 SupportedLang = "engfraengfra"
1446 Index = "1"
1447 Iso639Language = TRUE
1448 The return value is "fra".
1449 Another example:
1450 SupportedLang = "en;fr;en-US;fr-FR"
1451 Index = "1"
1452 Iso639Language = FALSE
1453 The return value is "fr".
1454
1455 @param SupportedLang Platform supported language codes.
1456 @param Index The index in supported language codes.
1457 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
1458
1459 @retval The language string in the language codes.
1460
1461 **/
1462 CHAR8 *
1463 GetLangFromSupportedLangCodes (
1464 IN CHAR8 *SupportedLang,
1465 IN UINTN Index,
1466 IN BOOLEAN Iso639Language
1467 )
1468 {
1469 UINTN SubIndex;
1470 UINTN CompareLength;
1471 CHAR8 *Supported;
1472
1473 SubIndex = 0;
1474 Supported = SupportedLang;
1475 if (Iso639Language) {
1476 //
1477 // According to the index of Lang string in SupportedLang string to get the language.
1478 // This code will be invoked in RUNTIME, therefore there is not a memory allocate/free operation.
1479 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
1480 //
1481 CompareLength = ISO_639_2_ENTRY_SIZE;
1482 mVariableModuleGlobal->Lang[CompareLength] = '\0';
1483 return CopyMem (mVariableModuleGlobal->Lang, SupportedLang + Index * CompareLength, CompareLength);
1484
1485 } else {
1486 while (TRUE) {
1487 //
1488 // Take semicolon as delimitation, sequentially traverse supported language codes.
1489 //
1490 for (CompareLength = 0; *Supported != ';' && *Supported != '\0'; CompareLength++) {
1491 Supported++;
1492 }
1493 if ((*Supported == '\0') && (SubIndex != Index)) {
1494 //
1495 // Have completed the traverse, but not find corrsponding string.
1496 // This case is not allowed to happen.
1497 //
1498 ASSERT(FALSE);
1499 return NULL;
1500 }
1501 if (SubIndex == Index) {
1502 //
1503 // According to the index of Lang string in SupportedLang string to get the language.
1504 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.
1505 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
1506 //
1507 mVariableModuleGlobal->PlatformLang[CompareLength] = '\0';
1508 return CopyMem (mVariableModuleGlobal->PlatformLang, Supported - CompareLength, CompareLength);
1509 }
1510 SubIndex++;
1511
1512 //
1513 // Skip ';' characters in Supported
1514 //
1515 for (; *Supported != '\0' && *Supported == ';'; Supported++);
1516 }
1517 }
1518 }
1519
1520 /**
1521 Returns a pointer to an allocated buffer that contains the best matching language
1522 from a set of supported languages.
1523
1524 This function supports both ISO 639-2 and RFC 4646 language codes, but language
1525 code types may not be mixed in a single call to this function. This function
1526 supports a variable argument list that allows the caller to pass in a prioritized
1527 list of language codes to test against all the language codes in SupportedLanguages.
1528
1529 If SupportedLanguages is NULL, then ASSERT().
1530
1531 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that
1532 contains a set of language codes in the format
1533 specified by Iso639Language.
1534 @param[in] Iso639Language If not zero, then all language codes are assumed to be
1535 in ISO 639-2 format. If zero, then all language
1536 codes are assumed to be in RFC 4646 language format
1537 @param[in] ... A variable argument list that contains pointers to
1538 Null-terminated ASCII strings that contain one or more
1539 language codes in the format specified by Iso639Language.
1540 The first language code from each of these language
1541 code lists is used to determine if it is an exact or
1542 close match to any of the language codes in
1543 SupportedLanguages. Close matches only apply to RFC 4646
1544 language codes, and the matching algorithm from RFC 4647
1545 is used to determine if a close match is present. If
1546 an exact or close match is found, then the matching
1547 language code from SupportedLanguages is returned. If
1548 no matches are found, then the next variable argument
1549 parameter is evaluated. The variable argument list
1550 is terminated by a NULL.
1551
1552 @retval NULL The best matching language could not be found in SupportedLanguages.
1553 @retval NULL There are not enough resources available to return the best matching
1554 language.
1555 @retval Other A pointer to a Null-terminated ASCII string that is the best matching
1556 language in SupportedLanguages.
1557
1558 **/
1559 CHAR8 *
1560 EFIAPI
1561 VariableGetBestLanguage (
1562 IN CONST CHAR8 *SupportedLanguages,
1563 IN UINTN Iso639Language,
1564 ...
1565 )
1566 {
1567 VA_LIST Args;
1568 CHAR8 *Language;
1569 UINTN CompareLength;
1570 UINTN LanguageLength;
1571 CONST CHAR8 *Supported;
1572 CHAR8 *Buffer;
1573
1574 if (SupportedLanguages == NULL) {
1575 return NULL;
1576 }
1577
1578 VA_START (Args, Iso639Language);
1579 while ((Language = VA_ARG (Args, CHAR8 *)) != NULL) {
1580 //
1581 // Default to ISO 639-2 mode
1582 //
1583 CompareLength = 3;
1584 LanguageLength = MIN (3, AsciiStrLen (Language));
1585
1586 //
1587 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language
1588 //
1589 if (Iso639Language == 0) {
1590 for (LanguageLength = 0; Language[LanguageLength] != 0 && Language[LanguageLength] != ';'; LanguageLength++);
1591 }
1592
1593 //
1594 // Trim back the length of Language used until it is empty
1595 //
1596 while (LanguageLength > 0) {
1597 //
1598 // Loop through all language codes in SupportedLanguages
1599 //
1600 for (Supported = SupportedLanguages; *Supported != '\0'; Supported += CompareLength) {
1601 //
1602 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages
1603 //
1604 if (Iso639Language == 0) {
1605 //
1606 // Skip ';' characters in Supported
1607 //
1608 for (; *Supported != '\0' && *Supported == ';'; Supported++);
1609 //
1610 // Determine the length of the next language code in Supported
1611 //
1612 for (CompareLength = 0; Supported[CompareLength] != 0 && Supported[CompareLength] != ';'; CompareLength++);
1613 //
1614 // If Language is longer than the Supported, then skip to the next language
1615 //
1616 if (LanguageLength > CompareLength) {
1617 continue;
1618 }
1619 }
1620 //
1621 // See if the first LanguageLength characters in Supported match Language
1622 //
1623 if (AsciiStrnCmp (Supported, Language, LanguageLength) == 0) {
1624 VA_END (Args);
1625
1626 Buffer = (Iso639Language != 0) ? mVariableModuleGlobal->Lang : mVariableModuleGlobal->PlatformLang;
1627 Buffer[CompareLength] = '\0';
1628 return CopyMem (Buffer, Supported, CompareLength);
1629 }
1630 }
1631
1632 if (Iso639Language != 0) {
1633 //
1634 // If ISO 639 mode, then each language can only be tested once
1635 //
1636 LanguageLength = 0;
1637 } else {
1638 //
1639 // If RFC 4646 mode, then trim Language from the right to the next '-' character
1640 //
1641 for (LanguageLength--; LanguageLength > 0 && Language[LanguageLength] != '-'; LanguageLength--);
1642 }
1643 }
1644 }
1645 VA_END (Args);
1646
1647 //
1648 // No matches were found
1649 //
1650 return NULL;
1651 }
1652
1653 /**
1654 This function is to check if the remaining variable space is enough to set
1655 all Variables from argument list successfully. The purpose of the check
1656 is to keep the consistency of the Variables to be in variable storage.
1657
1658 Note: Variables are assumed to be in same storage.
1659 The set sequence of Variables will be same with the sequence of VariableEntry from argument list,
1660 so follow the argument sequence to check the Variables.
1661
1662 @param[in] Attributes Variable attributes for Variable entries.
1663 @param[in] Marker VA_LIST style variable argument list.
1664 The variable argument list with type VARIABLE_ENTRY_CONSISTENCY *.
1665 A NULL terminates the list. The VariableSize of
1666 VARIABLE_ENTRY_CONSISTENCY is the variable data size as input.
1667 It will be changed to variable total size as output.
1668
1669 @retval TRUE Have enough variable space to set the Variables successfully.
1670 @retval FALSE No enough variable space to set the Variables successfully.
1671
1672 **/
1673 BOOLEAN
1674 EFIAPI
1675 CheckRemainingSpaceForConsistencyInternal (
1676 IN UINT32 Attributes,
1677 IN VA_LIST Marker
1678 )
1679 {
1680 EFI_STATUS Status;
1681 VA_LIST Args;
1682 VARIABLE_ENTRY_CONSISTENCY *VariableEntry;
1683 UINT64 MaximumVariableStorageSize;
1684 UINT64 RemainingVariableStorageSize;
1685 UINT64 MaximumVariableSize;
1686 UINTN TotalNeededSize;
1687 UINTN OriginalVarSize;
1688 VARIABLE_STORE_HEADER *VariableStoreHeader;
1689 VARIABLE_POINTER_TRACK VariablePtrTrack;
1690 VARIABLE_HEADER *NextVariable;
1691 UINTN VarNameSize;
1692 UINTN VarDataSize;
1693
1694 //
1695 // Non-Volatile related.
1696 //
1697 VariableStoreHeader = mNvVariableCache;
1698
1699 Status = VariableServiceQueryVariableInfoInternal (
1700 Attributes,
1701 &MaximumVariableStorageSize,
1702 &RemainingVariableStorageSize,
1703 &MaximumVariableSize
1704 );
1705 ASSERT_EFI_ERROR (Status);
1706
1707 TotalNeededSize = 0;
1708 VA_COPY (Args, Marker);
1709 VariableEntry = VA_ARG (Args, VARIABLE_ENTRY_CONSISTENCY *);
1710 while (VariableEntry != NULL) {
1711 //
1712 // Calculate variable total size.
1713 //
1714 VarNameSize = StrSize (VariableEntry->Name);
1715 VarNameSize += GET_PAD_SIZE (VarNameSize);
1716 VarDataSize = VariableEntry->VariableSize;
1717 VarDataSize += GET_PAD_SIZE (VarDataSize);
1718 VariableEntry->VariableSize = HEADER_ALIGN (GetVariableHeaderSize () + VarNameSize + VarDataSize);
1719
1720 TotalNeededSize += VariableEntry->VariableSize;
1721 VariableEntry = VA_ARG (Args, VARIABLE_ENTRY_CONSISTENCY *);
1722 }
1723 VA_END (Args);
1724
1725 if (RemainingVariableStorageSize >= TotalNeededSize) {
1726 //
1727 // Already have enough space.
1728 //
1729 return TRUE;
1730 } else if (AtRuntime ()) {
1731 //
1732 // At runtime, no reclaim.
1733 // The original variable space of Variables can't be reused.
1734 //
1735 return FALSE;
1736 }
1737
1738 VA_COPY (Args, Marker);
1739 VariableEntry = VA_ARG (Args, VARIABLE_ENTRY_CONSISTENCY *);
1740 while (VariableEntry != NULL) {
1741 //
1742 // Check if Variable[Index] has been present and get its size.
1743 //
1744 OriginalVarSize = 0;
1745 VariablePtrTrack.StartPtr = GetStartPointer (VariableStoreHeader);
1746 VariablePtrTrack.EndPtr = GetEndPointer (VariableStoreHeader);
1747 Status = FindVariableEx (
1748 VariableEntry->Name,
1749 VariableEntry->Guid,
1750 FALSE,
1751 &VariablePtrTrack
1752 );
1753 if (!EFI_ERROR (Status)) {
1754 //
1755 // Get size of Variable[Index].
1756 //
1757 NextVariable = GetNextVariablePtr (VariablePtrTrack.CurrPtr);
1758 OriginalVarSize = (UINTN) NextVariable - (UINTN) VariablePtrTrack.CurrPtr;
1759 //
1760 // Add the original size of Variable[Index] to remaining variable storage size.
1761 //
1762 RemainingVariableStorageSize += OriginalVarSize;
1763 }
1764 if (VariableEntry->VariableSize > RemainingVariableStorageSize) {
1765 //
1766 // No enough space for Variable[Index].
1767 //
1768 VA_END (Args);
1769 return FALSE;
1770 }
1771 //
1772 // Sub the (new) size of Variable[Index] from remaining variable storage size.
1773 //
1774 RemainingVariableStorageSize -= VariableEntry->VariableSize;
1775 VariableEntry = VA_ARG (Args, VARIABLE_ENTRY_CONSISTENCY *);
1776 }
1777 VA_END (Args);
1778
1779 return TRUE;
1780 }
1781
1782 /**
1783 This function is to check if the remaining variable space is enough to set
1784 all Variables from argument list successfully. The purpose of the check
1785 is to keep the consistency of the Variables to be in variable storage.
1786
1787 Note: Variables are assumed to be in same storage.
1788 The set sequence of Variables will be same with the sequence of VariableEntry from argument list,
1789 so follow the argument sequence to check the Variables.
1790
1791 @param[in] Attributes Variable attributes for Variable entries.
1792 @param ... The variable argument list with type VARIABLE_ENTRY_CONSISTENCY *.
1793 A NULL terminates the list. The VariableSize of
1794 VARIABLE_ENTRY_CONSISTENCY is the variable data size as input.
1795 It will be changed to variable total size as output.
1796
1797 @retval TRUE Have enough variable space to set the Variables successfully.
1798 @retval FALSE No enough variable space to set the Variables successfully.
1799
1800 **/
1801 BOOLEAN
1802 EFIAPI
1803 CheckRemainingSpaceForConsistency (
1804 IN UINT32 Attributes,
1805 ...
1806 )
1807 {
1808 VA_LIST Marker;
1809 BOOLEAN Return;
1810
1811 VA_START (Marker, Attributes);
1812
1813 Return = CheckRemainingSpaceForConsistencyInternal (Attributes, Marker);
1814
1815 VA_END (Marker);
1816
1817 return Return;
1818 }
1819
1820 /**
1821 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.
1822
1823 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.
1824
1825 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,
1826 and are read-only. Therefore, in variable driver, only store the original value for other use.
1827
1828 @param[in] VariableName Name of variable.
1829
1830 @param[in] Data Variable data.
1831
1832 @param[in] DataSize Size of data. 0 means delete.
1833
1834 @retval EFI_SUCCESS The update operation is successful or ignored.
1835 @retval EFI_WRITE_PROTECTED Update PlatformLangCodes/LangCodes at runtime.
1836 @retval EFI_OUT_OF_RESOURCES No enough variable space to do the update operation.
1837 @retval Others Other errors happened during the update operation.
1838
1839 **/
1840 EFI_STATUS
1841 AutoUpdateLangVariable (
1842 IN CHAR16 *VariableName,
1843 IN VOID *Data,
1844 IN UINTN DataSize
1845 )
1846 {
1847 EFI_STATUS Status;
1848 CHAR8 *BestPlatformLang;
1849 CHAR8 *BestLang;
1850 UINTN Index;
1851 UINT32 Attributes;
1852 VARIABLE_POINTER_TRACK Variable;
1853 BOOLEAN SetLanguageCodes;
1854 VARIABLE_ENTRY_CONSISTENCY VariableEntry[2];
1855
1856 //
1857 // Don't do updates for delete operation
1858 //
1859 if (DataSize == 0) {
1860 return EFI_SUCCESS;
1861 }
1862
1863 SetLanguageCodes = FALSE;
1864
1865 if (StrCmp (VariableName, EFI_PLATFORM_LANG_CODES_VARIABLE_NAME) == 0) {
1866 //
1867 // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.
1868 //
1869 if (AtRuntime ()) {
1870 return EFI_WRITE_PROTECTED;
1871 }
1872
1873 SetLanguageCodes = TRUE;
1874
1875 //
1876 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only
1877 // Therefore, in variable driver, only store the original value for other use.
1878 //
1879 if (mVariableModuleGlobal->PlatformLangCodes != NULL) {
1880 FreePool (mVariableModuleGlobal->PlatformLangCodes);
1881 }
1882 mVariableModuleGlobal->PlatformLangCodes = AllocateRuntimeCopyPool (DataSize, Data);
1883 ASSERT (mVariableModuleGlobal->PlatformLangCodes != NULL);
1884
1885 //
1886 // PlatformLang holds a single language from PlatformLangCodes,
1887 // so the size of PlatformLangCodes is enough for the PlatformLang.
1888 //
1889 if (mVariableModuleGlobal->PlatformLang != NULL) {
1890 FreePool (mVariableModuleGlobal->PlatformLang);
1891 }
1892 mVariableModuleGlobal->PlatformLang = AllocateRuntimePool (DataSize);
1893 ASSERT (mVariableModuleGlobal->PlatformLang != NULL);
1894
1895 } else if (StrCmp (VariableName, EFI_LANG_CODES_VARIABLE_NAME) == 0) {
1896 //
1897 // LangCodes is a volatile variable, so it can not be updated at runtime.
1898 //
1899 if (AtRuntime ()) {
1900 return EFI_WRITE_PROTECTED;
1901 }
1902
1903 SetLanguageCodes = TRUE;
1904
1905 //
1906 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only
1907 // Therefore, in variable driver, only store the original value for other use.
1908 //
1909 if (mVariableModuleGlobal->LangCodes != NULL) {
1910 FreePool (mVariableModuleGlobal->LangCodes);
1911 }
1912 mVariableModuleGlobal->LangCodes = AllocateRuntimeCopyPool (DataSize, Data);
1913 ASSERT (mVariableModuleGlobal->LangCodes != NULL);
1914 }
1915
1916 if (SetLanguageCodes
1917 && (mVariableModuleGlobal->PlatformLangCodes != NULL)
1918 && (mVariableModuleGlobal->LangCodes != NULL)) {
1919 //
1920 // Update Lang if PlatformLang is already set
1921 // Update PlatformLang if Lang is already set
1922 //
1923 Status = FindVariable (EFI_PLATFORM_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);
1924 if (!EFI_ERROR (Status)) {
1925 //
1926 // Update Lang
1927 //
1928 VariableName = EFI_PLATFORM_LANG_VARIABLE_NAME;
1929 Data = GetVariableDataPtr (Variable.CurrPtr);
1930 DataSize = DataSizeOfVariable (Variable.CurrPtr);
1931 } else {
1932 Status = FindVariable (EFI_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);
1933 if (!EFI_ERROR (Status)) {
1934 //
1935 // Update PlatformLang
1936 //
1937 VariableName = EFI_LANG_VARIABLE_NAME;
1938 Data = GetVariableDataPtr (Variable.CurrPtr);
1939 DataSize = DataSizeOfVariable (Variable.CurrPtr);
1940 } else {
1941 //
1942 // Neither PlatformLang nor Lang is set, directly return
1943 //
1944 return EFI_SUCCESS;
1945 }
1946 }
1947 }
1948
1949 Status = EFI_SUCCESS;
1950
1951 //
1952 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.
1953 //
1954 Attributes = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;
1955
1956 if (StrCmp (VariableName, EFI_PLATFORM_LANG_VARIABLE_NAME) == 0) {
1957 //
1958 // Update Lang when PlatformLangCodes/LangCodes were set.
1959 //
1960 if ((mVariableModuleGlobal->PlatformLangCodes != NULL) && (mVariableModuleGlobal->LangCodes != NULL)) {
1961 //
1962 // When setting PlatformLang, firstly get most matched language string from supported language codes.
1963 //
1964 BestPlatformLang = VariableGetBestLanguage (mVariableModuleGlobal->PlatformLangCodes, FALSE, Data, NULL);
1965 if (BestPlatformLang != NULL) {
1966 //
1967 // Get the corresponding index in language codes.
1968 //
1969 Index = GetIndexFromSupportedLangCodes (mVariableModuleGlobal->PlatformLangCodes, BestPlatformLang, FALSE);
1970
1971 //
1972 // Get the corresponding ISO639 language tag according to RFC4646 language tag.
1973 //
1974 BestLang = GetLangFromSupportedLangCodes (mVariableModuleGlobal->LangCodes, Index, TRUE);
1975
1976 //
1977 // Check the variable space for both Lang and PlatformLang variable.
1978 //
1979 VariableEntry[0].VariableSize = ISO_639_2_ENTRY_SIZE + 1;
1980 VariableEntry[0].Guid = &gEfiGlobalVariableGuid;
1981 VariableEntry[0].Name = EFI_LANG_VARIABLE_NAME;
1982
1983 VariableEntry[1].VariableSize = AsciiStrSize (BestPlatformLang);
1984 VariableEntry[1].Guid = &gEfiGlobalVariableGuid;
1985 VariableEntry[1].Name = EFI_PLATFORM_LANG_VARIABLE_NAME;
1986 if (!CheckRemainingSpaceForConsistency (VARIABLE_ATTRIBUTE_NV_BS_RT, &VariableEntry[0], &VariableEntry[1], NULL)) {
1987 //
1988 // No enough variable space to set both Lang and PlatformLang successfully.
1989 //
1990 Status = EFI_OUT_OF_RESOURCES;
1991 } else {
1992 //
1993 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.
1994 //
1995 FindVariable (EFI_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);
1996
1997 Status = UpdateVariable (EFI_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, BestLang,
1998 ISO_639_2_ENTRY_SIZE + 1, Attributes, 0, 0, &Variable, NULL);
1999 }
2000
2001 DEBUG ((EFI_D_INFO, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a Status: %r\n", BestPlatformLang, BestLang, Status));
2002 }
2003 }
2004
2005 } else if (StrCmp (VariableName, EFI_LANG_VARIABLE_NAME) == 0) {
2006 //
2007 // Update PlatformLang when PlatformLangCodes/LangCodes were set.
2008 //
2009 if ((mVariableModuleGlobal->PlatformLangCodes != NULL) && (mVariableModuleGlobal->LangCodes != NULL)) {
2010 //
2011 // When setting Lang, firstly get most matched language string from supported language codes.
2012 //
2013 BestLang = VariableGetBestLanguage (mVariableModuleGlobal->LangCodes, TRUE, Data, NULL);
2014 if (BestLang != NULL) {
2015 //
2016 // Get the corresponding index in language codes.
2017 //
2018 Index = GetIndexFromSupportedLangCodes (mVariableModuleGlobal->LangCodes, BestLang, TRUE);
2019
2020 //
2021 // Get the corresponding RFC4646 language tag according to ISO639 language tag.
2022 //
2023 BestPlatformLang = GetLangFromSupportedLangCodes (mVariableModuleGlobal->PlatformLangCodes, Index, FALSE);
2024
2025 //
2026 // Check the variable space for both PlatformLang and Lang variable.
2027 //
2028 VariableEntry[0].VariableSize = AsciiStrSize (BestPlatformLang);
2029 VariableEntry[0].Guid = &gEfiGlobalVariableGuid;
2030 VariableEntry[0].Name = EFI_PLATFORM_LANG_VARIABLE_NAME;
2031
2032 VariableEntry[1].VariableSize = ISO_639_2_ENTRY_SIZE + 1;
2033 VariableEntry[1].Guid = &gEfiGlobalVariableGuid;
2034 VariableEntry[1].Name = EFI_LANG_VARIABLE_NAME;
2035 if (!CheckRemainingSpaceForConsistency (VARIABLE_ATTRIBUTE_NV_BS_RT, &VariableEntry[0], &VariableEntry[1], NULL)) {
2036 //
2037 // No enough variable space to set both PlatformLang and Lang successfully.
2038 //
2039 Status = EFI_OUT_OF_RESOURCES;
2040 } else {
2041 //
2042 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.
2043 //
2044 FindVariable (EFI_PLATFORM_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);
2045
2046 Status = UpdateVariable (EFI_PLATFORM_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, BestPlatformLang,
2047 AsciiStrSize (BestPlatformLang), Attributes, 0, 0, &Variable, NULL);
2048 }
2049
2050 DEBUG ((EFI_D_INFO, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a Status: %r\n", BestLang, BestPlatformLang, Status));
2051 }
2052 }
2053 }
2054
2055 if (SetLanguageCodes) {
2056 //
2057 // Continue to set PlatformLangCodes or LangCodes.
2058 //
2059 return EFI_SUCCESS;
2060 } else {
2061 return Status;
2062 }
2063 }
2064
2065 /**
2066 Compare two EFI_TIME data.
2067
2068
2069 @param FirstTime A pointer to the first EFI_TIME data.
2070 @param SecondTime A pointer to the second EFI_TIME data.
2071
2072 @retval TRUE The FirstTime is not later than the SecondTime.
2073 @retval FALSE The FirstTime is later than the SecondTime.
2074
2075 **/
2076 BOOLEAN
2077 VariableCompareTimeStampInternal (
2078 IN EFI_TIME *FirstTime,
2079 IN EFI_TIME *SecondTime
2080 )
2081 {
2082 if (FirstTime->Year != SecondTime->Year) {
2083 return (BOOLEAN) (FirstTime->Year < SecondTime->Year);
2084 } else if (FirstTime->Month != SecondTime->Month) {
2085 return (BOOLEAN) (FirstTime->Month < SecondTime->Month);
2086 } else if (FirstTime->Day != SecondTime->Day) {
2087 return (BOOLEAN) (FirstTime->Day < SecondTime->Day);
2088 } else if (FirstTime->Hour != SecondTime->Hour) {
2089 return (BOOLEAN) (FirstTime->Hour < SecondTime->Hour);
2090 } else if (FirstTime->Minute != SecondTime->Minute) {
2091 return (BOOLEAN) (FirstTime->Minute < SecondTime->Minute);
2092 }
2093
2094 return (BOOLEAN) (FirstTime->Second <= SecondTime->Second);
2095 }
2096
2097 /**
2098 Update the variable region with Variable information. If EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is set,
2099 index of associated public key is needed.
2100
2101 @param[in] VariableName Name of variable.
2102 @param[in] VendorGuid Guid of variable.
2103 @param[in] Data Variable data.
2104 @param[in] DataSize Size of data. 0 means delete.
2105 @param[in] Attributes Attributes of the variable.
2106 @param[in] KeyIndex Index of associated public key.
2107 @param[in] MonotonicCount Value of associated monotonic count.
2108 @param[in, out] CacheVariable The variable information which is used to keep track of variable usage.
2109 @param[in] TimeStamp Value of associated TimeStamp.
2110
2111 @retval EFI_SUCCESS The update operation is success.
2112 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.
2113
2114 **/
2115 EFI_STATUS
2116 UpdateVariable (
2117 IN CHAR16 *VariableName,
2118 IN EFI_GUID *VendorGuid,
2119 IN VOID *Data,
2120 IN UINTN DataSize,
2121 IN UINT32 Attributes OPTIONAL,
2122 IN UINT32 KeyIndex OPTIONAL,
2123 IN UINT64 MonotonicCount OPTIONAL,
2124 IN OUT VARIABLE_POINTER_TRACK *CacheVariable,
2125 IN EFI_TIME *TimeStamp OPTIONAL
2126 )
2127 {
2128 EFI_STATUS Status;
2129 VARIABLE_HEADER *NextVariable;
2130 UINTN ScratchSize;
2131 UINTN MaxDataSize;
2132 UINTN VarNameOffset;
2133 UINTN VarDataOffset;
2134 UINTN VarNameSize;
2135 UINTN VarSize;
2136 BOOLEAN Volatile;
2137 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;
2138 UINT8 State;
2139 VARIABLE_POINTER_TRACK *Variable;
2140 VARIABLE_POINTER_TRACK NvVariable;
2141 VARIABLE_STORE_HEADER *VariableStoreHeader;
2142 UINTN CacheOffset;
2143 UINT8 *BufferForMerge;
2144 UINTN MergedBufSize;
2145 BOOLEAN DataReady;
2146 UINTN DataOffset;
2147 BOOLEAN IsCommonVariable;
2148 BOOLEAN IsCommonUserVariable;
2149 AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
2150
2151 if (mVariableModuleGlobal->FvbInstance == NULL) {
2152 //
2153 // The FVB protocol is not ready, so the EFI_VARIABLE_WRITE_ARCH_PROTOCOL is not installed.
2154 //
2155 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {
2156 //
2157 // Trying to update NV variable prior to the installation of EFI_VARIABLE_WRITE_ARCH_PROTOCOL
2158 //
2159 DEBUG ((EFI_D_ERROR, "Update NV variable before EFI_VARIABLE_WRITE_ARCH_PROTOCOL ready - %r\n", EFI_NOT_AVAILABLE_YET));
2160 return EFI_NOT_AVAILABLE_YET;
2161 } else if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {
2162 //
2163 // Trying to update volatile authenticated variable prior to the installation of EFI_VARIABLE_WRITE_ARCH_PROTOCOL
2164 // The authenticated variable perhaps is not initialized, just return here.
2165 //
2166 DEBUG ((EFI_D_ERROR, "Update AUTH variable before EFI_VARIABLE_WRITE_ARCH_PROTOCOL ready - %r\n", EFI_NOT_AVAILABLE_YET));
2167 return EFI_NOT_AVAILABLE_YET;
2168 }
2169 }
2170
2171 //
2172 // Check if CacheVariable points to the variable in variable HOB.
2173 // If yes, let CacheVariable points to the variable in NV variable cache.
2174 //
2175 if ((CacheVariable->CurrPtr != NULL) &&
2176 (mVariableModuleGlobal->VariableGlobal.HobVariableBase != 0) &&
2177 (CacheVariable->StartPtr == GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase))
2178 ) {
2179 CacheVariable->StartPtr = GetStartPointer (mNvVariableCache);
2180 CacheVariable->EndPtr = GetEndPointer (mNvVariableCache);
2181 CacheVariable->Volatile = FALSE;
2182 Status = FindVariableEx (VariableName, VendorGuid, FALSE, CacheVariable);
2183 if (CacheVariable->CurrPtr == NULL || EFI_ERROR (Status)) {
2184 //
2185 // There is no matched variable in NV variable cache.
2186 //
2187 if ((((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0) && (DataSize == 0)) || (Attributes == 0)) {
2188 //
2189 // It is to delete variable,
2190 // go to delete this variable in variable HOB and
2191 // try to flush other variables from HOB to flash.
2192 //
2193 UpdateVariableInfo (VariableName, VendorGuid, FALSE, FALSE, FALSE, TRUE, FALSE);
2194 FlushHobVariableToFlash (VariableName, VendorGuid);
2195 return EFI_SUCCESS;
2196 }
2197 }
2198 }
2199
2200 if ((CacheVariable->CurrPtr == NULL) || CacheVariable->Volatile) {
2201 Variable = CacheVariable;
2202 } else {
2203 //
2204 // Update/Delete existing NV variable.
2205 // CacheVariable points to the variable in the memory copy of Flash area
2206 // Now let Variable points to the same variable in Flash area.
2207 //
2208 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);
2209 Variable = &NvVariable;
2210 Variable->StartPtr = GetStartPointer (VariableStoreHeader);
2211 Variable->EndPtr = (VARIABLE_HEADER *)((UINTN)Variable->StartPtr + ((UINTN)CacheVariable->EndPtr - (UINTN)CacheVariable->StartPtr));
2212
2213 Variable->CurrPtr = (VARIABLE_HEADER *)((UINTN)Variable->StartPtr + ((UINTN)CacheVariable->CurrPtr - (UINTN)CacheVariable->StartPtr));
2214 if (CacheVariable->InDeletedTransitionPtr != NULL) {
2215 Variable->InDeletedTransitionPtr = (VARIABLE_HEADER *)((UINTN)Variable->StartPtr + ((UINTN)CacheVariable->InDeletedTransitionPtr - (UINTN)CacheVariable->StartPtr));
2216 } else {
2217 Variable->InDeletedTransitionPtr = NULL;
2218 }
2219 Variable->Volatile = FALSE;
2220 }
2221
2222 Fvb = mVariableModuleGlobal->FvbInstance;
2223
2224 //
2225 // Tricky part: Use scratch data area at the end of volatile variable store
2226 // as a temporary storage.
2227 //
2228 NextVariable = GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase));
2229 ScratchSize = mVariableModuleGlobal->ScratchBufferSize;
2230 SetMem (NextVariable, ScratchSize, 0xff);
2231 DataReady = FALSE;
2232
2233 if (Variable->CurrPtr != NULL) {
2234 //
2235 // Update/Delete existing variable.
2236 //
2237 if (AtRuntime ()) {
2238 //
2239 // If AtRuntime and the variable is Volatile and Runtime Access,
2240 // the volatile is ReadOnly, and SetVariable should be aborted and
2241 // return EFI_WRITE_PROTECTED.
2242 //
2243 if (Variable->Volatile) {
2244 Status = EFI_WRITE_PROTECTED;
2245 goto Done;
2246 }
2247 //
2248 // Only variable that have NV attributes can be updated/deleted in Runtime.
2249 //
2250 if ((CacheVariable->CurrPtr->Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {
2251 Status = EFI_INVALID_PARAMETER;
2252 goto Done;
2253 }
2254
2255 //
2256 // Only variable that have RT attributes can be updated/deleted in Runtime.
2257 //
2258 if ((CacheVariable->CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) {
2259 Status = EFI_INVALID_PARAMETER;
2260 goto Done;
2261 }
2262 }
2263
2264 //
2265 // Setting a data variable with no access, or zero DataSize attributes
2266 // causes it to be deleted.
2267 // When the EFI_VARIABLE_APPEND_WRITE attribute is set, DataSize of zero will
2268 // not delete the variable.
2269 //
2270 if ((((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0) && (DataSize == 0))|| ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0)) {
2271 if (Variable->InDeletedTransitionPtr != NULL) {
2272 //
2273 // Both ADDED and IN_DELETED_TRANSITION variable are present,
2274 // set IN_DELETED_TRANSITION one to DELETED state first.
2275 //
2276 ASSERT (CacheVariable->InDeletedTransitionPtr != NULL);
2277 State = CacheVariable->InDeletedTransitionPtr->State;
2278 State &= VAR_DELETED;
2279 Status = UpdateVariableStore (
2280 &mVariableModuleGlobal->VariableGlobal,
2281 Variable->Volatile,
2282 FALSE,
2283 Fvb,
2284 (UINTN) &Variable->InDeletedTransitionPtr->State,
2285 sizeof (UINT8),
2286 &State
2287 );
2288 if (!EFI_ERROR (Status)) {
2289 if (!Variable->Volatile) {
2290 CacheVariable->InDeletedTransitionPtr->State = State;
2291 }
2292 } else {
2293 goto Done;
2294 }
2295 }
2296
2297 State = CacheVariable->CurrPtr->State;
2298 State &= VAR_DELETED;
2299
2300 Status = UpdateVariableStore (
2301 &mVariableModuleGlobal->VariableGlobal,
2302 Variable->Volatile,
2303 FALSE,
2304 Fvb,
2305 (UINTN) &Variable->CurrPtr->State,
2306 sizeof (UINT8),
2307 &State
2308 );
2309 if (!EFI_ERROR (Status)) {
2310 UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, FALSE, TRUE, FALSE);
2311 if (!Variable->Volatile) {
2312 CacheVariable->CurrPtr->State = State;
2313 FlushHobVariableToFlash (VariableName, VendorGuid);
2314 }
2315 }
2316 goto Done;
2317 }
2318 //
2319 // If the variable is marked valid, and the same data has been passed in,
2320 // then return to the caller immediately.
2321 //
2322 if (DataSizeOfVariable (CacheVariable->CurrPtr) == DataSize &&
2323 (CompareMem (Data, GetVariableDataPtr (CacheVariable->CurrPtr), DataSize) == 0) &&
2324 ((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0) &&
2325 (TimeStamp == NULL)) {
2326 //
2327 // Variable content unchanged and no need to update timestamp, just return.
2328 //
2329 UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, TRUE, FALSE, FALSE);
2330 Status = EFI_SUCCESS;
2331 goto Done;
2332 } else if ((CacheVariable->CurrPtr->State == VAR_ADDED) ||
2333 (CacheVariable->CurrPtr->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {
2334
2335 //
2336 // EFI_VARIABLE_APPEND_WRITE attribute only effects for existing variable.
2337 //
2338 if ((Attributes & EFI_VARIABLE_APPEND_WRITE) != 0) {
2339 //
2340 // NOTE: From 0 to DataOffset of NextVariable is reserved for Variable Header and Name.
2341 // From DataOffset of NextVariable is to save the existing variable data.
2342 //
2343 DataOffset = GetVariableDataOffset (CacheVariable->CurrPtr);
2344 BufferForMerge = (UINT8 *) ((UINTN) NextVariable + DataOffset);
2345 CopyMem (BufferForMerge, (UINT8 *) ((UINTN) CacheVariable->CurrPtr + DataOffset), DataSizeOfVariable (CacheVariable->CurrPtr));
2346
2347 //
2348 // Set Max Auth/Non-Volatile/Volatile Variable Data Size as default MaxDataSize.
2349 //
2350 if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {
2351 MaxDataSize = mVariableModuleGlobal->MaxAuthVariableSize - DataOffset;
2352 } else if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {
2353 MaxDataSize = mVariableModuleGlobal->MaxVariableSize - DataOffset;
2354 } else {
2355 MaxDataSize = mVariableModuleGlobal->MaxVolatileVariableSize - DataOffset;
2356 }
2357
2358 //
2359 // Append the new data to the end of existing data.
2360 // Max Harware error record variable data size is different from common/auth variable.
2361 //
2362 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
2363 MaxDataSize = PcdGet32 (PcdMaxHardwareErrorVariableSize) - DataOffset;
2364 }
2365
2366 if (DataSizeOfVariable (CacheVariable->CurrPtr) + DataSize > MaxDataSize) {
2367 //
2368 // Existing data size + new data size exceed maximum variable size limitation.
2369 //
2370 Status = EFI_INVALID_PARAMETER;
2371 goto Done;
2372 }
2373 CopyMem ((UINT8*) ((UINTN) BufferForMerge + DataSizeOfVariable (CacheVariable->CurrPtr)), Data, DataSize);
2374 MergedBufSize = DataSizeOfVariable (CacheVariable->CurrPtr) + DataSize;
2375
2376 //
2377 // BufferForMerge(from DataOffset of NextVariable) has included the merged existing and new data.
2378 //
2379 Data = BufferForMerge;
2380 DataSize = MergedBufSize;
2381 DataReady = TRUE;
2382 }
2383
2384 //
2385 // Mark the old variable as in delete transition.
2386 //
2387 State = CacheVariable->CurrPtr->State;
2388 State &= VAR_IN_DELETED_TRANSITION;
2389
2390 Status = UpdateVariableStore (
2391 &mVariableModuleGlobal->VariableGlobal,
2392 Variable->Volatile,
2393 FALSE,
2394 Fvb,
2395 (UINTN) &Variable->CurrPtr->State,
2396 sizeof (UINT8),
2397 &State
2398 );
2399 if (EFI_ERROR (Status)) {
2400 goto Done;
2401 }
2402 if (!Variable->Volatile) {
2403 CacheVariable->CurrPtr->State = State;
2404 }
2405 }
2406 } else {
2407 //
2408 // Not found existing variable. Create a new variable.
2409 //
2410
2411 if ((DataSize == 0) && ((Attributes & EFI_VARIABLE_APPEND_WRITE) != 0)) {
2412 Status = EFI_SUCCESS;
2413 goto Done;
2414 }
2415
2416 //
2417 // Make sure we are trying to create a new variable.
2418 // Setting a data variable with zero DataSize or no access attributes means to delete it.
2419 //
2420 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {
2421 Status = EFI_NOT_FOUND;
2422 goto Done;
2423 }
2424
2425 //
2426 // Only variable have NV|RT attribute can be created in Runtime.
2427 //
2428 if (AtRuntime () &&
2429 (((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) || ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0))) {
2430 Status = EFI_INVALID_PARAMETER;
2431 goto Done;
2432 }
2433 }
2434
2435 //
2436 // Function part - create a new variable and copy the data.
2437 // Both update a variable and create a variable will come here.
2438 //
2439 NextVariable->StartId = VARIABLE_DATA;
2440 //
2441 // NextVariable->State = VAR_ADDED;
2442 //
2443 NextVariable->Reserved = 0;
2444 if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
2445 AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) NextVariable;
2446 AuthVariable->PubKeyIndex = KeyIndex;
2447 AuthVariable->MonotonicCount = MonotonicCount;
2448 ZeroMem (&AuthVariable->TimeStamp, sizeof (EFI_TIME));
2449
2450 if (((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) != 0) &&
2451 (TimeStamp != NULL)) {
2452 if ((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0) {
2453 CopyMem (&AuthVariable->TimeStamp, TimeStamp, sizeof (EFI_TIME));
2454 } else {
2455 //
2456 // In the case when the EFI_VARIABLE_APPEND_WRITE attribute is set, only
2457 // when the new TimeStamp value is later than the current timestamp associated
2458 // with the variable, we need associate the new timestamp with the updated value.
2459 //
2460 if (Variable->CurrPtr != NULL) {
2461 if (VariableCompareTimeStampInternal (&(((AUTHENTICATED_VARIABLE_HEADER *) CacheVariable->CurrPtr)->TimeStamp), TimeStamp)) {
2462 CopyMem (&AuthVariable->TimeStamp, TimeStamp, sizeof (EFI_TIME));
2463 } else {
2464 CopyMem (&AuthVariable->TimeStamp, &(((AUTHENTICATED_VARIABLE_HEADER *) CacheVariable->CurrPtr)->TimeStamp), sizeof (EFI_TIME));
2465 }
2466 }
2467 }
2468 }
2469 }
2470
2471 //
2472 // The EFI_VARIABLE_APPEND_WRITE attribute will never be set in the returned
2473 // Attributes bitmask parameter of a GetVariable() call.
2474 //
2475 NextVariable->Attributes = Attributes & (~EFI_VARIABLE_APPEND_WRITE);
2476
2477 VarNameOffset = GetVariableHeaderSize ();
2478 VarNameSize = StrSize (VariableName);
2479 CopyMem (
2480 (UINT8 *) ((UINTN) NextVariable + VarNameOffset),
2481 VariableName,
2482 VarNameSize
2483 );
2484 VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);
2485
2486 //
2487 // If DataReady is TRUE, it means the variable data has been saved into
2488 // NextVariable during EFI_VARIABLE_APPEND_WRITE operation preparation.
2489 //
2490 if (!DataReady) {
2491 CopyMem (
2492 (UINT8 *) ((UINTN) NextVariable + VarDataOffset),
2493 Data,
2494 DataSize
2495 );
2496 }
2497
2498 CopyMem (GetVendorGuidPtr (NextVariable), VendorGuid, sizeof (EFI_GUID));
2499 //
2500 // There will be pad bytes after Data, the NextVariable->NameSize and
2501 // NextVariable->DataSize should not include pad size so that variable
2502 // service can get actual size in GetVariable.
2503 //
2504 SetNameSizeOfVariable (NextVariable, VarNameSize);
2505 SetDataSizeOfVariable (NextVariable, DataSize);
2506
2507 //
2508 // The actual size of the variable that stores in storage should
2509 // include pad size.
2510 //
2511 VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);
2512 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {
2513 //
2514 // Create a nonvolatile variable.
2515 //
2516 Volatile = FALSE;
2517
2518 IsCommonVariable = FALSE;
2519 IsCommonUserVariable = FALSE;
2520 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0) {
2521 IsCommonVariable = TRUE;
2522 IsCommonUserVariable = IsUserVariable (NextVariable);
2523 }
2524 if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0)
2525 && ((VarSize + mVariableModuleGlobal->HwErrVariableTotalSize) > PcdGet32 (PcdHwErrStorageSize)))
2526 || (IsCommonVariable && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > mVariableModuleGlobal->CommonVariableSpace))
2527 || (IsCommonVariable && AtRuntime () && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > mVariableModuleGlobal->CommonRuntimeVariableSpace))
2528 || (IsCommonUserVariable && ((VarSize + mVariableModuleGlobal->CommonUserVariableTotalSize) > mVariableModuleGlobal->CommonMaxUserVariableSpace))) {
2529 if (AtRuntime ()) {
2530 if (IsCommonUserVariable && ((VarSize + mVariableModuleGlobal->CommonUserVariableTotalSize) > mVariableModuleGlobal->CommonMaxUserVariableSpace)) {
2531 RecordVarErrorFlag (VAR_ERROR_FLAG_USER_ERROR, VariableName, VendorGuid, Attributes, VarSize);
2532 }
2533 if (IsCommonVariable && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > mVariableModuleGlobal->CommonRuntimeVariableSpace)) {
2534 RecordVarErrorFlag (VAR_ERROR_FLAG_SYSTEM_ERROR, VariableName, VendorGuid, Attributes, VarSize);
2535 }
2536 Status = EFI_OUT_OF_RESOURCES;
2537 goto Done;
2538 }
2539 //
2540 // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.
2541 //
2542 Status = Reclaim (
2543 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,
2544 &mVariableModuleGlobal->NonVolatileLastVariableOffset,
2545 FALSE,
2546 Variable,
2547 NextVariable,
2548 HEADER_ALIGN (VarSize)
2549 );
2550 if (!EFI_ERROR (Status)) {
2551 //
2552 // The new variable has been integrated successfully during reclaiming.
2553 //
2554 if (Variable->CurrPtr != NULL) {
2555 CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN) CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr));
2556 CacheVariable->InDeletedTransitionPtr = NULL;
2557 }
2558 UpdateVariableInfo (VariableName, VendorGuid, FALSE, FALSE, TRUE, FALSE, FALSE);
2559 FlushHobVariableToFlash (VariableName, VendorGuid);
2560 } else {
2561 if (IsCommonUserVariable && ((VarSize + mVariableModuleGlobal->CommonUserVariableTotalSize) > mVariableModuleGlobal->CommonMaxUserVariableSpace)) {
2562 RecordVarErrorFlag (VAR_ERROR_FLAG_USER_ERROR, VariableName, VendorGuid, Attributes, VarSize);
2563 }
2564 if (IsCommonVariable && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > mVariableModuleGlobal->CommonVariableSpace)) {
2565 RecordVarErrorFlag (VAR_ERROR_FLAG_SYSTEM_ERROR, VariableName, VendorGuid, Attributes, VarSize);
2566 }
2567 }
2568 goto Done;
2569 }
2570 //
2571 // Four steps
2572 // 1. Write variable header
2573 // 2. Set variable state to header valid
2574 // 3. Write variable data
2575 // 4. Set variable state to valid
2576 //
2577 //
2578 // Step 1:
2579 //
2580 CacheOffset = mVariableModuleGlobal->NonVolatileLastVariableOffset;
2581 Status = UpdateVariableStore (
2582 &mVariableModuleGlobal->VariableGlobal,
2583 FALSE,
2584 TRUE,
2585 Fvb,
2586 mVariableModuleGlobal->NonVolatileLastVariableOffset,
2587 (UINT32) GetVariableHeaderSize (),
2588 (UINT8 *) NextVariable
2589 );
2590
2591 if (EFI_ERROR (Status)) {
2592 goto Done;
2593 }
2594
2595 //
2596 // Step 2:
2597 //
2598 NextVariable->State = VAR_HEADER_VALID_ONLY;
2599 Status = UpdateVariableStore (
2600 &mVariableModuleGlobal->VariableGlobal,
2601 FALSE,
2602 TRUE,
2603 Fvb,
2604 mVariableModuleGlobal->NonVolatileLastVariableOffset + OFFSET_OF (VARIABLE_HEADER, State),
2605 sizeof (UINT8),
2606 &NextVariable->State
2607 );
2608
2609 if (EFI_ERROR (Status)) {
2610 goto Done;
2611 }
2612 //
2613 // Step 3:
2614 //
2615 Status = UpdateVariableStore (
2616 &mVariableModuleGlobal->VariableGlobal,
2617 FALSE,
2618 TRUE,
2619 Fvb,
2620 mVariableModuleGlobal->NonVolatileLastVariableOffset + GetVariableHeaderSize (),
2621 (UINT32) (VarSize - GetVariableHeaderSize ()),
2622 (UINT8 *) NextVariable + GetVariableHeaderSize ()
2623 );
2624
2625 if (EFI_ERROR (Status)) {
2626 goto Done;
2627 }
2628 //
2629 // Step 4:
2630 //
2631 NextVariable->State = VAR_ADDED;
2632 Status = UpdateVariableStore (
2633 &mVariableModuleGlobal->VariableGlobal,
2634 FALSE,
2635 TRUE,
2636 Fvb,
2637 mVariableModuleGlobal->NonVolatileLastVariableOffset + OFFSET_OF (VARIABLE_HEADER, State),
2638 sizeof (UINT8),
2639 &NextVariable->State
2640 );
2641
2642 if (EFI_ERROR (Status)) {
2643 goto Done;
2644 }
2645
2646 mVariableModuleGlobal->NonVolatileLastVariableOffset += HEADER_ALIGN (VarSize);
2647
2648 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {
2649 mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VarSize);
2650 } else {
2651 mVariableModuleGlobal->CommonVariableTotalSize += HEADER_ALIGN (VarSize);
2652 if (IsCommonUserVariable) {
2653 mVariableModuleGlobal->CommonUserVariableTotalSize += HEADER_ALIGN (VarSize);
2654 }
2655 }
2656 //
2657 // update the memory copy of Flash region.
2658 //
2659 CopyMem ((UINT8 *)mNvVariableCache + CacheOffset, (UINT8 *)NextVariable, VarSize);
2660 } else {
2661 //
2662 // Create a volatile variable.
2663 //
2664 Volatile = TRUE;
2665
2666 if ((UINT32) (VarSize + mVariableModuleGlobal->VolatileLastVariableOffset) >
2667 ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size) {
2668 //
2669 // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.
2670 //
2671 Status = Reclaim (
2672 mVariableModuleGlobal->VariableGlobal.VolatileVariableBase,
2673 &mVariableModuleGlobal->VolatileLastVariableOffset,
2674 TRUE,
2675 Variable,
2676 NextVariable,
2677 HEADER_ALIGN (VarSize)
2678 );
2679 if (!EFI_ERROR (Status)) {
2680 //
2681 // The new variable has been integrated successfully during reclaiming.
2682 //
2683 if (Variable->CurrPtr != NULL) {
2684 CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN) CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr));
2685 CacheVariable->InDeletedTransitionPtr = NULL;
2686 }
2687 UpdateVariableInfo (VariableName, VendorGuid, TRUE, FALSE, TRUE, FALSE, FALSE);
2688 }
2689 goto Done;
2690 }
2691
2692 NextVariable->State = VAR_ADDED;
2693 Status = UpdateVariableStore (
2694 &mVariableModuleGlobal->VariableGlobal,
2695 TRUE,
2696 TRUE,
2697 Fvb,
2698 mVariableModuleGlobal->VolatileLastVariableOffset,
2699 (UINT32) VarSize,
2700 (UINT8 *) NextVariable
2701 );
2702
2703 if (EFI_ERROR (Status)) {
2704 goto Done;
2705 }
2706
2707 mVariableModuleGlobal->VolatileLastVariableOffset += HEADER_ALIGN (VarSize);
2708 }
2709
2710 //
2711 // Mark the old variable as deleted.
2712 //
2713 if (!EFI_ERROR (Status) && Variable->CurrPtr != NULL) {
2714 if (Variable->InDeletedTransitionPtr != NULL) {
2715 //
2716 // Both ADDED and IN_DELETED_TRANSITION old variable are present,
2717 // set IN_DELETED_TRANSITION one to DELETED state first.
2718 //
2719 ASSERT (CacheVariable->InDeletedTransitionPtr != NULL);
2720 State = CacheVariable->InDeletedTransitionPtr->State;
2721 State &= VAR_DELETED;
2722 Status = UpdateVariableStore (
2723 &mVariableModuleGlobal->VariableGlobal,
2724 Variable->Volatile,
2725 FALSE,
2726 Fvb,
2727 (UINTN) &Variable->InDeletedTransitionPtr->State,
2728 sizeof (UINT8),
2729 &State
2730 );
2731 if (!EFI_ERROR (Status)) {
2732 if (!Variable->Volatile) {
2733 CacheVariable->InDeletedTransitionPtr->State = State;
2734 }
2735 } else {
2736 goto Done;
2737 }
2738 }
2739
2740 State = CacheVariable->CurrPtr->State;
2741 State &= VAR_DELETED;
2742
2743 Status = UpdateVariableStore (
2744 &mVariableModuleGlobal->VariableGlobal,
2745 Variable->Volatile,
2746 FALSE,
2747 Fvb,
2748 (UINTN) &Variable->CurrPtr->State,
2749 sizeof (UINT8),
2750 &State
2751 );
2752 if (!EFI_ERROR (Status) && !Variable->Volatile) {
2753 CacheVariable->CurrPtr->State = State;
2754 }
2755 }
2756
2757 if (!EFI_ERROR (Status)) {
2758 UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);
2759 if (!Volatile) {
2760 FlushHobVariableToFlash (VariableName, VendorGuid);
2761 }
2762 }
2763
2764 Done:
2765 return Status;
2766 }
2767
2768 /**
2769
2770 This code finds variable in storage blocks (Volatile or Non-Volatile).
2771
2772 Caution: This function may receive untrusted input.
2773 This function may be invoked in SMM mode, and datasize is external input.
2774 This function will do basic validation, before parse the data.
2775
2776 @param VariableName Name of Variable to be found.
2777 @param VendorGuid Variable vendor GUID.
2778 @param Attributes Attribute value of the variable found.
2779 @param DataSize Size of Data found. If size is less than the
2780 data, this value contains the required size.
2781 @param Data The buffer to return the contents of the variable. May be NULL
2782 with a zero DataSize in order to determine the size buffer needed.
2783
2784 @return EFI_INVALID_PARAMETER Invalid parameter.
2785 @return EFI_SUCCESS Find the specified variable.
2786 @return EFI_NOT_FOUND Not found.
2787 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
2788
2789 **/
2790 EFI_STATUS
2791 EFIAPI
2792 VariableServiceGetVariable (
2793 IN CHAR16 *VariableName,
2794 IN EFI_GUID *VendorGuid,
2795 OUT UINT32 *Attributes OPTIONAL,
2796 IN OUT UINTN *DataSize,
2797 OUT VOID *Data OPTIONAL
2798 )
2799 {
2800 EFI_STATUS Status;
2801 VARIABLE_POINTER_TRACK Variable;
2802 UINTN VarDataSize;
2803
2804 if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {
2805 return EFI_INVALID_PARAMETER;
2806 }
2807
2808 if (VariableName[0] == 0) {
2809 return EFI_NOT_FOUND;
2810 }
2811
2812 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
2813
2814 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);
2815 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {
2816 goto Done;
2817 }
2818
2819 //
2820 // Get data size
2821 //
2822 VarDataSize = DataSizeOfVariable (Variable.CurrPtr);
2823 ASSERT (VarDataSize != 0);
2824
2825 if (*DataSize >= VarDataSize) {
2826 if (Data == NULL) {
2827 Status = EFI_INVALID_PARAMETER;
2828 goto Done;
2829 }
2830
2831 CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);
2832 if (Attributes != NULL) {
2833 *Attributes = Variable.CurrPtr->Attributes;
2834 }
2835
2836 *DataSize = VarDataSize;
2837 UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, TRUE, FALSE, FALSE, FALSE);
2838
2839 Status = EFI_SUCCESS;
2840 goto Done;
2841 } else {
2842 *DataSize = VarDataSize;
2843 Status = EFI_BUFFER_TOO_SMALL;
2844 goto Done;
2845 }
2846
2847 Done:
2848 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
2849 return Status;
2850 }
2851
2852 /**
2853 This code Finds the Next available variable.
2854
2855 Caution: This function may receive untrusted input.
2856 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
2857
2858 @param[in] VariableName Pointer to variable name.
2859 @param[in] VendorGuid Variable Vendor Guid.
2860 @param[out] VariablePtr Pointer to variable header address.
2861
2862 @retval EFI_SUCCESS The function completed successfully.
2863 @retval EFI_NOT_FOUND The next variable was not found.
2864 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while VendorGuid is NULL.
2865 @retval EFI_INVALID_PARAMETER The input values of VariableName and VendorGuid are not a name and
2866 GUID of an existing variable.
2867
2868 **/
2869 EFI_STATUS
2870 EFIAPI
2871 VariableServiceGetNextVariableInternal (
2872 IN CHAR16 *VariableName,
2873 IN EFI_GUID *VendorGuid,
2874 OUT VARIABLE_HEADER **VariablePtr
2875 )
2876 {
2877 VARIABLE_STORE_TYPE Type;
2878 VARIABLE_POINTER_TRACK Variable;
2879 VARIABLE_POINTER_TRACK VariableInHob;
2880 VARIABLE_POINTER_TRACK VariablePtrTrack;
2881 EFI_STATUS Status;
2882 VARIABLE_STORE_HEADER *VariableStoreHeader[VariableStoreTypeMax];
2883
2884 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);
2885 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {
2886 //
2887 // For VariableName is an empty string, FindVariable() will try to find and return
2888 // the first qualified variable, and if FindVariable() returns error (EFI_NOT_FOUND)
2889 // as no any variable is found, still go to return the error (EFI_NOT_FOUND).
2890 //
2891 if (VariableName[0] != 0) {
2892 //
2893 // For VariableName is not an empty string, and FindVariable() returns error as
2894 // VariableName and VendorGuid are not a name and GUID of an existing variable,
2895 // there is no way to get next variable, follow spec to return EFI_INVALID_PARAMETER.
2896 //
2897 Status = EFI_INVALID_PARAMETER;
2898 }
2899 goto Done;
2900 }
2901
2902 if (VariableName[0] != 0) {
2903 //
2904 // If variable name is not NULL, get next variable.
2905 //
2906 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
2907 }
2908
2909 //
2910 // 0: Volatile, 1: HOB, 2: Non-Volatile.
2911 // The index and attributes mapping must be kept in this order as FindVariable
2912 // makes use of this mapping to implement search algorithm.
2913 //
2914 VariableStoreHeader[VariableStoreTypeVolatile] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;
2915 VariableStoreHeader[VariableStoreTypeHob] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;
2916 VariableStoreHeader[VariableStoreTypeNv] = mNvVariableCache;
2917
2918 while (TRUE) {
2919 //
2920 // Switch from Volatile to HOB, to Non-Volatile.
2921 //
2922 while (!IsValidVariableHeader (Variable.CurrPtr, Variable.EndPtr)) {
2923 //
2924 // Find current storage index
2925 //
2926 for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {
2927 if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr == GetStartPointer (VariableStoreHeader[Type]))) {
2928 break;
2929 }
2930 }
2931 ASSERT (Type < VariableStoreTypeMax);
2932 //
2933 // Switch to next storage
2934 //
2935 for (Type++; Type < VariableStoreTypeMax; Type++) {
2936 if (VariableStoreHeader[Type] != NULL) {
2937 break;
2938 }
2939 }
2940 //
2941 // Capture the case that
2942 // 1. current storage is the last one, or
2943 // 2. no further storage
2944 //
2945 if (Type == VariableStoreTypeMax) {
2946 Status = EFI_NOT_FOUND;
2947 goto Done;
2948 }
2949 Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);
2950 Variable.EndPtr = GetEndPointer (VariableStoreHeader[Type]);
2951 Variable.CurrPtr = Variable.StartPtr;
2952 }
2953
2954 //
2955 // Variable is found
2956 //
2957 if (Variable.CurrPtr->State == VAR_ADDED || Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
2958 if (!AtRuntime () || ((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {
2959 if (Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
2960 //
2961 // If it is a IN_DELETED_TRANSITION variable,
2962 // and there is also a same ADDED one at the same time,
2963 // don't return it.
2964 //
2965 VariablePtrTrack.StartPtr = Variable.StartPtr;
2966 VariablePtrTrack.EndPtr = Variable.EndPtr;
2967 Status = FindVariableEx (
2968 GetVariableNamePtr (Variable.CurrPtr),
2969 GetVendorGuidPtr (Variable.CurrPtr),
2970 FALSE,
2971 &VariablePtrTrack
2972 );
2973 if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State == VAR_ADDED) {
2974 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
2975 continue;
2976 }
2977 }
2978
2979 //
2980 // Don't return NV variable when HOB overrides it
2981 //
2982 if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) && (VariableStoreHeader[VariableStoreTypeNv] != NULL) &&
2983 (Variable.StartPtr == GetStartPointer (VariableStoreHeader[VariableStoreTypeNv]))
2984 ) {
2985 VariableInHob.StartPtr = GetStartPointer (VariableStoreHeader[VariableStoreTypeHob]);
2986 VariableInHob.EndPtr = GetEndPointer (VariableStoreHeader[VariableStoreTypeHob]);
2987 Status = FindVariableEx (
2988 GetVariableNamePtr (Variable.CurrPtr),
2989 GetVendorGuidPtr (Variable.CurrPtr),
2990 FALSE,
2991 &VariableInHob
2992 );
2993 if (!EFI_ERROR (Status)) {
2994 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
2995 continue;
2996 }
2997 }
2998
2999 *VariablePtr = Variable.CurrPtr;
3000 Status = EFI_SUCCESS;
3001 goto Done;
3002 }
3003 }
3004
3005 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
3006 }
3007
3008 Done:
3009 return Status;
3010 }
3011
3012 /**
3013
3014 This code Finds the Next available variable.
3015
3016 Caution: This function may receive untrusted input.
3017 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
3018
3019 @param VariableNameSize The size of the VariableName buffer. The size must be large
3020 enough to fit input string supplied in VariableName buffer.
3021 @param VariableName Pointer to variable name.
3022 @param VendorGuid Variable Vendor Guid.
3023
3024 @retval EFI_SUCCESS The function completed successfully.
3025 @retval EFI_NOT_FOUND The next variable was not found.
3026 @retval EFI_BUFFER_TOO_SMALL The VariableNameSize is too small for the result.
3027 VariableNameSize has been updated with the size needed to complete the request.
3028 @retval EFI_INVALID_PARAMETER VariableNameSize is NULL.
3029 @retval EFI_INVALID_PARAMETER VariableName is NULL.
3030 @retval EFI_INVALID_PARAMETER VendorGuid is NULL.
3031 @retval EFI_INVALID_PARAMETER The input values of VariableName and VendorGuid are not a name and
3032 GUID of an existing variable.
3033 @retval EFI_INVALID_PARAMETER Null-terminator is not found in the first VariableNameSize bytes of
3034 the input VariableName buffer.
3035
3036 **/
3037 EFI_STATUS
3038 EFIAPI
3039 VariableServiceGetNextVariableName (
3040 IN OUT UINTN *VariableNameSize,
3041 IN OUT CHAR16 *VariableName,
3042 IN OUT EFI_GUID *VendorGuid
3043 )
3044 {
3045 EFI_STATUS Status;
3046 UINTN MaxLen;
3047 UINTN VarNameSize;
3048 VARIABLE_HEADER *VariablePtr;
3049
3050 if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {
3051 return EFI_INVALID_PARAMETER;
3052 }
3053
3054 //
3055 // Calculate the possible maximum length of name string, including the Null terminator.
3056 //
3057 MaxLen = *VariableNameSize / sizeof (CHAR16);
3058 if ((MaxLen == 0) || (StrnLenS (VariableName, MaxLen) == MaxLen)) {
3059 //
3060 // Null-terminator is not found in the first VariableNameSize bytes of the input VariableName buffer,
3061 // follow spec to return EFI_INVALID_PARAMETER.
3062 //
3063 return EFI_INVALID_PARAMETER;
3064 }
3065
3066 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
3067
3068 Status = VariableServiceGetNextVariableInternal (VariableName, VendorGuid, &VariablePtr);
3069 if (!EFI_ERROR (Status)) {
3070 VarNameSize = NameSizeOfVariable (VariablePtr);
3071 ASSERT (VarNameSize != 0);
3072 if (VarNameSize <= *VariableNameSize) {
3073 CopyMem (VariableName, GetVariableNamePtr (VariablePtr), VarNameSize);
3074 CopyMem (VendorGuid, GetVendorGuidPtr (VariablePtr), sizeof (EFI_GUID));
3075 Status = EFI_SUCCESS;
3076 } else {
3077 Status = EFI_BUFFER_TOO_SMALL;
3078 }
3079
3080 *VariableNameSize = VarNameSize;
3081 }
3082
3083 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
3084 return Status;
3085 }
3086
3087 /**
3088
3089 This code sets variable in storage blocks (Volatile or Non-Volatile).
3090
3091 Caution: This function may receive untrusted input.
3092 This function may be invoked in SMM mode, and datasize and data are external input.
3093 This function will do basic validation, before parse the data.
3094 This function will parse the authentication carefully to avoid security issues, like
3095 buffer overflow, integer overflow.
3096 This function will check attribute carefully to avoid authentication bypass.
3097
3098 @param VariableName Name of Variable to be found.
3099 @param VendorGuid Variable vendor GUID.
3100 @param Attributes Attribute value of the variable found
3101 @param DataSize Size of Data found. If size is less than the
3102 data, this value contains the required size.
3103 @param Data Data pointer.
3104
3105 @return EFI_INVALID_PARAMETER Invalid parameter.
3106 @return EFI_SUCCESS Set successfully.
3107 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.
3108 @return EFI_NOT_FOUND Not found.
3109 @return EFI_WRITE_PROTECTED Variable is read-only.
3110
3111 **/
3112 EFI_STATUS
3113 EFIAPI
3114 VariableServiceSetVariable (
3115 IN CHAR16 *VariableName,
3116 IN EFI_GUID *VendorGuid,
3117 IN UINT32 Attributes,
3118 IN UINTN DataSize,
3119 IN VOID *Data
3120 )
3121 {
3122 VARIABLE_POINTER_TRACK Variable;
3123 EFI_STATUS Status;
3124 VARIABLE_HEADER *NextVariable;
3125 EFI_PHYSICAL_ADDRESS Point;
3126 UINTN PayloadSize;
3127
3128 //
3129 // Check input parameters.
3130 //
3131 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {
3132 return EFI_INVALID_PARAMETER;
3133 }
3134
3135 if (DataSize != 0 && Data == NULL) {
3136 return EFI_INVALID_PARAMETER;
3137 }
3138
3139 //
3140 // Check for reserverd bit in variable attribute.
3141 // EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is deprecated but we still allow
3142 // the delete operation of common authenticated variable at user physical presence.
3143 // So leave EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS attribute check to AuthVariableLib
3144 //
3145 if ((Attributes & (~(EFI_VARIABLE_ATTRIBUTES_MASK | EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS))) != 0) {
3146 return EFI_INVALID_PARAMETER;
3147 }
3148
3149 //
3150 // Make sure if runtime bit is set, boot service bit is set also.
3151 //
3152 if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {
3153 if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {
3154 return EFI_UNSUPPORTED;
3155 } else {
3156 return EFI_INVALID_PARAMETER;
3157 }
3158 } else if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {
3159 if (!mVariableModuleGlobal->VariableGlobal.AuthSupport) {
3160 //
3161 // Not support authenticated variable write.
3162 //
3163 return EFI_INVALID_PARAMETER;
3164 }
3165 } else if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {
3166 if (PcdGet32 (PcdHwErrStorageSize) == 0) {
3167 //
3168 // Not support harware error record variable variable.
3169 //
3170 return EFI_INVALID_PARAMETER;
3171 }
3172 }
3173
3174 //
3175 // EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS and EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute
3176 // cannot be set both.
3177 //
3178 if (((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)
3179 && ((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {
3180 return EFI_UNSUPPORTED;
3181 }
3182
3183 if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) {
3184 //
3185 // If DataSize == AUTHINFO_SIZE and then PayloadSize is 0.
3186 // Maybe it's the delete operation of common authenticated variable at user physical presence.
3187 //
3188 if (DataSize != AUTHINFO_SIZE) {
3189 return EFI_UNSUPPORTED;
3190 }
3191 PayloadSize = DataSize - AUTHINFO_SIZE;
3192 } else if ((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {
3193 //
3194 // Sanity check for EFI_VARIABLE_AUTHENTICATION_2 descriptor.
3195 //
3196 if (DataSize < OFFSET_OF_AUTHINFO2_CERT_DATA ||
3197 ((EFI_VARIABLE_AUTHENTICATION_2 *) Data)->AuthInfo.Hdr.dwLength > DataSize - (OFFSET_OF (EFI_VARIABLE_AUTHENTICATION_2, AuthInfo)) ||
3198 ((EFI_VARIABLE_AUTHENTICATION_2 *) Data)->AuthInfo.Hdr.dwLength < OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID, CertData)) {
3199 return EFI_SECURITY_VIOLATION;
3200 }
3201 //
3202 // The VariableSpeculationBarrier() call here is to ensure the above sanity
3203 // check for the EFI_VARIABLE_AUTHENTICATION_2 descriptor has been completed
3204 // before the execution of subsequent codes.
3205 //
3206 VariableSpeculationBarrier ();
3207 PayloadSize = DataSize - AUTHINFO2_SIZE (Data);
3208 } else {
3209 PayloadSize = DataSize;
3210 }
3211
3212 if ((UINTN)(~0) - PayloadSize < StrSize(VariableName)){
3213 //
3214 // Prevent whole variable size overflow
3215 //
3216 return EFI_INVALID_PARAMETER;
3217 }
3218
3219 //
3220 // The size of the VariableName, including the Unicode Null in bytes plus
3221 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)
3222 // bytes for HwErrRec#### variable.
3223 //
3224 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
3225 if (StrSize (VariableName) + PayloadSize > PcdGet32 (PcdMaxHardwareErrorVariableSize) - GetVariableHeaderSize ()) {
3226 return EFI_INVALID_PARAMETER;
3227 }
3228 } else {
3229 //
3230 // The size of the VariableName, including the Unicode Null in bytes plus
3231 // the DataSize is limited to maximum size of Max(Auth|Volatile)VariableSize bytes.
3232 //
3233 if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {
3234 if (StrSize (VariableName) + PayloadSize > mVariableModuleGlobal->MaxAuthVariableSize - GetVariableHeaderSize ()) {
3235 DEBUG ((DEBUG_ERROR,
3236 "%a: Failed to set variable '%s' with Guid %g\n",
3237 __FUNCTION__, VariableName, VendorGuid));
3238 DEBUG ((DEBUG_ERROR,
3239 "NameSize(0x%x) + PayloadSize(0x%x) > "
3240 "MaxAuthVariableSize(0x%x) - HeaderSize(0x%x)\n",
3241 StrSize (VariableName), PayloadSize,
3242 mVariableModuleGlobal->MaxAuthVariableSize,
3243 GetVariableHeaderSize ()
3244 ));
3245 return EFI_INVALID_PARAMETER;
3246 }
3247 } else if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {
3248 if (StrSize (VariableName) + PayloadSize > mVariableModuleGlobal->MaxVariableSize - GetVariableHeaderSize ()) {
3249 DEBUG ((DEBUG_ERROR,
3250 "%a: Failed to set variable '%s' with Guid %g\n",
3251 __FUNCTION__, VariableName, VendorGuid));
3252 DEBUG ((DEBUG_ERROR,
3253 "NameSize(0x%x) + PayloadSize(0x%x) > "
3254 "MaxVariableSize(0x%x) - HeaderSize(0x%x)\n",
3255 StrSize (VariableName), PayloadSize,
3256 mVariableModuleGlobal->MaxVariableSize,
3257 GetVariableHeaderSize ()
3258 ));
3259 return EFI_INVALID_PARAMETER;
3260 }
3261 } else {
3262 if (StrSize (VariableName) + PayloadSize > mVariableModuleGlobal->MaxVolatileVariableSize - GetVariableHeaderSize ()) {
3263 DEBUG ((DEBUG_ERROR,
3264 "%a: Failed to set variable '%s' with Guid %g\n",
3265 __FUNCTION__, VariableName, VendorGuid));
3266 DEBUG ((DEBUG_ERROR,
3267 "NameSize(0x%x) + PayloadSize(0x%x) > "
3268 "MaxVolatileVariableSize(0x%x) - HeaderSize(0x%x)\n",
3269 StrSize (VariableName), PayloadSize,
3270 mVariableModuleGlobal->MaxVolatileVariableSize,
3271 GetVariableHeaderSize ()
3272 ));
3273 return EFI_INVALID_PARAMETER;
3274 }
3275 }
3276 }
3277
3278 //
3279 // Special Handling for MOR Lock variable.
3280 //
3281 Status = SetVariableCheckHandlerMor (VariableName, VendorGuid, Attributes, PayloadSize, (VOID *) ((UINTN) Data + DataSize - PayloadSize));
3282 if (Status == EFI_ALREADY_STARTED) {
3283 //
3284 // EFI_ALREADY_STARTED means the SetVariable() action is handled inside of SetVariableCheckHandlerMor().
3285 // Variable driver can just return SUCCESS.
3286 //
3287 return EFI_SUCCESS;
3288 }
3289 if (EFI_ERROR (Status)) {
3290 return Status;
3291 }
3292
3293 Status = VarCheckLibSetVariableCheck (VariableName, VendorGuid, Attributes, PayloadSize, (VOID *) ((UINTN) Data + DataSize - PayloadSize), mRequestSource);
3294 if (EFI_ERROR (Status)) {
3295 return Status;
3296 }
3297
3298 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
3299
3300 //
3301 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.
3302 //
3303 if (1 < InterlockedIncrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState)) {
3304 Point = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;
3305 //
3306 // Parse non-volatile variable data and get last variable offset.
3307 //
3308 NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point);
3309 while (IsValidVariableHeader (NextVariable, GetEndPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point))) {
3310 NextVariable = GetNextVariablePtr (NextVariable);
3311 }
3312 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) Point;
3313 }
3314
3315 //
3316 // Check whether the input variable is already existed.
3317 //
3318 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, TRUE);
3319 if (!EFI_ERROR (Status)) {
3320 if (((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) && AtRuntime ()) {
3321 Status = EFI_WRITE_PROTECTED;
3322 goto Done;
3323 }
3324 if (Attributes != 0 && (Attributes & (~EFI_VARIABLE_APPEND_WRITE)) != Variable.CurrPtr->Attributes) {
3325 //
3326 // If a preexisting variable is rewritten with different attributes, SetVariable() shall not
3327 // modify the variable and shall return EFI_INVALID_PARAMETER. Two exceptions to this rule:
3328 // 1. No access attributes specified
3329 // 2. The only attribute differing is EFI_VARIABLE_APPEND_WRITE
3330 //
3331 Status = EFI_INVALID_PARAMETER;
3332 DEBUG ((EFI_D_INFO, "[Variable]: Rewritten a preexisting variable(0x%08x) with different attributes(0x%08x) - %g:%s\n", Variable.CurrPtr->Attributes, Attributes, VendorGuid, VariableName));
3333 goto Done;
3334 }
3335 }
3336
3337 if (!FeaturePcdGet (PcdUefiVariableDefaultLangDeprecate)) {
3338 //
3339 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.
3340 //
3341 Status = AutoUpdateLangVariable (VariableName, Data, DataSize);
3342 if (EFI_ERROR (Status)) {
3343 //
3344 // The auto update operation failed, directly return to avoid inconsistency between PlatformLang and Lang.
3345 //
3346 goto Done;
3347 }
3348 }
3349
3350 if (mVariableModuleGlobal->VariableGlobal.AuthSupport) {
3351 Status = AuthVariableLibProcessVariable (VariableName, VendorGuid, Data, DataSize, Attributes);
3352 } else {
3353 Status = UpdateVariable (VariableName, VendorGuid, Data, DataSize, Attributes, 0, 0, &Variable, NULL);
3354 }
3355
3356 Done:
3357 InterlockedDecrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState);
3358 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
3359
3360 if (!AtRuntime ()) {
3361 if (!EFI_ERROR (Status)) {
3362 SecureBootHook (
3363 VariableName,
3364 VendorGuid
3365 );
3366 }
3367 }
3368
3369 return Status;
3370 }
3371
3372 /**
3373
3374 This code returns information about the EFI variables.
3375
3376 Caution: This function may receive untrusted input.
3377 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
3378
3379 @param Attributes Attributes bitmask to specify the type of variables
3380 on which to return information.
3381 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
3382 for the EFI variables associated with the attributes specified.
3383 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
3384 for EFI variables associated with the attributes specified.
3385 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
3386 associated with the attributes specified.
3387
3388 @return EFI_SUCCESS Query successfully.
3389
3390 **/
3391 EFI_STATUS
3392 EFIAPI
3393 VariableServiceQueryVariableInfoInternal (
3394 IN UINT32 Attributes,
3395 OUT UINT64 *MaximumVariableStorageSize,
3396 OUT UINT64 *RemainingVariableStorageSize,
3397 OUT UINT64 *MaximumVariableSize
3398 )
3399 {
3400 VARIABLE_HEADER *Variable;
3401 VARIABLE_HEADER *NextVariable;
3402 UINT64 VariableSize;
3403 VARIABLE_STORE_HEADER *VariableStoreHeader;
3404 UINT64 CommonVariableTotalSize;
3405 UINT64 HwErrVariableTotalSize;
3406 EFI_STATUS Status;
3407 VARIABLE_POINTER_TRACK VariablePtrTrack;
3408
3409 CommonVariableTotalSize = 0;
3410 HwErrVariableTotalSize = 0;
3411
3412 if((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {
3413 //
3414 // Query is Volatile related.
3415 //
3416 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);
3417 } else {
3418 //
3419 // Query is Non-Volatile related.
3420 //
3421 VariableStoreHeader = mNvVariableCache;
3422 }
3423
3424 //
3425 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
3426 // with the storage size (excluding the storage header size).
3427 //
3428 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);
3429
3430 //
3431 // Harware error record variable needs larger size.
3432 //
3433 if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {
3434 *MaximumVariableStorageSize = PcdGet32 (PcdHwErrStorageSize);
3435 *MaximumVariableSize = PcdGet32 (PcdMaxHardwareErrorVariableSize) - GetVariableHeaderSize ();
3436 } else {
3437 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {
3438 if (AtRuntime ()) {
3439 *MaximumVariableStorageSize = mVariableModuleGlobal->CommonRuntimeVariableSpace;
3440 } else {
3441 *MaximumVariableStorageSize = mVariableModuleGlobal->CommonVariableSpace;
3442 }
3443 }
3444
3445 //
3446 // Let *MaximumVariableSize be Max(Auth|Volatile)VariableSize with the exception of the variable header size.
3447 //
3448 if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {
3449 *MaximumVariableSize = mVariableModuleGlobal->MaxAuthVariableSize - GetVariableHeaderSize ();
3450 } else if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {
3451 *MaximumVariableSize = mVariableModuleGlobal->MaxVariableSize - GetVariableHeaderSize ();
3452 } else {
3453 *MaximumVariableSize = mVariableModuleGlobal->MaxVolatileVariableSize - GetVariableHeaderSize ();
3454 }
3455 }
3456
3457 //
3458 // Point to the starting address of the variables.
3459 //
3460 Variable = GetStartPointer (VariableStoreHeader);
3461
3462 //
3463 // Now walk through the related variable store.
3464 //
3465 while (IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))) {
3466 NextVariable = GetNextVariablePtr (Variable);
3467 VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;
3468
3469 if (AtRuntime ()) {
3470 //
3471 // We don't take the state of the variables in mind
3472 // when calculating RemainingVariableStorageSize,
3473 // since the space occupied by variables not marked with
3474 // VAR_ADDED is not allowed to be reclaimed in Runtime.
3475 //
3476 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
3477 HwErrVariableTotalSize += VariableSize;
3478 } else {
3479 CommonVariableTotalSize += VariableSize;
3480 }
3481 } else {
3482 //
3483 // Only care about Variables with State VAR_ADDED, because
3484 // the space not marked as VAR_ADDED is reclaimable now.
3485 //
3486 if (Variable->State == VAR_ADDED) {
3487 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
3488 HwErrVariableTotalSize += VariableSize;
3489 } else {
3490 CommonVariableTotalSize += VariableSize;
3491 }
3492 } else if (Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
3493 //
3494 // If it is a IN_DELETED_TRANSITION variable,
3495 // and there is not also a same ADDED one at the same time,
3496 // this IN_DELETED_TRANSITION variable is valid.
3497 //
3498 VariablePtrTrack.StartPtr = GetStartPointer (VariableStoreHeader);
3499 VariablePtrTrack.EndPtr = GetEndPointer (VariableStoreHeader);
3500 Status = FindVariableEx (
3501 GetVariableNamePtr (Variable),
3502 GetVendorGuidPtr (Variable),
3503 FALSE,
3504 &VariablePtrTrack
3505 );
3506 if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State != VAR_ADDED) {
3507 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
3508 HwErrVariableTotalSize += VariableSize;
3509 } else {
3510 CommonVariableTotalSize += VariableSize;
3511 }
3512 }
3513 }
3514 }
3515
3516 //
3517 // Go to the next one.
3518 //
3519 Variable = NextVariable;
3520 }
3521
3522 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD){
3523 *RemainingVariableStorageSize = *MaximumVariableStorageSize - HwErrVariableTotalSize;
3524 } else {
3525 if (*MaximumVariableStorageSize < CommonVariableTotalSize) {
3526 *RemainingVariableStorageSize = 0;
3527 } else {
3528 *RemainingVariableStorageSize = *MaximumVariableStorageSize - CommonVariableTotalSize;
3529 }
3530 }
3531
3532 if (*RemainingVariableStorageSize < GetVariableHeaderSize ()) {
3533 *MaximumVariableSize = 0;
3534 } else if ((*RemainingVariableStorageSize - GetVariableHeaderSize ()) < *MaximumVariableSize) {
3535 *MaximumVariableSize = *RemainingVariableStorageSize - GetVariableHeaderSize ();
3536 }
3537
3538 return EFI_SUCCESS;
3539 }
3540
3541 /**
3542
3543 This code returns information about the EFI variables.
3544
3545 Caution: This function may receive untrusted input.
3546 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
3547
3548 @param Attributes Attributes bitmask to specify the type of variables
3549 on which to return information.
3550 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
3551 for the EFI variables associated with the attributes specified.
3552 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
3553 for EFI variables associated with the attributes specified.
3554 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
3555 associated with the attributes specified.
3556
3557 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
3558 @return EFI_SUCCESS Query successfully.
3559 @return EFI_UNSUPPORTED The attribute is not supported on this platform.
3560
3561 **/
3562 EFI_STATUS
3563 EFIAPI
3564 VariableServiceQueryVariableInfo (
3565 IN UINT32 Attributes,
3566 OUT UINT64 *MaximumVariableStorageSize,
3567 OUT UINT64 *RemainingVariableStorageSize,
3568 OUT UINT64 *MaximumVariableSize
3569 )
3570 {
3571 EFI_STATUS Status;
3572
3573 if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {
3574 return EFI_INVALID_PARAMETER;
3575 }
3576
3577 if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {
3578 //
3579 // Deprecated attribute, make this check as highest priority.
3580 //
3581 return EFI_UNSUPPORTED;
3582 }
3583
3584 if ((Attributes & EFI_VARIABLE_ATTRIBUTES_MASK) == 0) {
3585 //
3586 // Make sure the Attributes combination is supported by the platform.
3587 //
3588 return EFI_UNSUPPORTED;
3589 } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {
3590 //
3591 // Make sure if runtime bit is set, boot service bit is set also.
3592 //
3593 return EFI_INVALID_PARAMETER;
3594 } else if (AtRuntime () && ((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0)) {
3595 //
3596 // Make sure RT Attribute is set if we are in Runtime phase.
3597 //
3598 return EFI_INVALID_PARAMETER;
3599 } else if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
3600 //
3601 // Make sure Hw Attribute is set with NV.
3602 //
3603 return EFI_INVALID_PARAMETER;
3604 } else if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {
3605 if (!mVariableModuleGlobal->VariableGlobal.AuthSupport) {
3606 //
3607 // Not support authenticated variable write.
3608 //
3609 return EFI_UNSUPPORTED;
3610 }
3611 } else if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {
3612 if (PcdGet32 (PcdHwErrStorageSize) == 0) {
3613 //
3614 // Not support harware error record variable variable.
3615 //
3616 return EFI_UNSUPPORTED;
3617 }
3618 }
3619
3620 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
3621
3622 Status = VariableServiceQueryVariableInfoInternal (
3623 Attributes,
3624 MaximumVariableStorageSize,
3625 RemainingVariableStorageSize,
3626 MaximumVariableSize
3627 );
3628
3629 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
3630 return Status;
3631 }
3632
3633 /**
3634 This function reclaims variable storage if free size is below the threshold.
3635
3636 Caution: This function may be invoked at SMM mode.
3637 Care must be taken to make sure not security issue.
3638
3639 **/
3640 VOID
3641 ReclaimForOS(
3642 VOID
3643 )
3644 {
3645 EFI_STATUS Status;
3646 UINTN RemainingCommonRuntimeVariableSpace;
3647 UINTN RemainingHwErrVariableSpace;
3648 STATIC BOOLEAN Reclaimed;
3649
3650 //
3651 // This function will be called only once at EndOfDxe or ReadyToBoot event.
3652 //
3653 if (Reclaimed) {
3654 return;
3655 }
3656 Reclaimed = TRUE;
3657
3658 Status = EFI_SUCCESS;
3659
3660 if (mVariableModuleGlobal->CommonRuntimeVariableSpace < mVariableModuleGlobal->CommonVariableTotalSize) {
3661 RemainingCommonRuntimeVariableSpace = 0;
3662 } else {
3663 RemainingCommonRuntimeVariableSpace = mVariableModuleGlobal->CommonRuntimeVariableSpace - mVariableModuleGlobal->CommonVariableTotalSize;
3664 }
3665
3666 RemainingHwErrVariableSpace = PcdGet32 (PcdHwErrStorageSize) - mVariableModuleGlobal->HwErrVariableTotalSize;
3667
3668 //
3669 // Check if the free area is below a threshold.
3670 //
3671 if (((RemainingCommonRuntimeVariableSpace < mVariableModuleGlobal->MaxVariableSize) ||
3672 (RemainingCommonRuntimeVariableSpace < mVariableModuleGlobal->MaxAuthVariableSize)) ||
3673 ((PcdGet32 (PcdHwErrStorageSize) != 0) &&
3674 (RemainingHwErrVariableSpace < PcdGet32 (PcdMaxHardwareErrorVariableSize)))){
3675 Status = Reclaim (
3676 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,
3677 &mVariableModuleGlobal->NonVolatileLastVariableOffset,
3678 FALSE,
3679 NULL,
3680 NULL,
3681 0
3682 );
3683 ASSERT_EFI_ERROR (Status);
3684 }
3685 }
3686
3687 /**
3688 Get non-volatile maximum variable size.
3689
3690 @return Non-volatile maximum variable size.
3691
3692 **/
3693 UINTN
3694 GetNonVolatileMaxVariableSize (
3695 VOID
3696 )
3697 {
3698 if (PcdGet32 (PcdHwErrStorageSize) != 0) {
3699 return MAX (MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxAuthVariableSize)),
3700 PcdGet32 (PcdMaxHardwareErrorVariableSize));
3701 } else {
3702 return MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxAuthVariableSize));
3703 }
3704 }
3705
3706 /**
3707 Get maximum variable size, covering both non-volatile and volatile variables.
3708
3709 @return Maximum variable size.
3710
3711 **/
3712 UINTN
3713 GetMaxVariableSize (
3714 VOID
3715 )
3716 {
3717 UINTN MaxVariableSize;
3718
3719 MaxVariableSize = GetNonVolatileMaxVariableSize();
3720 //
3721 // The condition below fails implicitly if PcdMaxVolatileVariableSize equals
3722 // the default zero value.
3723 //
3724 if (MaxVariableSize < PcdGet32 (PcdMaxVolatileVariableSize)) {
3725 MaxVariableSize = PcdGet32 (PcdMaxVolatileVariableSize);
3726 }
3727 return MaxVariableSize;
3728 }
3729
3730 /**
3731 Init non-volatile variable store.
3732
3733 @param[out] NvFvHeader Output pointer to non-volatile FV header address.
3734
3735 @retval EFI_SUCCESS Function successfully executed.
3736 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
3737 @retval EFI_VOLUME_CORRUPTED Variable Store or Firmware Volume for Variable Store is corrupted.
3738
3739 **/
3740 EFI_STATUS
3741 InitNonVolatileVariableStore (
3742 OUT EFI_FIRMWARE_VOLUME_HEADER **NvFvHeader
3743 )
3744 {
3745 EFI_FIRMWARE_VOLUME_HEADER *FvHeader;
3746 VARIABLE_HEADER *Variable;
3747 VARIABLE_HEADER *NextVariable;
3748 EFI_PHYSICAL_ADDRESS VariableStoreBase;
3749 UINT64 VariableStoreLength;
3750 UINTN VariableSize;
3751 EFI_HOB_GUID_TYPE *GuidHob;
3752 EFI_PHYSICAL_ADDRESS NvStorageBase;
3753 UINT8 *NvStorageData;
3754 UINT32 NvStorageSize;
3755 FAULT_TOLERANT_WRITE_LAST_WRITE_DATA *FtwLastWriteData;
3756 UINT32 BackUpOffset;
3757 UINT32 BackUpSize;
3758 UINT32 HwErrStorageSize;
3759 UINT32 MaxUserNvVariableSpaceSize;
3760 UINT32 BoottimeReservedNvVariableSpaceSize;
3761 EFI_STATUS Status;
3762 VOID *FtwProtocol;
3763
3764 mVariableModuleGlobal->FvbInstance = NULL;
3765
3766 //
3767 // Allocate runtime memory used for a memory copy of the FLASH region.
3768 // Keep the memory and the FLASH in sync as updates occur.
3769 //
3770 NvStorageSize = PcdGet32 (PcdFlashNvStorageVariableSize);
3771 NvStorageData = AllocateRuntimeZeroPool (NvStorageSize);
3772 if (NvStorageData == NULL) {
3773 return EFI_OUT_OF_RESOURCES;
3774 }
3775
3776 NvStorageBase = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);
3777 if (NvStorageBase == 0) {
3778 NvStorageBase = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);
3779 }
3780 //
3781 // Copy NV storage data to the memory buffer.
3782 //
3783 CopyMem (NvStorageData, (UINT8 *) (UINTN) NvStorageBase, NvStorageSize);
3784
3785 Status = GetFtwProtocol ((VOID **)&FtwProtocol);
3786 //
3787 // If FTW protocol has been installed, no need to check FTW last write data hob.
3788 //
3789 if (EFI_ERROR (Status)) {
3790 //
3791 // Check the FTW last write data hob.
3792 //
3793 GuidHob = GetFirstGuidHob (&gEdkiiFaultTolerantWriteGuid);
3794 if (GuidHob != NULL) {
3795 FtwLastWriteData = (FAULT_TOLERANT_WRITE_LAST_WRITE_DATA *) GET_GUID_HOB_DATA (GuidHob);
3796 if (FtwLastWriteData->TargetAddress == NvStorageBase) {
3797 DEBUG ((EFI_D_INFO, "Variable: NV storage is backed up in spare block: 0x%x\n", (UINTN) FtwLastWriteData->SpareAddress));
3798 //
3799 // Copy the backed up NV storage data to the memory buffer from spare block.
3800 //
3801 CopyMem (NvStorageData, (UINT8 *) (UINTN) (FtwLastWriteData->SpareAddress), NvStorageSize);
3802 } else if ((FtwLastWriteData->TargetAddress > NvStorageBase) &&
3803 (FtwLastWriteData->TargetAddress < (NvStorageBase + NvStorageSize))) {
3804 //
3805 // Flash NV storage from the Offset is backed up in spare block.
3806 //
3807 BackUpOffset = (UINT32) (FtwLastWriteData->TargetAddress - NvStorageBase);
3808 BackUpSize = NvStorageSize - BackUpOffset;
3809 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));
3810 //
3811 // Copy the partial backed up NV storage data to the memory buffer from spare block.
3812 //
3813 CopyMem (NvStorageData + BackUpOffset, (UINT8 *) (UINTN) FtwLastWriteData->SpareAddress, BackUpSize);
3814 }
3815 }
3816 }
3817
3818 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) NvStorageData;
3819
3820 //
3821 // Check if the Firmware Volume is not corrupted
3822 //
3823 if ((FvHeader->Signature != EFI_FVH_SIGNATURE) || (!CompareGuid (&gEfiSystemNvDataFvGuid, &FvHeader->FileSystemGuid))) {
3824 FreePool (NvStorageData);
3825 DEBUG ((EFI_D_ERROR, "Firmware Volume for Variable Store is corrupted\n"));
3826 return EFI_VOLUME_CORRUPTED;
3827 }
3828
3829 VariableStoreBase = (UINTN) FvHeader + FvHeader->HeaderLength;
3830 VariableStoreLength = NvStorageSize - FvHeader->HeaderLength;
3831
3832 mNvFvHeaderCache = FvHeader;
3833 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase;
3834 mNvVariableCache = (VARIABLE_STORE_HEADER *) (UINTN) VariableStoreBase;
3835 if (GetVariableStoreStatus (mNvVariableCache) != EfiValid) {
3836 FreePool (NvStorageData);
3837 mNvFvHeaderCache = NULL;
3838 mNvVariableCache = NULL;
3839 DEBUG((EFI_D_ERROR, "Variable Store header is corrupted\n"));
3840 return EFI_VOLUME_CORRUPTED;
3841 }
3842 ASSERT(mNvVariableCache->Size == VariableStoreLength);
3843
3844 ASSERT (sizeof (VARIABLE_STORE_HEADER) <= VariableStoreLength);
3845
3846 mVariableModuleGlobal->VariableGlobal.AuthFormat = (BOOLEAN)(CompareGuid (&mNvVariableCache->Signature, &gEfiAuthenticatedVariableGuid));
3847
3848 HwErrStorageSize = PcdGet32 (PcdHwErrStorageSize);
3849 MaxUserNvVariableSpaceSize = PcdGet32 (PcdMaxUserNvVariableSpaceSize);
3850 BoottimeReservedNvVariableSpaceSize = PcdGet32 (PcdBoottimeReservedNvVariableSpaceSize);
3851
3852 //
3853 // Note that in EdkII variable driver implementation, Hardware Error Record type variable
3854 // is stored with common variable in the same NV region. So the platform integrator should
3855 // ensure that the value of PcdHwErrStorageSize is less than the value of
3856 // (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)).
3857 //
3858 ASSERT (HwErrStorageSize < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)));
3859 //
3860 // Ensure that the value of PcdMaxUserNvVariableSpaceSize is less than the value of
3861 // (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)) - PcdGet32 (PcdHwErrStorageSize).
3862 //
3863 ASSERT (MaxUserNvVariableSpaceSize < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER) - HwErrStorageSize));
3864 //
3865 // Ensure that the value of PcdBoottimeReservedNvVariableSpaceSize is less than the value of
3866 // (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)) - PcdGet32 (PcdHwErrStorageSize).
3867 //
3868 ASSERT (BoottimeReservedNvVariableSpaceSize < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER) - HwErrStorageSize));
3869
3870 mVariableModuleGlobal->CommonVariableSpace = ((UINTN) VariableStoreLength - sizeof (VARIABLE_STORE_HEADER) - HwErrStorageSize);
3871 mVariableModuleGlobal->CommonMaxUserVariableSpace = ((MaxUserNvVariableSpaceSize != 0) ? MaxUserNvVariableSpaceSize : mVariableModuleGlobal->CommonVariableSpace);
3872 mVariableModuleGlobal->CommonRuntimeVariableSpace = mVariableModuleGlobal->CommonVariableSpace - BoottimeReservedNvVariableSpaceSize;
3873
3874 DEBUG ((EFI_D_INFO, "Variable driver common space: 0x%x 0x%x 0x%x\n", mVariableModuleGlobal->CommonVariableSpace, mVariableModuleGlobal->CommonMaxUserVariableSpace, mVariableModuleGlobal->CommonRuntimeVariableSpace));
3875
3876 //
3877 // The max NV variable size should be < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)).
3878 //
3879 ASSERT (GetNonVolatileMaxVariableSize () < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)));
3880
3881 mVariableModuleGlobal->MaxVariableSize = PcdGet32 (PcdMaxVariableSize);
3882 mVariableModuleGlobal->MaxAuthVariableSize = ((PcdGet32 (PcdMaxAuthVariableSize) != 0) ? PcdGet32 (PcdMaxAuthVariableSize) : mVariableModuleGlobal->MaxVariableSize);
3883
3884 //
3885 // Parse non-volatile variable data and get last variable offset.
3886 //
3887 Variable = GetStartPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase);
3888 while (IsValidVariableHeader (Variable, GetEndPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase))) {
3889 NextVariable = GetNextVariablePtr (Variable);
3890 VariableSize = (UINTN) NextVariable - (UINTN) Variable;
3891 if ((Variable->Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {
3892 mVariableModuleGlobal->HwErrVariableTotalSize += VariableSize;
3893 } else {
3894 mVariableModuleGlobal->CommonVariableTotalSize += VariableSize;
3895 }
3896
3897 Variable = NextVariable;
3898 }
3899 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) Variable - (UINTN) VariableStoreBase;
3900
3901 *NvFvHeader = FvHeader;
3902 return EFI_SUCCESS;
3903 }
3904
3905 /**
3906 Flush the HOB variable to flash.
3907
3908 @param[in] VariableName Name of variable has been updated or deleted.
3909 @param[in] VendorGuid Guid of variable has been updated or deleted.
3910
3911 **/
3912 VOID
3913 FlushHobVariableToFlash (
3914 IN CHAR16 *VariableName,
3915 IN EFI_GUID *VendorGuid
3916 )
3917 {
3918 EFI_STATUS Status;
3919 VARIABLE_STORE_HEADER *VariableStoreHeader;
3920 VARIABLE_HEADER *Variable;
3921 VOID *VariableData;
3922 VARIABLE_POINTER_TRACK VariablePtrTrack;
3923 BOOLEAN ErrorFlag;
3924
3925 ErrorFlag = FALSE;
3926
3927 //
3928 // Flush the HOB variable to flash.
3929 //
3930 if (mVariableModuleGlobal->VariableGlobal.HobVariableBase != 0) {
3931 VariableStoreHeader = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;
3932 //
3933 // Set HobVariableBase to 0, it can avoid SetVariable to call back.
3934 //
3935 mVariableModuleGlobal->VariableGlobal.HobVariableBase = 0;
3936 for ( Variable = GetStartPointer (VariableStoreHeader)
3937 ; IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))
3938 ; Variable = GetNextVariablePtr (Variable)
3939 ) {
3940 if (Variable->State != VAR_ADDED) {
3941 //
3942 // The HOB variable has been set to DELETED state in local.
3943 //
3944 continue;
3945 }
3946 ASSERT ((Variable->Attributes & EFI_VARIABLE_NON_VOLATILE) != 0);
3947 if (VendorGuid == NULL || VariableName == NULL ||
3948 !CompareGuid (VendorGuid, GetVendorGuidPtr (Variable)) ||
3949 StrCmp (VariableName, GetVariableNamePtr (Variable)) != 0) {
3950 VariableData = GetVariableDataPtr (Variable);
3951 FindVariable (GetVariableNamePtr (Variable), GetVendorGuidPtr (Variable), &VariablePtrTrack, &mVariableModuleGlobal->VariableGlobal, FALSE);
3952 Status = UpdateVariable (
3953 GetVariableNamePtr (Variable),
3954 GetVendorGuidPtr (Variable),
3955 VariableData,
3956 DataSizeOfVariable (Variable),
3957 Variable->Attributes,
3958 0,
3959 0,
3960 &VariablePtrTrack,
3961 NULL
3962 );
3963 DEBUG ((EFI_D_INFO, "Variable driver flush the HOB variable to flash: %g %s %r\n", GetVendorGuidPtr (Variable), GetVariableNamePtr (Variable), Status));
3964 } else {
3965 //
3966 // The updated or deleted variable is matched with this HOB variable.
3967 // Don't break here because we will try to set other HOB variables
3968 // since this variable could be set successfully.
3969 //
3970 Status = EFI_SUCCESS;
3971 }
3972 if (!EFI_ERROR (Status)) {
3973 //
3974 // If set variable successful, or the updated or deleted variable is matched with the HOB variable,
3975 // set the HOB variable to DELETED state in local.
3976 //
3977 DEBUG ((EFI_D_INFO, "Variable driver set the HOB variable to DELETED state in local: %g %s\n", GetVendorGuidPtr (Variable), GetVariableNamePtr (Variable)));
3978 Variable->State &= VAR_DELETED;
3979 } else {
3980 ErrorFlag = TRUE;
3981 }
3982 }
3983 if (ErrorFlag) {
3984 //
3985 // We still have HOB variable(s) not flushed in flash.
3986 //
3987 mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VariableStoreHeader;
3988 } else {
3989 //
3990 // All HOB variables have been flushed in flash.
3991 //
3992 DEBUG ((EFI_D_INFO, "Variable driver: all HOB variables have been flushed in flash.\n"));
3993 if (!AtRuntime ()) {
3994 FreePool ((VOID *) VariableStoreHeader);
3995 }
3996 }
3997 }
3998
3999 }
4000
4001 /**
4002 Initializes variable write service after FTW was ready.
4003
4004 @retval EFI_SUCCESS Function successfully executed.
4005 @retval Others Fail to initialize the variable service.
4006
4007 **/
4008 EFI_STATUS
4009 VariableWriteServiceInitialize (
4010 VOID
4011 )
4012 {
4013 EFI_STATUS Status;
4014 UINTN Index;
4015 UINT8 Data;
4016 EFI_PHYSICAL_ADDRESS VariableStoreBase;
4017 EFI_PHYSICAL_ADDRESS NvStorageBase;
4018 VARIABLE_ENTRY_PROPERTY *VariableEntry;
4019
4020 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
4021
4022 NvStorageBase = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);
4023 if (NvStorageBase == 0) {
4024 NvStorageBase = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);
4025 }
4026 VariableStoreBase = NvStorageBase + (mNvFvHeaderCache->HeaderLength);
4027
4028 //
4029 // Let NonVolatileVariableBase point to flash variable store base directly after FTW ready.
4030 //
4031 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase;
4032
4033 //
4034 // Check if the free area is really free.
4035 //
4036 for (Index = mVariableModuleGlobal->NonVolatileLastVariableOffset; Index < mNvVariableCache->Size; Index++) {
4037 Data = ((UINT8 *) mNvVariableCache)[Index];
4038 if (Data != 0xff) {
4039 //
4040 // There must be something wrong in variable store, do reclaim operation.
4041 //
4042 Status = Reclaim (
4043 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,
4044 &mVariableModuleGlobal->NonVolatileLastVariableOffset,
4045 FALSE,
4046 NULL,
4047 NULL,
4048 0
4049 );
4050 if (EFI_ERROR (Status)) {
4051 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
4052 return Status;
4053 }
4054 break;
4055 }
4056 }
4057
4058 FlushHobVariableToFlash (NULL, NULL);
4059
4060 Status = EFI_SUCCESS;
4061 ZeroMem (&mAuthContextOut, sizeof (mAuthContextOut));
4062 if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
4063 //
4064 // Authenticated variable initialize.
4065 //
4066 mAuthContextIn.StructSize = sizeof (AUTH_VAR_LIB_CONTEXT_IN);
4067 mAuthContextIn.MaxAuthVariableSize = mVariableModuleGlobal->MaxAuthVariableSize - GetVariableHeaderSize ();
4068 Status = AuthVariableLibInitialize (&mAuthContextIn, &mAuthContextOut);
4069 if (!EFI_ERROR (Status)) {
4070 DEBUG ((EFI_D_INFO, "Variable driver will work with auth variable support!\n"));
4071 mVariableModuleGlobal->VariableGlobal.AuthSupport = TRUE;
4072 if (mAuthContextOut.AuthVarEntry != NULL) {
4073 for (Index = 0; Index < mAuthContextOut.AuthVarEntryCount; Index++) {
4074 VariableEntry = &mAuthContextOut.AuthVarEntry[Index];
4075 Status = VarCheckLibVariablePropertySet (
4076 VariableEntry->Name,
4077 VariableEntry->Guid,
4078 &VariableEntry->VariableProperty
4079 );
4080 ASSERT_EFI_ERROR (Status);
4081 }
4082 }
4083 } else if (Status == EFI_UNSUPPORTED) {
4084 DEBUG ((EFI_D_INFO, "NOTICE - AuthVariableLibInitialize() returns %r!\n", Status));
4085 DEBUG ((EFI_D_INFO, "Variable driver will continue to work without auth variable support!\n"));
4086 mVariableModuleGlobal->VariableGlobal.AuthSupport = FALSE;
4087 Status = EFI_SUCCESS;
4088 }
4089 }
4090
4091 if (!EFI_ERROR (Status)) {
4092 for (Index = 0; Index < ARRAY_SIZE (mVariableEntryProperty); Index++) {
4093 VariableEntry = &mVariableEntryProperty[Index];
4094 Status = VarCheckLibVariablePropertySet (VariableEntry->Name, VariableEntry->Guid, &VariableEntry->VariableProperty);
4095 ASSERT_EFI_ERROR (Status);
4096 }
4097 }
4098
4099 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
4100
4101 //
4102 // Initialize MOR Lock variable.
4103 //
4104 MorLockInit ();
4105
4106 return Status;
4107 }
4108
4109 /**
4110 Convert normal variable storage to the allocated auth variable storage.
4111
4112 @param[in] NormalVarStorage Pointer to the normal variable storage header
4113
4114 @retval the allocated auth variable storage
4115 **/
4116 VOID *
4117 ConvertNormalVarStorageToAuthVarStorage (
4118 VARIABLE_STORE_HEADER *NormalVarStorage
4119 )
4120 {
4121 VARIABLE_HEADER *StartPtr;
4122 UINT8 *NextPtr;
4123 VARIABLE_HEADER *EndPtr;
4124 UINTN AuthVarStroageSize;
4125 AUTHENTICATED_VARIABLE_HEADER *AuthStartPtr;
4126 VARIABLE_STORE_HEADER *AuthVarStorage;
4127
4128 AuthVarStroageSize = sizeof (VARIABLE_STORE_HEADER);
4129 //
4130 // Set AuthFormat as FALSE for normal variable storage
4131 //
4132 mVariableModuleGlobal->VariableGlobal.AuthFormat = FALSE;
4133
4134 //
4135 // Calculate Auth Variable Storage Size
4136 //
4137 StartPtr = GetStartPointer (NormalVarStorage);
4138 EndPtr = GetEndPointer (NormalVarStorage);
4139 while (StartPtr < EndPtr) {
4140 if (StartPtr->State == VAR_ADDED) {
4141 AuthVarStroageSize = HEADER_ALIGN (AuthVarStroageSize);
4142 AuthVarStroageSize += sizeof (AUTHENTICATED_VARIABLE_HEADER);
4143 AuthVarStroageSize += StartPtr->NameSize + GET_PAD_SIZE (StartPtr->NameSize);
4144 AuthVarStroageSize += StartPtr->DataSize + GET_PAD_SIZE (StartPtr->DataSize);
4145 }
4146 StartPtr = GetNextVariablePtr (StartPtr);
4147 }
4148
4149 //
4150 // Allocate Runtime memory for Auth Variable Storage
4151 //
4152 AuthVarStorage = AllocateRuntimeZeroPool (AuthVarStroageSize);
4153 ASSERT (AuthVarStorage != NULL);
4154 if (AuthVarStorage == NULL) {
4155 return NULL;
4156 }
4157
4158 //
4159 // Copy Variable from Normal storage to Auth storage
4160 //
4161 StartPtr = GetStartPointer (NormalVarStorage);
4162 EndPtr = GetEndPointer (NormalVarStorage);
4163 AuthStartPtr = (AUTHENTICATED_VARIABLE_HEADER *) GetStartPointer (AuthVarStorage);
4164 while (StartPtr < EndPtr) {
4165 if (StartPtr->State == VAR_ADDED) {
4166 AuthStartPtr = (AUTHENTICATED_VARIABLE_HEADER *) HEADER_ALIGN (AuthStartPtr);
4167 //
4168 // Copy Variable Header
4169 //
4170 AuthStartPtr->StartId = StartPtr->StartId;
4171 AuthStartPtr->State = StartPtr->State;
4172 AuthStartPtr->Attributes = StartPtr->Attributes;
4173 AuthStartPtr->NameSize = StartPtr->NameSize;
4174 AuthStartPtr->DataSize = StartPtr->DataSize;
4175 CopyGuid (&AuthStartPtr->VendorGuid, &StartPtr->VendorGuid);
4176 //
4177 // Copy Variable Name
4178 //
4179 NextPtr = (UINT8 *) (AuthStartPtr + 1);
4180 CopyMem (NextPtr, GetVariableNamePtr (StartPtr), AuthStartPtr->NameSize);
4181 //
4182 // Copy Variable Data
4183 //
4184 NextPtr = NextPtr + AuthStartPtr->NameSize + GET_PAD_SIZE (AuthStartPtr->NameSize);
4185 CopyMem (NextPtr, GetVariableDataPtr (StartPtr), AuthStartPtr->DataSize);
4186 //
4187 // Go to next variable
4188 //
4189 AuthStartPtr = (AUTHENTICATED_VARIABLE_HEADER *) (NextPtr + AuthStartPtr->DataSize + GET_PAD_SIZE (AuthStartPtr->DataSize));
4190 }
4191 StartPtr = GetNextVariablePtr (StartPtr);
4192 }
4193 //
4194 // Update Auth Storage Header
4195 //
4196 AuthVarStorage->Format = NormalVarStorage->Format;
4197 AuthVarStorage->State = NormalVarStorage->State;
4198 AuthVarStorage->Size = (UINT32)((UINTN)AuthStartPtr - (UINTN)AuthVarStorage);
4199 CopyGuid (&AuthVarStorage->Signature, &gEfiAuthenticatedVariableGuid);
4200 ASSERT (AuthVarStorage->Size <= AuthVarStroageSize);
4201
4202 //
4203 // Restore AuthFormat
4204 //
4205 mVariableModuleGlobal->VariableGlobal.AuthFormat = TRUE;
4206 return AuthVarStorage;
4207 }
4208
4209 /**
4210 Get HOB variable store.
4211
4212 @param[in] VariableGuid NV variable store signature.
4213
4214 @retval EFI_SUCCESS Function successfully executed.
4215 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
4216
4217 **/
4218 EFI_STATUS
4219 GetHobVariableStore (
4220 IN EFI_GUID *VariableGuid
4221 )
4222 {
4223 VARIABLE_STORE_HEADER *VariableStoreHeader;
4224 UINT64 VariableStoreLength;
4225 EFI_HOB_GUID_TYPE *GuidHob;
4226 BOOLEAN NeedConvertNormalToAuth;
4227
4228 //
4229 // Make sure there is no more than one Variable HOB.
4230 //
4231 DEBUG_CODE (
4232 GuidHob = GetFirstGuidHob (&gEfiAuthenticatedVariableGuid);
4233 if (GuidHob != NULL) {
4234 if ((GetNextGuidHob (&gEfiAuthenticatedVariableGuid, GET_NEXT_HOB (GuidHob)) != NULL)) {
4235 DEBUG ((DEBUG_ERROR, "ERROR: Found two Auth Variable HOBs\n"));
4236 ASSERT (FALSE);
4237 } else if (GetFirstGuidHob (&gEfiVariableGuid) != NULL) {
4238 DEBUG ((DEBUG_ERROR, "ERROR: Found one Auth + one Normal Variable HOBs\n"));
4239 ASSERT (FALSE);
4240 }
4241 } else {
4242 GuidHob = GetFirstGuidHob (&gEfiVariableGuid);
4243 if (GuidHob != NULL) {
4244 if ((GetNextGuidHob (&gEfiVariableGuid, GET_NEXT_HOB (GuidHob)) != NULL)) {
4245 DEBUG ((DEBUG_ERROR, "ERROR: Found two Normal Variable HOBs\n"));
4246 ASSERT (FALSE);
4247 }
4248 }
4249 }
4250 );
4251
4252 //
4253 // Combinations supported:
4254 // 1. Normal NV variable store +
4255 // Normal HOB variable store
4256 // 2. Auth NV variable store +
4257 // Auth HOB variable store
4258 // 3. Auth NV variable store +
4259 // Normal HOB variable store (code will convert it to Auth Format)
4260 //
4261 NeedConvertNormalToAuth = FALSE;
4262 GuidHob = GetFirstGuidHob (VariableGuid);
4263 if (GuidHob == NULL && VariableGuid == &gEfiAuthenticatedVariableGuid) {
4264 //
4265 // Try getting it from normal variable HOB
4266 //
4267 GuidHob = GetFirstGuidHob (&gEfiVariableGuid);
4268 NeedConvertNormalToAuth = TRUE;
4269 }
4270 if (GuidHob != NULL) {
4271 VariableStoreHeader = GET_GUID_HOB_DATA (GuidHob);
4272 VariableStoreLength = GuidHob->Header.HobLength - sizeof (EFI_HOB_GUID_TYPE);
4273 if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) {
4274 if (!NeedConvertNormalToAuth) {
4275 mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) AllocateRuntimeCopyPool ((UINTN) VariableStoreLength, (VOID *) VariableStoreHeader);
4276 } else {
4277 mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) ConvertNormalVarStorageToAuthVarStorage ((VOID *) VariableStoreHeader);
4278 }
4279 if (mVariableModuleGlobal->VariableGlobal.HobVariableBase == 0) {
4280 return EFI_OUT_OF_RESOURCES;
4281 }
4282 } else {
4283 DEBUG ((EFI_D_ERROR, "HOB Variable Store header is corrupted!\n"));
4284 }
4285 }
4286
4287 return EFI_SUCCESS;
4288 }
4289
4290 /**
4291 Initializes variable store area for non-volatile and volatile variable.
4292
4293 @retval EFI_SUCCESS Function successfully executed.
4294 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
4295
4296 **/
4297 EFI_STATUS
4298 VariableCommonInitialize (
4299 VOID
4300 )
4301 {
4302 EFI_STATUS Status;
4303 VARIABLE_STORE_HEADER *VolatileVariableStore;
4304 UINTN ScratchSize;
4305 EFI_GUID *VariableGuid;
4306 EFI_FIRMWARE_VOLUME_HEADER *NvFvHeader;
4307
4308 //
4309 // Allocate runtime memory for variable driver global structure.
4310 //
4311 mVariableModuleGlobal = AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL));
4312 if (mVariableModuleGlobal == NULL) {
4313 return EFI_OUT_OF_RESOURCES;
4314 }
4315
4316 InitializeLock (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock, TPL_NOTIFY);
4317
4318 //
4319 // Init non-volatile variable store.
4320 //
4321 NvFvHeader = NULL;
4322 Status = InitNonVolatileVariableStore (&NvFvHeader);
4323 if (EFI_ERROR (Status)) {
4324 FreePool (mVariableModuleGlobal);
4325 return Status;
4326 }
4327
4328 //
4329 // mVariableModuleGlobal->VariableGlobal.AuthFormat
4330 // has been initialized in InitNonVolatileVariableStore().
4331 //
4332 if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
4333 DEBUG ((EFI_D_INFO, "Variable driver will work with auth variable format!\n"));
4334 //
4335 // Set AuthSupport to FALSE first, VariableWriteServiceInitialize() will initialize it.
4336 //
4337 mVariableModuleGlobal->VariableGlobal.AuthSupport = FALSE;
4338 VariableGuid = &gEfiAuthenticatedVariableGuid;
4339 } else {
4340 DEBUG ((EFI_D_INFO, "Variable driver will work without auth variable support!\n"));
4341 mVariableModuleGlobal->VariableGlobal.AuthSupport = FALSE;
4342 VariableGuid = &gEfiVariableGuid;
4343 }
4344
4345 //
4346 // Get HOB variable store.
4347 //
4348 Status = GetHobVariableStore (VariableGuid);
4349 if (EFI_ERROR (Status)) {
4350 FreePool (NvFvHeader);
4351 FreePool (mVariableModuleGlobal);
4352 return Status;
4353 }
4354
4355 mVariableModuleGlobal->MaxVolatileVariableSize = ((PcdGet32 (PcdMaxVolatileVariableSize) != 0) ?
4356 PcdGet32 (PcdMaxVolatileVariableSize) :
4357 mVariableModuleGlobal->MaxVariableSize
4358 );
4359 //
4360 // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.
4361 //
4362 ScratchSize = GetMaxVariableSize ();
4363 mVariableModuleGlobal->ScratchBufferSize = ScratchSize;
4364 VolatileVariableStore = AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize) + ScratchSize);
4365 if (VolatileVariableStore == NULL) {
4366 if (mVariableModuleGlobal->VariableGlobal.HobVariableBase != 0) {
4367 FreePool ((VOID *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase);
4368 }
4369 FreePool (NvFvHeader);
4370 FreePool (mVariableModuleGlobal);
4371 return EFI_OUT_OF_RESOURCES;
4372 }
4373
4374 SetMem (VolatileVariableStore, PcdGet32 (PcdVariableStoreSize) + ScratchSize, 0xff);
4375
4376 //
4377 // Initialize Variable Specific Data.
4378 //
4379 mVariableModuleGlobal->VariableGlobal.VolatileVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VolatileVariableStore;
4380 mVariableModuleGlobal->VolatileLastVariableOffset = (UINTN) GetStartPointer (VolatileVariableStore) - (UINTN) VolatileVariableStore;
4381
4382 CopyGuid (&VolatileVariableStore->Signature, VariableGuid);
4383 VolatileVariableStore->Size = PcdGet32 (PcdVariableStoreSize);
4384 VolatileVariableStore->Format = VARIABLE_STORE_FORMATTED;
4385 VolatileVariableStore->State = VARIABLE_STORE_HEALTHY;
4386 VolatileVariableStore->Reserved = 0;
4387 VolatileVariableStore->Reserved1 = 0;
4388
4389 return EFI_SUCCESS;
4390 }
4391
4392
4393 /**
4394 Get the proper fvb handle and/or fvb protocol by the given Flash address.
4395
4396 @param[in] Address The Flash address.
4397 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.
4398 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.
4399
4400 **/
4401 EFI_STATUS
4402 GetFvbInfoByAddress (
4403 IN EFI_PHYSICAL_ADDRESS Address,
4404 OUT EFI_HANDLE *FvbHandle OPTIONAL,
4405 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvbProtocol OPTIONAL
4406 )
4407 {
4408 EFI_STATUS Status;
4409 EFI_HANDLE *HandleBuffer;
4410 UINTN HandleCount;
4411 UINTN Index;
4412 EFI_PHYSICAL_ADDRESS FvbBaseAddress;
4413 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;
4414 EFI_FVB_ATTRIBUTES_2 Attributes;
4415 UINTN BlockSize;
4416 UINTN NumberOfBlocks;
4417
4418 HandleBuffer = NULL;
4419 //
4420 // Get all FVB handles.
4421 //
4422 Status = GetFvbCountAndBuffer (&HandleCount, &HandleBuffer);
4423 if (EFI_ERROR (Status)) {
4424 return EFI_NOT_FOUND;
4425 }
4426
4427 //
4428 // Get the FVB to access variable store.
4429 //
4430 Fvb = NULL;
4431 for (Index = 0; Index < HandleCount; Index += 1, Status = EFI_NOT_FOUND, Fvb = NULL) {
4432 Status = GetFvbByHandle (HandleBuffer[Index], &Fvb);
4433 if (EFI_ERROR (Status)) {
4434 Status = EFI_NOT_FOUND;
4435 break;
4436 }
4437
4438 //
4439 // Ensure this FVB protocol supported Write operation.
4440 //
4441 Status = Fvb->GetAttributes (Fvb, &Attributes);
4442 if (EFI_ERROR (Status) || ((Attributes & EFI_FVB2_WRITE_STATUS) == 0)) {
4443 continue;
4444 }
4445
4446 //
4447 // Compare the address and select the right one.
4448 //
4449 Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);
4450 if (EFI_ERROR (Status)) {
4451 continue;
4452 }
4453
4454 //
4455 // Assume one FVB has one type of BlockSize.
4456 //
4457 Status = Fvb->GetBlockSize (Fvb, 0, &BlockSize, &NumberOfBlocks);
4458 if (EFI_ERROR (Status)) {
4459 continue;
4460 }
4461
4462 if ((Address >= FvbBaseAddress) && (Address < (FvbBaseAddress + BlockSize * NumberOfBlocks))) {
4463 if (FvbHandle != NULL) {
4464 *FvbHandle = HandleBuffer[Index];
4465 }
4466 if (FvbProtocol != NULL) {
4467 *FvbProtocol = Fvb;
4468 }
4469 Status = EFI_SUCCESS;
4470 break;
4471 }
4472 }
4473 FreePool (HandleBuffer);
4474
4475 if (Fvb == NULL) {
4476 Status = EFI_NOT_FOUND;
4477 }
4478
4479 return Status;
4480 }
4481