1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/types.h>
4 #include <linux/kconfig.h>
5 #include <linux/list.h>
6 #include <linux/security.h>
8 #include <linux/sysctl.h>
9 #include <linux/module.h>
15 * firmware fallback mechanism
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.
24 void fw_fallback_set_cache_timeout(void)
26 fw_fallback_config.old_timeout = __firmware_loading_timeout();
27 __fw_fallback_set_timeout(10);
30 /* Restores the timeout to the value last configured during normal operation */
31 void fw_fallback_set_default_timeout(void)
33 __fw_fallback_set_timeout(fw_fallback_config.old_timeout);
36 static long firmware_loading_timeout(void)
38 return __firmware_loading_timeout() > 0 ?
39 __firmware_loading_timeout() * HZ : MAX_JIFFY_OFFSET;
42 static inline int fw_sysfs_wait_timeout(struct fw_priv *fw_priv, long timeout)
44 return __fw_state_wait_common(fw_priv, timeout);
47 static LIST_HEAD(pending_fw_head);
49 void kill_pending_fw_fallback_reqs(bool only_kill_custom)
51 struct fw_priv *fw_priv;
55 list_for_each_entry_safe(fw_priv, next, &pending_fw_head,
57 if (!fw_priv->need_uevent || !only_kill_custom)
58 __fw_load_abort(fw_priv);
60 mutex_unlock(&fw_lock);
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
68 * In charge of constructing a sysfs fallback interface for firmware loading.
70 static int fw_load_sysfs_fallback(struct fw_sysfs *fw_sysfs, long timeout)
73 struct device *f_dev = &fw_sysfs->dev;
74 struct fw_priv *fw_priv = fw_sysfs->fw_priv;
76 /* fall back on userspace loading */
78 fw_priv->is_paged_buf = true;
80 dev_set_uevent_suppress(f_dev, true);
82 retval = device_add(f_dev);
84 dev_err(f_dev, "%s: device_register failed\n", __func__);
89 if (fw_state_is_aborted(fw_priv)) {
90 mutex_unlock(&fw_lock);
94 list_add(&fw_priv->pending_list, &pending_fw_head);
95 mutex_unlock(&fw_lock);
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);
103 timeout = MAX_JIFFY_OFFSET;
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);
113 if (fw_state_is_aborted(fw_priv)) {
114 if (retval == -ERESTARTSYS)
116 } else if (fw_priv->is_paged_buf && !fw_priv->data)
126 static int fw_load_from_user_helper(struct firmware *firmware,
127 const char *name, struct device *device,
130 struct fw_sysfs *fw_sysfs;
134 timeout = is_nonfree_firmware(name) ? 1 : firmware_loading_timeout();
135 if (opt_flags & FW_OPT_NOWAIT) {
136 timeout = usermodehelper_read_lock_wait(timeout);
138 dev_dbg(device, "firmware: %s loading timed out\n",
143 ret = usermodehelper_read_trylock();
145 dev_err(device, "firmware: %s will not be loaded\n",
151 fw_sysfs = fw_create_instance(firmware, name, device, opt_flags);
152 if (IS_ERR(fw_sysfs)) {
153 ret = PTR_ERR(fw_sysfs);
157 fw_sysfs->fw_priv = firmware->priv;
158 ret = fw_load_sysfs_fallback(fw_sysfs, timeout);
161 ret = assign_fw(firmware, device);
164 usermodehelper_read_unlock();
169 static bool fw_force_sysfs_fallback(u32 opt_flags)
171 if (fw_fallback_config.force_sysfs_fallback)
173 if (!(opt_flags & FW_OPT_USERHELPER))
178 static bool fw_run_sysfs_fallback(u32 opt_flags)
182 if (fw_fallback_config.ignore_sysfs_fallback) {
183 pr_info_once("Ignoring firmware sysfs fallback due to sysctl knob\n");
187 if ((opt_flags & FW_OPT_NOFALLBACK_SYSFS))
190 /* Also permit LSMs and IMA to fail firmware sysfs fallback */
191 ret = security_kernel_load_data(LOADING_FIRMWARE, true);
195 return fw_force_sysfs_fallback(opt_flags);
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
205 * @ret: return value from direct lookup which triggered the fallback mechanism
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.
220 int firmware_fallback_sysfs(struct firmware *fw, const char *name,
221 struct device *device,
225 if (!fw_run_sysfs_fallback(opt_flags))
228 if (!(opt_flags & FW_OPT_NO_WARN))
229 dev_warn(device, "Falling back to sysfs fallback for: %s\n",
232 dev_dbg(device, "Falling back to sysfs fallback for: %s\n",
234 return fw_load_from_user_helper(fw, name, device, opt_flags);