From: Christian Marangi <ansuelsmth@gmail.com>
To: Tom Rini <trini@konsulko.com>, Simon Glass <sjg@chromium.org>,
Casey Connolly <casey.connolly@linaro.org>,
Christian Marangi <ansuelsmth@gmail.com>,
Peng Fan <peng.fan@nxp.com>,
Quentin Schulz <quentin.schulz@cherry.de>,
Harsha Vardhan V M <h-vm@ti.com>,
Neha Malcom Francis <n-francis@ti.com>,
Chen-Yu Tsai <wens@kernel.org>,
Jamie Gibbons <jamie.gibbons@microchip.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>,
Sky Huang <SkyLake.Huang@mediatek.com>,
Alif Zakuan Yuslaimi <alif.zakuan.yuslaimi@altera.com>,
u-boot@lists.denx.de
Subject: [PATCH 5/5] misc: fw_loader: implement request_firmware_size() OP
Date: Tue, 3 Mar 2026 14:29:12 +0100 [thread overview]
Message-ID: <20260303132916.5502-6-ansuelsmth@gmail.com> (raw)
In-Reply-To: <20260303132916.5502-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.
Both FS and FIP lodaer are updated to handle the new .get_size() handler.
Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
---
drivers/misc/fw_loader/fip_loader.c | 35 +++++++++++++++
drivers/misc/fw_loader/fs_loader.c | 70 ++++++++++++++++++++++++-----
drivers/misc/fw_loader/fw_loader.c | 20 +++++++++
drivers/misc/fw_loader/internal.h | 1 +
include/fw_loader.h | 9 ++++
5 files changed, 124 insertions(+), 11 deletions(-)
diff --git a/drivers/misc/fw_loader/fip_loader.c b/drivers/misc/fw_loader/fip_loader.c
index 376e1d2a59d8..a285f785db37 100644
--- a/drivers/misc/fw_loader/fip_loader.c
+++ b/drivers/misc/fw_loader/fip_loader.c
@@ -523,6 +523,40 @@ out:
return ret;
}
+/**
+ * fw_get_fip_firmware_size - get firmware size.
+ * @dev: An instance of a driver.
+ *
+ * Return: Size of firmware, negative value when error.
+ */
+static int fw_get_fip_firmware_size(struct udevice *dev)
+{
+ struct fip_toc_entry ent;
+ struct fip_storage_info info = { };
+ int ret;
+
+ ret = fw_parse_storage_info(dev, &info);
+ if (ret)
+ goto out;
+
+ struct firmware *firmwarep = dev_get_priv(dev);
+
+ if (!firmwarep) {
+ ret = -EINVAL;
+ goto out;
+ }
+
+ ret = parse_fip_firmware(firmwarep, &info, &ent);
+ if (ret)
+ goto out;
+
+ ret = ent.size;
+
+out:
+ fw_storage_info_release(dev, &info);
+ return ret;
+}
+
static int fip_loader_probe(struct udevice *dev)
{
struct device_plat *plat = dev_get_plat(dev);
@@ -533,6 +567,7 @@ static int fip_loader_probe(struct udevice *dev)
return ret;
plat->get_firmware = fw_get_fip_firmware;
+ plat->get_size = fw_get_fip_firmware_size;
return 0;
};
diff --git a/drivers/misc/fw_loader/fs_loader.c b/drivers/misc/fw_loader/fs_loader.c
index 4ae01b9c8f0d..d6b9f4998815 100644
--- a/drivers/misc/fw_loader/fs_loader.c
+++ b/drivers/misc/fw_loader/fs_loader.c
@@ -93,15 +93,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;
@@ -126,6 +119,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;
@@ -147,9 +162,41 @@ 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)
+{
+ loff_t size = 0;
+ int ret;
+
+ ret = __fw_get_filesystem_firmware_prepare(dev);
+ if (ret)
+ goto out;
+
+ struct firmware *firmwarep = dev_get_priv(dev);
+
+ if (!firmwarep)
+ return -ENOMEM;
+
+ ret = fs_size(firmwarep->name, &size);
+
+ if (ret) {
+ debug("Error: %d Failed to get size for %s.\n",
+ ret, firmwarep->name);
+ } else {
+ ret = size;
+ }
+
+out:
+ __fw_get_filesystem_firmware_release(dev);
return ret;
}
@@ -163,6 +210,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 d2446fdc07da..d5973fce4b9c 100644
--- a/drivers/misc/fw_loader/fw_loader.c
+++ b/drivers/misc/fw_loader/fw_loader.c
@@ -162,3 +162,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 5513e043925f..4701b494bf3f 100644
--- a/drivers/misc/fw_loader/internal.h
+++ b/drivers/misc/fw_loader/internal.h
@@ -36,6 +36,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 d07306c0674f..a3fa980c768d 100644
--- a/include/fw_loader.h
+++ b/include/fw_loader.h
@@ -37,6 +37,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.51.0
prev parent reply other threads:[~2026-03-03 13:30 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-03 13:29 [PATCH 0/5] misc: fs_loader: reorg and split to FS and FW loader + FIP loader Christian Marangi
2026-03-03 13:29 ` [PATCH 1/5] misc: fs_loader: fix ubifs not unmounted on dev_get_priv error Christian Marangi
2026-03-03 13:38 ` Daniel Golle
2026-03-03 13:43 ` Christian Marangi
2026-03-03 13:53 ` Daniel Golle
2026-03-03 13:29 ` [PATCH 2/5] misc: fs_loader: reorganize and split to FS and FW loader Christian Marangi
2026-03-03 13:29 ` [PATCH 3/5] misc: fw_loader: implement generic get_fw_loader_from_node() Christian Marangi
2026-03-03 13:29 ` [PATCH 4/5] misc: fw_loader: introduce FIP loader driver Christian Marangi
2026-03-03 13:29 ` Christian Marangi [this message]
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=20260303132916.5502-6-ansuelsmth@gmail.com \
--to=ansuelsmth@gmail.com \
--cc=SkyLake.Huang@mediatek.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=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=wens@kernel.org \
--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