From patchwork Mon Jan 22 11:54:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 764622 Delivered-To: patch@linaro.org Received: by 2002:a05:6000:10c4:b0:337:62d3:c6d5 with SMTP id b4csp1059030wrx; Mon, 22 Jan 2024 03:55:55 -0800 (PST) X-Google-Smtp-Source: AGHT+IHoeeqKrmE5pPQYBlYLHU7UIPlNh7/QfwOJ78qwa+qH8e92hSS4MUx+3k1KEZC7UyUh0Ly+ X-Received: by 2002:a05:600c:2241:b0:40e:7b93:d747 with SMTP id a1-20020a05600c224100b0040e7b93d747mr2305865wmm.139.1705924555555; Mon, 22 Jan 2024 03:55:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1705924555; cv=none; d=google.com; s=arc-20160816; b=BrQXySKzGt/J0GAt1PY1iZVKHSwDCFxDX5DgoUUAiSl96liRHiToulXZAMKTD9C1sI O6aGvqvUwu0bMG6cGsMK5mp1MSj7fGbcn3G9N+P2pDsTqNsLY2ViSkVhD+Ba/eJru0az DvTlw15xwDQK/6E+uCJplB92WJ821BqIre9FUGBD/VK0PcsjqNmb35cR5hsv3aAYSsHH 6PNTVdgClblBSg0Svy2Yy65dHDudyFH4xGnIWWaWtyCG5aH7GkoLC/4pXvxe7FH9fNcx 0wHJG/fFteEDJy2l/+ulOYlP1+rgRciLNmu8rvLLYyIk4yINXq1LHU7UaCkK8l1SaUsO EsdA== 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:references:in-reply-to:message-id:date:subject:cc:to :from; bh=EBj9EYU4kRw8LchWOMl+kF8RykNNLHK9D/iUhIWwHGw=; fh=rhmJ78R78zNUSUA9Nt0a5TbZO4CVsI4vi5GcqSIe+nI=; b=FGYzqh/FTPC3OLEgcjIcZ3KfdG6TIZZIgf4xPpvomRqeZcvH7CLuyz50oF91HNzFme FWbH80ZxminayU4dHjhMGtBCIQOEwPhuSkHd4siXP6eDLYSfYyxUooJI+tk65V+LByr1 OX5wEeKxuAxoUWeVOfDLRsbMdJLA+POtklU70I7MXc7FU/N5qP9Wq4YcfM5JFNRsR4M4 dTZpop2wc7U5msfgPuFif4frwvpIO+Zz/WwnKbmIweHzlL3P0PlVrntUvP3bC4fxfYyk Kevl/3Bb84eG5oK4U+2i0I21DTqh9cMqy5clIlPT6n4VVri/6KUF3BZp9yYuQ1NYbqfz q51Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 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. [85.214.62.61]) by mx.google.com with ESMTPS id e8-20020adfe7c8000000b00339272c885fsi2748790wrn.98.2024.01.22.03.55.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jan 2024 03:55:55 -0800 (PST) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 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 0C0E68796E; Mon, 22 Jan 2024 12:55:34 +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 E8A6A879E6; Mon, 22 Jan 2024 12:55:27 +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=-1.2 required=5.0 tests=BAYES_00,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 D293C87950 for ; Mon, 22 Jan 2024 12:55:24 +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 5F97F1FB; Mon, 22 Jan 2024 03:56:10 -0800 (PST) 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 ADED13F73F; Mon, 22 Jan 2024 03:55:21 -0800 (PST) From: Sughosh Ganu To: u-boot@lists.denx.de Cc: Heinrich Schuchardt , Ilias Apalodimas , Masahisa Kojima , Patrice Chotard , Patrick Delaunay , Yann Gautier , Etienne Carriere , Sughosh Ganu Subject: [PATCH 04/18] fwu: Add some API's for metadata version 2 access Date: Mon, 22 Jan 2024 17:24:25 +0530 Message-Id: <20240122115439.653871-5-sughosh.ganu@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240122115439.653871-1-sughosh.ganu@linaro.org> References: <20240122115439.653871-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 There are certain fields added in version 2 of the FWU metadata structure. Also, information like number of banks and number of images per bank are also part of the metadata structure. Add functions to access fields of the version 2 of the metadata structure. Signed-off-by: Sughosh Ganu --- include/fwu.h | 53 ++++++++++++++++ lib/fwu_updates/fwu.c | 142 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 195 insertions(+) diff --git a/include/fwu.h b/include/fwu.h index 1815bd0064..d3e97a5360 100644 --- a/include/fwu.h +++ b/include/fwu.h @@ -104,6 +104,26 @@ int fwu_write_mdata(struct udevice *dev, struct fwu_mdata *mdata, */ int fwu_get_mdata(struct fwu_mdata *mdata); +/** + * fwu_mdata_copies_allocate() - Allocate memory for metadata + * + * Allocate memory for storing both the copies of the FWU metadata. The + * copies are then used as a cache for storing FWU metadata contents. + * + * Return: 0 if OK, -ve on error + */ +int fwu_mdata_copies_allocate(void); + +/** + * fwu_get_mdata_size() - Get the FWU metadata size + * + * Get the size of the FWU metadata from the structure. This is later used + * to allocate memory for the structure. + * + * Return: 0 if OK, -ve on error + */ +int fwu_get_mdata_size(uint32_t *mdata_size); + /** * fwu_get_active_index() - Get active_index from the FWU metadata * @active_idxp: active_index value to be read @@ -153,6 +173,18 @@ int fwu_get_dfu_alt_num(u8 image_index, u8 *alt_num); */ int fwu_revert_boot_index(void); +/** + * fwu_bank_state_update() - Check and update the bank_state of the metadata + * @update_index: Bank for which the bank_state needs to be updated + * + * Check that all the images for the given bank have been accepted, and if + * they are, set the status of the bank to Accepted in the bank_state field + * of the metadata. + * + * Return: 0 if OK, -ve on error + */ +int fwu_bank_state_update(uint update_index); + /** * fwu_accept_image() - Set the Acceptance bit for the image * @img_type_id: GUID of the image type for which the accepted bit is to be @@ -286,4 +318,25 @@ int fwu_gen_alt_info_from_mtd(char *buf, size_t len, struct mtd_info *mtd); */ int fwu_mtd_get_alt_num(efi_guid_t *image_guid, u8 *alt_num, const char *mtd_dev); +/** + * fwu_get_banks_images() - Get the number of banks and images from the metadata + * @nbanks: Number of banks + * @nimages: Number of images per bank + * + * Get the values of number of banks and number of images per bank from the + * metadata. + * + * Return: 0 if OK, -ve on error + */ +__maybe_unused int fwu_get_banks_images(u8 *nbanks, u16 *nimages); + +/** + * fwu_get_dev() - Return the FWU metadata device + * + * Return the pointer to the FWU metadata device. + * + * Return: Pointer to the FWU metadata dev + */ +__maybe_unused struct udevice *fwu_get_dev(void); + #endif /* _FWU_H_ */ diff --git a/lib/fwu_updates/fwu.c b/lib/fwu_updates/fwu.c index 86518108c2..5bfa24067b 100644 --- a/lib/fwu_updates/fwu.c +++ b/lib/fwu_updates/fwu.c @@ -188,6 +188,114 @@ static inline int mdata_crc_check(struct fwu_mdata *mdata) return calc_crc32 == mdata->crc32 ? 0 : -EINVAL; } +/** + * fwu_mdata_copies_allocate() - Allocate memory for metadata + * + * Allocate memory for storing both the copies of the FWU metadata. The + * copies are then used as a cache for storing FWU metadata contents. + * + * Return: 0 if OK, -ve on error + */ +int fwu_mdata_copies_allocate(void) +{ + int err; + uint32_t mdata_size; + + if (g_mdata) + return 0; + + err = fwu_get_mdata_size(&mdata_size); + if (err) + return err; + + /* + * Now allocate the total memory that would be needed for both + * the copies. + */ + g_mdata = calloc(2, mdata_size); + if (!g_mdata) { + log_err("Unable to allocate space for FWU metadata\n"); + return -ENOMEM; + } + + return 0; +} + +/** + * fwu_get_mdata_size() - Get the FWU metadata size + * + * Get the size of the FWU metadata from the structure. This is later used + * to allocate memory for the structure. + * + * Return: 0 if OK, -ve on error + */ +int fwu_get_mdata_size(uint32_t *mdata_size) +{ + int err = 0; + struct fwu_mdata mdata = { 0 }; + + if (g_mdata && !mdata_crc_check(g_mdata)) { + *mdata_size = g_mdata->metadata_size; + return 0; + } + + err = fwu_read_mdata(g_dev, &mdata, 1, sizeof(struct fwu_mdata)); + if (err) { + log_err("FWU metadata read failed\n"); + return err; + } + + if (mdata.version != 0x2) { + log_err("FWU metadata version %u. Expected value of 2\n", + mdata.version); + return -EINVAL; + } + + *mdata_size = mdata.metadata_size; + if (!*mdata_size) + return -EINVAL; + + return 0; +} + +/** + * fwu_get_dev() - Return the FWU metadata device + * + * Return the pointer to the FWU metadata device. + * + * Return: Pointer to the FWU metadata dev + */ +__maybe_unused struct udevice *fwu_get_dev(void) +{ + return g_dev; +} + +/** + * fwu_get_banks_images() - Get the number of banks and images from the metadata + * @nbanks: Number of banks + * @nimages: Number of images per bank + * + * Get the values of number of banks and number of images per bank from the + * metadata. + * + * Return: 0 if OK, -ve on error + */ +__maybe_unused int fwu_get_banks_images(u8 *nbanks, u16 *nimages) +{ + int ret; + + if (mdata_crc_check(g_mdata)) { + ret = fwu_get_mdata(NULL); + if (ret) + return ret; + } + + *nbanks = g_mdata->fw_desc[0].num_banks; + *nimages = g_mdata->fw_desc[0].num_images; + + return 0; +} + /** * fwu_get_mdata() - Read, verify and return the FWU metadata * @mdata: Output FWU metadata read or NULL @@ -473,6 +581,40 @@ out: return ret; } +/** + * fwu_bank_state_update() - Check and update the bank_state of the metadata + * @update_index: Bank for which the bank_state needs to be updated + * + * Check that all the images for the given bank have been accepted, and if + * they are, set the status of the bank to Accepted in the bank_state field + * of the metadata. + * + * Return: 0 if OK, -ve on error + */ +int fwu_bank_state_update(uint update_index) +{ + int ret = 0, i; + u16 num_images; + struct fwu_mdata *mdata = g_mdata; + struct fwu_image_entry *img_entry; + struct fwu_image_bank_info *img_bank_info; + + img_entry = &mdata->fw_desc[0].img_entry[0]; + num_images = mdata->fw_desc[0].num_images; + for (i = 0; i < num_images; i++) { + img_bank_info = &img_entry[i].img_bank_info[update_index]; + if (!(img_bank_info->accepted & FWU_IMAGE_ACCEPTED)) + return 0; + } + + mdata->bank_state[update_index] = FWU_BANK_ACCEPTED; + ret = fwu_sync_mdata(mdata, BOTH_PARTS); + if (ret) + log_err("Unable to set bank_state for bank %u\n", update_index); + + return ret; +} + /** * fwu_accept_image() - Set the Acceptance bit for the image * @img_type_id: GUID of the image type for which the accepted bit is to be