Return EFI_WRITE_PROTECTED when setting HwErrRecSupport Global Variable at runtime.
[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
9622df63 6Copyright (c) 2006 - 2012, 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
507 @param UpdatingVariable Pointer to updating variable.\r
7c80e839 508\r
509 @return EFI_OUT_OF_RESOURCES\r
510 @return EFI_SUCCESS\r
511 @return Others\r
512\r
513**/\r
8d3a5c82 514EFI_STATUS\r
8d3a5c82 515Reclaim (\r
516 IN EFI_PHYSICAL_ADDRESS VariableBase,\r
517 OUT UINTN *LastVariableOffset,\r
814bae52 518 IN BOOLEAN IsVolatile,\r
519 IN VARIABLE_HEADER *UpdatingVariable\r
8d3a5c82 520 )\r
8d3a5c82 521{\r
522 VARIABLE_HEADER *Variable;\r
814bae52 523 VARIABLE_HEADER *AddedVariable;\r
8d3a5c82 524 VARIABLE_HEADER *NextVariable;\r
814bae52 525 VARIABLE_HEADER *NextAddedVariable;\r
8d3a5c82 526 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
527 UINT8 *ValidBuffer;\r
814bae52 528 UINTN MaximumBufferSize;\r
8d3a5c82 529 UINTN VariableSize;\r
49e70927 530 UINTN VariableNameSize;\r
531 UINTN UpdatingVariableNameSize;\r
814bae52 532 UINTN NameSize;\r
8d3a5c82 533 UINT8 *CurrPtr;\r
814bae52 534 VOID *Point0;\r
535 VOID *Point1;\r
536 BOOLEAN FoundAdded;\r
8d3a5c82 537 EFI_STATUS Status;\r
49e70927 538 CHAR16 *VariableNamePtr;\r
539 CHAR16 *UpdatingVariableNamePtr;\r
8d3a5c82 540\r
541 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) VariableBase);\r
2fcdca1d 542 //\r
8a2d4996 543 // Recalculate the total size of Common/HwErr type variables in non-volatile area.\r
2fcdca1d 544 //\r
545 if (!IsVolatile) {\r
546 mVariableModuleGlobal->CommonVariableTotalSize = 0;\r
547 mVariableModuleGlobal->HwErrVariableTotalSize = 0;\r
548 }\r
8d3a5c82 549\r
550 //\r
551 // Start Pointers for the variable.\r
552 //\r
814bae52 553 Variable = GetStartPointer (VariableStoreHeader);\r
554 MaximumBufferSize = sizeof (VARIABLE_STORE_HEADER);\r
8d3a5c82 555\r
556 while (IsValidVariableHeader (Variable)) {\r
557 NextVariable = GetNextVariablePtr (Variable);\r
814bae52 558 if (Variable->State == VAR_ADDED || \r
559 Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)\r
560 ) {\r
8d3a5c82 561 VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
814bae52 562 MaximumBufferSize += VariableSize;\r
8d3a5c82 563 }\r
564\r
565 Variable = NextVariable;\r
566 }\r
567\r
814bae52 568 //\r
569 // Reserve the 1 Bytes with Oxff to identify the \r
570 // end of the variable buffer. \r
571 // \r
572 MaximumBufferSize += 1;\r
573 ValidBuffer = AllocatePool (MaximumBufferSize);\r
8d3a5c82 574 if (ValidBuffer == NULL) {\r
575 return EFI_OUT_OF_RESOURCES;\r
576 }\r
577\r
814bae52 578 SetMem (ValidBuffer, MaximumBufferSize, 0xff);\r
8d3a5c82 579\r
580 //\r
8a2d4996 581 // Copy variable store header.\r
8d3a5c82 582 //\r
814bae52 583 CopyMem (ValidBuffer, VariableStoreHeader, sizeof (VARIABLE_STORE_HEADER));\r
584 CurrPtr = (UINT8 *) GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer);\r
8d3a5c82 585\r
814bae52 586 //\r
8a2d4996 587 // Reinstall all ADDED variables as long as they are not identical to Updating Variable.\r
814bae52 588 // \r
589 Variable = GetStartPointer (VariableStoreHeader);\r
8d3a5c82 590 while (IsValidVariableHeader (Variable)) {\r
591 NextVariable = GetNextVariablePtr (Variable);\r
592 if (Variable->State == VAR_ADDED) {\r
5ead4a07 593 if (UpdatingVariable != NULL) {\r
594 if (UpdatingVariable == Variable) {\r
595 Variable = NextVariable;\r
596 continue;\r
597 }\r
49e70927 598\r
599 VariableNameSize = NameSizeOfVariable(Variable);\r
600 UpdatingVariableNameSize = NameSizeOfVariable(UpdatingVariable);\r
601\r
602 VariableNamePtr = GetVariableNamePtr (Variable);\r
603 UpdatingVariableNamePtr = GetVariableNamePtr (UpdatingVariable);\r
5ead4a07 604 if (CompareGuid (&Variable->VendorGuid, &UpdatingVariable->VendorGuid) &&\r
49e70927 605 VariableNameSize == UpdatingVariableNameSize &&\r
606 CompareMem (VariableNamePtr, UpdatingVariableNamePtr, VariableNameSize) == 0 ) {\r
5ead4a07 607 Variable = NextVariable;\r
608 continue;\r
609 }\r
610 }\r
8d3a5c82 611 VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
612 CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);\r
613 CurrPtr += VariableSize;\r
2fcdca1d 614 if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
615 mVariableModuleGlobal->HwErrVariableTotalSize += VariableSize;\r
616 } else if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
617 mVariableModuleGlobal->CommonVariableTotalSize += VariableSize;\r
618 }\r
8d3a5c82 619 }\r
8d3a5c82 620 Variable = NextVariable;\r
621 }\r
5ead4a07 622\r
623 //\r
8a2d4996 624 // Reinstall the variable being updated if it is not NULL.\r
5ead4a07 625 //\r
626 if (UpdatingVariable != NULL) {\r
627 VariableSize = (UINTN)(GetNextVariablePtr (UpdatingVariable)) - (UINTN)UpdatingVariable;\r
628 CopyMem (CurrPtr, (UINT8 *) UpdatingVariable, VariableSize);\r
629 CurrPtr += VariableSize;\r
2fcdca1d 630 if ((!IsVolatile) && ((UpdatingVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
631 mVariableModuleGlobal->HwErrVariableTotalSize += VariableSize;\r
632 } else if ((!IsVolatile) && ((UpdatingVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
633 mVariableModuleGlobal->CommonVariableTotalSize += VariableSize;\r
634 }\r
5ead4a07 635 }\r
636\r
814bae52 637 //\r
8a2d4996 638 // Reinstall all in delete transition variables.\r
814bae52 639 // \r
640 Variable = GetStartPointer (VariableStoreHeader);\r
641 while (IsValidVariableHeader (Variable)) {\r
642 NextVariable = GetNextVariablePtr (Variable);\r
5ead4a07 643 if (Variable != UpdatingVariable && Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
814bae52 644\r
645 //\r
646 // Buffer has cached all ADDED variable. \r
647 // Per IN_DELETED variable, we have to guarantee that\r
648 // no ADDED one in previous buffer. \r
649 // \r
650 \r
651 FoundAdded = FALSE;\r
652 AddedVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer);\r
653 while (IsValidVariableHeader (AddedVariable)) {\r
654 NextAddedVariable = GetNextVariablePtr (AddedVariable);\r
655 NameSize = NameSizeOfVariable (AddedVariable);\r
656 if (CompareGuid (&AddedVariable->VendorGuid, &Variable->VendorGuid) &&\r
657 NameSize == NameSizeOfVariable (Variable)\r
658 ) {\r
659 Point0 = (VOID *) GetVariableNamePtr (AddedVariable);\r
660 Point1 = (VOID *) GetVariableNamePtr (Variable);\r
5ead4a07 661 if (CompareMem (Point0, Point1, NameSizeOfVariable (AddedVariable)) == 0) {\r
814bae52 662 FoundAdded = TRUE;\r
663 break;\r
664 }\r
665 }\r
666 AddedVariable = NextAddedVariable;\r
667 }\r
668 if (!FoundAdded) {\r
5ead4a07 669 //\r
8a2d4996 670 // Promote VAR_IN_DELETED_TRANSITION to VAR_ADDED.\r
5ead4a07 671 //\r
814bae52 672 VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
673 CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);\r
5ead4a07 674 ((VARIABLE_HEADER *) CurrPtr)->State = VAR_ADDED;\r
814bae52 675 CurrPtr += VariableSize;\r
2fcdca1d 676 if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
677 mVariableModuleGlobal->HwErrVariableTotalSize += VariableSize;\r
678 } else if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
679 mVariableModuleGlobal->CommonVariableTotalSize += VariableSize;\r
680 }\r
814bae52 681 }\r
682 }\r
683\r
684 Variable = NextVariable;\r
685 }\r
8d3a5c82 686\r
687 if (IsVolatile) {\r
688 //\r
8a2d4996 689 // If volatile variable store, just copy valid buffer.\r
8d3a5c82 690 //\r
691 SetMem ((UINT8 *) (UINTN) VariableBase, VariableStoreHeader->Size, 0xff);\r
814bae52 692 CopyMem ((UINT8 *) (UINTN) VariableBase, ValidBuffer, (UINTN) (CurrPtr - (UINT8 *) ValidBuffer));\r
8a2d4996 693 Status = EFI_SUCCESS;\r
8d3a5c82 694 } else {\r
695 //\r
696 // If non-volatile variable store, perform FTW here.\r
697 //\r
698 Status = FtwVariableSpace (\r
699 VariableBase,\r
700 ValidBuffer,\r
814bae52 701 (UINTN) (CurrPtr - (UINT8 *) ValidBuffer)\r
8d3a5c82 702 );\r
8a2d4996 703 CopyMem (mNvVariableCache, (CHAR8 *)(UINTN)VariableBase, VariableStoreHeader->Size);\r
8d3a5c82 704 }\r
814bae52 705 if (!EFI_ERROR (Status)) {\r
706 *LastVariableOffset = (UINTN) (CurrPtr - (UINT8 *) ValidBuffer);\r
707 } else {\r
8d3a5c82 708 *LastVariableOffset = 0;\r
709 }\r
710\r
814bae52 711 FreePool (ValidBuffer);\r
712\r
8d3a5c82 713 return Status;\r
714}\r
715\r
0f7aff72
RN
716/**\r
717 Find the variable in the specified variable store.\r
718\r
719 @param VariableName Name of the variable to be found\r
720 @param VendorGuid Vendor GUID to be found.\r
9622df63
SZ
721 @param IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute\r
722 check at runtime when searching variable.\r
0f7aff72
RN
723 @param PtrTrack Variable Track Pointer structure that contains Variable Information.\r
724\r
725 @retval EFI_SUCCESS Variable found successfully\r
726 @retval EFI_NOT_FOUND Variable not found\r
727**/\r
728EFI_STATUS\r
729FindVariableEx (\r
730 IN CHAR16 *VariableName,\r
731 IN EFI_GUID *VendorGuid,\r
9622df63 732 IN BOOLEAN IgnoreRtCheck,\r
0f7aff72
RN
733 IN OUT VARIABLE_POINTER_TRACK *PtrTrack\r
734 )\r
735{\r
736 VARIABLE_HEADER *InDeletedVariable;\r
737 VOID *Point;\r
738\r
739 //\r
740 // Find the variable by walk through HOB, volatile and non-volatile variable store.\r
741 //\r
742 InDeletedVariable = NULL;\r
743\r
744 for ( PtrTrack->CurrPtr = PtrTrack->StartPtr\r
745 ; (PtrTrack->CurrPtr < PtrTrack->EndPtr) && IsValidVariableHeader (PtrTrack->CurrPtr)\r
746 ; PtrTrack->CurrPtr = GetNextVariablePtr (PtrTrack->CurrPtr)\r
747 ) {\r
748 if (PtrTrack->CurrPtr->State == VAR_ADDED || \r
749 PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)\r
750 ) {\r
9622df63 751 if (IgnoreRtCheck || !AtRuntime () || ((PtrTrack->CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {\r
0f7aff72
RN
752 if (VariableName[0] == 0) {\r
753 if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
754 InDeletedVariable = PtrTrack->CurrPtr;\r
755 } else {\r
756 return EFI_SUCCESS;\r
757 }\r
758 } else {\r
759 if (CompareGuid (VendorGuid, &PtrTrack->CurrPtr->VendorGuid)) {\r
760 Point = (VOID *) GetVariableNamePtr (PtrTrack->CurrPtr);\r
761\r
762 ASSERT (NameSizeOfVariable (PtrTrack->CurrPtr) != 0);\r
763 if (CompareMem (VariableName, Point, NameSizeOfVariable (PtrTrack->CurrPtr)) == 0) {\r
764 if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
765 InDeletedVariable = PtrTrack->CurrPtr;\r
766 } else {\r
767 return EFI_SUCCESS;\r
768 }\r
769 }\r
770 }\r
771 }\r
772 }\r
773 }\r
774 }\r
775\r
776 PtrTrack->CurrPtr = InDeletedVariable;\r
777 return (PtrTrack->CurrPtr == NULL) ? EFI_NOT_FOUND : EFI_SUCCESS;\r
778}\r
779\r
33a5a666 780\r
7c80e839 781/**\r
782 Finds variable in storage blocks of volatile and non-volatile storage areas.\r
783\r
784 This code finds variable in storage blocks of volatile and non-volatile storage areas.\r
785 If VariableName is an empty string, then we just return the first\r
786 qualified variable without comparing VariableName and VendorGuid.\r
9622df63
SZ
787 If IgnoreRtCheck is TRUE, then we ignore the EFI_VARIABLE_RUNTIME_ACCESS attribute check\r
788 at runtime when searching existing variable, only VariableName and VendorGuid are compared.\r
789 Otherwise, variables without EFI_VARIABLE_RUNTIME_ACCESS are not visible at runtime.\r
7c80e839 790\r
8a2d4996 791 @param VariableName Name of the variable to be found.\r
7c80e839 792 @param VendorGuid Vendor GUID to be found.\r
793 @param PtrTrack VARIABLE_POINTER_TRACK structure for output,\r
794 including the range searched and the target position.\r
795 @param Global Pointer to VARIABLE_GLOBAL structure, including\r
796 base of volatile variable storage area, base of\r
797 NV variable storage area, and a lock.\r
9622df63
SZ
798 @param IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute\r
799 check at runtime when searching variable.\r
7c80e839 800\r
801 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while\r
8a2d4996 802 VendorGuid is NULL.\r
803 @retval EFI_SUCCESS Variable successfully found.\r
255a3f33 804 @retval EFI_NOT_FOUND Variable not found\r
33a5a666 805\r
7c80e839 806**/\r
8d3a5c82 807EFI_STATUS\r
8d3a5c82 808FindVariable (\r
809 IN CHAR16 *VariableName,\r
810 IN EFI_GUID *VendorGuid,\r
811 OUT VARIABLE_POINTER_TRACK *PtrTrack,\r
9622df63
SZ
812 IN VARIABLE_GLOBAL *Global,\r
813 IN BOOLEAN IgnoreRtCheck\r
8d3a5c82 814 )\r
8d3a5c82 815{\r
0f7aff72
RN
816 EFI_STATUS Status;\r
817 VARIABLE_STORE_HEADER *VariableStoreHeader[VariableStoreTypeMax];\r
818 VARIABLE_STORE_TYPE Type;\r
819\r
820 if (VariableName[0] != 0 && VendorGuid == NULL) {\r
821 return EFI_INVALID_PARAMETER;\r
822 }\r
8d3a5c82 823\r
8d3a5c82 824 //\r
0f7aff72 825 // 0: Volatile, 1: HOB, 2: Non-Volatile.\r
36873a61 826 // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName\r
8a2d4996 827 // make use of this mapping to implement search algorithm.\r
8d3a5c82 828 //\r
0f7aff72
RN
829 VariableStoreHeader[VariableStoreTypeVolatile] = (VARIABLE_STORE_HEADER *) (UINTN) Global->VolatileVariableBase;\r
830 VariableStoreHeader[VariableStoreTypeHob] = (VARIABLE_STORE_HEADER *) (UINTN) Global->HobVariableBase;\r
831 VariableStoreHeader[VariableStoreTypeNv] = mNvVariableCache;\r
8d3a5c82 832\r
833 //\r
0f7aff72 834 // Find the variable by walk through HOB, volatile and non-volatile variable store.\r
8d3a5c82 835 //\r
0f7aff72
RN
836 for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {\r
837 if (VariableStoreHeader[Type] == NULL) {\r
838 continue;\r
839 }\r
814bae52 840\r
0f7aff72
RN
841 PtrTrack->StartPtr = GetStartPointer (VariableStoreHeader[Type]);\r
842 PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader[Type]);\r
843 PtrTrack->Volatile = (BOOLEAN) (Type == VariableStoreTypeVolatile);\r
8d3a5c82 844\r
9622df63 845 Status = FindVariableEx (VariableName, VendorGuid, IgnoreRtCheck, PtrTrack);\r
0f7aff72
RN
846 if (!EFI_ERROR (Status)) {\r
847 return Status;\r
814bae52 848 }\r
8d3a5c82 849 }\r
8d3a5c82 850 return EFI_NOT_FOUND;\r
851}\r
852\r
7c80e839 853/**\r
72399dae 854 Get index from supported language codes according to language string.\r
855\r
856 This code is used to get corresponding index in supported language codes. It can handle\r
0254efc0 857 RFC4646 and ISO639 language tags.\r
72399dae 858 In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.\r
0254efc0 859 In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.\r
72399dae 860\r
861 For example:\r
862 SupportedLang = "engfraengfra"\r
863 Lang = "eng"\r
864 Iso639Language = TRUE\r
865 The return value is "0".\r
866 Another example:\r
867 SupportedLang = "en;fr;en-US;fr-FR"\r
868 Lang = "fr-FR"\r
869 Iso639Language = FALSE\r
870 The return value is "3".\r
871\r
872 @param SupportedLang Platform supported language codes.\r
873 @param Lang Configured language.\r
0254efc0 874 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.\r
72399dae 875\r
8a2d4996 876 @retval The index of language in the language codes.\r
8d3a5c82 877\r
7c80e839 878**/\r
72399dae 879UINTN\r
72399dae 880GetIndexFromSupportedLangCodes(\r
881 IN CHAR8 *SupportedLang,\r
882 IN CHAR8 *Lang,\r
883 IN BOOLEAN Iso639Language\r
884 ) \r
8d3a5c82 885{\r
72399dae 886 UINTN Index;\r
255a3f33
RN
887 UINTN CompareLength;\r
888 UINTN LanguageLength;\r
72399dae 889\r
72399dae 890 if (Iso639Language) {\r
255a3f33 891 CompareLength = ISO_639_2_ENTRY_SIZE;\r
72399dae 892 for (Index = 0; Index < AsciiStrLen (SupportedLang); Index += CompareLength) {\r
893 if (AsciiStrnCmp (Lang, SupportedLang + Index, CompareLength) == 0) {\r
894 //\r
895 // Successfully find the index of Lang string in SupportedLang string.\r
896 //\r
897 Index = Index / CompareLength;\r
898 return Index;\r
899 }\r
900 }\r
901 ASSERT (FALSE);\r
902 return 0;\r
903 } else {\r
904 //\r
0254efc0 905 // Compare RFC4646 language code\r
72399dae 906 //\r
255a3f33
RN
907 Index = 0;\r
908 for (LanguageLength = 0; Lang[LanguageLength] != '\0'; LanguageLength++);\r
909\r
910 for (Index = 0; *SupportedLang != '\0'; Index++, SupportedLang += CompareLength) {\r
72399dae 911 //\r
255a3f33 912 // Skip ';' characters in SupportedLang\r
72399dae 913 //\r
255a3f33
RN
914 for (; *SupportedLang != '\0' && *SupportedLang == ';'; SupportedLang++);\r
915 //\r
916 // Determine the length of the next language code in SupportedLang\r
917 //\r
918 for (CompareLength = 0; SupportedLang[CompareLength] != '\0' && SupportedLang[CompareLength] != ';'; CompareLength++);\r
919 \r
920 if ((CompareLength == LanguageLength) && \r
921 (AsciiStrnCmp (Lang, SupportedLang, CompareLength) == 0)) {\r
72399dae 922 //\r
923 // Successfully find the index of Lang string in SupportedLang string.\r
924 //\r
925 return Index;\r
926 }\r
72399dae 927 }\r
928 ASSERT (FALSE);\r
929 return 0;\r
8d3a5c82 930 }\r
72399dae 931}\r
33a5a666 932\r
72399dae 933/**\r
934 Get language string from supported language codes according to index.\r
935\r
8a2d4996 936 This code is used to get corresponding language strings in supported language codes. It can handle\r
0254efc0 937 RFC4646 and ISO639 language tags.\r
72399dae 938 In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.\r
0254efc0 939 In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.\r
72399dae 940\r
941 For example:\r
942 SupportedLang = "engfraengfra"\r
943 Index = "1"\r
944 Iso639Language = TRUE\r
945 The return value is "fra".\r
946 Another example:\r
947 SupportedLang = "en;fr;en-US;fr-FR"\r
948 Index = "1"\r
949 Iso639Language = FALSE\r
950 The return value is "fr".\r
951\r
952 @param SupportedLang Platform supported language codes.\r
8a2d4996 953 @param Index The index in supported language codes.\r
0254efc0 954 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.\r
72399dae 955\r
8a2d4996 956 @retval The language string in the language codes.\r
8d3a5c82 957\r
72399dae 958**/\r
959CHAR8 *\r
72399dae 960GetLangFromSupportedLangCodes (\r
961 IN CHAR8 *SupportedLang,\r
962 IN UINTN Index,\r
963 IN BOOLEAN Iso639Language\r
964)\r
965{\r
966 UINTN SubIndex;\r
255a3f33 967 UINTN CompareLength;\r
72399dae 968 CHAR8 *Supported;\r
8d3a5c82 969\r
72399dae 970 SubIndex = 0;\r
971 Supported = SupportedLang;\r
972 if (Iso639Language) {\r
973 //\r
8a2d4996 974 // According to the index of Lang string in SupportedLang string to get the language.\r
975 // This code will be invoked in RUNTIME, therefore there is not a memory allocate/free operation.\r
72399dae 976 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.\r
977 //\r
255a3f33
RN
978 CompareLength = ISO_639_2_ENTRY_SIZE;\r
979 mVariableModuleGlobal->Lang[CompareLength] = '\0';\r
72399dae 980 return CopyMem (mVariableModuleGlobal->Lang, SupportedLang + Index * CompareLength, CompareLength);\r
f68af18e 981\r
8d3a5c82 982 } else {\r
72399dae 983 while (TRUE) {\r
984 //\r
8a2d4996 985 // Take semicolon as delimitation, sequentially traverse supported language codes.\r
72399dae 986 //\r
987 for (CompareLength = 0; *Supported != ';' && *Supported != '\0'; CompareLength++) {\r
988 Supported++;\r
989 }\r
990 if ((*Supported == '\0') && (SubIndex != Index)) {\r
991 //\r
992 // Have completed the traverse, but not find corrsponding string.\r
993 // This case is not allowed to happen.\r
994 //\r
995 ASSERT(FALSE);\r
996 return NULL;\r
997 }\r
998 if (SubIndex == Index) {\r
999 //\r
8a2d4996 1000 // According to the index of Lang string in SupportedLang string to get the language.\r
72399dae 1001 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.\r
1002 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.\r
1003 //\r
255a3f33 1004 mVariableModuleGlobal->PlatformLang[CompareLength] = '\0';\r
72399dae 1005 return CopyMem (mVariableModuleGlobal->PlatformLang, Supported - CompareLength, CompareLength);\r
1006 }\r
1007 SubIndex++;\r
8a2d4996 1008\r
5c033766
RN
1009 //\r
1010 // Skip ';' characters in Supported\r
1011 //\r
1012 for (; *Supported != '\0' && *Supported == ';'; Supported++);\r
72399dae 1013 }\r
8d3a5c82 1014 }\r
8d3a5c82 1015}\r
1016\r
255a3f33
RN
1017/**\r
1018 Returns a pointer to an allocated buffer that contains the best matching language \r
1019 from a set of supported languages. \r
1020 \r
1021 This function supports both ISO 639-2 and RFC 4646 language codes, but language \r
1022 code types may not be mixed in a single call to this function. This function\r
1023 supports a variable argument list that allows the caller to pass in a prioritized\r
1024 list of language codes to test against all the language codes in SupportedLanguages.\r
1025\r
1026 If SupportedLanguages is NULL, then ASSERT().\r
1027\r
1028 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that\r
1029 contains a set of language codes in the format \r
1030 specified by Iso639Language.\r
1031 @param[in] Iso639Language If TRUE, then all language codes are assumed to be\r
1032 in ISO 639-2 format. If FALSE, then all language\r
1033 codes are assumed to be in RFC 4646 language format\r
1034 @param[in] ... A variable argument list that contains pointers to \r
1035 Null-terminated ASCII strings that contain one or more\r
1036 language codes in the format specified by Iso639Language.\r
1037 The first language code from each of these language\r
1038 code lists is used to determine if it is an exact or\r
1039 close match to any of the language codes in \r
1040 SupportedLanguages. Close matches only apply to RFC 4646\r
1041 language codes, and the matching algorithm from RFC 4647\r
1042 is used to determine if a close match is present. If \r
1043 an exact or close match is found, then the matching\r
1044 language code from SupportedLanguages is returned. If\r
1045 no matches are found, then the next variable argument\r
1046 parameter is evaluated. The variable argument list \r
1047 is terminated by a NULL.\r
1048\r
1049 @retval NULL The best matching language could not be found in SupportedLanguages.\r
1050 @retval NULL There are not enough resources available to return the best matching \r
1051 language.\r
1052 @retval Other A pointer to a Null-terminated ASCII string that is the best matching \r
1053 language in SupportedLanguages.\r
1054\r
1055**/\r
1056CHAR8 *\r
e1adae60 1057EFIAPI\r
255a3f33
RN
1058VariableGetBestLanguage (\r
1059 IN CONST CHAR8 *SupportedLanguages, \r
1060 IN BOOLEAN Iso639Language,\r
1061 ...\r
1062 )\r
1063{\r
1064 VA_LIST Args;\r
1065 CHAR8 *Language;\r
1066 UINTN CompareLength;\r
1067 UINTN LanguageLength;\r
1068 CONST CHAR8 *Supported;\r
1069 CHAR8 *Buffer;\r
1070\r
1071 ASSERT (SupportedLanguages != NULL);\r
1072\r
1073 VA_START (Args, Iso639Language);\r
1074 while ((Language = VA_ARG (Args, CHAR8 *)) != NULL) {\r
1075 //\r
1076 // Default to ISO 639-2 mode\r
1077 //\r
1078 CompareLength = 3;\r
1079 LanguageLength = MIN (3, AsciiStrLen (Language));\r
1080\r
1081 //\r
1082 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language\r
1083 //\r
1084 if (!Iso639Language) {\r
1085 for (LanguageLength = 0; Language[LanguageLength] != 0 && Language[LanguageLength] != ';'; LanguageLength++);\r
1086 }\r
1087\r
1088 //\r
1089 // Trim back the length of Language used until it is empty\r
1090 //\r
1091 while (LanguageLength > 0) {\r
1092 //\r
1093 // Loop through all language codes in SupportedLanguages\r
1094 //\r
1095 for (Supported = SupportedLanguages; *Supported != '\0'; Supported += CompareLength) {\r
1096 //\r
1097 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages\r
1098 //\r
1099 if (!Iso639Language) {\r
1100 //\r
1101 // Skip ';' characters in Supported\r
1102 //\r
1103 for (; *Supported != '\0' && *Supported == ';'; Supported++);\r
1104 //\r
1105 // Determine the length of the next language code in Supported\r
1106 //\r
1107 for (CompareLength = 0; Supported[CompareLength] != 0 && Supported[CompareLength] != ';'; CompareLength++);\r
1108 //\r
1109 // If Language is longer than the Supported, then skip to the next language\r
1110 //\r
1111 if (LanguageLength > CompareLength) {\r
1112 continue;\r
1113 }\r
1114 }\r
1115 //\r
1116 // See if the first LanguageLength characters in Supported match Language\r
1117 //\r
1118 if (AsciiStrnCmp (Supported, Language, LanguageLength) == 0) {\r
1119 VA_END (Args);\r
1120\r
1121 Buffer = Iso639Language ? mVariableModuleGlobal->Lang : mVariableModuleGlobal->PlatformLang;\r
1122 Buffer[CompareLength] = '\0';\r
1123 return CopyMem (Buffer, Supported, CompareLength);\r
1124 }\r
1125 }\r
1126\r
1127 if (Iso639Language) {\r
1128 //\r
1129 // If ISO 639 mode, then each language can only be tested once\r
1130 //\r
1131 LanguageLength = 0;\r
1132 } else {\r
1133 //\r
1134 // If RFC 4646 mode, then trim Language from the right to the next '-' character \r
1135 //\r
1136 for (LanguageLength--; LanguageLength > 0 && Language[LanguageLength] != '-'; LanguageLength--);\r
1137 }\r
1138 }\r
1139 }\r
1140 VA_END (Args);\r
1141\r
1142 //\r
1143 // No matches were found \r
1144 //\r
1145 return NULL;\r
1146}\r
1147\r
72399dae 1148/**\r
1149 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.\r
052ad7e1 1150\r
72399dae 1151 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.\r
052ad7e1 1152\r
72399dae 1153 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,\r
1154 and are read-only. Therefore, in variable driver, only store the original value for other use.\r
8d3a5c82 1155\r
8a2d4996 1156 @param[in] VariableName Name of variable.\r
8d3a5c82 1157\r
8a2d4996 1158 @param[in] Data Variable data.\r
8d3a5c82 1159\r
8a2d4996 1160 @param[in] DataSize Size of data. 0 means delete.\r
72399dae 1161\r
7c80e839 1162**/\r
255a3f33 1163VOID\r
d6550260 1164AutoUpdateLangVariable (\r
72399dae 1165 IN CHAR16 *VariableName,\r
1166 IN VOID *Data,\r
1167 IN UINTN DataSize\r
052ad7e1 1168 )\r
8d3a5c82 1169{\r
255a3f33
RN
1170 EFI_STATUS Status;\r
1171 CHAR8 *BestPlatformLang;\r
1172 CHAR8 *BestLang;\r
1173 UINTN Index;\r
1174 UINT32 Attributes;\r
72399dae 1175 VARIABLE_POINTER_TRACK Variable;\r
255a3f33 1176 BOOLEAN SetLanguageCodes;\r
8d3a5c82 1177\r
72399dae 1178 //\r
255a3f33 1179 // Don't do updates for delete operation\r
72399dae 1180 //\r
255a3f33
RN
1181 if (DataSize == 0) {\r
1182 return;\r
1183 }\r
1184\r
1185 SetLanguageCodes = FALSE;\r
8d3a5c82 1186\r
72399dae 1187 if (StrCmp (VariableName, L"PlatformLangCodes") == 0) {\r
255a3f33
RN
1188 //\r
1189 // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.\r
1190 //\r
8a2d4996 1191 if (AtRuntime ()) {\r
255a3f33
RN
1192 return;\r
1193 }\r
1194\r
1195 SetLanguageCodes = TRUE;\r
1196\r
72399dae 1197 //\r
1198 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only\r
1199 // Therefore, in variable driver, only store the original value for other use.\r
1200 //\r
255a3f33
RN
1201 if (mVariableModuleGlobal->PlatformLangCodes != NULL) {\r
1202 FreePool (mVariableModuleGlobal->PlatformLangCodes);\r
1203 }\r
1204 mVariableModuleGlobal->PlatformLangCodes = AllocateRuntimeCopyPool (DataSize, Data);\r
1205 ASSERT (mVariableModuleGlobal->PlatformLangCodes != NULL);\r
1206\r
72399dae 1207 //\r
255a3f33
RN
1208 // PlatformLang holds a single language from PlatformLangCodes, \r
1209 // so the size of PlatformLangCodes is enough for the PlatformLang.\r
72399dae 1210 //\r
255a3f33
RN
1211 if (mVariableModuleGlobal->PlatformLang != NULL) {\r
1212 FreePool (mVariableModuleGlobal->PlatformLang);\r
1213 }\r
1214 mVariableModuleGlobal->PlatformLang = AllocateRuntimePool (DataSize);\r
1215 ASSERT (mVariableModuleGlobal->PlatformLang != NULL);\r
fdb7765f 1216\r
255a3f33 1217 } else if (StrCmp (VariableName, L"LangCodes") == 0) {\r
72399dae 1218 //\r
255a3f33 1219 // LangCodes is a volatile variable, so it can not be updated at runtime.\r
72399dae 1220 //\r
8a2d4996 1221 if (AtRuntime ()) {\r
255a3f33
RN
1222 return;\r
1223 }\r
1224\r
1225 SetLanguageCodes = TRUE;\r
8d3a5c82 1226\r
8d3a5c82 1227 //\r
255a3f33
RN
1228 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only\r
1229 // Therefore, in variable driver, only store the original value for other use.\r
8d3a5c82 1230 //\r
255a3f33
RN
1231 if (mVariableModuleGlobal->LangCodes != NULL) {\r
1232 FreePool (mVariableModuleGlobal->LangCodes);\r
1233 }\r
1234 mVariableModuleGlobal->LangCodes = AllocateRuntimeCopyPool (DataSize, Data);\r
1235 ASSERT (mVariableModuleGlobal->LangCodes != NULL);\r
1236 }\r
8d3a5c82 1237\r
255a3f33
RN
1238 if (SetLanguageCodes \r
1239 && (mVariableModuleGlobal->PlatformLangCodes != NULL)\r
1240 && (mVariableModuleGlobal->LangCodes != NULL)) {\r
8d3a5c82 1241 //\r
255a3f33
RN
1242 // Update Lang if PlatformLang is already set\r
1243 // Update PlatformLang if Lang is already set\r
8d3a5c82 1244 //\r
9622df63 1245 Status = FindVariable (L"PlatformLang", &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
255a3f33
RN
1246 if (!EFI_ERROR (Status)) {\r
1247 //\r
1248 // Update Lang\r
1249 //\r
1250 VariableName = L"PlatformLang";\r
1251 Data = GetVariableDataPtr (Variable.CurrPtr);\r
1252 DataSize = Variable.CurrPtr->DataSize;\r
1253 } else {\r
9622df63 1254 Status = FindVariable (L"Lang", &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
255a3f33
RN
1255 if (!EFI_ERROR (Status)) {\r
1256 //\r
1257 // Update PlatformLang\r
1258 //\r
1259 VariableName = L"Lang";\r
1260 Data = GetVariableDataPtr (Variable.CurrPtr);\r
1261 DataSize = Variable.CurrPtr->DataSize;\r
1262 } else {\r
1263 //\r
1264 // Neither PlatformLang nor Lang is set, directly return\r
1265 //\r
1266 return;\r
1267 }\r
1268 }\r
1269 }\r
1270 \r
1271 //\r
1272 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.\r
1273 //\r
1274 Attributes = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;\r
8d3a5c82 1275\r
255a3f33 1276 if (StrCmp (VariableName, L"PlatformLang") == 0) {\r
8d3a5c82 1277 //\r
255a3f33 1278 // Update Lang when PlatformLangCodes/LangCodes were set.\r
8d3a5c82 1279 //\r
255a3f33
RN
1280 if ((mVariableModuleGlobal->PlatformLangCodes != NULL) && (mVariableModuleGlobal->LangCodes != NULL)) {\r
1281 //\r
1282 // When setting PlatformLang, firstly get most matched language string from supported language codes.\r
1283 //\r
1284 BestPlatformLang = VariableGetBestLanguage (mVariableModuleGlobal->PlatformLangCodes, FALSE, Data, NULL);\r
1285 if (BestPlatformLang != NULL) {\r
1286 //\r
1287 // Get the corresponding index in language codes.\r
1288 //\r
1289 Index = GetIndexFromSupportedLangCodes (mVariableModuleGlobal->PlatformLangCodes, BestPlatformLang, FALSE);\r
fdb7765f 1290\r
255a3f33
RN
1291 //\r
1292 // Get the corresponding ISO639 language tag according to RFC4646 language tag.\r
1293 //\r
1294 BestLang = GetLangFromSupportedLangCodes (mVariableModuleGlobal->LangCodes, Index, TRUE);\r
8d3a5c82 1295\r
255a3f33
RN
1296 //\r
1297 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.\r
1298 //\r
9622df63 1299 FindVariable (L"Lang", &gEfiGlobalVariableGuid, &Variable, (VARIABLE_GLOBAL *)mVariableModuleGlobal, FALSE);\r
8d3a5c82 1300\r
8a2d4996 1301 Status = UpdateVariable (L"Lang", &gEfiGlobalVariableGuid, BestLang,\r
1302 ISO_639_2_ENTRY_SIZE + 1, Attributes, &Variable);\r
8d3a5c82 1303\r
255a3f33 1304 DEBUG ((EFI_D_INFO, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a\n", BestPlatformLang, BestLang));\r
72399dae 1305\r
255a3f33
RN
1306 ASSERT_EFI_ERROR(Status);\r
1307 }\r
1308 }\r
72399dae 1309\r
255a3f33 1310 } else if (StrCmp (VariableName, L"Lang") == 0) {\r
72399dae 1311 //\r
255a3f33 1312 // Update PlatformLang when PlatformLangCodes/LangCodes were set.\r
72399dae 1313 //\r
255a3f33
RN
1314 if ((mVariableModuleGlobal->PlatformLangCodes != NULL) && (mVariableModuleGlobal->LangCodes != NULL)) {\r
1315 //\r
1316 // When setting Lang, firstly get most matched language string from supported language codes.\r
1317 //\r
1318 BestLang = VariableGetBestLanguage (mVariableModuleGlobal->LangCodes, TRUE, Data, NULL);\r
1319 if (BestLang != NULL) {\r
1320 //\r
1321 // Get the corresponding index in language codes.\r
1322 //\r
1323 Index = GetIndexFromSupportedLangCodes (mVariableModuleGlobal->LangCodes, BestLang, TRUE);\r
72399dae 1324\r
255a3f33
RN
1325 //\r
1326 // Get the corresponding RFC4646 language tag according to ISO639 language tag.\r
1327 //\r
1328 BestPlatformLang = GetLangFromSupportedLangCodes (mVariableModuleGlobal->PlatformLangCodes, Index, FALSE);\r
1329\r
1330 //\r
1331 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.\r
1332 //\r
9622df63 1333 FindVariable (L"PlatformLang", &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
72399dae 1334\r
255a3f33
RN
1335 Status = UpdateVariable (L"PlatformLang", &gEfiGlobalVariableGuid, BestPlatformLang, \r
1336 AsciiStrSize (BestPlatformLang), Attributes, &Variable);\r
72399dae 1337\r
255a3f33
RN
1338 DEBUG ((EFI_D_INFO, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a\n", BestLang, BestPlatformLang));\r
1339 ASSERT_EFI_ERROR (Status);\r
1340 }\r
1341 }\r
72399dae 1342 }\r
8d3a5c82 1343}\r
1344\r
7c80e839 1345/**\r
72399dae 1346 Update the variable region with Variable information. These are the same \r
1347 arguments as the EFI Variable services.\r
052ad7e1 1348\r
8a2d4996 1349 @param[in] VariableName Name of variable.\r
1350 @param[in] VendorGuid Guid of variable.\r
1351 @param[in] Data Variable data.\r
1352 @param[in] DataSize Size of data. 0 means delete.\r
1353 @param[in] Attributes Attribues of the variable.\r
1354 @param[in] CacheVariable The variable information which is used to keep track of variable usage.\r
1355 \r
72399dae 1356 @retval EFI_SUCCESS The update operation is success.\r
72399dae 1357 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.\r
8d3a5c82 1358\r
7c80e839 1359**/\r
052ad7e1 1360EFI_STATUS\r
72399dae 1361UpdateVariable (\r
8a2d4996 1362 IN CHAR16 *VariableName,\r
1363 IN EFI_GUID *VendorGuid,\r
1364 IN VOID *Data,\r
1365 IN UINTN DataSize,\r
1366 IN UINT32 Attributes OPTIONAL,\r
1367 IN VARIABLE_POINTER_TRACK *CacheVariable\r
052ad7e1 1368 )\r
8d3a5c82 1369{\r
8a9e0b72 1370 EFI_STATUS Status;\r
1371 VARIABLE_HEADER *NextVariable;\r
72399dae 1372 UINTN ScratchSize;\r
1373 UINTN NonVolatileVarableStoreSize;\r
8a9e0b72 1374 UINTN VarNameOffset;\r
1375 UINTN VarDataOffset;\r
72399dae 1376 UINTN VarNameSize;\r
8a9e0b72 1377 UINTN VarSize;\r
72399dae 1378 BOOLEAN Volatile;\r
1379 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r
8a9e0b72 1380 UINT8 State;\r
1381 BOOLEAN Reclaimed;\r
8a2d4996 1382 VARIABLE_POINTER_TRACK *Variable;\r
1383 VARIABLE_POINTER_TRACK NvVariable;\r
1384 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
1385 UINTN CacheOffset;\r
fdb7765f 1386\r
5456306f 1387 if ((mVariableModuleGlobal->FvbInstance == NULL) && ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0)) {\r
1388 //\r
1389 // The FVB protocol is not ready. Trying to update NV variable prior to the installation\r
1390 // of EFI_VARIABLE_WRITE_ARCH_PROTOCOL.\r
1391 //\r
1392 return EFI_NOT_AVAILABLE_YET; \r
1393 }\r
1394\r
1395 if ((CacheVariable->CurrPtr == NULL) || CacheVariable->Volatile) {\r
8a2d4996 1396 Variable = CacheVariable;\r
1397 } else {\r
8a2d4996 1398 //\r
5456306f 1399 // Update/Delete existing NV variable.\r
8a2d4996 1400 // CacheVariable points to the variable in the memory copy of Flash area\r
1401 // Now let Variable points to the same variable in Flash area.\r
1402 //\r
1403 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);\r
1404 Variable = &NvVariable; \r
1405 Variable->StartPtr = GetStartPointer (VariableStoreHeader);\r
1406 Variable->EndPtr = GetEndPointer (VariableStoreHeader);\r
5456306f 1407 Variable->CurrPtr = (VARIABLE_HEADER *)((UINTN)Variable->StartPtr + ((UINTN)CacheVariable->CurrPtr - (UINTN)CacheVariable->StartPtr));\r
1408 Variable->Volatile = FALSE;\r
1409 } \r
1410\r
1411 Fvb = mVariableModuleGlobal->FvbInstance;\r
1412 Reclaimed = FALSE;\r
fdb7765f 1413\r
72399dae 1414 if (Variable->CurrPtr != NULL) {\r
8d3a5c82 1415 //\r
8a2d4996 1416 // Update/Delete existing variable.\r
8d3a5c82 1417 //\r
8a2d4996 1418 if (AtRuntime ()) { \r
c6492839 1419 //\r
8a2d4996 1420 // If AtRuntime and the variable is Volatile and Runtime Access, \r
c6492839 1421 // the volatile is ReadOnly, and SetVariable should be aborted and \r
1422 // return EFI_WRITE_PROTECTED.\r
1423 //\r
72399dae 1424 if (Variable->Volatile) {\r
c6492839 1425 Status = EFI_WRITE_PROTECTED;\r
1426 goto Done;\r
1427 }\r
1428 //\r
9622df63 1429 // Only variable that have NV|RT attributes can be updated/deleted in Runtime.\r
c6492839 1430 //\r
9622df63 1431 if (((Variable->CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) || ((Variable->CurrPtr->Attributes & EFI_VARIABLE_NON_VOLATILE) == 0)) {\r
c6492839 1432 Status = EFI_INVALID_PARAMETER;\r
1433 goto Done; \r
1434 }\r
1435 }\r
8a2d4996 1436\r
8d3a5c82 1437 //\r
c6492839 1438 // Setting a data variable with no access, or zero DataSize attributes\r
8a2d4996 1439 // causes it to be deleted.\r
8d3a5c82 1440 //\r
c6492839 1441 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) { \r
72399dae 1442 State = Variable->CurrPtr->State;\r
c6492839 1443 State &= VAR_DELETED;\r
1444\r
1445 Status = UpdateVariableStore (\r
052ad7e1 1446 &mVariableModuleGlobal->VariableGlobal,\r
72399dae 1447 Variable->Volatile,\r
c6492839 1448 FALSE,\r
8a9e0b72 1449 Fvb,\r
72399dae 1450 (UINTN) &Variable->CurrPtr->State,\r
c6492839 1451 sizeof (UINT8),\r
1452 &State\r
1453 ); \r
33a5a666 1454 if (!EFI_ERROR (Status)) {\r
8a2d4996 1455 UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, FALSE, TRUE, FALSE);\r
1456 if (!Variable->Volatile) {\r
1457 CacheVariable->CurrPtr->State = State;\r
1458 }\r
33a5a666 1459 }\r
c6492839 1460 goto Done; \r
1461 }\r
8d3a5c82 1462 //\r
8a2d4996 1463 // If the variable is marked valid, and the same data has been passed in,\r
c6492839 1464 // then return to the caller immediately.\r
8d3a5c82 1465 //\r
72399dae 1466 if (DataSizeOfVariable (Variable->CurrPtr) == DataSize &&\r
1467 (CompareMem (Data, GetVariableDataPtr (Variable->CurrPtr), DataSize) == 0)) {\r
33a5a666 1468 \r
8a2d4996 1469 UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, TRUE, FALSE, FALSE);\r
c6492839 1470 Status = EFI_SUCCESS;\r
1471 goto Done;\r
72399dae 1472 } else if ((Variable->CurrPtr->State == VAR_ADDED) ||\r
1473 (Variable->CurrPtr->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {\r
814bae52 1474\r
c6492839 1475 //\r
8a2d4996 1476 // Mark the old variable as in delete transition.\r
c6492839 1477 //\r
72399dae 1478 State = Variable->CurrPtr->State;\r
c6492839 1479 State &= VAR_IN_DELETED_TRANSITION;\r
1480\r
1481 Status = UpdateVariableStore (\r
052ad7e1 1482 &mVariableModuleGlobal->VariableGlobal,\r
72399dae 1483 Variable->Volatile,\r
c6492839 1484 FALSE,\r
8a9e0b72 1485 Fvb,\r
72399dae 1486 (UINTN) &Variable->CurrPtr->State,\r
c6492839 1487 sizeof (UINT8),\r
1488 &State\r
1489 ); \r
1490 if (EFI_ERROR (Status)) {\r
1491 goto Done; \r
33a5a666 1492 } \r
8a2d4996 1493 if (!Variable->Volatile) {\r
1494 CacheVariable->CurrPtr->State = State;\r
1495 }\r
c6492839 1496 } \r
72399dae 1497 } else {\r
8d3a5c82 1498 //\r
8a2d4996 1499 // Not found existing variable. Create a new variable.\r
c6492839 1500 // \r
1501 \r
8d3a5c82 1502 //\r
c6492839 1503 // Make sure we are trying to create a new variable.\r
8a2d4996 1504 // Setting a data variable with zero DataSize or no access attributes means to delete it. \r
8d3a5c82 1505 //\r
c6492839 1506 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {\r
1507 Status = EFI_NOT_FOUND;\r
1508 goto Done;\r
1509 }\r
1510 \r
8d3a5c82 1511 //\r
8a2d4996 1512 // Only variable have NV|RT attribute can be created in Runtime.\r
c6492839 1513 //\r
8a2d4996 1514 if (AtRuntime () &&\r
45f6c85b 1515 (((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) || ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0))) {\r
c6492839 1516 Status = EFI_INVALID_PARAMETER;\r
1517 goto Done;\r
1518 } \r
c6492839 1519 }\r
1520\r
1521 //\r
1522 // Function part - create a new variable and copy the data.\r
1523 // Both update a variable and create a variable will come here.\r
8a2d4996 1524\r
c6492839 1525 //\r
1526 // Tricky part: Use scratch data area at the end of volatile variable store\r
1527 // as a temporary storage.\r
1528 //\r
052ad7e1 1529 NextVariable = GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase));\r
188e4e84 1530 ScratchSize = MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize));\r
c6492839 1531\r
2fcdca1d 1532 SetMem (NextVariable, ScratchSize, 0xff);\r
c6492839 1533\r
1534 NextVariable->StartId = VARIABLE_DATA;\r
1535 NextVariable->Attributes = Attributes;\r
1536 //\r
1537 // NextVariable->State = VAR_ADDED;\r
1538 //\r
8a2d4996 1539 NextVariable->Reserved = 0;\r
1540 VarNameOffset = sizeof (VARIABLE_HEADER);\r
1541 VarNameSize = StrSize (VariableName);\r
c6492839 1542 CopyMem (\r
1543 (UINT8 *) ((UINTN) NextVariable + VarNameOffset),\r
1544 VariableName,\r
1545 VarNameSize\r
1546 );\r
1547 VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);\r
1548 CopyMem (\r
1549 (UINT8 *) ((UINTN) NextVariable + VarDataOffset),\r
1550 Data,\r
1551 DataSize\r
1552 );\r
1553 CopyMem (&NextVariable->VendorGuid, VendorGuid, sizeof (EFI_GUID));\r
1554 //\r
1555 // There will be pad bytes after Data, the NextVariable->NameSize and\r
1556 // NextVariable->DataSize should not include pad size so that variable\r
8a2d4996 1557 // service can get actual size in GetVariable.\r
c6492839 1558 //\r
1559 NextVariable->NameSize = (UINT32)VarNameSize;\r
1560 NextVariable->DataSize = (UINT32)DataSize;\r
1561\r
1562 //\r
1563 // The actual size of the variable that stores in storage should\r
1564 // include pad size.\r
1565 //\r
1566 VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);\r
45f6c85b 1567 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
8d3a5c82 1568 //\r
8a2d4996 1569 // Create a nonvolatile variable.\r
8d3a5c82 1570 //\r
fd51bf70 1571 Volatile = FALSE;\r
2fcdca1d 1572 NonVolatileVarableStoreSize = ((VARIABLE_STORE_HEADER *)(UINTN)(mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase))->Size;\r
1573 if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) \r
188e4e84 1574 && ((VarSize + mVariableModuleGlobal->HwErrVariableTotalSize) > PcdGet32 (PcdHwErrStorageSize)))\r
2fcdca1d 1575 || (((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0) \r
188e4e84 1576 && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > NonVolatileVarableStoreSize - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize)))) {\r
8a2d4996 1577 if (AtRuntime ()) {\r
c6492839 1578 Status = EFI_OUT_OF_RESOURCES;\r
1579 goto Done;\r
1580 }\r
1581 //\r
8a2d4996 1582 // Perform garbage collection & reclaim operation.\r
c6492839 1583 //\r
72399dae 1584 Status = Reclaim (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase, \r
1585 &mVariableModuleGlobal->NonVolatileLastVariableOffset, FALSE, Variable->CurrPtr);\r
8d3a5c82 1586 if (EFI_ERROR (Status)) {\r
1587 goto Done;\r
1588 }\r
8d3a5c82 1589 //\r
8a2d4996 1590 // If still no enough space, return out of resources.\r
8d3a5c82 1591 //\r
2fcdca1d 1592 if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) \r
188e4e84 1593 && ((VarSize + mVariableModuleGlobal->HwErrVariableTotalSize) > PcdGet32 (PcdHwErrStorageSize)))\r
2fcdca1d 1594 || (((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0) \r
188e4e84 1595 && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > NonVolatileVarableStoreSize - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize)))) {\r
c6492839 1596 Status = EFI_OUT_OF_RESOURCES;\r
8d3a5c82 1597 goto Done;\r
8d3a5c82 1598 }\r
c6492839 1599 Reclaimed = TRUE;\r
8d3a5c82 1600 }\r
1601 //\r
8a2d4996 1602 // Four steps\r
c6492839 1603 // 1. Write variable header\r
130e2569 1604 // 2. Set variable state to header valid \r
1605 // 3. Write variable data\r
1606 // 4. Set variable state to valid\r
8d3a5c82 1607 //\r
8d3a5c82 1608 //\r
c6492839 1609 // Step 1:\r
8d3a5c82 1610 //\r
8a2d4996 1611 CacheOffset = mVariableModuleGlobal->NonVolatileLastVariableOffset;\r
c6492839 1612 Status = UpdateVariableStore (\r
052ad7e1 1613 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 1614 FALSE,\r
1615 TRUE,\r
8a9e0b72 1616 Fvb,\r
72399dae 1617 mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
c6492839 1618 sizeof (VARIABLE_HEADER),\r
1619 (UINT8 *) NextVariable\r
1620 );\r
1621\r
1622 if (EFI_ERROR (Status)) {\r
1623 goto Done;\r
1624 }\r
130e2569 1625\r
8d3a5c82 1626 //\r
c6492839 1627 // Step 2:\r
8d3a5c82 1628 //\r
130e2569 1629 NextVariable->State = VAR_HEADER_VALID_ONLY;\r
1630 Status = UpdateVariableStore (\r
1631 &mVariableModuleGlobal->VariableGlobal,\r
1632 FALSE,\r
1633 TRUE,\r
8a9e0b72 1634 Fvb,\r
8a2d4996 1635 mVariableModuleGlobal->NonVolatileLastVariableOffset + OFFSET_OF (VARIABLE_HEADER, State),\r
1636 sizeof (UINT8),\r
1637 &NextVariable->State\r
130e2569 1638 );\r
1639\r
1640 if (EFI_ERROR (Status)) {\r
1641 goto Done;\r
1642 }\r
1643 //\r
1644 // Step 3:\r
1645 //\r
c6492839 1646 Status = UpdateVariableStore (\r
052ad7e1 1647 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 1648 FALSE,\r
1649 TRUE,\r
8a9e0b72 1650 Fvb,\r
72399dae 1651 mVariableModuleGlobal->NonVolatileLastVariableOffset + sizeof (VARIABLE_HEADER),\r
c6492839 1652 (UINT32) VarSize - sizeof (VARIABLE_HEADER),\r
1653 (UINT8 *) NextVariable + sizeof (VARIABLE_HEADER)\r
1654 );\r
1655\r
1656 if (EFI_ERROR (Status)) {\r
1657 goto Done;\r
1658 }\r
8d3a5c82 1659 //\r
130e2569 1660 // Step 4:\r
8d3a5c82 1661 //\r
c6492839 1662 NextVariable->State = VAR_ADDED;\r
1663 Status = UpdateVariableStore (\r
052ad7e1 1664 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 1665 FALSE,\r
1666 TRUE,\r
8a9e0b72 1667 Fvb,\r
8a2d4996 1668 mVariableModuleGlobal->NonVolatileLastVariableOffset + OFFSET_OF (VARIABLE_HEADER, State),\r
1669 sizeof (UINT8),\r
1670 &NextVariable->State\r
c6492839 1671 );\r
1672\r
1673 if (EFI_ERROR (Status)) {\r
1674 goto Done;\r
1675 }\r
8d3a5c82 1676\r
72399dae 1677 mVariableModuleGlobal->NonVolatileLastVariableOffset += HEADER_ALIGN (VarSize);\r
8d3a5c82 1678\r
2fcdca1d 1679 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {\r
1680 mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VarSize);\r
1681 } else {\r
1682 mVariableModuleGlobal->CommonVariableTotalSize += HEADER_ALIGN (VarSize);\r
1683 }\r
8a2d4996 1684 //\r
1685 // update the memory copy of Flash region.\r
1686 //\r
1687 CopyMem ((UINT8 *)mNvVariableCache + CacheOffset, (UINT8 *)NextVariable, VarSize);\r
c6492839 1688 } else {\r
1689 //\r
8a2d4996 1690 // Create a volatile variable.\r
c6492839 1691 // \r
fd51bf70 1692 Volatile = TRUE;\r
c6492839 1693\r
72399dae 1694 if ((UINT32) (VarSize + mVariableModuleGlobal->VolatileLastVariableOffset) >\r
052ad7e1 1695 ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size) {\r
8d3a5c82 1696 //\r
8a2d4996 1697 // Perform garbage collection & reclaim operation.\r
8d3a5c82 1698 //\r
72399dae 1699 Status = Reclaim (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase, \r
1700 &mVariableModuleGlobal->VolatileLastVariableOffset, TRUE, Variable->CurrPtr);\r
8d3a5c82 1701 if (EFI_ERROR (Status)) {\r
1702 goto Done;\r
1703 }\r
1704 //\r
8a2d4996 1705 // If still no enough space, return out of resources.\r
8d3a5c82 1706 //\r
72399dae 1707 if ((UINT32) (VarSize + mVariableModuleGlobal->VolatileLastVariableOffset) >\r
052ad7e1 1708 ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size\r
8d3a5c82 1709 ) {\r
c6492839 1710 Status = EFI_OUT_OF_RESOURCES;\r
8d3a5c82 1711 goto Done;\r
1712 }\r
c6492839 1713 Reclaimed = TRUE;\r
8d3a5c82 1714 }\r
8d3a5c82 1715\r
c6492839 1716 NextVariable->State = VAR_ADDED;\r
1717 Status = UpdateVariableStore (\r
052ad7e1 1718 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 1719 TRUE,\r
1720 TRUE,\r
8a9e0b72 1721 Fvb,\r
72399dae 1722 mVariableModuleGlobal->VolatileLastVariableOffset,\r
c6492839 1723 (UINT32) VarSize,\r
1724 (UINT8 *) NextVariable\r
1725 );\r
1726\r
1727 if (EFI_ERROR (Status)) {\r
1728 goto Done;\r
8d3a5c82 1729 }\r
c6492839 1730\r
72399dae 1731 mVariableModuleGlobal->VolatileLastVariableOffset += HEADER_ALIGN (VarSize);\r
c6492839 1732 }\r
72399dae 1733\r
c6492839 1734 //\r
8a2d4996 1735 // Mark the old variable as deleted.\r
c6492839 1736 //\r
72399dae 1737 if (!Reclaimed && !EFI_ERROR (Status) && Variable->CurrPtr != NULL) {\r
1738 State = Variable->CurrPtr->State;\r
c6492839 1739 State &= VAR_DELETED;\r
1740\r
1741 Status = UpdateVariableStore (\r
72399dae 1742 &mVariableModuleGlobal->VariableGlobal,\r
1743 Variable->Volatile,\r
1744 FALSE,\r
1745 Fvb,\r
1746 (UINTN) &Variable->CurrPtr->State,\r
1747 sizeof (UINT8),\r
1748 &State\r
1749 );\r
8a2d4996 1750 if (!EFI_ERROR (Status) && !Variable->Volatile) { \r
1751 CacheVariable->CurrPtr->State = State;\r
1752 }\r
72399dae 1753 }\r
1754\r
1755 if (!EFI_ERROR (Status)) {\r
1756 UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);\r
72399dae 1757 }\r
1758\r
1759Done:\r
1760 return Status;\r
1761}\r
1762\r
1763/**\r
1764\r
1765 This code finds variable in storage blocks (Volatile or Non-Volatile).\r
1766\r
1767 @param VariableName Name of Variable to be found.\r
1768 @param VendorGuid Variable vendor GUID.\r
1769 @param Attributes Attribute value of the variable found.\r
1770 @param DataSize Size of Data found. If size is less than the\r
1771 data, this value contains the required size.\r
1772 @param Data Data pointer.\r
1773 \r
8a2d4996 1774 @return EFI_INVALID_PARAMETER Invalid parameter.\r
1775 @return EFI_SUCCESS Find the specified variable.\r
1776 @return EFI_NOT_FOUND Not found.\r
1777 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.\r
72399dae 1778\r
1779**/\r
1780EFI_STATUS\r
1781EFIAPI\r
8a2d4996 1782VariableServiceGetVariable (\r
72399dae 1783 IN CHAR16 *VariableName,\r
1784 IN EFI_GUID *VendorGuid,\r
1785 OUT UINT32 *Attributes OPTIONAL,\r
1786 IN OUT UINTN *DataSize,\r
1787 OUT VOID *Data\r
1788 )\r
1789{\r
1790 EFI_STATUS Status;\r
1791 VARIABLE_POINTER_TRACK Variable;\r
1792 UINTN VarDataSize;\r
1793\r
1794 if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {\r
1795 return EFI_INVALID_PARAMETER;\r
1796 }\r
1797\r
1798 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
72399dae 1799 \r
9622df63 1800 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
72399dae 1801 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
1802 goto Done;\r
1803 }\r
1804\r
1805 //\r
1806 // Get data size\r
1807 //\r
1808 VarDataSize = DataSizeOfVariable (Variable.CurrPtr);\r
1809 ASSERT (VarDataSize != 0);\r
1810\r
1811 if (*DataSize >= VarDataSize) {\r
1812 if (Data == NULL) {\r
1813 Status = EFI_INVALID_PARAMETER;\r
1814 goto Done;\r
33a5a666 1815 }\r
72399dae 1816\r
1817 CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);\r
1818 if (Attributes != NULL) {\r
1819 *Attributes = Variable.CurrPtr->Attributes;\r
1820 }\r
1821\r
1822 *DataSize = VarDataSize;\r
1823 UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, TRUE, FALSE, FALSE, FALSE);\r
72399dae 1824 \r
1825 Status = EFI_SUCCESS;\r
1826 goto Done;\r
1827 } else {\r
1828 *DataSize = VarDataSize;\r
1829 Status = EFI_BUFFER_TOO_SMALL;\r
1830 goto Done;\r
8d3a5c82 1831 }\r
1832\r
72399dae 1833Done:\r
1834 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
1835 return Status;\r
1836}\r
1837\r
1838\r
1839\r
1840/**\r
1841\r
1842 This code Finds the Next available variable.\r
1843\r
8a2d4996 1844 @param VariableNameSize Size of the variable name.\r
1845 @param VariableName Pointer to variable name.\r
1846 @param VendorGuid Variable Vendor Guid.\r
72399dae 1847\r
8a2d4996 1848 @return EFI_INVALID_PARAMETER Invalid parameter.\r
1849 @return EFI_SUCCESS Find the specified variable.\r
1850 @return EFI_NOT_FOUND Not found.\r
1851 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.\r
72399dae 1852\r
1853**/\r
1854EFI_STATUS\r
1855EFIAPI\r
8a2d4996 1856VariableServiceGetNextVariableName (\r
72399dae 1857 IN OUT UINTN *VariableNameSize,\r
1858 IN OUT CHAR16 *VariableName,\r
1859 IN OUT EFI_GUID *VendorGuid\r
1860 )\r
1861{\r
0f7aff72 1862 VARIABLE_STORE_TYPE Type;\r
72399dae 1863 VARIABLE_POINTER_TRACK Variable;\r
0f7aff72 1864 VARIABLE_POINTER_TRACK VariableInHob;\r
72399dae 1865 UINTN VarNameSize;\r
1866 EFI_STATUS Status;\r
0f7aff72 1867 VARIABLE_STORE_HEADER *VariableStoreHeader[VariableStoreTypeMax];\r
72399dae 1868\r
1869 if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {\r
1870 return EFI_INVALID_PARAMETER;\r
1871 }\r
1872\r
1873 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
1874\r
9622df63 1875 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
72399dae 1876 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
1877 goto Done;\r
1878 }\r
1879\r
1880 if (VariableName[0] != 0) {\r
1881 //\r
8a2d4996 1882 // If variable name is not NULL, get next variable.\r
72399dae 1883 //\r
1884 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
1885 }\r
1886\r
0f7aff72
RN
1887 //\r
1888 // 0: Volatile, 1: HOB, 2: Non-Volatile.\r
1889 // The index and attributes mapping must be kept in this order as FindVariable\r
1890 // makes use of this mapping to implement search algorithm.\r
1891 //\r
1892 VariableStoreHeader[VariableStoreTypeVolatile] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;\r
1893 VariableStoreHeader[VariableStoreTypeHob] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;\r
1894 VariableStoreHeader[VariableStoreTypeNv] = mNvVariableCache;\r
1895\r
72399dae 1896 while (TRUE) {\r
1897 //\r
0f7aff72 1898 // Switch from Volatile to HOB, to Non-Volatile.\r
72399dae 1899 //\r
0f7aff72
RN
1900 while ((Variable.CurrPtr >= Variable.EndPtr) ||\r
1901 (Variable.CurrPtr == NULL) ||\r
1902 !IsValidVariableHeader (Variable.CurrPtr)\r
1903 ) {\r
1904 //\r
1905 // Find current storage index\r
1906 //\r
1907 for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {\r
1908 if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr == GetStartPointer (VariableStoreHeader[Type]))) {\r
1909 break;\r
1910 }\r
1911 }\r
1912 ASSERT (Type < VariableStoreTypeMax);\r
1913 //\r
1914 // Switch to next storage\r
1915 //\r
1916 for (Type++; Type < VariableStoreTypeMax; Type++) {\r
1917 if (VariableStoreHeader[Type] != NULL) {\r
1918 break;\r
1919 }\r
1920 }\r
1921 //\r
1922 // Capture the case that \r
1923 // 1. current storage is the last one, or\r
1924 // 2. no further storage\r
1925 //\r
1926 if (Type == VariableStoreTypeMax) {\r
72399dae 1927 Status = EFI_NOT_FOUND;\r
1928 goto Done;\r
1929 }\r
0f7aff72
RN
1930 Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);\r
1931 Variable.EndPtr = GetEndPointer (VariableStoreHeader[Type]);\r
1932 Variable.CurrPtr = Variable.StartPtr;\r
72399dae 1933 }\r
0f7aff72 1934\r
72399dae 1935 //\r
1936 // Variable is found\r
1937 //\r
0f7aff72 1938 if (Variable.CurrPtr->State == VAR_ADDED) {\r
8a2d4996 1939 if ((AtRuntime () && ((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0)) == 0) {\r
0f7aff72
RN
1940\r
1941 //\r
1942 // Don't return NV variable when HOB overrides it\r
1943 //\r
1944 if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) && (VariableStoreHeader[VariableStoreTypeNv] != NULL) && \r
1945 (Variable.StartPtr == GetStartPointer (VariableStoreHeader[VariableStoreTypeNv]))\r
1946 ) {\r
1947 VariableInHob.StartPtr = GetStartPointer (VariableStoreHeader[VariableStoreTypeHob]);\r
1948 VariableInHob.EndPtr = GetEndPointer (VariableStoreHeader[VariableStoreTypeHob]);\r
1949 Status = FindVariableEx (\r
1950 GetVariableNamePtr (Variable.CurrPtr),\r
1951 &Variable.CurrPtr->VendorGuid,\r
9622df63 1952 FALSE,\r
0f7aff72
RN
1953 &VariableInHob\r
1954 );\r
1955 if (!EFI_ERROR (Status)) {\r
1956 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
1957 continue;\r
1958 }\r
1959 }\r
1960\r
72399dae 1961 VarNameSize = NameSizeOfVariable (Variable.CurrPtr);\r
1962 ASSERT (VarNameSize != 0);\r
1963\r
1964 if (VarNameSize <= *VariableNameSize) {\r
0f7aff72
RN
1965 CopyMem (VariableName, GetVariableNamePtr (Variable.CurrPtr), VarNameSize);\r
1966 CopyMem (VendorGuid, &Variable.CurrPtr->VendorGuid, sizeof (EFI_GUID));\r
72399dae 1967 Status = EFI_SUCCESS;\r
1968 } else {\r
1969 Status = EFI_BUFFER_TOO_SMALL;\r
1970 }\r
1971\r
1972 *VariableNameSize = VarNameSize;\r
1973 goto Done;\r
1974 }\r
1975 }\r
1976\r
1977 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
1978 }\r
33a5a666 1979\r
8d3a5c82 1980Done:\r
72399dae 1981 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
1982 return Status;\r
1983}\r
1984\r
1985/**\r
1986\r
1987 This code sets variable in storage blocks (Volatile or Non-Volatile).\r
1988\r
8a2d4996 1989 @param VariableName Name of Variable to be found.\r
1990 @param VendorGuid Variable vendor GUID.\r
72399dae 1991 @param Attributes Attribute value of the variable found\r
1992 @param DataSize Size of Data found. If size is less than the\r
1993 data, this value contains the required size.\r
8a2d4996 1994 @param Data Data pointer.\r
72399dae 1995\r
8a2d4996 1996 @return EFI_INVALID_PARAMETER Invalid parameter.\r
1997 @return EFI_SUCCESS Set successfully.\r
1998 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.\r
1999 @return EFI_NOT_FOUND Not found.\r
2000 @return EFI_WRITE_PROTECTED Variable is read-only.\r
72399dae 2001\r
2002**/\r
2003EFI_STATUS\r
2004EFIAPI\r
8a2d4996 2005VariableServiceSetVariable (\r
72399dae 2006 IN CHAR16 *VariableName,\r
2007 IN EFI_GUID *VendorGuid,\r
2008 IN UINT32 Attributes,\r
2009 IN UINTN DataSize,\r
2010 IN VOID *Data\r
2011 )\r
2012{\r
2013 VARIABLE_POINTER_TRACK Variable;\r
2014 EFI_STATUS Status;\r
2015 VARIABLE_HEADER *NextVariable;\r
2016 EFI_PHYSICAL_ADDRESS Point;\r
2017\r
2018 //\r
8a2d4996 2019 // Check input parameters.\r
72399dae 2020 //\r
2021 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r
2022 return EFI_INVALID_PARAMETER;\r
8a2d4996 2023 } \r
48a0e6bf 2024\r
2025 if (DataSize != 0 && Data == NULL) {\r
2026 return EFI_INVALID_PARAMETER;\r
2027 }\r
2028\r
8e38f18e 2029 //\r
2030 // Not support authenticated variable write yet.\r
2031 //\r
2032 if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {\r
2033 return EFI_INVALID_PARAMETER;\r
2034 }\r
2035\r
72399dae 2036 //\r
8a2d4996 2037 // Make sure if runtime bit is set, boot service bit is set also.\r
72399dae 2038 //\r
2039 if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
2040 return EFI_INVALID_PARAMETER;\r
2041 }\r
2042\r
2043 //\r
2044 // The size of the VariableName, including the Unicode Null in bytes plus\r
188e4e84 2045 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)\r
2046 // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.\r
72399dae 2047 //\r
2048 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
188e4e84 2049 if ((DataSize > PcdGet32 (PcdMaxHardwareErrorVariableSize)) ||\r
2050 (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > PcdGet32 (PcdMaxHardwareErrorVariableSize))) {\r
72399dae 2051 return EFI_INVALID_PARAMETER;\r
2052 }\r
2053 //\r
8a2d4996 2054 // According to UEFI spec, HARDWARE_ERROR_RECORD variable name convention should be L"HwErrRecXXXX".\r
72399dae 2055 //\r
2056 if (StrnCmp(VariableName, L"HwErrRec", StrLen(L"HwErrRec")) != 0) {\r
2057 return EFI_INVALID_PARAMETER;\r
2058 }\r
2059 } else {\r
2060 //\r
2061 // The size of the VariableName, including the Unicode Null in bytes plus\r
188e4e84 2062 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxVariableSize) bytes.\r
72399dae 2063 //\r
188e4e84 2064 if ((DataSize > PcdGet32 (PcdMaxVariableSize)) ||\r
2065 (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > PcdGet32 (PcdMaxVariableSize))) {\r
72399dae 2066 return EFI_INVALID_PARAMETER;\r
2067 } \r
021a1af9
SZ
2068 }\r
2069\r
2070 if (AtRuntime ()) {\r
2071 //\r
2072 // HwErrRecSupport Global Variable identifies the level of hardware error record persistence\r
2073 // support implemented by the platform. This variable is only modified by firmware and is read-only to the OS.\r
2074 //\r
2075 if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, L"HwErrRecSupport") == 0)) {\r
2076 return EFI_WRITE_PROTECTED;\r
2077 }\r
2078 }\r
72399dae 2079\r
2080 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2081\r
2082 //\r
8a2d4996 2083 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.\r
72399dae 2084 //\r
2085 if (1 < InterlockedIncrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState)) {\r
8a2d4996 2086 Point = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;\r
72399dae 2087 //\r
8a2d4996 2088 // Parse non-volatile variable data and get last variable offset.\r
72399dae 2089 //\r
2090 NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point);\r
2091 while ((NextVariable < GetEndPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point)) \r
2092 && IsValidVariableHeader (NextVariable)) {\r
2093 NextVariable = GetNextVariablePtr (NextVariable);\r
2094 }\r
2095 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) Point;\r
2096 }\r
2097\r
2098 //\r
8a2d4996 2099 // Check whether the input variable is already existed.\r
72399dae 2100 //\r
9622df63
SZ
2101 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, TRUE);\r
2102 if (!EFI_ERROR (Status)) {\r
2103 if (((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) && AtRuntime ()) {\r
2104 return EFI_WRITE_PROTECTED;\r
2105 }\r
2106 }\r
72399dae 2107\r
2108 //\r
8a2d4996 2109 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.\r
72399dae 2110 //\r
2111 AutoUpdateLangVariable (VariableName, Data, DataSize);\r
2112\r
2113 Status = UpdateVariable (VariableName, VendorGuid, Data, DataSize, Attributes, &Variable);\r
2114\r
fdb7765f 2115 InterlockedDecrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState);\r
052ad7e1 2116 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
fdb7765f 2117\r
8d3a5c82 2118 return Status;\r
2119}\r
2120\r
7c80e839 2121/**\r
8d3a5c82 2122\r
2123 This code returns information about the EFI variables.\r
2124\r
7c80e839 2125 @param Attributes Attributes bitmask to specify the type of variables\r
2126 on which to return information.\r
2127 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available\r
2128 for the EFI variables associated with the attributes specified.\r
2129 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available\r
2130 for EFI variables associated with the attributes specified.\r
2131 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables\r
2132 associated with the attributes specified.\r
8d3a5c82 2133\r
7c80e839 2134 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.\r
2135 @return EFI_SUCCESS Query successfully.\r
2136 @return EFI_UNSUPPORTED The attribute is not supported on this platform.\r
8d3a5c82 2137\r
7c80e839 2138**/\r
052ad7e1
A
2139EFI_STATUS\r
2140EFIAPI\r
8a2d4996 2141VariableServiceQueryVariableInfo (\r
052ad7e1
A
2142 IN UINT32 Attributes,\r
2143 OUT UINT64 *MaximumVariableStorageSize,\r
2144 OUT UINT64 *RemainingVariableStorageSize,\r
2145 OUT UINT64 *MaximumVariableSize\r
2146 )\r
8d3a5c82 2147{\r
2148 VARIABLE_HEADER *Variable;\r
2149 VARIABLE_HEADER *NextVariable;\r
2150 UINT64 VariableSize;\r
2151 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
2fcdca1d 2152 UINT64 CommonVariableTotalSize;\r
2153 UINT64 HwErrVariableTotalSize;\r
2154\r
2155 CommonVariableTotalSize = 0;\r
2156 HwErrVariableTotalSize = 0;\r
8d3a5c82 2157\r
c6492839 2158 if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {\r
8d3a5c82 2159 return EFI_INVALID_PARAMETER;\r
2160 }\r
2fcdca1d 2161\r
c6492839 2162 if((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == 0) {\r
8d3a5c82 2163 //\r
2164 // Make sure the Attributes combination is supported by the platform.\r
2165 //\r
c6492839 2166 return EFI_UNSUPPORTED; \r
8d3a5c82 2167 } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
2168 //\r
2169 // Make sure if runtime bit is set, boot service bit is set also.\r
2170 //\r
2171 return EFI_INVALID_PARAMETER;\r
8a2d4996 2172 } else if (AtRuntime () && ((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0)) {\r
8d3a5c82 2173 //\r
2174 // Make sure RT Attribute is set if we are in Runtime phase.\r
2175 //\r
2176 return EFI_INVALID_PARAMETER;\r
2fcdca1d 2177 } else if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2178 //\r
2179 // Make sure Hw Attribute is set with NV.\r
2180 //\r
2181 return EFI_INVALID_PARAMETER;\r
8e38f18e 2182 } else if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {\r
2183 //\r
2184 // Not support authentiated variable write yet.\r
2185 //\r
2186 return EFI_UNSUPPORTED;\r
8d3a5c82 2187 }\r
2188\r
052ad7e1 2189 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
8d3a5c82 2190\r
2191 if((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r
2192 //\r
2193 // Query is Volatile related.\r
2194 //\r
052ad7e1 2195 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r
8d3a5c82 2196 } else {\r
2197 //\r
2198 // Query is Non-Volatile related.\r
2199 //\r
8a2d4996 2200 VariableStoreHeader = mNvVariableCache;\r
8d3a5c82 2201 }\r
2202\r
2203 //\r
2204 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize\r
2205 // with the storage size (excluding the storage header size).\r
2206 //\r
2207 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);\r
c6492839 2208\r
2209 //\r
2210 // Harware error record variable needs larger size.\r
2211 //\r
2fcdca1d 2212 if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
188e4e84 2213 *MaximumVariableStorageSize = PcdGet32 (PcdHwErrStorageSize);\r
2214 *MaximumVariableSize = PcdGet32 (PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER);\r
2fcdca1d 2215 } else {\r
2216 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
188e4e84 2217 ASSERT (PcdGet32 (PcdHwErrStorageSize) < VariableStoreHeader->Size);\r
2218 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize);\r
2fcdca1d 2219 }\r
2220\r
2221 //\r
188e4e84 2222 // Let *MaximumVariableSize be PcdGet32 (PcdMaxVariableSize) with the exception of the variable header size.\r
2fcdca1d 2223 //\r
188e4e84 2224 *MaximumVariableSize = PcdGet32 (PcdMaxVariableSize) - sizeof (VARIABLE_HEADER);\r
c6492839 2225 }\r
8d3a5c82 2226\r
2227 //\r
2228 // Point to the starting address of the variables.\r
2229 //\r
9cad030b 2230 Variable = GetStartPointer (VariableStoreHeader);\r
8d3a5c82 2231\r
2232 //\r
2233 // Now walk through the related variable store.\r
2234 //\r
6f90dfbc 2235 while ((Variable < GetEndPointer (VariableStoreHeader)) && IsValidVariableHeader (Variable)) {\r
8d3a5c82 2236 NextVariable = GetNextVariablePtr (Variable);\r
2237 VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;\r
2238\r
8a2d4996 2239 if (AtRuntime ()) {\r
8d3a5c82 2240 //\r
8a2d4996 2241 // We don't take the state of the variables in mind\r
8d3a5c82 2242 // when calculating RemainingVariableStorageSize,\r
2243 // since the space occupied by variables not marked with\r
2244 // VAR_ADDED is not allowed to be reclaimed in Runtime.\r
2245 //\r
3b425367 2246 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2fcdca1d 2247 HwErrVariableTotalSize += VariableSize;\r
2248 } else {\r
2249 CommonVariableTotalSize += VariableSize;\r
2250 }\r
8d3a5c82 2251 } else {\r
2252 //\r
8a2d4996 2253 // Only care about Variables with State VAR_ADDED, because\r
8d3a5c82 2254 // the space not marked as VAR_ADDED is reclaimable now.\r
2255 //\r
2256 if (Variable->State == VAR_ADDED) {\r
3b425367 2257 if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2fcdca1d 2258 HwErrVariableTotalSize += VariableSize;\r
2259 } else {\r
2260 CommonVariableTotalSize += VariableSize;\r
2261 }\r
8d3a5c82 2262 }\r
2263 }\r
2264\r
2265 //\r
8a2d4996 2266 // Go to the next one.\r
8d3a5c82 2267 //\r
2268 Variable = NextVariable;\r
2269 }\r
2270\r
2fcdca1d 2271 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD){\r
2272 *RemainingVariableStorageSize = *MaximumVariableStorageSize - HwErrVariableTotalSize;\r
2273 }else {\r
2274 *RemainingVariableStorageSize = *MaximumVariableStorageSize - CommonVariableTotalSize;\r
2275 }\r
2276\r
c6492839 2277 if (*RemainingVariableStorageSize < sizeof (VARIABLE_HEADER)) {\r
2278 *MaximumVariableSize = 0;\r
2279 } else if ((*RemainingVariableStorageSize - sizeof (VARIABLE_HEADER)) < *MaximumVariableSize) {\r
2280 *MaximumVariableSize = *RemainingVariableStorageSize - sizeof (VARIABLE_HEADER);\r
2281 }\r
2282\r
052ad7e1 2283 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
8d3a5c82 2284 return EFI_SUCCESS;\r
2285}\r
2286\r
7c80e839 2287\r
2288/**\r
8a2d4996 2289 This function reclaims variable storage if free size is below the threshold.\r
2290 \r
7c80e839 2291**/\r
7800593d 2292VOID\r
7800593d 2293ReclaimForOS(\r
8a2d4996 2294 VOID\r
7800593d
LG
2295 )\r
2296{\r
9948c0b0 2297 EFI_STATUS Status;\r
2fcdca1d 2298 UINTN CommonVariableSpace;\r
2299 UINTN RemainingCommonVariableSpace;\r
2300 UINTN RemainingHwErrVariableSpace;\r
7800593d 2301\r
7800593d
LG
2302 Status = EFI_SUCCESS; \r
2303\r
2fcdca1d 2304 CommonVariableSpace = ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase)))->Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32(PcdHwErrStorageSize); //Allowable max size of common variable storage space\r
2305\r
2306 RemainingCommonVariableSpace = CommonVariableSpace - mVariableModuleGlobal->CommonVariableTotalSize;\r
2307\r
2308 RemainingHwErrVariableSpace = PcdGet32 (PcdHwErrStorageSize) - mVariableModuleGlobal->HwErrVariableTotalSize;\r
7800593d 2309 //\r
9948c0b0 2310 // Check if the free area is blow a threshold.\r
7800593d 2311 //\r
2fcdca1d 2312 if ((RemainingCommonVariableSpace < PcdGet32 (PcdMaxVariableSize))\r
9948c0b0 2313 || ((PcdGet32 (PcdHwErrStorageSize) != 0) && \r
2314 (RemainingHwErrVariableSpace < PcdGet32 (PcdMaxHardwareErrorVariableSize)))){\r
7800593d 2315 Status = Reclaim (\r
2fcdca1d 2316 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
2317 &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
2318 FALSE,\r
2319 NULL\r
2320 );\r
7800593d
LG
2321 ASSERT_EFI_ERROR (Status);\r
2322 }\r
2323}\r
2324\r
8a2d4996 2325\r
7c80e839 2326/**\r
8a2d4996 2327 Initializes variable write service after FVB was ready.\r
7c80e839 2328\r
8a2d4996 2329 @retval EFI_SUCCESS Function successfully executed.\r
2330 @retval Others Fail to initialize the variable service.\r
2331\r
2332**/\r
2333EFI_STATUS\r
2334VariableWriteServiceInitialize (\r
2335 VOID\r
2336 )\r
2337{\r
2338 EFI_STATUS Status;\r
2339 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
2340 UINTN Index;\r
2341 UINT8 Data;\r
8a2d4996 2342 EFI_PHYSICAL_ADDRESS VariableStoreBase;\r
0f7aff72
RN
2343 VARIABLE_HEADER *Variable;\r
2344 VOID *VariableData;\r
8a2d4996 2345\r
2346 VariableStoreBase = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;\r
2347 VariableStoreHeader = (VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase;\r
0f7aff72 2348 \r
8a2d4996 2349 //\r
2350 // Check if the free area is really free.\r
2351 //\r
0f7aff72 2352 for (Index = mVariableModuleGlobal->NonVolatileLastVariableOffset; Index < VariableStoreHeader->Size; Index++) {\r
8a2d4996 2353 Data = ((UINT8 *) mNvVariableCache)[Index];\r
2354 if (Data != 0xff) {\r
2355 //\r
2356 // There must be something wrong in variable store, do reclaim operation.\r
2357 //\r
2358 Status = Reclaim (\r
2359 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
2360 &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
2361 FALSE,\r
2362 NULL\r
2363 );\r
2364 if (EFI_ERROR (Status)) {\r
2365 return Status;\r
2366 }\r
2367 break;\r
2368 }\r
2369 }\r
2370\r
0f7aff72
RN
2371 //\r
2372 // Flush the HOB variable to flash and invalidate HOB variable.\r
2373 //\r
2374 if (mVariableModuleGlobal->VariableGlobal.HobVariableBase != 0) {\r
2375 //\r
2376 // Clear the HobVariableBase to avoid SetVariable() updating the variable in HOB\r
2377 //\r
2378 VariableStoreHeader = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;\r
2379 mVariableModuleGlobal->VariableGlobal.HobVariableBase = 0;\r
2380\r
2381 for ( Variable = GetStartPointer (VariableStoreHeader)\r
2382 ; (Variable < GetEndPointer (VariableStoreHeader) && IsValidVariableHeader (Variable))\r
2383 ; Variable = GetNextVariablePtr (Variable)\r
2384 ) {\r
2385 ASSERT (Variable->State == VAR_ADDED);\r
2386 ASSERT ((Variable->Attributes & EFI_VARIABLE_NON_VOLATILE) != 0);\r
2387 VariableData = GetVariableDataPtr (Variable);\r
2388 Status = VariableServiceSetVariable (\r
2389 GetVariableNamePtr (Variable),\r
2390 &Variable->VendorGuid,\r
2391 Variable->Attributes,\r
2392 Variable->DataSize,\r
2393 VariableData\r
2394 );\r
2395 ASSERT_EFI_ERROR (Status);\r
2396 }\r
2397 }\r
8a2d4996 2398 return EFI_SUCCESS;\r
2399}\r
2400\r
2401\r
2402/**\r
2403 Initializes variable store area for non-volatile and volatile variable.\r
7c80e839 2404\r
2405 @retval EFI_SUCCESS Function successfully executed.\r
2406 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.\r
2407\r
2408**/\r
8d3a5c82 2409EFI_STATUS\r
8d3a5c82 2410VariableCommonInitialize (\r
8a2d4996 2411 VOID\r
8d3a5c82 2412 )\r
8d3a5c82 2413{\r
2414 EFI_STATUS Status;\r
8d3a5c82 2415 VARIABLE_STORE_HEADER *VolatileVariableStore;\r
2416 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
2417 VARIABLE_HEADER *NextVariable;\r
8a9e0b72 2418 EFI_PHYSICAL_ADDRESS TempVariableStoreHeader;\r
8a9e0b72 2419 EFI_PHYSICAL_ADDRESS VariableStoreBase;\r
052ad7e1 2420 UINT64 VariableStoreLength;\r
2fcdca1d 2421 UINTN ScratchSize;\r
aa75dfec 2422 UINTN VariableSize;\r
0f7aff72 2423 EFI_HOB_GUID_TYPE *GuidHob;\r
8d3a5c82 2424\r
7800593d
LG
2425 //\r
2426 // Allocate runtime memory for variable driver global structure.\r
2427 //\r
72399dae 2428 mVariableModuleGlobal = AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL));\r
7800593d
LG
2429 if (mVariableModuleGlobal == NULL) {\r
2430 return EFI_OUT_OF_RESOURCES;\r
2431 }\r
8d3a5c82 2432\r
8a2d4996 2433 InitializeLock (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock, TPL_NOTIFY);\r
8d3a5c82 2434\r
48cd992a 2435 //\r
2436 // Note that in EdkII variable driver implementation, Hardware Error Record type variable\r
2437 // is stored with common variable in the same NV region. So the platform integrator should\r
2438 // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of \r
2439 // PcdFlashNvStorageVariableSize.\r
2440 //\r
188e4e84 2441 ASSERT (PcdGet32 (PcdHwErrStorageSize) <= PcdGet32 (PcdFlashNvStorageVariableSize));\r
48cd992a 2442\r
0f7aff72
RN
2443 //\r
2444 // Get HOB variable store.\r
2445 //\r
2446 GuidHob = GetFirstGuidHob (&gEfiVariableGuid);\r
2447 if (GuidHob != NULL) {\r
f68af18e
RN
2448 VariableStoreHeader = GET_GUID_HOB_DATA (GuidHob);\r
2449 if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) {\r
2450 mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VariableStoreHeader;\r
2451 } else {\r
2452 DEBUG ((EFI_D_ERROR, "HOB Variable Store header is corrupted!\n"));\r
2453 }\r
0f7aff72
RN
2454 }\r
2455\r
8d3a5c82 2456 //\r
2fcdca1d 2457 // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.\r
8d3a5c82 2458 //\r
188e4e84 2459 ScratchSize = MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize));\r
2460 VolatileVariableStore = AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize) + ScratchSize);\r
8d3a5c82 2461 if (VolatileVariableStore == NULL) {\r
2462 FreePool (mVariableModuleGlobal);\r
2463 return EFI_OUT_OF_RESOURCES;\r
2464 }\r
2465\r
188e4e84 2466 SetMem (VolatileVariableStore, PcdGet32 (PcdVariableStoreSize) + ScratchSize, 0xff);\r
8d3a5c82 2467\r
2468 //\r
8a2d4996 2469 // Initialize Variable Specific Data.\r
8d3a5c82 2470 //\r
052ad7e1 2471 mVariableModuleGlobal->VariableGlobal.VolatileVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VolatileVariableStore;\r
9cad030b 2472 mVariableModuleGlobal->VolatileLastVariableOffset = (UINTN) GetStartPointer (VolatileVariableStore) - (UINTN) VolatileVariableStore;\r
8a2d4996 2473 mVariableModuleGlobal->FvbInstance = NULL;\r
8d3a5c82 2474\r
3709c4cd 2475 CopyGuid (&VolatileVariableStore->Signature, &gEfiVariableGuid);\r
8a2d4996 2476 VolatileVariableStore->Size = PcdGet32 (PcdVariableStoreSize);\r
2477 VolatileVariableStore->Format = VARIABLE_STORE_FORMATTED;\r
2478 VolatileVariableStore->State = VARIABLE_STORE_HEALTHY;\r
2479 VolatileVariableStore->Reserved = 0;\r
2480 VolatileVariableStore->Reserved1 = 0;\r
8d3a5c82 2481\r
2482 //\r
d9303576 2483 // Get non-volatile variable store.\r
8d3a5c82 2484 //\r
2485\r
92a4f6f3 2486 TempVariableStoreHeader = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);\r
2487 if (TempVariableStoreHeader == 0) {\r
2488 TempVariableStoreHeader = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);\r
2489 }\r
d6550260 2490\r
2491 //\r
2492 // Check if the Firmware Volume is not corrupted\r
2493 //\r
2494 if ((((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(TempVariableStoreHeader))->Signature != EFI_FVH_SIGNATURE) ||\r
2495 (!CompareGuid (&gEfiSystemNvDataFvGuid, &((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(TempVariableStoreHeader))->FileSystemGuid))) {\r
2496 Status = EFI_VOLUME_CORRUPTED;\r
2497 DEBUG ((EFI_D_ERROR, "Firmware Volume for Variable Store is corrupted\n"));\r
2498 goto Done;\r
2499 }\r
2500\r
8a2d4996 2501 VariableStoreBase = TempVariableStoreHeader + \\r
2502 (((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(TempVariableStoreHeader)) -> HeaderLength);\r
2503 VariableStoreLength = (UINT64) PcdGet32 (PcdFlashNvStorageVariableSize) - \\r
8a9e0b72 2504 (((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(TempVariableStoreHeader)) -> HeaderLength);\r
8d3a5c82 2505\r
8a2d4996 2506 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase;\r
2507 VariableStoreHeader = (VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase;\r
2508 if (GetVariableStoreStatus (VariableStoreHeader) != EfiValid) {\r
2509 Status = EFI_VOLUME_CORRUPTED;\r
2510 DEBUG((EFI_D_INFO, "Variable Store header is corrupted\n"));\r
7800593d 2511 goto Done;\r
8a2d4996 2512 } \r
2513 ASSERT(VariableStoreHeader->Size == VariableStoreLength);\r
2514 \r
8d3a5c82 2515 //\r
8a2d4996 2516 // Parse non-volatile variable data and get last variable offset.\r
8d3a5c82 2517 //\r
8a2d4996 2518 NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase);\r
2519 while (IsValidVariableHeader (NextVariable)) {\r
2520 VariableSize = NextVariable->NameSize + NextVariable->DataSize + sizeof (VARIABLE_HEADER);\r
2521 if ((NextVariable->Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
2522 mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VariableSize);\r
2523 } else {\r
2524 mVariableModuleGlobal->CommonVariableTotalSize += HEADER_ALIGN (VariableSize);\r
8d3a5c82 2525 }\r
2526\r
8a2d4996 2527 NextVariable = GetNextVariablePtr (NextVariable);\r
2528 }\r
7800593d 2529\r
8a2d4996 2530 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) VariableStoreBase;\r
2531 \r
2532 //\r
2533 // Allocate runtime memory used for a memory copy of the FLASH region.\r
2534 // Keep the memory and the FLASH in sync as updates occur\r
2535 //\r
2536 mNvVariableCache = AllocateRuntimeZeroPool ((UINTN)VariableStoreLength);\r
2537 if (mNvVariableCache == NULL) {\r
2538 Status = EFI_OUT_OF_RESOURCES;\r
2539 goto Done;\r
8d3a5c82 2540 }\r
8a2d4996 2541 CopyMem (mNvVariableCache, (CHAR8 *)(UINTN)VariableStoreBase, (UINTN)VariableStoreLength);\r
2542 Status = EFI_SUCCESS;\r
8d3a5c82 2543\r
7800593d 2544Done:\r
8d3a5c82 2545 if (EFI_ERROR (Status)) {\r
2546 FreePool (mVariableModuleGlobal);\r
2547 FreePool (VolatileVariableStore);\r
2548 }\r
2549\r
2550 return Status;\r
2551}\r
052ad7e1 2552\r
052ad7e1 2553\r
aa75dfec 2554/**\r
8a2d4996 2555 Get the proper fvb handle and/or fvb protocol by the given Flash address.\r
aa75dfec 2556\r
8a2d4996 2557 @param[in] Address The Flash address.\r
2558 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.\r
2559 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.\r
aa75dfec 2560\r
aa75dfec 2561**/\r
8a2d4996 2562EFI_STATUS\r
2563GetFvbInfoByAddress (\r
2564 IN EFI_PHYSICAL_ADDRESS Address,\r
2565 OUT EFI_HANDLE *FvbHandle OPTIONAL,\r
2566 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvbProtocol OPTIONAL\r
8a9e0b72 2567 )\r
2568{\r
8a2d4996 2569 EFI_STATUS Status;\r
2570 EFI_HANDLE *HandleBuffer;\r
2571 UINTN HandleCount;\r
2572 UINTN Index;\r
2573 EFI_PHYSICAL_ADDRESS FvbBaseAddress;\r
2574 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r
2575 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r
2576 EFI_FVB_ATTRIBUTES_2 Attributes;\r
2577 \r
8a9e0b72 2578 //\r
8a2d4996 2579 // Get all FVB handles.\r
8a9e0b72 2580 //\r
8a2d4996 2581 Status = GetFvbCountAndBuffer (&HandleCount, &HandleBuffer);\r
8a9e0b72 2582 if (EFI_ERROR (Status)) {\r
8a2d4996 2583 return EFI_NOT_FOUND;\r
8a9e0b72 2584 }\r
8a2d4996 2585\r
8a9e0b72 2586 //\r
8a2d4996 2587 // Get the FVB to access variable store.\r
8a9e0b72 2588 //\r
8a2d4996 2589 Fvb = NULL;\r
f0480ecf 2590 for (Index = 0; Index < HandleCount; Index += 1, Status = EFI_NOT_FOUND, Fvb = NULL) {\r
8a2d4996 2591 Status = GetFvbByHandle (HandleBuffer[Index], &Fvb);\r
8a9e0b72 2592 if (EFI_ERROR (Status)) {\r
2593 Status = EFI_NOT_FOUND;\r
2594 break;\r
2595 }\r
2596\r
2597 //\r
2598 // Ensure this FVB protocol supported Write operation.\r
2599 //\r
2600 Status = Fvb->GetAttributes (Fvb, &Attributes);\r
2601 if (EFI_ERROR (Status) || ((Attributes & EFI_FVB2_WRITE_STATUS) == 0)) {\r
2602 continue; \r
2603 }\r
8a2d4996 2604 \r
8a9e0b72 2605 //\r
8a2d4996 2606 // Compare the address and select the right one.\r
8a9e0b72 2607 //\r
2608 Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);\r
2609 if (EFI_ERROR (Status)) {\r
2610 continue;\r
2611 }\r
2612\r
2613 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvbBaseAddress);\r
8a2d4996 2614 if ((Address >= FvbBaseAddress) && (Address < (FvbBaseAddress + FwVolHeader->FvLength))) {\r
2615 if (FvbHandle != NULL) {\r
2616 *FvbHandle = HandleBuffer[Index];\r
2617 }\r
2618 if (FvbProtocol != NULL) {\r
2619 *FvbProtocol = Fvb;\r
2620 }\r
2621 Status = EFI_SUCCESS;\r
8a9e0b72 2622 break;\r