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>
24 #include <linux/vmalloc.h>
26 #define TEST_FIRMWARE_NAME "test-firmware.bin"
27 #define TEST_FIRMWARE_NUM_REQS 4
29 static DEFINE_MUTEX(test_fw_mutex
);
30 static const struct firmware
*test_firmware
;
32 struct test_batched_req
{
36 const struct firmware
*fw
;
38 struct completion completion
;
39 struct task_struct
*task
;
44 * test_config - represents configuration for the test for different triggers
46 * @name: the name of the firmware file to look for
47 * @sync_direct: when the sync trigger is used if this is true
48 * request_firmware_direct() will be used instead.
49 * @send_uevent: whether or not to send a uevent for async requests
50 * @num_requests: number of requests to try per test case. This is trigger
52 * @reqs: stores all requests information
53 * @read_fw_idx: index of thread from which we want to read firmware results
54 * from through the read_fw trigger.
55 * @test_result: a test may use this to collect the result from the call
56 * of the request_firmware*() calls used in their tests. In order of
57 * priority we always keep first any setup error. If no setup errors were
58 * found then we move on to the first error encountered while running the
59 * API. Note that for async calls this typically will be a successful
60 * result (0) unless of course you've used bogus parameters, or the system
61 * is out of memory. In the async case the callback is expected to do a
62 * bit more homework to figure out what happened, unfortunately the only
63 * information passed today on error is the fact that no firmware was
64 * found so we can only assume -ENOENT on async calls if the firmware is
67 * Errors you can expect:
71 * 0: success for sync, for async it means request was sent
72 * -EINVAL: invalid parameters or request
73 * -ENOENT: files not found
77 * -ENOMEM: memory pressure on system
78 * -ENODEV: out of number of devices to test
79 * -EINVAL: an unexpected error has occurred
80 * @req_firmware: if @sync_direct is true this is set to
81 * request_firmware_direct(), otherwise request_firmware()
91 * These below don't belong her but we'll move them once we create
92 * a struct fw_test_device and stuff the misc_dev under there later.
94 struct test_batched_req
*reqs
;
96 int (*req_firmware
)(const struct firmware
**fw
, const char *name
,
97 struct device
*device
);
100 static struct test_config
*test_fw_config
;
102 static ssize_t
test_fw_misc_read(struct file
*f
, char __user
*buf
,
103 size_t size
, loff_t
*offset
)
107 mutex_lock(&test_fw_mutex
);
109 rc
= simple_read_from_buffer(buf
, size
, offset
,
111 test_firmware
->size
);
112 mutex_unlock(&test_fw_mutex
);
116 static const struct file_operations test_fw_fops
= {
117 .owner
= THIS_MODULE
,
118 .read
= test_fw_misc_read
,
121 static void __test_release_all_firmware(void)
123 struct test_batched_req
*req
;
126 if (!test_fw_config
->reqs
)
129 for (i
= 0; i
< test_fw_config
->num_requests
; i
++) {
130 req
= &test_fw_config
->reqs
[i
];
132 release_firmware(req
->fw
);
135 vfree(test_fw_config
->reqs
);
136 test_fw_config
->reqs
= NULL
;
139 static void test_release_all_firmware(void)
141 mutex_lock(&test_fw_mutex
);
142 __test_release_all_firmware();
143 mutex_unlock(&test_fw_mutex
);
147 static void __test_firmware_config_free(void)
149 __test_release_all_firmware();
150 kfree_const(test_fw_config
->name
);
151 test_fw_config
->name
= NULL
;
155 * XXX: move to kstrncpy() once merged.
157 * Users should use kfree_const() when freeing these.
159 static int __kstrncpy(char **dst
, const char *name
, size_t count
, gfp_t gfp
)
161 *dst
= kstrndup(name
, count
, gfp
);
167 static int __test_firmware_config_init(void)
171 ret
= __kstrncpy(&test_fw_config
->name
, TEST_FIRMWARE_NAME
,
172 strlen(TEST_FIRMWARE_NAME
), GFP_KERNEL
);
176 test_fw_config
->num_requests
= TEST_FIRMWARE_NUM_REQS
;
177 test_fw_config
->send_uevent
= true;
178 test_fw_config
->sync_direct
= false;
179 test_fw_config
->req_firmware
= request_firmware
;
180 test_fw_config
->test_result
= 0;
181 test_fw_config
->reqs
= NULL
;
186 __test_firmware_config_free();
190 static ssize_t
reset_store(struct device
*dev
,
191 struct device_attribute
*attr
,
192 const char *buf
, size_t count
)
196 mutex_lock(&test_fw_mutex
);
198 __test_firmware_config_free();
200 ret
= __test_firmware_config_init();
203 pr_err("could not alloc settings for config trigger: %d\n",
212 mutex_unlock(&test_fw_mutex
);
216 static DEVICE_ATTR_WO(reset
);
218 static ssize_t
config_show(struct device
*dev
,
219 struct device_attribute
*attr
,
224 mutex_lock(&test_fw_mutex
);
226 len
+= snprintf(buf
, PAGE_SIZE
,
227 "Custom trigger configuration for: %s\n",
230 if (test_fw_config
->name
)
231 len
+= snprintf(buf
+len
, PAGE_SIZE
,
233 test_fw_config
->name
);
235 len
+= snprintf(buf
+len
, PAGE_SIZE
,
238 len
+= snprintf(buf
+len
, PAGE_SIZE
,
239 "num_requests:\t%u\n", test_fw_config
->num_requests
);
241 len
+= snprintf(buf
+len
, PAGE_SIZE
,
242 "send_uevent:\t\t%s\n",
243 test_fw_config
->send_uevent
?
244 "FW_ACTION_HOTPLUG" :
245 "FW_ACTION_NOHOTPLUG");
246 len
+= snprintf(buf
+len
, PAGE_SIZE
,
247 "sync_direct:\t\t%s\n",
248 test_fw_config
->sync_direct
? "true" : "false");
249 len
+= snprintf(buf
+len
, PAGE_SIZE
,
250 "read_fw_idx:\t%u\n", test_fw_config
->read_fw_idx
);
252 mutex_unlock(&test_fw_mutex
);
256 static DEVICE_ATTR_RO(config
);
258 static ssize_t
config_name_store(struct device
*dev
,
259 struct device_attribute
*attr
,
260 const char *buf
, size_t count
)
264 mutex_lock(&test_fw_mutex
);
265 kfree_const(test_fw_config
->name
);
266 ret
= __kstrncpy(&test_fw_config
->name
, buf
, count
, GFP_KERNEL
);
267 mutex_unlock(&test_fw_mutex
);
273 * As per sysfs_kf_seq_show() the buf is max PAGE_SIZE.
275 static ssize_t
config_test_show_str(char *dst
,
280 mutex_lock(&test_fw_mutex
);
281 len
= snprintf(dst
, PAGE_SIZE
, "%s\n", src
);
282 mutex_unlock(&test_fw_mutex
);
287 static int test_dev_config_update_bool(const char *buf
, size_t size
,
292 mutex_lock(&test_fw_mutex
);
293 if (strtobool(buf
, cfg
) < 0)
297 mutex_unlock(&test_fw_mutex
);
303 test_dev_config_show_bool(char *buf
,
308 mutex_lock(&test_fw_mutex
);
310 mutex_unlock(&test_fw_mutex
);
312 return snprintf(buf
, PAGE_SIZE
, "%d\n", val
);
315 static ssize_t
test_dev_config_show_int(char *buf
, int cfg
)
319 mutex_lock(&test_fw_mutex
);
321 mutex_unlock(&test_fw_mutex
);
323 return snprintf(buf
, PAGE_SIZE
, "%d\n", val
);
326 static int test_dev_config_update_u8(const char *buf
, size_t size
, u8
*cfg
)
331 ret
= kstrtol(buf
, 10, &new);
338 mutex_lock(&test_fw_mutex
);
340 mutex_unlock(&test_fw_mutex
);
342 /* Always return full write size even if we didn't consume all */
346 static ssize_t
test_dev_config_show_u8(char *buf
, u8 cfg
)
350 mutex_lock(&test_fw_mutex
);
352 mutex_unlock(&test_fw_mutex
);
354 return snprintf(buf
, PAGE_SIZE
, "%u\n", val
);
357 static ssize_t
config_name_show(struct device
*dev
,
358 struct device_attribute
*attr
,
361 return config_test_show_str(buf
, test_fw_config
->name
);
363 static DEVICE_ATTR_RW(config_name
);
365 static ssize_t
config_num_requests_store(struct device
*dev
,
366 struct device_attribute
*attr
,
367 const char *buf
, size_t count
)
371 mutex_lock(&test_fw_mutex
);
372 if (test_fw_config
->reqs
) {
373 pr_err("Must call release_all_firmware prior to changing config\n");
375 mutex_unlock(&test_fw_mutex
);
378 mutex_unlock(&test_fw_mutex
);
380 rc
= test_dev_config_update_u8(buf
, count
,
381 &test_fw_config
->num_requests
);
387 static ssize_t
config_num_requests_show(struct device
*dev
,
388 struct device_attribute
*attr
,
391 return test_dev_config_show_u8(buf
, test_fw_config
->num_requests
);
393 static DEVICE_ATTR_RW(config_num_requests
);
395 static ssize_t
config_sync_direct_store(struct device
*dev
,
396 struct device_attribute
*attr
,
397 const char *buf
, size_t count
)
399 int rc
= test_dev_config_update_bool(buf
, count
,
400 &test_fw_config
->sync_direct
);
403 test_fw_config
->req_firmware
= test_fw_config
->sync_direct
?
404 request_firmware_direct
:
409 static ssize_t
config_sync_direct_show(struct device
*dev
,
410 struct device_attribute
*attr
,
413 return test_dev_config_show_bool(buf
, test_fw_config
->sync_direct
);
415 static DEVICE_ATTR_RW(config_sync_direct
);
417 static ssize_t
config_send_uevent_store(struct device
*dev
,
418 struct device_attribute
*attr
,
419 const char *buf
, size_t count
)
421 return test_dev_config_update_bool(buf
, count
,
422 &test_fw_config
->send_uevent
);
425 static ssize_t
config_send_uevent_show(struct device
*dev
,
426 struct device_attribute
*attr
,
429 return test_dev_config_show_bool(buf
, test_fw_config
->send_uevent
);
431 static DEVICE_ATTR_RW(config_send_uevent
);
433 static ssize_t
config_read_fw_idx_store(struct device
*dev
,
434 struct device_attribute
*attr
,
435 const char *buf
, size_t count
)
437 return test_dev_config_update_u8(buf
, count
,
438 &test_fw_config
->read_fw_idx
);
441 static ssize_t
config_read_fw_idx_show(struct device
*dev
,
442 struct device_attribute
*attr
,
445 return test_dev_config_show_u8(buf
, test_fw_config
->read_fw_idx
);
447 static DEVICE_ATTR_RW(config_read_fw_idx
);
450 static ssize_t
trigger_request_store(struct device
*dev
,
451 struct device_attribute
*attr
,
452 const char *buf
, size_t count
)
457 name
= kstrndup(buf
, count
, GFP_KERNEL
);
461 pr_info("loading '%s'\n", name
);
463 mutex_lock(&test_fw_mutex
);
464 release_firmware(test_firmware
);
465 test_firmware
= NULL
;
466 rc
= request_firmware(&test_firmware
, name
, dev
);
468 pr_info("load of '%s' failed: %d\n", name
, rc
);
471 pr_info("loaded: %zu\n", test_firmware
->size
);
475 mutex_unlock(&test_fw_mutex
);
481 static DEVICE_ATTR_WO(trigger_request
);
483 static DECLARE_COMPLETION(async_fw_done
);
485 static void trigger_async_request_cb(const struct firmware
*fw
, void *context
)
488 complete(&async_fw_done
);
491 static ssize_t
trigger_async_request_store(struct device
*dev
,
492 struct device_attribute
*attr
,
493 const char *buf
, size_t count
)
498 name
= kstrndup(buf
, count
, GFP_KERNEL
);
502 pr_info("loading '%s'\n", name
);
504 mutex_lock(&test_fw_mutex
);
505 release_firmware(test_firmware
);
506 test_firmware
= NULL
;
507 rc
= request_firmware_nowait(THIS_MODULE
, 1, name
, dev
, GFP_KERNEL
,
508 NULL
, trigger_async_request_cb
);
510 pr_info("async load of '%s' failed: %d\n", name
, rc
);
514 /* Free 'name' ASAP, to test for race conditions */
517 wait_for_completion(&async_fw_done
);
520 pr_info("loaded: %zu\n", test_firmware
->size
);
523 pr_err("failed to async load firmware\n");
528 mutex_unlock(&test_fw_mutex
);
532 static DEVICE_ATTR_WO(trigger_async_request
);
534 static ssize_t
trigger_custom_fallback_store(struct device
*dev
,
535 struct device_attribute
*attr
,
536 const char *buf
, size_t count
)
541 name
= kstrndup(buf
, count
, GFP_KERNEL
);
545 pr_info("loading '%s' using custom fallback mechanism\n", name
);
547 mutex_lock(&test_fw_mutex
);
548 release_firmware(test_firmware
);
549 test_firmware
= NULL
;
550 rc
= request_firmware_nowait(THIS_MODULE
, FW_ACTION_NOHOTPLUG
, name
,
551 dev
, GFP_KERNEL
, NULL
,
552 trigger_async_request_cb
);
554 pr_info("async load of '%s' failed: %d\n", name
, rc
);
558 /* Free 'name' ASAP, to test for race conditions */
561 wait_for_completion(&async_fw_done
);
564 pr_info("loaded: %zu\n", test_firmware
->size
);
567 pr_err("failed to async load firmware\n");
572 mutex_unlock(&test_fw_mutex
);
576 static DEVICE_ATTR_WO(trigger_custom_fallback
);
578 static int test_fw_run_batch_request(void *data
)
580 struct test_batched_req
*req
= data
;
583 test_fw_config
->test_result
= -EINVAL
;
587 req
->rc
= test_fw_config
->req_firmware(&req
->fw
, req
->name
, req
->dev
);
589 pr_info("#%u: batched sync load failed: %d\n",
591 if (!test_fw_config
->test_result
)
592 test_fw_config
->test_result
= req
->rc
;
593 } else if (req
->fw
) {
595 pr_info("#%u: batched sync loaded %zu\n",
596 req
->idx
, req
->fw
->size
);
598 complete(&req
->completion
);
606 * We use a kthread as otherwise the kernel serializes all our sync requests
607 * and we would not be able to mimic batched requests on a sync call. Batched
608 * requests on a sync call can for instance happen on a device driver when
609 * multiple cards are used and firmware loading happens outside of probe.
611 static ssize_t
trigger_batched_requests_store(struct device
*dev
,
612 struct device_attribute
*attr
,
613 const char *buf
, size_t count
)
615 struct test_batched_req
*req
;
619 mutex_lock(&test_fw_mutex
);
621 test_fw_config
->reqs
= vzalloc(sizeof(struct test_batched_req
) *
622 test_fw_config
->num_requests
* 2);
623 if (!test_fw_config
->reqs
) {
628 pr_info("batched sync firmware loading '%s' %u times\n",
629 test_fw_config
->name
, test_fw_config
->num_requests
);
631 for (i
= 0; i
< test_fw_config
->num_requests
; i
++) {
632 req
= &test_fw_config
->reqs
[i
];
640 req
->name
= test_fw_config
->name
;
642 init_completion(&req
->completion
);
643 req
->task
= kthread_run(test_fw_run_batch_request
, req
,
644 "%s-%u", KBUILD_MODNAME
, req
->idx
);
645 if (!req
->task
|| IS_ERR(req
->task
)) {
646 pr_err("Setting up thread %u failed\n", req
->idx
);
656 * We require an explicit release to enable more time and delay of
657 * calling release_firmware() to improve our chances of forcing a
658 * batched request. If we instead called release_firmware() right away
659 * then we might miss on an opportunity of having a successful firmware
660 * request pass on the opportunity to be come a batched request.
664 for (i
= 0; i
< test_fw_config
->num_requests
; i
++) {
665 req
= &test_fw_config
->reqs
[i
];
666 if (req
->task
|| req
->sent
)
667 wait_for_completion(&req
->completion
);
670 /* Override any worker error if we had a general setup error */
672 test_fw_config
->test_result
= rc
;
675 mutex_unlock(&test_fw_mutex
);
679 static DEVICE_ATTR_WO(trigger_batched_requests
);
682 * We wait for each callback to return with the lock held, no need to lock here
684 static void trigger_batched_cb(const struct firmware
*fw
, void *context
)
686 struct test_batched_req
*req
= context
;
689 test_fw_config
->test_result
= -EINVAL
;
693 /* forces *some* batched requests to queue up */
700 * Unfortunately the firmware API gives us nothing other than a null FW
701 * if the firmware was not found on async requests. Best we can do is
702 * just assume -ENOENT. A better API would pass the actual return
703 * value to the callback.
705 if (!fw
&& !test_fw_config
->test_result
)
706 test_fw_config
->test_result
= -ENOENT
;
708 complete(&req
->completion
);
712 ssize_t
trigger_batched_requests_async_store(struct device
*dev
,
713 struct device_attribute
*attr
,
714 const char *buf
, size_t count
)
716 struct test_batched_req
*req
;
721 mutex_lock(&test_fw_mutex
);
723 test_fw_config
->reqs
= vzalloc(sizeof(struct test_batched_req
) *
724 test_fw_config
->num_requests
* 2);
725 if (!test_fw_config
->reqs
) {
730 pr_info("batched loading '%s' custom fallback mechanism %u times\n",
731 test_fw_config
->name
, test_fw_config
->num_requests
);
733 send_uevent
= test_fw_config
->send_uevent
? FW_ACTION_HOTPLUG
:
736 for (i
= 0; i
< test_fw_config
->num_requests
; i
++) {
737 req
= &test_fw_config
->reqs
[i
];
742 req
->name
= test_fw_config
->name
;
745 init_completion(&req
->completion
);
746 rc
= request_firmware_nowait(THIS_MODULE
, send_uevent
,
748 dev
, GFP_KERNEL
, req
,
751 pr_info("#%u: batched async load failed setup: %d\n",
764 * We require an explicit release to enable more time and delay of
765 * calling release_firmware() to improve our chances of forcing a
766 * batched request. If we instead called release_firmware() right away
767 * then we might miss on an opportunity of having a successful firmware
768 * request pass on the opportunity to be come a batched request.
771 for (i
= 0; i
< test_fw_config
->num_requests
; i
++) {
772 req
= &test_fw_config
->reqs
[i
];
774 wait_for_completion(&req
->completion
);
777 /* Override any worker error if we had a general setup error */
779 test_fw_config
->test_result
= rc
;
782 mutex_unlock(&test_fw_mutex
);
786 static DEVICE_ATTR_WO(trigger_batched_requests_async
);
788 static ssize_t
test_result_show(struct device
*dev
,
789 struct device_attribute
*attr
,
792 return test_dev_config_show_int(buf
, test_fw_config
->test_result
);
794 static DEVICE_ATTR_RO(test_result
);
796 static ssize_t
release_all_firmware_store(struct device
*dev
,
797 struct device_attribute
*attr
,
798 const char *buf
, size_t count
)
800 test_release_all_firmware();
803 static DEVICE_ATTR_WO(release_all_firmware
);
805 static ssize_t
read_firmware_show(struct device
*dev
,
806 struct device_attribute
*attr
,
809 struct test_batched_req
*req
;
813 mutex_lock(&test_fw_mutex
);
815 idx
= test_fw_config
->read_fw_idx
;
816 if (idx
>= test_fw_config
->num_requests
) {
821 if (!test_fw_config
->reqs
) {
826 req
= &test_fw_config
->reqs
[idx
];
828 pr_err("#%u: failed to async load firmware\n", idx
);
833 pr_info("#%u: loaded %zu\n", idx
, req
->fw
->size
);
835 if (req
->fw
->size
> PAGE_SIZE
) {
836 pr_err("Testing interface must use PAGE_SIZE firmware for now\n");
839 memcpy(buf
, req
->fw
->data
, req
->fw
->size
);
843 mutex_unlock(&test_fw_mutex
);
847 static DEVICE_ATTR_RO(read_firmware
);
849 #define TEST_FW_DEV_ATTR(name) &dev_attr_##name.attr
851 static struct attribute
*test_dev_attrs
[] = {
852 TEST_FW_DEV_ATTR(reset
),
854 TEST_FW_DEV_ATTR(config
),
855 TEST_FW_DEV_ATTR(config_name
),
856 TEST_FW_DEV_ATTR(config_num_requests
),
857 TEST_FW_DEV_ATTR(config_sync_direct
),
858 TEST_FW_DEV_ATTR(config_send_uevent
),
859 TEST_FW_DEV_ATTR(config_read_fw_idx
),
861 /* These don't use the config at all - they could be ported! */
862 TEST_FW_DEV_ATTR(trigger_request
),
863 TEST_FW_DEV_ATTR(trigger_async_request
),
864 TEST_FW_DEV_ATTR(trigger_custom_fallback
),
866 /* These use the config and can use the test_result */
867 TEST_FW_DEV_ATTR(trigger_batched_requests
),
868 TEST_FW_DEV_ATTR(trigger_batched_requests_async
),
870 TEST_FW_DEV_ATTR(release_all_firmware
),
871 TEST_FW_DEV_ATTR(test_result
),
872 TEST_FW_DEV_ATTR(read_firmware
),
876 ATTRIBUTE_GROUPS(test_dev
);
878 static struct miscdevice test_fw_misc_device
= {
879 .minor
= MISC_DYNAMIC_MINOR
,
880 .name
= "test_firmware",
881 .fops
= &test_fw_fops
,
882 .groups
= test_dev_groups
,
885 static int __init
test_firmware_init(void)
889 test_fw_config
= kzalloc(sizeof(struct test_config
), GFP_KERNEL
);
893 rc
= __test_firmware_config_init();
897 rc
= misc_register(&test_fw_misc_device
);
899 kfree(test_fw_config
);
900 pr_err("could not register misc device: %d\n", rc
);
904 pr_warn("interface ready\n");
909 module_init(test_firmware_init
);
911 static void __exit
test_firmware_exit(void)
913 mutex_lock(&test_fw_mutex
);
914 release_firmware(test_firmware
);
915 misc_deregister(&test_fw_misc_device
);
916 __test_firmware_config_free();
917 kfree(test_fw_config
);
918 mutex_unlock(&test_fw_mutex
);
920 pr_warn("removed interface\n");
923 module_exit(test_firmware_exit
);
925 MODULE_AUTHOR("Kees Cook <keescook@chromium.org>");
926 MODULE_LICENSE("GPL");