blob: 690b9c35a274d925a673b1656a3f044e87d789d1 [file] [log] [blame]
Kees Cook0a8adf52014-07-14 14:38:12 -07001/*
2 * This module provides an interface to trigger and test firmware loading.
3 *
4 * It is designed to be used for basic evaluation of the firmware loading
5 * subsystem (for example when validating firmware verification). It lacks
6 * any extra dependencies, and will not normally be loaded by the system
7 * unless explicitly requested by name.
8 */
9
10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/printk.h>
15#include <linux/firmware.h>
16#include <linux/device.h>
17#include <linux/fs.h>
18#include <linux/miscdevice.h>
19#include <linux/slab.h>
20#include <linux/uaccess.h>
21
22static DEFINE_MUTEX(test_fw_mutex);
23static const struct firmware *test_firmware;
24
25static ssize_t test_fw_misc_read(struct file *f, char __user *buf,
26 size_t size, loff_t *offset)
27{
28 ssize_t rc = 0;
29
30 mutex_lock(&test_fw_mutex);
31 if (test_firmware)
32 rc = simple_read_from_buffer(buf, size, offset,
33 test_firmware->data,
34 test_firmware->size);
35 mutex_unlock(&test_fw_mutex);
36 return rc;
37}
38
39static const struct file_operations test_fw_fops = {
40 .owner = THIS_MODULE,
41 .read = test_fw_misc_read,
42};
43
44static struct miscdevice test_fw_misc_device = {
45 .minor = MISC_DYNAMIC_MINOR,
46 .name = "test_firmware",
47 .fops = &test_fw_fops,
48};
49
50static ssize_t trigger_request_store(struct device *dev,
51 struct device_attribute *attr,
52 const char *buf, size_t count)
53{
54 int rc;
55 char *name;
56
Brian Norrisbe4a1322015-12-09 14:50:26 -080057 name = kstrndup(buf, count, GFP_KERNEL);
Kees Cook0a8adf52014-07-14 14:38:12 -070058 if (!name)
59 return -ENOSPC;
Kees Cook0a8adf52014-07-14 14:38:12 -070060
61 pr_info("loading '%s'\n", name);
62
63 mutex_lock(&test_fw_mutex);
64 release_firmware(test_firmware);
65 test_firmware = NULL;
66 rc = request_firmware(&test_firmware, name, dev);
Brian Norris47e0bbb2015-12-09 14:50:25 -080067 if (rc) {
Kees Cook0a8adf52014-07-14 14:38:12 -070068 pr_info("load of '%s' failed: %d\n", name, rc);
Brian Norris47e0bbb2015-12-09 14:50:25 -080069 goto out;
70 }
71 pr_info("loaded: %zu\n", test_firmware->size);
72 rc = count;
73
74out:
Kees Cook0a8adf52014-07-14 14:38:12 -070075 mutex_unlock(&test_fw_mutex);
76
77 kfree(name);
78
Brian Norris47e0bbb2015-12-09 14:50:25 -080079 return rc;
Kees Cook0a8adf52014-07-14 14:38:12 -070080}
81static DEVICE_ATTR_WO(trigger_request);
82
83static int __init test_firmware_init(void)
84{
85 int rc;
86
87 rc = misc_register(&test_fw_misc_device);
88 if (rc) {
89 pr_err("could not register misc device: %d\n", rc);
90 return rc;
91 }
92 rc = device_create_file(test_fw_misc_device.this_device,
93 &dev_attr_trigger_request);
94 if (rc) {
95 pr_err("could not create sysfs interface: %d\n", rc);
96 goto dereg;
97 }
98
99 pr_warn("interface ready\n");
100
101 return 0;
102dereg:
103 misc_deregister(&test_fw_misc_device);
104 return rc;
105}
106
107module_init(test_firmware_init);
108
109static void __exit test_firmware_exit(void)
110{
111 release_firmware(test_firmware);
112 device_remove_file(test_fw_misc_device.this_device,
113 &dev_attr_trigger_request);
114 misc_deregister(&test_fw_misc_device);
115 pr_warn("removed interface\n");
116}
117
118module_exit(test_firmware_exit);
119
120MODULE_AUTHOR("Kees Cook <keescook@chromium.org>");
121MODULE_LICENSE("GPL");