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