blob: d96edd2178f663babe790c1a53aff19d83e40c8c [file] [log] [blame]
Aaron Durbina5be7fa2015-09-10 22:52:27 -05001/*
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 Durbina5be7fa2015-09-10 22:52:27 -050014 */
15
Stefan Reinauer6a001132017-07-13 02:20:27 +020016#include <compiler.h>
Aaron Durbina5be7fa2015-09-10 22:52:27 -050017#include <console/console.h>
Aaron Durbin923b4d52015-09-30 16:48:26 -050018#include <commonlib/endian.h>
Furquan Shaikhb0c2fe02016-05-09 12:23:01 -070019#include <commonlib/fsp.h>
Stefan Reinauerf76ceea2016-01-30 02:05:56 -080020/*
21 * Intel's code does not have a handle on changing global packing state.
22 * Therefore, one needs to protect against packing policies that are set
23 * globally for a compliation unit just by including a header file.
24 */
25#pragma pack(push)
26
27/* Default bind FSP 1.1 API to edk2 UEFI 2.4 types. */
28#include <vendorcode/intel/edk2/uefi_2.4/uefi_types.h>
29#include <vendorcode/intel/fsp/fsp1_1/IntelFspPkg/Include/FspInfoHeader.h>
30
31/* Restore original packing policy. */
32#pragma pack(pop)
33
Aaron Durbin923b4d52015-09-30 16:48:26 -050034#include <commonlib/helpers.h>
Aaron Durbina5be7fa2015-09-10 22:52:27 -050035#include <stdlib.h>
36#include <stdint.h>
37#include <string.h>
38
39#define FSP_DBG_LVL BIOS_NEVER
40
41/*
42 * UEFI defines everything as little endian. However, this piece of code
43 * can be integrated in a userland tool. That tool could be on a big endian
44 * machine so one needs to access the fields within UEFI structures using
45 * endian-aware accesses.
46 */
47
48/* Return 0 if equal. Non-zero if not equal. */
49static int guid_compare(const EFI_GUID *le_guid, const EFI_GUID *native_guid)
50{
Aaron Durbin923b4d52015-09-30 16:48:26 -050051 if (read_le32(&le_guid->Data1) != native_guid->Data1)
Aaron Durbina5be7fa2015-09-10 22:52:27 -050052 return 1;
Aaron Durbin923b4d52015-09-30 16:48:26 -050053 if (read_le16(&le_guid->Data2) != native_guid->Data2)
Aaron Durbina5be7fa2015-09-10 22:52:27 -050054 return 1;
Aaron Durbin923b4d52015-09-30 16:48:26 -050055 if (read_le16(&le_guid->Data3) != native_guid->Data3)
Aaron Durbina5be7fa2015-09-10 22:52:27 -050056 return 1;
57 return memcmp(le_guid->Data4, native_guid->Data4,
58 ARRAY_SIZE(le_guid->Data4));
59}
60
Aaron Durbina5be7fa2015-09-10 22:52:27 -050061static const EFI_GUID ffs2_guid = EFI_FIRMWARE_FILE_SYSTEM2_GUID;
62static const EFI_GUID fih_guid = FSP_INFO_HEADER_GUID;
63
64struct fsp_patch_table {
65 uint32_t signature;
66 uint16_t header_length;
67 uint8_t header_revision;
68 uint8_t reserved;
69 uint32_t patch_entry_num;
70 uint32_t patch_entries[0];
Stefan Reinauer6a001132017-07-13 02:20:27 +020071} __packed;
Aaron Durbina5be7fa2015-09-10 22:52:27 -050072
73#define FSPP_SIG 0x50505346
74
75static void *relative_offset(void *base, ssize_t offset)
76{
77 uintptr_t loc;
78
79 loc = (uintptr_t)base;
80 loc += offset;
81
82 return (void *)loc;
83}
84
85static uint32_t *fspp_reloc(void *fsp, size_t fsp_size, uint32_t e)
86{
87 size_t offset;
88
89 /* Offsets live in bits 23:0. */
90 offset = e & 0xffffff;
91
92 /* If bit 31 is set then the offset is considered a negative value
93 * relative to the end of the image using 16MiB as the offset's
94 * reference. */
95 if (e & (1 << 31))
96 offset = fsp_size - (16 * MiB - offset);
97
98 /* Determine if offset falls within fsp_size for a 32 bit relocation. */
99 if (offset > fsp_size - sizeof(uint32_t))
100 return NULL;
101
102 return relative_offset(fsp, offset);
103}
104
105static int reloc_type(uint16_t reloc_entry)
106{
107 /* Reloc type in upper 4 bits */
108 return reloc_entry >> 12;
109}
110
111static size_t reloc_offset(uint16_t reloc_entry)
112{
113 /* Offsets are in low 12 bits. */
114 return reloc_entry & ((1 << 12) - 1);
115}
116
Aaron Durbin923b4d52015-09-30 16:48:26 -0500117static int te_relocate(uintptr_t new_addr, void *te)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500118{
119 EFI_TE_IMAGE_HEADER *teih;
120 EFI_IMAGE_DATA_DIRECTORY *relocd;
121 EFI_IMAGE_BASE_RELOCATION *relocb;
122 uintptr_t image_base;
123 size_t fixup_offset;
124 size_t num_relocs;
125 uint16_t *reloc;
126 size_t relocd_offset;
127 uint8_t *te_base;
128 uint32_t adj;
129
130 teih = te;
131
Aaron Durbin923b4d52015-09-30 16:48:26 -0500132 if (read_le16(&teih->Signature) != EFI_TE_IMAGE_HEADER_SIGNATURE) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500133 printk(BIOS_ERR, "TE Signature mismatch: %x vs %x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500134 read_le16(&teih->Signature),
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500135 EFI_TE_IMAGE_HEADER_SIGNATURE);
136 return -1;
137 }
138
139 /*
140 * A TE image is created by converting a PE file. Because of this
141 * the offsets within the headers are off. In order to calculate
142 * the correct releative offets one needs to subtract fixup_offset
143 * from the encoded offets. Similarly, the linked address of the
144 * program is found by adding the fixup_offset to the ImageBase.
145 */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500146 fixup_offset = read_le16(&teih->StrippedSize);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500147 fixup_offset -= sizeof(EFI_TE_IMAGE_HEADER);
148 /* Keep track of a base that is correctly adjusted so that offsets
149 * can be used directly. */
150 te_base = te;
151 te_base -= fixup_offset;
152
Aaron Durbin923b4d52015-09-30 16:48:26 -0500153 image_base = read_le64(&teih->ImageBase);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500154 adj = new_addr - (image_base + fixup_offset);
155
156 printk(FSP_DBG_LVL, "TE Image %p -> %p adjust value: %x\n",
157 (void *)image_base, (void *)new_addr, adj);
158
159 /* Adjust ImageBase for consistency. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500160 write_le64(&teih->ImageBase, (uint32_t)(image_base + adj));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500161
162 relocd = &teih->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_BASERELOC];
163
164 relocd_offset = 0;
165 /* Though the field name is VirtualAddress it's actually relative to
166 * the beginning of the image which is linked at ImageBase. */
167 relocb = relative_offset(te,
Aaron Durbin923b4d52015-09-30 16:48:26 -0500168 read_le32(&relocd->VirtualAddress) - fixup_offset);
169 while (relocd_offset < read_le32(&relocd->Size)) {
170 size_t rva_offset = read_le32(&relocb->VirtualAddress);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500171
172 printk(FSP_DBG_LVL, "Relocs for RVA offset %zx\n", rva_offset);
Aaron Durbin923b4d52015-09-30 16:48:26 -0500173 num_relocs = read_le32(&relocb->SizeOfBlock) - sizeof(*relocb);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500174 num_relocs /= sizeof(uint16_t);
175 reloc = relative_offset(relocb, sizeof(*relocb));
176
177 printk(FSP_DBG_LVL, "Num relocs in block: %zx\n", num_relocs);
178
179 while (num_relocs > 0) {
Aaron Durbin923b4d52015-09-30 16:48:26 -0500180 uint16_t reloc_val = read_le16(reloc);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500181 int type = reloc_type(reloc_val);
182 size_t offset = reloc_offset(reloc_val);
183
184 printk(FSP_DBG_LVL, "reloc type %x offset %zx\n",
185 type, offset);
186
187 if (type == EFI_IMAGE_REL_BASED_HIGHLOW) {
188 uint32_t *reloc_addr;
189 uint32_t val;
190
191 offset += rva_offset;
192 reloc_addr = (void *)&te_base[offset];
Aaron Durbin923b4d52015-09-30 16:48:26 -0500193 val = read_le32(reloc_addr);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500194
195 printk(FSP_DBG_LVL, "Adjusting %p %x -> %x\n",
196 reloc_addr, val, val + adj);
Aaron Durbin923b4d52015-09-30 16:48:26 -0500197 write_le32(reloc_addr, val + adj);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500198 } else if (type != EFI_IMAGE_REL_BASED_ABSOLUTE) {
199 printk(BIOS_ERR, "Unknown reloc type: %x\n",
200 type);
201 return -1;
202 }
203 num_relocs--;
204 reloc++;
205 }
206
207 /* Track consumption of relocation directory contents. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500208 relocd_offset += read_le32(&relocb->SizeOfBlock);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500209 /* Get next relocation block to process. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500210 relocb = relative_offset(relocb,
211 read_le32(&relocb->SizeOfBlock));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500212 }
213
214 return 0;
215}
216
217static size_t csh_size(const EFI_COMMON_SECTION_HEADER *csh)
218{
219 size_t size;
220
221 /* Unpack the array into a type that can be used. */
222 size = 0;
Aaron Durbin923b4d52015-09-30 16:48:26 -0500223 size |= read_le8(&csh->Size[0]) << 0;
224 size |= read_le8(&csh->Size[1]) << 8;
225 size |= read_le8(&csh->Size[2]) << 16;
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500226
227 return size;
228}
229
230static size_t section_data_offset(const EFI_COMMON_SECTION_HEADER *csh)
231{
232 if (csh_size(csh) == 0x00ffffff)
233 return sizeof(EFI_COMMON_SECTION_HEADER2);
234 else
235 return sizeof(EFI_COMMON_SECTION_HEADER);
236}
237
238static size_t section_data_size(const EFI_COMMON_SECTION_HEADER *csh)
239{
240 size_t section_size;
241
242 if (csh_size(csh) == 0x00ffffff)
Aaron Durbin923b4d52015-09-30 16:48:26 -0500243 section_size = read_le32(&SECTION2_SIZE(csh));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500244 else
245 section_size = csh_size(csh);
246
247 return section_size - section_data_offset(csh);
248}
249
250static size_t file_section_offset(const EFI_FFS_FILE_HEADER *ffsfh)
251{
252 if (IS_FFS_FILE2(ffsfh))
253 return sizeof(EFI_FFS_FILE_HEADER2);
254 else
255 return sizeof(EFI_FFS_FILE_HEADER);
256}
257
258static size_t ffs_file_size(const EFI_FFS_FILE_HEADER *ffsfh)
259{
260 size_t size;
261
Brandon Breitenstein51a0f7c2016-08-23 14:55:13 -0700262 if (IS_FFS_FILE2(ffsfh)) {
263 /*
264 * this cast is needed with UEFI 2.6 headers in order
265 * to read the UINT32 value that FFS_FILE2_SIZE converts
266 * the return into
267 */
268 uint32_t file2_size = FFS_FILE2_SIZE(ffsfh);
269 size = read_le32(&file2_size);
Lee Leahy72c60a42017-03-10 10:53:36 -0800270 } else {
Aaron Durbin923b4d52015-09-30 16:48:26 -0500271 size = read_le8(&ffsfh->Size[0]) << 0;
272 size |= read_le8(&ffsfh->Size[1]) << 8;
273 size |= read_le8(&ffsfh->Size[2]) << 16;
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500274 }
275 return size;
276}
277
278static int relocate_patch_table(void *fsp, size_t size, size_t offset,
279 ssize_t adjustment)
280{
281 struct fsp_patch_table *table;
282 size_t num;
283 size_t num_entries;
284
285 table = relative_offset(fsp, offset);
286
287 if ((offset + sizeof(*table) > size) ||
Aaron Durbin923b4d52015-09-30 16:48:26 -0500288 (read_le16(&table->header_length) + offset) > size) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500289 printk(BIOS_ERR, "FSPP not entirely contained in region.\n");
290 return -1;
291 }
292
Aaron Durbin923b4d52015-09-30 16:48:26 -0500293 num_entries = read_le32(&table->patch_entry_num);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500294 printk(FSP_DBG_LVL, "FSPP relocs: %zx\n", num_entries);
295
Aaron Durbin923b4d52015-09-30 16:48:26 -0500296 for (num = 0; num < num_entries; num++) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500297 uint32_t *reloc;
298 uint32_t reloc_val;
299
300 reloc = fspp_reloc(fsp, size,
Aaron Durbin923b4d52015-09-30 16:48:26 -0500301 read_le32(&table->patch_entries[num]));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500302
303 if (reloc == NULL) {
304 printk(BIOS_ERR, "Ignoring FSPP entry: %x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500305 read_le32(&table->patch_entries[num]));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500306 continue;
307 }
308
Aaron Durbin923b4d52015-09-30 16:48:26 -0500309 reloc_val = read_le32(reloc);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500310 printk(FSP_DBG_LVL, "Adjusting %p %x -> %x\n",
311 reloc, reloc_val,
312 (unsigned int)(reloc_val + adjustment));
313
Aaron Durbin923b4d52015-09-30 16:48:26 -0500314 write_le32(reloc, reloc_val + adjustment);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500315 }
316
317 return 0;
318}
319
320static ssize_t relocate_remaining_items(void *fsp, size_t size,
321 uintptr_t new_addr, size_t fih_offset)
322{
323 EFI_FFS_FILE_HEADER *ffsfh;
324 EFI_COMMON_SECTION_HEADER *csh;
325 FSP_INFO_HEADER *fih;
326 ssize_t adjustment;
327 size_t offset;
328
329 printk(FSP_DBG_LVL, "FSP_INFO_HEADER offset is %zx\n", fih_offset);
330
331 if (fih_offset == 0) {
332 printk(BIOS_ERR, "FSP_INFO_HEADER offset is 0.\n");
333 return -1;
334 }
335
336 /* FSP_INFO_HEADER at first file in FV within first RAW section. */
337 ffsfh = relative_offset(fsp, fih_offset);
338 fih_offset += file_section_offset(ffsfh);
339 csh = relative_offset(fsp, fih_offset);
340 fih_offset += section_data_offset(csh);
341 fih = relative_offset(fsp, fih_offset);
342
343 if (guid_compare(&ffsfh->Name, &fih_guid)) {
344 printk(BIOS_ERR, "Bad FIH GUID.\n");
345 return -1;
346 }
347
Aaron Durbin923b4d52015-09-30 16:48:26 -0500348 if (read_le8(&csh->Type) != EFI_SECTION_RAW) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500349 printk(BIOS_ERR, "FIH file should have raw section: %x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500350 read_le8(&csh->Type));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500351 return -1;
352 }
353
Aaron Durbin923b4d52015-09-30 16:48:26 -0500354 if (read_le32(&fih->Signature) != FSP_SIG) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500355 printk(BIOS_ERR, "Unexpected FIH signature: %08x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500356 read_le32(&fih->Signature));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500357 return -1;
358 }
359
Aaron Durbin923b4d52015-09-30 16:48:26 -0500360 adjustment = (intptr_t)new_addr - read_le32(&fih->ImageBase);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500361
362 /* Update ImageBase to reflect FSP's new home. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500363 write_le32(&fih->ImageBase, adjustment + read_le32(&fih->ImageBase));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500364
365 /* Need to find patch table and adjust each entry. The tables
366 * following FSP_INFO_HEADER have a 32-bit signature and header
367 * length. The patch table is denoted as having a 'FSPP' signature;
368 * the table format doesn't follow the other tables. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500369 offset = fih_offset + read_le32(&fih->HeaderLength);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500370 while (offset + 2 * sizeof(uint32_t) <= size) {
371 uint32_t *table_headers;
372
373 table_headers = relative_offset(fsp, offset);
374
375 printk(FSP_DBG_LVL, "Checking offset %zx for 'FSPP'\n",
376 offset);
377
Aaron Durbin923b4d52015-09-30 16:48:26 -0500378 if (read_le32(&table_headers[0]) != FSPP_SIG) {
379 offset += read_le32(&table_headers[1]);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500380 continue;
381 }
382
383 if (relocate_patch_table(fsp, size, offset, adjustment)) {
384 printk(BIOS_ERR, "FSPP relocation failed.\n");
385 return -1;
386 }
387
388 return fih_offset;
389 }
390
391 printk(BIOS_ERR, "Could not find the FSP patch table.\n");
392 return -1;
393}
394
395static ssize_t relocate_fvh(uintptr_t new_addr, void *fsp, size_t fsp_size,
396 size_t fvh_offset, size_t *fih_offset)
397{
398 EFI_FIRMWARE_VOLUME_HEADER *fvh;
399 EFI_FFS_FILE_HEADER *ffsfh;
400 EFI_COMMON_SECTION_HEADER *csh;
401 size_t offset;
402 size_t file_offset;
403 size_t size;
404 size_t fv_length;
405
406 offset = fvh_offset;
407 fvh = relative_offset(fsp, offset);
408
Aaron Durbin923b4d52015-09-30 16:48:26 -0500409 if (read_le32(&fvh->Signature) != EFI_FVH_SIGNATURE)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500410 return -1;
411
Aaron Durbin923b4d52015-09-30 16:48:26 -0500412 fv_length = read_le64(&fvh->FvLength);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500413
414 printk(FSP_DBG_LVL, "FVH length: %zx Offset: %zx Mapping length: %zx\n",
415 fv_length, offset, fsp_size);
416
Aaron Durbin923b4d52015-09-30 16:48:26 -0500417 if (fv_length + offset > fsp_size)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500418 return -1;
419
420 /* Parse only this FV. However, the algorithm uses offsets into the
421 * entire FSP region so make size include the starting offset. */
422 size = fv_length + offset;
423
424 if (guid_compare(&fvh->FileSystemGuid, &ffs2_guid)) {
425 printk(BIOS_ERR, "FVH not an FFS2 type.\n");
426 return -1;
427 }
428
Aaron Durbin923b4d52015-09-30 16:48:26 -0500429 if (read_le16(&fvh->ExtHeaderOffset) != 0) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500430 EFI_FIRMWARE_VOLUME_EXT_HEADER *fveh;
431
Aaron Durbin923b4d52015-09-30 16:48:26 -0500432 offset += read_le16(&fvh->ExtHeaderOffset);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500433 fveh = relative_offset(fsp, offset);
434 printk(FSP_DBG_LVL, "Extended Header Offset: %zx Size: %zx\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500435 (size_t)read_le16(&fvh->ExtHeaderOffset),
436 (size_t)read_le32(&fveh->ExtHeaderSize));
437 offset += read_le32(&fveh->ExtHeaderSize);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500438 /* FFS files are 8 byte aligned after extended header. */
439 offset = ALIGN_UP(offset, 8);
440 } else {
Aaron Durbin923b4d52015-09-30 16:48:26 -0500441 offset += read_le16(&fvh->HeaderLength);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500442 }
443
444 file_offset = offset;
445 while (file_offset + sizeof(*ffsfh) < size) {
446 offset = file_offset;
447 printk(FSP_DBG_LVL, "file offset: %zx\n", file_offset);
448
449 /* First file and section should be FSP info header. */
450 if (fih_offset != NULL && *fih_offset == 0)
451 *fih_offset = file_offset;
452
453 ffsfh = relative_offset(fsp, file_offset);
454
Aaron Durbin923b4d52015-09-30 16:48:26 -0500455 printk(FSP_DBG_LVL, "file type = %x\n", read_le8(&ffsfh->Type));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500456 printk(FSP_DBG_LVL, "file attribs = %x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500457 read_le8(&ffsfh->Attributes));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500458
459 /* Exit FV relocation when empty space found */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500460 if (read_le8(&ffsfh->Type) == EFI_FV_FILETYPE_FFS_MAX)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500461 break;
462
463 /* Next file on 8 byte alignment. */
464 file_offset += ffs_file_size(ffsfh);
465 file_offset = ALIGN_UP(file_offset, 8);
466
467 /* Padding files have no section information. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500468 if (read_le8(&ffsfh->Type) == EFI_FV_FILETYPE_FFS_PAD)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500469 continue;
470
471 offset += file_section_offset(ffsfh);
472
473 while (offset + sizeof(*csh) < file_offset) {
474 size_t data_size;
475 size_t data_offset;
476
477 csh = relative_offset(fsp, offset);
478
479 printk(FSP_DBG_LVL, "section offset: %zx\n", offset);
480 printk(FSP_DBG_LVL, "section type: %x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500481 read_le8(&csh->Type));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500482
483 data_size = section_data_size(csh);
484 data_offset = section_data_offset(csh);
485
486 if (data_size + data_offset + offset > file_offset) {
487 printk(BIOS_ERR, "Section exceeds FV size.\n");
488 return -1;
489 }
490
491 /*
Furquan Shaikhb0c2fe02016-05-09 12:23:01 -0700492 * The entire FSP image can be thought of as one
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500493 * program with a single link address even though there
494 * are multiple TEs linked separately. The reason is
495 * that each TE is linked for XIP. So in order to
496 * relocate the TE properly we need to form the
497 * relocated address based on the TE offset within
498 * FSP proper.
499 */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500500 if (read_le8(&csh->Type) == EFI_SECTION_TE) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500501 void *te;
502 size_t te_offset = offset + data_offset;
503 uintptr_t te_addr = new_addr + te_offset;
504
505 printk(FSP_DBG_LVL, "TE image at offset %zx\n",
506 te_offset);
507 te = relative_offset(fsp, te_offset);
Aaron Durbin923b4d52015-09-30 16:48:26 -0500508 te_relocate(te_addr, te);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500509 }
510
511 offset += data_size + data_offset;
512 /* Sections are aligned to 4 bytes. */
513 offset = ALIGN_UP(offset, 4);
514 }
515 }
516
517 /* Return amount of buffer parsed: FV size. */
518 return fv_length;
519}
520
Furquan Shaikhb0c2fe02016-05-09 12:23:01 -0700521ssize_t fsp_component_relocate(uintptr_t new_addr, void *fsp, size_t size)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500522{
523 size_t offset;
524 size_t fih_offset;
525
526 offset = 0;
527 fih_offset = 0;
528 while (offset < size) {
529 ssize_t nparsed;
530
531 /* Relocate each FV within the FSP region. The FSP_INFO_HEADER
532 * should only be located in the first FV. */
533 if (offset == 0)
534 nparsed = relocate_fvh(new_addr, fsp, size, offset,
535 &fih_offset);
536 else
537 nparsed = relocate_fvh(new_addr, fsp, size, offset,
538 NULL);
539
540 /* FV should be larger than 0 or failed to parse. */
541 if (nparsed <= 0) {
542 printk(BIOS_ERR, "FV @ offset %zx relocation failed\n",
543 offset);
544 return -1;
545 }
546
547 offset += nparsed;
548 }
549
550 return relocate_remaining_items(fsp, size, new_addr, fih_offset);
551}
Furquan Shaikhb0c2fe02016-05-09 12:23:01 -0700552
553ssize_t fsp1_1_relocate(uintptr_t new_addr, void *fsp, size_t size)
554{
555 return fsp_component_relocate(new_addr, fsp, size);
556}