]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
LastVariableOffset is wrongly set to 0 when FtwVariableSpace() fails. Recalculate...
[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 - 2012, 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 UpdatingVariable Pointer to updating variable.
508
509 @return EFI_OUT_OF_RESOURCES
510 @return EFI_SUCCESS
511 @return Others
512
513 **/
514 EFI_STATUS
515 Reclaim (
516 IN EFI_PHYSICAL_ADDRESS VariableBase,
517 OUT UINTN *LastVariableOffset,
518 IN BOOLEAN IsVolatile,
519 IN VARIABLE_HEADER *UpdatingVariable
520 )
521 {
522 VARIABLE_HEADER *Variable;
523 VARIABLE_HEADER *AddedVariable;
524 VARIABLE_HEADER *NextVariable;
525 VARIABLE_HEADER *NextAddedVariable;
526 VARIABLE_STORE_HEADER *VariableStoreHeader;
527 UINT8 *ValidBuffer;
528 UINTN MaximumBufferSize;
529 UINTN VariableSize;
530 UINTN VariableNameSize;
531 UINTN UpdatingVariableNameSize;
532 UINTN NameSize;
533 UINT8 *CurrPtr;
534 VOID *Point0;
535 VOID *Point1;
536 BOOLEAN FoundAdded;
537 EFI_STATUS Status;
538 CHAR16 *VariableNamePtr;
539 CHAR16 *UpdatingVariableNamePtr;
540 UINTN CommonVariableTotalSize;
541 UINTN HwErrVariableTotalSize;
542
543 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) VariableBase);
544
545 CommonVariableTotalSize = 0;
546 HwErrVariableTotalSize = 0;
547
548 //
549 // Start Pointers for the variable.
550 //
551 Variable = GetStartPointer (VariableStoreHeader);
552 MaximumBufferSize = sizeof (VARIABLE_STORE_HEADER);
553
554 while (IsValidVariableHeader (Variable)) {
555 NextVariable = GetNextVariablePtr (Variable);
556 if (Variable->State == VAR_ADDED ||
557 Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)
558 ) {
559 VariableSize = (UINTN) NextVariable - (UINTN) Variable;
560 MaximumBufferSize += VariableSize;
561 }
562
563 Variable = NextVariable;
564 }
565
566 //
567 // Reserve the 1 Bytes with Oxff to identify the
568 // end of the variable buffer.
569 //
570 MaximumBufferSize += 1;
571 ValidBuffer = AllocatePool (MaximumBufferSize);
572 if (ValidBuffer == NULL) {
573 return EFI_OUT_OF_RESOURCES;
574 }
575
576 SetMem (ValidBuffer, MaximumBufferSize, 0xff);
577
578 //
579 // Copy variable store header.
580 //
581 CopyMem (ValidBuffer, VariableStoreHeader, sizeof (VARIABLE_STORE_HEADER));
582 CurrPtr = (UINT8 *) GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer);
583
584 //
585 // Reinstall all ADDED variables as long as they are not identical to Updating Variable.
586 //
587 Variable = GetStartPointer (VariableStoreHeader);
588 while (IsValidVariableHeader (Variable)) {
589 NextVariable = GetNextVariablePtr (Variable);
590 if (Variable->State == VAR_ADDED) {
591 if (UpdatingVariable != NULL) {
592 if (UpdatingVariable == Variable) {
593 Variable = NextVariable;
594 continue;
595 }
596
597 VariableNameSize = NameSizeOfVariable(Variable);
598 UpdatingVariableNameSize = NameSizeOfVariable(UpdatingVariable);
599
600 VariableNamePtr = GetVariableNamePtr (Variable);
601 UpdatingVariableNamePtr = GetVariableNamePtr (UpdatingVariable);
602 if (CompareGuid (&Variable->VendorGuid, &UpdatingVariable->VendorGuid) &&
603 VariableNameSize == UpdatingVariableNameSize &&
604 CompareMem (VariableNamePtr, UpdatingVariableNamePtr, VariableNameSize) == 0 ) {
605 Variable = NextVariable;
606 continue;
607 }
608 }
609 VariableSize = (UINTN) NextVariable - (UINTN) Variable;
610 CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);
611 CurrPtr += VariableSize;
612 if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {
613 HwErrVariableTotalSize += VariableSize;
614 } else if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {
615 CommonVariableTotalSize += VariableSize;
616 }
617 }
618 Variable = NextVariable;
619 }
620
621 //
622 // Reinstall the variable being updated if it is not NULL.
623 //
624 if (UpdatingVariable != NULL) {
625 VariableSize = (UINTN)(GetNextVariablePtr (UpdatingVariable)) - (UINTN)UpdatingVariable;
626 CopyMem (CurrPtr, (UINT8 *) UpdatingVariable, VariableSize);
627 CurrPtr += VariableSize;
628 if ((!IsVolatile) && ((UpdatingVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {
629 HwErrVariableTotalSize += VariableSize;
630 } else if ((!IsVolatile) && ((UpdatingVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {
631 CommonVariableTotalSize += VariableSize;
632 }
633 }
634
635 //
636 // Reinstall all in delete transition variables.
637 //
638 Variable = GetStartPointer (VariableStoreHeader);
639 while (IsValidVariableHeader (Variable)) {
640 NextVariable = GetNextVariablePtr (Variable);
641 if (Variable != UpdatingVariable && Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
642
643 //
644 // Buffer has cached all ADDED variable.
645 // Per IN_DELETED variable, we have to guarantee that
646 // no ADDED one in previous buffer.
647 //
648
649 FoundAdded = FALSE;
650 AddedVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer);
651 while (IsValidVariableHeader (AddedVariable)) {
652 NextAddedVariable = GetNextVariablePtr (AddedVariable);
653 NameSize = NameSizeOfVariable (AddedVariable);
654 if (CompareGuid (&AddedVariable->VendorGuid, &Variable->VendorGuid) &&
655 NameSize == NameSizeOfVariable (Variable)
656 ) {
657 Point0 = (VOID *) GetVariableNamePtr (AddedVariable);
658 Point1 = (VOID *) GetVariableNamePtr (Variable);
659 if (CompareMem (Point0, Point1, NameSizeOfVariable (AddedVariable)) == 0) {
660 FoundAdded = TRUE;
661 break;
662 }
663 }
664 AddedVariable = NextAddedVariable;
665 }
666 if (!FoundAdded) {
667 //
668 // Promote VAR_IN_DELETED_TRANSITION to VAR_ADDED.
669 //
670 VariableSize = (UINTN) NextVariable - (UINTN) Variable;
671 CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);
672 ((VARIABLE_HEADER *) CurrPtr)->State = VAR_ADDED;
673 CurrPtr += VariableSize;
674 if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {
675 HwErrVariableTotalSize += VariableSize;
676 } else if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {
677 CommonVariableTotalSize += VariableSize;
678 }
679 }
680 }
681
682 Variable = NextVariable;
683 }
684
685 if (IsVolatile) {
686 //
687 // If volatile variable store, just copy valid buffer.
688 //
689 SetMem ((UINT8 *) (UINTN) VariableBase, VariableStoreHeader->Size, 0xff);
690 CopyMem ((UINT8 *) (UINTN) VariableBase, ValidBuffer, (UINTN) (CurrPtr - (UINT8 *) ValidBuffer));
691 Status = EFI_SUCCESS;
692 } else {
693 //
694 // If non-volatile variable store, perform FTW here.
695 //
696 Status = FtwVariableSpace (
697 VariableBase,
698 ValidBuffer,
699 (UINTN) (CurrPtr - (UINT8 *) ValidBuffer)
700 );
701 CopyMem (mNvVariableCache, (CHAR8 *)(UINTN)VariableBase, VariableStoreHeader->Size);
702 }
703 if (!EFI_ERROR (Status)) {
704 *LastVariableOffset = (UINTN) (CurrPtr - (UINT8 *) ValidBuffer);
705 if (!IsVolatile) {
706 mVariableModuleGlobal->HwErrVariableTotalSize = HwErrVariableTotalSize;
707 mVariableModuleGlobal->CommonVariableTotalSize = CommonVariableTotalSize;
708 }
709 } else {
710 NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableBase);
711 while (IsValidVariableHeader (NextVariable)) {
712 VariableSize = NextVariable->NameSize + NextVariable->DataSize + sizeof (VARIABLE_HEADER);
713 if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {
714 mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VariableSize);
715 } else if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {
716 mVariableModuleGlobal->CommonVariableTotalSize += HEADER_ALIGN (VariableSize);
717 }
718
719 NextVariable = GetNextVariablePtr (NextVariable);
720 }
721 *LastVariableOffset = (UINTN) NextVariable - (UINTN) VariableBase;
722 }
723
724 FreePool (ValidBuffer);
725
726 return Status;
727 }
728
729 /**
730 Find the variable in the specified variable store.
731
732 @param VariableName Name of the variable to be found
733 @param VendorGuid Vendor GUID to be found.
734 @param IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
735 check at runtime when searching variable.
736 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
737
738 @retval EFI_SUCCESS Variable found successfully
739 @retval EFI_NOT_FOUND Variable not found
740 **/
741 EFI_STATUS
742 FindVariableEx (
743 IN CHAR16 *VariableName,
744 IN EFI_GUID *VendorGuid,
745 IN BOOLEAN IgnoreRtCheck,
746 IN OUT VARIABLE_POINTER_TRACK *PtrTrack
747 )
748 {
749 VARIABLE_HEADER *InDeletedVariable;
750 VOID *Point;
751
752 //
753 // Find the variable by walk through HOB, volatile and non-volatile variable store.
754 //
755 InDeletedVariable = NULL;
756
757 for ( PtrTrack->CurrPtr = PtrTrack->StartPtr
758 ; (PtrTrack->CurrPtr < PtrTrack->EndPtr) && IsValidVariableHeader (PtrTrack->CurrPtr)
759 ; PtrTrack->CurrPtr = GetNextVariablePtr (PtrTrack->CurrPtr)
760 ) {
761 if (PtrTrack->CurrPtr->State == VAR_ADDED ||
762 PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)
763 ) {
764 if (IgnoreRtCheck || !AtRuntime () || ((PtrTrack->CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {
765 if (VariableName[0] == 0) {
766 if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
767 InDeletedVariable = PtrTrack->CurrPtr;
768 } else {
769 return EFI_SUCCESS;
770 }
771 } else {
772 if (CompareGuid (VendorGuid, &PtrTrack->CurrPtr->VendorGuid)) {
773 Point = (VOID *) GetVariableNamePtr (PtrTrack->CurrPtr);
774
775 ASSERT (NameSizeOfVariable (PtrTrack->CurrPtr) != 0);
776 if (CompareMem (VariableName, Point, NameSizeOfVariable (PtrTrack->CurrPtr)) == 0) {
777 if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
778 InDeletedVariable = PtrTrack->CurrPtr;
779 } else {
780 return EFI_SUCCESS;
781 }
782 }
783 }
784 }
785 }
786 }
787 }
788
789 PtrTrack->CurrPtr = InDeletedVariable;
790 return (PtrTrack->CurrPtr == NULL) ? EFI_NOT_FOUND : EFI_SUCCESS;
791 }
792
793
794 /**
795 Finds variable in storage blocks of volatile and non-volatile storage areas.
796
797 This code finds variable in storage blocks of volatile and non-volatile storage areas.
798 If VariableName is an empty string, then we just return the first
799 qualified variable without comparing VariableName and VendorGuid.
800 If IgnoreRtCheck is TRUE, then we ignore the EFI_VARIABLE_RUNTIME_ACCESS attribute check
801 at runtime when searching existing variable, only VariableName and VendorGuid are compared.
802 Otherwise, variables without EFI_VARIABLE_RUNTIME_ACCESS are not visible at runtime.
803
804 @param VariableName Name of the variable to be found.
805 @param VendorGuid Vendor GUID to be found.
806 @param PtrTrack VARIABLE_POINTER_TRACK structure for output,
807 including the range searched and the target position.
808 @param Global Pointer to VARIABLE_GLOBAL structure, including
809 base of volatile variable storage area, base of
810 NV variable storage area, and a lock.
811 @param IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
812 check at runtime when searching variable.
813
814 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while
815 VendorGuid is NULL.
816 @retval EFI_SUCCESS Variable successfully found.
817 @retval EFI_NOT_FOUND Variable not found
818
819 **/
820 EFI_STATUS
821 FindVariable (
822 IN CHAR16 *VariableName,
823 IN EFI_GUID *VendorGuid,
824 OUT VARIABLE_POINTER_TRACK *PtrTrack,
825 IN VARIABLE_GLOBAL *Global,
826 IN BOOLEAN IgnoreRtCheck
827 )
828 {
829 EFI_STATUS Status;
830 VARIABLE_STORE_HEADER *VariableStoreHeader[VariableStoreTypeMax];
831 VARIABLE_STORE_TYPE Type;
832
833 if (VariableName[0] != 0 && VendorGuid == NULL) {
834 return EFI_INVALID_PARAMETER;
835 }
836
837 //
838 // 0: Volatile, 1: HOB, 2: Non-Volatile.
839 // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName
840 // make use of this mapping to implement search algorithm.
841 //
842 VariableStoreHeader[VariableStoreTypeVolatile] = (VARIABLE_STORE_HEADER *) (UINTN) Global->VolatileVariableBase;
843 VariableStoreHeader[VariableStoreTypeHob] = (VARIABLE_STORE_HEADER *) (UINTN) Global->HobVariableBase;
844 VariableStoreHeader[VariableStoreTypeNv] = mNvVariableCache;
845
846 //
847 // Find the variable by walk through HOB, volatile and non-volatile variable store.
848 //
849 for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {
850 if (VariableStoreHeader[Type] == NULL) {
851 continue;
852 }
853
854 PtrTrack->StartPtr = GetStartPointer (VariableStoreHeader[Type]);
855 PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader[Type]);
856 PtrTrack->Volatile = (BOOLEAN) (Type == VariableStoreTypeVolatile);
857
858 Status = FindVariableEx (VariableName, VendorGuid, IgnoreRtCheck, PtrTrack);
859 if (!EFI_ERROR (Status)) {
860 return Status;
861 }
862 }
863 return EFI_NOT_FOUND;
864 }
865
866 /**
867 Get index from supported language codes according to language string.
868
869 This code is used to get corresponding index in supported language codes. It can handle
870 RFC4646 and ISO639 language tags.
871 In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.
872 In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.
873
874 For example:
875 SupportedLang = "engfraengfra"
876 Lang = "eng"
877 Iso639Language = TRUE
878 The return value is "0".
879 Another example:
880 SupportedLang = "en;fr;en-US;fr-FR"
881 Lang = "fr-FR"
882 Iso639Language = FALSE
883 The return value is "3".
884
885 @param SupportedLang Platform supported language codes.
886 @param Lang Configured language.
887 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
888
889 @retval The index of language in the language codes.
890
891 **/
892 UINTN
893 GetIndexFromSupportedLangCodes(
894 IN CHAR8 *SupportedLang,
895 IN CHAR8 *Lang,
896 IN BOOLEAN Iso639Language
897 )
898 {
899 UINTN Index;
900 UINTN CompareLength;
901 UINTN LanguageLength;
902
903 if (Iso639Language) {
904 CompareLength = ISO_639_2_ENTRY_SIZE;
905 for (Index = 0; Index < AsciiStrLen (SupportedLang); Index += CompareLength) {
906 if (AsciiStrnCmp (Lang, SupportedLang + Index, CompareLength) == 0) {
907 //
908 // Successfully find the index of Lang string in SupportedLang string.
909 //
910 Index = Index / CompareLength;
911 return Index;
912 }
913 }
914 ASSERT (FALSE);
915 return 0;
916 } else {
917 //
918 // Compare RFC4646 language code
919 //
920 Index = 0;
921 for (LanguageLength = 0; Lang[LanguageLength] != '\0'; LanguageLength++);
922
923 for (Index = 0; *SupportedLang != '\0'; Index++, SupportedLang += CompareLength) {
924 //
925 // Skip ';' characters in SupportedLang
926 //
927 for (; *SupportedLang != '\0' && *SupportedLang == ';'; SupportedLang++);
928 //
929 // Determine the length of the next language code in SupportedLang
930 //
931 for (CompareLength = 0; SupportedLang[CompareLength] != '\0' && SupportedLang[CompareLength] != ';'; CompareLength++);
932
933 if ((CompareLength == LanguageLength) &&
934 (AsciiStrnCmp (Lang, SupportedLang, CompareLength) == 0)) {
935 //
936 // Successfully find the index of Lang string in SupportedLang string.
937 //
938 return Index;
939 }
940 }
941 ASSERT (FALSE);
942 return 0;
943 }
944 }
945
946 /**
947 Get language string from supported language codes according to index.
948
949 This code is used to get corresponding language strings in supported language codes. It can handle
950 RFC4646 and ISO639 language tags.
951 In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.
952 In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.
953
954 For example:
955 SupportedLang = "engfraengfra"
956 Index = "1"
957 Iso639Language = TRUE
958 The return value is "fra".
959 Another example:
960 SupportedLang = "en;fr;en-US;fr-FR"
961 Index = "1"
962 Iso639Language = FALSE
963 The return value is "fr".
964
965 @param SupportedLang Platform supported language codes.
966 @param Index The index in supported language codes.
967 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
968
969 @retval The language string in the language codes.
970
971 **/
972 CHAR8 *
973 GetLangFromSupportedLangCodes (
974 IN CHAR8 *SupportedLang,
975 IN UINTN Index,
976 IN BOOLEAN Iso639Language
977 )
978 {
979 UINTN SubIndex;
980 UINTN CompareLength;
981 CHAR8 *Supported;
982
983 SubIndex = 0;
984 Supported = SupportedLang;
985 if (Iso639Language) {
986 //
987 // According to the index of Lang string in SupportedLang string to get the language.
988 // This code will be invoked in RUNTIME, therefore there is not a memory allocate/free operation.
989 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
990 //
991 CompareLength = ISO_639_2_ENTRY_SIZE;
992 mVariableModuleGlobal->Lang[CompareLength] = '\0';
993 return CopyMem (mVariableModuleGlobal->Lang, SupportedLang + Index * CompareLength, CompareLength);
994
995 } else {
996 while (TRUE) {
997 //
998 // Take semicolon as delimitation, sequentially traverse supported language codes.
999 //
1000 for (CompareLength = 0; *Supported != ';' && *Supported != '\0'; CompareLength++) {
1001 Supported++;
1002 }
1003 if ((*Supported == '\0') && (SubIndex != Index)) {
1004 //
1005 // Have completed the traverse, but not find corrsponding string.
1006 // This case is not allowed to happen.
1007 //
1008 ASSERT(FALSE);
1009 return NULL;
1010 }
1011 if (SubIndex == Index) {
1012 //
1013 // According to the index of Lang string in SupportedLang string to get the language.
1014 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.
1015 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
1016 //
1017 mVariableModuleGlobal->PlatformLang[CompareLength] = '\0';
1018 return CopyMem (mVariableModuleGlobal->PlatformLang, Supported - CompareLength, CompareLength);
1019 }
1020 SubIndex++;
1021
1022 //
1023 // Skip ';' characters in Supported
1024 //
1025 for (; *Supported != '\0' && *Supported == ';'; Supported++);
1026 }
1027 }
1028 }
1029
1030 /**
1031 Returns a pointer to an allocated buffer that contains the best matching language
1032 from a set of supported languages.
1033
1034 This function supports both ISO 639-2 and RFC 4646 language codes, but language
1035 code types may not be mixed in a single call to this function. This function
1036 supports a variable argument list that allows the caller to pass in a prioritized
1037 list of language codes to test against all the language codes in SupportedLanguages.
1038
1039 If SupportedLanguages is NULL, then ASSERT().
1040
1041 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that
1042 contains a set of language codes in the format
1043 specified by Iso639Language.
1044 @param[in] Iso639Language If TRUE, then all language codes are assumed to be
1045 in ISO 639-2 format. If FALSE, then all language
1046 codes are assumed to be in RFC 4646 language format
1047 @param[in] ... A variable argument list that contains pointers to
1048 Null-terminated ASCII strings that contain one or more
1049 language codes in the format specified by Iso639Language.
1050 The first language code from each of these language
1051 code lists is used to determine if it is an exact or
1052 close match to any of the language codes in
1053 SupportedLanguages. Close matches only apply to RFC 4646
1054 language codes, and the matching algorithm from RFC 4647
1055 is used to determine if a close match is present. If
1056 an exact or close match is found, then the matching
1057 language code from SupportedLanguages is returned. If
1058 no matches are found, then the next variable argument
1059 parameter is evaluated. The variable argument list
1060 is terminated by a NULL.
1061
1062 @retval NULL The best matching language could not be found in SupportedLanguages.
1063 @retval NULL There are not enough resources available to return the best matching
1064 language.
1065 @retval Other A pointer to a Null-terminated ASCII string that is the best matching
1066 language in SupportedLanguages.
1067
1068 **/
1069 CHAR8 *
1070 EFIAPI
1071 VariableGetBestLanguage (
1072 IN CONST CHAR8 *SupportedLanguages,
1073 IN BOOLEAN Iso639Language,
1074 ...
1075 )
1076 {
1077 VA_LIST Args;
1078 CHAR8 *Language;
1079 UINTN CompareLength;
1080 UINTN LanguageLength;
1081 CONST CHAR8 *Supported;
1082 CHAR8 *Buffer;
1083
1084 ASSERT (SupportedLanguages != NULL);
1085
1086 VA_START (Args, Iso639Language);
1087 while ((Language = VA_ARG (Args, CHAR8 *)) != NULL) {
1088 //
1089 // Default to ISO 639-2 mode
1090 //
1091 CompareLength = 3;
1092 LanguageLength = MIN (3, AsciiStrLen (Language));
1093
1094 //
1095 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language
1096 //
1097 if (!Iso639Language) {
1098 for (LanguageLength = 0; Language[LanguageLength] != 0 && Language[LanguageLength] != ';'; LanguageLength++);
1099 }
1100
1101 //
1102 // Trim back the length of Language used until it is empty
1103 //
1104 while (LanguageLength > 0) {
1105 //
1106 // Loop through all language codes in SupportedLanguages
1107 //
1108 for (Supported = SupportedLanguages; *Supported != '\0'; Supported += CompareLength) {
1109 //
1110 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages
1111 //
1112 if (!Iso639Language) {
1113 //
1114 // Skip ';' characters in Supported
1115 //
1116 for (; *Supported != '\0' && *Supported == ';'; Supported++);
1117 //
1118 // Determine the length of the next language code in Supported
1119 //
1120 for (CompareLength = 0; Supported[CompareLength] != 0 && Supported[CompareLength] != ';'; CompareLength++);
1121 //
1122 // If Language is longer than the Supported, then skip to the next language
1123 //
1124 if (LanguageLength > CompareLength) {
1125 continue;
1126 }
1127 }
1128 //
1129 // See if the first LanguageLength characters in Supported match Language
1130 //
1131 if (AsciiStrnCmp (Supported, Language, LanguageLength) == 0) {
1132 VA_END (Args);
1133
1134 Buffer = Iso639Language ? mVariableModuleGlobal->Lang : mVariableModuleGlobal->PlatformLang;
1135 Buffer[CompareLength] = '\0';
1136 return CopyMem (Buffer, Supported, CompareLength);
1137 }
1138 }
1139
1140 if (Iso639Language) {
1141 //
1142 // If ISO 639 mode, then each language can only be tested once
1143 //
1144 LanguageLength = 0;
1145 } else {
1146 //
1147 // If RFC 4646 mode, then trim Language from the right to the next '-' character
1148 //
1149 for (LanguageLength--; LanguageLength > 0 && Language[LanguageLength] != '-'; LanguageLength--);
1150 }
1151 }
1152 }
1153 VA_END (Args);
1154
1155 //
1156 // No matches were found
1157 //
1158 return NULL;
1159 }
1160
1161 /**
1162 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.
1163
1164 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.
1165
1166 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,
1167 and are read-only. Therefore, in variable driver, only store the original value for other use.
1168
1169 @param[in] VariableName Name of variable.
1170
1171 @param[in] Data Variable data.
1172
1173 @param[in] DataSize Size of data. 0 means delete.
1174
1175 **/
1176 VOID
1177 AutoUpdateLangVariable (
1178 IN CHAR16 *VariableName,
1179 IN VOID *Data,
1180 IN UINTN DataSize
1181 )
1182 {
1183 EFI_STATUS Status;
1184 CHAR8 *BestPlatformLang;
1185 CHAR8 *BestLang;
1186 UINTN Index;
1187 UINT32 Attributes;
1188 VARIABLE_POINTER_TRACK Variable;
1189 BOOLEAN SetLanguageCodes;
1190
1191 //
1192 // Don't do updates for delete operation
1193 //
1194 if (DataSize == 0) {
1195 return;
1196 }
1197
1198 SetLanguageCodes = FALSE;
1199
1200 if (StrCmp (VariableName, L"PlatformLangCodes") == 0) {
1201 //
1202 // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.
1203 //
1204 if (AtRuntime ()) {
1205 return;
1206 }
1207
1208 SetLanguageCodes = TRUE;
1209
1210 //
1211 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only
1212 // Therefore, in variable driver, only store the original value for other use.
1213 //
1214 if (mVariableModuleGlobal->PlatformLangCodes != NULL) {
1215 FreePool (mVariableModuleGlobal->PlatformLangCodes);
1216 }
1217 mVariableModuleGlobal->PlatformLangCodes = AllocateRuntimeCopyPool (DataSize, Data);
1218 ASSERT (mVariableModuleGlobal->PlatformLangCodes != NULL);
1219
1220 //
1221 // PlatformLang holds a single language from PlatformLangCodes,
1222 // so the size of PlatformLangCodes is enough for the PlatformLang.
1223 //
1224 if (mVariableModuleGlobal->PlatformLang != NULL) {
1225 FreePool (mVariableModuleGlobal->PlatformLang);
1226 }
1227 mVariableModuleGlobal->PlatformLang = AllocateRuntimePool (DataSize);
1228 ASSERT (mVariableModuleGlobal->PlatformLang != NULL);
1229
1230 } else if (StrCmp (VariableName, L"LangCodes") == 0) {
1231 //
1232 // LangCodes is a volatile variable, so it can not be updated at runtime.
1233 //
1234 if (AtRuntime ()) {
1235 return;
1236 }
1237
1238 SetLanguageCodes = TRUE;
1239
1240 //
1241 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only
1242 // Therefore, in variable driver, only store the original value for other use.
1243 //
1244 if (mVariableModuleGlobal->LangCodes != NULL) {
1245 FreePool (mVariableModuleGlobal->LangCodes);
1246 }
1247 mVariableModuleGlobal->LangCodes = AllocateRuntimeCopyPool (DataSize, Data);
1248 ASSERT (mVariableModuleGlobal->LangCodes != NULL);
1249 }
1250
1251 if (SetLanguageCodes
1252 && (mVariableModuleGlobal->PlatformLangCodes != NULL)
1253 && (mVariableModuleGlobal->LangCodes != NULL)) {
1254 //
1255 // Update Lang if PlatformLang is already set
1256 // Update PlatformLang if Lang is already set
1257 //
1258 Status = FindVariable (L"PlatformLang", &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);
1259 if (!EFI_ERROR (Status)) {
1260 //
1261 // Update Lang
1262 //
1263 VariableName = L"PlatformLang";
1264 Data = GetVariableDataPtr (Variable.CurrPtr);
1265 DataSize = Variable.CurrPtr->DataSize;
1266 } else {
1267 Status = FindVariable (L"Lang", &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);
1268 if (!EFI_ERROR (Status)) {
1269 //
1270 // Update PlatformLang
1271 //
1272 VariableName = L"Lang";
1273 Data = GetVariableDataPtr (Variable.CurrPtr);
1274 DataSize = Variable.CurrPtr->DataSize;
1275 } else {
1276 //
1277 // Neither PlatformLang nor Lang is set, directly return
1278 //
1279 return;
1280 }
1281 }
1282 }
1283
1284 //
1285 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.
1286 //
1287 Attributes = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;
1288
1289 if (StrCmp (VariableName, L"PlatformLang") == 0) {
1290 //
1291 // Update Lang when PlatformLangCodes/LangCodes were set.
1292 //
1293 if ((mVariableModuleGlobal->PlatformLangCodes != NULL) && (mVariableModuleGlobal->LangCodes != NULL)) {
1294 //
1295 // When setting PlatformLang, firstly get most matched language string from supported language codes.
1296 //
1297 BestPlatformLang = VariableGetBestLanguage (mVariableModuleGlobal->PlatformLangCodes, FALSE, Data, NULL);
1298 if (BestPlatformLang != NULL) {
1299 //
1300 // Get the corresponding index in language codes.
1301 //
1302 Index = GetIndexFromSupportedLangCodes (mVariableModuleGlobal->PlatformLangCodes, BestPlatformLang, FALSE);
1303
1304 //
1305 // Get the corresponding ISO639 language tag according to RFC4646 language tag.
1306 //
1307 BestLang = GetLangFromSupportedLangCodes (mVariableModuleGlobal->LangCodes, Index, TRUE);
1308
1309 //
1310 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.
1311 //
1312 FindVariable (L"Lang", &gEfiGlobalVariableGuid, &Variable, (VARIABLE_GLOBAL *)mVariableModuleGlobal, FALSE);
1313
1314 Status = UpdateVariable (L"Lang", &gEfiGlobalVariableGuid, BestLang,
1315 ISO_639_2_ENTRY_SIZE + 1, Attributes, &Variable);
1316
1317 DEBUG ((EFI_D_INFO, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a\n", BestPlatformLang, BestLang));
1318
1319 ASSERT_EFI_ERROR(Status);
1320 }
1321 }
1322
1323 } else if (StrCmp (VariableName, L"Lang") == 0) {
1324 //
1325 // Update PlatformLang when PlatformLangCodes/LangCodes were set.
1326 //
1327 if ((mVariableModuleGlobal->PlatformLangCodes != NULL) && (mVariableModuleGlobal->LangCodes != NULL)) {
1328 //
1329 // When setting Lang, firstly get most matched language string from supported language codes.
1330 //
1331 BestLang = VariableGetBestLanguage (mVariableModuleGlobal->LangCodes, TRUE, Data, NULL);
1332 if (BestLang != NULL) {
1333 //
1334 // Get the corresponding index in language codes.
1335 //
1336 Index = GetIndexFromSupportedLangCodes (mVariableModuleGlobal->LangCodes, BestLang, TRUE);
1337
1338 //
1339 // Get the corresponding RFC4646 language tag according to ISO639 language tag.
1340 //
1341 BestPlatformLang = GetLangFromSupportedLangCodes (mVariableModuleGlobal->PlatformLangCodes, Index, FALSE);
1342
1343 //
1344 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.
1345 //
1346 FindVariable (L"PlatformLang", &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);
1347
1348 Status = UpdateVariable (L"PlatformLang", &gEfiGlobalVariableGuid, BestPlatformLang,
1349 AsciiStrSize (BestPlatformLang), Attributes, &Variable);
1350
1351 DEBUG ((EFI_D_INFO, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a\n", BestLang, BestPlatformLang));
1352 ASSERT_EFI_ERROR (Status);
1353 }
1354 }
1355 }
1356 }
1357
1358 /**
1359 Update the variable region with Variable information. These are the same
1360 arguments as the EFI Variable services.
1361
1362 @param[in] VariableName Name of variable.
1363 @param[in] VendorGuid Guid of variable.
1364 @param[in] Data Variable data.
1365 @param[in] DataSize Size of data. 0 means delete.
1366 @param[in] Attributes Attribues of the variable.
1367 @param[in] CacheVariable The variable information which is used to keep track of variable usage.
1368
1369 @retval EFI_SUCCESS The update operation is success.
1370 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.
1371
1372 **/
1373 EFI_STATUS
1374 UpdateVariable (
1375 IN CHAR16 *VariableName,
1376 IN EFI_GUID *VendorGuid,
1377 IN VOID *Data,
1378 IN UINTN DataSize,
1379 IN UINT32 Attributes OPTIONAL,
1380 IN VARIABLE_POINTER_TRACK *CacheVariable
1381 )
1382 {
1383 EFI_STATUS Status;
1384 VARIABLE_HEADER *NextVariable;
1385 UINTN ScratchSize;
1386 UINTN NonVolatileVarableStoreSize;
1387 UINTN VarNameOffset;
1388 UINTN VarDataOffset;
1389 UINTN VarNameSize;
1390 UINTN VarSize;
1391 BOOLEAN Volatile;
1392 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;
1393 UINT8 State;
1394 BOOLEAN Reclaimed;
1395 VARIABLE_POINTER_TRACK *Variable;
1396 VARIABLE_POINTER_TRACK NvVariable;
1397 VARIABLE_STORE_HEADER *VariableStoreHeader;
1398 UINTN CacheOffset;
1399
1400 if ((mVariableModuleGlobal->FvbInstance == NULL) && ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0)) {
1401 //
1402 // The FVB protocol is not ready. Trying to update NV variable prior to the installation
1403 // of EFI_VARIABLE_WRITE_ARCH_PROTOCOL.
1404 //
1405 return EFI_NOT_AVAILABLE_YET;
1406 }
1407
1408 if ((CacheVariable->CurrPtr == NULL) || CacheVariable->Volatile) {
1409 Variable = CacheVariable;
1410 } else {
1411 //
1412 // Update/Delete existing NV variable.
1413 // CacheVariable points to the variable in the memory copy of Flash area
1414 // Now let Variable points to the same variable in Flash area.
1415 //
1416 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);
1417 Variable = &NvVariable;
1418 Variable->StartPtr = GetStartPointer (VariableStoreHeader);
1419 Variable->EndPtr = GetEndPointer (VariableStoreHeader);
1420 Variable->CurrPtr = (VARIABLE_HEADER *)((UINTN)Variable->StartPtr + ((UINTN)CacheVariable->CurrPtr - (UINTN)CacheVariable->StartPtr));
1421 Variable->Volatile = FALSE;
1422 }
1423
1424 Fvb = mVariableModuleGlobal->FvbInstance;
1425 Reclaimed = FALSE;
1426
1427 if (Variable->CurrPtr != NULL) {
1428 //
1429 // Update/Delete existing variable.
1430 //
1431 if (AtRuntime ()) {
1432 //
1433 // If AtRuntime and the variable is Volatile and Runtime Access,
1434 // the volatile is ReadOnly, and SetVariable should be aborted and
1435 // return EFI_WRITE_PROTECTED.
1436 //
1437 if (Variable->Volatile) {
1438 Status = EFI_WRITE_PROTECTED;
1439 goto Done;
1440 }
1441 //
1442 // Only variable that have NV|RT attributes can be updated/deleted in Runtime.
1443 //
1444 if (((Variable->CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) || ((Variable->CurrPtr->Attributes & EFI_VARIABLE_NON_VOLATILE) == 0)) {
1445 Status = EFI_INVALID_PARAMETER;
1446 goto Done;
1447 }
1448 }
1449
1450 //
1451 // Setting a data variable with no access, or zero DataSize attributes
1452 // causes it to be deleted.
1453 //
1454 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {
1455 State = Variable->CurrPtr->State;
1456 State &= VAR_DELETED;
1457
1458 Status = UpdateVariableStore (
1459 &mVariableModuleGlobal->VariableGlobal,
1460 Variable->Volatile,
1461 FALSE,
1462 Fvb,
1463 (UINTN) &Variable->CurrPtr->State,
1464 sizeof (UINT8),
1465 &State
1466 );
1467 if (!EFI_ERROR (Status)) {
1468 UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, FALSE, TRUE, FALSE);
1469 if (!Variable->Volatile) {
1470 CacheVariable->CurrPtr->State = State;
1471 }
1472 }
1473 goto Done;
1474 }
1475 //
1476 // If the variable is marked valid, and the same data has been passed in,
1477 // then return to the caller immediately.
1478 //
1479 if (DataSizeOfVariable (Variable->CurrPtr) == DataSize &&
1480 (CompareMem (Data, GetVariableDataPtr (Variable->CurrPtr), DataSize) == 0)) {
1481
1482 UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, TRUE, FALSE, FALSE);
1483 Status = EFI_SUCCESS;
1484 goto Done;
1485 } else if ((Variable->CurrPtr->State == VAR_ADDED) ||
1486 (Variable->CurrPtr->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {
1487
1488 //
1489 // Mark the old variable as in delete transition.
1490 //
1491 State = Variable->CurrPtr->State;
1492 State &= VAR_IN_DELETED_TRANSITION;
1493
1494 Status = UpdateVariableStore (
1495 &mVariableModuleGlobal->VariableGlobal,
1496 Variable->Volatile,
1497 FALSE,
1498 Fvb,
1499 (UINTN) &Variable->CurrPtr->State,
1500 sizeof (UINT8),
1501 &State
1502 );
1503 if (EFI_ERROR (Status)) {
1504 goto Done;
1505 }
1506 if (!Variable->Volatile) {
1507 CacheVariable->CurrPtr->State = State;
1508 }
1509 }
1510 } else {
1511 //
1512 // Not found existing variable. Create a new variable.
1513 //
1514
1515 //
1516 // Make sure we are trying to create a new variable.
1517 // Setting a data variable with zero DataSize or no access attributes means to delete it.
1518 //
1519 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {
1520 Status = EFI_NOT_FOUND;
1521 goto Done;
1522 }
1523
1524 //
1525 // Only variable have NV|RT attribute can be created in Runtime.
1526 //
1527 if (AtRuntime () &&
1528 (((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) || ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0))) {
1529 Status = EFI_INVALID_PARAMETER;
1530 goto Done;
1531 }
1532 }
1533
1534 //
1535 // Function part - create a new variable and copy the data.
1536 // Both update a variable and create a variable will come here.
1537
1538 //
1539 // Tricky part: Use scratch data area at the end of volatile variable store
1540 // as a temporary storage.
1541 //
1542 NextVariable = GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase));
1543 ScratchSize = MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize));
1544
1545 SetMem (NextVariable, ScratchSize, 0xff);
1546
1547 NextVariable->StartId = VARIABLE_DATA;
1548 NextVariable->Attributes = Attributes;
1549 //
1550 // NextVariable->State = VAR_ADDED;
1551 //
1552 NextVariable->Reserved = 0;
1553 VarNameOffset = sizeof (VARIABLE_HEADER);
1554 VarNameSize = StrSize (VariableName);
1555 CopyMem (
1556 (UINT8 *) ((UINTN) NextVariable + VarNameOffset),
1557 VariableName,
1558 VarNameSize
1559 );
1560 VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);
1561 CopyMem (
1562 (UINT8 *) ((UINTN) NextVariable + VarDataOffset),
1563 Data,
1564 DataSize
1565 );
1566 CopyMem (&NextVariable->VendorGuid, VendorGuid, sizeof (EFI_GUID));
1567 //
1568 // There will be pad bytes after Data, the NextVariable->NameSize and
1569 // NextVariable->DataSize should not include pad size so that variable
1570 // service can get actual size in GetVariable.
1571 //
1572 NextVariable->NameSize = (UINT32)VarNameSize;
1573 NextVariable->DataSize = (UINT32)DataSize;
1574
1575 //
1576 // The actual size of the variable that stores in storage should
1577 // include pad size.
1578 //
1579 VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);
1580 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {
1581 //
1582 // Create a nonvolatile variable.
1583 //
1584 Volatile = FALSE;
1585 NonVolatileVarableStoreSize = ((VARIABLE_STORE_HEADER *)(UINTN)(mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase))->Size;
1586 if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0)
1587 && ((VarSize + mVariableModuleGlobal->HwErrVariableTotalSize) > PcdGet32 (PcdHwErrStorageSize)))
1588 || (((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0)
1589 && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > NonVolatileVarableStoreSize - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize)))) {
1590 if (AtRuntime ()) {
1591 Status = EFI_OUT_OF_RESOURCES;
1592 goto Done;
1593 }
1594 //
1595 // Perform garbage collection & reclaim operation.
1596 //
1597 Status = Reclaim (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,
1598 &mVariableModuleGlobal->NonVolatileLastVariableOffset, FALSE, Variable->CurrPtr);
1599 if (EFI_ERROR (Status)) {
1600 goto Done;
1601 }
1602 //
1603 // If still no enough space, return out of resources.
1604 //
1605 if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0)
1606 && ((VarSize + mVariableModuleGlobal->HwErrVariableTotalSize) > PcdGet32 (PcdHwErrStorageSize)))
1607 || (((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0)
1608 && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > NonVolatileVarableStoreSize - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize)))) {
1609 Status = EFI_OUT_OF_RESOURCES;
1610 goto Done;
1611 }
1612 Reclaimed = TRUE;
1613 }
1614 //
1615 // Four steps
1616 // 1. Write variable header
1617 // 2. Set variable state to header valid
1618 // 3. Write variable data
1619 // 4. Set variable state to valid
1620 //
1621 //
1622 // Step 1:
1623 //
1624 CacheOffset = mVariableModuleGlobal->NonVolatileLastVariableOffset;
1625 Status = UpdateVariableStore (
1626 &mVariableModuleGlobal->VariableGlobal,
1627 FALSE,
1628 TRUE,
1629 Fvb,
1630 mVariableModuleGlobal->NonVolatileLastVariableOffset,
1631 sizeof (VARIABLE_HEADER),
1632 (UINT8 *) NextVariable
1633 );
1634
1635 if (EFI_ERROR (Status)) {
1636 goto Done;
1637 }
1638
1639 //
1640 // Step 2:
1641 //
1642 NextVariable->State = VAR_HEADER_VALID_ONLY;
1643 Status = UpdateVariableStore (
1644 &mVariableModuleGlobal->VariableGlobal,
1645 FALSE,
1646 TRUE,
1647 Fvb,
1648 mVariableModuleGlobal->NonVolatileLastVariableOffset + OFFSET_OF (VARIABLE_HEADER, State),
1649 sizeof (UINT8),
1650 &NextVariable->State
1651 );
1652
1653 if (EFI_ERROR (Status)) {
1654 goto Done;
1655 }
1656 //
1657 // Step 3:
1658 //
1659 Status = UpdateVariableStore (
1660 &mVariableModuleGlobal->VariableGlobal,
1661 FALSE,
1662 TRUE,
1663 Fvb,
1664 mVariableModuleGlobal->NonVolatileLastVariableOffset + sizeof (VARIABLE_HEADER),
1665 (UINT32) VarSize - sizeof (VARIABLE_HEADER),
1666 (UINT8 *) NextVariable + sizeof (VARIABLE_HEADER)
1667 );
1668
1669 if (EFI_ERROR (Status)) {
1670 goto Done;
1671 }
1672 //
1673 // Step 4:
1674 //
1675 NextVariable->State = VAR_ADDED;
1676 Status = UpdateVariableStore (
1677 &mVariableModuleGlobal->VariableGlobal,
1678 FALSE,
1679 TRUE,
1680 Fvb,
1681 mVariableModuleGlobal->NonVolatileLastVariableOffset + OFFSET_OF (VARIABLE_HEADER, State),
1682 sizeof (UINT8),
1683 &NextVariable->State
1684 );
1685
1686 if (EFI_ERROR (Status)) {
1687 goto Done;
1688 }
1689
1690 mVariableModuleGlobal->NonVolatileLastVariableOffset += HEADER_ALIGN (VarSize);
1691
1692 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {
1693 mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VarSize);
1694 } else {
1695 mVariableModuleGlobal->CommonVariableTotalSize += HEADER_ALIGN (VarSize);
1696 }
1697 //
1698 // update the memory copy of Flash region.
1699 //
1700 CopyMem ((UINT8 *)mNvVariableCache + CacheOffset, (UINT8 *)NextVariable, VarSize);
1701 } else {
1702 //
1703 // Create a volatile variable.
1704 //
1705 Volatile = TRUE;
1706
1707 if ((UINT32) (VarSize + mVariableModuleGlobal->VolatileLastVariableOffset) >
1708 ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size) {
1709 //
1710 // Perform garbage collection & reclaim operation.
1711 //
1712 Status = Reclaim (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase,
1713 &mVariableModuleGlobal->VolatileLastVariableOffset, TRUE, Variable->CurrPtr);
1714 if (EFI_ERROR (Status)) {
1715 goto Done;
1716 }
1717 //
1718 // If still no enough space, return out of resources.
1719 //
1720 if ((UINT32) (VarSize + mVariableModuleGlobal->VolatileLastVariableOffset) >
1721 ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size
1722 ) {
1723 Status = EFI_OUT_OF_RESOURCES;
1724 goto Done;
1725 }
1726 Reclaimed = TRUE;
1727 }
1728
1729 NextVariable->State = VAR_ADDED;
1730 Status = UpdateVariableStore (
1731 &mVariableModuleGlobal->VariableGlobal,
1732 TRUE,
1733 TRUE,
1734 Fvb,
1735 mVariableModuleGlobal->VolatileLastVariableOffset,
1736 (UINT32) VarSize,
1737 (UINT8 *) NextVariable
1738 );
1739
1740 if (EFI_ERROR (Status)) {
1741 goto Done;
1742 }
1743
1744 mVariableModuleGlobal->VolatileLastVariableOffset += HEADER_ALIGN (VarSize);
1745 }
1746
1747 //
1748 // Mark the old variable as deleted.
1749 //
1750 if (!Reclaimed && !EFI_ERROR (Status) && Variable->CurrPtr != NULL) {
1751 State = Variable->CurrPtr->State;
1752 State &= VAR_DELETED;
1753
1754 Status = UpdateVariableStore (
1755 &mVariableModuleGlobal->VariableGlobal,
1756 Variable->Volatile,
1757 FALSE,
1758 Fvb,
1759 (UINTN) &Variable->CurrPtr->State,
1760 sizeof (UINT8),
1761 &State
1762 );
1763 if (!EFI_ERROR (Status) && !Variable->Volatile) {
1764 CacheVariable->CurrPtr->State = State;
1765 }
1766 }
1767
1768 if (!EFI_ERROR (Status)) {
1769 UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);
1770 }
1771
1772 Done:
1773 return Status;
1774 }
1775
1776 /**
1777 Check if a Unicode character is a hexadecimal character.
1778
1779 This function checks if a Unicode character is a
1780 hexadecimal character. The valid hexadecimal character is
1781 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
1782
1783
1784 @param Char The character to check against.
1785
1786 @retval TRUE If the Char is a hexadecmial character.
1787 @retval FALSE If the Char is not a hexadecmial character.
1788
1789 **/
1790 BOOLEAN
1791 EFIAPI
1792 IsHexaDecimalDigitCharacter (
1793 IN CHAR16 Char
1794 )
1795 {
1796 return (BOOLEAN) ((Char >= L'0' && Char <= L'9') || (Char >= L'A' && Char <= L'F') || (Char >= L'a' && Char <= L'f'));
1797 }
1798
1799 /**
1800
1801 This code checks if variable is hardware error record variable or not.
1802
1803 According to UEFI spec, hardware error record variable should use the EFI_HARDWARE_ERROR_VARIABLE VendorGuid
1804 and have the L"HwErrRec####" name convention, #### is a printed hex value and no 0x or h is included in the hex value.
1805
1806 @param VariableName Pointer to variable name.
1807 @param VendorGuid Variable Vendor Guid.
1808
1809 @retval TRUE Variable is hardware error record variable.
1810 @retval FALSE Variable is not hardware error record variable.
1811
1812 **/
1813 BOOLEAN
1814 EFIAPI
1815 IsHwErrRecVariable (
1816 IN CHAR16 *VariableName,
1817 IN EFI_GUID *VendorGuid
1818 )
1819 {
1820 if (!CompareGuid (VendorGuid, &gEfiHardwareErrorVariableGuid) ||
1821 (StrLen (VariableName) != StrLen (L"HwErrRec####")) ||
1822 (StrnCmp(VariableName, L"HwErrRec", StrLen (L"HwErrRec")) != 0) ||
1823 !IsHexaDecimalDigitCharacter (VariableName[0x8]) ||
1824 !IsHexaDecimalDigitCharacter (VariableName[0x9]) ||
1825 !IsHexaDecimalDigitCharacter (VariableName[0xA]) ||
1826 !IsHexaDecimalDigitCharacter (VariableName[0xB])) {
1827 return FALSE;
1828 }
1829
1830 return TRUE;
1831 }
1832
1833 /**
1834
1835 This code finds variable in storage blocks (Volatile or Non-Volatile).
1836
1837 @param VariableName Name of Variable to be found.
1838 @param VendorGuid Variable vendor GUID.
1839 @param Attributes Attribute value of the variable found.
1840 @param DataSize Size of Data found. If size is less than the
1841 data, this value contains the required size.
1842 @param Data Data pointer.
1843
1844 @return EFI_INVALID_PARAMETER Invalid parameter.
1845 @return EFI_SUCCESS Find the specified variable.
1846 @return EFI_NOT_FOUND Not found.
1847 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
1848
1849 **/
1850 EFI_STATUS
1851 EFIAPI
1852 VariableServiceGetVariable (
1853 IN CHAR16 *VariableName,
1854 IN EFI_GUID *VendorGuid,
1855 OUT UINT32 *Attributes OPTIONAL,
1856 IN OUT UINTN *DataSize,
1857 OUT VOID *Data
1858 )
1859 {
1860 EFI_STATUS Status;
1861 VARIABLE_POINTER_TRACK Variable;
1862 UINTN VarDataSize;
1863
1864 if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {
1865 return EFI_INVALID_PARAMETER;
1866 }
1867
1868 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
1869
1870 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);
1871 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {
1872 goto Done;
1873 }
1874
1875 //
1876 // Get data size
1877 //
1878 VarDataSize = DataSizeOfVariable (Variable.CurrPtr);
1879 ASSERT (VarDataSize != 0);
1880
1881 if (*DataSize >= VarDataSize) {
1882 if (Data == NULL) {
1883 Status = EFI_INVALID_PARAMETER;
1884 goto Done;
1885 }
1886
1887 CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);
1888 if (Attributes != NULL) {
1889 *Attributes = Variable.CurrPtr->Attributes;
1890 }
1891
1892 *DataSize = VarDataSize;
1893 UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, TRUE, FALSE, FALSE, FALSE);
1894
1895 Status = EFI_SUCCESS;
1896 goto Done;
1897 } else {
1898 *DataSize = VarDataSize;
1899 Status = EFI_BUFFER_TOO_SMALL;
1900 goto Done;
1901 }
1902
1903 Done:
1904 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
1905 return Status;
1906 }
1907
1908
1909
1910 /**
1911
1912 This code Finds the Next available variable.
1913
1914 @param VariableNameSize Size of the variable name.
1915 @param VariableName Pointer to variable name.
1916 @param VendorGuid Variable Vendor Guid.
1917
1918 @return EFI_INVALID_PARAMETER Invalid parameter.
1919 @return EFI_SUCCESS Find the specified variable.
1920 @return EFI_NOT_FOUND Not found.
1921 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
1922
1923 **/
1924 EFI_STATUS
1925 EFIAPI
1926 VariableServiceGetNextVariableName (
1927 IN OUT UINTN *VariableNameSize,
1928 IN OUT CHAR16 *VariableName,
1929 IN OUT EFI_GUID *VendorGuid
1930 )
1931 {
1932 VARIABLE_STORE_TYPE Type;
1933 VARIABLE_POINTER_TRACK Variable;
1934 VARIABLE_POINTER_TRACK VariableInHob;
1935 UINTN VarNameSize;
1936 EFI_STATUS Status;
1937 VARIABLE_STORE_HEADER *VariableStoreHeader[VariableStoreTypeMax];
1938
1939 if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {
1940 return EFI_INVALID_PARAMETER;
1941 }
1942
1943 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
1944
1945 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);
1946 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {
1947 goto Done;
1948 }
1949
1950 if (VariableName[0] != 0) {
1951 //
1952 // If variable name is not NULL, get next variable.
1953 //
1954 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
1955 }
1956
1957 //
1958 // 0: Volatile, 1: HOB, 2: Non-Volatile.
1959 // The index and attributes mapping must be kept in this order as FindVariable
1960 // makes use of this mapping to implement search algorithm.
1961 //
1962 VariableStoreHeader[VariableStoreTypeVolatile] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;
1963 VariableStoreHeader[VariableStoreTypeHob] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;
1964 VariableStoreHeader[VariableStoreTypeNv] = mNvVariableCache;
1965
1966 while (TRUE) {
1967 //
1968 // Switch from Volatile to HOB, to Non-Volatile.
1969 //
1970 while ((Variable.CurrPtr >= Variable.EndPtr) ||
1971 (Variable.CurrPtr == NULL) ||
1972 !IsValidVariableHeader (Variable.CurrPtr)
1973 ) {
1974 //
1975 // Find current storage index
1976 //
1977 for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {
1978 if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr == GetStartPointer (VariableStoreHeader[Type]))) {
1979 break;
1980 }
1981 }
1982 ASSERT (Type < VariableStoreTypeMax);
1983 //
1984 // Switch to next storage
1985 //
1986 for (Type++; Type < VariableStoreTypeMax; Type++) {
1987 if (VariableStoreHeader[Type] != NULL) {
1988 break;
1989 }
1990 }
1991 //
1992 // Capture the case that
1993 // 1. current storage is the last one, or
1994 // 2. no further storage
1995 //
1996 if (Type == VariableStoreTypeMax) {
1997 Status = EFI_NOT_FOUND;
1998 goto Done;
1999 }
2000 Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);
2001 Variable.EndPtr = GetEndPointer (VariableStoreHeader[Type]);
2002 Variable.CurrPtr = Variable.StartPtr;
2003 }
2004
2005 //
2006 // Variable is found
2007 //
2008 if (Variable.CurrPtr->State == VAR_ADDED) {
2009 if ((AtRuntime () && ((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0)) == 0) {
2010
2011 //
2012 // Don't return NV variable when HOB overrides it
2013 //
2014 if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) && (VariableStoreHeader[VariableStoreTypeNv] != NULL) &&
2015 (Variable.StartPtr == GetStartPointer (VariableStoreHeader[VariableStoreTypeNv]))
2016 ) {
2017 VariableInHob.StartPtr = GetStartPointer (VariableStoreHeader[VariableStoreTypeHob]);
2018 VariableInHob.EndPtr = GetEndPointer (VariableStoreHeader[VariableStoreTypeHob]);
2019 Status = FindVariableEx (
2020 GetVariableNamePtr (Variable.CurrPtr),
2021 &Variable.CurrPtr->VendorGuid,
2022 FALSE,
2023 &VariableInHob
2024 );
2025 if (!EFI_ERROR (Status)) {
2026 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
2027 continue;
2028 }
2029 }
2030
2031 VarNameSize = NameSizeOfVariable (Variable.CurrPtr);
2032 ASSERT (VarNameSize != 0);
2033
2034 if (VarNameSize <= *VariableNameSize) {
2035 CopyMem (VariableName, GetVariableNamePtr (Variable.CurrPtr), VarNameSize);
2036 CopyMem (VendorGuid, &Variable.CurrPtr->VendorGuid, sizeof (EFI_GUID));
2037 Status = EFI_SUCCESS;
2038 } else {
2039 Status = EFI_BUFFER_TOO_SMALL;
2040 }
2041
2042 *VariableNameSize = VarNameSize;
2043 goto Done;
2044 }
2045 }
2046
2047 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
2048 }
2049
2050 Done:
2051 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
2052 return Status;
2053 }
2054
2055 /**
2056
2057 This code sets variable in storage blocks (Volatile or Non-Volatile).
2058
2059 @param VariableName Name of Variable to be found.
2060 @param VendorGuid Variable vendor GUID.
2061 @param Attributes Attribute value of the variable found
2062 @param DataSize Size of Data found. If size is less than the
2063 data, this value contains the required size.
2064 @param Data Data pointer.
2065
2066 @return EFI_INVALID_PARAMETER Invalid parameter.
2067 @return EFI_SUCCESS Set successfully.
2068 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.
2069 @return EFI_NOT_FOUND Not found.
2070 @return EFI_WRITE_PROTECTED Variable is read-only.
2071
2072 **/
2073 EFI_STATUS
2074 EFIAPI
2075 VariableServiceSetVariable (
2076 IN CHAR16 *VariableName,
2077 IN EFI_GUID *VendorGuid,
2078 IN UINT32 Attributes,
2079 IN UINTN DataSize,
2080 IN VOID *Data
2081 )
2082 {
2083 VARIABLE_POINTER_TRACK Variable;
2084 EFI_STATUS Status;
2085 VARIABLE_HEADER *NextVariable;
2086 EFI_PHYSICAL_ADDRESS Point;
2087
2088 //
2089 // Check input parameters.
2090 //
2091 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {
2092 return EFI_INVALID_PARAMETER;
2093 }
2094
2095 if (DataSize != 0 && Data == NULL) {
2096 return EFI_INVALID_PARAMETER;
2097 }
2098
2099 //
2100 // Not support authenticated variable write yet.
2101 //
2102 if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {
2103 return EFI_INVALID_PARAMETER;
2104 }
2105
2106 //
2107 // Make sure if runtime bit is set, boot service bit is set also.
2108 //
2109 if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {
2110 return EFI_INVALID_PARAMETER;
2111 }
2112
2113 //
2114 // The size of the VariableName, including the Unicode Null in bytes plus
2115 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)
2116 // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.
2117 //
2118 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
2119 if ((DataSize > PcdGet32 (PcdMaxHardwareErrorVariableSize)) ||
2120 (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > PcdGet32 (PcdMaxHardwareErrorVariableSize))) {
2121 return EFI_INVALID_PARAMETER;
2122 }
2123 if (!IsHwErrRecVariable(VariableName, VendorGuid)) {
2124 return EFI_INVALID_PARAMETER;
2125 }
2126 } else {
2127 //
2128 // The size of the VariableName, including the Unicode Null in bytes plus
2129 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxVariableSize) bytes.
2130 //
2131 if ((DataSize > PcdGet32 (PcdMaxVariableSize)) ||
2132 (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > PcdGet32 (PcdMaxVariableSize))) {
2133 return EFI_INVALID_PARAMETER;
2134 }
2135 }
2136
2137 if (AtRuntime ()) {
2138 //
2139 // HwErrRecSupport Global Variable identifies the level of hardware error record persistence
2140 // support implemented by the platform. This variable is only modified by firmware and is read-only to the OS.
2141 //
2142 if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, L"HwErrRecSupport") == 0)) {
2143 return EFI_WRITE_PROTECTED;
2144 }
2145 }
2146
2147 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
2148
2149 //
2150 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.
2151 //
2152 if (1 < InterlockedIncrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState)) {
2153 Point = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;
2154 //
2155 // Parse non-volatile variable data and get last variable offset.
2156 //
2157 NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point);
2158 while ((NextVariable < GetEndPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point))
2159 && IsValidVariableHeader (NextVariable)) {
2160 NextVariable = GetNextVariablePtr (NextVariable);
2161 }
2162 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) Point;
2163 }
2164
2165 //
2166 // Check whether the input variable is already existed.
2167 //
2168 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, TRUE);
2169 if (!EFI_ERROR (Status)) {
2170 if (((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) && AtRuntime ()) {
2171 return EFI_WRITE_PROTECTED;
2172 }
2173 }
2174
2175 //
2176 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.
2177 //
2178 AutoUpdateLangVariable (VariableName, Data, DataSize);
2179
2180 Status = UpdateVariable (VariableName, VendorGuid, Data, DataSize, Attributes, &Variable);
2181
2182 InterlockedDecrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState);
2183 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
2184
2185 return Status;
2186 }
2187
2188 /**
2189
2190 This code returns information about the EFI variables.
2191
2192 @param Attributes Attributes bitmask to specify the type of variables
2193 on which to return information.
2194 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
2195 for the EFI variables associated with the attributes specified.
2196 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
2197 for EFI variables associated with the attributes specified.
2198 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
2199 associated with the attributes specified.
2200
2201 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
2202 @return EFI_SUCCESS Query successfully.
2203 @return EFI_UNSUPPORTED The attribute is not supported on this platform.
2204
2205 **/
2206 EFI_STATUS
2207 EFIAPI
2208 VariableServiceQueryVariableInfo (
2209 IN UINT32 Attributes,
2210 OUT UINT64 *MaximumVariableStorageSize,
2211 OUT UINT64 *RemainingVariableStorageSize,
2212 OUT UINT64 *MaximumVariableSize
2213 )
2214 {
2215 VARIABLE_HEADER *Variable;
2216 VARIABLE_HEADER *NextVariable;
2217 UINT64 VariableSize;
2218 VARIABLE_STORE_HEADER *VariableStoreHeader;
2219 UINT64 CommonVariableTotalSize;
2220 UINT64 HwErrVariableTotalSize;
2221
2222 CommonVariableTotalSize = 0;
2223 HwErrVariableTotalSize = 0;
2224
2225 if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {
2226 return EFI_INVALID_PARAMETER;
2227 }
2228
2229 if((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == 0) {
2230 //
2231 // Make sure the Attributes combination is supported by the platform.
2232 //
2233 return EFI_UNSUPPORTED;
2234 } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {
2235 //
2236 // Make sure if runtime bit is set, boot service bit is set also.
2237 //
2238 return EFI_INVALID_PARAMETER;
2239 } else if (AtRuntime () && ((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0)) {
2240 //
2241 // Make sure RT Attribute is set if we are in Runtime phase.
2242 //
2243 return EFI_INVALID_PARAMETER;
2244 } else if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
2245 //
2246 // Make sure Hw Attribute is set with NV.
2247 //
2248 return EFI_INVALID_PARAMETER;
2249 } else if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {
2250 //
2251 // Not support authentiated variable write yet.
2252 //
2253 return EFI_UNSUPPORTED;
2254 }
2255
2256 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
2257
2258 if((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {
2259 //
2260 // Query is Volatile related.
2261 //
2262 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);
2263 } else {
2264 //
2265 // Query is Non-Volatile related.
2266 //
2267 VariableStoreHeader = mNvVariableCache;
2268 }
2269
2270 //
2271 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
2272 // with the storage size (excluding the storage header size).
2273 //
2274 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);
2275
2276 //
2277 // Harware error record variable needs larger size.
2278 //
2279 if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {
2280 *MaximumVariableStorageSize = PcdGet32 (PcdHwErrStorageSize);
2281 *MaximumVariableSize = PcdGet32 (PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER);
2282 } else {
2283 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {
2284 ASSERT (PcdGet32 (PcdHwErrStorageSize) < VariableStoreHeader->Size);
2285 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize);
2286 }
2287
2288 //
2289 // Let *MaximumVariableSize be PcdGet32 (PcdMaxVariableSize) with the exception of the variable header size.
2290 //
2291 *MaximumVariableSize = PcdGet32 (PcdMaxVariableSize) - sizeof (VARIABLE_HEADER);
2292 }
2293
2294 //
2295 // Point to the starting address of the variables.
2296 //
2297 Variable = GetStartPointer (VariableStoreHeader);
2298
2299 //
2300 // Now walk through the related variable store.
2301 //
2302 while ((Variable < GetEndPointer (VariableStoreHeader)) && IsValidVariableHeader (Variable)) {
2303 NextVariable = GetNextVariablePtr (Variable);
2304 VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;
2305
2306 if (AtRuntime ()) {
2307 //
2308 // We don't take the state of the variables in mind
2309 // when calculating RemainingVariableStorageSize,
2310 // since the space occupied by variables not marked with
2311 // VAR_ADDED is not allowed to be reclaimed in Runtime.
2312 //
2313 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
2314 HwErrVariableTotalSize += VariableSize;
2315 } else {
2316 CommonVariableTotalSize += VariableSize;
2317 }
2318 } else {
2319 //
2320 // Only care about Variables with State VAR_ADDED, because
2321 // the space not marked as VAR_ADDED is reclaimable now.
2322 //
2323 if (Variable->State == VAR_ADDED) {
2324 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
2325 HwErrVariableTotalSize += VariableSize;
2326 } else {
2327 CommonVariableTotalSize += VariableSize;
2328 }
2329 }
2330 }
2331
2332 //
2333 // Go to the next one.
2334 //
2335 Variable = NextVariable;
2336 }
2337
2338 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD){
2339 *RemainingVariableStorageSize = *MaximumVariableStorageSize - HwErrVariableTotalSize;
2340 }else {
2341 *RemainingVariableStorageSize = *MaximumVariableStorageSize - CommonVariableTotalSize;
2342 }
2343
2344 if (*RemainingVariableStorageSize < sizeof (VARIABLE_HEADER)) {
2345 *MaximumVariableSize = 0;
2346 } else if ((*RemainingVariableStorageSize - sizeof (VARIABLE_HEADER)) < *MaximumVariableSize) {
2347 *MaximumVariableSize = *RemainingVariableStorageSize - sizeof (VARIABLE_HEADER);
2348 }
2349
2350 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
2351 return EFI_SUCCESS;
2352 }
2353
2354
2355 /**
2356 This function reclaims variable storage if free size is below the threshold.
2357
2358 **/
2359 VOID
2360 ReclaimForOS(
2361 VOID
2362 )
2363 {
2364 EFI_STATUS Status;
2365 UINTN CommonVariableSpace;
2366 UINTN RemainingCommonVariableSpace;
2367 UINTN RemainingHwErrVariableSpace;
2368
2369 Status = EFI_SUCCESS;
2370
2371 CommonVariableSpace = ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase)))->Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32(PcdHwErrStorageSize); //Allowable max size of common variable storage space
2372
2373 RemainingCommonVariableSpace = CommonVariableSpace - mVariableModuleGlobal->CommonVariableTotalSize;
2374
2375 RemainingHwErrVariableSpace = PcdGet32 (PcdHwErrStorageSize) - mVariableModuleGlobal->HwErrVariableTotalSize;
2376 //
2377 // Check if the free area is blow a threshold.
2378 //
2379 if ((RemainingCommonVariableSpace < PcdGet32 (PcdMaxVariableSize))
2380 || ((PcdGet32 (PcdHwErrStorageSize) != 0) &&
2381 (RemainingHwErrVariableSpace < PcdGet32 (PcdMaxHardwareErrorVariableSize)))){
2382 Status = Reclaim (
2383 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,
2384 &mVariableModuleGlobal->NonVolatileLastVariableOffset,
2385 FALSE,
2386 NULL
2387 );
2388 ASSERT_EFI_ERROR (Status);
2389 }
2390 }
2391
2392
2393 /**
2394 Initializes variable write service after FVB was ready.
2395
2396 @retval EFI_SUCCESS Function successfully executed.
2397 @retval Others Fail to initialize the variable service.
2398
2399 **/
2400 EFI_STATUS
2401 VariableWriteServiceInitialize (
2402 VOID
2403 )
2404 {
2405 EFI_STATUS Status;
2406 VARIABLE_STORE_HEADER *VariableStoreHeader;
2407 UINTN Index;
2408 UINT8 Data;
2409 EFI_PHYSICAL_ADDRESS VariableStoreBase;
2410 VARIABLE_HEADER *Variable;
2411 VOID *VariableData;
2412
2413 VariableStoreBase = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;
2414 VariableStoreHeader = (VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase;
2415
2416 //
2417 // Check if the free area is really free.
2418 //
2419 for (Index = mVariableModuleGlobal->NonVolatileLastVariableOffset; Index < VariableStoreHeader->Size; Index++) {
2420 Data = ((UINT8 *) mNvVariableCache)[Index];
2421 if (Data != 0xff) {
2422 //
2423 // There must be something wrong in variable store, do reclaim operation.
2424 //
2425 Status = Reclaim (
2426 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,
2427 &mVariableModuleGlobal->NonVolatileLastVariableOffset,
2428 FALSE,
2429 NULL
2430 );
2431 if (EFI_ERROR (Status)) {
2432 return Status;
2433 }
2434 break;
2435 }
2436 }
2437
2438 //
2439 // Flush the HOB variable to flash and invalidate HOB variable.
2440 //
2441 if (mVariableModuleGlobal->VariableGlobal.HobVariableBase != 0) {
2442 //
2443 // Clear the HobVariableBase to avoid SetVariable() updating the variable in HOB
2444 //
2445 VariableStoreHeader = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;
2446 mVariableModuleGlobal->VariableGlobal.HobVariableBase = 0;
2447
2448 for ( Variable = GetStartPointer (VariableStoreHeader)
2449 ; (Variable < GetEndPointer (VariableStoreHeader) && IsValidVariableHeader (Variable))
2450 ; Variable = GetNextVariablePtr (Variable)
2451 ) {
2452 ASSERT (Variable->State == VAR_ADDED);
2453 ASSERT ((Variable->Attributes & EFI_VARIABLE_NON_VOLATILE) != 0);
2454 VariableData = GetVariableDataPtr (Variable);
2455 Status = VariableServiceSetVariable (
2456 GetVariableNamePtr (Variable),
2457 &Variable->VendorGuid,
2458 Variable->Attributes,
2459 Variable->DataSize,
2460 VariableData
2461 );
2462 ASSERT_EFI_ERROR (Status);
2463 }
2464 }
2465 return EFI_SUCCESS;
2466 }
2467
2468
2469 /**
2470 Initializes variable store area for non-volatile and volatile variable.
2471
2472 @retval EFI_SUCCESS Function successfully executed.
2473 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
2474
2475 **/
2476 EFI_STATUS
2477 VariableCommonInitialize (
2478 VOID
2479 )
2480 {
2481 EFI_STATUS Status;
2482 VARIABLE_STORE_HEADER *VolatileVariableStore;
2483 VARIABLE_STORE_HEADER *VariableStoreHeader;
2484 VARIABLE_HEADER *NextVariable;
2485 EFI_PHYSICAL_ADDRESS TempVariableStoreHeader;
2486 EFI_PHYSICAL_ADDRESS VariableStoreBase;
2487 UINT64 VariableStoreLength;
2488 UINTN ScratchSize;
2489 UINTN VariableSize;
2490 EFI_HOB_GUID_TYPE *GuidHob;
2491
2492 //
2493 // Allocate runtime memory for variable driver global structure.
2494 //
2495 mVariableModuleGlobal = AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL));
2496 if (mVariableModuleGlobal == NULL) {
2497 return EFI_OUT_OF_RESOURCES;
2498 }
2499
2500 InitializeLock (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock, TPL_NOTIFY);
2501
2502 //
2503 // Note that in EdkII variable driver implementation, Hardware Error Record type variable
2504 // is stored with common variable in the same NV region. So the platform integrator should
2505 // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of
2506 // PcdFlashNvStorageVariableSize.
2507 //
2508 ASSERT (PcdGet32 (PcdHwErrStorageSize) <= PcdGet32 (PcdFlashNvStorageVariableSize));
2509
2510 //
2511 // Get HOB variable store.
2512 //
2513 GuidHob = GetFirstGuidHob (&gEfiVariableGuid);
2514 if (GuidHob != NULL) {
2515 VariableStoreHeader = GET_GUID_HOB_DATA (GuidHob);
2516 if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) {
2517 mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VariableStoreHeader;
2518 } else {
2519 DEBUG ((EFI_D_ERROR, "HOB Variable Store header is corrupted!\n"));
2520 }
2521 }
2522
2523 //
2524 // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.
2525 //
2526 ScratchSize = MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize));
2527 VolatileVariableStore = AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize) + ScratchSize);
2528 if (VolatileVariableStore == NULL) {
2529 FreePool (mVariableModuleGlobal);
2530 return EFI_OUT_OF_RESOURCES;
2531 }
2532
2533 SetMem (VolatileVariableStore, PcdGet32 (PcdVariableStoreSize) + ScratchSize, 0xff);
2534
2535 //
2536 // Initialize Variable Specific Data.
2537 //
2538 mVariableModuleGlobal->VariableGlobal.VolatileVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VolatileVariableStore;
2539 mVariableModuleGlobal->VolatileLastVariableOffset = (UINTN) GetStartPointer (VolatileVariableStore) - (UINTN) VolatileVariableStore;
2540 mVariableModuleGlobal->FvbInstance = NULL;
2541
2542 CopyGuid (&VolatileVariableStore->Signature, &gEfiVariableGuid);
2543 VolatileVariableStore->Size = PcdGet32 (PcdVariableStoreSize);
2544 VolatileVariableStore->Format = VARIABLE_STORE_FORMATTED;
2545 VolatileVariableStore->State = VARIABLE_STORE_HEALTHY;
2546 VolatileVariableStore->Reserved = 0;
2547 VolatileVariableStore->Reserved1 = 0;
2548
2549 //
2550 // Get non-volatile variable store.
2551 //
2552
2553 TempVariableStoreHeader = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);
2554 if (TempVariableStoreHeader == 0) {
2555 TempVariableStoreHeader = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);
2556 }
2557
2558 //
2559 // Check if the Firmware Volume is not corrupted
2560 //
2561 if ((((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(TempVariableStoreHeader))->Signature != EFI_FVH_SIGNATURE) ||
2562 (!CompareGuid (&gEfiSystemNvDataFvGuid, &((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(TempVariableStoreHeader))->FileSystemGuid))) {
2563 Status = EFI_VOLUME_CORRUPTED;
2564 DEBUG ((EFI_D_ERROR, "Firmware Volume for Variable Store is corrupted\n"));
2565 goto Done;
2566 }
2567
2568 VariableStoreBase = TempVariableStoreHeader + \
2569 (((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(TempVariableStoreHeader)) -> HeaderLength);
2570 VariableStoreLength = (UINT64) PcdGet32 (PcdFlashNvStorageVariableSize) - \
2571 (((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(TempVariableStoreHeader)) -> HeaderLength);
2572
2573 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase;
2574 VariableStoreHeader = (VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase;
2575 if (GetVariableStoreStatus (VariableStoreHeader) != EfiValid) {
2576 Status = EFI_VOLUME_CORRUPTED;
2577 DEBUG((EFI_D_INFO, "Variable Store header is corrupted\n"));
2578 goto Done;
2579 }
2580 ASSERT(VariableStoreHeader->Size == VariableStoreLength);
2581
2582 //
2583 // Parse non-volatile variable data and get last variable offset.
2584 //
2585 NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase);
2586 while (IsValidVariableHeader (NextVariable)) {
2587 VariableSize = NextVariable->NameSize + NextVariable->DataSize + sizeof (VARIABLE_HEADER);
2588 if ((NextVariable->Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {
2589 mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VariableSize);
2590 } else {
2591 mVariableModuleGlobal->CommonVariableTotalSize += HEADER_ALIGN (VariableSize);
2592 }
2593
2594 NextVariable = GetNextVariablePtr (NextVariable);
2595 }
2596
2597 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) VariableStoreBase;
2598
2599 //
2600 // Allocate runtime memory used for a memory copy of the FLASH region.
2601 // Keep the memory and the FLASH in sync as updates occur
2602 //
2603 mNvVariableCache = AllocateRuntimeZeroPool ((UINTN)VariableStoreLength);
2604 if (mNvVariableCache == NULL) {
2605 Status = EFI_OUT_OF_RESOURCES;
2606 goto Done;
2607 }
2608 CopyMem (mNvVariableCache, (CHAR8 *)(UINTN)VariableStoreBase, (UINTN)VariableStoreLength);
2609 Status = EFI_SUCCESS;
2610
2611 Done:
2612 if (EFI_ERROR (Status)) {
2613 FreePool (mVariableModuleGlobal);
2614 FreePool (VolatileVariableStore);
2615 }
2616
2617 return Status;
2618 }
2619
2620
2621 /**
2622 Get the proper fvb handle and/or fvb protocol by the given Flash address.
2623
2624 @param[in] Address The Flash address.
2625 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.
2626 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.
2627
2628 **/
2629 EFI_STATUS
2630 GetFvbInfoByAddress (
2631 IN EFI_PHYSICAL_ADDRESS Address,
2632 OUT EFI_HANDLE *FvbHandle OPTIONAL,
2633 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvbProtocol OPTIONAL
2634 )
2635 {
2636 EFI_STATUS Status;
2637 EFI_HANDLE *HandleBuffer;
2638 UINTN HandleCount;
2639 UINTN Index;
2640 EFI_PHYSICAL_ADDRESS FvbBaseAddress;
2641 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;
2642 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
2643 EFI_FVB_ATTRIBUTES_2 Attributes;
2644
2645 //
2646 // Get all FVB handles.
2647 //
2648 Status = GetFvbCountAndBuffer (&HandleCount, &HandleBuffer);
2649 if (EFI_ERROR (Status)) {
2650 return EFI_NOT_FOUND;
2651 }
2652
2653 //
2654 // Get the FVB to access variable store.
2655 //
2656 Fvb = NULL;
2657 for (Index = 0; Index < HandleCount; Index += 1, Status = EFI_NOT_FOUND, Fvb = NULL) {
2658 Status = GetFvbByHandle (HandleBuffer[Index], &Fvb);
2659 if (EFI_ERROR (Status)) {
2660 Status = EFI_NOT_FOUND;
2661 break;
2662 }
2663
2664 //
2665 // Ensure this FVB protocol supported Write operation.
2666 //
2667 Status = Fvb->GetAttributes (Fvb, &Attributes);
2668 if (EFI_ERROR (Status) || ((Attributes & EFI_FVB2_WRITE_STATUS) == 0)) {
2669 continue;
2670 }
2671
2672 //
2673 // Compare the address and select the right one.
2674 //
2675 Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);
2676 if (EFI_ERROR (Status)) {
2677 continue;
2678 }
2679
2680 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvbBaseAddress);
2681 if ((Address >= FvbBaseAddress) && (Address < (FvbBaseAddress + FwVolHeader->FvLength))) {
2682 if (FvbHandle != NULL) {
2683 *FvbHandle = HandleBuffer[Index];
2684 }
2685 if (FvbProtocol != NULL) {
2686 *FvbProtocol = Fvb;
2687 }
2688 Status = EFI_SUCCESS;
2689 break;
2690 }
2691 }
2692 FreePool (HandleBuffer);
2693
2694 if (Fvb == NULL) {
2695 Status = EFI_NOT_FOUND;
2696 }
2697
2698 return Status;
2699 }
2700