From patchwork Fri Jan 22 15:04:56 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 368647 Delivered-To: patch@linaro.org Received: by 2002:a02:a60d:0:0:0:0:0 with SMTP id c13csp1593925jam; Fri, 22 Jan 2021 07:05:39 -0800 (PST) X-Google-Smtp-Source: ABdhPJyHgLTgTObJHsMPMODTsJ7E3oaaiu8y2VnvrGqNx2cceBj/lwK2rh+Uq9L9/4VklSWu++mp X-Received: by 2002:a50:ed97:: with SMTP id h23mr3463826edr.278.1611327939596; Fri, 22 Jan 2021 07:05:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1611327939; cv=none; d=google.com; s=arc-20160816; b=eXPIYC16JoPRdy12sYMLveptw6ctjuQLGyayhwf1fKn65JDuNgT0a1vaXFkgV/pQf7 /c0vyQkL12srBx4hQW/2TOaWuxb/WGdW7P1Xzu8cjd15BMevUhJltVTaaTmR/BJ92UJ/ 30WDr/NxesQmSh1Hq0+5EKcaZXGOgfyAJ708TrWmy+RfscUr6ZtB2S0NWO4byXjYIfpV 7yNFgPj6dwnCUgiWkuD3Tsu42Knrn9X6pE7gAsdnfZ4aj2bpRVZjr4gejwOxSuV95Nh+ bMr8iClhGbcYKgpNsuLWtPKfi3fXhRe1PdUpuaK0QxcfQqAAZX5/90J4sIW/vSZf7/k2 4/oA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:message-id:date:subject:cc:to :from; bh=tqneg0+iZ0znaoixseS7jKoAjfn7xKkDMfFT9XEbO20=; b=LdIIKguFT2YeYyo0fWTMjpQaFhLx3OtzVpBlIOGPTCuIv8Fr8Z+XZo+NlrCxT2eCzA Byjzyuh9niqJkVksKNOn/Zd+yGxrnSjm1sMFu2BXkCKel75dBmt++A8GijmO1M5Y+/xS JbXdgYWSvvbD9slkj5iROL0b/xnRKNWNenRAaxY0p9f7JxLa6IwttV894TGWVjPKTv+A 98UiFXQMjq52CihIMz0KUbpSe8glBelx/nwiDQuKKBXwdIGjMjI+GHW4GIR6/OkrGsyV PSOtltGYvqxD4Kl85y91jUVUEkgABcEwBCQuk+0dcOCyDmUk5qS/3lMQjk9jKRrgFAhf nBUA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id x35si3958670ede.325.2021.01.22.07.05.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Jan 2021 07:05:39 -0800 (PST) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id F0FDD82923; Fri, 22 Jan 2021 16:05:21 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Received: by phobos.denx.de (Postfix, from userid 109) id E119E82925; Fri, 22 Jan 2021 16:05:19 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, SPF_HELO_NONE autolearn=ham autolearn_force=no version=3.4.2 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by phobos.denx.de (Postfix) with ESMTP id D37EF82791 for ; Fri, 22 Jan 2021 16:05:16 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=fail smtp.mailfrom=sughosh.ganu@linaro.org Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 9C53311D4; Fri, 22 Jan 2021 07:05:15 -0800 (PST) Received: from a076522.blr.arm.com (a076522.blr.arm.com [10.162.16.44]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id EC1003F66E; Fri, 22 Jan 2021 07:05:13 -0800 (PST) From: Sughosh Ganu To: u-boot@lists.denx.de Cc: Heinrich Schuchardt , Tom Rini , Takahiro Akashi , Sughosh Ganu Subject: [PATCH v2] mkeficapsule: Miscellaneous fixes in the utility Date: Fri, 22 Jan 2021 20:34:56 +0530 Message-Id: <20210122150456.11025-1-sughosh.ganu@linaro.org> X-Mailer: git-send-email 2.17.1 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 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.102.3 at phobos.denx.de X-Virus-Status: Clean Miscellaneous fixes in the mkeficapsule utility -- these include a few resource leak issues flagged by Coverity along with some additional code improvements suggested by Heinrich during code review. Signed-off-by: Sughosh Ganu --- Changes since V1: * Use a comparison with -1 for file descriptors before closing the fd's. * Remove the superfluous error != 0 check after a call to mmap. * Remove a printf that was used during debugging. * Return EXIT_SUCCESS and EXIT_FAILURE as return values through the exit libc call. tools/mkeficapsule.c | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) -- 2.17.1 diff --git a/tools/mkeficapsule.c b/tools/mkeficapsule.c index 270943fc90..c797d240d8 100644 --- a/tools/mkeficapsule.c +++ b/tools/mkeficapsule.c @@ -137,8 +137,8 @@ static int add_public_key(const char *pkey_file, const char *dtb_file, bool overlay) { int ret; - int srcfd = 0; - int destfd = 0; + int srcfd = -1; + int destfd = -1; void *sptr = NULL; void *dptr = NULL; off_t src_size; @@ -150,6 +150,7 @@ static int add_public_key(const char *pkey_file, const char *dtb_file, if (srcfd == -1) { fprintf(stderr, "%s: Can't open %s: %s\n", __func__, pkey_file, strerror(errno)); + ret = -1; goto err; } @@ -157,6 +158,7 @@ static int add_public_key(const char *pkey_file, const char *dtb_file, if (ret == -1) { fprintf(stderr, "%s: Can't stat %s: %s\n", __func__, pkey_file, strerror(errno)); + ret = -1; goto err; } @@ -164,9 +166,10 @@ static int add_public_key(const char *pkey_file, const char *dtb_file, /* mmap the public key esl file */ sptr = mmap(0, src_size, PROT_READ, MAP_SHARED, srcfd, 0); - if ((sptr == MAP_FAILED) || (errno != 0)) { + if (sptr == MAP_FAILED) { fprintf(stderr, "%s: Failed to mmap %s:%s\n", __func__, pkey_file, strerror(errno)); + ret = -1; goto err; } @@ -175,6 +178,7 @@ static int add_public_key(const char *pkey_file, const char *dtb_file, if (destfd == -1) { fprintf(stderr, "%s: Can't open %s: %s\n", __func__, dtb_file, strerror(errno)); + ret = -1; goto err; } @@ -189,21 +193,24 @@ static int add_public_key(const char *pkey_file, const char *dtb_file, if (ftruncate(destfd, dtb.st_size)) { fprintf(stderr, "%s: Can't expand %s: %s\n", __func__, dtb_file, strerror(errno)); - goto err;; + ret = -1; + goto err; } errno = 0; /* mmap the dtb file */ dptr = mmap(0, dtb.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, destfd, 0); - if ((dptr == MAP_FAILED) || (errno != 0)) { + if (dptr == MAP_FAILED) { fprintf(stderr, "%s: Failed to mmap %s:%s\n", __func__, dtb_file, strerror(errno)); + ret = -1; goto err; } if (fdt_check_header(dptr)) { fprintf(stderr, "%s: Invalid FDT header\n", __func__); + ret = -1; goto err; } @@ -211,6 +218,7 @@ static int add_public_key(const char *pkey_file, const char *dtb_file, if (ret) { fprintf(stderr, "%s: Cannot expand FDT: %s\n", __func__, fdt_strerror(ret)); + ret = -1; goto err; } @@ -219,10 +227,11 @@ static int add_public_key(const char *pkey_file, const char *dtb_file, if (ret < 0) { fprintf(stderr, "%s: Unable to add public key to the FDT\n", __func__); + ret = -1; goto err; } - return 0; + ret = 0; err: if (sptr) @@ -231,13 +240,13 @@ err: if (dptr) munmap(dptr, dtb.st_size); - if (srcfd >= 0) + if (srcfd != -1) close(srcfd); - if (destfd >= 0) + if (destfd != -1) close(destfd); - return -1; + return ret; } static int create_fwbin(char *path, char *bin, efi_guid_t *guid, @@ -421,26 +430,25 @@ int main(int argc, char **argv) /* need a fit image file or raw image file */ if (!file && !pkey_file && !dtb_file) { - printf("%s: %d\n", __func__, __LINE__); print_usage(); - return -1; + exit(EXIT_FAILURE); } if (pkey_file && dtb_file) { ret = add_public_key(pkey_file, dtb_file, overlay); if (ret == -1) { printf("Adding public key to the dtb failed\n"); - return -1; + exit(EXIT_FAILURE); } else { - return 0; + exit(EXIT_SUCCESS); } } if (create_fwbin(argv[optind], file, guid, index, instance) < 0) { printf("Creating firmware capsule failed\n"); - return -1; + exit(EXIT_FAILURE); } - return 0; + exit(EXIT_SUCCESS); }