test_firmware.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. pr_info("loaded: %zu\n", test_firmware ? test_firmware->size : 0);
  60. mutex_unlock(&test_fw_mutex);
  61. kfree(name);
  62. return count;
  63. }
  64. static DEVICE_ATTR_WO(trigger_request);
  65. static int __init test_firmware_init(void)
  66. {
  67. int rc;
  68. rc = misc_register(&test_fw_misc_device);
  69. if (rc) {
  70. pr_err("could not register misc device: %d\n", rc);
  71. return rc;
  72. }
  73. rc = device_create_file(test_fw_misc_device.this_device,
  74. &dev_attr_trigger_request);
  75. if (rc) {
  76. pr_err("could not create sysfs interface: %d\n", rc);
  77. goto dereg;
  78. }
  79. pr_warn("interface ready\n");
  80. return 0;
  81. dereg:
  82. misc_deregister(&test_fw_misc_device);
  83. return rc;
  84. }
  85. module_init(test_firmware_init);
  86. static void __exit test_firmware_exit(void)
  87. {
  88. release_firmware(test_firmware);
  89. device_remove_file(test_fw_misc_device.this_device,
  90. &dev_attr_trigger_request);
  91. misc_deregister(&test_fw_misc_device);
  92. pr_warn("removed interface\n");
  93. }
  94. module_exit(test_firmware_exit);
  95. MODULE_AUTHOR("Kees Cook <keescook@chromium.org>");
  96. MODULE_LICENSE("GPL");