summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/drivers/intel/fsp1_1/Makefile.inc1
-rw-r--r--src/drivers/intel/fsp1_1/fsp1_1_relocate.c539
-rw-r--r--src/drivers/intel/fsp1_1/fsp_relocate.c519
-rw-r--r--src/drivers/intel/fsp1_1/include/fsp/util.h6
4 files changed, 547 insertions, 518 deletions
diff --git a/src/drivers/intel/fsp1_1/Makefile.inc b/src/drivers/intel/fsp1_1/Makefile.inc
index bab68e142d..dffb6d2079 100644
--- a/src/drivers/intel/fsp1_1/Makefile.inc
+++ b/src/drivers/intel/fsp1_1/Makefile.inc
@@ -23,6 +23,7 @@ romstage-y += hob.c
ramstage-$(CONFIG_GOP_SUPPORT) += fsp_gop.c
ramstage-y += fsp_relocate.c
+ramstage-y += fsp1_1_relocate.c
ramstage-y += fsp_util.c
ramstage-y += hob.c
diff --git a/src/drivers/intel/fsp1_1/fsp1_1_relocate.c b/src/drivers/intel/fsp1_1/fsp1_1_relocate.c
new file mode 100644
index 0000000000..d57a18c4e1
--- /dev/null
+++ b/src/drivers/intel/fsp1_1/fsp1_1_relocate.c
@@ -0,0 +1,539 @@
+/*
+ * This file is part of the coreboot project.
+ *
+ * Copyright 2015 Google Inc
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc.
+ */
+
+#include <console/console.h>
+#include <endian.h>
+#include <fsp/api.h>
+#include <fsp/util.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+
+#define FSP_DBG_LVL BIOS_NEVER
+
+/*
+ * UEFI defines everything as little endian. However, this piece of code
+ * can be integrated in a userland tool. That tool could be on a big endian
+ * machine so one needs to access the fields within UEFI structures using
+ * endian-aware accesses.
+ */
+
+/* Return 0 if equal. Non-zero if not equal. */
+static int guid_compare(const EFI_GUID *le_guid, const EFI_GUID *native_guid)
+{
+ if (le32toh(le_guid->Data1) != native_guid->Data1)
+ return 1;
+ if (le16toh(le_guid->Data2) != native_guid->Data2)
+ return 1;
+ if (le16toh(le_guid->Data3) != native_guid->Data3)
+ return 1;
+ return memcmp(le_guid->Data4, native_guid->Data4,
+ ARRAY_SIZE(le_guid->Data4));
+}
+
+/* Provide this for symmetry when accessing UEFI fields. */
+static inline uint8_t le8toh(uint8_t byte)
+{
+ return byte;
+}
+
+static const EFI_GUID ffs2_guid = EFI_FIRMWARE_FILE_SYSTEM2_GUID;
+static const EFI_GUID fih_guid = FSP_INFO_HEADER_GUID;
+
+struct fsp_patch_table {
+ uint32_t signature;
+ uint16_t header_length;
+ uint8_t header_revision;
+ uint8_t reserved;
+ uint32_t patch_entry_num;
+ uint32_t patch_entries[0];
+} __attribute__((packed));
+
+#define FSPP_SIG 0x50505346
+
+static void *relative_offset(void *base, ssize_t offset)
+{
+ uintptr_t loc;
+
+ loc = (uintptr_t)base;
+ loc += offset;
+
+ return (void *)loc;
+}
+
+static uint32_t *fspp_reloc(void *fsp, size_t fsp_size, uint32_t e)
+{
+ size_t offset;
+
+ /* Offsets live in bits 23:0. */
+ offset = e & 0xffffff;
+
+ /* If bit 31 is set then the offset is considered a negative value
+ * relative to the end of the image using 16MiB as the offset's
+ * reference. */
+ if (e & (1 << 31))
+ offset = fsp_size - (16 * MiB - offset);
+
+ /* Determine if offset falls within fsp_size for a 32 bit relocation. */
+ if (offset > fsp_size - sizeof(uint32_t))
+ return NULL;
+
+ return relative_offset(fsp, offset);
+}
+
+static int reloc_type(uint16_t reloc_entry)
+{
+ /* Reloc type in upper 4 bits */
+ return reloc_entry >> 12;
+}
+
+static size_t reloc_offset(uint16_t reloc_entry)
+{
+ /* Offsets are in low 12 bits. */
+ return reloc_entry & ((1 << 12) - 1);
+}
+
+static int te_relocate(uintptr_t new_addr, void *te, size_t size)
+{
+ EFI_TE_IMAGE_HEADER *teih;
+ EFI_IMAGE_DATA_DIRECTORY *relocd;
+ EFI_IMAGE_BASE_RELOCATION *relocb;
+ uintptr_t image_base;
+ size_t fixup_offset;
+ size_t num_relocs;
+ uint16_t *reloc;
+ size_t relocd_offset;
+ uint8_t *te_base;
+ uint32_t adj;
+
+ teih = te;
+
+ if (le16toh(teih->Signature) != EFI_TE_IMAGE_HEADER_SIGNATURE) {
+ printk(BIOS_ERR, "TE Signature mismatch: %x vs %x\n",
+ le16toh(teih->Signature),
+ EFI_TE_IMAGE_HEADER_SIGNATURE);
+ return -1;
+ }
+
+ /*
+ * A TE image is created by converting a PE file. Because of this
+ * the offsets within the headers are off. In order to calculate
+ * the correct releative offets one needs to subtract fixup_offset
+ * from the encoded offets. Similarly, the linked address of the
+ * program is found by adding the fixup_offset to the ImageBase.
+ */
+ fixup_offset = le16toh(teih->StrippedSize);
+ fixup_offset -= sizeof(EFI_TE_IMAGE_HEADER);
+ /* Keep track of a base that is correctly adjusted so that offsets
+ * can be used directly. */
+ te_base = te;
+ te_base -= fixup_offset;
+
+ image_base = le64toh(teih->ImageBase);
+ adj = new_addr - (image_base + fixup_offset);
+
+ printk(FSP_DBG_LVL, "TE Image %p -> %p adjust value: %x\n",
+ (void *)image_base, (void *)new_addr, adj);
+
+ /* Adjust ImageBase for consistency. */
+ teih->ImageBase = htole32(image_base + adj);
+
+ relocd = &teih->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_BASERELOC];
+
+ relocd_offset = 0;
+ /* Though the field name is VirtualAddress it's actually relative to
+ * the beginning of the image which is linked at ImageBase. */
+ relocb = relative_offset(te,
+ le32toh(relocd->VirtualAddress) - fixup_offset);
+ while (relocd_offset < relocd->Size) {
+ size_t rva_offset = le32toh(relocb->VirtualAddress);
+
+ printk(FSP_DBG_LVL, "Relocs for RVA offset %zx\n", rva_offset);
+ num_relocs = le32toh(relocb->SizeOfBlock) - sizeof(*relocb);
+ num_relocs /= sizeof(uint16_t);
+ reloc = relative_offset(relocb, sizeof(*relocb));
+
+ printk(FSP_DBG_LVL, "Num relocs in block: %zx\n", num_relocs);
+
+ while (num_relocs > 0) {
+ uint16_t reloc_val = le16toh(*reloc);
+ int type = reloc_type(reloc_val);
+ size_t offset = reloc_offset(reloc_val);
+
+ printk(FSP_DBG_LVL, "reloc type %x offset %zx\n",
+ type, offset);
+
+ if (type == EFI_IMAGE_REL_BASED_HIGHLOW) {
+ uint32_t *reloc_addr;
+ uint32_t val;
+
+ offset += rva_offset;
+ reloc_addr = (void *)&te_base[offset];
+ val = le32toh(*reloc_addr);
+
+ printk(FSP_DBG_LVL, "Adjusting %p %x -> %x\n",
+ reloc_addr, val, val + adj);
+ *reloc_addr = htole32(val + adj);
+ } else if (type != EFI_IMAGE_REL_BASED_ABSOLUTE) {
+ printk(BIOS_ERR, "Unknown reloc type: %x\n",
+ type);
+ return -1;
+ }
+ num_relocs--;
+ reloc++;
+ }
+
+ /* Track consumption of relocation directory contents. */
+ relocd_offset += le32toh(relocb->SizeOfBlock);
+ /* Get next relocation block to process. */
+ relocb = relative_offset(relocb, le32toh(relocb->SizeOfBlock));
+ }
+
+ return 0;
+}
+
+static size_t csh_size(const EFI_COMMON_SECTION_HEADER *csh)
+{
+ size_t size;
+
+ /* Unpack the array into a type that can be used. */
+ size = 0;
+ size |= le8toh(csh->Size[0]) << 0;
+ size |= le8toh(csh->Size[1]) << 8;
+ size |= le8toh(csh->Size[2]) << 16;
+
+ return size;
+}
+
+static size_t section_data_offset(const EFI_COMMON_SECTION_HEADER *csh)
+{
+ if (csh_size(csh) == 0x00ffffff)
+ return sizeof(EFI_COMMON_SECTION_HEADER2);
+ else
+ return sizeof(EFI_COMMON_SECTION_HEADER);
+}
+
+static size_t section_data_size(const EFI_COMMON_SECTION_HEADER *csh)
+{
+ size_t section_size;
+
+ if (csh_size(csh) == 0x00ffffff)
+ section_size = le32toh(SECTION2_SIZE(csh));
+ else
+ section_size = csh_size(csh);
+
+ return section_size - section_data_offset(csh);
+}
+
+static size_t file_section_offset(const EFI_FFS_FILE_HEADER *ffsfh)
+{
+ if (IS_FFS_FILE2(ffsfh))
+ return sizeof(EFI_FFS_FILE_HEADER2);
+ else
+ return sizeof(EFI_FFS_FILE_HEADER);
+}
+
+static size_t ffs_file_size(const EFI_FFS_FILE_HEADER *ffsfh)
+{
+ size_t size;
+
+ if (IS_FFS_FILE2(ffsfh))
+ size = le32toh(FFS_FILE2_SIZE(ffsfh));
+ else {
+ size = le8toh(ffsfh->Size[0]) << 0;
+ size |= le8toh(ffsfh->Size[1]) << 8;
+ size |= le8toh(ffsfh->Size[2]) << 16;
+ }
+ return size;
+}
+
+static int relocate_patch_table(void *fsp, size_t size, size_t offset,
+ ssize_t adjustment)
+{
+ struct fsp_patch_table *table;
+ size_t num;
+ size_t num_entries;
+
+ table = relative_offset(fsp, offset);
+
+ if ((offset + sizeof(*table) > size) ||
+ (le16toh(table->header_length) + offset) > size) {
+ printk(BIOS_ERR, "FSPP not entirely contained in region.\n");
+ return -1;
+ }
+
+ num_entries = le32toh(table->patch_entry_num);
+ printk(FSP_DBG_LVL, "FSPP relocs: %zx\n", num_entries);
+
+ for (num = 0; num < table->patch_entry_num; num++) {
+ uint32_t *reloc;
+ uint32_t reloc_val;
+
+ reloc = fspp_reloc(fsp, size,
+ le32toh(table->patch_entries[num]));
+
+ if (reloc == NULL) {
+ printk(BIOS_ERR, "Ignoring FSPP entry: %x\n",
+ le32toh(table->patch_entries[num]));
+ continue;
+ }
+
+ reloc_val = le32toh(*reloc);
+ printk(FSP_DBG_LVL, "Adjusting %p %x -> %x\n",
+ reloc, reloc_val,
+ (unsigned int)(reloc_val + adjustment));
+
+ *reloc = htole32(reloc_val + adjustment);
+ }
+
+ return 0;
+}
+
+static ssize_t relocate_remaining_items(void *fsp, size_t size,
+ uintptr_t new_addr, size_t fih_offset)
+{
+ EFI_FFS_FILE_HEADER *ffsfh;
+ EFI_COMMON_SECTION_HEADER *csh;
+ FSP_INFO_HEADER *fih;
+ ssize_t adjustment;
+ size_t offset;
+
+ printk(FSP_DBG_LVL, "FSP_INFO_HEADER offset is %zx\n", fih_offset);
+
+ if (fih_offset == 0) {
+ printk(BIOS_ERR, "FSP_INFO_HEADER offset is 0.\n");
+ return -1;
+ }
+
+ /* FSP_INFO_HEADER at first file in FV within first RAW section. */
+ ffsfh = relative_offset(fsp, fih_offset);
+ fih_offset += file_section_offset(ffsfh);
+ csh = relative_offset(fsp, fih_offset);
+ fih_offset += section_data_offset(csh);
+ fih = relative_offset(fsp, fih_offset);
+
+ if (guid_compare(&ffsfh->Name, &fih_guid)) {
+ printk(BIOS_ERR, "Bad FIH GUID.\n");
+ return -1;
+ }
+
+ if (le8toh(csh->Type) != EFI_SECTION_RAW) {
+ printk(BIOS_ERR, "FIH file should have raw section: %x\n",
+ csh->Type);
+ return -1;
+ }
+
+ if (le32toh(fih->Signature) != FSP_SIG) {
+ printk(BIOS_ERR, "Unexpected FIH signature: %08x\n",
+ le32toh(fih->Signature));
+ return -1;
+ }
+
+ adjustment = (intptr_t)new_addr - le32toh(fih->ImageBase);
+
+ /* Update ImageBase to reflect FSP's new home. */
+ fih->ImageBase = htole32(adjustment + le32toh(fih->ImageBase));
+
+ /* Need to find patch table and adjust each entry. The tables
+ * following FSP_INFO_HEADER have a 32-bit signature and header
+ * length. The patch table is denoted as having a 'FSPP' signature;
+ * the table format doesn't follow the other tables. */
+ offset = fih_offset + le32toh(fih->HeaderLength);
+ while (offset + 2 * sizeof(uint32_t) <= size) {
+ uint32_t *table_headers;
+
+ table_headers = relative_offset(fsp, offset);
+
+ printk(FSP_DBG_LVL, "Checking offset %zx for 'FSPP'\n",
+ offset);
+
+ if (le32toh(table_headers[0]) != FSPP_SIG) {
+ offset += le32toh(table_headers[1]);
+ continue;
+ }
+
+ if (relocate_patch_table(fsp, size, offset, adjustment)) {
+ printk(BIOS_ERR, "FSPP relocation failed.\n");
+ return -1;
+ }
+
+ return fih_offset;
+ }
+
+ printk(BIOS_ERR, "Could not find the FSP patch table.\n");
+ return -1;
+}
+
+static ssize_t relocate_fvh(uintptr_t new_addr, void *fsp, size_t fsp_size,
+ size_t fvh_offset, size_t *fih_offset)
+{
+ EFI_FIRMWARE_VOLUME_HEADER *fvh;
+ EFI_FFS_FILE_HEADER *ffsfh;
+ EFI_COMMON_SECTION_HEADER *csh;
+ size_t offset;
+ size_t file_offset;
+ size_t size;
+ size_t fv_length;
+
+ offset = fvh_offset;
+ fvh = relative_offset(fsp, offset);
+
+ if (le32toh(fvh->Signature) != EFI_FVH_SIGNATURE)
+ return -1;
+
+ fv_length = le64toh(fvh->FvLength);
+
+ printk(FSP_DBG_LVL, "FVH length: %zx Offset: %zx Mapping length: %zx\n",
+ fv_length, offset, fsp_size);
+
+ if (fvh->FvLength + offset > fsp_size)
+ return -1;
+
+ /* Parse only this FV. However, the algorithm uses offsets into the
+ * entire FSP region so make size include the starting offset. */
+ size = fv_length + offset;
+
+ if (guid_compare(&fvh->FileSystemGuid, &ffs2_guid)) {
+ printk(BIOS_ERR, "FVH not an FFS2 type.\n");
+ return -1;
+ }
+
+ if (le16toh(fvh->ExtHeaderOffset) != 0) {
+ EFI_FIRMWARE_VOLUME_EXT_HEADER *fveh;
+
+ offset += le16toh(fvh->ExtHeaderOffset);
+ fveh = relative_offset(fsp, offset);
+ printk(FSP_DBG_LVL, "Extended Header Offset: %zx Size: %zx\n",
+ (size_t)le16toh(fvh->ExtHeaderOffset),
+ (size_t)le32toh(fveh->ExtHeaderSize));
+ offset += le32toh(fveh->ExtHeaderSize);
+ /* FFS files are 8 byte aligned after extended header. */
+ offset = ALIGN_UP(offset, 8);
+ } else {
+ offset += le16toh(fvh->HeaderLength);
+ }
+
+ file_offset = offset;
+ while (file_offset + sizeof(*ffsfh) < size) {
+ offset = file_offset;
+ printk(FSP_DBG_LVL, "file offset: %zx\n", file_offset);
+
+ /* First file and section should be FSP info header. */
+ if (fih_offset != NULL && *fih_offset == 0)
+ *fih_offset = file_offset;
+
+ ffsfh = relative_offset(fsp, file_offset);
+
+ printk(FSP_DBG_LVL, "file type = %x\n", le8toh(ffsfh->Type));
+ printk(FSP_DBG_LVL, "file attribs = %x\n",
+ le8toh(ffsfh->Attributes));
+
+ /* Exit FV relocation when empty space found */
+ if (le8toh(ffsfh->Type) == EFI_FV_FILETYPE_FFS_MAX)
+ break;
+
+ /* Next file on 8 byte alignment. */
+ file_offset += ffs_file_size(ffsfh);
+ file_offset = ALIGN_UP(file_offset, 8);
+
+ /* Padding files have no section information. */
+ if (le8toh(ffsfh->Type) == EFI_FV_FILETYPE_FFS_PAD)
+ continue;
+
+ offset += file_section_offset(ffsfh);
+
+ while (offset + sizeof(*csh) < file_offset) {
+ size_t data_size;
+ size_t data_offset;
+
+ csh = relative_offset(fsp, offset);
+
+ printk(FSP_DBG_LVL, "section offset: %zx\n", offset);
+ printk(FSP_DBG_LVL, "section type: %x\n",
+ le8toh(csh->Type));
+
+ data_size = section_data_size(csh);
+ data_offset = section_data_offset(csh);
+
+ if (data_size + data_offset + offset > file_offset) {
+ printk(BIOS_ERR, "Section exceeds FV size.\n");
+ return -1;
+ }
+
+ /*
+ * The entire FSP 1.1 image can be thought of as one
+ * program with a single link address even though there
+ * are multiple TEs linked separately. The reason is
+ * that each TE is linked for XIP. So in order to
+ * relocate the TE properly we need to form the
+ * relocated address based on the TE offset within
+ * FSP proper.
+ */
+ if (le8toh(csh->Type) == EFI_SECTION_TE) {
+ void *te;
+ size_t te_offset = offset + data_offset;
+ uintptr_t te_addr = new_addr + te_offset;
+
+ printk(FSP_DBG_LVL, "TE image at offset %zx\n",
+ te_offset);
+ te = relative_offset(fsp, te_offset);
+ te_relocate(te_addr, te, data_size);
+ }
+
+ offset += data_size + data_offset;
+ /* Sections are aligned to 4 bytes. */
+ offset = ALIGN_UP(offset, 4);
+ }
+ }
+
+ /* Return amount of buffer parsed: FV size. */
+ return fv_length;
+}
+
+ssize_t fsp1_1_relocate(uintptr_t new_addr, void *fsp, size_t size)
+{
+ size_t offset;
+ size_t fih_offset;
+
+ offset = 0;
+ fih_offset = 0;
+ while (offset < size) {
+ ssize_t nparsed;
+
+ /* Relocate each FV within the FSP region. The FSP_INFO_HEADER
+ * should only be located in the first FV. */
+ if (offset == 0)
+ nparsed = relocate_fvh(new_addr, fsp, size, offset,
+ &fih_offset);
+ else
+ nparsed = relocate_fvh(new_addr, fsp, size, offset,
+ NULL);
+
+ /* FV should be larger than 0 or failed to parse. */
+ if (nparsed <= 0) {
+ printk(BIOS_ERR, "FV @ offset %zx relocation failed\n",
+ offset);
+ return -1;
+ }
+
+ offset += nparsed;
+ }
+
+ return relocate_remaining_items(fsp, size, new_addr, fih_offset);
+}
diff --git a/src/drivers/intel/fsp1_1/fsp_relocate.c b/src/drivers/intel/fsp1_1/fsp_relocate.c
index 6e2a2a7e55..da0b341491 100644
--- a/src/drivers/intel/fsp1_1/fsp_relocate.c
+++ b/src/drivers/intel/fsp1_1/fsp_relocate.c
@@ -19,524 +19,7 @@
#include <console/console.h>
#include <cbmem.h>
-#include <endian.h>
#include <fsp/util.h>
-#include <stdlib.h>
-#include <stdint.h>
-#include <string.h>
-
-#define FSP_DBG_LVL BIOS_NEVER
-
-/*
- * UEFI defines everything as little endian. However, this piece of code
- * can be integrated in a userland tool. That tool could be on a big endian
- * machine so one needs to access the fields within UEFI structures using
- * endian-aware accesses.
- */
-
-/* Return 0 if equal. Non-zero if not equal. */
-static int guid_compare(const EFI_GUID *le_guid, const EFI_GUID *native_guid)
-{
- if (le32toh(le_guid->Data1) != native_guid->Data1)
- return 1;
- if (le16toh(le_guid->Data2) != native_guid->Data2)
- return 1;
- if (le16toh(le_guid->Data3) != native_guid->Data3)
- return 1;
- return memcmp(le_guid->Data4, native_guid->Data4,
- ARRAY_SIZE(le_guid->Data4));
-}
-
-/* Provide this for symmetry when accessing UEFI fields. */
-static inline uint8_t le8toh(uint8_t byte)
-{
- return byte;
-}
-
-static const EFI_GUID ffs2_guid = EFI_FIRMWARE_FILE_SYSTEM2_GUID;
-static const EFI_GUID fih_guid = FSP_INFO_HEADER_GUID;
-
-struct fsp_patch_table {
- uint32_t signature;
- uint16_t header_length;
- uint8_t header_revision;
- uint8_t reserved;
- uint32_t patch_entry_num;
- uint32_t patch_entries[0];
-} __attribute__((packed));
-
-#define FSPP_SIG 0x50505346
-
-static void *relative_offset(void *base, ssize_t offset)
-{
- uintptr_t loc;
-
- loc = (uintptr_t)base;
- loc += offset;
-
- return (void *)loc;
-}
-
-static uint32_t *fspp_reloc(void *fsp, size_t fsp_size, uint32_t e)
-{
- size_t offset;
-
- /* Offsets live in bits 23:0. */
- offset = e & 0xffffff;
-
- /* If bit 31 is set then the offset is considered a negative value
- * relative to the end of the image using 16MiB as the offset's
- * reference. */
- if (e & (1 << 31))
- offset = fsp_size - (16 * MiB - offset);
-
- /* Determine if offset falls within fsp_size for a 32 bit relocation. */
- if (offset > fsp_size - sizeof(uint32_t))
- return NULL;
-
- return relative_offset(fsp, offset);
-}
-
-static int reloc_type(uint16_t reloc_entry)
-{
- /* Reloc type in upper 4 bits */
- return reloc_entry >> 12;
-}
-
-static size_t reloc_offset(uint16_t reloc_entry)
-{
- /* Offsets are in low 12 bits. */
- return reloc_entry & ((1 << 12) - 1);
-}
-
-static int te_relocate(uintptr_t new_addr, void *te, size_t size)
-{
- EFI_TE_IMAGE_HEADER *teih;
- EFI_IMAGE_DATA_DIRECTORY *relocd;
- EFI_IMAGE_BASE_RELOCATION *relocb;
- uintptr_t image_base;
- size_t fixup_offset;
- size_t num_relocs;
- uint16_t *reloc;
- size_t relocd_offset;
- uint8_t *te_base;
- uint32_t adj;
-
- teih = te;
-
- if (le16toh(teih->Signature) != EFI_TE_IMAGE_HEADER_SIGNATURE) {
- printk(BIOS_ERR, "TE Signature mismatch: %x vs %x\n",
- le16toh(teih->Signature),
- EFI_TE_IMAGE_HEADER_SIGNATURE);
- return -1;
- }
-
- /*
- * A TE image is created by converting a PE file. Because of this
- * the offsets within the headers are off. In order to calculate
- * the correct releative offets one needs to subtract fixup_offset
- * from the encoded offets. Similarly, the linked address of the
- * program is found by adding the fixup_offset to the ImageBase.
- */
- fixup_offset = le16toh(teih->StrippedSize);
- fixup_offset -= sizeof(EFI_TE_IMAGE_HEADER);
- /* Keep track of a base that is correctly adjusted so that offsets
- * can be used directly. */
- te_base = te;
- te_base -= fixup_offset;
-
- image_base = le64toh(teih->ImageBase);
- adj = new_addr - (image_base + fixup_offset);
-
- printk(FSP_DBG_LVL, "TE Image %p -> %p adjust value: %x\n",
- (void *)image_base, (void *)new_addr, adj);
-
- /* Adjust ImageBase for consistency. */
- teih->ImageBase = htole32(image_base + adj);
-
- relocd = &teih->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_BASERELOC];
-
- relocd_offset = 0;
- /* Though the field name is VirtualAddress it's actually relative to
- * the beginning of the image which is linked at ImageBase. */
- relocb = relative_offset(te,
- le32toh(relocd->VirtualAddress) - fixup_offset);
- while (relocd_offset < relocd->Size) {
- size_t rva_offset = le32toh(relocb->VirtualAddress);
-
- printk(FSP_DBG_LVL, "Relocs for RVA offset %zx\n", rva_offset);
- num_relocs = le32toh(relocb->SizeOfBlock) - sizeof(*relocb);
- num_relocs /= sizeof(uint16_t);
- reloc = relative_offset(relocb, sizeof(*relocb));
-
- printk(FSP_DBG_LVL, "Num relocs in block: %zx\n", num_relocs);
-
- while (num_relocs > 0) {
- uint16_t reloc_val = le16toh(*reloc);
- int type = reloc_type(reloc_val);
- size_t offset = reloc_offset(reloc_val);
-
- printk(FSP_DBG_LVL, "reloc type %x offset %zx\n",
- type, offset);
-
- if (type == EFI_IMAGE_REL_BASED_HIGHLOW) {
- uint32_t *reloc_addr;
- uint32_t val;
-
- offset += rva_offset;
- reloc_addr = (void *)&te_base[offset];
- val = le32toh(*reloc_addr);
-
- printk(FSP_DBG_LVL, "Adjusting %p %x -> %x\n",
- reloc_addr, val, val + adj);
- *reloc_addr = htole32(val + adj);
- } else if (type != EFI_IMAGE_REL_BASED_ABSOLUTE) {
- printk(BIOS_ERR, "Unknown reloc type: %x\n",
- type);
- return -1;
- }
- num_relocs--;
- reloc++;
- }
-
- /* Track consumption of relocation directory contents. */
- relocd_offset += le32toh(relocb->SizeOfBlock);
- /* Get next relocation block to process. */
- relocb = relative_offset(relocb, le32toh(relocb->SizeOfBlock));
- }
-
- return 0;
-}
-
-static size_t csh_size(const EFI_COMMON_SECTION_HEADER *csh)
-{
- size_t size;
-
- /* Unpack the array into a type that can be used. */
- size = 0;
- size |= le8toh(csh->Size[0]) << 0;
- size |= le8toh(csh->Size[1]) << 8;
- size |= le8toh(csh->Size[2]) << 16;
-
- return size;
-}
-
-static size_t section_data_offset(const EFI_COMMON_SECTION_HEADER *csh)
-{
- if (csh_size(csh) == 0x00ffffff)
- return sizeof(EFI_COMMON_SECTION_HEADER2);
- else
- return sizeof(EFI_COMMON_SECTION_HEADER);
-}
-
-static size_t section_data_size(const EFI_COMMON_SECTION_HEADER *csh)
-{
- size_t section_size;
-
- if (csh_size(csh) == 0x00ffffff)
- section_size = le32toh(SECTION2_SIZE(csh));
- else
- section_size = csh_size(csh);
-
- return section_size - section_data_offset(csh);
-}
-
-static size_t file_section_offset(const EFI_FFS_FILE_HEADER *ffsfh)
-{
- if (IS_FFS_FILE2(ffsfh))
- return sizeof(EFI_FFS_FILE_HEADER2);
- else
- return sizeof(EFI_FFS_FILE_HEADER);
-}
-
-static size_t ffs_file_size(const EFI_FFS_FILE_HEADER *ffsfh)
-{
- size_t size;
-
- if (IS_FFS_FILE2(ffsfh))
- size = le32toh(FFS_FILE2_SIZE(ffsfh));
- else {
- size = le8toh(ffsfh->Size[0]) << 0;
- size |= le8toh(ffsfh->Size[1]) << 8;
- size |= le8toh(ffsfh->Size[2]) << 16;
- }
- return size;
-}
-
-static int relocate_patch_table(void *fsp, size_t size, size_t offset,
- ssize_t adjustment)
-{
- struct fsp_patch_table *table;
- size_t num;
- size_t num_entries;
-
- table = relative_offset(fsp, offset);
-
- if ((offset + sizeof(*table) > size) ||
- (le16toh(table->header_length) + offset) > size) {
- printk(BIOS_ERR, "FSPP not entirely contained in region.\n");
- return -1;
- }
-
- num_entries = le32toh(table->patch_entry_num);
- printk(FSP_DBG_LVL, "FSPP relocs: %zx\n", num_entries);
-
- for (num = 0; num < table->patch_entry_num; num++) {
- uint32_t *reloc;
- uint32_t reloc_val;
-
- reloc = fspp_reloc(fsp, size,
- le32toh(table->patch_entries[num]));
-
- if (reloc == NULL) {
- printk(BIOS_ERR, "Ignoring FSPP entry: %x\n",
- le32toh(table->patch_entries[num]));
- continue;
- }
-
- reloc_val = le32toh(*reloc);
- printk(FSP_DBG_LVL, "Adjusting %p %x -> %x\n",
- reloc, reloc_val,
- (unsigned int)(reloc_val + adjustment));
-
- *reloc = htole32(reloc_val + adjustment);
- }
-
- return 0;
-}
-
-static ssize_t relocate_remaining_items(void *fsp, size_t size,
- uintptr_t new_addr, size_t fih_offset)
-{
- EFI_FFS_FILE_HEADER *ffsfh;
- EFI_COMMON_SECTION_HEADER *csh;
- FSP_INFO_HEADER *fih;
- ssize_t adjustment;
- size_t offset;
-
- printk(FSP_DBG_LVL, "FSP_INFO_HEADER offset is %zx\n", fih_offset);
-
- if (fih_offset == 0) {
- printk(BIOS_ERR, "FSP_INFO_HEADER offset is 0.\n");
- return -1;
- }
-
- /* FSP_INFO_HEADER at first file in FV within first RAW section. */
- ffsfh = relative_offset(fsp, fih_offset);
- fih_offset += file_section_offset(ffsfh);
- csh = relative_offset(fsp, fih_offset);
- fih_offset += section_data_offset(csh);
- fih = relative_offset(fsp, fih_offset);
-
- if (guid_compare(&ffsfh->Name, &fih_guid)) {
- printk(BIOS_ERR, "Bad FIH GUID.\n");
- return -1;
- }
-
- if (le8toh(csh->Type) != EFI_SECTION_RAW) {
- printk(BIOS_ERR, "FIH file should have raw section: %x\n",
- csh->Type);
- return -1;
- }
-
- if (le32toh(fih->Signature) != FSP_SIG) {
- printk(BIOS_ERR, "Unexpected FIH signature: %08x\n",
- le32toh(fih->Signature));
- return -1;
- }
-
- adjustment = (intptr_t)new_addr - le32toh(fih->ImageBase);
-
- /* Update ImageBase to reflect FSP's new home. */
- fih->ImageBase = htole32(adjustment + le32toh(fih->ImageBase));
-
- /* Need to find patch table and adjust each entry. The tables
- * following FSP_INFO_HEADER have a 32-bit signature and header
- * length. The patch table is denoted as having a 'FSPP' signature;
- * the table format doesn't follow the other tables. */
- offset = fih_offset + le32toh(fih->HeaderLength);
- while (offset + 2 * sizeof(uint32_t) <= size) {
- uint32_t *table_headers;
-
- table_headers = relative_offset(fsp, offset);
-
- printk(FSP_DBG_LVL, "Checking offset %zx for 'FSPP'\n",
- offset);
-
- if (le32toh(table_headers[0]) != FSPP_SIG) {
- offset += le32toh(table_headers[1]);
- continue;
- }
-
- if (relocate_patch_table(fsp, size, offset, adjustment)) {
- printk(BIOS_ERR, "FSPP relocation failed.\n");
- return -1;
- }
-
- return fih_offset;
- }
-
- printk(BIOS_ERR, "Could not find the FSP patch table.\n");
- return -1;
-}
-
-static ssize_t relocate_fvh(uintptr_t new_addr, void *fsp, size_t fsp_size,
- size_t fvh_offset, size_t *fih_offset)
-{
- EFI_FIRMWARE_VOLUME_HEADER *fvh;
- EFI_FFS_FILE_HEADER *ffsfh;
- EFI_COMMON_SECTION_HEADER *csh;
- size_t offset;
- size_t file_offset;
- size_t size;
- size_t fv_length;
-
- offset = fvh_offset;
- fvh = relative_offset(fsp, offset);
-
- if (le32toh(fvh->Signature) != EFI_FVH_SIGNATURE)
- return -1;
-
- fv_length = le64toh(fvh->FvLength);
-
- printk(FSP_DBG_LVL, "FVH length: %zx Offset: %zx Mapping length: %zx\n",
- fv_length, offset, fsp_size);
-
- if (fvh->FvLength + offset > fsp_size)
- return -1;
-
- /* Parse only this FV. However, the algorithm uses offsets into the
- * entire FSP region so make size include the starting offset. */
- size = fv_length + offset;
-
- if (guid_compare(&fvh->FileSystemGuid, &ffs2_guid)) {
- printk(BIOS_ERR, "FVH not an FFS2 type.\n");
- return -1;
- }
-
- if (le16toh(fvh->ExtHeaderOffset) != 0) {
- EFI_FIRMWARE_VOLUME_EXT_HEADER *fveh;
-
- offset += le16toh(fvh->ExtHeaderOffset);
- fveh = relative_offset(fsp, offset);
- printk(FSP_DBG_LVL, "Extended Header Offset: %zx Size: %zx\n",
- (size_t)le16toh(fvh->ExtHeaderOffset),
- (size_t)le32toh(fveh->ExtHeaderSize));
- offset += le32toh(fveh->ExtHeaderSize);
- /* FFS files are 8 byte aligned after extended header. */
- offset = ALIGN_UP(offset, 8);
- } else {
- offset += le16toh(fvh->HeaderLength);
- }
-
- file_offset = offset;
- while (file_offset + sizeof(*ffsfh) < size) {
- offset = file_offset;
- printk(FSP_DBG_LVL, "file offset: %zx\n", file_offset);
-
- /* First file and section should be FSP info header. */
- if (fih_offset != NULL && *fih_offset == 0)
- *fih_offset = file_offset;
-
- ffsfh = relative_offset(fsp, file_offset);
-
- printk(FSP_DBG_LVL, "file type = %x\n", le8toh(ffsfh->Type));
- printk(FSP_DBG_LVL, "file attribs = %x\n",
- le8toh(ffsfh->Attributes));
-
- /* Exit FV relocation when empty space found */
- if (le8toh(ffsfh->Type) == EFI_FV_FILETYPE_FFS_MAX)
- break;
-
- /* Next file on 8 byte alignment. */
- file_offset += ffs_file_size(ffsfh);
- file_offset = ALIGN_UP(file_offset, 8);
-
- /* Padding files have no section information. */
- if (le8toh(ffsfh->Type) == EFI_FV_FILETYPE_FFS_PAD)
- continue;
-
- offset += file_section_offset(ffsfh);
-
- while (offset + sizeof(*csh) < file_offset) {
- size_t data_size;
- size_t data_offset;
-
- csh = relative_offset(fsp, offset);
-
- printk(FSP_DBG_LVL, "section offset: %zx\n", offset);
- printk(FSP_DBG_LVL, "section type: %x\n",
- le8toh(csh->Type));
-
- data_size = section_data_size(csh);
- data_offset = section_data_offset(csh);
-
- if (data_size + data_offset + offset > file_offset) {
- printk(BIOS_ERR, "Section exceeds FV size.\n");
- return -1;
- }
-
- /*
- * The entire FSP 1.1 image can be thought of as one
- * program with a single link address even though there
- * are multiple TEs linked separately. The reason is
- * that each TE is linked for XIP. So in order to
- * relocate the TE properly we need to form the
- * relocated address based on the TE offset within
- * FSP proper.
- */
- if (le8toh(csh->Type) == EFI_SECTION_TE) {
- void *te;
- size_t te_offset = offset + data_offset;
- uintptr_t te_addr = new_addr + te_offset;
-
- printk(FSP_DBG_LVL, "TE image at offset %zx\n",
- te_offset);
- te = relative_offset(fsp, te_offset);
- te_relocate(te_addr, te, data_size);
- }
-
- offset += data_size + data_offset;
- /* Sections are aligned to 4 bytes. */
- offset = ALIGN_UP(offset, 4);
- }
- }
-
- /* Return amount of buffer parsed: FV size. */
- return fv_length;
-}
-
-static ssize_t fsp1_1_relocate(uintptr_t new_addr, void *fsp, size_t size)
-{
- size_t offset;
- size_t fih_offset;
-
- offset = 0;
- fih_offset = 0;
- while (offset < size) {
- ssize_t nparsed;
-
- /* Relocate each FV within the FSP region. The FSP_INFO_HEADER
- * should only be located in the first FV. */
- if (offset == 0)
- nparsed = relocate_fvh(new_addr, fsp, size, offset,
- &fih_offset);
- else
- nparsed = relocate_fvh(new_addr, fsp, size, offset,
- NULL);
-
- /* FV should be larger than 0 or failed to parse. */
- if (nparsed <= 0) {
- printk(BIOS_ERR, "FV @ offset %zx relocation failed\n",
- offset);
- return -1;
- }
-
- offset += nparsed;
- }
-
- return relocate_remaining_items(fsp, size, new_addr, fih_offset);
-}
int fsp_relocate(struct prog *fsp_relocd, const struct region_device *fsp_src)
{
@@ -564,7 +47,7 @@ int fsp_relocate(struct prog *fsp_relocd, const struct region_device *fsp_src)
return -1;
}
- fih = relative_offset(new_loc, fih_offset);
+ fih = (void *)((uint8_t *)new_loc + fih_offset);
prog_set_area(fsp_relocd, new_loc, size);
prog_set_entry(fsp_relocd, fih, NULL);
diff --git a/src/drivers/intel/fsp1_1/include/fsp/util.h b/src/drivers/intel/fsp1_1/include/fsp/util.h
index 9695b3b697..041c0f1200 100644
--- a/src/drivers/intel/fsp1_1/include/fsp/util.h
+++ b/src/drivers/intel/fsp1_1/include/fsp/util.h
@@ -53,6 +53,12 @@ void *get_first_resource_hob(const EFI_GUID *guid);
*/
int fsp_relocate(struct prog *fsp_relocd, const struct region_device *fsp_src);
+/*
+ * Relocate FSP held within buffer defined by size to new_addr. Returns < 0
+ * on error, offset to FSP_INFO_HEADER on success.
+ */
+ssize_t fsp1_1_relocate(uintptr_t new_addr, void *fsp, size_t size);
+
/* Additional HOB types not included in the FSP:
* #define EFI_HOB_TYPE_HANDOFF 0x0001
* #define EFI_HOB_TYPE_MEMORY_ALLOCATION 0x0002