]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
Fix potential overflow for SetVariable interface
[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 ASSERT (CacheVariable->InDeletedTransitionPtr != NULL);
1500 CacheVariable->InDeletedTransitionPtr->State = State;
1501 }
1502 } else {
1503 goto Done;
1504 }
1505 }
1506
1507 State = Variable->CurrPtr->State;
1508 State &= VAR_DELETED;
1509
1510 Status = UpdateVariableStore (
1511 &mVariableModuleGlobal->VariableGlobal,
1512 Variable->Volatile,
1513 FALSE,
1514 Fvb,
1515 (UINTN) &Variable->CurrPtr->State,
1516 sizeof (UINT8),
1517 &State
1518 );
1519 if (!EFI_ERROR (Status)) {
1520 UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, FALSE, TRUE, FALSE);
1521 if (!Variable->Volatile) {
1522 CacheVariable->CurrPtr->State = State;
1523 FlushHobVariableToFlash (VariableName, VendorGuid);
1524 }
1525 }
1526 goto Done;
1527 }
1528 //
1529 // If the variable is marked valid, and the same data has been passed in,
1530 // then return to the caller immediately.
1531 //
1532 if (DataSizeOfVariable (Variable->CurrPtr) == DataSize &&
1533 (CompareMem (Data, GetVariableDataPtr (Variable->CurrPtr), DataSize) == 0)) {
1534
1535 UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, TRUE, FALSE, FALSE);
1536 Status = EFI_SUCCESS;
1537 goto Done;
1538 } else if ((Variable->CurrPtr->State == VAR_ADDED) ||
1539 (Variable->CurrPtr->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {
1540
1541 //
1542 // Mark the old variable as in delete transition.
1543 //
1544 State = Variable->CurrPtr->State;
1545 State &= VAR_IN_DELETED_TRANSITION;
1546
1547 Status = UpdateVariableStore (
1548 &mVariableModuleGlobal->VariableGlobal,
1549 Variable->Volatile,
1550 FALSE,
1551 Fvb,
1552 (UINTN) &Variable->CurrPtr->State,
1553 sizeof (UINT8),
1554 &State
1555 );
1556 if (EFI_ERROR (Status)) {
1557 goto Done;
1558 }
1559 if (!Variable->Volatile) {
1560 CacheVariable->CurrPtr->State = State;
1561 }
1562 }
1563 } else {
1564 //
1565 // Not found existing variable. Create a new variable.
1566 //
1567
1568 //
1569 // Make sure we are trying to create a new variable.
1570 // Setting a data variable with zero DataSize or no access attributes means to delete it.
1571 //
1572 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {
1573 Status = EFI_NOT_FOUND;
1574 goto Done;
1575 }
1576
1577 //
1578 // Only variable have NV|RT attribute can be created in Runtime.
1579 //
1580 if (AtRuntime () &&
1581 (((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) || ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0))) {
1582 Status = EFI_INVALID_PARAMETER;
1583 goto Done;
1584 }
1585 }
1586
1587 //
1588 // Function part - create a new variable and copy the data.
1589 // Both update a variable and create a variable will come here.
1590
1591 //
1592 // Tricky part: Use scratch data area at the end of volatile variable store
1593 // as a temporary storage.
1594 //
1595 NextVariable = GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase));
1596 ScratchSize = MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize));
1597
1598 SetMem (NextVariable, ScratchSize, 0xff);
1599
1600 NextVariable->StartId = VARIABLE_DATA;
1601 NextVariable->Attributes = Attributes;
1602 //
1603 // NextVariable->State = VAR_ADDED;
1604 //
1605 NextVariable->Reserved = 0;
1606 VarNameOffset = sizeof (VARIABLE_HEADER);
1607 VarNameSize = StrSize (VariableName);
1608 CopyMem (
1609 (UINT8 *) ((UINTN) NextVariable + VarNameOffset),
1610 VariableName,
1611 VarNameSize
1612 );
1613 VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);
1614 CopyMem (
1615 (UINT8 *) ((UINTN) NextVariable + VarDataOffset),
1616 Data,
1617 DataSize
1618 );
1619 CopyMem (&NextVariable->VendorGuid, VendorGuid, sizeof (EFI_GUID));
1620 //
1621 // There will be pad bytes after Data, the NextVariable->NameSize and
1622 // NextVariable->DataSize should not include pad size so that variable
1623 // service can get actual size in GetVariable.
1624 //
1625 NextVariable->NameSize = (UINT32)VarNameSize;
1626 NextVariable->DataSize = (UINT32)DataSize;
1627
1628 //
1629 // The actual size of the variable that stores in storage should
1630 // include pad size.
1631 //
1632 VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);
1633 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {
1634 //
1635 // Create a nonvolatile variable.
1636 //
1637 Volatile = FALSE;
1638 NonVolatileVarableStoreSize = ((VARIABLE_STORE_HEADER *)(UINTN)(mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase))->Size;
1639 if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0)
1640 && ((VarSize + mVariableModuleGlobal->HwErrVariableTotalSize) > PcdGet32 (PcdHwErrStorageSize)))
1641 || (((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0)
1642 && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > NonVolatileVarableStoreSize - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize)))) {
1643 if (AtRuntime ()) {
1644 Status = EFI_OUT_OF_RESOURCES;
1645 goto Done;
1646 }
1647 //
1648 // Perform garbage collection & reclaim operation.
1649 //
1650 Status = Reclaim (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,
1651 &mVariableModuleGlobal->NonVolatileLastVariableOffset, FALSE, Variable, FALSE);
1652 if (EFI_ERROR (Status)) {
1653 goto Done;
1654 }
1655 //
1656 // If still no enough space, return out of resources.
1657 //
1658 if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0)
1659 && ((VarSize + mVariableModuleGlobal->HwErrVariableTotalSize) > PcdGet32 (PcdHwErrStorageSize)))
1660 || (((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0)
1661 && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > NonVolatileVarableStoreSize - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize)))) {
1662 Status = EFI_OUT_OF_RESOURCES;
1663 goto Done;
1664 }
1665 if (Variable->CurrPtr != NULL) {
1666 CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN) CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr));
1667 CacheVariable->InDeletedTransitionPtr = NULL;
1668 }
1669 }
1670 //
1671 // Four steps
1672 // 1. Write variable header
1673 // 2. Set variable state to header valid
1674 // 3. Write variable data
1675 // 4. Set variable state to valid
1676 //
1677 //
1678 // Step 1:
1679 //
1680 CacheOffset = mVariableModuleGlobal->NonVolatileLastVariableOffset;
1681 Status = UpdateVariableStore (
1682 &mVariableModuleGlobal->VariableGlobal,
1683 FALSE,
1684 TRUE,
1685 Fvb,
1686 mVariableModuleGlobal->NonVolatileLastVariableOffset,
1687 sizeof (VARIABLE_HEADER),
1688 (UINT8 *) NextVariable
1689 );
1690
1691 if (EFI_ERROR (Status)) {
1692 goto Done;
1693 }
1694
1695 //
1696 // Step 2:
1697 //
1698 NextVariable->State = VAR_HEADER_VALID_ONLY;
1699 Status = UpdateVariableStore (
1700 &mVariableModuleGlobal->VariableGlobal,
1701 FALSE,
1702 TRUE,
1703 Fvb,
1704 mVariableModuleGlobal->NonVolatileLastVariableOffset + OFFSET_OF (VARIABLE_HEADER, State),
1705 sizeof (UINT8),
1706 &NextVariable->State
1707 );
1708
1709 if (EFI_ERROR (Status)) {
1710 goto Done;
1711 }
1712 //
1713 // Step 3:
1714 //
1715 Status = UpdateVariableStore (
1716 &mVariableModuleGlobal->VariableGlobal,
1717 FALSE,
1718 TRUE,
1719 Fvb,
1720 mVariableModuleGlobal->NonVolatileLastVariableOffset + sizeof (VARIABLE_HEADER),
1721 (UINT32) VarSize - sizeof (VARIABLE_HEADER),
1722 (UINT8 *) NextVariable + sizeof (VARIABLE_HEADER)
1723 );
1724
1725 if (EFI_ERROR (Status)) {
1726 goto Done;
1727 }
1728 //
1729 // Step 4:
1730 //
1731 NextVariable->State = VAR_ADDED;
1732 Status = UpdateVariableStore (
1733 &mVariableModuleGlobal->VariableGlobal,
1734 FALSE,
1735 TRUE,
1736 Fvb,
1737 mVariableModuleGlobal->NonVolatileLastVariableOffset + OFFSET_OF (VARIABLE_HEADER, State),
1738 sizeof (UINT8),
1739 &NextVariable->State
1740 );
1741
1742 if (EFI_ERROR (Status)) {
1743 goto Done;
1744 }
1745
1746 mVariableModuleGlobal->NonVolatileLastVariableOffset += HEADER_ALIGN (VarSize);
1747
1748 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {
1749 mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VarSize);
1750 } else {
1751 mVariableModuleGlobal->CommonVariableTotalSize += HEADER_ALIGN (VarSize);
1752 }
1753 //
1754 // update the memory copy of Flash region.
1755 //
1756 CopyMem ((UINT8 *)mNvVariableCache + CacheOffset, (UINT8 *)NextVariable, VarSize);
1757 } else {
1758 //
1759 // Create a volatile variable.
1760 //
1761 Volatile = TRUE;
1762
1763 if ((UINT32) (VarSize + mVariableModuleGlobal->VolatileLastVariableOffset) >
1764 ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size) {
1765 //
1766 // Perform garbage collection & reclaim operation.
1767 //
1768 Status = Reclaim (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase,
1769 &mVariableModuleGlobal->VolatileLastVariableOffset, TRUE, Variable, FALSE);
1770 if (EFI_ERROR (Status)) {
1771 goto Done;
1772 }
1773 //
1774 // If still no enough space, return out of resources.
1775 //
1776 if ((UINT32) (VarSize + mVariableModuleGlobal->VolatileLastVariableOffset) >
1777 ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size
1778 ) {
1779 Status = EFI_OUT_OF_RESOURCES;
1780 goto Done;
1781 }
1782 if (Variable->CurrPtr != NULL) {
1783 CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN) CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr));
1784 CacheVariable->InDeletedTransitionPtr = NULL;
1785 }
1786 }
1787
1788 NextVariable->State = VAR_ADDED;
1789 Status = UpdateVariableStore (
1790 &mVariableModuleGlobal->VariableGlobal,
1791 TRUE,
1792 TRUE,
1793 Fvb,
1794 mVariableModuleGlobal->VolatileLastVariableOffset,
1795 (UINT32) VarSize,
1796 (UINT8 *) NextVariable
1797 );
1798
1799 if (EFI_ERROR (Status)) {
1800 goto Done;
1801 }
1802
1803 mVariableModuleGlobal->VolatileLastVariableOffset += HEADER_ALIGN (VarSize);
1804 }
1805
1806 //
1807 // Mark the old variable as deleted.
1808 //
1809 if (!EFI_ERROR (Status) && Variable->CurrPtr != NULL) {
1810 if (Variable->InDeletedTransitionPtr != NULL) {
1811 //
1812 // Both ADDED and IN_DELETED_TRANSITION old variable are present,
1813 // set IN_DELETED_TRANSITION one to DELETED state first.
1814 //
1815 State = Variable->InDeletedTransitionPtr->State;
1816 State &= VAR_DELETED;
1817 Status = UpdateVariableStore (
1818 &mVariableModuleGlobal->VariableGlobal,
1819 Variable->Volatile,
1820 FALSE,
1821 Fvb,
1822 (UINTN) &Variable->InDeletedTransitionPtr->State,
1823 sizeof (UINT8),
1824 &State
1825 );
1826 if (!EFI_ERROR (Status)) {
1827 if (!Variable->Volatile) {
1828 ASSERT (CacheVariable->InDeletedTransitionPtr != NULL);
1829 CacheVariable->InDeletedTransitionPtr->State = State;
1830 }
1831 } else {
1832 goto Done;
1833 }
1834 }
1835
1836 State = Variable->CurrPtr->State;
1837 State &= VAR_DELETED;
1838
1839 Status = UpdateVariableStore (
1840 &mVariableModuleGlobal->VariableGlobal,
1841 Variable->Volatile,
1842 FALSE,
1843 Fvb,
1844 (UINTN) &Variable->CurrPtr->State,
1845 sizeof (UINT8),
1846 &State
1847 );
1848 if (!EFI_ERROR (Status) && !Variable->Volatile) {
1849 CacheVariable->CurrPtr->State = State;
1850 }
1851 }
1852
1853 if (!EFI_ERROR (Status)) {
1854 UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);
1855 if (!Volatile) {
1856 FlushHobVariableToFlash (VariableName, VendorGuid);
1857 }
1858 }
1859
1860 Done:
1861 return Status;
1862 }
1863
1864 /**
1865 Check if a Unicode character is a hexadecimal character.
1866
1867 This function checks if a Unicode character is a
1868 hexadecimal character. The valid hexadecimal character is
1869 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
1870
1871
1872 @param Char The character to check against.
1873
1874 @retval TRUE If the Char is a hexadecmial character.
1875 @retval FALSE If the Char is not a hexadecmial character.
1876
1877 **/
1878 BOOLEAN
1879 EFIAPI
1880 IsHexaDecimalDigitCharacter (
1881 IN CHAR16 Char
1882 )
1883 {
1884 return (BOOLEAN) ((Char >= L'0' && Char <= L'9') || (Char >= L'A' && Char <= L'F') || (Char >= L'a' && Char <= L'f'));
1885 }
1886
1887 /**
1888
1889 This code checks if variable is hardware error record variable or not.
1890
1891 According to UEFI spec, hardware error record variable should use the EFI_HARDWARE_ERROR_VARIABLE VendorGuid
1892 and have the L"HwErrRec####" name convention, #### is a printed hex value and no 0x or h is included in the hex value.
1893
1894 @param VariableName Pointer to variable name.
1895 @param VendorGuid Variable Vendor Guid.
1896
1897 @retval TRUE Variable is hardware error record variable.
1898 @retval FALSE Variable is not hardware error record variable.
1899
1900 **/
1901 BOOLEAN
1902 EFIAPI
1903 IsHwErrRecVariable (
1904 IN CHAR16 *VariableName,
1905 IN EFI_GUID *VendorGuid
1906 )
1907 {
1908 if (!CompareGuid (VendorGuid, &gEfiHardwareErrorVariableGuid) ||
1909 (StrLen (VariableName) != StrLen (L"HwErrRec####")) ||
1910 (StrnCmp(VariableName, L"HwErrRec", StrLen (L"HwErrRec")) != 0) ||
1911 !IsHexaDecimalDigitCharacter (VariableName[0x8]) ||
1912 !IsHexaDecimalDigitCharacter (VariableName[0x9]) ||
1913 !IsHexaDecimalDigitCharacter (VariableName[0xA]) ||
1914 !IsHexaDecimalDigitCharacter (VariableName[0xB])) {
1915 return FALSE;
1916 }
1917
1918 return TRUE;
1919 }
1920
1921 /**
1922
1923 This code finds variable in storage blocks (Volatile or Non-Volatile).
1924
1925 @param VariableName Name of Variable to be found.
1926 @param VendorGuid Variable vendor GUID.
1927 @param Attributes Attribute value of the variable found.
1928 @param DataSize Size of Data found. If size is less than the
1929 data, this value contains the required size.
1930 @param Data Data pointer.
1931
1932 @return EFI_INVALID_PARAMETER Invalid parameter.
1933 @return EFI_SUCCESS Find the specified variable.
1934 @return EFI_NOT_FOUND Not found.
1935 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
1936
1937 **/
1938 EFI_STATUS
1939 EFIAPI
1940 VariableServiceGetVariable (
1941 IN CHAR16 *VariableName,
1942 IN EFI_GUID *VendorGuid,
1943 OUT UINT32 *Attributes OPTIONAL,
1944 IN OUT UINTN *DataSize,
1945 OUT VOID *Data
1946 )
1947 {
1948 EFI_STATUS Status;
1949 VARIABLE_POINTER_TRACK Variable;
1950 UINTN VarDataSize;
1951
1952 if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {
1953 return EFI_INVALID_PARAMETER;
1954 }
1955
1956 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
1957
1958 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);
1959 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {
1960 goto Done;
1961 }
1962
1963 //
1964 // Get data size
1965 //
1966 VarDataSize = DataSizeOfVariable (Variable.CurrPtr);
1967 ASSERT (VarDataSize != 0);
1968
1969 if (*DataSize >= VarDataSize) {
1970 if (Data == NULL) {
1971 Status = EFI_INVALID_PARAMETER;
1972 goto Done;
1973 }
1974
1975 CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);
1976 if (Attributes != NULL) {
1977 *Attributes = Variable.CurrPtr->Attributes;
1978 }
1979
1980 *DataSize = VarDataSize;
1981 UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, TRUE, FALSE, FALSE, FALSE);
1982
1983 Status = EFI_SUCCESS;
1984 goto Done;
1985 } else {
1986 *DataSize = VarDataSize;
1987 Status = EFI_BUFFER_TOO_SMALL;
1988 goto Done;
1989 }
1990
1991 Done:
1992 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
1993 return Status;
1994 }
1995
1996
1997
1998 /**
1999
2000 This code Finds the Next available variable.
2001
2002 @param VariableNameSize Size of the variable name.
2003 @param VariableName Pointer to variable name.
2004 @param VendorGuid Variable Vendor Guid.
2005
2006 @return EFI_INVALID_PARAMETER Invalid parameter.
2007 @return EFI_SUCCESS Find the specified variable.
2008 @return EFI_NOT_FOUND Not found.
2009 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
2010
2011 **/
2012 EFI_STATUS
2013 EFIAPI
2014 VariableServiceGetNextVariableName (
2015 IN OUT UINTN *VariableNameSize,
2016 IN OUT CHAR16 *VariableName,
2017 IN OUT EFI_GUID *VendorGuid
2018 )
2019 {
2020 VARIABLE_STORE_TYPE Type;
2021 VARIABLE_POINTER_TRACK Variable;
2022 VARIABLE_POINTER_TRACK VariableInHob;
2023 VARIABLE_POINTER_TRACK VariablePtrTrack;
2024 UINTN VarNameSize;
2025 EFI_STATUS Status;
2026 VARIABLE_STORE_HEADER *VariableStoreHeader[VariableStoreTypeMax];
2027
2028 if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {
2029 return EFI_INVALID_PARAMETER;
2030 }
2031
2032 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
2033
2034 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);
2035 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {
2036 goto Done;
2037 }
2038
2039 if (VariableName[0] != 0) {
2040 //
2041 // If variable name is not NULL, get next variable.
2042 //
2043 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
2044 }
2045
2046 //
2047 // 0: Volatile, 1: HOB, 2: Non-Volatile.
2048 // The index and attributes mapping must be kept in this order as FindVariable
2049 // makes use of this mapping to implement search algorithm.
2050 //
2051 VariableStoreHeader[VariableStoreTypeVolatile] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;
2052 VariableStoreHeader[VariableStoreTypeHob] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;
2053 VariableStoreHeader[VariableStoreTypeNv] = mNvVariableCache;
2054
2055 while (TRUE) {
2056 //
2057 // Switch from Volatile to HOB, to Non-Volatile.
2058 //
2059 while ((Variable.CurrPtr >= Variable.EndPtr) ||
2060 (Variable.CurrPtr == NULL) ||
2061 !IsValidVariableHeader (Variable.CurrPtr)
2062 ) {
2063 //
2064 // Find current storage index
2065 //
2066 for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {
2067 if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr == GetStartPointer (VariableStoreHeader[Type]))) {
2068 break;
2069 }
2070 }
2071 ASSERT (Type < VariableStoreTypeMax);
2072 //
2073 // Switch to next storage
2074 //
2075 for (Type++; Type < VariableStoreTypeMax; Type++) {
2076 if (VariableStoreHeader[Type] != NULL) {
2077 break;
2078 }
2079 }
2080 //
2081 // Capture the case that
2082 // 1. current storage is the last one, or
2083 // 2. no further storage
2084 //
2085 if (Type == VariableStoreTypeMax) {
2086 Status = EFI_NOT_FOUND;
2087 goto Done;
2088 }
2089 Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);
2090 Variable.EndPtr = GetEndPointer (VariableStoreHeader[Type]);
2091 Variable.CurrPtr = Variable.StartPtr;
2092 }
2093
2094 //
2095 // Variable is found
2096 //
2097 if (Variable.CurrPtr->State == VAR_ADDED || Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
2098 if (!AtRuntime () || ((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {
2099 if (Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
2100 //
2101 // If it is a IN_DELETED_TRANSITION variable,
2102 // and there is also a same ADDED one at the same time,
2103 // don't return it.
2104 //
2105 VariablePtrTrack.StartPtr = Variable.StartPtr;
2106 VariablePtrTrack.EndPtr = Variable.EndPtr;
2107 Status = FindVariableEx (
2108 GetVariableNamePtr (Variable.CurrPtr),
2109 &Variable.CurrPtr->VendorGuid,
2110 FALSE,
2111 &VariablePtrTrack
2112 );
2113 if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State == VAR_ADDED) {
2114 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
2115 continue;
2116 }
2117 }
2118
2119 //
2120 // Don't return NV variable when HOB overrides it
2121 //
2122 if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) && (VariableStoreHeader[VariableStoreTypeNv] != NULL) &&
2123 (Variable.StartPtr == GetStartPointer (VariableStoreHeader[VariableStoreTypeNv]))
2124 ) {
2125 VariableInHob.StartPtr = GetStartPointer (VariableStoreHeader[VariableStoreTypeHob]);
2126 VariableInHob.EndPtr = GetEndPointer (VariableStoreHeader[VariableStoreTypeHob]);
2127 Status = FindVariableEx (
2128 GetVariableNamePtr (Variable.CurrPtr),
2129 &Variable.CurrPtr->VendorGuid,
2130 FALSE,
2131 &VariableInHob
2132 );
2133 if (!EFI_ERROR (Status)) {
2134 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
2135 continue;
2136 }
2137 }
2138
2139 VarNameSize = NameSizeOfVariable (Variable.CurrPtr);
2140 ASSERT (VarNameSize != 0);
2141
2142 if (VarNameSize <= *VariableNameSize) {
2143 CopyMem (VariableName, GetVariableNamePtr (Variable.CurrPtr), VarNameSize);
2144 CopyMem (VendorGuid, &Variable.CurrPtr->VendorGuid, sizeof (EFI_GUID));
2145 Status = EFI_SUCCESS;
2146 } else {
2147 Status = EFI_BUFFER_TOO_SMALL;
2148 }
2149
2150 *VariableNameSize = VarNameSize;
2151 goto Done;
2152 }
2153 }
2154
2155 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
2156 }
2157
2158 Done:
2159 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
2160 return Status;
2161 }
2162
2163 /**
2164
2165 This code sets variable in storage blocks (Volatile or Non-Volatile).
2166
2167 @param VariableName Name of Variable to be found.
2168 @param VendorGuid Variable vendor GUID.
2169 @param Attributes Attribute value of the variable found
2170 @param DataSize Size of Data found. If size is less than the
2171 data, this value contains the required size.
2172 @param Data Data pointer.
2173
2174 @return EFI_INVALID_PARAMETER Invalid parameter.
2175 @return EFI_SUCCESS Set successfully.
2176 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.
2177 @return EFI_NOT_FOUND Not found.
2178 @return EFI_WRITE_PROTECTED Variable is read-only.
2179
2180 **/
2181 EFI_STATUS
2182 EFIAPI
2183 VariableServiceSetVariable (
2184 IN CHAR16 *VariableName,
2185 IN EFI_GUID *VendorGuid,
2186 IN UINT32 Attributes,
2187 IN UINTN DataSize,
2188 IN VOID *Data
2189 )
2190 {
2191 VARIABLE_POINTER_TRACK Variable;
2192 EFI_STATUS Status;
2193 VARIABLE_HEADER *NextVariable;
2194 EFI_PHYSICAL_ADDRESS Point;
2195
2196 //
2197 // Check input parameters.
2198 //
2199 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {
2200 return EFI_INVALID_PARAMETER;
2201 }
2202
2203 if (DataSize != 0 && Data == NULL) {
2204 return EFI_INVALID_PARAMETER;
2205 }
2206
2207 //
2208 // Not support authenticated variable write yet.
2209 //
2210 if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {
2211 return EFI_INVALID_PARAMETER;
2212 }
2213
2214 //
2215 // Make sure if runtime bit is set, boot service bit is set also.
2216 //
2217 if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {
2218 return EFI_INVALID_PARAMETER;
2219 }
2220
2221 if ((UINTN)(~0) - DataSize < StrSize(VariableName)){
2222 //
2223 // Prevent whole variable size overflow
2224 //
2225 return EFI_INVALID_PARAMETER;
2226 }
2227
2228 //
2229 // The size of the VariableName, including the Unicode Null in bytes plus
2230 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)
2231 // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.
2232 //
2233 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
2234 if ( StrSize (VariableName) + DataSize > PcdGet32 (PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER)) {
2235 return EFI_INVALID_PARAMETER;
2236 }
2237 if (!IsHwErrRecVariable(VariableName, VendorGuid)) {
2238 return EFI_INVALID_PARAMETER;
2239 }
2240 } else {
2241 //
2242 // The size of the VariableName, including the Unicode Null in bytes plus
2243 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxVariableSize) bytes.
2244 //
2245 if (StrSize (VariableName) + DataSize > PcdGet32 (PcdMaxVariableSize) - sizeof (VARIABLE_HEADER)) {
2246 return EFI_INVALID_PARAMETER;
2247 }
2248 }
2249
2250 if (AtRuntime ()) {
2251 //
2252 // HwErrRecSupport Global Variable identifies the level of hardware error record persistence
2253 // support implemented by the platform. This variable is only modified by firmware and is read-only to the OS.
2254 //
2255 if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, L"HwErrRecSupport") == 0)) {
2256 return EFI_WRITE_PROTECTED;
2257 }
2258 }
2259
2260 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
2261
2262 //
2263 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.
2264 //
2265 if (1 < InterlockedIncrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState)) {
2266 Point = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;
2267 //
2268 // Parse non-volatile variable data and get last variable offset.
2269 //
2270 NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point);
2271 while ((NextVariable < GetEndPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point))
2272 && IsValidVariableHeader (NextVariable)) {
2273 NextVariable = GetNextVariablePtr (NextVariable);
2274 }
2275 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) Point;
2276 }
2277
2278 //
2279 // Check whether the input variable is already existed.
2280 //
2281 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, TRUE);
2282 if (!EFI_ERROR (Status)) {
2283 if (((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) && AtRuntime ()) {
2284 return EFI_WRITE_PROTECTED;
2285 }
2286 }
2287
2288 //
2289 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.
2290 //
2291 AutoUpdateLangVariable (VariableName, Data, DataSize);
2292
2293 Status = UpdateVariable (VariableName, VendorGuid, Data, DataSize, Attributes, &Variable);
2294
2295 InterlockedDecrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState);
2296 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
2297
2298 return Status;
2299 }
2300
2301 /**
2302
2303 This code returns information about the EFI variables.
2304
2305 @param Attributes Attributes bitmask to specify the type of variables
2306 on which to return information.
2307 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
2308 for the EFI variables associated with the attributes specified.
2309 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
2310 for EFI variables associated with the attributes specified.
2311 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
2312 associated with the attributes specified.
2313
2314 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
2315 @return EFI_SUCCESS Query successfully.
2316 @return EFI_UNSUPPORTED The attribute is not supported on this platform.
2317
2318 **/
2319 EFI_STATUS
2320 EFIAPI
2321 VariableServiceQueryVariableInfo (
2322 IN UINT32 Attributes,
2323 OUT UINT64 *MaximumVariableStorageSize,
2324 OUT UINT64 *RemainingVariableStorageSize,
2325 OUT UINT64 *MaximumVariableSize
2326 )
2327 {
2328 VARIABLE_HEADER *Variable;
2329 VARIABLE_HEADER *NextVariable;
2330 UINT64 VariableSize;
2331 VARIABLE_STORE_HEADER *VariableStoreHeader;
2332 UINT64 CommonVariableTotalSize;
2333 UINT64 HwErrVariableTotalSize;
2334
2335 CommonVariableTotalSize = 0;
2336 HwErrVariableTotalSize = 0;
2337
2338 if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {
2339 return EFI_INVALID_PARAMETER;
2340 }
2341
2342 if((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == 0) {
2343 //
2344 // Make sure the Attributes combination is supported by the platform.
2345 //
2346 return EFI_UNSUPPORTED;
2347 } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {
2348 //
2349 // Make sure if runtime bit is set, boot service bit is set also.
2350 //
2351 return EFI_INVALID_PARAMETER;
2352 } else if (AtRuntime () && ((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0)) {
2353 //
2354 // Make sure RT Attribute is set if we are in Runtime phase.
2355 //
2356 return EFI_INVALID_PARAMETER;
2357 } else if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
2358 //
2359 // Make sure Hw Attribute is set with NV.
2360 //
2361 return EFI_INVALID_PARAMETER;
2362 } else if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {
2363 //
2364 // Not support authentiated variable write yet.
2365 //
2366 return EFI_UNSUPPORTED;
2367 }
2368
2369 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
2370
2371 if((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {
2372 //
2373 // Query is Volatile related.
2374 //
2375 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);
2376 } else {
2377 //
2378 // Query is Non-Volatile related.
2379 //
2380 VariableStoreHeader = mNvVariableCache;
2381 }
2382
2383 //
2384 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
2385 // with the storage size (excluding the storage header size).
2386 //
2387 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);
2388
2389 //
2390 // Harware error record variable needs larger size.
2391 //
2392 if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {
2393 *MaximumVariableStorageSize = PcdGet32 (PcdHwErrStorageSize);
2394 *MaximumVariableSize = PcdGet32 (PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER);
2395 } else {
2396 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {
2397 ASSERT (PcdGet32 (PcdHwErrStorageSize) < VariableStoreHeader->Size);
2398 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize);
2399 }
2400
2401 //
2402 // Let *MaximumVariableSize be PcdGet32 (PcdMaxVariableSize) with the exception of the variable header size.
2403 //
2404 *MaximumVariableSize = PcdGet32 (PcdMaxVariableSize) - sizeof (VARIABLE_HEADER);
2405 }
2406
2407 //
2408 // Point to the starting address of the variables.
2409 //
2410 Variable = GetStartPointer (VariableStoreHeader);
2411
2412 //
2413 // Now walk through the related variable store.
2414 //
2415 while ((Variable < GetEndPointer (VariableStoreHeader)) && IsValidVariableHeader (Variable)) {
2416 NextVariable = GetNextVariablePtr (Variable);
2417 VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;
2418
2419 if (AtRuntime ()) {
2420 //
2421 // We don't take the state of the variables in mind
2422 // when calculating RemainingVariableStorageSize,
2423 // since the space occupied by variables not marked with
2424 // VAR_ADDED is not allowed to be reclaimed in Runtime.
2425 //
2426 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
2427 HwErrVariableTotalSize += VariableSize;
2428 } else {
2429 CommonVariableTotalSize += VariableSize;
2430 }
2431 } else {
2432 //
2433 // Only care about Variables with State VAR_ADDED, because
2434 // the space not marked as VAR_ADDED is reclaimable now.
2435 //
2436 if (Variable->State == VAR_ADDED) {
2437 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
2438 HwErrVariableTotalSize += VariableSize;
2439 } else {
2440 CommonVariableTotalSize += VariableSize;
2441 }
2442 }
2443 }
2444
2445 //
2446 // Go to the next one.
2447 //
2448 Variable = NextVariable;
2449 }
2450
2451 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD){
2452 *RemainingVariableStorageSize = *MaximumVariableStorageSize - HwErrVariableTotalSize;
2453 }else {
2454 *RemainingVariableStorageSize = *MaximumVariableStorageSize - CommonVariableTotalSize;
2455 }
2456
2457 if (*RemainingVariableStorageSize < sizeof (VARIABLE_HEADER)) {
2458 *MaximumVariableSize = 0;
2459 } else if ((*RemainingVariableStorageSize - sizeof (VARIABLE_HEADER)) < *MaximumVariableSize) {
2460 *MaximumVariableSize = *RemainingVariableStorageSize - sizeof (VARIABLE_HEADER);
2461 }
2462
2463 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
2464 return EFI_SUCCESS;
2465 }
2466
2467
2468 /**
2469 This function reclaims variable storage if free size is below the threshold.
2470
2471 **/
2472 VOID
2473 ReclaimForOS(
2474 VOID
2475 )
2476 {
2477 EFI_STATUS Status;
2478 UINTN CommonVariableSpace;
2479 UINTN RemainingCommonVariableSpace;
2480 UINTN RemainingHwErrVariableSpace;
2481
2482 Status = EFI_SUCCESS;
2483
2484 CommonVariableSpace = ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase)))->Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32(PcdHwErrStorageSize); //Allowable max size of common variable storage space
2485
2486 RemainingCommonVariableSpace = CommonVariableSpace - mVariableModuleGlobal->CommonVariableTotalSize;
2487
2488 RemainingHwErrVariableSpace = PcdGet32 (PcdHwErrStorageSize) - mVariableModuleGlobal->HwErrVariableTotalSize;
2489 //
2490 // Check if the free area is blow a threshold.
2491 //
2492 if ((RemainingCommonVariableSpace < PcdGet32 (PcdMaxVariableSize))
2493 || ((PcdGet32 (PcdHwErrStorageSize) != 0) &&
2494 (RemainingHwErrVariableSpace < PcdGet32 (PcdMaxHardwareErrorVariableSize)))){
2495 Status = Reclaim (
2496 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,
2497 &mVariableModuleGlobal->NonVolatileLastVariableOffset,
2498 FALSE,
2499 NULL,
2500 FALSE
2501 );
2502 ASSERT_EFI_ERROR (Status);
2503 }
2504 }
2505
2506 /**
2507 Flush the HOB variable to flash.
2508
2509 @param[in] VariableName Name of variable has been updated or deleted.
2510 @param[in] VendorGuid Guid of variable has been updated or deleted.
2511
2512 **/
2513 VOID
2514 FlushHobVariableToFlash (
2515 IN CHAR16 *VariableName,
2516 IN EFI_GUID *VendorGuid
2517 )
2518 {
2519 EFI_STATUS Status;
2520 VARIABLE_STORE_HEADER *VariableStoreHeader;
2521 VARIABLE_HEADER *Variable;
2522 VOID *VariableData;
2523 BOOLEAN ErrorFlag;
2524
2525 ErrorFlag = FALSE;
2526
2527 //
2528 // Flush the HOB variable to flash.
2529 //
2530 if (mVariableModuleGlobal->VariableGlobal.HobVariableBase != 0) {
2531 VariableStoreHeader = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;
2532 //
2533 // Set HobVariableBase to 0, it can avoid SetVariable to call back.
2534 //
2535 mVariableModuleGlobal->VariableGlobal.HobVariableBase = 0;
2536 for ( Variable = GetStartPointer (VariableStoreHeader)
2537 ; (Variable < GetEndPointer (VariableStoreHeader) && IsValidVariableHeader (Variable))
2538 ; Variable = GetNextVariablePtr (Variable)
2539 ) {
2540 if (Variable->State != VAR_ADDED) {
2541 //
2542 // The HOB variable has been set to DELETED state in local.
2543 //
2544 continue;
2545 }
2546 ASSERT ((Variable->Attributes & EFI_VARIABLE_NON_VOLATILE) != 0);
2547 if (VendorGuid == NULL || VariableName == NULL ||
2548 !CompareGuid (VendorGuid, &Variable->VendorGuid) ||
2549 StrCmp (VariableName, GetVariableNamePtr (Variable)) != 0) {
2550 VariableData = GetVariableDataPtr (Variable);
2551 Status = VariableServiceSetVariable (
2552 GetVariableNamePtr (Variable),
2553 &Variable->VendorGuid,
2554 Variable->Attributes,
2555 Variable->DataSize,
2556 VariableData
2557 );
2558 DEBUG ((EFI_D_INFO, "Variable driver flush the HOB variable to flash: %g %s %r\n", &Variable->VendorGuid, GetVariableNamePtr (Variable), Status));
2559 } else {
2560 //
2561 // The updated or deleted variable is matched with the HOB variable.
2562 // Don't break here because we will try to set other HOB variables
2563 // since this variable could be set successfully.
2564 //
2565 Status = EFI_SUCCESS;
2566 }
2567 if (!EFI_ERROR (Status)) {
2568 //
2569 // If set variable successful, or the updated or deleted variable is matched with the HOB variable,
2570 // set the HOB variable to DELETED state in local.
2571 //
2572 DEBUG ((EFI_D_INFO, "Variable driver set the HOB variable to DELETED state in local: %g %s\n", &Variable->VendorGuid, GetVariableNamePtr (Variable)));
2573 Variable->State &= VAR_DELETED;
2574 } else {
2575 ErrorFlag = TRUE;
2576 }
2577 }
2578 if (ErrorFlag) {
2579 //
2580 // We still have HOB variable(s) not flushed in flash.
2581 //
2582 mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VariableStoreHeader;
2583 } else {
2584 //
2585 // All HOB variables have been flushed in flash.
2586 //
2587 DEBUG ((EFI_D_INFO, "Variable driver: all HOB variables have been flushed in flash.\n"));
2588 if (!AtRuntime ()) {
2589 FreePool ((VOID *) VariableStoreHeader);
2590 }
2591 }
2592 }
2593
2594 }
2595
2596 /**
2597 Initializes variable write service after FVB was ready.
2598
2599 @retval EFI_SUCCESS Function successfully executed.
2600 @retval Others Fail to initialize the variable service.
2601
2602 **/
2603 EFI_STATUS
2604 VariableWriteServiceInitialize (
2605 VOID
2606 )
2607 {
2608 EFI_STATUS Status;
2609 VARIABLE_STORE_HEADER *VariableStoreHeader;
2610 UINTN Index;
2611 UINT8 Data;
2612 EFI_PHYSICAL_ADDRESS VariableStoreBase;
2613
2614 VariableStoreBase = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;
2615 VariableStoreHeader = (VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase;
2616
2617 //
2618 // Check if the free area is really free.
2619 //
2620 for (Index = mVariableModuleGlobal->NonVolatileLastVariableOffset; Index < VariableStoreHeader->Size; Index++) {
2621 Data = ((UINT8 *) mNvVariableCache)[Index];
2622 if (Data != 0xff) {
2623 //
2624 // There must be something wrong in variable store, do reclaim operation.
2625 //
2626 Status = Reclaim (
2627 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,
2628 &mVariableModuleGlobal->NonVolatileLastVariableOffset,
2629 FALSE,
2630 NULL,
2631 TRUE
2632 );
2633 if (EFI_ERROR (Status)) {
2634 return Status;
2635 }
2636 break;
2637 }
2638 }
2639
2640 FlushHobVariableToFlash (NULL, NULL);
2641
2642 return EFI_SUCCESS;
2643 }
2644
2645
2646 /**
2647 Initializes variable store area for non-volatile and volatile variable.
2648
2649 @retval EFI_SUCCESS Function successfully executed.
2650 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
2651
2652 **/
2653 EFI_STATUS
2654 VariableCommonInitialize (
2655 VOID
2656 )
2657 {
2658 EFI_STATUS Status;
2659 VARIABLE_STORE_HEADER *VolatileVariableStore;
2660 VARIABLE_STORE_HEADER *VariableStoreHeader;
2661 VARIABLE_HEADER *NextVariable;
2662 EFI_PHYSICAL_ADDRESS TempVariableStoreHeader;
2663 EFI_PHYSICAL_ADDRESS VariableStoreBase;
2664 UINT64 VariableStoreLength;
2665 UINTN ScratchSize;
2666 UINTN VariableSize;
2667 EFI_HOB_GUID_TYPE *GuidHob;
2668
2669 //
2670 // Allocate runtime memory for variable driver global structure.
2671 //
2672 mVariableModuleGlobal = AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL));
2673 if (mVariableModuleGlobal == NULL) {
2674 return EFI_OUT_OF_RESOURCES;
2675 }
2676
2677 InitializeLock (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock, TPL_NOTIFY);
2678
2679 //
2680 // Note that in EdkII variable driver implementation, Hardware Error Record type variable
2681 // is stored with common variable in the same NV region. So the platform integrator should
2682 // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of
2683 // PcdFlashNvStorageVariableSize.
2684 //
2685 ASSERT (PcdGet32 (PcdHwErrStorageSize) <= PcdGet32 (PcdFlashNvStorageVariableSize));
2686
2687 //
2688 // Get HOB variable store.
2689 //
2690 GuidHob = GetFirstGuidHob (&gEfiVariableGuid);
2691 if (GuidHob != NULL) {
2692 VariableStoreHeader = GET_GUID_HOB_DATA (GuidHob);
2693 VariableStoreLength = (UINT64) (GuidHob->Header.HobLength - sizeof (EFI_HOB_GUID_TYPE));
2694 if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) {
2695 mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) AllocateRuntimeCopyPool ((UINTN) VariableStoreLength, (VOID *) VariableStoreHeader);
2696 if (mVariableModuleGlobal->VariableGlobal.HobVariableBase == 0) {
2697 return EFI_OUT_OF_RESOURCES;
2698 }
2699 } else {
2700 DEBUG ((EFI_D_ERROR, "HOB Variable Store header is corrupted!\n"));
2701 }
2702 }
2703
2704 //
2705 // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.
2706 //
2707 ScratchSize = MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize));
2708 VolatileVariableStore = AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize) + ScratchSize);
2709 if (VolatileVariableStore == NULL) {
2710 FreePool (mVariableModuleGlobal);
2711 return EFI_OUT_OF_RESOURCES;
2712 }
2713
2714 SetMem (VolatileVariableStore, PcdGet32 (PcdVariableStoreSize) + ScratchSize, 0xff);
2715
2716 //
2717 // Initialize Variable Specific Data.
2718 //
2719 mVariableModuleGlobal->VariableGlobal.VolatileVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VolatileVariableStore;
2720 mVariableModuleGlobal->VolatileLastVariableOffset = (UINTN) GetStartPointer (VolatileVariableStore) - (UINTN) VolatileVariableStore;
2721 mVariableModuleGlobal->FvbInstance = NULL;
2722
2723 CopyGuid (&VolatileVariableStore->Signature, &gEfiVariableGuid);
2724 VolatileVariableStore->Size = PcdGet32 (PcdVariableStoreSize);
2725 VolatileVariableStore->Format = VARIABLE_STORE_FORMATTED;
2726 VolatileVariableStore->State = VARIABLE_STORE_HEALTHY;
2727 VolatileVariableStore->Reserved = 0;
2728 VolatileVariableStore->Reserved1 = 0;
2729
2730 //
2731 // Get non-volatile variable store.
2732 //
2733
2734 TempVariableStoreHeader = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);
2735 if (TempVariableStoreHeader == 0) {
2736 TempVariableStoreHeader = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);
2737 }
2738
2739 //
2740 // Check if the Firmware Volume is not corrupted
2741 //
2742 if ((((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(TempVariableStoreHeader))->Signature != EFI_FVH_SIGNATURE) ||
2743 (!CompareGuid (&gEfiSystemNvDataFvGuid, &((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(TempVariableStoreHeader))->FileSystemGuid))) {
2744 Status = EFI_VOLUME_CORRUPTED;
2745 DEBUG ((EFI_D_ERROR, "Firmware Volume for Variable Store is corrupted\n"));
2746 goto Done;
2747 }
2748
2749 VariableStoreBase = TempVariableStoreHeader + \
2750 (((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(TempVariableStoreHeader)) -> HeaderLength);
2751 VariableStoreLength = (UINT64) PcdGet32 (PcdFlashNvStorageVariableSize) - \
2752 (((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(TempVariableStoreHeader)) -> HeaderLength);
2753
2754 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase;
2755 VariableStoreHeader = (VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase;
2756 if (GetVariableStoreStatus (VariableStoreHeader) != EfiValid) {
2757 Status = EFI_VOLUME_CORRUPTED;
2758 DEBUG((EFI_D_INFO, "Variable Store header is corrupted\n"));
2759 goto Done;
2760 }
2761 ASSERT(VariableStoreHeader->Size == VariableStoreLength);
2762
2763 //
2764 // The max variable or hardware error variable size should be < variable store size.
2765 //
2766 ASSERT(MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize)) < VariableStoreLength);
2767
2768 //
2769 // Parse non-volatile variable data and get last variable offset.
2770 //
2771 NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase);
2772 while (IsValidVariableHeader (NextVariable)) {
2773 VariableSize = NextVariable->NameSize + NextVariable->DataSize + sizeof (VARIABLE_HEADER);
2774 if ((NextVariable->Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {
2775 mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VariableSize);
2776 } else {
2777 mVariableModuleGlobal->CommonVariableTotalSize += HEADER_ALIGN (VariableSize);
2778 }
2779
2780 NextVariable = GetNextVariablePtr (NextVariable);
2781 }
2782
2783 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) VariableStoreBase;
2784
2785 //
2786 // Allocate runtime memory used for a memory copy of the FLASH region.
2787 // Keep the memory and the FLASH in sync as updates occur
2788 //
2789 mNvVariableCache = AllocateRuntimeZeroPool ((UINTN)VariableStoreLength);
2790 if (mNvVariableCache == NULL) {
2791 Status = EFI_OUT_OF_RESOURCES;
2792 goto Done;
2793 }
2794 CopyMem (mNvVariableCache, (CHAR8 *)(UINTN)VariableStoreBase, (UINTN)VariableStoreLength);
2795 Status = EFI_SUCCESS;
2796
2797 Done:
2798 if (EFI_ERROR (Status)) {
2799 FreePool (mVariableModuleGlobal);
2800 FreePool (VolatileVariableStore);
2801 }
2802
2803 return Status;
2804 }
2805
2806
2807 /**
2808 Get the proper fvb handle and/or fvb protocol by the given Flash address.
2809
2810 @param[in] Address The Flash address.
2811 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.
2812 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.
2813
2814 **/
2815 EFI_STATUS
2816 GetFvbInfoByAddress (
2817 IN EFI_PHYSICAL_ADDRESS Address,
2818 OUT EFI_HANDLE *FvbHandle OPTIONAL,
2819 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvbProtocol OPTIONAL
2820 )
2821 {
2822 EFI_STATUS Status;
2823 EFI_HANDLE *HandleBuffer;
2824 UINTN HandleCount;
2825 UINTN Index;
2826 EFI_PHYSICAL_ADDRESS FvbBaseAddress;
2827 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;
2828 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
2829 EFI_FVB_ATTRIBUTES_2 Attributes;
2830
2831 //
2832 // Get all FVB handles.
2833 //
2834 Status = GetFvbCountAndBuffer (&HandleCount, &HandleBuffer);
2835 if (EFI_ERROR (Status)) {
2836 return EFI_NOT_FOUND;
2837 }
2838
2839 //
2840 // Get the FVB to access variable store.
2841 //
2842 Fvb = NULL;
2843 for (Index = 0; Index < HandleCount; Index += 1, Status = EFI_NOT_FOUND, Fvb = NULL) {
2844 Status = GetFvbByHandle (HandleBuffer[Index], &Fvb);
2845 if (EFI_ERROR (Status)) {
2846 Status = EFI_NOT_FOUND;
2847 break;
2848 }
2849
2850 //
2851 // Ensure this FVB protocol supported Write operation.
2852 //
2853 Status = Fvb->GetAttributes (Fvb, &Attributes);
2854 if (EFI_ERROR (Status) || ((Attributes & EFI_FVB2_WRITE_STATUS) == 0)) {
2855 continue;
2856 }
2857
2858 //
2859 // Compare the address and select the right one.
2860 //
2861 Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);
2862 if (EFI_ERROR (Status)) {
2863 continue;
2864 }
2865
2866 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvbBaseAddress);
2867 if ((Address >= FvbBaseAddress) && (Address < (FvbBaseAddress + FwVolHeader->FvLength))) {
2868 if (FvbHandle != NULL) {
2869 *FvbHandle = HandleBuffer[Index];
2870 }
2871 if (FvbProtocol != NULL) {
2872 *FvbProtocol = Fvb;
2873 }
2874 Status = EFI_SUCCESS;
2875 break;
2876 }
2877 }
2878 FreePool (HandleBuffer);
2879
2880 if (Fvb == NULL) {
2881 Status = EFI_NOT_FOUND;
2882 }
2883
2884 return Status;
2885 }
2886