Angel Pons | ebda03e | 2020-04-02 23:48:05 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 2 | |
| 3 | #include <console/console.h> |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 4 | #include <commonlib/endian.h> |
Furquan Shaikh | b0c2fe0 | 2016-05-09 12:23:01 -0700 | [diff] [blame] | 5 | #include <commonlib/fsp.h> |
Werner Zeh | 314f280 | 2022-11-16 07:56:38 +0100 | [diff] [blame] | 6 | #include <inttypes.h> |
Stefan Reinauer | f76ceea7 | 2016-01-30 02:05:56 -0800 | [diff] [blame] | 7 | /* |
| 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 |
Frans Hendriks | 47ed269 | 2018-11-26 09:47:49 +0100 | [diff] [blame] | 10 | * globally for a compilation unit just by including a header file. |
Stefan Reinauer | f76ceea7 | 2016-01-30 02:05:56 -0800 | [diff] [blame] | 11 | */ |
| 12 | #pragma pack(push) |
| 13 | |
| 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> |
| 17 | |
| 18 | /* Restore original packing policy. */ |
| 19 | #pragma pack(pop) |
| 20 | |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 21 | #include <commonlib/helpers.h> |
Elyes HAOUAS | 56ab575 | 2021-12-31 18:45:46 +0100 | [diff] [blame] | 22 | #include <stddef.h> |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 23 | #include <stdint.h> |
| 24 | #include <string.h> |
| 25 | |
| 26 | #define FSP_DBG_LVL BIOS_NEVER |
Eddie Vas | 1df1cf9 | 2022-08-16 20:12:04 -0700 | [diff] [blame] | 27 | #define MASK_24BITS 0x00FFFFFF |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 28 | |
| 29 | /* |
| 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. |
| 34 | */ |
| 35 | |
| 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) |
| 38 | { |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 39 | if (read_le32(&le_guid->Data1) != native_guid->Data1) |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 40 | return 1; |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 41 | if (read_le16(&le_guid->Data2) != native_guid->Data2) |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 42 | return 1; |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 43 | if (read_le16(&le_guid->Data3) != native_guid->Data3) |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 44 | return 1; |
| 45 | return memcmp(le_guid->Data4, native_guid->Data4, |
| 46 | ARRAY_SIZE(le_guid->Data4)); |
| 47 | } |
| 48 | |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 49 | static const EFI_GUID ffs2_guid = EFI_FIRMWARE_FILE_SYSTEM2_GUID; |
| 50 | static const EFI_GUID fih_guid = FSP_INFO_HEADER_GUID; |
| 51 | |
| 52 | struct fsp_patch_table { |
| 53 | uint32_t signature; |
| 54 | uint16_t header_length; |
| 55 | uint8_t header_revision; |
| 56 | uint8_t reserved; |
| 57 | uint32_t patch_entry_num; |
Elyes Haouas | c1700e0 | 2023-07-28 06:16:27 +0200 | [diff] [blame^] | 58 | uint32_t patch_entries[]; |
Stefan Reinauer | 6a00113 | 2017-07-13 02:20:27 +0200 | [diff] [blame] | 59 | } __packed; |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 60 | |
| 61 | #define FSPP_SIG 0x50505346 |
| 62 | |
| 63 | static void *relative_offset(void *base, ssize_t offset) |
| 64 | { |
| 65 | uintptr_t loc; |
| 66 | |
| 67 | loc = (uintptr_t)base; |
| 68 | loc += offset; |
| 69 | |
| 70 | return (void *)loc; |
| 71 | } |
| 72 | |
Eddie Vas | 1df1cf9 | 2022-08-16 20:12:04 -0700 | [diff] [blame] | 73 | static size_t csh_size(const EFI_COMMON_SECTION_HEADER *csh) |
| 74 | { |
| 75 | size_t size; |
| 76 | |
| 77 | /* Unpack the array into a type that can be used. */ |
| 78 | size = 0; |
| 79 | size |= read_le8(&csh->Size[0]) << 0; |
| 80 | size |= read_le8(&csh->Size[1]) << 8; |
| 81 | size |= read_le8(&csh->Size[2]) << 16; |
| 82 | |
| 83 | return size; |
| 84 | } |
| 85 | |
| 86 | static size_t file_section_offset(const EFI_FFS_FILE_HEADER *ffsfh) |
| 87 | { |
| 88 | if (IS_FFS_FILE2(ffsfh)) |
| 89 | return sizeof(EFI_FFS_FILE_HEADER2); |
| 90 | else |
| 91 | return sizeof(EFI_FFS_FILE_HEADER); |
| 92 | } |
| 93 | |
| 94 | static size_t section_data_offset(const EFI_COMMON_SECTION_HEADER *csh) |
| 95 | { |
| 96 | if (csh_size(csh) == MASK_24BITS) |
| 97 | return sizeof(EFI_COMMON_SECTION_HEADER2); |
| 98 | else |
| 99 | return sizeof(EFI_COMMON_SECTION_HEADER); |
| 100 | } |
| 101 | |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 102 | static uint32_t *fspp_reloc(void *fsp, size_t fsp_size, uint32_t e) |
| 103 | { |
| 104 | size_t offset; |
| 105 | |
| 106 | /* Offsets live in bits 23:0. */ |
Eddie Vas | 1df1cf9 | 2022-08-16 20:12:04 -0700 | [diff] [blame] | 107 | offset = e & MASK_24BITS; |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 108 | |
| 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 |
| 111 | * reference. */ |
| 112 | if (e & (1 << 31)) |
| 113 | offset = fsp_size - (16 * MiB - offset); |
| 114 | |
| 115 | /* Determine if offset falls within fsp_size for a 32 bit relocation. */ |
| 116 | if (offset > fsp_size - sizeof(uint32_t)) |
| 117 | return NULL; |
| 118 | |
| 119 | return relative_offset(fsp, offset); |
| 120 | } |
| 121 | |
| 122 | static int reloc_type(uint16_t reloc_entry) |
| 123 | { |
| 124 | /* Reloc type in upper 4 bits */ |
| 125 | return reloc_entry >> 12; |
| 126 | } |
| 127 | |
| 128 | static size_t reloc_offset(uint16_t reloc_entry) |
| 129 | { |
| 130 | /* Offsets are in low 12 bits. */ |
| 131 | return reloc_entry & ((1 << 12) - 1); |
| 132 | } |
| 133 | |
Eddie Vas | 1df1cf9 | 2022-08-16 20:12:04 -0700 | [diff] [blame] | 134 | static FSP_INFO_HEADER *fsp_get_info_hdr(void *fsp, size_t fih_offset) |
| 135 | { |
| 136 | EFI_FFS_FILE_HEADER *ffsfh; |
| 137 | EFI_COMMON_SECTION_HEADER *csh; |
| 138 | FSP_INFO_HEADER *fih; |
| 139 | |
| 140 | printk(FSP_DBG_LVL, "FSP_INFO_HEADER offset is %zx\n", fih_offset); |
| 141 | |
| 142 | if (fih_offset == 0) { |
| 143 | printk(BIOS_ERR, "FSP_INFO_HEADER offset is 0.\n"); |
| 144 | return NULL; |
| 145 | } |
| 146 | |
| 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); |
| 153 | |
| 154 | if (guid_compare(&ffsfh->Name, &fih_guid)) { |
| 155 | printk(BIOS_ERR, "Bad FIH GUID.\n"); |
| 156 | return NULL; |
| 157 | } |
| 158 | |
| 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)); |
| 162 | return NULL; |
| 163 | } |
| 164 | |
| 165 | if (read_le32(&fih->Signature) != FSP_SIG) { |
| 166 | printk(BIOS_ERR, "Unexpected FIH signature: %08x\n", |
| 167 | read_le32(&fih->Signature)); |
| 168 | return NULL; |
| 169 | } |
| 170 | |
| 171 | return fih; |
| 172 | } |
| 173 | |
| 174 | static int pe_relocate(uintptr_t new_addr, void *pe, void *fsp, size_t fih_off) |
| 175 | { |
| 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; |
| 181 | uint32_t offset; |
| 182 | uint8_t *pe_base = pe; |
| 183 | uint32_t image_base; |
| 184 | uint32_t img_base_off; |
| 185 | uint32_t delta; |
| 186 | |
| 187 | doshdr = pe; |
| 188 | if (read_le16(&doshdr->e_magic) != EFI_IMAGE_DOS_SIGNATURE) { |
| 189 | printk(BIOS_ERR, "Invalid DOS Header/magic\n"); |
| 190 | return -1; |
| 191 | } |
| 192 | |
| 193 | peih = relative_offset(pe, doshdr->e_lfanew); |
| 194 | |
| 195 | if (read_le32(&peih->Signature) != EFI_IMAGE_NT_SIGNATURE) { |
| 196 | printk(BIOS_ERR, "Invalid PE32 header\n"); |
| 197 | return -1; |
| 198 | } |
| 199 | |
| 200 | ophdr = &peih->OptionalHeader; |
| 201 | |
| 202 | if (read_le16(&ophdr->Magic) != EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) { |
| 203 | printk(BIOS_ERR, "No support for non-PE32 images\n"); |
| 204 | return -1; |
| 205 | } |
| 206 | |
| 207 | fih = fsp_get_info_hdr(fsp, fih_off); |
| 208 | if (fih == NULL) { |
| 209 | printk(BIOS_ERR, "No Image base found for FSP PE32\n"); |
| 210 | return -1; |
| 211 | } |
| 212 | image_base = read_le32(&fih->ImageBase); |
| 213 | printk(FSP_DBG_LVL, "FSP InfoHdr Image Base is %x\n", image_base); |
| 214 | |
| 215 | delta = new_addr - image_base; |
| 216 | |
| 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)); |
| 222 | |
Werner Zeh | 314f280 | 2022-11-16 07:56:38 +0100 | [diff] [blame] | 223 | printk(FSP_DBG_LVL, "relocating PE32 image at addr - 0x%" PRIxPTR "\n", new_addr); |
Eddie Vas | 1df1cf9 | 2022-08-16 20:12:04 -0700 | [diff] [blame] | 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 |
| 228 | |
| 229 | offset = roffset; |
| 230 | while (offset < (roffset + rsize)) { |
| 231 | uint32_t vaddr; |
| 232 | uint32_t rlen, rnum; |
| 233 | uint16_t *rdata; |
| 234 | uint32_t i; |
| 235 | EFI_IMAGE_DATA_DIRECTORY *relocd; |
| 236 | |
Elyes Haouas | 995dfef | 2022-11-18 15:22:07 +0100 | [diff] [blame] | 237 | relocd = (void *)&pe_base[offset]; |
Eddie Vas | 1df1cf9 | 2022-08-16 20:12:04 -0700 | [diff] [blame] | 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); |
Elyes Haouas | 995dfef | 2022-11-18 15:22:07 +0100 | [diff] [blame] | 243 | rdata = (uint16_t *)&pe_base[offset]; |
Eddie Vas | 1df1cf9 | 2022-08-16 20:12:04 -0700 | [diff] [blame] | 244 | printk(FSP_DBG_LVL, "\t%d Relocs for RVA %x\n", rnum, vaddr); |
| 245 | |
| 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; |
| 250 | uint32_t val; |
| 251 | printk(FSP_DBG_LVL, "\t\treloc type %x offset %x aoff %x, base-0x%x\n", |
| 252 | rtype, roff, aoff, img_base_off); |
| 253 | switch (rtype) { |
| 254 | case EFI_IMAGE_REL_BASED_ABSOLUTE: |
| 255 | continue; |
| 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); |
| 261 | break; |
| 262 | case EFI_IMAGE_REL_BASED_DIR64: |
Elyes Haouas | aba1c94 | 2022-11-09 15:05:23 +0100 | [diff] [blame] | 263 | printk(BIOS_ERR, "Unsupported DIR64\n"); |
Eddie Vas | 1df1cf9 | 2022-08-16 20:12:04 -0700 | [diff] [blame] | 264 | break; |
| 265 | default: |
Elyes Haouas | aba1c94 | 2022-11-09 15:05:23 +0100 | [diff] [blame] | 266 | printk(BIOS_ERR, "Unsupported relocation type %d\n", |
Eddie Vas | 1df1cf9 | 2022-08-16 20:12:04 -0700 | [diff] [blame] | 267 | rtype); |
| 268 | return -1; |
| 269 | } |
| 270 | } |
| 271 | offset += sizeof(*rdata) * rnum; |
| 272 | } |
| 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); |
| 277 | |
| 278 | return -1; |
| 279 | } |
| 280 | |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 281 | static int te_relocate(uintptr_t new_addr, void *te) |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 282 | { |
| 283 | EFI_TE_IMAGE_HEADER *teih; |
| 284 | EFI_IMAGE_DATA_DIRECTORY *relocd; |
| 285 | EFI_IMAGE_BASE_RELOCATION *relocb; |
| 286 | uintptr_t image_base; |
| 287 | size_t fixup_offset; |
| 288 | size_t num_relocs; |
| 289 | uint16_t *reloc; |
| 290 | size_t relocd_offset; |
| 291 | uint8_t *te_base; |
| 292 | uint32_t adj; |
| 293 | |
| 294 | teih = te; |
| 295 | |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 296 | if (read_le16(&teih->Signature) != EFI_TE_IMAGE_HEADER_SIGNATURE) { |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 297 | printk(BIOS_ERR, "TE Signature mismatch: %x vs %x\n", |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 298 | read_le16(&teih->Signature), |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 299 | EFI_TE_IMAGE_HEADER_SIGNATURE); |
| 300 | return -1; |
| 301 | } |
| 302 | |
| 303 | /* |
| 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 |
Elyes HAOUAS | 23c1c4e | 2019-12-18 13:21:37 +0100 | [diff] [blame] | 306 | * the correct relative offsets one needs to subtract fixup_offset |
| 307 | * from the encoded offsets. Similarly, the linked address of the |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 308 | * program is found by adding the fixup_offset to the ImageBase. |
| 309 | */ |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 310 | fixup_offset = read_le16(&teih->StrippedSize); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 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. */ |
| 314 | te_base = te; |
| 315 | te_base -= fixup_offset; |
| 316 | |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 317 | image_base = read_le64(&teih->ImageBase); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 318 | adj = new_addr - (image_base + fixup_offset); |
| 319 | |
| 320 | printk(FSP_DBG_LVL, "TE Image %p -> %p adjust value: %x\n", |
| 321 | (void *)image_base, (void *)new_addr, adj); |
| 322 | |
| 323 | /* Adjust ImageBase for consistency. */ |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 324 | write_le64(&teih->ImageBase, (uint32_t)(image_base + adj)); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 325 | |
| 326 | relocd = &teih->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_BASERELOC]; |
| 327 | |
| 328 | relocd_offset = 0; |
| 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, |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 332 | read_le32(&relocd->VirtualAddress) - fixup_offset); |
| 333 | while (relocd_offset < read_le32(&relocd->Size)) { |
| 334 | size_t rva_offset = read_le32(&relocb->VirtualAddress); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 335 | |
| 336 | printk(FSP_DBG_LVL, "Relocs for RVA offset %zx\n", rva_offset); |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 337 | num_relocs = read_le32(&relocb->SizeOfBlock) - sizeof(*relocb); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 338 | num_relocs /= sizeof(uint16_t); |
| 339 | reloc = relative_offset(relocb, sizeof(*relocb)); |
| 340 | |
| 341 | printk(FSP_DBG_LVL, "Num relocs in block: %zx\n", num_relocs); |
| 342 | |
| 343 | while (num_relocs > 0) { |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 344 | uint16_t reloc_val = read_le16(reloc); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 345 | int type = reloc_type(reloc_val); |
| 346 | size_t offset = reloc_offset(reloc_val); |
| 347 | |
| 348 | printk(FSP_DBG_LVL, "reloc type %x offset %zx\n", |
| 349 | type, offset); |
| 350 | |
Sridhar Siricilla | 5902d88 | 2022-06-25 16:30:52 +0530 | [diff] [blame] | 351 | if (type == EFI_IMAGE_REL_BASED_HIGHLOW || |
| 352 | type == EFI_IMAGE_REL_BASED_DIR64) { |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 353 | uint32_t *reloc_addr; |
| 354 | uint32_t val; |
| 355 | |
| 356 | offset += rva_offset; |
| 357 | reloc_addr = (void *)&te_base[offset]; |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 358 | val = read_le32(reloc_addr); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 359 | |
| 360 | printk(FSP_DBG_LVL, "Adjusting %p %x -> %x\n", |
| 361 | reloc_addr, val, val + adj); |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 362 | write_le32(reloc_addr, val + adj); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 363 | } else if (type != EFI_IMAGE_REL_BASED_ABSOLUTE) { |
| 364 | printk(BIOS_ERR, "Unknown reloc type: %x\n", |
| 365 | type); |
| 366 | return -1; |
| 367 | } |
| 368 | num_relocs--; |
| 369 | reloc++; |
| 370 | } |
| 371 | |
| 372 | /* Track consumption of relocation directory contents. */ |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 373 | relocd_offset += read_le32(&relocb->SizeOfBlock); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 374 | /* Get next relocation block to process. */ |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 375 | relocb = relative_offset(relocb, |
| 376 | read_le32(&relocb->SizeOfBlock)); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | return 0; |
| 380 | } |
| 381 | |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 382 | static size_t section_data_size(const EFI_COMMON_SECTION_HEADER *csh) |
| 383 | { |
| 384 | size_t section_size; |
| 385 | |
Eddie Vas | 1df1cf9 | 2022-08-16 20:12:04 -0700 | [diff] [blame] | 386 | if (csh_size(csh) == MASK_24BITS) |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 387 | section_size = read_le32(&SECTION2_SIZE(csh)); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 388 | else |
| 389 | section_size = csh_size(csh); |
| 390 | |
| 391 | return section_size - section_data_offset(csh); |
| 392 | } |
| 393 | |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 394 | static size_t ffs_file_size(const EFI_FFS_FILE_HEADER *ffsfh) |
| 395 | { |
| 396 | size_t size; |
| 397 | |
Brandon Breitenstein | 51a0f7c | 2016-08-23 14:55:13 -0700 | [diff] [blame] | 398 | if (IS_FFS_FILE2(ffsfh)) { |
| 399 | /* |
| 400 | * this cast is needed with UEFI 2.6 headers in order |
| 401 | * to read the UINT32 value that FFS_FILE2_SIZE converts |
| 402 | * the return into |
| 403 | */ |
| 404 | uint32_t file2_size = FFS_FILE2_SIZE(ffsfh); |
| 405 | size = read_le32(&file2_size); |
Lee Leahy | 72c60a4 | 2017-03-10 10:53:36 -0800 | [diff] [blame] | 406 | } else { |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 407 | size = read_le8(&ffsfh->Size[0]) << 0; |
| 408 | size |= read_le8(&ffsfh->Size[1]) << 8; |
| 409 | size |= read_le8(&ffsfh->Size[2]) << 16; |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 410 | } |
| 411 | return size; |
| 412 | } |
| 413 | |
| 414 | static int relocate_patch_table(void *fsp, size_t size, size_t offset, |
| 415 | ssize_t adjustment) |
| 416 | { |
| 417 | struct fsp_patch_table *table; |
| 418 | size_t num; |
| 419 | size_t num_entries; |
| 420 | |
| 421 | table = relative_offset(fsp, offset); |
| 422 | |
| 423 | if ((offset + sizeof(*table) > size) || |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 424 | (read_le16(&table->header_length) + offset) > size) { |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 425 | printk(BIOS_ERR, "FSPP not entirely contained in region.\n"); |
| 426 | return -1; |
| 427 | } |
| 428 | |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 429 | num_entries = read_le32(&table->patch_entry_num); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 430 | printk(FSP_DBG_LVL, "FSPP relocs: %zx\n", num_entries); |
| 431 | |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 432 | for (num = 0; num < num_entries; num++) { |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 433 | uint32_t *reloc; |
| 434 | uint32_t reloc_val; |
| 435 | |
| 436 | reloc = fspp_reloc(fsp, size, |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 437 | read_le32(&table->patch_entries[num])); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 438 | |
| 439 | if (reloc == NULL) { |
| 440 | printk(BIOS_ERR, "Ignoring FSPP entry: %x\n", |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 441 | read_le32(&table->patch_entries[num])); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 442 | continue; |
| 443 | } |
| 444 | |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 445 | reloc_val = read_le32(reloc); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 446 | printk(FSP_DBG_LVL, "Adjusting %p %x -> %x\n", |
| 447 | reloc, reloc_val, |
| 448 | (unsigned int)(reloc_val + adjustment)); |
| 449 | |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 450 | write_le32(reloc, reloc_val + adjustment); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | return 0; |
| 454 | } |
| 455 | |
| 456 | static ssize_t relocate_remaining_items(void *fsp, size_t size, |
| 457 | uintptr_t new_addr, size_t fih_offset) |
| 458 | { |
| 459 | EFI_FFS_FILE_HEADER *ffsfh; |
| 460 | EFI_COMMON_SECTION_HEADER *csh; |
| 461 | FSP_INFO_HEADER *fih; |
| 462 | ssize_t adjustment; |
| 463 | size_t offset; |
| 464 | |
| 465 | printk(FSP_DBG_LVL, "FSP_INFO_HEADER offset is %zx\n", fih_offset); |
| 466 | |
| 467 | if (fih_offset == 0) { |
| 468 | printk(BIOS_ERR, "FSP_INFO_HEADER offset is 0.\n"); |
| 469 | return -1; |
| 470 | } |
| 471 | |
| 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); |
| 478 | |
| 479 | if (guid_compare(&ffsfh->Name, &fih_guid)) { |
| 480 | printk(BIOS_ERR, "Bad FIH GUID.\n"); |
| 481 | return -1; |
| 482 | } |
| 483 | |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 484 | if (read_le8(&csh->Type) != EFI_SECTION_RAW) { |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 485 | printk(BIOS_ERR, "FIH file should have raw section: %x\n", |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 486 | read_le8(&csh->Type)); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 487 | return -1; |
| 488 | } |
| 489 | |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 490 | if (read_le32(&fih->Signature) != FSP_SIG) { |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 491 | printk(BIOS_ERR, "Unexpected FIH signature: %08x\n", |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 492 | read_le32(&fih->Signature)); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 493 | } |
| 494 | |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 495 | adjustment = (intptr_t)new_addr - read_le32(&fih->ImageBase); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 496 | |
| 497 | /* Update ImageBase to reflect FSP's new home. */ |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 498 | write_le32(&fih->ImageBase, adjustment + read_le32(&fih->ImageBase)); |
Eddie Vas | 1df1cf9 | 2022-08-16 20:12:04 -0700 | [diff] [blame] | 499 | printk(FSP_DBG_LVL, "Updated FSP InfoHdr Image Base to %x\n", |
| 500 | read_le32(&fih->ImageBase)); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 501 | |
| 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. */ |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 506 | offset = fih_offset + read_le32(&fih->HeaderLength); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 507 | while (offset + 2 * sizeof(uint32_t) <= size) { |
| 508 | uint32_t *table_headers; |
| 509 | |
| 510 | table_headers = relative_offset(fsp, offset); |
| 511 | |
| 512 | printk(FSP_DBG_LVL, "Checking offset %zx for 'FSPP'\n", |
| 513 | offset); |
| 514 | |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 515 | if (read_le32(&table_headers[0]) != FSPP_SIG) { |
| 516 | offset += read_le32(&table_headers[1]); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 517 | continue; |
| 518 | } |
| 519 | |
| 520 | if (relocate_patch_table(fsp, size, offset, adjustment)) { |
| 521 | printk(BIOS_ERR, "FSPP relocation failed.\n"); |
| 522 | return -1; |
| 523 | } |
| 524 | |
| 525 | return fih_offset; |
| 526 | } |
| 527 | |
| 528 | printk(BIOS_ERR, "Could not find the FSP patch table.\n"); |
| 529 | return -1; |
| 530 | } |
| 531 | |
| 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) |
| 534 | { |
| 535 | EFI_FIRMWARE_VOLUME_HEADER *fvh; |
| 536 | EFI_FFS_FILE_HEADER *ffsfh; |
| 537 | EFI_COMMON_SECTION_HEADER *csh; |
| 538 | size_t offset; |
| 539 | size_t file_offset; |
| 540 | size_t size; |
| 541 | size_t fv_length; |
| 542 | |
| 543 | offset = fvh_offset; |
| 544 | fvh = relative_offset(fsp, offset); |
| 545 | |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 546 | if (read_le32(&fvh->Signature) != EFI_FVH_SIGNATURE) |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 547 | return -1; |
| 548 | |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 549 | fv_length = read_le64(&fvh->FvLength); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 550 | |
| 551 | printk(FSP_DBG_LVL, "FVH length: %zx Offset: %zx Mapping length: %zx\n", |
| 552 | fv_length, offset, fsp_size); |
| 553 | |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 554 | if (fv_length + offset > fsp_size) |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 555 | return -1; |
| 556 | |
| 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; |
| 560 | |
| 561 | if (guid_compare(&fvh->FileSystemGuid, &ffs2_guid)) { |
| 562 | printk(BIOS_ERR, "FVH not an FFS2 type.\n"); |
| 563 | return -1; |
| 564 | } |
| 565 | |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 566 | if (read_le16(&fvh->ExtHeaderOffset) != 0) { |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 567 | EFI_FIRMWARE_VOLUME_EXT_HEADER *fveh; |
| 568 | |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 569 | offset += read_le16(&fvh->ExtHeaderOffset); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 570 | fveh = relative_offset(fsp, offset); |
| 571 | printk(FSP_DBG_LVL, "Extended Header Offset: %zx Size: %zx\n", |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 572 | (size_t)read_le16(&fvh->ExtHeaderOffset), |
| 573 | (size_t)read_le32(&fveh->ExtHeaderSize)); |
| 574 | offset += read_le32(&fveh->ExtHeaderSize); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 575 | /* FFS files are 8 byte aligned after extended header. */ |
| 576 | offset = ALIGN_UP(offset, 8); |
| 577 | } else { |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 578 | offset += read_le16(&fvh->HeaderLength); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 579 | } |
| 580 | |
| 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); |
| 585 | |
| 586 | /* First file and section should be FSP info header. */ |
| 587 | if (fih_offset != NULL && *fih_offset == 0) |
| 588 | *fih_offset = file_offset; |
| 589 | |
| 590 | ffsfh = relative_offset(fsp, file_offset); |
| 591 | |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 592 | printk(FSP_DBG_LVL, "file type = %x\n", read_le8(&ffsfh->Type)); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 593 | printk(FSP_DBG_LVL, "file attribs = %x\n", |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 594 | read_le8(&ffsfh->Attributes)); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 595 | |
| 596 | /* Exit FV relocation when empty space found */ |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 597 | if (read_le8(&ffsfh->Type) == EFI_FV_FILETYPE_FFS_MAX) |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 598 | break; |
| 599 | |
| 600 | /* Next file on 8 byte alignment. */ |
| 601 | file_offset += ffs_file_size(ffsfh); |
| 602 | file_offset = ALIGN_UP(file_offset, 8); |
| 603 | |
| 604 | /* Padding files have no section information. */ |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 605 | if (read_le8(&ffsfh->Type) == EFI_FV_FILETYPE_FFS_PAD) |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 606 | continue; |
| 607 | |
| 608 | offset += file_section_offset(ffsfh); |
| 609 | |
| 610 | while (offset + sizeof(*csh) < file_offset) { |
| 611 | size_t data_size; |
| 612 | size_t data_offset; |
Eddie Vas | 1df1cf9 | 2022-08-16 20:12:04 -0700 | [diff] [blame] | 613 | void *section_data; |
| 614 | size_t section_offset; |
| 615 | uintptr_t section_addr; |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 616 | |
| 617 | csh = relative_offset(fsp, offset); |
| 618 | |
| 619 | printk(FSP_DBG_LVL, "section offset: %zx\n", offset); |
| 620 | printk(FSP_DBG_LVL, "section type: %x\n", |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 621 | read_le8(&csh->Type)); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 622 | |
| 623 | data_size = section_data_size(csh); |
| 624 | data_offset = section_data_offset(csh); |
| 625 | |
| 626 | if (data_size + data_offset + offset > file_offset) { |
| 627 | printk(BIOS_ERR, "Section exceeds FV size.\n"); |
| 628 | return -1; |
| 629 | } |
| 630 | |
| 631 | /* |
Furquan Shaikh | b0c2fe0 | 2016-05-09 12:23:01 -0700 | [diff] [blame] | 632 | * The entire FSP image can be thought of as one |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 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 |
| 638 | * FSP proper. |
| 639 | */ |
Eddie Vas | 1df1cf9 | 2022-08-16 20:12:04 -0700 | [diff] [blame] | 640 | section_offset = offset + data_offset; |
| 641 | section_addr = new_addr + section_offset; |
| 642 | section_data = relative_offset(fsp, section_offset); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 643 | |
Eddie Vas | 1df1cf9 | 2022-08-16 20:12:04 -0700 | [diff] [blame] | 644 | if (read_le8(&csh->Type) == EFI_SECTION_TE) { |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 645 | printk(FSP_DBG_LVL, "TE image at offset %zx\n", |
Eddie Vas | 1df1cf9 | 2022-08-16 20:12:04 -0700 | [diff] [blame] | 646 | section_offset); |
| 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", |
| 650 | section_offset); |
| 651 | pe_relocate(new_addr, section_data, fsp, *fih_offset); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | offset += data_size + data_offset; |
| 655 | /* Sections are aligned to 4 bytes. */ |
| 656 | offset = ALIGN_UP(offset, 4); |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | /* Return amount of buffer parsed: FV size. */ |
| 661 | return fv_length; |
| 662 | } |
| 663 | |
Furquan Shaikh | b0c2fe0 | 2016-05-09 12:23:01 -0700 | [diff] [blame] | 664 | ssize_t fsp_component_relocate(uintptr_t new_addr, void *fsp, size_t size) |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 665 | { |
| 666 | size_t offset; |
| 667 | size_t fih_offset; |
| 668 | |
| 669 | offset = 0; |
| 670 | fih_offset = 0; |
| 671 | while (offset < size) { |
| 672 | ssize_t nparsed; |
| 673 | |
| 674 | /* Relocate each FV within the FSP region. The FSP_INFO_HEADER |
| 675 | * should only be located in the first FV. */ |
| 676 | if (offset == 0) |
| 677 | nparsed = relocate_fvh(new_addr, fsp, size, offset, |
| 678 | &fih_offset); |
| 679 | else |
| 680 | nparsed = relocate_fvh(new_addr, fsp, size, offset, |
| 681 | NULL); |
| 682 | |
| 683 | /* FV should be larger than 0 or failed to parse. */ |
| 684 | if (nparsed <= 0) { |
| 685 | printk(BIOS_ERR, "FV @ offset %zx relocation failed\n", |
| 686 | offset); |
| 687 | return -1; |
| 688 | } |
| 689 | |
| 690 | offset += nparsed; |
| 691 | } |
| 692 | |
| 693 | return relocate_remaining_items(fsp, size, new_addr, fih_offset); |
| 694 | } |
Furquan Shaikh | b0c2fe0 | 2016-05-09 12:23:01 -0700 | [diff] [blame] | 695 | |
| 696 | ssize_t fsp1_1_relocate(uintptr_t new_addr, void *fsp, size_t size) |
| 697 | { |
| 698 | return fsp_component_relocate(new_addr, fsp, size); |
| 699 | } |