blob: a8b45fa1faa5b1450572ff2d579042c19a1324ca [file] [log] [blame]
Aaron Durbina5be7fa2015-09-10 22:52:27 -05001/*
2 * This file is part of the coreboot project.
3 *
Aaron Durbina5be7fa2015-09-10 22:52:27 -05004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
Aaron Durbina5be7fa2015-09-10 22:52:27 -050012 */
13
14#include <console/console.h>
Aaron Durbin923b4d52015-09-30 16:48:26 -050015#include <commonlib/endian.h>
Furquan Shaikhb0c2fe02016-05-09 12:23:01 -070016#include <commonlib/fsp.h>
Stefan Reinauerf76ceea2016-01-30 02:05:56 -080017/*
18 * Intel's code does not have a handle on changing global packing state.
19 * Therefore, one needs to protect against packing policies that are set
Frans Hendriks47ed2692018-11-26 09:47:49 +010020 * globally for a compilation unit just by including a header file.
Stefan Reinauerf76ceea2016-01-30 02:05:56 -080021 */
22#pragma pack(push)
23
24/* Default bind FSP 1.1 API to edk2 UEFI 2.4 types. */
25#include <vendorcode/intel/edk2/uefi_2.4/uefi_types.h>
26#include <vendorcode/intel/fsp/fsp1_1/IntelFspPkg/Include/FspInfoHeader.h>
27
28/* Restore original packing policy. */
29#pragma pack(pop)
30
Aaron Durbin923b4d52015-09-30 16:48:26 -050031#include <commonlib/helpers.h>
Aaron Durbina5be7fa2015-09-10 22:52:27 -050032#include <stdlib.h>
33#include <stdint.h>
34#include <string.h>
35
36#define FSP_DBG_LVL BIOS_NEVER
37
38/*
39 * UEFI defines everything as little endian. However, this piece of code
40 * can be integrated in a userland tool. That tool could be on a big endian
41 * machine so one needs to access the fields within UEFI structures using
42 * endian-aware accesses.
43 */
44
45/* Return 0 if equal. Non-zero if not equal. */
46static int guid_compare(const EFI_GUID *le_guid, const EFI_GUID *native_guid)
47{
Aaron Durbin923b4d52015-09-30 16:48:26 -050048 if (read_le32(&le_guid->Data1) != native_guid->Data1)
Aaron Durbina5be7fa2015-09-10 22:52:27 -050049 return 1;
Aaron Durbin923b4d52015-09-30 16:48:26 -050050 if (read_le16(&le_guid->Data2) != native_guid->Data2)
Aaron Durbina5be7fa2015-09-10 22:52:27 -050051 return 1;
Aaron Durbin923b4d52015-09-30 16:48:26 -050052 if (read_le16(&le_guid->Data3) != native_guid->Data3)
Aaron Durbina5be7fa2015-09-10 22:52:27 -050053 return 1;
54 return memcmp(le_guid->Data4, native_guid->Data4,
55 ARRAY_SIZE(le_guid->Data4));
56}
57
Aaron Durbina5be7fa2015-09-10 22:52:27 -050058static const EFI_GUID ffs2_guid = EFI_FIRMWARE_FILE_SYSTEM2_GUID;
59static const EFI_GUID fih_guid = FSP_INFO_HEADER_GUID;
60
61struct fsp_patch_table {
62 uint32_t signature;
63 uint16_t header_length;
64 uint8_t header_revision;
65 uint8_t reserved;
66 uint32_t patch_entry_num;
67 uint32_t patch_entries[0];
Stefan Reinauer6a001132017-07-13 02:20:27 +020068} __packed;
Aaron Durbina5be7fa2015-09-10 22:52:27 -050069
70#define FSPP_SIG 0x50505346
71
72static void *relative_offset(void *base, ssize_t offset)
73{
74 uintptr_t loc;
75
76 loc = (uintptr_t)base;
77 loc += offset;
78
79 return (void *)loc;
80}
81
82static uint32_t *fspp_reloc(void *fsp, size_t fsp_size, uint32_t e)
83{
84 size_t offset;
85
86 /* Offsets live in bits 23:0. */
87 offset = e & 0xffffff;
88
89 /* If bit 31 is set then the offset is considered a negative value
90 * relative to the end of the image using 16MiB as the offset's
91 * reference. */
92 if (e & (1 << 31))
93 offset = fsp_size - (16 * MiB - offset);
94
95 /* Determine if offset falls within fsp_size for a 32 bit relocation. */
96 if (offset > fsp_size - sizeof(uint32_t))
97 return NULL;
98
99 return relative_offset(fsp, offset);
100}
101
102static int reloc_type(uint16_t reloc_entry)
103{
104 /* Reloc type in upper 4 bits */
105 return reloc_entry >> 12;
106}
107
108static size_t reloc_offset(uint16_t reloc_entry)
109{
110 /* Offsets are in low 12 bits. */
111 return reloc_entry & ((1 << 12) - 1);
112}
113
Aaron Durbin923b4d52015-09-30 16:48:26 -0500114static int te_relocate(uintptr_t new_addr, void *te)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500115{
116 EFI_TE_IMAGE_HEADER *teih;
117 EFI_IMAGE_DATA_DIRECTORY *relocd;
118 EFI_IMAGE_BASE_RELOCATION *relocb;
119 uintptr_t image_base;
120 size_t fixup_offset;
121 size_t num_relocs;
122 uint16_t *reloc;
123 size_t relocd_offset;
124 uint8_t *te_base;
125 uint32_t adj;
126
127 teih = te;
128
Aaron Durbin923b4d52015-09-30 16:48:26 -0500129 if (read_le16(&teih->Signature) != EFI_TE_IMAGE_HEADER_SIGNATURE) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500130 printk(BIOS_ERR, "TE Signature mismatch: %x vs %x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500131 read_le16(&teih->Signature),
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500132 EFI_TE_IMAGE_HEADER_SIGNATURE);
133 return -1;
134 }
135
136 /*
137 * A TE image is created by converting a PE file. Because of this
138 * the offsets within the headers are off. In order to calculate
Elyes HAOUAS23c1c4e2019-12-18 13:21:37 +0100139 * the correct relative offsets one needs to subtract fixup_offset
140 * from the encoded offsets. Similarly, the linked address of the
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500141 * program is found by adding the fixup_offset to the ImageBase.
142 */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500143 fixup_offset = read_le16(&teih->StrippedSize);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500144 fixup_offset -= sizeof(EFI_TE_IMAGE_HEADER);
145 /* Keep track of a base that is correctly adjusted so that offsets
146 * can be used directly. */
147 te_base = te;
148 te_base -= fixup_offset;
149
Aaron Durbin923b4d52015-09-30 16:48:26 -0500150 image_base = read_le64(&teih->ImageBase);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500151 adj = new_addr - (image_base + fixup_offset);
152
153 printk(FSP_DBG_LVL, "TE Image %p -> %p adjust value: %x\n",
154 (void *)image_base, (void *)new_addr, adj);
155
156 /* Adjust ImageBase for consistency. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500157 write_le64(&teih->ImageBase, (uint32_t)(image_base + adj));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500158
159 relocd = &teih->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_BASERELOC];
160
161 relocd_offset = 0;
162 /* Though the field name is VirtualAddress it's actually relative to
163 * the beginning of the image which is linked at ImageBase. */
164 relocb = relative_offset(te,
Aaron Durbin923b4d52015-09-30 16:48:26 -0500165 read_le32(&relocd->VirtualAddress) - fixup_offset);
166 while (relocd_offset < read_le32(&relocd->Size)) {
167 size_t rva_offset = read_le32(&relocb->VirtualAddress);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500168
169 printk(FSP_DBG_LVL, "Relocs for RVA offset %zx\n", rva_offset);
Aaron Durbin923b4d52015-09-30 16:48:26 -0500170 num_relocs = read_le32(&relocb->SizeOfBlock) - sizeof(*relocb);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500171 num_relocs /= sizeof(uint16_t);
172 reloc = relative_offset(relocb, sizeof(*relocb));
173
174 printk(FSP_DBG_LVL, "Num relocs in block: %zx\n", num_relocs);
175
176 while (num_relocs > 0) {
Aaron Durbin923b4d52015-09-30 16:48:26 -0500177 uint16_t reloc_val = read_le16(reloc);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500178 int type = reloc_type(reloc_val);
179 size_t offset = reloc_offset(reloc_val);
180
181 printk(FSP_DBG_LVL, "reloc type %x offset %zx\n",
182 type, offset);
183
184 if (type == EFI_IMAGE_REL_BASED_HIGHLOW) {
185 uint32_t *reloc_addr;
186 uint32_t val;
187
188 offset += rva_offset;
189 reloc_addr = (void *)&te_base[offset];
Aaron Durbin923b4d52015-09-30 16:48:26 -0500190 val = read_le32(reloc_addr);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500191
192 printk(FSP_DBG_LVL, "Adjusting %p %x -> %x\n",
193 reloc_addr, val, val + adj);
Aaron Durbin923b4d52015-09-30 16:48:26 -0500194 write_le32(reloc_addr, val + adj);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500195 } else if (type != EFI_IMAGE_REL_BASED_ABSOLUTE) {
196 printk(BIOS_ERR, "Unknown reloc type: %x\n",
197 type);
198 return -1;
199 }
200 num_relocs--;
201 reloc++;
202 }
203
204 /* Track consumption of relocation directory contents. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500205 relocd_offset += read_le32(&relocb->SizeOfBlock);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500206 /* Get next relocation block to process. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500207 relocb = relative_offset(relocb,
208 read_le32(&relocb->SizeOfBlock));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500209 }
210
211 return 0;
212}
213
214static size_t csh_size(const EFI_COMMON_SECTION_HEADER *csh)
215{
216 size_t size;
217
218 /* Unpack the array into a type that can be used. */
219 size = 0;
Aaron Durbin923b4d52015-09-30 16:48:26 -0500220 size |= read_le8(&csh->Size[0]) << 0;
221 size |= read_le8(&csh->Size[1]) << 8;
222 size |= read_le8(&csh->Size[2]) << 16;
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500223
224 return size;
225}
226
227static size_t section_data_offset(const EFI_COMMON_SECTION_HEADER *csh)
228{
229 if (csh_size(csh) == 0x00ffffff)
230 return sizeof(EFI_COMMON_SECTION_HEADER2);
231 else
232 return sizeof(EFI_COMMON_SECTION_HEADER);
233}
234
235static size_t section_data_size(const EFI_COMMON_SECTION_HEADER *csh)
236{
237 size_t section_size;
238
239 if (csh_size(csh) == 0x00ffffff)
Aaron Durbin923b4d52015-09-30 16:48:26 -0500240 section_size = read_le32(&SECTION2_SIZE(csh));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500241 else
242 section_size = csh_size(csh);
243
244 return section_size - section_data_offset(csh);
245}
246
247static size_t file_section_offset(const EFI_FFS_FILE_HEADER *ffsfh)
248{
249 if (IS_FFS_FILE2(ffsfh))
250 return sizeof(EFI_FFS_FILE_HEADER2);
251 else
252 return sizeof(EFI_FFS_FILE_HEADER);
253}
254
255static size_t ffs_file_size(const EFI_FFS_FILE_HEADER *ffsfh)
256{
257 size_t size;
258
Brandon Breitenstein51a0f7c2016-08-23 14:55:13 -0700259 if (IS_FFS_FILE2(ffsfh)) {
260 /*
261 * this cast is needed with UEFI 2.6 headers in order
262 * to read the UINT32 value that FFS_FILE2_SIZE converts
263 * the return into
264 */
265 uint32_t file2_size = FFS_FILE2_SIZE(ffsfh);
266 size = read_le32(&file2_size);
Lee Leahy72c60a42017-03-10 10:53:36 -0800267 } else {
Aaron Durbin923b4d52015-09-30 16:48:26 -0500268 size = read_le8(&ffsfh->Size[0]) << 0;
269 size |= read_le8(&ffsfh->Size[1]) << 8;
270 size |= read_le8(&ffsfh->Size[2]) << 16;
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500271 }
272 return size;
273}
274
275static int relocate_patch_table(void *fsp, size_t size, size_t offset,
276 ssize_t adjustment)
277{
278 struct fsp_patch_table *table;
279 size_t num;
280 size_t num_entries;
281
282 table = relative_offset(fsp, offset);
283
284 if ((offset + sizeof(*table) > size) ||
Aaron Durbin923b4d52015-09-30 16:48:26 -0500285 (read_le16(&table->header_length) + offset) > size) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500286 printk(BIOS_ERR, "FSPP not entirely contained in region.\n");
287 return -1;
288 }
289
Aaron Durbin923b4d52015-09-30 16:48:26 -0500290 num_entries = read_le32(&table->patch_entry_num);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500291 printk(FSP_DBG_LVL, "FSPP relocs: %zx\n", num_entries);
292
Aaron Durbin923b4d52015-09-30 16:48:26 -0500293 for (num = 0; num < num_entries; num++) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500294 uint32_t *reloc;
295 uint32_t reloc_val;
296
297 reloc = fspp_reloc(fsp, size,
Aaron Durbin923b4d52015-09-30 16:48:26 -0500298 read_le32(&table->patch_entries[num]));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500299
300 if (reloc == NULL) {
301 printk(BIOS_ERR, "Ignoring FSPP entry: %x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500302 read_le32(&table->patch_entries[num]));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500303 continue;
304 }
305
Aaron Durbin923b4d52015-09-30 16:48:26 -0500306 reloc_val = read_le32(reloc);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500307 printk(FSP_DBG_LVL, "Adjusting %p %x -> %x\n",
308 reloc, reloc_val,
309 (unsigned int)(reloc_val + adjustment));
310
Aaron Durbin923b4d52015-09-30 16:48:26 -0500311 write_le32(reloc, reloc_val + adjustment);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500312 }
313
314 return 0;
315}
316
317static ssize_t relocate_remaining_items(void *fsp, size_t size,
318 uintptr_t new_addr, size_t fih_offset)
319{
320 EFI_FFS_FILE_HEADER *ffsfh;
321 EFI_COMMON_SECTION_HEADER *csh;
322 FSP_INFO_HEADER *fih;
323 ssize_t adjustment;
324 size_t offset;
325
326 printk(FSP_DBG_LVL, "FSP_INFO_HEADER offset is %zx\n", fih_offset);
327
328 if (fih_offset == 0) {
329 printk(BIOS_ERR, "FSP_INFO_HEADER offset is 0.\n");
330 return -1;
331 }
332
333 /* FSP_INFO_HEADER at first file in FV within first RAW section. */
334 ffsfh = relative_offset(fsp, fih_offset);
335 fih_offset += file_section_offset(ffsfh);
336 csh = relative_offset(fsp, fih_offset);
337 fih_offset += section_data_offset(csh);
338 fih = relative_offset(fsp, fih_offset);
339
340 if (guid_compare(&ffsfh->Name, &fih_guid)) {
341 printk(BIOS_ERR, "Bad FIH GUID.\n");
342 return -1;
343 }
344
Aaron Durbin923b4d52015-09-30 16:48:26 -0500345 if (read_le8(&csh->Type) != EFI_SECTION_RAW) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500346 printk(BIOS_ERR, "FIH file should have raw section: %x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500347 read_le8(&csh->Type));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500348 return -1;
349 }
350
Aaron Durbin923b4d52015-09-30 16:48:26 -0500351 if (read_le32(&fih->Signature) != FSP_SIG) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500352 printk(BIOS_ERR, "Unexpected FIH signature: %08x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500353 read_le32(&fih->Signature));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500354 return -1;
355 }
356
Aaron Durbin923b4d52015-09-30 16:48:26 -0500357 adjustment = (intptr_t)new_addr - read_le32(&fih->ImageBase);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500358
359 /* Update ImageBase to reflect FSP's new home. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500360 write_le32(&fih->ImageBase, adjustment + read_le32(&fih->ImageBase));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500361
362 /* Need to find patch table and adjust each entry. The tables
363 * following FSP_INFO_HEADER have a 32-bit signature and header
364 * length. The patch table is denoted as having a 'FSPP' signature;
365 * the table format doesn't follow the other tables. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500366 offset = fih_offset + read_le32(&fih->HeaderLength);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500367 while (offset + 2 * sizeof(uint32_t) <= size) {
368 uint32_t *table_headers;
369
370 table_headers = relative_offset(fsp, offset);
371
372 printk(FSP_DBG_LVL, "Checking offset %zx for 'FSPP'\n",
373 offset);
374
Aaron Durbin923b4d52015-09-30 16:48:26 -0500375 if (read_le32(&table_headers[0]) != FSPP_SIG) {
376 offset += read_le32(&table_headers[1]);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500377 continue;
378 }
379
380 if (relocate_patch_table(fsp, size, offset, adjustment)) {
381 printk(BIOS_ERR, "FSPP relocation failed.\n");
382 return -1;
383 }
384
385 return fih_offset;
386 }
387
388 printk(BIOS_ERR, "Could not find the FSP patch table.\n");
389 return -1;
390}
391
392static ssize_t relocate_fvh(uintptr_t new_addr, void *fsp, size_t fsp_size,
393 size_t fvh_offset, size_t *fih_offset)
394{
395 EFI_FIRMWARE_VOLUME_HEADER *fvh;
396 EFI_FFS_FILE_HEADER *ffsfh;
397 EFI_COMMON_SECTION_HEADER *csh;
398 size_t offset;
399 size_t file_offset;
400 size_t size;
401 size_t fv_length;
402
403 offset = fvh_offset;
404 fvh = relative_offset(fsp, offset);
405
Aaron Durbin923b4d52015-09-30 16:48:26 -0500406 if (read_le32(&fvh->Signature) != EFI_FVH_SIGNATURE)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500407 return -1;
408
Aaron Durbin923b4d52015-09-30 16:48:26 -0500409 fv_length = read_le64(&fvh->FvLength);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500410
411 printk(FSP_DBG_LVL, "FVH length: %zx Offset: %zx Mapping length: %zx\n",
412 fv_length, offset, fsp_size);
413
Aaron Durbin923b4d52015-09-30 16:48:26 -0500414 if (fv_length + offset > fsp_size)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500415 return -1;
416
417 /* Parse only this FV. However, the algorithm uses offsets into the
418 * entire FSP region so make size include the starting offset. */
419 size = fv_length + offset;
420
421 if (guid_compare(&fvh->FileSystemGuid, &ffs2_guid)) {
422 printk(BIOS_ERR, "FVH not an FFS2 type.\n");
423 return -1;
424 }
425
Aaron Durbin923b4d52015-09-30 16:48:26 -0500426 if (read_le16(&fvh->ExtHeaderOffset) != 0) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500427 EFI_FIRMWARE_VOLUME_EXT_HEADER *fveh;
428
Aaron Durbin923b4d52015-09-30 16:48:26 -0500429 offset += read_le16(&fvh->ExtHeaderOffset);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500430 fveh = relative_offset(fsp, offset);
431 printk(FSP_DBG_LVL, "Extended Header Offset: %zx Size: %zx\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500432 (size_t)read_le16(&fvh->ExtHeaderOffset),
433 (size_t)read_le32(&fveh->ExtHeaderSize));
434 offset += read_le32(&fveh->ExtHeaderSize);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500435 /* FFS files are 8 byte aligned after extended header. */
436 offset = ALIGN_UP(offset, 8);
437 } else {
Aaron Durbin923b4d52015-09-30 16:48:26 -0500438 offset += read_le16(&fvh->HeaderLength);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500439 }
440
441 file_offset = offset;
442 while (file_offset + sizeof(*ffsfh) < size) {
443 offset = file_offset;
444 printk(FSP_DBG_LVL, "file offset: %zx\n", file_offset);
445
446 /* First file and section should be FSP info header. */
447 if (fih_offset != NULL && *fih_offset == 0)
448 *fih_offset = file_offset;
449
450 ffsfh = relative_offset(fsp, file_offset);
451
Aaron Durbin923b4d52015-09-30 16:48:26 -0500452 printk(FSP_DBG_LVL, "file type = %x\n", read_le8(&ffsfh->Type));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500453 printk(FSP_DBG_LVL, "file attribs = %x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500454 read_le8(&ffsfh->Attributes));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500455
456 /* Exit FV relocation when empty space found */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500457 if (read_le8(&ffsfh->Type) == EFI_FV_FILETYPE_FFS_MAX)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500458 break;
459
460 /* Next file on 8 byte alignment. */
461 file_offset += ffs_file_size(ffsfh);
462 file_offset = ALIGN_UP(file_offset, 8);
463
464 /* Padding files have no section information. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500465 if (read_le8(&ffsfh->Type) == EFI_FV_FILETYPE_FFS_PAD)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500466 continue;
467
468 offset += file_section_offset(ffsfh);
469
470 while (offset + sizeof(*csh) < file_offset) {
471 size_t data_size;
472 size_t data_offset;
473
474 csh = relative_offset(fsp, offset);
475
476 printk(FSP_DBG_LVL, "section offset: %zx\n", offset);
477 printk(FSP_DBG_LVL, "section type: %x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500478 read_le8(&csh->Type));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500479
480 data_size = section_data_size(csh);
481 data_offset = section_data_offset(csh);
482
483 if (data_size + data_offset + offset > file_offset) {
484 printk(BIOS_ERR, "Section exceeds FV size.\n");
485 return -1;
486 }
487
488 /*
Furquan Shaikhb0c2fe02016-05-09 12:23:01 -0700489 * The entire FSP image can be thought of as one
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500490 * program with a single link address even though there
491 * are multiple TEs linked separately. The reason is
492 * that each TE is linked for XIP. So in order to
493 * relocate the TE properly we need to form the
494 * relocated address based on the TE offset within
495 * FSP proper.
496 */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500497 if (read_le8(&csh->Type) == EFI_SECTION_TE) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500498 void *te;
499 size_t te_offset = offset + data_offset;
500 uintptr_t te_addr = new_addr + te_offset;
501
502 printk(FSP_DBG_LVL, "TE image at offset %zx\n",
503 te_offset);
504 te = relative_offset(fsp, te_offset);
Aaron Durbin923b4d52015-09-30 16:48:26 -0500505 te_relocate(te_addr, te);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500506 }
507
508 offset += data_size + data_offset;
509 /* Sections are aligned to 4 bytes. */
510 offset = ALIGN_UP(offset, 4);
511 }
512 }
513
514 /* Return amount of buffer parsed: FV size. */
515 return fv_length;
516}
517
Furquan Shaikhb0c2fe02016-05-09 12:23:01 -0700518ssize_t fsp_component_relocate(uintptr_t new_addr, void *fsp, size_t size)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500519{
520 size_t offset;
521 size_t fih_offset;
522
523 offset = 0;
524 fih_offset = 0;
525 while (offset < size) {
526 ssize_t nparsed;
527
528 /* Relocate each FV within the FSP region. The FSP_INFO_HEADER
529 * should only be located in the first FV. */
530 if (offset == 0)
531 nparsed = relocate_fvh(new_addr, fsp, size, offset,
532 &fih_offset);
533 else
534 nparsed = relocate_fvh(new_addr, fsp, size, offset,
535 NULL);
536
537 /* FV should be larger than 0 or failed to parse. */
538 if (nparsed <= 0) {
539 printk(BIOS_ERR, "FV @ offset %zx relocation failed\n",
540 offset);
541 return -1;
542 }
543
544 offset += nparsed;
545 }
546
547 return relocate_remaining_items(fsp, size, new_addr, fih_offset);
548}
Furquan Shaikhb0c2fe02016-05-09 12:23:01 -0700549
550ssize_t fsp1_1_relocate(uintptr_t new_addr, void *fsp, size_t size)
551{
552 return fsp_component_relocate(new_addr, fsp, size);
553}