[PATCH v6 7/7] soc/tegra: Expose Boot Configuration Table via sysfs

From: Dmitry Osipenko
Date: Sat May 16 2020 - 22:13:39 EST


It's quite useful to have unencrypted BCT exposed to userspace for
debugging purposes, so let's expose it via sysfs. The BCT data will be
present in '/sys/tegra/boot_config_table' binary file if BCT is available.

Suggested-by: MichaÅ MirosÅaw <mirq-linux@xxxxxxxxxxxx>
Signed-off-by: Dmitry Osipenko <digetx@xxxxxxxxx>
---
arch/arm/mach-tegra/tegra.c | 4 +++
drivers/soc/tegra/Makefile | 1 +
drivers/soc/tegra/bootdata.c | 51 ++++++++++++++++++++++++++++++++++++
drivers/soc/tegra/common.c | 17 ++++++++++++
include/soc/tegra/bootdata.h | 2 ++
include/soc/tegra/common.h | 3 +++
6 files changed, 78 insertions(+)
create mode 100644 drivers/soc/tegra/bootdata.c

diff --git a/arch/arm/mach-tegra/tegra.c b/arch/arm/mach-tegra/tegra.c
index da6bcd85398b..5f40463f1b97 100644
--- a/arch/arm/mach-tegra/tegra.c
+++ b/arch/arm/mach-tegra/tegra.c
@@ -72,6 +72,7 @@ static void __init tegra_boot_config_table_init(void)
u32 iram_end = TEGRA_IRAM_BASE + TEGRA_IRAM_SIZE;
u32 iram_start = TEGRA_IRAM_BASE;
u32 pt_addr, pt_size, bct_size;
+ void __iomem *bct_ptr;

t20_bit = IO_ADDRESS(TEGRA_IRAM_BASE);

@@ -90,6 +91,7 @@ static void __init tegra_boot_config_table_init(void)

pt_addr = t20_bct->partition_table_logical_sector_address;
pt_size = t20_bct->partition_table_num_logical_sectors;
+ bct_ptr = t20_bct;

} else if (of_machine_is_compatible("nvidia,tegra30")) {
bct_size = sizeof(*t30_bct);
@@ -106,12 +108,14 @@ static void __init tegra_boot_config_table_init(void)

pt_addr = t30_bct->partition_table_logical_sector_address;
pt_size = t30_bct->partition_table_num_logical_sectors;
+ bct_ptr = t30_bct;
} else {
return;
}

pr_info("%s: BCT found in IRAM\n", __func__);

+ tegra_bootdata_bct_setup(bct_ptr, bct_size);
tegra_partition_table_setup(pt_addr, pt_size);
}

diff --git a/drivers/soc/tegra/Makefile b/drivers/soc/tegra/Makefile
index 9c809c1814bd..8be2bfb4d95d 100644
--- a/drivers/soc/tegra/Makefile
+++ b/drivers/soc/tegra/Makefile
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: GPL-2.0
obj-y += fuse/

+obj-y += bootdata.o
obj-y += common.o
obj-$(CONFIG_SOC_TEGRA_FLOWCTRL) += flowctrl.o
obj-$(CONFIG_SOC_TEGRA_PMC) += pmc.o
diff --git a/drivers/soc/tegra/bootdata.c b/drivers/soc/tegra/bootdata.c
new file mode 100644
index 000000000000..3d028e0d343d
--- /dev/null
+++ b/drivers/soc/tegra/bootdata.c
@@ -0,0 +1,51 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/init.h>
+#include <linux/io.h>
+#include <linux/sizes.h>
+#include <linux/slab.h>
+#include <linux/sysfs.h>
+#include <linux/types.h>
+
+#include <soc/tegra/bootdata.h>
+#include <soc/tegra/common.h>
+
+/*
+ * spare_bct[] will be released once kernel is booted, hence not wasting
+ * kernel space if BCT is missing. The tegra_bct can't be allocated during
+ * of BCT setting up because it's too early for the slab allocator.
+ */
+static u8 spare_bct[SZ_8K] __initdata;
+static u8 *tegra_bct;
+
+static ssize_t boot_config_table_read(struct file *filp,
+ struct kobject *kobj,
+ struct bin_attribute *bin_attr,
+ char *buf, loff_t off, size_t count)
+{
+ memcpy(buf, tegra_bct + off, count);
+ return count;
+}
+static BIN_ATTR_RO(boot_config_table, 0);
+
+static int __init tegra_bootdata_bct_sysfs_init(void)
+{
+ if (!bin_attr_boot_config_table.size)
+ return 0;
+
+ tegra_bct = kmalloc(bin_attr_boot_config_table.size, GFP_KERNEL);
+ if (!tegra_bct)
+ return -ENOMEM;
+
+ memcpy(tegra_bct, spare_bct, bin_attr_boot_config_table.size);
+
+ return sysfs_create_bin_file(tegra_soc_kobj,
+ &bin_attr_boot_config_table);
+}
+late_initcall(tegra_bootdata_bct_sysfs_init)
+
+void __init tegra_bootdata_bct_setup(void __iomem *bct_ptr, size_t bct_size)
+{
+ memcpy_fromio(spare_bct, bct_ptr, bct_size);
+ bin_attr_boot_config_table.size = bct_size;
+}
diff --git a/drivers/soc/tegra/common.c b/drivers/soc/tegra/common.c
index 3dc54f59cafe..2b4b49eacb2e 100644
--- a/drivers/soc/tegra/common.c
+++ b/drivers/soc/tegra/common.c
@@ -3,10 +3,15 @@
* Copyright (C) 2014 NVIDIA CORPORATION. All rights reserved.
*/

+#include <linux/init.h>
+#include <linux/kernel.h>
#include <linux/of.h>
+#include <linux/sysfs.h>

#include <soc/tegra/common.h>

+struct kobject *tegra_soc_kobj;
+
static const struct of_device_id tegra_machine_match[] = {
{ .compatible = "nvidia,tegra20", },
{ .compatible = "nvidia,tegra30", },
@@ -31,3 +36,15 @@ bool soc_is_tegra(void)

return match != NULL;
}
+
+static int __init tegra_soc_sysfs_init(void)
+{
+ if (!soc_is_tegra())
+ return 0;
+
+ tegra_soc_kobj = kobject_create_and_add("tegra", NULL);
+ WARN_ON(!tegra_soc_kobj);
+
+ return 0;
+}
+arch_initcall(tegra_soc_sysfs_init)
diff --git a/include/soc/tegra/bootdata.h b/include/soc/tegra/bootdata.h
index 7be207cb2519..d5c7a251517d 100644
--- a/include/soc/tegra/bootdata.h
+++ b/include/soc/tegra/bootdata.h
@@ -43,4 +43,6 @@ struct tegra30_boot_config_table {
u32 unused_data[3];
} __packed;

+void tegra_bootdata_bct_setup(void __iomem *bct_ptr, size_t bct_size);
+
#endif /* __SOC_TEGRA_BOOTDATA_H__ */
diff --git a/include/soc/tegra/common.h b/include/soc/tegra/common.h
index 744280ecab5f..0bc11b45c98e 100644
--- a/include/soc/tegra/common.h
+++ b/include/soc/tegra/common.h
@@ -7,8 +7,11 @@
#define __SOC_TEGRA_COMMON_H__

#include <linux/types.h>
+#include <linux/sysfs.h>

#ifdef CONFIG_ARCH_TEGRA
+extern struct kobject *tegra_soc_kobj;
+
bool soc_is_tegra(void);
#else
static inline bool soc_is_tegra(void)
--
2.26.0