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