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