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