blob: f57e278cf0e39a045d10e7360bfeec17bd1f885b [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{
163 EFI_IMAGE_NT_HEADERS32 *peih;
164 EFI_IMAGE_DOS_HEADER *doshdr;
165 EFI_IMAGE_OPTIONAL_HEADER32 *ophdr;
166 FSP_INFO_HEADER *fih;
167 uint32_t roffset, rsize;
168 uint32_t offset;
169 uint8_t *pe_base = pe;
170 uint32_t image_base;
171 uint32_t img_base_off;
172 uint32_t delta;
173
174 doshdr = pe;
175 if (read_le16(&doshdr->e_magic) != EFI_IMAGE_DOS_SIGNATURE) {
176 printk(BIOS_ERR, "Invalid DOS Header/magic\n");
177 return -1;
178 }
179
180 peih = relative_offset(pe, doshdr->e_lfanew);
181
182 if (read_le32(&peih->Signature) != EFI_IMAGE_NT_SIGNATURE) {
183 printk(BIOS_ERR, "Invalid PE32 header\n");
184 return -1;
185 }
186
187 ophdr = &peih->OptionalHeader;
188
189 if (read_le16(&ophdr->Magic) != EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
190 printk(BIOS_ERR, "No support for non-PE32 images\n");
191 return -1;
192 }
193
194 fih = fsp_get_info_hdr(fsp, fih_off);
195 if (fih == NULL) {
196 printk(BIOS_ERR, "No Image base found for FSP PE32\n");
197 return -1;
198 }
199 image_base = read_le32(&fih->ImageBase);
200 printk(FSP_DBG_LVL, "FSP InfoHdr Image Base is %x\n", image_base);
201
202 delta = new_addr - image_base;
203
204 img_base_off = read_le32(&ophdr->ImageBase);
205 printk(FSP_DBG_LVL, "lfanew 0x%x, delta-0x%x, FSP Base 0x%x, NT32ImageBase 0x%x, offset 0x%x\n",
206 read_le32(&doshdr->e_lfanew),
207 delta, image_base, img_base_off,
208 (uint32_t)((uint8_t *)&ophdr->ImageBase - pe_base));
209
Werner Zeh314f2802022-11-16 07:56:38 +0100210 printk(FSP_DBG_LVL, "relocating PE32 image at addr - 0x%" PRIxPTR "\n", new_addr);
Eddie Vas1df1cf92022-08-16 20:12:04 -0700211 rsize = read_le32(&ophdr->DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC].Size);
212 roffset = read_le32(&ophdr->DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress);
213 printk(FSP_DBG_LVL, "relocation table at offset-%x,size=%x\n", roffset, rsize);
214 // TODO - add support for PE32+ also
215
216 offset = roffset;
217 while (offset < (roffset + rsize)) {
218 uint32_t vaddr;
219 uint32_t rlen, rnum;
220 uint16_t *rdata;
221 uint32_t i;
222 EFI_IMAGE_DATA_DIRECTORY *relocd;
223
Elyes Haouas995dfef2022-11-18 15:22:07 +0100224 relocd = (void *)&pe_base[offset];
Eddie Vas1df1cf92022-08-16 20:12:04 -0700225 offset += sizeof(*relocd);
226 // Read relocation type, offset pairs
227 rlen = read_le32(&relocd->Size) - sizeof(*relocd);
228 rnum = rlen / sizeof(uint16_t);
229 vaddr = read_le32(&relocd->VirtualAddress);
Elyes Haouas995dfef2022-11-18 15:22:07 +0100230 rdata = (uint16_t *)&pe_base[offset];
Eddie Vas1df1cf92022-08-16 20:12:04 -0700231 printk(FSP_DBG_LVL, "\t%d Relocs for RVA %x\n", rnum, vaddr);
232
233 for (i = 0; i < rnum; i++) {
234 uint16_t roff = reloc_offset(rdata[i]);
235 uint16_t rtype = reloc_type(rdata[i]);
236 uint32_t aoff = vaddr + roff;
237 uint32_t val;
238 printk(FSP_DBG_LVL, "\t\treloc type %x offset %x aoff %x, base-0x%x\n",
239 rtype, roff, aoff, img_base_off);
240 switch (rtype) {
241 case EFI_IMAGE_REL_BASED_ABSOLUTE:
242 continue;
243 case EFI_IMAGE_REL_BASED_HIGHLOW:
244 val = read_le32(&pe_base[aoff]);
245 printk(FSP_DBG_LVL, "Adjusting %p %x -> %x\n",
246 &pe_base[aoff], val, val + delta);
247 write_le32(&pe_base[aoff], val + delta);
248 break;
249 case EFI_IMAGE_REL_BASED_DIR64:
Elyes Haouasaba1c942022-11-09 15:05:23 +0100250 printk(BIOS_ERR, "Unsupported DIR64\n");
Eddie Vas1df1cf92022-08-16 20:12:04 -0700251 break;
252 default:
Elyes Haouasaba1c942022-11-09 15:05:23 +0100253 printk(BIOS_ERR, "Unsupported relocation type %d\n",
Eddie Vas1df1cf92022-08-16 20:12:04 -0700254 rtype);
255 return -1;
256 }
257 }
258 offset += sizeof(*rdata) * rnum;
259 }
260 printk(FSP_DBG_LVL, "Adjust Image Base %x->%x\n",
261 img_base_off, img_base_off + delta);
262 img_base_off += delta;
263 write_le32(&ophdr->ImageBase, img_base_off);
264
Jeremy Compostellad86260a2023-08-02 16:59:03 -0700265 return 0;
Eddie Vas1df1cf92022-08-16 20:12:04 -0700266}
267
Aaron Durbin923b4d52015-09-30 16:48:26 -0500268static int te_relocate(uintptr_t new_addr, void *te)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500269{
270 EFI_TE_IMAGE_HEADER *teih;
271 EFI_IMAGE_DATA_DIRECTORY *relocd;
272 EFI_IMAGE_BASE_RELOCATION *relocb;
273 uintptr_t image_base;
274 size_t fixup_offset;
275 size_t num_relocs;
276 uint16_t *reloc;
277 size_t relocd_offset;
278 uint8_t *te_base;
279 uint32_t adj;
280
281 teih = te;
282
Aaron Durbin923b4d52015-09-30 16:48:26 -0500283 if (read_le16(&teih->Signature) != EFI_TE_IMAGE_HEADER_SIGNATURE) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500284 printk(BIOS_ERR, "TE Signature mismatch: %x vs %x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500285 read_le16(&teih->Signature),
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500286 EFI_TE_IMAGE_HEADER_SIGNATURE);
287 return -1;
288 }
289
290 /*
291 * A TE image is created by converting a PE file. Because of this
292 * the offsets within the headers are off. In order to calculate
Elyes HAOUAS23c1c4e2019-12-18 13:21:37 +0100293 * the correct relative offsets one needs to subtract fixup_offset
294 * from the encoded offsets. Similarly, the linked address of the
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500295 * program is found by adding the fixup_offset to the ImageBase.
296 */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500297 fixup_offset = read_le16(&teih->StrippedSize);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500298 fixup_offset -= sizeof(EFI_TE_IMAGE_HEADER);
299 /* Keep track of a base that is correctly adjusted so that offsets
300 * can be used directly. */
301 te_base = te;
302 te_base -= fixup_offset;
303
Aaron Durbin923b4d52015-09-30 16:48:26 -0500304 image_base = read_le64(&teih->ImageBase);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500305 adj = new_addr - (image_base + fixup_offset);
306
307 printk(FSP_DBG_LVL, "TE Image %p -> %p adjust value: %x\n",
308 (void *)image_base, (void *)new_addr, adj);
309
310 /* Adjust ImageBase for consistency. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500311 write_le64(&teih->ImageBase, (uint32_t)(image_base + adj));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500312
313 relocd = &teih->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_BASERELOC];
314
315 relocd_offset = 0;
316 /* Though the field name is VirtualAddress it's actually relative to
317 * the beginning of the image which is linked at ImageBase. */
318 relocb = relative_offset(te,
Aaron Durbin923b4d52015-09-30 16:48:26 -0500319 read_le32(&relocd->VirtualAddress) - fixup_offset);
320 while (relocd_offset < read_le32(&relocd->Size)) {
321 size_t rva_offset = read_le32(&relocb->VirtualAddress);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500322
323 printk(FSP_DBG_LVL, "Relocs for RVA offset %zx\n", rva_offset);
Aaron Durbin923b4d52015-09-30 16:48:26 -0500324 num_relocs = read_le32(&relocb->SizeOfBlock) - sizeof(*relocb);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500325 num_relocs /= sizeof(uint16_t);
326 reloc = relative_offset(relocb, sizeof(*relocb));
327
328 printk(FSP_DBG_LVL, "Num relocs in block: %zx\n", num_relocs);
329
330 while (num_relocs > 0) {
Aaron Durbin923b4d52015-09-30 16:48:26 -0500331 uint16_t reloc_val = read_le16(reloc);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500332 int type = reloc_type(reloc_val);
333 size_t offset = reloc_offset(reloc_val);
334
335 printk(FSP_DBG_LVL, "reloc type %x offset %zx\n",
336 type, offset);
337
Sridhar Siricilla5902d882022-06-25 16:30:52 +0530338 if (type == EFI_IMAGE_REL_BASED_HIGHLOW ||
339 type == EFI_IMAGE_REL_BASED_DIR64) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500340 uint32_t *reloc_addr;
341 uint32_t val;
342
343 offset += rva_offset;
344 reloc_addr = (void *)&te_base[offset];
Aaron Durbin923b4d52015-09-30 16:48:26 -0500345 val = read_le32(reloc_addr);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500346
347 printk(FSP_DBG_LVL, "Adjusting %p %x -> %x\n",
348 reloc_addr, val, val + adj);
Aaron Durbin923b4d52015-09-30 16:48:26 -0500349 write_le32(reloc_addr, val + adj);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500350 } else if (type != EFI_IMAGE_REL_BASED_ABSOLUTE) {
351 printk(BIOS_ERR, "Unknown reloc type: %x\n",
352 type);
353 return -1;
354 }
355 num_relocs--;
356 reloc++;
357 }
358
359 /* Track consumption of relocation directory contents. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500360 relocd_offset += read_le32(&relocb->SizeOfBlock);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500361 /* Get next relocation block to process. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500362 relocb = relative_offset(relocb,
363 read_le32(&relocb->SizeOfBlock));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500364 }
365
366 return 0;
367}
368
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500369static size_t section_data_size(const EFI_COMMON_SECTION_HEADER *csh)
370{
371 size_t section_size;
372
Eddie Vas1df1cf92022-08-16 20:12:04 -0700373 if (csh_size(csh) == MASK_24BITS)
Aaron Durbin923b4d52015-09-30 16:48:26 -0500374 section_size = read_le32(&SECTION2_SIZE(csh));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500375 else
376 section_size = csh_size(csh);
377
378 return section_size - section_data_offset(csh);
379}
380
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500381static size_t ffs_file_size(const EFI_FFS_FILE_HEADER *ffsfh)
382{
383 size_t size;
384
Brandon Breitenstein51a0f7c2016-08-23 14:55:13 -0700385 if (IS_FFS_FILE2(ffsfh)) {
386 /*
387 * this cast is needed with UEFI 2.6 headers in order
388 * to read the UINT32 value that FFS_FILE2_SIZE converts
389 * the return into
390 */
391 uint32_t file2_size = FFS_FILE2_SIZE(ffsfh);
392 size = read_le32(&file2_size);
Lee Leahy72c60a42017-03-10 10:53:36 -0800393 } else {
Aaron Durbin923b4d52015-09-30 16:48:26 -0500394 size = read_le8(&ffsfh->Size[0]) << 0;
395 size |= read_le8(&ffsfh->Size[1]) << 8;
396 size |= read_le8(&ffsfh->Size[2]) << 16;
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500397 }
398 return size;
399}
400
401static int relocate_patch_table(void *fsp, size_t size, size_t offset,
402 ssize_t adjustment)
403{
404 struct fsp_patch_table *table;
405 size_t num;
406 size_t num_entries;
407
408 table = relative_offset(fsp, offset);
409
410 if ((offset + sizeof(*table) > size) ||
Aaron Durbin923b4d52015-09-30 16:48:26 -0500411 (read_le16(&table->header_length) + offset) > size) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500412 printk(BIOS_ERR, "FSPP not entirely contained in region.\n");
413 return -1;
414 }
415
Aaron Durbin923b4d52015-09-30 16:48:26 -0500416 num_entries = read_le32(&table->patch_entry_num);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500417 printk(FSP_DBG_LVL, "FSPP relocs: %zx\n", num_entries);
418
Aaron Durbin923b4d52015-09-30 16:48:26 -0500419 for (num = 0; num < num_entries; num++) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500420 uint32_t *reloc;
421 uint32_t reloc_val;
422
423 reloc = fspp_reloc(fsp, size,
Aaron Durbin923b4d52015-09-30 16:48:26 -0500424 read_le32(&table->patch_entries[num]));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500425
426 if (reloc == NULL) {
427 printk(BIOS_ERR, "Ignoring FSPP entry: %x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500428 read_le32(&table->patch_entries[num]));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500429 continue;
430 }
431
Aaron Durbin923b4d52015-09-30 16:48:26 -0500432 reloc_val = read_le32(reloc);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500433 printk(FSP_DBG_LVL, "Adjusting %p %x -> %x\n",
434 reloc, reloc_val,
435 (unsigned int)(reloc_val + adjustment));
436
Aaron Durbin923b4d52015-09-30 16:48:26 -0500437 write_le32(reloc, reloc_val + adjustment);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500438 }
439
440 return 0;
441}
442
443static ssize_t relocate_remaining_items(void *fsp, size_t size,
444 uintptr_t new_addr, size_t fih_offset)
445{
446 EFI_FFS_FILE_HEADER *ffsfh;
447 EFI_COMMON_SECTION_HEADER *csh;
448 FSP_INFO_HEADER *fih;
449 ssize_t adjustment;
450 size_t offset;
451
452 printk(FSP_DBG_LVL, "FSP_INFO_HEADER offset is %zx\n", fih_offset);
453
454 if (fih_offset == 0) {
455 printk(BIOS_ERR, "FSP_INFO_HEADER offset is 0.\n");
456 return -1;
457 }
458
459 /* FSP_INFO_HEADER at first file in FV within first RAW section. */
460 ffsfh = relative_offset(fsp, fih_offset);
461 fih_offset += file_section_offset(ffsfh);
462 csh = relative_offset(fsp, fih_offset);
463 fih_offset += section_data_offset(csh);
464 fih = relative_offset(fsp, fih_offset);
465
466 if (guid_compare(&ffsfh->Name, &fih_guid)) {
467 printk(BIOS_ERR, "Bad FIH GUID.\n");
468 return -1;
469 }
470
Aaron Durbin923b4d52015-09-30 16:48:26 -0500471 if (read_le8(&csh->Type) != EFI_SECTION_RAW) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500472 printk(BIOS_ERR, "FIH file should have raw section: %x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500473 read_le8(&csh->Type));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500474 return -1;
475 }
476
Aaron Durbin923b4d52015-09-30 16:48:26 -0500477 if (read_le32(&fih->Signature) != FSP_SIG) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500478 printk(BIOS_ERR, "Unexpected FIH signature: %08x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500479 read_le32(&fih->Signature));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500480 }
481
Aaron Durbin923b4d52015-09-30 16:48:26 -0500482 adjustment = (intptr_t)new_addr - read_le32(&fih->ImageBase);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500483
484 /* Update ImageBase to reflect FSP's new home. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500485 write_le32(&fih->ImageBase, adjustment + read_le32(&fih->ImageBase));
Eddie Vas1df1cf92022-08-16 20:12:04 -0700486 printk(FSP_DBG_LVL, "Updated FSP InfoHdr Image Base to %x\n",
487 read_le32(&fih->ImageBase));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500488
489 /* Need to find patch table and adjust each entry. The tables
490 * following FSP_INFO_HEADER have a 32-bit signature and header
491 * length. The patch table is denoted as having a 'FSPP' signature;
492 * the table format doesn't follow the other tables. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500493 offset = fih_offset + read_le32(&fih->HeaderLength);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500494 while (offset + 2 * sizeof(uint32_t) <= size) {
495 uint32_t *table_headers;
496
497 table_headers = relative_offset(fsp, offset);
498
499 printk(FSP_DBG_LVL, "Checking offset %zx for 'FSPP'\n",
500 offset);
501
Aaron Durbin923b4d52015-09-30 16:48:26 -0500502 if (read_le32(&table_headers[0]) != FSPP_SIG) {
503 offset += read_le32(&table_headers[1]);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500504 continue;
505 }
506
507 if (relocate_patch_table(fsp, size, offset, adjustment)) {
508 printk(BIOS_ERR, "FSPP relocation failed.\n");
509 return -1;
510 }
511
512 return fih_offset;
513 }
514
515 printk(BIOS_ERR, "Could not find the FSP patch table.\n");
516 return -1;
517}
518
519static ssize_t relocate_fvh(uintptr_t new_addr, void *fsp, size_t fsp_size,
520 size_t fvh_offset, size_t *fih_offset)
521{
522 EFI_FIRMWARE_VOLUME_HEADER *fvh;
523 EFI_FFS_FILE_HEADER *ffsfh;
524 EFI_COMMON_SECTION_HEADER *csh;
525 size_t offset;
526 size_t file_offset;
527 size_t size;
528 size_t fv_length;
529
530 offset = fvh_offset;
531 fvh = relative_offset(fsp, offset);
532
Aaron Durbin923b4d52015-09-30 16:48:26 -0500533 if (read_le32(&fvh->Signature) != EFI_FVH_SIGNATURE)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500534 return -1;
535
Aaron Durbin923b4d52015-09-30 16:48:26 -0500536 fv_length = read_le64(&fvh->FvLength);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500537
538 printk(FSP_DBG_LVL, "FVH length: %zx Offset: %zx Mapping length: %zx\n",
539 fv_length, offset, fsp_size);
540
Aaron Durbin923b4d52015-09-30 16:48:26 -0500541 if (fv_length + offset > fsp_size)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500542 return -1;
543
544 /* Parse only this FV. However, the algorithm uses offsets into the
545 * entire FSP region so make size include the starting offset. */
546 size = fv_length + offset;
547
548 if (guid_compare(&fvh->FileSystemGuid, &ffs2_guid)) {
549 printk(BIOS_ERR, "FVH not an FFS2 type.\n");
550 return -1;
551 }
552
Aaron Durbin923b4d52015-09-30 16:48:26 -0500553 if (read_le16(&fvh->ExtHeaderOffset) != 0) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500554 EFI_FIRMWARE_VOLUME_EXT_HEADER *fveh;
555
Aaron Durbin923b4d52015-09-30 16:48:26 -0500556 offset += read_le16(&fvh->ExtHeaderOffset);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500557 fveh = relative_offset(fsp, offset);
558 printk(FSP_DBG_LVL, "Extended Header Offset: %zx Size: %zx\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500559 (size_t)read_le16(&fvh->ExtHeaderOffset),
560 (size_t)read_le32(&fveh->ExtHeaderSize));
561 offset += read_le32(&fveh->ExtHeaderSize);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500562 /* FFS files are 8 byte aligned after extended header. */
563 offset = ALIGN_UP(offset, 8);
564 } else {
Aaron Durbin923b4d52015-09-30 16:48:26 -0500565 offset += read_le16(&fvh->HeaderLength);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500566 }
567
568 file_offset = offset;
569 while (file_offset + sizeof(*ffsfh) < size) {
570 offset = file_offset;
571 printk(FSP_DBG_LVL, "file offset: %zx\n", file_offset);
572
573 /* First file and section should be FSP info header. */
Jeremy Compostellaeb938082023-10-24 10:09:54 -0700574 if (*fih_offset == 0)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500575 *fih_offset = file_offset;
576
577 ffsfh = relative_offset(fsp, file_offset);
578
Aaron Durbin923b4d52015-09-30 16:48:26 -0500579 printk(FSP_DBG_LVL, "file type = %x\n", read_le8(&ffsfh->Type));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500580 printk(FSP_DBG_LVL, "file attribs = %x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500581 read_le8(&ffsfh->Attributes));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500582
583 /* Exit FV relocation when empty space found */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500584 if (read_le8(&ffsfh->Type) == EFI_FV_FILETYPE_FFS_MAX)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500585 break;
586
587 /* Next file on 8 byte alignment. */
588 file_offset += ffs_file_size(ffsfh);
589 file_offset = ALIGN_UP(file_offset, 8);
590
591 /* Padding files have no section information. */
Aaron Durbin923b4d52015-09-30 16:48:26 -0500592 if (read_le8(&ffsfh->Type) == EFI_FV_FILETYPE_FFS_PAD)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500593 continue;
594
595 offset += file_section_offset(ffsfh);
596
597 while (offset + sizeof(*csh) < file_offset) {
598 size_t data_size;
599 size_t data_offset;
Eddie Vas1df1cf92022-08-16 20:12:04 -0700600 void *section_data;
601 size_t section_offset;
602 uintptr_t section_addr;
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500603
604 csh = relative_offset(fsp, offset);
605
606 printk(FSP_DBG_LVL, "section offset: %zx\n", offset);
607 printk(FSP_DBG_LVL, "section type: %x\n",
Aaron Durbin923b4d52015-09-30 16:48:26 -0500608 read_le8(&csh->Type));
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500609
610 data_size = section_data_size(csh);
611 data_offset = section_data_offset(csh);
612
613 if (data_size + data_offset + offset > file_offset) {
614 printk(BIOS_ERR, "Section exceeds FV size.\n");
615 return -1;
616 }
617
618 /*
Furquan Shaikhb0c2fe02016-05-09 12:23:01 -0700619 * The entire FSP image can be thought of as one
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500620 * program with a single link address even though there
621 * are multiple TEs linked separately. The reason is
622 * that each TE is linked for XIP. So in order to
623 * relocate the TE properly we need to form the
624 * relocated address based on the TE offset within
625 * FSP proper.
626 */
Eddie Vas1df1cf92022-08-16 20:12:04 -0700627 section_offset = offset + data_offset;
628 section_addr = new_addr + section_offset;
629 section_data = relative_offset(fsp, section_offset);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500630
Eddie Vas1df1cf92022-08-16 20:12:04 -0700631 if (read_le8(&csh->Type) == EFI_SECTION_TE) {
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500632 printk(FSP_DBG_LVL, "TE image at offset %zx\n",
Eddie Vas1df1cf92022-08-16 20:12:04 -0700633 section_offset);
634 te_relocate(section_addr, section_data);
635 } else if (read_le8(&csh->Type) == EFI_SECTION_PE32) {
636 printk(FSP_DBG_LVL, "PE32 image at offset %zx\n",
637 section_offset);
638 pe_relocate(new_addr, section_data, fsp, *fih_offset);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500639 }
640
641 offset += data_size + data_offset;
642 /* Sections are aligned to 4 bytes. */
643 offset = ALIGN_UP(offset, 4);
644 }
645 }
646
647 /* Return amount of buffer parsed: FV size. */
648 return fv_length;
649}
650
Furquan Shaikhb0c2fe02016-05-09 12:23:01 -0700651ssize_t fsp_component_relocate(uintptr_t new_addr, void *fsp, size_t size)
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500652{
653 size_t offset;
654 size_t fih_offset;
655
656 offset = 0;
657 fih_offset = 0;
658 while (offset < size) {
659 ssize_t nparsed;
660
Jeremy Compostellaeb938082023-10-24 10:09:54 -0700661 /* Relocate each FV within the FSP region. */
662 nparsed = relocate_fvh(new_addr, fsp, size, offset, &fih_offset);
Aaron Durbina5be7fa2015-09-10 22:52:27 -0500663
664 /* FV should be larger than 0 or failed to parse. */
665 if (nparsed <= 0) {
666 printk(BIOS_ERR, "FV @ offset %zx relocation failed\n",
667 offset);
668 return -1;
669 }
670
671 offset += nparsed;
672 }
673
674 return relocate_remaining_items(fsp, size, new_addr, fih_offset);
675}
Furquan Shaikhb0c2fe02016-05-09 12:23:01 -0700676
677ssize_t fsp1_1_relocate(uintptr_t new_addr, void *fsp, size_t size)
678{
679 return fsp_component_relocate(new_addr, fsp, size);
680}