]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
Update code logic, remove ASSERT and use error handling.
[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
1271 @param ... Variable argument list with type VARIABLE_ENTRY_CONSISTENCY *.\r
1272 A NULL terminates the list.\r
1273\r
1274 @retval TRUE Have enough variable space to set the Variables successfully.\r
1275 @retval FALSE No enough variable space to set the Variables successfully.\r
1276\r
1277**/\r
1278BOOLEAN\r
1279EFIAPI\r
1280CheckRemainingSpaceForConsistency (\r
1281 IN UINT32 Attributes,\r
1282 ...\r
1283 )\r
1284{\r
1285 EFI_STATUS Status;\r
1286 VA_LIST Args;\r
1287 VARIABLE_ENTRY_CONSISTENCY *VariableEntry;\r
1288 UINT64 MaximumVariableStorageSize;\r
1289 UINT64 RemainingVariableStorageSize;\r
1290 UINT64 MaximumVariableSize;\r
1291 UINTN TotalNeededSize;\r
1292 UINTN OriginalVarSize;\r
1293 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
1294 VARIABLE_POINTER_TRACK VariablePtrTrack;\r
1295 VARIABLE_HEADER *NextVariable;\r
1296\r
1297 //\r
1298 // Non-Volatile related.\r
1299 //\r
1300 VariableStoreHeader = mNvVariableCache;\r
1301\r
1302 Status = VariableServiceQueryVariableInfoInternal (\r
1303 Attributes,\r
1304 &MaximumVariableStorageSize,\r
1305 &RemainingVariableStorageSize,\r
1306 &MaximumVariableSize\r
1307 );\r
1308 ASSERT_EFI_ERROR (Status);\r
1309\r
1310 TotalNeededSize = 0;\r
1311 VA_START (Args, Attributes);\r
1312 VariableEntry = VA_ARG (Args, VARIABLE_ENTRY_CONSISTENCY *);\r
1313 while (VariableEntry != NULL) {\r
1314 TotalNeededSize += VariableEntry->VariableSize;\r
1315 VariableEntry = VA_ARG (Args, VARIABLE_ENTRY_CONSISTENCY *);\r
1316 }\r
1317 VA_END (Args);\r
1318\r
1319 if (RemainingVariableStorageSize >= TotalNeededSize) {\r
1320 //\r
1321 // Already have enough space.\r
1322 //\r
1323 return TRUE;\r
1324 } else if (AtRuntime ()) {\r
1325 //\r
1326 // At runtime, no reclaim.\r
1327 // The original variable space of Variables can't be reused.\r
1328 //\r
1329 return FALSE;\r
1330 }\r
1331\r
1332 VA_START (Args, Attributes);\r
1333 VariableEntry = VA_ARG (Args, VARIABLE_ENTRY_CONSISTENCY *);\r
1334 while (VariableEntry != NULL) {\r
1335 //\r
1336 // Check if Variable[Index] has been present and get its size.\r
1337 //\r
1338 OriginalVarSize = 0;\r
1339 VariablePtrTrack.StartPtr = GetStartPointer (VariableStoreHeader);\r
1340 VariablePtrTrack.EndPtr = GetEndPointer (VariableStoreHeader);\r
1341 Status = FindVariableEx (\r
1342 VariableEntry->Name,\r
1343 VariableEntry->Guid,\r
1344 FALSE,\r
1345 &VariablePtrTrack\r
1346 );\r
1347 if (!EFI_ERROR (Status)) {\r
1348 //\r
1349 // Get size of Variable[Index].\r
1350 //\r
1351 NextVariable = GetNextVariablePtr (VariablePtrTrack.CurrPtr);\r
1352 OriginalVarSize = (UINTN) NextVariable - (UINTN) VariablePtrTrack.CurrPtr;\r
1353 //\r
1354 // Add the original size of Variable[Index] to remaining variable storage size.\r
1355 //\r
1356 RemainingVariableStorageSize += OriginalVarSize;\r
1357 }\r
1358 if (VariableEntry->VariableSize > RemainingVariableStorageSize) {\r
1359 //\r
1360 // No enough space for Variable[Index].\r
1361 //\r
1362 VA_END (Args);\r
1363 return FALSE;\r
1364 }\r
1365 //\r
1366 // Sub the (new) size of Variable[Index] from remaining variable storage size.\r
1367 //\r
1368 RemainingVariableStorageSize -= VariableEntry->VariableSize;\r
1369 VariableEntry = VA_ARG (Args, VARIABLE_ENTRY_CONSISTENCY *);\r
1370 }\r
1371 VA_END (Args);\r
1372\r
1373 return TRUE;\r
1374}\r
1375\r
72399dae 1376/**\r
1377 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.\r
052ad7e1 1378\r
72399dae 1379 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.\r
052ad7e1 1380\r
72399dae 1381 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,\r
1382 and are read-only. Therefore, in variable driver, only store the original value for other use.\r
8d3a5c82 1383\r
8a2d4996 1384 @param[in] VariableName Name of variable.\r
8d3a5c82 1385\r
8a2d4996 1386 @param[in] Data Variable data.\r
8d3a5c82 1387\r
8a2d4996 1388 @param[in] DataSize Size of data. 0 means delete.\r
72399dae 1389\r
9bc5dabb
SZ
1390 @retval EFI_SUCCESS The update operation is successful or ignored.\r
1391 @retval EFI_WRITE_PROTECTED Update PlatformLangCodes/LangCodes at runtime.\r
1392 @retval EFI_OUT_OF_RESOURCES No enough variable space to do the update operation.\r
1393 @retval Others Other errors happened during the update operation.\r
1394\r
7c80e839 1395**/\r
9bc5dabb 1396EFI_STATUS\r
d6550260 1397AutoUpdateLangVariable (\r
72399dae 1398 IN CHAR16 *VariableName,\r
1399 IN VOID *Data,\r
1400 IN UINTN DataSize\r
052ad7e1 1401 )\r
8d3a5c82 1402{\r
255a3f33
RN
1403 EFI_STATUS Status;\r
1404 CHAR8 *BestPlatformLang;\r
1405 CHAR8 *BestLang;\r
1406 UINTN Index;\r
1407 UINT32 Attributes;\r
72399dae 1408 VARIABLE_POINTER_TRACK Variable;\r
255a3f33 1409 BOOLEAN SetLanguageCodes;\r
b2bd493e
SZ
1410 UINTN VarNameSize;\r
1411 UINTN VarDataSize;\r
1412 VARIABLE_ENTRY_CONSISTENCY VariableEntry[2];\r
8d3a5c82 1413\r
72399dae 1414 //\r
255a3f33 1415 // Don't do updates for delete operation\r
72399dae 1416 //\r
255a3f33 1417 if (DataSize == 0) {\r
9bc5dabb 1418 return EFI_SUCCESS;\r
255a3f33
RN
1419 }\r
1420\r
1421 SetLanguageCodes = FALSE;\r
8d3a5c82 1422\r
6675a21f 1423 if (StrCmp (VariableName, EFI_PLATFORM_LANG_CODES_VARIABLE_NAME) == 0) {\r
255a3f33
RN
1424 //\r
1425 // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.\r
1426 //\r
8a2d4996 1427 if (AtRuntime ()) {\r
9bc5dabb 1428 return EFI_WRITE_PROTECTED;\r
255a3f33
RN
1429 }\r
1430\r
1431 SetLanguageCodes = TRUE;\r
1432\r
72399dae 1433 //\r
1434 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only\r
1435 // Therefore, in variable driver, only store the original value for other use.\r
1436 //\r
255a3f33
RN
1437 if (mVariableModuleGlobal->PlatformLangCodes != NULL) {\r
1438 FreePool (mVariableModuleGlobal->PlatformLangCodes);\r
1439 }\r
1440 mVariableModuleGlobal->PlatformLangCodes = AllocateRuntimeCopyPool (DataSize, Data);\r
1441 ASSERT (mVariableModuleGlobal->PlatformLangCodes != NULL);\r
1442\r
72399dae 1443 //\r
255a3f33
RN
1444 // PlatformLang holds a single language from PlatformLangCodes, \r
1445 // so the size of PlatformLangCodes is enough for the PlatformLang.\r
72399dae 1446 //\r
255a3f33
RN
1447 if (mVariableModuleGlobal->PlatformLang != NULL) {\r
1448 FreePool (mVariableModuleGlobal->PlatformLang);\r
1449 }\r
1450 mVariableModuleGlobal->PlatformLang = AllocateRuntimePool (DataSize);\r
1451 ASSERT (mVariableModuleGlobal->PlatformLang != NULL);\r
fdb7765f 1452\r
6675a21f 1453 } else if (StrCmp (VariableName, EFI_LANG_CODES_VARIABLE_NAME) == 0) {\r
72399dae 1454 //\r
255a3f33 1455 // LangCodes is a volatile variable, so it can not be updated at runtime.\r
72399dae 1456 //\r
8a2d4996 1457 if (AtRuntime ()) {\r
9bc5dabb 1458 return EFI_WRITE_PROTECTED;\r
255a3f33
RN
1459 }\r
1460\r
1461 SetLanguageCodes = TRUE;\r
8d3a5c82 1462\r
8d3a5c82 1463 //\r
255a3f33
RN
1464 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only\r
1465 // Therefore, in variable driver, only store the original value for other use.\r
8d3a5c82 1466 //\r
255a3f33
RN
1467 if (mVariableModuleGlobal->LangCodes != NULL) {\r
1468 FreePool (mVariableModuleGlobal->LangCodes);\r
1469 }\r
1470 mVariableModuleGlobal->LangCodes = AllocateRuntimeCopyPool (DataSize, Data);\r
1471 ASSERT (mVariableModuleGlobal->LangCodes != NULL);\r
1472 }\r
8d3a5c82 1473\r
255a3f33
RN
1474 if (SetLanguageCodes \r
1475 && (mVariableModuleGlobal->PlatformLangCodes != NULL)\r
1476 && (mVariableModuleGlobal->LangCodes != NULL)) {\r
8d3a5c82 1477 //\r
255a3f33
RN
1478 // Update Lang if PlatformLang is already set\r
1479 // Update PlatformLang if Lang is already set\r
8d3a5c82 1480 //\r
6675a21f 1481 Status = FindVariable (EFI_PLATFORM_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
255a3f33
RN
1482 if (!EFI_ERROR (Status)) {\r
1483 //\r
1484 // Update Lang\r
1485 //\r
6675a21f 1486 VariableName = EFI_PLATFORM_LANG_VARIABLE_NAME;\r
255a3f33
RN
1487 Data = GetVariableDataPtr (Variable.CurrPtr);\r
1488 DataSize = Variable.CurrPtr->DataSize;\r
1489 } else {\r
6675a21f 1490 Status = FindVariable (EFI_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
255a3f33
RN
1491 if (!EFI_ERROR (Status)) {\r
1492 //\r
1493 // Update PlatformLang\r
1494 //\r
6675a21f 1495 VariableName = EFI_LANG_VARIABLE_NAME;\r
255a3f33
RN
1496 Data = GetVariableDataPtr (Variable.CurrPtr);\r
1497 DataSize = Variable.CurrPtr->DataSize;\r
1498 } else {\r
1499 //\r
1500 // Neither PlatformLang nor Lang is set, directly return\r
1501 //\r
9bc5dabb 1502 return EFI_SUCCESS;\r
255a3f33
RN
1503 }\r
1504 }\r
1505 }\r
9bc5dabb
SZ
1506\r
1507 Status = EFI_SUCCESS;\r
1508\r
255a3f33
RN
1509 //\r
1510 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.\r
1511 //\r
1512 Attributes = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;\r
8d3a5c82 1513\r
6675a21f 1514 if (StrCmp (VariableName, EFI_PLATFORM_LANG_VARIABLE_NAME) == 0) {\r
8d3a5c82 1515 //\r
255a3f33 1516 // Update Lang when PlatformLangCodes/LangCodes were set.\r
8d3a5c82 1517 //\r
255a3f33
RN
1518 if ((mVariableModuleGlobal->PlatformLangCodes != NULL) && (mVariableModuleGlobal->LangCodes != NULL)) {\r
1519 //\r
1520 // When setting PlatformLang, firstly get most matched language string from supported language codes.\r
1521 //\r
1522 BestPlatformLang = VariableGetBestLanguage (mVariableModuleGlobal->PlatformLangCodes, FALSE, Data, NULL);\r
1523 if (BestPlatformLang != NULL) {\r
1524 //\r
1525 // Get the corresponding index in language codes.\r
1526 //\r
1527 Index = GetIndexFromSupportedLangCodes (mVariableModuleGlobal->PlatformLangCodes, BestPlatformLang, FALSE);\r
fdb7765f 1528\r
255a3f33
RN
1529 //\r
1530 // Get the corresponding ISO639 language tag according to RFC4646 language tag.\r
1531 //\r
1532 BestLang = GetLangFromSupportedLangCodes (mVariableModuleGlobal->LangCodes, Index, TRUE);\r
8d3a5c82 1533\r
255a3f33 1534 //\r
b2bd493e
SZ
1535 // Calculate the needed variable size for Lang variable.\r
1536 //\r
1537 VarNameSize = StrSize (EFI_LANG_VARIABLE_NAME);\r
1538 VarDataSize = ISO_639_2_ENTRY_SIZE + 1;\r
1539 VariableEntry[0].VariableSize = sizeof (VARIABLE_HEADER) + VarNameSize + GET_PAD_SIZE (VarNameSize) + VarDataSize + GET_PAD_SIZE (VarDataSize);\r
1540 VariableEntry[0].VariableSize = HEADER_ALIGN (VariableEntry[0].VariableSize);\r
1541 VariableEntry[0].Guid = &gEfiGlobalVariableGuid;\r
1542 VariableEntry[0].Name = EFI_LANG_VARIABLE_NAME;\r
255a3f33 1543 //\r
b2bd493e
SZ
1544 // Calculate the needed variable size for PlatformLang variable.\r
1545 //\r
1546 VarNameSize = StrSize (EFI_PLATFORM_LANG_VARIABLE_NAME);\r
1547 VarDataSize = AsciiStrSize (BestPlatformLang);\r
1548 VariableEntry[1].VariableSize = sizeof (VARIABLE_HEADER) + VarNameSize + GET_PAD_SIZE (VarNameSize) + VarDataSize + GET_PAD_SIZE (VarDataSize);\r
1549 VariableEntry[1].VariableSize = HEADER_ALIGN (VariableEntry[1].VariableSize);\r
1550 VariableEntry[1].Guid = &gEfiGlobalVariableGuid;\r
1551 VariableEntry[1].Name = EFI_PLATFORM_LANG_VARIABLE_NAME;\r
1552 if (!CheckRemainingSpaceForConsistency (VARIABLE_ATTRIBUTE_NV_BS_RT, &VariableEntry[0], &VariableEntry[1], NULL)) {\r
1553 //\r
1554 // No enough variable space to set both Lang and PlatformLang successfully.\r
1555 //\r
1556 Status = EFI_OUT_OF_RESOURCES;\r
1557 } else {\r
1558 //\r
1559 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.\r
1560 //\r
1561 FindVariable (EFI_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
8d3a5c82 1562\r
b2bd493e
SZ
1563 Status = UpdateVariable (EFI_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, BestLang,\r
1564 ISO_639_2_ENTRY_SIZE + 1, Attributes, &Variable);\r
1565 }\r
8d3a5c82 1566\r
b2bd493e 1567 DEBUG ((EFI_D_INFO, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a Status: %r\n", BestPlatformLang, BestLang, Status));\r
255a3f33
RN
1568 }\r
1569 }\r
72399dae 1570\r
6675a21f 1571 } else if (StrCmp (VariableName, EFI_LANG_VARIABLE_NAME) == 0) {\r
72399dae 1572 //\r
255a3f33 1573 // Update PlatformLang when PlatformLangCodes/LangCodes were set.\r
72399dae 1574 //\r
255a3f33
RN
1575 if ((mVariableModuleGlobal->PlatformLangCodes != NULL) && (mVariableModuleGlobal->LangCodes != NULL)) {\r
1576 //\r
1577 // When setting Lang, firstly get most matched language string from supported language codes.\r
1578 //\r
1579 BestLang = VariableGetBestLanguage (mVariableModuleGlobal->LangCodes, TRUE, Data, NULL);\r
1580 if (BestLang != NULL) {\r
1581 //\r
1582 // Get the corresponding index in language codes.\r
1583 //\r
1584 Index = GetIndexFromSupportedLangCodes (mVariableModuleGlobal->LangCodes, BestLang, TRUE);\r
72399dae 1585\r
255a3f33
RN
1586 //\r
1587 // Get the corresponding RFC4646 language tag according to ISO639 language tag.\r
1588 //\r
1589 BestPlatformLang = GetLangFromSupportedLangCodes (mVariableModuleGlobal->PlatformLangCodes, Index, FALSE);\r
1590\r
1591 //\r
b2bd493e
SZ
1592 // Calculate the needed variable size for PlatformLang variable.\r
1593 //\r
1594 VarNameSize = StrSize (EFI_PLATFORM_LANG_VARIABLE_NAME);\r
1595 VarDataSize = AsciiStrSize (BestPlatformLang);\r
1596 VariableEntry[0].VariableSize = sizeof (VARIABLE_HEADER) + VarNameSize + GET_PAD_SIZE (VarNameSize) + VarDataSize + GET_PAD_SIZE (VarDataSize);\r
1597 VariableEntry[0].VariableSize = HEADER_ALIGN (VariableEntry[0].VariableSize);\r
1598 VariableEntry[0].Guid = &gEfiGlobalVariableGuid;\r
1599 VariableEntry[0].Name = EFI_PLATFORM_LANG_VARIABLE_NAME;\r
255a3f33 1600 //\r
b2bd493e
SZ
1601 // Calculate the needed variable size for Lang variable.\r
1602 //\r
1603 VarNameSize = StrSize (EFI_LANG_VARIABLE_NAME);\r
1604 VarDataSize = ISO_639_2_ENTRY_SIZE + 1;\r
1605 VariableEntry[1].VariableSize = sizeof (VARIABLE_HEADER) + VarNameSize + GET_PAD_SIZE (VarNameSize) + VarDataSize + GET_PAD_SIZE (VarDataSize);\r
1606 VariableEntry[1].VariableSize = HEADER_ALIGN (VariableEntry[1].VariableSize);\r
1607 VariableEntry[1].Guid = &gEfiGlobalVariableGuid;\r
1608 VariableEntry[1].Name = EFI_LANG_VARIABLE_NAME;\r
1609 if (!CheckRemainingSpaceForConsistency (VARIABLE_ATTRIBUTE_NV_BS_RT, &VariableEntry[0], &VariableEntry[1], NULL)) {\r
1610 //\r
1611 // No enough variable space to set both PlatformLang and Lang successfully.\r
1612 //\r
1613 Status = EFI_OUT_OF_RESOURCES;\r
1614 } else {\r
1615 //\r
1616 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.\r
1617 //\r
1618 FindVariable (EFI_PLATFORM_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
72399dae 1619\r
b2bd493e
SZ
1620 Status = UpdateVariable (EFI_PLATFORM_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, BestPlatformLang, \r
1621 AsciiStrSize (BestPlatformLang), Attributes, &Variable);\r
1622 }\r
72399dae 1623\r
9bc5dabb 1624 DEBUG ((EFI_D_INFO, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a Status: %r\n", BestLang, BestPlatformLang, Status));\r
255a3f33
RN
1625 }\r
1626 }\r
72399dae 1627 }\r
9bc5dabb 1628\r
b2bd493e
SZ
1629 if (SetLanguageCodes) {\r
1630 //\r
1631 // Continue to set PlatformLangCodes or LangCodes.\r
1632 //\r
1633 return EFI_SUCCESS;\r
1634 } else {\r
1635 return Status;\r
1636 }\r
8d3a5c82 1637}\r
1638\r
7c80e839 1639/**\r
72399dae 1640 Update the variable region with Variable information. These are the same \r
1641 arguments as the EFI Variable services.\r
052ad7e1 1642\r
8a2d4996 1643 @param[in] VariableName Name of variable.\r
1644 @param[in] VendorGuid Guid of variable.\r
1645 @param[in] Data Variable data.\r
1646 @param[in] DataSize Size of data. 0 means delete.\r
1647 @param[in] Attributes Attribues of the variable.\r
23b06935 1648 @param[in, out] CacheVariable The variable information which is used to keep track of variable usage.\r
8a2d4996 1649 \r
72399dae 1650 @retval EFI_SUCCESS The update operation is success.\r
72399dae 1651 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.\r
8d3a5c82 1652\r
7c80e839 1653**/\r
052ad7e1 1654EFI_STATUS\r
72399dae 1655UpdateVariable (\r
8a2d4996 1656 IN CHAR16 *VariableName,\r
1657 IN EFI_GUID *VendorGuid,\r
1658 IN VOID *Data,\r
1659 IN UINTN DataSize,\r
1660 IN UINT32 Attributes OPTIONAL,\r
23b06935 1661 IN OUT VARIABLE_POINTER_TRACK *CacheVariable\r
052ad7e1 1662 )\r
8d3a5c82 1663{\r
8a9e0b72 1664 EFI_STATUS Status;\r
1665 VARIABLE_HEADER *NextVariable;\r
72399dae 1666 UINTN ScratchSize;\r
1667 UINTN NonVolatileVarableStoreSize;\r
8a9e0b72 1668 UINTN VarNameOffset;\r
1669 UINTN VarDataOffset;\r
72399dae 1670 UINTN VarNameSize;\r
8a9e0b72 1671 UINTN VarSize;\r
72399dae 1672 BOOLEAN Volatile;\r
1673 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r
8a9e0b72 1674 UINT8 State;\r
8a2d4996 1675 VARIABLE_POINTER_TRACK *Variable;\r
1676 VARIABLE_POINTER_TRACK NvVariable;\r
1677 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
1678 UINTN CacheOffset;\r
fdb7765f 1679\r
5456306f 1680 if ((mVariableModuleGlobal->FvbInstance == NULL) && ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0)) {\r
1681 //\r
1682 // The FVB protocol is not ready. Trying to update NV variable prior to the installation\r
1683 // of EFI_VARIABLE_WRITE_ARCH_PROTOCOL.\r
1684 //\r
1685 return EFI_NOT_AVAILABLE_YET; \r
1686 }\r
1687\r
1688 if ((CacheVariable->CurrPtr == NULL) || CacheVariable->Volatile) {\r
8a2d4996 1689 Variable = CacheVariable;\r
1690 } else {\r
8a2d4996 1691 //\r
5456306f 1692 // Update/Delete existing NV variable.\r
8a2d4996 1693 // CacheVariable points to the variable in the memory copy of Flash area\r
1694 // Now let Variable points to the same variable in Flash area.\r
1695 //\r
1696 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);\r
1697 Variable = &NvVariable; \r
1698 Variable->StartPtr = GetStartPointer (VariableStoreHeader);\r
1699 Variable->EndPtr = GetEndPointer (VariableStoreHeader);\r
5456306f 1700 Variable->CurrPtr = (VARIABLE_HEADER *)((UINTN)Variable->StartPtr + ((UINTN)CacheVariable->CurrPtr - (UINTN)CacheVariable->StartPtr));\r
23b06935
SZ
1701 if (CacheVariable->InDeletedTransitionPtr != NULL) {\r
1702 Variable->InDeletedTransitionPtr = (VARIABLE_HEADER *)((UINTN)Variable->StartPtr + ((UINTN)CacheVariable->InDeletedTransitionPtr - (UINTN)CacheVariable->StartPtr));\r
1703 } else {\r
1704 Variable->InDeletedTransitionPtr = NULL;\r
1705 }\r
5456306f 1706 Variable->Volatile = FALSE;\r
1707 } \r
1708\r
1709 Fvb = mVariableModuleGlobal->FvbInstance;\r
fdb7765f 1710\r
72399dae 1711 if (Variable->CurrPtr != NULL) {\r
8d3a5c82 1712 //\r
8a2d4996 1713 // Update/Delete existing variable.\r
8d3a5c82 1714 //\r
8a2d4996 1715 if (AtRuntime ()) { \r
c6492839 1716 //\r
8a2d4996 1717 // If AtRuntime and the variable is Volatile and Runtime Access, \r
c6492839 1718 // the volatile is ReadOnly, and SetVariable should be aborted and \r
1719 // return EFI_WRITE_PROTECTED.\r
1720 //\r
72399dae 1721 if (Variable->Volatile) {\r
c6492839 1722 Status = EFI_WRITE_PROTECTED;\r
1723 goto Done;\r
1724 }\r
1725 //\r
9622df63 1726 // Only variable that have NV|RT attributes can be updated/deleted in Runtime.\r
c6492839 1727 //\r
9622df63 1728 if (((Variable->CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) || ((Variable->CurrPtr->Attributes & EFI_VARIABLE_NON_VOLATILE) == 0)) {\r
c6492839 1729 Status = EFI_INVALID_PARAMETER;\r
1730 goto Done; \r
1731 }\r
1732 }\r
8a2d4996 1733\r
8d3a5c82 1734 //\r
c6492839 1735 // Setting a data variable with no access, or zero DataSize attributes\r
8a2d4996 1736 // causes it to be deleted.\r
8d3a5c82 1737 //\r
c6492839 1738 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) { \r
23b06935
SZ
1739 if (Variable->InDeletedTransitionPtr != NULL) {\r
1740 //\r
1741 // Both ADDED and IN_DELETED_TRANSITION variable are present,\r
1742 // set IN_DELETED_TRANSITION one to DELETED state first.\r
1743 //\r
1744 State = Variable->InDeletedTransitionPtr->State;\r
1745 State &= VAR_DELETED;\r
1746 Status = UpdateVariableStore (\r
1747 &mVariableModuleGlobal->VariableGlobal,\r
1748 Variable->Volatile,\r
1749 FALSE,\r
1750 Fvb,\r
1751 (UINTN) &Variable->InDeletedTransitionPtr->State,\r
1752 sizeof (UINT8),\r
1753 &State\r
1754 );\r
1755 if (!EFI_ERROR (Status)) {\r
1756 if (!Variable->Volatile) {\r
0cc565de 1757 ASSERT (CacheVariable->InDeletedTransitionPtr != NULL);\r
23b06935
SZ
1758 CacheVariable->InDeletedTransitionPtr->State = State;\r
1759 }\r
1760 } else {\r
1761 goto Done;\r
1762 }\r
1763 }\r
1764\r
72399dae 1765 State = Variable->CurrPtr->State;\r
c6492839 1766 State &= VAR_DELETED;\r
1767\r
1768 Status = UpdateVariableStore (\r
052ad7e1 1769 &mVariableModuleGlobal->VariableGlobal,\r
72399dae 1770 Variable->Volatile,\r
c6492839 1771 FALSE,\r
8a9e0b72 1772 Fvb,\r
72399dae 1773 (UINTN) &Variable->CurrPtr->State,\r
c6492839 1774 sizeof (UINT8),\r
1775 &State\r
1776 ); \r
33a5a666 1777 if (!EFI_ERROR (Status)) {\r
8a2d4996 1778 UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, FALSE, TRUE, FALSE);\r
1779 if (!Variable->Volatile) {\r
1780 CacheVariable->CurrPtr->State = State;\r
335e2681 1781 FlushHobVariableToFlash (VariableName, VendorGuid);\r
8a2d4996 1782 }\r
33a5a666 1783 }\r
c6492839 1784 goto Done; \r
1785 }\r
8d3a5c82 1786 //\r
8a2d4996 1787 // If the variable is marked valid, and the same data has been passed in,\r
c6492839 1788 // then return to the caller immediately.\r
8d3a5c82 1789 //\r
72399dae 1790 if (DataSizeOfVariable (Variable->CurrPtr) == DataSize &&\r
1791 (CompareMem (Data, GetVariableDataPtr (Variable->CurrPtr), DataSize) == 0)) {\r
33a5a666 1792 \r
8a2d4996 1793 UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, TRUE, FALSE, FALSE);\r
c6492839 1794 Status = EFI_SUCCESS;\r
1795 goto Done;\r
72399dae 1796 } else if ((Variable->CurrPtr->State == VAR_ADDED) ||\r
1797 (Variable->CurrPtr->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {\r
814bae52 1798\r
c6492839 1799 //\r
8a2d4996 1800 // Mark the old variable as in delete transition.\r
c6492839 1801 //\r
72399dae 1802 State = Variable->CurrPtr->State;\r
c6492839 1803 State &= VAR_IN_DELETED_TRANSITION;\r
1804\r
1805 Status = UpdateVariableStore (\r
052ad7e1 1806 &mVariableModuleGlobal->VariableGlobal,\r
72399dae 1807 Variable->Volatile,\r
c6492839 1808 FALSE,\r
8a9e0b72 1809 Fvb,\r
72399dae 1810 (UINTN) &Variable->CurrPtr->State,\r
c6492839 1811 sizeof (UINT8),\r
1812 &State\r
1813 ); \r
1814 if (EFI_ERROR (Status)) {\r
1815 goto Done; \r
33a5a666 1816 } \r
8a2d4996 1817 if (!Variable->Volatile) {\r
1818 CacheVariable->CurrPtr->State = State;\r
1819 }\r
c6492839 1820 } \r
72399dae 1821 } else {\r
8d3a5c82 1822 //\r
8a2d4996 1823 // Not found existing variable. Create a new variable.\r
c6492839 1824 // \r
1825 \r
8d3a5c82 1826 //\r
c6492839 1827 // Make sure we are trying to create a new variable.\r
8a2d4996 1828 // Setting a data variable with zero DataSize or no access attributes means to delete it. \r
8d3a5c82 1829 //\r
c6492839 1830 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {\r
1831 Status = EFI_NOT_FOUND;\r
1832 goto Done;\r
1833 }\r
1834 \r
8d3a5c82 1835 //\r
8a2d4996 1836 // Only variable have NV|RT attribute can be created in Runtime.\r
c6492839 1837 //\r
8a2d4996 1838 if (AtRuntime () &&\r
45f6c85b 1839 (((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) || ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0))) {\r
c6492839 1840 Status = EFI_INVALID_PARAMETER;\r
1841 goto Done;\r
1842 } \r
c6492839 1843 }\r
1844\r
1845 //\r
1846 // Function part - create a new variable and copy the data.\r
1847 // Both update a variable and create a variable will come here.\r
8a2d4996 1848\r
c6492839 1849 //\r
1850 // Tricky part: Use scratch data area at the end of volatile variable store\r
1851 // as a temporary storage.\r
1852 //\r
052ad7e1 1853 NextVariable = GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase));\r
188e4e84 1854 ScratchSize = MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize));\r
c6492839 1855\r
2fcdca1d 1856 SetMem (NextVariable, ScratchSize, 0xff);\r
c6492839 1857\r
1858 NextVariable->StartId = VARIABLE_DATA;\r
1859 NextVariable->Attributes = Attributes;\r
1860 //\r
1861 // NextVariable->State = VAR_ADDED;\r
1862 //\r
8a2d4996 1863 NextVariable->Reserved = 0;\r
1864 VarNameOffset = sizeof (VARIABLE_HEADER);\r
1865 VarNameSize = StrSize (VariableName);\r
c6492839 1866 CopyMem (\r
1867 (UINT8 *) ((UINTN) NextVariable + VarNameOffset),\r
1868 VariableName,\r
1869 VarNameSize\r
1870 );\r
1871 VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);\r
1872 CopyMem (\r
1873 (UINT8 *) ((UINTN) NextVariable + VarDataOffset),\r
1874 Data,\r
1875 DataSize\r
1876 );\r
1877 CopyMem (&NextVariable->VendorGuid, VendorGuid, sizeof (EFI_GUID));\r
1878 //\r
1879 // There will be pad bytes after Data, the NextVariable->NameSize and\r
1880 // NextVariable->DataSize should not include pad size so that variable\r
8a2d4996 1881 // service can get actual size in GetVariable.\r
c6492839 1882 //\r
1883 NextVariable->NameSize = (UINT32)VarNameSize;\r
1884 NextVariable->DataSize = (UINT32)DataSize;\r
1885\r
1886 //\r
1887 // The actual size of the variable that stores in storage should\r
1888 // include pad size.\r
1889 //\r
1890 VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);\r
45f6c85b 1891 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
8d3a5c82 1892 //\r
8a2d4996 1893 // Create a nonvolatile variable.\r
8d3a5c82 1894 //\r
fd51bf70 1895 Volatile = FALSE;\r
2fcdca1d 1896 NonVolatileVarableStoreSize = ((VARIABLE_STORE_HEADER *)(UINTN)(mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase))->Size;\r
1897 if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) \r
188e4e84 1898 && ((VarSize + mVariableModuleGlobal->HwErrVariableTotalSize) > PcdGet32 (PcdHwErrStorageSize)))\r
2fcdca1d 1899 || (((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0) \r
188e4e84 1900 && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > NonVolatileVarableStoreSize - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize)))) {\r
8a2d4996 1901 if (AtRuntime ()) {\r
c6492839 1902 Status = EFI_OUT_OF_RESOURCES;\r
1903 goto Done;\r
1904 }\r
1905 //\r
7baf3c69 1906 // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.\r
c6492839 1907 //\r
72399dae 1908 Status = Reclaim (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase, \r
7baf3c69
SZ
1909 &mVariableModuleGlobal->NonVolatileLastVariableOffset, FALSE, Variable, NextVariable, HEADER_ALIGN (VarSize));\r
1910 if (!EFI_ERROR (Status)) {\r
1911 //\r
1912 // The new variable has been integrated successfully during reclaiming.\r
1913 //\r
1914 if (Variable->CurrPtr != NULL) {\r
1915 CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN) CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr));\r
1916 CacheVariable->InDeletedTransitionPtr = NULL;\r
1917 }\r
1918 UpdateVariableInfo (VariableName, VendorGuid, FALSE, FALSE, TRUE, FALSE, FALSE);\r
1919 FlushHobVariableToFlash (VariableName, VendorGuid);\r
23b06935 1920 }\r
7baf3c69 1921 goto Done;\r
8d3a5c82 1922 }\r
1923 //\r
8a2d4996 1924 // Four steps\r
c6492839 1925 // 1. Write variable header\r
130e2569 1926 // 2. Set variable state to header valid \r
1927 // 3. Write variable data\r
1928 // 4. Set variable state to valid\r
8d3a5c82 1929 //\r
8d3a5c82 1930 //\r
c6492839 1931 // Step 1:\r
8d3a5c82 1932 //\r
8a2d4996 1933 CacheOffset = mVariableModuleGlobal->NonVolatileLastVariableOffset;\r
c6492839 1934 Status = UpdateVariableStore (\r
052ad7e1 1935 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 1936 FALSE,\r
1937 TRUE,\r
8a9e0b72 1938 Fvb,\r
72399dae 1939 mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
c6492839 1940 sizeof (VARIABLE_HEADER),\r
1941 (UINT8 *) NextVariable\r
1942 );\r
1943\r
1944 if (EFI_ERROR (Status)) {\r
1945 goto Done;\r
1946 }\r
130e2569 1947\r
8d3a5c82 1948 //\r
c6492839 1949 // Step 2:\r
8d3a5c82 1950 //\r
130e2569 1951 NextVariable->State = VAR_HEADER_VALID_ONLY;\r
1952 Status = UpdateVariableStore (\r
1953 &mVariableModuleGlobal->VariableGlobal,\r
1954 FALSE,\r
1955 TRUE,\r
8a9e0b72 1956 Fvb,\r
8a2d4996 1957 mVariableModuleGlobal->NonVolatileLastVariableOffset + OFFSET_OF (VARIABLE_HEADER, State),\r
1958 sizeof (UINT8),\r
1959 &NextVariable->State\r
130e2569 1960 );\r
1961\r
1962 if (EFI_ERROR (Status)) {\r
1963 goto Done;\r
1964 }\r
1965 //\r
1966 // Step 3:\r
1967 //\r
c6492839 1968 Status = UpdateVariableStore (\r
052ad7e1 1969 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 1970 FALSE,\r
1971 TRUE,\r
8a9e0b72 1972 Fvb,\r
72399dae 1973 mVariableModuleGlobal->NonVolatileLastVariableOffset + sizeof (VARIABLE_HEADER),\r
c6492839 1974 (UINT32) VarSize - sizeof (VARIABLE_HEADER),\r
1975 (UINT8 *) NextVariable + sizeof (VARIABLE_HEADER)\r
1976 );\r
1977\r
1978 if (EFI_ERROR (Status)) {\r
1979 goto Done;\r
1980 }\r
8d3a5c82 1981 //\r
130e2569 1982 // Step 4:\r
8d3a5c82 1983 //\r
c6492839 1984 NextVariable->State = VAR_ADDED;\r
1985 Status = UpdateVariableStore (\r
052ad7e1 1986 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 1987 FALSE,\r
1988 TRUE,\r
8a9e0b72 1989 Fvb,\r
8a2d4996 1990 mVariableModuleGlobal->NonVolatileLastVariableOffset + OFFSET_OF (VARIABLE_HEADER, State),\r
1991 sizeof (UINT8),\r
1992 &NextVariable->State\r
c6492839 1993 );\r
1994\r
1995 if (EFI_ERROR (Status)) {\r
1996 goto Done;\r
1997 }\r
8d3a5c82 1998\r
72399dae 1999 mVariableModuleGlobal->NonVolatileLastVariableOffset += HEADER_ALIGN (VarSize);\r
8d3a5c82 2000\r
2fcdca1d 2001 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {\r
2002 mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VarSize);\r
2003 } else {\r
2004 mVariableModuleGlobal->CommonVariableTotalSize += HEADER_ALIGN (VarSize);\r
2005 }\r
8a2d4996 2006 //\r
2007 // update the memory copy of Flash region.\r
2008 //\r
2009 CopyMem ((UINT8 *)mNvVariableCache + CacheOffset, (UINT8 *)NextVariable, VarSize);\r
c6492839 2010 } else {\r
2011 //\r
8a2d4996 2012 // Create a volatile variable.\r
c6492839 2013 // \r
fd51bf70 2014 Volatile = TRUE;\r
c6492839 2015\r
72399dae 2016 if ((UINT32) (VarSize + mVariableModuleGlobal->VolatileLastVariableOffset) >\r
052ad7e1 2017 ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size) {\r
8d3a5c82 2018 //\r
7baf3c69 2019 // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.\r
8d3a5c82 2020 //\r
72399dae 2021 Status = Reclaim (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase, \r
7baf3c69
SZ
2022 &mVariableModuleGlobal->VolatileLastVariableOffset, TRUE, Variable, NextVariable, HEADER_ALIGN (VarSize));\r
2023 if (!EFI_ERROR (Status)) {\r
2024 //\r
2025 // The new variable has been integrated successfully during reclaiming.\r
2026 //\r
2027 if (Variable->CurrPtr != NULL) {\r
2028 CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN) CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr));\r
2029 CacheVariable->InDeletedTransitionPtr = NULL;\r
2030 }\r
2031 UpdateVariableInfo (VariableName, VendorGuid, TRUE, FALSE, TRUE, FALSE, FALSE);\r
23b06935 2032 }\r
7baf3c69 2033 goto Done;\r
8d3a5c82 2034 }\r
8d3a5c82 2035\r
c6492839 2036 NextVariable->State = VAR_ADDED;\r
2037 Status = UpdateVariableStore (\r
052ad7e1 2038 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 2039 TRUE,\r
2040 TRUE,\r
8a9e0b72 2041 Fvb,\r
72399dae 2042 mVariableModuleGlobal->VolatileLastVariableOffset,\r
c6492839 2043 (UINT32) VarSize,\r
2044 (UINT8 *) NextVariable\r
2045 );\r
2046\r
2047 if (EFI_ERROR (Status)) {\r
2048 goto Done;\r
8d3a5c82 2049 }\r
c6492839 2050\r
72399dae 2051 mVariableModuleGlobal->VolatileLastVariableOffset += HEADER_ALIGN (VarSize);\r
c6492839 2052 }\r
72399dae 2053\r
c6492839 2054 //\r
8a2d4996 2055 // Mark the old variable as deleted.\r
c6492839 2056 //\r
23b06935
SZ
2057 if (!EFI_ERROR (Status) && Variable->CurrPtr != NULL) {\r
2058 if (Variable->InDeletedTransitionPtr != NULL) {\r
2059 //\r
2060 // Both ADDED and IN_DELETED_TRANSITION old variable are present,\r
2061 // set IN_DELETED_TRANSITION one to DELETED state first.\r
2062 //\r
2063 State = Variable->InDeletedTransitionPtr->State;\r
2064 State &= VAR_DELETED;\r
2065 Status = UpdateVariableStore (\r
2066 &mVariableModuleGlobal->VariableGlobal,\r
2067 Variable->Volatile,\r
2068 FALSE,\r
2069 Fvb,\r
2070 (UINTN) &Variable->InDeletedTransitionPtr->State,\r
2071 sizeof (UINT8),\r
2072 &State\r
2073 );\r
2074 if (!EFI_ERROR (Status)) {\r
2075 if (!Variable->Volatile) {\r
0cc565de 2076 ASSERT (CacheVariable->InDeletedTransitionPtr != NULL);\r
23b06935
SZ
2077 CacheVariable->InDeletedTransitionPtr->State = State;\r
2078 }\r
2079 } else {\r
2080 goto Done;\r
2081 }\r
2082 }\r
2083\r
72399dae 2084 State = Variable->CurrPtr->State;\r
c6492839 2085 State &= VAR_DELETED;\r
2086\r
2087 Status = UpdateVariableStore (\r
72399dae 2088 &mVariableModuleGlobal->VariableGlobal,\r
2089 Variable->Volatile,\r
2090 FALSE,\r
2091 Fvb,\r
2092 (UINTN) &Variable->CurrPtr->State,\r
2093 sizeof (UINT8),\r
2094 &State\r
2095 );\r
8a2d4996 2096 if (!EFI_ERROR (Status) && !Variable->Volatile) { \r
2097 CacheVariable->CurrPtr->State = State;\r
2098 }\r
72399dae 2099 }\r
2100\r
2101 if (!EFI_ERROR (Status)) {\r
2102 UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);\r
335e2681
SZ
2103 if (!Volatile) {\r
2104 FlushHobVariableToFlash (VariableName, VendorGuid);\r
2105 }\r
72399dae 2106 }\r
2107\r
2108Done:\r
2109 return Status;\r
2110}\r
2111\r
a5f15e30
SZ
2112/**\r
2113 Check if a Unicode character is a hexadecimal character.\r
2114\r
2115 This function checks if a Unicode character is a \r
2116 hexadecimal character. The valid hexadecimal character is \r
2117 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.\r
2118\r
2119\r
2120 @param Char The character to check against.\r
2121\r
2122 @retval TRUE If the Char is a hexadecmial character.\r
2123 @retval FALSE If the Char is not a hexadecmial character.\r
2124\r
2125**/\r
2126BOOLEAN\r
2127EFIAPI\r
2128IsHexaDecimalDigitCharacter (\r
2129 IN CHAR16 Char\r
2130 )\r
2131{\r
2132 return (BOOLEAN) ((Char >= L'0' && Char <= L'9') || (Char >= L'A' && Char <= L'F') || (Char >= L'a' && Char <= L'f'));\r
2133}\r
2134\r
2135/**\r
2136\r
2137 This code checks if variable is hardware error record variable or not.\r
2138\r
2139 According to UEFI spec, hardware error record variable should use the EFI_HARDWARE_ERROR_VARIABLE VendorGuid\r
2140 and have the L"HwErrRec####" name convention, #### is a printed hex value and no 0x or h is included in the hex value.\r
2141\r
2142 @param VariableName Pointer to variable name.\r
2143 @param VendorGuid Variable Vendor Guid.\r
2144\r
2145 @retval TRUE Variable is hardware error record variable.\r
2146 @retval FALSE Variable is not hardware error record variable.\r
2147\r
2148**/\r
2149BOOLEAN\r
2150EFIAPI\r
2151IsHwErrRecVariable (\r
2152 IN CHAR16 *VariableName,\r
2153 IN EFI_GUID *VendorGuid\r
2154 )\r
2155{\r
2156 if (!CompareGuid (VendorGuid, &gEfiHardwareErrorVariableGuid) ||\r
2157 (StrLen (VariableName) != StrLen (L"HwErrRec####")) ||\r
2158 (StrnCmp(VariableName, L"HwErrRec", StrLen (L"HwErrRec")) != 0) ||\r
2159 !IsHexaDecimalDigitCharacter (VariableName[0x8]) ||\r
2160 !IsHexaDecimalDigitCharacter (VariableName[0x9]) ||\r
2161 !IsHexaDecimalDigitCharacter (VariableName[0xA]) ||\r
2162 !IsHexaDecimalDigitCharacter (VariableName[0xB])) {\r
2163 return FALSE;\r
2164 }\r
2165\r
2166 return TRUE;\r
2167}\r
2168\r
6675a21f
SZ
2169/**\r
2170 This code checks if variable guid is global variable guid first.\r
2171 If yes, further check if variable name is in mGlobalVariableList or mGlobalVariableList2 and attributes matched.\r
2172\r
2173 @param[in] VariableName Pointer to variable name.\r
2174 @param[in] VendorGuid Variable Vendor Guid.\r
2175 @param[in] Attributes Attributes of the variable.\r
2176\r
2177 @retval EFI_SUCCESS Variable is not global variable, or Variable is global variable, variable name is in the lists and attributes matched.\r
2178 @retval EFI_INVALID_PARAMETER Variable is global variable, but variable name is not in the lists or attributes unmatched.\r
2179\r
2180**/\r
2181EFI_STATUS\r
2182EFIAPI\r
2183CheckEfiGlobalVariable (\r
2184 IN CHAR16 *VariableName,\r
2185 IN EFI_GUID *VendorGuid,\r
2186 IN UINT32 Attributes\r
2187 )\r
2188{\r
2189 UINTN Index;\r
2190 UINTN NameLength;\r
2191\r
2192 if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid)){\r
2193 //\r
2194 // Try list 1, exactly match.\r
2195 //\r
2196 for (Index = 0; Index < sizeof (mGlobalVariableList)/sizeof (mGlobalVariableList[0]); Index++) {\r
2197 if ((StrCmp (mGlobalVariableList[Index].Name, VariableName) == 0) &&\r
2198 (Attributes == 0 || Attributes == mGlobalVariableList[Index].Attributes)) {\r
2199 return EFI_SUCCESS;\r
2200 }\r
2201 }\r
2202\r
2203 //\r
2204 // Try list 2.\r
2205 //\r
2206 NameLength = StrLen (VariableName) - 4;\r
2207 for (Index = 0; Index < sizeof (mGlobalVariableList2)/sizeof (mGlobalVariableList2[0]); Index++) {\r
2208 if ((StrLen (VariableName) == StrLen (mGlobalVariableList2[Index].Name)) &&\r
2209 (StrnCmp (mGlobalVariableList2[Index].Name, VariableName, NameLength) == 0) &&\r
2210 IsHexaDecimalDigitCharacter (VariableName[NameLength]) &&\r
2211 IsHexaDecimalDigitCharacter (VariableName[NameLength + 1]) &&\r
2212 IsHexaDecimalDigitCharacter (VariableName[NameLength + 2]) &&\r
2213 IsHexaDecimalDigitCharacter (VariableName[NameLength + 3]) &&\r
2214 (Attributes == 0 || Attributes == mGlobalVariableList2[Index].Attributes)) {\r
2215 return EFI_SUCCESS;\r
2216 }\r
2217 }\r
2218\r
2219 DEBUG ((EFI_D_INFO, "[Variable]: set global variable with invalid variable name or attributes - %g:%s:%x\n", VendorGuid, VariableName, Attributes));\r
2220 return EFI_INVALID_PARAMETER;\r
2221 }\r
2222\r
2223 return EFI_SUCCESS;\r
2224}\r
2225\r
ff843847
RN
2226/**\r
2227 Mark a variable that will become read-only after leaving the DXE phase of execution.\r
2228\r
2229 @param[in] This The VARIABLE_LOCK_PROTOCOL instance.\r
2230 @param[in] VariableName A pointer to the variable name that will be made read-only subsequently.\r
2231 @param[in] VendorGuid A pointer to the vendor GUID that will be made read-only subsequently.\r
2232\r
2233 @retval EFI_SUCCESS The variable specified by the VariableName and the VendorGuid was marked\r
2234 as pending to be read-only.\r
2235 @retval EFI_INVALID_PARAMETER VariableName or VendorGuid is NULL.\r
2236 Or VariableName is an empty string.\r
2237 @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has\r
2238 already been signaled.\r
2239 @retval EFI_OUT_OF_RESOURCES There is not enough resource to hold the lock request.\r
2240**/\r
2241EFI_STATUS\r
2242EFIAPI\r
2243VariableLockRequestToLock (\r
2244 IN CONST EDKII_VARIABLE_LOCK_PROTOCOL *This,\r
2245 IN CHAR16 *VariableName,\r
2246 IN EFI_GUID *VendorGuid\r
2247 )\r
2248{\r
2249 VARIABLE_ENTRY *Entry;\r
2250\r
2251 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r
2252 return EFI_INVALID_PARAMETER;\r
2253 }\r
2254\r
2255 if (mEndOfDxe) {\r
2256 return EFI_ACCESS_DENIED;\r
2257 }\r
2258\r
2259 Entry = AllocateRuntimePool (sizeof (*Entry) + StrSize (VariableName));\r
2260 if (Entry == NULL) {\r
2261 return EFI_OUT_OF_RESOURCES;\r
2262 }\r
2263\r
2264 DEBUG ((EFI_D_INFO, "[Variable] Lock: %g:%s\n", VendorGuid, VariableName));\r
2265\r
2266 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2267\r
2268 Entry->Name = (CHAR16 *) (Entry + 1);\r
2269 StrCpy (Entry->Name, VariableName);\r
2270 CopyGuid (&Entry->Guid, VendorGuid);\r
2271 InsertTailList (&mLockedVariableList, &Entry->Link);\r
2272\r
2273 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2274\r
2275 return EFI_SUCCESS;\r
2276}\r
2277\r
72399dae 2278/**\r
2279\r
2280 This code finds variable in storage blocks (Volatile or Non-Volatile).\r
2281\r
2282 @param VariableName Name of Variable to be found.\r
2283 @param VendorGuid Variable vendor GUID.\r
2284 @param Attributes Attribute value of the variable found.\r
2285 @param DataSize Size of Data found. If size is less than the\r
2286 data, this value contains the required size.\r
2287 @param Data Data pointer.\r
2288 \r
8a2d4996 2289 @return EFI_INVALID_PARAMETER Invalid parameter.\r
2290 @return EFI_SUCCESS Find the specified variable.\r
2291 @return EFI_NOT_FOUND Not found.\r
2292 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.\r
72399dae 2293\r
2294**/\r
2295EFI_STATUS\r
2296EFIAPI\r
8a2d4996 2297VariableServiceGetVariable (\r
72399dae 2298 IN CHAR16 *VariableName,\r
2299 IN EFI_GUID *VendorGuid,\r
2300 OUT UINT32 *Attributes OPTIONAL,\r
2301 IN OUT UINTN *DataSize,\r
2302 OUT VOID *Data\r
2303 )\r
2304{\r
2305 EFI_STATUS Status;\r
2306 VARIABLE_POINTER_TRACK Variable;\r
2307 UINTN VarDataSize;\r
2308\r
2309 if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {\r
2310 return EFI_INVALID_PARAMETER;\r
2311 }\r
2312\r
2313 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
72399dae 2314 \r
9622df63 2315 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
72399dae 2316 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
2317 goto Done;\r
2318 }\r
2319\r
2320 //\r
2321 // Get data size\r
2322 //\r
2323 VarDataSize = DataSizeOfVariable (Variable.CurrPtr);\r
2324 ASSERT (VarDataSize != 0);\r
2325\r
2326 if (*DataSize >= VarDataSize) {\r
2327 if (Data == NULL) {\r
2328 Status = EFI_INVALID_PARAMETER;\r
2329 goto Done;\r
33a5a666 2330 }\r
72399dae 2331\r
2332 CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);\r
2333 if (Attributes != NULL) {\r
2334 *Attributes = Variable.CurrPtr->Attributes;\r
2335 }\r
2336\r
2337 *DataSize = VarDataSize;\r
2338 UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, TRUE, FALSE, FALSE, FALSE);\r
72399dae 2339 \r
2340 Status = EFI_SUCCESS;\r
2341 goto Done;\r
2342 } else {\r
2343 *DataSize = VarDataSize;\r
2344 Status = EFI_BUFFER_TOO_SMALL;\r
2345 goto Done;\r
8d3a5c82 2346 }\r
2347\r
72399dae 2348Done:\r
2349 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2350 return Status;\r
2351}\r
2352\r
2353\r
2354\r
2355/**\r
2356\r
2357 This code Finds the Next available variable.\r
2358\r
8a2d4996 2359 @param VariableNameSize Size of the variable name.\r
2360 @param VariableName Pointer to variable name.\r
2361 @param VendorGuid Variable Vendor Guid.\r
72399dae 2362\r
8a2d4996 2363 @return EFI_INVALID_PARAMETER Invalid parameter.\r
2364 @return EFI_SUCCESS Find the specified variable.\r
2365 @return EFI_NOT_FOUND Not found.\r
2366 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.\r
72399dae 2367\r
2368**/\r
2369EFI_STATUS\r
2370EFIAPI\r
8a2d4996 2371VariableServiceGetNextVariableName (\r
72399dae 2372 IN OUT UINTN *VariableNameSize,\r
2373 IN OUT CHAR16 *VariableName,\r
2374 IN OUT EFI_GUID *VendorGuid\r
2375 )\r
2376{\r
0f7aff72 2377 VARIABLE_STORE_TYPE Type;\r
72399dae 2378 VARIABLE_POINTER_TRACK Variable;\r
0f7aff72 2379 VARIABLE_POINTER_TRACK VariableInHob;\r
23b06935 2380 VARIABLE_POINTER_TRACK VariablePtrTrack;\r
72399dae 2381 UINTN VarNameSize;\r
2382 EFI_STATUS Status;\r
0f7aff72 2383 VARIABLE_STORE_HEADER *VariableStoreHeader[VariableStoreTypeMax];\r
72399dae 2384\r
2385 if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {\r
2386 return EFI_INVALID_PARAMETER;\r
2387 }\r
2388\r
2389 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2390\r
9622df63 2391 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
72399dae 2392 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
2393 goto Done;\r
2394 }\r
2395\r
2396 if (VariableName[0] != 0) {\r
2397 //\r
8a2d4996 2398 // If variable name is not NULL, get next variable.\r
72399dae 2399 //\r
2400 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
2401 }\r
2402\r
0f7aff72
RN
2403 //\r
2404 // 0: Volatile, 1: HOB, 2: Non-Volatile.\r
2405 // The index and attributes mapping must be kept in this order as FindVariable\r
2406 // makes use of this mapping to implement search algorithm.\r
2407 //\r
2408 VariableStoreHeader[VariableStoreTypeVolatile] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;\r
2409 VariableStoreHeader[VariableStoreTypeHob] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;\r
2410 VariableStoreHeader[VariableStoreTypeNv] = mNvVariableCache;\r
2411\r
72399dae 2412 while (TRUE) {\r
2413 //\r
0f7aff72 2414 // Switch from Volatile to HOB, to Non-Volatile.\r
72399dae 2415 //\r
0f7aff72
RN
2416 while ((Variable.CurrPtr >= Variable.EndPtr) ||\r
2417 (Variable.CurrPtr == NULL) ||\r
2418 !IsValidVariableHeader (Variable.CurrPtr)\r
2419 ) {\r
2420 //\r
2421 // Find current storage index\r
2422 //\r
2423 for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {\r
2424 if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr == GetStartPointer (VariableStoreHeader[Type]))) {\r
2425 break;\r
2426 }\r
2427 }\r
2428 ASSERT (Type < VariableStoreTypeMax);\r
2429 //\r
2430 // Switch to next storage\r
2431 //\r
2432 for (Type++; Type < VariableStoreTypeMax; Type++) {\r
2433 if (VariableStoreHeader[Type] != NULL) {\r
2434 break;\r
2435 }\r
2436 }\r
2437 //\r
2438 // Capture the case that \r
2439 // 1. current storage is the last one, or\r
2440 // 2. no further storage\r
2441 //\r
2442 if (Type == VariableStoreTypeMax) {\r
72399dae 2443 Status = EFI_NOT_FOUND;\r
2444 goto Done;\r
2445 }\r
0f7aff72
RN
2446 Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);\r
2447 Variable.EndPtr = GetEndPointer (VariableStoreHeader[Type]);\r
2448 Variable.CurrPtr = Variable.StartPtr;\r
72399dae 2449 }\r
0f7aff72 2450\r
72399dae 2451 //\r
2452 // Variable is found\r
2453 //\r
23b06935
SZ
2454 if (Variable.CurrPtr->State == VAR_ADDED || Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
2455 if (!AtRuntime () || ((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {\r
2456 if (Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
2457 //\r
2458 // If it is a IN_DELETED_TRANSITION variable,\r
2459 // and there is also a same ADDED one at the same time,\r
2460 // don't return it.\r
2461 //\r
2462 VariablePtrTrack.StartPtr = Variable.StartPtr;\r
2463 VariablePtrTrack.EndPtr = Variable.EndPtr;\r
2464 Status = FindVariableEx (\r
2465 GetVariableNamePtr (Variable.CurrPtr),\r
2466 &Variable.CurrPtr->VendorGuid,\r
2467 FALSE,\r
2468 &VariablePtrTrack\r
2469 );\r
2470 if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State == VAR_ADDED) {\r
2471 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
2472 continue;\r
2473 }\r
2474 }\r
0f7aff72
RN
2475\r
2476 //\r
2477 // Don't return NV variable when HOB overrides it\r
2478 //\r
2479 if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) && (VariableStoreHeader[VariableStoreTypeNv] != NULL) && \r
2480 (Variable.StartPtr == GetStartPointer (VariableStoreHeader[VariableStoreTypeNv]))\r
2481 ) {\r
2482 VariableInHob.StartPtr = GetStartPointer (VariableStoreHeader[VariableStoreTypeHob]);\r
2483 VariableInHob.EndPtr = GetEndPointer (VariableStoreHeader[VariableStoreTypeHob]);\r
2484 Status = FindVariableEx (\r
2485 GetVariableNamePtr (Variable.CurrPtr),\r
2486 &Variable.CurrPtr->VendorGuid,\r
9622df63 2487 FALSE,\r
0f7aff72
RN
2488 &VariableInHob\r
2489 );\r
2490 if (!EFI_ERROR (Status)) {\r
2491 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
2492 continue;\r
2493 }\r
2494 }\r
2495\r
72399dae 2496 VarNameSize = NameSizeOfVariable (Variable.CurrPtr);\r
2497 ASSERT (VarNameSize != 0);\r
2498\r
2499 if (VarNameSize <= *VariableNameSize) {\r
0f7aff72
RN
2500 CopyMem (VariableName, GetVariableNamePtr (Variable.CurrPtr), VarNameSize);\r
2501 CopyMem (VendorGuid, &Variable.CurrPtr->VendorGuid, sizeof (EFI_GUID));\r
72399dae 2502 Status = EFI_SUCCESS;\r
2503 } else {\r
2504 Status = EFI_BUFFER_TOO_SMALL;\r
2505 }\r
2506\r
2507 *VariableNameSize = VarNameSize;\r
2508 goto Done;\r
2509 }\r
2510 }\r
2511\r
2512 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
2513 }\r
33a5a666 2514\r
8d3a5c82 2515Done:\r
72399dae 2516 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2517 return Status;\r
2518}\r
2519\r
2520/**\r
2521\r
2522 This code sets variable in storage blocks (Volatile or Non-Volatile).\r
2523\r
8a2d4996 2524 @param VariableName Name of Variable to be found.\r
2525 @param VendorGuid Variable vendor GUID.\r
72399dae 2526 @param Attributes Attribute value of the variable found\r
2527 @param DataSize Size of Data found. If size is less than the\r
2528 data, this value contains the required size.\r
8a2d4996 2529 @param Data Data pointer.\r
72399dae 2530\r
8a2d4996 2531 @return EFI_INVALID_PARAMETER Invalid parameter.\r
2532 @return EFI_SUCCESS Set successfully.\r
2533 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.\r
2534 @return EFI_NOT_FOUND Not found.\r
2535 @return EFI_WRITE_PROTECTED Variable is read-only.\r
72399dae 2536\r
2537**/\r
2538EFI_STATUS\r
2539EFIAPI\r
8a2d4996 2540VariableServiceSetVariable (\r
72399dae 2541 IN CHAR16 *VariableName,\r
2542 IN EFI_GUID *VendorGuid,\r
2543 IN UINT32 Attributes,\r
2544 IN UINTN DataSize,\r
2545 IN VOID *Data\r
2546 )\r
2547{\r
2548 VARIABLE_POINTER_TRACK Variable;\r
2549 EFI_STATUS Status;\r
2550 VARIABLE_HEADER *NextVariable;\r
2551 EFI_PHYSICAL_ADDRESS Point;\r
ff843847
RN
2552 LIST_ENTRY *Link;\r
2553 VARIABLE_ENTRY *Entry;\r
72399dae 2554\r
2555 //\r
8a2d4996 2556 // Check input parameters.\r
72399dae 2557 //\r
2558 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r
2559 return EFI_INVALID_PARAMETER;\r
8a2d4996 2560 } \r
48a0e6bf 2561\r
2562 if (DataSize != 0 && Data == NULL) {\r
2563 return EFI_INVALID_PARAMETER;\r
2564 }\r
2565\r
8e38f18e 2566 //\r
6e67fec0 2567 // Not support authenticated or append variable write yet.\r
8e38f18e 2568 //\r
6e67fec0 2569 if ((Attributes & (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS | EFI_VARIABLE_APPEND_WRITE)) != 0) {\r
8e38f18e 2570 return EFI_INVALID_PARAMETER;\r
2571 }\r
2572\r
72399dae 2573 //\r
8a2d4996 2574 // Make sure if runtime bit is set, boot service bit is set also.\r
72399dae 2575 //\r
2576 if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
2577 return EFI_INVALID_PARAMETER;\r
2578 }\r
2579\r
56251c66 2580 if ((UINTN)(~0) - DataSize < StrSize(VariableName)){\r
2581 //\r
2582 // Prevent whole variable size overflow \r
2583 // \r
2584 return EFI_INVALID_PARAMETER;\r
2585 }\r
2586\r
72399dae 2587 //\r
2588 // The size of the VariableName, including the Unicode Null in bytes plus\r
188e4e84 2589 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)\r
2590 // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.\r
72399dae 2591 //\r
2592 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
56251c66 2593 if ( StrSize (VariableName) + DataSize > PcdGet32 (PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER)) {\r
72399dae 2594 return EFI_INVALID_PARAMETER;\r
2595 }\r
a5f15e30 2596 if (!IsHwErrRecVariable(VariableName, VendorGuid)) {\r
72399dae 2597 return EFI_INVALID_PARAMETER;\r
2598 }\r
2599 } else {\r
2600 //\r
2601 // The size of the VariableName, including the Unicode Null in bytes plus\r
188e4e84 2602 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxVariableSize) bytes.\r
72399dae 2603 //\r
56251c66 2604 if (StrSize (VariableName) + DataSize > PcdGet32 (PcdMaxVariableSize) - sizeof (VARIABLE_HEADER)) {\r
72399dae 2605 return EFI_INVALID_PARAMETER;\r
2606 } \r
021a1af9
SZ
2607 }\r
2608\r
6675a21f
SZ
2609 Status = CheckEfiGlobalVariable (VariableName, VendorGuid, Attributes);\r
2610 if (EFI_ERROR (Status)) {\r
2611 return Status;\r
2612 }\r
2613\r
72399dae 2614 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2615\r
2616 //\r
8a2d4996 2617 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.\r
72399dae 2618 //\r
2619 if (1 < InterlockedIncrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState)) {\r
8a2d4996 2620 Point = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;\r
72399dae 2621 //\r
8a2d4996 2622 // Parse non-volatile variable data and get last variable offset.\r
72399dae 2623 //\r
2624 NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point);\r
2625 while ((NextVariable < GetEndPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point)) \r
2626 && IsValidVariableHeader (NextVariable)) {\r
2627 NextVariable = GetNextVariablePtr (NextVariable);\r
2628 }\r
2629 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) Point;\r
2630 }\r
2631\r
ff843847
RN
2632 if (mEndOfDxe && mEnableLocking) {\r
2633 //\r
2634 // Treat the variables listed in the forbidden variable list as read-only after leaving DXE phase.\r
2635 //\r
2636 for ( Link = GetFirstNode (&mLockedVariableList)\r
2637 ; !IsNull (&mLockedVariableList, Link)\r
2638 ; Link = GetNextNode (&mLockedVariableList, Link)\r
2639 ) {\r
2640 Entry = BASE_CR (Link, VARIABLE_ENTRY, Link);\r
2641 if (CompareGuid (&Entry->Guid, VendorGuid) && (StrCmp (Entry->Name, VariableName) == 0)) {\r
2642 Status = EFI_WRITE_PROTECTED;\r
2643 DEBUG ((EFI_D_INFO, "[Variable]: Changing readonly variable after leaving DXE phase - %g:%s\n", VendorGuid, VariableName));\r
2644 goto Done;\r
2645 }\r
2646 }\r
2647 }\r
2648\r
72399dae 2649 //\r
8a2d4996 2650 // Check whether the input variable is already existed.\r
72399dae 2651 //\r
9622df63
SZ
2652 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, TRUE);\r
2653 if (!EFI_ERROR (Status)) {\r
2654 if (((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) && AtRuntime ()) {\r
ff843847
RN
2655 Status = EFI_WRITE_PROTECTED;\r
2656 goto Done;\r
9622df63 2657 }\r
6e67fec0
SZ
2658 if (Attributes != 0 && Attributes != Variable.CurrPtr->Attributes) {\r
2659 //\r
2660 // If a preexisting variable is rewritten with different attributes, SetVariable() shall not\r
2661 // modify the variable and shall return EFI_INVALID_PARAMETER. Two exceptions to this rule:\r
2662 // 1. No access attributes specified\r
2663 // 2. The only attribute differing is EFI_VARIABLE_APPEND_WRITE\r
2664 //\r
2665 Status = EFI_INVALID_PARAMETER;\r
2666 goto Done;\r
2667 }\r
9622df63 2668 }\r
72399dae 2669\r
b2bd493e 2670 if (!FeaturePcdGet (PcdUefiVariableDefaultLangDeprecate)) {\r
9bc5dabb 2671 //\r
b2bd493e 2672 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.\r
9bc5dabb 2673 //\r
b2bd493e
SZ
2674 Status = AutoUpdateLangVariable (VariableName, Data, DataSize);\r
2675 if (EFI_ERROR (Status)) {\r
2676 //\r
2677 // The auto update operation failed, directly return to avoid inconsistency between PlatformLang and Lang.\r
2678 //\r
2679 goto Done;\r
2680 }\r
9bc5dabb 2681 }\r
72399dae 2682\r
2683 Status = UpdateVariable (VariableName, VendorGuid, Data, DataSize, Attributes, &Variable);\r
2684\r
ff843847 2685Done:\r
fdb7765f 2686 InterlockedDecrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState);\r
052ad7e1 2687 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
fdb7765f 2688\r
8d3a5c82 2689 return Status;\r
2690}\r
2691\r
7c80e839 2692/**\r
8d3a5c82 2693\r
2694 This code returns information about the EFI variables.\r
2695\r
7c80e839 2696 @param Attributes Attributes bitmask to specify the type of variables\r
2697 on which to return information.\r
2698 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available\r
2699 for the EFI variables associated with the attributes specified.\r
2700 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available\r
2701 for EFI variables associated with the attributes specified.\r
2702 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables\r
2703 associated with the attributes specified.\r
8d3a5c82 2704\r
7c80e839 2705 @return EFI_SUCCESS Query successfully.\r
8d3a5c82 2706\r
7c80e839 2707**/\r
052ad7e1
A
2708EFI_STATUS\r
2709EFIAPI\r
b2bd493e 2710VariableServiceQueryVariableInfoInternal (\r
052ad7e1
A
2711 IN UINT32 Attributes,\r
2712 OUT UINT64 *MaximumVariableStorageSize,\r
2713 OUT UINT64 *RemainingVariableStorageSize,\r
2714 OUT UINT64 *MaximumVariableSize\r
2715 )\r
8d3a5c82 2716{\r
2717 VARIABLE_HEADER *Variable;\r
2718 VARIABLE_HEADER *NextVariable;\r
2719 UINT64 VariableSize;\r
2720 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
2fcdca1d 2721 UINT64 CommonVariableTotalSize;\r
2722 UINT64 HwErrVariableTotalSize;\r
b2bd493e
SZ
2723 EFI_STATUS Status;\r
2724 VARIABLE_POINTER_TRACK VariablePtrTrack;\r
2fcdca1d 2725\r
2726 CommonVariableTotalSize = 0;\r
2727 HwErrVariableTotalSize = 0;\r
8d3a5c82 2728\r
8d3a5c82 2729 if((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r
2730 //\r
2731 // Query is Volatile related.\r
2732 //\r
052ad7e1 2733 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r
8d3a5c82 2734 } else {\r
2735 //\r
2736 // Query is Non-Volatile related.\r
2737 //\r
8a2d4996 2738 VariableStoreHeader = mNvVariableCache;\r
8d3a5c82 2739 }\r
2740\r
2741 //\r
2742 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize\r
2743 // with the storage size (excluding the storage header size).\r
2744 //\r
2745 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);\r
c6492839 2746\r
2747 //\r
2748 // Harware error record variable needs larger size.\r
2749 //\r
2fcdca1d 2750 if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
188e4e84 2751 *MaximumVariableStorageSize = PcdGet32 (PcdHwErrStorageSize);\r
2752 *MaximumVariableSize = PcdGet32 (PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER);\r
2fcdca1d 2753 } else {\r
2754 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
188e4e84 2755 ASSERT (PcdGet32 (PcdHwErrStorageSize) < VariableStoreHeader->Size);\r
2756 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize);\r
2fcdca1d 2757 }\r
2758\r
2759 //\r
188e4e84 2760 // Let *MaximumVariableSize be PcdGet32 (PcdMaxVariableSize) with the exception of the variable header size.\r
2fcdca1d 2761 //\r
188e4e84 2762 *MaximumVariableSize = PcdGet32 (PcdMaxVariableSize) - sizeof (VARIABLE_HEADER);\r
c6492839 2763 }\r
8d3a5c82 2764\r
2765 //\r
2766 // Point to the starting address of the variables.\r
2767 //\r
9cad030b 2768 Variable = GetStartPointer (VariableStoreHeader);\r
8d3a5c82 2769\r
2770 //\r
2771 // Now walk through the related variable store.\r
2772 //\r
6f90dfbc 2773 while ((Variable < GetEndPointer (VariableStoreHeader)) && IsValidVariableHeader (Variable)) {\r
8d3a5c82 2774 NextVariable = GetNextVariablePtr (Variable);\r
2775 VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;\r
2776\r
8a2d4996 2777 if (AtRuntime ()) {\r
8d3a5c82 2778 //\r
8a2d4996 2779 // We don't take the state of the variables in mind\r
8d3a5c82 2780 // when calculating RemainingVariableStorageSize,\r
2781 // since the space occupied by variables not marked with\r
2782 // VAR_ADDED is not allowed to be reclaimed in Runtime.\r
2783 //\r
3b425367 2784 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2fcdca1d 2785 HwErrVariableTotalSize += VariableSize;\r
2786 } else {\r
2787 CommonVariableTotalSize += VariableSize;\r
2788 }\r
8d3a5c82 2789 } else {\r
2790 //\r
8a2d4996 2791 // Only care about Variables with State VAR_ADDED, because\r
8d3a5c82 2792 // the space not marked as VAR_ADDED is reclaimable now.\r
2793 //\r
2794 if (Variable->State == VAR_ADDED) {\r
3b425367 2795 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2fcdca1d 2796 HwErrVariableTotalSize += VariableSize;\r
2797 } else {\r
2798 CommonVariableTotalSize += VariableSize;\r
2799 }\r
b2bd493e
SZ
2800 } else if (Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
2801 //\r
2802 // If it is a IN_DELETED_TRANSITION variable,\r
2803 // and there is not also a same ADDED one at the same time,\r
2804 // this IN_DELETED_TRANSITION variable is valid.\r
2805 //\r
2806 VariablePtrTrack.StartPtr = GetStartPointer (VariableStoreHeader);\r
2807 VariablePtrTrack.EndPtr = GetEndPointer (VariableStoreHeader);\r
2808 Status = FindVariableEx (\r
2809 GetVariableNamePtr (Variable),\r
2810 &Variable->VendorGuid,\r
2811 FALSE,\r
2812 &VariablePtrTrack\r
2813 );\r
2814 if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State != VAR_ADDED) {\r
2815 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2816 HwErrVariableTotalSize += VariableSize;\r
2817 } else {\r
2818 CommonVariableTotalSize += VariableSize;\r
2819 }\r
2820 }\r
8d3a5c82 2821 }\r
2822 }\r
2823\r
2824 //\r
8a2d4996 2825 // Go to the next one.\r
8d3a5c82 2826 //\r
2827 Variable = NextVariable;\r
2828 }\r
2829\r
2fcdca1d 2830 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD){\r
2831 *RemainingVariableStorageSize = *MaximumVariableStorageSize - HwErrVariableTotalSize;\r
2832 }else {\r
2833 *RemainingVariableStorageSize = *MaximumVariableStorageSize - CommonVariableTotalSize;\r
2834 }\r
2835\r
c6492839 2836 if (*RemainingVariableStorageSize < sizeof (VARIABLE_HEADER)) {\r
2837 *MaximumVariableSize = 0;\r
2838 } else if ((*RemainingVariableStorageSize - sizeof (VARIABLE_HEADER)) < *MaximumVariableSize) {\r
2839 *MaximumVariableSize = *RemainingVariableStorageSize - sizeof (VARIABLE_HEADER);\r
2840 }\r
2841\r
8d3a5c82 2842 return EFI_SUCCESS;\r
2843}\r
2844\r
b2bd493e
SZ
2845/**\r
2846\r
2847 This code returns information about the EFI variables.\r
2848\r
2849 @param Attributes Attributes bitmask to specify the type of variables\r
2850 on which to return information.\r
2851 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available\r
2852 for the EFI variables associated with the attributes specified.\r
2853 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available\r
2854 for EFI variables associated with the attributes specified.\r
2855 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables\r
2856 associated with the attributes specified.\r
2857\r
2858 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.\r
2859 @return EFI_SUCCESS Query successfully.\r
2860 @return EFI_UNSUPPORTED The attribute is not supported on this platform.\r
2861\r
2862**/\r
2863EFI_STATUS\r
2864EFIAPI\r
2865VariableServiceQueryVariableInfo (\r
2866 IN UINT32 Attributes,\r
2867 OUT UINT64 *MaximumVariableStorageSize,\r
2868 OUT UINT64 *RemainingVariableStorageSize,\r
2869 OUT UINT64 *MaximumVariableSize\r
2870 )\r
2871{\r
2872 EFI_STATUS Status;\r
2873\r
2874 if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {\r
2875 return EFI_INVALID_PARAMETER;\r
2876 }\r
2877\r
2878 if((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == 0) {\r
2879 //\r
2880 // Make sure the Attributes combination is supported by the platform.\r
2881 //\r
2882 return EFI_UNSUPPORTED;\r
2883 } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
2884 //\r
2885 // Make sure if runtime bit is set, boot service bit is set also.\r
2886 //\r
2887 return EFI_INVALID_PARAMETER;\r
2888 } else if (AtRuntime () && ((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0)) {\r
2889 //\r
2890 // Make sure RT Attribute is set if we are in Runtime phase.\r
2891 //\r
2892 return EFI_INVALID_PARAMETER;\r
2893 } else if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2894 //\r
2895 // Make sure Hw Attribute is set with NV.\r
2896 //\r
2897 return EFI_INVALID_PARAMETER;\r
2898 } else if ((Attributes & (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS | EFI_VARIABLE_APPEND_WRITE)) != 0) {\r
2899 //\r
2900 // Not support authenticated or append variable write yet.\r
2901 //\r
2902 return EFI_UNSUPPORTED;\r
2903 }\r
2904\r
2905 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2906\r
2907 Status = VariableServiceQueryVariableInfoInternal (\r
2908 Attributes,\r
2909 MaximumVariableStorageSize,\r
2910 RemainingVariableStorageSize,\r
2911 MaximumVariableSize\r
2912 );\r
2913\r
2914 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2915 return Status;\r
2916}\r
7c80e839 2917\r
2918/**\r
8a2d4996 2919 This function reclaims variable storage if free size is below the threshold.\r
2920 \r
7c80e839 2921**/\r
7800593d 2922VOID\r
7800593d 2923ReclaimForOS(\r
8a2d4996 2924 VOID\r
7800593d
LG
2925 )\r
2926{\r
9948c0b0 2927 EFI_STATUS Status;\r
2fcdca1d 2928 UINTN CommonVariableSpace;\r
2929 UINTN RemainingCommonVariableSpace;\r
2930 UINTN RemainingHwErrVariableSpace;\r
7800593d 2931\r
7800593d
LG
2932 Status = EFI_SUCCESS; \r
2933\r
2fcdca1d 2934 CommonVariableSpace = ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase)))->Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32(PcdHwErrStorageSize); //Allowable max size of common variable storage space\r
2935\r
2936 RemainingCommonVariableSpace = CommonVariableSpace - mVariableModuleGlobal->CommonVariableTotalSize;\r
2937\r
2938 RemainingHwErrVariableSpace = PcdGet32 (PcdHwErrStorageSize) - mVariableModuleGlobal->HwErrVariableTotalSize;\r
7800593d 2939 //\r
9948c0b0 2940 // Check if the free area is blow a threshold.\r
7800593d 2941 //\r
2fcdca1d 2942 if ((RemainingCommonVariableSpace < PcdGet32 (PcdMaxVariableSize))\r
9948c0b0 2943 || ((PcdGet32 (PcdHwErrStorageSize) != 0) && \r
2944 (RemainingHwErrVariableSpace < PcdGet32 (PcdMaxHardwareErrorVariableSize)))){\r
7800593d 2945 Status = Reclaim (\r
2fcdca1d 2946 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
2947 &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
2948 FALSE,\r
335e2681 2949 NULL,\r
7baf3c69
SZ
2950 NULL,\r
2951 0\r
2fcdca1d 2952 );\r
7800593d
LG
2953 ASSERT_EFI_ERROR (Status);\r
2954 }\r
2955}\r
2956\r
3e02ebb2
SZ
2957/**\r
2958 Init non-volatile variable store.\r
2959\r
2960 @retval EFI_SUCCESS Function successfully executed.\r
2961 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.\r
2962 @retval EFI_VOLUME_CORRUPTED Variable Store or Firmware Volume for Variable Store is corrupted.\r
2963\r
2964**/\r
2965EFI_STATUS\r
2966InitNonVolatileVariableStore (\r
2967 VOID\r
2968 )\r
2969{\r
2970 EFI_FIRMWARE_VOLUME_HEADER *FvHeader;\r
2971 VARIABLE_HEADER *NextVariable;\r
2972 EFI_PHYSICAL_ADDRESS VariableStoreBase;\r
2973 UINT64 VariableStoreLength;\r
2974 UINTN VariableSize;\r
2975 EFI_HOB_GUID_TYPE *GuidHob;\r
2976 EFI_PHYSICAL_ADDRESS NvStorageBase;\r
2977 UINT8 *NvStorageData;\r
2978 UINT32 NvStorageSize;\r
2979 FAULT_TOLERANT_WRITE_LAST_WRITE_DATA *FtwLastWriteData;\r
2980 UINT32 BackUpOffset;\r
2981 UINT32 BackUpSize;\r
2982\r
2983 mVariableModuleGlobal->FvbInstance = NULL;\r
2984\r
2985 //\r
2986 // Note that in EdkII variable driver implementation, Hardware Error Record type variable\r
2987 // is stored with common variable in the same NV region. So the platform integrator should\r
2988 // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of\r
2989 // PcdFlashNvStorageVariableSize.\r
2990 //\r
2991 ASSERT (PcdGet32 (PcdHwErrStorageSize) <= PcdGet32 (PcdFlashNvStorageVariableSize));\r
2992\r
2993 //\r
2994 // Allocate runtime memory used for a memory copy of the FLASH region.\r
2995 // Keep the memory and the FLASH in sync as updates occur.\r
2996 //\r
2997 NvStorageSize = PcdGet32 (PcdFlashNvStorageVariableSize);\r
2998 NvStorageData = AllocateRuntimeZeroPool (NvStorageSize);\r
2999 if (NvStorageData == NULL) {\r
3000 return EFI_OUT_OF_RESOURCES;\r
3001 }\r
3002\r
3003 NvStorageBase = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);\r
3004 if (NvStorageBase == 0) {\r
3005 NvStorageBase = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);\r
3006 }\r
3007 //\r
3008 // Copy NV storage data to the memory buffer.\r
3009 //\r
3010 CopyMem (NvStorageData, (UINT8 *) (UINTN) NvStorageBase, NvStorageSize);\r
3011\r
3012 //\r
3013 // Check the FTW last write data hob.\r
3014 //\r
3015 GuidHob = GetFirstGuidHob (&gEdkiiFaultTolerantWriteGuid);\r
3016 if (GuidHob != NULL) {\r
3017 FtwLastWriteData = (FAULT_TOLERANT_WRITE_LAST_WRITE_DATA *) GET_GUID_HOB_DATA (GuidHob);\r
3018 if (FtwLastWriteData->TargetAddress == NvStorageBase) {\r
3019 DEBUG ((EFI_D_INFO, "Variable: NV storage is backed up in spare block: 0x%x\n", (UINTN) FtwLastWriteData->SpareAddress));\r
3020 //\r
3021 // Copy the backed up NV storage data to the memory buffer from spare block.\r
3022 //\r
3023 CopyMem (NvStorageData, (UINT8 *) (UINTN) (FtwLastWriteData->SpareAddress), NvStorageSize);\r
3024 } else if ((FtwLastWriteData->TargetAddress > NvStorageBase) &&\r
3025 (FtwLastWriteData->TargetAddress < (NvStorageBase + NvStorageSize))) {\r
3026 //\r
3027 // Flash NV storage from the offset is backed up in spare block.\r
3028 //\r
3029 BackUpOffset = (UINT32) (FtwLastWriteData->TargetAddress - NvStorageBase);\r
3030 BackUpSize = NvStorageSize - BackUpOffset;\r
3031 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
3032 //\r
3033 // Copy the partial backed up NV storage data to the memory buffer from spare block.\r
3034 //\r
3035 CopyMem (NvStorageData + BackUpOffset, (UINT8 *) (UINTN) FtwLastWriteData->SpareAddress, BackUpSize);\r
3036 }\r
3037 }\r
3038\r
3039 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) NvStorageData;\r
3040\r
3041 //\r
3042 // Check if the Firmware Volume is not corrupted\r
3043 //\r
3044 if ((FvHeader->Signature != EFI_FVH_SIGNATURE) || (!CompareGuid (&gEfiSystemNvDataFvGuid, &FvHeader->FileSystemGuid))) {\r
3045 FreePool (NvStorageData);\r
3046 DEBUG ((EFI_D_ERROR, "Firmware Volume for Variable Store is corrupted\n"));\r
3047 return EFI_VOLUME_CORRUPTED;\r
3048 }\r
3049\r
3050 VariableStoreBase = (EFI_PHYSICAL_ADDRESS) ((UINTN) FvHeader + FvHeader->HeaderLength);\r
3051 VariableStoreLength = (UINT64) (NvStorageSize - FvHeader->HeaderLength);\r
3052\r
3053 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase;\r
3054 mNvVariableCache = (VARIABLE_STORE_HEADER *) (UINTN) VariableStoreBase;\r
3055 if (GetVariableStoreStatus (mNvVariableCache) != EfiValid) {\r
3056 FreePool (NvStorageData);\r
3057 DEBUG((EFI_D_ERROR, "Variable Store header is corrupted\n"));\r
3058 return EFI_VOLUME_CORRUPTED;\r
3059 }\r
3060 ASSERT(mNvVariableCache->Size == VariableStoreLength);\r
3061\r
3062 //\r
3063 // The max variable or hardware error variable size should be < variable store size.\r
3064 //\r
3065 ASSERT(MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize)) < VariableStoreLength);\r
3066\r
3067 //\r
3068 // Parse non-volatile variable data and get last variable offset.\r
3069 //\r
3070 NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase);\r
3071 while (IsValidVariableHeader (NextVariable)) {\r
3072 VariableSize = NextVariable->NameSize + NextVariable->DataSize + sizeof (VARIABLE_HEADER);\r
3073 if ((NextVariable->Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
3074 mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VariableSize);\r
3075 } else {\r
3076 mVariableModuleGlobal->CommonVariableTotalSize += HEADER_ALIGN (VariableSize);\r
3077 }\r
3078\r
3079 NextVariable = GetNextVariablePtr (NextVariable);\r
3080 }\r
3081 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) VariableStoreBase;\r
3082\r
3083 return EFI_SUCCESS;\r
3084}\r
3085\r
335e2681
SZ
3086/**\r
3087 Flush the HOB variable to flash.\r
3088\r
3089 @param[in] VariableName Name of variable has been updated or deleted.\r
3090 @param[in] VendorGuid Guid of variable has been updated or deleted.\r
3091\r
3092**/\r
3093VOID\r
3094FlushHobVariableToFlash (\r
3095 IN CHAR16 *VariableName,\r
3096 IN EFI_GUID *VendorGuid\r
3097 )\r
3098{\r
3099 EFI_STATUS Status;\r
3100 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
3101 VARIABLE_HEADER *Variable;\r
3102 VOID *VariableData;\r
3103 BOOLEAN ErrorFlag;\r
3104\r
3105 ErrorFlag = FALSE;\r
3106\r
3107 //\r
3108 // Flush the HOB variable to flash.\r
3109 //\r
3110 if (mVariableModuleGlobal->VariableGlobal.HobVariableBase != 0) {\r
3111 VariableStoreHeader = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;\r
3112 //\r
3113 // Set HobVariableBase to 0, it can avoid SetVariable to call back.\r
3114 //\r
3115 mVariableModuleGlobal->VariableGlobal.HobVariableBase = 0;\r
3116 for ( Variable = GetStartPointer (VariableStoreHeader)\r
3117 ; (Variable < GetEndPointer (VariableStoreHeader) && IsValidVariableHeader (Variable))\r
3118 ; Variable = GetNextVariablePtr (Variable)\r
3119 ) {\r
3120 if (Variable->State != VAR_ADDED) {\r
3121 //\r
3122 // The HOB variable has been set to DELETED state in local.\r
3123 //\r
3124 continue;\r
3125 }\r
3126 ASSERT ((Variable->Attributes & EFI_VARIABLE_NON_VOLATILE) != 0);\r
3127 if (VendorGuid == NULL || VariableName == NULL ||\r
3128 !CompareGuid (VendorGuid, &Variable->VendorGuid) ||\r
3129 StrCmp (VariableName, GetVariableNamePtr (Variable)) != 0) {\r
3130 VariableData = GetVariableDataPtr (Variable);\r
3131 Status = VariableServiceSetVariable (\r
3132 GetVariableNamePtr (Variable),\r
3133 &Variable->VendorGuid,\r
3134 Variable->Attributes,\r
3135 Variable->DataSize,\r
3136 VariableData\r
3137 );\r
3138 DEBUG ((EFI_D_INFO, "Variable driver flush the HOB variable to flash: %g %s %r\n", &Variable->VendorGuid, GetVariableNamePtr (Variable), Status));\r
3139 } else {\r
3140 //\r
3141 // The updated or deleted variable is matched with the HOB variable.\r
3142 // Don't break here because we will try to set other HOB variables\r
3143 // since this variable could be set successfully.\r
3144 //\r
3145 Status = EFI_SUCCESS;\r
3146 }\r
3147 if (!EFI_ERROR (Status)) {\r
3148 //\r
3149 // If set variable successful, or the updated or deleted variable is matched with the HOB variable,\r
3150 // set the HOB variable to DELETED state in local.\r
3151 //\r
3152 DEBUG ((EFI_D_INFO, "Variable driver set the HOB variable to DELETED state in local: %g %s\n", &Variable->VendorGuid, GetVariableNamePtr (Variable)));\r
3153 Variable->State &= VAR_DELETED;\r
3154 } else {\r
3155 ErrorFlag = TRUE;\r
3156 }\r
3157 }\r
3158 if (ErrorFlag) {\r
3159 //\r
3160 // We still have HOB variable(s) not flushed in flash.\r
3161 //\r
3162 mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VariableStoreHeader;\r
3163 } else {\r
3164 //\r
3165 // All HOB variables have been flushed in flash.\r
3166 //\r
3167 DEBUG ((EFI_D_INFO, "Variable driver: all HOB variables have been flushed in flash.\n"));\r
3168 if (!AtRuntime ()) {\r
3169 FreePool ((VOID *) VariableStoreHeader);\r
3170 }\r
3171 }\r
3172 }\r
3173\r
3174}\r
8a2d4996 3175\r
7c80e839 3176/**\r
3e02ebb2 3177 Initializes variable write service after FTW was ready.\r
7c80e839 3178\r
8a2d4996 3179 @retval EFI_SUCCESS Function successfully executed.\r
3180 @retval Others Fail to initialize the variable service.\r
3181\r
3182**/\r
3183EFI_STATUS\r
3184VariableWriteServiceInitialize (\r
3185 VOID\r
3186 )\r
3187{\r
3188 EFI_STATUS Status;\r
3189 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
3190 UINTN Index;\r
3191 UINT8 Data;\r
8a2d4996 3192 EFI_PHYSICAL_ADDRESS VariableStoreBase;\r
3e02ebb2 3193 EFI_PHYSICAL_ADDRESS NvStorageBase;\r
8a2d4996 3194\r
3e02ebb2
SZ
3195 NvStorageBase = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);\r
3196 if (NvStorageBase == 0) {\r
3197 NvStorageBase = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);\r
3198 }\r
3199 VariableStoreBase = NvStorageBase + (((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(NvStorageBase))->HeaderLength);\r
3200\r
3201 //\r
3202 // Let NonVolatileVariableBase point to flash variable store base directly after FTW ready.\r
3203 //\r
3204 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase;\r
8a2d4996 3205 VariableStoreHeader = (VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase;\r
0f7aff72 3206 \r
8a2d4996 3207 //\r
3208 // Check if the free area is really free.\r
3209 //\r
0f7aff72 3210 for (Index = mVariableModuleGlobal->NonVolatileLastVariableOffset; Index < VariableStoreHeader->Size; Index++) {\r
8a2d4996 3211 Data = ((UINT8 *) mNvVariableCache)[Index];\r
3212 if (Data != 0xff) {\r
3213 //\r
3214 // There must be something wrong in variable store, do reclaim operation.\r
3215 //\r
3216 Status = Reclaim (\r
3217 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
3218 &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
3219 FALSE,\r
335e2681 3220 NULL,\r
7baf3c69
SZ
3221 NULL,\r
3222 0\r
8a2d4996 3223 );\r
3224 if (EFI_ERROR (Status)) {\r
3225 return Status;\r
3226 }\r
3227 break;\r
3228 }\r
3229 }\r
3230\r
335e2681 3231 FlushHobVariableToFlash (NULL, NULL);\r
0f7aff72 3232\r
8a2d4996 3233 return EFI_SUCCESS;\r
3234}\r
3235\r
3236\r
3237/**\r
3238 Initializes variable store area for non-volatile and volatile variable.\r
7c80e839 3239\r
3240 @retval EFI_SUCCESS Function successfully executed.\r
3241 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.\r
3242\r
3243**/\r
8d3a5c82 3244EFI_STATUS\r
8d3a5c82 3245VariableCommonInitialize (\r
8a2d4996 3246 VOID\r
8d3a5c82 3247 )\r
8d3a5c82 3248{\r
3249 EFI_STATUS Status;\r
8d3a5c82 3250 VARIABLE_STORE_HEADER *VolatileVariableStore;\r
3251 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
052ad7e1 3252 UINT64 VariableStoreLength;\r
2fcdca1d 3253 UINTN ScratchSize;\r
0f7aff72 3254 EFI_HOB_GUID_TYPE *GuidHob;\r
8d3a5c82 3255\r
7800593d
LG
3256 //\r
3257 // Allocate runtime memory for variable driver global structure.\r
3258 //\r
72399dae 3259 mVariableModuleGlobal = AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL));\r
7800593d
LG
3260 if (mVariableModuleGlobal == NULL) {\r
3261 return EFI_OUT_OF_RESOURCES;\r
3262 }\r
8d3a5c82 3263\r
8a2d4996 3264 InitializeLock (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock, TPL_NOTIFY);\r
8d3a5c82 3265\r
0f7aff72
RN
3266 //\r
3267 // Get HOB variable store.\r
3268 //\r
3269 GuidHob = GetFirstGuidHob (&gEfiVariableGuid);\r
3270 if (GuidHob != NULL) {\r
f68af18e 3271 VariableStoreHeader = GET_GUID_HOB_DATA (GuidHob);\r
335e2681 3272 VariableStoreLength = (UINT64) (GuidHob->Header.HobLength - sizeof (EFI_HOB_GUID_TYPE));\r
f68af18e 3273 if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) {\r
335e2681
SZ
3274 mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) AllocateRuntimeCopyPool ((UINTN) VariableStoreLength, (VOID *) VariableStoreHeader);\r
3275 if (mVariableModuleGlobal->VariableGlobal.HobVariableBase == 0) {\r
3e02ebb2 3276 FreePool (mVariableModuleGlobal);\r
335e2681
SZ
3277 return EFI_OUT_OF_RESOURCES;\r
3278 }\r
f68af18e
RN
3279 } else {\r
3280 DEBUG ((EFI_D_ERROR, "HOB Variable Store header is corrupted!\n"));\r
3281 }\r
0f7aff72
RN
3282 }\r
3283\r
8d3a5c82 3284 //\r
2fcdca1d 3285 // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.\r
8d3a5c82 3286 //\r
188e4e84 3287 ScratchSize = MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize));\r
3288 VolatileVariableStore = AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize) + ScratchSize);\r
8d3a5c82 3289 if (VolatileVariableStore == NULL) {\r
3e02ebb2
SZ
3290 if (mVariableModuleGlobal->VariableGlobal.HobVariableBase != 0) {\r
3291 FreePool ((VOID *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase);\r
3292 }\r
8d3a5c82 3293 FreePool (mVariableModuleGlobal);\r
3294 return EFI_OUT_OF_RESOURCES;\r
3295 }\r
3296\r
188e4e84 3297 SetMem (VolatileVariableStore, PcdGet32 (PcdVariableStoreSize) + ScratchSize, 0xff);\r
8d3a5c82 3298\r
3299 //\r
8a2d4996 3300 // Initialize Variable Specific Data.\r
8d3a5c82 3301 //\r
052ad7e1 3302 mVariableModuleGlobal->VariableGlobal.VolatileVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VolatileVariableStore;\r
9cad030b 3303 mVariableModuleGlobal->VolatileLastVariableOffset = (UINTN) GetStartPointer (VolatileVariableStore) - (UINTN) VolatileVariableStore;\r
8d3a5c82 3304\r
3709c4cd 3305 CopyGuid (&VolatileVariableStore->Signature, &gEfiVariableGuid);\r
8a2d4996 3306 VolatileVariableStore->Size = PcdGet32 (PcdVariableStoreSize);\r
3307 VolatileVariableStore->Format = VARIABLE_STORE_FORMATTED;\r
3308 VolatileVariableStore->State = VARIABLE_STORE_HEALTHY;\r
3309 VolatileVariableStore->Reserved = 0;\r
3310 VolatileVariableStore->Reserved1 = 0;\r
8d3a5c82 3311\r
3312 //\r
3e02ebb2 3313 // Init non-volatile variable store.\r
8a2d4996 3314 //\r
3e02ebb2 3315 Status = InitNonVolatileVariableStore ();\r
8d3a5c82 3316 if (EFI_ERROR (Status)) {\r
3e02ebb2
SZ
3317 if (mVariableModuleGlobal->VariableGlobal.HobVariableBase != 0) {\r
3318 FreePool ((VOID *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase);\r
3319 }\r
8d3a5c82 3320 FreePool (mVariableModuleGlobal);\r
3321 FreePool (VolatileVariableStore);\r
3322 }\r
3323\r
3324 return Status;\r
3325}\r
052ad7e1 3326\r
052ad7e1 3327\r
aa75dfec 3328/**\r
8a2d4996 3329 Get the proper fvb handle and/or fvb protocol by the given Flash address.\r
aa75dfec 3330\r
8a2d4996 3331 @param[in] Address The Flash address.\r
3332 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.\r
3333 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.\r
aa75dfec 3334\r
aa75dfec 3335**/\r
8a2d4996 3336EFI_STATUS\r
3337GetFvbInfoByAddress (\r
3338 IN EFI_PHYSICAL_ADDRESS Address,\r
3339 OUT EFI_HANDLE *FvbHandle OPTIONAL,\r
3340 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvbProtocol OPTIONAL\r
8a9e0b72 3341 )\r
3342{\r
8a2d4996 3343 EFI_STATUS Status;\r
3344 EFI_HANDLE *HandleBuffer;\r
3345 UINTN HandleCount;\r
3346 UINTN Index;\r
3347 EFI_PHYSICAL_ADDRESS FvbBaseAddress;\r
3348 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r
3349 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r
3350 EFI_FVB_ATTRIBUTES_2 Attributes;\r
3351 \r
8a9e0b72 3352 //\r
8a2d4996 3353 // Get all FVB handles.\r
8a9e0b72 3354 //\r
8a2d4996 3355 Status = GetFvbCountAndBuffer (&HandleCount, &HandleBuffer);\r
8a9e0b72 3356 if (EFI_ERROR (Status)) {\r
8a2d4996 3357 return EFI_NOT_FOUND;\r
8a9e0b72 3358 }\r
8a2d4996 3359\r
8a9e0b72 3360 //\r
8a2d4996 3361 // Get the FVB to access variable store.\r
8a9e0b72 3362 //\r
8a2d4996 3363 Fvb = NULL;\r
f0480ecf 3364 for (Index = 0; Index < HandleCount; Index += 1, Status = EFI_NOT_FOUND, Fvb = NULL) {\r
8a2d4996 3365 Status = GetFvbByHandle (HandleBuffer[Index], &Fvb);\r
8a9e0b72 3366 if (EFI_ERROR (Status)) {\r
3367 Status = EFI_NOT_FOUND;\r
3368 break;\r
3369 }\r
3370\r
3371 //\r
3372 // Ensure this FVB protocol supported Write operation.\r
3373 //\r
3374 Status = Fvb->GetAttributes (Fvb, &Attributes);\r
3375 if (EFI_ERROR (Status) || ((Attributes & EFI_FVB2_WRITE_STATUS) == 0)) {\r
3376 continue; \r
3377 }\r
8a2d4996 3378 \r
8a9e0b72 3379 //\r
8a2d4996 3380 // Compare the address and select the right one.\r
8a9e0b72 3381 //\r
3382 Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);\r
3383 if (EFI_ERROR (Status)) {\r
3384 continue;\r
3385 }\r
3386\r
3387 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvbBaseAddress);\r
8a2d4996 3388 if ((Address >= FvbBaseAddress) && (Address < (FvbBaseAddress + FwVolHeader->FvLength))) {\r
3389 if (FvbHandle != NULL) {\r
3390 *FvbHandle = HandleBuffer[Index];\r
3391 }\r
3392 if (FvbProtocol != NULL) {\r
3393 *FvbProtocol = Fvb;\r
3394 }\r
3395 Status = EFI_SUCCESS;\r
8a9e0b72 3396 break;\r
3397 }\r
3398 }\r
8a9e0b72 3399 FreePool (HandleBuffer);\r
533020ef 3400\r
8a2d4996 3401 if (Fvb == NULL) {\r
3402 Status = EFI_NOT_FOUND;\r
8a9e0b72 3403 }\r
052ad7e1 3404 \r
8a2d4996 3405 return Status; \r
052ad7e1
A
3406}\r
3407\r