Resguardo inicial

This commit is contained in:
lastdragon
2026-09-06 12:53:21 -06:00
parent 662efe3c2a
commit ad2088d178
94853 changed files with 43200404 additions and 0 deletions
@@ -0,0 +1,239 @@
# SPDX-License-Identifier: GPL-2.0
menu "Firmware loader"
config FW_LOADER
tristate "Firmware loading facility" if EXPERT
select CRYPTO_LIB_SHA256 if FW_LOADER_DEBUG
default y
help
This enables the firmware loading facility in the kernel. The kernel
will first look for built-in firmware, if it has any. Next, it will
look for the requested firmware in a series of filesystem paths:
o firmware_class path module parameter or kernel boot param
o /lib/firmware/updates/UTS_RELEASE
o /lib/firmware/updates
o /lib/firmware/UTS_RELEASE
o /lib/firmware
Enabling this feature only increases your kernel image by about
828 bytes, enable this option unless you are certain you don't
need firmware.
You typically want this built-in (=y) but you can also enable this
as a module, in which case the firmware_class module will be built.
You also want to be sure to enable this built-in if you are going to
enable built-in firmware (CONFIG_EXTRA_FIRMWARE).
config FW_LOADER_DEBUG
bool "Log filenames and checksums for loaded firmware"
depends on DYNAMIC_DEBUG
depends on FW_LOADER
default FW_LOADER
help
Select this option to use dynamic debug to log firmware filenames and
SHA256 checksums to the kernel log for each firmware file that is
loaded.
config RUST_FW_LOADER_ABSTRACTIONS
bool "Rust Firmware Loader abstractions"
depends on RUST
select FW_LOADER
help
This enables the Rust abstractions for the firmware loader API.
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
Device drivers which require firmware can typically deal with
having the kernel load firmware from the various supported
/lib/firmware/ paths. This option enables you to build into the
kernel firmware files. Built-in firmware searches are preceded
over firmware lookups using your filesystem over the supported
/lib/firmware paths documented on CONFIG_FW_LOADER.
This may be useful for testing or if the firmware is required early on
in boot and cannot rely on the firmware being placed in an initrd or
initramfs.
This option is a string and takes the (space-separated) names of the
firmware files -- the same names that appear in MODULE_FIRMWARE()
and request_firmware() in the source. These files should exist under
the directory specified by the EXTRA_FIRMWARE_DIR option, which is
/lib/firmware by default.
For example, you might set CONFIG_EXTRA_FIRMWARE="usb8388.bin", copy
the usb8388.bin file into /lib/firmware, and build the kernel. Then
any request_firmware("usb8388.bin") will be satisfied internally
inside the kernel without ever looking at your filesystem at runtime.
WARNING: If you include additional firmware files into your binary
kernel image that are not available under the terms of the GPL,
then it may be a violation of the GPL to distribute the resulting
image since it combines both GPL and non-GPL work. You should
consult a lawyer of your own before distributing such an image.
NOTE: Compressed files are not supported in EXTRA_FIRMWARE.
config EXTRA_FIRMWARE_DIR
string "Firmware blobs root directory"
depends on EXTRA_FIRMWARE != ""
default "/lib/firmware"
help
This option controls the directory in which the kernel build system
looks for the firmware files listed in the EXTRA_FIRMWARE option.
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
loading to the kernel through userspace as a fallback mechanism
if and only if the kernel's direct filesystem lookup for the
firmware failed using the different /lib/firmware/ paths, or the
path specified in the firmware_class path module parameter, or the
firmware_class path kernel boot parameter if the firmware_class is
built-in. For details on how to work with the sysfs fallback mechanism
refer to Documentation/driver-api/firmware/fallback-mechanisms.rst.
The direct filesystem lookup for firmware is always used first now.
If the kernel's direct filesystem lookup for firmware fails to find
the requested firmware a sysfs fallback loading facility is made
available and userspace is informed about this through uevents.
The uevent can be suppressed if the driver explicitly requested it,
this is known as the driver using the custom fallback mechanism.
If the custom fallback mechanism is used userspace must always
acknowledge failure to find firmware as the timeout for the fallback
mechanism is disabled, and failed requests will linger forever.
This used to be the default firmware loading facility, and udev used
to listen for uvents to load firmware for the kernel. The firmware
loading facility functionality in udev has been removed, as such it
can no longer be relied upon as a fallback mechanism. Linux no longer
relies on or uses a fallback mechanism in userspace. If you need to
rely on one refer to the permissively licensed firmwared:
https://github.com/teg/firmwared
Since this was the default firmware loading facility at one point,
old userspace may exist which relies upon it, and as such this
mechanism can never be removed from the kernel.
You should only enable this functionality if you are certain you
require a fallback mechanism and have a userspace mechanism ready to
load firmware in case it is not found. One main reason for this may
be if you have drivers which require firmware built-in and for
whatever reason cannot place the required firmware in initramfs.
Another reason kernels may have this feature enabled is to support a
driver which explicitly relies on this fallback mechanism. Only two
drivers need this today:
o CONFIG_LEDS_LP55XX_COMMON
o CONFIG_DELL_RBU
Outside of supporting the above drivers, another reason for needing
this may be that your firmware resides outside of the paths the kernel
looks for and cannot possibly be specified using the firmware_class
path module parameter or kernel firmware_class path boot parameter
if firmware_class is built-in.
A modern use case may be to temporarily mount a custom partition
during provisioning which is only accessible to userspace, and then
to use it to look for and fetch the required firmware. Such type of
driver functionality may not even ever be desirable upstream by
vendors, and as such is only required to be supported as an interface
for provisioning. Since udev's firmware loading facility has been
removed you can use firmwared or a fork of it to customize how you
want to load firmware based on uevents issued.
Enabling this option will increase your kernel image size by about
13436 bytes.
If you are unsure about this, say N here, unless you are Linux
distribution and need to support the above two drivers, or you are
certain you need to support some really custom firmware loading
facility in userspace.
config FW_LOADER_USER_HELPER_FALLBACK
bool "Force the firmware sysfs fallback mechanism when possible"
depends on FW_LOADER_USER_HELPER
help
Enabling this option forces a sysfs userspace fallback mechanism
to be used for all firmware requests which explicitly do not disable a
a fallback mechanism. Firmware calls which do prohibit a fallback
mechanism is request_firmware_direct(). This option is kept for
backward compatibility purposes given this precise mechanism can also
be enabled by setting the proc sysctl value to true:
/proc/sys/kernel/firmware_config/force_sysfs_fallback
If you are unsure about this, say N here.
config FW_LOADER_COMPRESS
bool "Enable compressed firmware support"
help
This option enables the support for loading compressed firmware
files. The caller of firmware API receives the decompressed file
content. The compressed file is loaded as a fallback, only after
loading the raw file failed at first.
Compressed firmware support does not apply to firmware images
that are built into the kernel image (CONFIG_EXTRA_FIRMWARE).
if FW_LOADER_COMPRESS
config FW_LOADER_COMPRESS_XZ
bool "Enable XZ-compressed firmware support"
select FW_LOADER_PAGED_BUF
select XZ_DEC
default y
help
This option adds the support for XZ-compressed files.
The files have to be compressed with either none or crc32
integrity check type (pass "-C crc32" option to xz command).
config FW_LOADER_COMPRESS_ZSTD
bool "Enable ZSTD-compressed firmware support"
select ZSTD_DECOMPRESS
help
This option adds the support for ZSTD-compressed files.
endif # FW_LOADER_COMPRESS
config FW_CACHE
bool "Enable firmware caching during suspend"
depends on PM_SLEEP
default y if PM_SLEEP
help
Because firmware caching generates uevent messages that are sent
over a netlink socket, it can prevent suspend on many platforms.
It is also not always useful, so on such platforms we have the
option.
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
@@ -0,0 +1,12 @@
# SPDX-License-Identifier: GPL-2.0
# Makefile for the Linux firmware loader
obj-$(CONFIG_FW_LOADER_USER_HELPER) += fallback_table.o
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
firmware_class-$(CONFIG_FW_UPLOAD) += sysfs_upload.o
obj-y += builtin/
@@ -0,0 +1,2 @@
# SPDX-License-Identifier: GPL-2.0
*.gen.S
@@ -0,0 +1,42 @@
# SPDX-License-Identifier: GPL-2.0
obj-y += main.o
# Create $(fwdir) from $(CONFIG_EXTRA_FIRMWARE_DIR) -- if it doesn't have a
# leading /, it's relative to $(srctree).
fwdir := $(CONFIG_EXTRA_FIRMWARE_DIR)
fwdir := $(addprefix $(srctree)/,$(filter-out /%,$(fwdir)))$(filter /%,$(fwdir))
firmware := $(addsuffix .gen.o, $(CONFIG_EXTRA_FIRMWARE))
obj-y += $(firmware)
FWNAME = $(patsubst $(obj)/%.gen.S,%,$@)
FWSTR = $(subst $(comma),_,$(subst /,_,$(subst .,_,$(subst -,_,$(FWNAME)))))
ASM_WORD = $(if $(CONFIG_64BIT),.quad,.long)
ASM_ALIGN = $(if $(CONFIG_64BIT),3,2)
PROGBITS = $(if $(CONFIG_ARM),%,@)progbits
filechk_fwbin = \
echo "/* Generated by $(src)/Makefile */" ;\
echo " .section .rodata" ;\
echo " .p2align 4" ;\
echo "_fw_$(FWSTR)_bin:" ;\
echo " .incbin \"$(fwdir)/$(FWNAME)\"" ;\
echo "_fw_end:" ;\
echo " .section .rodata.str,\"aMS\",$(PROGBITS),1" ;\
echo " .p2align $(ASM_ALIGN)" ;\
echo "_fw_$(FWSTR)_name:" ;\
echo " .string \"$(FWNAME)\"" ;\
echo " .section .builtin_fw,\"a\",$(PROGBITS)" ;\
echo " .p2align $(ASM_ALIGN)" ;\
echo " $(ASM_WORD) _fw_$(FWSTR)_name" ;\
echo " $(ASM_WORD) _fw_$(FWSTR)_bin" ;\
echo " $(ASM_WORD) _fw_end - _fw_$(FWSTR)_bin"
$(obj)/%.gen.S: FORCE
$(call filechk,fwbin)
# The .o files depend on the binaries directly; the .S files don't.
$(addprefix $(obj)/, $(firmware)): $(obj)/%.gen.o: $(fwdir)/%
targets := $(patsubst $(obj)/%,%, \
$(shell find $(obj) -name \*.gen.S 2>/dev/null))
@@ -0,0 +1,106 @@
// SPDX-License-Identifier: GPL-2.0
/* Builtin firmware support */
#include <linux/firmware.h>
#include "../firmware.h"
/* Only if FW_LOADER=y */
#ifdef CONFIG_FW_LOADER
struct builtin_fw {
char *name;
void *data;
unsigned long size;
};
extern struct builtin_fw __start_builtin_fw[];
extern struct builtin_fw __end_builtin_fw[];
static bool fw_copy_to_prealloc_buf(struct firmware *fw,
void *buf, size_t size)
{
if (!buf)
return true;
if (size < fw->size)
return false;
memcpy(buf, fw->data, fw->size);
return true;
}
/**
* firmware_request_builtin() - load builtin firmware
* @fw: pointer to firmware struct
* @name: name of firmware file
*
* Some use cases in the kernel have a requirement so that no memory allocator
* is involved as these calls take place early in boot process. An example is
* the x86 CPU microcode loader. In these cases all the caller wants is to see
* if the firmware was built-in and if so use it right away. This can be used
* for such cases.
*
* This looks for the firmware in the built-in kernel. Only if the kernel was
* built-in with the firmware you are looking for will this return successfully.
*
* Callers of this API do not need to use release_firmware() as the pointer to
* the firmware is expected to be provided locally on the stack of the caller.
**/
bool firmware_request_builtin(struct firmware *fw, const char *name)
{
struct builtin_fw *b_fw;
if (!fw)
return false;
for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
if (strcmp(name, b_fw->name) == 0) {
fw->size = b_fw->size;
fw->data = b_fw->data;
return true;
}
}
return false;
}
EXPORT_SYMBOL_NS_GPL(firmware_request_builtin, "TEST_FIRMWARE");
/**
* firmware_request_builtin_buf() - load builtin firmware into optional buffer
* @fw: pointer to firmware struct
* @name: name of firmware file
* @buf: If set this lets you use a pre-allocated buffer so that the built-in
* firmware into is copied into. This field can be NULL. It is used by
* callers such as request_firmware_into_buf() and
* request_partial_firmware_into_buf()
* @size: if buf was provided, the max size of the allocated buffer available.
* If the built-in firmware does not fit into the pre-allocated @buf this
* call will fail.
*
* This looks for the firmware in the built-in kernel. Only if the kernel was
* built-in with the firmware you are looking for will this call possibly
* succeed. If you passed a @buf the firmware will be copied into it *iff* the
* built-in firmware fits into the pre-allocated buffer size specified in
* @size.
*
* This caller is to be used internally by the firmware_loader only.
**/
bool firmware_request_builtin_buf(struct firmware *fw, const char *name,
void *buf, size_t size)
{
if (!firmware_request_builtin(fw, name))
return false;
return fw_copy_to_prealloc_buf(fw, buf, size);
}
bool firmware_is_builtin(const struct firmware *fw)
{
struct builtin_fw *b_fw;
for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
if (fw->data == b_fw->data)
return true;
return false;
}
#endif
@@ -0,0 +1,239 @@
// SPDX-License-Identifier: GPL-2.0
#include <linux/types.h>
#include <linux/kconfig.h>
#include <linux/list.h>
#include <linux/security.h>
#include <linux/umh.h>
#include <linux/sysctl.h>
#include <linux/module.h>
#include "fallback.h"
#include "firmware.h"
/*
* firmware fallback mechanism
*/
/*
* use small loading timeout for caching devices' firmware because all these
* firmware images have been loaded successfully at lease once, also system is
* ready for completing firmware loading now. The maximum size of firmware in
* current distributions is about 2M bytes, so 10 secs should be enough.
*/
void fw_fallback_set_cache_timeout(void)
{
fw_fallback_config.old_timeout = __firmware_loading_timeout();
__fw_fallback_set_timeout(10);
}
/* Restores the timeout to the value last configured during normal operation */
void fw_fallback_set_default_timeout(void)
{
__fw_fallback_set_timeout(fw_fallback_config.old_timeout);
}
static long firmware_loading_timeout(void)
{
return __firmware_loading_timeout() > 0 ?
__firmware_loading_timeout() * HZ : MAX_JIFFY_OFFSET;
}
static inline int fw_sysfs_wait_timeout(struct fw_priv *fw_priv, long timeout)
{
return __fw_state_wait_common(fw_priv, timeout);
}
static LIST_HEAD(pending_fw_head);
void kill_pending_fw_fallback_reqs(bool kill_all)
{
struct fw_priv *fw_priv;
struct fw_priv *next;
mutex_lock(&fw_lock);
list_for_each_entry_safe(fw_priv, next, &pending_fw_head,
pending_list) {
if (kill_all || !fw_priv->need_uevent)
__fw_load_abort(fw_priv);
}
if (kill_all)
fw_load_abort_all = true;
mutex_unlock(&fw_lock);
}
/**
* fw_load_sysfs_fallback() - load a firmware via the sysfs fallback mechanism
* @fw_sysfs: firmware sysfs information for the firmware to load
* @timeout: timeout to wait for the load
*
* In charge of constructing a sysfs fallback interface for firmware loading.
**/
static int fw_load_sysfs_fallback(struct fw_sysfs *fw_sysfs, long timeout)
{
int retval = 0;
struct device *f_dev = &fw_sysfs->dev;
struct fw_priv *fw_priv = fw_sysfs->fw_priv;
/* fall back on userspace loading */
if (!fw_priv->data)
fw_priv->is_paged_buf = true;
dev_set_uevent_suppress(f_dev, true);
retval = device_add(f_dev);
if (retval) {
dev_err(f_dev, "%s: device_register failed\n", __func__);
goto err_put_dev;
}
mutex_lock(&fw_lock);
if (fw_load_abort_all || fw_state_is_aborted(fw_priv)) {
mutex_unlock(&fw_lock);
retval = -EINTR;
goto out;
}
list_add(&fw_priv->pending_list, &pending_fw_head);
mutex_unlock(&fw_lock);
if (fw_priv->opt_flags & FW_OPT_UEVENT) {
fw_priv->need_uevent = true;
dev_set_uevent_suppress(f_dev, false);
dev_dbg(f_dev, "firmware: requesting %s\n", fw_priv->fw_name);
kobject_uevent(&fw_sysfs->dev.kobj, KOBJ_ADD);
} else {
timeout = MAX_JIFFY_OFFSET;
}
retval = fw_sysfs_wait_timeout(fw_priv, timeout);
if (retval < 0 && retval != -ENOENT) {
mutex_lock(&fw_lock);
fw_load_abort(fw_sysfs);
mutex_unlock(&fw_lock);
}
if (fw_state_is_aborted(fw_priv)) {
if (retval == -ERESTARTSYS)
retval = -EINTR;
} else if (fw_priv->is_paged_buf && !fw_priv->data)
retval = -ENOMEM;
out:
device_del(f_dev);
err_put_dev:
put_device(f_dev);
return retval;
}
static int fw_load_from_user_helper(struct firmware *firmware,
const char *name, struct device *device,
u32 opt_flags)
{
struct fw_sysfs *fw_sysfs;
long timeout;
int ret;
timeout = firmware_loading_timeout();
if (opt_flags & FW_OPT_NOWAIT) {
timeout = usermodehelper_read_lock_wait(timeout);
if (!timeout) {
dev_dbg(device, "firmware: %s loading timed out\n",
name);
return -EBUSY;
}
} else {
ret = usermodehelper_read_trylock();
if (WARN_ON(ret)) {
dev_err(device, "firmware: %s will not be loaded\n",
name);
return ret;
}
}
fw_sysfs = fw_create_instance(firmware, name, device, opt_flags);
if (IS_ERR(fw_sysfs)) {
ret = PTR_ERR(fw_sysfs);
goto out_unlock;
}
fw_sysfs->fw_priv = firmware->priv;
ret = fw_load_sysfs_fallback(fw_sysfs, timeout);
if (!ret)
ret = assign_fw(firmware, device);
out_unlock:
usermodehelper_read_unlock();
return ret;
}
static bool fw_force_sysfs_fallback(u32 opt_flags)
{
if (fw_fallback_config.force_sysfs_fallback)
return true;
if (!(opt_flags & FW_OPT_USERHELPER))
return false;
return true;
}
static bool fw_run_sysfs_fallback(u32 opt_flags)
{
int ret;
if (fw_fallback_config.ignore_sysfs_fallback) {
pr_info_once("Ignoring firmware sysfs fallback due to sysctl knob\n");
return false;
}
if ((opt_flags & FW_OPT_NOFALLBACK_SYSFS))
return false;
/* Also permit LSMs and IMA to fail firmware sysfs fallback */
ret = security_kernel_load_data(LOADING_FIRMWARE, true);
if (ret < 0)
return false;
return fw_force_sysfs_fallback(opt_flags);
}
/**
* firmware_fallback_sysfs() - use the fallback mechanism to find firmware
* @fw: pointer to firmware image
* @name: name of firmware file to look for
* @device: device for which firmware is being loaded
* @opt_flags: options to control firmware loading behaviour, as defined by
* &enum fw_opt
* @ret: return value from direct lookup which triggered the fallback mechanism
*
* This function is called if direct lookup for the firmware failed, it enables
* a fallback mechanism through userspace by exposing a sysfs loading
* interface. Userspace is in charge of loading the firmware through the sysfs
* loading interface. This sysfs fallback mechanism may be disabled completely
* on a system by setting the proc sysctl value ignore_sysfs_fallback to true.
* If this is false we check if the internal API caller set the
* @FW_OPT_NOFALLBACK_SYSFS flag, if so it would also disable the fallback
* mechanism. A system may want to enforce the sysfs fallback mechanism at all
* times, it can do this by setting ignore_sysfs_fallback to false and
* force_sysfs_fallback to true.
* Enabling force_sysfs_fallback is functionally equivalent to build a kernel
* with CONFIG_FW_LOADER_USER_HELPER_FALLBACK.
**/
int firmware_fallback_sysfs(struct firmware *fw, const char *name,
struct device *device,
u32 opt_flags,
int ret)
{
if (!fw_run_sysfs_fallback(opt_flags))
return ret;
if (!(opt_flags & FW_OPT_NO_WARN))
dev_warn(device, "Falling back to sysfs fallback for: %s\n",
name);
else
dev_dbg(device, "Falling back to sysfs fallback for: %s\n",
name);
return fw_load_from_user_helper(fw, name, device, opt_flags);
}
@@ -0,0 +1,45 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef __FIRMWARE_FALLBACK_H
#define __FIRMWARE_FALLBACK_H
#include <linux/firmware.h>
#include <linux/device.h>
#include "firmware.h"
#include "sysfs.h"
#ifdef CONFIG_FW_LOADER_USER_HELPER
int firmware_fallback_sysfs(struct firmware *fw, const char *name,
struct device *device,
u32 opt_flags,
int ret);
void kill_pending_fw_fallback_reqs(bool kill_all);
void fw_fallback_set_cache_timeout(void);
void fw_fallback_set_default_timeout(void);
#else /* CONFIG_FW_LOADER_USER_HELPER */
static inline int firmware_fallback_sysfs(struct firmware *fw, const char *name,
struct device *device,
u32 opt_flags,
int ret)
{
/* Keep carrying over the same error */
return ret;
}
static inline void kill_pending_fw_fallback_reqs(bool kill_all) { }
static inline void fw_fallback_set_cache_timeout(void) { }
static inline void fw_fallback_set_default_timeout(void) { }
#endif /* CONFIG_FW_LOADER_USER_HELPER */
#ifdef CONFIG_EFI_EMBEDDED_FIRMWARE
int firmware_fallback_platform(struct fw_priv *fw_priv);
#else
static inline int firmware_fallback_platform(struct fw_priv *fw_priv)
{
return -ENOENT;
}
#endif
#endif /* __FIRMWARE_FALLBACK_H */
@@ -0,0 +1,45 @@
// SPDX-License-Identifier: GPL-2.0
#include <linux/efi_embedded_fw.h>
#include <linux/property.h>
#include <linux/security.h>
#include <linux/vmalloc.h>
#include "fallback.h"
#include "firmware.h"
int firmware_fallback_platform(struct fw_priv *fw_priv)
{
const u8 *data;
size_t size;
int rc;
if (!(fw_priv->opt_flags & FW_OPT_FALLBACK_PLATFORM))
return -ENOENT;
rc = security_kernel_load_data(LOADING_FIRMWARE, true);
if (rc)
return rc;
rc = efi_get_embedded_fw(fw_priv->fw_name, &data, &size);
if (rc)
return rc; /* rc == -ENOENT when the fw was not found */
if (fw_priv->data && size > fw_priv->allocated_size)
return -ENOMEM;
rc = security_kernel_post_load_data((u8 *)data, size, LOADING_FIRMWARE,
"platform");
if (rc)
return rc;
if (!fw_priv->data)
fw_priv->data = vmalloc(size);
if (!fw_priv->data)
return -ENOMEM;
memcpy(fw_priv->data, data, size);
fw_priv->size = size;
fw_state_done(fw_priv);
return 0;
}
@@ -0,0 +1,68 @@
// SPDX-License-Identifier: GPL-2.0
#include <linux/types.h>
#include <linux/kconfig.h>
#include <linux/list.h>
#include <linux/slab.h>
#include <linux/export.h>
#include <linux/security.h>
#include <linux/highmem.h>
#include <linux/umh.h>
#include <linux/sysctl.h>
#include "fallback.h"
#include "firmware.h"
/*
* firmware fallback configuration table
*/
struct firmware_fallback_config fw_fallback_config = {
.force_sysfs_fallback = IS_ENABLED(CONFIG_FW_LOADER_USER_HELPER_FALLBACK),
.loading_timeout = 60,
.old_timeout = 60,
};
EXPORT_SYMBOL_NS_GPL(fw_fallback_config, "FIRMWARE_LOADER_PRIVATE");
#ifdef CONFIG_SYSCTL
static const struct ctl_table firmware_config_table[] = {
{
.procname = "force_sysfs_fallback",
.data = &fw_fallback_config.force_sysfs_fallback,
.maxlen = sizeof(unsigned int),
.mode = 0644,
.proc_handler = proc_douintvec_minmax,
.extra1 = SYSCTL_ZERO,
.extra2 = SYSCTL_ONE,
},
{
.procname = "ignore_sysfs_fallback",
.data = &fw_fallback_config.ignore_sysfs_fallback,
.maxlen = sizeof(unsigned int),
.mode = 0644,
.proc_handler = proc_douintvec_minmax,
.extra1 = SYSCTL_ZERO,
.extra2 = SYSCTL_ONE,
},
};
static struct ctl_table_header *firmware_config_sysct_table_header;
int register_firmware_config_sysctl(void)
{
firmware_config_sysct_table_header =
register_sysctl("kernel/firmware_config",
firmware_config_table);
if (!firmware_config_sysct_table_header)
return -ENOMEM;
return 0;
}
EXPORT_SYMBOL_NS_GPL(register_firmware_config_sysctl, "FIRMWARE_LOADER_PRIVATE");
void unregister_firmware_config_sysctl(void)
{
unregister_sysctl_table(firmware_config_sysct_table_header);
firmware_config_sysct_table_header = NULL;
}
EXPORT_SYMBOL_NS_GPL(unregister_firmware_config_sysctl, "FIRMWARE_LOADER_PRIVATE");
#endif /* CONFIG_SYSCTL */
@@ -0,0 +1,198 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef __FIRMWARE_LOADER_H
#define __FIRMWARE_LOADER_H
#include <linux/bitops.h>
#include <linux/firmware.h>
#include <linux/types.h>
#include <linux/kref.h>
#include <linux/list.h>
#include <linux/completion.h>
/**
* enum fw_opt - options to control firmware loading behaviour
*
* @FW_OPT_UEVENT: Enables the fallback mechanism to send a kobject uevent
* when the firmware is not found. Userspace is in charge to load the
* firmware using the sysfs loading facility.
* @FW_OPT_NOWAIT: Used to describe the firmware request is asynchronous.
* @FW_OPT_USERHELPER: Enable the fallback mechanism, in case the direct
* filesystem lookup fails at finding the firmware. For details refer to
* firmware_fallback_sysfs().
* @FW_OPT_NO_WARN: Quiet, avoid printing warning messages.
* @FW_OPT_NOCACHE: Disables firmware caching. Firmware caching is used to
* cache the firmware upon suspend, so that upon resume races against the
* firmware file lookup on storage is avoided. Used for calls where the
* file may be too big, or where the driver takes charge of its own
* firmware caching mechanism.
* @FW_OPT_NOFALLBACK_SYSFS: Disable the sysfs fallback mechanism. Takes
* precedence over &FW_OPT_UEVENT and &FW_OPT_USERHELPER.
* @FW_OPT_FALLBACK_PLATFORM: Enable fallback to device fw copy embedded in
* the platform's main firmware. If both this fallback and the sysfs
* fallback are enabled, then this fallback will be tried first.
* @FW_OPT_PARTIAL: Allow partial read of firmware instead of needing to read
* entire file.
*/
enum fw_opt {
FW_OPT_UEVENT = BIT(0),
FW_OPT_NOWAIT = BIT(1),
FW_OPT_USERHELPER = BIT(2),
FW_OPT_NO_WARN = BIT(3),
FW_OPT_NOCACHE = BIT(4),
FW_OPT_NOFALLBACK_SYSFS = BIT(5),
FW_OPT_FALLBACK_PLATFORM = BIT(6),
FW_OPT_PARTIAL = BIT(7),
};
enum fw_status {
FW_STATUS_UNKNOWN,
FW_STATUS_LOADING,
FW_STATUS_DONE,
FW_STATUS_ABORTED,
};
/*
* Concurrent request_firmware() for the same firmware need to be
* serialized. struct fw_state is simple state machine which hold the
* state of the firmware loading.
*/
struct fw_state {
struct completion completion;
enum fw_status status;
};
struct fw_priv {
struct kref ref;
struct list_head list;
struct firmware_cache *fwc;
struct fw_state fw_st;
void *data;
size_t size;
size_t allocated_size;
size_t offset;
u32 opt_flags;
#ifdef CONFIG_FW_LOADER_PAGED_BUF
bool is_paged_buf;
struct page **pages;
int nr_pages;
int page_array_size;
#endif
#ifdef CONFIG_FW_LOADER_USER_HELPER
bool need_uevent;
struct list_head pending_list;
#endif
const char *fw_name;
};
extern struct mutex fw_lock;
extern struct firmware_cache fw_cache;
extern bool fw_load_abort_all;
static inline bool __fw_state_check(struct fw_priv *fw_priv,
enum fw_status status)
{
struct fw_state *fw_st = &fw_priv->fw_st;
return fw_st->status == status;
}
static inline int __fw_state_wait_common(struct fw_priv *fw_priv, long timeout)
{
struct fw_state *fw_st = &fw_priv->fw_st;
long ret;
ret = wait_for_completion_killable_timeout(&fw_st->completion, timeout);
if (ret != 0 && fw_st->status == FW_STATUS_ABORTED)
return -ENOENT;
if (!ret)
return -ETIMEDOUT;
return ret < 0 ? ret : 0;
}
static inline void __fw_state_set(struct fw_priv *fw_priv,
enum fw_status status)
{
struct fw_state *fw_st = &fw_priv->fw_st;
WRITE_ONCE(fw_st->status, status);
if (status == FW_STATUS_DONE || status == FW_STATUS_ABORTED) {
#ifdef CONFIG_FW_LOADER_USER_HELPER
/*
* Doing this here ensures that the fw_priv is deleted from
* the pending list in all abort/done paths.
*/
list_del_init(&fw_priv->pending_list);
#endif
complete_all(&fw_st->completion);
}
}
static inline void fw_state_aborted(struct fw_priv *fw_priv)
{
__fw_state_set(fw_priv, FW_STATUS_ABORTED);
}
static inline bool fw_state_is_aborted(struct fw_priv *fw_priv)
{
return __fw_state_check(fw_priv, FW_STATUS_ABORTED);
}
static inline void fw_state_start(struct fw_priv *fw_priv)
{
__fw_state_set(fw_priv, FW_STATUS_LOADING);
}
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);
}
static inline bool fw_state_is_loading(struct fw_priv *fw_priv)
{
return __fw_state_check(fw_priv, FW_STATUS_LOADING);
}
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);
bool firmware_request_builtin_buf(struct firmware *fw, const char *name,
void *buf, size_t size);
#else /* module case */
static inline bool firmware_is_builtin(const struct firmware *fw)
{
return false;
}
static inline bool firmware_request_builtin_buf(struct firmware *fw,
const char *name,
void *buf, size_t size)
{
return false;
}
#endif
#ifdef CONFIG_FW_LOADER_PAGED_BUF
void fw_free_paged_buf(struct fw_priv *fw_priv);
int fw_grow_paged_buf(struct fw_priv *fw_priv, int pages_needed);
int fw_map_paged_buf(struct fw_priv *fw_priv);
bool fw_is_paged_buf(struct fw_priv *fw_priv);
#else
static inline void fw_free_paged_buf(struct fw_priv *fw_priv) {}
static inline int fw_grow_paged_buf(struct fw_priv *fw_priv, int pages_needed) { return -ENXIO; }
static inline int fw_map_paged_buf(struct fw_priv *fw_priv) { return -ENXIO; }
static inline bool fw_is_paged_buf(struct fw_priv *fw_priv) { return false; }
#endif
#endif /* __FIRMWARE_LOADER_H */
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,425 @@
// 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 "sysfs.h"
/*
* sysfs support for firmware loader
*/
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_state_is_done(fw_priv))
return;
fw_state_aborted(fw_priv);
}
#ifdef CONFIG_FW_LOADER_USER_HELPER
static ssize_t timeout_show(const struct class *class, const 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(const struct class *class, const struct class_attribute *attr,
const char *buf, size_t count)
{
int tmp_loading_timeout;
if (kstrtoint(buf, 10, &tmp_loading_timeout))
return -EINVAL;
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 int do_firmware_uevent(const 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(const struct device *dev, struct kobj_uevent_env *env)
{
const 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 void fw_dev_release(struct device *dev)
{
struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
if (fw_sysfs->fw_upload_priv)
fw_upload_free(fw_sysfs);
kfree(fw_sysfs);
}
static struct class firmware_class = {
.name = "firmware",
#ifdef CONFIG_FW_LOADER_USER_HELPER
.class_groups = firmware_class_groups,
.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_state_is_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;
if (kstrtoint(buf, 10, &loading))
return -EINVAL;
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 */
fw_free_paged_buf(fw_priv);
fw_state_start(fw_priv);
break;
case 0:
if (fw_state_is_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);
/*
* 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;
}
fallthrough;
default:
dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
fallthrough;
case -1:
fw_load_abort(fw_sysfs);
if (fw_sysfs->fw_upload_priv)
fw_state_init(fw_sysfs->fw_priv);
break;
}
out:
mutex_unlock(&fw_lock);
return written;
}
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) {
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);
if (read)
memcpy_from_page(buffer, fw_priv->pages[page_nr],
page_ofs, page_cnt);
else
memcpy_to_page(fw_priv->pages[page_nr], page_ofs,
buffer, page_cnt);
buffer += page_cnt;
offset += page_cnt;
count -= page_cnt;
}
}
static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
const 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_state_is_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,
const 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_state_is_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 const 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,
#ifdef CONFIG_FW_UPLOAD
&dev_attr_cancel.attr,
&dev_attr_status.attr,
&dev_attr_error.attr,
&dev_attr_remaining_size.attr,
#endif
NULL
};
static const struct bin_attribute *const 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,
#ifdef CONFIG_FW_UPLOAD
.is_visible = fw_upload_is_visible,
#endif
};
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_obj(*fw_sysfs);
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;
}
@@ -0,0 +1,118 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef __FIRMWARE_SYSFS_H
#define __FIRMWARE_SYSFS_H
#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
/**
* 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;
};
/* 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;
}
#endif
#ifdef CONFIG_FW_LOADER_SYSFS
int register_sysfs_loader(void);
void unregister_sysfs_loader(void);
#if defined(CONFIG_FW_LOADER_USER_HELPER) && defined(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_FW_LOADER_USER_HELPER && CONFIG_SYSCTL */
#else /* CONFIG_FW_LOADER_SYSFS */
static inline int register_sysfs_loader(void)
{
return 0;
}
static inline void unregister_sysfs_loader(void)
{
}
#endif /* CONFIG_FW_LOADER_SYSFS */
struct fw_sysfs {
bool nowait;
struct device dev;
struct fw_priv *fw_priv;
struct firmware *fw;
void *fw_upload_priv;
};
#define to_fw_sysfs(__dev) container_of_const(__dev, struct fw_sysfs, dev)
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);
#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);
void fw_upload_free(struct fw_sysfs *fw_sysfs);
umode_t fw_upload_is_visible(struct kobject *kobj, struct attribute *attr, int n);
#else
static inline int fw_upload_start(struct fw_sysfs *fw_sysfs)
{
return 0;
}
static inline void fw_upload_free(struct fw_sysfs *fw_sysfs)
{
}
#endif
#endif /* __FIRMWARE_SYSFS_H */
@@ -0,0 +1,408 @@
// SPDX-License-Identifier: GPL-2.0
#include <linux/firmware.h>
#include <linux/module.h>
#include <linux/slab.h>
#include "sysfs_upload.h"
/*
* Support for user-space to initiate a firmware upload to a device.
*/
static const char * const fw_upload_prog_str[] = {
[FW_UPLOAD_PROG_IDLE] = "idle",
[FW_UPLOAD_PROG_RECEIVING] = "receiving",
[FW_UPLOAD_PROG_PREPARING] = "preparing",
[FW_UPLOAD_PROG_TRANSFERRING] = "transferring",
[FW_UPLOAD_PROG_PROGRAMMING] = "programming"
};
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",
[FW_UPLOAD_ERR_FW_INVALID] = "firmware-invalid",
};
static const char *fw_upload_progress(struct device *dev,
enum fw_upload_prog prog)
{
const char *status = "unknown-status";
if (prog < FW_UPLOAD_PROG_MAX)
status = fw_upload_prog_str[prog];
else
dev_err(dev, "Invalid status during secure update: %d\n", prog);
return status;
}
static const char *fw_upload_error(struct device *dev,
enum fw_upload_err err_code)
{
const char *error = "unknown-error";
if (err_code < FW_UPLOAD_ERR_MAX)
error = fw_upload_err_str[err_code];
else
dev_err(dev, "Invalid error code during secure update: %d\n",
err_code);
return error;
}
static ssize_t
status_show(struct device *dev, struct device_attribute *attr, char *buf)
{
struct fw_upload_priv *fwlp = to_fw_sysfs(dev)->fw_upload_priv;
return sysfs_emit(buf, "%s\n", fw_upload_progress(dev, fwlp->progress));
}
DEVICE_ATTR_RO(status);
static ssize_t
error_show(struct device *dev, struct device_attribute *attr, char *buf)
{
struct fw_upload_priv *fwlp = to_fw_sysfs(dev)->fw_upload_priv;
int ret;
mutex_lock(&fwlp->lock);
if (fwlp->progress != FW_UPLOAD_PROG_IDLE)
ret = -EBUSY;
else if (!fwlp->err_code)
ret = 0;
else
ret = sysfs_emit(buf, "%s:%s\n",
fw_upload_progress(dev, fwlp->err_progress),
fw_upload_error(dev, fwlp->err_code));
mutex_unlock(&fwlp->lock);
return ret;
}
DEVICE_ATTR_RO(error);
static ssize_t cancel_store(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
struct fw_upload_priv *fwlp = to_fw_sysfs(dev)->fw_upload_priv;
int ret = count;
bool cancel;
if (kstrtobool(buf, &cancel) || !cancel)
return -EINVAL;
mutex_lock(&fwlp->lock);
if (fwlp->progress == FW_UPLOAD_PROG_IDLE) {
mutex_unlock(&fwlp->lock);
return -ENODEV;
}
fwlp->ops->cancel(fwlp->fw_upload);
mutex_unlock(&fwlp->lock);
return ret;
}
DEVICE_ATTR_WO(cancel);
static ssize_t remaining_size_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct fw_upload_priv *fwlp = to_fw_sysfs(dev)->fw_upload_priv;
return sysfs_emit(buf, "%u\n", fwlp->remaining_size);
}
DEVICE_ATTR_RO(remaining_size);
umode_t
fw_upload_is_visible(struct kobject *kobj, struct attribute *attr, int n)
{
static struct fw_sysfs *fw_sysfs;
fw_sysfs = to_fw_sysfs(kobj_to_dev(kobj));
if (fw_sysfs->fw_upload_priv || attr == &dev_attr_loading.attr)
return attr->mode;
return 0;
}
static void fw_upload_update_progress(struct fw_upload_priv *fwlp,
enum fw_upload_prog new_progress)
{
mutex_lock(&fwlp->lock);
fwlp->progress = new_progress;
mutex_unlock(&fwlp->lock);
}
static void fw_upload_set_error(struct fw_upload_priv *fwlp,
enum fw_upload_err err_code)
{
mutex_lock(&fwlp->lock);
fwlp->err_progress = fwlp->progress;
fwlp->err_code = err_code;
mutex_unlock(&fwlp->lock);
}
static void fw_upload_prog_complete(struct fw_upload_priv *fwlp)
{
mutex_lock(&fwlp->lock);
fwlp->progress = FW_UPLOAD_PROG_IDLE;
mutex_unlock(&fwlp->lock);
}
static void fw_upload_main(struct work_struct *work)
{
struct fw_upload_priv *fwlp;
struct fw_sysfs *fw_sysfs;
u32 written = 0, offset = 0;
enum fw_upload_err ret;
struct device *fw_dev;
struct fw_upload *fwl;
fwlp = container_of(work, struct fw_upload_priv, work);
fwl = fwlp->fw_upload;
fw_sysfs = (struct fw_sysfs *)fwl->priv;
fw_dev = &fw_sysfs->dev;
fw_upload_update_progress(fwlp, FW_UPLOAD_PROG_PREPARING);
ret = fwlp->ops->prepare(fwl, fwlp->data, fwlp->remaining_size);
if (ret != FW_UPLOAD_ERR_NONE) {
fw_upload_set_error(fwlp, ret);
goto putdev_exit;
}
fw_upload_update_progress(fwlp, FW_UPLOAD_PROG_TRANSFERRING);
while (fwlp->remaining_size) {
ret = fwlp->ops->write(fwl, fwlp->data, offset,
fwlp->remaining_size, &written);
if (ret != FW_UPLOAD_ERR_NONE || !written) {
if (ret == FW_UPLOAD_ERR_NONE) {
dev_warn(fw_dev, "write-op wrote zero data\n");
ret = FW_UPLOAD_ERR_RW_ERROR;
}
fw_upload_set_error(fwlp, ret);
goto done;
}
fwlp->remaining_size -= written;
offset += written;
}
fw_upload_update_progress(fwlp, FW_UPLOAD_PROG_PROGRAMMING);
ret = fwlp->ops->poll_complete(fwl);
if (ret != FW_UPLOAD_ERR_NONE)
fw_upload_set_error(fwlp, ret);
done:
if (fwlp->ops->cleanup)
fwlp->ops->cleanup(fwl);
putdev_exit:
put_device(fw_dev->parent);
/*
* Note: fwlp->remaining_size is left unmodified here to provide
* additional information on errors. It will be reinitialized when
* the next firmeware upload begins.
*/
mutex_lock(&fw_lock);
fw_free_paged_buf(fw_sysfs->fw_priv);
fw_state_init(fw_sysfs->fw_priv);
mutex_unlock(&fw_lock);
fwlp->data = NULL;
fw_upload_prog_complete(fwlp);
}
/*
* Start a worker thread to upload data to the parent driver.
* Must be called with fw_lock held.
*/
int fw_upload_start(struct fw_sysfs *fw_sysfs)
{
struct fw_priv *fw_priv = fw_sysfs->fw_priv;
struct device *fw_dev = &fw_sysfs->dev;
struct fw_upload_priv *fwlp;
if (!fw_sysfs->fw_upload_priv)
return 0;
if (!fw_priv->size) {
fw_free_paged_buf(fw_priv);
fw_state_init(fw_sysfs->fw_priv);
return 0;
}
fwlp = fw_sysfs->fw_upload_priv;
mutex_lock(&fwlp->lock);
/* Do not interfere with an on-going fw_upload */
if (fwlp->progress != FW_UPLOAD_PROG_IDLE) {
mutex_unlock(&fwlp->lock);
return -EBUSY;
}
get_device(fw_dev->parent); /* released in fw_upload_main */
fwlp->progress = FW_UPLOAD_PROG_RECEIVING;
fwlp->err_code = 0;
fwlp->remaining_size = fw_priv->size;
fwlp->data = fw_priv->data;
pr_debug("%s: fw-%s fw_priv=%p data=%p size=%u\n",
__func__, fw_priv->fw_name,
fw_priv, fw_priv->data,
(unsigned int)fw_priv->size);
queue_work(system_long_wq, &fwlp->work);
mutex_unlock(&fwlp->lock);
return 0;
}
void fw_upload_free(struct fw_sysfs *fw_sysfs)
{
struct fw_upload_priv *fw_upload_priv = fw_sysfs->fw_upload_priv;
free_fw_priv(fw_sysfs->fw_priv);
kfree(fw_upload_priv->fw_upload);
kfree(fw_upload_priv);
}
/**
* firmware_upload_register() - register for the firmware upload sysfs API
* @module: kernel module of this device
* @parent: parent device instantiating firmware upload
* @name: firmware name to be associated with this device
* @ops: pointer to structure of firmware upload ops
* @dd_handle: pointer to parent driver private data
*
* @name must be unique among all users of firmware upload. The firmware
* sysfs files for this device will be found at /sys/class/firmware/@name.
*
* Return: struct fw_upload pointer or ERR_PTR()
*
**/
struct fw_upload *
firmware_upload_register(struct module *module, struct device *parent,
const char *name, const struct fw_upload_ops *ops,
void *dd_handle)
{
u32 opt_flags = FW_OPT_NOCACHE;
struct fw_upload *fw_upload;
struct fw_upload_priv *fw_upload_priv;
struct fw_sysfs *fw_sysfs;
struct fw_priv *fw_priv;
struct device *fw_dev;
int ret;
if (!name || name[0] == '\0')
return ERR_PTR(-EINVAL);
if (!ops || !ops->cancel || !ops->prepare ||
!ops->write || !ops->poll_complete) {
dev_err(parent, "Attempt to register without all required ops\n");
return ERR_PTR(-EINVAL);
}
if (!try_module_get(module))
return ERR_PTR(-EFAULT);
fw_upload = kzalloc_obj(*fw_upload);
if (!fw_upload) {
ret = -ENOMEM;
goto exit_module_put;
}
fw_upload_priv = kzalloc_obj(*fw_upload_priv);
if (!fw_upload_priv) {
ret = -ENOMEM;
goto free_fw_upload;
}
fw_upload_priv->fw_upload = fw_upload;
fw_upload_priv->ops = ops;
mutex_init(&fw_upload_priv->lock);
fw_upload_priv->module = module;
fw_upload_priv->name = name;
fw_upload_priv->err_code = 0;
fw_upload_priv->progress = FW_UPLOAD_PROG_IDLE;
INIT_WORK(&fw_upload_priv->work, fw_upload_main);
fw_upload->dd_handle = dd_handle;
fw_sysfs = fw_create_instance(NULL, name, parent, opt_flags);
if (IS_ERR(fw_sysfs)) {
ret = PTR_ERR(fw_sysfs);
goto free_fw_upload_priv;
}
fw_upload->priv = fw_sysfs;
fw_dev = &fw_sysfs->dev;
ret = alloc_lookup_fw_priv(name, &fw_cache, &fw_priv, NULL, 0, 0,
FW_OPT_NOCACHE);
if (ret != 0) {
if (ret > 0)
ret = -EINVAL;
put_device(fw_dev);
goto free_fw_upload_priv;
}
fw_priv->is_paged_buf = true;
fw_sysfs->fw_priv = fw_priv;
fw_sysfs->fw_upload_priv = fw_upload_priv;
ret = device_add(fw_dev);
if (ret) {
dev_err(fw_dev, "%s: device_register failed\n", __func__);
put_device(fw_dev);
goto exit_module_put;
}
return fw_upload;
free_fw_upload_priv:
kfree(fw_upload_priv);
free_fw_upload:
kfree(fw_upload);
exit_module_put:
module_put(module);
return ERR_PTR(ret);
}
EXPORT_SYMBOL_GPL(firmware_upload_register);
/**
* firmware_upload_unregister() - Unregister firmware upload interface
* @fw_upload: pointer to struct fw_upload
**/
void firmware_upload_unregister(struct fw_upload *fw_upload)
{
struct fw_sysfs *fw_sysfs = fw_upload->priv;
struct fw_upload_priv *fw_upload_priv = fw_sysfs->fw_upload_priv;
struct module *module = fw_upload_priv->module;
mutex_lock(&fw_upload_priv->lock);
if (fw_upload_priv->progress == FW_UPLOAD_PROG_IDLE) {
mutex_unlock(&fw_upload_priv->lock);
goto unregister;
}
fw_upload_priv->ops->cancel(fw_upload);
mutex_unlock(&fw_upload_priv->lock);
/* Ensure lower-level device-driver is finished */
flush_work(&fw_upload_priv->work);
unregister:
device_unregister(&fw_sysfs->dev);
module_put(module);
}
EXPORT_SYMBOL_GPL(firmware_upload_unregister);
@@ -0,0 +1,41 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef __SYSFS_UPLOAD_H
#define __SYSFS_UPLOAD_H
#include <linux/device.h>
#include "sysfs.h"
/**
* enum fw_upload_prog - firmware upload progress codes
* @FW_UPLOAD_PROG_IDLE: there is no firmware upload in progress
* @FW_UPLOAD_PROG_RECEIVING: worker thread is receiving firmware data
* @FW_UPLOAD_PROG_PREPARING: target device is preparing for firmware upload
* @FW_UPLOAD_PROG_TRANSFERRING: data is being copied to the device
* @FW_UPLOAD_PROG_PROGRAMMING: device is performing the firmware update
* @FW_UPLOAD_PROG_MAX: Maximum progress code marker
*/
enum fw_upload_prog {
FW_UPLOAD_PROG_IDLE,
FW_UPLOAD_PROG_RECEIVING,
FW_UPLOAD_PROG_PREPARING,
FW_UPLOAD_PROG_TRANSFERRING,
FW_UPLOAD_PROG_PROGRAMMING,
FW_UPLOAD_PROG_MAX
};
struct fw_upload_priv {
struct fw_upload *fw_upload;
struct module *module;
const char *name;
const struct fw_upload_ops *ops;
struct mutex lock; /* protect data structure contents */
struct work_struct work;
const u8 *data; /* pointer to update data */
u32 remaining_size; /* size remaining to transfer */
enum fw_upload_prog progress;
enum fw_upload_prog err_progress; /* progress at time of failure */
enum fw_upload_err err_code; /* security manager error code */
};
#endif /* __SYSFS_UPLOAD_H */