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 kill_all
)
51 struct fw_priv
*fw_priv
;
55 list_for_each_entry_safe(fw_priv
, next
, &pending_fw_head
,
57 if (kill_all
|| !fw_priv
->need_uevent
)
58 __fw_load_abort(fw_priv
);
62 fw_load_abort_all
= true;
64 mutex_unlock(&fw_lock
);
68 * fw_load_sysfs_fallback() - load a firmware via the sysfs fallback mechanism
69 * @fw_sysfs: firmware sysfs information for the firmware to load
70 * @timeout: timeout to wait for the load
72 * In charge of constructing a sysfs fallback interface for firmware loading.
74 static int fw_load_sysfs_fallback(struct fw_sysfs
*fw_sysfs
, long timeout
)
77 struct device
*f_dev
= &fw_sysfs
->dev
;
78 struct fw_priv
*fw_priv
= fw_sysfs
->fw_priv
;
80 /* fall back on userspace loading */
82 fw_priv
->is_paged_buf
= true;
84 dev_set_uevent_suppress(f_dev
, true);
86 retval
= device_add(f_dev
);
88 dev_err(f_dev
, "%s: device_register failed\n", __func__
);
93 if (fw_load_abort_all
|| fw_state_is_aborted(fw_priv
)) {
94 mutex_unlock(&fw_lock
);
98 list_add(&fw_priv
->pending_list
, &pending_fw_head
);
99 mutex_unlock(&fw_lock
);
101 if (fw_priv
->opt_flags
& FW_OPT_UEVENT
) {
102 fw_priv
->need_uevent
= true;
103 dev_set_uevent_suppress(f_dev
, false);
104 dev_dbg(f_dev
, "firmware: requesting %s\n", fw_priv
->fw_name
);
105 kobject_uevent(&fw_sysfs
->dev
.kobj
, KOBJ_ADD
);
107 timeout
= MAX_JIFFY_OFFSET
;
110 retval
= fw_sysfs_wait_timeout(fw_priv
, timeout
);
111 if (retval
< 0 && retval
!= -ENOENT
) {
112 mutex_lock(&fw_lock
);
113 fw_load_abort(fw_sysfs
);
114 mutex_unlock(&fw_lock
);
117 if (fw_state_is_aborted(fw_priv
)) {
118 if (retval
== -ERESTARTSYS
)
120 } else if (fw_priv
->is_paged_buf
&& !fw_priv
->data
)
130 static int fw_load_from_user_helper(struct firmware
*firmware
,
131 const char *name
, struct device
*device
,
134 struct fw_sysfs
*fw_sysfs
;
138 timeout
= firmware_loading_timeout();
139 if (opt_flags
& FW_OPT_NOWAIT
) {
140 timeout
= usermodehelper_read_lock_wait(timeout
);
142 dev_dbg(device
, "firmware: %s loading timed out\n",
147 ret
= usermodehelper_read_trylock();
149 dev_err(device
, "firmware: %s will not be loaded\n",
155 fw_sysfs
= fw_create_instance(firmware
, name
, device
, opt_flags
);
156 if (IS_ERR(fw_sysfs
)) {
157 ret
= PTR_ERR(fw_sysfs
);
161 fw_sysfs
->fw_priv
= firmware
->priv
;
162 ret
= fw_load_sysfs_fallback(fw_sysfs
, timeout
);
165 ret
= assign_fw(firmware
, device
);
168 usermodehelper_read_unlock();
173 static bool fw_force_sysfs_fallback(u32 opt_flags
)
175 if (fw_fallback_config
.force_sysfs_fallback
)
177 if (!(opt_flags
& FW_OPT_USERHELPER
))
182 static bool fw_run_sysfs_fallback(u32 opt_flags
)
186 if (fw_fallback_config
.ignore_sysfs_fallback
) {
187 pr_info_once("Ignoring firmware sysfs fallback due to sysctl knob\n");
191 if ((opt_flags
& FW_OPT_NOFALLBACK_SYSFS
))
194 /* Also permit LSMs and IMA to fail firmware sysfs fallback */
195 ret
= security_kernel_load_data(LOADING_FIRMWARE
, true);
199 return fw_force_sysfs_fallback(opt_flags
);
203 * firmware_fallback_sysfs() - use the fallback mechanism to find firmware
204 * @fw: pointer to firmware image
205 * @name: name of firmware file to look for
206 * @device: device for which firmware is being loaded
207 * @opt_flags: options to control firmware loading behaviour, as defined by
209 * @ret: return value from direct lookup which triggered the fallback mechanism
211 * This function is called if direct lookup for the firmware failed, it enables
212 * a fallback mechanism through userspace by exposing a sysfs loading
213 * interface. Userspace is in charge of loading the firmware through the sysfs
214 * loading interface. This sysfs fallback mechanism may be disabled completely
215 * on a system by setting the proc sysctl value ignore_sysfs_fallback to true.
216 * If this is false we check if the internal API caller set the
217 * @FW_OPT_NOFALLBACK_SYSFS flag, if so it would also disable the fallback
218 * mechanism. A system may want to enforce the sysfs fallback mechanism at all
219 * times, it can do this by setting ignore_sysfs_fallback to false and
220 * force_sysfs_fallback to true.
221 * Enabling force_sysfs_fallback is functionally equivalent to build a kernel
222 * with CONFIG_FW_LOADER_USER_HELPER_FALLBACK.
224 int firmware_fallback_sysfs(struct firmware
*fw
, const char *name
,
225 struct device
*device
,
229 if (!fw_run_sysfs_fallback(opt_flags
))
232 if (!(opt_flags
& FW_OPT_NO_WARN
))
233 dev_warn(device
, "Falling back to sysfs fallback for: %s\n",
236 dev_dbg(device
, "Falling back to sysfs fallback for: %s\n",
238 return fw_load_from_user_helper(fw
, name
, device
, opt_flags
);