Linux Audio

Check our new training course

Loading...
  1// SPDX-License-Identifier: GPL-2.0
  2
  3#include <linux/types.h>
  4#include <linux/kconfig.h>
  5#include <linux/list.h>
  6#include <linux/security.h>
  7#include <linux/umh.h>
  8#include <linux/sysctl.h>
  9#include <linux/module.h>
 10
 11#include "fallback.h"
 12#include "firmware.h"
 13
 14/*
 15 * firmware fallback mechanism
 16 */
 17
 18/*
 19 * use small loading timeout for caching devices' firmware because all these
 20 * firmware images have been loaded successfully at lease once, also system is
 21 * ready for completing firmware loading now. The maximum size of firmware in
 22 * current distributions is about 2M bytes, so 10 secs should be enough.
 23 */
 24void fw_fallback_set_cache_timeout(void)
 25{
 26	fw_fallback_config.old_timeout = __firmware_loading_timeout();
 27	__fw_fallback_set_timeout(10);
 28}
 29
 30/* Restores the timeout to the value last configured during normal operation */
 31void fw_fallback_set_default_timeout(void)
 32{
 33	__fw_fallback_set_timeout(fw_fallback_config.old_timeout);
 34}
 35
 36static long firmware_loading_timeout(void)
 37{
 38	return __firmware_loading_timeout() > 0 ?
 39		__firmware_loading_timeout() * HZ : MAX_JIFFY_OFFSET;
 40}
 41
 42static inline int fw_sysfs_wait_timeout(struct fw_priv *fw_priv,  long timeout)
 43{
 44	return __fw_state_wait_common(fw_priv, timeout);
 45}
 46
 47static LIST_HEAD(pending_fw_head);
 48
 49void kill_pending_fw_fallback_reqs(bool only_kill_custom)
 50{
 51	struct fw_priv *fw_priv;
 52	struct fw_priv *next;
 53
 54	mutex_lock(&fw_lock);
 55	list_for_each_entry_safe(fw_priv, next, &pending_fw_head,
 56				 pending_list) {
 57		if (!fw_priv->need_uevent || !only_kill_custom)
 58			 __fw_load_abort(fw_priv);
 59	}
 60	mutex_unlock(&fw_lock);
 61}
 62
 63/**
 64 * fw_load_sysfs_fallback() - load a firmware via the sysfs fallback mechanism
 65 * @fw_sysfs: firmware sysfs information for the firmware to load
 66 * @timeout: timeout to wait for the load
 67 *
 68 * In charge of constructing a sysfs fallback interface for firmware loading.
 69 **/
 70static int fw_load_sysfs_fallback(struct fw_sysfs *fw_sysfs, long timeout)
 71{
 72	int retval = 0;
 73	struct device *f_dev = &fw_sysfs->dev;
 74	struct fw_priv *fw_priv = fw_sysfs->fw_priv;
 75
 76	/* fall back on userspace loading */
 77	if (!fw_priv->data)
 78		fw_priv->is_paged_buf = true;
 79
 80	dev_set_uevent_suppress(f_dev, true);
 81
 82	retval = device_add(f_dev);
 83	if (retval) {
 84		dev_err(f_dev, "%s: device_register failed\n", __func__);
 85		goto err_put_dev;
 86	}
 87
 88	mutex_lock(&fw_lock);
 89	if (fw_state_is_aborted(fw_priv)) {
 90		mutex_unlock(&fw_lock);
 91		retval = -EINTR;
 92		goto out;
 93	}
 94	list_add(&fw_priv->pending_list, &pending_fw_head);
 95	mutex_unlock(&fw_lock);
 96
 97	if (fw_priv->opt_flags & FW_OPT_UEVENT) {
 98		fw_priv->need_uevent = true;
 99		dev_set_uevent_suppress(f_dev, false);
100		dev_dbg(f_dev, "firmware: requesting %s\n", fw_priv->fw_name);
101		kobject_uevent(&fw_sysfs->dev.kobj, KOBJ_ADD);
102	} else {
103		timeout = MAX_JIFFY_OFFSET;
104	}
105
106	retval = fw_sysfs_wait_timeout(fw_priv, timeout);
107	if (retval < 0 && retval != -ENOENT) {
108		mutex_lock(&fw_lock);
109		fw_load_abort(fw_sysfs);
110		mutex_unlock(&fw_lock);
111	}
112
113	if (fw_state_is_aborted(fw_priv)) {
114		if (retval == -ERESTARTSYS)
115			retval = -EINTR;
116	} else if (fw_priv->is_paged_buf && !fw_priv->data)
117		retval = -ENOMEM;
118
119out:
120	device_del(f_dev);
121err_put_dev:
122	put_device(f_dev);
123	return retval;
124}
125
126static int fw_load_from_user_helper(struct firmware *firmware,
127				    const char *name, struct device *device,
128				    u32 opt_flags)
129{
130	struct fw_sysfs *fw_sysfs;
131	long timeout;
132	int ret;
133
134	timeout = firmware_loading_timeout();
135	if (opt_flags & FW_OPT_NOWAIT) {
136		timeout = usermodehelper_read_lock_wait(timeout);
137		if (!timeout) {
138			dev_dbg(device, "firmware: %s loading timed out\n",
139				name);
140			return -EBUSY;
141		}
142	} else {
143		ret = usermodehelper_read_trylock();
144		if (WARN_ON(ret)) {
145			dev_err(device, "firmware: %s will not be loaded\n",
146				name);
147			return ret;
148		}
149	}
150
151	fw_sysfs = fw_create_instance(firmware, name, device, opt_flags);
152	if (IS_ERR(fw_sysfs)) {
153		ret = PTR_ERR(fw_sysfs);
154		goto out_unlock;
155	}
156
157	fw_sysfs->fw_priv = firmware->priv;
158	ret = fw_load_sysfs_fallback(fw_sysfs, timeout);
159
160	if (!ret)
161		ret = assign_fw(firmware, device);
162
163out_unlock:
164	usermodehelper_read_unlock();
165
166	return ret;
167}
168
169static bool fw_force_sysfs_fallback(u32 opt_flags)
170{
171	if (fw_fallback_config.force_sysfs_fallback)
172		return true;
173	if (!(opt_flags & FW_OPT_USERHELPER))
174		return false;
175	return true;
176}
177
178static bool fw_run_sysfs_fallback(u32 opt_flags)
179{
180	int ret;
181
182	if (fw_fallback_config.ignore_sysfs_fallback) {
183		pr_info_once("Ignoring firmware sysfs fallback due to sysctl knob\n");
184		return false;
185	}
186
187	if ((opt_flags & FW_OPT_NOFALLBACK_SYSFS))
188		return false;
189
190	/* Also permit LSMs and IMA to fail firmware sysfs fallback */
191	ret = security_kernel_load_data(LOADING_FIRMWARE, true);
192	if (ret < 0)
193		return false;
194
195	return fw_force_sysfs_fallback(opt_flags);
196}
197
198/**
199 * firmware_fallback_sysfs() - use the fallback mechanism to find firmware
200 * @fw: pointer to firmware image
201 * @name: name of firmware file to look for
202 * @device: device for which firmware is being loaded
203 * @opt_flags: options to control firmware loading behaviour, as defined by
204 *	       &enum fw_opt
205 * @ret: return value from direct lookup which triggered the fallback mechanism
206 *
207 * This function is called if direct lookup for the firmware failed, it enables
208 * a fallback mechanism through userspace by exposing a sysfs loading
209 * interface. Userspace is in charge of loading the firmware through the sysfs
210 * loading interface. This sysfs fallback mechanism may be disabled completely
211 * on a system by setting the proc sysctl value ignore_sysfs_fallback to true.
212 * If this is false we check if the internal API caller set the
213 * @FW_OPT_NOFALLBACK_SYSFS flag, if so it would also disable the fallback
214 * mechanism. A system may want to enforce the sysfs fallback mechanism at all
215 * times, it can do this by setting ignore_sysfs_fallback to false and
216 * force_sysfs_fallback to true.
217 * Enabling force_sysfs_fallback is functionally equivalent to build a kernel
218 * with CONFIG_FW_LOADER_USER_HELPER_FALLBACK.
219 **/
220int firmware_fallback_sysfs(struct firmware *fw, const char *name,
221			    struct device *device,
222			    u32 opt_flags,
223			    int ret)
224{
225	if (!fw_run_sysfs_fallback(opt_flags))
226		return ret;
227
228	if (!(opt_flags & FW_OPT_NO_WARN))
229		dev_warn(device, "Falling back to sysfs fallback for: %s\n",
230				 name);
231	else
232		dev_dbg(device, "Falling back to sysfs fallback for: %s\n",
233				name);
234	return fw_load_from_user_helper(fw, name, device, opt_flags);
235}
1