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