blob: 8970a5315e85470606af414d15b457173b7df02a [file] [log] [blame]
Luis R. Rodriguezd73f8212018-03-10 06:14:49 -08001// 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/slab.h>
7#include <linux/security.h>
8#include <linux/highmem.h>
9#include <linux/umh.h>
Luis R. Rodriguezceb18132018-03-10 06:14:51 -080010#include <linux/sysctl.h>
Stephen Rothwellccce3052018-03-21 19:06:35 +110011#include <linux/vmalloc.h>
Luis R. Rodriguezd73f8212018-03-10 06:14:49 -080012
Luis R. Rodriguez5d6d1dd2018-03-10 06:14:50 -080013#include "fallback.h"
14#include "firmware.h"
Luis R. Rodriguezd73f8212018-03-10 06:14:49 -080015
16/*
17 * firmware fallback mechanism
18 */
19
20extern struct firmware_fallback_config fw_fallback_config;
21
22/* These getters are vetted to use int properly */
23static inline int __firmware_loading_timeout(void)
24{
25 return fw_fallback_config.loading_timeout;
26}
27
28/* These setters are vetted to use int properly */
29static void __fw_fallback_set_timeout(int timeout)
30{
31 fw_fallback_config.loading_timeout = timeout;
32}
33
34/*
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.
39 */
40void fw_fallback_set_cache_timeout(void)
41{
42 fw_fallback_config.old_timeout = __firmware_loading_timeout();
43 __fw_fallback_set_timeout(10);
44}
45
46/* Restores the timeout to the value last configured during normal operation */
47void fw_fallback_set_default_timeout(void)
48{
49 __fw_fallback_set_timeout(fw_fallback_config.old_timeout);
50}
51
52static long firmware_loading_timeout(void)
53{
54 return __firmware_loading_timeout() > 0 ?
55 __firmware_loading_timeout() * HZ : MAX_JIFFY_OFFSET;
56}
57
58static inline bool fw_sysfs_done(struct fw_priv *fw_priv)
59{
60 return __fw_state_check(fw_priv, FW_STATUS_DONE);
61}
62
63static inline bool fw_sysfs_loading(struct fw_priv *fw_priv)
64{
65 return __fw_state_check(fw_priv, FW_STATUS_LOADING);
66}
67
68static inline int fw_sysfs_wait_timeout(struct fw_priv *fw_priv, long timeout)
69{
70 return __fw_state_wait_common(fw_priv, timeout);
71}
72
73struct fw_sysfs {
74 bool nowait;
75 struct device dev;
76 struct fw_priv *fw_priv;
77 struct firmware *fw;
78};
79
80static struct fw_sysfs *to_fw_sysfs(struct device *dev)
81{
82 return container_of(dev, struct fw_sysfs, dev);
83}
84
85static void __fw_load_abort(struct fw_priv *fw_priv)
86{
87 /*
88 * There is a small window in which user can write to 'loading'
89 * between loading done and disappearance of 'loading'
90 */
91 if (fw_sysfs_done(fw_priv))
92 return;
93
94 list_del_init(&fw_priv->pending_list);
95 fw_state_aborted(fw_priv);
96}
97
98static void fw_load_abort(struct fw_sysfs *fw_sysfs)
99{
100 struct fw_priv *fw_priv = fw_sysfs->fw_priv;
101
102 __fw_load_abort(fw_priv);
103}
104
105static LIST_HEAD(pending_fw_head);
106
107void kill_pending_fw_fallback_reqs(bool only_kill_custom)
108{
109 struct fw_priv *fw_priv;
110 struct fw_priv *next;
111
112 mutex_lock(&fw_lock);
113 list_for_each_entry_safe(fw_priv, next, &pending_fw_head,
114 pending_list) {
115 if (!fw_priv->need_uevent || !only_kill_custom)
116 __fw_load_abort(fw_priv);
117 }
118 mutex_unlock(&fw_lock);
119}
120
121static ssize_t timeout_show(struct class *class, struct class_attribute *attr,
122 char *buf)
123{
124 return sprintf(buf, "%d\n", __firmware_loading_timeout());
125}
126
127/**
Andres Rodriguezc35f9cbb2018-05-10 13:08:38 -0700128 * firmware_timeout_store() - set number of seconds to wait for firmware
Luis R. Rodriguezd73f8212018-03-10 06:14:49 -0800129 * @class: device class pointer
130 * @attr: device attribute pointer
131 * @buf: buffer to scan for timeout value
132 * @count: number of bytes in @buf
133 *
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.
137 *
138 * Note: zero means 'wait forever'.
139 **/
140static ssize_t timeout_store(struct class *class, struct class_attribute *attr,
141 const char *buf, size_t count)
142{
143 int tmp_loading_timeout = simple_strtol(buf, NULL, 10);
144
145 if (tmp_loading_timeout < 0)
146 tmp_loading_timeout = 0;
147
148 __fw_fallback_set_timeout(tmp_loading_timeout);
149
150 return count;
151}
152static CLASS_ATTR_RW(timeout);
153
154static struct attribute *firmware_class_attrs[] = {
155 &class_attr_timeout.attr,
156 NULL,
157};
158ATTRIBUTE_GROUPS(firmware_class);
159
160static void fw_dev_release(struct device *dev)
161{
162 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
163
164 kfree(fw_sysfs);
165}
166
167static int do_firmware_uevent(struct fw_sysfs *fw_sysfs, struct kobj_uevent_env *env)
168{
169 if (add_uevent_var(env, "FIRMWARE=%s", fw_sysfs->fw_priv->fw_name))
170 return -ENOMEM;
171 if (add_uevent_var(env, "TIMEOUT=%i", __firmware_loading_timeout()))
172 return -ENOMEM;
173 if (add_uevent_var(env, "ASYNC=%d", fw_sysfs->nowait))
174 return -ENOMEM;
175
176 return 0;
177}
178
179static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
180{
181 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
182 int err = 0;
183
184 mutex_lock(&fw_lock);
185 if (fw_sysfs->fw_priv)
186 err = do_firmware_uevent(fw_sysfs, env);
187 mutex_unlock(&fw_lock);
188 return err;
189}
190
191static struct class firmware_class = {
192 .name = "firmware",
193 .class_groups = firmware_class_groups,
194 .dev_uevent = firmware_uevent,
195 .dev_release = fw_dev_release,
196};
197
198int register_sysfs_loader(void)
199{
200 return class_register(&firmware_class);
201}
202
203void unregister_sysfs_loader(void)
204{
205 class_unregister(&firmware_class);
206}
207
208static ssize_t firmware_loading_show(struct device *dev,
209 struct device_attribute *attr, char *buf)
210{
211 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
212 int loading = 0;
213
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);
218
219 return sprintf(buf, "%d\n", loading);
220}
221
Luis R. Rodriguezd73f8212018-03-10 06:14:49 -0800222/* one pages buffer should be mapped/unmapped only once */
223static int map_fw_priv_pages(struct fw_priv *fw_priv)
224{
Takashi Iwaiddaf29f2019-05-20 11:26:43 +0200225 if (!fw_priv->pages)
Luis R. Rodriguezd73f8212018-03-10 06:14:49 -0800226 return 0;
227
228 vunmap(fw_priv->data);
229 fw_priv->data = vmap(fw_priv->pages, fw_priv->nr_pages, 0,
230 PAGE_KERNEL_RO);
231 if (!fw_priv->data)
232 return -ENOMEM;
Takashi Iwaiddaf29f2019-05-20 11:26:43 +0200233
234 /* page table is no longer needed after mapping, let's free */
235 vfree(fw_priv->pages);
236 fw_priv->pages = NULL;
237
Luis R. Rodriguezd73f8212018-03-10 06:14:49 -0800238 return 0;
239}
240
241/**
Andres Rodriguezc35f9cbb2018-05-10 13:08:38 -0700242 * firmware_loading_store() - set value in the 'loading' control file
Luis R. Rodriguezd73f8212018-03-10 06:14:49 -0800243 * @dev: device pointer
244 * @attr: device attribute pointer
245 * @buf: buffer to scan for loading control value
246 * @count: number of bytes in @buf
247 *
248 * The relevant values are:
249 *
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.
253 **/
254static ssize_t firmware_loading_store(struct device *dev,
255 struct device_attribute *attr,
256 const char *buf, size_t count)
257{
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);
Luis R. Rodriguezd73f8212018-03-10 06:14:49 -0800262
263 mutex_lock(&fw_lock);
264 fw_priv = fw_sysfs->fw_priv;
265 if (fw_state_is_aborted(fw_priv))
266 goto out;
267
268 switch (loading) {
269 case 1:
270 /* discarding any previous partial load */
271 if (!fw_sysfs_done(fw_priv)) {
Takashi Iwai8f585702019-05-20 11:26:44 +0200272 fw_free_paged_buf(fw_priv);
Luis R. Rodriguezd73f8212018-03-10 06:14:49 -0800273 fw_state_start(fw_priv);
274 }
275 break;
276 case 0:
277 if (fw_sysfs_loading(fw_priv)) {
278 int rc;
279
280 /*
281 * Several loading requests may be pending on
282 * one same firmware buf, so let all requests
283 * see the mapped 'buf->data' once the loading
284 * is completed.
285 * */
286 rc = map_fw_priv_pages(fw_priv);
287 if (rc)
288 dev_err(dev, "%s: map pages failed\n",
289 __func__);
290 else
291 rc = security_kernel_post_read_file(NULL,
292 fw_priv->data, fw_priv->size,
293 READING_FIRMWARE);
294
295 /*
296 * Same logic as fw_load_abort, only the DONE bit
297 * is ignored and we set ABORT only on failure.
298 */
299 list_del_init(&fw_priv->pending_list);
300 if (rc) {
301 fw_state_aborted(fw_priv);
302 written = rc;
303 } else {
304 fw_state_done(fw_priv);
305 }
306 break;
307 }
308 /* fallthrough */
309 default:
310 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
311 /* fallthrough */
312 case -1:
313 fw_load_abort(fw_sysfs);
314 break;
315 }
316out:
317 mutex_unlock(&fw_lock);
318 return written;
319}
320
321static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
322
323static void firmware_rw_data(struct fw_priv *fw_priv, char *buffer,
324 loff_t offset, size_t count, bool read)
325{
326 if (read)
327 memcpy(buffer, fw_priv->data + offset, count);
328 else
329 memcpy(fw_priv->data + offset, buffer, count);
330}
331
332static void firmware_rw(struct fw_priv *fw_priv, char *buffer,
333 loff_t offset, size_t count, bool read)
334{
335 while (count) {
336 void *page_data;
337 int page_nr = offset >> PAGE_SHIFT;
338 int page_ofs = offset & (PAGE_SIZE-1);
339 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
340
341 page_data = kmap(fw_priv->pages[page_nr]);
342
343 if (read)
344 memcpy(buffer, page_data + page_ofs, page_cnt);
345 else
346 memcpy(page_data + page_ofs, buffer, page_cnt);
347
348 kunmap(fw_priv->pages[page_nr]);
349 buffer += page_cnt;
350 offset += page_cnt;
351 count -= page_cnt;
352 }
353}
354
355static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
356 struct bin_attribute *bin_attr,
357 char *buffer, loff_t offset, size_t count)
358{
359 struct device *dev = kobj_to_dev(kobj);
360 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
361 struct fw_priv *fw_priv;
362 ssize_t ret_count;
363
364 mutex_lock(&fw_lock);
365 fw_priv = fw_sysfs->fw_priv;
366 if (!fw_priv || fw_sysfs_done(fw_priv)) {
367 ret_count = -ENODEV;
368 goto out;
369 }
370 if (offset > fw_priv->size) {
371 ret_count = 0;
372 goto out;
373 }
374 if (count > fw_priv->size - offset)
375 count = fw_priv->size - offset;
376
377 ret_count = count;
378
379 if (fw_priv->data)
380 firmware_rw_data(fw_priv, buffer, offset, count, true);
381 else
382 firmware_rw(fw_priv, buffer, offset, count, true);
383
384out:
385 mutex_unlock(&fw_lock);
386 return ret_count;
387}
388
389static int fw_realloc_pages(struct fw_sysfs *fw_sysfs, int min_size)
390{
391 struct fw_priv *fw_priv= fw_sysfs->fw_priv;
392 int pages_needed = PAGE_ALIGN(min_size) >> PAGE_SHIFT;
393
394 /* If the array of pages is too small, grow it... */
395 if (fw_priv->page_array_size < pages_needed) {
396 int new_array_size = max(pages_needed,
397 fw_priv->page_array_size * 2);
398 struct page **new_pages;
399
Kees Cook42bc47b2018-06-12 14:27:11 -0700400 new_pages = vmalloc(array_size(new_array_size, sizeof(void *)));
Luis R. Rodriguezd73f8212018-03-10 06:14:49 -0800401 if (!new_pages) {
402 fw_load_abort(fw_sysfs);
403 return -ENOMEM;
404 }
405 memcpy(new_pages, fw_priv->pages,
406 fw_priv->page_array_size * sizeof(void *));
407 memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) *
408 (new_array_size - fw_priv->page_array_size));
409 vfree(fw_priv->pages);
410 fw_priv->pages = new_pages;
411 fw_priv->page_array_size = new_array_size;
412 }
413
414 while (fw_priv->nr_pages < pages_needed) {
415 fw_priv->pages[fw_priv->nr_pages] =
416 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
417
418 if (!fw_priv->pages[fw_priv->nr_pages]) {
419 fw_load_abort(fw_sysfs);
420 return -ENOMEM;
421 }
422 fw_priv->nr_pages++;
423 }
424 return 0;
425}
426
427/**
Andres Rodriguezc35f9cbb2018-05-10 13:08:38 -0700428 * firmware_data_write() - write method for firmware
Luis R. Rodriguezd73f8212018-03-10 06:14:49 -0800429 * @filp: open sysfs file
430 * @kobj: kobject for the device
431 * @bin_attr: bin_attr structure
432 * @buffer: buffer being written
433 * @offset: buffer offset for write in total data store area
434 * @count: buffer size
435 *
436 * Data written to the 'data' attribute will be later handed to
437 * the driver as a firmware image.
438 **/
439static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
440 struct bin_attribute *bin_attr,
441 char *buffer, loff_t offset, size_t count)
442{
443 struct device *dev = kobj_to_dev(kobj);
444 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
445 struct fw_priv *fw_priv;
446 ssize_t retval;
447
448 if (!capable(CAP_SYS_RAWIO))
449 return -EPERM;
450
451 mutex_lock(&fw_lock);
452 fw_priv = fw_sysfs->fw_priv;
453 if (!fw_priv || fw_sysfs_done(fw_priv)) {
454 retval = -ENODEV;
455 goto out;
456 }
457
458 if (fw_priv->data) {
459 if (offset + count > fw_priv->allocated_size) {
460 retval = -ENOMEM;
461 goto out;
462 }
463 firmware_rw_data(fw_priv, buffer, offset, count, false);
464 retval = count;
465 } else {
466 retval = fw_realloc_pages(fw_sysfs, offset + count);
467 if (retval)
468 goto out;
469
470 retval = count;
471 firmware_rw(fw_priv, buffer, offset, count, false);
472 }
473
474 fw_priv->size = max_t(size_t, offset + count, fw_priv->size);
475out:
476 mutex_unlock(&fw_lock);
477 return retval;
478}
479
480static struct bin_attribute firmware_attr_data = {
481 .attr = { .name = "data", .mode = 0644 },
482 .size = 0,
483 .read = firmware_data_read,
484 .write = firmware_data_write,
485};
486
487static struct attribute *fw_dev_attrs[] = {
488 &dev_attr_loading.attr,
489 NULL
490};
491
492static struct bin_attribute *fw_dev_bin_attrs[] = {
493 &firmware_attr_data,
494 NULL
495};
496
497static const struct attribute_group fw_dev_attr_group = {
498 .attrs = fw_dev_attrs,
499 .bin_attrs = fw_dev_bin_attrs,
500};
501
502static const struct attribute_group *fw_dev_attr_groups[] = {
503 &fw_dev_attr_group,
504 NULL
505};
506
507static struct fw_sysfs *
508fw_create_instance(struct firmware *firmware, const char *fw_name,
Andres Rodriguezeb33eb02018-05-10 13:08:37 -0700509 struct device *device, enum fw_opt opt_flags)
Luis R. Rodriguezd73f8212018-03-10 06:14:49 -0800510{
511 struct fw_sysfs *fw_sysfs;
512 struct device *f_dev;
513
514 fw_sysfs = kzalloc(sizeof(*fw_sysfs), GFP_KERNEL);
515 if (!fw_sysfs) {
516 fw_sysfs = ERR_PTR(-ENOMEM);
517 goto exit;
518 }
519
520 fw_sysfs->nowait = !!(opt_flags & FW_OPT_NOWAIT);
521 fw_sysfs->fw = firmware;
522 f_dev = &fw_sysfs->dev;
523
524 device_initialize(f_dev);
525 dev_set_name(f_dev, "%s", fw_name);
526 f_dev->parent = device;
527 f_dev->class = &firmware_class;
528 f_dev->groups = fw_dev_attr_groups;
529exit:
530 return fw_sysfs;
531}
532
Luis R. Rodriguez60fa7422018-03-10 06:14:55 -0800533/**
Andres Rodriguezc35f9cbb2018-05-10 13:08:38 -0700534 * fw_load_sysfs_fallback() - load a firmware via the sysfs fallback mechanism
Andres Rodriguezb93815d2018-04-25 12:25:39 -0400535 * @fw_sysfs: firmware sysfs information for the firmware to load
Luis R. Rodriguez60fa7422018-03-10 06:14:55 -0800536 * @opt_flags: flags of options, FW_OPT_*
537 * @timeout: timeout to wait for the load
538 *
539 * In charge of constructing a sysfs fallback interface for firmware loading.
540 **/
541static int fw_load_sysfs_fallback(struct fw_sysfs *fw_sysfs,
Andres Rodriguezeb33eb02018-05-10 13:08:37 -0700542 enum fw_opt opt_flags, long timeout)
Luis R. Rodriguezd73f8212018-03-10 06:14:49 -0800543{
544 int retval = 0;
545 struct device *f_dev = &fw_sysfs->dev;
546 struct fw_priv *fw_priv = fw_sysfs->fw_priv;
547
548 /* fall back on userspace loading */
549 if (!fw_priv->data)
550 fw_priv->is_paged_buf = true;
551
552 dev_set_uevent_suppress(f_dev, true);
553
554 retval = device_add(f_dev);
555 if (retval) {
556 dev_err(f_dev, "%s: device_register failed\n", __func__);
557 goto err_put_dev;
558 }
559
560 mutex_lock(&fw_lock);
561 list_add(&fw_priv->pending_list, &pending_fw_head);
562 mutex_unlock(&fw_lock);
563
564 if (opt_flags & FW_OPT_UEVENT) {
565 fw_priv->need_uevent = true;
566 dev_set_uevent_suppress(f_dev, false);
567 dev_dbg(f_dev, "firmware: requesting %s\n", fw_priv->fw_name);
568 kobject_uevent(&fw_sysfs->dev.kobj, KOBJ_ADD);
569 } else {
570 timeout = MAX_JIFFY_OFFSET;
571 }
572
573 retval = fw_sysfs_wait_timeout(fw_priv, timeout);
574 if (retval < 0) {
575 mutex_lock(&fw_lock);
576 fw_load_abort(fw_sysfs);
577 mutex_unlock(&fw_lock);
578 }
579
580 if (fw_state_is_aborted(fw_priv)) {
581 if (retval == -ERESTARTSYS)
582 retval = -EINTR;
583 else
584 retval = -EAGAIN;
585 } else if (fw_priv->is_paged_buf && !fw_priv->data)
586 retval = -ENOMEM;
587
588 device_del(f_dev);
589err_put_dev:
590 put_device(f_dev);
591 return retval;
592}
593
594static int fw_load_from_user_helper(struct firmware *firmware,
595 const char *name, struct device *device,
Andres Rodriguezeb33eb02018-05-10 13:08:37 -0700596 enum fw_opt opt_flags)
Luis R. Rodriguezd73f8212018-03-10 06:14:49 -0800597{
598 struct fw_sysfs *fw_sysfs;
599 long timeout;
600 int ret;
601
602 timeout = firmware_loading_timeout();
603 if (opt_flags & FW_OPT_NOWAIT) {
604 timeout = usermodehelper_read_lock_wait(timeout);
605 if (!timeout) {
606 dev_dbg(device, "firmware: %s loading timed out\n",
607 name);
608 return -EBUSY;
609 }
610 } else {
611 ret = usermodehelper_read_trylock();
612 if (WARN_ON(ret)) {
613 dev_err(device, "firmware: %s will not be loaded\n",
614 name);
615 return ret;
616 }
617 }
618
619 fw_sysfs = fw_create_instance(firmware, name, device, opt_flags);
620 if (IS_ERR(fw_sysfs)) {
621 ret = PTR_ERR(fw_sysfs);
622 goto out_unlock;
623 }
624
625 fw_sysfs->fw_priv = firmware->priv;
Luis R. Rodriguez60fa7422018-03-10 06:14:55 -0800626 ret = fw_load_sysfs_fallback(fw_sysfs, opt_flags, timeout);
Luis R. Rodriguezd73f8212018-03-10 06:14:49 -0800627
628 if (!ret)
629 ret = assign_fw(firmware, device, opt_flags);
630
631out_unlock:
632 usermodehelper_read_unlock();
633
634 return ret;
635}
636
Andres Rodriguezeb33eb02018-05-10 13:08:37 -0700637static bool fw_force_sysfs_fallback(enum fw_opt opt_flags)
Luis R. Rodriguezd73f8212018-03-10 06:14:49 -0800638{
639 if (fw_fallback_config.force_sysfs_fallback)
640 return true;
641 if (!(opt_flags & FW_OPT_USERHELPER))
642 return false;
643 return true;
644}
645
Andres Rodriguezeb33eb02018-05-10 13:08:37 -0700646static bool fw_run_sysfs_fallback(enum fw_opt opt_flags)
Luis R. Rodriguezd73f8212018-03-10 06:14:49 -0800647{
Mimi Zohar6e8526512018-07-13 14:05:59 -0400648 int ret;
649
Luis R. Rodriguez2cd7a1c2018-03-10 06:14:52 -0800650 if (fw_fallback_config.ignore_sysfs_fallback) {
Luis R. Rodriguezc6263a42018-03-21 15:34:28 -0700651 pr_info_once("Ignoring firmware sysfs fallback due to sysctl knob\n");
Luis R. Rodriguez2cd7a1c2018-03-10 06:14:52 -0800652 return false;
653 }
654
Luis R. Rodriguezd73f8212018-03-10 06:14:49 -0800655 if ((opt_flags & FW_OPT_NOFALLBACK))
656 return false;
657
Mimi Zohar6e8526512018-07-13 14:05:59 -0400658 /* Also permit LSMs and IMA to fail firmware sysfs fallback */
659 ret = security_kernel_load_data(LOADING_FIRMWARE);
660 if (ret < 0)
661 return ret;
662
Luis R. Rodriguezd73f8212018-03-10 06:14:49 -0800663 return fw_force_sysfs_fallback(opt_flags);
664}
665
Luis R. Rodriguez84b5c4f2018-05-10 13:08:40 -0700666/**
667 * firmware_fallback_sysfs() - use the fallback mechanism to find firmware
668 * @fw: pointer to firmware image
669 * @name: name of firmware file to look for
670 * @device: device for which firmware is being loaded
671 * @opt_flags: options to control firmware loading behaviour
672 * @ret: return value from direct lookup which triggered the fallback mechanism
673 *
674 * This function is called if direct lookup for the firmware failed, it enables
675 * a fallback mechanism through userspace by exposing a sysfs loading
Jonathan Neuschäferbbabc3f2019-04-30 16:56:10 +0200676 * interface. Userspace is in charge of loading the firmware through the sysfs
677 * loading interface. This sysfs fallback mechanism may be disabled completely
Luis R. Rodriguez84b5c4f2018-05-10 13:08:40 -0700678 * on a system by setting the proc sysctl value ignore_sysfs_fallback to true.
679 * If this false we check if the internal API caller set the @FW_OPT_NOFALLBACK
680 * flag, if so it would also disable the fallback mechanism. A system may want
681 * to enfoce the sysfs fallback mechanism at all times, it can do this by
682 * setting ignore_sysfs_fallback to false and force_sysfs_fallback to true.
683 * Enabling force_sysfs_fallback is functionally equivalent to build a kernel
684 * with CONFIG_FW_LOADER_USER_HELPER_FALLBACK.
685 **/
Andres Rodriguezcf1cde72018-05-10 13:08:39 -0700686int firmware_fallback_sysfs(struct firmware *fw, const char *name,
687 struct device *device,
688 enum fw_opt opt_flags,
689 int ret)
Luis R. Rodriguezd73f8212018-03-10 06:14:49 -0800690{
691 if (!fw_run_sysfs_fallback(opt_flags))
692 return ret;
693
Luis R. Rodriguez27d5d7d2018-05-10 13:08:44 -0700694 if (!(opt_flags & FW_OPT_NO_WARN))
Jonathan Neuschäferbbabc3f2019-04-30 16:56:10 +0200695 dev_warn(device, "Falling back to sysfs fallback for: %s\n",
Luis R. Rodriguez27d5d7d2018-05-10 13:08:44 -0700696 name);
697 else
698 dev_dbg(device, "Falling back to sysfs fallback for: %s\n",
699 name);
Luis R. Rodriguezd73f8212018-03-10 06:14:49 -0800700 return fw_load_from_user_helper(fw, name, device, opt_flags);
701}