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