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