Skip to contentSkip to conversation

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

PATCH New Christian Marangi 1 participant 10 messages Nov 17 Char and misc drivers

lore
Share & tools
lore
#0 lore raw

v12 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 BL33 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:

fw_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 = <&fw_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 v12:
- Rebase on top of next
- Improve log error for offset
- Add review-by tag from Simon
- Add additional documentation patch for develop
- Move DT patch after code change
Changes v11:
- Address request for test patch from v9
- Add additional test and comments
- Validate memory for FS test
- Fix defconfig order
Changes v10:
- Add review tag by Simon
- Use min_t for FIP driver
- Prevent double mount of ubi
Changes v9:
- Drop FIP blob in favor of dynamic generation
- Rename fs_loader to fw_loader
- Add additional patch for offset validation
- More use of lbaint_t
- Handle reading 0 for UBI
- Update MTD/UBI documentation and YAML conditions
- Make generic_fw_loader_get_phandle_desc handling explicit Changes v8:
- Address all the comments from Simon
- Massively improve test coverage for both FS and FIP
- Enable mmc node when needed for the tests
- Change FIP implementation to follow FS one
(read up to buf size or end of entry size) (complain when offset is bigger than size of entry)
- Update related documentation for fw loader
- Rebase on top of next
Changes v7:
- Add tests
- Rebase on top of next
- Fix compilation for vpl
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 (9):
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: fs_loader: validate requested offset on reading file
misc: fw_loader: implement request_firmware_size() OP
misc: fw_loader: introduce FIP loader driver
doc: dtbinding: Update documentation for Generic Firmware loader
doc: develop/driver-model: firmware_loader: document new FS and FIP
API
test: dm: Add test for FW Loader feature

 arch/sandbox/dts/test.dts                     |  38 ++
 configs/sandbox64_defconfig                   |   2 +
 configs/sandbox_defconfig                     |   2 +
 configs/sandbox_flattree_defconfig            |   2 +
 configs/sandbox_noinst_defconfig              |   2 +
 configs/sandbox_spl_defconfig                 |   4 +
 configs/sandbox_vpl_defconfig                 |   4 +
 ...irmware_loader.rst => firmware_loader.rst} | 101 +++-
 doc/develop/driver-model/index.rst            |   2 +-
 doc/device-tree-bindings/misc/fs_loader.txt   |  48 --
 doc/device-tree-bindings/misc/fw_loader.yaml  | 112 ++++
 drivers/misc/Kconfig                          |  31 +
 drivers/misc/Makefile                         |   2 +-
 drivers/misc/fs_loader.c                      | 326 ----------
 drivers/misc/fw_loader/Makefile               |   5 +
 drivers/misc/fw_loader/fip_loader.c           | 559 ++++++++++++++++++
 drivers/misc/fw_loader/fs_loader.c            | 261 ++++++++
 drivers/misc/fw_loader/fw_loader-uclass.c     | 231 ++++++++
 drivers/misc/fw_loader/internal.h             | 100 ++++
 include/dm/uclass-id.h                        |   2 +-
 include/fs_loader.h                           |  47 +-
 include/fw_loader.h                           |  42 ++
 test/dm/Makefile                              |   1 +
 test/dm/fw_loader.c                           | 254 ++++++++
 test/py/tests/test_ut.py                      |  58 ++
 25 files changed, 1802 insertions(+), 434 deletions(-)
 rename doc/develop/driver-model/{fs_firmware_loader.rst => firmware_loader.rst} (60%)
 delete mode 100644 doc/device-tree-bindings/misc/fs_loader.txt
 create mode 100644 doc/device-tree-bindings/misc/fw_loader.yaml
 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
 create mode 100644 test/dm/fw_loader.c
Signature · 1 line
2.55.0
#1 lore raw

PATCH v12 1/9 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.55.0
#2 lore raw

PATCH v12 2/9 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.
Doc are renamed and updated to a more generic name and for the new UCLASS.

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

---
 ...irmware_loader.rst => firmware_loader.rst} |   6 +-
 doc/develop/driver-model/index.rst            |   2 +-
 drivers/misc/Kconfig                          |   8 +
 drivers/misc/Makefile                         |   2 +-
 drivers/misc/fs_loader.c                      | 328 ------------------
 drivers/misc/fw_loader/Makefile               |   4 +
 drivers/misc/fw_loader/fs_loader.c            | 175 ++++++++++
 drivers/misc/fw_loader/fw_loader-uclass.c     | 188 ++++++++++
 drivers/misc/fw_loader/internal.h             |  91 +++++
 include/dm/uclass-id.h                        |   2 +-
 include/fs_loader.h                           |  47 +--
 include/fw_loader.h                           |  19 +
 12 files changed, 493 insertions(+), 379 deletions(-)
 rename doc/develop/driver-model/{fs_firmware_loader.rst => firmware_loader.rst} (95%)
 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
doc/develop/driver-model/firmware_loader.rst renamed from doc/develop/driver-model/fs_firmware_loader.rst +3 -3
similarity index 95%
rename from doc/develop/driver-model/fs_firmware_loader.rst
rename to doc/develop/driver-model/firmware_loader.rst
index 630daf636f18..2d11a74d5e2f 100644
@@ -4,7 +4,7 @@
 File System Firmware Loader
 ===========================
 
-This is file system firmware loader for U-Boot framework, which has very close
+This is firmware loader for U-Boot framework, which has very close
 to some Linux Firmware API. For the details of Linux Firmware API, you can refer
 to https://docs.kernel.org/driver-api/firmware/index.html.
 
@@ -87,7 +87,7 @@ For example of getting DT phandle from /chosen and creating instance:
 	}
 
 	phandle = fdt32_to_cpu(*phandle_p);
-	ret = uclass_get_device_by_phandle_id(UCLASS_FS_FIRMWARE_LOADER,
+	ret = uclass_get_device_by_phandle_id(UCLASS_FIRMWARE_LOADER,
 					     phandle, &dev);
 	if (ret)
 		return ret;
@@ -145,7 +145,7 @@ instance:
 
 .. code-block:: c
 
-	ret = uclass_get_device_by_phandle_id(UCLASS_FS_FIRMWARE_LOADER,
+	ret = uclass_get_device_by_phandle_id(UCLASS_FIRMWARE_LOADER,
 					     phandle, &dev);
 	if (ret)
 		return ret;
doc/develop/driver-model/index.rst +1 -1
index 8e12bbd9366a..b2d46da4e239 100644
@@ -15,7 +15,7 @@ subsystems
    design
    ethernet
    fdt-fixup
-   fs_firmware_loader
+   firmware_loader
    i2c-howto
    livetree
    migration
drivers/misc/Kconfig +8 -0
index 05e67c8d5668..8ed4fba47834 100644
@@ -642,8 +642,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.
@@ -651,8 +655,12 @@ config FS_LOADER
 	  The consumer driver would then use this loader to program whatever,
 	  ie. the FPGA device.
 
+config SPL_FW_LOADER_UCLASS
+	bool
+
 config SPL_FS_LOADER
 	bool "Enable loader driver for file system in SPL"
+	select SPL_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 e68bbe34ab10..390907ba7e12 100644
@@ -37,7 +37,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_$(PHASE_)FW_LOADER_UCLASS) += fw_loader/
 obj-$(CONFIG_GATEWORKS_SC) += gsc.o
 obj-$(CONFIG_GXP_EEPROM) += gxp_eeprom.o
 obj-$(CONFIG_GDSYS_IOEP) += gdsys_ioep.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..f5df0db61c22
@@ -0,0 +1,4 @@
+# SPDX-License-Identifier: GPL-2.0+
+
+obj-$(CONFIG_$(PHASE_)FW_LOADER_UCLASS) += fw_loader-uclass.o
+obj-$(CONFIG_$(PHASE_)FS_LOADER) += fs_loader.o
drivers/misc/fw_loader/fs_loader.c new file +175 -0

175 lines not on this page · raw message

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

188 lines not on this page · raw message

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

91 lines not on this page · raw message

include/dm/uclass-id.h +1 -1
index d4822177764f..dcb60449460f 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.55.0
#3 lore raw

PATCH v12 3/9 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 | 24 +++++++++++++++++++++++
 include/fw_loader.h                       | 14 +++++++++++++
 2 files changed, 38 insertions(+)
drivers/misc/fw_loader/fw_loader-uclass.c +24 -0
index 0d95fac8f42b..7f19d6c4af4c 100644
@@ -7,6 +7,8 @@
 #include <blk.h>
 #include <dm.h>
 #include <dm/device.h>
+#include <dm/device-internal.h>
+#include <dm/uclass.h>
 #include <env.h>
 #include <errno.h>
 #include <fw_loader.h>
@@ -137,6 +139,28 @@ 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 = uclass_get_device_by_ofnode(UCLASS_FIRMWARE_LOADER,
+					  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.55.0
#4 lore raw

PATCH v12 4/9 misc: fs_loader: validate requested offset on reading file

In reply to Christian Marangi

Make sure we are not requesting an offset for the requesting file beyond the size of the file.

If it's such the case, log an error and return -EINVAL.

Code is reworked and split in various subfunction to permit re-preparing the filesystem after a fs_close (called by fs_size) and reduce patch delta of upcoming support for reading ONLY the size of the requested firmware.

Dedicated mount function are created to mount relevant filesystem only once and follow the same if-else logic pattern used by the prepare functions.

---
 drivers/misc/fw_loader/fs_loader.c | 98 ++++++++++++++++++++++++------
 1 file changed, 78 insertions(+), 20 deletions(-)
drivers/misc/fw_loader/fs_loader.c +78 -20
index b1eb0311dd2b..01b0a221c8a7 100644
@@ -49,6 +49,16 @@ static int mount_ubifs(char *mtdpart, char *ubivol)
 }
 #endif
 
+static int mount_fs_dev(struct fw_loader_plat *plat)
+{
+	if (plat->phandlepart.phandle)
+		return 0;
+	else if (plat->mtdpart && plat->ubivol)
+		return mount_ubifs(plat->mtdpart, plat->ubivol);
+	else
+		return 0;
+}
+
 static int select_fs_dev(struct fw_loader_plat *plat)
 {
 	int ret;
@@ -63,10 +73,6 @@ static int select_fs_dev(struct fw_loader_plat *plat)
 			ret = fs_set_blk_dev_with_part(desc,
 				plat->phandlepart.partition);
 	} else if (plat->mtdpart && plat->ubivol) {
-		ret = mount_ubifs(plat->mtdpart, plat->ubivol);
-		if (ret)
-			return ret;
-
 		ret = fs_set_blk_dev("ubi", NULL, FS_TYPE_UBIFS);
 	} else {
 		debug("Error: unsupported storage device.\n");
@@ -79,18 +85,26 @@ static int select_fs_dev(struct fw_loader_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_mount(struct udevice *dev)
+{
+	struct fw_loader_plat *plat = dev_get_uclass_plat(dev);
+	struct fw_loader_env_info info = { };
+
+	generic_fw_loader_env_info(&info);
+
+	if (info.storage_interface && info.dev_part)
+		return 0;
+	else if (info.storage_interface && info.ubi_mtdpart &&
+		 info.ubi_volume)
+		return mount_ubifs(info.ubi_mtdpart, info.ubi_volume);
+	else
+		return mount_fs_dev(plat);
+}
+
+static int fw_get_filesystem_prepare(struct udevice *dev)
 {
-	struct firmware *upriv = dev_get_uclass_priv(dev);
 	struct fw_loader_plat *plat = dev_get_uclass_plat(dev);
 	struct fw_loader_env_info info = { };
-	loff_t actread = 0;
 	int ret;
 
 	generic_fw_loader_env_info(&info);
@@ -100,10 +114,6 @@ static int fw_get_filesystem_firmware(struct udevice *dev)
 				     info.dev_part, FS_TYPE_ANY);
 	} else if (info.storage_interface && info.ubi_mtdpart &&
 		   info.ubi_volume) {
-		ret = mount_ubifs(info.ubi_mtdpart, info.ubi_volume);
-		if (ret)
-			return ret;
-
 		if (!strcmp("ubi", info.storage_interface))
 			ret = fs_set_blk_dev(info.storage_interface, NULL,
 					     FS_TYPE_UBIFS);
@@ -113,6 +123,56 @@ 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
+}
+
+static int __fw_get_filesystem_firmware_size(struct udevice *dev,
+					     const char *name, loff_t *size)
+{
+	int ret;
+
+	ret = fw_get_filesystem_prepare(dev);
+	if (ret)
+		return ret;
+
+	return fs_size(name, size);
+}
+
+/**
+ * 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 size, actread = 0;
+	int ret;
+
+	ret = fw_get_filesystem_mount(dev);
+	if (ret)
+		goto out;
+
+	ret = __fw_get_filesystem_firmware_size(dev, upriv->name, &size);
+	if (ret)
+		goto out;
+
+	if (upriv->offset > size) {
+		log_err("Offset %#x beyond file size %#llx\n",
+			upriv->offset, size);
+		ret = -EINVAL;
+		goto out;
+	}
+
+	ret = fw_get_filesystem_prepare(dev);
 	if (ret)
 		goto out;
 
@@ -127,9 +187,7 @@ static int fw_get_filesystem_firmware(struct udevice *dev)
 	}
 
 out:
-#ifdef CONFIG_CMD_UBIFS
-	umount_ubifs();
-#endif
+	fw_get_filesystem_release(dev);
 	return ret;
 }
 
Signature · 1 line
2.55.0
#5 lore raw

PATCH v12 5/9 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        | 28 +++++++++++++++++++++++
 drivers/misc/fw_loader/fw_loader-uclass.c | 19 +++++++++++++++
 drivers/misc/fw_loader/internal.h         |  7 ++++++
 include/fw_loader.h                       |  9 ++++++++
 4 files changed, 63 insertions(+)
drivers/misc/fw_loader/fs_loader.c +28 -0
index 01b0a221c8a7..00f72ae24233 100644
@@ -191,8 +191,36 @@ out:
 	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;
+	int ret;
+
+	ret = fw_get_filesystem_mount(dev);
+	if (ret)
+		goto out;
+
+	ret = __fw_get_filesystem_firmware_size(dev, upriv->name, &size);
+	if (ret)
+		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 7f19d6c4af4c..4e67f2559fa2 100644
@@ -210,3 +210,22 @@ int request_firmware_into_buf(struct udevice *dev,
 
 	return ops->get_firmware(dev);
 }
+
+int request_firmware_size(struct udevice *dev, const char *name)
+{
+	const 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 9ff43e872554..1abe7504a81e 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)	((const 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.55.0
#6 lore raw

PATCH v12 6/9 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:

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

	ethernet@1fb50000 {
		firmware-loader = <&fw_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                |  23 ++
 drivers/misc/fw_loader/Makefile     |   1 +
 drivers/misc/fw_loader/fip_loader.c | 559 ++++++++++++++++++++++++++++
 drivers/misc/fw_loader/internal.h   |   2 +
 4 files changed, 585 insertions(+)
 create mode 100644 drivers/misc/fw_loader/fip_loader.c
drivers/misc/Kconfig +23 -0
index 8ed4fba47834..d7163a96ea15 100644
@@ -645,6 +645,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
@@ -658,6 +669,18 @@ config FS_LOADER
 config SPL_FW_LOADER_UCLASS
 	bool
 
+config SPL_FIP_LOADER
+	bool "Enable loader driver from FIP partition in SPL"
+	select LIB_UUID
+	select SPL_FW_LOADER_UCLASS
+	depends on SPL
+	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 SPL_FS_LOADER
 	bool "Enable loader driver for file system in SPL"
 	select SPL_FW_LOADER_UCLASS
drivers/misc/fw_loader/Makefile +1 -0
index f5df0db61c22..120722c4ed17 100644
@@ -1,4 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0+
 
 obj-$(CONFIG_$(PHASE_)FW_LOADER_UCLASS) += fw_loader-uclass.o
+obj-$(CONFIG_$(PHASE_)FIP_LOADER) += fip_loader.o
 obj-$(CONFIG_$(PHASE_)FS_LOADER) += fs_loader.o
drivers/misc/fw_loader/fip_loader.c new file +559 -0

559 lines not on this page · raw message

drivers/misc/fw_loader/internal.h +2 -0
index 1abe7504a81e..5af061321512 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 fw_loader_plat {
 	struct phandle_part phandlepart;
+	u32 partoffset;
 	char *mtdpart;
 	char *ubivol;
 };
Signature · 1 line
2.55.0
#7 lore raw

PATCH v12 7/9 doc: dtbinding: Update documentation for Generic Firmware loader

In reply to Christian Marangi

Update documentation for Generic Firmware loader to YAML schema, 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.

---
 doc/device-tree-bindings/misc/fs_loader.txt  |  48 --------
 doc/device-tree-bindings/misc/fw_loader.yaml | 112 +++++++++++++++++++
 2 files changed, 112 insertions(+), 48 deletions(-)
 delete mode 100644 doc/device-tree-bindings/misc/fs_loader.txt
 create mode 100644 doc/device-tree-bindings/misc/fw_loader.yaml
doc/device-tree-bindings/misc/fs_loader.txt deleted +0 -48
deleted file mode 100644
index 542be4b25a0a..000000000000
@@ -1,48 +0,0 @@
-* File system firmware loader
-
-Required properties:
---------------------
-
-- compatible: should contain "u-boot,fs-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
-	   required for ubi and mounting.
-- ubivol: which volume of ubi the image loading from, this property is required
-	  for ubi and mounting.
-
-Example of storage device and partition search set for mmc, usb, sata and
-ubi in device tree source as shown in below:
-
-	Example of storage type and device partition search set for mmc, usb,
-	sata and ubi as shown in below:
-	Example for mmc:
-	fs_loader0: fs-loader@0 {
-		bootph-all;
-		compatible = "u-boot,fs-loader";
-		phandlepart = <&mmc_0 1>;
-	};
-
-	Example for usb:
-	fs_loader1: fs-loader@1 {
-		bootph-all;
-		compatible = "u-boot,fs-loader";
-		phandlepart = <&usb0 1>;
-	};
-
-	Example for sata:
-	fs_loader2: fs-loader@2 {
-		bootph-all;
-		compatible = "u-boot,fs-loader";
-		phandlepart = <&sata0 1>;
-	};
-
-	Example for ubi:
-	fs_loader3: fs-loader@3 {
-		bootph-all;
-		compatible = "u-boot,fs-loader";
-		mtdpart = "UBI",
-		ubivol = "ubi0";
-	};
doc/device-tree-bindings/misc/fw_loader.yaml new file +112 -0

112 lines not on this page · raw message

Signature · 1 line
2.55.0
#8 lore raw

PATCH v12 8/9 doc: develop/driver-model: firmware_loader: document new FS and FIP API

In reply to Christian Marangi

Update the firmware loader documentation with the new get_fw_loader_from_node() and request_firmware_size() API and the new FIP config.

Provide additional example and description of the new suggested order of operation on dynamic buffer allocation with request_firmware_size() and how to reach a firmware loader device.

---
 doc/develop/driver-model/firmware_loader.rst | 95 ++++++++++++++++++--
 1 file changed, 86 insertions(+), 9 deletions(-)
doc/develop/driver-model/firmware_loader.rst +86 -9
index 2d11a74d5e2f..d4e8063e9d72 100644
@@ -1,7 +1,7 @@
 .. SPDX-License-Identifier: GPL-2.0+
 .. Copyright (C) 2018-2019 Intel Corporation <www.intel.com>
 
-File System Firmware Loader
+Firmware Loader
 ===========================
 
 This is firmware loader for U-Boot framework, which has very close
@@ -13,9 +13,17 @@ and binary) from the storage device in file system format into target location
 such as memory, then consumer driver such as FPGA driver can program FPGA image
 from the target location into FPGA.
 
-To enable firmware loader, CONFIG_FS_LOADER need to be set at
+FIP firmware loader can be used to load a firmware from a specific FIP entry
+referenced by the specific UUID into target location such as memory, then
+consumer driver such as PHY driver can load such firmware to make the PHY
+operational.
+
+To enable file system firmware loader, CONFIG_FS_LOADER need to be set at
 <board_name>_defconfig such as "CONFIG_FS_LOADER=y".
 
+To enable FIP firmware loader, CONFIG_FIP_LOADER need to be set at
+<board_name>_defconfig such as "CONFIG_FIP_LOADER=y".
+
 Firmware Loader API core features
 ---------------------------------
 
@@ -23,11 +31,11 @@ Firmware storage device described in device tree source
 -------------------------------------------------------
 For passing data like storage device phandle and partition where the
 firmware loading from to the firmware loader driver, those data could be
-defined in fs-loader node as shown in below:
+defined in fw-loader node as shown in below:
 
 Example for block device::
 
-	fs_loader0: fs-loader {
+	fw_loader0: fs-loader {
 		bootph-all;
 		compatible = "u-boot,fs-loader";
 		phandlepart = <&mmc 1>;
@@ -40,20 +48,31 @@ device, it can be described in FDT as shown in below:
 
 Example for ubi::
 
-	fs_loader1: fs-loader {
+	fw_loader1: fs-loader {
 		bootph-all;
 		compatible = "u-boot,fs-loader";
 		mtdpart = "UBI",
 		ubivol = "ubi0";
 	};
 
+(FIP loader follow the same exact implementation by just using the
+dedicated compatible "u-boo,fip-loader")
+
 Then, firmware-loader property can be added with any device node, which
 driver would use the firmware loader for loading.
 
 The value of the firmware-loader property should be set with phandle
-of the fs-loader node. For example::
+of the fw-loader node. For example::
 
-	firmware-loader = <&fs_loader0>;
+	firmware-loader = <&fw_loader0>;
+
+And firmware loader device can be reached by the use get_fw_loader_from_node API:
+
+For example:
+
+.. code-block:: c
+
+	get_fw_loader_from_node(dev_ofnode(dev), &fw_loader);
 
 If there are majority of devices using the same fs-loader node, then
 firmware-loader property can be added under /chosen node instead of
@@ -63,7 +82,7 @@ For example::
 
 	/{
 		chosen {
-			firmware-loader = <&fs_loader0>;
+			firmware-loader = <&fw_loader0>;
 		};
 	};
 
@@ -117,6 +136,44 @@ compilation is required for both driver and FDT.
 File system firmware Loader API
 -------------------------------
 
+.. code-block:: c
+
+	int get_fw_loader_from_node(ofnode node, struct udevice **dev);
+
+Get firmware loader from referenced node
+
+Parameters:
+
+* ofnode \*node: node where the "firmware-loader" phandle needs to be parsed
+* struct udevice \**dev:  pointer where to store the firmware loader device
+
+Returns:
+	0 on success
+	-ve when error
+
+Description:
+	Parse the "firmware-loader" phandle from the passed node and
+	probe and put the matching firmware loader device in the dev pointer.
+
+.. code-block:: c
+
+	int request_firmware_size(struct udevice *dev, const char *name)
+
+Get firmware size from Firmware Loader driver
+
+Parameters:
+
+* struct udevice \*dev: An instance of a driver
+* const char \*name: name of firmware file
+
+Returns:
+	size of firmware
+	-ve when error
+
+Description:
+	Get the firmware size before reading it to validate how much buffer
+	is needed for it
+
 .. code-block:: c
 
 	int request_firmware_into_buf(struct udevice *dev,
@@ -151,4 +208,24 @@ instance:
 		return ret;
 
 	request_firmware_into_buf(dev, filename, buffer_location, buffer_size,
-				 offset_ofreading);
+				  offset_ofreading);
+
+Example of request_firmware_into_buf API from a "firmware-loader" defined in
+the device node and dynamic buffer allocation:
+
+.. code-block:: c
+
+	ret = get_fw_loader_from_node(dev_ofnode(dev), &fw_loader);
+	if (ret)
+		return ret;
+
+	buffer_size = request_firmware_size(fw_loader, filename);
+	if (buffer_size <= 0)
+		return buffer_size < 0 ? buffer_size : -EINVAL;
+
+	buffer_location = calloc(buffer_size, sizeof(*buffer_location));
+	if (!buffer_location)
+		return -ENOMEM;
+
+	request_firmware_into_buf(fw_loader, filename, buffer_location, buffer_size,
+				  offset_ofreading);
Signature · 1 line
2.55.0
#9 lore raw

PATCH v12 9/9 test: dm: Add test for FW Loader feature

In reply to Christian Marangi

Add basic test for FW Loader feature for the Filesystem way and FIP.

Additional MMC testing image are added that loads a dummy firmware.bin in a fat partition or a simple FIP blob.

---
 arch/sandbox/dts/test.dts          |  38 +++++
 configs/sandbox64_defconfig        |   2 +
 configs/sandbox_defconfig          |   2 +
 configs/sandbox_flattree_defconfig |   2 +
 configs/sandbox_noinst_defconfig   |   2 +
 configs/sandbox_spl_defconfig      |   4 +
 configs/sandbox_vpl_defconfig      |   4 +
 test/dm/Makefile                   |   1 +
 test/dm/fw_loader.c                | 254 +++++++++++++++++++++++++++++
 test/py/tests/test_ut.py           |  58 +++++++
 10 files changed, 367 insertions(+)
 create mode 100644 test/dm/fw_loader.c
arch/sandbox/dts/test.dts +38 -0
index 02a9ca29b792..bcdfeee91b9d 100644
@@ -190,6 +190,18 @@
 		};
 	};
 
+	fs_loader: fs-loader {
+		bootph-all;
+		compatible = "u-boot,fs-loader";
+		phandlepart = <&mmc13 1>;
+	};
+
+	fip_loader: fip-loader {
+		bootph-all;
+		compatible = "u-boot,fip-loader";
+		phandlepart = <&mmc14 1>;
+	};
+
 	cedit: cedit {
 	};
 
@@ -1406,6 +1418,20 @@
 		filename = "mmc12.img";
 	};
 
+	/* This is used for FS Loader tests */
+	mmc13: mmc13 {
+		status = "disabled";
+		compatible = "sandbox,mmc";
+		filename = "mmc13.img";
+	};
+
+	/* This is used for FIP Loader tests */
+	mmc14: mmc14 {
+		status = "disabled";
+		compatible = "sandbox,mmc";
+		filename = "mmc14.img";
+	};
+
 	pch {
 		compatible = "sandbox,pch";
 	};
@@ -2165,6 +2191,18 @@
 		ethphy1: ethernet-phy@1 {
 			reg = <1>;
 		};
+
+		ethernet-phy@2 {
+			reg = <2>;
+
+			firmware-loader = <&fs_loader>;
+		};
+
+		ethernet-phy@3 {
+			reg = <3>;
+
+			firmware-loader = <&fip_loader>;
+		};
 	};
 
 	pm-bus-test {
configs/sandbox64_defconfig +2 -0
index c54e1d8141b8..820cc12c19b9 100644
@@ -176,6 +176,8 @@ CONFIG_CROS_EC_SPI=y
 CONFIG_P2SB=y
 CONFIG_PWRSEQ=y
 CONFIG_I2C_EEPROM=y
+CONFIG_FIP_LOADER=y
+CONFIG_FS_LOADER=y
 CONFIG_MMC_SANDBOX=y
 CONFIG_DM_MTD=y
 CONFIG_MTD_RAW_NAND=y
configs/sandbox_defconfig +2 -0
index 9bc64d898fac..f61932992361 100644
@@ -261,6 +261,8 @@ CONFIG_CROS_EC_SPI=y
 CONFIG_P2SB=y
 CONFIG_PWRSEQ=y
 CONFIG_I2C_EEPROM=y
+CONFIG_FIP_LOADER=y
+CONFIG_FS_LOADER=y
 CONFIG_SPI_EEPROM=y
 CONFIG_MMC_PCI=y
 CONFIG_MMC_SANDBOX=y
configs/sandbox_flattree_defconfig +2 -0
index 0b048ac085b7..d1a61538317d 100644
@@ -139,6 +139,8 @@ CONFIG_CROS_EC_SPI=y
 CONFIG_P2SB=y
 CONFIG_PWRSEQ=y
 CONFIG_I2C_EEPROM=y
+CONFIG_FIP_LOADER=y
+CONFIG_FS_LOADER=y
 CONFIG_MMC_SANDBOX=y
 CONFIG_SPI_FLASH_SANDBOX=y
 CONFIG_BOOTDEV_SPI_FLASH=y
configs/sandbox_noinst_defconfig +2 -0
index 7d5c91cf30c1..6cb42561d8cd 100644
@@ -174,7 +174,9 @@ CONFIG_CROS_EC_SPI=y
 CONFIG_P2SB=y
 CONFIG_PWRSEQ=y
 CONFIG_SPL_PWRSEQ=y
+CONFIG_FIP_LOADER=y
 CONFIG_FS_LOADER=y
+CONFIG_SPL_FIP_LOADER=y
 CONFIG_SPL_FS_LOADER=y
 CONFIG_MMC_SANDBOX=y
 CONFIG_DM_MTD=y
configs/sandbox_spl_defconfig +4 -0
index 83808c94283f..9b4d7245af1b 100644
@@ -154,6 +154,10 @@ CONFIG_P2SB=y
 CONFIG_PWRSEQ=y
 CONFIG_SPL_PWRSEQ=y
 CONFIG_I2C_EEPROM=y
+CONFIG_FIP_LOADER=y
+CONFIG_FS_LOADER=y
+CONFIG_SPL_FIP_LOADER=y
+CONFIG_SPL_FS_LOADER=y
 CONFIG_MMC_SANDBOX=y
 CONFIG_SPI_FLASH_SANDBOX=y
 CONFIG_SPI_FLASH_ATMEL=y
configs/sandbox_vpl_defconfig +4 -0
index 1aa4f37b8e93..a050fe83ba0b 100644
@@ -161,6 +161,10 @@ CONFIG_CROS_EC_SPI=y
 CONFIG_P2SB=y
 CONFIG_PWRSEQ=y
 CONFIG_SPL_PWRSEQ=y
+CONFIG_FIP_LOADER=y
+CONFIG_FS_LOADER=y
+CONFIG_SPL_FIP_LOADER=y
+CONFIG_SPL_FS_LOADER=y
 CONFIG_MMC_SANDBOX=y
 CONFIG_SPI_FLASH_SANDBOX=y
 CONFIG_SPI_FLASH_ATMEL=y
test/dm/Makefile +1 -0
index 112326c85bbb..12066af6c3e8 100644
@@ -78,6 +78,7 @@ obj-$(CONFIG_EXTCON) += extcon.o
 ifneq ($(CONFIG_EFI_PARTITION),)
 obj-$(CONFIG_FASTBOOT_FLASH_MMC) += fastboot.o
 endif
+obj-$(CONFIG_FW_LOADER_UCLASS) += fw_loader.o
 obj-$(CONFIG_FIRMWARE) += firmware.o
 obj-$(CONFIG_DM_FPGA) += fpga.o
 obj-$(CONFIG_FWU_MDATA_GPT_BLK) += fwu_mdata.o
test/dm/fw_loader.c new file +254 -0

254 lines not on this page · raw message

test/py/tests/test_ut.py +58 -0

76 lines not on this page · raw message

Signature · 1 line
2.55.0