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