staging: most: configfs: use strlcpy
[linux/fpc-iii.git] / drivers / firmware / efi / rci2-table.c
blob76b0c354a0277ecd1cb3531fe6ca60ef3eed3eb0
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Export Runtime Configuration Interface Table Version 2 (RCI2)
4 * to sysfs
6 * Copyright (C) 2019 Dell Inc
7 * by Narendra K <Narendra.K@dell.com>
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.
15 #include <linux/kobject.h>
16 #include <linux/device.h>
17 #include <linux/sysfs.h>
18 #include <linux/efi.h>
19 #include <linux/types.h>
20 #include <linux/io.h>
22 #define RCI_SIGNATURE "_RC_"
24 struct rci2_table_global_hdr {
25 u16 type;
26 u16 resvd0;
27 u16 hdr_len;
28 u8 rci2_sig[4];
29 u16 resvd1;
30 u32 resvd2;
31 u32 resvd3;
32 u8 major_rev;
33 u8 minor_rev;
34 u16 num_of_structs;
35 u32 rci2_len;
36 u16 rci2_chksum;
37 } __packed;
39 static u8 *rci2_base;
40 static u32 rci2_table_len;
41 unsigned long rci2_table_phys __ro_after_init = EFI_INVALID_TABLE_ADDR;
43 static ssize_t raw_table_read(struct file *file, struct kobject *kobj,
44 struct bin_attribute *attr, char *buf,
45 loff_t pos, size_t count)
47 memcpy(buf, attr->private + pos, count);
48 return count;
51 static BIN_ATTR(rci2, S_IRUSR, raw_table_read, NULL, 0);
53 static u16 checksum(void)
55 u8 len_is_odd = rci2_table_len % 2;
56 u32 chksum_len = rci2_table_len;
57 u16 *base = (u16 *)rci2_base;
58 u8 buf[2] = {0};
59 u32 offset = 0;
60 u16 chksum = 0;
62 if (len_is_odd)
63 chksum_len -= 1;
65 while (offset < chksum_len) {
66 chksum += *base;
67 offset += 2;
68 base++;
71 if (len_is_odd) {
72 buf[0] = *(u8 *)base;
73 chksum += *(u16 *)(buf);
76 return chksum;
79 static int __init efi_rci2_sysfs_init(void)
81 struct kobject *tables_kobj;
82 int ret = -ENOMEM;
84 rci2_base = memremap(rci2_table_phys,
85 sizeof(struct rci2_table_global_hdr),
86 MEMREMAP_WB);
87 if (!rci2_base) {
88 pr_debug("RCI2 table init failed - could not map RCI2 table\n");
89 goto err;
92 if (strncmp(rci2_base +
93 offsetof(struct rci2_table_global_hdr, rci2_sig),
94 RCI_SIGNATURE, 4)) {
95 pr_debug("RCI2 table init failed - incorrect signature\n");
96 ret = -ENODEV;
97 goto err_unmap;
100 rci2_table_len = *(u32 *)(rci2_base +
101 offsetof(struct rci2_table_global_hdr,
102 rci2_len));
104 memunmap(rci2_base);
106 if (!rci2_table_len) {
107 pr_debug("RCI2 table init failed - incorrect table length\n");
108 goto err;
111 rci2_base = memremap(rci2_table_phys, rci2_table_len, MEMREMAP_WB);
112 if (!rci2_base) {
113 pr_debug("RCI2 table - could not map RCI2 table\n");
114 goto err;
117 if (checksum() != 0) {
118 pr_debug("RCI2 table - incorrect checksum\n");
119 ret = -ENODEV;
120 goto err_unmap;
123 tables_kobj = kobject_create_and_add("tables", efi_kobj);
124 if (!tables_kobj) {
125 pr_debug("RCI2 table - tables_kobj creation failed\n");
126 goto err_unmap;
129 bin_attr_rci2.size = rci2_table_len;
130 bin_attr_rci2.private = rci2_base;
131 ret = sysfs_create_bin_file(tables_kobj, &bin_attr_rci2);
132 if (ret != 0) {
133 pr_debug("RCI2 table - rci2 sysfs bin file creation failed\n");
134 kobject_del(tables_kobj);
135 kobject_put(tables_kobj);
136 goto err_unmap;
139 return 0;
141 err_unmap:
142 memunmap(rci2_base);
143 err:
144 pr_debug("RCI2 table - sysfs initialization failed\n");
145 return ret;
147 late_initcall(efi_rci2_sysfs_init);