1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <console/console.h>
4 #include <commonlib/endian.h>
5 #include <commonlib/fsp.h>
7 * Intel's code does not have a handle on changing global packing state.
8 * Therefore, one needs to protect against packing policies that are set
9 * globally for a compilation unit just by including a header file.
13 /* Default bind FSP 1.1 API to edk2 UEFI 2.4 types. */
14 #include <vendorcode/intel/edk2/uefi_2.4/uefi_types.h>
15 #include <vendorcode/intel/fsp/fsp1_1/IntelFspPkg/Include/FspInfoHeader.h>
17 /* Restore original packing policy. */
20 #include <commonlib/helpers.h>
25 #define FSP_DBG_LVL BIOS_NEVER
28 * UEFI defines everything as little endian. However, this piece of code
29 * can be integrated in a userland tool. That tool could be on a big endian
30 * machine so one needs to access the fields within UEFI structures using
31 * endian-aware accesses.
34 /* Return 0 if equal. Non-zero if not equal. */
35 static int guid_compare(const EFI_GUID
*le_guid
, const EFI_GUID
*native_guid
)
37 if (read_le32(&le_guid
->Data1
) != native_guid
->Data1
)
39 if (read_le16(&le_guid
->Data2
) != native_guid
->Data2
)
41 if (read_le16(&le_guid
->Data3
) != native_guid
->Data3
)
43 return memcmp(le_guid
->Data4
, native_guid
->Data4
,
44 ARRAY_SIZE(le_guid
->Data4
));
47 static const EFI_GUID ffs2_guid
= EFI_FIRMWARE_FILE_SYSTEM2_GUID
;
48 static const EFI_GUID fih_guid
= FSP_INFO_HEADER_GUID
;
50 struct fsp_patch_table
{
52 uint16_t header_length
;
53 uint8_t header_revision
;
55 uint32_t patch_entry_num
;
56 uint32_t patch_entries
[0];
59 #define FSPP_SIG 0x50505346
61 static void *relative_offset(void *base
, ssize_t offset
)
65 loc
= (uintptr_t)base
;
71 static uint32_t *fspp_reloc(void *fsp
, size_t fsp_size
, uint32_t e
)
75 /* Offsets live in bits 23:0. */
76 offset
= e
& 0xffffff;
78 /* If bit 31 is set then the offset is considered a negative value
79 * relative to the end of the image using 16MiB as the offset's
82 offset
= fsp_size
- (16 * MiB
- offset
);
84 /* Determine if offset falls within fsp_size for a 32 bit relocation. */
85 if (offset
> fsp_size
- sizeof(uint32_t))
88 return relative_offset(fsp
, offset
);
91 static int reloc_type(uint16_t reloc_entry
)
93 /* Reloc type in upper 4 bits */
94 return reloc_entry
>> 12;
97 static size_t reloc_offset(uint16_t reloc_entry
)
99 /* Offsets are in low 12 bits. */
100 return reloc_entry
& ((1 << 12) - 1);
103 static int te_relocate(uintptr_t new_addr
, void *te
)
105 EFI_TE_IMAGE_HEADER
*teih
;
106 EFI_IMAGE_DATA_DIRECTORY
*relocd
;
107 EFI_IMAGE_BASE_RELOCATION
*relocb
;
108 uintptr_t image_base
;
112 size_t relocd_offset
;
118 if (read_le16(&teih
->Signature
) != EFI_TE_IMAGE_HEADER_SIGNATURE
) {
119 printk(BIOS_ERR
, "TE Signature mismatch: %x vs %x\n",
120 read_le16(&teih
->Signature
),
121 EFI_TE_IMAGE_HEADER_SIGNATURE
);
126 * A TE image is created by converting a PE file. Because of this
127 * the offsets within the headers are off. In order to calculate
128 * the correct relative offsets one needs to subtract fixup_offset
129 * from the encoded offsets. Similarly, the linked address of the
130 * program is found by adding the fixup_offset to the ImageBase.
132 fixup_offset
= read_le16(&teih
->StrippedSize
);
133 fixup_offset
-= sizeof(EFI_TE_IMAGE_HEADER
);
134 /* Keep track of a base that is correctly adjusted so that offsets
135 * can be used directly. */
137 te_base
-= fixup_offset
;
139 image_base
= read_le64(&teih
->ImageBase
);
140 adj
= new_addr
- (image_base
+ fixup_offset
);
142 printk(FSP_DBG_LVL
, "TE Image %p -> %p adjust value: %x\n",
143 (void *)image_base
, (void *)new_addr
, adj
);
145 /* Adjust ImageBase for consistency. */
146 write_le64(&teih
->ImageBase
, (uint32_t)(image_base
+ adj
));
148 relocd
= &teih
->DataDirectory
[EFI_TE_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
151 /* Though the field name is VirtualAddress it's actually relative to
152 * the beginning of the image which is linked at ImageBase. */
153 relocb
= relative_offset(te
,
154 read_le32(&relocd
->VirtualAddress
) - fixup_offset
);
155 while (relocd_offset
< read_le32(&relocd
->Size
)) {
156 size_t rva_offset
= read_le32(&relocb
->VirtualAddress
);
158 printk(FSP_DBG_LVL
, "Relocs for RVA offset %zx\n", rva_offset
);
159 num_relocs
= read_le32(&relocb
->SizeOfBlock
) - sizeof(*relocb
);
160 num_relocs
/= sizeof(uint16_t);
161 reloc
= relative_offset(relocb
, sizeof(*relocb
));
163 printk(FSP_DBG_LVL
, "Num relocs in block: %zx\n", num_relocs
);
165 while (num_relocs
> 0) {
166 uint16_t reloc_val
= read_le16(reloc
);
167 int type
= reloc_type(reloc_val
);
168 size_t offset
= reloc_offset(reloc_val
);
170 printk(FSP_DBG_LVL
, "reloc type %x offset %zx\n",
173 if (type
== EFI_IMAGE_REL_BASED_HIGHLOW
) {
174 uint32_t *reloc_addr
;
177 offset
+= rva_offset
;
178 reloc_addr
= (void *)&te_base
[offset
];
179 val
= read_le32(reloc_addr
);
181 printk(FSP_DBG_LVL
, "Adjusting %p %x -> %x\n",
182 reloc_addr
, val
, val
+ adj
);
183 write_le32(reloc_addr
, val
+ adj
);
184 } else if (type
!= EFI_IMAGE_REL_BASED_ABSOLUTE
) {
185 printk(BIOS_ERR
, "Unknown reloc type: %x\n",
193 /* Track consumption of relocation directory contents. */
194 relocd_offset
+= read_le32(&relocb
->SizeOfBlock
);
195 /* Get next relocation block to process. */
196 relocb
= relative_offset(relocb
,
197 read_le32(&relocb
->SizeOfBlock
));
203 static size_t csh_size(const EFI_COMMON_SECTION_HEADER
*csh
)
207 /* Unpack the array into a type that can be used. */
209 size
|= read_le8(&csh
->Size
[0]) << 0;
210 size
|= read_le8(&csh
->Size
[1]) << 8;
211 size
|= read_le8(&csh
->Size
[2]) << 16;
216 static size_t section_data_offset(const EFI_COMMON_SECTION_HEADER
*csh
)
218 if (csh_size(csh
) == 0x00ffffff)
219 return sizeof(EFI_COMMON_SECTION_HEADER2
);
221 return sizeof(EFI_COMMON_SECTION_HEADER
);
224 static size_t section_data_size(const EFI_COMMON_SECTION_HEADER
*csh
)
228 if (csh_size(csh
) == 0x00ffffff)
229 section_size
= read_le32(&SECTION2_SIZE(csh
));
231 section_size
= csh_size(csh
);
233 return section_size
- section_data_offset(csh
);
236 static size_t file_section_offset(const EFI_FFS_FILE_HEADER
*ffsfh
)
238 if (IS_FFS_FILE2(ffsfh
))
239 return sizeof(EFI_FFS_FILE_HEADER2
);
241 return sizeof(EFI_FFS_FILE_HEADER
);
244 static size_t ffs_file_size(const EFI_FFS_FILE_HEADER
*ffsfh
)
248 if (IS_FFS_FILE2(ffsfh
)) {
250 * this cast is needed with UEFI 2.6 headers in order
251 * to read the UINT32 value that FFS_FILE2_SIZE converts
254 uint32_t file2_size
= FFS_FILE2_SIZE(ffsfh
);
255 size
= read_le32(&file2_size
);
257 size
= read_le8(&ffsfh
->Size
[0]) << 0;
258 size
|= read_le8(&ffsfh
->Size
[1]) << 8;
259 size
|= read_le8(&ffsfh
->Size
[2]) << 16;
264 static int relocate_patch_table(void *fsp
, size_t size
, size_t offset
,
267 struct fsp_patch_table
*table
;
271 table
= relative_offset(fsp
, offset
);
273 if ((offset
+ sizeof(*table
) > size
) ||
274 (read_le16(&table
->header_length
) + offset
) > size
) {
275 printk(BIOS_ERR
, "FSPP not entirely contained in region.\n");
279 num_entries
= read_le32(&table
->patch_entry_num
);
280 printk(FSP_DBG_LVL
, "FSPP relocs: %zx\n", num_entries
);
282 for (num
= 0; num
< num_entries
; num
++) {
286 reloc
= fspp_reloc(fsp
, size
,
287 read_le32(&table
->patch_entries
[num
]));
290 printk(BIOS_ERR
, "Ignoring FSPP entry: %x\n",
291 read_le32(&table
->patch_entries
[num
]));
295 reloc_val
= read_le32(reloc
);
296 printk(FSP_DBG_LVL
, "Adjusting %p %x -> %x\n",
298 (unsigned int)(reloc_val
+ adjustment
));
300 write_le32(reloc
, reloc_val
+ adjustment
);
306 static ssize_t
relocate_remaining_items(void *fsp
, size_t size
,
307 uintptr_t new_addr
, size_t fih_offset
)
309 EFI_FFS_FILE_HEADER
*ffsfh
;
310 EFI_COMMON_SECTION_HEADER
*csh
;
311 FSP_INFO_HEADER
*fih
;
315 printk(FSP_DBG_LVL
, "FSP_INFO_HEADER offset is %zx\n", fih_offset
);
317 if (fih_offset
== 0) {
318 printk(BIOS_ERR
, "FSP_INFO_HEADER offset is 0.\n");
322 /* FSP_INFO_HEADER at first file in FV within first RAW section. */
323 ffsfh
= relative_offset(fsp
, fih_offset
);
324 fih_offset
+= file_section_offset(ffsfh
);
325 csh
= relative_offset(fsp
, fih_offset
);
326 fih_offset
+= section_data_offset(csh
);
327 fih
= relative_offset(fsp
, fih_offset
);
329 if (guid_compare(&ffsfh
->Name
, &fih_guid
)) {
330 printk(BIOS_ERR
, "Bad FIH GUID.\n");
334 if (read_le8(&csh
->Type
) != EFI_SECTION_RAW
) {
335 printk(BIOS_ERR
, "FIH file should have raw section: %x\n",
336 read_le8(&csh
->Type
));
340 if (read_le32(&fih
->Signature
) != FSP_SIG
) {
341 printk(BIOS_ERR
, "Unexpected FIH signature: %08x\n",
342 read_le32(&fih
->Signature
));
346 adjustment
= (intptr_t)new_addr
- read_le32(&fih
->ImageBase
);
348 /* Update ImageBase to reflect FSP's new home. */
349 write_le32(&fih
->ImageBase
, adjustment
+ read_le32(&fih
->ImageBase
));
351 /* Need to find patch table and adjust each entry. The tables
352 * following FSP_INFO_HEADER have a 32-bit signature and header
353 * length. The patch table is denoted as having a 'FSPP' signature;
354 * the table format doesn't follow the other tables. */
355 offset
= fih_offset
+ read_le32(&fih
->HeaderLength
);
356 while (offset
+ 2 * sizeof(uint32_t) <= size
) {
357 uint32_t *table_headers
;
359 table_headers
= relative_offset(fsp
, offset
);
361 printk(FSP_DBG_LVL
, "Checking offset %zx for 'FSPP'\n",
364 if (read_le32(&table_headers
[0]) != FSPP_SIG
) {
365 offset
+= read_le32(&table_headers
[1]);
369 if (relocate_patch_table(fsp
, size
, offset
, adjustment
)) {
370 printk(BIOS_ERR
, "FSPP relocation failed.\n");
377 printk(BIOS_ERR
, "Could not find the FSP patch table.\n");
381 static ssize_t
relocate_fvh(uintptr_t new_addr
, void *fsp
, size_t fsp_size
,
382 size_t fvh_offset
, size_t *fih_offset
)
384 EFI_FIRMWARE_VOLUME_HEADER
*fvh
;
385 EFI_FFS_FILE_HEADER
*ffsfh
;
386 EFI_COMMON_SECTION_HEADER
*csh
;
393 fvh
= relative_offset(fsp
, offset
);
395 if (read_le32(&fvh
->Signature
) != EFI_FVH_SIGNATURE
)
398 fv_length
= read_le64(&fvh
->FvLength
);
400 printk(FSP_DBG_LVL
, "FVH length: %zx Offset: %zx Mapping length: %zx\n",
401 fv_length
, offset
, fsp_size
);
403 if (fv_length
+ offset
> fsp_size
)
406 /* Parse only this FV. However, the algorithm uses offsets into the
407 * entire FSP region so make size include the starting offset. */
408 size
= fv_length
+ offset
;
410 if (guid_compare(&fvh
->FileSystemGuid
, &ffs2_guid
)) {
411 printk(BIOS_ERR
, "FVH not an FFS2 type.\n");
415 if (read_le16(&fvh
->ExtHeaderOffset
) != 0) {
416 EFI_FIRMWARE_VOLUME_EXT_HEADER
*fveh
;
418 offset
+= read_le16(&fvh
->ExtHeaderOffset
);
419 fveh
= relative_offset(fsp
, offset
);
420 printk(FSP_DBG_LVL
, "Extended Header Offset: %zx Size: %zx\n",
421 (size_t)read_le16(&fvh
->ExtHeaderOffset
),
422 (size_t)read_le32(&fveh
->ExtHeaderSize
));
423 offset
+= read_le32(&fveh
->ExtHeaderSize
);
424 /* FFS files are 8 byte aligned after extended header. */
425 offset
= ALIGN_UP(offset
, 8);
427 offset
+= read_le16(&fvh
->HeaderLength
);
430 file_offset
= offset
;
431 while (file_offset
+ sizeof(*ffsfh
) < size
) {
432 offset
= file_offset
;
433 printk(FSP_DBG_LVL
, "file offset: %zx\n", file_offset
);
435 /* First file and section should be FSP info header. */
436 if (fih_offset
!= NULL
&& *fih_offset
== 0)
437 *fih_offset
= file_offset
;
439 ffsfh
= relative_offset(fsp
, file_offset
);
441 printk(FSP_DBG_LVL
, "file type = %x\n", read_le8(&ffsfh
->Type
));
442 printk(FSP_DBG_LVL
, "file attribs = %x\n",
443 read_le8(&ffsfh
->Attributes
));
445 /* Exit FV relocation when empty space found */
446 if (read_le8(&ffsfh
->Type
) == EFI_FV_FILETYPE_FFS_MAX
)
449 /* Next file on 8 byte alignment. */
450 file_offset
+= ffs_file_size(ffsfh
);
451 file_offset
= ALIGN_UP(file_offset
, 8);
453 /* Padding files have no section information. */
454 if (read_le8(&ffsfh
->Type
) == EFI_FV_FILETYPE_FFS_PAD
)
457 offset
+= file_section_offset(ffsfh
);
459 while (offset
+ sizeof(*csh
) < file_offset
) {
463 csh
= relative_offset(fsp
, offset
);
465 printk(FSP_DBG_LVL
, "section offset: %zx\n", offset
);
466 printk(FSP_DBG_LVL
, "section type: %x\n",
467 read_le8(&csh
->Type
));
469 data_size
= section_data_size(csh
);
470 data_offset
= section_data_offset(csh
);
472 if (data_size
+ data_offset
+ offset
> file_offset
) {
473 printk(BIOS_ERR
, "Section exceeds FV size.\n");
478 * The entire FSP image can be thought of as one
479 * program with a single link address even though there
480 * are multiple TEs linked separately. The reason is
481 * that each TE is linked for XIP. So in order to
482 * relocate the TE properly we need to form the
483 * relocated address based on the TE offset within
486 if (read_le8(&csh
->Type
) == EFI_SECTION_TE
) {
488 size_t te_offset
= offset
+ data_offset
;
489 uintptr_t te_addr
= new_addr
+ te_offset
;
491 printk(FSP_DBG_LVL
, "TE image at offset %zx\n",
493 te
= relative_offset(fsp
, te_offset
);
494 te_relocate(te_addr
, te
);
497 offset
+= data_size
+ data_offset
;
498 /* Sections are aligned to 4 bytes. */
499 offset
= ALIGN_UP(offset
, 4);
503 /* Return amount of buffer parsed: FV size. */
507 ssize_t
fsp_component_relocate(uintptr_t new_addr
, void *fsp
, size_t size
)
514 while (offset
< size
) {
517 /* Relocate each FV within the FSP region. The FSP_INFO_HEADER
518 * should only be located in the first FV. */
520 nparsed
= relocate_fvh(new_addr
, fsp
, size
, offset
,
523 nparsed
= relocate_fvh(new_addr
, fsp
, size
, offset
,
526 /* FV should be larger than 0 or failed to parse. */
528 printk(BIOS_ERR
, "FV @ offset %zx relocation failed\n",
536 return relocate_remaining_items(fsp
, size
, new_addr
, fih_offset
);
539 ssize_t
fsp1_1_relocate(uintptr_t new_addr
, void *fsp
, size_t size
)
541 return fsp_component_relocate(new_addr
, fsp
, size
);