]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
MdeModulePkg/Variable: Consolidate common parsing functions
[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
2361\r
2362 if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {\r
2363 return EFI_INVALID_PARAMETER;\r
2364 }\r
2365\r
6f817f9b
SZ
2366 //\r
2367 // Calculate the possible maximum length of name string, including the Null terminator.\r
2368 //\r
2369 MaxLen = *VariableNameSize / sizeof (CHAR16);\r
2370 if ((MaxLen == 0) || (StrnLenS (VariableName, MaxLen) == MaxLen)) {\r
2371 //\r
2372 // Null-terminator is not found in the first VariableNameSize bytes of the input VariableName buffer,\r
2373 // follow spec to return EFI_INVALID_PARAMETER.\r
2374 //\r
2375 return EFI_INVALID_PARAMETER;\r
2376 }\r
2377\r
fa0737a8
SZ
2378 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2379\r
2380 Status = VariableServiceGetNextVariableInternal (VariableName, VendorGuid, &VariablePtr);\r
2381 if (!EFI_ERROR (Status)) {\r
2382 VarNameSize = NameSizeOfVariable (VariablePtr);\r
2383 ASSERT (VarNameSize != 0);\r
2384 if (VarNameSize <= *VariableNameSize) {\r
2385 CopyMem (VariableName, GetVariableNamePtr (VariablePtr), VarNameSize);\r
2386 CopyMem (VendorGuid, GetVendorGuidPtr (VariablePtr), sizeof (EFI_GUID));\r
2387 Status = EFI_SUCCESS;\r
2388 } else {\r
2389 Status = EFI_BUFFER_TOO_SMALL;\r
2390 }\r
2391\r
2392 *VariableNameSize = VarNameSize;\r
2393 }\r
2394\r
72399dae 2395 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2396 return Status;\r
2397}\r
2398\r
2399/**\r
2400\r
2401 This code sets variable in storage blocks (Volatile or Non-Volatile).\r
2402\r
18a7dbbc
SZ
2403 Caution: This function may receive untrusted input.\r
2404 This function may be invoked in SMM mode, and datasize and data are external input.\r
2405 This function will do basic validation, before parse the data.\r
fa0737a8
SZ
2406 This function will parse the authentication carefully to avoid security issues, like\r
2407 buffer overflow, integer overflow.\r
2408 This function will check attribute carefully to avoid authentication bypass.\r
18a7dbbc 2409\r
8a2d4996 2410 @param VariableName Name of Variable to be found.\r
2411 @param VendorGuid Variable vendor GUID.\r
72399dae 2412 @param Attributes Attribute value of the variable found\r
2413 @param DataSize Size of Data found. If size is less than the\r
2414 data, this value contains the required size.\r
8a2d4996 2415 @param Data Data pointer.\r
72399dae 2416\r
8a2d4996 2417 @return EFI_INVALID_PARAMETER Invalid parameter.\r
2418 @return EFI_SUCCESS Set successfully.\r
2419 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.\r
2420 @return EFI_NOT_FOUND Not found.\r
2421 @return EFI_WRITE_PROTECTED Variable is read-only.\r
72399dae 2422\r
2423**/\r
2424EFI_STATUS\r
2425EFIAPI\r
8a2d4996 2426VariableServiceSetVariable (\r
72399dae 2427 IN CHAR16 *VariableName,\r
2428 IN EFI_GUID *VendorGuid,\r
2429 IN UINT32 Attributes,\r
2430 IN UINTN DataSize,\r
2431 IN VOID *Data\r
2432 )\r
2433{\r
2434 VARIABLE_POINTER_TRACK Variable;\r
2435 EFI_STATUS Status;\r
2436 VARIABLE_HEADER *NextVariable;\r
2437 EFI_PHYSICAL_ADDRESS Point;\r
fa0737a8 2438 UINTN PayloadSize;\r
72399dae 2439\r
2440 //\r
8a2d4996 2441 // Check input parameters.\r
72399dae 2442 //\r
2443 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r
2444 return EFI_INVALID_PARAMETER;\r
fa0737a8 2445 }\r
48a0e6bf 2446\r
2447 if (DataSize != 0 && Data == NULL) {\r
2448 return EFI_INVALID_PARAMETER;\r
2449 }\r
2450\r
8e38f18e 2451 //\r
fa0737a8 2452 // Check for reserverd bit in variable attribute.\r
4073f85d
ZC
2453 // EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is deprecated but we still allow\r
2454 // the delete operation of common authenticated variable at user physical presence.\r
2455 // So leave EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS attribute check to AuthVariableLib\r
8e38f18e 2456 //\r
4073f85d 2457 if ((Attributes & (~(EFI_VARIABLE_ATTRIBUTES_MASK | EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS))) != 0) {\r
8e38f18e 2458 return EFI_INVALID_PARAMETER;\r
2459 }\r
2460\r
72399dae 2461 //\r
8a2d4996 2462 // Make sure if runtime bit is set, boot service bit is set also.\r
72399dae 2463 //\r
2464 if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
67943427 2465 if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {\r
2466 return EFI_UNSUPPORTED;\r
2467 } else {\r
2468 return EFI_INVALID_PARAMETER;\r
2469 }\r
fa0737a8
SZ
2470 } else if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {\r
2471 if (!mVariableModuleGlobal->VariableGlobal.AuthSupport) {\r
2472 //\r
2473 // Not support authenticated variable write.\r
2474 //\r
2475 return EFI_INVALID_PARAMETER;\r
2476 }\r
2477 } else if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {\r
2478 if (PcdGet32 (PcdHwErrStorageSize) == 0) {\r
2479 //\r
2480 // Not support harware error record variable variable.\r
2481 //\r
2482 return EFI_INVALID_PARAMETER;\r
2483 }\r
2484 }\r
2485\r
2486 //\r
2487 // EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS and EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute\r
2488 // cannot be set both.\r
2489 //\r
2490 if (((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)\r
2491 && ((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {\r
67943427 2492 return EFI_UNSUPPORTED;\r
72399dae 2493 }\r
2494\r
fa0737a8 2495 if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) {\r
67943427 2496 //\r
2497 // If DataSize == AUTHINFO_SIZE and then PayloadSize is 0.\r
2498 // Maybe it's the delete operation of common authenticated variable at user physical presence.\r
2499 //\r
2500 if (DataSize != AUTHINFO_SIZE) {\r
2501 return EFI_UNSUPPORTED;\r
fa0737a8
SZ
2502 }\r
2503 PayloadSize = DataSize - AUTHINFO_SIZE;\r
2504 } else if ((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {\r
2505 //\r
2506 // Sanity check for EFI_VARIABLE_AUTHENTICATION_2 descriptor.\r
2507 //\r
2508 if (DataSize < OFFSET_OF_AUTHINFO2_CERT_DATA ||\r
2509 ((EFI_VARIABLE_AUTHENTICATION_2 *) Data)->AuthInfo.Hdr.dwLength > DataSize - (OFFSET_OF (EFI_VARIABLE_AUTHENTICATION_2, AuthInfo)) ||\r
2510 ((EFI_VARIABLE_AUTHENTICATION_2 *) Data)->AuthInfo.Hdr.dwLength < OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID, CertData)) {\r
2511 return EFI_SECURITY_VIOLATION;\r
2512 }\r
e83d841f 2513 //\r
49395ea0
HW
2514 // The VariableSpeculationBarrier() call here is to ensure the above sanity\r
2515 // check for the EFI_VARIABLE_AUTHENTICATION_2 descriptor has been completed\r
e83d841f
HW
2516 // before the execution of subsequent codes.\r
2517 //\r
49395ea0 2518 VariableSpeculationBarrier ();\r
fa0737a8
SZ
2519 PayloadSize = DataSize - AUTHINFO2_SIZE (Data);\r
2520 } else {\r
2521 PayloadSize = DataSize;\r
2522 }\r
2523\r
2524 if ((UINTN)(~0) - PayloadSize < StrSize(VariableName)){\r
2525 //\r
2526 // Prevent whole variable size overflow\r
56251c66 2527 //\r
56251c66 2528 return EFI_INVALID_PARAMETER;\r
2529 }\r
2530\r
72399dae 2531 //\r
2532 // The size of the VariableName, including the Unicode Null in bytes plus\r
188e4e84 2533 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)\r
fa0737a8 2534 // bytes for HwErrRec#### variable.\r
72399dae 2535 //\r
2536 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
fa0737a8 2537 if (StrSize (VariableName) + PayloadSize > PcdGet32 (PcdMaxHardwareErrorVariableSize) - GetVariableHeaderSize ()) {\r
72399dae 2538 return EFI_INVALID_PARAMETER;\r
2539 }\r
72399dae 2540 } else {\r
2541 //\r
2542 // The size of the VariableName, including the Unicode Null in bytes plus\r
9b4a2032 2543 // the DataSize is limited to maximum size of Max(Auth|Volatile)VariableSize bytes.\r
72399dae 2544 //\r
fa0737a8
SZ
2545 if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {\r
2546 if (StrSize (VariableName) + PayloadSize > mVariableModuleGlobal->MaxAuthVariableSize - GetVariableHeaderSize ()) {\r
a90c7df7
VS
2547 DEBUG ((DEBUG_ERROR,\r
2548 "%a: Failed to set variable '%s' with Guid %g\n",\r
2549 __FUNCTION__, VariableName, VendorGuid));\r
2550 DEBUG ((DEBUG_ERROR,\r
2551 "NameSize(0x%x) + PayloadSize(0x%x) > "\r
2552 "MaxAuthVariableSize(0x%x) - HeaderSize(0x%x)\n",\r
2553 StrSize (VariableName), PayloadSize,\r
2554 mVariableModuleGlobal->MaxAuthVariableSize,\r
2555 GetVariableHeaderSize ()\r
2556 ));\r
fa0737a8
SZ
2557 return EFI_INVALID_PARAMETER;\r
2558 }\r
9b4a2032 2559 } else if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
fa0737a8 2560 if (StrSize (VariableName) + PayloadSize > mVariableModuleGlobal->MaxVariableSize - GetVariableHeaderSize ()) {\r
a90c7df7
VS
2561 DEBUG ((DEBUG_ERROR,\r
2562 "%a: Failed to set variable '%s' with Guid %g\n",\r
2563 __FUNCTION__, VariableName, VendorGuid));\r
2564 DEBUG ((DEBUG_ERROR,\r
2565 "NameSize(0x%x) + PayloadSize(0x%x) > "\r
2566 "MaxVariableSize(0x%x) - HeaderSize(0x%x)\n",\r
2567 StrSize (VariableName), PayloadSize,\r
2568 mVariableModuleGlobal->MaxVariableSize,\r
2569 GetVariableHeaderSize ()\r
2570 ));\r
fa0737a8
SZ
2571 return EFI_INVALID_PARAMETER;\r
2572 }\r
9b4a2032
LE
2573 } else {\r
2574 if (StrSize (VariableName) + PayloadSize > mVariableModuleGlobal->MaxVolatileVariableSize - GetVariableHeaderSize ()) {\r
a90c7df7
VS
2575 DEBUG ((DEBUG_ERROR,\r
2576 "%a: Failed to set variable '%s' with Guid %g\n",\r
2577 __FUNCTION__, VariableName, VendorGuid));\r
2578 DEBUG ((DEBUG_ERROR,\r
2579 "NameSize(0x%x) + PayloadSize(0x%x) > "\r
2580 "MaxVolatileVariableSize(0x%x) - HeaderSize(0x%x)\n",\r
2581 StrSize (VariableName), PayloadSize,\r
2582 mVariableModuleGlobal->MaxVolatileVariableSize,\r
2583 GetVariableHeaderSize ()\r
2584 ));\r
9b4a2032
LE
2585 return EFI_INVALID_PARAMETER;\r
2586 }\r
efb01a10 2587 }\r
6675a21f
SZ
2588 }\r
2589\r
2f6aa774
JY
2590 //\r
2591 // Special Handling for MOR Lock variable.\r
2592 //\r
2593 Status = SetVariableCheckHandlerMor (VariableName, VendorGuid, Attributes, PayloadSize, (VOID *) ((UINTN) Data + DataSize - PayloadSize));\r
2594 if (Status == EFI_ALREADY_STARTED) {\r
2595 //\r
2596 // EFI_ALREADY_STARTED means the SetVariable() action is handled inside of SetVariableCheckHandlerMor().\r
2597 // Variable driver can just return SUCCESS.\r
2598 //\r
2599 return EFI_SUCCESS;\r
2600 }\r
2601 if (EFI_ERROR (Status)) {\r
2602 return Status;\r
2603 }\r
2604\r
8021f4c7 2605 Status = VarCheckLibSetVariableCheck (VariableName, VendorGuid, Attributes, PayloadSize, (VOID *) ((UINTN) Data + DataSize - PayloadSize), mRequestSource);\r
fa0737a8
SZ
2606 if (EFI_ERROR (Status)) {\r
2607 return Status;\r
2608 }\r
2609\r
72399dae 2610 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2611\r
2612 //\r
8a2d4996 2613 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.\r
72399dae 2614 //\r
2615 if (1 < InterlockedIncrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState)) {\r
8a2d4996 2616 Point = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;\r
72399dae 2617 //\r
8a2d4996 2618 // Parse non-volatile variable data and get last variable offset.\r
72399dae 2619 //\r
2620 NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point);\r
6ebffb67 2621 while (IsValidVariableHeader (NextVariable, GetEndPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point))) {\r
72399dae 2622 NextVariable = GetNextVariablePtr (NextVariable);\r
2623 }\r
2624 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) Point;\r
2625 }\r
2626\r
2627 //\r
8a2d4996 2628 // Check whether the input variable is already existed.\r
72399dae 2629 //\r
9622df63
SZ
2630 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, TRUE);\r
2631 if (!EFI_ERROR (Status)) {\r
2632 if (((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) && AtRuntime ()) {\r
ff843847
RN
2633 Status = EFI_WRITE_PROTECTED;\r
2634 goto Done;\r
9622df63 2635 }\r
fa0737a8 2636 if (Attributes != 0 && (Attributes & (~EFI_VARIABLE_APPEND_WRITE)) != Variable.CurrPtr->Attributes) {\r
6e67fec0
SZ
2637 //\r
2638 // If a preexisting variable is rewritten with different attributes, SetVariable() shall not\r
2639 // modify the variable and shall return EFI_INVALID_PARAMETER. Two exceptions to this rule:\r
2640 // 1. No access attributes specified\r
2641 // 2. The only attribute differing is EFI_VARIABLE_APPEND_WRITE\r
2642 //\r
2643 Status = EFI_INVALID_PARAMETER;\r
4edb1866 2644 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
2645 goto Done;\r
2646 }\r
9622df63 2647 }\r
72399dae 2648\r
b2bd493e 2649 if (!FeaturePcdGet (PcdUefiVariableDefaultLangDeprecate)) {\r
9bc5dabb 2650 //\r
b2bd493e 2651 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.\r
9bc5dabb 2652 //\r
b2bd493e
SZ
2653 Status = AutoUpdateLangVariable (VariableName, Data, DataSize);\r
2654 if (EFI_ERROR (Status)) {\r
2655 //\r
2656 // The auto update operation failed, directly return to avoid inconsistency between PlatformLang and Lang.\r
2657 //\r
2658 goto Done;\r
2659 }\r
9bc5dabb 2660 }\r
72399dae 2661\r
fa0737a8
SZ
2662 if (mVariableModuleGlobal->VariableGlobal.AuthSupport) {\r
2663 Status = AuthVariableLibProcessVariable (VariableName, VendorGuid, Data, DataSize, Attributes);\r
2664 } else {\r
2665 Status = UpdateVariable (VariableName, VendorGuid, Data, DataSize, Attributes, 0, 0, &Variable, NULL);\r
2666 }\r
72399dae 2667\r
ff843847 2668Done:\r
fdb7765f 2669 InterlockedDecrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState);\r
052ad7e1 2670 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
fdb7765f 2671\r
fa0737a8
SZ
2672 if (!AtRuntime ()) {\r
2673 if (!EFI_ERROR (Status)) {\r
2674 SecureBootHook (\r
2675 VariableName,\r
2676 VendorGuid\r
2677 );\r
2678 }\r
2679 }\r
2680\r
8d3a5c82 2681 return Status;\r
2682}\r
2683\r
7c80e839 2684/**\r
8d3a5c82 2685\r
2686 This code returns information about the EFI variables.\r
2687\r
18a7dbbc
SZ
2688 Caution: This function may receive untrusted input.\r
2689 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r
2690\r
7c80e839 2691 @param Attributes Attributes bitmask to specify the type of variables\r
2692 on which to return information.\r
2693 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available\r
2694 for the EFI variables associated with the attributes specified.\r
2695 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available\r
2696 for EFI variables associated with the attributes specified.\r
2697 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables\r
2698 associated with the attributes specified.\r
8d3a5c82 2699\r
7c80e839 2700 @return EFI_SUCCESS Query successfully.\r
8d3a5c82 2701\r
7c80e839 2702**/\r
052ad7e1
A
2703EFI_STATUS\r
2704EFIAPI\r
b2bd493e 2705VariableServiceQueryVariableInfoInternal (\r
052ad7e1
A
2706 IN UINT32 Attributes,\r
2707 OUT UINT64 *MaximumVariableStorageSize,\r
2708 OUT UINT64 *RemainingVariableStorageSize,\r
2709 OUT UINT64 *MaximumVariableSize\r
2710 )\r
8d3a5c82 2711{\r
2712 VARIABLE_HEADER *Variable;\r
2713 VARIABLE_HEADER *NextVariable;\r
2714 UINT64 VariableSize;\r
2715 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
2fcdca1d 2716 UINT64 CommonVariableTotalSize;\r
2717 UINT64 HwErrVariableTotalSize;\r
b2bd493e
SZ
2718 EFI_STATUS Status;\r
2719 VARIABLE_POINTER_TRACK VariablePtrTrack;\r
2fcdca1d 2720\r
2721 CommonVariableTotalSize = 0;\r
2722 HwErrVariableTotalSize = 0;\r
8d3a5c82 2723\r
8d3a5c82 2724 if((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r
2725 //\r
2726 // Query is Volatile related.\r
2727 //\r
052ad7e1 2728 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r
8d3a5c82 2729 } else {\r
2730 //\r
2731 // Query is Non-Volatile related.\r
2732 //\r
8a2d4996 2733 VariableStoreHeader = mNvVariableCache;\r
8d3a5c82 2734 }\r
2735\r
2736 //\r
2737 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize\r
2738 // with the storage size (excluding the storage header size).\r
2739 //\r
2740 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);\r
c6492839 2741\r
2742 //\r
2743 // Harware error record variable needs larger size.\r
2744 //\r
2fcdca1d 2745 if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
188e4e84 2746 *MaximumVariableStorageSize = PcdGet32 (PcdHwErrStorageSize);\r
fa0737a8 2747 *MaximumVariableSize = PcdGet32 (PcdMaxHardwareErrorVariableSize) - GetVariableHeaderSize ();\r
2fcdca1d 2748 } else {\r
2749 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
4edb1866
SZ
2750 if (AtRuntime ()) {\r
2751 *MaximumVariableStorageSize = mVariableModuleGlobal->CommonRuntimeVariableSpace;\r
2752 } else {\r
2753 *MaximumVariableStorageSize = mVariableModuleGlobal->CommonVariableSpace;\r
2754 }\r
2fcdca1d 2755 }\r
2756\r
2757 //\r
9b4a2032 2758 // Let *MaximumVariableSize be Max(Auth|Volatile)VariableSize with the exception of the variable header size.\r
2fcdca1d 2759 //\r
fa0737a8
SZ
2760 if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {\r
2761 *MaximumVariableSize = mVariableModuleGlobal->MaxAuthVariableSize - GetVariableHeaderSize ();\r
9b4a2032 2762 } else if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
fa0737a8 2763 *MaximumVariableSize = mVariableModuleGlobal->MaxVariableSize - GetVariableHeaderSize ();\r
9b4a2032
LE
2764 } else {\r
2765 *MaximumVariableSize = mVariableModuleGlobal->MaxVolatileVariableSize - GetVariableHeaderSize ();\r
fa0737a8 2766 }\r
c6492839 2767 }\r
8d3a5c82 2768\r
2769 //\r
2770 // Point to the starting address of the variables.\r
2771 //\r
9cad030b 2772 Variable = GetStartPointer (VariableStoreHeader);\r
8d3a5c82 2773\r
2774 //\r
2775 // Now walk through the related variable store.\r
2776 //\r
6ebffb67 2777 while (IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))) {\r
8d3a5c82 2778 NextVariable = GetNextVariablePtr (Variable);\r
2779 VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;\r
2780\r
8a2d4996 2781 if (AtRuntime ()) {\r
8d3a5c82 2782 //\r
8a2d4996 2783 // We don't take the state of the variables in mind\r
8d3a5c82 2784 // when calculating RemainingVariableStorageSize,\r
2785 // since the space occupied by variables not marked with\r
2786 // VAR_ADDED is not allowed to be reclaimed in Runtime.\r
2787 //\r
3b425367 2788 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2fcdca1d 2789 HwErrVariableTotalSize += VariableSize;\r
2790 } else {\r
2791 CommonVariableTotalSize += VariableSize;\r
2792 }\r
8d3a5c82 2793 } else {\r
2794 //\r
8a2d4996 2795 // Only care about Variables with State VAR_ADDED, because\r
8d3a5c82 2796 // the space not marked as VAR_ADDED is reclaimable now.\r
2797 //\r
2798 if (Variable->State == VAR_ADDED) {\r
3b425367 2799 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2fcdca1d 2800 HwErrVariableTotalSize += VariableSize;\r
2801 } else {\r
2802 CommonVariableTotalSize += VariableSize;\r
2803 }\r
b2bd493e
SZ
2804 } else if (Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
2805 //\r
2806 // If it is a IN_DELETED_TRANSITION variable,\r
2807 // and there is not also a same ADDED one at the same time,\r
2808 // this IN_DELETED_TRANSITION variable is valid.\r
2809 //\r
2810 VariablePtrTrack.StartPtr = GetStartPointer (VariableStoreHeader);\r
2811 VariablePtrTrack.EndPtr = GetEndPointer (VariableStoreHeader);\r
2812 Status = FindVariableEx (\r
2813 GetVariableNamePtr (Variable),\r
fa0737a8 2814 GetVendorGuidPtr (Variable),\r
b2bd493e
SZ
2815 FALSE,\r
2816 &VariablePtrTrack\r
2817 );\r
2818 if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State != VAR_ADDED) {\r
2819 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2820 HwErrVariableTotalSize += VariableSize;\r
2821 } else {\r
2822 CommonVariableTotalSize += VariableSize;\r
2823 }\r
2824 }\r
8d3a5c82 2825 }\r
2826 }\r
2827\r
2828 //\r
8a2d4996 2829 // Go to the next one.\r
8d3a5c82 2830 //\r
2831 Variable = NextVariable;\r
2832 }\r
2833\r
2fcdca1d 2834 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD){\r
2835 *RemainingVariableStorageSize = *MaximumVariableStorageSize - HwErrVariableTotalSize;\r
4edb1866
SZ
2836 } else {\r
2837 if (*MaximumVariableStorageSize < CommonVariableTotalSize) {\r
2838 *RemainingVariableStorageSize = 0;\r
2839 } else {\r
2840 *RemainingVariableStorageSize = *MaximumVariableStorageSize - CommonVariableTotalSize;\r
2841 }\r
2fcdca1d 2842 }\r
2843\r
fa0737a8 2844 if (*RemainingVariableStorageSize < GetVariableHeaderSize ()) {\r
c6492839 2845 *MaximumVariableSize = 0;\r
fa0737a8
SZ
2846 } else if ((*RemainingVariableStorageSize - GetVariableHeaderSize ()) < *MaximumVariableSize) {\r
2847 *MaximumVariableSize = *RemainingVariableStorageSize - GetVariableHeaderSize ();\r
c6492839 2848 }\r
2849\r
8d3a5c82 2850 return EFI_SUCCESS;\r
2851}\r
2852\r
b2bd493e
SZ
2853/**\r
2854\r
2855 This code returns information about the EFI variables.\r
2856\r
18a7dbbc
SZ
2857 Caution: This function may receive untrusted input.\r
2858 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r
2859\r
b2bd493e
SZ
2860 @param Attributes Attributes bitmask to specify the type of variables\r
2861 on which to return information.\r
2862 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available\r
2863 for the EFI variables associated with the attributes specified.\r
2864 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available\r
2865 for EFI variables associated with the attributes specified.\r
2866 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables\r
2867 associated with the attributes specified.\r
2868\r
2869 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.\r
2870 @return EFI_SUCCESS Query successfully.\r
2871 @return EFI_UNSUPPORTED The attribute is not supported on this platform.\r
2872\r
2873**/\r
2874EFI_STATUS\r
2875EFIAPI\r
2876VariableServiceQueryVariableInfo (\r
2877 IN UINT32 Attributes,\r
2878 OUT UINT64 *MaximumVariableStorageSize,\r
2879 OUT UINT64 *RemainingVariableStorageSize,\r
2880 OUT UINT64 *MaximumVariableSize\r
2881 )\r
2882{\r
2883 EFI_STATUS Status;\r
2884\r
2885 if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {\r
2886 return EFI_INVALID_PARAMETER;\r
2887 }\r
2888\r
67943427 2889 if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {\r
2890 //\r
2891 // Deprecated attribute, make this check as highest priority.\r
2892 //\r
2893 return EFI_UNSUPPORTED;\r
2894 }\r
2895\r
fa0737a8 2896 if ((Attributes & EFI_VARIABLE_ATTRIBUTES_MASK) == 0) {\r
b2bd493e
SZ
2897 //\r
2898 // Make sure the Attributes combination is supported by the platform.\r
2899 //\r
2900 return EFI_UNSUPPORTED;\r
2901 } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
2902 //\r
2903 // Make sure if runtime bit is set, boot service bit is set also.\r
2904 //\r
2905 return EFI_INVALID_PARAMETER;\r
2906 } else if (AtRuntime () && ((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0)) {\r
2907 //\r
2908 // Make sure RT Attribute is set if we are in Runtime phase.\r
2909 //\r
2910 return EFI_INVALID_PARAMETER;\r
2911 } else if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2912 //\r
2913 // Make sure Hw Attribute is set with NV.\r
2914 //\r
2915 return EFI_INVALID_PARAMETER;\r
fa0737a8
SZ
2916 } else if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {\r
2917 if (!mVariableModuleGlobal->VariableGlobal.AuthSupport) {\r
2918 //\r
2919 // Not support authenticated variable write.\r
2920 //\r
2921 return EFI_UNSUPPORTED;\r
2922 }\r
2923 } else if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {\r
2924 if (PcdGet32 (PcdHwErrStorageSize) == 0) {\r
2925 //\r
2926 // Not support harware error record variable variable.\r
2927 //\r
2928 return EFI_UNSUPPORTED;\r
2929 }\r
b2bd493e
SZ
2930 }\r
2931\r
2932 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2933\r
2934 Status = VariableServiceQueryVariableInfoInternal (\r
2935 Attributes,\r
2936 MaximumVariableStorageSize,\r
2937 RemainingVariableStorageSize,\r
2938 MaximumVariableSize\r
2939 );\r
2940\r
2941 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2942 return Status;\r
2943}\r
7c80e839 2944\r
2945/**\r
8a2d4996 2946 This function reclaims variable storage if free size is below the threshold.\r
18a7dbbc
SZ
2947\r
2948 Caution: This function may be invoked at SMM mode.\r
2949 Care must be taken to make sure not security issue.\r
2950\r
7c80e839 2951**/\r
7800593d 2952VOID\r
7800593d 2953ReclaimForOS(\r
8a2d4996 2954 VOID\r
7800593d
LG
2955 )\r
2956{\r
9948c0b0 2957 EFI_STATUS Status;\r
4edb1866 2958 UINTN RemainingCommonRuntimeVariableSpace;\r
2fcdca1d 2959 UINTN RemainingHwErrVariableSpace;\r
0fb5e515
SZ
2960 STATIC BOOLEAN Reclaimed;\r
2961\r
2962 //\r
2963 // This function will be called only once at EndOfDxe or ReadyToBoot event.\r
2964 //\r
2965 if (Reclaimed) {\r
2966 return;\r
2967 }\r
2968 Reclaimed = TRUE;\r
7800593d 2969\r
4edb1866 2970 Status = EFI_SUCCESS;\r
7800593d 2971\r
4edb1866
SZ
2972 if (mVariableModuleGlobal->CommonRuntimeVariableSpace < mVariableModuleGlobal->CommonVariableTotalSize) {\r
2973 RemainingCommonRuntimeVariableSpace = 0;\r
2974 } else {\r
2975 RemainingCommonRuntimeVariableSpace = mVariableModuleGlobal->CommonRuntimeVariableSpace - mVariableModuleGlobal->CommonVariableTotalSize;\r
2976 }\r
2fcdca1d 2977\r
2978 RemainingHwErrVariableSpace = PcdGet32 (PcdHwErrStorageSize) - mVariableModuleGlobal->HwErrVariableTotalSize;\r
fa0737a8 2979\r
7800593d 2980 //\r
4edb1866 2981 // Check if the free area is below a threshold.\r
7800593d 2982 //\r
fa0737a8
SZ
2983 if (((RemainingCommonRuntimeVariableSpace < mVariableModuleGlobal->MaxVariableSize) ||\r
2984 (RemainingCommonRuntimeVariableSpace < mVariableModuleGlobal->MaxAuthVariableSize)) ||\r
2985 ((PcdGet32 (PcdHwErrStorageSize) != 0) &&\r
9948c0b0 2986 (RemainingHwErrVariableSpace < PcdGet32 (PcdMaxHardwareErrorVariableSize)))){\r
7800593d 2987 Status = Reclaim (\r
2fcdca1d 2988 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
2989 &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
2990 FALSE,\r
335e2681 2991 NULL,\r
7baf3c69
SZ
2992 NULL,\r
2993 0\r
2fcdca1d 2994 );\r
7800593d
LG
2995 ASSERT_EFI_ERROR (Status);\r
2996 }\r
2997}\r
2998\r
fa0737a8
SZ
2999/**\r
3000 Get non-volatile maximum variable size.\r
3001\r
3002 @return Non-volatile maximum variable size.\r
3003\r
3004**/\r
3005UINTN\r
3006GetNonVolatileMaxVariableSize (\r
3007 VOID\r
3008 )\r
3009{\r
3010 if (PcdGet32 (PcdHwErrStorageSize) != 0) {\r
3011 return MAX (MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxAuthVariableSize)),\r
3012 PcdGet32 (PcdMaxHardwareErrorVariableSize));\r
3013 } else {\r
3014 return MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxAuthVariableSize));\r
3015 }\r
3016}\r
3017\r
9b4a2032
LE
3018/**\r
3019 Get maximum variable size, covering both non-volatile and volatile variables.\r
3020\r
3021 @return Maximum variable size.\r
3022\r
3023**/\r
3024UINTN\r
3025GetMaxVariableSize (\r
3026 VOID\r
3027 )\r
3028{\r
3029 UINTN MaxVariableSize;\r
3030\r
3031 MaxVariableSize = GetNonVolatileMaxVariableSize();\r
3032 //\r
3033 // The condition below fails implicitly if PcdMaxVolatileVariableSize equals\r
3034 // the default zero value.\r
3035 //\r
3036 if (MaxVariableSize < PcdGet32 (PcdMaxVolatileVariableSize)) {\r
3037 MaxVariableSize = PcdGet32 (PcdMaxVolatileVariableSize);\r
3038 }\r
3039 return MaxVariableSize;\r
3040}\r
3041\r
3e02ebb2 3042/**\r
701d603f 3043 Init real non-volatile variable store.\r
3e02ebb2 3044\r
701d603f 3045 @param[out] VariableStoreBase Output pointer to real non-volatile variable store base.\r
fa0737a8 3046\r
3e02ebb2
SZ
3047 @retval EFI_SUCCESS Function successfully executed.\r
3048 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.\r
3049 @retval EFI_VOLUME_CORRUPTED Variable Store or Firmware Volume for Variable Store is corrupted.\r
3050\r
3051**/\r
3052EFI_STATUS\r
701d603f
SZ
3053InitRealNonVolatileVariableStore (\r
3054 OUT EFI_PHYSICAL_ADDRESS *VariableStoreBase\r
3e02ebb2
SZ
3055 )\r
3056{\r
3057 EFI_FIRMWARE_VOLUME_HEADER *FvHeader;\r
701d603f
SZ
3058 VARIABLE_STORE_HEADER *VariableStore;\r
3059 UINT32 VariableStoreLength;\r
3e02ebb2
SZ
3060 EFI_HOB_GUID_TYPE *GuidHob;\r
3061 EFI_PHYSICAL_ADDRESS NvStorageBase;\r
3062 UINT8 *NvStorageData;\r
3063 UINT32 NvStorageSize;\r
3064 FAULT_TOLERANT_WRITE_LAST_WRITE_DATA *FtwLastWriteData;\r
3065 UINT32 BackUpOffset;\r
3066 UINT32 BackUpSize;\r
4edb1866
SZ
3067 UINT32 HwErrStorageSize;\r
3068 UINT32 MaxUserNvVariableSpaceSize;\r
3069 UINT32 BoottimeReservedNvVariableSpaceSize;\r
14326ed0
SZ
3070 EFI_STATUS Status;\r
3071 VOID *FtwProtocol;\r
3e02ebb2
SZ
3072\r
3073 mVariableModuleGlobal->FvbInstance = NULL;\r
3074\r
3e02ebb2
SZ
3075 //\r
3076 // Allocate runtime memory used for a memory copy of the FLASH region.\r
3077 // Keep the memory and the FLASH in sync as updates occur.\r
3078 //\r
3079 NvStorageSize = PcdGet32 (PcdFlashNvStorageVariableSize);\r
3080 NvStorageData = AllocateRuntimeZeroPool (NvStorageSize);\r
3081 if (NvStorageData == NULL) {\r
3082 return EFI_OUT_OF_RESOURCES;\r
3083 }\r
3084\r
904e0ca9 3085 NvStorageBase = NV_STORAGE_VARIABLE_BASE;\r
701d603f
SZ
3086 ASSERT (NvStorageBase != 0);\r
3087\r
3e02ebb2
SZ
3088 //\r
3089 // Copy NV storage data to the memory buffer.\r
3090 //\r
3091 CopyMem (NvStorageData, (UINT8 *) (UINTN) NvStorageBase, NvStorageSize);\r
3092\r
14326ed0 3093 Status = GetFtwProtocol ((VOID **)&FtwProtocol);\r
3e02ebb2 3094 //\r
14326ed0 3095 // If FTW protocol has been installed, no need to check FTW last write data hob.\r
3e02ebb2 3096 //\r
14326ed0
SZ
3097 if (EFI_ERROR (Status)) {\r
3098 //\r
3099 // Check the FTW last write data hob.\r
3100 //\r
3101 GuidHob = GetFirstGuidHob (&gEdkiiFaultTolerantWriteGuid);\r
3102 if (GuidHob != NULL) {\r
3103 FtwLastWriteData = (FAULT_TOLERANT_WRITE_LAST_WRITE_DATA *) GET_GUID_HOB_DATA (GuidHob);\r
3104 if (FtwLastWriteData->TargetAddress == NvStorageBase) {\r
3105 DEBUG ((EFI_D_INFO, "Variable: NV storage is backed up in spare block: 0x%x\n", (UINTN) FtwLastWriteData->SpareAddress));\r
3106 //\r
3107 // Copy the backed up NV storage data to the memory buffer from spare block.\r
3108 //\r
3109 CopyMem (NvStorageData, (UINT8 *) (UINTN) (FtwLastWriteData->SpareAddress), NvStorageSize);\r
3110 } else if ((FtwLastWriteData->TargetAddress > NvStorageBase) &&\r
3111 (FtwLastWriteData->TargetAddress < (NvStorageBase + NvStorageSize))) {\r
3112 //\r
3113 // Flash NV storage from the Offset is backed up in spare block.\r
3114 //\r
3115 BackUpOffset = (UINT32) (FtwLastWriteData->TargetAddress - NvStorageBase);\r
3116 BackUpSize = NvStorageSize - BackUpOffset;\r
3117 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
3118 //\r
3119 // Copy the partial backed up NV storage data to the memory buffer from spare block.\r
3120 //\r
3121 CopyMem (NvStorageData + BackUpOffset, (UINT8 *) (UINTN) FtwLastWriteData->SpareAddress, BackUpSize);\r
3122 }\r
3e02ebb2
SZ
3123 }\r
3124 }\r
3125\r
3126 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) NvStorageData;\r
3127\r
3128 //\r
3129 // Check if the Firmware Volume is not corrupted\r
3130 //\r
3131 if ((FvHeader->Signature != EFI_FVH_SIGNATURE) || (!CompareGuid (&gEfiSystemNvDataFvGuid, &FvHeader->FileSystemGuid))) {\r
3132 FreePool (NvStorageData);\r
3133 DEBUG ((EFI_D_ERROR, "Firmware Volume for Variable Store is corrupted\n"));\r
3134 return EFI_VOLUME_CORRUPTED;\r
3135 }\r
3136\r
701d603f 3137 VariableStore = (VARIABLE_STORE_HEADER *) ((UINTN) FvHeader + FvHeader->HeaderLength);\r
16f69227 3138 VariableStoreLength = NvStorageSize - FvHeader->HeaderLength;\r
701d603f
SZ
3139 ASSERT (sizeof (VARIABLE_STORE_HEADER) <= VariableStoreLength);\r
3140 ASSERT (VariableStore->Size == VariableStoreLength);\r
3e02ebb2 3141\r
701d603f
SZ
3142 //\r
3143 // Check if the Variable Store header is not corrupted\r
3144 //\r
3145 if (GetVariableStoreStatus (VariableStore) != EfiValid) {\r
3e02ebb2
SZ
3146 FreePool (NvStorageData);\r
3147 DEBUG((EFI_D_ERROR, "Variable Store header is corrupted\n"));\r
3148 return EFI_VOLUME_CORRUPTED;\r
3149 }\r
3e02ebb2 3150\r
701d603f 3151 mNvFvHeaderCache = FvHeader;\r
4edb1866 3152\r
701d603f 3153 *VariableStoreBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VariableStore;\r
fa0737a8 3154\r
4edb1866
SZ
3155 HwErrStorageSize = PcdGet32 (PcdHwErrStorageSize);\r
3156 MaxUserNvVariableSpaceSize = PcdGet32 (PcdMaxUserNvVariableSpaceSize);\r
3157 BoottimeReservedNvVariableSpaceSize = PcdGet32 (PcdBoottimeReservedNvVariableSpaceSize);\r
3158\r
3159 //\r
3160 // Note that in EdkII variable driver implementation, Hardware Error Record type variable\r
3161 // is stored with common variable in the same NV region. So the platform integrator should\r
3162 // ensure that the value of PcdHwErrStorageSize is less than the value of\r
fa0737a8 3163 // (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)).\r
4edb1866
SZ
3164 //\r
3165 ASSERT (HwErrStorageSize < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)));\r
3166 //\r
3167 // Ensure that the value of PcdMaxUserNvVariableSpaceSize is less than the value of\r
fa0737a8 3168 // (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)) - PcdGet32 (PcdHwErrStorageSize).\r
4edb1866
SZ
3169 //\r
3170 ASSERT (MaxUserNvVariableSpaceSize < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER) - HwErrStorageSize));\r
3171 //\r
3172 // Ensure that the value of PcdBoottimeReservedNvVariableSpaceSize is less than the value of\r
fa0737a8 3173 // (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)) - PcdGet32 (PcdHwErrStorageSize).\r
4edb1866
SZ
3174 //\r
3175 ASSERT (BoottimeReservedNvVariableSpaceSize < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER) - HwErrStorageSize));\r
3176\r
3177 mVariableModuleGlobal->CommonVariableSpace = ((UINTN) VariableStoreLength - sizeof (VARIABLE_STORE_HEADER) - HwErrStorageSize);\r
3178 mVariableModuleGlobal->CommonMaxUserVariableSpace = ((MaxUserNvVariableSpaceSize != 0) ? MaxUserNvVariableSpaceSize : mVariableModuleGlobal->CommonVariableSpace);\r
3179 mVariableModuleGlobal->CommonRuntimeVariableSpace = mVariableModuleGlobal->CommonVariableSpace - BoottimeReservedNvVariableSpaceSize;\r
3180\r
3181 DEBUG ((EFI_D_INFO, "Variable driver common space: 0x%x 0x%x 0x%x\n", mVariableModuleGlobal->CommonVariableSpace, mVariableModuleGlobal->CommonMaxUserVariableSpace, mVariableModuleGlobal->CommonRuntimeVariableSpace));\r
3182\r
3e02ebb2 3183 //\r
fa0737a8 3184 // The max NV variable size should be < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)).\r
3e02ebb2 3185 //\r
fa0737a8
SZ
3186 ASSERT (GetNonVolatileMaxVariableSize () < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)));\r
3187\r
701d603f
SZ
3188 return EFI_SUCCESS;\r
3189}\r
3190\r
7cd69959
SZ
3191/**\r
3192 Init emulated non-volatile variable store.\r
3193\r
3194 @param[out] VariableStoreBase Output pointer to emulated non-volatile variable store base.\r
3195\r
3196 @retval EFI_SUCCESS Function successfully executed.\r
3197 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.\r
3198\r
3199**/\r
3200EFI_STATUS\r
3201InitEmuNonVolatileVariableStore (\r
3202 EFI_PHYSICAL_ADDRESS *VariableStoreBase\r
3203 )\r
3204{\r
3205 VARIABLE_STORE_HEADER *VariableStore;\r
3206 UINT32 VariableStoreLength;\r
3207 BOOLEAN FullyInitializeStore;\r
3208 UINT32 HwErrStorageSize;\r
3209\r
3210 FullyInitializeStore = TRUE;\r
3211\r
3212 VariableStoreLength = PcdGet32 (PcdVariableStoreSize);\r
3213 ASSERT (sizeof (VARIABLE_STORE_HEADER) <= VariableStoreLength);\r
3214\r
3215 //\r
3216 // Allocate memory for variable store.\r
3217 //\r
3218 if (PcdGet64 (PcdEmuVariableNvStoreReserved) == 0) {\r
3219 VariableStore = (VARIABLE_STORE_HEADER *) AllocateRuntimePool (VariableStoreLength);\r
3220 if (VariableStore == NULL) {\r
3221 return EFI_OUT_OF_RESOURCES;\r
3222 }\r
3223 } else {\r
3224 //\r
3225 // A memory location has been reserved for the NV variable store. Certain\r
3226 // platforms may be able to preserve a memory range across system resets,\r
3227 // thereby providing better NV variable emulation.\r
3228 //\r
3229 VariableStore =\r
3230 (VARIABLE_STORE_HEADER *)(VOID*)(UINTN)\r
3231 PcdGet64 (PcdEmuVariableNvStoreReserved);\r
3232 if ((VariableStore->Size == VariableStoreLength) &&\r
3233 (CompareGuid (&VariableStore->Signature, &gEfiAuthenticatedVariableGuid) ||\r
3234 CompareGuid (&VariableStore->Signature, &gEfiVariableGuid)) &&\r
3235 (VariableStore->Format == VARIABLE_STORE_FORMATTED) &&\r
3236 (VariableStore->State == VARIABLE_STORE_HEALTHY)) {\r
3237 DEBUG((\r
3238 DEBUG_INFO,\r
3239 "Variable Store reserved at %p appears to be valid\n",\r
3240 VariableStore\r
3241 ));\r
3242 FullyInitializeStore = FALSE;\r
3243 }\r
3244 }\r
3245\r
3246 if (FullyInitializeStore) {\r
3247 SetMem (VariableStore, VariableStoreLength, 0xff);\r
3248 //\r
3249 // Use gEfiAuthenticatedVariableGuid for potential auth variable support.\r
3250 //\r
3251 CopyGuid (&VariableStore->Signature, &gEfiAuthenticatedVariableGuid);\r
3252 VariableStore->Size = VariableStoreLength;\r
3253 VariableStore->Format = VARIABLE_STORE_FORMATTED;\r
3254 VariableStore->State = VARIABLE_STORE_HEALTHY;\r
3255 VariableStore->Reserved = 0;\r
3256 VariableStore->Reserved1 = 0;\r
3257 }\r
3258\r
3259 *VariableStoreBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VariableStore;\r
3260\r
3261 HwErrStorageSize = PcdGet32 (PcdHwErrStorageSize);\r
3262\r
3263 //\r
3264 // Note that in EdkII variable driver implementation, Hardware Error Record type variable\r
3265 // is stored with common variable in the same NV region. So the platform integrator should\r
3266 // ensure that the value of PcdHwErrStorageSize is less than the value of\r
3267 // (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)).\r
3268 //\r
3269 ASSERT (HwErrStorageSize < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)));\r
3270\r
3271 mVariableModuleGlobal->CommonVariableSpace = ((UINTN) VariableStoreLength - sizeof (VARIABLE_STORE_HEADER) - HwErrStorageSize);\r
3272 mVariableModuleGlobal->CommonMaxUserVariableSpace = mVariableModuleGlobal->CommonVariableSpace;\r
3273 mVariableModuleGlobal->CommonRuntimeVariableSpace = mVariableModuleGlobal->CommonVariableSpace;\r
3274\r
3275 return EFI_SUCCESS;\r
3276}\r
3277\r
701d603f
SZ
3278/**\r
3279 Init non-volatile variable store.\r
3280\r
3281 @retval EFI_SUCCESS Function successfully executed.\r
3282 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.\r
3283 @retval EFI_VOLUME_CORRUPTED Variable Store or Firmware Volume for Variable Store is corrupted.\r
3284\r
3285**/\r
3286EFI_STATUS\r
3287InitNonVolatileVariableStore (\r
3288 VOID\r
3289 )\r
3290{\r
3291 VARIABLE_HEADER *Variable;\r
3292 VARIABLE_HEADER *NextVariable;\r
3293 EFI_PHYSICAL_ADDRESS VariableStoreBase;\r
3294 UINTN VariableSize;\r
3295 EFI_STATUS Status;\r
3296\r
7cd69959
SZ
3297 if (PcdGetBool (PcdEmuVariableNvModeEnable)) {\r
3298 Status = InitEmuNonVolatileVariableStore (&VariableStoreBase);\r
3299 if (EFI_ERROR (Status)) {\r
3300 return Status;\r
3301 }\r
3302 mVariableModuleGlobal->VariableGlobal.EmuNvMode = TRUE;\r
3303 DEBUG ((DEBUG_INFO, "Variable driver will work at emulated non-volatile variable mode!\n"));\r
3304 } else {\r
3305 Status = InitRealNonVolatileVariableStore (&VariableStoreBase);\r
3306 if (EFI_ERROR (Status)) {\r
3307 return Status;\r
3308 }\r
3309 mVariableModuleGlobal->VariableGlobal.EmuNvMode = FALSE;\r
701d603f
SZ
3310 }\r
3311\r
3312 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase;\r
3313 mNvVariableCache = (VARIABLE_STORE_HEADER *) (UINTN) VariableStoreBase;\r
3314 mVariableModuleGlobal->VariableGlobal.AuthFormat = (BOOLEAN)(CompareGuid (&mNvVariableCache->Signature, &gEfiAuthenticatedVariableGuid));\r
3315\r
fa0737a8
SZ
3316 mVariableModuleGlobal->MaxVariableSize = PcdGet32 (PcdMaxVariableSize);\r
3317 mVariableModuleGlobal->MaxAuthVariableSize = ((PcdGet32 (PcdMaxAuthVariableSize) != 0) ? PcdGet32 (PcdMaxAuthVariableSize) : mVariableModuleGlobal->MaxVariableSize);\r
3e02ebb2
SZ
3318\r
3319 //\r
3320 // Parse non-volatile variable data and get last variable offset.\r
3321 //\r
701d603f
SZ
3322 Variable = GetStartPointer (mNvVariableCache);\r
3323 while (IsValidVariableHeader (Variable, GetEndPointer (mNvVariableCache))) {\r
4edb1866
SZ
3324 NextVariable = GetNextVariablePtr (Variable);\r
3325 VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
3326 if ((Variable->Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
3327 mVariableModuleGlobal->HwErrVariableTotalSize += VariableSize;\r
3e02ebb2 3328 } else {\r
4edb1866 3329 mVariableModuleGlobal->CommonVariableTotalSize += VariableSize;\r
3e02ebb2
SZ
3330 }\r
3331\r
4edb1866 3332 Variable = NextVariable;\r
3e02ebb2 3333 }\r
701d603f 3334 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) Variable - (UINTN) mNvVariableCache;\r
3e02ebb2
SZ
3335\r
3336 return EFI_SUCCESS;\r
3337}\r
3338\r
335e2681
SZ
3339/**\r
3340 Flush the HOB variable to flash.\r
3341\r
3342 @param[in] VariableName Name of variable has been updated or deleted.\r
3343 @param[in] VendorGuid Guid of variable has been updated or deleted.\r
3344\r
3345**/\r
3346VOID\r
3347FlushHobVariableToFlash (\r
3348 IN CHAR16 *VariableName,\r
3349 IN EFI_GUID *VendorGuid\r
3350 )\r
3351{\r
3352 EFI_STATUS Status;\r
3353 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
3354 VARIABLE_HEADER *Variable;\r
3355 VOID *VariableData;\r
31349131 3356 VARIABLE_POINTER_TRACK VariablePtrTrack;\r
335e2681
SZ
3357 BOOLEAN ErrorFlag;\r
3358\r
3359 ErrorFlag = FALSE;\r
3360\r
3361 //\r
3362 // Flush the HOB variable to flash.\r
3363 //\r
3364 if (mVariableModuleGlobal->VariableGlobal.HobVariableBase != 0) {\r
3365 VariableStoreHeader = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;\r
3366 //\r
3367 // Set HobVariableBase to 0, it can avoid SetVariable to call back.\r
3368 //\r
3369 mVariableModuleGlobal->VariableGlobal.HobVariableBase = 0;\r
3370 for ( Variable = GetStartPointer (VariableStoreHeader)\r
6ebffb67 3371 ; IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))\r
335e2681
SZ
3372 ; Variable = GetNextVariablePtr (Variable)\r
3373 ) {\r
3374 if (Variable->State != VAR_ADDED) {\r
3375 //\r
3376 // The HOB variable has been set to DELETED state in local.\r
3377 //\r
3378 continue;\r
3379 }\r
3380 ASSERT ((Variable->Attributes & EFI_VARIABLE_NON_VOLATILE) != 0);\r
3381 if (VendorGuid == NULL || VariableName == NULL ||\r
fa0737a8 3382 !CompareGuid (VendorGuid, GetVendorGuidPtr (Variable)) ||\r
335e2681
SZ
3383 StrCmp (VariableName, GetVariableNamePtr (Variable)) != 0) {\r
3384 VariableData = GetVariableDataPtr (Variable);\r
31349131
SZ
3385 FindVariable (GetVariableNamePtr (Variable), GetVendorGuidPtr (Variable), &VariablePtrTrack, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
3386 Status = UpdateVariable (\r
335e2681 3387 GetVariableNamePtr (Variable),\r
fa0737a8 3388 GetVendorGuidPtr (Variable),\r
31349131 3389 VariableData,\r
fa0737a8 3390 DataSizeOfVariable (Variable),\r
31349131
SZ
3391 Variable->Attributes,\r
3392 0,\r
3393 0,\r
3394 &VariablePtrTrack,\r
3395 NULL\r
3396 );\r
fa0737a8 3397 DEBUG ((EFI_D_INFO, "Variable driver flush the HOB variable to flash: %g %s %r\n", GetVendorGuidPtr (Variable), GetVariableNamePtr (Variable), Status));\r
335e2681
SZ
3398 } else {\r
3399 //\r
31349131 3400 // The updated or deleted variable is matched with this HOB variable.\r
335e2681
SZ
3401 // Don't break here because we will try to set other HOB variables\r
3402 // since this variable could be set successfully.\r
3403 //\r
3404 Status = EFI_SUCCESS;\r
3405 }\r
3406 if (!EFI_ERROR (Status)) {\r
3407 //\r
3408 // If set variable successful, or the updated or deleted variable is matched with the HOB variable,\r
3409 // set the HOB variable to DELETED state in local.\r
3410 //\r
fa0737a8 3411 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
3412 Variable->State &= VAR_DELETED;\r
3413 } else {\r
3414 ErrorFlag = TRUE;\r
3415 }\r
3416 }\r
3417 if (ErrorFlag) {\r
3418 //\r
3419 // We still have HOB variable(s) not flushed in flash.\r
3420 //\r
3421 mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VariableStoreHeader;\r
3422 } else {\r
3423 //\r
3424 // All HOB variables have been flushed in flash.\r
3425 //\r
3426 DEBUG ((EFI_D_INFO, "Variable driver: all HOB variables have been flushed in flash.\n"));\r
3427 if (!AtRuntime ()) {\r
3428 FreePool ((VOID *) VariableStoreHeader);\r
3429 }\r
3430 }\r
3431 }\r
3432\r
3433}\r
8a2d4996 3434\r
7c80e839 3435/**\r
904e0ca9 3436 Initializes variable write service.\r
7c80e839 3437\r
8a2d4996 3438 @retval EFI_SUCCESS Function successfully executed.\r
3439 @retval Others Fail to initialize the variable service.\r
3440\r
3441**/\r
3442EFI_STATUS\r
3443VariableWriteServiceInitialize (\r
3444 VOID\r
3445 )\r
3446{\r
3447 EFI_STATUS Status;\r
8a2d4996 3448 UINTN Index;\r
3449 UINT8 Data;\r
fa0737a8 3450 VARIABLE_ENTRY_PROPERTY *VariableEntry;\r
8a2d4996 3451\r
31349131
SZ
3452 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
3453\r
8a2d4996 3454 //\r
3455 // Check if the free area is really free.\r
3456 //\r
9b18845a 3457 for (Index = mVariableModuleGlobal->NonVolatileLastVariableOffset; Index < mNvVariableCache->Size; Index++) {\r
8a2d4996 3458 Data = ((UINT8 *) mNvVariableCache)[Index];\r
3459 if (Data != 0xff) {\r
3460 //\r
3461 // There must be something wrong in variable store, do reclaim operation.\r
3462 //\r
3463 Status = Reclaim (\r
3464 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
3465 &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
3466 FALSE,\r
335e2681 3467 NULL,\r
7baf3c69
SZ
3468 NULL,\r
3469 0\r
8a2d4996 3470 );\r
3471 if (EFI_ERROR (Status)) {\r
31349131 3472 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
8a2d4996 3473 return Status;\r
3474 }\r
3475 break;\r
3476 }\r
3477 }\r
3478\r
335e2681 3479 FlushHobVariableToFlash (NULL, NULL);\r
0f7aff72 3480\r
fa0737a8 3481 Status = EFI_SUCCESS;\r
8021f4c7 3482 ZeroMem (&mAuthContextOut, sizeof (mAuthContextOut));\r
fa0737a8
SZ
3483 if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {\r
3484 //\r
3485 // Authenticated variable initialize.\r
3486 //\r
8021f4c7
SZ
3487 mAuthContextIn.StructSize = sizeof (AUTH_VAR_LIB_CONTEXT_IN);\r
3488 mAuthContextIn.MaxAuthVariableSize = mVariableModuleGlobal->MaxAuthVariableSize - GetVariableHeaderSize ();\r
3489 Status = AuthVariableLibInitialize (&mAuthContextIn, &mAuthContextOut);\r
fa0737a8
SZ
3490 if (!EFI_ERROR (Status)) {\r
3491 DEBUG ((EFI_D_INFO, "Variable driver will work with auth variable support!\n"));\r
3492 mVariableModuleGlobal->VariableGlobal.AuthSupport = TRUE;\r
8021f4c7
SZ
3493 if (mAuthContextOut.AuthVarEntry != NULL) {\r
3494 for (Index = 0; Index < mAuthContextOut.AuthVarEntryCount; Index++) {\r
3495 VariableEntry = &mAuthContextOut.AuthVarEntry[Index];\r
3496 Status = VarCheckLibVariablePropertySet (\r
fa0737a8
SZ
3497 VariableEntry->Name,\r
3498 VariableEntry->Guid,\r
3499 &VariableEntry->VariableProperty\r
3500 );\r
3501 ASSERT_EFI_ERROR (Status);\r
3502 }\r
3503 }\r
3504 } else if (Status == EFI_UNSUPPORTED) {\r
3505 DEBUG ((EFI_D_INFO, "NOTICE - AuthVariableLibInitialize() returns %r!\n", Status));\r
3506 DEBUG ((EFI_D_INFO, "Variable driver will continue to work without auth variable support!\n"));\r
3507 mVariableModuleGlobal->VariableGlobal.AuthSupport = FALSE;\r
3508 Status = EFI_SUCCESS;\r
3509 }\r
3510 }\r
3511\r
3512 if (!EFI_ERROR (Status)) {\r
33f615e2 3513 for (Index = 0; Index < ARRAY_SIZE (mVariableEntryProperty); Index++) {\r
fa0737a8 3514 VariableEntry = &mVariableEntryProperty[Index];\r
8021f4c7 3515 Status = VarCheckLibVariablePropertySet (VariableEntry->Name, VariableEntry->Guid, &VariableEntry->VariableProperty);\r
fa0737a8
SZ
3516 ASSERT_EFI_ERROR (Status);\r
3517 }\r
3518 }\r
3519\r
31349131 3520 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2f6aa774
JY
3521\r
3522 //\r
3523 // Initialize MOR Lock variable.\r
3524 //\r
3525 MorLockInit ();\r
3526\r
fa0737a8 3527 return Status;\r
8a2d4996 3528}\r
3529\r
a37044ef
LG
3530/**\r
3531 Convert normal variable storage to the allocated auth variable storage.\r
3532\r
3533 @param[in] NormalVarStorage Pointer to the normal variable storage header\r
3534\r
3535 @retval the allocated auth variable storage\r
3536**/\r
3537VOID *\r
3538ConvertNormalVarStorageToAuthVarStorage (\r
3539 VARIABLE_STORE_HEADER *NormalVarStorage\r
3540 )\r
3541{\r
3542 VARIABLE_HEADER *StartPtr;\r
3543 UINT8 *NextPtr;\r
3544 VARIABLE_HEADER *EndPtr;\r
3545 UINTN AuthVarStroageSize;\r
3546 AUTHENTICATED_VARIABLE_HEADER *AuthStartPtr;\r
3547 VARIABLE_STORE_HEADER *AuthVarStorage;\r
3548\r
3549 AuthVarStroageSize = sizeof (VARIABLE_STORE_HEADER);\r
3550 //\r
3551 // Set AuthFormat as FALSE for normal variable storage\r
3552 //\r
3553 mVariableModuleGlobal->VariableGlobal.AuthFormat = FALSE;\r
3554\r
3555 //\r
3556 // Calculate Auth Variable Storage Size\r
3557 //\r
3558 StartPtr = GetStartPointer (NormalVarStorage);\r
3559 EndPtr = GetEndPointer (NormalVarStorage);\r
3560 while (StartPtr < EndPtr) {\r
3561 if (StartPtr->State == VAR_ADDED) {\r
3562 AuthVarStroageSize = HEADER_ALIGN (AuthVarStroageSize);\r
3563 AuthVarStroageSize += sizeof (AUTHENTICATED_VARIABLE_HEADER);\r
3564 AuthVarStroageSize += StartPtr->NameSize + GET_PAD_SIZE (StartPtr->NameSize);\r
3565 AuthVarStroageSize += StartPtr->DataSize + GET_PAD_SIZE (StartPtr->DataSize);\r
3566 }\r
3567 StartPtr = GetNextVariablePtr (StartPtr);\r
3568 }\r
3569\r
3570 //\r
3571 // Allocate Runtime memory for Auth Variable Storage\r
3572 //\r
3573 AuthVarStorage = AllocateRuntimeZeroPool (AuthVarStroageSize);\r
3574 ASSERT (AuthVarStorage != NULL);\r
3575 if (AuthVarStorage == NULL) {\r
3576 return NULL;\r
3577 }\r
3578\r
3579 //\r
3580 // Copy Variable from Normal storage to Auth storage\r
3581 //\r
3582 StartPtr = GetStartPointer (NormalVarStorage);\r
3583 EndPtr = GetEndPointer (NormalVarStorage);\r
3584 AuthStartPtr = (AUTHENTICATED_VARIABLE_HEADER *) GetStartPointer (AuthVarStorage);\r
3585 while (StartPtr < EndPtr) {\r
3586 if (StartPtr->State == VAR_ADDED) {\r
3587 AuthStartPtr = (AUTHENTICATED_VARIABLE_HEADER *) HEADER_ALIGN (AuthStartPtr);\r
3588 //\r
3589 // Copy Variable Header\r
3590 //\r
3591 AuthStartPtr->StartId = StartPtr->StartId;\r
3592 AuthStartPtr->State = StartPtr->State;\r
3593 AuthStartPtr->Attributes = StartPtr->Attributes;\r
3594 AuthStartPtr->NameSize = StartPtr->NameSize;\r
3595 AuthStartPtr->DataSize = StartPtr->DataSize;\r
3596 CopyGuid (&AuthStartPtr->VendorGuid, &StartPtr->VendorGuid);\r
3597 //\r
3598 // Copy Variable Name\r
3599 //\r
3600 NextPtr = (UINT8 *) (AuthStartPtr + 1);\r
3601 CopyMem (NextPtr, GetVariableNamePtr (StartPtr), AuthStartPtr->NameSize);\r
3602 //\r
3603 // Copy Variable Data\r
3604 //\r
3605 NextPtr = NextPtr + AuthStartPtr->NameSize + GET_PAD_SIZE (AuthStartPtr->NameSize);\r
3606 CopyMem (NextPtr, GetVariableDataPtr (StartPtr), AuthStartPtr->DataSize);\r
3607 //\r
3608 // Go to next variable\r
3609 //\r
3610 AuthStartPtr = (AUTHENTICATED_VARIABLE_HEADER *) (NextPtr + AuthStartPtr->DataSize + GET_PAD_SIZE (AuthStartPtr->DataSize));\r
3611 }\r
3612 StartPtr = GetNextVariablePtr (StartPtr);\r
3613 }\r
3614 //\r
3615 // Update Auth Storage Header\r
3616 //\r
3617 AuthVarStorage->Format = NormalVarStorage->Format;\r
3618 AuthVarStorage->State = NormalVarStorage->State;\r
1b79547a 3619 AuthVarStorage->Size = (UINT32)((UINTN)AuthStartPtr - (UINTN)AuthVarStorage);\r
a37044ef
LG
3620 CopyGuid (&AuthVarStorage->Signature, &gEfiAuthenticatedVariableGuid);\r
3621 ASSERT (AuthVarStorage->Size <= AuthVarStroageSize);\r
8a2d4996 3622\r
a37044ef
LG
3623 //\r
3624 // Restore AuthFormat\r
3625 //\r
3626 mVariableModuleGlobal->VariableGlobal.AuthFormat = TRUE;\r
3627 return AuthVarStorage;\r
3628}\r
09808bd3
SZ
3629\r
3630/**\r
3631 Get HOB variable store.\r
3632\r
5f463523 3633 @param[in] VariableGuid NV variable store signature.\r
09808bd3
SZ
3634\r
3635 @retval EFI_SUCCESS Function successfully executed.\r
3636 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.\r
3637\r
3638**/\r
3639EFI_STATUS\r
3640GetHobVariableStore (\r
3641 IN EFI_GUID *VariableGuid\r
3642 )\r
3643{\r
3644 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
3645 UINT64 VariableStoreLength;\r
3646 EFI_HOB_GUID_TYPE *GuidHob;\r
3647 BOOLEAN NeedConvertNormalToAuth;\r
3648\r
fdd3e77a
SZ
3649 //\r
3650 // Make sure there is no more than one Variable HOB.\r
3651 //\r
3652 DEBUG_CODE (\r
3653 GuidHob = GetFirstGuidHob (&gEfiAuthenticatedVariableGuid);\r
3654 if (GuidHob != NULL) {\r
3655 if ((GetNextGuidHob (&gEfiAuthenticatedVariableGuid, GET_NEXT_HOB (GuidHob)) != NULL)) {\r
3656 DEBUG ((DEBUG_ERROR, "ERROR: Found two Auth Variable HOBs\n"));\r
3657 ASSERT (FALSE);\r
3658 } else if (GetFirstGuidHob (&gEfiVariableGuid) != NULL) {\r
3659 DEBUG ((DEBUG_ERROR, "ERROR: Found one Auth + one Normal Variable HOBs\n"));\r
3660 ASSERT (FALSE);\r
3661 }\r
3662 } else {\r
3663 GuidHob = GetFirstGuidHob (&gEfiVariableGuid);\r
3664 if (GuidHob != NULL) {\r
3665 if ((GetNextGuidHob (&gEfiVariableGuid, GET_NEXT_HOB (GuidHob)) != NULL)) {\r
3666 DEBUG ((DEBUG_ERROR, "ERROR: Found two Normal Variable HOBs\n"));\r
3667 ASSERT (FALSE);\r
3668 }\r
3669 }\r
3670 }\r
3671 );\r
3672\r
09808bd3
SZ
3673 //\r
3674 // Combinations supported:\r
3675 // 1. Normal NV variable store +\r
3676 // Normal HOB variable store\r
3677 // 2. Auth NV variable store +\r
3678 // Auth HOB variable store\r
3679 // 3. Auth NV variable store +\r
3680 // Normal HOB variable store (code will convert it to Auth Format)\r
3681 //\r
3682 NeedConvertNormalToAuth = FALSE;\r
3683 GuidHob = GetFirstGuidHob (VariableGuid);\r
3684 if (GuidHob == NULL && VariableGuid == &gEfiAuthenticatedVariableGuid) {\r
3685 //\r
3686 // Try getting it from normal variable HOB\r
3687 //\r
3688 GuidHob = GetFirstGuidHob (&gEfiVariableGuid);\r
3689 NeedConvertNormalToAuth = TRUE;\r
3690 }\r
3691 if (GuidHob != NULL) {\r
3692 VariableStoreHeader = GET_GUID_HOB_DATA (GuidHob);\r
3693 VariableStoreLength = GuidHob->Header.HobLength - sizeof (EFI_HOB_GUID_TYPE);\r
3694 if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) {\r
3695 if (!NeedConvertNormalToAuth) {\r
3696 mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) AllocateRuntimeCopyPool ((UINTN) VariableStoreLength, (VOID *) VariableStoreHeader);\r
3697 } else {\r
3698 mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) ConvertNormalVarStorageToAuthVarStorage ((VOID *) VariableStoreHeader);\r
3699 }\r
3700 if (mVariableModuleGlobal->VariableGlobal.HobVariableBase == 0) {\r
3701 return EFI_OUT_OF_RESOURCES;\r
3702 }\r
3703 } else {\r
3704 DEBUG ((EFI_D_ERROR, "HOB Variable Store header is corrupted!\n"));\r
3705 }\r
3706 }\r
3707\r
3708 return EFI_SUCCESS;\r
3709}\r
3710\r
8a2d4996 3711/**\r
3712 Initializes variable store area for non-volatile and volatile variable.\r
7c80e839 3713\r
3714 @retval EFI_SUCCESS Function successfully executed.\r
3715 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.\r
3716\r
3717**/\r
8d3a5c82 3718EFI_STATUS\r
8d3a5c82 3719VariableCommonInitialize (\r
8a2d4996 3720 VOID\r
8d3a5c82 3721 )\r
8d3a5c82 3722{\r
3723 EFI_STATUS Status;\r
8d3a5c82 3724 VARIABLE_STORE_HEADER *VolatileVariableStore;\r
2fcdca1d 3725 UINTN ScratchSize;\r
fa0737a8 3726 EFI_GUID *VariableGuid;\r
8d3a5c82 3727\r
7800593d
LG
3728 //\r
3729 // Allocate runtime memory for variable driver global structure.\r
3730 //\r
72399dae 3731 mVariableModuleGlobal = AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL));\r
7800593d
LG
3732 if (mVariableModuleGlobal == NULL) {\r
3733 return EFI_OUT_OF_RESOURCES;\r
3734 }\r
8d3a5c82 3735\r
8a2d4996 3736 InitializeLock (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock, TPL_NOTIFY);\r
8d3a5c82 3737\r
fa0737a8
SZ
3738 //\r
3739 // Init non-volatile variable store.\r
3740 //\r
701d603f 3741 Status = InitNonVolatileVariableStore ();\r
fa0737a8
SZ
3742 if (EFI_ERROR (Status)) {\r
3743 FreePool (mVariableModuleGlobal);\r
3744 return Status;\r
3745 }\r
3746\r
3747 //\r
3748 // mVariableModuleGlobal->VariableGlobal.AuthFormat\r
3749 // has been initialized in InitNonVolatileVariableStore().\r
3750 //\r
3751 if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {\r
3752 DEBUG ((EFI_D_INFO, "Variable driver will work with auth variable format!\n"));\r
3753 //\r
3754 // Set AuthSupport to FALSE first, VariableWriteServiceInitialize() will initialize it.\r
3755 //\r
3756 mVariableModuleGlobal->VariableGlobal.AuthSupport = FALSE;\r
3757 VariableGuid = &gEfiAuthenticatedVariableGuid;\r
3758 } else {\r
3759 DEBUG ((EFI_D_INFO, "Variable driver will work without auth variable support!\n"));\r
3760 mVariableModuleGlobal->VariableGlobal.AuthSupport = FALSE;\r
3761 VariableGuid = &gEfiVariableGuid;\r
3762 }\r
3763\r
0f7aff72
RN
3764 //\r
3765 // Get HOB variable store.\r
3766 //\r
09808bd3
SZ
3767 Status = GetHobVariableStore (VariableGuid);\r
3768 if (EFI_ERROR (Status)) {\r
701d603f
SZ
3769 if (mNvFvHeaderCache != NULL) {\r
3770 FreePool (mNvFvHeaderCache);\r
3771 }\r
09808bd3
SZ
3772 FreePool (mVariableModuleGlobal);\r
3773 return Status;\r
0f7aff72
RN
3774 }\r
3775\r
9b4a2032
LE
3776 mVariableModuleGlobal->MaxVolatileVariableSize = ((PcdGet32 (PcdMaxVolatileVariableSize) != 0) ?\r
3777 PcdGet32 (PcdMaxVolatileVariableSize) :\r
3778 mVariableModuleGlobal->MaxVariableSize\r
3779 );\r
8d3a5c82 3780 //\r
2fcdca1d 3781 // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.\r
8d3a5c82 3782 //\r
9b4a2032 3783 ScratchSize = GetMaxVariableSize ();\r
fa0737a8 3784 mVariableModuleGlobal->ScratchBufferSize = ScratchSize;\r
188e4e84 3785 VolatileVariableStore = AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize) + ScratchSize);\r
8d3a5c82 3786 if (VolatileVariableStore == NULL) {\r
3e02ebb2
SZ
3787 if (mVariableModuleGlobal->VariableGlobal.HobVariableBase != 0) {\r
3788 FreePool ((VOID *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase);\r
3789 }\r
701d603f
SZ
3790 if (mNvFvHeaderCache != NULL) {\r
3791 FreePool (mNvFvHeaderCache);\r
3792 }\r
8d3a5c82 3793 FreePool (mVariableModuleGlobal);\r
3794 return EFI_OUT_OF_RESOURCES;\r
3795 }\r
3796\r
188e4e84 3797 SetMem (VolatileVariableStore, PcdGet32 (PcdVariableStoreSize) + ScratchSize, 0xff);\r
8d3a5c82 3798\r
3799 //\r
8a2d4996 3800 // Initialize Variable Specific Data.\r
8d3a5c82 3801 //\r
052ad7e1 3802 mVariableModuleGlobal->VariableGlobal.VolatileVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VolatileVariableStore;\r
9cad030b 3803 mVariableModuleGlobal->VolatileLastVariableOffset = (UINTN) GetStartPointer (VolatileVariableStore) - (UINTN) VolatileVariableStore;\r
8d3a5c82 3804\r
fa0737a8 3805 CopyGuid (&VolatileVariableStore->Signature, VariableGuid);\r
8a2d4996 3806 VolatileVariableStore->Size = PcdGet32 (PcdVariableStoreSize);\r
3807 VolatileVariableStore->Format = VARIABLE_STORE_FORMATTED;\r
3808 VolatileVariableStore->State = VARIABLE_STORE_HEALTHY;\r
3809 VolatileVariableStore->Reserved = 0;\r
3810 VolatileVariableStore->Reserved1 = 0;\r
8d3a5c82 3811\r
fa0737a8 3812 return EFI_SUCCESS;\r
8d3a5c82 3813}\r
052ad7e1 3814\r
052ad7e1 3815\r
aa75dfec 3816/**\r
8a2d4996 3817 Get the proper fvb handle and/or fvb protocol by the given Flash address.\r
aa75dfec 3818\r
fa0737a8 3819 @param[in] Address The Flash address.\r
8a2d4996 3820 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.\r
3821 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.\r
aa75dfec 3822\r
aa75dfec 3823**/\r
8a2d4996 3824EFI_STATUS\r
3825GetFvbInfoByAddress (\r
3826 IN EFI_PHYSICAL_ADDRESS Address,\r
3827 OUT EFI_HANDLE *FvbHandle OPTIONAL,\r
3828 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvbProtocol OPTIONAL\r
8a9e0b72 3829 )\r
3830{\r
8a2d4996 3831 EFI_STATUS Status;\r
3832 EFI_HANDLE *HandleBuffer;\r
3833 UINTN HandleCount;\r
3834 UINTN Index;\r
3835 EFI_PHYSICAL_ADDRESS FvbBaseAddress;\r
3836 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r
8a2d4996 3837 EFI_FVB_ATTRIBUTES_2 Attributes;\r
1fcbeaea
DG
3838 UINTN BlockSize;\r
3839 UINTN NumberOfBlocks;\r
3840\r
4e1005ec 3841 HandleBuffer = NULL;\r
8a9e0b72 3842 //\r
8a2d4996 3843 // Get all FVB handles.\r
8a9e0b72 3844 //\r
8a2d4996 3845 Status = GetFvbCountAndBuffer (&HandleCount, &HandleBuffer);\r
8a9e0b72 3846 if (EFI_ERROR (Status)) {\r
8a2d4996 3847 return EFI_NOT_FOUND;\r
8a9e0b72 3848 }\r
8a2d4996 3849\r
8a9e0b72 3850 //\r
8a2d4996 3851 // Get the FVB to access variable store.\r
8a9e0b72 3852 //\r
8a2d4996 3853 Fvb = NULL;\r
f0480ecf 3854 for (Index = 0; Index < HandleCount; Index += 1, Status = EFI_NOT_FOUND, Fvb = NULL) {\r
8a2d4996 3855 Status = GetFvbByHandle (HandleBuffer[Index], &Fvb);\r
8a9e0b72 3856 if (EFI_ERROR (Status)) {\r
3857 Status = EFI_NOT_FOUND;\r
3858 break;\r
3859 }\r
3860\r
3861 //\r
3862 // Ensure this FVB protocol supported Write operation.\r
3863 //\r
3864 Status = Fvb->GetAttributes (Fvb, &Attributes);\r
3865 if (EFI_ERROR (Status) || ((Attributes & EFI_FVB2_WRITE_STATUS) == 0)) {\r
1fcbeaea 3866 continue;\r
8a9e0b72 3867 }\r
1fcbeaea 3868\r
8a9e0b72 3869 //\r
8a2d4996 3870 // Compare the address and select the right one.\r
8a9e0b72 3871 //\r
3872 Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);\r
3873 if (EFI_ERROR (Status)) {\r
3874 continue;\r
3875 }\r
3876\r
1fcbeaea
DG
3877 //\r
3878 // Assume one FVB has one type of BlockSize.\r
3879 //\r
3880 Status = Fvb->GetBlockSize (Fvb, 0, &BlockSize, &NumberOfBlocks);\r
3881 if (EFI_ERROR (Status)) {\r
3882 continue;\r
3883 }\r
3884\r
3885 if ((Address >= FvbBaseAddress) && (Address < (FvbBaseAddress + BlockSize * NumberOfBlocks))) {\r
8a2d4996 3886 if (FvbHandle != NULL) {\r
3887 *FvbHandle = HandleBuffer[Index];\r
3888 }\r
3889 if (FvbProtocol != NULL) {\r
3890 *FvbProtocol = Fvb;\r
3891 }\r
3892 Status = EFI_SUCCESS;\r
8a9e0b72 3893 break;\r
3894 }\r
3895 }\r
8a9e0b72 3896 FreePool (HandleBuffer);\r
533020ef 3897\r
8a2d4996 3898 if (Fvb == NULL) {\r
3899 Status = EFI_NOT_FOUND;\r
8a9e0b72 3900 }\r
fa0737a8
SZ
3901\r
3902 return Status;\r
052ad7e1
A
3903}\r
3904\r