test_firmware.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/init.h>
  11. #include <linux/module.h>
  12. #include <linux/printk.h>
  13. #include <linux/firmware.h>
  14. #include <linux/device.h>
  15. #include <linux/fs.h>
  16. #include <linux/miscdevice.h>
  17. #include <linux/slab.h>
  18. #include <linux/uaccess.h>
  19. static DEFINE_MUTEX(test_fw_mutex);
  20. static const struct firmware *test_firmware;
  21. static ssize_t test_fw_misc_read(struct file *f, char __user *buf,
  22. size_t size, loff_t *offset)
  23. {
  24. ssize_t rc = 0;
  25. mutex_lock(&test_fw_mutex);
  26. if (test_firmware)
  27. rc = simple_read_from_buffer(buf, size, offset,
  28. test_firmware->data,
  29. test_firmware->size);
  30. mutex_unlock(&test_fw_mutex);
  31. return rc;
  32. }
  33. static const struct file_operations test_fw_fops = {
  34. .owner = THIS_MODULE,
  35. .read = test_fw_misc_read,
  36. };
  37. static struct miscdevice test_fw_misc_device = {
  38. .minor = MISC_DYNAMIC_MINOR,
  39. .name = "test_firmware",
  40. .fops = &test_fw_fops,
  41. };
  42. static ssize_t trigger_request_store(struct device *dev,
  43. struct device_attribute *attr,
  44. const char *buf, size_t count)
  45. {
  46. int rc;
  47. char *name;
  48. name = kzalloc(count + 1, GFP_KERNEL);
  49. if (!name)
  50. return -ENOSPC;
  51. memcpy(name, buf, count);
  52. pr_info("loading '%s'\n", name);
  53. mutex_lock(&test_fw_mutex);
  54. release_firmware(test_firmware);
  55. test_firmware = NULL;
  56. rc = request_firmware(&test_firmware, name, dev);
  57. if (rc) {
  58. pr_info("load of '%s' failed: %d\n", name, rc);
  59. goto out;
  60. }
  61. pr_info("loaded: %zu\n", test_firmware->size);
  62. rc = count;
  63. out:
  64. mutex_unlock(&test_fw_mutex);
  65. kfree(name);
  66. return rc;
  67. }
  68. static DEVICE_ATTR_WO(trigger_request);
  69. static int __init test_firmware_init(void)
  70. {
  71. int rc;
  72. rc = misc_register(&test_fw_misc_device);
  73. if (rc) {
  74. pr_err("could not register misc device: %d\n", rc);
  75. return rc;
  76. }
  77. rc = device_create_file(test_fw_misc_device.this_device,
  78. &dev_attr_trigger_request);
  79. if (rc) {
  80. pr_err("could not create sysfs interface: %d\n", rc);
  81. goto dereg;
  82. }
  83. pr_warn("interface ready\n");
  84. return 0;
  85. dereg:
  86. misc_deregister(&test_fw_misc_device);
  87. return rc;
  88. }
  89. module_init(test_firmware_init);
  90. static void __exit test_firmware_exit(void)
  91. {
  92. release_firmware(test_firmware);
  93. device_remove_file(test_fw_misc_device.this_device,
  94. &dev_attr_trigger_request);
  95. misc_deregister(&test_fw_misc_device);
  96. pr_warn("removed interface\n");
  97. }
  98. module_exit(test_firmware_exit);
  99. MODULE_AUTHOR("Kees Cook <keescook@chromium.org>");
  100. MODULE_LICENSE("GPL");