blob: 994da094091acfd76ba8ffa4dda8863e65b49bfd [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.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc.
18 */
19
20#include <console/console.h>
Aaron Durbin923b4d52015-09-30 16:48:26 -050021#include <commonlib/endian.h>
22#include <commonlib/fsp1_1.h>
23#include <commonlib/helpers.h>
Aaron Durbina5be7fa2015-09-10 22:52:27 -050024#include <stdlib.h>
25#include <stdint.h>
26#include <string.h>
27
28#define FSP_DBG_LVL BIOS_NEVER
29
30/*
31 * UEFI defines everything as little endian. However, this piece of code
32 * can be integrated in a userland tool. That tool could be on a big endian
33 * machine so one needs to access the fields within UEFI structures using
34 * endian-aware accesses.
35 */
36
37/* Return 0 if equal. Non-zero if not equal. */
38static int guid_compare(const EFI_GUID *le_guid, const EFI_GUID *native_guid)
39{
Aaron Durbin923b4d52015-09-30 16:48:26 -050040 if (read_le32(&le_guid->Data1) != native_guid->Data1)
Aaron Durbina5be7fa2015-09-10 22:52:27 -050041 return 1;
Aaron Durbin923b4d52015-09-30 16:48:26 -050042 if (read_le16(&le_guid->Data2) != native_guid->Data2)
Aaron Durbina5be7fa2015-09-10 22:52:27 -050043 return 1;
Aaron Durbin923b4d52015-09-30 16:48:26 -050044 if (read_le16(&le_guid->Data3) != native_guid->Data3)
Aaron Durbina5be7fa2015-09-10 22:52:27 -050045 return 1;
46 return memcmp(le_guid->Data4, native_guid->Data4,
47 ARRAY_SIZE(le_guid->Data4));
48}
49
50/* Provide this for symmetry when accessing UEFI fields. */
51static inline uint8_t le8toh(uint8_t byte)
52{
53 return byte;
54}
55
56static const EFI_GUID ffs2_guid = EFI_FIRMWARE_FILE_SYSTEM2_GUID;
57static const EFI_GUID fih_guid = FSP_INFO_HEADER_GUID;
58
59struct fsp_patch_table {
60 uint32_t signature;
61 uint16_t header_length;
62 uint8_t header_revision;
63 uint8_t reserved;
64 uint32_t patch_entry_num;
65 uint32_t patch_entries[0];
66} __attribute__((packed));
67
68#define FSPP_SIG 0x50505346
69
70static void *relative_offset(void *base, ssize_t offset)
71{
72 uintptr_t loc;
73
74 loc = (uintptr_t)base;
75 loc += offset;
76
77 return (void *)loc;
78}
79
80static uint32_t *fspp_reloc(void *fsp, size_t fsp_size, uint32_t e)
81{
82 size_t offset;
83
84 /* Offsets live in bits 23:0. */
85 offset = e & 0xffffff;
86
87 /* If bit 31 is set then the offset is considered a negative value
88 * relative to the end of the image using 16MiB as the offset's
89 * reference. */
90 if (e & (1 << 31))
91 offset = fsp_size - (16 * MiB - offset);
92
93 /* Determine if offset falls within fsp_size for a 32 bit relocation. */
94 if (offset > fsp_size - sizeof(uint32_t))
95 return NULL;
96
97 return relative_offset(fsp, offset);
98}
99
100static int reloc_type(uint16_t reloc_entry)
101{
102 /* Reloc type in upper 4 bits */
103 return reloc_entry >> 12;
104}
105
106static size_t reloc_offset(uint16_t reloc_entry)
107{
108 /* Offsets are in low 12 bits. */
109 return reloc_entry & ((1 << 12) - 1);
110}
111
Aaron Durbin923b4d52015-09-30 16:48:26 -0500112static int te_relocate(uintptr_t new_addr, void *te)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500113{
114 EFI_TE_IMAGE_HEADER *teih;
115 EFI_IMAGE_DATA_DIRECTORY *relocd;
116 EFI_IMAGE_BASE_RELOCATION *relocb;
117 uintptr_t image_base;
118 size_t fixup_offset;
119 size_t num_relocs;
120 uint16_t *reloc;
121 size_t relocd_offset;
122 uint8_t *te_base;
123 uint32_t adj;
124
125 teih = te;
126
Aaron Durbin923b4d52015-09-30 16:48:26 -0500127 if (read_le16(&teih->Signature) != EFI_TE_IMAGE_HEADER_SIGNATURE) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500128 printk(BIOS_ERR, "TE Signature mismatch: %x vs %x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500129 read_le16(&teih->Signature),
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500130 EFI_TE_IMAGE_HEADER_SIGNATURE);
131 return -1;
132 }
133
134 /*
135 * A TE image is created by converting a PE file. Because of this
136 * the offsets within the headers are off. In order to calculate
137 * the correct releative offets one needs to subtract fixup_offset
138 * from the encoded offets. Similarly, the linked address of the
139 * program is found by adding the fixup_offset to the ImageBase.
140 */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500141 fixup_offset = read_le16(&teih->StrippedSize);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500142 fixup_offset -= sizeof(EFI_TE_IMAGE_HEADER);
143 /* Keep track of a base that is correctly adjusted so that offsets
144 * can be used directly. */
145 te_base = te;
146 te_base -= fixup_offset;
147
Aaron Durbin923b4d52015-09-30 16:48:26 -0500148 image_base = read_le64(&teih->ImageBase);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500149 adj = new_addr - (image_base + fixup_offset);
150
151 printk(FSP_DBG_LVL, "TE Image %p -> %p adjust value: %x\n",
152 (void *)image_base, (void *)new_addr, adj);
153
154 /* Adjust ImageBase for consistency. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500155 write_le64(&teih->ImageBase, (uint32_t)(image_base + adj));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500156
157 relocd = &teih->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_BASERELOC];
158
159 relocd_offset = 0;
160 /* Though the field name is VirtualAddress it's actually relative to
161 * the beginning of the image which is linked at ImageBase. */
162 relocb = relative_offset(te,
Aaron Durbin923b4d52015-09-30 16:48:26 -0500163 read_le32(&relocd->VirtualAddress) - fixup_offset);
164 while (relocd_offset < read_le32(&relocd->Size)) {
165 size_t rva_offset = read_le32(&relocb->VirtualAddress);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500166
167 printk(FSP_DBG_LVL, "Relocs for RVA offset %zx\n", rva_offset);
Aaron Durbin923b4d52015-09-30 16:48:26 -0500168 num_relocs = read_le32(&relocb->SizeOfBlock) - sizeof(*relocb);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500169 num_relocs /= sizeof(uint16_t);
170 reloc = relative_offset(relocb, sizeof(*relocb));
171
172 printk(FSP_DBG_LVL, "Num relocs in block: %zx\n", num_relocs);
173
174 while (num_relocs > 0) {
Aaron Durbin923b4d52015-09-30 16:48:26 -0500175 uint16_t reloc_val = read_le16(reloc);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500176 int type = reloc_type(reloc_val);
177 size_t offset = reloc_offset(reloc_val);
178
179 printk(FSP_DBG_LVL, "reloc type %x offset %zx\n",
180 type, offset);
181
182 if (type == EFI_IMAGE_REL_BASED_HIGHLOW) {
183 uint32_t *reloc_addr;
184 uint32_t val;
185
186 offset += rva_offset;
187 reloc_addr = (void *)&te_base[offset];
Aaron Durbin923b4d52015-09-30 16:48:26 -0500188 val = read_le32(reloc_addr);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500189
190 printk(FSP_DBG_LVL, "Adjusting %p %x -> %x\n",
191 reloc_addr, val, val + adj);
Aaron Durbin923b4d52015-09-30 16:48:26 -0500192 write_le32(reloc_addr, val + adj);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500193 } else if (type != EFI_IMAGE_REL_BASED_ABSOLUTE) {
194 printk(BIOS_ERR, "Unknown reloc type: %x\n",
195 type);
196 return -1;
197 }
198 num_relocs--;
199 reloc++;
200 }
201
202 /* Track consumption of relocation directory contents. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500203 relocd_offset += read_le32(&relocb->SizeOfBlock);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500204 /* Get next relocation block to process. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500205 relocb = relative_offset(relocb,
206 read_le32(&relocb->SizeOfBlock));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500207 }
208
209 return 0;
210}
211
212static size_t csh_size(const EFI_COMMON_SECTION_HEADER *csh)
213{
214 size_t size;
215
216 /* Unpack the array into a type that can be used. */
217 size = 0;
Aaron Durbin923b4d52015-09-30 16:48:26 -0500218 size |= read_le8(&csh->Size[0]) << 0;
219 size |= read_le8(&csh->Size[1]) << 8;
220 size |= read_le8(&csh->Size[2]) << 16;
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500221
222 return size;
223}
224
225static size_t section_data_offset(const EFI_COMMON_SECTION_HEADER *csh)
226{
227 if (csh_size(csh) == 0x00ffffff)
228 return sizeof(EFI_COMMON_SECTION_HEADER2);
229 else
230 return sizeof(EFI_COMMON_SECTION_HEADER);
231}
232
233static size_t section_data_size(const EFI_COMMON_SECTION_HEADER *csh)
234{
235 size_t section_size;
236
237 if (csh_size(csh) == 0x00ffffff)
Aaron Durbin923b4d52015-09-30 16:48:26 -0500238 section_size = read_le32(&SECTION2_SIZE(csh));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500239 else
240 section_size = csh_size(csh);
241
242 return section_size - section_data_offset(csh);
243}
244
245static size_t file_section_offset(const EFI_FFS_FILE_HEADER *ffsfh)
246{
247 if (IS_FFS_FILE2(ffsfh))
248 return sizeof(EFI_FFS_FILE_HEADER2);
249 else
250 return sizeof(EFI_FFS_FILE_HEADER);
251}
252
253static size_t ffs_file_size(const EFI_FFS_FILE_HEADER *ffsfh)
254{
255 size_t size;
256
257 if (IS_FFS_FILE2(ffsfh))
Aaron Durbin923b4d52015-09-30 16:48:26 -0500258 size = read_le32(&FFS_FILE2_SIZE(ffsfh));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500259 else {
Aaron Durbin923b4d52015-09-30 16:48:26 -0500260 size = read_le8(&ffsfh->Size[0]) << 0;
261 size |= read_le8(&ffsfh->Size[1]) << 8;
262 size |= read_le8(&ffsfh->Size[2]) << 16;
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500263 }
264 return size;
265}
266
267static int relocate_patch_table(void *fsp, size_t size, size_t offset,
268 ssize_t adjustment)
269{
270 struct fsp_patch_table *table;
271 size_t num;
272 size_t num_entries;
273
274 table = relative_offset(fsp, offset);
275
276 if ((offset + sizeof(*table) > size) ||
Aaron Durbin923b4d52015-09-30 16:48:26 -0500277 (read_le16(&table->header_length) + offset) > size) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500278 printk(BIOS_ERR, "FSPP not entirely contained in region.\n");
279 return -1;
280 }
281
Aaron Durbin923b4d52015-09-30 16:48:26 -0500282 num_entries = read_le32(&table->patch_entry_num);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500283 printk(FSP_DBG_LVL, "FSPP relocs: %zx\n", num_entries);
284
Aaron Durbin923b4d52015-09-30 16:48:26 -0500285 for (num = 0; num < num_entries; num++) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500286 uint32_t *reloc;
287 uint32_t reloc_val;
288
289 reloc = fspp_reloc(fsp, size,
Aaron Durbin923b4d52015-09-30 16:48:26 -0500290 read_le32(&table->patch_entries[num]));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500291
292 if (reloc == NULL) {
293 printk(BIOS_ERR, "Ignoring FSPP entry: %x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500294 read_le32(&table->patch_entries[num]));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500295 continue;
296 }
297
Aaron Durbin923b4d52015-09-30 16:48:26 -0500298 reloc_val = read_le32(reloc);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500299 printk(FSP_DBG_LVL, "Adjusting %p %x -> %x\n",
300 reloc, reloc_val,
301 (unsigned int)(reloc_val + adjustment));
302
Aaron Durbin923b4d52015-09-30 16:48:26 -0500303 write_le32(reloc, reloc_val + adjustment);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500304 }
305
306 return 0;
307}
308
309static ssize_t relocate_remaining_items(void *fsp, size_t size,
310 uintptr_t new_addr, size_t fih_offset)
311{
312 EFI_FFS_FILE_HEADER *ffsfh;
313 EFI_COMMON_SECTION_HEADER *csh;
314 FSP_INFO_HEADER *fih;
315 ssize_t adjustment;
316 size_t offset;
317
318 printk(FSP_DBG_LVL, "FSP_INFO_HEADER offset is %zx\n", fih_offset);
319
320 if (fih_offset == 0) {
321 printk(BIOS_ERR, "FSP_INFO_HEADER offset is 0.\n");
322 return -1;
323 }
324
325 /* FSP_INFO_HEADER at first file in FV within first RAW section. */
326 ffsfh = relative_offset(fsp, fih_offset);
327 fih_offset += file_section_offset(ffsfh);
328 csh = relative_offset(fsp, fih_offset);
329 fih_offset += section_data_offset(csh);
330 fih = relative_offset(fsp, fih_offset);
331
332 if (guid_compare(&ffsfh->Name, &fih_guid)) {
333 printk(BIOS_ERR, "Bad FIH GUID.\n");
334 return -1;
335 }
336
Aaron Durbin923b4d52015-09-30 16:48:26 -0500337 if (read_le8(&csh->Type) != EFI_SECTION_RAW) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500338 printk(BIOS_ERR, "FIH file should have raw section: %x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500339 read_le8(&csh->Type));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500340 return -1;
341 }
342
Aaron Durbin923b4d52015-09-30 16:48:26 -0500343 if (read_le32(&fih->Signature) != FSP_SIG) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500344 printk(BIOS_ERR, "Unexpected FIH signature: %08x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500345 read_le32(&fih->Signature));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500346 return -1;
347 }
348
Aaron Durbin923b4d52015-09-30 16:48:26 -0500349 adjustment = (intptr_t)new_addr - read_le32(&fih->ImageBase);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500350
351 /* Update ImageBase to reflect FSP's new home. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500352 write_le32(&fih->ImageBase, adjustment + read_le32(&fih->ImageBase));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500353
354 /* Need to find patch table and adjust each entry. The tables
355 * following FSP_INFO_HEADER have a 32-bit signature and header
356 * length. The patch table is denoted as having a 'FSPP' signature;
357 * the table format doesn't follow the other tables. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500358 offset = fih_offset + read_le32(&fih->HeaderLength);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500359 while (offset + 2 * sizeof(uint32_t) <= size) {
360 uint32_t *table_headers;
361
362 table_headers = relative_offset(fsp, offset);
363
364 printk(FSP_DBG_LVL, "Checking offset %zx for 'FSPP'\n",
365 offset);
366
Aaron Durbin923b4d52015-09-30 16:48:26 -0500367 if (read_le32(&table_headers[0]) != FSPP_SIG) {
368 offset += read_le32(&table_headers[1]);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500369 continue;
370 }
371
372 if (relocate_patch_table(fsp, size, offset, adjustment)) {
373 printk(BIOS_ERR, "FSPP relocation failed.\n");
374 return -1;
375 }
376
377 return fih_offset;
378 }
379
380 printk(BIOS_ERR, "Could not find the FSP patch table.\n");
381 return -1;
382}
383
384static ssize_t relocate_fvh(uintptr_t new_addr, void *fsp, size_t fsp_size,
385 size_t fvh_offset, size_t *fih_offset)
386{
387 EFI_FIRMWARE_VOLUME_HEADER *fvh;
388 EFI_FFS_FILE_HEADER *ffsfh;
389 EFI_COMMON_SECTION_HEADER *csh;
390 size_t offset;
391 size_t file_offset;
392 size_t size;
393 size_t fv_length;
394
395 offset = fvh_offset;
396 fvh = relative_offset(fsp, offset);
397
Aaron Durbin923b4d52015-09-30 16:48:26 -0500398 if (read_le32(&fvh->Signature) != EFI_FVH_SIGNATURE)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500399 return -1;
400
Aaron Durbin923b4d52015-09-30 16:48:26 -0500401 fv_length = read_le64(&fvh->FvLength);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500402
403 printk(FSP_DBG_LVL, "FVH length: %zx Offset: %zx Mapping length: %zx\n",
404 fv_length, offset, fsp_size);
405
Aaron Durbin923b4d52015-09-30 16:48:26 -0500406 if (fv_length + offset > fsp_size)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500407 return -1;
408
409 /* Parse only this FV. However, the algorithm uses offsets into the
410 * entire FSP region so make size include the starting offset. */
411 size = fv_length + offset;
412
413 if (guid_compare(&fvh->FileSystemGuid, &ffs2_guid)) {
414 printk(BIOS_ERR, "FVH not an FFS2 type.\n");
415 return -1;
416 }
417
Aaron Durbin923b4d52015-09-30 16:48:26 -0500418 if (read_le16(&fvh->ExtHeaderOffset) != 0) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500419 EFI_FIRMWARE_VOLUME_EXT_HEADER *fveh;
420
Aaron Durbin923b4d52015-09-30 16:48:26 -0500421 offset += read_le16(&fvh->ExtHeaderOffset);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500422 fveh = relative_offset(fsp, offset);
423 printk(FSP_DBG_LVL, "Extended Header Offset: %zx Size: %zx\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500424 (size_t)read_le16(&fvh->ExtHeaderOffset),
425 (size_t)read_le32(&fveh->ExtHeaderSize));
426 offset += read_le32(&fveh->ExtHeaderSize);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500427 /* FFS files are 8 byte aligned after extended header. */
428 offset = ALIGN_UP(offset, 8);
429 } else {
Aaron Durbin923b4d52015-09-30 16:48:26 -0500430 offset += read_le16(&fvh->HeaderLength);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500431 }
432
433 file_offset = offset;
434 while (file_offset + sizeof(*ffsfh) < size) {
435 offset = file_offset;
436 printk(FSP_DBG_LVL, "file offset: %zx\n", file_offset);
437
438 /* First file and section should be FSP info header. */
439 if (fih_offset != NULL && *fih_offset == 0)
440 *fih_offset = file_offset;
441
442 ffsfh = relative_offset(fsp, file_offset);
443
Aaron Durbin923b4d52015-09-30 16:48:26 -0500444 printk(FSP_DBG_LVL, "file type = %x\n", read_le8(&ffsfh->Type));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500445 printk(FSP_DBG_LVL, "file attribs = %x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500446 read_le8(&ffsfh->Attributes));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500447
448 /* Exit FV relocation when empty space found */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500449 if (read_le8(&ffsfh->Type) == EFI_FV_FILETYPE_FFS_MAX)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500450 break;
451
452 /* Next file on 8 byte alignment. */
453 file_offset += ffs_file_size(ffsfh);
454 file_offset = ALIGN_UP(file_offset, 8);
455
456 /* Padding files have no section information. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500457 if (read_le8(&ffsfh->Type) == EFI_FV_FILETYPE_FFS_PAD)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500458 continue;
459
460 offset += file_section_offset(ffsfh);
461
462 while (offset + sizeof(*csh) < file_offset) {
463 size_t data_size;
464 size_t data_offset;
465
466 csh = relative_offset(fsp, offset);
467
468 printk(FSP_DBG_LVL, "section offset: %zx\n", offset);
469 printk(FSP_DBG_LVL, "section type: %x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500470 read_le8(&csh->Type));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500471
472 data_size = section_data_size(csh);
473 data_offset = section_data_offset(csh);
474
475 if (data_size + data_offset + offset > file_offset) {
476 printk(BIOS_ERR, "Section exceeds FV size.\n");
477 return -1;
478 }
479
480 /*
481 * The entire FSP 1.1 image can be thought of as one
482 * program with a single link address even though there
483 * are multiple TEs linked separately. The reason is
484 * that each TE is linked for XIP. So in order to
485 * relocate the TE properly we need to form the
486 * relocated address based on the TE offset within
487 * FSP proper.
488 */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500489 if (read_le8(&csh->Type) == EFI_SECTION_TE) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500490 void *te;
491 size_t te_offset = offset + data_offset;
492 uintptr_t te_addr = new_addr + te_offset;
493
494 printk(FSP_DBG_LVL, "TE image at offset %zx\n",
495 te_offset);
496 te = relative_offset(fsp, te_offset);
Aaron Durbin923b4d52015-09-30 16:48:26 -0500497 te_relocate(te_addr, te);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500498 }
499
500 offset += data_size + data_offset;
501 /* Sections are aligned to 4 bytes. */
502 offset = ALIGN_UP(offset, 4);
503 }
504 }
505
506 /* Return amount of buffer parsed: FV size. */
507 return fv_length;
508}
509
510ssize_t fsp1_1_relocate(uintptr_t new_addr, void *fsp, size_t size)
511{
512 size_t offset;
513 size_t fih_offset;
514
515 offset = 0;
516 fih_offset = 0;
517 while (offset < size) {
518 ssize_t nparsed;
519
520 /* Relocate each FV within the FSP region. The FSP_INFO_HEADER
521 * should only be located in the first FV. */
522 if (offset == 0)
523 nparsed = relocate_fvh(new_addr, fsp, size, offset,
524 &fih_offset);
525 else
526 nparsed = relocate_fvh(new_addr, fsp, size, offset,
527 NULL);
528
529 /* FV should be larger than 0 or failed to parse. */
530 if (nparsed <= 0) {
531 printk(BIOS_ERR, "FV @ offset %zx relocation failed\n",
532 offset);
533 return -1;
534 }
535
536 offset += nparsed;
537 }
538
539 return relocate_remaining_items(fsp, size, new_addr, fih_offset);
540}