]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
Sync EDKII BaseTools to BaseTools project r2100.
[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 VariableGetBestLanguage (
1252 IN CONST CHAR8 *SupportedLanguages,
1253 IN BOOLEAN Iso639Language,
1254 ...
1255 )
1256 {
1257 VA_LIST Args;
1258 CHAR8 *Language;
1259 UINTN CompareLength;
1260 UINTN LanguageLength;
1261 CONST CHAR8 *Supported;
1262 CHAR8 *Buffer;
1263
1264 ASSERT (SupportedLanguages != NULL);
1265
1266 VA_START (Args, Iso639Language);
1267 while ((Language = VA_ARG (Args, CHAR8 *)) != NULL) {
1268 //
1269 // Default to ISO 639-2 mode
1270 //
1271 CompareLength = 3;
1272 LanguageLength = MIN (3, AsciiStrLen (Language));
1273
1274 //
1275 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language
1276 //
1277 if (!Iso639Language) {
1278 for (LanguageLength = 0; Language[LanguageLength] != 0 && Language[LanguageLength] != ';'; LanguageLength++);
1279 }
1280
1281 //
1282 // Trim back the length of Language used until it is empty
1283 //
1284 while (LanguageLength > 0) {
1285 //
1286 // Loop through all language codes in SupportedLanguages
1287 //
1288 for (Supported = SupportedLanguages; *Supported != '\0'; Supported += CompareLength) {
1289 //
1290 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages
1291 //
1292 if (!Iso639Language) {
1293 //
1294 // Skip ';' characters in Supported
1295 //
1296 for (; *Supported != '\0' && *Supported == ';'; Supported++);
1297 //
1298 // Determine the length of the next language code in Supported
1299 //
1300 for (CompareLength = 0; Supported[CompareLength] != 0 && Supported[CompareLength] != ';'; CompareLength++);
1301 //
1302 // If Language is longer than the Supported, then skip to the next language
1303 //
1304 if (LanguageLength > CompareLength) {
1305 continue;
1306 }
1307 }
1308 //
1309 // See if the first LanguageLength characters in Supported match Language
1310 //
1311 if (AsciiStrnCmp (Supported, Language, LanguageLength) == 0) {
1312 VA_END (Args);
1313
1314 Buffer = Iso639Language ? mVariableModuleGlobal->Lang : mVariableModuleGlobal->PlatformLang;
1315 Buffer[CompareLength] = '\0';
1316 return CopyMem (Buffer, Supported, CompareLength);
1317 }
1318 }
1319
1320 if (Iso639Language) {
1321 //
1322 // If ISO 639 mode, then each language can only be tested once
1323 //
1324 LanguageLength = 0;
1325 } else {
1326 //
1327 // If RFC 4646 mode, then trim Language from the right to the next '-' character
1328 //
1329 for (LanguageLength--; LanguageLength > 0 && Language[LanguageLength] != '-'; LanguageLength--);
1330 }
1331 }
1332 }
1333 VA_END (Args);
1334
1335 //
1336 // No matches were found
1337 //
1338 return NULL;
1339 }
1340
1341 /**
1342 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.
1343
1344 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.
1345
1346 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,
1347 and are read-only. Therefore, in variable driver, only store the original value for other use.
1348
1349 @param[in] VariableName Name of variable
1350
1351 @param[in] Data Variable data
1352
1353 @param[in] DataSize Size of data. 0 means delete
1354
1355 **/
1356 VOID
1357 AutoUpdateLangVariable(
1358 IN CHAR16 *VariableName,
1359 IN VOID *Data,
1360 IN UINTN DataSize
1361 )
1362 {
1363 EFI_STATUS Status;
1364 CHAR8 *BestPlatformLang;
1365 CHAR8 *BestLang;
1366 UINTN Index;
1367 UINT32 Attributes;
1368 VARIABLE_POINTER_TRACK Variable;
1369 BOOLEAN SetLanguageCodes;
1370
1371 //
1372 // Don't do updates for delete operation
1373 //
1374 if (DataSize == 0) {
1375 return;
1376 }
1377
1378 SetLanguageCodes = FALSE;
1379
1380 if (StrCmp (VariableName, L"PlatformLangCodes") == 0) {
1381 //
1382 // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.
1383 //
1384 if (EfiAtRuntime ()) {
1385 return;
1386 }
1387
1388 SetLanguageCodes = TRUE;
1389
1390 //
1391 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only
1392 // Therefore, in variable driver, only store the original value for other use.
1393 //
1394 if (mVariableModuleGlobal->PlatformLangCodes != NULL) {
1395 FreePool (mVariableModuleGlobal->PlatformLangCodes);
1396 }
1397 mVariableModuleGlobal->PlatformLangCodes = AllocateRuntimeCopyPool (DataSize, Data);
1398 ASSERT (mVariableModuleGlobal->PlatformLangCodes != NULL);
1399
1400 //
1401 // PlatformLang holds a single language from PlatformLangCodes,
1402 // so the size of PlatformLangCodes is enough for the PlatformLang.
1403 //
1404 if (mVariableModuleGlobal->PlatformLang != NULL) {
1405 FreePool (mVariableModuleGlobal->PlatformLang);
1406 }
1407 mVariableModuleGlobal->PlatformLang = AllocateRuntimePool (DataSize);
1408 ASSERT (mVariableModuleGlobal->PlatformLang != NULL);
1409
1410 } else if (StrCmp (VariableName, L"LangCodes") == 0) {
1411 //
1412 // LangCodes is a volatile variable, so it can not be updated at runtime.
1413 //
1414 if (EfiAtRuntime ()) {
1415 return;
1416 }
1417
1418 SetLanguageCodes = TRUE;
1419
1420 //
1421 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only
1422 // Therefore, in variable driver, only store the original value for other use.
1423 //
1424 if (mVariableModuleGlobal->LangCodes != NULL) {
1425 FreePool (mVariableModuleGlobal->LangCodes);
1426 }
1427 mVariableModuleGlobal->LangCodes = AllocateRuntimeCopyPool (DataSize, Data);
1428 ASSERT (mVariableModuleGlobal->LangCodes != NULL);
1429 }
1430
1431 if (SetLanguageCodes
1432 && (mVariableModuleGlobal->PlatformLangCodes != NULL)
1433 && (mVariableModuleGlobal->LangCodes != NULL)) {
1434 //
1435 // Update Lang if PlatformLang is already set
1436 // Update PlatformLang if Lang is already set
1437 //
1438 Status = FindVariable (L"PlatformLang", &gEfiGlobalVariableGuid, &Variable, (VARIABLE_GLOBAL *) mVariableModuleGlobal);
1439 if (!EFI_ERROR (Status)) {
1440 //
1441 // Update Lang
1442 //
1443 VariableName = L"PlatformLang";
1444 Data = GetVariableDataPtr (Variable.CurrPtr);
1445 DataSize = Variable.CurrPtr->DataSize;
1446 } else {
1447 Status = FindVariable (L"Lang", &gEfiGlobalVariableGuid, &Variable, (VARIABLE_GLOBAL *) mVariableModuleGlobal);
1448 if (!EFI_ERROR (Status)) {
1449 //
1450 // Update PlatformLang
1451 //
1452 VariableName = L"Lang";
1453 Data = GetVariableDataPtr (Variable.CurrPtr);
1454 DataSize = Variable.CurrPtr->DataSize;
1455 } else {
1456 //
1457 // Neither PlatformLang nor Lang is set, directly return
1458 //
1459 return;
1460 }
1461 }
1462 }
1463
1464 //
1465 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.
1466 //
1467 Attributes = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;
1468
1469 if (StrCmp (VariableName, L"PlatformLang") == 0) {
1470 //
1471 // Update Lang when PlatformLangCodes/LangCodes were set.
1472 //
1473 if ((mVariableModuleGlobal->PlatformLangCodes != NULL) && (mVariableModuleGlobal->LangCodes != NULL)) {
1474 //
1475 // When setting PlatformLang, firstly get most matched language string from supported language codes.
1476 //
1477 BestPlatformLang = VariableGetBestLanguage (mVariableModuleGlobal->PlatformLangCodes, FALSE, Data, NULL);
1478 if (BestPlatformLang != NULL) {
1479 //
1480 // Get the corresponding index in language codes.
1481 //
1482 Index = GetIndexFromSupportedLangCodes (mVariableModuleGlobal->PlatformLangCodes, BestPlatformLang, FALSE);
1483
1484 //
1485 // Get the corresponding ISO639 language tag according to RFC4646 language tag.
1486 //
1487 BestLang = GetLangFromSupportedLangCodes (mVariableModuleGlobal->LangCodes, Index, TRUE);
1488
1489 //
1490 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.
1491 //
1492 FindVariable (L"Lang", &gEfiGlobalVariableGuid, &Variable, (VARIABLE_GLOBAL *)mVariableModuleGlobal);
1493
1494 Status = UpdateVariable (L"Lang", &gEfiGlobalVariableGuid, BestLang, ISO_639_2_ENTRY_SIZE + 1, Attributes, &Variable);
1495
1496 DEBUG ((EFI_D_INFO, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a\n", BestPlatformLang, BestLang));
1497
1498 ASSERT_EFI_ERROR(Status);
1499 }
1500 }
1501
1502 } else if (StrCmp (VariableName, L"Lang") == 0) {
1503 //
1504 // Update PlatformLang when PlatformLangCodes/LangCodes were set.
1505 //
1506 if ((mVariableModuleGlobal->PlatformLangCodes != NULL) && (mVariableModuleGlobal->LangCodes != NULL)) {
1507 //
1508 // When setting Lang, firstly get most matched language string from supported language codes.
1509 //
1510 BestLang = VariableGetBestLanguage (mVariableModuleGlobal->LangCodes, TRUE, Data, NULL);
1511 if (BestLang != NULL) {
1512 //
1513 // Get the corresponding index in language codes.
1514 //
1515 Index = GetIndexFromSupportedLangCodes (mVariableModuleGlobal->LangCodes, BestLang, TRUE);
1516
1517 //
1518 // Get the corresponding RFC4646 language tag according to ISO639 language tag.
1519 //
1520 BestPlatformLang = GetLangFromSupportedLangCodes (mVariableModuleGlobal->PlatformLangCodes, Index, FALSE);
1521
1522 //
1523 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.
1524 //
1525 FindVariable (L"PlatformLang", &gEfiGlobalVariableGuid, &Variable, (VARIABLE_GLOBAL *)mVariableModuleGlobal);
1526
1527 Status = UpdateVariable (L"PlatformLang", &gEfiGlobalVariableGuid, BestPlatformLang,
1528 AsciiStrSize (BestPlatformLang), Attributes, &Variable);
1529
1530 DEBUG ((EFI_D_INFO, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a\n", BestLang, BestPlatformLang));
1531 ASSERT_EFI_ERROR (Status);
1532 }
1533 }
1534 }
1535 }
1536
1537 /**
1538 Update the variable region with Variable information. These are the same
1539 arguments as the EFI Variable services.
1540
1541 @param[in] VariableName Name of variable
1542
1543 @param[in] VendorGuid Guid of variable
1544
1545 @param[in] Data Variable data
1546
1547 @param[in] DataSize Size of data. 0 means delete
1548
1549 @param[in] Attributes Attribues of the variable
1550
1551 @param[in] Variable The variable information which is used to keep track of variable usage.
1552
1553 @retval EFI_SUCCESS The update operation is success.
1554
1555 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.
1556
1557 **/
1558 EFI_STATUS
1559 EFIAPI
1560 UpdateVariable (
1561 IN CHAR16 *VariableName,
1562 IN EFI_GUID *VendorGuid,
1563 IN VOID *Data,
1564 IN UINTN DataSize,
1565 IN UINT32 Attributes OPTIONAL,
1566 IN VARIABLE_POINTER_TRACK *Variable
1567 )
1568 {
1569 EFI_STATUS Status;
1570 VARIABLE_HEADER *NextVariable;
1571 UINTN ScratchSize;
1572 UINTN NonVolatileVarableStoreSize;
1573 UINTN VarNameOffset;
1574 UINTN VarDataOffset;
1575 UINTN VarNameSize;
1576 UINTN VarSize;
1577 BOOLEAN Volatile;
1578 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;
1579 UINT8 State;
1580 BOOLEAN Reclaimed;
1581
1582 Fvb = mVariableModuleGlobal->FvbInstance;
1583 Reclaimed = FALSE;
1584
1585 if (Variable->CurrPtr != NULL) {
1586 //
1587 // Update/Delete existing variable
1588 //
1589 Volatile = Variable->Volatile;
1590
1591 if (EfiAtRuntime ()) {
1592 //
1593 // If EfiAtRuntime and the variable is Volatile and Runtime Access,
1594 // the volatile is ReadOnly, and SetVariable should be aborted and
1595 // return EFI_WRITE_PROTECTED.
1596 //
1597 if (Variable->Volatile) {
1598 Status = EFI_WRITE_PROTECTED;
1599 goto Done;
1600 }
1601 //
1602 // Only variable have NV attribute can be updated/deleted in Runtime
1603 //
1604 if ((Variable->CurrPtr->Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {
1605 Status = EFI_INVALID_PARAMETER;
1606 goto Done;
1607 }
1608 }
1609 //
1610 // Setting a data variable with no access, or zero DataSize attributes
1611 // specified causes it to be deleted.
1612 //
1613 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {
1614 State = Variable->CurrPtr->State;
1615 State &= VAR_DELETED;
1616
1617 Status = UpdateVariableStore (
1618 &mVariableModuleGlobal->VariableGlobal,
1619 Variable->Volatile,
1620 FALSE,
1621 Fvb,
1622 (UINTN) &Variable->CurrPtr->State,
1623 sizeof (UINT8),
1624 &State
1625 );
1626 if (!EFI_ERROR (Status)) {
1627 UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, FALSE, TRUE, FALSE);
1628 UpdateVariableCache (VariableName, VendorGuid, Attributes, DataSize, Data);
1629 }
1630 goto Done;
1631 }
1632 //
1633 // If the variable is marked valid and the same data has been passed in
1634 // then return to the caller immediately.
1635 //
1636 if (DataSizeOfVariable (Variable->CurrPtr) == DataSize &&
1637 (CompareMem (Data, GetVariableDataPtr (Variable->CurrPtr), DataSize) == 0)) {
1638
1639 UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);
1640 Status = EFI_SUCCESS;
1641 goto Done;
1642 } else if ((Variable->CurrPtr->State == VAR_ADDED) ||
1643 (Variable->CurrPtr->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {
1644
1645 //
1646 // Mark the old variable as in delete transition
1647 //
1648 State = Variable->CurrPtr->State;
1649 State &= VAR_IN_DELETED_TRANSITION;
1650
1651 Status = UpdateVariableStore (
1652 &mVariableModuleGlobal->VariableGlobal,
1653 Variable->Volatile,
1654 FALSE,
1655 Fvb,
1656 (UINTN) &Variable->CurrPtr->State,
1657 sizeof (UINT8),
1658 &State
1659 );
1660 if (EFI_ERROR (Status)) {
1661 goto Done;
1662 }
1663 }
1664 } else {
1665 //
1666 // Not found existing variable. Create a new variable
1667 //
1668
1669 //
1670 // Make sure we are trying to create a new variable.
1671 // Setting a data variable with no access, or zero DataSize attributes means to delete it.
1672 //
1673 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {
1674 Status = EFI_NOT_FOUND;
1675 goto Done;
1676 }
1677
1678 //
1679 // Only variable have NV|RT attribute can be created in Runtime
1680 //
1681 if (EfiAtRuntime () &&
1682 (((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) || ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0))) {
1683 Status = EFI_INVALID_PARAMETER;
1684 goto Done;
1685 }
1686 }
1687
1688 //
1689 // Function part - create a new variable and copy the data.
1690 // Both update a variable and create a variable will come here.
1691 //
1692 // Tricky part: Use scratch data area at the end of volatile variable store
1693 // as a temporary storage.
1694 //
1695 NextVariable = GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase));
1696 ScratchSize = MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize));
1697
1698 SetMem (NextVariable, ScratchSize, 0xff);
1699
1700 NextVariable->StartId = VARIABLE_DATA;
1701 NextVariable->Attributes = Attributes;
1702 //
1703 // NextVariable->State = VAR_ADDED;
1704 //
1705 NextVariable->Reserved = 0;
1706 VarNameOffset = sizeof (VARIABLE_HEADER);
1707 VarNameSize = StrSize (VariableName);
1708 CopyMem (
1709 (UINT8 *) ((UINTN) NextVariable + VarNameOffset),
1710 VariableName,
1711 VarNameSize
1712 );
1713 VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);
1714 CopyMem (
1715 (UINT8 *) ((UINTN) NextVariable + VarDataOffset),
1716 Data,
1717 DataSize
1718 );
1719 CopyMem (&NextVariable->VendorGuid, VendorGuid, sizeof (EFI_GUID));
1720 //
1721 // There will be pad bytes after Data, the NextVariable->NameSize and
1722 // NextVariable->DataSize should not include pad size so that variable
1723 // service can get actual size in GetVariable
1724 //
1725 NextVariable->NameSize = (UINT32)VarNameSize;
1726 NextVariable->DataSize = (UINT32)DataSize;
1727
1728 //
1729 // The actual size of the variable that stores in storage should
1730 // include pad size.
1731 //
1732 VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);
1733 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {
1734 //
1735 // Create a nonvolatile variable
1736 //
1737 Volatile = FALSE;
1738 NonVolatileVarableStoreSize = ((VARIABLE_STORE_HEADER *)(UINTN)(mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase))->Size;
1739 if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0)
1740 && ((VarSize + mVariableModuleGlobal->HwErrVariableTotalSize) > PcdGet32 (PcdHwErrStorageSize)))
1741 || (((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0)
1742 && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > NonVolatileVarableStoreSize - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize)))) {
1743 if (EfiAtRuntime ()) {
1744 Status = EFI_OUT_OF_RESOURCES;
1745 goto Done;
1746 }
1747 //
1748 // Perform garbage collection & reclaim operation
1749 //
1750 Status = Reclaim (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,
1751 &mVariableModuleGlobal->NonVolatileLastVariableOffset, FALSE, Variable->CurrPtr);
1752 if (EFI_ERROR (Status)) {
1753 goto Done;
1754 }
1755 //
1756 // If still no enough space, return out of resources
1757 //
1758 if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0)
1759 && ((VarSize + mVariableModuleGlobal->HwErrVariableTotalSize) > PcdGet32 (PcdHwErrStorageSize)))
1760 || (((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0)
1761 && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > NonVolatileVarableStoreSize - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize)))) {
1762 Status = EFI_OUT_OF_RESOURCES;
1763 goto Done;
1764 }
1765 Reclaimed = TRUE;
1766 }
1767 //
1768 // Three steps
1769 // 1. Write variable header
1770 // 2. Set variable state to header valid
1771 // 3. Write variable data
1772 // 4. Set variable state to valid
1773 //
1774 //
1775 // Step 1:
1776 //
1777 Status = UpdateVariableStore (
1778 &mVariableModuleGlobal->VariableGlobal,
1779 FALSE,
1780 TRUE,
1781 Fvb,
1782 mVariableModuleGlobal->NonVolatileLastVariableOffset,
1783 sizeof (VARIABLE_HEADER),
1784 (UINT8 *) NextVariable
1785 );
1786
1787 if (EFI_ERROR (Status)) {
1788 goto Done;
1789 }
1790
1791 //
1792 // Step 2:
1793 //
1794 NextVariable->State = VAR_HEADER_VALID_ONLY;
1795 Status = UpdateVariableStore (
1796 &mVariableModuleGlobal->VariableGlobal,
1797 FALSE,
1798 TRUE,
1799 Fvb,
1800 mVariableModuleGlobal->NonVolatileLastVariableOffset,
1801 sizeof (VARIABLE_HEADER),
1802 (UINT8 *) NextVariable
1803 );
1804
1805 if (EFI_ERROR (Status)) {
1806 goto Done;
1807 }
1808 //
1809 // Step 3:
1810 //
1811 Status = UpdateVariableStore (
1812 &mVariableModuleGlobal->VariableGlobal,
1813 FALSE,
1814 TRUE,
1815 Fvb,
1816 mVariableModuleGlobal->NonVolatileLastVariableOffset + sizeof (VARIABLE_HEADER),
1817 (UINT32) VarSize - sizeof (VARIABLE_HEADER),
1818 (UINT8 *) NextVariable + sizeof (VARIABLE_HEADER)
1819 );
1820
1821 if (EFI_ERROR (Status)) {
1822 goto Done;
1823 }
1824 //
1825 // Step 4:
1826 //
1827 NextVariable->State = VAR_ADDED;
1828 Status = UpdateVariableStore (
1829 &mVariableModuleGlobal->VariableGlobal,
1830 FALSE,
1831 TRUE,
1832 Fvb,
1833 mVariableModuleGlobal->NonVolatileLastVariableOffset,
1834 sizeof (VARIABLE_HEADER),
1835 (UINT8 *) NextVariable
1836 );
1837
1838 if (EFI_ERROR (Status)) {
1839 goto Done;
1840 }
1841
1842 mVariableModuleGlobal->NonVolatileLastVariableOffset += HEADER_ALIGN (VarSize);
1843
1844 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {
1845 mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VarSize);
1846 } else {
1847 mVariableModuleGlobal->CommonVariableTotalSize += HEADER_ALIGN (VarSize);
1848 }
1849 } else {
1850 //
1851 // Create a volatile variable
1852 //
1853 Volatile = TRUE;
1854
1855 if ((UINT32) (VarSize + mVariableModuleGlobal->VolatileLastVariableOffset) >
1856 ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size) {
1857 //
1858 // Perform garbage collection & reclaim operation
1859 //
1860 Status = Reclaim (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase,
1861 &mVariableModuleGlobal->VolatileLastVariableOffset, TRUE, Variable->CurrPtr);
1862 if (EFI_ERROR (Status)) {
1863 goto Done;
1864 }
1865 //
1866 // If still no enough space, return out of resources
1867 //
1868 if ((UINT32) (VarSize + mVariableModuleGlobal->VolatileLastVariableOffset) >
1869 ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size
1870 ) {
1871 Status = EFI_OUT_OF_RESOURCES;
1872 goto Done;
1873 }
1874 Reclaimed = TRUE;
1875 }
1876
1877 NextVariable->State = VAR_ADDED;
1878 Status = UpdateVariableStore (
1879 &mVariableModuleGlobal->VariableGlobal,
1880 TRUE,
1881 TRUE,
1882 Fvb,
1883 mVariableModuleGlobal->VolatileLastVariableOffset,
1884 (UINT32) VarSize,
1885 (UINT8 *) NextVariable
1886 );
1887
1888 if (EFI_ERROR (Status)) {
1889 goto Done;
1890 }
1891
1892 mVariableModuleGlobal->VolatileLastVariableOffset += HEADER_ALIGN (VarSize);
1893 }
1894
1895 //
1896 // Mark the old variable as deleted
1897 //
1898 if (!Reclaimed && !EFI_ERROR (Status) && Variable->CurrPtr != NULL) {
1899 State = Variable->CurrPtr->State;
1900 State &= VAR_DELETED;
1901
1902 Status = UpdateVariableStore (
1903 &mVariableModuleGlobal->VariableGlobal,
1904 Variable->Volatile,
1905 FALSE,
1906 Fvb,
1907 (UINTN) &Variable->CurrPtr->State,
1908 sizeof (UINT8),
1909 &State
1910 );
1911 }
1912
1913 if (!EFI_ERROR (Status)) {
1914 UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);
1915 UpdateVariableCache (VariableName, VendorGuid, Attributes, DataSize, Data);
1916 }
1917
1918 Done:
1919 return Status;
1920 }
1921
1922 /**
1923
1924 This code finds variable in storage blocks (Volatile or Non-Volatile).
1925
1926 @param VariableName Name of Variable to be found.
1927 @param VendorGuid Variable vendor GUID.
1928 @param Attributes Attribute value of the variable found.
1929 @param DataSize Size of Data found. If size is less than the
1930 data, this value contains the required size.
1931 @param Data Data pointer.
1932
1933 @return EFI_INVALID_PARAMETER Invalid parameter
1934 @return EFI_SUCCESS Find the specified variable
1935 @return EFI_NOT_FOUND Not found
1936 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result
1937
1938 **/
1939 EFI_STATUS
1940 EFIAPI
1941 RuntimeServiceGetVariable (
1942 IN CHAR16 *VariableName,
1943 IN EFI_GUID *VendorGuid,
1944 OUT UINT32 *Attributes OPTIONAL,
1945 IN OUT UINTN *DataSize,
1946 OUT VOID *Data
1947 )
1948 {
1949 EFI_STATUS Status;
1950 VARIABLE_POINTER_TRACK Variable;
1951 UINTN VarDataSize;
1952
1953 if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {
1954 return EFI_INVALID_PARAMETER;
1955 }
1956
1957 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
1958
1959 //
1960 // Find existing variable
1961 //
1962 Status = FindVariableInCache (VariableName, VendorGuid, Attributes, DataSize, Data);
1963 if ((Status == EFI_BUFFER_TOO_SMALL) || (Status == EFI_SUCCESS)){
1964 // Hit in the Cache
1965 UpdateVariableInfo (VariableName, VendorGuid, FALSE, TRUE, FALSE, FALSE, TRUE);
1966 goto Done;
1967 }
1968
1969 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal);
1970 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {
1971 goto Done;
1972 }
1973
1974 //
1975 // Get data size
1976 //
1977 VarDataSize = DataSizeOfVariable (Variable.CurrPtr);
1978 ASSERT (VarDataSize != 0);
1979
1980 if (*DataSize >= VarDataSize) {
1981 if (Data == NULL) {
1982 Status = EFI_INVALID_PARAMETER;
1983 goto Done;
1984 }
1985
1986 CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);
1987 if (Attributes != NULL) {
1988 *Attributes = Variable.CurrPtr->Attributes;
1989 }
1990
1991 *DataSize = VarDataSize;
1992 UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, TRUE, FALSE, FALSE, FALSE);
1993 UpdateVariableCache (VariableName, VendorGuid, Variable.CurrPtr->Attributes, VarDataSize, Data);
1994
1995 Status = EFI_SUCCESS;
1996 goto Done;
1997 } else {
1998 *DataSize = VarDataSize;
1999 Status = EFI_BUFFER_TOO_SMALL;
2000 goto Done;
2001 }
2002
2003 Done:
2004 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
2005 return Status;
2006 }
2007
2008
2009
2010 /**
2011
2012 This code Finds the Next available variable.
2013
2014 @param VariableNameSize Size of the variable name
2015 @param VariableName Pointer to variable name
2016 @param VendorGuid Variable Vendor Guid
2017
2018 @return EFI_INVALID_PARAMETER Invalid parameter
2019 @return EFI_SUCCESS Find the specified variable
2020 @return EFI_NOT_FOUND Not found
2021 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result
2022
2023 **/
2024 EFI_STATUS
2025 EFIAPI
2026 RuntimeServiceGetNextVariableName (
2027 IN OUT UINTN *VariableNameSize,
2028 IN OUT CHAR16 *VariableName,
2029 IN OUT EFI_GUID *VendorGuid
2030 )
2031 {
2032 VARIABLE_POINTER_TRACK Variable;
2033 UINTN VarNameSize;
2034 EFI_STATUS Status;
2035
2036 if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {
2037 return EFI_INVALID_PARAMETER;
2038 }
2039
2040 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
2041
2042 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal);
2043 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {
2044 goto Done;
2045 }
2046
2047 if (VariableName[0] != 0) {
2048 //
2049 // If variable name is not NULL, get next variable
2050 //
2051 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
2052 }
2053
2054 while (TRUE) {
2055 //
2056 // If both volatile and non-volatile variable store are parsed,
2057 // return not found
2058 //
2059 if (Variable.CurrPtr >= Variable.EndPtr || Variable.CurrPtr == NULL) {
2060 Variable.Volatile = (BOOLEAN) (Variable.Volatile ^ ((BOOLEAN) 0x1));
2061 if (!Variable.Volatile) {
2062 Variable.StartPtr = GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);
2063 Variable.EndPtr = GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase));
2064 } else {
2065 Status = EFI_NOT_FOUND;
2066 goto Done;
2067 }
2068
2069 Variable.CurrPtr = Variable.StartPtr;
2070 if (!IsValidVariableHeader (Variable.CurrPtr)) {
2071 continue;
2072 }
2073 }
2074 //
2075 // Variable is found
2076 //
2077 if (IsValidVariableHeader (Variable.CurrPtr) && Variable.CurrPtr->State == VAR_ADDED) {
2078 if ((EfiAtRuntime () && ((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0)) == 0) {
2079 VarNameSize = NameSizeOfVariable (Variable.CurrPtr);
2080 ASSERT (VarNameSize != 0);
2081
2082 if (VarNameSize <= *VariableNameSize) {
2083 CopyMem (
2084 VariableName,
2085 GetVariableNamePtr (Variable.CurrPtr),
2086 VarNameSize
2087 );
2088 CopyMem (
2089 VendorGuid,
2090 &Variable.CurrPtr->VendorGuid,
2091 sizeof (EFI_GUID)
2092 );
2093 Status = EFI_SUCCESS;
2094 } else {
2095 Status = EFI_BUFFER_TOO_SMALL;
2096 }
2097
2098 *VariableNameSize = VarNameSize;
2099 goto Done;
2100 }
2101 }
2102
2103 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
2104 }
2105
2106 Done:
2107 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
2108 return Status;
2109 }
2110
2111 /**
2112
2113 This code sets variable in storage blocks (Volatile or Non-Volatile).
2114
2115 @param VariableName Name of Variable to be found
2116 @param VendorGuid Variable vendor GUID
2117 @param Attributes Attribute value of the variable found
2118 @param DataSize Size of Data found. If size is less than the
2119 data, this value contains the required size.
2120 @param Data Data pointer
2121
2122 @return EFI_INVALID_PARAMETER Invalid parameter
2123 @return EFI_SUCCESS Set successfully
2124 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable
2125 @return EFI_NOT_FOUND Not found
2126 @return EFI_WRITE_PROTECTED Variable is read-only
2127
2128 **/
2129 EFI_STATUS
2130 EFIAPI
2131 RuntimeServiceSetVariable (
2132 IN CHAR16 *VariableName,
2133 IN EFI_GUID *VendorGuid,
2134 IN UINT32 Attributes,
2135 IN UINTN DataSize,
2136 IN VOID *Data
2137 )
2138 {
2139 VARIABLE_POINTER_TRACK Variable;
2140 EFI_STATUS Status;
2141 VARIABLE_HEADER *NextVariable;
2142 EFI_PHYSICAL_ADDRESS Point;
2143
2144 //
2145 // Check input parameters
2146 //
2147 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {
2148 return EFI_INVALID_PARAMETER;
2149 }
2150
2151 if (DataSize != 0 && Data == NULL) {
2152 return EFI_INVALID_PARAMETER;
2153 }
2154
2155 //
2156 // Not support authenticated variable write yet.
2157 //
2158 if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {
2159 return EFI_INVALID_PARAMETER;
2160 }
2161
2162 //
2163 // Make sure if runtime bit is set, boot service bit is set also
2164 //
2165 if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {
2166 return EFI_INVALID_PARAMETER;
2167 }
2168
2169 //
2170 // The size of the VariableName, including the Unicode Null in bytes plus
2171 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)
2172 // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.
2173 //
2174 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
2175 if ((DataSize > PcdGet32 (PcdMaxHardwareErrorVariableSize)) ||
2176 (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > PcdGet32 (PcdMaxHardwareErrorVariableSize))) {
2177 return EFI_INVALID_PARAMETER;
2178 }
2179 //
2180 // According to UEFI spec, HARDWARE_ERROR_RECORD variable name convention should be L"HwErrRecXXXX"
2181 //
2182 if (StrnCmp(VariableName, L"HwErrRec", StrLen(L"HwErrRec")) != 0) {
2183 return EFI_INVALID_PARAMETER;
2184 }
2185 } else {
2186 //
2187 // The size of the VariableName, including the Unicode Null in bytes plus
2188 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxVariableSize) bytes.
2189 //
2190 if ((DataSize > PcdGet32 (PcdMaxVariableSize)) ||
2191 (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > PcdGet32 (PcdMaxVariableSize))) {
2192 return EFI_INVALID_PARAMETER;
2193 }
2194 }
2195
2196 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
2197
2198 //
2199 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated;
2200 //
2201 if (1 < InterlockedIncrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState)) {
2202 Point = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;;
2203 //
2204 // Parse non-volatile variable data and get last variable offset
2205 //
2206 NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point);
2207 while ((NextVariable < GetEndPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point))
2208 && IsValidVariableHeader (NextVariable)) {
2209 NextVariable = GetNextVariablePtr (NextVariable);
2210 }
2211 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) Point;
2212 }
2213
2214 //
2215 // Check whether the input variable is already existed
2216 //
2217 FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal);
2218
2219 //
2220 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang
2221 //
2222 AutoUpdateLangVariable (VariableName, Data, DataSize);
2223
2224 Status = UpdateVariable (VariableName, VendorGuid, Data, DataSize, Attributes, &Variable);
2225
2226 InterlockedDecrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState);
2227 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
2228
2229 return Status;
2230 }
2231
2232 /**
2233
2234 This code returns information about the EFI variables.
2235
2236 @param Attributes Attributes bitmask to specify the type of variables
2237 on which to return information.
2238 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
2239 for the EFI variables associated with the attributes specified.
2240 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
2241 for EFI variables associated with the attributes specified.
2242 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
2243 associated with the attributes specified.
2244
2245 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
2246 @return EFI_SUCCESS Query successfully.
2247 @return EFI_UNSUPPORTED The attribute is not supported on this platform.
2248
2249 **/
2250 EFI_STATUS
2251 EFIAPI
2252 RuntimeServiceQueryVariableInfo (
2253 IN UINT32 Attributes,
2254 OUT UINT64 *MaximumVariableStorageSize,
2255 OUT UINT64 *RemainingVariableStorageSize,
2256 OUT UINT64 *MaximumVariableSize
2257 )
2258 {
2259 VARIABLE_HEADER *Variable;
2260 VARIABLE_HEADER *NextVariable;
2261 UINT64 VariableSize;
2262 VARIABLE_STORE_HEADER *VariableStoreHeader;
2263 UINT64 CommonVariableTotalSize;
2264 UINT64 HwErrVariableTotalSize;
2265
2266 CommonVariableTotalSize = 0;
2267 HwErrVariableTotalSize = 0;
2268
2269 if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {
2270 return EFI_INVALID_PARAMETER;
2271 }
2272
2273 if((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == 0) {
2274 //
2275 // Make sure the Attributes combination is supported by the platform.
2276 //
2277 return EFI_UNSUPPORTED;
2278 } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {
2279 //
2280 // Make sure if runtime bit is set, boot service bit is set also.
2281 //
2282 return EFI_INVALID_PARAMETER;
2283 } else if (EfiAtRuntime () && ((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0)) {
2284 //
2285 // Make sure RT Attribute is set if we are in Runtime phase.
2286 //
2287 return EFI_INVALID_PARAMETER;
2288 } else if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
2289 //
2290 // Make sure Hw Attribute is set with NV.
2291 //
2292 return EFI_INVALID_PARAMETER;
2293 } else if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {
2294 //
2295 // Not support authentiated variable write yet.
2296 //
2297 return EFI_UNSUPPORTED;
2298 }
2299
2300 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
2301
2302 if((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {
2303 //
2304 // Query is Volatile related.
2305 //
2306 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);
2307 } else {
2308 //
2309 // Query is Non-Volatile related.
2310 //
2311 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);
2312 }
2313
2314 //
2315 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
2316 // with the storage size (excluding the storage header size).
2317 //
2318 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);
2319
2320 //
2321 // Harware error record variable needs larger size.
2322 //
2323 if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {
2324 *MaximumVariableStorageSize = PcdGet32 (PcdHwErrStorageSize);
2325 *MaximumVariableSize = PcdGet32 (PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER);
2326 } else {
2327 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {
2328 ASSERT (PcdGet32 (PcdHwErrStorageSize) < VariableStoreHeader->Size);
2329 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize);
2330 }
2331
2332 //
2333 // Let *MaximumVariableSize be PcdGet32 (PcdMaxVariableSize) with the exception of the variable header size.
2334 //
2335 *MaximumVariableSize = PcdGet32 (PcdMaxVariableSize) - sizeof (VARIABLE_HEADER);
2336 }
2337
2338 //
2339 // Point to the starting address of the variables.
2340 //
2341 Variable = GetStartPointer (VariableStoreHeader);
2342
2343 //
2344 // Now walk through the related variable store.
2345 //
2346 while ((Variable < GetEndPointer (VariableStoreHeader)) && IsValidVariableHeader (Variable)) {
2347 NextVariable = GetNextVariablePtr (Variable);
2348 VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;
2349
2350 if (EfiAtRuntime ()) {
2351 //
2352 // we don't take the state of the variables in mind
2353 // when calculating RemainingVariableStorageSize,
2354 // since the space occupied by variables not marked with
2355 // VAR_ADDED is not allowed to be reclaimed in Runtime.
2356 //
2357 if ((NextVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
2358 HwErrVariableTotalSize += VariableSize;
2359 } else {
2360 CommonVariableTotalSize += VariableSize;
2361 }
2362 } else {
2363 //
2364 // Only care about Variables with State VAR_ADDED,because
2365 // the space not marked as VAR_ADDED is reclaimable now.
2366 //
2367 if (Variable->State == VAR_ADDED) {
2368 if ((NextVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
2369 HwErrVariableTotalSize += VariableSize;
2370 } else {
2371 CommonVariableTotalSize += VariableSize;
2372 }
2373 }
2374 }
2375
2376 //
2377 // Go to the next one
2378 //
2379 Variable = NextVariable;
2380 }
2381
2382 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD){
2383 *RemainingVariableStorageSize = *MaximumVariableStorageSize - HwErrVariableTotalSize;
2384 }else {
2385 *RemainingVariableStorageSize = *MaximumVariableStorageSize - CommonVariableTotalSize;
2386 }
2387
2388 if (*RemainingVariableStorageSize < sizeof (VARIABLE_HEADER)) {
2389 *MaximumVariableSize = 0;
2390 } else if ((*RemainingVariableStorageSize - sizeof (VARIABLE_HEADER)) < *MaximumVariableSize) {
2391 *MaximumVariableSize = *RemainingVariableStorageSize - sizeof (VARIABLE_HEADER);
2392 }
2393
2394 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
2395 return EFI_SUCCESS;
2396 }
2397
2398
2399 /**
2400 Notification function of EVT_GROUP_READY_TO_BOOT event group.
2401
2402 This is a notification function registered on EVT_GROUP_READY_TO_BOOT event group.
2403 When the Boot Manager is about to load and execute a boot option, it reclaims variable
2404 storage if free size is below the threshold.
2405
2406 @param Event Event whose notification function is being invoked
2407 @param Context Pointer to the notification function's context
2408
2409 **/
2410 VOID
2411 EFIAPI
2412 ReclaimForOS(
2413 EFI_EVENT Event,
2414 VOID *Context
2415 )
2416 {
2417 EFI_STATUS Status;
2418 UINTN CommonVariableSpace;
2419 UINTN RemainingCommonVariableSpace;
2420 UINTN RemainingHwErrVariableSpace;
2421
2422 Status = EFI_SUCCESS;
2423
2424 CommonVariableSpace = ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase)))->Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32(PcdHwErrStorageSize); //Allowable max size of common variable storage space
2425
2426 RemainingCommonVariableSpace = CommonVariableSpace - mVariableModuleGlobal->CommonVariableTotalSize;
2427
2428 RemainingHwErrVariableSpace = PcdGet32 (PcdHwErrStorageSize) - mVariableModuleGlobal->HwErrVariableTotalSize;
2429 //
2430 // Check if the free area is blow a threshold.
2431 //
2432 if ((RemainingCommonVariableSpace < PcdGet32 (PcdMaxVariableSize))
2433 || ((PcdGet32 (PcdHwErrStorageSize) != 0) &&
2434 (RemainingHwErrVariableSpace < PcdGet32 (PcdMaxHardwareErrorVariableSize)))){
2435 Status = Reclaim (
2436 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,
2437 &mVariableModuleGlobal->NonVolatileLastVariableOffset,
2438 FALSE,
2439 NULL
2440 );
2441 ASSERT_EFI_ERROR (Status);
2442 }
2443 }
2444
2445 /**
2446 Initializes variable store area for non-volatile and volatile variable.
2447
2448 @param FvbProtocol Pointer to an instance of EFI Firmware Volume Block Protocol.
2449
2450 @retval EFI_SUCCESS Function successfully executed.
2451 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
2452
2453 **/
2454 EFI_STATUS
2455 VariableCommonInitialize (
2456 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvbProtocol
2457 )
2458 {
2459 EFI_STATUS Status;
2460 VARIABLE_STORE_HEADER *VolatileVariableStore;
2461 VARIABLE_STORE_HEADER *VariableStoreHeader;
2462 VARIABLE_HEADER *NextVariable;
2463 EFI_PHYSICAL_ADDRESS TempVariableStoreHeader;
2464 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;
2465 EFI_PHYSICAL_ADDRESS BaseAddress;
2466 UINT64 Length;
2467 UINTN Index;
2468 UINT8 Data;
2469 EFI_PHYSICAL_ADDRESS VariableStoreBase;
2470 UINT64 VariableStoreLength;
2471 EFI_EVENT ReadyToBootEvent;
2472 UINTN ScratchSize;
2473 UINTN VariableSize;
2474
2475 Status = EFI_SUCCESS;
2476 //
2477 // Allocate runtime memory for variable driver global structure.
2478 //
2479 mVariableModuleGlobal = AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL));
2480 if (mVariableModuleGlobal == NULL) {
2481 return EFI_OUT_OF_RESOURCES;
2482 }
2483
2484 EfiInitializeLock(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock, TPL_NOTIFY);
2485
2486 //
2487 // Note that in EdkII variable driver implementation, Hardware Error Record type variable
2488 // is stored with common variable in the same NV region. So the platform integrator should
2489 // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of
2490 // PcdFlashNvStorageVariableSize.
2491 //
2492 ASSERT (PcdGet32 (PcdHwErrStorageSize) <= PcdGet32 (PcdFlashNvStorageVariableSize));
2493
2494 //
2495 // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.
2496 //
2497 ScratchSize = MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize));
2498 VolatileVariableStore = AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize) + ScratchSize);
2499 if (VolatileVariableStore == NULL) {
2500 FreePool (mVariableModuleGlobal);
2501 return EFI_OUT_OF_RESOURCES;
2502 }
2503
2504 SetMem (VolatileVariableStore, PcdGet32 (PcdVariableStoreSize) + ScratchSize, 0xff);
2505
2506 //
2507 // Variable Specific Data
2508 //
2509 mVariableModuleGlobal->VariableGlobal.VolatileVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VolatileVariableStore;
2510 mVariableModuleGlobal->VolatileLastVariableOffset = (UINTN) GetStartPointer (VolatileVariableStore) - (UINTN) VolatileVariableStore;
2511 mVariableModuleGlobal->FvbInstance = FvbProtocol;
2512
2513 CopyGuid (&VolatileVariableStore->Signature, &gEfiVariableGuid);
2514 VolatileVariableStore->Size = PcdGet32 (PcdVariableStoreSize);
2515 VolatileVariableStore->Format = VARIABLE_STORE_FORMATTED;
2516 VolatileVariableStore->State = VARIABLE_STORE_HEALTHY;
2517 VolatileVariableStore->Reserved = 0;
2518 VolatileVariableStore->Reserved1 = 0;
2519
2520 //
2521 // Get non volatile varaible store
2522 //
2523
2524 TempVariableStoreHeader = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);
2525 if (TempVariableStoreHeader == 0) {
2526 TempVariableStoreHeader = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);
2527 }
2528
2529 VariableStoreBase = TempVariableStoreHeader + \
2530 (((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(TempVariableStoreHeader)) -> HeaderLength);
2531 VariableStoreLength = (UINT64) PcdGet32 (PcdFlashNvStorageVariableSize) - \
2532 (((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(TempVariableStoreHeader)) -> HeaderLength);
2533 //
2534 // Mark the variable storage region of the FLASH as RUNTIME
2535 //
2536 BaseAddress = VariableStoreBase & (~EFI_PAGE_MASK);
2537 Length = VariableStoreLength + (VariableStoreBase - BaseAddress);
2538 Length = (Length + EFI_PAGE_SIZE - 1) & (~EFI_PAGE_MASK);
2539
2540 Status = gDS->GetMemorySpaceDescriptor (BaseAddress, &GcdDescriptor);
2541 if (EFI_ERROR (Status)) {
2542 goto Done;
2543 }
2544
2545 Status = gDS->SetMemorySpaceAttributes (
2546 BaseAddress,
2547 Length,
2548 GcdDescriptor.Attributes | EFI_MEMORY_RUNTIME
2549 );
2550 if (EFI_ERROR (Status)) {
2551 goto Done;
2552 }
2553 //
2554 // Get address of non volatile variable store base
2555 //
2556 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase;
2557 VariableStoreHeader = (VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase;
2558 if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) {
2559 if (~VariableStoreHeader->Size == 0) {
2560 Status = UpdateVariableStore (
2561 &mVariableModuleGlobal->VariableGlobal,
2562 FALSE,
2563 FALSE,
2564 mVariableModuleGlobal->FvbInstance,
2565 (UINTN) &VariableStoreHeader->Size,
2566 sizeof (UINT32),
2567 (UINT8 *) &VariableStoreLength
2568 );
2569 //
2570 // As Variables are stored in NV storage, which are slow devices,such as flash.
2571 // Variable operation may skip checking variable program result to improve performance,
2572 // We can assume Variable program is OK through some check point.
2573 // Variable Store Size Setting should be the first Variable write operation,
2574 // We can assume all Read/Write is OK if we can set Variable store size successfully.
2575 // If write fail, we will assert here
2576 //
2577 ASSERT(VariableStoreHeader->Size == VariableStoreLength);
2578
2579 if (EFI_ERROR (Status)) {
2580 goto Done;
2581 }
2582 }
2583
2584 //
2585 // Parse non-volatile variable data and get last variable offset
2586 //
2587 NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase);
2588 Status = EFI_SUCCESS;
2589
2590 while (IsValidVariableHeader (NextVariable)) {
2591 VariableSize = NextVariable->NameSize + NextVariable->DataSize + sizeof (VARIABLE_HEADER);
2592 if ((NextVariable->Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {
2593 mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VariableSize);
2594 } else {
2595 mVariableModuleGlobal->CommonVariableTotalSize += HEADER_ALIGN (VariableSize);
2596 }
2597
2598 NextVariable = GetNextVariablePtr (NextVariable);
2599 }
2600
2601 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) VariableStoreBase;
2602
2603 //
2604 // Check if the free area is really free.
2605 //
2606 for (Index = mVariableModuleGlobal->NonVolatileLastVariableOffset; Index < VariableStoreHeader->Size; Index++) {
2607 Data = ((UINT8 *) (UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase)[Index];
2608 if (Data != 0xff) {
2609 //
2610 // There must be something wrong in variable store, do reclaim operation.
2611 //
2612 Status = Reclaim (
2613 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,
2614 &mVariableModuleGlobal->NonVolatileLastVariableOffset,
2615 FALSE,
2616 NULL
2617 );
2618
2619 if (EFI_ERROR (Status)) {
2620 goto Done;
2621 }
2622
2623 break;
2624 }
2625 }
2626
2627 //
2628 // Register the event handling function to reclaim variable for OS usage.
2629 //
2630 Status = EfiCreateEventReadyToBootEx (
2631 TPL_NOTIFY,
2632 ReclaimForOS,
2633 NULL,
2634 &ReadyToBootEvent
2635 );
2636 } else {
2637 Status = EFI_VOLUME_CORRUPTED;
2638 DEBUG((EFI_D_INFO, "Variable Store header is corrupted\n"));
2639 }
2640
2641 Done:
2642 if (EFI_ERROR (Status)) {
2643 FreePool (mVariableModuleGlobal);
2644 FreePool (VolatileVariableStore);
2645 }
2646
2647 return Status;
2648 }
2649
2650 /**
2651 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
2652
2653 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
2654 It convers pointer to new virtual address.
2655
2656 @param Event Event whose notification function is being invoked
2657 @param Context Pointer to the notification function's context
2658
2659 **/
2660 VOID
2661 EFIAPI
2662 VariableClassAddressChangeEvent (
2663 IN EFI_EVENT Event,
2664 IN VOID *Context
2665 )
2666 {
2667 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->GetBlockSize);
2668 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->GetPhysicalAddress);
2669 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->GetAttributes);
2670 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->SetAttributes);
2671 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->Read);
2672 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->Write);
2673 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->EraseBlocks);
2674 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance);
2675 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->PlatformLangCodes);
2676 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->LangCodes);
2677 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->PlatformLang);
2678 EfiConvertPointer (
2679 0x0,
2680 (VOID **) &mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase
2681 );
2682 EfiConvertPointer (
2683 0x0,
2684 (VOID **) &mVariableModuleGlobal->VariableGlobal.VolatileVariableBase
2685 );
2686 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal);
2687 }
2688
2689 /**
2690 Firmware Volume Block Protocol notification event handler.
2691
2692 Discover NV Variable Store and install Variable Arch Protocol.
2693
2694 @param[in] Event Event whose notification function is being invoked.
2695 @param[in] Context Pointer to the notification function's context.
2696 **/
2697 VOID
2698 EFIAPI
2699 FvbNotificationEvent (
2700 IN EFI_EVENT Event,
2701 IN VOID *Context
2702 )
2703 {
2704 EFI_STATUS Status;
2705 EFI_HANDLE *HandleBuffer;
2706 UINTN HandleCount;
2707 UINTN Index;
2708 EFI_PHYSICAL_ADDRESS FvbBaseAddress;
2709 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;
2710 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
2711 EFI_FVB_ATTRIBUTES_2 Attributes;
2712 EFI_SYSTEM_TABLE *SystemTable;
2713 EFI_PHYSICAL_ADDRESS NvStorageVariableBase;
2714
2715 SystemTable = (EFI_SYSTEM_TABLE *)Context;
2716 Fvb = NULL;
2717
2718 //
2719 // Locate all handles of Fvb protocol
2720 //
2721 Status = gBS->LocateHandleBuffer (
2722 ByProtocol,
2723 &gEfiFirmwareVolumeBlockProtocolGuid,
2724 NULL,
2725 &HandleCount,
2726 &HandleBuffer
2727 );
2728 if (EFI_ERROR (Status)) {
2729 return ;
2730 }
2731
2732 //
2733 // Get the FVB to access variable store
2734 //
2735 for (Index = 0; Index < HandleCount; Index += 1, Status = EFI_NOT_FOUND, Fvb = NULL) {
2736 Status = gBS->HandleProtocol (
2737 HandleBuffer[Index],
2738 &gEfiFirmwareVolumeBlockProtocolGuid,
2739 (VOID **) &Fvb
2740 );
2741 if (EFI_ERROR (Status)) {
2742 Status = EFI_NOT_FOUND;
2743 break;
2744 }
2745
2746 //
2747 // Ensure this FVB protocol supported Write operation.
2748 //
2749 Status = Fvb->GetAttributes (Fvb, &Attributes);
2750 if (EFI_ERROR (Status) || ((Attributes & EFI_FVB2_WRITE_STATUS) == 0)) {
2751 continue;
2752 }
2753 //
2754 // Compare the address and select the right one
2755 //
2756 Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);
2757 if (EFI_ERROR (Status)) {
2758 continue;
2759 }
2760
2761 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvbBaseAddress);
2762 NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);
2763 if (NvStorageVariableBase == 0) {
2764 NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);
2765 }
2766
2767 if ((NvStorageVariableBase >= FvbBaseAddress) && (NvStorageVariableBase < (FvbBaseAddress + FwVolHeader->FvLength))) {
2768 Status = EFI_SUCCESS;
2769 break;
2770 }
2771 }
2772
2773 FreePool (HandleBuffer);
2774 if (!EFI_ERROR (Status) && Fvb != NULL) {
2775 //
2776 // Close the notify event to avoid install gEfiVariableArchProtocolGuid & gEfiVariableWriteArchProtocolGuid again.
2777 //
2778 Status = gBS->CloseEvent (Event);
2779 ASSERT_EFI_ERROR (Status);
2780
2781 Status = VariableCommonInitialize (Fvb);
2782 ASSERT_EFI_ERROR (Status);
2783
2784 SystemTable->RuntimeServices->GetVariable = RuntimeServiceGetVariable;
2785 SystemTable->RuntimeServices->GetNextVariableName = RuntimeServiceGetNextVariableName;
2786 SystemTable->RuntimeServices->SetVariable = RuntimeServiceSetVariable;
2787 SystemTable->RuntimeServices->QueryVariableInfo = RuntimeServiceQueryVariableInfo;
2788
2789 //
2790 // Now install the Variable Runtime Architectural Protocol on a new handle
2791 //
2792 Status = gBS->InstallMultipleProtocolInterfaces (
2793 &mHandle,
2794 &gEfiVariableArchProtocolGuid, NULL,
2795 &gEfiVariableWriteArchProtocolGuid, NULL,
2796 NULL
2797 );
2798 ASSERT_EFI_ERROR (Status);
2799
2800 Status = gBS->CreateEventEx (
2801 EVT_NOTIFY_SIGNAL,
2802 TPL_NOTIFY,
2803 VariableClassAddressChangeEvent,
2804 NULL,
2805 &gEfiEventVirtualAddressChangeGuid,
2806 &mVirtualAddressChangeEvent
2807 );
2808 ASSERT_EFI_ERROR (Status);
2809 }
2810
2811 }
2812
2813 /**
2814 Variable Driver main entry point. The Variable driver places the 4 EFI
2815 runtime services in the EFI System Table and installs arch protocols
2816 for variable read and write services being availible. It also registers
2817 notification function for EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
2818
2819 @param[in] ImageHandle The firmware allocated handle for the EFI image.
2820 @param[in] SystemTable A pointer to the EFI System Table.
2821
2822 @retval EFI_SUCCESS Variable service successfully initialized.
2823
2824 **/
2825 EFI_STATUS
2826 EFIAPI
2827 VariableServiceInitialize (
2828 IN EFI_HANDLE ImageHandle,
2829 IN EFI_SYSTEM_TABLE *SystemTable
2830 )
2831 {
2832 //
2833 // Register FvbNotificationEvent () notify function.
2834 //
2835 EfiCreateProtocolNotifyEvent (
2836 &gEfiFirmwareVolumeBlockProtocolGuid,
2837 TPL_CALLBACK,
2838 FvbNotificationEvent,
2839 (VOID *)SystemTable,
2840 &mFvbRegistration
2841 );
2842
2843 return EFI_SUCCESS;
2844 }
2845