]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
Sync EDKII BaseTools to BaseTools project r2100.
[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
1251VariableGetBestLanguage (\r
1252 IN CONST CHAR8 *SupportedLanguages, \r
1253 IN BOOLEAN Iso639Language,\r
1254 ...\r
1255 )\r
1256{\r
1257 VA_LIST Args;\r
1258 CHAR8 *Language;\r
1259 UINTN CompareLength;\r
1260 UINTN LanguageLength;\r
1261 CONST CHAR8 *Supported;\r
1262 CHAR8 *Buffer;\r
1263\r
1264 ASSERT (SupportedLanguages != NULL);\r
1265\r
1266 VA_START (Args, Iso639Language);\r
1267 while ((Language = VA_ARG (Args, CHAR8 *)) != NULL) {\r
1268 //\r
1269 // Default to ISO 639-2 mode\r
1270 //\r
1271 CompareLength = 3;\r
1272 LanguageLength = MIN (3, AsciiStrLen (Language));\r
1273\r
1274 //\r
1275 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language\r
1276 //\r
1277 if (!Iso639Language) {\r
1278 for (LanguageLength = 0; Language[LanguageLength] != 0 && Language[LanguageLength] != ';'; LanguageLength++);\r
1279 }\r
1280\r
1281 //\r
1282 // Trim back the length of Language used until it is empty\r
1283 //\r
1284 while (LanguageLength > 0) {\r
1285 //\r
1286 // Loop through all language codes in SupportedLanguages\r
1287 //\r
1288 for (Supported = SupportedLanguages; *Supported != '\0'; Supported += CompareLength) {\r
1289 //\r
1290 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages\r
1291 //\r
1292 if (!Iso639Language) {\r
1293 //\r
1294 // Skip ';' characters in Supported\r
1295 //\r
1296 for (; *Supported != '\0' && *Supported == ';'; Supported++);\r
1297 //\r
1298 // Determine the length of the next language code in Supported\r
1299 //\r
1300 for (CompareLength = 0; Supported[CompareLength] != 0 && Supported[CompareLength] != ';'; CompareLength++);\r
1301 //\r
1302 // If Language is longer than the Supported, then skip to the next language\r
1303 //\r
1304 if (LanguageLength > CompareLength) {\r
1305 continue;\r
1306 }\r
1307 }\r
1308 //\r
1309 // See if the first LanguageLength characters in Supported match Language\r
1310 //\r
1311 if (AsciiStrnCmp (Supported, Language, LanguageLength) == 0) {\r
1312 VA_END (Args);\r
1313\r
1314 Buffer = Iso639Language ? mVariableModuleGlobal->Lang : mVariableModuleGlobal->PlatformLang;\r
1315 Buffer[CompareLength] = '\0';\r
1316 return CopyMem (Buffer, Supported, CompareLength);\r
1317 }\r
1318 }\r
1319\r
1320 if (Iso639Language) {\r
1321 //\r
1322 // If ISO 639 mode, then each language can only be tested once\r
1323 //\r
1324 LanguageLength = 0;\r
1325 } else {\r
1326 //\r
1327 // If RFC 4646 mode, then trim Language from the right to the next '-' character \r
1328 //\r
1329 for (LanguageLength--; LanguageLength > 0 && Language[LanguageLength] != '-'; LanguageLength--);\r
1330 }\r
1331 }\r
1332 }\r
1333 VA_END (Args);\r
1334\r
1335 //\r
1336 // No matches were found \r
1337 //\r
1338 return NULL;\r
1339}\r
1340\r
72399dae 1341/**\r
1342 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.\r
052ad7e1 1343\r
72399dae 1344 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.\r
052ad7e1 1345\r
72399dae 1346 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,\r
1347 and are read-only. Therefore, in variable driver, only store the original value for other use.\r
8d3a5c82 1348\r
72399dae 1349 @param[in] VariableName Name of variable\r
8d3a5c82 1350\r
72399dae 1351 @param[in] Data Variable data\r
8d3a5c82 1352\r
72399dae 1353 @param[in] DataSize Size of data. 0 means delete\r
1354\r
7c80e839 1355**/\r
255a3f33 1356VOID\r
72399dae 1357AutoUpdateLangVariable(\r
1358 IN CHAR16 *VariableName,\r
1359 IN VOID *Data,\r
1360 IN UINTN DataSize\r
052ad7e1 1361 )\r
8d3a5c82 1362{\r
255a3f33
RN
1363 EFI_STATUS Status;\r
1364 CHAR8 *BestPlatformLang;\r
1365 CHAR8 *BestLang;\r
1366 UINTN Index;\r
1367 UINT32 Attributes;\r
72399dae 1368 VARIABLE_POINTER_TRACK Variable;\r
255a3f33 1369 BOOLEAN SetLanguageCodes;\r
8d3a5c82 1370\r
72399dae 1371 //\r
255a3f33 1372 // Don't do updates for delete operation\r
72399dae 1373 //\r
255a3f33
RN
1374 if (DataSize == 0) {\r
1375 return;\r
1376 }\r
1377\r
1378 SetLanguageCodes = FALSE;\r
8d3a5c82 1379\r
72399dae 1380 if (StrCmp (VariableName, L"PlatformLangCodes") == 0) {\r
255a3f33
RN
1381 //\r
1382 // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.\r
1383 //\r
1384 if (EfiAtRuntime ()) {\r
1385 return;\r
1386 }\r
1387\r
1388 SetLanguageCodes = TRUE;\r
1389\r
72399dae 1390 //\r
1391 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only\r
1392 // Therefore, in variable driver, only store the original value for other use.\r
1393 //\r
255a3f33
RN
1394 if (mVariableModuleGlobal->PlatformLangCodes != NULL) {\r
1395 FreePool (mVariableModuleGlobal->PlatformLangCodes);\r
1396 }\r
1397 mVariableModuleGlobal->PlatformLangCodes = AllocateRuntimeCopyPool (DataSize, Data);\r
1398 ASSERT (mVariableModuleGlobal->PlatformLangCodes != NULL);\r
1399\r
72399dae 1400 //\r
255a3f33
RN
1401 // PlatformLang holds a single language from PlatformLangCodes, \r
1402 // so the size of PlatformLangCodes is enough for the PlatformLang.\r
72399dae 1403 //\r
255a3f33
RN
1404 if (mVariableModuleGlobal->PlatformLang != NULL) {\r
1405 FreePool (mVariableModuleGlobal->PlatformLang);\r
1406 }\r
1407 mVariableModuleGlobal->PlatformLang = AllocateRuntimePool (DataSize);\r
1408 ASSERT (mVariableModuleGlobal->PlatformLang != NULL);\r
fdb7765f 1409\r
255a3f33 1410 } else if (StrCmp (VariableName, L"LangCodes") == 0) {\r
72399dae 1411 //\r
255a3f33 1412 // LangCodes is a volatile variable, so it can not be updated at runtime.\r
72399dae 1413 //\r
255a3f33
RN
1414 if (EfiAtRuntime ()) {\r
1415 return;\r
1416 }\r
1417\r
1418 SetLanguageCodes = TRUE;\r
8d3a5c82 1419\r
8d3a5c82 1420 //\r
255a3f33
RN
1421 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only\r
1422 // Therefore, in variable driver, only store the original value for other use.\r
8d3a5c82 1423 //\r
255a3f33
RN
1424 if (mVariableModuleGlobal->LangCodes != NULL) {\r
1425 FreePool (mVariableModuleGlobal->LangCodes);\r
1426 }\r
1427 mVariableModuleGlobal->LangCodes = AllocateRuntimeCopyPool (DataSize, Data);\r
1428 ASSERT (mVariableModuleGlobal->LangCodes != NULL);\r
1429 }\r
8d3a5c82 1430\r
255a3f33
RN
1431 if (SetLanguageCodes \r
1432 && (mVariableModuleGlobal->PlatformLangCodes != NULL)\r
1433 && (mVariableModuleGlobal->LangCodes != NULL)) {\r
8d3a5c82 1434 //\r
255a3f33
RN
1435 // Update Lang if PlatformLang is already set\r
1436 // Update PlatformLang if Lang is already set\r
8d3a5c82 1437 //\r
255a3f33
RN
1438 Status = FindVariable (L"PlatformLang", &gEfiGlobalVariableGuid, &Variable, (VARIABLE_GLOBAL *) mVariableModuleGlobal);\r
1439 if (!EFI_ERROR (Status)) {\r
1440 //\r
1441 // Update Lang\r
1442 //\r
1443 VariableName = L"PlatformLang";\r
1444 Data = GetVariableDataPtr (Variable.CurrPtr);\r
1445 DataSize = Variable.CurrPtr->DataSize;\r
1446 } else {\r
1447 Status = FindVariable (L"Lang", &gEfiGlobalVariableGuid, &Variable, (VARIABLE_GLOBAL *) mVariableModuleGlobal);\r
1448 if (!EFI_ERROR (Status)) {\r
1449 //\r
1450 // Update PlatformLang\r
1451 //\r
1452 VariableName = L"Lang";\r
1453 Data = GetVariableDataPtr (Variable.CurrPtr);\r
1454 DataSize = Variable.CurrPtr->DataSize;\r
1455 } else {\r
1456 //\r
1457 // Neither PlatformLang nor Lang is set, directly return\r
1458 //\r
1459 return;\r
1460 }\r
1461 }\r
1462 }\r
1463 \r
1464 //\r
1465 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.\r
1466 //\r
1467 Attributes = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;\r
8d3a5c82 1468\r
255a3f33 1469 if (StrCmp (VariableName, L"PlatformLang") == 0) {\r
8d3a5c82 1470 //\r
255a3f33 1471 // Update Lang when PlatformLangCodes/LangCodes were set.\r
8d3a5c82 1472 //\r
255a3f33
RN
1473 if ((mVariableModuleGlobal->PlatformLangCodes != NULL) && (mVariableModuleGlobal->LangCodes != NULL)) {\r
1474 //\r
1475 // When setting PlatformLang, firstly get most matched language string from supported language codes.\r
1476 //\r
1477 BestPlatformLang = VariableGetBestLanguage (mVariableModuleGlobal->PlatformLangCodes, FALSE, Data, NULL);\r
1478 if (BestPlatformLang != NULL) {\r
1479 //\r
1480 // Get the corresponding index in language codes.\r
1481 //\r
1482 Index = GetIndexFromSupportedLangCodes (mVariableModuleGlobal->PlatformLangCodes, BestPlatformLang, FALSE);\r
fdb7765f 1483\r
255a3f33
RN
1484 //\r
1485 // Get the corresponding ISO639 language tag according to RFC4646 language tag.\r
1486 //\r
1487 BestLang = GetLangFromSupportedLangCodes (mVariableModuleGlobal->LangCodes, Index, TRUE);\r
8d3a5c82 1488\r
255a3f33
RN
1489 //\r
1490 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.\r
1491 //\r
1492 FindVariable (L"Lang", &gEfiGlobalVariableGuid, &Variable, (VARIABLE_GLOBAL *)mVariableModuleGlobal);\r
8d3a5c82 1493\r
255a3f33 1494 Status = UpdateVariable (L"Lang", &gEfiGlobalVariableGuid, BestLang, ISO_639_2_ENTRY_SIZE + 1, Attributes, &Variable);\r
8d3a5c82 1495\r
255a3f33 1496 DEBUG ((EFI_D_INFO, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a\n", BestPlatformLang, BestLang));\r
72399dae 1497\r
255a3f33
RN
1498 ASSERT_EFI_ERROR(Status);\r
1499 }\r
1500 }\r
72399dae 1501\r
255a3f33 1502 } else if (StrCmp (VariableName, L"Lang") == 0) {\r
72399dae 1503 //\r
255a3f33 1504 // Update PlatformLang when PlatformLangCodes/LangCodes were set.\r
72399dae 1505 //\r
255a3f33
RN
1506 if ((mVariableModuleGlobal->PlatformLangCodes != NULL) && (mVariableModuleGlobal->LangCodes != NULL)) {\r
1507 //\r
1508 // When setting Lang, firstly get most matched language string from supported language codes.\r
1509 //\r
1510 BestLang = VariableGetBestLanguage (mVariableModuleGlobal->LangCodes, TRUE, Data, NULL);\r
1511 if (BestLang != NULL) {\r
1512 //\r
1513 // Get the corresponding index in language codes.\r
1514 //\r
1515 Index = GetIndexFromSupportedLangCodes (mVariableModuleGlobal->LangCodes, BestLang, TRUE);\r
72399dae 1516\r
255a3f33
RN
1517 //\r
1518 // Get the corresponding RFC4646 language tag according to ISO639 language tag.\r
1519 //\r
1520 BestPlatformLang = GetLangFromSupportedLangCodes (mVariableModuleGlobal->PlatformLangCodes, Index, FALSE);\r
1521\r
1522 //\r
1523 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.\r
1524 //\r
1525 FindVariable (L"PlatformLang", &gEfiGlobalVariableGuid, &Variable, (VARIABLE_GLOBAL *)mVariableModuleGlobal);\r
72399dae 1526\r
255a3f33
RN
1527 Status = UpdateVariable (L"PlatformLang", &gEfiGlobalVariableGuid, BestPlatformLang, \r
1528 AsciiStrSize (BestPlatformLang), Attributes, &Variable);\r
72399dae 1529\r
255a3f33
RN
1530 DEBUG ((EFI_D_INFO, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a\n", BestLang, BestPlatformLang));\r
1531 ASSERT_EFI_ERROR (Status);\r
1532 }\r
1533 }\r
72399dae 1534 }\r
8d3a5c82 1535}\r
1536\r
7c80e839 1537/**\r
72399dae 1538 Update the variable region with Variable information. These are the same \r
1539 arguments as the EFI Variable services.\r
052ad7e1 1540\r
72399dae 1541 @param[in] VariableName Name of variable\r
8d3a5c82 1542\r
72399dae 1543 @param[in] VendorGuid Guid of variable\r
8d3a5c82 1544\r
72399dae 1545 @param[in] Data Variable data\r
1546\r
1547 @param[in] DataSize Size of data. 0 means delete\r
1548\r
1549 @param[in] Attributes Attribues of the variable\r
1550\r
1551 @param[in] Variable The variable information which is used to keep track of variable usage.\r
1552\r
1553 @retval EFI_SUCCESS The update operation is success.\r
1554\r
1555 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.\r
8d3a5c82 1556\r
7c80e839 1557**/\r
052ad7e1
A
1558EFI_STATUS\r
1559EFIAPI\r
72399dae 1560UpdateVariable (\r
1561 IN CHAR16 *VariableName,\r
1562 IN EFI_GUID *VendorGuid,\r
1563 IN VOID *Data,\r
1564 IN UINTN DataSize,\r
1565 IN UINT32 Attributes OPTIONAL,\r
1566 IN VARIABLE_POINTER_TRACK *Variable\r
052ad7e1 1567 )\r
8d3a5c82 1568{\r
8a9e0b72 1569 EFI_STATUS Status;\r
1570 VARIABLE_HEADER *NextVariable;\r
72399dae 1571 UINTN ScratchSize;\r
1572 UINTN NonVolatileVarableStoreSize;\r
8a9e0b72 1573 UINTN VarNameOffset;\r
1574 UINTN VarDataOffset;\r
72399dae 1575 UINTN VarNameSize;\r
8a9e0b72 1576 UINTN VarSize;\r
72399dae 1577 BOOLEAN Volatile;\r
1578 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r
8a9e0b72 1579 UINT8 State;\r
1580 BOOLEAN Reclaimed;\r
fdb7765f 1581\r
8a9e0b72 1582 Fvb = mVariableModuleGlobal->FvbInstance;\r
72399dae 1583 Reclaimed = FALSE;\r
fdb7765f 1584\r
72399dae 1585 if (Variable->CurrPtr != NULL) {\r
8d3a5c82 1586 //\r
c6492839 1587 // Update/Delete existing variable\r
8d3a5c82 1588 //\r
72399dae 1589 Volatile = Variable->Volatile;\r
c6492839 1590 \r
1591 if (EfiAtRuntime ()) { \r
1592 //\r
1593 // If EfiAtRuntime and the variable is Volatile and Runtime Access, \r
1594 // the volatile is ReadOnly, and SetVariable should be aborted and \r
1595 // return EFI_WRITE_PROTECTED.\r
1596 //\r
72399dae 1597 if (Variable->Volatile) {\r
c6492839 1598 Status = EFI_WRITE_PROTECTED;\r
1599 goto Done;\r
1600 }\r
1601 //\r
1602 // Only variable have NV attribute can be updated/deleted in Runtime\r
1603 //\r
72399dae 1604 if ((Variable->CurrPtr->Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r
c6492839 1605 Status = EFI_INVALID_PARAMETER;\r
1606 goto Done; \r
1607 }\r
1608 }\r
8d3a5c82 1609 //\r
c6492839 1610 // Setting a data variable with no access, or zero DataSize attributes\r
1611 // specified causes it to be deleted.\r
8d3a5c82 1612 //\r
c6492839 1613 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) { \r
72399dae 1614 State = Variable->CurrPtr->State;\r
c6492839 1615 State &= VAR_DELETED;\r
1616\r
1617 Status = UpdateVariableStore (\r
052ad7e1 1618 &mVariableModuleGlobal->VariableGlobal,\r
72399dae 1619 Variable->Volatile,\r
c6492839 1620 FALSE,\r
8a9e0b72 1621 Fvb,\r
72399dae 1622 (UINTN) &Variable->CurrPtr->State,\r
c6492839 1623 sizeof (UINT8),\r
1624 &State\r
1625 ); \r
33a5a666 1626 if (!EFI_ERROR (Status)) {\r
fd51bf70 1627 UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, FALSE, TRUE, FALSE);\r
33a5a666
A
1628 UpdateVariableCache (VariableName, VendorGuid, Attributes, DataSize, Data);\r
1629 }\r
c6492839 1630 goto Done; \r
1631 }\r
8d3a5c82 1632 //\r
c6492839 1633 // If the variable is marked valid and the same data has been passed in\r
1634 // then return to the caller immediately.\r
8d3a5c82 1635 //\r
72399dae 1636 if (DataSizeOfVariable (Variable->CurrPtr) == DataSize &&\r
1637 (CompareMem (Data, GetVariableDataPtr (Variable->CurrPtr), DataSize) == 0)) {\r
33a5a666 1638 \r
fd51bf70 1639 UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);\r
c6492839 1640 Status = EFI_SUCCESS;\r
1641 goto Done;\r
72399dae 1642 } else if ((Variable->CurrPtr->State == VAR_ADDED) ||\r
1643 (Variable->CurrPtr->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {\r
814bae52 1644\r
c6492839 1645 //\r
1646 // Mark the old variable as in delete transition\r
1647 //\r
72399dae 1648 State = Variable->CurrPtr->State;\r
c6492839 1649 State &= VAR_IN_DELETED_TRANSITION;\r
1650\r
1651 Status = UpdateVariableStore (\r
052ad7e1 1652 &mVariableModuleGlobal->VariableGlobal,\r
72399dae 1653 Variable->Volatile,\r
c6492839 1654 FALSE,\r
8a9e0b72 1655 Fvb,\r
72399dae 1656 (UINTN) &Variable->CurrPtr->State,\r
c6492839 1657 sizeof (UINT8),\r
1658 &State\r
1659 ); \r
1660 if (EFI_ERROR (Status)) {\r
1661 goto Done; \r
33a5a666 1662 } \r
c6492839 1663 } \r
72399dae 1664 } else {\r
8d3a5c82 1665 //\r
72399dae 1666 // Not found existing variable. Create a new variable\r
c6492839 1667 // \r
1668 \r
8d3a5c82 1669 //\r
c6492839 1670 // Make sure we are trying to create a new variable.\r
1671 // Setting a data variable with no access, or zero DataSize attributes means to delete it. \r
8d3a5c82 1672 //\r
c6492839 1673 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {\r
1674 Status = EFI_NOT_FOUND;\r
1675 goto Done;\r
1676 }\r
1677 \r
8d3a5c82 1678 //\r
c6492839 1679 // Only variable have NV|RT attribute can be created in Runtime\r
1680 //\r
1681 if (EfiAtRuntime () &&\r
45f6c85b 1682 (((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) || ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0))) {\r
c6492839 1683 Status = EFI_INVALID_PARAMETER;\r
1684 goto Done;\r
1685 } \r
c6492839 1686 }\r
1687\r
1688 //\r
1689 // Function part - create a new variable and copy the data.\r
1690 // Both update a variable and create a variable will come here.\r
1691 //\r
1692 // Tricky part: Use scratch data area at the end of volatile variable store\r
1693 // as a temporary storage.\r
1694 //\r
052ad7e1 1695 NextVariable = GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase));\r
188e4e84 1696 ScratchSize = MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize));\r
c6492839 1697\r
2fcdca1d 1698 SetMem (NextVariable, ScratchSize, 0xff);\r
c6492839 1699\r
1700 NextVariable->StartId = VARIABLE_DATA;\r
1701 NextVariable->Attributes = Attributes;\r
1702 //\r
1703 // NextVariable->State = VAR_ADDED;\r
1704 //\r
1705 NextVariable->Reserved = 0;\r
1706 VarNameOffset = sizeof (VARIABLE_HEADER);\r
1707 VarNameSize = StrSize (VariableName);\r
1708 CopyMem (\r
1709 (UINT8 *) ((UINTN) NextVariable + VarNameOffset),\r
1710 VariableName,\r
1711 VarNameSize\r
1712 );\r
1713 VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);\r
1714 CopyMem (\r
1715 (UINT8 *) ((UINTN) NextVariable + VarDataOffset),\r
1716 Data,\r
1717 DataSize\r
1718 );\r
1719 CopyMem (&NextVariable->VendorGuid, VendorGuid, sizeof (EFI_GUID));\r
1720 //\r
1721 // There will be pad bytes after Data, the NextVariable->NameSize and\r
1722 // NextVariable->DataSize should not include pad size so that variable\r
1723 // service can get actual size in GetVariable\r
1724 //\r
1725 NextVariable->NameSize = (UINT32)VarNameSize;\r
1726 NextVariable->DataSize = (UINT32)DataSize;\r
1727\r
1728 //\r
1729 // The actual size of the variable that stores in storage should\r
1730 // include pad size.\r
1731 //\r
1732 VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);\r
45f6c85b 1733 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
8d3a5c82 1734 //\r
c6492839 1735 // Create a nonvolatile variable\r
8d3a5c82 1736 //\r
fd51bf70 1737 Volatile = FALSE;\r
2fcdca1d 1738 NonVolatileVarableStoreSize = ((VARIABLE_STORE_HEADER *)(UINTN)(mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase))->Size;\r
1739 if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) \r
188e4e84 1740 && ((VarSize + mVariableModuleGlobal->HwErrVariableTotalSize) > PcdGet32 (PcdHwErrStorageSize)))\r
2fcdca1d 1741 || (((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0) \r
188e4e84 1742 && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > NonVolatileVarableStoreSize - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize)))) {\r
c6492839 1743 if (EfiAtRuntime ()) {\r
1744 Status = EFI_OUT_OF_RESOURCES;\r
1745 goto Done;\r
1746 }\r
1747 //\r
1748 // Perform garbage collection & reclaim operation\r
1749 //\r
72399dae 1750 Status = Reclaim (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase, \r
1751 &mVariableModuleGlobal->NonVolatileLastVariableOffset, FALSE, Variable->CurrPtr);\r
8d3a5c82 1752 if (EFI_ERROR (Status)) {\r
1753 goto Done;\r
1754 }\r
8d3a5c82 1755 //\r
c6492839 1756 // If still no enough space, return out of resources\r
8d3a5c82 1757 //\r
2fcdca1d 1758 if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) \r
188e4e84 1759 && ((VarSize + mVariableModuleGlobal->HwErrVariableTotalSize) > PcdGet32 (PcdHwErrStorageSize)))\r
2fcdca1d 1760 || (((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0) \r
188e4e84 1761 && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > NonVolatileVarableStoreSize - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize)))) {\r
c6492839 1762 Status = EFI_OUT_OF_RESOURCES;\r
8d3a5c82 1763 goto Done;\r
8d3a5c82 1764 }\r
c6492839 1765 Reclaimed = TRUE;\r
8d3a5c82 1766 }\r
1767 //\r
c6492839 1768 // Three steps\r
1769 // 1. Write variable header\r
130e2569 1770 // 2. Set variable state to header valid \r
1771 // 3. Write variable data\r
1772 // 4. Set variable state to valid\r
8d3a5c82 1773 //\r
8d3a5c82 1774 //\r
c6492839 1775 // Step 1:\r
8d3a5c82 1776 //\r
c6492839 1777 Status = UpdateVariableStore (\r
052ad7e1 1778 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 1779 FALSE,\r
1780 TRUE,\r
8a9e0b72 1781 Fvb,\r
72399dae 1782 mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
c6492839 1783 sizeof (VARIABLE_HEADER),\r
1784 (UINT8 *) NextVariable\r
1785 );\r
1786\r
1787 if (EFI_ERROR (Status)) {\r
1788 goto Done;\r
1789 }\r
130e2569 1790\r
8d3a5c82 1791 //\r
c6492839 1792 // Step 2:\r
8d3a5c82 1793 //\r
130e2569 1794 NextVariable->State = VAR_HEADER_VALID_ONLY;\r
1795 Status = UpdateVariableStore (\r
1796 &mVariableModuleGlobal->VariableGlobal,\r
1797 FALSE,\r
1798 TRUE,\r
8a9e0b72 1799 Fvb,\r
72399dae 1800 mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
130e2569 1801 sizeof (VARIABLE_HEADER),\r
1802 (UINT8 *) NextVariable\r
1803 );\r
1804\r
1805 if (EFI_ERROR (Status)) {\r
1806 goto Done;\r
1807 }\r
1808 //\r
1809 // Step 3:\r
1810 //\r
c6492839 1811 Status = UpdateVariableStore (\r
052ad7e1 1812 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 1813 FALSE,\r
1814 TRUE,\r
8a9e0b72 1815 Fvb,\r
72399dae 1816 mVariableModuleGlobal->NonVolatileLastVariableOffset + sizeof (VARIABLE_HEADER),\r
c6492839 1817 (UINT32) VarSize - sizeof (VARIABLE_HEADER),\r
1818 (UINT8 *) NextVariable + sizeof (VARIABLE_HEADER)\r
1819 );\r
1820\r
1821 if (EFI_ERROR (Status)) {\r
1822 goto Done;\r
1823 }\r
8d3a5c82 1824 //\r
130e2569 1825 // Step 4:\r
8d3a5c82 1826 //\r
c6492839 1827 NextVariable->State = VAR_ADDED;\r
1828 Status = UpdateVariableStore (\r
052ad7e1 1829 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 1830 FALSE,\r
1831 TRUE,\r
8a9e0b72 1832 Fvb,\r
72399dae 1833 mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
c6492839 1834 sizeof (VARIABLE_HEADER),\r
1835 (UINT8 *) NextVariable\r
1836 );\r
1837\r
1838 if (EFI_ERROR (Status)) {\r
1839 goto Done;\r
1840 }\r
8d3a5c82 1841\r
72399dae 1842 mVariableModuleGlobal->NonVolatileLastVariableOffset += HEADER_ALIGN (VarSize);\r
8d3a5c82 1843\r
2fcdca1d 1844 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {\r
1845 mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VarSize);\r
1846 } else {\r
1847 mVariableModuleGlobal->CommonVariableTotalSize += HEADER_ALIGN (VarSize);\r
1848 }\r
c6492839 1849 } else {\r
1850 //\r
1851 // Create a volatile variable\r
1852 // \r
fd51bf70 1853 Volatile = TRUE;\r
c6492839 1854\r
72399dae 1855 if ((UINT32) (VarSize + mVariableModuleGlobal->VolatileLastVariableOffset) >\r
052ad7e1 1856 ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size) {\r
8d3a5c82 1857 //\r
c6492839 1858 // Perform garbage collection & reclaim operation\r
8d3a5c82 1859 //\r
72399dae 1860 Status = Reclaim (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase, \r
1861 &mVariableModuleGlobal->VolatileLastVariableOffset, TRUE, Variable->CurrPtr);\r
8d3a5c82 1862 if (EFI_ERROR (Status)) {\r
1863 goto Done;\r
1864 }\r
1865 //\r
c6492839 1866 // If still no enough space, return out of resources\r
8d3a5c82 1867 //\r
72399dae 1868 if ((UINT32) (VarSize + mVariableModuleGlobal->VolatileLastVariableOffset) >\r
052ad7e1 1869 ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size\r
8d3a5c82 1870 ) {\r
c6492839 1871 Status = EFI_OUT_OF_RESOURCES;\r
8d3a5c82 1872 goto Done;\r
1873 }\r
c6492839 1874 Reclaimed = TRUE;\r
8d3a5c82 1875 }\r
8d3a5c82 1876\r
c6492839 1877 NextVariable->State = VAR_ADDED;\r
1878 Status = UpdateVariableStore (\r
052ad7e1 1879 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 1880 TRUE,\r
1881 TRUE,\r
8a9e0b72 1882 Fvb,\r
72399dae 1883 mVariableModuleGlobal->VolatileLastVariableOffset,\r
c6492839 1884 (UINT32) VarSize,\r
1885 (UINT8 *) NextVariable\r
1886 );\r
1887\r
1888 if (EFI_ERROR (Status)) {\r
1889 goto Done;\r
8d3a5c82 1890 }\r
c6492839 1891\r
72399dae 1892 mVariableModuleGlobal->VolatileLastVariableOffset += HEADER_ALIGN (VarSize);\r
c6492839 1893 }\r
72399dae 1894\r
c6492839 1895 //\r
1896 // Mark the old variable as deleted\r
1897 //\r
72399dae 1898 if (!Reclaimed && !EFI_ERROR (Status) && Variable->CurrPtr != NULL) {\r
1899 State = Variable->CurrPtr->State;\r
c6492839 1900 State &= VAR_DELETED;\r
1901\r
1902 Status = UpdateVariableStore (\r
72399dae 1903 &mVariableModuleGlobal->VariableGlobal,\r
1904 Variable->Volatile,\r
1905 FALSE,\r
1906 Fvb,\r
1907 (UINTN) &Variable->CurrPtr->State,\r
1908 sizeof (UINT8),\r
1909 &State\r
1910 );\r
1911 }\r
1912\r
1913 if (!EFI_ERROR (Status)) {\r
1914 UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);\r
1915 UpdateVariableCache (VariableName, VendorGuid, Attributes, DataSize, Data);\r
1916 }\r
1917\r
1918Done:\r
1919 return Status;\r
1920}\r
1921\r
1922/**\r
1923\r
1924 This code finds variable in storage blocks (Volatile or Non-Volatile).\r
1925\r
1926 @param VariableName Name of Variable to be found.\r
1927 @param VendorGuid Variable vendor GUID.\r
1928 @param Attributes Attribute value of the variable found.\r
1929 @param DataSize Size of Data found. If size is less than the\r
1930 data, this value contains the required size.\r
1931 @param Data Data pointer.\r
1932 \r
1933 @return EFI_INVALID_PARAMETER Invalid parameter\r
1934 @return EFI_SUCCESS Find the specified variable\r
1935 @return EFI_NOT_FOUND Not found\r
1936 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result\r
1937\r
1938**/\r
1939EFI_STATUS\r
1940EFIAPI\r
1941RuntimeServiceGetVariable (\r
1942 IN CHAR16 *VariableName,\r
1943 IN EFI_GUID *VendorGuid,\r
1944 OUT UINT32 *Attributes OPTIONAL,\r
1945 IN OUT UINTN *DataSize,\r
1946 OUT VOID *Data\r
1947 )\r
1948{\r
1949 EFI_STATUS Status;\r
1950 VARIABLE_POINTER_TRACK Variable;\r
1951 UINTN VarDataSize;\r
1952\r
1953 if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {\r
1954 return EFI_INVALID_PARAMETER;\r
1955 }\r
1956\r
1957 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
1958\r
1959 //\r
1960 // Find existing variable\r
1961 //\r
1962 Status = FindVariableInCache (VariableName, VendorGuid, Attributes, DataSize, Data);\r
1963 if ((Status == EFI_BUFFER_TOO_SMALL) || (Status == EFI_SUCCESS)){\r
1964 // Hit in the Cache\r
1965 UpdateVariableInfo (VariableName, VendorGuid, FALSE, TRUE, FALSE, FALSE, TRUE);\r
1966 goto Done;\r
1967 }\r
1968 \r
1969 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal);\r
1970 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
1971 goto Done;\r
1972 }\r
1973\r
1974 //\r
1975 // Get data size\r
1976 //\r
1977 VarDataSize = DataSizeOfVariable (Variable.CurrPtr);\r
1978 ASSERT (VarDataSize != 0);\r
1979\r
1980 if (*DataSize >= VarDataSize) {\r
1981 if (Data == NULL) {\r
1982 Status = EFI_INVALID_PARAMETER;\r
1983 goto Done;\r
33a5a666 1984 }\r
72399dae 1985\r
1986 CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);\r
1987 if (Attributes != NULL) {\r
1988 *Attributes = Variable.CurrPtr->Attributes;\r
1989 }\r
1990\r
1991 *DataSize = VarDataSize;\r
1992 UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, TRUE, FALSE, FALSE, FALSE);\r
1993 UpdateVariableCache (VariableName, VendorGuid, Variable.CurrPtr->Attributes, VarDataSize, Data);\r
1994 \r
1995 Status = EFI_SUCCESS;\r
1996 goto Done;\r
1997 } else {\r
1998 *DataSize = VarDataSize;\r
1999 Status = EFI_BUFFER_TOO_SMALL;\r
2000 goto Done;\r
8d3a5c82 2001 }\r
2002\r
72399dae 2003Done:\r
2004 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2005 return Status;\r
2006}\r
2007\r
2008\r
2009\r
2010/**\r
2011\r
2012 This code Finds the Next available variable.\r
2013\r
2014 @param VariableNameSize Size of the variable name\r
2015 @param VariableName Pointer to variable name\r
2016 @param VendorGuid Variable Vendor Guid\r
2017\r
2018 @return EFI_INVALID_PARAMETER Invalid parameter\r
2019 @return EFI_SUCCESS Find the specified variable\r
2020 @return EFI_NOT_FOUND Not found\r
2021 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result\r
2022\r
2023**/\r
2024EFI_STATUS\r
2025EFIAPI\r
2026RuntimeServiceGetNextVariableName (\r
2027 IN OUT UINTN *VariableNameSize,\r
2028 IN OUT CHAR16 *VariableName,\r
2029 IN OUT EFI_GUID *VendorGuid\r
2030 )\r
2031{\r
2032 VARIABLE_POINTER_TRACK Variable;\r
2033 UINTN VarNameSize;\r
2034 EFI_STATUS Status;\r
2035\r
2036 if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {\r
2037 return EFI_INVALID_PARAMETER;\r
2038 }\r
2039\r
2040 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2041\r
2042 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal);\r
2043 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
2044 goto Done;\r
2045 }\r
2046\r
2047 if (VariableName[0] != 0) {\r
2048 //\r
2049 // If variable name is not NULL, get next variable\r
2050 //\r
2051 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
2052 }\r
2053\r
2054 while (TRUE) {\r
2055 //\r
2056 // If both volatile and non-volatile variable store are parsed,\r
2057 // return not found\r
2058 //\r
2059 if (Variable.CurrPtr >= Variable.EndPtr || Variable.CurrPtr == NULL) {\r
2060 Variable.Volatile = (BOOLEAN) (Variable.Volatile ^ ((BOOLEAN) 0x1));\r
2061 if (!Variable.Volatile) {\r
2062 Variable.StartPtr = GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);\r
2063 Variable.EndPtr = GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase));\r
2064 } else {\r
2065 Status = EFI_NOT_FOUND;\r
2066 goto Done;\r
2067 }\r
2068\r
2069 Variable.CurrPtr = Variable.StartPtr;\r
2070 if (!IsValidVariableHeader (Variable.CurrPtr)) {\r
2071 continue;\r
2072 }\r
2073 }\r
2074 //\r
2075 // Variable is found\r
2076 //\r
2077 if (IsValidVariableHeader (Variable.CurrPtr) && Variable.CurrPtr->State == VAR_ADDED) {\r
2078 if ((EfiAtRuntime () && ((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0)) == 0) {\r
2079 VarNameSize = NameSizeOfVariable (Variable.CurrPtr);\r
2080 ASSERT (VarNameSize != 0);\r
2081\r
2082 if (VarNameSize <= *VariableNameSize) {\r
2083 CopyMem (\r
2084 VariableName,\r
2085 GetVariableNamePtr (Variable.CurrPtr),\r
2086 VarNameSize\r
2087 );\r
2088 CopyMem (\r
2089 VendorGuid,\r
2090 &Variable.CurrPtr->VendorGuid,\r
2091 sizeof (EFI_GUID)\r
2092 );\r
2093 Status = EFI_SUCCESS;\r
2094 } else {\r
2095 Status = EFI_BUFFER_TOO_SMALL;\r
2096 }\r
2097\r
2098 *VariableNameSize = VarNameSize;\r
2099 goto Done;\r
2100 }\r
2101 }\r
2102\r
2103 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
2104 }\r
33a5a666 2105\r
8d3a5c82 2106Done:\r
72399dae 2107 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2108 return Status;\r
2109}\r
2110\r
2111/**\r
2112\r
2113 This code sets variable in storage blocks (Volatile or Non-Volatile).\r
2114\r
2115 @param VariableName Name of Variable to be found\r
2116 @param VendorGuid Variable vendor GUID\r
2117 @param Attributes Attribute value of the variable found\r
2118 @param DataSize Size of Data found. If size is less than the\r
2119 data, this value contains the required size.\r
2120 @param Data Data pointer\r
2121\r
2122 @return EFI_INVALID_PARAMETER Invalid parameter\r
2123 @return EFI_SUCCESS Set successfully\r
2124 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable\r
2125 @return EFI_NOT_FOUND Not found\r
2126 @return EFI_WRITE_PROTECTED Variable is read-only\r
2127\r
2128**/\r
2129EFI_STATUS\r
2130EFIAPI\r
2131RuntimeServiceSetVariable (\r
2132 IN CHAR16 *VariableName,\r
2133 IN EFI_GUID *VendorGuid,\r
2134 IN UINT32 Attributes,\r
2135 IN UINTN DataSize,\r
2136 IN VOID *Data\r
2137 )\r
2138{\r
2139 VARIABLE_POINTER_TRACK Variable;\r
2140 EFI_STATUS Status;\r
2141 VARIABLE_HEADER *NextVariable;\r
2142 EFI_PHYSICAL_ADDRESS Point;\r
2143\r
2144 //\r
2145 // Check input parameters\r
2146 //\r
2147 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r
2148 return EFI_INVALID_PARAMETER;\r
48a0e6bf 2149 }\r
2150\r
2151 if (DataSize != 0 && Data == NULL) {\r
2152 return EFI_INVALID_PARAMETER;\r
2153 }\r
2154\r
8e38f18e 2155 //\r
2156 // Not support authenticated variable write yet.\r
2157 //\r
2158 if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {\r
2159 return EFI_INVALID_PARAMETER;\r
2160 }\r
2161\r
72399dae 2162 //\r
2163 // Make sure if runtime bit is set, boot service bit is set also\r
2164 //\r
2165 if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
2166 return EFI_INVALID_PARAMETER;\r
2167 }\r
2168\r
2169 //\r
2170 // The size of the VariableName, including the Unicode Null in bytes plus\r
188e4e84 2171 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)\r
2172 // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.\r
72399dae 2173 //\r
2174 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
188e4e84 2175 if ((DataSize > PcdGet32 (PcdMaxHardwareErrorVariableSize)) ||\r
2176 (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > PcdGet32 (PcdMaxHardwareErrorVariableSize))) {\r
72399dae 2177 return EFI_INVALID_PARAMETER;\r
2178 }\r
2179 //\r
2180 // According to UEFI spec, HARDWARE_ERROR_RECORD variable name convention should be L"HwErrRecXXXX"\r
2181 //\r
2182 if (StrnCmp(VariableName, L"HwErrRec", StrLen(L"HwErrRec")) != 0) {\r
2183 return EFI_INVALID_PARAMETER;\r
2184 }\r
2185 } else {\r
2186 //\r
2187 // The size of the VariableName, including the Unicode Null in bytes plus\r
188e4e84 2188 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxVariableSize) bytes.\r
72399dae 2189 //\r
188e4e84 2190 if ((DataSize > PcdGet32 (PcdMaxVariableSize)) ||\r
2191 (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > PcdGet32 (PcdMaxVariableSize))) {\r
72399dae 2192 return EFI_INVALID_PARAMETER;\r
2193 } \r
2194 } \r
2195\r
2196 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
2197\r
2198 //\r
2199 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated;\r
2200 //\r
2201 if (1 < InterlockedIncrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState)) {\r
2202 Point = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;;\r
2203 //\r
2204 // Parse non-volatile variable data and get last variable offset\r
2205 //\r
2206 NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point);\r
2207 while ((NextVariable < GetEndPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point)) \r
2208 && IsValidVariableHeader (NextVariable)) {\r
2209 NextVariable = GetNextVariablePtr (NextVariable);\r
2210 }\r
2211 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) Point;\r
2212 }\r
2213\r
2214 //\r
2215 // Check whether the input variable is already existed\r
2216 //\r
2217 FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal);\r
2218\r
2219 //\r
2220 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang\r
2221 //\r
2222 AutoUpdateLangVariable (VariableName, Data, DataSize);\r
2223\r
2224 Status = UpdateVariable (VariableName, VendorGuid, Data, DataSize, Attributes, &Variable);\r
2225\r
fdb7765f 2226 InterlockedDecrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState);\r
052ad7e1 2227 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
fdb7765f 2228\r
8d3a5c82 2229 return Status;\r
2230}\r
2231\r
7c80e839 2232/**\r
8d3a5c82 2233\r
2234 This code returns information about the EFI variables.\r
2235\r
7c80e839 2236 @param Attributes Attributes bitmask to specify the type of variables\r
2237 on which to return information.\r
2238 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available\r
2239 for the EFI variables associated with the attributes specified.\r
2240 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available\r
2241 for EFI variables associated with the attributes specified.\r
2242 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables\r
2243 associated with the attributes specified.\r
8d3a5c82 2244\r
7c80e839 2245 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.\r
2246 @return EFI_SUCCESS Query successfully.\r
2247 @return EFI_UNSUPPORTED The attribute is not supported on this platform.\r
8d3a5c82 2248\r
7c80e839 2249**/\r
052ad7e1
A
2250EFI_STATUS\r
2251EFIAPI\r
2252RuntimeServiceQueryVariableInfo (\r
2253 IN UINT32 Attributes,\r
2254 OUT UINT64 *MaximumVariableStorageSize,\r
2255 OUT UINT64 *RemainingVariableStorageSize,\r
2256 OUT UINT64 *MaximumVariableSize\r
2257 )\r
8d3a5c82 2258{\r
2259 VARIABLE_HEADER *Variable;\r
2260 VARIABLE_HEADER *NextVariable;\r
2261 UINT64 VariableSize;\r
2262 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
2fcdca1d 2263 UINT64 CommonVariableTotalSize;\r
2264 UINT64 HwErrVariableTotalSize;\r
2265\r
2266 CommonVariableTotalSize = 0;\r
2267 HwErrVariableTotalSize = 0;\r
8d3a5c82 2268\r
c6492839 2269 if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {\r
8d3a5c82 2270 return EFI_INVALID_PARAMETER;\r
2271 }\r
2fcdca1d 2272\r
c6492839 2273 if((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == 0) {\r
8d3a5c82 2274 //\r
2275 // Make sure the Attributes combination is supported by the platform.\r
2276 //\r
c6492839 2277 return EFI_UNSUPPORTED; \r
8d3a5c82 2278 } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
2279 //\r
2280 // Make sure if runtime bit is set, boot service bit is set also.\r
2281 //\r
2282 return EFI_INVALID_PARAMETER;\r
45f6c85b 2283 } else if (EfiAtRuntime () && ((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0)) {\r
8d3a5c82 2284 //\r
2285 // Make sure RT Attribute is set if we are in Runtime phase.\r
2286 //\r
2287 return EFI_INVALID_PARAMETER;\r
2fcdca1d 2288 } else if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2289 //\r
2290 // Make sure Hw Attribute is set with NV.\r
2291 //\r
2292 return EFI_INVALID_PARAMETER;\r
8e38f18e 2293 } else if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {\r
2294 //\r
2295 // Not support authentiated variable write yet.\r
2296 //\r
2297 return EFI_UNSUPPORTED;\r
8d3a5c82 2298 }\r
2299\r
052ad7e1 2300 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
8d3a5c82 2301\r
2302 if((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r
2303 //\r
2304 // Query is Volatile related.\r
2305 //\r
052ad7e1 2306 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r
8d3a5c82 2307 } else {\r
2308 //\r
2309 // Query is Non-Volatile related.\r
2310 //\r
052ad7e1 2311 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);\r
8d3a5c82 2312 }\r
2313\r
2314 //\r
2315 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize\r
2316 // with the storage size (excluding the storage header size).\r
2317 //\r
2318 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);\r
c6492839 2319\r
2320 //\r
2321 // Harware error record variable needs larger size.\r
2322 //\r
2fcdca1d 2323 if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
188e4e84 2324 *MaximumVariableStorageSize = PcdGet32 (PcdHwErrStorageSize);\r
2325 *MaximumVariableSize = PcdGet32 (PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER);\r
2fcdca1d 2326 } else {\r
2327 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
188e4e84 2328 ASSERT (PcdGet32 (PcdHwErrStorageSize) < VariableStoreHeader->Size);\r
2329 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize);\r
2fcdca1d 2330 }\r
2331\r
2332 //\r
188e4e84 2333 // Let *MaximumVariableSize be PcdGet32 (PcdMaxVariableSize) with the exception of the variable header size.\r
2fcdca1d 2334 //\r
188e4e84 2335 *MaximumVariableSize = PcdGet32 (PcdMaxVariableSize) - sizeof (VARIABLE_HEADER);\r
c6492839 2336 }\r
8d3a5c82 2337\r
2338 //\r
2339 // Point to the starting address of the variables.\r
2340 //\r
9cad030b 2341 Variable = GetStartPointer (VariableStoreHeader);\r
8d3a5c82 2342\r
2343 //\r
2344 // Now walk through the related variable store.\r
2345 //\r
6f90dfbc 2346 while ((Variable < GetEndPointer (VariableStoreHeader)) && IsValidVariableHeader (Variable)) {\r
8d3a5c82 2347 NextVariable = GetNextVariablePtr (Variable);\r
2348 VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;\r
2349\r
2350 if (EfiAtRuntime ()) {\r
2351 //\r
2352 // we don't take the state of the variables in mind\r
2353 // when calculating RemainingVariableStorageSize,\r
2354 // since the space occupied by variables not marked with\r
2355 // VAR_ADDED is not allowed to be reclaimed in Runtime.\r
2356 //\r
2fcdca1d 2357 if ((NextVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2358 HwErrVariableTotalSize += VariableSize;\r
2359 } else {\r
2360 CommonVariableTotalSize += VariableSize;\r
2361 }\r
8d3a5c82 2362 } else {\r
2363 //\r
2364 // Only care about Variables with State VAR_ADDED,because\r
2365 // the space not marked as VAR_ADDED is reclaimable now.\r
2366 //\r
2367 if (Variable->State == VAR_ADDED) {\r
2fcdca1d 2368 if ((NextVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
2369 HwErrVariableTotalSize += VariableSize;\r
2370 } else {\r
2371 CommonVariableTotalSize += VariableSize;\r
2372 }\r
8d3a5c82 2373 }\r
2374 }\r
2375\r
2376 //\r
2377 // Go to the next one\r
2378 //\r
2379 Variable = NextVariable;\r
2380 }\r
2381\r
2fcdca1d 2382 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD){\r
2383 *RemainingVariableStorageSize = *MaximumVariableStorageSize - HwErrVariableTotalSize;\r
2384 }else {\r
2385 *RemainingVariableStorageSize = *MaximumVariableStorageSize - CommonVariableTotalSize;\r
2386 }\r
2387\r
c6492839 2388 if (*RemainingVariableStorageSize < sizeof (VARIABLE_HEADER)) {\r
2389 *MaximumVariableSize = 0;\r
2390 } else if ((*RemainingVariableStorageSize - sizeof (VARIABLE_HEADER)) < *MaximumVariableSize) {\r
2391 *MaximumVariableSize = *RemainingVariableStorageSize - sizeof (VARIABLE_HEADER);\r
2392 }\r
2393\r
052ad7e1 2394 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
8d3a5c82 2395 return EFI_SUCCESS;\r
2396}\r
2397\r
7c80e839 2398\r
2399/**\r
2400 Notification function of EVT_GROUP_READY_TO_BOOT event group.\r
2401\r
2402 This is a notification function registered on EVT_GROUP_READY_TO_BOOT event group.\r
2403 When the Boot Manager is about to load and execute a boot option, it reclaims variable\r
2404 storage if free size is below the threshold.\r
2405\r
2406 @param Event Event whose notification function is being invoked\r
2407 @param Context Pointer to the notification function's context\r
2408\r
2409**/\r
7800593d
LG
2410VOID\r
2411EFIAPI\r
2412ReclaimForOS(\r
2413 EFI_EVENT Event,\r
2414 VOID *Context\r
2415 )\r
2416{\r
9948c0b0 2417 EFI_STATUS Status;\r
2fcdca1d 2418 UINTN CommonVariableSpace;\r
2419 UINTN RemainingCommonVariableSpace;\r
2420 UINTN RemainingHwErrVariableSpace;\r
7800593d 2421\r
7800593d
LG
2422 Status = EFI_SUCCESS; \r
2423\r
2fcdca1d 2424 CommonVariableSpace = ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase)))->Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32(PcdHwErrStorageSize); //Allowable max size of common variable storage space\r
2425\r
2426 RemainingCommonVariableSpace = CommonVariableSpace - mVariableModuleGlobal->CommonVariableTotalSize;\r
2427\r
2428 RemainingHwErrVariableSpace = PcdGet32 (PcdHwErrStorageSize) - mVariableModuleGlobal->HwErrVariableTotalSize;\r
7800593d 2429 //\r
9948c0b0 2430 // Check if the free area is blow a threshold.\r
7800593d 2431 //\r
2fcdca1d 2432 if ((RemainingCommonVariableSpace < PcdGet32 (PcdMaxVariableSize))\r
9948c0b0 2433 || ((PcdGet32 (PcdHwErrStorageSize) != 0) && \r
2434 (RemainingHwErrVariableSpace < PcdGet32 (PcdMaxHardwareErrorVariableSize)))){\r
7800593d 2435 Status = Reclaim (\r
2fcdca1d 2436 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
2437 &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
2438 FALSE,\r
2439 NULL\r
2440 );\r
7800593d
LG
2441 ASSERT_EFI_ERROR (Status);\r
2442 }\r
2443}\r
2444\r
7c80e839 2445/**\r
2446 Initializes variable store area for non-volatile and volatile variable.\r
2447\r
aa75dfec 2448 @param FvbProtocol Pointer to an instance of EFI Firmware Volume Block Protocol.\r
7c80e839 2449\r
2450 @retval EFI_SUCCESS Function successfully executed.\r
2451 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.\r
2452\r
2453**/\r
8d3a5c82 2454EFI_STATUS\r
8d3a5c82 2455VariableCommonInitialize (\r
8a9e0b72 2456 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvbProtocol\r
8d3a5c82 2457 )\r
8d3a5c82 2458{\r
2459 EFI_STATUS Status;\r
8d3a5c82 2460 VARIABLE_STORE_HEADER *VolatileVariableStore;\r
2461 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
2462 VARIABLE_HEADER *NextVariable;\r
8a9e0b72 2463 EFI_PHYSICAL_ADDRESS TempVariableStoreHeader;\r
8d3a5c82 2464 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;\r
8a9e0b72 2465 EFI_PHYSICAL_ADDRESS BaseAddress;\r
8d3a5c82 2466 UINT64 Length;\r
2467 UINTN Index;\r
2468 UINT8 Data;\r
8a9e0b72 2469 EFI_PHYSICAL_ADDRESS VariableStoreBase;\r
052ad7e1 2470 UINT64 VariableStoreLength;\r
7800593d 2471 EFI_EVENT ReadyToBootEvent;\r
2fcdca1d 2472 UINTN ScratchSize;\r
aa75dfec 2473 UINTN VariableSize;\r
8d3a5c82 2474\r
7800593d
LG
2475 Status = EFI_SUCCESS;\r
2476 //\r
2477 // Allocate runtime memory for variable driver global structure.\r
2478 //\r
72399dae 2479 mVariableModuleGlobal = AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL));\r
7800593d
LG
2480 if (mVariableModuleGlobal == NULL) {\r
2481 return EFI_OUT_OF_RESOURCES;\r
2482 }\r
8d3a5c82 2483\r
052ad7e1 2484 EfiInitializeLock(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock, TPL_NOTIFY);\r
8d3a5c82 2485\r
48cd992a 2486 //\r
2487 // Note that in EdkII variable driver implementation, Hardware Error Record type variable\r
2488 // is stored with common variable in the same NV region. So the platform integrator should\r
2489 // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of \r
2490 // PcdFlashNvStorageVariableSize.\r
2491 //\r
188e4e84 2492 ASSERT (PcdGet32 (PcdHwErrStorageSize) <= PcdGet32 (PcdFlashNvStorageVariableSize));\r
48cd992a 2493\r
8d3a5c82 2494 //\r
2fcdca1d 2495 // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.\r
8d3a5c82 2496 //\r
188e4e84 2497 ScratchSize = MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize));\r
2498 VolatileVariableStore = AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize) + ScratchSize);\r
8d3a5c82 2499 if (VolatileVariableStore == NULL) {\r
2500 FreePool (mVariableModuleGlobal);\r
2501 return EFI_OUT_OF_RESOURCES;\r
2502 }\r
2503\r
188e4e84 2504 SetMem (VolatileVariableStore, PcdGet32 (PcdVariableStoreSize) + ScratchSize, 0xff);\r
8d3a5c82 2505\r
2506 //\r
2507 // Variable Specific Data\r
2508 //\r
052ad7e1 2509 mVariableModuleGlobal->VariableGlobal.VolatileVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VolatileVariableStore;\r
9cad030b 2510 mVariableModuleGlobal->VolatileLastVariableOffset = (UINTN) GetStartPointer (VolatileVariableStore) - (UINTN) VolatileVariableStore;\r
8a9e0b72 2511 mVariableModuleGlobal->FvbInstance = FvbProtocol;\r
8d3a5c82 2512\r
3709c4cd 2513 CopyGuid (&VolatileVariableStore->Signature, &gEfiVariableGuid);\r
188e4e84 2514 VolatileVariableStore->Size = PcdGet32 (PcdVariableStoreSize);\r
8d3a5c82 2515 VolatileVariableStore->Format = VARIABLE_STORE_FORMATTED;\r
2516 VolatileVariableStore->State = VARIABLE_STORE_HEALTHY;\r
2517 VolatileVariableStore->Reserved = 0;\r
2518 VolatileVariableStore->Reserved1 = 0;\r
2519\r
2520 //\r
2521 // Get non volatile varaible store\r
2522 //\r
2523\r
92a4f6f3 2524 TempVariableStoreHeader = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);\r
2525 if (TempVariableStoreHeader == 0) {\r
2526 TempVariableStoreHeader = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);\r
2527 }\r
2528 \r
052ad7e1 2529 VariableStoreBase = TempVariableStoreHeader + \\r
8a9e0b72 2530 (((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(TempVariableStoreHeader)) -> HeaderLength);\r
052ad7e1 2531 VariableStoreLength = (UINT64) PcdGet32 (PcdFlashNvStorageVariableSize) - \\r
8a9e0b72 2532 (((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(TempVariableStoreHeader)) -> HeaderLength);\r
8d3a5c82 2533 //\r
2534 // Mark the variable storage region of the FLASH as RUNTIME\r
2535 //\r
052ad7e1
A
2536 BaseAddress = VariableStoreBase & (~EFI_PAGE_MASK);\r
2537 Length = VariableStoreLength + (VariableStoreBase - BaseAddress);\r
8d3a5c82 2538 Length = (Length + EFI_PAGE_SIZE - 1) & (~EFI_PAGE_MASK);\r
2539\r
2540 Status = gDS->GetMemorySpaceDescriptor (BaseAddress, &GcdDescriptor);\r
2541 if (EFI_ERROR (Status)) {\r
7800593d 2542 goto Done;\r
8d3a5c82 2543 }\r
2544\r
2545 Status = gDS->SetMemorySpaceAttributes (\r
2546 BaseAddress,\r
2547 Length,\r
2548 GcdDescriptor.Attributes | EFI_MEMORY_RUNTIME\r
2549 );\r
2550 if (EFI_ERROR (Status)) {\r
7800593d 2551 goto Done;\r
8d3a5c82 2552 }\r
2553 //\r
2554 // Get address of non volatile variable store base\r
2555 //\r
052ad7e1 2556 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase;\r
8a9e0b72 2557 VariableStoreHeader = (VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase;\r
8d3a5c82 2558 if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) {\r
2559 if (~VariableStoreHeader->Size == 0) {\r
2560 Status = UpdateVariableStore (\r
052ad7e1 2561 &mVariableModuleGlobal->VariableGlobal,\r
8d3a5c82 2562 FALSE,\r
2563 FALSE,\r
2564 mVariableModuleGlobal->FvbInstance,\r
2565 (UINTN) &VariableStoreHeader->Size,\r
2566 sizeof (UINT32),\r
052ad7e1 2567 (UINT8 *) &VariableStoreLength\r
8d3a5c82 2568 );\r
2569 //\r
2570 // As Variables are stored in NV storage, which are slow devices,such as flash.\r
2571 // Variable operation may skip checking variable program result to improve performance,\r
2572 // We can assume Variable program is OK through some check point.\r
2573 // Variable Store Size Setting should be the first Variable write operation,\r
2574 // We can assume all Read/Write is OK if we can set Variable store size successfully.\r
2575 // If write fail, we will assert here\r
2576 //\r
052ad7e1 2577 ASSERT(VariableStoreHeader->Size == VariableStoreLength);\r
8d3a5c82 2578\r
2579 if (EFI_ERROR (Status)) {\r
7800593d 2580 goto Done;\r
8d3a5c82 2581 }\r
2582 }\r
2583\r
8d3a5c82 2584 //\r
2585 // Parse non-volatile variable data and get last variable offset\r
2586 //\r
8a9e0b72 2587 NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase);\r
8d3a5c82 2588 Status = EFI_SUCCESS;\r
2589\r
2590 while (IsValidVariableHeader (NextVariable)) {\r
2fcdca1d 2591 VariableSize = NextVariable->NameSize + NextVariable->DataSize + sizeof (VARIABLE_HEADER);\r
2592 if ((NextVariable->Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
2593 mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VariableSize);\r
2594 } else {\r
2595 mVariableModuleGlobal->CommonVariableTotalSize += HEADER_ALIGN (VariableSize);\r
2596 }\r
2597\r
8d3a5c82 2598 NextVariable = GetNextVariablePtr (NextVariable);\r
2599 }\r
2600\r
8a9e0b72 2601 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) VariableStoreBase;\r
8d3a5c82 2602\r
8d3a5c82 2603 //\r
2604 // Check if the free area is really free.\r
2605 //\r
2606 for (Index = mVariableModuleGlobal->NonVolatileLastVariableOffset; Index < VariableStoreHeader->Size; Index++) {\r
052ad7e1 2607 Data = ((UINT8 *) (UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase)[Index];\r
8d3a5c82 2608 if (Data != 0xff) {\r
2609 //\r
2610 // There must be something wrong in variable store, do reclaim operation.\r
2611 //\r
2612 Status = Reclaim (\r
052ad7e1 2613 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
8d3a5c82 2614 &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
814bae52 2615 FALSE,\r
2616 NULL\r
8d3a5c82 2617 );\r
7800593d
LG
2618\r
2619 if (EFI_ERROR (Status)) {\r
2620 goto Done;\r
2621 }\r
2622\r
8d3a5c82 2623 break;\r
2624 }\r
2625 }\r
7800593d
LG
2626\r
2627 //\r
2628 // Register the event handling function to reclaim variable for OS usage.\r
2629 //\r
2630 Status = EfiCreateEventReadyToBootEx (\r
2631 TPL_NOTIFY, \r
2632 ReclaimForOS, \r
2633 NULL, \r
2634 &ReadyToBootEvent\r
2635 );\r
5bb820af 2636 } else {\r
2637 Status = EFI_VOLUME_CORRUPTED;\r
2638 DEBUG((EFI_D_INFO, "Variable Store header is corrupted\n"));\r
8d3a5c82 2639 }\r
2640\r
7800593d 2641Done:\r
8d3a5c82 2642 if (EFI_ERROR (Status)) {\r
2643 FreePool (mVariableModuleGlobal);\r
2644 FreePool (VolatileVariableStore);\r
2645 }\r
2646\r
2647 return Status;\r
2648}\r
052ad7e1 2649\r
7c80e839 2650/**\r
2651 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE\r
2652\r
2653 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
2654 It convers pointer to new virtual address.\r
2655\r
2656 @param Event Event whose notification function is being invoked\r
2657 @param Context Pointer to the notification function's context\r
2658\r
2659**/\r
052ad7e1
A
2660VOID\r
2661EFIAPI\r
2662VariableClassAddressChangeEvent (\r
2663 IN EFI_EVENT Event,\r
2664 IN VOID *Context\r
2665 )\r
2666{\r
8a9e0b72 2667 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->GetBlockSize);\r
2668 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->GetPhysicalAddress);\r
2669 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->GetAttributes);\r
2670 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->SetAttributes);\r
2671 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->Read);\r
2672 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->Write);\r
2673 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->EraseBlocks);\r
2674 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance);\r
255a3f33
RN
2675 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->PlatformLangCodes);\r
2676 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->LangCodes);\r
2677 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->PlatformLang);\r
052ad7e1
A
2678 EfiConvertPointer (\r
2679 0x0,\r
2680 (VOID **) &mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase\r
2681 );\r
2682 EfiConvertPointer (\r
2683 0x0,\r
2684 (VOID **) &mVariableModuleGlobal->VariableGlobal.VolatileVariableBase\r
2685 );\r
2686 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal);\r
2687}\r
2688\r
aa75dfec 2689/**\r
2690 Firmware Volume Block Protocol notification event handler.\r
2691\r
2692 Discover NV Variable Store and install Variable Arch Protocol.\r
2693\r
2694 @param[in] Event Event whose notification function is being invoked.\r
2695 @param[in] Context Pointer to the notification function's context.\r
2696**/\r
8a9e0b72 2697VOID\r
2698EFIAPI\r
2699FvbNotificationEvent (\r
2700 IN EFI_EVENT Event,\r
2701 IN VOID *Context\r
2702 )\r
2703{\r
2704 EFI_STATUS Status;\r
2705 EFI_HANDLE *HandleBuffer;\r
8a9e0b72 2706 UINTN HandleCount;\r
2707 UINTN Index;\r
2708 EFI_PHYSICAL_ADDRESS FvbBaseAddress;\r
2709 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r
2710 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r
2711 EFI_FVB_ATTRIBUTES_2 Attributes;\r
2712 EFI_SYSTEM_TABLE *SystemTable;\r
2713 EFI_PHYSICAL_ADDRESS NvStorageVariableBase;\r
2714\r
2715 SystemTable = (EFI_SYSTEM_TABLE *)Context;\r
2716 Fvb = NULL;\r
8a9e0b72 2717 \r
2718 //\r
2719 // Locate all handles of Fvb protocol\r
2720 //\r
2721 Status = gBS->LocateHandleBuffer (\r
2722 ByProtocol,\r
2723 &gEfiFirmwareVolumeBlockProtocolGuid,\r
2724 NULL,\r
2725 &HandleCount,\r
2726 &HandleBuffer\r
2727 );\r
2728 if (EFI_ERROR (Status)) {\r
2729 return ;\r
2730 }\r
2731 \r
2732 //\r
2733 // Get the FVB to access variable store\r
2734 //\r
f0480ecf 2735 for (Index = 0; Index < HandleCount; Index += 1, Status = EFI_NOT_FOUND, Fvb = NULL) {\r
8a9e0b72 2736 Status = gBS->HandleProtocol (\r
2737 HandleBuffer[Index],\r
2738 &gEfiFirmwareVolumeBlockProtocolGuid,\r
2739 (VOID **) &Fvb\r
2740 );\r
2741 if (EFI_ERROR (Status)) {\r
2742 Status = EFI_NOT_FOUND;\r
2743 break;\r
2744 }\r
2745\r
2746 //\r
2747 // Ensure this FVB protocol supported Write operation.\r
2748 //\r
2749 Status = Fvb->GetAttributes (Fvb, &Attributes);\r
2750 if (EFI_ERROR (Status) || ((Attributes & EFI_FVB2_WRITE_STATUS) == 0)) {\r
2751 continue; \r
2752 }\r
2753 //\r
2754 // Compare the address and select the right one\r
2755 //\r
2756 Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);\r
2757 if (EFI_ERROR (Status)) {\r
2758 continue;\r
2759 }\r
2760\r
2761 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvbBaseAddress);\r
92a4f6f3 2762 NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);\r
2763 if (NvStorageVariableBase == 0) {\r
2764 NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);\r
2765 }\r
2766 \r
8a9e0b72 2767 if ((NvStorageVariableBase >= FvbBaseAddress) && (NvStorageVariableBase < (FvbBaseAddress + FwVolHeader->FvLength))) {\r
8a9e0b72 2768 Status = EFI_SUCCESS;\r
2769 break;\r
2770 }\r
2771 }\r
2772\r
2773 FreePool (HandleBuffer);\r
f0480ecf 2774 if (!EFI_ERROR (Status) && Fvb != NULL) {\r
533020ef 2775 //\r
2776 // Close the notify event to avoid install gEfiVariableArchProtocolGuid & gEfiVariableWriteArchProtocolGuid again.\r
2777 //\r
2778 Status = gBS->CloseEvent (Event); \r
2779 ASSERT_EFI_ERROR (Status);\r
2780\r
8a9e0b72 2781 Status = VariableCommonInitialize (Fvb);\r
2782 ASSERT_EFI_ERROR (Status);\r
2783 \r
79749182 2784 SystemTable->RuntimeServices->GetVariable = RuntimeServiceGetVariable;\r
8a9e0b72 2785 SystemTable->RuntimeServices->GetNextVariableName = RuntimeServiceGetNextVariableName;\r
79749182 2786 SystemTable->RuntimeServices->SetVariable = RuntimeServiceSetVariable;\r
2787 SystemTable->RuntimeServices->QueryVariableInfo = RuntimeServiceQueryVariableInfo;\r
8a9e0b72 2788 \r
2789 //\r
2790 // Now install the Variable Runtime Architectural Protocol on a new handle\r
2791 //\r
2792 Status = gBS->InstallMultipleProtocolInterfaces (\r
2793 &mHandle,\r
2794 &gEfiVariableArchProtocolGuid, NULL,\r
2795 &gEfiVariableWriteArchProtocolGuid, NULL,\r
2796 NULL\r
2797 );\r
2798 ASSERT_EFI_ERROR (Status);\r
2799 \r
2800 Status = gBS->CreateEventEx (\r
2801 EVT_NOTIFY_SIGNAL,\r
2802 TPL_NOTIFY,\r
2803 VariableClassAddressChangeEvent,\r
2804 NULL,\r
2805 &gEfiEventVirtualAddressChangeGuid,\r
2806 &mVirtualAddressChangeEvent\r
2807 );\r
2808 ASSERT_EFI_ERROR (Status);\r
2809 }\r
2810\r
2811}\r
052ad7e1
A
2812\r
2813/**\r
2814 Variable Driver main entry point. The Variable driver places the 4 EFI\r
2815 runtime services in the EFI System Table and installs arch protocols \r
7c80e839 2816 for variable read and write services being availible. It also registers\r
2817 notification function for EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
052ad7e1
A
2818\r
2819 @param[in] ImageHandle The firmware allocated handle for the EFI image. \r
2820 @param[in] SystemTable A pointer to the EFI System Table.\r
2821 \r
7c80e839 2822 @retval EFI_SUCCESS Variable service successfully initialized.\r
052ad7e1
A
2823\r
2824**/\r
2825EFI_STATUS\r
2826EFIAPI\r
2827VariableServiceInitialize (\r
2828 IN EFI_HANDLE ImageHandle,\r
2829 IN EFI_SYSTEM_TABLE *SystemTable\r
2830 )\r
2831{\r
052ad7e1 2832 //\r
8a9e0b72 2833 // Register FvbNotificationEvent () notify function.\r
2834 // \r
2835 EfiCreateProtocolNotifyEvent (\r
2836 &gEfiFirmwareVolumeBlockProtocolGuid,\r
2837 TPL_CALLBACK,\r
2838 FvbNotificationEvent,\r
2839 (VOID *)SystemTable,\r
2840 &mFvbRegistration\r
2841 );\r
052ad7e1
A
2842\r
2843 return EFI_SUCCESS;\r
2844}\r
2845\r