From mboxrd@z Thu Jan 1 00:00:00 1970 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 D3EDEC433EF for ; Fri, 14 Jan 2022 17:35:23 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 532238341A; Fri, 14 Jan 2022 18:35:21 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.b="R/Y1hQz3"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 9DC598341A; Fri, 14 Jan 2022 18:35:19 +0100 (CET) Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 02A3F833BE for ; Fri, 14 Jan 2022 18:35:16 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=pali@kernel.org Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 9CD5EB829D2; Fri, 14 Jan 2022 17:35:15 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 49CB7C36AE9; Fri, 14 Jan 2022 17:35:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1642181714; bh=exyIU1EB/a9xmRlQ9r+KwkwkYW1qv3bhmw++cZoUJbA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=R/Y1hQz3hCMQCSaozg2rOWrwsaZp+L8M681XaXT0MfUkyo1BR0UrrRvQBmOlvXMQ6 7NfwzHdJ75vEDpXNEwKJdpe2EzWqDIt3aJclbWAmNVuxQE+Ujc+N4sK09/4IhvIKDw zjsTUK9UIa6hLRKjnCEaX44w8jmtFLwpzOjtmV3ManqpDJeo0d6HfaznCvM7bSGE+F eLgCM+iV7SEjC5eVyo9hCZRQEdTXiWXKjOhAXJtTF0SdwTPszml7MTTRslZmXyV9CC SzKQegxdS6Id5ShgId7D19Q4YyLlX8JHQ/jQI/UvUsC49Kk5tPaiezQtL8i1oqk0l8 Uu+CLyCiAPLEg== Received: by pali.im (Postfix) id C6D3A7D1; Fri, 14 Jan 2022 18:35:11 +0100 (CET) From: =?UTF-8?q?Pali=20Roh=C3=A1r?= To: Simon Glass , Alexandru Gagniuc , Yann Dirson , Stefan Roese , =?UTF-8?q?Marek=20Beh=C3=BAn?= Cc: u-boot@lists.denx.de Subject: [PATCH v2] tools: mkimage: Call verify_header after writing image to disk Date: Fri, 14 Jan 2022 18:34:43 +0100 Message-Id: <20220114173443.9877-1-pali@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 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.2 at phobos.denx.de X-Virus-Status: Clean If image backend provides verify_header callback then call it after writing image to disk. This ensures that written image is correct. Signed-off-by: Pali Rohár --- tools/mkimage.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/tools/mkimage.c b/tools/mkimage.c index fbe883ce3620..d5ad0925225c 100644 --- a/tools/mkimage.c +++ b/tools/mkimage.c @@ -336,6 +336,44 @@ static void process_args(int argc, char **argv) usage("Missing output filename"); } +static void verify_image(const struct image_type_params *tparams) +{ + struct stat sbuf; + void *ptr; + int ifd; + + ifd = open(params.imagefile, O_RDONLY | O_BINARY); + if (ifd < 0) { + fprintf(stderr, "%s: Can't open %s: %s\n", + params.cmdname, params.imagefile, + strerror(errno)); + exit(EXIT_FAILURE); + } + + if (fstat(ifd, &sbuf) < 0) { + fprintf(stderr, "%s: Can't stat %s: %s\n", + params.cmdname, params.imagefile, strerror(errno)); + exit(EXIT_FAILURE); + } + params.file_size = sbuf.st_size; + + ptr = mmap(0, params.file_size, PROT_READ, MAP_SHARED, ifd, 0); + if (ptr == MAP_FAILED) { + fprintf(stderr, "%s: Can't map %s: %s\n", + params.cmdname, params.imagefile, strerror(errno)); + exit(EXIT_FAILURE); + } + + if (tparams->verify_header((unsigned char *)ptr, params.file_size, ¶ms) != 0) { + fprintf(stderr, "%s: Failed to verify header of %s\n", + params.cmdname, params.imagefile); + exit(EXIT_FAILURE); + } + + (void)munmap(ptr, params.file_size); + (void)close(ifd); +} + int main(int argc, char **argv) { int ifd = -1; @@ -698,6 +736,9 @@ int main(int argc, char **argv) exit (EXIT_FAILURE); } + if (tparams->verify_header) + verify_image(tparams); + exit (EXIT_SUCCESS); } -- 2.20.1