xref: /linux-6.15/lib/test_firmware.c (revision eb910947)
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>
15*eb910947SBrian 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 struct miscdevice test_fw_misc_device = {
460a8adf58SKees Cook 	.minor          = MISC_DYNAMIC_MINOR,
470a8adf58SKees Cook 	.name           = "test_firmware",
480a8adf58SKees Cook 	.fops           = &test_fw_fops,
490a8adf58SKees Cook };
500a8adf58SKees Cook 
510a8adf58SKees Cook static ssize_t trigger_request_store(struct device *dev,
520a8adf58SKees Cook 				     struct device_attribute *attr,
530a8adf58SKees Cook 				     const char *buf, size_t count)
540a8adf58SKees Cook {
550a8adf58SKees Cook 	int rc;
560a8adf58SKees Cook 	char *name;
570a8adf58SKees Cook 
58be4a1326SBrian Norris 	name = kstrndup(buf, count, GFP_KERNEL);
590a8adf58SKees Cook 	if (!name)
600a8adf58SKees Cook 		return -ENOSPC;
610a8adf58SKees Cook 
620a8adf58SKees Cook 	pr_info("loading '%s'\n", name);
630a8adf58SKees Cook 
640a8adf58SKees Cook 	mutex_lock(&test_fw_mutex);
650a8adf58SKees Cook 	release_firmware(test_firmware);
660a8adf58SKees Cook 	test_firmware = NULL;
670a8adf58SKees Cook 	rc = request_firmware(&test_firmware, name, dev);
6847e0bbb7SBrian Norris 	if (rc) {
690a8adf58SKees Cook 		pr_info("load of '%s' failed: %d\n", name, rc);
7047e0bbb7SBrian Norris 		goto out;
7147e0bbb7SBrian Norris 	}
7247e0bbb7SBrian Norris 	pr_info("loaded: %zu\n", test_firmware->size);
7347e0bbb7SBrian Norris 	rc = count;
7447e0bbb7SBrian Norris 
7547e0bbb7SBrian Norris out:
760a8adf58SKees Cook 	mutex_unlock(&test_fw_mutex);
770a8adf58SKees Cook 
780a8adf58SKees Cook 	kfree(name);
790a8adf58SKees Cook 
8047e0bbb7SBrian Norris 	return rc;
810a8adf58SKees Cook }
820a8adf58SKees Cook static DEVICE_ATTR_WO(trigger_request);
830a8adf58SKees Cook 
84*eb910947SBrian Norris static DECLARE_COMPLETION(async_fw_done);
85*eb910947SBrian Norris 
86*eb910947SBrian Norris static void trigger_async_request_cb(const struct firmware *fw, void *context)
87*eb910947SBrian Norris {
88*eb910947SBrian Norris 	test_firmware = fw;
89*eb910947SBrian Norris 	complete(&async_fw_done);
90*eb910947SBrian Norris }
91*eb910947SBrian Norris 
92*eb910947SBrian Norris static ssize_t trigger_async_request_store(struct device *dev,
93*eb910947SBrian Norris 					   struct device_attribute *attr,
94*eb910947SBrian Norris 					   const char *buf, size_t count)
95*eb910947SBrian Norris {
96*eb910947SBrian Norris 	int rc;
97*eb910947SBrian Norris 	char *name;
98*eb910947SBrian Norris 
99*eb910947SBrian Norris 	name = kstrndup(buf, count, GFP_KERNEL);
100*eb910947SBrian Norris 	if (!name)
101*eb910947SBrian Norris 		return -ENOSPC;
102*eb910947SBrian Norris 
103*eb910947SBrian Norris 	pr_info("loading '%s'\n", name);
104*eb910947SBrian Norris 
105*eb910947SBrian Norris 	mutex_lock(&test_fw_mutex);
106*eb910947SBrian Norris 	release_firmware(test_firmware);
107*eb910947SBrian Norris 	test_firmware = NULL;
108*eb910947SBrian Norris 	rc = request_firmware_nowait(THIS_MODULE, 1, name, dev, GFP_KERNEL,
109*eb910947SBrian Norris 				     NULL, trigger_async_request_cb);
110*eb910947SBrian Norris 	if (rc) {
111*eb910947SBrian Norris 		pr_info("async load of '%s' failed: %d\n", name, rc);
112*eb910947SBrian Norris 		kfree(name);
113*eb910947SBrian Norris 		goto out;
114*eb910947SBrian Norris 	}
115*eb910947SBrian Norris 	/* Free 'name' ASAP, to test for race conditions */
116*eb910947SBrian Norris 	kfree(name);
117*eb910947SBrian Norris 
118*eb910947SBrian Norris 	wait_for_completion(&async_fw_done);
119*eb910947SBrian Norris 
120*eb910947SBrian Norris 	if (test_firmware) {
121*eb910947SBrian Norris 		pr_info("loaded: %zu\n", test_firmware->size);
122*eb910947SBrian Norris 		rc = count;
123*eb910947SBrian Norris 	} else {
124*eb910947SBrian Norris 		pr_err("failed to async load firmware\n");
125*eb910947SBrian Norris 		rc = -ENODEV;
126*eb910947SBrian Norris 	}
127*eb910947SBrian Norris 
128*eb910947SBrian Norris out:
129*eb910947SBrian Norris 	mutex_unlock(&test_fw_mutex);
130*eb910947SBrian Norris 
131*eb910947SBrian Norris 	return rc;
132*eb910947SBrian Norris }
133*eb910947SBrian Norris static DEVICE_ATTR_WO(trigger_async_request);
134*eb910947SBrian Norris 
1350a8adf58SKees Cook static int __init test_firmware_init(void)
1360a8adf58SKees Cook {
1370a8adf58SKees Cook 	int rc;
1380a8adf58SKees Cook 
1390a8adf58SKees Cook 	rc = misc_register(&test_fw_misc_device);
1400a8adf58SKees Cook 	if (rc) {
1410a8adf58SKees Cook 		pr_err("could not register misc device: %d\n", rc);
1420a8adf58SKees Cook 		return rc;
1430a8adf58SKees Cook 	}
1440a8adf58SKees Cook 	rc = device_create_file(test_fw_misc_device.this_device,
1450a8adf58SKees Cook 				&dev_attr_trigger_request);
1460a8adf58SKees Cook 	if (rc) {
1470a8adf58SKees Cook 		pr_err("could not create sysfs interface: %d\n", rc);
1480a8adf58SKees Cook 		goto dereg;
1490a8adf58SKees Cook 	}
1500a8adf58SKees Cook 
151*eb910947SBrian Norris 	rc = device_create_file(test_fw_misc_device.this_device,
152*eb910947SBrian Norris 				&dev_attr_trigger_async_request);
153*eb910947SBrian Norris 	if (rc) {
154*eb910947SBrian Norris 		pr_err("could not create async sysfs interface: %d\n", rc);
155*eb910947SBrian Norris 		goto remove_file;
156*eb910947SBrian Norris 	}
157*eb910947SBrian Norris 
1580a8adf58SKees Cook 	pr_warn("interface ready\n");
1590a8adf58SKees Cook 
1600a8adf58SKees Cook 	return 0;
161*eb910947SBrian Norris 
162*eb910947SBrian Norris remove_file:
163*eb910947SBrian Norris 	device_remove_file(test_fw_misc_device.this_device,
164*eb910947SBrian Norris 			   &dev_attr_trigger_async_request);
1650a8adf58SKees Cook dereg:
1660a8adf58SKees Cook 	misc_deregister(&test_fw_misc_device);
1670a8adf58SKees Cook 	return rc;
1680a8adf58SKees Cook }
1690a8adf58SKees Cook 
1700a8adf58SKees Cook module_init(test_firmware_init);
1710a8adf58SKees Cook 
1720a8adf58SKees Cook static void __exit test_firmware_exit(void)
1730a8adf58SKees Cook {
1740a8adf58SKees Cook 	release_firmware(test_firmware);
1750a8adf58SKees Cook 	device_remove_file(test_fw_misc_device.this_device,
176*eb910947SBrian Norris 			   &dev_attr_trigger_async_request);
177*eb910947SBrian Norris 	device_remove_file(test_fw_misc_device.this_device,
1780a8adf58SKees Cook 			   &dev_attr_trigger_request);
1790a8adf58SKees Cook 	misc_deregister(&test_fw_misc_device);
1800a8adf58SKees Cook 	pr_warn("removed interface\n");
1810a8adf58SKees Cook }
1820a8adf58SKees Cook 
1830a8adf58SKees Cook module_exit(test_firmware_exit);
1840a8adf58SKees Cook 
1850a8adf58SKees Cook MODULE_AUTHOR("Kees Cook <[email protected]>");
1860a8adf58SKees Cook MODULE_LICENSE("GPL");
187