Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id EDA81C4332F for ; Sat, 17 Dec 2022 16:12:40 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 6E22C8538D; Sat, 17 Dec 2022 17:12:00 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="be0uvHPo"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id C311E81A10; Sat, 17 Dec 2022 17:11:54 +0100 (CET) Received: from mail-il1-x12c.google.com (mail-il1-x12c.google.com [IPv6:2607:f8b0:4864:20::12c]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id F368D85384 for ; Sat, 17 Dec 2022 17:11:51 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sjg@chromium.org Received: by mail-il1-x12c.google.com with SMTP id x11so2739166ilo.13 for ; Sat, 17 Dec 2022 08:11:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=GXqZZhXoAUymhvTgmoReawFKlEVE0ZAkRlUfzMA1zVU=; b=be0uvHPo/tDU2hu46gF6IJ/LFzFsx32HuOUX/8jNay3MD/ybvKyyxeIwYRuRLJW11I UaKSwW6WqLFX7vN0jUbpCO5ChQux/ungdZQWmAcp2ApmLDsYZZoFR/SMBTgTKc+1K55W Vk1YZ9Ir0ZBgJQZn1Fv0SkmYCGa03QNNZ3O88= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=GXqZZhXoAUymhvTgmoReawFKlEVE0ZAkRlUfzMA1zVU=; b=jFRy0RnJBKvEiJfpn+hPvk654bpMXEeG+OMwH/AGHR/feDs5LmksYsY5kAI3XNpy3G yJ2WDlzoXSvlujAFIyB5KxMjUgEMuggH1kVU1WCWtoDleURqqOmMLXgHIpKCYyg5AYi6 +QUkcVAP2hyifQgH0dN15aWQuvtHpWElqSioEaCx3lPOXY6LCqdn4QcHRT5344H5jjwb 2OxXiJyKuB1sCnX7FS+bq+A78U6GmS/B8gIcI2862tamATBvJyCad+MPMZAt76JFIDbW kxHkleLd9zU0ge6/uIE1rSuj5KeIOQCIr6B1KXpXr10sT4g9AaaO51AWq68eai+NPSdu wzTw== X-Gm-Message-State: ANoB5pn9njJ56iyQ9gJ6Xomu7fTQQbfa1B9pOVq1bj+qLSwaBS3VFY58 3WV9mLwSvtbQJflUvaD+3nNrElPtrXJetL89 X-Google-Smtp-Source: AA0mqf41hEfae5eBnkYfD9hJfh0a+Kywt+GSabc8hr5D2RdVwa9SM8EvdG02HcQCjufplqAoONDrcQ== X-Received: by 2002:a05:6e02:10e:b0:304:b227:d2e0 with SMTP id t14-20020a056e02010e00b00304b227d2e0mr15989843ilm.25.1671293510398; Sat, 17 Dec 2022 08:11:50 -0800 (PST) Received: from sjg1.roam.corp.google.com (c-67-190-102-125.hsd1.co.comcast.net. [67.190.102.125]) by smtp.gmail.com with ESMTPSA id o5-20020a056e02092500b00302f958e71dsm1652817ilt.49.2022.12.17.08.11.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 17 Dec 2022 08:11:50 -0800 (PST) From: Simon Glass To: U-Boot Mailing List Cc: Peter Geis , Jerome Forissier , Tom Rini , Philippe Reynes , Alper Nebi Yasak , huang lin , Jeffy Chen , Simon Glass , Kever Yang , Philipp Tomsich , Quentin Schulz , Ivan Mikhaylov , Roger Quadros Subject: [PATCH v6 05/12] binman: Add a way to check for a valid ELF file Date: Sat, 17 Dec 2022 09:11:10 -0700 Message-Id: <20221217161117.61699-6-sjg@chromium.org> X-Mailer: git-send-email 2.39.0.314.g84b9a713c41-goog In-Reply-To: <20221217161117.61699-1-sjg@chromium.org> References: <20221217161117.61699-1-sjg@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.6 at phobos.denx.de X-Virus-Status: Clean 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 --- (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 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: + return False diff --git a/tools/binman/elf_test.py b/tools/binman/elf_test.py index 75b867c2be8..082a3e1d28c 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() -- 2.39.0.314.g84b9a713c41-goog