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(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(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 kill_all)
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 (kill_all || !fw_priv->need_uevent)
58 __fw_load_abort(fw_priv);
59 }
60
61 if (kill_all)
62 fw_load_abort_all = true;
63
64 mutex_unlock(lock: &fw_lock);
65}
66
67/**
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
71 *
72 * In charge of constructing a sysfs fallback interface for firmware loading.
73 **/
74static int fw_load_sysfs_fallback(struct fw_sysfs *fw_sysfs, long timeout)
75{
76 int retval = 0;
77 struct device *f_dev = &fw_sysfs->dev;
78 struct fw_priv *fw_priv = fw_sysfs->fw_priv;
79
80 /* fall back on userspace loading */
81 if (!fw_priv->data)
82 fw_priv->is_paged_buf = true;
83
84 dev_set_uevent_suppress(dev: f_dev, val: true);
85
86 retval = device_add(dev: f_dev);
87 if (retval) {
88 dev_err(f_dev, "%s: device_register failed\n", __func__);
89 goto err_put_dev;
90 }
91
92 mutex_lock(&fw_lock);
93 if (fw_load_abort_all || fw_state_is_aborted(fw_priv)) {
94 mutex_unlock(lock: &fw_lock);
95 retval = -EINTR;
96 goto out;
97 }
98 list_add(new: &fw_priv->pending_list, head: &pending_fw_head);
99 mutex_unlock(lock: &fw_lock);
100
101 if (fw_priv->opt_flags & FW_OPT_UEVENT) {
102 fw_priv->need_uevent = true;
103 dev_set_uevent_suppress(dev: f_dev, val: false);
104 dev_dbg(f_dev, "firmware: requesting %s\n", fw_priv->fw_name);
105 kobject_uevent(kobj: &fw_sysfs->dev.kobj, action: KOBJ_ADD);
106 } else {
107 timeout = MAX_JIFFY_OFFSET;
108 }
109
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(lock: &fw_lock);
115 }
116
117 if (fw_state_is_aborted(fw_priv)) {
118 if (retval == -ERESTARTSYS)
119 retval = -EINTR;
120 } else if (fw_priv->is_paged_buf && !fw_priv->data)
121 retval = -ENOMEM;
122
123out:
124 device_del(dev: f_dev);
125err_put_dev:
126 put_device(dev: f_dev);
127 return retval;
128}
129
130static int fw_load_from_user_helper(struct firmware *firmware,
131 const char *name, struct device *device,
132 u32 opt_flags)
133{
134 struct fw_sysfs *fw_sysfs;
135 long timeout;
136 int ret;
137
138 timeout = firmware_loading_timeout();
139 if (opt_flags & FW_OPT_NOWAIT) {
140 timeout = usermodehelper_read_lock_wait(timeout);
141 if (!timeout) {
142 dev_dbg(device, "firmware: %s loading timed out\n",
143 name);
144 return -EBUSY;
145 }
146 } else {
147 ret = usermodehelper_read_trylock();
148 if (WARN_ON(ret)) {
149 dev_err(device, "firmware: %s will not be loaded\n",
150 name);
151 return ret;
152 }
153 }
154
155 fw_sysfs = fw_create_instance(firmware, fw_name: name, device, opt_flags);
156 if (IS_ERR(ptr: fw_sysfs)) {
157 ret = PTR_ERR(ptr: fw_sysfs);
158 goto out_unlock;
159 }
160
161 fw_sysfs->fw_priv = firmware->priv;
162 ret = fw_load_sysfs_fallback(fw_sysfs, timeout);
163
164 if (!ret)
165 ret = assign_fw(fw: firmware, device);
166
167out_unlock:
168 usermodehelper_read_unlock();
169
170 return ret;
171}
172
173static bool fw_force_sysfs_fallback(u32 opt_flags)
174{
175 if (fw_fallback_config.force_sysfs_fallback)
176 return true;
177 if (!(opt_flags & FW_OPT_USERHELPER))
178 return false;
179 return true;
180}
181
182static bool fw_run_sysfs_fallback(u32 opt_flags)
183{
184 int ret;
185
186 if (fw_fallback_config.ignore_sysfs_fallback) {
187 pr_info_once("Ignoring firmware sysfs fallback due to sysctl knob\n");
188 return false;
189 }
190
191 if ((opt_flags & FW_OPT_NOFALLBACK_SYSFS))
192 return false;
193
194 /* Also permit LSMs and IMA to fail firmware sysfs fallback */
195 ret = security_kernel_load_data(id: LOADING_FIRMWARE, contents: true);
196 if (ret < 0)
197 return false;
198
199 return fw_force_sysfs_fallback(opt_flags);
200}
201
202/**
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
208 * &enum fw_opt
209 * @ret: return value from direct lookup which triggered the fallback mechanism
210 *
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.
223 **/
224int firmware_fallback_sysfs(struct firmware *fw, const char *name,
225 struct device *device,
226 u32 opt_flags,
227 int ret)
228{
229 if (!fw_run_sysfs_fallback(opt_flags))
230 return ret;
231
232 if (!(opt_flags & FW_OPT_NO_WARN))
233 dev_warn(device, "Falling back to sysfs fallback for: %s\n",
234 name);
235 else
236 dev_dbg(device, "Falling back to sysfs fallback for: %s\n",
237 name);
238 return fw_load_from_user_helper(firmware: fw, name, device, opt_flags);
239}
240

source code of linux/drivers/base/firmware_loader/fallback.c