cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

rci2-table.c (3332B)


      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
     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>
     21
     22#define RCI_SIGNATURE	"_RC_"
     23
     24struct 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;
     38
     39static u8 *rci2_base;
     40static u32 rci2_table_len;
     41unsigned long rci2_table_phys __ro_after_init = EFI_INVALID_TABLE_ADDR;
     42
     43static 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)
     46{
     47	memcpy(buf, attr->private + pos, count);
     48	return count;
     49}
     50
     51static BIN_ATTR(rci2, S_IRUSR, raw_table_read, NULL, 0);
     52
     53static u16 checksum(void)
     54{
     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;
     61
     62	if (len_is_odd)
     63		chksum_len -= 1;
     64
     65	while (offset < chksum_len) {
     66		chksum += *base;
     67		offset += 2;
     68		base++;
     69	}
     70
     71	if (len_is_odd) {
     72		buf[0] = *(u8 *)base;
     73		chksum += *(u16 *)(buf);
     74	}
     75
     76	return chksum;
     77}
     78
     79static int __init efi_rci2_sysfs_init(void)
     80{
     81	struct kobject *tables_kobj;
     82	int ret = -ENOMEM;
     83
     84	if (rci2_table_phys == EFI_INVALID_TABLE_ADDR)
     85		return 0;
     86
     87	rci2_base = memremap(rci2_table_phys,
     88			     sizeof(struct rci2_table_global_hdr),
     89			     MEMREMAP_WB);
     90	if (!rci2_base) {
     91		pr_debug("RCI2 table init failed - could not map RCI2 table\n");
     92		goto err;
     93	}
     94
     95	if (strncmp(rci2_base +
     96		    offsetof(struct rci2_table_global_hdr, rci2_sig),
     97		    RCI_SIGNATURE, 4)) {
     98		pr_debug("RCI2 table init failed - incorrect signature\n");
     99		ret = -ENODEV;
    100		goto err_unmap;
    101	}
    102
    103	rci2_table_len = *(u32 *)(rci2_base +
    104				  offsetof(struct rci2_table_global_hdr,
    105				  rci2_len));
    106
    107	memunmap(rci2_base);
    108
    109	if (!rci2_table_len) {
    110		pr_debug("RCI2 table init failed - incorrect table length\n");
    111		goto err;
    112	}
    113
    114	rci2_base = memremap(rci2_table_phys, rci2_table_len, MEMREMAP_WB);
    115	if (!rci2_base) {
    116		pr_debug("RCI2 table - could not map RCI2 table\n");
    117		goto err;
    118	}
    119
    120	if (checksum() != 0) {
    121		pr_debug("RCI2 table - incorrect checksum\n");
    122		ret = -ENODEV;
    123		goto err_unmap;
    124	}
    125
    126	tables_kobj = kobject_create_and_add("tables", efi_kobj);
    127	if (!tables_kobj) {
    128		pr_debug("RCI2 table - tables_kobj creation failed\n");
    129		goto err_unmap;
    130	}
    131
    132	bin_attr_rci2.size = rci2_table_len;
    133	bin_attr_rci2.private = rci2_base;
    134	ret = sysfs_create_bin_file(tables_kobj, &bin_attr_rci2);
    135	if (ret != 0) {
    136		pr_debug("RCI2 table - rci2 sysfs bin file creation failed\n");
    137		kobject_del(tables_kobj);
    138		kobject_put(tables_kobj);
    139		goto err_unmap;
    140	}
    141
    142	return 0;
    143
    144 err_unmap:
    145	memunmap(rci2_base);
    146 err:
    147	pr_debug("RCI2 table - sysfs initialization failed\n");
    148	return ret;
    149}
    150late_initcall(efi_rci2_sysfs_init);