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