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