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 570a8adf58SKees Cook name = kzalloc(count + 1, GFP_KERNEL); 580a8adf58SKees Cook if (!name) 590a8adf58SKees Cook return -ENOSPC; 600a8adf58SKees Cook memcpy(name, buf, count); 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); 68*47e0bbb7SBrian Norris if (rc) { 690a8adf58SKees Cook pr_info("load of '%s' failed: %d\n", name, rc); 70*47e0bbb7SBrian Norris goto out; 71*47e0bbb7SBrian Norris } 72*47e0bbb7SBrian Norris pr_info("loaded: %zu\n", test_firmware->size); 73*47e0bbb7SBrian Norris rc = count; 74*47e0bbb7SBrian Norris 75*47e0bbb7SBrian Norris out: 760a8adf58SKees Cook mutex_unlock(&test_fw_mutex); 770a8adf58SKees Cook 780a8adf58SKees Cook kfree(name); 790a8adf58SKees Cook 80*47e0bbb7SBrian Norris return rc; 810a8adf58SKees Cook } 820a8adf58SKees Cook static DEVICE_ATTR_WO(trigger_request); 830a8adf58SKees Cook 840a8adf58SKees Cook static int __init test_firmware_init(void) 850a8adf58SKees Cook { 860a8adf58SKees Cook int rc; 870a8adf58SKees Cook 880a8adf58SKees Cook rc = misc_register(&test_fw_misc_device); 890a8adf58SKees Cook if (rc) { 900a8adf58SKees Cook pr_err("could not register misc device: %d\n", rc); 910a8adf58SKees Cook return rc; 920a8adf58SKees Cook } 930a8adf58SKees Cook rc = device_create_file(test_fw_misc_device.this_device, 940a8adf58SKees Cook &dev_attr_trigger_request); 950a8adf58SKees Cook if (rc) { 960a8adf58SKees Cook pr_err("could not create sysfs interface: %d\n", rc); 970a8adf58SKees Cook goto dereg; 980a8adf58SKees Cook } 990a8adf58SKees Cook 1000a8adf58SKees Cook pr_warn("interface ready\n"); 1010a8adf58SKees Cook 1020a8adf58SKees Cook return 0; 1030a8adf58SKees Cook dereg: 1040a8adf58SKees Cook misc_deregister(&test_fw_misc_device); 1050a8adf58SKees Cook return rc; 1060a8adf58SKees Cook } 1070a8adf58SKees Cook 1080a8adf58SKees Cook module_init(test_firmware_init); 1090a8adf58SKees Cook 1100a8adf58SKees Cook static void __exit test_firmware_exit(void) 1110a8adf58SKees Cook { 1120a8adf58SKees Cook release_firmware(test_firmware); 1130a8adf58SKees Cook device_remove_file(test_fw_misc_device.this_device, 1140a8adf58SKees Cook &dev_attr_trigger_request); 1150a8adf58SKees Cook misc_deregister(&test_fw_misc_device); 1160a8adf58SKees Cook pr_warn("removed interface\n"); 1170a8adf58SKees Cook } 1180a8adf58SKees Cook 1190a8adf58SKees Cook module_exit(test_firmware_exit); 1200a8adf58SKees Cook 1210a8adf58SKees Cook MODULE_AUTHOR("Kees Cook <[email protected]>"); 1220a8adf58SKees Cook MODULE_LICENSE("GPL"); 123