~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/lib/test_firmware.c

Version: ~ [ linux-5.4-rc3 ] ~ [ linux-5.3.6 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.79 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.149 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.196 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.196 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.75 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  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 
 22 static DEFINE_MUTEX(test_fw_mutex);
 23 static const struct firmware *test_firmware;
 24 
 25 static 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 
 39 static const struct file_operations test_fw_fops = {
 40         .owner          = THIS_MODULE,
 41         .read           = test_fw_misc_read,
 42 };
 43 
 44 static struct miscdevice test_fw_misc_device = {
 45         .minor          = MISC_DYNAMIC_MINOR,
 46         .name           = "test_firmware",
 47         .fops           = &test_fw_fops,
 48 };
 49 
 50 static 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 
 57         name = kzalloc(count + 1, GFP_KERNEL);
 58         if (!name)
 59                 return -ENOSPC;
 60         memcpy(name, buf, count);
 61 
 62         pr_info("loading '%s'\n", name);
 63 
 64         mutex_lock(&test_fw_mutex);
 65         release_firmware(test_firmware);
 66         test_firmware = NULL;
 67         rc = request_firmware(&test_firmware, name, dev);
 68         if (rc) {
 69                 pr_info("load of '%s' failed: %d\n", name, rc);
 70                 goto out;
 71         }
 72         pr_info("loaded: %zu\n", test_firmware->size);
 73         rc = count;
 74 
 75 out:
 76         mutex_unlock(&test_fw_mutex);
 77 
 78         kfree(name);
 79 
 80         return rc;
 81 }
 82 static DEVICE_ATTR_WO(trigger_request);
 83 
 84 static int __init test_firmware_init(void)
 85 {
 86         int rc;
 87 
 88         rc = misc_register(&test_fw_misc_device);
 89         if (rc) {
 90                 pr_err("could not register misc device: %d\n", rc);
 91                 return rc;
 92         }
 93         rc = device_create_file(test_fw_misc_device.this_device,
 94                                 &dev_attr_trigger_request);
 95         if (rc) {
 96                 pr_err("could not create sysfs interface: %d\n", rc);
 97                 goto dereg;
 98         }
 99 
100         pr_warn("interface ready\n");
101 
102         return 0;
103 dereg:
104         misc_deregister(&test_fw_misc_device);
105         return rc;
106 }
107 
108 module_init(test_firmware_init);
109 
110 static void __exit test_firmware_exit(void)
111 {
112         release_firmware(test_firmware);
113         device_remove_file(test_fw_misc_device.this_device,
114                            &dev_attr_trigger_request);
115         misc_deregister(&test_fw_misc_device);
116         pr_warn("removed interface\n");
117 }
118 
119 module_exit(test_firmware_exit);
120 
121 MODULE_AUTHOR("Kees Cook <keescook@chromium.org>");
122 MODULE_LICENSE("GPL");
123 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp