Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
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  * firmware fallback mechanism
0016  */
0017 
0018 /*
0019  * use small loading timeout for caching devices' firmware because all these
0020  * firmware images have been loaded successfully at lease once, also system is
0021  * ready for completing firmware loading now. The maximum size of firmware in
0022  * current distributions is about 2M bytes, so 10 secs should be enough.
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 /* Restores the timeout to the value last configured during normal operation */
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  * fw_load_sysfs_fallback() - load a firmware via the sysfs fallback mechanism
0065  * @fw_sysfs: firmware sysfs information for the firmware to load
0066  * @timeout: timeout to wait for the load
0067  *
0068  * In charge of constructing a sysfs fallback interface for firmware loading.
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     /* fall back on userspace loading */
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     /* Also permit LSMs and IMA to fail firmware sysfs fallback */
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  * firmware_fallback_sysfs() - use the fallback mechanism to find firmware
0200  * @fw: pointer to firmware image
0201  * @name: name of firmware file to look for
0202  * @device: device for which firmware is being loaded
0203  * @opt_flags: options to control firmware loading behaviour, as defined by
0204  *         &enum fw_opt
0205  * @ret: return value from direct lookup which triggered the fallback mechanism
0206  *
0207  * This function is called if direct lookup for the firmware failed, it enables
0208  * a fallback mechanism through userspace by exposing a sysfs loading
0209  * interface. Userspace is in charge of loading the firmware through the sysfs
0210  * loading interface. This sysfs fallback mechanism may be disabled completely
0211  * on a system by setting the proc sysctl value ignore_sysfs_fallback to true.
0212  * If this is false we check if the internal API caller set the
0213  * @FW_OPT_NOFALLBACK_SYSFS flag, if so it would also disable the fallback
0214  * mechanism. A system may want to enforce the sysfs fallback mechanism at all
0215  * times, it can do this by setting ignore_sysfs_fallback to false and
0216  * force_sysfs_fallback to true.
0217  * Enabling force_sysfs_fallback is functionally equivalent to build a kernel
0218  * with CONFIG_FW_LOADER_USER_HELPER_FALLBACK.
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 }