Update the local header file name.
[mirror_edk2.git] / MdeModulePkg / Universal / Variable / RuntimeDxe / Variable.c
CommitLineData
8d3a5c82 1/*++\r
2\r
3Copyright (c) 2006 - 2007, Intel Corporation\r
4All rights reserved. This program and the accompanying materials\r
5are licensed and made available under the terms and conditions of the BSD License\r
6which accompanies this distribution. The full text of the license may be found at\r
7http://opensource.org/licenses/bsd-license.php\r
8\r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11\r
12Module Name:\r
13\r
14 Variable.c\r
15\r
16Abstract:\r
17\r
18Revision History\r
19\r
20--*/\r
21\r
22\r
23#include "Variable.h"\r
3cfb790c 24#include <Guid/FlashMapHob.h>\r
8d3a5c82 25\r
26//\r
27// Don't use module globals after the SetVirtualAddress map is signaled\r
28//\r
29ESAL_VARIABLE_GLOBAL *mVariableModuleGlobal;\r
30\r
31//\r
32// This is a temperary function which will be removed\r
33// when EfiAcquireLock in UefiLib can handle the\r
34// the call in UEFI Runtimer driver in RT phase.\r
35//\r
36STATIC\r
37VOID\r
38AcquireLockOnlyAtBootTime (\r
39 IN EFI_LOCK *Lock\r
40 )\r
41{\r
42 if (!EfiAtRuntime ()) {\r
43 EfiAcquireLock (Lock);\r
44 }\r
45}\r
46\r
47//\r
48// This is a temperary function which will be removed\r
49// when EfiAcquireLock in UefiLib can handle the\r
50// the call in UEFI Runtimer driver in RT phase.\r
51//\r
52STATIC\r
53VOID\r
54ReleaseLockOnlyAtBootTime (\r
55 IN EFI_LOCK *Lock\r
56 )\r
57{\r
58 if (!EfiAtRuntime ()) {\r
59 EfiReleaseLock (Lock);\r
60 }\r
61}\r
62\r
63STATIC\r
64BOOLEAN\r
65EFIAPI\r
66IsValidVariableHeader (\r
67 IN VARIABLE_HEADER *Variable\r
68 )\r
69/*++\r
70\r
71Routine Description:\r
72\r
73 This code checks if variable header is valid or not.\r
74\r
75Arguments:\r
76 Variable Pointer to the Variable Header.\r
77\r
78Returns:\r
79 TRUE Variable header is valid.\r
80 FALSE Variable header is not valid.\r
81\r
82--*/\r
83{\r
84 if (Variable == NULL ||\r
85 Variable->StartId != VARIABLE_DATA ||\r
86 (sizeof (VARIABLE_HEADER) + Variable->NameSize + Variable->DataSize) > MAX_VARIABLE_SIZE\r
87 ) {\r
88 return FALSE;\r
89 }\r
90\r
91 return TRUE;\r
92}\r
93\r
94STATIC\r
95EFI_STATUS\r
96EFIAPI\r
97UpdateVariableStore (\r
98 IN VARIABLE_GLOBAL *Global,\r
99 IN BOOLEAN Volatile,\r
100 IN BOOLEAN SetByIndex,\r
101 IN UINTN Instance,\r
102 IN UINTN DataPtrIndex,\r
103 IN UINT32 DataSize,\r
104 IN UINT8 *Buffer\r
105 )\r
106/*++\r
107\r
108Routine Description:\r
109\r
110 This function writes data to the FWH at the correct LBA even if the LBAs\r
111 are fragmented.\r
112\r
113Arguments:\r
114\r
115 Global Pointer to VARAIBLE_GLOBAL structure\r
116 Volatile If the Variable is Volatile or Non-Volatile\r
117 SetByIndex TRUE: Target pointer is given as index\r
118 FALSE: Target pointer is absolute\r
119 Instance Instance of FV Block services\r
120 DataPtrIndex Pointer to the Data from the end of VARIABLE_STORE_HEADER\r
121 structure\r
122 DataSize Size of data to be written.\r
123 Buffer Pointer to the buffer from which data is written\r
124\r
125Returns:\r
126\r
127 EFI STATUS\r
128\r
129--*/\r
130{\r
131 EFI_FV_BLOCK_MAP_ENTRY *PtrBlockMapEntry;\r
132 UINTN BlockIndex2;\r
133 UINTN LinearOffset;\r
134 UINTN CurrWriteSize;\r
135 UINTN CurrWritePtr;\r
136 UINT8 *CurrBuffer;\r
137 EFI_LBA LbaNumber;\r
138 UINTN Size;\r
139 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r
140 VARIABLE_STORE_HEADER *VolatileBase;\r
141 EFI_PHYSICAL_ADDRESS FvVolHdr;\r
142 EFI_PHYSICAL_ADDRESS DataPtr;\r
143 EFI_STATUS Status;\r
144\r
145 FwVolHeader = NULL;\r
146 DataPtr = DataPtrIndex;\r
147\r
148 //\r
149 // Check if the Data is Volatile\r
150 //\r
151 if (!Volatile) {\r
152 EfiFvbGetPhysicalAddress (Instance, &FvVolHdr);\r
153 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvVolHdr);\r
154 //\r
155 // Data Pointer should point to the actual Address where data is to be\r
156 // written\r
157 //\r
158 if (SetByIndex) {\r
159 DataPtr += Global->NonVolatileVariableBase;\r
160 }\r
161\r
162 if ((DataPtr + DataSize) >= ((EFI_PHYSICAL_ADDRESS) (UINTN) ((UINT8 *) FwVolHeader + FwVolHeader->FvLength))) {\r
163 return EFI_INVALID_PARAMETER;\r
164 }\r
165 } else {\r
166 //\r
167 // Data Pointer should point to the actual Address where data is to be\r
168 // written\r
169 //\r
170 VolatileBase = (VARIABLE_STORE_HEADER *) ((UINTN) Global->VolatileVariableBase);\r
171 if (SetByIndex) {\r
172 DataPtr += Global->VolatileVariableBase;\r
173 }\r
174\r
175 if ((DataPtr + DataSize) >= ((UINTN) ((UINT8 *) VolatileBase + VolatileBase->Size))) {\r
176 return EFI_INVALID_PARAMETER;\r
177 }\r
178 }\r
179 //\r
180 // If Volatile Variable just do a simple mem copy.\r
181 //\r
182 if (Volatile) {\r
183 CopyMem ((UINT8 *) ((UINTN) DataPtr), Buffer, DataSize);\r
184 return EFI_SUCCESS;\r
185 }\r
186 //\r
187 // If we are here we are dealing with Non-Volatile Variables\r
188 //\r
189 LinearOffset = (UINTN) FwVolHeader;\r
190 CurrWritePtr = (UINTN) DataPtr;\r
191 CurrWriteSize = DataSize;\r
192 CurrBuffer = Buffer;\r
193 LbaNumber = 0;\r
194\r
195 if (CurrWritePtr < LinearOffset) {\r
196 return EFI_INVALID_PARAMETER;\r
197 }\r
198\r
199 for (PtrBlockMapEntry = FwVolHeader->BlockMap; PtrBlockMapEntry->NumBlocks != 0; PtrBlockMapEntry++) {\r
200 for (BlockIndex2 = 0; BlockIndex2 < PtrBlockMapEntry->NumBlocks; BlockIndex2++) {\r
201 //\r
202 // Check to see if the Variable Writes are spanning through multiple\r
203 // blocks.\r
204 //\r
205 if ((CurrWritePtr >= LinearOffset) && (CurrWritePtr < LinearOffset + PtrBlockMapEntry->Length)) {\r
206 if ((CurrWritePtr + CurrWriteSize) <= (LinearOffset + PtrBlockMapEntry->Length)) {\r
207 Status = EfiFvbWriteBlock (\r
208 Instance,\r
209 LbaNumber,\r
210 (UINTN) (CurrWritePtr - LinearOffset),\r
211 &CurrWriteSize,\r
212 CurrBuffer\r
213 );\r
214 if (EFI_ERROR (Status)) {\r
215 return Status;\r
216 }\r
217 } else {\r
218 Size = (UINT32) (LinearOffset + PtrBlockMapEntry->Length - CurrWritePtr);\r
219 Status = EfiFvbWriteBlock (\r
220 Instance,\r
221 LbaNumber,\r
222 (UINTN) (CurrWritePtr - LinearOffset),\r
223 &Size,\r
224 CurrBuffer\r
225 );\r
226 if (EFI_ERROR (Status)) {\r
227 return Status;\r
228 }\r
229\r
230 CurrWritePtr = LinearOffset + PtrBlockMapEntry->Length;\r
231 CurrBuffer = CurrBuffer + Size;\r
232 CurrWriteSize = CurrWriteSize - Size;\r
233 }\r
234 }\r
235\r
236 LinearOffset += PtrBlockMapEntry->Length;\r
237 LbaNumber++;\r
238 }\r
239 }\r
240\r
241 return EFI_SUCCESS;\r
242}\r
243\r
244STATIC\r
245VARIABLE_STORE_STATUS\r
246EFIAPI\r
247GetVariableStoreStatus (\r
248 IN VARIABLE_STORE_HEADER *VarStoreHeader\r
249 )\r
250/*++\r
251\r
252Routine Description:\r
253\r
254 This code gets the current status of Variable Store.\r
255\r
256Arguments:\r
257\r
258 VarStoreHeader Pointer to the Variable Store Header.\r
259\r
260Returns:\r
261\r
262 EfiRaw Variable store status is raw\r
263 EfiValid Variable store status is valid\r
264 EfiInvalid Variable store status is invalid\r
265\r
266--*/\r
267{\r
268 if (VarStoreHeader->Signature == VARIABLE_STORE_SIGNATURE &&\r
269 VarStoreHeader->Format == VARIABLE_STORE_FORMATTED &&\r
270 VarStoreHeader->State == VARIABLE_STORE_HEALTHY\r
271 ) {\r
272\r
273 return EfiValid;\r
274 } else if (VarStoreHeader->Signature == 0xffffffff &&\r
275 VarStoreHeader->Size == 0xffffffff &&\r
276 VarStoreHeader->Format == 0xff &&\r
277 VarStoreHeader->State == 0xff\r
278 ) {\r
279\r
280 return EfiRaw;\r
281 } else {\r
282 return EfiInvalid;\r
283 }\r
284}\r
285\r
286STATIC\r
287UINT8 *\r
288EFIAPI\r
289GetVariableDataPtr (\r
290 IN VARIABLE_HEADER *Variable\r
291 )\r
292/*++\r
293\r
294Routine Description:\r
295\r
296 This code gets the pointer to the variable data.\r
297\r
298Arguments:\r
299\r
300 Variable Pointer to the Variable Header.\r
301\r
302Returns:\r
303\r
304 UINT8* Pointer to Variable Data\r
305\r
306--*/\r
307{\r
308 //\r
309 // Be careful about pad size for alignment\r
310 //\r
311 return (UINT8 *) ((UINTN) GET_VARIABLE_NAME_PTR (Variable) + Variable->NameSize + GET_PAD_SIZE (Variable->NameSize));\r
312}\r
313\r
314STATIC\r
315VARIABLE_HEADER *\r
316EFIAPI\r
317GetNextVariablePtr (\r
318 IN VARIABLE_HEADER *Variable\r
319 )\r
320/*++\r
321\r
322Routine Description:\r
323\r
324 This code gets the pointer to the next variable header.\r
325\r
326Arguments:\r
327\r
328 Variable Pointer to the Variable Header.\r
329\r
330Returns:\r
331\r
332 VARIABLE_HEADER* Pointer to next variable header.\r
333\r
334--*/\r
335{\r
336 if (!IsValidVariableHeader (Variable)) {\r
337 return NULL;\r
338 }\r
339 //\r
340 // Be careful about pad size for alignment\r
341 //\r
342 return (VARIABLE_HEADER *) ((UINTN) GetVariableDataPtr (Variable) + Variable->DataSize + GET_PAD_SIZE (Variable->DataSize));\r
343}\r
344\r
345STATIC\r
346VARIABLE_HEADER *\r
347EFIAPI\r
348GetEndPointer (\r
349 IN VARIABLE_STORE_HEADER *VarStoreHeader\r
350 )\r
351/*++\r
352\r
353Routine Description:\r
354\r
355 This code gets the pointer to the last variable memory pointer byte\r
356\r
357Arguments:\r
358\r
359 VarStoreHeader Pointer to the Variable Store Header.\r
360\r
361Returns:\r
362\r
363 VARIABLE_HEADER* Pointer to last unavailable Variable Header\r
364\r
365--*/\r
366{\r
367 //\r
368 // The end of variable store\r
369 //\r
370 return (VARIABLE_HEADER *) ((UINTN) VarStoreHeader + VarStoreHeader->Size);\r
371}\r
372\r
373STATIC\r
374EFI_STATUS\r
375EFIAPI\r
376Reclaim (\r
377 IN EFI_PHYSICAL_ADDRESS VariableBase,\r
378 OUT UINTN *LastVariableOffset,\r
379 IN BOOLEAN IsVolatile\r
380 )\r
381/*++\r
382\r
383Routine Description:\r
384\r
385 Variable store garbage collection and reclaim operation\r
386\r
387Arguments:\r
388\r
389 VariableBase Base address of variable store\r
390 LastVariableOffset Offset of last variable\r
391 IsVolatile The variable store is volatile or not,\r
392 if it is non-volatile, need FTW\r
393\r
394Returns:\r
395\r
396 EFI STATUS\r
397\r
398--*/\r
399{\r
400 VARIABLE_HEADER *Variable;\r
401 VARIABLE_HEADER *NextVariable;\r
402 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
403 UINT8 *ValidBuffer;\r
404 UINTN ValidBufferSize;\r
405 UINTN VariableSize;\r
406 UINT8 *CurrPtr;\r
407 EFI_STATUS Status;\r
408\r
409 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) VariableBase);\r
410\r
411 //\r
412 // Start Pointers for the variable.\r
413 //\r
414 Variable = (VARIABLE_HEADER *) (VariableStoreHeader + 1);\r
415\r
416 ValidBufferSize = sizeof (VARIABLE_STORE_HEADER);\r
417\r
418 while (IsValidVariableHeader (Variable)) {\r
419 NextVariable = GetNextVariablePtr (Variable);\r
420 if (Variable->State == VAR_ADDED) {\r
421 VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
422 ValidBufferSize += VariableSize;\r
423 }\r
424\r
425 Variable = NextVariable;\r
426 }\r
427\r
428 ValidBuffer = AllocatePool (ValidBufferSize);\r
429 if (ValidBuffer == NULL) {\r
430 return EFI_OUT_OF_RESOURCES;\r
431 }\r
432\r
433 SetMem (ValidBuffer, ValidBufferSize, 0xff);\r
434\r
435 CurrPtr = ValidBuffer;\r
436\r
437 //\r
438 // Copy variable store header\r
439 //\r
440 CopyMem (CurrPtr, VariableStoreHeader, sizeof (VARIABLE_STORE_HEADER));\r
441 CurrPtr += sizeof (VARIABLE_STORE_HEADER);\r
442\r
443 //\r
444 // Start Pointers for the variable.\r
445 //\r
446 Variable = (VARIABLE_HEADER *) (VariableStoreHeader + 1);\r
447\r
448 while (IsValidVariableHeader (Variable)) {\r
449 NextVariable = GetNextVariablePtr (Variable);\r
450 if (Variable->State == VAR_ADDED) {\r
451 VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
452 CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);\r
453 CurrPtr += VariableSize;\r
454 }\r
455\r
456 Variable = NextVariable;\r
457 }\r
458\r
459 if (IsVolatile) {\r
460 //\r
461 // If volatile variable store, just copy valid buffer\r
462 //\r
463 SetMem ((UINT8 *) (UINTN) VariableBase, VariableStoreHeader->Size, 0xff);\r
464 CopyMem ((UINT8 *) (UINTN) VariableBase, ValidBuffer, ValidBufferSize);\r
465 *LastVariableOffset = ValidBufferSize;\r
466 Status = EFI_SUCCESS;\r
467 } else {\r
468 //\r
469 // If non-volatile variable store, perform FTW here.\r
470 //\r
471 Status = FtwVariableSpace (\r
472 VariableBase,\r
473 ValidBuffer,\r
474 ValidBufferSize\r
475 );\r
476 if (!EFI_ERROR (Status)) {\r
477 *LastVariableOffset = ValidBufferSize;\r
478 }\r
479 }\r
480\r
481 FreePool (ValidBuffer);\r
482\r
483 if (EFI_ERROR (Status)) {\r
484 *LastVariableOffset = 0;\r
485 }\r
486\r
487 return Status;\r
488}\r
489\r
490STATIC\r
491EFI_STATUS\r
492EFIAPI\r
493FindVariable (\r
494 IN CHAR16 *VariableName,\r
495 IN EFI_GUID *VendorGuid,\r
496 OUT VARIABLE_POINTER_TRACK *PtrTrack,\r
497 IN VARIABLE_GLOBAL *Global\r
498 )\r
499/*++\r
500\r
501Routine Description:\r
502\r
503 This code finds variable in storage blocks (Volatile or Non-Volatile)\r
504\r
505Arguments:\r
506\r
507 VariableName Name of the variable to be found\r
508 VendorGuid Vendor GUID to be found.\r
509 PtrTrack Variable Track Pointer structure that contains\r
510 Variable Information.\r
511 Contains the pointer of Variable header.\r
512 Global VARIABLE_GLOBAL pointer\r
513\r
514Returns:\r
515\r
516 EFI STATUS\r
517\r
518--*/\r
519{\r
520 VARIABLE_HEADER *Variable[2];\r
521 VARIABLE_STORE_HEADER *VariableStoreHeader[2];\r
522 UINTN Index;\r
523\r
524 //\r
525 // We aquire the lock at the entry of FindVariable as GetVariable, GetNextVariableName\r
526 // SetVariable all call FindVariable at entry point. Please move "Aquire Lock" to\r
527 // the correct places if this assumption does not hold TRUE anymore.\r
528 //\r
529 AcquireLockOnlyAtBootTime(&Global->VariableServicesLock);\r
530\r
531 //\r
532 // 0: Non-Volatile, 1: Volatile\r
533 //\r
534 VariableStoreHeader[0] = (VARIABLE_STORE_HEADER *) ((UINTN) Global->NonVolatileVariableBase);\r
535 VariableStoreHeader[1] = (VARIABLE_STORE_HEADER *) ((UINTN) Global->VolatileVariableBase);\r
536\r
537 //\r
538 // Start Pointers for the variable.\r
539 // Actual Data Pointer where data can be written.\r
540 //\r
541 Variable[0] = (VARIABLE_HEADER *) (VariableStoreHeader[0] + 1);\r
542 Variable[1] = (VARIABLE_HEADER *) (VariableStoreHeader[1] + 1);\r
543\r
544 if (VariableName[0] != 0 && VendorGuid == NULL) {\r
545 return EFI_INVALID_PARAMETER;\r
546 }\r
547 //\r
548 // Find the variable by walk through non-volatile and volatile variable store\r
549 //\r
550 for (Index = 0; Index < 2; Index++) {\r
551 PtrTrack->StartPtr = (VARIABLE_HEADER *) (VariableStoreHeader[Index] + 1);\r
552 PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader[Index]);\r
553\r
554 while (IsValidVariableHeader (Variable[Index]) && (Variable[Index] <= GetEndPointer (VariableStoreHeader[Index]))) {\r
555 if (Variable[Index]->State == VAR_ADDED) {\r
556 if (!(EfiAtRuntime () && !(Variable[Index]->Attributes & EFI_VARIABLE_RUNTIME_ACCESS))) {\r
557 if (VariableName[0] == 0) {\r
558 PtrTrack->CurrPtr = Variable[Index];\r
559 PtrTrack->Volatile = (BOOLEAN) Index;\r
560 return EFI_SUCCESS;\r
561 } else {\r
562 if (CompareGuid (VendorGuid, &Variable[Index]->VendorGuid)) {\r
563 if (!CompareMem (VariableName, GET_VARIABLE_NAME_PTR (Variable[Index]), Variable[Index]->NameSize)) {\r
564 PtrTrack->CurrPtr = Variable[Index];\r
565 PtrTrack->Volatile = (BOOLEAN) Index;\r
566 return EFI_SUCCESS;\r
567 }\r
568 }\r
569 }\r
570 }\r
571 }\r
572\r
573 Variable[Index] = GetNextVariablePtr (Variable[Index]);\r
574 }\r
575 //\r
576 // While (...)\r
577 //\r
578 }\r
579 //\r
580 // for (...)\r
581 //\r
582 PtrTrack->CurrPtr = NULL;\r
583 return EFI_NOT_FOUND;\r
584}\r
585\r
586EFI_STATUS\r
587EFIAPI\r
588GetVariable (\r
589 IN CHAR16 *VariableName,\r
590 IN EFI_GUID * VendorGuid,\r
591 OUT UINT32 *Attributes OPTIONAL,\r
592 IN OUT UINTN *DataSize,\r
593 OUT VOID *Data,\r
594 IN VARIABLE_GLOBAL * Global,\r
595 IN UINT32 Instance\r
596 )\r
597/*++\r
598\r
599Routine Description:\r
600\r
601 This code finds variable in storage blocks (Volatile or Non-Volatile)\r
602\r
603Arguments:\r
604\r
605 VariableName Name of Variable to be found\r
606 VendorGuid Variable vendor GUID\r
607 Attributes OPTIONAL Attribute value of the variable found\r
608 DataSize Size of Data found. If size is less than the\r
609 data, this value contains the required size.\r
610 Data Data pointer\r
611 Global Pointer to VARIABLE_GLOBAL structure\r
612 Instance Instance of the Firmware Volume.\r
613\r
614Returns:\r
615\r
616 EFI STATUS\r
617\r
618--*/\r
619{\r
620 VARIABLE_POINTER_TRACK Variable;\r
621 UINTN VarDataSize;\r
622 EFI_STATUS Status;\r
623\r
624 if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {\r
625 return EFI_INVALID_PARAMETER;\r
626 }\r
627 //\r
628 // Find existing variable\r
629 //\r
630 Status = FindVariable (VariableName, VendorGuid, &Variable, Global);\r
631\r
632 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
633 goto Done;\r
634 }\r
635 //\r
636 // Get data size\r
637 //\r
638 VarDataSize = Variable.CurrPtr->DataSize;\r
639 if (*DataSize >= VarDataSize) {\r
640 if (Data == NULL) {\r
641 Status = EFI_INVALID_PARAMETER;\r
642 goto Done;\r
643 }\r
644\r
645 CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);\r
646 if (Attributes != NULL) {\r
647 *Attributes = Variable.CurrPtr->Attributes;\r
648 }\r
649\r
650 *DataSize = VarDataSize;\r
651 Status = EFI_SUCCESS;\r
652 goto Done;\r
653 } else {\r
654 *DataSize = VarDataSize;\r
655 Status = EFI_BUFFER_TOO_SMALL;\r
656 goto Done;\r
657 }\r
658\r
659Done:\r
660 ReleaseLockOnlyAtBootTime (&Global->VariableServicesLock);\r
661 return Status;\r
662}\r
663\r
664EFI_STATUS\r
665EFIAPI\r
666GetNextVariableName (\r
667 IN OUT UINTN *VariableNameSize,\r
668 IN OUT CHAR16 *VariableName,\r
669 IN OUT EFI_GUID *VendorGuid,\r
670 IN VARIABLE_GLOBAL *Global,\r
671 IN UINT32 Instance\r
672 )\r
673/*++\r
674\r
675Routine Description:\r
676\r
677 This code Finds the Next available variable\r
678\r
679Arguments:\r
680\r
681 VariableNameSize Size of the variable\r
682 VariableName Pointer to variable name\r
683 VendorGuid Variable Vendor Guid\r
684 Global VARIABLE_GLOBAL structure pointer.\r
685 Instance FV instance\r
686\r
687Returns:\r
688\r
689 EFI STATUS\r
690\r
691--*/\r
692{\r
693 VARIABLE_POINTER_TRACK Variable;\r
694 UINTN VarNameSize;\r
695 EFI_STATUS Status;\r
696\r
697 if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {\r
698 return EFI_INVALID_PARAMETER;\r
699 }\r
700\r
701 Status = FindVariable (VariableName, VendorGuid, &Variable, Global);\r
702\r
703 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
704 goto Done;\r
705 }\r
706\r
707 if (VariableName[0] != 0) {\r
708 //\r
709 // If variable name is not NULL, get next variable\r
710 //\r
711 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
712 }\r
713\r
714 while (TRUE) {\r
715 //\r
716 // If both volatile and non-volatile variable store are parsed,\r
717 // return not found\r
718 //\r
719 if (Variable.CurrPtr >= Variable.EndPtr || Variable.CurrPtr == NULL) {\r
720 Variable.Volatile = (BOOLEAN) (Variable.Volatile ^ ((BOOLEAN) 0x1));\r
721 if (Variable.Volatile) {\r
722 Variable.StartPtr = (VARIABLE_HEADER *) ((UINTN) (Global->VolatileVariableBase + sizeof (VARIABLE_STORE_HEADER)));\r
723 Variable.EndPtr = (VARIABLE_HEADER *) GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) Global->VolatileVariableBase));\r
724 } else {\r
725 Status = EFI_NOT_FOUND;\r
726 goto Done;\r
727 }\r
728\r
729 Variable.CurrPtr = Variable.StartPtr;\r
730 if (!IsValidVariableHeader (Variable.CurrPtr)) {\r
731 continue;\r
732 }\r
733 }\r
734 //\r
735 // Variable is found\r
736 //\r
737 if (IsValidVariableHeader (Variable.CurrPtr) && Variable.CurrPtr->State == VAR_ADDED) {\r
738 if (!(EfiAtRuntime () && !(Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS))) {\r
739 VarNameSize = Variable.CurrPtr->NameSize;\r
740 if (VarNameSize <= *VariableNameSize) {\r
741 CopyMem (\r
742 VariableName,\r
743 GET_VARIABLE_NAME_PTR (Variable.CurrPtr),\r
744 VarNameSize\r
745 );\r
746 CopyMem (\r
747 VendorGuid,\r
748 &Variable.CurrPtr->VendorGuid,\r
749 sizeof (EFI_GUID)\r
750 );\r
751 Status = EFI_SUCCESS;\r
752 } else {\r
753 Status = EFI_BUFFER_TOO_SMALL;\r
754 }\r
755\r
756 *VariableNameSize = VarNameSize;\r
757 goto Done;\r
758 }\r
759 }\r
760\r
761 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
762 }\r
763\r
764Done:\r
765 ReleaseLockOnlyAtBootTime (&Global->VariableServicesLock);\r
766 return Status;\r
767}\r
768\r
769EFI_STATUS\r
770EFIAPI\r
771SetVariable (\r
772 IN CHAR16 *VariableName,\r
773 IN EFI_GUID *VendorGuid,\r
774 IN UINT32 Attributes,\r
775 IN UINTN DataSize,\r
776 IN VOID *Data,\r
777 IN VARIABLE_GLOBAL *Global,\r
778 IN UINTN *VolatileOffset,\r
779 IN UINTN *NonVolatileOffset,\r
780 IN UINT32 Instance\r
781 )\r
782/*++\r
783\r
784Routine Description:\r
785\r
786 This code sets variable in storage blocks (Volatile or Non-Volatile)\r
787\r
788Arguments:\r
789\r
790 VariableName Name of Variable to be found\r
791 VendorGuid Variable vendor GUID\r
792 Attributes Attribute value of the variable found\r
793 DataSize Size of Data found. If size is less than the\r
794 data, this value contains the required size.\r
795 Data Data pointer\r
796 Global Pointer to VARIABLE_GLOBAL structure\r
797 VolatileOffset The offset of last volatile variable\r
798 NonVolatileOffset The offset of last non-volatile variable\r
799 Instance Instance of the Firmware Volume.\r
800\r
801Returns:\r
802\r
803 EFI STATUS\r
804 EFI_INVALID_PARAMETER - Invalid parameter\r
805 EFI_SUCCESS - Set successfully\r
806 EFI_OUT_OF_RESOURCES - Resource not enough to set variable\r
807 EFI_NOT_FOUND - Not found\r
808\r
809--*/\r
810{\r
811 VARIABLE_POINTER_TRACK Variable;\r
812 EFI_STATUS Status;\r
813 VARIABLE_HEADER *NextVariable;\r
814 UINTN VarNameSize;\r
815 UINTN VarNameOffset;\r
816 UINTN VarDataOffset;\r
817 UINTN VarSize;\r
818 UINT8 State;\r
819 BOOLEAN Reclaimed;\r
820\r
821 Reclaimed = FALSE;\r
822\r
823 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r
824 return EFI_INVALID_PARAMETER;\r
825 }\r
826\r
827 Status = FindVariable (VariableName, VendorGuid, &Variable, Global);\r
828\r
829 if (Status == EFI_INVALID_PARAMETER) {\r
830 goto Done;\r
831 } else if (!EFI_ERROR (Status) && Variable.Volatile && EfiAtRuntime()) {\r
832 //\r
833 // If EfiAtRuntime and the variable is Volatile and Runtime Access,\r
834 // the volatile is ReadOnly, and SetVariable should be aborted and\r
835 // return EFI_WRITE_PROTECTED.\r
836 //\r
837 Status = EFI_WRITE_PROTECTED;\r
838 goto Done;\r
839 } else if (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > MAX_VARIABLE_SIZE) {\r
840 //\r
841 // The size of the VariableName, including the Unicode Null in bytes plus\r
842 // the DataSize is limited to maximum size of MAX_VARIABLE_SIZE (1024) bytes.\r
843 //\r
844 Status = EFI_INVALID_PARAMETER;\r
845 goto Done;\r
846 } else if (Attributes == EFI_VARIABLE_NON_VOLATILE) {\r
847 //\r
848 // Make sure not only EFI_VARIABLE_NON_VOLATILE is set\r
849 //\r
850 Status = EFI_INVALID_PARAMETER;\r
851 goto Done;\r
852 } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) ==\r
853 EFI_VARIABLE_RUNTIME_ACCESS) {\r
854 //\r
855 // Make sure if runtime bit is set, boot service bit is set also\r
856 //\r
857 Status = EFI_INVALID_PARAMETER;\r
858 goto Done;\r
859 } else if (EfiAtRuntime () && Attributes && !(Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {\r
860 //\r
861 // Runtime but Attribute is not Runtime\r
862 //\r
863 Status = EFI_INVALID_PARAMETER;\r
864 goto Done;\r
865 } else if (EfiAtRuntime () && Attributes && !(Attributes & EFI_VARIABLE_NON_VOLATILE)) {\r
866 //\r
867 // Cannot set volatile variable in Runtime\r
868 //\r
869 Status = EFI_INVALID_PARAMETER;\r
870 goto Done;\r
871 } else if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {\r
872 //\r
873 // Setting a data variable with no access, or zero DataSize attributes\r
874 // specified causes it to be deleted.\r
875 //\r
876 if (!EFI_ERROR (Status)) {\r
877 State = Variable.CurrPtr->State;\r
878 State &= VAR_DELETED;\r
879\r
880 Status = UpdateVariableStore (\r
881 Global,\r
882 Variable.Volatile,\r
883 FALSE,\r
884 Instance,\r
885 (UINTN) &Variable.CurrPtr->State,\r
886 sizeof (UINT8),\r
887 &State\r
888 );\r
889 if (EFI_ERROR (Status)) {\r
890 goto Done;\r
891 }\r
892\r
893 Status = EFI_SUCCESS;\r
894 goto Done;\r
895 }\r
896\r
897 Status = EFI_NOT_FOUND;\r
898 goto Done;\r
899 } else {\r
900 if (!EFI_ERROR (Status)) {\r
901 //\r
902 // If the variable is marked valid and the same data has been passed in\r
903 // then return to the caller immediately.\r
904 //\r
905 if (Variable.CurrPtr->DataSize == DataSize &&\r
906 !CompareMem (Data, GetVariableDataPtr (Variable.CurrPtr), DataSize)\r
907 ) {\r
908 Status = EFI_SUCCESS;\r
909 goto Done;\r
910 } else if (Variable.CurrPtr->State == VAR_ADDED) {\r
911 //\r
912 // Mark the old variable as in delete transition\r
913 //\r
914 State = Variable.CurrPtr->State;\r
915 State &= VAR_IN_DELETED_TRANSITION;\r
916\r
917 Status = UpdateVariableStore (\r
918 Global,\r
919 Variable.Volatile,\r
920 FALSE,\r
921 Instance,\r
922 (UINTN) &Variable.CurrPtr->State,\r
923 sizeof (UINT8),\r
924 &State\r
925 );\r
926 if (EFI_ERROR (Status)) {\r
927 goto Done;\r
928 }\r
929 }\r
930 }\r
931 //\r
932 // Create a new variable and copy the data.\r
933 //\r
934 // Tricky part: Use scratch data area at the end of volatile variable store\r
935 // as a temporary storage.\r
936 //\r
937 NextVariable = GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) Global->VolatileVariableBase));\r
938\r
939 SetMem (NextVariable, SCRATCH_SIZE, 0xff);\r
940\r
941 NextVariable->StartId = VARIABLE_DATA;\r
942 NextVariable->Attributes = Attributes;\r
943 //\r
944 // NextVariable->State = VAR_ADDED;\r
945 //\r
946 NextVariable->Reserved = 0;\r
947 VarNameOffset = sizeof (VARIABLE_HEADER);\r
948 VarNameSize = StrSize (VariableName);\r
949 CopyMem (\r
950 (UINT8 *) ((UINTN) NextVariable + VarNameOffset),\r
951 VariableName,\r
952 VarNameSize\r
953 );\r
954 VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);\r
955 CopyMem (\r
956 (UINT8 *) ((UINTN) NextVariable + VarDataOffset),\r
957 Data,\r
958 DataSize\r
959 );\r
960 CopyMem (&NextVariable->VendorGuid, VendorGuid, sizeof (EFI_GUID));\r
961 //\r
962 // There will be pad bytes after Data, the NextVariable->NameSize and\r
963 // NextVariable->DataSize should not include pad size so that variable\r
964 // service can get actual size in GetVariable\r
965 //\r
966 NextVariable->NameSize = (UINT32)VarNameSize;\r
967 NextVariable->DataSize = (UINT32)DataSize;\r
968\r
969 //\r
970 // The actual size of the variable that stores in storage should\r
971 // include pad size.\r
972 //\r
973 VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);\r
974 if (Attributes & EFI_VARIABLE_NON_VOLATILE) {\r
975 if ((UINT32) (VarSize +*NonVolatileOffset) >\r
976 ((VARIABLE_STORE_HEADER *) ((UINTN) (Global->NonVolatileVariableBase)))->Size\r
977 ) {\r
978 if (EfiAtRuntime ()) {\r
979 Status = EFI_OUT_OF_RESOURCES;\r
980 goto Done;\r
981 }\r
982 //\r
983 // Perform garbage collection & reclaim operation\r
984 //\r
985 Status = Reclaim (Global->NonVolatileVariableBase, NonVolatileOffset, FALSE);\r
986 if (EFI_ERROR (Status)) {\r
987 goto Done;\r
988 }\r
989 //\r
990 // If still no enough space, return out of resources\r
991 //\r
992 if ((UINT32) (VarSize +*NonVolatileOffset) >\r
993 ((VARIABLE_STORE_HEADER *) ((UINTN) (Global->NonVolatileVariableBase)))->Size\r
994 ) {\r
995 Status = EFI_OUT_OF_RESOURCES;\r
996 goto Done;\r
997 }\r
998\r
999 Reclaimed = TRUE;\r
1000 }\r
1001 //\r
1002 // Three steps\r
1003 // 1. Write variable header\r
1004 // 2. Write variable data\r
1005 // 3. Set variable state to valid\r
1006 //\r
1007 //\r
1008 // Step 1:\r
1009 //\r
1010 Status = UpdateVariableStore (\r
1011 Global,\r
1012 FALSE,\r
1013 TRUE,\r
1014 Instance,\r
1015 *NonVolatileOffset,\r
1016 sizeof (VARIABLE_HEADER),\r
1017 (UINT8 *) NextVariable\r
1018 );\r
1019\r
1020 if (EFI_ERROR (Status)) {\r
1021 goto Done;\r
1022 }\r
1023 //\r
1024 // Step 2:\r
1025 //\r
1026 Status = UpdateVariableStore (\r
1027 Global,\r
1028 FALSE,\r
1029 TRUE,\r
1030 Instance,\r
1031 *NonVolatileOffset + sizeof (VARIABLE_HEADER),\r
1032 (UINT32) VarSize - sizeof (VARIABLE_HEADER),\r
1033 (UINT8 *) NextVariable + sizeof (VARIABLE_HEADER)\r
1034 );\r
1035\r
1036 if (EFI_ERROR (Status)) {\r
1037 goto Done;\r
1038 }\r
1039 //\r
1040 // Step 3:\r
1041 //\r
1042 NextVariable->State = VAR_ADDED;\r
1043 Status = UpdateVariableStore (\r
1044 Global,\r
1045 FALSE,\r
1046 TRUE,\r
1047 Instance,\r
1048 *NonVolatileOffset,\r
1049 sizeof (VARIABLE_HEADER),\r
1050 (UINT8 *) NextVariable\r
1051 );\r
1052\r
1053 if (EFI_ERROR (Status)) {\r
1054 goto Done;\r
1055 }\r
1056\r
1057 *NonVolatileOffset = *NonVolatileOffset + VarSize;\r
1058\r
1059 } else {\r
1060 if (EfiAtRuntime ()) {\r
1061 Status = EFI_INVALID_PARAMETER;\r
1062 goto Done;\r
1063 }\r
1064\r
1065 if ((UINT32) (VarSize +*VolatileOffset) >\r
1066 ((VARIABLE_STORE_HEADER *) ((UINTN) (Global->VolatileVariableBase)))->Size\r
1067 ) {\r
1068 //\r
1069 // Perform garbage collection & reclaim operation\r
1070 //\r
1071 Status = Reclaim (Global->VolatileVariableBase, VolatileOffset, TRUE);\r
1072 if (EFI_ERROR (Status)) {\r
1073 goto Done;\r
1074 }\r
1075 //\r
1076 // If still no enough space, return out of resources\r
1077 //\r
1078 if ((UINT32) (VarSize +*VolatileOffset) >\r
1079 ((VARIABLE_STORE_HEADER *) ((UINTN) (Global->VolatileVariableBase)))->Size\r
1080 ) {\r
1081 Status = EFI_OUT_OF_RESOURCES;\r
1082 goto Done;\r
1083 }\r
1084\r
1085 Reclaimed = TRUE;\r
1086 }\r
1087\r
1088 NextVariable->State = VAR_ADDED;\r
1089 Status = UpdateVariableStore (\r
1090 Global,\r
1091 TRUE,\r
1092 TRUE,\r
1093 Instance,\r
1094 *VolatileOffset,\r
1095 (UINT32) VarSize,\r
1096 (UINT8 *) NextVariable\r
1097 );\r
1098\r
1099 if (EFI_ERROR (Status)) {\r
1100 goto Done;\r
1101 }\r
1102\r
1103 *VolatileOffset = *VolatileOffset + VarSize;\r
1104 }\r
1105 //\r
1106 // Mark the old variable as deleted\r
1107 //\r
1108 if (!Reclaimed && !EFI_ERROR (Status) && Variable.CurrPtr != NULL) {\r
1109 State = Variable.CurrPtr->State;\r
1110 State &= VAR_DELETED;\r
1111\r
1112 Status = UpdateVariableStore (\r
1113 Global,\r
1114 Variable.Volatile,\r
1115 FALSE,\r
1116 Instance,\r
1117 (UINTN) &Variable.CurrPtr->State,\r
1118 sizeof (UINT8),\r
1119 &State\r
1120 );\r
1121\r
1122 if (EFI_ERROR (Status)) {\r
1123 goto Done;\r
1124 }\r
1125 }\r
1126 }\r
1127\r
1128 Status = EFI_SUCCESS;\r
1129Done:\r
1130 ReleaseLockOnlyAtBootTime (&Global->VariableServicesLock);\r
1131 return Status;\r
1132}\r
1133\r
1134EFI_STATUS\r
1135EFIAPI\r
1136QueryVariableInfo (\r
1137 IN UINT32 Attributes,\r
1138 OUT UINT64 *MaximumVariableStorageSize,\r
1139 OUT UINT64 *RemainingVariableStorageSize,\r
1140 OUT UINT64 *MaximumVariableSize,\r
1141 IN VARIABLE_GLOBAL *Global,\r
1142 IN UINT32 Instance\r
1143 )\r
1144/*++\r
1145\r
1146Routine Description:\r
1147\r
1148 This code returns information about the EFI variables.\r
1149\r
1150Arguments:\r
1151\r
1152 Attributes Attributes bitmask to specify the type of variables\r
1153 on which to return information.\r
1154 MaximumVariableStorageSize Pointer to the maximum size of the storage space available\r
1155 for the EFI variables associated with the attributes specified.\r
1156 RemainingVariableStorageSize Pointer to the remaining size of the storage space available\r
1157 for the EFI variables associated with the attributes specified.\r
1158 MaximumVariableSize Pointer to the maximum size of the individual EFI variables\r
1159 associated with the attributes specified.\r
1160 Global Pointer to VARIABLE_GLOBAL structure.\r
1161 Instance Instance of the Firmware Volume.\r
1162\r
1163Returns:\r
1164\r
1165 EFI STATUS\r
1166 EFI_INVALID_PARAMETER - An invalid combination of attribute bits was supplied.\r
1167 EFI_SUCCESS - Query successfully.\r
1168 EFI_UNSUPPORTED - The attribute is not supported on this platform.\r
1169\r
1170--*/\r
1171{\r
1172 VARIABLE_HEADER *Variable;\r
1173 VARIABLE_HEADER *NextVariable;\r
1174 UINT64 VariableSize;\r
1175 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
1176\r
1177 if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL) {\r
1178 return EFI_INVALID_PARAMETER;\r
1179 }\r
1180\r
1181 if((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS)) == 0) {\r
1182 //\r
1183 // Make sure the Attributes combination is supported by the platform.\r
1184 //\r
1185 return EFI_UNSUPPORTED;\r
1186 } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
1187 //\r
1188 // Make sure if runtime bit is set, boot service bit is set also.\r
1189 //\r
1190 return EFI_INVALID_PARAMETER;\r
1191 } else if (EfiAtRuntime () && !(Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {\r
1192 //\r
1193 // Make sure RT Attribute is set if we are in Runtime phase.\r
1194 //\r
1195 return EFI_INVALID_PARAMETER;\r
1196 }\r
1197\r
1198 AcquireLockOnlyAtBootTime(&Global->VariableServicesLock);\r
1199\r
1200 if((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r
1201 //\r
1202 // Query is Volatile related.\r
1203 //\r
1204 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) Global->VolatileVariableBase);\r
1205 } else {\r
1206 //\r
1207 // Query is Non-Volatile related.\r
1208 //\r
1209 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) Global->NonVolatileVariableBase);\r
1210 }\r
1211\r
1212 //\r
1213 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize\r
1214 // with the storage size (excluding the storage header size).\r
1215 //\r
1216 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);\r
1217 *RemainingVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);\r
1218\r
1219 //\r
1220 // Let *MaximumVariableSize be MAX_VARIABLE_SIZE.\r
1221 //\r
1222 *MaximumVariableSize = MAX_VARIABLE_SIZE;\r
1223\r
1224 //\r
1225 // Point to the starting address of the variables.\r
1226 //\r
1227 Variable = (VARIABLE_HEADER *) (VariableStoreHeader + 1);\r
1228\r
1229 //\r
1230 // Now walk through the related variable store.\r
1231 //\r
1232 while (IsValidVariableHeader (Variable) && (Variable < GetEndPointer (VariableStoreHeader))) {\r
1233 NextVariable = GetNextVariablePtr (Variable);\r
1234 VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;\r
1235\r
1236 if (EfiAtRuntime ()) {\r
1237 //\r
1238 // we don't take the state of the variables in mind\r
1239 // when calculating RemainingVariableStorageSize,\r
1240 // since the space occupied by variables not marked with\r
1241 // VAR_ADDED is not allowed to be reclaimed in Runtime.\r
1242 //\r
1243 *RemainingVariableStorageSize -= VariableSize;\r
1244 } else {\r
1245 //\r
1246 // Only care about Variables with State VAR_ADDED,because\r
1247 // the space not marked as VAR_ADDED is reclaimable now.\r
1248 //\r
1249 if (Variable->State == VAR_ADDED) {\r
1250 *RemainingVariableStorageSize -= VariableSize;\r
1251 }\r
1252 }\r
1253\r
1254 //\r
1255 // Go to the next one\r
1256 //\r
1257 Variable = NextVariable;\r
1258 }\r
1259\r
1260 ReleaseLockOnlyAtBootTime (&Global->VariableServicesLock);\r
1261 return EFI_SUCCESS;\r
1262}\r
1263\r
1264EFI_STATUS\r
1265EFIAPI\r
1266VariableCommonInitialize (\r
1267 IN EFI_HANDLE ImageHandle,\r
1268 IN EFI_SYSTEM_TABLE *SystemTable\r
1269 )\r
1270/*++\r
1271\r
1272Routine Description:\r
1273 This function does common initialization for variable services\r
1274\r
1275Arguments:\r
1276\r
1277 ImageHandle - The firmware allocated handle for the EFI image.\r
1278 SystemTable - A pointer to the EFI System Table.\r
1279\r
1280Returns:\r
1281\r
1282 Status code.\r
1283\r
1284 EFI_NOT_FOUND - Variable store area not found.\r
1285 EFI_UNSUPPORTED - Currently only one non-volatile variable store is supported.\r
1286 EFI_SUCCESS - Variable services successfully initialized.\r
1287\r
1288--*/\r
1289{\r
1290 EFI_STATUS Status;\r
1291 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r
1292 CHAR8 *CurrPtr;\r
1293 VARIABLE_STORE_HEADER *VolatileVariableStore;\r
1294 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
1295 VARIABLE_HEADER *NextVariable;\r
1296 UINT32 Instance;\r
1297 EFI_PHYSICAL_ADDRESS FvVolHdr;\r
1298\r
1299 UINT64 TempVariableStoreHeader;\r
1300\r
1301 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;\r
1302 EFI_FLASH_SUBAREA_ENTRY VariableStoreEntry;\r
1303 UINT64 BaseAddress;\r
1304 UINT64 Length;\r
1305 UINTN Index;\r
1306 UINT8 Data;\r
1307\r
1308 mVariableModuleGlobal = AllocateRuntimePool (sizeof (ESAL_VARIABLE_GLOBAL));\r
1309 if (mVariableModuleGlobal == NULL) {\r
1310 return EFI_OUT_OF_RESOURCES;\r
1311 }\r
1312\r
1313 EfiInitializeLock(&mVariableModuleGlobal->VariableGlobal[Physical].VariableServicesLock, TPL_NOTIFY);\r
1314\r
1315 //\r
1316 // Allocate memory for volatile variable store\r
1317 //\r
1318 VolatileVariableStore = AllocateRuntimePool (VARIABLE_STORE_SIZE + SCRATCH_SIZE);\r
1319 if (VolatileVariableStore == NULL) {\r
1320 FreePool (mVariableModuleGlobal);\r
1321 return EFI_OUT_OF_RESOURCES;\r
1322 }\r
1323\r
1324 SetMem (VolatileVariableStore, VARIABLE_STORE_SIZE + SCRATCH_SIZE, 0xff);\r
1325\r
1326 //\r
1327 // Variable Specific Data\r
1328 //\r
1329 mVariableModuleGlobal->VariableGlobal[Physical].VolatileVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VolatileVariableStore;\r
1330 mVariableModuleGlobal->VolatileLastVariableOffset = sizeof (VARIABLE_STORE_HEADER);\r
1331\r
1332 VolatileVariableStore->Signature = VARIABLE_STORE_SIGNATURE;\r
1333 VolatileVariableStore->Size = VARIABLE_STORE_SIZE;\r
1334 VolatileVariableStore->Format = VARIABLE_STORE_FORMATTED;\r
1335 VolatileVariableStore->State = VARIABLE_STORE_HEALTHY;\r
1336 VolatileVariableStore->Reserved = 0;\r
1337 VolatileVariableStore->Reserved1 = 0;\r
1338\r
1339 //\r
1340 // Get non volatile varaible store\r
1341 //\r
1342\r
1343 TempVariableStoreHeader = (UINT64) PcdGet32 (PcdFlashNvStorageVariableBase);\r
1344 VariableStoreEntry.Base = TempVariableStoreHeader + \\r
1345 (((EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) (TempVariableStoreHeader)) -> HeaderLength);\r
1346 VariableStoreEntry.Length = (UINT64) PcdGet32 (PcdFlashNvStorageVariableSize) - \\r
1347 (((EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) (TempVariableStoreHeader)) -> HeaderLength);\r
1348 //\r
1349 // Mark the variable storage region of the FLASH as RUNTIME\r
1350 //\r
1351 BaseAddress = VariableStoreEntry.Base & (~EFI_PAGE_MASK);\r
1352 Length = VariableStoreEntry.Length + (VariableStoreEntry.Base - BaseAddress);\r
1353 Length = (Length + EFI_PAGE_SIZE - 1) & (~EFI_PAGE_MASK);\r
1354\r
1355 Status = gDS->GetMemorySpaceDescriptor (BaseAddress, &GcdDescriptor);\r
1356 if (EFI_ERROR (Status)) {\r
1357 FreePool (mVariableModuleGlobal);\r
1358 FreePool (VolatileVariableStore);\r
1359 return EFI_UNSUPPORTED;\r
1360 }\r
1361\r
1362 Status = gDS->SetMemorySpaceAttributes (\r
1363 BaseAddress,\r
1364 Length,\r
1365 GcdDescriptor.Attributes | EFI_MEMORY_RUNTIME\r
1366 );\r
1367 if (EFI_ERROR (Status)) {\r
1368 FreePool (mVariableModuleGlobal);\r
1369 FreePool (VolatileVariableStore);\r
1370 return EFI_UNSUPPORTED;\r
1371 }\r
1372 //\r
1373 // Get address of non volatile variable store base\r
1374 //\r
1375 mVariableModuleGlobal->VariableGlobal[Physical].NonVolatileVariableBase = VariableStoreEntry.Base;\r
1376\r
1377 //\r
1378 // Check Integrity\r
1379 //\r
1380 //\r
1381 // Find the Correct Instance of the FV Block Service.\r
1382 //\r
1383 Instance = 0;\r
1384 CurrPtr = (CHAR8 *) ((UINTN) mVariableModuleGlobal->VariableGlobal[Physical].NonVolatileVariableBase);\r
1385 while (EfiFvbGetPhysicalAddress (Instance, &FvVolHdr) == EFI_SUCCESS) {\r
1386 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvVolHdr);\r
1387 if (CurrPtr >= (CHAR8 *) FwVolHeader && CurrPtr < (((CHAR8 *) FwVolHeader) + FwVolHeader->FvLength)) {\r
1388 mVariableModuleGlobal->FvbInstance = Instance;\r
1389 break;\r
1390 }\r
1391\r
1392 Instance++;\r
1393 }\r
1394\r
1395 VariableStoreHeader = (VARIABLE_STORE_HEADER *) CurrPtr;\r
1396 if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) {\r
1397 if (~VariableStoreHeader->Size == 0) {\r
1398 Status = UpdateVariableStore (\r
1399 &mVariableModuleGlobal->VariableGlobal[Physical],\r
1400 FALSE,\r
1401 FALSE,\r
1402 mVariableModuleGlobal->FvbInstance,\r
1403 (UINTN) &VariableStoreHeader->Size,\r
1404 sizeof (UINT32),\r
1405 (UINT8 *) &VariableStoreEntry.Length\r
1406 );\r
1407 //\r
1408 // As Variables are stored in NV storage, which are slow devices,such as flash.\r
1409 // Variable operation may skip checking variable program result to improve performance,\r
1410 // We can assume Variable program is OK through some check point.\r
1411 // Variable Store Size Setting should be the first Variable write operation,\r
1412 // We can assume all Read/Write is OK if we can set Variable store size successfully.\r
1413 // If write fail, we will assert here\r
1414 //\r
1415 ASSERT(VariableStoreHeader->Size == VariableStoreEntry.Length);\r
1416\r
1417 if (EFI_ERROR (Status)) {\r
1418 return Status;\r
1419 }\r
1420 }\r
1421\r
1422 mVariableModuleGlobal->VariableGlobal[Physical].NonVolatileVariableBase = (EFI_PHYSICAL_ADDRESS) ((UINTN) CurrPtr);\r
1423 //\r
1424 // Parse non-volatile variable data and get last variable offset\r
1425 //\r
1426 NextVariable = (VARIABLE_HEADER *) (CurrPtr + sizeof (VARIABLE_STORE_HEADER));\r
1427 Status = EFI_SUCCESS;\r
1428\r
1429 while (IsValidVariableHeader (NextVariable)) {\r
1430 NextVariable = GetNextVariablePtr (NextVariable);\r
1431 }\r
1432\r
1433 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) CurrPtr;\r
1434\r
1435 //\r
1436 // Check if the free area is blow a threshold\r
1437 //\r
1438 if ((((VARIABLE_STORE_HEADER *)((UINTN) CurrPtr))->Size - mVariableModuleGlobal->NonVolatileLastVariableOffset) < VARIABLE_RECLAIM_THRESHOLD) {\r
1439 Status = Reclaim (\r
1440 mVariableModuleGlobal->VariableGlobal[Physical].NonVolatileVariableBase,\r
1441 &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
1442 FALSE\r
1443 );\r
1444 }\r
1445\r
1446 if (EFI_ERROR (Status)) {\r
1447 FreePool (mVariableModuleGlobal);\r
1448 FreePool (VolatileVariableStore);\r
1449 return Status;\r
1450 }\r
1451\r
1452 //\r
1453 // Check if the free area is really free.\r
1454 //\r
1455 for (Index = mVariableModuleGlobal->NonVolatileLastVariableOffset; Index < VariableStoreHeader->Size; Index++) {\r
1456 Data = ((UINT8 *) (UINTN) mVariableModuleGlobal->VariableGlobal[Physical].NonVolatileVariableBase)[Index];\r
1457 if (Data != 0xff) {\r
1458 //\r
1459 // There must be something wrong in variable store, do reclaim operation.\r
1460 //\r
1461 Status = Reclaim (\r
1462 mVariableModuleGlobal->VariableGlobal[Physical].NonVolatileVariableBase,\r
1463 &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
1464 FALSE\r
1465 );\r
1466 break;\r
1467 }\r
1468 }\r
1469 }\r
1470\r
1471 if (EFI_ERROR (Status)) {\r
1472 FreePool (mVariableModuleGlobal);\r
1473 FreePool (VolatileVariableStore);\r
1474 }\r
1475\r
1476 return Status;\r
1477}\r