MdeModulePkg Variable: Abstract GetHobVariableStore function
[mirror_edk2.git] / MdeModulePkg / Universal / Variable / RuntimeDxe / Variable.c
CommitLineData
052ad7e1 1/** @file\r
fa0737a8 2 The common variable operation routines shared by DXE_RUNTIME variable\r
8a2d4996 3 module and DXE_SMM variable module.\r
fa0737a8 4\r
18a7dbbc
SZ
5 Caution: This module requires additional review when modified.\r
6 This driver will have external input - variable data. They may be input in SMM mode.\r
7 This external input must be validated carefully to avoid security issue like\r
8 buffer overflow, integer overflow.\r
9\r
10 VariableServiceGetNextVariableName () and VariableServiceQueryVariableInfo() are external API.\r
11 They need check input parameter.\r
12\r
13 VariableServiceGetVariable() and VariableServiceSetVariable() are external API\r
14 to receive datasize and data buffer. The size should be checked carefully.\r
15\r
fa0737a8
SZ
16 VariableServiceSetVariable() should also check authenticate data to avoid buffer overflow,\r
17 integer overflow. It should also check attribute to avoid authentication bypass.\r
18\r
051bf6e0 19Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
d741d141 20(C) Copyright 2015-2018 Hewlett Packard Enterprise Development LP<BR>\r
fa0737a8
SZ
21This program and the accompanying materials\r
22are licensed and made available under the terms and conditions of the BSD License\r
23which accompanies this distribution. The full text of the license may be found at\r
24http://opensource.org/licenses/bsd-license.php\r
504214c4 25\r
fa0737a8
SZ
26THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
27WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
8d3a5c82 28\r
052ad7e1 29**/\r
8d3a5c82 30\r
8d3a5c82 31#include "Variable.h"\r
33a5a666 32\r
7800593d 33VARIABLE_MODULE_GLOBAL *mVariableModuleGlobal;\r
8d3a5c82 34\r
7c80e839 35///\r
8a2d4996 36/// Define a memory cache that improves the search performance for a variable.\r
7c80e839 37///\r
ff843847 38VARIABLE_STORE_HEADER *mNvVariableCache = NULL;\r
aa79b0b3 39\r
9b18845a
DL
40///\r
41/// Memory cache of Fv Header.\r
42///\r
43EFI_FIRMWARE_VOLUME_HEADER *mNvFvHeaderCache = NULL;\r
44\r
8a2d4996 45///\r
46/// The memory entry used for variable statistics data.\r
47///\r
ff843847
RN
48VARIABLE_INFO_ENTRY *gVariableInfo = NULL;\r
49\r
ff843847
RN
50///\r
51/// The flag to indicate whether the platform has left the DXE phase of execution.\r
52///\r
53BOOLEAN mEndOfDxe = FALSE;\r
54\r
55///\r
8021f4c7
SZ
56/// It indicates the var check request source.\r
57/// In the implementation, DXE is regarded as untrusted, and SMM is trusted.\r
ff843847 58///\r
8021f4c7 59VAR_CHECK_REQUEST_SOURCE mRequestSource = VarCheckFromUntrusted;\r
8d3a5c82 60\r
00ab76e0
SZ
61//\r
62// It will record the current boot error flag before EndOfDxe.\r
63//\r
64VAR_ERROR_FLAG mCurrentBootVarErrFlag = VAR_ERROR_FLAG_NO_ERROR;\r
33a5a666 65\r
fa0737a8
SZ
66VARIABLE_ENTRY_PROPERTY mVariableEntryProperty[] = {\r
67 {\r
68 &gEdkiiVarErrorFlagGuid,\r
69 VAR_ERROR_FLAG_NAME,\r
70 {\r
71 VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
72 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,\r
73 VARIABLE_ATTRIBUTE_NV_BS_RT,\r
74 sizeof (VAR_ERROR_FLAG),\r
75 sizeof (VAR_ERROR_FLAG)\r
76 }\r
77 },\r
78};\r
79\r
8021f4c7 80AUTH_VAR_LIB_CONTEXT_IN mAuthContextIn = {\r
fa0737a8 81 AUTH_VAR_LIB_CONTEXT_IN_STRUCT_VERSION,\r
dbd030bb
SZ
82 //\r
83 // StructSize, TO BE FILLED\r
84 //\r
85 0,\r
fa0737a8
SZ
86 //\r
87 // MaxAuthVariableSize, TO BE FILLED\r
88 //\r
89 0,\r
90 VariableExLibFindVariable,\r
91 VariableExLibFindNextVariable,\r
92 VariableExLibUpdateVariable,\r
93 VariableExLibGetScratchBuffer,\r
94 VariableExLibCheckRemainingSpaceForConsistency,\r
95 VariableExLibAtRuntime,\r
96};\r
97\r
8021f4c7 98AUTH_VAR_LIB_CONTEXT_OUT mAuthContextOut;\r
fa0737a8 99\r
fa0737a8
SZ
100/**\r
101 Routine used to track statistical information about variable usage.\r
052ad7e1 102 The data is stored in the EFI system table so it can be accessed later.\r
fa0737a8 103 VariableInfo.efi can dump out the table. Only Boot Services variable\r
052ad7e1 104 accesses are tracked by this code. The PcdVariableCollectStatistics\r
fa0737a8 105 build flag controls if this feature is enabled.\r
052ad7e1 106\r
fa0737a8 107 A read that hits in the cache will have Read and Cache true for\r
052ad7e1
A
108 the transaction. Data is allocated by this routine, but never\r
109 freed.\r
110\r
8a2d4996 111 @param[in] VariableName Name of the Variable to track.\r
112 @param[in] VendorGuid Guid of the Variable to track.\r
113 @param[in] Volatile TRUE if volatile FALSE if non-volatile.\r
114 @param[in] Read TRUE if GetVariable() was called.\r
115 @param[in] Write TRUE if SetVariable() was called.\r
116 @param[in] Delete TRUE if deleted via SetVariable().\r
052ad7e1
A
117 @param[in] Cache TRUE for a cache hit.\r
118\r
119**/\r
33a5a666
A
120VOID\r
121UpdateVariableInfo (\r
122 IN CHAR16 *VariableName,\r
123 IN EFI_GUID *VendorGuid,\r
124 IN BOOLEAN Volatile,\r
125 IN BOOLEAN Read,\r
126 IN BOOLEAN Write,\r
127 IN BOOLEAN Delete,\r
128 IN BOOLEAN Cache\r
129 )\r
130{\r
131 VARIABLE_INFO_ENTRY *Entry;\r
132\r
133 if (FeaturePcdGet (PcdVariableCollectStatistics)) {\r
134\r
8a2d4996 135 if (AtRuntime ()) {\r
136 // Don't collect statistics at runtime.\r
33a5a666
A
137 return;\r
138 }\r
139\r
140 if (gVariableInfo == NULL) {\r
052ad7e1 141 //\r
8a2d4996 142 // On the first call allocate a entry and place a pointer to it in\r
143 // the EFI System Table.\r
052ad7e1 144 //\r
33a5a666 145 gVariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));\r
052ad7e1
A
146 ASSERT (gVariableInfo != NULL);\r
147\r
33a5a666 148 CopyGuid (&gVariableInfo->VendorGuid, VendorGuid);\r
6e1e5405 149 gVariableInfo->Name = AllocateZeroPool (StrSize (VariableName));\r
e6c4ef13 150 ASSERT (gVariableInfo->Name != NULL);\r
568a5119 151 StrCpyS (gVariableInfo->Name, StrSize(VariableName)/sizeof(CHAR16), VariableName);\r
33a5a666 152 gVariableInfo->Volatile = Volatile;\r
33a5a666
A
153 }\r
154\r
fa0737a8 155\r
33a5a666
A
156 for (Entry = gVariableInfo; Entry != NULL; Entry = Entry->Next) {\r
157 if (CompareGuid (VendorGuid, &Entry->VendorGuid)) {\r
158 if (StrCmp (VariableName, Entry->Name) == 0) {\r
159 if (Read) {\r
160 Entry->ReadCount++;\r
161 }\r
162 if (Write) {\r
163 Entry->WriteCount++;\r
164 }\r
165 if (Delete) {\r
166 Entry->DeleteCount++;\r
167 }\r
168 if (Cache) {\r
169 Entry->CacheCount++;\r
170 }\r
171\r
172 return;\r
173 }\r
174 }\r
175\r
176 if (Entry->Next == NULL) {\r
052ad7e1
A
177 //\r
178 // If the entry is not in the table add it.\r
8a2d4996 179 // Next iteration of the loop will fill in the data.\r
052ad7e1 180 //\r
33a5a666 181 Entry->Next = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));\r
052ad7e1 182 ASSERT (Entry->Next != NULL);\r
33a5a666
A
183\r
184 CopyGuid (&Entry->Next->VendorGuid, VendorGuid);\r
6e1e5405 185 Entry->Next->Name = AllocateZeroPool (StrSize (VariableName));\r
e6c4ef13 186 ASSERT (Entry->Next->Name != NULL);\r
568a5119 187 StrCpyS (Entry->Next->Name, StrSize(VariableName)/sizeof(CHAR16), VariableName);\r
33a5a666
A
188 Entry->Next->Volatile = Volatile;\r
189 }\r
190\r
191 }\r
192 }\r
193}\r
194\r
195\r
7c80e839 196/**\r
8d3a5c82 197\r
198 This code checks if variable header is valid or not.\r
199\r
6ebffb67
SZ
200 @param Variable Pointer to the Variable Header.\r
201 @param VariableStoreEnd Pointer to the Variable Store End.\r
8d3a5c82 202\r
6ebffb67
SZ
203 @retval TRUE Variable header is valid.\r
204 @retval FALSE Variable header is not valid.\r
8d3a5c82 205\r
7c80e839 206**/\r
207BOOLEAN\r
208IsValidVariableHeader (\r
6ebffb67
SZ
209 IN VARIABLE_HEADER *Variable,\r
210 IN VARIABLE_HEADER *VariableStoreEnd\r
7c80e839 211 )\r
8d3a5c82 212{\r
6ebffb67
SZ
213 if ((Variable == NULL) || (Variable >= VariableStoreEnd) || (Variable->StartId != VARIABLE_DATA)) {\r
214 //\r
215 // Variable is NULL or has reached the end of variable store,\r
216 // or the StartId is not correct.\r
217 //\r
8d3a5c82 218 return FALSE;\r
219 }\r
220\r
221 return TRUE;\r
222}\r
223\r
052ad7e1 224\r
7c80e839 225/**\r
226\r
227 This function writes data to the FWH at the correct LBA even if the LBAs\r
228 are fragmented.\r
229\r
8a2d4996 230 @param Global Pointer to VARAIBLE_GLOBAL structure.\r
231 @param Volatile Point out the Variable is Volatile or Non-Volatile.\r
232 @param SetByIndex TRUE if target pointer is given as index.\r
233 FALSE if target pointer is absolute.\r
234 @param Fvb Pointer to the writable FVB protocol.\r
7c80e839 235 @param DataPtrIndex Pointer to the Data from the end of VARIABLE_STORE_HEADER\r
8a2d4996 236 structure.\r
237 @param DataSize Size of data to be written.\r
238 @param Buffer Pointer to the buffer from which data is written.\r
7c80e839 239\r
8a2d4996 240 @retval EFI_INVALID_PARAMETER Parameters not valid.\r
989f7a2c 241 @retval EFI_UNSUPPORTED Fvb is a NULL for Non-Volatile variable update.\r
242 @retval EFI_OUT_OF_RESOURCES The remaining size is not enough.\r
8a2d4996 243 @retval EFI_SUCCESS Variable store successfully updated.\r
7c80e839 244\r
245**/\r
8d3a5c82 246EFI_STATUS\r
8d3a5c82 247UpdateVariableStore (\r
8a9e0b72 248 IN VARIABLE_GLOBAL *Global,\r
249 IN BOOLEAN Volatile,\r
250 IN BOOLEAN SetByIndex,\r
251 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb,\r
252 IN UINTN DataPtrIndex,\r
253 IN UINT32 DataSize,\r
254 IN UINT8 *Buffer\r
8d3a5c82 255 )\r
8d3a5c82 256{\r
257 EFI_FV_BLOCK_MAP_ENTRY *PtrBlockMapEntry;\r
258 UINTN BlockIndex2;\r
259 UINTN LinearOffset;\r
260 UINTN CurrWriteSize;\r
261 UINTN CurrWritePtr;\r
262 UINT8 *CurrBuffer;\r
263 EFI_LBA LbaNumber;\r
264 UINTN Size;\r
265 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r
266 VARIABLE_STORE_HEADER *VolatileBase;\r
267 EFI_PHYSICAL_ADDRESS FvVolHdr;\r
268 EFI_PHYSICAL_ADDRESS DataPtr;\r
269 EFI_STATUS Status;\r
270\r
271 FwVolHeader = NULL;\r
272 DataPtr = DataPtrIndex;\r
273\r
274 //\r
8a2d4996 275 // Check if the Data is Volatile.\r
8d3a5c82 276 //\r
277 if (!Volatile) {\r
fa0737a8 278 if (Fvb == NULL) {\r
989f7a2c 279 return EFI_UNSUPPORTED;\r
fa0737a8 280 }\r
8a9e0b72 281 Status = Fvb->GetPhysicalAddress(Fvb, &FvVolHdr);\r
282 ASSERT_EFI_ERROR (Status);\r
283\r
8d3a5c82 284 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvVolHdr);\r
285 //\r
286 // Data Pointer should point to the actual Address where data is to be\r
8a2d4996 287 // written.\r
8d3a5c82 288 //\r
289 if (SetByIndex) {\r
052ad7e1 290 DataPtr += mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;\r
8d3a5c82 291 }\r
292\r
d741d141 293 if ((DataPtr + DataSize) > ((EFI_PHYSICAL_ADDRESS) (UINTN) ((UINT8 *) FwVolHeader + FwVolHeader->FvLength))) {\r
989f7a2c 294 return EFI_OUT_OF_RESOURCES;\r
8d3a5c82 295 }\r
296 } else {\r
297 //\r
298 // Data Pointer should point to the actual Address where data is to be\r
8a2d4996 299 // written.\r
8d3a5c82 300 //\r
052ad7e1 301 VolatileBase = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r
8d3a5c82 302 if (SetByIndex) {\r
052ad7e1 303 DataPtr += mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;\r
8d3a5c82 304 }\r
305\r
d741d141 306 if ((DataPtr + DataSize) > ((UINTN) ((UINT8 *) VolatileBase + VolatileBase->Size))) {\r
989f7a2c 307 return EFI_OUT_OF_RESOURCES;\r
8d3a5c82 308 }\r
fa0737a8 309\r
c6492839 310 //\r
311 // If Volatile Variable just do a simple mem copy.\r
fa0737a8 312 //\r
c6492839 313 CopyMem ((UINT8 *)(UINTN)DataPtr, Buffer, DataSize);\r
8d3a5c82 314 return EFI_SUCCESS;\r
315 }\r
fa0737a8 316\r
8d3a5c82 317 //\r
8a2d4996 318 // If we are here we are dealing with Non-Volatile Variables.\r
8d3a5c82 319 //\r
320 LinearOffset = (UINTN) FwVolHeader;\r
321 CurrWritePtr = (UINTN) DataPtr;\r
322 CurrWriteSize = DataSize;\r
323 CurrBuffer = Buffer;\r
324 LbaNumber = 0;\r
325\r
326 if (CurrWritePtr < LinearOffset) {\r
327 return EFI_INVALID_PARAMETER;\r
328 }\r
329\r
9b18845a 330 for (PtrBlockMapEntry = mNvFvHeaderCache->BlockMap; PtrBlockMapEntry->NumBlocks != 0; PtrBlockMapEntry++) {\r
8d3a5c82 331 for (BlockIndex2 = 0; BlockIndex2 < PtrBlockMapEntry->NumBlocks; BlockIndex2++) {\r
332 //\r
333 // Check to see if the Variable Writes are spanning through multiple\r
334 // blocks.\r
335 //\r
336 if ((CurrWritePtr >= LinearOffset) && (CurrWritePtr < LinearOffset + PtrBlockMapEntry->Length)) {\r
337 if ((CurrWritePtr + CurrWriteSize) <= (LinearOffset + PtrBlockMapEntry->Length)) {\r
8a9e0b72 338 Status = Fvb->Write (\r
339 Fvb,\r
8d3a5c82 340 LbaNumber,\r
341 (UINTN) (CurrWritePtr - LinearOffset),\r
342 &CurrWriteSize,\r
343 CurrBuffer\r
344 );\r
8a9e0b72 345 return Status;\r
8d3a5c82 346 } else {\r
347 Size = (UINT32) (LinearOffset + PtrBlockMapEntry->Length - CurrWritePtr);\r
8a9e0b72 348 Status = Fvb->Write (\r
349 Fvb,\r
8d3a5c82 350 LbaNumber,\r
351 (UINTN) (CurrWritePtr - LinearOffset),\r
352 &Size,\r
353 CurrBuffer\r
354 );\r
355 if (EFI_ERROR (Status)) {\r
356 return Status;\r
357 }\r
358\r
359 CurrWritePtr = LinearOffset + PtrBlockMapEntry->Length;\r
360 CurrBuffer = CurrBuffer + Size;\r
361 CurrWriteSize = CurrWriteSize - Size;\r
362 }\r
363 }\r
364\r
365 LinearOffset += PtrBlockMapEntry->Length;\r
366 LbaNumber++;\r
367 }\r
368 }\r
369\r
370 return EFI_SUCCESS;\r
371}\r
372\r
052ad7e1 373\r
7c80e839 374/**\r
8d3a5c82 375\r
376 This code gets the current status of Variable Store.\r
377\r
7c80e839 378 @param VarStoreHeader Pointer to the Variable Store Header.\r
8d3a5c82 379\r
8a2d4996 380 @retval EfiRaw Variable store status is raw.\r
381 @retval EfiValid Variable store status is valid.\r
382 @retval EfiInvalid Variable store status is invalid.\r
8d3a5c82 383\r
7c80e839 384**/\r
385VARIABLE_STORE_STATUS\r
386GetVariableStoreStatus (\r
387 IN VARIABLE_STORE_HEADER *VarStoreHeader\r
388 )\r
8d3a5c82 389{\r
fa0737a8
SZ
390 if ((CompareGuid (&VarStoreHeader->Signature, &gEfiAuthenticatedVariableGuid) ||\r
391 CompareGuid (&VarStoreHeader->Signature, &gEfiVariableGuid)) &&\r
8d3a5c82 392 VarStoreHeader->Format == VARIABLE_STORE_FORMATTED &&\r
393 VarStoreHeader->State == VARIABLE_STORE_HEALTHY\r
394 ) {\r
395\r
396 return EfiValid;\r
3709c4cd 397 } else if (((UINT32 *)(&VarStoreHeader->Signature))[0] == 0xffffffff &&\r
398 ((UINT32 *)(&VarStoreHeader->Signature))[1] == 0xffffffff &&\r
399 ((UINT32 *)(&VarStoreHeader->Signature))[2] == 0xffffffff &&\r
400 ((UINT32 *)(&VarStoreHeader->Signature))[3] == 0xffffffff &&\r
401 VarStoreHeader->Size == 0xffffffff &&\r
402 VarStoreHeader->Format == 0xff &&\r
403 VarStoreHeader->State == 0xff\r
8d3a5c82 404 ) {\r
405\r
406 return EfiRaw;\r
407 } else {\r
408 return EfiInvalid;\r
409 }\r
410}\r
411\r
fa0737a8
SZ
412/**\r
413 This code gets the size of variable header.\r
414\r
415 @return Size of variable header in bytes in type UINTN.\r
416\r
417**/\r
418UINTN\r
419GetVariableHeaderSize (\r
420 VOID\r
421 )\r
422{\r
423 UINTN Value;\r
424\r
425 if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {\r
426 Value = sizeof (AUTHENTICATED_VARIABLE_HEADER);\r
427 } else {\r
428 Value = sizeof (VARIABLE_HEADER);\r
429 }\r
430\r
431 return Value;\r
432}\r
130e2569 433\r
7c80e839 434/**\r
130e2569 435\r
436 This code gets the size of name of variable.\r
437\r
8a2d4996 438 @param Variable Pointer to the Variable Header.\r
130e2569 439\r
8a2d4996 440 @return UINTN Size of variable in bytes.\r
130e2569 441\r
7c80e839 442**/\r
443UINTN\r
444NameSizeOfVariable (\r
445 IN VARIABLE_HEADER *Variable\r
446 )\r
130e2569 447{\r
fa0737a8
SZ
448 AUTHENTICATED_VARIABLE_HEADER *AuthVariable;\r
449\r
450 AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;\r
451 if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {\r
452 if (AuthVariable->State == (UINT8) (-1) ||\r
453 AuthVariable->DataSize == (UINT32) (-1) ||\r
454 AuthVariable->NameSize == (UINT32) (-1) ||\r
455 AuthVariable->Attributes == (UINT32) (-1)) {\r
456 return 0;\r
457 }\r
458 return (UINTN) AuthVariable->NameSize;\r
459 } else {\r
460 if (Variable->State == (UINT8) (-1) ||\r
461 Variable->DataSize == (UINT32) (-1) ||\r
462 Variable->NameSize == (UINT32) (-1) ||\r
463 Variable->Attributes == (UINT32) (-1)) {\r
464 return 0;\r
465 }\r
466 return (UINTN) Variable->NameSize;\r
467 }\r
468}\r
469\r
470/**\r
471 This code sets the size of name of variable.\r
472\r
473 @param[in] Variable Pointer to the Variable Header.\r
474 @param[in] NameSize Name size to set.\r
475\r
476**/\r
477VOID\r
478SetNameSizeOfVariable (\r
479 IN VARIABLE_HEADER *Variable,\r
480 IN UINTN NameSize\r
481 )\r
482{\r
483 AUTHENTICATED_VARIABLE_HEADER *AuthVariable;\r
484\r
485 AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;\r
486 if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {\r
487 AuthVariable->NameSize = (UINT32) NameSize;\r
488 } else {\r
489 Variable->NameSize = (UINT32) NameSize;\r
130e2569 490 }\r
130e2569 491}\r
492\r
7c80e839 493/**\r
130e2569 494\r
7c80e839 495 This code gets the size of variable data.\r
130e2569 496\r
8a2d4996 497 @param Variable Pointer to the Variable Header.\r
130e2569 498\r
8a2d4996 499 @return Size of variable in bytes.\r
130e2569 500\r
7c80e839 501**/\r
502UINTN\r
503DataSizeOfVariable (\r
504 IN VARIABLE_HEADER *Variable\r
505 )\r
130e2569 506{\r
fa0737a8
SZ
507 AUTHENTICATED_VARIABLE_HEADER *AuthVariable;\r
508\r
509 AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;\r
510 if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {\r
511 if (AuthVariable->State == (UINT8) (-1) ||\r
512 AuthVariable->DataSize == (UINT32) (-1) ||\r
513 AuthVariable->NameSize == (UINT32) (-1) ||\r
514 AuthVariable->Attributes == (UINT32) (-1)) {\r
515 return 0;\r
516 }\r
517 return (UINTN) AuthVariable->DataSize;\r
518 } else {\r
519 if (Variable->State == (UINT8) (-1) ||\r
520 Variable->DataSize == (UINT32) (-1) ||\r
521 Variable->NameSize == (UINT32) (-1) ||\r
522 Variable->Attributes == (UINT32) (-1)) {\r
523 return 0;\r
524 }\r
525 return (UINTN) Variable->DataSize;\r
526 }\r
527}\r
528\r
529/**\r
530 This code sets the size of variable data.\r
531\r
532 @param[in] Variable Pointer to the Variable Header.\r
533 @param[in] DataSize Data size to set.\r
534\r
535**/\r
536VOID\r
537SetDataSizeOfVariable (\r
538 IN VARIABLE_HEADER *Variable,\r
539 IN UINTN DataSize\r
540 )\r
541{\r
542 AUTHENTICATED_VARIABLE_HEADER *AuthVariable;\r
543\r
544 AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;\r
545 if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {\r
546 AuthVariable->DataSize = (UINT32) DataSize;\r
547 } else {\r
548 Variable->DataSize = (UINT32) DataSize;\r
130e2569 549 }\r
130e2569 550}\r
551\r
7c80e839 552/**\r
130e2569 553\r
554 This code gets the pointer to the variable name.\r
555\r
8a2d4996 556 @param Variable Pointer to the Variable Header.\r
130e2569 557\r
8a2d4996 558 @return Pointer to Variable Name which is Unicode encoding.\r
130e2569 559\r
7c80e839 560**/\r
561CHAR16 *\r
562GetVariableNamePtr (\r
563 IN VARIABLE_HEADER *Variable\r
564 )\r
130e2569 565{\r
fa0737a8
SZ
566 return (CHAR16 *) ((UINTN) Variable + GetVariableHeaderSize ());\r
567}\r
130e2569 568\r
fa0737a8
SZ
569/**\r
570 This code gets the pointer to the variable guid.\r
571\r
572 @param Variable Pointer to the Variable Header.\r
573\r
574 @return A EFI_GUID* pointer to Vendor Guid.\r
575\r
576**/\r
577EFI_GUID *\r
578GetVendorGuidPtr (\r
579 IN VARIABLE_HEADER *Variable\r
580 )\r
581{\r
582 AUTHENTICATED_VARIABLE_HEADER *AuthVariable;\r
583\r
584 AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;\r
585 if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {\r
586 return &AuthVariable->VendorGuid;\r
587 } else {\r
588 return &Variable->VendorGuid;\r
589 }\r
130e2569 590}\r
591\r
7c80e839 592/**\r
8d3a5c82 593\r
594 This code gets the pointer to the variable data.\r
595\r
8a2d4996 596 @param Variable Pointer to the Variable Header.\r
8d3a5c82 597\r
8a2d4996 598 @return Pointer to Variable Data.\r
8d3a5c82 599\r
7c80e839 600**/\r
601UINT8 *\r
602GetVariableDataPtr (\r
603 IN VARIABLE_HEADER *Variable\r
604 )\r
8d3a5c82 605{\r
130e2569 606 UINTN Value;\r
fa0737a8 607\r
8d3a5c82 608 //\r
8a2d4996 609 // Be careful about pad size for alignment.\r
8d3a5c82 610 //\r
130e2569 611 Value = (UINTN) GetVariableNamePtr (Variable);\r
612 Value += NameSizeOfVariable (Variable);\r
613 Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));\r
614\r
615 return (UINT8 *) Value;\r
8d3a5c82 616}\r
617\r
fa0737a8
SZ
618/**\r
619 This code gets the variable data offset related to variable header.\r
620\r
621 @param Variable Pointer to the Variable Header.\r
622\r
623 @return Variable Data offset.\r
624\r
625**/\r
626UINTN\r
627GetVariableDataOffset (\r
628 IN VARIABLE_HEADER *Variable\r
629 )\r
630{\r
631 UINTN Value;\r
632\r
633 //\r
634 // Be careful about pad size for alignment\r
635 //\r
636 Value = GetVariableHeaderSize ();\r
637 Value += NameSizeOfVariable (Variable);\r
638 Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));\r
639\r
640 return Value;\r
641}\r
052ad7e1 642\r
7c80e839 643/**\r
8d3a5c82 644\r
645 This code gets the pointer to the next variable header.\r
646\r
8a2d4996 647 @param Variable Pointer to the Variable Header.\r
8d3a5c82 648\r
8a2d4996 649 @return Pointer to next variable header.\r
8d3a5c82 650\r
7c80e839 651**/\r
652VARIABLE_HEADER *\r
653GetNextVariablePtr (\r
654 IN VARIABLE_HEADER *Variable\r
655 )\r
8d3a5c82 656{\r
130e2569 657 UINTN Value;\r
658\r
130e2569 659 Value = (UINTN) GetVariableDataPtr (Variable);\r
660 Value += DataSizeOfVariable (Variable);\r
661 Value += GET_PAD_SIZE (DataSizeOfVariable (Variable));\r
662\r
8d3a5c82 663 //\r
8a2d4996 664 // Be careful about pad size for alignment.\r
8d3a5c82 665 //\r
130e2569 666 return (VARIABLE_HEADER *) HEADER_ALIGN (Value);\r
8d3a5c82 667}\r
668\r
7c80e839 669/**\r
9cad030b 670\r
7c80e839 671 Gets the pointer to the first variable header in given variable store area.\r
9cad030b 672\r
7c80e839 673 @param VarStoreHeader Pointer to the Variable Store Header.\r
9cad030b 674\r
8a2d4996 675 @return Pointer to the first variable header.\r
9cad030b 676\r
7c80e839 677**/\r
678VARIABLE_HEADER *\r
679GetStartPointer (\r
680 IN VARIABLE_STORE_HEADER *VarStoreHeader\r
681 )\r
9cad030b 682{\r
683 //\r
8a2d4996 684 // The end of variable store.\r
9cad030b 685 //\r
686 return (VARIABLE_HEADER *) HEADER_ALIGN (VarStoreHeader + 1);\r
687}\r
052ad7e1 688\r
7c80e839 689/**\r
8d3a5c82 690\r
7c80e839 691 Gets the pointer to the end of the variable storage area.\r
8d3a5c82 692\r
7c80e839 693 This function gets pointer to the end of the variable storage\r
694 area, according to the input variable store header.\r
8d3a5c82 695\r
8a2d4996 696 @param VarStoreHeader Pointer to the Variable Store Header.\r
8d3a5c82 697\r
fa0737a8 698 @return Pointer to the end of the variable storage area.\r
8d3a5c82 699\r
7c80e839 700**/\r
701VARIABLE_HEADER *\r
702GetEndPointer (\r
703 IN VARIABLE_STORE_HEADER *VarStoreHeader\r
704 )\r
8d3a5c82 705{\r
706 //\r
707 // The end of variable store\r
708 //\r
9cad030b 709 return (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) VarStoreHeader + VarStoreHeader->Size);\r
8d3a5c82 710}\r
711\r
4edb1866
SZ
712/**\r
713 Record variable error flag.\r
714\r
715 @param[in] Flag Variable error flag to record.\r
716 @param[in] VariableName Name of variable.\r
717 @param[in] VendorGuid Guid of variable.\r
718 @param[in] Attributes Attributes of the variable.\r
719 @param[in] VariableSize Size of the variable.\r
720\r
721**/\r
722VOID\r
723RecordVarErrorFlag (\r
724 IN VAR_ERROR_FLAG Flag,\r
725 IN CHAR16 *VariableName,\r
726 IN EFI_GUID *VendorGuid,\r
727 IN UINT32 Attributes,\r
728 IN UINTN VariableSize\r
729 )\r
730{\r
731 EFI_STATUS Status;\r
732 VARIABLE_POINTER_TRACK Variable;\r
733 VAR_ERROR_FLAG *VarErrFlag;\r
734 VAR_ERROR_FLAG TempFlag;\r
735\r
736 DEBUG_CODE (\r
737 DEBUG ((EFI_D_ERROR, "RecordVarErrorFlag (0x%02x) %s:%g - 0x%08x - 0x%x\n", Flag, VariableName, VendorGuid, Attributes, VariableSize));\r
738 if (Flag == VAR_ERROR_FLAG_SYSTEM_ERROR) {\r
739 if (AtRuntime ()) {\r
740 DEBUG ((EFI_D_ERROR, "CommonRuntimeVariableSpace = 0x%x - CommonVariableTotalSize = 0x%x\n", mVariableModuleGlobal->CommonRuntimeVariableSpace, mVariableModuleGlobal->CommonVariableTotalSize));\r
741 } else {\r
742 DEBUG ((EFI_D_ERROR, "CommonVariableSpace = 0x%x - CommonVariableTotalSize = 0x%x\n", mVariableModuleGlobal->CommonVariableSpace, mVariableModuleGlobal->CommonVariableTotalSize));\r
743 }\r
744 } else {\r
745 DEBUG ((EFI_D_ERROR, "CommonMaxUserVariableSpace = 0x%x - CommonUserVariableTotalSize = 0x%x\n", mVariableModuleGlobal->CommonMaxUserVariableSpace, mVariableModuleGlobal->CommonUserVariableTotalSize));\r
746 }\r
747 );\r
748\r
00ab76e0
SZ
749 if (!mEndOfDxe) {\r
750 //\r
751 // Before EndOfDxe, just record the current boot variable error flag to local variable,\r
752 // and leave the variable error flag in NV flash as the last boot variable error flag.\r
753 // After EndOfDxe in InitializeVarErrorFlag (), the variable error flag in NV flash\r
754 // will be initialized to this local current boot variable error flag.\r
755 //\r
756 mCurrentBootVarErrFlag &= Flag;\r
757 return;\r
758 }\r
759\r
4edb1866
SZ
760 //\r
761 // Record error flag (it should have be initialized).\r
762 //\r
763 Status = FindVariable (\r
764 VAR_ERROR_FLAG_NAME,\r
765 &gEdkiiVarErrorFlagGuid,\r
766 &Variable,\r
767 &mVariableModuleGlobal->VariableGlobal,\r
768 FALSE\r
769 );\r
770 if (!EFI_ERROR (Status)) {\r
771 VarErrFlag = (VAR_ERROR_FLAG *) GetVariableDataPtr (Variable.CurrPtr);\r
772 TempFlag = *VarErrFlag;\r
773 TempFlag &= Flag;\r
774 if (TempFlag == *VarErrFlag) {\r
775 return;\r
776 }\r
777 Status = UpdateVariableStore (\r
778 &mVariableModuleGlobal->VariableGlobal,\r
779 FALSE,\r
780 FALSE,\r
781 mVariableModuleGlobal->FvbInstance,\r
782 (UINTN) VarErrFlag - (UINTN) mNvVariableCache + (UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
783 sizeof (TempFlag),\r
784 &TempFlag\r
785 );\r
786 if (!EFI_ERROR (Status)) {\r
787 //\r
788 // Update the data in NV cache.\r
789 //\r
4a23636a 790 *VarErrFlag = TempFlag;\r
4edb1866
SZ
791 }\r
792 }\r
793}\r
794\r
795/**\r
796 Initialize variable error flag.\r
797\r
798 Before EndOfDxe, the variable indicates the last boot variable error flag,\r
799 then it means the last boot variable error flag must be got before EndOfDxe.\r
800 After EndOfDxe, the variable indicates the current boot variable error flag,\r
801 then it means the current boot variable error flag must be got after EndOfDxe.\r
802\r
803**/\r
804VOID\r
805InitializeVarErrorFlag (\r
806 VOID\r
807 )\r
808{\r
809 EFI_STATUS Status;\r
810 VARIABLE_POINTER_TRACK Variable;\r
811 VAR_ERROR_FLAG Flag;\r
812 VAR_ERROR_FLAG VarErrFlag;\r
813\r
814 if (!mEndOfDxe) {\r
815 return;\r
816 }\r
817\r
00ab76e0 818 Flag = mCurrentBootVarErrFlag;\r
4edb1866
SZ
819 DEBUG ((EFI_D_INFO, "Initialize variable error flag (%02x)\n", Flag));\r
820\r
821 Status = FindVariable (\r
822 VAR_ERROR_FLAG_NAME,\r
823 &gEdkiiVarErrorFlagGuid,\r
824 &Variable,\r
825 &mVariableModuleGlobal->VariableGlobal,\r
826 FALSE\r
827 );\r
828 if (!EFI_ERROR (Status)) {\r
829 VarErrFlag = *((VAR_ERROR_FLAG *) GetVariableDataPtr (Variable.CurrPtr));\r
830 if (VarErrFlag == Flag) {\r
831 return;\r
832 }\r
833 }\r
834\r
835 UpdateVariable (\r
836 VAR_ERROR_FLAG_NAME,\r
837 &gEdkiiVarErrorFlagGuid,\r
838 &Flag,\r
839 sizeof (Flag),\r
840 VARIABLE_ATTRIBUTE_NV_BS_RT,\r
fa0737a8
SZ
841 0,\r
842 0,\r
843 &Variable,\r
844 NULL\r
4edb1866
SZ
845 );\r
846}\r
847\r
848/**\r
849 Is user variable?\r
850\r
851 @param[in] Variable Pointer to variable header.\r
852\r
853 @retval TRUE User variable.\r
854 @retval FALSE System variable.\r
855\r
856**/\r
857BOOLEAN\r
858IsUserVariable (\r
859 IN VARIABLE_HEADER *Variable\r
860 )\r
861{\r
862 VAR_CHECK_VARIABLE_PROPERTY Property;\r
863\r
864 //\r
865 // Only after End Of Dxe, the variables belong to system variable are fixed.\r
866 // If PcdMaxUserNvStorageVariableSize is 0, it means user variable share the same NV storage with system variable,\r
867 // then no need to check if the variable is user variable or not specially.\r
868 //\r
869 if (mEndOfDxe && (mVariableModuleGlobal->CommonMaxUserVariableSpace != mVariableModuleGlobal->CommonVariableSpace)) {\r
8021f4c7 870 if (VarCheckLibVariablePropertyGet (GetVariableNamePtr (Variable), GetVendorGuidPtr (Variable), &Property) == EFI_NOT_FOUND) {\r
4edb1866
SZ
871 return TRUE;\r
872 }\r
873 }\r
874 return FALSE;\r
875}\r
876\r
877/**\r
878 Calculate common user variable total size.\r
879\r
880**/\r
881VOID\r
882CalculateCommonUserVariableTotalSize (\r
883 VOID\r
884 )\r
885{\r
886 VARIABLE_HEADER *Variable;\r
887 VARIABLE_HEADER *NextVariable;\r
888 UINTN VariableSize;\r
889 VAR_CHECK_VARIABLE_PROPERTY Property;\r
890\r
891 //\r
892 // Only after End Of Dxe, the variables belong to system variable are fixed.\r
893 // If PcdMaxUserNvStorageVariableSize is 0, it means user variable share the same NV storage with system variable,\r
894 // then no need to calculate the common user variable total size specially.\r
895 //\r
896 if (mEndOfDxe && (mVariableModuleGlobal->CommonMaxUserVariableSpace != mVariableModuleGlobal->CommonVariableSpace)) {\r
897 Variable = GetStartPointer (mNvVariableCache);\r
898 while (IsValidVariableHeader (Variable, GetEndPointer (mNvVariableCache))) {\r
899 NextVariable = GetNextVariablePtr (Variable);\r
900 VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
901 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
8021f4c7 902 if (VarCheckLibVariablePropertyGet (GetVariableNamePtr (Variable), GetVendorGuidPtr (Variable), &Property) == EFI_NOT_FOUND) {\r
4edb1866
SZ
903 //\r
904 // No property, it is user variable.\r
905 //\r
906 mVariableModuleGlobal->CommonUserVariableTotalSize += VariableSize;\r
907 }\r
908 }\r
909\r
910 Variable = NextVariable;\r
911 }\r
912 }\r
913}\r
914\r
915/**\r
916 Initialize variable quota.\r
917\r
918**/\r
919VOID\r
920InitializeVariableQuota (\r
921 VOID\r
922 )\r
923{\r
8021f4c7 924 if (!mEndOfDxe) {\r
4edb1866
SZ
925 return;\r
926 }\r
4edb1866
SZ
927\r
928 InitializeVarErrorFlag ();\r
929 CalculateCommonUserVariableTotalSize ();\r
930}\r
052ad7e1 931\r
7c80e839 932/**\r
933\r
934 Variable store garbage collection and reclaim operation.\r
935\r
fa0737a8
SZ
936 @param[in] VariableBase Base address of variable store.\r
937 @param[out] LastVariableOffset Offset of last variable.\r
938 @param[in] IsVolatile The variable store is volatile or not;\r
939 if it is non-volatile, need FTW.\r
940 @param[in, out] UpdatingPtrTrack Pointer to updating variable pointer track structure.\r
941 @param[in] NewVariable Pointer to new variable.\r
942 @param[in] NewVariableSize New variable size.\r
7c80e839 943\r
fa0737a8
SZ
944 @return EFI_SUCCESS Reclaim operation has finished successfully.\r
945 @return EFI_OUT_OF_RESOURCES No enough memory resources or variable space.\r
946 @return Others Unexpect error happened during reclaim operation.\r
7c80e839 947\r
948**/\r
8d3a5c82 949EFI_STATUS\r
8d3a5c82 950Reclaim (\r
fa0737a8
SZ
951 IN EFI_PHYSICAL_ADDRESS VariableBase,\r
952 OUT UINTN *LastVariableOffset,\r
953 IN BOOLEAN IsVolatile,\r
954 IN OUT VARIABLE_POINTER_TRACK *UpdatingPtrTrack,\r
955 IN VARIABLE_HEADER *NewVariable,\r
956 IN UINTN NewVariableSize\r
8d3a5c82 957 )\r
8d3a5c82 958{\r
959 VARIABLE_HEADER *Variable;\r
814bae52 960 VARIABLE_HEADER *AddedVariable;\r
8d3a5c82 961 VARIABLE_HEADER *NextVariable;\r
814bae52 962 VARIABLE_HEADER *NextAddedVariable;\r
8d3a5c82 963 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
964 UINT8 *ValidBuffer;\r
814bae52 965 UINTN MaximumBufferSize;\r
8d3a5c82 966 UINTN VariableSize;\r
814bae52 967 UINTN NameSize;\r
8d3a5c82 968 UINT8 *CurrPtr;\r
814bae52 969 VOID *Point0;\r
970 VOID *Point1;\r
971 BOOLEAN FoundAdded;\r
8d3a5c82 972 EFI_STATUS Status;\r
8f3a9e58 973 UINTN CommonVariableTotalSize;\r
4edb1866 974 UINTN CommonUserVariableTotalSize;\r
8f3a9e58 975 UINTN HwErrVariableTotalSize;\r
23b06935 976 VARIABLE_HEADER *UpdatingVariable;\r
7baf3c69 977 VARIABLE_HEADER *UpdatingInDeletedTransition;\r
23b06935
SZ
978\r
979 UpdatingVariable = NULL;\r
7baf3c69 980 UpdatingInDeletedTransition = NULL;\r
23b06935
SZ
981 if (UpdatingPtrTrack != NULL) {\r
982 UpdatingVariable = UpdatingPtrTrack->CurrPtr;\r
7baf3c69 983 UpdatingInDeletedTransition = UpdatingPtrTrack->InDeletedTransitionPtr;\r
23b06935 984 }\r
8d3a5c82 985\r
986 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) VariableBase);\r
8f3a9e58
SZ
987\r
988 CommonVariableTotalSize = 0;\r
4edb1866 989 CommonUserVariableTotalSize = 0;\r
8f3a9e58 990 HwErrVariableTotalSize = 0;\r
8d3a5c82 991\r
128ef095
SZ
992 if (IsVolatile) {\r
993 //\r
994 // Start Pointers for the variable.\r
995 //\r
996 Variable = GetStartPointer (VariableStoreHeader);\r
997 MaximumBufferSize = sizeof (VARIABLE_STORE_HEADER);\r
998\r
6ebffb67 999 while (IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))) {\r
128ef095
SZ
1000 NextVariable = GetNextVariablePtr (Variable);\r
1001 if ((Variable->State == VAR_ADDED || Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) &&\r
1002 Variable != UpdatingVariable &&\r
1003 Variable != UpdatingInDeletedTransition\r
1004 ) {\r
1005 VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
1006 MaximumBufferSize += VariableSize;\r
1007 }\r
8d3a5c82 1008\r
128ef095 1009 Variable = NextVariable;\r
8d3a5c82 1010 }\r
1011\r
128ef095
SZ
1012 if (NewVariable != NULL) {\r
1013 //\r
1014 // Add the new variable size.\r
1015 //\r
1016 MaximumBufferSize += NewVariableSize;\r
1017 }\r
8d3a5c82 1018\r
7baf3c69 1019 //\r
128ef095
SZ
1020 // Reserve the 1 Bytes with Oxff to identify the\r
1021 // end of the variable buffer.\r
7baf3c69 1022 //\r
128ef095
SZ
1023 MaximumBufferSize += 1;\r
1024 ValidBuffer = AllocatePool (MaximumBufferSize);\r
1025 if (ValidBuffer == NULL) {\r
1026 return EFI_OUT_OF_RESOURCES;\r
1027 }\r
1028 } else {\r
1029 //\r
1030 // For NV variable reclaim, don't allocate pool here and just use mNvVariableCache\r
1031 // as the buffer to reduce SMRAM consumption for SMM variable driver.\r
1032 //\r
1033 MaximumBufferSize = mNvVariableCache->Size;\r
1034 ValidBuffer = (UINT8 *) mNvVariableCache;\r
8d3a5c82 1035 }\r
1036\r
814bae52 1037 SetMem (ValidBuffer, MaximumBufferSize, 0xff);\r
8d3a5c82 1038\r
1039 //\r
8a2d4996 1040 // Copy variable store header.\r
8d3a5c82 1041 //\r
814bae52 1042 CopyMem (ValidBuffer, VariableStoreHeader, sizeof (VARIABLE_STORE_HEADER));\r
1043 CurrPtr = (UINT8 *) GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer);\r
8d3a5c82 1044\r
814bae52 1045 //\r
8a2d4996 1046 // Reinstall all ADDED variables as long as they are not identical to Updating Variable.\r
fa0737a8 1047 //\r
814bae52 1048 Variable = GetStartPointer (VariableStoreHeader);\r
6ebffb67 1049 while (IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))) {\r
8d3a5c82 1050 NextVariable = GetNextVariablePtr (Variable);\r
7baf3c69 1051 if (Variable != UpdatingVariable && Variable->State == VAR_ADDED) {\r
8d3a5c82 1052 VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
1053 CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);\r
1054 CurrPtr += VariableSize;\r
2fcdca1d 1055 if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
8f3a9e58 1056 HwErrVariableTotalSize += VariableSize;\r
2fcdca1d 1057 } else if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
8f3a9e58 1058 CommonVariableTotalSize += VariableSize;\r
4edb1866
SZ
1059 if (IsUserVariable (Variable)) {\r
1060 CommonUserVariableTotalSize += VariableSize;\r
1061 }\r
2fcdca1d 1062 }\r
8d3a5c82 1063 }\r
8d3a5c82 1064 Variable = NextVariable;\r
1065 }\r
5ead4a07 1066\r
814bae52 1067 //\r
8a2d4996 1068 // Reinstall all in delete transition variables.\r
fa0737a8 1069 //\r
7baf3c69 1070 Variable = GetStartPointer (VariableStoreHeader);\r
6ebffb67 1071 while (IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))) {\r
814bae52 1072 NextVariable = GetNextVariablePtr (Variable);\r
7baf3c69 1073 if (Variable != UpdatingVariable && Variable != UpdatingInDeletedTransition && Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
814bae52 1074\r
1075 //\r
fa0737a8 1076 // Buffer has cached all ADDED variable.\r
814bae52 1077 // Per IN_DELETED variable, we have to guarantee that\r
fa0737a8
SZ
1078 // no ADDED one in previous buffer.\r
1079 //\r
1080\r
814bae52 1081 FoundAdded = FALSE;\r
1082 AddedVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer);\r
6ebffb67 1083 while (IsValidVariableHeader (AddedVariable, GetEndPointer ((VARIABLE_STORE_HEADER *) ValidBuffer))) {\r
814bae52 1084 NextAddedVariable = GetNextVariablePtr (AddedVariable);\r
1085 NameSize = NameSizeOfVariable (AddedVariable);\r
fa0737a8 1086 if (CompareGuid (GetVendorGuidPtr (AddedVariable), GetVendorGuidPtr (Variable)) &&\r
814bae52 1087 NameSize == NameSizeOfVariable (Variable)\r
1088 ) {\r
1089 Point0 = (VOID *) GetVariableNamePtr (AddedVariable);\r
1090 Point1 = (VOID *) GetVariableNamePtr (Variable);\r
23b06935 1091 if (CompareMem (Point0, Point1, NameSize) == 0) {\r
814bae52 1092 FoundAdded = TRUE;\r
1093 break;\r
1094 }\r
1095 }\r
1096 AddedVariable = NextAddedVariable;\r
1097 }\r
1098 if (!FoundAdded) {\r
5ead4a07 1099 //\r
8a2d4996 1100 // Promote VAR_IN_DELETED_TRANSITION to VAR_ADDED.\r
5ead4a07 1101 //\r
814bae52 1102 VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
1103 CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);\r
5ead4a07 1104 ((VARIABLE_HEADER *) CurrPtr)->State = VAR_ADDED;\r
814bae52 1105 CurrPtr += VariableSize;\r
2fcdca1d 1106 if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
8f3a9e58 1107 HwErrVariableTotalSize += VariableSize;\r
2fcdca1d 1108 } else if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
8f3a9e58 1109 CommonVariableTotalSize += VariableSize;\r
4edb1866
SZ
1110 if (IsUserVariable (Variable)) {\r
1111 CommonUserVariableTotalSize += VariableSize;\r
1112 }\r
2fcdca1d 1113 }\r
814bae52 1114 }\r
1115 }\r
1116\r
1117 Variable = NextVariable;\r
1118 }\r
8d3a5c82 1119\r
7baf3c69
SZ
1120 //\r
1121 // Install the new variable if it is not NULL.\r
1122 //\r
1123 if (NewVariable != NULL) {\r
809e2bbf 1124 if (((UINTN) CurrPtr - (UINTN) ValidBuffer) + NewVariableSize > VariableStoreHeader->Size) {\r
7baf3c69
SZ
1125 //\r
1126 // No enough space to store the new variable.\r
1127 //\r
1128 Status = EFI_OUT_OF_RESOURCES;\r
1129 goto Done;\r
1130 }\r
1131 if (!IsVolatile) {\r
1132 if ((NewVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
1133 HwErrVariableTotalSize += NewVariableSize;\r
1134 } else if ((NewVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
1135 CommonVariableTotalSize += NewVariableSize;\r
4edb1866
SZ
1136 if (IsUserVariable (NewVariable)) {\r
1137 CommonUserVariableTotalSize += NewVariableSize;\r
1138 }\r
7baf3c69
SZ
1139 }\r
1140 if ((HwErrVariableTotalSize > PcdGet32 (PcdHwErrStorageSize)) ||\r
4edb1866
SZ
1141 (CommonVariableTotalSize > mVariableModuleGlobal->CommonVariableSpace) ||\r
1142 (CommonUserVariableTotalSize > mVariableModuleGlobal->CommonMaxUserVariableSpace)) {\r
7baf3c69
SZ
1143 //\r
1144 // No enough space to store the new variable by NV or NV+HR attribute.\r
1145 //\r
1146 Status = EFI_OUT_OF_RESOURCES;\r
1147 goto Done;\r
1148 }\r
1149 }\r
1150\r
1151 CopyMem (CurrPtr, (UINT8 *) NewVariable, NewVariableSize);\r
1152 ((VARIABLE_HEADER *) CurrPtr)->State = VAR_ADDED;\r
1153 if (UpdatingVariable != NULL) {\r
1154 UpdatingPtrTrack->CurrPtr = (VARIABLE_HEADER *)((UINTN)UpdatingPtrTrack->StartPtr + ((UINTN)CurrPtr - (UINTN)GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer)));\r
1155 UpdatingPtrTrack->InDeletedTransitionPtr = NULL;\r
1156 }\r
1157 CurrPtr += NewVariableSize;\r
1158 }\r
1159\r
8d3a5c82 1160 if (IsVolatile) {\r
1161 //\r
8a2d4996 1162 // If volatile variable store, just copy valid buffer.\r
8d3a5c82 1163 //\r
1164 SetMem ((UINT8 *) (UINTN) VariableBase, VariableStoreHeader->Size, 0xff);\r
809e2bbf
HW
1165 CopyMem ((UINT8 *) (UINTN) VariableBase, ValidBuffer, (UINTN) CurrPtr - (UINTN) ValidBuffer);\r
1166 *LastVariableOffset = (UINTN) CurrPtr - (UINTN) ValidBuffer;\r
8a2d4996 1167 Status = EFI_SUCCESS;\r
8d3a5c82 1168 } else {\r
1169 //\r
1170 // If non-volatile variable store, perform FTW here.\r
1171 //\r
1172 Status = FtwVariableSpace (\r
1173 VariableBase,\r
128ef095 1174 (VARIABLE_STORE_HEADER *) ValidBuffer\r
8d3a5c82 1175 );\r
128ef095 1176 if (!EFI_ERROR (Status)) {\r
809e2bbf 1177 *LastVariableOffset = (UINTN) CurrPtr - (UINTN) ValidBuffer;\r
8f3a9e58
SZ
1178 mVariableModuleGlobal->HwErrVariableTotalSize = HwErrVariableTotalSize;\r
1179 mVariableModuleGlobal->CommonVariableTotalSize = CommonVariableTotalSize;\r
4edb1866 1180 mVariableModuleGlobal->CommonUserVariableTotalSize = CommonUserVariableTotalSize;\r
128ef095 1181 } else {\r
b2c59ce8
SZ
1182 mVariableModuleGlobal->HwErrVariableTotalSize = 0;\r
1183 mVariableModuleGlobal->CommonVariableTotalSize = 0;\r
1184 mVariableModuleGlobal->CommonUserVariableTotalSize = 0;\r
4edb1866
SZ
1185 Variable = GetStartPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableBase);\r
1186 while (IsValidVariableHeader (Variable, GetEndPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableBase))) {\r
1187 NextVariable = GetNextVariablePtr (Variable);\r
1188 VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
128ef095 1189 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
4edb1866 1190 mVariableModuleGlobal->HwErrVariableTotalSize += VariableSize;\r
128ef095 1191 } else if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
4edb1866
SZ
1192 mVariableModuleGlobal->CommonVariableTotalSize += VariableSize;\r
1193 if (IsUserVariable (Variable)) {\r
1194 mVariableModuleGlobal->CommonUserVariableTotalSize += VariableSize;\r
1195 }\r
128ef095 1196 }\r
8f3a9e58 1197\r
4edb1866 1198 Variable = NextVariable;\r
128ef095 1199 }\r
4edb1866 1200 *LastVariableOffset = (UINTN) Variable - (UINTN) VariableBase;\r
8f3a9e58 1201 }\r
8d3a5c82 1202 }\r
1203\r
7baf3c69 1204Done:\r
128ef095
SZ
1205 if (IsVolatile) {\r
1206 FreePool (ValidBuffer);\r
1207 } else {\r
1208 //\r
1209 // For NV variable reclaim, we use mNvVariableCache as the buffer, so copy the data back.\r
1210 //\r
1211 CopyMem (mNvVariableCache, (UINT8 *)(UINTN)VariableBase, VariableStoreHeader->Size);\r
1212 }\r
814bae52 1213\r
8d3a5c82 1214 return Status;\r
1215}\r
1216\r
0f7aff72
RN
1217/**\r
1218 Find the variable in the specified variable store.\r
1219\r
fa0737a8
SZ
1220 @param[in] VariableName Name of the variable to be found\r
1221 @param[in] VendorGuid Vendor GUID to be found.\r
1222 @param[in] IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute\r
1223 check at runtime when searching variable.\r
1224 @param[in, out] PtrTrack Variable Track Pointer structure that contains Variable Information.\r
0f7aff72 1225\r
fa0737a8
SZ
1226 @retval EFI_SUCCESS Variable found successfully\r
1227 @retval EFI_NOT_FOUND Variable not found\r
0f7aff72
RN
1228**/\r
1229EFI_STATUS\r
1230FindVariableEx (\r
1231 IN CHAR16 *VariableName,\r
1232 IN EFI_GUID *VendorGuid,\r
9622df63 1233 IN BOOLEAN IgnoreRtCheck,\r
0f7aff72
RN
1234 IN OUT VARIABLE_POINTER_TRACK *PtrTrack\r
1235 )\r
1236{\r
1237 VARIABLE_HEADER *InDeletedVariable;\r
1238 VOID *Point;\r
1239\r
23b06935
SZ
1240 PtrTrack->InDeletedTransitionPtr = NULL;\r
1241\r
0f7aff72
RN
1242 //\r
1243 // Find the variable by walk through HOB, volatile and non-volatile variable store.\r
1244 //\r
1245 InDeletedVariable = NULL;\r
1246\r
1247 for ( PtrTrack->CurrPtr = PtrTrack->StartPtr\r
6ebffb67 1248 ; IsValidVariableHeader (PtrTrack->CurrPtr, PtrTrack->EndPtr)\r
0f7aff72
RN
1249 ; PtrTrack->CurrPtr = GetNextVariablePtr (PtrTrack->CurrPtr)\r
1250 ) {\r
fa0737a8 1251 if (PtrTrack->CurrPtr->State == VAR_ADDED ||\r
0f7aff72
RN
1252 PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)\r
1253 ) {\r
9622df63 1254 if (IgnoreRtCheck || !AtRuntime () || ((PtrTrack->CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {\r
0f7aff72
RN
1255 if (VariableName[0] == 0) {\r
1256 if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
1257 InDeletedVariable = PtrTrack->CurrPtr;\r
1258 } else {\r
23b06935 1259 PtrTrack->InDeletedTransitionPtr = InDeletedVariable;\r
0f7aff72
RN
1260 return EFI_SUCCESS;\r
1261 }\r
1262 } else {\r
fa0737a8 1263 if (CompareGuid (VendorGuid, GetVendorGuidPtr (PtrTrack->CurrPtr))) {\r
0f7aff72
RN
1264 Point = (VOID *) GetVariableNamePtr (PtrTrack->CurrPtr);\r
1265\r
1266 ASSERT (NameSizeOfVariable (PtrTrack->CurrPtr) != 0);\r
1267 if (CompareMem (VariableName, Point, NameSizeOfVariable (PtrTrack->CurrPtr)) == 0) {\r
1268 if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
1269 InDeletedVariable = PtrTrack->CurrPtr;\r
1270 } else {\r
23b06935 1271 PtrTrack->InDeletedTransitionPtr = InDeletedVariable;\r
0f7aff72
RN
1272 return EFI_SUCCESS;\r
1273 }\r
1274 }\r
1275 }\r
1276 }\r
1277 }\r
1278 }\r
1279 }\r
1280\r
1281 PtrTrack->CurrPtr = InDeletedVariable;\r
1282 return (PtrTrack->CurrPtr == NULL) ? EFI_NOT_FOUND : EFI_SUCCESS;\r
1283}\r
1284\r
33a5a666 1285\r
7c80e839 1286/**\r
1287 Finds variable in storage blocks of volatile and non-volatile storage areas.\r
1288\r
1289 This code finds variable in storage blocks of volatile and non-volatile storage areas.\r
1290 If VariableName is an empty string, then we just return the first\r
1291 qualified variable without comparing VariableName and VendorGuid.\r
9622df63
SZ
1292 If IgnoreRtCheck is TRUE, then we ignore the EFI_VARIABLE_RUNTIME_ACCESS attribute check\r
1293 at runtime when searching existing variable, only VariableName and VendorGuid are compared.\r
1294 Otherwise, variables without EFI_VARIABLE_RUNTIME_ACCESS are not visible at runtime.\r
7c80e839 1295\r
fa0737a8
SZ
1296 @param[in] VariableName Name of the variable to be found.\r
1297 @param[in] VendorGuid Vendor GUID to be found.\r
1298 @param[out] PtrTrack VARIABLE_POINTER_TRACK structure for output,\r
7c80e839 1299 including the range searched and the target position.\r
fa0737a8 1300 @param[in] Global Pointer to VARIABLE_GLOBAL structure, including\r
7c80e839 1301 base of volatile variable storage area, base of\r
1302 NV variable storage area, and a lock.\r
fa0737a8 1303 @param[in] IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute\r
9622df63 1304 check at runtime when searching variable.\r
7c80e839 1305\r
1306 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while\r
8a2d4996 1307 VendorGuid is NULL.\r
1308 @retval EFI_SUCCESS Variable successfully found.\r
255a3f33 1309 @retval EFI_NOT_FOUND Variable not found\r
33a5a666 1310\r
7c80e839 1311**/\r
8d3a5c82 1312EFI_STATUS\r
8d3a5c82 1313FindVariable (\r
1314 IN CHAR16 *VariableName,\r
1315 IN EFI_GUID *VendorGuid,\r
1316 OUT VARIABLE_POINTER_TRACK *PtrTrack,\r
9622df63
SZ
1317 IN VARIABLE_GLOBAL *Global,\r
1318 IN BOOLEAN IgnoreRtCheck\r
8d3a5c82 1319 )\r
8d3a5c82 1320{\r
0f7aff72
RN
1321 EFI_STATUS Status;\r
1322 VARIABLE_STORE_HEADER *VariableStoreHeader[VariableStoreTypeMax];\r
1323 VARIABLE_STORE_TYPE Type;\r
1324\r
1325 if (VariableName[0] != 0 && VendorGuid == NULL) {\r
1326 return EFI_INVALID_PARAMETER;\r
1327 }\r
8d3a5c82 1328\r
8d3a5c82 1329 //\r
0f7aff72 1330 // 0: Volatile, 1: HOB, 2: Non-Volatile.\r
36873a61 1331 // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName\r
8a2d4996 1332 // make use of this mapping to implement search algorithm.\r
8d3a5c82 1333 //\r
0f7aff72
RN
1334 VariableStoreHeader[VariableStoreTypeVolatile] = (VARIABLE_STORE_HEADER *) (UINTN) Global->VolatileVariableBase;\r
1335 VariableStoreHeader[VariableStoreTypeHob] = (VARIABLE_STORE_HEADER *) (UINTN) Global->HobVariableBase;\r
1336 VariableStoreHeader[VariableStoreTypeNv] = mNvVariableCache;\r
8d3a5c82 1337\r
1338 //\r
0f7aff72 1339 // Find the variable by walk through HOB, volatile and non-volatile variable store.\r
8d3a5c82 1340 //\r
0f7aff72
RN
1341 for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {\r
1342 if (VariableStoreHeader[Type] == NULL) {\r
1343 continue;\r
1344 }\r
814bae52 1345\r
0f7aff72
RN
1346 PtrTrack->StartPtr = GetStartPointer (VariableStoreHeader[Type]);\r
1347 PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader[Type]);\r
1348 PtrTrack->Volatile = (BOOLEAN) (Type == VariableStoreTypeVolatile);\r
8d3a5c82 1349\r
9622df63 1350 Status = FindVariableEx (VariableName, VendorGuid, IgnoreRtCheck, PtrTrack);\r
0f7aff72
RN
1351 if (!EFI_ERROR (Status)) {\r
1352 return Status;\r
814bae52 1353 }\r
8d3a5c82 1354 }\r
8d3a5c82 1355 return EFI_NOT_FOUND;\r
1356}\r
1357\r
7c80e839 1358/**\r
72399dae 1359 Get index from supported language codes according to language string.\r
1360\r
1361 This code is used to get corresponding index in supported language codes. It can handle\r
0254efc0 1362 RFC4646 and ISO639 language tags.\r
72399dae 1363 In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.\r
0254efc0 1364 In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.\r
72399dae 1365\r
1366 For example:\r
1367 SupportedLang = "engfraengfra"\r
1368 Lang = "eng"\r
1369 Iso639Language = TRUE\r
1370 The return value is "0".\r
1371 Another example:\r
1372 SupportedLang = "en;fr;en-US;fr-FR"\r
1373 Lang = "fr-FR"\r
1374 Iso639Language = FALSE\r
1375 The return value is "3".\r
1376\r
1377 @param SupportedLang Platform supported language codes.\r
1378 @param Lang Configured language.\r
0254efc0 1379 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.\r
72399dae 1380\r
8a2d4996 1381 @retval The index of language in the language codes.\r
8d3a5c82 1382\r
7c80e839 1383**/\r
72399dae 1384UINTN\r
72399dae 1385GetIndexFromSupportedLangCodes(\r
1386 IN CHAR8 *SupportedLang,\r
1387 IN CHAR8 *Lang,\r
1388 IN BOOLEAN Iso639Language\r
fa0737a8 1389 )\r
8d3a5c82 1390{\r
72399dae 1391 UINTN Index;\r
255a3f33
RN
1392 UINTN CompareLength;\r
1393 UINTN LanguageLength;\r
72399dae 1394\r
72399dae 1395 if (Iso639Language) {\r
255a3f33 1396 CompareLength = ISO_639_2_ENTRY_SIZE;\r
72399dae 1397 for (Index = 0; Index < AsciiStrLen (SupportedLang); Index += CompareLength) {\r
1398 if (AsciiStrnCmp (Lang, SupportedLang + Index, CompareLength) == 0) {\r
1399 //\r
1400 // Successfully find the index of Lang string in SupportedLang string.\r
1401 //\r
1402 Index = Index / CompareLength;\r
1403 return Index;\r
1404 }\r
1405 }\r
1406 ASSERT (FALSE);\r
1407 return 0;\r
1408 } else {\r
1409 //\r
0254efc0 1410 // Compare RFC4646 language code\r
72399dae 1411 //\r
255a3f33
RN
1412 Index = 0;\r
1413 for (LanguageLength = 0; Lang[LanguageLength] != '\0'; LanguageLength++);\r
1414\r
1415 for (Index = 0; *SupportedLang != '\0'; Index++, SupportedLang += CompareLength) {\r
72399dae 1416 //\r
255a3f33 1417 // Skip ';' characters in SupportedLang\r
72399dae 1418 //\r
255a3f33
RN
1419 for (; *SupportedLang != '\0' && *SupportedLang == ';'; SupportedLang++);\r
1420 //\r
1421 // Determine the length of the next language code in SupportedLang\r
1422 //\r
1423 for (CompareLength = 0; SupportedLang[CompareLength] != '\0' && SupportedLang[CompareLength] != ';'; CompareLength++);\r
fa0737a8
SZ
1424\r
1425 if ((CompareLength == LanguageLength) &&\r
255a3f33 1426 (AsciiStrnCmp (Lang, SupportedLang, CompareLength) == 0)) {\r
72399dae 1427 //\r
1428 // Successfully find the index of Lang string in SupportedLang string.\r
1429 //\r
1430 return Index;\r
1431 }\r
72399dae 1432 }\r
1433 ASSERT (FALSE);\r
1434 return 0;\r
8d3a5c82 1435 }\r
72399dae 1436}\r
33a5a666 1437\r
72399dae 1438/**\r
1439 Get language string from supported language codes according to index.\r
1440\r
8a2d4996 1441 This code is used to get corresponding language strings in supported language codes. It can handle\r
0254efc0 1442 RFC4646 and ISO639 language tags.\r
72399dae 1443 In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.\r
0254efc0 1444 In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.\r
72399dae 1445\r
1446 For example:\r
1447 SupportedLang = "engfraengfra"\r
1448 Index = "1"\r
1449 Iso639Language = TRUE\r
1450 The return value is "fra".\r
1451 Another example:\r
1452 SupportedLang = "en;fr;en-US;fr-FR"\r
1453 Index = "1"\r
1454 Iso639Language = FALSE\r
1455 The return value is "fr".\r
1456\r
1457 @param SupportedLang Platform supported language codes.\r
8a2d4996 1458 @param Index The index in supported language codes.\r
0254efc0 1459 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.\r
72399dae 1460\r
8a2d4996 1461 @retval The language string in the language codes.\r
8d3a5c82 1462\r
72399dae 1463**/\r
1464CHAR8 *\r
72399dae 1465GetLangFromSupportedLangCodes (\r
1466 IN CHAR8 *SupportedLang,\r
1467 IN UINTN Index,\r
1468 IN BOOLEAN Iso639Language\r
1469)\r
1470{\r
1471 UINTN SubIndex;\r
255a3f33 1472 UINTN CompareLength;\r
72399dae 1473 CHAR8 *Supported;\r
8d3a5c82 1474\r
72399dae 1475 SubIndex = 0;\r
1476 Supported = SupportedLang;\r
1477 if (Iso639Language) {\r
1478 //\r
8a2d4996 1479 // According to the index of Lang string in SupportedLang string to get the language.\r
1480 // This code will be invoked in RUNTIME, therefore there is not a memory allocate/free operation.\r
72399dae 1481 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.\r
1482 //\r
255a3f33
RN
1483 CompareLength = ISO_639_2_ENTRY_SIZE;\r
1484 mVariableModuleGlobal->Lang[CompareLength] = '\0';\r
72399dae 1485 return CopyMem (mVariableModuleGlobal->Lang, SupportedLang + Index * CompareLength, CompareLength);\r
f68af18e 1486\r
8d3a5c82 1487 } else {\r
72399dae 1488 while (TRUE) {\r
1489 //\r
8a2d4996 1490 // Take semicolon as delimitation, sequentially traverse supported language codes.\r
72399dae 1491 //\r
1492 for (CompareLength = 0; *Supported != ';' && *Supported != '\0'; CompareLength++) {\r
1493 Supported++;\r
1494 }\r
1495 if ((*Supported == '\0') && (SubIndex != Index)) {\r
1496 //\r
1497 // Have completed the traverse, but not find corrsponding string.\r
1498 // This case is not allowed to happen.\r
1499 //\r
1500 ASSERT(FALSE);\r
1501 return NULL;\r
1502 }\r
1503 if (SubIndex == Index) {\r
1504 //\r
8a2d4996 1505 // According to the index of Lang string in SupportedLang string to get the language.\r
72399dae 1506 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.\r
1507 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.\r
1508 //\r
255a3f33 1509 mVariableModuleGlobal->PlatformLang[CompareLength] = '\0';\r
72399dae 1510 return CopyMem (mVariableModuleGlobal->PlatformLang, Supported - CompareLength, CompareLength);\r
1511 }\r
1512 SubIndex++;\r
8a2d4996 1513\r
5c033766
RN
1514 //\r
1515 // Skip ';' characters in Supported\r
1516 //\r
1517 for (; *Supported != '\0' && *Supported == ';'; Supported++);\r
72399dae 1518 }\r
8d3a5c82 1519 }\r
8d3a5c82 1520}\r
1521\r
255a3f33 1522/**\r
fa0737a8
SZ
1523 Returns a pointer to an allocated buffer that contains the best matching language\r
1524 from a set of supported languages.\r
1525\r
1526 This function supports both ISO 639-2 and RFC 4646 language codes, but language\r
255a3f33
RN
1527 code types may not be mixed in a single call to this function. This function\r
1528 supports a variable argument list that allows the caller to pass in a prioritized\r
1529 list of language codes to test against all the language codes in SupportedLanguages.\r
1530\r
1531 If SupportedLanguages is NULL, then ASSERT().\r
1532\r
1533 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that\r
fa0737a8 1534 contains a set of language codes in the format\r
255a3f33 1535 specified by Iso639Language.\r
051bf6e0
LG
1536 @param[in] Iso639Language If not zero, then all language codes are assumed to be\r
1537 in ISO 639-2 format. If zero, then all language\r
255a3f33 1538 codes are assumed to be in RFC 4646 language format\r
fa0737a8 1539 @param[in] ... A variable argument list that contains pointers to\r
255a3f33
RN
1540 Null-terminated ASCII strings that contain one or more\r
1541 language codes in the format specified by Iso639Language.\r
1542 The first language code from each of these language\r
1543 code lists is used to determine if it is an exact or\r
fa0737a8 1544 close match to any of the language codes in\r
255a3f33
RN
1545 SupportedLanguages. Close matches only apply to RFC 4646\r
1546 language codes, and the matching algorithm from RFC 4647\r
fa0737a8 1547 is used to determine if a close match is present. If\r
255a3f33
RN
1548 an exact or close match is found, then the matching\r
1549 language code from SupportedLanguages is returned. If\r
1550 no matches are found, then the next variable argument\r
fa0737a8 1551 parameter is evaluated. The variable argument list\r
255a3f33
RN
1552 is terminated by a NULL.\r
1553\r
1554 @retval NULL The best matching language could not be found in SupportedLanguages.\r
fa0737a8 1555 @retval NULL There are not enough resources available to return the best matching\r
255a3f33 1556 language.\r
fa0737a8 1557 @retval Other A pointer to a Null-terminated ASCII string that is the best matching\r
255a3f33
RN
1558 language in SupportedLanguages.\r
1559\r
1560**/\r
1561CHAR8 *\r
e1adae60 1562EFIAPI\r
255a3f33 1563VariableGetBestLanguage (\r
fa0737a8 1564 IN CONST CHAR8 *SupportedLanguages,\r
180ac200 1565 IN UINTN Iso639Language,\r
255a3f33
RN
1566 ...\r
1567 )\r
1568{\r
1569 VA_LIST Args;\r
1570 CHAR8 *Language;\r
1571 UINTN CompareLength;\r
1572 UINTN LanguageLength;\r
1573 CONST CHAR8 *Supported;\r
1574 CHAR8 *Buffer;\r
1575\r
fa0737a8
SZ
1576 if (SupportedLanguages == NULL) {\r
1577 return NULL;\r
1578 }\r
255a3f33
RN
1579\r
1580 VA_START (Args, Iso639Language);\r
1581 while ((Language = VA_ARG (Args, CHAR8 *)) != NULL) {\r
1582 //\r
1583 // Default to ISO 639-2 mode\r
1584 //\r
1585 CompareLength = 3;\r
1586 LanguageLength = MIN (3, AsciiStrLen (Language));\r
1587\r
1588 //\r
1589 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language\r
1590 //\r
051bf6e0 1591 if (Iso639Language == 0) {\r
255a3f33
RN
1592 for (LanguageLength = 0; Language[LanguageLength] != 0 && Language[LanguageLength] != ';'; LanguageLength++);\r
1593 }\r
1594\r
1595 //\r
1596 // Trim back the length of Language used until it is empty\r
1597 //\r
1598 while (LanguageLength > 0) {\r
1599 //\r
1600 // Loop through all language codes in SupportedLanguages\r
1601 //\r
1602 for (Supported = SupportedLanguages; *Supported != '\0'; Supported += CompareLength) {\r
1603 //\r
1604 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages\r
1605 //\r
051bf6e0 1606 if (Iso639Language == 0) {\r
255a3f33
RN
1607 //\r
1608 // Skip ';' characters in Supported\r
1609 //\r
1610 for (; *Supported != '\0' && *Supported == ';'; Supported++);\r
1611 //\r
1612 // Determine the length of the next language code in Supported\r
1613 //\r
1614 for (CompareLength = 0; Supported[CompareLength] != 0 && Supported[CompareLength] != ';'; CompareLength++);\r
1615 //\r
1616 // If Language is longer than the Supported, then skip to the next language\r
1617 //\r
1618 if (LanguageLength > CompareLength) {\r
1619 continue;\r
1620 }\r
1621 }\r
1622 //\r
1623 // See if the first LanguageLength characters in Supported match Language\r
1624 //\r
1625 if (AsciiStrnCmp (Supported, Language, LanguageLength) == 0) {\r
1626 VA_END (Args);\r
1627\r
051bf6e0 1628 Buffer = (Iso639Language != 0) ? mVariableModuleGlobal->Lang : mVariableModuleGlobal->PlatformLang;\r
255a3f33
RN
1629 Buffer[CompareLength] = '\0';\r
1630 return CopyMem (Buffer, Supported, CompareLength);\r
1631 }\r
1632 }\r
1633\r
051bf6e0 1634 if (Iso639Language != 0) {\r
255a3f33
RN
1635 //\r
1636 // If ISO 639 mode, then each language can only be tested once\r
1637 //\r
1638 LanguageLength = 0;\r
1639 } else {\r
1640 //\r
fa0737a8 1641 // If RFC 4646 mode, then trim Language from the right to the next '-' character\r
255a3f33
RN
1642 //\r
1643 for (LanguageLength--; LanguageLength > 0 && Language[LanguageLength] != '-'; LanguageLength--);\r
1644 }\r
1645 }\r
1646 }\r
1647 VA_END (Args);\r
1648\r
1649 //\r
fa0737a8 1650 // No matches were found\r
255a3f33
RN
1651 //\r
1652 return NULL;\r
1653}\r
1654\r
b2bd493e
SZ
1655/**\r
1656 This function is to check if the remaining variable space is enough to set\r
1657 all Variables from argument list successfully. The purpose of the check\r
1658 is to keep the consistency of the Variables to be in variable storage.\r
1659\r
1660 Note: Variables are assumed to be in same storage.\r
1661 The set sequence of Variables will be same with the sequence of VariableEntry from argument list,\r
1662 so follow the argument sequence to check the Variables.\r
1663\r
1664 @param[in] Attributes Variable attributes for Variable entries.\r
fa0737a8
SZ
1665 @param[in] Marker VA_LIST style variable argument list.\r
1666 The variable argument list with type VARIABLE_ENTRY_CONSISTENCY *.\r
1667 A NULL terminates the list. The VariableSize of\r
9a12e582
DG
1668 VARIABLE_ENTRY_CONSISTENCY is the variable data size as input.\r
1669 It will be changed to variable total size as output.\r
b2bd493e
SZ
1670\r
1671 @retval TRUE Have enough variable space to set the Variables successfully.\r
1672 @retval FALSE No enough variable space to set the Variables successfully.\r
1673\r
1674**/\r
1675BOOLEAN\r
1676EFIAPI\r
fa0737a8 1677CheckRemainingSpaceForConsistencyInternal (\r
b2bd493e 1678 IN UINT32 Attributes,\r
fa0737a8 1679 IN VA_LIST Marker\r
b2bd493e
SZ
1680 )\r
1681{\r
1682 EFI_STATUS Status;\r
1683 VA_LIST Args;\r
1684 VARIABLE_ENTRY_CONSISTENCY *VariableEntry;\r
1685 UINT64 MaximumVariableStorageSize;\r
1686 UINT64 RemainingVariableStorageSize;\r
1687 UINT64 MaximumVariableSize;\r
1688 UINTN TotalNeededSize;\r
1689 UINTN OriginalVarSize;\r
1690 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
1691 VARIABLE_POINTER_TRACK VariablePtrTrack;\r
1692 VARIABLE_HEADER *NextVariable;\r
9a12e582
DG
1693 UINTN VarNameSize;\r
1694 UINTN VarDataSize;\r
b2bd493e
SZ
1695\r
1696 //\r
1697 // Non-Volatile related.\r
1698 //\r
1699 VariableStoreHeader = mNvVariableCache;\r
1700\r
1701 Status = VariableServiceQueryVariableInfoInternal (\r
1702 Attributes,\r
1703 &MaximumVariableStorageSize,\r
1704 &RemainingVariableStorageSize,\r
1705 &MaximumVariableSize\r
1706 );\r
1707 ASSERT_EFI_ERROR (Status);\r
1708\r
1709 TotalNeededSize = 0;\r
b7789dc6 1710 VA_COPY (Args, Marker);\r
b2bd493e
SZ
1711 VariableEntry = VA_ARG (Args, VARIABLE_ENTRY_CONSISTENCY *);\r
1712 while (VariableEntry != NULL) {\r
9a12e582
DG
1713 //\r
1714 // Calculate variable total size.\r
1715 //\r
1716 VarNameSize = StrSize (VariableEntry->Name);\r
1717 VarNameSize += GET_PAD_SIZE (VarNameSize);\r
1718 VarDataSize = VariableEntry->VariableSize;\r
1719 VarDataSize += GET_PAD_SIZE (VarDataSize);\r
fa0737a8 1720 VariableEntry->VariableSize = HEADER_ALIGN (GetVariableHeaderSize () + VarNameSize + VarDataSize);\r
9a12e582 1721\r
b2bd493e
SZ
1722 TotalNeededSize += VariableEntry->VariableSize;\r
1723 VariableEntry = VA_ARG (Args, VARIABLE_ENTRY_CONSISTENCY *);\r
1724 }\r
415700ec 1725 VA_END (Args);\r
b2bd493e
SZ
1726\r
1727 if (RemainingVariableStorageSize >= TotalNeededSize) {\r
1728 //\r
1729 // Already have enough space.\r
1730 //\r
1731 return TRUE;\r
1732 } else if (AtRuntime ()) {\r
1733 //\r
1734 // At runtime, no reclaim.\r
1735 // The original variable space of Variables can't be reused.\r
1736 //\r
1737 return FALSE;\r
1738 }\r
1739\r
b7789dc6 1740 VA_COPY (Args, Marker);\r
b2bd493e
SZ
1741 VariableEntry = VA_ARG (Args, VARIABLE_ENTRY_CONSISTENCY *);\r
1742 while (VariableEntry != NULL) {\r
1743 //\r
1744 // Check if Variable[Index] has been present and get its size.\r
1745 //\r
1746 OriginalVarSize = 0;\r
1747 VariablePtrTrack.StartPtr = GetStartPointer (VariableStoreHeader);\r
1748 VariablePtrTrack.EndPtr = GetEndPointer (VariableStoreHeader);\r
1749 Status = FindVariableEx (\r
1750 VariableEntry->Name,\r
1751 VariableEntry->Guid,\r
1752 FALSE,\r
1753 &VariablePtrTrack\r
1754 );\r
1755 if (!EFI_ERROR (Status)) {\r
1756 //\r
1757 // Get size of Variable[Index].\r
1758 //\r
1759 NextVariable = GetNextVariablePtr (VariablePtrTrack.CurrPtr);\r
1760 OriginalVarSize = (UINTN) NextVariable - (UINTN) VariablePtrTrack.CurrPtr;\r
1761 //\r
1762 // Add the original size of Variable[Index] to remaining variable storage size.\r
1763 //\r
1764 RemainingVariableStorageSize += OriginalVarSize;\r
1765 }\r
1766 if (VariableEntry->VariableSize > RemainingVariableStorageSize) {\r
1767 //\r
1768 // No enough space for Variable[Index].\r
1769 //\r
a0460be7 1770 VA_END (Args);\r
b2bd493e
SZ
1771 return FALSE;\r
1772 }\r
1773 //\r
1774 // Sub the (new) size of Variable[Index] from remaining variable storage size.\r
1775 //\r
1776 RemainingVariableStorageSize -= VariableEntry->VariableSize;\r
1777 VariableEntry = VA_ARG (Args, VARIABLE_ENTRY_CONSISTENCY *);\r
1778 }\r
415700ec 1779 VA_END (Args);\r
b2bd493e
SZ
1780\r
1781 return TRUE;\r
1782}\r
1783\r
fa0737a8
SZ
1784/**\r
1785 This function is to check if the remaining variable space is enough to set\r
1786 all Variables from argument list successfully. The purpose of the check\r
1787 is to keep the consistency of the Variables to be in variable storage.\r
1788\r
1789 Note: Variables are assumed to be in same storage.\r
1790 The set sequence of Variables will be same with the sequence of VariableEntry from argument list,\r
1791 so follow the argument sequence to check the Variables.\r
1792\r
1793 @param[in] Attributes Variable attributes for Variable entries.\r
1794 @param ... The variable argument list with type VARIABLE_ENTRY_CONSISTENCY *.\r
1795 A NULL terminates the list. The VariableSize of\r
1796 VARIABLE_ENTRY_CONSISTENCY is the variable data size as input.\r
1797 It will be changed to variable total size as output.\r
1798\r
1799 @retval TRUE Have enough variable space to set the Variables successfully.\r
1800 @retval FALSE No enough variable space to set the Variables successfully.\r
1801\r
1802**/\r
1803BOOLEAN\r
1804EFIAPI\r
1805CheckRemainingSpaceForConsistency (\r
1806 IN UINT32 Attributes,\r
1807 ...\r
1808 )\r
1809{\r
1810 VA_LIST Marker;\r
1811 BOOLEAN Return;\r
1812\r
1813 VA_START (Marker, Attributes);\r
1814\r
1815 Return = CheckRemainingSpaceForConsistencyInternal (Attributes, Marker);\r
1816\r
1817 VA_END (Marker);\r
1818\r
1819 return Return;\r
1820}\r
1821\r
72399dae 1822/**\r
1823 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.\r
052ad7e1 1824\r
72399dae 1825 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.\r
052ad7e1 1826\r
72399dae 1827 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,\r
1828 and are read-only. Therefore, in variable driver, only store the original value for other use.\r
8d3a5c82 1829\r
8a2d4996 1830 @param[in] VariableName Name of variable.\r
8d3a5c82 1831\r
8a2d4996 1832 @param[in] Data Variable data.\r
8d3a5c82 1833\r
8a2d4996 1834 @param[in] DataSize Size of data. 0 means delete.\r
72399dae 1835\r
9bc5dabb
SZ
1836 @retval EFI_SUCCESS The update operation is successful or ignored.\r
1837 @retval EFI_WRITE_PROTECTED Update PlatformLangCodes/LangCodes at runtime.\r
1838 @retval EFI_OUT_OF_RESOURCES No enough variable space to do the update operation.\r
1839 @retval Others Other errors happened during the update operation.\r
1840\r
7c80e839 1841**/\r
9bc5dabb 1842EFI_STATUS\r
d6550260 1843AutoUpdateLangVariable (\r
72399dae 1844 IN CHAR16 *VariableName,\r
1845 IN VOID *Data,\r
1846 IN UINTN DataSize\r
052ad7e1 1847 )\r
8d3a5c82 1848{\r
255a3f33
RN
1849 EFI_STATUS Status;\r
1850 CHAR8 *BestPlatformLang;\r
1851 CHAR8 *BestLang;\r
1852 UINTN Index;\r
1853 UINT32 Attributes;\r
72399dae 1854 VARIABLE_POINTER_TRACK Variable;\r
255a3f33 1855 BOOLEAN SetLanguageCodes;\r
b2bd493e 1856 VARIABLE_ENTRY_CONSISTENCY VariableEntry[2];\r
8d3a5c82 1857\r
72399dae 1858 //\r
255a3f33 1859 // Don't do updates for delete operation\r
72399dae 1860 //\r
255a3f33 1861 if (DataSize == 0) {\r
9bc5dabb 1862 return EFI_SUCCESS;\r
255a3f33
RN
1863 }\r
1864\r
1865 SetLanguageCodes = FALSE;\r
8d3a5c82 1866\r
6675a21f 1867 if (StrCmp (VariableName, EFI_PLATFORM_LANG_CODES_VARIABLE_NAME) == 0) {\r
255a3f33
RN
1868 //\r
1869 // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.\r
1870 //\r
8a2d4996 1871 if (AtRuntime ()) {\r
9bc5dabb 1872 return EFI_WRITE_PROTECTED;\r
255a3f33
RN
1873 }\r
1874\r
1875 SetLanguageCodes = TRUE;\r
1876\r
72399dae 1877 //\r
1878 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only\r
1879 // Therefore, in variable driver, only store the original value for other use.\r
1880 //\r
255a3f33
RN
1881 if (mVariableModuleGlobal->PlatformLangCodes != NULL) {\r
1882 FreePool (mVariableModuleGlobal->PlatformLangCodes);\r
1883 }\r
1884 mVariableModuleGlobal->PlatformLangCodes = AllocateRuntimeCopyPool (DataSize, Data);\r
1885 ASSERT (mVariableModuleGlobal->PlatformLangCodes != NULL);\r
1886\r
72399dae 1887 //\r
fa0737a8 1888 // PlatformLang holds a single language from PlatformLangCodes,\r
255a3f33 1889 // so the size of PlatformLangCodes is enough for the PlatformLang.\r
72399dae 1890 //\r
255a3f33
RN
1891 if (mVariableModuleGlobal->PlatformLang != NULL) {\r
1892 FreePool (mVariableModuleGlobal->PlatformLang);\r
1893 }\r
1894 mVariableModuleGlobal->PlatformLang = AllocateRuntimePool (DataSize);\r
1895 ASSERT (mVariableModuleGlobal->PlatformLang != NULL);\r
fdb7765f 1896\r
6675a21f 1897 } else if (StrCmp (VariableName, EFI_LANG_CODES_VARIABLE_NAME) == 0) {\r
72399dae 1898 //\r
255a3f33 1899 // LangCodes is a volatile variable, so it can not be updated at runtime.\r
72399dae 1900 //\r
8a2d4996 1901 if (AtRuntime ()) {\r
9bc5dabb 1902 return EFI_WRITE_PROTECTED;\r
255a3f33
RN
1903 }\r
1904\r
1905 SetLanguageCodes = TRUE;\r
8d3a5c82 1906\r
8d3a5c82 1907 //\r
255a3f33
RN
1908 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only\r
1909 // Therefore, in variable driver, only store the original value for other use.\r
8d3a5c82 1910 //\r
255a3f33
RN
1911 if (mVariableModuleGlobal->LangCodes != NULL) {\r
1912 FreePool (mVariableModuleGlobal->LangCodes);\r
1913 }\r
1914 mVariableModuleGlobal->LangCodes = AllocateRuntimeCopyPool (DataSize, Data);\r
1915 ASSERT (mVariableModuleGlobal->LangCodes != NULL);\r
1916 }\r
8d3a5c82 1917\r
fa0737a8 1918 if (SetLanguageCodes\r
255a3f33
RN
1919 && (mVariableModuleGlobal->PlatformLangCodes != NULL)\r
1920 && (mVariableModuleGlobal->LangCodes != NULL)) {\r
8d3a5c82 1921 //\r
255a3f33
RN
1922 // Update Lang if PlatformLang is already set\r
1923 // Update PlatformLang if Lang is already set\r
8d3a5c82 1924 //\r
6675a21f 1925 Status = FindVariable (EFI_PLATFORM_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
255a3f33
RN
1926 if (!EFI_ERROR (Status)) {\r
1927 //\r
1928 // Update Lang\r
1929 //\r
6675a21f 1930 VariableName = EFI_PLATFORM_LANG_VARIABLE_NAME;\r
255a3f33 1931 Data = GetVariableDataPtr (Variable.CurrPtr);\r
fa0737a8 1932 DataSize = DataSizeOfVariable (Variable.CurrPtr);\r
255a3f33 1933 } else {\r
6675a21f 1934 Status = FindVariable (EFI_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
255a3f33
RN
1935 if (!EFI_ERROR (Status)) {\r
1936 //\r
1937 // Update PlatformLang\r
1938 //\r
6675a21f 1939 VariableName = EFI_LANG_VARIABLE_NAME;\r
255a3f33 1940 Data = GetVariableDataPtr (Variable.CurrPtr);\r
fa0737a8 1941 DataSize = DataSizeOfVariable (Variable.CurrPtr);\r
255a3f33
RN
1942 } else {\r
1943 //\r
1944 // Neither PlatformLang nor Lang is set, directly return\r
1945 //\r
9bc5dabb 1946 return EFI_SUCCESS;\r
255a3f33
RN
1947 }\r
1948 }\r
1949 }\r
9bc5dabb
SZ
1950\r
1951 Status = EFI_SUCCESS;\r
1952\r
255a3f33
RN
1953 //\r
1954 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.\r
1955 //\r
1956 Attributes = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;\r
8d3a5c82 1957\r
6675a21f 1958 if (StrCmp (VariableName, EFI_PLATFORM_LANG_VARIABLE_NAME) == 0) {\r
8d3a5c82 1959 //\r
255a3f33 1960 // Update Lang when PlatformLangCodes/LangCodes were set.\r
8d3a5c82 1961 //\r
255a3f33
RN
1962 if ((mVariableModuleGlobal->PlatformLangCodes != NULL) && (mVariableModuleGlobal->LangCodes != NULL)) {\r
1963 //\r
1964 // When setting PlatformLang, firstly get most matched language string from supported language codes.\r
1965 //\r
1966 BestPlatformLang = VariableGetBestLanguage (mVariableModuleGlobal->PlatformLangCodes, FALSE, Data, NULL);\r
1967 if (BestPlatformLang != NULL) {\r
1968 //\r
1969 // Get the corresponding index in language codes.\r
1970 //\r
1971 Index = GetIndexFromSupportedLangCodes (mVariableModuleGlobal->PlatformLangCodes, BestPlatformLang, FALSE);\r
fdb7765f 1972\r
255a3f33
RN
1973 //\r
1974 // Get the corresponding ISO639 language tag according to RFC4646 language tag.\r
1975 //\r
1976 BestLang = GetLangFromSupportedLangCodes (mVariableModuleGlobal->LangCodes, Index, TRUE);\r
8d3a5c82 1977\r
255a3f33 1978 //\r
9a12e582 1979 // Check the variable space for both Lang and PlatformLang variable.\r
b2bd493e 1980 //\r
9a12e582 1981 VariableEntry[0].VariableSize = ISO_639_2_ENTRY_SIZE + 1;\r
b2bd493e
SZ
1982 VariableEntry[0].Guid = &gEfiGlobalVariableGuid;\r
1983 VariableEntry[0].Name = EFI_LANG_VARIABLE_NAME;\r
fa0737a8 1984\r
9a12e582 1985 VariableEntry[1].VariableSize = AsciiStrSize (BestPlatformLang);\r
b2bd493e
SZ
1986 VariableEntry[1].Guid = &gEfiGlobalVariableGuid;\r
1987 VariableEntry[1].Name = EFI_PLATFORM_LANG_VARIABLE_NAME;\r
1988 if (!CheckRemainingSpaceForConsistency (VARIABLE_ATTRIBUTE_NV_BS_RT, &VariableEntry[0], &VariableEntry[1], NULL)) {\r
1989 //\r
1990 // No enough variable space to set both Lang and PlatformLang successfully.\r
1991 //\r
1992 Status = EFI_OUT_OF_RESOURCES;\r
1993 } else {\r
1994 //\r
1995 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.\r
1996 //\r
1997 FindVariable (EFI_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
8d3a5c82 1998\r
b2bd493e 1999 Status = UpdateVariable (EFI_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, BestLang,\r
fa0737a8 2000 ISO_639_2_ENTRY_SIZE + 1, Attributes, 0, 0, &Variable, NULL);\r
b2bd493e 2001 }\r
8d3a5c82 2002\r
b2bd493e 2003 DEBUG ((EFI_D_INFO, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a Status: %r\n", BestPlatformLang, BestLang, Status));\r
255a3f33
RN
2004 }\r
2005 }\r
72399dae 2006\r
6675a21f 2007 } else if (StrCmp (VariableName, EFI_LANG_VARIABLE_NAME) == 0) {\r
72399dae 2008 //\r
255a3f33 2009 // Update PlatformLang when PlatformLangCodes/LangCodes were set.\r
72399dae 2010 //\r
255a3f33
RN
2011 if ((mVariableModuleGlobal->PlatformLangCodes != NULL) && (mVariableModuleGlobal->LangCodes != NULL)) {\r
2012 //\r
2013 // When setting Lang, firstly get most matched language string from supported language codes.\r
2014 //\r
2015 BestLang = VariableGetBestLanguage (mVariableModuleGlobal->LangCodes, TRUE, Data, NULL);\r
2016 if (BestLang != NULL) {\r
2017 //\r
2018 // Get the corresponding index in language codes.\r
2019 //\r
2020 Index = GetIndexFromSupportedLangCodes (mVariableModuleGlobal->LangCodes, BestLang, TRUE);\r
72399dae 2021\r
255a3f33
RN
2022 //\r
2023 // Get the corresponding RFC4646 language tag according to ISO639 language tag.\r
2024 //\r
2025 BestPlatformLang = GetLangFromSupportedLangCodes (mVariableModuleGlobal->PlatformLangCodes, Index, FALSE);\r
2026\r
2027 //\r
9a12e582 2028 // Check the variable space for both PlatformLang and Lang variable.\r
b2bd493e 2029 //\r
9a12e582 2030 VariableEntry[0].VariableSize = AsciiStrSize (BestPlatformLang);\r
b2bd493e
SZ
2031 VariableEntry[0].Guid = &gEfiGlobalVariableGuid;\r
2032 VariableEntry[0].Name = EFI_PLATFORM_LANG_VARIABLE_NAME;\r
9a12e582
DG
2033\r
2034 VariableEntry[1].VariableSize = ISO_639_2_ENTRY_SIZE + 1;\r
b2bd493e
SZ
2035 VariableEntry[1].Guid = &gEfiGlobalVariableGuid;\r
2036 VariableEntry[1].Name = EFI_LANG_VARIABLE_NAME;\r
2037 if (!CheckRemainingSpaceForConsistency (VARIABLE_ATTRIBUTE_NV_BS_RT, &VariableEntry[0], &VariableEntry[1], NULL)) {\r
2038 //\r
2039 // No enough variable space to set both PlatformLang and Lang successfully.\r
2040 //\r
2041 Status = EFI_OUT_OF_RESOURCES;\r
2042 } else {\r
2043 //\r
2044 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.\r
2045 //\r
2046 FindVariable (EFI_PLATFORM_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
72399dae 2047\r
fa0737a8
SZ
2048 Status = UpdateVariable (EFI_PLATFORM_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, BestPlatformLang,\r
2049 AsciiStrSize (BestPlatformLang), Attributes, 0, 0, &Variable, NULL);\r
b2bd493e 2050 }\r
72399dae 2051\r
9bc5dabb 2052 DEBUG ((EFI_D_INFO, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a Status: %r\n", BestLang, BestPlatformLang, Status));\r
255a3f33
RN
2053 }\r
2054 }\r
72399dae 2055 }\r
9bc5dabb 2056\r
b2bd493e
SZ
2057 if (SetLanguageCodes) {\r
2058 //\r
2059 // Continue to set PlatformLangCodes or LangCodes.\r
2060 //\r
2061 return EFI_SUCCESS;\r
2062 } else {\r
2063 return Status;\r
2064 }\r
8d3a5c82 2065}\r
2066\r
7c80e839 2067/**\r
fa0737a8
SZ
2068 Compare two EFI_TIME data.\r
2069\r
2070\r
2071 @param FirstTime A pointer to the first EFI_TIME data.\r
2072 @param SecondTime A pointer to the second EFI_TIME data.\r
2073\r
2074 @retval TRUE The FirstTime is not later than the SecondTime.\r
2075 @retval FALSE The FirstTime is later than the SecondTime.\r
2076\r
2077**/\r
2078BOOLEAN\r
2079VariableCompareTimeStampInternal (\r
2080 IN EFI_TIME *FirstTime,\r
2081 IN EFI_TIME *SecondTime\r
2082 )\r
2083{\r
2084 if (FirstTime->Year != SecondTime->Year) {\r
2085 return (BOOLEAN) (FirstTime->Year < SecondTime->Year);\r
2086 } else if (FirstTime->Month != SecondTime->Month) {\r
2087 return (BOOLEAN) (FirstTime->Month < SecondTime->Month);\r
2088 } else if (FirstTime->Day != SecondTime->Day) {\r
2089 return (BOOLEAN) (FirstTime->Day < SecondTime->Day);\r
2090 } else if (FirstTime->Hour != SecondTime->Hour) {\r
2091 return (BOOLEAN) (FirstTime->Hour < SecondTime->Hour);\r
2092 } else if (FirstTime->Minute != SecondTime->Minute) {\r
2093 return (BOOLEAN) (FirstTime->Minute < SecondTime->Minute);\r
2094 }\r
2095\r
2096 return (BOOLEAN) (FirstTime->Second <= SecondTime->Second);\r
2097}\r
2098\r
2099/**\r
2100 Update the variable region with Variable information. If EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is set,\r
2101 index of associated public key is needed.\r
052ad7e1 2102\r
8a2d4996 2103 @param[in] VariableName Name of variable.\r
2104 @param[in] VendorGuid Guid of variable.\r
2105 @param[in] Data Variable data.\r
2106 @param[in] DataSize Size of data. 0 means delete.\r
fa0737a8
SZ
2107 @param[in] Attributes Attributes of the variable.\r
2108 @param[in] KeyIndex Index of associated public key.\r
2109 @param[in] MonotonicCount Value of associated monotonic count.\r
23b06935 2110 @param[in, out] CacheVariable The variable information which is used to keep track of variable usage.\r
fa0737a8
SZ
2111 @param[in] TimeStamp Value of associated TimeStamp.\r
2112\r
72399dae 2113 @retval EFI_SUCCESS The update operation is success.\r
72399dae 2114 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.\r
8d3a5c82 2115\r
7c80e839 2116**/\r
052ad7e1 2117EFI_STATUS\r
72399dae 2118UpdateVariable (\r
8a2d4996 2119 IN CHAR16 *VariableName,\r
2120 IN EFI_GUID *VendorGuid,\r
2121 IN VOID *Data,\r
2122 IN UINTN DataSize,\r
2123 IN UINT32 Attributes OPTIONAL,\r
fa0737a8
SZ
2124 IN UINT32 KeyIndex OPTIONAL,\r
2125 IN UINT64 MonotonicCount OPTIONAL,\r
2126 IN OUT VARIABLE_POINTER_TRACK *CacheVariable,\r
2127 IN EFI_TIME *TimeStamp OPTIONAL\r
052ad7e1 2128 )\r
8d3a5c82 2129{\r
8a9e0b72 2130 EFI_STATUS Status;\r
2131 VARIABLE_HEADER *NextVariable;\r
72399dae 2132 UINTN ScratchSize;\r
fa0737a8 2133 UINTN MaxDataSize;\r
8a9e0b72 2134 UINTN VarNameOffset;\r
2135 UINTN VarDataOffset;\r
72399dae 2136 UINTN VarNameSize;\r
8a9e0b72 2137 UINTN VarSize;\r
72399dae 2138 BOOLEAN Volatile;\r
2139 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r
8a9e0b72 2140 UINT8 State;\r
8a2d4996 2141 VARIABLE_POINTER_TRACK *Variable;\r
2142 VARIABLE_POINTER_TRACK NvVariable;\r
2143 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
2144 UINTN CacheOffset;\r
fa0737a8
SZ
2145 UINT8 *BufferForMerge;\r
2146 UINTN MergedBufSize;\r
2147 BOOLEAN DataReady;\r
2148 UINTN DataOffset;\r
4edb1866
SZ
2149 BOOLEAN IsCommonVariable;\r
2150 BOOLEAN IsCommonUserVariable;\r
fa0737a8 2151 AUTHENTICATED_VARIABLE_HEADER *AuthVariable;\r
fdb7765f 2152\r
fa0737a8 2153 if (mVariableModuleGlobal->FvbInstance == NULL) {\r
5456306f 2154 //\r
fa0737a8 2155 // The FVB protocol is not ready, so the EFI_VARIABLE_WRITE_ARCH_PROTOCOL is not installed.\r
5456306f 2156 //\r
fa0737a8
SZ
2157 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
2158 //\r
2159 // Trying to update NV variable prior to the installation of EFI_VARIABLE_WRITE_ARCH_PROTOCOL\r
2160 //\r
2161 DEBUG ((EFI_D_ERROR, "Update NV variable before EFI_VARIABLE_WRITE_ARCH_PROTOCOL ready - %r\n", EFI_NOT_AVAILABLE_YET));\r
2162 return EFI_NOT_AVAILABLE_YET;\r
2163 } else if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {\r
2164 //\r
2165 // Trying to update volatile authenticated variable prior to the installation of EFI_VARIABLE_WRITE_ARCH_PROTOCOL\r
2166 // The authenticated variable perhaps is not initialized, just return here.\r
2167 //\r
2168 DEBUG ((EFI_D_ERROR, "Update AUTH variable before EFI_VARIABLE_WRITE_ARCH_PROTOCOL ready - %r\n", EFI_NOT_AVAILABLE_YET));\r
2169 return EFI_NOT_AVAILABLE_YET;\r
2170 }\r
5456306f 2171 }\r
2172\r
31349131
SZ
2173 //\r
2174 // Check if CacheVariable points to the variable in variable HOB.\r
2175 // If yes, let CacheVariable points to the variable in NV variable cache.\r
2176 //\r
2177 if ((CacheVariable->CurrPtr != NULL) &&\r
2178 (mVariableModuleGlobal->VariableGlobal.HobVariableBase != 0) &&\r
2179 (CacheVariable->StartPtr == GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase))\r
2180 ) {\r
2181 CacheVariable->StartPtr = GetStartPointer (mNvVariableCache);\r
2182 CacheVariable->EndPtr = GetEndPointer (mNvVariableCache);\r
2183 CacheVariable->Volatile = FALSE;\r
2184 Status = FindVariableEx (VariableName, VendorGuid, FALSE, CacheVariable);\r
2185 if (CacheVariable->CurrPtr == NULL || EFI_ERROR (Status)) {\r
2186 //\r
2187 // There is no matched variable in NV variable cache.\r
2188 //\r
2189 if ((((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0) && (DataSize == 0)) || (Attributes == 0)) {\r
2190 //\r
2191 // It is to delete variable,\r
2192 // go to delete this variable in variable HOB and\r
2193 // try to flush other variables from HOB to flash.\r
2194 //\r
38eb83b8 2195 UpdateVariableInfo (VariableName, VendorGuid, FALSE, FALSE, FALSE, TRUE, FALSE);\r
31349131
SZ
2196 FlushHobVariableToFlash (VariableName, VendorGuid);\r
2197 return EFI_SUCCESS;\r
2198 }\r
2199 }\r
2200 }\r
2201\r
5456306f 2202 if ((CacheVariable->CurrPtr == NULL) || CacheVariable->Volatile) {\r
8a2d4996 2203 Variable = CacheVariable;\r
2204 } else {\r
8a2d4996 2205 //\r
5456306f 2206 // Update/Delete existing NV variable.\r
8a2d4996 2207 // CacheVariable points to the variable in the memory copy of Flash area\r
2208 // Now let Variable points to the same variable in Flash area.\r
2209 //\r
2210 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);\r
fa0737a8 2211 Variable = &NvVariable;\r
8a2d4996 2212 Variable->StartPtr = GetStartPointer (VariableStoreHeader);\r
9b18845a
DL
2213 Variable->EndPtr = (VARIABLE_HEADER *)((UINTN)Variable->StartPtr + ((UINTN)CacheVariable->EndPtr - (UINTN)CacheVariable->StartPtr));\r
2214\r
5456306f 2215 Variable->CurrPtr = (VARIABLE_HEADER *)((UINTN)Variable->StartPtr + ((UINTN)CacheVariable->CurrPtr - (UINTN)CacheVariable->StartPtr));\r
23b06935
SZ
2216 if (CacheVariable->InDeletedTransitionPtr != NULL) {\r
2217 Variable->InDeletedTransitionPtr = (VARIABLE_HEADER *)((UINTN)Variable->StartPtr + ((UINTN)CacheVariable->InDeletedTransitionPtr - (UINTN)CacheVariable->StartPtr));\r
2218 } else {\r
2219 Variable->InDeletedTransitionPtr = NULL;\r
2220 }\r
5456306f 2221 Variable->Volatile = FALSE;\r
fa0737a8 2222 }\r
5456306f 2223\r
2224 Fvb = mVariableModuleGlobal->FvbInstance;\r
fdb7765f 2225\r
fa0737a8
SZ
2226 //\r
2227 // Tricky part: Use scratch data area at the end of volatile variable store\r
2228 // as a temporary storage.\r
2229 //\r
2230 NextVariable = GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase));\r
2231 ScratchSize = mVariableModuleGlobal->ScratchBufferSize;\r
2232 SetMem (NextVariable, ScratchSize, 0xff);\r
2233 DataReady = FALSE;\r
2234\r
72399dae 2235 if (Variable->CurrPtr != NULL) {\r
8d3a5c82 2236 //\r
8a2d4996 2237 // Update/Delete existing variable.\r
8d3a5c82 2238 //\r
fa0737a8 2239 if (AtRuntime ()) {\r
c6492839 2240 //\r
fa0737a8
SZ
2241 // If AtRuntime and the variable is Volatile and Runtime Access,\r
2242 // the volatile is ReadOnly, and SetVariable should be aborted and\r
c6492839 2243 // return EFI_WRITE_PROTECTED.\r
2244 //\r
72399dae 2245 if (Variable->Volatile) {\r
c6492839 2246 Status = EFI_WRITE_PROTECTED;\r
2247 goto Done;\r
2248 }\r
2249 //\r
fa0737a8
SZ
2250 // Only variable that have NV attributes can be updated/deleted in Runtime.\r
2251 //\r
9b18845a 2252 if ((CacheVariable->CurrPtr->Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r
fa0737a8
SZ
2253 Status = EFI_INVALID_PARAMETER;\r
2254 goto Done;\r
2255 }\r
2256\r
2257 //\r
2258 // Only variable that have RT attributes can be updated/deleted in Runtime.\r
c6492839 2259 //\r
9b18845a 2260 if ((CacheVariable->CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) {\r
c6492839 2261 Status = EFI_INVALID_PARAMETER;\r
fa0737a8 2262 goto Done;\r
c6492839 2263 }\r
2264 }\r
8a2d4996 2265\r
8d3a5c82 2266 //\r
c6492839 2267 // Setting a data variable with no access, or zero DataSize attributes\r
8a2d4996 2268 // causes it to be deleted.\r
fa0737a8
SZ
2269 // When the EFI_VARIABLE_APPEND_WRITE attribute is set, DataSize of zero will\r
2270 // not delete the variable.\r
8d3a5c82 2271 //\r
fa0737a8 2272 if ((((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0) && (DataSize == 0))|| ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0)) {\r
23b06935
SZ
2273 if (Variable->InDeletedTransitionPtr != NULL) {\r
2274 //\r
2275 // Both ADDED and IN_DELETED_TRANSITION variable are present,\r
2276 // set IN_DELETED_TRANSITION one to DELETED state first.\r
2277 //\r
ae658d91 2278 ASSERT (CacheVariable->InDeletedTransitionPtr != NULL);\r
9b18845a 2279 State = CacheVariable->InDeletedTransitionPtr->State;\r
23b06935
SZ
2280 State &= VAR_DELETED;\r
2281 Status = UpdateVariableStore (\r
2282 &mVariableModuleGlobal->VariableGlobal,\r
2283 Variable->Volatile,\r
2284 FALSE,\r
2285 Fvb,\r
2286 (UINTN) &Variable->InDeletedTransitionPtr->State,\r
2287 sizeof (UINT8),\r
2288 &State\r
2289 );\r
2290 if (!EFI_ERROR (Status)) {\r
2291 if (!Variable->Volatile) {\r
2292 CacheVariable->InDeletedTransitionPtr->State = State;\r
2293 }\r
2294 } else {\r
2295 goto Done;\r
2296 }\r
2297 }\r
2298\r
9b18845a 2299 State = CacheVariable->CurrPtr->State;\r
c6492839 2300 State &= VAR_DELETED;\r
2301\r
2302 Status = UpdateVariableStore (\r
052ad7e1 2303 &mVariableModuleGlobal->VariableGlobal,\r
72399dae 2304 Variable->Volatile,\r
c6492839 2305 FALSE,\r
8a9e0b72 2306 Fvb,\r
72399dae 2307 (UINTN) &Variable->CurrPtr->State,\r
c6492839 2308 sizeof (UINT8),\r
2309 &State\r
fa0737a8 2310 );\r
33a5a666 2311 if (!EFI_ERROR (Status)) {\r
8a2d4996 2312 UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, FALSE, TRUE, FALSE);\r
2313 if (!Variable->Volatile) {\r
2314 CacheVariable->CurrPtr->State = State;\r
335e2681 2315 FlushHobVariableToFlash (VariableName, VendorGuid);\r
8a2d4996 2316 }\r
33a5a666 2317 }\r
fa0737a8 2318 goto Done;\r
c6492839 2319 }\r
8d3a5c82 2320 //\r
8a2d4996 2321 // If the variable is marked valid, and the same data has been passed in,\r
c6492839 2322 // then return to the caller immediately.\r
8d3a5c82 2323 //\r
9b18845a
DL
2324 if (DataSizeOfVariable (CacheVariable->CurrPtr) == DataSize &&\r
2325 (CompareMem (Data, GetVariableDataPtr (CacheVariable->CurrPtr), DataSize) == 0) &&\r
fa0737a8
SZ
2326 ((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0) &&\r
2327 (TimeStamp == NULL)) {\r
2328 //\r
2329 // Variable content unchanged and no need to update timestamp, just return.\r
2330 //\r
8a2d4996 2331 UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, TRUE, FALSE, FALSE);\r
c6492839 2332 Status = EFI_SUCCESS;\r
2333 goto Done;\r
9b18845a
DL
2334 } else if ((CacheVariable->CurrPtr->State == VAR_ADDED) ||\r
2335 (CacheVariable->CurrPtr->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {\r
814bae52 2336\r
fa0737a8
SZ
2337 //\r
2338 // EFI_VARIABLE_APPEND_WRITE attribute only effects for existing variable.\r
2339 //\r
2340 if ((Attributes & EFI_VARIABLE_APPEND_WRITE) != 0) {\r
2341 //\r
2342 // NOTE: From 0 to DataOffset of NextVariable is reserved for Variable Header and Name.\r
2343 // From DataOffset of NextVariable is to save the existing variable data.\r
2344 //\r
9b18845a 2345 DataOffset = GetVariableDataOffset (CacheVariable->CurrPtr);\r
fa0737a8 2346 BufferForMerge = (UINT8 *) ((UINTN) NextVariable + DataOffset);\r
9b18845a 2347 CopyMem (BufferForMerge, (UINT8 *) ((UINTN) CacheVariable->CurrPtr + DataOffset), DataSizeOfVariable (CacheVariable->CurrPtr));\r
fa0737a8
SZ
2348\r
2349 //\r
9b4a2032 2350 // Set Max Auth/Non-Volatile/Volatile Variable Data Size as default MaxDataSize.\r
fa0737a8
SZ
2351 //\r
2352 if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {\r
2353 MaxDataSize = mVariableModuleGlobal->MaxAuthVariableSize - DataOffset;\r
9b4a2032 2354 } else if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
fa0737a8 2355 MaxDataSize = mVariableModuleGlobal->MaxVariableSize - DataOffset;\r
9b4a2032
LE
2356 } else {\r
2357 MaxDataSize = mVariableModuleGlobal->MaxVolatileVariableSize - DataOffset;\r
fa0737a8
SZ
2358 }\r
2359\r
2360 //\r
2361 // Append the new data to the end of existing data.\r
2362 // Max Harware error record variable data size is different from common/auth variable.\r
2363 //\r
2364 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2365 MaxDataSize = PcdGet32 (PcdMaxHardwareErrorVariableSize) - DataOffset;\r
2366 }\r
2367\r
9b18845a 2368 if (DataSizeOfVariable (CacheVariable->CurrPtr) + DataSize > MaxDataSize) {\r
fa0737a8
SZ
2369 //\r
2370 // Existing data size + new data size exceed maximum variable size limitation.\r
2371 //\r
2372 Status = EFI_INVALID_PARAMETER;\r
2373 goto Done;\r
2374 }\r
9b18845a
DL
2375 CopyMem ((UINT8*) ((UINTN) BufferForMerge + DataSizeOfVariable (CacheVariable->CurrPtr)), Data, DataSize);\r
2376 MergedBufSize = DataSizeOfVariable (CacheVariable->CurrPtr) + DataSize;\r
fa0737a8
SZ
2377\r
2378 //\r
2379 // BufferForMerge(from DataOffset of NextVariable) has included the merged existing and new data.\r
2380 //\r
2381 Data = BufferForMerge;\r
2382 DataSize = MergedBufSize;\r
2383 DataReady = TRUE;\r
2384 }\r
2385\r
c6492839 2386 //\r
8a2d4996 2387 // Mark the old variable as in delete transition.\r
c6492839 2388 //\r
9b18845a 2389 State = CacheVariable->CurrPtr->State;\r
c6492839 2390 State &= VAR_IN_DELETED_TRANSITION;\r
2391\r
2392 Status = UpdateVariableStore (\r
052ad7e1 2393 &mVariableModuleGlobal->VariableGlobal,\r
72399dae 2394 Variable->Volatile,\r
c6492839 2395 FALSE,\r
8a9e0b72 2396 Fvb,\r
72399dae 2397 (UINTN) &Variable->CurrPtr->State,\r
c6492839 2398 sizeof (UINT8),\r
2399 &State\r
fa0737a8 2400 );\r
c6492839 2401 if (EFI_ERROR (Status)) {\r
fa0737a8
SZ
2402 goto Done;\r
2403 }\r
8a2d4996 2404 if (!Variable->Volatile) {\r
2405 CacheVariable->CurrPtr->State = State;\r
2406 }\r
fa0737a8 2407 }\r
72399dae 2408 } else {\r
8d3a5c82 2409 //\r
8a2d4996 2410 // Not found existing variable. Create a new variable.\r
fa0737a8
SZ
2411 //\r
2412\r
2413 if ((DataSize == 0) && ((Attributes & EFI_VARIABLE_APPEND_WRITE) != 0)) {\r
2414 Status = EFI_SUCCESS;\r
2415 goto Done;\r
2416 }\r
2417\r
8d3a5c82 2418 //\r
c6492839 2419 // Make sure we are trying to create a new variable.\r
fa0737a8 2420 // Setting a data variable with zero DataSize or no access attributes means to delete it.\r
8d3a5c82 2421 //\r
c6492839 2422 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {\r
2423 Status = EFI_NOT_FOUND;\r
2424 goto Done;\r
2425 }\r
fa0737a8 2426\r
8d3a5c82 2427 //\r
8a2d4996 2428 // Only variable have NV|RT attribute can be created in Runtime.\r
c6492839 2429 //\r
8a2d4996 2430 if (AtRuntime () &&\r
45f6c85b 2431 (((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) || ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0))) {\r
c6492839 2432 Status = EFI_INVALID_PARAMETER;\r
2433 goto Done;\r
fa0737a8 2434 }\r
c6492839 2435 }\r
2436\r
2437 //\r
2438 // Function part - create a new variable and copy the data.\r
2439 // Both update a variable and create a variable will come here.\r
c6492839 2440 //\r
c6492839 2441 NextVariable->StartId = VARIABLE_DATA;\r
c6492839 2442 //\r
2443 // NextVariable->State = VAR_ADDED;\r
2444 //\r
8a2d4996 2445 NextVariable->Reserved = 0;\r
fa0737a8
SZ
2446 if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {\r
2447 AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) NextVariable;\r
2448 AuthVariable->PubKeyIndex = KeyIndex;\r
2449 AuthVariable->MonotonicCount = MonotonicCount;\r
2450 ZeroMem (&AuthVariable->TimeStamp, sizeof (EFI_TIME));\r
2451\r
2452 if (((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) != 0) &&\r
2453 (TimeStamp != NULL)) {\r
2454 if ((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0) {\r
2455 CopyMem (&AuthVariable->TimeStamp, TimeStamp, sizeof (EFI_TIME));\r
2456 } else {\r
2457 //\r
2458 // In the case when the EFI_VARIABLE_APPEND_WRITE attribute is set, only\r
2459 // when the new TimeStamp value is later than the current timestamp associated\r
2460 // with the variable, we need associate the new timestamp with the updated value.\r
2461 //\r
2462 if (Variable->CurrPtr != NULL) {\r
9b18845a 2463 if (VariableCompareTimeStampInternal (&(((AUTHENTICATED_VARIABLE_HEADER *) CacheVariable->CurrPtr)->TimeStamp), TimeStamp)) {\r
fa0737a8
SZ
2464 CopyMem (&AuthVariable->TimeStamp, TimeStamp, sizeof (EFI_TIME));\r
2465 }\r
2466 }\r
2467 }\r
2468 }\r
2469 }\r
2470\r
2471 //\r
2472 // The EFI_VARIABLE_APPEND_WRITE attribute will never be set in the returned\r
2473 // Attributes bitmask parameter of a GetVariable() call.\r
2474 //\r
2475 NextVariable->Attributes = Attributes & (~EFI_VARIABLE_APPEND_WRITE);\r
2476\r
2477 VarNameOffset = GetVariableHeaderSize ();\r
8a2d4996 2478 VarNameSize = StrSize (VariableName);\r
c6492839 2479 CopyMem (\r
2480 (UINT8 *) ((UINTN) NextVariable + VarNameOffset),\r
2481 VariableName,\r
2482 VarNameSize\r
2483 );\r
2484 VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);\r
fa0737a8
SZ
2485\r
2486 //\r
2487 // If DataReady is TRUE, it means the variable data has been saved into\r
2488 // NextVariable during EFI_VARIABLE_APPEND_WRITE operation preparation.\r
2489 //\r
2490 if (!DataReady) {\r
2491 CopyMem (\r
2492 (UINT8 *) ((UINTN) NextVariable + VarDataOffset),\r
2493 Data,\r
2494 DataSize\r
2495 );\r
2496 }\r
2497\r
2498 CopyMem (GetVendorGuidPtr (NextVariable), VendorGuid, sizeof (EFI_GUID));\r
c6492839 2499 //\r
2500 // There will be pad bytes after Data, the NextVariable->NameSize and\r
2501 // NextVariable->DataSize should not include pad size so that variable\r
8a2d4996 2502 // service can get actual size in GetVariable.\r
c6492839 2503 //\r
fa0737a8
SZ
2504 SetNameSizeOfVariable (NextVariable, VarNameSize);\r
2505 SetDataSizeOfVariable (NextVariable, DataSize);\r
c6492839 2506\r
2507 //\r
2508 // The actual size of the variable that stores in storage should\r
2509 // include pad size.\r
2510 //\r
2511 VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);\r
45f6c85b 2512 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
8d3a5c82 2513 //\r
8a2d4996 2514 // Create a nonvolatile variable.\r
8d3a5c82 2515 //\r
fd51bf70 2516 Volatile = FALSE;\r
4edb1866
SZ
2517\r
2518 IsCommonVariable = FALSE;\r
2519 IsCommonUserVariable = FALSE;\r
2520 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0) {\r
2521 IsCommonVariable = TRUE;\r
2522 IsCommonUserVariable = IsUserVariable (NextVariable);\r
2523 }\r
2524 if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0)\r
188e4e84 2525 && ((VarSize + mVariableModuleGlobal->HwErrVariableTotalSize) > PcdGet32 (PcdHwErrStorageSize)))\r
4edb1866
SZ
2526 || (IsCommonVariable && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > mVariableModuleGlobal->CommonVariableSpace))\r
2527 || (IsCommonVariable && AtRuntime () && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > mVariableModuleGlobal->CommonRuntimeVariableSpace))\r
2528 || (IsCommonUserVariable && ((VarSize + mVariableModuleGlobal->CommonUserVariableTotalSize) > mVariableModuleGlobal->CommonMaxUserVariableSpace))) {\r
8a2d4996 2529 if (AtRuntime ()) {\r
4edb1866
SZ
2530 if (IsCommonUserVariable && ((VarSize + mVariableModuleGlobal->CommonUserVariableTotalSize) > mVariableModuleGlobal->CommonMaxUserVariableSpace)) {\r
2531 RecordVarErrorFlag (VAR_ERROR_FLAG_USER_ERROR, VariableName, VendorGuid, Attributes, VarSize);\r
2532 }\r
2533 if (IsCommonVariable && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > mVariableModuleGlobal->CommonRuntimeVariableSpace)) {\r
2534 RecordVarErrorFlag (VAR_ERROR_FLAG_SYSTEM_ERROR, VariableName, VendorGuid, Attributes, VarSize);\r
2535 }\r
c6492839 2536 Status = EFI_OUT_OF_RESOURCES;\r
2537 goto Done;\r
2538 }\r
2539 //\r
7baf3c69 2540 // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.\r
c6492839 2541 //\r
fa0737a8
SZ
2542 Status = Reclaim (\r
2543 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
2544 &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
2545 FALSE,\r
2546 Variable,\r
2547 NextVariable,\r
2548 HEADER_ALIGN (VarSize)\r
2549 );\r
7baf3c69
SZ
2550 if (!EFI_ERROR (Status)) {\r
2551 //\r
2552 // The new variable has been integrated successfully during reclaiming.\r
2553 //\r
2554 if (Variable->CurrPtr != NULL) {\r
2555 CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN) CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr));\r
2556 CacheVariable->InDeletedTransitionPtr = NULL;\r
2557 }\r
2558 UpdateVariableInfo (VariableName, VendorGuid, FALSE, FALSE, TRUE, FALSE, FALSE);\r
2559 FlushHobVariableToFlash (VariableName, VendorGuid);\r
4edb1866
SZ
2560 } else {\r
2561 if (IsCommonUserVariable && ((VarSize + mVariableModuleGlobal->CommonUserVariableTotalSize) > mVariableModuleGlobal->CommonMaxUserVariableSpace)) {\r
2562 RecordVarErrorFlag (VAR_ERROR_FLAG_USER_ERROR, VariableName, VendorGuid, Attributes, VarSize);\r
2563 }\r
2564 if (IsCommonVariable && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > mVariableModuleGlobal->CommonVariableSpace)) {\r
2565 RecordVarErrorFlag (VAR_ERROR_FLAG_SYSTEM_ERROR, VariableName, VendorGuid, Attributes, VarSize);\r
2566 }\r
23b06935 2567 }\r
7baf3c69 2568 goto Done;\r
8d3a5c82 2569 }\r
2570 //\r
8a2d4996 2571 // Four steps\r
c6492839 2572 // 1. Write variable header\r
fa0737a8 2573 // 2. Set variable state to header valid\r
130e2569 2574 // 3. Write variable data\r
2575 // 4. Set variable state to valid\r
8d3a5c82 2576 //\r
8d3a5c82 2577 //\r
c6492839 2578 // Step 1:\r
8d3a5c82 2579 //\r
8a2d4996 2580 CacheOffset = mVariableModuleGlobal->NonVolatileLastVariableOffset;\r
c6492839 2581 Status = UpdateVariableStore (\r
052ad7e1 2582 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 2583 FALSE,\r
2584 TRUE,\r
8a9e0b72 2585 Fvb,\r
72399dae 2586 mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
fa0737a8 2587 (UINT32) GetVariableHeaderSize (),\r
c6492839 2588 (UINT8 *) NextVariable\r
2589 );\r
2590\r
2591 if (EFI_ERROR (Status)) {\r
2592 goto Done;\r
2593 }\r
130e2569 2594\r
8d3a5c82 2595 //\r
c6492839 2596 // Step 2:\r
8d3a5c82 2597 //\r
130e2569 2598 NextVariable->State = VAR_HEADER_VALID_ONLY;\r
2599 Status = UpdateVariableStore (\r
2600 &mVariableModuleGlobal->VariableGlobal,\r
2601 FALSE,\r
2602 TRUE,\r
8a9e0b72 2603 Fvb,\r
8a2d4996 2604 mVariableModuleGlobal->NonVolatileLastVariableOffset + OFFSET_OF (VARIABLE_HEADER, State),\r
2605 sizeof (UINT8),\r
2606 &NextVariable->State\r
130e2569 2607 );\r
2608\r
2609 if (EFI_ERROR (Status)) {\r
2610 goto Done;\r
2611 }\r
2612 //\r
2613 // Step 3:\r
2614 //\r
c6492839 2615 Status = UpdateVariableStore (\r
052ad7e1 2616 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 2617 FALSE,\r
2618 TRUE,\r
8a9e0b72 2619 Fvb,\r
fa0737a8
SZ
2620 mVariableModuleGlobal->NonVolatileLastVariableOffset + GetVariableHeaderSize (),\r
2621 (UINT32) (VarSize - GetVariableHeaderSize ()),\r
2622 (UINT8 *) NextVariable + GetVariableHeaderSize ()\r
c6492839 2623 );\r
2624\r
2625 if (EFI_ERROR (Status)) {\r
2626 goto Done;\r
2627 }\r
8d3a5c82 2628 //\r
130e2569 2629 // Step 4:\r
8d3a5c82 2630 //\r
c6492839 2631 NextVariable->State = VAR_ADDED;\r
2632 Status = UpdateVariableStore (\r
052ad7e1 2633 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 2634 FALSE,\r
2635 TRUE,\r
8a9e0b72 2636 Fvb,\r
8a2d4996 2637 mVariableModuleGlobal->NonVolatileLastVariableOffset + OFFSET_OF (VARIABLE_HEADER, State),\r
2638 sizeof (UINT8),\r
2639 &NextVariable->State\r
c6492839 2640 );\r
2641\r
2642 if (EFI_ERROR (Status)) {\r
2643 goto Done;\r
2644 }\r
8d3a5c82 2645\r
72399dae 2646 mVariableModuleGlobal->NonVolatileLastVariableOffset += HEADER_ALIGN (VarSize);\r
8d3a5c82 2647\r
2fcdca1d 2648 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {\r
2649 mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VarSize);\r
2650 } else {\r
2651 mVariableModuleGlobal->CommonVariableTotalSize += HEADER_ALIGN (Va