blob: 5d326b6290a31c26b43f37bec682ea0a5b05df5b [file] [log] [blame]
Angel Ponsebda03e2020-04-02 23:48:05 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Aaron Durbina5be7fa2015-09-10 22:52:27 -05002
3#include <console/console.h>
Aaron Durbin923b4d52015-09-30 16:48:26 -05004#include <commonlib/endian.h>
Furquan Shaikhb0c2fe02016-05-09 12:23:01 -07005#include <commonlib/fsp.h>
Stefan Reinauerf76ceea72016-01-30 02:05:56 -08006/*
7 * Intel's code does not have a handle on changing global packing state.
8 * Therefore, one needs to protect against packing policies that are set
Frans Hendriks47ed2692018-11-26 09:47:49 +01009 * globally for a compilation unit just by including a header file.
Stefan Reinauerf76ceea72016-01-30 02:05:56 -080010 */
11#pragma pack(push)
12
13/* Default bind FSP 1.1 API to edk2 UEFI 2.4 types. */
14#include <vendorcode/intel/edk2/uefi_2.4/uefi_types.h>
15#include <vendorcode/intel/fsp/fsp1_1/IntelFspPkg/Include/FspInfoHeader.h>
16
17/* Restore original packing policy. */
18#pragma pack(pop)
19
Aaron Durbin923b4d52015-09-30 16:48:26 -050020#include <commonlib/helpers.h>
Aaron Durbina5be7fa2015-09-10 22:52:27 -050021#include <stdlib.h>
22#include <stdint.h>
23#include <string.h>
24
25#define FSP_DBG_LVL BIOS_NEVER
26
27/*
28 * UEFI defines everything as little endian. However, this piece of code
29 * can be integrated in a userland tool. That tool could be on a big endian
30 * machine so one needs to access the fields within UEFI structures using
31 * endian-aware accesses.
32 */
33
34/* Return 0 if equal. Non-zero if not equal. */
35static int guid_compare(const EFI_GUID *le_guid, const EFI_GUID *native_guid)
36{
Aaron Durbin923b4d52015-09-30 16:48:26 -050037 if (read_le32(&le_guid->Data1) != native_guid->Data1)
Aaron Durbina5be7fa2015-09-10 22:52:27 -050038 return 1;
Aaron Durbin923b4d52015-09-30 16:48:26 -050039 if (read_le16(&le_guid->Data2) != native_guid->Data2)
Aaron Durbina5be7fa2015-09-10 22:52:27 -050040 return 1;
Aaron Durbin923b4d52015-09-30 16:48:26 -050041 if (read_le16(&le_guid->Data3) != native_guid->Data3)
Aaron Durbina5be7fa2015-09-10 22:52:27 -050042 return 1;
43 return memcmp(le_guid->Data4, native_guid->Data4,
44 ARRAY_SIZE(le_guid->Data4));
45}
46
Aaron Durbina5be7fa2015-09-10 22:52:27 -050047static const EFI_GUID ffs2_guid = EFI_FIRMWARE_FILE_SYSTEM2_GUID;
48static const EFI_GUID fih_guid = FSP_INFO_HEADER_GUID;
49
50struct fsp_patch_table {
51 uint32_t signature;
52 uint16_t header_length;
53 uint8_t header_revision;
54 uint8_t reserved;
55 uint32_t patch_entry_num;
56 uint32_t patch_entries[0];
Stefan Reinauer6a001132017-07-13 02:20:27 +020057} __packed;
Aaron Durbina5be7fa2015-09-10 22:52:27 -050058
59#define FSPP_SIG 0x50505346
60
61static void *relative_offset(void *base, ssize_t offset)
62{
63 uintptr_t loc;
64
65 loc = (uintptr_t)base;
66 loc += offset;
67
68 return (void *)loc;
69}
70
71static uint32_t *fspp_reloc(void *fsp, size_t fsp_size, uint32_t e)
72{
73 size_t offset;
74
75 /* Offsets live in bits 23:0. */
76 offset = e & 0xffffff;
77
78 /* If bit 31 is set then the offset is considered a negative value
79 * relative to the end of the image using 16MiB as the offset's
80 * reference. */
81 if (e & (1 << 31))
82 offset = fsp_size - (16 * MiB - offset);
83
84 /* Determine if offset falls within fsp_size for a 32 bit relocation. */
85 if (offset > fsp_size - sizeof(uint32_t))
86 return NULL;
87
88 return relative_offset(fsp, offset);
89}
90
91static int reloc_type(uint16_t reloc_entry)
92{
93 /* Reloc type in upper 4 bits */
94 return reloc_entry >> 12;
95}
96
97static size_t reloc_offset(uint16_t reloc_entry)
98{
99 /* Offsets are in low 12 bits. */
100 return reloc_entry & ((1 << 12) - 1);
101}
102
Aaron Durbin923b4d52015-09-30 16:48:26 -0500103static int te_relocate(uintptr_t new_addr, void *te)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500104{
105 EFI_TE_IMAGE_HEADER *teih;
106 EFI_IMAGE_DATA_DIRECTORY *relocd;
107 EFI_IMAGE_BASE_RELOCATION *relocb;
108 uintptr_t image_base;
109 size_t fixup_offset;
110 size_t num_relocs;
111 uint16_t *reloc;
112 size_t relocd_offset;
113 uint8_t *te_base;
114 uint32_t adj;
115
116 teih = te;
117
Aaron Durbin923b4d52015-09-30 16:48:26 -0500118 if (read_le16(&teih->Signature) != EFI_TE_IMAGE_HEADER_SIGNATURE) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500119 printk(BIOS_ERR, "TE Signature mismatch: %x vs %x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500120 read_le16(&teih->Signature),
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500121 EFI_TE_IMAGE_HEADER_SIGNATURE);
122 return -1;
123 }
124
125 /*
126 * A TE image is created by converting a PE file. Because of this
127 * the offsets within the headers are off. In order to calculate
Elyes HAOUAS23c1c4e2019-12-18 13:21:37 +0100128 * the correct relative offsets one needs to subtract fixup_offset
129 * from the encoded offsets. Similarly, the linked address of the
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500130 * program is found by adding the fixup_offset to the ImageBase.
131 */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500132 fixup_offset = read_le16(&teih->StrippedSize);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500133 fixup_offset -= sizeof(EFI_TE_IMAGE_HEADER);
134 /* Keep track of a base that is correctly adjusted so that offsets
135 * can be used directly. */
136 te_base = te;
137 te_base -= fixup_offset;
138
Aaron Durbin923b4d52015-09-30 16:48:26 -0500139 image_base = read_le64(&teih->ImageBase);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500140 adj = new_addr - (image_base + fixup_offset);
141
142 printk(FSP_DBG_LVL, "TE Image %p -> %p adjust value: %x\n",
143 (void *)image_base, (void *)new_addr, adj);
144
145 /* Adjust ImageBase for consistency. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500146 write_le64(&teih->ImageBase, (uint32_t)(image_base + adj));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500147
148 relocd = &teih->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_BASERELOC];
149
150 relocd_offset = 0;
151 /* Though the field name is VirtualAddress it's actually relative to
152 * the beginning of the image which is linked at ImageBase. */
153 relocb = relative_offset(te,
Aaron Durbin923b4d52015-09-30 16:48:26 -0500154 read_le32(&relocd->VirtualAddress) - fixup_offset);
155 while (relocd_offset < read_le32(&relocd->Size)) {
156 size_t rva_offset = read_le32(&relocb->VirtualAddress);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500157
158 printk(FSP_DBG_LVL, "Relocs for RVA offset %zx\n", rva_offset);
Aaron Durbin923b4d52015-09-30 16:48:26 -0500159 num_relocs = read_le32(&relocb->SizeOfBlock) - sizeof(*relocb);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500160 num_relocs /= sizeof(uint16_t);
161 reloc = relative_offset(relocb, sizeof(*relocb));
162
163 printk(FSP_DBG_LVL, "Num relocs in block: %zx\n", num_relocs);
164
165 while (num_relocs > 0) {
Aaron Durbin923b4d52015-09-30 16:48:26 -0500166 uint16_t reloc_val = read_le16(reloc);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500167 int type = reloc_type(reloc_val);
168 size_t offset = reloc_offset(reloc_val);
169
170 printk(FSP_DBG_LVL, "reloc type %x offset %zx\n",
171 type, offset);
172
173 if (type == EFI_IMAGE_REL_BASED_HIGHLOW) {
174 uint32_t *reloc_addr;
175 uint32_t val;
176
177 offset += rva_offset;
178 reloc_addr = (void *)&te_base[offset];
Aaron Durbin923b4d52015-09-30 16:48:26 -0500179 val = read_le32(reloc_addr);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500180
181 printk(FSP_DBG_LVL, "Adjusting %p %x -> %x\n",
182 reloc_addr, val, val + adj);
Aaron Durbin923b4d52015-09-30 16:48:26 -0500183 write_le32(reloc_addr, val + adj);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500184 } else if (type != EFI_IMAGE_REL_BASED_ABSOLUTE) {
185 printk(BIOS_ERR, "Unknown reloc type: %x\n",
186 type);
187 return -1;
188 }
189 num_relocs--;
190 reloc++;
191 }
192
193 /* Track consumption of relocation directory contents. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500194 relocd_offset += read_le32(&relocb->SizeOfBlock);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500195 /* Get next relocation block to process. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500196 relocb = relative_offset(relocb,
197 read_le32(&relocb->SizeOfBlock));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500198 }
199
200 return 0;
201}
202
203static size_t csh_size(const EFI_COMMON_SECTION_HEADER *csh)
204{
205 size_t size;
206
207 /* Unpack the array into a type that can be used. */
208 size = 0;
Aaron Durbin923b4d52015-09-30 16:48:26 -0500209 size |= read_le8(&csh->Size[0]) << 0;
210 size |= read_le8(&csh->Size[1]) << 8;
211 size |= read_le8(&csh->Size[2]) << 16;
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500212
213 return size;
214}
215
216static size_t section_data_offset(const EFI_COMMON_SECTION_HEADER *csh)
217{
218 if (csh_size(csh) == 0x00ffffff)
219 return sizeof(EFI_COMMON_SECTION_HEADER2);
220 else
221 return sizeof(EFI_COMMON_SECTION_HEADER);
222}
223
224static size_t section_data_size(const EFI_COMMON_SECTION_HEADER *csh)
225{
226 size_t section_size;
227
228 if (csh_size(csh) == 0x00ffffff)
Aaron Durbin923b4d52015-09-30 16:48:26 -0500229 section_size = read_le32(&SECTION2_SIZE(csh));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500230 else
231 section_size = csh_size(csh);
232
233 return section_size - section_data_offset(csh);
234}
235
236static size_t file_section_offset(const EFI_FFS_FILE_HEADER *ffsfh)
237{
238 if (IS_FFS_FILE2(ffsfh))
239 return sizeof(EFI_FFS_FILE_HEADER2);
240 else
241 return sizeof(EFI_FFS_FILE_HEADER);
242}
243
244static size_t ffs_file_size(const EFI_FFS_FILE_HEADER *ffsfh)
245{
246 size_t size;
247
Brandon Breitenstein51a0f7c2016-08-23 14:55:13 -0700248 if (IS_FFS_FILE2(ffsfh)) {
249 /*
250 * this cast is needed with UEFI 2.6 headers in order
251 * to read the UINT32 value that FFS_FILE2_SIZE converts
252 * the return into
253 */
254 uint32_t file2_size = FFS_FILE2_SIZE(ffsfh);
255 size = read_le32(&file2_size);
Lee Leahy72c60a42017-03-10 10:53:36 -0800256 } else {
Aaron Durbin923b4d52015-09-30 16:48:26 -0500257 size = read_le8(&ffsfh->Size[0]) << 0;
258 size |= read_le8(&ffsfh->Size[1]) << 8;
259 size |= read_le8(&ffsfh->Size[2]) << 16;
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500260 }
261 return size;
262}
263
264static int relocate_patch_table(void *fsp, size_t size, size_t offset,
265 ssize_t adjustment)
266{
267 struct fsp_patch_table *table;
268 size_t num;
269 size_t num_entries;
270
271 table = relative_offset(fsp, offset);
272
273 if ((offset + sizeof(*table) > size) ||
Aaron Durbin923b4d52015-09-30 16:48:26 -0500274 (read_le16(&table->header_length) + offset) > size) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500275 printk(BIOS_ERR, "FSPP not entirely contained in region.\n");
276 return -1;
277 }
278
Aaron Durbin923b4d52015-09-30 16:48:26 -0500279 num_entries = read_le32(&table->patch_entry_num);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500280 printk(FSP_DBG_LVL, "FSPP relocs: %zx\n", num_entries);
281
Aaron Durbin923b4d52015-09-30 16:48:26 -0500282 for (num = 0; num < num_entries; num++) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500283 uint32_t *reloc;
284 uint32_t reloc_val;
285
286 reloc = fspp_reloc(fsp, size,
Aaron Durbin923b4d52015-09-30 16:48:26 -0500287 read_le32(&table->patch_entries[num]));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500288
289 if (reloc == NULL) {
290 printk(BIOS_ERR, "Ignoring FSPP entry: %x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500291 read_le32(&table->patch_entries[num]));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500292 continue;
293 }
294
Aaron Durbin923b4d52015-09-30 16:48:26 -0500295 reloc_val = read_le32(reloc);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500296 printk(FSP_DBG_LVL, "Adjusting %p %x -> %x\n",
297 reloc, reloc_val,
298 (unsigned int)(reloc_val + adjustment));
299
Aaron Durbin923b4d52015-09-30 16:48:26 -0500300 write_le32(reloc, reloc_val + adjustment);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500301 }
302
303 return 0;
304}
305
306static ssize_t relocate_remaining_items(void *fsp, size_t size,
307 uintptr_t new_addr, size_t fih_offset)
308{
309 EFI_FFS_FILE_HEADER *ffsfh;
310 EFI_COMMON_SECTION_HEADER *csh;
311 FSP_INFO_HEADER *fih;
312 ssize_t adjustment;
313 size_t offset;
314
315 printk(FSP_DBG_LVL, "FSP_INFO_HEADER offset is %zx\n", fih_offset);
316
317 if (fih_offset == 0) {
318 printk(BIOS_ERR, "FSP_INFO_HEADER offset is 0.\n");
319 return -1;
320 }
321
322 /* FSP_INFO_HEADER at first file in FV within first RAW section. */
323 ffsfh = relative_offset(fsp, fih_offset);
324 fih_offset += file_section_offset(ffsfh);
325 csh = relative_offset(fsp, fih_offset);
326 fih_offset += section_data_offset(csh);
327 fih = relative_offset(fsp, fih_offset);
328
329 if (guid_compare(&ffsfh->Name, &fih_guid)) {
330 printk(BIOS_ERR, "Bad FIH GUID.\n");
331 return -1;
332 }
333
Aaron Durbin923b4d52015-09-30 16:48:26 -0500334 if (read_le8(&csh->Type) != EFI_SECTION_RAW) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500335 printk(BIOS_ERR, "FIH file should have raw section: %x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500336 read_le8(&csh->Type));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500337 return -1;
338 }
339
Aaron Durbin923b4d52015-09-30 16:48:26 -0500340 if (read_le32(&fih->Signature) != FSP_SIG) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500341 printk(BIOS_ERR, "Unexpected FIH signature: %08x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500342 read_le32(&fih->Signature));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500343 return -1;
344 }
345
Aaron Durbin923b4d52015-09-30 16:48:26 -0500346 adjustment = (intptr_t)new_addr - read_le32(&fih->ImageBase);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500347
348 /* Update ImageBase to reflect FSP's new home. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500349 write_le32(&fih->ImageBase, adjustment + read_le32(&fih->ImageBase));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500350
351 /* Need to find patch table and adjust each entry. The tables
352 * following FSP_INFO_HEADER have a 32-bit signature and header
353 * length. The patch table is denoted as having a 'FSPP' signature;
354 * the table format doesn't follow the other tables. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500355 offset = fih_offset + read_le32(&fih->HeaderLength);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500356 while (offset + 2 * sizeof(uint32_t) <= size) {
357 uint32_t *table_headers;
358
359 table_headers = relative_offset(fsp, offset);
360
361 printk(FSP_DBG_LVL, "Checking offset %zx for 'FSPP'\n",
362 offset);
363
Aaron Durbin923b4d52015-09-30 16:48:26 -0500364 if (read_le32(&table_headers[0]) != FSPP_SIG) {
365 offset += read_le32(&table_headers[1]);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500366 continue;
367 }
368
369 if (relocate_patch_table(fsp, size, offset, adjustment)) {
370 printk(BIOS_ERR, "FSPP relocation failed.\n");
371 return -1;
372 }
373
374 return fih_offset;
375 }
376
377 printk(BIOS_ERR, "Could not find the FSP patch table.\n");
378 return -1;
379}
380
381static ssize_t relocate_fvh(uintptr_t new_addr, void *fsp, size_t fsp_size,
382 size_t fvh_offset, size_t *fih_offset)
383{
384 EFI_FIRMWARE_VOLUME_HEADER *fvh;
385 EFI_FFS_FILE_HEADER *ffsfh;
386 EFI_COMMON_SECTION_HEADER *csh;
387 size_t offset;
388 size_t file_offset;
389 size_t size;
390 size_t fv_length;
391
392 offset = fvh_offset;
393 fvh = relative_offset(fsp, offset);
394
Aaron Durbin923b4d52015-09-30 16:48:26 -0500395 if (read_le32(&fvh->Signature) != EFI_FVH_SIGNATURE)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500396 return -1;
397
Aaron Durbin923b4d52015-09-30 16:48:26 -0500398 fv_length = read_le64(&fvh->FvLength);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500399
400 printk(FSP_DBG_LVL, "FVH length: %zx Offset: %zx Mapping length: %zx\n",
401 fv_length, offset, fsp_size);
402
Aaron Durbin923b4d52015-09-30 16:48:26 -0500403 if (fv_length + offset > fsp_size)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500404 return -1;
405
406 /* Parse only this FV. However, the algorithm uses offsets into the
407 * entire FSP region so make size include the starting offset. */
408 size = fv_length + offset;
409
410 if (guid_compare(&fvh->FileSystemGuid, &ffs2_guid)) {
411 printk(BIOS_ERR, "FVH not an FFS2 type.\n");
412 return -1;
413 }
414
Aaron Durbin923b4d52015-09-30 16:48:26 -0500415 if (read_le16(&fvh->ExtHeaderOffset) != 0) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500416 EFI_FIRMWARE_VOLUME_EXT_HEADER *fveh;
417
Aaron Durbin923b4d52015-09-30 16:48:26 -0500418 offset += read_le16(&fvh->ExtHeaderOffset);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500419 fveh = relative_offset(fsp, offset);
420 printk(FSP_DBG_LVL, "Extended Header Offset: %zx Size: %zx\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500421 (size_t)read_le16(&fvh->ExtHeaderOffset),
422 (size_t)read_le32(&fveh->ExtHeaderSize));
423 offset += read_le32(&fveh->ExtHeaderSize);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500424 /* FFS files are 8 byte aligned after extended header. */
425 offset = ALIGN_UP(offset, 8);
426 } else {
Aaron Durbin923b4d52015-09-30 16:48:26 -0500427 offset += read_le16(&fvh->HeaderLength);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500428 }
429
430 file_offset = offset;
431 while (file_offset + sizeof(*ffsfh) < size) {
432 offset = file_offset;
433 printk(FSP_DBG_LVL, "file offset: %zx\n", file_offset);
434
435 /* First file and section should be FSP info header. */
436 if (fih_offset != NULL && *fih_offset == 0)
437 *fih_offset = file_offset;
438
439 ffsfh = relative_offset(fsp, file_offset);
440
Aaron Durbin923b4d52015-09-30 16:48:26 -0500441 printk(FSP_DBG_LVL, "file type = %x\n", read_le8(&ffsfh->Type));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500442 printk(FSP_DBG_LVL, "file attribs = %x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500443 read_le8(&ffsfh->Attributes));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500444
445 /* Exit FV relocation when empty space found */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500446 if (read_le8(&ffsfh->Type) == EFI_FV_FILETYPE_FFS_MAX)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500447 break;
448
449 /* Next file on 8 byte alignment. */
450 file_offset += ffs_file_size(ffsfh);
451 file_offset = ALIGN_UP(file_offset, 8);
452
453 /* Padding files have no section information. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500454 if (read_le8(&ffsfh->Type) == EFI_FV_FILETYPE_FFS_PAD)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500455 continue;
456
457 offset += file_section_offset(ffsfh);
458
459 while (offset + sizeof(*csh) < file_offset) {
460 size_t data_size;
461 size_t data_offset;
462
463 csh = relative_offset(fsp, offset);
464
465 printk(FSP_DBG_LVL, "section offset: %zx\n", offset);
466 printk(FSP_DBG_LVL, "section type: %x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500467 read_le8(&csh->Type));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500468
469 data_size = section_data_size(csh);
470 data_offset = section_data_offset(csh);
471
472 if (data_size + data_offset + offset > file_offset) {
473 printk(BIOS_ERR, "Section exceeds FV size.\n");
474 return -1;
475 }
476
477 /*
Furquan Shaikhb0c2fe02016-05-09 12:23:01 -0700478 * The entire FSP image can be thought of as one
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500479 * program with a single link address even though there
480 * are multiple TEs linked separately. The reason is
481 * that each TE is linked for XIP. So in order to
482 * relocate the TE properly we need to form the
483 * relocated address based on the TE offset within
484 * FSP proper.
485 */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500486 if (read_le8(&csh->Type) == EFI_SECTION_TE) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500487 void *te;
488 size_t te_offset = offset + data_offset;
489 uintptr_t te_addr = new_addr + te_offset;
490
491 printk(FSP_DBG_LVL, "TE image at offset %zx\n",
492 te_offset);
493 te = relative_offset(fsp, te_offset);
Aaron Durbin923b4d52015-09-30 16:48:26 -0500494 te_relocate(te_addr, te);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500495 }
496
497 offset += data_size + data_offset;
498 /* Sections are aligned to 4 bytes. */
499 offset = ALIGN_UP(offset, 4);
500 }
501 }
502
503 /* Return amount of buffer parsed: FV size. */
504 return fv_length;
505}
506
Furquan Shaikhb0c2fe02016-05-09 12:23:01 -0700507ssize_t fsp_component_relocate(uintptr_t new_addr, void *fsp, size_t size)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500508{
509 size_t offset;
510 size_t fih_offset;
511
512 offset = 0;
513 fih_offset = 0;
514 while (offset < size) {
515 ssize_t nparsed;
516
517 /* Relocate each FV within the FSP region. The FSP_INFO_HEADER
518 * should only be located in the first FV. */
519 if (offset == 0)
520 nparsed = relocate_fvh(new_addr, fsp, size, offset,
521 &fih_offset);
522 else
523 nparsed = relocate_fvh(new_addr, fsp, size, offset,
524 NULL);
525
526 /* FV should be larger than 0 or failed to parse. */
527 if (nparsed <= 0) {
528 printk(BIOS_ERR, "FV @ offset %zx relocation failed\n",
529 offset);
530 return -1;
531 }
532
533 offset += nparsed;
534 }
535
536 return relocate_remaining_items(fsp, size, new_addr, fih_offset);
537}
Furquan Shaikhb0c2fe02016-05-09 12:23:01 -0700538
539ssize_t fsp1_1_relocate(uintptr_t new_addr, void *fsp, size_t size)
540{
541 return fsp_component_relocate(new_addr, fsp, size);
542}