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