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;
48 struct completion completion;
49 struct task_struct *task;
54 * struct test_config - represents configuration for the test for different triggers
56 * @name: the name of the firmware file to look for
57 * @into_buf: when the into_buf is used if this is true
58 * request_firmware_into_buf() will be used instead.
59 * @buf_size: size of buf to allocate when into_buf is true
60 * @file_offset: file offset to request when calling request_firmware_into_buf
61 * @partial: partial read opt when calling request_firmware_into_buf
62 * @sync_direct: when the sync trigger is used if this is true
63 * request_firmware_direct() will be used instead.
64 * @send_uevent: whether or not to send a uevent for async requests
65 * @num_requests: number of requests to try per test case. This is trigger
67 * @reqs: stores all requests information
68 * @read_fw_idx: index of thread from which we want to read firmware results
69 * from through the read_fw trigger.
70 * @upload_name: firmware name to be used with upload_read sysfs node
71 * @test_result: a test may use this to collect the result from the call
72 * of the request_firmware*() calls used in their tests. In order of
73 * priority we always keep first any setup error. If no setup errors were
74 * found then we move on to the first error encountered while running the
75 * API. Note that for async calls this typically will be a successful
76 * result (0) unless of course you've used bogus parameters, or the system
77 * is out of memory. In the async case the callback is expected to do a
78 * bit more homework to figure out what happened, unfortunately the only
79 * information passed today on error is the fact that no firmware was
80 * found so we can only assume -ENOENT on async calls if the firmware is
83 * Errors you can expect:
87 * 0: success for sync, for async it means request was sent
88 * -EINVAL: invalid parameters or request
89 * -ENOENT: files not found
93 * -ENOMEM: memory pressure on system
94 * -ENODEV: out of number of devices to test
95 * -EINVAL: an unexpected error has occurred
96 * @req_firmware: if @sync_direct is true this is set to
97 * request_firmware_direct(), otherwise request_firmware()
112 * These below don't belong her but we'll move them once we create
113 * a struct fw_test_device and stuff the misc_dev under there later.
115 struct test_batched_req *reqs;
117 int (*req_firmware)(const struct firmware **fw, const char *name,
118 struct device *device);
121 struct upload_inject_err {
123 enum fw_upload_err err_code;
126 struct test_firmware_upload {
128 struct list_head node;
132 struct upload_inject_err inject;
133 struct fw_upload *fwl;
136 static struct test_config *test_fw_config;
138 static struct test_firmware_upload *upload_lookup_name(const char *name)
140 struct test_firmware_upload *tst;
142 list_for_each_entry(tst, &test_upload_list, node)
143 if (strncmp(name, tst->name, strlen(tst->name)) == 0)
149 static ssize_t test_fw_misc_read(struct file *f, char __user *buf,
150 size_t size, loff_t *offset)
154 mutex_lock(&test_fw_mutex);
156 rc = simple_read_from_buffer(buf, size, offset,
158 test_firmware->size);
159 mutex_unlock(&test_fw_mutex);
163 static const struct file_operations test_fw_fops = {
164 .owner = THIS_MODULE,
165 .read = test_fw_misc_read,
168 static void __test_release_all_firmware(void)
170 struct test_batched_req *req;
173 if (!test_fw_config->reqs)
176 for (i = 0; i < test_fw_config->num_requests; i++) {
177 req = &test_fw_config->reqs[i];
179 release_firmware(req->fw);
182 vfree(test_fw_config->reqs);
183 test_fw_config->reqs = NULL;
186 static void test_release_all_firmware(void)
188 mutex_lock(&test_fw_mutex);
189 __test_release_all_firmware();
190 mutex_unlock(&test_fw_mutex);
194 static void __test_firmware_config_free(void)
196 __test_release_all_firmware();
197 kfree_const(test_fw_config->name);
198 test_fw_config->name = NULL;
202 * XXX: move to kstrncpy() once merged.
204 * Users should use kfree_const() when freeing these.
206 static int __kstrncpy(char **dst, const char *name, size_t count, gfp_t gfp)
208 *dst = kstrndup(name, count, gfp);
214 static int __test_firmware_config_init(void)
218 ret = __kstrncpy(&test_fw_config->name, TEST_FIRMWARE_NAME,
219 strlen(TEST_FIRMWARE_NAME), GFP_KERNEL);
223 test_fw_config->num_requests = TEST_FIRMWARE_NUM_REQS;
224 test_fw_config->send_uevent = true;
225 test_fw_config->into_buf = false;
226 test_fw_config->buf_size = TEST_FIRMWARE_BUF_SIZE;
227 test_fw_config->file_offset = 0;
228 test_fw_config->partial = false;
229 test_fw_config->sync_direct = false;
230 test_fw_config->req_firmware = request_firmware;
231 test_fw_config->test_result = 0;
232 test_fw_config->reqs = NULL;
233 test_fw_config->upload_name = NULL;
238 __test_firmware_config_free();
242 static ssize_t reset_store(struct device *dev,
243 struct device_attribute *attr,
244 const char *buf, size_t count)
248 mutex_lock(&test_fw_mutex);
250 __test_firmware_config_free();
252 ret = __test_firmware_config_init();
255 pr_err("could not alloc settings for config trigger: %d\n",
264 mutex_unlock(&test_fw_mutex);
268 static DEVICE_ATTR_WO(reset);
270 static ssize_t config_show(struct device *dev,
271 struct device_attribute *attr,
276 mutex_lock(&test_fw_mutex);
278 len += scnprintf(buf, PAGE_SIZE - len,
279 "Custom trigger configuration for: %s\n",
282 if (test_fw_config->name)
283 len += scnprintf(buf + len, PAGE_SIZE - len,
285 test_fw_config->name);
287 len += scnprintf(buf + len, PAGE_SIZE - len,
290 len += scnprintf(buf + len, PAGE_SIZE - len,
291 "num_requests:\t%u\n", test_fw_config->num_requests);
293 len += scnprintf(buf + len, PAGE_SIZE - len,
294 "send_uevent:\t\t%s\n",
295 test_fw_config->send_uevent ?
297 "FW_ACTION_NOUEVENT");
298 len += scnprintf(buf + len, PAGE_SIZE - len,
300 test_fw_config->into_buf ? "true" : "false");
301 len += scnprintf(buf + len, PAGE_SIZE - len,
302 "buf_size:\t%zu\n", test_fw_config->buf_size);
303 len += scnprintf(buf + len, PAGE_SIZE - len,
304 "file_offset:\t%zu\n", test_fw_config->file_offset);
305 len += scnprintf(buf + len, PAGE_SIZE - len,
307 test_fw_config->partial ? "true" : "false");
308 len += scnprintf(buf + len, PAGE_SIZE - len,
309 "sync_direct:\t\t%s\n",
310 test_fw_config->sync_direct ? "true" : "false");
311 len += scnprintf(buf + len, PAGE_SIZE - len,
312 "read_fw_idx:\t%u\n", test_fw_config->read_fw_idx);
313 if (test_fw_config->upload_name)
314 len += scnprintf(buf + len, PAGE_SIZE - len,
315 "upload_name:\t%s\n",
316 test_fw_config->upload_name);
318 len += scnprintf(buf + len, PAGE_SIZE - len,
319 "upload_name:\tEMPTY\n");
321 mutex_unlock(&test_fw_mutex);
325 static DEVICE_ATTR_RO(config);
327 static ssize_t config_name_store(struct device *dev,
328 struct device_attribute *attr,
329 const char *buf, size_t count)
333 mutex_lock(&test_fw_mutex);
334 kfree_const(test_fw_config->name);
335 ret = __kstrncpy(&test_fw_config->name, buf, count, GFP_KERNEL);
336 mutex_unlock(&test_fw_mutex);
342 * As per sysfs_kf_seq_show() the buf is max PAGE_SIZE.
344 static ssize_t config_test_show_str(char *dst,
349 mutex_lock(&test_fw_mutex);
350 len = snprintf(dst, PAGE_SIZE, "%s\n", src);
351 mutex_unlock(&test_fw_mutex);
356 static int test_dev_config_update_bool(const char *buf, size_t size,
361 mutex_lock(&test_fw_mutex);
362 if (kstrtobool(buf, cfg) < 0)
366 mutex_unlock(&test_fw_mutex);
371 static ssize_t test_dev_config_show_bool(char *buf, bool val)
373 return snprintf(buf, PAGE_SIZE, "%d\n", val);
376 static int test_dev_config_update_size_t(const char *buf,
383 ret = kstrtol(buf, 10, &new);
387 mutex_lock(&test_fw_mutex);
388 *(size_t *)cfg = new;
389 mutex_unlock(&test_fw_mutex);
391 /* Always return full write size even if we didn't consume all */
395 static ssize_t test_dev_config_show_size_t(char *buf, size_t val)
397 return snprintf(buf, PAGE_SIZE, "%zu\n", val);
400 static ssize_t test_dev_config_show_int(char *buf, int val)
402 return snprintf(buf, PAGE_SIZE, "%d\n", val);
405 static int test_dev_config_update_u8(const char *buf, size_t size, u8 *cfg)
410 ret = kstrtou8(buf, 10, &val);
414 mutex_lock(&test_fw_mutex);
416 mutex_unlock(&test_fw_mutex);
418 /* Always return full write size even if we didn't consume all */
422 static ssize_t test_dev_config_show_u8(char *buf, u8 val)
424 return snprintf(buf, PAGE_SIZE, "%u\n", val);
427 static ssize_t config_name_show(struct device *dev,
428 struct device_attribute *attr,
431 return config_test_show_str(buf, test_fw_config->name);
433 static DEVICE_ATTR_RW(config_name);
435 static ssize_t config_upload_name_store(struct device *dev,
436 struct device_attribute *attr,
437 const char *buf, size_t count)
439 struct test_firmware_upload *tst;
442 mutex_lock(&test_fw_mutex);
443 tst = upload_lookup_name(buf);
445 test_fw_config->upload_name = tst->name;
448 mutex_unlock(&test_fw_mutex);
453 static ssize_t config_upload_name_show(struct device *dev,
454 struct device_attribute *attr,
457 return config_test_show_str(buf, test_fw_config->upload_name);
459 static DEVICE_ATTR_RW(config_upload_name);
461 static ssize_t config_num_requests_store(struct device *dev,
462 struct device_attribute *attr,
463 const char *buf, size_t count)
467 mutex_lock(&test_fw_mutex);
468 if (test_fw_config->reqs) {
469 pr_err("Must call release_all_firmware prior to changing config\n");
471 mutex_unlock(&test_fw_mutex);
474 mutex_unlock(&test_fw_mutex);
476 rc = test_dev_config_update_u8(buf, count,
477 &test_fw_config->num_requests);
483 static ssize_t config_num_requests_show(struct device *dev,
484 struct device_attribute *attr,
487 return test_dev_config_show_u8(buf, test_fw_config->num_requests);
489 static DEVICE_ATTR_RW(config_num_requests);
491 static ssize_t config_into_buf_store(struct device *dev,
492 struct device_attribute *attr,
493 const char *buf, size_t count)
495 return test_dev_config_update_bool(buf,
497 &test_fw_config->into_buf);
500 static ssize_t config_into_buf_show(struct device *dev,
501 struct device_attribute *attr,
504 return test_dev_config_show_bool(buf, test_fw_config->into_buf);
506 static DEVICE_ATTR_RW(config_into_buf);
508 static ssize_t config_buf_size_store(struct device *dev,
509 struct device_attribute *attr,
510 const char *buf, size_t count)
514 mutex_lock(&test_fw_mutex);
515 if (test_fw_config->reqs) {
516 pr_err("Must call release_all_firmware prior to changing config\n");
518 mutex_unlock(&test_fw_mutex);
521 mutex_unlock(&test_fw_mutex);
523 rc = test_dev_config_update_size_t(buf, count,
524 &test_fw_config->buf_size);
530 static ssize_t config_buf_size_show(struct device *dev,
531 struct device_attribute *attr,
534 return test_dev_config_show_size_t(buf, test_fw_config->buf_size);
536 static DEVICE_ATTR_RW(config_buf_size);
538 static ssize_t config_file_offset_store(struct device *dev,
539 struct device_attribute *attr,
540 const char *buf, size_t count)
544 mutex_lock(&test_fw_mutex);
545 if (test_fw_config->reqs) {
546 pr_err("Must call release_all_firmware prior to changing config\n");
548 mutex_unlock(&test_fw_mutex);
551 mutex_unlock(&test_fw_mutex);
553 rc = test_dev_config_update_size_t(buf, count,
554 &test_fw_config->file_offset);
560 static ssize_t config_file_offset_show(struct device *dev,
561 struct device_attribute *attr,
564 return test_dev_config_show_size_t(buf, test_fw_config->file_offset);
566 static DEVICE_ATTR_RW(config_file_offset);
568 static ssize_t config_partial_store(struct device *dev,
569 struct device_attribute *attr,
570 const char *buf, size_t count)
572 return test_dev_config_update_bool(buf,
574 &test_fw_config->partial);
577 static ssize_t config_partial_show(struct device *dev,
578 struct device_attribute *attr,
581 return test_dev_config_show_bool(buf, test_fw_config->partial);
583 static DEVICE_ATTR_RW(config_partial);
585 static ssize_t config_sync_direct_store(struct device *dev,
586 struct device_attribute *attr,
587 const char *buf, size_t count)
589 int rc = test_dev_config_update_bool(buf, count,
590 &test_fw_config->sync_direct);
593 test_fw_config->req_firmware = test_fw_config->sync_direct ?
594 request_firmware_direct :
599 static ssize_t config_sync_direct_show(struct device *dev,
600 struct device_attribute *attr,
603 return test_dev_config_show_bool(buf, test_fw_config->sync_direct);
605 static DEVICE_ATTR_RW(config_sync_direct);
607 static ssize_t config_send_uevent_store(struct device *dev,
608 struct device_attribute *attr,
609 const char *buf, size_t count)
611 return test_dev_config_update_bool(buf, count,
612 &test_fw_config->send_uevent);
615 static ssize_t config_send_uevent_show(struct device *dev,
616 struct device_attribute *attr,
619 return test_dev_config_show_bool(buf, test_fw_config->send_uevent);
621 static DEVICE_ATTR_RW(config_send_uevent);
623 static ssize_t config_read_fw_idx_store(struct device *dev,
624 struct device_attribute *attr,
625 const char *buf, size_t count)
627 return test_dev_config_update_u8(buf, count,
628 &test_fw_config->read_fw_idx);
631 static ssize_t config_read_fw_idx_show(struct device *dev,
632 struct device_attribute *attr,
635 return test_dev_config_show_u8(buf, test_fw_config->read_fw_idx);
637 static DEVICE_ATTR_RW(config_read_fw_idx);
640 static ssize_t trigger_request_store(struct device *dev,
641 struct device_attribute *attr,
642 const char *buf, size_t count)
647 name = kstrndup(buf, count, GFP_KERNEL);
651 pr_info("loading '%s'\n", name);
653 mutex_lock(&test_fw_mutex);
654 release_firmware(test_firmware);
655 test_firmware = NULL;
656 rc = request_firmware(&test_firmware, name, dev);
658 pr_info("load of '%s' failed: %d\n", name, rc);
661 pr_info("loaded: %zu\n", test_firmware->size);
665 mutex_unlock(&test_fw_mutex);
671 static DEVICE_ATTR_WO(trigger_request);
673 #ifdef CONFIG_EFI_EMBEDDED_FIRMWARE
674 extern struct list_head efi_embedded_fw_list;
675 extern bool efi_embedded_fw_checked;
677 static ssize_t trigger_request_platform_store(struct device *dev,
678 struct device_attribute *attr,
679 const char *buf, size_t count)
681 static const u8 test_data[] = {
682 0x55, 0xaa, 0x55, 0xaa, 0x01, 0x02, 0x03, 0x04,
683 0x55, 0xaa, 0x55, 0xaa, 0x05, 0x06, 0x07, 0x08,
684 0x55, 0xaa, 0x55, 0xaa, 0x10, 0x20, 0x30, 0x40,
685 0x55, 0xaa, 0x55, 0xaa, 0x50, 0x60, 0x70, 0x80
687 struct efi_embedded_fw efi_embedded_fw;
688 const struct firmware *firmware = NULL;
689 bool saved_efi_embedded_fw_checked;
693 name = kstrndup(buf, count, GFP_KERNEL);
697 pr_info("inserting test platform fw '%s'\n", name);
698 efi_embedded_fw.name = name;
699 efi_embedded_fw.data = (void *)test_data;
700 efi_embedded_fw.length = sizeof(test_data);
701 list_add(&efi_embedded_fw.list, &efi_embedded_fw_list);
702 saved_efi_embedded_fw_checked = efi_embedded_fw_checked;
703 efi_embedded_fw_checked = true;
705 pr_info("loading '%s'\n", name);
706 rc = firmware_request_platform(&firmware, name, dev);
708 pr_info("load of '%s' failed: %d\n", name, rc);
711 if (firmware->size != sizeof(test_data) ||
712 memcmp(firmware->data, test_data, sizeof(test_data)) != 0) {
713 pr_info("firmware contents mismatch for '%s'\n", name);
717 pr_info("loaded: %zu\n", firmware->size);
721 efi_embedded_fw_checked = saved_efi_embedded_fw_checked;
722 release_firmware(firmware);
723 list_del(&efi_embedded_fw.list);
728 static DEVICE_ATTR_WO(trigger_request_platform);
731 static DECLARE_COMPLETION(async_fw_done);
733 static void trigger_async_request_cb(const struct firmware *fw, void *context)
736 complete(&async_fw_done);
739 static ssize_t trigger_async_request_store(struct device *dev,
740 struct device_attribute *attr,
741 const char *buf, size_t count)
746 name = kstrndup(buf, count, GFP_KERNEL);
750 pr_info("loading '%s'\n", name);
752 mutex_lock(&test_fw_mutex);
753 release_firmware(test_firmware);
754 test_firmware = NULL;
755 rc = request_firmware_nowait(THIS_MODULE, 1, name, dev, GFP_KERNEL,
756 NULL, trigger_async_request_cb);
758 pr_info("async load of '%s' failed: %d\n", name, rc);
762 /* Free 'name' ASAP, to test for race conditions */
765 wait_for_completion(&async_fw_done);
768 pr_info("loaded: %zu\n", test_firmware->size);
771 pr_err("failed to async load firmware\n");
776 mutex_unlock(&test_fw_mutex);
780 static DEVICE_ATTR_WO(trigger_async_request);
782 static ssize_t trigger_custom_fallback_store(struct device *dev,
783 struct device_attribute *attr,
784 const char *buf, size_t count)
789 name = kstrndup(buf, count, GFP_KERNEL);
793 pr_info("loading '%s' using custom fallback mechanism\n", name);
795 mutex_lock(&test_fw_mutex);
796 release_firmware(test_firmware);
797 test_firmware = NULL;
798 rc = request_firmware_nowait(THIS_MODULE, FW_ACTION_NOUEVENT, name,
799 dev, GFP_KERNEL, NULL,
800 trigger_async_request_cb);
802 pr_info("async load of '%s' failed: %d\n", name, rc);
806 /* Free 'name' ASAP, to test for race conditions */
809 wait_for_completion(&async_fw_done);
812 pr_info("loaded: %zu\n", test_firmware->size);
815 pr_err("failed to async load firmware\n");
820 mutex_unlock(&test_fw_mutex);
824 static DEVICE_ATTR_WO(trigger_custom_fallback);
826 static int test_fw_run_batch_request(void *data)
828 struct test_batched_req *req = data;
831 test_fw_config->test_result = -EINVAL;
835 if (test_fw_config->into_buf) {
838 test_buf = kzalloc(TEST_FIRMWARE_BUF_SIZE, GFP_KERNEL);
842 if (test_fw_config->partial)
843 req->rc = request_partial_firmware_into_buf
848 test_fw_config->buf_size,
849 test_fw_config->file_offset);
851 req->rc = request_firmware_into_buf
856 test_fw_config->buf_size);
860 req->rc = test_fw_config->req_firmware(&req->fw,
866 pr_info("#%u: batched sync load failed: %d\n",
868 if (!test_fw_config->test_result)
869 test_fw_config->test_result = req->rc;
870 } else if (req->fw) {
872 pr_info("#%u: batched sync loaded %zu\n",
873 req->idx, req->fw->size);
875 complete(&req->completion);
883 * We use a kthread as otherwise the kernel serializes all our sync requests
884 * and we would not be able to mimic batched requests on a sync call. Batched
885 * requests on a sync call can for instance happen on a device driver when
886 * multiple cards are used and firmware loading happens outside of probe.
888 static ssize_t trigger_batched_requests_store(struct device *dev,
889 struct device_attribute *attr,
890 const char *buf, size_t count)
892 struct test_batched_req *req;
896 mutex_lock(&test_fw_mutex);
898 test_fw_config->reqs =
899 vzalloc(array3_size(sizeof(struct test_batched_req),
900 test_fw_config->num_requests, 2));
901 if (!test_fw_config->reqs) {
906 pr_info("batched sync firmware loading '%s' %u times\n",
907 test_fw_config->name, test_fw_config->num_requests);
909 for (i = 0; i < test_fw_config->num_requests; i++) {
910 req = &test_fw_config->reqs[i];
913 req->name = test_fw_config->name;
915 init_completion(&req->completion);
916 req->task = kthread_run(test_fw_run_batch_request, req,
917 "%s-%u", KBUILD_MODNAME, req->idx);
918 if (!req->task || IS_ERR(req->task)) {
919 pr_err("Setting up thread %u failed\n", req->idx);
929 * We require an explicit release to enable more time and delay of
930 * calling release_firmware() to improve our chances of forcing a
931 * batched request. If we instead called release_firmware() right away
932 * then we might miss on an opportunity of having a successful firmware
933 * request pass on the opportunity to be come a batched request.
937 for (i = 0; i < test_fw_config->num_requests; i++) {
938 req = &test_fw_config->reqs[i];
939 if (req->task || req->sent)
940 wait_for_completion(&req->completion);
943 /* Override any worker error if we had a general setup error */
945 test_fw_config->test_result = rc;
948 mutex_unlock(&test_fw_mutex);
952 static DEVICE_ATTR_WO(trigger_batched_requests);
955 * We wait for each callback to return with the lock held, no need to lock here
957 static void trigger_batched_cb(const struct firmware *fw, void *context)
959 struct test_batched_req *req = context;
962 test_fw_config->test_result = -EINVAL;
966 /* forces *some* batched requests to queue up */
973 * Unfortunately the firmware API gives us nothing other than a null FW
974 * if the firmware was not found on async requests. Best we can do is
975 * just assume -ENOENT. A better API would pass the actual return
976 * value to the callback.
978 if (!fw && !test_fw_config->test_result)
979 test_fw_config->test_result = -ENOENT;
981 complete(&req->completion);
985 ssize_t trigger_batched_requests_async_store(struct device *dev,
986 struct device_attribute *attr,
987 const char *buf, size_t count)
989 struct test_batched_req *req;
994 mutex_lock(&test_fw_mutex);
996 test_fw_config->reqs =
997 vzalloc(array3_size(sizeof(struct test_batched_req),
998 test_fw_config->num_requests, 2));
999 if (!test_fw_config->reqs) {
1004 pr_info("batched loading '%s' custom fallback mechanism %u times\n",
1005 test_fw_config->name, test_fw_config->num_requests);
1007 send_uevent = test_fw_config->send_uevent ? FW_ACTION_UEVENT :
1010 for (i = 0; i < test_fw_config->num_requests; i++) {
1011 req = &test_fw_config->reqs[i];
1012 req->name = test_fw_config->name;
1015 init_completion(&req->completion);
1016 rc = request_firmware_nowait(THIS_MODULE, send_uevent,
1018 dev, GFP_KERNEL, req,
1019 trigger_batched_cb);
1021 pr_info("#%u: batched async load failed setup: %d\n",
1034 * We require an explicit release to enable more time and delay of
1035 * calling release_firmware() to improve our chances of forcing a
1036 * batched request. If we instead called release_firmware() right away
1037 * then we might miss on an opportunity of having a successful firmware
1038 * request pass on the opportunity to be come a batched request.
1041 for (i = 0; i < test_fw_config->num_requests; i++) {
1042 req = &test_fw_config->reqs[i];
1044 wait_for_completion(&req->completion);
1047 /* Override any worker error if we had a general setup error */
1049 test_fw_config->test_result = rc;
1052 mutex_unlock(&test_fw_mutex);
1056 static DEVICE_ATTR_WO(trigger_batched_requests_async);
1058 static void upload_release(struct test_firmware_upload *tst)
1060 firmware_upload_unregister(tst->fwl);
1066 static void upload_release_all(void)
1068 struct test_firmware_upload *tst, *tmp;
1070 list_for_each_entry_safe(tst, tmp, &test_upload_list, node) {
1071 list_del(&tst->node);
1072 upload_release(tst);
1074 test_fw_config->upload_name = NULL;
1078 * This table is replicated from .../firmware_loader/sysfs_upload.c
1079 * and needs to be kept in sync.
1081 static const char * const fw_upload_err_str[] = {
1082 [FW_UPLOAD_ERR_NONE] = "none",
1083 [FW_UPLOAD_ERR_HW_ERROR] = "hw-error",
1084 [FW_UPLOAD_ERR_TIMEOUT] = "timeout",
1085 [FW_UPLOAD_ERR_CANCELED] = "user-abort",
1086 [FW_UPLOAD_ERR_BUSY] = "device-busy",
1087 [FW_UPLOAD_ERR_INVALID_SIZE] = "invalid-file-size",
1088 [FW_UPLOAD_ERR_RW_ERROR] = "read-write-error",
1089 [FW_UPLOAD_ERR_WEAROUT] = "flash-wearout",
1092 static void upload_err_inject_error(struct test_firmware_upload *tst,
1093 const u8 *p, const char *prog)
1095 enum fw_upload_err err;
1097 for (err = FW_UPLOAD_ERR_NONE + 1; err < FW_UPLOAD_ERR_MAX; err++) {
1098 if (strncmp(p, fw_upload_err_str[err],
1099 strlen(fw_upload_err_str[err])) == 0) {
1100 tst->inject.prog = prog;
1101 tst->inject.err_code = err;
1107 static void upload_err_inject_prog(struct test_firmware_upload *tst,
1110 static const char * const progs[] = {
1111 "preparing:", "transferring:", "programming:"
1115 for (i = 0; i < ARRAY_SIZE(progs); i++) {
1116 if (strncmp(p, progs[i], strlen(progs[i])) == 0) {
1117 upload_err_inject_error(tst, p + strlen(progs[i]),
1124 #define FIVE_MINUTES_MS (5 * 60 * 1000)
1125 static enum fw_upload_err
1126 fw_upload_wait_on_cancel(struct test_firmware_upload *tst)
1130 for (ms_delay = 0; ms_delay < FIVE_MINUTES_MS; ms_delay += 100) {
1132 if (tst->cancel_request)
1133 return FW_UPLOAD_ERR_CANCELED;
1135 return FW_UPLOAD_ERR_NONE;
1138 static enum fw_upload_err test_fw_upload_prepare(struct fw_upload *fwl,
1139 const u8 *data, u32 size)
1141 struct test_firmware_upload *tst = fwl->dd_handle;
1142 enum fw_upload_err ret = FW_UPLOAD_ERR_NONE;
1143 const char *progress = "preparing:";
1145 tst->cancel_request = false;
1147 if (!size || size > TEST_UPLOAD_MAX_SIZE) {
1148 ret = FW_UPLOAD_ERR_INVALID_SIZE;
1152 if (strncmp(data, "inject:", strlen("inject:")) == 0)
1153 upload_err_inject_prog(tst, data + strlen("inject:"));
1155 memset(tst->buf, 0, TEST_UPLOAD_MAX_SIZE);
1158 if (tst->inject.err_code == FW_UPLOAD_ERR_NONE ||
1159 strncmp(tst->inject.prog, progress, strlen(progress)) != 0)
1160 return FW_UPLOAD_ERR_NONE;
1162 if (tst->inject.err_code == FW_UPLOAD_ERR_CANCELED)
1163 ret = fw_upload_wait_on_cancel(tst);
1165 ret = tst->inject.err_code;
1169 * The cleanup op only executes if the prepare op succeeds.
1170 * If the prepare op fails, it must do it's own clean-up.
1172 tst->inject.err_code = FW_UPLOAD_ERR_NONE;
1173 tst->inject.prog = NULL;
1178 static enum fw_upload_err test_fw_upload_write(struct fw_upload *fwl,
1179 const u8 *data, u32 offset,
1180 u32 size, u32 *written)
1182 struct test_firmware_upload *tst = fwl->dd_handle;
1183 const char *progress = "transferring:";
1186 if (tst->cancel_request)
1187 return FW_UPLOAD_ERR_CANCELED;
1189 blk_size = min_t(u32, TEST_UPLOAD_BLK_SIZE, size);
1190 memcpy(tst->buf + offset, data + offset, blk_size);
1192 *written = blk_size;
1194 if (tst->inject.err_code == FW_UPLOAD_ERR_NONE ||
1195 strncmp(tst->inject.prog, progress, strlen(progress)) != 0)
1196 return FW_UPLOAD_ERR_NONE;
1198 if (tst->inject.err_code == FW_UPLOAD_ERR_CANCELED)
1199 return fw_upload_wait_on_cancel(tst);
1201 return tst->inject.err_code;
1204 static enum fw_upload_err test_fw_upload_complete(struct fw_upload *fwl)
1206 struct test_firmware_upload *tst = fwl->dd_handle;
1207 const char *progress = "programming:";
1209 if (tst->cancel_request)
1210 return FW_UPLOAD_ERR_CANCELED;
1212 if (tst->inject.err_code == FW_UPLOAD_ERR_NONE ||
1213 strncmp(tst->inject.prog, progress, strlen(progress)) != 0)
1214 return FW_UPLOAD_ERR_NONE;
1216 if (tst->inject.err_code == FW_UPLOAD_ERR_CANCELED)
1217 return fw_upload_wait_on_cancel(tst);
1219 return tst->inject.err_code;
1222 static void test_fw_upload_cancel(struct fw_upload *fwl)
1224 struct test_firmware_upload *tst = fwl->dd_handle;
1226 tst->cancel_request = true;
1229 static void test_fw_cleanup(struct fw_upload *fwl)
1231 struct test_firmware_upload *tst = fwl->dd_handle;
1233 tst->inject.err_code = FW_UPLOAD_ERR_NONE;
1234 tst->inject.prog = NULL;
1237 static const struct fw_upload_ops upload_test_ops = {
1238 .prepare = test_fw_upload_prepare,
1239 .write = test_fw_upload_write,
1240 .poll_complete = test_fw_upload_complete,
1241 .cancel = test_fw_upload_cancel,
1242 .cleanup = test_fw_cleanup
1245 static ssize_t upload_register_store(struct device *dev,
1246 struct device_attribute *attr,
1247 const char *buf, size_t count)
1249 struct test_firmware_upload *tst;
1250 struct fw_upload *fwl;
1254 name = kstrndup(buf, count, GFP_KERNEL);
1258 mutex_lock(&test_fw_mutex);
1259 tst = upload_lookup_name(name);
1265 tst = kzalloc(sizeof(*tst), GFP_KERNEL);
1272 tst->buf = kzalloc(TEST_UPLOAD_MAX_SIZE, GFP_KERNEL);
1278 fwl = firmware_upload_register(THIS_MODULE, dev, tst->name,
1279 &upload_test_ops, tst);
1286 list_add_tail(&tst->node, &test_upload_list);
1287 mutex_unlock(&test_fw_mutex);
1297 mutex_unlock(&test_fw_mutex);
1302 static DEVICE_ATTR_WO(upload_register);
1304 static ssize_t upload_unregister_store(struct device *dev,
1305 struct device_attribute *attr,
1306 const char *buf, size_t count)
1308 struct test_firmware_upload *tst;
1311 mutex_lock(&test_fw_mutex);
1312 tst = upload_lookup_name(buf);
1318 if (test_fw_config->upload_name == tst->name)
1319 test_fw_config->upload_name = NULL;
1321 list_del(&tst->node);
1322 upload_release(tst);
1325 mutex_unlock(&test_fw_mutex);
1328 static DEVICE_ATTR_WO(upload_unregister);
1330 static ssize_t test_result_show(struct device *dev,
1331 struct device_attribute *attr,
1334 return test_dev_config_show_int(buf, test_fw_config->test_result);
1336 static DEVICE_ATTR_RO(test_result);
1338 static ssize_t release_all_firmware_store(struct device *dev,
1339 struct device_attribute *attr,
1340 const char *buf, size_t count)
1342 test_release_all_firmware();
1345 static DEVICE_ATTR_WO(release_all_firmware);
1347 static ssize_t read_firmware_show(struct device *dev,
1348 struct device_attribute *attr,
1351 struct test_batched_req *req;
1355 mutex_lock(&test_fw_mutex);
1357 idx = test_fw_config->read_fw_idx;
1358 if (idx >= test_fw_config->num_requests) {
1363 if (!test_fw_config->reqs) {
1368 req = &test_fw_config->reqs[idx];
1370 pr_err("#%u: failed to async load firmware\n", idx);
1375 pr_info("#%u: loaded %zu\n", idx, req->fw->size);
1377 if (req->fw->size > PAGE_SIZE) {
1378 pr_err("Testing interface must use PAGE_SIZE firmware for now\n");
1382 memcpy(buf, req->fw->data, req->fw->size);
1386 mutex_unlock(&test_fw_mutex);
1390 static DEVICE_ATTR_RO(read_firmware);
1392 static ssize_t upload_read_show(struct device *dev,
1393 struct device_attribute *attr,
1396 struct test_firmware_upload *tst = NULL;
1397 struct test_firmware_upload *tst_iter;
1400 if (!test_fw_config->upload_name) {
1401 pr_err("Set config_upload_name before using upload_read\n");
1405 mutex_lock(&test_fw_mutex);
1406 list_for_each_entry(tst_iter, &test_upload_list, node)
1407 if (tst_iter->name == test_fw_config->upload_name) {
1413 pr_err("Firmware name not found: %s\n",
1414 test_fw_config->upload_name);
1418 if (tst->size > PAGE_SIZE) {
1419 pr_err("Testing interface must use PAGE_SIZE firmware for now\n");
1423 memcpy(buf, tst->buf, tst->size);
1426 mutex_unlock(&test_fw_mutex);
1429 static DEVICE_ATTR_RO(upload_read);
1431 #define TEST_FW_DEV_ATTR(name) &dev_attr_##name.attr
1433 static struct attribute *test_dev_attrs[] = {
1434 TEST_FW_DEV_ATTR(reset),
1436 TEST_FW_DEV_ATTR(config),
1437 TEST_FW_DEV_ATTR(config_name),
1438 TEST_FW_DEV_ATTR(config_num_requests),
1439 TEST_FW_DEV_ATTR(config_into_buf),
1440 TEST_FW_DEV_ATTR(config_buf_size),
1441 TEST_FW_DEV_ATTR(config_file_offset),
1442 TEST_FW_DEV_ATTR(config_partial),
1443 TEST_FW_DEV_ATTR(config_sync_direct),
1444 TEST_FW_DEV_ATTR(config_send_uevent),
1445 TEST_FW_DEV_ATTR(config_read_fw_idx),
1446 TEST_FW_DEV_ATTR(config_upload_name),
1448 /* These don't use the config at all - they could be ported! */
1449 TEST_FW_DEV_ATTR(trigger_request),
1450 TEST_FW_DEV_ATTR(trigger_async_request),
1451 TEST_FW_DEV_ATTR(trigger_custom_fallback),
1452 #ifdef CONFIG_EFI_EMBEDDED_FIRMWARE
1453 TEST_FW_DEV_ATTR(trigger_request_platform),
1456 /* These use the config and can use the test_result */
1457 TEST_FW_DEV_ATTR(trigger_batched_requests),
1458 TEST_FW_DEV_ATTR(trigger_batched_requests_async),
1460 TEST_FW_DEV_ATTR(release_all_firmware),
1461 TEST_FW_DEV_ATTR(test_result),
1462 TEST_FW_DEV_ATTR(read_firmware),
1463 TEST_FW_DEV_ATTR(upload_read),
1464 TEST_FW_DEV_ATTR(upload_register),
1465 TEST_FW_DEV_ATTR(upload_unregister),
1469 ATTRIBUTE_GROUPS(test_dev);
1471 static struct miscdevice test_fw_misc_device = {
1472 .minor = MISC_DYNAMIC_MINOR,
1473 .name = "test_firmware",
1474 .fops = &test_fw_fops,
1475 .groups = test_dev_groups,
1478 static int __init test_firmware_init(void)
1482 test_fw_config = kzalloc(sizeof(struct test_config), GFP_KERNEL);
1483 if (!test_fw_config)
1486 rc = __test_firmware_config_init();
1488 kfree(test_fw_config);
1489 pr_err("could not init firmware test config: %d\n", rc);
1493 rc = misc_register(&test_fw_misc_device);
1495 __test_firmware_config_free();
1496 kfree(test_fw_config);
1497 pr_err("could not register misc device: %d\n", rc);
1501 pr_warn("interface ready\n");
1506 module_init(test_firmware_init);
1508 static void __exit test_firmware_exit(void)
1510 mutex_lock(&test_fw_mutex);
1511 release_firmware(test_firmware);
1512 misc_deregister(&test_fw_misc_device);
1513 upload_release_all();
1514 __test_firmware_config_free();
1515 kfree(test_fw_config);
1516 mutex_unlock(&test_fw_mutex);
1518 pr_warn("removed interface\n");
1521 module_exit(test_firmware_exit);
1523 MODULE_AUTHOR("Kees Cook <keescook@chromium.org>");
1524 MODULE_LICENSE("GPL");