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
35 static DEFINE_MUTEX(test_fw_mutex);
36 static const struct firmware *test_firmware;
38 struct test_batched_req {
42 const struct firmware *fw;
44 struct completion completion;
45 struct task_struct *task;
50 * test_config - represents configuration for the test for different triggers
52 * @name: the name of the firmware file to look for
53 * @into_buf: when the into_buf is used if this is true
54 * request_firmware_into_buf() will be used instead.
55 * @buf_size: size of buf to allocate when into_buf is true
56 * @file_offset: file offset to request when calling request_firmware_into_buf
57 * @partial: partial read opt when calling request_firmware_into_buf
58 * @sync_direct: when the sync trigger is used if this is true
59 * request_firmware_direct() will be used instead.
60 * @send_uevent: whether or not to send a uevent for async requests
61 * @num_requests: number of requests to try per test case. This is trigger
63 * @reqs: stores all requests information
64 * @read_fw_idx: index of thread from which we want to read firmware results
65 * from through the read_fw trigger.
66 * @test_result: a test may use this to collect the result from the call
67 * of the request_firmware*() calls used in their tests. In order of
68 * priority we always keep first any setup error. If no setup errors were
69 * found then we move on to the first error encountered while running the
70 * API. Note that for async calls this typically will be a successful
71 * result (0) unless of course you've used bogus parameters, or the system
72 * is out of memory. In the async case the callback is expected to do a
73 * bit more homework to figure out what happened, unfortunately the only
74 * information passed today on error is the fact that no firmware was
75 * found so we can only assume -ENOENT on async calls if the firmware is
78 * Errors you can expect:
82 * 0: success for sync, for async it means request was sent
83 * -EINVAL: invalid parameters or request
84 * -ENOENT: files not found
88 * -ENOMEM: memory pressure on system
89 * -ENODEV: out of number of devices to test
90 * -EINVAL: an unexpected error has occurred
91 * @req_firmware: if @sync_direct is true this is set to
92 * request_firmware_direct(), otherwise request_firmware()
106 * These below don't belong her but we'll move them once we create
107 * a struct fw_test_device and stuff the misc_dev under there later.
109 struct test_batched_req *reqs;
111 int (*req_firmware)(const struct firmware **fw, const char *name,
112 struct device *device);
115 static struct test_config *test_fw_config;
117 static ssize_t test_fw_misc_read(struct file *f, char __user *buf,
118 size_t size, loff_t *offset)
122 mutex_lock(&test_fw_mutex);
124 rc = simple_read_from_buffer(buf, size, offset,
126 test_firmware->size);
127 mutex_unlock(&test_fw_mutex);
131 static const struct file_operations test_fw_fops = {
132 .owner = THIS_MODULE,
133 .read = test_fw_misc_read,
136 static void __test_release_all_firmware(void)
138 struct test_batched_req *req;
141 if (!test_fw_config->reqs)
144 for (i = 0; i < test_fw_config->num_requests; i++) {
145 req = &test_fw_config->reqs[i];
147 release_firmware(req->fw);
150 vfree(test_fw_config->reqs);
151 test_fw_config->reqs = NULL;
154 static void test_release_all_firmware(void)
156 mutex_lock(&test_fw_mutex);
157 __test_release_all_firmware();
158 mutex_unlock(&test_fw_mutex);
162 static void __test_firmware_config_free(void)
164 __test_release_all_firmware();
165 kfree_const(test_fw_config->name);
166 test_fw_config->name = NULL;
170 * XXX: move to kstrncpy() once merged.
172 * Users should use kfree_const() when freeing these.
174 static int __kstrncpy(char **dst, const char *name, size_t count, gfp_t gfp)
176 *dst = kstrndup(name, count, gfp);
182 static int __test_firmware_config_init(void)
186 ret = __kstrncpy(&test_fw_config->name, TEST_FIRMWARE_NAME,
187 strlen(TEST_FIRMWARE_NAME), GFP_KERNEL);
191 test_fw_config->num_requests = TEST_FIRMWARE_NUM_REQS;
192 test_fw_config->send_uevent = true;
193 test_fw_config->into_buf = false;
194 test_fw_config->buf_size = TEST_FIRMWARE_BUF_SIZE;
195 test_fw_config->file_offset = 0;
196 test_fw_config->partial = false;
197 test_fw_config->sync_direct = false;
198 test_fw_config->req_firmware = request_firmware;
199 test_fw_config->test_result = 0;
200 test_fw_config->reqs = NULL;
205 __test_firmware_config_free();
209 static ssize_t reset_store(struct device *dev,
210 struct device_attribute *attr,
211 const char *buf, size_t count)
215 mutex_lock(&test_fw_mutex);
217 __test_firmware_config_free();
219 ret = __test_firmware_config_init();
222 pr_err("could not alloc settings for config trigger: %d\n",
231 mutex_unlock(&test_fw_mutex);
235 static DEVICE_ATTR_WO(reset);
237 static ssize_t config_show(struct device *dev,
238 struct device_attribute *attr,
243 mutex_lock(&test_fw_mutex);
245 len += scnprintf(buf, PAGE_SIZE - len,
246 "Custom trigger configuration for: %s\n",
249 if (test_fw_config->name)
250 len += scnprintf(buf + len, PAGE_SIZE - len,
252 test_fw_config->name);
254 len += scnprintf(buf + len, PAGE_SIZE - len,
257 len += scnprintf(buf + len, PAGE_SIZE - len,
258 "num_requests:\t%u\n", test_fw_config->num_requests);
260 len += scnprintf(buf + len, PAGE_SIZE - len,
261 "send_uevent:\t\t%s\n",
262 test_fw_config->send_uevent ?
263 "FW_ACTION_HOTPLUG" :
264 "FW_ACTION_NOHOTPLUG");
265 len += scnprintf(buf + len, PAGE_SIZE - len,
267 test_fw_config->into_buf ? "true" : "false");
268 len += scnprintf(buf + len, PAGE_SIZE - len,
269 "buf_size:\t%zu\n", test_fw_config->buf_size);
270 len += scnprintf(buf + len, PAGE_SIZE - len,
271 "file_offset:\t%zu\n", test_fw_config->file_offset);
272 len += scnprintf(buf + len, PAGE_SIZE - len,
274 test_fw_config->partial ? "true" : "false");
275 len += scnprintf(buf + len, PAGE_SIZE - len,
276 "sync_direct:\t\t%s\n",
277 test_fw_config->sync_direct ? "true" : "false");
278 len += scnprintf(buf + len, PAGE_SIZE - len,
279 "read_fw_idx:\t%u\n", test_fw_config->read_fw_idx);
281 mutex_unlock(&test_fw_mutex);
285 static DEVICE_ATTR_RO(config);
287 static ssize_t config_name_store(struct device *dev,
288 struct device_attribute *attr,
289 const char *buf, size_t count)
293 mutex_lock(&test_fw_mutex);
294 kfree_const(test_fw_config->name);
295 ret = __kstrncpy(&test_fw_config->name, buf, count, GFP_KERNEL);
296 mutex_unlock(&test_fw_mutex);
302 * As per sysfs_kf_seq_show() the buf is max PAGE_SIZE.
304 static ssize_t config_test_show_str(char *dst,
309 mutex_lock(&test_fw_mutex);
310 len = snprintf(dst, PAGE_SIZE, "%s\n", src);
311 mutex_unlock(&test_fw_mutex);
316 static int test_dev_config_update_bool(const char *buf, size_t size,
321 mutex_lock(&test_fw_mutex);
322 if (strtobool(buf, cfg) < 0)
326 mutex_unlock(&test_fw_mutex);
331 static ssize_t test_dev_config_show_bool(char *buf, bool val)
333 return snprintf(buf, PAGE_SIZE, "%d\n", val);
336 static int test_dev_config_update_size_t(const char *buf,
343 ret = kstrtol(buf, 10, &new);
347 mutex_lock(&test_fw_mutex);
348 *(size_t *)cfg = new;
349 mutex_unlock(&test_fw_mutex);
351 /* Always return full write size even if we didn't consume all */
355 static ssize_t test_dev_config_show_size_t(char *buf, size_t val)
357 return snprintf(buf, PAGE_SIZE, "%zu\n", val);
360 static ssize_t test_dev_config_show_int(char *buf, int val)
362 return snprintf(buf, PAGE_SIZE, "%d\n", val);
365 static int test_dev_config_update_u8(const char *buf, size_t size, u8 *cfg)
370 ret = kstrtol(buf, 10, &new);
377 mutex_lock(&test_fw_mutex);
379 mutex_unlock(&test_fw_mutex);
381 /* Always return full write size even if we didn't consume all */
385 static ssize_t test_dev_config_show_u8(char *buf, u8 val)
387 return snprintf(buf, PAGE_SIZE, "%u\n", val);
390 static ssize_t config_name_show(struct device *dev,
391 struct device_attribute *attr,
394 return config_test_show_str(buf, test_fw_config->name);
396 static DEVICE_ATTR_RW(config_name);
398 static ssize_t config_num_requests_store(struct device *dev,
399 struct device_attribute *attr,
400 const char *buf, size_t count)
404 mutex_lock(&test_fw_mutex);
405 if (test_fw_config->reqs) {
406 pr_err("Must call release_all_firmware prior to changing config\n");
408 mutex_unlock(&test_fw_mutex);
411 mutex_unlock(&test_fw_mutex);
413 rc = test_dev_config_update_u8(buf, count,
414 &test_fw_config->num_requests);
420 static ssize_t config_num_requests_show(struct device *dev,
421 struct device_attribute *attr,
424 return test_dev_config_show_u8(buf, test_fw_config->num_requests);
426 static DEVICE_ATTR_RW(config_num_requests);
428 static ssize_t config_into_buf_store(struct device *dev,
429 struct device_attribute *attr,
430 const char *buf, size_t count)
432 return test_dev_config_update_bool(buf,
434 &test_fw_config->into_buf);
437 static ssize_t config_into_buf_show(struct device *dev,
438 struct device_attribute *attr,
441 return test_dev_config_show_bool(buf, test_fw_config->into_buf);
443 static DEVICE_ATTR_RW(config_into_buf);
445 static ssize_t config_buf_size_store(struct device *dev,
446 struct device_attribute *attr,
447 const char *buf, size_t count)
451 mutex_lock(&test_fw_mutex);
452 if (test_fw_config->reqs) {
453 pr_err("Must call release_all_firmware prior to changing config\n");
455 mutex_unlock(&test_fw_mutex);
458 mutex_unlock(&test_fw_mutex);
460 rc = test_dev_config_update_size_t(buf, count,
461 &test_fw_config->buf_size);
467 static ssize_t config_buf_size_show(struct device *dev,
468 struct device_attribute *attr,
471 return test_dev_config_show_size_t(buf, test_fw_config->buf_size);
473 static DEVICE_ATTR_RW(config_buf_size);
475 static ssize_t config_file_offset_store(struct device *dev,
476 struct device_attribute *attr,
477 const char *buf, size_t count)
481 mutex_lock(&test_fw_mutex);
482 if (test_fw_config->reqs) {
483 pr_err("Must call release_all_firmware prior to changing config\n");
485 mutex_unlock(&test_fw_mutex);
488 mutex_unlock(&test_fw_mutex);
490 rc = test_dev_config_update_size_t(buf, count,
491 &test_fw_config->file_offset);
497 static ssize_t config_file_offset_show(struct device *dev,
498 struct device_attribute *attr,
501 return test_dev_config_show_size_t(buf, test_fw_config->file_offset);
503 static DEVICE_ATTR_RW(config_file_offset);
505 static ssize_t config_partial_store(struct device *dev,
506 struct device_attribute *attr,
507 const char *buf, size_t count)
509 return test_dev_config_update_bool(buf,
511 &test_fw_config->partial);
514 static ssize_t config_partial_show(struct device *dev,
515 struct device_attribute *attr,
518 return test_dev_config_show_bool(buf, test_fw_config->partial);
520 static DEVICE_ATTR_RW(config_partial);
522 static ssize_t config_sync_direct_store(struct device *dev,
523 struct device_attribute *attr,
524 const char *buf, size_t count)
526 int rc = test_dev_config_update_bool(buf, count,
527 &test_fw_config->sync_direct);
530 test_fw_config->req_firmware = test_fw_config->sync_direct ?
531 request_firmware_direct :
536 static ssize_t config_sync_direct_show(struct device *dev,
537 struct device_attribute *attr,
540 return test_dev_config_show_bool(buf, test_fw_config->sync_direct);
542 static DEVICE_ATTR_RW(config_sync_direct);
544 static ssize_t config_send_uevent_store(struct device *dev,
545 struct device_attribute *attr,
546 const char *buf, size_t count)
548 return test_dev_config_update_bool(buf, count,
549 &test_fw_config->send_uevent);
552 static ssize_t config_send_uevent_show(struct device *dev,
553 struct device_attribute *attr,
556 return test_dev_config_show_bool(buf, test_fw_config->send_uevent);
558 static DEVICE_ATTR_RW(config_send_uevent);
560 static ssize_t config_read_fw_idx_store(struct device *dev,
561 struct device_attribute *attr,
562 const char *buf, size_t count)
564 return test_dev_config_update_u8(buf, count,
565 &test_fw_config->read_fw_idx);
568 static ssize_t config_read_fw_idx_show(struct device *dev,
569 struct device_attribute *attr,
572 return test_dev_config_show_u8(buf, test_fw_config->read_fw_idx);
574 static DEVICE_ATTR_RW(config_read_fw_idx);
577 static ssize_t trigger_request_store(struct device *dev,
578 struct device_attribute *attr,
579 const char *buf, size_t count)
584 name = kstrndup(buf, count, GFP_KERNEL);
588 pr_info("loading '%s'\n", name);
590 mutex_lock(&test_fw_mutex);
591 release_firmware(test_firmware);
592 test_firmware = NULL;
593 rc = request_firmware(&test_firmware, name, dev);
595 pr_info("load of '%s' failed: %d\n", name, rc);
598 pr_info("loaded: %zu\n", test_firmware->size);
602 mutex_unlock(&test_fw_mutex);
608 static DEVICE_ATTR_WO(trigger_request);
610 #ifdef CONFIG_EFI_EMBEDDED_FIRMWARE
611 extern struct list_head efi_embedded_fw_list;
612 extern bool efi_embedded_fw_checked;
614 static ssize_t trigger_request_platform_store(struct device *dev,
615 struct device_attribute *attr,
616 const char *buf, size_t count)
618 static const u8 test_data[] = {
619 0x55, 0xaa, 0x55, 0xaa, 0x01, 0x02, 0x03, 0x04,
620 0x55, 0xaa, 0x55, 0xaa, 0x05, 0x06, 0x07, 0x08,
621 0x55, 0xaa, 0x55, 0xaa, 0x10, 0x20, 0x30, 0x40,
622 0x55, 0xaa, 0x55, 0xaa, 0x50, 0x60, 0x70, 0x80
624 struct efi_embedded_fw efi_embedded_fw;
625 const struct firmware *firmware = NULL;
626 bool saved_efi_embedded_fw_checked;
630 name = kstrndup(buf, count, GFP_KERNEL);
634 pr_info("inserting test platform fw '%s'\n", name);
635 efi_embedded_fw.name = name;
636 efi_embedded_fw.data = (void *)test_data;
637 efi_embedded_fw.length = sizeof(test_data);
638 list_add(&efi_embedded_fw.list, &efi_embedded_fw_list);
639 saved_efi_embedded_fw_checked = efi_embedded_fw_checked;
640 efi_embedded_fw_checked = true;
642 pr_info("loading '%s'\n", name);
643 rc = firmware_request_platform(&firmware, name, dev);
645 pr_info("load of '%s' failed: %d\n", name, rc);
648 if (firmware->size != sizeof(test_data) ||
649 memcmp(firmware->data, test_data, sizeof(test_data)) != 0) {
650 pr_info("firmware contents mismatch for '%s'\n", name);
654 pr_info("loaded: %zu\n", firmware->size);
658 efi_embedded_fw_checked = saved_efi_embedded_fw_checked;
659 release_firmware(firmware);
660 list_del(&efi_embedded_fw.list);
665 static DEVICE_ATTR_WO(trigger_request_platform);
668 static DECLARE_COMPLETION(async_fw_done);
670 static void trigger_async_request_cb(const struct firmware *fw, void *context)
673 complete(&async_fw_done);
676 static ssize_t trigger_async_request_store(struct device *dev,
677 struct device_attribute *attr,
678 const char *buf, size_t count)
683 name = kstrndup(buf, count, GFP_KERNEL);
687 pr_info("loading '%s'\n", name);
689 mutex_lock(&test_fw_mutex);
690 release_firmware(test_firmware);
691 test_firmware = NULL;
692 rc = request_firmware_nowait(THIS_MODULE, 1, name, dev, GFP_KERNEL,
693 NULL, trigger_async_request_cb);
695 pr_info("async load of '%s' failed: %d\n", name, rc);
699 /* Free 'name' ASAP, to test for race conditions */
702 wait_for_completion(&async_fw_done);
705 pr_info("loaded: %zu\n", test_firmware->size);
708 pr_err("failed to async load firmware\n");
713 mutex_unlock(&test_fw_mutex);
717 static DEVICE_ATTR_WO(trigger_async_request);
719 static ssize_t trigger_custom_fallback_store(struct device *dev,
720 struct device_attribute *attr,
721 const char *buf, size_t count)
726 name = kstrndup(buf, count, GFP_KERNEL);
730 pr_info("loading '%s' using custom fallback mechanism\n", name);
732 mutex_lock(&test_fw_mutex);
733 release_firmware(test_firmware);
734 test_firmware = NULL;
735 rc = request_firmware_nowait(THIS_MODULE, FW_ACTION_NOHOTPLUG, name,
736 dev, GFP_KERNEL, NULL,
737 trigger_async_request_cb);
739 pr_info("async load of '%s' failed: %d\n", name, rc);
743 /* Free 'name' ASAP, to test for race conditions */
746 wait_for_completion(&async_fw_done);
749 pr_info("loaded: %zu\n", test_firmware->size);
752 pr_err("failed to async load firmware\n");
757 mutex_unlock(&test_fw_mutex);
761 static DEVICE_ATTR_WO(trigger_custom_fallback);
763 static int test_fw_run_batch_request(void *data)
765 struct test_batched_req *req = data;
768 test_fw_config->test_result = -EINVAL;
772 if (test_fw_config->into_buf) {
775 test_buf = kzalloc(TEST_FIRMWARE_BUF_SIZE, GFP_KERNEL);
779 if (test_fw_config->partial)
780 req->rc = request_partial_firmware_into_buf
785 test_fw_config->buf_size,
786 test_fw_config->file_offset);
788 req->rc = request_firmware_into_buf
793 test_fw_config->buf_size);
797 req->rc = test_fw_config->req_firmware(&req->fw,
803 pr_info("#%u: batched sync load failed: %d\n",
805 if (!test_fw_config->test_result)
806 test_fw_config->test_result = req->rc;
807 } else if (req->fw) {
809 pr_info("#%u: batched sync loaded %zu\n",
810 req->idx, req->fw->size);
812 complete(&req->completion);
820 * We use a kthread as otherwise the kernel serializes all our sync requests
821 * and we would not be able to mimic batched requests on a sync call. Batched
822 * requests on a sync call can for instance happen on a device driver when
823 * multiple cards are used and firmware loading happens outside of probe.
825 static ssize_t trigger_batched_requests_store(struct device *dev,
826 struct device_attribute *attr,
827 const char *buf, size_t count)
829 struct test_batched_req *req;
833 mutex_lock(&test_fw_mutex);
835 test_fw_config->reqs =
836 vzalloc(array3_size(sizeof(struct test_batched_req),
837 test_fw_config->num_requests, 2));
838 if (!test_fw_config->reqs) {
843 pr_info("batched sync firmware loading '%s' %u times\n",
844 test_fw_config->name, test_fw_config->num_requests);
846 for (i = 0; i < test_fw_config->num_requests; i++) {
847 req = &test_fw_config->reqs[i];
850 req->name = test_fw_config->name;
852 init_completion(&req->completion);
853 req->task = kthread_run(test_fw_run_batch_request, req,
854 "%s-%u", KBUILD_MODNAME, req->idx);
855 if (!req->task || IS_ERR(req->task)) {
856 pr_err("Setting up thread %u failed\n", req->idx);
866 * We require an explicit release to enable more time and delay of
867 * calling release_firmware() to improve our chances of forcing a
868 * batched request. If we instead called release_firmware() right away
869 * then we might miss on an opportunity of having a successful firmware
870 * request pass on the opportunity to be come a batched request.
874 for (i = 0; i < test_fw_config->num_requests; i++) {
875 req = &test_fw_config->reqs[i];
876 if (req->task || req->sent)
877 wait_for_completion(&req->completion);
880 /* Override any worker error if we had a general setup error */
882 test_fw_config->test_result = rc;
885 mutex_unlock(&test_fw_mutex);
889 static DEVICE_ATTR_WO(trigger_batched_requests);
892 * We wait for each callback to return with the lock held, no need to lock here
894 static void trigger_batched_cb(const struct firmware *fw, void *context)
896 struct test_batched_req *req = context;
899 test_fw_config->test_result = -EINVAL;
903 /* forces *some* batched requests to queue up */
910 * Unfortunately the firmware API gives us nothing other than a null FW
911 * if the firmware was not found on async requests. Best we can do is
912 * just assume -ENOENT. A better API would pass the actual return
913 * value to the callback.
915 if (!fw && !test_fw_config->test_result)
916 test_fw_config->test_result = -ENOENT;
918 complete(&req->completion);
922 ssize_t trigger_batched_requests_async_store(struct device *dev,
923 struct device_attribute *attr,
924 const char *buf, size_t count)
926 struct test_batched_req *req;
931 mutex_lock(&test_fw_mutex);
933 test_fw_config->reqs =
934 vzalloc(array3_size(sizeof(struct test_batched_req),
935 test_fw_config->num_requests, 2));
936 if (!test_fw_config->reqs) {
941 pr_info("batched loading '%s' custom fallback mechanism %u times\n",
942 test_fw_config->name, test_fw_config->num_requests);
944 send_uevent = test_fw_config->send_uevent ? FW_ACTION_HOTPLUG :
947 for (i = 0; i < test_fw_config->num_requests; i++) {
948 req = &test_fw_config->reqs[i];
949 req->name = test_fw_config->name;
952 init_completion(&req->completion);
953 rc = request_firmware_nowait(THIS_MODULE, send_uevent,
955 dev, GFP_KERNEL, req,
958 pr_info("#%u: batched async load failed setup: %d\n",
971 * We require an explicit release to enable more time and delay of
972 * calling release_firmware() to improve our chances of forcing a
973 * batched request. If we instead called release_firmware() right away
974 * then we might miss on an opportunity of having a successful firmware
975 * request pass on the opportunity to be come a batched request.
978 for (i = 0; i < test_fw_config->num_requests; i++) {
979 req = &test_fw_config->reqs[i];
981 wait_for_completion(&req->completion);
984 /* Override any worker error if we had a general setup error */
986 test_fw_config->test_result = rc;
989 mutex_unlock(&test_fw_mutex);
993 static DEVICE_ATTR_WO(trigger_batched_requests_async);
995 static ssize_t test_result_show(struct device *dev,
996 struct device_attribute *attr,
999 return test_dev_config_show_int(buf, test_fw_config->test_result);
1001 static DEVICE_ATTR_RO(test_result);
1003 static ssize_t release_all_firmware_store(struct device *dev,
1004 struct device_attribute *attr,
1005 const char *buf, size_t count)
1007 test_release_all_firmware();
1010 static DEVICE_ATTR_WO(release_all_firmware);
1012 static ssize_t read_firmware_show(struct device *dev,
1013 struct device_attribute *attr,
1016 struct test_batched_req *req;
1020 mutex_lock(&test_fw_mutex);
1022 idx = test_fw_config->read_fw_idx;
1023 if (idx >= test_fw_config->num_requests) {
1028 if (!test_fw_config->reqs) {
1033 req = &test_fw_config->reqs[idx];
1035 pr_err("#%u: failed to async load firmware\n", idx);
1040 pr_info("#%u: loaded %zu\n", idx, req->fw->size);
1042 if (req->fw->size > PAGE_SIZE) {
1043 pr_err("Testing interface must use PAGE_SIZE firmware for now\n");
1047 memcpy(buf, req->fw->data, req->fw->size);
1051 mutex_unlock(&test_fw_mutex);
1055 static DEVICE_ATTR_RO(read_firmware);
1057 #define TEST_FW_DEV_ATTR(name) &dev_attr_##name.attr
1059 static struct attribute *test_dev_attrs[] = {
1060 TEST_FW_DEV_ATTR(reset),
1062 TEST_FW_DEV_ATTR(config),
1063 TEST_FW_DEV_ATTR(config_name),
1064 TEST_FW_DEV_ATTR(config_num_requests),
1065 TEST_FW_DEV_ATTR(config_into_buf),
1066 TEST_FW_DEV_ATTR(config_buf_size),
1067 TEST_FW_DEV_ATTR(config_file_offset),
1068 TEST_FW_DEV_ATTR(config_partial),
1069 TEST_FW_DEV_ATTR(config_sync_direct),
1070 TEST_FW_DEV_ATTR(config_send_uevent),
1071 TEST_FW_DEV_ATTR(config_read_fw_idx),
1073 /* These don't use the config at all - they could be ported! */
1074 TEST_FW_DEV_ATTR(trigger_request),
1075 TEST_FW_DEV_ATTR(trigger_async_request),
1076 TEST_FW_DEV_ATTR(trigger_custom_fallback),
1077 #ifdef CONFIG_EFI_EMBEDDED_FIRMWARE
1078 TEST_FW_DEV_ATTR(trigger_request_platform),
1081 /* These use the config and can use the test_result */
1082 TEST_FW_DEV_ATTR(trigger_batched_requests),
1083 TEST_FW_DEV_ATTR(trigger_batched_requests_async),
1085 TEST_FW_DEV_ATTR(release_all_firmware),
1086 TEST_FW_DEV_ATTR(test_result),
1087 TEST_FW_DEV_ATTR(read_firmware),
1091 ATTRIBUTE_GROUPS(test_dev);
1093 static struct miscdevice test_fw_misc_device = {
1094 .minor = MISC_DYNAMIC_MINOR,
1095 .name = "test_firmware",
1096 .fops = &test_fw_fops,
1097 .groups = test_dev_groups,
1100 static int __init test_firmware_init(void)
1104 test_fw_config = kzalloc(sizeof(struct test_config), GFP_KERNEL);
1105 if (!test_fw_config)
1108 rc = __test_firmware_config_init();
1110 kfree(test_fw_config);
1111 pr_err("could not init firmware test config: %d\n", rc);
1115 rc = misc_register(&test_fw_misc_device);
1117 kfree(test_fw_config);
1118 pr_err("could not register misc device: %d\n", rc);
1122 pr_warn("interface ready\n");
1127 module_init(test_firmware_init);
1129 static void __exit test_firmware_exit(void)
1131 mutex_lock(&test_fw_mutex);
1132 release_firmware(test_firmware);
1133 misc_deregister(&test_fw_misc_device);
1134 __test_firmware_config_free();
1135 kfree(test_fw_config);
1136 mutex_unlock(&test_fw_mutex);
1138 pr_warn("removed interface\n");
1141 module_exit(test_firmware_exit);
1143 MODULE_AUTHOR("Kees Cook <keescook@chromium.org>");
1144 MODULE_LICENSE("GPL");