[PATCH] fdtgrep: Add option to reserve free space in the output FDT
Simon Glass
sjg at chromium.org
Wed Apr 22 04:31:23 CEST 2026
When producing a DTB from fdtgrep, there is no way to reserve extra
space so that more nodes/properties can be added at runtime without
growing totalsize first.
Add a -B/--pad option that appends a caller-specified number of free
bytes after the strings section and grows totalsize accordingly. The
padding is applied after fdt_pack(), so it is not reclaimed when -r is
used.
Suggested-by: Marek Vasut <marek.vasut at mailbox.org>
Signed-off-by: Simon Glass <sjg at chromium.org>
---
tools/binman/btool/fdtgrep.py | 3 ++-
tools/fdtgrep.c | 31 ++++++++++++++++++++++++++++++-
2 files changed, 32 insertions(+), 2 deletions(-)
diff --git a/tools/binman/btool/fdtgrep.py b/tools/binman/btool/fdtgrep.py
index 446b2f4144b..7b393241878 100644
--- a/tools/binman/btool/fdtgrep.py
+++ b/tools/binman/btool/fdtgrep.py
@@ -16,9 +16,10 @@ Output formats are:
dtb - device tree blob (sets -Hmt automatically)
bin - device tree fragment (may not be a valid .dtb)
-Options: -[haAc:b:C:defg:G:HIlLmn:N:o:O:p:P:rRsStTvhV]
+Options: -[haAB:c:b:C:defg:G:HIlLmn:N:o:O:p:P:rRsStTvhV]
-a, --show-address Display address
-A, --colour Show all nodes/tags, colour those that match
+ -B, --pad <arg> Append free space to the output FDT, for later growth
-b, --include-node-with-prop <arg> Include contains containing property
-c, --include-compat <arg> Compatible nodes to include in grep
-C, --exclude-compat <arg> Compatible nodes to exclude in grep
diff --git a/tools/fdtgrep.c b/tools/fdtgrep.c
index b4c041070f5..83e3d0b9314 100644
--- a/tools/fdtgrep.c
+++ b/tools/fdtgrep.c
@@ -64,6 +64,7 @@ struct display_info {
int types_exc; /* Mask of types that we exclude (FDT_IS...) */
int invert; /* Invert polarity of match */
int props_up; /* Imply properties up to supernodes */
+ int pad; /* Free bytes to append after the strings section */
struct value_node *value_head; /* List of values to match */
const char *output_fname; /* Output filename */
FILE *fout; /* File to write dts/dtb output */
@@ -913,6 +914,27 @@ static int do_fdtgrep(struct display_info *disp, const char *filename)
size = fdt_totalsize(fdt);
}
+ if (disp->pad) {
+ int new_size = fdt_totalsize(fdt) + disp->pad;
+ void *new_fdt = realloc(fdt, new_size);
+
+ if (!new_fdt) {
+ fprintf(stderr, "Out_of_memory\n");
+ ret = -1;
+ goto err;
+ }
+ fdt = new_fdt;
+ ret = fdt_open_into(fdt, fdt, new_size);
+ if (ret < 0) {
+ fprintf(stderr,
+ "Failed to expand FDT: %s\n",
+ fdt_strerror(ret));
+ goto err;
+ }
+ memset((char *)fdt + size, '\0', new_size - size);
+ size = new_size;
+ }
+
if ((size_t)size != fwrite(fdt, 1, size, disp->fout)) {
fprintf(stderr, "Write failure, %d bytes\n", size);
free(fdt);
@@ -965,11 +987,12 @@ static const char usage_synopsis[] =
case '?': usage("unknown option");
static const char usage_short_opts[] =
- "haAc:b:C:defg:G:HIlLmn:N:o:O:p:P:rRsStTuv"
+ "haAB:c:b:C:defg:G:HIlLmn:N:o:O:p:P:rRsStTuv"
USAGE_COMMON_SHORT_OPTS;
static const struct option usage_long_opts[] = {
{"show-address", no_argument, NULL, 'a'},
{"colour", no_argument, NULL, 'A'},
+ {"pad", a_argument, NULL, 'B'},
{"include-node-with-prop", a_argument, NULL, 'b'},
{"include-compat", a_argument, NULL, 'c'},
{"exclude-compat", a_argument, NULL, 'C'},
@@ -1002,6 +1025,7 @@ static const struct option usage_long_opts[] = {
static const char * const usage_opts_help[] = {
"Display address",
"Show all nodes/tags, colour those that match",
+ "Append free space to the output FDT, for later growth",
"Include contains containing property",
"Compatible nodes to include in grep",
"Compatible nodes to exclude in grep",
@@ -1132,6 +1156,11 @@ static void scan_args(struct display_info *disp, int argc, char *argv[])
case 'A':
disp->all = 1;
break;
+ case 'B':
+ disp->pad = atoi(optarg);
+ if (disp->pad < 0)
+ usage("Padding must be non-negative");
+ break;
case 'b':
type = FDT_NODE_HAS_PROP;
break;
---
base-commit: 052988aa29bfd506d7ce207fbb3f5374a5dbecbb
branch: fdtgrep
--
2.43.0
More information about the U-Boot
mailing list