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