Extend FW framework for user FW uploads
Subsystems (5)
cover Extend FW framework for user FW uploads
Extend the firmware loader subsystem to support a persistent sysfs interface that userspace may use to initiate a firmware update. For example, FPGA based PCIe cards automatically load firmware and FPGA images from local FLASH when the card boots. The images in FLASH may be updated with new images that are uploaded by the user.
A device driver may call firmware_upload_register() to expose persistent "loading" and "data" sysfs files at /sys/class/firmare/<NAME>/*. These files are used in the same way as the fallback sysfs "loading" and "data" files. However, when 0 is written to "loading" to complete the write of firmware data, the data is also transferred to the lower-level driver using pre-registered call-back functions. The data transfer is done in the context of a kernel worker thread.
Additional sysfs nodes are added in the same location as "loading" and "data" to monitor the transfer of the image data to the device using callback functions provided by the lower-level device driver and to allow the data transfer to be cancelled.
Example usage:
$ pwd /sys/class/firmware/n3000bmc-sec-update.8 $ ls cancel device loading remaining_size subsystem data error power status uevent $ echo 1 > loading $ cat /tmp/firmware.bin > data $ echo 0 > loading $ while :; do cat status; cat remaining_size ; sleep 3; done preparing 44590080 <--snip--> transferring 44459008 transferring 44311552 <--snip--> transferring 173056 <--snip--> programming 0 <--snip--> idle 0 ^C $ cat error
The first two patches in this set make minor changes to enable the fw_priv data structure and the sysfs interfaces to be used multiple times during the existence of the device driver instance. The third patch is mostly a reorganization of existing code in preparation for sharing common code with the firmware-upload support. The fourth and fifth patches provide the code for user-initiated firmware uploads. The final 3 patches extend selftest support to test firmware-upload functionality.
Changelog RFC -> v1:
- Renamed files fw_sysfs.c and fw_sysfs.h to sysfs.c and sysfs.h
- Moved "MODULE_IMPORT_NS(FIRMWARE_LOADER_PRIVATE);" from sysfs.c to sysfs.h to address an error identified by the kernel test robot <[email protected]>
- renamed fw_upload_register() and fw_upload_unregister() to firmware_upload_register() and fw_upload_unregister().
- Moved ifdef'd section of code out of firmware_loading_store() in sysfs.c into a new function, fw_upload_start(), in sysfs_upload.c.
- Changed #defines to enums for error codes and progress states
- Added additional kernel-doc supported symbols into the documentation. Some rewording in documentation as well.
- Added module reference counting for the parent module in the firmware_upload_register() and firmware_upload_unregister() functions to fix problems found when testing with test_firmware module.
- Removed unnecessary module reference counting for THIS_MODULE.
- Added a new patch to modify the test_firmware module to support testing of the firmware upload mechanism.
- Added a new patch to modify the test_firmware module to support error injection for firmware upload.
- Added a new patch to extend the existing firmware selftests to cover firmware upload.
Russ Weight (8):
firmware_loader: Clear data and size in fw_free_paged_buf
firmware_loader: Check fw_state_is_done in loading_store
firmware_loader: Split sysfs support from fallback
firmware_loader: Add firmware-upload support
firmware_loader: Add sysfs nodes to monitor fw_upload
test_firmware: Add test support for firmware upload
test_firmware: Error injection for firmware upload
selftests: firmware: Add firmware upload selftests
.../ABI/testing/sysfs-class-firmware | 77 ++++ .../driver-api/firmware/fw_upload.rst | 117 +++++ Documentation/driver-api/firmware/index.rst | 1 + drivers/base/firmware_loader/Kconfig | 18 + drivers/base/firmware_loader/Makefile | 2 + drivers/base/firmware_loader/fallback.c | 430 ----------------- drivers/base/firmware_loader/fallback.h | 46 +- drivers/base/firmware_loader/firmware.h | 11 + drivers/base/firmware_loader/main.c | 18 +- drivers/base/firmware_loader/sysfs.c | 435 ++++++++++++++++++ drivers/base/firmware_loader/sysfs.h | 100 ++++ drivers/base/firmware_loader/sysfs_upload.c | 396 ++++++++++++++++ drivers/base/firmware_loader/sysfs_upload.h | 47 ++ include/linux/firmware.h | 82 ++++ lib/test_firmware.c | 378 +++++++++++++++ tools/testing/selftests/firmware/Makefile | 2 +- tools/testing/selftests/firmware/config | 1 + tools/testing/selftests/firmware/fw_lib.sh | 7 + .../selftests/firmware/fw_run_tests.sh | 4 + tools/testing/selftests/firmware/fw_upload.sh | 214 +++++++++ 20 files changed, 1900 insertions(+), 486 deletions(-) create mode 100644 Documentation/ABI/testing/sysfs-class-firmware create mode 100644 Documentation/driver-api/firmware/fw_upload.rst create mode 100644 drivers/base/firmware_loader/sysfs.c create mode 100644 drivers/base/firmware_loader/sysfs.h create mode 100644 drivers/base/firmware_loader/sysfs_upload.c create mode 100644 drivers/base/firmware_loader/sysfs_upload.h create mode 100755 tools/testing/selftests/firmware/fw_upload.sh
Signature · 1 line
2.25.1
PATCH RESEND 1/8 firmware_loader: Clear data and size in fw_free_paged_buf
The fw_free_paged_buf() function resets the paged buffer information in the fw_priv data structure. Additionally, clear the data and size members of fw_priv in order to facilitate the reuse of fw_priv. This is being done in preparation for enabling userspace to initiate multiple firmware uploads using this sysfs interface.
---
v1:
- No change from RFC patch
--- drivers/base/firmware_loader/main.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/base/firmware_loader/main.c +2 -0
@@ -253,6 +253,8 @@ void fw_free_paged_buf(struct fw_priv *fw_priv) fw_priv->pages =NULL ; fw_priv->page_array_size =0 ; fw_priv->nr_pages =0 ; + fw_priv->data =NULL ; + fw_priv->size =0 ; }int fw_grow_paged_buf (struct fw_priv *fw_priv,int pages_needed)
Signature · 1 line
2.25.1
Re: [RESEND PATCH v1 1/8] firmware_loader: Clear data and size in fw_free_paged_buf
drivers/base/firmware_loader/main.c void fw_free_paged_buf(struct fw_priv *fw_priv)
Quoted 19 lines · Russ Weight
The
fw_free_paged_buf()function resets the paged buffer information in thefw_privdata structure. Additionally, clear the data and size members offw_privin order to facilitate the reuse offw_priv. This is being done in preparation for enabling userspace to initiate multiple firmware uploads using this sysfs interface.Signed-off-by Russ Weight <[email protected]>---
v1:
- No change from RFC patch--- drivers/base/firmware_loader/main.c | 2 ++ 1 file changed, 2 insertions(+)drivers/base/firmware_loader/main.c +0
-0@@ -253,6 +253,8 @@ void fw_free_paged_buf(struct fw_priv *fw_priv)
Why isn't a vfree needed or realloc done?
Tom
Quoted 8 lines
drivers/base/firmware_loader/main.c +2
-0fw_priv->pages =NULL ; fw_priv->page_array_size =0 ; fw_priv->nr_pages =0 ; + fw_priv->data =NULL ; + fw_priv->size =0 ; }int fw_grow_paged_buf (struct fw_priv *fw_priv,int pages_needed)
Re: [RESEND PATCH v1 1/8] firmware_loader: Clear data and size in fw_free_paged_buf
Hi Tom,
Quoted 16 lines · Tom Rix
Quoted 14 lines · Russ Weight
The
fw_free_paged_buf()function resets the paged buffer information in thefw_privdata structure. Additionally, clear the data and size members offw_privin order to facilitate the reuse offw_priv. This is being done in preparation for enabling userspace to initiate multiple firmware uploads using this sysfs interface.Signed-off-by Russ Weight <[email protected]>---
v1:
- No change from RFC patch
---
drivers/base/firmware_loader/main.c| 2 ++1 file changed, 2 insertions(+)
Quoted 7 lines
drivers/base/firmware_loader/main.c void fw_free_paged_buf(struct fw_priv *fw_priv)
Quoted 5 lines
drivers/base/firmware_loader/main.c +0
-0@@ -253,6 +253,8 @@ void fw_free_paged_buf(struct fw_priv *fw_priv)Why isn't a vfree needed or realloc done?
The free and realloc support was present prior to my changes. The page buffer support was designed such that if a firmware write was cancelled, the existing fw_priv structure could be re-used for another write in the context of the same firmware upload. However, there was no prior case for completing a write and then reusing the fw_priv structure for subsequent firmware writes; fw_priv previously had a one-time use. The changes I have made are to enable the re-use of the fw_priv structure.
Initially, fw_priv->data is NULL. The "realloc" functionality happens during the write of the data binary attribute here:
The fw_priv->data pointer remains NULL until all data is written and the user writes '0' to the loading attribute. The fw_priv->data pointer is set in fw_map_paged_buf() which is called here:
In the unmodified code, the fw_priv->data pointer is never cleared. My changes reset the pointer to NULL after the memory is released so that the fw_priv can be resused.
The new firmware-upload happens in the context of a kernel worker thread and the work function is fw_upload_main(). At the end of fw_upload_main(), fw_free_paged_buf() is called to do the free. This is the function that is being modified by the lines below. This function calls "__free_page(fw_priv->pages[i])" in a loop to free the memory pages. It also calls "vunmap(fw_priv->data)" to free the virtual mapping. You can see the unmodified implementation of this function here:
- Russ
Quoted 11 lines
Tom
Quoted 7 lines
drivers/base/firmware_loader/main.c +2
-0fw_priv->pages = NULL; fw_priv->page_array_size = 0; fw_priv->nr_pages = 0; + fw_priv->data = NULL; + fw_priv->size = 0; } int fw_grow_paged_buf(struct fw_priv *fw_priv, int pages_needed)
Re: [RESEND PATCH v1 1/8] firmware_loader: Clear data and size in fw_free_paged_buf
Quoted 24 lines · Russ Weight
Hi Tom,
Quoted 21 lines · Tom Rix
drivers/base/firmware_loader/main.c void fw_free_paged_buf(struct fw_priv *fw_priv)
Quoted 19 lines · Russ Weight
The
fw_free_paged_buf()function resets the paged buffer information in thefw_privdata structure. Additionally, clear the data and size members offw_privin order to facilitate the reuse offw_priv. This is being done in preparation for enabling userspace to initiate multiple firmware uploads using this sysfs interface.Signed-off-by Russ Weight <[email protected]>---
v1:
- No change from RFC patch--- drivers/base/firmware_loader/main.c | 2 ++ 1 file changed, 2 insertions(+)drivers/base/firmware_loader/main.c +0
-0@@ -253,6 +253,8 @@ void fw_free_paged_buf(struct fw_priv *fw_priv)Why isn't a vfree needed or realloc done?
I am looking at the use of this function in __free_fw_priv
if (fw_is_paged_buf(fw_priv))
fw_free_paged_buf(fw_priv);
else if (!fw_priv->allocated_size)
vfree(fw_priv->data);
Where it seems like there is another way to set data, so it needs another way to unset.
The vfree here looks suspect because the pointer comes in from request_firmware_info_buf with a hope that it was allocated by vmalloc.
Tom
Quoted 43 lines
The free and realloc support was present prior to my changes. The page buffer support was designed such that if a firmware write was cancelled, the existing
fw_privstructure could be re-used for another write in the context of the same firmware upload. However, there was no prior case for completing a write and then reusing thefw_privstructure for subsequent firmware writes;fw_privpreviously had a one-time use. The changes I have made are to enable the re-use of thefw_privstructure.Initially,
fw_priv->datais NULL. The "realloc" functionality happens during the write of the data binary attribute here:The
fw_priv->datapointer remains NULL until all data is written and the user writes '0' to the loading attribute. Thefw_priv->datapointer is set infw_map_paged_buf()which is called here:In the unmodified code, the
fw_priv->datapointer is never cleared. My changes reset the pointer to NULL after the memory is released so that thefw_privcan be resused.The new firmware-upload happens in the context of a kernel worker thread and the work function is
fw_upload_main(). At the end offw_upload_main(),fw_free_paged_buf()is called to do the free. This is the function that is being modified by the lines below. This function calls "__free_page(fw_priv->pages[i])" in a loop to free the memory pages. It also calls "vunmap(fw_priv->data)" to free the virtual mapping. You can see the unmodified implementation of this function here:- Russ
Quoted 9 lines
Tom
Quoted 7 lines
drivers/base/firmware_loader/main.c +2
-0fw_priv->pages = NULL; fw_priv->page_array_size = 0; fw_priv->nr_pages = 0; + fw_priv->data = NULL; + fw_priv->size = 0; } int fw_grow_paged_buf(struct fw_priv *fw_priv, int pages_needed)
Re: [RESEND PATCH v1 1/8] firmware_loader: Clear data and size in fw_free_paged_buf
Quoted 29 lines · Tom Rix
Quoted 24 lines · Russ Weight
Hi Tom,
Quoted 21 lines · Tom Rix
drivers/base/firmware_loader/main.c void fw_free_paged_buf(struct fw_priv *fw_priv)
Quoted 19 lines · Russ Weight
The
fw_free_paged_buf()function resets the paged buffer information in thefw_privdata structure. Additionally, clear the data and size members offw_privin order to facilitate the reuse offw_priv. This is being done in preparation for enabling userspace to initiate multiple firmware uploads using this sysfs interface.Signed-off-by Russ Weight <[email protected]>---
v1:
- No change from RFC patch
---
drivers/base/firmware_loader/main.c| 2 ++1 file changed, 2 insertions(+)drivers/base/firmware_loader/main.c +0
-0@@ -253,6 +253,8 @@ void fw_free_paged_buf(struct fw_priv *fw_priv)Why isn't a vfree needed or realloc done?
I am looking at the use of this function in
__free_fw_priv
Quoted 8 lines
if (
fw_is_paged_buf(fw_priv))
fw_free_paged_buf(fw_priv);
else if (!fw_priv->allocated_size)
vfree(fw_priv->data);Where it seems like there is another way to set data, so it needs another way to unset.
The vfree here looks suspect because the pointer comes in from
request_firmware_info_bufwith a hope that it was allocated by vmalloc.
There are places in the code where, if fw_priv->data is set, it is assumed that it was allocated by the caller. In the sysfs-upload path, vmalloc is never used - only paged buffers.
As I have revisited the code to answer your questions, I see that in the firmware
fallback path, after it checks for (!fw_priv->data), it sets "fw_priv->is_paged_buf = true".
I realize now that I should be setting "fw_priv->is_paged_buf = true" in firmware_upload_register(). I'll make that change and rebase on 5.18-rc1 when it is available.
Thanks for the comments!
- Russ
Quoted 47 lines
Tom
Quoted 43 lines
The free and realloc support was present prior to my changes. The page buffer support was designed such that if a firmware write was cancelled, the existing
fw_privstructure could be re-used for another write in the context of the same firmware upload. However, there was no prior case for completing a write and then reusing thefw_privstructure for subsequent firmware writes;fw_privpreviously had a one-time use. The changes I have made are to enable the re-use of thefw_privstructure.Initially,
fw_priv->datais NULL. The "realloc" functionality happens during the write of the data binary attribute here:The
fw_priv->datapointer remains NULL until all data is written and the user writes '0' to the loading attribute. Thefw_priv->datapointer is set infw_map_paged_buf()which is called here:In the unmodified code, the
fw_priv->datapointer is never cleared. My changes reset the pointer to NULL after the memory is released so that thefw_privcan be resused.The new firmware-upload happens in the context of a kernel worker thread and the work function is
fw_upload_main(). At the end offw_upload_main(),fw_free_paged_buf()is called to do the free. This is the function that is being modified by the lines below. This function calls "__free_page(fw_priv->pages[i])" in a loop to free the memory pages. It also calls "vunmap(fw_priv->data)" to free the virtual mapping. You can see the unmodified implementation of this function here:- Russ
Quoted 9 lines
Tom
Quoted 7 lines
drivers/base/firmware_loader/main.c +2
-0fw_priv->pages = NULL; fw_priv->page_array_size = 0; fw_priv->nr_pages = 0; + fw_priv->data = NULL; + fw_priv->size = 0; } int fw_grow_paged_buf(struct fw_priv *fw_priv, int pages_needed)
PATCH RESEND 2/8 firmware_loader: Check fw_state_is_done in loading_store
In reply to Russ WeightAdd the fw_state_is_done() function and exit early from firmware_loading_store() if the state is already "done". This is being done in preparation for supporting persistent sysfs nodes to allow userspace to upload firmware to a device, potentially reusing the sysfs loading and data files multiple times.
---
v1:
- No change from RFC patch
--- drivers/base/firmware_loader/fallback.c | 2 +- drivers/base/firmware_loader/firmware.h | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-)
drivers/base/firmware_loader/fallback.c +1 -1
@@ -250,7 +250,7 @@ static ssize_t firmware_loading_store(struct device *dev,mutex_lock (&fw_lock); fw_priv = fw_sysfs->fw_priv; -if (fw_state_is_aborted (fw_priv)) +if (fw_state_is_aborted (fw_priv) ||fw_state_is_done (fw_priv)) goto out;switch (loading) {
drivers/base/firmware_loader/firmware.h +5 -0
@@ -149,6 +149,11 @@ static inline void fw_state_done(struct fw_priv *fw_priv)__fw_state_set (fw_priv,FW_STATUS_DONE ); } +static inline bool fw_state_is_done (struct fw_priv *fw_priv) +{ +return __fw_state_check (fw_priv,FW_STATUS_DONE ); +} +int assign_fw (struct firmware *fw,struct device *device);#ifdef CONFIG_FW_LOADER
Signature · 1 line
2.25.1
Re: [RESEND PATCH v1 2/8] firmware_loader: Check fw_state_is_done in loading_store
Quoted 33 lines · Russ Weight
Add the
fw_state_is_done()function and exit early fromfirmware_loading_store()if the state is already "done". This is being done in preparation for supporting persistent sysfs nodes to allow userspace to upload firmware to a device, potentially reusing the sysfs loading and data files multiple times.Signed-off-by Russ Weight <[email protected]>---
v1:
- No change from RFC patch--- drivers/base/firmware_loader/fallback.c | 2 +- drivers/base/firmware_loader/firmware.h | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-)drivers/base/firmware_loader/fallback.c +1
-1@@ -250,7 +250,7 @@ static ssize_t firmware_loading_store(struct device *dev,mutex_lock (&fw_lock); fw_priv = fw_sysfs->fw_priv; -if (fw_state_is_aborted (fw_priv)) +if (fw_state_is_aborted (fw_priv) ||fw_state_is_done (fw_priv)) goto out;switch (loading) {drivers/base/firmware_loader/firmware.h +0
-0
drivers/base/firmware_loader/firmware.h static inline void fw_state_done(struct fw_priv *fw_priv) · line 154
Quoted 6 lines
drivers/base/firmware_loader/firmware.h +3
-0@@ -149,6 +149,11 @@ static inline void fw_state_done(struct fw_priv *fw_priv)__fw_state_set (fw_priv,FW_STATUS_DONE ); } +static inline bool fw_state_is_done (struct fw_priv *fw_priv) +{ +return __fw_state_check (fw_priv,FW_STATUS_DONE );
This looks like the fw_sysfs_done() in fallback.c
IMO this one and similar wrappers should move to firmware.h and use the *_is_* naming.
Tom
Quoted 5 lines
drivers/base/firmware_loader/firmware.h +2
-0+} +int assign_fw (struct firmware *fw,struct device *device);#ifdef CONFIG_FW_LOADER
Re: [RESEND PATCH v1 2/8] firmware_loader: Check fw_state_is_done in loading_store
Quoted 41 lines · Tom Rix
Quoted 31 lines · Russ Weight
Add the
fw_state_is_done()function and exit early fromfirmware_loading_store()if the state is already "done". This is being done in preparation for supporting persistent sysfs nodes to allow userspace to upload firmware to a device, potentially reusing the sysfs loading and data files multiple times.Signed-off-by Russ Weight <[email protected]>---
v1:
- No change from RFC patch
---
drivers/base/firmware_loader/fallback.c| 2 +-drivers/base/firmware_loader/firmware.h| 5 +++++2 files changed, 6 insertions(+), 1 deletion(-)drivers/base/firmware_loader/fallback.c +1
-1@@ -250,7 +250,7 @@ static ssize_t firmware_loading_store(struct device *dev, mutex_lock(&fw_lock); fw_priv = fw_sysfs->fw_priv; - if (fw_state_is_aborted(fw_priv)) + if (fw_state_is_aborted(fw_priv) || fw_state_is_done(fw_priv)) goto out; switch (loading) {drivers/base/firmware_loader/firmware.h +0
-0drivers/base/firmware_loader/firmware.h static inline void fw_state_done(struct fw_priv *fw_priv) · line 153
Quoted 5 lines
drivers/base/firmware_loader/firmware.h +2
-0@@ -149,6 +149,11 @@ static inline void fw_state_done(struct fw_priv *fw_priv) __fw_state_set(fw_priv, FW_STATUS_DONE); } +static inline bool fw_state_is_done(struct fw_priv *fw_priv) +{ + return __fw_state_check(fw_priv, FW_STATUS_DONE);This looks like the
fw_sysfs_done()infallback.c
Quoted 1 line
IMO this one and similar wrappers should move to
firmware.hand use the *_is_* naming.
Thanks for catching that Tom. Yes, the new fw_state_is_done() function is exactly the same as fw_sysfs_done(). I'll follow your suggestion and move fw_sysfs_done() and fw_sysfs_loading() to firmware.h as fw_state_is_done() and fw_state_is_loading().
- Russ
Quoted 8 lines
Tom
Quoted 4 lines
drivers/base/firmware_loader/firmware.h +2
-0+} + int assign_fw(struct firmware *fw, struct device *device); #ifdef CONFIG_FW_LOADER
PATCH RESEND 3/8 firmware_loader: Split sysfs support from fallback
In reply to Russ WeightIn preparation for sharing the "loading" and "data" sysfs nodes with the new firmware upload support, split out sysfs functionality from fallback.c and fallback.h into sysfs.c and sysfs.h. This includes the firmware class driver code that is associated with the sysfs files and the fw_fallback_config support for the timeout sysfs node.
CONFIG_FW_LOADER_SYSFS is created and is selected by CONFIG_FW_LOADER_USER_HELPER in order to include sysfs.o in firmware_class-objs.
This is mostly just a code reorganization. There are a few symbols that change in scope, and these can be identified by looking at the header file changes. A few white-space warnings from checkpatch are also addressed in this patch.
---
v1:
- Renamed files fw_sysfs.c and fw_sysfs.h to sysfs.c and sysfs.h
- Moved "MODULE_IMPORT_NS(FIRMWARE_LOADER_PRIVATE);" from sysfs.c to sysfs.h to address an error identified by the kernel test robot <[email protected]>
--- drivers/base/firmware_loader/Kconfig | 4 + drivers/base/firmware_loader/Makefile | 1 + drivers/base/firmware_loader/fallback.c | 430 ------------------------ drivers/base/firmware_loader/fallback.h | 46 +-- drivers/base/firmware_loader/sysfs.c | 411 ++++++++++++++++++++++ drivers/base/firmware_loader/sysfs.h | 96 ++++++ 6 files changed, 513 insertions(+), 475 deletions(-) create mode 100644 drivers/base/firmware_loader/sysfs.c create mode 100644 drivers/base/firmware_loader/sysfs.h
drivers/base/firmware_loader/Kconfig +4 -0
@@ -29,6 +29,9 @@ if FW_LOADER config FW_LOADER_PAGED_BUF bool +config FW_LOADER_SYSFS + bool + config EXTRA_FIRMWARE string "Build named firmware blobs into the kernel binary" help
@@ -72,6 +75,7 @@ config EXTRA_FIRMWARE_DIR config FW_LOADER_USER_HELPER bool "Enable the firmware sysfs fallback mechanism" + select FW_LOADER_SYSFS select FW_LOADER_PAGED_BUF help This option enables a sysfs loading facility to enable firmware
drivers/base/firmware_loader/Makefile +1 -0
@@ -6,5 +6,6 @@ obj-$(CONFIG_FW_LOADER) += firmware_class.o firmware_class-objs := main.o firmware_class-$(CONFIG_FW_LOADER_USER_HELPER) += fallback.o firmware_class-$(CONFIG_EFI_EMBEDDED_FIRMWARE) += fallback_platform.o +firmware_class-$(CONFIG_FW_LOADER_SYSFS) += sysfs.o obj-y += builtin/
drivers/base/firmware_loader/fallback.c +0 -430
462 lines not on this page · raw message
drivers/base/firmware_loader/fallback.h +1 -45
64 lines not on this page · raw message
drivers/base/firmware_loader/sysfs.c new file +411 -0
411 lines not on this page · raw message
drivers/base/firmware_loader/sysfs.h new file +96 -0
96 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [RESEND PATCH v1 3/8] firmware_loader: Split sysfs support from fallback
Quoted 48 lines · Russ Weight
In preparation for sharing the "loading" and "data" sysfs nodes with the new firmware upload support, split out sysfs functionality from
fallback.candfallback.hintosysfs.candsysfs.h. This includes the firmware class driver code that is associated with the sysfs files and thefw_fallback_configsupport for the timeout sysfs node.
CONFIG_FW_LOADER_SYSFSis created and is selected byCONFIG_FW_LOADER_USER_HELPERin order to include sysfs.o in firmware_class-objs.This is mostly just a code reorganization. There are a few symbols that change in scope, and these can be identified by looking at the header file changes. A few white-space warnings from checkpatch are also addressed in this patch.
Signed-off-by Russ Weight <[email protected]>---
v1:
- Renamed filesfw_sysfs.candfw_sysfs.htosysfs.candsysfs.h
- Moved "MODULE_IMPORT_NS(FIRMWARE_LOADER_PRIVATE);" fromsysfs.ctosysfs.hto address an error identified by the kernel test robot <[email protected]>--- drivers/base/firmware_loader/Kconfig | 4 + drivers/base/firmware_loader/Makefile | 1 + drivers/base/firmware_loader/fallback.c | 430 ------------------------ drivers/base/firmware_loader/fallback.h | 46 +-- drivers/base/firmware_loader/sysfs.c | 411 ++++++++++++++++++++++ drivers/base/firmware_loader/sysfs.h | 96 ++++++ 6 files changed, 513 insertions(+), 475 deletions(-)create mode 100644
drivers/base/firmware_loader/sysfs.ccreate mode 100644drivers/base/firmware_loader/sysfs.hdrivers/base/firmware_loader/Kconfig +3
-0@@ -29,6 +29,9 @@ if FW_LOADER config FW_LOADER_PAGED_BUF bool +config FW_LOADER_SYSFS + bool + config EXTRA_FIRMWARE string "Build named firmware blobs into the kernel binary" help
drivers/base/firmware_loader/Kconfig config EXTRA_FIRMWARE_DIR · line 78
Quoted 4 lines
drivers/base/firmware_loader/Kconfig +1
-0@@ -72,6 +75,7 @@ config EXTRA_FIRMWARE_DIR config FW_LOADER_USER_HELPER bool "Enable the firmware sysfs fallback mechanism" + select FW_LOADER_SYSFS
Is this code reordering necessary ?
This config is not removed or renamed later and has the same configs are the later FW_UPLOAD.
Maybe leave fallback.c as-is and rename FW_LOADER_USER_HELPER to FW_LOADER_SYSFS because the name is more descriptive.
The 'sorry we suck' help message replaced with a shorter message to indicate this is now a more capable config.
The later FW_UPLOAD would have a 'depends on FW_LOADER_SYSFS'
If you end up needing to do the reorder, move it to patch 1 because bisecting-wise it should not depend on improvements in the current 1,2 patches.
Tom
Quoted 1,074 lines
drivers/base/firmware_loader/Kconfig +0
-0select FW_LOADER_PAGED_BUF help This option enables a sysfs loading facility to enable firmwaredrivers/base/firmware_loader/Makefile +1
-0@@ -6,5 +6,6 @@ obj-$(CONFIG_FW_LOADER) += firmware_class.o firmware_class-objs := main.o firmware_class-$(CONFIG_FW_LOADER_USER_HELPER) += fallback.o firmware_class-$(CONFIG_EFI_EMBEDDED_FIRMWARE) += fallback_platform.o +firmware_class-$(CONFIG_FW_LOADER_SYSFS) += sysfs.o obj-y += builtin/drivers/base/firmware_loader/fallback.c +0
-430@@ -3,12 +3,9 @@#include <linux/types.h> #include <linux/kconfig.h> #include <linux/list.h> -#include <linux/slab.h> #include <linux/security.h> -#include <linux/highmem.h> #include <linux/umh.h> #include <linux/sysctl.h> -#include <linux/vmalloc.h> #include <linux/module.h> #include "fallback.h" @@ -18,22 +15,6 @@* firmware fallback mechanism */ -MODULE_IMPORT_NS (FIRMWARE_LOADER_PRIVATE ); - -extern struct firmware_fallback_config fw_fallback_config; - -/* These getters are vetted to use int properly */ -static inline int __firmware_loading_timeout (void ) -{ -return fw_fallback_config.loading_timeout; -} - -/* These setters are vetted to use int properly */ -static void __fw_fallback_set_timeout (int timeout) -{ - fw_fallback_config.loading_timeout = timeout; -} -/* * use small loading timeout for caching devices' firmware because all these * firmware images have been loaded successfully at lease once, also system is @@ -58,52 +39,11 @@ static long firmware_loading_timeout(void) __firmware_loading_timeout() * HZ : MAX_JIFFY_OFFSET; } -static inline bool fw_sysfs_done (struct fw_priv *fw_priv) -{ -return __fw_state_check (fw_priv,FW_STATUS_DONE ); -} - -static inline bool fw_sysfs_loading (struct fw_priv *fw_priv) -{ -return __fw_state_check (fw_priv,FW_STATUS_LOADING ); -} -static inline int fw_sysfs_wait_timeout (struct fw_priv *fw_priv,long timeout) {return __fw_state_wait_common (fw_priv, timeout); } -struct fw_sysfs { -bool nowait; -struct device dev; -struct fw_priv *fw_priv; -struct firmware *fw; -}; - -static struct fw_sysfs *to_fw_sysfs (struct device *dev) -{ - return container_of(dev, struct fw_sysfs, dev); -} - -static void __fw_load_abort (struct fw_priv *fw_priv) -{ -/* -* There is a small window in which user can write to 'loading' -* between loading done/aborted and disappearance of 'loading' -*/ -if (fw_state_is_aborted (fw_priv) ||fw_sysfs_done (fw_priv)) -return ; - -fw_state_aborted (fw_priv); -} - -static void fw_load_abort (struct fw_sysfs *fw_sysfs) -{ -struct fw_priv *fw_priv = fw_sysfs->fw_priv; - -__fw_load_abort (fw_priv); -} -static LIST_HEAD (pending_fw_head);void kill_pending_fw_fallback_reqs (bool only_kill_custom)@@ -120,376 +60,6 @@ void kill_pending_fw_fallback_reqs(bool only_kill_custom)mutex_unlock (&fw_lock); } -static ssize_t timeout_show (struct class *class,struct class_attribute *attr, -char *buf) -{ -return sysfs_emit (buf,"%d\n" ,__firmware_loading_timeout ()); -} - -/** -* timeout_store() - set number of seconds to wait for firmware -* @class: device class pointer -* @attr: device attribute pointer -* @buf: buffer to scan for timeout value -* @count: number of bytes in @buf -* -* Sets the number of seconds to wait for the firmware. Once -* this expires an error will be returned to the driver and no -* firmware will be provided. -* -* Note: zero means 'wait forever'. -**/ -static ssize_t timeout_store (struct class *class,struct class_attribute *attr, -const char *buf,size_t count) -{ -int tmp_loading_timeout =simple_strtol (buf,NULL ,10 ); - -if (tmp_loading_timeout <0 ) - tmp_loading_timeout =0 ; - -__fw_fallback_set_timeout (tmp_loading_timeout); - -return count; -} -static CLASS_ATTR_RW (timeout); - -static struct attribute *firmware_class_attrs[] = { - &class_attr_timeout.attr, -NULL , -}; -ATTRIBUTE_GROUPS (firmware_class); - -static void fw_dev_release (struct device *dev) -{ -struct fw_sysfs *fw_sysfs =to_fw_sysfs (dev); - -kfree (fw_sysfs); -} - -static int do_firmware_uevent (struct fw_sysfs *fw_sysfs,struct kobj_uevent_env *env) -{ -if (add_uevent_var (env,"FIRMWARE=%s" , fw_sysfs->fw_priv->fw_name)) -return -ENOMEM ; -if (add_uevent_var (env,"TIMEOUT=%i" ,__firmware_loading_timeout ())) -return -ENOMEM ; -if (add_uevent_var (env,"ASYNC=%d" , fw_sysfs->nowait)) -return -ENOMEM ; - -return 0 ; -} - -static int firmware_uevent (struct device *dev,struct kobj_uevent_env *env) -{ -struct fw_sysfs *fw_sysfs =to_fw_sysfs (dev); -int err =0 ; - -mutex_lock (&fw_lock); -if (fw_sysfs->fw_priv) - err =do_firmware_uevent (fw_sysfs, env); -mutex_unlock (&fw_lock); -return err; -} - -static struct class firmware_class = { - .name ="firmware" , - .class_groups = firmware_class_groups, - .dev_uevent = firmware_uevent, - .dev_release = fw_dev_release, -}; - -int register_sysfs_loader (void ) -{ -int ret =class_register (&firmware_class); - -if (ret !=0 ) -return ret; -return register_firmware_config_sysctl (); -} - -void unregister_sysfs_loader (void ) -{ -unregister_firmware_config_sysctl (); -class_unregister (&firmware_class); -} - -static ssize_t firmware_loading_show (struct device *dev, -struct device_attribute *attr,char *buf) -{ -struct fw_sysfs *fw_sysfs =to_fw_sysfs (dev); -int loading =0 ; - -mutex_lock (&fw_lock); -if (fw_sysfs->fw_priv) - loading =fw_sysfs_loading (fw_sysfs->fw_priv); -mutex_unlock (&fw_lock); - -return sysfs_emit (buf,"%d\n" , loading); -} - -/** -* firmware_loading_store() - set value in the 'loading' control file -* @dev: device pointer -* @attr: device attribute pointer -* @buf: buffer to scan for loading control value -* @count: number of bytes in @buf -* -* The relevant values are: -* -* 1: Start a load, discarding any previous partial load. -* 0: Conclude the load and hand the data to the driver code. -* -1: Conclude the load with an error and discard any written data. -**/ -static ssize_t firmware_loading_store (struct device *dev, -struct device_attribute *attr, -const char *buf,size_t count) -{ -struct fw_sysfs *fw_sysfs =to_fw_sysfs (dev); -struct fw_priv *fw_priv; -ssize_t written = count; -int loading =simple_strtol (buf,NULL ,10 ); - -mutex_lock (&fw_lock); - fw_priv = fw_sysfs->fw_priv; -if (fw_state_is_aborted (fw_priv) ||fw_state_is_done (fw_priv)) - goto out; - -switch (loading) { -case 1 : -/* discarding any previous partial load */ -if (!fw_sysfs_done (fw_priv)) { -fw_free_paged_buf (fw_priv); -fw_state_start (fw_priv); - } -break ; -case 0 : -if (fw_sysfs_loading (fw_priv)) { -int rc; - -/* -* Several loading requests may be pending on -* one same firmware buf, so let all requests -* see the mapped 'buf->data' once the loading -* is completed. -* */ - rc =fw_map_paged_buf (fw_priv); -if (rc) -dev_err (dev,"%s: map pages failed\n" , - __func__); -else - rc =security_kernel_post_load_data (fw_priv->data, - fw_priv->size, -LOADING_FIRMWARE ,"blob" ); - -/* -* Same logic as fw_load_abort, only the DONE bit -* is ignored and we set ABORT only on failure. -*/ -if (rc) { -fw_state_aborted (fw_priv); - written = rc; - }else { -fw_state_done (fw_priv); - } -break ; - } - fallthrough; -default : -dev_err (dev,"%s: unexpected value (%d)\n" , __func__, loading); - fallthrough; -case -1 : -fw_load_abort (fw_sysfs); -break ; - } -out: -mutex_unlock (&fw_lock); -return written; -} - -static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store); - -static void firmware_rw_data (struct fw_priv *fw_priv,char *buffer, -loff_t offset,size_t count,bool read) -{ -if (read) -memcpy (buffer, fw_priv->data + offset, count); -else -memcpy (fw_priv->data + offset, buffer, count); -} - -static void firmware_rw (struct fw_priv *fw_priv,char *buffer, -loff_t offset,size_t count,bool read) -{ -while (count) { -void *page_data; -int page_nr = offset >>PAGE_SHIFT ; -int page_ofs = offset & (PAGE_SIZE -1 ); -int page_cnt =min_t (size_t,PAGE_SIZE - page_ofs, count); - - page_data =kmap (fw_priv->pages[page_nr]); - -if (read) -memcpy (buffer, page_data + page_ofs, page_cnt); -else -memcpy (page_data + page_ofs, buffer, page_cnt); - -kunmap (fw_priv->pages[page_nr]); - buffer += page_cnt; - offset += page_cnt; - count -= page_cnt; - } -} - -static ssize_t firmware_data_read (struct file *filp,struct kobject *kobj, -struct bin_attribute *bin_attr, -char *buffer,loff_t offset,size_t count) -{ -struct device *dev =kobj_to_dev (kobj); -struct fw_sysfs *fw_sysfs =to_fw_sysfs (dev); -struct fw_priv *fw_priv; -ssize_t ret_count; - -mutex_lock (&fw_lock); - fw_priv = fw_sysfs->fw_priv; -if (!fw_priv ||fw_sysfs_done (fw_priv)) { - ret_count = -ENODEV ; - goto out; - } -if (offset > fw_priv->size) { - ret_count =0 ; - goto out; - } -if (count > fw_priv->size - offset) - count = fw_priv->size - offset; - - ret_count = count; - -if (fw_priv->data) -firmware_rw_data (fw_priv, buffer, offset, count, true); -else -firmware_rw (fw_priv, buffer, offset, count, true); - -out: -mutex_unlock (&fw_lock); -return ret_count; -} - -static int fw_realloc_pages (struct fw_sysfs *fw_sysfs,int min_size) -{ -int err; - - err =fw_grow_paged_buf (fw_sysfs->fw_priv, -PAGE_ALIGN (min_size) >>PAGE_SHIFT ); -if (err) -fw_load_abort (fw_sysfs); -return err; -} - -/** -* firmware_data_write() - write method for firmware -* @filp: open sysfs file -* @kobj: kobject for the device -* @bin_attr: bin_attr structure -* @buffer: buffer being written -* @offset: buffer offset for write in total data store area -* @count: buffer size -* -* Data written to the 'data' attribute will be later handed to -* the driver as a firmware image. -**/ -static ssize_t firmware_data_write (struct file *filp,struct kobject *kobj, -struct bin_attribute *bin_attr, -char *buffer,loff_t offset,size_t count) -{ -struct device *dev =kobj_to_dev (kobj); -struct fw_sysfs *fw_sysfs =to_fw_sysfs (dev); -struct fw_priv *fw_priv; -ssize_t retval; - -if (!capable (CAP_SYS_RAWIO )) -return -EPERM ; - -mutex_lock (&fw_lock); - fw_priv = fw_sysfs->fw_priv; -if (!fw_priv ||fw_sysfs_done (fw_priv)) { - retval = -ENODEV ; - goto out; - } - -if (fw_priv->data) { -if (offset + count > fw_priv->allocated_size) { - retval = -ENOMEM ; - goto out; - } -firmware_rw_data (fw_priv, buffer, offset, count, false); - retval = count; - }else { - retval =fw_realloc_pages (fw_sysfs, offset + count); -if (retval) - goto out; - - retval = count; -firmware_rw (fw_priv, buffer, offset, count, false); - } - - fw_priv->size =max_t (size_t, offset + count, fw_priv->size); -out: -mutex_unlock (&fw_lock); -return retval; -} - -static struct bin_attribute firmware_attr_data = { - .attr = { .name ="data" , .mode =0644 }, - .size =0 , - .read = firmware_data_read, - .write = firmware_data_write, -}; - -static struct attribute *fw_dev_attrs[] = { - &dev_attr_loading.attr, -NULL -}; - -static struct bin_attribute *fw_dev_bin_attrs[] = { - &firmware_attr_data, -NULL -}; - -static const struct attribute_group fw_dev_attr_group = { - .attrs = fw_dev_attrs, - .bin_attrs = fw_dev_bin_attrs, -}; - -static const struct attribute_group *fw_dev_attr_groups[] = { - &fw_dev_attr_group, -NULL -}; - -static struct fw_sysfs * -fw_create_instance (struct firmware *firmware,const char *fw_name, -struct device *device,u32 opt_flags) -{ -struct fw_sysfs *fw_sysfs; -struct device *f_dev; - - fw_sysfs =kzalloc (sizeof (*fw_sysfs),GFP_KERNEL ); -if (!fw_sysfs) { - fw_sysfs =ERR_PTR (-ENOMEM ); - goto exit; - } - - fw_sysfs->nowait = !!(opt_flags &FW_OPT_NOWAIT ); - fw_sysfs->fw = firmware; - f_dev = &fw_sysfs->dev; - -device_initialize (f_dev); -dev_set_name (f_dev,"%s" , fw_name); - f_dev->parent = device; - f_dev->class = &firmware_class; - f_dev->groups = fw_dev_attr_groups; -exit: -return fw_sysfs; -} -/** * fw_load_sysfs_fallback() - load a firmware via the sysfs fallback mechanism * @fw_sysfs: firmware sysfs information for the firmware to load drivers/base/firmware_loader/fallback.h +1
-45@@ -6,29 +6,7 @@#include <linux/device.h> #include "firmware.h" - -/** -* struct firmware_fallback_config - firmware fallback configuration settings -* -* Helps describe and fine tune the fallback mechanism. -* -* @force_sysfs_fallback: force the sysfs fallback mechanism to be used -* as if one had enabled CONFIG_FW_LOADER_USER_HELPER_FALLBACK=y. -* Useful to help debug a CONFIG_FW_LOADER_USER_HELPER_FALLBACK=y -* functionality on a kernel where that config entry has been disabled. -* @ignore_sysfs_fallback: force to disable the sysfs fallback mechanism. -* This emulates the behaviour as if we had set the kernel -* config CONFIG_FW_LOADER_USER_HELPER=n. -* @old_timeout: for internal use -* @loading_timeout: the timeout to wait for the fallback mechanism before -* giving up, in seconds. -*/ -struct firmware_fallback_config { -unsigned int force_sysfs_fallback; -unsigned int ignore_sysfs_fallback; -int old_timeout; -int loading_timeout; -}; +#include "sysfs.h" #ifdef CONFIG_FW_LOADER_USER_HELPER int firmware_fallback_sysfs(struct firmware *fw, const char *name,@@ -40,19 +18,6 @@ void kill_pending_fw_fallback_reqs(bool only_kill_custom);void fw_fallback_set_cache_timeout (void );void fw_fallback_set_default_timeout (void ); -int register_sysfs_loader (void ); -void unregister_sysfs_loader (void ); -#ifdef CONFIG_SYSCTL -extern int register_firmware_config_sysctl (void ); -extern void unregister_firmware_config_sysctl (void ); -#else -static inline int register_firmware_config_sysctl (void ) -{ -return 0 ; -} -static inline void unregister_firmware_config_sysctl (void ) { } -#endif /* CONFIG_SYSCTL */ -#else /* CONFIG_FW_LOADER_USER_HELPER */ static inline int firmware_fallback_sysfs(struct firmware *fw, const char *name, struct device *device,@@ -66,15 +31,6 @@ static inline int firmware_fallback_sysfs(struct firmware *fw, const char *name,static inline void kill_pending_fw_fallback_reqs (bool only_kill_custom) { }static inline void fw_fallback_set_cache_timeout (void ) { }static inline void fw_fallback_set_default_timeout (void ) { } - -static inline int register_sysfs_loader (void ) -{ -return 0 ; -} - -static inline void unregister_sysfs_loader (void ) -{ -}#endif /* CONFIG_FW_LOADER_USER_HELPER */ #ifdef CONFIG_EFI_EMBEDDED_FIRMWARE drivers/base/firmware_loader/sysfs.c new file +411
-0@@ -0,0 +1,411 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/highmem.h> +#include <linux/module.h> +#include <linux/security.h> +#include <linux/slab.h> +#include <linux/types.h> + +#include "firmware.h" +#include "sysfs.h" + +/* +* sysfs support for firmware loader +*/ + +static inline bool fw_sysfs_done (struct fw_priv *fw_priv) +{ +return __fw_state_check (fw_priv,FW_STATUS_DONE ); +} + +static inline bool fw_sysfs_loading (struct fw_priv *fw_priv) +{ +return __fw_state_check (fw_priv,FW_STATUS_LOADING ); +} + +void __fw_load_abort (struct fw_priv *fw_priv) +{ +/* +* There is a small window in which user can write to 'loading' +* between loading done/aborted and disappearance of 'loading' +*/ +if (fw_state_is_aborted (fw_priv) ||fw_sysfs_done (fw_priv)) +return ; + +fw_state_aborted (fw_priv); +} + +static ssize_t timeout_show (struct class *class,struct class_attribute *attr, +char *buf) +{ +return sysfs_emit (buf,"%d\n" ,__firmware_loading_timeout ()); +} + +/** +* timeout_store() - set number of seconds to wait for firmware +* @class: device class pointer +* @attr: device attribute pointer +* @buf: buffer to scan for timeout value +* @count: number of bytes in @buf +* +* Sets the number of seconds to wait for the firmware. Once +* this expires an error will be returned to the driver and no +* firmware will be provided. +* +* Note: zero means 'wait forever'. +**/ +static ssize_t timeout_store (struct class *class,struct class_attribute *attr, +const char *buf,size_t count) +{ +int tmp_loading_timeout =simple_strtol (buf,NULL ,10 ); + +if (tmp_loading_timeout <0 ) + tmp_loading_timeout =0 ; + +__fw_fallback_set_timeout (tmp_loading_timeout); + +return count; +} +static CLASS_ATTR_RW (timeout); + +static struct attribute *firmware_class_attrs[] = { + &class_attr_timeout.attr, +NULL , +}; +ATTRIBUTE_GROUPS (firmware_class); + +static void fw_dev_release (struct device *dev) +{ +struct fw_sysfs *fw_sysfs =to_fw_sysfs (dev); + +kfree (fw_sysfs); +} + +#ifdef CONFIG_FW_LOADER_USER_HELPER +static int do_firmware_uevent (struct fw_sysfs *fw_sysfs,struct kobj_uevent_env *env) +{ +if (add_uevent_var (env,"FIRMWARE=%s" , fw_sysfs->fw_priv->fw_name)) +return -ENOMEM ; +if (add_uevent_var (env,"TIMEOUT=%i" ,__firmware_loading_timeout ())) +return -ENOMEM ; +if (add_uevent_var (env,"ASYNC=%d" , fw_sysfs->nowait)) +return -ENOMEM ; + +return 0 ; +} + +static int firmware_uevent (struct device *dev,struct kobj_uevent_env *env) +{ +struct fw_sysfs *fw_sysfs =to_fw_sysfs (dev); +int err =0 ; + +mutex_lock (&fw_lock); +if (fw_sysfs->fw_priv) + err =do_firmware_uevent (fw_sysfs, env); +mutex_unlock (&fw_lock); +return err; +} +#endif /* CONFIG_FW_LOADER_USER_HELPER */ + +static struct class firmware_class = { + .name ="firmware" , + .class_groups = firmware_class_groups, +#ifdef CONFIG_FW_LOADER_USER_HELPER + .dev_uevent = firmware_uevent, +#endif + .dev_release = fw_dev_release, +}; + +int register_sysfs_loader (void ) +{ +int ret =class_register (&firmware_class); + +if (ret !=0 ) +return ret; +return register_firmware_config_sysctl (); +} + +void unregister_sysfs_loader (void ) +{ +unregister_firmware_config_sysctl (); +class_unregister (&firmware_class); +} + +static ssize_t firmware_loading_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct fw_sysfs *fw_sysfs =to_fw_sysfs (dev); +int loading =0 ; + +mutex_lock (&fw_lock); +if (fw_sysfs->fw_priv) + loading =fw_sysfs_loading (fw_sysfs->fw_priv); +mutex_unlock (&fw_lock); + +return sysfs_emit (buf,"%d\n" , loading); +} + +/** +* firmware_loading_store() - set value in the 'loading' control file +* @dev: device pointer +* @attr: device attribute pointer +* @buf: buffer to scan for loading control value +* @count: number of bytes in @buf +* +* The relevant values are: +* +* 1: Start a load, discarding any previous partial load. +* 0: Conclude the load and hand the data to the driver code. +* -1: Conclude the load with an error and discard any written data. +**/ +static ssize_t firmware_loading_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count) +{ +struct fw_sysfs *fw_sysfs =to_fw_sysfs (dev); +struct fw_priv *fw_priv; +ssize_t written = count; +int loading =simple_strtol (buf,NULL ,10 ); + +mutex_lock (&fw_lock); + fw_priv = fw_sysfs->fw_priv; +if (fw_state_is_aborted (fw_priv) ||fw_state_is_done (fw_priv)) + goto out; + +switch (loading) { +case 1 : +/* discarding any previous partial load */ +if (!fw_sysfs_done (fw_priv)) { +fw_free_paged_buf (fw_priv); +fw_state_start (fw_priv); + } +break ; +case 0 : +if (fw_sysfs_loading (fw_priv)) { +int rc; + +/* +* Several loading requests may be pending on +* one same firmware buf, so let all requests +* see the mapped 'buf->data' once the loading +* is completed. +*/ + rc =fw_map_paged_buf (fw_priv); +if (rc) +dev_err (dev,"%s: map pages failed\n" , + __func__); +else + rc =security_kernel_post_load_data (fw_priv->data, + fw_priv->size, +LOADING_FIRMWARE , +"blob" ); + +/* +* Same logic as fw_load_abort, only the DONE bit +* is ignored and we set ABORT only on failure. +*/ +if (rc) { +fw_state_aborted (fw_priv); + written = rc; + }else { +fw_state_done (fw_priv); + } +break ; + } + fallthrough; +default : +dev_err (dev,"%s: unexpected value (%d)\n" , __func__, loading); + fallthrough; +case -1 : +fw_load_abort (fw_sysfs); +break ; + } +out: +mutex_unlock (&fw_lock); +return written; +} + +static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store); + +static void firmware_rw_data (struct fw_priv *fw_priv,char *buffer, +loff_t offset,size_t count,bool read) +{ +if (read) +memcpy (buffer, fw_priv->data + offset, count); +else +memcpy (fw_priv->data + offset, buffer, count); +} + +static void firmware_rw (struct fw_priv *fw_priv,char *buffer, +loff_t offset,size_t count,bool read) +{ +while (count) { +void *page_data; +int page_nr = offset >>PAGE_SHIFT ; +int page_ofs = offset & (PAGE_SIZE -1 ); +int page_cnt =min_t (size_t,PAGE_SIZE - page_ofs, count); + + page_data =kmap (fw_priv->pages[page_nr]); + +if (read) +memcpy (buffer, page_data + page_ofs, page_cnt); +else +memcpy (page_data + page_ofs, buffer, page_cnt); + +kunmap (fw_priv->pages[page_nr]); + buffer += page_cnt; + offset += page_cnt; + count -= page_cnt; + } +} + +static ssize_t firmware_data_read (struct file *filp,struct kobject *kobj, +struct bin_attribute *bin_attr, +char *buffer,loff_t offset,size_t count) +{ +struct device *dev =kobj_to_dev (kobj); +struct fw_sysfs *fw_sysfs =to_fw_sysfs (dev); +struct fw_priv *fw_priv; +ssize_t ret_count; + +mutex_lock (&fw_lock); + fw_priv = fw_sysfs->fw_priv; +if (!fw_priv ||fw_sysfs_done (fw_priv)) { + ret_count = -ENODEV ; + goto out; + } +if (offset > fw_priv->size) { + ret_count =0 ; + goto out; + } +if (count > fw_priv->size - offset) + count = fw_priv->size - offset; + + ret_count = count; + +if (fw_priv->data) +firmware_rw_data (fw_priv, buffer, offset, count, true); +else +firmware_rw (fw_priv, buffer, offset, count, true); + +out: +mutex_unlock (&fw_lock); +return ret_count; +} + +static int fw_realloc_pages (struct fw_sysfs *fw_sysfs,int min_size) +{ +int err; + + err =fw_grow_paged_buf (fw_sysfs->fw_priv, +PAGE_ALIGN (min_size) >>PAGE_SHIFT ); +if (err) +fw_load_abort (fw_sysfs); +return err; +} + +/** +* firmware_data_write() - write method for firmware +* @filp: open sysfs file +* @kobj: kobject for the device +* @bin_attr: bin_attr structure +* @buffer: buffer being written +* @offset: buffer offset for write in total data store area +* @count: buffer size +* +* Data written to the 'data' attribute will be later handed to +* the driver as a firmware image. +**/ +static ssize_t firmware_data_write (struct file *filp,struct kobject *kobj, +struct bin_attribute *bin_attr, +char *buffer,loff_t offset,size_t count) +{ +struct device *dev =kobj_to_dev (kobj); +struct fw_sysfs *fw_sysfs =to_fw_sysfs (dev); +struct fw_priv *fw_priv; +ssize_t retval; + +if (!capable (CAP_SYS_RAWIO )) +return -EPERM ; + +mutex_lock (&fw_lock); + fw_priv = fw_sysfs->fw_priv; +if (!fw_priv ||fw_sysfs_done (fw_priv)) { + retval = -ENODEV ; + goto out; + } + +if (fw_priv->data) { +if (offset + count > fw_priv->allocated_size) { + retval = -ENOMEM ; + goto out; + } +firmware_rw_data (fw_priv, buffer, offset, count, false); + retval = count; + }else { + retval =fw_realloc_pages (fw_sysfs, offset + count); +if (retval) + goto out; + + retval = count; +firmware_rw (fw_priv, buffer, offset, count, false); + } + + fw_priv->size =max_t (size_t, offset + count, fw_priv->size); +out: +mutex_unlock (&fw_lock); +return retval; +} + +static struct bin_attribute firmware_attr_data = { + .attr = { .name ="data" , .mode =0644 }, + .size =0 , + .read = firmware_data_read, + .write = firmware_data_write, +}; + +static struct attribute *fw_dev_attrs[] = { + &dev_attr_loading.attr, +NULL +}; + +static struct bin_attribute *fw_dev_bin_attrs[] = { + &firmware_attr_data, +NULL +}; + +static const struct attribute_group fw_dev_attr_group = { + .attrs = fw_dev_attrs, + .bin_attrs = fw_dev_bin_attrs, +}; + +static const struct attribute_group *fw_dev_attr_groups[] = { + &fw_dev_attr_group, +NULL +}; + +struct fw_sysfs * +fw_create_instance (struct firmware *firmware,const char *fw_name, +struct device *device,u32 opt_flags) +{ +struct fw_sysfs *fw_sysfs; +struct device *f_dev; + + fw_sysfs =kzalloc (sizeof (*fw_sysfs),GFP_KERNEL ); +if (!fw_sysfs) { + fw_sysfs =ERR_PTR (-ENOMEM ); + goto exit; + } + + fw_sysfs->nowait = !!(opt_flags &FW_OPT_NOWAIT ); + fw_sysfs->fw = firmware; + f_dev = &fw_sysfs->dev; + +device_initialize (f_dev); +dev_set_name (f_dev,"%s" , fw_name); + f_dev->parent = device; + f_dev->class = &firmware_class; + f_dev->groups = fw_dev_attr_groups; +exit: +return fw_sysfs; +}drivers/base/firmware_loader/sysfs.h new file +96
-0@@ -0,0 +1,96 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __FIRMWARE_SYSFS_H +#define __FIRMWARE_SYSFS_H + +#include <linux/device.h> + +MODULE_IMPORT_NS (FIRMWARE_LOADER_PRIVATE ); + +extern struct firmware_fallback_config fw_fallback_config; + +#ifdef CONFIG_FW_LOADER_USER_HELPER +/** +* struct firmware_fallback_config - firmware fallback configuration settings +* +* Helps describe and fine tune the fallback mechanism. +* +* @force_sysfs_fallback: force the sysfs fallback mechanism to be used +* as if one had enabled CONFIG_FW_LOADER_USER_HELPER_FALLBACK=y. +* Useful to help debug a CONFIG_FW_LOADER_USER_HELPER_FALLBACK=y +* functionality on a kernel where that config entry has been disabled. +* @ignore_sysfs_fallback: force to disable the sysfs fallback mechanism. +* This emulates the behaviour as if we had set the kernel +* config CONFIG_FW_LOADER_USER_HELPER=n. +* @old_timeout: for internal use +* @loading_timeout: the timeout to wait for the fallback mechanism before +* giving up, in seconds. +*/ +struct firmware_fallback_config { +unsigned int force_sysfs_fallback; +unsigned int ignore_sysfs_fallback; +int old_timeout; +int loading_timeout; +}; + +int register_sysfs_loader (void ); +void unregister_sysfs_loader (void ); +#ifdef CONFIG_SYSCTL +int register_firmware_config_sysctl (void ); +void unregister_firmware_config_sysctl (void ); +#else +static inline int register_firmware_config_sysctl (void ) +{ +return 0 ; +} + +static inline void unregister_firmware_config_sysctl (void ) { } +#endif /* CONFIG_SYSCTL */ +#else /* CONFIG_FW_LOADER_USER_HELPER */ +static inline int register_sysfs_loader (void ) +{ +return 0 ; +} + +static inline void unregister_sysfs_loader (void ) +{ +} +#endif /* CONFIG_FW_LOADER_USER_HELPER */ + +struct fw_sysfs { +bool nowait; +struct device dev; +struct fw_priv *fw_priv; +struct firmware *fw; +}; + +static inline struct fw_sysfs *to_fw_sysfs (struct device *dev) +{ + return container_of(dev, struct fw_sysfs, dev); +} + +/* These getters are vetted to use int properly */ +static inline int __firmware_loading_timeout (void ) +{ +return fw_fallback_config.loading_timeout; +} + +/* These setters are vetted to use int properly */ +static inline void __fw_fallback_set_timeout (int timeout) +{ + fw_fallback_config.loading_timeout = timeout; +} + +void __fw_load_abort (struct fw_priv *fw_priv); + +static inline void fw_load_abort (struct fw_sysfs *fw_sysfs) +{ +struct fw_priv *fw_priv = fw_sysfs->fw_priv; + +__fw_load_abort (fw_priv); +} + +struct fw_sysfs * +fw_create_instance (struct firmware *firmware,const char *fw_name, +struct device *device,u32 opt_flags); + +#endif /* __FIRMWARE_SYSFS_H */
Re: [RESEND PATCH v1 3/8] firmware_loader: Split sysfs support from fallback
Quoted 55 lines · Tom Rix
Quoted 47 lines · Russ Weight
In preparation for sharing the "loading" and "data" sysfs nodes with the new firmware upload support, split out sysfs functionality from
fallback.candfallback.hintosysfs.candsysfs.h. This includes the firmware class driver code that is associated with the sysfs files and thefw_fallback_configsupport for the timeout sysfs node.
CONFIG_FW_LOADER_SYSFSis created and is selected byCONFIG_FW_LOADER_USER_HELPERin order to include sysfs.o in firmware_class-objs.This is mostly just a code reorganization. There are a few symbols that change in scope, and these can be identified by looking at the header file changes. A few white-space warnings from checkpatch are also addressed in this patch.
Signed-off-by Russ Weight <[email protected]>---
v1:
- Renamed filesfw_sysfs.candfw_sysfs.htosysfs.candsysfs.h
- Moved "MODULE_IMPORT_NS(FIRMWARE_LOADER_PRIVATE);" fromsysfs.ctosysfs.hto address an error identified by the kernel test robot <[email protected]>
---
drivers/base/firmware_loader/Kconfig | 4 + drivers/base/firmware_loader/Makefile | 1 +drivers/base/firmware_loader/fallback.c| 430 ------------------------drivers/base/firmware_loader/fallback.h| 46 +-- drivers/base/firmware_loader/sysfs.c | 411 ++++++++++++++++++++++ drivers/base/firmware_loader/sysfs.h | 96 ++++++6 files changed, 513 insertions(+), 475 deletions(-)create mode 100644
drivers/base/firmware_loader/sysfs.ccreate mode 100644drivers/base/firmware_loader/sysfs.hdrivers/base/firmware_loader/Kconfig +0
-0@@ -29,6 +29,9 @@ if FW_LOADERconfig
FW_LOADER_PAGED_BUF
bool
+configFW_LOADER_SYSFS
+ bool
+
configEXTRA_FIRMWARE
string "Build named firmware blobs into the kernel binary" helpdrivers/base/firmware_loader/Kconfig +0
-0@@ -72,6 +75,7 @@ config EXTRA_FIRMWARE_DIRQuoted 3 lines
config
FW_LOADER_USER_HELPER
bool "Enable the firmware sysfs fallback mechanism" + selectFW_LOADER_SYSFSIs this code reordering necessary ?
Quoted 1 line
This config is not removed or renamed later and has the same configs are the later
FW_UPLOAD.
FW_UPLOAD and FW_LOADER_USER_HELPER both select FW_LOADER_SYSFS and FW_LOADER_PAGED_BUF, but they aren't exactly the same. With the split, you can have fallback.c without compiling sysfs_upload.c and you can have sysfs_upload.c without compiling fallback.c.
Quoted 6 lines
Maybe leave
fallback.cas-is and renameFW_LOADER_USER_HELPERtoFW_LOADER_SYSFSbecause the name is more descriptive.The 'sorry we suck' help message replaced with a shorter message to indicate this is now a more capable config.
The later
FW_UPLOADwould have a 'depends onFW_LOADER_SYSFS'
I could, of course, leave fallback.c as is and add to it, but then you couldn't compile in the firmware-upload support without also including the firmware-fallback support, and vice versa. That seems like a disadvantage to me. Isn't it better to allow a user to select these features separately but have them share code if they are both selected?
Quoted 2 lines
If you end up needing to do the reorder, move it to patch 1 because bisecting-wise it should not depend on improvements in the current 1,2 patches.
Patches 1 & 2 are standalone patches. They could be accepted independently without implementing firmware-upload. They don't depend on the code reorganization, and in my opinion, they are easier to understand when viewed before the code reorganization.
What do other's think? Should I keep all the code in fallback.c? Or is it better to split out the code? If I keep the split, should I move patches 1 & 2 to after the split?
- Russ
Quoted 6 lines
Tom
Quoted 3 lines
select
FW_LOADER_PAGED_BUF
help
This option enables a sysfs loading facility to enable firmware
PATCH RESEND 4/8 firmware_loader: Add firmware-upload support
In reply to Russ WeightExtend the firmware subsystem to support a persistent sysfs interface that userspace may use to initiate a firmware update. For example, FPGA based PCIe cards load firmware and FPGA images from local FLASH when the card boots. The images in FLASH may be updated with new images provided by the user at his/her convenience.
A device driver may call firmware_upload_register() to expose persistent "loading" and "data" sysfs files. These files are used in the same way as the fallback sysfs "loading" and "data" files. When 0 is written to "loading" to complete the write of firmware data, the data is transferred to the lower-level driver using pre-registered call-back functions. The data transfer is done in the context of a kernel worker thread.
---
v1:
- renamed fw_upload_register() and fw_upload_unregister() to firmware_upload_register() and fw_upload_unregister().
- Moved ifdef'd section of code out of firmware_loading_store() in sysfs.c into a new function, fw_upload_start(), in sysfs_upload.c.
- Changed #defines to enums for error codes and progress states
- Added additional kernel-doc supported symbols into the documentation. Some rewording in documentation as well.
- Added module reference counting for the parent module in the firmware_upload_register() and firmware_upload_unregister() functions to fix problems found when testing with test_firmware module.
- Removed unnecessary module reference counting for THIS_MODULE. This module holds a reference count for the parent module, and the parent module has symbol dependencies on this module.
--- .../ABI/testing/sysfs-class-firmware | 32 ++ .../driver-api/firmware/fw_upload.rst | 100 +++++++ Documentation/driver-api/firmware/index.rst | 1 + drivers/base/firmware_loader/Kconfig | 14 + drivers/base/firmware_loader/Makefile | 1 + drivers/base/firmware_loader/firmware.h | 6 + drivers/base/firmware_loader/main.c | 16 +- drivers/base/firmware_loader/sysfs.c | 19 +- drivers/base/firmware_loader/sysfs.h | 4 + drivers/base/firmware_loader/sysfs_upload.c | 275 ++++++++++++++++++ drivers/base/firmware_loader/sysfs_upload.h | 42 +++ include/linux/firmware.h | 82 ++++++ 12 files changed, 580 insertions(+), 12 deletions(-) create mode 100644 Documentation/ABI/testing/sysfs-class-firmware create mode 100644 Documentation/driver-api/firmware/fw_upload.rst create mode 100644 drivers/base/firmware_loader/sysfs_upload.c create mode 100644 drivers/base/firmware_loader/sysfs_upload.h
Documentation/ABI/testing/sysfs-class-firmware new file +32 -0
@@ -0,0 +1,32 @@ +What: /sys/class/firmware/.../data +Date: Mar 2022 +KernelVersion: 5.18 +Contact: Russ Weight <[email protected]> +Description: The data sysfs file is used for firmware-fallback and for + firmware uploads. Cat a firmware image to this sysfs file + after you echo 1 to the loading sysfs file. When the firmware + image write is complete, echo 0 to the loading sysfs file. This + sequence will signal the completion of the firmware write and + signal the lower-level driver that the firmware data is + available. + +What: /sys/class/firmware/.../loading +Date: Mar 2022 +KernelVersion: 5.18 +Contact: Russ Weight <[email protected]> +Description: The loading sysfs file is used for both firmware-fallback and + for firmware uploads. Echo 1 onto the loading file to indicate + you are writing a firmware file to the data sysfs node. Echo + -1 onto this file to abort the data write or echo 0 onto this + file to indicate that the write is complete. For firmware + uploads, the zero value also triggers the transfer of the + firmware data to the lower-level device driver. + +What: /sys/class/firmware/.../timeout +Date: Mar 2022 +KernelVersion: 5.18 +Contact: Russ Weight <[email protected]> +Description: This file supports the timeout mechanism for firmware + fallback. This file has no affect on firmware uploads. For + more information on timeouts please see the documentation + for firmware fallback.
Documentation/driver-api/firmware/fw_upload.rst new file +100 -0
100 lines not on this page · raw message
Documentation/driver-api/firmware/index.rst +1 -0
@@ -8,6 +8,7 @@ Linux Firmware API core efi/index request_firmware + fw_upload other_interfaces .. only:: subproject and html
drivers/base/firmware_loader/Kconfig +14 -0
@@ -190,5 +190,19 @@ config FW_CACHE If unsure, say Y. +config FW_UPLOAD + bool "Enable users to initiate firmware updates using sysfs" + select FW_LOADER_SYSFS + select FW_LOADER_PAGED_BUF + help + Enabling this option will allow device drivers to expose a persistent + sysfs interface that allows firmware updates to be initiated from + userspace. For example, FPGA based PCIe cards load firmware and FPGA + images from local FLASH when the card boots. The images in FLASH may + be updated with new images provided by the user. Enable this device + to support cards that rely on user-initiated updates for firmware files. + + If unsure, say N. + endif # FW_LOADER endmenu
drivers/base/firmware_loader/Makefile +1 -0
@@ -7,5 +7,6 @@ firmware_class-objs := main.o firmware_class-$(CONFIG_FW_LOADER_USER_HELPER) += fallback.o firmware_class-$(CONFIG_EFI_EMBEDDED_FIRMWARE) += fallback_platform.o firmware_class-$(CONFIG_FW_LOADER_SYSFS) += sysfs.o +firmware_class-$(CONFIG_FW_UPLOAD) += sysfs_upload.o obj-y += builtin/
drivers/base/firmware_loader/firmware.h +6 -0
@@ -87,6 +87,7 @@ struct fw_priv { };extern struct mutex fw_lock; +extern struct firmware_cache fw_cache;static inline bool __fw_state_check (struct fw_priv *fw_priv,enum fw_status status)
@@ -154,7 +155,12 @@ static inline bool fw_state_is_done(struct fw_priv *fw_priv)return __fw_state_check (fw_priv,FW_STATUS_DONE ); } +int alloc_lookup_fw_priv (const char *fw_name,struct firmware_cache *fwc, +struct fw_priv **fw_priv,void *dbuf,size_t size, +size_t offset,u32 opt_flags);int assign_fw (struct firmware *fw,struct device *device); +void free_fw_priv (struct fw_priv *fw_priv); +void fw_state_init (struct fw_priv *fw_priv);#ifdef CONFIG_FW_LOADER bool firmware_is_builtin (const struct firmware *fw);
drivers/base/firmware_loader/main.c +6 -10
@@ -91,9 +91,9 @@ static inline struct fw_priv *to_fw_priv(struct kref *ref)* guarding for corner cases a global lock should be OK */ DEFINE_MUTEX (fw_lock); -static struct firmware_cache fw_cache; +struct firmware_cache fw_cache; -static void fw_state_init (struct fw_priv *fw_priv) +void fw_state_init (struct fw_priv *fw_priv) {struct fw_state *fw_st = &fw_priv->fw_st;
@@ -163,13 +163,9 @@ static struct fw_priv *__lookup_fw_priv(const char *fw_name) }/* Returns 1 for batching firmware requests with the same name */ -static int alloc_lookup_fw_priv (const char *fw_name, -struct firmware_cache *fwc, -struct fw_priv **fw_priv, -void *dbuf, -size_t size, -size_t offset, -u32 opt_flags) +int alloc_lookup_fw_priv (const char *fw_name,struct firmware_cache *fwc, +struct fw_priv **fw_priv,void *dbuf,size_t size, +size_t offset,u32 opt_flags) {struct fw_priv *tmp;
@@ -224,7 +220,7 @@ static void __free_fw_priv(struct kref *ref)kfree (fw_priv); } -static void free_fw_priv (struct fw_priv *fw_priv) +void free_fw_priv (struct fw_priv *fw_priv) {struct firmware_cache *fwc = fw_priv->fwc;spin_lock (&fwc->lock);
drivers/base/firmware_loader/sysfs.c +17 -2
@@ -6,8 +6,8 @@#include <linux/slab.h> #include <linux/types.h> -#include "firmware.h" #include "sysfs.h" +#include "sysfs_upload.h" /* * sysfs support for firmware loader
@@ -78,6 +78,10 @@ static void fw_dev_release(struct device *dev) {struct fw_sysfs *fw_sysfs =to_fw_sysfs (dev); +if (fw_sysfs->fw_upload_priv) { +free_fw_priv (fw_sysfs->fw_priv); +kfree (fw_sysfs->fw_upload_priv); + }kfree (fw_sysfs); }
@@ -209,6 +213,14 @@ static ssize_t firmware_loading_store(struct device *dev, written = rc; }else {fw_state_done (fw_priv); + +/* +* If this is a user-initiated firmware upload +* then start the upload in a worker thread now. +*/ + rc =fw_upload_start (fw_sysfs); +if (rc) + written = rc; }break ; }
@@ -218,6 +230,9 @@ static ssize_t firmware_loading_store(struct device *dev, fallthrough;case -1 :fw_load_abort (fw_sysfs); +if (fw_sysfs->fw_upload_priv) +fw_state_init (fw_sysfs->fw_priv); +break ; } out:
@@ -225,7 +240,7 @@ static ssize_t firmware_loading_store(struct device *dev,return written; } -static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store); +DEVICE_ATTR (loading,0644 , firmware_loading_show, firmware_loading_store);static void firmware_rw_data (struct fw_priv *fw_priv,char *buffer,loff_t offset,size_t count,bool read)
drivers/base/firmware_loader/sysfs.h +4 -0
@@ -4,9 +4,12 @@#include <linux/device.h> +#include "firmware.h" +MODULE_IMPORT_NS (FIRMWARE_LOADER_PRIVATE );extern struct firmware_fallback_config fw_fallback_config; +extern struct device_attribute dev_attr_loading;#ifdef CONFIG_FW_LOADER_USER_HELPER /**
@@ -61,6 +64,7 @@ struct fw_sysfs {struct device dev;struct fw_priv *fw_priv;struct firmware *fw; +void *fw_upload_priv; };static inline struct fw_sysfs *to_fw_sysfs (struct device *dev)
drivers/base/firmware_loader/sysfs_upload.c new file +275 -0
275 lines not on this page · raw message
drivers/base/firmware_loader/sysfs_upload.h new file +41 -0
41 lines not on this page · raw message
include/linux/firmware.h +82 -0
94 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH RESEND 5/8 firmware_loader: Add sysfs nodes to monitor fw_upload
In reply to Russ WeightAdd additional sysfs nodes to monitor the transfer of firmware upload data to the target device:
cancel: Write 1 to cancel the data transfer
error: Display error status for a failed firmware uploadremaining_size: Display the remaining amount of data to be transferred
status: Display the progress of the firmware upload
---
v1:
- Adapted to enums and filename changes. Otherwise no changes.
--- .../ABI/testing/sysfs-class-firmware | 45 +++++++ .../driver-api/firmware/fw_upload.rst | 19 ++- drivers/base/firmware_loader/sysfs.c | 9 ++ drivers/base/firmware_loader/sysfs_upload.c | 121 ++++++++++++++++++ drivers/base/firmware_loader/sysfs_upload.h | 5 + 5 files changed, 198 insertions(+), 1 deletion(-)
Documentation/ABI/testing/sysfs-class-firmware +45 -0
@@ -10,6 +10,30 @@ Description: The data sysfs file is used for firmware-fallback and for signal the lower-level driver that the firmware data is available. +What: /sys/class/firmware/.../cancel +Date: Mar 2022 +KernelVersion: 5.18 +Contact: Russ Weight <[email protected]> +Description: Write-only. For firmware uploads, write a "1" to this file to + request that the transfer of firmware data to the lower-level + device be canceled. This request will be rejected (EBUSY) if + the update cannot be canceled (e.g. a FLASH write is in + progress) or (ENODEV) if there is no firmware update in progress. + +What: /sys/class/firmware/.../error +Date: Mar 2022 +KernelVersion: 5.18 +Contact: Russ Weight <[email protected]> +Description: Read-only. Returns a string describing a failed firmware + upload. This string will be in the form of <STATUS>:<ERROR>, + where <STATUS> will be one of the status strings described + for the status sysfs file and <ERROR> will be one of the + following: "hw-error", "timeout", "user-abort", "device-busy", + "invalid-file-size", "read-write-error", "flash-wearout". The + error sysfs file is only meaningful when the current firmware + upload status is "idle". If this file is read while a firmware + transfer is in progress, then the read will fail with EBUSY. + What: /sys/class/firmware/.../loading Date: Mar 2022 KernelVersion: 5.18
@@ -22,6 +46,27 @@ Description: The loading sysfs file is used for both firmware-fallback and uploads, the zero value also triggers the transfer of the firmware data to the lower-level device driver. +What: /sys/class/firmware/.../remaining_size +Date: Mar 2022 +KernelVersion: 5.18 +Contact: Russ Weight <[email protected]> +Description: Read-only. For firmware upload, this file contains the size + of the firmware data that remains to be transferred to the + lower-level device driver. The size value is initialized to + the full size of the firmware image that was previously + written to the data sysfs file. This value is periodically + updated during the "transferring" phase of the firmware + upload. + Format: "%u". + +What: /sys/class/firmware/.../status +Date: Mar 2022 +KernelVersion: 5.18 +Contact: Russ Weight <[email protected]> +Description: Read-only. Returns a string describing the current status of + a firmware upload. The string will be one of the following: + idle, "receiving", "preparing", "transferring", "programming". + What: /sys/class/firmware/.../timeout Date: Mar 2022 KernelVersion: 5.18
Documentation/driver-api/firmware/fw_upload.rst +18 -1
@@ -9,7 +9,8 @@ persistent sysfs nodes to enable users to initiate firmware updates for that device. It is the responsibility of the device driver and/or the device itself to perform any validation on the data received. Firmware upload uses the same *loading* and *data* sysfs files described in the -documentation for firmware fallback. +documentation for firmware fallback. It also adds additional sysfs files +to provide status on the transfer of the firmware image to the device. Register for firmware upload ============================
@@ -98,3 +99,19 @@ failure: .. kernel-doc:: include/linux/firmware.h :identifiers: fw_upload_err + +Sysfs Attributes +================ + +In addition to the *loading* and *data* sysfs files, there are additional +sysfs files to monitor the status of the data transfer to the target +device and to determine the final pass/fail status of the transfer. +Depending on the device and the size of the firmware image, a firmware +update could take milliseconds or minutes. + +The additional sysfs files are: + +* status - provides an indication of the progress of a firmware update +* error - provides error information for a failed firmware update +* remaining_size - tracks the data transfer portion of an update +* cancel - echo 1 to this file to cancel the update
drivers/base/firmware_loader/sysfs.c +9 -0
@@ -381,6 +381,12 @@ static struct bin_attribute firmware_attr_data = {static struct attribute *fw_dev_attrs[] = { &dev_attr_loading.attr, +#ifdef CONFIG_FW_UPLOAD + &dev_attr_cancel.attr, + &dev_attr_status.attr, + &dev_attr_error.attr, + &dev_attr_remaining_size.attr, +#endifNULL };
@@ -392,6 +398,9 @@ static struct bin_attribute *fw_dev_bin_attrs[] = {static const struct attribute_group fw_dev_attr_group = { .attrs = fw_dev_attrs, .bin_attrs = fw_dev_bin_attrs, +#ifdef CONFIG_FW_UPLOAD + .is_visible = fw_upload_is_visible, +#endif };static const struct attribute_group *fw_dev_attr_groups[] = {
drivers/base/firmware_loader/sysfs_upload.c +121 -0
127 lines not on this page · raw message
drivers/base/firmware_loader/sysfs_upload.h +5 -0
@@ -30,6 +30,11 @@ struct fw_upload_priv { };#ifdef CONFIG_FW_UPLOAD +extern struct device_attribute dev_attr_status; +extern struct device_attribute dev_attr_error; +extern struct device_attribute dev_attr_cancel; +extern struct device_attribute dev_attr_remaining_size; +int fw_upload_start (struct fw_sysfs *fw_sysfs);umode_t fw_upload_is_visible (struct kobject *kobj,struct attribute *attr,int n);#else
Signature · 1 line
2.25.1
PATCH RESEND 6/8 test_firmware: Add test support for firmware upload
In reply to Russ WeightAdd support for testing the firmware upload driver. There are four sysfs nodes added:
upload_register: write-only
Write the name of the firmware device node to be created
upload_unregister: write-only
Write the name of the firmware device node to be destroyed
config_upload_name: read/write
Set the name to be used by upload_read
upload_read: read-only
Read back the data associated with the firmware device node named in config_upload_name
You can create multiple, concurrent firmware device nodes for firmware upload testing. Read firmware back and validate it using config_upload_name and upload_red.
Example:
$ cd /sys/devices/virtual/misc/test_firmware
$ echo -n fw1 > upload_register
$ ls fw1
cancel data device error loading power remaining_size status
subsystem uevent
$ dd if=/dev/urandom of=/tmp/random-firmware.bin bs=512 count=4
4+0 records in
4+0 records out
2048 bytes (2.0 kB, 2.0 KiB) copied, 0.000131959 s, 15.5 MB/s
$ echo 1 > fw1/loading
$ cat /tmp/random-firmware.bin > fw1/data
$ echo 0 > fw1/loading
$ cat fw1/status
idle
$ cat fw1/error
$ echo -n fw1 > config_upload_name
$ cmp /tmp/random-firmware.bin upload_read
$ echo $?
0
$ echo -n fw1 > upload_unregister--- lib/test_firmware.c | 261 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 261 insertions(+)
lib/test_firmware.c +261 -0
@@ -31,9 +31,12 @@ MODULE_IMPORT_NS(TEST_FIRMWARE);#define TEST_FIRMWARE_NAME "test-firmware.bin"#define TEST_FIRMWARE_NUM_REQS 4#define TEST_FIRMWARE_BUF_SIZE SZ_1K +#define TEST_UPLOAD_MAX_SIZE SZ_2K +#define TEST_UPLOAD_BLK_SIZE 37/* Avoid powers of two in testing */ static DEFINE_MUTEX (test_fw_mutex);static const struct firmware *test_firmware; +static LIST_HEAD (test_upload_list);struct test_batched_req {u8 idx;
@@ -63,6 +66,7 @@ struct test_batched_req {* @reqs: stores all requests information * @read_fw_idx: index of thread from which we want to read firmware results * from through the read_fw trigger. +* @upload_name: firmware name to be used with upload_read sysfs node * @test_result: a test may use this to collect the result from the call * of the request_firmware*() calls used in their tests. In order of * priority we always keep first any setup error. If no setup errors were
@@ -101,6 +105,7 @@ struct test_config {bool send_uevent;u8 num_requests;u8 read_fw_idx; +char *upload_name;/* * These below don't belong her but we'll move them once we create
@@ -112,8 +117,28 @@ struct test_config { struct device *device); }; +struct test_firmware_upload { +char *name; +struct list_head node; +char *buf; +size_t size; +bool cancel_request; +struct fw_upload *fwl; +}; +static struct test_config *test_fw_config; +static struct test_firmware_upload *upload_lookup_name (const char *name) +{ +struct test_firmware_upload *tst; + +list_for_each_entry (tst, &test_upload_list, node) +if (strncmp (name, tst->name,strlen (tst->name)) ==0 ) +return tst; + +return NULL ; +} +static ssize_t test_fw_misc_read (struct file *f,char __user *buf,size_t size,loff_t *offset) {
@@ -198,6 +223,7 @@ static int __test_firmware_config_init(void) test_fw_config->req_firmware = request_firmware; test_fw_config->test_result =0 ; test_fw_config->reqs =NULL ; + test_fw_config->upload_name =NULL ;return 0 ;
@@ -277,6 +303,13 @@ static ssize_t config_show(struct device *dev, test_fw_config->sync_direct ? "true" : "false"); len +=scnprintf (buf + len,PAGE_SIZE - len,"read_fw_idx:\t%u\n" , test_fw_config->read_fw_idx); +if (test_fw_config->upload_name) + len +=scnprintf (buf + len,PAGE_SIZE - len, +"upload_name:\t%s\n" , + test_fw_config->upload_name); +else + len +=scnprintf (buf + len,PAGE_SIZE - len, +"upload_name:\tEMTPY\n" );mutex_unlock (&test_fw_mutex);
@@ -392,6 +425,32 @@ static ssize_t config_name_show(struct device *dev, }static DEVICE_ATTR_RW (config_name); +static ssize_t config_upload_name_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count) +{ +struct test_firmware_upload *tst; +int ret = count; + +mutex_lock (&test_fw_mutex); + tst =upload_lookup_name (buf); +if (tst) + test_fw_config->upload_name = tst->name; +else + ret = -EINVAL ; +mutex_unlock (&test_fw_mutex); + +return ret; +} + +static ssize_t config_upload_name_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +return config_test_show_str (buf, test_fw_config->upload_name); +} +static DEVICE_ATTR_RW (config_upload_name); +static ssize_t config_num_requests_store (struct device *dev,struct device_attribute *attr,const char *buf,size_t count)
@@ -989,6 +1048,167 @@ ssize_t trigger_batched_requests_async_store(struct device *dev, }static DEVICE_ATTR_WO (trigger_batched_requests_async); +static void upload_release (struct test_firmware_upload *tst) +{ +firmware_upload_unregister (tst->fwl); +kfree (tst->buf); +kfree (tst->name); +kfree (tst); +} + +static void upload_release_all (void ) +{ +struct test_firmware_upload *tst, *tmp; + +list_for_each_entry_safe (tst, tmp, &test_upload_list, node) { +list_del (&tst->node); +upload_release (tst); + } + test_fw_config->upload_name =NULL ; +} + +static enum fw_upload_err test_fw_upload_prepare (struct fw_upload *fwl, +const u8 *data,u32 size) +{ +struct test_firmware_upload *tst = fwl->dd_handle; + + tst->cancel_request = false; + +if (!size || size >TEST_UPLOAD_MAX_SIZE ) +return FW_UPLOAD_ERR_INVALID_SIZE ; + +memset (tst->buf,0 ,TEST_UPLOAD_MAX_SIZE ); + tst->size = size; + +return FW_UPLOAD_ERR_NONE ; +} + +static enum fw_upload_err test_fw_upload_write (struct fw_upload *fwl, +const u8 *data,u32 offset, +u32 size,u32 *written) +{ +struct test_firmware_upload *tst = fwl->dd_handle; +u32 blk_size; + +if (tst->cancel_request) +return FW_UPLOAD_ERR_CANCELED ; + + blk_size =min_t (u32,TEST_UPLOAD_BLK_SIZE , size); +memcpy (tst->buf + offset, data + offset, blk_size); + + *written = blk_size; +return FW_UPLOAD_ERR_NONE ; +} + +static enum fw_upload_err test_fw_upload_complete (struct fw_upload *fwl) +{ +struct test_firmware_upload *tst = fwl->dd_handle; + +if (tst->cancel_request) +return FW_UPLOAD_ERR_CANCELED ; + +return FW_UPLOAD_ERR_NONE ; +} + +static void test_fw_upload_cancel (struct fw_upload *fwl) +{ +struct test_firmware_upload *tst = fwl->dd_handle; + + tst->cancel_request = true; +} + +static const struct fw_upload_ops upload_test_ops = { + .prepare = test_fw_upload_prepare, + .write = test_fw_upload_write, + .poll_complete = test_fw_upload_complete, + .cancel = test_fw_upload_cancel, +}; + +static ssize_t upload_register_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count) +{ +struct test_firmware_upload *tst; +struct fw_upload *fwl; +char *name; +int ret; + + name =kstrndup (buf, count,GFP_KERNEL ); +if (!name) +return -ENOMEM ; + +mutex_lock (&test_fw_mutex); + tst =upload_lookup_name (name); +if (tst) { + ret = -EEXIST ; + goto free_name; + } + + tst =kzalloc (sizeof (*tst),GFP_KERNEL ); +if (!tst) { + ret = -ENOMEM ; + goto free_name; + } + + tst->name = name; + tst->buf =kzalloc (TEST_UPLOAD_MAX_SIZE ,GFP_KERNEL ); +if (!tst->buf) { + ret = -ENOMEM ; + goto free_tst; + } + + fwl =firmware_upload_register (THIS_MODULE , dev, tst->name, + &upload_test_ops, tst); +if (IS_ERR (fwl)) { + ret =PTR_ERR (fwl); + goto free_buf; + } + + tst->fwl = fwl; +list_add_tail (&tst->node, &test_upload_list); +mutex_unlock (&test_fw_mutex); +return count; + +free_buf: +kfree (tst->buf); + +free_tst: +kfree (tst); + +free_name: +mutex_unlock (&test_fw_mutex); +kfree (name); + +return ret; +} +static DEVICE_ATTR_WO (upload_register); + +static ssize_t upload_unregister_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count) +{ +struct test_firmware_upload *tst; +int ret = count; + +mutex_lock (&test_fw_mutex); + tst =upload_lookup_name (buf); +if (!tst) { + ret = -EINVAL ; + goto out; + } + +if (test_fw_config->upload_name == tst->name) + test_fw_config->upload_name =NULL ; + +list_del (&tst->node); +upload_release (tst); + +out: +mutex_unlock (&test_fw_mutex); +return ret; +} +static DEVICE_ATTR_WO (upload_unregister); +static ssize_t test_result_show (struct device *dev,struct device_attribute *attr,char *buf)
@@ -1051,6 +1271,42 @@ static ssize_t read_firmware_show(struct device *dev, }static DEVICE_ATTR_RO (read_firmware); +static ssize_t upload_read_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +struct test_firmware_upload *tst; +int ret = -EINVAL ; + +if (!test_fw_config->upload_name) { +pr_err ("Set config_upload_name before using upload_read\n" ); +return -EINVAL ; + } + +mutex_lock (&test_fw_mutex); +list_for_each_entry (tst, &test_upload_list, node) +if (tst->name == test_fw_config->upload_name) +break ; + +if (tst->name != test_fw_config->upload_name) { +pr_err ("Firmware name not found: %s\n" , + test_fw_config->upload_name); + goto out; + } + +if (tst->size >PAGE_SIZE ) { +pr_err ("Testing interface must use PAGE_SIZE firmware for now\n" ); + goto out; + } + +memcpy (buf, tst->buf, tst->size); + ret = tst->size; +out: +mutex_unlock (&test_fw_mutex); +return ret; +} +static DEVICE_ATTR_RO (upload_read); +#define TEST_FW_DEV_ATTR (name) &dev_attr_##name.attrstatic struct attribute *test_dev_attrs[] = {
@@ -1066,6 +1322,7 @@ static struct attribute *test_dev_attrs[] = {TEST_FW_DEV_ATTR (config_sync_direct),TEST_FW_DEV_ATTR (config_send_uevent),TEST_FW_DEV_ATTR (config_read_fw_idx), +TEST_FW_DEV_ATTR (config_upload_name),/* These don't use the config at all - they could be ported! */ TEST_FW_DEV_ATTR(trigger_request),
@@ -1082,6 +1339,9 @@ static struct attribute *test_dev_attrs[] = {TEST_FW_DEV_ATTR (release_all_firmware),TEST_FW_DEV_ATTR (test_result),TEST_FW_DEV_ATTR (read_firmware), +TEST_FW_DEV_ATTR (upload_read), +TEST_FW_DEV_ATTR (upload_register), +TEST_FW_DEV_ATTR (upload_unregister), NULL, };
@@ -1128,6 +1388,7 @@ static void __exit test_firmware_exit(void)mutex_lock (&test_fw_mutex);release_firmware (test_firmware);misc_deregister (&test_fw_misc_device); +upload_release_all ();__test_firmware_config_free ();kfree (test_fw_config);mutex_unlock (&test_fw_mutex);
Signature · 1 line
2.25.1
PATCH RESEND 7/8 test_firmware: Error injection for firmware upload
In reply to Russ WeightAdd error injection capability to the test_firmware module specifically for firmware upload testing. Error injection instructions are transferred as the first part of the firmware payload. The format of an error injection string is similar to the error strings that may be read from the error sysfs node.
To inject the error "programming:hw-error", one would use the error injection string "inject:programming:hw-error" as the firmware payload:
$ echo 1 > loading $ echo inject:programming:hw-error > data $ echo 0 > loading $ cat status idle $ cat error programming:hw-error
The first part of the error string is the progress state of the upload at the time of the error. The progress state would be one of the following:
"preparing", "transferring", or "programming". The second part of the error string is one of the following: "hw-error", "timeout", "device-busy", "invalid-file-size", "read-write-error", "flash-wearout", and "user-abort".
Note that all of the error strings except "user-abort" will fail without delay. The "user-abort" error will cause the firmware upload to stall at the requested progress state for up to 5 minutes to allow you to echo 1 to the cancel sysfs node. It is this cancellation that causes the 'user-abort" error. If the upload is not cancelled within the 5 minute time period, then the upload will complete without an error.
--- lib/test_firmware.c | 127 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 122 insertions(+), 5 deletions(-)
lib/test_firmware.c +122 -5
@@ -117,12 +117,18 @@ struct test_config { struct device *device); }; +struct upload_inject_err { +const char *prog; +enum fw_upload_err err_code; +}; +struct test_firmware_upload {char *name;struct list_head node;char *buf;size_t size;bool cancel_request; +struct upload_inject_err inject;struct fw_upload *fwl; };
@@ -1067,20 +1073,105 @@ static void upload_release_all(void) test_fw_config->upload_name =NULL ; } +/* +* This table is replicated from .../firmware_loader/sysfs_upload.c +* and needs to be kept in sync. +*/ +static const char *const fw_upload_err_str[] = { + [FW_UPLOAD_ERR_NONE ] ="none" , + [FW_UPLOAD_ERR_HW_ERROR ] ="hw-error" , + [FW_UPLOAD_ERR_TIMEOUT ] ="timeout" , + [FW_UPLOAD_ERR_CANCELED ] ="user-abort" , + [FW_UPLOAD_ERR_BUSY ] ="device-busy" , + [FW_UPLOAD_ERR_INVALID_SIZE ] ="invalid-file-size" , + [FW_UPLOAD_ERR_RW_ERROR ] ="read-write-error" , + [FW_UPLOAD_ERR_WEAROUT ] ="flash-wearout" , +}; + +static void upload_err_inject_error (struct test_firmware_upload *tst, +const u8 *p,const char *prog) +{ +enum fw_upload_err err; + +for (err =FW_UPLOAD_ERR_NONE +1 ; err <FW_UPLOAD_ERR_MAX ; err++) { +if (strncmp (p, fw_upload_err_str[err], +strlen (fw_upload_err_str[err])) ==0 ) { + tst->inject.prog = prog; + tst->inject.err_code = err; +return ; + } + } +} + +static void upload_err_inject_prog (struct test_firmware_upload *tst, +const u8 *p) +{ +static const char *const progs[] = { +"preparing:" ,"transferring:" ,"programming:" + }; +int i; + +for (i =0 ; i <ARRAY_SIZE (progs); i++) { +if (strncmp (p, progs[i],strlen (progs[i])) ==0 ) { +upload_err_inject_error (tst, p +strlen (progs[i]), + progs[i]); +return ; + } + } +} + +#define FIVE_MINUTES_MS (5 * 60 * 1000) +static enum fw_upload_err +fw_upload_wait_on_cancel (struct test_firmware_upload *tst) +{ +int ms_delay; + +for (ms_delay =0 ; ms_delay <FIVE_MINUTES_MS ; ms_delay +=100 ) { +msleep (100 ); +if (tst->cancel_request) +return FW_UPLOAD_ERR_CANCELED ; + } +return FW_UPLOAD_ERR_NONE ; +} +static enum fw_upload_err test_fw_upload_prepare (struct fw_upload *fwl,const u8 *data,u32 size) {struct test_firmware_upload *tst = fwl->dd_handle; +enum fw_upload_err ret =FW_UPLOAD_ERR_NONE ; +const char *progress ="preparing:" ; tst->cancel_request = false; -if (!size || size >TEST_UPLOAD_MAX_SIZE ) -return FW_UPLOAD_ERR_INVALID_SIZE ; +if (!size || size >TEST_UPLOAD_MAX_SIZE ) { + ret =FW_UPLOAD_ERR_INVALID_SIZE ; + goto err_out; + } + +if (strncmp (data,"inject:" ,strlen ("inject:" )) ==0 ) +upload_err_inject_prog (tst, data +strlen ("inject:" ));memset (tst->buf,0 ,TEST_UPLOAD_MAX_SIZE ); tst->size = size; -return FW_UPLOAD_ERR_NONE ; +if (tst->inject.err_code ==FW_UPLOAD_ERR_NONE || +strncmp (tst->inject.prog, progress,strlen (progress)) !=0 ) +return FW_UPLOAD_ERR_NONE ; + +if (tst->inject.err_code ==FW_UPLOAD_ERR_CANCELED ) + ret =fw_upload_wait_on_cancel (tst); +else + ret = tst->inject.err_code; + +err_out: +/* +* The cleanup op only executes if the prepare op succeeds. +* If the prepare op fails, it must do it's own clean-up. +*/ + tst->inject.err_code =FW_UPLOAD_ERR_NONE ; + tst->inject.prog =NULL ; + +return ret; } static enum fw_upload_err test_fw_upload_write(struct fw_upload *fwl,
@@ -1088,6 +1179,7 @@ static enum fw_upload_err test_fw_upload_write(struct fw_upload *fwl, u32 size, u32 *written) { struct test_firmware_upload *tst = fwl->dd_handle; +const char *progress ="transferring:" ;u32 blk_size;if (tst->cancel_request)
@@ -1097,17 +1189,33 @@ static enum fw_upload_err test_fw_upload_write(struct fw_upload *fwl,memcpy (tst->buf + offset, data + offset, blk_size); *written = blk_size; -return FW_UPLOAD_ERR_NONE ; + +if (tst->inject.err_code ==FW_UPLOAD_ERR_NONE || +strncmp (tst->inject.prog, progress,strlen (progress)) !=0 ) +return FW_UPLOAD_ERR_NONE ; + +if (tst->inject.err_code ==FW_UPLOAD_ERR_CANCELED ) +return fw_upload_wait_on_cancel (tst); + +return tst->inject.err_code; }static enum fw_upload_err test_fw_upload_complete (struct fw_upload *fwl) {struct test_firmware_upload *tst = fwl->dd_handle; +const char *progress ="programming:" ;if (tst->cancel_request)return FW_UPLOAD_ERR_CANCELED ; -return FW_UPLOAD_ERR_NONE ; +if (tst->inject.err_code ==FW_UPLOAD_ERR_NONE || +strncmp (tst->inject.prog, progress,strlen (progress)) !=0 ) +return FW_UPLOAD_ERR_NONE ; + +if (tst->inject.err_code ==FW_UPLOAD_ERR_CANCELED ) +return fw_upload_wait_on_cancel (tst); + +return tst->inject.err_code; }static void test_fw_upload_cancel (struct fw_upload *fwl)
@@ -1117,11 +1225,20 @@ static void test_fw_upload_cancel(struct fw_upload *fwl) tst->cancel_request = true; } +static void test_fw_cleanup (struct fw_upload *fwl) +{ +struct test_firmware_upload *tst = fwl->dd_handle; + + tst->inject.err_code =FW_UPLOAD_ERR_NONE ; + tst->inject.prog =NULL ; +} +static const struct fw_upload_ops upload_test_ops = { .prepare = test_fw_upload_prepare, .write = test_fw_upload_write, .poll_complete = test_fw_upload_complete, .cancel = test_fw_upload_cancel, + .cleanup = test_fw_cleanup }; static ssize_t upload_register_store(struct device *dev,
Signature · 1 line
2.25.1
PATCH RESEND 8/8 selftests: firmware: Add firmware upload selftests
In reply to Russ WeightAdd selftests to verify the firmware upload mechanism. These test include simple firmware uploads as well as upload cancellation and error injection. The test creates three firmware devices and verifies that they all work correctly and independently.
--- tools/testing/selftests/firmware/Makefile | 2 +- tools/testing/selftests/firmware/config | 1 + tools/testing/selftests/firmware/fw_lib.sh | 7 + .../selftests/firmware/fw_run_tests.sh | 4 + tools/testing/selftests/firmware/fw_upload.sh | 214 ++++++++++++++++++ 5 files changed, 227 insertions(+), 1 deletion(-) create mode 100755 tools/testing/selftests/firmware/fw_upload.sh
tools/testing/selftests/firmware/Makefile +1 -1
@@ -4,7 +4,7 @@ CFLAGS = -Wall \ -O2 TEST_PROGS := fw_run_tests.sh -TEST_FILES := fw_fallback.sh fw_filesystem.sh fw_lib.sh +TEST_FILES := fw_fallback.sh fw_filesystem.sh fw_upload.sh fw_lib.sh TEST_GEN_FILES := fw_namespace include ../lib.mk
tools/testing/selftests/firmware/config +1 -0
@@ -3,3 +3,4 @@ CONFIG_FW_LOADER=y CONFIG_FW_LOADER_USER_HELPER=y CONFIG_IKCONFIG=y CONFIG_IKCONFIG_PROC=y +CONFIG_FW_UPLOAD=y
tools/testing/selftests/firmware/fw_lib.sh +7 -0
@@ -63,6 +63,7 @@ check_setup() HAS_FW_LOADER_USER_HELPER="$(kconfig_has CONFIG_FW_LOADER_USER_HELPER=y)" HAS_FW_LOADER_USER_HELPER_FALLBACK="$(kconfig_has CONFIG_FW_LOADER_USER_HELPER_FALLBACK=y)" HAS_FW_LOADER_COMPRESS="$(kconfig_has CONFIG_FW_LOADER_COMPRESS=y)" + HAS_FW_UPLOAD="$(kconfig_has CONFIG_FW_UPLOAD=y)" PROC_FW_IGNORE_SYSFS_FALLBACK="0" PROC_FW_FORCE_SYSFS_FALLBACK="0"
@@ -113,6 +114,12 @@ verify_reqs() exit 0 fi fi + if [ "$TEST_REQS_FW_UPLOAD" = "yes" ]; then + if [ ! "$HAS_FW_UPLOAD" = "yes" ]; then + echo "firmware upload disabled so ignoring test" + exit 0 + fi + fi } setup_tmp_file()
tools/testing/selftests/firmware/fw_run_tests.sh +4 -0
@@ -22,6 +22,10 @@ run_tests() proc_set_force_sysfs_fallback $1 proc_set_ignore_sysfs_fallback $2 $TEST_DIR/fw_fallback.sh + + proc_set_force_sysfs_fallback $1 + proc_set_ignore_sysfs_fallback $2 + $TEST_DIR/fw_upload.sh } run_test_config_0001()
tools/testing/selftests/firmware/fw_upload.sh new file +214 -0
214 lines not on this page · raw message
Signature · 1 line
2.25.1