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/sizes.h>
22 #include <linux/slab.h>
23 #include <linux/uaccess.h>
24 #include <linux/delay.h>
25 #include <linux/kthread.h>
26 #include <linux/vmalloc.h>
28 #define TEST_FIRMWARE_NAME "test-firmware.bin"
29 #define TEST_FIRMWARE_NUM_REQS 4
30 #define TEST_FIRMWARE_BUF_SIZE SZ_1K
32 static DEFINE_MUTEX(test_fw_mutex
);
33 static const struct firmware
*test_firmware
;
35 struct test_batched_req
{
39 const struct firmware
*fw
;
41 struct completion completion
;
42 struct task_struct
*task
;
47 * test_config - represents configuration for the test for different triggers
49 * @name: the name of the firmware file to look for
50 * @into_buf: when the into_buf is used if this is true
51 * request_firmware_into_buf() will be used instead.
52 * @sync_direct: when the sync trigger is used if this is true
53 * request_firmware_direct() will be used instead.
54 * @send_uevent: whether or not to send a uevent for async requests
55 * @num_requests: number of requests to try per test case. This is trigger
57 * @reqs: stores all requests information
58 * @read_fw_idx: index of thread from which we want to read firmware results
59 * from through the read_fw trigger.
60 * @test_result: a test may use this to collect the result from the call
61 * of the request_firmware*() calls used in their tests. In order of
62 * priority we always keep first any setup error. If no setup errors were
63 * found then we move on to the first error encountered while running the
64 * API. Note that for async calls this typically will be a successful
65 * result (0) unless of course you've used bogus parameters, or the system
66 * is out of memory. In the async case the callback is expected to do a
67 * bit more homework to figure out what happened, unfortunately the only
68 * information passed today on error is the fact that no firmware was
69 * found so we can only assume -ENOENT on async calls if the firmware is
72 * Errors you can expect:
76 * 0: success for sync, for async it means request was sent
77 * -EINVAL: invalid parameters or request
78 * -ENOENT: files not found
82 * -ENOMEM: memory pressure on system
83 * -ENODEV: out of number of devices to test
84 * -EINVAL: an unexpected error has occurred
85 * @req_firmware: if @sync_direct is true this is set to
86 * request_firmware_direct(), otherwise request_firmware()
97 * These below don't belong her but we'll move them once we create
98 * a struct fw_test_device and stuff the misc_dev under there later.
100 struct test_batched_req
*reqs
;
102 int (*req_firmware
)(const struct firmware
**fw
, const char *name
,
103 struct device
*device
);
106 static struct test_config
*test_fw_config
;
108 static ssize_t
test_fw_misc_read(struct file
*f
, char __user
*buf
,
109 size_t size
, loff_t
*offset
)
113 mutex_lock(&test_fw_mutex
);
115 rc
= simple_read_from_buffer(buf
, size
, offset
,
117 test_firmware
->size
);
118 mutex_unlock(&test_fw_mutex
);
122 static const struct file_operations test_fw_fops
= {
123 .owner
= THIS_MODULE
,
124 .read
= test_fw_misc_read
,
127 static void __test_release_all_firmware(void)
129 struct test_batched_req
*req
;
132 if (!test_fw_config
->reqs
)
135 for (i
= 0; i
< test_fw_config
->num_requests
; i
++) {
136 req
= &test_fw_config
->reqs
[i
];
138 release_firmware(req
->fw
);
141 vfree(test_fw_config
->reqs
);
142 test_fw_config
->reqs
= NULL
;
145 static void test_release_all_firmware(void)
147 mutex_lock(&test_fw_mutex
);
148 __test_release_all_firmware();
149 mutex_unlock(&test_fw_mutex
);
153 static void __test_firmware_config_free(void)
155 __test_release_all_firmware();
156 kfree_const(test_fw_config
->name
);
157 test_fw_config
->name
= NULL
;
161 * XXX: move to kstrncpy() once merged.
163 * Users should use kfree_const() when freeing these.
165 static int __kstrncpy(char **dst
, const char *name
, size_t count
, gfp_t gfp
)
167 *dst
= kstrndup(name
, count
, gfp
);
173 static int __test_firmware_config_init(void)
177 ret
= __kstrncpy(&test_fw_config
->name
, TEST_FIRMWARE_NAME
,
178 strlen(TEST_FIRMWARE_NAME
), GFP_KERNEL
);
182 test_fw_config
->num_requests
= TEST_FIRMWARE_NUM_REQS
;
183 test_fw_config
->send_uevent
= true;
184 test_fw_config
->into_buf
= false;
185 test_fw_config
->sync_direct
= false;
186 test_fw_config
->req_firmware
= request_firmware
;
187 test_fw_config
->test_result
= 0;
188 test_fw_config
->reqs
= NULL
;
193 __test_firmware_config_free();
197 static ssize_t
reset_store(struct device
*dev
,
198 struct device_attribute
*attr
,
199 const char *buf
, size_t count
)
203 mutex_lock(&test_fw_mutex
);
205 __test_firmware_config_free();
207 ret
= __test_firmware_config_init();
210 pr_err("could not alloc settings for config trigger: %d\n",
219 mutex_unlock(&test_fw_mutex
);
223 static DEVICE_ATTR_WO(reset
);
225 static ssize_t
config_show(struct device
*dev
,
226 struct device_attribute
*attr
,
231 mutex_lock(&test_fw_mutex
);
233 len
+= scnprintf(buf
, PAGE_SIZE
- len
,
234 "Custom trigger configuration for: %s\n",
237 if (test_fw_config
->name
)
238 len
+= scnprintf(buf
+len
, PAGE_SIZE
- len
,
240 test_fw_config
->name
);
242 len
+= scnprintf(buf
+len
, PAGE_SIZE
- len
,
245 len
+= scnprintf(buf
+len
, PAGE_SIZE
- len
,
246 "num_requests:\t%u\n", test_fw_config
->num_requests
);
248 len
+= scnprintf(buf
+len
, PAGE_SIZE
- len
,
249 "send_uevent:\t\t%s\n",
250 test_fw_config
->send_uevent
?
251 "FW_ACTION_HOTPLUG" :
252 "FW_ACTION_NOHOTPLUG");
253 len
+= scnprintf(buf
+len
, PAGE_SIZE
- len
,
255 test_fw_config
->into_buf
? "true" : "false");
256 len
+= scnprintf(buf
+len
, PAGE_SIZE
- len
,
257 "sync_direct:\t\t%s\n",
258 test_fw_config
->sync_direct
? "true" : "false");
259 len
+= scnprintf(buf
+len
, PAGE_SIZE
- len
,
260 "read_fw_idx:\t%u\n", test_fw_config
->read_fw_idx
);
262 mutex_unlock(&test_fw_mutex
);
266 static DEVICE_ATTR_RO(config
);
268 static ssize_t
config_name_store(struct device
*dev
,
269 struct device_attribute
*attr
,
270 const char *buf
, size_t count
)
274 mutex_lock(&test_fw_mutex
);
275 kfree_const(test_fw_config
->name
);
276 ret
= __kstrncpy(&test_fw_config
->name
, buf
, count
, GFP_KERNEL
);
277 mutex_unlock(&test_fw_mutex
);
283 * As per sysfs_kf_seq_show() the buf is max PAGE_SIZE.
285 static ssize_t
config_test_show_str(char *dst
,
290 mutex_lock(&test_fw_mutex
);
291 len
= snprintf(dst
, PAGE_SIZE
, "%s\n", src
);
292 mutex_unlock(&test_fw_mutex
);
297 static int test_dev_config_update_bool(const char *buf
, size_t size
,
302 mutex_lock(&test_fw_mutex
);
303 if (strtobool(buf
, cfg
) < 0)
307 mutex_unlock(&test_fw_mutex
);
313 test_dev_config_show_bool(char *buf
,
318 mutex_lock(&test_fw_mutex
);
320 mutex_unlock(&test_fw_mutex
);
322 return snprintf(buf
, PAGE_SIZE
, "%d\n", val
);
325 static ssize_t
test_dev_config_show_int(char *buf
, int cfg
)
329 mutex_lock(&test_fw_mutex
);
331 mutex_unlock(&test_fw_mutex
);
333 return snprintf(buf
, PAGE_SIZE
, "%d\n", val
);
336 static int test_dev_config_update_u8(const char *buf
, size_t size
, u8
*cfg
)
341 ret
= kstrtol(buf
, 10, &new);
348 mutex_lock(&test_fw_mutex
);
350 mutex_unlock(&test_fw_mutex
);
352 /* Always return full write size even if we didn't consume all */
356 static ssize_t
test_dev_config_show_u8(char *buf
, u8 cfg
)
360 mutex_lock(&test_fw_mutex
);
362 mutex_unlock(&test_fw_mutex
);
364 return snprintf(buf
, PAGE_SIZE
, "%u\n", val
);
367 static ssize_t
config_name_show(struct device
*dev
,
368 struct device_attribute
*attr
,
371 return config_test_show_str(buf
, test_fw_config
->name
);
373 static DEVICE_ATTR_RW(config_name
);
375 static ssize_t
config_num_requests_store(struct device
*dev
,
376 struct device_attribute
*attr
,
377 const char *buf
, size_t count
)
381 mutex_lock(&test_fw_mutex
);
382 if (test_fw_config
->reqs
) {
383 pr_err("Must call release_all_firmware prior to changing config\n");
385 mutex_unlock(&test_fw_mutex
);
388 mutex_unlock(&test_fw_mutex
);
390 rc
= test_dev_config_update_u8(buf
, count
,
391 &test_fw_config
->num_requests
);
397 static ssize_t
config_num_requests_show(struct device
*dev
,
398 struct device_attribute
*attr
,
401 return test_dev_config_show_u8(buf
, test_fw_config
->num_requests
);
403 static DEVICE_ATTR_RW(config_num_requests
);
405 static ssize_t
config_into_buf_store(struct device
*dev
,
406 struct device_attribute
*attr
,
407 const char *buf
, size_t count
)
409 return test_dev_config_update_bool(buf
,
411 &test_fw_config
->into_buf
);
414 static ssize_t
config_into_buf_show(struct device
*dev
,
415 struct device_attribute
*attr
,
418 return test_dev_config_show_bool(buf
, test_fw_config
->into_buf
);
420 static DEVICE_ATTR_RW(config_into_buf
);
422 static ssize_t
config_sync_direct_store(struct device
*dev
,
423 struct device_attribute
*attr
,
424 const char *buf
, size_t count
)
426 int rc
= test_dev_config_update_bool(buf
, count
,
427 &test_fw_config
->sync_direct
);
430 test_fw_config
->req_firmware
= test_fw_config
->sync_direct
?
431 request_firmware_direct
:
436 static ssize_t
config_sync_direct_show(struct device
*dev
,
437 struct device_attribute
*attr
,
440 return test_dev_config_show_bool(buf
, test_fw_config
->sync_direct
);
442 static DEVICE_ATTR_RW(config_sync_direct
);
444 static ssize_t
config_send_uevent_store(struct device
*dev
,
445 struct device_attribute
*attr
,
446 const char *buf
, size_t count
)
448 return test_dev_config_update_bool(buf
, count
,
449 &test_fw_config
->send_uevent
);
452 static ssize_t
config_send_uevent_show(struct device
*dev
,
453 struct device_attribute
*attr
,
456 return test_dev_config_show_bool(buf
, test_fw_config
->send_uevent
);
458 static DEVICE_ATTR_RW(config_send_uevent
);
460 static ssize_t
config_read_fw_idx_store(struct device
*dev
,
461 struct device_attribute
*attr
,
462 const char *buf
, size_t count
)
464 return test_dev_config_update_u8(buf
, count
,
465 &test_fw_config
->read_fw_idx
);
468 static ssize_t
config_read_fw_idx_show(struct device
*dev
,
469 struct device_attribute
*attr
,
472 return test_dev_config_show_u8(buf
, test_fw_config
->read_fw_idx
);
474 static DEVICE_ATTR_RW(config_read_fw_idx
);
477 static ssize_t
trigger_request_store(struct device
*dev
,
478 struct device_attribute
*attr
,
479 const char *buf
, size_t count
)
484 name
= kstrndup(buf
, count
, GFP_KERNEL
);
488 pr_info("loading '%s'\n", name
);
490 mutex_lock(&test_fw_mutex
);
491 release_firmware(test_firmware
);
492 test_firmware
= NULL
;
493 rc
= request_firmware(&test_firmware
, name
, dev
);
495 pr_info("load of '%s' failed: %d\n", name
, rc
);
498 pr_info("loaded: %zu\n", test_firmware
->size
);
502 mutex_unlock(&test_fw_mutex
);
508 static DEVICE_ATTR_WO(trigger_request
);
510 static DECLARE_COMPLETION(async_fw_done
);
512 static void trigger_async_request_cb(const struct firmware
*fw
, void *context
)
515 complete(&async_fw_done
);
518 static ssize_t
trigger_async_request_store(struct device
*dev
,
519 struct device_attribute
*attr
,
520 const char *buf
, size_t count
)
525 name
= kstrndup(buf
, count
, GFP_KERNEL
);
529 pr_info("loading '%s'\n", name
);
531 mutex_lock(&test_fw_mutex
);
532 release_firmware(test_firmware
);
533 test_firmware
= NULL
;
534 rc
= request_firmware_nowait(THIS_MODULE
, 1, name
, dev
, GFP_KERNEL
,
535 NULL
, trigger_async_request_cb
);
537 pr_info("async load of '%s' failed: %d\n", name
, rc
);
541 /* Free 'name' ASAP, to test for race conditions */
544 wait_for_completion(&async_fw_done
);
547 pr_info("loaded: %zu\n", test_firmware
->size
);
550 pr_err("failed to async load firmware\n");
555 mutex_unlock(&test_fw_mutex
);
559 static DEVICE_ATTR_WO(trigger_async_request
);
561 static ssize_t
trigger_custom_fallback_store(struct device
*dev
,
562 struct device_attribute
*attr
,
563 const char *buf
, size_t count
)
568 name
= kstrndup(buf
, count
, GFP_KERNEL
);
572 pr_info("loading '%s' using custom fallback mechanism\n", name
);
574 mutex_lock(&test_fw_mutex
);
575 release_firmware(test_firmware
);
576 test_firmware
= NULL
;
577 rc
= request_firmware_nowait(THIS_MODULE
, FW_ACTION_NOHOTPLUG
, name
,
578 dev
, GFP_KERNEL
, NULL
,
579 trigger_async_request_cb
);
581 pr_info("async load of '%s' failed: %d\n", name
, rc
);
585 /* Free 'name' ASAP, to test for race conditions */
588 wait_for_completion(&async_fw_done
);
591 pr_info("loaded: %zu\n", test_firmware
->size
);
594 pr_err("failed to async load firmware\n");
599 mutex_unlock(&test_fw_mutex
);
603 static DEVICE_ATTR_WO(trigger_custom_fallback
);
605 static int test_fw_run_batch_request(void *data
)
607 struct test_batched_req
*req
= data
;
610 test_fw_config
->test_result
= -EINVAL
;
614 if (test_fw_config
->into_buf
) {
617 test_buf
= kzalloc(TEST_FIRMWARE_BUF_SIZE
, GFP_KERNEL
);
621 req
->rc
= request_firmware_into_buf(&req
->fw
,
625 TEST_FIRMWARE_BUF_SIZE
);
629 req
->rc
= test_fw_config
->req_firmware(&req
->fw
,
635 pr_info("#%u: batched sync load failed: %d\n",
637 if (!test_fw_config
->test_result
)
638 test_fw_config
->test_result
= req
->rc
;
639 } else if (req
->fw
) {
641 pr_info("#%u: batched sync loaded %zu\n",
642 req
->idx
, req
->fw
->size
);
644 complete(&req
->completion
);
652 * We use a kthread as otherwise the kernel serializes all our sync requests
653 * and we would not be able to mimic batched requests on a sync call. Batched
654 * requests on a sync call can for instance happen on a device driver when
655 * multiple cards are used and firmware loading happens outside of probe.
657 static ssize_t
trigger_batched_requests_store(struct device
*dev
,
658 struct device_attribute
*attr
,
659 const char *buf
, size_t count
)
661 struct test_batched_req
*req
;
665 mutex_lock(&test_fw_mutex
);
667 test_fw_config
->reqs
=
668 vzalloc(array3_size(sizeof(struct test_batched_req
),
669 test_fw_config
->num_requests
, 2));
670 if (!test_fw_config
->reqs
) {
675 pr_info("batched sync firmware loading '%s' %u times\n",
676 test_fw_config
->name
, test_fw_config
->num_requests
);
678 for (i
= 0; i
< test_fw_config
->num_requests
; i
++) {
679 req
= &test_fw_config
->reqs
[i
];
682 req
->name
= test_fw_config
->name
;
684 init_completion(&req
->completion
);
685 req
->task
= kthread_run(test_fw_run_batch_request
, req
,
686 "%s-%u", KBUILD_MODNAME
, req
->idx
);
687 if (!req
->task
|| IS_ERR(req
->task
)) {
688 pr_err("Setting up thread %u failed\n", req
->idx
);
698 * We require an explicit release to enable more time and delay of
699 * calling release_firmware() to improve our chances of forcing a
700 * batched request. If we instead called release_firmware() right away
701 * then we might miss on an opportunity of having a successful firmware
702 * request pass on the opportunity to be come a batched request.
706 for (i
= 0; i
< test_fw_config
->num_requests
; i
++) {
707 req
= &test_fw_config
->reqs
[i
];
708 if (req
->task
|| req
->sent
)
709 wait_for_completion(&req
->completion
);
712 /* Override any worker error if we had a general setup error */
714 test_fw_config
->test_result
= rc
;
717 mutex_unlock(&test_fw_mutex
);
721 static DEVICE_ATTR_WO(trigger_batched_requests
);
724 * We wait for each callback to return with the lock held, no need to lock here
726 static void trigger_batched_cb(const struct firmware
*fw
, void *context
)
728 struct test_batched_req
*req
= context
;
731 test_fw_config
->test_result
= -EINVAL
;
735 /* forces *some* batched requests to queue up */
742 * Unfortunately the firmware API gives us nothing other than a null FW
743 * if the firmware was not found on async requests. Best we can do is
744 * just assume -ENOENT. A better API would pass the actual return
745 * value to the callback.
747 if (!fw
&& !test_fw_config
->test_result
)
748 test_fw_config
->test_result
= -ENOENT
;
750 complete(&req
->completion
);
754 ssize_t
trigger_batched_requests_async_store(struct device
*dev
,
755 struct device_attribute
*attr
,
756 const char *buf
, size_t count
)
758 struct test_batched_req
*req
;
763 mutex_lock(&test_fw_mutex
);
765 test_fw_config
->reqs
=
766 vzalloc(array3_size(sizeof(struct test_batched_req
),
767 test_fw_config
->num_requests
, 2));
768 if (!test_fw_config
->reqs
) {
773 pr_info("batched loading '%s' custom fallback mechanism %u times\n",
774 test_fw_config
->name
, test_fw_config
->num_requests
);
776 send_uevent
= test_fw_config
->send_uevent
? FW_ACTION_HOTPLUG
:
779 for (i
= 0; i
< test_fw_config
->num_requests
; i
++) {
780 req
= &test_fw_config
->reqs
[i
];
781 req
->name
= test_fw_config
->name
;
784 init_completion(&req
->completion
);
785 rc
= request_firmware_nowait(THIS_MODULE
, send_uevent
,
787 dev
, GFP_KERNEL
, req
,
790 pr_info("#%u: batched async load failed setup: %d\n",
803 * We require an explicit release to enable more time and delay of
804 * calling release_firmware() to improve our chances of forcing a
805 * batched request. If we instead called release_firmware() right away
806 * then we might miss on an opportunity of having a successful firmware
807 * request pass on the opportunity to be come a batched request.
810 for (i
= 0; i
< test_fw_config
->num_requests
; i
++) {
811 req
= &test_fw_config
->reqs
[i
];
813 wait_for_completion(&req
->completion
);
816 /* Override any worker error if we had a general setup error */
818 test_fw_config
->test_result
= rc
;
821 mutex_unlock(&test_fw_mutex
);
825 static DEVICE_ATTR_WO(trigger_batched_requests_async
);
827 static ssize_t
test_result_show(struct device
*dev
,
828 struct device_attribute
*attr
,
831 return test_dev_config_show_int(buf
, test_fw_config
->test_result
);
833 static DEVICE_ATTR_RO(test_result
);
835 static ssize_t
release_all_firmware_store(struct device
*dev
,
836 struct device_attribute
*attr
,
837 const char *buf
, size_t count
)
839 test_release_all_firmware();
842 static DEVICE_ATTR_WO(release_all_firmware
);
844 static ssize_t
read_firmware_show(struct device
*dev
,
845 struct device_attribute
*attr
,
848 struct test_batched_req
*req
;
852 mutex_lock(&test_fw_mutex
);
854 idx
= test_fw_config
->read_fw_idx
;
855 if (idx
>= test_fw_config
->num_requests
) {
860 if (!test_fw_config
->reqs
) {
865 req
= &test_fw_config
->reqs
[idx
];
867 pr_err("#%u: failed to async load firmware\n", idx
);
872 pr_info("#%u: loaded %zu\n", idx
, req
->fw
->size
);
874 if (req
->fw
->size
> PAGE_SIZE
) {
875 pr_err("Testing interface must use PAGE_SIZE firmware for now\n");
879 memcpy(buf
, req
->fw
->data
, req
->fw
->size
);
883 mutex_unlock(&test_fw_mutex
);
887 static DEVICE_ATTR_RO(read_firmware
);
889 #define TEST_FW_DEV_ATTR(name) &dev_attr_##name.attr
891 static struct attribute
*test_dev_attrs
[] = {
892 TEST_FW_DEV_ATTR(reset
),
894 TEST_FW_DEV_ATTR(config
),
895 TEST_FW_DEV_ATTR(config_name
),
896 TEST_FW_DEV_ATTR(config_num_requests
),
897 TEST_FW_DEV_ATTR(config_into_buf
),
898 TEST_FW_DEV_ATTR(config_sync_direct
),
899 TEST_FW_DEV_ATTR(config_send_uevent
),
900 TEST_FW_DEV_ATTR(config_read_fw_idx
),
902 /* These don't use the config at all - they could be ported! */
903 TEST_FW_DEV_ATTR(trigger_request
),
904 TEST_FW_DEV_ATTR(trigger_async_request
),
905 TEST_FW_DEV_ATTR(trigger_custom_fallback
),
907 /* These use the config and can use the test_result */
908 TEST_FW_DEV_ATTR(trigger_batched_requests
),
909 TEST_FW_DEV_ATTR(trigger_batched_requests_async
),
911 TEST_FW_DEV_ATTR(release_all_firmware
),
912 TEST_FW_DEV_ATTR(test_result
),
913 TEST_FW_DEV_ATTR(read_firmware
),
917 ATTRIBUTE_GROUPS(test_dev
);
919 static struct miscdevice test_fw_misc_device
= {
920 .minor
= MISC_DYNAMIC_MINOR
,
921 .name
= "test_firmware",
922 .fops
= &test_fw_fops
,
923 .groups
= test_dev_groups
,
926 static int __init
test_firmware_init(void)
930 test_fw_config
= kzalloc(sizeof(struct test_config
), GFP_KERNEL
);
934 rc
= __test_firmware_config_init();
936 kfree(test_fw_config
);
937 pr_err("could not init firmware test config: %d\n", rc
);
941 rc
= misc_register(&test_fw_misc_device
);
943 kfree(test_fw_config
);
944 pr_err("could not register misc device: %d\n", rc
);
948 pr_warn("interface ready\n");
953 module_init(test_firmware_init
);
955 static void __exit
test_firmware_exit(void)
957 mutex_lock(&test_fw_mutex
);
958 release_firmware(test_firmware
);
959 misc_deregister(&test_fw_misc_device
);
960 __test_firmware_config_free();
961 kfree(test_fw_config
);
962 mutex_unlock(&test_fw_mutex
);
964 pr_warn("removed interface\n");
967 module_exit(test_firmware_exit
);
969 MODULE_AUTHOR("Kees Cook <keescook@chromium.org>");
970 MODULE_LICENSE("GPL");