[RFC PATCH] bootsource: add helper to set instance by name

Michael Riesch michael.riesch at wolfvision.net
Wed Nov 17 06:24:48 PST 2021


Instance numbers should be related to device tree aliases, which may be
board-specific. In order to establish a board-independent link between
the boot source and the actual alias, introduce a helper that sets the
instance by the OF name.

Signed-off-by: Michael Riesch <michael.riesch at wolfvision.net>
---
 common/bootsource.c  | 74 ++++++++++++++++++++++++++++----------------
 include/bootsource.h |  1 +
 2 files changed, 48 insertions(+), 27 deletions(-)

diff --git a/common/bootsource.c b/common/bootsource.c
index 1f8d053a8..1b45956a5 100644
--- a/common/bootsource.c
+++ b/common/bootsource.c
@@ -9,6 +9,7 @@
 #include <environment.h>
 #include <magicvar.h>
 #include <init.h>
+#include <of.h>
 
 static const char *bootsource_str[] = {
 	[BOOTSOURCE_UNKNOWN] = "unknown",
@@ -33,31 +34,8 @@ static enum bootsource bootsource = BOOTSOURCE_UNKNOWN;
 static int bootsource_instance = BOOTSOURCE_INSTANCE_UNKNOWN;
 const char *bootsource_alias_name = NULL;
 
-/**
- * bootsource_get_alias_name() - Get the name of the bootsource alias
- *
- * This function will return newly allocated string containing name of
- * the alias that is expected to point to DTB node corresponding to
- * detected bootsource
- *
- * NOTE: Caller is expected to free() the string allocated by this
- * function
- */
-char *bootsource_get_alias_name(void)
+static const char *bootsource_get_of_stem(void)
 {
-	const char *stem;
-
-	/*
-	 * If alias name was overridden via
-	 * bootsource_set_alias_name() return that value without
-	 * asking any questions.
-	 *
-	 * Note that we have to strdup() the result to make it
-	 * free-able.
-	 */
-	if (bootsource_alias_name)
-		return strdup(bootsource_alias_name);
-
 	switch (bootsource) {
 		/*
 		 * For I2C and SPI EEPROMs we set the stem to be 'i2c'
@@ -69,22 +47,50 @@ char *bootsource_get_alias_name(void)
 		 * controller
 		 */
 	case BOOTSOURCE_I2C_EEPROM:
-		stem = bootsource_str[BOOTSOURCE_I2C];
+		return bootsource_str[BOOTSOURCE_I2C];
 		break;
 	case BOOTSOURCE_SPI_EEPROM:
 	case BOOTSOURCE_SPI_NOR:
-		stem = bootsource_str[BOOTSOURCE_SPI];
+		return bootsource_str[BOOTSOURCE_SPI];
 		break;
 	case BOOTSOURCE_SERIAL:	/* FALLTHROUGH */
 	case BOOTSOURCE_I2C:	/* FALLTHROUGH */
 	case BOOTSOURCE_MMC:	/* FALLTHROUGH */
 	case BOOTSOURCE_SPI:	/* FALLTHROUGH */
 	case BOOTSOURCE_CAN:
-		stem = bootsource_str[bootsource];
+		return bootsource_str[bootsource];
 		break;
 	default:
 		return NULL;
 	}
+}
+
+/**
+ * bootsource_get_alias_name() - Get the name of the bootsource alias
+ *
+ * This function will return newly allocated string containing name of
+ * the alias that is expected to point to DTB node corresponding to
+ * detected bootsource
+ *
+ * NOTE: Caller is expected to free() the string allocated by this
+ * function
+ */
+char *bootsource_get_alias_name(void)
+{
+	const char *stem;
+
+	/*
+	 * If alias name was overridden via
+	 * bootsource_set_alias_name() return that value without
+	 * asking any questions.
+	 *
+	 * Note that we have to strdup() the result to make it
+	 * free-able.
+	 */
+	if (bootsource_alias_name)
+		return strdup(bootsource_alias_name);
+
+	stem = bootsource_get_of_stem();
 
 	/*
 	 * We expect SoC specific bootsource detection code to properly
@@ -125,6 +131,20 @@ void bootsource_set_instance(int instance)
 	setenv("bootsource_instance", buf);
 }
 
+void bootsource_set_instance_by_of_name(const char *name)
+{
+	int instance = BOOTSOURCE_UNKNOWN;
+	struct device_node *node;
+
+	node = of_find_node_by_name(of_get_root_node(), name);
+	if (node) {
+		instance = of_alias_get_id(node, bootsource_get_of_stem());
+		if (instance < 0)
+			instance = BOOTSOURCE_UNKNOWN;
+	}
+	bootsource_set_instance(instance);
+}
+
 enum bootsource bootsource_get(void)
 {
 	return bootsource;
diff --git a/include/bootsource.h b/include/bootsource.h
index 646b0e91c..4dcb969ac 100644
--- a/include/bootsource.h
+++ b/include/bootsource.h
@@ -28,6 +28,7 @@ enum bootsource bootsource_get(void);
 int bootsource_get_instance(void);
 void bootsource_set(enum bootsource src);
 void bootsource_set_instance(int instance);
+void bootsource_set_instance_by_of_name(const char *name);
 void bootsource_set_alias_name(const char *name);
 char *bootsource_get_alias_name(void);
 
-- 
2.30.2




More information about the barebox mailing list