blob: c2c3811aced5bdb1c89853b4fcf740efb52c669c [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>
Werner Zeh314f2802022-11-16 07:56:38 +01006#include <inttypes.h>
Stefan Reinauerf76ceea72016-01-30 02:05:56 -08007/*
8 * Intel's code does not have a handle on changing global packing state.
9 * Therefore, one needs to protect against packing policies that are set
Frans Hendriks47ed2692018-11-26 09:47:49 +010010 * globally for a compilation unit just by including a header file.
Stefan Reinauerf76ceea72016-01-30 02:05:56 -080011 */
12#pragma pack(push)
13
14/* Default bind FSP 1.1 API to edk2 UEFI 2.4 types. */
15#include <vendorcode/intel/edk2/uefi_2.4/uefi_types.h>
16#include <vendorcode/intel/fsp/fsp1_1/IntelFspPkg/Include/FspInfoHeader.h>
17
18/* Restore original packing policy. */
19#pragma pack(pop)
20
Aaron Durbin923b4d52015-09-30 16:48:26 -050021#include <commonlib/helpers.h>
Elyes HAOUAS56ab5752021-12-31 18:45:46 +010022#include <stddef.h>
Aaron Durbina5be7fa2015-09-10 22:52:27 -050023#include <stdint.h>
24#include <string.h>
25
26#define FSP_DBG_LVL BIOS_NEVER
Eddie Vas1df1cf92022-08-16 20:12:04 -070027#define MASK_24BITS 0x00FFFFFF
Aaron Durbina5be7fa2015-09-10 22:52:27 -050028
29/*
30 * UEFI defines everything as little endian. However, this piece of code
31 * can be integrated in a userland tool. That tool could be on a big endian
32 * machine so one needs to access the fields within UEFI structures using
33 * endian-aware accesses.
34 */
35
36/* Return 0 if equal. Non-zero if not equal. */
37static int guid_compare(const EFI_GUID *le_guid, const EFI_GUID *native_guid)
38{
Aaron Durbin923b4d52015-09-30 16:48:26 -050039 if (read_le32(&le_guid->Data1) != native_guid->Data1)
Aaron Durbina5be7fa2015-09-10 22:52:27 -050040 return 1;
Aaron Durbin923b4d52015-09-30 16:48:26 -050041 if (read_le16(&le_guid->Data2) != native_guid->Data2)
Aaron Durbina5be7fa2015-09-10 22:52:27 -050042 return 1;
Aaron Durbin923b4d52015-09-30 16:48:26 -050043 if (read_le16(&le_guid->Data3) != native_guid->Data3)
Aaron Durbina5be7fa2015-09-10 22:52:27 -050044 return 1;
45 return memcmp(le_guid->Data4, native_guid->Data4,
46 ARRAY_SIZE(le_guid->Data4));
47}
48
Aaron Durbina5be7fa2015-09-10 22:52:27 -050049static const EFI_GUID ffs2_guid = EFI_FIRMWARE_FILE_SYSTEM2_GUID;
50static const EFI_GUID fih_guid = FSP_INFO_HEADER_GUID;
51
52struct fsp_patch_table {
53 uint32_t signature;
54 uint16_t header_length;
55 uint8_t header_revision;
56 uint8_t reserved;
57 uint32_t patch_entry_num;
58 uint32_t patch_entries[0];
Stefan Reinauer6a001132017-07-13 02:20:27 +020059} __packed;
Aaron Durbina5be7fa2015-09-10 22:52:27 -050060
61#define FSPP_SIG 0x50505346
62
63static void *relative_offset(void *base, ssize_t offset)
64{
65 uintptr_t loc;
66
67 loc = (uintptr_t)base;
68 loc += offset;
69
70 return (void *)loc;
71}
72
Eddie Vas1df1cf92022-08-16 20:12:04 -070073static size_t csh_size(const EFI_COMMON_SECTION_HEADER *csh)
74{
75 size_t size;
76
77 /* Unpack the array into a type that can be used. */
78 size = 0;
79 size |= read_le8(&csh->Size[0]) << 0;
80 size |= read_le8(&csh->Size[1]) << 8;
81 size |= read_le8(&csh->Size[2]) << 16;
82
83 return size;
84}
85
86static size_t file_section_offset(const EFI_FFS_FILE_HEADER *ffsfh)
87{
88 if (IS_FFS_FILE2(ffsfh))
89 return sizeof(EFI_FFS_FILE_HEADER2);
90 else
91 return sizeof(EFI_FFS_FILE_HEADER);
92}
93
94static size_t section_data_offset(const EFI_COMMON_SECTION_HEADER *csh)
95{
96 if (csh_size(csh) == MASK_24BITS)
97 return sizeof(EFI_COMMON_SECTION_HEADER2);
98 else
99 return sizeof(EFI_COMMON_SECTION_HEADER);
100}
101
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500102static uint32_t *fspp_reloc(void *fsp, size_t fsp_size, uint32_t e)
103{
104 size_t offset;
105
106 /* Offsets live in bits 23:0. */
Eddie Vas1df1cf92022-08-16 20:12:04 -0700107 offset = e & MASK_24BITS;
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500108
109 /* If bit 31 is set then the offset is considered a negative value
110 * relative to the end of the image using 16MiB as the offset's
111 * reference. */
112 if (e & (1 << 31))
113 offset = fsp_size - (16 * MiB - offset);
114
115 /* Determine if offset falls within fsp_size for a 32 bit relocation. */
116 if (offset > fsp_size - sizeof(uint32_t))
117 return NULL;
118
119 return relative_offset(fsp, offset);
120}
121
122static int reloc_type(uint16_t reloc_entry)
123{
124 /* Reloc type in upper 4 bits */
125 return reloc_entry >> 12;
126}
127
128static size_t reloc_offset(uint16_t reloc_entry)
129{
130 /* Offsets are in low 12 bits. */
131 return reloc_entry & ((1 << 12) - 1);
132}
133
Eddie Vas1df1cf92022-08-16 20:12:04 -0700134static FSP_INFO_HEADER *fsp_get_info_hdr(void *fsp, size_t fih_offset)
135{
136 EFI_FFS_FILE_HEADER *ffsfh;
137 EFI_COMMON_SECTION_HEADER *csh;
138 FSP_INFO_HEADER *fih;
139
140 printk(FSP_DBG_LVL, "FSP_INFO_HEADER offset is %zx\n", fih_offset);
141
142 if (fih_offset == 0) {
143 printk(BIOS_ERR, "FSP_INFO_HEADER offset is 0.\n");
144 return NULL;
145 }
146
147 /* FSP_INFO_HEADER is located at first file in FV within first RAW section. */
148 ffsfh = relative_offset(fsp, fih_offset);
149 fih_offset += file_section_offset(ffsfh);
150 csh = relative_offset(fsp, fih_offset);
151 fih_offset += section_data_offset(csh);
152 fih = relative_offset(fsp, fih_offset);
153
154 if (guid_compare(&ffsfh->Name, &fih_guid)) {
155 printk(BIOS_ERR, "Bad FIH GUID.\n");
156 return NULL;
157 }
158
159 if (read_le8(&csh->Type) != EFI_SECTION_RAW) {
160 printk(BIOS_ERR, "FIH file should have raw section: %x\n",
161 read_le8(&csh->Type));
162 return NULL;
163 }
164
165 if (read_le32(&fih->Signature) != FSP_SIG) {
166 printk(BIOS_ERR, "Unexpected FIH signature: %08x\n",
167 read_le32(&fih->Signature));
168 return NULL;
169 }
170
171 return fih;
172}
173
174static int pe_relocate(uintptr_t new_addr, void *pe, void *fsp, size_t fih_off)
175{
176 EFI_IMAGE_NT_HEADERS32 *peih;
177 EFI_IMAGE_DOS_HEADER *doshdr;
178 EFI_IMAGE_OPTIONAL_HEADER32 *ophdr;
179 FSP_INFO_HEADER *fih;
180 uint32_t roffset, rsize;
181 uint32_t offset;
182 uint8_t *pe_base = pe;
183 uint32_t image_base;
184 uint32_t img_base_off;
185 uint32_t delta;
186
187 doshdr = pe;
188 if (read_le16(&doshdr->e_magic) != EFI_IMAGE_DOS_SIGNATURE) {
189 printk(BIOS_ERR, "Invalid DOS Header/magic\n");
190 return -1;
191 }
192
193 peih = relative_offset(pe, doshdr->e_lfanew);
194
195 if (read_le32(&peih->Signature) != EFI_IMAGE_NT_SIGNATURE) {
196 printk(BIOS_ERR, "Invalid PE32 header\n");
197 return -1;
198 }
199
200 ophdr = &peih->OptionalHeader;
201
202 if (read_le16(&ophdr->Magic) != EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
203 printk(BIOS_ERR, "No support for non-PE32 images\n");
204 return -1;
205 }
206
207 fih = fsp_get_info_hdr(fsp, fih_off);
208 if (fih == NULL) {
209 printk(BIOS_ERR, "No Image base found for FSP PE32\n");
210 return -1;
211 }
212 image_base = read_le32(&fih->ImageBase);
213 printk(FSP_DBG_LVL, "FSP InfoHdr Image Base is %x\n", image_base);
214
215 delta = new_addr - image_base;
216
217 img_base_off = read_le32(&ophdr->ImageBase);
218 printk(FSP_DBG_LVL, "lfanew 0x%x, delta-0x%x, FSP Base 0x%x, NT32ImageBase 0x%x, offset 0x%x\n",
219 read_le32(&doshdr->e_lfanew),
220 delta, image_base, img_base_off,
221 (uint32_t)((uint8_t *)&ophdr->ImageBase - pe_base));
222
Werner Zeh314f2802022-11-16 07:56:38 +0100223 printk(FSP_DBG_LVL, "relocating PE32 image at addr - 0x%" PRIxPTR "\n", new_addr);
Eddie Vas1df1cf92022-08-16 20:12:04 -0700224 rsize = read_le32(&ophdr->DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC].Size);
225 roffset = read_le32(&ophdr->DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress);
226 printk(FSP_DBG_LVL, "relocation table at offset-%x,size=%x\n", roffset, rsize);
227 // TODO - add support for PE32+ also
228
229 offset = roffset;
230 while (offset < (roffset + rsize)) {
231 uint32_t vaddr;
232 uint32_t rlen, rnum;
233 uint16_t *rdata;
234 uint32_t i;
235 EFI_IMAGE_DATA_DIRECTORY *relocd;
236
Elyes Haouas995dfef2022-11-18 15:22:07 +0100237 relocd = (void *)&pe_base[offset];
Eddie Vas1df1cf92022-08-16 20:12:04 -0700238 offset += sizeof(*relocd);
239 // Read relocation type, offset pairs
240 rlen = read_le32(&relocd->Size) - sizeof(*relocd);
241 rnum = rlen / sizeof(uint16_t);
242 vaddr = read_le32(&relocd->VirtualAddress);
Elyes Haouas995dfef2022-11-18 15:22:07 +0100243 rdata = (uint16_t *)&pe_base[offset];
Eddie Vas1df1cf92022-08-16 20:12:04 -0700244 printk(FSP_DBG_LVL, "\t%d Relocs for RVA %x\n", rnum, vaddr);
245
246 for (i = 0; i < rnum; i++) {
247 uint16_t roff = reloc_offset(rdata[i]);
248 uint16_t rtype = reloc_type(rdata[i]);
249 uint32_t aoff = vaddr + roff;
250 uint32_t val;
251 printk(FSP_DBG_LVL, "\t\treloc type %x offset %x aoff %x, base-0x%x\n",
252 rtype, roff, aoff, img_base_off);
253 switch (rtype) {
254 case EFI_IMAGE_REL_BASED_ABSOLUTE:
255 continue;
256 case EFI_IMAGE_REL_BASED_HIGHLOW:
257 val = read_le32(&pe_base[aoff]);
258 printk(FSP_DBG_LVL, "Adjusting %p %x -> %x\n",
259 &pe_base[aoff], val, val + delta);
260 write_le32(&pe_base[aoff], val + delta);
261 break;
262 case EFI_IMAGE_REL_BASED_DIR64:
Elyes Haouasaba1c942022-11-09 15:05:23 +0100263 printk(BIOS_ERR, "Unsupported DIR64\n");
Eddie Vas1df1cf92022-08-16 20:12:04 -0700264 break;
265 default:
Elyes Haouasaba1c942022-11-09 15:05:23 +0100266 printk(BIOS_ERR, "Unsupported relocation type %d\n",
Eddie Vas1df1cf92022-08-16 20:12:04 -0700267 rtype);
268 return -1;
269 }
270 }
271 offset += sizeof(*rdata) * rnum;
272 }
273 printk(FSP_DBG_LVL, "Adjust Image Base %x->%x\n",
274 img_base_off, img_base_off + delta);
275 img_base_off += delta;
276 write_le32(&ophdr->ImageBase, img_base_off);
277
278 return -1;
279}
280
Aaron Durbin923b4d52015-09-30 16:48:26 -0500281static int te_relocate(uintptr_t new_addr, void *te)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500282{
283 EFI_TE_IMAGE_HEADER *teih;
284 EFI_IMAGE_DATA_DIRECTORY *relocd;
285 EFI_IMAGE_BASE_RELOCATION *relocb;
286 uintptr_t image_base;
287 size_t fixup_offset;
288 size_t num_relocs;
289 uint16_t *reloc;
290 size_t relocd_offset;
291 uint8_t *te_base;
292 uint32_t adj;
293
294 teih = te;
295
Aaron Durbin923b4d52015-09-30 16:48:26 -0500296 if (read_le16(&teih->Signature) != EFI_TE_IMAGE_HEADER_SIGNATURE) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500297 printk(BIOS_ERR, "TE Signature mismatch: %x vs %x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500298 read_le16(&teih->Signature),
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500299 EFI_TE_IMAGE_HEADER_SIGNATURE);
300 return -1;
301 }
302
303 /*
304 * A TE image is created by converting a PE file. Because of this
305 * the offsets within the headers are off. In order to calculate
Elyes HAOUAS23c1c4e2019-12-18 13:21:37 +0100306 * the correct relative offsets one needs to subtract fixup_offset
307 * from the encoded offsets. Similarly, the linked address of the
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500308 * program is found by adding the fixup_offset to the ImageBase.
309 */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500310 fixup_offset = read_le16(&teih->StrippedSize);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500311 fixup_offset -= sizeof(EFI_TE_IMAGE_HEADER);
312 /* Keep track of a base that is correctly adjusted so that offsets
313 * can be used directly. */
314 te_base = te;
315 te_base -= fixup_offset;
316
Aaron Durbin923b4d52015-09-30 16:48:26 -0500317 image_base = read_le64(&teih->ImageBase);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500318 adj = new_addr - (image_base + fixup_offset);
319
320 printk(FSP_DBG_LVL, "TE Image %p -> %p adjust value: %x\n",
321 (void *)image_base, (void *)new_addr, adj);
322
323 /* Adjust ImageBase for consistency. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500324 write_le64(&teih->ImageBase, (uint32_t)(image_base + adj));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500325
326 relocd = &teih->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_BASERELOC];
327
328 relocd_offset = 0;
329 /* Though the field name is VirtualAddress it's actually relative to
330 * the beginning of the image which is linked at ImageBase. */
331 relocb = relative_offset(te,
Aaron Durbin923b4d52015-09-30 16:48:26 -0500332 read_le32(&relocd->VirtualAddress) - fixup_offset);
333 while (relocd_offset < read_le32(&relocd->Size)) {
334 size_t rva_offset = read_le32(&relocb->VirtualAddress);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500335
336 printk(FSP_DBG_LVL, "Relocs for RVA offset %zx\n", rva_offset);
Aaron Durbin923b4d52015-09-30 16:48:26 -0500337 num_relocs = read_le32(&relocb->SizeOfBlock) - sizeof(*relocb);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500338 num_relocs /= sizeof(uint16_t);
339 reloc = relative_offset(relocb, sizeof(*relocb));
340
341 printk(FSP_DBG_LVL, "Num relocs in block: %zx\n", num_relocs);
342
343 while (num_relocs > 0) {
Aaron Durbin923b4d52015-09-30 16:48:26 -0500344 uint16_t reloc_val = read_le16(reloc);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500345 int type = reloc_type(reloc_val);
346 size_t offset = reloc_offset(reloc_val);
347
348 printk(FSP_DBG_LVL, "reloc type %x offset %zx\n",
349 type, offset);
350
Sridhar Siricilla5902d882022-06-25 16:30:52 +0530351 if (type == EFI_IMAGE_REL_BASED_HIGHLOW ||
352 type == EFI_IMAGE_REL_BASED_DIR64) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500353 uint32_t *reloc_addr;
354 uint32_t val;
355
356 offset += rva_offset;
357 reloc_addr = (void *)&te_base[offset];
Aaron Durbin923b4d52015-09-30 16:48:26 -0500358 val = read_le32(reloc_addr);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500359
360 printk(FSP_DBG_LVL, "Adjusting %p %x -> %x\n",
361 reloc_addr, val, val + adj);
Aaron Durbin923b4d52015-09-30 16:48:26 -0500362 write_le32(reloc_addr, val + adj);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500363 } else if (type != EFI_IMAGE_REL_BASED_ABSOLUTE) {
364 printk(BIOS_ERR, "Unknown reloc type: %x\n",
365 type);
366 return -1;
367 }
368 num_relocs--;
369 reloc++;
370 }
371
372 /* Track consumption of relocation directory contents. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500373 relocd_offset += read_le32(&relocb->SizeOfBlock);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500374 /* Get next relocation block to process. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500375 relocb = relative_offset(relocb,
376 read_le32(&relocb->SizeOfBlock));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500377 }
378
379 return 0;
380}
381
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500382static size_t section_data_size(const EFI_COMMON_SECTION_HEADER *csh)
383{
384 size_t section_size;
385
Eddie Vas1df1cf92022-08-16 20:12:04 -0700386 if (csh_size(csh) == MASK_24BITS)
Aaron Durbin923b4d52015-09-30 16:48:26 -0500387 section_size = read_le32(&SECTION2_SIZE(csh));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500388 else
389 section_size = csh_size(csh);
390
391 return section_size - section_data_offset(csh);
392}
393
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500394static size_t ffs_file_size(const EFI_FFS_FILE_HEADER *ffsfh)
395{
396 size_t size;
397
Brandon Breitenstein51a0f7c2016-08-23 14:55:13 -0700398 if (IS_FFS_FILE2(ffsfh)) {
399 /*
400 * this cast is needed with UEFI 2.6 headers in order
401 * to read the UINT32 value that FFS_FILE2_SIZE converts
402 * the return into
403 */
404 uint32_t file2_size = FFS_FILE2_SIZE(ffsfh);
405 size = read_le32(&file2_size);
Lee Leahy72c60a42017-03-10 10:53:36 -0800406 } else {
Aaron Durbin923b4d52015-09-30 16:48:26 -0500407 size = read_le8(&ffsfh->Size[0]) << 0;
408 size |= read_le8(&ffsfh->Size[1]) << 8;
409 size |= read_le8(&ffsfh->Size[2]) << 16;
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500410 }
411 return size;
412}
413
414static int relocate_patch_table(void *fsp, size_t size, size_t offset,
415 ssize_t adjustment)
416{
417 struct fsp_patch_table *table;
418 size_t num;
419 size_t num_entries;
420
421 table = relative_offset(fsp, offset);
422
423 if ((offset + sizeof(*table) > size) ||
Aaron Durbin923b4d52015-09-30 16:48:26 -0500424 (read_le16(&table->header_length) + offset) > size) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500425 printk(BIOS_ERR, "FSPP not entirely contained in region.\n");
426 return -1;
427 }
428
Aaron Durbin923b4d52015-09-30 16:48:26 -0500429 num_entries = read_le32(&table->patch_entry_num);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500430 printk(FSP_DBG_LVL, "FSPP relocs: %zx\n", num_entries);
431
Aaron Durbin923b4d52015-09-30 16:48:26 -0500432 for (num = 0; num < num_entries; num++) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500433 uint32_t *reloc;
434 uint32_t reloc_val;
435
436 reloc = fspp_reloc(fsp, size,
Aaron Durbin923b4d52015-09-30 16:48:26 -0500437 read_le32(&table->patch_entries[num]));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500438
439 if (reloc == NULL) {
440 printk(BIOS_ERR, "Ignoring FSPP entry: %x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500441 read_le32(&table->patch_entries[num]));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500442 continue;
443 }
444
Aaron Durbin923b4d52015-09-30 16:48:26 -0500445 reloc_val = read_le32(reloc);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500446 printk(FSP_DBG_LVL, "Adjusting %p %x -> %x\n",
447 reloc, reloc_val,
448 (unsigned int)(reloc_val + adjustment));
449
Aaron Durbin923b4d52015-09-30 16:48:26 -0500450 write_le32(reloc, reloc_val + adjustment);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500451 }
452
453 return 0;
454}
455
456static ssize_t relocate_remaining_items(void *fsp, size_t size,
457 uintptr_t new_addr, size_t fih_offset)
458{
459 EFI_FFS_FILE_HEADER *ffsfh;
460 EFI_COMMON_SECTION_HEADER *csh;
461 FSP_INFO_HEADER *fih;
462 ssize_t adjustment;
463 size_t offset;
464
465 printk(FSP_DBG_LVL, "FSP_INFO_HEADER offset is %zx\n", fih_offset);
466
467 if (fih_offset == 0) {
468 printk(BIOS_ERR, "FSP_INFO_HEADER offset is 0.\n");
469 return -1;
470 }
471
472 /* FSP_INFO_HEADER at first file in FV within first RAW section. */
473 ffsfh = relative_offset(fsp, fih_offset);
474 fih_offset += file_section_offset(ffsfh);
475 csh = relative_offset(fsp, fih_offset);
476 fih_offset += section_data_offset(csh);
477 fih = relative_offset(fsp, fih_offset);
478
479 if (guid_compare(&ffsfh->Name, &fih_guid)) {
480 printk(BIOS_ERR, "Bad FIH GUID.\n");
481 return -1;
482 }
483
Aaron Durbin923b4d52015-09-30 16:48:26 -0500484 if (read_le8(&csh->Type) != EFI_SECTION_RAW) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500485 printk(BIOS_ERR, "FIH file should have raw section: %x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500486 read_le8(&csh->Type));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500487 return -1;
488 }
489
Aaron Durbin923b4d52015-09-30 16:48:26 -0500490 if (read_le32(&fih->Signature) != FSP_SIG) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500491 printk(BIOS_ERR, "Unexpected FIH signature: %08x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500492 read_le32(&fih->Signature));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500493 }
494
Aaron Durbin923b4d52015-09-30 16:48:26 -0500495 adjustment = (intptr_t)new_addr - read_le32(&fih->ImageBase);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500496
497 /* Update ImageBase to reflect FSP's new home. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500498 write_le32(&fih->ImageBase, adjustment + read_le32(&fih->ImageBase));
Eddie Vas1df1cf92022-08-16 20:12:04 -0700499 printk(FSP_DBG_LVL, "Updated FSP InfoHdr Image Base to %x\n",
500 read_le32(&fih->ImageBase));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500501
502 /* Need to find patch table and adjust each entry. The tables
503 * following FSP_INFO_HEADER have a 32-bit signature and header
504 * length. The patch table is denoted as having a 'FSPP' signature;
505 * the table format doesn't follow the other tables. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500506 offset = fih_offset + read_le32(&fih->HeaderLength);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500507 while (offset + 2 * sizeof(uint32_t) <= size) {
508 uint32_t *table_headers;
509
510 table_headers = relative_offset(fsp, offset);
511
512 printk(FSP_DBG_LVL, "Checking offset %zx for 'FSPP'\n",
513 offset);
514
Aaron Durbin923b4d52015-09-30 16:48:26 -0500515 if (read_le32(&table_headers[0]) != FSPP_SIG) {
516 offset += read_le32(&table_headers[1]);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500517 continue;
518 }
519
520 if (relocate_patch_table(fsp, size, offset, adjustment)) {
521 printk(BIOS_ERR, "FSPP relocation failed.\n");
522 return -1;
523 }
524
525 return fih_offset;
526 }
527
528 printk(BIOS_ERR, "Could not find the FSP patch table.\n");
529 return -1;
530}
531
532static ssize_t relocate_fvh(uintptr_t new_addr, void *fsp, size_t fsp_size,
533 size_t fvh_offset, size_t *fih_offset)
534{
535 EFI_FIRMWARE_VOLUME_HEADER *fvh;
536 EFI_FFS_FILE_HEADER *ffsfh;
537 EFI_COMMON_SECTION_HEADER *csh;
538 size_t offset;
539 size_t file_offset;
540 size_t size;
541 size_t fv_length;
542
543 offset = fvh_offset;
544 fvh = relative_offset(fsp, offset);
545
Aaron Durbin923b4d52015-09-30 16:48:26 -0500546 if (read_le32(&fvh->Signature) != EFI_FVH_SIGNATURE)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500547 return -1;
548
Aaron Durbin923b4d52015-09-30 16:48:26 -0500549 fv_length = read_le64(&fvh->FvLength);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500550
551 printk(FSP_DBG_LVL, "FVH length: %zx Offset: %zx Mapping length: %zx\n",
552 fv_length, offset, fsp_size);
553
Aaron Durbin923b4d52015-09-30 16:48:26 -0500554 if (fv_length + offset > fsp_size)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500555 return -1;
556
557 /* Parse only this FV. However, the algorithm uses offsets into the
558 * entire FSP region so make size include the starting offset. */
559 size = fv_length + offset;
560
561 if (guid_compare(&fvh->FileSystemGuid, &ffs2_guid)) {
562 printk(BIOS_ERR, "FVH not an FFS2 type.\n");
563 return -1;
564 }
565
Aaron Durbin923b4d52015-09-30 16:48:26 -0500566 if (read_le16(&fvh->ExtHeaderOffset) != 0) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500567 EFI_FIRMWARE_VOLUME_EXT_HEADER *fveh;
568
Aaron Durbin923b4d52015-09-30 16:48:26 -0500569 offset += read_le16(&fvh->ExtHeaderOffset);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500570 fveh = relative_offset(fsp, offset);
571 printk(FSP_DBG_LVL, "Extended Header Offset: %zx Size: %zx\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500572 (size_t)read_le16(&fvh->ExtHeaderOffset),
573 (size_t)read_le32(&fveh->ExtHeaderSize));
574 offset += read_le32(&fveh->ExtHeaderSize);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500575 /* FFS files are 8 byte aligned after extended header. */
576 offset = ALIGN_UP(offset, 8);
577 } else {
Aaron Durbin923b4d52015-09-30 16:48:26 -0500578 offset += read_le16(&fvh->HeaderLength);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500579 }
580
581 file_offset = offset;
582 while (file_offset + sizeof(*ffsfh) < size) {
583 offset = file_offset;
584 printk(FSP_DBG_LVL, "file offset: %zx\n", file_offset);
585
586 /* First file and section should be FSP info header. */
587 if (fih_offset != NULL && *fih_offset == 0)
588 *fih_offset = file_offset;
589
590 ffsfh = relative_offset(fsp, file_offset);
591
Aaron Durbin923b4d52015-09-30 16:48:26 -0500592 printk(FSP_DBG_LVL, "file type = %x\n", read_le8(&ffsfh->Type));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500593 printk(FSP_DBG_LVL, "file attribs = %x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500594 read_le8(&ffsfh->Attributes));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500595
596 /* Exit FV relocation when empty space found */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500597 if (read_le8(&ffsfh->Type) == EFI_FV_FILETYPE_FFS_MAX)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500598 break;
599
600 /* Next file on 8 byte alignment. */
601 file_offset += ffs_file_size(ffsfh);
602 file_offset = ALIGN_UP(file_offset, 8);
603
604 /* Padding files have no section information. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500605 if (read_le8(&ffsfh->Type) == EFI_FV_FILETYPE_FFS_PAD)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500606 continue;
607
608 offset += file_section_offset(ffsfh);
609
610 while (offset + sizeof(*csh) < file_offset) {
611 size_t data_size;
612 size_t data_offset;
Eddie Vas1df1cf92022-08-16 20:12:04 -0700613 void *section_data;
614 size_t section_offset;
615 uintptr_t section_addr;
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500616
617 csh = relative_offset(fsp, offset);
618
619 printk(FSP_DBG_LVL, "section offset: %zx\n", offset);
620 printk(FSP_DBG_LVL, "section type: %x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500621 read_le8(&csh->Type));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500622
623 data_size = section_data_size(csh);
624 data_offset = section_data_offset(csh);
625
626 if (data_size + data_offset + offset > file_offset) {
627 printk(BIOS_ERR, "Section exceeds FV size.\n");
628 return -1;
629 }
630
631 /*
Furquan Shaikhb0c2fe02016-05-09 12:23:01 -0700632 * The entire FSP image can be thought of as one
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500633 * program with a single link address even though there
634 * are multiple TEs linked separately. The reason is
635 * that each TE is linked for XIP. So in order to
636 * relocate the TE properly we need to form the
637 * relocated address based on the TE offset within
638 * FSP proper.
639 */
Eddie Vas1df1cf92022-08-16 20:12:04 -0700640 section_offset = offset + data_offset;
641 section_addr = new_addr + section_offset;
642 section_data = relative_offset(fsp, section_offset);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500643
Eddie Vas1df1cf92022-08-16 20:12:04 -0700644 if (read_le8(&csh->Type) == EFI_SECTION_TE) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500645 printk(FSP_DBG_LVL, "TE image at offset %zx\n",
Eddie Vas1df1cf92022-08-16 20:12:04 -0700646 section_offset);
647 te_relocate(section_addr, section_data);
648 } else if (read_le8(&csh->Type) == EFI_SECTION_PE32) {
649 printk(FSP_DBG_LVL, "PE32 image at offset %zx\n",
650 section_offset);
651 pe_relocate(new_addr, section_data, fsp, *fih_offset);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500652 }
653
654 offset += data_size + data_offset;
655 /* Sections are aligned to 4 bytes. */
656 offset = ALIGN_UP(offset, 4);
657 }
658 }
659
660 /* Return amount of buffer parsed: FV size. */
661 return fv_length;
662}
663
Furquan Shaikhb0c2fe02016-05-09 12:23:01 -0700664ssize_t fsp_component_relocate(uintptr_t new_addr, void *fsp, size_t size)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500665{
666 size_t offset;
667 size_t fih_offset;
668
669 offset = 0;
670 fih_offset = 0;
671 while (offset < size) {
672 ssize_t nparsed;
673
674 /* Relocate each FV within the FSP region. The FSP_INFO_HEADER
675 * should only be located in the first FV. */
676 if (offset == 0)
677 nparsed = relocate_fvh(new_addr, fsp, size, offset,
678 &fih_offset);
679 else
680 nparsed = relocate_fvh(new_addr, fsp, size, offset,
681 NULL);
682
683 /* FV should be larger than 0 or failed to parse. */
684 if (nparsed <= 0) {
685 printk(BIOS_ERR, "FV @ offset %zx relocation failed\n",
686 offset);
687 return -1;
688 }
689
690 offset += nparsed;
691 }
692
693 return relocate_remaining_items(fsp, size, new_addr, fih_offset);
694}
Furquan Shaikhb0c2fe02016-05-09 12:23:01 -0700695
696ssize_t fsp1_1_relocate(uintptr_t new_addr, void *fsp, size_t size)
697{
698 return fsp_component_relocate(new_addr, fsp, size);
699}