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