public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Christian Marangi <ansuelsmth@gmail.com>
To: Tom Rini <trini@konsulko.com>, Simon Glass <sjg@chromium.org>,
	Christian Marangi <ansuelsmth@gmail.com>,
	Casey Connolly <casey.connolly@linaro.org>,
	Quentin Schulz <quentin.schulz@cherry.de>,
	Peng Fan <peng.fan@nxp.com>,
	Justin Klaassen <justin@tidylabs.net>,
	Neha Malcom Francis <n-francis@ti.com>,
	Heinrich Schuchardt <xypron.glpk@gmx.de>,
	Jamie Gibbons <jamie.gibbons@microchip.com>,
	Leo Yu-Chi Liang <ycliang@andestech.com>,
	Harsha Vardhan V M <h-vm@ti.com>,
	Weijie Gao <weijie.gao@mediatek.com>,
	Marek Vasut <marek.vasut+renesas@mailbox.org>,
	Patrice Chotard <patrice.chotard@foss.st.com>,
	Yao Zi <me@ziyao.cc>,
	Alif Zakuan Yuslaimi <alif.zakuan.yuslaimi@altera.com>,
	"Lucien.Jheng" <lucienzx159@gmail.com>,
	u-boot@lists.denx.de
Subject: [PATCH v5 4/6] misc: fw_loader: implement request_firmware_size() OP
Date: Fri,  3 Apr 2026 15:52:01 +0200	[thread overview]
Message-ID: <20260403135205.26979-5-ansuelsmth@gmail.com> (raw)
In-Reply-To: <20260403135205.26979-1-ansuelsmth@gmail.com>

It might be useful to get the firmware size before reading it to allocate
the correct size. This is needed for case where the firmware size change
across different firmware version and is not always the same. In such case
the only way to handle this scenario is to allocate a big-enough buffer to
read the firmware.

To better handle this, introduce the request_firmware_size() OP to get the
firmware size before reading it. This way proper buffer can be allocated
and dynamic firmware size can be better supported.

FS loader is updated to handle the new .get_size() handler.

Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
---
 drivers/misc/fw_loader/fs_loader.c | 72 +++++++++++++++++++++++++-----
 drivers/misc/fw_loader/fw_loader.c | 20 +++++++++
 drivers/misc/fw_loader/internal.h  |  1 +
 include/fw_loader.h                |  9 ++++
 4 files changed, 91 insertions(+), 11 deletions(-)

diff --git a/drivers/misc/fw_loader/fs_loader.c b/drivers/misc/fw_loader/fs_loader.c
index 2748d9f041e8..87e7840686f1 100644
--- a/drivers/misc/fw_loader/fs_loader.c
+++ b/drivers/misc/fw_loader/fs_loader.c
@@ -89,15 +89,8 @@ static int select_fs_dev(struct device_plat *plat)
 	return ret;
 }
 
-/**
- * fw_get_filesystem_firmware - load firmware into an allocated buffer.
- * @dev: An instance of a driver.
- *
- * Return: Size of total read, negative value when error.
- */
-static int fw_get_filesystem_firmware(struct udevice *dev)
+static int __fw_get_filesystem_firmware_prepare(struct udevice *dev)
 {
-	loff_t actread = 0;
 	char *storage_interface, *dev_part, *ubi_mtdpart, *ubi_volume;
 	int ret;
 
@@ -122,6 +115,28 @@ static int fw_get_filesystem_firmware(struct udevice *dev)
 		ret = select_fs_dev(dev_get_plat(dev));
 	}
 
+	return ret;
+}
+
+static void __fw_get_filesystem_firmware_release(struct udevice *dev)
+{
+#ifdef CONFIG_CMD_UBIFS
+	umount_ubifs();
+#endif
+}
+
+/**
+ * fw_get_filesystem_firmware - load firmware into an allocated buffer.
+ * @dev: An instance of a driver.
+ *
+ * Return: Size of total read, negative value when error.
+ */
+static int fw_get_filesystem_firmware(struct udevice *dev)
+{
+	loff_t actread = 0;
+	int ret;
+
+	ret = __fw_get_filesystem_firmware_prepare(dev);
 	if (ret)
 		goto out;
 
@@ -143,9 +158,43 @@ static int fw_get_filesystem_firmware(struct udevice *dev)
 	}
 
 out:
-#ifdef CONFIG_CMD_UBIFS
-	umount_ubifs();
-#endif
+	__fw_get_filesystem_firmware_release(dev);
+	return ret;
+}
+
+/**
+ * fw_get_filesystem_firmware_size - get firmware size.
+ * @dev: An instance of a driver.
+ *
+ * Return: Size of firmware, negative value when error.
+ */
+static int fw_get_filesystem_firmware_size(struct udevice *dev)
+{
+	struct firmware *firmwarep;
+	loff_t size = 0;
+	int ret;
+
+	ret = __fw_get_filesystem_firmware_prepare(dev);
+	if (ret)
+		goto out;
+
+	firmwarep = dev_get_priv(dev);
+	if (!firmwarep) {
+		ret = -ENOMEM;
+		goto out;
+	}
+
+	ret = fs_size(firmwarep->name, &size);
+	if (ret) {
+		debug("Error: %d Failed to get size for %s.\n",
+		      ret, firmwarep->name);
+		goto out;
+	}
+
+	ret = size;
+
+out:
+	__fw_get_filesystem_firmware_release(dev);
 	return ret;
 }
 
@@ -159,6 +208,7 @@ static int fs_loader_probe(struct udevice *dev)
 		return ret;
 
 	plat->get_firmware = fw_get_filesystem_firmware;
+	plat->get_size = fw_get_filesystem_firmware_size;
 
 	return 0;
 };
diff --git a/drivers/misc/fw_loader/fw_loader.c b/drivers/misc/fw_loader/fw_loader.c
index c3d0de1b93e7..06bd6ab9ab12 100644
--- a/drivers/misc/fw_loader/fw_loader.c
+++ b/drivers/misc/fw_loader/fw_loader.c
@@ -174,3 +174,23 @@ int request_firmware_into_buf(struct udevice *dev,
 
 	return plat->get_firmware(dev);
 }
+
+int request_firmware_size(struct udevice *dev, const char *name)
+{
+	struct device_plat *plat;
+	int ret;
+
+	if (!dev)
+		return -EINVAL;
+
+	ret = _request_firmware_prepare(dev, name, NULL, 0, 0);
+	if (ret < 0) /* error */
+		return ret;
+
+	plat = dev_get_plat(dev);
+
+	if (!plat->get_size)
+		return -EOPNOTSUPP;
+
+	return plat->get_size(dev);
+}
diff --git a/drivers/misc/fw_loader/internal.h b/drivers/misc/fw_loader/internal.h
index fa006b7e6077..a94de609d0b6 100644
--- a/drivers/misc/fw_loader/internal.h
+++ b/drivers/misc/fw_loader/internal.h
@@ -34,6 +34,7 @@ struct device_plat {
 	char *ubivol;
 
 	int (*get_firmware)(struct udevice *dev);
+	int (*get_size)(struct udevice *dev);
 };
 
 /**
diff --git a/include/fw_loader.h b/include/fw_loader.h
index 7053c7bc6f05..1af3db385161 100644
--- a/include/fw_loader.h
+++ b/include/fw_loader.h
@@ -39,6 +39,15 @@ int request_firmware_into_buf(struct udevice *dev,
 			      const char *name,
 			      void *buf, size_t size, u32 offset);
 
+/**
+ * request_firmware_size - Get firmware size.
+ * @dev: An instance of a driver.
+ * @name: Name of firmware file.
+ *
+ * Return: Size of firmware, negative value when error.
+ */
+int request_firmware_size(struct udevice *dev, const char *name);
+
 /**
  * request_firmware_into_buf_via_script() -
  * Load firmware using a U-Boot script and copy to buffer
-- 
2.53.0


  parent reply	other threads:[~2026-04-03 13:52 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-03 13:51 [PATCH v5 0/6] misc: fs_loader: reorg and split to FS and FW loader + FIP loader Christian Marangi
2026-04-03 13:51 ` [PATCH v5 1/6] misc: fs_loader: fix ubifs not unmounted on dev_get_priv error Christian Marangi
2026-04-03 13:51 ` [PATCH v5 2/6] misc: fs_loader: reorganize and split to FS and FW loader Christian Marangi
2026-04-06 17:10   ` Simon Glass
2026-04-03 13:52 ` [PATCH v5 3/6] misc: fw_loader: implement generic get_fw_loader_from_node() Christian Marangi
2026-04-06 17:10   ` Simon Glass
2026-04-03 13:52 ` Christian Marangi [this message]
2026-04-06 17:13   ` [PATCH v5 4/6] misc: fw_loader: implement request_firmware_size() OP Simon Glass
2026-04-03 13:52 ` [PATCH v5 5/6] misc: fw_loader: introduce FIP loader driver Christian Marangi
2026-04-06 17:14   ` Simon Glass
2026-04-09 13:36     ` Christian Marangi
2026-04-03 13:52 ` [PATCH v5 6/6] doc: dtbinding: Update documentation for Generic Firmware loader Christian Marangi
2026-04-06 17:14   ` Simon Glass

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260403135205.26979-5-ansuelsmth@gmail.com \
    --to=ansuelsmth@gmail.com \
    --cc=alif.zakuan.yuslaimi@altera.com \
    --cc=casey.connolly@linaro.org \
    --cc=h-vm@ti.com \
    --cc=jamie.gibbons@microchip.com \
    --cc=justin@tidylabs.net \
    --cc=lucienzx159@gmail.com \
    --cc=marek.vasut+renesas@mailbox.org \
    --cc=me@ziyao.cc \
    --cc=n-francis@ti.com \
    --cc=patrice.chotard@foss.st.com \
    --cc=peng.fan@nxp.com \
    --cc=quentin.schulz@cherry.de \
    --cc=sjg@chromium.org \
    --cc=trini@konsulko.com \
    --cc=u-boot@lists.denx.de \
    --cc=weijie.gao@mediatek.com \
    --cc=xypron.glpk@gmx.de \
    --cc=ycliang@andestech.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox