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