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