[PATCH v8 05/13] binman: Add a way to check for a valid ELF file

Quentin Schulz quentin.schulz at theobroma-systems.com
Mon Jan 2 17:26:42 CET 2023


Hi Simon,

On 12/22/22 00:07, Simon Glass wrote:
> Add a function which checks whether data is in ELF format or not. This
> will be used by binman to check this for entries.
> 
> Signed-off-by: Simon Glass <sjg at chromium.org>
> ---
> 
> (no changes since v1)
> 
>   tools/binman/elf.py      | 16 ++++++++++++++++
>   tools/binman/elf_test.py | 10 ++++++++++
>   2 files changed, 26 insertions(+)
> 
> diff --git a/tools/binman/elf.py b/tools/binman/elf.py
> index fe50bf542c3..2f4b7d1a08e 100644
> --- a/tools/binman/elf.py
> +++ b/tools/binman/elf.py
> @@ -21,6 +21,7 @@ ELF_TOOLS = True
>   try:
>       from elftools.elf.elffile import ELFFile
>       from elftools.elf.elffile import ELFError
> +    import elftools

I believe this is not needed?

>       from elftools.elf.sections import SymbolTableSection
>   except:  # pragma: no cover
>       ELF_TOOLS = False
> @@ -518,3 +519,18 @@ def read_loadable_segments(data):
>               rend = start + segment['p_filesz']
>               segments.append((i, segment['p_paddr'], data[start:rend]))
>       return segments, entry
> +
> +def is_valid(data):
> +    """Check if some binary data is a valid ELF file
> +
> +    Args:
> +        data (bytes): Bytes to check
> +
> +    Returns:
> +        bool: True if a valid Elf file, False if not
> +    """
> +    try:
> +        DecodeElf(data, 0)
> +        return True
> +    except elftools.common.exceptions.ELFError:

You can directly use ELFError here because of
from elftools.elf.elffile import ELFError
line above the one I suggested to remove?

Cheers,
Quentin


More information about the U-Boot mailing list