u-boot.lists.denx.de archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] fdtgrep: Correct alignment of struct section
@ 2021-12-08 16:55 Simon Glass
  2021-12-08 16:55 ` [PATCH 2/2] fdtgrep: Handle an empty output tree Simon Glass
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Simon Glass @ 2021-12-08 16:55 UTC (permalink / raw)
  To: U-Boot Mailing List; +Cc: Simon Glass, Samuel Dionne-Riel

When outputting a devicetree we should not align the struct section to a
16-byte boundary. The normal position is fine, which is 8-byte aligned.

This avoids leaving adding 8 extra zero bytes in the output tree in the
case where the reserved section is empty (i.e has 16 zero bytes).

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 tools/fdtgrep.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/tools/fdtgrep.c b/tools/fdtgrep.c
index db512465db1..641d6a2e3e0 100644
--- a/tools/fdtgrep.c
+++ b/tools/fdtgrep.c
@@ -438,8 +438,7 @@ static int dump_fdt_regions(struct display_info *disp, const void *blob,
 	fdt = (struct fdt_header *)out;
 	memset(fdt, '\0', sizeof(*fdt));
 	fdt_set_magic(fdt, FDT_MAGIC);
-	struct_start = FDT_ALIGN(sizeof(struct fdt_header),
-					sizeof(struct fdt_reserve_entry));
+	struct_start = sizeof(struct fdt_header);
 	fdt_set_off_mem_rsvmap(fdt, struct_start);
 	fdt_set_version(fdt, FDT_LAST_SUPPORTED_VERSION);
 	fdt_set_last_comp_version(fdt, FDT_FIRST_SUPPORTED_VERSION);
-- 
2.34.1.400.ga245620fadb-goog


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] fdtgrep: Handle an empty output tree
  2021-12-08 16:55 [PATCH 1/2] fdtgrep: Correct alignment of struct section Simon Glass
@ 2021-12-08 16:55 ` Simon Glass
  2021-12-17 16:46 ` Simon Glass
  2021-12-17 16:46 ` [PATCH 1/2] fdtgrep: Correct alignment of struct section Simon Glass
  2 siblings, 0 replies; 4+ messages in thread
From: Simon Glass @ 2021-12-08 16:55 UTC (permalink / raw)
  To: U-Boot Mailing List; +Cc: Simon Glass, Artem Lapkin

In strange cases it is possible for fdtgrep to find nothing to output.
Typically this means that the resulting SPL device tree is not going to
allow anything to boot, but at present the tree is actually invalid,
since it only has an END tag in the struct region.

The FDT spec requires at least a root node. So add a special case to
include at least this, if the FDT_REG_SUPERNODES flag is set.

This ensures that grepping an empty tree still produces a valid tree.

Also add comments to the enum since it is not completely obvious from
the names now.

The typical symptom of this problem is a message from binman:

   pylibfdt error -11: FDT_ERR_BADSTRUCTURE

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 boot/fdt_region.c    | 43 +++++++++++++++++++++++++++++++++++++------
 include/fdt_region.h |  1 +
 2 files changed, 38 insertions(+), 6 deletions(-)

diff --git a/boot/fdt_region.c b/boot/fdt_region.c
index e4ef0ca7703..bac55593294 100644
--- a/boot/fdt_region.c
+++ b/boot/fdt_region.c
@@ -185,6 +185,8 @@ static int fdt_add_region(struct fdt_region_state *info, int offset, int size)
 			reg++;
 			reg->offset = offset;
 			reg->size = size;
+			if (!(offset - fdt_off_dt_struct(info->fdt)))
+				info->have_node = true;
 		}
 	} else {
 		return -1;
@@ -342,13 +344,19 @@ static int fdt_include_supernodes(struct fdt_region_state *info, int depth)
 	return 0;
 }
 
+/*
+ * Tracks the progress through the device tree. Everything fdt_next_region() is
+ * called it picks up at the same state as last time, looks at info->start and
+ * decides what region to add next
+ */
 enum {
-	FDT_DONE_NOTHING,
-	FDT_DONE_MEM_RSVMAP,
-	FDT_DONE_STRUCT,
-	FDT_DONE_END,
-	FDT_DONE_STRINGS,
-	FDT_DONE_ALL,
+	FDT_DONE_NOTHING,	/* Starting */
+	FDT_DONE_MEM_RSVMAP,	/* Completed mem_rsvmap region */
+	FDT_DONE_STRUCT,	/* Completed struct region */
+	FDT_DONE_EMPTY,		/* Completed checking for empty struct region */
+	FDT_DONE_END,		/* Completed the FDT_END tag */
+	FDT_DONE_STRINGS,	/* Completed the strings */
+	FDT_DONE_ALL,		/* All done */
 };
 
 int fdt_first_region(const void *fdt,
@@ -365,6 +373,7 @@ int fdt_first_region(const void *fdt,
 	info->can_merge = 1;
 	info->max_regions = 1;
 	info->start = -1;
+	info->have_node = false;
 	p->want = WANT_NOTHING;
 	p->end = path;
 	*p->end = '\0';
@@ -633,6 +642,8 @@ int fdt_next_region(const void *fdt,
 		 * region.
 		 */
 		if (!include && info->start != -1) {
+			if (!info->start)
+				info->have_node = true;
 			if (fdt_add_region(info, base + info->start,
 					   stop_at - info->start))
 				return 0;
@@ -644,11 +655,31 @@ int fdt_next_region(const void *fdt,
 		info->ptrs = p;
 	}
 
+	if (info->ptrs.done < FDT_DONE_EMPTY) {
+		/*
+		 * Handle a special case where no nodes have been written. Write
+		 * the first { so we have at least something, since
+		 * FDT_REG_SUPERNODES means that a valid tree is required. A
+		 * tree with no nodes is not valid
+		 */
+		if ((flags & FDT_REG_SUPERNODES) && !info->have_node &&
+		    info->start) {
+			/* Output the FDT_BEGIN_NODE and the empty name */
+			if (fdt_add_region(info, base, 8))
+				return 0;
+		}
+		info->ptrs.done++;
+	}
+
 	/* Add a region for the END tag and a separate one for string table */
 	if (info->ptrs.done < FDT_DONE_END) {
 		if (info->ptrs.nextoffset != fdt_size_dt_struct(fdt))
 			return -FDT_ERR_BADSTRUCTURE;
 
+		/* Output the } before the end tag to finish it off */
+		if (info->start == fdt_size_dt_struct(fdt) - 4)
+			info->start -= 4;
+
 		if (fdt_add_region(info, base + info->start,
 				   info->ptrs.nextoffset - info->start))
 			return 0;
diff --git a/include/fdt_region.h b/include/fdt_region.h
index ff7a1ccb9af..d0c68760f78 100644
--- a/include/fdt_region.h
+++ b/include/fdt_region.h
@@ -77,6 +77,7 @@ struct fdt_region_state {
 	int max_regions;		/* Maximum regions to find */
 	int can_merge;		/* 1 if we can merge with previous region */
 	int start;			/* Start position of current region */
+	bool have_node;			/* True if any node is included */
 	struct fdt_region_ptrs ptrs;	/* Pointers for what we are up to */
 };
 
-- 
2.34.1.400.ga245620fadb-goog


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 2/2] fdtgrep: Handle an empty output tree
  2021-12-08 16:55 [PATCH 1/2] fdtgrep: Correct alignment of struct section Simon Glass
  2021-12-08 16:55 ` [PATCH 2/2] fdtgrep: Handle an empty output tree Simon Glass
@ 2021-12-17 16:46 ` Simon Glass
  2021-12-17 16:46 ` [PATCH 1/2] fdtgrep: Correct alignment of struct section Simon Glass
  2 siblings, 0 replies; 4+ messages in thread
From: Simon Glass @ 2021-12-17 16:46 UTC (permalink / raw)
  To: Simon Glass; +Cc: Artem Lapkin, U-Boot Mailing List

In strange cases it is possible for fdtgrep to find nothing to output.
Typically this means that the resulting SPL device tree is not going to
allow anything to boot, but at present the tree is actually invalid,
since it only has an END tag in the struct region.

The FDT spec requires at least a root node. So add a special case to
include at least this, if the FDT_REG_SUPERNODES flag is set.

This ensures that grepping an empty tree still produces a valid tree.

Also add comments to the enum since it is not completely obvious from
the names now.

The typical symptom of this problem is a message from binman:

   pylibfdt error -11: FDT_ERR_BADSTRUCTURE

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 boot/fdt_region.c    | 43 +++++++++++++++++++++++++++++++++++++------
 include/fdt_region.h |  1 +
 2 files changed, 38 insertions(+), 6 deletions(-)

Applied to u-boot-dm/next, thanks!

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/2] fdtgrep: Correct alignment of struct section
  2021-12-08 16:55 [PATCH 1/2] fdtgrep: Correct alignment of struct section Simon Glass
  2021-12-08 16:55 ` [PATCH 2/2] fdtgrep: Handle an empty output tree Simon Glass
  2021-12-17 16:46 ` Simon Glass
@ 2021-12-17 16:46 ` Simon Glass
  2 siblings, 0 replies; 4+ messages in thread
From: Simon Glass @ 2021-12-17 16:46 UTC (permalink / raw)
  To: Simon Glass; +Cc: Samuel Dionne-Riel, U-Boot Mailing List

When outputting a devicetree we should not align the struct section to a
16-byte boundary. The normal position is fine, which is 8-byte aligned.

This avoids leaving adding 8 extra zero bytes in the output tree in the
case where the reserved section is empty (i.e has 16 zero bytes).

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 tools/fdtgrep.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

Applied to u-boot-dm/next, thanks!

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-12-17 16:46 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-08 16:55 [PATCH 1/2] fdtgrep: Correct alignment of struct section Simon Glass
2021-12-08 16:55 ` [PATCH 2/2] fdtgrep: Handle an empty output tree Simon Glass
2021-12-17 16:46 ` Simon Glass
2021-12-17 16:46 ` [PATCH 1/2] fdtgrep: Correct alignment of struct section Simon Glass

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).