18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Export Runtime Configuration Interface Table Version 2 (RCI2) 48c2ecf20Sopenharmony_ci * to sysfs 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * Copyright (C) 2019 Dell Inc 78c2ecf20Sopenharmony_ci * by Narendra K <Narendra.K@dell.com> 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * System firmware advertises the address of the RCI2 Table via 108c2ecf20Sopenharmony_ci * an EFI Configuration Table entry. This code retrieves the RCI2 118c2ecf20Sopenharmony_ci * table from the address and exports it to sysfs as a binary 128c2ecf20Sopenharmony_ci * attribute 'rci2' under /sys/firmware/efi/tables directory. 138c2ecf20Sopenharmony_ci */ 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci#include <linux/kobject.h> 168c2ecf20Sopenharmony_ci#include <linux/device.h> 178c2ecf20Sopenharmony_ci#include <linux/sysfs.h> 188c2ecf20Sopenharmony_ci#include <linux/efi.h> 198c2ecf20Sopenharmony_ci#include <linux/types.h> 208c2ecf20Sopenharmony_ci#include <linux/io.h> 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci#define RCI_SIGNATURE "_RC_" 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_cistruct rci2_table_global_hdr { 258c2ecf20Sopenharmony_ci u16 type; 268c2ecf20Sopenharmony_ci u16 resvd0; 278c2ecf20Sopenharmony_ci u16 hdr_len; 288c2ecf20Sopenharmony_ci u8 rci2_sig[4]; 298c2ecf20Sopenharmony_ci u16 resvd1; 308c2ecf20Sopenharmony_ci u32 resvd2; 318c2ecf20Sopenharmony_ci u32 resvd3; 328c2ecf20Sopenharmony_ci u8 major_rev; 338c2ecf20Sopenharmony_ci u8 minor_rev; 348c2ecf20Sopenharmony_ci u16 num_of_structs; 358c2ecf20Sopenharmony_ci u32 rci2_len; 368c2ecf20Sopenharmony_ci u16 rci2_chksum; 378c2ecf20Sopenharmony_ci} __packed; 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_cistatic u8 *rci2_base; 408c2ecf20Sopenharmony_cistatic u32 rci2_table_len; 418c2ecf20Sopenharmony_ciunsigned long rci2_table_phys __ro_after_init = EFI_INVALID_TABLE_ADDR; 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_cistatic ssize_t raw_table_read(struct file *file, struct kobject *kobj, 448c2ecf20Sopenharmony_ci struct bin_attribute *attr, char *buf, 458c2ecf20Sopenharmony_ci loff_t pos, size_t count) 468c2ecf20Sopenharmony_ci{ 478c2ecf20Sopenharmony_ci memcpy(buf, attr->private + pos, count); 488c2ecf20Sopenharmony_ci return count; 498c2ecf20Sopenharmony_ci} 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_cistatic BIN_ATTR(rci2, S_IRUSR, raw_table_read, NULL, 0); 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_cistatic u16 checksum(void) 548c2ecf20Sopenharmony_ci{ 558c2ecf20Sopenharmony_ci u8 len_is_odd = rci2_table_len % 2; 568c2ecf20Sopenharmony_ci u32 chksum_len = rci2_table_len; 578c2ecf20Sopenharmony_ci u16 *base = (u16 *)rci2_base; 588c2ecf20Sopenharmony_ci u8 buf[2] = {0}; 598c2ecf20Sopenharmony_ci u32 offset = 0; 608c2ecf20Sopenharmony_ci u16 chksum = 0; 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci if (len_is_odd) 638c2ecf20Sopenharmony_ci chksum_len -= 1; 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci while (offset < chksum_len) { 668c2ecf20Sopenharmony_ci chksum += *base; 678c2ecf20Sopenharmony_ci offset += 2; 688c2ecf20Sopenharmony_ci base++; 698c2ecf20Sopenharmony_ci } 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci if (len_is_odd) { 728c2ecf20Sopenharmony_ci buf[0] = *(u8 *)base; 738c2ecf20Sopenharmony_ci chksum += *(u16 *)(buf); 748c2ecf20Sopenharmony_ci } 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci return chksum; 778c2ecf20Sopenharmony_ci} 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_cistatic int __init efi_rci2_sysfs_init(void) 808c2ecf20Sopenharmony_ci{ 818c2ecf20Sopenharmony_ci struct kobject *tables_kobj; 828c2ecf20Sopenharmony_ci int ret = -ENOMEM; 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci if (rci2_table_phys == EFI_INVALID_TABLE_ADDR) 858c2ecf20Sopenharmony_ci return 0; 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci rci2_base = memremap(rci2_table_phys, 888c2ecf20Sopenharmony_ci sizeof(struct rci2_table_global_hdr), 898c2ecf20Sopenharmony_ci MEMREMAP_WB); 908c2ecf20Sopenharmony_ci if (!rci2_base) { 918c2ecf20Sopenharmony_ci pr_debug("RCI2 table init failed - could not map RCI2 table\n"); 928c2ecf20Sopenharmony_ci goto err; 938c2ecf20Sopenharmony_ci } 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci if (strncmp(rci2_base + 968c2ecf20Sopenharmony_ci offsetof(struct rci2_table_global_hdr, rci2_sig), 978c2ecf20Sopenharmony_ci RCI_SIGNATURE, 4)) { 988c2ecf20Sopenharmony_ci pr_debug("RCI2 table init failed - incorrect signature\n"); 998c2ecf20Sopenharmony_ci ret = -ENODEV; 1008c2ecf20Sopenharmony_ci goto err_unmap; 1018c2ecf20Sopenharmony_ci } 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci rci2_table_len = *(u32 *)(rci2_base + 1048c2ecf20Sopenharmony_ci offsetof(struct rci2_table_global_hdr, 1058c2ecf20Sopenharmony_ci rci2_len)); 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci memunmap(rci2_base); 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci if (!rci2_table_len) { 1108c2ecf20Sopenharmony_ci pr_debug("RCI2 table init failed - incorrect table length\n"); 1118c2ecf20Sopenharmony_ci goto err; 1128c2ecf20Sopenharmony_ci } 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci rci2_base = memremap(rci2_table_phys, rci2_table_len, MEMREMAP_WB); 1158c2ecf20Sopenharmony_ci if (!rci2_base) { 1168c2ecf20Sopenharmony_ci pr_debug("RCI2 table - could not map RCI2 table\n"); 1178c2ecf20Sopenharmony_ci goto err; 1188c2ecf20Sopenharmony_ci } 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci if (checksum() != 0) { 1218c2ecf20Sopenharmony_ci pr_debug("RCI2 table - incorrect checksum\n"); 1228c2ecf20Sopenharmony_ci ret = -ENODEV; 1238c2ecf20Sopenharmony_ci goto err_unmap; 1248c2ecf20Sopenharmony_ci } 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci tables_kobj = kobject_create_and_add("tables", efi_kobj); 1278c2ecf20Sopenharmony_ci if (!tables_kobj) { 1288c2ecf20Sopenharmony_ci pr_debug("RCI2 table - tables_kobj creation failed\n"); 1298c2ecf20Sopenharmony_ci goto err_unmap; 1308c2ecf20Sopenharmony_ci } 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci bin_attr_rci2.size = rci2_table_len; 1338c2ecf20Sopenharmony_ci bin_attr_rci2.private = rci2_base; 1348c2ecf20Sopenharmony_ci ret = sysfs_create_bin_file(tables_kobj, &bin_attr_rci2); 1358c2ecf20Sopenharmony_ci if (ret != 0) { 1368c2ecf20Sopenharmony_ci pr_debug("RCI2 table - rci2 sysfs bin file creation failed\n"); 1378c2ecf20Sopenharmony_ci kobject_del(tables_kobj); 1388c2ecf20Sopenharmony_ci kobject_put(tables_kobj); 1398c2ecf20Sopenharmony_ci goto err_unmap; 1408c2ecf20Sopenharmony_ci } 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ci return 0; 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci err_unmap: 1458c2ecf20Sopenharmony_ci memunmap(rci2_base); 1468c2ecf20Sopenharmony_ci err: 1478c2ecf20Sopenharmony_ci pr_debug("RCI2 table - sysfs initialization failed\n"); 1488c2ecf20Sopenharmony_ci return ret; 1498c2ecf20Sopenharmony_ci} 1508c2ecf20Sopenharmony_cilate_initcall(efi_rci2_sysfs_init); 151