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