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 5A461C46467 for ; Sat, 7 Jan 2023 21:08:52 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 3D96485565; Sat, 7 Jan 2023 22:08:18 +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="TPNEgWHM"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 6DA4385413; Sat, 7 Jan 2023 22:07:46 +0100 (CET) Received: from mail-il1-x12d.google.com (mail-il1-x12d.google.com [IPv6:2607:f8b0:4864:20::12d]) (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 53ACD85377 for ; Sat, 7 Jan 2023 22:07:37 +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-x12d.google.com with SMTP id m15so2807957ilj.8 for ; Sat, 07 Jan 2023 13:07:37 -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=vusI24oicEwrjXonNdE919oo/YMU/Ct184598ym7W14=; b=TPNEgWHMmWujaAxHpu/RgyGDRrJbFR0dXdeWMrVH4ihIk0g4cboy8kkPxfC7ObxYLy RQGIIhcyxTv6ubRtOeMT+nzGocSRiDZjb0+tID+5FBzaTNq5gBQu+yUHfjhfqrOgBy4y nK066263IMFkcogRWjR8onaZfjN0Ih0lRGgQo= 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=vusI24oicEwrjXonNdE919oo/YMU/Ct184598ym7W14=; b=8OAPXQBjCldBfzoRPtVqXZtWVNPvk93Khf29E1NCSoidPgze8bYkb6l6ki60gGNMJT fYf/+XPV03y8fCNi94hkXwisnoICioKXFkatsFkvAyy95TFLZT2SLnq/3K13QPU5qLhj 2gNXTyjSBgb9dGui4gCd3l//iW1QI2Jl2iCyif5pjQQ7degc+kIgWhaDs3/5t3f03E9s xF93zAJ4LSwCJAZ5T/CBgkLHJ+1S4sAvG1qypuvGkwHK2JJrGFLDRgwE2e84oMhN5uPJ EZ4C8zK4/S/epbJ6sPRP5NkvHD9QtWX8lQIZ2jicmelmfY/+Q+VObhHWKNlBHaxUQwEt zO7g== X-Gm-Message-State: AFqh2kreDbuNuzNA271pd8kVkW+OzcpzzqvlVPInK4ipdyPcmw0F2lp2 GjZa/MDTYKd9iq5WR/I6srwXG3X+nx1AoHmr X-Google-Smtp-Source: AMrXdXuFNZ2KpBigvia0WIo1h2G684Rwge0wayMwAgdwicv2iz8LPTJIHDRbaF6nb7sxCTMu4HH4Wg== X-Received: by 2002:a92:c90b:0:b0:304:d0b7:e56c with SMTP id t11-20020a92c90b000000b00304d0b7e56cmr41247513ilp.14.1673125656863; Sat, 07 Jan 2023 13:07:36 -0800 (PST) Received: from sjg1.roam.corp.google.com (c-71-56-217-229.hsd1.co.comcast.net. [71.56.217.229]) by smtp.gmail.com with ESMTPSA id l10-20020a92d94a000000b0030c00897497sm1465825ilq.19.2023.01.07.13.07.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Jan 2023 13:07:36 -0800 (PST) From: Simon Glass To: U-Boot Mailing List Cc: Roger Quadros , Tom Rini , Alper Nebi Yasak , Peter Geis , Jerome Forissier , huang lin , Jeffy Chen , Simon Glass , Kever Yang , Philipp Tomsich , Ivan Mikhaylov , Quentin Schulz , Philippe Reynes Subject: [PATCH v9 06/14] binman: Add a way to check for a valid ELF file Date: Sat, 7 Jan 2023 14:07:13 -0700 Message-Id: <20230107210721.478062-7-sjg@chromium.org> X-Mailer: git-send-email 2.39.0.314.g84b9a713c41-goog In-Reply-To: <20230107210721.478062-1-sjg@chromium.org> References: <20230107210721.478062-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 --- Changes in v9: - Drop unnecessary import and fully qualified symbol tools/binman/elf.py | 15 +++++++++++++++ tools/binman/elf_test.py | 10 ++++++++++ 2 files changed, 25 insertions(+) diff --git a/tools/binman/elf.py b/tools/binman/elf.py index fe50bf542c3..73f318b81d2 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 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