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