2 * This module provides an interface to trigger and test firmware loading.
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.
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/printk.h>
15 #include <linux/completion.h>
16 #include <linux/firmware.h>
17 #include <linux/device.h>
19 #include <linux/miscdevice.h>
20 #include <linux/slab.h>
21 #include <linux/uaccess.h>
23 static DEFINE_MUTEX(test_fw_mutex
);
24 static const struct firmware
*test_firmware
;
26 static ssize_t
test_fw_misc_read(struct file
*f
, char __user
*buf
,
27 size_t size
, loff_t
*offset
)
31 mutex_lock(&test_fw_mutex
);
33 rc
= simple_read_from_buffer(buf
, size
, offset
,
36 mutex_unlock(&test_fw_mutex
);
40 static const struct file_operations test_fw_fops
= {
42 .read
= test_fw_misc_read
,
45 static struct miscdevice test_fw_misc_device
= {
46 .minor
= MISC_DYNAMIC_MINOR
,
47 .name
= "test_firmware",
48 .fops
= &test_fw_fops
,
51 static ssize_t
trigger_request_store(struct device
*dev
,
52 struct device_attribute
*attr
,
53 const char *buf
, size_t count
)
58 name
= kstrndup(buf
, count
, GFP_KERNEL
);
62 pr_info("loading '%s'\n", name
);
64 mutex_lock(&test_fw_mutex
);
65 release_firmware(test_firmware
);
67 rc
= request_firmware(&test_firmware
, name
, dev
);
69 pr_info("load of '%s' failed: %d\n", name
, rc
);
72 pr_info("loaded: %zu\n", test_firmware
->size
);
76 mutex_unlock(&test_fw_mutex
);
82 static DEVICE_ATTR_WO(trigger_request
);
84 static DECLARE_COMPLETION(async_fw_done
);
86 static void trigger_async_request_cb(const struct firmware
*fw
, void *context
)
89 complete(&async_fw_done
);
92 static ssize_t
trigger_async_request_store(struct device
*dev
,
93 struct device_attribute
*attr
,
94 const char *buf
, size_t count
)
99 name
= kstrndup(buf
, count
, GFP_KERNEL
);
103 pr_info("loading '%s'\n", name
);
105 mutex_lock(&test_fw_mutex
);
106 release_firmware(test_firmware
);
107 test_firmware
= NULL
;
108 rc
= request_firmware_nowait(THIS_MODULE
, 1, name
, dev
, GFP_KERNEL
,
109 NULL
, trigger_async_request_cb
);
111 pr_info("async load of '%s' failed: %d\n", name
, rc
);
115 /* Free 'name' ASAP, to test for race conditions */
118 wait_for_completion(&async_fw_done
);
121 pr_info("loaded: %zu\n", test_firmware
->size
);
124 pr_err("failed to async load firmware\n");
129 mutex_unlock(&test_fw_mutex
);
133 static DEVICE_ATTR_WO(trigger_async_request
);
135 static int __init
test_firmware_init(void)
139 rc
= misc_register(&test_fw_misc_device
);
141 pr_err("could not register misc device: %d\n", rc
);
144 rc
= device_create_file(test_fw_misc_device
.this_device
,
145 &dev_attr_trigger_request
);
147 pr_err("could not create sysfs interface: %d\n", rc
);
151 rc
= device_create_file(test_fw_misc_device
.this_device
,
152 &dev_attr_trigger_async_request
);
154 pr_err("could not create async sysfs interface: %d\n", rc
);
158 pr_warn("interface ready\n");
163 device_remove_file(test_fw_misc_device
.this_device
,
164 &dev_attr_trigger_async_request
);
166 misc_deregister(&test_fw_misc_device
);
170 module_init(test_firmware_init
);
172 static void __exit
test_firmware_exit(void)
174 release_firmware(test_firmware
);
175 device_remove_file(test_fw_misc_device
.this_device
,
176 &dev_attr_trigger_async_request
);
177 device_remove_file(test_fw_misc_device
.this_device
,
178 &dev_attr_trigger_request
);
179 misc_deregister(&test_fw_misc_device
);
180 pr_warn("removed interface\n");
183 module_exit(test_firmware_exit
);
185 MODULE_AUTHOR("Kees Cook <keescook@chromium.org>");
186 MODULE_LICENSE("GPL");