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