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>
27 #include <linux/efi_embedded_fw.h>
29 MODULE_IMPORT_NS(TEST_FIRMWARE);
31 #define TEST_FIRMWARE_NAME "test-firmware.bin"
32 #define TEST_FIRMWARE_NUM_REQS 4
33 #define TEST_FIRMWARE_BUF_SIZE SZ_1K
34 #define TEST_UPLOAD_MAX_SIZE SZ_2K
35 #define TEST_UPLOAD_BLK_SIZE 37 /* Avoid powers of two in testing */
37 static DEFINE_MUTEX(test_fw_mutex);
38 static const struct firmware *test_firmware;
39 static LIST_HEAD(test_upload_list);
41 struct test_batched_req {
45 const struct firmware *fw;
47 struct completion completion;
48 struct task_struct *task;
53 * test_config - represents configuration for the test for different triggers
55 * @name: the name of the firmware file to look for
56 * @into_buf: when the into_buf is used if this is true
57 * request_firmware_into_buf() will be used instead.
58 * @buf_size: size of buf to allocate when into_buf is true
59 * @file_offset: file offset to request when calling request_firmware_into_buf
60 * @partial: partial read opt when calling request_firmware_into_buf
61 * @sync_direct: when the sync trigger is used if this is true
62 * request_firmware_direct() will be used instead.
63 * @send_uevent: whether or not to send a uevent for async requests
64 * @num_requests: number of requests to try per test case. This is trigger
66 * @reqs: stores all requests information
67 * @read_fw_idx: index of thread from which we want to read firmware results
68 * from through the read_fw trigger.
69 * @upload_name: firmware name to be used with upload_read sysfs node
70 * @test_result: a test may use this to collect the result from the call
71 * of the request_firmware*() calls used in their tests. In order of
72 * priority we always keep first any setup error. If no setup errors were
73 * found then we move on to the first error encountered while running the
74 * API. Note that for async calls this typically will be a successful
75 * result (0) unless of course you've used bogus parameters, or the system
76 * is out of memory. In the async case the callback is expected to do a
77 * bit more homework to figure out what happened, unfortunately the only
78 * information passed today on error is the fact that no firmware was
79 * found so we can only assume -ENOENT on async calls if the firmware is
82 * Errors you can expect:
86 * 0: success for sync, for async it means request was sent
87 * -EINVAL: invalid parameters or request
88 * -ENOENT: files not found
92 * -ENOMEM: memory pressure on system
93 * -ENODEV: out of number of devices to test
94 * -EINVAL: an unexpected error has occurred
95 * @req_firmware: if @sync_direct is true this is set to
96 * request_firmware_direct(), otherwise request_firmware()
111 * These below don't belong her but we'll move them once we create
112 * a struct fw_test_device and stuff the misc_dev under there later.
114 struct test_batched_req *reqs;
116 int (*req_firmware)(const struct firmware **fw, const char *name,
117 struct device *device);
120 struct upload_inject_err {
122 enum fw_upload_err err_code;
125 struct test_firmware_upload {
127 struct list_head node;
131 struct upload_inject_err inject;
132 struct fw_upload *fwl;
135 static struct test_config *test_fw_config;
137 static struct test_firmware_upload *upload_lookup_name(const char *name)
139 struct test_firmware_upload *tst;
141 list_for_each_entry(tst, &test_upload_list, node)
142 if (strncmp(name, tst->name, strlen(tst->name)) == 0)
148 static ssize_t test_fw_misc_read(struct file *f, char __user *buf,
149 size_t size, loff_t *offset)
153 mutex_lock(&test_fw_mutex);
155 rc = simple_read_from_buffer(buf, size, offset,
157 test_firmware->size);
158 mutex_unlock(&test_fw_mutex);
162 static const struct file_operations test_fw_fops = {
163 .owner = THIS_MODULE,
164 .read = test_fw_misc_read,
167 static void __test_release_all_firmware(void)
169 struct test_batched_req *req;
172 if (!test_fw_config->reqs)
175 for (i = 0; i < test_fw_config->num_requests; i++) {
176 req = &test_fw_config->reqs[i];
178 release_firmware(req->fw);
181 vfree(test_fw_config->reqs);
182 test_fw_config->reqs = NULL;
185 static void test_release_all_firmware(void)
187 mutex_lock(&test_fw_mutex);
188 __test_release_all_firmware();
189 mutex_unlock(&test_fw_mutex);
193 static void __test_firmware_config_free(void)
195 __test_release_all_firmware();
196 kfree_const(test_fw_config->name);
197 test_fw_config->name = NULL;
201 * XXX: move to kstrncpy() once merged.
203 * Users should use kfree_const() when freeing these.
205 static int __kstrncpy(char **dst, const char *name, size_t count, gfp_t gfp)
207 *dst = kstrndup(name, count, gfp);
213 static int __test_firmware_config_init(void)
217 ret = __kstrncpy(&test_fw_config->name, TEST_FIRMWARE_NAME,
218 strlen(TEST_FIRMWARE_NAME), GFP_KERNEL);
222 test_fw_config->num_requests = TEST_FIRMWARE_NUM_REQS;
223 test_fw_config->send_uevent = true;
224 test_fw_config->into_buf = false;
225 test_fw_config->buf_size = TEST_FIRMWARE_BUF_SIZE;
226 test_fw_config->file_offset = 0;
227 test_fw_config->partial = false;
228 test_fw_config->sync_direct = false;
229 test_fw_config->req_firmware = request_firmware;
230 test_fw_config->test_result = 0;
231 test_fw_config->reqs = NULL;
232 test_fw_config->upload_name = NULL;
237 __test_firmware_config_free();
241 static ssize_t reset_store(struct device *dev,
242 struct device_attribute *attr,
243 const char *buf, size_t count)
247 mutex_lock(&test_fw_mutex);
249 __test_firmware_config_free();
251 ret = __test_firmware_config_init();
254 pr_err("could not alloc settings for config trigger: %d\n",
263 mutex_unlock(&test_fw_mutex);
267 static DEVICE_ATTR_WO(reset);
269 static ssize_t config_show(struct device *dev,
270 struct device_attribute *attr,
275 mutex_lock(&test_fw_mutex);
277 len += scnprintf(buf, PAGE_SIZE - len,
278 "Custom trigger configuration for: %s\n",
281 if (test_fw_config->name)
282 len += scnprintf(buf + len, PAGE_SIZE - len,
284 test_fw_config->name);
286 len += scnprintf(buf + len, PAGE_SIZE - len,
289 len += scnprintf(buf + len, PAGE_SIZE - len,
290 "num_requests:\t%u\n", test_fw_config->num_requests);
292 len += scnprintf(buf + len, PAGE_SIZE - len,
293 "send_uevent:\t\t%s\n",
294 test_fw_config->send_uevent ?
296 "FW_ACTION_NOUEVENT");
297 len += scnprintf(buf + len, PAGE_SIZE - len,
299 test_fw_config->into_buf ? "true" : "false");
300 len += scnprintf(buf + len, PAGE_SIZE - len,
301 "buf_size:\t%zu\n", test_fw_config->buf_size);
302 len += scnprintf(buf + len, PAGE_SIZE - len,
303 "file_offset:\t%zu\n", test_fw_config->file_offset);
304 len += scnprintf(buf + len, PAGE_SIZE - len,
306 test_fw_config->partial ? "true" : "false");
307 len += scnprintf(buf + len, PAGE_SIZE - len,
308 "sync_direct:\t\t%s\n",
309 test_fw_config->sync_direct ? "true" : "false");
310 len += scnprintf(buf + len, PAGE_SIZE - len,
311 "read_fw_idx:\t%u\n", test_fw_config->read_fw_idx);
312 if (test_fw_config->upload_name)
313 len += scnprintf(buf + len, PAGE_SIZE - len,
314 "upload_name:\t%s\n",
315 test_fw_config->upload_name);
317 len += scnprintf(buf + len, PAGE_SIZE - len,
318 "upload_name:\tEMTPY\n");
320 mutex_unlock(&test_fw_mutex);
324 static DEVICE_ATTR_RO(config);
326 static ssize_t config_name_store(struct device *dev,
327 struct device_attribute *attr,
328 const char *buf, size_t count)
332 mutex_lock(&test_fw_mutex);
333 kfree_const(test_fw_config->name);
334 ret = __kstrncpy(&test_fw_config->name, buf, count, GFP_KERNEL);
335 mutex_unlock(&test_fw_mutex);
341 * As per sysfs_kf_seq_show() the buf is max PAGE_SIZE.
343 static ssize_t config_test_show_str(char *dst,
348 mutex_lock(&test_fw_mutex);
349 len = snprintf(dst, PAGE_SIZE, "%s\n", src);
350 mutex_unlock(&test_fw_mutex);
355 static int test_dev_config_update_bool(const char *buf, size_t size,
360 mutex_lock(&test_fw_mutex);
361 if (strtobool(buf, cfg) < 0)
365 mutex_unlock(&test_fw_mutex);
370 static ssize_t test_dev_config_show_bool(char *buf, bool val)
372 return snprintf(buf, PAGE_SIZE, "%d\n", val);
375 static int test_dev_config_update_size_t(const char *buf,
382 ret = kstrtol(buf, 10, &new);
386 mutex_lock(&test_fw_mutex);
387 *(size_t *)cfg = new;
388 mutex_unlock(&test_fw_mutex);
390 /* Always return full write size even if we didn't consume all */
394 static ssize_t test_dev_config_show_size_t(char *buf, size_t val)
396 return snprintf(buf, PAGE_SIZE, "%zu\n", val);
399 static ssize_t test_dev_config_show_int(char *buf, int val)
401 return snprintf(buf, PAGE_SIZE, "%d\n", val);
404 static int test_dev_config_update_u8(const char *buf, size_t size, u8 *cfg)
409 ret = kstrtou8(buf, 10, &val);
413 mutex_lock(&test_fw_mutex);
415 mutex_unlock(&test_fw_mutex);
417 /* Always return full write size even if we didn't consume all */
421 static ssize_t test_dev_config_show_u8(char *buf, u8 val)
423 return snprintf(buf, PAGE_SIZE, "%u\n", val);
426 static ssize_t config_name_show(struct device *dev,
427 struct device_attribute *attr,
430 return config_test_show_str(buf, test_fw_config->name);
432 static DEVICE_ATTR_RW(config_name);
434 static ssize_t config_upload_name_store(struct device *dev,
435 struct device_attribute *attr,
436 const char *buf, size_t count)
438 struct test_firmware_upload *tst;
441 mutex_lock(&test_fw_mutex);
442 tst = upload_lookup_name(buf);
444 test_fw_config->upload_name = tst->name;
447 mutex_unlock(&test_fw_mutex);
452 static ssize_t config_upload_name_show(struct device *dev,
453 struct device_attribute *attr,
456 return config_test_show_str(buf, test_fw_config->upload_name);
458 static DEVICE_ATTR_RW(config_upload_name);
460 static ssize_t config_num_requests_store(struct device *dev,
461 struct device_attribute *attr,
462 const char *buf, size_t count)
466 mutex_lock(&test_fw_mutex);
467 if (test_fw_config->reqs) {
468 pr_err("Must call release_all_firmware prior to changing config\n");
470 mutex_unlock(&test_fw_mutex);
473 mutex_unlock(&test_fw_mutex);
475 rc = test_dev_config_update_u8(buf, count,
476 &test_fw_config->num_requests);
482 static ssize_t config_num_requests_show(struct device *dev,
483 struct device_attribute *attr,
486 return test_dev_config_show_u8(buf, test_fw_config->num_requests);
488 static DEVICE_ATTR_RW(config_num_requests);
490 static ssize_t config_into_buf_store(struct device *dev,
491 struct device_attribute *attr,
492 const char *buf, size_t count)
494 return test_dev_config_update_bool(buf,
496 &test_fw_config->into_buf);
499 static ssize_t config_into_buf_show(struct device *dev,
500 struct device_attribute *attr,
503 return test_dev_config_show_bool(buf, test_fw_config->into_buf);
505 static DEVICE_ATTR_RW(config_into_buf);
507 static ssize_t config_buf_size_store(struct device *dev,
508 struct device_attribute *attr,
509 const char *buf, size_t count)
513 mutex_lock(&test_fw_mutex);
514 if (test_fw_config->reqs) {
515 pr_err("Must call release_all_firmware prior to changing config\n");
517 mutex_unlock(&test_fw_mutex);
520 mutex_unlock(&test_fw_mutex);
522 rc = test_dev_config_update_size_t(buf, count,
523 &test_fw_config->buf_size);
529 static ssize_t config_buf_size_show(struct device *dev,
530 struct device_attribute *attr,
533 return test_dev_config_show_size_t(buf, test_fw_config->buf_size);
535 static DEVICE_ATTR_RW(config_buf_size);
537 static ssize_t config_file_offset_store(struct device *dev,
538 struct device_attribute *attr,
539 const char *buf, size_t count)
543 mutex_lock(&test_fw_mutex);
544 if (test_fw_config->reqs) {
545 pr_err("Must call release_all_firmware prior to changing config\n");
547 mutex_unlock(&test_fw_mutex);
550 mutex_unlock(&test_fw_mutex);
552 rc = test_dev_config_update_size_t(buf, count,
553 &test_fw_config->file_offset);
559 static ssize_t config_file_offset_show(struct device *dev,
560 struct device_attribute *attr,
563 return test_dev_config_show_size_t(buf, test_fw_config->file_offset);
565 static DEVICE_ATTR_RW(config_file_offset);
567 static ssize_t config_partial_store(struct device *dev,
568 struct device_attribute *attr,
569 const char *buf, size_t count)
571 return test_dev_config_update_bool(buf,
573 &test_fw_config->partial);
576 static ssize_t config_partial_show(struct device *dev,
577 struct device_attribute *attr,
580 return test_dev_config_show_bool(buf, test_fw_config->partial);
582 static DEVICE_ATTR_RW(config_partial);
584 static ssize_t config_sync_direct_store(struct device *dev,
585 struct device_attribute *attr,
586 const char *buf, size_t count)
588 int rc = test_dev_config_update_bool(buf, count,
589 &test_fw_config->sync_direct);
592 test_fw_config->req_firmware = test_fw_config->sync_direct ?
593 request_firmware_direct :
598 static ssize_t config_sync_direct_show(struct device *dev,
599 struct device_attribute *attr,
602 return test_dev_config_show_bool(buf, test_fw_config->sync_direct);
604 static DEVICE_ATTR_RW(config_sync_direct);
606 static ssize_t config_send_uevent_store(struct device *dev,
607 struct device_attribute *attr,
608 const char *buf, size_t count)
610 return test_dev_config_update_bool(buf, count,
611 &test_fw_config->send_uevent);
614 static ssize_t config_send_uevent_show(struct device *dev,
615 struct device_attribute *attr,
618 return test_dev_config_show_bool(buf, test_fw_config->send_uevent);
620 static DEVICE_ATTR_RW(config_send_uevent);
622 static ssize_t config_read_fw_idx_store(struct device *dev,
623 struct device_attribute *attr,
624 const char *buf, size_t count)
626 return test_dev_config_update_u8(buf, count,
627 &test_fw_config->read_fw_idx);
630 static ssize_t config_read_fw_idx_show(struct device *dev,
631 struct device_attribute *attr,
634 return test_dev_config_show_u8(buf, test_fw_config->read_fw_idx);
636 static DEVICE_ATTR_RW(config_read_fw_idx);
639 static ssize_t trigger_request_store(struct device *dev,
640 struct device_attribute *attr,
641 const char *buf, size_t count)
646 name = kstrndup(buf, count, GFP_KERNEL);
650 pr_info("loading '%s'\n", name);
652 mutex_lock(&test_fw_mutex);
653 release_firmware(test_firmware);
654 test_firmware = NULL;
655 rc = request_firmware(&test_firmware, name, dev);
657 pr_info("load of '%s' failed: %d\n", name, rc);
660 pr_info("loaded: %zu\n", test_firmware->size);
664 mutex_unlock(&test_fw_mutex);
670 static DEVICE_ATTR_WO(trigger_request);
672 #ifdef CONFIG_EFI_EMBEDDED_FIRMWARE
673 extern struct list_head efi_embedded_fw_list;
674 extern bool efi_embedded_fw_checked;
676 static ssize_t trigger_request_platform_store(struct device *dev,
677 struct device_attribute *attr,
678 const char *buf, size_t count)
680 static const u8 test_data[] = {
681 0x55, 0xaa, 0x55, 0xaa, 0x01, 0x02, 0x03, 0x04,
682 0x55, 0xaa, 0x55, 0xaa, 0x05, 0x06, 0x07, 0x08,
683 0x55, 0xaa, 0x55, 0xaa, 0x10, 0x20, 0x30, 0x40,
684 0x55, 0xaa, 0x55, 0xaa, 0x50, 0x60, 0x70, 0x80
686 struct efi_embedded_fw efi_embedded_fw;
687 const struct firmware *firmware = NULL;
688 bool saved_efi_embedded_fw_checked;
692 name = kstrndup(buf, count, GFP_KERNEL);
696 pr_info("inserting test platform fw '%s'\n", name);
697 efi_embedded_fw.name = name;
698 efi_embedded_fw.data = (void *)test_data;
699 efi_embedded_fw.length = sizeof(test_data);
700 list_add(&efi_embedded_fw.list, &efi_embedded_fw_list);
701 saved_efi_embedded_fw_checked = efi_embedded_fw_checked;
702 efi_embedded_fw_checked = true;
704 pr_info("loading '%s'\n", name);
705 rc = firmware_request_platform(&firmware, name, dev);
707 pr_info("load of '%s' failed: %d\n", name, rc);
710 if (firmware->size != sizeof(test_data) ||
711 memcmp(firmware->data, test_data, sizeof(test_data)) != 0) {
712 pr_info("firmware contents mismatch for '%s'\n", name);
716 pr_info("loaded: %zu\n", firmware->size);
720 efi_embedded_fw_checked = saved_efi_embedded_fw_checked;
721 release_firmware(firmware);
722 list_del(&efi_embedded_fw.list);
727 static DEVICE_ATTR_WO(trigger_request_platform);
730 static DECLARE_COMPLETION(async_fw_done);
732 static void trigger_async_request_cb(const struct firmware *fw, void *context)
735 complete(&async_fw_done);
738 static ssize_t trigger_async_request_store(struct device *dev,
739 struct device_attribute *attr,
740 const char *buf, size_t count)
745 name = kstrndup(buf, count, GFP_KERNEL);
749 pr_info("loading '%s'\n", name);
751 mutex_lock(&test_fw_mutex);
752 release_firmware(test_firmware);
753 test_firmware = NULL;
754 rc = request_firmware_nowait(THIS_MODULE, 1, name, dev, GFP_KERNEL,
755 NULL, trigger_async_request_cb);
757 pr_info("async load of '%s' failed: %d\n", name, rc);
761 /* Free 'name' ASAP, to test for race conditions */
764 wait_for_completion(&async_fw_done);
767 pr_info("loaded: %zu\n", test_firmware->size);
770 pr_err("failed to async load firmware\n");
775 mutex_unlock(&test_fw_mutex);
779 static DEVICE_ATTR_WO(trigger_async_request);
781 static ssize_t trigger_custom_fallback_store(struct device *dev,
782 struct device_attribute *attr,
783 const char *buf, size_t count)
788 name = kstrndup(buf, count, GFP_KERNEL);
792 pr_info("loading '%s' using custom fallback mechanism\n", name);
794 mutex_lock(&test_fw_mutex);
795 release_firmware(test_firmware);
796 test_firmware = NULL;
797 rc = request_firmware_nowait(THIS_MODULE, FW_ACTION_NOUEVENT, name,
798 dev, GFP_KERNEL, NULL,
799 trigger_async_request_cb);
801 pr_info("async load of '%s' failed: %d\n", name, rc);
805 /* Free 'name' ASAP, to test for race conditions */
808 wait_for_completion(&async_fw_done);
811 pr_info("loaded: %zu\n", test_firmware->size);
814 pr_err("failed to async load firmware\n");
819 mutex_unlock(&test_fw_mutex);
823 static DEVICE_ATTR_WO(trigger_custom_fallback);
825 static int test_fw_run_batch_request(void *data)
827 struct test_batched_req *req = data;
830 test_fw_config->test_result = -EINVAL;
834 if (test_fw_config->into_buf) {
837 test_buf = kzalloc(TEST_FIRMWARE_BUF_SIZE, GFP_KERNEL);
841 if (test_fw_config->partial)
842 req->rc = request_partial_firmware_into_buf
847 test_fw_config->buf_size,
848 test_fw_config->file_offset);
850 req->rc = request_firmware_into_buf
855 test_fw_config->buf_size);
859 req->rc = test_fw_config->req_firmware(&req->fw,
865 pr_info("#%u: batched sync load failed: %d\n",
867 if (!test_fw_config->test_result)
868 test_fw_config->test_result = req->rc;
869 } else if (req->fw) {
871 pr_info("#%u: batched sync loaded %zu\n",
872 req->idx, req->fw->size);
874 complete(&req->completion);
882 * We use a kthread as otherwise the kernel serializes all our sync requests
883 * and we would not be able to mimic batched requests on a sync call. Batched
884 * requests on a sync call can for instance happen on a device driver when
885 * multiple cards are used and firmware loading happens outside of probe.
887 static ssize_t trigger_batched_requests_store(struct device *dev,
888 struct device_attribute *attr,
889 const char *buf, size_t count)
891 struct test_batched_req *req;
895 mutex_lock(&test_fw_mutex);
897 test_fw_config->reqs =
898 vzalloc(array3_size(sizeof(struct test_batched_req),
899 test_fw_config->num_requests, 2));
900 if (!test_fw_config->reqs) {
905 pr_info("batched sync firmware loading '%s' %u times\n",
906 test_fw_config->name, test_fw_config->num_requests);
908 for (i = 0; i < test_fw_config->num_requests; i++) {
909 req = &test_fw_config->reqs[i];
912 req->name = test_fw_config->name;
914 init_completion(&req->completion);
915 req->task = kthread_run(test_fw_run_batch_request, req,
916 "%s-%u", KBUILD_MODNAME, req->idx);
917 if (!req->task || IS_ERR(req->task)) {
918 pr_err("Setting up thread %u failed\n", req->idx);
928 * We require an explicit release to enable more time and delay of
929 * calling release_firmware() to improve our chances of forcing a
930 * batched request. If we instead called release_firmware() right away
931 * then we might miss on an opportunity of having a successful firmware
932 * request pass on the opportunity to be come a batched request.
936 for (i = 0; i < test_fw_config->num_requests; i++) {
937 req = &test_fw_config->reqs[i];
938 if (req->task || req->sent)
939 wait_for_completion(&req->completion);
942 /* Override any worker error if we had a general setup error */
944 test_fw_config->test_result = rc;
947 mutex_unlock(&test_fw_mutex);
951 static DEVICE_ATTR_WO(trigger_batched_requests);
954 * We wait for each callback to return with the lock held, no need to lock here
956 static void trigger_batched_cb(const struct firmware *fw, void *context)
958 struct test_batched_req *req = context;
961 test_fw_config->test_result = -EINVAL;
965 /* forces *some* batched requests to queue up */
972 * Unfortunately the firmware API gives us nothing other than a null FW
973 * if the firmware was not found on async requests. Best we can do is
974 * just assume -ENOENT. A better API would pass the actual return
975 * value to the callback.
977 if (!fw && !test_fw_config->test_result)
978 test_fw_config->test_result = -ENOENT;
980 complete(&req->completion);
984 ssize_t trigger_batched_requests_async_store(struct device *dev,
985 struct device_attribute *attr,
986 const char *buf, size_t count)
988 struct test_batched_req *req;
993 mutex_lock(&test_fw_mutex);
995 test_fw_config->reqs =
996 vzalloc(array3_size(sizeof(struct test_batched_req),
997 test_fw_config->num_requests, 2));
998 if (!test_fw_config->reqs) {
1003 pr_info("batched loading '%s' custom fallback mechanism %u times\n",
1004 test_fw_config->name, test_fw_config->num_requests);
1006 send_uevent = test_fw_config->send_uevent ? FW_ACTION_UEVENT :
1009 for (i = 0; i < test_fw_config->num_requests; i++) {
1010 req = &test_fw_config->reqs[i];
1011 req->name = test_fw_config->name;
1014 init_completion(&req->completion);
1015 rc = request_firmware_nowait(THIS_MODULE, send_uevent,
1017 dev, GFP_KERNEL, req,
1018 trigger_batched_cb);
1020 pr_info("#%u: batched async load failed setup: %d\n",
1033 * We require an explicit release to enable more time and delay of
1034 * calling release_firmware() to improve our chances of forcing a
1035 * batched request. If we instead called release_firmware() right away
1036 * then we might miss on an opportunity of having a successful firmware
1037 * request pass on the opportunity to be come a batched request.
1040 for (i = 0; i < test_fw_config->num_requests; i++) {
1041 req = &test_fw_config->reqs[i];
1043 wait_for_completion(&req->completion);
1046 /* Override any worker error if we had a general setup error */
1048 test_fw_config->test_result = rc;
1051 mutex_unlock(&test_fw_mutex);
1055 static DEVICE_ATTR_WO(trigger_batched_requests_async);
1057 static void upload_release(struct test_firmware_upload *tst)
1059 firmware_upload_unregister(tst->fwl);
1065 static void upload_release_all(void)
1067 struct test_firmware_upload *tst, *tmp;
1069 list_for_each_entry_safe(tst, tmp, &test_upload_list, node) {
1070 list_del(&tst->node);
1071 upload_release(tst);
1073 test_fw_config->upload_name = NULL;
1077 * This table is replicated from .../firmware_loader/sysfs_upload.c
1078 * and needs to be kept in sync.
1080 static const char * const fw_upload_err_str[] = {
1081 [FW_UPLOAD_ERR_NONE] = "none",
1082 [FW_UPLOAD_ERR_HW_ERROR] = "hw-error",
1083 [FW_UPLOAD_ERR_TIMEOUT] = "timeout",
1084 [FW_UPLOAD_ERR_CANCELED] = "user-abort",
1085 [FW_UPLOAD_ERR_BUSY] = "device-busy",
1086 [FW_UPLOAD_ERR_INVALID_SIZE] = "invalid-file-size",
1087 [FW_UPLOAD_ERR_RW_ERROR] = "read-write-error",
1088 [FW_UPLOAD_ERR_WEAROUT] = "flash-wearout",
1091 static void upload_err_inject_error(struct test_firmware_upload *tst,
1092 const u8 *p, const char *prog)
1094 enum fw_upload_err err;
1096 for (err = FW_UPLOAD_ERR_NONE + 1; err < FW_UPLOAD_ERR_MAX; err++) {
1097 if (strncmp(p, fw_upload_err_str[err],
1098 strlen(fw_upload_err_str[err])) == 0) {
1099 tst->inject.prog = prog;
1100 tst->inject.err_code = err;
1106 static void upload_err_inject_prog(struct test_firmware_upload *tst,
1109 static const char * const progs[] = {
1110 "preparing:", "transferring:", "programming:"
1114 for (i = 0; i < ARRAY_SIZE(progs); i++) {
1115 if (strncmp(p, progs[i], strlen(progs[i])) == 0) {
1116 upload_err_inject_error(tst, p + strlen(progs[i]),
1123 #define FIVE_MINUTES_MS (5 * 60 * 1000)
1124 static enum fw_upload_err
1125 fw_upload_wait_on_cancel(struct test_firmware_upload *tst)
1129 for (ms_delay = 0; ms_delay < FIVE_MINUTES_MS; ms_delay += 100) {
1131 if (tst->cancel_request)
1132 return FW_UPLOAD_ERR_CANCELED;
1134 return FW_UPLOAD_ERR_NONE;
1137 static enum fw_upload_err test_fw_upload_prepare(struct fw_upload *fwl,
1138 const u8 *data, u32 size)
1140 struct test_firmware_upload *tst = fwl->dd_handle;
1141 enum fw_upload_err ret = FW_UPLOAD_ERR_NONE;
1142 const char *progress = "preparing:";
1144 tst->cancel_request = false;
1146 if (!size || size > TEST_UPLOAD_MAX_SIZE) {
1147 ret = FW_UPLOAD_ERR_INVALID_SIZE;
1151 if (strncmp(data, "inject:", strlen("inject:")) == 0)
1152 upload_err_inject_prog(tst, data + strlen("inject:"));
1154 memset(tst->buf, 0, TEST_UPLOAD_MAX_SIZE);
1157 if (tst->inject.err_code == FW_UPLOAD_ERR_NONE ||
1158 strncmp(tst->inject.prog, progress, strlen(progress)) != 0)
1159 return FW_UPLOAD_ERR_NONE;
1161 if (tst->inject.err_code == FW_UPLOAD_ERR_CANCELED)
1162 ret = fw_upload_wait_on_cancel(tst);
1164 ret = tst->inject.err_code;
1168 * The cleanup op only executes if the prepare op succeeds.
1169 * If the prepare op fails, it must do it's own clean-up.
1171 tst->inject.err_code = FW_UPLOAD_ERR_NONE;
1172 tst->inject.prog = NULL;
1177 static enum fw_upload_err test_fw_upload_write(struct fw_upload *fwl,
1178 const u8 *data, u32 offset,
1179 u32 size, u32 *written)
1181 struct test_firmware_upload *tst = fwl->dd_handle;
1182 const char *progress = "transferring:";
1185 if (tst->cancel_request)
1186 return FW_UPLOAD_ERR_CANCELED;
1188 blk_size = min_t(u32, TEST_UPLOAD_BLK_SIZE, size);
1189 memcpy(tst->buf + offset, data + offset, blk_size);
1191 *written = blk_size;
1193 if (tst->inject.err_code == FW_UPLOAD_ERR_NONE ||
1194 strncmp(tst->inject.prog, progress, strlen(progress)) != 0)
1195 return FW_UPLOAD_ERR_NONE;
1197 if (tst->inject.err_code == FW_UPLOAD_ERR_CANCELED)
1198 return fw_upload_wait_on_cancel(tst);
1200 return tst->inject.err_code;
1203 static enum fw_upload_err test_fw_upload_complete(struct fw_upload *fwl)
1205 struct test_firmware_upload *tst = fwl->dd_handle;
1206 const char *progress = "programming:";
1208 if (tst->cancel_request)
1209 return FW_UPLOAD_ERR_CANCELED;
1211 if (tst->inject.err_code == FW_UPLOAD_ERR_NONE ||
1212 strncmp(tst->inject.prog, progress, strlen(progress)) != 0)
1213 return FW_UPLOAD_ERR_NONE;
1215 if (tst->inject.err_code == FW_UPLOAD_ERR_CANCELED)
1216 return fw_upload_wait_on_cancel(tst);
1218 return tst->inject.err_code;
1221 static void test_fw_upload_cancel(struct fw_upload *fwl)
1223 struct test_firmware_upload *tst = fwl->dd_handle;
1225 tst->cancel_request = true;
1228 static void test_fw_cleanup(struct fw_upload *fwl)
1230 struct test_firmware_upload *tst = fwl->dd_handle;
1232 tst->inject.err_code = FW_UPLOAD_ERR_NONE;
1233 tst->inject.prog = NULL;
1236 static const struct fw_upload_ops upload_test_ops = {
1237 .prepare = test_fw_upload_prepare,
1238 .write = test_fw_upload_write,
1239 .poll_complete = test_fw_upload_complete,
1240 .cancel = test_fw_upload_cancel,
1241 .cleanup = test_fw_cleanup
1244 static ssize_t upload_register_store(struct device *dev,
1245 struct device_attribute *attr,
1246 const char *buf, size_t count)
1248 struct test_firmware_upload *tst;
1249 struct fw_upload *fwl;
1253 name = kstrndup(buf, count, GFP_KERNEL);
1257 mutex_lock(&test_fw_mutex);
1258 tst = upload_lookup_name(name);
1264 tst = kzalloc(sizeof(*tst), GFP_KERNEL);
1271 tst->buf = kzalloc(TEST_UPLOAD_MAX_SIZE, GFP_KERNEL);
1277 fwl = firmware_upload_register(THIS_MODULE, dev, tst->name,
1278 &upload_test_ops, tst);
1285 list_add_tail(&tst->node, &test_upload_list);
1286 mutex_unlock(&test_fw_mutex);
1296 mutex_unlock(&test_fw_mutex);
1301 static DEVICE_ATTR_WO(upload_register);
1303 static ssize_t upload_unregister_store(struct device *dev,
1304 struct device_attribute *attr,
1305 const char *buf, size_t count)
1307 struct test_firmware_upload *tst;
1310 mutex_lock(&test_fw_mutex);
1311 tst = upload_lookup_name(buf);
1317 if (test_fw_config->upload_name == tst->name)
1318 test_fw_config->upload_name = NULL;
1320 list_del(&tst->node);
1321 upload_release(tst);
1324 mutex_unlock(&test_fw_mutex);
1327 static DEVICE_ATTR_WO(upload_unregister);
1329 static ssize_t test_result_show(struct device *dev,
1330 struct device_attribute *attr,
1333 return test_dev_config_show_int(buf, test_fw_config->test_result);
1335 static DEVICE_ATTR_RO(test_result);
1337 static ssize_t release_all_firmware_store(struct device *dev,
1338 struct device_attribute *attr,
1339 const char *buf, size_t count)
1341 test_release_all_firmware();
1344 static DEVICE_ATTR_WO(release_all_firmware);
1346 static ssize_t read_firmware_show(struct device *dev,
1347 struct device_attribute *attr,
1350 struct test_batched_req *req;
1354 mutex_lock(&test_fw_mutex);
1356 idx = test_fw_config->read_fw_idx;
1357 if (idx >= test_fw_config->num_requests) {
1362 if (!test_fw_config->reqs) {
1367 req = &test_fw_config->reqs[idx];
1369 pr_err("#%u: failed to async load firmware\n", idx);
1374 pr_info("#%u: loaded %zu\n", idx, req->fw->size);
1376 if (req->fw->size > PAGE_SIZE) {
1377 pr_err("Testing interface must use PAGE_SIZE firmware for now\n");
1381 memcpy(buf, req->fw->data, req->fw->size);
1385 mutex_unlock(&test_fw_mutex);
1389 static DEVICE_ATTR_RO(read_firmware);
1391 static ssize_t upload_read_show(struct device *dev,
1392 struct device_attribute *attr,
1395 struct test_firmware_upload *tst = NULL;
1396 struct test_firmware_upload *tst_iter;
1399 if (!test_fw_config->upload_name) {
1400 pr_err("Set config_upload_name before using upload_read\n");
1404 mutex_lock(&test_fw_mutex);
1405 list_for_each_entry(tst_iter, &test_upload_list, node)
1406 if (tst_iter->name == test_fw_config->upload_name) {
1412 pr_err("Firmware name not found: %s\n",
1413 test_fw_config->upload_name);
1417 if (tst->size > PAGE_SIZE) {
1418 pr_err("Testing interface must use PAGE_SIZE firmware for now\n");
1422 memcpy(buf, tst->buf, tst->size);
1425 mutex_unlock(&test_fw_mutex);
1428 static DEVICE_ATTR_RO(upload_read);
1430 #define TEST_FW_DEV_ATTR(name) &dev_attr_##name.attr
1432 static struct attribute *test_dev_attrs[] = {
1433 TEST_FW_DEV_ATTR(reset),
1435 TEST_FW_DEV_ATTR(config),
1436 TEST_FW_DEV_ATTR(config_name),
1437 TEST_FW_DEV_ATTR(config_num_requests),
1438 TEST_FW_DEV_ATTR(config_into_buf),
1439 TEST_FW_DEV_ATTR(config_buf_size),
1440 TEST_FW_DEV_ATTR(config_file_offset),
1441 TEST_FW_DEV_ATTR(config_partial),
1442 TEST_FW_DEV_ATTR(config_sync_direct),
1443 TEST_FW_DEV_ATTR(config_send_uevent),
1444 TEST_FW_DEV_ATTR(config_read_fw_idx),
1445 TEST_FW_DEV_ATTR(config_upload_name),
1447 /* These don't use the config at all - they could be ported! */
1448 TEST_FW_DEV_ATTR(trigger_request),
1449 TEST_FW_DEV_ATTR(trigger_async_request),
1450 TEST_FW_DEV_ATTR(trigger_custom_fallback),
1451 #ifdef CONFIG_EFI_EMBEDDED_FIRMWARE
1452 TEST_FW_DEV_ATTR(trigger_request_platform),
1455 /* These use the config and can use the test_result */
1456 TEST_FW_DEV_ATTR(trigger_batched_requests),
1457 TEST_FW_DEV_ATTR(trigger_batched_requests_async),
1459 TEST_FW_DEV_ATTR(release_all_firmware),
1460 TEST_FW_DEV_ATTR(test_result),
1461 TEST_FW_DEV_ATTR(read_firmware),
1462 TEST_FW_DEV_ATTR(upload_read),
1463 TEST_FW_DEV_ATTR(upload_register),
1464 TEST_FW_DEV_ATTR(upload_unregister),
1468 ATTRIBUTE_GROUPS(test_dev);
1470 static struct miscdevice test_fw_misc_device = {
1471 .minor = MISC_DYNAMIC_MINOR,
1472 .name = "test_firmware",
1473 .fops = &test_fw_fops,
1474 .groups = test_dev_groups,
1477 static int __init test_firmware_init(void)
1481 test_fw_config = kzalloc(sizeof(struct test_config), GFP_KERNEL);
1482 if (!test_fw_config)
1485 rc = __test_firmware_config_init();
1487 kfree(test_fw_config);
1488 pr_err("could not init firmware test config: %d\n", rc);
1492 rc = misc_register(&test_fw_misc_device);
1494 kfree(test_fw_config);
1495 pr_err("could not register misc device: %d\n", rc);
1499 pr_warn("interface ready\n");
1504 module_init(test_firmware_init);
1506 static void __exit test_firmware_exit(void)
1508 mutex_lock(&test_fw_mutex);
1509 release_firmware(test_firmware);
1510 misc_deregister(&test_fw_misc_device);
1511 upload_release_all();
1512 __test_firmware_config_free();
1513 kfree(test_fw_config);
1514 mutex_unlock(&test_fw_mutex);
1516 pr_warn("removed interface\n");
1519 module_exit(test_firmware_exit);
1521 MODULE_AUTHOR("Kees Cook <keescook@chromium.org>");
1522 MODULE_LICENSE("GPL");