2 * This module provides an interface to trigger and test firmware loading.
4 * It is designed to be used for basic evaluation of the firmware loading
5 * subsystem (for example when validating firmware verification). It lacks
6 * any extra dependencies, and will not normally be loaded by the system
7 * unless explicitly requested by name.
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/printk.h>
15 #include <linux/completion.h>
16 #include <linux/firmware.h>
17 #include <linux/device.h>
19 #include <linux/miscdevice.h>
20 #include <linux/slab.h>
21 #include <linux/uaccess.h>
22 #include <linux/delay.h>
23 #include <linux/kthread.h>
25 #define TEST_FIRMWARE_NAME "test-firmware.bin"
26 #define TEST_FIRMWARE_NUM_REQS 4
28 static DEFINE_MUTEX(test_fw_mutex
);
29 static const struct firmware
*test_firmware
;
31 struct test_batched_req
{
35 const struct firmware
*fw
;
37 struct completion completion
;
38 struct task_struct
*task
;
43 * test_config - represents configuration for the test for different triggers
45 * @name: the name of the firmware file to look for
46 * @sync_direct: when the sync trigger is used if this is true
47 * request_firmware_direct() will be used instead.
48 * @send_uevent: whether or not to send a uevent for async requests
49 * @num_requests: number of requests to try per test case. This is trigger
51 * @reqs: stores all requests information
52 * @read_fw_idx: index of thread from which we want to read firmware results
53 * from through the read_fw trigger.
54 * @test_result: a test may use this to collect the result from the call
55 * of the request_firmware*() calls used in their tests. In order of
56 * priority we always keep first any setup error. If no setup errors were
57 * found then we move on to the first error encountered while running the
58 * API. Note that for async calls this typically will be a successful
59 * result (0) unless of course you've used bogus parameters, or the system
60 * is out of memory. In the async case the callback is expected to do a
61 * bit more homework to figure out what happened, unfortunately the only
62 * information passed today on error is the fact that no firmware was
63 * found so we can only assume -ENOENT on async calls if the firmware is
66 * Errors you can expect:
70 * 0: success for sync, for async it means request was sent
71 * -EINVAL: invalid parameters or request
72 * -ENOENT: files not found
76 * -ENOMEM: memory pressure on system
77 * -ENODEV: out of number of devices to test
78 * -EINVAL: an unexpected error has occurred
79 * @req_firmware: if @sync_direct is true this is set to
80 * request_firmware_direct(), otherwise request_firmware()
90 * These below don't belong her but we'll move them once we create
91 * a struct fw_test_device and stuff the misc_dev under there later.
93 struct test_batched_req
*reqs
;
95 int (*req_firmware
)(const struct firmware
**fw
, const char *name
,
96 struct device
*device
);
99 static struct test_config
*test_fw_config
;
101 static ssize_t
test_fw_misc_read(struct file
*f
, char __user
*buf
,
102 size_t size
, loff_t
*offset
)
106 mutex_lock(&test_fw_mutex
);
108 rc
= simple_read_from_buffer(buf
, size
, offset
,
110 test_firmware
->size
);
111 mutex_unlock(&test_fw_mutex
);
115 static const struct file_operations test_fw_fops
= {
116 .owner
= THIS_MODULE
,
117 .read
= test_fw_misc_read
,
120 static void __test_release_all_firmware(void)
122 struct test_batched_req
*req
;
125 if (!test_fw_config
->reqs
)
128 for (i
= 0; i
< test_fw_config
->num_requests
; i
++) {
129 req
= &test_fw_config
->reqs
[i
];
131 release_firmware(req
->fw
);
134 vfree(test_fw_config
->reqs
);
135 test_fw_config
->reqs
= NULL
;
138 static void test_release_all_firmware(void)
140 mutex_lock(&test_fw_mutex
);
141 __test_release_all_firmware();
142 mutex_unlock(&test_fw_mutex
);
146 static void __test_firmware_config_free(void)
148 __test_release_all_firmware();
149 kfree_const(test_fw_config
->name
);
150 test_fw_config
->name
= NULL
;
154 * XXX: move to kstrncpy() once merged.
156 * Users should use kfree_const() when freeing these.
158 static int __kstrncpy(char **dst
, const char *name
, size_t count
, gfp_t gfp
)
160 *dst
= kstrndup(name
, count
, gfp
);
166 static int __test_firmware_config_init(void)
170 ret
= __kstrncpy(&test_fw_config
->name
, TEST_FIRMWARE_NAME
,
171 strlen(TEST_FIRMWARE_NAME
), GFP_KERNEL
);
175 test_fw_config
->num_requests
= TEST_FIRMWARE_NUM_REQS
;
176 test_fw_config
->send_uevent
= true;
177 test_fw_config
->sync_direct
= false;
178 test_fw_config
->req_firmware
= request_firmware
;
179 test_fw_config
->test_result
= 0;
180 test_fw_config
->reqs
= NULL
;
185 __test_firmware_config_free();
189 static ssize_t
reset_store(struct device
*dev
,
190 struct device_attribute
*attr
,
191 const char *buf
, size_t count
)
195 mutex_lock(&test_fw_mutex
);
197 __test_firmware_config_free();
199 ret
= __test_firmware_config_init();
202 pr_err("could not alloc settings for config trigger: %d\n",
211 mutex_unlock(&test_fw_mutex
);
215 static DEVICE_ATTR_WO(reset
);
217 static ssize_t
config_show(struct device
*dev
,
218 struct device_attribute
*attr
,
223 mutex_lock(&test_fw_mutex
);
225 len
+= snprintf(buf
, PAGE_SIZE
,
226 "Custom trigger configuration for: %s\n",
229 if (test_fw_config
->name
)
230 len
+= snprintf(buf
+len
, PAGE_SIZE
,
232 test_fw_config
->name
);
234 len
+= snprintf(buf
+len
, PAGE_SIZE
,
237 len
+= snprintf(buf
+len
, PAGE_SIZE
,
238 "num_requests:\t%u\n", test_fw_config
->num_requests
);
240 len
+= snprintf(buf
+len
, PAGE_SIZE
,
241 "send_uevent:\t\t%s\n",
242 test_fw_config
->send_uevent
?
243 "FW_ACTION_HOTPLUG" :
244 "FW_ACTION_NOHOTPLUG");
245 len
+= snprintf(buf
+len
, PAGE_SIZE
,
246 "sync_direct:\t\t%s\n",
247 test_fw_config
->sync_direct
? "true" : "false");
248 len
+= snprintf(buf
+len
, PAGE_SIZE
,
249 "read_fw_idx:\t%u\n", test_fw_config
->read_fw_idx
);
251 mutex_unlock(&test_fw_mutex
);
255 static DEVICE_ATTR_RO(config
);
257 static ssize_t
config_name_store(struct device
*dev
,
258 struct device_attribute
*attr
,
259 const char *buf
, size_t count
)
263 mutex_lock(&test_fw_mutex
);
264 kfree_const(test_fw_config
->name
);
265 ret
= __kstrncpy(&test_fw_config
->name
, buf
, count
, GFP_KERNEL
);
266 mutex_unlock(&test_fw_mutex
);
272 * As per sysfs_kf_seq_show() the buf is max PAGE_SIZE.
274 static ssize_t
config_test_show_str(char *dst
,
279 mutex_lock(&test_fw_mutex
);
280 len
= snprintf(dst
, PAGE_SIZE
, "%s\n", src
);
281 mutex_unlock(&test_fw_mutex
);
286 static int test_dev_config_update_bool(const char *buf
, size_t size
,
291 mutex_lock(&test_fw_mutex
);
292 if (strtobool(buf
, cfg
) < 0)
296 mutex_unlock(&test_fw_mutex
);
302 test_dev_config_show_bool(char *buf
,
307 mutex_lock(&test_fw_mutex
);
309 mutex_unlock(&test_fw_mutex
);
311 return snprintf(buf
, PAGE_SIZE
, "%d\n", val
);
314 static ssize_t
test_dev_config_show_int(char *buf
, int cfg
)
318 mutex_lock(&test_fw_mutex
);
320 mutex_unlock(&test_fw_mutex
);
322 return snprintf(buf
, PAGE_SIZE
, "%d\n", val
);
325 static int test_dev_config_update_u8(const char *buf
, size_t size
, u8
*cfg
)
330 ret
= kstrtol(buf
, 10, &new);
337 mutex_lock(&test_fw_mutex
);
339 mutex_unlock(&test_fw_mutex
);
341 /* Always return full write size even if we didn't consume all */
345 static ssize_t
test_dev_config_show_u8(char *buf
, u8 cfg
)
349 mutex_lock(&test_fw_mutex
);
351 mutex_unlock(&test_fw_mutex
);
353 return snprintf(buf
, PAGE_SIZE
, "%u\n", val
);
356 static ssize_t
config_name_show(struct device
*dev
,
357 struct device_attribute
*attr
,
360 return config_test_show_str(buf
, test_fw_config
->name
);
362 static DEVICE_ATTR_RW(config_name
);
364 static ssize_t
config_num_requests_store(struct device
*dev
,
365 struct device_attribute
*attr
,
366 const char *buf
, size_t count
)
370 mutex_lock(&test_fw_mutex
);
371 if (test_fw_config
->reqs
) {
372 pr_err("Must call release_all_firmware prior to changing config\n");
374 mutex_unlock(&test_fw_mutex
);
377 mutex_unlock(&test_fw_mutex
);
379 rc
= test_dev_config_update_u8(buf
, count
,
380 &test_fw_config
->num_requests
);
386 static ssize_t
config_num_requests_show(struct device
*dev
,
387 struct device_attribute
*attr
,
390 return test_dev_config_show_u8(buf
, test_fw_config
->num_requests
);
392 static DEVICE_ATTR_RW(config_num_requests
);
394 static ssize_t
config_sync_direct_store(struct device
*dev
,
395 struct device_attribute
*attr
,
396 const char *buf
, size_t count
)
398 int rc
= test_dev_config_update_bool(buf
, count
,
399 &test_fw_config
->sync_direct
);
402 test_fw_config
->req_firmware
= test_fw_config
->sync_direct
?
403 request_firmware_direct
:
408 static ssize_t
config_sync_direct_show(struct device
*dev
,
409 struct device_attribute
*attr
,
412 return test_dev_config_show_bool(buf
, test_fw_config
->sync_direct
);
414 static DEVICE_ATTR_RW(config_sync_direct
);
416 static ssize_t
config_send_uevent_store(struct device
*dev
,
417 struct device_attribute
*attr
,
418 const char *buf
, size_t count
)
420 return test_dev_config_update_bool(buf
, count
,
421 &test_fw_config
->send_uevent
);
424 static ssize_t
config_send_uevent_show(struct device
*dev
,
425 struct device_attribute
*attr
,
428 return test_dev_config_show_bool(buf
, test_fw_config
->send_uevent
);
430 static DEVICE_ATTR_RW(config_send_uevent
);
432 static ssize_t
config_read_fw_idx_store(struct device
*dev
,
433 struct device_attribute
*attr
,
434 const char *buf
, size_t count
)
436 return test_dev_config_update_u8(buf
, count
,
437 &test_fw_config
->read_fw_idx
);
440 static ssize_t
config_read_fw_idx_show(struct device
*dev
,
441 struct device_attribute
*attr
,
444 return test_dev_config_show_u8(buf
, test_fw_config
->read_fw_idx
);
446 static DEVICE_ATTR_RW(config_read_fw_idx
);
449 static ssize_t
trigger_request_store(struct device
*dev
,
450 struct device_attribute
*attr
,
451 const char *buf
, size_t count
)
456 name
= kstrndup(buf
, count
, GFP_KERNEL
);
460 pr_info("loading '%s'\n", name
);
462 mutex_lock(&test_fw_mutex
);
463 release_firmware(test_firmware
);
464 test_firmware
= NULL
;
465 rc
= request_firmware(&test_firmware
, name
, dev
);
467 pr_info("load of '%s' failed: %d\n", name
, rc
);
470 pr_info("loaded: %zu\n", test_firmware
->size
);
474 mutex_unlock(&test_fw_mutex
);
480 static DEVICE_ATTR_WO(trigger_request
);
482 static DECLARE_COMPLETION(async_fw_done
);
484 static void trigger_async_request_cb(const struct firmware
*fw
, void *context
)
487 complete(&async_fw_done
);
490 static ssize_t
trigger_async_request_store(struct device
*dev
,
491 struct device_attribute
*attr
,
492 const char *buf
, size_t count
)
497 name
= kstrndup(buf
, count
, GFP_KERNEL
);
501 pr_info("loading '%s'\n", name
);
503 mutex_lock(&test_fw_mutex
);
504 release_firmware(test_firmware
);
505 test_firmware
= NULL
;
506 rc
= request_firmware_nowait(THIS_MODULE
, 1, name
, dev
, GFP_KERNEL
,
507 NULL
, trigger_async_request_cb
);
509 pr_info("async load of '%s' failed: %d\n", name
, rc
);
513 /* Free 'name' ASAP, to test for race conditions */
516 wait_for_completion(&async_fw_done
);
519 pr_info("loaded: %zu\n", test_firmware
->size
);
522 pr_err("failed to async load firmware\n");
527 mutex_unlock(&test_fw_mutex
);
531 static DEVICE_ATTR_WO(trigger_async_request
);
533 static ssize_t
trigger_custom_fallback_store(struct device
*dev
,
534 struct device_attribute
*attr
,
535 const char *buf
, size_t count
)
540 name
= kstrndup(buf
, count
, GFP_KERNEL
);
544 pr_info("loading '%s' using custom fallback mechanism\n", name
);
546 mutex_lock(&test_fw_mutex
);
547 release_firmware(test_firmware
);
548 test_firmware
= NULL
;
549 rc
= request_firmware_nowait(THIS_MODULE
, FW_ACTION_NOHOTPLUG
, name
,
550 dev
, GFP_KERNEL
, NULL
,
551 trigger_async_request_cb
);
553 pr_info("async load of '%s' failed: %d\n", name
, rc
);
557 /* Free 'name' ASAP, to test for race conditions */
560 wait_for_completion(&async_fw_done
);
563 pr_info("loaded: %zu\n", test_firmware
->size
);
566 pr_err("failed to async load firmware\n");
571 mutex_unlock(&test_fw_mutex
);
575 static DEVICE_ATTR_WO(trigger_custom_fallback
);
577 static int test_fw_run_batch_request(void *data
)
579 struct test_batched_req
*req
= data
;
582 test_fw_config
->test_result
= -EINVAL
;
586 req
->rc
= test_fw_config
->req_firmware(&req
->fw
, req
->name
, req
->dev
);
588 pr_info("#%u: batched sync load failed: %d\n",
590 if (!test_fw_config
->test_result
)
591 test_fw_config
->test_result
= req
->rc
;
592 } else if (req
->fw
) {
594 pr_info("#%u: batched sync loaded %zu\n",
595 req
->idx
, req
->fw
->size
);
597 complete(&req
->completion
);
605 * We use a kthread as otherwise the kernel serializes all our sync requests
606 * and we would not be able to mimic batched requests on a sync call. Batched
607 * requests on a sync call can for instance happen on a device driver when
608 * multiple cards are used and firmware loading happens outside of probe.
610 static ssize_t
trigger_batched_requests_store(struct device
*dev
,
611 struct device_attribute
*attr
,
612 const char *buf
, size_t count
)
614 struct test_batched_req
*req
;
618 mutex_lock(&test_fw_mutex
);
620 test_fw_config
->reqs
= vzalloc(sizeof(struct test_batched_req
) *
621 test_fw_config
->num_requests
* 2);
622 if (!test_fw_config
->reqs
) {
627 pr_info("batched sync firmware loading '%s' %u times\n",
628 test_fw_config
->name
, test_fw_config
->num_requests
);
630 for (i
= 0; i
< test_fw_config
->num_requests
; i
++) {
631 req
= &test_fw_config
->reqs
[i
];
639 req
->name
= test_fw_config
->name
;
641 init_completion(&req
->completion
);
642 req
->task
= kthread_run(test_fw_run_batch_request
, req
,
643 "%s-%u", KBUILD_MODNAME
, req
->idx
);
644 if (!req
->task
|| IS_ERR(req
->task
)) {
645 pr_err("Setting up thread %u failed\n", req
->idx
);
655 * We require an explicit release to enable more time and delay of
656 * calling release_firmware() to improve our chances of forcing a
657 * batched request. If we instead called release_firmware() right away
658 * then we might miss on an opportunity of having a successful firmware
659 * request pass on the opportunity to be come a batched request.
663 for (i
= 0; i
< test_fw_config
->num_requests
; i
++) {
664 req
= &test_fw_config
->reqs
[i
];
665 if (req
->task
|| req
->sent
)
666 wait_for_completion(&req
->completion
);
669 /* Override any worker error if we had a general setup error */
671 test_fw_config
->test_result
= rc
;
674 mutex_unlock(&test_fw_mutex
);
678 static DEVICE_ATTR_WO(trigger_batched_requests
);
681 * We wait for each callback to return with the lock held, no need to lock here
683 static void trigger_batched_cb(const struct firmware
*fw
, void *context
)
685 struct test_batched_req
*req
= context
;
688 test_fw_config
->test_result
= -EINVAL
;
692 /* forces *some* batched requests to queue up */
699 * Unfortunately the firmware API gives us nothing other than a null FW
700 * if the firmware was not found on async requests. Best we can do is
701 * just assume -ENOENT. A better API would pass the actual return
702 * value to the callback.
704 if (!fw
&& !test_fw_config
->test_result
)
705 test_fw_config
->test_result
= -ENOENT
;
707 complete(&req
->completion
);
711 ssize_t
trigger_batched_requests_async_store(struct device
*dev
,
712 struct device_attribute
*attr
,
713 const char *buf
, size_t count
)
715 struct test_batched_req
*req
;
720 mutex_lock(&test_fw_mutex
);
722 test_fw_config
->reqs
= vzalloc(sizeof(struct test_batched_req
) *
723 test_fw_config
->num_requests
* 2);
724 if (!test_fw_config
->reqs
) {
729 pr_info("batched loading '%s' custom fallback mechanism %u times\n",
730 test_fw_config
->name
, test_fw_config
->num_requests
);
732 send_uevent
= test_fw_config
->send_uevent
? FW_ACTION_HOTPLUG
:
735 for (i
= 0; i
< test_fw_config
->num_requests
; i
++) {
736 req
= &test_fw_config
->reqs
[i
];
741 req
->name
= test_fw_config
->name
;
744 init_completion(&req
->completion
);
745 rc
= request_firmware_nowait(THIS_MODULE
, send_uevent
,
747 dev
, GFP_KERNEL
, req
,
750 pr_info("#%u: batched async load failed setup: %d\n",
763 * We require an explicit release to enable more time and delay of
764 * calling release_firmware() to improve our chances of forcing a
765 * batched request. If we instead called release_firmware() right away
766 * then we might miss on an opportunity of having a successful firmware
767 * request pass on the opportunity to be come a batched request.
770 for (i
= 0; i
< test_fw_config
->num_requests
; i
++) {
771 req
= &test_fw_config
->reqs
[i
];
773 wait_for_completion(&req
->completion
);
776 /* Override any worker error if we had a general setup error */
778 test_fw_config
->test_result
= rc
;
781 mutex_unlock(&test_fw_mutex
);
785 static DEVICE_ATTR_WO(trigger_batched_requests_async
);
787 static ssize_t
test_result_show(struct device
*dev
,
788 struct device_attribute
*attr
,
791 return test_dev_config_show_int(buf
, test_fw_config
->test_result
);
793 static DEVICE_ATTR_RO(test_result
);
795 static ssize_t
release_all_firmware_store(struct device
*dev
,
796 struct device_attribute
*attr
,
797 const char *buf
, size_t count
)
799 test_release_all_firmware();
802 static DEVICE_ATTR_WO(release_all_firmware
);
804 static ssize_t
read_firmware_show(struct device
*dev
,
805 struct device_attribute
*attr
,
808 struct test_batched_req
*req
;
812 mutex_lock(&test_fw_mutex
);
814 idx
= test_fw_config
->read_fw_idx
;
815 if (idx
>= test_fw_config
->num_requests
) {
820 if (!test_fw_config
->reqs
) {
825 req
= &test_fw_config
->reqs
[idx
];
827 pr_err("#%u: failed to async load firmware\n", idx
);
832 pr_info("#%u: loaded %zu\n", idx
, req
->fw
->size
);
834 if (req
->fw
->size
> PAGE_SIZE
) {
835 pr_err("Testing interface must use PAGE_SIZE firmware for now\n");
838 memcpy(buf
, req
->fw
->data
, req
->fw
->size
);
842 mutex_unlock(&test_fw_mutex
);
846 static DEVICE_ATTR_RO(read_firmware
);
848 #define TEST_FW_DEV_ATTR(name) &dev_attr_##name.attr
850 static struct attribute
*test_dev_attrs
[] = {
851 TEST_FW_DEV_ATTR(reset
),
853 TEST_FW_DEV_ATTR(config
),
854 TEST_FW_DEV_ATTR(config_name
),
855 TEST_FW_DEV_ATTR(config_num_requests
),
856 TEST_FW_DEV_ATTR(config_sync_direct
),
857 TEST_FW_DEV_ATTR(config_send_uevent
),
858 TEST_FW_DEV_ATTR(config_read_fw_idx
),
860 /* These don't use the config at all - they could be ported! */
861 TEST_FW_DEV_ATTR(trigger_request
),
862 TEST_FW_DEV_ATTR(trigger_async_request
),
863 TEST_FW_DEV_ATTR(trigger_custom_fallback
),
865 /* These use the config and can use the test_result */
866 TEST_FW_DEV_ATTR(trigger_batched_requests
),
867 TEST_FW_DEV_ATTR(trigger_batched_requests_async
),
869 TEST_FW_DEV_ATTR(release_all_firmware
),
870 TEST_FW_DEV_ATTR(test_result
),
871 TEST_FW_DEV_ATTR(read_firmware
),
875 ATTRIBUTE_GROUPS(test_dev
);
877 static struct miscdevice test_fw_misc_device
= {
878 .minor
= MISC_DYNAMIC_MINOR
,
879 .name
= "test_firmware",
880 .fops
= &test_fw_fops
,
881 .groups
= test_dev_groups
,
884 static int __init
test_firmware_init(void)
888 test_fw_config
= kzalloc(sizeof(struct test_config
), GFP_KERNEL
);
892 rc
= __test_firmware_config_init();
896 rc
= misc_register(&test_fw_misc_device
);
898 kfree(test_fw_config
);
899 pr_err("could not register misc device: %d\n", rc
);
903 pr_warn("interface ready\n");
908 module_init(test_firmware_init
);
910 static void __exit
test_firmware_exit(void)
912 mutex_lock(&test_fw_mutex
);
913 release_firmware(test_firmware
);
914 misc_deregister(&test_fw_misc_device
);
915 __test_firmware_config_free();
916 kfree(test_fw_config
);
917 mutex_unlock(&test_fw_mutex
);
919 pr_warn("removed interface\n");
922 module_exit(test_firmware_exit
);
924 MODULE_AUTHOR("Kees Cook <keescook@chromium.org>");
925 MODULE_LICENSE("GPL");