Support load 64 bit image from 32 bit core.
[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
72399dae 1261/**\r
1262 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.\r
052ad7e1 1263\r
72399dae 1264 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.\r
052ad7e1 1265\r
72399dae 1266 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,\r
1267 and are read-only. Therefore, in variable driver, only store the original value for other use.\r
8d3a5c82 1268\r
8a2d4996 1269 @param[in] VariableName Name of variable.\r
8d3a5c82 1270\r
8a2d4996 1271 @param[in] Data Variable data.\r
8d3a5c82 1272\r
8a2d4996 1273 @param[in] DataSize Size of data. 0 means delete.\r
72399dae 1274\r
9bc5dabb
SZ
1275 @retval EFI_SUCCESS The update operation is successful or ignored.\r
1276 @retval EFI_WRITE_PROTECTED Update PlatformLangCodes/LangCodes at runtime.\r
1277 @retval EFI_OUT_OF_RESOURCES No enough variable space to do the update operation.\r
1278 @retval Others Other errors happened during the update operation.\r
1279\r
7c80e839 1280**/\r
9bc5dabb 1281EFI_STATUS\r
d6550260 1282AutoUpdateLangVariable (\r
72399dae 1283 IN CHAR16 *VariableName,\r
1284 IN VOID *Data,\r
1285 IN UINTN DataSize\r
052ad7e1 1286 )\r
8d3a5c82 1287{\r
255a3f33
RN
1288 EFI_STATUS Status;\r
1289 CHAR8 *BestPlatformLang;\r
1290 CHAR8 *BestLang;\r
1291 UINTN Index;\r
1292 UINT32 Attributes;\r
72399dae 1293 VARIABLE_POINTER_TRACK Variable;\r
255a3f33 1294 BOOLEAN SetLanguageCodes;\r
8d3a5c82 1295\r
72399dae 1296 //\r
255a3f33 1297 // Don't do updates for delete operation\r
72399dae 1298 //\r
255a3f33 1299 if (DataSize == 0) {\r
9bc5dabb 1300 return EFI_SUCCESS;\r
255a3f33
RN
1301 }\r
1302\r
1303 SetLanguageCodes = FALSE;\r
8d3a5c82 1304\r
6675a21f 1305 if (StrCmp (VariableName, EFI_PLATFORM_LANG_CODES_VARIABLE_NAME) == 0) {\r
255a3f33
RN
1306 //\r
1307 // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.\r
1308 //\r
8a2d4996 1309 if (AtRuntime ()) {\r
9bc5dabb 1310 return EFI_WRITE_PROTECTED;\r
255a3f33
RN
1311 }\r
1312\r
1313 SetLanguageCodes = TRUE;\r
1314\r
72399dae 1315 //\r
1316 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only\r
1317 // Therefore, in variable driver, only store the original value for other use.\r
1318 //\r
255a3f33
RN
1319 if (mVariableModuleGlobal->PlatformLangCodes != NULL) {\r
1320 FreePool (mVariableModuleGlobal->PlatformLangCodes);\r
1321 }\r
1322 mVariableModuleGlobal->PlatformLangCodes = AllocateRuntimeCopyPool (DataSize, Data);\r
1323 ASSERT (mVariableModuleGlobal->PlatformLangCodes != NULL);\r
1324\r
72399dae 1325 //\r
255a3f33
RN
1326 // PlatformLang holds a single language from PlatformLangCodes, \r
1327 // so the size of PlatformLangCodes is enough for the PlatformLang.\r
72399dae 1328 //\r
255a3f33
RN
1329 if (mVariableModuleGlobal->PlatformLang != NULL) {\r
1330 FreePool (mVariableModuleGlobal->PlatformLang);\r
1331 }\r
1332 mVariableModuleGlobal->PlatformLang = AllocateRuntimePool (DataSize);\r
1333 ASSERT (mVariableModuleGlobal->PlatformLang != NULL);\r
fdb7765f 1334\r
6675a21f 1335 } else if (StrCmp (VariableName, EFI_LANG_CODES_VARIABLE_NAME) == 0) {\r
72399dae 1336 //\r
255a3f33 1337 // LangCodes is a volatile variable, so it can not be updated at runtime.\r
72399dae 1338 //\r
8a2d4996 1339 if (AtRuntime ()) {\r
9bc5dabb 1340 return EFI_WRITE_PROTECTED;\r
255a3f33
RN
1341 }\r
1342\r
1343 SetLanguageCodes = TRUE;\r
8d3a5c82 1344\r
8d3a5c82 1345 //\r
255a3f33
RN
1346 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only\r
1347 // Therefore, in variable driver, only store the original value for other use.\r
8d3a5c82 1348 //\r
255a3f33
RN
1349 if (mVariableModuleGlobal->LangCodes != NULL) {\r
1350 FreePool (mVariableModuleGlobal->LangCodes);\r
1351 }\r
1352 mVariableModuleGlobal->LangCodes = AllocateRuntimeCopyPool (DataSize, Data);\r
1353 ASSERT (mVariableModuleGlobal->LangCodes != NULL);\r
1354 }\r
8d3a5c82 1355\r
255a3f33
RN
1356 if (SetLanguageCodes \r
1357 && (mVariableModuleGlobal->PlatformLangCodes != NULL)\r
1358 && (mVariableModuleGlobal->LangCodes != NULL)) {\r
8d3a5c82 1359 //\r
255a3f33
RN
1360 // Update Lang if PlatformLang is already set\r
1361 // Update PlatformLang if Lang is already set\r
8d3a5c82 1362 //\r
6675a21f 1363 Status = FindVariable (EFI_PLATFORM_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
255a3f33
RN
1364 if (!EFI_ERROR (Status)) {\r
1365 //\r
1366 // Update Lang\r
1367 //\r
6675a21f 1368 VariableName = EFI_PLATFORM_LANG_VARIABLE_NAME;\r
255a3f33
RN
1369 Data = GetVariableDataPtr (Variable.CurrPtr);\r
1370 DataSize = Variable.CurrPtr->DataSize;\r
1371 } else {\r
6675a21f 1372 Status = FindVariable (EFI_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
255a3f33
RN
1373 if (!EFI_ERROR (Status)) {\r
1374 //\r
1375 // Update PlatformLang\r
1376 //\r
6675a21f 1377 VariableName = EFI_LANG_VARIABLE_NAME;\r
255a3f33
RN
1378 Data = GetVariableDataPtr (Variable.CurrPtr);\r
1379 DataSize = Variable.CurrPtr->DataSize;\r
1380 } else {\r
1381 //\r
1382 // Neither PlatformLang nor Lang is set, directly return\r
1383 //\r
9bc5dabb 1384 return EFI_SUCCESS;\r
255a3f33
RN
1385 }\r
1386 }\r
1387 }\r
9bc5dabb
SZ
1388\r
1389 Status = EFI_SUCCESS;\r
1390\r
255a3f33
RN
1391 //\r
1392 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.\r
1393 //\r
1394 Attributes = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;\r
8d3a5c82 1395\r
6675a21f 1396 if (StrCmp (VariableName, EFI_PLATFORM_LANG_VARIABLE_NAME) == 0) {\r
8d3a5c82 1397 //\r
255a3f33 1398 // Update Lang when PlatformLangCodes/LangCodes were set.\r
8d3a5c82 1399 //\r
255a3f33
RN
1400 if ((mVariableModuleGlobal->PlatformLangCodes != NULL) && (mVariableModuleGlobal->LangCodes != NULL)) {\r
1401 //\r
1402 // When setting PlatformLang, firstly get most matched language string from supported language codes.\r
1403 //\r
1404 BestPlatformLang = VariableGetBestLanguage (mVariableModuleGlobal->PlatformLangCodes, FALSE, Data, NULL);\r
1405 if (BestPlatformLang != NULL) {\r
1406 //\r
1407 // Get the corresponding index in language codes.\r
1408 //\r
1409 Index = GetIndexFromSupportedLangCodes (mVariableModuleGlobal->PlatformLangCodes, BestPlatformLang, FALSE);\r
fdb7765f 1410\r
255a3f33
RN
1411 //\r
1412 // Get the corresponding ISO639 language tag according to RFC4646 language tag.\r
1413 //\r
1414 BestLang = GetLangFromSupportedLangCodes (mVariableModuleGlobal->LangCodes, Index, TRUE);\r
8d3a5c82 1415\r
255a3f33
RN
1416 //\r
1417 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.\r
1418 //\r
6675a21f 1419 FindVariable (EFI_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
8d3a5c82 1420\r
6675a21f 1421 Status = UpdateVariable (EFI_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, BestLang,\r
8a2d4996 1422 ISO_639_2_ENTRY_SIZE + 1, Attributes, &Variable);\r
8d3a5c82 1423\r
9bc5dabb 1424 DEBUG ((EFI_D_INFO, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a: Status: %r\n", BestPlatformLang, BestLang, Status));\r
255a3f33
RN
1425 }\r
1426 }\r
72399dae 1427\r
6675a21f 1428 } else if (StrCmp (VariableName, EFI_LANG_VARIABLE_NAME) == 0) {\r
72399dae 1429 //\r
255a3f33 1430 // Update PlatformLang when PlatformLangCodes/LangCodes were set.\r
72399dae 1431 //\r
255a3f33
RN
1432 if ((mVariableModuleGlobal->PlatformLangCodes != NULL) && (mVariableModuleGlobal->LangCodes != NULL)) {\r
1433 //\r
1434 // When setting Lang, firstly get most matched language string from supported language codes.\r
1435 //\r
1436 BestLang = VariableGetBestLanguage (mVariableModuleGlobal->LangCodes, TRUE, Data, NULL);\r
1437 if (BestLang != NULL) {\r
1438 //\r
1439 // Get the corresponding index in language codes.\r
1440 //\r
1441 Index = GetIndexFromSupportedLangCodes (mVariableModuleGlobal->LangCodes, BestLang, TRUE);\r
72399dae 1442\r
255a3f33
RN
1443 //\r
1444 // Get the corresponding RFC4646 language tag according to ISO639 language tag.\r
1445 //\r
1446 BestPlatformLang = GetLangFromSupportedLangCodes (mVariableModuleGlobal->PlatformLangCodes, Index, FALSE);\r
1447\r
1448 //\r
1449 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.\r
1450 //\r
6675a21f 1451 FindVariable (EFI_PLATFORM_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
72399dae 1452\r
6675a21f 1453 Status = UpdateVariable (EFI_PLATFORM_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, BestPlatformLang, \r
255a3f33 1454 AsciiStrSize (BestPlatformLang), Attributes, &Variable);\r
72399dae 1455\r
9bc5dabb 1456 DEBUG ((EFI_D_INFO, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a Status: %r\n", BestLang, BestPlatformLang, Status));\r
255a3f33
RN
1457 }\r
1458 }\r
72399dae 1459 }\r
9bc5dabb
SZ
1460\r
1461 return Status;\r
8d3a5c82 1462}\r
1463\r
7c80e839 1464/**\r
72399dae 1465 Update the variable region with Variable information. These are the same \r
1466 arguments as the EFI Variable services.\r
052ad7e1 1467\r
8a2d4996 1468 @param[in] VariableName Name of variable.\r
1469 @param[in] VendorGuid Guid of variable.\r
1470 @param[in] Data Variable data.\r
1471 @param[in] DataSize Size of data. 0 means delete.\r
1472 @param[in] Attributes Attribues of the variable.\r
23b06935 1473 @param[in, out] CacheVariable The variable information which is used to keep track of variable usage.\r
8a2d4996 1474 \r
72399dae 1475 @retval EFI_SUCCESS The update operation is success.\r
72399dae 1476 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.\r
8d3a5c82 1477\r
7c80e839 1478**/\r
052ad7e1 1479EFI_STATUS\r
72399dae 1480UpdateVariable (\r
8a2d4996 1481 IN CHAR16 *VariableName,\r
1482 IN EFI_GUID *VendorGuid,\r
1483 IN VOID *Data,\r
1484 IN UINTN DataSize,\r
1485 IN UINT32 Attributes OPTIONAL,\r
23b06935 1486 IN OUT VARIABLE_POINTER_TRACK *CacheVariable\r
052ad7e1 1487 )\r
8d3a5c82 1488{\r
8a9e0b72 1489 EFI_STATUS Status;\r
1490 VARIABLE_HEADER *NextVariable;\r
72399dae 1491 UINTN ScratchSize;\r
1492 UINTN NonVolatileVarableStoreSize;\r
8a9e0b72 1493 UINTN VarNameOffset;\r
1494 UINTN VarDataOffset;\r
72399dae 1495 UINTN VarNameSize;\r
8a9e0b72 1496 UINTN VarSize;\r
72399dae 1497 BOOLEAN Volatile;\r
1498 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r
8a9e0b72 1499 UINT8 State;\r
8a2d4996 1500 VARIABLE_POINTER_TRACK *Variable;\r
1501 VARIABLE_POINTER_TRACK NvVariable;\r
1502 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
1503 UINTN CacheOffset;\r
fdb7765f 1504\r
5456306f 1505 if ((mVariableModuleGlobal->FvbInstance == NULL) && ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0)) {\r
1506 //\r
1507 // The FVB protocol is not ready. Trying to update NV variable prior to the installation\r
1508 // of EFI_VARIABLE_WRITE_ARCH_PROTOCOL.\r
1509 //\r
1510 return EFI_NOT_AVAILABLE_YET; \r
1511 }\r
1512\r
1513 if ((CacheVariable->CurrPtr == NULL) || CacheVariable->Volatile) {\r
8a2d4996 1514 Variable = CacheVariable;\r
1515 } else {\r
8a2d4996 1516 //\r
5456306f 1517 // Update/Delete existing NV variable.\r
8a2d4996 1518 // CacheVariable points to the variable in the memory copy of Flash area\r
1519 // Now let Variable points to the same variable in Flash area.\r
1520 //\r
1521 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);\r
1522 Variable = &NvVariable; \r
1523 Variable->StartPtr = GetStartPointer (VariableStoreHeader);\r
1524 Variable->EndPtr = GetEndPointer (VariableStoreHeader);\r
5456306f 1525 Variable->CurrPtr = (VARIABLE_HEADER *)((UINTN)Variable->StartPtr + ((UINTN)CacheVariable->CurrPtr - (UINTN)CacheVariable->StartPtr));\r
23b06935
SZ
1526 if (CacheVariable->InDeletedTransitionPtr != NULL) {\r
1527 Variable->InDeletedTransitionPtr = (VARIABLE_HEADER *)((UINTN)Variable->StartPtr + ((UINTN)CacheVariable->InDeletedTransitionPtr - (UINTN)CacheVariable->StartPtr));\r
1528 } else {\r
1529 Variable->InDeletedTransitionPtr = NULL;\r
1530 }\r
5456306f 1531 Variable->Volatile = FALSE;\r
1532 } \r
1533\r
1534 Fvb = mVariableModuleGlobal->FvbInstance;\r
fdb7765f 1535\r
72399dae 1536 if (Variable->CurrPtr != NULL) {\r
8d3a5c82 1537 //\r
8a2d4996 1538 // Update/Delete existing variable.\r
8d3a5c82 1539 //\r
8a2d4996 1540 if (AtRuntime ()) { \r
c6492839 1541 //\r
8a2d4996 1542 // If AtRuntime and the variable is Volatile and Runtime Access, \r
c6492839 1543 // the volatile is ReadOnly, and SetVariable should be aborted and \r
1544 // return EFI_WRITE_PROTECTED.\r
1545 //\r
72399dae 1546 if (Variable->Volatile) {\r
c6492839 1547 Status = EFI_WRITE_PROTECTED;\r
1548 goto Done;\r
1549 }\r
1550 //\r
9622df63 1551 // Only variable that have NV|RT attributes can be updated/deleted in Runtime.\r
c6492839 1552 //\r
9622df63 1553 if (((Variable->CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) || ((Variable->CurrPtr->Attributes & EFI_VARIABLE_NON_VOLATILE) == 0)) {\r
c6492839 1554 Status = EFI_INVALID_PARAMETER;\r
1555 goto Done; \r
1556 }\r
1557 }\r
8a2d4996 1558\r
8d3a5c82 1559 //\r
c6492839 1560 // Setting a data variable with no access, or zero DataSize attributes\r
8a2d4996 1561 // causes it to be deleted.\r
8d3a5c82 1562 //\r
c6492839 1563 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) { \r
23b06935
SZ
1564 if (Variable->InDeletedTransitionPtr != NULL) {\r
1565 //\r
1566 // Both ADDED and IN_DELETED_TRANSITION variable are present,\r
1567 // set IN_DELETED_TRANSITION one to DELETED state first.\r
1568 //\r
1569 State = Variable->InDeletedTransitionPtr->State;\r
1570 State &= VAR_DELETED;\r
1571 Status = UpdateVariableStore (\r
1572 &mVariableModuleGlobal->VariableGlobal,\r
1573 Variable->Volatile,\r
1574 FALSE,\r
1575 Fvb,\r
1576 (UINTN) &Variable->InDeletedTransitionPtr->State,\r
1577 sizeof (UINT8),\r
1578 &State\r
1579 );\r
1580 if (!EFI_ERROR (Status)) {\r
1581 if (!Variable->Volatile) {\r
0cc565de 1582 ASSERT (CacheVariable->InDeletedTransitionPtr != NULL);\r
23b06935
SZ
1583 CacheVariable->InDeletedTransitionPtr->State = State;\r
1584 }\r
1585 } else {\r
1586 goto Done;\r
1587 }\r
1588 }\r
1589\r
72399dae 1590 State = Variable->CurrPtr->State;\r
c6492839 1591 State &= VAR_DELETED;\r
1592\r
1593 Status = UpdateVariableStore (\r
052ad7e1 1594 &mVariableModuleGlobal->VariableGlobal,\r
72399dae 1595 Variable->Volatile,\r
c6492839 1596 FALSE,\r
8a9e0b72 1597 Fvb,\r
72399dae 1598 (UINTN) &Variable->CurrPtr->State,\r
c6492839 1599 sizeof (UINT8),\r
1600 &State\r
1601 ); \r
33a5a666 1602 if (!EFI_ERROR (Status)) {\r
8a2d4996 1603 UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, FALSE, TRUE, FALSE);\r
1604 if (!Variable->Volatile) {\r
1605 CacheVariable->CurrPtr->State = State;\r
335e2681 1606 FlushHobVariableToFlash (VariableName, VendorGuid);\r
8a2d4996 1607 }\r
33a5a666 1608 }\r
c6492839 1609 goto Done; \r
1610 }\r
8d3a5c82 1611 //\r
8a2d4996 1612 // If the variable is marked valid, and the same data has been passed in,\r
c6492839 1613 // then return to the caller immediately.\r
8d3a5c82 1614 //\r
72399dae 1615 if (DataSizeOfVariable (Variable->CurrPtr) == DataSize &&\r
1616 (CompareMem (Data, GetVariableDataPtr (Variable->CurrPtr), DataSize) == 0)) {\r
33a5a666 1617 \r
8a2d4996 1618 UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, TRUE, FALSE, FALSE);\r
c6492839 1619 Status = EFI_SUCCESS;\r
1620 goto Done;\r
72399dae 1621 } else if ((Variable->CurrPtr->State == VAR_ADDED) ||\r
1622 (Variable->CurrPtr->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {\r
814bae52 1623\r
c6492839 1624 //\r
8a2d4996 1625 // Mark the old variable as in delete transition.\r
c6492839 1626 //\r
72399dae 1627 State = Variable->CurrPtr->State;\r
c6492839 1628 State &= VAR_IN_DELETED_TRANSITION;\r
1629\r
1630 Status = UpdateVariableStore (\r
052ad7e1 1631 &mVariableModuleGlobal->VariableGlobal,\r
72399dae 1632 Variable->Volatile,\r
c6492839 1633 FALSE,\r
8a9e0b72 1634 Fvb,\r
72399dae 1635 (UINTN) &Variable->CurrPtr->State,\r
c6492839 1636 sizeof (UINT8),\r
1637 &State\r
1638 ); \r
1639 if (EFI_ERROR (Status)) {\r
1640 goto Done; \r
33a5a666 1641 } \r
8a2d4996 1642 if (!Variable->Volatile) {\r
1643 CacheVariable->CurrPtr->State = State;\r
1644 }\r
c6492839 1645 } \r
72399dae 1646 } else {\r
8d3a5c82 1647 //\r
8a2d4996 1648 // Not found existing variable. Create a new variable.\r
c6492839 1649 // \r
1650 \r
8d3a5c82 1651 //\r
c6492839 1652 // Make sure we are trying to create a new variable.\r
8a2d4996 1653 // Setting a data variable with zero DataSize or no access attributes means to delete it. \r
8d3a5c82 1654 //\r
c6492839 1655 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {\r
1656 Status = EFI_NOT_FOUND;\r
1657 goto Done;\r
1658 }\r
1659 \r
8d3a5c82 1660 //\r
8a2d4996 1661 // Only variable have NV|RT attribute can be created in Runtime.\r
c6492839 1662 //\r
8a2d4996 1663 if (AtRuntime () &&\r
45f6c85b 1664 (((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) || ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0))) {\r
c6492839 1665 Status = EFI_INVALID_PARAMETER;\r
1666 goto Done;\r
1667 } \r
c6492839 1668 }\r
1669\r
1670 //\r
1671 // Function part - create a new variable and copy the data.\r
1672 // Both update a variable and create a variable will come here.\r
8a2d4996 1673\r
c6492839 1674 //\r
1675 // Tricky part: Use scratch data area at the end of volatile variable store\r
1676 // as a temporary storage.\r
1677 //\r
052ad7e1 1678 NextVariable = GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase));\r
188e4e84 1679 ScratchSize = MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize));\r
c6492839 1680\r
2fcdca1d 1681 SetMem (NextVariable, ScratchSize, 0xff);\r
c6492839 1682\r
1683 NextVariable->StartId = VARIABLE_DATA;\r
1684 NextVariable->Attributes = Attributes;\r
1685 //\r
1686 // NextVariable->State = VAR_ADDED;\r
1687 //\r
8a2d4996 1688 NextVariable->Reserved = 0;\r
1689 VarNameOffset = sizeof (VARIABLE_HEADER);\r
1690 VarNameSize = StrSize (VariableName);\r
c6492839 1691 CopyMem (\r
1692 (UINT8 *) ((UINTN) NextVariable + VarNameOffset),\r
1693 VariableName,\r
1694 VarNameSize\r
1695 );\r
1696 VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);\r
1697 CopyMem (\r
1698 (UINT8 *) ((UINTN) NextVariable + VarDataOffset),\r
1699 Data,\r
1700 DataSize\r
1701 );\r
1702 CopyMem (&NextVariable->VendorGuid, VendorGuid, sizeof (EFI_GUID));\r
1703 //\r
1704 // There will be pad bytes after Data, the NextVariable->NameSize and\r
1705 // NextVariable->DataSize should not include pad size so that variable\r
8a2d4996 1706 // service can get actual size in GetVariable.\r
c6492839 1707 //\r
1708 NextVariable->NameSize = (UINT32)VarNameSize;\r
1709 NextVariable->DataSize = (UINT32)DataSize;\r
1710\r
1711 //\r
1712 // The actual size of the variable that stores in storage should\r
1713 // include pad size.\r
1714 //\r
1715 VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);\r
45f6c85b 1716 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
8d3a5c82 1717 //\r
8a2d4996 1718 // Create a nonvolatile variable.\r
8d3a5c82 1719 //\r
fd51bf70 1720 Volatile = FALSE;\r
2fcdca1d 1721 NonVolatileVarableStoreSize = ((VARIABLE_STORE_HEADER *)(UINTN)(mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase))->Size;\r
1722 if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) \r
188e4e84 1723 && ((VarSize + mVariableModuleGlobal->HwErrVariableTotalSize) > PcdGet32 (PcdHwErrStorageSize)))\r
2fcdca1d 1724 || (((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0) \r
188e4e84 1725 && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > NonVolatileVarableStoreSize - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize)))) {\r
8a2d4996 1726 if (AtRuntime ()) {\r
c6492839 1727 Status = EFI_OUT_OF_RESOURCES;\r
1728 goto Done;\r
1729 }\r
1730 //\r
7baf3c69 1731 // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.\r
c6492839 1732 //\r
72399dae 1733 Status = Reclaim (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase, \r
7baf3c69
SZ
1734 &mVariableModuleGlobal->NonVolatileLastVariableOffset, FALSE, Variable, NextVariable, HEADER_ALIGN (VarSize));\r
1735 if (!EFI_ERROR (Status)) {\r
1736 //\r
1737 // The new variable has been integrated successfully during reclaiming.\r
1738 //\r
1739 if (Variable->CurrPtr != NULL) {\r
1740 CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN) CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr));\r
1741 CacheVariable->InDeletedTransitionPtr = NULL;\r
1742 }\r
1743 UpdateVariableInfo (VariableName, VendorGuid, FALSE, FALSE, TRUE, FALSE, FALSE);\r
1744 FlushHobVariableToFlash (VariableName, VendorGuid);\r
23b06935 1745 }\r
7baf3c69 1746 goto Done;\r
8d3a5c82 1747 }\r
1748 //\r
8a2d4996 1749 // Four steps\r
c6492839 1750 // 1. Write variable header\r
130e2569 1751 // 2. Set variable state to header valid \r
1752 // 3. Write variable data\r
1753 // 4. Set variable state to valid\r
8d3a5c82 1754 //\r
8d3a5c82 1755 //\r
c6492839 1756 // Step 1:\r
8d3a5c82 1757 //\r
8a2d4996 1758 CacheOffset = mVariableModuleGlobal->NonVolatileLastVariableOffset;\r
c6492839 1759 Status = UpdateVariableStore (\r
052ad7e1 1760 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 1761 FALSE,\r
1762 TRUE,\r
8a9e0b72 1763 Fvb,\r
72399dae 1764 mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
c6492839 1765 sizeof (VARIABLE_HEADER),\r
1766 (UINT8 *) NextVariable\r
1767 );\r
1768\r
1769 if (EFI_ERROR (Status)) {\r
1770 goto Done;\r
1771 }\r
130e2569 1772\r
8d3a5c82 1773 //\r
c6492839 1774 // Step 2:\r
8d3a5c82 1775 //\r
130e2569 1776 NextVariable->State = VAR_HEADER_VALID_ONLY;\r
1777 Status = UpdateVariableStore (\r
1778 &mVariableModuleGlobal->VariableGlobal,\r
1779 FALSE,\r
1780 TRUE,\r
8a9e0b72 1781 Fvb,\r
8a2d4996 1782 mVariableModuleGlobal->NonVolatileLastVariableOffset + OFFSET_OF (VARIABLE_HEADER, State),\r
1783 sizeof (UINT8),\r
1784 &NextVariable->State\r
130e2569 1785 );\r
1786\r
1787 if (EFI_ERROR (Status)) {\r
1788 goto Done;\r
1789 }\r
1790 //\r
1791 // Step 3:\r
1792 //\r
c6492839 1793 Status = UpdateVariableStore (\r
052ad7e1 1794 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 1795 FALSE,\r
1796 TRUE,\r
8a9e0b72 1797 Fvb,\r
72399dae 1798 mVariableModuleGlobal->NonVolatileLastVariableOffset + sizeof (VARIABLE_HEADER),\r
c6492839 1799 (UINT32) VarSize - sizeof (VARIABLE_HEADER),\r
1800 (UINT8 *) NextVariable + sizeof (VARIABLE_HEADER)\r
1801 );\r
1802\r
1803 if (EFI_ERROR (Status)) {\r
1804 goto Done;\r
1805 }\r
8d3a5c82 1806 //\r
130e2569 1807 // Step 4:\r
8d3a5c82 1808 //\r
c6492839 1809 NextVariable->State = VAR_ADDED;\r
1810 Status = UpdateVariableStore (\r
052ad7e1 1811 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 1812 FALSE,\r
1813 TRUE,\r
8a9e0b72 1814 Fvb,\r
8a2d4996 1815 mVariableModuleGlobal->NonVolatileLastVariableOffset + OFFSET_OF (VARIABLE_HEADER, State),\r
1816 sizeof (UINT8),\r
1817 &NextVariable->State\r
c6492839 1818 );\r
1819\r
1820 if (EFI_ERROR (Status)) {\r
1821 goto Done;\r
1822 }\r
8d3a5c82 1823\r
72399dae 1824 mVariableModuleGlobal->NonVolatileLastVariableOffset += HEADER_ALIGN (VarSize);\r
8d3a5c82 1825\r
2fcdca1d 1826 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {\r
1827 mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VarSize);\r
1828 } else {\r
1829 mVariableModuleGlobal->CommonVariableTotalSize += HEADER_ALIGN (VarSize);\r
1830 }\r
8a2d4996 1831 //\r
1832 // update the memory copy of Flash region.\r
1833 //\r
1834 CopyMem ((UINT8 *)mNvVariableCache + CacheOffset, (UINT8 *)NextVariable, VarSize);\r
c6492839 1835 } else {\r
1836 //\r
8a2d4996 1837 // Create a volatile variable.\r
c6492839 1838 // \r
fd51bf70 1839 Volatile = TRUE;\r
c6492839 1840\r
72399dae 1841 if ((UINT32) (VarSize + mVariableModuleGlobal->VolatileLastVariableOffset) >\r
052ad7e1 1842 ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size) {\r
8d3a5c82 1843 //\r
7baf3c69 1844 // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.\r
8d3a5c82 1845 //\r
72399dae 1846 Status = Reclaim (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase, \r
7baf3c69
SZ
1847 &mVariableModuleGlobal->VolatileLastVariableOffset, TRUE, Variable, NextVariable, HEADER_ALIGN (VarSize));\r
1848 if (!EFI_ERROR (Status)) {\r
1849 //\r
1850 // The new variable has been integrated successfully during reclaiming.\r
1851 //\r
1852 if (Variable->CurrPtr != NULL) {\r
1853 CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN) CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr));\r
1854 CacheVariable->InDeletedTransitionPtr = NULL;\r
1855 }\r
1856 UpdateVariableInfo (VariableName, VendorGuid, TRUE, FALSE, TRUE, FALSE, FALSE);\r
23b06935 1857 }\r
7baf3c69 1858 goto Done;\r
8d3a5c82 1859 }\r
8d3a5c82 1860\r
c6492839 1861 NextVariable->State = VAR_ADDED;\r
1862 Status = UpdateVariableStore (\r
052ad7e1 1863 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 1864 TRUE,\r
1865 TRUE,\r
8a9e0b72 1866 Fvb,\r
72399dae 1867 mVariableModuleGlobal->VolatileLastVariableOffset,\r
c6492839 1868 (UINT32) VarSize,\r
1869 (UINT8 *) NextVariable\r
1870 );\r
1871\r
1872 if (EFI_ERROR (Status)) {\r
1873 goto Done;\r
8d3a5c82 1874 }\r
c6492839 1875\r
72399dae 1876 mVariableModuleGlobal->VolatileLastVariableOffset += HEADER_ALIGN (VarSize);\r
c6492839 1877 }\r
72399dae 1878\r
c6492839 1879 //\r
8a2d4996 1880 // Mark the old variable as deleted.\r
c6492839 1881 //\r
23b06935
SZ
1882 if (!EFI_ERROR (Status) && Variable->CurrPtr != NULL) {\r
1883 if (Variable->InDeletedTransitionPtr != NULL) {\r
1884 //\r
1885 // Both ADDED and IN_DELETED_TRANSITION old variable are present,\r
1886 // set IN_DELETED_TRANSITION one to DELETED state first.\r
1887 //\r
1888 State = Variable->InDeletedTransitionPtr->State;\r
1889 State &= VAR_DELETED;\r
1890 Status = UpdateVariableStore (\r
1891 &mVariableModuleGlobal->VariableGlobal,\r
1892 Variable->Volatile,\r
1893 FALSE,\r
1894 Fvb,\r
1895 (UINTN) &Variable->InDeletedTransitionPtr->State,\r
1896 sizeof (UINT8),\r
1897 &State\r
1898 );\r
1899 if (!EFI_ERROR (Status)) {\r
1900 if (!Variable->Volatile) {\r
0cc565de 1901 ASSERT (CacheVariable->InDeletedTransitionPtr != NULL);\r
23b06935
SZ
1902 CacheVariable->InDeletedTransitionPtr->State = State;\r
1903 }\r
1904 } else {\r
1905 goto Done;\r
1906 }\r
1907 }\r
1908\r
72399dae 1909 State = Variable->CurrPtr->State;\r
c6492839 1910 State &= VAR_DELETED;\r
1911\r
1912 Status = UpdateVariableStore (\r
72399dae 1913 &mVariableModuleGlobal->VariableGlobal,\r
1914 Variable->Volatile,\r
1915 FALSE,\r
1916 Fvb,\r
1917 (UINTN) &Variable->CurrPtr->State,\r
1918 sizeof (UINT8),\r
1919 &State\r
1920 );\r
8a2d4996 1921 if (!EFI_ERROR (Status) && !Variable->Volatile) { \r
1922 CacheVariable->CurrPtr->State = State;\r
1923 }\r
72399dae 1924 }\r
1925\r
1926 if (!EFI_ERROR (Status)) {\r
1927 UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);\r
335e2681
SZ
1928 if (!Volatile) {\r
1929 FlushHobVariableToFlash (VariableName, VendorGuid);\r
1930 }\r
72399dae 1931 }\r
1932\r
1933Done:\r
1934 return Status;\r
1935}\r
1936\r
a5f15e30
SZ
1937/**\r
1938 Check if a Unicode character is a hexadecimal character.\r
1939\r
1940 This function checks if a Unicode character is a \r
1941 hexadecimal character. The valid hexadecimal character is \r
1942 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.\r
1943\r
1944\r
1945 @param Char The character to check against.\r
1946\r
1947 @retval TRUE If the Char is a hexadecmial character.\r
1948 @retval FALSE If the Char is not a hexadecmial character.\r
1949\r
1950**/\r
1951BOOLEAN\r
1952EFIAPI\r
1953IsHexaDecimalDigitCharacter (\r
1954 IN CHAR16 Char\r
1955 )\r
1956{\r
1957 return (BOOLEAN) ((Char >= L'0' && Char <= L'9') || (Char >= L'A' && Char <= L'F') || (Char >= L'a' && Char <= L'f'));\r
1958}\r
1959\r
1960/**\r
1961\r
1962 This code checks if variable is hardware error record variable or not.\r
1963\r
1964 According to UEFI spec, hardware error record variable should use the EFI_HARDWARE_ERROR_VARIABLE VendorGuid\r
1965 and have the L"HwErrRec####" name convention, #### is a printed hex value and no 0x or h is included in the hex value.\r
1966\r
1967 @param VariableName Pointer to variable name.\r
1968 @param VendorGuid Variable Vendor Guid.\r
1969\r
1970 @retval TRUE Variable is hardware error record variable.\r
1971 @retval FALSE Variable is not hardware error record variable.\r
1972\r
1973**/\r
1974BOOLEAN\r
1975EFIAPI\r
1976IsHwErrRecVariable (\r
1977 IN CHAR16 *VariableName,\r
1978 IN EFI_GUID *VendorGuid\r
1979 )\r
1980{\r
1981 if (!CompareGuid (VendorGuid, &gEfiHardwareErrorVariableGuid) ||\r
1982 (StrLen (VariableName) != StrLen (L"HwErrRec####")) ||\r
1983 (StrnCmp(VariableName, L"HwErrRec", StrLen (L"HwErrRec")) != 0) ||\r
1984 !IsHexaDecimalDigitCharacter (VariableName[0x8]) ||\r
1985 !IsHexaDecimalDigitCharacter (VariableName[0x9]) ||\r
1986 !IsHexaDecimalDigitCharacter (VariableName[0xA]) ||\r
1987 !IsHexaDecimalDigitCharacter (VariableName[0xB])) {\r
1988 return FALSE;\r
1989 }\r
1990\r
1991 return TRUE;\r
1992}\r
1993\r
6675a21f
SZ
1994/**\r
1995 This code checks if variable guid is global variable guid first.\r
1996 If yes, further check if variable name is in mGlobalVariableList or mGlobalVariableList2 and attributes matched.\r
1997\r
1998 @param[in] VariableName Pointer to variable name.\r
1999 @param[in] VendorGuid Variable Vendor Guid.\r
2000 @param[in] Attributes Attributes of the variable.\r
2001\r
2002 @retval EFI_SUCCESS Variable is not global variable, or Variable is global variable, variable name is in the lists and attributes matched.\r
2003 @retval EFI_INVALID_PARAMETER Variable is global variable, but variable name is not in the lists or attributes unmatched.\r
2004\r
2005**/\r
2006EFI_STATUS\r
2007EFIAPI\r
2008CheckEfiGlobalVariable (\r
2009 IN CHAR16 *VariableName,\r
2010 IN EFI_GUID *VendorGuid,\r
2011 IN UINT32 Attributes\r
2012 )\r
2013{\r
2014 UINTN Index;\r
2015 UINTN NameLength;\r
2016\r
2017 if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid)){\r
2018 //\r
2019 // Try list 1, exactly match.\r
2020 //\r
2021 for (Index = 0; Index < sizeof (mGlobalVariableList)/sizeof (mGlobalVariableList[0]); Index++) {\r
2022 if ((StrCmp (mGlobalVariableList[Index].Name, VariableName) == 0) &&\r
2023 (Attributes == 0 || Attributes == mGlobalVariableList[Index].Attributes)) {\r
2024 return EFI_SUCCESS;\r
2025 }\r
2026 }\r
2027\r
2028 //\r
2029 // Try list 2.\r
2030 //\r
2031 NameLength = StrLen (VariableName) - 4;\r
2032 for (Index = 0; Index < sizeof (mGlobalVariableList2)/sizeof (mGlobalVariableList2[0]); Index++) {\r
2033 if ((StrLen (VariableName) == StrLen (mGlobalVariableList2[Index].Name)) &&\r
2034 (StrnCmp (mGlobalVariableList2[Index].Name, VariableName, NameLength) == 0) &&\r
2035 IsHexaDecimalDigitCharacter (VariableName[NameLength]) &&\r
2036 IsHexaDecimalDigitCharacter (VariableName[NameLength + 1]) &&\r
2037 IsHexaDecimalDigitCharacter (VariableName[NameLength + 2]) &&\r
2038 IsHexaDecimalDigitCharacter (VariableName[NameLength + 3]) &&\r
2039 (Attributes == 0 || Attributes == mGlobalVariableList2[Index].Attributes)) {\r
2040 return EFI_SUCCESS;\r
2041 }\r
2042 }\r
2043\r
2044 DEBUG ((EFI_D_INFO, "[Variable]: set global variable with invalid variable name or attributes - %g:%s:%x\n", VendorGuid, VariableName, Attributes));\r
2045 return EFI_INVALID_PARAMETER;\r
2046 }\r
2047\r
2048 return EFI_SUCCESS;\r
2049}\r
2050\r
ff843847
RN
2051/**\r
2052 Mark a variable that will become read-only after leaving the DXE phase of execution.\r
2053\r
2054 @param[in] This The VARIABLE_LOCK_PROTOCOL instance.\r
2055 @param[in] VariableName A pointer to the variable name that will be made read-only subsequently.\r
2056 @param[in] VendorGuid A pointer to the vendor GUID that will be made read-only subsequently.\r
2057\r
2058 @retval EFI_SUCCESS The variable specified by the VariableName and the VendorGuid was marked\r
2059 as pending to be read-only.\r
2060 @retval EFI_INVALID_PARAMETER VariableName or VendorGuid is NULL.\r
2061 Or VariableName is an empty string.\r
2062 @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has\r
2063 already been signaled.\r
2064 @retval EFI_OUT_OF_RESOURCES There is not enough resource to hold the lock request.\r
2065**/\r
2066EFI_STATUS\r
2067EFIAPI\r
2068VariableLockRequestToLock (\r
2069 IN CONST EDKII_VARIABLE_LOCK_PROTOCOL *This,\r
2070 IN CHAR16 *VariableName,\r
2071 IN EFI_GUID *VendorGuid\r
2072 )\r
2073{\r
2074 VARIABLE_ENTRY *Entry;\r
2075\r
2076 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r
2077 return EFI_INVALID_PARAMETER;\r
2078 }\r
2079\r
2080 if (mEndOfDxe) {\r
2081 return EFI_ACCESS_DENIED;\r
2082 }\r
2083\r
2084 Entry = AllocateRuntimePool (sizeof (*Entry) + StrSize (VariableName));\r
2085 if (Entry == NULL) {\r
2086 return EFI_OUT_OF_RESOURCES;\r
2087 }\r
2088\r
2089 DEBUG ((EFI_D_INFO, "[Variable] Lock: %g:%s\n", VendorGuid, VariableName));\r
2090\r
2091 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2092\r
2093 Entry->Name = (CHAR16 *) (Entry + 1);\r
2094 StrCpy (Entry->Name, VariableName);\r
2095 CopyGuid (&Entry->Guid, VendorGuid);\r
2096 InsertTailList (&mLockedVariableList, &Entry->Link);\r
2097\r
2098 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2099\r
2100 return EFI_SUCCESS;\r
2101}\r
2102\r
72399dae 2103/**\r
2104\r
2105 This code finds variable in storage blocks (Volatile or Non-Volatile).\r
2106\r
2107 @param VariableName Name of Variable to be found.\r
2108 @param VendorGuid Variable vendor GUID.\r
2109 @param Attributes Attribute value of the variable found.\r
2110 @param DataSize Size of Data found. If size is less than the\r
2111 data, this value contains the required size.\r
2112 @param Data Data pointer.\r
2113 \r
8a2d4996 2114 @return EFI_INVALID_PARAMETER Invalid parameter.\r
2115 @return EFI_SUCCESS Find the specified variable.\r
2116 @return EFI_NOT_FOUND Not found.\r
2117 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.\r
72399dae 2118\r
2119**/\r
2120EFI_STATUS\r
2121EFIAPI\r
8a2d4996 2122VariableServiceGetVariable (\r
72399dae 2123 IN CHAR16 *VariableName,\r
2124 IN EFI_GUID *VendorGuid,\r
2125 OUT UINT32 *Attributes OPTIONAL,\r
2126 IN OUT UINTN *DataSize,\r
2127 OUT VOID *Data\r
2128 )\r
2129{\r
2130 EFI_STATUS Status;\r
2131 VARIABLE_POINTER_TRACK Variable;\r
2132 UINTN VarDataSize;\r
2133\r
2134 if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {\r
2135 return EFI_INVALID_PARAMETER;\r
2136 }\r
2137\r
2138 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
72399dae 2139 \r
9622df63 2140 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
72399dae 2141 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
2142 goto Done;\r
2143 }\r
2144\r
2145 //\r
2146 // Get data size\r
2147 //\r
2148 VarDataSize = DataSizeOfVariable (Variable.CurrPtr);\r
2149 ASSERT (VarDataSize != 0);\r
2150\r
2151 if (*DataSize >= VarDataSize) {\r
2152 if (Data == NULL) {\r
2153 Status = EFI_INVALID_PARAMETER;\r
2154 goto Done;\r
33a5a666 2155 }\r
72399dae 2156\r
2157 CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);\r
2158 if (Attributes != NULL) {\r
2159 *Attributes = Variable.CurrPtr->Attributes;\r
2160 }\r
2161\r
2162 *DataSize = VarDataSize;\r
2163 UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, TRUE, FALSE, FALSE, FALSE);\r
72399dae 2164 \r
2165 Status = EFI_SUCCESS;\r
2166 goto Done;\r
2167 } else {\r
2168 *DataSize = VarDataSize;\r
2169 Status = EFI_BUFFER_TOO_SMALL;\r
2170 goto Done;\r
8d3a5c82 2171 }\r
2172\r
72399dae 2173Done:\r
2174 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2175 return Status;\r
2176}\r
2177\r
2178\r
2179\r
2180/**\r
2181\r
2182 This code Finds the Next available variable.\r
2183\r
8a2d4996 2184 @param VariableNameSize Size of the variable name.\r
2185 @param VariableName Pointer to variable name.\r
2186 @param VendorGuid Variable Vendor Guid.\r
72399dae 2187\r
8a2d4996 2188 @return EFI_INVALID_PARAMETER Invalid parameter.\r
2189 @return EFI_SUCCESS Find the specified variable.\r
2190 @return EFI_NOT_FOUND Not found.\r
2191 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.\r
72399dae 2192\r
2193**/\r
2194EFI_STATUS\r
2195EFIAPI\r
8a2d4996 2196VariableServiceGetNextVariableName (\r
72399dae 2197 IN OUT UINTN *VariableNameSize,\r
2198 IN OUT CHAR16 *VariableName,\r
2199 IN OUT EFI_GUID *VendorGuid\r
2200 )\r
2201{\r
0f7aff72 2202 VARIABLE_STORE_TYPE Type;\r
72399dae 2203 VARIABLE_POINTER_TRACK Variable;\r
0f7aff72 2204 VARIABLE_POINTER_TRACK VariableInHob;\r
23b06935 2205 VARIABLE_POINTER_TRACK VariablePtrTrack;\r
72399dae 2206 UINTN VarNameSize;\r
2207 EFI_STATUS Status;\r
0f7aff72 2208 VARIABLE_STORE_HEADER *VariableStoreHeader[VariableStoreTypeMax];\r
72399dae 2209\r
2210 if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {\r
2211 return EFI_INVALID_PARAMETER;\r
2212 }\r
2213\r
2214 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2215\r
9622df63 2216 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
72399dae 2217 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
2218 goto Done;\r
2219 }\r
2220\r
2221 if (VariableName[0] != 0) {\r
2222 //\r
8a2d4996 2223 // If variable name is not NULL, get next variable.\r
72399dae 2224 //\r
2225 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
2226 }\r
2227\r
0f7aff72
RN
2228 //\r
2229 // 0: Volatile, 1: HOB, 2: Non-Volatile.\r
2230 // The index and attributes mapping must be kept in this order as FindVariable\r
2231 // makes use of this mapping to implement search algorithm.\r
2232 //\r
2233 VariableStoreHeader[VariableStoreTypeVolatile] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;\r
2234 VariableStoreHeader[VariableStoreTypeHob] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;\r
2235 VariableStoreHeader[VariableStoreTypeNv] = mNvVariableCache;\r
2236\r
72399dae 2237 while (TRUE) {\r
2238 //\r
0f7aff72 2239 // Switch from Volatile to HOB, to Non-Volatile.\r
72399dae 2240 //\r
0f7aff72
RN
2241 while ((Variable.CurrPtr >= Variable.EndPtr) ||\r
2242 (Variable.CurrPtr == NULL) ||\r
2243 !IsValidVariableHeader (Variable.CurrPtr)\r
2244 ) {\r
2245 //\r
2246 // Find current storage index\r
2247 //\r
2248 for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {\r
2249 if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr == GetStartPointer (VariableStoreHeader[Type]))) {\r
2250 break;\r
2251 }\r
2252 }\r
2253 ASSERT (Type < VariableStoreTypeMax);\r
2254 //\r
2255 // Switch to next storage\r
2256 //\r
2257 for (Type++; Type < VariableStoreTypeMax; Type++) {\r
2258 if (VariableStoreHeader[Type] != NULL) {\r
2259 break;\r
2260 }\r
2261 }\r
2262 //\r
2263 // Capture the case that \r
2264 // 1. current storage is the last one, or\r
2265 // 2. no further storage\r
2266 //\r
2267 if (Type == VariableStoreTypeMax) {\r
72399dae 2268 Status = EFI_NOT_FOUND;\r
2269 goto Done;\r
2270 }\r
0f7aff72
RN
2271 Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);\r
2272 Variable.EndPtr = GetEndPointer (VariableStoreHeader[Type]);\r
2273 Variable.CurrPtr = Variable.StartPtr;\r
72399dae 2274 }\r
0f7aff72 2275\r
72399dae 2276 //\r
2277 // Variable is found\r
2278 //\r
23b06935
SZ
2279 if (Variable.CurrPtr->State == VAR_ADDED || Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
2280 if (!AtRuntime () || ((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {\r
2281 if (Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
2282 //\r
2283 // If it is a IN_DELETED_TRANSITION variable,\r
2284 // and there is also a same ADDED one at the same time,\r
2285 // don't return it.\r
2286 //\r
2287 VariablePtrTrack.StartPtr = Variable.StartPtr;\r
2288 VariablePtrTrack.EndPtr = Variable.EndPtr;\r
2289 Status = FindVariableEx (\r
2290 GetVariableNamePtr (Variable.CurrPtr),\r
2291 &Variable.CurrPtr->VendorGuid,\r
2292 FALSE,\r
2293 &VariablePtrTrack\r
2294 );\r
2295 if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State == VAR_ADDED) {\r
2296 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
2297 continue;\r
2298 }\r
2299 }\r
0f7aff72
RN
2300\r
2301 //\r
2302 // Don't return NV variable when HOB overrides it\r
2303 //\r
2304 if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) && (VariableStoreHeader[VariableStoreTypeNv] != NULL) && \r
2305 (Variable.StartPtr == GetStartPointer (VariableStoreHeader[VariableStoreTypeNv]))\r
2306 ) {\r
2307 VariableInHob.StartPtr = GetStartPointer (VariableStoreHeader[VariableStoreTypeHob]);\r
2308 VariableInHob.EndPtr = GetEndPointer (VariableStoreHeader[VariableStoreTypeHob]);\r
2309 Status = FindVariableEx (\r
2310 GetVariableNamePtr (Variable.CurrPtr),\r
2311 &Variable.CurrPtr->VendorGuid,\r
9622df63 2312 FALSE,\r
0f7aff72
RN
2313 &VariableInHob\r
2314 );\r
2315 if (!EFI_ERROR (Status)) {\r
2316 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
2317 continue;\r
2318 }\r
2319 }\r
2320\r
72399dae 2321 VarNameSize = NameSizeOfVariable (Variable.CurrPtr);\r
2322 ASSERT (VarNameSize != 0);\r
2323\r
2324 if (VarNameSize <= *VariableNameSize) {\r
0f7aff72
RN
2325 CopyMem (VariableName, GetVariableNamePtr (Variable.CurrPtr), VarNameSize);\r
2326 CopyMem (VendorGuid, &Variable.CurrPtr->VendorGuid, sizeof (EFI_GUID));\r
72399dae 2327 Status = EFI_SUCCESS;\r
2328 } else {\r
2329 Status = EFI_BUFFER_TOO_SMALL;\r
2330 }\r
2331\r
2332 *VariableNameSize = VarNameSize;\r
2333 goto Done;\r
2334 }\r
2335 }\r
2336\r
2337 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
2338 }\r
33a5a666 2339\r
8d3a5c82 2340Done:\r
72399dae 2341 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2342 return Status;\r
2343}\r
2344\r
2345/**\r
2346\r
2347 This code sets variable in storage blocks (Volatile or Non-Volatile).\r
2348\r
8a2d4996 2349 @param VariableName Name of Variable to be found.\r
2350 @param VendorGuid Variable vendor GUID.\r
72399dae 2351 @param Attributes Attribute value of the variable found\r
2352 @param DataSize Size of Data found. If size is less than the\r
2353 data, this value contains the required size.\r
8a2d4996 2354 @param Data Data pointer.\r
72399dae 2355\r
8a2d4996 2356 @return EFI_INVALID_PARAMETER Invalid parameter.\r
2357 @return EFI_SUCCESS Set successfully.\r
2358 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.\r
2359 @return EFI_NOT_FOUND Not found.\r
2360 @return EFI_WRITE_PROTECTED Variable is read-only.\r
72399dae 2361\r
2362**/\r
2363EFI_STATUS\r
2364EFIAPI\r
8a2d4996 2365VariableServiceSetVariable (\r
72399dae 2366 IN CHAR16 *VariableName,\r
2367 IN EFI_GUID *VendorGuid,\r
2368 IN UINT32 Attributes,\r
2369 IN UINTN DataSize,\r
2370 IN VOID *Data\r
2371 )\r
2372{\r
2373 VARIABLE_POINTER_TRACK Variable;\r
2374 EFI_STATUS Status;\r
2375 VARIABLE_HEADER *NextVariable;\r
2376 EFI_PHYSICAL_ADDRESS Point;\r
ff843847
RN
2377 LIST_ENTRY *Link;\r
2378 VARIABLE_ENTRY *Entry;\r
72399dae 2379\r
2380 //\r
8a2d4996 2381 // Check input parameters.\r
72399dae 2382 //\r
2383 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r
2384 return EFI_INVALID_PARAMETER;\r
8a2d4996 2385 } \r
48a0e6bf 2386\r
2387 if (DataSize != 0 && Data == NULL) {\r
2388 return EFI_INVALID_PARAMETER;\r
2389 }\r
2390\r
8e38f18e 2391 //\r
6e67fec0 2392 // Not support authenticated or append variable write yet.\r
8e38f18e 2393 //\r
6e67fec0 2394 if ((Attributes & (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS | EFI_VARIABLE_APPEND_WRITE)) != 0) {\r
8e38f18e 2395 return EFI_INVALID_PARAMETER;\r
2396 }\r
2397\r
72399dae 2398 //\r
8a2d4996 2399 // Make sure if runtime bit is set, boot service bit is set also.\r
72399dae 2400 //\r
2401 if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
2402 return EFI_INVALID_PARAMETER;\r
2403 }\r
2404\r
56251c66 2405 if ((UINTN)(~0) - DataSize < StrSize(VariableName)){\r
2406 //\r
2407 // Prevent whole variable size overflow \r
2408 // \r
2409 return EFI_INVALID_PARAMETER;\r
2410 }\r
2411\r
72399dae 2412 //\r
2413 // The size of the VariableName, including the Unicode Null in bytes plus\r
188e4e84 2414 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)\r
2415 // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.\r
72399dae 2416 //\r
2417 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
56251c66 2418 if ( StrSize (VariableName) + DataSize > PcdGet32 (PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER)) {\r
72399dae 2419 return EFI_INVALID_PARAMETER;\r
2420 }\r
a5f15e30 2421 if (!IsHwErrRecVariable(VariableName, VendorGuid)) {\r
72399dae 2422 return EFI_INVALID_PARAMETER;\r
2423 }\r
2424 } else {\r
2425 //\r
2426 // The size of the VariableName, including the Unicode Null in bytes plus\r
188e4e84 2427 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxVariableSize) bytes.\r
72399dae 2428 //\r
56251c66 2429 if (StrSize (VariableName) + DataSize > PcdGet32 (PcdMaxVariableSize) - sizeof (VARIABLE_HEADER)) {\r
72399dae 2430 return EFI_INVALID_PARAMETER;\r
2431 } \r
021a1af9
SZ
2432 }\r
2433\r
6675a21f
SZ
2434 Status = CheckEfiGlobalVariable (VariableName, VendorGuid, Attributes);\r
2435 if (EFI_ERROR (Status)) {\r
2436 return Status;\r
2437 }\r
2438\r
72399dae 2439 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2440\r
2441 //\r
8a2d4996 2442 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.\r
72399dae 2443 //\r
2444 if (1 < InterlockedIncrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState)) {\r
8a2d4996 2445 Point = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;\r
72399dae 2446 //\r
8a2d4996 2447 // Parse non-volatile variable data and get last variable offset.\r
72399dae 2448 //\r
2449 NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point);\r
2450 while ((NextVariable < GetEndPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point)) \r
2451 && IsValidVariableHeader (NextVariable)) {\r
2452 NextVariable = GetNextVariablePtr (NextVariable);\r
2453 }\r
2454 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) Point;\r
2455 }\r
2456\r
ff843847
RN
2457 if (mEndOfDxe && mEnableLocking) {\r
2458 //\r
2459 // Treat the variables listed in the forbidden variable list as read-only after leaving DXE phase.\r
2460 //\r
2461 for ( Link = GetFirstNode (&mLockedVariableList)\r
2462 ; !IsNull (&mLockedVariableList, Link)\r
2463 ; Link = GetNextNode (&mLockedVariableList, Link)\r
2464 ) {\r
2465 Entry = BASE_CR (Link, VARIABLE_ENTRY, Link);\r
2466 if (CompareGuid (&Entry->Guid, VendorGuid) && (StrCmp (Entry->Name, VariableName) == 0)) {\r
2467 Status = EFI_WRITE_PROTECTED;\r
2468 DEBUG ((EFI_D_INFO, "[Variable]: Changing readonly variable after leaving DXE phase - %g:%s\n", VendorGuid, VariableName));\r
2469 goto Done;\r
2470 }\r
2471 }\r
2472 }\r
2473\r
72399dae 2474 //\r
8a2d4996 2475 // Check whether the input variable is already existed.\r
72399dae 2476 //\r
9622df63
SZ
2477 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, TRUE);\r
2478 if (!EFI_ERROR (Status)) {\r
2479 if (((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) && AtRuntime ()) {\r
ff843847
RN
2480 Status = EFI_WRITE_PROTECTED;\r
2481 goto Done;\r
9622df63 2482 }\r
6e67fec0
SZ
2483 if (Attributes != 0 && Attributes != Variable.CurrPtr->Attributes) {\r
2484 //\r
2485 // If a preexisting variable is rewritten with different attributes, SetVariable() shall not\r
2486 // modify the variable and shall return EFI_INVALID_PARAMETER. Two exceptions to this rule:\r
2487 // 1. No access attributes specified\r
2488 // 2. The only attribute differing is EFI_VARIABLE_APPEND_WRITE\r
2489 //\r
2490 Status = EFI_INVALID_PARAMETER;\r
2491 goto Done;\r
2492 }\r
9622df63 2493 }\r
72399dae 2494\r
2495 //\r
8a2d4996 2496 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.\r
72399dae 2497 //\r
9bc5dabb
SZ
2498 Status = AutoUpdateLangVariable (VariableName, Data, DataSize);\r
2499 if (EFI_ERROR (Status)) {\r
2500 //\r
2501 // The auto update operation failed, directly return to avoid inconsistency between PlatformLang and Lang.\r
2502 //\r
2503 goto Done;\r
2504 }\r
72399dae 2505\r
2506 Status = UpdateVariable (VariableName, VendorGuid, Data, DataSize, Attributes, &Variable);\r
2507\r
ff843847 2508Done:\r
fdb7765f 2509 InterlockedDecrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState);\r
052ad7e1 2510 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
fdb7765f 2511\r
8d3a5c82 2512 return Status;\r
2513}\r
2514\r
7c80e839 2515/**\r
8d3a5c82 2516\r
2517 This code returns information about the EFI variables.\r
2518\r
7c80e839 2519 @param Attributes Attributes bitmask to specify the type of variables\r
2520 on which to return information.\r
2521 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available\r
2522 for the EFI variables associated with the attributes specified.\r
2523 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available\r
2524 for EFI variables associated with the attributes specified.\r
2525 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables\r
2526 associated with the attributes specified.\r
8d3a5c82 2527\r
7c80e839 2528 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.\r
2529 @return EFI_SUCCESS Query successfully.\r
2530 @return EFI_UNSUPPORTED The attribute is not supported on this platform.\r
8d3a5c82 2531\r
7c80e839 2532**/\r
052ad7e1
A
2533EFI_STATUS\r
2534EFIAPI\r
8a2d4996 2535VariableServiceQueryVariableInfo (\r
052ad7e1
A
2536 IN UINT32 Attributes,\r
2537 OUT UINT64 *MaximumVariableStorageSize,\r
2538 OUT UINT64 *RemainingVariableStorageSize,\r
2539 OUT UINT64 *MaximumVariableSize\r
2540 )\r
8d3a5c82 2541{\r
2542 VARIABLE_HEADER *Variable;\r
2543 VARIABLE_HEADER *NextVariable;\r
2544 UINT64 VariableSize;\r
2545 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
2fcdca1d 2546 UINT64 CommonVariableTotalSize;\r
2547 UINT64 HwErrVariableTotalSize;\r
2548\r
2549 CommonVariableTotalSize = 0;\r
2550 HwErrVariableTotalSize = 0;\r
8d3a5c82 2551\r
c6492839 2552 if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {\r
8d3a5c82 2553 return EFI_INVALID_PARAMETER;\r
2554 }\r
2fcdca1d 2555\r
c6492839 2556 if((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == 0) {\r
8d3a5c82 2557 //\r
2558 // Make sure the Attributes combination is supported by the platform.\r
2559 //\r
c6492839 2560 return EFI_UNSUPPORTED; \r
8d3a5c82 2561 } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
2562 //\r
2563 // Make sure if runtime bit is set, boot service bit is set also.\r
2564 //\r
2565 return EFI_INVALID_PARAMETER;\r
8a2d4996 2566 } else if (AtRuntime () && ((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0)) {\r
8d3a5c82 2567 //\r
2568 // Make sure RT Attribute is set if we are in Runtime phase.\r
2569 //\r
2570 return EFI_INVALID_PARAMETER;\r
2fcdca1d 2571 } else if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2572 //\r
2573 // Make sure Hw Attribute is set with NV.\r
2574 //\r
2575 return EFI_INVALID_PARAMETER;\r
8e38f18e 2576 } else if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {\r
2577 //\r
2578 // Not support authentiated variable write yet.\r
2579 //\r
2580 return EFI_UNSUPPORTED;\r
8d3a5c82 2581 }\r
2582\r
052ad7e1 2583 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
8d3a5c82 2584\r
2585 if((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r
2586 //\r
2587 // Query is Volatile related.\r
2588 //\r
052ad7e1 2589 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r
8d3a5c82 2590 } else {\r
2591 //\r
2592 // Query is Non-Volatile related.\r
2593 //\r
8a2d4996 2594 VariableStoreHeader = mNvVariableCache;\r
8d3a5c82 2595 }\r
2596\r
2597 //\r
2598 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize\r
2599 // with the storage size (excluding the storage header size).\r
2600 //\r
2601 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);\r
c6492839 2602\r
2603 //\r
2604 // Harware error record variable needs larger size.\r
2605 //\r
2fcdca1d 2606 if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
188e4e84 2607 *MaximumVariableStorageSize = PcdGet32 (PcdHwErrStorageSize);\r
2608 *MaximumVariableSize = PcdGet32 (PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER);\r
2fcdca1d 2609 } else {\r
2610 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
188e4e84 2611 ASSERT (PcdGet32 (PcdHwErrStorageSize) < VariableStoreHeader->Size);\r
2612 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize);\r
2fcdca1d 2613 }\r
2614\r
2615 //\r
188e4e84 2616 // Let *MaximumVariableSize be PcdGet32 (PcdMaxVariableSize) with the exception of the variable header size.\r
2fcdca1d 2617 //\r
188e4e84 2618 *MaximumVariableSize = PcdGet32 (PcdMaxVariableSize) - sizeof (VARIABLE_HEADER);\r
c6492839 2619 }\r
8d3a5c82 2620\r
2621 //\r
2622 // Point to the starting address of the variables.\r
2623 //\r
9cad030b 2624 Variable = GetStartPointer (VariableStoreHeader);\r
8d3a5c82 2625\r
2626 //\r
2627 // Now walk through the related variable store.\r
2628 //\r
6f90dfbc 2629 while ((Variable < GetEndPointer (VariableStoreHeader)) && IsValidVariableHeader (Variable)) {\r
8d3a5c82 2630 NextVariable = GetNextVariablePtr (Variable);\r
2631 VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;\r
2632\r
8a2d4996 2633 if (AtRuntime ()) {\r
8d3a5c82 2634 //\r
8a2d4996 2635 // We don't take the state of the variables in mind\r
8d3a5c82 2636 // when calculating RemainingVariableStorageSize,\r
2637 // since the space occupied by variables not marked with\r
2638 // VAR_ADDED is not allowed to be reclaimed in Runtime.\r
2639 //\r
3b425367 2640 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2fcdca1d 2641 HwErrVariableTotalSize += VariableSize;\r
2642 } else {\r
2643 CommonVariableTotalSize += VariableSize;\r
2644 }\r
8d3a5c82 2645 } else {\r
2646 //\r
8a2d4996 2647 // Only care about Variables with State VAR_ADDED, because\r
8d3a5c82 2648 // the space not marked as VAR_ADDED is reclaimable now.\r
2649 //\r
2650 if (Variable->State == VAR_ADDED) {\r
3b425367 2651 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2fcdca1d 2652 HwErrVariableTotalSize += VariableSize;\r
2653 } else {\r
2654 CommonVariableTotalSize += VariableSize;\r
2655 }\r
8d3a5c82 2656 }\r
2657 }\r
2658\r
2659 //\r
8a2d4996 2660 // Go to the next one.\r
8d3a5c82 2661 //\r
2662 Variable = NextVariable;\r
2663 }\r
2664\r
2fcdca1d 2665 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD){\r