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