Change EFI_OPTIONAL_POINTER to EFI_OPTIONAL_PTR
[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
504214c4
LG
6Copyright (c) 2006 - 2008, Intel Corporation \r
7All rights reserved. This program and the accompanying materials \r
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
17\r
18#include "Variable.h"\r
33a5a666 19\r
7800593d 20VARIABLE_MODULE_GLOBAL *mVariableModuleGlobal;\r
052ad7e1
A
21EFI_EVENT mVirtualAddressChangeEvent = NULL;\r
22EFI_HANDLE mHandle = NULL;\r
8d3a5c82 23\r
aa79b0b3 24//\r
25// The current Hii implementation accesses this variable a larg # of times on every boot.\r
26// Other common variables are only accessed a single time. This is why this cache algorithm\r
27// only targets a single variable. Probably to get an performance improvement out of\r
28// a Cache you would need a cache that improves the search performance for a variable.\r
29//\r
30VARIABLE_CACHE_ENTRY mVariableCache[] = {\r
31 {\r
32 &gEfiGlobalVariableGuid,\r
33 L"Lang",\r
34 0x00000000,\r
35 0x00,\r
36 NULL\r
37 }\r
38};\r
39\r
40GLOBAL_REMOVE_IF_UNREFERENCED VARIABLE_INFO_ENTRY *gVariableInfo = NULL;\r
41\r
42\r
33a5a666 43\r
8d3a5c82 44//\r
45// This is a temperary function which will be removed\r
46// when EfiAcquireLock in UefiLib can handle the\r
47// the call in UEFI Runtimer driver in RT phase.\r
48//\r
8d3a5c82 49VOID\r
50AcquireLockOnlyAtBootTime (\r
51 IN EFI_LOCK *Lock\r
52 )\r
53{\r
54 if (!EfiAtRuntime ()) {\r
55 EfiAcquireLock (Lock);\r
56 }\r
57}\r
58\r
59//\r
60// This is a temperary function which will be removed\r
61// when EfiAcquireLock in UefiLib can handle the\r
62// the call in UEFI Runtimer driver in RT phase.\r
63//\r
8d3a5c82 64VOID\r
65ReleaseLockOnlyAtBootTime (\r
66 IN EFI_LOCK *Lock\r
67 )\r
68{\r
69 if (!EfiAtRuntime ()) {\r
70 EfiReleaseLock (Lock);\r
71 }\r
72}\r
73\r
33a5a666 74\r
052ad7e1
A
75/**\r
76 Routine used to track statistical information about variable usage. \r
77 The data is stored in the EFI system table so it can be accessed later.\r
78 VariableInfo.efi can dump out the table. Only Boot Services variable \r
79 accesses are tracked by this code. The PcdVariableCollectStatistics\r
80 build flag controls if this feature is enabled. \r
81\r
82 A read that hits in the cache will have Read and Cache true for \r
83 the transaction. Data is allocated by this routine, but never\r
84 freed.\r
85\r
86 @param[in] VariableName Name of the Variable to track\r
87 @param[in] VendorGuid Guid of the Variable to track\r
88 @param[in] Volatile TRUE if volatile FALSE if non-volatile\r
89 @param[in] Read TRUE if GetVariable() was called\r
90 @param[in] Write TRUE if SetVariable() was called\r
91 @param[in] Delete TRUE if deleted via SetVariable()\r
92 @param[in] Cache TRUE for a cache hit.\r
93\r
94**/\r
33a5a666
A
95VOID\r
96UpdateVariableInfo (\r
97 IN CHAR16 *VariableName,\r
98 IN EFI_GUID *VendorGuid,\r
99 IN BOOLEAN Volatile,\r
100 IN BOOLEAN Read,\r
101 IN BOOLEAN Write,\r
102 IN BOOLEAN Delete,\r
103 IN BOOLEAN Cache\r
104 )\r
105{\r
106 VARIABLE_INFO_ENTRY *Entry;\r
107\r
108 if (FeaturePcdGet (PcdVariableCollectStatistics)) {\r
109\r
110 if (EfiAtRuntime ()) {\r
111 // Don't collect statistics at runtime\r
112 return;\r
113 }\r
114\r
115 if (gVariableInfo == NULL) {\r
052ad7e1
A
116 //\r
117 // on the first call allocate a entry and place a pointer to it in\r
118 // the EFI System Table\r
119 //\r
33a5a666 120 gVariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));\r
052ad7e1
A
121 ASSERT (gVariableInfo != NULL);\r
122\r
33a5a666
A
123 CopyGuid (&gVariableInfo->VendorGuid, VendorGuid);\r
124 gVariableInfo->Name = AllocatePool (StrLen (VariableName));\r
125 StrCpy (gVariableInfo->Name, VariableName);\r
126 gVariableInfo->Volatile = Volatile;\r
127\r
128 gBS->InstallConfigurationTable (&gEfiVariableInfoGuid, gVariableInfo);\r
129 }\r
130\r
131 \r
132 for (Entry = gVariableInfo; Entry != NULL; Entry = Entry->Next) {\r
133 if (CompareGuid (VendorGuid, &Entry->VendorGuid)) {\r
134 if (StrCmp (VariableName, Entry->Name) == 0) {\r
135 if (Read) {\r
136 Entry->ReadCount++;\r
137 }\r
138 if (Write) {\r
139 Entry->WriteCount++;\r
140 }\r
141 if (Delete) {\r
142 Entry->DeleteCount++;\r
143 }\r
144 if (Cache) {\r
145 Entry->CacheCount++;\r
146 }\r
147\r
148 return;\r
149 }\r
150 }\r
151\r
152 if (Entry->Next == NULL) {\r
052ad7e1
A
153 //\r
154 // If the entry is not in the table add it.\r
155 // Next iteration of the loop will fill in the data\r
156 //\r
33a5a666 157 Entry->Next = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));\r
052ad7e1 158 ASSERT (Entry->Next != NULL);\r
33a5a666
A
159\r
160 CopyGuid (&Entry->Next->VendorGuid, VendorGuid);\r
161 Entry->Next->Name = AllocatePool (StrLen (VariableName));\r
162 StrCpy (Entry->Next->Name, VariableName);\r
163 Entry->Next->Volatile = Volatile;\r
164 }\r
165\r
166 }\r
167 }\r
168}\r
169\r
170\r
8d3a5c82 171BOOLEAN\r
8d3a5c82 172IsValidVariableHeader (\r
173 IN VARIABLE_HEADER *Variable\r
174 )\r
175/*++\r
176\r
177Routine Description:\r
178\r
179 This code checks if variable header is valid or not.\r
180\r
181Arguments:\r
182 Variable Pointer to the Variable Header.\r
183\r
184Returns:\r
185 TRUE Variable header is valid.\r
186 FALSE Variable header is not valid.\r
187\r
188--*/\r
189{\r
fdb7765f 190 if (Variable == NULL || Variable->StartId != VARIABLE_DATA) {\r
8d3a5c82 191 return FALSE;\r
192 }\r
193\r
194 return TRUE;\r
195}\r
196\r
052ad7e1 197\r
8d3a5c82 198EFI_STATUS\r
8d3a5c82 199UpdateVariableStore (\r
200 IN VARIABLE_GLOBAL *Global,\r
201 IN BOOLEAN Volatile,\r
202 IN BOOLEAN SetByIndex,\r
203 IN UINTN Instance,\r
204 IN UINTN DataPtrIndex,\r
205 IN UINT32 DataSize,\r
206 IN UINT8 *Buffer\r
207 )\r
208/*++\r
209\r
210Routine Description:\r
211\r
212 This function writes data to the FWH at the correct LBA even if the LBAs\r
213 are fragmented.\r
214\r
215Arguments:\r
216\r
c6492839 217 Global - Pointer to VARAIBLE_GLOBAL structure\r
218 Volatile - If the Variable is Volatile or Non-Volatile\r
219 SetByIndex - TRUE: Target pointer is given as index\r
220 FALSE: Target pointer is absolute\r
221 Instance - Instance of FV Block services\r
222 DataPtrIndex - Pointer to the Data from the end of VARIABLE_STORE_HEADER\r
223 structure\r
224 DataSize - Size of data to be written.\r
225 Buffer - Pointer to the buffer from which data is written\r
8d3a5c82 226\r
227Returns:\r
228\r
c6492839 229 EFI_INVALID_PARAMETER - Parameters not valid\r
230 EFI_SUCCESS - Variable store successfully updated\r
8d3a5c82 231\r
232--*/\r
233{\r
234 EFI_FV_BLOCK_MAP_ENTRY *PtrBlockMapEntry;\r
235 UINTN BlockIndex2;\r
236 UINTN LinearOffset;\r
237 UINTN CurrWriteSize;\r
238 UINTN CurrWritePtr;\r
239 UINT8 *CurrBuffer;\r
240 EFI_LBA LbaNumber;\r
241 UINTN Size;\r
242 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r
243 VARIABLE_STORE_HEADER *VolatileBase;\r
244 EFI_PHYSICAL_ADDRESS FvVolHdr;\r
245 EFI_PHYSICAL_ADDRESS DataPtr;\r
246 EFI_STATUS Status;\r
247\r
248 FwVolHeader = NULL;\r
249 DataPtr = DataPtrIndex;\r
250\r
251 //\r
252 // Check if the Data is Volatile\r
253 //\r
254 if (!Volatile) {\r
255 EfiFvbGetPhysicalAddress (Instance, &FvVolHdr);\r
256 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvVolHdr);\r
257 //\r
258 // Data Pointer should point to the actual Address where data is to be\r
259 // written\r
260 //\r
261 if (SetByIndex) {\r
052ad7e1 262 DataPtr += mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;\r
8d3a5c82 263 }\r
264\r
265 if ((DataPtr + DataSize) >= ((EFI_PHYSICAL_ADDRESS) (UINTN) ((UINT8 *) FwVolHeader + FwVolHeader->FvLength))) {\r
266 return EFI_INVALID_PARAMETER;\r
267 }\r
268 } else {\r
269 //\r
270 // Data Pointer should point to the actual Address where data is to be\r
271 // written\r
272 //\r
052ad7e1 273 VolatileBase = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r
8d3a5c82 274 if (SetByIndex) {\r
052ad7e1 275 DataPtr += mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;\r
8d3a5c82 276 }\r
277\r
278 if ((DataPtr + DataSize) >= ((UINTN) ((UINT8 *) VolatileBase + VolatileBase->Size))) {\r
279 return EFI_INVALID_PARAMETER;\r
280 }\r
c6492839 281 \r
282 //\r
283 // If Volatile Variable just do a simple mem copy.\r
284 // \r
285 CopyMem ((UINT8 *)(UINTN)DataPtr, Buffer, DataSize);\r
8d3a5c82 286 return EFI_SUCCESS;\r
287 }\r
c6492839 288 \r
8d3a5c82 289 //\r
290 // If we are here we are dealing with Non-Volatile Variables\r
291 //\r
292 LinearOffset = (UINTN) FwVolHeader;\r
293 CurrWritePtr = (UINTN) DataPtr;\r
294 CurrWriteSize = DataSize;\r
295 CurrBuffer = Buffer;\r
296 LbaNumber = 0;\r
297\r
298 if (CurrWritePtr < LinearOffset) {\r
299 return EFI_INVALID_PARAMETER;\r
300 }\r
301\r
302 for (PtrBlockMapEntry = FwVolHeader->BlockMap; PtrBlockMapEntry->NumBlocks != 0; PtrBlockMapEntry++) {\r
303 for (BlockIndex2 = 0; BlockIndex2 < PtrBlockMapEntry->NumBlocks; BlockIndex2++) {\r
304 //\r
305 // Check to see if the Variable Writes are spanning through multiple\r
306 // blocks.\r
307 //\r
308 if ((CurrWritePtr >= LinearOffset) && (CurrWritePtr < LinearOffset + PtrBlockMapEntry->Length)) {\r
309 if ((CurrWritePtr + CurrWriteSize) <= (LinearOffset + PtrBlockMapEntry->Length)) {\r
310 Status = EfiFvbWriteBlock (\r
311 Instance,\r
312 LbaNumber,\r
313 (UINTN) (CurrWritePtr - LinearOffset),\r
314 &CurrWriteSize,\r
315 CurrBuffer\r
316 );\r
8d3a5c82 317 return Status;\r
8d3a5c82 318 } else {\r
319 Size = (UINT32) (LinearOffset + PtrBlockMapEntry->Length - CurrWritePtr);\r
320 Status = EfiFvbWriteBlock (\r
321 Instance,\r
322 LbaNumber,\r
323 (UINTN) (CurrWritePtr - LinearOffset),\r
324 &Size,\r
325 CurrBuffer\r
326 );\r
327 if (EFI_ERROR (Status)) {\r
328 return Status;\r
329 }\r
330\r
331 CurrWritePtr = LinearOffset + PtrBlockMapEntry->Length;\r
332 CurrBuffer = CurrBuffer + Size;\r
333 CurrWriteSize = CurrWriteSize - Size;\r
334 }\r
335 }\r
336\r
337 LinearOffset += PtrBlockMapEntry->Length;\r
338 LbaNumber++;\r
339 }\r
340 }\r
341\r
342 return EFI_SUCCESS;\r
343}\r
344\r
052ad7e1 345\r
8d3a5c82 346VARIABLE_STORE_STATUS\r
8d3a5c82 347GetVariableStoreStatus (\r
348 IN VARIABLE_STORE_HEADER *VarStoreHeader\r
349 )\r
350/*++\r
351\r
352Routine Description:\r
353\r
354 This code gets the current status of Variable Store.\r
355\r
356Arguments:\r
357\r
358 VarStoreHeader Pointer to the Variable Store Header.\r
359\r
360Returns:\r
361\r
362 EfiRaw Variable store status is raw\r
363 EfiValid Variable store status is valid\r
364 EfiInvalid Variable store status is invalid\r
365\r
366--*/\r
367{\r
368 if (VarStoreHeader->Signature == VARIABLE_STORE_SIGNATURE &&\r
369 VarStoreHeader->Format == VARIABLE_STORE_FORMATTED &&\r
370 VarStoreHeader->State == VARIABLE_STORE_HEALTHY\r
371 ) {\r
372\r
373 return EfiValid;\r
374 } else if (VarStoreHeader->Signature == 0xffffffff &&\r
375 VarStoreHeader->Size == 0xffffffff &&\r
376 VarStoreHeader->Format == 0xff &&\r
377 VarStoreHeader->State == 0xff\r
378 ) {\r
379\r
380 return EfiRaw;\r
381 } else {\r
382 return EfiInvalid;\r
383 }\r
384}\r
385\r
130e2569 386\r
387UINTN\r
388NameSizeOfVariable (\r
389 IN VARIABLE_HEADER *Variable\r
390 )\r
391/*++\r
392\r
393Routine Description:\r
394\r
395 This code gets the size of name of variable.\r
396\r
397Arguments:\r
398\r
399 Variable Pointer to the Variable Header.\r
400\r
401Returns:\r
402\r
403 UINTN Size of variable in bytes\r
404\r
405--*/\r
406{\r
407 if (Variable->State == (UINT8) (-1) ||\r
408 Variable->DataSize == (UINT32) -1 ||\r
409 Variable->NameSize == (UINT32) -1 ||\r
410 Variable->Attributes == (UINT32) -1) {\r
411 return 0;\r
412 }\r
413 return (UINTN) Variable->NameSize;\r
414}\r
415\r
416UINTN\r
417DataSizeOfVariable (\r
418 IN VARIABLE_HEADER *Variable\r
419 )\r
420/*++\r
421\r
422Routine Description:\r
423\r
424 This code gets the size of name of variable.\r
425\r
426Arguments:\r
427\r
428 Variable Pointer to the Variable Header.\r
429\r
430Returns:\r
431\r
432 UINTN Size of variable in bytes\r
433\r
434--*/\r
435{\r
436 if (Variable->State == (UINT8) -1 ||\r
437 Variable->DataSize == (UINT32) -1 ||\r
438 Variable->NameSize == (UINT32) -1 ||\r
439 Variable->Attributes == (UINT32) -1) {\r
440 return 0;\r
441 }\r
442 return (UINTN) Variable->DataSize;\r
443}\r
444\r
445CHAR16 *\r
446GetVariableNamePtr (\r
447 IN VARIABLE_HEADER *Variable\r
448 )\r
449/*++\r
450\r
451Routine Description:\r
452\r
453 This code gets the pointer to the variable name.\r
454\r
455Arguments:\r
456\r
457 Variable Pointer to the Variable Header.\r
458\r
459Returns:\r
460\r
461 CHAR16* Pointer to Variable Name\r
462\r
463--*/\r
464{\r
465\r
466 return (CHAR16 *) (Variable + 1);\r
467}\r
468\r
8d3a5c82 469UINT8 *\r
8d3a5c82 470GetVariableDataPtr (\r
471 IN VARIABLE_HEADER *Variable\r
472 )\r
473/*++\r
474\r
475Routine Description:\r
476\r
477 This code gets the pointer to the variable data.\r
478\r
479Arguments:\r
480\r
481 Variable Pointer to the Variable Header.\r
482\r
483Returns:\r
484\r
485 UINT8* Pointer to Variable Data\r
486\r
487--*/\r
488{\r
130e2569 489 UINTN Value;\r
490 \r
8d3a5c82 491 //\r
492 // Be careful about pad size for alignment\r
493 //\r
130e2569 494 Value = (UINTN) GetVariableNamePtr (Variable);\r
495 Value += NameSizeOfVariable (Variable);\r
496 Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));\r
497\r
498 return (UINT8 *) Value;\r
8d3a5c82 499}\r
500\r
052ad7e1 501\r
8d3a5c82 502VARIABLE_HEADER *\r
8d3a5c82 503GetNextVariablePtr (\r
504 IN VARIABLE_HEADER *Variable\r
505 )\r
506/*++\r
507\r
508Routine Description:\r
509\r
510 This code gets the pointer to the next variable header.\r
511\r
512Arguments:\r
513\r
514 Variable Pointer to the Variable Header.\r
515\r
516Returns:\r
517\r
518 VARIABLE_HEADER* Pointer to next variable header.\r
519\r
520--*/\r
521{\r
130e2569 522 UINTN Value;\r
523\r
8d3a5c82 524 if (!IsValidVariableHeader (Variable)) {\r
525 return NULL;\r
526 }\r
130e2569 527\r
528 Value = (UINTN) GetVariableDataPtr (Variable);\r
529 Value += DataSizeOfVariable (Variable);\r
530 Value += GET_PAD_SIZE (DataSizeOfVariable (Variable));\r
531\r
8d3a5c82 532 //\r
533 // Be careful about pad size for alignment\r
534 //\r
130e2569 535 return (VARIABLE_HEADER *) HEADER_ALIGN (Value);\r
8d3a5c82 536}\r
537\r
9cad030b 538VARIABLE_HEADER *\r
539GetStartPointer (\r
540 IN VARIABLE_STORE_HEADER *VarStoreHeader\r
541 )\r
542/*++\r
543\r
544Routine Description:\r
545\r
546 This code gets the pointer to the first variable memory pointer byte\r
547\r
548Arguments:\r
549\r
550 VarStoreHeader Pointer to the Variable Store Header.\r
551\r
552Returns:\r
553\r
554 VARIABLE_HEADER* Pointer to last unavailable Variable Header\r
555\r
556--*/\r
557{\r
558 //\r
559 // The end of variable store\r
560 //\r
561 return (VARIABLE_HEADER *) HEADER_ALIGN (VarStoreHeader + 1);\r
562}\r
052ad7e1 563\r
8d3a5c82 564VARIABLE_HEADER *\r
8d3a5c82 565GetEndPointer (\r
566 IN VARIABLE_STORE_HEADER *VarStoreHeader\r
567 )\r
568/*++\r
569\r
570Routine Description:\r
571\r
572 This code gets the pointer to the last variable memory pointer byte\r
573\r
574Arguments:\r
575\r
576 VarStoreHeader Pointer to the Variable Store Header.\r
577\r
578Returns:\r
579\r
580 VARIABLE_HEADER* Pointer to last unavailable Variable Header\r
581\r
582--*/\r
583{\r
584 //\r
585 // The end of variable store\r
586 //\r
9cad030b 587 return (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) VarStoreHeader + VarStoreHeader->Size);\r
8d3a5c82 588}\r
589\r
052ad7e1 590\r
8d3a5c82 591EFI_STATUS\r
8d3a5c82 592Reclaim (\r
593 IN EFI_PHYSICAL_ADDRESS VariableBase,\r
594 OUT UINTN *LastVariableOffset,\r
814bae52 595 IN BOOLEAN IsVolatile,\r
596 IN VARIABLE_HEADER *UpdatingVariable\r
8d3a5c82 597 )\r
598/*++\r
599\r
600Routine Description:\r
601\r
602 Variable store garbage collection and reclaim operation\r
603\r
604Arguments:\r
605\r
606 VariableBase Base address of variable store\r
607 LastVariableOffset Offset of last variable\r
608 IsVolatile The variable store is volatile or not,\r
609 if it is non-volatile, need FTW\r
610\r
611Returns:\r
612\r
613 EFI STATUS\r
614\r
615--*/\r
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
637\r
638 //\r
639 // Start Pointers for the variable.\r
640 //\r
814bae52 641 Variable = GetStartPointer (VariableStoreHeader);\r
642 MaximumBufferSize = sizeof (VARIABLE_STORE_HEADER);\r
8d3a5c82 643\r
644 while (IsValidVariableHeader (Variable)) {\r
645 NextVariable = GetNextVariablePtr (Variable);\r
814bae52 646 if (Variable->State == VAR_ADDED || \r
647 Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)\r
648 ) {\r
8d3a5c82 649 VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
814bae52 650 MaximumBufferSize += VariableSize;\r
8d3a5c82 651 }\r
652\r
653 Variable = NextVariable;\r
654 }\r
655\r
814bae52 656 //\r
657 // Reserve the 1 Bytes with Oxff to identify the \r
658 // end of the variable buffer. \r
659 // \r
660 MaximumBufferSize += 1;\r
661 ValidBuffer = AllocatePool (MaximumBufferSize);\r
8d3a5c82 662 if (ValidBuffer == NULL) {\r
663 return EFI_OUT_OF_RESOURCES;\r
664 }\r
665\r
814bae52 666 SetMem (ValidBuffer, MaximumBufferSize, 0xff);\r
8d3a5c82 667\r
668 //\r
669 // Copy variable store header\r
670 //\r
814bae52 671 CopyMem (ValidBuffer, VariableStoreHeader, sizeof (VARIABLE_STORE_HEADER));\r
672 CurrPtr = (UINT8 *) GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer);\r
8d3a5c82 673\r
674 //\r
675 // Start Pointers for the variable.\r
676 //\r
8d3a5c82 677\r
814bae52 678 //\r
5ead4a07 679 // Reinstall all ADDED variables as long as they are not identical to Updating Variable\r
814bae52 680 // \r
681 Variable = GetStartPointer (VariableStoreHeader);\r
8d3a5c82 682 while (IsValidVariableHeader (Variable)) {\r
683 NextVariable = GetNextVariablePtr (Variable);\r
684 if (Variable->State == VAR_ADDED) {\r
5ead4a07 685 if (UpdatingVariable != NULL) {\r
686 if (UpdatingVariable == Variable) {\r
687 Variable = NextVariable;\r
688 continue;\r
689 }\r
49e70927 690\r
691 VariableNameSize = NameSizeOfVariable(Variable);\r
692 UpdatingVariableNameSize = NameSizeOfVariable(UpdatingVariable);\r
693\r
694 VariableNamePtr = GetVariableNamePtr (Variable);\r
695 UpdatingVariableNamePtr = GetVariableNamePtr (UpdatingVariable);\r
5ead4a07 696 if (CompareGuid (&Variable->VendorGuid, &UpdatingVariable->VendorGuid) &&\r
49e70927 697 VariableNameSize == UpdatingVariableNameSize &&\r
698 CompareMem (VariableNamePtr, UpdatingVariableNamePtr, VariableNameSize) == 0 ) {\r
5ead4a07 699 Variable = NextVariable;\r
700 continue;\r
701 }\r
702 }\r
8d3a5c82 703 VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
704 CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);\r
705 CurrPtr += VariableSize;\r
706 }\r
8d3a5c82 707 Variable = NextVariable;\r
708 }\r
5ead4a07 709\r
710 //\r
711 // Reinstall the variable being updated if it is not NULL\r
712 //\r
713 if (UpdatingVariable != NULL) {\r
714 VariableSize = (UINTN)(GetNextVariablePtr (UpdatingVariable)) - (UINTN)UpdatingVariable;\r
715 CopyMem (CurrPtr, (UINT8 *) UpdatingVariable, VariableSize);\r
716 CurrPtr += VariableSize;\r
717 }\r
718\r
814bae52 719 //\r
720 // Reinstall all in delete transition variables\r
721 // \r
722 Variable = GetStartPointer (VariableStoreHeader);\r
723 while (IsValidVariableHeader (Variable)) {\r
724 NextVariable = GetNextVariablePtr (Variable);\r
5ead4a07 725 if (Variable != UpdatingVariable && Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
814bae52 726\r
727 //\r
728 // Buffer has cached all ADDED variable. \r
729 // Per IN_DELETED variable, we have to guarantee that\r
730 // no ADDED one in previous buffer. \r
731 // \r
732 \r
733 FoundAdded = FALSE;\r
734 AddedVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer);\r
735 while (IsValidVariableHeader (AddedVariable)) {\r
736 NextAddedVariable = GetNextVariablePtr (AddedVariable);\r
737 NameSize = NameSizeOfVariable (AddedVariable);\r
738 if (CompareGuid (&AddedVariable->VendorGuid, &Variable->VendorGuid) &&\r
739 NameSize == NameSizeOfVariable (Variable)\r
740 ) {\r
741 Point0 = (VOID *) GetVariableNamePtr (AddedVariable);\r
742 Point1 = (VOID *) GetVariableNamePtr (Variable);\r
5ead4a07 743 if (CompareMem (Point0, Point1, NameSizeOfVariable (AddedVariable)) == 0) {\r
814bae52 744 FoundAdded = TRUE;\r
745 break;\r
746 }\r
747 }\r
748 AddedVariable = NextAddedVariable;\r
749 }\r
750 if (!FoundAdded) {\r
5ead4a07 751 //\r
752 // Promote VAR_IN_DELETED_TRANSITION to VAR_ADDED\r
753 //\r
814bae52 754 VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
755 CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);\r
5ead4a07 756 ((VARIABLE_HEADER *) CurrPtr)->State = VAR_ADDED;\r
814bae52 757 CurrPtr += VariableSize;\r
758 }\r
759 }\r
760\r
761 Variable = NextVariable;\r
762 }\r
8d3a5c82 763\r
764 if (IsVolatile) {\r
765 //\r
766 // If volatile variable store, just copy valid buffer\r
767 //\r
768 SetMem ((UINT8 *) (UINTN) VariableBase, VariableStoreHeader->Size, 0xff);\r
814bae52 769 CopyMem ((UINT8 *) (UINTN) VariableBase, ValidBuffer, (UINTN) (CurrPtr - (UINT8 *) ValidBuffer));\r
8d3a5c82 770 Status = EFI_SUCCESS;\r
771 } else {\r
772 //\r
773 // If non-volatile variable store, perform FTW here.\r
774 //\r
775 Status = FtwVariableSpace (\r
776 VariableBase,\r
777 ValidBuffer,\r
814bae52 778 (UINTN) (CurrPtr - (UINT8 *) ValidBuffer)\r
8d3a5c82 779 );\r
8d3a5c82 780 }\r
814bae52 781 if (!EFI_ERROR (Status)) {\r
782 *LastVariableOffset = (UINTN) (CurrPtr - (UINT8 *) ValidBuffer);\r
783 } else {\r
8d3a5c82 784 *LastVariableOffset = 0;\r
785 }\r
786\r
814bae52 787 FreePool (ValidBuffer);\r
788\r
8d3a5c82 789 return Status;\r
790}\r
791\r
33a5a666 792\r
052ad7e1
A
793/**\r
794 Update the Cache with Variable information. These are the same \r
795 arguments as the EFI Variable services.\r
796\r
797 @param[in] VariableName Name of variable\r
798 @param[in] VendorGuid Guid of variable\r
799 @param[in] Attribute Attribue of the variable\r
800 @param[in] DataSize Size of data. 0 means delete\r
801 @param[in] Data Variable data\r
802\r
803**/\r
33a5a666
A
804VOID\r
805UpdateVariableCache (\r
806 IN CHAR16 *VariableName,\r
807 IN EFI_GUID *VendorGuid,\r
052ad7e1
A
808 IN UINT32 Attributes,\r
809 IN UINTN DataSize,\r
810 IN VOID *Data\r
33a5a666
A
811 )\r
812{\r
813 VARIABLE_CACHE_ENTRY *Entry;\r
814 UINTN Index;\r
815\r
816 if (EfiAtRuntime ()) {\r
817 // Don't use the cache at runtime\r
818 return;\r
819 }\r
820\r
821 for (Index = 0, Entry = mVariableCache; Index < sizeof (mVariableCache)/sizeof (VARIABLE_CACHE_ENTRY); Index++, Entry++) {\r
822 if (CompareGuid (VendorGuid, Entry->Guid)) {\r
823 if (StrCmp (VariableName, Entry->Name) == 0) { \r
824 Entry->Attributes = Attributes;\r
825 if (DataSize == 0) {\r
826 // Delete Case\r
827 if (Entry->DataSize != 0) {\r
828 FreePool (Entry->Data);\r
829 }\r
830 Entry->DataSize = DataSize;\r
831 } else if (DataSize == Entry->DataSize) {\r
832 CopyMem (Entry->Data, Data, DataSize);\r
833 } else {\r
834 Entry->Data = AllocatePool (DataSize);\r
835 Entry->DataSize = DataSize;\r
836 CopyMem (Entry->Data, Data, DataSize);\r
837 }\r
838 }\r
839 }\r
840 }\r
841}\r
842\r
843\r
052ad7e1
A
844/**\r
845 Search the cache to see if the variable is in the cache.\r
846\r
847 @param[in] VariableName Name of variable\r
848 @param[in] VendorGuid Guid of variable\r
849 @param[in] Attribute Attribue returned \r
850 @param[in] DataSize Size of data returned\r
851 @param[in] Data Variable data returned\r
852\r
853 @retval EFI_SUCCESS VariableGuid & VariableName data was returned.\r
854 @retval other Not found.\r
855\r
856**/\r
33a5a666
A
857EFI_STATUS\r
858FindVariableInCache (\r
859 IN CHAR16 *VariableName,\r
860 IN EFI_GUID *VendorGuid,\r
861 OUT UINT32 *Attributes OPTIONAL,\r
862 IN OUT UINTN *DataSize,\r
863 OUT VOID *Data\r
864 )\r
865{\r
866 VARIABLE_CACHE_ENTRY *Entry;\r
867 UINTN Index;\r
868\r
869 if (EfiAtRuntime ()) {\r
870 // Don't use the cache at runtime\r
871 return EFI_NOT_FOUND;\r
872 }\r
873\r
874 for (Index = 0, Entry = mVariableCache; Index < sizeof (mVariableCache)/sizeof (VARIABLE_CACHE_ENTRY); Index++, Entry++) {\r
875 if (CompareGuid (VendorGuid, Entry->Guid)) {\r
876 if (StrCmp (VariableName, Entry->Name) == 0) {\r
877 if (Entry->DataSize == 0) {\r
052ad7e1 878 // Variable was deleted so return not found\r
33a5a666 879 return EFI_NOT_FOUND;\r
aa09397b 880 } else if (Entry->DataSize > *DataSize) {\r
052ad7e1 881 // If the buffer is too small return correct size\r
33a5a666
A
882 *DataSize = Entry->DataSize;\r
883 return EFI_BUFFER_TOO_SMALL;\r
884 } else {\r
aa09397b 885 *DataSize = Entry->DataSize;\r
052ad7e1 886 // Return the data\r
33a5a666
A
887 CopyMem (Data, Entry->Data, Entry->DataSize);\r
888 if (Attributes != NULL) {\r
889 *Attributes = Entry->Attributes;\r
890 }\r
891 return EFI_SUCCESS;\r
892 }\r
893 }\r
894 }\r
895 }\r
896 \r
897 return EFI_NOT_FOUND;\r
898}\r
899\r
900\r
8d3a5c82 901EFI_STATUS\r
8d3a5c82 902FindVariable (\r
903 IN CHAR16 *VariableName,\r
904 IN EFI_GUID *VendorGuid,\r
905 OUT VARIABLE_POINTER_TRACK *PtrTrack,\r
906 IN VARIABLE_GLOBAL *Global\r
907 )\r
908/*++\r
909\r
910Routine Description:\r
911\r
912 This code finds variable in storage blocks (Volatile or Non-Volatile)\r
913\r
914Arguments:\r
915\r
916 VariableName Name of the variable to be found\r
917 VendorGuid Vendor GUID to be found.\r
918 PtrTrack Variable Track Pointer structure that contains\r
919 Variable Information.\r
920 Contains the pointer of Variable header.\r
921 Global VARIABLE_GLOBAL pointer\r
922\r
923Returns:\r
924\r
925 EFI STATUS\r
926\r
927--*/\r
928{\r
814bae52 929 VARIABLE_HEADER *Variable[2];\r
930 VARIABLE_HEADER *InDeletedVariable;\r
931 VARIABLE_STORE_HEADER *VariableStoreHeader[2];\r
932 UINTN InDeletedStorageIndex;\r
933 UINTN Index;\r
934 VOID *Point;\r
8d3a5c82 935\r
8d3a5c82 936 //\r
33a5a666 937 // 0: Volatile, 1: Non-Volatile\r
36873a61 938 // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName\r
939 // make use of this mapping to implement search algorithme.\r
8d3a5c82 940 //\r
052ad7e1
A
941 VariableStoreHeader[0] = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r
942 VariableStoreHeader[1] = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);\r
8d3a5c82 943\r
944 //\r
945 // Start Pointers for the variable.\r
946 // Actual Data Pointer where data can be written.\r
947 //\r
9cad030b 948 Variable[0] = GetStartPointer (VariableStoreHeader[0]);\r
949 Variable[1] = GetStartPointer (VariableStoreHeader[1]);\r
8d3a5c82 950\r
951 if (VariableName[0] != 0 && VendorGuid == NULL) {\r
952 return EFI_INVALID_PARAMETER;\r
953 }\r
814bae52 954\r
8d3a5c82 955 //\r
33a5a666 956 // Find the variable by walk through volatile and then non-volatile variable store\r
8d3a5c82 957 //\r
814bae52 958 InDeletedVariable = NULL;\r
959 InDeletedStorageIndex = 0;\r
8d3a5c82 960 for (Index = 0; Index < 2; Index++) {\r
8d3a5c82 961 while (IsValidVariableHeader (Variable[Index]) && (Variable[Index] <= GetEndPointer (VariableStoreHeader[Index]))) {\r
814bae52 962 if (Variable[Index]->State == VAR_ADDED || \r
963 Variable[Index]->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)\r
964 ) {\r
c6492839 965 if (!EfiAtRuntime () || (Variable[Index]->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {\r
8d3a5c82 966 if (VariableName[0] == 0) {\r
814bae52 967 if (Variable[Index]->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
968 InDeletedVariable = Variable[Index];\r
969 InDeletedStorageIndex = Index;\r
970 } else {\r
971 PtrTrack->StartPtr = GetStartPointer (VariableStoreHeader[Index]);\r
972 PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader[Index]);\r
973 PtrTrack->CurrPtr = Variable[Index];\r
974 PtrTrack->Volatile = (BOOLEAN)(Index == 0);\r
975\r
976 return EFI_SUCCESS;\r
977 }\r
8d3a5c82 978 } else {\r
979 if (CompareGuid (VendorGuid, &Variable[Index]->VendorGuid)) {\r
130e2569 980 Point = (VOID *) GetVariableNamePtr (Variable[Index]);\r
981\r
982 ASSERT (NameSizeOfVariable (Variable[Index]) != 0);\r
983 if (!CompareMem (VariableName, Point, NameSizeOfVariable (Variable[Index]))) {\r
814bae52 984 if (Variable[Index]->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
985 InDeletedVariable = Variable[Index];\r
986 InDeletedStorageIndex = Index;\r
987 } else {\r
988 PtrTrack->StartPtr = GetStartPointer (VariableStoreHeader[Index]);\r
989 PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader[Index]);\r
990 PtrTrack->CurrPtr = Variable[Index];\r
991 PtrTrack->Volatile = (BOOLEAN)(Index == 0);\r
992\r
993 return EFI_SUCCESS;\r
994 }\r
8d3a5c82 995 }\r
996 }\r
997 }\r
998 }\r
999 }\r
1000\r
1001 Variable[Index] = GetNextVariablePtr (Variable[Index]);\r
1002 }\r
814bae52 1003 if (InDeletedVariable != NULL) {\r
1004 PtrTrack->StartPtr = GetStartPointer (VariableStoreHeader[InDeletedStorageIndex]);\r
1005 PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader[InDeletedStorageIndex]);\r
1006 PtrTrack->CurrPtr = InDeletedVariable;\r
1007 PtrTrack->Volatile = (BOOLEAN)(InDeletedStorageIndex == 0);\r
1008 return EFI_SUCCESS;\r
1009 }\r
8d3a5c82 1010 }\r
8d3a5c82 1011 PtrTrack->CurrPtr = NULL;\r
1012 return EFI_NOT_FOUND;\r
1013}\r
1014\r
33a5a666 1015\r
052ad7e1 1016\r
8d3a5c82 1017/*++\r
1018\r
1019Routine Description:\r
1020\r
1021 This code finds variable in storage blocks (Volatile or Non-Volatile)\r
1022\r
1023Arguments:\r
1024\r
c6492839 1025 VariableName Name of Variable to be found\r
1026 VendorGuid Variable vendor GUID\r
1027 Attributes OPTIONAL Attribute value of the variable found\r
1028 DataSize Size of Data found. If size is less than the\r
1029 data, this value contains the required size.\r
1030 Data Data pointer\r
1031 Global Pointer to VARIABLE_GLOBAL structure\r
1032 Instance Instance of the Firmware Volume.\r
8d3a5c82 1033\r
1034Returns:\r
1035\r
c6492839 1036 EFI_INVALID_PARAMETER - Invalid parameter\r
1037 EFI_SUCCESS - Find the specified variable\r
1038 EFI_NOT_FOUND - Not found\r
1039 EFI_BUFFER_TO_SMALL - DataSize is too small for the result\r
1040\r
8d3a5c82 1041\r
1042--*/\r
052ad7e1
A
1043EFI_STATUS\r
1044EFIAPI\r
1045RuntimeServiceGetVariable (\r
1046 IN CHAR16 *VariableName,\r
1047 IN EFI_GUID *VendorGuid,\r
1048 OUT UINT32 *Attributes OPTIONAL,\r
1049 IN OUT UINTN *DataSize,\r
1050 OUT VOID *Data\r
1051 )\r
8d3a5c82 1052{\r
052ad7e1 1053 EFI_STATUS Status;\r
8d3a5c82 1054 VARIABLE_POINTER_TRACK Variable;\r
1055 UINTN VarDataSize;\r
8d3a5c82 1056\r
1057 if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {\r
1058 return EFI_INVALID_PARAMETER;\r
1059 }\r
33a5a666 1060\r
fdb7765f 1061 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
1062\r
8d3a5c82 1063 //\r
1064 // Find existing variable\r
1065 //\r
33a5a666
A
1066 Status = FindVariableInCache (VariableName, VendorGuid, Attributes, DataSize, Data);\r
1067 if ((Status == EFI_BUFFER_TOO_SMALL) || (Status == EFI_SUCCESS)){\r
1068 // Hit in the Cache\r
1069 UpdateVariableInfo (VariableName, VendorGuid, FALSE, TRUE, FALSE, FALSE, TRUE);\r
fdb7765f 1070 goto Done;\r
33a5a666
A
1071 }\r
1072 \r
052ad7e1 1073 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal);\r
8d3a5c82 1074 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
1075 goto Done;\r
1076 }\r
33a5a666 1077\r
8d3a5c82 1078 //\r
1079 // Get data size\r
1080 //\r
130e2569 1081 VarDataSize = DataSizeOfVariable (Variable.CurrPtr);\r
fdb7765f 1082 ASSERT (VarDataSize != 0);\r
1083\r
8d3a5c82 1084 if (*DataSize >= VarDataSize) {\r
1085 if (Data == NULL) {\r
1086 Status = EFI_INVALID_PARAMETER;\r
1087 goto Done;\r
1088 }\r
1089\r
1090 CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);\r
1091 if (Attributes != NULL) {\r
1092 *Attributes = Variable.CurrPtr->Attributes;\r
1093 }\r
1094\r
1095 *DataSize = VarDataSize;\r
33a5a666
A
1096 UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, TRUE, FALSE, FALSE, FALSE);\r
1097 UpdateVariableCache (VariableName, VendorGuid, Variable.CurrPtr->Attributes, VarDataSize, Data);\r
1098 \r
8d3a5c82 1099 Status = EFI_SUCCESS;\r
1100 goto Done;\r
1101 } else {\r
1102 *DataSize = VarDataSize;\r
1103 Status = EFI_BUFFER_TOO_SMALL;\r
1104 goto Done;\r
1105 }\r
1106\r
1107Done:\r
052ad7e1 1108 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
8d3a5c82 1109 return Status;\r
1110}\r
1111\r
052ad7e1
A
1112\r
1113\r
8d3a5c82 1114/*++\r
1115\r
1116Routine Description:\r
1117\r
1118 This code Finds the Next available variable\r
1119\r
1120Arguments:\r
1121\r
1122 VariableNameSize Size of the variable\r
1123 VariableName Pointer to variable name\r
1124 VendorGuid Variable Vendor Guid\r
1125 Global VARIABLE_GLOBAL structure pointer.\r
1126 Instance FV instance\r
1127\r
1128Returns:\r
1129\r
1130 EFI STATUS\r
1131\r
1132--*/\r
052ad7e1
A
1133EFI_STATUS\r
1134EFIAPI\r
1135RuntimeServiceGetNextVariableName (\r
1136 IN OUT UINTN *VariableNameSize,\r
1137 IN OUT CHAR16 *VariableName,\r
1138 IN OUT EFI_GUID *VendorGuid\r
1139 )\r
8d3a5c82 1140{\r
1141 VARIABLE_POINTER_TRACK Variable;\r
1142 UINTN VarNameSize;\r
1143 EFI_STATUS Status;\r
1144\r
1145 if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {\r
1146 return EFI_INVALID_PARAMETER;\r
1147 }\r
1148\r
fdb7765f 1149 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
1150\r
052ad7e1 1151 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal);\r
8d3a5c82 1152 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
1153 goto Done;\r
1154 }\r
1155\r
1156 if (VariableName[0] != 0) {\r
1157 //\r
1158 // If variable name is not NULL, get next variable\r
1159 //\r
1160 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
1161 }\r
1162\r
1163 while (TRUE) {\r
1164 //\r
1165 // If both volatile and non-volatile variable store are parsed,\r
1166 // return not found\r
1167 //\r
1168 if (Variable.CurrPtr >= Variable.EndPtr || Variable.CurrPtr == NULL) {\r
1169 Variable.Volatile = (BOOLEAN) (Variable.Volatile ^ ((BOOLEAN) 0x1));\r
36873a61 1170 if (!Variable.Volatile) {\r
9cad030b 1171 Variable.StartPtr = GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);\r
1172 Variable.EndPtr = GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase));\r
8d3a5c82 1173 } else {\r
1174 Status = EFI_NOT_FOUND;\r
1175 goto Done;\r
1176 }\r
1177\r
1178 Variable.CurrPtr = Variable.StartPtr;\r
1179 if (!IsValidVariableHeader (Variable.CurrPtr)) {\r
1180 continue;\r
1181 }\r
1182 }\r
1183 //\r
1184 // Variable is found\r
1185 //\r
1186 if (IsValidVariableHeader (Variable.CurrPtr) && Variable.CurrPtr->State == VAR_ADDED) {\r
1187 if (!(EfiAtRuntime () && !(Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS))) {\r
130e2569 1188 VarNameSize = NameSizeOfVariable (Variable.CurrPtr);\r
fdb7765f 1189 ASSERT (VarNameSize != 0);\r
1190\r
8d3a5c82 1191 if (VarNameSize <= *VariableNameSize) {\r
1192 CopyMem (\r
1193 VariableName,\r
130e2569 1194 GetVariableNamePtr (Variable.CurrPtr),\r
8d3a5c82 1195 VarNameSize\r
1196 );\r
1197 CopyMem (\r
1198 VendorGuid,\r
1199 &Variable.CurrPtr->VendorGuid,\r
1200 sizeof (EFI_GUID)\r
1201 );\r
1202 Status = EFI_SUCCESS;\r
1203 } else {\r
1204 Status = EFI_BUFFER_TOO_SMALL;\r
1205 }\r
1206\r
1207 *VariableNameSize = VarNameSize;\r
1208 goto Done;\r
1209 }\r
1210 }\r
1211\r
1212 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
1213 }\r
1214\r
1215Done:\r
052ad7e1 1216 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
8d3a5c82 1217 return Status;\r
1218}\r
1219\r
052ad7e1 1220\r
8d3a5c82 1221/*++\r
1222\r
1223Routine Description:\r
1224\r
1225 This code sets variable in storage blocks (Volatile or Non-Volatile)\r
1226\r
1227Arguments:\r
1228\r
1229 VariableName Name of Variable to be found\r
1230 VendorGuid Variable vendor GUID\r
1231 Attributes Attribute value of the variable found\r
1232 DataSize Size of Data found. If size is less than the\r
1233 data, this value contains the required size.\r
1234 Data Data pointer\r
1235 Global Pointer to VARIABLE_GLOBAL structure\r
1236 VolatileOffset The offset of last volatile variable\r
1237 NonVolatileOffset The offset of last non-volatile variable\r
1238 Instance Instance of the Firmware Volume.\r
1239\r
1240Returns:\r
1241\r
8d3a5c82 1242 EFI_INVALID_PARAMETER - Invalid parameter\r
1243 EFI_SUCCESS - Set successfully\r
1244 EFI_OUT_OF_RESOURCES - Resource not enough to set variable\r
1245 EFI_NOT_FOUND - Not found\r
c6492839 1246 EFI_DEVICE_ERROR - Variable can not be saved due to hardware failure\r
1247 EFI_WRITE_PROTECTED - Variable is read-only\r
8d3a5c82 1248\r
1249--*/\r
052ad7e1
A
1250EFI_STATUS\r
1251EFIAPI\r
1252RuntimeServiceSetVariable (\r
1253 IN CHAR16 *VariableName,\r
1254 IN EFI_GUID *VendorGuid,\r
1255 IN UINT32 Attributes,\r
1256 IN UINTN DataSize,\r
1257 IN VOID *Data\r
1258 )\r
8d3a5c82 1259{\r
1260 VARIABLE_POINTER_TRACK Variable;\r
1261 EFI_STATUS Status;\r
1262 VARIABLE_HEADER *NextVariable;\r
1263 UINTN VarNameSize;\r
1264 UINTN VarNameOffset;\r
1265 UINTN VarDataOffset;\r
1266 UINTN VarSize;\r
1267 UINT8 State;\r
1268 BOOLEAN Reclaimed;\r
052ad7e1
A
1269 UINTN *VolatileOffset;\r
1270 UINTN *NonVolatileOffset;\r
1271 UINT32 Instance;\r
fd51bf70 1272 BOOLEAN Volatile;\r
fdb7765f 1273 EFI_PHYSICAL_ADDRESS Point;\r
8d3a5c82 1274\r
c6492839 1275 //\r
1276 // Check input parameters\r
1277 //\r
8d3a5c82 1278 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r
1279 return EFI_INVALID_PARAMETER;\r
c6492839 1280 } \r
1281 //\r
1282 // Make sure if runtime bit is set, boot service bit is set also\r
1283 //\r
1284 if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
1285 return EFI_INVALID_PARAMETER;\r
8d3a5c82 1286 }\r
fdb7765f 1287\r
c6492839 1288 //\r
1289 // The size of the VariableName, including the Unicode Null in bytes plus\r
1290 // the DataSize is limited to maximum size of MAX_HARDWARE_ERROR_VARIABLE_SIZE (32K)\r
1291 // bytes for HwErrRec, and MAX_VARIABLE_SIZE (1024) bytes for the others.\r
1292 //\r
1293 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
1294 if ((DataSize > MAX_HARDWARE_ERROR_VARIABLE_SIZE) || \r
1295 (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > MAX_HARDWARE_ERROR_VARIABLE_SIZE)) {\r
1296 return EFI_INVALID_PARAMETER;\r
1297 } \r
1298 } else {\r
1299 //\r
1300 // The size of the VariableName, including the Unicode Null in bytes plus\r
1301 // the DataSize is limited to maximum size of MAX_VARIABLE_SIZE (1024) bytes.\r
1302 //\r
1303 if ((DataSize > MAX_VARIABLE_SIZE) ||\r
1304 (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > MAX_VARIABLE_SIZE)) {\r
1305 return EFI_INVALID_PARAMETER;\r
1306 } \r
1307 } \r
fdb7765f 1308\r
1309 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
1310\r
1311 Reclaimed = FALSE;\r
1312 Instance = mVariableModuleGlobal->FvbInstance;\r
1313 VolatileOffset = &mVariableModuleGlobal->VolatileLastVariableOffset;\r
1314\r
1315 //\r
1316 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated;\r
1317 //\r
1318 if (1 < InterlockedIncrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState)) {\r
fdb7765f 1319 Point = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;;\r
1320 //\r
1321 // Parse non-volatile variable data and get last variable offset\r
1322 //\r
9cad030b 1323 NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point);\r
fdb7765f 1324 while (IsValidVariableHeader (NextVariable)) {\r
1325 NextVariable = GetNextVariablePtr (NextVariable);\r
1326 }\r
1327 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) Point;\r
1328 }\r
1329\r
1330 NonVolatileOffset = &mVariableModuleGlobal->NonVolatileLastVariableOffset;\r
1331 \r
1332\r
c6492839 1333 //\r
1334 // Check whether the input variable is already existed\r
1335 //\r
1336 \r
052ad7e1 1337 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal);\r
c6492839 1338 if (Status == EFI_SUCCESS && Variable.CurrPtr != NULL) {\r
8d3a5c82 1339 //\r
c6492839 1340 // Update/Delete existing variable\r
8d3a5c82 1341 //\r
fd51bf70 1342 Volatile = Variable.Volatile;\r
c6492839 1343 \r
1344 if (EfiAtRuntime ()) { \r
1345 //\r
1346 // If EfiAtRuntime and the variable is Volatile and Runtime Access, \r
1347 // the volatile is ReadOnly, and SetVariable should be aborted and \r
1348 // return EFI_WRITE_PROTECTED.\r
1349 //\r
1350 if (Variable.Volatile) {\r
1351 Status = EFI_WRITE_PROTECTED;\r
1352 goto Done;\r
1353 }\r
1354 //\r
1355 // Only variable have NV attribute can be updated/deleted in Runtime\r
1356 //\r
1357 if (!(Variable.CurrPtr->Attributes & EFI_VARIABLE_NON_VOLATILE)) {\r
1358 Status = EFI_INVALID_PARAMETER;\r
1359 goto Done; \r
1360 }\r
1361 }\r
8d3a5c82 1362 //\r
c6492839 1363 // Setting a data variable with no access, or zero DataSize attributes\r
1364 // specified causes it to be deleted.\r
8d3a5c82 1365 //\r
c6492839 1366 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) { \r
1367 State = Variable.CurrPtr->State;\r
1368 State &= VAR_DELETED;\r
1369\r
1370 Status = UpdateVariableStore (\r
052ad7e1 1371 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 1372 Variable.Volatile,\r
1373 FALSE,\r
1374 Instance,\r
1375 (UINTN) &Variable.CurrPtr->State,\r
1376 sizeof (UINT8),\r
1377 &State\r
1378 ); \r
33a5a666 1379 if (!EFI_ERROR (Status)) {\r
fd51bf70 1380 UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, FALSE, TRUE, FALSE);\r
33a5a666
A
1381 UpdateVariableCache (VariableName, VendorGuid, Attributes, DataSize, Data);\r
1382 }\r
c6492839 1383 goto Done; \r
1384 }\r
8d3a5c82 1385 //\r
c6492839 1386 // If the variable is marked valid and the same data has been passed in\r
1387 // then return to the caller immediately.\r
8d3a5c82 1388 //\r
130e2569 1389 if (DataSizeOfVariable (Variable.CurrPtr) == DataSize &&\r
c6492839 1390 (CompareMem (Data, GetVariableDataPtr (Variable.CurrPtr), DataSize) == 0)) {\r
33a5a666 1391 \r
fd51bf70 1392 UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);\r
c6492839 1393 Status = EFI_SUCCESS;\r
1394 goto Done;\r
1395 } else if ((Variable.CurrPtr->State == VAR_ADDED) ||\r
1396 (Variable.CurrPtr->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {\r
814bae52 1397\r
c6492839 1398 //\r
1399 // Mark the old variable as in delete transition\r
1400 //\r
1401 State = Variable.CurrPtr->State;\r
1402 State &= VAR_IN_DELETED_TRANSITION;\r
1403\r
1404 Status = UpdateVariableStore (\r
052ad7e1 1405 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 1406 Variable.Volatile,\r
1407 FALSE,\r
1408 Instance,\r
1409 (UINTN) &Variable.CurrPtr->State,\r
1410 sizeof (UINT8),\r
1411 &State\r
1412 ); \r
1413 if (EFI_ERROR (Status)) {\r
1414 goto Done; \r
33a5a666 1415 } \r
c6492839 1416 } \r
1417 } else if (Status == EFI_NOT_FOUND) {\r
8d3a5c82 1418 //\r
c6492839 1419 // Create a new variable\r
1420 // \r
1421 \r
8d3a5c82 1422 //\r
c6492839 1423 // Make sure we are trying to create a new variable.\r
1424 // Setting a data variable with no access, or zero DataSize attributes means to delete it. \r
8d3a5c82 1425 //\r
c6492839 1426 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {\r
1427 Status = EFI_NOT_FOUND;\r
1428 goto Done;\r
1429 }\r
1430 \r
8d3a5c82 1431 //\r
c6492839 1432 // Only variable have NV|RT attribute can be created in Runtime\r
1433 //\r
1434 if (EfiAtRuntime () &&\r
1435 (!(Attributes & EFI_VARIABLE_RUNTIME_ACCESS) || !(Attributes & EFI_VARIABLE_NON_VOLATILE))) {\r
1436 Status = EFI_INVALID_PARAMETER;\r
1437 goto Done;\r
1438 } \r
1439 } else {\r
8d3a5c82 1440 //\r
c6492839 1441 // Status should be EFI_INVALID_PARAMETER here according to return status of FindVariable().\r
8d3a5c82 1442 //\r
c6492839 1443 ASSERT (Status == EFI_INVALID_PARAMETER);\r
8d3a5c82 1444 goto Done;\r
c6492839 1445 }\r
1446\r
1447 //\r
1448 // Function part - create a new variable and copy the data.\r
1449 // Both update a variable and create a variable will come here.\r
1450 //\r
1451 // Tricky part: Use scratch data area at the end of volatile variable store\r
1452 // as a temporary storage.\r
1453 //\r
052ad7e1 1454 NextVariable = GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase));\r
c6492839 1455\r
1456 SetMem (NextVariable, SCRATCH_SIZE, 0xff);\r
1457\r
1458 NextVariable->StartId = VARIABLE_DATA;\r
1459 NextVariable->Attributes = Attributes;\r
1460 //\r
1461 // NextVariable->State = VAR_ADDED;\r
1462 //\r
1463 NextVariable->Reserved = 0;\r
1464 VarNameOffset = sizeof (VARIABLE_HEADER);\r
1465 VarNameSize = StrSize (VariableName);\r
1466 CopyMem (\r
1467 (UINT8 *) ((UINTN) NextVariable + VarNameOffset),\r
1468 VariableName,\r
1469 VarNameSize\r
1470 );\r
1471 VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);\r
1472 CopyMem (\r
1473 (UINT8 *) ((UINTN) NextVariable + VarDataOffset),\r
1474 Data,\r
1475 DataSize\r
1476 );\r
1477 CopyMem (&NextVariable->VendorGuid, VendorGuid, sizeof (EFI_GUID));\r
1478 //\r
1479 // There will be pad bytes after Data, the NextVariable->NameSize and\r
1480 // NextVariable->DataSize should not include pad size so that variable\r
1481 // service can get actual size in GetVariable\r
1482 //\r
1483 NextVariable->NameSize = (UINT32)VarNameSize;\r
1484 NextVariable->DataSize = (UINT32)DataSize;\r
1485\r
1486 //\r
1487 // The actual size of the variable that stores in storage should\r
1488 // include pad size.\r
1489 //\r
1490 VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);\r
1491 if (Attributes & EFI_VARIABLE_NON_VOLATILE) {\r
8d3a5c82 1492 //\r
c6492839 1493 // Create a nonvolatile variable\r
8d3a5c82 1494 //\r
fd51bf70 1495 Volatile = FALSE;\r
c6492839 1496 \r
1497 if ((UINT32) (VarSize +*NonVolatileOffset) >\r
052ad7e1 1498 ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase)))->Size\r
c6492839 1499 ) {\r
1500 if (EfiAtRuntime ()) {\r
1501 Status = EFI_OUT_OF_RESOURCES;\r
1502 goto Done;\r
1503 }\r
1504 //\r
1505 // Perform garbage collection & reclaim operation\r
1506 //\r
814bae52 1507 Status = Reclaim (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase, NonVolatileOffset, FALSE, Variable.CurrPtr);\r
8d3a5c82 1508 if (EFI_ERROR (Status)) {\r
1509 goto Done;\r
1510 }\r
8d3a5c82 1511 //\r
c6492839 1512 // If still no enough space, return out of resources\r
8d3a5c82 1513 //\r
c6492839 1514 if ((UINT32) (VarSize +*NonVolatileOffset) >\r
052ad7e1 1515 ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase)))->Size\r
8d3a5c82 1516 ) {\r
c6492839 1517 Status = EFI_OUT_OF_RESOURCES;\r
8d3a5c82 1518 goto Done;\r
8d3a5c82 1519 }\r
c6492839 1520 \r
1521 Reclaimed = TRUE;\r
8d3a5c82 1522 }\r
1523 //\r
c6492839 1524 // Three steps\r
1525 // 1. Write variable header\r
130e2569 1526 // 2. Set variable state to header valid \r
1527 // 3. Write variable data\r
1528 // 4. Set variable state to valid\r
8d3a5c82 1529 //\r
8d3a5c82 1530 //\r
c6492839 1531 // Step 1:\r
8d3a5c82 1532 //\r
c6492839 1533 Status = UpdateVariableStore (\r
052ad7e1 1534 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 1535 FALSE,\r
1536 TRUE,\r
1537 Instance,\r
1538 *NonVolatileOffset,\r
1539 sizeof (VARIABLE_HEADER),\r
1540 (UINT8 *) NextVariable\r
1541 );\r
1542\r
1543 if (EFI_ERROR (Status)) {\r
1544 goto Done;\r
1545 }\r
130e2569 1546\r
8d3a5c82 1547 //\r
c6492839 1548 // Step 2:\r
8d3a5c82 1549 //\r
130e2569 1550 NextVariable->State = VAR_HEADER_VALID_ONLY;\r
1551 Status = UpdateVariableStore (\r
1552 &mVariableModuleGlobal->VariableGlobal,\r
1553 FALSE,\r
1554 TRUE,\r
1555 Instance,\r
1556 *NonVolatileOffset,\r
1557 sizeof (VARIABLE_HEADER),\r
1558 (UINT8 *) NextVariable\r
1559 );\r
1560\r
1561 if (EFI_ERROR (Status)) {\r
1562 goto Done;\r
1563 }\r
1564 //\r
1565 // Step 3:\r
1566 //\r
c6492839 1567 Status = UpdateVariableStore (\r
052ad7e1 1568 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 1569 FALSE,\r
1570 TRUE,\r
1571 Instance,\r
1572 *NonVolatileOffset + sizeof (VARIABLE_HEADER),\r
1573 (UINT32) VarSize - sizeof (VARIABLE_HEADER),\r
1574 (UINT8 *) NextVariable + sizeof (VARIABLE_HEADER)\r
1575 );\r
1576\r
1577 if (EFI_ERROR (Status)) {\r
1578 goto Done;\r
1579 }\r
8d3a5c82 1580 //\r
130e2569 1581 // Step 4:\r
8d3a5c82 1582 //\r
c6492839 1583 NextVariable->State = VAR_ADDED;\r
1584 Status = UpdateVariableStore (\r
052ad7e1 1585 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 1586 FALSE,\r
1587 TRUE,\r
1588 Instance,\r
1589 *NonVolatileOffset,\r
1590 sizeof (VARIABLE_HEADER),\r
1591 (UINT8 *) NextVariable\r
1592 );\r
1593\r
1594 if (EFI_ERROR (Status)) {\r
1595 goto Done;\r
1596 }\r
8d3a5c82 1597\r
9cad030b 1598 *NonVolatileOffset = HEADER_ALIGN (*NonVolatileOffset + VarSize);\r
8d3a5c82 1599\r
c6492839 1600 } else {\r
1601 //\r
1602 // Create a volatile variable\r
1603 // \r
fd51bf70 1604 Volatile = TRUE;\r
c6492839 1605\r
1606 if ((UINT32) (VarSize +*VolatileOffset) >\r
052ad7e1 1607 ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size) {\r
8d3a5c82 1608 //\r
c6492839 1609 // Perform garbage collection & reclaim operation\r
8d3a5c82 1610 //\r
814bae52 1611 Status = Reclaim (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase, VolatileOffset, TRUE, Variable.CurrPtr);\r
8d3a5c82 1612 if (EFI_ERROR (Status)) {\r
1613 goto Done;\r
1614 }\r
1615 //\r
c6492839 1616 // If still no enough space, return out of resources\r
8d3a5c82 1617 //\r
8d3a5c82 1618 if ((UINT32) (VarSize +*VolatileOffset) >\r
052ad7e1 1619 ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size\r
8d3a5c82 1620 ) {\r
c6492839 1621 Status = EFI_OUT_OF_RESOURCES;\r
8d3a5c82 1622 goto Done;\r
1623 }\r
c6492839 1624 \r
1625 Reclaimed = TRUE;\r
8d3a5c82 1626 }\r
8d3a5c82 1627\r
c6492839 1628 NextVariable->State = VAR_ADDED;\r
1629 Status = UpdateVariableStore (\r
052ad7e1 1630 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 1631 TRUE,\r
1632 TRUE,\r
1633 Instance,\r
1634 *VolatileOffset,\r
1635 (UINT32) VarSize,\r
1636 (UINT8 *) NextVariable\r
1637 );\r
1638\r
1639 if (EFI_ERROR (Status)) {\r
1640 goto Done;\r
8d3a5c82 1641 }\r
c6492839 1642\r
9cad030b 1643 *VolatileOffset = HEADER_ALIGN (*VolatileOffset + VarSize);\r
c6492839 1644 }\r
1645 //\r
1646 // Mark the old variable as deleted\r
1647 //\r
1648 if (!Reclaimed && !EFI_ERROR (Status) && Variable.CurrPtr != NULL) {\r
1649 State = Variable.CurrPtr->State;\r
1650 State &= VAR_DELETED;\r
1651\r
1652 Status = UpdateVariableStore (\r
052ad7e1 1653 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 1654 Variable.Volatile,\r
1655 FALSE,\r
1656 Instance,\r
1657 (UINTN) &Variable.CurrPtr->State,\r
1658 sizeof (UINT8),\r
1659 &State\r
1660 );\r
33a5a666
A
1661 \r
1662 if (!EFI_ERROR (Status)) {\r
fd51bf70 1663 UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);\r
33a5a666
A
1664 UpdateVariableCache (VariableName, VendorGuid, Attributes, DataSize, Data);\r
1665 }\r
c6492839 1666 goto Done; \r
8d3a5c82 1667 }\r
1668\r
1669 Status = EFI_SUCCESS;\r
fd51bf70 1670 UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);\r
33a5a666
A
1671 UpdateVariableCache (VariableName, VendorGuid, Attributes, DataSize, Data);\r
1672\r
8d3a5c82 1673Done:\r
fdb7765f 1674 InterlockedDecrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState);\r
052ad7e1 1675 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
fdb7765f 1676\r
8d3a5c82 1677 return Status;\r
1678}\r
1679\r
052ad7e1 1680\r
8d3a5c82 1681/*++\r
1682\r
1683Routine Description:\r
1684\r
1685 This code returns information about the EFI variables.\r
1686\r
1687Arguments:\r
1688\r
1689 Attributes Attributes bitmask to specify the type of variables\r
1690 on which to return information.\r
1691 MaximumVariableStorageSize Pointer to the maximum size of the storage space available\r
1692 for the EFI variables associated with the attributes specified.\r
1693 RemainingVariableStorageSize Pointer to the remaining size of the storage space available\r
c6492839 1694 for EFI variables associated with the attributes specified.\r
1695 MaximumVariableSize Pointer to the maximum size of an individual EFI variables\r
8d3a5c82 1696 associated with the attributes specified.\r
1697 Global Pointer to VARIABLE_GLOBAL structure.\r
1698 Instance Instance of the Firmware Volume.\r
1699\r
1700Returns:\r
1701\r
1702 EFI STATUS\r
1703 EFI_INVALID_PARAMETER - An invalid combination of attribute bits was supplied.\r
1704 EFI_SUCCESS - Query successfully.\r
1705 EFI_UNSUPPORTED - The attribute is not supported on this platform.\r
1706\r
1707--*/\r
052ad7e1
A
1708EFI_STATUS\r
1709EFIAPI\r
1710RuntimeServiceQueryVariableInfo (\r
1711 IN UINT32 Attributes,\r
1712 OUT UINT64 *MaximumVariableStorageSize,\r
1713 OUT UINT64 *RemainingVariableStorageSize,\r
1714 OUT UINT64 *MaximumVariableSize\r
1715 )\r
8d3a5c82 1716{\r
1717 VARIABLE_HEADER *Variable;\r
1718 VARIABLE_HEADER *NextVariable;\r
1719 UINT64 VariableSize;\r
1720 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
1721\r
c6492839 1722 if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {\r
8d3a5c82 1723 return EFI_INVALID_PARAMETER;\r
1724 }\r
c6492839 1725 \r
1726 if((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == 0) {\r
8d3a5c82 1727 //\r
1728 // Make sure the Attributes combination is supported by the platform.\r
1729 //\r
c6492839 1730 return EFI_UNSUPPORTED; \r
8d3a5c82 1731 } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
1732 //\r
1733 // Make sure if runtime bit is set, boot service bit is set also.\r
1734 //\r
1735 return EFI_INVALID_PARAMETER;\r
1736 } else if (EfiAtRuntime () && !(Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {\r
1737 //\r
1738 // Make sure RT Attribute is set if we are in Runtime phase.\r
1739 //\r
1740 return EFI_INVALID_PARAMETER;\r
1741 }\r
1742\r
052ad7e1 1743 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
8d3a5c82 1744\r
1745 if((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r
1746 //\r
1747 // Query is Volatile related.\r
1748 //\r
052ad7e1 1749 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r
8d3a5c82 1750 } else {\r
1751 //\r
1752 // Query is Non-Volatile related.\r
1753 //\r
052ad7e1 1754 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);\r
8d3a5c82 1755 }\r
1756\r
1757 //\r
1758 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize\r
1759 // with the storage size (excluding the storage header size).\r
1760 //\r
1761 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);\r
1762 *RemainingVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);\r
1763\r
1764 //\r
c6492839 1765 // Let *MaximumVariableSize be MAX_VARIABLE_SIZE with the exception of the variable header size.\r
8d3a5c82 1766 //\r
c6492839 1767 *MaximumVariableSize = MAX_VARIABLE_SIZE - sizeof (VARIABLE_HEADER);\r
1768\r
1769 //\r
1770 // Harware error record variable needs larger size.\r
1771 //\r
1772 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
1773 *MaximumVariableSize = MAX_HARDWARE_ERROR_VARIABLE_SIZE - sizeof (VARIABLE_HEADER);\r
1774 }\r
8d3a5c82 1775\r
1776 //\r
1777 // Point to the starting address of the variables.\r
1778 //\r
9cad030b 1779 Variable = GetStartPointer (VariableStoreHeader);\r
8d3a5c82 1780\r
1781 //\r
1782 // Now walk through the related variable store.\r
1783 //\r
1784 while (IsValidVariableHeader (Variable) && (Variable < GetEndPointer (VariableStoreHeader))) {\r
1785 NextVariable = GetNextVariablePtr (Variable);\r
1786 VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;\r
1787\r
1788 if (EfiAtRuntime ()) {\r
1789 //\r
1790 // we don't take the state of the variables in mind\r
1791 // when calculating RemainingVariableStorageSize,\r
1792 // since the space occupied by variables not marked with\r
1793 // VAR_ADDED is not allowed to be reclaimed in Runtime.\r
1794 //\r
1795 *RemainingVariableStorageSize -= VariableSize;\r
1796 } else {\r
1797 //\r
1798 // Only care about Variables with State VAR_ADDED,because\r
1799 // the space not marked as VAR_ADDED is reclaimable now.\r
1800 //\r
1801 if (Variable->State == VAR_ADDED) {\r
1802 *RemainingVariableStorageSize -= VariableSize;\r
1803 }\r
1804 }\r
1805\r
1806 //\r
1807 // Go to the next one\r
1808 //\r
1809 Variable = NextVariable;\r
1810 }\r
1811\r
c6492839 1812 if (*RemainingVariableStorageSize < sizeof (VARIABLE_HEADER)) {\r
1813 *MaximumVariableSize = 0;\r
1814 } else if ((*RemainingVariableStorageSize - sizeof (VARIABLE_HEADER)) < *MaximumVariableSize) {\r
1815 *MaximumVariableSize = *RemainingVariableStorageSize - sizeof (VARIABLE_HEADER);\r
1816 }\r
1817\r
052ad7e1 1818 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
8d3a5c82 1819 return EFI_SUCCESS;\r
1820}\r
1821\r
7800593d
LG
1822VOID\r
1823EFIAPI\r
1824ReclaimForOS(\r
1825 EFI_EVENT Event,\r
1826 VOID *Context\r
1827 )\r
1828{\r
1829 UINT32 VarSize;\r
1830 EFI_STATUS Status;\r
1831\r
1832 VarSize = ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase))->Size;\r
1833 Status = EFI_SUCCESS; \r
1834\r
1835 //\r
1836 // Check if the free area is blow a threshold\r
1837 //\r
1838 if ((VarSize - mVariableModuleGlobal->NonVolatileLastVariableOffset) < VARIABLE_RECLAIM_THRESHOLD) {\r
1839 Status = Reclaim (\r
1840 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
1841 &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
814bae52 1842 FALSE,\r
1843 NULL\r
7800593d
LG
1844 );\r
1845 ASSERT_EFI_ERROR (Status);\r
1846 }\r
1847}\r
1848\r
8d3a5c82 1849EFI_STATUS\r
8d3a5c82 1850VariableCommonInitialize (\r
1851 IN EFI_HANDLE ImageHandle,\r
1852 IN EFI_SYSTEM_TABLE *SystemTable\r
1853 )\r
1854/*++\r
1855\r
1856Routine Description:\r
1857 This function does common initialization for variable services\r
1858\r
1859Arguments:\r
1860\r
1861 ImageHandle - The firmware allocated handle for the EFI image.\r
1862 SystemTable - A pointer to the EFI System Table.\r
1863\r
1864Returns:\r
1865\r
1866 Status code.\r
1867\r
1868 EFI_NOT_FOUND - Variable store area not found.\r
1869 EFI_UNSUPPORTED - Currently only one non-volatile variable store is supported.\r
1870 EFI_SUCCESS - Variable services successfully initialized.\r
1871\r
1872--*/\r
1873{\r
1874 EFI_STATUS Status;\r
1875 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r
1876 CHAR8 *CurrPtr;\r
1877 VARIABLE_STORE_HEADER *VolatileVariableStore;\r
1878 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
1879 VARIABLE_HEADER *NextVariable;\r
1880 UINT32 Instance;\r
1881 EFI_PHYSICAL_ADDRESS FvVolHdr;\r
8d3a5c82 1882 UINT64 TempVariableStoreHeader;\r
8d3a5c82 1883 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;\r
8d3a5c82 1884 UINT64 BaseAddress;\r
1885 UINT64 Length;\r
1886 UINTN Index;\r
1887 UINT8 Data;\r
052ad7e1
A
1888 UINT64 VariableStoreBase;\r
1889 UINT64 VariableStoreLength;\r
7800593d 1890 EFI_EVENT ReadyToBootEvent;\r
8d3a5c82 1891\r
7800593d
LG
1892 Status = EFI_SUCCESS;\r
1893 //\r
1894 // Allocate runtime memory for variable driver global structure.\r
1895 //\r
1896 mVariableModuleGlobal = AllocateRuntimePool (sizeof (VARIABLE_MODULE_GLOBAL));\r
1897 if (mVariableModuleGlobal == NULL) {\r
1898 return EFI_OUT_OF_RESOURCES;\r
1899 }\r
8d3a5c82 1900\r
052ad7e1 1901 EfiInitializeLock(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock, TPL_NOTIFY);\r
fdb7765f 1902 mVariableModuleGlobal->VariableGlobal.ReentrantState = 0;\r
8d3a5c82 1903\r
1904 //\r
1905 // Allocate memory for volatile variable store\r
1906 //\r
1907 VolatileVariableStore = AllocateRuntimePool (VARIABLE_STORE_SIZE + SCRATCH_SIZE);\r
1908 if (VolatileVariableStore == NULL) {\r
1909 FreePool (mVariableModuleGlobal);\r
1910 return EFI_OUT_OF_RESOURCES;\r
1911 }\r
1912\r
1913 SetMem (VolatileVariableStore, VARIABLE_STORE_SIZE + SCRATCH_SIZE, 0xff);\r
1914\r
1915 //\r
1916 // Variable Specific Data\r
1917 //\r
052ad7e1 1918 mVariableModuleGlobal->VariableGlobal.VolatileVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VolatileVariableStore;\r
9cad030b 1919 mVariableModuleGlobal->VolatileLastVariableOffset = (UINTN) GetStartPointer (VolatileVariableStore) - (UINTN) VolatileVariableStore;\r
8d3a5c82 1920\r
1921 VolatileVariableStore->Signature = VARIABLE_STORE_SIGNATURE;\r
1922 VolatileVariableStore->Size = VARIABLE_STORE_SIZE;\r
1923 VolatileVariableStore->Format = VARIABLE_STORE_FORMATTED;\r
1924 VolatileVariableStore->State = VARIABLE_STORE_HEALTHY;\r
1925 VolatileVariableStore->Reserved = 0;\r
1926 VolatileVariableStore->Reserved1 = 0;\r
1927\r
1928 //\r
1929 // Get non volatile varaible store\r
1930 //\r
1931\r
1932 TempVariableStoreHeader = (UINT64) PcdGet32 (PcdFlashNvStorageVariableBase);\r
052ad7e1 1933 VariableStoreBase = TempVariableStoreHeader + \\r
8d3a5c82 1934 (((EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) (TempVariableStoreHeader)) -> HeaderLength);\r
052ad7e1 1935 VariableStoreLength = (UINT64) PcdGet32 (PcdFlashNvStorageVariableSize) - \\r
8d3a5c82 1936 (((EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) (TempVariableStoreHeader)) -> HeaderLength);\r
1937 //\r
1938 // Mark the variable storage region of the FLASH as RUNTIME\r
1939 //\r
052ad7e1
A
1940 BaseAddress = VariableStoreBase & (~EFI_PAGE_MASK);\r
1941 Length = VariableStoreLength + (VariableStoreBase - BaseAddress);\r
8d3a5c82 1942 Length = (Length + EFI_PAGE_SIZE - 1) & (~EFI_PAGE_MASK);\r
1943\r
1944 Status = gDS->GetMemorySpaceDescriptor (BaseAddress, &GcdDescriptor);\r
1945 if (EFI_ERROR (Status)) {\r
7800593d 1946 goto Done;\r
8d3a5c82 1947 }\r
1948\r
1949 Status = gDS->SetMemorySpaceAttributes (\r
1950 BaseAddress,\r
1951 Length,\r
1952 GcdDescriptor.Attributes | EFI_MEMORY_RUNTIME\r
1953 );\r
1954 if (EFI_ERROR (Status)) {\r
7800593d 1955 goto Done;\r
8d3a5c82 1956 }\r
1957 //\r
1958 // Get address of non volatile variable store base\r
1959 //\r
052ad7e1 1960 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase;\r
8d3a5c82 1961\r
1962 //\r
1963 // Check Integrity\r
1964 //\r
1965 //\r
1966 // Find the Correct Instance of the FV Block Service.\r
1967 //\r
1968 Instance = 0;\r
052ad7e1 1969 CurrPtr = (CHAR8 *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);\r
8d3a5c82 1970 while (EfiFvbGetPhysicalAddress (Instance, &FvVolHdr) == EFI_SUCCESS) {\r
1971 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvVolHdr);\r
1972 if (CurrPtr >= (CHAR8 *) FwVolHeader && CurrPtr < (((CHAR8 *) FwVolHeader) + FwVolHeader->FvLength)) {\r
1973 mVariableModuleGlobal->FvbInstance = Instance;\r
1974 break;\r
1975 }\r
1976\r
1977 Instance++;\r
1978 }\r
1979\r
1980 VariableStoreHeader = (VARIABLE_STORE_HEADER *) CurrPtr;\r
1981 if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) {\r
1982 if (~VariableStoreHeader->Size == 0) {\r
1983 Status = UpdateVariableStore (\r
052ad7e1 1984 &mVariableModuleGlobal->VariableGlobal,\r
8d3a5c82 1985 FALSE,\r
1986 FALSE,\r
1987 mVariableModuleGlobal->FvbInstance,\r
1988 (UINTN) &VariableStoreHeader->Size,\r
1989 sizeof (UINT32),\r
052ad7e1 1990 (UINT8 *) &VariableStoreLength\r
8d3a5c82 1991 );\r
1992 //\r
1993 // As Variables are stored in NV storage, which are slow devices,such as flash.\r
1994 // Variable operation may skip checking variable program result to improve performance,\r
1995 // We can assume Variable program is OK through some check point.\r
1996 // Variable Store Size Setting should be the first Variable write operation,\r
1997 // We can assume all Read/Write is OK if we can set Variable store size successfully.\r
1998 // If write fail, we will assert here\r
1999 //\r
052ad7e1 2000 ASSERT(VariableStoreHeader->Size == VariableStoreLength);\r
8d3a5c82 2001\r
2002 if (EFI_ERROR (Status)) {\r
7800593d 2003 goto Done;\r
8d3a5c82 2004 }\r
2005 }\r
2006\r
052ad7e1 2007 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = (EFI_PHYSICAL_ADDRESS) ((UINTN) CurrPtr);\r
8d3a5c82 2008 //\r
2009 // Parse non-volatile variable data and get last variable offset\r
2010 //\r
9cad030b 2011 NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) CurrPtr);\r
8d3a5c82 2012 Status = EFI_SUCCESS;\r
2013\r
2014 while (IsValidVariableHeader (NextVariable)) {\r
2015 NextVariable = GetNextVariablePtr (NextVariable);\r
2016 }\r
2017\r
2018 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) CurrPtr;\r
2019\r
8d3a5c82 2020 //\r
2021 // Check if the free area is really free.\r
2022 //\r
2023 for (Index = mVariableModuleGlobal->NonVolatileLastVariableOffset; Index < VariableStoreHeader->Size; Index++) {\r
052ad7e1 2024 Data = ((UINT8 *) (UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase)[Index];\r
8d3a5c82 2025 if (Data != 0xff) {\r
2026 //\r
2027 // There must be something wrong in variable store, do reclaim operation.\r
2028 //\r
2029 Status = Reclaim (\r
052ad7e1 2030 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
8d3a5c82 2031 &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
814bae52 2032 FALSE,\r
2033 NULL\r
8d3a5c82 2034 );\r
7800593d
LG
2035\r
2036 if (EFI_ERROR (Status)) {\r
2037 goto Done;\r
2038 }\r
2039\r
8d3a5c82 2040 break;\r
2041 }\r
2042 }\r
7800593d
LG
2043\r
2044 //\r
2045 // Register the event handling function to reclaim variable for OS usage.\r
2046 //\r
2047 Status = EfiCreateEventReadyToBootEx (\r
2048 TPL_NOTIFY, \r
2049 ReclaimForOS, \r
2050 NULL, \r
2051 &ReadyToBootEvent\r
2052 );\r
8d3a5c82 2053 }\r
2054\r
7800593d 2055Done:\r
8d3a5c82 2056 if (EFI_ERROR (Status)) {\r
2057 FreePool (mVariableModuleGlobal);\r
2058 FreePool (VolatileVariableStore);\r
2059 }\r
2060\r
2061 return Status;\r
2062}\r
052ad7e1 2063\r
052ad7e1
A
2064VOID\r
2065EFIAPI\r
2066VariableClassAddressChangeEvent (\r
2067 IN EFI_EVENT Event,\r
2068 IN VOID *Context\r
2069 )\r
2070{\r
2071 EfiConvertPointer (\r
2072 0x0,\r
2073 (VOID **) &mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase\r
2074 );\r
2075 EfiConvertPointer (\r
2076 0x0,\r
2077 (VOID **) &mVariableModuleGlobal->VariableGlobal.VolatileVariableBase\r
2078 );\r
2079 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal);\r
2080}\r
2081\r
2082\r
2083/**\r
2084 Variable Driver main entry point. The Variable driver places the 4 EFI\r
2085 runtime services in the EFI System Table and installs arch protocols \r
2086 for variable read and write services being availible. \r
2087\r
2088 @param[in] ImageHandle The firmware allocated handle for the EFI image. \r
2089 @param[in] SystemTable A pointer to the EFI System Table.\r
2090 \r
2091 @retval EFI_SUCCESS The entry point is executed successfully.\r
2092 @retval other Some error occurs when executing this entry point.\r
2093\r
2094**/\r
2095EFI_STATUS\r
2096EFIAPI\r
2097VariableServiceInitialize (\r
2098 IN EFI_HANDLE ImageHandle,\r
2099 IN EFI_SYSTEM_TABLE *SystemTable\r
2100 )\r
2101{\r
2102 EFI_STATUS Status;\r
2103\r
2104 Status = VariableCommonInitialize (ImageHandle, SystemTable);\r
2105 ASSERT_EFI_ERROR (Status);\r
2106\r
2107 SystemTable->RuntimeServices->GetVariable = RuntimeServiceGetVariable;\r
2108 SystemTable->RuntimeServices->GetNextVariableName = RuntimeServiceGetNextVariableName;\r
2109 SystemTable->RuntimeServices->SetVariable = RuntimeServiceSetVariable;\r
2110 SystemTable->RuntimeServices->QueryVariableInfo = RuntimeServiceQueryVariableInfo;\r
2111\r
2112 //\r
2113 // Now install the Variable Runtime Architectural Protocol on a new handle\r
2114 //\r
2115 Status = gBS->InstallMultipleProtocolInterfaces (\r
2116 &mHandle,\r
2117 &gEfiVariableArchProtocolGuid, NULL,\r
2118 &gEfiVariableWriteArchProtocolGuid, NULL,\r
2119 NULL\r
2120 );\r
2121 ASSERT_EFI_ERROR (Status);\r
2122\r
2123 Status = gBS->CreateEvent (\r
2124 EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE,\r
2125 TPL_NOTIFY,\r
2126 VariableClassAddressChangeEvent,\r
2127 NULL,\r
2128 &mVirtualAddressChangeEvent\r
2129 );\r
2130 ASSERT_EFI_ERROR (Status);\r
2131\r
2132 return EFI_SUCCESS;\r
2133}\r
2134\r