]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
binman: Add a way to check for a valid ELF file
authorSimon Glass <sjg@chromium.org>
Sat, 7 Jan 2023 21:07:13 +0000 (14:07 -0700)
committerSimon Glass <sjg@chromium.org>
Wed, 18 Jan 2023 18:50:01 +0000 (11:50 -0700)
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>
tools/binman/elf.py
tools/binman/elf_test.py

index fe50bf542c3f2aaa98a0762ba821043ee32a1837..73f318b81d21bbfd7f3a8d4e8b4a965e307aa6ae 100644 (file)
@@ -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
index 75b867c2be80f3dff334d2c495ff97fa8b7340f4..082a3e1d28c6551f54423a50b904d354e2b3fda4 100644 (file)
@@ -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()