acpi_configfs.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ACPI configfs support
  4. *
  5. * Copyright (c) 2016 Intel Corporation
  6. */
  7. #define pr_fmt(fmt) "ACPI configfs: " fmt
  8. #include <linux/init.h>
  9. #include <linux/module.h>
  10. #include <linux/configfs.h>
  11. #include <linux/acpi.h>
  12. #include <linux/security.h>
  13. #include "acpica/accommon.h"
  14. #include "acpica/actables.h"
  15. static struct config_group *acpi_table_group;
  16. struct acpi_table {
  17. struct config_item cfg;
  18. struct acpi_table_header *header;
  19. u32 index;
  20. };
  21. static ssize_t acpi_table_aml_write(struct config_item *cfg,
  22. const void *data, size_t size)
  23. {
  24. const struct acpi_table_header *header = data;
  25. struct acpi_table *table;
  26. int ret = security_locked_down(LOCKDOWN_ACPI_TABLES);
  27. if (ret)
  28. return ret;
  29. table = container_of(cfg, struct acpi_table, cfg);
  30. if (table->header) {
  31. pr_err("table already loaded\n");
  32. return -EBUSY;
  33. }
  34. if (header->length != size) {
  35. pr_err("invalid table length\n");
  36. return -EINVAL;
  37. }
  38. if (memcmp(header->signature, ACPI_SIG_SSDT, 4)) {
  39. pr_err("invalid table signature\n");
  40. return -EINVAL;
  41. }
  42. table = container_of(cfg, struct acpi_table, cfg);
  43. table->header = kmemdup(header, header->length, GFP_KERNEL);
  44. if (!table->header)
  45. return -ENOMEM;
  46. ret = acpi_load_table(table->header, &table->index);
  47. if (ret) {
  48. kfree(table->header);
  49. table->header = NULL;
  50. }
  51. return ret;
  52. }
  53. static inline struct acpi_table_header *get_header(struct config_item *cfg)
  54. {
  55. struct acpi_table *table = container_of(cfg, struct acpi_table, cfg);
  56. if (!table->header)
  57. pr_err("table not loaded\n");
  58. return table->header;
  59. }
  60. static ssize_t acpi_table_aml_read(struct config_item *cfg,
  61. void *data, size_t size)
  62. {
  63. struct acpi_table_header *h = get_header(cfg);
  64. if (!h)
  65. return -EINVAL;
  66. if (data)
  67. memcpy(data, h, h->length);
  68. return h->length;
  69. }
  70. #define MAX_ACPI_TABLE_SIZE (128 * 1024)
  71. CONFIGFS_BIN_ATTR(acpi_table_, aml, NULL, MAX_ACPI_TABLE_SIZE);
  72. static struct configfs_bin_attribute *acpi_table_bin_attrs[] = {
  73. &acpi_table_attr_aml,
  74. NULL,
  75. };
  76. static ssize_t acpi_table_signature_show(struct config_item *cfg, char *str)
  77. {
  78. struct acpi_table_header *h = get_header(cfg);
  79. if (!h)
  80. return -EINVAL;
  81. return sprintf(str, "%.*s\n", ACPI_NAMESEG_SIZE, h->signature);
  82. }
  83. static ssize_t acpi_table_length_show(struct config_item *cfg, char *str)
  84. {
  85. struct acpi_table_header *h = get_header(cfg);
  86. if (!h)
  87. return -EINVAL;
  88. return sprintf(str, "%d\n", h->length);
  89. }
  90. static ssize_t acpi_table_revision_show(struct config_item *cfg, char *str)
  91. {
  92. struct acpi_table_header *h = get_header(cfg);
  93. if (!h)
  94. return -EINVAL;
  95. return sprintf(str, "%d\n", h->revision);
  96. }
  97. static ssize_t acpi_table_oem_id_show(struct config_item *cfg, char *str)
  98. {
  99. struct acpi_table_header *h = get_header(cfg);
  100. if (!h)
  101. return -EINVAL;
  102. return sprintf(str, "%.*s\n", ACPI_OEM_ID_SIZE, h->oem_id);
  103. }
  104. static ssize_t acpi_table_oem_table_id_show(struct config_item *cfg, char *str)
  105. {
  106. struct acpi_table_header *h = get_header(cfg);
  107. if (!h)
  108. return -EINVAL;
  109. return sprintf(str, "%.*s\n", ACPI_OEM_TABLE_ID_SIZE, h->oem_table_id);
  110. }
  111. static ssize_t acpi_table_oem_revision_show(struct config_item *cfg, char *str)
  112. {
  113. struct acpi_table_header *h = get_header(cfg);
  114. if (!h)
  115. return -EINVAL;
  116. return sprintf(str, "%d\n", h->oem_revision);
  117. }
  118. static ssize_t acpi_table_asl_compiler_id_show(struct config_item *cfg,
  119. char *str)
  120. {
  121. struct acpi_table_header *h = get_header(cfg);
  122. if (!h)
  123. return -EINVAL;
  124. return sprintf(str, "%.*s\n", ACPI_NAMESEG_SIZE, h->asl_compiler_id);
  125. }
  126. static ssize_t acpi_table_asl_compiler_revision_show(struct config_item *cfg,
  127. char *str)
  128. {
  129. struct acpi_table_header *h = get_header(cfg);
  130. if (!h)
  131. return -EINVAL;
  132. return sprintf(str, "%d\n", h->asl_compiler_revision);
  133. }
  134. CONFIGFS_ATTR_RO(acpi_table_, signature);
  135. CONFIGFS_ATTR_RO(acpi_table_, length);
  136. CONFIGFS_ATTR_RO(acpi_table_, revision);
  137. CONFIGFS_ATTR_RO(acpi_table_, oem_id);
  138. CONFIGFS_ATTR_RO(acpi_table_, oem_table_id);
  139. CONFIGFS_ATTR_RO(acpi_table_, oem_revision);
  140. CONFIGFS_ATTR_RO(acpi_table_, asl_compiler_id);
  141. CONFIGFS_ATTR_RO(acpi_table_, asl_compiler_revision);
  142. static struct configfs_attribute *acpi_table_attrs[] = {
  143. &acpi_table_attr_signature,
  144. &acpi_table_attr_length,
  145. &acpi_table_attr_revision,
  146. &acpi_table_attr_oem_id,
  147. &acpi_table_attr_oem_table_id,
  148. &acpi_table_attr_oem_revision,
  149. &acpi_table_attr_asl_compiler_id,
  150. &acpi_table_attr_asl_compiler_revision,
  151. NULL,
  152. };
  153. static const struct config_item_type acpi_table_type = {
  154. .ct_owner = THIS_MODULE,
  155. .ct_bin_attrs = acpi_table_bin_attrs,
  156. .ct_attrs = acpi_table_attrs,
  157. };
  158. static struct config_item *acpi_table_make_item(struct config_group *group,
  159. const char *name)
  160. {
  161. struct acpi_table *table;
  162. table = kzalloc(sizeof(*table), GFP_KERNEL);
  163. if (!table)
  164. return ERR_PTR(-ENOMEM);
  165. config_item_init_type_name(&table->cfg, name, &acpi_table_type);
  166. return &table->cfg;
  167. }
  168. static void acpi_table_drop_item(struct config_group *group,
  169. struct config_item *cfg)
  170. {
  171. struct acpi_table *table = container_of(cfg, struct acpi_table, cfg);
  172. ACPI_INFO(("Host-directed Dynamic ACPI Table Unload"));
  173. acpi_unload_table(table->index);
  174. config_item_put(cfg);
  175. }
  176. static struct configfs_group_operations acpi_table_group_ops = {
  177. .make_item = acpi_table_make_item,
  178. .drop_item = acpi_table_drop_item,
  179. };
  180. static const struct config_item_type acpi_tables_type = {
  181. .ct_owner = THIS_MODULE,
  182. .ct_group_ops = &acpi_table_group_ops,
  183. };
  184. static const struct config_item_type acpi_root_group_type = {
  185. .ct_owner = THIS_MODULE,
  186. };
  187. static struct configfs_subsystem acpi_configfs = {
  188. .su_group = {
  189. .cg_item = {
  190. .ci_namebuf = "acpi",
  191. .ci_type = &acpi_root_group_type,
  192. },
  193. },
  194. .su_mutex = __MUTEX_INITIALIZER(acpi_configfs.su_mutex),
  195. };
  196. static int __init acpi_configfs_init(void)
  197. {
  198. int ret;
  199. struct config_group *root = &acpi_configfs.su_group;
  200. config_group_init(root);
  201. ret = configfs_register_subsystem(&acpi_configfs);
  202. if (ret)
  203. return ret;
  204. acpi_table_group = configfs_register_default_group(root, "table",
  205. &acpi_tables_type);
  206. if (IS_ERR(acpi_table_group)) {
  207. configfs_unregister_subsystem(&acpi_configfs);
  208. return PTR_ERR(acpi_table_group);
  209. }
  210. return 0;
  211. }
  212. module_init(acpi_configfs_init);
  213. static void __exit acpi_configfs_exit(void)
  214. {
  215. configfs_unregister_default_group(acpi_table_group);
  216. configfs_unregister_subsystem(&acpi_configfs);
  217. }
  218. module_exit(acpi_configfs_exit);
  219. MODULE_AUTHOR("Octavian Purdila <octavian.purdila@intel.com>");
  220. MODULE_DESCRIPTION("ACPI configfs support");
  221. MODULE_LICENSE("GPL v2");