]> git.proxmox.com Git - mirror_edk2.git/blob - InOsEmuPkg/Unix/Sec/FwVol.c
Add InOsEmuPkg. Like UnixPkg and Nt32Pkg, but EFI code can be common and does not...
[mirror_edk2.git] / InOsEmuPkg / Unix / Sec / 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 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "SecMain.h"
17
18 #define GET_OCCUPIED_SIZE(ActualSize, Alignment) \
19 (ActualSize) + (((Alignment) - ((ActualSize) & ((Alignment) - 1))) & ((Alignment) - 1))
20
21 EFI_FFS_FILE_STATE
22 GetFileState (
23 IN UINT8 ErasePolarity,
24 IN EFI_FFS_FILE_HEADER *FfsHeader
25 )
26 /*++
27
28 Routine Description:
29 Returns the highest bit set of the State field
30
31 Arguments:
32 ErasePolarity - Erase Polarity as defined by EFI_FVB2_ERASE_POLARITY
33 in the Attributes field.
34 FfsHeader - Pointer to FFS File Header.
35
36 Returns:
37 Returns the highest bit in the State field
38
39 **/
40 {
41 EFI_FFS_FILE_STATE FileState;
42 EFI_FFS_FILE_STATE HighestBit;
43
44 FileState = FfsHeader->State;
45
46 if (ErasePolarity != 0) {
47 FileState = (EFI_FFS_FILE_STATE)~FileState;
48 }
49
50 HighestBit = 0x80;
51 while (HighestBit != 0 && (HighestBit & FileState) == 0) {
52 HighestBit >>= 1;
53 }
54
55 return HighestBit;
56 }
57
58 UINT8
59 CalculateHeaderChecksum (
60 IN EFI_FFS_FILE_HEADER *FileHeader
61 )
62 /*++
63
64 Routine Description:
65 Calculates the checksum of the header of a file.
66
67 Arguments:
68 FileHeader - Pointer to FFS File Header.
69
70 Returns:
71 Checksum of the header.
72
73 **/
74 {
75 UINT8 *ptr;
76 UINTN Index;
77 UINT8 Sum;
78
79 Sum = 0;
80 ptr = (UINT8 *) FileHeader;
81
82 for (Index = 0; Index < sizeof (EFI_FFS_FILE_HEADER) - 3; Index += 4) {
83 Sum = (UINT8) (Sum + ptr[Index]);
84 Sum = (UINT8) (Sum + ptr[Index + 1]);
85 Sum = (UINT8) (Sum + ptr[Index + 2]);
86 Sum = (UINT8) (Sum + ptr[Index + 3]);
87 }
88
89 for (; Index < sizeof (EFI_FFS_FILE_HEADER); Index++) {
90 Sum = (UINT8) (Sum + ptr[Index]);
91 }
92 //
93 // State field (since this indicates the different state of file).
94 //
95 Sum = (UINT8) (Sum - FileHeader->State);
96 //
97 // Checksum field of the file is not part of the header checksum.
98 //
99 Sum = (UINT8) (Sum - FileHeader->IntegrityCheck.Checksum.File);
100
101 return Sum;
102 }
103
104 EFI_STATUS
105 SecFfsFindNextFile (
106 IN EFI_FV_FILETYPE SearchType,
107 IN EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader,
108 IN OUT EFI_FFS_FILE_HEADER **FileHeader
109 )
110 /*++
111
112 Routine Description:
113 Given the input file pointer, search for the next matching file in the
114 FFS volume as defined by SearchType. The search starts from FileHeader inside
115 the Firmware Volume defined by FwVolHeader.
116
117 Arguments:
118 SearchType - Filter to find only files of this type.
119 Type EFI_FV_FILETYPE_ALL causes no filtering to be done.
120 FwVolHeader - Pointer to the FV header of the volume to search.
121 This parameter must point to a valid FFS volume.
122 FileHeader - Pointer to the current file from which to begin searching.
123 This pointer will be updated upon return to reflect the file
124 found.
125
126 Returns:
127 EFI_NOT_FOUND - No files matching the search criteria were found
128 EFI_SUCCESS
129
130 **/
131 {
132 EFI_FFS_FILE_HEADER *FfsFileHeader;
133 UINT32 FileLength;
134 UINT32 FileOccupiedSize;
135 UINT32 FileOffset;
136 UINT64 FvLength;
137 UINT8 ErasePolarity;
138 UINT8 FileState;
139
140 FvLength = FwVolHeader->FvLength;
141 if (FwVolHeader->Attributes & EFI_FVB2_ERASE_POLARITY) {
142 ErasePolarity = 1;
143 } else {
144 ErasePolarity = 0;
145 }
146 //
147 // If FileHeader is not specified (NULL) start with the first file in the
148 // firmware volume. Otherwise, start from the FileHeader.
149 //
150 if (*FileHeader == NULL) {
151 FfsFileHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FwVolHeader + FwVolHeader->HeaderLength);
152 } else {
153 //
154 // Length is 24 bits wide so mask upper 8 bits
155 // FileLength is adjusted to FileOccupiedSize as it is 8 byte aligned.
156 //
157 FileLength = *(UINT32 *) (*FileHeader)->Size & 0x00FFFFFF;
158 FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);
159 FfsFileHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) *FileHeader + FileOccupiedSize);
160 }
161
162 FileOffset = (UINT32) ((UINT8 *) FfsFileHeader - (UINT8 *) FwVolHeader);
163
164 while (FileOffset < (FvLength - sizeof (EFI_FFS_FILE_HEADER))) {
165 //
166 // Get FileState which is the highest bit of the State
167 //
168 FileState = GetFileState (ErasePolarity, FfsFileHeader);
169
170 switch (FileState) {
171
172 case EFI_FILE_HEADER_INVALID:
173 FileOffset += sizeof (EFI_FFS_FILE_HEADER);
174 FfsFileHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER));
175 break;
176
177 case EFI_FILE_DATA_VALID:
178 case EFI_FILE_MARKED_FOR_UPDATE:
179 if (CalculateHeaderChecksum (FfsFileHeader) == 0) {
180 FileLength = *(UINT32 *) (FfsFileHeader->Size) & 0x00FFFFFF;
181 FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);
182
183 if ((SearchType == FfsFileHeader->Type) || (SearchType == EFI_FV_FILETYPE_ALL)) {
184
185 *FileHeader = FfsFileHeader;
186
187 return EFI_SUCCESS;
188 }
189
190 FileOffset += FileOccupiedSize;
191 FfsFileHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FfsFileHeader + FileOccupiedSize);
192 } else {
193 return EFI_NOT_FOUND;
194 }
195 break;
196
197 case EFI_FILE_DELETED:
198 FileLength = *(UINT32 *) (FfsFileHeader->Size) & 0x00FFFFFF;
199 FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);
200 FileOffset += FileOccupiedSize;
201 FfsFileHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FfsFileHeader + FileOccupiedSize);
202 break;
203
204 default:
205 return EFI_NOT_FOUND;
206
207 }
208 }
209
210 return EFI_NOT_FOUND;
211 }
212
213 EFI_STATUS
214 SecFfsFindSectionData (
215 IN EFI_SECTION_TYPE SectionType,
216 IN EFI_FFS_FILE_HEADER *FfsFileHeader,
217 IN OUT VOID **SectionData
218 )
219 /*++
220
221 Routine Description:
222 Given the input file pointer, search for the next matching section in the
223 FFS volume.
224
225 Arguments:
226 SearchType - Filter to find only sections of this type.
227 FfsFileHeader - Pointer to the current file to search.
228 SectionData - Pointer to the Section matching SectionType in FfsFileHeader.
229 NULL if section not found
230
231 Returns:
232 EFI_NOT_FOUND - No files matching the search criteria were found
233 EFI_SUCCESS
234
235 **/
236 {
237 UINT32 FileSize;
238 EFI_COMMON_SECTION_HEADER *Section;
239 UINT32 SectionLength;
240 UINT32 ParsedLength;
241
242 //
243 // Size is 24 bits wide so mask upper 8 bits.
244 // Does not include FfsFileHeader header size
245 // FileSize is adjusted to FileOccupiedSize as it is 8 byte aligned.
246 //
247 Section = (EFI_COMMON_SECTION_HEADER *) (FfsFileHeader + 1);
248 FileSize = *(UINT32 *) (FfsFileHeader->Size) & 0x00FFFFFF;
249 FileSize -= sizeof (EFI_FFS_FILE_HEADER);
250
251 *SectionData = NULL;
252 ParsedLength = 0;
253 while (ParsedLength < FileSize) {
254 if (Section->Type == SectionType) {
255 *SectionData = (VOID *) (Section + 1);
256 return EFI_SUCCESS;
257 }
258 //
259 // Size is 24 bits wide so mask upper 8 bits.
260 // SectionLength is adjusted it is 4 byte aligned.
261 // Go to the next section
262 //
263 SectionLength = *(UINT32 *) Section->Size & 0x00FFFFFF;
264 SectionLength = GET_OCCUPIED_SIZE (SectionLength, 4);
265
266 ParsedLength += SectionLength;
267 Section = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) Section + SectionLength);
268 }
269
270 return EFI_NOT_FOUND;
271 }
272
273 EFI_STATUS
274 SecFfsFindPeiCore (
275 IN EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader,
276 OUT VOID **Pe32Data
277 )
278 /*++
279
280 Routine Description:
281 Given the pointer to the Firmware Volume Header find the SEC
282 core and return it's PE32 image.
283
284 Arguments:
285 FwVolHeader - Pointer to memory mapped FV
286 Pe32Data - Pointer to SEC PE32 iamge.
287
288 Returns:
289 EFI_SUCCESS - Pe32Data is valid
290 other - Failure
291
292 **/
293 {
294 EFI_STATUS Status;
295 EFI_FFS_FILE_HEADER *FileHeader;
296 EFI_FV_FILETYPE SearchType;
297
298 SearchType = EFI_FV_FILETYPE_PEI_CORE;
299 FileHeader = NULL;
300 do {
301 Status = SecFfsFindNextFile (SearchType, FwVolHeader, &FileHeader);
302 if (!EFI_ERROR (Status)) {
303 Status = SecFfsFindSectionData (EFI_SECTION_PE32, FileHeader, Pe32Data);
304 return Status;
305 }
306 } while (!EFI_ERROR (Status));
307
308 return Status;
309 }