]> git.proxmox.com Git - mirror_edk2.git/blob - EmulatorPkg/Library/SecPeiServicesLib/FwVol.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / EmulatorPkg / Library / SecPeiServicesLib / FwVol.c
1 /*++ @file
2 A simple FV stack so the SEC can extract the SEC Core from an
3 FV.
4
5 Copyright (c) 2006, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #include <PiPei.h>
11
12 #define GET_OCCUPIED_SIZE(ActualSize, Alignment) \
13 (ActualSize) + (((Alignment) - ((ActualSize) & ((Alignment) - 1))) & ((Alignment) - 1))
14
15 EFI_FFS_FILE_STATE
16 GetFileState (
17 IN UINT8 ErasePolarity,
18 IN EFI_FFS_FILE_HEADER *FfsHeader
19 )
20
21 /*++
22
23 Routine Description:
24 Returns the highest bit set of the State field
25
26 Arguments:
27 ErasePolarity - Erase Polarity as defined by EFI_FVB2_ERASE_POLARITY
28 in the Attributes field.
29 FfsHeader - Pointer to FFS File Header.
30
31 Returns:
32 Returns the highest bit in the State field
33
34 **/
35 {
36 EFI_FFS_FILE_STATE FileState;
37 EFI_FFS_FILE_STATE HighestBit;
38
39 FileState = FfsHeader->State;
40
41 if (ErasePolarity != 0) {
42 FileState = (EFI_FFS_FILE_STATE) ~FileState;
43 }
44
45 HighestBit = 0x80;
46 while (HighestBit != 0 && (HighestBit & FileState) == 0) {
47 HighestBit >>= 1;
48 }
49
50 return HighestBit;
51 }
52
53 UINT8
54 CalculateHeaderChecksum (
55 IN EFI_FFS_FILE_HEADER *FileHeader
56 )
57
58 /*++
59
60 Routine Description:
61 Calculates the checksum of the header of a file.
62
63 Arguments:
64 FileHeader - Pointer to FFS File Header.
65
66 Returns:
67 Checksum of the header.
68
69 **/
70 {
71 UINT8 *ptr;
72 UINTN Index;
73 UINT8 Sum;
74
75 Sum = 0;
76 ptr = (UINT8 *)FileHeader;
77
78 for (Index = 0; Index < sizeof (EFI_FFS_FILE_HEADER) - 3; Index += 4) {
79 Sum = (UINT8)(Sum + ptr[Index]);
80 Sum = (UINT8)(Sum + ptr[Index + 1]);
81 Sum = (UINT8)(Sum + ptr[Index + 2]);
82 Sum = (UINT8)(Sum + ptr[Index + 3]);
83 }
84
85 for ( ; Index < sizeof (EFI_FFS_FILE_HEADER); Index++) {
86 Sum = (UINT8)(Sum + ptr[Index]);
87 }
88
89 //
90 // State field (since this indicates the different state of file).
91 //
92 Sum = (UINT8)(Sum - FileHeader->State);
93 //
94 // Checksum field of the file is not part of the header checksum.
95 //
96 Sum = (UINT8)(Sum - FileHeader->IntegrityCheck.Checksum.File);
97
98 return Sum;
99 }
100
101 EFI_STATUS
102 SecFfsFindNextFile (
103 IN EFI_FV_FILETYPE SearchType,
104 IN EFI_PEI_FV_HANDLE FvHandle,
105 IN OUT EFI_PEI_FILE_HANDLE *FileHandle
106 )
107
108 /*++
109
110 Routine Description:
111 Given the input file pointer, search for the next matching file in the
112 FFS volume as defined by SearchType. The search starts from FileHeader inside
113 the Firmware Volume defined by FwVolHeader.
114
115 Arguments:
116 SearchType - Filter to find only files of this type.
117 Type EFI_FV_FILETYPE_ALL causes no filtering to be done.
118 FwVolHeader - Pointer to the FV header of the volume to search.
119 This parameter must point to a valid FFS volume.
120 FileHeader - Pointer to the current file from which to begin searching.
121 This pointer will be updated upon return to reflect the file
122 found.
123
124 Returns:
125 EFI_NOT_FOUND - No files matching the search criteria were found
126 EFI_SUCCESS
127
128 **/
129 {
130 EFI_FFS_FILE_HEADER *FfsFileHeader;
131 UINT32 FileLength;
132 UINT32 FileOccupiedSize;
133 UINT32 FileOffset;
134 UINT64 FvLength;
135 UINT8 ErasePolarity;
136 UINT8 FileState;
137 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
138 EFI_FFS_FILE_HEADER **FileHeader;
139
140 //
141 // Convert the handle of FV to FV header for memory-mapped firmware volume
142 //
143 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *)FvHandle;
144 FileHeader = (EFI_FFS_FILE_HEADER **)FileHandle;
145
146 FvLength = FwVolHeader->FvLength;
147 if (FwVolHeader->Attributes & EFI_FVB2_ERASE_POLARITY) {
148 ErasePolarity = 1;
149 } else {
150 ErasePolarity = 0;
151 }
152
153 //
154 // If FileHeader is not specified (NULL) start with the first file in the
155 // firmware volume. Otherwise, start from the FileHeader.
156 //
157 if (*FileHeader == NULL) {
158 FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FwVolHeader + FwVolHeader->HeaderLength);
159 } else {
160 //
161 // Length is 24 bits wide so mask upper 8 bits
162 // FileLength is adjusted to FileOccupiedSize as it is 8 byte aligned.
163 //
164 FileLength = *(UINT32 *)(*FileHeader)->Size & 0x00FFFFFF;
165 FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);
166 FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)*FileHeader + FileOccupiedSize);
167 }
168
169 FileOffset = (UINT32)((UINT8 *)FfsFileHeader - (UINT8 *)FwVolHeader);
170
171 while (FileOffset < (FvLength - sizeof (EFI_FFS_FILE_HEADER))) {
172 //
173 // Get FileState which is the highest bit of the State
174 //
175 FileState = GetFileState (ErasePolarity, FfsFileHeader);
176
177 switch (FileState) {
178 case EFI_FILE_HEADER_INVALID:
179 FileOffset += sizeof (EFI_FFS_FILE_HEADER);
180 FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER));
181 break;
182
183 case EFI_FILE_DATA_VALID:
184 case EFI_FILE_MARKED_FOR_UPDATE:
185 if (CalculateHeaderChecksum (FfsFileHeader) == 0) {
186 FileLength = *(UINT32 *)(FfsFileHeader->Size) & 0x00FFFFFF;
187 FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);
188
189 if ((SearchType == FfsFileHeader->Type) || (SearchType == EFI_FV_FILETYPE_ALL)) {
190 *FileHeader = FfsFileHeader;
191
192 return EFI_SUCCESS;
193 }
194
195 FileOffset += FileOccupiedSize;
196 FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + FileOccupiedSize);
197 } else {
198 return EFI_NOT_FOUND;
199 }
200
201 break;
202
203 case EFI_FILE_DELETED:
204 FileLength = *(UINT32 *)(FfsFileHeader->Size) & 0x00FFFFFF;
205 FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);
206 FileOffset += FileOccupiedSize;
207 FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + FileOccupiedSize);
208 break;
209
210 default:
211 return EFI_NOT_FOUND;
212 }
213 }
214
215 return EFI_NOT_FOUND;
216 }
217
218 EFI_STATUS
219 SecFfsFindSectionData (
220 IN EFI_SECTION_TYPE SectionType,
221 IN EFI_FFS_FILE_HEADER *FfsFileHeader,
222 IN OUT VOID **SectionData
223 )
224
225 /*++
226
227 Routine Description:
228 Given the input file pointer, search for the next matching section in the
229 FFS volume.
230
231 Arguments:
232 SearchType - Filter to find only sections of this type.
233 FfsFileHeader - Pointer to the current file to search.
234 SectionData - Pointer to the Section matching SectionType in FfsFileHeader.
235 NULL if section not found
236
237 Returns:
238 EFI_NOT_FOUND - No files matching the search criteria were found
239 EFI_SUCCESS
240
241 **/
242 {
243 UINT32 FileSize;
244 EFI_COMMON_SECTION_HEADER *Section;
245 UINT32 SectionLength;
246 UINT32 ParsedLength;
247
248 //
249 // Size is 24 bits wide so mask upper 8 bits.
250 // Does not include FfsFileHeader header size
251 // FileSize is adjusted to FileOccupiedSize as it is 8 byte aligned.
252 //
253 Section = (EFI_COMMON_SECTION_HEADER *)(FfsFileHeader + 1);
254 FileSize = *(UINT32 *)(FfsFileHeader->Size) & 0x00FFFFFF;
255 FileSize -= sizeof (EFI_FFS_FILE_HEADER);
256
257 *SectionData = NULL;
258 ParsedLength = 0;
259 while (ParsedLength < FileSize) {
260 if (Section->Type == SectionType) {
261 *SectionData = (VOID *)(Section + 1);
262 return EFI_SUCCESS;
263 }
264
265 //
266 // Size is 24 bits wide so mask upper 8 bits.
267 // SectionLength is adjusted it is 4 byte aligned.
268 // Go to the next section
269 //
270 SectionLength = *(UINT32 *)Section->Size & 0x00FFFFFF;
271 SectionLength = GET_OCCUPIED_SIZE (SectionLength, 4);
272
273 ParsedLength += SectionLength;
274 Section = (EFI_COMMON_SECTION_HEADER *)((UINT8 *)Section + SectionLength);
275 }
276
277 return EFI_NOT_FOUND;
278 }