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