]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
MdeModulePkg: Add MorLockDxe to variable driver.
[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
4a23636a 802 *VarErrFlag = TempFlag;\r
4edb1866
SZ
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
b2c59ce8
SZ
1194 mVariableModuleGlobal->HwErrVariableTotalSize = 0;\r
1195 mVariableModuleGlobal->CommonVariableTotalSize = 0;\r
1196 mVariableModuleGlobal->CommonUserVariableTotalSize = 0;\r
4edb1866
SZ
1197 Variable = GetStartPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableBase);\r
1198 while (IsValidVariableHeader (Variable, GetEndPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableBase))) {\r
1199 NextVariable = GetNextVariablePtr (Variable);\r
1200 VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
128ef095 1201 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
4edb1866 1202 mVariableModuleGlobal->HwErrVariableTotalSize += VariableSize;\r
128ef095 1203 } else if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
4edb1866
SZ
1204 mVariableModuleGlobal->CommonVariableTotalSize += VariableSize;\r
1205 if (IsUserVariable (Variable)) {\r
1206 mVariableModuleGlobal->CommonUserVariableTotalSize += VariableSize;\r
1207 }\r
128ef095 1208 }\r
8f3a9e58 1209\r
4edb1866 1210 Variable = NextVariable;\r
128ef095 1211 }\r
4edb1866 1212 *LastVariableOffset = (UINTN) Variable - (UINTN) VariableBase;\r
8f3a9e58 1213 }\r
8d3a5c82 1214 }\r
1215\r
7baf3c69 1216Done:\r
128ef095
SZ
1217 if (IsVolatile) {\r
1218 FreePool (ValidBuffer);\r
1219 } else {\r
1220 //\r
1221 // For NV variable reclaim, we use mNvVariableCache as the buffer, so copy the data back.\r
1222 //\r
1223 CopyMem (mNvVariableCache, (UINT8 *)(UINTN)VariableBase, VariableStoreHeader->Size);\r
1224 }\r
814bae52 1225\r
8d3a5c82 1226 return Status;\r
1227}\r
1228\r
0f7aff72
RN
1229/**\r
1230 Find the variable in the specified variable store.\r
1231\r
fa0737a8
SZ
1232 @param[in] VariableName Name of the variable to be found\r
1233 @param[in] VendorGuid Vendor GUID to be found.\r
1234 @param[in] IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute\r
1235 check at runtime when searching variable.\r
1236 @param[in, out] PtrTrack Variable Track Pointer structure that contains Variable Information.\r
0f7aff72 1237\r
fa0737a8
SZ
1238 @retval EFI_SUCCESS Variable found successfully\r
1239 @retval EFI_NOT_FOUND Variable not found\r
0f7aff72
RN
1240**/\r
1241EFI_STATUS\r
1242FindVariableEx (\r
1243 IN CHAR16 *VariableName,\r
1244 IN EFI_GUID *VendorGuid,\r
9622df63 1245 IN BOOLEAN IgnoreRtCheck,\r
0f7aff72
RN
1246 IN OUT VARIABLE_POINTER_TRACK *PtrTrack\r
1247 )\r
1248{\r
1249 VARIABLE_HEADER *InDeletedVariable;\r
1250 VOID *Point;\r
1251\r
23b06935
SZ
1252 PtrTrack->InDeletedTransitionPtr = NULL;\r
1253\r
0f7aff72
RN
1254 //\r
1255 // Find the variable by walk through HOB, volatile and non-volatile variable store.\r
1256 //\r
1257 InDeletedVariable = NULL;\r
1258\r
1259 for ( PtrTrack->CurrPtr = PtrTrack->StartPtr\r
6ebffb67 1260 ; IsValidVariableHeader (PtrTrack->CurrPtr, PtrTrack->EndPtr)\r
0f7aff72
RN
1261 ; PtrTrack->CurrPtr = GetNextVariablePtr (PtrTrack->CurrPtr)\r
1262 ) {\r
fa0737a8 1263 if (PtrTrack->CurrPtr->State == VAR_ADDED ||\r
0f7aff72
RN
1264 PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)\r
1265 ) {\r
9622df63 1266 if (IgnoreRtCheck || !AtRuntime () || ((PtrTrack->CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {\r
0f7aff72
RN
1267 if (VariableName[0] == 0) {\r
1268 if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
1269 InDeletedVariable = PtrTrack->CurrPtr;\r
1270 } else {\r
23b06935 1271 PtrTrack->InDeletedTransitionPtr = InDeletedVariable;\r
0f7aff72
RN
1272 return EFI_SUCCESS;\r
1273 }\r
1274 } else {\r
fa0737a8 1275 if (CompareGuid (VendorGuid, GetVendorGuidPtr (PtrTrack->CurrPtr))) {\r
0f7aff72
RN
1276 Point = (VOID *) GetVariableNamePtr (PtrTrack->CurrPtr);\r
1277\r
1278 ASSERT (NameSizeOfVariable (PtrTrack->CurrPtr) != 0);\r
1279 if (CompareMem (VariableName, Point, NameSizeOfVariable (PtrTrack->CurrPtr)) == 0) {\r
1280 if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
1281 InDeletedVariable = PtrTrack->CurrPtr;\r
1282 } else {\r
23b06935 1283 PtrTrack->InDeletedTransitionPtr = InDeletedVariable;\r
0f7aff72
RN
1284 return EFI_SUCCESS;\r
1285 }\r
1286 }\r
1287 }\r
1288 }\r
1289 }\r
1290 }\r
1291 }\r
1292\r
1293 PtrTrack->CurrPtr = InDeletedVariable;\r
1294 return (PtrTrack->CurrPtr == NULL) ? EFI_NOT_FOUND : EFI_SUCCESS;\r
1295}\r
1296\r
33a5a666 1297\r
7c80e839 1298/**\r
1299 Finds variable in storage blocks of volatile and non-volatile storage areas.\r
1300\r
1301 This code finds variable in storage blocks of volatile and non-volatile storage areas.\r
1302 If VariableName is an empty string, then we just return the first\r
1303 qualified variable without comparing VariableName and VendorGuid.\r
9622df63
SZ
1304 If IgnoreRtCheck is TRUE, then we ignore the EFI_VARIABLE_RUNTIME_ACCESS attribute check\r
1305 at runtime when searching existing variable, only VariableName and VendorGuid are compared.\r
1306 Otherwise, variables without EFI_VARIABLE_RUNTIME_ACCESS are not visible at runtime.\r
7c80e839 1307\r
fa0737a8
SZ
1308 @param[in] VariableName Name of the variable to be found.\r
1309 @param[in] VendorGuid Vendor GUID to be found.\r
1310 @param[out] PtrTrack VARIABLE_POINTER_TRACK structure for output,\r
7c80e839 1311 including the range searched and the target position.\r
fa0737a8 1312 @param[in] Global Pointer to VARIABLE_GLOBAL structure, including\r
7c80e839 1313 base of volatile variable storage area, base of\r
1314 NV variable storage area, and a lock.\r
fa0737a8 1315 @param[in] IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute\r
9622df63 1316 check at runtime when searching variable.\r
7c80e839 1317\r
1318 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while\r
8a2d4996 1319 VendorGuid is NULL.\r
1320 @retval EFI_SUCCESS Variable successfully found.\r
255a3f33 1321 @retval EFI_NOT_FOUND Variable not found\r
33a5a666 1322\r
7c80e839 1323**/\r
8d3a5c82 1324EFI_STATUS\r
8d3a5c82 1325FindVariable (\r
1326 IN CHAR16 *VariableName,\r
1327 IN EFI_GUID *VendorGuid,\r
1328 OUT VARIABLE_POINTER_TRACK *PtrTrack,\r
9622df63
SZ
1329 IN VARIABLE_GLOBAL *Global,\r
1330 IN BOOLEAN IgnoreRtCheck\r
8d3a5c82 1331 )\r
8d3a5c82 1332{\r
0f7aff72
RN
1333 EFI_STATUS Status;\r
1334 VARIABLE_STORE_HEADER *VariableStoreHeader[VariableStoreTypeMax];\r
1335 VARIABLE_STORE_TYPE Type;\r
1336\r
1337 if (VariableName[0] != 0 && VendorGuid == NULL) {\r
1338 return EFI_INVALID_PARAMETER;\r
1339 }\r
8d3a5c82 1340\r
8d3a5c82 1341 //\r
0f7aff72 1342 // 0: Volatile, 1: HOB, 2: Non-Volatile.\r
36873a61 1343 // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName\r
8a2d4996 1344 // make use of this mapping to implement search algorithm.\r
8d3a5c82 1345 //\r
0f7aff72
RN
1346 VariableStoreHeader[VariableStoreTypeVolatile] = (VARIABLE_STORE_HEADER *) (UINTN) Global->VolatileVariableBase;\r
1347 VariableStoreHeader[VariableStoreTypeHob] = (VARIABLE_STORE_HEADER *) (UINTN) Global->HobVariableBase;\r
1348 VariableStoreHeader[VariableStoreTypeNv] = mNvVariableCache;\r
8d3a5c82 1349\r
1350 //\r
0f7aff72 1351 // Find the variable by walk through HOB, volatile and non-volatile variable store.\r
8d3a5c82 1352 //\r
0f7aff72
RN
1353 for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {\r
1354 if (VariableStoreHeader[Type] == NULL) {\r
1355 continue;\r
1356 }\r
814bae52 1357\r
0f7aff72
RN
1358 PtrTrack->StartPtr = GetStartPointer (VariableStoreHeader[Type]);\r
1359 PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader[Type]);\r
1360 PtrTrack->Volatile = (BOOLEAN) (Type == VariableStoreTypeVolatile);\r
8d3a5c82 1361\r
9622df63 1362 Status = FindVariableEx (VariableName, VendorGuid, IgnoreRtCheck, PtrTrack);\r
0f7aff72
RN
1363 if (!EFI_ERROR (Status)) {\r
1364 return Status;\r
814bae52 1365 }\r
8d3a5c82 1366 }\r
8d3a5c82 1367 return EFI_NOT_FOUND;\r
1368}\r
1369\r
7c80e839 1370/**\r
72399dae 1371 Get index from supported language codes according to language string.\r
1372\r
1373 This code is used to get corresponding index in supported language codes. It can handle\r
0254efc0 1374 RFC4646 and ISO639 language tags.\r
72399dae 1375 In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.\r
0254efc0 1376 In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.\r
72399dae 1377\r
1378 For example:\r
1379 SupportedLang = "engfraengfra"\r
1380 Lang = "eng"\r
1381 Iso639Language = TRUE\r
1382 The return value is "0".\r
1383 Another example:\r
1384 SupportedLang = "en;fr;en-US;fr-FR"\r
1385 Lang = "fr-FR"\r
1386 Iso639Language = FALSE\r
1387 The return value is "3".\r
1388\r
1389 @param SupportedLang Platform supported language codes.\r
1390 @param Lang Configured language.\r
0254efc0 1391 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.\r
72399dae 1392\r
8a2d4996 1393 @retval The index of language in the language codes.\r
8d3a5c82 1394\r
7c80e839 1395**/\r
72399dae 1396UINTN\r
72399dae 1397GetIndexFromSupportedLangCodes(\r
1398 IN CHAR8 *SupportedLang,\r
1399 IN CHAR8 *Lang,\r
1400 IN BOOLEAN Iso639Language\r
fa0737a8 1401 )\r
8d3a5c82 1402{\r
72399dae 1403 UINTN Index;\r
255a3f33
RN
1404 UINTN CompareLength;\r
1405 UINTN LanguageLength;\r
72399dae 1406\r
72399dae 1407 if (Iso639Language) {\r
255a3f33 1408 CompareLength = ISO_639_2_ENTRY_SIZE;\r
72399dae 1409 for (Index = 0; Index < AsciiStrLen (SupportedLang); Index += CompareLength) {\r
1410 if (AsciiStrnCmp (Lang, SupportedLang + Index, CompareLength) == 0) {\r
1411 //\r
1412 // Successfully find the index of Lang string in SupportedLang string.\r
1413 //\r
1414 Index = Index / CompareLength;\r
1415 return Index;\r
1416 }\r
1417 }\r
1418 ASSERT (FALSE);\r
1419 return 0;\r
1420 } else {\r
1421 //\r
0254efc0 1422 // Compare RFC4646 language code\r
72399dae 1423 //\r
255a3f33
RN
1424 Index = 0;\r
1425 for (LanguageLength = 0; Lang[LanguageLength] != '\0'; LanguageLength++);\r
1426\r
1427 for (Index = 0; *SupportedLang != '\0'; Index++, SupportedLang += CompareLength) {\r
72399dae 1428 //\r
255a3f33 1429 // Skip ';' characters in SupportedLang\r
72399dae 1430 //\r
255a3f33
RN
1431 for (; *SupportedLang != '\0' && *SupportedLang == ';'; SupportedLang++);\r
1432 //\r
1433 // Determine the length of the next language code in SupportedLang\r
1434 //\r
1435 for (CompareLength = 0; SupportedLang[CompareLength] != '\0' && SupportedLang[CompareLength] != ';'; CompareLength++);\r
fa0737a8
SZ
1436\r
1437 if ((CompareLength == LanguageLength) &&\r
255a3f33 1438 (AsciiStrnCmp (Lang, SupportedLang, CompareLength) == 0)) {\r
72399dae 1439 //\r
1440 // Successfully find the index of Lang string in SupportedLang string.\r
1441 //\r
1442 return Index;\r
1443 }\r
72399dae 1444 }\r
1445 ASSERT (FALSE);\r
1446 return 0;\r
8d3a5c82 1447 }\r
72399dae 1448}\r
33a5a666 1449\r
72399dae 1450/**\r
1451 Get language string from supported language codes according to index.\r
1452\r
8a2d4996 1453 This code is used to get corresponding language strings in supported language codes. It can handle\r
0254efc0 1454 RFC4646 and ISO639 language tags.\r
72399dae 1455 In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.\r
0254efc0 1456 In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.\r
72399dae 1457\r
1458 For example:\r
1459 SupportedLang = "engfraengfra"\r
1460 Index = "1"\r
1461 Iso639Language = TRUE\r
1462 The return value is "fra".\r
1463 Another example:\r
1464 SupportedLang = "en;fr;en-US;fr-FR"\r
1465 Index = "1"\r
1466 Iso639Language = FALSE\r
1467 The return value is "fr".\r
1468\r
1469 @param SupportedLang Platform supported language codes.\r
8a2d4996 1470 @param Index The index in supported language codes.\r
0254efc0 1471 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.\r
72399dae 1472\r
8a2d4996 1473 @retval The language string in the language codes.\r
8d3a5c82 1474\r
72399dae 1475**/\r
1476CHAR8 *\r
72399dae 1477GetLangFromSupportedLangCodes (\r
1478 IN CHAR8 *SupportedLang,\r
1479 IN UINTN Index,\r
1480 IN BOOLEAN Iso639Language\r
1481)\r
1482{\r
1483 UINTN SubIndex;\r
255a3f33 1484 UINTN CompareLength;\r
72399dae 1485 CHAR8 *Supported;\r
8d3a5c82 1486\r
72399dae 1487 SubIndex = 0;\r
1488 Supported = SupportedLang;\r
1489 if (Iso639Language) {\r
1490 //\r
8a2d4996 1491 // According to the index of Lang string in SupportedLang string to get the language.\r
1492 // This code will be invoked in RUNTIME, therefore there is not a memory allocate/free operation.\r
72399dae 1493 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.\r
1494 //\r
255a3f33
RN
1495 CompareLength = ISO_639_2_ENTRY_SIZE;\r
1496 mVariableModuleGlobal->Lang[CompareLength] = '\0';\r
72399dae 1497 return CopyMem (mVariableModuleGlobal->Lang, SupportedLang + Index * CompareLength, CompareLength);\r
f68af18e 1498\r
8d3a5c82 1499 } else {\r
72399dae 1500 while (TRUE) {\r
1501 //\r
8a2d4996 1502 // Take semicolon as delimitation, sequentially traverse supported language codes.\r
72399dae 1503 //\r
1504 for (CompareLength = 0; *Supported != ';' && *Supported != '\0'; CompareLength++) {\r
1505 Supported++;\r
1506 }\r
1507 if ((*Supported == '\0') && (SubIndex != Index)) {\r
1508 //\r
1509 // Have completed the traverse, but not find corrsponding string.\r
1510 // This case is not allowed to happen.\r
1511 //\r
1512 ASSERT(FALSE);\r
1513 return NULL;\r
1514 }\r
1515 if (SubIndex == Index) {\r
1516 //\r
8a2d4996 1517 // According to the index of Lang string in SupportedLang string to get the language.\r
72399dae 1518 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.\r
1519 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.\r
1520 //\r
255a3f33 1521 mVariableModuleGlobal->PlatformLang[CompareLength] = '\0';\r
72399dae 1522 return CopyMem (mVariableModuleGlobal->PlatformLang, Supported - CompareLength, CompareLength);\r
1523 }\r
1524 SubIndex++;\r
8a2d4996 1525\r
5c033766
RN
1526 //\r
1527 // Skip ';' characters in Supported\r
1528 //\r
1529 for (; *Supported != '\0' && *Supported == ';'; Supported++);\r
72399dae 1530 }\r
8d3a5c82 1531 }\r
8d3a5c82 1532}\r
1533\r
255a3f33 1534/**\r
fa0737a8
SZ
1535 Returns a pointer to an allocated buffer that contains the best matching language\r
1536 from a set of supported languages.\r
1537\r
1538 This function supports both ISO 639-2 and RFC 4646 language codes, but language\r
255a3f33
RN
1539 code types may not be mixed in a single call to this function. This function\r
1540 supports a variable argument list that allows the caller to pass in a prioritized\r
1541 list of language codes to test against all the language codes in SupportedLanguages.\r
1542\r
1543 If SupportedLanguages is NULL, then ASSERT().\r
1544\r
1545 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that\r
fa0737a8 1546 contains a set of language codes in the format\r
255a3f33
RN
1547 specified by Iso639Language.\r
1548 @param[in] Iso639Language If TRUE, then all language codes are assumed to be\r
1549 in ISO 639-2 format. If FALSE, then all language\r
1550 codes are assumed to be in RFC 4646 language format\r
fa0737a8 1551 @param[in] ... A variable argument list that contains pointers to\r
255a3f33
RN
1552 Null-terminated ASCII strings that contain one or more\r
1553 language codes in the format specified by Iso639Language.\r
1554 The first language code from each of these language\r
1555 code lists is used to determine if it is an exact or\r
fa0737a8 1556 close match to any of the language codes in\r
255a3f33
RN
1557 SupportedLanguages. Close matches only apply to RFC 4646\r
1558 language codes, and the matching algorithm from RFC 4647\r
fa0737a8 1559 is used to determine if a close match is present. If\r
255a3f33
RN
1560 an exact or close match is found, then the matching\r
1561 language code from SupportedLanguages is returned. If\r
1562 no matches are found, then the next variable argument\r
fa0737a8 1563 parameter is evaluated. The variable argument list\r
255a3f33
RN
1564 is terminated by a NULL.\r
1565\r
1566 @retval NULL The best matching language could not be found in SupportedLanguages.\r
fa0737a8 1567 @retval NULL There are not enough resources available to return the best matching\r
255a3f33 1568 language.\r
fa0737a8 1569 @retval Other A pointer to a Null-terminated ASCII string that is the best matching\r
255a3f33
RN
1570 language in SupportedLanguages.\r
1571\r
1572**/\r
1573CHAR8 *\r
e1adae60 1574EFIAPI\r
255a3f33 1575VariableGetBestLanguage (\r
fa0737a8 1576 IN CONST CHAR8 *SupportedLanguages,\r
255a3f33
RN
1577 IN BOOLEAN Iso639Language,\r
1578 ...\r
1579 )\r
1580{\r
1581 VA_LIST Args;\r
1582 CHAR8 *Language;\r
1583 UINTN CompareLength;\r
1584 UINTN LanguageLength;\r
1585 CONST CHAR8 *Supported;\r
1586 CHAR8 *Buffer;\r
1587\r
fa0737a8
SZ
1588 if (SupportedLanguages == NULL) {\r
1589 return NULL;\r
1590 }\r
255a3f33
RN
1591\r
1592 VA_START (Args, Iso639Language);\r
1593 while ((Language = VA_ARG (Args, CHAR8 *)) != NULL) {\r
1594 //\r
1595 // Default to ISO 639-2 mode\r
1596 //\r
1597 CompareLength = 3;\r
1598 LanguageLength = MIN (3, AsciiStrLen (Language));\r
1599\r
1600 //\r
1601 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language\r
1602 //\r
1603 if (!Iso639Language) {\r
1604 for (LanguageLength = 0; Language[LanguageLength] != 0 && Language[LanguageLength] != ';'; LanguageLength++);\r
1605 }\r
1606\r
1607 //\r
1608 // Trim back the length of Language used until it is empty\r
1609 //\r
1610 while (LanguageLength > 0) {\r
1611 //\r
1612 // Loop through all language codes in SupportedLanguages\r
1613 //\r
1614 for (Supported = SupportedLanguages; *Supported != '\0'; Supported += CompareLength) {\r
1615 //\r
1616 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages\r
1617 //\r
1618 if (!Iso639Language) {\r
1619 //\r
1620 // Skip ';' characters in Supported\r
1621 //\r
1622 for (; *Supported != '\0' && *Supported == ';'; Supported++);\r
1623 //\r
1624 // Determine the length of the next language code in Supported\r
1625 //\r
1626 for (CompareLength = 0; Supported[CompareLength] != 0 && Supported[CompareLength] != ';'; CompareLength++);\r
1627 //\r
1628 // If Language is longer than the Supported, then skip to the next language\r
1629 //\r
1630 if (LanguageLength > CompareLength) {\r
1631 continue;\r
1632 }\r
1633 }\r
1634 //\r
1635 // See if the first LanguageLength characters in Supported match Language\r
1636 //\r
1637 if (AsciiStrnCmp (Supported, Language, LanguageLength) == 0) {\r
1638 VA_END (Args);\r
1639\r
1640 Buffer = Iso639Language ? mVariableModuleGlobal->Lang : mVariableModuleGlobal->PlatformLang;\r
1641 Buffer[CompareLength] = '\0';\r
1642 return CopyMem (Buffer, Supported, CompareLength);\r
1643 }\r
1644 }\r
1645\r
1646 if (Iso639Language) {\r
1647 //\r
1648 // If ISO 639 mode, then each language can only be tested once\r
1649 //\r
1650 LanguageLength = 0;\r
1651 } else {\r
1652 //\r
fa0737a8 1653 // If RFC 4646 mode, then trim Language from the right to the next '-' character\r
255a3f33
RN
1654 //\r
1655 for (LanguageLength--; LanguageLength > 0 && Language[LanguageLength] != '-'; LanguageLength--);\r
1656 }\r
1657 }\r
1658 }\r
1659 VA_END (Args);\r
1660\r
1661 //\r
fa0737a8 1662 // No matches were found\r
255a3f33
RN
1663 //\r
1664 return NULL;\r
1665}\r
1666\r
b2bd493e
SZ
1667/**\r
1668 This function is to check if the remaining variable space is enough to set\r
1669 all Variables from argument list successfully. The purpose of the check\r
1670 is to keep the consistency of the Variables to be in variable storage.\r
1671\r
1672 Note: Variables are assumed to be in same storage.\r
1673 The set sequence of Variables will be same with the sequence of VariableEntry from argument list,\r
1674 so follow the argument sequence to check the Variables.\r
1675\r
1676 @param[in] Attributes Variable attributes for Variable entries.\r
fa0737a8
SZ
1677 @param[in] Marker VA_LIST style variable argument list.\r
1678 The variable argument list with type VARIABLE_ENTRY_CONSISTENCY *.\r
1679 A NULL terminates the list. The VariableSize of\r
9a12e582
DG
1680 VARIABLE_ENTRY_CONSISTENCY is the variable data size as input.\r
1681 It will be changed to variable total size as output.\r
b2bd493e
SZ
1682\r
1683 @retval TRUE Have enough variable space to set the Variables successfully.\r
1684 @retval FALSE No enough variable space to set the Variables successfully.\r
1685\r
1686**/\r
1687BOOLEAN\r
1688EFIAPI\r
fa0737a8 1689CheckRemainingSpaceForConsistencyInternal (\r
b2bd493e 1690 IN UINT32 Attributes,\r
fa0737a8 1691 IN VA_LIST Marker\r
b2bd493e
SZ
1692 )\r
1693{\r
1694 EFI_STATUS Status;\r
1695 VA_LIST Args;\r
1696 VARIABLE_ENTRY_CONSISTENCY *VariableEntry;\r
1697 UINT64 MaximumVariableStorageSize;\r
1698 UINT64 RemainingVariableStorageSize;\r
1699 UINT64 MaximumVariableSize;\r
1700 UINTN TotalNeededSize;\r
1701 UINTN OriginalVarSize;\r
1702 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
1703 VARIABLE_POINTER_TRACK VariablePtrTrack;\r
1704 VARIABLE_HEADER *NextVariable;\r
9a12e582
DG
1705 UINTN VarNameSize;\r
1706 UINTN VarDataSize;\r
b2bd493e
SZ
1707\r
1708 //\r
1709 // Non-Volatile related.\r
1710 //\r
1711 VariableStoreHeader = mNvVariableCache;\r
1712\r
1713 Status = VariableServiceQueryVariableInfoInternal (\r
1714 Attributes,\r
1715 &MaximumVariableStorageSize,\r
1716 &RemainingVariableStorageSize,\r
1717 &MaximumVariableSize\r
1718 );\r
1719 ASSERT_EFI_ERROR (Status);\r
1720\r
1721 TotalNeededSize = 0;\r
b7789dc6 1722 VA_COPY (Args, Marker);\r
b2bd493e
SZ
1723 VariableEntry = VA_ARG (Args, VARIABLE_ENTRY_CONSISTENCY *);\r
1724 while (VariableEntry != NULL) {\r
9a12e582
DG
1725 //\r
1726 // Calculate variable total size.\r
1727 //\r
1728 VarNameSize = StrSize (VariableEntry->Name);\r
1729 VarNameSize += GET_PAD_SIZE (VarNameSize);\r
1730 VarDataSize = VariableEntry->VariableSize;\r
1731 VarDataSize += GET_PAD_SIZE (VarDataSize);\r
fa0737a8 1732 VariableEntry->VariableSize = HEADER_ALIGN (GetVariableHeaderSize () + VarNameSize + VarDataSize);\r
9a12e582 1733\r
b2bd493e
SZ
1734 TotalNeededSize += VariableEntry->VariableSize;\r
1735 VariableEntry = VA_ARG (Args, VARIABLE_ENTRY_CONSISTENCY *);\r
1736 }\r
b2bd493e
SZ
1737\r
1738 if (RemainingVariableStorageSize >= TotalNeededSize) {\r
1739 //\r
1740 // Already have enough space.\r
1741 //\r
1742 return TRUE;\r
1743 } else if (AtRuntime ()) {\r
1744 //\r
1745 // At runtime, no reclaim.\r
1746 // The original variable space of Variables can't be reused.\r
1747 //\r
1748 return FALSE;\r
1749 }\r
1750\r
b7789dc6 1751 VA_COPY (Args, Marker);\r
b2bd493e
SZ
1752 VariableEntry = VA_ARG (Args, VARIABLE_ENTRY_CONSISTENCY *);\r
1753 while (VariableEntry != NULL) {\r
1754 //\r
1755 // Check if Variable[Index] has been present and get its size.\r
1756 //\r
1757 OriginalVarSize = 0;\r
1758 VariablePtrTrack.StartPtr = GetStartPointer (VariableStoreHeader);\r
1759 VariablePtrTrack.EndPtr = GetEndPointer (VariableStoreHeader);\r
1760 Status = FindVariableEx (\r
1761 VariableEntry->Name,\r
1762 VariableEntry->Guid,\r
1763 FALSE,\r
1764 &VariablePtrTrack\r
1765 );\r
1766 if (!EFI_ERROR (Status)) {\r
1767 //\r
1768 // Get size of Variable[Index].\r
1769 //\r
1770 NextVariable = GetNextVariablePtr (VariablePtrTrack.CurrPtr);\r
1771 OriginalVarSize = (UINTN) NextVariable - (UINTN) VariablePtrTrack.CurrPtr;\r
1772 //\r
1773 // Add the original size of Variable[Index] to remaining variable storage size.\r
1774 //\r
1775 RemainingVariableStorageSize += OriginalVarSize;\r
1776 }\r
1777 if (VariableEntry->VariableSize > RemainingVariableStorageSize) {\r
1778 //\r
1779 // No enough space for Variable[Index].\r
1780 //\r
b2bd493e
SZ
1781 return FALSE;\r
1782 }\r
1783 //\r
1784 // Sub the (new) size of Variable[Index] from remaining variable storage size.\r
1785 //\r
1786 RemainingVariableStorageSize -= VariableEntry->VariableSize;\r
1787 VariableEntry = VA_ARG (Args, VARIABLE_ENTRY_CONSISTENCY *);\r
1788 }\r
b2bd493e
SZ
1789\r
1790 return TRUE;\r
1791}\r
1792\r
fa0737a8
SZ
1793/**\r
1794 This function is to check if the remaining variable space is enough to set\r
1795 all Variables from argument list successfully. The purpose of the check\r
1796 is to keep the consistency of the Variables to be in variable storage.\r
1797\r
1798 Note: Variables are assumed to be in same storage.\r
1799 The set sequence of Variables will be same with the sequence of VariableEntry from argument list,\r
1800 so follow the argument sequence to check the Variables.\r
1801\r
1802 @param[in] Attributes Variable attributes for Variable entries.\r
1803 @param ... The variable argument list with type VARIABLE_ENTRY_CONSISTENCY *.\r
1804 A NULL terminates the list. The VariableSize of\r
1805 VARIABLE_ENTRY_CONSISTENCY is the variable data size as input.\r
1806 It will be changed to variable total size as output.\r
1807\r
1808 @retval TRUE Have enough variable space to set the Variables successfully.\r
1809 @retval FALSE No enough variable space to set the Variables successfully.\r
1810\r
1811**/\r
1812BOOLEAN\r
1813EFIAPI\r
1814CheckRemainingSpaceForConsistency (\r
1815 IN UINT32 Attributes,\r
1816 ...\r
1817 )\r
1818{\r
1819 VA_LIST Marker;\r
1820 BOOLEAN Return;\r
1821\r
1822 VA_START (Marker, Attributes);\r
1823\r
1824 Return = CheckRemainingSpaceForConsistencyInternal (Attributes, Marker);\r
1825\r
1826 VA_END (Marker);\r
1827\r
1828 return Return;\r
1829}\r
1830\r
72399dae 1831/**\r
1832 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.\r
052ad7e1 1833\r
72399dae 1834 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.\r
052ad7e1 1835\r
72399dae 1836 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,\r
1837 and are read-only. Therefore, in variable driver, only store the original value for other use.\r
8d3a5c82 1838\r
8a2d4996 1839 @param[in] VariableName Name of variable.\r
8d3a5c82 1840\r
8a2d4996 1841 @param[in] Data Variable data.\r
8d3a5c82 1842\r
8a2d4996 1843 @param[in] DataSize Size of data. 0 means delete.\r
72399dae 1844\r
9bc5dabb
SZ
1845 @retval EFI_SUCCESS The update operation is successful or ignored.\r
1846 @retval EFI_WRITE_PROTECTED Update PlatformLangCodes/LangCodes at runtime.\r
1847 @retval EFI_OUT_OF_RESOURCES No enough variable space to do the update operation.\r
1848 @retval Others Other errors happened during the update operation.\r
1849\r
7c80e839 1850**/\r
9bc5dabb 1851EFI_STATUS\r
d6550260 1852AutoUpdateLangVariable (\r
72399dae 1853 IN CHAR16 *VariableName,\r
1854 IN VOID *Data,\r
1855 IN UINTN DataSize\r
052ad7e1 1856 )\r
8d3a5c82 1857{\r
255a3f33
RN
1858 EFI_STATUS Status;\r
1859 CHAR8 *BestPlatformLang;\r
1860 CHAR8 *BestLang;\r
1861 UINTN Index;\r
1862 UINT32 Attributes;\r
72399dae 1863 VARIABLE_POINTER_TRACK Variable;\r
255a3f33 1864 BOOLEAN SetLanguageCodes;\r
b2bd493e 1865 VARIABLE_ENTRY_CONSISTENCY VariableEntry[2];\r
8d3a5c82 1866\r
72399dae 1867 //\r
255a3f33 1868 // Don't do updates for delete operation\r
72399dae 1869 //\r
255a3f33 1870 if (DataSize == 0) {\r
9bc5dabb 1871 return EFI_SUCCESS;\r
255a3f33
RN
1872 }\r
1873\r
1874 SetLanguageCodes = FALSE;\r
8d3a5c82 1875\r
6675a21f 1876 if (StrCmp (VariableName, EFI_PLATFORM_LANG_CODES_VARIABLE_NAME) == 0) {\r
255a3f33
RN
1877 //\r
1878 // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.\r
1879 //\r
8a2d4996 1880 if (AtRuntime ()) {\r
9bc5dabb 1881 return EFI_WRITE_PROTECTED;\r
255a3f33
RN
1882 }\r
1883\r
1884 SetLanguageCodes = TRUE;\r
1885\r
72399dae 1886 //\r
1887 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only\r
1888 // Therefore, in variable driver, only store the original value for other use.\r
1889 //\r
255a3f33
RN
1890 if (mVariableModuleGlobal->PlatformLangCodes != NULL) {\r
1891 FreePool (mVariableModuleGlobal->PlatformLangCodes);\r
1892 }\r
1893 mVariableModuleGlobal->PlatformLangCodes = AllocateRuntimeCopyPool (DataSize, Data);\r
1894 ASSERT (mVariableModuleGlobal->PlatformLangCodes != NULL);\r
1895\r
72399dae 1896 //\r
fa0737a8 1897 // PlatformLang holds a single language from PlatformLangCodes,\r
255a3f33 1898 // so the size of PlatformLangCodes is enough for the PlatformLang.\r
72399dae 1899 //\r
255a3f33
RN
1900 if (mVariableModuleGlobal->PlatformLang != NULL) {\r
1901 FreePool (mVariableModuleGlobal->PlatformLang);\r
1902 }\r
1903 mVariableModuleGlobal->PlatformLang = AllocateRuntimePool (DataSize);\r
1904 ASSERT (mVariableModuleGlobal->PlatformLang != NULL);\r
fdb7765f 1905\r
6675a21f 1906 } else if (StrCmp (VariableName, EFI_LANG_CODES_VARIABLE_NAME) == 0) {\r
72399dae 1907 //\r
255a3f33 1908 // LangCodes is a volatile variable, so it can not be updated at runtime.\r
72399dae 1909 //\r
8a2d4996 1910 if (AtRuntime ()) {\r
9bc5dabb 1911 return EFI_WRITE_PROTECTED;\r
255a3f33
RN
1912 }\r
1913\r
1914 SetLanguageCodes = TRUE;\r
8d3a5c82 1915\r
8d3a5c82 1916 //\r
255a3f33
RN
1917 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only\r
1918 // Therefore, in variable driver, only store the original value for other use.\r
8d3a5c82 1919 //\r
255a3f33
RN
1920 if (mVariableModuleGlobal->LangCodes != NULL) {\r
1921 FreePool (mVariableModuleGlobal->LangCodes);\r
1922 }\r
1923 mVariableModuleGlobal->LangCodes = AllocateRuntimeCopyPool (DataSize, Data);\r
1924 ASSERT (mVariableModuleGlobal->LangCodes != NULL);\r
1925 }\r
8d3a5c82 1926\r
fa0737a8 1927 if (SetLanguageCodes\r
255a3f33
RN
1928 && (mVariableModuleGlobal->PlatformLangCodes != NULL)\r
1929 && (mVariableModuleGlobal->LangCodes != NULL)) {\r
8d3a5c82 1930 //\r
255a3f33
RN
1931 // Update Lang if PlatformLang is already set\r
1932 // Update PlatformLang if Lang is already set\r
8d3a5c82 1933 //\r
6675a21f 1934 Status = FindVariable (EFI_PLATFORM_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
255a3f33
RN
1935 if (!EFI_ERROR (Status)) {\r
1936 //\r
1937 // Update Lang\r
1938 //\r
6675a21f 1939 VariableName = EFI_PLATFORM_LANG_VARIABLE_NAME;\r
255a3f33 1940 Data = GetVariableDataPtr (Variable.CurrPtr);\r
fa0737a8 1941 DataSize = DataSizeOfVariable (Variable.CurrPtr);\r
255a3f33 1942 } else {\r
6675a21f 1943 Status = FindVariable (EFI_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
255a3f33
RN
1944 if (!EFI_ERROR (Status)) {\r
1945 //\r
1946 // Update PlatformLang\r
1947 //\r
6675a21f 1948 VariableName = EFI_LANG_VARIABLE_NAME;\r
255a3f33 1949 Data = GetVariableDataPtr (Variable.CurrPtr);\r
fa0737a8 1950 DataSize = DataSizeOfVariable (Variable.CurrPtr);\r
255a3f33
RN
1951 } else {\r
1952 //\r
1953 // Neither PlatformLang nor Lang is set, directly return\r
1954 //\r
9bc5dabb 1955 return EFI_SUCCESS;\r
255a3f33
RN
1956 }\r
1957 }\r
1958 }\r
9bc5dabb
SZ
1959\r
1960 Status = EFI_SUCCESS;\r
1961\r
255a3f33
RN
1962 //\r
1963 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.\r
1964 //\r
1965 Attributes = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;\r
8d3a5c82 1966\r
6675a21f 1967 if (StrCmp (VariableName, EFI_PLATFORM_LANG_VARIABLE_NAME) == 0) {\r
8d3a5c82 1968 //\r
255a3f33 1969 // Update Lang when PlatformLangCodes/LangCodes were set.\r
8d3a5c82 1970 //\r
255a3f33
RN
1971 if ((mVariableModuleGlobal->PlatformLangCodes != NULL) && (mVariableModuleGlobal->LangCodes != NULL)) {\r
1972 //\r
1973 // When setting PlatformLang, firstly get most matched language string from supported language codes.\r
1974 //\r
1975 BestPlatformLang = VariableGetBestLanguage (mVariableModuleGlobal->PlatformLangCodes, FALSE, Data, NULL);\r
1976 if (BestPlatformLang != NULL) {\r
1977 //\r
1978 // Get the corresponding index in language codes.\r
1979 //\r
1980 Index = GetIndexFromSupportedLangCodes (mVariableModuleGlobal->PlatformLangCodes, BestPlatformLang, FALSE);\r
fdb7765f 1981\r
255a3f33
RN
1982 //\r
1983 // Get the corresponding ISO639 language tag according to RFC4646 language tag.\r
1984 //\r
1985 BestLang = GetLangFromSupportedLangCodes (mVariableModuleGlobal->LangCodes, Index, TRUE);\r
8d3a5c82 1986\r
255a3f33 1987 //\r
9a12e582 1988 // Check the variable space for both Lang and PlatformLang variable.\r
b2bd493e 1989 //\r
9a12e582 1990 VariableEntry[0].VariableSize = ISO_639_2_ENTRY_SIZE + 1;\r
b2bd493e
SZ
1991 VariableEntry[0].Guid = &gEfiGlobalVariableGuid;\r
1992 VariableEntry[0].Name = EFI_LANG_VARIABLE_NAME;\r
fa0737a8 1993\r
9a12e582 1994 VariableEntry[1].VariableSize = AsciiStrSize (BestPlatformLang);\r
b2bd493e
SZ
1995 VariableEntry[1].Guid = &gEfiGlobalVariableGuid;\r
1996 VariableEntry[1].Name = EFI_PLATFORM_LANG_VARIABLE_NAME;\r
1997 if (!CheckRemainingSpaceForConsistency (VARIABLE_ATTRIBUTE_NV_BS_RT, &VariableEntry[0], &VariableEntry[1], NULL)) {\r
1998 //\r
1999 // No enough variable space to set both Lang and PlatformLang successfully.\r
2000 //\r
2001 Status = EFI_OUT_OF_RESOURCES;\r
2002 } else {\r
2003 //\r
2004 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.\r
2005 //\r
2006 FindVariable (EFI_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
8d3a5c82 2007\r
b2bd493e 2008 Status = UpdateVariable (EFI_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, BestLang,\r
fa0737a8 2009 ISO_639_2_ENTRY_SIZE + 1, Attributes, 0, 0, &Variable, NULL);\r
b2bd493e 2010 }\r
8d3a5c82 2011\r
b2bd493e 2012 DEBUG ((EFI_D_INFO, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a Status: %r\n", BestPlatformLang, BestLang, Status));\r
255a3f33
RN
2013 }\r
2014 }\r
72399dae 2015\r
6675a21f 2016 } else if (StrCmp (VariableName, EFI_LANG_VARIABLE_NAME) == 0) {\r
72399dae 2017 //\r
255a3f33 2018 // Update PlatformLang when PlatformLangCodes/LangCodes were set.\r
72399dae 2019 //\r
255a3f33
RN
2020 if ((mVariableModuleGlobal->PlatformLangCodes != NULL) && (mVariableModuleGlobal->LangCodes != NULL)) {\r
2021 //\r
2022 // When setting Lang, firstly get most matched language string from supported language codes.\r
2023 //\r
2024 BestLang = VariableGetBestLanguage (mVariableModuleGlobal->LangCodes, TRUE, Data, NULL);\r
2025 if (BestLang != NULL) {\r
2026 //\r
2027 // Get the corresponding index in language codes.\r
2028 //\r
2029 Index = GetIndexFromSupportedLangCodes (mVariableModuleGlobal->LangCodes, BestLang, TRUE);\r
72399dae 2030\r
255a3f33
RN
2031 //\r
2032 // Get the corresponding RFC4646 language tag according to ISO639 language tag.\r
2033 //\r
2034 BestPlatformLang = GetLangFromSupportedLangCodes (mVariableModuleGlobal->PlatformLangCodes, Index, FALSE);\r
2035\r
2036 //\r
9a12e582 2037 // Check the variable space for both PlatformLang and Lang variable.\r
b2bd493e 2038 //\r
9a12e582 2039 VariableEntry[0].VariableSize = AsciiStrSize (BestPlatformLang);\r
b2bd493e
SZ
2040 VariableEntry[0].Guid = &gEfiGlobalVariableGuid;\r
2041 VariableEntry[0].Name = EFI_PLATFORM_LANG_VARIABLE_NAME;\r
9a12e582
DG
2042\r
2043 VariableEntry[1].VariableSize = ISO_639_2_ENTRY_SIZE + 1;\r
b2bd493e
SZ
2044 VariableEntry[1].Guid = &gEfiGlobalVariableGuid;\r
2045 VariableEntry[1].Name = EFI_LANG_VARIABLE_NAME;\r
2046 if (!CheckRemainingSpaceForConsistency (VARIABLE_ATTRIBUTE_NV_BS_RT, &VariableEntry[0], &VariableEntry[1], NULL)) {\r
2047 //\r
2048 // No enough variable space to set both PlatformLang and Lang successfully.\r
2049 //\r
2050 Status = EFI_OUT_OF_RESOURCES;\r
2051 } else {\r
2052 //\r
2053 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.\r
2054 //\r
2055 FindVariable (EFI_PLATFORM_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
72399dae 2056\r
fa0737a8
SZ
2057 Status = UpdateVariable (EFI_PLATFORM_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, BestPlatformLang,\r
2058 AsciiStrSize (BestPlatformLang), Attributes, 0, 0, &Variable, NULL);\r
b2bd493e 2059 }\r
72399dae 2060\r
9bc5dabb 2061 DEBUG ((EFI_D_INFO, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a Status: %r\n", BestLang, BestPlatformLang, Status));\r
255a3f33
RN
2062 }\r
2063 }\r
72399dae 2064 }\r
9bc5dabb 2065\r
b2bd493e
SZ
2066 if (SetLanguageCodes) {\r
2067 //\r
2068 // Continue to set PlatformLangCodes or LangCodes.\r
2069 //\r
2070 return EFI_SUCCESS;\r
2071 } else {\r
2072 return Status;\r
2073 }\r
8d3a5c82 2074}\r
2075\r
7c80e839 2076/**\r
fa0737a8
SZ
2077 Compare two EFI_TIME data.\r
2078\r
2079\r
2080 @param FirstTime A pointer to the first EFI_TIME data.\r
2081 @param SecondTime A pointer to the second EFI_TIME data.\r
2082\r
2083 @retval TRUE The FirstTime is not later than the SecondTime.\r
2084 @retval FALSE The FirstTime is later than the SecondTime.\r
2085\r
2086**/\r
2087BOOLEAN\r
2088VariableCompareTimeStampInternal (\r
2089 IN EFI_TIME *FirstTime,\r
2090 IN EFI_TIME *SecondTime\r
2091 )\r
2092{\r
2093 if (FirstTime->Year != SecondTime->Year) {\r
2094 return (BOOLEAN) (FirstTime->Year < SecondTime->Year);\r
2095 } else if (FirstTime->Month != SecondTime->Month) {\r
2096 return (BOOLEAN) (FirstTime->Month < SecondTime->Month);\r
2097 } else if (FirstTime->Day != SecondTime->Day) {\r
2098 return (BOOLEAN) (FirstTime->Day < SecondTime->Day);\r
2099 } else if (FirstTime->Hour != SecondTime->Hour) {\r
2100 return (BOOLEAN) (FirstTime->Hour < SecondTime->Hour);\r
2101 } else if (FirstTime->Minute != SecondTime->Minute) {\r
2102 return (BOOLEAN) (FirstTime->Minute < SecondTime->Minute);\r
2103 }\r
2104\r
2105 return (BOOLEAN) (FirstTime->Second <= SecondTime->Second);\r
2106}\r
2107\r
2108/**\r
2109 Update the variable region with Variable information. If EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is set,\r
2110 index of associated public key is needed.\r
052ad7e1 2111\r
8a2d4996 2112 @param[in] VariableName Name of variable.\r
2113 @param[in] VendorGuid Guid of variable.\r
2114 @param[in] Data Variable data.\r
2115 @param[in] DataSize Size of data. 0 means delete.\r
fa0737a8
SZ
2116 @param[in] Attributes Attributes of the variable.\r
2117 @param[in] KeyIndex Index of associated public key.\r
2118 @param[in] MonotonicCount Value of associated monotonic count.\r
23b06935 2119 @param[in, out] CacheVariable The variable information which is used to keep track of variable usage.\r
fa0737a8
SZ
2120 @param[in] TimeStamp Value of associated TimeStamp.\r
2121\r
72399dae 2122 @retval EFI_SUCCESS The update operation is success.\r
72399dae 2123 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.\r
8d3a5c82 2124\r
7c80e839 2125**/\r
052ad7e1 2126EFI_STATUS\r
72399dae 2127UpdateVariable (\r
8a2d4996 2128 IN CHAR16 *VariableName,\r
2129 IN EFI_GUID *VendorGuid,\r
2130 IN VOID *Data,\r
2131 IN UINTN DataSize,\r
2132 IN UINT32 Attributes OPTIONAL,\r
fa0737a8
SZ
2133 IN UINT32 KeyIndex OPTIONAL,\r
2134 IN UINT64 MonotonicCount OPTIONAL,\r
2135 IN OUT VARIABLE_POINTER_TRACK *CacheVariable,\r
2136 IN EFI_TIME *TimeStamp OPTIONAL\r
052ad7e1 2137 )\r
8d3a5c82 2138{\r
8a9e0b72 2139 EFI_STATUS Status;\r
2140 VARIABLE_HEADER *NextVariable;\r
72399dae 2141 UINTN ScratchSize;\r
fa0737a8 2142 UINTN MaxDataSize;\r
8a9e0b72 2143 UINTN VarNameOffset;\r
2144 UINTN VarDataOffset;\r
72399dae 2145 UINTN VarNameSize;\r
8a9e0b72 2146 UINTN VarSize;\r
72399dae 2147 BOOLEAN Volatile;\r
2148 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r
8a9e0b72 2149 UINT8 State;\r
8a2d4996 2150 VARIABLE_POINTER_TRACK *Variable;\r
2151 VARIABLE_POINTER_TRACK NvVariable;\r
2152 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
2153 UINTN CacheOffset;\r
fa0737a8
SZ
2154 UINT8 *BufferForMerge;\r
2155 UINTN MergedBufSize;\r
2156 BOOLEAN DataReady;\r
2157 UINTN DataOffset;\r
4edb1866
SZ
2158 BOOLEAN IsCommonVariable;\r
2159 BOOLEAN IsCommonUserVariable;\r
fa0737a8 2160 AUTHENTICATED_VARIABLE_HEADER *AuthVariable;\r
fdb7765f 2161\r
fa0737a8 2162 if (mVariableModuleGlobal->FvbInstance == NULL) {\r
5456306f 2163 //\r
fa0737a8 2164 // The FVB protocol is not ready, so the EFI_VARIABLE_WRITE_ARCH_PROTOCOL is not installed.\r
5456306f 2165 //\r
fa0737a8
SZ
2166 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
2167 //\r
2168 // Trying to update NV variable prior to the installation of EFI_VARIABLE_WRITE_ARCH_PROTOCOL\r
2169 //\r
2170 DEBUG ((EFI_D_ERROR, "Update NV variable before EFI_VARIABLE_WRITE_ARCH_PROTOCOL ready - %r\n", EFI_NOT_AVAILABLE_YET));\r
2171 return EFI_NOT_AVAILABLE_YET;\r
2172 } else if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {\r
2173 //\r
2174 // Trying to update volatile authenticated variable prior to the installation of EFI_VARIABLE_WRITE_ARCH_PROTOCOL\r
2175 // The authenticated variable perhaps is not initialized, just return here.\r
2176 //\r
2177 DEBUG ((EFI_D_ERROR, "Update AUTH variable before EFI_VARIABLE_WRITE_ARCH_PROTOCOL ready - %r\n", EFI_NOT_AVAILABLE_YET));\r
2178 return EFI_NOT_AVAILABLE_YET;\r
2179 }\r
5456306f 2180 }\r
2181\r
31349131
SZ
2182 //\r
2183 // Check if CacheVariable points to the variable in variable HOB.\r
2184 // If yes, let CacheVariable points to the variable in NV variable cache.\r
2185 //\r
2186 if ((CacheVariable->CurrPtr != NULL) &&\r
2187 (mVariableModuleGlobal->VariableGlobal.HobVariableBase != 0) &&\r
2188 (CacheVariable->StartPtr == GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase))\r
2189 ) {\r
2190 CacheVariable->StartPtr = GetStartPointer (mNvVariableCache);\r
2191 CacheVariable->EndPtr = GetEndPointer (mNvVariableCache);\r
2192 CacheVariable->Volatile = FALSE;\r
2193 Status = FindVariableEx (VariableName, VendorGuid, FALSE, CacheVariable);\r
2194 if (CacheVariable->CurrPtr == NULL || EFI_ERROR (Status)) {\r
2195 //\r
2196 // There is no matched variable in NV variable cache.\r
2197 //\r
2198 if ((((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0) && (DataSize == 0)) || (Attributes == 0)) {\r
2199 //\r
2200 // It is to delete variable,\r
2201 // go to delete this variable in variable HOB and\r
2202 // try to flush other variables from HOB to flash.\r
2203 //\r
38eb83b8 2204 UpdateVariableInfo (VariableName, VendorGuid, FALSE, FALSE, FALSE, TRUE, FALSE);\r
31349131
SZ
2205 FlushHobVariableToFlash (VariableName, VendorGuid);\r
2206 return EFI_SUCCESS;\r
2207 }\r
2208 }\r
2209 }\r
2210\r
5456306f 2211 if ((CacheVariable->CurrPtr == NULL) || CacheVariable->Volatile) {\r
8a2d4996 2212 Variable = CacheVariable;\r
2213 } else {\r
8a2d4996 2214 //\r
5456306f 2215 // Update/Delete existing NV variable.\r
8a2d4996 2216 // CacheVariable points to the variable in the memory copy of Flash area\r
2217 // Now let Variable points to the same variable in Flash area.\r
2218 //\r
2219 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);\r
fa0737a8 2220 Variable = &NvVariable;\r
8a2d4996 2221 Variable->StartPtr = GetStartPointer (VariableStoreHeader);\r
9b18845a
DL
2222 Variable->EndPtr = (VARIABLE_HEADER *)((UINTN)Variable->StartPtr + ((UINTN)CacheVariable->EndPtr - (UINTN)CacheVariable->StartPtr));\r
2223\r
5456306f 2224 Variable->CurrPtr = (VARIABLE_HEADER *)((UINTN)Variable->StartPtr + ((UINTN)CacheVariable->CurrPtr - (UINTN)CacheVariable->StartPtr));\r
23b06935
SZ
2225 if (CacheVariable->InDeletedTransitionPtr != NULL) {\r
2226 Variable->InDeletedTransitionPtr = (VARIABLE_HEADER *)((UINTN)Variable->StartPtr + ((UINTN)CacheVariable->InDeletedTransitionPtr - (UINTN)CacheVariable->StartPtr));\r
2227 } else {\r
2228 Variable->InDeletedTransitionPtr = NULL;\r
2229 }\r
5456306f 2230 Variable->Volatile = FALSE;\r
fa0737a8 2231 }\r
5456306f 2232\r
2233 Fvb = mVariableModuleGlobal->FvbInstance;\r
fdb7765f 2234\r
fa0737a8
SZ
2235 //\r
2236 // Tricky part: Use scratch data area at the end of volatile variable store\r
2237 // as a temporary storage.\r
2238 //\r
2239 NextVariable = GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase));\r
2240 ScratchSize = mVariableModuleGlobal->ScratchBufferSize;\r
2241 SetMem (NextVariable, ScratchSize, 0xff);\r
2242 DataReady = FALSE;\r
2243\r
72399dae 2244 if (Variable->CurrPtr != NULL) {\r
8d3a5c82 2245 //\r
8a2d4996 2246 // Update/Delete existing variable.\r
8d3a5c82 2247 //\r
fa0737a8 2248 if (AtRuntime ()) {\r
c6492839 2249 //\r
fa0737a8
SZ
2250 // If AtRuntime and the variable is Volatile and Runtime Access,\r
2251 // the volatile is ReadOnly, and SetVariable should be aborted and\r
c6492839 2252 // return EFI_WRITE_PROTECTED.\r
2253 //\r
72399dae 2254 if (Variable->Volatile) {\r
c6492839 2255 Status = EFI_WRITE_PROTECTED;\r
2256 goto Done;\r
2257 }\r
2258 //\r
fa0737a8
SZ
2259 // Only variable that have NV attributes can be updated/deleted in Runtime.\r
2260 //\r
9b18845a 2261 if ((CacheVariable->CurrPtr->Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r
fa0737a8
SZ
2262 Status = EFI_INVALID_PARAMETER;\r
2263 goto Done;\r
2264 }\r
2265\r
2266 //\r
2267 // Only variable that have RT attributes can be updated/deleted in Runtime.\r
c6492839 2268 //\r
9b18845a 2269 if ((CacheVariable->CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) {\r
c6492839 2270 Status = EFI_INVALID_PARAMETER;\r
fa0737a8 2271 goto Done;\r
c6492839 2272 }\r
2273 }\r
8a2d4996 2274\r
8d3a5c82 2275 //\r
c6492839 2276 // Setting a data variable with no access, or zero DataSize attributes\r
8a2d4996 2277 // causes it to be deleted.\r
fa0737a8
SZ
2278 // When the EFI_VARIABLE_APPEND_WRITE attribute is set, DataSize of zero will\r
2279 // not delete the variable.\r
8d3a5c82 2280 //\r
fa0737a8 2281 if ((((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0) && (DataSize == 0))|| ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0)) {\r
23b06935
SZ
2282 if (Variable->InDeletedTransitionPtr != NULL) {\r
2283 //\r
2284 // Both ADDED and IN_DELETED_TRANSITION variable are present,\r
2285 // set IN_DELETED_TRANSITION one to DELETED state first.\r
2286 //\r
ae658d91 2287 ASSERT (CacheVariable->InDeletedTransitionPtr != NULL);\r
9b18845a 2288 State = CacheVariable->InDeletedTransitionPtr->State;\r
23b06935
SZ
2289 State &= VAR_DELETED;\r
2290 Status = UpdateVariableStore (\r
2291 &mVariableModuleGlobal->VariableGlobal,\r
2292 Variable->Volatile,\r
2293 FALSE,\r
2294 Fvb,\r
2295 (UINTN) &Variable->InDeletedTransitionPtr->State,\r
2296 sizeof (UINT8),\r
2297 &State\r
2298 );\r
2299 if (!EFI_ERROR (Status)) {\r
2300 if (!Variable->Volatile) {\r
2301 CacheVariable->InDeletedTransitionPtr->State = State;\r
2302 }\r
2303 } else {\r
2304 goto Done;\r
2305 }\r
2306 }\r
2307\r
9b18845a 2308 State = CacheVariable->CurrPtr->State;\r
c6492839 2309 State &= VAR_DELETED;\r
2310\r
2311 Status = UpdateVariableStore (\r
052ad7e1 2312 &mVariableModuleGlobal->VariableGlobal,\r
72399dae 2313 Variable->Volatile,\r
c6492839 2314 FALSE,\r
8a9e0b72 2315 Fvb,\r
72399dae 2316 (UINTN) &Variable->CurrPtr->State,\r
c6492839 2317 sizeof (UINT8),\r
2318 &State\r
fa0737a8 2319 );\r
33a5a666 2320 if (!EFI_ERROR (Status)) {\r
8a2d4996 2321 UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, FALSE, TRUE, FALSE);\r
2322 if (!Variable->Volatile) {\r
2323 CacheVariable->CurrPtr->State = State;\r
335e2681 2324 FlushHobVariableToFlash (VariableName, VendorGuid);\r
8a2d4996 2325 }\r
33a5a666 2326 }\r
fa0737a8 2327 goto Done;\r
c6492839 2328 }\r
8d3a5c82 2329 //\r
8a2d4996 2330 // If the variable is marked valid, and the same data has been passed in,\r
c6492839 2331 // then return to the caller immediately.\r
8d3a5c82 2332 //\r
9b18845a
DL
2333 if (DataSizeOfVariable (CacheVariable->CurrPtr) == DataSize &&\r
2334 (CompareMem (Data, GetVariableDataPtr (CacheVariable->CurrPtr), DataSize) == 0) &&\r
fa0737a8
SZ
2335 ((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0) &&\r
2336 (TimeStamp == NULL)) {\r
2337 //\r
2338 // Variable content unchanged and no need to update timestamp, just return.\r
2339 //\r
8a2d4996 2340 UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, TRUE, FALSE, FALSE);\r
c6492839 2341 Status = EFI_SUCCESS;\r
2342 goto Done;\r
9b18845a
DL
2343 } else if ((CacheVariable->CurrPtr->State == VAR_ADDED) ||\r
2344 (CacheVariable->CurrPtr->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {\r
814bae52 2345\r
fa0737a8
SZ
2346 //\r
2347 // EFI_VARIABLE_APPEND_WRITE attribute only effects for existing variable.\r
2348 //\r
2349 if ((Attributes & EFI_VARIABLE_APPEND_WRITE) != 0) {\r
2350 //\r
2351 // NOTE: From 0 to DataOffset of NextVariable is reserved for Variable Header and Name.\r
2352 // From DataOffset of NextVariable is to save the existing variable data.\r
2353 //\r
9b18845a 2354 DataOffset = GetVariableDataOffset (CacheVariable->CurrPtr);\r
fa0737a8 2355 BufferForMerge = (UINT8 *) ((UINTN) NextVariable + DataOffset);\r
9b18845a 2356 CopyMem (BufferForMerge, (UINT8 *) ((UINTN) CacheVariable->CurrPtr + DataOffset), DataSizeOfVariable (CacheVariable->CurrPtr));\r
fa0737a8
SZ
2357\r
2358 //\r
2359 // Set Max Common/Auth Variable Data Size as default MaxDataSize.\r
2360 //\r
2361 if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {\r
2362 MaxDataSize = mVariableModuleGlobal->MaxAuthVariableSize - DataOffset;\r
2363 } else {\r
2364 MaxDataSize = mVariableModuleGlobal->MaxVariableSize - DataOffset;\r
2365 }\r
2366\r
2367 //\r
2368 // Append the new data to the end of existing data.\r
2369 // Max Harware error record variable data size is different from common/auth variable.\r
2370 //\r
2371 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2372 MaxDataSize = PcdGet32 (PcdMaxHardwareErrorVariableSize) - DataOffset;\r
2373 }\r
2374\r
9b18845a 2375 if (DataSizeOfVariable (CacheVariable->CurrPtr) + DataSize > MaxDataSize) {\r
fa0737a8
SZ
2376 //\r
2377 // Existing data size + new data size exceed maximum variable size limitation.\r
2378 //\r
2379 Status = EFI_INVALID_PARAMETER;\r
2380 goto Done;\r
2381 }\r
9b18845a
DL
2382 CopyMem ((UINT8*) ((UINTN) BufferForMerge + DataSizeOfVariable (CacheVariable->CurrPtr)), Data, DataSize);\r
2383 MergedBufSize = DataSizeOfVariable (CacheVariable->CurrPtr) + DataSize;\r
fa0737a8
SZ
2384\r
2385 //\r
2386 // BufferForMerge(from DataOffset of NextVariable) has included the merged existing and new data.\r
2387 //\r
2388 Data = BufferForMerge;\r
2389 DataSize = MergedBufSize;\r
2390 DataReady = TRUE;\r
2391 }\r
2392\r
c6492839 2393 //\r
8a2d4996 2394 // Mark the old variable as in delete transition.\r
c6492839 2395 //\r
9b18845a 2396 State = CacheVariable->CurrPtr->State;\r
c6492839 2397 State &= VAR_IN_DELETED_TRANSITION;\r
2398\r
2399 Status = UpdateVariableStore (\r
052ad7e1 2400 &mVariableModuleGlobal->VariableGlobal,\r
72399dae 2401 Variable->Volatile,\r
c6492839 2402 FALSE,\r
8a9e0b72 2403 Fvb,\r
72399dae 2404 (UINTN) &Variable->CurrPtr->State,\r
c6492839 2405 sizeof (UINT8),\r
2406 &State\r
fa0737a8 2407 );\r
c6492839 2408 if (EFI_ERROR (Status)) {\r
fa0737a8
SZ
2409 goto Done;\r
2410 }\r
8a2d4996 2411 if (!Variable->Volatile) {\r
2412 CacheVariable->CurrPtr->State = State;\r
2413 }\r
fa0737a8 2414 }\r
72399dae 2415 } else {\r
8d3a5c82 2416 //\r
8a2d4996 2417 // Not found existing variable. Create a new variable.\r
fa0737a8
SZ
2418 //\r
2419\r
2420 if ((DataSize == 0) && ((Attributes & EFI_VARIABLE_APPEND_WRITE) != 0)) {\r
2421 Status = EFI_SUCCESS;\r
2422 goto Done;\r
2423 }\r
2424\r
8d3a5c82 2425 //\r
c6492839 2426 // Make sure we are trying to create a new variable.\r
fa0737a8 2427 // Setting a data variable with zero DataSize or no access attributes means to delete it.\r
8d3a5c82 2428 //\r
c6492839 2429 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {\r
2430 Status = EFI_NOT_FOUND;\r
2431 goto Done;\r
2432 }\r
fa0737a8 2433\r
8d3a5c82 2434 //\r
8a2d4996 2435 // Only variable have NV|RT attribute can be created in Runtime.\r
c6492839 2436 //\r
8a2d4996 2437 if (AtRuntime () &&\r
45f6c85b 2438 (((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) || ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0))) {\r
c6492839 2439 Status = EFI_INVALID_PARAMETER;\r
2440 goto Done;\r
fa0737a8 2441 }\r
c6492839 2442 }\r
2443\r
2444 //\r
2445 // Function part - create a new variable and copy the data.\r
2446 // Both update a variable and create a variable will come here.\r
c6492839 2447 //\r
c6492839 2448 NextVariable->StartId = VARIABLE_DATA;\r
c6492839 2449 //\r
2450 // NextVariable->State = VAR_ADDED;\r
2451 //\r
8a2d4996 2452 NextVariable->Reserved = 0;\r
fa0737a8
SZ
2453 if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {\r
2454 AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) NextVariable;\r
2455 AuthVariable->PubKeyIndex = KeyIndex;\r
2456 AuthVariable->MonotonicCount = MonotonicCount;\r
2457 ZeroMem (&AuthVariable->TimeStamp, sizeof (EFI_TIME));\r
2458\r
2459 if (((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) != 0) &&\r
2460 (TimeStamp != NULL)) {\r
2461 if ((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0) {\r
2462 CopyMem (&AuthVariable->TimeStamp, TimeStamp, sizeof (EFI_TIME));\r
2463 } else {\r
2464 //\r
2465 // In the case when the EFI_VARIABLE_APPEND_WRITE attribute is set, only\r
2466 // when the new TimeStamp value is later than the current timestamp associated\r
2467 // with the variable, we need associate the new timestamp with the updated value.\r
2468 //\r
2469 if (Variable->CurrPtr != NULL) {\r
9b18845a 2470 if (VariableCompareTimeStampInternal (&(((AUTHENTICATED_VARIABLE_HEADER *) CacheVariable->CurrPtr)->TimeStamp), TimeStamp)) {\r
fa0737a8
SZ
2471 CopyMem (&AuthVariable->TimeStamp, TimeStamp, sizeof (EFI_TIME));\r
2472 }\r
2473 }\r
2474 }\r
2475 }\r
2476 }\r
2477\r
2478 //\r
2479 // The EFI_VARIABLE_APPEND_WRITE attribute will never be set in the returned\r
2480 // Attributes bitmask parameter of a GetVariable() call.\r
2481 //\r
2482 NextVariable->Attributes = Attributes & (~EFI_VARIABLE_APPEND_WRITE);\r
2483\r
2484 VarNameOffset = GetVariableHeaderSize ();\r
8a2d4996 2485 VarNameSize = StrSize (VariableName);\r
c6492839 2486 CopyMem (\r
2487 (UINT8 *) ((UINTN) NextVariable + VarNameOffset),\r
2488 VariableName,\r
2489 VarNameSize\r
2490 );\r
2491 VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);\r
fa0737a8
SZ
2492\r
2493 //\r
2494 // If DataReady is TRUE, it means the variable data has been saved into\r
2495 // NextVariable during EFI_VARIABLE_APPEND_WRITE operation preparation.\r
2496 //\r
2497 if (!DataReady) {\r
2498 CopyMem (\r
2499 (UINT8 *) ((UINTN) NextVariable + VarDataOffset),\r
2500 Data,\r
2501 DataSize\r
2502 );\r
2503 }\r
2504\r
2505 CopyMem (GetVendorGuidPtr (NextVariable), VendorGuid, sizeof (EFI_GUID));\r
c6492839 2506 //\r
2507 // There will be pad bytes after Data, the NextVariable->NameSize and\r
2508 // NextVariable->DataSize should not include pad size so that variable\r
8a2d4996 2509 // service can get actual size in GetVariable.\r
c6492839 2510 //\r
fa0737a8
SZ
2511 SetNameSizeOfVariable (NextVariable, VarNameSize);\r
2512 SetDataSizeOfVariable (NextVariable, DataSize);\r
c6492839 2513\r
2514 //\r
2515 // The actual size of the variable that stores in storage should\r
2516 // include pad size.\r
2517 //\r
2518 VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);\r
45f6c85b 2519 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
8d3a5c82 2520 //\r
8a2d4996 2521 // Create a nonvolatile variable.\r
8d3a5c82 2522 //\r
fd51bf70 2523 Volatile = FALSE;\r
4edb1866
SZ
2524\r
2525 IsCommonVariable = FALSE;\r
2526 IsCommonUserVariable = FALSE;\r
2527 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0) {\r
2528 IsCommonVariable = TRUE;\r
2529 IsCommonUserVariable = IsUserVariable (NextVariable);\r
2530 }\r
2531 if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0)\r
188e4e84 2532 && ((VarSize + mVariableModuleGlobal->HwErrVariableTotalSize) > PcdGet32 (PcdHwErrStorageSize)))\r
4edb1866
SZ
2533 || (IsCommonVariable && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > mVariableModuleGlobal->CommonVariableSpace))\r
2534 || (IsCommonVariable && AtRuntime () && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > mVariableModuleGlobal->CommonRuntimeVariableSpace))\r
2535 || (IsCommonUserVariable && ((VarSize + mVariableModuleGlobal->CommonUserVariableTotalSize) > mVariableModuleGlobal->CommonMaxUserVariableSpace))) {\r
8a2d4996 2536 if (AtRuntime ()) {\r
4edb1866
SZ
2537 if (IsCommonUserVariable && ((VarSize + mVariableModuleGlobal->CommonUserVariableTotalSize) > mVariableModuleGlobal->CommonMaxUserVariableSpace)) {\r
2538 RecordVarErrorFlag (VAR_ERROR_FLAG_USER_ERROR, VariableName, VendorGuid, Attributes, VarSize);\r
2539 }\r
2540 if (IsCommonVariable && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > mVariableModuleGlobal->CommonRuntimeVariableSpace)) {\r
2541 RecordVarErrorFlag (VAR_ERROR_FLAG_SYSTEM_ERROR, VariableName, VendorGuid, Attributes, VarSize);\r
2542 }\r
c6492839 2543 Status = EFI_OUT_OF_RESOURCES;\r
2544 goto Done;\r
2545 }\r
2546 //\r
7baf3c69 2547 // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.\r
c6492839 2548 //\r
fa0737a8
SZ
2549 Status = Reclaim (\r
2550 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
2551 &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
2552 FALSE,\r
2553 Variable,\r
2554 NextVariable,\r
2555 HEADER_ALIGN (VarSize)\r
2556 );\r
7baf3c69
SZ
2557 if (!EFI_ERROR (Status)) {\r
2558 //\r
2559 // The new variable has been integrated successfully during reclaiming.\r
2560 //\r
2561 if (Variable->CurrPtr != NULL) {\r
2562 CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN) CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr));\r
2563 CacheVariable->InDeletedTransitionPtr = NULL;\r
2564 }\r
2565 UpdateVariableInfo (VariableName, VendorGuid, FALSE, FALSE, TRUE, FALSE, FALSE);\r
2566 FlushHobVariableToFlash (VariableName, VendorGuid);\r
4edb1866
SZ
2567 } else {\r
2568 if (IsCommonUserVariable && ((VarSize + mVariableModuleGlobal->CommonUserVariableTotalSize) > mVariableModuleGlobal->CommonMaxUserVariableSpace)) {\r
2569 RecordVarErrorFlag (VAR_ERROR_FLAG_USER_ERROR, VariableName, VendorGuid, Attributes, VarSize);\r
2570 }\r
2571 if (IsCommonVariable && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > mVariableModuleGlobal->CommonVariableSpace)) {\r
2572 RecordVarErrorFlag (VAR_ERROR_FLAG_SYSTEM_ERROR, VariableName, VendorGuid, Attributes, VarSize);\r
2573 }\r
23b06935 2574 }\r
7baf3c69 2575 goto Done;\r
8d3a5c82 2576 }\r
2577 //\r
8a2d4996 2578 // Four steps\r
c6492839 2579 // 1. Write variable header\r
fa0737a8 2580 // 2. Set variable state to header valid\r
130e2569 2581 // 3. Write variable data\r
2582 // 4. Set variable state to valid\r
8d3a5c82 2583 //\r
8d3a5c82 2584 //\r
c6492839 2585 // Step 1:\r
8d3a5c82 2586 //\r
8a2d4996 2587 CacheOffset = mVariableModuleGlobal->NonVolatileLastVariableOffset;\r
c6492839 2588 Status = UpdateVariableStore (\r
052ad7e1 2589 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 2590 FALSE,\r
2591 TRUE,\r
8a9e0b72 2592 Fvb,\r
72399dae 2593 mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
fa0737a8 2594 (UINT32) GetVariableHeaderSize (),\r
c6492839 2595 (UINT8 *) NextVariable\r
2596 );\r
2597\r
2598 if (EFI_ERROR (Status)) {\r
2599 goto Done;\r
2600 }\r
130e2569 2601\r
8d3a5c82 2602 //\r
c6492839 2603 // Step 2:\r
8d3a5c82 2604 //\r
130e2569 2605 NextVariable->State = VAR_HEADER_VALID_ONLY;\r
2606 Status = UpdateVariableStore (\r
2607 &mVariableModuleGlobal->VariableGlobal,\r
2608 FALSE,\r
2609 TRUE,\r
8a9e0b72 2610 Fvb,\r
8a2d4996 2611 mVariableModuleGlobal->NonVolatileLastVariableOffset + OFFSET_OF (VARIABLE_HEADER, State),\r
2612 sizeof (UINT8),\r
2613 &NextVariable->State\r
130e2569 2614 );\r
2615\r
2616 if (EFI_ERROR (Status)) {\r
2617 goto Done;\r
2618 }\r
2619 //\r
2620 // Step 3:\r
2621 //\r
c6492839 2622 Status = UpdateVariableStore (\r
052ad7e1 2623 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 2624 FALSE,\r
2625 TRUE,\r
8a9e0b72 2626 Fvb,\r
fa0737a8
SZ
2627 mVariableModuleGlobal->NonVolatileLastVariableOffset + GetVariableHeaderSize (),\r
2628 (UINT32) (VarSize - GetVariableHeaderSize ()),\r
2629 (UINT8 *) NextVariable + GetVariableHeaderSize ()\r
c6492839 2630 );\r
2631\r
2632 if (EFI_ERROR (Status)) {\r
2633 goto Done;\r
2634 }\r
8d3a5c82 2635 //\r
130e2569 2636 // Step 4:\r
8d3a5c82 2637 //\r
c6492839 2638 NextVariable->State = VAR_ADDED;\r
2639 Status = UpdateVariableStore (\r
052ad7e1 2640 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 2641 FALSE,\r
2642 TRUE,\r
8a9e0b72 2643 Fvb,\r
8a2d4996 2644 mVariableModuleGlobal->NonVolatileLastVariableOffset + OFFSET_OF (VARIABLE_HEADER, State),\r
2645 sizeof (UINT8),\r
2646 &NextVariable->State\r
c6492839 2647 );\r
2648\r
2649 if (EFI_ERROR (Status)) {\r
2650 goto Done;\r
2651 }\r
8d3a5c82 2652\r
72399dae 2653 mVariableModuleGlobal->NonVolatileLastVariableOffset += HEADER_ALIGN (VarSize);\r
8d3a5c82 2654\r
2fcdca1d 2655 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {\r
2656 mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VarSize);\r
2657 } else {\r
2658 mVariableModuleGlobal->CommonVariableTotalSize += HEADER_ALIGN (VarSize);\r
4edb1866
SZ
2659 if (IsCommonUserVariable) {\r
2660 mVariableModuleGlobal->CommonUserVariableTotalSize += HEADER_ALIGN (VarSize);\r
2661 }\r
2fcdca1d 2662 }\r
8a2d4996 2663 //\r
2664 // update the memory copy of Flash region.\r
2665 //\r
2666 CopyMem ((UINT8 *)mNvVariableCache + CacheOffset, (UINT8 *)NextVariable, VarSize);\r
c6492839 2667 } else {\r
2668 //\r
8a2d4996 2669 // Create a volatile variable.\r
fa0737a8 2670 //\r
fd51bf70 2671 Volatile = TRUE;\r
c6492839 2672\r
72399dae 2673 if ((UINT32) (VarSize + mVariableModuleGlobal->VolatileLastVariableOffset) >\r
052ad7e1 2674 ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size) {\r
8d3a5c82 2675 //\r
7baf3c69 2676 // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.\r
8d3a5c82 2677 //\r
fa0737a8
SZ
2678 Status = Reclaim (\r
2679 mVariableModuleGlobal->VariableGlobal.VolatileVariableBase,\r
2680 &mVariableModuleGlobal->VolatileLastVariableOffset,\r
2681 TRUE,\r
2682 Variable,\r
2683 NextVariable,\r
2684 HEADER_ALIGN (VarSize)\r
2685 );\r
7baf3c69
SZ
2686 if (!EFI_ERROR (Status)) {\r
2687 //\r
2688 // The new variable has been integrated successfully during reclaiming.\r
2689 //\r
2690 if (Variable->CurrPtr != NULL) {\r
2691 CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN) CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr));\r
2692 CacheVariable->InDeletedTransitionPtr = NULL;\r
2693 }\r
2694 UpdateVariableInfo (VariableName, VendorGuid, TRUE, FALSE, TRUE, FALSE, FALSE);\r
23b06935 2695 }\r
7baf3c69 2696 goto Done;\r
8d3a5c82 2697 }\r
8d3a5c82 2698\r
c6492839 2699 NextVariable->State = VAR_ADDED;\r
2700 Status = UpdateVariableStore (\r
052ad7e1 2701 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 2702 TRUE,\r
2703 TRUE,\r
8a9e0b72 2704 Fvb,\r
72399dae 2705 mVariableModuleGlobal->VolatileLastVariableOffset,\r
c6492839 2706 (UINT32) VarSize,\r
2707 (UINT8 *) NextVariable\r
2708 );\r
2709\r
2710 if (EFI_ERROR (Status)) {\r
2711 goto Done;\r
8d3a5c82 2712 }\r
c6492839 2713\r
72399dae 2714 mVariableModuleGlobal->VolatileLastVariableOffset += HEADER_ALIGN (VarSize);\r
c6492839 2715 }\r
72399dae 2716\r
c6492839 2717 //\r
8a2d4996 2718 // Mark the old variable as deleted.\r
c6492839 2719 //\r
23b06935
SZ
2720 if (!EFI_ERROR (Status) && Variable->CurrPtr != NULL) {\r
2721 if (Variable->InDeletedTransitionPtr != NULL) {\r
2722 //\r
2723 // Both ADDED and IN_DELETED_TRANSITION old variable are present,\r
2724 // set IN_DELETED_TRANSITION one to DELETED state first.\r
2725 //\r
ae658d91 2726 ASSERT (CacheVariable->InDeletedTransitionPtr != NULL);\r
9b18845a 2727 State = CacheVariable->InDeletedTransitionPtr->State;\r
23b06935
SZ
2728 State &= VAR_DELETED;\r
2729 Status = UpdateVariableStore (\r
2730 &mVariableModuleGlobal->VariableGlobal,\r
2731 Variable->Volatile,\r
2732 FALSE,\r
2733 Fvb,\r
2734 (UINTN) &Variable->InDeletedTransitionPtr->State,\r
2735 sizeof (UINT8),\r
2736 &State\r
2737 );\r
2738 if (!EFI_ERROR (Status)) {\r
2739 if (!Variable->Volatile) {\r
2740 CacheVariable->InDeletedTransitionPtr->State = State;\r
2741 }\r
2742 } else {\r
2743 goto Done;\r
2744 }\r
2745 }\r
2746\r
72399dae 2747 State = Variable->CurrPtr->State;\r
c6492839 2748 State &= VAR_DELETED;\r
2749\r
2750 Status = UpdateVariableStore (\r
72399dae 2751 &mVariableModuleGlobal->VariableGlobal,\r
2752 Variable->Volatile,\r
2753 FALSE,\r
2754 Fvb,\r
2755 (UINTN) &Variable->CurrPtr->State,\r
2756 sizeof (UINT8),\r
2757 &State\r
2758 );\r
fa0737a8 2759 if (!EFI_ERROR (Status) && !Variable->Volatile) {\r
8a2d4996 2760 CacheVariable->CurrPtr->State = State;\r
2761 }\r
72399dae 2762 }\r
2763\r
2764 if (!EFI_ERROR (Status)) {\r
2765 UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);\r
335e2681
SZ
2766 if (!Volatile) {\r
2767 FlushHobVariableToFlash (VariableName, VendorGuid);\r
2768 }\r
72399dae 2769 }\r
2770\r
2771Done:\r
2772 return Status;\r
2773}\r
2774\r
2775/**\r
2776\r
2777 This code finds variable in storage blocks (Volatile or Non-Volatile).\r
2778\r
18a7dbbc
SZ
2779 Caution: This function may receive untrusted input.\r
2780 This function may be invoked in SMM mode, and datasize is external input.\r
2781 This function will do basic validation, before parse the data.\r
2782\r
72399dae 2783 @param VariableName Name of Variable to be found.\r
2784 @param VendorGuid Variable vendor GUID.\r
2785 @param Attributes Attribute value of the variable found.\r
2786 @param DataSize Size of Data found. If size is less than the\r
2787 data, this value contains the required size.\r
2788 @param Data Data pointer.\r
fa0737a8 2789\r
8a2d4996 2790 @return EFI_INVALID_PARAMETER Invalid parameter.\r
2791 @return EFI_SUCCESS Find the specified variable.\r
2792 @return EFI_NOT_FOUND Not found.\r
2793 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.\r
72399dae 2794\r
2795**/\r
2796EFI_STATUS\r
2797EFIAPI\r
8a2d4996 2798VariableServiceGetVariable (\r
72399dae 2799 IN CHAR16 *VariableName,\r
2800 IN EFI_GUID *VendorGuid,\r
2801 OUT UINT32 *Attributes OPTIONAL,\r
2802 IN OUT UINTN *DataSize,\r
2803 OUT VOID *Data\r
2804 )\r
2805{\r
2806 EFI_STATUS Status;\r
2807 VARIABLE_POINTER_TRACK Variable;\r
2808 UINTN VarDataSize;\r
2809\r
2810 if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {\r
2811 return EFI_INVALID_PARAMETER;\r
2812 }\r
2813\r
2814 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
fa0737a8 2815\r
9622df63 2816 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
72399dae 2817 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
2818 goto Done;\r
2819 }\r
2820\r
2821 //\r
2822 // Get data size\r
2823 //\r
2824 VarDataSize = DataSizeOfVariable (Variable.CurrPtr);\r
2825 ASSERT (VarDataSize != 0);\r
2826\r
2827 if (*DataSize >= VarDataSize) {\r
2828 if (Data == NULL) {\r
2829 Status = EFI_INVALID_PARAMETER;\r
2830 goto Done;\r
33a5a666 2831 }\r
72399dae 2832\r
2833 CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);\r
2834 if (Attributes != NULL) {\r
2835 *Attributes = Variable.CurrPtr->Attributes;\r
2836 }\r
2837\r
2838 *DataSize = VarDataSize;\r
2839 UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, TRUE, FALSE, FALSE, FALSE);\r
fa0737a8 2840\r
72399dae 2841 Status = EFI_SUCCESS;\r
2842 goto Done;\r
2843 } else {\r
2844 *DataSize = VarDataSize;\r
2845 Status = EFI_BUFFER_TOO_SMALL;\r
2846 goto Done;\r
8d3a5c82 2847 }\r
2848\r
72399dae 2849Done:\r
2850 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2851 return Status;\r
2852}\r
2853\r
72399dae 2854/**\r
72399dae 2855 This code Finds the Next available variable.\r
2856\r
18a7dbbc
SZ
2857 Caution: This function may receive untrusted input.\r
2858 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r
2859\r
fa0737a8
SZ
2860 @param[in] VariableName Pointer to variable name.\r
2861 @param[in] VendorGuid Variable Vendor Guid.\r
2862 @param[out] VariablePtr Pointer to variable header address.\r
72399dae 2863\r
fa0737a8
SZ
2864 @return EFI_SUCCESS Find the specified variable.\r
2865 @return EFI_NOT_FOUND Not found.\r
72399dae 2866\r
2867**/\r
2868EFI_STATUS\r
2869EFIAPI\r
fa0737a8
SZ
2870VariableServiceGetNextVariableInternal (\r
2871 IN CHAR16 *VariableName,\r
2872 IN EFI_GUID *VendorGuid,\r
2873 OUT VARIABLE_HEADER **VariablePtr\r
72399dae 2874 )\r
2875{\r
0f7aff72 2876 VARIABLE_STORE_TYPE Type;\r
72399dae 2877 VARIABLE_POINTER_TRACK Variable;\r
0f7aff72 2878 VARIABLE_POINTER_TRACK VariableInHob;\r
23b06935 2879 VARIABLE_POINTER_TRACK VariablePtrTrack;\r
72399dae 2880 EFI_STATUS Status;\r
0f7aff72 2881 VARIABLE_STORE_HEADER *VariableStoreHeader[VariableStoreTypeMax];\r
72399dae 2882\r
9622df63 2883 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
72399dae 2884 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
2885 goto Done;\r
2886 }\r
2887\r
2888 if (VariableName[0] != 0) {\r
2889 //\r
8a2d4996 2890 // If variable name is not NULL, get next variable.\r
72399dae 2891 //\r
2892 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
2893 }\r
2894\r
0f7aff72
RN
2895 //\r
2896 // 0: Volatile, 1: HOB, 2: Non-Volatile.\r
2897 // The index and attributes mapping must be kept in this order as FindVariable\r
2898 // makes use of this mapping to implement search algorithm.\r
2899 //\r
2900 VariableStoreHeader[VariableStoreTypeVolatile] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;\r
2901 VariableStoreHeader[VariableStoreTypeHob] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;\r
2902 VariableStoreHeader[VariableStoreTypeNv] = mNvVariableCache;\r
2903\r
72399dae 2904 while (TRUE) {\r
2905 //\r
0f7aff72 2906 // Switch from Volatile to HOB, to Non-Volatile.\r
72399dae 2907 //\r
6ebffb67 2908 while (!IsValidVariableHeader (Variable.CurrPtr, Variable.EndPtr)) {\r
0f7aff72
RN
2909 //\r
2910 // Find current storage index\r
2911 //\r
2912 for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {\r
2913 if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr == GetStartPointer (VariableStoreHeader[Type]))) {\r
2914 break;\r
2915 }\r
2916 }\r
2917 ASSERT (Type < VariableStoreTypeMax);\r
2918 //\r
2919 // Switch to next storage\r
2920 //\r
2921 for (Type++; Type < VariableStoreTypeMax; Type++) {\r
2922 if (VariableStoreHeader[Type] != NULL) {\r
2923 break;\r
2924 }\r
2925 }\r
2926 //\r
fa0737a8 2927 // Capture the case that\r
0f7aff72
RN
2928 // 1. current storage is the last one, or\r
2929 // 2. no further storage\r
2930 //\r
2931 if (Type == VariableStoreTypeMax) {\r
72399dae 2932 Status = EFI_NOT_FOUND;\r
2933 goto Done;\r
2934 }\r
0f7aff72
RN
2935 Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);\r
2936 Variable.EndPtr = GetEndPointer (VariableStoreHeader[Type]);\r
2937 Variable.CurrPtr = Variable.StartPtr;\r
72399dae 2938 }\r
0f7aff72 2939\r
72399dae 2940 //\r
2941 // Variable is found\r
2942 //\r
23b06935
SZ
2943 if (Variable.CurrPtr->State == VAR_ADDED || Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
2944 if (!AtRuntime () || ((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {\r
2945 if (Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
2946 //\r
2947 // If it is a IN_DELETED_TRANSITION variable,\r
2948 // and there is also a same ADDED one at the same time,\r
2949 // don't return it.\r
2950 //\r
2951 VariablePtrTrack.StartPtr = Variable.StartPtr;\r
2952 VariablePtrTrack.EndPtr = Variable.EndPtr;\r
2953 Status = FindVariableEx (\r
2954 GetVariableNamePtr (Variable.CurrPtr),\r
fa0737a8 2955 GetVendorGuidPtr (Variable.CurrPtr),\r
23b06935
SZ
2956 FALSE,\r
2957 &VariablePtrTrack\r
2958 );\r
2959 if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State == VAR_ADDED) {\r
2960 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
2961 continue;\r
2962 }\r
2963 }\r
0f7aff72
RN
2964\r
2965 //\r
2966 // Don't return NV variable when HOB overrides it\r
2967 //\r
fa0737a8 2968 if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) && (VariableStoreHeader[VariableStoreTypeNv] != NULL) &&\r
0f7aff72
RN
2969 (Variable.StartPtr == GetStartPointer (VariableStoreHeader[VariableStoreTypeNv]))\r
2970 ) {\r
2971 VariableInHob.StartPtr = GetStartPointer (VariableStoreHeader[VariableStoreTypeHob]);\r
2972 VariableInHob.EndPtr = GetEndPointer (VariableStoreHeader[VariableStoreTypeHob]);\r
2973 Status = FindVariableEx (\r
2974 GetVariableNamePtr (Variable.CurrPtr),\r
fa0737a8 2975 GetVendorGuidPtr (Variable.CurrPtr),\r
9622df63 2976 FALSE,\r
0f7aff72
RN
2977 &VariableInHob\r
2978 );\r
2979 if (!EFI_ERROR (Status)) {\r
2980 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
2981 continue;\r
2982 }\r
2983 }\r
2984\r
fa0737a8
SZ
2985 *VariablePtr = Variable.CurrPtr;\r
2986 Status = EFI_SUCCESS;\r
72399dae 2987 goto Done;\r
2988 }\r
2989 }\r
2990\r
2991 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
2992 }\r
33a5a666 2993\r
8d3a5c82 2994Done:\r
fa0737a8
SZ
2995 return Status;\r
2996}\r
2997\r
2998/**\r
2999\r
3000 This code Finds the Next available variable.\r
3001\r
3002 Caution: This function may receive untrusted input.\r
3003 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r
3004\r
3005 @param VariableNameSize Size of the variable name.\r
3006 @param VariableName Pointer to variable name.\r
3007 @param VendorGuid Variable Vendor Guid.\r
3008\r
3009 @return EFI_INVALID_PARAMETER Invalid parameter.\r
3010 @return EFI_SUCCESS Find the specified variable.\r
3011 @return EFI_NOT_FOUND Not found.\r
3012 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.\r
3013\r
3014**/\r
3015EFI_STATUS\r
3016EFIAPI\r
3017VariableServiceGetNextVariableName (\r
3018 IN OUT UINTN *VariableNameSize,\r
3019 IN OUT CHAR16 *VariableName,\r
3020 IN OUT EFI_GUID *VendorGuid\r
3021 )\r
3022{\r
3023 EFI_STATUS Status;\r
3024 UINTN VarNameSize;\r
3025 VARIABLE_HEADER *VariablePtr;\r
3026\r
3027 if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {\r
3028 return EFI_INVALID_PARAMETER;\r
3029 }\r
3030\r
3031 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
3032\r
3033 Status = VariableServiceGetNextVariableInternal (VariableName, VendorGuid, &VariablePtr);\r
3034 if (!EFI_ERROR (Status)) {\r
3035 VarNameSize = NameSizeOfVariable (VariablePtr);\r
3036 ASSERT (VarNameSize != 0);\r
3037 if (VarNameSize <= *VariableNameSize) {\r
3038 CopyMem (VariableName, GetVariableNamePtr (VariablePtr), VarNameSize);\r
3039 CopyMem (VendorGuid, GetVendorGuidPtr (VariablePtr), sizeof (EFI_GUID));\r
3040 Status = EFI_SUCCESS;\r
3041 } else {\r
3042 Status = EFI_BUFFER_TOO_SMALL;\r
3043 }\r
3044\r
3045 *VariableNameSize = VarNameSize;\r
3046 }\r
3047\r
72399dae 3048 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
3049 return Status;\r
3050}\r
3051\r
3052/**\r
3053\r
3054 This code sets variable in storage blocks (Volatile or Non-Volatile).\r
3055\r
18a7dbbc
SZ
3056 Caution: This function may receive untrusted input.\r
3057 This function may be invoked in SMM mode, and datasize and data are external input.\r
3058 This function will do basic validation, before parse the data.\r
fa0737a8
SZ
3059 This function will parse the authentication carefully to avoid security issues, like\r
3060 buffer overflow, integer overflow.\r
3061 This function will check attribute carefully to avoid authentication bypass.\r
18a7dbbc 3062\r
8a2d4996 3063 @param VariableName Name of Variable to be found.\r
3064 @param VendorGuid Variable vendor GUID.\r
72399dae 3065 @param Attributes Attribute value of the variable found\r
3066 @param DataSize Size of Data found. If size is less than the\r
3067 data, this value contains the required size.\r
8a2d4996 3068 @param Data Data pointer.\r
72399dae 3069\r
8a2d4996 3070 @return EFI_INVALID_PARAMETER Invalid parameter.\r
3071 @return EFI_SUCCESS Set successfully.\r
3072 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.\r
3073 @return EFI_NOT_FOUND Not found.\r
3074 @return EFI_WRITE_PROTECTED Variable is read-only.\r
72399dae 3075\r
3076**/\r
3077EFI_STATUS\r
3078EFIAPI\r
8a2d4996 3079VariableServiceSetVariable (\r
72399dae 3080 IN CHAR16 *VariableName,\r
3081 IN EFI_GUID *VendorGuid,\r
3082 IN UINT32 Attributes,\r
3083 IN UINTN DataSize,\r
3084 IN VOID *Data\r
3085 )\r
3086{\r
3087 VARIABLE_POINTER_TRACK Variable;\r
3088 EFI_STATUS Status;\r
3089 VARIABLE_HEADER *NextVariable;\r
3090 EFI_PHYSICAL_ADDRESS Point;\r
fa0737a8 3091 UINTN PayloadSize;\r
72399dae 3092\r
3093 //\r
8a2d4996 3094 // Check input parameters.\r
72399dae 3095 //\r
3096 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r
3097 return EFI_INVALID_PARAMETER;\r
fa0737a8 3098 }\r
48a0e6bf 3099\r
3100 if (DataSize != 0 && Data == NULL) {\r
3101 return EFI_INVALID_PARAMETER;\r
3102 }\r
3103\r
8e38f18e 3104 //\r
fa0737a8 3105 // Check for reserverd bit in variable attribute.\r
8e38f18e 3106 //\r
fa0737a8 3107 if ((Attributes & (~EFI_VARIABLE_ATTRIBUTES_MASK)) != 0) {\r
8e38f18e 3108 return EFI_INVALID_PARAMETER;\r
3109 }\r
3110\r
72399dae 3111 //\r
8a2d4996 3112 // Make sure if runtime bit is set, boot service bit is set also.\r
72399dae 3113 //\r
3114 if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
3115 return EFI_INVALID_PARAMETER;\r
fa0737a8
SZ
3116 } else if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {\r
3117 if (!mVariableModuleGlobal->VariableGlobal.AuthSupport) {\r
3118 //\r
3119 // Not support authenticated variable write.\r
3120 //\r
3121 return EFI_INVALID_PARAMETER;\r
3122 }\r
3123 } else if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {\r
3124 if (PcdGet32 (PcdHwErrStorageSize) == 0) {\r
3125 //\r
3126 // Not support harware error record variable variable.\r
3127 //\r
3128 return EFI_INVALID_PARAMETER;\r
3129 }\r
3130 }\r
3131\r
3132 //\r
3133 // EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS and EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute\r
3134 // cannot be set both.\r
3135 //\r
3136 if (((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)\r
3137 && ((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {\r
3138 return EFI_INVALID_PARAMETER;\r
72399dae 3139 }\r
3140\r
fa0737a8
SZ
3141 if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) {\r
3142 if (DataSize < AUTHINFO_SIZE) {\r
3143 //\r
3144 // Try to write Authenticated Variable without AuthInfo.\r
3145 //\r
3146 return EFI_SECURITY_VIOLATION;\r
3147 }\r
3148 PayloadSize = DataSize - AUTHINFO_SIZE;\r
3149 } else if ((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {\r
3150 //\r
3151 // Sanity check for EFI_VARIABLE_AUTHENTICATION_2 descriptor.\r
3152 //\r
3153 if (DataSize < OFFSET_OF_AUTHINFO2_CERT_DATA ||\r
3154 ((EFI_VARIABLE_AUTHENTICATION_2 *) Data)->AuthInfo.Hdr.dwLength > DataSize - (OFFSET_OF (EFI_VARIABLE_AUTHENTICATION_2, AuthInfo)) ||\r
3155 ((EFI_VARIABLE_AUTHENTICATION_2 *) Data)->AuthInfo.Hdr.dwLength < OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID, CertData)) {\r
3156 return EFI_SECURITY_VIOLATION;\r
3157 }\r
3158 PayloadSize = DataSize - AUTHINFO2_SIZE (Data);\r
3159 } else {\r
3160 PayloadSize = DataSize;\r
3161 }\r
3162\r
3163 if ((UINTN)(~0) - PayloadSize < StrSize(VariableName)){\r
3164 //\r
3165 // Prevent whole variable size overflow\r
56251c66 3166 //\r
56251c66 3167 return EFI_INVALID_PARAMETER;\r
3168 }\r
3169\r
72399dae 3170 //\r
3171 // The size of the VariableName, including the Unicode Null in bytes plus\r
188e4e84 3172 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)\r
fa0737a8 3173 // bytes for HwErrRec#### variable.\r
72399dae 3174 //\r
3175 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
fa0737a8 3176 if (StrSize (VariableName) + PayloadSize > PcdGet32 (PcdMaxHardwareErrorVariableSize) - GetVariableHeaderSize ()) {\r
72399dae 3177 return EFI_INVALID_PARAMETER;\r
3178 }\r
72399dae 3179 } else {\r
3180 //\r
3181 // The size of the VariableName, including the Unicode Null in bytes plus\r
fa0737a8 3182 // the DataSize is limited to maximum size of Max(Auth)VariableSize bytes.\r
72399dae 3183 //\r
fa0737a8
SZ
3184 if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {\r
3185 if (StrSize (VariableName) + PayloadSize > mVariableModuleGlobal->MaxAuthVariableSize - GetVariableHeaderSize ()) {\r
3186 return EFI_INVALID_PARAMETER;\r
3187 }\r
3188 } else {\r
3189 if (StrSize (VariableName) + PayloadSize > mVariableModuleGlobal->MaxVariableSize - GetVariableHeaderSize ()) {\r
3190 return EFI_INVALID_PARAMETER;\r
3191 }\r
efb01a10 3192 }\r
6675a21f
SZ
3193 }\r
3194\r
8021f4c7 3195 Status = VarCheckLibSetVariableCheck (VariableName, VendorGuid, Attributes, PayloadSize, (VOID *) ((UINTN) Data + DataSize - PayloadSize), mRequestSource);\r
fa0737a8
SZ
3196 if (EFI_ERROR (Status)) {\r
3197 return Status;\r
3198 }\r
3199\r
72399dae 3200 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
3201\r
3202 //\r
8a2d4996 3203 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.\r
72399dae 3204 //\r
3205 if (1 < InterlockedIncrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState)) {\r
8a2d4996 3206 Point = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;\r
72399dae 3207 //\r
8a2d4996 3208 // Parse non-volatile variable data and get last variable offset.\r
72399dae 3209 //\r
3210 NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point);\r
6ebffb67 3211 while (IsValidVariableHeader (NextVariable, GetEndPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point))) {\r
72399dae 3212 NextVariable = GetNextVariablePtr (NextVariable);\r
3213 }\r
3214 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) Point;\r
3215 }\r
3216\r
3217 //\r
8a2d4996 3218 // Check whether the input variable is already existed.\r
72399dae 3219 //\r
9622df63
SZ
3220 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, TRUE);\r
3221 if (!EFI_ERROR (Status)) {\r
3222 if (((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) && AtRuntime ()) {\r
ff843847
RN
3223 Status = EFI_WRITE_PROTECTED;\r
3224 goto Done;\r
9622df63 3225 }\r
fa0737a8 3226 if (Attributes != 0 && (Attributes & (~EFI_VARIABLE_APPEND_WRITE)) != Variable.CurrPtr->Attributes) {\r
6e67fec0
SZ
3227 //\r
3228 // If a preexisting variable is rewritten with different attributes, SetVariable() shall not\r
3229 // modify the variable and shall return EFI_INVALID_PARAMETER. Two exceptions to this rule:\r
3230 // 1. No access attributes specified\r
3231 // 2. The only attribute differing is EFI_VARIABLE_APPEND_WRITE\r
3232 //\r
3233 Status = EFI_INVALID_PARAMETER;\r
4edb1866 3234 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
3235 goto Done;\r
3236 }\r
9622df63 3237 }\r
72399dae 3238\r
b2bd493e 3239 if (!FeaturePcdGet (PcdUefiVariableDefaultLangDeprecate)) {\r
9bc5dabb 3240 //\r
b2bd493e 3241 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.\r
9bc5dabb 3242 //\r
b2bd493e
SZ
3243 Status = AutoUpdateLangVariable (VariableName, Data, DataSize);\r
3244 if (EFI_ERROR (Status)) {\r
3245 //\r
3246 // The auto update operation failed, directly return to avoid inconsistency between PlatformLang and Lang.\r
3247 //\r
3248 goto Done;\r
3249 }\r
9bc5dabb 3250 }\r
72399dae 3251\r
fa0737a8
SZ
3252 if (mVariableModuleGlobal->VariableGlobal.AuthSupport) {\r
3253 Status = AuthVariableLibProcessVariable (VariableName, VendorGuid, Data, DataSize, Attributes);\r
3254 } else {\r
3255 Status = UpdateVariable (VariableName, VendorGuid, Data, DataSize, Attributes, 0, 0, &Variable, NULL);\r
3256 }\r
72399dae 3257\r
ff843847 3258Done:\r
fdb7765f 3259 InterlockedDecrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState);\r
052ad7e1 3260 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
fdb7765f 3261\r
fa0737a8
SZ
3262 if (!AtRuntime ()) {\r
3263 if (!EFI_ERROR (Status)) {\r
3264 SecureBootHook (\r
3265 VariableName,\r
3266 VendorGuid\r
3267 );\r
3268 }\r
3269 }\r
3270\r
8d3a5c82 3271 return Status;\r
3272}\r
3273\r
7c80e839 3274/**\r
8d3a5c82 3275\r
3276 This code returns information about the EFI variables.\r
3277\r
18a7dbbc
SZ
3278 Caution: This function may receive untrusted input.\r
3279 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r
3280\r
7c80e839 3281 @param Attributes Attributes bitmask to specify the type of variables\r
3282 on which to return information.\r
3283 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available\r
3284 for the EFI variables associated with the attributes specified.\r
3285 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available\r
3286 for EFI variables associated with the attributes specified.\r
3287 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables\r
3288 associated with the attributes specified.\r
8d3a5c82 3289\r
7c80e839 3290 @return EFI_SUCCESS Query successfully.\r
8d3a5c82 3291\r
7c80e839 3292**/\r
052ad7e1
A
3293EFI_STATUS\r
3294EFIAPI\r
b2bd493e 3295VariableServiceQueryVariableInfoInternal (\r
052ad7e1
A
3296 IN UINT32 Attributes,\r
3297 OUT UINT64 *MaximumVariableStorageSize,\r
3298 OUT UINT64 *RemainingVariableStorageSize,\r
3299 OUT UINT64 *MaximumVariableSize\r
3300 )\r
8d3a5c82 3301{\r
3302 VARIABLE_HEADER *Variable;\r
3303 VARIABLE_HEADER *NextVariable;\r
3304 UINT64 VariableSize;\r
3305 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
2fcdca1d 3306 UINT64 CommonVariableTotalSize;\r
3307 UINT64 HwErrVariableTotalSize;\r
b2bd493e
SZ
3308 EFI_STATUS Status;\r
3309 VARIABLE_POINTER_TRACK VariablePtrTrack;\r
2fcdca1d 3310\r
3311 CommonVariableTotalSize = 0;\r
3312 HwErrVariableTotalSize = 0;\r
8d3a5c82 3313\r
8d3a5c82 3314 if((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r
3315 //\r
3316 // Query is Volatile related.\r
3317 //\r
052ad7e1 3318 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r
8d3a5c82 3319 } else {\r
3320 //\r
3321 // Query is Non-Volatile related.\r
3322 //\r
8a2d4996 3323 VariableStoreHeader = mNvVariableCache;\r
8d3a5c82 3324 }\r
3325\r
3326 //\r
3327 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize\r
3328 // with the storage size (excluding the storage header size).\r
3329 //\r
3330 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);\r
c6492839 3331\r
3332 //\r
3333 // Harware error record variable needs larger size.\r
3334 //\r
2fcdca1d 3335 if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
188e4e84 3336 *MaximumVariableStorageSize = PcdGet32 (PcdHwErrStorageSize);\r
fa0737a8 3337 *MaximumVariableSize = PcdGet32 (PcdMaxHardwareErrorVariableSize) - GetVariableHeaderSize ();\r
2fcdca1d 3338 } else {\r
3339 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
4edb1866
SZ
3340 if (AtRuntime ()) {\r
3341 *MaximumVariableStorageSize = mVariableModuleGlobal->CommonRuntimeVariableSpace;\r
3342 } else {\r
3343 *MaximumVariableStorageSize = mVariableModuleGlobal->CommonVariableSpace;\r
3344 }\r
2fcdca1d 3345 }\r
3346\r
3347 //\r
fa0737a8 3348 // Let *MaximumVariableSize be Max(Auth)VariableSize with the exception of the variable header size.\r
2fcdca1d 3349 //\r
fa0737a8
SZ
3350 if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {\r
3351 *MaximumVariableSize = mVariableModuleGlobal->MaxAuthVariableSize - GetVariableHeaderSize ();\r
3352 } else {\r
3353 *MaximumVariableSize = mVariableModuleGlobal->MaxVariableSize - GetVariableHeaderSize ();\r
3354 }\r
c6492839 3355 }\r
8d3a5c82 3356\r
3357 //\r
3358 // Point to the starting address of the variables.\r
3359 //\r
9cad030b 3360 Variable = GetStartPointer (VariableStoreHeader);\r
8d3a5c82 3361\r
3362 //\r
3363 // Now walk through the related variable store.\r
3364 //\r
6ebffb67 3365 while (IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))) {\r
8d3a5c82 3366 NextVariable = GetNextVariablePtr (Variable);\r
3367 VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;\r
3368\r
8a2d4996 3369 if (AtRuntime ()) {\r
8d3a5c82 3370 //\r
8a2d4996 3371 // We don't take the state of the variables in mind\r
8d3a5c82 3372 // when calculating RemainingVariableStorageSize,\r
3373 // since the space occupied by variables not marked with\r
3374 // VAR_ADDED is not allowed to be reclaimed in Runtime.\r
3375 //\r
3b425367 3376 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2fcdca1d 3377 HwErrVariableTotalSize += VariableSize;\r
3378 } else {\r
3379 CommonVariableTotalSize += VariableSize;\r
3380 }\r
8d3a5c82 3381 } else {\r
3382 //\r
8a2d4996 3383 // Only care about Variables with State VAR_ADDED, because\r
8d3a5c82 3384 // the space not marked as VAR_ADDED is reclaimable now.\r
3385 //\r
3386 if (Variable->State == VAR_ADDED) {\r
3b425367 3387 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2fcdca1d 3388 HwErrVariableTotalSize += VariableSize;\r
3389 } else {\r
3390 CommonVariableTotalSize += VariableSize;\r
3391 }\r
b2bd493e
SZ
3392 } else if (Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
3393 //\r
3394 // If it is a IN_DELETED_TRANSITION variable,\r
3395 // and there is not also a same ADDED one at the same time,\r
3396 // this IN_DELETED_TRANSITION variable is valid.\r
3397 //\r
3398 VariablePtrTrack.StartPtr = GetStartPointer (VariableStoreHeader);\r
3399 VariablePtrTrack.EndPtr = GetEndPointer (VariableStoreHeader);\r
3400 Status = FindVariableEx (\r
3401 GetVariableNamePtr (Variable),\r
fa0737a8 3402 GetVendorGuidPtr (Variable),\r
b2bd493e
SZ
3403 FALSE,\r
3404 &VariablePtrTrack\r
3405 );\r
3406 if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State != VAR_ADDED) {\r
3407 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
3408 HwErrVariableTotalSize += VariableSize;\r
3409 } else {\r
3410 CommonVariableTotalSize += VariableSize;\r
3411 }\r
3412 }\r
8d3a5c82 3413 }\r
3414 }\r
3415\r
3416 //\r
8a2d4996 3417 // Go to the next one.\r
8d3a5c82 3418 //\r
3419 Variable = NextVariable;\r
3420 }\r
3421\r
2fcdca1d 3422 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD){\r
3423 *RemainingVariableStorageSize = *MaximumVariableStorageSize - HwErrVariableTotalSize;\r
4edb1866
SZ
3424 } else {\r
3425 if (*MaximumVariableStorageSize < CommonVariableTotalSize) {\r
3426 *RemainingVariableStorageSize = 0;\r
3427 } else {\r
3428 *RemainingVariableStorageSize = *MaximumVariableStorageSize - CommonVariableTotalSize;\r
3429 }\r
2fcdca1d 3430 }\r
3431\r
fa0737a8 3432 if (*RemainingVariableStorageSize < GetVariableHeaderSize ()) {\r
c6492839 3433 *MaximumVariableSize = 0;\r
fa0737a8
SZ
3434 } else if ((*RemainingVariableStorageSize - GetVariableHeaderSize ()) < *MaximumVariableSize) {\r
3435 *MaximumVariableSize = *RemainingVariableStorageSize - GetVariableHeaderSize ();\r
c6492839 3436 }\r
3437\r
8d3a5c82 3438 return EFI_SUCCESS;\r
3439}\r
3440\r
b2bd493e
SZ
3441/**\r
3442\r
3443 This code returns information about the EFI variables.\r
3444\r
18a7dbbc
SZ
3445 Caution: This function may receive untrusted input.\r
3446 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r
3447\r
b2bd493e
SZ
3448 @param Attributes Attributes bitmask to specify the type of variables\r
3449 on which to return information.\r
3450 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available\r
3451 for the EFI variables associated with the attributes specified.\r
3452 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available\r
3453 for EFI variables associated with the attributes specified.\r
3454 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables\r
3455 associated with the attributes specified.\r
3456\r
3457 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.\r
3458 @return EFI_SUCCESS Query successfully.\r
3459 @return EFI_UNSUPPORTED The attribute is not supported on this platform.\r
3460\r
3461**/\r
3462EFI_STATUS\r
3463EFIAPI\r
3464VariableServiceQueryVariableInfo (\r
3465 IN UINT32 Attributes,\r
3466 OUT UINT64 *MaximumVariableStorageSize,\r
3467 OUT UINT64 *RemainingVariableStorageSize,\r
3468 OUT UINT64 *MaximumVariableSize\r
3469 )\r
3470{\r
3471 EFI_STATUS Status;\r
3472\r
3473 if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {\r
3474 return EFI_INVALID_PARAMETER;\r
3475 }\r
3476\r
fa0737a8 3477 if ((Attributes & EFI_VARIABLE_ATTRIBUTES_MASK) == 0) {\r
b2bd493e
SZ
3478 //\r
3479 // Make sure the Attributes combination is supported by the platform.\r
3480 //\r
3481 return EFI_UNSUPPORTED;\r
3482 } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
3483 //\r
3484 // Make sure if runtime bit is set, boot service bit is set also.\r
3485 //\r
3486 return EFI_INVALID_PARAMETER;\r
3487 } else if (AtRuntime () && ((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0)) {\r
3488 //\r
3489 // Make sure RT Attribute is set if we are in Runtime phase.\r
3490 //\r
3491 return EFI_INVALID_PARAMETER;\r
3492 } else if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
3493 //\r
3494 // Make sure Hw Attribute is set with NV.\r
3495 //\r
3496 return EFI_INVALID_PARAMETER;\r
fa0737a8
SZ
3497 } else if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {\r
3498 if (!mVariableModuleGlobal->VariableGlobal.AuthSupport) {\r
3499 //\r
3500 // Not support authenticated variable write.\r
3501 //\r
3502 return EFI_UNSUPPORTED;\r
3503 }\r
3504 } else if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {\r
3505 if (PcdGet32 (PcdHwErrStorageSize) == 0) {\r
3506 //\r
3507 // Not support harware error record variable variable.\r
3508 //\r
3509 return EFI_UNSUPPORTED;\r
3510 }\r
b2bd493e
SZ
3511 }\r
3512\r
3513 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
3514\r
3515 Status = VariableServiceQueryVariableInfoInternal (\r
3516 Attributes,\r
3517 MaximumVariableStorageSize,\r
3518 RemainingVariableStorageSize,\r
3519 MaximumVariableSize\r
3520 );\r
3521\r
3522 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
3523 return Status;\r
3524}\r
7c80e839 3525\r
3526/**\r
8a2d4996 3527 This function reclaims variable storage if free size is below the threshold.\r
18a7dbbc
SZ
3528\r
3529 Caution: This function may be invoked at SMM mode.\r
3530 Care must be taken to make sure not security issue.\r
3531\r
7c80e839 3532**/\r
7800593d 3533VOID\r
7800593d 3534ReclaimForOS(\r
8a2d4996 3535 VOID\r
7800593d
LG
3536 )\r
3537{\r
9948c0b0 3538 EFI_STATUS Status;\r
4edb1866 3539 UINTN RemainingCommonRuntimeVariableSpace;\r
2fcdca1d 3540 UINTN RemainingHwErrVariableSpace;\r
0fb5e515
SZ
3541 STATIC BOOLEAN Reclaimed;\r
3542\r
3543 //\r
3544 // This function will be called only once at EndOfDxe or ReadyToBoot event.\r
3545 //\r
3546 if (Reclaimed) {\r
3547 return;\r
3548 }\r
3549 Reclaimed = TRUE;\r
7800593d 3550\r
4edb1866 3551 Status = EFI_SUCCESS;\r
7800593d 3552\r
4edb1866
SZ
3553 if (mVariableModuleGlobal->CommonRuntimeVariableSpace < mVariableModuleGlobal->CommonVariableTotalSize) {\r
3554 RemainingCommonRuntimeVariableSpace = 0;\r
3555 } else {\r
3556 RemainingCommonRuntimeVariableSpace = mVariableModuleGlobal->CommonRuntimeVariableSpace - mVariableModuleGlobal->CommonVariableTotalSize;\r
3557 }\r
2fcdca1d 3558\r
3559 RemainingHwErrVariableSpace = PcdGet32 (PcdHwErrStorageSize) - mVariableModuleGlobal->HwErrVariableTotalSize;\r
fa0737a8 3560\r
7800593d 3561 //\r
4edb1866 3562 // Check if the free area is below a threshold.\r
7800593d 3563 //\r
fa0737a8
SZ
3564 if (((RemainingCommonRuntimeVariableSpace < mVariableModuleGlobal->MaxVariableSize) ||\r
3565 (RemainingCommonRuntimeVariableSpace < mVariableModuleGlobal->MaxAuthVariableSize)) ||\r
3566 ((PcdGet32 (PcdHwErrStorageSize) != 0) &&\r
9948c0b0 3567 (RemainingHwErrVariableSpace < PcdGet32 (PcdMaxHardwareErrorVariableSize)))){\r
7800593d 3568 Status = Reclaim (\r
2fcdca1d 3569 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
3570 &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
3571 FALSE,\r
335e2681 3572 NULL,\r
7baf3c69
SZ
3573 NULL,\r
3574 0\r
2fcdca1d 3575 );\r
7800593d
LG
3576 ASSERT_EFI_ERROR (Status);\r
3577 }\r
3578}\r
3579\r
fa0737a8
SZ
3580/**\r
3581 Get non-volatile maximum variable size.\r
3582\r
3583 @return Non-volatile maximum variable size.\r
3584\r
3585**/\r
3586UINTN\r
3587GetNonVolatileMaxVariableSize (\r
3588 VOID\r
3589 )\r
3590{\r
3591 if (PcdGet32 (PcdHwErrStorageSize) != 0) {\r
3592 return MAX (MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxAuthVariableSize)),\r
3593 PcdGet32 (PcdMaxHardwareErrorVariableSize));\r
3594 } else {\r
3595 return MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxAuthVariableSize));\r
3596 }\r
3597}\r
3598\r
3e02ebb2
SZ
3599/**\r
3600 Init non-volatile variable store.\r
3601\r
fa0737a8
SZ
3602 @param[out] NvFvHeader Output pointer to non-volatile FV header address.\r
3603\r
3e02ebb2
SZ
3604 @retval EFI_SUCCESS Function successfully executed.\r
3605 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.\r
3606 @retval EFI_VOLUME_CORRUPTED Variable Store or Firmware Volume for Variable Store is corrupted.\r
3607\r
3608**/\r
3609EFI_STATUS\r
3610InitNonVolatileVariableStore (\r
fa0737a8 3611 OUT EFI_FIRMWARE_VOLUME_HEADER **NvFvHeader\r
3e02ebb2
SZ
3612 )\r
3613{\r
3614 EFI_FIRMWARE_VOLUME_HEADER *FvHeader;\r
4edb1866 3615 VARIABLE_HEADER *Variable;\r
3e02ebb2
SZ
3616 VARIABLE_HEADER *NextVariable;\r
3617 EFI_PHYSICAL_ADDRESS VariableStoreBase;\r
3618 UINT64 VariableStoreLength;\r
3619 UINTN VariableSize;\r
3620 EFI_HOB_GUID_TYPE *GuidHob;\r
3621 EFI_PHYSICAL_ADDRESS NvStorageBase;\r
3622 UINT8 *NvStorageData;\r
3623 UINT32 NvStorageSize;\r
3624 FAULT_TOLERANT_WRITE_LAST_WRITE_DATA *FtwLastWriteData;\r
3625 UINT32 BackUpOffset;\r
3626 UINT32 BackUpSize;\r
4edb1866
SZ
3627 UINT32 HwErrStorageSize;\r
3628 UINT32 MaxUserNvVariableSpaceSize;\r
3629 UINT32 BoottimeReservedNvVariableSpaceSize;\r
14326ed0
SZ
3630 EFI_STATUS Status;\r
3631 VOID *FtwProtocol;\r
3e02ebb2
SZ
3632\r
3633 mVariableModuleGlobal->FvbInstance = NULL;\r
3634\r
3e02ebb2
SZ
3635 //\r
3636 // Allocate runtime memory used for a memory copy of the FLASH region.\r
3637 // Keep the memory and the FLASH in sync as updates occur.\r
3638 //\r
3639 NvStorageSize = PcdGet32 (PcdFlashNvStorageVariableSize);\r
3640 NvStorageData = AllocateRuntimeZeroPool (NvStorageSize);\r
3641 if (NvStorageData == NULL) {\r
3642 return EFI_OUT_OF_RESOURCES;\r
3643 }\r
3644\r
3645 NvStorageBase = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);\r
3646 if (NvStorageBase == 0) {\r
3647 NvStorageBase = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);\r
3648 }\r
3649 //\r
3650 // Copy NV storage data to the memory buffer.\r
3651 //\r
3652 CopyMem (NvStorageData, (UINT8 *) (UINTN) NvStorageBase, NvStorageSize);\r
3653\r
14326ed0 3654 Status = GetFtwProtocol ((VOID **)&FtwProtocol);\r
3e02ebb2 3655 //\r
14326ed0 3656 // If FTW protocol has been installed, no need to check FTW last write data hob.\r
3e02ebb2 3657 //\r
14326ed0
SZ
3658 if (EFI_ERROR (Status)) {\r
3659 //\r
3660 // Check the FTW last write data hob.\r
3661 //\r
3662 GuidHob = GetFirstGuidHob (&gEdkiiFaultTolerantWriteGuid);\r
3663 if (GuidHob != NULL) {\r
3664 FtwLastWriteData = (FAULT_TOLERANT_WRITE_LAST_WRITE_DATA *) GET_GUID_HOB_DATA (GuidHob);\r
3665 if (FtwLastWriteData->TargetAddress == NvStorageBase) {\r
3666 DEBUG ((EFI_D_INFO, "Variable: NV storage is backed up in spare block: 0x%x\n", (UINTN) FtwLastWriteData->SpareAddress));\r
3667 //\r
3668 // Copy the backed up NV storage data to the memory buffer from spare block.\r
3669 //\r
3670 CopyMem (NvStorageData, (UINT8 *) (UINTN) (FtwLastWriteData->SpareAddress), NvStorageSize);\r
3671 } else if ((FtwLastWriteData->TargetAddress > NvStorageBase) &&\r
3672 (FtwLastWriteData->TargetAddress < (NvStorageBase + NvStorageSize))) {\r
3673 //\r
3674 // Flash NV storage from the Offset is backed up in spare block.\r
3675 //\r
3676 BackUpOffset = (UINT32) (FtwLastWriteData->TargetAddress - NvStorageBase);\r
3677 BackUpSize = NvStorageSize - BackUpOffset;\r
3678 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
3679 //\r
3680 // Copy the partial backed up NV storage data to the memory buffer from spare block.\r
3681 //\r
3682 CopyMem (NvStorageData + BackUpOffset, (UINT8 *) (UINTN) FtwLastWriteData->SpareAddress, BackUpSize);\r
3683 }\r
3e02ebb2
SZ
3684 }\r
3685 }\r
3686\r
3687 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) NvStorageData;\r
3688\r
3689 //\r
3690 // Check if the Firmware Volume is not corrupted\r
3691 //\r
3692 if ((FvHeader->Signature != EFI_FVH_SIGNATURE) || (!CompareGuid (&gEfiSystemNvDataFvGuid, &FvHeader->FileSystemGuid))) {\r
3693 FreePool (NvStorageData);\r
3694 DEBUG ((EFI_D_ERROR, "Firmware Volume for Variable Store is corrupted\n"));\r
3695 return EFI_VOLUME_CORRUPTED;\r
3696 }\r
3697\r
3698 VariableStoreBase = (EFI_PHYSICAL_ADDRESS) ((UINTN) FvHeader + FvHeader->HeaderLength);\r
3699 VariableStoreLength = (UINT64) (NvStorageSize - FvHeader->HeaderLength);\r
3700\r
9b18845a 3701 mNvFvHeaderCache = FvHeader;\r
3e02ebb2
SZ
3702 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase;\r
3703 mNvVariableCache = (VARIABLE_STORE_HEADER *) (UINTN) VariableStoreBase;\r
3704 if (GetVariableStoreStatus (mNvVariableCache) != EfiValid) {\r
3705 FreePool (NvStorageData);\r
9b18845a
DL
3706 mNvFvHeaderCache = NULL;\r
3707 mNvVariableCache = NULL;\r
3e02ebb2
SZ
3708 DEBUG((EFI_D_ERROR, "Variable Store header is corrupted\n"));\r
3709 return EFI_VOLUME_CORRUPTED;\r
3710 }\r
3711 ASSERT(mNvVariableCache->Size == VariableStoreLength);\r
3712\r
4edb1866
SZ
3713 ASSERT (sizeof (VARIABLE_STORE_HEADER) <= VariableStoreLength);\r
3714\r
fa0737a8
SZ
3715 mVariableModuleGlobal->VariableGlobal.AuthFormat = (BOOLEAN)(CompareGuid (&mNvVariableCache->Signature, &gEfiAuthenticatedVariableGuid));\r
3716\r
4edb1866
SZ
3717 HwErrStorageSize = PcdGet32 (PcdHwErrStorageSize);\r
3718 MaxUserNvVariableSpaceSize = PcdGet32 (PcdMaxUserNvVariableSpaceSize);\r
3719 BoottimeReservedNvVariableSpaceSize = PcdGet32 (PcdBoottimeReservedNvVariableSpaceSize);\r
3720\r
3721 //\r
3722 // Note that in EdkII variable driver implementation, Hardware Error Record type variable\r
3723 // is stored with common variable in the same NV region. So the platform integrator should\r
3724 // ensure that the value of PcdHwErrStorageSize is less than the value of\r
fa0737a8 3725 // (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)).\r
4edb1866
SZ
3726 //\r
3727 ASSERT (HwErrStorageSize < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)));\r
3728 //\r
3729 // Ensure that the value of PcdMaxUserNvVariableSpaceSize is less than the value of\r
fa0737a8 3730 // (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)) - PcdGet32 (PcdHwErrStorageSize).\r
4edb1866
SZ
3731 //\r
3732 ASSERT (MaxUserNvVariableSpaceSize < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER) - HwErrStorageSize));\r
3733 //\r
3734 // Ensure that the value of PcdBoottimeReservedNvVariableSpaceSize is less than the value of\r
fa0737a8 3735 // (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)) - PcdGet32 (PcdHwErrStorageSize).\r
4edb1866
SZ
3736 //\r
3737 ASSERT (BoottimeReservedNvVariableSpaceSize < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER) - HwErrStorageSize));\r
3738\r
3739 mVariableModuleGlobal->CommonVariableSpace = ((UINTN) VariableStoreLength - sizeof (VARIABLE_STORE_HEADER) - HwErrStorageSize);\r
3740 mVariableModuleGlobal->CommonMaxUserVariableSpace = ((MaxUserNvVariableSpaceSize != 0) ? MaxUserNvVariableSpaceSize : mVariableModuleGlobal->CommonVariableSpace);\r
3741 mVariableModuleGlobal->CommonRuntimeVariableSpace = mVariableModuleGlobal->CommonVariableSpace - BoottimeReservedNvVariableSpaceSize;\r
3742\r
3743 DEBUG ((EFI_D_INFO, "Variable driver common space: 0x%x 0x%x 0x%x\n", mVariableModuleGlobal->CommonVariableSpace, mVariableModuleGlobal->CommonMaxUserVariableSpace, mVariableModuleGlobal->CommonRuntimeVariableSpace));\r
3744\r
3e02ebb2 3745 //\r
fa0737a8 3746 // The max NV variable size should be < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)).\r
3e02ebb2 3747 //\r
fa0737a8
SZ
3748 ASSERT (GetNonVolatileMaxVariableSize () < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)));\r
3749\r
3750 mVariableModuleGlobal->MaxVariableSize = PcdGet32 (PcdMaxVariableSize);\r
3751 mVariableModuleGlobal->MaxAuthVariableSize = ((PcdGet32 (PcdMaxAuthVariableSize) != 0) ? PcdGet32 (PcdMaxAuthVariableSize) : mVariableModuleGlobal->MaxVariableSize);\r
3e02ebb2
SZ
3752\r
3753 //\r
3754 // Parse non-volatile variable data and get last variable offset.\r
3755 //\r
4edb1866
SZ
3756 Variable = GetStartPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase);\r
3757 while (IsValidVariableHeader (Variable, GetEndPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase))) {\r
3758 NextVariable = GetNextVariablePtr (Variable);\r
3759 VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
3760 if ((Variable->Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
3761 mVariableModuleGlobal->HwErrVariableTotalSize += VariableSize;\r
3e02ebb2 3762 } else {\r
4edb1866 3763 mVariableModuleGlobal->CommonVariableTotalSize += VariableSize;\r
3e02ebb2
SZ
3764 }\r
3765\r
4edb1866 3766 Variable = NextVariable;\r
3e02ebb2 3767 }\r
4edb1866 3768 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) Variable - (UINTN) VariableStoreBase;\r
3e02ebb2 3769\r
fa0737a8 3770 *NvFvHeader = FvHeader;\r
3e02ebb2
SZ
3771 return EFI_SUCCESS;\r
3772}\r
3773\r
335e2681
SZ
3774/**\r
3775 Flush the HOB variable to flash.\r
3776\r
3777 @param[in] VariableName Name of variable has been updated or deleted.\r
3778 @param[in] VendorGuid Guid of variable has been updated or deleted.\r
3779\r
3780**/\r
3781VOID\r
3782FlushHobVariableToFlash (\r
3783 IN CHAR16 *VariableName,\r
3784 IN EFI_GUID *VendorGuid\r
3785 )\r
3786{\r
3787 EFI_STATUS Status;\r
3788 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
3789 VARIABLE_HEADER *Variable;\r
3790 VOID *VariableData;\r
31349131 3791 VARIABLE_POINTER_TRACK VariablePtrTrack;\r
335e2681
SZ
3792 BOOLEAN ErrorFlag;\r
3793\r
3794 ErrorFlag = FALSE;\r
3795\r
3796 //\r
3797 // Flush the HOB variable to flash.\r
3798 //\r
3799 if (mVariableModuleGlobal->VariableGlobal.HobVariableBase != 0) {\r
3800 VariableStoreHeader = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;\r
3801 //\r
3802 // Set HobVariableBase to 0, it can avoid SetVariable to call back.\r
3803 //\r
3804 mVariableModuleGlobal->VariableGlobal.HobVariableBase = 0;\r
3805 for ( Variable = GetStartPointer (VariableStoreHeader)\r
6ebffb67 3806 ; IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))\r
335e2681
SZ
3807 ; Variable = GetNextVariablePtr (Variable)\r
3808 ) {\r
3809 if (Variable->State != VAR_ADDED) {\r
3810 //\r
3811 // The HOB variable has been set to DELETED state in local.\r
3812 //\r
3813 continue;\r
3814 }\r
3815 ASSERT ((Variable->Attributes & EFI_VARIABLE_NON_VOLATILE) != 0);\r
3816 if (VendorGuid == NULL || VariableName == NULL ||\r
fa0737a8 3817 !CompareGuid (VendorGuid, GetVendorGuidPtr (Variable)) ||\r
335e2681
SZ
3818 StrCmp (VariableName, GetVariableNamePtr (Variable)) != 0) {\r
3819 VariableData = GetVariableDataPtr (Variable);\r
31349131
SZ
3820 FindVariable (GetVariableNamePtr (Variable), GetVendorGuidPtr (Variable), &VariablePtrTrack, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
3821 Status = UpdateVariable (\r
335e2681 3822 GetVariableNamePtr (Variable),\r
fa0737a8 3823 GetVendorGuidPtr (Variable),\r
31349131 3824 VariableData,\r
fa0737a8 3825 DataSizeOfVariable (Variable),\r
31349131
SZ
3826 Variable->Attributes,\r
3827 0,\r
3828 0,\r
3829 &VariablePtrTrack,\r
3830 NULL\r
3831 );\r
fa0737a8 3832 DEBUG ((EFI_D_INFO, "Variable driver flush the HOB variable to flash: %g %s %r\n", GetVendorGuidPtr (Variable), GetVariableNamePtr (Variable), Status));\r
335e2681
SZ
3833 } else {\r
3834 //\r
31349131 3835 // The updated or deleted variable is matched with this HOB variable.\r
335e2681
SZ
3836 // Don't break here because we will try to set other HOB variables\r
3837 // since this variable could be set successfully.\r
3838 //\r
3839 Status = EFI_SUCCESS;\r
3840 }\r
3841 if (!EFI_ERROR (Status)) {\r
3842 //\r
3843 // If set variable successful, or the updated or deleted variable is matched with the HOB variable,\r
3844 // set the HOB variable to DELETED state in local.\r
3845 //\r
fa0737a8 3846 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
3847 Variable->State &= VAR_DELETED;\r
3848 } else {\r
3849 ErrorFlag = TRUE;\r
3850 }\r
3851 }\r
3852 if (ErrorFlag) {\r
3853 //\r
3854 // We still have HOB variable(s) not flushed in flash.\r
3855 //\r
3856 mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VariableStoreHeader;\r
3857 } else {\r
3858 //\r
3859 // All HOB variables have been flushed in flash.\r
3860 //\r
3861 DEBUG ((EFI_D_INFO, "Variable driver: all HOB variables have been flushed in flash.\n"));\r
3862 if (!AtRuntime ()) {\r
3863 FreePool ((VOID *) VariableStoreHeader);\r
3864 }\r
3865 }\r
3866 }\r
3867\r
3868}\r
8a2d4996 3869\r
7c80e839 3870/**\r
3e02ebb2 3871 Initializes variable write service after FTW was ready.\r
7c80e839 3872\r
8a2d4996 3873 @retval EFI_SUCCESS Function successfully executed.\r
3874 @retval Others Fail to initialize the variable service.\r
3875\r
3876**/\r
3877EFI_STATUS\r
3878VariableWriteServiceInitialize (\r
3879 VOID\r
3880 )\r
3881{\r
3882 EFI_STATUS Status;\r
8a2d4996 3883 UINTN Index;\r
3884 UINT8 Data;\r
8a2d4996 3885 EFI_PHYSICAL_ADDRESS VariableStoreBase;\r
3e02ebb2 3886 EFI_PHYSICAL_ADDRESS NvStorageBase;\r
fa0737a8 3887 VARIABLE_ENTRY_PROPERTY *VariableEntry;\r
8a2d4996 3888\r
31349131
SZ
3889 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
3890\r
3e02ebb2
SZ
3891 NvStorageBase = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);\r
3892 if (NvStorageBase == 0) {\r
3893 NvStorageBase = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);\r
3894 }\r
9b18845a 3895 VariableStoreBase = NvStorageBase + (mNvFvHeaderCache->HeaderLength);\r
3e02ebb2
SZ
3896\r
3897 //\r
3898 // Let NonVolatileVariableBase point to flash variable store base directly after FTW ready.\r
3899 //\r
3900 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase;\r
fa0737a8 3901\r
8a2d4996 3902 //\r
3903 // Check if the free area is really free.\r
3904 //\r
9b18845a 3905 for (Index = mVariableModuleGlobal->NonVolatileLastVariableOffset; Index < mNvVariableCache->Size; Index++) {\r
8a2d4996 3906 Data = ((UINT8 *) mNvVariableCache)[Index];\r
3907 if (Data != 0xff) {\r
3908 //\r
3909 // There must be something wrong in variable store, do reclaim operation.\r
3910 //\r
3911 Status = Reclaim (\r
3912 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
3913 &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
3914 FALSE,\r
335e2681 3915 NULL,\r
7baf3c69
SZ
3916 NULL,\r
3917 0\r
8a2d4996 3918 );\r
3919 if (EFI_ERROR (Status)) {\r
31349131 3920 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
8a2d4996 3921 return Status;\r
3922 }\r
3923 break;\r
3924 }\r
3925 }\r
3926\r
335e2681 3927 FlushHobVariableToFlash (NULL, NULL);\r
0f7aff72 3928\r
fa0737a8 3929 Status = EFI_SUCCESS;\r
8021f4c7 3930 ZeroMem (&mAuthContextOut, sizeof (mAuthContextOut));\r
fa0737a8
SZ
3931 if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {\r
3932 //\r
3933 // Authenticated variable initialize.\r
3934 //\r
8021f4c7
SZ
3935 mAuthContextIn.StructSize = sizeof (AUTH_VAR_LIB_CONTEXT_IN);\r
3936 mAuthContextIn.MaxAuthVariableSize = mVariableModuleGlobal->MaxAuthVariableSize - GetVariableHeaderSize ();\r
3937 Status = AuthVariableLibInitialize (&mAuthContextIn, &mAuthContextOut);\r
fa0737a8
SZ
3938 if (!EFI_ERROR (Status)) {\r
3939 DEBUG ((EFI_D_INFO, "Variable driver will work with auth variable support!\n"));\r
3940 mVariableModuleGlobal->VariableGlobal.AuthSupport = TRUE;\r
8021f4c7
SZ
3941 if (mAuthContextOut.AuthVarEntry != NULL) {\r
3942 for (Index = 0; Index < mAuthContextOut.AuthVarEntryCount; Index++) {\r
3943 VariableEntry = &mAuthContextOut.AuthVarEntry[Index];\r
3944 Status = VarCheckLibVariablePropertySet (\r
fa0737a8
SZ
3945 VariableEntry->Name,\r
3946 VariableEntry->Guid,\r
3947 &VariableEntry->VariableProperty\r
3948 );\r
3949 ASSERT_EFI_ERROR (Status);\r
3950 }\r
3951 }\r
3952 } else if (Status == EFI_UNSUPPORTED) {\r
3953 DEBUG ((EFI_D_INFO, "NOTICE - AuthVariableLibInitialize() returns %r!\n", Status));\r
3954 DEBUG ((EFI_D_INFO, "Variable driver will continue to work without auth variable support!\n"));\r
3955 mVariableModuleGlobal->VariableGlobal.AuthSupport = FALSE;\r
3956 Status = EFI_SUCCESS;\r
3957 }\r
3958 }\r
3959\r
3960 if (!EFI_ERROR (Status)) {\r
3961 for (Index = 0; Index < sizeof (mVariableEntryProperty) / sizeof (mVariableEntryProperty[0]); Index++) {\r
3962 VariableEntry = &mVariableEntryProperty[Index];\r
8021f4c7 3963 Status = VarCheckLibVariablePropertySet (VariableEntry->Name, VariableEntry->Guid, &VariableEntry->VariableProperty);\r
fa0737a8
SZ
3964 ASSERT_EFI_ERROR (Status);\r
3965 }\r
3966 }\r
3967\r
31349131 3968 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
fa0737a8 3969 return Status;\r
8a2d4996 3970}\r
3971\r
3972\r
3973/**\r
3974 Initializes variable store area for non-volatile and volatile variable.\r
7c80e839 3975\r
3976 @retval EFI_SUCCESS Function successfully executed.\r
3977 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.\r
3978\r
3979**/\r
8d3a5c82 3980EFI_STATUS\r
8d3a5c82 3981VariableCommonInitialize (\r
8a2d4996 3982 VOID\r
8d3a5c82 3983 )\r
8d3a5c82 3984{\r
3985 EFI_STATUS Status;\r
8d3a5c82 3986 VARIABLE_STORE_HEADER *VolatileVariableStore;\r
3987 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
052ad7e1 3988 UINT64 VariableStoreLength;\r
2fcdca1d 3989 UINTN ScratchSize;\r
0f7aff72 3990 EFI_HOB_GUID_TYPE *GuidHob;\r
fa0737a8
SZ
3991 EFI_GUID *VariableGuid;\r
3992 EFI_FIRMWARE_VOLUME_HEADER *NvFvHeader;\r
8d3a5c82 3993\r
7800593d
LG
3994 //\r
3995 // Allocate runtime memory for variable driver global structure.\r
3996 //\r
72399dae 3997 mVariableModuleGlobal = AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL));\r
7800593d
LG
3998 if (mVariableModuleGlobal == NULL) {\r
3999 return EFI_OUT_OF_RESOURCES;\r
4000 }\r
8d3a5c82 4001\r
8a2d4996 4002 InitializeLock (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock, TPL_NOTIFY);\r
8d3a5c82 4003\r
fa0737a8
SZ
4004 //\r
4005 // Init non-volatile variable store.\r
4006 //\r
26c2edd5 4007 NvFvHeader = NULL;\r
fa0737a8
SZ
4008 Status = InitNonVolatileVariableStore (&NvFvHeader);\r
4009 if (EFI_ERROR (Status)) {\r
4010 FreePool (mVariableModuleGlobal);\r
4011 return Status;\r
4012 }\r
4013\r
4014 //\r
4015 // mVariableModuleGlobal->VariableGlobal.AuthFormat\r
4016 // has been initialized in InitNonVolatileVariableStore().\r
4017 //\r
4018 if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {\r
4019 DEBUG ((EFI_D_INFO, "Variable driver will work with auth variable format!\n"));\r
4020 //\r
4021 // Set AuthSupport to FALSE first, VariableWriteServiceInitialize() will initialize it.\r
4022 //\r
4023 mVariableModuleGlobal->VariableGlobal.AuthSupport = FALSE;\r
4024 VariableGuid = &gEfiAuthenticatedVariableGuid;\r
4025 } else {\r
4026 DEBUG ((EFI_D_INFO, "Variable driver will work without auth variable support!\n"));\r
4027 mVariableModuleGlobal->VariableGlobal.AuthSupport = FALSE;\r
4028 VariableGuid = &gEfiVariableGuid;\r
4029 }\r
4030\r
0f7aff72
RN
4031 //\r
4032 // Get HOB variable store.\r
4033 //\r
fa0737a8 4034 GuidHob = GetFirstGuidHob (VariableGuid);\r
0f7aff72 4035 if (GuidHob != NULL) {\r
f68af18e 4036 VariableStoreHeader = GET_GUID_HOB_DATA (GuidHob);\r
335e2681 4037 VariableStoreLength = (UINT64) (GuidHob->Header.HobLength - sizeof (EFI_HOB_GUID_TYPE));\r
f68af18e 4038 if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) {\r
335e2681
SZ
4039 mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) AllocateRuntimeCopyPool ((UINTN) VariableStoreLength, (VOID *) VariableStoreHeader);\r
4040 if (mVariableModuleGlobal->VariableGlobal.HobVariableBase == 0) {\r
fa0737a8 4041 FreePool (NvFvHeader);\r
3e02ebb2 4042 FreePool (mVariableModuleGlobal);\r
335e2681
SZ
4043 return EFI_OUT_OF_RESOURCES;\r
4044 }\r
f68af18e
RN
4045 } else {\r
4046 DEBUG ((EFI_D_ERROR, "HOB Variable Store header is corrupted!\n"));\r
4047 }\r
0f7aff72
RN
4048 }\r
4049\r
8d3a5c82 4050 //\r
2fcdca1d 4051 // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.\r
8d3a5c82 4052 //\r
fa0737a8
SZ
4053 ScratchSize = GetNonVolatileMaxVariableSize ();\r
4054 mVariableModuleGlobal->ScratchBufferSize = ScratchSize;\r
188e4e84 4055 VolatileVariableStore = AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize) + ScratchSize);\r
8d3a5c82 4056 if (VolatileVariableStore == NULL) {\r
3e02ebb2
SZ
4057 if (mVariableModuleGlobal->VariableGlobal.HobVariableBase != 0) {\r
4058 FreePool ((VOID *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase);\r
4059 }\r
fa0737a8 4060 FreePool (NvFvHeader);\r
8d3a5c82 4061 FreePool (mVariableModuleGlobal);\r
4062 return EFI_OUT_OF_RESOURCES;\r
4063 }\r
4064\r
188e4e84 4065 SetMem (VolatileVariableStore, PcdGet32 (PcdVariableStoreSize) + ScratchSize, 0xff);\r
8d3a5c82 4066\r
4067 //\r
8a2d4996 4068 // Initialize Variable Specific Data.\r
8d3a5c82 4069 //\r
052ad7e1 4070 mVariableModuleGlobal->VariableGlobal.VolatileVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VolatileVariableStore;\r
9cad030b 4071 mVariableModuleGlobal->VolatileLastVariableOffset = (UINTN) GetStartPointer (VolatileVariableStore) - (UINTN) VolatileVariableStore;\r
8d3a5c82 4072\r
fa0737a8 4073 CopyGuid (&VolatileVariableStore->Signature, VariableGuid);\r
8a2d4996 4074 VolatileVariableStore->Size = PcdGet32 (PcdVariableStoreSize);\r
4075 VolatileVariableStore->Format = VARIABLE_STORE_FORMATTED;\r
4076 VolatileVariableStore->State = VARIABLE_STORE_HEALTHY;\r
4077 VolatileVariableStore->Reserved = 0;\r
4078 VolatileVariableStore->Reserved1 = 0;\r
8d3a5c82 4079\r
fa0737a8 4080 return EFI_SUCCESS;\r
8d3a5c82 4081}\r
052ad7e1 4082\r
052ad7e1 4083\r
aa75dfec 4084/**\r
8a2d4996 4085 Get the proper fvb handle and/or fvb protocol by the given Flash address.\r
aa75dfec 4086\r
fa0737a8 4087 @param[in] Address The Flash address.\r
8a2d4996 4088 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.\r
4089 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.\r
aa75dfec 4090\r
aa75dfec 4091**/\r
8a2d4996 4092EFI_STATUS\r
4093GetFvbInfoByAddress (\r
4094 IN EFI_PHYSICAL_ADDRESS Address,\r
4095 OUT EFI_HANDLE *FvbHandle OPTIONAL,\r
4096 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvbProtocol OPTIONAL\r
8a9e0b72 4097 )\r
4098{\r
8a2d4996 4099 EFI_STATUS Status;\r
4100 EFI_HANDLE *HandleBuffer;\r
4101 UINTN HandleCount;\r
4102 UINTN Index;\r
4103 EFI_PHYSICAL_ADDRESS FvbBaseAddress;\r
4104 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r
8a2d4996 4105 EFI_FVB_ATTRIBUTES_2 Attributes;\r
1fcbeaea
DG
4106 UINTN BlockSize;\r
4107 UINTN NumberOfBlocks;\r
4108\r
4e1005ec 4109 HandleBuffer = NULL;\r
8a9e0b72 4110 //\r
8a2d4996 4111 // Get all FVB handles.\r
8a9e0b72 4112 //\r
8a2d4996 4113 Status = GetFvbCountAndBuffer (&HandleCount, &HandleBuffer);\r
8a9e0b72 4114 if (EFI_ERROR (Status)) {\r
8a2d4996 4115 return EFI_NOT_FOUND;\r
8a9e0b72 4116 }\r
8a2d4996 4117\r
8a9e0b72 4118 //\r
8a2d4996 4119 // Get the FVB to access variable store.\r
8a9e0b72 4120 //\r
8a2d4996 4121 Fvb = NULL;\r
f0480ecf 4122 for (Index = 0; Index < HandleCount; Index += 1, Status = EFI_NOT_FOUND, Fvb = NULL) {\r
8a2d4996 4123 Status = GetFvbByHandle (HandleBuffer[Index], &Fvb);\r
8a9e0b72 4124 if (EFI_ERROR (Status)) {\r
4125 Status = EFI_NOT_FOUND;\r
4126 break;\r
4127 }\r
4128\r
4129 //\r
4130 // Ensure this FVB protocol supported Write operation.\r
4131 //\r
4132 Status = Fvb->GetAttributes (Fvb, &Attributes);\r
4133 if (EFI_ERROR (Status) || ((Attributes & EFI_FVB2_WRITE_STATUS) == 0)) {\r
1fcbeaea 4134 continue;\r
8a9e0b72 4135 }\r
1fcbeaea 4136\r
8a9e0b72 4137 //\r
8a2d4996 4138 // Compare the address and select the right one.\r
8a9e0b72 4139 //\r
4140 Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);\r
4141 if (EFI_ERROR (Status)) {\r
4142 continue;\r
4143 }\r
4144\r
1fcbeaea
DG
4145 //\r
4146 // Assume one FVB has one type of BlockSize.\r
4147 //\r
4148 Status = Fvb->GetBlockSize (Fvb, 0, &BlockSize, &NumberOfBlocks);\r
4149 if (EFI_ERROR (Status)) {\r
4150 continue;\r
4151 }\r
4152\r
4153 if ((Address >= FvbBaseAddress) && (Address < (FvbBaseAddress + BlockSize * NumberOfBlocks))) {\r
8a2d4996 4154 if (FvbHandle != NULL) {\r
4155 *FvbHandle = HandleBuffer[Index];\r
4156 }\r
4157 if (FvbProtocol != NULL) {\r
4158 *FvbProtocol = Fvb;\r
4159 }\r
4160 Status = EFI_SUCCESS;\r
8a9e0b72 4161 break;\r
4162 }\r
4163 }\r
8a9e0b72 4164 FreePool (HandleBuffer);\r
533020ef 4165\r
8a2d4996 4166 if (Fvb == NULL) {\r
4167 Status = EFI_NOT_FOUND;\r
8a9e0b72 4168 }\r
fa0737a8
SZ
4169\r
4170 return Status;\r
052ad7e1
A
4171}\r
4172\r