]> git.proxmox.com Git - mirror_edk2.git/blob - DuetPkg/FSVariable/FSVariable.c
Patch to remove STATIC modifier. This is on longer recommended by EFI Framework codin...
[mirror_edk2.git] / DuetPkg / FSVariable / FSVariable.c
1 /*++
2
3 Copyright (c) 2006 - 2007, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 FSVariable.c
15
16 Abstract:
17
18 Provide support functions for variable services.
19
20 --*/
21
22 #include "FSVariable.h"
23
24 VARIABLE_STORE_HEADER mStoreHeaderTemplate = {
25 VARIABLE_STORE_SIGNATURE,
26 VOLATILE_VARIABLE_STORE_SIZE,
27 VARIABLE_STORE_FORMATTED,
28 VARIABLE_STORE_HEALTHY,
29 0,
30 0
31 };
32
33 //
34 // Don't use module globals after the SetVirtualAddress map is signaled
35 //
36 VARIABLE_GLOBAL *mGlobal;
37
38 VOID
39 EFIAPI
40 OnVirtualAddressChangeFsv (
41 IN EFI_EVENT Event,
42 IN VOID *Context
43 );
44
45 VOID
46 EFIAPI
47 OnSimpleFileSystemInstall (
48 IN EFI_EVENT Event,
49 IN VOID *Context
50 );
51
52 BOOLEAN
53 IsValidVariableHeader (
54 IN VARIABLE_HEADER *Variable
55 )
56 /*++
57
58 Routine Description:
59
60 This code checks if variable header is valid or not.
61
62 Arguments:
63 Variable Pointer to the Variable Header.
64
65 Returns:
66 TRUE Variable header is valid.
67 FALSE Variable header is not valid.
68
69 --*/
70 {
71 if (Variable == NULL ||
72 Variable->StartId != VARIABLE_DATA ||
73 (sizeof (VARIABLE_HEADER) + Variable->NameSize + Variable->DataSize) > MAX_VARIABLE_SIZE
74 ) {
75 return FALSE;
76 }
77
78 return TRUE;
79 }
80
81 VARIABLE_STORE_STATUS
82 GetVariableStoreStatus (
83 IN VARIABLE_STORE_HEADER *VarStoreHeader
84 )
85 /*++
86
87 Routine Description:
88
89 This code gets the current status of Variable Store.
90
91 Arguments:
92
93 VarStoreHeader Pointer to the Variable Store Header.
94
95 Returns:
96
97 EfiRaw Variable store status is raw
98 EfiValid Variable store status is valid
99 EfiInvalid Variable store status is invalid
100
101 --*/
102 {
103 if ((VarStoreHeader->Signature == mStoreHeaderTemplate.Signature) &&
104 (VarStoreHeader->Format == mStoreHeaderTemplate.Format) &&
105 (VarStoreHeader->State == mStoreHeaderTemplate.State)
106 ) {
107 return EfiValid;
108 } else if (VarStoreHeader->Signature == VAR_DEFAULT_VALUE_32 &&
109 VarStoreHeader->Size == VAR_DEFAULT_VALUE_32 &&
110 VarStoreHeader->Format == VAR_DEFAULT_VALUE &&
111 VarStoreHeader->State == VAR_DEFAULT_VALUE
112 ) {
113
114 return EfiRaw;
115 } else {
116 return EfiInvalid;
117 }
118 }
119
120 UINT8 *
121 GetVariableDataPtr (
122 IN VARIABLE_HEADER *Variable
123 )
124 /*++
125
126 Routine Description:
127
128 This code gets the pointer to the variable data.
129
130 Arguments:
131
132 Variable Pointer to the Variable Header.
133
134 Returns:
135
136 UINT8* Pointer to Variable Data
137
138 --*/
139 {
140 //
141 // Be careful about pad size for alignment
142 //
143 return (UINT8 *) ((UINTN) GET_VARIABLE_NAME_PTR (Variable) + Variable->NameSize + GET_PAD_SIZE (Variable->NameSize));
144 }
145
146 VARIABLE_HEADER *
147 GetNextVariablePtr (
148 IN VARIABLE_HEADER *Variable
149 )
150 /*++
151
152 Routine Description:
153
154 This code gets the pointer to the next variable header.
155
156 Arguments:
157
158 Variable Pointer to the Variable Header.
159
160 Returns:
161
162 VARIABLE_HEADER* Pointer to next variable header.
163
164 --*/
165 {
166 if (!IsValidVariableHeader (Variable)) {
167 return NULL;
168 }
169 //
170 // Be careful about pad size for alignment
171 //
172 return (VARIABLE_HEADER *) ((UINTN) GetVariableDataPtr (Variable) + Variable->DataSize + GET_PAD_SIZE (Variable->DataSize));
173 }
174
175 VARIABLE_HEADER *
176 GetEndPointer (
177 IN VARIABLE_STORE_HEADER *VarStoreHeader
178 )
179 /*++
180
181 Routine Description:
182
183 This code gets the pointer to the last variable memory pointer byte
184
185 Arguments:
186
187 VarStoreHeader Pointer to the Variable Store Header.
188
189 Returns:
190
191 VARIABLE_HEADER* Pointer to last unavailable Variable Header
192
193 --*/
194 {
195 //
196 // The end of variable store
197 //
198 return (VARIABLE_HEADER *) ((UINTN) VarStoreHeader + VarStoreHeader->Size);
199 }
200
201 BOOLEAN
202 ExistNewerVariable (
203 IN VARIABLE_HEADER *Variable
204 )
205 /*++
206
207 Routine Description:
208
209 Check if exist newer variable when doing reclaim
210
211 Arguments:
212
213 Variable Pointer to start position
214
215 Returns:
216
217 TRUE - Exists another variable, which is newer than the current one
218 FALSE - Doesn't exist another vairable which is newer than the current one
219
220 --*/
221 {
222 VARIABLE_HEADER *NextVariable;
223 CHAR16 *VariableName;
224 EFI_GUID *VendorGuid;
225
226 VendorGuid = &Variable->VendorGuid;
227 VariableName = GET_VARIABLE_NAME_PTR(Variable);
228
229 NextVariable = GetNextVariablePtr (Variable);
230 while (IsValidVariableHeader (NextVariable)) {
231 if ((NextVariable->State == VAR_ADDED) || (NextVariable->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {
232 //
233 // If match Guid and Name
234 //
235 if (CompareGuid (VendorGuid, &NextVariable->VendorGuid)) {
236 if (CompareMem (VariableName, GET_VARIABLE_NAME_PTR (NextVariable), StrSize (VariableName)) == 0) {
237 return TRUE;
238 }
239 }
240 }
241 NextVariable = GetNextVariablePtr (NextVariable);
242 }
243 return FALSE;
244 }
245
246 EFI_STATUS
247 Reclaim (
248 IN VARIABLE_STORAGE_TYPE StorageType,
249 IN VARIABLE_HEADER *CurrentVariable OPTIONAL
250 )
251 /*++
252
253 Routine Description:
254
255 Variable store garbage collection and reclaim operation
256
257 Arguments:
258
259 IsVolatile The variable store is volatile or not,
260 if it is non-volatile, need FTW
261 CurrentVairable If it is not NULL, it means not to process
262 current variable for Reclaim.
263
264 Returns:
265
266 EFI STATUS
267
268 --*/
269 {
270 VARIABLE_HEADER *Variable;
271 VARIABLE_HEADER *NextVariable;
272 VARIABLE_STORE_HEADER *VariableStoreHeader;
273 UINT8 *ValidBuffer;
274 UINTN ValidBufferSize;
275 UINTN VariableSize;
276 UINT8 *CurrPtr;
277 EFI_STATUS Status;
278
279 VariableStoreHeader = (VARIABLE_STORE_HEADER *) mGlobal->VariableBase[StorageType];
280
281 //
282 // Start Pointers for the variable.
283 //
284 Variable = (VARIABLE_HEADER *) (VariableStoreHeader + 1);
285
286
287 //
288 // To make the reclaim, here we just allocate a memory that equal to the original memory
289 //
290 ValidBufferSize = sizeof (VARIABLE_STORE_HEADER) + VariableStoreHeader->Size;
291
292 Status = gBS->AllocatePool (
293 EfiBootServicesData,
294 ValidBufferSize,
295 &ValidBuffer
296 );
297 if (EFI_ERROR (Status)) {
298 return Status;
299 }
300
301 CurrPtr = ValidBuffer;
302
303 //
304 // Copy variable store header
305 //
306 CopyMem (CurrPtr, VariableStoreHeader, sizeof (VARIABLE_STORE_HEADER));
307 CurrPtr += sizeof (VARIABLE_STORE_HEADER);
308
309 //
310 // Start Pointers for the variable.
311 //
312 Variable = (VARIABLE_HEADER *) (VariableStoreHeader + 1);
313
314
315 ValidBufferSize = sizeof (VARIABLE_STORE_HEADER);
316 while (IsValidVariableHeader (Variable)) {
317 NextVariable = GetNextVariablePtr (Variable);
318 //
319 // State VAR_ADDED or VAR_IN_DELETED_TRANSITION are to kept,
320 // The CurrentVariable, is also saved, as SetVariable may fail due to lack of space
321 //
322 if (Variable->State == VAR_ADDED) {
323 VariableSize = (UINTN) NextVariable - (UINTN) Variable;
324 CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);
325 ValidBufferSize += VariableSize;
326 CurrPtr += VariableSize;
327 } else if (Variable->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION)) {
328 //
329 // As variables that with the same guid and name may exist in NV due to power failure during SetVariable,
330 // we will only save the latest valid one
331 //
332 if (!ExistNewerVariable(Variable)) {
333 VariableSize = (UINTN) NextVariable - (UINTN) Variable;
334 CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);
335 //
336 // If CurrentVariable == Variable, mark as VAR_IN_DELETED_TRANSITION
337 //
338 if (Variable != CurrentVariable){
339 ((VARIABLE_HEADER *)CurrPtr)->State = VAR_ADDED;
340 }
341 CurrPtr += VariableSize;
342 ValidBufferSize += VariableSize;
343 }
344 }
345 Variable = NextVariable;
346 }
347
348 //
349 // TODO: cannot restore to original state, basic FTW needed
350 //
351 Status = mGlobal->VariableStore[StorageType]->Erase (
352 mGlobal->VariableStore[StorageType]
353 );
354 Status = mGlobal->VariableStore[StorageType]->Write (
355 mGlobal->VariableStore[StorageType],
356 0,
357 ValidBufferSize,
358 ValidBuffer
359 );
360
361 // ASSERT_EFI_ERROR (Status);
362
363 mGlobal->LastVariableOffset[StorageType] = ValidBufferSize;
364 gBS->FreePool (ValidBuffer);
365
366 return Status;
367 }
368
369 EFI_STATUS
370 FindVariable (
371 IN CHAR16 *VariableName,
372 IN EFI_GUID *VendorGuid,
373 OUT VARIABLE_POINTER_TRACK *PtrTrack
374 )
375 /*++
376
377 Routine Description:
378
379 This code finds variable in storage blocks (Volatile or Non-Volatile)
380
381 Arguments:
382
383 VariableName Name of the variable to be found
384 VendorGuid Vendor GUID to be found.
385 PtrTrack Variable Track Pointer structure that contains
386 Variable Information.
387 Contains the pointer of Variable header.
388
389 Returns:
390
391 EFI_INVALID_PARAMETER - Invalid parameter
392 EFI_SUCCESS - Find the specified variable
393 EFI_NOT_FOUND - Not found
394
395 --*/
396 {
397 VARIABLE_HEADER *Variable;
398 VARIABLE_STORE_HEADER *VariableStoreHeader;
399 UINTN Index;
400 VARIABLE_HEADER *InDeleteVariable;
401 UINTN InDeleteIndex;
402 VARIABLE_HEADER *InDeleteStartPtr;
403 VARIABLE_HEADER *InDeleteEndPtr;
404
405 if (VariableName[0] != 0 && VendorGuid == NULL) {
406 return EFI_INVALID_PARAMETER;
407 }
408
409 InDeleteVariable = NULL;
410 InDeleteIndex = (UINTN)-1;
411 InDeleteStartPtr = NULL;
412 InDeleteEndPtr = NULL;
413
414 for (Index = 0; Index < MaxType; Index ++) {
415 //
416 // 0: Non-Volatile, 1: Volatile
417 //
418 VariableStoreHeader = (VARIABLE_STORE_HEADER *) mGlobal->VariableBase[Index];
419
420 //
421 // Start Pointers for the variable.
422 // Actual Data Pointer where data can be written.
423 //
424 Variable = (VARIABLE_HEADER *) (VariableStoreHeader + 1);
425
426 //
427 // Find the variable by walk through non-volatile and volatile variable store
428 //
429 PtrTrack->StartPtr = Variable;
430 PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader);
431
432 while (IsValidVariableHeader (Variable) && (Variable < PtrTrack->EndPtr)) {
433 if (Variable->State == VAR_ADDED) {
434 if (!EfiAtRuntime () || (Variable->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {
435 if (VariableName[0] == 0) {
436 PtrTrack->CurrPtr = Variable;
437 PtrTrack->Type = (VARIABLE_STORAGE_TYPE) Index;
438 return EFI_SUCCESS;
439 } else {
440 if (CompareGuid (VendorGuid, &Variable->VendorGuid)) {
441 if (!CompareMem (VariableName, GET_VARIABLE_NAME_PTR (Variable), StrSize (VariableName))) {
442 PtrTrack->CurrPtr = Variable;
443 PtrTrack->Type = (VARIABLE_STORAGE_TYPE) Index;
444 return EFI_SUCCESS;
445 }
446 }
447 }
448 }
449 } else if (Variable->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION)) {
450 //
451 // VAR_IN_DELETED_TRANSITION should also be checked.
452 //
453 if (!EfiAtRuntime () || (Variable->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {
454 if (VariableName[0] == 0) {
455 InDeleteVariable = Variable;
456 InDeleteIndex = Index;
457 InDeleteStartPtr = PtrTrack->StartPtr;
458 InDeleteEndPtr = PtrTrack->EndPtr;
459 } else {
460 if (CompareGuid (VendorGuid, &Variable->VendorGuid)) {
461 if (!CompareMem (VariableName, GET_VARIABLE_NAME_PTR (Variable), StrSize (VariableName))) {
462 InDeleteVariable = Variable;
463 InDeleteIndex = Index;
464 InDeleteStartPtr = PtrTrack->StartPtr;
465 InDeleteEndPtr = PtrTrack->EndPtr;
466 }
467 }
468 }
469 }
470 }
471
472 Variable = GetNextVariablePtr (Variable);
473 }
474 //
475 // While (...)
476 //
477 }
478 //
479 // for (...)
480 //
481
482 //
483 // if VAR_IN_DELETED_TRANSITION found, and VAR_ADDED not found,
484 // we return it.
485 //
486 if (InDeleteVariable != NULL) {
487 PtrTrack->CurrPtr = InDeleteVariable;
488 PtrTrack->Type = (VARIABLE_STORAGE_TYPE) InDeleteIndex;
489 PtrTrack->StartPtr = InDeleteStartPtr;
490 PtrTrack->EndPtr = InDeleteEndPtr;
491 return EFI_SUCCESS;
492 }
493
494 PtrTrack->CurrPtr = NULL;
495 return EFI_NOT_FOUND;
496 }
497
498 EFI_STATUS
499 EFIAPI
500 GetVariable (
501 IN CHAR16 *VariableName,
502 IN EFI_GUID *VendorGuid,
503 OUT UINT32 *Attributes OPTIONAL,
504 IN OUT UINTN *DataSize,
505 OUT VOID *Data
506 )
507 /*++
508
509 Routine Description:
510
511 This code finds variable in storage blocks (Volatile or Non-Volatile)
512
513 Arguments:
514
515 VariableName Name of Variable to be found
516 VendorGuid Variable vendor GUID
517 Attributes OPTIONAL Attribute value of the variable found
518 DataSize Size of Data found. If size is less than the
519 data, this value contains the required size.
520 Data Data pointer
521
522 Returns:
523
524 EFI STATUS
525
526 --*/
527 {
528 VARIABLE_POINTER_TRACK Variable;
529 UINTN VarDataSize;
530 EFI_STATUS Status;
531
532 if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {
533 return EFI_INVALID_PARAMETER;
534 }
535
536 //
537 // Find existing variable
538 //
539 Status = FindVariable (VariableName, VendorGuid, &Variable);
540
541 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {
542 return Status;
543 }
544 //
545 // Get data size
546 //
547 VarDataSize = Variable.CurrPtr->DataSize;
548 if (*DataSize >= VarDataSize) {
549 if (Data == NULL) {
550 return EFI_INVALID_PARAMETER;
551 }
552 CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);
553
554 if (Attributes != NULL) {
555 *Attributes = Variable.CurrPtr->Attributes;
556 }
557
558 *DataSize = VarDataSize;
559
560 return EFI_SUCCESS;
561 } else {
562 *DataSize = VarDataSize;
563 return EFI_BUFFER_TOO_SMALL;
564 }
565 }
566
567 EFI_STATUS
568 EFIAPI
569 GetNextVariableName (
570 IN OUT UINTN *VariableNameSize,
571 IN OUT CHAR16 *VariableName,
572 IN OUT EFI_GUID *VendorGuid
573 )
574 /*++
575
576 Routine Description:
577
578 This code Finds the Next available variable
579
580 Arguments:
581
582 VariableNameSize Size of the variable
583 VariableName Pointer to variable name
584 VendorGuid Variable Vendor Guid
585
586 Returns:
587
588 EFI STATUS
589
590 --*/
591 {
592 VARIABLE_POINTER_TRACK Variable;
593 UINTN VarNameSize;
594 EFI_STATUS Status;
595
596 if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {
597 return EFI_INVALID_PARAMETER;
598 }
599
600 Status = FindVariable (VariableName, VendorGuid, &Variable);
601
602 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {
603 return Status;
604 }
605
606 if (VariableName[0] != 0) {
607 //
608 // If variable name is not NULL, get next variable
609 //
610 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
611 }
612
613 while (TRUE) {
614 //
615 // The order we find variable is: 1). NonVolatile; 2). Volatile
616 // If both volatile and non-volatile variable store are parsed,
617 // return not found
618 //
619 if (Variable.CurrPtr >= Variable.EndPtr || Variable.CurrPtr == NULL) {
620 if (Variable.Type == Volatile) {
621 //
622 // Since we met the end of Volatile storage, we have parsed all the stores.
623 //
624 return EFI_NOT_FOUND;
625 }
626
627 //
628 // End of NonVolatile, continue to parse Volatile
629 //
630 Variable.Type = Volatile;
631 Variable.StartPtr = (VARIABLE_HEADER *) ((VARIABLE_STORE_HEADER *) mGlobal->VariableBase[Volatile] + 1);
632 Variable.EndPtr = (VARIABLE_HEADER *) GetEndPointer ((VARIABLE_STORE_HEADER *) mGlobal->VariableBase[Volatile]);
633
634 Variable.CurrPtr = Variable.StartPtr;
635 if (!IsValidVariableHeader (Variable.CurrPtr)) {
636 continue;
637 }
638 }
639 //
640 // Variable is found
641 //
642 if (IsValidVariableHeader (Variable.CurrPtr) &&
643 ((Variable.CurrPtr->State == VAR_ADDED) ||
644 (Variable.CurrPtr->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION)))) {
645 if (!EfiAtRuntime () || (Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {
646 VarNameSize = Variable.CurrPtr->NameSize;
647 if (VarNameSize <= *VariableNameSize) {
648 CopyMem (
649 VariableName,
650 GET_VARIABLE_NAME_PTR (Variable.CurrPtr),
651 VarNameSize
652 );
653 CopyMem (
654 VendorGuid,
655 &Variable.CurrPtr->VendorGuid,
656 sizeof (EFI_GUID)
657 );
658 Status = EFI_SUCCESS;
659 } else {
660 Status = EFI_BUFFER_TOO_SMALL;
661 }
662
663 *VariableNameSize = VarNameSize;
664 return Status;
665 }
666 }
667
668 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
669 }
670
671 return EFI_NOT_FOUND;
672 }
673
674 EFI_STATUS
675 EFIAPI
676 SetVariable (
677 IN CHAR16 *VariableName,
678 IN EFI_GUID *VendorGuid,
679 IN UINT32 Attributes,
680 IN UINTN DataSize,
681 IN VOID *Data
682 )
683 /*++
684
685 Routine Description:
686
687 This code sets variable in storage blocks (Volatile or Non-Volatile)
688
689 Arguments:
690
691 VariableName Name of Variable to be found
692 VendorGuid Variable vendor GUID
693 Attributes Attribute value of the variable found
694 DataSize Size of Data found. If size is less than the
695 data, this value contains the required size.
696 Data Data pointer
697
698 Returns:
699
700 EFI_INVALID_PARAMETER - Invalid parameter
701 EFI_SUCCESS - Set successfully
702 EFI_OUT_OF_RESOURCES - Resource not enough to set variable
703 EFI_NOT_FOUND - Not found
704 EFI_DEVICE_ERROR - Variable can not be saved due to hardware failure
705 EFI_WRITE_PROTECTED - Variable is read-only
706
707 --*/
708 {
709 VARIABLE_POINTER_TRACK Variable;
710 EFI_STATUS Status;
711 VARIABLE_HEADER *NextVariable;
712 UINTN VarNameSize;
713 UINTN VarNameOffset;
714 UINTN VarDataOffset;
715 UINTN VarSize;
716 UINT8 State;
717 BOOLEAN Reclaimed;
718 VARIABLE_STORAGE_TYPE StorageType;
719
720 Reclaimed = FALSE;
721
722 //
723 // Check input parameters
724 //
725
726 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {
727 return EFI_INVALID_PARAMETER;
728 }
729
730 //
731 // Make sure if runtime bit is set, boot service bit is set also
732 //
733 if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {
734 return EFI_INVALID_PARAMETER;
735 }
736
737 //
738 // The size of the VariableName, including the Unicode Null in bytes plus
739 // the DataSize is limited to maximum size of MAX_HARDWARE_ERROR_VARIABLE_SIZE (32K)
740 // bytes for HwErrRec, and MAX_VARIABLE_SIZE (1024) bytes for the others.
741 //
742 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
743 if ((DataSize > MAX_HARDWARE_ERROR_VARIABLE_SIZE) ||
744 (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > MAX_HARDWARE_ERROR_VARIABLE_SIZE)) {
745 return EFI_INVALID_PARAMETER;
746 }
747 } else {
748 if ((DataSize > MAX_VARIABLE_SIZE) ||
749 (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > MAX_VARIABLE_SIZE)) {
750 return EFI_INVALID_PARAMETER;
751 }
752 }
753
754 //
755 // Check whether the input variable is already existed
756 //
757
758 Status = FindVariable (VariableName, VendorGuid, &Variable);
759
760 if (Status == EFI_SUCCESS && Variable.CurrPtr != NULL) {
761 //
762 // Update/Delete existing variable
763 //
764
765 if (EfiAtRuntime ()) {
766 //
767 // If EfiAtRuntime and the variable is Volatile and Runtime Access,
768 // the volatile is ReadOnly, and SetVariable should be aborted and
769 // return EFI_WRITE_PROTECTED.
770 //
771 if (Variable.Type == Volatile) {
772 return EFI_WRITE_PROTECTED;
773 }
774 //
775 // Only variable have NV attribute can be updated/deleted in Runtime
776 //
777 if (!(Variable.CurrPtr->Attributes & EFI_VARIABLE_NON_VOLATILE)) {
778 return EFI_INVALID_PARAMETER;
779 }
780 }
781
782 //
783 // Setting a data variable with no access, or zero DataSize attributes
784 // specified causes it to be deleted.
785 //
786 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {
787 //
788 // Found this variable in storage
789 //
790 State = Variable.CurrPtr->State;
791 State &= VAR_DELETED;
792
793 Status = mGlobal->VariableStore[Variable.Type]->Write (
794 mGlobal->VariableStore[Variable.Type],
795 VARIABLE_MEMBER_OFFSET (State, (UINTN) Variable.CurrPtr - (UINTN) Variable.StartPtr),
796 sizeof (Variable.CurrPtr->State),
797 &State
798 );
799 //
800 // NOTE: Write operation at least can write data to memory cache
801 // Discard file writing failure here.
802 //
803 return EFI_SUCCESS;
804 }
805
806 //
807 // Found this variable in storage
808 // If the variable is marked valid and the same data has been passed in
809 // then return to the caller immediately.
810 //
811 if ((Variable.CurrPtr->DataSize == DataSize) &&
812 (CompareMem (Data, GetVariableDataPtr (Variable.CurrPtr), DataSize) == 0)
813 ) {
814 return EFI_SUCCESS;
815 } else if ((Variable.CurrPtr->State == VAR_ADDED) ||
816 (Variable.CurrPtr->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {
817 //
818 // Mark the old variable as in delete transition
819 //
820 State = Variable.CurrPtr->State;
821 State &= VAR_IN_DELETED_TRANSITION;
822
823 Status = mGlobal->VariableStore[Variable.Type]->Write (
824 mGlobal->VariableStore[Variable.Type],
825 VARIABLE_MEMBER_OFFSET (State, (UINTN) Variable.CurrPtr - (UINTN) Variable.StartPtr),
826 sizeof (Variable.CurrPtr->State),
827 &State
828 );
829 //
830 // NOTE: Write operation at least can write data to memory cache
831 // Discard file writing failure here.
832 //
833 }
834 } else if (Status == EFI_NOT_FOUND) {
835 //
836 // Create a new variable
837 //
838
839 //
840 // Make sure we are trying to create a new variable.
841 // Setting a data variable with no access, or zero DataSize attributes means to delete it.
842 //
843 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {
844 return EFI_NOT_FOUND;
845 }
846 //
847 // Only variable have NV|RT attribute can be created in Runtime
848 //
849 if (EfiAtRuntime () &&
850 (!(Attributes & EFI_VARIABLE_RUNTIME_ACCESS) || !(Attributes & EFI_VARIABLE_NON_VOLATILE))) {
851 return EFI_INVALID_PARAMETER;
852 }
853
854 } else {
855 //
856 // Status should be EFI_INVALID_PARAMETER here according to return status of FindVariable().
857 //
858 return Status;
859 }
860
861 //
862 // Function part - create a new variable and copy the data.
863 // Both update a variable and create a variable will come here.
864 // We can firstly write all the data in memory, then write them to file
865 // This can reduce the times of write operation
866 //
867
868 NextVariable = (VARIABLE_HEADER *) mGlobal->Scratch;
869
870 NextVariable->StartId = VARIABLE_DATA;
871 NextVariable->Attributes = Attributes;
872 NextVariable->State = VAR_ADDED;
873 NextVariable->Reserved = 0;
874 VarNameOffset = sizeof (VARIABLE_HEADER);
875 VarNameSize = StrSize (VariableName);
876 CopyMem (
877 (UINT8 *) ((UINTN) NextVariable + VarNameOffset),
878 VariableName,
879 VarNameSize
880 );
881 VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);
882 CopyMem (
883 (UINT8 *) ((UINTN) NextVariable + VarDataOffset),
884 Data,
885 DataSize
886 );
887 CopyMem (&NextVariable->VendorGuid, VendorGuid, sizeof (EFI_GUID));
888 //
889 // There will be pad bytes after Data, the NextVariable->NameSize and
890 // NextVariable->DataSize should not include pad size so that variable
891 // service can get actual size in GetVariable
892 //
893 NextVariable->NameSize = (UINT32)VarNameSize;
894 NextVariable->DataSize = (UINT32)DataSize;
895
896 //
897 // The actual size of the variable that stores in storage should
898 // include pad size.
899 // VarDataOffset: offset from begin of current variable header
900 //
901 VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);
902
903 StorageType = (Attributes & EFI_VARIABLE_NON_VOLATILE) ? NonVolatile : Volatile;
904
905 if ((UINT32) (VarSize + mGlobal->LastVariableOffset[StorageType]) >
906 ((VARIABLE_STORE_HEADER *) mGlobal->VariableBase[StorageType])->Size
907 ) {
908 if ((StorageType == NonVolatile) && EfiAtRuntime ()) {
909 return EFI_OUT_OF_RESOURCES;
910 }
911 //
912 // Perform garbage collection & reclaim operation
913 //
914 Status = Reclaim (StorageType, Variable.CurrPtr);
915 if (EFI_ERROR (Status)) {
916 //
917 // Reclaim error
918 // we cannot restore to original state, fetal error, report to user
919 //
920 DEBUG ((EFI_D_ERROR, "FSVariable: Recalim error (fetal error) - %r\n", Status));
921 return Status;
922 }
923 //
924 // If still no enough space, return out of resources
925 //
926 if ((UINT32) (VarSize + mGlobal->LastVariableOffset[StorageType]) >
927 ((VARIABLE_STORE_HEADER *) mGlobal->VariableBase[StorageType])->Size
928 ) {
929 return EFI_OUT_OF_RESOURCES;
930 }
931
932 Reclaimed = TRUE;
933 }
934 Status = mGlobal->VariableStore[StorageType]->Write (
935 mGlobal->VariableStore[StorageType],
936 mGlobal->LastVariableOffset[StorageType],
937 VarSize,
938 NextVariable
939 );
940 //
941 // NOTE: Write operation at least can write data to memory cache
942 // Discard file writing failure here.
943 //
944 mGlobal->LastVariableOffset[StorageType] += VarSize;
945
946 //
947 // Mark the old variable as deleted
948 //
949 if (!Reclaimed && !EFI_ERROR (Status) && Variable.CurrPtr != NULL) {
950 State = Variable.CurrPtr->State;
951 State &= VAR_DELETED;
952
953 Status = mGlobal->VariableStore[StorageType]->Write (
954 mGlobal->VariableStore[StorageType],
955 VARIABLE_MEMBER_OFFSET (State, (UINTN) Variable.CurrPtr - (UINTN) Variable.StartPtr),
956 sizeof (Variable.CurrPtr->State),
957 &State
958 );
959 //
960 // NOTE: Write operation at least can write data to memory cache
961 // Discard file writing failure here.
962 //
963 }
964
965 return EFI_SUCCESS;
966 }
967
968 EFI_STATUS
969 EFIAPI
970 QueryVariableInfo (
971 IN UINT32 Attributes,
972 OUT UINT64 *MaximumVariableStorageSize,
973 OUT UINT64 *RemainingVariableStorageSize,
974 OUT UINT64 *MaximumVariableSize
975 )
976 /*++
977
978 Routine Description:
979
980 This code returns information about the EFI variables.
981
982 Arguments:
983
984 Attributes Attributes bitmask to specify the type of variables
985 on which to return information.
986 MaximumVariableStorageSize Pointer to the maximum size of the storage space available
987 for the EFI variables associated with the attributes specified.
988 RemainingVariableStorageSize Pointer to the remaining size of the storage space available
989 for the EFI variables associated with the attributes specified.
990 MaximumVariableSize Pointer to the maximum size of the individual EFI variables
991 associated with the attributes specified.
992
993 Returns:
994
995 EFI STATUS
996 EFI_INVALID_PARAMETER - An invalid combination of attribute bits was supplied.
997 EFI_SUCCESS - Query successfully.
998 EFI_UNSUPPORTED - The attribute is not supported on this platform.
999
1000 --*/
1001 {
1002 VARIABLE_HEADER *Variable;
1003 VARIABLE_HEADER *NextVariable;
1004 UINT64 VariableSize;
1005 VARIABLE_STORE_HEADER *VariableStoreHeader;
1006
1007 if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {
1008 return EFI_INVALID_PARAMETER;
1009 }
1010
1011 if((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == 0) {
1012 //
1013 // Make sure the Attributes combination is supported by the platform.
1014 //
1015 return EFI_UNSUPPORTED;
1016 }
1017 else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {
1018 //
1019 // Make sure if runtime bit is set, boot service bit is set also.
1020 //
1021 return EFI_INVALID_PARAMETER;
1022 } else if (EfiAtRuntime () && !(Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {
1023 //
1024 // Make sure RT Attribute is set if we are in Runtime phase.
1025 //
1026 return EFI_INVALID_PARAMETER;
1027 }
1028
1029 VariableStoreHeader = (VARIABLE_STORE_HEADER *) mGlobal->VariableBase[
1030 (Attributes & EFI_VARIABLE_NON_VOLATILE) ? NonVolatile : Volatile
1031 ];
1032 //
1033 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
1034 // with the storage size (excluding the storage header size).
1035 //
1036 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);
1037 *RemainingVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);
1038
1039 //
1040 // Let *MaximumVariableSize be MAX_VARIABLE_SIZE with the exception of the variable header size.
1041 //
1042 *MaximumVariableSize = MAX_VARIABLE_SIZE - sizeof (VARIABLE_HEADER);
1043
1044 //
1045 // Harware error record variable needs larger size.
1046 //
1047 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
1048 *MaximumVariableSize = MAX_HARDWARE_ERROR_VARIABLE_SIZE - sizeof (VARIABLE_HEADER);
1049 }
1050
1051 //
1052 // Point to the starting address of the variables.
1053 //
1054 Variable = (VARIABLE_HEADER *) (VariableStoreHeader + 1);
1055
1056 //
1057 // Now walk through the related variable store.
1058 //
1059 while (IsValidVariableHeader (Variable) && (Variable < GetEndPointer (VariableStoreHeader))) {
1060 NextVariable = GetNextVariablePtr (Variable);
1061 VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;
1062
1063 if (EfiAtRuntime ()) {
1064 //
1065 // we don't take the state of the variables in mind
1066 // when calculating RemainingVariableStorageSize,
1067 // since the space occupied by variables not marked with
1068 // VAR_ADDED is not allowed to be reclaimed in Runtime.
1069 //
1070 *RemainingVariableStorageSize -= VariableSize;
1071 } else {
1072 //
1073 // Only care about Variables with State VAR_ADDED,because
1074 // the space not marked as VAR_ADDED is reclaimable now.
1075 //
1076 if ((Variable->State == VAR_ADDED) ||
1077 (Variable->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {
1078 *RemainingVariableStorageSize -= VariableSize;
1079 }
1080 }
1081
1082 //
1083 // Go to the next one
1084 //
1085 Variable = NextVariable;
1086 }
1087
1088 if (*RemainingVariableStorageSize < sizeof (VARIABLE_HEADER)) {
1089 *MaximumVariableSize = 0;
1090 } else if ((*RemainingVariableStorageSize - sizeof (VARIABLE_HEADER)) < *MaximumVariableSize) {
1091 *MaximumVariableSize = *RemainingVariableStorageSize - sizeof (VARIABLE_HEADER);
1092 }
1093
1094 return EFI_SUCCESS;
1095 }
1096
1097 EFI_STATUS
1098 EFIAPI
1099 VariableServiceInitialize (
1100 IN EFI_HANDLE ImageHandle,
1101 IN EFI_SYSTEM_TABLE *SystemTable
1102 )
1103 /*++
1104
1105 Routine Description:
1106 This function does initialization for variable services
1107
1108 Arguments:
1109
1110 ImageHandle - The firmware allocated handle for the EFI image.
1111 SystemTable - A pointer to the EFI System Table.
1112
1113 Returns:
1114
1115 Status code.
1116
1117 EFI_NOT_FOUND - Variable store area not found.
1118 EFI_SUCCESS - Variable services successfully initialized.
1119
1120 --*/
1121 {
1122 EFI_STATUS Status;
1123 EFI_HANDLE NewHandle;
1124 VS_DEV *Dev;
1125 EFI_PEI_HOB_POINTERS GuidHob;
1126 VARIABLE_HEADER *NextVariable;
1127 VARIABLE_STORE_HEADER *VariableStoreHeader;
1128 EFI_FLASH_MAP_FS_ENTRY_DATA *FlashMapEntryData;
1129 EFI_FLASH_SUBAREA_ENTRY VariableStoreEntry;
1130 UINT64 BaseAddress;
1131 UINT64 Length;
1132 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;
1133
1134 Status = gBS->AllocatePool (
1135 EfiRuntimeServicesData,
1136 (UINTN) sizeof (VARIABLE_GLOBAL),
1137 &mGlobal
1138 );
1139 if (EFI_ERROR (Status)) {
1140 return Status;
1141 }
1142
1143 GuidHob.Raw = GetHobList ();
1144 FlashMapEntryData = NULL;
1145 while ((GuidHob.Raw = GetNextGuidHob (&gEfiFlashMapHobGuid, GuidHob.Raw)) != NULL) {
1146 FlashMapEntryData = (EFI_FLASH_MAP_FS_ENTRY_DATA *) GET_GUID_HOB_DATA (GuidHob.Guid);
1147 if (FlashMapEntryData->AreaType == EFI_FLASH_AREA_EFI_VARIABLES) {
1148 break;
1149 }
1150 GuidHob.Raw = GET_NEXT_HOB (GuidHob);
1151 }
1152
1153 if (FlashMapEntryData == NULL) {
1154 DEBUG ((EFI_D_ERROR, "FSVariable: Could not find flash area for variable!\n"));
1155 Status = EFI_NOT_FOUND;
1156 return Status;
1157 }
1158
1159 CopyMem(
1160 (VOID*)&VariableStoreEntry,
1161 (VOID*)&FlashMapEntryData->Entries[0],
1162 sizeof(EFI_FLASH_SUBAREA_ENTRY)
1163 );
1164
1165 //
1166 // Mark the variable storage region of the FLASH as RUNTIME
1167 //
1168 BaseAddress = VariableStoreEntry.Base & (~EFI_PAGE_MASK);
1169 Length = VariableStoreEntry.Length + (VariableStoreEntry.Base - BaseAddress);
1170 Length = (Length + EFI_PAGE_SIZE - 1) & (~EFI_PAGE_MASK);
1171 Status = gDS->GetMemorySpaceDescriptor (BaseAddress, &GcdDescriptor);
1172 if (EFI_ERROR (Status)) {
1173 Status = EFI_UNSUPPORTED;
1174 return Status;
1175 }
1176 Status = gDS->SetMemorySpaceAttributes (
1177 BaseAddress,
1178 Length,
1179 GcdDescriptor.Attributes | EFI_MEMORY_RUNTIME
1180 );
1181 if (EFI_ERROR (Status)) {
1182 Status = EFI_UNSUPPORTED;
1183 return Status;
1184 }
1185
1186 Status = FileStorageConstructor (
1187 &mGlobal->VariableStore[NonVolatile],
1188 &mGlobal->GoVirtualChildEvent[NonVolatile],
1189 VariableStoreEntry.Base,
1190 (UINT32) VariableStoreEntry.Length,
1191 FlashMapEntryData->VolumeId,
1192 FlashMapEntryData->FilePath
1193 );
1194 ASSERT_EFI_ERROR (Status);
1195
1196 //
1197 // Volatile Storage
1198 //
1199 Status = MemStorageConstructor (
1200 &mGlobal->VariableStore[Volatile],
1201 &mGlobal->GoVirtualChildEvent[Volatile],
1202 VOLATILE_VARIABLE_STORE_SIZE
1203 );
1204 ASSERT_EFI_ERROR (Status);
1205
1206 //
1207 // Scratch
1208 //
1209 Status = gBS->AllocatePool (
1210 EfiRuntimeServicesData,
1211 VARIABLE_SCRATCH_SIZE,
1212 &mGlobal->Scratch
1213 );
1214 ASSERT_EFI_ERROR (Status);
1215
1216 //
1217 // 1. NV Storage
1218 //
1219 Dev = DEV_FROM_THIS (mGlobal->VariableStore[NonVolatile]);
1220 VariableStoreHeader = (VARIABLE_STORE_HEADER *) VAR_DATA_PTR (Dev);
1221 if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) {
1222 if (~VariableStoreHeader->Size == 0) {
1223 VariableStoreHeader->Size = (UINT32) VariableStoreEntry.Length;
1224 }
1225 }
1226 //
1227 // Calculate LastVariableOffset
1228 //
1229 NextVariable = (VARIABLE_HEADER *) (VariableStoreHeader + 1);
1230 while (IsValidVariableHeader (NextVariable)) {
1231 NextVariable = GetNextVariablePtr (NextVariable);
1232 }
1233 mGlobal->LastVariableOffset[NonVolatile] = (UINTN) NextVariable - (UINTN) VariableStoreHeader;
1234 mGlobal->VariableBase[NonVolatile] = VariableStoreHeader;
1235
1236 //
1237 // Reclaim if remaining space is too small
1238 //
1239 if ((VariableStoreHeader->Size - mGlobal->LastVariableOffset[NonVolatile]) < VARIABLE_RECLAIM_THRESHOLD) {
1240 Status = Reclaim (NonVolatile, NULL);
1241 if (EFI_ERROR (Status)) {
1242 //
1243 // Reclaim error
1244 // we cannot restore to original state
1245 //
1246 DEBUG ((EFI_D_ERROR, "FSVariable: Recalim error (fetal error) - %r\n", Status));
1247 ASSERT_EFI_ERROR (Status);
1248 }
1249 }
1250
1251 //
1252 // 2. Volatile Storage
1253 //
1254 Dev = DEV_FROM_THIS (mGlobal->VariableStore[Volatile]);
1255 VariableStoreHeader = (VARIABLE_STORE_HEADER *) VAR_DATA_PTR (Dev);
1256 mGlobal->VariableBase[Volatile] = VAR_DATA_PTR (Dev);
1257 mGlobal->LastVariableOffset[Volatile] = sizeof (VARIABLE_STORE_HEADER);
1258 //
1259 // init store_header & body in memory.
1260 //
1261 mGlobal->VariableStore[Volatile]->Erase (mGlobal->VariableStore[Volatile]);
1262 mGlobal->VariableStore[Volatile]->Write (
1263 mGlobal->VariableStore[Volatile],
1264 0,
1265 sizeof (VARIABLE_STORE_HEADER),
1266 &mStoreHeaderTemplate
1267 );
1268
1269
1270 SystemTable->RuntimeServices->GetVariable = GetVariable;
1271 SystemTable->RuntimeServices->GetNextVariableName = GetNextVariableName;
1272 SystemTable->RuntimeServices->SetVariable = SetVariable;
1273
1274 SystemTable->RuntimeServices->QueryVariableInfo = QueryVariableInfo;
1275
1276 //
1277 // Now install the Variable Runtime Architectural Protocol on a new handle
1278 //
1279 NewHandle = NULL;
1280 Status = gBS->InstallMultipleProtocolInterfaces (
1281 &NewHandle,
1282 &gEfiVariableArchProtocolGuid,
1283 NULL,
1284 &gEfiVariableWriteArchProtocolGuid,
1285 NULL,
1286 NULL
1287 );
1288 ASSERT_EFI_ERROR (Status);
1289
1290 return Status;
1291
1292 //Shutdown:
1293 // EfiShutdownRuntimeDriverLib ();
1294 // return Status;
1295 }
1296
1297
1298
1299 VOID
1300 EFIAPI
1301 OnVirtualAddressChangeFsv (
1302 IN EFI_EVENT Event,
1303 IN VOID *Context
1304 )
1305 {
1306 UINTN Index;
1307
1308 for (Index = 0; Index < MaxType; Index++) {
1309 mGlobal->GoVirtualChildEvent[Index] (Event, mGlobal->VariableStore[Index]);
1310 EfiConvertPointer (0, &mGlobal->VariableStore[Index]);
1311 EfiConvertPointer (0, &mGlobal->VariableBase[Index]);
1312 }
1313 EfiConvertPointer (0, &mGlobal->Scratch);
1314 EfiConvertPointer (0, &mGlobal);
1315 }