1 /** @file
2 This file contains describes the public interfaces to the GenFvImage Library.
3 The basic purpose of the library is to create Firmware Volume images.
4 
5 Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7 
8 **/
9 
10 #ifndef _EFI_GEN_FV_INTERNAL_LIB_H
11 #define _EFI_GEN_FV_INTERNAL_LIB_H
12 
13 //
14 // Include files
15 //
16 #include <stdlib.h>
17 
18 #include <Common/UefiBaseTypes.h>
19 #include <Common/UefiCapsule.h>
20 
21 #include <Common/PiFirmwareFile.h>
22 #include <Common/PiFirmwareVolume.h>
23 #include <Guid/PiFirmwareFileSystem.h>
24 #include <IndustryStandard/PeImage.h>
25 
26 #include "CommonLib.h"
27 #include "ParseInf.h"
28 #include "EfiUtilityMsgs.h"
29 
30 //
31 // Different file separator for Linux and Windows
32 //
33 #define FILE_SEP_CHAR '/'
34 
35 //
36 // The maximum number of Pad file guid entries.
37 //
38 #define MAX_NUMBER_OF_PAD_FILE_GUIDS    1024
39 
40 //
41 // The maximum number of block map entries supported by the library
42 //
43 #define MAX_NUMBER_OF_FV_BLOCKS         100
44 
45 //
46 // The maximum number of files in the FV supported by the library
47 //
48 #define MAX_NUMBER_OF_FILES_IN_FV       1000
49 #define MAX_NUMBER_OF_FILES_IN_CAP      1000
50 #define EFI_FFS_FILE_HEADER_ALIGNMENT   8
51 //
52 // INF file strings
53 //
54 #define OPTIONS_SECTION_STRING                "[options]"
55 #define ATTRIBUTES_SECTION_STRING             "[attributes]"
56 #define FILES_SECTION_STRING                  "[files]"
57 #define FV_BASE_ADDRESS_STRING                "[FV_BASE_ADDRESS]"
58 
59 //
60 // Options section
61 //
62 #define EFI_FV_BASE_ADDRESS_STRING        "EFI_BASE_ADDRESS"
63 #define EFI_FV_FILE_NAME_STRING           "EFI_FILE_NAME"
64 #define EFI_NUM_BLOCKS_STRING             "EFI_NUM_BLOCKS"
65 #define EFI_BLOCK_SIZE_STRING             "EFI_BLOCK_SIZE"
66 #define EFI_GUID_STRING                   "EFI_GUID"
67 #define EFI_FV_FILESYSTEMGUID_STRING      "EFI_FV_GUID"
68 #define EFI_FV_NAMEGUID_STRING            "EFI_FVNAME_GUID"
69 #define EFI_CAPSULE_GUID_STRING           "EFI_CAPSULE_GUID"
70 #define EFI_CAPSULE_HEADER_SIZE_STRING    "EFI_CAPSULE_HEADER_SIZE"
71 #define EFI_CAPSULE_FLAGS_STRING          "EFI_CAPSULE_FLAGS"
72 #define EFI_OEM_CAPSULE_FLAGS_STRING      "EFI_OEM_CAPSULE_FLAGS"
73 #define EFI_CAPSULE_VERSION_STRING        "EFI_CAPSULE_VERSION"
74 
75 #define EFI_FV_TOTAL_SIZE_STRING    "EFI_FV_TOTAL_SIZE"
76 #define EFI_FV_TAKEN_SIZE_STRING    "EFI_FV_TAKEN_SIZE"
77 #define EFI_FV_SPACE_SIZE_STRING    "EFI_FV_SPACE_SIZE"
78 
79 //
80 // Attributes section
81 //
82 #define EFI_FVB2_READ_DISABLED_CAP_STRING  "EFI_READ_DISABLED_CAP"
83 #define EFI_FVB2_READ_ENABLED_CAP_STRING   "EFI_READ_ENABLED_CAP"
84 #define EFI_FVB2_READ_STATUS_STRING        "EFI_READ_STATUS"
85 
86 #define EFI_FVB2_WRITE_DISABLED_CAP_STRING "EFI_WRITE_DISABLED_CAP"
87 #define EFI_FVB2_WRITE_ENABLED_CAP_STRING  "EFI_WRITE_ENABLED_CAP"
88 #define EFI_FVB2_WRITE_STATUS_STRING       "EFI_WRITE_STATUS"
89 
90 #define EFI_FVB2_LOCK_CAP_STRING           "EFI_LOCK_CAP"
91 #define EFI_FVB2_LOCK_STATUS_STRING        "EFI_LOCK_STATUS"
92 
93 #define EFI_FVB2_STICKY_WRITE_STRING       "EFI_STICKY_WRITE"
94 #define EFI_FVB2_MEMORY_MAPPED_STRING      "EFI_MEMORY_MAPPED"
95 #define EFI_FVB2_ERASE_POLARITY_STRING     "EFI_ERASE_POLARITY"
96 
97 #define EFI_FVB2_READ_LOCK_CAP_STRING      "EFI_READ_LOCK_CAP"
98 #define EFI_FVB2_READ_LOCK_STATUS_STRING   "EFI_READ_LOCK_STATUS"
99 #define EFI_FVB2_WRITE_LOCK_CAP_STRING     "EFI_WRITE_LOCK_CAP"
100 #define EFI_FVB2_WRITE_LOCK_STATUS_STRING  "EFI_WRITE_LOCK_STATUS"
101 
102 #define EFI_FVB2_ALIGNMENT_1_STRING       "EFI_FVB2_ALIGNMENT_1"
103 #define EFI_FVB2_ALIGNMENT_2_STRING       "EFI_FVB2_ALIGNMENT_2"
104 #define EFI_FVB2_ALIGNMENT_4_STRING       "EFI_FVB2_ALIGNMENT_4"
105 #define EFI_FVB2_ALIGNMENT_8_STRING       "EFI_FVB2_ALIGNMENT_8"
106 #define EFI_FVB2_ALIGNMENT_16_STRING      "EFI_FVB2_ALIGNMENT_16"
107 #define EFI_FVB2_ALIGNMENT_32_STRING      "EFI_FVB2_ALIGNMENT_32"
108 #define EFI_FVB2_ALIGNMENT_64_STRING      "EFI_FVB2_ALIGNMENT_64"
109 #define EFI_FVB2_ALIGNMENT_128_STRING     "EFI_FVB2_ALIGNMENT_128"
110 #define EFI_FVB2_ALIGNMENT_256_STRING     "EFI_FVB2_ALIGNMENT_256"
111 #define EFI_FVB2_ALIGNMENT_512_STRING     "EFI_FVB2_ALIGNMENT_512"
112 #define EFI_FVB2_ALIGNMENT_1K_STRING      "EFI_FVB2_ALIGNMENT_1K"
113 #define EFI_FVB2_ALIGNMENT_2K_STRING      "EFI_FVB2_ALIGNMENT_2K"
114 #define EFI_FVB2_ALIGNMENT_4K_STRING      "EFI_FVB2_ALIGNMENT_4K"
115 #define EFI_FVB2_ALIGNMENT_8K_STRING      "EFI_FVB2_ALIGNMENT_8K"
116 #define EFI_FVB2_ALIGNMENT_16K_STRING     "EFI_FVB2_ALIGNMENT_16K"
117 #define EFI_FVB2_ALIGNMENT_32K_STRING     "EFI_FVB2_ALIGNMENT_32K"
118 #define EFI_FVB2_ALIGNMENT_64K_STRING     "EFI_FVB2_ALIGNMENT_64K"
119 #define EFI_FVB2_ALIGNMENT_128K_STRING    "EFI_FVB2_ALIGNMENT_128K"
120 #define EFI_FVB2_ALIGNMENT_256K_STRING    "EFI_FVB2_ALIGNMENT_256K"
121 #define EFI_FVB2_ALIGNMENT_512K_STRING    "EFI_FVB2_ALIGNMENT_512K"
122 #define EFI_FVB2_ALIGNMENT_1M_STRING      "EFI_FVB2_ALIGNMENT_1M"
123 #define EFI_FVB2_ALIGNMENT_2M_STRING      "EFI_FVB2_ALIGNMENT_2M"
124 #define EFI_FVB2_ALIGNMENT_4M_STRING      "EFI_FVB2_ALIGNMENT_4M"
125 #define EFI_FVB2_ALIGNMENT_8M_STRING      "EFI_FVB2_ALIGNMENT_8M"
126 #define EFI_FVB2_ALIGNMENT_16M_STRING     "EFI_FVB2_ALIGNMENT_16M"
127 #define EFI_FVB2_ALIGNMENT_32M_STRING     "EFI_FVB2_ALIGNMENT_32M"
128 #define EFI_FVB2_ALIGNMENT_64M_STRING     "EFI_FVB2_ALIGNMENT_64M"
129 #define EFI_FVB2_ALIGNMENT_128M_STRING    "EFI_FVB2_ALIGNMENT_128M"
130 #define EFI_FVB2_ALIGNMENT_256M_STRING    "EFI_FVB2_ALIGNMENT_256M"
131 #define EFI_FVB2_ALIGNMENT_512M_STRING    "EFI_FVB2_ALIGNMENT_512M"
132 #define EFI_FVB2_ALIGNMENT_1G_STRING      "EFI_FVB2_ALIGNMENT_1G"
133 #define EFI_FVB2_ALIGNMENT_2G_STRING      "EFI_FVB2_ALIGNMENT_2G"
134 
135 #define EFI_FV_WEAK_ALIGNMENT_STRING      "EFI_WEAK_ALIGNMENT"
136 
137 //
138 // File sections
139 //
140 #define EFI_FILE_NAME_STRING      "EFI_FILE_NAME"
141 
142 #define ONE_STRING                "1"
143 #define ZERO_STRING               "0"
144 #define TRUE_STRING               "TRUE"
145 #define FALSE_STRING              "FALSE"
146 #define NULL_STRING               "NULL"
147 
148 //
149 //
150 //
151 #define EFI_FV_EXT_HEADER_FILE_NAME     "EFI_FV_EXT_HEADER_FILE_NAME"
152 
153 
154 //
155 // VTF (Firmware Volume Top File) signatures
156 //
157 #define IA32_X64_VTF_SIGNATURE_OFFSET    0x14
158 #define IA32_X64_VTF0_SIGNATURE SIGNATURE_32('V','T','F',0)
159 
160 //
161 // Defines to calculate the offset for PEI CORE entry points
162 //
163 #define IA32_PEI_CORE_ENTRY_OFFSET    0x20
164 
165 //
166 // Defines to calculate the offset for IA32 SEC CORE entry point
167 //
168 #define IA32_SEC_CORE_ENTRY_OFFSET     0xD
169 
170 //
171 // Symbol file definitions, current max size if 512K
172 //
173 #define SYMBOL_FILE_SIZE              0x80000
174 
175 #define FV_IMAGES_TOP_ADDRESS         0x100000000ULL
176 
177 //
178 // Following definition is used for FIT in IPF
179 //
180 #define COMP_TYPE_FIT_PEICORE 0x10
181 #define COMP_TYPE_FIT_UNUSED  0x7F
182 
183 #define FIT_TYPE_MASK         0x7F
184 #define CHECKSUM_BIT_MASK     0x80
185 
186 //
187 // Private data types
188 //
189 //
190 // Component information
191 //
192 typedef struct {
193   UINTN Size;
194   CHAR8 ComponentName[MAX_LONG_FILE_PATH];
195 } COMPONENT_INFO;
196 
197 //
198 // FV and capsule information holder
199 //
200 typedef struct {
201   BOOLEAN                 BaseAddressSet;
202   EFI_PHYSICAL_ADDRESS    BaseAddress;
203   EFI_GUID                FvFileSystemGuid;
204   BOOLEAN                 FvFileSystemGuidSet;
205   EFI_GUID                FvNameGuid;
206   BOOLEAN                 FvNameGuidSet;
207   CHAR8                   FvExtHeaderFile[MAX_LONG_FILE_PATH];
208   UINTN                   Size;
209   EFI_FVB_ATTRIBUTES_2    FvAttributes;
210   CHAR8                   FvName[MAX_LONG_FILE_PATH];
211   EFI_FV_BLOCK_MAP_ENTRY  FvBlocks[MAX_NUMBER_OF_FV_BLOCKS];
212   CHAR8                   FvFiles[MAX_NUMBER_OF_FILES_IN_FV][MAX_LONG_FILE_PATH];
213   UINT32                  SizeofFvFiles[MAX_NUMBER_OF_FILES_IN_FV];
214   BOOLEAN                 IsPiFvImage;
215   INT8                    ForceRebase;
216 } FV_INFO;
217 
218 typedef struct {
219   EFI_GUID                CapGuid;
220   UINT32                  HeaderSize;
221   UINT32                  Flags;
222   CHAR8                   CapName[MAX_LONG_FILE_PATH];
223   CHAR8                   CapFiles[MAX_NUMBER_OF_FILES_IN_CAP][MAX_LONG_FILE_PATH];
224 } CAP_INFO;
225 
226 #pragma pack(1)
227 
228 typedef struct {
229   UINT64  CompAddress;
230   UINT32  CompSize;
231   UINT16  CompVersion;
232   UINT8   CvAndType;
233   UINT8   CheckSum;
234 } FIT_TABLE;
235 
236 #pragma pack()
237 
238 #define FV_DEFAULT_ATTRIBUTE  0x0004FEFF
239 extern FV_INFO    mFvDataInfo;
240 extern CAP_INFO   mCapDataInfo;
241 extern EFI_GUID   mEfiFirmwareFileSystem2Guid;
242 extern EFI_GUID   mEfiFirmwareFileSystem3Guid;
243 extern UINT32     mFvTotalSize;
244 extern UINT32     mFvTakenSize;
245 
246 extern EFI_PHYSICAL_ADDRESS mFvBaseAddress[];
247 extern UINT32               mFvBaseAddressNumber;
248 //
249 // Local function prototypes
250 //
251 EFI_STATUS
252 ParseFvInf (
253   IN  MEMORY_FILE  *InfFile,
254   OUT FV_INFO      *FvInfo
255   )
256 ;
257 
258 EFI_STATUS
259 UpdatePeiCoreEntryInFit (
260   IN FIT_TABLE     *FitTablePtr,
261   IN UINT64        PeiCorePhysicalAddress
262   )
263 /*++
264 
265 Routine Description:
266 
267   This function is used to update the Pei Core address in FIT, this can be used by Sec core to pass control from
268   Sec to Pei Core
269 
270 Arguments:
271 
272   FitTablePtr             - The pointer of FIT_TABLE.
273   PeiCorePhysicalAddress  - The address of Pei Core entry.
274 
275 Returns:
276 
277   EFI_SUCCESS             - The PEI_CORE FIT entry was updated successfully.
278   EFI_NOT_FOUND           - Not found the PEI_CORE FIT entry.
279 
280 --*/
281 ;
282 
283 VOID
284 UpdateFitCheckSum (
285   IN FIT_TABLE   *FitTablePtr
286   )
287 /*++
288 
289 Routine Description:
290 
291   This function is used to update the checksum for FIT.
292 
293 
294 Arguments:
295 
296   FitTablePtr             - The pointer of FIT_TABLE.
297 
298 Returns:
299 
300   None.
301 
302 --*/
303 ;
304 
305 EFI_STATUS
306 GetPe32Info (
307   IN UINT8                  *Pe32,
308   OUT UINT32                *EntryPoint,
309   OUT UINT32                *BaseOfCode,
310   OUT UINT16                *MachineType
311   );
312 
313 EFI_STATUS
314 ParseCapInf (
315   IN  MEMORY_FILE  *InfFile,
316   OUT CAP_INFO     *CapInfo
317   );
318 
319 EFI_STATUS
320 FindApResetVectorPosition (
321   IN  MEMORY_FILE  *FvImage,
322   OUT UINT8        **Pointer
323   );
324 
325 EFI_STATUS
326 CalculateFvSize (
327   FV_INFO *FvInfoPtr
328   );
329 
330 EFI_STATUS
331 FfsRebase (
332   IN OUT  FV_INFO               *FvInfo,
333   IN      CHAR8                 *FileName,
334   IN OUT  EFI_FFS_FILE_HEADER   *FfsFile,
335   IN      UINTN                 XipOffset,
336   IN      FILE                  *FvMapFile
337   );
338 
339 //
340 // Exported function prototypes
341 //
342 EFI_STATUS
343 GenerateCapImage (
344   IN CHAR8                *InfFileImage,
345   IN UINTN                InfFileSize,
346   IN CHAR8                *CapFileName
347   )
348 /*++
349 
350 Routine Description:
351 
352   This is the main function which will be called from application to
353   generate UEFI Capsule image.
354 
355 Arguments:
356 
357   InfFileImage   Buffer containing the INF file contents.
358   InfFileSize    Size of the contents of the InfFileImage buffer.
359   CapFileName    Requested name for the Cap file.
360 
361 Returns:
362 
363   EFI_SUCCESS             Function completed successfully.
364   EFI_OUT_OF_RESOURCES    Could not allocate required resources.
365   EFI_ABORTED             Error encountered.
366   EFI_INVALID_PARAMETER   A required parameter was NULL.
367 
368 --*/
369 ;
370 
371 EFI_STATUS
372 GenerateFvImage (
373   IN CHAR8                *InfFileImage,
374   IN UINTN                InfFileSize,
375   IN CHAR8                *FvFileName,
376   IN CHAR8                *MapFileName
377   )
378 /*++
379 
380 Routine Description:
381 
382   This is the main function which will be called from application to
383   generate Firmware Image conforms to PI spec.
384 
385 Arguments:
386 
387   InfFileImage   Buffer containing the INF file contents.
388   InfFileSize    Size of the contents of the InfFileImage buffer.
389   FvFileName     Requested name for the FV file.
390   MapFileName    Fv map file to log fv driver information.
391 
392 Returns:
393 
394   EFI_SUCCESS             Function completed successfully.
395   EFI_OUT_OF_RESOURCES    Could not allocate required resources.
396   EFI_ABORTED             Error encountered.
397   EFI_INVALID_PARAMETER   A required parameter was NULL.
398 
399 --*/
400 ;
401 
402 #endif
403