]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
Vlv2TbltDevicePkg: Link AuthVariableLib for following merged variable driver deploy
[mirror_edk2.git] / MdeModulePkg / Universal / Variable / RuntimeDxe / Variable.c
... / ...
CommitLineData
1/** @file\r
2\r
3 The common variable operation routines shared by DXE_RUNTIME variable \r
4 module and DXE_SMM variable module.\r
5 \r
6 Caution: This module requires additional review when modified.\r
7 This driver will have external input - variable data. They may be input in SMM mode.\r
8 This external input must be validated carefully to avoid security issue like\r
9 buffer overflow, integer overflow.\r
10\r
11 VariableServiceGetNextVariableName () and VariableServiceQueryVariableInfo() are external API.\r
12 They need check input parameter.\r
13\r
14 VariableServiceGetVariable() and VariableServiceSetVariable() are external API\r
15 to receive datasize and data buffer. The size should be checked carefully.\r
16\r
17Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>\r
18This program and the accompanying materials \r
19are licensed and made available under the terms and conditions of the BSD License \r
20which accompanies this distribution. The full text of the license may be found at \r
21http://opensource.org/licenses/bsd-license.php \r
22\r
23THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
24WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
25\r
26**/\r
27\r
28#include "Variable.h"\r
29\r
30VARIABLE_MODULE_GLOBAL *mVariableModuleGlobal;\r
31\r
32///\r
33/// Define a memory cache that improves the search performance for a variable.\r
34///\r
35VARIABLE_STORE_HEADER *mNvVariableCache = NULL;\r
36\r
37///\r
38/// The memory entry used for variable statistics data.\r
39///\r
40VARIABLE_INFO_ENTRY *gVariableInfo = NULL;\r
41\r
42///\r
43/// The list to store the variables which cannot be set after the EFI_END_OF_DXE_EVENT_GROUP_GUID\r
44/// or EVT_GROUP_READY_TO_BOOT event.\r
45///\r
46LIST_ENTRY mLockedVariableList = INITIALIZE_LIST_HEAD_VARIABLE (mLockedVariableList);\r
47\r
48///\r
49/// The flag to indicate whether the platform has left the DXE phase of execution.\r
50///\r
51BOOLEAN mEndOfDxe = FALSE;\r
52\r
53///\r
54/// The flag to indicate whether the variable storage locking is enabled.\r
55///\r
56BOOLEAN mEnableLocking = TRUE;\r
57\r
58//\r
59// It will record the current boot error flag before EndOfDxe.\r
60//\r
61VAR_ERROR_FLAG mCurrentBootVarErrFlag = VAR_ERROR_FLAG_NO_ERROR;\r
62\r
63/**\r
64 Routine used to track statistical information about variable usage. \r
65 The data is stored in the EFI system table so it can be accessed later.\r
66 VariableInfo.efi can dump out the table. Only Boot Services variable \r
67 accesses are tracked by this code. The PcdVariableCollectStatistics\r
68 build flag controls if this feature is enabled. \r
69\r
70 A read that hits in the cache will have Read and Cache true for \r
71 the transaction. Data is allocated by this routine, but never\r
72 freed.\r
73\r
74 @param[in] VariableName Name of the Variable to track.\r
75 @param[in] VendorGuid Guid of the Variable to track.\r
76 @param[in] Volatile TRUE if volatile FALSE if non-volatile.\r
77 @param[in] Read TRUE if GetVariable() was called.\r
78 @param[in] Write TRUE if SetVariable() was called.\r
79 @param[in] Delete TRUE if deleted via SetVariable().\r
80 @param[in] Cache TRUE for a cache hit.\r
81\r
82**/\r
83VOID\r
84UpdateVariableInfo (\r
85 IN CHAR16 *VariableName,\r
86 IN EFI_GUID *VendorGuid,\r
87 IN BOOLEAN Volatile,\r
88 IN BOOLEAN Read,\r
89 IN BOOLEAN Write,\r
90 IN BOOLEAN Delete,\r
91 IN BOOLEAN Cache\r
92 )\r
93{\r
94 VARIABLE_INFO_ENTRY *Entry;\r
95\r
96 if (FeaturePcdGet (PcdVariableCollectStatistics)) {\r
97\r
98 if (AtRuntime ()) {\r
99 // Don't collect statistics at runtime.\r
100 return;\r
101 }\r
102\r
103 if (gVariableInfo == NULL) {\r
104 //\r
105 // On the first call allocate a entry and place a pointer to it in\r
106 // the EFI System Table.\r
107 //\r
108 gVariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));\r
109 ASSERT (gVariableInfo != NULL);\r
110\r
111 CopyGuid (&gVariableInfo->VendorGuid, VendorGuid);\r
112 gVariableInfo->Name = AllocateZeroPool (StrSize (VariableName));\r
113 ASSERT (gVariableInfo->Name != NULL);\r
114 StrnCpy (gVariableInfo->Name, VariableName, StrLen (VariableName));\r
115 gVariableInfo->Volatile = Volatile;\r
116 }\r
117\r
118 \r
119 for (Entry = gVariableInfo; Entry != NULL; Entry = Entry->Next) {\r
120 if (CompareGuid (VendorGuid, &Entry->VendorGuid)) {\r
121 if (StrCmp (VariableName, Entry->Name) == 0) {\r
122 if (Read) {\r
123 Entry->ReadCount++;\r
124 }\r
125 if (Write) {\r
126 Entry->WriteCount++;\r
127 }\r
128 if (Delete) {\r
129 Entry->DeleteCount++;\r
130 }\r
131 if (Cache) {\r
132 Entry->CacheCount++;\r
133 }\r
134\r
135 return;\r
136 }\r
137 }\r
138\r
139 if (Entry->Next == NULL) {\r
140 //\r
141 // If the entry is not in the table add it.\r
142 // Next iteration of the loop will fill in the data.\r
143 //\r
144 Entry->Next = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));\r
145 ASSERT (Entry->Next != NULL);\r
146\r
147 CopyGuid (&Entry->Next->VendorGuid, VendorGuid);\r
148 Entry->Next->Name = AllocateZeroPool (StrSize (VariableName));\r
149 ASSERT (Entry->Next->Name != NULL);\r
150 StrnCpy (Entry->Next->Name, VariableName, StrLen (VariableName));\r
151 Entry->Next->Volatile = Volatile;\r
152 }\r
153\r
154 }\r
155 }\r
156}\r
157\r
158\r
159/**\r
160\r
161 This code checks if variable header is valid or not.\r
162\r
163 @param Variable Pointer to the Variable Header.\r
164 @param VariableStoreEnd Pointer to the Variable Store End.\r
165\r
166 @retval TRUE Variable header is valid.\r
167 @retval FALSE Variable header is not valid.\r
168\r
169**/\r
170BOOLEAN\r
171IsValidVariableHeader (\r
172 IN VARIABLE_HEADER *Variable,\r
173 IN VARIABLE_HEADER *VariableStoreEnd\r
174 )\r
175{\r
176 if ((Variable == NULL) || (Variable >= VariableStoreEnd) || (Variable->StartId != VARIABLE_DATA)) {\r
177 //\r
178 // Variable is NULL or has reached the end of variable store,\r
179 // or the StartId is not correct.\r
180 //\r
181 return FALSE;\r
182 }\r
183\r
184 return TRUE;\r
185}\r
186\r
187\r
188/**\r
189\r
190 This function writes data to the FWH at the correct LBA even if the LBAs\r
191 are fragmented.\r
192\r
193 @param Global Pointer to VARAIBLE_GLOBAL structure.\r
194 @param Volatile Point out the Variable is Volatile or Non-Volatile.\r
195 @param SetByIndex TRUE if target pointer is given as index.\r
196 FALSE if target pointer is absolute.\r
197 @param Fvb Pointer to the writable FVB protocol.\r
198 @param DataPtrIndex Pointer to the Data from the end of VARIABLE_STORE_HEADER\r
199 structure.\r
200 @param DataSize Size of data to be written.\r
201 @param Buffer Pointer to the buffer from which data is written.\r
202\r
203 @retval EFI_INVALID_PARAMETER Parameters not valid.\r
204 @retval EFI_SUCCESS Variable store successfully updated.\r
205\r
206**/\r
207EFI_STATUS\r
208UpdateVariableStore (\r
209 IN VARIABLE_GLOBAL *Global,\r
210 IN BOOLEAN Volatile,\r
211 IN BOOLEAN SetByIndex,\r
212 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb,\r
213 IN UINTN DataPtrIndex,\r
214 IN UINT32 DataSize,\r
215 IN UINT8 *Buffer\r
216 )\r
217{\r
218 EFI_FV_BLOCK_MAP_ENTRY *PtrBlockMapEntry;\r
219 UINTN BlockIndex2;\r
220 UINTN LinearOffset;\r
221 UINTN CurrWriteSize;\r
222 UINTN CurrWritePtr;\r
223 UINT8 *CurrBuffer;\r
224 EFI_LBA LbaNumber;\r
225 UINTN Size;\r
226 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r
227 VARIABLE_STORE_HEADER *VolatileBase;\r
228 EFI_PHYSICAL_ADDRESS FvVolHdr;\r
229 EFI_PHYSICAL_ADDRESS DataPtr;\r
230 EFI_STATUS Status;\r
231\r
232 FwVolHeader = NULL;\r
233 DataPtr = DataPtrIndex;\r
234\r
235 //\r
236 // Check if the Data is Volatile.\r
237 //\r
238 if (!Volatile) {\r
239 ASSERT (Fvb != NULL);\r
240 Status = Fvb->GetPhysicalAddress(Fvb, &FvVolHdr);\r
241 ASSERT_EFI_ERROR (Status);\r
242\r
243 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvVolHdr);\r
244 //\r
245 // Data Pointer should point to the actual Address where data is to be\r
246 // written.\r
247 //\r
248 if (SetByIndex) {\r
249 DataPtr += mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;\r
250 }\r
251\r
252 if ((DataPtr + DataSize) >= ((EFI_PHYSICAL_ADDRESS) (UINTN) ((UINT8 *) FwVolHeader + FwVolHeader->FvLength))) {\r
253 return EFI_INVALID_PARAMETER;\r
254 }\r
255 } else {\r
256 //\r
257 // Data Pointer should point to the actual Address where data is to be\r
258 // written.\r
259 //\r
260 VolatileBase = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r
261 if (SetByIndex) {\r
262 DataPtr += mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;\r
263 }\r
264\r
265 if ((DataPtr + DataSize) >= ((UINTN) ((UINT8 *) VolatileBase + VolatileBase->Size))) {\r
266 return EFI_INVALID_PARAMETER;\r
267 }\r
268 \r
269 //\r
270 // If Volatile Variable just do a simple mem copy.\r
271 // \r
272 CopyMem ((UINT8 *)(UINTN)DataPtr, Buffer, DataSize);\r
273 return EFI_SUCCESS;\r
274 }\r
275 \r
276 //\r
277 // If we are here we are dealing with Non-Volatile Variables.\r
278 //\r
279 LinearOffset = (UINTN) FwVolHeader;\r
280 CurrWritePtr = (UINTN) DataPtr;\r
281 CurrWriteSize = DataSize;\r
282 CurrBuffer = Buffer;\r
283 LbaNumber = 0;\r
284\r
285 if (CurrWritePtr < LinearOffset) {\r
286 return EFI_INVALID_PARAMETER;\r
287 }\r
288\r
289 for (PtrBlockMapEntry = FwVolHeader->BlockMap; PtrBlockMapEntry->NumBlocks != 0; PtrBlockMapEntry++) {\r
290 for (BlockIndex2 = 0; BlockIndex2 < PtrBlockMapEntry->NumBlocks; BlockIndex2++) {\r
291 //\r
292 // Check to see if the Variable Writes are spanning through multiple\r
293 // blocks.\r
294 //\r
295 if ((CurrWritePtr >= LinearOffset) && (CurrWritePtr < LinearOffset + PtrBlockMapEntry->Length)) {\r
296 if ((CurrWritePtr + CurrWriteSize) <= (LinearOffset + PtrBlockMapEntry->Length)) {\r
297 Status = Fvb->Write (\r
298 Fvb,\r
299 LbaNumber,\r
300 (UINTN) (CurrWritePtr - LinearOffset),\r
301 &CurrWriteSize,\r
302 CurrBuffer\r
303 );\r
304 return Status;\r
305 } else {\r
306 Size = (UINT32) (LinearOffset + PtrBlockMapEntry->Length - CurrWritePtr);\r
307 Status = Fvb->Write (\r
308 Fvb,\r
309 LbaNumber,\r
310 (UINTN) (CurrWritePtr - LinearOffset),\r
311 &Size,\r
312 CurrBuffer\r
313 );\r
314 if (EFI_ERROR (Status)) {\r
315 return Status;\r
316 }\r
317\r
318 CurrWritePtr = LinearOffset + PtrBlockMapEntry->Length;\r
319 CurrBuffer = CurrBuffer + Size;\r
320 CurrWriteSize = CurrWriteSize - Size;\r
321 }\r
322 }\r
323\r
324 LinearOffset += PtrBlockMapEntry->Length;\r
325 LbaNumber++;\r
326 }\r
327 }\r
328\r
329 return EFI_SUCCESS;\r
330}\r
331\r
332\r
333/**\r
334\r
335 This code gets the current status of Variable Store.\r
336\r
337 @param VarStoreHeader Pointer to the Variable Store Header.\r
338\r
339 @retval EfiRaw Variable store status is raw.\r
340 @retval EfiValid Variable store status is valid.\r
341 @retval EfiInvalid Variable store status is invalid.\r
342\r
343**/\r
344VARIABLE_STORE_STATUS\r
345GetVariableStoreStatus (\r
346 IN VARIABLE_STORE_HEADER *VarStoreHeader\r
347 )\r
348{\r
349 if (CompareGuid (&VarStoreHeader->Signature, &gEfiVariableGuid) &&\r
350 VarStoreHeader->Format == VARIABLE_STORE_FORMATTED &&\r
351 VarStoreHeader->State == VARIABLE_STORE_HEALTHY\r
352 ) {\r
353\r
354 return EfiValid;\r
355 } else if (((UINT32 *)(&VarStoreHeader->Signature))[0] == 0xffffffff &&\r
356 ((UINT32 *)(&VarStoreHeader->Signature))[1] == 0xffffffff &&\r
357 ((UINT32 *)(&VarStoreHeader->Signature))[2] == 0xffffffff &&\r
358 ((UINT32 *)(&VarStoreHeader->Signature))[3] == 0xffffffff &&\r
359 VarStoreHeader->Size == 0xffffffff &&\r
360 VarStoreHeader->Format == 0xff &&\r
361 VarStoreHeader->State == 0xff\r
362 ) {\r
363\r
364 return EfiRaw;\r
365 } else {\r
366 return EfiInvalid;\r
367 }\r
368}\r
369\r
370\r
371/**\r
372\r
373 This code gets the size of name of variable.\r
374\r
375 @param Variable Pointer to the Variable Header.\r
376\r
377 @return UINTN Size of variable in bytes.\r
378\r
379**/\r
380UINTN\r
381NameSizeOfVariable (\r
382 IN VARIABLE_HEADER *Variable\r
383 )\r
384{\r
385 if (Variable->State == (UINT8) (-1) ||\r
386 Variable->DataSize == (UINT32) (-1) ||\r
387 Variable->NameSize == (UINT32) (-1) ||\r
388 Variable->Attributes == (UINT32) (-1)) {\r
389 return 0;\r
390 }\r
391 return (UINTN) Variable->NameSize;\r
392}\r
393\r
394/**\r
395\r
396 This code gets the size of variable data.\r
397\r
398 @param Variable Pointer to the Variable Header.\r
399\r
400 @return Size of variable in bytes.\r
401\r
402**/\r
403UINTN\r
404DataSizeOfVariable (\r
405 IN VARIABLE_HEADER *Variable\r
406 )\r
407{\r
408 if (Variable->State == (UINT8) (-1) ||\r
409 Variable->DataSize == (UINT32) (-1) ||\r
410 Variable->NameSize == (UINT32) (-1) ||\r
411 Variable->Attributes == (UINT32) (-1)) {\r
412 return 0;\r
413 }\r
414 return (UINTN) Variable->DataSize;\r
415}\r
416\r
417/**\r
418\r
419 This code gets the pointer to the variable name.\r
420\r
421 @param Variable Pointer to the Variable Header.\r
422\r
423 @return Pointer to Variable Name which is Unicode encoding.\r
424\r
425**/\r
426CHAR16 *\r
427GetVariableNamePtr (\r
428 IN VARIABLE_HEADER *Variable\r
429 )\r
430{\r
431\r
432 return (CHAR16 *) (Variable + 1);\r
433}\r
434\r
435/**\r
436\r
437 This code gets the pointer to the variable data.\r
438\r
439 @param Variable Pointer to the Variable Header.\r
440\r
441 @return Pointer to Variable Data.\r
442\r
443**/\r
444UINT8 *\r
445GetVariableDataPtr (\r
446 IN VARIABLE_HEADER *Variable\r
447 )\r
448{\r
449 UINTN Value;\r
450 \r
451 //\r
452 // Be careful about pad size for alignment.\r
453 //\r
454 Value = (UINTN) GetVariableNamePtr (Variable);\r
455 Value += NameSizeOfVariable (Variable);\r
456 Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));\r
457\r
458 return (UINT8 *) Value;\r
459}\r
460\r
461\r
462/**\r
463\r
464 This code gets the pointer to the next variable header.\r
465\r
466 @param Variable Pointer to the Variable Header.\r
467\r
468 @return Pointer to next variable header.\r
469\r
470**/\r
471VARIABLE_HEADER *\r
472GetNextVariablePtr (\r
473 IN VARIABLE_HEADER *Variable\r
474 )\r
475{\r
476 UINTN Value;\r
477\r
478 Value = (UINTN) GetVariableDataPtr (Variable);\r
479 Value += DataSizeOfVariable (Variable);\r
480 Value += GET_PAD_SIZE (DataSizeOfVariable (Variable));\r
481\r
482 //\r
483 // Be careful about pad size for alignment.\r
484 //\r
485 return (VARIABLE_HEADER *) HEADER_ALIGN (Value);\r
486}\r
487\r
488/**\r
489\r
490 Gets the pointer to the first variable header in given variable store area.\r
491\r
492 @param VarStoreHeader Pointer to the Variable Store Header.\r
493\r
494 @return Pointer to the first variable header.\r
495\r
496**/\r
497VARIABLE_HEADER *\r
498GetStartPointer (\r
499 IN VARIABLE_STORE_HEADER *VarStoreHeader\r
500 )\r
501{\r
502 //\r
503 // The end of variable store.\r
504 //\r
505 return (VARIABLE_HEADER *) HEADER_ALIGN (VarStoreHeader + 1);\r
506}\r
507\r
508/**\r
509\r
510 Gets the pointer to the end of the variable storage area.\r
511\r
512 This function gets pointer to the end of the variable storage\r
513 area, according to the input variable store header.\r
514\r
515 @param VarStoreHeader Pointer to the Variable Store Header.\r
516\r
517 @return Pointer to the end of the variable storage area. \r
518\r
519**/\r
520VARIABLE_HEADER *\r
521GetEndPointer (\r
522 IN VARIABLE_STORE_HEADER *VarStoreHeader\r
523 )\r
524{\r
525 //\r
526 // The end of variable store\r
527 //\r
528 return (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) VarStoreHeader + VarStoreHeader->Size);\r
529}\r
530\r
531/**\r
532 Record variable error flag.\r
533\r
534 @param[in] Flag Variable error flag to record.\r
535 @param[in] VariableName Name of variable.\r
536 @param[in] VendorGuid Guid of variable.\r
537 @param[in] Attributes Attributes of the variable.\r
538 @param[in] VariableSize Size of the variable.\r
539\r
540**/\r
541VOID\r
542RecordVarErrorFlag (\r
543 IN VAR_ERROR_FLAG Flag,\r
544 IN CHAR16 *VariableName,\r
545 IN EFI_GUID *VendorGuid,\r
546 IN UINT32 Attributes,\r
547 IN UINTN VariableSize\r
548 )\r
549{\r
550 EFI_STATUS Status;\r
551 VARIABLE_POINTER_TRACK Variable;\r
552 VAR_ERROR_FLAG *VarErrFlag;\r
553 VAR_ERROR_FLAG TempFlag;\r
554\r
555 DEBUG_CODE (\r
556 DEBUG ((EFI_D_ERROR, "RecordVarErrorFlag (0x%02x) %s:%g - 0x%08x - 0x%x\n", Flag, VariableName, VendorGuid, Attributes, VariableSize));\r
557 if (Flag == VAR_ERROR_FLAG_SYSTEM_ERROR) {\r
558 if (AtRuntime ()) {\r
559 DEBUG ((EFI_D_ERROR, "CommonRuntimeVariableSpace = 0x%x - CommonVariableTotalSize = 0x%x\n", mVariableModuleGlobal->CommonRuntimeVariableSpace, mVariableModuleGlobal->CommonVariableTotalSize));\r
560 } else {\r
561 DEBUG ((EFI_D_ERROR, "CommonVariableSpace = 0x%x - CommonVariableTotalSize = 0x%x\n", mVariableModuleGlobal->CommonVariableSpace, mVariableModuleGlobal->CommonVariableTotalSize));\r
562 }\r
563 } else {\r
564 DEBUG ((EFI_D_ERROR, "CommonMaxUserVariableSpace = 0x%x - CommonUserVariableTotalSize = 0x%x\n", mVariableModuleGlobal->CommonMaxUserVariableSpace, mVariableModuleGlobal->CommonUserVariableTotalSize));\r
565 }\r
566 );\r
567\r
568 if (!mEndOfDxe) {\r
569 //\r
570 // Before EndOfDxe, just record the current boot variable error flag to local variable,\r
571 // and leave the variable error flag in NV flash as the last boot variable error flag.\r
572 // After EndOfDxe in InitializeVarErrorFlag (), the variable error flag in NV flash\r
573 // will be initialized to this local current boot variable error flag.\r
574 //\r
575 mCurrentBootVarErrFlag &= Flag;\r
576 return;\r
577 }\r
578\r
579 //\r
580 // Record error flag (it should have be initialized).\r
581 //\r
582 Status = FindVariable (\r
583 VAR_ERROR_FLAG_NAME,\r
584 &gEdkiiVarErrorFlagGuid,\r
585 &Variable,\r
586 &mVariableModuleGlobal->VariableGlobal,\r
587 FALSE\r
588 );\r
589 if (!EFI_ERROR (Status)) {\r
590 VarErrFlag = (VAR_ERROR_FLAG *) GetVariableDataPtr (Variable.CurrPtr);\r
591 TempFlag = *VarErrFlag;\r
592 TempFlag &= Flag;\r
593 if (TempFlag == *VarErrFlag) {\r
594 return;\r
595 }\r
596 Status = UpdateVariableStore (\r
597 &mVariableModuleGlobal->VariableGlobal,\r
598 FALSE,\r
599 FALSE,\r
600 mVariableModuleGlobal->FvbInstance,\r
601 (UINTN) VarErrFlag - (UINTN) mNvVariableCache + (UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
602 sizeof (TempFlag),\r
603 &TempFlag\r
604 );\r
605 if (!EFI_ERROR (Status)) {\r
606 //\r
607 // Update the data in NV cache.\r
608 //\r
609 *VarErrFlag = Flag;\r
610 }\r
611 }\r
612}\r
613\r
614/**\r
615 Initialize variable error flag.\r
616\r
617 Before EndOfDxe, the variable indicates the last boot variable error flag,\r
618 then it means the last boot variable error flag must be got before EndOfDxe.\r
619 After EndOfDxe, the variable indicates the current boot variable error flag,\r
620 then it means the current boot variable error flag must be got after EndOfDxe.\r
621\r
622**/\r
623VOID\r
624InitializeVarErrorFlag (\r
625 VOID\r
626 )\r
627{\r
628 EFI_STATUS Status;\r
629 VARIABLE_POINTER_TRACK Variable;\r
630 VAR_ERROR_FLAG Flag;\r
631 VAR_ERROR_FLAG VarErrFlag;\r
632\r
633 if (!mEndOfDxe) {\r
634 return;\r
635 }\r
636\r
637 Flag = mCurrentBootVarErrFlag;\r
638 DEBUG ((EFI_D_INFO, "Initialize variable error flag (%02x)\n", Flag));\r
639\r
640 Status = FindVariable (\r
641 VAR_ERROR_FLAG_NAME,\r
642 &gEdkiiVarErrorFlagGuid,\r
643 &Variable,\r
644 &mVariableModuleGlobal->VariableGlobal,\r
645 FALSE\r
646 );\r
647 if (!EFI_ERROR (Status)) {\r
648 VarErrFlag = *((VAR_ERROR_FLAG *) GetVariableDataPtr (Variable.CurrPtr));\r
649 if (VarErrFlag == Flag) {\r
650 return;\r
651 }\r
652 }\r
653\r
654 UpdateVariable (\r
655 VAR_ERROR_FLAG_NAME,\r
656 &gEdkiiVarErrorFlagGuid,\r
657 &Flag,\r
658 sizeof (Flag),\r
659 VARIABLE_ATTRIBUTE_NV_BS_RT,\r
660 &Variable\r
661 );\r
662}\r
663\r
664/**\r
665 Is user variable?\r
666\r
667 @param[in] Variable Pointer to variable header.\r
668\r
669 @retval TRUE User variable.\r
670 @retval FALSE System variable.\r
671\r
672**/\r
673BOOLEAN\r
674IsUserVariable (\r
675 IN VARIABLE_HEADER *Variable\r
676 )\r
677{\r
678 VAR_CHECK_VARIABLE_PROPERTY Property;\r
679\r
680 //\r
681 // Only after End Of Dxe, the variables belong to system variable are fixed.\r
682 // If PcdMaxUserNvStorageVariableSize is 0, it means user variable share the same NV storage with system variable,\r
683 // then no need to check if the variable is user variable or not specially.\r
684 //\r
685 if (mEndOfDxe && (mVariableModuleGlobal->CommonMaxUserVariableSpace != mVariableModuleGlobal->CommonVariableSpace)) {\r
686 if (InternalVarCheckVariablePropertyGet (GetVariableNamePtr (Variable), &Variable->VendorGuid, &Property) == EFI_NOT_FOUND) {\r
687 return TRUE;\r
688 }\r
689 }\r
690 return FALSE;\r
691}\r
692\r
693/**\r
694 Calculate common user variable total size.\r
695\r
696**/\r
697VOID\r
698CalculateCommonUserVariableTotalSize (\r
699 VOID\r
700 )\r
701{\r
702 VARIABLE_HEADER *Variable;\r
703 VARIABLE_HEADER *NextVariable;\r
704 UINTN VariableSize;\r
705 VAR_CHECK_VARIABLE_PROPERTY Property;\r
706\r
707 //\r
708 // Only after End Of Dxe, the variables belong to system variable are fixed.\r
709 // If PcdMaxUserNvStorageVariableSize is 0, it means user variable share the same NV storage with system variable,\r
710 // then no need to calculate the common user variable total size specially.\r
711 //\r
712 if (mEndOfDxe && (mVariableModuleGlobal->CommonMaxUserVariableSpace != mVariableModuleGlobal->CommonVariableSpace)) {\r
713 Variable = GetStartPointer (mNvVariableCache);\r
714 while (IsValidVariableHeader (Variable, GetEndPointer (mNvVariableCache))) {\r
715 NextVariable = GetNextVariablePtr (Variable);\r
716 VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
717 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
718 if (InternalVarCheckVariablePropertyGet (GetVariableNamePtr (Variable), &Variable->VendorGuid, &Property) == EFI_NOT_FOUND) {\r
719 //\r
720 // No property, it is user variable.\r
721 //\r
722 mVariableModuleGlobal->CommonUserVariableTotalSize += VariableSize;\r
723 }\r
724 }\r
725\r
726 Variable = NextVariable;\r
727 }\r
728 }\r
729}\r
730\r
731/**\r
732 Initialize variable quota.\r
733\r
734**/\r
735VOID\r
736InitializeVariableQuota (\r
737 VOID\r
738 )\r
739{\r
740 STATIC BOOLEAN Initialized;\r
741\r
742 if (!mEndOfDxe || Initialized) {\r
743 return;\r
744 }\r
745 Initialized = TRUE;\r
746\r
747 InitializeVarErrorFlag ();\r
748 CalculateCommonUserVariableTotalSize ();\r
749}\r
750\r
751/**\r
752\r
753 Variable store garbage collection and reclaim operation.\r
754\r
755 @param VariableBase Base address of variable store.\r
756 @param LastVariableOffset Offset of last variable.\r
757 @param IsVolatile The variable store is volatile or not;\r
758 if it is non-volatile, need FTW.\r
759 @param UpdatingPtrTrack Pointer to updating variable pointer track structure.\r
760 @param NewVariable Pointer to new variable.\r
761 @param NewVariableSize New variable size.\r
762\r
763 @return EFI_OUT_OF_RESOURCES\r
764 @return EFI_SUCCESS\r
765 @return Others\r
766\r
767**/\r
768EFI_STATUS\r
769Reclaim (\r
770 IN EFI_PHYSICAL_ADDRESS VariableBase,\r
771 OUT UINTN *LastVariableOffset,\r
772 IN BOOLEAN IsVolatile,\r
773 IN OUT VARIABLE_POINTER_TRACK *UpdatingPtrTrack,\r
774 IN VARIABLE_HEADER *NewVariable,\r
775 IN UINTN NewVariableSize\r
776 )\r
777{\r
778 VARIABLE_HEADER *Variable;\r
779 VARIABLE_HEADER *AddedVariable;\r
780 VARIABLE_HEADER *NextVariable;\r
781 VARIABLE_HEADER *NextAddedVariable;\r
782 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
783 UINT8 *ValidBuffer;\r
784 UINTN MaximumBufferSize;\r
785 UINTN VariableSize;\r
786 UINTN NameSize;\r
787 UINT8 *CurrPtr;\r
788 VOID *Point0;\r
789 VOID *Point1;\r
790 BOOLEAN FoundAdded;\r
791 EFI_STATUS Status;\r
792 UINTN CommonVariableTotalSize;\r
793 UINTN CommonUserVariableTotalSize;\r
794 UINTN HwErrVariableTotalSize;\r
795 VARIABLE_HEADER *UpdatingVariable;\r
796 VARIABLE_HEADER *UpdatingInDeletedTransition;\r
797\r
798 UpdatingVariable = NULL;\r
799 UpdatingInDeletedTransition = NULL;\r
800 if (UpdatingPtrTrack != NULL) {\r
801 UpdatingVariable = UpdatingPtrTrack->CurrPtr;\r
802 UpdatingInDeletedTransition = UpdatingPtrTrack->InDeletedTransitionPtr;\r
803 }\r
804\r
805 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) VariableBase);\r
806\r
807 CommonVariableTotalSize = 0;\r
808 CommonUserVariableTotalSize = 0;\r
809 HwErrVariableTotalSize = 0;\r
810\r
811 if (IsVolatile) {\r
812 //\r
813 // Start Pointers for the variable.\r
814 //\r
815 Variable = GetStartPointer (VariableStoreHeader);\r
816 MaximumBufferSize = sizeof (VARIABLE_STORE_HEADER);\r
817\r
818 while (IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))) {\r
819 NextVariable = GetNextVariablePtr (Variable);\r
820 if ((Variable->State == VAR_ADDED || Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) &&\r
821 Variable != UpdatingVariable &&\r
822 Variable != UpdatingInDeletedTransition\r
823 ) {\r
824 VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
825 MaximumBufferSize += VariableSize;\r
826 }\r
827\r
828 Variable = NextVariable;\r
829 }\r
830\r
831 if (NewVariable != NULL) {\r
832 //\r
833 // Add the new variable size.\r
834 //\r
835 MaximumBufferSize += NewVariableSize;\r
836 }\r
837\r
838 //\r
839 // Reserve the 1 Bytes with Oxff to identify the\r
840 // end of the variable buffer.\r
841 //\r
842 MaximumBufferSize += 1;\r
843 ValidBuffer = AllocatePool (MaximumBufferSize);\r
844 if (ValidBuffer == NULL) {\r
845 return EFI_OUT_OF_RESOURCES;\r
846 }\r
847 } else {\r
848 //\r
849 // For NV variable reclaim, don't allocate pool here and just use mNvVariableCache\r
850 // as the buffer to reduce SMRAM consumption for SMM variable driver.\r
851 //\r
852 MaximumBufferSize = mNvVariableCache->Size;\r
853 ValidBuffer = (UINT8 *) mNvVariableCache;\r
854 }\r
855\r
856 SetMem (ValidBuffer, MaximumBufferSize, 0xff);\r
857\r
858 //\r
859 // Copy variable store header.\r
860 //\r
861 CopyMem (ValidBuffer, VariableStoreHeader, sizeof (VARIABLE_STORE_HEADER));\r
862 CurrPtr = (UINT8 *) GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer);\r
863\r
864 //\r
865 // Reinstall all ADDED variables as long as they are not identical to Updating Variable.\r
866 // \r
867 Variable = GetStartPointer (VariableStoreHeader);\r
868 while (IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))) {\r
869 NextVariable = GetNextVariablePtr (Variable);\r
870 if (Variable != UpdatingVariable && Variable->State == VAR_ADDED) {\r
871 VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
872 CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);\r
873 CurrPtr += VariableSize;\r
874 if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
875 HwErrVariableTotalSize += VariableSize;\r
876 } else if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
877 CommonVariableTotalSize += VariableSize;\r
878 if (IsUserVariable (Variable)) {\r
879 CommonUserVariableTotalSize += VariableSize;\r
880 }\r
881 }\r
882 }\r
883 Variable = NextVariable;\r
884 }\r
885\r
886 //\r
887 // Reinstall all in delete transition variables.\r
888 // \r
889 Variable = GetStartPointer (VariableStoreHeader);\r
890 while (IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))) {\r
891 NextVariable = GetNextVariablePtr (Variable);\r
892 if (Variable != UpdatingVariable && Variable != UpdatingInDeletedTransition && Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
893\r
894 //\r
895 // Buffer has cached all ADDED variable. \r
896 // Per IN_DELETED variable, we have to guarantee that\r
897 // no ADDED one in previous buffer. \r
898 // \r
899 \r
900 FoundAdded = FALSE;\r
901 AddedVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer);\r
902 while (IsValidVariableHeader (AddedVariable, GetEndPointer ((VARIABLE_STORE_HEADER *) ValidBuffer))) {\r
903 NextAddedVariable = GetNextVariablePtr (AddedVariable);\r
904 NameSize = NameSizeOfVariable (AddedVariable);\r
905 if (CompareGuid (&AddedVariable->VendorGuid, &Variable->VendorGuid) &&\r
906 NameSize == NameSizeOfVariable (Variable)\r
907 ) {\r
908 Point0 = (VOID *) GetVariableNamePtr (AddedVariable);\r
909 Point1 = (VOID *) GetVariableNamePtr (Variable);\r
910 if (CompareMem (Point0, Point1, NameSize) == 0) {\r
911 FoundAdded = TRUE;\r
912 break;\r
913 }\r
914 }\r
915 AddedVariable = NextAddedVariable;\r
916 }\r
917 if (!FoundAdded) {\r
918 //\r
919 // Promote VAR_IN_DELETED_TRANSITION to VAR_ADDED.\r
920 //\r
921 VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
922 CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);\r
923 ((VARIABLE_HEADER *) CurrPtr)->State = VAR_ADDED;\r
924 CurrPtr += VariableSize;\r
925 if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
926 HwErrVariableTotalSize += VariableSize;\r
927 } else if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
928 CommonVariableTotalSize += VariableSize;\r
929 if (IsUserVariable (Variable)) {\r
930 CommonUserVariableTotalSize += VariableSize;\r
931 }\r
932 }\r
933 }\r
934 }\r
935\r
936 Variable = NextVariable;\r
937 }\r
938\r
939 //\r
940 // Install the new variable if it is not NULL.\r
941 //\r
942 if (NewVariable != NULL) {\r
943 if ((UINTN) (CurrPtr - ValidBuffer) + NewVariableSize > VariableStoreHeader->Size) {\r
944 //\r
945 // No enough space to store the new variable.\r
946 //\r
947 Status = EFI_OUT_OF_RESOURCES;\r
948 goto Done;\r
949 }\r
950 if (!IsVolatile) {\r
951 if ((NewVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
952 HwErrVariableTotalSize += NewVariableSize;\r
953 } else if ((NewVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
954 CommonVariableTotalSize += NewVariableSize;\r
955 if (IsUserVariable (NewVariable)) {\r
956 CommonUserVariableTotalSize += NewVariableSize;\r
957 }\r
958 }\r
959 if ((HwErrVariableTotalSize > PcdGet32 (PcdHwErrStorageSize)) ||\r
960 (CommonVariableTotalSize > mVariableModuleGlobal->CommonVariableSpace) ||\r
961 (CommonUserVariableTotalSize > mVariableModuleGlobal->CommonMaxUserVariableSpace)) {\r
962 //\r
963 // No enough space to store the new variable by NV or NV+HR attribute.\r
964 //\r
965 Status = EFI_OUT_OF_RESOURCES;\r
966 goto Done;\r
967 }\r
968 }\r
969\r
970 CopyMem (CurrPtr, (UINT8 *) NewVariable, NewVariableSize);\r
971 ((VARIABLE_HEADER *) CurrPtr)->State = VAR_ADDED;\r
972 if (UpdatingVariable != NULL) {\r
973 UpdatingPtrTrack->CurrPtr = (VARIABLE_HEADER *)((UINTN)UpdatingPtrTrack->StartPtr + ((UINTN)CurrPtr - (UINTN)GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer)));\r
974 UpdatingPtrTrack->InDeletedTransitionPtr = NULL;\r
975 }\r
976 CurrPtr += NewVariableSize;\r
977 }\r
978\r
979 if (IsVolatile) {\r
980 //\r
981 // If volatile variable store, just copy valid buffer.\r
982 //\r
983 SetMem ((UINT8 *) (UINTN) VariableBase, VariableStoreHeader->Size, 0xff);\r
984 CopyMem ((UINT8 *) (UINTN) VariableBase, ValidBuffer, (UINTN) (CurrPtr - ValidBuffer));\r
985 *LastVariableOffset = (UINTN) (CurrPtr - ValidBuffer);\r
986 Status = EFI_SUCCESS;\r
987 } else {\r
988 //\r
989 // If non-volatile variable store, perform FTW here.\r
990 //\r
991 Status = FtwVariableSpace (\r
992 VariableBase,\r
993 (VARIABLE_STORE_HEADER *) ValidBuffer\r
994 );\r
995 if (!EFI_ERROR (Status)) {\r
996 *LastVariableOffset = (UINTN) (CurrPtr - ValidBuffer);\r
997 mVariableModuleGlobal->HwErrVariableTotalSize = HwErrVariableTotalSize;\r
998 mVariableModuleGlobal->CommonVariableTotalSize = CommonVariableTotalSize;\r
999 mVariableModuleGlobal->CommonUserVariableTotalSize = CommonUserVariableTotalSize;\r
1000 } else {\r
1001 Variable = GetStartPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableBase);\r
1002 while (IsValidVariableHeader (Variable, GetEndPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableBase))) {\r
1003 NextVariable = GetNextVariablePtr (Variable);\r
1004 VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
1005 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
1006 mVariableModuleGlobal->HwErrVariableTotalSize += VariableSize;\r
1007 } else if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
1008 mVariableModuleGlobal->CommonVariableTotalSize += VariableSize;\r
1009 if (IsUserVariable (Variable)) {\r
1010 mVariableModuleGlobal->CommonUserVariableTotalSize += VariableSize;\r
1011 }\r
1012 }\r
1013\r
1014 Variable = NextVariable;\r
1015 }\r
1016 *LastVariableOffset = (UINTN) Variable - (UINTN) VariableBase;\r
1017 }\r
1018 }\r
1019\r
1020Done:\r
1021 if (IsVolatile) {\r
1022 FreePool (ValidBuffer);\r
1023 } else {\r
1024 //\r
1025 // For NV variable reclaim, we use mNvVariableCache as the buffer, so copy the data back.\r
1026 //\r
1027 CopyMem (mNvVariableCache, (UINT8 *)(UINTN)VariableBase, VariableStoreHeader->Size);\r
1028 }\r
1029\r
1030 return Status;\r
1031}\r
1032\r
1033/**\r
1034 Find the variable in the specified variable store.\r
1035\r
1036 @param VariableName Name of the variable to be found\r
1037 @param VendorGuid Vendor GUID to be found.\r
1038 @param IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute\r
1039 check at runtime when searching variable.\r
1040 @param PtrTrack Variable Track Pointer structure that contains Variable Information.\r
1041\r
1042 @retval EFI_SUCCESS Variable found successfully\r
1043 @retval EFI_NOT_FOUND Variable not found\r
1044**/\r
1045EFI_STATUS\r
1046FindVariableEx (\r
1047 IN CHAR16 *VariableName,\r
1048 IN EFI_GUID *VendorGuid,\r
1049 IN BOOLEAN IgnoreRtCheck,\r
1050 IN OUT VARIABLE_POINTER_TRACK *PtrTrack\r
1051 )\r
1052{\r
1053 VARIABLE_HEADER *InDeletedVariable;\r
1054 VOID *Point;\r
1055\r
1056 PtrTrack->InDeletedTransitionPtr = NULL;\r
1057\r
1058 //\r
1059 // Find the variable by walk through HOB, volatile and non-volatile variable store.\r
1060 //\r
1061 InDeletedVariable = NULL;\r
1062\r
1063 for ( PtrTrack->CurrPtr = PtrTrack->StartPtr\r
1064 ; IsValidVariableHeader (PtrTrack->CurrPtr, PtrTrack->EndPtr)\r
1065 ; PtrTrack->CurrPtr = GetNextVariablePtr (PtrTrack->CurrPtr)\r
1066 ) {\r
1067 if (PtrTrack->CurrPtr->State == VAR_ADDED || \r
1068 PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)\r
1069 ) {\r
1070 if (IgnoreRtCheck || !AtRuntime () || ((PtrTrack->CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {\r
1071 if (VariableName[0] == 0) {\r
1072 if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
1073 InDeletedVariable = PtrTrack->CurrPtr;\r
1074 } else {\r
1075 PtrTrack->InDeletedTransitionPtr = InDeletedVariable;\r
1076 return EFI_SUCCESS;\r
1077 }\r
1078 } else {\r
1079 if (CompareGuid (VendorGuid, &PtrTrack->CurrPtr->VendorGuid)) {\r
1080 Point = (VOID *) GetVariableNamePtr (PtrTrack->CurrPtr);\r
1081\r
1082 ASSERT (NameSizeOfVariable (PtrTrack->CurrPtr) != 0);\r
1083 if (CompareMem (VariableName, Point, NameSizeOfVariable (PtrTrack->CurrPtr)) == 0) {\r
1084 if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
1085 InDeletedVariable = PtrTrack->CurrPtr;\r
1086 } else {\r
1087 PtrTrack->InDeletedTransitionPtr = InDeletedVariable;\r
1088 return EFI_SUCCESS;\r
1089 }\r
1090 }\r
1091 }\r
1092 }\r
1093 }\r
1094 }\r
1095 }\r
1096\r
1097 PtrTrack->CurrPtr = InDeletedVariable;\r
1098 return (PtrTrack->CurrPtr == NULL) ? EFI_NOT_FOUND : EFI_SUCCESS;\r
1099}\r
1100\r
1101\r
1102/**\r
1103 Finds variable in storage blocks of volatile and non-volatile storage areas.\r
1104\r
1105 This code finds variable in storage blocks of volatile and non-volatile storage areas.\r
1106 If VariableName is an empty string, then we just return the first\r
1107 qualified variable without comparing VariableName and VendorGuid.\r
1108 If IgnoreRtCheck is TRUE, then we ignore the EFI_VARIABLE_RUNTIME_ACCESS attribute check\r
1109 at runtime when searching existing variable, only VariableName and VendorGuid are compared.\r
1110 Otherwise, variables without EFI_VARIABLE_RUNTIME_ACCESS are not visible at runtime.\r
1111\r
1112 @param VariableName Name of the variable to be found.\r
1113 @param VendorGuid Vendor GUID to be found.\r
1114 @param PtrTrack VARIABLE_POINTER_TRACK structure for output,\r
1115 including the range searched and the target position.\r
1116 @param Global Pointer to VARIABLE_GLOBAL structure, including\r
1117 base of volatile variable storage area, base of\r
1118 NV variable storage area, and a lock.\r
1119 @param IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute\r
1120 check at runtime when searching variable.\r
1121\r
1122 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while\r
1123 VendorGuid is NULL.\r
1124 @retval EFI_SUCCESS Variable successfully found.\r
1125 @retval EFI_NOT_FOUND Variable not found\r
1126\r
1127**/\r
1128EFI_STATUS\r
1129FindVariable (\r
1130 IN CHAR16 *VariableName,\r
1131 IN EFI_GUID *VendorGuid,\r
1132 OUT VARIABLE_POINTER_TRACK *PtrTrack,\r
1133 IN VARIABLE_GLOBAL *Global,\r
1134 IN BOOLEAN IgnoreRtCheck\r
1135 )\r
1136{\r
1137 EFI_STATUS Status;\r
1138 VARIABLE_STORE_HEADER *VariableStoreHeader[VariableStoreTypeMax];\r
1139 VARIABLE_STORE_TYPE Type;\r
1140\r
1141 if (VariableName[0] != 0 && VendorGuid == NULL) {\r
1142 return EFI_INVALID_PARAMETER;\r
1143 }\r
1144\r
1145 //\r
1146 // 0: Volatile, 1: HOB, 2: Non-Volatile.\r
1147 // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName\r
1148 // make use of this mapping to implement search algorithm.\r
1149 //\r
1150 VariableStoreHeader[VariableStoreTypeVolatile] = (VARIABLE_STORE_HEADER *) (UINTN) Global->VolatileVariableBase;\r
1151 VariableStoreHeader[VariableStoreTypeHob] = (VARIABLE_STORE_HEADER *) (UINTN) Global->HobVariableBase;\r
1152 VariableStoreHeader[VariableStoreTypeNv] = mNvVariableCache;\r
1153\r
1154 //\r
1155 // Find the variable by walk through HOB, volatile and non-volatile variable store.\r
1156 //\r
1157 for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {\r
1158 if (VariableStoreHeader[Type] == NULL) {\r
1159 continue;\r
1160 }\r
1161\r
1162 PtrTrack->StartPtr = GetStartPointer (VariableStoreHeader[Type]);\r
1163 PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader[Type]);\r
1164 PtrTrack->Volatile = (BOOLEAN) (Type == VariableStoreTypeVolatile);\r
1165\r
1166 Status = FindVariableEx (VariableName, VendorGuid, IgnoreRtCheck, PtrTrack);\r
1167 if (!EFI_ERROR (Status)) {\r
1168 return Status;\r
1169 }\r
1170 }\r
1171 return EFI_NOT_FOUND;\r
1172}\r
1173\r
1174/**\r
1175 Get index from supported language codes according to language string.\r
1176\r
1177 This code is used to get corresponding index in supported language codes. It can handle\r
1178 RFC4646 and ISO639 language tags.\r
1179 In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.\r
1180 In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.\r
1181\r
1182 For example:\r
1183 SupportedLang = "engfraengfra"\r
1184 Lang = "eng"\r
1185 Iso639Language = TRUE\r
1186 The return value is "0".\r
1187 Another example:\r
1188 SupportedLang = "en;fr;en-US;fr-FR"\r
1189 Lang = "fr-FR"\r
1190 Iso639Language = FALSE\r
1191 The return value is "3".\r
1192\r
1193 @param SupportedLang Platform supported language codes.\r
1194 @param Lang Configured language.\r
1195 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.\r
1196\r
1197 @retval The index of language in the language codes.\r
1198\r
1199**/\r
1200UINTN\r
1201GetIndexFromSupportedLangCodes(\r
1202 IN CHAR8 *SupportedLang,\r
1203 IN CHAR8 *Lang,\r
1204 IN BOOLEAN Iso639Language\r
1205 ) \r
1206{\r
1207 UINTN Index;\r
1208 UINTN CompareLength;\r
1209 UINTN LanguageLength;\r
1210\r
1211 if (Iso639Language) {\r
1212 CompareLength = ISO_639_2_ENTRY_SIZE;\r
1213 for (Index = 0; Index < AsciiStrLen (SupportedLang); Index += CompareLength) {\r
1214 if (AsciiStrnCmp (Lang, SupportedLang + Index, CompareLength) == 0) {\r
1215 //\r
1216 // Successfully find the index of Lang string in SupportedLang string.\r
1217 //\r
1218 Index = Index / CompareLength;\r
1219 return Index;\r
1220 }\r
1221 }\r
1222 ASSERT (FALSE);\r
1223 return 0;\r
1224 } else {\r
1225 //\r
1226 // Compare RFC4646 language code\r
1227 //\r
1228 Index = 0;\r
1229 for (LanguageLength = 0; Lang[LanguageLength] != '\0'; LanguageLength++);\r
1230\r
1231 for (Index = 0; *SupportedLang != '\0'; Index++, SupportedLang += CompareLength) {\r
1232 //\r
1233 // Skip ';' characters in SupportedLang\r
1234 //\r
1235 for (; *SupportedLang != '\0' && *SupportedLang == ';'; SupportedLang++);\r
1236 //\r
1237 // Determine the length of the next language code in SupportedLang\r
1238 //\r
1239 for (CompareLength = 0; SupportedLang[CompareLength] != '\0' && SupportedLang[CompareLength] != ';'; CompareLength++);\r
1240 \r
1241 if ((CompareLength == LanguageLength) && \r
1242 (AsciiStrnCmp (Lang, SupportedLang, CompareLength) == 0)) {\r
1243 //\r
1244 // Successfully find the index of Lang string in SupportedLang string.\r
1245 //\r
1246 return Index;\r
1247 }\r
1248 }\r
1249 ASSERT (FALSE);\r
1250 return 0;\r
1251 }\r
1252}\r
1253\r
1254/**\r
1255 Get language string from supported language codes according to index.\r
1256\r
1257 This code is used to get corresponding language strings in supported language codes. It can handle\r
1258 RFC4646 and ISO639 language tags.\r
1259 In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.\r
1260 In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.\r
1261\r
1262 For example:\r
1263 SupportedLang = "engfraengfra"\r
1264 Index = "1"\r
1265 Iso639Language = TRUE\r
1266 The return value is "fra".\r
1267 Another example:\r
1268 SupportedLang = "en;fr;en-US;fr-FR"\r
1269 Index = "1"\r
1270 Iso639Language = FALSE\r
1271 The return value is "fr".\r
1272\r
1273 @param SupportedLang Platform supported language codes.\r
1274 @param Index The index in supported language codes.\r
1275 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.\r
1276\r
1277 @retval The language string in the language codes.\r
1278\r
1279**/\r
1280CHAR8 *\r
1281GetLangFromSupportedLangCodes (\r
1282 IN CHAR8 *SupportedLang,\r
1283 IN UINTN Index,\r
1284 IN BOOLEAN Iso639Language\r
1285)\r
1286{\r
1287 UINTN SubIndex;\r
1288 UINTN CompareLength;\r
1289 CHAR8 *Supported;\r
1290\r
1291 SubIndex = 0;\r
1292 Supported = SupportedLang;\r
1293 if (Iso639Language) {\r
1294 //\r
1295 // According to the index of Lang string in SupportedLang string to get the language.\r
1296 // This code will be invoked in RUNTIME, therefore there is not a memory allocate/free operation.\r
1297 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.\r
1298 //\r
1299 CompareLength = ISO_639_2_ENTRY_SIZE;\r
1300 mVariableModuleGlobal->Lang[CompareLength] = '\0';\r
1301 return CopyMem (mVariableModuleGlobal->Lang, SupportedLang + Index * CompareLength, CompareLength);\r
1302\r
1303 } else {\r
1304 while (TRUE) {\r
1305 //\r
1306 // Take semicolon as delimitation, sequentially traverse supported language codes.\r
1307 //\r
1308 for (CompareLength = 0; *Supported != ';' && *Supported != '\0'; CompareLength++) {\r
1309 Supported++;\r
1310 }\r
1311 if ((*Supported == '\0') && (SubIndex != Index)) {\r
1312 //\r
1313 // Have completed the traverse, but not find corrsponding string.\r
1314 // This case is not allowed to happen.\r
1315 //\r
1316 ASSERT(FALSE);\r
1317 return NULL;\r
1318 }\r
1319 if (SubIndex == Index) {\r
1320 //\r
1321 // According to the index of Lang string in SupportedLang string to get the language.\r
1322 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.\r
1323 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.\r
1324 //\r
1325 mVariableModuleGlobal->PlatformLang[CompareLength] = '\0';\r
1326 return CopyMem (mVariableModuleGlobal->PlatformLang, Supported - CompareLength, CompareLength);\r
1327 }\r
1328 SubIndex++;\r
1329\r
1330 //\r
1331 // Skip ';' characters in Supported\r
1332 //\r
1333 for (; *Supported != '\0' && *Supported == ';'; Supported++);\r
1334 }\r
1335 }\r
1336}\r
1337\r
1338/**\r
1339 Returns a pointer to an allocated buffer that contains the best matching language \r
1340 from a set of supported languages. \r
1341 \r
1342 This function supports both ISO 639-2 and RFC 4646 language codes, but language \r
1343 code types may not be mixed in a single call to this function. This function\r
1344 supports a variable argument list that allows the caller to pass in a prioritized\r
1345 list of language codes to test against all the language codes in SupportedLanguages.\r
1346\r
1347 If SupportedLanguages is NULL, then ASSERT().\r
1348\r
1349 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that\r
1350 contains a set of language codes in the format \r
1351 specified by Iso639Language.\r
1352 @param[in] Iso639Language If TRUE, then all language codes are assumed to be\r
1353 in ISO 639-2 format. If FALSE, then all language\r
1354 codes are assumed to be in RFC 4646 language format\r
1355 @param[in] ... A variable argument list that contains pointers to \r
1356 Null-terminated ASCII strings that contain one or more\r
1357 language codes in the format specified by Iso639Language.\r
1358 The first language code from each of these language\r
1359 code lists is used to determine if it is an exact or\r
1360 close match to any of the language codes in \r
1361 SupportedLanguages. Close matches only apply to RFC 4646\r
1362 language codes, and the matching algorithm from RFC 4647\r
1363 is used to determine if a close match is present. If \r
1364 an exact or close match is found, then the matching\r
1365 language code from SupportedLanguages is returned. If\r
1366 no matches are found, then the next variable argument\r
1367 parameter is evaluated. The variable argument list \r
1368 is terminated by a NULL.\r
1369\r
1370 @retval NULL The best matching language could not be found in SupportedLanguages.\r
1371 @retval NULL There are not enough resources available to return the best matching \r
1372 language.\r
1373 @retval Other A pointer to a Null-terminated ASCII string that is the best matching \r
1374 language in SupportedLanguages.\r
1375\r
1376**/\r
1377CHAR8 *\r
1378EFIAPI\r
1379VariableGetBestLanguage (\r
1380 IN CONST CHAR8 *SupportedLanguages, \r
1381 IN BOOLEAN Iso639Language,\r
1382 ...\r
1383 )\r
1384{\r
1385 VA_LIST Args;\r
1386 CHAR8 *Language;\r
1387 UINTN CompareLength;\r
1388 UINTN LanguageLength;\r
1389 CONST CHAR8 *Supported;\r
1390 CHAR8 *Buffer;\r
1391\r
1392 ASSERT (SupportedLanguages != NULL);\r
1393\r
1394 VA_START (Args, Iso639Language);\r
1395 while ((Language = VA_ARG (Args, CHAR8 *)) != NULL) {\r
1396 //\r
1397 // Default to ISO 639-2 mode\r
1398 //\r
1399 CompareLength = 3;\r
1400 LanguageLength = MIN (3, AsciiStrLen (Language));\r
1401\r
1402 //\r
1403 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language\r
1404 //\r
1405 if (!Iso639Language) {\r
1406 for (LanguageLength = 0; Language[LanguageLength] != 0 && Language[LanguageLength] != ';'; LanguageLength++);\r
1407 }\r
1408\r
1409 //\r
1410 // Trim back the length of Language used until it is empty\r
1411 //\r
1412 while (LanguageLength > 0) {\r
1413 //\r
1414 // Loop through all language codes in SupportedLanguages\r
1415 //\r
1416 for (Supported = SupportedLanguages; *Supported != '\0'; Supported += CompareLength) {\r
1417 //\r
1418 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages\r
1419 //\r
1420 if (!Iso639Language) {\r
1421 //\r
1422 // Skip ';' characters in Supported\r
1423 //\r
1424 for (; *Supported != '\0' && *Supported == ';'; Supported++);\r
1425 //\r
1426 // Determine the length of the next language code in Supported\r
1427 //\r
1428 for (CompareLength = 0; Supported[CompareLength] != 0 && Supported[CompareLength] != ';'; CompareLength++);\r
1429 //\r
1430 // If Language is longer than the Supported, then skip to the next language\r
1431 //\r
1432 if (LanguageLength > CompareLength) {\r
1433 continue;\r
1434 }\r
1435 }\r
1436 //\r
1437 // See if the first LanguageLength characters in Supported match Language\r
1438 //\r
1439 if (AsciiStrnCmp (Supported, Language, LanguageLength) == 0) {\r
1440 VA_END (Args);\r
1441\r
1442 Buffer = Iso639Language ? mVariableModuleGlobal->Lang : mVariableModuleGlobal->PlatformLang;\r
1443 Buffer[CompareLength] = '\0';\r
1444 return CopyMem (Buffer, Supported, CompareLength);\r
1445 }\r
1446 }\r
1447\r
1448 if (Iso639Language) {\r
1449 //\r
1450 // If ISO 639 mode, then each language can only be tested once\r
1451 //\r
1452 LanguageLength = 0;\r
1453 } else {\r
1454 //\r
1455 // If RFC 4646 mode, then trim Language from the right to the next '-' character \r
1456 //\r
1457 for (LanguageLength--; LanguageLength > 0 && Language[LanguageLength] != '-'; LanguageLength--);\r
1458 }\r
1459 }\r
1460 }\r
1461 VA_END (Args);\r
1462\r
1463 //\r
1464 // No matches were found \r
1465 //\r
1466 return NULL;\r
1467}\r
1468\r
1469/**\r
1470 This function is to check if the remaining variable space is enough to set\r
1471 all Variables from argument list successfully. The purpose of the check\r
1472 is to keep the consistency of the Variables to be in variable storage.\r
1473\r
1474 Note: Variables are assumed to be in same storage.\r
1475 The set sequence of Variables will be same with the sequence of VariableEntry from argument list,\r
1476 so follow the argument sequence to check the Variables.\r
1477\r
1478 @param[in] Attributes Variable attributes for Variable entries.\r
1479 @param ... The variable argument list with type VARIABLE_ENTRY_CONSISTENCY *.\r
1480 A NULL terminates the list. The VariableSize of \r
1481 VARIABLE_ENTRY_CONSISTENCY is the variable data size as input.\r
1482 It will be changed to variable total size as output.\r
1483\r
1484 @retval TRUE Have enough variable space to set the Variables successfully.\r
1485 @retval FALSE No enough variable space to set the Variables successfully.\r
1486\r
1487**/\r
1488BOOLEAN\r
1489EFIAPI\r
1490CheckRemainingSpaceForConsistency (\r
1491 IN UINT32 Attributes,\r
1492 ...\r
1493 )\r
1494{\r
1495 EFI_STATUS Status;\r
1496 VA_LIST Args;\r
1497 VARIABLE_ENTRY_CONSISTENCY *VariableEntry;\r
1498 UINT64 MaximumVariableStorageSize;\r
1499 UINT64 RemainingVariableStorageSize;\r
1500 UINT64 MaximumVariableSize;\r
1501 UINTN TotalNeededSize;\r
1502 UINTN OriginalVarSize;\r
1503 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
1504 VARIABLE_POINTER_TRACK VariablePtrTrack;\r
1505 VARIABLE_HEADER *NextVariable;\r
1506 UINTN VarNameSize;\r
1507 UINTN VarDataSize;\r
1508\r
1509 //\r
1510 // Non-Volatile related.\r
1511 //\r
1512 VariableStoreHeader = mNvVariableCache;\r
1513\r
1514 Status = VariableServiceQueryVariableInfoInternal (\r
1515 Attributes,\r
1516 &MaximumVariableStorageSize,\r
1517 &RemainingVariableStorageSize,\r
1518 &MaximumVariableSize\r
1519 );\r
1520 ASSERT_EFI_ERROR (Status);\r
1521\r
1522 TotalNeededSize = 0;\r
1523 VA_START (Args, Attributes);\r
1524 VariableEntry = VA_ARG (Args, VARIABLE_ENTRY_CONSISTENCY *);\r
1525 while (VariableEntry != NULL) {\r
1526 //\r
1527 // Calculate variable total size.\r
1528 //\r
1529 VarNameSize = StrSize (VariableEntry->Name);\r
1530 VarNameSize += GET_PAD_SIZE (VarNameSize);\r
1531 VarDataSize = VariableEntry->VariableSize;\r
1532 VarDataSize += GET_PAD_SIZE (VarDataSize);\r
1533 VariableEntry->VariableSize = HEADER_ALIGN (sizeof (VARIABLE_HEADER) + VarNameSize + VarDataSize);\r
1534\r
1535 TotalNeededSize += VariableEntry->VariableSize;\r
1536 VariableEntry = VA_ARG (Args, VARIABLE_ENTRY_CONSISTENCY *);\r
1537 }\r
1538 VA_END (Args);\r
1539\r
1540 if (RemainingVariableStorageSize >= TotalNeededSize) {\r
1541 //\r
1542 // Already have enough space.\r
1543 //\r
1544 return TRUE;\r
1545 } else if (AtRuntime ()) {\r
1546 //\r
1547 // At runtime, no reclaim.\r
1548 // The original variable space of Variables can't be reused.\r
1549 //\r
1550 return FALSE;\r
1551 }\r
1552\r
1553 VA_START (Args, Attributes);\r
1554 VariableEntry = VA_ARG (Args, VARIABLE_ENTRY_CONSISTENCY *);\r
1555 while (VariableEntry != NULL) {\r
1556 //\r
1557 // Check if Variable[Index] has been present and get its size.\r
1558 //\r
1559 OriginalVarSize = 0;\r
1560 VariablePtrTrack.StartPtr = GetStartPointer (VariableStoreHeader);\r
1561 VariablePtrTrack.EndPtr = GetEndPointer (VariableStoreHeader);\r
1562 Status = FindVariableEx (\r
1563 VariableEntry->Name,\r
1564 VariableEntry->Guid,\r
1565 FALSE,\r
1566 &VariablePtrTrack\r
1567 );\r
1568 if (!EFI_ERROR (Status)) {\r
1569 //\r
1570 // Get size of Variable[Index].\r
1571 //\r
1572 NextVariable = GetNextVariablePtr (VariablePtrTrack.CurrPtr);\r
1573 OriginalVarSize = (UINTN) NextVariable - (UINTN) VariablePtrTrack.CurrPtr;\r
1574 //\r
1575 // Add the original size of Variable[Index] to remaining variable storage size.\r
1576 //\r
1577 RemainingVariableStorageSize += OriginalVarSize;\r
1578 }\r
1579 if (VariableEntry->VariableSize > RemainingVariableStorageSize) {\r
1580 //\r
1581 // No enough space for Variable[Index].\r
1582 //\r
1583 VA_END (Args);\r
1584 return FALSE;\r
1585 }\r
1586 //\r
1587 // Sub the (new) size of Variable[Index] from remaining variable storage size.\r
1588 //\r
1589 RemainingVariableStorageSize -= VariableEntry->VariableSize;\r
1590 VariableEntry = VA_ARG (Args, VARIABLE_ENTRY_CONSISTENCY *);\r
1591 }\r
1592 VA_END (Args);\r
1593\r
1594 return TRUE;\r
1595}\r
1596\r
1597/**\r
1598 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.\r
1599\r
1600 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.\r
1601\r
1602 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,\r
1603 and are read-only. Therefore, in variable driver, only store the original value for other use.\r
1604\r
1605 @param[in] VariableName Name of variable.\r
1606\r
1607 @param[in] Data Variable data.\r
1608\r
1609 @param[in] DataSize Size of data. 0 means delete.\r
1610\r
1611 @retval EFI_SUCCESS The update operation is successful or ignored.\r
1612 @retval EFI_WRITE_PROTECTED Update PlatformLangCodes/LangCodes at runtime.\r
1613 @retval EFI_OUT_OF_RESOURCES No enough variable space to do the update operation.\r
1614 @retval Others Other errors happened during the update operation.\r
1615\r
1616**/\r
1617EFI_STATUS\r
1618AutoUpdateLangVariable (\r
1619 IN CHAR16 *VariableName,\r
1620 IN VOID *Data,\r
1621 IN UINTN DataSize\r
1622 )\r
1623{\r
1624 EFI_STATUS Status;\r
1625 CHAR8 *BestPlatformLang;\r
1626 CHAR8 *BestLang;\r
1627 UINTN Index;\r
1628 UINT32 Attributes;\r
1629 VARIABLE_POINTER_TRACK Variable;\r
1630 BOOLEAN SetLanguageCodes;\r
1631 VARIABLE_ENTRY_CONSISTENCY VariableEntry[2];\r
1632\r
1633 //\r
1634 // Don't do updates for delete operation\r
1635 //\r
1636 if (DataSize == 0) {\r
1637 return EFI_SUCCESS;\r
1638 }\r
1639\r
1640 SetLanguageCodes = FALSE;\r
1641\r
1642 if (StrCmp (VariableName, EFI_PLATFORM_LANG_CODES_VARIABLE_NAME) == 0) {\r
1643 //\r
1644 // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.\r
1645 //\r
1646 if (AtRuntime ()) {\r
1647 return EFI_WRITE_PROTECTED;\r
1648 }\r
1649\r
1650 SetLanguageCodes = TRUE;\r
1651\r
1652 //\r
1653 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only\r
1654 // Therefore, in variable driver, only store the original value for other use.\r
1655 //\r
1656 if (mVariableModuleGlobal->PlatformLangCodes != NULL) {\r
1657 FreePool (mVariableModuleGlobal->PlatformLangCodes);\r
1658 }\r
1659 mVariableModuleGlobal->PlatformLangCodes = AllocateRuntimeCopyPool (DataSize, Data);\r
1660 ASSERT (mVariableModuleGlobal->PlatformLangCodes != NULL);\r
1661\r
1662 //\r
1663 // PlatformLang holds a single language from PlatformLangCodes, \r
1664 // so the size of PlatformLangCodes is enough for the PlatformLang.\r
1665 //\r
1666 if (mVariableModuleGlobal->PlatformLang != NULL) {\r
1667 FreePool (mVariableModuleGlobal->PlatformLang);\r
1668 }\r
1669 mVariableModuleGlobal->PlatformLang = AllocateRuntimePool (DataSize);\r
1670 ASSERT (mVariableModuleGlobal->PlatformLang != NULL);\r
1671\r
1672 } else if (StrCmp (VariableName, EFI_LANG_CODES_VARIABLE_NAME) == 0) {\r
1673 //\r
1674 // LangCodes is a volatile variable, so it can not be updated at runtime.\r
1675 //\r
1676 if (AtRuntime ()) {\r
1677 return EFI_WRITE_PROTECTED;\r
1678 }\r
1679\r
1680 SetLanguageCodes = TRUE;\r
1681\r
1682 //\r
1683 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only\r
1684 // Therefore, in variable driver, only store the original value for other use.\r
1685 //\r
1686 if (mVariableModuleGlobal->LangCodes != NULL) {\r
1687 FreePool (mVariableModuleGlobal->LangCodes);\r
1688 }\r
1689 mVariableModuleGlobal->LangCodes = AllocateRuntimeCopyPool (DataSize, Data);\r
1690 ASSERT (mVariableModuleGlobal->LangCodes != NULL);\r
1691 }\r
1692\r
1693 if (SetLanguageCodes \r
1694 && (mVariableModuleGlobal->PlatformLangCodes != NULL)\r
1695 && (mVariableModuleGlobal->LangCodes != NULL)) {\r
1696 //\r
1697 // Update Lang if PlatformLang is already set\r
1698 // Update PlatformLang if Lang is already set\r
1699 //\r
1700 Status = FindVariable (EFI_PLATFORM_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
1701 if (!EFI_ERROR (Status)) {\r
1702 //\r
1703 // Update Lang\r
1704 //\r
1705 VariableName = EFI_PLATFORM_LANG_VARIABLE_NAME;\r
1706 Data = GetVariableDataPtr (Variable.CurrPtr);\r
1707 DataSize = Variable.CurrPtr->DataSize;\r
1708 } else {\r
1709 Status = FindVariable (EFI_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
1710 if (!EFI_ERROR (Status)) {\r
1711 //\r
1712 // Update PlatformLang\r
1713 //\r
1714 VariableName = EFI_LANG_VARIABLE_NAME;\r
1715 Data = GetVariableDataPtr (Variable.CurrPtr);\r
1716 DataSize = Variable.CurrPtr->DataSize;\r
1717 } else {\r
1718 //\r
1719 // Neither PlatformLang nor Lang is set, directly return\r
1720 //\r
1721 return EFI_SUCCESS;\r
1722 }\r
1723 }\r
1724 }\r
1725\r
1726 Status = EFI_SUCCESS;\r
1727\r
1728 //\r
1729 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.\r
1730 //\r
1731 Attributes = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;\r
1732\r
1733 if (StrCmp (VariableName, EFI_PLATFORM_LANG_VARIABLE_NAME) == 0) {\r
1734 //\r
1735 // Update Lang when PlatformLangCodes/LangCodes were set.\r
1736 //\r
1737 if ((mVariableModuleGlobal->PlatformLangCodes != NULL) && (mVariableModuleGlobal->LangCodes != NULL)) {\r
1738 //\r
1739 // When setting PlatformLang, firstly get most matched language string from supported language codes.\r
1740 //\r
1741 BestPlatformLang = VariableGetBestLanguage (mVariableModuleGlobal->PlatformLangCodes, FALSE, Data, NULL);\r
1742 if (BestPlatformLang != NULL) {\r
1743 //\r
1744 // Get the corresponding index in language codes.\r
1745 //\r
1746 Index = GetIndexFromSupportedLangCodes (mVariableModuleGlobal->PlatformLangCodes, BestPlatformLang, FALSE);\r
1747\r
1748 //\r
1749 // Get the corresponding ISO639 language tag according to RFC4646 language tag.\r
1750 //\r
1751 BestLang = GetLangFromSupportedLangCodes (mVariableModuleGlobal->LangCodes, Index, TRUE);\r
1752\r
1753 //\r
1754 // Check the variable space for both Lang and PlatformLang variable.\r
1755 //\r
1756 VariableEntry[0].VariableSize = ISO_639_2_ENTRY_SIZE + 1;\r
1757 VariableEntry[0].Guid = &gEfiGlobalVariableGuid;\r
1758 VariableEntry[0].Name = EFI_LANG_VARIABLE_NAME;\r
1759 \r
1760 VariableEntry[1].VariableSize = AsciiStrSize (BestPlatformLang);\r
1761 VariableEntry[1].Guid = &gEfiGlobalVariableGuid;\r
1762 VariableEntry[1].Name = EFI_PLATFORM_LANG_VARIABLE_NAME;\r
1763 if (!CheckRemainingSpaceForConsistency (VARIABLE_ATTRIBUTE_NV_BS_RT, &VariableEntry[0], &VariableEntry[1], NULL)) {\r
1764 //\r
1765 // No enough variable space to set both Lang and PlatformLang successfully.\r
1766 //\r
1767 Status = EFI_OUT_OF_RESOURCES;\r
1768 } else {\r
1769 //\r
1770 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.\r
1771 //\r
1772 FindVariable (EFI_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
1773\r
1774 Status = UpdateVariable (EFI_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, BestLang,\r
1775 ISO_639_2_ENTRY_SIZE + 1, Attributes, &Variable);\r
1776 }\r
1777\r
1778 DEBUG ((EFI_D_INFO, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a Status: %r\n", BestPlatformLang, BestLang, Status));\r
1779 }\r
1780 }\r
1781\r
1782 } else if (StrCmp (VariableName, EFI_LANG_VARIABLE_NAME) == 0) {\r
1783 //\r
1784 // Update PlatformLang when PlatformLangCodes/LangCodes were set.\r
1785 //\r
1786 if ((mVariableModuleGlobal->PlatformLangCodes != NULL) && (mVariableModuleGlobal->LangCodes != NULL)) {\r
1787 //\r
1788 // When setting Lang, firstly get most matched language string from supported language codes.\r
1789 //\r
1790 BestLang = VariableGetBestLanguage (mVariableModuleGlobal->LangCodes, TRUE, Data, NULL);\r
1791 if (BestLang != NULL) {\r
1792 //\r
1793 // Get the corresponding index in language codes.\r
1794 //\r
1795 Index = GetIndexFromSupportedLangCodes (mVariableModuleGlobal->LangCodes, BestLang, TRUE);\r
1796\r
1797 //\r
1798 // Get the corresponding RFC4646 language tag according to ISO639 language tag.\r
1799 //\r
1800 BestPlatformLang = GetLangFromSupportedLangCodes (mVariableModuleGlobal->PlatformLangCodes, Index, FALSE);\r
1801\r
1802 //\r
1803 // Check the variable space for both PlatformLang and Lang variable.\r
1804 //\r
1805 VariableEntry[0].VariableSize = AsciiStrSize (BestPlatformLang);\r
1806 VariableEntry[0].Guid = &gEfiGlobalVariableGuid;\r
1807 VariableEntry[0].Name = EFI_PLATFORM_LANG_VARIABLE_NAME;\r
1808\r
1809 VariableEntry[1].VariableSize = ISO_639_2_ENTRY_SIZE + 1;\r
1810 VariableEntry[1].Guid = &gEfiGlobalVariableGuid;\r
1811 VariableEntry[1].Name = EFI_LANG_VARIABLE_NAME;\r
1812 if (!CheckRemainingSpaceForConsistency (VARIABLE_ATTRIBUTE_NV_BS_RT, &VariableEntry[0], &VariableEntry[1], NULL)) {\r
1813 //\r
1814 // No enough variable space to set both PlatformLang and Lang successfully.\r
1815 //\r
1816 Status = EFI_OUT_OF_RESOURCES;\r
1817 } else {\r
1818 //\r
1819 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.\r
1820 //\r
1821 FindVariable (EFI_PLATFORM_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
1822\r
1823 Status = UpdateVariable (EFI_PLATFORM_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, BestPlatformLang, \r
1824 AsciiStrSize (BestPlatformLang), Attributes, &Variable);\r
1825 }\r
1826\r
1827 DEBUG ((EFI_D_INFO, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a Status: %r\n", BestLang, BestPlatformLang, Status));\r
1828 }\r
1829 }\r
1830 }\r
1831\r
1832 if (SetLanguageCodes) {\r
1833 //\r
1834 // Continue to set PlatformLangCodes or LangCodes.\r
1835 //\r
1836 return EFI_SUCCESS;\r
1837 } else {\r
1838 return Status;\r
1839 }\r
1840}\r
1841\r
1842/**\r
1843 Update the variable region with Variable information. These are the same \r
1844 arguments as the EFI Variable services.\r
1845\r
1846 @param[in] VariableName Name of variable.\r
1847 @param[in] VendorGuid Guid of variable.\r
1848 @param[in] Data Variable data.\r
1849 @param[in] DataSize Size of data. 0 means delete.\r
1850 @param[in] Attributes Attribues of the variable.\r
1851 @param[in, out] CacheVariable The variable information which is used to keep track of variable usage.\r
1852 \r
1853 @retval EFI_SUCCESS The update operation is success.\r
1854 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.\r
1855\r
1856**/\r
1857EFI_STATUS\r
1858UpdateVariable (\r
1859 IN CHAR16 *VariableName,\r
1860 IN EFI_GUID *VendorGuid,\r
1861 IN VOID *Data,\r
1862 IN UINTN DataSize,\r
1863 IN UINT32 Attributes OPTIONAL,\r
1864 IN OUT VARIABLE_POINTER_TRACK *CacheVariable\r
1865 )\r
1866{\r
1867 EFI_STATUS Status;\r
1868 VARIABLE_HEADER *NextVariable;\r
1869 UINTN ScratchSize;\r
1870 UINTN VarNameOffset;\r
1871 UINTN VarDataOffset;\r
1872 UINTN VarNameSize;\r
1873 UINTN VarSize;\r
1874 BOOLEAN Volatile;\r
1875 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r
1876 UINT8 State;\r
1877 VARIABLE_POINTER_TRACK *Variable;\r
1878 VARIABLE_POINTER_TRACK NvVariable;\r
1879 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
1880 UINTN CacheOffset;\r
1881 BOOLEAN IsCommonVariable;\r
1882 BOOLEAN IsCommonUserVariable;\r
1883\r
1884 if ((mVariableModuleGlobal->FvbInstance == NULL) && ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0)) {\r
1885 //\r
1886 // The FVB protocol is not ready. Trying to update NV variable prior to the installation\r
1887 // of EFI_VARIABLE_WRITE_ARCH_PROTOCOL.\r
1888 //\r
1889 return EFI_NOT_AVAILABLE_YET; \r
1890 }\r
1891\r
1892 if ((CacheVariable->CurrPtr == NULL) || CacheVariable->Volatile) {\r
1893 Variable = CacheVariable;\r
1894 } else {\r
1895 //\r
1896 // Update/Delete existing NV variable.\r
1897 // CacheVariable points to the variable in the memory copy of Flash area\r
1898 // Now let Variable points to the same variable in Flash area.\r
1899 //\r
1900 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);\r
1901 Variable = &NvVariable; \r
1902 Variable->StartPtr = GetStartPointer (VariableStoreHeader);\r
1903 Variable->EndPtr = GetEndPointer (VariableStoreHeader);\r
1904 Variable->CurrPtr = (VARIABLE_HEADER *)((UINTN)Variable->StartPtr + ((UINTN)CacheVariable->CurrPtr - (UINTN)CacheVariable->StartPtr));\r
1905 if (CacheVariable->InDeletedTransitionPtr != NULL) {\r
1906 Variable->InDeletedTransitionPtr = (VARIABLE_HEADER *)((UINTN)Variable->StartPtr + ((UINTN)CacheVariable->InDeletedTransitionPtr - (UINTN)CacheVariable->StartPtr));\r
1907 } else {\r
1908 Variable->InDeletedTransitionPtr = NULL;\r
1909 }\r
1910 Variable->Volatile = FALSE;\r
1911 } \r
1912\r
1913 Fvb = mVariableModuleGlobal->FvbInstance;\r
1914\r
1915 if (Variable->CurrPtr != NULL) {\r
1916 //\r
1917 // Update/Delete existing variable.\r
1918 //\r
1919 if (AtRuntime ()) { \r
1920 //\r
1921 // If AtRuntime and the variable is Volatile and Runtime Access, \r
1922 // the volatile is ReadOnly, and SetVariable should be aborted and \r
1923 // return EFI_WRITE_PROTECTED.\r
1924 //\r
1925 if (Variable->Volatile) {\r
1926 Status = EFI_WRITE_PROTECTED;\r
1927 goto Done;\r
1928 }\r
1929 //\r
1930 // Only variable that have NV|RT attributes can be updated/deleted in Runtime.\r
1931 //\r
1932 if (((Variable->CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) || ((Variable->CurrPtr->Attributes & EFI_VARIABLE_NON_VOLATILE) == 0)) {\r
1933 Status = EFI_INVALID_PARAMETER;\r
1934 goto Done; \r
1935 }\r
1936 }\r
1937\r
1938 //\r
1939 // Setting a data variable with no access, or zero DataSize attributes\r
1940 // causes it to be deleted.\r
1941 //\r
1942 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) { \r
1943 if (Variable->InDeletedTransitionPtr != NULL) {\r
1944 //\r
1945 // Both ADDED and IN_DELETED_TRANSITION variable are present,\r
1946 // set IN_DELETED_TRANSITION one to DELETED state first.\r
1947 //\r
1948 State = Variable->InDeletedTransitionPtr->State;\r
1949 State &= VAR_DELETED;\r
1950 Status = UpdateVariableStore (\r
1951 &mVariableModuleGlobal->VariableGlobal,\r
1952 Variable->Volatile,\r
1953 FALSE,\r
1954 Fvb,\r
1955 (UINTN) &Variable->InDeletedTransitionPtr->State,\r
1956 sizeof (UINT8),\r
1957 &State\r
1958 );\r
1959 if (!EFI_ERROR (Status)) {\r
1960 if (!Variable->Volatile) {\r
1961 ASSERT (CacheVariable->InDeletedTransitionPtr != NULL);\r
1962 CacheVariable->InDeletedTransitionPtr->State = State;\r
1963 }\r
1964 } else {\r
1965 goto Done;\r
1966 }\r
1967 }\r
1968\r
1969 State = Variable->CurrPtr->State;\r
1970 State &= VAR_DELETED;\r
1971\r
1972 Status = UpdateVariableStore (\r
1973 &mVariableModuleGlobal->VariableGlobal,\r
1974 Variable->Volatile,\r
1975 FALSE,\r
1976 Fvb,\r
1977 (UINTN) &Variable->CurrPtr->State,\r
1978 sizeof (UINT8),\r
1979 &State\r
1980 ); \r
1981 if (!EFI_ERROR (Status)) {\r
1982 UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, FALSE, TRUE, FALSE);\r
1983 if (!Variable->Volatile) {\r
1984 CacheVariable->CurrPtr->State = State;\r
1985 FlushHobVariableToFlash (VariableName, VendorGuid);\r
1986 }\r
1987 }\r
1988 goto Done; \r
1989 }\r
1990 //\r
1991 // If the variable is marked valid, and the same data has been passed in,\r
1992 // then return to the caller immediately.\r
1993 //\r
1994 if (DataSizeOfVariable (Variable->CurrPtr) == DataSize &&\r
1995 (CompareMem (Data, GetVariableDataPtr (Variable->CurrPtr), DataSize) == 0)) {\r
1996 \r
1997 UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, TRUE, FALSE, FALSE);\r
1998 Status = EFI_SUCCESS;\r
1999 goto Done;\r
2000 } else if ((Variable->CurrPtr->State == VAR_ADDED) ||\r
2001 (Variable->CurrPtr->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {\r
2002\r
2003 //\r
2004 // Mark the old variable as in delete transition.\r
2005 //\r
2006 State = Variable->CurrPtr->State;\r
2007 State &= VAR_IN_DELETED_TRANSITION;\r
2008\r
2009 Status = UpdateVariableStore (\r
2010 &mVariableModuleGlobal->VariableGlobal,\r
2011 Variable->Volatile,\r
2012 FALSE,\r
2013 Fvb,\r
2014 (UINTN) &Variable->CurrPtr->State,\r
2015 sizeof (UINT8),\r
2016 &State\r
2017 ); \r
2018 if (EFI_ERROR (Status)) {\r
2019 goto Done; \r
2020 } \r
2021 if (!Variable->Volatile) {\r
2022 CacheVariable->CurrPtr->State = State;\r
2023 }\r
2024 } \r
2025 } else {\r
2026 //\r
2027 // Not found existing variable. Create a new variable.\r
2028 // \r
2029 \r
2030 //\r
2031 // Make sure we are trying to create a new variable.\r
2032 // Setting a data variable with zero DataSize or no access attributes means to delete it. \r
2033 //\r
2034 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {\r
2035 Status = EFI_NOT_FOUND;\r
2036 goto Done;\r
2037 }\r
2038 \r
2039 //\r
2040 // Only variable have NV|RT attribute can be created in Runtime.\r
2041 //\r
2042 if (AtRuntime () &&\r
2043 (((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) || ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0))) {\r
2044 Status = EFI_INVALID_PARAMETER;\r
2045 goto Done;\r
2046 } \r
2047 }\r
2048\r
2049 //\r
2050 // Function part - create a new variable and copy the data.\r
2051 // Both update a variable and create a variable will come here.\r
2052\r
2053 //\r
2054 // Tricky part: Use scratch data area at the end of volatile variable store\r
2055 // as a temporary storage.\r
2056 //\r
2057 NextVariable = GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase));\r
2058 ScratchSize = MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize));\r
2059\r
2060 SetMem (NextVariable, ScratchSize, 0xff);\r
2061\r
2062 NextVariable->StartId = VARIABLE_DATA;\r
2063 NextVariable->Attributes = Attributes;\r
2064 //\r
2065 // NextVariable->State = VAR_ADDED;\r
2066 //\r
2067 NextVariable->Reserved = 0;\r
2068 VarNameOffset = sizeof (VARIABLE_HEADER);\r
2069 VarNameSize = StrSize (VariableName);\r
2070 CopyMem (\r
2071 (UINT8 *) ((UINTN) NextVariable + VarNameOffset),\r
2072 VariableName,\r
2073 VarNameSize\r
2074 );\r
2075 VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);\r
2076 CopyMem (\r
2077 (UINT8 *) ((UINTN) NextVariable + VarDataOffset),\r
2078 Data,\r
2079 DataSize\r
2080 );\r
2081 CopyMem (&NextVariable->VendorGuid, VendorGuid, sizeof (EFI_GUID));\r
2082 //\r
2083 // There will be pad bytes after Data, the NextVariable->NameSize and\r
2084 // NextVariable->DataSize should not include pad size so that variable\r
2085 // service can get actual size in GetVariable.\r
2086 //\r
2087 NextVariable->NameSize = (UINT32)VarNameSize;\r
2088 NextVariable->DataSize = (UINT32)DataSize;\r
2089\r
2090 //\r
2091 // The actual size of the variable that stores in storage should\r
2092 // include pad size.\r
2093 //\r
2094 VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);\r
2095 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
2096 //\r
2097 // Create a nonvolatile variable.\r
2098 //\r
2099 Volatile = FALSE;\r
2100\r
2101 IsCommonVariable = FALSE;\r
2102 IsCommonUserVariable = FALSE;\r
2103 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0) {\r
2104 IsCommonVariable = TRUE;\r
2105 IsCommonUserVariable = IsUserVariable (NextVariable);\r
2106 }\r
2107 if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0)\r
2108 && ((VarSize + mVariableModuleGlobal->HwErrVariableTotalSize) > PcdGet32 (PcdHwErrStorageSize)))\r
2109 || (IsCommonVariable && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > mVariableModuleGlobal->CommonVariableSpace))\r
2110 || (IsCommonVariable && AtRuntime () && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > mVariableModuleGlobal->CommonRuntimeVariableSpace))\r
2111 || (IsCommonUserVariable && ((VarSize + mVariableModuleGlobal->CommonUserVariableTotalSize) > mVariableModuleGlobal->CommonMaxUserVariableSpace))) {\r
2112 if (AtRuntime ()) {\r
2113 if (IsCommonUserVariable && ((VarSize + mVariableModuleGlobal->CommonUserVariableTotalSize) > mVariableModuleGlobal->CommonMaxUserVariableSpace)) {\r
2114 RecordVarErrorFlag (VAR_ERROR_FLAG_USER_ERROR, VariableName, VendorGuid, Attributes, VarSize);\r
2115 }\r
2116 if (IsCommonVariable && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > mVariableModuleGlobal->CommonRuntimeVariableSpace)) {\r
2117 RecordVarErrorFlag (VAR_ERROR_FLAG_SYSTEM_ERROR, VariableName, VendorGuid, Attributes, VarSize);\r
2118 }\r
2119 Status = EFI_OUT_OF_RESOURCES;\r
2120 goto Done;\r
2121 }\r
2122 //\r
2123 // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.\r
2124 //\r
2125 Status = Reclaim (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase, \r
2126 &mVariableModuleGlobal->NonVolatileLastVariableOffset, FALSE, Variable, NextVariable, HEADER_ALIGN (VarSize));\r
2127 if (!EFI_ERROR (Status)) {\r
2128 //\r
2129 // The new variable has been integrated successfully during reclaiming.\r
2130 //\r
2131 if (Variable->CurrPtr != NULL) {\r
2132 CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN) CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr));\r
2133 CacheVariable->InDeletedTransitionPtr = NULL;\r
2134 }\r
2135 UpdateVariableInfo (VariableName, VendorGuid, FALSE, FALSE, TRUE, FALSE, FALSE);\r
2136 FlushHobVariableToFlash (VariableName, VendorGuid);\r
2137 } else {\r
2138 if (IsCommonUserVariable && ((VarSize + mVariableModuleGlobal->CommonUserVariableTotalSize) > mVariableModuleGlobal->CommonMaxUserVariableSpace)) {\r
2139 RecordVarErrorFlag (VAR_ERROR_FLAG_USER_ERROR, VariableName, VendorGuid, Attributes, VarSize);\r
2140 }\r
2141 if (IsCommonVariable && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > mVariableModuleGlobal->CommonVariableSpace)) {\r
2142 RecordVarErrorFlag (VAR_ERROR_FLAG_SYSTEM_ERROR, VariableName, VendorGuid, Attributes, VarSize);\r
2143 }\r
2144 }\r
2145 goto Done;\r
2146 }\r
2147 //\r
2148 // Four steps\r
2149 // 1. Write variable header\r
2150 // 2. Set variable state to header valid \r
2151 // 3. Write variable data\r
2152 // 4. Set variable state to valid\r
2153 //\r
2154 //\r
2155 // Step 1:\r
2156 //\r
2157 CacheOffset = mVariableModuleGlobal->NonVolatileLastVariableOffset;\r
2158 Status = UpdateVariableStore (\r
2159 &mVariableModuleGlobal->VariableGlobal,\r
2160 FALSE,\r
2161 TRUE,\r
2162 Fvb,\r
2163 mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
2164 sizeof (VARIABLE_HEADER),\r
2165 (UINT8 *) NextVariable\r
2166 );\r
2167\r
2168 if (EFI_ERROR (Status)) {\r
2169 goto Done;\r
2170 }\r
2171\r
2172 //\r
2173 // Step 2:\r
2174 //\r
2175 NextVariable->State = VAR_HEADER_VALID_ONLY;\r
2176 Status = UpdateVariableStore (\r
2177 &mVariableModuleGlobal->VariableGlobal,\r
2178 FALSE,\r
2179 TRUE,\r
2180 Fvb,\r
2181 mVariableModuleGlobal->NonVolatileLastVariableOffset + OFFSET_OF (VARIABLE_HEADER, State),\r
2182 sizeof (UINT8),\r
2183 &NextVariable->State\r
2184 );\r
2185\r
2186 if (EFI_ERROR (Status)) {\r
2187 goto Done;\r
2188 }\r
2189 //\r
2190 // Step 3:\r
2191 //\r
2192 Status = UpdateVariableStore (\r
2193 &mVariableModuleGlobal->VariableGlobal,\r
2194 FALSE,\r
2195 TRUE,\r
2196 Fvb,\r
2197 mVariableModuleGlobal->NonVolatileLastVariableOffset + sizeof (VARIABLE_HEADER),\r
2198 (UINT32) VarSize - sizeof (VARIABLE_HEADER),\r
2199 (UINT8 *) NextVariable + sizeof (VARIABLE_HEADER)\r
2200 );\r
2201\r
2202 if (EFI_ERROR (Status)) {\r
2203 goto Done;\r
2204 }\r
2205 //\r
2206 // Step 4:\r
2207 //\r
2208 NextVariable->State = VAR_ADDED;\r
2209 Status = UpdateVariableStore (\r
2210 &mVariableModuleGlobal->VariableGlobal,\r
2211 FALSE,\r
2212 TRUE,\r
2213 Fvb,\r
2214 mVariableModuleGlobal->NonVolatileLastVariableOffset + OFFSET_OF (VARIABLE_HEADER, State),\r
2215 sizeof (UINT8),\r
2216 &NextVariable->State\r
2217 );\r
2218\r
2219 if (EFI_ERROR (Status)) {\r
2220 goto Done;\r
2221 }\r
2222\r
2223 mVariableModuleGlobal->NonVolatileLastVariableOffset += HEADER_ALIGN (VarSize);\r
2224\r
2225 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {\r
2226 mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VarSize);\r
2227 } else {\r
2228 mVariableModuleGlobal->CommonVariableTotalSize += HEADER_ALIGN (VarSize);\r
2229 if (IsCommonUserVariable) {\r
2230 mVariableModuleGlobal->CommonUserVariableTotalSize += HEADER_ALIGN (VarSize);\r
2231 }\r
2232 }\r
2233 //\r
2234 // update the memory copy of Flash region.\r
2235 //\r
2236 CopyMem ((UINT8 *)mNvVariableCache + CacheOffset, (UINT8 *)NextVariable, VarSize);\r
2237 } else {\r
2238 //\r
2239 // Create a volatile variable.\r
2240 // \r
2241 Volatile = TRUE;\r
2242\r
2243 if ((UINT32) (VarSize + mVariableModuleGlobal->VolatileLastVariableOffset) >\r
2244 ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size) {\r
2245 //\r
2246 // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.\r
2247 //\r
2248 Status = Reclaim (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase, \r
2249 &mVariableModuleGlobal->VolatileLastVariableOffset, TRUE, Variable, NextVariable, HEADER_ALIGN (VarSize));\r
2250 if (!EFI_ERROR (Status)) {\r
2251 //\r
2252 // The new variable has been integrated successfully during reclaiming.\r
2253 //\r
2254 if (Variable->CurrPtr != NULL) {\r
2255 CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN) CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr));\r
2256 CacheVariable->InDeletedTransitionPtr = NULL;\r
2257 }\r
2258 UpdateVariableInfo (VariableName, VendorGuid, TRUE, FALSE, TRUE, FALSE, FALSE);\r
2259 }\r
2260 goto Done;\r
2261 }\r
2262\r
2263 NextVariable->State = VAR_ADDED;\r
2264 Status = UpdateVariableStore (\r
2265 &mVariableModuleGlobal->VariableGlobal,\r
2266 TRUE,\r
2267 TRUE,\r
2268 Fvb,\r
2269 mVariableModuleGlobal->VolatileLastVariableOffset,\r
2270 (UINT32) VarSize,\r
2271 (UINT8 *) NextVariable\r
2272 );\r
2273\r
2274 if (EFI_ERROR (Status)) {\r
2275 goto Done;\r
2276 }\r
2277\r
2278 mVariableModuleGlobal->VolatileLastVariableOffset += HEADER_ALIGN (VarSize);\r
2279 }\r
2280\r
2281 //\r
2282 // Mark the old variable as deleted.\r
2283 //\r
2284 if (!EFI_ERROR (Status) && Variable->CurrPtr != NULL) {\r
2285 if (Variable->InDeletedTransitionPtr != NULL) {\r
2286 //\r
2287 // Both ADDED and IN_DELETED_TRANSITION old variable are present,\r
2288 // set IN_DELETED_TRANSITION one to DELETED state first.\r
2289 //\r
2290 State = Variable->InDeletedTransitionPtr->State;\r
2291 State &= VAR_DELETED;\r
2292 Status = UpdateVariableStore (\r
2293 &mVariableModuleGlobal->VariableGlobal,\r
2294 Variable->Volatile,\r
2295 FALSE,\r
2296 Fvb,\r
2297 (UINTN) &Variable->InDeletedTransitionPtr->State,\r
2298 sizeof (UINT8),\r
2299 &State\r
2300 );\r
2301 if (!EFI_ERROR (Status)) {\r
2302 if (!Variable->Volatile) {\r
2303 ASSERT (CacheVariable->InDeletedTransitionPtr != NULL);\r
2304 CacheVariable->InDeletedTransitionPtr->State = State;\r
2305 }\r
2306 } else {\r
2307 goto Done;\r
2308 }\r
2309 }\r
2310\r
2311 State = Variable->CurrPtr->State;\r
2312 State &= VAR_DELETED;\r
2313\r
2314 Status = UpdateVariableStore (\r
2315 &mVariableModuleGlobal->VariableGlobal,\r
2316 Variable->Volatile,\r
2317 FALSE,\r
2318 Fvb,\r
2319 (UINTN) &Variable->CurrPtr->State,\r
2320 sizeof (UINT8),\r
2321 &State\r
2322 );\r
2323 if (!EFI_ERROR (Status) && !Variable->Volatile) { \r
2324 CacheVariable->CurrPtr->State = State;\r
2325 }\r
2326 }\r
2327\r
2328 if (!EFI_ERROR (Status)) {\r
2329 UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);\r
2330 if (!Volatile) {\r
2331 FlushHobVariableToFlash (VariableName, VendorGuid);\r
2332 }\r
2333 }\r
2334\r
2335Done:\r
2336 return Status;\r
2337}\r
2338\r
2339/**\r
2340 Check if a Unicode character is a hexadecimal character.\r
2341\r
2342 This function checks if a Unicode character is a \r
2343 hexadecimal character. The valid hexadecimal character is \r
2344 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.\r
2345\r
2346\r
2347 @param Char The character to check against.\r
2348\r
2349 @retval TRUE If the Char is a hexadecmial character.\r
2350 @retval FALSE If the Char is not a hexadecmial character.\r
2351\r
2352**/\r
2353BOOLEAN\r
2354EFIAPI\r
2355IsHexaDecimalDigitCharacter (\r
2356 IN CHAR16 Char\r
2357 )\r
2358{\r
2359 return (BOOLEAN) ((Char >= L'0' && Char <= L'9') || (Char >= L'A' && Char <= L'F') || (Char >= L'a' && Char <= L'f'));\r
2360}\r
2361\r
2362/**\r
2363\r
2364 This code checks if variable is hardware error record variable or not.\r
2365\r
2366 According to UEFI spec, hardware error record variable should use the EFI_HARDWARE_ERROR_VARIABLE VendorGuid\r
2367 and have the L"HwErrRec####" name convention, #### is a printed hex value and no 0x or h is included in the hex value.\r
2368\r
2369 @param VariableName Pointer to variable name.\r
2370 @param VendorGuid Variable Vendor Guid.\r
2371\r
2372 @retval TRUE Variable is hardware error record variable.\r
2373 @retval FALSE Variable is not hardware error record variable.\r
2374\r
2375**/\r
2376BOOLEAN\r
2377EFIAPI\r
2378IsHwErrRecVariable (\r
2379 IN CHAR16 *VariableName,\r
2380 IN EFI_GUID *VendorGuid\r
2381 )\r
2382{\r
2383 if (!CompareGuid (VendorGuid, &gEfiHardwareErrorVariableGuid) ||\r
2384 (StrLen (VariableName) != StrLen (L"HwErrRec####")) ||\r
2385 (StrnCmp(VariableName, L"HwErrRec", StrLen (L"HwErrRec")) != 0) ||\r
2386 !IsHexaDecimalDigitCharacter (VariableName[0x8]) ||\r
2387 !IsHexaDecimalDigitCharacter (VariableName[0x9]) ||\r
2388 !IsHexaDecimalDigitCharacter (VariableName[0xA]) ||\r
2389 !IsHexaDecimalDigitCharacter (VariableName[0xB])) {\r
2390 return FALSE;\r
2391 }\r
2392\r
2393 return TRUE;\r
2394}\r
2395\r
2396/**\r
2397 Mark a variable that will become read-only after leaving the DXE phase of execution.\r
2398\r
2399 @param[in] This The VARIABLE_LOCK_PROTOCOL instance.\r
2400 @param[in] VariableName A pointer to the variable name that will be made read-only subsequently.\r
2401 @param[in] VendorGuid A pointer to the vendor GUID that will be made read-only subsequently.\r
2402\r
2403 @retval EFI_SUCCESS The variable specified by the VariableName and the VendorGuid was marked\r
2404 as pending to be read-only.\r
2405 @retval EFI_INVALID_PARAMETER VariableName or VendorGuid is NULL.\r
2406 Or VariableName is an empty string.\r
2407 @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has\r
2408 already been signaled.\r
2409 @retval EFI_OUT_OF_RESOURCES There is not enough resource to hold the lock request.\r
2410**/\r
2411EFI_STATUS\r
2412EFIAPI\r
2413VariableLockRequestToLock (\r
2414 IN CONST EDKII_VARIABLE_LOCK_PROTOCOL *This,\r
2415 IN CHAR16 *VariableName,\r
2416 IN EFI_GUID *VendorGuid\r
2417 )\r
2418{\r
2419 VARIABLE_ENTRY *Entry;\r
2420 CHAR16 *Name;\r
2421 LIST_ENTRY *Link;\r
2422 VARIABLE_ENTRY *LockedEntry;\r
2423\r
2424 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r
2425 return EFI_INVALID_PARAMETER;\r
2426 }\r
2427\r
2428 if (mEndOfDxe) {\r
2429 return EFI_ACCESS_DENIED;\r
2430 }\r
2431\r
2432 Entry = AllocateRuntimeZeroPool (sizeof (*Entry) + StrSize (VariableName));\r
2433 if (Entry == NULL) {\r
2434 return EFI_OUT_OF_RESOURCES;\r
2435 }\r
2436\r
2437 DEBUG ((EFI_D_INFO, "[Variable] Lock: %g:%s\n", VendorGuid, VariableName));\r
2438\r
2439 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2440\r
2441 for ( Link = GetFirstNode (&mLockedVariableList)\r
2442 ; !IsNull (&mLockedVariableList, Link)\r
2443 ; Link = GetNextNode (&mLockedVariableList, Link)\r
2444 ) {\r
2445 LockedEntry = BASE_CR (Link, VARIABLE_ENTRY, Link);\r
2446 Name = (CHAR16 *) ((UINTN) LockedEntry + sizeof (*LockedEntry));\r
2447 if (CompareGuid (&LockedEntry->Guid, VendorGuid) && (StrCmp (Name, VariableName) == 0)) {\r
2448 goto Done;\r
2449 }\r
2450 }\r
2451\r
2452 Name = (CHAR16 *) ((UINTN) Entry + sizeof (*Entry));\r
2453 StrnCpy (Name, VariableName, StrLen (VariableName));\r
2454 CopyGuid (&Entry->Guid, VendorGuid);\r
2455 InsertTailList (&mLockedVariableList, &Entry->Link);\r
2456\r
2457Done:\r
2458 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2459\r
2460 return EFI_SUCCESS;\r
2461}\r
2462\r
2463/**\r
2464\r
2465 This code finds variable in storage blocks (Volatile or Non-Volatile).\r
2466\r
2467 Caution: This function may receive untrusted input.\r
2468 This function may be invoked in SMM mode, and datasize is external input.\r
2469 This function will do basic validation, before parse the data.\r
2470\r
2471 @param VariableName Name of Variable to be found.\r
2472 @param VendorGuid Variable vendor GUID.\r
2473 @param Attributes Attribute value of the variable found.\r
2474 @param DataSize Size of Data found. If size is less than the\r
2475 data, this value contains the required size.\r
2476 @param Data Data pointer.\r
2477 \r
2478 @return EFI_INVALID_PARAMETER Invalid parameter.\r
2479 @return EFI_SUCCESS Find the specified variable.\r
2480 @return EFI_NOT_FOUND Not found.\r
2481 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.\r
2482\r
2483**/\r
2484EFI_STATUS\r
2485EFIAPI\r
2486VariableServiceGetVariable (\r
2487 IN CHAR16 *VariableName,\r
2488 IN EFI_GUID *VendorGuid,\r
2489 OUT UINT32 *Attributes OPTIONAL,\r
2490 IN OUT UINTN *DataSize,\r
2491 OUT VOID *Data\r
2492 )\r
2493{\r
2494 EFI_STATUS Status;\r
2495 VARIABLE_POINTER_TRACK Variable;\r
2496 UINTN VarDataSize;\r
2497\r
2498 if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {\r
2499 return EFI_INVALID_PARAMETER;\r
2500 }\r
2501\r
2502 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2503 \r
2504 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
2505 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
2506 goto Done;\r
2507 }\r
2508\r
2509 //\r
2510 // Get data size\r
2511 //\r
2512 VarDataSize = DataSizeOfVariable (Variable.CurrPtr);\r
2513 ASSERT (VarDataSize != 0);\r
2514\r
2515 if (*DataSize >= VarDataSize) {\r
2516 if (Data == NULL) {\r
2517 Status = EFI_INVALID_PARAMETER;\r
2518 goto Done;\r
2519 }\r
2520\r
2521 CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);\r
2522 if (Attributes != NULL) {\r
2523 *Attributes = Variable.CurrPtr->Attributes;\r
2524 }\r
2525\r
2526 *DataSize = VarDataSize;\r
2527 UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, TRUE, FALSE, FALSE, FALSE);\r
2528 \r
2529 Status = EFI_SUCCESS;\r
2530 goto Done;\r
2531 } else {\r
2532 *DataSize = VarDataSize;\r
2533 Status = EFI_BUFFER_TOO_SMALL;\r
2534 goto Done;\r
2535 }\r
2536\r
2537Done:\r
2538 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2539 return Status;\r
2540}\r
2541\r
2542\r
2543\r
2544/**\r
2545\r
2546 This code Finds the Next available variable.\r
2547\r
2548 Caution: This function may receive untrusted input.\r
2549 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r
2550\r
2551 @param VariableNameSize Size of the variable name.\r
2552 @param VariableName Pointer to variable name.\r
2553 @param VendorGuid Variable Vendor Guid.\r
2554\r
2555 @return EFI_INVALID_PARAMETER Invalid parameter.\r
2556 @return EFI_SUCCESS Find the specified variable.\r
2557 @return EFI_NOT_FOUND Not found.\r
2558 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.\r
2559\r
2560**/\r
2561EFI_STATUS\r
2562EFIAPI\r
2563VariableServiceGetNextVariableName (\r
2564 IN OUT UINTN *VariableNameSize,\r
2565 IN OUT CHAR16 *VariableName,\r
2566 IN OUT EFI_GUID *VendorGuid\r
2567 )\r
2568{\r
2569 VARIABLE_STORE_TYPE Type;\r
2570 VARIABLE_POINTER_TRACK Variable;\r
2571 VARIABLE_POINTER_TRACK VariableInHob;\r
2572 VARIABLE_POINTER_TRACK VariablePtrTrack;\r
2573 UINTN VarNameSize;\r
2574 EFI_STATUS Status;\r
2575 VARIABLE_STORE_HEADER *VariableStoreHeader[VariableStoreTypeMax];\r
2576\r
2577 if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {\r
2578 return EFI_INVALID_PARAMETER;\r
2579 }\r
2580\r
2581 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2582\r
2583 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
2584 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
2585 goto Done;\r
2586 }\r
2587\r
2588 if (VariableName[0] != 0) {\r
2589 //\r
2590 // If variable name is not NULL, get next variable.\r
2591 //\r
2592 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
2593 }\r
2594\r
2595 //\r
2596 // 0: Volatile, 1: HOB, 2: Non-Volatile.\r
2597 // The index and attributes mapping must be kept in this order as FindVariable\r
2598 // makes use of this mapping to implement search algorithm.\r
2599 //\r
2600 VariableStoreHeader[VariableStoreTypeVolatile] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;\r
2601 VariableStoreHeader[VariableStoreTypeHob] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;\r
2602 VariableStoreHeader[VariableStoreTypeNv] = mNvVariableCache;\r
2603\r
2604 while (TRUE) {\r
2605 //\r
2606 // Switch from Volatile to HOB, to Non-Volatile.\r
2607 //\r
2608 while (!IsValidVariableHeader (Variable.CurrPtr, Variable.EndPtr)) {\r
2609 //\r
2610 // Find current storage index\r
2611 //\r
2612 for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {\r
2613 if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr == GetStartPointer (VariableStoreHeader[Type]))) {\r
2614 break;\r
2615 }\r
2616 }\r
2617 ASSERT (Type < VariableStoreTypeMax);\r
2618 //\r
2619 // Switch to next storage\r
2620 //\r
2621 for (Type++; Type < VariableStoreTypeMax; Type++) {\r
2622 if (VariableStoreHeader[Type] != NULL) {\r
2623 break;\r
2624 }\r
2625 }\r
2626 //\r
2627 // Capture the case that \r
2628 // 1. current storage is the last one, or\r
2629 // 2. no further storage\r
2630 //\r
2631 if (Type == VariableStoreTypeMax) {\r
2632 Status = EFI_NOT_FOUND;\r
2633 goto Done;\r
2634 }\r
2635 Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);\r
2636 Variable.EndPtr = GetEndPointer (VariableStoreHeader[Type]);\r
2637 Variable.CurrPtr = Variable.StartPtr;\r
2638 }\r
2639\r
2640 //\r
2641 // Variable is found\r
2642 //\r
2643 if (Variable.CurrPtr->State == VAR_ADDED || Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
2644 if (!AtRuntime () || ((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {\r
2645 if (Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
2646 //\r
2647 // If it is a IN_DELETED_TRANSITION variable,\r
2648 // and there is also a same ADDED one at the same time,\r
2649 // don't return it.\r
2650 //\r
2651 VariablePtrTrack.StartPtr = Variable.StartPtr;\r
2652 VariablePtrTrack.EndPtr = Variable.EndPtr;\r
2653 Status = FindVariableEx (\r
2654 GetVariableNamePtr (Variable.CurrPtr),\r
2655 &Variable.CurrPtr->VendorGuid,\r
2656 FALSE,\r
2657 &VariablePtrTrack\r
2658 );\r
2659 if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State == VAR_ADDED) {\r
2660 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
2661 continue;\r
2662 }\r
2663 }\r
2664\r
2665 //\r
2666 // Don't return NV variable when HOB overrides it\r
2667 //\r
2668 if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) && (VariableStoreHeader[VariableStoreTypeNv] != NULL) && \r
2669 (Variable.StartPtr == GetStartPointer (VariableStoreHeader[VariableStoreTypeNv]))\r
2670 ) {\r
2671 VariableInHob.StartPtr = GetStartPointer (VariableStoreHeader[VariableStoreTypeHob]);\r
2672 VariableInHob.EndPtr = GetEndPointer (VariableStoreHeader[VariableStoreTypeHob]);\r
2673 Status = FindVariableEx (\r
2674 GetVariableNamePtr (Variable.CurrPtr),\r
2675 &Variable.CurrPtr->VendorGuid,\r
2676 FALSE,\r
2677 &VariableInHob\r
2678 );\r
2679 if (!EFI_ERROR (Status)) {\r
2680 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
2681 continue;\r
2682 }\r
2683 }\r
2684\r
2685 VarNameSize = NameSizeOfVariable (Variable.CurrPtr);\r
2686 ASSERT (VarNameSize != 0);\r
2687\r
2688 if (VarNameSize <= *VariableNameSize) {\r
2689 CopyMem (VariableName, GetVariableNamePtr (Variable.CurrPtr), VarNameSize);\r
2690 CopyMem (VendorGuid, &Variable.CurrPtr->VendorGuid, sizeof (EFI_GUID));\r
2691 Status = EFI_SUCCESS;\r
2692 } else {\r
2693 Status = EFI_BUFFER_TOO_SMALL;\r
2694 }\r
2695\r
2696 *VariableNameSize = VarNameSize;\r
2697 goto Done;\r
2698 }\r
2699 }\r
2700\r
2701 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
2702 }\r
2703\r
2704Done:\r
2705 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2706 return Status;\r
2707}\r
2708\r
2709/**\r
2710\r
2711 This code sets variable in storage blocks (Volatile or Non-Volatile).\r
2712\r
2713 Caution: This function may receive untrusted input.\r
2714 This function may be invoked in SMM mode, and datasize and data are external input.\r
2715 This function will do basic validation, before parse the data.\r
2716\r
2717 @param VariableName Name of Variable to be found.\r
2718 @param VendorGuid Variable vendor GUID.\r
2719 @param Attributes Attribute value of the variable found\r
2720 @param DataSize Size of Data found. If size is less than the\r
2721 data, this value contains the required size.\r
2722 @param Data Data pointer.\r
2723\r
2724 @return EFI_INVALID_PARAMETER Invalid parameter.\r
2725 @return EFI_SUCCESS Set successfully.\r
2726 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.\r
2727 @return EFI_NOT_FOUND Not found.\r
2728 @return EFI_WRITE_PROTECTED Variable is read-only.\r
2729\r
2730**/\r
2731EFI_STATUS\r
2732EFIAPI\r
2733VariableServiceSetVariable (\r
2734 IN CHAR16 *VariableName,\r
2735 IN EFI_GUID *VendorGuid,\r
2736 IN UINT32 Attributes,\r
2737 IN UINTN DataSize,\r
2738 IN VOID *Data\r
2739 )\r
2740{\r
2741 VARIABLE_POINTER_TRACK Variable;\r
2742 EFI_STATUS Status;\r
2743 VARIABLE_HEADER *NextVariable;\r
2744 EFI_PHYSICAL_ADDRESS Point;\r
2745 LIST_ENTRY *Link;\r
2746 VARIABLE_ENTRY *Entry;\r
2747 CHAR16 *Name;\r
2748\r
2749 //\r
2750 // Check input parameters.\r
2751 //\r
2752 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r
2753 return EFI_INVALID_PARAMETER;\r
2754 } \r
2755\r
2756 if (DataSize != 0 && Data == NULL) {\r
2757 return EFI_INVALID_PARAMETER;\r
2758 }\r
2759\r
2760 //\r
2761 // Not support authenticated or append variable write yet.\r
2762 //\r
2763 if ((Attributes & (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS | EFI_VARIABLE_APPEND_WRITE)) != 0) {\r
2764 return EFI_INVALID_PARAMETER;\r
2765 }\r
2766\r
2767 //\r
2768 // Make sure if runtime bit is set, boot service bit is set also.\r
2769 //\r
2770 if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
2771 return EFI_INVALID_PARAMETER;\r
2772 }\r
2773\r
2774 if ((UINTN)(~0) - DataSize < StrSize(VariableName)){\r
2775 //\r
2776 // Prevent whole variable size overflow \r
2777 // \r
2778 return EFI_INVALID_PARAMETER;\r
2779 }\r
2780\r
2781 //\r
2782 // The size of the VariableName, including the Unicode Null in bytes plus\r
2783 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)\r
2784 // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.\r
2785 //\r
2786 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2787 if ( StrSize (VariableName) + DataSize > PcdGet32 (PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER)) {\r
2788 return EFI_INVALID_PARAMETER;\r
2789 }\r
2790 if (!IsHwErrRecVariable(VariableName, VendorGuid)) {\r
2791 return EFI_INVALID_PARAMETER;\r
2792 }\r
2793 } else {\r
2794 //\r
2795 // The size of the VariableName, including the Unicode Null in bytes plus\r
2796 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxVariableSize) bytes.\r
2797 //\r
2798 if (StrSize (VariableName) + DataSize > PcdGet32 (PcdMaxVariableSize) - sizeof (VARIABLE_HEADER)) {\r
2799 return EFI_INVALID_PARAMETER;\r
2800 }\r
2801 }\r
2802\r
2803 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2804\r
2805 //\r
2806 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.\r
2807 //\r
2808 if (1 < InterlockedIncrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState)) {\r
2809 Point = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;\r
2810 //\r
2811 // Parse non-volatile variable data and get last variable offset.\r
2812 //\r
2813 NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point);\r
2814 while (IsValidVariableHeader (NextVariable, GetEndPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point))) {\r
2815 NextVariable = GetNextVariablePtr (NextVariable);\r
2816 }\r
2817 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) Point;\r
2818 }\r
2819\r
2820 if (mEndOfDxe && mEnableLocking) {\r
2821 //\r
2822 // Treat the variables listed in the forbidden variable list as read-only after leaving DXE phase.\r
2823 //\r
2824 for ( Link = GetFirstNode (&mLockedVariableList)\r
2825 ; !IsNull (&mLockedVariableList, Link)\r
2826 ; Link = GetNextNode (&mLockedVariableList, Link)\r
2827 ) {\r
2828 Entry = BASE_CR (Link, VARIABLE_ENTRY, Link);\r
2829 Name = (CHAR16 *) ((UINTN) Entry + sizeof (*Entry));\r
2830 if (CompareGuid (&Entry->Guid, VendorGuid) && (StrCmp (Name, VariableName) == 0)) {\r
2831 Status = EFI_WRITE_PROTECTED;\r
2832 DEBUG ((EFI_D_INFO, "[Variable]: Changing readonly variable after leaving DXE phase - %g:%s\n", VendorGuid, VariableName));\r
2833 goto Done;\r
2834 }\r
2835 }\r
2836 }\r
2837\r
2838 Status = InternalVarCheckSetVariableCheck (VariableName, VendorGuid, Attributes, DataSize, Data);\r
2839 if (EFI_ERROR (Status)) {\r
2840 goto Done;\r
2841 }\r
2842\r
2843 //\r
2844 // Check whether the input variable is already existed.\r
2845 //\r
2846 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, TRUE);\r
2847 if (!EFI_ERROR (Status)) {\r
2848 if (((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) && AtRuntime ()) {\r
2849 Status = EFI_WRITE_PROTECTED;\r
2850 goto Done;\r
2851 }\r
2852 if (Attributes != 0 && Attributes != Variable.CurrPtr->Attributes) {\r
2853 //\r
2854 // If a preexisting variable is rewritten with different attributes, SetVariable() shall not\r
2855 // modify the variable and shall return EFI_INVALID_PARAMETER. Two exceptions to this rule:\r
2856 // 1. No access attributes specified\r
2857 // 2. The only attribute differing is EFI_VARIABLE_APPEND_WRITE\r
2858 //\r
2859 Status = EFI_INVALID_PARAMETER;\r
2860 DEBUG ((EFI_D_INFO, "[Variable]: Rewritten a preexisting variable(0x%08x) with different attributes(0x%08x) - %g:%s\n", Variable.CurrPtr->Attributes, Attributes, VendorGuid, VariableName));\r
2861 goto Done;\r
2862 }\r
2863 }\r
2864\r
2865 if (!FeaturePcdGet (PcdUefiVariableDefaultLangDeprecate)) {\r
2866 //\r
2867 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.\r
2868 //\r
2869 Status = AutoUpdateLangVariable (VariableName, Data, DataSize);\r
2870 if (EFI_ERROR (Status)) {\r
2871 //\r
2872 // The auto update operation failed, directly return to avoid inconsistency between PlatformLang and Lang.\r
2873 //\r
2874 goto Done;\r
2875 }\r
2876 }\r
2877\r
2878 Status = UpdateVariable (VariableName, VendorGuid, Data, DataSize, Attributes, &Variable);\r
2879\r
2880Done:\r
2881 InterlockedDecrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState);\r
2882 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2883\r
2884 return Status;\r
2885}\r
2886\r
2887/**\r
2888\r
2889 This code returns information about the EFI variables.\r
2890\r
2891 Caution: This function may receive untrusted input.\r
2892 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r
2893\r
2894 @param Attributes Attributes bitmask to specify the type of variables\r
2895 on which to return information.\r
2896 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available\r
2897 for the EFI variables associated with the attributes specified.\r
2898 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available\r
2899 for EFI variables associated with the attributes specified.\r
2900 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables\r
2901 associated with the attributes specified.\r
2902\r
2903 @return EFI_SUCCESS Query successfully.\r
2904\r
2905**/\r
2906EFI_STATUS\r
2907EFIAPI\r
2908VariableServiceQueryVariableInfoInternal (\r
2909 IN UINT32 Attributes,\r
2910 OUT UINT64 *MaximumVariableStorageSize,\r
2911 OUT UINT64 *RemainingVariableStorageSize,\r
2912 OUT UINT64 *MaximumVariableSize\r
2913 )\r
2914{\r
2915 VARIABLE_HEADER *Variable;\r
2916 VARIABLE_HEADER *NextVariable;\r
2917 UINT64 VariableSize;\r
2918 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
2919 UINT64 CommonVariableTotalSize;\r
2920 UINT64 HwErrVariableTotalSize;\r
2921 EFI_STATUS Status;\r
2922 VARIABLE_POINTER_TRACK VariablePtrTrack;\r
2923\r
2924 CommonVariableTotalSize = 0;\r
2925 HwErrVariableTotalSize = 0;\r
2926\r
2927 if((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r
2928 //\r
2929 // Query is Volatile related.\r
2930 //\r
2931 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r
2932 } else {\r
2933 //\r
2934 // Query is Non-Volatile related.\r
2935 //\r
2936 VariableStoreHeader = mNvVariableCache;\r
2937 }\r
2938\r
2939 //\r
2940 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize\r
2941 // with the storage size (excluding the storage header size).\r
2942 //\r
2943 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);\r
2944\r
2945 //\r
2946 // Harware error record variable needs larger size.\r
2947 //\r
2948 if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
2949 *MaximumVariableStorageSize = PcdGet32 (PcdHwErrStorageSize);\r
2950 *MaximumVariableSize = PcdGet32 (PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER);\r
2951 } else {\r
2952 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
2953 if (AtRuntime ()) {\r
2954 *MaximumVariableStorageSize = mVariableModuleGlobal->CommonRuntimeVariableSpace;\r
2955 } else {\r
2956 *MaximumVariableStorageSize = mVariableModuleGlobal->CommonVariableSpace;\r
2957 }\r
2958 }\r
2959\r
2960 //\r
2961 // Let *MaximumVariableSize be PcdGet32 (PcdMaxVariableSize) with the exception of the variable header size.\r
2962 //\r
2963 *MaximumVariableSize = PcdGet32 (PcdMaxVariableSize) - sizeof (VARIABLE_HEADER);\r
2964 }\r
2965\r
2966 //\r
2967 // Point to the starting address of the variables.\r
2968 //\r
2969 Variable = GetStartPointer (VariableStoreHeader);\r
2970\r
2971 //\r
2972 // Now walk through the related variable store.\r
2973 //\r
2974 while (IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))) {\r
2975 NextVariable = GetNextVariablePtr (Variable);\r
2976 VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;\r
2977\r
2978 if (AtRuntime ()) {\r
2979 //\r
2980 // We don't take the state of the variables in mind\r
2981 // when calculating RemainingVariableStorageSize,\r
2982 // since the space occupied by variables not marked with\r
2983 // VAR_ADDED is not allowed to be reclaimed in Runtime.\r
2984 //\r
2985 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2986 HwErrVariableTotalSize += VariableSize;\r
2987 } else {\r
2988 CommonVariableTotalSize += VariableSize;\r
2989 }\r
2990 } else {\r
2991 //\r
2992 // Only care about Variables with State VAR_ADDED, because\r
2993 // the space not marked as VAR_ADDED is reclaimable now.\r
2994 //\r
2995 if (Variable->State == VAR_ADDED) {\r
2996 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2997 HwErrVariableTotalSize += VariableSize;\r
2998 } else {\r
2999 CommonVariableTotalSize += VariableSize;\r
3000 }\r
3001 } else if (Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
3002 //\r
3003 // If it is a IN_DELETED_TRANSITION variable,\r
3004 // and there is not also a same ADDED one at the same time,\r
3005 // this IN_DELETED_TRANSITION variable is valid.\r
3006 //\r
3007 VariablePtrTrack.StartPtr = GetStartPointer (VariableStoreHeader);\r
3008 VariablePtrTrack.EndPtr = GetEndPointer (VariableStoreHeader);\r
3009 Status = FindVariableEx (\r
3010 GetVariableNamePtr (Variable),\r
3011 &Variable->VendorGuid,\r
3012 FALSE,\r
3013 &VariablePtrTrack\r
3014 );\r
3015 if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State != VAR_ADDED) {\r
3016 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
3017 HwErrVariableTotalSize += VariableSize;\r
3018 } else {\r
3019 CommonVariableTotalSize += VariableSize;\r
3020 }\r
3021 }\r
3022 }\r
3023 }\r
3024\r
3025 //\r
3026 // Go to the next one.\r
3027 //\r
3028 Variable = NextVariable;\r
3029 }\r
3030\r
3031 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD){\r
3032 *RemainingVariableStorageSize = *MaximumVariableStorageSize - HwErrVariableTotalSize;\r
3033 } else {\r
3034 if (*MaximumVariableStorageSize < CommonVariableTotalSize) {\r
3035 *RemainingVariableStorageSize = 0;\r
3036 } else {\r
3037 *RemainingVariableStorageSize = *MaximumVariableStorageSize - CommonVariableTotalSize;\r
3038 }\r
3039 }\r
3040\r
3041 if (*RemainingVariableStorageSize < sizeof (VARIABLE_HEADER)) {\r
3042 *MaximumVariableSize = 0;\r
3043 } else if ((*RemainingVariableStorageSize - sizeof (VARIABLE_HEADER)) < *MaximumVariableSize) {\r
3044 *MaximumVariableSize = *RemainingVariableStorageSize - sizeof (VARIABLE_HEADER);\r
3045 }\r
3046\r
3047 return EFI_SUCCESS;\r
3048}\r
3049\r
3050/**\r
3051\r
3052 This code returns information about the EFI variables.\r
3053\r
3054 Caution: This function may receive untrusted input.\r
3055 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r
3056\r
3057 @param Attributes Attributes bitmask to specify the type of variables\r
3058 on which to return information.\r
3059 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available\r
3060 for the EFI variables associated with the attributes specified.\r
3061 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available\r
3062 for EFI variables associated with the attributes specified.\r
3063 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables\r
3064 associated with the attributes specified.\r
3065\r
3066 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.\r
3067 @return EFI_SUCCESS Query successfully.\r
3068 @return EFI_UNSUPPORTED The attribute is not supported on this platform.\r
3069\r
3070**/\r
3071EFI_STATUS\r
3072EFIAPI\r
3073VariableServiceQueryVariableInfo (\r
3074 IN UINT32 Attributes,\r
3075 OUT UINT64 *MaximumVariableStorageSize,\r
3076 OUT UINT64 *RemainingVariableStorageSize,\r
3077 OUT UINT64 *MaximumVariableSize\r
3078 )\r
3079{\r
3080 EFI_STATUS Status;\r
3081\r
3082 if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {\r
3083 return EFI_INVALID_PARAMETER;\r
3084 }\r
3085\r
3086 if((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == 0) {\r
3087 //\r
3088 // Make sure the Attributes combination is supported by the platform.\r
3089 //\r
3090 return EFI_UNSUPPORTED;\r
3091 } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
3092 //\r
3093 // Make sure if runtime bit is set, boot service bit is set also.\r
3094 //\r
3095 return EFI_INVALID_PARAMETER;\r
3096 } else if (AtRuntime () && ((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0)) {\r
3097 //\r
3098 // Make sure RT Attribute is set if we are in Runtime phase.\r
3099 //\r
3100 return EFI_INVALID_PARAMETER;\r
3101 } else if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
3102 //\r
3103 // Make sure Hw Attribute is set with NV.\r
3104 //\r
3105 return EFI_INVALID_PARAMETER;\r
3106 } else if ((Attributes & (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS | EFI_VARIABLE_APPEND_WRITE)) != 0) {\r
3107 //\r
3108 // Not support authenticated or append variable write yet.\r
3109 //\r
3110 return EFI_UNSUPPORTED;\r
3111 }\r
3112\r
3113 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
3114\r
3115 Status = VariableServiceQueryVariableInfoInternal (\r
3116 Attributes,\r
3117 MaximumVariableStorageSize,\r
3118 RemainingVariableStorageSize,\r
3119 MaximumVariableSize\r
3120 );\r
3121\r
3122 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
3123 return Status;\r
3124}\r
3125\r
3126/**\r
3127 This function reclaims variable storage if free size is below the threshold.\r
3128\r
3129 Caution: This function may be invoked at SMM mode.\r
3130 Care must be taken to make sure not security issue.\r
3131\r
3132**/\r
3133VOID\r
3134ReclaimForOS(\r
3135 VOID\r
3136 )\r
3137{\r
3138 EFI_STATUS Status;\r
3139 UINTN RemainingCommonRuntimeVariableSpace;\r
3140 UINTN RemainingHwErrVariableSpace;\r
3141 STATIC BOOLEAN Reclaimed;\r
3142\r
3143 //\r
3144 // This function will be called only once at EndOfDxe or ReadyToBoot event.\r
3145 //\r
3146 if (Reclaimed) {\r
3147 return;\r
3148 }\r
3149 Reclaimed = TRUE;\r
3150\r
3151 Status = EFI_SUCCESS;\r
3152\r
3153 if (mVariableModuleGlobal->CommonRuntimeVariableSpace < mVariableModuleGlobal->CommonVariableTotalSize) {\r
3154 RemainingCommonRuntimeVariableSpace = 0;\r
3155 } else {\r
3156 RemainingCommonRuntimeVariableSpace = mVariableModuleGlobal->CommonRuntimeVariableSpace - mVariableModuleGlobal->CommonVariableTotalSize;\r
3157 }\r
3158\r
3159 RemainingHwErrVariableSpace = PcdGet32 (PcdHwErrStorageSize) - mVariableModuleGlobal->HwErrVariableTotalSize;\r
3160 //\r
3161 // Check if the free area is below a threshold.\r
3162 //\r
3163 if ((RemainingCommonRuntimeVariableSpace < PcdGet32 (PcdMaxVariableSize))\r
3164 || ((PcdGet32 (PcdHwErrStorageSize) != 0) &&\r
3165 (RemainingHwErrVariableSpace < PcdGet32 (PcdMaxHardwareErrorVariableSize)))){\r
3166 Status = Reclaim (\r
3167 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
3168 &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
3169 FALSE,\r
3170 NULL,\r
3171 NULL,\r
3172 0\r
3173 );\r
3174 ASSERT_EFI_ERROR (Status);\r
3175 }\r
3176}\r
3177\r
3178/**\r
3179 Init non-volatile variable store.\r
3180\r
3181 @retval EFI_SUCCESS Function successfully executed.\r
3182 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.\r
3183 @retval EFI_VOLUME_CORRUPTED Variable Store or Firmware Volume for Variable Store is corrupted.\r
3184\r
3185**/\r
3186EFI_STATUS\r
3187InitNonVolatileVariableStore (\r
3188 VOID\r
3189 )\r
3190{\r
3191 EFI_FIRMWARE_VOLUME_HEADER *FvHeader;\r
3192 VARIABLE_HEADER *Variable;\r
3193 VARIABLE_HEADER *NextVariable;\r
3194 EFI_PHYSICAL_ADDRESS VariableStoreBase;\r
3195 UINT64 VariableStoreLength;\r
3196 UINTN VariableSize;\r
3197 EFI_HOB_GUID_TYPE *GuidHob;\r
3198 EFI_PHYSICAL_ADDRESS NvStorageBase;\r
3199 UINT8 *NvStorageData;\r
3200 UINT32 NvStorageSize;\r
3201 FAULT_TOLERANT_WRITE_LAST_WRITE_DATA *FtwLastWriteData;\r
3202 UINT32 BackUpOffset;\r
3203 UINT32 BackUpSize;\r
3204 UINT32 HwErrStorageSize;\r
3205 UINT32 MaxUserNvVariableSpaceSize;\r
3206 UINT32 BoottimeReservedNvVariableSpaceSize;\r
3207\r
3208 mVariableModuleGlobal->FvbInstance = NULL;\r
3209\r
3210 //\r
3211 // Allocate runtime memory used for a memory copy of the FLASH region.\r
3212 // Keep the memory and the FLASH in sync as updates occur.\r
3213 //\r
3214 NvStorageSize = PcdGet32 (PcdFlashNvStorageVariableSize);\r
3215 NvStorageData = AllocateRuntimeZeroPool (NvStorageSize);\r
3216 if (NvStorageData == NULL) {\r
3217 return EFI_OUT_OF_RESOURCES;\r
3218 }\r
3219\r
3220 NvStorageBase = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);\r
3221 if (NvStorageBase == 0) {\r
3222 NvStorageBase = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);\r
3223 }\r
3224 //\r
3225 // Copy NV storage data to the memory buffer.\r
3226 //\r
3227 CopyMem (NvStorageData, (UINT8 *) (UINTN) NvStorageBase, NvStorageSize);\r
3228\r
3229 //\r
3230 // Check the FTW last write data hob.\r
3231 //\r
3232 GuidHob = GetFirstGuidHob (&gEdkiiFaultTolerantWriteGuid);\r
3233 if (GuidHob != NULL) {\r
3234 FtwLastWriteData = (FAULT_TOLERANT_WRITE_LAST_WRITE_DATA *) GET_GUID_HOB_DATA (GuidHob);\r
3235 if (FtwLastWriteData->TargetAddress == NvStorageBase) {\r
3236 DEBUG ((EFI_D_INFO, "Variable: NV storage is backed up in spare block: 0x%x\n", (UINTN) FtwLastWriteData->SpareAddress));\r
3237 //\r
3238 // Copy the backed up NV storage data to the memory buffer from spare block.\r
3239 //\r
3240 CopyMem (NvStorageData, (UINT8 *) (UINTN) (FtwLastWriteData->SpareAddress), NvStorageSize);\r
3241 } else if ((FtwLastWriteData->TargetAddress > NvStorageBase) &&\r
3242 (FtwLastWriteData->TargetAddress < (NvStorageBase + NvStorageSize))) {\r
3243 //\r
3244 // Flash NV storage from the offset is backed up in spare block.\r
3245 //\r
3246 BackUpOffset = (UINT32) (FtwLastWriteData->TargetAddress - NvStorageBase);\r
3247 BackUpSize = NvStorageSize - BackUpOffset;\r
3248 DEBUG ((EFI_D_INFO, "Variable: High partial NV storage from offset: %x is backed up in spare block: 0x%x\n", BackUpOffset, (UINTN) FtwLastWriteData->SpareAddress));\r
3249 //\r
3250 // Copy the partial backed up NV storage data to the memory buffer from spare block.\r
3251 //\r
3252 CopyMem (NvStorageData + BackUpOffset, (UINT8 *) (UINTN) FtwLastWriteData->SpareAddress, BackUpSize);\r
3253 }\r
3254 }\r
3255\r
3256 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) NvStorageData;\r
3257\r
3258 //\r
3259 // Check if the Firmware Volume is not corrupted\r
3260 //\r
3261 if ((FvHeader->Signature != EFI_FVH_SIGNATURE) || (!CompareGuid (&gEfiSystemNvDataFvGuid, &FvHeader->FileSystemGuid))) {\r
3262 FreePool (NvStorageData);\r
3263 DEBUG ((EFI_D_ERROR, "Firmware Volume for Variable Store is corrupted\n"));\r
3264 return EFI_VOLUME_CORRUPTED;\r
3265 }\r
3266\r
3267 VariableStoreBase = (EFI_PHYSICAL_ADDRESS) ((UINTN) FvHeader + FvHeader->HeaderLength);\r
3268 VariableStoreLength = (UINT64) (NvStorageSize - FvHeader->HeaderLength);\r
3269\r
3270 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase;\r
3271 mNvVariableCache = (VARIABLE_STORE_HEADER *) (UINTN) VariableStoreBase;\r
3272 if (GetVariableStoreStatus (mNvVariableCache) != EfiValid) {\r
3273 FreePool (NvStorageData);\r
3274 DEBUG((EFI_D_ERROR, "Variable Store header is corrupted\n"));\r
3275 return EFI_VOLUME_CORRUPTED;\r
3276 }\r
3277 ASSERT(mNvVariableCache->Size == VariableStoreLength);\r
3278\r
3279\r
3280 ASSERT (sizeof (VARIABLE_STORE_HEADER) <= VariableStoreLength);\r
3281\r
3282 HwErrStorageSize = PcdGet32 (PcdHwErrStorageSize);\r
3283 MaxUserNvVariableSpaceSize = PcdGet32 (PcdMaxUserNvVariableSpaceSize);\r
3284 BoottimeReservedNvVariableSpaceSize = PcdGet32 (PcdBoottimeReservedNvVariableSpaceSize);\r
3285\r
3286 //\r
3287 // Note that in EdkII variable driver implementation, Hardware Error Record type variable\r
3288 // is stored with common variable in the same NV region. So the platform integrator should\r
3289 // ensure that the value of PcdHwErrStorageSize is less than the value of\r
3290 // VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)).\r
3291 //\r
3292 ASSERT (HwErrStorageSize < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)));\r
3293 //\r
3294 // Ensure that the value of PcdMaxUserNvVariableSpaceSize is less than the value of\r
3295 // VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)) - PcdGet32 (PcdHwErrStorageSize).\r
3296 //\r
3297 ASSERT (MaxUserNvVariableSpaceSize < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER) - HwErrStorageSize));\r
3298 //\r
3299 // Ensure that the value of PcdBoottimeReservedNvVariableSpaceSize is less than the value of\r
3300 // VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)) - PcdGet32 (PcdHwErrStorageSize).\r
3301 //\r
3302 ASSERT (BoottimeReservedNvVariableSpaceSize < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER) - HwErrStorageSize));\r
3303\r
3304 mVariableModuleGlobal->CommonVariableSpace = ((UINTN) VariableStoreLength - sizeof (VARIABLE_STORE_HEADER) - HwErrStorageSize);\r
3305 mVariableModuleGlobal->CommonMaxUserVariableSpace = ((MaxUserNvVariableSpaceSize != 0) ? MaxUserNvVariableSpaceSize : mVariableModuleGlobal->CommonVariableSpace);\r
3306 mVariableModuleGlobal->CommonRuntimeVariableSpace = mVariableModuleGlobal->CommonVariableSpace - BoottimeReservedNvVariableSpaceSize;\r
3307\r
3308 DEBUG ((EFI_D_INFO, "Variable driver common space: 0x%x 0x%x 0x%x\n", mVariableModuleGlobal->CommonVariableSpace, mVariableModuleGlobal->CommonMaxUserVariableSpace, mVariableModuleGlobal->CommonRuntimeVariableSpace));\r
3309\r
3310 //\r
3311 // The max variable or hardware error variable size should be < variable store size.\r
3312 //\r
3313 ASSERT(MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize)) < VariableStoreLength);\r
3314\r
3315 //\r
3316 // Parse non-volatile variable data and get last variable offset.\r
3317 //\r
3318 Variable = GetStartPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase);\r
3319 while (IsValidVariableHeader (Variable, GetEndPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase))) {\r
3320 NextVariable = GetNextVariablePtr (Variable);\r
3321 VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
3322 if ((Variable->Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
3323 mVariableModuleGlobal->HwErrVariableTotalSize += VariableSize;\r
3324 } else {\r
3325 mVariableModuleGlobal->CommonVariableTotalSize += VariableSize;\r
3326 }\r
3327\r
3328 Variable = NextVariable;\r
3329 }\r
3330 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) Variable - (UINTN) VariableStoreBase;\r
3331\r
3332 return EFI_SUCCESS;\r
3333}\r
3334\r
3335/**\r
3336 Flush the HOB variable to flash.\r
3337\r
3338 @param[in] VariableName Name of variable has been updated or deleted.\r
3339 @param[in] VendorGuid Guid of variable has been updated or deleted.\r
3340\r
3341**/\r
3342VOID\r
3343FlushHobVariableToFlash (\r
3344 IN CHAR16 *VariableName,\r
3345 IN EFI_GUID *VendorGuid\r
3346 )\r
3347{\r
3348 EFI_STATUS Status;\r
3349 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
3350 VARIABLE_HEADER *Variable;\r
3351 VOID *VariableData;\r
3352 BOOLEAN ErrorFlag;\r
3353\r
3354 ErrorFlag = FALSE;\r
3355\r
3356 //\r
3357 // Flush the HOB variable to flash.\r
3358 //\r
3359 if (mVariableModuleGlobal->VariableGlobal.HobVariableBase != 0) {\r
3360 VariableStoreHeader = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;\r
3361 //\r
3362 // Set HobVariableBase to 0, it can avoid SetVariable to call back.\r
3363 //\r
3364 mVariableModuleGlobal->VariableGlobal.HobVariableBase = 0;\r
3365 for ( Variable = GetStartPointer (VariableStoreHeader)\r
3366 ; IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))\r
3367 ; Variable = GetNextVariablePtr (Variable)\r
3368 ) {\r
3369 if (Variable->State != VAR_ADDED) {\r
3370 //\r
3371 // The HOB variable has been set to DELETED state in local.\r
3372 //\r
3373 continue;\r
3374 }\r
3375 ASSERT ((Variable->Attributes & EFI_VARIABLE_NON_VOLATILE) != 0);\r
3376 if (VendorGuid == NULL || VariableName == NULL ||\r
3377 !CompareGuid (VendorGuid, &Variable->VendorGuid) ||\r
3378 StrCmp (VariableName, GetVariableNamePtr (Variable)) != 0) {\r
3379 VariableData = GetVariableDataPtr (Variable);\r
3380 Status = VariableServiceSetVariable (\r
3381 GetVariableNamePtr (Variable),\r
3382 &Variable->VendorGuid,\r
3383 Variable->Attributes,\r
3384 Variable->DataSize,\r
3385 VariableData\r
3386 );\r
3387 DEBUG ((EFI_D_INFO, "Variable driver flush the HOB variable to flash: %g %s %r\n", &Variable->VendorGuid, GetVariableNamePtr (Variable), Status));\r
3388 } else {\r
3389 //\r
3390 // The updated or deleted variable is matched with the HOB variable.\r
3391 // Don't break here because we will try to set other HOB variables\r
3392 // since this variable could be set successfully.\r
3393 //\r
3394 Status = EFI_SUCCESS;\r
3395 }\r
3396 if (!EFI_ERROR (Status)) {\r
3397 //\r
3398 // If set variable successful, or the updated or deleted variable is matched with the HOB variable,\r
3399 // set the HOB variable to DELETED state in local.\r
3400 //\r
3401 DEBUG ((EFI_D_INFO, "Variable driver set the HOB variable to DELETED state in local: %g %s\n", &Variable->VendorGuid, GetVariableNamePtr (Variable)));\r
3402 Variable->State &= VAR_DELETED;\r
3403 } else {\r
3404 ErrorFlag = TRUE;\r
3405 }\r
3406 }\r
3407 if (ErrorFlag) {\r
3408 //\r
3409 // We still have HOB variable(s) not flushed in flash.\r
3410 //\r
3411 mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VariableStoreHeader;\r
3412 } else {\r
3413 //\r
3414 // All HOB variables have been flushed in flash.\r
3415 //\r
3416 DEBUG ((EFI_D_INFO, "Variable driver: all HOB variables have been flushed in flash.\n"));\r
3417 if (!AtRuntime ()) {\r
3418 FreePool ((VOID *) VariableStoreHeader);\r
3419 }\r
3420 }\r
3421 }\r
3422\r
3423}\r
3424\r
3425/**\r
3426 Initializes variable write service after FTW was ready.\r
3427\r
3428 @retval EFI_SUCCESS Function successfully executed.\r
3429 @retval Others Fail to initialize the variable service.\r
3430\r
3431**/\r
3432EFI_STATUS\r
3433VariableWriteServiceInitialize (\r
3434 VOID\r
3435 )\r
3436{\r
3437 EFI_STATUS Status;\r
3438 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
3439 UINTN Index;\r
3440 UINT8 Data;\r
3441 EFI_PHYSICAL_ADDRESS VariableStoreBase;\r
3442 EFI_PHYSICAL_ADDRESS NvStorageBase;\r
3443\r
3444 NvStorageBase = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);\r
3445 if (NvStorageBase == 0) {\r
3446 NvStorageBase = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);\r
3447 }\r
3448 VariableStoreBase = NvStorageBase + (((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(NvStorageBase))->HeaderLength);\r
3449\r
3450 //\r
3451 // Let NonVolatileVariableBase point to flash variable store base directly after FTW ready.\r
3452 //\r
3453 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase;\r
3454 VariableStoreHeader = (VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase;\r
3455 \r
3456 //\r
3457 // Check if the free area is really free.\r
3458 //\r
3459 for (Index = mVariableModuleGlobal->NonVolatileLastVariableOffset; Index < VariableStoreHeader->Size; Index++) {\r
3460 Data = ((UINT8 *) mNvVariableCache)[Index];\r
3461 if (Data != 0xff) {\r
3462 //\r
3463 // There must be something wrong in variable store, do reclaim operation.\r
3464 //\r
3465 Status = Reclaim (\r
3466 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
3467 &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
3468 FALSE,\r
3469 NULL,\r
3470 NULL,\r
3471 0\r
3472 );\r
3473 if (EFI_ERROR (Status)) {\r
3474 return Status;\r
3475 }\r
3476 break;\r
3477 }\r
3478 }\r
3479\r
3480 FlushHobVariableToFlash (NULL, NULL);\r
3481\r
3482 return EFI_SUCCESS;\r
3483}\r
3484\r
3485\r
3486/**\r
3487 Initializes variable store area for non-volatile and volatile variable.\r
3488\r
3489 @retval EFI_SUCCESS Function successfully executed.\r
3490 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.\r
3491\r
3492**/\r
3493EFI_STATUS\r
3494VariableCommonInitialize (\r
3495 VOID\r
3496 )\r
3497{\r
3498 EFI_STATUS Status;\r
3499 VARIABLE_STORE_HEADER *VolatileVariableStore;\r
3500 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
3501 UINT64 VariableStoreLength;\r
3502 UINTN ScratchSize;\r
3503 EFI_HOB_GUID_TYPE *GuidHob;\r
3504\r
3505 //\r
3506 // Allocate runtime memory for variable driver global structure.\r
3507 //\r
3508 mVariableModuleGlobal = AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL));\r
3509 if (mVariableModuleGlobal == NULL) {\r
3510 return EFI_OUT_OF_RESOURCES;\r
3511 }\r
3512\r
3513 InitializeLock (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock, TPL_NOTIFY);\r
3514\r
3515 //\r
3516 // Get HOB variable store.\r
3517 //\r
3518 GuidHob = GetFirstGuidHob (&gEfiVariableGuid);\r
3519 if (GuidHob != NULL) {\r
3520 VariableStoreHeader = GET_GUID_HOB_DATA (GuidHob);\r
3521 VariableStoreLength = (UINT64) (GuidHob->Header.HobLength - sizeof (EFI_HOB_GUID_TYPE));\r
3522 if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) {\r
3523 mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) AllocateRuntimeCopyPool ((UINTN) VariableStoreLength, (VOID *) VariableStoreHeader);\r
3524 if (mVariableModuleGlobal->VariableGlobal.HobVariableBase == 0) {\r
3525 FreePool (mVariableModuleGlobal);\r
3526 return EFI_OUT_OF_RESOURCES;\r
3527 }\r
3528 } else {\r
3529 DEBUG ((EFI_D_ERROR, "HOB Variable Store header is corrupted!\n"));\r
3530 }\r
3531 }\r
3532\r
3533 //\r
3534 // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.\r
3535 //\r
3536 ScratchSize = MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize));\r
3537 VolatileVariableStore = AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize) + ScratchSize);\r
3538 if (VolatileVariableStore == NULL) {\r
3539 if (mVariableModuleGlobal->VariableGlobal.HobVariableBase != 0) {\r
3540 FreePool ((VOID *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase);\r
3541 }\r
3542 FreePool (mVariableModuleGlobal);\r
3543 return EFI_OUT_OF_RESOURCES;\r
3544 }\r
3545\r
3546 SetMem (VolatileVariableStore, PcdGet32 (PcdVariableStoreSize) + ScratchSize, 0xff);\r
3547\r
3548 //\r
3549 // Initialize Variable Specific Data.\r
3550 //\r
3551 mVariableModuleGlobal->VariableGlobal.VolatileVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VolatileVariableStore;\r
3552 mVariableModuleGlobal->VolatileLastVariableOffset = (UINTN) GetStartPointer (VolatileVariableStore) - (UINTN) VolatileVariableStore;\r
3553\r
3554 CopyGuid (&VolatileVariableStore->Signature, &gEfiVariableGuid);\r
3555 VolatileVariableStore->Size = PcdGet32 (PcdVariableStoreSize);\r
3556 VolatileVariableStore->Format = VARIABLE_STORE_FORMATTED;\r
3557 VolatileVariableStore->State = VARIABLE_STORE_HEALTHY;\r
3558 VolatileVariableStore->Reserved = 0;\r
3559 VolatileVariableStore->Reserved1 = 0;\r
3560\r
3561 //\r
3562 // Init non-volatile variable store.\r
3563 //\r
3564 Status = InitNonVolatileVariableStore ();\r
3565 if (EFI_ERROR (Status)) {\r
3566 if (mVariableModuleGlobal->VariableGlobal.HobVariableBase != 0) {\r
3567 FreePool ((VOID *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase);\r
3568 }\r
3569 FreePool (mVariableModuleGlobal);\r
3570 FreePool (VolatileVariableStore);\r
3571 }\r
3572\r
3573 return Status;\r
3574}\r
3575\r
3576\r
3577/**\r
3578 Get the proper fvb handle and/or fvb protocol by the given Flash address.\r
3579\r
3580 @param[in] Address The Flash address.\r
3581 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.\r
3582 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.\r
3583\r
3584**/\r
3585EFI_STATUS\r
3586GetFvbInfoByAddress (\r
3587 IN EFI_PHYSICAL_ADDRESS Address,\r
3588 OUT EFI_HANDLE *FvbHandle OPTIONAL,\r
3589 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvbProtocol OPTIONAL\r
3590 )\r
3591{\r
3592 EFI_STATUS Status;\r
3593 EFI_HANDLE *HandleBuffer;\r
3594 UINTN HandleCount;\r
3595 UINTN Index;\r
3596 EFI_PHYSICAL_ADDRESS FvbBaseAddress;\r
3597 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r
3598 EFI_FVB_ATTRIBUTES_2 Attributes;\r
3599 UINTN BlockSize;\r
3600 UINTN NumberOfBlocks;\r
3601\r
3602 HandleBuffer = NULL;\r
3603\r
3604 //\r
3605 // Get all FVB handles.\r
3606 //\r
3607 Status = GetFvbCountAndBuffer (&HandleCount, &HandleBuffer);\r
3608 if (EFI_ERROR (Status)) {\r
3609 return EFI_NOT_FOUND;\r
3610 }\r
3611\r
3612 //\r
3613 // Get the FVB to access variable store.\r
3614 //\r
3615 Fvb = NULL;\r
3616 for (Index = 0; Index < HandleCount; Index += 1, Status = EFI_NOT_FOUND, Fvb = NULL) {\r
3617 Status = GetFvbByHandle (HandleBuffer[Index], &Fvb);\r
3618 if (EFI_ERROR (Status)) {\r
3619 Status = EFI_NOT_FOUND;\r
3620 break;\r
3621 }\r
3622\r
3623 //\r
3624 // Ensure this FVB protocol supported Write operation.\r
3625 //\r
3626 Status = Fvb->GetAttributes (Fvb, &Attributes);\r
3627 if (EFI_ERROR (Status) || ((Attributes & EFI_FVB2_WRITE_STATUS) == 0)) {\r
3628 continue;\r
3629 }\r
3630\r
3631 //\r
3632 // Compare the address and select the right one.\r
3633 //\r
3634 Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);\r
3635 if (EFI_ERROR (Status)) {\r
3636 continue;\r
3637 }\r
3638\r
3639 //\r
3640 // Assume one FVB has one type of BlockSize.\r
3641 //\r
3642 Status = Fvb->GetBlockSize (Fvb, 0, &BlockSize, &NumberOfBlocks);\r
3643 if (EFI_ERROR (Status)) {\r
3644 continue;\r
3645 }\r
3646\r
3647 if ((Address >= FvbBaseAddress) && (Address < (FvbBaseAddress + BlockSize * NumberOfBlocks))) {\r
3648 if (FvbHandle != NULL) {\r
3649 *FvbHandle = HandleBuffer[Index];\r
3650 }\r
3651 if (FvbProtocol != NULL) {\r
3652 *FvbProtocol = Fvb;\r
3653 }\r
3654 Status = EFI_SUCCESS;\r
3655 break;\r
3656 }\r
3657 }\r
3658 FreePool (HandleBuffer);\r
3659\r
3660 if (Fvb == NULL) {\r
3661 Status = EFI_NOT_FOUND;\r
3662 }\r
3663 \r
3664 return Status; \r
3665}\r
3666\r