diff options
author | Simon Glass <sjg@chromium.org> | 2023-01-07 14:07:13 -0700 |
---|---|---|
committer | Simon Glass <sjg@chromium.org> | 2023-01-18 11:50:01 -0700 |
commit | 39f4a85bb2dd5915ebc86921c34da26faa278fec (patch) | |
tree | a0d4f87155ef4100a4ff6a3e0fca16eddae2e35e | |
parent | c8c9f3108a7b8c3ff391f60b184fa372ae4f32f2 (diff) | |
download | u-boot-39f4a85bb2dd5915ebc86921c34da26faa278fec.zip u-boot-39f4a85bb2dd5915ebc86921c34da26faa278fec.tar.gz u-boot-39f4a85bb2dd5915ebc86921c34da26faa278fec.tar.bz2 |
binman: Add a way to check for a valid ELF file
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@chromium.org>
-rw-r--r-- | tools/binman/elf.py | 15 | ||||
-rw-r--r-- | tools/binman/elf_test.py | 10 |
2 files changed, 25 insertions, 0 deletions
diff --git a/tools/binman/elf.py b/tools/binman/elf.py index fe50bf5..73f318b 100644 --- a/tools/binman/elf.py +++ b/tools/binman/elf.py @@ -518,3 +518,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 ELFError: + return False diff --git a/tools/binman/elf_test.py b/tools/binman/elf_test.py index 75b867c..082a3e1 100644 --- a/tools/binman/elf_test.py +++ b/tools/binman/elf_test.py @@ -348,6 +348,16 @@ class TestElf(unittest.TestCase): finally: elf.ELF_TOOLS = old_val + def test_is_valid(self): + """Test is_valid()""" + self.assertEqual(False, elf.is_valid(b'')) + self.assertEqual(False, elf.is_valid(b'1234')) + + fname = self.ElfTestFile('elf_sections') + data = tools.read_file(fname) + self.assertEqual(True, elf.is_valid(data)) + self.assertEqual(False, elf.is_valid(data[4:])) + if __name__ == '__main__': unittest.main() |