Skip to contentSkip to conversation

libfdt: Rename and publish _fdt_check_header()

PATCH Applied David Gibson 2 participants 4 messages Oct 24, 2007

lore
Share & tools
lore
#0 lore raw

It's potentially useful for users of libfdt to sanity check a device tree (or, rather, a blob of data which may or may not be a device tree) before processing it in more detail with libfdt.

This patch renames the libfdt internal function _fdt_check_header() to fdt_check_header() and makes it a published function, so it can now be used for this purpose.

libfdt/fdt.c +2 -2
Index: dtc/libfdt/fdt.c
===================================================================
@@ -55,7 +55,7 @@
 
 #include "libfdt_internal.h"
 
-int _fdt_check_header(const void *fdt)
+int fdt_check_header(const void *fdt)
 {
 	if (fdt_magic(fdt) == FDT_MAGIC) {
 		/* Complete tree */
@@ -143,7 +143,7 @@
 
 int fdt_move(const void *fdt, void *buf, int bufsize)
 {
-	int err = _fdt_check_header(fdt);
+	int err = fdt_check_header(fdt);
 
 	if (err)
 		return err;
libfdt/libfdt.h +17 -12
Index: dtc/libfdt/libfdt.h
===================================================================
@@ -79,6 +79,22 @@
 
 #define FDT_ERR_MAX		12
 
+/* Low-level functions (you probably don't need these) */
+
+const void *fdt_offset_ptr(const void *fdt, int offset, int checklen);
+static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)
+{
+	return (void *)fdt_offset_ptr(fdt, offset, checklen);
+}
+
+
+#define fdt_offset_ptr_typed(fdt, offset, var) \
+	((typeof(var))(fdt_offset_ptr((fdt), (offset), sizeof(*(var)))))
+#define fdt_offset_ptr_typed_w(fdt, offset, var) \
+	((typeof(var))(fdt_offset_ptr_w((fdt), (offset), sizeof(*(var)))))
+
+/* General functions */
+
 #define fdt_get_header(fdt, field) \
 	(fdt32_to_cpu(((const struct fdt_header *)(fdt))->field))
 #define fdt_magic(fdt) 			(fdt_get_header(fdt, magic))
@@ -95,18 +111,7 @@
 #define fdt_set_header(fdt, field, val) \
 	((struct fdt_header *)(fdt))->field = cpu_to_fdt32(val)
 
-const void *fdt_offset_ptr(const void *fdt, int offset, int checklen);
-static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)
-{
-	return (void *)fdt_offset_ptr(fdt, offset, checklen);
-}
-
-
-#define fdt_offset_ptr_typed(fdt, offset, var) \
-	((typeof(var))(fdt_offset_ptr((fdt), (offset), sizeof(*(var)))))
-#define fdt_offset_ptr_typed_w(fdt, offset, var) \
-	((typeof(var))(fdt_offset_ptr_w((fdt), (offset), sizeof(*(var)))))
-
+int fdt_check_header(const void *fdt);
 int fdt_move(const void *fdt, void *buf, int bufsize);
 
 /* Read-only functions */
libfdt/libfdt_internal.h +0 -1
Index: dtc/libfdt/libfdt_internal.h
===================================================================
@@ -58,7 +58,6 @@
 #define memeq(p, q, n)	(memcmp((p), (q), (n)) == 0)
 #define streq(p, q)	(strcmp((p), (q)) == 0)
 
-int _fdt_check_header(const void *fdt);
 uint32_t _fdt_next_tag(const void *fdt, int startoffset, int *nextoffset);
 const char *_fdt_find_string(const char *strtab, int tabsize, const char *s);
 int _fdt_node_end_offset(void *fdt, int nodeoffset);
libfdt/fdt_ro.c +3 -3
Index: dtc/libfdt/fdt_ro.c
===================================================================
@@ -58,7 +58,7 @@
 #define CHECK_HEADER(fdt) \
 	{ \
 		int err; \
-		if ((err = _fdt_check_header(fdt)) != 0) \
+		if ((err = fdt_check_header(fdt)) != 0) \
 			return err; \
 	}
 
@@ -193,7 +193,7 @@
 	const struct fdt_node_header *nh;
 	int err;
 
-	if ((err = _fdt_check_header(fdt)) != 0)
+	if ((err = fdt_check_header(fdt)) != 0)
 		goto fail;
 
 	err = -FDT_ERR_BADOFFSET;
@@ -222,7 +222,7 @@
 	int offset, nextoffset;
 	int err;
 
-	if ((err = _fdt_check_header(fdt)) != 0)
+	if ((err = fdt_check_header(fdt)) != 0)
 		goto fail;
 
 	err = -FDT_ERR_BADOFFSET;
libfdt/fdt_rw.c +1 -1
Index: dtc/libfdt/fdt_rw.c
===================================================================
@@ -59,7 +59,7 @@
 {
 	int err;
 
-	if ((err = _fdt_check_header(fdt)))
+	if ((err = fdt_check_header(fdt)))
 		return err;
 	if (fdt_version(fdt) < 0x11)
 		return -FDT_ERR_BADVERSION;
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
#1 lore raw

libfdt: Rename and publish _fdt_next_tag()

Although it's a low-level function that shouldn't normally be needed, there are circumstances where it's useful for users of libfdt to use the _fdt_next_tag() function. Therefore, this patch renames it to fdt_next_tag() and publishes it in libfdt.h.

In addition, this patch adds a new testcase using fdt_next_tag(), dtbs_equal_ordered. This testcase tests for structural equality of two dtbs, including the order of properties and subnodes, but ignoring NOP tags, the order of the dtb sections and the layout of strings in the strings block. This will be useful for testing other dtc functionality in the future.

libfdt/fdt.c +1 -1
Index: dtc/libfdt/fdt.c
===================================================================
@@ -90,7 +90,7 @@
 	return p;
 }
 
-uint32_t _fdt_next_tag(const void *fdt, int offset, int *nextoffset)
+uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset)
 {
 	const uint32_t *tagp, *lenp;
 	uint32_t tag;
libfdt/libfdt.h +2 -1
Index: dtc/libfdt/libfdt.h
===================================================================
@@ -92,8 +92,9 @@
 #define fdt_offset_ptr_typed_w(fdt, offset, var) \
 	((typeof(var))(fdt_offset_ptr_w((fdt), (offset), sizeof(*(var)))))
 
-/* General functions */
+uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
 
+/* General functions */
 #define fdt_get_header(fdt, field) \
 	(fdt32_to_cpu(((const struct fdt_header *)(fdt))->field))
 #define fdt_magic(fdt) 			(fdt_get_header(fdt, magic))
libfdt/fdt_ro.c +11 -11
Index: dtc/libfdt/fdt_ro.c
===================================================================
@@ -113,13 +113,13 @@
 
 	CHECK_HEADER(fdt);
 
-	tag = _fdt_next_tag(fdt, parentoffset, &nextoffset);
+	tag = fdt_next_tag(fdt, parentoffset, &nextoffset);
 	if (tag != FDT_BEGIN_NODE)
 		return -FDT_ERR_BADOFFSET;
 
 	do {
 		offset = nextoffset;
-		tag = _fdt_next_tag(fdt, offset, &nextoffset);
+		tag = fdt_next_tag(fdt, offset, &nextoffset);
 
 		switch (tag) {
 		case FDT_END:
@@ -229,14 +229,14 @@
 	if (nodeoffset % FDT_TAGSIZE)
 		goto fail;
 
-	tag = _fdt_next_tag(fdt, nodeoffset, &nextoffset);
+	tag = fdt_next_tag(fdt, nodeoffset, &nextoffset);
 	if (tag != FDT_BEGIN_NODE)
 		goto fail;
 
 	do {
 		offset = nextoffset;
 
-		tag = _fdt_next_tag(fdt, offset, &nextoffset);
+		tag = fdt_next_tag(fdt, offset, &nextoffset);
 		switch (tag) {
 		case FDT_END:
 			err = -FDT_ERR_TRUNCATED;
@@ -302,7 +302,7 @@
 
 	CHECK_HEADER(fdt);
 
-	tag = _fdt_next_tag(fdt, 0, &nextoffset);
+	tag = fdt_next_tag(fdt, 0, &nextoffset);
 	if (tag != FDT_BEGIN_NODE)
 		return -FDT_ERR_BADSTRUCTURE;
 
@@ -313,7 +313,7 @@
 
 	while (nextoffset <= nodeoffset) {
 		offset = nextoffset;
-		tag = _fdt_next_tag(fdt, offset, &nextoffset);
+		tag = fdt_next_tag(fdt, offset, &nextoffset);
 		switch (tag) {
 		case FDT_END:
 			return -FDT_ERR_BADOFFSET;
@@ -374,7 +374,7 @@
 
 	do {
 		offset = nextoffset;
-		tag = _fdt_next_tag(fdt, offset, &nextoffset);
+		tag = fdt_next_tag(fdt, offset, &nextoffset);
 		switch (tag) {
 		case FDT_END:
 			return -FDT_ERR_BADOFFSET;
@@ -439,7 +439,7 @@
 	CHECK_HEADER(fdt);
 
 	if (startoffset >= 0) {
-		tag = _fdt_next_tag(fdt, startoffset, &nextoffset);
+		tag = fdt_next_tag(fdt, startoffset, &nextoffset);
 		if (tag != FDT_BEGIN_NODE)
 			return -FDT_ERR_BADOFFSET;
 	} else {
@@ -453,7 +453,7 @@
 	 * approach; performance can come later. */
 	do {
 		offset = nextoffset;
-		tag = _fdt_next_tag(fdt, offset, &nextoffset);
+		tag = fdt_next_tag(fdt, offset, &nextoffset);
 
 		switch (tag) {
 		case FDT_BEGIN_NODE:
@@ -520,7 +520,7 @@
 	CHECK_HEADER(fdt);
 
 	if (startoffset >= 0) {
-		tag = _fdt_next_tag(fdt, startoffset, &nextoffset);
+		tag = fdt_next_tag(fdt, startoffset, &nextoffset);
 		if (tag != FDT_BEGIN_NODE)
 			return -FDT_ERR_BADOFFSET;
 	} else {
@@ -534,7 +534,7 @@
 	 * implement approach; performance can come later. */
 	do {
 		offset = nextoffset;
-		tag = _fdt_next_tag(fdt, offset, &nextoffset);
+		tag = fdt_next_tag(fdt, offset, &nextoffset);
 
 		switch (tag) {
 		case FDT_BEGIN_NODE:
libfdt/fdt_rw.c +3 -3
Index: dtc/libfdt/fdt_rw.c
===================================================================
@@ -224,7 +224,7 @@
 	int namestroff;
 	int err;
 
-	tag = _fdt_next_tag(fdt, nodeoffset, &nextoffset);
+	tag = fdt_next_tag(fdt, nodeoffset, &nextoffset);
 	if (tag != FDT_BEGIN_NODE)
 		return -FDT_ERR_BADOFFSET;
 
@@ -298,10 +298,10 @@
 		return offset;
 
 	/* Try to place the new node after the parent's properties */
-	_fdt_next_tag(fdt, parentoffset, &nextoffset); /* skip the BEGIN_NODE */
+	fdt_next_tag(fdt, parentoffset, &nextoffset); /* skip the BEGIN_NODE */
 	do {
 		offset = nextoffset;
-		tag = _fdt_next_tag(fdt, offset, &nextoffset);
+		tag = fdt_next_tag(fdt, offset, &nextoffset);
 	} while (tag == FDT_PROP);
 
 	nh = _fdt_offset_ptr_w(fdt, offset);
libfdt/fdt_sw.c +1 -1
Index: dtc/libfdt/fdt_sw.c
===================================================================
@@ -235,7 +235,7 @@
 
 	/* Walk the structure, correcting string offsets */
 	offset = 0;
-	while ((tag = _fdt_next_tag(fdt, offset, &nextoffset)) != FDT_END) {
+	while ((tag = fdt_next_tag(fdt, offset, &nextoffset)) != FDT_END) {
 		if (tag == FDT_PROP) {
 			struct fdt_property *prop =
 				fdt_offset_ptr_w(fdt, offset, sizeof(*prop));
libfdt/fdt_wip.c +2 -2
Index: dtc/libfdt/fdt_wip.c
===================================================================
@@ -100,12 +100,12 @@
 	uint32_t tag;
 	int offset, nextoffset;
 
-	tag = _fdt_next_tag(fdt, nodeoffset, &nextoffset);
+	tag = fdt_next_tag(fdt, nodeoffset, &nextoffset);
 	if (tag != FDT_BEGIN_NODE)
 		return -FDT_ERR_BADOFFSET;
 	do {
 		offset = nextoffset;
-		tag = _fdt_next_tag(fdt, offset, &nextoffset);
+		tag = fdt_next_tag(fdt, offset, &nextoffset);
 
 		switch (tag) {
 		case FDT_END:
tests/dtbs_equal_ordered.c new file +139 -0

139 lines not on this page · raw message

tests/Makefile.tests +1 -1
Index: dtc/tests/Makefile.tests
===================================================================
@@ -8,7 +8,7 @@
 	sw_tree1 \
 	move_and_save \
 	open_pack rw_tree1 setprop del_property del_node \
-	string_escapes
+	string_escapes dtbs_equal_ordered
 LIB_TESTS = $(LIB_TESTS_L:%=$(TESTS_PREFIX)%)
 
 LIBTREE_TESTS_L = truncated_property
tests/tests.h +1 -1
Index: dtc/tests/tests.h
===================================================================
@@ -129,7 +129,7 @@
 #define check_getprop_string(fdt, nodeoffset, name, s) \
 	check_getprop((fdt), (nodeoffset), (name), strlen(s)+1, (s))
 int nodename_eq(const char *s1, const char *s2);
-//void *load_blob(const char *filename);
+void *load_blob(const char *filename);
 void *load_blob_arg(int argc, char *argv[]);
 void save_blob(const char *filename, void *blob);
 
tests/run_tests.sh +1 -0
Index: dtc/tests/run_tests.sh
===================================================================
@@ -104,6 +104,7 @@
     run_test dtc.sh -I dts -O dtb -o dtc_tree1.test.dtb test_tree1.dts
     tree1_tests dtc_tree1.test.dtb
     tree1_tests_rw dtc_tree1.test.dtb
+    run_test dtbs_equal_ordered dtc_tree1.test.dtb test_tree1.dtb
 
     run_test dtc.sh -I dts -O dtb -o dtc_escapes.test.dtb escapes.dts
     run_test string_escapes dtc_escapes.test.dtb
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
#2 lore raw

Re: libfdt: Rename and publish _fdt_next_tag()

So, like, the other day David Gibson mumbled:

Quoted 5 lines

Although it's a low-level function that shouldn't normally be needed, there are circumstances where it's useful for users of libfdt to use the _fdt_next_tag() function. Therefore, this patch renames it to fdt_next_tag() and publishes it in libfdt.h.

Quoted 8 lines

In addition, this patch adds a new testcase using fdt_next_tag(), dtbs_equal_ordered. This testcase tests for structural equality of two dtbs, including the order of properties and subnodes, but ignoring NOP tags, the order of the dtb sections and the layout of strings in the strings block. This will be useful for testing other dtc functionality in the future.

Applied.

Thanks,
jdl

Applied
#3 lore raw
In reply to David Gibson

So, like, the other day David Gibson mumbled:

Quoted 9 lines

It's potentially useful for users of libfdt to sanity check a device tree (or, rather, a blob of data which may or may not be a device tree) before processing it in more detail with libfdt.

This patch renames the libfdt internal function _fdt_check_header() to fdt_check_header() and makes it a published function, so it can now be used for this purpose.

Applied.

Thanks,
jdl