1
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>
10#include <linux/sysctl.h>
11#include <linux/vmalloc.h>
12
13#include "fallback.h"
14#include "firmware.h"
15
16
17
18
19
20extern struct firmware_fallback_config fw_fallback_config;
21
22
23static inline int __firmware_loading_timeout(void)
24{
25 return fw_fallback_config.loading_timeout;
26}
27
28
29static void __fw_fallback_set_timeout(int timeout)
30{
31 fw_fallback_config.loading_timeout = timeout;
32}
33
34
35
36
37
38
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
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
89
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
128
129
130
131
132
133
134
135
136
137
138
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
222
223static int map_fw_priv_pages(struct fw_priv *fw_priv)
224{
225 if (!fw_priv->is_paged_buf)
226 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;
233 return 0;
234}
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249static ssize_t firmware_loading_store(struct device *dev,
250 struct device_attribute *attr,
251 const char *buf, size_t count)
252{
253 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
254 struct fw_priv *fw_priv;
255 ssize_t written = count;
256 int loading = simple_strtol(buf, NULL, 10);
257 int i;
258
259 mutex_lock(&fw_lock);
260 fw_priv = fw_sysfs->fw_priv;
261 if (fw_state_is_aborted(fw_priv))
262 goto out;
263
264 switch (loading) {
265 case 1:
266
267 if (!fw_sysfs_done(fw_priv)) {
268 for (i = 0; i < fw_priv->nr_pages; i++)
269 __free_page(fw_priv->pages[i]);
270 vfree(fw_priv->pages);
271 fw_priv->pages = NULL;
272 fw_priv->page_array_size = 0;
273 fw_priv->nr_pages = 0;
274 fw_state_start(fw_priv);
275 }
276 break;
277 case 0:
278 if (fw_sysfs_loading(fw_priv)) {
279 int rc;
280
281
282
283
284
285
286
287 rc = map_fw_priv_pages(fw_priv);
288 if (rc)
289 dev_err(dev, "%s: map pages failed\n",
290 __func__);
291 else
292 rc = security_kernel_post_read_file(NULL,
293 fw_priv->data, fw_priv->size,
294 READING_FIRMWARE);
295
296
297
298
299
300 list_del_init(&fw_priv->pending_list);
301 if (rc) {
302 fw_state_aborted(fw_priv);
303 written = rc;
304 } else {
305 fw_state_done(fw_priv);
306 }
307 break;
308 }
309
310 default:
311 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
312
313 case -1:
314 fw_load_abort(fw_sysfs);
315 break;
316 }
317out:
318 mutex_unlock(&fw_lock);
319 return written;
320}
321
322static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
323
324static void firmware_rw_data(struct fw_priv *fw_priv, char *buffer,
325 loff_t offset, size_t count, bool read)
326{
327 if (read)
328 memcpy(buffer, fw_priv->data + offset, count);
329 else
330 memcpy(fw_priv->data + offset, buffer, count);
331}
332
333static void firmware_rw(struct fw_priv *fw_priv, char *buffer,
334 loff_t offset, size_t count, bool read)
335{
336 while (count) {
337 void *page_data;
338 int page_nr = offset >> PAGE_SHIFT;
339 int page_ofs = offset & (PAGE_SIZE-1);
340 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
341
342 page_data = kmap(fw_priv->pages[page_nr]);
343
344 if (read)
345 memcpy(buffer, page_data + page_ofs, page_cnt);
346 else
347 memcpy(page_data + page_ofs, buffer, page_cnt);
348
349 kunmap(fw_priv->pages[page_nr]);
350 buffer += page_cnt;
351 offset += page_cnt;
352 count -= page_cnt;
353 }
354}
355
356static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
357 struct bin_attribute *bin_attr,
358 char *buffer, loff_t offset, size_t count)
359{
360 struct device *dev = kobj_to_dev(kobj);
361 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
362 struct fw_priv *fw_priv;
363 ssize_t ret_count;
364
365 mutex_lock(&fw_lock);
366 fw_priv = fw_sysfs->fw_priv;
367 if (!fw_priv || fw_sysfs_done(fw_priv)) {
368 ret_count = -ENODEV;
369 goto out;
370 }
371 if (offset > fw_priv->size) {
372 ret_count = 0;
373 goto out;
374 }
375 if (count > fw_priv->size - offset)
376 count = fw_priv->size - offset;
377
378 ret_count = count;
379
380 if (fw_priv->data)
381 firmware_rw_data(fw_priv, buffer, offset, count, true);
382 else
383 firmware_rw(fw_priv, buffer, offset, count, true);
384
385out:
386 mutex_unlock(&fw_lock);
387 return ret_count;
388}
389
390static int fw_realloc_pages(struct fw_sysfs *fw_sysfs, int min_size)
391{
392 struct fw_priv *fw_priv= fw_sysfs->fw_priv;
393 int pages_needed = PAGE_ALIGN(min_size) >> PAGE_SHIFT;
394
395
396 if (fw_priv->page_array_size < pages_needed) {
397 int new_array_size = max(pages_needed,
398 fw_priv->page_array_size * 2);
399 struct page **new_pages;
400
401 new_pages = vmalloc(array_size(new_array_size, sizeof(void *)));
402 if (!new_pages) {
403 fw_load_abort(fw_sysfs);
404 return -ENOMEM;
405 }
406 memcpy(new_pages, fw_priv->pages,
407 fw_priv->page_array_size * sizeof(void *));
408 memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) *
409 (new_array_size - fw_priv->page_array_size));
410 vfree(fw_priv->pages);
411 fw_priv->pages = new_pages;
412 fw_priv->page_array_size = new_array_size;
413 }
414
415 while (fw_priv->nr_pages < pages_needed) {
416 fw_priv->pages[fw_priv->nr_pages] =
417 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
418
419 if (!fw_priv->pages[fw_priv->nr_pages]) {
420 fw_load_abort(fw_sysfs);
421 return -ENOMEM;
422 }
423 fw_priv->nr_pages++;
424 }
425 return 0;
426}
427
428
429
430
431
432
433
434
435
436
437
438
439
440static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
441 struct bin_attribute *bin_attr,
442 char *buffer, loff_t offset, size_t count)
443{
444 struct device *dev = kobj_to_dev(kobj);
445 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
446 struct fw_priv *fw_priv;
447 ssize_t retval;
448
449 if (!capable(CAP_SYS_RAWIO))
450 return -EPERM;
451
452 mutex_lock(&fw_lock);
453 fw_priv = fw_sysfs->fw_priv;
454 if (!fw_priv || fw_sysfs_done(fw_priv)) {
455 retval = -ENODEV;
456 goto out;
457 }
458
459 if (fw_priv->data) {
460 if (offset + count > fw_priv->allocated_size) {
461 retval = -ENOMEM;
462 goto out;
463 }
464 firmware_rw_data(fw_priv, buffer, offset, count, false);
465 retval = count;
466 } else {
467 retval = fw_realloc_pages(fw_sysfs, offset + count);
468 if (retval)
469 goto out;
470
471 retval = count;
472 firmware_rw(fw_priv, buffer, offset, count, false);
473 }
474
475 fw_priv->size = max_t(size_t, offset + count, fw_priv->size);
476out:
477 mutex_unlock(&fw_lock);
478 return retval;
479}
480
481static struct bin_attribute firmware_attr_data = {
482 .attr = { .name = "data", .mode = 0644 },
483 .size = 0,
484 .read = firmware_data_read,
485 .write = firmware_data_write,
486};
487
488static struct attribute *fw_dev_attrs[] = {
489 &dev_attr_loading.attr,
490 NULL
491};
492
493static struct bin_attribute *fw_dev_bin_attrs[] = {
494 &firmware_attr_data,
495 NULL
496};
497
498static const struct attribute_group fw_dev_attr_group = {
499 .attrs = fw_dev_attrs,
500 .bin_attrs = fw_dev_bin_attrs,
501};
502
503static const struct attribute_group *fw_dev_attr_groups[] = {
504 &fw_dev_attr_group,
505 NULL
506};
507
508static struct fw_sysfs *
509fw_create_instance(struct firmware *firmware, const char *fw_name,
510 struct device *device, enum fw_opt opt_flags)
511{
512 struct fw_sysfs *fw_sysfs;
513 struct device *f_dev;
514
515 fw_sysfs = kzalloc(sizeof(*fw_sysfs), GFP_KERNEL);
516 if (!fw_sysfs) {
517 fw_sysfs = ERR_PTR(-ENOMEM);
518 goto exit;
519 }
520
521 fw_sysfs->nowait = !!(opt_flags & FW_OPT_NOWAIT);
522 fw_sysfs->fw = firmware;
523 f_dev = &fw_sysfs->dev;
524
525 device_initialize(f_dev);
526 dev_set_name(f_dev, "%s", fw_name);
527 f_dev->parent = device;
528 f_dev->class = &firmware_class;
529 f_dev->groups = fw_dev_attr_groups;
530exit:
531 return fw_sysfs;
532}
533
534
535
536
537
538
539
540
541
542static int fw_load_sysfs_fallback(struct fw_sysfs *fw_sysfs,
543 enum fw_opt opt_flags, long timeout)
544{
545 int retval = 0;
546 struct device *f_dev = &fw_sysfs->dev;
547 struct fw_priv *fw_priv = fw_sysfs->fw_priv;
548
549
550 if (!fw_priv->data)
551 fw_priv->is_paged_buf = true;
552
553 dev_set_uevent_suppress(f_dev, true);
554
555 retval = device_add(f_dev);
556 if (retval) {
557 dev_err(f_dev, "%s: device_register failed\n", __func__);
558 goto err_put_dev;
559 }
560
561 mutex_lock(&fw_lock);
562 list_add(&fw_priv->pending_list, &pending_fw_head);
563 mutex_unlock(&fw_lock);
564
565 if (opt_flags & FW_OPT_UEVENT) {
566 fw_priv->need_uevent = true;
567 dev_set_uevent_suppress(f_dev, false);
568 dev_dbg(f_dev, "firmware: requesting %s\n", fw_priv->fw_name);
569 kobject_uevent(&fw_sysfs->dev.kobj, KOBJ_ADD);
570 } else {
571 timeout = MAX_JIFFY_OFFSET;
572 }
573
574 retval = fw_sysfs_wait_timeout(fw_priv, timeout);
575 if (retval < 0) {
576 mutex_lock(&fw_lock);
577 fw_load_abort(fw_sysfs);
578 mutex_unlock(&fw_lock);
579 }
580
581 if (fw_state_is_aborted(fw_priv)) {
582 if (retval == -ERESTARTSYS)
583 retval = -EINTR;
584 else
585 retval = -EAGAIN;
586 } else if (fw_priv->is_paged_buf && !fw_priv->data)
587 retval = -ENOMEM;
588
589 device_del(f_dev);
590err_put_dev:
591 put_device(f_dev);
592 return retval;
593}
594
595static int fw_load_from_user_helper(struct firmware *firmware,
596 const char *name, struct device *device,
597 enum fw_opt opt_flags)
598{
599 struct fw_sysfs *fw_sysfs;
600 long timeout;
601 int ret;
602
603 timeout = firmware_loading_timeout();
604 if (opt_flags & FW_OPT_NOWAIT) {
605 timeout = usermodehelper_read_lock_wait(timeout);
606 if (!timeout) {
607 dev_dbg(device, "firmware: %s loading timed out\n",
608 name);
609 return -EBUSY;
610 }
611 } else {
612 ret = usermodehelper_read_trylock();
613 if (WARN_ON(ret)) {
614 dev_err(device, "firmware: %s will not be loaded\n",
615 name);
616 return ret;
617 }
618 }
619
620 fw_sysfs = fw_create_instance(firmware, name, device, opt_flags);
621 if (IS_ERR(fw_sysfs)) {
622 ret = PTR_ERR(fw_sysfs);
623 goto out_unlock;
624 }
625
626 fw_sysfs->fw_priv = firmware->priv;
627 ret = fw_load_sysfs_fallback(fw_sysfs, opt_flags, timeout);
628
629 if (!ret)
630 ret = assign_fw(firmware, device, opt_flags);
631
632out_unlock:
633 usermodehelper_read_unlock();
634
635 return ret;
636}
637
638static bool fw_force_sysfs_fallback(enum fw_opt opt_flags)
639{
640 if (fw_fallback_config.force_sysfs_fallback)
641 return true;
642 if (!(opt_flags & FW_OPT_USERHELPER))
643 return false;
644 return true;
645}
646
647static bool fw_run_sysfs_fallback(enum fw_opt opt_flags)
648{
649 int ret;
650
651 if (fw_fallback_config.ignore_sysfs_fallback) {
652 pr_info_once("Ignoring firmware sysfs fallback due to sysctl knob\n");
653 return false;
654 }
655
656 if ((opt_flags & FW_OPT_NOFALLBACK))
657 return false;
658
659
660 ret = security_kernel_load_data(LOADING_FIRMWARE);
661 if (ret < 0)
662 return ret;
663
664 return fw_force_sysfs_fallback(opt_flags);
665}
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687int firmware_fallback_sysfs(struct firmware *fw, const char *name,
688 struct device *device,
689 enum fw_opt opt_flags,
690 int ret)
691{
692 if (!fw_run_sysfs_fallback(opt_flags))
693 return ret;
694
695 if (!(opt_flags & FW_OPT_NO_WARN))
696 dev_warn(device, "Falling back to sysfs fallback for: %s\n",
697 name);
698 else
699 dev_dbg(device, "Falling back to sysfs fallback for: %s\n",
700 name);
701 return fw_load_from_user_helper(fw, name, device, opt_flags);
702}
703