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>,
Kever Yang <kever.yang@rock-chips.com>,
Heinrich Schuchardt <xypron.glpk@gmx.de>,
Mateus Lima Alves <mateuslima.ti@gmail.com>,
Jamie Gibbons <jamie.gibbons@microchip.com>,
Neha Malcom Francis <n-francis@ti.com>,
Justin Klaassen <justin@tidylabs.net>,
Leo Yu-Chi Liang <ycliang@andestech.com>,
Weijie Gao <weijie.gao@mediatek.com>,
Marek Vasut <marek.vasut+renesas@mailbox.org>,
"Lucien.Jheng" <lucienzx159@gmail.com>,
u-boot@lists.denx.de
Subject: [PATCH v6 4/6] misc: fw_loader: implement request_firmware_size() OP
Date: Thu, 9 Apr 2026 15:33:00 +0200 [thread overview]
Message-ID: <20260409133303.31875-5-ansuelsmth@gmail.com> (raw)
In-Reply-To: <20260409133303.31875-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 | 68 +++++++++++++++++++----
drivers/misc/fw_loader/fw_loader-uclass.c | 19 +++++++
drivers/misc/fw_loader/internal.h | 7 +++
include/fw_loader.h | 9 +++
4 files changed, 91 insertions(+), 12 deletions(-)
diff --git a/drivers/misc/fw_loader/fs_loader.c b/drivers/misc/fw_loader/fs_loader.c
index 2694111d8396..8f549654a1be 100644
--- a/drivers/misc/fw_loader/fs_loader.c
+++ b/drivers/misc/fw_loader/fs_loader.c
@@ -89,18 +89,10 @@ 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_prepare(struct udevice *dev)
{
char *storage_interface, *dev_part, *ubi_mtdpart, *ubi_volume;
- struct firmware *upriv = dev_get_uclass_priv(dev);
struct device_plat *plat = dev_get_uclass_plat(dev);
- loff_t actread = 0;
int ret;
storage_interface = env_get("storage_interface");
@@ -124,6 +116,29 @@ static int fw_get_filesystem_firmware(struct udevice *dev)
ret = select_fs_dev(plat);
}
+ return ret;
+}
+
+static void fw_get_filesystem_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)
+{
+ struct firmware *upriv = dev_get_uclass_priv(dev);
+ loff_t actread = 0;
+ int ret;
+
+ ret = fw_get_filesystem_prepare(dev);
if (ret)
goto out;
@@ -138,14 +153,43 @@ static int fw_get_filesystem_firmware(struct udevice *dev)
}
out:
-#ifdef CONFIG_CMD_UBIFS
- umount_ubifs();
-#endif
+ fw_get_filesystem_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 *upriv = dev_get_uclass_priv(dev);
+ loff_t size = 0;
+ int ret;
+
+ ret = fw_get_filesystem_prepare(dev);
+ if (ret)
+ goto out;
+
+ ret = fs_size(upriv->name, &size);
+ if (ret) {
+ debug("Error: %d Failed to get size for %s.\n",
+ ret, upriv->name);
+ goto out;
+ }
+
+ ret = size;
+
+out:
+ fw_get_filesystem_release(dev);
return ret;
}
static const struct fw_loader_ops fs_loader_ops = {
.get_firmware = fw_get_filesystem_firmware,
+ .get_size = fw_get_filesystem_firmware_size,
};
static const struct udevice_id fs_loader_ids[] = {
diff --git a/drivers/misc/fw_loader/fw_loader-uclass.c b/drivers/misc/fw_loader/fw_loader-uclass.c
index c32c213b5b38..19379c23411b 100644
--- a/drivers/misc/fw_loader/fw_loader-uclass.c
+++ b/drivers/misc/fw_loader/fw_loader-uclass.c
@@ -168,3 +168,22 @@ int request_firmware_into_buf(struct udevice *dev,
return ops->get_firmware(dev);
}
+
+int request_firmware_size(struct udevice *dev, const char *name)
+{
+ struct fw_loader_ops *ops;
+ int ret;
+
+ if (!dev)
+ return -EINVAL;
+
+ ret = _request_firmware_prepare(dev, name, NULL, 0, 0);
+ if (ret < 0) /* error */
+ return ret;
+
+ ops = fw_loader_get_ops(dev);
+ if (!ops->get_size)
+ return -EOPNOTSUPP;
+
+ return ops->get_size(dev);
+}
diff --git a/drivers/misc/fw_loader/internal.h b/drivers/misc/fw_loader/internal.h
index 2f93d0c706b6..9964dc436afb 100644
--- a/drivers/misc/fw_loader/internal.h
+++ b/drivers/misc/fw_loader/internal.h
@@ -32,6 +32,13 @@ struct fw_loader_ops {
* @dev: Firmware Loader device to read firmware from
*/
int (*get_firmware)(struct udevice *dev);
+
+ /**
+ * get_size() - get firmware size from Firmware Loader driver
+ *
+ * @dev: Firmware Loader device to get firmware size from
+ */
+ int (*get_size)(struct udevice *dev);
};
#define fw_loader_get_ops(dev) ((struct fw_loader_ops *)(dev)->driver->ops)
diff --git a/include/fw_loader.h b/include/fw_loader.h
index 99c47380a172..01f242dcbefd 100644
--- a/include/fw_loader.h
+++ b/include/fw_loader.h
@@ -38,6 +38,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
next prev parent reply other threads:[~2026-04-09 13:33 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-09 13:32 [PATCH v6 0/6] misc: fs_loader: reorg and split to FS and FW loader + FIP loader Christian Marangi
2026-04-09 13:32 ` [PATCH v6 1/6] misc: fs_loader: fix ubifs not unmounted on dev_get_priv error Christian Marangi
2026-04-09 13:32 ` [PATCH v6 2/6] misc: fs_loader: reorganize and split to FS loader and FW UCLASS Christian Marangi
2026-04-09 13:32 ` [PATCH v6 3/6] misc: fw_loader: implement generic get_fw_loader_from_node() Christian Marangi
2026-04-09 13:33 ` Christian Marangi [this message]
2026-04-09 13:33 ` [PATCH v6 5/6] misc: fw_loader: introduce FIP loader driver Christian Marangi
2026-04-09 13:33 ` [PATCH v6 6/6] doc: dtbinding: Update documentation for Generic Firmware loader Christian Marangi
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=20260409133303.31875-5-ansuelsmth@gmail.com \
--to=ansuelsmth@gmail.com \
--cc=casey.connolly@linaro.org \
--cc=jamie.gibbons@microchip.com \
--cc=justin@tidylabs.net \
--cc=kever.yang@rock-chips.com \
--cc=lucienzx159@gmail.com \
--cc=marek.vasut+renesas@mailbox.org \
--cc=mateuslima.ti@gmail.com \
--cc=n-francis@ti.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