From patchwork Fri Aug 30 11:40:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 823981 Delivered-To: patch@linaro.org Received: by 2002:a5d:48c1:0:b0:367:895a:4699 with SMTP id p1csp730672wrs; Fri, 30 Aug 2024 04:41:34 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCX61FLOUwn0eCWUP31lqOIMVaUI4qm0JqxzG1PlsyeWZCttPc5PJwmTlemHB3gtYJO8EGaWLA==@linaro.org X-Google-Smtp-Source: AGHT+IHoeXlAXmkqIntQj9RwlMzht0nBJmna0re7wW9dEh7QaL+idq6FGa6aqFXSMTpSMmp3NbsY X-Received: by 2002:a17:907:3ea2:b0:a7a:cc10:667c with SMTP id a640c23a62f3a-a897f835ca1mr411171566b.16.1725018094774; Fri, 30 Aug 2024 04:41:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1725018094; cv=none; d=google.com; s=arc-20240605; b=ILLskJq07rNuQvm3VM6EhJVZAdg4LpcDba9RniUZhs4JI8GW5UTi35mcfLaKap9gxV rW8Es6kV9cL2c+FRU3hnkiCgGyWb4ZQBhLmhStnBHV3KnP6flMNoFoQW1kdecBNwdfWw /hXw18iZc5Nf2Fm1pPMPr0qf/9cUpQCSLtmBgHpEjjDg2PhTau4tX3gt5j/3RqoI1il2 PN4LUyT7gW3F/964sl2V8NO/tDVn6muJw/+nZNmhOhO/omgeDljL2F39chPrsCSlMquZ hq8cvilHQ17QI5SLB0hPYpcZjM6lpcN8VGF4g6uf4dO8IK+sWVVZJYL/lVnBLxIKlcfH OtfA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from; bh=eWtfQ2lFDfU6A8DoMDODMR66kg8+/IQ1zJDjoCInE/o=; fh=2L6lzFTxXJKn0sJyMiGOHr75auJVv+HSnsI1uFLsQPk=; b=V8wPtvmtDAU1L2omXV6SNz6eFTEblVf9ubtF2ikzwMwymqQFBkycox1ZyuRkqPEHjw P6EQNNzeur9pKE6qKi+8kM7dPHI7yGT65siQklmfEuOog+/Jcwosu0UMfgGXHrUdNpQj z8WkRQINFEjq3ePC+5obhLa18rGGFGyhSHuuBNC0jjemvvpoIwD+WHAb4vL45iGkPURS SWtscrxBkckpiHhi2xB6FKbO8jtxaWMd+/4Yo8udjXYgAt0hwJjPtb6paQ5XBXTzAFUg bE8Wv5naKKdRqxJ677CKPar3t0IVKob6lS6Q57fSQOLFt8l0cGrsZjRFWIZHR3TkBycS /Hkg==; dara=google.com 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 a640c23a62f3a-a8988f40946si283478466b.4.2024.08.30.04.41.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Aug 2024 04:41:34 -0700 (PDT) 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 C606088AA4; Fri, 30 Aug 2024 13:41:17 +0200 (CEST) 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 4961988A5B; Fri, 30 Aug 2024 13:41:17 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.2 required=5.0 tests=BAYES_00, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED,RCVD_IN_VALIDITY_RPBL_BLOCKED, SPF_HELO_NONE,SPF_SOFTFAIL,T_SCC_BODY_TEXT_LINE autolearn=no 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 189DF88A10 for ; Fri, 30 Aug 2024 13:41:15 +0200 (CEST) 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 C54BA339; Fri, 30 Aug 2024 04:41:40 -0700 (PDT) Received: from a079122.blr.arm.com (a079122.arm.com [10.162.17.48]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id B4D4E3F66E; Fri, 30 Aug 2024 04:41:12 -0700 (PDT) From: Sughosh Ganu To: u-boot@lists.denx.de Cc: Michal Simek , Ilias Apalodimas , Heinrich Schuchardt , Sughosh Ganu Subject: [PATCH 2/6] fwu: v2: try reading both copies of metadata Date: Fri, 30 Aug 2024 17:10:53 +0530 Message-Id: <20240830114057.891069-3-sughosh.ganu@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240830114057.891069-1-sughosh.ganu@linaro.org> References: <20240830114057.891069-1-sughosh.ganu@linaro.org> 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.8 at phobos.denx.de X-Virus-Status: Clean In the version 2 of the FWU metadata, the metadata is broken into two parts, a top-level structure, which provides information on the total size of the structure among other things. Try reading the primary partition first, and if that fails, try reading the secondary partition. This will help in the scenario where the primary metadata partition has been corrupted, but the secondary partition is intact. Signed-off-by: Sughosh Ganu --- lib/fwu_updates/fwu_v2.c | 77 +++++++++++++++++++++------------------- 1 file changed, 41 insertions(+), 36 deletions(-) diff --git a/lib/fwu_updates/fwu_v2.c b/lib/fwu_updates/fwu_v2.c index d0d8a25929..69306282aa 100644 --- a/lib/fwu_updates/fwu_v2.c +++ b/lib/fwu_updates/fwu_v2.c @@ -10,6 +10,12 @@ #include #define FWU_MDATA_VERSION 0x2U +#define FWU_IMG_DESC_OFFSET 0x20U + +static struct fwu_mdata g_mdata; + +#define PRI_PART 0x1U +#define SEC_PART 0x2U static inline struct fwu_fw_store_desc *fwu_get_fw_desc(struct fwu_mdata *mdata) { @@ -109,6 +115,31 @@ static int fwu_trial_state_start(uint update_index) return 0; } +static bool fwu_get_mdata_mandatory(uint part) +{ + int ret = 0; + struct udevice *fwu_dev = fwu_get_dev(); + + memset(&g_mdata, 0, sizeof(struct fwu_mdata)); + + ret = fwu_read_mdata(fwu_dev, &g_mdata, + part == PRI_PART ? true : false, + sizeof(struct fwu_mdata)); + if (ret) + return false; + + if (g_mdata.version != FWU_MDATA_VERSION) + return false; + + if (!g_mdata.desc_offset) + return false; + + if (g_mdata.desc_offset != FWU_IMG_DESC_OFFSET) + return false; + + return true; +} + /** * fwu_populate_mdata_image_info() - Populate the image information * of the metadata @@ -169,24 +200,14 @@ int fwu_state_machine_updates(bool trial_state, uint32_t update_index) */ int fwu_get_mdata_size(uint32_t *mdata_size) { - int ret = 0; - struct fwu_mdata mdata = { 0 }; struct fwu_data *data = fwu_get_data(); - struct udevice *fwu_dev = fwu_get_dev(); if (data->metadata_size) { *mdata_size = data->metadata_size; return 0; } - ret = fwu_read_mdata(fwu_dev, &mdata, 1, - sizeof(struct fwu_mdata)); - if (ret) { - log_err("FWU metadata read failed\n"); - return ret; - } - - *mdata_size = mdata.metadata_size; + *mdata_size = g_mdata.metadata_size; if (!*mdata_size) return -EINVAL; @@ -206,39 +227,23 @@ int fwu_get_mdata_size(uint32_t *mdata_size) int fwu_init(void) { int ret; - struct fwu_mdata mdata = { 0 }; - struct udevice *fwu_dev = fwu_get_dev(); /* * First we read only the top level structure * and get the size of the complete structure. + * Try reading the first partition first, if + * that does not work, try the secondary + * partition. The idea is, if one of the + * partitions is corrupted, it should be restored + * from the intact partition. */ - ret = fwu_read_mdata(fwu_dev, &mdata, 1, - sizeof(struct fwu_mdata)); - if (ret) { + if (!fwu_get_mdata_mandatory(PRI_PART) && + !fwu_get_mdata_mandatory(SEC_PART)) { log_err("FWU metadata read failed\n"); - return ret; - } - - if (mdata.version != FWU_MDATA_VERSION) { - log_err("FWU metadata version %u. Expected value of %u\n", - mdata.version, FWU_MDATA_VERSION); - return -EINVAL; - } - - if (!mdata.desc_offset) { - log_err("No image information provided with the Metadata. "); - log_err("Image information expected in the metadata\n"); - return -EINVAL; - } - - if (mdata.desc_offset != 0x20) { - log_err("Descriptor Offset(0x%x) in the FWU Metadata not equal to 0x20\n", - mdata.desc_offset); - return -EINVAL; + return -1; } - ret = fwu_mdata_copies_allocate(mdata.metadata_size); + ret = fwu_mdata_copies_allocate(g_mdata.metadata_size); if (ret) return ret;