rci2-table.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Export Runtime Configuration Interface Table Version 2 (RCI2)
  4. * to sysfs
  5. *
  6. * Copyright (C) 2019 Dell Inc
  7. * by Narendra K <Narendra.K@dell.com>
  8. *
  9. * System firmware advertises the address of the RCI2 Table via
  10. * an EFI Configuration Table entry. This code retrieves the RCI2
  11. * table from the address and exports it to sysfs as a binary
  12. * attribute 'rci2' under /sys/firmware/efi/tables directory.
  13. */
  14. #include <linux/kobject.h>
  15. #include <linux/device.h>
  16. #include <linux/sysfs.h>
  17. #include <linux/efi.h>
  18. #include <linux/types.h>
  19. #include <linux/io.h>
  20. #define RCI_SIGNATURE "_RC_"
  21. struct rci2_table_global_hdr {
  22. u16 type;
  23. u16 resvd0;
  24. u16 hdr_len;
  25. u8 rci2_sig[4];
  26. u16 resvd1;
  27. u32 resvd2;
  28. u32 resvd3;
  29. u8 major_rev;
  30. u8 minor_rev;
  31. u16 num_of_structs;
  32. u32 rci2_len;
  33. u16 rci2_chksum;
  34. } __packed;
  35. static u8 *rci2_base;
  36. static u32 rci2_table_len;
  37. unsigned long rci2_table_phys __ro_after_init = EFI_INVALID_TABLE_ADDR;
  38. static __ro_after_init BIN_ATTR_SIMPLE_ADMIN_RO(rci2);
  39. static u16 checksum(void)
  40. {
  41. u8 len_is_odd = rci2_table_len % 2;
  42. u32 chksum_len = rci2_table_len;
  43. u16 *base = (u16 *)rci2_base;
  44. u8 buf[2] = {0};
  45. u32 offset = 0;
  46. u16 chksum = 0;
  47. if (len_is_odd)
  48. chksum_len -= 1;
  49. while (offset < chksum_len) {
  50. chksum += *base;
  51. offset += 2;
  52. base++;
  53. }
  54. if (len_is_odd) {
  55. buf[0] = *(u8 *)base;
  56. chksum += *(u16 *)(buf);
  57. }
  58. return chksum;
  59. }
  60. static int __init efi_rci2_sysfs_init(void)
  61. {
  62. struct kobject *tables_kobj;
  63. int ret = -ENOMEM;
  64. if (rci2_table_phys == EFI_INVALID_TABLE_ADDR)
  65. return 0;
  66. rci2_base = memremap(rci2_table_phys,
  67. sizeof(struct rci2_table_global_hdr),
  68. MEMREMAP_WB);
  69. if (!rci2_base) {
  70. pr_debug("RCI2 table init failed - could not map RCI2 table\n");
  71. goto err;
  72. }
  73. if (strncmp(rci2_base +
  74. offsetof(struct rci2_table_global_hdr, rci2_sig),
  75. RCI_SIGNATURE, 4)) {
  76. pr_debug("RCI2 table init failed - incorrect signature\n");
  77. ret = -ENODEV;
  78. goto err_unmap;
  79. }
  80. rci2_table_len = *(u32 *)(rci2_base +
  81. offsetof(struct rci2_table_global_hdr,
  82. rci2_len));
  83. memunmap(rci2_base);
  84. if (!rci2_table_len) {
  85. pr_debug("RCI2 table init failed - incorrect table length\n");
  86. goto err;
  87. }
  88. rci2_base = memremap(rci2_table_phys, rci2_table_len, MEMREMAP_WB);
  89. if (!rci2_base) {
  90. pr_debug("RCI2 table - could not map RCI2 table\n");
  91. goto err;
  92. }
  93. if (checksum() != 0) {
  94. pr_debug("RCI2 table - incorrect checksum\n");
  95. ret = -ENODEV;
  96. goto err_unmap;
  97. }
  98. tables_kobj = kobject_create_and_add("tables", efi_kobj);
  99. if (!tables_kobj) {
  100. pr_debug("RCI2 table - tables_kobj creation failed\n");
  101. goto err_unmap;
  102. }
  103. bin_attr_rci2.size = rci2_table_len;
  104. bin_attr_rci2.private = rci2_base;
  105. ret = sysfs_create_bin_file(tables_kobj, &bin_attr_rci2);
  106. if (ret != 0) {
  107. pr_debug("RCI2 table - rci2 sysfs bin file creation failed\n");
  108. kobject_del(tables_kobj);
  109. kobject_put(tables_kobj);
  110. goto err_unmap;
  111. }
  112. return 0;
  113. err_unmap:
  114. memunmap(rci2_base);
  115. err:
  116. pr_debug("RCI2 table - sysfs initialization failed\n");
  117. return ret;
  118. }
  119. late_initcall(efi_rci2_sysfs_init);