1 /* ffs.h
2 
3 Copyright (c) 2016, Nikolaj Schlej. All rights reserved.
4 This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution.  The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8 
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHWARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11 */
12 
13 #ifndef FFS_H
14 #define FFS_H
15 
16 #include <vector>
17 
18 #include "basetypes.h"
19 #include "ubytearray.h"
20 #include "ustring.h"
21 
22 // Make sure we use right packing rules
23 #pragma pack(push,1)
24 
25 extern UString guidToUString(const EFI_GUID& guid, bool convertToString = true);
26 extern bool ustringToGuid(const UString& str, EFI_GUID& guid);
27 extern UString fileTypeToUString(const UINT8 type);
28 extern UString sectionTypeToUString(const UINT8 type);
29 extern UString bpdtEntryTypeToUString(const UINT16 type);
30 extern UString cpdExtensionTypeToUstring(const UINT32 type);
31 //*****************************************************************************
32 // EFI Capsule
33 //*****************************************************************************
34 // Capsule header
35 typedef struct EFI_CAPSULE_HEADER_ {
36     EFI_GUID  CapsuleGuid;
37     UINT32    HeaderSize;
38     UINT32    Flags;
39     UINT32    CapsuleImageSize;
40 } EFI_CAPSULE_HEADER;
41 
42 // Capsule flags
43 #define EFI_CAPSULE_HEADER_FLAG_SETUP                   0x00000001
44 #define EFI_CAPSULE_HEADER_FLAG_PERSIST_ACROSS_RESET    0x00010000
45 #define EFI_CAPSULE_HEADER_FLAG_POPULATE_SYSTEM_TABLE   0x00020000
46 
47 // Standard FMP capsule GUID
48 const UByteArray EFI_FMP_CAPSULE_GUID // 6DCBD5ED-E82D-4C44-BDA1-7194199AD92A
49 ("\xED\xD5\xCB\x6D\x2D\xE8\x44\x4C\xBD\xA1\x71\x94\x19\x9A\xD9\x2A", 16);
50 
51 // Standard EFI capsule GUID
52 const UByteArray EFI_CAPSULE_GUID
53 ("\xBD\x86\x66\x3B\x76\x0D\x30\x40\xB7\x0E\xB5\x51\x9E\x2F\xC5\xA0", 16);
54 
55 // Intel capsule GUID
56 const UByteArray INTEL_CAPSULE_GUID
57 ("\xB9\x82\x91\x53\xB5\xAB\x91\x43\xB6\x9A\xE3\xA9\x43\xF7\x2F\xCC", 16);
58 
59 // Lenovo capsule GUID
60 const UByteArray LENOVO_CAPSULE_GUID
61 ("\xD3\xAF\x0B\xE2\x14\x99\x4F\x4F\x95\x37\x31\x29\xE0\x90\xEB\x3C", 16);
62 
63 // Another Lenovo capsule GUID
64 const UByteArray LENOVO2_CAPSULE_GUID
65 ("\x76\xFE\xB5\x25\x43\x82\x5C\x4A\xA9\xBD\x7E\xE3\x24\x61\x98\xB5", 16);
66 
67 // Toshiba EFI Capsule header
68 typedef struct TOSHIBA_CAPSULE_HEADER_ {
69     EFI_GUID  CapsuleGuid;
70     UINT32    HeaderSize;
71     UINT32    FullSize;
72     UINT32    Flags;
73 } TOSHIBA_CAPSULE_HEADER;
74 
75 // Toshiba capsule GUID
76 const UByteArray TOSHIBA_CAPSULE_GUID
77 ("\x62\x70\xE0\x3B\x51\x1D\xD2\x45\x83\x2B\xF0\x93\x25\x7E\xD4\x61", 16);
78 
79 // AMI Aptio extended capsule header
80 typedef struct APTIO_CAPSULE_HEADER_ {
81     EFI_CAPSULE_HEADER    CapsuleHeader;
82     UINT16                RomImageOffset;  // offset in bytes from the beginning of the capsule header to the start of the capsule volume
83     UINT16                RomLayoutOffset; // offset to the table of the module descriptors in the capsule's volume that are included in the signature calculation
84     //FW_CERTIFICATE      FWCert;
85     //ROM_AREA            RomAreaMap[1];
86 } APTIO_CAPSULE_HEADER;
87 
88 // AMI Aptio signed extended capsule GUID
89 const UByteArray APTIO_SIGNED_CAPSULE_GUID
90 ("\x8B\xA6\x3C\x4A\x23\x77\xFB\x48\x80\x3D\x57\x8C\xC1\xFE\xC4\x4D", 16);
91 
92 // AMI Aptio unsigned extended capsule GUID
93 const UByteArray APTIO_UNSIGNED_CAPSULE_GUID
94 ("\x90\xBB\xEE\x14\x0A\x89\xDB\x43\xAE\xD1\x5D\x3C\x45\x88\xA4\x18", 16);
95 
96 //*****************************************************************************
97 // EFI Firmware Volume
98 //*****************************************************************************
99 // Firmware block map entry
100 // FvBlockMap ends with an entry {0x00000000, 0x00000000}
101 typedef struct EFI_FV_BLOCK_MAP_ENTRY_ {
102     UINT32  NumBlocks;
103     UINT32  Length;
104 } EFI_FV_BLOCK_MAP_ENTRY;
105 
106 // Volume header
107 typedef struct EFI_FIRMWARE_VOLUME_HEADER_ {
108     UINT8                  ZeroVector[16];
109     EFI_GUID               FileSystemGuid;
110     UINT64                 FvLength;
111     UINT32                 Signature;
112     UINT32                 Attributes;
113     UINT16                 HeaderLength;
114     UINT16                 Checksum;
115     UINT16                 ExtHeaderOffset;  //Reserved in Revision 1
116     UINT8                  Reserved;
117     UINT8                  Revision;
118     //EFI_FV_BLOCK_MAP_ENTRY FvBlockMap[2];
119 } EFI_FIRMWARE_VOLUME_HEADER;
120 
121 // Standard file system GUIDs
122 const UByteArray EFI_FIRMWARE_FILE_SYSTEM_GUID // 7A9354D9-0468-444A-81CE-0BF617D890DF
123 ("\xD9\x54\x93\x7A\x68\x04\x4A\x44\x81\xCE\x0B\xF6\x17\xD8\x90\xDF", 16);
124 
125 const UByteArray EFI_FIRMWARE_FILE_SYSTEM2_GUID // 8C8CE578-8A3D-4F1C-9935-896185C32DD3
126 ("\x78\xE5\x8C\x8C\x3D\x8A\x1C\x4F\x99\x35\x89\x61\x85\xC3\x2D\xD3", 16);
127 
128 const UByteArray EFI_FIRMWARE_FILE_SYSTEM3_GUID // 5473C07A-3DCB-4DCA-BD6F-1E9689E7349A
129 ("\x7A\xC0\x73\x54\xCB\x3D\xCA\x4D\xBD\x6F\x1E\x96\x89\xE7\x34\x9A", 16);
130 
131 // Vendor-specific file system GUIDs
132 const UByteArray EFI_APPLE_IMMUTABLE_FV_GUID // 04ADEEAD-61FF-4D31-B6BA-64F8BF901F5A
133 ("\xAD\xEE\xAD\x04\xFF\x61\x31\x4D\xB6\xBA\x64\xF8\xBF\x90\x1F\x5A", 16);
134 
135 const UByteArray EFI_APPLE_AUTHENTICATION_FV_GUID // BD001B8C-6A71-487B-A14F-0C2A2DCF7A5D
136 ("\x8C\x1B\x00\xBD\x71\x6A\x7B\x48\xA1\x4F\x0C\x2A\x2D\xCF\x7A\x5D", 16);
137 
138 const UByteArray EFI_APPLE_MICROCODE_VOLUME_GUID // 153D2197-29BD-44DC-AC59-887F70E41A6B
139 ("\x97\x21\x3D\x15\xBD\x29\xDC\x44\xAC\x59\x88\x7F\x70\xE4\x1A\x6B", 16);
140 #define EFI_APPLE_MICROCODE_VOLUME_HEADER_SIZE 0x100
141 
142 const UByteArray EFI_INTEL_FILE_SYSTEM_GUID // AD3FFFFF-D28B-44C4-9F13-9EA98A97F9F0
143 ("\xFF\xFF\x3F\xAD\x8B\xD2\xC4\x44\x9F\x13\x9E\xA9\x8A\x97\xF9\xF0", 16);
144 
145 const UByteArray EFI_INTEL_FILE_SYSTEM2_GUID // D6A1CD70-4B33-4994-A6EA-375F2CCC5437
146 ("\x70\xCD\xA1\xD6\x33\x4B\x94\x49\xA6\xEA\x37\x5F\x2C\xCC\x54\x37", 16);
147 
148 const UByteArray EFI_SONY_FILE_SYSTEM_GUID // 4F494156-AED6-4D64-A537-B8A5557BCEEC
149 ("\x56\x41\x49\x4F\xD6\xAE\x64\x4D\xA5\x37\xB8\xA5\x55\x7B\xCE\xEC", 16);
150 
151 // Vector of volume GUIDs with FFSv2-compatible files
152 extern const std::vector<UByteArray> FFSv2Volumes;
153 
154 // Vector of volume GUIDs with FFSv3-compatible files
155 extern const std::vector<UByteArray> FFSv3Volumes;
156 
157 // Firmware volume signature
158 #define EFI_FV_SIGNATURE 0x4856465F // _FVH
159 #define EFI_FV_SIGNATURE_OFFSET 0x28
160 
161 // Firmware volume attributes
162 // Revision 1
163 #define EFI_FVB_READ_DISABLED_CAP   0x00000001
164 #define EFI_FVB_READ_ENABLED_CAP    0x00000002
165 #define EFI_FVB_READ_STATUS         0x00000004
166 #define EFI_FVB_WRITE_DISABLED_CAP  0x00000008
167 #define EFI_FVB_WRITE_ENABLED_CAP   0x00000010
168 #define EFI_FVB_WRITE_STATUS        0x00000020
169 #define EFI_FVB_LOCK_CAP            0x00000040
170 #define EFI_FVB_LOCK_STATUS         0x00000080
171 #define EFI_FVB_STICKY_WRITE        0x00000200
172 #define EFI_FVB_MEMORY_MAPPED       0x00000400
173 #define EFI_FVB_ERASE_POLARITY      0x00000800
174 #define EFI_FVB_ALIGNMENT_CAP       0x00008000
175 #define EFI_FVB_ALIGNMENT_2         0x00010000
176 #define EFI_FVB_ALIGNMENT_4         0x00020000
177 #define EFI_FVB_ALIGNMENT_8         0x00040000
178 #define EFI_FVB_ALIGNMENT_16        0x00080000
179 #define EFI_FVB_ALIGNMENT_32        0x00100000
180 #define EFI_FVB_ALIGNMENT_64        0x00200000
181 #define EFI_FVB_ALIGNMENT_128       0x00400000
182 #define EFI_FVB_ALIGNMENT_256       0x00800000
183 #define EFI_FVB_ALIGNMENT_512       0x01000000
184 #define EFI_FVB_ALIGNMENT_1K        0x02000000
185 #define EFI_FVB_ALIGNMENT_2K        0x04000000
186 #define EFI_FVB_ALIGNMENT_4K        0x08000000
187 #define EFI_FVB_ALIGNMENT_8K        0x10000000
188 #define EFI_FVB_ALIGNMENT_16K       0x20000000
189 #define EFI_FVB_ALIGNMENT_32K       0x40000000
190 #define EFI_FVB_ALIGNMENT_64K       0x80000000
191 // Revision 2
192 #define EFI_FVB2_READ_DISABLED_CAP  0x00000001
193 #define EFI_FVB2_READ_ENABLED_CAP   0x00000002
194 #define EFI_FVB2_READ_STATUS        0x00000004
195 #define EFI_FVB2_WRITE_DISABLED_CAP 0x00000008
196 #define EFI_FVB2_WRITE_ENABLED_CAP  0x00000010
197 #define EFI_FVB2_WRITE_STATUS       0x00000020
198 #define EFI_FVB2_LOCK_CAP           0x00000040
199 #define EFI_FVB2_LOCK_STATUS        0x00000080
200 #define EFI_FVB2_STICKY_WRITE       0x00000200
201 #define EFI_FVB2_MEMORY_MAPPED      0x00000400
202 #define EFI_FVB2_ERASE_POLARITY     0x00000800
203 #define EFI_FVB2_READ_LOCK_CAP      0x00001000
204 #define EFI_FVB2_READ_LOCK_STATUS   0x00002000
205 #define EFI_FVB2_WRITE_LOCK_CAP     0x00004000
206 #define EFI_FVB2_WRITE_LOCK_STATUS  0x00008000
207 #define EFI_FVB2_ALIGNMENT          0x001F0000
208 #define EFI_FVB2_ALIGNMENT_1        0x00000000
209 #define EFI_FVB2_ALIGNMENT_2        0x00010000
210 #define EFI_FVB2_ALIGNMENT_4        0x00020000
211 #define EFI_FVB2_ALIGNMENT_8        0x00030000
212 #define EFI_FVB2_ALIGNMENT_16       0x00040000
213 #define EFI_FVB2_ALIGNMENT_32       0x00050000
214 #define EFI_FVB2_ALIGNMENT_64       0x00060000
215 #define EFI_FVB2_ALIGNMENT_128      0x00070000
216 #define EFI_FVB2_ALIGNMENT_256      0x00080000
217 #define EFI_FVB2_ALIGNMENT_512      0x00090000
218 #define EFI_FVB2_ALIGNMENT_1K       0x000A0000
219 #define EFI_FVB2_ALIGNMENT_2K       0x000B0000
220 #define EFI_FVB2_ALIGNMENT_4K       0x000C0000
221 #define EFI_FVB2_ALIGNMENT_8K       0x000D0000
222 #define EFI_FVB2_ALIGNMENT_16K      0x000E0000
223 #define EFI_FVB2_ALIGNMENT_32K      0x000F0000
224 #define EFI_FVB2_ALIGNMENT_64K      0x00100000
225 #define EFI_FVB2_ALIGNMENT_128K     0x00110000
226 #define EFI_FVB2_ALIGNMENT_256K     0x00120000
227 #define EFI_FVB2_ALIGNMENT_512K     0x00130000
228 #define EFI_FVB2_ALIGNMENT_1M       0x00140000
229 #define EFI_FVB2_ALIGNMENT_2M       0x00150000
230 #define EFI_FVB2_ALIGNMENT_4M       0x00160000
231 #define EFI_FVB2_ALIGNMENT_8M       0x00170000
232 #define EFI_FVB2_ALIGNMENT_16M      0x00180000
233 #define EFI_FVB2_ALIGNMENT_32M      0x00190000
234 #define EFI_FVB2_ALIGNMENT_64M      0x001A0000
235 #define EFI_FVB2_ALIGNMENT_128M     0x001B0000
236 #define EFI_FVB2_ALIGNMENT_256M     0x001C0000
237 #define EFI_FVB2_ALIGNMENT_512M     0x001D0000
238 #define EFI_FVB2_ALIGNMENT_1G       0x001E0000
239 #define EFI_FVB2_ALIGNMENT_2G       0x001F0000
240 #define EFI_FVB2_WEAK_ALIGNMENT     0x80000000
241 
242 // Extended firmware volume header
243 typedef struct EFI_FIRMWARE_VOLUME_EXT_HEADER_ {
244     EFI_GUID          FvName;
245     UINT32            ExtHeaderSize;
246 } EFI_FIRMWARE_VOLUME_EXT_HEADER;
247 
248 // Extended header entry
249 // The extended header entries follow each other and are
250 // terminated by ExtHeaderType EFI_FV_EXT_TYPE_END
251 #define EFI_FV_EXT_TYPE_END        0x0000
252 typedef struct EFI_FIRMWARE_VOLUME_EXT_ENTRY_ {
253     UINT16  ExtEntrySize;
254     UINT16  ExtEntryType;
255 } EFI_FIRMWARE_VOLUME_EXT_ENTRY;
256 
257 // GUID that maps OEM file types to GUIDs
258 #define EFI_FV_EXT_TYPE_OEM_TYPE   0x0001
259 typedef struct EFI_FIRMWARE_VOLUME_EXT_HEADER_OEM_TYPE_ {
260     EFI_FIRMWARE_VOLUME_EXT_ENTRY    Header;
261     UINT32                           TypeMask;
262     //EFI_GUID                       Types[];
263 } EFI_FIRMWARE_VOLUME_EXT_HEADER_OEM_TYPE;
264 
265 #define EFI_FV_EXT_TYPE_GUID_TYPE  0x0002
266 typedef struct EFI_FIRMWARE_VOLUME_EXT_ENTRY_GUID_TYPE_ {
267     EFI_FIRMWARE_VOLUME_EXT_ENTRY Header;
268     EFI_GUID FormatType;
269     //UINT8 Data[];
270 } EFI_FIRMWARE_VOLUME_EXT_ENTRY_GUID_TYPE;
271 
272 //*****************************************************************************
273 // EFI FFS File
274 //*****************************************************************************
275 // Integrity check
276 typedef union {
277     struct {
278         UINT8 Header;
279         UINT8 File;
280     } Checksum;
281     UINT16 TailReference;   // Revision 1
282     UINT16 Checksum16;      // Revision 2
283 } EFI_FFS_INTEGRITY_CHECK;
284 // File header
285 typedef struct EFI_FFS_FILE_HEADER_ {
286     EFI_GUID                Name;
287     EFI_FFS_INTEGRITY_CHECK IntegrityCheck;
288     UINT8                   Type;
289     UINT8                   Attributes;
290     UINT8                   Size[3];
291     UINT8                   State;
292 } EFI_FFS_FILE_HEADER;
293 
294 // Large file header
295 typedef struct EFI_FFS_FILE_HEADER2_ {
296 EFI_GUID                Name;
297 EFI_FFS_INTEGRITY_CHECK IntegrityCheck;
298 UINT8                   Type;
299 UINT8                   Attributes;
300 UINT8                   Size[3]; // Set to 0xFFFFFF
301 UINT8                   State;
302 UINT64                  ExtendedSize;
303 } EFI_FFS_FILE_HEADER2;
304 
305 // Standard data checksum, used if FFS_ATTRIB_CHECKSUM is clear
306 #define FFS_FIXED_CHECKSUM   0x5A
307 #define FFS_FIXED_CHECKSUM2  0xAA
308 
309 // File types
310 #define EFI_FV_FILETYPE_ALL                     0x00
311 #define EFI_FV_FILETYPE_RAW                     0x01
312 #define EFI_FV_FILETYPE_FREEFORM                0x02
313 #define EFI_FV_FILETYPE_SECURITY_CORE           0x03
314 #define EFI_FV_FILETYPE_PEI_CORE                0x04
315 #define EFI_FV_FILETYPE_DXE_CORE                0x05
316 #define EFI_FV_FILETYPE_PEIM                    0x06
317 #define EFI_FV_FILETYPE_DRIVER                  0x07
318 #define EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER    0x08
319 #define EFI_FV_FILETYPE_APPLICATION             0x09
320 #define EFI_FV_FILETYPE_MM                      0x0A
321 #define EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE   0x0B
322 #define EFI_FV_FILETYPE_COMBINED_MM_DXE         0x0C
323 #define EFI_FV_FILETYPE_MM_CORE                 0x0D
324 #define EFI_FV_FILETYPE_MM_STANDALONE           0x0E
325 #define EFI_FV_FILETYPE_MM_CORE_STANDALONE      0x0F
326 #define EFI_FV_FILETYPE_OEM_MIN                 0xC0
327 #define EFI_FV_FILETYPE_OEM_MAX                 0xDF
328 #define EFI_FV_FILETYPE_DEBUG_MIN               0xE0
329 #define EFI_FV_FILETYPE_DEBUG_MAX               0xEF
330 #define EFI_FV_FILETYPE_PAD                     0xF0
331 #define EFI_FV_FILETYPE_FFS_MIN                 0xF0
332 #define EFI_FV_FILETYPE_FFS_MAX                 0xFF
333 
334 // File attributes
335 #define FFS_ATTRIB_TAIL_PRESENT       0x01 // Valid only for revision 1 volumes
336 #define FFS_ATTRIB_RECOVERY           0x02 // Valid only for revision 1 volumes
337 #define FFS_ATTRIB_LARGE_FILE         0x01 // Valid only for FFSv3 volumes
338 #define FFS_ATTRIB_DATA_ALIGNMENT2    0x02 // Volaid only for revision 2 volumes, added in UEFI PI 1.6
339 #define FFS_ATTRIB_FIXED              0x04
340 #define FFS_ATTRIB_DATA_ALIGNMENT     0x38
341 #define FFS_ATTRIB_CHECKSUM           0x40
342 
343 // FFS alignment table
344 extern const UINT8 ffsAlignmentTable[];
345 
346 // Extended FFS alignment table, added in UEFI PI 1.6
347 extern const UINT8 ffsAlignment2Table[];
348 
349 
350 // File states
351 #define EFI_FILE_HEADER_CONSTRUCTION    0x01
352 #define EFI_FILE_HEADER_VALID           0x02
353 #define EFI_FILE_DATA_VALID             0x04
354 #define EFI_FILE_MARKED_FOR_UPDATE      0x08
355 #define EFI_FILE_DELETED                0x10
356 #define EFI_FILE_HEADER_INVALID         0x20
357 #define EFI_FILE_ERASE_POLARITY         0x80 // Defined as "all other bits must be set to ERASE_POLARITY" in UEFI PI
358 
359 // PEI apriori file
360 const UByteArray EFI_PEI_APRIORI_FILE_GUID
361 ("\x0A\xCC\x45\x1B\x6A\x15\x8A\x42\xAF\x62\x49\x86\x4D\xA0\xE6\xE6", 16);
362 
363 // DXE apriori file
364 const UByteArray EFI_DXE_APRIORI_FILE_GUID
365 ("\xE7\x0E\x51\xFC\xDC\xFF\xD4\x11\xBD\x41\x00\x80\xC7\x3C\x88\x81", 16);
366 
367 // Volume top file
368 const UByteArray EFI_FFS_VOLUME_TOP_FILE_GUID
369 ("\x2E\x06\xA0\x1B\x79\xC7\x82\x45\x85\x66\x33\x6A\xE8\xF7\x8F\x09", 16);
370 
371 // Pad file GUID
372 const UByteArray EFI_FFS_PAD_FILE_GUID
373 ("\x85\x65\x53\xE4\x09\x79\x60\x4A\xB5\xC6\xEC\xDE\xA6\xEB\xFB\x54", 16);
374 
375 // AMI DXE core file
376 const UByteArray AMI_CORE_DXE_GUID // 5AE3F37E-4EAE-41AE-8240-35465B5E81EB
377 ("\x7E\xF3\xE3\x5A\xAE\x4E\xAE\x41\x82\x40\x35\x46\x5B\x5E\x81\xEB", 16);
378 
379 // EDK2 DXE code file
380 const UByteArray EFI_DXE_CORE_GUID // D6A2CB7F-6A18-4E2F-B43B-9920A733700A
381 ("\x7F\xCB\xA2\xD6\x18\x6A\x2F\x4E\xB4\x3B\x99\x20\xA7\x33\x70\x0A", 16);
382 
383 // TXT ACM
384 const UByteArray EFI_TXT_ACM_GUID // 2D27C618-7DCD-41F5-BB10-21166BE7E143
385 ("\x18\xC6\x27\x2D\xCD\x7D\xF5\x41\xBB\x10\x21\x16\x6B\xE7\xE1\x43", 16);
386 
387 // FFS size conversion routines
388 extern VOID uint32ToUint24(UINT32 size, UINT8* ffsSize);
389 extern UINT32 uint24ToUint32(const UINT8* ffsSize);
390 
391 //*****************************************************************************
392 // EFI FFS File Section
393 //*****************************************************************************
394 // Common section header
395 typedef struct EFI_COMMON_SECTION_HEADER_ {
396     UINT8    Size[3];
397     UINT8    Type;
398 } EFI_COMMON_SECTION_HEADER;
399 
400 // Large file common section header
401 typedef struct EFI_COMMON_SECTION_HEADER2_ {
402     UINT8    Size[3];    // Must be 0xFFFFFF for this header to be used
403     UINT8    Type;
404     UINT32   ExtendedSize;
405 } EFI_COMMON_SECTION_HEADER2;
406 
407 // Apple common section header
408 typedef struct EFI_COMMON_SECTION_HEADER_APPLE {
409     UINT8    Size[3];
410     UINT8    Type;
411     UINT32   Reserved;   // Must be 0x7FFF for this header to be used
412 } EFI_COMMON_SECTION_HEADER_APPLE;
413 
414 // Section2 usage indicator
415 #define EFI_SECTION2_IS_USED 0xFFFFFF
416 
417 // Apple section usage indicator
418 #define EFI_SECTION_APPLE_USED 0x7FFF
419 
420 // File section types
421 #define EFI_SECTION_ALL 0x00 // Impossible attribute for file in the FS
422 
423 // Encapsulation section types
424 #define EFI_SECTION_COMPRESSION     0x01
425 #define EFI_SECTION_GUID_DEFINED    0x02
426 #define EFI_SECTION_DISPOSABLE      0x03
427 
428 // Leaf section types
429 #define EFI_SECTION_PE32                    0x10
430 #define EFI_SECTION_PIC                     0x11
431 #define EFI_SECTION_TE                      0x12
432 #define EFI_SECTION_DXE_DEPEX               0x13
433 #define EFI_SECTION_VERSION                 0x14
434 #define EFI_SECTION_USER_INTERFACE          0x15
435 #define EFI_SECTION_COMPATIBILITY16         0x16
436 #define EFI_SECTION_FIRMWARE_VOLUME_IMAGE   0x17
437 #define EFI_SECTION_FREEFORM_SUBTYPE_GUID   0x18
438 #define EFI_SECTION_RAW                     0x19
439 #define EFI_SECTION_PEI_DEPEX               0x1B
440 #define EFI_SECTION_MM_DEPEX                0x1C
441 #define PHOENIX_SECTION_POSTCODE            0xF0 // Specific to Phoenix SCT images
442 #define INSYDE_SECTION_POSTCODE             0x20 // Specific to Insyde H2O images
443 
444 // Compression section
445 typedef struct EFI_COMPRESSION_SECTION_ {
446     UINT32   UncompressedLength;
447     UINT8    CompressionType;
448 } EFI_COMPRESSION_SECTION;
449 
450 typedef struct EFI_COMPRESSION_SECTION_APPLE_ {
451     UINT32   UncompressedLength;
452     UINT32   CompressionType;
453 } EFI_COMPRESSION_SECTION_APPLE;
454 
455 // Compression types
456 #define EFI_NOT_COMPRESSED                 0x00
457 #define EFI_STANDARD_COMPRESSION           0x01
458 #define EFI_CUSTOMIZED_COMPRESSION         0x02
459 #define EFI_CUSTOMIZED_COMPRESSION_LZMAF86 0x86
460 
461 //GUID defined section
462 typedef struct EFI_GUID_DEFINED_SECTION_ {
463     EFI_GUID SectionDefinitionGuid;
464     UINT16   DataOffset;
465     UINT16   Attributes;
466 } EFI_GUID_DEFINED_SECTION;
467 
468 typedef struct EFI_GUID_DEFINED_SECTION_APPLE_ {
469     EFI_GUID SectionDefinitionGuid;
470     UINT16   DataOffset;
471     UINT16   Attributes;
472     UINT32   Reserved;
473 } EFI_GUID_DEFINED_SECTION_APPLE;
474 
475 // Attributes for GUID defined section
476 #define EFI_GUIDED_SECTION_PROCESSING_REQUIRED  0x01
477 #define EFI_GUIDED_SECTION_AUTH_STATUS_VALID    0x02
478 
479 // GUIDs of GUID-defined sections
480 const UByteArray EFI_GUIDED_SECTION_CRC32 // FC1BCDB0-7D31-49AA-936A-A4600D9DD083
481 ("\xB0\xCD\x1B\xFC\x31\x7D\xAA\x49\x93\x6A\xA4\x60\x0D\x9D\xD0\x83", 16);
482 
483 const UByteArray EFI_GUIDED_SECTION_TIANO // A31280AD-481E-41B6-95E8-127F4C984779
484 ("\xAD\x80\x12\xA3\x1E\x48\xB6\x41\x95\xE8\x12\x7F\x4C\x98\x47\x79", 16);
485 
486 const UByteArray EFI_GUIDED_SECTION_LZMA // EE4E5898-3914-4259-9D6E-DC7BD79403CF
487 ("\x98\x58\x4E\xEE\x14\x39\x59\x42\x9D\x6E\xDC\x7B\xD7\x94\x03\xCF", 16);
488 
489 const UByteArray EFI_GUIDED_SECTION_LZMAF86 // D42AE6BD-1352-4BFB-909A-CA72A6EAE889
490 ("\xBD\xE6\x2A\xD4\x52\x13\xFB\x4B\x90\x9A\xCA\x72\xA6\xEA\xE8\x89", 16);
491 
492 const UByteArray EFI_GUIDED_SECTION_GZIP // 1D301FE9-BE79-4353-91C2-D23BC959AE0C
493 ("\xE9\x1F\x30\x1D\x79\xBE\x53\x43\x91\xC2\xD2\x3B\xC9\x59\xAE\x0C", 16);
494 
495 const UByteArray EFI_FIRMWARE_CONTENTS_SIGNED_GUID // 0F9D89E8-9259-4F76-A5AF-0C89E34023DF
496 ("\xE8\x89\x9D\x0F\x59\x92\x76\x4F\xA5\xAF\x0C\x89\xE3\x40\x23\xDF", 16);
497 
498 //#define WIN_CERT_TYPE_PKCS_SIGNED_DATA 0x0002
499 #define WIN_CERT_TYPE_EFI_GUID         0x0EF1
500 
501 typedef struct WIN_CERTIFICATE_ {
502     UINT32  Length;
503     UINT16  Revision;
504     UINT16  CertificateType;
505     //UINT8 CertData[];
506 } WIN_CERTIFICATE;
507 
508 typedef struct WIN_CERTIFICATE_UEFI_GUID_ {
509     WIN_CERTIFICATE   Header;     // Standard WIN_CERTIFICATE
510     EFI_GUID          CertType;   // Determines format of CertData
511     // UINT8          CertData[]; // Certificate data follows
512 } WIN_CERTIFICATE_UEFI_GUID;
513 
514 // WIN_CERTIFICATE_UEFI_GUID.CertType
515 const UByteArray EFI_CERT_TYPE_RSA2048_SHA256_GUID // A7717414-C616-4977-9420-844712A735BF
516 ("\x14\x74\x71\xA7\x16\xC6\x77\x49\x94\x20\x84\x47\x12\xA7\x35\xBF");
517 
518 // WIN_CERTIFICATE_UEFI_GUID.CertData
519 typedef struct EFI_CERT_BLOCK_RSA2048_SHA256_ {
520     EFI_GUID  HashType;
521     UINT8     PublicKey[256];
522     UINT8     Signature[256];
523 } EFI_CERT_BLOCK_RSA2048_SHA256;
524 
525 const UByteArray EFI_HASH_ALGORITHM_SHA256_GUID // 51aa59de-fdf2-4ea3-bc63-875fb7842ee9
526 ("\xde\x59\xAA\x51\xF2\xFD\xA3\x4E\xBC\x63\x87\x5F\xB7\x84\x2E\xE9");
527 
528 // Version section
529 typedef struct EFI_VERSION_SECTION_ {
530     UINT16   BuildNumber;
531 } EFI_VERSION_SECTION;
532 
533 // Freeform subtype GUID section
534 typedef struct EFI_FREEFORM_SUBTYPE_GUID_SECTION_ {
535     EFI_GUID SubTypeGuid;
536 } EFI_FREEFORM_SUBTYPE_GUID_SECTION;
537 
538 // Phoenix SCT and Insyde postcode section
539 typedef struct POSTCODE_SECTION_ {
540     UINT32   Postcode;
541 } POSTCODE_SECTION;
542 
543 //*****************************************************************************
544 // EFI Dependency Expression
545 //*****************************************************************************
546 #define EFI_DEP_OPCODE_SIZE   1
547 
548 ///
549 /// If present, this must be the first and only opcode,
550 /// EFI_DEP_BEFORE is only used by DXE drivers
551 ///
552 #define EFI_DEP_BEFORE        0x00
553 
554 ///
555 /// If present, this must be the first and only opcode,
556 /// EFI_DEP_AFTER is only used by DXE drivers
557 ///
558 #define EFI_DEP_AFTER         0x01
559 
560 #define EFI_DEP_PUSH          0x02
561 #define EFI_DEP_AND           0x03
562 #define EFI_DEP_OR            0x04
563 #define EFI_DEP_NOT           0x05
564 #define EFI_DEP_TRUE          0x06
565 #define EFI_DEP_FALSE         0x07
566 #define EFI_DEP_END           0x08
567 
568 
569 ///
570 /// If present, this must be the first opcode,
571 /// EFI_DEP_SOR is only used by DXE drivers
572 ///
573 #define EFI_DEP_SOR           0x09
574 
575 //*****************************************************************************
576 // X86 Reset Vector Data
577 //*****************************************************************************
578 typedef struct X86_RESET_VECTOR_DATA_ {
579     UINT8  ApEntryVector[8];   // Base: 0xffffffd0
580     UINT8  Reserved0[8];
581     UINT32 PeiCoreEntryPoint;  // Base: 0xffffffe0
582     UINT8  Reserved1[12];
583     UINT8  ResetVector[8];     // Base: 0xfffffff0
584     UINT32 ApStartupSegment;   // Base: 0xfffffff8
585     UINT32 BootFvBaseAddress;  // Base: 0xfffffffc
586 } X86_RESET_VECTOR_DATA;
587 
588 #define X86_RESET_VECTOR_DATA_UNPOPULATED 0x12345678
589 
590 //*****************************************************************************
591 // IFWI
592 //*****************************************************************************
593 
594 // BPDT
595 #define BPDT_GREEN_SIGNATURE  0x000055AA
596 #define BPDT_YELLOW_SIGNATURE 0x00AA55AA
597 
598 typedef struct BPDT_HEADER_ {
599     UINT32 Signature;
600     UINT16 NumEntries;
601     UINT8  HeaderVersion;
602     UINT8  RedundancyFlag; // Reserved zero in version 1
603     UINT32 Checksum;
604     UINT32 IfwiVersion;
605     UINT16 FitcMajor;
606     UINT16 FitcMinor;
607     UINT16 FitcHotfix;
608     UINT16 FitcBuild;
609 } BPDT_HEADER;
610 
611 #define BPDT_HEADER_VERSION_1 1
612 #define BPDT_HEADER_VERSION_2 2
613 
614 typedef struct BPDT_ENTRY_ {
615     UINT32 Type : 16;
616     UINT32 SplitSubPartitionFirstPart : 1;
617     UINT32 SplitSubPartitionSecondPart : 1;
618     UINT32 CodeSubPartition : 1;
619     UINT32 UmaCachable : 1;
620     UINT32 Reserved: 12;
621     UINT32 Offset;
622     UINT32 Size;
623 } BPDT_ENTRY;
624 
625 #define BPDT_ENTRY_TYPE_OEM_SMIP         0
626 #define BPDT_ENTRY_TYPE_OEM_RBE          1
627 #define BPDT_ENTRY_TYPE_CSE_BUP          2
628 #define BPDT_ENTRY_TYPE_UCODE            3
629 #define BPDT_ENTRY_TYPE_IBB              4
630 #define BPDT_ENTRY_TYPE_SBPDT            5
631 #define BPDT_ENTRY_TYPE_OBB              6
632 #define BPDT_ENTRY_TYPE_CSE_MAIN         7
633 #define BPDT_ENTRY_TYPE_ISH              8
634 #define BPDT_ENTRY_TYPE_CSE_IDLM         9
635 #define BPDT_ENTRY_TYPE_IFP_OVERRIDE     10
636 #define BPDT_ENTRY_TYPE_DEBUG_TOKENS     11
637 #define BPDT_ENTRY_TYPE_USF_PHY_CONFIG   12
638 #define BPDT_ENTRY_TYPE_USB_GPP_LUN_ID   13
639 #define BPDT_ENTRY_TYPE_PMC              14
640 #define BPDT_ENTRY_TYPE_IUNIT            15
641 #define BPDT_ENTRY_TYPE_NVM_CONFIG       16
642 #define BPDT_ENTRY_TYPE_UEP              17
643 #define BPDT_ENTRY_TYPE_WLAN_UCODE       18
644 #define BPDT_ENTRY_TYPE_LOCL_SPRITES     19
645 #define BPDT_ENTRY_TYPE_OEM_KEY_MANIFEST 20
646 #define BPDT_ENTRY_TYPE_DEFAULTS         21
647 #define BPDT_ENTRY_TYPE_PAVP             22
648 #define BPDT_ENTRY_TYPE_TCSS_FW_IOM      23
649 #define BPDT_ENTRY_TYPE_TCSS_FW_PHY      24
650 #define BPDT_ENTRY_TYPE_TBT              25
651 #define BPDT_ENTRY_TYPE_SAMF             41
652 #define BPDT_ENTRY_TYPE_PPHY             42
653 
654 // CPD
655 #define CPD_SIGNATURE 0x44504324 //$CPD
656 
657 typedef struct CPD_REV1_HEADER_ {
658     UINT32 Signature;
659     UINT32 NumEntries;
660     UINT8  HeaderVersion; // 1
661     UINT8  EntryVersion;
662     UINT8  HeaderLength;
663     UINT8  HeaderChecksum;
664     UINT8  ShortName[4];
665 } CPD_REV1_HEADER;
666 
667 typedef struct CPD_REV2_HEADER_ {
668     UINT32 Signature;
669     UINT32 NumEntries;
670     UINT8  HeaderVersion; // 2
671     UINT8  EntryVersion;
672     UINT8  HeaderLength;
673     UINT8  Reserved;
674     UINT8  ShortName[4];
675     UINT32 Checksum;
676 } CPD_REV2_HEADER;
677 
678 typedef struct CPD_ENTRY_ {
679     UINT8  EntryName[12];
680     struct {
681         UINT32 Offset : 25;
682         UINT32 HuffmanCompressed : 1;
683         UINT32 Reserved : 6;
684     } Offset;
685     UINT32 Length;
686     UINT32 Reserved;
687 } CPD_ENTRY;
688 
689 typedef struct CPD_MANIFEST_HEADER_ {
690     UINT32 HeaderType;
691     UINT32 HeaderLength;
692     UINT32 HeaderVersion;
693     UINT32 Flags;
694     UINT32 Vendor;
695     UINT32 Date;
696     UINT32 Size;
697     UINT32 HeaderId;
698     UINT32 Reserved1;
699     UINT16 VersionMajor;
700     UINT16 VersionMinor;
701     UINT16 VersionBugfix;
702     UINT16 VersionBuild;
703     UINT32 SecurityVersion;
704     UINT8  Reserved2[8];
705     UINT8  Reserved3[64];
706     UINT32 ModulusSize;
707     UINT32 ExponentSize;
708     //manifest_rsa_key_t public_key;
709     //manifest_signature_t signature;
710 } CPD_MANIFEST_HEADER;
711 
712 typedef struct CPD_EXTENTION_HEADER_ {
713     UINT32 Type;
714     UINT32 Length;
715 } CPD_EXTENTION_HEADER;
716 
717 #define CPD_EXT_TYPE_SYSTEM_INFO             0
718 #define CPD_EXT_TYPE_INIT_SCRIPT             1
719 #define CPD_EXT_TYPE_FEATURE_PERMISSIONS     2
720 #define CPD_EXT_TYPE_PARTITION_INFO          3
721 #define CPD_EXT_TYPE_SHARED_LIB_ATTRIBUTES   4
722 #define CPD_EXT_TYPE_PROCESS_ATTRIBUTES      5
723 #define CPD_EXT_TYPE_THREAD_ATTRIBUTES       6
724 #define CPD_EXT_TYPE_DEVICE_TYPE             7
725 #define CPD_EXT_TYPE_MMIO_RANGE              8
726 #define CPD_EXT_TYPE_SPEC_FILE_PRODUCER      9
727 #define CPD_EXT_TYPE_MODULE_ATTRIBUTES       10
728 #define CPD_EXT_TYPE_LOCKED_RANGES           11
729 #define CPD_EXT_TYPE_CLIENT_SYSTEM_INFO      12
730 #define CPD_EXT_TYPE_USER_INFO               13
731 #define CPD_EXT_TYPE_KEY_MANIFEST            14
732 #define CPD_EXT_TYPE_SIGNED_PACKAGE_INFO     15
733 #define CPD_EXT_TYPE_ANTI_CLONING_SKU_ID     16
734 #define CPD_EXT_TYPE_CAVS                    17
735 #define CPD_EXT_TYPE_IMR_INFO                18
736 #define CPD_EXT_TYPE_BOOT_POLICY             19
737 #define CPD_EXT_TYPE_RCIP_INFO               20
738 #define CPD_EXT_TYPE_SECURE_TOKEN            21
739 #define CPD_EXT_TYPE_IFWI_PARTITION_MANIFEST 22
740 #define CPD_EXT_TYPE_FD_HASH                 23
741 #define CPD_EXT_TYPE_IOM_METADATA            24
742 #define CPD_EXT_TYPE_MGP_METADATA            25
743 #define CPD_EXT_TYPE_TBT_METADATA            26
744 #define CPD_EXT_TYPE_GMF_CERTIFICATE         30
745 #define CPD_EXT_TYPE_GMF_BODY                31
746 #define CPD_EXT_TYPE_KEY_MANIFEST_EXT        34
747 #define CPD_EXT_TYPE_SIGNED_PACKAGE_INFO_EXT 35
748 #define CPD_EXT_TYPE_SPS_PLATFORM_ID         50
749 
750 typedef struct CPD_EXT_SIGNED_PACKAGE_INFO_MODULE_ {
751     UINT8  Name[12];
752     UINT8  Type;
753     UINT8  HashAlgorithm;
754     UINT16 HashSize;
755     UINT32 MetadataSize;
756     // UINT8  MetadataHash[]; with the actual hash size is 32 or 48 bytes
757 } CPD_EXT_SIGNED_PACKAGE_INFO_MODULE;
758 
759 static const size_t CpdExtSignedPkgMetadataHashOffset = sizeof(CPD_EXT_SIGNED_PACKAGE_INFO_MODULE);
760 
761 typedef struct CPD_EXT_SIGNED_PACKAGE_INFO_ {
762     UINT32 ExtensionType;
763     UINT32 ExtensionLength;
764     UINT8  PackageName[4];
765     UINT32 Vcn;
766     UINT8  UsageBitmap[16];
767     UINT32 Svn;
768     UINT8  Reserved[16];
769     // EXT_SIGNED_PACKAGE_INFO_MODULE Modules[];
770 } CPD_EXT_SIGNED_PACKAGE_INFO;
771 
772 typedef struct CPD_EXT_MODULE_ATTRIBUTES_ {
773     UINT32 ExtensionType;
774     UINT32 ExtensionLength;
775     UINT32 CompressionType;
776     UINT32 UncompressedSize;
777     UINT32 CompressedSize;
778     UINT32 GlobalModuleId;
779     // UINT8  ImageHash[]; with the actual hash size is 32 or 48 bytes
780 } CPD_EXT_MODULE_ATTRIBUTES;
781 
782 static const size_t CpdExtModuleImageHashOffset = sizeof(CPD_EXT_MODULE_ATTRIBUTES);
783 
784 #define CPD_EXT_MODULE_COMPRESSION_TYPE_UNCOMPRESSED 0
785 #define CPD_EXT_MODULE_COMPRESSION_TYPE_HUFFMAN 1
786 #define CPD_EXT_MODULE_COMPRESSION_TYPE_LZMA 2
787 
788 typedef struct CPD_EXT_IFWI_PARTITION_MANIFEST_ {
789     UINT32 ExtensionType;
790     UINT32 ExtensionLength;
791     UINT8  PartitionName[4];
792     UINT32 CompletePartitionLength;
793     UINT16 PartitionVersionMinor;
794     UINT16 PartitionVersionMajor;
795     UINT32 DataFormatVersion;
796     UINT32 InstanceId;
797     UINT32 SupportMultipleInstances : 1;
798     UINT32 SupportApiVersionBasedUpdate : 1;
799     UINT32 ActionOnUpdate : 2;
800     UINT32 ObeyFullUpdateRules : 1;
801     UINT32 IfrEnableOnly : 1;
802     UINT32 AllowCrossPointUpdate : 1;
803     UINT32 AllowCrossHotfixUpdate : 1;
804     UINT32 PartialUpdateOnly : 1;
805     UINT32 ReservedFlags : 23;
806     UINT32 HashAlgorithm : 8;
807     UINT32 HashSize : 24;
808     UINT8  CompletePartitionHash[48];
809     UINT8  Reserved[4];
810 } CPD_EXT_IFWI_PARTITION_MANIFEST;
811 
812 // Restore previous packing rules
813 #pragma pack(pop)
814 
815 #endif // FFS_H
816