1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <console/console.h>
4 #include <commonlib/endian.h>
5 #include <commonlib/fsp.h>
8 * Intel's code does not have a handle on changing global packing state.
9 * Therefore, one needs to protect against packing policies that are set
10 * globally for a compilation unit just by including a header file.
14 /* Default bind FSP 1.1 API to edk2 UEFI 2.4 types. */
15 #include <vendorcode/intel/edk2/uefi_2.4/uefi_types.h>
16 #include <vendorcode/intel/fsp/fsp1_1/IntelFspPkg/Include/FspInfoHeader.h>
18 /* Restore original packing policy. */
21 #include <commonlib/helpers.h>
26 #define FSP_DBG_LVL BIOS_NEVER
27 #define MASK_24BITS 0x00FFFFFF
30 * UEFI defines everything as little endian. However, this piece of code
31 * can be integrated in a userland tool. That tool could be on a big endian
32 * machine so one needs to access the fields within UEFI structures using
33 * endian-aware accesses.
36 /* Return 0 if equal. Non-zero if not equal. */
37 static int guid_compare(const EFI_GUID
*le_guid
, const EFI_GUID
*native_guid
)
39 if (read_le32(&le_guid
->Data1
) != native_guid
->Data1
)
41 if (read_le16(&le_guid
->Data2
) != native_guid
->Data2
)
43 if (read_le16(&le_guid
->Data3
) != native_guid
->Data3
)
45 return memcmp(le_guid
->Data4
, native_guid
->Data4
,
46 ARRAY_SIZE(le_guid
->Data4
));
49 static const EFI_GUID ffs2_guid
= EFI_FIRMWARE_FILE_SYSTEM2_GUID
;
50 static const EFI_GUID fih_guid
= FSP_INFO_HEADER_GUID
;
52 struct fsp_patch_table
{
54 uint16_t header_length
;
55 uint8_t header_revision
;
57 uint32_t patch_entry_num
;
58 uint32_t patch_entries
[];
61 #define FSPP_SIG 0x50505346
63 static void *relative_offset(void *base
, ssize_t offset
)
67 loc
= (uintptr_t)base
;
73 static size_t csh_size(const EFI_COMMON_SECTION_HEADER
*csh
)
77 /* Unpack the array into a type that can be used. */
79 size
|= read_le8(&csh
->Size
[0]) << 0;
80 size
|= read_le8(&csh
->Size
[1]) << 8;
81 size
|= read_le8(&csh
->Size
[2]) << 16;
86 static size_t file_section_offset(const EFI_FFS_FILE_HEADER
*ffsfh
)
88 if (IS_FFS_FILE2(ffsfh
))
89 return sizeof(EFI_FFS_FILE_HEADER2
);
91 return sizeof(EFI_FFS_FILE_HEADER
);
94 static size_t section_data_offset(const EFI_COMMON_SECTION_HEADER
*csh
)
96 if (csh_size(csh
) == MASK_24BITS
)
97 return sizeof(EFI_COMMON_SECTION_HEADER2
);
99 return sizeof(EFI_COMMON_SECTION_HEADER
);
102 static uint32_t *fspp_reloc(void *fsp
, size_t fsp_size
, uint32_t e
)
106 /* Offsets live in bits 23:0. */
107 offset
= e
& MASK_24BITS
;
109 /* If bit 31 is set then the offset is considered a negative value
110 * relative to the end of the image using 16MiB as the offset's
113 offset
= fsp_size
- (16 * MiB
- offset
);
115 /* Determine if offset falls within fsp_size for a 32 bit relocation. */
116 if (offset
> fsp_size
- sizeof(uint32_t))
119 return relative_offset(fsp
, offset
);
122 static int reloc_type(uint16_t reloc_entry
)
124 /* Reloc type in upper 4 bits */
125 return reloc_entry
>> 12;
128 static size_t reloc_offset(uint16_t reloc_entry
)
130 /* Offsets are in low 12 bits. */
131 return reloc_entry
& ((1 << 12) - 1);
134 static FSP_INFO_HEADER
*fsp_get_info_hdr(void *fsp
, size_t fih_offset
)
136 EFI_FFS_FILE_HEADER
*ffsfh
;
137 EFI_COMMON_SECTION_HEADER
*csh
;
138 FSP_INFO_HEADER
*fih
;
140 printk(FSP_DBG_LVL
, "FSP_INFO_HEADER offset is %zx\n", fih_offset
);
142 if (fih_offset
== 0) {
143 printk(BIOS_ERR
, "FSP_INFO_HEADER offset is 0.\n");
147 /* FSP_INFO_HEADER is located at first file in FV within first RAW section. */
148 ffsfh
= relative_offset(fsp
, fih_offset
);
149 fih_offset
+= file_section_offset(ffsfh
);
150 csh
= relative_offset(fsp
, fih_offset
);
151 fih_offset
+= section_data_offset(csh
);
152 fih
= relative_offset(fsp
, fih_offset
);
154 if (guid_compare(&ffsfh
->Name
, &fih_guid
)) {
155 printk(BIOS_ERR
, "Bad FIH GUID.\n");
159 if (read_le8(&csh
->Type
) != EFI_SECTION_RAW
) {
160 printk(BIOS_ERR
, "FIH file should have raw section: %x\n",
161 read_le8(&csh
->Type
));
165 if (read_le32(&fih
->Signature
) != FSP_SIG
) {
166 printk(BIOS_ERR
, "Unexpected FIH signature: %08x\n",
167 read_le32(&fih
->Signature
));
174 static int pe_relocate(uintptr_t new_addr
, void *pe
, void *fsp
, size_t fih_off
)
176 EFI_IMAGE_NT_HEADERS32
*peih
;
177 EFI_IMAGE_DOS_HEADER
*doshdr
;
178 EFI_IMAGE_OPTIONAL_HEADER32
*ophdr
;
179 FSP_INFO_HEADER
*fih
;
180 uint32_t roffset
, rsize
;
182 uint8_t *pe_base
= pe
;
184 uint32_t img_base_off
;
188 if (read_le16(&doshdr
->e_magic
) != EFI_IMAGE_DOS_SIGNATURE
) {
189 printk(BIOS_ERR
, "Invalid DOS Header/magic\n");
193 peih
= relative_offset(pe
, doshdr
->e_lfanew
);
195 if (read_le32(&peih
->Signature
) != EFI_IMAGE_NT_SIGNATURE
) {
196 printk(BIOS_ERR
, "Invalid PE32 header\n");
200 ophdr
= &peih
->OptionalHeader
;
202 if (read_le16(&ophdr
->Magic
) != EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
203 printk(BIOS_ERR
, "No support for non-PE32 images\n");
207 fih
= fsp_get_info_hdr(fsp
, fih_off
);
209 printk(BIOS_ERR
, "No Image base found for FSP PE32\n");
212 image_base
= read_le32(&fih
->ImageBase
);
213 printk(FSP_DBG_LVL
, "FSP InfoHdr Image Base is %x\n", image_base
);
215 delta
= new_addr
- image_base
;
217 img_base_off
= read_le32(&ophdr
->ImageBase
);
218 printk(FSP_DBG_LVL
, "lfanew 0x%x, delta-0x%x, FSP Base 0x%x, NT32ImageBase 0x%x, offset 0x%x\n",
219 read_le32(&doshdr
->e_lfanew
),
220 delta
, image_base
, img_base_off
,
221 (uint32_t)((uint8_t *)&ophdr
->ImageBase
- pe_base
));
223 printk(FSP_DBG_LVL
, "relocating PE32 image at addr - 0x%" PRIxPTR
"\n", new_addr
);
224 rsize
= read_le32(&ophdr
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
].Size
);
225 roffset
= read_le32(&ophdr
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
].VirtualAddress
);
226 printk(FSP_DBG_LVL
, "relocation table at offset-%x,size=%x\n", roffset
, rsize
);
227 // TODO - add support for PE32+ also
230 while (offset
< (roffset
+ rsize
)) {
235 EFI_IMAGE_DATA_DIRECTORY
*relocd
;
237 relocd
= (void *)&pe_base
[offset
];
238 offset
+= sizeof(*relocd
);
239 // Read relocation type, offset pairs
240 rlen
= read_le32(&relocd
->Size
) - sizeof(*relocd
);
241 rnum
= rlen
/ sizeof(uint16_t);
242 vaddr
= read_le32(&relocd
->VirtualAddress
);
243 rdata
= (uint16_t *)&pe_base
[offset
];
244 printk(FSP_DBG_LVL
, "\t%d Relocs for RVA %x\n", rnum
, vaddr
);
246 for (i
= 0; i
< rnum
; i
++) {
247 uint16_t roff
= reloc_offset(rdata
[i
]);
248 uint16_t rtype
= reloc_type(rdata
[i
]);
249 uint32_t aoff
= vaddr
+ roff
;
251 printk(FSP_DBG_LVL
, "\t\treloc type %x offset %x aoff %x, base-0x%x\n",
252 rtype
, roff
, aoff
, img_base_off
);
254 case EFI_IMAGE_REL_BASED_ABSOLUTE
:
256 case EFI_IMAGE_REL_BASED_HIGHLOW
:
257 val
= read_le32(&pe_base
[aoff
]);
258 printk(FSP_DBG_LVL
, "Adjusting %p %x -> %x\n",
259 &pe_base
[aoff
], val
, val
+ delta
);
260 write_le32(&pe_base
[aoff
], val
+ delta
);
262 case EFI_IMAGE_REL_BASED_DIR64
:
263 printk(BIOS_ERR
, "Unsupported DIR64\n");
266 printk(BIOS_ERR
, "Unsupported relocation type %d\n",
271 offset
+= sizeof(*rdata
) * rnum
;
273 printk(FSP_DBG_LVL
, "Adjust Image Base %x->%x\n",
274 img_base_off
, img_base_off
+ delta
);
275 img_base_off
+= delta
;
276 write_le32(&ophdr
->ImageBase
, img_base_off
);
281 static int te_relocate(uintptr_t new_addr
, void *te
)
283 EFI_TE_IMAGE_HEADER
*teih
;
284 EFI_IMAGE_DATA_DIRECTORY
*relocd
;
285 EFI_IMAGE_BASE_RELOCATION
*relocb
;
286 uintptr_t image_base
;
290 size_t relocd_offset
;
296 if (read_le16(&teih
->Signature
) != EFI_TE_IMAGE_HEADER_SIGNATURE
) {
297 printk(BIOS_ERR
, "TE Signature mismatch: %x vs %x\n",
298 read_le16(&teih
->Signature
),
299 EFI_TE_IMAGE_HEADER_SIGNATURE
);
304 * A TE image is created by converting a PE file. Because of this
305 * the offsets within the headers are off. In order to calculate
306 * the correct relative offsets one needs to subtract fixup_offset
307 * from the encoded offsets. Similarly, the linked address of the
308 * program is found by adding the fixup_offset to the ImageBase.
310 fixup_offset
= read_le16(&teih
->StrippedSize
);
311 fixup_offset
-= sizeof(EFI_TE_IMAGE_HEADER
);
312 /* Keep track of a base that is correctly adjusted so that offsets
313 * can be used directly. */
315 te_base
-= fixup_offset
;
317 image_base
= read_le64(&teih
->ImageBase
);
318 adj
= new_addr
- (image_base
+ fixup_offset
);
320 printk(FSP_DBG_LVL
, "TE Image %p -> %p adjust value: %x\n",
321 (void *)image_base
, (void *)new_addr
, adj
);
323 /* Adjust ImageBase for consistency. */
324 write_le64(&teih
->ImageBase
, (uint32_t)(image_base
+ adj
));
326 relocd
= &teih
->DataDirectory
[EFI_TE_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
329 /* Though the field name is VirtualAddress it's actually relative to
330 * the beginning of the image which is linked at ImageBase. */
331 relocb
= relative_offset(te
,
332 read_le32(&relocd
->VirtualAddress
) - fixup_offset
);
333 while (relocd_offset
< read_le32(&relocd
->Size
)) {
334 size_t rva_offset
= read_le32(&relocb
->VirtualAddress
);
336 printk(FSP_DBG_LVL
, "Relocs for RVA offset %zx\n", rva_offset
);
337 num_relocs
= read_le32(&relocb
->SizeOfBlock
) - sizeof(*relocb
);
338 num_relocs
/= sizeof(uint16_t);
339 reloc
= relative_offset(relocb
, sizeof(*relocb
));
341 printk(FSP_DBG_LVL
, "Num relocs in block: %zx\n", num_relocs
);
343 while (num_relocs
> 0) {
344 uint16_t reloc_val
= read_le16(reloc
);
345 int type
= reloc_type(reloc_val
);
346 size_t offset
= reloc_offset(reloc_val
);
348 printk(FSP_DBG_LVL
, "reloc type %x offset %zx\n",
351 if (type
== EFI_IMAGE_REL_BASED_HIGHLOW
||
352 type
== EFI_IMAGE_REL_BASED_DIR64
) {
353 uint32_t *reloc_addr
;
356 offset
+= rva_offset
;
357 reloc_addr
= (void *)&te_base
[offset
];
358 val
= read_le32(reloc_addr
);
360 printk(FSP_DBG_LVL
, "Adjusting %p %x -> %x\n",
361 reloc_addr
, val
, val
+ adj
);
362 write_le32(reloc_addr
, val
+ adj
);
363 } else if (type
!= EFI_IMAGE_REL_BASED_ABSOLUTE
) {
364 printk(BIOS_ERR
, "Unknown reloc type: %x\n",
372 /* Track consumption of relocation directory contents. */
373 relocd_offset
+= read_le32(&relocb
->SizeOfBlock
);
374 /* Get next relocation block to process. */
375 relocb
= relative_offset(relocb
,
376 read_le32(&relocb
->SizeOfBlock
));
382 static size_t section_data_size(const EFI_COMMON_SECTION_HEADER
*csh
)
386 if (csh_size(csh
) == MASK_24BITS
)
387 section_size
= read_le32(&SECTION2_SIZE(csh
));
389 section_size
= csh_size(csh
);
391 return section_size
- section_data_offset(csh
);
394 static size_t ffs_file_size(const EFI_FFS_FILE_HEADER
*ffsfh
)
398 if (IS_FFS_FILE2(ffsfh
)) {
400 * this cast is needed with UEFI 2.6 headers in order
401 * to read the UINT32 value that FFS_FILE2_SIZE converts
404 uint32_t file2_size
= FFS_FILE2_SIZE(ffsfh
);
405 size
= read_le32(&file2_size
);
407 size
= read_le8(&ffsfh
->Size
[0]) << 0;
408 size
|= read_le8(&ffsfh
->Size
[1]) << 8;
409 size
|= read_le8(&ffsfh
->Size
[2]) << 16;
414 static int relocate_patch_table(void *fsp
, size_t size
, size_t offset
,
417 struct fsp_patch_table
*table
;
421 table
= relative_offset(fsp
, offset
);
423 if ((offset
+ sizeof(*table
) > size
) ||
424 (read_le16(&table
->header_length
) + offset
) > size
) {
425 printk(BIOS_ERR
, "FSPP not entirely contained in region.\n");
429 num_entries
= read_le32(&table
->patch_entry_num
);
430 printk(FSP_DBG_LVL
, "FSPP relocs: %zx\n", num_entries
);
432 for (num
= 0; num
< num_entries
; num
++) {
436 reloc
= fspp_reloc(fsp
, size
,
437 read_le32(&table
->patch_entries
[num
]));
440 printk(BIOS_ERR
, "Ignoring FSPP entry: %x\n",
441 read_le32(&table
->patch_entries
[num
]));
445 reloc_val
= read_le32(reloc
);
446 printk(FSP_DBG_LVL
, "Adjusting %p %x -> %x\n",
448 (unsigned int)(reloc_val
+ adjustment
));
450 write_le32(reloc
, reloc_val
+ adjustment
);
456 static ssize_t
relocate_remaining_items(void *fsp
, size_t size
,
457 uintptr_t new_addr
, size_t fih_offset
)
459 EFI_FFS_FILE_HEADER
*ffsfh
;
460 EFI_COMMON_SECTION_HEADER
*csh
;
461 FSP_INFO_HEADER
*fih
;
465 printk(FSP_DBG_LVL
, "FSP_INFO_HEADER offset is %zx\n", fih_offset
);
467 if (fih_offset
== 0) {
468 printk(BIOS_ERR
, "FSP_INFO_HEADER offset is 0.\n");
472 /* FSP_INFO_HEADER at first file in FV within first RAW section. */
473 ffsfh
= relative_offset(fsp
, fih_offset
);
474 fih_offset
+= file_section_offset(ffsfh
);
475 csh
= relative_offset(fsp
, fih_offset
);
476 fih_offset
+= section_data_offset(csh
);
477 fih
= relative_offset(fsp
, fih_offset
);
479 if (guid_compare(&ffsfh
->Name
, &fih_guid
)) {
480 printk(BIOS_ERR
, "Bad FIH GUID.\n");
484 if (read_le8(&csh
->Type
) != EFI_SECTION_RAW
) {
485 printk(BIOS_ERR
, "FIH file should have raw section: %x\n",
486 read_le8(&csh
->Type
));
490 if (read_le32(&fih
->Signature
) != FSP_SIG
) {
491 printk(BIOS_ERR
, "Unexpected FIH signature: %08x\n",
492 read_le32(&fih
->Signature
));
495 adjustment
= (intptr_t)new_addr
- read_le32(&fih
->ImageBase
);
497 /* Update ImageBase to reflect FSP's new home. */
498 write_le32(&fih
->ImageBase
, adjustment
+ read_le32(&fih
->ImageBase
));
499 printk(FSP_DBG_LVL
, "Updated FSP InfoHdr Image Base to %x\n",
500 read_le32(&fih
->ImageBase
));
502 /* Need to find patch table and adjust each entry. The tables
503 * following FSP_INFO_HEADER have a 32-bit signature and header
504 * length. The patch table is denoted as having a 'FSPP' signature;
505 * the table format doesn't follow the other tables. */
506 offset
= fih_offset
+ read_le32(&fih
->HeaderLength
);
507 while (offset
+ 2 * sizeof(uint32_t) <= size
) {
508 uint32_t *table_headers
;
510 table_headers
= relative_offset(fsp
, offset
);
512 printk(FSP_DBG_LVL
, "Checking offset %zx for 'FSPP'\n",
515 if (read_le32(&table_headers
[0]) != FSPP_SIG
) {
516 offset
+= read_le32(&table_headers
[1]);
520 if (relocate_patch_table(fsp
, size
, offset
, adjustment
)) {
521 printk(BIOS_ERR
, "FSPP relocation failed.\n");
528 printk(BIOS_ERR
, "Could not find the FSP patch table.\n");
532 static ssize_t
relocate_fvh(uintptr_t new_addr
, void *fsp
, size_t fsp_size
,
533 size_t fvh_offset
, size_t *fih_offset
)
535 EFI_FIRMWARE_VOLUME_HEADER
*fvh
;
536 EFI_FFS_FILE_HEADER
*ffsfh
;
537 EFI_COMMON_SECTION_HEADER
*csh
;
544 fvh
= relative_offset(fsp
, offset
);
546 if (read_le32(&fvh
->Signature
) != EFI_FVH_SIGNATURE
)
549 fv_length
= read_le64(&fvh
->FvLength
);
551 printk(FSP_DBG_LVL
, "FVH length: %zx Offset: %zx Mapping length: %zx\n",
552 fv_length
, offset
, fsp_size
);
554 if (fv_length
+ offset
> fsp_size
)
557 /* Parse only this FV. However, the algorithm uses offsets into the
558 * entire FSP region so make size include the starting offset. */
559 size
= fv_length
+ offset
;
561 if (guid_compare(&fvh
->FileSystemGuid
, &ffs2_guid
)) {
562 printk(BIOS_ERR
, "FVH not an FFS2 type.\n");
566 if (read_le16(&fvh
->ExtHeaderOffset
) != 0) {
567 EFI_FIRMWARE_VOLUME_EXT_HEADER
*fveh
;
569 offset
+= read_le16(&fvh
->ExtHeaderOffset
);
570 fveh
= relative_offset(fsp
, offset
);
571 printk(FSP_DBG_LVL
, "Extended Header Offset: %zx Size: %zx\n",
572 (size_t)read_le16(&fvh
->ExtHeaderOffset
),
573 (size_t)read_le32(&fveh
->ExtHeaderSize
));
574 offset
+= read_le32(&fveh
->ExtHeaderSize
);
575 /* FFS files are 8 byte aligned after extended header. */
576 offset
= ALIGN_UP(offset
, 8);
578 offset
+= read_le16(&fvh
->HeaderLength
);
581 file_offset
= offset
;
582 while (file_offset
+ sizeof(*ffsfh
) < size
) {
583 offset
= file_offset
;
584 printk(FSP_DBG_LVL
, "file offset: %zx\n", file_offset
);
586 /* First file and section should be FSP info header. */
587 if (*fih_offset
== 0)
588 *fih_offset
= file_offset
;
590 ffsfh
= relative_offset(fsp
, file_offset
);
592 printk(FSP_DBG_LVL
, "file type = %x\n", read_le8(&ffsfh
->Type
));
593 printk(FSP_DBG_LVL
, "file attribs = %x\n",
594 read_le8(&ffsfh
->Attributes
));
596 /* Exit FV relocation when empty space found */
597 if (read_le8(&ffsfh
->Type
) == EFI_FV_FILETYPE_FFS_MAX
)
600 /* Next file on 8 byte alignment. */
601 file_offset
+= ffs_file_size(ffsfh
);
602 file_offset
= ALIGN_UP(file_offset
, 8);
604 /* Padding files have no section information. */
605 if (read_le8(&ffsfh
->Type
) == EFI_FV_FILETYPE_FFS_PAD
)
608 offset
+= file_section_offset(ffsfh
);
610 while (offset
+ sizeof(*csh
) < file_offset
) {
614 size_t section_offset
;
615 uintptr_t section_addr
;
617 csh
= relative_offset(fsp
, offset
);
619 printk(FSP_DBG_LVL
, "section offset: %zx\n", offset
);
620 printk(FSP_DBG_LVL
, "section type: %x\n",
621 read_le8(&csh
->Type
));
623 data_size
= section_data_size(csh
);
624 data_offset
= section_data_offset(csh
);
626 if (data_size
+ data_offset
+ offset
> file_offset
) {
627 printk(BIOS_ERR
, "Section exceeds FV size.\n");
632 * The entire FSP image can be thought of as one
633 * program with a single link address even though there
634 * are multiple TEs linked separately. The reason is
635 * that each TE is linked for XIP. So in order to
636 * relocate the TE properly we need to form the
637 * relocated address based on the TE offset within
640 section_offset
= offset
+ data_offset
;
641 section_addr
= new_addr
+ section_offset
;
642 section_data
= relative_offset(fsp
, section_offset
);
644 if (read_le8(&csh
->Type
) == EFI_SECTION_TE
) {
645 printk(FSP_DBG_LVL
, "TE image at offset %zx\n",
647 te_relocate(section_addr
, section_data
);
648 } else if (read_le8(&csh
->Type
) == EFI_SECTION_PE32
) {
649 printk(FSP_DBG_LVL
, "PE32 image at offset %zx\n",
651 pe_relocate(new_addr
, section_data
, fsp
, *fih_offset
);
654 offset
+= data_size
+ data_offset
;
655 /* Sections are aligned to 4 bytes. */
656 offset
= ALIGN_UP(offset
, 4);
660 /* Return amount of buffer parsed: FV size. */
664 ssize_t
fsp_component_relocate(uintptr_t new_addr
, void *fsp
, size_t size
)
671 while (offset
< size
) {
674 /* Relocate each FV within the FSP region. */
675 nparsed
= relocate_fvh(new_addr
, fsp
, size
, offset
, &fih_offset
);
677 /* FV should be larger than 0 or failed to parse. */
679 printk(BIOS_ERR
, "FV @ offset %zx relocation failed\n",
687 return relocate_remaining_items(fsp
, size
, new_addr
, fih_offset
);
690 ssize_t
fsp1_1_relocate(uintptr_t new_addr
, void *fsp
, size_t size
)
692 return fsp_component_relocate(new_addr
, fsp
, size
);