2 * This file is part of the coreboot project.
4 * Copyright 2015 Google Inc
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <console/console.h>
17 #include <commonlib/endian.h>
18 #include <commonlib/fsp1_1.h>
19 #include <commonlib/helpers.h>
24 #define FSP_DBG_LVL BIOS_NEVER
27 * UEFI defines everything as little endian. However, this piece of code
28 * can be integrated in a userland tool. That tool could be on a big endian
29 * machine so one needs to access the fields within UEFI structures using
30 * endian-aware accesses.
33 /* Return 0 if equal. Non-zero if not equal. */
34 static int guid_compare(const EFI_GUID
*le_guid
, const EFI_GUID
*native_guid
)
36 if (read_le32(&le_guid
->Data1
) != native_guid
->Data1
)
38 if (read_le16(&le_guid
->Data2
) != native_guid
->Data2
)
40 if (read_le16(&le_guid
->Data3
) != native_guid
->Data3
)
42 return memcmp(le_guid
->Data4
, native_guid
->Data4
,
43 ARRAY_SIZE(le_guid
->Data4
));
46 /* Provide this for symmetry when accessing UEFI fields. */
47 static inline uint8_t le8toh(uint8_t byte
)
52 static const EFI_GUID ffs2_guid
= EFI_FIRMWARE_FILE_SYSTEM2_GUID
;
53 static const EFI_GUID fih_guid
= FSP_INFO_HEADER_GUID
;
55 struct fsp_patch_table
{
57 uint16_t header_length
;
58 uint8_t header_revision
;
60 uint32_t patch_entry_num
;
61 uint32_t patch_entries
[0];
62 } __attribute__((packed
));
64 #define FSPP_SIG 0x50505346
66 static void *relative_offset(void *base
, ssize_t offset
)
70 loc
= (uintptr_t)base
;
76 static uint32_t *fspp_reloc(void *fsp
, size_t fsp_size
, uint32_t e
)
80 /* Offsets live in bits 23:0. */
81 offset
= e
& 0xffffff;
83 /* If bit 31 is set then the offset is considered a negative value
84 * relative to the end of the image using 16MiB as the offset's
87 offset
= fsp_size
- (16 * MiB
- offset
);
89 /* Determine if offset falls within fsp_size for a 32 bit relocation. */
90 if (offset
> fsp_size
- sizeof(uint32_t))
93 return relative_offset(fsp
, offset
);
96 static int reloc_type(uint16_t reloc_entry
)
98 /* Reloc type in upper 4 bits */
99 return reloc_entry
>> 12;
102 static size_t reloc_offset(uint16_t reloc_entry
)
104 /* Offsets are in low 12 bits. */
105 return reloc_entry
& ((1 << 12) - 1);
108 static int te_relocate(uintptr_t new_addr
, void *te
)
110 EFI_TE_IMAGE_HEADER
*teih
;
111 EFI_IMAGE_DATA_DIRECTORY
*relocd
;
112 EFI_IMAGE_BASE_RELOCATION
*relocb
;
113 uintptr_t image_base
;
117 size_t relocd_offset
;
123 if (read_le16(&teih
->Signature
) != EFI_TE_IMAGE_HEADER_SIGNATURE
) {
124 printk(BIOS_ERR
, "TE Signature mismatch: %x vs %x\n",
125 read_le16(&teih
->Signature
),
126 EFI_TE_IMAGE_HEADER_SIGNATURE
);
131 * A TE image is created by converting a PE file. Because of this
132 * the offsets within the headers are off. In order to calculate
133 * the correct releative offets one needs to subtract fixup_offset
134 * from the encoded offets. Similarly, the linked address of the
135 * program is found by adding the fixup_offset to the ImageBase.
137 fixup_offset
= read_le16(&teih
->StrippedSize
);
138 fixup_offset
-= sizeof(EFI_TE_IMAGE_HEADER
);
139 /* Keep track of a base that is correctly adjusted so that offsets
140 * can be used directly. */
142 te_base
-= fixup_offset
;
144 image_base
= read_le64(&teih
->ImageBase
);
145 adj
= new_addr
- (image_base
+ fixup_offset
);
147 printk(FSP_DBG_LVL
, "TE Image %p -> %p adjust value: %x\n",
148 (void *)image_base
, (void *)new_addr
, adj
);
150 /* Adjust ImageBase for consistency. */
151 write_le64(&teih
->ImageBase
, (uint32_t)(image_base
+ adj
));
153 relocd
= &teih
->DataDirectory
[EFI_TE_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
156 /* Though the field name is VirtualAddress it's actually relative to
157 * the beginning of the image which is linked at ImageBase. */
158 relocb
= relative_offset(te
,
159 read_le32(&relocd
->VirtualAddress
) - fixup_offset
);
160 while (relocd_offset
< read_le32(&relocd
->Size
)) {
161 size_t rva_offset
= read_le32(&relocb
->VirtualAddress
);
163 printk(FSP_DBG_LVL
, "Relocs for RVA offset %zx\n", rva_offset
);
164 num_relocs
= read_le32(&relocb
->SizeOfBlock
) - sizeof(*relocb
);
165 num_relocs
/= sizeof(uint16_t);
166 reloc
= relative_offset(relocb
, sizeof(*relocb
));
168 printk(FSP_DBG_LVL
, "Num relocs in block: %zx\n", num_relocs
);
170 while (num_relocs
> 0) {
171 uint16_t reloc_val
= read_le16(reloc
);
172 int type
= reloc_type(reloc_val
);
173 size_t offset
= reloc_offset(reloc_val
);
175 printk(FSP_DBG_LVL
, "reloc type %x offset %zx\n",
178 if (type
== EFI_IMAGE_REL_BASED_HIGHLOW
) {
179 uint32_t *reloc_addr
;
182 offset
+= rva_offset
;
183 reloc_addr
= (void *)&te_base
[offset
];
184 val
= read_le32(reloc_addr
);
186 printk(FSP_DBG_LVL
, "Adjusting %p %x -> %x\n",
187 reloc_addr
, val
, val
+ adj
);
188 write_le32(reloc_addr
, val
+ adj
);
189 } else if (type
!= EFI_IMAGE_REL_BASED_ABSOLUTE
) {
190 printk(BIOS_ERR
, "Unknown reloc type: %x\n",
198 /* Track consumption of relocation directory contents. */
199 relocd_offset
+= read_le32(&relocb
->SizeOfBlock
);
200 /* Get next relocation block to process. */
201 relocb
= relative_offset(relocb
,
202 read_le32(&relocb
->SizeOfBlock
));
208 static size_t csh_size(const EFI_COMMON_SECTION_HEADER
*csh
)
212 /* Unpack the array into a type that can be used. */
214 size
|= read_le8(&csh
->Size
[0]) << 0;
215 size
|= read_le8(&csh
->Size
[1]) << 8;
216 size
|= read_le8(&csh
->Size
[2]) << 16;
221 static size_t section_data_offset(const EFI_COMMON_SECTION_HEADER
*csh
)
223 if (csh_size(csh
) == 0x00ffffff)
224 return sizeof(EFI_COMMON_SECTION_HEADER2
);
226 return sizeof(EFI_COMMON_SECTION_HEADER
);
229 static size_t section_data_size(const EFI_COMMON_SECTION_HEADER
*csh
)
233 if (csh_size(csh
) == 0x00ffffff)
234 section_size
= read_le32(&SECTION2_SIZE(csh
));
236 section_size
= csh_size(csh
);
238 return section_size
- section_data_offset(csh
);
241 static size_t file_section_offset(const EFI_FFS_FILE_HEADER
*ffsfh
)
243 if (IS_FFS_FILE2(ffsfh
))
244 return sizeof(EFI_FFS_FILE_HEADER2
);
246 return sizeof(EFI_FFS_FILE_HEADER
);
249 static size_t ffs_file_size(const EFI_FFS_FILE_HEADER
*ffsfh
)
253 if (IS_FFS_FILE2(ffsfh
))
254 size
= read_le32(&FFS_FILE2_SIZE(ffsfh
));
256 size
= read_le8(&ffsfh
->Size
[0]) << 0;
257 size
|= read_le8(&ffsfh
->Size
[1]) << 8;
258 size
|= read_le8(&ffsfh
->Size
[2]) << 16;
263 static int relocate_patch_table(void *fsp
, size_t size
, size_t offset
,
266 struct fsp_patch_table
*table
;
270 table
= relative_offset(fsp
, offset
);
272 if ((offset
+ sizeof(*table
) > size
) ||
273 (read_le16(&table
->header_length
) + offset
) > size
) {
274 printk(BIOS_ERR
, "FSPP not entirely contained in region.\n");
278 num_entries
= read_le32(&table
->patch_entry_num
);
279 printk(FSP_DBG_LVL
, "FSPP relocs: %zx\n", num_entries
);
281 for (num
= 0; num
< num_entries
; num
++) {
285 reloc
= fspp_reloc(fsp
, size
,
286 read_le32(&table
->patch_entries
[num
]));
289 printk(BIOS_ERR
, "Ignoring FSPP entry: %x\n",
290 read_le32(&table
->patch_entries
[num
]));
294 reloc_val
= read_le32(reloc
);
295 printk(FSP_DBG_LVL
, "Adjusting %p %x -> %x\n",
297 (unsigned int)(reloc_val
+ adjustment
));
299 write_le32(reloc
, reloc_val
+ adjustment
);
305 static ssize_t
relocate_remaining_items(void *fsp
, size_t size
,
306 uintptr_t new_addr
, size_t fih_offset
)
308 EFI_FFS_FILE_HEADER
*ffsfh
;
309 EFI_COMMON_SECTION_HEADER
*csh
;
310 FSP_INFO_HEADER
*fih
;
314 printk(FSP_DBG_LVL
, "FSP_INFO_HEADER offset is %zx\n", fih_offset
);
316 if (fih_offset
== 0) {
317 printk(BIOS_ERR
, "FSP_INFO_HEADER offset is 0.\n");
321 /* FSP_INFO_HEADER at first file in FV within first RAW section. */
322 ffsfh
= relative_offset(fsp
, fih_offset
);
323 fih_offset
+= file_section_offset(ffsfh
);
324 csh
= relative_offset(fsp
, fih_offset
);
325 fih_offset
+= section_data_offset(csh
);
326 fih
= relative_offset(fsp
, fih_offset
);
328 if (guid_compare(&ffsfh
->Name
, &fih_guid
)) {
329 printk(BIOS_ERR
, "Bad FIH GUID.\n");
333 if (read_le8(&csh
->Type
) != EFI_SECTION_RAW
) {
334 printk(BIOS_ERR
, "FIH file should have raw section: %x\n",
335 read_le8(&csh
->Type
));
339 if (read_le32(&fih
->Signature
) != FSP_SIG
) {
340 printk(BIOS_ERR
, "Unexpected FIH signature: %08x\n",
341 read_le32(&fih
->Signature
));
345 adjustment
= (intptr_t)new_addr
- read_le32(&fih
->ImageBase
);
347 /* Update ImageBase to reflect FSP's new home. */
348 write_le32(&fih
->ImageBase
, adjustment
+ read_le32(&fih
->ImageBase
));
350 /* Need to find patch table and adjust each entry. The tables
351 * following FSP_INFO_HEADER have a 32-bit signature and header
352 * length. The patch table is denoted as having a 'FSPP' signature;
353 * the table format doesn't follow the other tables. */
354 offset
= fih_offset
+ read_le32(&fih
->HeaderLength
);
355 while (offset
+ 2 * sizeof(uint32_t) <= size
) {
356 uint32_t *table_headers
;
358 table_headers
= relative_offset(fsp
, offset
);
360 printk(FSP_DBG_LVL
, "Checking offset %zx for 'FSPP'\n",
363 if (read_le32(&table_headers
[0]) != FSPP_SIG
) {
364 offset
+= read_le32(&table_headers
[1]);
368 if (relocate_patch_table(fsp
, size
, offset
, adjustment
)) {
369 printk(BIOS_ERR
, "FSPP relocation failed.\n");
376 printk(BIOS_ERR
, "Could not find the FSP patch table.\n");
380 static ssize_t
relocate_fvh(uintptr_t new_addr
, void *fsp
, size_t fsp_size
,
381 size_t fvh_offset
, size_t *fih_offset
)
383 EFI_FIRMWARE_VOLUME_HEADER
*fvh
;
384 EFI_FFS_FILE_HEADER
*ffsfh
;
385 EFI_COMMON_SECTION_HEADER
*csh
;
392 fvh
= relative_offset(fsp
, offset
);
394 if (read_le32(&fvh
->Signature
) != EFI_FVH_SIGNATURE
)
397 fv_length
= read_le64(&fvh
->FvLength
);
399 printk(FSP_DBG_LVL
, "FVH length: %zx Offset: %zx Mapping length: %zx\n",
400 fv_length
, offset
, fsp_size
);
402 if (fv_length
+ offset
> fsp_size
)
405 /* Parse only this FV. However, the algorithm uses offsets into the
406 * entire FSP region so make size include the starting offset. */
407 size
= fv_length
+ offset
;
409 if (guid_compare(&fvh
->FileSystemGuid
, &ffs2_guid
)) {
410 printk(BIOS_ERR
, "FVH not an FFS2 type.\n");
414 if (read_le16(&fvh
->ExtHeaderOffset
) != 0) {
415 EFI_FIRMWARE_VOLUME_EXT_HEADER
*fveh
;
417 offset
+= read_le16(&fvh
->ExtHeaderOffset
);
418 fveh
= relative_offset(fsp
, offset
);
419 printk(FSP_DBG_LVL
, "Extended Header Offset: %zx Size: %zx\n",
420 (size_t)read_le16(&fvh
->ExtHeaderOffset
),
421 (size_t)read_le32(&fveh
->ExtHeaderSize
));
422 offset
+= read_le32(&fveh
->ExtHeaderSize
);
423 /* FFS files are 8 byte aligned after extended header. */
424 offset
= ALIGN_UP(offset
, 8);
426 offset
+= read_le16(&fvh
->HeaderLength
);
429 file_offset
= offset
;
430 while (file_offset
+ sizeof(*ffsfh
) < size
) {
431 offset
= file_offset
;
432 printk(FSP_DBG_LVL
, "file offset: %zx\n", file_offset
);
434 /* First file and section should be FSP info header. */
435 if (fih_offset
!= NULL
&& *fih_offset
== 0)
436 *fih_offset
= file_offset
;
438 ffsfh
= relative_offset(fsp
, file_offset
);
440 printk(FSP_DBG_LVL
, "file type = %x\n", read_le8(&ffsfh
->Type
));
441 printk(FSP_DBG_LVL
, "file attribs = %x\n",
442 read_le8(&ffsfh
->Attributes
));
444 /* Exit FV relocation when empty space found */
445 if (read_le8(&ffsfh
->Type
) == EFI_FV_FILETYPE_FFS_MAX
)
448 /* Next file on 8 byte alignment. */
449 file_offset
+= ffs_file_size(ffsfh
);
450 file_offset
= ALIGN_UP(file_offset
, 8);
452 /* Padding files have no section information. */
453 if (read_le8(&ffsfh
->Type
) == EFI_FV_FILETYPE_FFS_PAD
)
456 offset
+= file_section_offset(ffsfh
);
458 while (offset
+ sizeof(*csh
) < file_offset
) {
462 csh
= relative_offset(fsp
, offset
);
464 printk(FSP_DBG_LVL
, "section offset: %zx\n", offset
);
465 printk(FSP_DBG_LVL
, "section type: %x\n",
466 read_le8(&csh
->Type
));
468 data_size
= section_data_size(csh
);
469 data_offset
= section_data_offset(csh
);
471 if (data_size
+ data_offset
+ offset
> file_offset
) {
472 printk(BIOS_ERR
, "Section exceeds FV size.\n");
477 * The entire FSP 1.1 image can be thought of as one
478 * program with a single link address even though there
479 * are multiple TEs linked separately. The reason is
480 * that each TE is linked for XIP. So in order to
481 * relocate the TE properly we need to form the
482 * relocated address based on the TE offset within
485 if (read_le8(&csh
->Type
) == EFI_SECTION_TE
) {
487 size_t te_offset
= offset
+ data_offset
;
488 uintptr_t te_addr
= new_addr
+ te_offset
;
490 printk(FSP_DBG_LVL
, "TE image at offset %zx\n",
492 te
= relative_offset(fsp
, te_offset
);
493 te_relocate(te_addr
, te
);
496 offset
+= data_size
+ data_offset
;
497 /* Sections are aligned to 4 bytes. */
498 offset
= ALIGN_UP(offset
, 4);
502 /* Return amount of buffer parsed: FV size. */
506 ssize_t
fsp1_1_relocate(uintptr_t new_addr
, void *fsp
, size_t size
)
513 while (offset
< size
) {
516 /* Relocate each FV within the FSP region. The FSP_INFO_HEADER
517 * should only be located in the first FV. */
519 nparsed
= relocate_fvh(new_addr
, fsp
, size
, offset
,
522 nparsed
= relocate_fvh(new_addr
, fsp
, size
, offset
,
525 /* FV should be larger than 0 or failed to parse. */
527 printk(BIOS_ERR
, "FV @ offset %zx relocation failed\n",
535 return relocate_remaining_items(fsp
, size
, new_addr
, fih_offset
);