blob: 8dabad5643788f21eb1e3bb5c619e36437613989 [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>
Elyes HAOUAS56ab5752021-12-31 18:45:46 +010021#include <stddef.h>
Aaron Durbina5be7fa2015-09-10 22:52:27 -050022#include <stdint.h>
23#include <string.h>
24
25#define FSP_DBG_LVL BIOS_NEVER
Eddie Vas1df1cf92022-08-16 20:12:04 -070026#define MASK_24BITS 0x00FFFFFF
Aaron Durbina5be7fa2015-09-10 22:52:27 -050027
28/*
29 * UEFI defines everything as little endian. However, this piece of code
30 * can be integrated in a userland tool. That tool could be on a big endian
31 * machine so one needs to access the fields within UEFI structures using
32 * endian-aware accesses.
33 */
34
35/* Return 0 if equal. Non-zero if not equal. */
36static int guid_compare(const EFI_GUID *le_guid, const EFI_GUID *native_guid)
37{
Aaron Durbin923b4d52015-09-30 16:48:26 -050038 if (read_le32(&le_guid->Data1) != native_guid->Data1)
Aaron Durbina5be7fa2015-09-10 22:52:27 -050039 return 1;
Aaron Durbin923b4d52015-09-30 16:48:26 -050040 if (read_le16(&le_guid->Data2) != native_guid->Data2)
Aaron Durbina5be7fa2015-09-10 22:52:27 -050041 return 1;
Aaron Durbin923b4d52015-09-30 16:48:26 -050042 if (read_le16(&le_guid->Data3) != native_guid->Data3)
Aaron Durbina5be7fa2015-09-10 22:52:27 -050043 return 1;
44 return memcmp(le_guid->Data4, native_guid->Data4,
45 ARRAY_SIZE(le_guid->Data4));
46}
47
Aaron Durbina5be7fa2015-09-10 22:52:27 -050048static const EFI_GUID ffs2_guid = EFI_FIRMWARE_FILE_SYSTEM2_GUID;
49static const EFI_GUID fih_guid = FSP_INFO_HEADER_GUID;
50
51struct fsp_patch_table {
52 uint32_t signature;
53 uint16_t header_length;
54 uint8_t header_revision;
55 uint8_t reserved;
56 uint32_t patch_entry_num;
57 uint32_t patch_entries[0];
Stefan Reinauer6a001132017-07-13 02:20:27 +020058} __packed;
Aaron Durbina5be7fa2015-09-10 22:52:27 -050059
60#define FSPP_SIG 0x50505346
61
62static void *relative_offset(void *base, ssize_t offset)
63{
64 uintptr_t loc;
65
66 loc = (uintptr_t)base;
67 loc += offset;
68
69 return (void *)loc;
70}
71
Eddie Vas1df1cf92022-08-16 20:12:04 -070072static size_t csh_size(const EFI_COMMON_SECTION_HEADER *csh)
73{
74 size_t size;
75
76 /* Unpack the array into a type that can be used. */
77 size = 0;
78 size |= read_le8(&csh->Size[0]) << 0;
79 size |= read_le8(&csh->Size[1]) << 8;
80 size |= read_le8(&csh->Size[2]) << 16;
81
82 return size;
83}
84
85static size_t file_section_offset(const EFI_FFS_FILE_HEADER *ffsfh)
86{
87 if (IS_FFS_FILE2(ffsfh))
88 return sizeof(EFI_FFS_FILE_HEADER2);
89 else
90 return sizeof(EFI_FFS_FILE_HEADER);
91}
92
93static size_t section_data_offset(const EFI_COMMON_SECTION_HEADER *csh)
94{
95 if (csh_size(csh) == MASK_24BITS)
96 return sizeof(EFI_COMMON_SECTION_HEADER2);
97 else
98 return sizeof(EFI_COMMON_SECTION_HEADER);
99}
100
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500101static uint32_t *fspp_reloc(void *fsp, size_t fsp_size, uint32_t e)
102{
103 size_t offset;
104
105 /* Offsets live in bits 23:0. */
Eddie Vas1df1cf92022-08-16 20:12:04 -0700106 offset = e & MASK_24BITS;
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500107
108 /* If bit 31 is set then the offset is considered a negative value
109 * relative to the end of the image using 16MiB as the offset's
110 * reference. */
111 if (e & (1 << 31))
112 offset = fsp_size - (16 * MiB - offset);
113
114 /* Determine if offset falls within fsp_size for a 32 bit relocation. */
115 if (offset > fsp_size - sizeof(uint32_t))
116 return NULL;
117
118 return relative_offset(fsp, offset);
119}
120
121static int reloc_type(uint16_t reloc_entry)
122{
123 /* Reloc type in upper 4 bits */
124 return reloc_entry >> 12;
125}
126
127static size_t reloc_offset(uint16_t reloc_entry)
128{
129 /* Offsets are in low 12 bits. */
130 return reloc_entry & ((1 << 12) - 1);
131}
132
Eddie Vas1df1cf92022-08-16 20:12:04 -0700133static FSP_INFO_HEADER *fsp_get_info_hdr(void *fsp, size_t fih_offset)
134{
135 EFI_FFS_FILE_HEADER *ffsfh;
136 EFI_COMMON_SECTION_HEADER *csh;
137 FSP_INFO_HEADER *fih;
138
139 printk(FSP_DBG_LVL, "FSP_INFO_HEADER offset is %zx\n", fih_offset);
140
141 if (fih_offset == 0) {
142 printk(BIOS_ERR, "FSP_INFO_HEADER offset is 0.\n");
143 return NULL;
144 }
145
146 /* FSP_INFO_HEADER is located at first file in FV within first RAW section. */
147 ffsfh = relative_offset(fsp, fih_offset);
148 fih_offset += file_section_offset(ffsfh);
149 csh = relative_offset(fsp, fih_offset);
150 fih_offset += section_data_offset(csh);
151 fih = relative_offset(fsp, fih_offset);
152
153 if (guid_compare(&ffsfh->Name, &fih_guid)) {
154 printk(BIOS_ERR, "Bad FIH GUID.\n");
155 return NULL;
156 }
157
158 if (read_le8(&csh->Type) != EFI_SECTION_RAW) {
159 printk(BIOS_ERR, "FIH file should have raw section: %x\n",
160 read_le8(&csh->Type));
161 return NULL;
162 }
163
164 if (read_le32(&fih->Signature) != FSP_SIG) {
165 printk(BIOS_ERR, "Unexpected FIH signature: %08x\n",
166 read_le32(&fih->Signature));
167 return NULL;
168 }
169
170 return fih;
171}
172
173static int pe_relocate(uintptr_t new_addr, void *pe, void *fsp, size_t fih_off)
174{
175 EFI_IMAGE_NT_HEADERS32 *peih;
176 EFI_IMAGE_DOS_HEADER *doshdr;
177 EFI_IMAGE_OPTIONAL_HEADER32 *ophdr;
178 FSP_INFO_HEADER *fih;
179 uint32_t roffset, rsize;
180 uint32_t offset;
181 uint8_t *pe_base = pe;
182 uint32_t image_base;
183 uint32_t img_base_off;
184 uint32_t delta;
185
186 doshdr = pe;
187 if (read_le16(&doshdr->e_magic) != EFI_IMAGE_DOS_SIGNATURE) {
188 printk(BIOS_ERR, "Invalid DOS Header/magic\n");
189 return -1;
190 }
191
192 peih = relative_offset(pe, doshdr->e_lfanew);
193
194 if (read_le32(&peih->Signature) != EFI_IMAGE_NT_SIGNATURE) {
195 printk(BIOS_ERR, "Invalid PE32 header\n");
196 return -1;
197 }
198
199 ophdr = &peih->OptionalHeader;
200
201 if (read_le16(&ophdr->Magic) != EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
202 printk(BIOS_ERR, "No support for non-PE32 images\n");
203 return -1;
204 }
205
206 fih = fsp_get_info_hdr(fsp, fih_off);
207 if (fih == NULL) {
208 printk(BIOS_ERR, "No Image base found for FSP PE32\n");
209 return -1;
210 }
211 image_base = read_le32(&fih->ImageBase);
212 printk(FSP_DBG_LVL, "FSP InfoHdr Image Base is %x\n", image_base);
213
214 delta = new_addr - image_base;
215
216 img_base_off = read_le32(&ophdr->ImageBase);
217 printk(FSP_DBG_LVL, "lfanew 0x%x, delta-0x%x, FSP Base 0x%x, NT32ImageBase 0x%x, offset 0x%x\n",
218 read_le32(&doshdr->e_lfanew),
219 delta, image_base, img_base_off,
220 (uint32_t)((uint8_t *)&ophdr->ImageBase - pe_base));
221
222 printk(FSP_DBG_LVL, "relocating PE32 image at addr - 0x%lx\n", new_addr);
223 rsize = read_le32(&ophdr->DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC].Size);
224 roffset = read_le32(&ophdr->DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress);
225 printk(FSP_DBG_LVL, "relocation table at offset-%x,size=%x\n", roffset, rsize);
226 // TODO - add support for PE32+ also
227
228 offset = roffset;
229 while (offset < (roffset + rsize)) {
230 uint32_t vaddr;
231 uint32_t rlen, rnum;
232 uint16_t *rdata;
233 uint32_t i;
234 EFI_IMAGE_DATA_DIRECTORY *relocd;
235
236 relocd = (void *) &pe_base[offset];
237 offset += sizeof(*relocd);
238 // Read relocation type, offset pairs
239 rlen = read_le32(&relocd->Size) - sizeof(*relocd);
240 rnum = rlen / sizeof(uint16_t);
241 vaddr = read_le32(&relocd->VirtualAddress);
242 rdata = (uint16_t *) &pe_base[offset];
243 printk(FSP_DBG_LVL, "\t%d Relocs for RVA %x\n", rnum, vaddr);
244
245 for (i = 0; i < rnum; i++) {
246 uint16_t roff = reloc_offset(rdata[i]);
247 uint16_t rtype = reloc_type(rdata[i]);
248 uint32_t aoff = vaddr + roff;
249 uint32_t val;
250 printk(FSP_DBG_LVL, "\t\treloc type %x offset %x aoff %x, base-0x%x\n",
251 rtype, roff, aoff, img_base_off);
252 switch (rtype) {
253 case EFI_IMAGE_REL_BASED_ABSOLUTE:
254 continue;
255 case EFI_IMAGE_REL_BASED_HIGHLOW:
256 val = read_le32(&pe_base[aoff]);
257 printk(FSP_DBG_LVL, "Adjusting %p %x -> %x\n",
258 &pe_base[aoff], val, val + delta);
259 write_le32(&pe_base[aoff], val + delta);
260 break;
261 case EFI_IMAGE_REL_BASED_DIR64:
262 printk(BIOS_ERR, "Error: Unsupported DIR64\n");
263 break;
264 default:
265 printk(BIOS_ERR, "Error: Unsupported relocation type %d\n",
266 rtype);
267 return -1;
268 }
269 }
270 offset += sizeof(*rdata) * rnum;
271 }
272 printk(FSP_DBG_LVL, "Adjust Image Base %x->%x\n",
273 img_base_off, img_base_off + delta);
274 img_base_off += delta;
275 write_le32(&ophdr->ImageBase, img_base_off);
276
277 return -1;
278}
279
Aaron Durbin923b4d52015-09-30 16:48:26 -0500280static int te_relocate(uintptr_t new_addr, void *te)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500281{
282 EFI_TE_IMAGE_HEADER *teih;
283 EFI_IMAGE_DATA_DIRECTORY *relocd;
284 EFI_IMAGE_BASE_RELOCATION *relocb;
285 uintptr_t image_base;
286 size_t fixup_offset;
287 size_t num_relocs;
288 uint16_t *reloc;
289 size_t relocd_offset;
290 uint8_t *te_base;
291 uint32_t adj;
292
293 teih = te;
294
Aaron Durbin923b4d52015-09-30 16:48:26 -0500295 if (read_le16(&teih->Signature) != EFI_TE_IMAGE_HEADER_SIGNATURE) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500296 printk(BIOS_ERR, "TE Signature mismatch: %x vs %x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500297 read_le16(&teih->Signature),
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500298 EFI_TE_IMAGE_HEADER_SIGNATURE);
299 return -1;
300 }
301
302 /*
303 * A TE image is created by converting a PE file. Because of this
304 * the offsets within the headers are off. In order to calculate
Elyes HAOUAS23c1c4e2019-12-18 13:21:37 +0100305 * the correct relative offsets one needs to subtract fixup_offset
306 * from the encoded offsets. Similarly, the linked address of the
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500307 * program is found by adding the fixup_offset to the ImageBase.
308 */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500309 fixup_offset = read_le16(&teih->StrippedSize);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500310 fixup_offset -= sizeof(EFI_TE_IMAGE_HEADER);
311 /* Keep track of a base that is correctly adjusted so that offsets
312 * can be used directly. */
313 te_base = te;
314 te_base -= fixup_offset;
315
Aaron Durbin923b4d52015-09-30 16:48:26 -0500316 image_base = read_le64(&teih->ImageBase);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500317 adj = new_addr - (image_base + fixup_offset);
318
319 printk(FSP_DBG_LVL, "TE Image %p -> %p adjust value: %x\n",
320 (void *)image_base, (void *)new_addr, adj);
321
322 /* Adjust ImageBase for consistency. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500323 write_le64(&teih->ImageBase, (uint32_t)(image_base + adj));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500324
325 relocd = &teih->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_BASERELOC];
326
327 relocd_offset = 0;
328 /* Though the field name is VirtualAddress it's actually relative to
329 * the beginning of the image which is linked at ImageBase. */
330 relocb = relative_offset(te,
Aaron Durbin923b4d52015-09-30 16:48:26 -0500331 read_le32(&relocd->VirtualAddress) - fixup_offset);
332 while (relocd_offset < read_le32(&relocd->Size)) {
333 size_t rva_offset = read_le32(&relocb->VirtualAddress);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500334
335 printk(FSP_DBG_LVL, "Relocs for RVA offset %zx\n", rva_offset);
Aaron Durbin923b4d52015-09-30 16:48:26 -0500336 num_relocs = read_le32(&relocb->SizeOfBlock) - sizeof(*relocb);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500337 num_relocs /= sizeof(uint16_t);
338 reloc = relative_offset(relocb, sizeof(*relocb));
339
340 printk(FSP_DBG_LVL, "Num relocs in block: %zx\n", num_relocs);
341
342 while (num_relocs > 0) {
Aaron Durbin923b4d52015-09-30 16:48:26 -0500343 uint16_t reloc_val = read_le16(reloc);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500344 int type = reloc_type(reloc_val);
345 size_t offset = reloc_offset(reloc_val);
346
347 printk(FSP_DBG_LVL, "reloc type %x offset %zx\n",
348 type, offset);
349
Sridhar Siricilla5902d882022-06-25 16:30:52 +0530350 if (type == EFI_IMAGE_REL_BASED_HIGHLOW ||
351 type == EFI_IMAGE_REL_BASED_DIR64) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500352 uint32_t *reloc_addr;
353 uint32_t val;
354
355 offset += rva_offset;
356 reloc_addr = (void *)&te_base[offset];
Aaron Durbin923b4d52015-09-30 16:48:26 -0500357 val = read_le32(reloc_addr);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500358
359 printk(FSP_DBG_LVL, "Adjusting %p %x -> %x\n",
360 reloc_addr, val, val + adj);
Aaron Durbin923b4d52015-09-30 16:48:26 -0500361 write_le32(reloc_addr, val + adj);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500362 } else if (type != EFI_IMAGE_REL_BASED_ABSOLUTE) {
363 printk(BIOS_ERR, "Unknown reloc type: %x\n",
364 type);
365 return -1;
366 }
367 num_relocs--;
368 reloc++;
369 }
370
371 /* Track consumption of relocation directory contents. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500372 relocd_offset += read_le32(&relocb->SizeOfBlock);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500373 /* Get next relocation block to process. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500374 relocb = relative_offset(relocb,
375 read_le32(&relocb->SizeOfBlock));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500376 }
377
378 return 0;
379}
380
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500381static size_t section_data_size(const EFI_COMMON_SECTION_HEADER *csh)
382{
383 size_t section_size;
384
Eddie Vas1df1cf92022-08-16 20:12:04 -0700385 if (csh_size(csh) == MASK_24BITS)
Aaron Durbin923b4d52015-09-30 16:48:26 -0500386 section_size = read_le32(&SECTION2_SIZE(csh));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500387 else
388 section_size = csh_size(csh);
389
390 return section_size - section_data_offset(csh);
391}
392
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500393static size_t ffs_file_size(const EFI_FFS_FILE_HEADER *ffsfh)
394{
395 size_t size;
396
Brandon Breitenstein51a0f7c2016-08-23 14:55:13 -0700397 if (IS_FFS_FILE2(ffsfh)) {
398 /*
399 * this cast is needed with UEFI 2.6 headers in order
400 * to read the UINT32 value that FFS_FILE2_SIZE converts
401 * the return into
402 */
403 uint32_t file2_size = FFS_FILE2_SIZE(ffsfh);
404 size = read_le32(&file2_size);
Lee Leahy72c60a42017-03-10 10:53:36 -0800405 } else {
Aaron Durbin923b4d52015-09-30 16:48:26 -0500406 size = read_le8(&ffsfh->Size[0]) << 0;
407 size |= read_le8(&ffsfh->Size[1]) << 8;
408 size |= read_le8(&ffsfh->Size[2]) << 16;
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500409 }
410 return size;
411}
412
413static int relocate_patch_table(void *fsp, size_t size, size_t offset,
414 ssize_t adjustment)
415{
416 struct fsp_patch_table *table;
417 size_t num;
418 size_t num_entries;
419
420 table = relative_offset(fsp, offset);
421
422 if ((offset + sizeof(*table) > size) ||
Aaron Durbin923b4d52015-09-30 16:48:26 -0500423 (read_le16(&table->header_length) + offset) > size) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500424 printk(BIOS_ERR, "FSPP not entirely contained in region.\n");
425 return -1;
426 }
427
Aaron Durbin923b4d52015-09-30 16:48:26 -0500428 num_entries = read_le32(&table->patch_entry_num);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500429 printk(FSP_DBG_LVL, "FSPP relocs: %zx\n", num_entries);
430
Aaron Durbin923b4d52015-09-30 16:48:26 -0500431 for (num = 0; num < num_entries; num++) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500432 uint32_t *reloc;
433 uint32_t reloc_val;
434
435 reloc = fspp_reloc(fsp, size,
Aaron Durbin923b4d52015-09-30 16:48:26 -0500436 read_le32(&table->patch_entries[num]));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500437
438 if (reloc == NULL) {
439 printk(BIOS_ERR, "Ignoring FSPP entry: %x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500440 read_le32(&table->patch_entries[num]));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500441 continue;
442 }
443
Aaron Durbin923b4d52015-09-30 16:48:26 -0500444 reloc_val = read_le32(reloc);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500445 printk(FSP_DBG_LVL, "Adjusting %p %x -> %x\n",
446 reloc, reloc_val,
447 (unsigned int)(reloc_val + adjustment));
448
Aaron Durbin923b4d52015-09-30 16:48:26 -0500449 write_le32(reloc, reloc_val + adjustment);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500450 }
451
452 return 0;
453}
454
455static ssize_t relocate_remaining_items(void *fsp, size_t size,
456 uintptr_t new_addr, size_t fih_offset)
457{
458 EFI_FFS_FILE_HEADER *ffsfh;
459 EFI_COMMON_SECTION_HEADER *csh;
460 FSP_INFO_HEADER *fih;
461 ssize_t adjustment;
462 size_t offset;
463
464 printk(FSP_DBG_LVL, "FSP_INFO_HEADER offset is %zx\n", fih_offset);
465
466 if (fih_offset == 0) {
467 printk(BIOS_ERR, "FSP_INFO_HEADER offset is 0.\n");
468 return -1;
469 }
470
471 /* FSP_INFO_HEADER at first file in FV within first RAW section. */
472 ffsfh = relative_offset(fsp, fih_offset);
473 fih_offset += file_section_offset(ffsfh);
474 csh = relative_offset(fsp, fih_offset);
475 fih_offset += section_data_offset(csh);
476 fih = relative_offset(fsp, fih_offset);
477
478 if (guid_compare(&ffsfh->Name, &fih_guid)) {
479 printk(BIOS_ERR, "Bad FIH GUID.\n");
480 return -1;
481 }
482
Aaron Durbin923b4d52015-09-30 16:48:26 -0500483 if (read_le8(&csh->Type) != EFI_SECTION_RAW) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500484 printk(BIOS_ERR, "FIH file should have raw section: %x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500485 read_le8(&csh->Type));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500486 return -1;
487 }
488
Aaron Durbin923b4d52015-09-30 16:48:26 -0500489 if (read_le32(&fih->Signature) != FSP_SIG) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500490 printk(BIOS_ERR, "Unexpected FIH signature: %08x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500491 read_le32(&fih->Signature));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500492 }
493
Aaron Durbin923b4d52015-09-30 16:48:26 -0500494 adjustment = (intptr_t)new_addr - read_le32(&fih->ImageBase);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500495
496 /* Update ImageBase to reflect FSP's new home. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500497 write_le32(&fih->ImageBase, adjustment + read_le32(&fih->ImageBase));
Eddie Vas1df1cf92022-08-16 20:12:04 -0700498 printk(FSP_DBG_LVL, "Updated FSP InfoHdr Image Base to %x\n",
499 read_le32(&fih->ImageBase));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500500
501 /* Need to find patch table and adjust each entry. The tables
502 * following FSP_INFO_HEADER have a 32-bit signature and header
503 * length. The patch table is denoted as having a 'FSPP' signature;
504 * the table format doesn't follow the other tables. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500505 offset = fih_offset + read_le32(&fih->HeaderLength);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500506 while (offset + 2 * sizeof(uint32_t) <= size) {
507 uint32_t *table_headers;
508
509 table_headers = relative_offset(fsp, offset);
510
511 printk(FSP_DBG_LVL, "Checking offset %zx for 'FSPP'\n",
512 offset);
513
Aaron Durbin923b4d52015-09-30 16:48:26 -0500514 if (read_le32(&table_headers[0]) != FSPP_SIG) {
515 offset += read_le32(&table_headers[1]);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500516 continue;
517 }
518
519 if (relocate_patch_table(fsp, size, offset, adjustment)) {
520 printk(BIOS_ERR, "FSPP relocation failed.\n");
521 return -1;
522 }
523
524 return fih_offset;
525 }
526
527 printk(BIOS_ERR, "Could not find the FSP patch table.\n");
528 return -1;
529}
530
531static ssize_t relocate_fvh(uintptr_t new_addr, void *fsp, size_t fsp_size,
532 size_t fvh_offset, size_t *fih_offset)
533{
534 EFI_FIRMWARE_VOLUME_HEADER *fvh;
535 EFI_FFS_FILE_HEADER *ffsfh;
536 EFI_COMMON_SECTION_HEADER *csh;
537 size_t offset;
538 size_t file_offset;
539 size_t size;
540 size_t fv_length;
541
542 offset = fvh_offset;
543 fvh = relative_offset(fsp, offset);
544
Aaron Durbin923b4d52015-09-30 16:48:26 -0500545 if (read_le32(&fvh->Signature) != EFI_FVH_SIGNATURE)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500546 return -1;
547
Aaron Durbin923b4d52015-09-30 16:48:26 -0500548 fv_length = read_le64(&fvh->FvLength);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500549
550 printk(FSP_DBG_LVL, "FVH length: %zx Offset: %zx Mapping length: %zx\n",
551 fv_length, offset, fsp_size);
552
Aaron Durbin923b4d52015-09-30 16:48:26 -0500553 if (fv_length + offset > fsp_size)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500554 return -1;
555
556 /* Parse only this FV. However, the algorithm uses offsets into the
557 * entire FSP region so make size include the starting offset. */
558 size = fv_length + offset;
559
560 if (guid_compare(&fvh->FileSystemGuid, &ffs2_guid)) {
561 printk(BIOS_ERR, "FVH not an FFS2 type.\n");
562 return -1;
563 }
564
Aaron Durbin923b4d52015-09-30 16:48:26 -0500565 if (read_le16(&fvh->ExtHeaderOffset) != 0) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500566 EFI_FIRMWARE_VOLUME_EXT_HEADER *fveh;
567
Aaron Durbin923b4d52015-09-30 16:48:26 -0500568 offset += read_le16(&fvh->ExtHeaderOffset);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500569 fveh = relative_offset(fsp, offset);
570 printk(FSP_DBG_LVL, "Extended Header Offset: %zx Size: %zx\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500571 (size_t)read_le16(&fvh->ExtHeaderOffset),
572 (size_t)read_le32(&fveh->ExtHeaderSize));
573 offset += read_le32(&fveh->ExtHeaderSize);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500574 /* FFS files are 8 byte aligned after extended header. */
575 offset = ALIGN_UP(offset, 8);
576 } else {
Aaron Durbin923b4d52015-09-30 16:48:26 -0500577 offset += read_le16(&fvh->HeaderLength);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500578 }
579
580 file_offset = offset;
581 while (file_offset + sizeof(*ffsfh) < size) {
582 offset = file_offset;
583 printk(FSP_DBG_LVL, "file offset: %zx\n", file_offset);
584
585 /* First file and section should be FSP info header. */
586 if (fih_offset != NULL && *fih_offset == 0)
587 *fih_offset = file_offset;
588
589 ffsfh = relative_offset(fsp, file_offset);
590
Aaron Durbin923b4d52015-09-30 16:48:26 -0500591 printk(FSP_DBG_LVL, "file type = %x\n", read_le8(&ffsfh->Type));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500592 printk(FSP_DBG_LVL, "file attribs = %x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500593 read_le8(&ffsfh->Attributes));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500594
595 /* Exit FV relocation when empty space found */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500596 if (read_le8(&ffsfh->Type) == EFI_FV_FILETYPE_FFS_MAX)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500597 break;
598
599 /* Next file on 8 byte alignment. */
600 file_offset += ffs_file_size(ffsfh);
601 file_offset = ALIGN_UP(file_offset, 8);
602
603 /* Padding files have no section information. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500604 if (read_le8(&ffsfh->Type) == EFI_FV_FILETYPE_FFS_PAD)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500605 continue;
606
607 offset += file_section_offset(ffsfh);
608
609 while (offset + sizeof(*csh) < file_offset) {
610 size_t data_size;
611 size_t data_offset;
Eddie Vas1df1cf92022-08-16 20:12:04 -0700612 void *section_data;
613 size_t section_offset;
614 uintptr_t section_addr;
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500615
616 csh = relative_offset(fsp, offset);
617
618 printk(FSP_DBG_LVL, "section offset: %zx\n", offset);
619 printk(FSP_DBG_LVL, "section type: %x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500620 read_le8(&csh->Type));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500621
622 data_size = section_data_size(csh);
623 data_offset = section_data_offset(csh);
624
625 if (data_size + data_offset + offset > file_offset) {
626 printk(BIOS_ERR, "Section exceeds FV size.\n");
627 return -1;
628 }
629
630 /*
Furquan Shaikhb0c2fe02016-05-09 12:23:01 -0700631 * The entire FSP image can be thought of as one
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500632 * program with a single link address even though there
633 * are multiple TEs linked separately. The reason is
634 * that each TE is linked for XIP. So in order to
635 * relocate the TE properly we need to form the
636 * relocated address based on the TE offset within
637 * FSP proper.
638 */
Eddie Vas1df1cf92022-08-16 20:12:04 -0700639 section_offset = offset + data_offset;
640 section_addr = new_addr + section_offset;
641 section_data = relative_offset(fsp, section_offset);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500642
Eddie Vas1df1cf92022-08-16 20:12:04 -0700643 if (read_le8(&csh->Type) == EFI_SECTION_TE) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500644 printk(FSP_DBG_LVL, "TE image at offset %zx\n",
Eddie Vas1df1cf92022-08-16 20:12:04 -0700645 section_offset);
646 te_relocate(section_addr, section_data);
647 } else if (read_le8(&csh->Type) == EFI_SECTION_PE32) {
648 printk(FSP_DBG_LVL, "PE32 image at offset %zx\n",
649 section_offset);
650 pe_relocate(new_addr, section_data, fsp, *fih_offset);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500651 }
652
653 offset += data_size + data_offset;
654 /* Sections are aligned to 4 bytes. */
655 offset = ALIGN_UP(offset, 4);
656 }
657 }
658
659 /* Return amount of buffer parsed: FV size. */
660 return fv_length;
661}
662
Furquan Shaikhb0c2fe02016-05-09 12:23:01 -0700663ssize_t fsp_component_relocate(uintptr_t new_addr, void *fsp, size_t size)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500664{
665 size_t offset;
666 size_t fih_offset;
667
668 offset = 0;
669 fih_offset = 0;
670 while (offset < size) {
671 ssize_t nparsed;
672
673 /* Relocate each FV within the FSP region. The FSP_INFO_HEADER
674 * should only be located in the first FV. */
675 if (offset == 0)
676 nparsed = relocate_fvh(new_addr, fsp, size, offset,
677 &fih_offset);
678 else
679 nparsed = relocate_fvh(new_addr, fsp, size, offset,
680 NULL);
681
682 /* FV should be larger than 0 or failed to parse. */
683 if (nparsed <= 0) {
684 printk(BIOS_ERR, "FV @ offset %zx relocation failed\n",
685 offset);
686 return -1;
687 }
688
689 offset += nparsed;
690 }
691
692 return relocate_remaining_items(fsp, size, new_addr, fih_offset);
693}
Furquan Shaikhb0c2fe02016-05-09 12:23:01 -0700694
695ssize_t fsp1_1_relocate(uintptr_t new_addr, void *fsp, size_t size)
696{
697 return fsp_component_relocate(new_addr, fsp, size);
698}