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