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