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> 15eb910947SBrian 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 ssize_t trigger_request_store(struct device *dev, 460a8adf58SKees Cook struct device_attribute *attr, 470a8adf58SKees Cook const char *buf, size_t count) 480a8adf58SKees Cook { 490a8adf58SKees Cook int rc; 500a8adf58SKees Cook char *name; 510a8adf58SKees Cook 52be4a1326SBrian Norris name = kstrndup(buf, count, GFP_KERNEL); 530a8adf58SKees Cook if (!name) 540a8adf58SKees Cook return -ENOSPC; 550a8adf58SKees Cook 560a8adf58SKees Cook pr_info("loading '%s'\n", name); 570a8adf58SKees Cook 580a8adf58SKees Cook mutex_lock(&test_fw_mutex); 590a8adf58SKees Cook release_firmware(test_firmware); 600a8adf58SKees Cook test_firmware = NULL; 610a8adf58SKees Cook rc = request_firmware(&test_firmware, name, dev); 6247e0bbb7SBrian Norris if (rc) { 630a8adf58SKees Cook pr_info("load of '%s' failed: %d\n", name, rc); 6447e0bbb7SBrian Norris goto out; 6547e0bbb7SBrian Norris } 6647e0bbb7SBrian Norris pr_info("loaded: %zu\n", test_firmware->size); 6747e0bbb7SBrian Norris rc = count; 6847e0bbb7SBrian Norris 6947e0bbb7SBrian Norris out: 700a8adf58SKees Cook mutex_unlock(&test_fw_mutex); 710a8adf58SKees Cook 720a8adf58SKees Cook kfree(name); 730a8adf58SKees Cook 7447e0bbb7SBrian Norris return rc; 750a8adf58SKees Cook } 760a8adf58SKees Cook static DEVICE_ATTR_WO(trigger_request); 770a8adf58SKees Cook 78eb910947SBrian Norris static DECLARE_COMPLETION(async_fw_done); 79eb910947SBrian Norris 80eb910947SBrian Norris static void trigger_async_request_cb(const struct firmware *fw, void *context) 81eb910947SBrian Norris { 82eb910947SBrian Norris test_firmware = fw; 83eb910947SBrian Norris complete(&async_fw_done); 84eb910947SBrian Norris } 85eb910947SBrian Norris 86eb910947SBrian Norris static ssize_t trigger_async_request_store(struct device *dev, 87eb910947SBrian Norris struct device_attribute *attr, 88eb910947SBrian Norris const char *buf, size_t count) 89eb910947SBrian Norris { 90eb910947SBrian Norris int rc; 91eb910947SBrian Norris char *name; 92eb910947SBrian Norris 93eb910947SBrian Norris name = kstrndup(buf, count, GFP_KERNEL); 94eb910947SBrian Norris if (!name) 95eb910947SBrian Norris return -ENOSPC; 96eb910947SBrian Norris 97eb910947SBrian Norris pr_info("loading '%s'\n", name); 98eb910947SBrian Norris 99eb910947SBrian Norris mutex_lock(&test_fw_mutex); 100eb910947SBrian Norris release_firmware(test_firmware); 101eb910947SBrian Norris test_firmware = NULL; 102eb910947SBrian Norris rc = request_firmware_nowait(THIS_MODULE, 1, name, dev, GFP_KERNEL, 103eb910947SBrian Norris NULL, trigger_async_request_cb); 104eb910947SBrian Norris if (rc) { 105eb910947SBrian Norris pr_info("async load of '%s' failed: %d\n", name, rc); 106eb910947SBrian Norris kfree(name); 107eb910947SBrian Norris goto out; 108eb910947SBrian Norris } 109eb910947SBrian Norris /* Free 'name' ASAP, to test for race conditions */ 110eb910947SBrian Norris kfree(name); 111eb910947SBrian Norris 112eb910947SBrian Norris wait_for_completion(&async_fw_done); 113eb910947SBrian Norris 114eb910947SBrian Norris if (test_firmware) { 115eb910947SBrian Norris pr_info("loaded: %zu\n", test_firmware->size); 116eb910947SBrian Norris rc = count; 117eb910947SBrian Norris } else { 118eb910947SBrian Norris pr_err("failed to async load firmware\n"); 119eb910947SBrian Norris rc = -ENODEV; 120eb910947SBrian Norris } 121eb910947SBrian Norris 122eb910947SBrian Norris out: 123eb910947SBrian Norris mutex_unlock(&test_fw_mutex); 124eb910947SBrian Norris 125eb910947SBrian Norris return rc; 126eb910947SBrian Norris } 127eb910947SBrian Norris static DEVICE_ATTR_WO(trigger_async_request); 128eb910947SBrian Norris 129*67fd553cSLuis R. Rodriguez static struct miscdevice test_fw_misc_device = { 130*67fd553cSLuis R. Rodriguez .minor = MISC_DYNAMIC_MINOR, 131*67fd553cSLuis R. Rodriguez .name = "test_firmware", 132*67fd553cSLuis R. Rodriguez .fops = &test_fw_fops, 133*67fd553cSLuis R. Rodriguez }; 134*67fd553cSLuis R. Rodriguez 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 151eb910947SBrian Norris rc = device_create_file(test_fw_misc_device.this_device, 152eb910947SBrian Norris &dev_attr_trigger_async_request); 153eb910947SBrian Norris if (rc) { 154eb910947SBrian Norris pr_err("could not create async sysfs interface: %d\n", rc); 155eb910947SBrian Norris goto remove_file; 156eb910947SBrian Norris } 157eb910947SBrian Norris 1580a8adf58SKees Cook pr_warn("interface ready\n"); 1590a8adf58SKees Cook 1600a8adf58SKees Cook return 0; 161eb910947SBrian Norris 162eb910947SBrian Norris remove_file: 163eb910947SBrian Norris device_remove_file(test_fw_misc_device.this_device, 164eb910947SBrian 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, 176eb910947SBrian Norris &dev_attr_trigger_async_request); 177eb910947SBrian 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