Skip to contentSkip to conversation

libfdt: Add function to explicitly expand aliases

PATCH New David Gibson 3 participants 3 messages Aug 20, 2008 – Oct 4, 2008

lore
Share & tools
lore
#0 lore raw

Kumar has already added alias expansion to fdt_path_offset(). However, in some circumstances it may be convenient for the user of libfdt to explicitly get the string expansion of an alias. This patch adds a function to do this, fdt_get_alias(), and uses it to implement fdt_path_offset().

libfdt/fdt_ro.c +19 -7
Index: dtc/libfdt/fdt_ro.c
===================================================================
@@ -141,17 +141,12 @@ int fdt_path_offset(const void *fdt, con
 
 	/* see if we have an alias */
 	if (*path != '/') {
-		const char *q;
-		int aliasoffset = fdt_path_offset(fdt, "/aliases");
-
-		if (aliasoffset < 0)
-			return -FDT_ERR_BADPATH;
+		const char *q = strchr(path, '/');
 
-		q = strchr(path, '/');
 		if (!q)
 			q = end;
 
-		p = fdt_getprop_namelen(fdt, aliasoffset, path, q - p, NULL);
+		p = fdt_get_alias_namelen(fdt, p, q - p);
 		if (!p)
 			return -FDT_ERR_BADPATH;
 		offset = fdt_path_offset(fdt, p);
@@ -302,6 +297,23 @@ uint32_t fdt_get_phandle(const void *fdt
 	return fdt32_to_cpu(*php);
 }
 
+const char *fdt_get_alias_namelen(const void *fdt,
+				  const char *name, int namelen)
+{
+	int aliasoffset;
+
+	aliasoffset = fdt_path_offset(fdt, "/aliases");
+	if (aliasoffset < 0)
+		return NULL;
+
+	return fdt_getprop_namelen(fdt, aliasoffset, name, namelen, NULL);
+}
+
+const char *fdt_get_alias(const void *fdt, const char *name)
+{
+	return fdt_get_alias_namelen(fdt, name, strlen(name));
+}
+
 int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
 {
 	int pdepth = 0, p = 0;
libfdt/libfdt.h +26 -0
Index: dtc/libfdt/libfdt.h
===================================================================
@@ -459,6 +459,32 @@ static inline void *fdt_getprop_w(void *
 uint32_t fdt_get_phandle(const void *fdt, int nodeoffset);
 
 /**
+ * fdt_get_namelen - get alias based on substring
+ * @fdt: pointer to the device tree blob
+ * @name: name of the alias th look up
+ * @namelen: number of characters of name to consider
+ *
+ * Identical to fdt_get_alias(), but only examine the first namelen
+ * characters of name for matching the alias name.
+ */
+const char *fdt_get_alias_namelen(const void *fdt,
+				  const char *name, int namelen);
+
+/**
+ * fdt_get_alias - retreive the path referenced by a given alias
+ * @fdt: pointer to the device tree blob
+ * @name: name of the alias th look up
+ *
+ * fdt_get_alias() retrieves the value of a given alias.  That is, the
+ * value of the property named 'name' in the node /aliases.
+ *
+ * returns:
+ *	a pointer to the expansion of the alias named 'name', of it exists
+ *	NULL, if the given alias or the /aliases node does not exist
+ */
+const char *fdt_get_alias(const void *fdt, const char *name);
+
+/**
  * fdt_get_path - determine the full path of a node
  * @fdt: pointer to the device tree blob
  * @nodeoffset: offset of the node whose path to find
tests/get_alias.c new file +58 -0

58 lines not on this page · raw message

tests/Makefile.tests +1 -0
Index: dtc/tests/Makefile.tests
===================================================================
@@ -4,6 +4,7 @@ LIB_TESTS_L = get_mem_rsv \
 	get_path supernode_atdepth_offset parent_offset \
 	node_offset_by_prop_value node_offset_by_phandle \
 	node_check_compatible node_offset_by_compatible \
+	get_alias \
 	notfound \
 	setprop_inplace nop_property nop_node \
 	sw_tree1 \
tests/run_tests.sh +1 -0
Index: dtc/tests/run_tests.sh
===================================================================
@@ -216,6 +216,7 @@ dtc_tests () {
 
     # Check aliases support in fdt_path_offset
     run_dtc_test -I dts -O dtb -o aliases.dtb aliases.dts
+    run_test get_alias aliases.dtb
     run_test path_offset_aliases aliases.dtb
 
     # Check /include/ directive
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
Applied
#1 lore raw
Quoted 7 lines

Kumar has already added alias expansion to fdt_path_offset(). However, in some circumstances it may be convenient for the user of libfdt to explicitly get the string expansion of an alias. This patch adds a function to do this, fdt_get_alias(), and uses it to implement fdt_path_offset().

Applied.

jdl

#2 lore raw

PATCH 1/1 libfdt: Fix error in documentation for fdt_get_alias_namelen()

In reply to David Gibson

Oops, screwed up the function name in the documenting comment for this function. Trivial correction in this patch.

---

David's cut-n-paste fix git-ized and applied to the dtc repo.

Best regards,
gvb

 libfdt/libfdt.h |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
libfdt/libfdt.h +1 -1
index a11bbf3..4a95e11 100644
@@ -459,7 +459,7 @@ static inline void *fdt_getprop_w(void *fdt, int nodeoffset,
 uint32_t fdt_get_phandle(const void *fdt, int nodeoffset);
 
 /**
- * fdt_get_namelen - get alias based on substring
+ * fdt_get_alias_namelen - get alias based on substring
  * @fdt: pointer to the device tree blob
  * @name: name of the alias th look up
  * @namelen: number of characters of name to consider
Signature · 1 line
1.5.6.5