]> git.proxmox.com Git - mirror_edk2.git/blob - UefiPayloadPkg/FvbRuntimeDxe/FvbInfo.c
UefiPayloadPkg: Apply uncrustify changes
[mirror_edk2.git] / UefiPayloadPkg / FvbRuntimeDxe / FvbInfo.c
1 /** @file
2
3 Copyright (c) 2014 - 2021, Intel Corporation. All rights reserved.<BR>
4 SPDX-License-Identifier: BSD-2-Clause-Patent
5
6 **/
7
8 #include <PiDxe.h>
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>
17
18 #define FVB_MEDIA_BLOCK_SIZE 0x1000
19
20 typedef struct {
21 EFI_FIRMWARE_VOLUME_HEADER FvInfo;
22 EFI_FV_BLOCK_MAP_ENTRY End[1];
23 } EFI_FVB2_MEDIA_INFO;
24
25 //
26 // This data structure contains a template of FV header which is used to restore
27 // Fv header if it's corrupted.
28 //
29 EFI_FVB2_MEDIA_INFO mFvbMediaInfo = {
30 {
31 { 0, }, // ZeroVector[16]
32 EFI_SYSTEM_NV_DATA_FV_GUID,
33 0,
34 EFI_FVH_SIGNATURE,
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.
38 0, // ExtHeaderOffset
39 { 0, },
40 EFI_FVH_REVISION,
41 {
42 {
43 0,
44 FVB_MEDIA_BLOCK_SIZE,
45 }
46 }
47 },
48 {
49 {
50 0,
51 0
52 }
53 }
54 };
55
56 /**
57 Initialize the variable store
58
59 @retval EFI_SUCCESS if initialize the store success.
60
61 **/
62 EFI_STATUS
63 InitVariableStore (
64 VOID
65 )
66 {
67 EFI_STATUS Status;
68 UINT32 NvStorageBase;
69 UINT32 NvStorageSize;
70 UINT32 NvVariableSize;
71 UINT32 FtwWorkingSize;
72 UINT32 FtwSpareSize;
73 EFI_HOB_GUID_TYPE *GuidHob;
74 NV_VARIABLE_INFO *NvVariableInfo;
75
76 //
77 // Find SPI flash variable hob
78 //
79 GuidHob = GetFirstGuidHob (&gNvVariableInfoGuid);
80 if (GuidHob == NULL) {
81 ASSERT (FALSE);
82 return EFI_NOT_FOUND;
83 }
84
85 NvVariableInfo = (NV_VARIABLE_INFO *)GET_GUID_HOB_DATA (GuidHob);
86
87 //
88 // Get variable region base and size.
89 //
90 NvStorageSize = NvVariableInfo->VariableStoreSize;
91 NvStorageBase = NvVariableInfo->VariableStoreBase;
92
93 //
94 // NvStorageBase needs to be 4KB aligned, NvStorageSize needs to be 8KB * n
95 //
96 if (((NvStorageBase & (SIZE_4KB - 1)) != 0) || ((NvStorageSize & (SIZE_8KB - 1)) != 0)) {
97 return EFI_INVALID_PARAMETER;
98 }
99
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));
104
105 if (NvVariableSize >= 0x80000000) {
106 return EFI_INVALID_PARAMETER;
107 }
108
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);
115
116 Status = PcdSet32S (PcdFlashNvStorageFtwWorkingSize, FtwWorkingSize);
117 ASSERT_EFI_ERROR (Status);
118 Status = PcdSet32S (PcdFlashNvStorageFtwWorkingBase, NvStorageBase + NvVariableSize);
119 ASSERT_EFI_ERROR (Status);
120
121 Status = PcdSet32S (PcdFlashNvStorageFtwSpareSize, FtwSpareSize);
122 ASSERT_EFI_ERROR (Status);
123 Status = PcdSet32S (PcdFlashNvStorageFtwSpareBase, NvStorageBase + FtwSpareSize);
124 ASSERT_EFI_ERROR (Status);
125
126 return EFI_SUCCESS;
127 }
128
129 /**
130 Get a heathy FV header used for variable store recovery
131
132 @retval The FV header.
133
134 **/
135 EFI_FIRMWARE_VOLUME_HEADER *
136 GetFvHeaderTemplate (
137 VOID
138 )
139 {
140 EFI_FIRMWARE_VOLUME_HEADER *FvHeader;
141 UINTN FvSize;
142
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);
149
150 return FvHeader;
151 }