[PATCH] of: fdt: justify fdt_machine_is_compatible parsing only the root node

Ahmad Fatoum a.fatoum at pengutronix.de
Wed Mar 13 02:31:45 PDT 2024


fdt_machine_is_compatible() is a lightweight way to check the top-level
compatible without unflattening the DT or pulling in libfdt.

The lightness is due to parsing only the root node and looking for the
compatible right away. This assumes that no child nodes of the root node
precede the compatible property. This is mandated by the specification
and dtc throws a syntax error when attempting to violate this.

Add a comment referencing the specification to tell future readers why
it should be ok for us to assume this.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 drivers/of/fdt.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c
index 2f3b0763999d..8dca41990c87 100644
--- a/drivers/of/fdt.c
+++ b/drivers/of/fdt.c
@@ -730,6 +730,17 @@ int fdt_machine_is_compatible(const struct fdt_header *fdt, size_t fdt_size, con
 			dt_struct = dt_struct_advance(&f, dt_struct,
 					sizeof(struct fdt_node_header) + 1);
 
+			/*
+			 * Quoting Device Tree Specification v0.4 §5.4.2:
+			 *
+			 *   [T]his process requires that all property definitions for
+			 *   a particular node precede any subnode definitions for that
+			 *   node. Although the structure would not be ambiguous if
+			 *   properties and subnodes were intermingled, the code needed
+			 *   to process a flat tree is simplified by this requirement.
+			 *
+			 * So let's make use of this simplification.
+			 */
 			expect = FDT_PROP;
 			break;
 
-- 
2.39.2




More information about the barebox mailing list