1 // SPDX-License-Identifier: GPL-2.0-only
3 * This module provides an interface to trigger and test firmware loading.
5 * It is designed to be used for basic evaluation of the firmware loading
6 * subsystem (for example when validating firmware verification). It lacks
7 * any extra dependencies, and will not normally be loaded by the system
8 * unless explicitly requested by name.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/printk.h>
16 #include <linux/completion.h>
17 #include <linux/firmware.h>
18 #include <linux/device.h>
20 #include <linux/miscdevice.h>
21 #include <linux/slab.h>
22 #include <linux/uaccess.h>
23 #include <linux/delay.h>
24 #include <linux/kthread.h>
25 #include <linux/vmalloc.h>
27 #define TEST_FIRMWARE_NAME "test-firmware.bin"
28 #define TEST_FIRMWARE_NUM_REQS 4
30 static DEFINE_MUTEX(test_fw_mutex
);
31 static const struct firmware
*test_firmware
;
33 struct test_batched_req
{
37 const struct firmware
*fw
;
39 struct completion completion
;
40 struct task_struct
*task
;
45 * test_config - represents configuration for the test for different triggers
47 * @name: the name of the firmware file to look for
48 * @sync_direct: when the sync trigger is used if this is true
49 * request_firmware_direct() will be used instead.
50 * @send_uevent: whether or not to send a uevent for async requests
51 * @num_requests: number of requests to try per test case. This is trigger
53 * @reqs: stores all requests information
54 * @read_fw_idx: index of thread from which we want to read firmware results
55 * from through the read_fw trigger.
56 * @test_result: a test may use this to collect the result from the call
57 * of the request_firmware*() calls used in their tests. In order of
58 * priority we always keep first any setup error. If no setup errors were
59 * found then we move on to the first error encountered while running the
60 * API. Note that for async calls this typically will be a successful
61 * result (0) unless of course you've used bogus parameters, or the system
62 * is out of memory. In the async case the callback is expected to do a
63 * bit more homework to figure out what happened, unfortunately the only
64 * information passed today on error is the fact that no firmware was
65 * found so we can only assume -ENOENT on async calls if the firmware is
68 * Errors you can expect:
72 * 0: success for sync, for async it means request was sent
73 * -EINVAL: invalid parameters or request
74 * -ENOENT: files not found
78 * -ENOMEM: memory pressure on system
79 * -ENODEV: out of number of devices to test
80 * -EINVAL: an unexpected error has occurred
81 * @req_firmware: if @sync_direct is true this is set to
82 * request_firmware_direct(), otherwise request_firmware()
92 * These below don't belong her but we'll move them once we create
93 * a struct fw_test_device and stuff the misc_dev under there later.
95 struct test_batched_req
*reqs
;
97 int (*req_firmware
)(const struct firmware
**fw
, const char *name
,
98 struct device
*device
);
101 static struct test_config
*test_fw_config
;
103 static ssize_t
test_fw_misc_read(struct file
*f
, char __user
*buf
,
104 size_t size
, loff_t
*offset
)
108 mutex_lock(&test_fw_mutex
);
110 rc
= simple_read_from_buffer(buf
, size
, offset
,
112 test_firmware
->size
);
113 mutex_unlock(&test_fw_mutex
);
117 static const struct file_operations test_fw_fops
= {
118 .owner
= THIS_MODULE
,
119 .read
= test_fw_misc_read
,
122 static void __test_release_all_firmware(void)
124 struct test_batched_req
*req
;
127 if (!test_fw_config
->reqs
)
130 for (i
= 0; i
< test_fw_config
->num_requests
; i
++) {
131 req
= &test_fw_config
->reqs
[i
];
133 release_firmware(req
->fw
);
136 vfree(test_fw_config
->reqs
);
137 test_fw_config
->reqs
= NULL
;
140 static void test_release_all_firmware(void)
142 mutex_lock(&test_fw_mutex
);
143 __test_release_all_firmware();
144 mutex_unlock(&test_fw_mutex
);
148 static void __test_firmware_config_free(void)
150 __test_release_all_firmware();
151 kfree_const(test_fw_config
->name
);
152 test_fw_config
->name
= NULL
;
156 * XXX: move to kstrncpy() once merged.
158 * Users should use kfree_const() when freeing these.
160 static int __kstrncpy(char **dst
, const char *name
, size_t count
, gfp_t gfp
)
162 *dst
= kstrndup(name
, count
, gfp
);
168 static int __test_firmware_config_init(void)
172 ret
= __kstrncpy(&test_fw_config
->name
, TEST_FIRMWARE_NAME
,
173 strlen(TEST_FIRMWARE_NAME
), GFP_KERNEL
);
177 test_fw_config
->num_requests
= TEST_FIRMWARE_NUM_REQS
;
178 test_fw_config
->send_uevent
= true;
179 test_fw_config
->sync_direct
= false;
180 test_fw_config
->req_firmware
= request_firmware
;
181 test_fw_config
->test_result
= 0;
182 test_fw_config
->reqs
= NULL
;
187 __test_firmware_config_free();
191 static ssize_t
reset_store(struct device
*dev
,
192 struct device_attribute
*attr
,
193 const char *buf
, size_t count
)
197 mutex_lock(&test_fw_mutex
);
199 __test_firmware_config_free();
201 ret
= __test_firmware_config_init();
204 pr_err("could not alloc settings for config trigger: %d\n",
213 mutex_unlock(&test_fw_mutex
);
217 static DEVICE_ATTR_WO(reset
);
219 static ssize_t
config_show(struct device
*dev
,
220 struct device_attribute
*attr
,
225 mutex_lock(&test_fw_mutex
);
227 len
+= scnprintf(buf
, PAGE_SIZE
- len
,
228 "Custom trigger configuration for: %s\n",
231 if (test_fw_config
->name
)
232 len
+= scnprintf(buf
+len
, PAGE_SIZE
- len
,
234 test_fw_config
->name
);
236 len
+= scnprintf(buf
+len
, PAGE_SIZE
- len
,
239 len
+= scnprintf(buf
+len
, PAGE_SIZE
- len
,
240 "num_requests:\t%u\n", test_fw_config
->num_requests
);
242 len
+= scnprintf(buf
+len
, PAGE_SIZE
- len
,
243 "send_uevent:\t\t%s\n",
244 test_fw_config
->send_uevent
?
245 "FW_ACTION_HOTPLUG" :
246 "FW_ACTION_NOHOTPLUG");
247 len
+= scnprintf(buf
+len
, PAGE_SIZE
- len
,
248 "sync_direct:\t\t%s\n",
249 test_fw_config
->sync_direct
? "true" : "false");
250 len
+= scnprintf(buf
+len
, PAGE_SIZE
- len
,
251 "read_fw_idx:\t%u\n", test_fw_config
->read_fw_idx
);
253 mutex_unlock(&test_fw_mutex
);
257 static DEVICE_ATTR_RO(config
);
259 static ssize_t
config_name_store(struct device
*dev
,
260 struct device_attribute
*attr
,
261 const char *buf
, size_t count
)
265 mutex_lock(&test_fw_mutex
);
266 kfree_const(test_fw_config
->name
);
267 ret
= __kstrncpy(&test_fw_config
->name
, buf
, count
, GFP_KERNEL
);
268 mutex_unlock(&test_fw_mutex
);
274 * As per sysfs_kf_seq_show() the buf is max PAGE_SIZE.
276 static ssize_t
config_test_show_str(char *dst
,
281 mutex_lock(&test_fw_mutex
);
282 len
= snprintf(dst
, PAGE_SIZE
, "%s\n", src
);
283 mutex_unlock(&test_fw_mutex
);
288 static int test_dev_config_update_bool(const char *buf
, size_t size
,
293 mutex_lock(&test_fw_mutex
);
294 if (strtobool(buf
, cfg
) < 0)
298 mutex_unlock(&test_fw_mutex
);
304 test_dev_config_show_bool(char *buf
,
309 mutex_lock(&test_fw_mutex
);
311 mutex_unlock(&test_fw_mutex
);
313 return snprintf(buf
, PAGE_SIZE
, "%d\n", val
);
316 static ssize_t
test_dev_config_show_int(char *buf
, int cfg
)
320 mutex_lock(&test_fw_mutex
);
322 mutex_unlock(&test_fw_mutex
);
324 return snprintf(buf
, PAGE_SIZE
, "%d\n", val
);
327 static int test_dev_config_update_u8(const char *buf
, size_t size
, u8
*cfg
)
332 ret
= kstrtol(buf
, 10, &new);
339 mutex_lock(&test_fw_mutex
);
341 mutex_unlock(&test_fw_mutex
);
343 /* Always return full write size even if we didn't consume all */
347 static ssize_t
test_dev_config_show_u8(char *buf
, u8 cfg
)
351 mutex_lock(&test_fw_mutex
);
353 mutex_unlock(&test_fw_mutex
);
355 return snprintf(buf
, PAGE_SIZE
, "%u\n", val
);
358 static ssize_t
config_name_show(struct device
*dev
,
359 struct device_attribute
*attr
,
362 return config_test_show_str(buf
, test_fw_config
->name
);
364 static DEVICE_ATTR_RW(config_name
);
366 static ssize_t
config_num_requests_store(struct device
*dev
,
367 struct device_attribute
*attr
,
368 const char *buf
, size_t count
)
372 mutex_lock(&test_fw_mutex
);
373 if (test_fw_config
->reqs
) {
374 pr_err("Must call release_all_firmware prior to changing config\n");
376 mutex_unlock(&test_fw_mutex
);
379 mutex_unlock(&test_fw_mutex
);
381 rc
= test_dev_config_update_u8(buf
, count
,
382 &test_fw_config
->num_requests
);
388 static ssize_t
config_num_requests_show(struct device
*dev
,
389 struct device_attribute
*attr
,
392 return test_dev_config_show_u8(buf
, test_fw_config
->num_requests
);
394 static DEVICE_ATTR_RW(config_num_requests
);
396 static ssize_t
config_sync_direct_store(struct device
*dev
,
397 struct device_attribute
*attr
,
398 const char *buf
, size_t count
)
400 int rc
= test_dev_config_update_bool(buf
, count
,
401 &test_fw_config
->sync_direct
);
404 test_fw_config
->req_firmware
= test_fw_config
->sync_direct
?
405 request_firmware_direct
:
410 static ssize_t
config_sync_direct_show(struct device
*dev
,
411 struct device_attribute
*attr
,
414 return test_dev_config_show_bool(buf
, test_fw_config
->sync_direct
);
416 static DEVICE_ATTR_RW(config_sync_direct
);
418 static ssize_t
config_send_uevent_store(struct device
*dev
,
419 struct device_attribute
*attr
,
420 const char *buf
, size_t count
)
422 return test_dev_config_update_bool(buf
, count
,
423 &test_fw_config
->send_uevent
);
426 static ssize_t
config_send_uevent_show(struct device
*dev
,
427 struct device_attribute
*attr
,
430 return test_dev_config_show_bool(buf
, test_fw_config
->send_uevent
);
432 static DEVICE_ATTR_RW(config_send_uevent
);
434 static ssize_t
config_read_fw_idx_store(struct device
*dev
,
435 struct device_attribute
*attr
,
436 const char *buf
, size_t count
)
438 return test_dev_config_update_u8(buf
, count
,
439 &test_fw_config
->read_fw_idx
);
442 static ssize_t
config_read_fw_idx_show(struct device
*dev
,
443 struct device_attribute
*attr
,
446 return test_dev_config_show_u8(buf
, test_fw_config
->read_fw_idx
);
448 static DEVICE_ATTR_RW(config_read_fw_idx
);
451 static ssize_t
trigger_request_store(struct device
*dev
,
452 struct device_attribute
*attr
,
453 const char *buf
, size_t count
)
458 name
= kstrndup(buf
, count
, GFP_KERNEL
);
462 pr_info("loading '%s'\n", name
);
464 mutex_lock(&test_fw_mutex
);
465 release_firmware(test_firmware
);
466 test_firmware
= NULL
;
467 rc
= request_firmware(&test_firmware
, name
, dev
);
469 pr_info("load of '%s' failed: %d\n", name
, rc
);
472 pr_info("loaded: %zu\n", test_firmware
->size
);
476 mutex_unlock(&test_fw_mutex
);
482 static DEVICE_ATTR_WO(trigger_request
);
484 static DECLARE_COMPLETION(async_fw_done
);
486 static void trigger_async_request_cb(const struct firmware
*fw
, void *context
)
489 complete(&async_fw_done
);
492 static ssize_t
trigger_async_request_store(struct device
*dev
,
493 struct device_attribute
*attr
,
494 const char *buf
, size_t count
)
499 name
= kstrndup(buf
, count
, GFP_KERNEL
);
503 pr_info("loading '%s'\n", name
);
505 mutex_lock(&test_fw_mutex
);
506 release_firmware(test_firmware
);
507 test_firmware
= NULL
;
508 rc
= request_firmware_nowait(THIS_MODULE
, 1, name
, dev
, GFP_KERNEL
,
509 NULL
, trigger_async_request_cb
);
511 pr_info("async load of '%s' failed: %d\n", name
, rc
);
515 /* Free 'name' ASAP, to test for race conditions */
518 wait_for_completion(&async_fw_done
);
521 pr_info("loaded: %zu\n", test_firmware
->size
);
524 pr_err("failed to async load firmware\n");
529 mutex_unlock(&test_fw_mutex
);
533 static DEVICE_ATTR_WO(trigger_async_request
);
535 static ssize_t
trigger_custom_fallback_store(struct device
*dev
,
536 struct device_attribute
*attr
,
537 const char *buf
, size_t count
)
542 name
= kstrndup(buf
, count
, GFP_KERNEL
);
546 pr_info("loading '%s' using custom fallback mechanism\n", name
);
548 mutex_lock(&test_fw_mutex
);
549 release_firmware(test_firmware
);
550 test_firmware
= NULL
;
551 rc
= request_firmware_nowait(THIS_MODULE
, FW_ACTION_NOHOTPLUG
, name
,
552 dev
, GFP_KERNEL
, NULL
,
553 trigger_async_request_cb
);
555 pr_info("async load of '%s' failed: %d\n", name
, rc
);
559 /* Free 'name' ASAP, to test for race conditions */
562 wait_for_completion(&async_fw_done
);
565 pr_info("loaded: %zu\n", test_firmware
->size
);
568 pr_err("failed to async load firmware\n");
573 mutex_unlock(&test_fw_mutex
);
577 static DEVICE_ATTR_WO(trigger_custom_fallback
);
579 static int test_fw_run_batch_request(void *data
)
581 struct test_batched_req
*req
= data
;
584 test_fw_config
->test_result
= -EINVAL
;
588 req
->rc
= test_fw_config
->req_firmware(&req
->fw
, req
->name
, req
->dev
);
590 pr_info("#%u: batched sync load failed: %d\n",
592 if (!test_fw_config
->test_result
)
593 test_fw_config
->test_result
= req
->rc
;
594 } else if (req
->fw
) {
596 pr_info("#%u: batched sync loaded %zu\n",
597 req
->idx
, req
->fw
->size
);
599 complete(&req
->completion
);
607 * We use a kthread as otherwise the kernel serializes all our sync requests
608 * and we would not be able to mimic batched requests on a sync call. Batched
609 * requests on a sync call can for instance happen on a device driver when
610 * multiple cards are used and firmware loading happens outside of probe.
612 static ssize_t
trigger_batched_requests_store(struct device
*dev
,
613 struct device_attribute
*attr
,
614 const char *buf
, size_t count
)
616 struct test_batched_req
*req
;
620 mutex_lock(&test_fw_mutex
);
622 test_fw_config
->reqs
=
623 vzalloc(array3_size(sizeof(struct test_batched_req
),
624 test_fw_config
->num_requests
, 2));
625 if (!test_fw_config
->reqs
) {
630 pr_info("batched sync firmware loading '%s' %u times\n",
631 test_fw_config
->name
, test_fw_config
->num_requests
);
633 for (i
= 0; i
< test_fw_config
->num_requests
; i
++) {
634 req
= &test_fw_config
->reqs
[i
];
637 req
->name
= test_fw_config
->name
;
639 init_completion(&req
->completion
);
640 req
->task
= kthread_run(test_fw_run_batch_request
, req
,
641 "%s-%u", KBUILD_MODNAME
, req
->idx
);
642 if (!req
->task
|| IS_ERR(req
->task
)) {
643 pr_err("Setting up thread %u failed\n", req
->idx
);
653 * We require an explicit release to enable more time and delay of
654 * calling release_firmware() to improve our chances of forcing a
655 * batched request. If we instead called release_firmware() right away
656 * then we might miss on an opportunity of having a successful firmware
657 * request pass on the opportunity to be come a batched request.
661 for (i
= 0; i
< test_fw_config
->num_requests
; i
++) {
662 req
= &test_fw_config
->reqs
[i
];
663 if (req
->task
|| req
->sent
)
664 wait_for_completion(&req
->completion
);
667 /* Override any worker error if we had a general setup error */
669 test_fw_config
->test_result
= rc
;
672 mutex_unlock(&test_fw_mutex
);
676 static DEVICE_ATTR_WO(trigger_batched_requests
);
679 * We wait for each callback to return with the lock held, no need to lock here
681 static void trigger_batched_cb(const struct firmware
*fw
, void *context
)
683 struct test_batched_req
*req
= context
;
686 test_fw_config
->test_result
= -EINVAL
;
690 /* forces *some* batched requests to queue up */
697 * Unfortunately the firmware API gives us nothing other than a null FW
698 * if the firmware was not found on async requests. Best we can do is
699 * just assume -ENOENT. A better API would pass the actual return
700 * value to the callback.
702 if (!fw
&& !test_fw_config
->test_result
)
703 test_fw_config
->test_result
= -ENOENT
;
705 complete(&req
->completion
);
709 ssize_t
trigger_batched_requests_async_store(struct device
*dev
,
710 struct device_attribute
*attr
,
711 const char *buf
, size_t count
)
713 struct test_batched_req
*req
;
718 mutex_lock(&test_fw_mutex
);
720 test_fw_config
->reqs
=
721 vzalloc(array3_size(sizeof(struct test_batched_req
),
722 test_fw_config
->num_requests
, 2));
723 if (!test_fw_config
->reqs
) {
728 pr_info("batched loading '%s' custom fallback mechanism %u times\n",
729 test_fw_config
->name
, test_fw_config
->num_requests
);
731 send_uevent
= test_fw_config
->send_uevent
? FW_ACTION_HOTPLUG
:
734 for (i
= 0; i
< test_fw_config
->num_requests
; i
++) {
735 req
= &test_fw_config
->reqs
[i
];
736 req
->name
= test_fw_config
->name
;
739 init_completion(&req
->completion
);
740 rc
= request_firmware_nowait(THIS_MODULE
, send_uevent
,
742 dev
, GFP_KERNEL
, req
,
745 pr_info("#%u: batched async load failed setup: %d\n",
758 * We require an explicit release to enable more time and delay of
759 * calling release_firmware() to improve our chances of forcing a
760 * batched request. If we instead called release_firmware() right away
761 * then we might miss on an opportunity of having a successful firmware
762 * request pass on the opportunity to be come a batched request.
765 for (i
= 0; i
< test_fw_config
->num_requests
; i
++) {
766 req
= &test_fw_config
->reqs
[i
];
768 wait_for_completion(&req
->completion
);
771 /* Override any worker error if we had a general setup error */
773 test_fw_config
->test_result
= rc
;
776 mutex_unlock(&test_fw_mutex
);
780 static DEVICE_ATTR_WO(trigger_batched_requests_async
);
782 static ssize_t
test_result_show(struct device
*dev
,
783 struct device_attribute
*attr
,
786 return test_dev_config_show_int(buf
, test_fw_config
->test_result
);
788 static DEVICE_ATTR_RO(test_result
);
790 static ssize_t
release_all_firmware_store(struct device
*dev
,
791 struct device_attribute
*attr
,
792 const char *buf
, size_t count
)
794 test_release_all_firmware();
797 static DEVICE_ATTR_WO(release_all_firmware
);
799 static ssize_t
read_firmware_show(struct device
*dev
,
800 struct device_attribute
*attr
,
803 struct test_batched_req
*req
;
807 mutex_lock(&test_fw_mutex
);
809 idx
= test_fw_config
->read_fw_idx
;
810 if (idx
>= test_fw_config
->num_requests
) {
815 if (!test_fw_config
->reqs
) {
820 req
= &test_fw_config
->reqs
[idx
];
822 pr_err("#%u: failed to async load firmware\n", idx
);
827 pr_info("#%u: loaded %zu\n", idx
, req
->fw
->size
);
829 if (req
->fw
->size
> PAGE_SIZE
) {
830 pr_err("Testing interface must use PAGE_SIZE firmware for now\n");
834 memcpy(buf
, req
->fw
->data
, req
->fw
->size
);
838 mutex_unlock(&test_fw_mutex
);
842 static DEVICE_ATTR_RO(read_firmware
);
844 #define TEST_FW_DEV_ATTR(name) &dev_attr_##name.attr
846 static struct attribute
*test_dev_attrs
[] = {
847 TEST_FW_DEV_ATTR(reset
),
849 TEST_FW_DEV_ATTR(config
),
850 TEST_FW_DEV_ATTR(config_name
),
851 TEST_FW_DEV_ATTR(config_num_requests
),
852 TEST_FW_DEV_ATTR(config_sync_direct
),
853 TEST_FW_DEV_ATTR(config_send_uevent
),
854 TEST_FW_DEV_ATTR(config_read_fw_idx
),
856 /* These don't use the config at all - they could be ported! */
857 TEST_FW_DEV_ATTR(trigger_request
),
858 TEST_FW_DEV_ATTR(trigger_async_request
),
859 TEST_FW_DEV_ATTR(trigger_custom_fallback
),
861 /* These use the config and can use the test_result */
862 TEST_FW_DEV_ATTR(trigger_batched_requests
),
863 TEST_FW_DEV_ATTR(trigger_batched_requests_async
),
865 TEST_FW_DEV_ATTR(release_all_firmware
),
866 TEST_FW_DEV_ATTR(test_result
),
867 TEST_FW_DEV_ATTR(read_firmware
),
871 ATTRIBUTE_GROUPS(test_dev
);
873 static struct miscdevice test_fw_misc_device
= {
874 .minor
= MISC_DYNAMIC_MINOR
,
875 .name
= "test_firmware",
876 .fops
= &test_fw_fops
,
877 .groups
= test_dev_groups
,
880 static int __init
test_firmware_init(void)
884 test_fw_config
= kzalloc(sizeof(struct test_config
), GFP_KERNEL
);
888 rc
= __test_firmware_config_init();
892 rc
= misc_register(&test_fw_misc_device
);
894 kfree(test_fw_config
);
895 pr_err("could not register misc device: %d\n", rc
);
899 pr_warn("interface ready\n");
904 module_init(test_firmware_init
);
906 static void __exit
test_firmware_exit(void)
908 mutex_lock(&test_fw_mutex
);
909 release_firmware(test_firmware
);
910 misc_deregister(&test_fw_misc_device
);
911 __test_firmware_config_free();
912 kfree(test_fw_config
);
913 mutex_unlock(&test_fw_mutex
);
915 pr_warn("removed interface\n");
918 module_exit(test_firmware_exit
);
920 MODULE_AUTHOR("Kees Cook <keescook@chromium.org>");
921 MODULE_LICENSE("GPL");