test_firmware.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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/completion.h>
  14. #include <linux/firmware.h>
  15. #include <linux/device.h>
  16. #include <linux/fs.h>
  17. #include <linux/miscdevice.h>
  18. #include <linux/slab.h>
  19. #include <linux/uaccess.h>
  20. static DEFINE_MUTEX(test_fw_mutex);
  21. static const struct firmware *test_firmware;
  22. static ssize_t test_fw_misc_read(struct file *f, char __user *buf,
  23. size_t size, loff_t *offset)
  24. {
  25. ssize_t rc = 0;
  26. mutex_lock(&test_fw_mutex);
  27. if (test_firmware)
  28. rc = simple_read_from_buffer(buf, size, offset,
  29. test_firmware->data,
  30. test_firmware->size);
  31. mutex_unlock(&test_fw_mutex);
  32. return rc;
  33. }
  34. static const struct file_operations test_fw_fops = {
  35. .owner = THIS_MODULE,
  36. .read = test_fw_misc_read,
  37. };
  38. static struct miscdevice test_fw_misc_device = {
  39. .minor = MISC_DYNAMIC_MINOR,
  40. .name = "test_firmware",
  41. .fops = &test_fw_fops,
  42. };
  43. static ssize_t trigger_request_store(struct device *dev,
  44. struct device_attribute *attr,
  45. const char *buf, size_t count)
  46. {
  47. int rc;
  48. char *name;
  49. name = kstrndup(buf, count, GFP_KERNEL);
  50. if (!name)
  51. return -ENOSPC;
  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 DECLARE_COMPLETION(async_fw_done);
  70. static void trigger_async_request_cb(const struct firmware *fw, void *context)
  71. {
  72. test_firmware = fw;
  73. complete(&async_fw_done);
  74. }
  75. static ssize_t trigger_async_request_store(struct device *dev,
  76. struct device_attribute *attr,
  77. const char *buf, size_t count)
  78. {
  79. int rc;
  80. char *name;
  81. name = kstrndup(buf, count, GFP_KERNEL);
  82. if (!name)
  83. return -ENOSPC;
  84. pr_info("loading '%s'\n", name);
  85. mutex_lock(&test_fw_mutex);
  86. release_firmware(test_firmware);
  87. test_firmware = NULL;
  88. rc = request_firmware_nowait(THIS_MODULE, 1, name, dev, GFP_KERNEL,
  89. NULL, trigger_async_request_cb);
  90. if (rc) {
  91. pr_info("async load of '%s' failed: %d\n", name, rc);
  92. kfree(name);
  93. goto out;
  94. }
  95. /* Free 'name' ASAP, to test for race conditions */
  96. kfree(name);
  97. wait_for_completion(&async_fw_done);
  98. if (test_firmware) {
  99. pr_info("loaded: %zu\n", test_firmware->size);
  100. rc = count;
  101. } else {
  102. pr_err("failed to async load firmware\n");
  103. rc = -ENODEV;
  104. }
  105. out:
  106. mutex_unlock(&test_fw_mutex);
  107. return rc;
  108. }
  109. static DEVICE_ATTR_WO(trigger_async_request);
  110. static int __init test_firmware_init(void)
  111. {
  112. int rc;
  113. rc = misc_register(&test_fw_misc_device);
  114. if (rc) {
  115. pr_err("could not register misc device: %d\n", rc);
  116. return rc;
  117. }
  118. rc = device_create_file(test_fw_misc_device.this_device,
  119. &dev_attr_trigger_request);
  120. if (rc) {
  121. pr_err("could not create sysfs interface: %d\n", rc);
  122. goto dereg;
  123. }
  124. rc = device_create_file(test_fw_misc_device.this_device,
  125. &dev_attr_trigger_async_request);
  126. if (rc) {
  127. pr_err("could not create async sysfs interface: %d\n", rc);
  128. goto remove_file;
  129. }
  130. pr_warn("interface ready\n");
  131. return 0;
  132. remove_file:
  133. device_remove_file(test_fw_misc_device.this_device,
  134. &dev_attr_trigger_async_request);
  135. dereg:
  136. misc_deregister(&test_fw_misc_device);
  137. return rc;
  138. }
  139. module_init(test_firmware_init);
  140. static void __exit test_firmware_exit(void)
  141. {
  142. release_firmware(test_firmware);
  143. device_remove_file(test_fw_misc_device.this_device,
  144. &dev_attr_trigger_async_request);
  145. device_remove_file(test_fw_misc_device.this_device,
  146. &dev_attr_trigger_request);
  147. misc_deregister(&test_fw_misc_device);
  148. pr_warn("removed interface\n");
  149. }
  150. module_exit(test_firmware_exit);
  151. MODULE_AUTHOR("Kees Cook <keescook@chromium.org>");
  152. MODULE_LICENSE("GPL");