linux/drivers/base/firmware_loader/main.c

// SPDX-License-Identifier: GPL-2.0
/*
 * main.c - Multi purpose firmware loading support
 *
 * Copyright (c) 2003 Manuel Estrada Sainz
 *
 * Please see Documentation/driver-api/firmware/ for more information.
 *
 */

#define pr_fmt(fmt)

#include <linux/capability.h>
#include <linux/device.h>
#include <linux/kernel_read_file.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/initrd.h>
#include <linux/timer.h>
#include <linux/vmalloc.h>
#include <linux/interrupt.h>
#include <linux/bitops.h>
#include <linux/mutex.h>
#include <linux/workqueue.h>
#include <linux/highmem.h>
#include <linux/firmware.h>
#include <linux/slab.h>
#include <linux/sched.h>
#include <linux/file.h>
#include <linux/list.h>
#include <linux/fs.h>
#include <linux/async.h>
#include <linux/pm.h>
#include <linux/suspend.h>
#include <linux/syscore_ops.h>
#include <linux/reboot.h>
#include <linux/security.h>
#include <linux/zstd.h>
#include <linux/xz.h>

#include <generated/utsrelease.h>

#include "../base.h"
#include "firmware.h"
#include "fallback.h"

MODULE_AUTHOR();
MODULE_DESCRIPTION();
MODULE_LICENSE();

struct firmware_cache {};

struct fw_cache_entry {};

struct fw_name_devm {};

static inline struct fw_priv *to_fw_priv(struct kref *ref)
{}

#define FW_LOADER_NO_CACHE
#define FW_LOADER_START_CACHE

/* fw_lock could be moved to 'struct fw_sysfs' but since it is just
 * guarding for corner cases a global lock should be OK */
DEFINE_MUTEX();

struct firmware_cache fw_cache;
bool fw_load_abort_all;

void fw_state_init(struct fw_priv *fw_priv)
{}

static inline int fw_state_wait(struct fw_priv *fw_priv)
{}

static void fw_cache_piggyback_on_request(struct fw_priv *fw_priv);

static struct fw_priv *__allocate_fw_priv(const char *fw_name,
					  struct firmware_cache *fwc,
					  void *dbuf,
					  size_t size,
					  size_t offset,
					  u32 opt_flags)
{}

static struct fw_priv *__lookup_fw_priv(const char *fw_name)
{}

/* Returns 1 for batching firmware requests with the same name */
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)
{}

static void __free_fw_priv(struct kref *ref)
	__releases(&fwc->lock)
{}

void free_fw_priv(struct fw_priv *fw_priv)
{}

#ifdef CONFIG_FW_LOADER_PAGED_BUF
bool fw_is_paged_buf(struct fw_priv *fw_priv)
{}

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)
{}
#endif

/*
 * ZSTD-compressed firmware support
 */
#ifdef CONFIG_FW_LOADER_COMPRESS_ZSTD
static int fw_decompress_zstd(struct device *dev, struct fw_priv *fw_priv,
			      size_t in_size, const void *in_buffer)
{}
#endif /* CONFIG_FW_LOADER_COMPRESS_ZSTD */

/*
 * XZ-compressed firmware support
 */
#ifdef CONFIG_FW_LOADER_COMPRESS_XZ
/* show an error and return the standard error code */
static int fw_decompress_xz_error(struct device *dev, enum xz_ret xz_ret)
{}

/* single-shot decompression onto the pre-allocated buffer */
static int fw_decompress_xz_single(struct device *dev, struct fw_priv *fw_priv,
				   size_t in_size, const void *in_buffer)
{}

/* decompression on paged buffer and map it */
static int fw_decompress_xz_pages(struct device *dev, struct fw_priv *fw_priv,
				  size_t in_size, const void *in_buffer)
{}

static int fw_decompress_xz(struct device *dev, struct fw_priv *fw_priv,
			    size_t in_size, const void *in_buffer)
{}
#endif /* CONFIG_FW_LOADER_COMPRESS_XZ */

/* direct firmware loading support */
static char fw_path_para[256];
static const char * const fw_path[] =;

/*
 * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
 * from kernel command line because firmware_class is generally built in
 * kernel instead of module.
 */
module_param_string();
MODULE_PARM_DESC();

static int
fw_get_filesystem_firmware(struct device *device, struct fw_priv *fw_priv,
			   const char *suffix,
			   int (*decompress)(struct device *dev,
					     struct fw_priv *fw_priv,
					     size_t in_size,
					     const void *in_buffer))
{}

/* firmware holds the ownership of pages */
static void firmware_free_data(const struct firmware *fw)
{}

/* store the pages buffer info firmware from buf */
static void fw_set_page_data(struct fw_priv *fw_priv, struct firmware *fw)
{}

#ifdef CONFIG_FW_CACHE
static void fw_name_devm_release(struct device *dev, void *res)
{}

static int fw_devm_match(struct device *dev, void *res,
		void *match_data)
{}

static struct fw_name_devm *fw_find_devm_name(struct device *dev,
		const char *name)
{}

static bool fw_cache_is_setup(struct device *dev, const char *name)
{}

/* add firmware name into devres list */
static int fw_add_devm_name(struct device *dev, const char *name)
{}
#else
static bool fw_cache_is_setup(struct device *dev, const char *name)
{
	return false;
}

static int fw_add_devm_name(struct device *dev, const char *name)
{
	return 0;
}
#endif

int assign_fw(struct firmware *fw, struct device *device)
{}

/* prepare firmware and firmware_buf structs;
 * return 0 if a firmware is already assigned, 1 if need to load one,
 * or a negative error code
 */
static int
_request_firmware_prepare(struct firmware **firmware_p, const char *name,
			  struct device *device, void *dbuf, size_t size,
			  size_t offset, u32 opt_flags)
{}

/*
 * Batched requests need only one wake, we need to do this step last due to the
 * fallback mechanism. The buf is protected with kref_get(), and it won't be
 * released until the last user calls release_firmware().
 *
 * Failed batched requests are possible as well, in such cases we just share
 * the struct fw_priv and won't release it until all requests are woken
 * and have gone through this same path.
 */
static void fw_abort_batch_reqs(struct firmware *fw)
{}

#if defined(CONFIG_FW_LOADER_DEBUG)
#include <crypto/hash.h>
#include <crypto/sha2.h>

static void fw_log_firmware_info(const struct firmware *fw, const char *name, struct device *device)
{}
#else
static void fw_log_firmware_info(const struct firmware *fw, const char *name,
				 struct device *device)
{}
#endif

/* called from request_firmware() and request_firmware_work_func() */
static int
_request_firmware(const struct firmware **firmware_p, const char *name,
		  struct device *device, void *buf, size_t size,
		  size_t offset, u32 opt_flags)
{}

/**
 * request_firmware() - send firmware request and wait for it
 * @firmware_p: pointer to firmware image
 * @name: name of firmware file
 * @device: device for which firmware is being loaded
 *
 *      @firmware_p will be used to return a firmware image by the name
 *      of @name for device @device.
 *
 *      Should be called from user context where sleeping is allowed.
 *
 *      @name will be used as $FIRMWARE in the uevent environment and
 *      should be distinctive enough not to be confused with any other
 *      firmware image for this or any other device.
 *
 *	Caller must hold the reference count of @device.
 *
 *	The function can be called safely inside device's suspend and
 *	resume callback.
 **/
int
request_firmware(const struct firmware **firmware_p, const char *name,
		 struct device *device)
{}
EXPORT_SYMBOL();

/**
 * firmware_request_nowarn() - request for an optional fw module
 * @firmware: pointer to firmware image
 * @name: name of firmware file
 * @device: device for which firmware is being loaded
 *
 * This function is similar in behaviour to request_firmware(), except it
 * doesn't produce warning messages when the file is not found. The sysfs
 * fallback mechanism is enabled if direct filesystem lookup fails. However,
 * failures to find the firmware file with it are still suppressed. It is
 * therefore up to the driver to check for the return value of this call and to
 * decide when to inform the users of errors.
 **/
int firmware_request_nowarn(const struct firmware **firmware, const char *name,
			    struct device *device)
{}
EXPORT_SYMBOL_GPL();

/**
 * request_firmware_direct() - load firmware directly without usermode helper
 * @firmware_p: pointer to firmware image
 * @name: name of firmware file
 * @device: device for which firmware is being loaded
 *
 * This function works pretty much like request_firmware(), but this doesn't
 * fall back to usermode helper even if the firmware couldn't be loaded
 * directly from fs.  Hence it's useful for loading optional firmwares, which
 * aren't always present, without extra long timeouts of udev.
 **/
int request_firmware_direct(const struct firmware **firmware_p,
			    const char *name, struct device *device)
{}
EXPORT_SYMBOL_GPL();

/**
 * firmware_request_platform() - request firmware with platform-fw fallback
 * @firmware: pointer to firmware image
 * @name: name of firmware file
 * @device: device for which firmware is being loaded
 *
 * This function is similar in behaviour to request_firmware, except that if
 * direct filesystem lookup fails, it will fallback to looking for a copy of the
 * requested firmware embedded in the platform's main (e.g. UEFI) firmware.
 **/
int firmware_request_platform(const struct firmware **firmware,
			      const char *name, struct device *device)
{}
EXPORT_SYMBOL_GPL();

/**
 * firmware_request_cache() - cache firmware for suspend so resume can use it
 * @name: name of firmware file
 * @device: device for which firmware should be cached for
 *
 * There are some devices with an optimization that enables the device to not
 * require loading firmware on system reboot. This optimization may still
 * require the firmware present on resume from suspend. This routine can be
 * used to ensure the firmware is present on resume from suspend in these
 * situations. This helper is not compatible with drivers which use
 * request_firmware_into_buf() or request_firmware_nowait() with no uevent set.
 **/
int firmware_request_cache(struct device *device, const char *name)
{}
EXPORT_SYMBOL_GPL();

/**
 * request_firmware_into_buf() - load firmware into a previously allocated buffer
 * @firmware_p: pointer to firmware image
 * @name: name of firmware file
 * @device: device for which firmware is being loaded and DMA region allocated
 * @buf: address of buffer to load firmware into
 * @size: size of buffer
 *
 * This function works pretty much like request_firmware(), but it doesn't
 * allocate a buffer to hold the firmware data. Instead, the firmware
 * is loaded directly into the buffer pointed to by @buf and the @firmware_p
 * data member is pointed at @buf.
 *
 * This function doesn't cache firmware either.
 */
int
request_firmware_into_buf(const struct firmware **firmware_p, const char *name,
			  struct device *device, void *buf, size_t size)
{}
EXPORT_SYMBOL();

/**
 * request_partial_firmware_into_buf() - load partial firmware into a previously allocated buffer
 * @firmware_p: pointer to firmware image
 * @name: name of firmware file
 * @device: device for which firmware is being loaded and DMA region allocated
 * @buf: address of buffer to load firmware into
 * @size: size of buffer
 * @offset: offset into file to read
 *
 * This function works pretty much like request_firmware_into_buf except
 * it allows a partial read of the file.
 */
int
request_partial_firmware_into_buf(const struct firmware **firmware_p,
				  const char *name, struct device *device,
				  void *buf, size_t size, size_t offset)
{}
EXPORT_SYMBOL();

/**
 * release_firmware() - release the resource associated with a firmware image
 * @fw: firmware resource to release
 **/
void release_firmware(const struct firmware *fw)
{}
EXPORT_SYMBOL();

/* Async support */
struct firmware_work {};

static void request_firmware_work_func(struct work_struct *work)
{}


static int _request_firmware_nowait(
	struct module *module, bool uevent,
	const char *name, struct device *device, gfp_t gfp, void *context,
	void (*cont)(const struct firmware *fw, void *context), bool nowarn)
{}

/**
 * request_firmware_nowait() - asynchronous version of request_firmware
 * @module: module requesting the firmware
 * @uevent: sends uevent to copy the firmware image if this flag
 *	is non-zero else the firmware copy must be done manually.
 * @name: name of firmware file
 * @device: device for which firmware is being loaded
 * @gfp: allocation flags
 * @context: will be passed over to @cont, and
 *	@fw may be %NULL if firmware request fails.
 * @cont: function will be called asynchronously when the firmware
 *	request is over.
 *
 *	Caller must hold the reference count of @device.
 *
 *	Asynchronous variant of request_firmware() for user contexts:
 *		- sleep for as small periods as possible since it may
 *		  increase kernel boot time of built-in device drivers
 *		  requesting firmware in their ->probe() methods, if
 *		  @gfp is GFP_KERNEL.
 *
 *		- can't sleep at all if @gfp is GFP_ATOMIC.
 **/
int request_firmware_nowait(
	struct module *module, bool uevent,
	const char *name, struct device *device, gfp_t gfp, void *context,
	void (*cont)(const struct firmware *fw, void *context))
{}
EXPORT_SYMBOL();

/**
 * firmware_request_nowait_nowarn() - async version of request_firmware_nowarn
 * @module: module requesting the firmware
 * @name: name of firmware file
 * @device: device for which firmware is being loaded
 * @gfp: allocation flags
 * @context: will be passed over to @cont, and
 *	@fw may be %NULL if firmware request fails.
 * @cont: function will be called asynchronously when the firmware
 *	request is over.
 *
 * Similar in function to request_firmware_nowait(), but doesn't print a warning
 * when the firmware file could not be found and always sends a uevent to copy
 * the firmware image.
 */
int firmware_request_nowait_nowarn(
	struct module *module, const char *name,
	struct device *device, gfp_t gfp, void *context,
	void (*cont)(const struct firmware *fw, void *context))
{}
EXPORT_SYMBOL_GPL();

#ifdef CONFIG_FW_CACHE
static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);

/**
 * cache_firmware() - cache one firmware image in kernel memory space
 * @fw_name: the firmware image name
 *
 * Cache firmware in kernel memory so that drivers can use it when
 * system isn't ready for them to request firmware image from userspace.
 * Once it returns successfully, driver can use request_firmware or its
 * nowait version to get the cached firmware without any interacting
 * with userspace
 *
 * Return 0 if the firmware image has been cached successfully
 * Return !0 otherwise
 *
 */
static int cache_firmware(const char *fw_name)
{}

static struct fw_priv *lookup_fw_priv(const char *fw_name)
{}

/**
 * uncache_firmware() - remove one cached firmware image
 * @fw_name: the firmware image name
 *
 * Uncache one firmware image which has been cached successfully
 * before.
 *
 * Return 0 if the firmware cache has been removed successfully
 * Return !0 otherwise
 *
 */
static int uncache_firmware(const char *fw_name)
{}

static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
{}

static int __fw_entry_found(const char *name)
{}

static void fw_cache_piggyback_on_request(struct fw_priv *fw_priv)
{}

static void free_fw_cache_entry(struct fw_cache_entry *fce)
{}

static void __async_dev_cache_fw_image(void *fw_entry,
				       async_cookie_t cookie)
{}

/* called with dev->devres_lock held */
static void dev_create_fw_entry(struct device *dev, void *res,
				void *data)
{}

static int devm_name_match(struct device *dev, void *res,
			   void *match_data)
{}

static void dev_cache_fw_image(struct device *dev, void *data)
{}

static void __device_uncache_fw_images(void)
{}

/**
 * device_cache_fw_images() - cache devices' firmware
 *
 * If one device called request_firmware or its nowait version
 * successfully before, the firmware names are recored into the
 * device's devres link list, so device_cache_fw_images can call
 * cache_firmware() to cache these firmwares for the device,
 * then the device driver can load its firmwares easily at
 * time when system is not ready to complete loading firmware.
 */
static void device_cache_fw_images(void)
{}

/**
 * device_uncache_fw_images() - uncache devices' firmware
 *
 * uncache all firmwares which have been cached successfully
 * by device_uncache_fw_images earlier
 */
static void device_uncache_fw_images(void)
{}

static void device_uncache_fw_images_work(struct work_struct *work)
{}

/**
 * device_uncache_fw_images_delay() - uncache devices firmwares
 * @delay: number of milliseconds to delay uncache device firmwares
 *
 * uncache all devices's firmwares which has been cached successfully
 * by device_cache_fw_images after @delay milliseconds.
 */
static void device_uncache_fw_images_delay(unsigned long delay)
{}

static int fw_pm_notify(struct notifier_block *notify_block,
			unsigned long mode, void *unused)
{}

/* stop caching firmware once syscore_suspend is reached */
static int fw_suspend(void)
{}

static struct syscore_ops fw_syscore_ops =;

static int __init register_fw_pm_ops(void)
{}

static inline void unregister_fw_pm_ops(void)
{}
#else
static void fw_cache_piggyback_on_request(struct fw_priv *fw_priv)
{
}
static inline int register_fw_pm_ops(void)
{
	return 0;
}
static inline void unregister_fw_pm_ops(void)
{
}
#endif

static void __init fw_cache_init(void)
{}

static int fw_shutdown_notify(struct notifier_block *unused1,
			      unsigned long unused2, void *unused3)
{}

static struct notifier_block fw_shutdown_nb =;

static int __init firmware_class_init(void)
{}

static void __exit firmware_class_exit(void)
{}

fs_initcall(firmware_class_init);
module_exit(firmware_class_exit);