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> 150a8adf58SKees Cook #include <linux/firmware.h> 160a8adf58SKees Cook #include <linux/device.h> 170a8adf58SKees Cook #include <linux/fs.h> 180a8adf58SKees Cook #include <linux/miscdevice.h> 190a8adf58SKees Cook #include <linux/slab.h> 200a8adf58SKees Cook #include <linux/uaccess.h> 210a8adf58SKees Cook 220a8adf58SKees Cook static DEFINE_MUTEX(test_fw_mutex); 230a8adf58SKees Cook static const struct firmware *test_firmware; 240a8adf58SKees Cook 250a8adf58SKees Cook static ssize_t test_fw_misc_read(struct file *f, char __user *buf, 260a8adf58SKees Cook size_t size, loff_t *offset) 270a8adf58SKees Cook { 280a8adf58SKees Cook ssize_t rc = 0; 290a8adf58SKees Cook 300a8adf58SKees Cook mutex_lock(&test_fw_mutex); 310a8adf58SKees Cook if (test_firmware) 320a8adf58SKees Cook rc = simple_read_from_buffer(buf, size, offset, 330a8adf58SKees Cook test_firmware->data, 340a8adf58SKees Cook test_firmware->size); 350a8adf58SKees Cook mutex_unlock(&test_fw_mutex); 360a8adf58SKees Cook return rc; 370a8adf58SKees Cook } 380a8adf58SKees Cook 390a8adf58SKees Cook static const struct file_operations test_fw_fops = { 400a8adf58SKees Cook .owner = THIS_MODULE, 410a8adf58SKees Cook .read = test_fw_misc_read, 420a8adf58SKees Cook }; 430a8adf58SKees Cook 440a8adf58SKees Cook static struct miscdevice test_fw_misc_device = { 450a8adf58SKees Cook .minor = MISC_DYNAMIC_MINOR, 460a8adf58SKees Cook .name = "test_firmware", 470a8adf58SKees Cook .fops = &test_fw_fops, 480a8adf58SKees Cook }; 490a8adf58SKees Cook 500a8adf58SKees Cook static ssize_t trigger_request_store(struct device *dev, 510a8adf58SKees Cook struct device_attribute *attr, 520a8adf58SKees Cook const char *buf, size_t count) 530a8adf58SKees Cook { 540a8adf58SKees Cook int rc; 550a8adf58SKees Cook char *name; 560a8adf58SKees Cook 57*be4a1326SBrian Norris name = kstrndup(buf, count, GFP_KERNEL); 580a8adf58SKees Cook if (!name) 590a8adf58SKees Cook return -ENOSPC; 600a8adf58SKees Cook 610a8adf58SKees Cook pr_info("loading '%s'\n", name); 620a8adf58SKees Cook 630a8adf58SKees Cook mutex_lock(&test_fw_mutex); 640a8adf58SKees Cook release_firmware(test_firmware); 650a8adf58SKees Cook test_firmware = NULL; 660a8adf58SKees Cook rc = request_firmware(&test_firmware, name, dev); 6747e0bbb7SBrian Norris if (rc) { 680a8adf58SKees Cook pr_info("load of '%s' failed: %d\n", name, rc); 6947e0bbb7SBrian Norris goto out; 7047e0bbb7SBrian Norris } 7147e0bbb7SBrian Norris pr_info("loaded: %zu\n", test_firmware->size); 7247e0bbb7SBrian Norris rc = count; 7347e0bbb7SBrian Norris 7447e0bbb7SBrian Norris out: 750a8adf58SKees Cook mutex_unlock(&test_fw_mutex); 760a8adf58SKees Cook 770a8adf58SKees Cook kfree(name); 780a8adf58SKees Cook 7947e0bbb7SBrian Norris return rc; 800a8adf58SKees Cook } 810a8adf58SKees Cook static DEVICE_ATTR_WO(trigger_request); 820a8adf58SKees Cook 830a8adf58SKees Cook static int __init test_firmware_init(void) 840a8adf58SKees Cook { 850a8adf58SKees Cook int rc; 860a8adf58SKees Cook 870a8adf58SKees Cook rc = misc_register(&test_fw_misc_device); 880a8adf58SKees Cook if (rc) { 890a8adf58SKees Cook pr_err("could not register misc device: %d\n", rc); 900a8adf58SKees Cook return rc; 910a8adf58SKees Cook } 920a8adf58SKees Cook rc = device_create_file(test_fw_misc_device.this_device, 930a8adf58SKees Cook &dev_attr_trigger_request); 940a8adf58SKees Cook if (rc) { 950a8adf58SKees Cook pr_err("could not create sysfs interface: %d\n", rc); 960a8adf58SKees Cook goto dereg; 970a8adf58SKees Cook } 980a8adf58SKees Cook 990a8adf58SKees Cook pr_warn("interface ready\n"); 1000a8adf58SKees Cook 1010a8adf58SKees Cook return 0; 1020a8adf58SKees Cook dereg: 1030a8adf58SKees Cook misc_deregister(&test_fw_misc_device); 1040a8adf58SKees Cook return rc; 1050a8adf58SKees Cook } 1060a8adf58SKees Cook 1070a8adf58SKees Cook module_init(test_firmware_init); 1080a8adf58SKees Cook 1090a8adf58SKees Cook static void __exit test_firmware_exit(void) 1100a8adf58SKees Cook { 1110a8adf58SKees Cook release_firmware(test_firmware); 1120a8adf58SKees Cook device_remove_file(test_fw_misc_device.this_device, 1130a8adf58SKees Cook &dev_attr_trigger_request); 1140a8adf58SKees Cook misc_deregister(&test_fw_misc_device); 1150a8adf58SKees Cook pr_warn("removed interface\n"); 1160a8adf58SKees Cook } 1170a8adf58SKees Cook 1180a8adf58SKees Cook module_exit(test_firmware_exit); 1190a8adf58SKees Cook 1200a8adf58SKees Cook MODULE_AUTHOR("Kees Cook <[email protected]>"); 1210a8adf58SKees Cook MODULE_LICENSE("GPL"); 122