rci2-table.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 ssize_t raw_table_read(struct file *file, struct kobject *kobj,
  39. struct bin_attribute *attr, char *buf,
  40. loff_t pos, size_t count)
  41. {
  42. memcpy(buf, attr->private + pos, count);
  43. return count;
  44. }
  45. static BIN_ATTR(rci2, S_IRUSR, raw_table_read, NULL, 0);
  46. static u16 checksum(void)
  47. {
  48. u8 len_is_odd = rci2_table_len % 2;
  49. u32 chksum_len = rci2_table_len;
  50. u16 *base = (u16 *)rci2_base;
  51. u8 buf[2] = {0};
  52. u32 offset = 0;
  53. u16 chksum = 0;
  54. if (len_is_odd)
  55. chksum_len -= 1;
  56. while (offset < chksum_len) {
  57. chksum += *base;
  58. offset += 2;
  59. base++;
  60. }
  61. if (len_is_odd) {
  62. buf[0] = *(u8 *)base;
  63. chksum += *(u16 *)(buf);
  64. }
  65. return chksum;
  66. }
  67. static int __init efi_rci2_sysfs_init(void)
  68. {
  69. struct kobject *tables_kobj;
  70. int ret = -ENOMEM;
  71. if (rci2_table_phys == EFI_INVALID_TABLE_ADDR)
  72. return 0;
  73. rci2_base = memremap(rci2_table_phys,
  74. sizeof(struct rci2_table_global_hdr),
  75. MEMREMAP_WB);
  76. if (!rci2_base) {
  77. pr_debug("RCI2 table init failed - could not map RCI2 table\n");
  78. goto err;
  79. }
  80. if (strncmp(rci2_base +
  81. offsetof(struct rci2_table_global_hdr, rci2_sig),
  82. RCI_SIGNATURE, 4)) {
  83. pr_debug("RCI2 table init failed - incorrect signature\n");
  84. ret = -ENODEV;
  85. goto err_unmap;
  86. }
  87. rci2_table_len = *(u32 *)(rci2_base +
  88. offsetof(struct rci2_table_global_hdr,
  89. rci2_len));
  90. memunmap(rci2_base);
  91. if (!rci2_table_len) {
  92. pr_debug("RCI2 table init failed - incorrect table length\n");
  93. goto err;
  94. }
  95. rci2_base = memremap(rci2_table_phys, rci2_table_len, MEMREMAP_WB);
  96. if (!rci2_base) {
  97. pr_debug("RCI2 table - could not map RCI2 table\n");
  98. goto err;
  99. }
  100. if (checksum() != 0) {
  101. pr_debug("RCI2 table - incorrect checksum\n");
  102. ret = -ENODEV;
  103. goto err_unmap;
  104. }
  105. tables_kobj = kobject_create_and_add("tables", efi_kobj);
  106. if (!tables_kobj) {
  107. pr_debug("RCI2 table - tables_kobj creation failed\n");
  108. goto err_unmap;
  109. }
  110. bin_attr_rci2.size = rci2_table_len;
  111. bin_attr_rci2.private = rci2_base;
  112. ret = sysfs_create_bin_file(tables_kobj, &bin_attr_rci2);
  113. if (ret != 0) {
  114. pr_debug("RCI2 table - rci2 sysfs bin file creation failed\n");
  115. kobject_del(tables_kobj);
  116. kobject_put(tables_kobj);
  117. goto err_unmap;
  118. }
  119. return 0;
  120. err_unmap:
  121. memunmap(rci2_base);
  122. err:
  123. pr_debug("RCI2 table - sysfs initialization failed\n");
  124. return ret;
  125. }
  126. late_initcall(efi_rci2_sysfs_init);