Skip to contentSkip to conversation

misc: fs_loader: reorg and split to FS and FW loader + FIP loader

PATCH Superseded Christian Marangi 3 participants 14 messages Apr 9 – Apr 11 Char and misc drivers

v6 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

v6 cover misc: fs_loader: reorg and split to FS and FW loader + FIP loader

This series mainly rework the FS loader to permit reading firmware from container that are not exactly a readable filesystem.

Also splitting out the generic firmware loader infra from the filesystem-specific code will make adding new loader types easier.

One scenario is when a firmware is shipped in a FIP container as a generic blob with an UUID.

FIP are mostly used on ARM in the context of ATF.
In such context U-Boot is loaded as BL31 and the PHY firmware can't be stored in the FIT image for the kernel as U-Boot should not depend on the presence of the kernel to correctly enable PHY for Ethernet port.

To handle such case the PHY firmware is stored in FIP with a predictable UUID.

One example is with Airoha 8811H firmware where the FIP can have a blob with UUID "d39d2cf2-9bd0-3ca7-93e9-e71b4f9250b2". (generated from command "uuidgen -n @dns --md5 --name en8811h.bin")

With these example DTS entry:

fs_loader0: fip-loader {
	bootph-all;
	compatible = "u-boot,fip-loader";
	phandlepart = <&mmc0 0>;
	partoffset = <0x100>;
};

mdio {

  en8811: ethernet-phy@f {
    reg = <0xf>;
    firmware-name = "d39d2cf2-9bd0-3ca7-93e9-e71b4f9250b2";
    firmware-loader = <&fs_loader0>;

};
};

And PHY driver using the get_fw_loader_from_node() and the common request_firmware_into_buf() it's possible to load the PHY firmware transparently by just declaring the required entry in the DTS.

get_fw_loader_from_node() is implemented to actual get the loader from DT. This was something that was already in mind from when the FS loader was implemented but then it was never implemented in favor of a single loader per device.

The first patch is a minor fixup for something that probably won't ever happen.

The second one is a good reworking of the FS and FW loader moving the internal struct to a dedicated header. (this is really to enforce what drivers should use and what driver should not mess with)

Then there is the request_firmware_size() new OP to get only the size of the firmware. Useful for case where the firmware size is not always the same and change across different version. (the patter might be get size -> alloc buffer -> get firmware).

Then the FIP loader as a basic parser of FIP. This only read the FIP header, loop all the entry and search for a matching UUID. If nothing is found then no firmware blob. Very simple implementation.

This is being CI tested on [0]

[0] https://github.com/u-boot/u-boot/pull/884

Changes v6:
- Rework to generic FW loader UCLASS
- Account for firmware offset in FIP loader
- Add UBI example in documentation
- Improve variables order
Changes v5:
- Fix unment dependency for config flag
- Move request_firmware_size before FIP loader
- Add documentation patch
- Improve get_fw_loader_from_node() implementation
- use min() instead of custom MIN()
- Fix handling of some return value
Changes v4:
- Rebase on top of next
Changes v3:
- Add review tag where possible
- Check blk_dread ret
- Generalize mount_ubifs with generic_fw_loader_ubi_select
- Fix some typo
- Drop useless ubifs umount (nothing is mounted in FIP) Changes v2:
- Better handle header include to fix compilation error on some devices
- Fix typo for ubifs fix commit

Christian Marangi (6):
misc: fs_loader: fix ubifs not unmounted on dev_get_priv error
misc: fs_loader: reorganize and split to FS loader and FW UCLASS
misc: fw_loader: implement generic get_fw_loader_from_node()
misc: fw_loader: implement request_firmware_size() OP
misc: fw_loader: introduce FIP loader driver
doc: dtbinding: Update documentation for Generic Firmware loader

 .../misc/{fs_loader.txt => fw_loader.txt}     |  29 +-
 drivers/misc/Kconfig                          |  16 +
 drivers/misc/Makefile                         |   2 +-
 drivers/misc/fs_loader.c                      | 326 -----------
 drivers/misc/fw_loader/Makefile               |   5 +
 drivers/misc/fw_loader/fip_loader.c           | 544 ++++++++++++++++++
 drivers/misc/fw_loader/fs_loader.c            | 230 ++++++++
 drivers/misc/fw_loader/fw_loader-uclass.c     | 192 +++++++
 drivers/misc/fw_loader/internal.h             |  82 +++
 include/dm/uclass-id.h                        |   2 +-
 include/fs_loader.h                           |  47 +-
 include/fw_loader.h                           |  42 ++
 12 files changed, 1141 insertions(+), 376 deletions(-)
 rename doc/device-tree-bindings/misc/{fs_loader.txt => fw_loader.txt} (60%)
 delete mode 100644 drivers/misc/fs_loader.c
 create mode 100644 drivers/misc/fw_loader/Makefile
 create mode 100644 drivers/misc/fw_loader/fip_loader.c
 create mode 100644 drivers/misc/fw_loader/fs_loader.c
 create mode 100644 drivers/misc/fw_loader/fw_loader-uclass.c
 create mode 100644 drivers/misc/fw_loader/internal.h
Signature · 1 line
2.53.0
#1 lore raw

PATCH v6 1/6 misc: fs_loader: fix ubifs not unmounted on dev_get_priv error

When dev_get_priv errors out, the ubifs is not umounted (if used).

Correctly handle this handle condition and while at it also return -EINVAL instead of -ENOMEM as a better error since no memory is allocated but is actually an invalid scenario for fw_get_filesystem_firmware().

---
 drivers/misc/fs_loader.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/misc/fs_loader.c +4 -2
index 6af4c7f15e7b..cd4695b08eea 100644
@@ -175,8 +175,10 @@ static int fw_get_filesystem_firmware(struct udevice *dev)
 
 	struct firmware *firmwarep = dev_get_priv(dev);
 
-	if (!firmwarep)
-		return -ENOMEM;
+	if (!firmwarep) {
+		ret = -EINVAL;
+		goto out;
+	}
 
 	ret = fs_read(firmwarep->name, (ulong)map_to_sysmem(firmwarep->data),
 			firmwarep->offset, firmwarep->size, &actread);
Signature · 1 line
2.53.0
#2 lore raw

PATCH v6 2/6 misc: fs_loader: reorganize and split to FS loader and FW UCLASS

In reply to Christian Marangi

In preparation to the introduction of variant of the FS loader, reorganize and split the driver to generic fw_loader UCLASS and specific fs_loader function. Create a dedicated directory for the loader and move the internal structs and functions to a dedicated header file. While at it also drop some redundant check and rename some variables as we are updating the code for the UCLASS rework.

This will permit to reuse all the property and logic of FS loader with container that are not exactly a readable filesystem.

---
 drivers/misc/Kconfig                      |   5 +
 drivers/misc/Makefile                     |   2 +-
 drivers/misc/fs_loader.c                  | 328 ----------------------
 drivers/misc/fw_loader/Makefile           |   4 +
 drivers/misc/fw_loader/fs_loader.c        | 186 ++++++++++++
 drivers/misc/fw_loader/fw_loader-uclass.c | 147 ++++++++++
 drivers/misc/fw_loader/internal.h         |  73 +++++
 include/dm/uclass-id.h                    |   2 +-
 include/fs_loader.h                       |  47 +---
 include/fw_loader.h                       |  19 ++
 10 files changed, 438 insertions(+), 375 deletions(-)
 delete mode 100644 drivers/misc/fs_loader.c
 create mode 100644 drivers/misc/fw_loader/Makefile
 create mode 100644 drivers/misc/fw_loader/fs_loader.c
 create mode 100644 drivers/misc/fw_loader/fw_loader-uclass.c
 create mode 100644 drivers/misc/fw_loader/internal.h
drivers/misc/Kconfig +5 -0
index b2dfc7f5b663..9b181339c468 100644
@@ -612,8 +612,12 @@ config MPC83XX_SERDES
 	help
 	  Support for serdes found on MPC83xx SoCs.
 
+config FW_LOADER_UCLASS
+	bool
+
 config FS_LOADER
 	bool "Enable loader driver for file system"
+	select FW_LOADER_UCLASS
 	help
 	  This is file system generic loader which can be used to load
 	  the file image from the storage into target such as memory.
@@ -623,6 +627,7 @@ config FS_LOADER
 
 config SPL_FS_LOADER
 	bool "Enable loader driver for file system in SPL"
+	select FW_LOADER_UCLASS
 	depends on SPL
 	help
 	  This is file system generic loader which can be used to load
drivers/misc/Makefile +1 -1
index e2170212e5ad..5a3e368767c2 100644
@@ -36,7 +36,7 @@ obj-$(CONFIG_FSL_DEVICE_DISABLE) += fsl_devdis.o
 obj-$(CONFIG_FSL_IFC) += fsl_ifc.o
 obj-$(CONFIG_FSL_IIM) += fsl_iim.o
 obj-$(CONFIG_FSL_SEC_MON) += fsl_sec_mon.o
-obj-$(CONFIG_$(PHASE_)FS_LOADER) += fs_loader.o
+obj-$(CONFIG_FW_LOADER_UCLASS) += fw_loader/
 obj-$(CONFIG_GATEWORKS_SC) += gsc.o
 obj-$(CONFIG_GDSYS_IOEP) += gdsys_ioep.o
 obj-$(CONFIG_GDSYS_RXAUI_CTRL) += gdsys_rxaui_ctrl.o
drivers/misc/fs_loader.c deleted +0 -328

328 lines not on this page · raw message

drivers/misc/fw_loader/Makefile new file +4 -0
new file mode 100644
index 000000000000..3d5e9f5b338e
@@ -0,0 +1,4 @@
+# SPDX-License-Identifier: GPL-2.0+
+
+obj-y += fw_loader-uclass.o
+obj-$(CONFIG_$(PHASE_)FS_LOADER) += fs_loader.o
drivers/misc/fw_loader/fs_loader.c new file +186 -0

186 lines not on this page · raw message

drivers/misc/fw_loader/fw_loader-uclass.c new file +147 -0

147 lines not on this page · raw message

drivers/misc/fw_loader/internal.h new file +73 -0

73 lines not on this page · raw message

include/dm/uclass-id.h +1 -1
index 36b5d87c304f..5ef3138e6ac7 100644
@@ -69,7 +69,7 @@ enum uclass_id {
 	UCLASS_FIRMWARE,	/* Firmware */
 	UCLASS_FPGA,		/* FPGA device */
 	UCLASS_FUZZING_ENGINE,	/* Fuzzing engine */
-	UCLASS_FS_FIRMWARE_LOADER,		/* Generic loader */
+	UCLASS_FIRMWARE_LOADER,	/* Firmware loader */
 	UCLASS_FWU_MDATA,	/* FWU Metadata Access */
 	UCLASS_GPIO,		/* Bank of general-purpose I/O pins */
 	UCLASS_HASH,		/* Hash device */
include/fs_loader.h +2 -45

54 lines not on this page · raw message

include/fw_loader.h +19 -0
index 35574482b2b9..56f5e3be6195 100644
@@ -1,10 +1,29 @@
 /* SPDX-License-Identifier: GPL-2.0 */
 /*
+ * Copyright (C) 2018-2019 Intel Corporation <www.intel.com>
  * Copyright (C) 2025 Lucien Jheng <[email protected]>
  */
 #ifndef _FW_LOADER_H_
 #define _FW_LOADER_H_
 
+struct udevice;
+
+/**
+ * request_firmware_into_buf - Load firmware into a previously allocated buffer.
+ * @dev: An instance of a driver.
+ * @name: Name of firmware file.
+ * @buf: Address of buffer to load firmware into.
+ * @size: Size of buffer.
+ * @offset: Offset of a file for start reading into buffer.
+ *
+ * The firmware is loaded directly into the buffer pointed to by @buf.
+ *
+ * Return: Size of total read, negative value when error.
+ */
+int request_firmware_into_buf(struct udevice *dev,
+			      const char *name,
+			      void *buf, size_t size, u32 offset);
+
 /**
  * request_firmware_into_buf_via_script() -
  * Load firmware using a U-Boot script and copy to buffer
Signature · 1 line
2.53.0
#3 lore raw

Re: [PATCH v6 2/6] misc: fs_loader: reorganize and split to FS loader and FW UCLASS

Hi Christian,

Quoted 29 lines · Christian Marangi

misc: fs_loader: reorganize and split to FS loader and FW UCLASS

In preparation to the introduction of variant of the FS loader, reorganize and split the driver to generic fw_loader UCLASS and specific fs_loader function. Create a dedicated directory for the loader and move the internal structs and functions to a dedicated header file. While at it also drop some redundant check and rename some variables as we are updating the code for the UCLASS rework.

This will permit to reuse all the property and logic of FS loader with container that are not exactly a readable filesystem.

drivers/misc/Kconfig | 5 +

 drivers/misc/Makefile                     |   2 +-
 drivers/misc/fs_loader.c                  | 328 ------------------------------
 drivers/misc/fw_loader/Makefile           |   4 +
 drivers/misc/fw_loader/fs_loader.c        | 186 +++++++++++++++++
 drivers/misc/fw_loader/fw_loader-uclass.c | 147 +++++++++++++
 drivers/misc/fw_loader/internal.h         |  73 +++++++
 include/dm/uclass-id.h                    |   2 +-
 include/fs_loader.h                       |  47 +----
 include/fw_loader.h                       |  19 ++
 10 files changed, 438 insertions(+), 375 deletions(-)
drivers/misc/fw_loader/fw_loader-uclass.c new file +0 -0
new file mode 100644

drivers/misc/fw_loader/fw_loader-uclass.c line 7

Quoted 7 lines
drivers/misc/fw_loader/fw_loader-uclass.c new file +7 -0
@@ -0,0 +1,147 @@
+UCLASS_DRIVER(fw_loader) = {
+     .id             = UCLASS_FIRMWARE_LOADER,
+     .name           = "fw_loader",
+     .pre_probe      = fw_loader_pre_probe,
+     .per_device_plat_auto = sizeof(struct device_plat),
+     .per_device_auto = sizeof(struct firmware),
+};

The uclass uses per_device_auto but the code uses dev_get_uclass_priv() to access it. Please can you use priv_auto in the driver, or per_device_auto with dev_get_uclass_auto() in the code. I suspect you meant the latter.

drivers/misc/fw_loader/fw_loader-uclass.c line 8

Quoted 10 lines
drivers/misc/fw_loader/fw_loader-uclass.c +8 -0
@@ -0,0 +1,147 @@
+static int fw_loader_pre_probe(struct udevice *dev)
+{
+     struct device_plat *plat = dev_get_uclass_plat(dev);
+     ofnode fw_loader_node;
+     int ret;
+
+     if (!IS_ENABLED(CONFIG_DM))
+             return 0;

The IS_ENABLED(CONFIG_DM) check is not needed since this code is inside a uclass — driver model must be enabled for this to compile or run.

Regards,
Simon

#4 lore raw

PATCH v6 3/6 misc: fw_loader: implement generic get_fw_loader_from_node()

In reply to Christian Marangi

Implement a generic function to get a FW loader dev from a node.

There is currently only get_fs_loader() but that is limited to chosen node and is limited only to FS loader.

Introduce get_fw_loader_from_node() that will parse the "firmware-loader" from a specified node and will search and probe a matching FW loader device, if found.

---
 drivers/misc/fw_loader/fw_loader-uclass.c | 23 +++++++++++++++++++++++
 include/fw_loader.h                       | 14 ++++++++++++++
 2 files changed, 37 insertions(+)
drivers/misc/fw_loader/fw_loader-uclass.c +23 -0
index 16a1fe2d4a65..c32c213b5b38 100644
@@ -9,6 +9,8 @@
 #include <linux/types.h>
 #include <dm.h>
 #include <dm/device.h>
+#include <dm/device-internal.h>
+#include <dm/uclass.h>
 #include <fw_loader.h>
 
 #ifdef CONFIG_CMD_UBIFS
@@ -96,6 +98,27 @@ UCLASS_DRIVER(fw_loader) = {
 	.per_device_auto = sizeof(struct firmware),
 };
 
+int get_fw_loader_from_node(ofnode node, struct udevice **dev)
+{
+	struct udevice *fw_dev;
+	int ret;
+
+	node = ofnode_parse_phandle(node, "firmware-loader", 0);
+	if (!ofnode_valid(node))
+		return -ENODEV;
+
+	ret = device_find_global_by_ofnode(node, &fw_dev);
+	if (ret)
+		return ret;
+
+	ret = device_probe(fw_dev);
+	if (ret)
+		return ret;
+
+	*dev = fw_dev;
+	return 0;
+}
+
 /**
  * _request_firmware_prepare - Prepare firmware struct.
  *
include/fw_loader.h +14 -0
index 56f5e3be6195..99c47380a172 100644
@@ -6,8 +6,22 @@
 #ifndef _FW_LOADER_H_
 #define _FW_LOADER_H_
 
+#include <dm/ofnode_decl.h>
+
 struct udevice;
 
+/**
+ * get_fw_loader_from_node - Get FW loader dev from @node.
+ *
+ * @node: ofnode where "firmware-loader" phandle is stored.
+ * @dev: pointer where to store the FW loader dev.
+ *
+ * Search and probe a matching FW loader device, if found.
+ *
+ * Return: Negative value if fail, 0 for successful.
+ */
+int get_fw_loader_from_node(ofnode node, struct udevice **dev);
+
 /**
  * request_firmware_into_buf - Load firmware into a previously allocated buffer.
  * @dev: An instance of a driver.
Signature · 1 line
2.53.0
#5 lore raw

Re: [PATCH v6 3/6] misc: fw_loader: implement generic get_fw_loader_from_node()

Hi Christian,

Quoted 23 lines · Christian Marangi

misc: fw_loader: implement generic get_fw_loader_from_node()

Implement a generic function to get a FW loader dev from a node.

There is currently only get_fs_loader() but that is limited to chosen node and is limited only to FS loader.

Introduce get_fw_loader_from_node() that will parse the "firmware-loader" from a specified node and will search and probe a matching FW loader device, if found.

drivers/misc/fw_loader/fw_loader-uclass.c | 23 +++++++++++++++++++++++

 include/fw_loader.h                       | 14 ++++++++++++++
 2 files changed, 37 insertions(+)
drivers/misc/fw_loader/fw_loader-uclass.c +4 -0
@@ -96,6 +98,27 @@ UCLASS_DRIVER(fw_loader) = {
+int get_fw_loader_from_node(ofnode node, struct udevice **dev)
+{
+     struct udevice *fw_dev;
+     int ret;

drivers/misc/fw_loader/fw_loader-uclass.c UCLASS_DRIVER(fw_loader) = { · line 111

Quoted 10 lines
drivers/misc/fw_loader/fw_loader-uclass.c +10 -0
+
+     node = ofnode_parse_phandle(node, "firmware-loader", 0);
+     if (!ofnode_valid(node))
+             return -ENODEV;
+
+     ret = device_find_global_by_ofnode(node, &fw_dev);
+     if (ret)
+             return ret;
+
+     ret = device_probe(fw_dev);

Using device_find_global_by_ofnode() followed by device_probe() does not verify that the device belongs to UCLASS_FIRMWARE_LOADER. If the device tree contains an erroneous phandle pointing to some other device type, this function would happily return it.

Please can you use uclass_get_device_by_ofnode(UCLASS_FIRMWARE_LOADER, node, dev) instead? That is the standard pattern for obtaining a device by ofnode within a specific uclass. See get_fs_loader() for an example.

Regards,
Simon

#6 lore raw

PATCH v6 4/6 misc: fw_loader: implement request_firmware_size() OP

In reply to Christian Marangi

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.

---
 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(-)
drivers/misc/fw_loader/fs_loader.c +56 -12
index 2694111d8396..8f549654a1be 100644
@@ -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[] = {
drivers/misc/fw_loader/fw_loader-uclass.c +19 -0
index c32c213b5b38..19379c23411b 100644
@@ -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);
+}
drivers/misc/fw_loader/internal.h +7 -0
index 2f93d0c706b6..9964dc436afb 100644
@@ -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)
include/fw_loader.h +9 -0
index 99c47380a172..01f242dcbefd 100644
@@ -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
Signature · 1 line
2.53.0
#7 lore raw

Re: [PATCH v6 4/6] misc: fw_loader: implement request_firmware_size() OP

Hi Christian,

Quoted 14 lines · Christian Marangi

misc: fw_loader: implement request_firmware_size() OP

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.

Quoted 7 lines

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(-)

The 'int' thing I mentioned in the last version is not that important, so this is fine.

drivers/misc/fw_loader/internal.h struct fw_loader_ops { · line 37

Quoted 8 lines
drivers/misc/fw_loader/internal.h +6 -0
@@ -32,6 +32,13 @@ struct fw_loader_ops {
+     /**
+      * get_size() - get firmware size from Firmware Loader driver
+      *
+      * @dev:        Firmware Loader device to get firmware size from
+      */
+     int (*get_size)(struct udevice *dev);

The kerneldoc is missing the Return: line documenting the return value (size of firmware or negative errno).

Regards,
Simon

#8 lore raw

PATCH v6 5/6 misc: fw_loader: introduce FIP loader driver

In reply to Christian Marangi

Introduce a variant of the FS loader driver to extract images from FIP image. These image can contain additional binary used to init Network accelerator or PHY firmware blob.

The way FIP handle image type is with the usage of UUID.

This FIP loader driver implement a simple FIP image parser that check every entry for a matching UUID.

Similar to FS loader, this driver also support both UBI and Block devices.

Also an additional property is added to handle special case with eMMC that doesn't have a GPT partition and require a global offset to reference the FIP partition.

An example usage of this driver is the following:

Entry in DTS:

	fs_loader0: fip-loader {
		bootph-all;
		compatible = "u-boot,fip-loader";
		phandlepart = <&mmc0 0>;
		partoffset = <0x100>;
	};

	ethernet@1fb50000 {
		firmware-loader = <&fs_loader0>;
	}

FIP loader user:

	/* get the FW loader from the ethernet node */
	get_fw_loader_from_node(dev_ofnode(dev), &fw_loader);

	/* read the blob identified by "58704aef-389f-3e52-b475-e0bf2234a6a2" UUID */
	request_firmware_into_buf(fw_loader, "58704aef-389f-3e52-b475-e0bf2234a6a2",
				  buf, 261400, 0);
---
 drivers/misc/Kconfig                      |  11 +
 drivers/misc/fw_loader/Makefile           |   1 +
 drivers/misc/fw_loader/fip_loader.c       | 544 ++++++++++++++++++++++
 drivers/misc/fw_loader/fw_loader-uclass.c |   3 +
 drivers/misc/fw_loader/internal.h         |   2 +
 5 files changed, 561 insertions(+)
 create mode 100644 drivers/misc/fw_loader/fip_loader.c
drivers/misc/Kconfig +11 -0
index 9b181339c468..ee0a9f40d99e 100644
@@ -615,6 +615,17 @@ config MPC83XX_SERDES
 config FW_LOADER_UCLASS
 	bool
 
+config FIP_LOADER
+	bool "Enable loader driver from FIP partition"
+	select LIB_UUID
+	select FW_LOADER_UCLASS
+	help
+	  This is FIP partition generic loader which can be used to load
+	  the file image from the FIP image into target such as memory.
+
+	  The consumer driver would then use this loader to program whatever,
+	  ie. the FPGA device/PHY firmware.
+
 config FS_LOADER
 	bool "Enable loader driver for file system"
 	select FW_LOADER_UCLASS
drivers/misc/fw_loader/Makefile +1 -0
index 3d5e9f5b338e..29418a008fb3 100644
@@ -1,4 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0+
 
 obj-y += fw_loader-uclass.o
+obj-$(CONFIG_FIP_LOADER) += fip_loader.o
 obj-$(CONFIG_$(PHASE_)FS_LOADER) += fs_loader.o
drivers/misc/fw_loader/fip_loader.c new file +544 -0

544 lines not on this page · raw message

drivers/misc/fw_loader/fw_loader-uclass.c +3 -0
index 19379c23411b..1d47c4437072 100644
@@ -62,6 +62,9 @@ static int fw_loader_pre_probe(struct udevice *dev)
 
 		plat->ubivol = (char *)ofnode_read_string(fw_loader_node,
 							  "ubivol");
+
+		ofnode_read_u32(fw_loader_node, "partoffset",
+				&plat->partoffset);
 	}
 
 	if (plat->phandlepart.phandle) {
drivers/misc/fw_loader/internal.h +2 -0
index 9964dc436afb..6003b15ab9ac 100644
@@ -49,11 +49,13 @@ struct fw_loader_ops {
  * This holds information about all supported storage devices for driver use.
  *
  * @phandlepart: Attribute data for block device.
+ * @partoffset: Global offset for BLK partition.
  * @mtdpart: MTD partition for ubi partition.
  * @ubivol: UBI volume-name for ubifsmount.
  */
 struct device_plat {
 	struct phandle_part phandlepart;
+	u32 partoffset;
 	char *mtdpart;
 	char *ubivol;
 };
Signature · 1 line
2.53.0
#9 lore raw

Re: [PATCH v6 5/6] misc: fw_loader: introduce FIP loader driver

Hi Christian,

Quoted 31 lines · Christian Marangi

misc: fw_loader: introduce FIP loader driver

Introduce a variant of the FS loader driver to extract images from FIP image. These image can contain additional binary used to init Network accelerator or PHY firmware blob.

The way FIP handle image type is with the usage of UUID.

This FIP loader driver implement a simple FIP image parser that check every entry for a matching UUID.

Similar to FS loader, this driver also support both UBI and Block devices.

Also an additional property is added to handle special case with eMMC that doesn't have a GPT partition and require a global offset to reference the FIP partition.

An example usage of this driver is the following:

[...]

drivers/misc/Kconfig | 11 +

 drivers/misc/fw_loader/Makefile           |   1 +
 drivers/misc/fw_loader/fip_loader.c       | 544 ++++++++++++++++++++++++++++++
 drivers/misc/fw_loader/fw_loader-uclass.c |   3 +
 drivers/misc/fw_loader/internal.h         |   2 +
 5 files changed, 561 insertions(+)

drivers/misc/fw_loader/fip_loader.c line 1

Quoted 1 line
drivers/misc/fw_loader/fip_loader.c +1 -0
@@ -0,0 +1,544 @@
+#include <fs.h>

Is this header actually used?

Quoted 12 lines
drivers/misc/fw_loader/fip_loader.c +10 -0
@@ -0,0 +1,544 @@
+static int blk_read_fip_toc_entry(struct blk_desc *desc, u32 offset,
+                               int pos, char *buf,
+                               struct fip_toc_entry *ent)
+{
+     unsigned long left, consumed, to_read, read = 0;
+     unsigned int blkstart, blkcnt;
+     int i, ret;
+
+     consumed = pos % desc->blksz;
+     left = desc->blksz - consumed;

drivers/misc/fw_loader/fip_loader.c line 20

Quoted 10 lines
drivers/misc/fw_loader/fip_loader.c +10 -0
+     to_read = min(left, (unsigned long)sizeof(*ent));
+
+     blkstart = BLOCK_CNT(pos, desc);
+     blkcnt = BLOCK_CNT(sizeof(*ent) - to_read, desc);
+
+     /* Read data from previous cached block if present */
+     if (left) {
+             memcpy(ent, buf + consumed, to_read);
+             read += to_read;
+     }

I suspect this condition is wrong. When pos is exactly at a block boundary (e.g. pos = 512 on a 512-byte block device), consumed is 0 and left is 512. The code then enters the branch and copies stale data from the buffer. The condition should probably be if (consumed) to match the logic in blk_read_fip_firmware() which correctly uses if (pos)

Regards,
Simon

#10 lore raw

PATCH v6 6/6 doc: dtbinding: Update documentation for Generic Firmware loader

In reply to Christian Marangi

Update documentation for Generic Firmware loader, generalize it from FS specific and add new property and example for FIP loader.

Also add details on the usage of 'phandlepart', 'mtdpart' and 'ubivol' property.

---
 .../misc/{fs_loader.txt => fw_loader.txt}     | 29 +++++++++++++++++--
 1 file changed, 26 insertions(+), 3 deletions(-)
 rename doc/device-tree-bindings/misc/{fs_loader.txt => fw_loader.txt} (60%)
doc/device-tree-bindings/misc/fw_loader.txt renamed from doc/device-tree-bindings/misc/fs_loader.txt +26 -3
similarity index 60%
rename from doc/device-tree-bindings/misc/fs_loader.txt
rename to doc/device-tree-bindings/misc/fw_loader.txt
index 542be4b25a0a..9b00b2c887c0 100644
@@ -1,17 +1,23 @@
-* File system firmware loader
+* Generic Firmware loader
 
 Required properties:
 --------------------
 
-- compatible: should contain "u-boot,fs-loader"
+- compatible: should contain "u-boot,fs-loader" or "u-boot,fip-loader"
 - phandlepart: which block storage device and partition the image loading from,
 	       this property is required for mmc, usb and sata. This is unsigned
 	       32-bit array. For example phandlepart=<&mmc_0 1>, meaning use
 	       that MMC0 node pointer, partition 1.
-- mdtpart: which partition of ubi the image loading from, this property is
+- mtdpart: which partition of ubi the image loading from, this property is
 	   required for ubi and mounting.
 - ubivol: which volume of ubi the image loading from, this property is required
 	  for ubi and mounting.
+- partoffset: valid ONLY for "u-boot,fip-loader". Offset of the partition to
+	      parse the FIP partition from.
+
+Either 'phandlepart' or 'mtdpart' can be used. For Block device, 'phandlepart'
+should be used and for MTD device, 'mtdpart' should be used. With UBI device,
+in addition to 'mtdpart' also 'ubivol' is required.
 
 Example of storage device and partition search set for mmc, usb, sata and
 ubi in device tree source as shown in below:
@@ -46,3 +52,20 @@ ubi in device tree source as shown in below:
 		mtdpart = "UBI",
 		ubivol = "ubi0";
 	};
+
+	Example for FIP from eMMC:
+	fs_loader4: fip-loader@4 {
+		bootph-all;
+		compatible = "u-boot,fip-loader";
+		phandlepart = <&mmc0 0>;
+		partoffset = <0x100>;
+	};
+
+	Example for FIP from ubi:
+	fs_loader4: fip-loader@4 {
+		bootph-all;
+		compatible = "u-boot,fip-loader";
+		mtdpart = "ubi",
+		ubivol = "bl31";
+	};
+
Signature · 1 line
2.53.0
#11 lore raw

Re: [PATCH v6 6/6] doc: dtbinding: Update documentation for Generic Firmware loader

Am 9. April 2026 15:33:02 MESZ schrieb Christian Marangi <[email protected]>:

Quoted 5 lines

Update documentation for Generic Firmware loader, generalize it from FS specific and add new property and example for FIP loader.

Also add details on the usage of 'phandlepart', 'mtdpart' and 'ubivol' property.

The sevice-tree documents should be integrated into the generated html documentation.

Shouldn't we use the same yaml schema format as the kernel to enable automated CI tests?

Best regards

Heinrich

Quoted 62 lines

---
.../misc/{fs_loader.txt => fw_loader.txt} | 29 +++++++++++++++++--

1 file changed, 26 insertions(+), 3 deletions(-)

rename doc/device-tree-bindings/misc/{fs_loader.txt => fw_loader.txt} (60%)

doc/device-tree-bindings/misc/fw_loader.txt renamed from doc/device-tree-bindings/misc/fs_loader.txt +1 -1
similarity index 60%
rename from doc/device-tree-bindings/misc/fs_loader.txt
rename to doc/device-tree-bindings/misc/fw_loader.txt
index 542be4b25a0a..9b00b2c887c0 100644
@@ -1,17 +1,23 @@
-* File system firmware loader
+* Generic Firmware loader
 

Required properties:
--------------------

-- compatible: should contain "u-boot,fs-loader"
+- compatible: should contain "u-boot,fs-loader" or "u-boot,fip-loader"
- phandlepart: which block storage device and partition the image loading from, this property is required for mmc, usb and sata. This is unsigned 32-bit array. For example phandlepart=<&mmc_0 1>, meaning use that MMC0 node pointer, partition 1.
-- mdtpart: which partition of ubi the image loading from, this property is +- mtdpart: which partition of ubi the image loading from, this property is
required for ubi and mounting.
- ubivol: which volume of ubi the image loading from, this property is required for ubi and mounting.
+- partoffset: valid ONLY for "u-boot,fip-loader". Offset of the partition to

doc/device-tree-bindings/misc/fw_loader.txt renamed from doc/device-tree-bindings/misc/fs_loader.txt +5 -0
+	      parse the FIP partition from.
+
+Either 'phandlepart' or 'mtdpart' can be used. For Block device, 'phandlepart'
+should be used and for MTD device, 'mtdpart' should be used. With UBI device,
+in addition to 'mtdpart' also 'ubivol' is required.

Example of storage device and partition search set for mmc, usb, sata and ubi in device tree source as shown in below:

doc/device-tree-bindings/misc/fw_loader.txt renamed from doc/device-tree-bindings/misc/fs_loader.txt +17 -0
@@ -46,3 +52,20 @@ ubi in device tree source as shown in below:
 		mtdpart = "UBI",
 		ubivol = "ubi0";
 	};
+
+	Example for FIP from eMMC:
+	fs_loader4: fip-loader@4 {
+		bootph-all;
+		compatible = "u-boot,fip-loader";
+		phandlepart = <&mmc0 0>;
+		partoffset = <0x100>;
+	};
+
+	Example for FIP from ubi:
+	fs_loader4: fip-loader@4 {
+		bootph-all;
+		compatible = "u-boot,fip-loader";
+		mtdpart = "ubi",
+		ubivol = "bl31";
+	};
+
#12 lore raw

Re: [PATCH v6 6/6] doc: dtbinding: Update documentation for Generic Firmware loader

In reply to Christian Marangi

Hi Christian,

Quoted 20 lines · Christian Marangi

doc: dtbinding: Update documentation for Generic Firmware loader

Update documentation for Generic Firmware loader, generalize it from FS specific and add new property and example for FIP loader.

Also add details on the usage of 'phandlepart', 'mtdpart' and 'ubivol' property.

.../misc/{fs_loader.txt => fw_loader.txt} | 29 +++++++++++++++++++---

 1 file changed, 26 insertions(+), 3 deletions(-)
doc/device-tree-bindings/misc/fw_loader.txt renamed from doc/device-tree-bindings/misc/fs_loader.txt +1 -1
@@ -1,17 +1,23 @@
-* File system firmware loader
+* Generic Firmware loader
 
 Required properties:
 --------------------

doc/device-tree-bindings/misc/fw_loader.txt line 15

Quoted 10 lines
doc/device-tree-bindings/misc/fw_loader.txt renamed from doc/device-tree-bindings/misc/fs_loader.txt +5 -1
 
-- compatible: should contain "u-boot,fs-loader"
+- compatible: should contain "u-boot,fs-loader" or "u-boot,fip-loader"
+- phandlepart: which block storage device and partition the image loading from,
...
+- mtdpart: which partition of ubi the image loading from, this property is
...
+- ubivol: which volume of ubi the image loading from, this property is required
...
+- partoffset: valid ONLY for "u-boot,fip-loader". Offset of the partition to

All these properties except compatible are listed under 'Required properties' but they are actually optional, right? Only one of phandlepart or mtdpart+ubivol is needed, and partoffset is FIP-specific. Please can you split this into 'Required properties' (just compatible) and 'Optional properties' sections?

Regards,
Simon

#13 lore raw
In reply to Christian Marangi

Hi Christian,

Quoted 2 lines · Christian Marangi

Rework the FS loader to permit reading firmware from container that are not exactly a readable filesystem.

The overall design looks reasonable. Splitting the generic firmware loader infrastructure from the filesystem-specific code makes sense, and the new FIP loader is a sensible addition for ARM platforms.

Quoted 1 line

Then the FIP loader as a basic parser of FIP.

Neither the existing FS loader nor the new FIP loader have any tests. Since you are adding new API entry points like get_fw_loader_from_node() please can you add a simple dm_test for the firmware loader uclass. Even a basic sandbox test exercising the ops callbacks would help catch regressions.

Regards,
Simon