3 Copyright (c) 2014 - 2021, Intel Corporation. All rights reserved.<BR>
4 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include <Protocol/FirmwareVolumeBlock.h>
10 #include <Library/PcdLib.h>
11 #include <Library/DebugLib.h>
12 #include <Library/BaseLib.h>
13 #include <Guid/FirmwareFileSystem2.h>
14 #include <Guid/SystemNvDataGuid.h>
15 #include <Guid/NvVariableInfoGuid.h>
16 #include <Library/HobLib.h>
18 #define FVB_MEDIA_BLOCK_SIZE 0x1000
21 EFI_FIRMWARE_VOLUME_HEADER FvInfo
;
22 EFI_FV_BLOCK_MAP_ENTRY End
[1];
23 } EFI_FVB2_MEDIA_INFO
;
26 // This data structure contains a template of FV header which is used to restore
27 // Fv header if it's corrupted.
29 EFI_FVB2_MEDIA_INFO mFvbMediaInfo
= {
31 { 0, }, // ZeroVector[16]
32 EFI_SYSTEM_NV_DATA_FV_GUID
,
35 0x0004feff, // check PiFirmwareVolume.h for details on EFI_FVB_ATTRIBUTES_2
36 sizeof (EFI_FIRMWARE_VOLUME_HEADER
) + sizeof (EFI_FV_BLOCK_MAP_ENTRY
),
37 0, // CheckSum which will be calucated dynamically.
57 Initialize the variable store
59 @retval EFI_SUCCESS if initialize the store success.
70 UINT32 NvVariableSize
;
71 UINT32 FtwWorkingSize
;
73 EFI_HOB_GUID_TYPE
*GuidHob
;
74 NV_VARIABLE_INFO
*NvVariableInfo
;
77 // Find SPI flash variable hob
79 GuidHob
= GetFirstGuidHob (&gNvVariableInfoGuid
);
80 if (GuidHob
== NULL
) {
85 NvVariableInfo
= (NV_VARIABLE_INFO
*)GET_GUID_HOB_DATA (GuidHob
);
88 // Get variable region base and size.
90 NvStorageSize
= NvVariableInfo
->VariableStoreSize
;
91 NvStorageBase
= NvVariableInfo
->VariableStoreBase
;
94 // NvStorageBase needs to be 4KB aligned, NvStorageSize needs to be 8KB * n
96 if (((NvStorageBase
& (SIZE_4KB
- 1)) != 0) || ((NvStorageSize
& (SIZE_8KB
- 1)) != 0)) {
97 return EFI_INVALID_PARAMETER
;
100 FtwSpareSize
= NvStorageSize
/ 2;
101 FtwWorkingSize
= 0x2000;
102 NvVariableSize
= NvStorageSize
/ 2 - FtwWorkingSize
;
103 DEBUG ((DEBUG_INFO
, "NvStorageBase:0x%x, NvStorageSize:0x%x\n", NvStorageBase
, NvStorageSize
));
105 if (NvVariableSize
>= 0x80000000) {
106 return EFI_INVALID_PARAMETER
;
109 Status
= PcdSet32S (PcdFlashNvStorageVariableSize
, NvVariableSize
);
110 ASSERT_EFI_ERROR (Status
);
111 Status
= PcdSet32S (PcdFlashNvStorageVariableBase
, NvStorageBase
);
112 ASSERT_EFI_ERROR (Status
);
113 Status
= PcdSet64S (PcdFlashNvStorageVariableBase64
, NvStorageBase
);
114 ASSERT_EFI_ERROR (Status
);
116 Status
= PcdSet32S (PcdFlashNvStorageFtwWorkingSize
, FtwWorkingSize
);
117 ASSERT_EFI_ERROR (Status
);
118 Status
= PcdSet32S (PcdFlashNvStorageFtwWorkingBase
, NvStorageBase
+ NvVariableSize
);
119 ASSERT_EFI_ERROR (Status
);
121 Status
= PcdSet32S (PcdFlashNvStorageFtwSpareSize
, FtwSpareSize
);
122 ASSERT_EFI_ERROR (Status
);
123 Status
= PcdSet32S (PcdFlashNvStorageFtwSpareBase
, NvStorageBase
+ FtwSpareSize
);
124 ASSERT_EFI_ERROR (Status
);
130 Get a heathy FV header used for variable store recovery
132 @retval The FV header.
135 EFI_FIRMWARE_VOLUME_HEADER
*
136 GetFvHeaderTemplate (
140 EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
;
143 FvSize
= PcdGet32 (PcdFlashNvStorageFtwSpareSize
) * 2;
144 FvHeader
= &mFvbMediaInfo
.FvInfo
;
145 FvHeader
->FvLength
= FvSize
;
146 FvHeader
->BlockMap
[0].NumBlocks
= (UINT32
)(FvSize
/ FvHeader
->BlockMap
[0].Length
);
147 FvHeader
->Checksum
= 0;
148 FvHeader
->Checksum
= CalculateCheckSum16 ((UINT16
*)FvHeader
, FvHeader
->HeaderLength
);