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 6E934C433EF for ; Thu, 13 Jan 2022 14:43:02 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 50F2681B4B; Thu, 13 Jan 2022 15:43:00 +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="H8IEmCug"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id B721180031; Thu, 13 Jan 2022 15:42:58 +0100 (CET) Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) (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 205F680031 for ; Thu, 13 Jan 2022 15:42:56 +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 dfw.source.kernel.org (Postfix) with ESMTPS id 5F20761C60; Thu, 13 Jan 2022 14:42:54 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9FE90C36AE9; Thu, 13 Jan 2022 14:42:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1642084973; bh=aSfSEJh3Fzp50cE/stKN87u5XkabfLBVCJbJStCGvMw=; h=From:To:Cc:Subject:Date:From; b=H8IEmCuggRdP5JRSWh1T1k7mZFf6oeZV3zRby0FJ6OuHskkJZrcCHa1ZMYHIjP53j E9/EAQKtmqf5/NjsZ/8NZ3tqeQcPl5mD4U1L2qaphn/sbt8MUgFTyg9YXfAwNU/l7/ O/kqCuZe5/drTTd0KwjLW9FkVYLv6IgwhQDIpDg1dD+2p+PamIwMP1RegZbUvCekHd vsE2kfiuCwJpFC3PRE34/7jY98wRSRJWP86MFm3eIQh2NyQo0Df3sue9PyLjsCIw/X 9FvazKi+g96DwCT38cOjiZYEXdmQKr3nYvw1nzsYpqTsJlJ/aNCFnwFMBHzEvYpKzi z+HtNOqD+Z3Zg== Received: by pali.im (Postfix) id 26D8E778; Thu, 13 Jan 2022 15:42:51 +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] mkimage: Call verify_header after writing image to disk Date: Thu, 13 Jan 2022 15:42:22 +0100 Message-Id: <20220113144222.25162-1-pali@kernel.org> X-Mailer: git-send-email 2.20.1 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 | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tools/mkimage.c b/tools/mkimage.c index fbe883ce3620..2d282630787c 100644 --- a/tools/mkimage.c +++ b/tools/mkimage.c @@ -698,6 +698,40 @@ int main(int argc, char **argv) exit (EXIT_FAILURE); } + if (tparams->verify_header) { + 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; + + map_len = sbuf.st_size; + ptr = mmap(0, sbuf.st_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, sbuf.st_size, ¶ms) != 0) { + fprintf(stderr, "%s: Failed to verify header of %s\n", + params.cmdname, params.imagefile); + exit(EXIT_FAILURE); + } + + (void)munmap((void *)ptr, map_len); + close(ifd); + } + exit (EXIT_SUCCESS); } -- 2.20.1