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