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