From patchwork Thu Feb 17 15:11:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masami Hiramatsu X-Patchwork-Id: 543383 Delivered-To: patch@linaro.org Received: by 2002:ac0:e142:0:0:0:0:0 with SMTP id r2csp448108imn; Thu, 17 Feb 2022 07:12:01 -0800 (PST) X-Google-Smtp-Source: ABdhPJyKgaOghVNYKGm8VYi8njfdKPAfU+szlvOnAeqxzNBuYB7YdjgJv1GXikwk3Qeg86Zs1wx/ X-Received: by 2002:a05:6402:3591:b0:412:95d3:4aed with SMTP id y17-20020a056402359100b0041295d34aedmr3147780edc.450.1645110720929; Thu, 17 Feb 2022 07:12:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645110720; cv=none; d=google.com; s=arc-20160816; b=r5VFZpq+j2tVb6LaPbpoIgvQFwOY50ccKVEA6K6toO1vvxNlGLloupf+jrGu4Ykqoj uEj53zwJfS04Fy6uHmpsSgc04Znn/uyLX74AJI9v0ygIMVjbwOdVJ9Z1yD/VIK4QylpW oKUis29e9xNHwLzx3Yz2G7VUCWoYmk+cwzZLNPEkFbxZ16fQp6bnCr/jvE/nGhxsn/Ku Q3ZEwxH6RyCjCk6WxpyI0Xz+eeH1ScedZxAAzZavfDDnSyjX5NtMosAPMp1hS35l1tab HPZvtE6DOk9aOkIXnFPp97H82qb+wFdh2lGtz9Q0N71Kg6cK5AWM95QYC2sBxX+Gu7eF TZvQ== 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:content-transfer-encoding :mime-version:user-agent:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=vtGlBQNtxMvJoPrV8wtFzpMuQ3OYwL2ZLTqZX+f3A2k=; b=kFTbrbqN0AFfuELBhLiTb7wb8iKNodkon6K5SL2UJV6kH+A2E27DHhOJdclCF9JXf3 U5kzFuk7HuobyzZQcg8T5H//XuoIMFUqbGKPRDEYTo0ElHljv1/P8d2crhAsi0zDt3rq ZJC01wAEwUaVNT30pSZSM0OoCwUohMF3cqscks3OKlQLVIrvceutVN55HPEnAR1oamAy 4+mQEQdacfiSbM5MQbwN1zmaXz74VSbYU9ndD349uaEoGMBX31LcySXcN8BHB0ktxI+P UlFQmFFgf0TdjBXVEr9iXS9RaRKZYcXBBeB0WmdnEjMoV3876MWNLYrWdu2ldqlrJ9wi FVXQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=OZD0qk80; 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=pass (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 j23si2074960eje.624.2022.02.17.07.12.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Feb 2022 07:12:00 -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; dkim=pass header.i=@linaro.org header.s=google header.b=OZD0qk80; 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=pass (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 7406083BDC; Thu, 17 Feb 2022 16:11:58 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.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=linaro.org header.i=@linaro.org header.b="OZD0qk80"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id E002B83BA1; Thu, 17 Feb 2022 16:11:50 +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=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-pf1-x42c.google.com (mail-pf1-x42c.google.com [IPv6:2607:f8b0:4864:20::42c]) (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 16E8B83B8D for ; Thu, 17 Feb 2022 16:11:44 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=masami.hiramatsu@linaro.org Received: by mail-pf1-x42c.google.com with SMTP id x18so1267830pfh.5 for ; Thu, 17 Feb 2022 07:11:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :user-agent:mime-version:content-transfer-encoding; bh=vtGlBQNtxMvJoPrV8wtFzpMuQ3OYwL2ZLTqZX+f3A2k=; b=OZD0qk80y87BVv4kjwWN7yvLXWu545ZjQbq/cU/X7s0KRCoyJFnoQie2OOQQO2IEq1 uLKJiHxbwHsInUtvjAYeHr2NuWVdAHHSLWlnM1u+fvKqO0USszKIkVj5+7WoeUedrQyD bhnE8jEPw+VWMd+9/lHZLSzBu3VMmcRgCI7nYBZ1gJJ/0+bPpcv8q/5wBleSMxWMMSIm EY7clTJg29dld6eLWNbP5uVzoTlA2MDv0QnAei1bkaLyUhWy7kpBLo9smdCYBjdMbgA9 gnVYf9DZyHfI9M2eQTWGh/uuIrIeJTjL29TRkoshR0D0p/T/8EZl8zBkgEe/Pb1aj+d4 0Klg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=vtGlBQNtxMvJoPrV8wtFzpMuQ3OYwL2ZLTqZX+f3A2k=; b=z3MCFjm1UCZc4Td4E6WDrVaympBoQj/J2eK3shVsnkgetfTuFqnwz2xxqDBFPgvSwf Ym6y/aFX7MG5OkLJACLfHvodZyIhTqd0wWuST9EV/IosUfxf6Yk/jFCsjLaHnCBtlvXO sZ3XXgj4Fmbgam6+Z1SlG5PcDvLoZvrRZRHLlmgOpYUDDTkBpBUV3M5PMf1psQ1KYOLw AkLuokijk9ABwnHlADx1k0iyYr+frffhMtDFdlrzZ2dKjnktgg66L/tS+Pe0j9hbLGnL VwzbTyZo/30oCE6wiZMl9JzwP9mCOaOguDvas9ZR+w8SLM6oclAvm6E6MwFfJAPPg3t3 LIkw== X-Gm-Message-State: AOAM532rq/OZhf50tXQwr5s/GBRbFKSnW7BX3BZqvnXedVJ34N1de5JN elz6ncyV0NmWka5dCPz5CAz8yA4xRPmzgw== X-Received: by 2002:a63:cd49:0:b0:373:5fbb:c790 with SMTP id a9-20020a63cd49000000b003735fbbc790mr2819771pgj.206.1645110701066; Thu, 17 Feb 2022 07:11:41 -0800 (PST) Received: from localhost.localdomain (113x37x226x201.ap113.ftth.ucom.ne.jp. [113.37.226.201]) by smtp.gmail.com with ESMTPSA id x1sm8445149pfh.54.2022.02.17.07.11.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Feb 2022 07:11:40 -0800 (PST) From: Masami Hiramatsu To: u-boot@lists.denx.de Cc: Masami Hiramatsu , Patrick Delaunay , Patrice Chotard , Heinrich Schuchardt , Alexander Graf , AKASHI Takahiro , Simon Glass , Bin Meng , Ilias Apalodimas , Jose Marinho , Grant Likely , Tom Rini , Etienne Carriere , Sughosh Ganu , Paul Liu Subject: [RFC PATCH v2 2/8] FWU: Free metadata copy if gpt_get_mdata() failed Date: Fri, 18 Feb 2022 00:11:36 +0900 Message-Id: <164511069650.43219.13712227378672274605.stgit@localhost> X-Mailer: git-send-email 2.25.1 In-Reply-To: <164511067605.43219.15508992404634142079.stgit@localhost> References: <164511067605.43219.15508992404634142079.stgit@localhost> User-Agent: StGit/0.19 MIME-Version: 1.0 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.5 at phobos.denx.de X-Virus-Status: Clean It is better if a function which returns an error then release all allocated memory resources. This simplifies the mind model and less chance to forgot to free memory and double free. Signed-off-by: Masami Hiramatsu --- drivers/fwu-mdata/fwu-mdata-uclass.c | 24 ++++++---------- drivers/fwu-mdata/fwu_mdata_gpt_blk.c | 49 ++++++++++++++++++--------------- 2 files changed, 36 insertions(+), 37 deletions(-) diff --git a/drivers/fwu-mdata/fwu-mdata-uclass.c b/drivers/fwu-mdata/fwu-mdata-uclass.c index b98eda3789..c1cd77243f 100644 --- a/drivers/fwu-mdata/fwu-mdata-uclass.c +++ b/drivers/fwu-mdata/fwu-mdata-uclass.c @@ -79,12 +79,12 @@ int fwu_verify_mdata(struct fwu_mdata *mdata, bool pri_part) int fwu_get_active_index(u32 *active_idx) { int ret; - struct fwu_mdata *mdata = NULL; + struct fwu_mdata *mdata; ret = fwu_get_mdata(&mdata); if (ret < 0) { log_err("Unable to get valid FWU metadata\n"); - goto out; + return ret; } /* @@ -96,8 +96,6 @@ int fwu_get_active_index(u32 *active_idx) log_err("Active index value read is incorrect\n"); ret = -EINVAL; } - -out: free(mdata); return ret; @@ -115,17 +113,17 @@ out: int fwu_update_active_index(u32 active_idx) { int ret; - struct fwu_mdata *mdata = NULL; + struct fwu_mdata *mdata; if (active_idx > CONFIG_FWU_NUM_BANKS - 1) { log_err("Active index value to be updated is incorrect\n"); - return -1; + return -EINVAL; } ret = fwu_get_mdata(&mdata); if (ret < 0) { log_err("Unable to get valid FWU metadata\n"); - goto out; + return ret; } /* @@ -144,8 +142,6 @@ int fwu_update_active_index(u32 active_idx) log_err("Failed to update FWU metadata partitions\n"); ret = -EIO; } - -out: free(mdata); return ret; @@ -225,12 +221,12 @@ int fwu_revert_boot_index(void) { int ret; u32 cur_active_index; - struct fwu_mdata *mdata = NULL; + struct fwu_mdata *mdata; ret = fwu_get_mdata(&mdata); if (ret < 0) { log_err("Unable to get valid FWU metadata\n"); - goto out; + return ret; } /* @@ -250,8 +246,6 @@ int fwu_revert_boot_index(void) log_err("Failed to update FWU metadata partitions\n"); ret = -EIO; } - -out: free(mdata); return ret; @@ -277,14 +271,14 @@ static int fwu_set_clear_image_accept(efi_guid_t *img_type_id, { int ret, i; u32 nimages; - struct fwu_mdata *mdata = NULL; + struct fwu_mdata *mdata; struct fwu_image_entry *img_entry; struct fwu_image_bank_info *img_bank_info; ret = fwu_get_mdata(&mdata); if (ret < 0) { log_err("Unable to get valid FWU metadata\n"); - goto out; + return ret; } nimages = CONFIG_FWU_NUM_IMAGES_PER_BANK; diff --git a/drivers/fwu-mdata/fwu_mdata_gpt_blk.c b/drivers/fwu-mdata/fwu_mdata_gpt_blk.c index 9170c3f6af..a32195db2e 100644 --- a/drivers/fwu-mdata/fwu_mdata_gpt_blk.c +++ b/drivers/fwu-mdata/fwu_mdata_gpt_blk.c @@ -177,18 +177,9 @@ static int fwu_gpt_update_mdata(struct udevice * dev, struct fwu_mdata *mdata) return 0; } -static int gpt_get_mdata(struct blk_desc *desc, struct fwu_mdata **mdata) +static int gpt_get_mdata_part(struct blk_desc *desc, struct fwu_mdata **mdata, u16 part) { int ret; - u16 primary_mpart = 0, secondary_mpart = 0; - - ret = gpt_get_mdata_partitions(desc, &primary_mpart, - &secondary_mpart); - - if (ret < 0) { - log_err("Error getting the FWU metadata partitions\n"); - return -ENODEV; - } *mdata = malloc(sizeof(struct fwu_mdata)); if (!*mdata) { @@ -196,28 +187,42 @@ static int gpt_get_mdata(struct blk_desc *desc, struct fwu_mdata **mdata) return -ENOMEM; } - ret = gpt_read_mdata(desc, *mdata, primary_mpart); + ret = gpt_read_mdata(desc, *mdata, part); if (ret < 0) { log_err("Failed to read the FWU metadata from the device\n"); - return -EIO; + ret = -EIO; + } else { + ret = fwu_verify_mdata(*mdata, 1); + if (!ret) + return 0; } + free(*mdata); + + return ret; +} - ret = fwu_verify_mdata(*mdata, 1); +static int gpt_get_mdata(struct blk_desc *desc, struct fwu_mdata **mdata) +{ + int ret; + u16 primary_mpart = 0, secondary_mpart = 0; + + ret = gpt_get_mdata_partitions(desc, &primary_mpart, + &secondary_mpart); + + if (ret < 0) { + log_err("Error getting the FWU metadata partitions\n"); + return -ENODEV; + } + + ret = gpt_get_mdata_part(desc, mdata, primary_mpart); if (!ret) return 0; /* - * Verification of the primary FWU metadata copy failed. + * Reading of the primary FWU metadata copy failed. * Try to read the replica. */ - memset(*mdata, 0, sizeof(struct fwu_mdata)); - ret = gpt_read_mdata(desc, *mdata, secondary_mpart); - if (ret < 0) { - log_err("Failed to read the FWU metadata from the device\n"); - return -EIO; - } - - ret = fwu_verify_mdata(*mdata, 0); + ret = gpt_get_mdata_part(desc, mdata, secondary_mpart); if (!ret) return 0;