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/kstrtox.h>
26 #include <linux/kthread.h>
27 #include <linux/vmalloc.h>
28 #include <linux/efi_embedded_fw.h>
30 MODULE_IMPORT_NS("TEST_FIRMWARE");
32 #define TEST_FIRMWARE_NAME "test-firmware.bin"
33 #define TEST_FIRMWARE_NUM_REQS 4
34 #define TEST_FIRMWARE_BUF_SIZE SZ_1K
35 #define TEST_UPLOAD_MAX_SIZE SZ_2K
36 #define TEST_UPLOAD_BLK_SIZE 37 /* Avoid powers of two in testing */
38 static DEFINE_MUTEX(test_fw_mutex
);
39 static const struct firmware
*test_firmware
;
40 static LIST_HEAD(test_upload_list
);
42 struct test_batched_req
{
46 const struct firmware
*fw
;
49 struct completion completion
;
50 struct task_struct
*task
;
55 * struct test_config - represents configuration for the test for different triggers
57 * @name: the name of the firmware file to look for
58 * @into_buf: when the into_buf is used if this is true
59 * request_firmware_into_buf() will be used instead.
60 * @buf_size: size of buf to allocate when into_buf is true
61 * @file_offset: file offset to request when calling request_firmware_into_buf
62 * @partial: partial read opt when calling request_firmware_into_buf
63 * @sync_direct: when the sync trigger is used if this is true
64 * request_firmware_direct() will be used instead.
65 * @send_uevent: whether or not to send a uevent for async requests
66 * @num_requests: number of requests to try per test case. This is trigger
68 * @reqs: stores all requests information
69 * @read_fw_idx: index of thread from which we want to read firmware results
70 * from through the read_fw trigger.
71 * @upload_name: firmware name to be used with upload_read sysfs node
72 * @test_result: a test may use this to collect the result from the call
73 * of the request_firmware*() calls used in their tests. In order of
74 * priority we always keep first any setup error. If no setup errors were
75 * found then we move on to the first error encountered while running the
76 * API. Note that for async calls this typically will be a successful
77 * result (0) unless of course you've used bogus parameters, or the system
78 * is out of memory. In the async case the callback is expected to do a
79 * bit more homework to figure out what happened, unfortunately the only
80 * information passed today on error is the fact that no firmware was
81 * found so we can only assume -ENOENT on async calls if the firmware is
84 * Errors you can expect:
88 * 0: success for sync, for async it means request was sent
89 * -EINVAL: invalid parameters or request
90 * -ENOENT: files not found
94 * -ENOMEM: memory pressure on system
95 * -ENODEV: out of number of devices to test
96 * -EINVAL: an unexpected error has occurred
97 * @req_firmware: if @sync_direct is true this is set to
98 * request_firmware_direct(), otherwise request_firmware()
113 * These below don't belong her but we'll move them once we create
114 * a struct fw_test_device and stuff the misc_dev under there later.
116 struct test_batched_req
*reqs
;
118 int (*req_firmware
)(const struct firmware
**fw
, const char *name
,
119 struct device
*device
);
122 struct upload_inject_err
{
124 enum fw_upload_err err_code
;
127 struct test_firmware_upload
{
129 struct list_head node
;
133 struct upload_inject_err inject
;
134 struct fw_upload
*fwl
;
137 static struct test_config
*test_fw_config
;
139 static struct test_firmware_upload
*upload_lookup_name(const char *name
)
141 struct test_firmware_upload
*tst
;
143 list_for_each_entry(tst
, &test_upload_list
, node
)
144 if (strncmp(name
, tst
->name
, strlen(tst
->name
)) == 0)
150 static ssize_t
test_fw_misc_read(struct file
*f
, char __user
*buf
,
151 size_t size
, loff_t
*offset
)
155 mutex_lock(&test_fw_mutex
);
157 rc
= simple_read_from_buffer(buf
, size
, offset
,
159 test_firmware
->size
);
160 mutex_unlock(&test_fw_mutex
);
164 static const struct file_operations test_fw_fops
= {
165 .owner
= THIS_MODULE
,
166 .read
= test_fw_misc_read
,
169 static void __test_release_all_firmware(void)
171 struct test_batched_req
*req
;
174 if (!test_fw_config
->reqs
)
177 for (i
= 0; i
< test_fw_config
->num_requests
; i
++) {
178 req
= &test_fw_config
->reqs
[i
];
181 kfree_const(req
->fw_buf
);
184 release_firmware(req
->fw
);
189 vfree(test_fw_config
->reqs
);
190 test_fw_config
->reqs
= NULL
;
193 static void test_release_all_firmware(void)
195 mutex_lock(&test_fw_mutex
);
196 __test_release_all_firmware();
197 mutex_unlock(&test_fw_mutex
);
201 static void __test_firmware_config_free(void)
203 __test_release_all_firmware();
204 kfree_const(test_fw_config
->name
);
205 test_fw_config
->name
= NULL
;
209 * XXX: move to kstrncpy() once merged.
211 * Users should use kfree_const() when freeing these.
213 static int __kstrncpy(char **dst
, const char *name
, size_t count
, gfp_t gfp
)
215 *dst
= kstrndup(name
, count
, gfp
);
221 static int __test_firmware_config_init(void)
225 ret
= __kstrncpy(&test_fw_config
->name
, TEST_FIRMWARE_NAME
,
226 strlen(TEST_FIRMWARE_NAME
), GFP_KERNEL
);
230 test_fw_config
->num_requests
= TEST_FIRMWARE_NUM_REQS
;
231 test_fw_config
->send_uevent
= true;
232 test_fw_config
->into_buf
= false;
233 test_fw_config
->buf_size
= TEST_FIRMWARE_BUF_SIZE
;
234 test_fw_config
->file_offset
= 0;
235 test_fw_config
->partial
= false;
236 test_fw_config
->sync_direct
= false;
237 test_fw_config
->req_firmware
= request_firmware
;
238 test_fw_config
->test_result
= 0;
239 test_fw_config
->reqs
= NULL
;
240 test_fw_config
->upload_name
= NULL
;
245 __test_firmware_config_free();
249 static ssize_t
reset_store(struct device
*dev
,
250 struct device_attribute
*attr
,
251 const char *buf
, size_t count
)
255 mutex_lock(&test_fw_mutex
);
257 __test_firmware_config_free();
259 ret
= __test_firmware_config_init();
262 pr_err("could not alloc settings for config trigger: %d\n",
271 mutex_unlock(&test_fw_mutex
);
275 static DEVICE_ATTR_WO(reset
);
277 static ssize_t
config_show(struct device
*dev
,
278 struct device_attribute
*attr
,
283 mutex_lock(&test_fw_mutex
);
285 len
+= scnprintf(buf
, PAGE_SIZE
- len
,
286 "Custom trigger configuration for: %s\n",
289 if (test_fw_config
->name
)
290 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
,
292 test_fw_config
->name
);
294 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
,
297 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
,
298 "num_requests:\t%u\n", test_fw_config
->num_requests
);
300 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
,
301 "send_uevent:\t\t%s\n",
302 test_fw_config
->send_uevent
?
304 "FW_ACTION_NOUEVENT");
305 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
,
307 test_fw_config
->into_buf
? "true" : "false");
308 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
,
309 "buf_size:\t%zu\n", test_fw_config
->buf_size
);
310 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
,
311 "file_offset:\t%zu\n", test_fw_config
->file_offset
);
312 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
,
314 test_fw_config
->partial
? "true" : "false");
315 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
,
316 "sync_direct:\t\t%s\n",
317 test_fw_config
->sync_direct
? "true" : "false");
318 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
,
319 "read_fw_idx:\t%u\n", test_fw_config
->read_fw_idx
);
320 if (test_fw_config
->upload_name
)
321 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
,
322 "upload_name:\t%s\n",
323 test_fw_config
->upload_name
);
325 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
,
326 "upload_name:\tEMPTY\n");
328 mutex_unlock(&test_fw_mutex
);
332 static DEVICE_ATTR_RO(config
);
334 static ssize_t
config_name_store(struct device
*dev
,
335 struct device_attribute
*attr
,
336 const char *buf
, size_t count
)
340 mutex_lock(&test_fw_mutex
);
341 kfree_const(test_fw_config
->name
);
342 ret
= __kstrncpy(&test_fw_config
->name
, buf
, count
, GFP_KERNEL
);
343 mutex_unlock(&test_fw_mutex
);
349 * As per sysfs_kf_seq_show() the buf is max PAGE_SIZE.
351 static ssize_t
config_test_show_str(char *dst
,
356 mutex_lock(&test_fw_mutex
);
357 len
= snprintf(dst
, PAGE_SIZE
, "%s\n", src
);
358 mutex_unlock(&test_fw_mutex
);
363 static inline int __test_dev_config_update_bool(const char *buf
, size_t size
,
368 if (kstrtobool(buf
, cfg
) < 0)
376 static int test_dev_config_update_bool(const char *buf
, size_t size
,
381 mutex_lock(&test_fw_mutex
);
382 ret
= __test_dev_config_update_bool(buf
, size
, cfg
);
383 mutex_unlock(&test_fw_mutex
);
388 static ssize_t
test_dev_config_show_bool(char *buf
, bool val
)
390 return snprintf(buf
, PAGE_SIZE
, "%d\n", val
);
393 static int __test_dev_config_update_size_t(
401 ret
= kstrtol(buf
, 10, &new);
405 *(size_t *)cfg
= new;
407 /* Always return full write size even if we didn't consume all */
411 static ssize_t
test_dev_config_show_size_t(char *buf
, size_t val
)
413 return snprintf(buf
, PAGE_SIZE
, "%zu\n", val
);
416 static ssize_t
test_dev_config_show_int(char *buf
, int val
)
418 return snprintf(buf
, PAGE_SIZE
, "%d\n", val
);
421 static int __test_dev_config_update_u8(const char *buf
, size_t size
, u8
*cfg
)
426 ret
= kstrtou8(buf
, 10, &val
);
432 /* Always return full write size even if we didn't consume all */
436 static int test_dev_config_update_u8(const char *buf
, size_t size
, u8
*cfg
)
440 mutex_lock(&test_fw_mutex
);
441 ret
= __test_dev_config_update_u8(buf
, size
, cfg
);
442 mutex_unlock(&test_fw_mutex
);
447 static ssize_t
test_dev_config_show_u8(char *buf
, u8 val
)
449 return snprintf(buf
, PAGE_SIZE
, "%u\n", val
);
452 static ssize_t
config_name_show(struct device
*dev
,
453 struct device_attribute
*attr
,
456 return config_test_show_str(buf
, test_fw_config
->name
);
458 static DEVICE_ATTR_RW(config_name
);
460 static ssize_t
config_upload_name_store(struct device
*dev
,
461 struct device_attribute
*attr
,
462 const char *buf
, size_t count
)
464 struct test_firmware_upload
*tst
;
467 mutex_lock(&test_fw_mutex
);
468 tst
= upload_lookup_name(buf
);
470 test_fw_config
->upload_name
= tst
->name
;
473 mutex_unlock(&test_fw_mutex
);
478 static ssize_t
config_upload_name_show(struct device
*dev
,
479 struct device_attribute
*attr
,
482 return config_test_show_str(buf
, test_fw_config
->upload_name
);
484 static DEVICE_ATTR_RW(config_upload_name
);
486 static ssize_t
config_num_requests_store(struct device
*dev
,
487 struct device_attribute
*attr
,
488 const char *buf
, size_t count
)
492 mutex_lock(&test_fw_mutex
);
493 if (test_fw_config
->reqs
) {
494 pr_err("Must call release_all_firmware prior to changing config\n");
496 mutex_unlock(&test_fw_mutex
);
500 rc
= __test_dev_config_update_u8(buf
, count
,
501 &test_fw_config
->num_requests
);
502 mutex_unlock(&test_fw_mutex
);
508 static ssize_t
config_num_requests_show(struct device
*dev
,
509 struct device_attribute
*attr
,
512 return test_dev_config_show_u8(buf
, test_fw_config
->num_requests
);
514 static DEVICE_ATTR_RW(config_num_requests
);
516 static ssize_t
config_into_buf_store(struct device
*dev
,
517 struct device_attribute
*attr
,
518 const char *buf
, size_t count
)
520 return test_dev_config_update_bool(buf
,
522 &test_fw_config
->into_buf
);
525 static ssize_t
config_into_buf_show(struct device
*dev
,
526 struct device_attribute
*attr
,
529 return test_dev_config_show_bool(buf
, test_fw_config
->into_buf
);
531 static DEVICE_ATTR_RW(config_into_buf
);
533 static ssize_t
config_buf_size_store(struct device
*dev
,
534 struct device_attribute
*attr
,
535 const char *buf
, size_t count
)
539 mutex_lock(&test_fw_mutex
);
540 if (test_fw_config
->reqs
) {
541 pr_err("Must call release_all_firmware prior to changing config\n");
543 mutex_unlock(&test_fw_mutex
);
547 rc
= __test_dev_config_update_size_t(buf
, count
,
548 &test_fw_config
->buf_size
);
549 mutex_unlock(&test_fw_mutex
);
555 static ssize_t
config_buf_size_show(struct device
*dev
,
556 struct device_attribute
*attr
,
559 return test_dev_config_show_size_t(buf
, test_fw_config
->buf_size
);
561 static DEVICE_ATTR_RW(config_buf_size
);
563 static ssize_t
config_file_offset_store(struct device
*dev
,
564 struct device_attribute
*attr
,
565 const char *buf
, size_t count
)
569 mutex_lock(&test_fw_mutex
);
570 if (test_fw_config
->reqs
) {
571 pr_err("Must call release_all_firmware prior to changing config\n");
573 mutex_unlock(&test_fw_mutex
);
577 rc
= __test_dev_config_update_size_t(buf
, count
,
578 &test_fw_config
->file_offset
);
579 mutex_unlock(&test_fw_mutex
);
585 static ssize_t
config_file_offset_show(struct device
*dev
,
586 struct device_attribute
*attr
,
589 return test_dev_config_show_size_t(buf
, test_fw_config
->file_offset
);
591 static DEVICE_ATTR_RW(config_file_offset
);
593 static ssize_t
config_partial_store(struct device
*dev
,
594 struct device_attribute
*attr
,
595 const char *buf
, size_t count
)
597 return test_dev_config_update_bool(buf
,
599 &test_fw_config
->partial
);
602 static ssize_t
config_partial_show(struct device
*dev
,
603 struct device_attribute
*attr
,
606 return test_dev_config_show_bool(buf
, test_fw_config
->partial
);
608 static DEVICE_ATTR_RW(config_partial
);
610 static ssize_t
config_sync_direct_store(struct device
*dev
,
611 struct device_attribute
*attr
,
612 const char *buf
, size_t count
)
614 int rc
= test_dev_config_update_bool(buf
, count
,
615 &test_fw_config
->sync_direct
);
618 test_fw_config
->req_firmware
= test_fw_config
->sync_direct
?
619 request_firmware_direct
:
624 static ssize_t
config_sync_direct_show(struct device
*dev
,
625 struct device_attribute
*attr
,
628 return test_dev_config_show_bool(buf
, test_fw_config
->sync_direct
);
630 static DEVICE_ATTR_RW(config_sync_direct
);
632 static ssize_t
config_send_uevent_store(struct device
*dev
,
633 struct device_attribute
*attr
,
634 const char *buf
, size_t count
)
636 return test_dev_config_update_bool(buf
, count
,
637 &test_fw_config
->send_uevent
);
640 static ssize_t
config_send_uevent_show(struct device
*dev
,
641 struct device_attribute
*attr
,
644 return test_dev_config_show_bool(buf
, test_fw_config
->send_uevent
);
646 static DEVICE_ATTR_RW(config_send_uevent
);
648 static ssize_t
config_read_fw_idx_store(struct device
*dev
,
649 struct device_attribute
*attr
,
650 const char *buf
, size_t count
)
652 return test_dev_config_update_u8(buf
, count
,
653 &test_fw_config
->read_fw_idx
);
656 static ssize_t
config_read_fw_idx_show(struct device
*dev
,
657 struct device_attribute
*attr
,
660 return test_dev_config_show_u8(buf
, test_fw_config
->read_fw_idx
);
662 static DEVICE_ATTR_RW(config_read_fw_idx
);
665 static ssize_t
trigger_request_store(struct device
*dev
,
666 struct device_attribute
*attr
,
667 const char *buf
, size_t count
)
672 name
= kstrndup(buf
, count
, GFP_KERNEL
);
676 pr_info("loading '%s'\n", name
);
678 mutex_lock(&test_fw_mutex
);
679 release_firmware(test_firmware
);
680 if (test_fw_config
->reqs
)
681 __test_release_all_firmware();
682 test_firmware
= NULL
;
683 rc
= request_firmware(&test_firmware
, name
, dev
);
685 pr_info("load of '%s' failed: %d\n", name
, rc
);
688 pr_info("loaded: %zu\n", test_firmware
->size
);
692 mutex_unlock(&test_fw_mutex
);
698 static DEVICE_ATTR_WO(trigger_request
);
700 #ifdef CONFIG_EFI_EMBEDDED_FIRMWARE
701 extern struct list_head efi_embedded_fw_list
;
702 extern bool efi_embedded_fw_checked
;
704 static ssize_t
trigger_request_platform_store(struct device
*dev
,
705 struct device_attribute
*attr
,
706 const char *buf
, size_t count
)
708 static const u8 test_data
[] = {
709 0x55, 0xaa, 0x55, 0xaa, 0x01, 0x02, 0x03, 0x04,
710 0x55, 0xaa, 0x55, 0xaa, 0x05, 0x06, 0x07, 0x08,
711 0x55, 0xaa, 0x55, 0xaa, 0x10, 0x20, 0x30, 0x40,
712 0x55, 0xaa, 0x55, 0xaa, 0x50, 0x60, 0x70, 0x80
714 struct efi_embedded_fw efi_embedded_fw
;
715 const struct firmware
*firmware
= NULL
;
716 bool saved_efi_embedded_fw_checked
;
720 name
= kstrndup(buf
, count
, GFP_KERNEL
);
724 pr_info("inserting test platform fw '%s'\n", name
);
725 efi_embedded_fw
.name
= name
;
726 efi_embedded_fw
.data
= (void *)test_data
;
727 efi_embedded_fw
.length
= sizeof(test_data
);
728 list_add(&efi_embedded_fw
.list
, &efi_embedded_fw_list
);
729 saved_efi_embedded_fw_checked
= efi_embedded_fw_checked
;
730 efi_embedded_fw_checked
= true;
732 pr_info("loading '%s'\n", name
);
733 rc
= firmware_request_platform(&firmware
, name
, dev
);
735 pr_info("load of '%s' failed: %d\n", name
, rc
);
738 if (firmware
->size
!= sizeof(test_data
) ||
739 memcmp(firmware
->data
, test_data
, sizeof(test_data
)) != 0) {
740 pr_info("firmware contents mismatch for '%s'\n", name
);
744 pr_info("loaded: %zu\n", firmware
->size
);
748 efi_embedded_fw_checked
= saved_efi_embedded_fw_checked
;
749 release_firmware(firmware
);
750 list_del(&efi_embedded_fw
.list
);
755 static DEVICE_ATTR_WO(trigger_request_platform
);
758 static DECLARE_COMPLETION(async_fw_done
);
760 static void trigger_async_request_cb(const struct firmware
*fw
, void *context
)
763 complete(&async_fw_done
);
766 static ssize_t
trigger_async_request_store(struct device
*dev
,
767 struct device_attribute
*attr
,
768 const char *buf
, size_t count
)
773 name
= kstrndup(buf
, count
, GFP_KERNEL
);
777 pr_info("loading '%s'\n", name
);
779 mutex_lock(&test_fw_mutex
);
780 release_firmware(test_firmware
);
781 test_firmware
= NULL
;
782 if (test_fw_config
->reqs
)
783 __test_release_all_firmware();
784 rc
= request_firmware_nowait(THIS_MODULE
, 1, name
, dev
, GFP_KERNEL
,
785 NULL
, trigger_async_request_cb
);
787 pr_info("async load of '%s' failed: %d\n", name
, rc
);
791 /* Free 'name' ASAP, to test for race conditions */
794 wait_for_completion(&async_fw_done
);
797 pr_info("loaded: %zu\n", test_firmware
->size
);
800 pr_err("failed to async load firmware\n");
805 mutex_unlock(&test_fw_mutex
);
809 static DEVICE_ATTR_WO(trigger_async_request
);
811 static ssize_t
trigger_custom_fallback_store(struct device
*dev
,
812 struct device_attribute
*attr
,
813 const char *buf
, size_t count
)
818 name
= kstrndup(buf
, count
, GFP_KERNEL
);
822 pr_info("loading '%s' using custom fallback mechanism\n", name
);
824 mutex_lock(&test_fw_mutex
);
825 release_firmware(test_firmware
);
826 if (test_fw_config
->reqs
)
827 __test_release_all_firmware();
828 test_firmware
= NULL
;
829 rc
= request_firmware_nowait(THIS_MODULE
, FW_ACTION_NOUEVENT
, name
,
830 dev
, GFP_KERNEL
, NULL
,
831 trigger_async_request_cb
);
833 pr_info("async load of '%s' failed: %d\n", name
, rc
);
837 /* Free 'name' ASAP, to test for race conditions */
840 wait_for_completion(&async_fw_done
);
843 pr_info("loaded: %zu\n", test_firmware
->size
);
846 pr_err("failed to async load firmware\n");
851 mutex_unlock(&test_fw_mutex
);
855 static DEVICE_ATTR_WO(trigger_custom_fallback
);
857 static int test_fw_run_batch_request(void *data
)
859 struct test_batched_req
*req
= data
;
862 test_fw_config
->test_result
= -EINVAL
;
866 if (test_fw_config
->into_buf
) {
869 test_buf
= kzalloc(TEST_FIRMWARE_BUF_SIZE
, GFP_KERNEL
);
873 if (test_fw_config
->partial
)
874 req
->rc
= request_partial_firmware_into_buf
879 test_fw_config
->buf_size
,
880 test_fw_config
->file_offset
);
882 req
->rc
= request_firmware_into_buf
887 test_fw_config
->buf_size
);
891 req
->fw_buf
= test_buf
;
893 req
->rc
= test_fw_config
->req_firmware(&req
->fw
,
899 pr_info("#%u: batched sync load failed: %d\n",
901 if (!test_fw_config
->test_result
)
902 test_fw_config
->test_result
= req
->rc
;
903 } else if (req
->fw
) {
905 pr_info("#%u: batched sync loaded %zu\n",
906 req
->idx
, req
->fw
->size
);
908 complete(&req
->completion
);
916 * We use a kthread as otherwise the kernel serializes all our sync requests
917 * and we would not be able to mimic batched requests on a sync call. Batched
918 * requests on a sync call can for instance happen on a device driver when
919 * multiple cards are used and firmware loading happens outside of probe.
921 static ssize_t
trigger_batched_requests_store(struct device
*dev
,
922 struct device_attribute
*attr
,
923 const char *buf
, size_t count
)
925 struct test_batched_req
*req
;
929 mutex_lock(&test_fw_mutex
);
931 if (test_fw_config
->reqs
) {
936 test_fw_config
->reqs
=
937 vzalloc(array3_size(sizeof(struct test_batched_req
),
938 test_fw_config
->num_requests
, 2));
939 if (!test_fw_config
->reqs
) {
944 pr_info("batched sync firmware loading '%s' %u times\n",
945 test_fw_config
->name
, test_fw_config
->num_requests
);
947 for (i
= 0; i
< test_fw_config
->num_requests
; i
++) {
948 req
= &test_fw_config
->reqs
[i
];
951 req
->name
= test_fw_config
->name
;
954 init_completion(&req
->completion
);
955 req
->task
= kthread_run(test_fw_run_batch_request
, req
,
956 "%s-%u", KBUILD_MODNAME
, req
->idx
);
957 if (!req
->task
|| IS_ERR(req
->task
)) {
958 pr_err("Setting up thread %u failed\n", req
->idx
);
968 * We require an explicit release to enable more time and delay of
969 * calling release_firmware() to improve our chances of forcing a
970 * batched request. If we instead called release_firmware() right away
971 * then we might miss on an opportunity of having a successful firmware
972 * request pass on the opportunity to be come a batched request.
976 for (i
= 0; i
< test_fw_config
->num_requests
; i
++) {
977 req
= &test_fw_config
->reqs
[i
];
978 if (req
->task
|| req
->sent
)
979 wait_for_completion(&req
->completion
);
982 /* Override any worker error if we had a general setup error */
984 test_fw_config
->test_result
= rc
;
987 mutex_unlock(&test_fw_mutex
);
991 static DEVICE_ATTR_WO(trigger_batched_requests
);
994 * We wait for each callback to return with the lock held, no need to lock here
996 static void trigger_batched_cb(const struct firmware
*fw
, void *context
)
998 struct test_batched_req
*req
= context
;
1001 test_fw_config
->test_result
= -EINVAL
;
1005 /* forces *some* batched requests to queue up */
1012 * Unfortunately the firmware API gives us nothing other than a null FW
1013 * if the firmware was not found on async requests. Best we can do is
1014 * just assume -ENOENT. A better API would pass the actual return
1015 * value to the callback.
1017 if (!fw
&& !test_fw_config
->test_result
)
1018 test_fw_config
->test_result
= -ENOENT
;
1020 complete(&req
->completion
);
1024 ssize_t
trigger_batched_requests_async_store(struct device
*dev
,
1025 struct device_attribute
*attr
,
1026 const char *buf
, size_t count
)
1028 struct test_batched_req
*req
;
1033 mutex_lock(&test_fw_mutex
);
1035 if (test_fw_config
->reqs
) {
1040 test_fw_config
->reqs
=
1041 vzalloc(array3_size(sizeof(struct test_batched_req
),
1042 test_fw_config
->num_requests
, 2));
1043 if (!test_fw_config
->reqs
) {
1048 pr_info("batched loading '%s' custom fallback mechanism %u times\n",
1049 test_fw_config
->name
, test_fw_config
->num_requests
);
1051 send_uevent
= test_fw_config
->send_uevent
? FW_ACTION_UEVENT
:
1054 for (i
= 0; i
< test_fw_config
->num_requests
; i
++) {
1055 req
= &test_fw_config
->reqs
[i
];
1056 req
->name
= test_fw_config
->name
;
1060 init_completion(&req
->completion
);
1061 rc
= request_firmware_nowait(THIS_MODULE
, send_uevent
,
1063 dev
, GFP_KERNEL
, req
,
1064 trigger_batched_cb
);
1066 pr_info("#%u: batched async load failed setup: %d\n",
1079 * We require an explicit release to enable more time and delay of
1080 * calling release_firmware() to improve our chances of forcing a
1081 * batched request. If we instead called release_firmware() right away
1082 * then we might miss on an opportunity of having a successful firmware
1083 * request pass on the opportunity to be come a batched request.
1086 for (i
= 0; i
< test_fw_config
->num_requests
; i
++) {
1087 req
= &test_fw_config
->reqs
[i
];
1089 wait_for_completion(&req
->completion
);
1092 /* Override any worker error if we had a general setup error */
1094 test_fw_config
->test_result
= rc
;
1097 mutex_unlock(&test_fw_mutex
);
1101 static DEVICE_ATTR_WO(trigger_batched_requests_async
);
1103 static void upload_release(struct test_firmware_upload
*tst
)
1105 firmware_upload_unregister(tst
->fwl
);
1111 static void upload_release_all(void)
1113 struct test_firmware_upload
*tst
, *tmp
;
1115 list_for_each_entry_safe(tst
, tmp
, &test_upload_list
, node
) {
1116 list_del(&tst
->node
);
1117 upload_release(tst
);
1119 test_fw_config
->upload_name
= NULL
;
1123 * This table is replicated from .../firmware_loader/sysfs_upload.c
1124 * and needs to be kept in sync.
1126 static const char * const fw_upload_err_str
[] = {
1127 [FW_UPLOAD_ERR_NONE
] = "none",
1128 [FW_UPLOAD_ERR_HW_ERROR
] = "hw-error",
1129 [FW_UPLOAD_ERR_TIMEOUT
] = "timeout",
1130 [FW_UPLOAD_ERR_CANCELED
] = "user-abort",
1131 [FW_UPLOAD_ERR_BUSY
] = "device-busy",
1132 [FW_UPLOAD_ERR_INVALID_SIZE
] = "invalid-file-size",
1133 [FW_UPLOAD_ERR_RW_ERROR
] = "read-write-error",
1134 [FW_UPLOAD_ERR_WEAROUT
] = "flash-wearout",
1135 [FW_UPLOAD_ERR_FW_INVALID
] = "firmware-invalid",
1138 static void upload_err_inject_error(struct test_firmware_upload
*tst
,
1139 const u8
*p
, const char *prog
)
1141 enum fw_upload_err err
;
1143 for (err
= FW_UPLOAD_ERR_NONE
+ 1; err
< FW_UPLOAD_ERR_MAX
; err
++) {
1144 if (strncmp(p
, fw_upload_err_str
[err
],
1145 strlen(fw_upload_err_str
[err
])) == 0) {
1146 tst
->inject
.prog
= prog
;
1147 tst
->inject
.err_code
= err
;
1153 static void upload_err_inject_prog(struct test_firmware_upload
*tst
,
1156 static const char * const progs
[] = {
1157 "preparing:", "transferring:", "programming:"
1161 for (i
= 0; i
< ARRAY_SIZE(progs
); i
++) {
1162 if (strncmp(p
, progs
[i
], strlen(progs
[i
])) == 0) {
1163 upload_err_inject_error(tst
, p
+ strlen(progs
[i
]),
1170 #define FIVE_MINUTES_MS (5 * 60 * 1000)
1171 static enum fw_upload_err
1172 fw_upload_wait_on_cancel(struct test_firmware_upload
*tst
)
1176 for (ms_delay
= 0; ms_delay
< FIVE_MINUTES_MS
; ms_delay
+= 100) {
1178 if (tst
->cancel_request
)
1179 return FW_UPLOAD_ERR_CANCELED
;
1181 return FW_UPLOAD_ERR_NONE
;
1184 static enum fw_upload_err
test_fw_upload_prepare(struct fw_upload
*fwl
,
1185 const u8
*data
, u32 size
)
1187 struct test_firmware_upload
*tst
= fwl
->dd_handle
;
1188 enum fw_upload_err ret
= FW_UPLOAD_ERR_NONE
;
1189 const char *progress
= "preparing:";
1191 tst
->cancel_request
= false;
1193 if (!size
|| size
> TEST_UPLOAD_MAX_SIZE
) {
1194 ret
= FW_UPLOAD_ERR_INVALID_SIZE
;
1198 if (strncmp(data
, "inject:", strlen("inject:")) == 0)
1199 upload_err_inject_prog(tst
, data
+ strlen("inject:"));
1201 memset(tst
->buf
, 0, TEST_UPLOAD_MAX_SIZE
);
1204 if (tst
->inject
.err_code
== FW_UPLOAD_ERR_NONE
||
1205 strncmp(tst
->inject
.prog
, progress
, strlen(progress
)) != 0)
1206 return FW_UPLOAD_ERR_NONE
;
1208 if (tst
->inject
.err_code
== FW_UPLOAD_ERR_CANCELED
)
1209 ret
= fw_upload_wait_on_cancel(tst
);
1211 ret
= tst
->inject
.err_code
;
1215 * The cleanup op only executes if the prepare op succeeds.
1216 * If the prepare op fails, it must do it's own clean-up.
1218 tst
->inject
.err_code
= FW_UPLOAD_ERR_NONE
;
1219 tst
->inject
.prog
= NULL
;
1224 static enum fw_upload_err
test_fw_upload_write(struct fw_upload
*fwl
,
1225 const u8
*data
, u32 offset
,
1226 u32 size
, u32
*written
)
1228 struct test_firmware_upload
*tst
= fwl
->dd_handle
;
1229 const char *progress
= "transferring:";
1232 if (tst
->cancel_request
)
1233 return FW_UPLOAD_ERR_CANCELED
;
1235 blk_size
= min_t(u32
, TEST_UPLOAD_BLK_SIZE
, size
);
1236 memcpy(tst
->buf
+ offset
, data
+ offset
, blk_size
);
1238 *written
= blk_size
;
1240 if (tst
->inject
.err_code
== FW_UPLOAD_ERR_NONE
||
1241 strncmp(tst
->inject
.prog
, progress
, strlen(progress
)) != 0)
1242 return FW_UPLOAD_ERR_NONE
;
1244 if (tst
->inject
.err_code
== FW_UPLOAD_ERR_CANCELED
)
1245 return fw_upload_wait_on_cancel(tst
);
1247 return tst
->inject
.err_code
;
1250 static enum fw_upload_err
test_fw_upload_complete(struct fw_upload
*fwl
)
1252 struct test_firmware_upload
*tst
= fwl
->dd_handle
;
1253 const char *progress
= "programming:";
1255 if (tst
->cancel_request
)
1256 return FW_UPLOAD_ERR_CANCELED
;
1258 if (tst
->inject
.err_code
== FW_UPLOAD_ERR_NONE
||
1259 strncmp(tst
->inject
.prog
, progress
, strlen(progress
)) != 0)
1260 return FW_UPLOAD_ERR_NONE
;
1262 if (tst
->inject
.err_code
== FW_UPLOAD_ERR_CANCELED
)
1263 return fw_upload_wait_on_cancel(tst
);
1265 return tst
->inject
.err_code
;
1268 static void test_fw_upload_cancel(struct fw_upload
*fwl
)
1270 struct test_firmware_upload
*tst
= fwl
->dd_handle
;
1272 tst
->cancel_request
= true;
1275 static void test_fw_cleanup(struct fw_upload
*fwl
)
1277 struct test_firmware_upload
*tst
= fwl
->dd_handle
;
1279 tst
->inject
.err_code
= FW_UPLOAD_ERR_NONE
;
1280 tst
->inject
.prog
= NULL
;
1283 static const struct fw_upload_ops upload_test_ops
= {
1284 .prepare
= test_fw_upload_prepare
,
1285 .write
= test_fw_upload_write
,
1286 .poll_complete
= test_fw_upload_complete
,
1287 .cancel
= test_fw_upload_cancel
,
1288 .cleanup
= test_fw_cleanup
1291 static ssize_t
upload_register_store(struct device
*dev
,
1292 struct device_attribute
*attr
,
1293 const char *buf
, size_t count
)
1295 struct test_firmware_upload
*tst
;
1296 struct fw_upload
*fwl
;
1300 name
= kstrndup(buf
, count
, GFP_KERNEL
);
1304 mutex_lock(&test_fw_mutex
);
1305 tst
= upload_lookup_name(name
);
1311 tst
= kzalloc(sizeof(*tst
), GFP_KERNEL
);
1318 tst
->buf
= kzalloc(TEST_UPLOAD_MAX_SIZE
, GFP_KERNEL
);
1324 fwl
= firmware_upload_register(THIS_MODULE
, dev
, tst
->name
,
1325 &upload_test_ops
, tst
);
1332 list_add_tail(&tst
->node
, &test_upload_list
);
1333 mutex_unlock(&test_fw_mutex
);
1343 mutex_unlock(&test_fw_mutex
);
1348 static DEVICE_ATTR_WO(upload_register
);
1350 static ssize_t
upload_unregister_store(struct device
*dev
,
1351 struct device_attribute
*attr
,
1352 const char *buf
, size_t count
)
1354 struct test_firmware_upload
*tst
;
1357 mutex_lock(&test_fw_mutex
);
1358 tst
= upload_lookup_name(buf
);
1364 if (test_fw_config
->upload_name
== tst
->name
)
1365 test_fw_config
->upload_name
= NULL
;
1367 list_del(&tst
->node
);
1368 upload_release(tst
);
1371 mutex_unlock(&test_fw_mutex
);
1374 static DEVICE_ATTR_WO(upload_unregister
);
1376 static ssize_t
test_result_show(struct device
*dev
,
1377 struct device_attribute
*attr
,
1380 return test_dev_config_show_int(buf
, test_fw_config
->test_result
);
1382 static DEVICE_ATTR_RO(test_result
);
1384 static ssize_t
release_all_firmware_store(struct device
*dev
,
1385 struct device_attribute
*attr
,
1386 const char *buf
, size_t count
)
1388 test_release_all_firmware();
1391 static DEVICE_ATTR_WO(release_all_firmware
);
1393 static ssize_t
read_firmware_show(struct device
*dev
,
1394 struct device_attribute
*attr
,
1397 struct test_batched_req
*req
;
1401 mutex_lock(&test_fw_mutex
);
1403 idx
= test_fw_config
->read_fw_idx
;
1404 if (idx
>= test_fw_config
->num_requests
) {
1409 if (!test_fw_config
->reqs
) {
1414 req
= &test_fw_config
->reqs
[idx
];
1416 pr_err("#%u: failed to async load firmware\n", idx
);
1421 pr_info("#%u: loaded %zu\n", idx
, req
->fw
->size
);
1423 if (req
->fw
->size
> PAGE_SIZE
) {
1424 pr_err("Testing interface must use PAGE_SIZE firmware for now\n");
1428 memcpy(buf
, req
->fw
->data
, req
->fw
->size
);
1432 mutex_unlock(&test_fw_mutex
);
1436 static DEVICE_ATTR_RO(read_firmware
);
1438 static ssize_t
upload_read_show(struct device
*dev
,
1439 struct device_attribute
*attr
,
1442 struct test_firmware_upload
*tst
= NULL
;
1443 struct test_firmware_upload
*tst_iter
;
1446 if (!test_fw_config
->upload_name
) {
1447 pr_err("Set config_upload_name before using upload_read\n");
1451 mutex_lock(&test_fw_mutex
);
1452 list_for_each_entry(tst_iter
, &test_upload_list
, node
)
1453 if (tst_iter
->name
== test_fw_config
->upload_name
) {
1459 pr_err("Firmware name not found: %s\n",
1460 test_fw_config
->upload_name
);
1464 if (tst
->size
> PAGE_SIZE
) {
1465 pr_err("Testing interface must use PAGE_SIZE firmware for now\n");
1469 memcpy(buf
, tst
->buf
, tst
->size
);
1472 mutex_unlock(&test_fw_mutex
);
1475 static DEVICE_ATTR_RO(upload_read
);
1477 #define TEST_FW_DEV_ATTR(name) &dev_attr_##name.attr
1479 static struct attribute
*test_dev_attrs
[] = {
1480 TEST_FW_DEV_ATTR(reset
),
1482 TEST_FW_DEV_ATTR(config
),
1483 TEST_FW_DEV_ATTR(config_name
),
1484 TEST_FW_DEV_ATTR(config_num_requests
),
1485 TEST_FW_DEV_ATTR(config_into_buf
),
1486 TEST_FW_DEV_ATTR(config_buf_size
),
1487 TEST_FW_DEV_ATTR(config_file_offset
),
1488 TEST_FW_DEV_ATTR(config_partial
),
1489 TEST_FW_DEV_ATTR(config_sync_direct
),
1490 TEST_FW_DEV_ATTR(config_send_uevent
),
1491 TEST_FW_DEV_ATTR(config_read_fw_idx
),
1492 TEST_FW_DEV_ATTR(config_upload_name
),
1494 /* These don't use the config at all - they could be ported! */
1495 TEST_FW_DEV_ATTR(trigger_request
),
1496 TEST_FW_DEV_ATTR(trigger_async_request
),
1497 TEST_FW_DEV_ATTR(trigger_custom_fallback
),
1498 #ifdef CONFIG_EFI_EMBEDDED_FIRMWARE
1499 TEST_FW_DEV_ATTR(trigger_request_platform
),
1502 /* These use the config and can use the test_result */
1503 TEST_FW_DEV_ATTR(trigger_batched_requests
),
1504 TEST_FW_DEV_ATTR(trigger_batched_requests_async
),
1506 TEST_FW_DEV_ATTR(release_all_firmware
),
1507 TEST_FW_DEV_ATTR(test_result
),
1508 TEST_FW_DEV_ATTR(read_firmware
),
1509 TEST_FW_DEV_ATTR(upload_read
),
1510 TEST_FW_DEV_ATTR(upload_register
),
1511 TEST_FW_DEV_ATTR(upload_unregister
),
1515 ATTRIBUTE_GROUPS(test_dev
);
1517 static struct miscdevice test_fw_misc_device
= {
1518 .minor
= MISC_DYNAMIC_MINOR
,
1519 .name
= "test_firmware",
1520 .fops
= &test_fw_fops
,
1521 .groups
= test_dev_groups
,
1524 static int __init
test_firmware_init(void)
1528 test_fw_config
= kzalloc(sizeof(struct test_config
), GFP_KERNEL
);
1529 if (!test_fw_config
)
1532 rc
= __test_firmware_config_init();
1534 kfree(test_fw_config
);
1535 pr_err("could not init firmware test config: %d\n", rc
);
1539 rc
= misc_register(&test_fw_misc_device
);
1541 __test_firmware_config_free();
1542 kfree(test_fw_config
);
1543 pr_err("could not register misc device: %d\n", rc
);
1547 pr_warn("interface ready\n");
1552 module_init(test_firmware_init
);
1554 static void __exit
test_firmware_exit(void)
1556 mutex_lock(&test_fw_mutex
);
1557 release_firmware(test_firmware
);
1558 misc_deregister(&test_fw_misc_device
);
1559 upload_release_all();
1560 __test_firmware_config_free();
1561 kfree(test_fw_config
);
1562 mutex_unlock(&test_fw_mutex
);
1564 pr_warn("removed interface\n");
1567 module_exit(test_firmware_exit
);
1569 MODULE_AUTHOR("Kees Cook <keescook@chromium.org>");
1570 MODULE_DESCRIPTION("interface to trigger and test firmware loading");
1571 MODULE_LICENSE("GPL");