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 318CBC4332F for ; Wed, 21 Dec 2022 23:08:36 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id CC1E285351; Thu, 22 Dec 2022 00:08:03 +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="kQtRC/Je"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id C056285282; Thu, 22 Dec 2022 00:07:45 +0100 (CET) Received: from mail-io1-xd33.google.com (mail-io1-xd33.google.com [IPv6:2607:f8b0:4864:20::d33]) (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 2ECC085282 for ; Thu, 22 Dec 2022 00:07:42 +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-io1-xd33.google.com with SMTP id z144so192954iof.3 for ; Wed, 21 Dec 2022 15:07:42 -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=kQtRC/JeB3BCYHQcRMNuHVM7xsPolM8ogYFRyf0kHz4gK8SFKrXjKu40B/Zo+3jr9X Vn/vvF8/o2KqYMpue+ezDKUCz58pD5Yi0/G7WoGJRemrI9tUHpa3Bi+P6k/M8bG7YgCx 2SfjyFXcinQeCmcoX76bqkK8mSvSwyqI3pezk= 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=Q0PyYLG/WrMsoku3VARSkInq74G2mQvZ6m1oCyHB/9vgemXavYFbWmt+6NRkFf9Lb8 tpZOwlPE9RDJtQcscUY0t4YrF7GhcvVqqVXQV90GgBonCIW90mNJ4yomqj5/feQLU0vV yzjsKhROzi8Ngh0bBBS+ov7CyIxFJmF8gELeL2lJNiKWX+J6kORj0VG2CtrBd3HuSXYT B7k/pcv+4lMPakTwFAYhdss/KROtawf13IHTDkwm4xTk6JVDiI4teGkTJkwZir1CjmeY GtQxQKRup11kd9LbHjsXE/uykm4pWJFLQ/B2+2ER8iPafmtIqe70MIi87heFUgRoAL7Q H0Fg== X-Gm-Message-State: AFqh2kpOSwhWsxBxbbklGug8HnKTDavFP1JMAgXYryS6A+QplGKFY8OI tUvpY6KnJx/ylCew3TZIjqSg3CKa/3aJJl+L X-Google-Smtp-Source: AMrXdXvt4HbmvFy92U9lhRnAJqcTDDHT44Ab2xVbCQjge/quXuxfTo138PrXFOB6COdtWAawWHEFRQ== X-Received: by 2002:a5d:8712:0:b0:6df:8fef:5a9b with SMTP id u18-20020a5d8712000000b006df8fef5a9bmr2217317iom.14.1671664061668; Wed, 21 Dec 2022 15:07:41 -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 c2-20020a5ea802000000b006a175fe334dsm6499433ioa.1.2022.12.21.15.07.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Dec 2022 15:07:41 -0800 (PST) From: Simon Glass To: U-Boot Mailing List Cc: Tom Rini , huang lin , Jeffy Chen , Simon Glass , Kever Yang , Philipp Tomsich , Ivan Mikhaylov , Quentin Schulz , Roger Quadros , Philippe Reynes , Alper Nebi Yasak , Jerome Forissier , Peter Geis Subject: [PATCH v8 05/13] binman: Add a way to check for a valid ELF file Date: Wed, 21 Dec 2022 16:07:18 -0700 Message-Id: <20221221230726.638740-6-sjg@chromium.org> X-Mailer: git-send-email 2.39.0.314.g84b9a713c41-goog In-Reply-To: <20221221230726.638740-1-sjg@chromium.org> References: <20221221230726.638740-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