]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
This revision can only work with Intel(c) UDK Debugger Tool version 1.3 or greater...
[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
1499 CacheVariable->InDeletedTransitionPtr->State = State;\r
1500 }\r
1501 } else {\r
1502 goto Done;\r
1503 }\r
1504 }\r
1505\r
72399dae 1506 State = Variable->CurrPtr->State;\r
c6492839 1507 State &= VAR_DELETED;\r
1508\r
1509 Status = UpdateVariableStore (\r
052ad7e1 1510 &mVariableModuleGlobal->VariableGlobal,\r
72399dae 1511 Variable->Volatile,\r
c6492839 1512 FALSE,\r
8a9e0b72 1513 Fvb,\r
72399dae 1514 (UINTN) &Variable->CurrPtr->State,\r
c6492839 1515 sizeof (UINT8),\r
1516 &State\r
1517 ); \r
33a5a666 1518 if (!EFI_ERROR (Status)) {\r
8a2d4996 1519 UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, FALSE, TRUE, FALSE);\r
1520 if (!Variable->Volatile) {\r
1521 CacheVariable->CurrPtr->State = State;\r
335e2681 1522 FlushHobVariableToFlash (VariableName, VendorGuid);\r
8a2d4996 1523 }\r
33a5a666 1524 }\r
c6492839 1525 goto Done; \r
1526 }\r
8d3a5c82 1527 //\r
8a2d4996 1528 // If the variable is marked valid, and the same data has been passed in,\r
c6492839 1529 // then return to the caller immediately.\r
8d3a5c82 1530 //\r
72399dae 1531 if (DataSizeOfVariable (Variable->CurrPtr) == DataSize &&\r
1532 (CompareMem (Data, GetVariableDataPtr (Variable->CurrPtr), DataSize) == 0)) {\r
33a5a666 1533 \r
8a2d4996 1534 UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, TRUE, FALSE, FALSE);\r
c6492839 1535 Status = EFI_SUCCESS;\r
1536 goto Done;\r
72399dae 1537 } else if ((Variable->CurrPtr->State == VAR_ADDED) ||\r
1538 (Variable->CurrPtr->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {\r
814bae52 1539\r
c6492839 1540 //\r
8a2d4996 1541 // Mark the old variable as in delete transition.\r
c6492839 1542 //\r
72399dae 1543 State = Variable->CurrPtr->State;\r
c6492839 1544 State &= VAR_IN_DELETED_TRANSITION;\r
1545\r
1546 Status = UpdateVariableStore (\r
052ad7e1 1547 &mVariableModuleGlobal->VariableGlobal,\r
72399dae 1548 Variable->Volatile,\r
c6492839 1549 FALSE,\r
8a9e0b72 1550 Fvb,\r
72399dae 1551 (UINTN) &Variable->CurrPtr->State,\r
c6492839 1552 sizeof (UINT8),\r
1553 &State\r
1554 ); \r
1555 if (EFI_ERROR (Status)) {\r
1556 goto Done; \r
33a5a666 1557 } \r
8a2d4996 1558 if (!Variable->Volatile) {\r
1559 CacheVariable->CurrPtr->State = State;\r
1560 }\r
c6492839 1561 } \r
72399dae 1562 } else {\r
8d3a5c82 1563 //\r
8a2d4996 1564 // Not found existing variable. Create a new variable.\r
c6492839 1565 // \r
1566 \r
8d3a5c82 1567 //\r
c6492839 1568 // Make sure we are trying to create a new variable.\r
8a2d4996 1569 // Setting a data variable with zero DataSize or no access attributes means to delete it. \r
8d3a5c82 1570 //\r
c6492839 1571 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {\r
1572 Status = EFI_NOT_FOUND;\r
1573 goto Done;\r
1574 }\r
1575 \r
8d3a5c82 1576 //\r
8a2d4996 1577 // Only variable have NV|RT attribute can be created in Runtime.\r
c6492839 1578 //\r
8a2d4996 1579 if (AtRuntime () &&\r
45f6c85b 1580 (((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) || ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0))) {\r
c6492839 1581 Status = EFI_INVALID_PARAMETER;\r
1582 goto Done;\r
1583 } \r
c6492839 1584 }\r
1585\r
1586 //\r
1587 // Function part - create a new variable and copy the data.\r
1588 // Both update a variable and create a variable will come here.\r
8a2d4996 1589\r
c6492839 1590 //\r
1591 // Tricky part: Use scratch data area at the end of volatile variable store\r
1592 // as a temporary storage.\r
1593 //\r
052ad7e1 1594 NextVariable = GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase));\r
188e4e84 1595 ScratchSize = MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize));\r
c6492839 1596\r
2fcdca1d 1597 SetMem (NextVariable, ScratchSize, 0xff);\r
c6492839 1598\r
1599 NextVariable->StartId = VARIABLE_DATA;\r
1600 NextVariable->Attributes = Attributes;\r
1601 //\r
1602 // NextVariable->State = VAR_ADDED;\r
1603 //\r
8a2d4996 1604 NextVariable->Reserved = 0;\r
1605 VarNameOffset = sizeof (VARIABLE_HEADER);\r
1606 VarNameSize = StrSize (VariableName);\r
c6492839 1607 CopyMem (\r
1608 (UINT8 *) ((UINTN) NextVariable + VarNameOffset),\r
1609 VariableName,\r
1610 VarNameSize\r
1611 );\r
1612 VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);\r
1613 CopyMem (\r
1614 (UINT8 *) ((UINTN) NextVariable + VarDataOffset),\r
1615 Data,\r
1616 DataSize\r
1617 );\r
1618 CopyMem (&NextVariable->VendorGuid, VendorGuid, sizeof (EFI_GUID));\r
1619 //\r
1620 // There will be pad bytes after Data, the NextVariable->NameSize and\r
1621 // NextVariable->DataSize should not include pad size so that variable\r
8a2d4996 1622 // service can get actual size in GetVariable.\r
c6492839 1623 //\r
1624 NextVariable->NameSize = (UINT32)VarNameSize;\r
1625 NextVariable->DataSize = (UINT32)DataSize;\r
1626\r
1627 //\r
1628 // The actual size of the variable that stores in storage should\r
1629 // include pad size.\r
1630 //\r
1631 VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);\r
45f6c85b 1632 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
8d3a5c82 1633 //\r
8a2d4996 1634 // Create a nonvolatile variable.\r
8d3a5c82 1635 //\r
fd51bf70 1636 Volatile = FALSE;\r
2fcdca1d 1637 NonVolatileVarableStoreSize = ((VARIABLE_STORE_HEADER *)(UINTN)(mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase))->Size;\r
1638 if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) \r
188e4e84 1639 && ((VarSize + mVariableModuleGlobal->HwErrVariableTotalSize) > PcdGet32 (PcdHwErrStorageSize)))\r
2fcdca1d 1640 || (((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0) \r
188e4e84 1641 && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > NonVolatileVarableStoreSize - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize)))) {\r
8a2d4996 1642 if (AtRuntime ()) {\r
c6492839 1643 Status = EFI_OUT_OF_RESOURCES;\r
1644 goto Done;\r
1645 }\r
1646 //\r
8a2d4996 1647 // Perform garbage collection & reclaim operation.\r
c6492839 1648 //\r
72399dae 1649 Status = Reclaim (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase, \r
23b06935 1650 &mVariableModuleGlobal->NonVolatileLastVariableOffset, FALSE, Variable, FALSE);\r
8d3a5c82 1651 if (EFI_ERROR (Status)) {\r
1652 goto Done;\r
1653 }\r
8d3a5c82 1654 //\r
8a2d4996 1655 // If still no enough space, return out of resources.\r
8d3a5c82 1656 //\r
2fcdca1d 1657 if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) \r
188e4e84 1658 && ((VarSize + mVariableModuleGlobal->HwErrVariableTotalSize) > PcdGet32 (PcdHwErrStorageSize)))\r
2fcdca1d 1659 || (((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0) \r
188e4e84 1660 && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > NonVolatileVarableStoreSize - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize)))) {\r
c6492839 1661 Status = EFI_OUT_OF_RESOURCES;\r
8d3a5c82 1662 goto Done;\r
8d3a5c82 1663 }\r
23b06935
SZ
1664 if (Variable->CurrPtr != NULL) {\r
1665 CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN) CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr));\r
1666 CacheVariable->InDeletedTransitionPtr = NULL;\r
1667 }\r
8d3a5c82 1668 }\r
1669 //\r
8a2d4996 1670 // Four steps\r
c6492839 1671 // 1. Write variable header\r
130e2569 1672 // 2. Set variable state to header valid \r
1673 // 3. Write variable data\r
1674 // 4. Set variable state to valid\r
8d3a5c82 1675 //\r
8d3a5c82 1676 //\r
c6492839 1677 // Step 1:\r
8d3a5c82 1678 //\r
8a2d4996 1679 CacheOffset = mVariableModuleGlobal->NonVolatileLastVariableOffset;\r
c6492839 1680 Status = UpdateVariableStore (\r
052ad7e1 1681 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 1682 FALSE,\r
1683 TRUE,\r
8a9e0b72 1684 Fvb,\r
72399dae 1685 mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
c6492839 1686 sizeof (VARIABLE_HEADER),\r
1687 (UINT8 *) NextVariable\r
1688 );\r
1689\r
1690 if (EFI_ERROR (Status)) {\r
1691 goto Done;\r
1692 }\r
130e2569 1693\r
8d3a5c82 1694 //\r
c6492839 1695 // Step 2:\r
8d3a5c82 1696 //\r
130e2569 1697 NextVariable->State = VAR_HEADER_VALID_ONLY;\r
1698 Status = UpdateVariableStore (\r
1699 &mVariableModuleGlobal->VariableGlobal,\r
1700 FALSE,\r
1701 TRUE,\r
8a9e0b72 1702 Fvb,\r
8a2d4996 1703 mVariableModuleGlobal->NonVolatileLastVariableOffset + OFFSET_OF (VARIABLE_HEADER, State),\r
1704 sizeof (UINT8),\r
1705 &NextVariable->State\r
130e2569 1706 );\r
1707\r
1708 if (EFI_ERROR (Status)) {\r
1709 goto Done;\r
1710 }\r
1711 //\r
1712 // Step 3:\r
1713 //\r
c6492839 1714 Status = UpdateVariableStore (\r
052ad7e1 1715 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 1716 FALSE,\r
1717 TRUE,\r
8a9e0b72 1718 Fvb,\r
72399dae 1719 mVariableModuleGlobal->NonVolatileLastVariableOffset + sizeof (VARIABLE_HEADER),\r
c6492839 1720 (UINT32) VarSize - sizeof (VARIABLE_HEADER),\r
1721 (UINT8 *) NextVariable + sizeof (VARIABLE_HEADER)\r
1722 );\r
1723\r
1724 if (EFI_ERROR (Status)) {\r
1725 goto Done;\r
1726 }\r
8d3a5c82 1727 //\r
130e2569 1728 // Step 4:\r
8d3a5c82 1729 //\r
c6492839 1730 NextVariable->State = VAR_ADDED;\r
1731 Status = UpdateVariableStore (\r
052ad7e1 1732 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 1733 FALSE,\r
1734 TRUE,\r
8a9e0b72 1735 Fvb,\r
8a2d4996 1736 mVariableModuleGlobal->NonVolatileLastVariableOffset + OFFSET_OF (VARIABLE_HEADER, State),\r
1737 sizeof (UINT8),\r
1738 &NextVariable->State\r
c6492839 1739 );\r
1740\r
1741 if (EFI_ERROR (Status)) {\r
1742 goto Done;\r
1743 }\r
8d3a5c82 1744\r
72399dae 1745 mVariableModuleGlobal->NonVolatileLastVariableOffset += HEADER_ALIGN (VarSize);\r
8d3a5c82 1746\r
2fcdca1d 1747 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {\r
1748 mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VarSize);\r
1749 } else {\r
1750 mVariableModuleGlobal->CommonVariableTotalSize += HEADER_ALIGN (VarSize);\r
1751 }\r
8a2d4996 1752 //\r
1753 // update the memory copy of Flash region.\r
1754 //\r
1755 CopyMem ((UINT8 *)mNvVariableCache + CacheOffset, (UINT8 *)NextVariable, VarSize);\r
c6492839 1756 } else {\r
1757 //\r
8a2d4996 1758 // Create a volatile variable.\r
c6492839 1759 // \r
fd51bf70 1760 Volatile = TRUE;\r
c6492839 1761\r
72399dae 1762 if ((UINT32) (VarSize + mVariableModuleGlobal->VolatileLastVariableOffset) >\r
052ad7e1 1763 ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size) {\r
8d3a5c82 1764 //\r
8a2d4996 1765 // Perform garbage collection & reclaim operation.\r
8d3a5c82 1766 //\r
72399dae 1767 Status = Reclaim (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase, \r
23b06935 1768 &mVariableModuleGlobal->VolatileLastVariableOffset, TRUE, Variable, FALSE);\r
8d3a5c82 1769 if (EFI_ERROR (Status)) {\r
1770 goto Done;\r
1771 }\r
1772 //\r
8a2d4996 1773 // If still no enough space, return out of resources.\r
8d3a5c82 1774 //\r
72399dae 1775 if ((UINT32) (VarSize + mVariableModuleGlobal->VolatileLastVariableOffset) >\r
052ad7e1 1776 ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size\r
8d3a5c82 1777 ) {\r
c6492839 1778 Status = EFI_OUT_OF_RESOURCES;\r
8d3a5c82 1779 goto Done;\r
1780 }\r
23b06935
SZ
1781 if (Variable->CurrPtr != NULL) {\r
1782 CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN) CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr));\r
1783 CacheVariable->InDeletedTransitionPtr = NULL;\r
1784 }\r
8d3a5c82 1785 }\r
8d3a5c82 1786\r
c6492839 1787 NextVariable->State = VAR_ADDED;\r
1788 Status = UpdateVariableStore (\r
052ad7e1 1789 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 1790 TRUE,\r
1791 TRUE,\r
8a9e0b72 1792 Fvb,\r
72399dae 1793 mVariableModuleGlobal->VolatileLastVariableOffset,\r
c6492839 1794 (UINT32) VarSize,\r
1795 (UINT8 *) NextVariable\r
1796 );\r
1797\r
1798 if (EFI_ERROR (Status)) {\r
1799 goto Done;\r
8d3a5c82 1800 }\r
c6492839 1801\r
72399dae 1802 mVariableModuleGlobal->VolatileLastVariableOffset += HEADER_ALIGN (VarSize);\r
c6492839 1803 }\r
72399dae 1804\r
c6492839 1805 //\r
8a2d4996 1806 // Mark the old variable as deleted.\r
c6492839 1807 //\r
23b06935
SZ
1808 if (!EFI_ERROR (Status) && Variable->CurrPtr != NULL) {\r
1809 if (Variable->InDeletedTransitionPtr != NULL) {\r
1810 //\r
1811 // Both ADDED and IN_DELETED_TRANSITION old variable are present,\r
1812 // set IN_DELETED_TRANSITION one to DELETED state first.\r
1813 //\r
1814 State = Variable->InDeletedTransitionPtr->State;\r
1815 State &= VAR_DELETED;\r
1816 Status = UpdateVariableStore (\r
1817 &mVariableModuleGlobal->VariableGlobal,\r
1818 Variable->Volatile,\r
1819 FALSE,\r
1820 Fvb,\r
1821 (UINTN) &Variable->InDeletedTransitionPtr->State,\r
1822 sizeof (UINT8),\r
1823 &State\r
1824 );\r
1825 if (!EFI_ERROR (Status)) {\r
1826 if (!Variable->Volatile) {\r
1827 CacheVariable->InDeletedTransitionPtr->State = State;\r
1828 }\r
1829 } else {\r
1830 goto Done;\r
1831 }\r
1832 }\r
1833\r
72399dae 1834 State = Variable->CurrPtr->State;\r
c6492839 1835 State &= VAR_DELETED;\r
1836\r
1837 Status = UpdateVariableStore (\r
72399dae 1838 &mVariableModuleGlobal->VariableGlobal,\r
1839 Variable->Volatile,\r
1840 FALSE,\r
1841 Fvb,\r
1842 (UINTN) &Variable->CurrPtr->State,\r
1843 sizeof (UINT8),\r
1844 &State\r
1845 );\r
8a2d4996 1846 if (!EFI_ERROR (Status) && !Variable->Volatile) { \r
1847 CacheVariable->CurrPtr->State = State;\r
1848 }\r
72399dae 1849 }\r
1850\r
1851 if (!EFI_ERROR (Status)) {\r
1852 UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);\r
335e2681
SZ
1853 if (!Volatile) {\r
1854 FlushHobVariableToFlash (VariableName, VendorGuid);\r
1855 }\r
72399dae 1856 }\r
1857\r
1858Done:\r
1859 return Status;\r
1860}\r
1861\r
a5f15e30
SZ
1862/**\r
1863 Check if a Unicode character is a hexadecimal character.\r
1864\r
1865 This function checks if a Unicode character is a \r
1866 hexadecimal character. The valid hexadecimal character is \r
1867 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.\r
1868\r
1869\r
1870 @param Char The character to check against.\r
1871\r
1872 @retval TRUE If the Char is a hexadecmial character.\r
1873 @retval FALSE If the Char is not a hexadecmial character.\r
1874\r
1875**/\r
1876BOOLEAN\r
1877EFIAPI\r
1878IsHexaDecimalDigitCharacter (\r
1879 IN CHAR16 Char\r
1880 )\r
1881{\r
1882 return (BOOLEAN) ((Char >= L'0' && Char <= L'9') || (Char >= L'A' && Char <= L'F') || (Char >= L'a' && Char <= L'f'));\r
1883}\r
1884\r
1885/**\r
1886\r
1887 This code checks if variable is hardware error record variable or not.\r
1888\r
1889 According to UEFI spec, hardware error record variable should use the EFI_HARDWARE_ERROR_VARIABLE VendorGuid\r
1890 and have the L"HwErrRec####" name convention, #### is a printed hex value and no 0x or h is included in the hex value.\r
1891\r
1892 @param VariableName Pointer to variable name.\r
1893 @param VendorGuid Variable Vendor Guid.\r
1894\r
1895 @retval TRUE Variable is hardware error record variable.\r
1896 @retval FALSE Variable is not hardware error record variable.\r
1897\r
1898**/\r
1899BOOLEAN\r
1900EFIAPI\r
1901IsHwErrRecVariable (\r
1902 IN CHAR16 *VariableName,\r
1903 IN EFI_GUID *VendorGuid\r
1904 )\r
1905{\r
1906 if (!CompareGuid (VendorGuid, &gEfiHardwareErrorVariableGuid) ||\r
1907 (StrLen (VariableName) != StrLen (L"HwErrRec####")) ||\r
1908 (StrnCmp(VariableName, L"HwErrRec", StrLen (L"HwErrRec")) != 0) ||\r
1909 !IsHexaDecimalDigitCharacter (VariableName[0x8]) ||\r
1910 !IsHexaDecimalDigitCharacter (VariableName[0x9]) ||\r
1911 !IsHexaDecimalDigitCharacter (VariableName[0xA]) ||\r
1912 !IsHexaDecimalDigitCharacter (VariableName[0xB])) {\r
1913 return FALSE;\r
1914 }\r
1915\r
1916 return TRUE;\r
1917}\r
1918\r
72399dae 1919/**\r
1920\r
1921 This code finds variable in storage blocks (Volatile or Non-Volatile).\r
1922\r
1923 @param VariableName Name of Variable to be found.\r
1924 @param VendorGuid Variable vendor GUID.\r
1925 @param Attributes Attribute value of the variable found.\r
1926 @param DataSize Size of Data found. If size is less than the\r
1927 data, this value contains the required size.\r
1928 @param Data Data pointer.\r
1929 \r
8a2d4996 1930 @return EFI_INVALID_PARAMETER Invalid parameter.\r
1931 @return EFI_SUCCESS Find the specified variable.\r
1932 @return EFI_NOT_FOUND Not found.\r
1933 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.\r
72399dae 1934\r
1935**/\r
1936EFI_STATUS\r
1937EFIAPI\r
8a2d4996 1938VariableServiceGetVariable (\r
72399dae 1939 IN CHAR16 *VariableName,\r
1940 IN EFI_GUID *VendorGuid,\r
1941 OUT UINT32 *Attributes OPTIONAL,\r
1942 IN OUT UINTN *DataSize,\r
1943 OUT VOID *Data\r
1944 )\r
1945{\r
1946 EFI_STATUS Status;\r
1947 VARIABLE_POINTER_TRACK Variable;\r
1948 UINTN VarDataSize;\r
1949\r
1950 if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {\r
1951 return EFI_INVALID_PARAMETER;\r
1952 }\r
1953\r
1954 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
72399dae 1955 \r
9622df63 1956 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
72399dae 1957 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
1958 goto Done;\r
1959 }\r
1960\r
1961 //\r
1962 // Get data size\r
1963 //\r
1964 VarDataSize = DataSizeOfVariable (Variable.CurrPtr);\r
1965 ASSERT (VarDataSize != 0);\r
1966\r
1967 if (*DataSize >= VarDataSize) {\r
1968 if (Data == NULL) {\r
1969 Status = EFI_INVALID_PARAMETER;\r
1970 goto Done;\r
33a5a666 1971 }\r
72399dae 1972\r
1973 CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);\r
1974 if (Attributes != NULL) {\r
1975 *Attributes = Variable.CurrPtr->Attributes;\r
1976 }\r
1977\r
1978 *DataSize = VarDataSize;\r
1979 UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, TRUE, FALSE, FALSE, FALSE);\r
72399dae 1980 \r
1981 Status = EFI_SUCCESS;\r
1982 goto Done;\r
1983 } else {\r
1984 *DataSize = VarDataSize;\r
1985 Status = EFI_BUFFER_TOO_SMALL;\r
1986 goto Done;\r
8d3a5c82 1987 }\r
1988\r
72399dae 1989Done:\r
1990 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
1991 return Status;\r
1992}\r
1993\r
1994\r
1995\r
1996/**\r
1997\r
1998 This code Finds the Next available variable.\r
1999\r
8a2d4996 2000 @param VariableNameSize Size of the variable name.\r
2001 @param VariableName Pointer to variable name.\r
2002 @param VendorGuid Variable Vendor Guid.\r
72399dae 2003\r
8a2d4996 2004 @return EFI_INVALID_PARAMETER Invalid parameter.\r
2005 @return EFI_SUCCESS Find the specified variable.\r
2006 @return EFI_NOT_FOUND Not found.\r
2007 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.\r
72399dae 2008\r
2009**/\r
2010EFI_STATUS\r
2011EFIAPI\r
8a2d4996 2012VariableServiceGetNextVariableName (\r
72399dae 2013 IN OUT UINTN *VariableNameSize,\r
2014 IN OUT CHAR16 *VariableName,\r
2015 IN OUT EFI_GUID *VendorGuid\r
2016 )\r
2017{\r
0f7aff72 2018 VARIABLE_STORE_TYPE Type;\r
72399dae 2019 VARIABLE_POINTER_TRACK Variable;\r
0f7aff72 2020 VARIABLE_POINTER_TRACK VariableInHob;\r
23b06935 2021 VARIABLE_POINTER_TRACK VariablePtrTrack;\r
72399dae 2022 UINTN VarNameSize;\r
2023 EFI_STATUS Status;\r
0f7aff72 2024 VARIABLE_STORE_HEADER *VariableStoreHeader[VariableStoreTypeMax];\r
72399dae 2025\r
2026 if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {\r
2027 return EFI_INVALID_PARAMETER;\r
2028 }\r
2029\r
2030 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2031\r
9622df63 2032 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
72399dae 2033 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
2034 goto Done;\r
2035 }\r
2036\r
2037 if (VariableName[0] != 0) {\r
2038 //\r
8a2d4996 2039 // If variable name is not NULL, get next variable.\r
72399dae 2040 //\r
2041 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
2042 }\r
2043\r
0f7aff72
RN
2044 //\r
2045 // 0: Volatile, 1: HOB, 2: Non-Volatile.\r
2046 // The index and attributes mapping must be kept in this order as FindVariable\r
2047 // makes use of this mapping to implement search algorithm.\r
2048 //\r
2049 VariableStoreHeader[VariableStoreTypeVolatile] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;\r
2050 VariableStoreHeader[VariableStoreTypeHob] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;\r
2051 VariableStoreHeader[VariableStoreTypeNv] = mNvVariableCache;\r
2052\r
72399dae 2053 while (TRUE) {\r
2054 //\r
0f7aff72 2055 // Switch from Volatile to HOB, to Non-Volatile.\r
72399dae 2056 //\r
0f7aff72
RN
2057 while ((Variable.CurrPtr >= Variable.EndPtr) ||\r
2058 (Variable.CurrPtr == NULL) ||\r
2059 !IsValidVariableHeader (Variable.CurrPtr)\r
2060 ) {\r
2061 //\r
2062 // Find current storage index\r
2063 //\r
2064 for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {\r
2065 if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr == GetStartPointer (VariableStoreHeader[Type]))) {\r
2066 break;\r
2067 }\r
2068 }\r
2069 ASSERT (Type < VariableStoreTypeMax);\r
2070 //\r
2071 // Switch to next storage\r
2072 //\r
2073 for (Type++; Type < VariableStoreTypeMax; Type++) {\r
2074 if (VariableStoreHeader[Type] != NULL) {\r
2075 break;\r
2076 }\r
2077 }\r
2078 //\r
2079 // Capture the case that \r
2080 // 1. current storage is the last one, or\r
2081 // 2. no further storage\r
2082 //\r
2083 if (Type == VariableStoreTypeMax) {\r
72399dae 2084 Status = EFI_NOT_FOUND;\r
2085 goto Done;\r
2086 }\r
0f7aff72
RN
2087 Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);\r
2088 Variable.EndPtr = GetEndPointer (VariableStoreHeader[Type]);\r
2089 Variable.CurrPtr = Variable.StartPtr;\r
72399dae 2090 }\r
0f7aff72 2091\r
72399dae 2092 //\r
2093 // Variable is found\r
2094 //\r
23b06935
SZ
2095 if (Variable.CurrPtr->State == VAR_ADDED || Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
2096 if (!AtRuntime () || ((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {\r
2097 if (Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
2098 //\r
2099 // If it is a IN_DELETED_TRANSITION variable,\r
2100 // and there is also a same ADDED one at the same time,\r
2101 // don't return it.\r
2102 //\r
2103 VariablePtrTrack.StartPtr = Variable.StartPtr;\r
2104 VariablePtrTrack.EndPtr = Variable.EndPtr;\r
2105 Status = FindVariableEx (\r
2106 GetVariableNamePtr (Variable.CurrPtr),\r
2107 &Variable.CurrPtr->VendorGuid,\r
2108 FALSE,\r
2109 &VariablePtrTrack\r
2110 );\r
2111 if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State == VAR_ADDED) {\r
2112 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
2113 continue;\r
2114 }\r
2115 }\r
0f7aff72
RN
2116\r
2117 //\r
2118 // Don't return NV variable when HOB overrides it\r
2119 //\r
2120 if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) && (VariableStoreHeader[VariableStoreTypeNv] != NULL) && \r
2121 (Variable.StartPtr == GetStartPointer (VariableStoreHeader[VariableStoreTypeNv]))\r
2122 ) {\r
2123 VariableInHob.StartPtr = GetStartPointer (VariableStoreHeader[VariableStoreTypeHob]);\r
2124 VariableInHob.EndPtr = GetEndPointer (VariableStoreHeader[VariableStoreTypeHob]);\r
2125 Status = FindVariableEx (\r
2126 GetVariableNamePtr (Variable.CurrPtr),\r
2127 &Variable.CurrPtr->VendorGuid,\r
9622df63 2128 FALSE,\r
0f7aff72
RN
2129 &VariableInHob\r
2130 );\r
2131 if (!EFI_ERROR (Status)) {\r
2132 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
2133 continue;\r
2134 }\r
2135 }\r
2136\r
72399dae 2137 VarNameSize = NameSizeOfVariable (Variable.CurrPtr);\r
2138 ASSERT (VarNameSize != 0);\r
2139\r
2140 if (VarNameSize <= *VariableNameSize) {\r
0f7aff72
RN
2141 CopyMem (VariableName, GetVariableNamePtr (Variable.CurrPtr), VarNameSize);\r
2142 CopyMem (VendorGuid, &Variable.CurrPtr->VendorGuid, sizeof (EFI_GUID));\r
72399dae 2143 Status = EFI_SUCCESS;\r
2144 } else {\r
2145 Status = EFI_BUFFER_TOO_SMALL;\r
2146 }\r
2147\r
2148 *VariableNameSize = VarNameSize;\r
2149 goto Done;\r
2150 }\r
2151 }\r
2152\r
2153 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
2154 }\r
33a5a666 2155\r
8d3a5c82 2156Done:\r
72399dae 2157 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2158 return Status;\r
2159}\r
2160\r
2161/**\r
2162\r
2163 This code sets variable in storage blocks (Volatile or Non-Volatile).\r
2164\r
8a2d4996 2165 @param VariableName Name of Variable to be found.\r
2166 @param VendorGuid Variable vendor GUID.\r
72399dae 2167 @param Attributes Attribute value of the variable found\r
2168 @param DataSize Size of Data found. If size is less than the\r
2169 data, this value contains the required size.\r
8a2d4996 2170 @param Data Data pointer.\r
72399dae 2171\r
8a2d4996 2172 @return EFI_INVALID_PARAMETER Invalid parameter.\r
2173 @return EFI_SUCCESS Set successfully.\r
2174 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.\r
2175 @return EFI_NOT_FOUND Not found.\r
2176 @return EFI_WRITE_PROTECTED Variable is read-only.\r
72399dae 2177\r
2178**/\r
2179EFI_STATUS\r
2180EFIAPI\r
8a2d4996 2181VariableServiceSetVariable (\r
72399dae 2182 IN CHAR16 *VariableName,\r
2183 IN EFI_GUID *VendorGuid,\r
2184 IN UINT32 Attributes,\r
2185 IN UINTN DataSize,\r
2186 IN VOID *Data\r
2187 )\r
2188{\r
2189 VARIABLE_POINTER_TRACK Variable;\r
2190 EFI_STATUS Status;\r
2191 VARIABLE_HEADER *NextVariable;\r
2192 EFI_PHYSICAL_ADDRESS Point;\r
2193\r
2194 //\r
8a2d4996 2195 // Check input parameters.\r
72399dae 2196 //\r
2197 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r
2198 return EFI_INVALID_PARAMETER;\r
8a2d4996 2199 } \r
48a0e6bf 2200\r
2201 if (DataSize != 0 && Data == NULL) {\r
2202 return EFI_INVALID_PARAMETER;\r
2203 }\r
2204\r
8e38f18e 2205 //\r
2206 // Not support authenticated variable write yet.\r
2207 //\r
2208 if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {\r
2209 return EFI_INVALID_PARAMETER;\r
2210 }\r
2211\r
72399dae 2212 //\r
8a2d4996 2213 // Make sure if runtime bit is set, boot service bit is set also.\r
72399dae 2214 //\r
2215 if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
2216 return EFI_INVALID_PARAMETER;\r
2217 }\r
2218\r
2219 //\r
2220 // The size of the VariableName, including the Unicode Null in bytes plus\r
188e4e84 2221 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)\r
2222 // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.\r
72399dae 2223 //\r
2224 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
188e4e84 2225 if ((DataSize > PcdGet32 (PcdMaxHardwareErrorVariableSize)) ||\r
2226 (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > PcdGet32 (PcdMaxHardwareErrorVariableSize))) {\r
72399dae 2227 return EFI_INVALID_PARAMETER;\r
2228 }\r
a5f15e30 2229 if (!IsHwErrRecVariable(VariableName, VendorGuid)) {\r
72399dae 2230 return EFI_INVALID_PARAMETER;\r
2231 }\r
2232 } else {\r
2233 //\r
2234 // The size of the VariableName, including the Unicode Null in bytes plus\r
188e4e84 2235 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxVariableSize) bytes.\r
72399dae 2236 //\r
188e4e84 2237 if ((DataSize > PcdGet32 (PcdMaxVariableSize)) ||\r
2238 (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > PcdGet32 (PcdMaxVariableSize))) {\r
72399dae 2239 return EFI_INVALID_PARAMETER;\r
2240 } \r
021a1af9
SZ
2241 }\r
2242\r
2243 if (AtRuntime ()) {\r
2244 //\r
2245 // HwErrRecSupport Global Variable identifies the level of hardware error record persistence\r
2246 // support implemented by the platform. This variable is only modified by firmware and is read-only to the OS.\r
2247 //\r
2248 if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, L"HwErrRecSupport") == 0)) {\r
2249 return EFI_WRITE_PROTECTED;\r
2250 }\r
2251 }\r
72399dae 2252\r
2253 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2254\r
2255 //\r
8a2d4996 2256 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.\r
72399dae 2257 //\r
2258 if (1 < InterlockedIncrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState)) {\r
8a2d4996 2259 Point = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;\r
72399dae 2260 //\r
8a2d4996 2261 // Parse non-volatile variable data and get last variable offset.\r
72399dae 2262 //\r
2263 NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point);\r
2264 while ((NextVariable < GetEndPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point)) \r
2265 && IsValidVariableHeader (NextVariable)) {\r
2266 NextVariable = GetNextVariablePtr (NextVariable);\r
2267 }\r
2268 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) Point;\r
2269 }\r
2270\r
2271 //\r
8a2d4996 2272 // Check whether the input variable is already existed.\r
72399dae 2273 //\r
9622df63
SZ
2274 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, TRUE);\r
2275 if (!EFI_ERROR (Status)) {\r
2276 if (((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) && AtRuntime ()) {\r
2277 return EFI_WRITE_PROTECTED;\r
2278 }\r
2279 }\r
72399dae 2280\r
2281 //\r
8a2d4996 2282 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.\r
72399dae 2283 //\r
2284 AutoUpdateLangVariable (VariableName, Data, DataSize);\r
2285\r
2286 Status = UpdateVariable (VariableName, VendorGuid, Data, DataSize, Attributes, &Variable);\r
2287\r
fdb7765f 2288 InterlockedDecrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState);\r
052ad7e1 2289 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
fdb7765f 2290\r
8d3a5c82 2291 return Status;\r
2292}\r
2293\r
7c80e839 2294/**\r
8d3a5c82 2295\r
2296 This code returns information about the EFI variables.\r
2297\r
7c80e839 2298 @param Attributes Attributes bitmask to specify the type of variables\r
2299 on which to return information.\r
2300 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available\r
2301 for the EFI variables associated with the attributes specified.\r
2302 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available\r
2303 for EFI variables associated with the attributes specified.\r
2304 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables\r
2305 associated with the attributes specified.\r
8d3a5c82 2306\r
7c80e839 2307 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.\r
2308 @return EFI_SUCCESS Query successfully.\r
2309 @return EFI_UNSUPPORTED The attribute is not supported on this platform.\r
8d3a5c82 2310\r
7c80e839 2311**/\r
052ad7e1
A
2312EFI_STATUS\r
2313EFIAPI\r
8a2d4996 2314VariableServiceQueryVariableInfo (\r
052ad7e1
A
2315 IN UINT32 Attributes,\r
2316 OUT UINT64 *MaximumVariableStorageSize,\r
2317 OUT UINT64 *RemainingVariableStorageSize,\r
2318 OUT UINT64 *MaximumVariableSize\r
2319 )\r
8d3a5c82 2320{\r
2321 VARIABLE_HEADER *Variable;\r
2322 VARIABLE_HEADER *NextVariable;\r
2323 UINT64 VariableSize;\r
2324 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
2fcdca1d 2325 UINT64 CommonVariableTotalSize;\r
2326 UINT64 HwErrVariableTotalSize;\r
2327\r
2328 CommonVariableTotalSize = 0;\r
2329 HwErrVariableTotalSize = 0;\r
8d3a5c82 2330\r
c6492839 2331 if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {\r
8d3a5c82 2332 return EFI_INVALID_PARAMETER;\r
2333 }\r
2fcdca1d 2334\r
c6492839 2335 if((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == 0) {\r
8d3a5c82 2336 //\r
2337 // Make sure the Attributes combination is supported by the platform.\r
2338 //\r
c6492839 2339 return EFI_UNSUPPORTED; \r
8d3a5c82 2340 } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
2341 //\r
2342 // Make sure if runtime bit is set, boot service bit is set also.\r
2343 //\r
2344 return EFI_INVALID_PARAMETER;\r
8a2d4996 2345 } else if (AtRuntime () && ((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0)) {\r
8d3a5c82 2346 //\r
2347 // Make sure RT Attribute is set if we are in Runtime phase.\r
2348 //\r
2349 return EFI_INVALID_PARAMETER;\r
2fcdca1d 2350 } else if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2351 //\r
2352 // Make sure Hw Attribute is set with NV.\r
2353 //\r
2354 return EFI_INVALID_PARAMETER;\r
8e38f18e 2355 } else if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {\r
2356 //\r
2357 // Not support authentiated variable write yet.\r
2358 //\r
2359 return EFI_UNSUPPORTED;\r
8d3a5c82 2360 }\r
2361\r
052ad7e1 2362 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
8d3a5c82 2363\r
2364 if((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r
2365 //\r
2366 // Query is Volatile related.\r
2367 //\r
052ad7e1 2368 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r
8d3a5c82 2369 } else {\r
2370 //\r
2371 // Query is Non-Volatile related.\r
2372 //\r
8a2d4996 2373 VariableStoreHeader = mNvVariableCache;\r
8d3a5c82 2374 }\r
2375\r
2376 //\r
2377 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize\r
2378 // with the storage size (excluding the storage header size).\r
2379 //\r
2380 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);\r
c6492839 2381\r
2382 //\r
2383 // Harware error record variable needs larger size.\r
2384 //\r
2fcdca1d 2385 if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
188e4e84 2386 *MaximumVariableStorageSize = PcdGet32 (PcdHwErrStorageSize);\r
2387 *MaximumVariableSize = PcdGet32 (PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER);\r
2fcdca1d 2388 } else {\r
2389 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
188e4e84 2390 ASSERT (PcdGet32 (PcdHwErrStorageSize) < VariableStoreHeader->Size);\r
2391 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize);\r
2fcdca1d 2392 }\r
2393\r
2394 //\r
188e4e84 2395 // Let *MaximumVariableSize be PcdGet32 (PcdMaxVariableSize) with the exception of the variable header size.\r
2fcdca1d 2396 //\r
188e4e84 2397 *MaximumVariableSize = PcdGet32 (PcdMaxVariableSize) - sizeof (VARIABLE_HEADER);\r
c6492839 2398 }\r
8d3a5c82 2399\r
2400 //\r
2401 // Point to the starting address of the variables.\r
2402 //\r
9cad030b 2403 Variable = GetStartPointer (VariableStoreHeader);\r
8d3a5c82 2404\r
2405 //\r
2406 // Now walk through the related variable store.\r
2407 //\r
6f90dfbc 2408 while ((Variable < GetEndPointer (VariableStoreHeader)) && IsValidVariableHeader (Variable)) {\r
8d3a5c82 2409 NextVariable = GetNextVariablePtr (Variable);\r
2410 VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;\r
2411\r
8a2d4996 2412 if (AtRuntime ()) {\r
8d3a5c82 2413 //\r
8a2d4996 2414 // We don't take the state of the variables in mind\r
8d3a5c82 2415 // when calculating RemainingVariableStorageSize,\r
2416 // since the space occupied by variables not marked with\r
2417 // VAR_ADDED is not allowed to be reclaimed in Runtime.\r
2418 //\r
3b425367 2419 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2fcdca1d 2420 HwErrVariableTotalSize += VariableSize;\r
2421 } else {\r
2422 CommonVariableTotalSize += VariableSize;\r
2423 }\r
8d3a5c82 2424 } else {\r
2425 //\r
8a2d4996 2426 // Only care about Variables with State VAR_ADDED, because\r
8d3a5c82 2427 // the space not marked as VAR_ADDED is reclaimable now.\r
2428 //\r
2429 if (Variable->State == VAR_ADDED) {\r
3b425367 2430 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2fcdca1d 2431 HwErrVariableTotalSize += VariableSize;\r
2432 } else {\r
2433 CommonVariableTotalSize += VariableSize;\r
2434 }\r
8d3a5c82 2435 }\r
2436 }\r
2437\r
2438 //\r
8a2d4996 2439 // Go to the next one.\r
8d3a5c82 2440 //\r
2441 Variable = NextVariable;\r
2442 }\r
2443\r
2fcdca1d 2444 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD){\r
2445 *RemainingVariableStorageSize = *MaximumVariableStorageSize - HwErrVariableTotalSize;\r
2446 }else {\r
2447 *RemainingVariableStorageSize = *MaximumVariableStorageSize - CommonVariableTotalSize;\r
2448 }\r
2449\r
c6492839 2450 if (*RemainingVariableStorageSize < sizeof (VARIABLE_HEADER)) {\r
2451 *MaximumVariableSize = 0;\r
2452 } else if ((*RemainingVariableStorageSize - sizeof (VARIABLE_HEADER)) < *MaximumVariableSize) {\r
2453 *MaximumVariableSize = *RemainingVariableStorageSize - sizeof (VARIABLE_HEADER);\r
2454 }\r
2455\r
052ad7e1 2456 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
8d3a5c82 2457 return EFI_SUCCESS;\r
2458}\r
2459\r
7c80e839 2460\r
2461/**\r
8a2d4996 2462 This function reclaims variable storage if free size is below the threshold.\r
2463 \r
7c80e839 2464**/\r
7800593d 2465VOID\r
7800593d 2466ReclaimForOS(\r
8a2d4996 2467 VOID\r
7800593d
LG
2468 )\r
2469{\r
9948c0b0 2470 EFI_STATUS Status;\r
2fcdca1d 2471 UINTN CommonVariableSpace;\r
2472 UINTN RemainingCommonVariableSpace;\r
2473 UINTN RemainingHwErrVariableSpace;\r
7800593d 2474\r
7800593d
LG
2475 Status = EFI_SUCCESS; \r
2476\r
2fcdca1d 2477 CommonVariableSpace = ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase)))->Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32(PcdHwErrStorageSize); //Allowable max size of common variable storage space\r
2478\r
2479 RemainingCommonVariableSpace = CommonVariableSpace - mVariableModuleGlobal->CommonVariableTotalSize;\r
2480\r
2481 RemainingHwErrVariableSpace = PcdGet32 (PcdHwErrStorageSize) - mVariableModuleGlobal->HwErrVariableTotalSize;\r
7800593d 2482 //\r
9948c0b0 2483 // Check if the free area is blow a threshold.\r
7800593d 2484 //\r
2fcdca1d 2485 if ((RemainingCommonVariableSpace < PcdGet32 (PcdMaxVariableSize))\r
9948c0b0 2486 || ((PcdGet32 (PcdHwErrStorageSize) != 0) && \r
2487 (RemainingHwErrVariableSpace < PcdGet32 (PcdMaxHardwareErrorVariableSize)))){\r
7800593d 2488 Status = Reclaim (\r
2fcdca1d 2489 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
2490 &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
2491 FALSE,\r
335e2681
SZ
2492 NULL,\r
2493 FALSE\r
2fcdca1d 2494 );\r
7800593d
LG
2495 ASSERT_EFI_ERROR (Status);\r
2496 }\r
2497}\r
2498\r
335e2681
SZ
2499/**\r
2500 Flush the HOB variable to flash.\r
2501\r
2502 @param[in] VariableName Name of variable has been updated or deleted.\r
2503 @param[in] VendorGuid Guid of variable has been updated or deleted.\r
2504\r
2505**/\r
2506VOID\r
2507FlushHobVariableToFlash (\r
2508 IN CHAR16 *VariableName,\r
2509 IN EFI_GUID *VendorGuid\r
2510 )\r
2511{\r
2512 EFI_STATUS Status;\r
2513 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
2514 VARIABLE_HEADER *Variable;\r
2515 VOID *VariableData;\r
2516 BOOLEAN ErrorFlag;\r
2517\r
2518 ErrorFlag = FALSE;\r
2519\r
2520 //\r
2521 // Flush the HOB variable to flash.\r
2522 //\r
2523 if (mVariableModuleGlobal->VariableGlobal.HobVariableBase != 0) {\r
2524 VariableStoreHeader = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;\r
2525 //\r
2526 // Set HobVariableBase to 0, it can avoid SetVariable to call back.\r
2527 //\r
2528 mVariableModuleGlobal->VariableGlobal.HobVariableBase = 0;\r
2529 for ( Variable = GetStartPointer (VariableStoreHeader)\r
2530 ; (Variable < GetEndPointer (VariableStoreHeader) && IsValidVariableHeader (Variable))\r
2531 ; Variable = GetNextVariablePtr (Variable)\r
2532 ) {\r
2533 if (Variable->State != VAR_ADDED) {\r
2534 //\r
2535 // The HOB variable has been set to DELETED state in local.\r
2536 //\r
2537 continue;\r
2538 }\r
2539 ASSERT ((Variable->Attributes & EFI_VARIABLE_NON_VOLATILE) != 0);\r
2540 if (VendorGuid == NULL || VariableName == NULL ||\r
2541 !CompareGuid (VendorGuid, &Variable->VendorGuid) ||\r
2542 StrCmp (VariableName, GetVariableNamePtr (Variable)) != 0) {\r
2543 VariableData = GetVariableDataPtr (Variable);\r
2544 Status = VariableServiceSetVariable (\r
2545 GetVariableNamePtr (Variable),\r
2546 &Variable->VendorGuid,\r
2547 Variable->Attributes,\r
2548 Variable->DataSize,\r
2549 VariableData\r
2550 );\r
2551 DEBUG ((EFI_D_INFO, "Variable driver flush the HOB variable to flash: %g %s %r\n", &Variable->VendorGuid, GetVariableNamePtr (Variable), Status));\r
2552 } else {\r
2553 //\r
2554 // The updated or deleted variable is matched with the HOB variable.\r
2555 // Don't break here because we will try to set other HOB variables\r
2556 // since this variable could be set successfully.\r
2557 //\r
2558 Status = EFI_SUCCESS;\r
2559 }\r
2560 if (!EFI_ERROR (Status)) {\r
2561 //\r
2562 // If set variable successful, or the updated or deleted variable is matched with the HOB variable,\r
2563 // set the HOB variable to DELETED state in local.\r
2564 //\r
2565 DEBUG ((EFI_D_INFO, "Variable driver set the HOB variable to DELETED state in local: %g %s\n", &Variable->VendorGuid, GetVariableNamePtr (Variable)));\r
2566 Variable->State &= VAR_DELETED;\r
2567 } else {\r
2568 ErrorFlag = TRUE;\r
2569 }\r
2570 }\r
2571 if (ErrorFlag) {\r
2572 //\r
2573 // We still have HOB variable(s) not flushed in flash.\r
2574 //\r
2575 mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VariableStoreHeader;\r
2576 } else {\r
2577 //\r
2578 // All HOB variables have been flushed in flash.\r
2579 //\r
2580 DEBUG ((EFI_D_INFO, "Variable driver: all HOB variables have been flushed in flash.\n"));\r
2581 if (!AtRuntime ()) {\r
2582 FreePool ((VOID *) VariableStoreHeader);\r
2583 }\r
2584 }\r
2585 }\r
2586\r
2587}\r
8a2d4996 2588\r
7c80e839 2589/**\r
8a2d4996 2590 Initializes variable write service after FVB was ready.\r
7c80e839 2591\r
8a2d4996 2592 @retval EFI_SUCCESS Function successfully executed.\r
2593 @retval Others Fail to initialize the variable service.\r
2594\r
2595**/\r
2596EFI_STATUS\r
2597VariableWriteServiceInitialize (\r
2598 VOID\r
2599 )\r
2600{\r
2601 EFI_STATUS Status;\r
2602 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
2603 UINTN Index;\r
2604 UINT8 Data;\r
8a2d4996 2605 EFI_PHYSICAL_ADDRESS VariableStoreBase;\r
8a2d4996 2606\r
2607 VariableStoreBase = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;\r
2608 VariableStoreHeader = (VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase;\r
0f7aff72 2609 \r
8a2d4996 2610 //\r
2611 // Check if the free area is really free.\r
2612 //\r
0f7aff72 2613 for (Index = mVariableModuleGlobal->NonVolatileLastVariableOffset; Index < VariableStoreHeader->Size; Index++) {\r
8a2d4996 2614 Data = ((UINT8 *) mNvVariableCache)[Index];\r
2615 if (Data != 0xff) {\r
2616 //\r
2617 // There must be something wrong in variable store, do reclaim operation.\r
2618 //\r
2619 Status = Reclaim (\r
2620 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
2621 &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
2622 FALSE,\r
335e2681
SZ
2623 NULL,\r
2624 TRUE\r
8a2d4996 2625 );\r
2626 if (EFI_ERROR (Status)) {\r
2627 return Status;\r
2628 }\r
2629 break;\r
2630 }\r
2631 }\r
2632\r
335e2681 2633 FlushHobVariableToFlash (NULL, NULL);\r
0f7aff72 2634\r
8a2d4996 2635 return EFI_SUCCESS;\r
2636}\r
2637\r
2638\r
2639/**\r
2640 Initializes variable store area for non-volatile and volatile variable.\r
7c80e839 2641\r
2642 @retval EFI_SUCCESS Function successfully executed.\r
2643 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.\r
2644\r
2645**/\r
8d3a5c82 2646EFI_STATUS\r
8d3a5c82 2647VariableCommonInitialize (\r
8a2d4996 2648 VOID\r
8d3a5c82 2649 )\r
8d3a5c82 2650{\r
2651 EFI_STATUS Status;\r
8d3a5c82 2652 VARIABLE_STORE_HEADER *VolatileVariableStore;\r
2653 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
2654 VARIABLE_HEADER *NextVariable;\r
8a9e0b72 2655 EFI_PHYSICAL_ADDRESS TempVariableStoreHeader;\r
8a9e0b72 2656 EFI_PHYSICAL_ADDRESS VariableStoreBase;\r
052ad7e1 2657 UINT64 VariableStoreLength;\r
2fcdca1d 2658 UINTN ScratchSize;\r
aa75dfec 2659 UINTN VariableSize;\r
0f7aff72 2660 EFI_HOB_GUID_TYPE *GuidHob;\r
8d3a5c82 2661\r
7800593d
LG
2662 //\r
2663 // Allocate runtime memory for variable driver global structure.\r
2664 //\r
72399dae 2665 mVariableModuleGlobal = AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL));\r
7800593d
LG
2666 if (mVariableModuleGlobal == NULL) {\r
2667 return EFI_OUT_OF_RESOURCES;\r
2668 }\r
8d3a5c82 2669\r
8a2d4996 2670 InitializeLock (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock, TPL_NOTIFY);\r
8d3a5c82 2671\r
48cd992a 2672 //\r
2673 // Note that in EdkII variable driver implementation, Hardware Error Record type variable\r
2674 // is stored with common variable in the same NV region. So the platform integrator should\r
2675 // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of \r
2676 // PcdFlashNvStorageVariableSize.\r
2677 //\r
188e4e84 2678 ASSERT (PcdGet32 (PcdHwErrStorageSize) <= PcdGet32 (PcdFlashNvStorageVariableSize));\r
48cd992a 2679\r
0f7aff72
RN
2680 //\r
2681 // Get HOB variable store.\r
2682 //\r
2683 GuidHob = GetFirstGuidHob (&gEfiVariableGuid);\r
2684 if (GuidHob != NULL) {\r
f68af18e 2685 VariableStoreHeader = GET_GUID_HOB_DATA (GuidHob);\r
335e2681 2686 VariableStoreLength = (UINT64) (GuidHob->Header.HobLength - sizeof (EFI_HOB_GUID_TYPE));\r
f68af18e 2687 if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) {\r
335e2681
SZ
2688 mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) AllocateRuntimeCopyPool ((UINTN) VariableStoreLength, (VOID *) VariableStoreHeader);\r
2689 if (mVariableModuleGlobal->VariableGlobal.HobVariableBase == 0) {\r
2690 return EFI_OUT_OF_RESOURCES;\r
2691 }\r
f68af18e
RN
2692 } else {\r
2693 DEBUG ((EFI_D_ERROR, "HOB Variable Store header is corrupted!\n"));\r
2694 }\r
0f7aff72
RN
2695 }\r
2696\r
8d3a5c82 2697 //\r
2fcdca1d 2698 // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.\r
8d3a5c82 2699 //\r
188e4e84 2700 ScratchSize = MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize));\r
2701 VolatileVariableStore = AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize) + ScratchSize);\r
8d3a5c82 2702 if (VolatileVariableStore == NULL) {\r
2703 FreePool (mVariableModuleGlobal);\r
2704 return EFI_OUT_OF_RESOURCES;\r
2705 }\r
2706\r
188e4e84 2707 SetMem (VolatileVariableStore, PcdGet32 (PcdVariableStoreSize) + ScratchSize, 0xff);\r
8d3a5c82 2708\r
2709 //\r
8a2d4996 2710 // Initialize Variable Specific Data.\r
8d3a5c82 2711 //\r
052ad7e1 2712 mVariableModuleGlobal->VariableGlobal.VolatileVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VolatileVariableStore;\r
9cad030b 2713 mVariableModuleGlobal->VolatileLastVariableOffset = (UINTN) GetStartPointer (VolatileVariableStore) - (UINTN) VolatileVariableStore;\r
8a2d4996 2714 mVariableModuleGlobal->FvbInstance = NULL;\r
8d3a5c82 2715\r
3709c4cd 2716 CopyGuid (&VolatileVariableStore->Signature, &gEfiVariableGuid);\r
8a2d4996 2717 VolatileVariableStore->Size = PcdGet32 (PcdVariableStoreSize);\r
2718 VolatileVariableStore->Format = VARIABLE_STORE_FORMATTED;\r
2719 VolatileVariableStore->State = VARIABLE_STORE_HEALTHY;\r
2720 VolatileVariableStore->Reserved = 0;\r
2721 VolatileVariableStore->Reserved1 = 0;\r
8d3a5c82 2722\r
2723 //\r
d9303576 2724 // Get non-volatile variable store.\r
8d3a5c82 2725 //\r
2726\r
92a4f6f3 2727 TempVariableStoreHeader = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);\r
2728 if (TempVariableStoreHeader == 0) {\r
2729 TempVariableStoreHeader = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);\r
2730 }\r
d6550260 2731\r
2732 //\r
2733 // Check if the Firmware Volume is not corrupted\r
2734 //\r
2735 if ((((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(TempVariableStoreHeader))->Signature != EFI_FVH_SIGNATURE) ||\r
2736 (!CompareGuid (&gEfiSystemNvDataFvGuid, &((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(TempVariableStoreHeader))->FileSystemGuid))) {\r
2737 Status = EFI_VOLUME_CORRUPTED;\r
2738 DEBUG ((EFI_D_ERROR, "Firmware Volume for Variable Store is corrupted\n"));\r
2739 goto Done;\r
2740 }\r
2741\r
8a2d4996 2742 VariableStoreBase = TempVariableStoreHeader + \\r
2743 (((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(TempVariableStoreHeader)) -> HeaderLength);\r
2744 VariableStoreLength = (UINT64) PcdGet32 (PcdFlashNvStorageVariableSize) - \\r
8a9e0b72 2745 (((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(TempVariableStoreHeader)) -> HeaderLength);\r
8d3a5c82 2746\r
8a2d4996 2747 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase;\r
2748 VariableStoreHeader = (VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase;\r
2749 if (GetVariableStoreStatus (VariableStoreHeader) != EfiValid) {\r
2750 Status = EFI_VOLUME_CORRUPTED;\r
2751 DEBUG((EFI_D_INFO, "Variable Store header is corrupted\n"));\r
7800593d 2752 goto Done;\r
8a2d4996 2753 } \r
2754 ASSERT(VariableStoreHeader->Size == VariableStoreLength);\r
2755 \r
8d3a5c82 2756 //\r
8a2d4996 2757 // Parse non-volatile variable data and get last variable offset.\r
8d3a5c82 2758 //\r
8a2d4996 2759 NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase);\r
2760 while (IsValidVariableHeader (NextVariable)) {\r
2761 VariableSize = NextVariable->NameSize + NextVariable->DataSize + sizeof (VARIABLE_HEADER);\r
2762 if ((NextVariable->Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
2763 mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VariableSize);\r
2764 } else {\r
2765 mVariableModuleGlobal->CommonVariableTotalSize += HEADER_ALIGN (VariableSize);\r
8d3a5c82 2766 }\r
2767\r
8a2d4996 2768 NextVariable = GetNextVariablePtr (NextVariable);\r
2769 }\r
7800593d 2770\r
8a2d4996 2771 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) VariableStoreBase;\r
2772 \r
2773 //\r
2774 // Allocate runtime memory used for a memory copy of the FLASH region.\r
2775 // Keep the memory and the FLASH in sync as updates occur\r
2776 //\r
2777 mNvVariableCache = AllocateRuntimeZeroPool ((UINTN)VariableStoreLength);\r
2778 if (mNvVariableCache == NULL) {\r
2779 Status = EFI_OUT_OF_RESOURCES;\r
2780 goto Done;\r
8d3a5c82 2781 }\r
8a2d4996 2782 CopyMem (mNvVariableCache, (CHAR8 *)(UINTN)VariableStoreBase, (UINTN)VariableStoreLength);\r
2783 Status = EFI_SUCCESS;\r
8d3a5c82 2784\r
7800593d 2785Done:\r
8d3a5c82 2786 if (EFI_ERROR (Status)) {\r
2787 FreePool (mVariableModuleGlobal);\r
2788 FreePool (VolatileVariableStore);\r
2789 }\r
2790\r
2791 return Status;\r
2792}\r
052ad7e1 2793\r
052ad7e1 2794\r
aa75dfec 2795/**\r
8a2d4996 2796 Get the proper fvb handle and/or fvb protocol by the given Flash address.\r
aa75dfec 2797\r
8a2d4996 2798 @param[in] Address The Flash address.\r
2799 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.\r
2800 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.\r
aa75dfec 2801\r
aa75dfec 2802**/\r
8a2d4996 2803EFI_STATUS\r
2804GetFvbInfoByAddress (\r
2805 IN EFI_PHYSICAL_ADDRESS Address,\r
2806 OUT EFI_HANDLE *FvbHandle OPTIONAL,\r
2807 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvbProtocol OPTIONAL\r
8a9e0b72 2808 )\r
2809{\r
8a2d4996 2810 EFI_STATUS Status;\r
2811 EFI_HANDLE *HandleBuffer;\r
2812 UINTN HandleCount;\r
2813 UINTN Index;\r
2814 EFI_PHYSICAL_ADDRESS FvbBaseAddress;\r
2815 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r
2816 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r
2817 EFI_FVB_ATTRIBUTES_2 Attributes;\r
2818 \r
8a9e0b72 2819 //\r
8a2d4996 2820 // Get all FVB handles.\r
8a9e0b72 2821 //\r
8a2d4996 2822 Status = GetFvbCountAndBuffer (&HandleCount, &HandleBuffer);\r
8a9e0b72 2823 if (EFI_ERROR (Status)) {\r
8a2d4996 2824 return EFI_NOT_FOUND;\r
8a9e0b72 2825 }\r
8a2d4996 2826\r
8a9e0b72 2827 //\r
8a2d4996 2828 // Get the FVB to access variable store.\r
8a9e0b72 2829 //\r
8a2d4996 2830 Fvb = NULL;\r
f0480ecf 2831 for (Index = 0; Index < HandleCount; Index += 1, Status = EFI_NOT_FOUND, Fvb = NULL) {\r
8a2d4996 2832 Status = GetFvbByHandle (HandleBuffer[Index], &Fvb);\r
8a9e0b72 2833 if (EFI_ERROR (Status)) {\r
2834 Status = EFI_NOT_FOUND;\r
2835 break;\r
2836 }\r
2837\r
2838 //\r
2839 // Ensure this FVB protocol supported Write operation.\r
2840 //\r
2841 Status = Fvb->GetAttributes (Fvb, &Attributes);\r
2842 if (EFI_ERROR (Status) || ((Attributes & EFI_FVB2_WRITE_STATUS) == 0)) {\r
2843 continue; \r
2844 }\r
8a2d4996 2845 \r
8a9e0b72 2846 //\r
8a2d4996 2847 // Compare the address and select the right one.\r
8a9e0b72 2848 //\r
2849 Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);\r
2850 if (EFI_ERROR (Status)) {\r
2851 continue;\r
2852 }\r
2853\r
2854 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvbBaseAddress);\r
8a2d4996 2855 if ((Address >= FvbBaseAddress) && (Address < (FvbBaseAddress + FwVolHeader->FvLength))) {\r
2856 if (FvbHandle != NULL) {\r
2857 *FvbHandle = HandleBuffer[Index];\r
2858 }\r
2859 if (FvbProtocol != NULL) {\r
2860 *FvbProtocol = Fvb;\r
2861 }\r
2862 Status = EFI_SUCCESS;\r
8a9e0b72 2863 break;\r
2864 }\r
2865 }\r
8a9e0b72 2866 FreePool (HandleBuffer);\r
533020ef 2867\r
8a2d4996 2868 if (Fvb == NULL) {\r
2869 Status = EFI_NOT_FOUND;\r
8a9e0b72 2870 }\r
052ad7e1 2871 \r
8a2d4996 2872 return Status; \r
052ad7e1
A
2873}\r
2874\r