From patchwork Mon Feb 12 07:46:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 772028 Delivered-To: patch@linaro.org Received: by 2002:adf:9dca:0:b0:33b:4db1:f5b3 with SMTP id q10csp2000491wre; Sun, 11 Feb 2024 23:48:41 -0800 (PST) X-Google-Smtp-Source: AGHT+IFwRR16K6y3E2fg9YBWpkQMc+qREWeFZOU+ec+kDFy3k5TbWrpyQ0pkEh222Nb0AXBf/Xqa X-Received: by 2002:a17:906:138b:b0:a3c:c75e:df60 with SMTP id f11-20020a170906138b00b00a3cc75edf60mr461334ejc.14.1707724121408; Sun, 11 Feb 2024 23:48:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1707724121; cv=none; d=google.com; s=arc-20160816; b=absZt0lYYvik0wuFB3/0zBoax6Emn96V9ne4F//Xq3VUpIN4LQ0+GoF8q8WKUnJlvf xy/8rsWPHB+I+f07nZrapVvPW6ayeDZq+XuY9psKMiEsDeCwyfPARnWMR+iQp0OctfLz 7PnIJhTrYKy5rcZ3+nLsrYDIBk1gZlEX3oM9My+LKV7C8PAKJXuMtKfYHjWpe/EdXQFk 2fAC7SV9veHFd8YpMTi4uhAFFhJZmJNVOL0+by/gQz5N2JGGebeaLR1L/w5l410I/rd9 DgVM+JBvyvWim93GJ8eQHqCdvdCdrH1sC/qpHof0vD2mP4R9WIYiYgEaQqQ5tmOQWsjt AEVA== 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=yPx2gNHGAttfODFfpA+YHe/VtL2ODE0uWRlqhGVoo9k=; fh=t3R/nFVSxKfmnVlW35tzlMor9J0O+CsbPpotZiPIN3s=; b=Zk0t/l8bPIHgE2tgIwLfS5zrChU8e7z+zmPh7Syo95W1Nw0NM/tW4Hcc41b8mBXkSH 3WguSgfL/9qdG3ntEKYm5Pdf0Yd0vmIxFtnsWDL0cRt52UXaLcO0DCcVPTcCinaPcpPp O2+ddZoH9ZSPREN5IZIbYwu1IKMriMyax4pnFalwMazlGuJuhKF1oLs011dHdCYvEa4z hdUMJtnL7CTRgNU7kU6PThEc2XYVHn8EA1N7ay/GgDzKLrfP6E/OvrTNU5do2/hUbcg2 u2OrvYHjwDvq3VIMz2nN3K+GquJyccpgtgRD35U1b8CvzcztQqCdn2VCpqyYUVrOPdvx tW6Q==; darn=linaro.org 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 X-Forwarded-Encrypted: i=1; AJvYcCWKftDkBAQRVfd2zAJv8aDoP9iSVm3F9gaiFfDZ6fVtIPgsaQFfxXlAHGv9XeQrSDOjXtEpaXFYpylE+Wikie/40GfE0kBsb6e3r9sCIW+F+jLHFeBaN4kqjVWJfJWM1GI1+NI5BoU/U7cmME99uqxq0cFiET1k+qouKTtHZ1WlNDGL1hOuVCLkMTCLRJCeztbOg+zydVMBSgFbHLTMAyxQWqxvBw7EPJ2KXI0rdApZrnvgV0/GlNFL+5Ed3BSWFHlZEUP+i/okxxl+xcgxIvV696WKJAjqonY79tHXQzMdKbhuk6O5ExkngKT22AhsmxWByoD9xijNgMom/SEbqOiyjR9pi8BjRVZPEAp7eZErKMN7cZZIDCYd/X+T92dhZ0T+CW3Zp1dLBJm3n8yIKYyPRH0Pc0UcJxIZnqNtUjGRpvvuBE2VpWRKH4F3cOkp7aggu4fCRUgIFfHeT8mXzk+JhK1JYtqLkMY8mrtnT/9yimLVOV8C7hpcwQBElMQv+/X6c8Is10kQP33P4qKVXpGX5LjH9cJWfUhNLT7vtfTq51FTxiY6JwadkBhjNsYvbdXhnSzEQUjMwzeuyLZxEIN9nALQimIw8WrBPyqRQhDSbJtGNqYxhRJgI539/UQb7ViFu6huYY8fh9Pv58gcK+LLTIwIYwpN+TDbTcU549+aqN30IgVORuc/dDDfGsNVnlLq2cB2yNKDcZp8l5nYNw8FrQ60rfiU8py7bHoi3Y7mvEehSfzBh+PKT+F/QX5supULIC1u0hoojbgwdl3sYtYmz2/gcr8CC/dtd6o6Y0p3s/dMEKkpCj1wcVaB0Kd37114WOe1Lw8Nn1WjulhWttHEkPtxzT3kNHA3oknRx33W9lA= Return-Path: Received: from phobos.denx.de (phobos.denx.de. [85.214.62.61]) by mx.google.com with ESMTPS id f15-20020a170906048f00b00a31619d6350si3338409eja.493.2024.02.11.23.48.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 11 Feb 2024 23:48:41 -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 DCC2987ED2; Mon, 12 Feb 2024 08:47:59 +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 4DC0D87EDE; Mon, 12 Feb 2024 08:47:59 +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 120E287808 for ; Mon, 12 Feb 2024 08:47:57 +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 1AF3EDA7; Sun, 11 Feb 2024 23:48:38 -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 4E7E03F7BD; Sun, 11 Feb 2024 23:47:53 -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 , Michal Simek , Jassi Brar , Sughosh Ganu Subject: [PATCH v2 06/21] fwu: add some API's for metadata version 2 access Date: Mon, 12 Feb 2024 13:16:57 +0530 Message-Id: <20240212074712.3657076-7-sughosh.ganu@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240212074712.3657076-1-sughosh.ganu@linaro.org> References: <20240212074712.3657076-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 --- Changes since V1: * Use the helper functions from the previous patch to access the image information in the metadata. include/fwu.h | 53 ++++++++++++++++ lib/fwu_updates/fwu.c | 144 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 197 insertions(+) diff --git a/include/fwu.h b/include/fwu.h index 8f2492bb7e..ce8c98921a 100644 --- a/include/fwu.h +++ b/include/fwu.h @@ -153,6 +153,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 @@ -202,6 +222,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 @@ -335,4 +367,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 + */ +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 2da19c9003..587ca779d3 100644 --- a/lib/fwu_updates/fwu.c +++ b/lib/fwu_updates/fwu.c @@ -240,6 +240,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 + */ +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 = fwu_get_fw_desc(g_mdata)->num_banks; + *nimages = fwu_get_fw_desc(g_mdata)->num_images; + + return 0; +} + /** * fwu_get_mdata() - Read, verify and return the FWU metadata * @mdata: Output FWU metadata read or NULL @@ -564,6 +672,42 @@ 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_bank_info *img_bank_info; + + if (!mdata) + return -EINVAL; + + num_images = fwu_get_fw_desc(mdata)->num_images; + for (i = 0; i < num_images; i++) { + img_bank_info = fwu_img_bank_info_offset(g_mdata, i, + 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