0001
0002
0003 #include <linux/types.h>
0004 #include <linux/kconfig.h>
0005 #include <linux/list.h>
0006 #include <linux/security.h>
0007 #include <linux/umh.h>
0008 #include <linux/sysctl.h>
0009 #include <linux/module.h>
0010
0011 #include "fallback.h"
0012 #include "firmware.h"
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024 void fw_fallback_set_cache_timeout(void)
0025 {
0026 fw_fallback_config.old_timeout = __firmware_loading_timeout();
0027 __fw_fallback_set_timeout(10);
0028 }
0029
0030
0031 void fw_fallback_set_default_timeout(void)
0032 {
0033 __fw_fallback_set_timeout(fw_fallback_config.old_timeout);
0034 }
0035
0036 static long firmware_loading_timeout(void)
0037 {
0038 return __firmware_loading_timeout() > 0 ?
0039 __firmware_loading_timeout() * HZ : MAX_JIFFY_OFFSET;
0040 }
0041
0042 static inline int fw_sysfs_wait_timeout(struct fw_priv *fw_priv, long timeout)
0043 {
0044 return __fw_state_wait_common(fw_priv, timeout);
0045 }
0046
0047 static LIST_HEAD(pending_fw_head);
0048
0049 void kill_pending_fw_fallback_reqs(bool only_kill_custom)
0050 {
0051 struct fw_priv *fw_priv;
0052 struct fw_priv *next;
0053
0054 mutex_lock(&fw_lock);
0055 list_for_each_entry_safe(fw_priv, next, &pending_fw_head,
0056 pending_list) {
0057 if (!fw_priv->need_uevent || !only_kill_custom)
0058 __fw_load_abort(fw_priv);
0059 }
0060 mutex_unlock(&fw_lock);
0061 }
0062
0063
0064
0065
0066
0067
0068
0069
0070 static int fw_load_sysfs_fallback(struct fw_sysfs *fw_sysfs, long timeout)
0071 {
0072 int retval = 0;
0073 struct device *f_dev = &fw_sysfs->dev;
0074 struct fw_priv *fw_priv = fw_sysfs->fw_priv;
0075
0076
0077 if (!fw_priv->data)
0078 fw_priv->is_paged_buf = true;
0079
0080 dev_set_uevent_suppress(f_dev, true);
0081
0082 retval = device_add(f_dev);
0083 if (retval) {
0084 dev_err(f_dev, "%s: device_register failed\n", __func__);
0085 goto err_put_dev;
0086 }
0087
0088 mutex_lock(&fw_lock);
0089 if (fw_state_is_aborted(fw_priv)) {
0090 mutex_unlock(&fw_lock);
0091 retval = -EINTR;
0092 goto out;
0093 }
0094 list_add(&fw_priv->pending_list, &pending_fw_head);
0095 mutex_unlock(&fw_lock);
0096
0097 if (fw_priv->opt_flags & FW_OPT_UEVENT) {
0098 fw_priv->need_uevent = true;
0099 dev_set_uevent_suppress(f_dev, false);
0100 dev_dbg(f_dev, "firmware: requesting %s\n", fw_priv->fw_name);
0101 kobject_uevent(&fw_sysfs->dev.kobj, KOBJ_ADD);
0102 } else {
0103 timeout = MAX_JIFFY_OFFSET;
0104 }
0105
0106 retval = fw_sysfs_wait_timeout(fw_priv, timeout);
0107 if (retval < 0 && retval != -ENOENT) {
0108 mutex_lock(&fw_lock);
0109 fw_load_abort(fw_sysfs);
0110 mutex_unlock(&fw_lock);
0111 }
0112
0113 if (fw_state_is_aborted(fw_priv)) {
0114 if (retval == -ERESTARTSYS)
0115 retval = -EINTR;
0116 } else if (fw_priv->is_paged_buf && !fw_priv->data)
0117 retval = -ENOMEM;
0118
0119 out:
0120 device_del(f_dev);
0121 err_put_dev:
0122 put_device(f_dev);
0123 return retval;
0124 }
0125
0126 static int fw_load_from_user_helper(struct firmware *firmware,
0127 const char *name, struct device *device,
0128 u32 opt_flags)
0129 {
0130 struct fw_sysfs *fw_sysfs;
0131 long timeout;
0132 int ret;
0133
0134 timeout = firmware_loading_timeout();
0135 if (opt_flags & FW_OPT_NOWAIT) {
0136 timeout = usermodehelper_read_lock_wait(timeout);
0137 if (!timeout) {
0138 dev_dbg(device, "firmware: %s loading timed out\n",
0139 name);
0140 return -EBUSY;
0141 }
0142 } else {
0143 ret = usermodehelper_read_trylock();
0144 if (WARN_ON(ret)) {
0145 dev_err(device, "firmware: %s will not be loaded\n",
0146 name);
0147 return ret;
0148 }
0149 }
0150
0151 fw_sysfs = fw_create_instance(firmware, name, device, opt_flags);
0152 if (IS_ERR(fw_sysfs)) {
0153 ret = PTR_ERR(fw_sysfs);
0154 goto out_unlock;
0155 }
0156
0157 fw_sysfs->fw_priv = firmware->priv;
0158 ret = fw_load_sysfs_fallback(fw_sysfs, timeout);
0159
0160 if (!ret)
0161 ret = assign_fw(firmware, device);
0162
0163 out_unlock:
0164 usermodehelper_read_unlock();
0165
0166 return ret;
0167 }
0168
0169 static bool fw_force_sysfs_fallback(u32 opt_flags)
0170 {
0171 if (fw_fallback_config.force_sysfs_fallback)
0172 return true;
0173 if (!(opt_flags & FW_OPT_USERHELPER))
0174 return false;
0175 return true;
0176 }
0177
0178 static bool fw_run_sysfs_fallback(u32 opt_flags)
0179 {
0180 int ret;
0181
0182 if (fw_fallback_config.ignore_sysfs_fallback) {
0183 pr_info_once("Ignoring firmware sysfs fallback due to sysctl knob\n");
0184 return false;
0185 }
0186
0187 if ((opt_flags & FW_OPT_NOFALLBACK_SYSFS))
0188 return false;
0189
0190
0191 ret = security_kernel_load_data(LOADING_FIRMWARE, true);
0192 if (ret < 0)
0193 return false;
0194
0195 return fw_force_sysfs_fallback(opt_flags);
0196 }
0197
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220 int firmware_fallback_sysfs(struct firmware *fw, const char *name,
0221 struct device *device,
0222 u32 opt_flags,
0223 int ret)
0224 {
0225 if (!fw_run_sysfs_fallback(opt_flags))
0226 return ret;
0227
0228 if (!(opt_flags & FW_OPT_NO_WARN))
0229 dev_warn(device, "Falling back to sysfs fallback for: %s\n",
0230 name);
0231 else
0232 dev_dbg(device, "Falling back to sysfs fallback for: %s\n",
0233 name);
0234 return fw_load_from_user_helper(fw, name, device, opt_flags);
0235 }