[PATCH 02/21] bbu: make it possible to check multiple of-compatibles

Marco Felsch m.felsch at pengutronix.de
Thu Mar 9 02:40:34 PST 2023


It is possible that one barebox can rule multiple platforms e.g.
multiple platform revision each has a dedicated device tree and of
compatible. While it is possible to add multiple of-compatibles via
IMD_USED_OF() (see: zii-imx8mq-dev) we didn't allowed it to flash it
without 'force'. Fix this by check all possible IMD_TYPE_OF_COMPATIBLE
image metadata types.

Signed-off-by: Marco Felsch <m.felsch at pengutronix.de>
---
 common/bbu.c | 42 +++++++++++++++++++++++++++---------------
 1 file changed, 27 insertions(+), 15 deletions(-)

diff --git a/common/bbu.c b/common/bbu.c
index 3ec17216cb..a9b3fef7a0 100644
--- a/common/bbu.c
+++ b/common/bbu.c
@@ -154,36 +154,44 @@ struct bbu_handler *bbu_find_handler_by_device(const char *devicepath)
 	return NULL;
 }
 
-static int bbu_check_of_compat(struct bbu_data *data)
+static int bbu_check_of_compat(struct bbu_data *data, unsigned short of_compat_nr)
 {
+	const struct imd_header *imd = data->imd_data;
+	const struct imd_header *of_compat;
 	struct device_node *root_node;
 	const char *machine, *str;
 	int ret;
-	const struct imd_header *of_compat;
 
 	if (!IS_ENABLED(CONFIG_OFDEVICE) || !IS_ENABLED(CONFIG_IMD))
 		return 0;
 
-	of_compat = imd_find_type(data->imd_data, IMD_TYPE_OF_COMPATIBLE);
-	if (!of_compat)
-		return 0;
-
 	root_node = of_get_root_node();
 	if (!root_node)
 		return 0;
 
-	str = imd_string_data(of_compat, 0);
-
-	if (of_machine_is_compatible(str)) {
-		pr_info("Devicetree compatible \"%s\" matches current machine\n", str);
-		return 0;
-	}
-
 	ret = of_property_read_string(root_node, "compatible", &machine);
 	if (ret)
 		return 0;
 
-	if (!bbu_force(data, "machine is incompatible with \"%s\", have \"%s\"\n", str, machine))
+	for (; of_compat_nr; of_compat_nr--) {
+		of_compat = imd_find_type(imd, IMD_TYPE_OF_COMPATIBLE);
+		if (!of_compat)
+			return 0;
+
+		str = imd_string_data(of_compat, 0);
+
+		if (of_machine_is_compatible(str)) {
+			pr_info("Devicetree compatible \"%s\" matches current machine\n", str);
+			return 0;
+		}
+
+		pr_debug("machine is incompatible with \"%s\", have \"%s\"\n",
+			 str, machine);
+
+		imd = of_compat;
+	}
+
+	if (!bbu_force(data, "incompatible machine \"%s\"\n", machine))
 		return -EINVAL;
 
 	return 0;
@@ -191,6 +199,7 @@ static int bbu_check_of_compat(struct bbu_data *data)
 
 static int bbu_check_metadata(struct bbu_data *data)
 {
+	unsigned short imd_of_compat_nr = 0;
 	const struct imd_header *imd;
 	int ret;
 	char *str;
@@ -211,6 +220,9 @@ static int bbu_check_metadata(struct bbu_data *data)
 	imd_for_each(data->imd_data, imd) {
 		uint32_t type = imd_read_type(imd);
 
+		if (imd_read_type(imd) == IMD_TYPE_OF_COMPATIBLE)
+			imd_of_compat_nr++;
+
 		if (!imd_is_string(type))
 			continue;
 
@@ -220,7 +232,7 @@ static int bbu_check_metadata(struct bbu_data *data)
 		free(str);
 	}
 
-	ret = bbu_check_of_compat(data);
+	ret = bbu_check_of_compat(data, imd_of_compat_nr);
 	if (ret)
 		return ret;
 

-- 
2.30.2




More information about the barebox mailing list