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