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