misc: fs_loader: reorg and split to FS and FW loader + FIP loader
v5 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 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 and FW loader
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} | 15 +-
drivers/misc/Kconfig | 16 +
drivers/misc/Makefile | 2 +-
drivers/misc/fw_loader/Makefile | 5 +
drivers/misc/fw_loader/fip_loader.c | 578 ++++++++++++++++++
drivers/misc/{ => fw_loader}/fs_loader.c | 193 ++----
drivers/misc/fw_loader/fw_loader.c | 196 ++++++
drivers/misc/fw_loader/internal.h | 64 ++
include/dm/uclass-id.h | 3 +-
include/fs_loader.h | 47 +-
include/fw_loader.h | 43 ++
11 files changed, 983 insertions(+), 179 deletions(-)
rename doc/device-tree-bindings/misc/{fs_loader.txt => fw_loader.txt} (77%)
create mode 100644 drivers/misc/fw_loader/Makefile
create mode 100644 drivers/misc/fw_loader/fip_loader.c
rename drivers/misc/{ => fw_loader}/fs_loader.c (59%)
create mode 100644 drivers/misc/fw_loader/fw_loader.c
create mode 100644 drivers/misc/fw_loader/internal.hSignature · 1 line
2.53.0
PATCH v5 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
@@ -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
PATCH v5 2/6 misc: fs_loader: reorganize and split to FS and FW loader
In reply to Christian MarangiIn preparation to the introduction of variant of the FS loader, reorganize and split the driver to generic fw_loader function and specific fs_loader function. Create a dedicated directory for the loader and move the internal structs and functions to a dedicated header file.
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/fw_loader/Makefile | 4 +
drivers/misc/{ => fw_loader}/fs_loader.c | 141 ++--------------------
drivers/misc/fw_loader/fw_loader.c | 144 +++++++++++++++++++++++
drivers/misc/fw_loader/internal.h | 61 ++++++++++
include/fs_loader.h | 47 +-------
include/fw_loader.h | 19 +++
8 files changed, 247 insertions(+), 176 deletions(-)
create mode 100644 drivers/misc/fw_loader/Makefile
rename drivers/misc/{ => fw_loader}/fs_loader.c (58%)
create mode 100644 drivers/misc/fw_loader/fw_loader.c
create mode 100644 drivers/misc/fw_loader/internal.hdrivers/misc/Kconfig +5 -0
@@ -612,8 +612,12 @@ config MPC83XX_SERDES help Support for serdes found on MPC83xx SoCs. +config FW_LOADER_LIB + bool + config FS_LOADER bool "Enable loader driver for file system" + select FW_LOADER_DRV 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_DRV depends on SPL help This is file system generic loader which can be used to load
drivers/misc/Makefile +1 -1
@@ -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_DRV) += 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/fw_loader/Makefile new file +4 -0
@@ -0,0 +1,4 @@ +# SPDX-License-Identifier: GPL-2.0+ + +obj-y += fw_loader.o +obj-$(CONFIG_$(PHASE_)FS_LOADER) += fs_loader.o
drivers/misc/fw_loader/fs_loader.c renamed from drivers/misc/fs_loader.c +11 -130
180 lines not on this page · raw message
drivers/misc/fw_loader/fw_loader.c new file +144 -0
144 lines not on this page · raw message
drivers/misc/fw_loader/internal.h new file +61 -0
61 lines not on this page · raw message
include/fs_loader.h +2 -45
54 lines not on this page · raw message
include/fw_loader.h +19 -0
@@ -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
Re: [PATCH v5 2/6] misc: fs_loader: reorganize and split to FS and FW loader
Hi Christian,
Quoted 12 lines · Christian Marangi
misc:
fs_loader: reorganize and split to FS and FW loaderIn preparation to the introduction of variant of the FS loader, reorganize and split the driver to generic
fw_loaderfunction and specificfs_loaderfunction. Create a dedicated directory for the loader and move the internal structs and functions to a dedicated header file.This will permit to reuse all the property and logic of FS loader with container that are not exactly a readable filesystem.
Signed-off-by Christian Marangi <[email protected]>
drivers/misc/fw_loader/internal.h line 37
Quoted 7 lines
drivers/misc/fw_loader/internal.h new file +7
-0+struct device_plat { +struct phandle_part phandlepart; +char *mtdpart; +char *ubivol; + +int (*get_firmware)(struct udevice *dev); +};
This series introduces a generic firmware-loader framework with two implementations (FS and FIP), which is a good idea. But the dispatch mechanism uses function pointers in platform data rather than U-Boot's driver model ops.
In U-Boot's driver model, polymorphic behaviour is handled by uclass operations. Instead of storing function pointers in device_plat and setting them during probe, please can you define a proper ops structure:
struct fw_loader_ops {
int (*get_firmware)(struct udevice *dev);
int (*get_size)(struct udevice *dev);
};Each driver would then declare its ops:
static const struct fw_loader_ops fs_loader_ops = {
.get_firmware = fw_get_filesystem_firmware, .get_size = fw_get_filesystem_firmware_size,
};
And fw_loader.c would use dev_get_uclass_ops() to dispatch. This also means you only need a single UCLASS_FIRMWARE_LOADER rather than separate UCLASS_FS_FIRMWARE_LOADER and UCLASS_FIP_FIRMWARE_LOADER - the ops handle the polymorphism, which is the whole point of having a uclass.
See drivers/spi/spi-uclass.c or drivers/i2c/i2c-uclass.c for examples of this pattern.
drivers/misc/Kconfig config MPC83XX_SERDES · line 617
Quoted 8 lines
drivers/misc/Kconfig +4
-0@@ -612,8 +612,12 @@ config MPC83XX_SERDES +config FW_LOADER_LIB + bool + config FS_LOADER bool "Enable loader driver for file system" + select FW_LOADER_DRV
FW_LOADER_LIB is defined but never used. FW_LOADER_DRV is selected but never defined. I suspect you meant to define FW_LOADER_DRV here instead of FW_LOADER_LIB.
drivers/misc/fw_loader/fw_loader.c line 8
Quoted 10 lines
drivers/misc/fw_loader/fw_loader.c +8
-0@@ -0,0 +1,144 @@ +static int _request_firmware_prepare (struct udevice *dev, +const char *name,void *dbuf, +size_t size,u32 offset) +{ +if (!name || name[0 ] =='\0' ) +return -EINVAL ; + +struct firmware *firmwarep =dev_get_priv (dev);
Please can you move the variable declaration to the top of the function, before the statements.
Quoted 9 lines
drivers/misc/fw_loader/internal.h +7
-0@@ -0,0 +1,61 @@ +/** +* struct firmware - A place for storing firmware and its attribute data. +* +* This holds information about a firmware and its content. +* +* @size: Size of a file +* @data: Buffer for file
drivers/misc/fw_loader/internal.h line 17
Quoted 10 lines
drivers/misc/fw_loader/internal.h +10
-0+* @priv: Firmware loader private fields +* @name: Filename +* @offset: Offset of reading a file +*/ +struct firmware { +size_t size; +const u8 *data; +const char *name; +u32 offset; +};
The kernel-doc mentions @priv but there is no priv member in the struct.
Regards,
Simon
PATCH v5 3/6 misc: fw_loader: implement generic get_fw_loader_from_node()
In reply to Christian MarangiImplement 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.c | 32 ++++++++++++++++++++++++++++++ include/fw_loader.h | 15 ++++++++++++++ 2 files changed, 47 insertions(+)
drivers/misc/fw_loader/fw_loader.c +32 -0
@@ -8,6 +8,7 @@#include <blk.h> #include <linux/types.h> #include <dm/device.h> +#include <dm/uclass.h> #include <fw_loader.h> #ifdef CONFIG_CMD_UBIFS
@@ -90,6 +91,37 @@ int generic_fw_loader_probe(struct udevice *dev)return 0 ; } +/** +* 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) +{ +struct udevice *fw_dev; +int i, 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 +15 -0
@@ -6,8 +6,23 @@#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. +* +* Loop over all the supported FW loader and find a matching +* one. +* +* 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
Re: [PATCH v5 3/6] misc: fw_loader: implement generic get_fw_loader_from_node()
Hi Christian,
Quoted 15 lines · Christian Marangi
misc:
fw_loader: implement genericget_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.Signed-off-by Christian Marangi <[email protected]>
include/fw_loader.h line 7
Quoted 2 lines
include/fw_loader.h +2
-0@@ -6,8 +6,23 @@ +* Loop over all the supported FW loader and find a matching +* one.
Is there a compatible string for this node? Is it in the DT schema?
The comment says it loops over all supported FW loaders but the implementation does a direct lookup by ofnode with device_find_global_by_ofnode(). Please can you update the comment to match.
Regards,
Simon
PATCH v5 4/6 misc: fw_loader: implement request_firmware_size() OP
In reply to Christian MarangiIt 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 | 72 +++++++++++++++++++++++++----- drivers/misc/fw_loader/fw_loader.c | 20 +++++++++ drivers/misc/fw_loader/internal.h | 1 + include/fw_loader.h | 9 ++++ 4 files changed, 91 insertions(+), 11 deletions(-)
drivers/misc/fw_loader/fs_loader.c +61 -11
@@ -89,15 +89,8 @@ static int select_fs_dev(struct device_plat *plat)return ret; } -/** -* fw_get_filesystem_firmware - load firmware into an allocated buffer. -* @dev: An instance of a driver. -* -* Return: Size of total read, negative value when error. -*/ -static int fw_get_filesystem_firmware (struct udevice *dev) +static int __fw_get_filesystem_firmware_prepare (struct udevice *dev) { -loff_t actread =0 ;char *storage_interface, *dev_part, *ubi_mtdpart, *ubi_volume;int ret;
@@ -122,6 +115,28 @@ static int fw_get_filesystem_firmware(struct udevice *dev) ret =select_fs_dev (dev_get_plat (dev)); } +return ret; +} + +static void __fw_get_filesystem_firmware_release (struct udevice *dev) +{ +#ifdef CONFIG_CMD_UBIFS +umount_ubifs (); +#endif +} + +/** +* fw_get_filesystem_firmware - load firmware into an allocated buffer. +* @dev: An instance of a driver. +* +* Return: Size of total read, negative value when error. +*/ +static int fw_get_filesystem_firmware (struct udevice *dev) +{ +loff_t actread =0 ; +int ret; + + ret =__fw_get_filesystem_firmware_prepare (dev);if (ret) goto out;
@@ -143,9 +158,43 @@ static int fw_get_filesystem_firmware(struct udevice *dev) } out: -#ifdef CONFIG_CMD_UBIFS -umount_ubifs (); -#endif +__fw_get_filesystem_firmware_release (dev); +return ret; +} + +/** +* fw_get_filesystem_firmware_size - get firmware size. +* @dev: An instance of a driver. +* +* Return: Size of firmware, negative value when error. +*/ +static int fw_get_filesystem_firmware_size (struct udevice *dev) +{ +struct firmware *firmwarep; +loff_t size =0 ; +int ret; + + ret =__fw_get_filesystem_firmware_prepare (dev); +if (ret) + goto out; + + firmwarep =dev_get_priv (dev); +if (!firmwarep) { + ret = -ENOMEM ; + goto out; + } + + ret =fs_size (firmwarep->name, &size); +if (ret) { +debug ("Error: %d Failed to get size for %s.\n" , + ret, firmwarep->name); + goto out; + } + + ret = size; + +out: +__fw_get_filesystem_firmware_release (dev);return ret; }
@@ -159,6 +208,7 @@ static int fs_loader_probe(struct udevice *dev)return ret; plat->get_firmware = fw_get_filesystem_firmware; + plat->get_size = fw_get_filesystem_firmware_size;return 0 ; };
drivers/misc/fw_loader/fw_loader.c +20 -0
@@ -174,3 +174,23 @@ int request_firmware_into_buf(struct udevice *dev,return plat->get_firmware (dev); } + +int request_firmware_size (struct udevice *dev,const char *name) +{ +struct device_plat *plat; +int ret; + +if (!dev) +return -EINVAL ; + + ret =_request_firmware_prepare (dev, name,NULL ,0 ,0 ); +if (ret <0 )/* error */ +return ret; + + plat =dev_get_plat (dev); + +if (!plat->get_size) +return -EOPNOTSUPP ; + +return plat->get_size (dev); +}
drivers/misc/fw_loader/internal.h +1 -0
@@ -34,6 +34,7 @@ struct device_plat {char *ubivol;int (*get_firmware)(struct udevice *dev); +int (*get_size)(struct udevice *dev); };/**
include/fw_loader.h +9 -0
@@ -39,6 +39,15 @@ int request_firmware_into_buf(struct udevice *dev,const char *name, void *buf, size_t size, u32 offset); +/** +* request_firmware_size - Get firmware size. +* @dev: An instance of a driver. +* @name: Name of firmware file. +* +* Return: Size of firmware, negative value when error. +*/ +int request_firmware_size (struct udevice *dev,const char *name); +/** * request_firmware_into_buf_via_script() - * Load firmware using a U-Boot script and copy to buffer
Signature · 1 line
2.53.0
Re: [PATCH v5 4/6] misc: fw_loader: implement request_firmware_size() OP
Hi Christian,
Quoted 18 lines · Christian Marangi
misc:
fw_loader: implementrequest_firmware_size()OPIt 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 <[email protected]>
drivers/misc/fw_loader/fs_loader.c static int fw_get_filesystem_firmware(struct udevice *dev) · line 115
Quoted 1 line
drivers/misc/fw_loader/fs_loader.c +1
-0@@ -122,6 +115,28 @@ static int fw_get_filesystem_firmware(struct udevice *dev) +static int __fw_get_filesystem_firmware_prepare (struct udevice *dev)
Please can you avoid double-underscore prefix for static functions? Something like fw_fs_prepare() would be cleaner.
drivers/misc/fw_loader/fs_loader.c static int fw_get_filesystem_firmware(struct udevice *dev) · line 162
Quoted 7 lines
drivers/misc/fw_loader/fs_loader.c +5
-0@@ -143,9 +158,43 @@ static int fw_get_filesystem_firmware(struct udevice *dev) + firmwarep =dev_get_priv (dev); +if (!firmwarep) { + ret = -ENOMEM ; + goto out; + }
This cannot happen, since driver model ensures the priv data exists.
Also how about using 'priv' as the variable name? We normally use a 'p' suffix to indicate a parameter which returns a value.
drivers/misc/fw_loader/fw_loader.c int request_firmware_into_buf(struct udevice *dev, · line 175
Quoted 4 lines
drivers/misc/fw_loader/fw_loader.c +2
-0@@ -174,3 +174,23 @@ int request_firmware_into_buf(struct udevice *dev, +int request_firmware_size (struct udevice *dev,const char *name) +{
The function needs a kerneldoc comment in the implementation.
Regards,
Simon
PATCH v5 5/6 misc: fw_loader: introduce FIP loader driver
In reply to Christian MarangiIntroduce 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 | 578 ++++++++++++++++++++++++++++ drivers/misc/fw_loader/internal.h | 2 + include/dm/uclass-id.h | 3 +- 5 files changed, 594 insertions(+), 1 deletion(-) create mode 100644 drivers/misc/fw_loader/fip_loader.c
drivers/misc/Kconfig +11 -0
@@ -615,6 +615,17 @@ config MPC83XX_SERDES config FW_LOADER_LIB bool +config FIP_LOADER + bool "Enable loader driver from FIP partition" + select LIB_UUID + select FW_LOADER_DRV + 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_DRV
drivers/misc/fw_loader/Makefile +1 -0
@@ -1,4 +1,5 @@ # SPDX-License-Identifier: GPL-2.0+ obj-y += fw_loader.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 +578 -0
578 lines not on this page · raw message
drivers/misc/fw_loader/internal.h +2 -0
@@ -25,11 +25,13 @@ struct phandle_part {* 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; +int partoffset;char *mtdpart;char *ubivol;
include/dm/uclass-id.h +2 -1
@@ -69,7 +69,8 @@ enum uclass_id {UCLASS_FIRMWARE ,/* Firmware */ UCLASS_FPGA ,/* FPGA device */ UCLASS_FUZZING_ENGINE ,/* Fuzzing engine */ -UCLASS_FS_FIRMWARE_LOADER ,/* Generic loader */ +UCLASS_FIP_FIRMWARE_LOADER ,/* FIP image loader */ +UCLASS_FS_FIRMWARE_LOADER ,/* Generic loader */ UCLASS_FWU_MDATA ,/* FWU Metadata Access */ UCLASS_GPIO ,/* Bank of general-purpose I/O pins */ UCLASS_HASH, /* Hash device */
Signature · 1 line
2.53.0
Re: [PATCH v5 5/6] misc: fw_loader: introduce FIP loader driver
Hi Christian,
Quoted 34 lines · Christian Marangi
misc:
fw_loader: introduce FIP loader driverIntroduce 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/fw_loader/fip_loader.c +10
-0@@ -0,0 +1,578 @@ +static int blk_read_fip_firmware (struct firmware *firmwarep, +struct blk_desc *desc, +struct disk_partition *part_info, +unsigned int part_offset, +const struct fip_toc_entry *ent) +{ + ... +size_t size = ent->size; + ... + blkcnt =BLOCK_CNT (size + firmwarep->offset, desc);
drivers/misc/fw_loader/fip_loader.c line 20
Quoted 10 lines
drivers/misc/fw_loader/fip_loader.c +10
-0+ blkstart = ent->offset_address + firmwarep->offset; + ... +if (pos) { + to_read =min (desc->blksz - pos, (unsigned long )size); + ... + } + +/* Consume all the remaining block */ +for (i =0 ; i < blkcnt && read < size; i++) { + to_read =min (desc->blksz, (unsigned long )(size - read));
This reads the full ent->size bytes but starts at ent->offset_address
+ firmwarep->offset. Compare with ubi_read_fip_firmware() which correctly reads (size - offset) bytes. The blk variant should also adjust size by firmwarep->offset so the behaviour is consistent.
drivers/misc/fw_loader/fip_loader.c line 5
Quoted 7 lines
drivers/misc/fw_loader/fip_loader.c +5
-0@@ -0,0 +1,578 @@ +static int fw_get_fip_firmware_size (struct udevice *dev) +{ + ... +return ent.size; +}
ent.size is u64 but the function returns int. This could truncate large sizes. The same issue exists in fw_get_fip_firmware(). How about using ulong which is the common type in U-Boot?
drivers/misc/fw_loader/fip_loader.c line 8
Quoted 10 lines
drivers/misc/fw_loader/fip_loader.c +8
-0@@ -0,0 +1,578 @@ +static int fw_get_fip_firmware (struct udevice *dev) +{ + ... + ret =fw_parse_storage_info (dev, &info); +if (ret) +return ret; + +struct firmware *firmwarep =dev_get_priv (dev);
Please can you move the firmwarep declaration to the top of the block. The same pattern appears in fw_get_fip_firmware_size().
drivers/misc/fw_loader/fip_loader.c line 4
Quoted 6 lines
drivers/misc/fw_loader/fip_loader.c +4
-0@@ -0,0 +1,578 @@ +if (ent.size + firmwarep->offset > firmwarep->size) { +log_err ("Not enough space to read firmware\n" ); +return -ENOMEM ; + }
I suspect this check is wrong. If firmwarep->offset is an offset into the FIP entry, you likely want to verify that (ent.size - firmwarep->offset) fits in the buffer, not (ent.size + firmwarep->offset).
Also -NOSPC might be better, since we are not actually out of memory. When people see -ENOMEM they tend to want to increase the malloc size.
drivers/misc/fw_loader/internal.h struct phandle_part { · line 25
Quoted 3 lines
drivers/misc/fw_loader/internal.h +1
-0@@ -25,11 +25,13 @@ struct phandle_part { +int partoffset;
partoffset is declared as int but used as unsigned int in fip_storage_info and read from DT with ofnode_read_u32(). Please can you change this to u32 for consistency.
Regards,
Simon
Re: [PATCH v5 5/6] misc: fw_loader: introduce FIP loader driver
Quoted 53 lines · Simon Glass
Hi Christian,
Quoted 34 lines · Christian Marangi
misc:
fw_loader: introduce FIP loader driverIntroduce 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/fw_loader/fip_loader.c +10
-0@@ -0,0 +1,578 @@ +static int blk_read_fip_firmware (struct firmware *firmwarep, +struct blk_desc *desc, +struct disk_partition *part_info, +unsigned int part_offset, +const struct fip_toc_entry *ent) +{ + ... +size_t size = ent->size; + ... + blkcnt =BLOCK_CNT (size + firmwarep->offset, desc);drivers/misc/fw_loader/fip_loader.c line 20
Quoted 10 lines
drivers/misc/fw_loader/fip_loader.c +10
-0+ blkstart = ent->offset_address + firmwarep->offset; + ... +if (pos) { + to_read =min (desc->blksz - pos, (unsigned long )size); + ... + } + +/* Consume all the remaining block */ +for (i =0 ; i < blkcnt && read < size; i++) { + to_read =min (desc->blksz, (unsigned long )(size - read));This reads the full
ent->sizebytes but starts atent->offset_address
+firmwarep->offset. Compare withubi_read_fip_firmware()which correctly reads (size - offset) bytes. The blk variant should also adjust size byfirmwarep->offsetso the behaviour is consistent.
Quoted 12 lines
drivers/misc/fw_loader/fip_loader.c line 5
Quoted 7 lines
drivers/misc/fw_loader/fip_loader.c +5
-0@@ -0,0 +1,578 @@ +static int fw_get_fip_firmware_size (struct udevice *dev) +{ + ... +return ent.size; +}ent.size is u64 but the function returns int. This could truncate large sizes. The same issue exists in
fw_get_fip_firmware(). How about using ulong which is the common type in U-Boot?
Can you better explain this? We need to return int to handle the negative error.
Quoted 26 lines
drivers/misc/fw_loader/fip_loader.c line 8
Quoted 10 lines
drivers/misc/fw_loader/fip_loader.c +8
-0@@ -0,0 +1,578 @@ +static int fw_get_fip_firmware (struct udevice *dev) +{ + ... + ret =fw_parse_storage_info (dev, &info); +if (ret) +return ret; + +struct firmware *firmwarep =dev_get_priv (dev);Please can you move the firmwarep declaration to the top of the block. The same pattern appears in
fw_get_fip_firmware_size().drivers/misc/fw_loader/fip_loader.c line 4
Quoted 6 lines
drivers/misc/fw_loader/fip_loader.c +4
-0@@ -0,0 +1,578 @@ +if (ent.size + firmwarep->offset > firmwarep->size) { +log_err ("Not enough space to read firmware\n" ); +return -ENOMEM ; + }I suspect this check is wrong. If
firmwarep->offsetis an offset into the FIP entry, you likely want to verify that (ent.size -firmwarep->offset) fits in the buffer, not (ent.size +firmwarep->offset).
Quoted 3 lines
Also -NOSPC might be better, since we are not actually out of memory. When people see
-ENOMEMthey tend to want to increase the malloc size.
This might be confusing but I feel -ENOMEM is correct here. In firmwarep->size we set the maximum size where we can store the firmware data so the random guy might be correct to increase malloc size.
I posted v6 with all the other comments addressed, these 2 are the only one left.
Quoted 10 lines
drivers/misc/fw_loader/internal.h struct phandle_part { · line 25
Quoted 3 lines
drivers/misc/fw_loader/internal.h +1
-0@@ -25,11 +25,13 @@ struct phandle_part { +int partoffset;partoffset is declared as int but used as unsigned int in
fip_storage_infoand read from DT withofnode_read_u32(). Please can you change this to u32 for consistency.Regards,
Simon
Signature · 1 line
Ansuel
PATCH v5 6/6 doc: dtbinding: Update documentation for Generic Firmware loader
In reply to Christian MarangiUpdate documentation for Generic Firmware loader, generalize it from FS specific and add new property and example for FIP loader.
---
.../misc/{fs_loader.txt => fw_loader.txt} | 15 +++++++++++++--
1 file changed, 13 insertions(+), 2 deletions(-)
rename doc/device-tree-bindings/misc/{fs_loader.txt => fw_loader.txt} (77%)doc/device-tree-bindings/misc/fw_loader.txt renamed from doc/device-tree-bindings/misc/fs_loader.txt +13 -2
@@ -1,9 +1,9 @@ -* 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
@@ -12,6 +12,8 @@ Required properties: 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 parition from. Example of storage device and partition search set for mmc, usb, sata and ubi in device tree source as shown in below:
@@ -46,3 +48,12 @@ 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>; + }; +
Signature · 1 line
2.53.0
Re: [PATCH v5 6/6] doc: dtbinding: Update documentation for Generic Firmware loader
Hi Christian,
doc/device-tree-bindings/misc/fw_loader.txt Required properties: · line 8
Quoted 11 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.
Signed-off-by Christian Marangi <[email protected]>doc/device-tree-bindings/misc/fw_loader.txt renamed from doc/device-tree-bindings/misc/fs_loader.txt +2
-0@@ -7,6 +7,8 @@ Required properties: +- mdtpart: which partition of ubi the image loading from, this property is + required for ubi and mounting.
mtdpart
doc/device-tree-bindings/misc/fw_loader.txt Required properties: · line 13
Quoted 4 lines
doc/device-tree-bindings/misc/fw_loader.txt renamed from doc/device-tree-bindings/misc/fs_loader.txt +2
-0@@ -12,6 +12,8 @@ Required properties: +- partoffset: valid ONLY for "u-boot,fip-loader". Offset of the partition to + parse the FIP parition from.
partition
doc/device-tree-bindings/misc/fw_loader.txt line 5
Quoted 8 lines
doc/device-tree-bindings/misc/fw_loader.txt renamed from doc/device-tree-bindings/misc/fs_loader.txt +2
-1@@ -1,9 +1,9 @@ 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,
The section says 'Required properties' but phandlepart, mtdpart and ubivol are alternatives; only one set is required depending on the storage type. Please can you clarify this.
doc/device-tree-bindings/misc/fw_loader.txt ubi in device tree source as shown in below: · line 54
Quoted 9 lines
doc/device-tree-bindings/misc/fw_loader.txt renamed from doc/device-tree-bindings/misc/fs_loader.txt +7
-0@@ -46,3 +48,12 @@ ubi in device tree source as shown in below: + Example for FIP from eMMC: + fs_loader4: fip-loader@4 { + bootph-all; + compatible = "u-boot,fip-loader"; + phandlepart = <&mmc0 0>; + partoffset = <0x100>; + };
Since the FIP loader supports UBI storage as well, it would be good to add an example for FIP on UBI too.
Regards,
Simon