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