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