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> |
Stefan Reinauer | f76ceea7 | 2016-01-30 02:05:56 -0800 | [diff] [blame] | 6 | /* |
| 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 |
Frans Hendriks | 47ed269 | 2018-11-26 09:47:49 +0100 | [diff] [blame] | 9 | * globally for a compilation unit just by including a header file. |
Stefan Reinauer | f76ceea7 | 2016-01-30 02:05:56 -0800 | [diff] [blame] | 10 | */ |
| 11 | #pragma pack(push) |
| 12 | |
| 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> |
| 16 | |
| 17 | /* Restore original packing policy. */ |
| 18 | #pragma pack(pop) |
| 19 | |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 20 | #include <commonlib/helpers.h> |
Elyes HAOUAS | 56ab575 | 2021-12-31 18:45:46 +0100 | [diff] [blame] | 21 | #include <stddef.h> |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 22 | #include <stdint.h> |
| 23 | #include <string.h> |
| 24 | |
| 25 | #define FSP_DBG_LVL BIOS_NEVER |
| 26 | |
| 27 | /* |
| 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. |
| 32 | */ |
| 33 | |
| 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) |
| 36 | { |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 37 | if (read_le32(&le_guid->Data1) != native_guid->Data1) |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 38 | return 1; |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 39 | if (read_le16(&le_guid->Data2) != native_guid->Data2) |
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->Data3) != native_guid->Data3) |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 42 | return 1; |
| 43 | return memcmp(le_guid->Data4, native_guid->Data4, |
| 44 | ARRAY_SIZE(le_guid->Data4)); |
| 45 | } |
| 46 | |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 47 | static const EFI_GUID ffs2_guid = EFI_FIRMWARE_FILE_SYSTEM2_GUID; |
| 48 | static const EFI_GUID fih_guid = FSP_INFO_HEADER_GUID; |
| 49 | |
| 50 | struct fsp_patch_table { |
| 51 | uint32_t signature; |
| 52 | uint16_t header_length; |
| 53 | uint8_t header_revision; |
| 54 | uint8_t reserved; |
| 55 | uint32_t patch_entry_num; |
| 56 | uint32_t patch_entries[0]; |
Stefan Reinauer | 6a00113 | 2017-07-13 02:20:27 +0200 | [diff] [blame] | 57 | } __packed; |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 58 | |
| 59 | #define FSPP_SIG 0x50505346 |
| 60 | |
| 61 | static void *relative_offset(void *base, ssize_t offset) |
| 62 | { |
| 63 | uintptr_t loc; |
| 64 | |
| 65 | loc = (uintptr_t)base; |
| 66 | loc += offset; |
| 67 | |
| 68 | return (void *)loc; |
| 69 | } |
| 70 | |
| 71 | static uint32_t *fspp_reloc(void *fsp, size_t fsp_size, uint32_t e) |
| 72 | { |
| 73 | size_t offset; |
| 74 | |
| 75 | /* Offsets live in bits 23:0. */ |
| 76 | offset = e & 0xffffff; |
| 77 | |
| 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 |
| 80 | * reference. */ |
| 81 | if (e & (1 << 31)) |
| 82 | offset = fsp_size - (16 * MiB - offset); |
| 83 | |
| 84 | /* Determine if offset falls within fsp_size for a 32 bit relocation. */ |
| 85 | if (offset > fsp_size - sizeof(uint32_t)) |
| 86 | return NULL; |
| 87 | |
| 88 | return relative_offset(fsp, offset); |
| 89 | } |
| 90 | |
| 91 | static int reloc_type(uint16_t reloc_entry) |
| 92 | { |
| 93 | /* Reloc type in upper 4 bits */ |
| 94 | return reloc_entry >> 12; |
| 95 | } |
| 96 | |
| 97 | static size_t reloc_offset(uint16_t reloc_entry) |
| 98 | { |
| 99 | /* Offsets are in low 12 bits. */ |
| 100 | return reloc_entry & ((1 << 12) - 1); |
| 101 | } |
| 102 | |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 103 | static int te_relocate(uintptr_t new_addr, void *te) |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 104 | { |
| 105 | EFI_TE_IMAGE_HEADER *teih; |
| 106 | EFI_IMAGE_DATA_DIRECTORY *relocd; |
| 107 | EFI_IMAGE_BASE_RELOCATION *relocb; |
| 108 | uintptr_t image_base; |
| 109 | size_t fixup_offset; |
| 110 | size_t num_relocs; |
| 111 | uint16_t *reloc; |
| 112 | size_t relocd_offset; |
| 113 | uint8_t *te_base; |
| 114 | uint32_t adj; |
| 115 | |
| 116 | teih = te; |
| 117 | |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 118 | if (read_le16(&teih->Signature) != EFI_TE_IMAGE_HEADER_SIGNATURE) { |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 119 | printk(BIOS_ERR, "TE Signature mismatch: %x vs %x\n", |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 120 | read_le16(&teih->Signature), |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 121 | EFI_TE_IMAGE_HEADER_SIGNATURE); |
| 122 | return -1; |
| 123 | } |
| 124 | |
| 125 | /* |
| 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 |
Elyes HAOUAS | 23c1c4e | 2019-12-18 13:21:37 +0100 | [diff] [blame] | 128 | * the correct relative offsets one needs to subtract fixup_offset |
| 129 | * from the encoded offsets. Similarly, the linked address of the |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 130 | * program is found by adding the fixup_offset to the ImageBase. |
| 131 | */ |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 132 | fixup_offset = read_le16(&teih->StrippedSize); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 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. */ |
| 136 | te_base = te; |
| 137 | te_base -= fixup_offset; |
| 138 | |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 139 | image_base = read_le64(&teih->ImageBase); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 140 | adj = new_addr - (image_base + fixup_offset); |
| 141 | |
| 142 | printk(FSP_DBG_LVL, "TE Image %p -> %p adjust value: %x\n", |
| 143 | (void *)image_base, (void *)new_addr, adj); |
| 144 | |
| 145 | /* Adjust ImageBase for consistency. */ |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 146 | write_le64(&teih->ImageBase, (uint32_t)(image_base + adj)); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 147 | |
| 148 | relocd = &teih->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_BASERELOC]; |
| 149 | |
| 150 | relocd_offset = 0; |
| 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, |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 154 | read_le32(&relocd->VirtualAddress) - fixup_offset); |
| 155 | while (relocd_offset < read_le32(&relocd->Size)) { |
| 156 | size_t rva_offset = read_le32(&relocb->VirtualAddress); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 157 | |
| 158 | printk(FSP_DBG_LVL, "Relocs for RVA offset %zx\n", rva_offset); |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 159 | num_relocs = read_le32(&relocb->SizeOfBlock) - sizeof(*relocb); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 160 | num_relocs /= sizeof(uint16_t); |
| 161 | reloc = relative_offset(relocb, sizeof(*relocb)); |
| 162 | |
| 163 | printk(FSP_DBG_LVL, "Num relocs in block: %zx\n", num_relocs); |
| 164 | |
| 165 | while (num_relocs > 0) { |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 166 | uint16_t reloc_val = read_le16(reloc); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 167 | int type = reloc_type(reloc_val); |
| 168 | size_t offset = reloc_offset(reloc_val); |
| 169 | |
| 170 | printk(FSP_DBG_LVL, "reloc type %x offset %zx\n", |
| 171 | type, offset); |
| 172 | |
Sridhar Siricilla | 5902d88 | 2022-06-25 16:30:52 +0530 | [diff] [blame^] | 173 | if (type == EFI_IMAGE_REL_BASED_HIGHLOW || |
| 174 | type == EFI_IMAGE_REL_BASED_DIR64) { |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 175 | uint32_t *reloc_addr; |
| 176 | uint32_t val; |
| 177 | |
| 178 | offset += rva_offset; |
| 179 | reloc_addr = (void *)&te_base[offset]; |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 180 | val = read_le32(reloc_addr); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 181 | |
| 182 | printk(FSP_DBG_LVL, "Adjusting %p %x -> %x\n", |
| 183 | reloc_addr, val, val + adj); |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 184 | write_le32(reloc_addr, val + adj); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 185 | } else if (type != EFI_IMAGE_REL_BASED_ABSOLUTE) { |
| 186 | printk(BIOS_ERR, "Unknown reloc type: %x\n", |
| 187 | type); |
| 188 | return -1; |
| 189 | } |
| 190 | num_relocs--; |
| 191 | reloc++; |
| 192 | } |
| 193 | |
| 194 | /* Track consumption of relocation directory contents. */ |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 195 | relocd_offset += read_le32(&relocb->SizeOfBlock); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 196 | /* Get next relocation block to process. */ |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 197 | relocb = relative_offset(relocb, |
| 198 | read_le32(&relocb->SizeOfBlock)); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | static size_t csh_size(const EFI_COMMON_SECTION_HEADER *csh) |
| 205 | { |
| 206 | size_t size; |
| 207 | |
| 208 | /* Unpack the array into a type that can be used. */ |
| 209 | size = 0; |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 210 | size |= read_le8(&csh->Size[0]) << 0; |
| 211 | size |= read_le8(&csh->Size[1]) << 8; |
| 212 | size |= read_le8(&csh->Size[2]) << 16; |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 213 | |
| 214 | return size; |
| 215 | } |
| 216 | |
| 217 | static size_t section_data_offset(const EFI_COMMON_SECTION_HEADER *csh) |
| 218 | { |
| 219 | if (csh_size(csh) == 0x00ffffff) |
| 220 | return sizeof(EFI_COMMON_SECTION_HEADER2); |
| 221 | else |
| 222 | return sizeof(EFI_COMMON_SECTION_HEADER); |
| 223 | } |
| 224 | |
| 225 | static size_t section_data_size(const EFI_COMMON_SECTION_HEADER *csh) |
| 226 | { |
| 227 | size_t section_size; |
| 228 | |
| 229 | if (csh_size(csh) == 0x00ffffff) |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 230 | section_size = read_le32(&SECTION2_SIZE(csh)); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 231 | else |
| 232 | section_size = csh_size(csh); |
| 233 | |
| 234 | return section_size - section_data_offset(csh); |
| 235 | } |
| 236 | |
| 237 | static size_t file_section_offset(const EFI_FFS_FILE_HEADER *ffsfh) |
| 238 | { |
| 239 | if (IS_FFS_FILE2(ffsfh)) |
| 240 | return sizeof(EFI_FFS_FILE_HEADER2); |
| 241 | else |
| 242 | return sizeof(EFI_FFS_FILE_HEADER); |
| 243 | } |
| 244 | |
| 245 | static size_t ffs_file_size(const EFI_FFS_FILE_HEADER *ffsfh) |
| 246 | { |
| 247 | size_t size; |
| 248 | |
Brandon Breitenstein | 51a0f7c | 2016-08-23 14:55:13 -0700 | [diff] [blame] | 249 | if (IS_FFS_FILE2(ffsfh)) { |
| 250 | /* |
| 251 | * this cast is needed with UEFI 2.6 headers in order |
| 252 | * to read the UINT32 value that FFS_FILE2_SIZE converts |
| 253 | * the return into |
| 254 | */ |
| 255 | uint32_t file2_size = FFS_FILE2_SIZE(ffsfh); |
| 256 | size = read_le32(&file2_size); |
Lee Leahy | 72c60a4 | 2017-03-10 10:53:36 -0800 | [diff] [blame] | 257 | } else { |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 258 | size = read_le8(&ffsfh->Size[0]) << 0; |
| 259 | size |= read_le8(&ffsfh->Size[1]) << 8; |
| 260 | size |= read_le8(&ffsfh->Size[2]) << 16; |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 261 | } |
| 262 | return size; |
| 263 | } |
| 264 | |
| 265 | static int relocate_patch_table(void *fsp, size_t size, size_t offset, |
| 266 | ssize_t adjustment) |
| 267 | { |
| 268 | struct fsp_patch_table *table; |
| 269 | size_t num; |
| 270 | size_t num_entries; |
| 271 | |
| 272 | table = relative_offset(fsp, offset); |
| 273 | |
| 274 | if ((offset + sizeof(*table) > size) || |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 275 | (read_le16(&table->header_length) + offset) > size) { |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 276 | printk(BIOS_ERR, "FSPP not entirely contained in region.\n"); |
| 277 | return -1; |
| 278 | } |
| 279 | |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 280 | num_entries = read_le32(&table->patch_entry_num); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 281 | printk(FSP_DBG_LVL, "FSPP relocs: %zx\n", num_entries); |
| 282 | |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 283 | for (num = 0; num < num_entries; num++) { |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 284 | uint32_t *reloc; |
| 285 | uint32_t reloc_val; |
| 286 | |
| 287 | reloc = fspp_reloc(fsp, size, |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 288 | read_le32(&table->patch_entries[num])); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 289 | |
| 290 | if (reloc == NULL) { |
| 291 | printk(BIOS_ERR, "Ignoring FSPP entry: %x\n", |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 292 | read_le32(&table->patch_entries[num])); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 293 | continue; |
| 294 | } |
| 295 | |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 296 | reloc_val = read_le32(reloc); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 297 | printk(FSP_DBG_LVL, "Adjusting %p %x -> %x\n", |
| 298 | reloc, reloc_val, |
| 299 | (unsigned int)(reloc_val + adjustment)); |
| 300 | |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 301 | write_le32(reloc, reloc_val + adjustment); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | static ssize_t relocate_remaining_items(void *fsp, size_t size, |
| 308 | uintptr_t new_addr, size_t fih_offset) |
| 309 | { |
| 310 | EFI_FFS_FILE_HEADER *ffsfh; |
| 311 | EFI_COMMON_SECTION_HEADER *csh; |
| 312 | FSP_INFO_HEADER *fih; |
| 313 | ssize_t adjustment; |
| 314 | size_t offset; |
| 315 | |
| 316 | printk(FSP_DBG_LVL, "FSP_INFO_HEADER offset is %zx\n", fih_offset); |
| 317 | |
| 318 | if (fih_offset == 0) { |
| 319 | printk(BIOS_ERR, "FSP_INFO_HEADER offset is 0.\n"); |
| 320 | return -1; |
| 321 | } |
| 322 | |
| 323 | /* FSP_INFO_HEADER at first file in FV within first RAW section. */ |
| 324 | ffsfh = relative_offset(fsp, fih_offset); |
| 325 | fih_offset += file_section_offset(ffsfh); |
| 326 | csh = relative_offset(fsp, fih_offset); |
| 327 | fih_offset += section_data_offset(csh); |
| 328 | fih = relative_offset(fsp, fih_offset); |
| 329 | |
| 330 | if (guid_compare(&ffsfh->Name, &fih_guid)) { |
| 331 | printk(BIOS_ERR, "Bad FIH GUID.\n"); |
| 332 | return -1; |
| 333 | } |
| 334 | |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 335 | if (read_le8(&csh->Type) != EFI_SECTION_RAW) { |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 336 | printk(BIOS_ERR, "FIH file should have raw section: %x\n", |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 337 | read_le8(&csh->Type)); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 338 | return -1; |
| 339 | } |
| 340 | |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 341 | if (read_le32(&fih->Signature) != FSP_SIG) { |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 342 | printk(BIOS_ERR, "Unexpected FIH signature: %08x\n", |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 343 | read_le32(&fih->Signature)); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 344 | return -1; |
| 345 | } |
| 346 | |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 347 | adjustment = (intptr_t)new_addr - read_le32(&fih->ImageBase); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 348 | |
| 349 | /* Update ImageBase to reflect FSP's new home. */ |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 350 | write_le32(&fih->ImageBase, adjustment + read_le32(&fih->ImageBase)); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 351 | |
| 352 | /* Need to find patch table and adjust each entry. The tables |
| 353 | * following FSP_INFO_HEADER have a 32-bit signature and header |
| 354 | * length. The patch table is denoted as having a 'FSPP' signature; |
| 355 | * the table format doesn't follow the other tables. */ |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 356 | offset = fih_offset + read_le32(&fih->HeaderLength); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 357 | while (offset + 2 * sizeof(uint32_t) <= size) { |
| 358 | uint32_t *table_headers; |
| 359 | |
| 360 | table_headers = relative_offset(fsp, offset); |
| 361 | |
| 362 | printk(FSP_DBG_LVL, "Checking offset %zx for 'FSPP'\n", |
| 363 | offset); |
| 364 | |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 365 | if (read_le32(&table_headers[0]) != FSPP_SIG) { |
| 366 | offset += read_le32(&table_headers[1]); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 367 | continue; |
| 368 | } |
| 369 | |
| 370 | if (relocate_patch_table(fsp, size, offset, adjustment)) { |
| 371 | printk(BIOS_ERR, "FSPP relocation failed.\n"); |
| 372 | return -1; |
| 373 | } |
| 374 | |
| 375 | return fih_offset; |
| 376 | } |
| 377 | |
| 378 | printk(BIOS_ERR, "Could not find the FSP patch table.\n"); |
| 379 | return -1; |
| 380 | } |
| 381 | |
| 382 | static ssize_t relocate_fvh(uintptr_t new_addr, void *fsp, size_t fsp_size, |
| 383 | size_t fvh_offset, size_t *fih_offset) |
| 384 | { |
| 385 | EFI_FIRMWARE_VOLUME_HEADER *fvh; |
| 386 | EFI_FFS_FILE_HEADER *ffsfh; |
| 387 | EFI_COMMON_SECTION_HEADER *csh; |
| 388 | size_t offset; |
| 389 | size_t file_offset; |
| 390 | size_t size; |
| 391 | size_t fv_length; |
| 392 | |
| 393 | offset = fvh_offset; |
| 394 | fvh = relative_offset(fsp, offset); |
| 395 | |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 396 | if (read_le32(&fvh->Signature) != EFI_FVH_SIGNATURE) |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 397 | return -1; |
| 398 | |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 399 | fv_length = read_le64(&fvh->FvLength); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 400 | |
| 401 | printk(FSP_DBG_LVL, "FVH length: %zx Offset: %zx Mapping length: %zx\n", |
| 402 | fv_length, offset, fsp_size); |
| 403 | |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 404 | if (fv_length + offset > fsp_size) |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 405 | return -1; |
| 406 | |
| 407 | /* Parse only this FV. However, the algorithm uses offsets into the |
| 408 | * entire FSP region so make size include the starting offset. */ |
| 409 | size = fv_length + offset; |
| 410 | |
| 411 | if (guid_compare(&fvh->FileSystemGuid, &ffs2_guid)) { |
| 412 | printk(BIOS_ERR, "FVH not an FFS2 type.\n"); |
| 413 | return -1; |
| 414 | } |
| 415 | |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 416 | if (read_le16(&fvh->ExtHeaderOffset) != 0) { |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 417 | EFI_FIRMWARE_VOLUME_EXT_HEADER *fveh; |
| 418 | |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 419 | offset += read_le16(&fvh->ExtHeaderOffset); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 420 | fveh = relative_offset(fsp, offset); |
| 421 | printk(FSP_DBG_LVL, "Extended Header Offset: %zx Size: %zx\n", |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 422 | (size_t)read_le16(&fvh->ExtHeaderOffset), |
| 423 | (size_t)read_le32(&fveh->ExtHeaderSize)); |
| 424 | offset += read_le32(&fveh->ExtHeaderSize); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 425 | /* FFS files are 8 byte aligned after extended header. */ |
| 426 | offset = ALIGN_UP(offset, 8); |
| 427 | } else { |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 428 | offset += read_le16(&fvh->HeaderLength); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | file_offset = offset; |
| 432 | while (file_offset + sizeof(*ffsfh) < size) { |
| 433 | offset = file_offset; |
| 434 | printk(FSP_DBG_LVL, "file offset: %zx\n", file_offset); |
| 435 | |
| 436 | /* First file and section should be FSP info header. */ |
| 437 | if (fih_offset != NULL && *fih_offset == 0) |
| 438 | *fih_offset = file_offset; |
| 439 | |
| 440 | ffsfh = relative_offset(fsp, file_offset); |
| 441 | |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 442 | printk(FSP_DBG_LVL, "file type = %x\n", read_le8(&ffsfh->Type)); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 443 | printk(FSP_DBG_LVL, "file attribs = %x\n", |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 444 | read_le8(&ffsfh->Attributes)); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 445 | |
| 446 | /* Exit FV relocation when empty space found */ |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 447 | if (read_le8(&ffsfh->Type) == EFI_FV_FILETYPE_FFS_MAX) |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 448 | break; |
| 449 | |
| 450 | /* Next file on 8 byte alignment. */ |
| 451 | file_offset += ffs_file_size(ffsfh); |
| 452 | file_offset = ALIGN_UP(file_offset, 8); |
| 453 | |
| 454 | /* Padding files have no section information. */ |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 455 | if (read_le8(&ffsfh->Type) == EFI_FV_FILETYPE_FFS_PAD) |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 456 | continue; |
| 457 | |
| 458 | offset += file_section_offset(ffsfh); |
| 459 | |
| 460 | while (offset + sizeof(*csh) < file_offset) { |
| 461 | size_t data_size; |
| 462 | size_t data_offset; |
| 463 | |
| 464 | csh = relative_offset(fsp, offset); |
| 465 | |
| 466 | printk(FSP_DBG_LVL, "section offset: %zx\n", offset); |
| 467 | printk(FSP_DBG_LVL, "section type: %x\n", |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 468 | read_le8(&csh->Type)); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 469 | |
| 470 | data_size = section_data_size(csh); |
| 471 | data_offset = section_data_offset(csh); |
| 472 | |
| 473 | if (data_size + data_offset + offset > file_offset) { |
| 474 | printk(BIOS_ERR, "Section exceeds FV size.\n"); |
| 475 | return -1; |
| 476 | } |
| 477 | |
| 478 | /* |
Furquan Shaikh | b0c2fe0 | 2016-05-09 12:23:01 -0700 | [diff] [blame] | 479 | * The entire FSP image can be thought of as one |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 480 | * program with a single link address even though there |
| 481 | * are multiple TEs linked separately. The reason is |
| 482 | * that each TE is linked for XIP. So in order to |
| 483 | * relocate the TE properly we need to form the |
| 484 | * relocated address based on the TE offset within |
| 485 | * FSP proper. |
| 486 | */ |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 487 | if (read_le8(&csh->Type) == EFI_SECTION_TE) { |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 488 | void *te; |
| 489 | size_t te_offset = offset + data_offset; |
| 490 | uintptr_t te_addr = new_addr + te_offset; |
| 491 | |
| 492 | printk(FSP_DBG_LVL, "TE image at offset %zx\n", |
| 493 | te_offset); |
| 494 | te = relative_offset(fsp, te_offset); |
Aaron Durbin | 923b4d5 | 2015-09-30 16:48:26 -0500 | [diff] [blame] | 495 | te_relocate(te_addr, te); |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | offset += data_size + data_offset; |
| 499 | /* Sections are aligned to 4 bytes. */ |
| 500 | offset = ALIGN_UP(offset, 4); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | /* Return amount of buffer parsed: FV size. */ |
| 505 | return fv_length; |
| 506 | } |
| 507 | |
Furquan Shaikh | b0c2fe0 | 2016-05-09 12:23:01 -0700 | [diff] [blame] | 508 | 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] | 509 | { |
| 510 | size_t offset; |
| 511 | size_t fih_offset; |
| 512 | |
| 513 | offset = 0; |
| 514 | fih_offset = 0; |
| 515 | while (offset < size) { |
| 516 | ssize_t nparsed; |
| 517 | |
| 518 | /* Relocate each FV within the FSP region. The FSP_INFO_HEADER |
| 519 | * should only be located in the first FV. */ |
| 520 | if (offset == 0) |
| 521 | nparsed = relocate_fvh(new_addr, fsp, size, offset, |
| 522 | &fih_offset); |
| 523 | else |
| 524 | nparsed = relocate_fvh(new_addr, fsp, size, offset, |
| 525 | NULL); |
| 526 | |
| 527 | /* FV should be larger than 0 or failed to parse. */ |
| 528 | if (nparsed <= 0) { |
| 529 | printk(BIOS_ERR, "FV @ offset %zx relocation failed\n", |
| 530 | offset); |
| 531 | return -1; |
| 532 | } |
| 533 | |
| 534 | offset += nparsed; |
| 535 | } |
| 536 | |
| 537 | return relocate_remaining_items(fsp, size, new_addr, fih_offset); |
| 538 | } |
Furquan Shaikh | b0c2fe0 | 2016-05-09 12:23:01 -0700 | [diff] [blame] | 539 | |
| 540 | ssize_t fsp1_1_relocate(uintptr_t new_addr, void *fsp, size_t size) |
| 541 | { |
| 542 | return fsp_component_relocate(new_addr, fsp, size); |
| 543 | } |