util/cbfstool/amdcompress: Bail out on invalid ELF

Ensure that only one PT_LOAD segment is inside the input ELF as
the tool only expects and support one PT_LOAD segment. Instead of silently
discarding all other PT_LOAD segments than the first throw an error.

Change-Id: I90cfc8b9dd0b5e8060880790e5ff0ce73843943b
Signed-off-by: Patrick Rudolph <patrick.rudolph@9elements.com>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/87315
Reviewed-by: Matt DeVillier <matt.devillier@gmail.com>
Reviewed-by: Maximilian Brune <maximilian.brune@9elements.com>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
This commit is contained in:
Patrick Rudolph 2025-04-15 09:37:28 +02:00 committed by Maximilian Brune
commit 69888bc7fc
2 changed files with 20 additions and 3 deletions

View file

@ -137,8 +137,9 @@ SECTIONS {
* .text and .reset breaks the integrity, as .bss or .data might get modified.
*
* Enable ENV_SEPARATE_DATA_AND_BSS for AMD's bootblock and mark as
* data_segment to place it in the second PT_LOAD area. AMDCOMPRESS will ignore
* the second PT_LOAD area and discard it.
* data_segment to place it in the second PT_LOAD area. The second PT_LOAD area
* must be removed using objcopy since AMDCOMPRESS doesn't support multiple
* PT_LOAD segments.
*/
. = BOOTBLOCK_ADDR & ~(16 - 1);
.data . : {

View file

@ -75,12 +75,28 @@ static int do_file(char *name, size_t *size, int oflag)
}
static int parse_elf_to_xip_ram(const struct buffer *input,
struct buffer *output)
struct buffer *output)
{
struct parsed_elf pelf;
const Elf64_Phdr *phdr;
if (parse_elf(input, &pelf, ELF_PARSE_ALL))
return 1;
/* Validate ELF. Expect only on PT_LOAD program header at index 0. */
for (int i = 0; i < pelf.ehdr.e_phnum; i++) {
phdr = &pelf.phdr[i];
/* Expect PT_LOAD on first entry */
if (i == 0 && (phdr->p_type != PT_LOAD || !phdr->p_memsz)) {
printf("\tProgram header #0 is invalid.\n");
}
/* Expect not to find more PT_LOAD program headers */
if (i > 0 && phdr->p_type == PT_LOAD && phdr->p_memsz) {
printf("\tMultiple program headers found, but only one is supported!\n");
return 1;
}
}
if (buffer_create(output, pelf.phdr->p_filesz, "") != 0)
return 1;