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 */ 24 void 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 */ 31 void fw_fallback_set_default_timeout(void) 32 { 33 __fw_fallback_set_timeout(fw_fallback_config.old_timeout); 34 } 35 36 static long firmware_loading_timeout(void) 37 { 38 return __firmware_loading_timeout() > 0 ? 39 __firmware_loading_timeout() * HZ : MAX_JIFFY_OFFSET; 40 } 41 42 static 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 47 static LIST_HEAD(pending_fw_head); 48 49 void 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(&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 **/ 74 static 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(f_dev, true); 85 86 retval = device_add(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(&fw_lock); 95 retval = -EINTR; 96 goto out; 97 } 98 99 /* 100 * device_add() exposes the loading interface before pending_list is 101 * linked into pending_fw_head, so fw_state_done() may run first. 102 */ 103 if (fw_state_is_done(fw_priv)) { 104 mutex_unlock(&fw_lock); 105 goto out; 106 } 107 108 list_add(&fw_priv->pending_list, &pending_fw_head); 109 mutex_unlock(&fw_lock); 110 111 if (fw_priv->opt_flags & FW_OPT_UEVENT) { 112 fw_priv->need_uevent = true; 113 dev_set_uevent_suppress(f_dev, false); 114 dev_dbg(f_dev, "firmware: requesting %s\n", fw_priv->fw_name); 115 kobject_uevent(&fw_sysfs->dev.kobj, KOBJ_ADD); 116 } else { 117 timeout = MAX_JIFFY_OFFSET; 118 } 119 120 retval = fw_sysfs_wait_timeout(fw_priv, timeout); 121 if (retval < 0 && retval != -ENOENT) { 122 mutex_lock(&fw_lock); 123 fw_load_abort(fw_sysfs); 124 mutex_unlock(&fw_lock); 125 } 126 127 if (fw_state_is_aborted(fw_priv)) { 128 if (retval == -ERESTARTSYS) 129 retval = -EINTR; 130 } else if (fw_priv->is_paged_buf && !fw_priv->data) 131 retval = -ENOMEM; 132 133 out: 134 device_del(f_dev); 135 err_put_dev: 136 put_device(f_dev); 137 return retval; 138 } 139 140 static int fw_load_from_user_helper(struct firmware *firmware, 141 const char *name, struct device *device, 142 u32 opt_flags) 143 { 144 struct fw_sysfs *fw_sysfs; 145 long timeout; 146 int ret; 147 148 timeout = firmware_loading_timeout(); 149 if (opt_flags & FW_OPT_NOWAIT) { 150 timeout = usermodehelper_read_lock_wait(timeout); 151 if (!timeout) { 152 dev_dbg(device, "firmware: %s loading timed out\n", 153 name); 154 return -EBUSY; 155 } 156 } else { 157 ret = usermodehelper_read_trylock(); 158 if (WARN_ON(ret)) { 159 dev_err(device, "firmware: %s will not be loaded\n", 160 name); 161 return ret; 162 } 163 } 164 165 fw_sysfs = fw_create_instance(firmware, name, device, opt_flags); 166 if (IS_ERR(fw_sysfs)) { 167 ret = PTR_ERR(fw_sysfs); 168 goto out_unlock; 169 } 170 171 fw_sysfs->fw_priv = firmware->priv; 172 ret = fw_load_sysfs_fallback(fw_sysfs, timeout); 173 174 if (!ret) 175 ret = assign_fw(firmware, device); 176 177 out_unlock: 178 usermodehelper_read_unlock(); 179 180 return ret; 181 } 182 183 static bool fw_force_sysfs_fallback(u32 opt_flags) 184 { 185 if (fw_fallback_config.force_sysfs_fallback) 186 return true; 187 if (!(opt_flags & FW_OPT_USERHELPER)) 188 return false; 189 return true; 190 } 191 192 static bool fw_run_sysfs_fallback(u32 opt_flags) 193 { 194 int ret; 195 196 if (fw_fallback_config.ignore_sysfs_fallback) { 197 pr_info_once("Ignoring firmware sysfs fallback due to sysctl knob\n"); 198 return false; 199 } 200 201 if ((opt_flags & FW_OPT_NOFALLBACK_SYSFS)) 202 return false; 203 204 /* Also permit LSMs and IMA to fail firmware sysfs fallback */ 205 ret = security_kernel_load_data(LOADING_FIRMWARE, true); 206 if (ret < 0) 207 return false; 208 209 return fw_force_sysfs_fallback(opt_flags); 210 } 211 212 /** 213 * firmware_fallback_sysfs() - use the fallback mechanism to find firmware 214 * @fw: pointer to firmware image 215 * @name: name of firmware file to look for 216 * @device: device for which firmware is being loaded 217 * @opt_flags: options to control firmware loading behaviour, as defined by 218 * &enum fw_opt 219 * @ret: return value from direct lookup which triggered the fallback mechanism 220 * 221 * This function is called if direct lookup for the firmware failed, it enables 222 * a fallback mechanism through userspace by exposing a sysfs loading 223 * interface. Userspace is in charge of loading the firmware through the sysfs 224 * loading interface. This sysfs fallback mechanism may be disabled completely 225 * on a system by setting the proc sysctl value ignore_sysfs_fallback to true. 226 * If this is false we check if the internal API caller set the 227 * @FW_OPT_NOFALLBACK_SYSFS flag, if so it would also disable the fallback 228 * mechanism. A system may want to enforce the sysfs fallback mechanism at all 229 * times, it can do this by setting ignore_sysfs_fallback to false and 230 * force_sysfs_fallback to true. 231 * Enabling force_sysfs_fallback is functionally equivalent to build a kernel 232 * with CONFIG_FW_LOADER_USER_HELPER_FALLBACK. 233 **/ 234 int firmware_fallback_sysfs(struct firmware *fw, const char *name, 235 struct device *device, 236 u32 opt_flags, 237 int ret) 238 { 239 if (!fw_run_sysfs_fallback(opt_flags)) 240 return ret; 241 242 if (!(opt_flags & FW_OPT_NO_WARN)) 243 dev_warn(device, "Falling back to sysfs fallback for: %s\n", 244 name); 245 else 246 dev_dbg(device, "Falling back to sysfs fallback for: %s\n", 247 name); 248 return fw_load_from_user_helper(fw, name, device, opt_flags); 249 } 250