xref: /linux-6.15/lib/test_firmware.c (revision 061132d2)
10a8adf58SKees Cook /*
20a8adf58SKees Cook  * This module provides an interface to trigger and test firmware loading.
30a8adf58SKees Cook  *
40a8adf58SKees Cook  * It is designed to be used for basic evaluation of the firmware loading
50a8adf58SKees Cook  * subsystem (for example when validating firmware verification). It lacks
60a8adf58SKees Cook  * any extra dependencies, and will not normally be loaded by the system
70a8adf58SKees Cook  * unless explicitly requested by name.
80a8adf58SKees Cook  */
90a8adf58SKees Cook 
100a8adf58SKees Cook #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
110a8adf58SKees Cook 
120a8adf58SKees Cook #include <linux/init.h>
130a8adf58SKees Cook #include <linux/module.h>
140a8adf58SKees Cook #include <linux/printk.h>
15eb910947SBrian Norris #include <linux/completion.h>
160a8adf58SKees Cook #include <linux/firmware.h>
170a8adf58SKees Cook #include <linux/device.h>
180a8adf58SKees Cook #include <linux/fs.h>
190a8adf58SKees Cook #include <linux/miscdevice.h>
200a8adf58SKees Cook #include <linux/slab.h>
210a8adf58SKees Cook #include <linux/uaccess.h>
220a8adf58SKees Cook 
230a8adf58SKees Cook static DEFINE_MUTEX(test_fw_mutex);
240a8adf58SKees Cook static const struct firmware *test_firmware;
250a8adf58SKees Cook 
260a8adf58SKees Cook static ssize_t test_fw_misc_read(struct file *f, char __user *buf,
270a8adf58SKees Cook 				 size_t size, loff_t *offset)
280a8adf58SKees Cook {
290a8adf58SKees Cook 	ssize_t rc = 0;
300a8adf58SKees Cook 
310a8adf58SKees Cook 	mutex_lock(&test_fw_mutex);
320a8adf58SKees Cook 	if (test_firmware)
330a8adf58SKees Cook 		rc = simple_read_from_buffer(buf, size, offset,
340a8adf58SKees Cook 					     test_firmware->data,
350a8adf58SKees Cook 					     test_firmware->size);
360a8adf58SKees Cook 	mutex_unlock(&test_fw_mutex);
370a8adf58SKees Cook 	return rc;
380a8adf58SKees Cook }
390a8adf58SKees Cook 
400a8adf58SKees Cook static const struct file_operations test_fw_fops = {
410a8adf58SKees Cook 	.owner          = THIS_MODULE,
420a8adf58SKees Cook 	.read           = test_fw_misc_read,
430a8adf58SKees Cook };
440a8adf58SKees Cook 
450a8adf58SKees Cook static ssize_t trigger_request_store(struct device *dev,
460a8adf58SKees Cook 				     struct device_attribute *attr,
470a8adf58SKees Cook 				     const char *buf, size_t count)
480a8adf58SKees Cook {
490a8adf58SKees Cook 	int rc;
500a8adf58SKees Cook 	char *name;
510a8adf58SKees Cook 
52be4a1326SBrian Norris 	name = kstrndup(buf, count, GFP_KERNEL);
530a8adf58SKees Cook 	if (!name)
540a8adf58SKees Cook 		return -ENOSPC;
550a8adf58SKees Cook 
560a8adf58SKees Cook 	pr_info("loading '%s'\n", name);
570a8adf58SKees Cook 
580a8adf58SKees Cook 	mutex_lock(&test_fw_mutex);
590a8adf58SKees Cook 	release_firmware(test_firmware);
600a8adf58SKees Cook 	test_firmware = NULL;
610a8adf58SKees Cook 	rc = request_firmware(&test_firmware, name, dev);
6247e0bbb7SBrian Norris 	if (rc) {
630a8adf58SKees Cook 		pr_info("load of '%s' failed: %d\n", name, rc);
6447e0bbb7SBrian Norris 		goto out;
6547e0bbb7SBrian Norris 	}
6647e0bbb7SBrian Norris 	pr_info("loaded: %zu\n", test_firmware->size);
6747e0bbb7SBrian Norris 	rc = count;
6847e0bbb7SBrian Norris 
6947e0bbb7SBrian Norris out:
700a8adf58SKees Cook 	mutex_unlock(&test_fw_mutex);
710a8adf58SKees Cook 
720a8adf58SKees Cook 	kfree(name);
730a8adf58SKees Cook 
7447e0bbb7SBrian Norris 	return rc;
750a8adf58SKees Cook }
760a8adf58SKees Cook static DEVICE_ATTR_WO(trigger_request);
770a8adf58SKees Cook 
78eb910947SBrian Norris static DECLARE_COMPLETION(async_fw_done);
79eb910947SBrian Norris 
80eb910947SBrian Norris static void trigger_async_request_cb(const struct firmware *fw, void *context)
81eb910947SBrian Norris {
82eb910947SBrian Norris 	test_firmware = fw;
83eb910947SBrian Norris 	complete(&async_fw_done);
84eb910947SBrian Norris }
85eb910947SBrian Norris 
86eb910947SBrian Norris static ssize_t trigger_async_request_store(struct device *dev,
87eb910947SBrian Norris 					   struct device_attribute *attr,
88eb910947SBrian Norris 					   const char *buf, size_t count)
89eb910947SBrian Norris {
90eb910947SBrian Norris 	int rc;
91eb910947SBrian Norris 	char *name;
92eb910947SBrian Norris 
93eb910947SBrian Norris 	name = kstrndup(buf, count, GFP_KERNEL);
94eb910947SBrian Norris 	if (!name)
95eb910947SBrian Norris 		return -ENOSPC;
96eb910947SBrian Norris 
97eb910947SBrian Norris 	pr_info("loading '%s'\n", name);
98eb910947SBrian Norris 
99eb910947SBrian Norris 	mutex_lock(&test_fw_mutex);
100eb910947SBrian Norris 	release_firmware(test_firmware);
101eb910947SBrian Norris 	test_firmware = NULL;
102eb910947SBrian Norris 	rc = request_firmware_nowait(THIS_MODULE, 1, name, dev, GFP_KERNEL,
103eb910947SBrian Norris 				     NULL, trigger_async_request_cb);
104eb910947SBrian Norris 	if (rc) {
105eb910947SBrian Norris 		pr_info("async load of '%s' failed: %d\n", name, rc);
106eb910947SBrian Norris 		kfree(name);
107eb910947SBrian Norris 		goto out;
108eb910947SBrian Norris 	}
109eb910947SBrian Norris 	/* Free 'name' ASAP, to test for race conditions */
110eb910947SBrian Norris 	kfree(name);
111eb910947SBrian Norris 
112eb910947SBrian Norris 	wait_for_completion(&async_fw_done);
113eb910947SBrian Norris 
114eb910947SBrian Norris 	if (test_firmware) {
115eb910947SBrian Norris 		pr_info("loaded: %zu\n", test_firmware->size);
116eb910947SBrian Norris 		rc = count;
117eb910947SBrian Norris 	} else {
118eb910947SBrian Norris 		pr_err("failed to async load firmware\n");
119eb910947SBrian Norris 		rc = -ENODEV;
120eb910947SBrian Norris 	}
121eb910947SBrian Norris 
122eb910947SBrian Norris out:
123eb910947SBrian Norris 	mutex_unlock(&test_fw_mutex);
124eb910947SBrian Norris 
125eb910947SBrian Norris 	return rc;
126eb910947SBrian Norris }
127eb910947SBrian Norris static DEVICE_ATTR_WO(trigger_async_request);
128eb910947SBrian Norris 
129*061132d2SLuis R. Rodriguez static ssize_t trigger_custom_fallback_store(struct device *dev,
130*061132d2SLuis R. Rodriguez 					     struct device_attribute *attr,
131*061132d2SLuis R. Rodriguez 					     const char *buf, size_t count)
132*061132d2SLuis R. Rodriguez {
133*061132d2SLuis R. Rodriguez 	int rc;
134*061132d2SLuis R. Rodriguez 	char *name;
135*061132d2SLuis R. Rodriguez 
136*061132d2SLuis R. Rodriguez 	name = kstrndup(buf, count, GFP_KERNEL);
137*061132d2SLuis R. Rodriguez 	if (!name)
138*061132d2SLuis R. Rodriguez 		return -ENOSPC;
139*061132d2SLuis R. Rodriguez 
140*061132d2SLuis R. Rodriguez 	pr_info("loading '%s' using custom fallback mechanism\n", name);
141*061132d2SLuis R. Rodriguez 
142*061132d2SLuis R. Rodriguez 	mutex_lock(&test_fw_mutex);
143*061132d2SLuis R. Rodriguez 	release_firmware(test_firmware);
144*061132d2SLuis R. Rodriguez 	test_firmware = NULL;
145*061132d2SLuis R. Rodriguez 	rc = request_firmware_nowait(THIS_MODULE, FW_ACTION_NOHOTPLUG, name,
146*061132d2SLuis R. Rodriguez 				     dev, GFP_KERNEL, NULL,
147*061132d2SLuis R. Rodriguez 				     trigger_async_request_cb);
148*061132d2SLuis R. Rodriguez 	if (rc) {
149*061132d2SLuis R. Rodriguez 		pr_info("async load of '%s' failed: %d\n", name, rc);
150*061132d2SLuis R. Rodriguez 		kfree(name);
151*061132d2SLuis R. Rodriguez 		goto out;
152*061132d2SLuis R. Rodriguez 	}
153*061132d2SLuis R. Rodriguez 	/* Free 'name' ASAP, to test for race conditions */
154*061132d2SLuis R. Rodriguez 	kfree(name);
155*061132d2SLuis R. Rodriguez 
156*061132d2SLuis R. Rodriguez 	wait_for_completion(&async_fw_done);
157*061132d2SLuis R. Rodriguez 
158*061132d2SLuis R. Rodriguez 	if (test_firmware) {
159*061132d2SLuis R. Rodriguez 		pr_info("loaded: %zu\n", test_firmware->size);
160*061132d2SLuis R. Rodriguez 		rc = count;
161*061132d2SLuis R. Rodriguez 	} else {
162*061132d2SLuis R. Rodriguez 		pr_err("failed to async load firmware\n");
163*061132d2SLuis R. Rodriguez 		rc = -ENODEV;
164*061132d2SLuis R. Rodriguez 	}
165*061132d2SLuis R. Rodriguez 
166*061132d2SLuis R. Rodriguez out:
167*061132d2SLuis R. Rodriguez 	mutex_unlock(&test_fw_mutex);
168*061132d2SLuis R. Rodriguez 
169*061132d2SLuis R. Rodriguez 	return rc;
170*061132d2SLuis R. Rodriguez }
171*061132d2SLuis R. Rodriguez static DEVICE_ATTR_WO(trigger_custom_fallback);
172*061132d2SLuis R. Rodriguez 
173083a93b0SLuis R. Rodriguez #define TEST_FW_DEV_ATTR(name)          &dev_attr_##name.attr
174083a93b0SLuis R. Rodriguez 
175083a93b0SLuis R. Rodriguez static struct attribute *test_dev_attrs[] = {
176083a93b0SLuis R. Rodriguez 	TEST_FW_DEV_ATTR(trigger_request),
177083a93b0SLuis R. Rodriguez 	TEST_FW_DEV_ATTR(trigger_async_request),
178*061132d2SLuis R. Rodriguez 	TEST_FW_DEV_ATTR(trigger_custom_fallback),
179083a93b0SLuis R. Rodriguez 	NULL,
180083a93b0SLuis R. Rodriguez };
181083a93b0SLuis R. Rodriguez 
182083a93b0SLuis R. Rodriguez ATTRIBUTE_GROUPS(test_dev);
183083a93b0SLuis R. Rodriguez 
18467fd553cSLuis R. Rodriguez static struct miscdevice test_fw_misc_device = {
18567fd553cSLuis R. Rodriguez 	.minor          = MISC_DYNAMIC_MINOR,
18667fd553cSLuis R. Rodriguez 	.name           = "test_firmware",
18767fd553cSLuis R. Rodriguez 	.fops           = &test_fw_fops,
188083a93b0SLuis R. Rodriguez 	.groups 	= test_dev_groups,
18967fd553cSLuis R. Rodriguez };
19067fd553cSLuis R. Rodriguez 
1910a8adf58SKees Cook static int __init test_firmware_init(void)
1920a8adf58SKees Cook {
1930a8adf58SKees Cook 	int rc;
1940a8adf58SKees Cook 
1950a8adf58SKees Cook 	rc = misc_register(&test_fw_misc_device);
1960a8adf58SKees Cook 	if (rc) {
1970a8adf58SKees Cook 		pr_err("could not register misc device: %d\n", rc);
1980a8adf58SKees Cook 		return rc;
1990a8adf58SKees Cook 	}
200eb910947SBrian Norris 
2010a8adf58SKees Cook 	pr_warn("interface ready\n");
2020a8adf58SKees Cook 
2030a8adf58SKees Cook 	return 0;
2040a8adf58SKees Cook }
2050a8adf58SKees Cook 
2060a8adf58SKees Cook module_init(test_firmware_init);
2070a8adf58SKees Cook 
2080a8adf58SKees Cook static void __exit test_firmware_exit(void)
2090a8adf58SKees Cook {
2100a8adf58SKees Cook 	release_firmware(test_firmware);
2110a8adf58SKees Cook 	misc_deregister(&test_fw_misc_device);
2120a8adf58SKees Cook 	pr_warn("removed interface\n");
2130a8adf58SKees Cook }
2140a8adf58SKees Cook 
2150a8adf58SKees Cook module_exit(test_firmware_exit);
2160a8adf58SKees Cook 
2170a8adf58SKees Cook MODULE_AUTHOR("Kees Cook <[email protected]>");
2180a8adf58SKees Cook MODULE_LICENSE("GPL");
219