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/slab.h>
7 #include <linux/security.h>
8 #include <linux/highmem.h>
10 #include <linux/sysctl.h>
11 #include <linux/vmalloc.h>
17 * firmware fallback mechanism
20 extern struct firmware_fallback_config fw_fallback_config;
22 /* These getters are vetted to use int properly */
23 static inline int __firmware_loading_timeout(void)
25 return fw_fallback_config.loading_timeout;
28 /* These setters are vetted to use int properly */
29 static void __fw_fallback_set_timeout(int timeout)
31 fw_fallback_config.loading_timeout = timeout;
35 * use small loading timeout for caching devices' firmware because all these
36 * firmware images have been loaded successfully at lease once, also system is
37 * ready for completing firmware loading now. The maximum size of firmware in
38 * current distributions is about 2M bytes, so 10 secs should be enough.
40 void fw_fallback_set_cache_timeout(void)
42 fw_fallback_config.old_timeout = __firmware_loading_timeout();
43 __fw_fallback_set_timeout(10);
46 /* Restores the timeout to the value last configured during normal operation */
47 void fw_fallback_set_default_timeout(void)
49 __fw_fallback_set_timeout(fw_fallback_config.old_timeout);
52 static long firmware_loading_timeout(void)
54 return __firmware_loading_timeout() > 0 ?
55 __firmware_loading_timeout() * HZ : MAX_JIFFY_OFFSET;
58 static inline bool fw_sysfs_done(struct fw_priv *fw_priv)
60 return __fw_state_check(fw_priv, FW_STATUS_DONE);
63 static inline bool fw_sysfs_loading(struct fw_priv *fw_priv)
65 return __fw_state_check(fw_priv, FW_STATUS_LOADING);
68 static inline int fw_sysfs_wait_timeout(struct fw_priv *fw_priv, long timeout)
70 return __fw_state_wait_common(fw_priv, timeout);
76 struct fw_priv *fw_priv;
80 static struct fw_sysfs *to_fw_sysfs(struct device *dev)
82 return container_of(dev, struct fw_sysfs, dev);
85 static void __fw_load_abort(struct fw_priv *fw_priv)
88 * There is a small window in which user can write to 'loading'
89 * between loading done and disappearance of 'loading'
91 if (fw_sysfs_done(fw_priv))
94 list_del_init(&fw_priv->pending_list);
95 fw_state_aborted(fw_priv);
98 static void fw_load_abort(struct fw_sysfs *fw_sysfs)
100 struct fw_priv *fw_priv = fw_sysfs->fw_priv;
102 __fw_load_abort(fw_priv);
105 static LIST_HEAD(pending_fw_head);
107 void kill_pending_fw_fallback_reqs(bool only_kill_custom)
109 struct fw_priv *fw_priv;
110 struct fw_priv *next;
112 mutex_lock(&fw_lock);
113 list_for_each_entry_safe(fw_priv, next, &pending_fw_head,
115 if (!fw_priv->need_uevent || !only_kill_custom)
116 __fw_load_abort(fw_priv);
118 mutex_unlock(&fw_lock);
121 static ssize_t timeout_show(struct class *class, struct class_attribute *attr,
124 return sprintf(buf, "%d\n", __firmware_loading_timeout());
128 * firmware_timeout_store() - set number of seconds to wait for firmware
129 * @class: device class pointer
130 * @attr: device attribute pointer
131 * @buf: buffer to scan for timeout value
132 * @count: number of bytes in @buf
134 * Sets the number of seconds to wait for the firmware. Once
135 * this expires an error will be returned to the driver and no
136 * firmware will be provided.
138 * Note: zero means 'wait forever'.
140 static ssize_t timeout_store(struct class *class, struct class_attribute *attr,
141 const char *buf, size_t count)
143 int tmp_loading_timeout = simple_strtol(buf, NULL, 10);
145 if (tmp_loading_timeout < 0)
146 tmp_loading_timeout = 0;
148 __fw_fallback_set_timeout(tmp_loading_timeout);
152 static CLASS_ATTR_RW(timeout);
154 static struct attribute *firmware_class_attrs[] = {
155 &class_attr_timeout.attr,
158 ATTRIBUTE_GROUPS(firmware_class);
160 static void fw_dev_release(struct device *dev)
162 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
167 static int do_firmware_uevent(struct fw_sysfs *fw_sysfs, struct kobj_uevent_env *env)
169 if (add_uevent_var(env, "FIRMWARE=%s", fw_sysfs->fw_priv->fw_name))
171 if (add_uevent_var(env, "TIMEOUT=%i", __firmware_loading_timeout()))
173 if (add_uevent_var(env, "ASYNC=%d", fw_sysfs->nowait))
179 static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
181 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
184 mutex_lock(&fw_lock);
185 if (fw_sysfs->fw_priv)
186 err = do_firmware_uevent(fw_sysfs, env);
187 mutex_unlock(&fw_lock);
191 static struct class firmware_class = {
193 .class_groups = firmware_class_groups,
194 .dev_uevent = firmware_uevent,
195 .dev_release = fw_dev_release,
198 int register_sysfs_loader(void)
200 return class_register(&firmware_class);
203 void unregister_sysfs_loader(void)
205 class_unregister(&firmware_class);
208 static ssize_t firmware_loading_show(struct device *dev,
209 struct device_attribute *attr, char *buf)
211 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
214 mutex_lock(&fw_lock);
215 if (fw_sysfs->fw_priv)
216 loading = fw_sysfs_loading(fw_sysfs->fw_priv);
217 mutex_unlock(&fw_lock);
219 return sprintf(buf, "%d\n", loading);
222 /* Some architectures don't have PAGE_KERNEL_RO */
223 #ifndef PAGE_KERNEL_RO
224 #define PAGE_KERNEL_RO PAGE_KERNEL
227 /* one pages buffer should be mapped/unmapped only once */
228 static int map_fw_priv_pages(struct fw_priv *fw_priv)
230 if (!fw_priv->is_paged_buf)
233 vunmap(fw_priv->data);
234 fw_priv->data = vmap(fw_priv->pages, fw_priv->nr_pages, 0,
242 * firmware_loading_store() - set value in the 'loading' control file
243 * @dev: device pointer
244 * @attr: device attribute pointer
245 * @buf: buffer to scan for loading control value
246 * @count: number of bytes in @buf
248 * The relevant values are:
250 * 1: Start a load, discarding any previous partial load.
251 * 0: Conclude the load and hand the data to the driver code.
252 * -1: Conclude the load with an error and discard any written data.
254 static ssize_t firmware_loading_store(struct device *dev,
255 struct device_attribute *attr,
256 const char *buf, size_t count)
258 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
259 struct fw_priv *fw_priv;
260 ssize_t written = count;
261 int loading = simple_strtol(buf, NULL, 10);
264 mutex_lock(&fw_lock);
265 fw_priv = fw_sysfs->fw_priv;
266 if (fw_state_is_aborted(fw_priv))
271 /* discarding any previous partial load */
272 if (!fw_sysfs_done(fw_priv)) {
273 for (i = 0; i < fw_priv->nr_pages; i++)
274 __free_page(fw_priv->pages[i]);
275 vfree(fw_priv->pages);
276 fw_priv->pages = NULL;
277 fw_priv->page_array_size = 0;
278 fw_priv->nr_pages = 0;
279 fw_state_start(fw_priv);
283 if (fw_sysfs_loading(fw_priv)) {
287 * Several loading requests may be pending on
288 * one same firmware buf, so let all requests
289 * see the mapped 'buf->data' once the loading
292 rc = map_fw_priv_pages(fw_priv);
294 dev_err(dev, "%s: map pages failed\n",
297 rc = security_kernel_post_read_file(NULL,
298 fw_priv->data, fw_priv->size,
302 * Same logic as fw_load_abort, only the DONE bit
303 * is ignored and we set ABORT only on failure.
305 list_del_init(&fw_priv->pending_list);
307 fw_state_aborted(fw_priv);
310 fw_state_done(fw_priv);
316 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
319 fw_load_abort(fw_sysfs);
323 mutex_unlock(&fw_lock);
327 static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
329 static void firmware_rw_data(struct fw_priv *fw_priv, char *buffer,
330 loff_t offset, size_t count, bool read)
333 memcpy(buffer, fw_priv->data + offset, count);
335 memcpy(fw_priv->data + offset, buffer, count);
338 static void firmware_rw(struct fw_priv *fw_priv, char *buffer,
339 loff_t offset, size_t count, bool read)
343 int page_nr = offset >> PAGE_SHIFT;
344 int page_ofs = offset & (PAGE_SIZE-1);
345 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
347 page_data = kmap(fw_priv->pages[page_nr]);
350 memcpy(buffer, page_data + page_ofs, page_cnt);
352 memcpy(page_data + page_ofs, buffer, page_cnt);
354 kunmap(fw_priv->pages[page_nr]);
361 static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
362 struct bin_attribute *bin_attr,
363 char *buffer, loff_t offset, size_t count)
365 struct device *dev = kobj_to_dev(kobj);
366 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
367 struct fw_priv *fw_priv;
370 mutex_lock(&fw_lock);
371 fw_priv = fw_sysfs->fw_priv;
372 if (!fw_priv || fw_sysfs_done(fw_priv)) {
376 if (offset > fw_priv->size) {
380 if (count > fw_priv->size - offset)
381 count = fw_priv->size - offset;
386 firmware_rw_data(fw_priv, buffer, offset, count, true);
388 firmware_rw(fw_priv, buffer, offset, count, true);
391 mutex_unlock(&fw_lock);
395 static int fw_realloc_pages(struct fw_sysfs *fw_sysfs, int min_size)
397 struct fw_priv *fw_priv= fw_sysfs->fw_priv;
398 int pages_needed = PAGE_ALIGN(min_size) >> PAGE_SHIFT;
400 /* If the array of pages is too small, grow it... */
401 if (fw_priv->page_array_size < pages_needed) {
402 int new_array_size = max(pages_needed,
403 fw_priv->page_array_size * 2);
404 struct page **new_pages;
406 new_pages = vmalloc(new_array_size * sizeof(void *));
408 fw_load_abort(fw_sysfs);
411 memcpy(new_pages, fw_priv->pages,
412 fw_priv->page_array_size * sizeof(void *));
413 memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) *
414 (new_array_size - fw_priv->page_array_size));
415 vfree(fw_priv->pages);
416 fw_priv->pages = new_pages;
417 fw_priv->page_array_size = new_array_size;
420 while (fw_priv->nr_pages < pages_needed) {
421 fw_priv->pages[fw_priv->nr_pages] =
422 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
424 if (!fw_priv->pages[fw_priv->nr_pages]) {
425 fw_load_abort(fw_sysfs);
434 * firmware_data_write() - write method for firmware
435 * @filp: open sysfs file
436 * @kobj: kobject for the device
437 * @bin_attr: bin_attr structure
438 * @buffer: buffer being written
439 * @offset: buffer offset for write in total data store area
440 * @count: buffer size
442 * Data written to the 'data' attribute will be later handed to
443 * the driver as a firmware image.
445 static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
446 struct bin_attribute *bin_attr,
447 char *buffer, loff_t offset, size_t count)
449 struct device *dev = kobj_to_dev(kobj);
450 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
451 struct fw_priv *fw_priv;
454 if (!capable(CAP_SYS_RAWIO))
457 mutex_lock(&fw_lock);
458 fw_priv = fw_sysfs->fw_priv;
459 if (!fw_priv || fw_sysfs_done(fw_priv)) {
465 if (offset + count > fw_priv->allocated_size) {
469 firmware_rw_data(fw_priv, buffer, offset, count, false);
472 retval = fw_realloc_pages(fw_sysfs, offset + count);
477 firmware_rw(fw_priv, buffer, offset, count, false);
480 fw_priv->size = max_t(size_t, offset + count, fw_priv->size);
482 mutex_unlock(&fw_lock);
486 static struct bin_attribute firmware_attr_data = {
487 .attr = { .name = "data", .mode = 0644 },
489 .read = firmware_data_read,
490 .write = firmware_data_write,
493 static struct attribute *fw_dev_attrs[] = {
494 &dev_attr_loading.attr,
498 static struct bin_attribute *fw_dev_bin_attrs[] = {
503 static const struct attribute_group fw_dev_attr_group = {
504 .attrs = fw_dev_attrs,
505 .bin_attrs = fw_dev_bin_attrs,
508 static const struct attribute_group *fw_dev_attr_groups[] = {
513 static struct fw_sysfs *
514 fw_create_instance(struct firmware *firmware, const char *fw_name,
515 struct device *device, enum fw_opt opt_flags)
517 struct fw_sysfs *fw_sysfs;
518 struct device *f_dev;
520 fw_sysfs = kzalloc(sizeof(*fw_sysfs), GFP_KERNEL);
522 fw_sysfs = ERR_PTR(-ENOMEM);
526 fw_sysfs->nowait = !!(opt_flags & FW_OPT_NOWAIT);
527 fw_sysfs->fw = firmware;
528 f_dev = &fw_sysfs->dev;
530 device_initialize(f_dev);
531 dev_set_name(f_dev, "%s", fw_name);
532 f_dev->parent = device;
533 f_dev->class = &firmware_class;
534 f_dev->groups = fw_dev_attr_groups;
540 * fw_load_sysfs_fallback() - load a firmware via the sysfs fallback mechanism
541 * @fw_sysfs: firmware sysfs information for the firmware to load
542 * @opt_flags: flags of options, FW_OPT_*
543 * @timeout: timeout to wait for the load
545 * In charge of constructing a sysfs fallback interface for firmware loading.
547 static int fw_load_sysfs_fallback(struct fw_sysfs *fw_sysfs,
548 enum fw_opt opt_flags, long timeout)
551 struct device *f_dev = &fw_sysfs->dev;
552 struct fw_priv *fw_priv = fw_sysfs->fw_priv;
554 /* fall back on userspace loading */
556 fw_priv->is_paged_buf = true;
558 dev_set_uevent_suppress(f_dev, true);
560 retval = device_add(f_dev);
562 dev_err(f_dev, "%s: device_register failed\n", __func__);
566 mutex_lock(&fw_lock);
567 list_add(&fw_priv->pending_list, &pending_fw_head);
568 mutex_unlock(&fw_lock);
570 if (opt_flags & FW_OPT_UEVENT) {
571 fw_priv->need_uevent = true;
572 dev_set_uevent_suppress(f_dev, false);
573 dev_dbg(f_dev, "firmware: requesting %s\n", fw_priv->fw_name);
574 kobject_uevent(&fw_sysfs->dev.kobj, KOBJ_ADD);
576 timeout = MAX_JIFFY_OFFSET;
579 retval = fw_sysfs_wait_timeout(fw_priv, timeout);
581 mutex_lock(&fw_lock);
582 fw_load_abort(fw_sysfs);
583 mutex_unlock(&fw_lock);
586 if (fw_state_is_aborted(fw_priv)) {
587 if (retval == -ERESTARTSYS)
591 } else if (fw_priv->is_paged_buf && !fw_priv->data)
600 static int fw_load_from_user_helper(struct firmware *firmware,
601 const char *name, struct device *device,
602 enum fw_opt opt_flags)
604 struct fw_sysfs *fw_sysfs;
608 timeout = firmware_loading_timeout();
609 if (opt_flags & FW_OPT_NOWAIT) {
610 timeout = usermodehelper_read_lock_wait(timeout);
612 dev_dbg(device, "firmware: %s loading timed out\n",
617 ret = usermodehelper_read_trylock();
619 dev_err(device, "firmware: %s will not be loaded\n",
625 fw_sysfs = fw_create_instance(firmware, name, device, opt_flags);
626 if (IS_ERR(fw_sysfs)) {
627 ret = PTR_ERR(fw_sysfs);
631 fw_sysfs->fw_priv = firmware->priv;
632 ret = fw_load_sysfs_fallback(fw_sysfs, opt_flags, timeout);
635 ret = assign_fw(firmware, device, opt_flags);
638 usermodehelper_read_unlock();
643 static bool fw_force_sysfs_fallback(enum fw_opt opt_flags)
645 if (fw_fallback_config.force_sysfs_fallback)
647 if (!(opt_flags & FW_OPT_USERHELPER))
652 static bool fw_run_sysfs_fallback(enum fw_opt opt_flags)
654 if (fw_fallback_config.ignore_sysfs_fallback) {
655 pr_info_once("Ignoring firmware sysfs fallback due to sysctl knob\n");
659 if ((opt_flags & FW_OPT_NOFALLBACK))
662 return fw_force_sysfs_fallback(opt_flags);
666 * firmware_fallback_sysfs() - use the fallback mechanism to find firmware
667 * @fw: pointer to firmware image
668 * @name: name of firmware file to look for
669 * @device: device for which firmware is being loaded
670 * @opt_flags: options to control firmware loading behaviour
671 * @ret: return value from direct lookup which triggered the fallback mechanism
673 * This function is called if direct lookup for the firmware failed, it enables
674 * a fallback mechanism through userspace by exposing a sysfs loading
675 * interface. Userspace is in charge of loading the firmware through the syfs
676 * loading interface. This syfs fallback mechanism may be disabled completely
677 * on a system by setting the proc sysctl value ignore_sysfs_fallback to true.
678 * If this false we check if the internal API caller set the @FW_OPT_NOFALLBACK
679 * flag, if so it would also disable the fallback mechanism. A system may want
680 * to enfoce the sysfs fallback mechanism at all times, it can do this by
681 * setting ignore_sysfs_fallback to false and force_sysfs_fallback to true.
682 * Enabling force_sysfs_fallback is functionally equivalent to build a kernel
683 * with CONFIG_FW_LOADER_USER_HELPER_FALLBACK.
685 int firmware_fallback_sysfs(struct firmware *fw, const char *name,
686 struct device *device,
687 enum fw_opt opt_flags,
690 if (!fw_run_sysfs_fallback(opt_flags))
693 if (!(opt_flags & FW_OPT_NO_WARN))
694 dev_warn(device, "Falling back to syfs fallback for: %s\n",
697 dev_dbg(device, "Falling back to sysfs fallback for: %s\n",
699 return fw_load_from_user_helper(fw, name, device, opt_flags);