[PATCH 1/2] of: create of_root if no dtb provided

From: frowand . list
Date: Thu Jun 23 2022 - 23:44:01 EST


From: Frank Rowand <frank.rowand@xxxxxxxx>

When enabling CONFIG_OF on a platform where of_root is not populated by
firmware, we end up without a root node. In order to apply overlays and
create subnodes of the root node, we need one. Create this root node
by unflattening an empty builtin dtb.

If firmware provides a flattened device tree (FDT) then the FDT is
unflattened via setup_arch(). setup_of() is called immediately
after setup_arch(), and will create the default root node if it
does not exist.

Signed-off-by: Frank Rowand <frank.rowand@xxxxxxxx>
---

This patch modifies init/main.c. It was not clear to me whether
to add Linus to the distribution, or if the change should flow
through Rob. (I did not add Linus.)

drivers/of/Makefile | 2 +-
drivers/of/fdt.c | 19 ++++++++++++++++++-
include/linux/of_fdt.h | 2 ++
init/main.c | 2 ++
4 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/drivers/of/Makefile b/drivers/of/Makefile
index e0360a44306e..cbae92c5ed02 100644
--- a/drivers/of/Makefile
+++ b/drivers/of/Makefile
@@ -2,7 +2,7 @@
obj-y = base.o device.o platform.o property.o
obj-$(CONFIG_OF_KOBJ) += kobj.o
obj-$(CONFIG_OF_DYNAMIC) += dynamic.o
-obj-$(CONFIG_OF_FLATTREE) += fdt.o
+obj-$(CONFIG_OF_FLATTREE) += fdt.o empty_root.dtb.o
obj-$(CONFIG_OF_EARLY_FLATTREE) += fdt_address.o
obj-$(CONFIG_OF_PROMTREE) += pdt.o
obj-$(CONFIG_OF_ADDRESS) += address.o
diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c
index a8f5b6532165..d439ded3b6c6 100644
--- a/drivers/of/fdt.c
+++ b/drivers/of/fdt.c
@@ -33,6 +33,12 @@

#include "of_private.h"

+/*
+ * __dtb_empty_root_begin[] magically created by cmd_dt_S_dtb in
+ * scripts/Makefile.lib
+ */
+extern void *__dtb_empty_root_begin;
+
/*
* of_fdt_limit_memory - limit the number of regions in the /memory node
* @limit: maximum entries
@@ -1332,8 +1338,13 @@ bool __init early_init_dt_scan(void *params)
*/
void __init unflatten_device_tree(void)
{
- __unflatten_device_tree(initial_boot_params, NULL, &of_root,
+ if (!initial_boot_params) {
+ initial_boot_params = (void *) __dtb_empty_root_begin;
+ unflatten_and_copy_device_tree();
+ } else {
+ __unflatten_device_tree(initial_boot_params, NULL, &of_root,
early_init_dt_alloc_memory_arch, false);
+ }

/* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */
of_alias_scan(early_init_dt_alloc_memory_arch);
@@ -1373,6 +1384,12 @@ void __init unflatten_and_copy_device_tree(void)
unflatten_device_tree();
}

+void __init setup_of(void)
+{
+ if (!of_root)
+ unflatten_device_tree();
+}
+
#ifdef CONFIG_SYSFS
static ssize_t of_fdt_raw_read(struct file *filp, struct kobject *kobj,
struct bin_attribute *bin_attr,
diff --git a/include/linux/of_fdt.h b/include/linux/of_fdt.h
index d69ad5bb1eb1..4566876db351 100644
--- a/include/linux/of_fdt.h
+++ b/include/linux/of_fdt.h
@@ -81,6 +81,7 @@ extern const void *of_flat_dt_match_machine(const void *default_match,
/* Other Prototypes */
extern void unflatten_device_tree(void);
extern void unflatten_and_copy_device_tree(void);
+extern void setup_of(void);
extern void early_init_devtree(void *);
extern void early_get_first_memblock_info(void *, phys_addr_t *);
#else /* CONFIG_OF_EARLY_FLATTREE */
@@ -91,6 +92,7 @@ static inline void early_init_fdt_reserve_self(void) {}
static inline const char *of_flat_dt_get_machine_name(void) { return NULL; }
static inline void unflatten_device_tree(void) {}
static inline void unflatten_and_copy_device_tree(void) {}
+static inline void of_setup(void) {}
#endif /* CONFIG_OF_EARLY_FLATTREE */

#endif /* __ASSEMBLY__ */
diff --git a/init/main.c b/init/main.c
index 0ee39cdcfcac..8b3a60d14bea 100644
--- a/init/main.c
+++ b/init/main.c
@@ -99,6 +99,7 @@
#include <linux/kcsan.h>
#include <linux/init_syscalls.h>
#include <linux/stackdepot.h>
+#include <linux/of_fdt.h>
#include <net/net_namespace.h>

#include <asm/io.h>
@@ -949,6 +950,7 @@ asmlinkage __visible void __init __no_sanitize_address start_kernel(void)
pr_notice("%s", linux_banner);
early_security_init();
setup_arch(&command_line);
+ setup_of();
setup_boot_config();
setup_command_line(command_line);
setup_nr_cpu_ids();
--
Frank Rowand <frank.rowand@xxxxxxxx>