Commit 08d53aa5 authored by Ard Biesheuvel's avatar Ard Biesheuvel Committed by Grant Likely

of/fdt: export fdt blob as /sys/firmware/fdt

Create a new /sys entry '/sys/firmware/fdt' to export the FDT blob
that was passed to the kernel by the bootloader. This allows userland
applications such as kexec to access the raw binary.

The fact that this node does not reside under /sys/firmware/device-tree
is deliberate: FDT is also used on arm64 UEFI/ACPI systems to
communicate just the UEFI and ACPI entry points, but the FDT is never
unflattened and used to configure the system.

A CRC32 checksum is calculated over the entire FDT blob, and verified
at late_initcall time. The sysfs entry is instantiated only if the
checksum is valid, i.e., if the FDT blob has not been modified in the
mean time. Otherwise, a warning is printed.
Signed-off-by: default avatarArd Biesheuvel <ard.biesheuvel@linaro.org>
Signed-off-by: default avatarGrant Likely <grant.likely@linaro.org>
parent 53a4ab96
...@@ -23,6 +23,7 @@ config OF_FLATTREE ...@@ -23,6 +23,7 @@ config OF_FLATTREE
bool bool
select DTC select DTC
select LIBFDT select LIBFDT
select CRC32
config OF_EARLY_FLATTREE config OF_EARLY_FLATTREE
bool bool
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
* version 2 as published by the Free Software Foundation. * version 2 as published by the Free Software Foundation.
*/ */
#include <linux/crc32.h>
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/initrd.h> #include <linux/initrd.h>
#include <linux/memblock.h> #include <linux/memblock.h>
...@@ -22,6 +23,7 @@ ...@@ -22,6 +23,7 @@
#include <linux/libfdt.h> #include <linux/libfdt.h>
#include <linux/debugfs.h> #include <linux/debugfs.h>
#include <linux/serial_core.h> #include <linux/serial_core.h>
#include <linux/sysfs.h>
#include <asm/setup.h> /* for COMMAND_LINE_SIZE */ #include <asm/setup.h> /* for COMMAND_LINE_SIZE */
#include <asm/page.h> #include <asm/page.h>
...@@ -423,6 +425,8 @@ void *initial_boot_params; ...@@ -423,6 +425,8 @@ void *initial_boot_params;
#ifdef CONFIG_OF_EARLY_FLATTREE #ifdef CONFIG_OF_EARLY_FLATTREE
static u32 of_fdt_crc32;
/** /**
* res_mem_reserve_reg() - reserve all memory described in 'reg' property * res_mem_reserve_reg() - reserve all memory described in 'reg' property
*/ */
...@@ -1003,6 +1007,8 @@ bool __init early_init_dt_verify(void *params) ...@@ -1003,6 +1007,8 @@ bool __init early_init_dt_verify(void *params)
/* Setup flat device-tree pointer */ /* Setup flat device-tree pointer */
initial_boot_params = params; initial_boot_params = params;
of_fdt_crc32 = crc32_be(~0, initial_boot_params,
fdt_totalsize(initial_boot_params));
return true; return true;
} }
...@@ -1080,27 +1086,32 @@ void __init unflatten_and_copy_device_tree(void) ...@@ -1080,27 +1086,32 @@ void __init unflatten_and_copy_device_tree(void)
unflatten_device_tree(); unflatten_device_tree();
} }
#if defined(CONFIG_DEBUG_FS) && defined(DEBUG) #ifdef CONFIG_SYSFS
static struct debugfs_blob_wrapper flat_dt_blob; static ssize_t of_fdt_raw_read(struct file *filp, struct kobject *kobj,
struct bin_attribute *bin_attr,
static int __init of_flat_dt_debugfs_export_fdt(void) char *buf, loff_t off, size_t count)
{ {
struct dentry *d = debugfs_create_dir("device-tree", NULL); memcpy(buf, initial_boot_params + off, count);
return count;
if (!d) }
return -ENOENT;
flat_dt_blob.data = initial_boot_params; static int __init of_fdt_raw_init(void)
flat_dt_blob.size = fdt_totalsize(initial_boot_params); {
static struct bin_attribute of_fdt_raw_attr =
__BIN_ATTR(fdt, S_IRUSR, of_fdt_raw_read, NULL, 0);
d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR, if (!initial_boot_params)
d, &flat_dt_blob); return 0;
if (!d)
return -ENOENT;
return 0; if (of_fdt_crc32 != crc32_be(~0, initial_boot_params,
fdt_totalsize(initial_boot_params))) {
pr_warn("fdt: not creating '/sys/firmware/fdt': CRC check failed\n");
return 0;
}
of_fdt_raw_attr.size = fdt_totalsize(initial_boot_params);
return sysfs_create_bin_file(firmware_kobj, &of_fdt_raw_attr);
} }
module_init(of_flat_dt_debugfs_export_fdt); late_initcall(of_fdt_raw_init);
#endif #endif
#endif /* CONFIG_OF_EARLY_FLATTREE */ #endif /* CONFIG_OF_EARLY_FLATTREE */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment