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