]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
Vlv2TbltDevicePkg: Add VarCheckLib library mapping
[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
31349131
SZ
2181 //\r
2182 // Check if CacheVariable points to the variable in variable HOB.\r
2183 // If yes, let CacheVariable points to the variable in NV variable cache.\r
2184 //\r
2185 if ((CacheVariable->CurrPtr != NULL) &&\r
2186 (mVariableModuleGlobal->VariableGlobal.HobVariableBase != 0) &&\r
2187 (CacheVariable->StartPtr == GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase))\r
2188 ) {\r
2189 CacheVariable->StartPtr = GetStartPointer (mNvVariableCache);\r
2190 CacheVariable->EndPtr = GetEndPointer (mNvVariableCache);\r
2191 CacheVariable->Volatile = FALSE;\r
2192 Status = FindVariableEx (VariableName, VendorGuid, FALSE, CacheVariable);\r
2193 if (CacheVariable->CurrPtr == NULL || EFI_ERROR (Status)) {\r
2194 //\r
2195 // There is no matched variable in NV variable cache.\r
2196 //\r
2197 if ((((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0) && (DataSize == 0)) || (Attributes == 0)) {\r
2198 //\r
2199 // It is to delete variable,\r
2200 // go to delete this variable in variable HOB and\r
2201 // try to flush other variables from HOB to flash.\r
2202 //\r
2203 FlushHobVariableToFlash (VariableName, VendorGuid);\r
2204 return EFI_SUCCESS;\r
2205 }\r
2206 }\r
2207 }\r
2208\r
5456306f 2209 if ((CacheVariable->CurrPtr == NULL) || CacheVariable->Volatile) {\r
8a2d4996 2210 Variable = CacheVariable;\r
2211 } else {\r
8a2d4996 2212 //\r
5456306f 2213 // Update/Delete existing NV variable.\r
8a2d4996 2214 // CacheVariable points to the variable in the memory copy of Flash area\r
2215 // Now let Variable points to the same variable in Flash area.\r
2216 //\r
2217 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);\r
fa0737a8 2218 Variable = &NvVariable;\r
8a2d4996 2219 Variable->StartPtr = GetStartPointer (VariableStoreHeader);\r
2220 Variable->EndPtr = GetEndPointer (VariableStoreHeader);\r
5456306f 2221 Variable->CurrPtr = (VARIABLE_HEADER *)((UINTN)Variable->StartPtr + ((UINTN)CacheVariable->CurrPtr - (UINTN)CacheVariable->StartPtr));\r
23b06935
SZ
2222 if (CacheVariable->InDeletedTransitionPtr != NULL) {\r
2223 Variable->InDeletedTransitionPtr = (VARIABLE_HEADER *)((UINTN)Variable->StartPtr + ((UINTN)CacheVariable->InDeletedTransitionPtr - (UINTN)CacheVariable->StartPtr));\r
2224 } else {\r
2225 Variable->InDeletedTransitionPtr = NULL;\r
2226 }\r
5456306f 2227 Variable->Volatile = FALSE;\r
fa0737a8 2228 }\r
5456306f 2229\r
2230 Fvb = mVariableModuleGlobal->FvbInstance;\r
fdb7765f 2231\r
fa0737a8
SZ
2232 //\r
2233 // Tricky part: Use scratch data area at the end of volatile variable store\r
2234 // as a temporary storage.\r
2235 //\r
2236 NextVariable = GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase));\r
2237 ScratchSize = mVariableModuleGlobal->ScratchBufferSize;\r
2238 SetMem (NextVariable, ScratchSize, 0xff);\r
2239 DataReady = FALSE;\r
2240\r
72399dae 2241 if (Variable->CurrPtr != NULL) {\r
8d3a5c82 2242 //\r
8a2d4996 2243 // Update/Delete existing variable.\r
8d3a5c82 2244 //\r
fa0737a8 2245 if (AtRuntime ()) {\r
c6492839 2246 //\r
fa0737a8
SZ
2247 // If AtRuntime and the variable is Volatile and Runtime Access,\r
2248 // the volatile is ReadOnly, and SetVariable should be aborted and\r
c6492839 2249 // return EFI_WRITE_PROTECTED.\r
2250 //\r
72399dae 2251 if (Variable->Volatile) {\r
c6492839 2252 Status = EFI_WRITE_PROTECTED;\r
2253 goto Done;\r
2254 }\r
2255 //\r
fa0737a8
SZ
2256 // Only variable that have NV attributes can be updated/deleted in Runtime.\r
2257 //\r
2258 if ((Variable->CurrPtr->Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r
2259 Status = EFI_INVALID_PARAMETER;\r
2260 goto Done;\r
2261 }\r
2262\r
2263 //\r
2264 // Only variable that have RT attributes can be updated/deleted in Runtime.\r
c6492839 2265 //\r
fa0737a8 2266 if ((Variable->CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) {\r
c6492839 2267 Status = EFI_INVALID_PARAMETER;\r
fa0737a8 2268 goto Done;\r
c6492839 2269 }\r
2270 }\r
8a2d4996 2271\r
8d3a5c82 2272 //\r
c6492839 2273 // Setting a data variable with no access, or zero DataSize attributes\r
8a2d4996 2274 // causes it to be deleted.\r
fa0737a8
SZ
2275 // When the EFI_VARIABLE_APPEND_WRITE attribute is set, DataSize of zero will\r
2276 // not delete the variable.\r
8d3a5c82 2277 //\r
fa0737a8 2278 if ((((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0) && (DataSize == 0))|| ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0)) {\r
23b06935
SZ
2279 if (Variable->InDeletedTransitionPtr != NULL) {\r
2280 //\r
2281 // Both ADDED and IN_DELETED_TRANSITION variable are present,\r
2282 // set IN_DELETED_TRANSITION one to DELETED state first.\r
2283 //\r
2284 State = Variable->InDeletedTransitionPtr->State;\r
2285 State &= VAR_DELETED;\r
2286 Status = UpdateVariableStore (\r
2287 &mVariableModuleGlobal->VariableGlobal,\r
2288 Variable->Volatile,\r
2289 FALSE,\r
2290 Fvb,\r
2291 (UINTN) &Variable->InDeletedTransitionPtr->State,\r
2292 sizeof (UINT8),\r
2293 &State\r
2294 );\r
2295 if (!EFI_ERROR (Status)) {\r
2296 if (!Variable->Volatile) {\r
0cc565de 2297 ASSERT (CacheVariable->InDeletedTransitionPtr != NULL);\r
23b06935
SZ
2298 CacheVariable->InDeletedTransitionPtr->State = State;\r
2299 }\r
2300 } else {\r
2301 goto Done;\r
2302 }\r
2303 }\r
2304\r
72399dae 2305 State = Variable->CurrPtr->State;\r
c6492839 2306 State &= VAR_DELETED;\r
2307\r
2308 Status = UpdateVariableStore (\r
052ad7e1 2309 &mVariableModuleGlobal->VariableGlobal,\r
72399dae 2310 Variable->Volatile,\r
c6492839 2311 FALSE,\r
8a9e0b72 2312 Fvb,\r
72399dae 2313 (UINTN) &Variable->CurrPtr->State,\r
c6492839 2314 sizeof (UINT8),\r
2315 &State\r
fa0737a8 2316 );\r
33a5a666 2317 if (!EFI_ERROR (Status)) {\r
8a2d4996 2318 UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, FALSE, TRUE, FALSE);\r
2319 if (!Variable->Volatile) {\r
2320 CacheVariable->CurrPtr->State = State;\r
335e2681 2321 FlushHobVariableToFlash (VariableName, VendorGuid);\r
8a2d4996 2322 }\r
33a5a666 2323 }\r
fa0737a8 2324 goto Done;\r
c6492839 2325 }\r
8d3a5c82 2326 //\r
8a2d4996 2327 // If the variable is marked valid, and the same data has been passed in,\r
c6492839 2328 // then return to the caller immediately.\r
8d3a5c82 2329 //\r
72399dae 2330 if (DataSizeOfVariable (Variable->CurrPtr) == DataSize &&\r
fa0737a8
SZ
2331 (CompareMem (Data, GetVariableDataPtr (Variable->CurrPtr), DataSize) == 0) &&\r
2332 ((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0) &&\r
2333 (TimeStamp == NULL)) {\r
2334 //\r
2335 // Variable content unchanged and no need to update timestamp, just return.\r
2336 //\r
8a2d4996 2337 UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, TRUE, FALSE, FALSE);\r
c6492839 2338 Status = EFI_SUCCESS;\r
2339 goto Done;\r
72399dae 2340 } else if ((Variable->CurrPtr->State == VAR_ADDED) ||\r
2341 (Variable->CurrPtr->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {\r
814bae52 2342\r
fa0737a8
SZ
2343 //\r
2344 // EFI_VARIABLE_APPEND_WRITE attribute only effects for existing variable.\r
2345 //\r
2346 if ((Attributes & EFI_VARIABLE_APPEND_WRITE) != 0) {\r
2347 //\r
2348 // NOTE: From 0 to DataOffset of NextVariable is reserved for Variable Header and Name.\r
2349 // From DataOffset of NextVariable is to save the existing variable data.\r
2350 //\r
2351 DataOffset = GetVariableDataOffset (Variable->CurrPtr);\r
2352 BufferForMerge = (UINT8 *) ((UINTN) NextVariable + DataOffset);\r
2353 CopyMem (BufferForMerge, (UINT8 *) ((UINTN) Variable->CurrPtr + DataOffset), DataSizeOfVariable (Variable->CurrPtr));\r
2354\r
2355 //\r
2356 // Set Max Common/Auth Variable Data Size as default MaxDataSize.\r
2357 //\r
2358 if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {\r
2359 MaxDataSize = mVariableModuleGlobal->MaxAuthVariableSize - DataOffset;\r
2360 } else {\r
2361 MaxDataSize = mVariableModuleGlobal->MaxVariableSize - DataOffset;\r
2362 }\r
2363\r
2364 //\r
2365 // Append the new data to the end of existing data.\r
2366 // Max Harware error record variable data size is different from common/auth variable.\r
2367 //\r
2368 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2369 MaxDataSize = PcdGet32 (PcdMaxHardwareErrorVariableSize) - DataOffset;\r
2370 }\r
2371\r
2372 if (DataSizeOfVariable (Variable->CurrPtr) + DataSize > MaxDataSize) {\r
2373 //\r
2374 // Existing data size + new data size exceed maximum variable size limitation.\r
2375 //\r
2376 Status = EFI_INVALID_PARAMETER;\r
2377 goto Done;\r
2378 }\r
2379 CopyMem ((UINT8*) ((UINTN) BufferForMerge + DataSizeOfVariable (Variable->CurrPtr)), Data, DataSize);\r
2380 MergedBufSize = DataSizeOfVariable (Variable->CurrPtr) + DataSize;\r
2381\r
2382 //\r
2383 // BufferForMerge(from DataOffset of NextVariable) has included the merged existing and new data.\r
2384 //\r
2385 Data = BufferForMerge;\r
2386 DataSize = MergedBufSize;\r
2387 DataReady = TRUE;\r
2388 }\r
2389\r
c6492839 2390 //\r
8a2d4996 2391 // Mark the old variable as in delete transition.\r
c6492839 2392 //\r
72399dae 2393 State = Variable->CurrPtr->State;\r
c6492839 2394 State &= VAR_IN_DELETED_TRANSITION;\r
2395\r
2396 Status = UpdateVariableStore (\r
052ad7e1 2397 &mVariableModuleGlobal->VariableGlobal,\r
72399dae 2398 Variable->Volatile,\r
c6492839 2399 FALSE,\r
8a9e0b72 2400 Fvb,\r
72399dae 2401 (UINTN) &Variable->CurrPtr->State,\r
c6492839 2402 sizeof (UINT8),\r
2403 &State\r
fa0737a8 2404 );\r
c6492839 2405 if (EFI_ERROR (Status)) {\r
fa0737a8
SZ
2406 goto Done;\r
2407 }\r
8a2d4996 2408 if (!Variable->Volatile) {\r
2409 CacheVariable->CurrPtr->State = State;\r
2410 }\r
fa0737a8 2411 }\r
72399dae 2412 } else {\r
8d3a5c82 2413 //\r
8a2d4996 2414 // Not found existing variable. Create a new variable.\r
fa0737a8
SZ
2415 //\r
2416\r
2417 if ((DataSize == 0) && ((Attributes & EFI_VARIABLE_APPEND_WRITE) != 0)) {\r
2418 Status = EFI_SUCCESS;\r
2419 goto Done;\r
2420 }\r
2421\r
8d3a5c82 2422 //\r
c6492839 2423 // Make sure we are trying to create a new variable.\r
fa0737a8 2424 // Setting a data variable with zero DataSize or no access attributes means to delete it.\r
8d3a5c82 2425 //\r
c6492839 2426 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {\r
2427 Status = EFI_NOT_FOUND;\r
2428 goto Done;\r
2429 }\r
fa0737a8 2430\r
8d3a5c82 2431 //\r
8a2d4996 2432 // Only variable have NV|RT attribute can be created in Runtime.\r
c6492839 2433 //\r
8a2d4996 2434 if (AtRuntime () &&\r
45f6c85b 2435 (((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) || ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0))) {\r
c6492839 2436 Status = EFI_INVALID_PARAMETER;\r
2437 goto Done;\r
fa0737a8 2438 }\r
c6492839 2439 }\r
2440\r
2441 //\r
2442 // Function part - create a new variable and copy the data.\r
2443 // Both update a variable and create a variable will come here.\r
c6492839 2444 //\r
c6492839 2445 NextVariable->StartId = VARIABLE_DATA;\r
c6492839 2446 //\r
2447 // NextVariable->State = VAR_ADDED;\r
2448 //\r
8a2d4996 2449 NextVariable->Reserved = 0;\r
fa0737a8
SZ
2450 if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {\r
2451 AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) NextVariable;\r
2452 AuthVariable->PubKeyIndex = KeyIndex;\r
2453 AuthVariable->MonotonicCount = MonotonicCount;\r
2454 ZeroMem (&AuthVariable->TimeStamp, sizeof (EFI_TIME));\r
2455\r
2456 if (((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) != 0) &&\r
2457 (TimeStamp != NULL)) {\r
2458 if ((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0) {\r
2459 CopyMem (&AuthVariable->TimeStamp, TimeStamp, sizeof (EFI_TIME));\r
2460 } else {\r
2461 //\r
2462 // In the case when the EFI_VARIABLE_APPEND_WRITE attribute is set, only\r
2463 // when the new TimeStamp value is later than the current timestamp associated\r
2464 // with the variable, we need associate the new timestamp with the updated value.\r
2465 //\r
2466 if (Variable->CurrPtr != NULL) {\r
2467 if (VariableCompareTimeStampInternal (&(((AUTHENTICATED_VARIABLE_HEADER *) Variable->CurrPtr)->TimeStamp), TimeStamp)) {\r
2468 CopyMem (&AuthVariable->TimeStamp, TimeStamp, sizeof (EFI_TIME));\r
2469 }\r
2470 }\r
2471 }\r
2472 }\r
2473 }\r
2474\r
2475 //\r
2476 // The EFI_VARIABLE_APPEND_WRITE attribute will never be set in the returned\r
2477 // Attributes bitmask parameter of a GetVariable() call.\r
2478 //\r
2479 NextVariable->Attributes = Attributes & (~EFI_VARIABLE_APPEND_WRITE);\r
2480\r
2481 VarNameOffset = GetVariableHeaderSize ();\r
8a2d4996 2482 VarNameSize = StrSize (VariableName);\r
c6492839 2483 CopyMem (\r
2484 (UINT8 *) ((UINTN) NextVariable + VarNameOffset),\r
2485 VariableName,\r
2486 VarNameSize\r
2487 );\r
2488 VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);\r
fa0737a8
SZ
2489\r
2490 //\r
2491 // If DataReady is TRUE, it means the variable data has been saved into\r
2492 // NextVariable during EFI_VARIABLE_APPEND_WRITE operation preparation.\r
2493 //\r
2494 if (!DataReady) {\r
2495 CopyMem (\r
2496 (UINT8 *) ((UINTN) NextVariable + VarDataOffset),\r
2497 Data,\r
2498 DataSize\r
2499 );\r
2500 }\r
2501\r
2502 CopyMem (GetVendorGuidPtr (NextVariable), VendorGuid, sizeof (EFI_GUID));\r
c6492839 2503 //\r
2504 // There will be pad bytes after Data, the NextVariable->NameSize and\r
2505 // NextVariable->DataSize should not include pad size so that variable\r
8a2d4996 2506 // service can get actual size in GetVariable.\r
c6492839 2507 //\r
fa0737a8
SZ
2508 SetNameSizeOfVariable (NextVariable, VarNameSize);\r
2509 SetDataSizeOfVariable (NextVariable, DataSize);\r
c6492839 2510\r
2511 //\r
2512 // The actual size of the variable that stores in storage should\r
2513 // include pad size.\r
2514 //\r
2515 VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);\r
45f6c85b 2516 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
8d3a5c82 2517 //\r
8a2d4996 2518 // Create a nonvolatile variable.\r
8d3a5c82 2519 //\r
fd51bf70 2520 Volatile = FALSE;\r
4edb1866
SZ
2521\r
2522 IsCommonVariable = FALSE;\r
2523 IsCommonUserVariable = FALSE;\r
2524 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0) {\r
2525 IsCommonVariable = TRUE;\r
2526 IsCommonUserVariable = IsUserVariable (NextVariable);\r
2527 }\r
2528 if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0)\r
188e4e84 2529 && ((VarSize + mVariableModuleGlobal->HwErrVariableTotalSize) > PcdGet32 (PcdHwErrStorageSize)))\r
4edb1866
SZ
2530 || (IsCommonVariable && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > mVariableModuleGlobal->CommonVariableSpace))\r
2531 || (IsCommonVariable && AtRuntime () && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > mVariableModuleGlobal->CommonRuntimeVariableSpace))\r
2532 || (IsCommonUserVariable && ((VarSize + mVariableModuleGlobal->CommonUserVariableTotalSize) > mVariableModuleGlobal->CommonMaxUserVariableSpace))) {\r
8a2d4996 2533 if (AtRuntime ()) {\r
4edb1866
SZ
2534 if (IsCommonUserVariable && ((VarSize + mVariableModuleGlobal->CommonUserVariableTotalSize) > mVariableModuleGlobal->CommonMaxUserVariableSpace)) {\r
2535 RecordVarErrorFlag (VAR_ERROR_FLAG_USER_ERROR, VariableName, VendorGuid, Attributes, VarSize);\r
2536 }\r
2537 if (IsCommonVariable && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > mVariableModuleGlobal->CommonRuntimeVariableSpace)) {\r
2538 RecordVarErrorFlag (VAR_ERROR_FLAG_SYSTEM_ERROR, VariableName, VendorGuid, Attributes, VarSize);\r
2539 }\r
c6492839 2540 Status = EFI_OUT_OF_RESOURCES;\r
2541 goto Done;\r
2542 }\r
2543 //\r
7baf3c69 2544 // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.\r
c6492839 2545 //\r
fa0737a8
SZ
2546 Status = Reclaim (\r
2547 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
2548 &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
2549 FALSE,\r
2550 Variable,\r
2551 NextVariable,\r
2552 HEADER_ALIGN (VarSize)\r
2553 );\r
7baf3c69
SZ
2554 if (!EFI_ERROR (Status)) {\r
2555 //\r
2556 // The new variable has been integrated successfully during reclaiming.\r
2557 //\r
2558 if (Variable->CurrPtr != NULL) {\r
2559 CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN) CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr));\r
2560 CacheVariable->InDeletedTransitionPtr = NULL;\r
2561 }\r
2562 UpdateVariableInfo (VariableName, VendorGuid, FALSE, FALSE, TRUE, FALSE, FALSE);\r
2563 FlushHobVariableToFlash (VariableName, VendorGuid);\r
4edb1866
SZ
2564 } else {\r
2565 if (IsCommonUserVariable && ((VarSize + mVariableModuleGlobal->CommonUserVariableTotalSize) > mVariableModuleGlobal->CommonMaxUserVariableSpace)) {\r
2566 RecordVarErrorFlag (VAR_ERROR_FLAG_USER_ERROR, VariableName, VendorGuid, Attributes, VarSize);\r
2567 }\r
2568 if (IsCommonVariable && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > mVariableModuleGlobal->CommonVariableSpace)) {\r
2569 RecordVarErrorFlag (VAR_ERROR_FLAG_SYSTEM_ERROR, VariableName, VendorGuid, Attributes, VarSize);\r
2570 }\r
23b06935 2571 }\r
7baf3c69 2572 goto Done;\r
8d3a5c82 2573 }\r
2574 //\r
8a2d4996 2575 // Four steps\r
c6492839 2576 // 1. Write variable header\r
fa0737a8 2577 // 2. Set variable state to header valid\r
130e2569 2578 // 3. Write variable data\r
2579 // 4. Set variable state to valid\r
8d3a5c82 2580 //\r
8d3a5c82 2581 //\r
c6492839 2582 // Step 1:\r
8d3a5c82 2583 //\r
8a2d4996 2584 CacheOffset = mVariableModuleGlobal->NonVolatileLastVariableOffset;\r
c6492839 2585 Status = UpdateVariableStore (\r
052ad7e1 2586 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 2587 FALSE,\r
2588 TRUE,\r
8a9e0b72 2589 Fvb,\r
72399dae 2590 mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
fa0737a8 2591 (UINT32) GetVariableHeaderSize (),\r
c6492839 2592 (UINT8 *) NextVariable\r
2593 );\r
2594\r
2595 if (EFI_ERROR (Status)) {\r
2596 goto Done;\r
2597 }\r
130e2569 2598\r
8d3a5c82 2599 //\r
c6492839 2600 // Step 2:\r
8d3a5c82 2601 //\r
130e2569 2602 NextVariable->State = VAR_HEADER_VALID_ONLY;\r
2603 Status = UpdateVariableStore (\r
2604 &mVariableModuleGlobal->VariableGlobal,\r
2605 FALSE,\r
2606 TRUE,\r
8a9e0b72 2607 Fvb,\r
8a2d4996 2608 mVariableModuleGlobal->NonVolatileLastVariableOffset + OFFSET_OF (VARIABLE_HEADER, State),\r
2609 sizeof (UINT8),\r
2610 &NextVariable->State\r
130e2569 2611 );\r
2612\r
2613 if (EFI_ERROR (Status)) {\r
2614 goto Done;\r
2615 }\r
2616 //\r
2617 // Step 3:\r
2618 //\r
c6492839 2619 Status = UpdateVariableStore (\r
052ad7e1 2620 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 2621 FALSE,\r
2622 TRUE,\r
8a9e0b72 2623 Fvb,\r
fa0737a8
SZ
2624 mVariableModuleGlobal->NonVolatileLastVariableOffset + GetVariableHeaderSize (),\r
2625 (UINT32) (VarSize - GetVariableHeaderSize ()),\r
2626 (UINT8 *) NextVariable + GetVariableHeaderSize ()\r
c6492839 2627 );\r
2628\r
2629 if (EFI_ERROR (Status)) {\r
2630 goto Done;\r
2631 }\r
8d3a5c82 2632 //\r
130e2569 2633 // Step 4:\r
8d3a5c82 2634 //\r
c6492839 2635 NextVariable->State = VAR_ADDED;\r
2636 Status = UpdateVariableStore (\r
052ad7e1 2637 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 2638 FALSE,\r
2639 TRUE,\r
8a9e0b72 2640 Fvb,\r
8a2d4996 2641 mVariableModuleGlobal->NonVolatileLastVariableOffset + OFFSET_OF (VARIABLE_HEADER, State),\r
2642 sizeof (UINT8),\r
2643 &NextVariable->State\r
c6492839 2644 );\r
2645\r
2646 if (EFI_ERROR (Status)) {\r
2647 goto Done;\r
2648 }\r
8d3a5c82 2649\r
72399dae 2650 mVariableModuleGlobal->NonVolatileLastVariableOffset += HEADER_ALIGN (VarSize);\r
8d3a5c82 2651\r
2fcdca1d 2652 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {\r
2653 mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VarSize);\r
2654 } else {\r
2655 mVariableModuleGlobal->CommonVariableTotalSize += HEADER_ALIGN (VarSize);\r
4edb1866
SZ
2656 if (IsCommonUserVariable) {\r
2657 mVariableModuleGlobal->CommonUserVariableTotalSize += HEADER_ALIGN (VarSize);\r
2658 }\r
2fcdca1d 2659 }\r
8a2d4996 2660 //\r
2661 // update the memory copy of Flash region.\r
2662 //\r
2663 CopyMem ((UINT8 *)mNvVariableCache + CacheOffset, (UINT8 *)NextVariable, VarSize);\r
c6492839 2664 } else {\r
2665 //\r
8a2d4996 2666 // Create a volatile variable.\r
fa0737a8 2667 //\r
fd51bf70 2668 Volatile = TRUE;\r
c6492839 2669\r
72399dae 2670 if ((UINT32) (VarSize + mVariableModuleGlobal->VolatileLastVariableOffset) >\r
052ad7e1 2671 ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size) {\r
8d3a5c82 2672 //\r
7baf3c69 2673 // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.\r
8d3a5c82 2674 //\r
fa0737a8
SZ
2675 Status = Reclaim (\r
2676 mVariableModuleGlobal->VariableGlobal.VolatileVariableBase,\r
2677 &mVariableModuleGlobal->VolatileLastVariableOffset,\r
2678 TRUE,\r
2679 Variable,\r
2680 NextVariable,\r
2681 HEADER_ALIGN (VarSize)\r
2682 );\r
7baf3c69
SZ
2683 if (!EFI_ERROR (Status)) {\r
2684 //\r
2685 // The new variable has been integrated successfully during reclaiming.\r
2686 //\r
2687 if (Variable->CurrPtr != NULL) {\r
2688 CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN) CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr));\r
2689 CacheVariable->InDeletedTransitionPtr = NULL;\r
2690 }\r
2691 UpdateVariableInfo (VariableName, VendorGuid, TRUE, FALSE, TRUE, FALSE, FALSE);\r
23b06935 2692 }\r
7baf3c69 2693 goto Done;\r
8d3a5c82 2694 }\r
8d3a5c82 2695\r
c6492839 2696 NextVariable->State = VAR_ADDED;\r
2697 Status = UpdateVariableStore (\r
052ad7e1 2698 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 2699 TRUE,\r
2700 TRUE,\r
8a9e0b72 2701 Fvb,\r
72399dae 2702 mVariableModuleGlobal->VolatileLastVariableOffset,\r
c6492839 2703 (UINT32) VarSize,\r
2704 (UINT8 *) NextVariable\r
2705 );\r
2706\r
2707 if (EFI_ERROR (Status)) {\r
2708 goto Done;\r
8d3a5c82 2709 }\r
c6492839 2710\r
72399dae 2711 mVariableModuleGlobal->VolatileLastVariableOffset += HEADER_ALIGN (VarSize);\r
c6492839 2712 }\r
72399dae 2713\r
c6492839 2714 //\r
8a2d4996 2715 // Mark the old variable as deleted.\r
c6492839 2716 //\r
23b06935
SZ
2717 if (!EFI_ERROR (Status) && Variable->CurrPtr != NULL) {\r
2718 if (Variable->InDeletedTransitionPtr != NULL) {\r
2719 //\r
2720 // Both ADDED and IN_DELETED_TRANSITION old variable are present,\r
2721 // set IN_DELETED_TRANSITION one to DELETED state first.\r
2722 //\r
2723 State = Variable->InDeletedTransitionPtr->State;\r
2724 State &= VAR_DELETED;\r
2725 Status = UpdateVariableStore (\r
2726 &mVariableModuleGlobal->VariableGlobal,\r
2727 Variable->Volatile,\r
2728 FALSE,\r
2729 Fvb,\r
2730 (UINTN) &Variable->InDeletedTransitionPtr->State,\r
2731 sizeof (UINT8),\r
2732 &State\r
2733 );\r
2734 if (!EFI_ERROR (Status)) {\r
2735 if (!Variable->Volatile) {\r
0cc565de 2736 ASSERT (CacheVariable->InDeletedTransitionPtr != NULL);\r
23b06935
SZ
2737 CacheVariable->InDeletedTransitionPtr->State = State;\r
2738 }\r
2739 } else {\r
2740 goto Done;\r
2741 }\r
2742 }\r
2743\r
72399dae 2744 State = Variable->CurrPtr->State;\r
c6492839 2745 State &= VAR_DELETED;\r
2746\r
2747 Status = UpdateVariableStore (\r
72399dae 2748 &mVariableModuleGlobal->VariableGlobal,\r
2749 Variable->Volatile,\r
2750 FALSE,\r
2751 Fvb,\r
2752 (UINTN) &Variable->CurrPtr->State,\r
2753 sizeof (UINT8),\r
2754 &State\r
2755 );\r
fa0737a8 2756 if (!EFI_ERROR (Status) && !Variable->Volatile) {\r
8a2d4996 2757 CacheVariable->CurrPtr->State = State;\r
2758 }\r
72399dae 2759 }\r
2760\r
2761 if (!EFI_ERROR (Status)) {\r
2762 UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);\r
335e2681
SZ
2763 if (!Volatile) {\r
2764 FlushHobVariableToFlash (VariableName, VendorGuid);\r
2765 }\r
72399dae 2766 }\r
2767\r
2768Done:\r
2769 return Status;\r
2770}\r
2771\r
a5f15e30
SZ
2772/**\r
2773 Check if a Unicode character is a hexadecimal character.\r
2774\r
fa0737a8
SZ
2775 This function checks if a Unicode character is a\r
2776 hexadecimal character. The valid hexadecimal character is\r
a5f15e30
SZ
2777 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.\r
2778\r
2779\r
2780 @param Char The character to check against.\r
2781\r
2782 @retval TRUE If the Char is a hexadecmial character.\r
2783 @retval FALSE If the Char is not a hexadecmial character.\r
2784\r
2785**/\r
2786BOOLEAN\r
2787EFIAPI\r
2788IsHexaDecimalDigitCharacter (\r
2789 IN CHAR16 Char\r
2790 )\r
2791{\r
2792 return (BOOLEAN) ((Char >= L'0' && Char <= L'9') || (Char >= L'A' && Char <= L'F') || (Char >= L'a' && Char <= L'f'));\r
2793}\r
2794\r
2795/**\r
2796\r
2797 This code checks if variable is hardware error record variable or not.\r
2798\r
2799 According to UEFI spec, hardware error record variable should use the EFI_HARDWARE_ERROR_VARIABLE VendorGuid\r
2800 and have the L"HwErrRec####" name convention, #### is a printed hex value and no 0x or h is included in the hex value.\r
2801\r
2802 @param VariableName Pointer to variable name.\r
2803 @param VendorGuid Variable Vendor Guid.\r
2804\r
2805 @retval TRUE Variable is hardware error record variable.\r
2806 @retval FALSE Variable is not hardware error record variable.\r
2807\r
2808**/\r
2809BOOLEAN\r
2810EFIAPI\r
2811IsHwErrRecVariable (\r
2812 IN CHAR16 *VariableName,\r
2813 IN EFI_GUID *VendorGuid\r
2814 )\r
2815{\r
2816 if (!CompareGuid (VendorGuid, &gEfiHardwareErrorVariableGuid) ||\r
2817 (StrLen (VariableName) != StrLen (L"HwErrRec####")) ||\r
2818 (StrnCmp(VariableName, L"HwErrRec", StrLen (L"HwErrRec")) != 0) ||\r
2819 !IsHexaDecimalDigitCharacter (VariableName[0x8]) ||\r
2820 !IsHexaDecimalDigitCharacter (VariableName[0x9]) ||\r
2821 !IsHexaDecimalDigitCharacter (VariableName[0xA]) ||\r
2822 !IsHexaDecimalDigitCharacter (VariableName[0xB])) {\r
2823 return FALSE;\r
2824 }\r
2825\r
2826 return TRUE;\r
2827}\r
2828\r
ff843847
RN
2829/**\r
2830 Mark a variable that will become read-only after leaving the DXE phase of execution.\r
2831\r
fa0737a8 2832\r
ff843847
RN
2833 @param[in] This The VARIABLE_LOCK_PROTOCOL instance.\r
2834 @param[in] VariableName A pointer to the variable name that will be made read-only subsequently.\r
2835 @param[in] VendorGuid A pointer to the vendor GUID that will be made read-only subsequently.\r
2836\r
2837 @retval EFI_SUCCESS The variable specified by the VariableName and the VendorGuid was marked\r
2838 as pending to be read-only.\r
2839 @retval EFI_INVALID_PARAMETER VariableName or VendorGuid is NULL.\r
2840 Or VariableName is an empty string.\r
2841 @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has\r
2842 already been signaled.\r
2843 @retval EFI_OUT_OF_RESOURCES There is not enough resource to hold the lock request.\r
2844**/\r
2845EFI_STATUS\r
2846EFIAPI\r
2847VariableLockRequestToLock (\r
2848 IN CONST EDKII_VARIABLE_LOCK_PROTOCOL *This,\r
2849 IN CHAR16 *VariableName,\r
2850 IN EFI_GUID *VendorGuid\r
2851 )\r
2852{\r
2853 VARIABLE_ENTRY *Entry;\r
efb01a10 2854 CHAR16 *Name;\r
e56e1870
SZ
2855 LIST_ENTRY *Link;\r
2856 VARIABLE_ENTRY *LockedEntry;\r
ff843847
RN
2857\r
2858 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r
2859 return EFI_INVALID_PARAMETER;\r
2860 }\r
2861\r
2862 if (mEndOfDxe) {\r
2863 return EFI_ACCESS_DENIED;\r
2864 }\r
2865\r
6e1e5405 2866 Entry = AllocateRuntimeZeroPool (sizeof (*Entry) + StrSize (VariableName));\r
ff843847
RN
2867 if (Entry == NULL) {\r
2868 return EFI_OUT_OF_RESOURCES;\r
2869 }\r
2870\r
2871 DEBUG ((EFI_D_INFO, "[Variable] Lock: %g:%s\n", VendorGuid, VariableName));\r
2872\r
2873 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2874\r
e56e1870
SZ
2875 for ( Link = GetFirstNode (&mLockedVariableList)\r
2876 ; !IsNull (&mLockedVariableList, Link)\r
2877 ; Link = GetNextNode (&mLockedVariableList, Link)\r
2878 ) {\r
2879 LockedEntry = BASE_CR (Link, VARIABLE_ENTRY, Link);\r
2880 Name = (CHAR16 *) ((UINTN) LockedEntry + sizeof (*LockedEntry));\r
2881 if (CompareGuid (&LockedEntry->Guid, VendorGuid) && (StrCmp (Name, VariableName) == 0)) {\r
2882 goto Done;\r
2883 }\r
2884 }\r
2885\r
efb01a10 2886 Name = (CHAR16 *) ((UINTN) Entry + sizeof (*Entry));\r
568a5119 2887 StrCpyS (Name, StrSize (VariableName)/sizeof(CHAR16), VariableName);\r
ff843847
RN
2888 CopyGuid (&Entry->Guid, VendorGuid);\r
2889 InsertTailList (&mLockedVariableList, &Entry->Link);\r
2890\r
e56e1870 2891Done:\r
ff843847
RN
2892 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2893\r
2894 return EFI_SUCCESS;\r
2895}\r
2896\r
72399dae 2897/**\r
2898\r
2899 This code finds variable in storage blocks (Volatile or Non-Volatile).\r
2900\r
18a7dbbc
SZ
2901 Caution: This function may receive untrusted input.\r
2902 This function may be invoked in SMM mode, and datasize is external input.\r
2903 This function will do basic validation, before parse the data.\r
2904\r
72399dae 2905 @param VariableName Name of Variable to be found.\r
2906 @param VendorGuid Variable vendor GUID.\r
2907 @param Attributes Attribute value of the variable found.\r
2908 @param DataSize Size of Data found. If size is less than the\r
2909 data, this value contains the required size.\r
2910 @param Data Data pointer.\r
fa0737a8 2911\r
8a2d4996 2912 @return EFI_INVALID_PARAMETER Invalid parameter.\r
2913 @return EFI_SUCCESS Find the specified variable.\r
2914 @return EFI_NOT_FOUND Not found.\r
2915 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.\r
72399dae 2916\r
2917**/\r
2918EFI_STATUS\r
2919EFIAPI\r
8a2d4996 2920VariableServiceGetVariable (\r
72399dae 2921 IN CHAR16 *VariableName,\r
2922 IN EFI_GUID *VendorGuid,\r
2923 OUT UINT32 *Attributes OPTIONAL,\r
2924 IN OUT UINTN *DataSize,\r
2925 OUT VOID *Data\r
2926 )\r
2927{\r
2928 EFI_STATUS Status;\r
2929 VARIABLE_POINTER_TRACK Variable;\r
2930 UINTN VarDataSize;\r
2931\r
2932 if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {\r
2933 return EFI_INVALID_PARAMETER;\r
2934 }\r
2935\r
2936 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
fa0737a8 2937\r
9622df63 2938 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
72399dae 2939 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
2940 goto Done;\r
2941 }\r
2942\r
2943 //\r
2944 // Get data size\r
2945 //\r
2946 VarDataSize = DataSizeOfVariable (Variable.CurrPtr);\r
2947 ASSERT (VarDataSize != 0);\r
2948\r
2949 if (*DataSize >= VarDataSize) {\r
2950 if (Data == NULL) {\r
2951 Status = EFI_INVALID_PARAMETER;\r
2952 goto Done;\r
33a5a666 2953 }\r
72399dae 2954\r
2955 CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);\r
2956 if (Attributes != NULL) {\r
2957 *Attributes = Variable.CurrPtr->Attributes;\r
2958 }\r
2959\r
2960 *DataSize = VarDataSize;\r
2961 UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, TRUE, FALSE, FALSE, FALSE);\r
fa0737a8 2962\r
72399dae 2963 Status = EFI_SUCCESS;\r
2964 goto Done;\r
2965 } else {\r
2966 *DataSize = VarDataSize;\r
2967 Status = EFI_BUFFER_TOO_SMALL;\r
2968 goto Done;\r
8d3a5c82 2969 }\r
2970\r
72399dae 2971Done:\r
2972 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2973 return Status;\r
2974}\r
2975\r
72399dae 2976/**\r
72399dae 2977 This code Finds the Next available variable.\r
2978\r
18a7dbbc
SZ
2979 Caution: This function may receive untrusted input.\r
2980 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r
2981\r
fa0737a8
SZ
2982 @param[in] VariableName Pointer to variable name.\r
2983 @param[in] VendorGuid Variable Vendor Guid.\r
2984 @param[out] VariablePtr Pointer to variable header address.\r
72399dae 2985\r
fa0737a8
SZ
2986 @return EFI_SUCCESS Find the specified variable.\r
2987 @return EFI_NOT_FOUND Not found.\r
72399dae 2988\r
2989**/\r
2990EFI_STATUS\r
2991EFIAPI\r
fa0737a8
SZ
2992VariableServiceGetNextVariableInternal (\r
2993 IN CHAR16 *VariableName,\r
2994 IN EFI_GUID *VendorGuid,\r
2995 OUT VARIABLE_HEADER **VariablePtr\r
72399dae 2996 )\r
2997{\r
0f7aff72 2998 VARIABLE_STORE_TYPE Type;\r
72399dae 2999 VARIABLE_POINTER_TRACK Variable;\r
0f7aff72 3000 VARIABLE_POINTER_TRACK VariableInHob;\r
23b06935 3001 VARIABLE_POINTER_TRACK VariablePtrTrack;\r
72399dae 3002 EFI_STATUS Status;\r
0f7aff72 3003 VARIABLE_STORE_HEADER *VariableStoreHeader[VariableStoreTypeMax];\r
72399dae 3004\r
9622df63 3005 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
72399dae 3006 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
3007 goto Done;\r
3008 }\r
3009\r
3010 if (VariableName[0] != 0) {\r
3011 //\r
8a2d4996 3012 // If variable name is not NULL, get next variable.\r
72399dae 3013 //\r
3014 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
3015 }\r
3016\r
0f7aff72
RN
3017 //\r
3018 // 0: Volatile, 1: HOB, 2: Non-Volatile.\r
3019 // The index and attributes mapping must be kept in this order as FindVariable\r
3020 // makes use of this mapping to implement search algorithm.\r
3021 //\r
3022 VariableStoreHeader[VariableStoreTypeVolatile] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;\r
3023 VariableStoreHeader[VariableStoreTypeHob] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;\r
3024 VariableStoreHeader[VariableStoreTypeNv] = mNvVariableCache;\r
3025\r
72399dae 3026 while (TRUE) {\r
3027 //\r
0f7aff72 3028 // Switch from Volatile to HOB, to Non-Volatile.\r
72399dae 3029 //\r
6ebffb67 3030 while (!IsValidVariableHeader (Variable.CurrPtr, Variable.EndPtr)) {\r
0f7aff72
RN
3031 //\r
3032 // Find current storage index\r
3033 //\r
3034 for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {\r
3035 if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr == GetStartPointer (VariableStoreHeader[Type]))) {\r
3036 break;\r
3037 }\r
3038 }\r
3039 ASSERT (Type < VariableStoreTypeMax);\r
3040 //\r
3041 // Switch to next storage\r
3042 //\r
3043 for (Type++; Type < VariableStoreTypeMax; Type++) {\r
3044 if (VariableStoreHeader[Type] != NULL) {\r
3045 break;\r
3046 }\r
3047 }\r
3048 //\r
fa0737a8 3049 // Capture the case that\r
0f7aff72
RN
3050 // 1. current storage is the last one, or\r
3051 // 2. no further storage\r
3052 //\r
3053 if (Type == VariableStoreTypeMax) {\r
72399dae 3054 Status = EFI_NOT_FOUND;\r
3055 goto Done;\r
3056 }\r
0f7aff72
RN
3057 Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);\r
3058 Variable.EndPtr = GetEndPointer (VariableStoreHeader[Type]);\r
3059 Variable.CurrPtr = Variable.StartPtr;\r
72399dae 3060 }\r
0f7aff72 3061\r
72399dae 3062 //\r
3063 // Variable is found\r
3064 //\r
23b06935
SZ
3065 if (Variable.CurrPtr->State == VAR_ADDED || Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
3066 if (!AtRuntime () || ((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {\r
3067 if (Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
3068 //\r
3069 // If it is a IN_DELETED_TRANSITION variable,\r
3070 // and there is also a same ADDED one at the same time,\r
3071 // don't return it.\r
3072 //\r
3073 VariablePtrTrack.StartPtr = Variable.StartPtr;\r
3074 VariablePtrTrack.EndPtr = Variable.EndPtr;\r
3075 Status = FindVariableEx (\r
3076 GetVariableNamePtr (Variable.CurrPtr),\r
fa0737a8 3077 GetVendorGuidPtr (Variable.CurrPtr),\r
23b06935
SZ
3078 FALSE,\r
3079 &VariablePtrTrack\r
3080 );\r
3081 if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State == VAR_ADDED) {\r
3082 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
3083 continue;\r
3084 }\r
3085 }\r
0f7aff72
RN
3086\r
3087 //\r
3088 // Don't return NV variable when HOB overrides it\r
3089 //\r
fa0737a8 3090 if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) && (VariableStoreHeader[VariableStoreTypeNv] != NULL) &&\r
0f7aff72
RN
3091 (Variable.StartPtr == GetStartPointer (VariableStoreHeader[VariableStoreTypeNv]))\r
3092 ) {\r
3093 VariableInHob.StartPtr = GetStartPointer (VariableStoreHeader[VariableStoreTypeHob]);\r
3094 VariableInHob.EndPtr = GetEndPointer (VariableStoreHeader[VariableStoreTypeHob]);\r
3095 Status = FindVariableEx (\r
3096 GetVariableNamePtr (Variable.CurrPtr),\r
fa0737a8 3097 GetVendorGuidPtr (Variable.CurrPtr),\r
9622df63 3098 FALSE,\r
0f7aff72
RN
3099 &VariableInHob\r
3100 );\r
3101 if (!EFI_ERROR (Status)) {\r
3102 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
3103 continue;\r
3104 }\r
3105 }\r
3106\r
fa0737a8
SZ
3107 *VariablePtr = Variable.CurrPtr;\r
3108 Status = EFI_SUCCESS;\r
72399dae 3109 goto Done;\r
3110 }\r
3111 }\r
3112\r
3113 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
3114 }\r
33a5a666 3115\r
8d3a5c82 3116Done:\r
fa0737a8
SZ
3117 return Status;\r
3118}\r
3119\r
3120/**\r
3121\r
3122 This code Finds the Next available variable.\r
3123\r
3124 Caution: This function may receive untrusted input.\r
3125 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r
3126\r
3127 @param VariableNameSize Size of the variable name.\r
3128 @param VariableName Pointer to variable name.\r
3129 @param VendorGuid Variable Vendor Guid.\r
3130\r
3131 @return EFI_INVALID_PARAMETER Invalid parameter.\r
3132 @return EFI_SUCCESS Find the specified variable.\r
3133 @return EFI_NOT_FOUND Not found.\r
3134 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.\r
3135\r
3136**/\r
3137EFI_STATUS\r
3138EFIAPI\r
3139VariableServiceGetNextVariableName (\r
3140 IN OUT UINTN *VariableNameSize,\r
3141 IN OUT CHAR16 *VariableName,\r
3142 IN OUT EFI_GUID *VendorGuid\r
3143 )\r
3144{\r
3145 EFI_STATUS Status;\r
3146 UINTN VarNameSize;\r
3147 VARIABLE_HEADER *VariablePtr;\r
3148\r
3149 if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {\r
3150 return EFI_INVALID_PARAMETER;\r
3151 }\r
3152\r
3153 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
3154\r
3155 Status = VariableServiceGetNextVariableInternal (VariableName, VendorGuid, &VariablePtr);\r
3156 if (!EFI_ERROR (Status)) {\r
3157 VarNameSize = NameSizeOfVariable (VariablePtr);\r
3158 ASSERT (VarNameSize != 0);\r
3159 if (VarNameSize <= *VariableNameSize) {\r
3160 CopyMem (VariableName, GetVariableNamePtr (VariablePtr), VarNameSize);\r
3161 CopyMem (VendorGuid, GetVendorGuidPtr (VariablePtr), sizeof (EFI_GUID));\r
3162 Status = EFI_SUCCESS;\r
3163 } else {\r
3164 Status = EFI_BUFFER_TOO_SMALL;\r
3165 }\r
3166\r
3167 *VariableNameSize = VarNameSize;\r
3168 }\r
3169\r
72399dae 3170 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
3171 return Status;\r
3172}\r
3173\r
3174/**\r
3175\r
3176 This code sets variable in storage blocks (Volatile or Non-Volatile).\r
3177\r
18a7dbbc
SZ
3178 Caution: This function may receive untrusted input.\r
3179 This function may be invoked in SMM mode, and datasize and data are external input.\r
3180 This function will do basic validation, before parse the data.\r
fa0737a8
SZ
3181 This function will parse the authentication carefully to avoid security issues, like\r
3182 buffer overflow, integer overflow.\r
3183 This function will check attribute carefully to avoid authentication bypass.\r
18a7dbbc 3184\r
8a2d4996 3185 @param VariableName Name of Variable to be found.\r
3186 @param VendorGuid Variable vendor GUID.\r
72399dae 3187 @param Attributes Attribute value of the variable found\r
3188 @param DataSize Size of Data found. If size is less than the\r
3189 data, this value contains the required size.\r
8a2d4996 3190 @param Data Data pointer.\r
72399dae 3191\r
8a2d4996 3192 @return EFI_INVALID_PARAMETER Invalid parameter.\r
3193 @return EFI_SUCCESS Set successfully.\r
3194 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.\r
3195 @return EFI_NOT_FOUND Not found.\r
3196 @return EFI_WRITE_PROTECTED Variable is read-only.\r
72399dae 3197\r
3198**/\r
3199EFI_STATUS\r
3200EFIAPI\r
8a2d4996 3201VariableServiceSetVariable (\r
72399dae 3202 IN CHAR16 *VariableName,\r
3203 IN EFI_GUID *VendorGuid,\r
3204 IN UINT32 Attributes,\r
3205 IN UINTN DataSize,\r
3206 IN VOID *Data\r
3207 )\r
3208{\r
3209 VARIABLE_POINTER_TRACK Variable;\r
3210 EFI_STATUS Status;\r
3211 VARIABLE_HEADER *NextVariable;\r
3212 EFI_PHYSICAL_ADDRESS Point;\r
fa0737a8 3213 UINTN PayloadSize;\r
ff843847
RN
3214 LIST_ENTRY *Link;\r
3215 VARIABLE_ENTRY *Entry;\r
efb01a10 3216 CHAR16 *Name;\r
72399dae 3217\r
3218 //\r
8a2d4996 3219 // Check input parameters.\r
72399dae 3220 //\r
3221 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r
3222 return EFI_INVALID_PARAMETER;\r
fa0737a8 3223 }\r
48a0e6bf 3224\r
3225 if (DataSize != 0 && Data == NULL) {\r
3226 return EFI_INVALID_PARAMETER;\r
3227 }\r
3228\r
8e38f18e 3229 //\r
fa0737a8 3230 // Check for reserverd bit in variable attribute.\r
8e38f18e 3231 //\r
fa0737a8 3232 if ((Attributes & (~EFI_VARIABLE_ATTRIBUTES_MASK)) != 0) {\r
8e38f18e 3233 return EFI_INVALID_PARAMETER;\r
3234 }\r
3235\r
72399dae 3236 //\r
8a2d4996 3237 // Make sure if runtime bit is set, boot service bit is set also.\r
72399dae 3238 //\r
3239 if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
3240 return EFI_INVALID_PARAMETER;\r
fa0737a8
SZ
3241 } else if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {\r
3242 if (!mVariableModuleGlobal->VariableGlobal.AuthSupport) {\r
3243 //\r
3244 // Not support authenticated variable write.\r
3245 //\r
3246 return EFI_INVALID_PARAMETER;\r
3247 }\r
3248 } else if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {\r
3249 if (PcdGet32 (PcdHwErrStorageSize) == 0) {\r
3250 //\r
3251 // Not support harware error record variable variable.\r
3252 //\r
3253 return EFI_INVALID_PARAMETER;\r
3254 }\r
3255 }\r
3256\r
3257 //\r
3258 // EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS and EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute\r
3259 // cannot be set both.\r
3260 //\r
3261 if (((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)\r
3262 && ((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {\r
3263 return EFI_INVALID_PARAMETER;\r
72399dae 3264 }\r
3265\r
fa0737a8
SZ
3266 if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) {\r
3267 if (DataSize < AUTHINFO_SIZE) {\r
3268 //\r
3269 // Try to write Authenticated Variable without AuthInfo.\r
3270 //\r
3271 return EFI_SECURITY_VIOLATION;\r
3272 }\r
3273 PayloadSize = DataSize - AUTHINFO_SIZE;\r
3274 } else if ((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {\r
3275 //\r
3276 // Sanity check for EFI_VARIABLE_AUTHENTICATION_2 descriptor.\r
3277 //\r
3278 if (DataSize < OFFSET_OF_AUTHINFO2_CERT_DATA ||\r
3279 ((EFI_VARIABLE_AUTHENTICATION_2 *) Data)->AuthInfo.Hdr.dwLength > DataSize - (OFFSET_OF (EFI_VARIABLE_AUTHENTICATION_2, AuthInfo)) ||\r
3280 ((EFI_VARIABLE_AUTHENTICATION_2 *) Data)->AuthInfo.Hdr.dwLength < OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID, CertData)) {\r
3281 return EFI_SECURITY_VIOLATION;\r
3282 }\r
3283 PayloadSize = DataSize - AUTHINFO2_SIZE (Data);\r
3284 } else {\r
3285 PayloadSize = DataSize;\r
3286 }\r
3287\r
3288 if ((UINTN)(~0) - PayloadSize < StrSize(VariableName)){\r
3289 //\r
3290 // Prevent whole variable size overflow\r
56251c66 3291 //\r
56251c66 3292 return EFI_INVALID_PARAMETER;\r
3293 }\r
3294\r
72399dae 3295 //\r
3296 // The size of the VariableName, including the Unicode Null in bytes plus\r
188e4e84 3297 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)\r
fa0737a8 3298 // bytes for HwErrRec#### variable.\r
72399dae 3299 //\r
3300 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
fa0737a8 3301 if (StrSize (VariableName) + PayloadSize > PcdGet32 (PcdMaxHardwareErrorVariableSize) - GetVariableHeaderSize ()) {\r
72399dae 3302 return EFI_INVALID_PARAMETER;\r
3303 }\r
a5f15e30 3304 if (!IsHwErrRecVariable(VariableName, VendorGuid)) {\r
72399dae 3305 return EFI_INVALID_PARAMETER;\r
3306 }\r
3307 } else {\r
3308 //\r
3309 // The size of the VariableName, including the Unicode Null in bytes plus\r
fa0737a8 3310 // the DataSize is limited to maximum size of Max(Auth)VariableSize bytes.\r
72399dae 3311 //\r
fa0737a8
SZ
3312 if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {\r
3313 if (StrSize (VariableName) + PayloadSize > mVariableModuleGlobal->MaxAuthVariableSize - GetVariableHeaderSize ()) {\r
3314 return EFI_INVALID_PARAMETER;\r
3315 }\r
3316 } else {\r
3317 if (StrSize (VariableName) + PayloadSize > mVariableModuleGlobal->MaxVariableSize - GetVariableHeaderSize ()) {\r
3318 return EFI_INVALID_PARAMETER;\r
3319 }\r
efb01a10 3320 }\r
6675a21f
SZ
3321 }\r
3322\r
fa0737a8
SZ
3323 Status = InternalVarCheckSetVariableCheck (VariableName, VendorGuid, Attributes, PayloadSize, (VOID *) ((UINTN) Data + DataSize - PayloadSize));\r
3324 if (EFI_ERROR (Status)) {\r
3325 return Status;\r
3326 }\r
3327\r
72399dae 3328 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
3329\r
3330 //\r
8a2d4996 3331 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.\r
72399dae 3332 //\r
3333 if (1 < InterlockedIncrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState)) {\r
8a2d4996 3334 Point = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;\r
72399dae 3335 //\r
8a2d4996 3336 // Parse non-volatile variable data and get last variable offset.\r
72399dae 3337 //\r
3338 NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point);\r
6ebffb67 3339 while (IsValidVariableHeader (NextVariable, GetEndPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point))) {\r
72399dae 3340 NextVariable = GetNextVariablePtr (NextVariable);\r
3341 }\r
3342 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) Point;\r
3343 }\r
3344\r
ff843847
RN
3345 if (mEndOfDxe && mEnableLocking) {\r
3346 //\r
3347 // Treat the variables listed in the forbidden variable list as read-only after leaving DXE phase.\r
3348 //\r
3349 for ( Link = GetFirstNode (&mLockedVariableList)\r
3350 ; !IsNull (&mLockedVariableList, Link)\r
3351 ; Link = GetNextNode (&mLockedVariableList, Link)\r
3352 ) {\r
3353 Entry = BASE_CR (Link, VARIABLE_ENTRY, Link);\r
efb01a10
SZ
3354 Name = (CHAR16 *) ((UINTN) Entry + sizeof (*Entry));\r
3355 if (CompareGuid (&Entry->Guid, VendorGuid) && (StrCmp (Name, VariableName) == 0)) {\r
ff843847
RN
3356 Status = EFI_WRITE_PROTECTED;\r
3357 DEBUG ((EFI_D_INFO, "[Variable]: Changing readonly variable after leaving DXE phase - %g:%s\n", VendorGuid, VariableName));\r
3358 goto Done;\r
3359 }\r
3360 }\r
3361 }\r
3362\r
72399dae 3363 //\r
8a2d4996 3364 // Check whether the input variable is already existed.\r
72399dae 3365 //\r
9622df63
SZ
3366 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, TRUE);\r
3367 if (!EFI_ERROR (Status)) {\r
3368 if (((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) && AtRuntime ()) {\r
ff843847
RN
3369 Status = EFI_WRITE_PROTECTED;\r
3370 goto Done;\r
9622df63 3371 }\r
fa0737a8 3372 if (Attributes != 0 && (Attributes & (~EFI_VARIABLE_APPEND_WRITE)) != Variable.CurrPtr->Attributes) {\r
6e67fec0
SZ
3373 //\r
3374 // If a preexisting variable is rewritten with different attributes, SetVariable() shall not\r
3375 // modify the variable and shall return EFI_INVALID_PARAMETER. Two exceptions to this rule:\r
3376 // 1. No access attributes specified\r
3377 // 2. The only attribute differing is EFI_VARIABLE_APPEND_WRITE\r
3378 //\r
3379 Status = EFI_INVALID_PARAMETER;\r
4edb1866 3380 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
3381 goto Done;\r
3382 }\r
9622df63 3383 }\r
72399dae 3384\r
b2bd493e 3385 if (!FeaturePcdGet (PcdUefiVariableDefaultLangDeprecate)) {\r
9bc5dabb 3386 //\r
b2bd493e 3387 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.\r
9bc5dabb 3388 //\r
b2bd493e
SZ
3389 Status = AutoUpdateLangVariable (VariableName, Data, DataSize);\r
3390 if (EFI_ERROR (Status)) {\r
3391 //\r
3392 // The auto update operation failed, directly return to avoid inconsistency between PlatformLang and Lang.\r
3393 //\r
3394 goto Done;\r
3395 }\r
9bc5dabb 3396 }\r
72399dae 3397\r
fa0737a8
SZ
3398 if (mVariableModuleGlobal->VariableGlobal.AuthSupport) {\r
3399 Status = AuthVariableLibProcessVariable (VariableName, VendorGuid, Data, DataSize, Attributes);\r
3400 } else {\r
3401 Status = UpdateVariable (VariableName, VendorGuid, Data, DataSize, Attributes, 0, 0, &Variable, NULL);\r
3402 }\r
72399dae 3403\r
ff843847 3404Done:\r
fdb7765f 3405 InterlockedDecrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState);\r
052ad7e1 3406 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
fdb7765f 3407\r
fa0737a8
SZ
3408 if (!AtRuntime ()) {\r
3409 if (!EFI_ERROR (Status)) {\r
3410 SecureBootHook (\r
3411 VariableName,\r
3412 VendorGuid\r
3413 );\r
3414 }\r
3415 }\r
3416\r
8d3a5c82 3417 return Status;\r
3418}\r
3419\r
7c80e839 3420/**\r
8d3a5c82 3421\r
3422 This code returns information about the EFI variables.\r
3423\r
18a7dbbc
SZ
3424 Caution: This function may receive untrusted input.\r
3425 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r
3426\r
7c80e839 3427 @param Attributes Attributes bitmask to specify the type of variables\r
3428 on which to return information.\r
3429 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available\r
3430 for the EFI variables associated with the attributes specified.\r
3431 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available\r
3432 for EFI variables associated with the attributes specified.\r
3433 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables\r
3434 associated with the attributes specified.\r
8d3a5c82 3435\r
7c80e839 3436 @return EFI_SUCCESS Query successfully.\r
8d3a5c82 3437\r
7c80e839 3438**/\r
052ad7e1
A
3439EFI_STATUS\r
3440EFIAPI\r
b2bd493e 3441VariableServiceQueryVariableInfoInternal (\r
052ad7e1
A
3442 IN UINT32 Attributes,\r
3443 OUT UINT64 *MaximumVariableStorageSize,\r
3444 OUT UINT64 *RemainingVariableStorageSize,\r
3445 OUT UINT64 *MaximumVariableSize\r
3446 )\r
8d3a5c82 3447{\r
3448 VARIABLE_HEADER *Variable;\r
3449 VARIABLE_HEADER *NextVariable;\r
3450 UINT64 VariableSize;\r
3451 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
2fcdca1d 3452 UINT64 CommonVariableTotalSize;\r
3453 UINT64 HwErrVariableTotalSize;\r
b2bd493e
SZ
3454 EFI_STATUS Status;\r
3455 VARIABLE_POINTER_TRACK VariablePtrTrack;\r
2fcdca1d 3456\r
3457 CommonVariableTotalSize = 0;\r
3458 HwErrVariableTotalSize = 0;\r
8d3a5c82 3459\r
8d3a5c82 3460 if((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r
3461 //\r
3462 // Query is Volatile related.\r
3463 //\r
052ad7e1 3464 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r
8d3a5c82 3465 } else {\r
3466 //\r
3467 // Query is Non-Volatile related.\r
3468 //\r
8a2d4996 3469 VariableStoreHeader = mNvVariableCache;\r
8d3a5c82 3470 }\r
3471\r
3472 //\r
3473 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize\r
3474 // with the storage size (excluding the storage header size).\r
3475 //\r
3476 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);\r
c6492839 3477\r
3478 //\r
3479 // Harware error record variable needs larger size.\r
3480 //\r
2fcdca1d 3481 if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
188e4e84 3482 *MaximumVariableStorageSize = PcdGet32 (PcdHwErrStorageSize);\r
fa0737a8 3483 *MaximumVariableSize = PcdGet32 (PcdMaxHardwareErrorVariableSize) - GetVariableHeaderSize ();\r
2fcdca1d 3484 } else {\r
3485 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
4edb1866
SZ
3486 if (AtRuntime ()) {\r
3487 *MaximumVariableStorageSize = mVariableModuleGlobal->CommonRuntimeVariableSpace;\r
3488 } else {\r
3489 *MaximumVariableStorageSize = mVariableModuleGlobal->CommonVariableSpace;\r
3490 }\r
2fcdca1d 3491 }\r
3492\r
3493 //\r
fa0737a8 3494 // Let *MaximumVariableSize be Max(Auth)VariableSize with the exception of the variable header size.\r
2fcdca1d 3495 //\r
fa0737a8
SZ
3496 if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {\r
3497 *MaximumVariableSize = mVariableModuleGlobal->MaxAuthVariableSize - GetVariableHeaderSize ();\r
3498 } else {\r
3499 *MaximumVariableSize = mVariableModuleGlobal->MaxVariableSize - GetVariableHeaderSize ();\r
3500 }\r
c6492839 3501 }\r
8d3a5c82 3502\r
3503 //\r
3504 // Point to the starting address of the variables.\r
3505 //\r
9cad030b 3506 Variable = GetStartPointer (VariableStoreHeader);\r
8d3a5c82 3507\r
3508 //\r
3509 // Now walk through the related variable store.\r
3510 //\r
6ebffb67 3511 while (IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))) {\r
8d3a5c82 3512 NextVariable = GetNextVariablePtr (Variable);\r
3513 VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;\r
3514\r
8a2d4996 3515 if (AtRuntime ()) {\r
8d3a5c82 3516 //\r
8a2d4996 3517 // We don't take the state of the variables in mind\r
8d3a5c82 3518 // when calculating RemainingVariableStorageSize,\r
3519 // since the space occupied by variables not marked with\r
3520 // VAR_ADDED is not allowed to be reclaimed in Runtime.\r
3521 //\r
3b425367 3522 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2fcdca1d 3523 HwErrVariableTotalSize += VariableSize;\r
3524 } else {\r
3525 CommonVariableTotalSize += VariableSize;\r
3526 }\r
8d3a5c82 3527 } else {\r
3528 //\r
8a2d4996 3529 // Only care about Variables with State VAR_ADDED, because\r
8d3a5c82 3530 // the space not marked as VAR_ADDED is reclaimable now.\r
3531 //\r
3532 if (Variable->State == VAR_ADDED) {\r
3b425367 3533 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2fcdca1d 3534 HwErrVariableTotalSize += VariableSize;\r
3535 } else {\r
3536 CommonVariableTotalSize += VariableSize;\r
3537 }\r
b2bd493e
SZ
3538 } else if (Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
3539 //\r
3540 // If it is a IN_DELETED_TRANSITION variable,\r
3541 // and there is not also a same ADDED one at the same time,\r
3542 // this IN_DELETED_TRANSITION variable is valid.\r
3543 //\r
3544 VariablePtrTrack.StartPtr = GetStartPointer (VariableStoreHeader);\r
3545 VariablePtrTrack.EndPtr = GetEndPointer (VariableStoreHeader);\r
3546 Status = FindVariableEx (\r
3547 GetVariableNamePtr (Variable),\r
fa0737a8 3548 GetVendorGuidPtr (Variable),\r
b2bd493e
SZ
3549 FALSE,\r
3550 &VariablePtrTrack\r
3551 );\r
3552 if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State != VAR_ADDED) {\r
3553 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
3554 HwErrVariableTotalSize += VariableSize;\r
3555 } else {\r
3556 CommonVariableTotalSize += VariableSize;\r
3557 }\r
3558 }\r
8d3a5c82 3559 }\r
3560 }\r
3561\r
3562 //\r
8a2d4996 3563 // Go to the next one.\r
8d3a5c82 3564 //\r
3565 Variable = NextVariable;\r
3566 }\r
3567\r
2fcdca1d 3568 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD){\r
3569 *RemainingVariableStorageSize = *MaximumVariableStorageSize - HwErrVariableTotalSize;\r
4edb1866
SZ
3570 } else {\r
3571 if (*MaximumVariableStorageSize < CommonVariableTotalSize) {\r
3572 *RemainingVariableStorageSize = 0;\r
3573 } else {\r
3574 *RemainingVariableStorageSize = *MaximumVariableStorageSize - CommonVariableTotalSize;\r
3575 }\r
2fcdca1d 3576 }\r
3577\r
fa0737a8 3578 if (*RemainingVariableStorageSize < GetVariableHeaderSize ()) {\r
c6492839 3579 *MaximumVariableSize = 0;\r
fa0737a8
SZ
3580 } else if ((*RemainingVariableStorageSize - GetVariableHeaderSize ()) < *MaximumVariableSize) {\r
3581 *MaximumVariableSize = *RemainingVariableStorageSize - GetVariableHeaderSize ();\r
c6492839 3582 }\r
3583\r
8d3a5c82 3584 return EFI_SUCCESS;\r
3585}\r
3586\r
b2bd493e
SZ
3587/**\r
3588\r
3589 This code returns information about the EFI variables.\r
3590\r
18a7dbbc
SZ
3591 Caution: This function may receive untrusted input.\r
3592 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r
3593\r
b2bd493e
SZ
3594 @param Attributes Attributes bitmask to specify the type of variables\r
3595 on which to return information.\r
3596 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available\r
3597 for the EFI variables associated with the attributes specified.\r
3598 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available\r
3599 for EFI variables associated with the attributes specified.\r
3600 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables\r
3601 associated with the attributes specified.\r
3602\r
3603 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.\r
3604 @return EFI_SUCCESS Query successfully.\r
3605 @return EFI_UNSUPPORTED The attribute is not supported on this platform.\r
3606\r
3607**/\r
3608EFI_STATUS\r
3609EFIAPI\r
3610VariableServiceQueryVariableInfo (\r
3611 IN UINT32 Attributes,\r
3612 OUT UINT64 *MaximumVariableStorageSize,\r
3613 OUT UINT64 *RemainingVariableStorageSize,\r
3614 OUT UINT64 *MaximumVariableSize\r
3615 )\r
3616{\r
3617 EFI_STATUS Status;\r
3618\r
3619 if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {\r
3620 return EFI_INVALID_PARAMETER;\r
3621 }\r
3622\r
fa0737a8 3623 if ((Attributes & EFI_VARIABLE_ATTRIBUTES_MASK) == 0) {\r
b2bd493e
SZ
3624 //\r
3625 // Make sure the Attributes combination is supported by the platform.\r
3626 //\r
3627 return EFI_UNSUPPORTED;\r
3628 } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
3629 //\r
3630 // Make sure if runtime bit is set, boot service bit is set also.\r
3631 //\r
3632 return EFI_INVALID_PARAMETER;\r
3633 } else if (AtRuntime () && ((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0)) {\r
3634 //\r
3635 // Make sure RT Attribute is set if we are in Runtime phase.\r
3636 //\r
3637 return EFI_INVALID_PARAMETER;\r
3638 } else if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
3639 //\r
3640 // Make sure Hw Attribute is set with NV.\r
3641 //\r
3642 return EFI_INVALID_PARAMETER;\r
fa0737a8
SZ
3643 } else if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {\r
3644 if (!mVariableModuleGlobal->VariableGlobal.AuthSupport) {\r
3645 //\r
3646 // Not support authenticated variable write.\r
3647 //\r
3648 return EFI_UNSUPPORTED;\r
3649 }\r
3650 } else if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {\r
3651 if (PcdGet32 (PcdHwErrStorageSize) == 0) {\r
3652 //\r
3653 // Not support harware error record variable variable.\r
3654 //\r
3655 return EFI_UNSUPPORTED;\r
3656 }\r
b2bd493e
SZ
3657 }\r
3658\r
3659 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
3660\r
3661 Status = VariableServiceQueryVariableInfoInternal (\r
3662 Attributes,\r
3663 MaximumVariableStorageSize,\r
3664 RemainingVariableStorageSize,\r
3665 MaximumVariableSize\r
3666 );\r
3667\r
3668 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
3669 return Status;\r
3670}\r
7c80e839 3671\r
3672/**\r
8a2d4996 3673 This function reclaims variable storage if free size is below the threshold.\r
18a7dbbc
SZ
3674\r
3675 Caution: This function may be invoked at SMM mode.\r
3676 Care must be taken to make sure not security issue.\r
3677\r
7c80e839 3678**/\r
7800593d 3679VOID\r
7800593d 3680ReclaimForOS(\r
8a2d4996 3681 VOID\r
7800593d
LG
3682 )\r
3683{\r
9948c0b0 3684 EFI_STATUS Status;\r
4edb1866 3685 UINTN RemainingCommonRuntimeVariableSpace;\r
2fcdca1d 3686 UINTN RemainingHwErrVariableSpace;\r
0fb5e515
SZ
3687 STATIC BOOLEAN Reclaimed;\r
3688\r
3689 //\r
3690 // This function will be called only once at EndOfDxe or ReadyToBoot event.\r
3691 //\r
3692 if (Reclaimed) {\r
3693 return;\r
3694 }\r
3695 Reclaimed = TRUE;\r
7800593d 3696\r
4edb1866 3697 Status = EFI_SUCCESS;\r
7800593d 3698\r
4edb1866
SZ
3699 if (mVariableModuleGlobal->CommonRuntimeVariableSpace < mVariableModuleGlobal->CommonVariableTotalSize) {\r
3700 RemainingCommonRuntimeVariableSpace = 0;\r
3701 } else {\r
3702 RemainingCommonRuntimeVariableSpace = mVariableModuleGlobal->CommonRuntimeVariableSpace - mVariableModuleGlobal->CommonVariableTotalSize;\r
3703 }\r
2fcdca1d 3704\r
3705 RemainingHwErrVariableSpace = PcdGet32 (PcdHwErrStorageSize) - mVariableModuleGlobal->HwErrVariableTotalSize;\r
fa0737a8 3706\r
7800593d 3707 //\r
4edb1866 3708 // Check if the free area is below a threshold.\r
7800593d 3709 //\r
fa0737a8
SZ
3710 if (((RemainingCommonRuntimeVariableSpace < mVariableModuleGlobal->MaxVariableSize) ||\r
3711 (RemainingCommonRuntimeVariableSpace < mVariableModuleGlobal->MaxAuthVariableSize)) ||\r
3712 ((PcdGet32 (PcdHwErrStorageSize) != 0) &&\r
9948c0b0 3713 (RemainingHwErrVariableSpace < PcdGet32 (PcdMaxHardwareErrorVariableSize)))){\r
7800593d 3714 Status = Reclaim (\r
2fcdca1d 3715 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
3716 &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
3717 FALSE,\r
335e2681 3718 NULL,\r
7baf3c69
SZ
3719 NULL,\r
3720 0\r
2fcdca1d 3721 );\r
7800593d
LG
3722 ASSERT_EFI_ERROR (Status);\r
3723 }\r
3724}\r
3725\r
fa0737a8
SZ
3726/**\r
3727 Get non-volatile maximum variable size.\r
3728\r
3729 @return Non-volatile maximum variable size.\r
3730\r
3731**/\r
3732UINTN\r
3733GetNonVolatileMaxVariableSize (\r
3734 VOID\r
3735 )\r
3736{\r
3737 if (PcdGet32 (PcdHwErrStorageSize) != 0) {\r
3738 return MAX (MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxAuthVariableSize)),\r
3739 PcdGet32 (PcdMaxHardwareErrorVariableSize));\r
3740 } else {\r
3741 return MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxAuthVariableSize));\r
3742 }\r
3743}\r
3744\r
3e02ebb2
SZ
3745/**\r
3746 Init non-volatile variable store.\r
3747\r
fa0737a8
SZ
3748 @param[out] NvFvHeader Output pointer to non-volatile FV header address.\r
3749\r
3e02ebb2
SZ
3750 @retval EFI_SUCCESS Function successfully executed.\r
3751 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.\r
3752 @retval EFI_VOLUME_CORRUPTED Variable Store or Firmware Volume for Variable Store is corrupted.\r
3753\r
3754**/\r
3755EFI_STATUS\r
3756InitNonVolatileVariableStore (\r
fa0737a8 3757 OUT EFI_FIRMWARE_VOLUME_HEADER **NvFvHeader\r
3e02ebb2
SZ
3758 )\r
3759{\r
3760 EFI_FIRMWARE_VOLUME_HEADER *FvHeader;\r
4edb1866 3761 VARIABLE_HEADER *Variable;\r
3e02ebb2
SZ
3762 VARIABLE_HEADER *NextVariable;\r
3763 EFI_PHYSICAL_ADDRESS VariableStoreBase;\r
3764 UINT64 VariableStoreLength;\r
3765 UINTN VariableSize;\r
3766 EFI_HOB_GUID_TYPE *GuidHob;\r
3767 EFI_PHYSICAL_ADDRESS NvStorageBase;\r
3768 UINT8 *NvStorageData;\r
3769 UINT32 NvStorageSize;\r
3770 FAULT_TOLERANT_WRITE_LAST_WRITE_DATA *FtwLastWriteData;\r
3771 UINT32 BackUpOffset;\r
3772 UINT32 BackUpSize;\r
4edb1866
SZ
3773 UINT32 HwErrStorageSize;\r
3774 UINT32 MaxUserNvVariableSpaceSize;\r
3775 UINT32 BoottimeReservedNvVariableSpaceSize;\r
3e02ebb2
SZ
3776\r
3777 mVariableModuleGlobal->FvbInstance = NULL;\r
3778\r
3e02ebb2
SZ
3779 //\r
3780 // Allocate runtime memory used for a memory copy of the FLASH region.\r
3781 // Keep the memory and the FLASH in sync as updates occur.\r
3782 //\r
3783 NvStorageSize = PcdGet32 (PcdFlashNvStorageVariableSize);\r
3784 NvStorageData = AllocateRuntimeZeroPool (NvStorageSize);\r
3785 if (NvStorageData == NULL) {\r
3786 return EFI_OUT_OF_RESOURCES;\r
3787 }\r
3788\r
3789 NvStorageBase = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);\r
3790 if (NvStorageBase == 0) {\r
3791 NvStorageBase = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);\r
3792 }\r
3793 //\r
3794 // Copy NV storage data to the memory buffer.\r
3795 //\r
3796 CopyMem (NvStorageData, (UINT8 *) (UINTN) NvStorageBase, NvStorageSize);\r
3797\r
3798 //\r
3799 // Check the FTW last write data hob.\r
3800 //\r
3801 GuidHob = GetFirstGuidHob (&gEdkiiFaultTolerantWriteGuid);\r
3802 if (GuidHob != NULL) {\r
3803 FtwLastWriteData = (FAULT_TOLERANT_WRITE_LAST_WRITE_DATA *) GET_GUID_HOB_DATA (GuidHob);\r
3804 if (FtwLastWriteData->TargetAddress == NvStorageBase) {\r
3805 DEBUG ((EFI_D_INFO, "Variable: NV storage is backed up in spare block: 0x%x\n", (UINTN) FtwLastWriteData->SpareAddress));\r
3806 //\r
3807 // Copy the backed up NV storage data to the memory buffer from spare block.\r
3808 //\r
3809 CopyMem (NvStorageData, (UINT8 *) (UINTN) (FtwLastWriteData->SpareAddress), NvStorageSize);\r
3810 } else if ((FtwLastWriteData->TargetAddress > NvStorageBase) &&\r
3811 (FtwLastWriteData->TargetAddress < (NvStorageBase + NvStorageSize))) {\r
3812 //\r
fa0737a8 3813 // Flash NV storage from the Offset is backed up in spare block.\r
3e02ebb2
SZ
3814 //\r
3815 BackUpOffset = (UINT32) (FtwLastWriteData->TargetAddress - NvStorageBase);\r
3816 BackUpSize = NvStorageSize - BackUpOffset;\r
3817 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
3818 //\r
3819 // Copy the partial backed up NV storage data to the memory buffer from spare block.\r
3820 //\r
3821 CopyMem (NvStorageData + BackUpOffset, (UINT8 *) (UINTN) FtwLastWriteData->SpareAddress, BackUpSize);\r
3822 }\r
3823 }\r
3824\r
3825 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) NvStorageData;\r
3826\r
3827 //\r
3828 // Check if the Firmware Volume is not corrupted\r
3829 //\r
3830 if ((FvHeader->Signature != EFI_FVH_SIGNATURE) || (!CompareGuid (&gEfiSystemNvDataFvGuid, &FvHeader->FileSystemGuid))) {\r
3831 FreePool (NvStorageData);\r
3832 DEBUG ((EFI_D_ERROR, "Firmware Volume for Variable Store is corrupted\n"));\r
3833 return EFI_VOLUME_CORRUPTED;\r
3834 }\r
3835\r
3836 VariableStoreBase = (EFI_PHYSICAL_ADDRESS) ((UINTN) FvHeader + FvHeader->HeaderLength);\r
3837 VariableStoreLength = (UINT64) (NvStorageSize - FvHeader->HeaderLength);\r
3838\r
3839 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase;\r
3840 mNvVariableCache = (VARIABLE_STORE_HEADER *) (UINTN) VariableStoreBase;\r
3841 if (GetVariableStoreStatus (mNvVariableCache) != EfiValid) {\r
3842 FreePool (NvStorageData);\r
3843 DEBUG((EFI_D_ERROR, "Variable Store header is corrupted\n"));\r
3844 return EFI_VOLUME_CORRUPTED;\r
3845 }\r
3846 ASSERT(mNvVariableCache->Size == VariableStoreLength);\r
3847\r
4edb1866
SZ
3848 ASSERT (sizeof (VARIABLE_STORE_HEADER) <= VariableStoreLength);\r
3849\r
fa0737a8
SZ
3850 mVariableModuleGlobal->VariableGlobal.AuthFormat = (BOOLEAN)(CompareGuid (&mNvVariableCache->Signature, &gEfiAuthenticatedVariableGuid));\r
3851\r
4edb1866
SZ
3852 HwErrStorageSize = PcdGet32 (PcdHwErrStorageSize);\r
3853 MaxUserNvVariableSpaceSize = PcdGet32 (PcdMaxUserNvVariableSpaceSize);\r
3854 BoottimeReservedNvVariableSpaceSize = PcdGet32 (PcdBoottimeReservedNvVariableSpaceSize);\r
3855\r
3856 //\r
3857 // Note that in EdkII variable driver implementation, Hardware Error Record type variable\r
3858 // is stored with common variable in the same NV region. So the platform integrator should\r
3859 // ensure that the value of PcdHwErrStorageSize is less than the value of\r
fa0737a8 3860 // (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)).\r
4edb1866
SZ
3861 //\r
3862 ASSERT (HwErrStorageSize < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)));\r
3863 //\r
3864 // Ensure that the value of PcdMaxUserNvVariableSpaceSize is less than the value of\r
fa0737a8 3865 // (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)) - PcdGet32 (PcdHwErrStorageSize).\r
4edb1866
SZ
3866 //\r
3867 ASSERT (MaxUserNvVariableSpaceSize < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER) - HwErrStorageSize));\r
3868 //\r
3869 // Ensure that the value of PcdBoottimeReservedNvVariableSpaceSize is less than the value of\r
fa0737a8 3870 // (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)) - PcdGet32 (PcdHwErrStorageSize).\r
4edb1866
SZ
3871 //\r
3872 ASSERT (BoottimeReservedNvVariableSpaceSize < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER) - HwErrStorageSize));\r
3873\r
3874 mVariableModuleGlobal->CommonVariableSpace = ((UINTN) VariableStoreLength - sizeof (VARIABLE_STORE_HEADER) - HwErrStorageSize);\r
3875 mVariableModuleGlobal->CommonMaxUserVariableSpace = ((MaxUserNvVariableSpaceSize != 0) ? MaxUserNvVariableSpaceSize : mVariableModuleGlobal->CommonVariableSpace);\r
3876 mVariableModuleGlobal->CommonRuntimeVariableSpace = mVariableModuleGlobal->CommonVariableSpace - BoottimeReservedNvVariableSpaceSize;\r
3877\r
3878 DEBUG ((EFI_D_INFO, "Variable driver common space: 0x%x 0x%x 0x%x\n", mVariableModuleGlobal->CommonVariableSpace, mVariableModuleGlobal->CommonMaxUserVariableSpace, mVariableModuleGlobal->CommonRuntimeVariableSpace));\r
3879\r
3e02ebb2 3880 //\r
fa0737a8 3881 // The max NV variable size should be < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)).\r
3e02ebb2 3882 //\r
fa0737a8
SZ
3883 ASSERT (GetNonVolatileMaxVariableSize () < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)));\r
3884\r
3885 mVariableModuleGlobal->MaxVariableSize = PcdGet32 (PcdMaxVariableSize);\r
3886 mVariableModuleGlobal->MaxAuthVariableSize = ((PcdGet32 (PcdMaxAuthVariableSize) != 0) ? PcdGet32 (PcdMaxAuthVariableSize) : mVariableModuleGlobal->MaxVariableSize);\r
3e02ebb2
SZ
3887\r
3888 //\r
3889 // Parse non-volatile variable data and get last variable offset.\r
3890 //\r
4edb1866
SZ
3891 Variable = GetStartPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase);\r
3892 while (IsValidVariableHeader (Variable, GetEndPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase))) {\r
3893 NextVariable = GetNextVariablePtr (Variable);\r
3894 VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
3895 if ((Variable->Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
3896 mVariableModuleGlobal->HwErrVariableTotalSize += VariableSize;\r
3e02ebb2 3897 } else {\r
4edb1866 3898 mVariableModuleGlobal->CommonVariableTotalSize += VariableSize;\r
3e02ebb2
SZ
3899 }\r
3900\r
4edb1866 3901 Variable = NextVariable;\r
3e02ebb2 3902 }\r
4edb1866 3903 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) Variable - (UINTN) VariableStoreBase;\r
3e02ebb2 3904\r
fa0737a8 3905 *NvFvHeader = FvHeader;\r
3e02ebb2
SZ
3906 return EFI_SUCCESS;\r
3907}\r
3908\r
335e2681
SZ
3909/**\r
3910 Flush the HOB variable to flash.\r
3911\r
3912 @param[in] VariableName Name of variable has been updated or deleted.\r
3913 @param[in] VendorGuid Guid of variable has been updated or deleted.\r
3914\r
3915**/\r
3916VOID\r
3917FlushHobVariableToFlash (\r
3918 IN CHAR16 *VariableName,\r
3919 IN EFI_GUID *VendorGuid\r
3920 )\r
3921{\r
3922 EFI_STATUS Status;\r
3923 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
3924 VARIABLE_HEADER *Variable;\r
3925 VOID *VariableData;\r
31349131 3926 VARIABLE_POINTER_TRACK VariablePtrTrack;\r
335e2681
SZ
3927 BOOLEAN ErrorFlag;\r
3928\r
3929 ErrorFlag = FALSE;\r
3930\r
3931 //\r
3932 // Flush the HOB variable to flash.\r
3933 //\r
3934 if (mVariableModuleGlobal->VariableGlobal.HobVariableBase != 0) {\r
3935 VariableStoreHeader = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;\r
3936 //\r
3937 // Set HobVariableBase to 0, it can avoid SetVariable to call back.\r
3938 //\r
3939 mVariableModuleGlobal->VariableGlobal.HobVariableBase = 0;\r
3940 for ( Variable = GetStartPointer (VariableStoreHeader)\r
6ebffb67 3941 ; IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))\r
335e2681
SZ
3942 ; Variable = GetNextVariablePtr (Variable)\r
3943 ) {\r
3944 if (Variable->State != VAR_ADDED) {\r
3945 //\r
3946 // The HOB variable has been set to DELETED state in local.\r
3947 //\r
3948 continue;\r
3949 }\r
3950 ASSERT ((Variable->Attributes & EFI_VARIABLE_NON_VOLATILE) != 0);\r
3951 if (VendorGuid == NULL || VariableName == NULL ||\r
fa0737a8 3952 !CompareGuid (VendorGuid, GetVendorGuidPtr (Variable)) ||\r
335e2681
SZ
3953 StrCmp (VariableName, GetVariableNamePtr (Variable)) != 0) {\r
3954 VariableData = GetVariableDataPtr (Variable);\r
31349131
SZ
3955 FindVariable (GetVariableNamePtr (Variable), GetVendorGuidPtr (Variable), &VariablePtrTrack, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
3956 Status = UpdateVariable (\r
335e2681 3957 GetVariableNamePtr (Variable),\r
fa0737a8 3958 GetVendorGuidPtr (Variable),\r
31349131 3959 VariableData,\r
fa0737a8 3960 DataSizeOfVariable (Variable),\r
31349131
SZ
3961 Variable->Attributes,\r
3962 0,\r
3963 0,\r
3964 &VariablePtrTrack,\r
3965 NULL\r
3966 );\r
fa0737a8 3967 DEBUG ((EFI_D_INFO, "Variable driver flush the HOB variable to flash: %g %s %r\n", GetVendorGuidPtr (Variable), GetVariableNamePtr (Variable), Status));\r
335e2681
SZ
3968 } else {\r
3969 //\r
31349131 3970 // The updated or deleted variable is matched with this HOB variable.\r
335e2681
SZ
3971 // Don't break here because we will try to set other HOB variables\r
3972 // since this variable could be set successfully.\r
3973 //\r
3974 Status = EFI_SUCCESS;\r
3975 }\r
3976 if (!EFI_ERROR (Status)) {\r
3977 //\r
3978 // If set variable successful, or the updated or deleted variable is matched with the HOB variable,\r
3979 // set the HOB variable to DELETED state in local.\r
3980 //\r
fa0737a8 3981 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
3982 Variable->State &= VAR_DELETED;\r
3983 } else {\r
3984 ErrorFlag = TRUE;\r
3985 }\r
3986 }\r
3987 if (ErrorFlag) {\r
3988 //\r
3989 // We still have HOB variable(s) not flushed in flash.\r
3990 //\r
3991 mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VariableStoreHeader;\r
3992 } else {\r
3993 //\r
3994 // All HOB variables have been flushed in flash.\r
3995 //\r
3996 DEBUG ((EFI_D_INFO, "Variable driver: all HOB variables have been flushed in flash.\n"));\r
3997 if (!AtRuntime ()) {\r
3998 FreePool ((VOID *) VariableStoreHeader);\r
3999 }\r
4000 }\r
4001 }\r
4002\r
4003}\r
8a2d4996 4004\r
7c80e839 4005/**\r
3e02ebb2 4006 Initializes variable write service after FTW was ready.\r
7c80e839 4007\r
8a2d4996 4008 @retval EFI_SUCCESS Function successfully executed.\r
4009 @retval Others Fail to initialize the variable service.\r
4010\r
4011**/\r
4012EFI_STATUS\r
4013VariableWriteServiceInitialize (\r
4014 VOID\r
4015 )\r
4016{\r
4017 EFI_STATUS Status;\r
4018 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
4019 UINTN Index;\r
4020 UINT8 Data;\r
8a2d4996 4021 EFI_PHYSICAL_ADDRESS VariableStoreBase;\r
3e02ebb2 4022 EFI_PHYSICAL_ADDRESS NvStorageBase;\r
fa0737a8 4023 VARIABLE_ENTRY_PROPERTY *VariableEntry;\r
8a2d4996 4024\r
31349131
SZ
4025 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
4026\r
3e02ebb2
SZ
4027 NvStorageBase = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);\r
4028 if (NvStorageBase == 0) {\r
4029 NvStorageBase = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);\r
4030 }\r
4031 VariableStoreBase = NvStorageBase + (((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(NvStorageBase))->HeaderLength);\r
4032\r
4033 //\r
4034 // Let NonVolatileVariableBase point to flash variable store base directly after FTW ready.\r
4035 //\r
4036 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase;\r
8a2d4996 4037 VariableStoreHeader = (VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase;\r
fa0737a8 4038\r
8a2d4996 4039 //\r
4040 // Check if the free area is really free.\r
4041 //\r
0f7aff72 4042 for (Index = mVariableModuleGlobal->NonVolatileLastVariableOffset; Index < VariableStoreHeader->Size; Index++) {\r
8a2d4996 4043 Data = ((UINT8 *) mNvVariableCache)[Index];\r
4044 if (Data != 0xff) {\r
4045 //\r
4046 // There must be something wrong in variable store, do reclaim operation.\r
4047 //\r
4048 Status = Reclaim (\r
4049 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
4050 &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
4051 FALSE,\r
335e2681 4052 NULL,\r
7baf3c69
SZ
4053 NULL,\r
4054 0\r
8a2d4996 4055 );\r
4056 if (EFI_ERROR (Status)) {\r
31349131 4057 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
8a2d4996 4058 return Status;\r
4059 }\r
4060 break;\r
4061 }\r
4062 }\r
4063\r
335e2681 4064 FlushHobVariableToFlash (NULL, NULL);\r
0f7aff72 4065\r
fa0737a8
SZ
4066 Status = EFI_SUCCESS;\r
4067 ZeroMem (&mContextOut, sizeof (mContextOut));\r
4068 if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {\r
4069 //\r
4070 // Authenticated variable initialize.\r
4071 //\r
dbd030bb 4072 mContextIn.StructSize = sizeof (AUTH_VAR_LIB_CONTEXT_IN);\r
fa0737a8
SZ
4073 mContextIn.MaxAuthVariableSize = mVariableModuleGlobal->MaxAuthVariableSize - GetVariableHeaderSize ();\r
4074 Status = AuthVariableLibInitialize (&mContextIn, &mContextOut);\r
4075 if (!EFI_ERROR (Status)) {\r
4076 DEBUG ((EFI_D_INFO, "Variable driver will work with auth variable support!\n"));\r
4077 mVariableModuleGlobal->VariableGlobal.AuthSupport = TRUE;\r
4078 if (mContextOut.AuthVarEntry != NULL) {\r
4079 for (Index = 0; Index < mContextOut.AuthVarEntryCount; Index++) {\r
4080 VariableEntry = &mContextOut.AuthVarEntry[Index];\r
4081 Status = InternalVarCheckVariablePropertySet (\r
4082 VariableEntry->Name,\r
4083 VariableEntry->Guid,\r
4084 &VariableEntry->VariableProperty\r
4085 );\r
4086 ASSERT_EFI_ERROR (Status);\r
4087 }\r
4088 }\r
4089 } else if (Status == EFI_UNSUPPORTED) {\r
4090 DEBUG ((EFI_D_INFO, "NOTICE - AuthVariableLibInitialize() returns %r!\n", Status));\r
4091 DEBUG ((EFI_D_INFO, "Variable driver will continue to work without auth variable support!\n"));\r
4092 mVariableModuleGlobal->VariableGlobal.AuthSupport = FALSE;\r
4093 Status = EFI_SUCCESS;\r
4094 }\r
4095 }\r
4096\r
4097 if (!EFI_ERROR (Status)) {\r
4098 for (Index = 0; Index < sizeof (mVariableEntryProperty) / sizeof (mVariableEntryProperty[0]); Index++) {\r
4099 VariableEntry = &mVariableEntryProperty[Index];\r
4100 Status = InternalVarCheckVariablePropertySet (VariableEntry->Name, VariableEntry->Guid, &VariableEntry->VariableProperty);\r
4101 ASSERT_EFI_ERROR (Status);\r
4102 }\r
4103 }\r
4104\r
31349131 4105 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
fa0737a8 4106 return Status;\r
8a2d4996 4107}\r
4108\r
4109\r
4110/**\r
4111 Initializes variable store area for non-volatile and volatile variable.\r
7c80e839 4112\r
4113 @retval EFI_SUCCESS Function successfully executed.\r
4114 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.\r
4115\r
4116**/\r
8d3a5c82 4117EFI_STATUS\r
8d3a5c82 4118VariableCommonInitialize (\r
8a2d4996 4119 VOID\r
8d3a5c82 4120 )\r
8d3a5c82 4121{\r
4122 EFI_STATUS Status;\r
8d3a5c82 4123 VARIABLE_STORE_HEADER *VolatileVariableStore;\r
4124 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
052ad7e1 4125 UINT64 VariableStoreLength;\r
2fcdca1d 4126 UINTN ScratchSize;\r
0f7aff72 4127 EFI_HOB_GUID_TYPE *GuidHob;\r
fa0737a8
SZ
4128 EFI_GUID *VariableGuid;\r
4129 EFI_FIRMWARE_VOLUME_HEADER *NvFvHeader;\r
8d3a5c82 4130\r
7800593d
LG
4131 //\r
4132 // Allocate runtime memory for variable driver global structure.\r
4133 //\r
72399dae 4134 mVariableModuleGlobal = AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL));\r
7800593d
LG
4135 if (mVariableModuleGlobal == NULL) {\r
4136 return EFI_OUT_OF_RESOURCES;\r
4137 }\r
8d3a5c82 4138\r
8a2d4996 4139 InitializeLock (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock, TPL_NOTIFY);\r
8d3a5c82 4140\r
fa0737a8
SZ
4141 //\r
4142 // Init non-volatile variable store.\r
4143 //\r
26c2edd5 4144 NvFvHeader = NULL;\r
fa0737a8
SZ
4145 Status = InitNonVolatileVariableStore (&NvFvHeader);\r
4146 if (EFI_ERROR (Status)) {\r
4147 FreePool (mVariableModuleGlobal);\r
4148 return Status;\r
4149 }\r
4150\r
4151 //\r
4152 // mVariableModuleGlobal->VariableGlobal.AuthFormat\r
4153 // has been initialized in InitNonVolatileVariableStore().\r
4154 //\r
4155 if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {\r
4156 DEBUG ((EFI_D_INFO, "Variable driver will work with auth variable format!\n"));\r
4157 //\r
4158 // Set AuthSupport to FALSE first, VariableWriteServiceInitialize() will initialize it.\r
4159 //\r
4160 mVariableModuleGlobal->VariableGlobal.AuthSupport = FALSE;\r
4161 VariableGuid = &gEfiAuthenticatedVariableGuid;\r
4162 } else {\r
4163 DEBUG ((EFI_D_INFO, "Variable driver will work without auth variable support!\n"));\r
4164 mVariableModuleGlobal->VariableGlobal.AuthSupport = FALSE;\r
4165 VariableGuid = &gEfiVariableGuid;\r
4166 }\r
4167\r
0f7aff72
RN
4168 //\r
4169 // Get HOB variable store.\r
4170 //\r
fa0737a8 4171 GuidHob = GetFirstGuidHob (VariableGuid);\r
0f7aff72 4172 if (GuidHob != NULL) {\r
f68af18e 4173 VariableStoreHeader = GET_GUID_HOB_DATA (GuidHob);\r
335e2681 4174 VariableStoreLength = (UINT64) (GuidHob->Header.HobLength - sizeof (EFI_HOB_GUID_TYPE));\r
f68af18e 4175 if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) {\r
335e2681
SZ
4176 mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) AllocateRuntimeCopyPool ((UINTN) VariableStoreLength, (VOID *) VariableStoreHeader);\r
4177 if (mVariableModuleGlobal->VariableGlobal.HobVariableBase == 0) {\r
fa0737a8 4178 FreePool (NvFvHeader);\r
3e02ebb2 4179 FreePool (mVariableModuleGlobal);\r
335e2681
SZ
4180 return EFI_OUT_OF_RESOURCES;\r
4181 }\r
f68af18e
RN
4182 } else {\r
4183 DEBUG ((EFI_D_ERROR, "HOB Variable Store header is corrupted!\n"));\r
4184 }\r
0f7aff72
RN
4185 }\r
4186\r
8d3a5c82 4187 //\r
2fcdca1d 4188 // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.\r
8d3a5c82 4189 //\r
fa0737a8
SZ
4190 ScratchSize = GetNonVolatileMaxVariableSize ();\r
4191 mVariableModuleGlobal->ScratchBufferSize = ScratchSize;\r
188e4e84 4192 VolatileVariableStore = AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize) + ScratchSize);\r
8d3a5c82 4193 if (VolatileVariableStore == NULL) {\r
3e02ebb2
SZ
4194 if (mVariableModuleGlobal->VariableGlobal.HobVariableBase != 0) {\r
4195 FreePool ((VOID *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase);\r
4196 }\r
fa0737a8 4197 FreePool (NvFvHeader);\r
8d3a5c82 4198 FreePool (mVariableModuleGlobal);\r
4199 return EFI_OUT_OF_RESOURCES;\r
4200 }\r
4201\r
188e4e84 4202 SetMem (VolatileVariableStore, PcdGet32 (PcdVariableStoreSize) + ScratchSize, 0xff);\r
8d3a5c82 4203\r
4204 //\r
8a2d4996 4205 // Initialize Variable Specific Data.\r
8d3a5c82 4206 //\r
052ad7e1 4207 mVariableModuleGlobal->VariableGlobal.VolatileVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VolatileVariableStore;\r
9cad030b 4208 mVariableModuleGlobal->VolatileLastVariableOffset = (UINTN) GetStartPointer (VolatileVariableStore) - (UINTN) VolatileVariableStore;\r
8d3a5c82 4209\r
fa0737a8 4210 CopyGuid (&VolatileVariableStore->Signature, VariableGuid);\r
8a2d4996 4211 VolatileVariableStore->Size = PcdGet32 (PcdVariableStoreSize);\r
4212 VolatileVariableStore->Format = VARIABLE_STORE_FORMATTED;\r
4213 VolatileVariableStore->State = VARIABLE_STORE_HEALTHY;\r
4214 VolatileVariableStore->Reserved = 0;\r
4215 VolatileVariableStore->Reserved1 = 0;\r
8d3a5c82 4216\r
fa0737a8 4217 return EFI_SUCCESS;\r
8d3a5c82 4218}\r
052ad7e1 4219\r
052ad7e1 4220\r
aa75dfec 4221/**\r
8a2d4996 4222 Get the proper fvb handle and/or fvb protocol by the given Flash address.\r
aa75dfec 4223\r
fa0737a8 4224 @param[in] Address The Flash address.\r
8a2d4996 4225 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.\r
4226 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.\r
aa75dfec 4227\r
aa75dfec 4228**/\r
8a2d4996 4229EFI_STATUS\r
4230GetFvbInfoByAddress (\r
4231 IN EFI_PHYSICAL_ADDRESS Address,\r
4232 OUT EFI_HANDLE *FvbHandle OPTIONAL,\r
4233 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvbProtocol OPTIONAL\r
8a9e0b72 4234 )\r
4235{\r
8a2d4996 4236 EFI_STATUS Status;\r
4237 EFI_HANDLE *HandleBuffer;\r
4238 UINTN HandleCount;\r
4239 UINTN Index;\r
4240 EFI_PHYSICAL_ADDRESS FvbBaseAddress;\r
4241 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r
8a2d4996 4242 EFI_FVB_ATTRIBUTES_2 Attributes;\r
1fcbeaea
DG
4243 UINTN BlockSize;\r
4244 UINTN NumberOfBlocks;\r
4245\r
4e1005ec 4246 HandleBuffer = NULL;\r
8a9e0b72 4247 //\r
8a2d4996 4248 // Get all FVB handles.\r
8a9e0b72 4249 //\r
8a2d4996 4250 Status = GetFvbCountAndBuffer (&HandleCount, &HandleBuffer);\r
8a9e0b72 4251 if (EFI_ERROR (Status)) {\r
8a2d4996 4252 return EFI_NOT_FOUND;\r
8a9e0b72 4253 }\r
8a2d4996 4254\r
8a9e0b72 4255 //\r
8a2d4996 4256 // Get the FVB to access variable store.\r
8a9e0b72 4257 //\r
8a2d4996 4258 Fvb = NULL;\r
f0480ecf 4259 for (Index = 0; Index < HandleCount; Index += 1, Status = EFI_NOT_FOUND, Fvb = NULL) {\r
8a2d4996 4260 Status = GetFvbByHandle (HandleBuffer[Index], &Fvb);\r
8a9e0b72 4261 if (EFI_ERROR (Status)) {\r
4262 Status = EFI_NOT_FOUND;\r
4263 break;\r
4264 }\r
4265\r
4266 //\r
4267 // Ensure this FVB protocol supported Write operation.\r
4268 //\r
4269 Status = Fvb->GetAttributes (Fvb, &Attributes);\r
4270 if (EFI_ERROR (Status) || ((Attributes & EFI_FVB2_WRITE_STATUS) == 0)) {\r
1fcbeaea 4271 continue;\r
8a9e0b72 4272 }\r
1fcbeaea 4273\r
8a9e0b72 4274 //\r
8a2d4996 4275 // Compare the address and select the right one.\r
8a9e0b72 4276 //\r
4277 Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);\r
4278 if (EFI_ERROR (Status)) {\r
4279 continue;\r
4280 }\r
4281\r
1fcbeaea
DG
4282 //\r
4283 // Assume one FVB has one type of BlockSize.\r
4284 //\r
4285 Status = Fvb->GetBlockSize (Fvb, 0, &BlockSize, &NumberOfBlocks);\r
4286 if (EFI_ERROR (Status)) {\r
4287 continue;\r
4288 }\r
4289\r
4290 if ((Address >= FvbBaseAddress) && (Address < (FvbBaseAddress + BlockSize * NumberOfBlocks))) {\r
8a2d4996 4291 if (FvbHandle != NULL) {\r
4292 *FvbHandle = HandleBuffer[Index];\r
4293 }\r
4294 if (FvbProtocol != NULL) {\r
4295 *FvbProtocol = Fvb;\r
4296 }\r
4297 Status = EFI_SUCCESS;\r
8a9e0b72 4298 break;\r
4299 }\r
4300 }\r
8a9e0b72 4301 FreePool (HandleBuffer);\r
533020ef 4302\r
8a2d4996 4303 if (Fvb == NULL) {\r
4304 Status = EFI_NOT_FOUND;\r
8a9e0b72 4305 }\r
fa0737a8
SZ
4306\r
4307 return Status;\r
052ad7e1
A
4308}\r
4309\r