blob: 0bf50b497eed613978c0189be4cabba7e2e81c74 [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>
Aaron Durbin923b4d52015-09-30 16:48:26 -05007#include <commonlib/helpers.h>
Elyes HAOUAS56ab5752021-12-31 18:45:46 +01008#include <stddef.h>
Aaron Durbina5be7fa2015-09-10 22:52:27 -05009#include <stdint.h>
10#include <string.h>
Subrata Banik698fa272024-04-01 23:44:43 +053011#include <vendorcode/intel/fsp/fsp_header.h>
Aaron Durbina5be7fa2015-09-10 22:52:27 -050012
13#define FSP_DBG_LVL BIOS_NEVER
Eddie Vas1df1cf92022-08-16 20:12:04 -070014#define MASK_24BITS 0x00FFFFFF
Aaron Durbina5be7fa2015-09-10 22:52:27 -050015
16/*
17 * UEFI defines everything as little endian. However, this piece of code
18 * can be integrated in a userland tool. That tool could be on a big endian
19 * machine so one needs to access the fields within UEFI structures using
20 * endian-aware accesses.
21 */
22
23/* Return 0 if equal. Non-zero if not equal. */
24static int guid_compare(const EFI_GUID *le_guid, const EFI_GUID *native_guid)
25{
Aaron Durbin923b4d52015-09-30 16:48:26 -050026 if (read_le32(&le_guid->Data1) != native_guid->Data1)
Aaron Durbina5be7fa2015-09-10 22:52:27 -050027 return 1;
Aaron Durbin923b4d52015-09-30 16:48:26 -050028 if (read_le16(&le_guid->Data2) != native_guid->Data2)
Aaron Durbina5be7fa2015-09-10 22:52:27 -050029 return 1;
Aaron Durbin923b4d52015-09-30 16:48:26 -050030 if (read_le16(&le_guid->Data3) != native_guid->Data3)
Aaron Durbina5be7fa2015-09-10 22:52:27 -050031 return 1;
32 return memcmp(le_guid->Data4, native_guid->Data4,
33 ARRAY_SIZE(le_guid->Data4));
34}
35
Aaron Durbina5be7fa2015-09-10 22:52:27 -050036static const EFI_GUID ffs2_guid = EFI_FIRMWARE_FILE_SYSTEM2_GUID;
37static const EFI_GUID fih_guid = FSP_INFO_HEADER_GUID;
38
39struct fsp_patch_table {
40 uint32_t signature;
41 uint16_t header_length;
42 uint8_t header_revision;
43 uint8_t reserved;
44 uint32_t patch_entry_num;
Elyes Haouasc1700e02023-07-28 06:16:27 +020045 uint32_t patch_entries[];
Stefan Reinauer6a001132017-07-13 02:20:27 +020046} __packed;
Aaron Durbina5be7fa2015-09-10 22:52:27 -050047
48#define FSPP_SIG 0x50505346
49
50static void *relative_offset(void *base, ssize_t offset)
51{
52 uintptr_t loc;
53
54 loc = (uintptr_t)base;
55 loc += offset;
56
57 return (void *)loc;
58}
59
Eddie Vas1df1cf92022-08-16 20:12:04 -070060static size_t csh_size(const EFI_COMMON_SECTION_HEADER *csh)
61{
62 size_t size;
63
64 /* Unpack the array into a type that can be used. */
65 size = 0;
66 size |= read_le8(&csh->Size[0]) << 0;
67 size |= read_le8(&csh->Size[1]) << 8;
68 size |= read_le8(&csh->Size[2]) << 16;
69
70 return size;
71}
72
73static size_t file_section_offset(const EFI_FFS_FILE_HEADER *ffsfh)
74{
75 if (IS_FFS_FILE2(ffsfh))
76 return sizeof(EFI_FFS_FILE_HEADER2);
77 else
78 return sizeof(EFI_FFS_FILE_HEADER);
79}
80
81static size_t section_data_offset(const EFI_COMMON_SECTION_HEADER *csh)
82{
83 if (csh_size(csh) == MASK_24BITS)
84 return sizeof(EFI_COMMON_SECTION_HEADER2);
85 else
86 return sizeof(EFI_COMMON_SECTION_HEADER);
87}
88
Aaron Durbina5be7fa2015-09-10 22:52:27 -050089static uint32_t *fspp_reloc(void *fsp, size_t fsp_size, uint32_t e)
90{
91 size_t offset;
92
93 /* Offsets live in bits 23:0. */
Eddie Vas1df1cf92022-08-16 20:12:04 -070094 offset = e & MASK_24BITS;
Aaron Durbina5be7fa2015-09-10 22:52:27 -050095
96 /* If bit 31 is set then the offset is considered a negative value
97 * relative to the end of the image using 16MiB as the offset's
98 * reference. */
99 if (e & (1 << 31))
100 offset = fsp_size - (16 * MiB - offset);
101
102 /* Determine if offset falls within fsp_size for a 32 bit relocation. */
103 if (offset > fsp_size - sizeof(uint32_t))
104 return NULL;
105
106 return relative_offset(fsp, offset);
107}
108
109static int reloc_type(uint16_t reloc_entry)
110{
111 /* Reloc type in upper 4 bits */
112 return reloc_entry >> 12;
113}
114
115static size_t reloc_offset(uint16_t reloc_entry)
116{
117 /* Offsets are in low 12 bits. */
118 return reloc_entry & ((1 << 12) - 1);
119}
120
Eddie Vas1df1cf92022-08-16 20:12:04 -0700121static FSP_INFO_HEADER *fsp_get_info_hdr(void *fsp, size_t fih_offset)
122{
123 EFI_FFS_FILE_HEADER *ffsfh;
124 EFI_COMMON_SECTION_HEADER *csh;
125 FSP_INFO_HEADER *fih;
126
127 printk(FSP_DBG_LVL, "FSP_INFO_HEADER offset is %zx\n", fih_offset);
128
129 if (fih_offset == 0) {
130 printk(BIOS_ERR, "FSP_INFO_HEADER offset is 0.\n");
131 return NULL;
132 }
133
134 /* FSP_INFO_HEADER is located at first file in FV within first RAW section. */
135 ffsfh = relative_offset(fsp, fih_offset);
136 fih_offset += file_section_offset(ffsfh);
137 csh = relative_offset(fsp, fih_offset);
138 fih_offset += section_data_offset(csh);
139 fih = relative_offset(fsp, fih_offset);
140
141 if (guid_compare(&ffsfh->Name, &fih_guid)) {
142 printk(BIOS_ERR, "Bad FIH GUID.\n");
143 return NULL;
144 }
145
146 if (read_le8(&csh->Type) != EFI_SECTION_RAW) {
147 printk(BIOS_ERR, "FIH file should have raw section: %x\n",
148 read_le8(&csh->Type));
149 return NULL;
150 }
151
152 if (read_le32(&fih->Signature) != FSP_SIG) {
153 printk(BIOS_ERR, "Unexpected FIH signature: %08x\n",
154 read_le32(&fih->Signature));
155 return NULL;
156 }
157
158 return fih;
159}
160
161static int pe_relocate(uintptr_t new_addr, void *pe, void *fsp, size_t fih_off)
162{
Patrick Rudolphf40f5b62024-05-27 16:09:59 +0200163 EFI_IMAGE_OPTIONAL_HEADER_UNION *peih;
Eddie Vas1df1cf92022-08-16 20:12:04 -0700164 EFI_IMAGE_DOS_HEADER *doshdr;
165 EFI_IMAGE_OPTIONAL_HEADER32 *ophdr;
Patrick Rudolphf40f5b62024-05-27 16:09:59 +0200166 EFI_IMAGE_OPTIONAL_HEADER64 *ophdr64;
Eddie Vas1df1cf92022-08-16 20:12:04 -0700167 FSP_INFO_HEADER *fih;
168 uint32_t roffset, rsize;
169 uint32_t offset;
170 uint8_t *pe_base = pe;
Patrick Rudolphf40f5b62024-05-27 16:09:59 +0200171 uint64_t image_base;
172 uint64_t img_base_off;
173 uint64_t delta;
Eddie Vas1df1cf92022-08-16 20:12:04 -0700174
175 doshdr = pe;
176 if (read_le16(&doshdr->e_magic) != EFI_IMAGE_DOS_SIGNATURE) {
177 printk(BIOS_ERR, "Invalid DOS Header/magic\n");
178 return -1;
179 }
180
181 peih = relative_offset(pe, doshdr->e_lfanew);
182
Patrick Rudolphf40f5b62024-05-27 16:09:59 +0200183 if (read_le32(&peih->Pe32.Signature) != EFI_IMAGE_NT_SIGNATURE) {
Eddie Vas1df1cf92022-08-16 20:12:04 -0700184 printk(BIOS_ERR, "Invalid PE32 header\n");
185 return -1;
186 }
187
Patrick Rudolphf40f5b62024-05-27 16:09:59 +0200188 ophdr = &peih->Pe32.OptionalHeader;
189 ophdr64 = &peih->Pe32Plus.OptionalHeader;
Eddie Vas1df1cf92022-08-16 20:12:04 -0700190
Patrick Rudolphf40f5b62024-05-27 16:09:59 +0200191 if (read_le16(&ophdr->Magic) == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
192 ophdr64 = NULL;
193 } else if (read_le16(&ophdr64->Magic) == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
194 ophdr = NULL;
195 } else {
196 printk(BIOS_ERR, "No support for non-PE32/PE32+ images\n");
Eddie Vas1df1cf92022-08-16 20:12:04 -0700197 return -1;
198 }
199
200 fih = fsp_get_info_hdr(fsp, fih_off);
201 if (fih == NULL) {
202 printk(BIOS_ERR, "No Image base found for FSP PE32\n");
203 return -1;
204 }
205 image_base = read_le32(&fih->ImageBase);
Patrick Rudolphf40f5b62024-05-27 16:09:59 +0200206 printk(FSP_DBG_LVL, "FSP InfoHdr Image Base is %" PRIX64"\n", image_base);
Eddie Vas1df1cf92022-08-16 20:12:04 -0700207
208 delta = new_addr - image_base;
209
Patrick Rudolphf40f5b62024-05-27 16:09:59 +0200210 img_base_off = ophdr ? read_le32(&ophdr->ImageBase) : read_le64(&ophdr64->ImageBase);
211 printk(FSP_DBG_LVL, "lfanew 0x%x, delta-0x%" PRIX64 ", FSP Base 0x%" PRIX64 ", NT32ImageBase 0x%" PRIX64 ", offset 0x%" PRIX64 "\n",
Eddie Vas1df1cf92022-08-16 20:12:04 -0700212 read_le32(&doshdr->e_lfanew),
213 delta, image_base, img_base_off,
Patrick Rudolphf40f5b62024-05-27 16:09:59 +0200214 (uint64_t)((uint8_t *)(uintptr_t)img_base_off - pe_base));
Eddie Vas1df1cf92022-08-16 20:12:04 -0700215
Patrick Rudolphf40f5b62024-05-27 16:09:59 +0200216 printk(FSP_DBG_LVL, "relocating PE32%s image at addr - 0x%" PRIxPTR "\n", ophdr ? "" : "+", new_addr);
217 if (ophdr) {
218 rsize = read_le32(&ophdr->DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC].Size);
219 roffset = read_le32(&ophdr->DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress);
220 } else {
221 rsize = read_le32(&ophdr64->DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC].Size);
222 roffset = read_le32(&ophdr64->DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress);
223 }
224
Eddie Vas1df1cf92022-08-16 20:12:04 -0700225 printk(FSP_DBG_LVL, "relocation table at offset-%x,size=%x\n", roffset, rsize);
Eddie Vas1df1cf92022-08-16 20:12:04 -0700226
227 offset = roffset;
228 while (offset < (roffset + rsize)) {
229 uint32_t vaddr;
230 uint32_t rlen, rnum;
231 uint16_t *rdata;
232 uint32_t i;
233 EFI_IMAGE_DATA_DIRECTORY *relocd;
234
Elyes Haouas995dfef2022-11-18 15:22:07 +0100235 relocd = (void *)&pe_base[offset];
Eddie Vas1df1cf92022-08-16 20:12:04 -0700236 offset += sizeof(*relocd);
237 // Read relocation type, offset pairs
238 rlen = read_le32(&relocd->Size) - sizeof(*relocd);
239 rnum = rlen / sizeof(uint16_t);
240 vaddr = read_le32(&relocd->VirtualAddress);
Elyes Haouas995dfef2022-11-18 15:22:07 +0100241 rdata = (uint16_t *)&pe_base[offset];
Eddie Vas1df1cf92022-08-16 20:12:04 -0700242 printk(FSP_DBG_LVL, "\t%d Relocs for RVA %x\n", rnum, vaddr);
243
244 for (i = 0; i < rnum; i++) {
245 uint16_t roff = reloc_offset(rdata[i]);
246 uint16_t rtype = reloc_type(rdata[i]);
247 uint32_t aoff = vaddr + roff;
Patrick Rudolphf40f5b62024-05-27 16:09:59 +0200248 uint64_t val;
249 printk(FSP_DBG_LVL, "\t\treloc type %x offset %x aoff %x, base-0x%" PRIX64 "\n",
Eddie Vas1df1cf92022-08-16 20:12:04 -0700250 rtype, roff, aoff, img_base_off);
251 switch (rtype) {
252 case EFI_IMAGE_REL_BASED_ABSOLUTE:
253 continue;
254 case EFI_IMAGE_REL_BASED_HIGHLOW:
255 val = read_le32(&pe_base[aoff]);
Patrick Rudolphf40f5b62024-05-27 16:09:59 +0200256 printk(FSP_DBG_LVL, "Adjusting %p %" PRIX64 " -> %" PRIX64 "\n",
Eddie Vas1df1cf92022-08-16 20:12:04 -0700257 &pe_base[aoff], val, val + delta);
258 write_le32(&pe_base[aoff], val + delta);
259 break;
260 case EFI_IMAGE_REL_BASED_DIR64:
Patrick Rudolphf40f5b62024-05-27 16:09:59 +0200261 val = read_le64(&pe_base[aoff]);
262 printk(FSP_DBG_LVL, "Adjusting %p %" PRIX64 " -> %" PRIX64 "\n",
263 &pe_base[aoff], val, val + delta);
264 write_le64(&pe_base[aoff], val + delta);
Eddie Vas1df1cf92022-08-16 20:12:04 -0700265 break;
266 default:
Elyes Haouasaba1c942022-11-09 15:05:23 +0100267 printk(BIOS_ERR, "Unsupported relocation type %d\n",
Eddie Vas1df1cf92022-08-16 20:12:04 -0700268 rtype);
269 return -1;
270 }
271 }
272 offset += sizeof(*rdata) * rnum;
273 }
Patrick Rudolphf40f5b62024-05-27 16:09:59 +0200274 printk(FSP_DBG_LVL, "Adjust Image Base %" PRIX64 "->%" PRIX64 "\n",
Eddie Vas1df1cf92022-08-16 20:12:04 -0700275 img_base_off, img_base_off + delta);
276 img_base_off += delta;
Patrick Rudolphf40f5b62024-05-27 16:09:59 +0200277 if (ophdr)
278 write_le32(&ophdr->ImageBase, img_base_off);
279 else
280 write_le64(&ophdr64->ImageBase, img_base_off);
Eddie Vas1df1cf92022-08-16 20:12:04 -0700281
Jeremy Compostellad86260a2023-08-02 16:59:03 -0700282 return 0;
Eddie Vas1df1cf92022-08-16 20:12:04 -0700283}
284
Aaron Durbin923b4d52015-09-30 16:48:26 -0500285static int te_relocate(uintptr_t new_addr, void *te)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500286{
287 EFI_TE_IMAGE_HEADER *teih;
288 EFI_IMAGE_DATA_DIRECTORY *relocd;
289 EFI_IMAGE_BASE_RELOCATION *relocb;
290 uintptr_t image_base;
291 size_t fixup_offset;
292 size_t num_relocs;
293 uint16_t *reloc;
294 size_t relocd_offset;
295 uint8_t *te_base;
296 uint32_t adj;
297
298 teih = te;
299
Aaron Durbin923b4d52015-09-30 16:48:26 -0500300 if (read_le16(&teih->Signature) != EFI_TE_IMAGE_HEADER_SIGNATURE) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500301 printk(BIOS_ERR, "TE Signature mismatch: %x vs %x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500302 read_le16(&teih->Signature),
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500303 EFI_TE_IMAGE_HEADER_SIGNATURE);
304 return -1;
305 }
306
307 /*
308 * A TE image is created by converting a PE file. Because of this
309 * the offsets within the headers are off. In order to calculate
Elyes HAOUAS23c1c4e2019-12-18 13:21:37 +0100310 * the correct relative offsets one needs to subtract fixup_offset
311 * from the encoded offsets. Similarly, the linked address of the
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500312 * program is found by adding the fixup_offset to the ImageBase.
313 */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500314 fixup_offset = read_le16(&teih->StrippedSize);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500315 fixup_offset -= sizeof(EFI_TE_IMAGE_HEADER);
316 /* Keep track of a base that is correctly adjusted so that offsets
317 * can be used directly. */
318 te_base = te;
319 te_base -= fixup_offset;
320
Aaron Durbin923b4d52015-09-30 16:48:26 -0500321 image_base = read_le64(&teih->ImageBase);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500322 adj = new_addr - (image_base + fixup_offset);
323
324 printk(FSP_DBG_LVL, "TE Image %p -> %p adjust value: %x\n",
325 (void *)image_base, (void *)new_addr, adj);
326
327 /* Adjust ImageBase for consistency. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500328 write_le64(&teih->ImageBase, (uint32_t)(image_base + adj));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500329
330 relocd = &teih->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_BASERELOC];
331
332 relocd_offset = 0;
333 /* Though the field name is VirtualAddress it's actually relative to
334 * the beginning of the image which is linked at ImageBase. */
335 relocb = relative_offset(te,
Aaron Durbin923b4d52015-09-30 16:48:26 -0500336 read_le32(&relocd->VirtualAddress) - fixup_offset);
337 while (relocd_offset < read_le32(&relocd->Size)) {
338 size_t rva_offset = read_le32(&relocb->VirtualAddress);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500339
340 printk(FSP_DBG_LVL, "Relocs for RVA offset %zx\n", rva_offset);
Aaron Durbin923b4d52015-09-30 16:48:26 -0500341 num_relocs = read_le32(&relocb->SizeOfBlock) - sizeof(*relocb);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500342 num_relocs /= sizeof(uint16_t);
343 reloc = relative_offset(relocb, sizeof(*relocb));
344
345 printk(FSP_DBG_LVL, "Num relocs in block: %zx\n", num_relocs);
346
347 while (num_relocs > 0) {
Aaron Durbin923b4d52015-09-30 16:48:26 -0500348 uint16_t reloc_val = read_le16(reloc);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500349 int type = reloc_type(reloc_val);
350 size_t offset = reloc_offset(reloc_val);
351
352 printk(FSP_DBG_LVL, "reloc type %x offset %zx\n",
353 type, offset);
354
Sridhar Siricilla5902d882022-06-25 16:30:52 +0530355 if (type == EFI_IMAGE_REL_BASED_HIGHLOW ||
356 type == EFI_IMAGE_REL_BASED_DIR64) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500357 uint32_t *reloc_addr;
358 uint32_t val;
359
360 offset += rva_offset;
361 reloc_addr = (void *)&te_base[offset];
Aaron Durbin923b4d52015-09-30 16:48:26 -0500362 val = read_le32(reloc_addr);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500363
364 printk(FSP_DBG_LVL, "Adjusting %p %x -> %x\n",
365 reloc_addr, val, val + adj);
Aaron Durbin923b4d52015-09-30 16:48:26 -0500366 write_le32(reloc_addr, val + adj);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500367 } else if (type != EFI_IMAGE_REL_BASED_ABSOLUTE) {
368 printk(BIOS_ERR, "Unknown reloc type: %x\n",
369 type);
370 return -1;
371 }
372 num_relocs--;
373 reloc++;
374 }
375
376 /* Track consumption of relocation directory contents. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500377 relocd_offset += read_le32(&relocb->SizeOfBlock);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500378 /* Get next relocation block to process. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500379 relocb = relative_offset(relocb,
380 read_le32(&relocb->SizeOfBlock));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500381 }
382
383 return 0;
384}
385
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500386static size_t section_data_size(const EFI_COMMON_SECTION_HEADER *csh)
387{
388 size_t section_size;
389
Eddie Vas1df1cf92022-08-16 20:12:04 -0700390 if (csh_size(csh) == MASK_24BITS)
Aaron Durbin923b4d52015-09-30 16:48:26 -0500391 section_size = read_le32(&SECTION2_SIZE(csh));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500392 else
393 section_size = csh_size(csh);
394
395 return section_size - section_data_offset(csh);
396}
397
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500398static size_t ffs_file_size(const EFI_FFS_FILE_HEADER *ffsfh)
399{
400 size_t size;
401
Brandon Breitenstein51a0f7c2016-08-23 14:55:13 -0700402 if (IS_FFS_FILE2(ffsfh)) {
403 /*
404 * this cast is needed with UEFI 2.6 headers in order
405 * to read the UINT32 value that FFS_FILE2_SIZE converts
406 * the return into
407 */
408 uint32_t file2_size = FFS_FILE2_SIZE(ffsfh);
409 size = read_le32(&file2_size);
Lee Leahy72c60a42017-03-10 10:53:36 -0800410 } else {
Aaron Durbin923b4d52015-09-30 16:48:26 -0500411 size = read_le8(&ffsfh->Size[0]) << 0;
412 size |= read_le8(&ffsfh->Size[1]) << 8;
413 size |= read_le8(&ffsfh->Size[2]) << 16;
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500414 }
415 return size;
416}
417
418static int relocate_patch_table(void *fsp, size_t size, size_t offset,
419 ssize_t adjustment)
420{
421 struct fsp_patch_table *table;
422 size_t num;
423 size_t num_entries;
424
425 table = relative_offset(fsp, offset);
426
427 if ((offset + sizeof(*table) > size) ||
Aaron Durbin923b4d52015-09-30 16:48:26 -0500428 (read_le16(&table->header_length) + offset) > size) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500429 printk(BIOS_ERR, "FSPP not entirely contained in region.\n");
430 return -1;
431 }
432
Aaron Durbin923b4d52015-09-30 16:48:26 -0500433 num_entries = read_le32(&table->patch_entry_num);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500434 printk(FSP_DBG_LVL, "FSPP relocs: %zx\n", num_entries);
435
Aaron Durbin923b4d52015-09-30 16:48:26 -0500436 for (num = 0; num < num_entries; num++) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500437 uint32_t *reloc;
438 uint32_t reloc_val;
439
440 reloc = fspp_reloc(fsp, size,
Aaron Durbin923b4d52015-09-30 16:48:26 -0500441 read_le32(&table->patch_entries[num]));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500442
443 if (reloc == NULL) {
444 printk(BIOS_ERR, "Ignoring FSPP entry: %x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500445 read_le32(&table->patch_entries[num]));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500446 continue;
447 }
448
Aaron Durbin923b4d52015-09-30 16:48:26 -0500449 reloc_val = read_le32(reloc);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500450 printk(FSP_DBG_LVL, "Adjusting %p %x -> %x\n",
451 reloc, reloc_val,
452 (unsigned int)(reloc_val + adjustment));
453
Aaron Durbin923b4d52015-09-30 16:48:26 -0500454 write_le32(reloc, reloc_val + adjustment);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500455 }
456
457 return 0;
458}
459
460static ssize_t relocate_remaining_items(void *fsp, size_t size,
461 uintptr_t new_addr, size_t fih_offset)
462{
463 EFI_FFS_FILE_HEADER *ffsfh;
464 EFI_COMMON_SECTION_HEADER *csh;
465 FSP_INFO_HEADER *fih;
466 ssize_t adjustment;
467 size_t offset;
468
469 printk(FSP_DBG_LVL, "FSP_INFO_HEADER offset is %zx\n", fih_offset);
470
471 if (fih_offset == 0) {
472 printk(BIOS_ERR, "FSP_INFO_HEADER offset is 0.\n");
473 return -1;
474 }
475
476 /* FSP_INFO_HEADER at first file in FV within first RAW section. */
477 ffsfh = relative_offset(fsp, fih_offset);
478 fih_offset += file_section_offset(ffsfh);
479 csh = relative_offset(fsp, fih_offset);
480 fih_offset += section_data_offset(csh);
481 fih = relative_offset(fsp, fih_offset);
482
483 if (guid_compare(&ffsfh->Name, &fih_guid)) {
484 printk(BIOS_ERR, "Bad FIH GUID.\n");
485 return -1;
486 }
487
Aaron Durbin923b4d52015-09-30 16:48:26 -0500488 if (read_le8(&csh->Type) != EFI_SECTION_RAW) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500489 printk(BIOS_ERR, "FIH file should have raw section: %x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500490 read_le8(&csh->Type));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500491 return -1;
492 }
493
Aaron Durbin923b4d52015-09-30 16:48:26 -0500494 if (read_le32(&fih->Signature) != FSP_SIG) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500495 printk(BIOS_ERR, "Unexpected FIH signature: %08x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500496 read_le32(&fih->Signature));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500497 }
498
Aaron Durbin923b4d52015-09-30 16:48:26 -0500499 adjustment = (intptr_t)new_addr - read_le32(&fih->ImageBase);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500500
501 /* Update ImageBase to reflect FSP's new home. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500502 write_le32(&fih->ImageBase, adjustment + read_le32(&fih->ImageBase));
Eddie Vas1df1cf92022-08-16 20:12:04 -0700503 printk(FSP_DBG_LVL, "Updated FSP InfoHdr Image Base to %x\n",
504 read_le32(&fih->ImageBase));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500505
506 /* Need to find patch table and adjust each entry. The tables
507 * following FSP_INFO_HEADER have a 32-bit signature and header
508 * length. The patch table is denoted as having a 'FSPP' signature;
509 * the table format doesn't follow the other tables. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500510 offset = fih_offset + read_le32(&fih->HeaderLength);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500511 while (offset + 2 * sizeof(uint32_t) <= size) {
512 uint32_t *table_headers;
513
514 table_headers = relative_offset(fsp, offset);
515
516 printk(FSP_DBG_LVL, "Checking offset %zx for 'FSPP'\n",
517 offset);
518
Aaron Durbin923b4d52015-09-30 16:48:26 -0500519 if (read_le32(&table_headers[0]) != FSPP_SIG) {
520 offset += read_le32(&table_headers[1]);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500521 continue;
522 }
523
524 if (relocate_patch_table(fsp, size, offset, adjustment)) {
525 printk(BIOS_ERR, "FSPP relocation failed.\n");
526 return -1;
527 }
528
529 return fih_offset;
530 }
531
532 printk(BIOS_ERR, "Could not find the FSP patch table.\n");
533 return -1;
534}
535
536static ssize_t relocate_fvh(uintptr_t new_addr, void *fsp, size_t fsp_size,
537 size_t fvh_offset, size_t *fih_offset)
538{
539 EFI_FIRMWARE_VOLUME_HEADER *fvh;
540 EFI_FFS_FILE_HEADER *ffsfh;
541 EFI_COMMON_SECTION_HEADER *csh;
542 size_t offset;
543 size_t file_offset;
544 size_t size;
545 size_t fv_length;
546
547 offset = fvh_offset;
548 fvh = relative_offset(fsp, offset);
549
Aaron Durbin923b4d52015-09-30 16:48:26 -0500550 if (read_le32(&fvh->Signature) != EFI_FVH_SIGNATURE)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500551 return -1;
552
Aaron Durbin923b4d52015-09-30 16:48:26 -0500553 fv_length = read_le64(&fvh->FvLength);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500554
555 printk(FSP_DBG_LVL, "FVH length: %zx Offset: %zx Mapping length: %zx\n",
556 fv_length, offset, fsp_size);
557
Aaron Durbin923b4d52015-09-30 16:48:26 -0500558 if (fv_length + offset > fsp_size)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500559 return -1;
560
561 /* Parse only this FV. However, the algorithm uses offsets into the
562 * entire FSP region so make size include the starting offset. */
563 size = fv_length + offset;
564
565 if (guid_compare(&fvh->FileSystemGuid, &ffs2_guid)) {
566 printk(BIOS_ERR, "FVH not an FFS2 type.\n");
567 return -1;
568 }
569
Aaron Durbin923b4d52015-09-30 16:48:26 -0500570 if (read_le16(&fvh->ExtHeaderOffset) != 0) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500571 EFI_FIRMWARE_VOLUME_EXT_HEADER *fveh;
572
Aaron Durbin923b4d52015-09-30 16:48:26 -0500573 offset += read_le16(&fvh->ExtHeaderOffset);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500574 fveh = relative_offset(fsp, offset);
575 printk(FSP_DBG_LVL, "Extended Header Offset: %zx Size: %zx\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500576 (size_t)read_le16(&fvh->ExtHeaderOffset),
577 (size_t)read_le32(&fveh->ExtHeaderSize));
578 offset += read_le32(&fveh->ExtHeaderSize);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500579 /* FFS files are 8 byte aligned after extended header. */
580 offset = ALIGN_UP(offset, 8);
581 } else {
Aaron Durbin923b4d52015-09-30 16:48:26 -0500582 offset += read_le16(&fvh->HeaderLength);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500583 }
584
585 file_offset = offset;
586 while (file_offset + sizeof(*ffsfh) < size) {
587 offset = file_offset;
588 printk(FSP_DBG_LVL, "file offset: %zx\n", file_offset);
589
590 /* First file and section should be FSP info header. */
Jeremy Compostellaeb938082023-10-24 10:09:54 -0700591 if (*fih_offset == 0)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500592 *fih_offset = file_offset;
593
594 ffsfh = relative_offset(fsp, file_offset);
595
Aaron Durbin923b4d52015-09-30 16:48:26 -0500596 printk(FSP_DBG_LVL, "file type = %x\n", read_le8(&ffsfh->Type));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500597 printk(FSP_DBG_LVL, "file attribs = %x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500598 read_le8(&ffsfh->Attributes));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500599
600 /* Exit FV relocation when empty space found */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500601 if (read_le8(&ffsfh->Type) == EFI_FV_FILETYPE_FFS_MAX)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500602 break;
603
604 /* Next file on 8 byte alignment. */
605 file_offset += ffs_file_size(ffsfh);
606 file_offset = ALIGN_UP(file_offset, 8);
607
608 /* Padding files have no section information. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500609 if (read_le8(&ffsfh->Type) == EFI_FV_FILETYPE_FFS_PAD)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500610 continue;
611
612 offset += file_section_offset(ffsfh);
613
614 while (offset + sizeof(*csh) < file_offset) {
615 size_t data_size;
616 size_t data_offset;
Eddie Vas1df1cf92022-08-16 20:12:04 -0700617 void *section_data;
618 size_t section_offset;
619 uintptr_t section_addr;
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500620
621 csh = relative_offset(fsp, offset);
622
623 printk(FSP_DBG_LVL, "section offset: %zx\n", offset);
624 printk(FSP_DBG_LVL, "section type: %x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500625 read_le8(&csh->Type));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500626
627 data_size = section_data_size(csh);
628 data_offset = section_data_offset(csh);
629
630 if (data_size + data_offset + offset > file_offset) {
631 printk(BIOS_ERR, "Section exceeds FV size.\n");
632 return -1;
633 }
634
635 /*
Furquan Shaikhb0c2fe02016-05-09 12:23:01 -0700636 * The entire FSP image can be thought of as one
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500637 * program with a single link address even though there
638 * are multiple TEs linked separately. The reason is
639 * that each TE is linked for XIP. So in order to
640 * relocate the TE properly we need to form the
641 * relocated address based on the TE offset within
642 * FSP proper.
643 */
Eddie Vas1df1cf92022-08-16 20:12:04 -0700644 section_offset = offset + data_offset;
645 section_addr = new_addr + section_offset;
646 section_data = relative_offset(fsp, section_offset);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500647
Eddie Vas1df1cf92022-08-16 20:12:04 -0700648 if (read_le8(&csh->Type) == EFI_SECTION_TE) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500649 printk(FSP_DBG_LVL, "TE image at offset %zx\n",
Eddie Vas1df1cf92022-08-16 20:12:04 -0700650 section_offset);
651 te_relocate(section_addr, section_data);
652 } else if (read_le8(&csh->Type) == EFI_SECTION_PE32) {
653 printk(FSP_DBG_LVL, "PE32 image at offset %zx\n",
654 section_offset);
655 pe_relocate(new_addr, section_data, fsp, *fih_offset);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500656 }
657
658 offset += data_size + data_offset;
659 /* Sections are aligned to 4 bytes. */
660 offset = ALIGN_UP(offset, 4);
661 }
662 }
663
664 /* Return amount of buffer parsed: FV size. */
665 return fv_length;
666}
667
Furquan Shaikhb0c2fe02016-05-09 12:23:01 -0700668ssize_t fsp_component_relocate(uintptr_t new_addr, void *fsp, size_t size)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500669{
670 size_t offset;
671 size_t fih_offset;
672
673 offset = 0;
674 fih_offset = 0;
675 while (offset < size) {
676 ssize_t nparsed;
677
Jeremy Compostellaeb938082023-10-24 10:09:54 -0700678 /* Relocate each FV within the FSP region. */
679 nparsed = relocate_fvh(new_addr, fsp, size, offset, &fih_offset);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500680
681 /* FV should be larger than 0 or failed to parse. */
682 if (nparsed <= 0) {
683 printk(BIOS_ERR, "FV @ offset %zx relocation failed\n",
684 offset);
685 return -1;
686 }
687
688 offset += nparsed;
689 }
690
691 return relocate_remaining_items(fsp, size, new_addr, fih_offset);
692}
Furquan Shaikhb0c2fe02016-05-09 12:23:01 -0700693
694ssize_t fsp1_1_relocate(uintptr_t new_addr, void *fsp, size_t size)
695{
696 return fsp_component_relocate(new_addr, fsp, size);
697}