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