]> git.proxmox.com Git - mirror_edk2.git/blob - DuetPkg/FSVariable/FSVariable.c
Move Varialbe common definitions (GET_PAD_SIZE and HEADER_ALIGN macro) into MdeModule...
[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) > FixedPcdGet32(PcdMaxVariableSize)
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 (VOID**) &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
672 EFI_STATUS
673 EFIAPI
674 SetVariable (
675 IN CHAR16 *VariableName,
676 IN EFI_GUID *VendorGuid,
677 IN UINT32 Attributes,
678 IN UINTN DataSize,
679 IN VOID *Data
680 )
681 /*++
682
683 Routine Description:
684
685 This code sets variable in storage blocks (Volatile or Non-Volatile)
686
687 Arguments:
688
689 VariableName Name of Variable to be found
690 VendorGuid Variable vendor GUID
691 Attributes Attribute value of the variable found
692 DataSize Size of Data found. If size is less than the
693 data, this value contains the required size.
694 Data Data pointer
695
696 Returns:
697
698 EFI_INVALID_PARAMETER - Invalid parameter
699 EFI_SUCCESS - Set successfully
700 EFI_OUT_OF_RESOURCES - Resource not enough to set variable
701 EFI_NOT_FOUND - Not found
702 EFI_DEVICE_ERROR - Variable can not be saved due to hardware failure
703 EFI_WRITE_PROTECTED - Variable is read-only
704
705 --*/
706 {
707 VARIABLE_POINTER_TRACK Variable;
708 EFI_STATUS Status;
709 VARIABLE_HEADER *NextVariable;
710 UINTN VarNameSize;
711 UINTN VarNameOffset;
712 UINTN VarDataOffset;
713 UINTN VarSize;
714 UINT8 State;
715 BOOLEAN Reclaimed;
716 VARIABLE_STORAGE_TYPE StorageType;
717
718 Reclaimed = FALSE;
719
720 //
721 // Check input parameters
722 //
723
724 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {
725 return EFI_INVALID_PARAMETER;
726 }
727
728 //
729 // Make sure if runtime bit is set, boot service bit is set also
730 //
731 if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {
732 return EFI_INVALID_PARAMETER;
733 }
734
735 //
736 // The size of the VariableName, including the Unicode Null in bytes plus
737 // the DataSize is limited to maximum size of FixedPcdGet32(PcdMaxHardwareErrorVariableSize)
738 // bytes for HwErrRec, and FixedPcdGet32(PcdMaxVariableSize) bytes for the others.
739 //
740 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
741 if ((DataSize > FixedPcdGet32(PcdMaxHardwareErrorVariableSize)) ||
742 (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > FixedPcdGet32(PcdMaxHardwareErrorVariableSize))) {
743 return EFI_INVALID_PARAMETER;
744 }
745 } else {
746 if ((DataSize > FixedPcdGet32(PcdMaxVariableSize)) ||
747 (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > FixedPcdGet32(PcdMaxVariableSize))) {
748 return EFI_INVALID_PARAMETER;
749 }
750 }
751
752 //
753 // Check whether the input variable is already existed
754 //
755
756 Status = FindVariable (VariableName, VendorGuid, &Variable);
757
758 if (Status == EFI_SUCCESS && Variable.CurrPtr != NULL) {
759 //
760 // Update/Delete existing variable
761 //
762
763 if (EfiAtRuntime ()) {
764 //
765 // If EfiAtRuntime and the variable is Volatile and Runtime Access,
766 // the volatile is ReadOnly, and SetVariable should be aborted and
767 // return EFI_WRITE_PROTECTED.
768 //
769 if (Variable.Type == Volatile) {
770 return EFI_WRITE_PROTECTED;
771 }
772 //
773 // Only variable have NV attribute can be updated/deleted in Runtime
774 //
775 if (!(Variable.CurrPtr->Attributes & EFI_VARIABLE_NON_VOLATILE)) {
776 return EFI_INVALID_PARAMETER;
777 }
778 }
779
780 //
781 // Setting a data variable with no access, or zero DataSize attributes
782 // specified causes it to be deleted.
783 //
784 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {
785 //
786 // Found this variable in storage
787 //
788 State = Variable.CurrPtr->State;
789 State &= VAR_DELETED;
790
791 Status = mGlobal->VariableStore[Variable.Type]->Write (
792 mGlobal->VariableStore[Variable.Type],
793 VARIABLE_MEMBER_OFFSET (State, (UINTN) Variable.CurrPtr - (UINTN) Variable.StartPtr),
794 sizeof (Variable.CurrPtr->State),
795 &State
796 );
797 //
798 // NOTE: Write operation at least can write data to memory cache
799 // Discard file writing failure here.
800 //
801 return EFI_SUCCESS;
802 }
803
804 //
805 // Found this variable in storage
806 // If the variable is marked valid and the same data has been passed in
807 // then return to the caller immediately.
808 //
809 if ((Variable.CurrPtr->DataSize == DataSize) &&
810 (CompareMem (Data, GetVariableDataPtr (Variable.CurrPtr), DataSize) == 0)
811 ) {
812 return EFI_SUCCESS;
813 } else if ((Variable.CurrPtr->State == VAR_ADDED) ||
814 (Variable.CurrPtr->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {
815 //
816 // Mark the old variable as in delete transition
817 //
818 State = Variable.CurrPtr->State;
819 State &= VAR_IN_DELETED_TRANSITION;
820
821 Status = mGlobal->VariableStore[Variable.Type]->Write (
822 mGlobal->VariableStore[Variable.Type],
823 VARIABLE_MEMBER_OFFSET (State, (UINTN) Variable.CurrPtr - (UINTN) Variable.StartPtr),
824 sizeof (Variable.CurrPtr->State),
825 &State
826 );
827 //
828 // NOTE: Write operation at least can write data to memory cache
829 // Discard file writing failure here.
830 //
831 }
832 } else if (Status == EFI_NOT_FOUND) {
833 //
834 // Create a new variable
835 //
836
837 //
838 // Make sure we are trying to create a new variable.
839 // Setting a data variable with no access, or zero DataSize attributes means to delete it.
840 //
841 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {
842 return EFI_NOT_FOUND;
843 }
844 //
845 // Only variable have NV|RT attribute can be created in Runtime
846 //
847 if (EfiAtRuntime () &&
848 (!(Attributes & EFI_VARIABLE_RUNTIME_ACCESS) || !(Attributes & EFI_VARIABLE_NON_VOLATILE))) {
849 return EFI_INVALID_PARAMETER;
850 }
851
852 } else {
853 //
854 // Status should be EFI_INVALID_PARAMETER here according to return status of FindVariable().
855 //
856 return Status;
857 }
858
859 //
860 // Function part - create a new variable and copy the data.
861 // Both update a variable and create a variable will come here.
862 // We can firstly write all the data in memory, then write them to file
863 // This can reduce the times of write operation
864 //
865
866 NextVariable = (VARIABLE_HEADER *) mGlobal->Scratch;
867
868 NextVariable->StartId = VARIABLE_DATA;
869 NextVariable->Attributes = Attributes;
870 NextVariable->State = VAR_ADDED;
871 NextVariable->Reserved = 0;
872 VarNameOffset = sizeof (VARIABLE_HEADER);
873 VarNameSize = StrSize (VariableName);
874 CopyMem (
875 (UINT8 *) ((UINTN) NextVariable + VarNameOffset),
876 VariableName,
877 VarNameSize
878 );
879 VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);
880 CopyMem (
881 (UINT8 *) ((UINTN) NextVariable + VarDataOffset),
882 Data,
883 DataSize
884 );
885 CopyMem (&NextVariable->VendorGuid, VendorGuid, sizeof (EFI_GUID));
886 //
887 // There will be pad bytes after Data, the NextVariable->NameSize and
888 // NextVariable->DataSize should not include pad size so that variable
889 // service can get actual size in GetVariable
890 //
891 NextVariable->NameSize = (UINT32)VarNameSize;
892 NextVariable->DataSize = (UINT32)DataSize;
893
894 //
895 // The actual size of the variable that stores in storage should
896 // include pad size.
897 // VarDataOffset: offset from begin of current variable header
898 //
899 VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);
900
901 StorageType = (Attributes & EFI_VARIABLE_NON_VOLATILE) ? NonVolatile : Volatile;
902
903 if ((UINT32) (VarSize + mGlobal->LastVariableOffset[StorageType]) >
904 ((VARIABLE_STORE_HEADER *) mGlobal->VariableBase[StorageType])->Size
905 ) {
906 if ((StorageType == NonVolatile) && EfiAtRuntime ()) {
907 return EFI_OUT_OF_RESOURCES;
908 }
909 //
910 // Perform garbage collection & reclaim operation
911 //
912 Status = Reclaim (StorageType, Variable.CurrPtr);
913 if (EFI_ERROR (Status)) {
914 //
915 // Reclaim error
916 // we cannot restore to original state, fetal error, report to user
917 //
918 DEBUG ((EFI_D_ERROR, "FSVariable: Recalim error (fetal error) - %r\n", Status));
919 return Status;
920 }
921 //
922 // If still no enough space, return out of resources
923 //
924 if ((UINT32) (VarSize + mGlobal->LastVariableOffset[StorageType]) >
925 ((VARIABLE_STORE_HEADER *) mGlobal->VariableBase[StorageType])->Size
926 ) {
927 return EFI_OUT_OF_RESOURCES;
928 }
929
930 Reclaimed = TRUE;
931 }
932 Status = mGlobal->VariableStore[StorageType]->Write (
933 mGlobal->VariableStore[StorageType],
934 mGlobal->LastVariableOffset[StorageType],
935 VarSize,
936 NextVariable
937 );
938 //
939 // NOTE: Write operation at least can write data to memory cache
940 // Discard file writing failure here.
941 //
942 mGlobal->LastVariableOffset[StorageType] += VarSize;
943
944 //
945 // Mark the old variable as deleted
946 //
947 if (!Reclaimed && !EFI_ERROR (Status) && Variable.CurrPtr != NULL) {
948 State = Variable.CurrPtr->State;
949 State &= VAR_DELETED;
950
951 Status = mGlobal->VariableStore[StorageType]->Write (
952 mGlobal->VariableStore[StorageType],
953 VARIABLE_MEMBER_OFFSET (State, (UINTN) Variable.CurrPtr - (UINTN) Variable.StartPtr),
954 sizeof (Variable.CurrPtr->State),
955 &State
956 );
957 //
958 // NOTE: Write operation at least can write data to memory cache
959 // Discard file writing failure here.
960 //
961 }
962
963 return EFI_SUCCESS;
964 }
965
966 EFI_STATUS
967 EFIAPI
968 QueryVariableInfo (
969 IN UINT32 Attributes,
970 OUT UINT64 *MaximumVariableStorageSize,
971 OUT UINT64 *RemainingVariableStorageSize,
972 OUT UINT64 *MaximumVariableSize
973 )
974 /*++
975
976 Routine Description:
977
978 This code returns information about the EFI variables.
979
980 Arguments:
981
982 Attributes Attributes bitmask to specify the type of variables
983 on which to return information.
984 MaximumVariableStorageSize Pointer to the maximum size of the storage space available
985 for the EFI variables associated with the attributes specified.
986 RemainingVariableStorageSize Pointer to the remaining size of the storage space available
987 for the EFI variables associated with the attributes specified.
988 MaximumVariableSize Pointer to the maximum size of the individual EFI variables
989 associated with the attributes specified.
990
991 Returns:
992
993 EFI STATUS
994 EFI_INVALID_PARAMETER - An invalid combination of attribute bits was supplied.
995 EFI_SUCCESS - Query successfully.
996 EFI_UNSUPPORTED - The attribute is not supported on this platform.
997
998 --*/
999 {
1000 VARIABLE_HEADER *Variable;
1001 VARIABLE_HEADER *NextVariable;
1002 UINT64 VariableSize;
1003 VARIABLE_STORE_HEADER *VariableStoreHeader;
1004
1005 if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {
1006 return EFI_INVALID_PARAMETER;
1007 }
1008
1009 if((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == 0) {
1010 //
1011 // Make sure the Attributes combination is supported by the platform.
1012 //
1013 return EFI_UNSUPPORTED;
1014 }
1015 else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {
1016 //
1017 // Make sure if runtime bit is set, boot service bit is set also.
1018 //
1019 return EFI_INVALID_PARAMETER;
1020 } else if (EfiAtRuntime () && !(Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {
1021 //
1022 // Make sure RT Attribute is set if we are in Runtime phase.
1023 //
1024 return EFI_INVALID_PARAMETER;
1025 }
1026
1027 VariableStoreHeader = (VARIABLE_STORE_HEADER *) mGlobal->VariableBase[
1028 (Attributes & EFI_VARIABLE_NON_VOLATILE) ? NonVolatile : Volatile
1029 ];
1030 //
1031 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
1032 // with the storage size (excluding the storage header size).
1033 //
1034 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);
1035 *RemainingVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);
1036
1037 //
1038 // Let *MaximumVariableSize be FixedPcdGet32(PcdMaxVariableSize) with the exception of the variable header size.
1039 //
1040 *MaximumVariableSize = FixedPcdGet32(PcdMaxVariableSize) - sizeof (VARIABLE_HEADER);
1041
1042 //
1043 // Harware error record variable needs larger size.
1044 //
1045 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
1046 *MaximumVariableSize = FixedPcdGet32(PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER);
1047 }
1048
1049 //
1050 // Point to the starting address of the variables.
1051 //
1052 Variable = (VARIABLE_HEADER *) (VariableStoreHeader + 1);
1053
1054 //
1055 // Now walk through the related variable store.
1056 //
1057 while (IsValidVariableHeader (Variable) && (Variable < GetEndPointer (VariableStoreHeader))) {
1058 NextVariable = GetNextVariablePtr (Variable);
1059 VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;
1060
1061 if (EfiAtRuntime ()) {
1062 //
1063 // we don't take the state of the variables in mind
1064 // when calculating RemainingVariableStorageSize,
1065 // since the space occupied by variables not marked with
1066 // VAR_ADDED is not allowed to be reclaimed in Runtime.
1067 //
1068 *RemainingVariableStorageSize -= VariableSize;
1069 } else {
1070 //
1071 // Only care about Variables with State VAR_ADDED,because
1072 // the space not marked as VAR_ADDED is reclaimable now.
1073 //
1074 if ((Variable->State == VAR_ADDED) ||
1075 (Variable->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {
1076 *RemainingVariableStorageSize -= VariableSize;
1077 }
1078 }
1079
1080 //
1081 // Go to the next one
1082 //
1083 Variable = NextVariable;
1084 }
1085
1086 if (*RemainingVariableStorageSize < sizeof (VARIABLE_HEADER)) {
1087 *MaximumVariableSize = 0;
1088 } else if ((*RemainingVariableStorageSize - sizeof (VARIABLE_HEADER)) < *MaximumVariableSize) {
1089 *MaximumVariableSize = *RemainingVariableStorageSize - sizeof (VARIABLE_HEADER);
1090 }
1091
1092 return EFI_SUCCESS;
1093 }
1094
1095 EFI_STATUS
1096 EFIAPI
1097 VariableServiceInitialize (
1098 IN EFI_HANDLE ImageHandle,
1099 IN EFI_SYSTEM_TABLE *SystemTable
1100 )
1101 /*++
1102
1103 Routine Description:
1104 This function does initialization for variable services
1105
1106 Arguments:
1107
1108 ImageHandle - The firmware allocated handle for the EFI image.
1109 SystemTable - A pointer to the EFI System Table.
1110
1111 Returns:
1112
1113 Status code.
1114
1115 EFI_NOT_FOUND - Variable store area not found.
1116 EFI_SUCCESS - Variable services successfully initialized.
1117
1118 --*/
1119 {
1120 EFI_STATUS Status;
1121 EFI_HANDLE NewHandle;
1122 VS_DEV *Dev;
1123 EFI_PEI_HOB_POINTERS GuidHob;
1124 VARIABLE_HEADER *NextVariable;
1125 VARIABLE_STORE_HEADER *VariableStoreHeader;
1126 EFI_FLASH_MAP_FS_ENTRY_DATA *FlashMapEntryData;
1127 EFI_FLASH_SUBAREA_ENTRY VariableStoreEntry;
1128 UINT64 BaseAddress;
1129 UINT64 Length;
1130 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;
1131
1132 Status = gBS->AllocatePool (
1133 EfiRuntimeServicesData,
1134 (UINTN) sizeof (VARIABLE_GLOBAL),
1135 (VOID**) &mGlobal
1136 );
1137 if (EFI_ERROR (Status)) {
1138 return Status;
1139 }
1140
1141 GuidHob.Raw = GetHobList ();
1142 FlashMapEntryData = NULL;
1143 while ((GuidHob.Raw = GetNextGuidHob (&gEfiFlashMapHobGuid, GuidHob.Raw)) != NULL) {
1144 FlashMapEntryData = (EFI_FLASH_MAP_FS_ENTRY_DATA *) GET_GUID_HOB_DATA (GuidHob.Guid);
1145 if (FlashMapEntryData->AreaType == EFI_FLASH_AREA_EFI_VARIABLES) {
1146 break;
1147 }
1148 GuidHob.Raw = GET_NEXT_HOB (GuidHob);
1149 }
1150
1151 if (FlashMapEntryData == NULL) {
1152 DEBUG ((EFI_D_ERROR, "FSVariable: Could not find flash area for variable!\n"));
1153 Status = EFI_NOT_FOUND;
1154 return Status;
1155 }
1156
1157 CopyMem(
1158 (VOID*)&VariableStoreEntry,
1159 (VOID*)&FlashMapEntryData->Entries[0],
1160 sizeof(EFI_FLASH_SUBAREA_ENTRY)
1161 );
1162
1163 //
1164 // Mark the variable storage region of the FLASH as RUNTIME
1165 //
1166 BaseAddress = VariableStoreEntry.Base & (~EFI_PAGE_MASK);
1167 Length = VariableStoreEntry.Length + (VariableStoreEntry.Base - BaseAddress);
1168 Length = (Length + EFI_PAGE_SIZE - 1) & (~EFI_PAGE_MASK);
1169 Status = gDS->GetMemorySpaceDescriptor (BaseAddress, &GcdDescriptor);
1170 if (EFI_ERROR (Status)) {
1171 Status = EFI_UNSUPPORTED;
1172 return Status;
1173 }
1174 Status = gDS->SetMemorySpaceAttributes (
1175 BaseAddress,
1176 Length,
1177 GcdDescriptor.Attributes | EFI_MEMORY_RUNTIME
1178 );
1179 if (EFI_ERROR (Status)) {
1180 Status = EFI_UNSUPPORTED;
1181 return Status;
1182 }
1183
1184 Status = FileStorageConstructor (
1185 &mGlobal->VariableStore[NonVolatile],
1186 &mGlobal->GoVirtualChildEvent[NonVolatile],
1187 VariableStoreEntry.Base,
1188 (UINT32) VariableStoreEntry.Length,
1189 FlashMapEntryData->VolumeId,
1190 FlashMapEntryData->FilePath
1191 );
1192 ASSERT_EFI_ERROR (Status);
1193
1194 //
1195 // Volatile Storage
1196 //
1197 Status = MemStorageConstructor (
1198 &mGlobal->VariableStore[Volatile],
1199 &mGlobal->GoVirtualChildEvent[Volatile],
1200 VOLATILE_VARIABLE_STORE_SIZE
1201 );
1202 ASSERT_EFI_ERROR (Status);
1203
1204 //
1205 // Scratch
1206 //
1207 Status = gBS->AllocatePool (
1208 EfiRuntimeServicesData,
1209 VARIABLE_SCRATCH_SIZE,
1210 &mGlobal->Scratch
1211 );
1212 ASSERT_EFI_ERROR (Status);
1213
1214 //
1215 // 1. NV Storage
1216 //
1217 Dev = DEV_FROM_THIS (mGlobal->VariableStore[NonVolatile]);
1218 VariableStoreHeader = (VARIABLE_STORE_HEADER *) VAR_DATA_PTR (Dev);
1219 if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) {
1220 if (~VariableStoreHeader->Size == 0) {
1221 VariableStoreHeader->Size = (UINT32) VariableStoreEntry.Length;
1222 }
1223 }
1224 //
1225 // Calculate LastVariableOffset
1226 //
1227 NextVariable = (VARIABLE_HEADER *) (VariableStoreHeader + 1);
1228 while (IsValidVariableHeader (NextVariable)) {
1229 NextVariable = GetNextVariablePtr (NextVariable);
1230 }
1231 mGlobal->LastVariableOffset[NonVolatile] = (UINTN) NextVariable - (UINTN) VariableStoreHeader;
1232 mGlobal->VariableBase[NonVolatile] = VariableStoreHeader;
1233
1234 //
1235 // Reclaim if remaining space is too small
1236 //
1237 if ((VariableStoreHeader->Size - mGlobal->LastVariableOffset[NonVolatile]) < VARIABLE_RECLAIM_THRESHOLD) {
1238 Status = Reclaim (NonVolatile, NULL);
1239 if (EFI_ERROR (Status)) {
1240 //
1241 // Reclaim error
1242 // we cannot restore to original state
1243 //
1244 DEBUG ((EFI_D_ERROR, "FSVariable: Recalim error (fetal error) - %r\n", Status));
1245 ASSERT_EFI_ERROR (Status);
1246 }
1247 }
1248
1249 //
1250 // 2. Volatile Storage
1251 //
1252 Dev = DEV_FROM_THIS (mGlobal->VariableStore[Volatile]);
1253 VariableStoreHeader = (VARIABLE_STORE_HEADER *) VAR_DATA_PTR (Dev);
1254 mGlobal->VariableBase[Volatile] = VAR_DATA_PTR (Dev);
1255 mGlobal->LastVariableOffset[Volatile] = sizeof (VARIABLE_STORE_HEADER);
1256 //
1257 // init store_header & body in memory.
1258 //
1259 mGlobal->VariableStore[Volatile]->Erase (mGlobal->VariableStore[Volatile]);
1260 mGlobal->VariableStore[Volatile]->Write (
1261 mGlobal->VariableStore[Volatile],
1262 0,
1263 sizeof (VARIABLE_STORE_HEADER),
1264 &mStoreHeaderTemplate
1265 );
1266
1267
1268 SystemTable->RuntimeServices->GetVariable = GetVariable;
1269 SystemTable->RuntimeServices->GetNextVariableName = GetNextVariableName;
1270 SystemTable->RuntimeServices->SetVariable = SetVariable;
1271
1272 SystemTable->RuntimeServices->QueryVariableInfo = QueryVariableInfo;
1273
1274 //
1275 // Now install the Variable Runtime Architectural Protocol on a new handle
1276 //
1277 NewHandle = NULL;
1278 Status = gBS->InstallMultipleProtocolInterfaces (
1279 &NewHandle,
1280 &gEfiVariableArchProtocolGuid,
1281 NULL,
1282 &gEfiVariableWriteArchProtocolGuid,
1283 NULL,
1284 NULL
1285 );
1286 ASSERT_EFI_ERROR (Status);
1287
1288 return Status;
1289
1290 //Shutdown:
1291 // EfiShutdownRuntimeDriverLib ();
1292 // return Status;
1293 }
1294
1295
1296
1297 VOID
1298 EFIAPI
1299 OnVirtualAddressChangeFsv (
1300 IN EFI_EVENT Event,
1301 IN VOID *Context
1302 )
1303 {
1304 UINTN Index;
1305
1306 for (Index = 0; Index < MaxType; Index++) {
1307 mGlobal->GoVirtualChildEvent[Index] (Event, mGlobal->VariableStore[Index]);
1308 EfiConvertPointer (0, (VOID**) &mGlobal->VariableStore[Index]);
1309 EfiConvertPointer (0, &mGlobal->VariableBase[Index]);
1310 }
1311 EfiConvertPointer (0, &mGlobal->Scratch);
1312 EfiConvertPointer (0, (VOID**) &mGlobal);
1313 }