// 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. *
*/
struct firmware_cache { /* firmware_buf instance will be added into the below list */
spinlock_t lock; struct list_head head; int state;
#ifdef CONFIG_FW_CACHE /* * Names of firmware images which have been cached successfully * will be added into the below list so that device uncache * helper can trace which firmware images have been cached * before.
*/
spinlock_t name_lock; struct list_head fw_names;
/* Returns 1 for batching firmware requests with the same name */ int alloc_lookup_fw_priv(constchar *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;
spin_lock(&fwc->lock); /* * Do not merge requests that are marked to be non-cached or * are performing partial reads.
*/ if (!(opt_flags & (FW_OPT_NOCACHE | FW_OPT_PARTIAL))) {
tmp = __lookup_fw_priv(fw_name); if (tmp) {
kref_get(&tmp->ref);
spin_unlock(&fwc->lock);
*fw_priv = tmp;
pr_debug("batched request - sharing the same struct fw_priv and lookup for multiple requests\n"); return 1;
}
}
tmp = __allocate_fw_priv(fw_name, fwc, dbuf, size, offset, opt_flags); if (tmp) {
INIT_LIST_HEAD(&tmp->list); if (!(opt_flags & FW_OPT_NOCACHE))
list_add(&tmp->list, &fwc->head);
}
spin_unlock(&fwc->lock);
void fw_free_paged_buf(struct fw_priv *fw_priv)
{ int i;
if (!fw_priv->pages) return;
vunmap(fw_priv->data);
for (i = 0; i < fw_priv->nr_pages; i++)
__free_page(fw_priv->pages[i]);
kvfree(fw_priv->pages);
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)
{ /* If the array of pages is too small, grow it */ if (fw_priv->page_array_size < pages_needed) { int new_array_size = max(pages_needed,
fw_priv->page_array_size * 2); struct page **new_pages;
/* * 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(path, fw_path_para, sizeof(fw_path_para), 0644);
MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
/* Already populated data member means we're loading into a buffer */ if (!decompress && fw_priv->data) {
buffer = fw_priv->data;
msize = fw_priv->allocated_size;
}
path = __getname(); if (!path) return -ENOMEM;
wait_for_initramfs(); for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
size_t file_size = 0;
size_t *file_size_ptr = NULL;
/* skip the unset customized path */ if (!fw_path[i][0]) continue;
/* strip off \n from customized path */
maxlen = strlen(fw_path[i]); if (i == 0) {
nt = strchr(fw_path[i], '\n'); if (nt)
maxlen = nt - fw_path[i];
}
len = snprintf(path, PATH_MAX, "%.*s/%s%s",
maxlen, fw_path[i],
fw_priv->fw_name, suffix); if (len >= PATH_MAX) {
rc = -ENAMETOOLONG; break;
}
fw_priv->size = 0;
/* * The total file size is only examined when doing a partial * read; the "full read" case needs to fail if the whole * firmware was not completely loaded.
*/ if ((fw_priv->opt_flags & FW_OPT_PARTIAL) && buffer)
file_size_ptr = &file_size;
/* load firmware files from the mount namespace of init */
rc = kernel_read_file_from_path_initns(path, fw_priv->offset,
&buffer, msize,
file_size_ptr,
READING_FIRMWARE); if (rc < 0) { if (!(fw_priv->opt_flags & FW_OPT_NO_WARN)) { if (rc != -ENOENT)
dev_warn(device, "loading %s failed with error %d\n",
path, rc); else
dev_dbg(device, "loading %s failed for no such file or directory.\n",
path);
} continue;
}
size = rc;
rc = 0;
dev_dbg(device, "Loading firmware from %s\n", path); if (decompress) {
dev_dbg(device, "f/w decompressing %s\n",
fw_priv->fw_name);
rc = decompress(device, fw_priv, size, buffer); /* discard the superfluous original content */
vfree(buffer);
buffer = NULL; if (rc) {
fw_free_paged_buf(fw_priv); continue;
}
} else {
dev_dbg(device, "direct-loading %s\n",
fw_priv->fw_name); if (!fw_priv->data)
fw_priv->data = buffer;
fw_priv->size = size;
}
fw_state_done(fw_priv); break;
}
__putname(path);
return rc;
}
/* firmware holds the ownership of pages */ staticvoid firmware_free_data(conststruct firmware *fw)
{ /* Loaded directly? */ if (!fw->priv) {
vfree(fw->data); return;
}
free_fw_priv(fw->priv);
}
/* store the pages buffer info firmware from buf */ staticvoid fw_set_page_data(struct fw_priv *fw_priv, struct firmware *fw)
{
fw->priv = fw_priv;
fw->size = fw_priv->size;
fw->data = fw_priv->data;
int assign_fw(struct firmware *fw, struct device *device)
{ struct fw_priv *fw_priv = fw->priv; int ret;
mutex_lock(&fw_lock); if (!fw_priv->size || fw_state_is_aborted(fw_priv)) {
mutex_unlock(&fw_lock); return -ENOENT;
}
/* * add firmware name into devres list so that we can auto cache * and uncache firmware for device. * * device may has been deleted already, but the problem * should be fixed in devres or driver core.
*/ /* don't cache firmware handled without uevent */ if (device && (fw_priv->opt_flags & FW_OPT_UEVENT) &&
!(fw_priv->opt_flags & FW_OPT_NOCACHE)) {
ret = fw_add_devm_name(device, fw_priv->fw_name); if (ret) {
mutex_unlock(&fw_lock); return ret;
}
}
/* * After caching firmware image is started, let it piggyback * on request firmware.
*/ if (!(fw_priv->opt_flags & FW_OPT_NOCACHE) &&
fw_priv->fwc->state == FW_LOADER_START_CACHE)
fw_cache_piggyback_on_request(fw_priv);
/* pass the pages buffer to driver at the last minute */
fw_set_page_data(fw_priv, fw);
mutex_unlock(&fw_lock); return 0;
}
/* 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
*/ staticint
_request_firmware_prepare(struct firmware **firmware_p, constchar *name, struct device *device, void *dbuf, size_t size,
size_t offset, u32 opt_flags)
{ struct firmware *firmware; struct fw_priv *fw_priv; int ret;
ret = alloc_lookup_fw_priv(name, &fw_cache, &fw_priv, dbuf, size,
offset, opt_flags);
/* * bind with 'priv' now to avoid warning in failure path * of requesting firmware.
*/
firmware->priv = fw_priv;
if (ret > 0) {
ret = fw_state_wait(fw_priv); if (!ret) {
fw_set_page_data(fw_priv, firmware); return 0; /* assigned */
}
}
if (ret < 0) return ret; return 1; /* need to load */
}
/* * 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.
*/ staticvoid fw_abort_batch_reqs(struct firmware *fw)
{ struct fw_priv *fw_priv;
/* Loaded directly? */ if (!fw || !fw->priv) return;
fw_priv = fw->priv;
mutex_lock(&fw_lock); if (!fw_state_is_aborted(fw_priv))
fw_state_aborted(fw_priv);
mutex_unlock(&fw_lock);
}
if (!name || name[0] == '\0') {
ret = -EINVAL; goto out;
}
/* * Reject firmware file names with ".." path components. * There are drivers that construct firmware file names from * device-supplied strings, and we don't want some device to be * able to tell us "I would like to be sent my firmware from * ../../../etc/shadow, please". * * This intentionally only looks at the firmware name, not at * the firmware base directory or at symlink contents.
*/ if (name_contains_dotdot(name)) {
dev_warn(device, "Firmware load for '%s' refused, path contains '..' component\n",
name);
ret = -EINVAL; goto out;
}
ret = _request_firmware_prepare(&fw, name, device, buf, size,
offset, opt_flags); if (ret <= 0) /* error or already assigned */ goto out;
/* * We are about to try to access the firmware file. Because we may have been * called by a driver when serving an unrelated request from userland, we use * the kernel credentials to read the file.
*/
kern_cred = prepare_kernel_cred(&init_task); if (!kern_cred) {
ret = -ENOMEM; goto out;
}
old_cred = override_creds(kern_cred);
ret = fw_get_filesystem_firmware(device, fw->priv, "", NULL);
/* Only full reads can support decompression, platform, and sysfs. */ if (!(opt_flags & FW_OPT_PARTIAL))
nondirect = true;
#ifdef CONFIG_FW_LOADER_COMPRESS_ZSTD if (ret == -ENOENT && nondirect)
ret = fw_get_filesystem_firmware(device, fw->priv, ".zst",
fw_decompress_zstd); #endif #ifdef CONFIG_FW_LOADER_COMPRESS_XZ if (ret == -ENOENT && nondirect)
ret = fw_get_filesystem_firmware(device, fw->priv, ".xz",
fw_decompress_xz); #endif if (ret == -ENOENT && nondirect)
ret = firmware_fallback_platform(fw->priv);
if (ret) { if (!(opt_flags & FW_OPT_NO_WARN))
dev_warn(device, "Direct firmware load for %s failed with error %d\n",
name, ret); if (nondirect)
ret = firmware_fallback_sysfs(fw, name, device,
opt_flags, ret);
} else
ret = assign_fw(fw, device);
/** * 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. * It must not contain any ".." path components - "foo/bar..bin" is * allowed, but "foo/../bar.bin" is not. * * Caller must hold the reference count of @device. * * The function can be called safely inside device's suspend and * resume callback.
**/ int
request_firmware(conststruct firmware **firmware_p, constchar *name, struct device *device)
{ int ret;
/* Need to pin this module until return */
__module_get(THIS_MODULE);
ret = _request_firmware(firmware_p, name, device, NULL, 0, 0,
FW_OPT_UEVENT);
module_put(THIS_MODULE); return ret;
}
EXPORT_SYMBOL(request_firmware);
/** * 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(conststruct firmware **firmware, constchar *name, struct device *device)
{ int ret;
/* Need to pin this module until return */
__module_get(THIS_MODULE);
ret = _request_firmware(firmware, name, device, NULL, 0, 0,
FW_OPT_UEVENT | FW_OPT_NO_WARN);
module_put(THIS_MODULE); return ret;
}
EXPORT_SYMBOL_GPL(firmware_request_nowarn);
/** * 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(conststruct firmware **firmware_p, constchar *name, struct device *device)
{ int ret;
/** * 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(conststruct firmware **firmware, constchar *name, struct device *device)
{ int ret;
/* Need to pin this module until return */
__module_get(THIS_MODULE);
ret = _request_firmware(firmware, name, device, NULL, 0, 0,
FW_OPT_UEVENT | FW_OPT_FALLBACK_PLATFORM);
module_put(THIS_MODULE); return ret;
}
EXPORT_SYMBOL_GPL(firmware_request_platform);
/** * firmware_request_cache() - cache firmware for suspend so resume can use it * @device: device for which firmware should be cached for * @name: name of firmware file * * 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, constchar *name)
{ int ret;
mutex_lock(&fw_lock);
ret = fw_add_devm_name(device, name);
mutex_unlock(&fw_lock);
/** * 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(conststruct firmware **firmware_p, constchar *name, struct device *device, void *buf, size_t size)
{ int ret;
if (fw_cache_is_setup(device, name)) return -EOPNOTSUPP;
/** * 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(conststruct firmware **firmware_p, constchar *name, struct device *device, void *buf, size_t size, size_t offset)
{ int ret;
if (fw_cache_is_setup(device, name)) return -EOPNOTSUPP;
/** * 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, constchar *name, struct device *device, gfp_t gfp, void *context, void (*cont)(conststruct firmware *fw, void *context))
{ return _request_firmware_nowait(module, uevent, name, device, gfp,
context, cont, false);
}
EXPORT_SYMBOL(request_firmware_nowait);
/** * 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, constchar *name, struct device *device, gfp_t gfp, void *context, void (*cont)(conststruct firmware *fw, void *context))
{ return _request_firmware_nowait(module, FW_ACTION_UEVENT, name, device,
gfp, context, cont, true);
}
EXPORT_SYMBOL_GPL(firmware_request_nowait_nowarn);
/** * 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 *
*/ staticint cache_firmware(constchar *fw_name)
{ int ret; conststruct firmware *fw;
pr_debug("%s: %s\n", __func__, fw_name);
ret = request_firmware(&fw, fw_name, NULL); if (!ret)
kfree(fw);
spin_lock(&fwc->name_lock); /* only one cache entry for one firmware */ if (!__fw_entry_found(fce->name)) {
list_add(&fce->list, &fwc->fw_names);
} else {
free_fw_cache_entry(fce);
fce = NULL;
}
spin_unlock(&fwc->name_lock);
if (fce)
async_schedule_domain(__async_dev_cache_fw_image,
(void *)fce,
&fw_cache_domain);
}
}
/** * 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.
*/ staticvoid device_cache_fw_images(void)
{ struct firmware_cache *fwc = &fw_cache;
DEFINE_WAIT(wait);
pr_debug("%s\n", __func__);
/* cancel uncache work */
cancel_delayed_work_sync(&fwc->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.
*/ staticvoid device_uncache_fw_images_delay(unsignedlong delay)
{
queue_delayed_work(system_power_efficient_wq, &fw_cache.work,
msecs_to_jiffies(delay));
}
staticint fw_pm_notify(struct notifier_block *notify_block, unsignedlong mode, void *unused)
{ switch (mode) { case PM_HIBERNATION_PREPARE: case PM_SUSPEND_PREPARE: case PM_RESTORE_PREPARE: /* * Here, kill pending fallback requests will only kill * non-uevent firmware request to avoid stalling suspend.
*/
kill_pending_fw_fallback_reqs(false);
device_cache_fw_images(); break;
case PM_POST_SUSPEND: case PM_POST_HIBERNATION: case PM_POST_RESTORE: /* * In case that system sleep failed and syscore_suspend is * not called.
*/
mutex_lock(&fw_lock);
fw_cache.state = FW_LOADER_NO_CACHE;
mutex_unlock(&fw_lock);
staticint fw_shutdown_notify(struct notifier_block *unused1, unsignedlong unused2, void *unused3)
{ /* * Kill all pending fallback requests to avoid both stalling shutdown, * and avoid a deadlock with the usermode_lock.
*/
kill_pending_fw_fallback_reqs(true);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.