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