tests: Replace trees.S/dumptrees with treegen for making test dtbs

Switch the build system to use treegen instead of trees.S + dumptrees
for generating the test DTB files.  treegen produces byte-identical
output without requiring a GNU assembler.

Remove the old tree.S, dumptrees.c and associated pieces.

This also removes the need for the ASM_CONST_LL macro and separated
values for the high and low halves of 64-bit values: those only
existed so the same constants could be embedded in both C and
assembly.  Remove those from testdata.h, and the copies in
pylibfdt_tests.py.

Assisted-by: Claude:claude-opus-4-6
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
This commit is contained in:
David Gibson 2026-06-14 16:30:42 +10:00
parent 4df9ca4c8d
commit 214372a273
7 changed files with 23 additions and 501 deletions

View file

@ -50,17 +50,17 @@ TESTS_TREES = $(TESTS_TREES_L:%=$(TESTS_PREFIX)%)
TESTS_TARGETS = $(TESTS) $(TESTS_TREES) TESTS_TARGETS = $(TESTS) $(TESTS_TREES)
TESTS_DEPFILES = $(TESTS:%=%.d) \ TESTS_DEPFILES = $(TESTS:%=%.d) \
$(addprefix $(TESTS_PREFIX),testutils.d trees.d dumptrees.d) $(addprefix $(TESTS_PREFIX),testutils.d)
TESTS_CLEANFILES_L = $(STD_CLEANFILES) \ TESTS_CLEANFILES_L = $(STD_CLEANFILES) \
*.dtb *.test.dts *.test.dt.yaml *.dtsv1 tmp.* *.bak \ *.dtb *.test.dts *.test.dt.yaml *.dtsv1 tmp.* *.bak \
dumptrees treegen treegen
TESTS_CLEANFILES = $(TESTS) $(TESTS_CLEANFILES_L:%=$(TESTS_PREFIX)%) TESTS_CLEANFILES = $(TESTS) $(TESTS_CLEANFILES_L:%=$(TESTS_PREFIX)%)
TESTS_CLEANDIRS_L = fs treegen-out TESTS_CLEANDIRS_L = fs
TESTS_CLEANDIRS = $(TESTS_CLEANDIRS_L:%=$(TESTS_PREFIX)%) TESTS_CLEANDIRS = $(TESTS_CLEANDIRS_L:%=$(TESTS_PREFIX)%)
.PHONY: tests .PHONY: tests
tests: $(TESTS) $(TESTS_TREES) $(TESTS_PREFIX)treegen tests: $(TESTS) $(TESTS_TREES)
$(LIB_TESTS): %: $(TESTS_PREFIX)testutils.o util.o $(LIBFDT_dep) $(LIB_TESTS): %: $(TESTS_PREFIX)testutils.o util.o $(LIBFDT_dep)
@ -71,13 +71,9 @@ $(DL_LIB_TESTS): %: %.o $(TESTS_PREFIX)testutils.o util.o $(LIBFDT_dep)
@$(VECHO) LD [libdl] $@ @$(VECHO) LD [libdl] $@
$(LINK.c) -o $@ $^ $(LIBDL) $(LINK.c) -o $@ $^ $(LIBDL)
$(TESTS_PREFIX)treegen: $(TESTS_TREES): $(TESTS_PREFIX)treegen
@$(VECHO) TREEGEN
$(TESTS_PREFIX)dumptrees: $(TESTS_PREFIX)trees.o cd $(TESTS_PREFIX); ./treegen . >/dev/null
$(TESTS_TREES): $(TESTS_PREFIX)dumptrees
@$(VECHO) DUMPTREES
cd $(TESTS_PREFIX); ./dumptrees . >/dev/null
tests_clean: tests_clean:
@$(VECHO) CLEAN "(tests)" @$(VECHO) CLEAN "(tests)"

View file

@ -1,70 +0,0 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* dumptrees - utility for libfdt testing
*
* (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2006.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdint.h>
#include <libfdt.h>
#include "testdata.h"
static struct {
void *blob;
const char *filename;
} trees[] = {
#define TREE(name) { &name, #name ".dtb" }
TREE(test_tree1),
TREE(bad_node_char), TREE(bad_node_format), TREE(bad_prop_char),
TREE(ovf_size_strings),
TREE(truncated_property), TREE(truncated_string),
TREE(truncated_memrsv),
TREE(unterminated_memrsv),
TREE(two_roots),
TREE(named_root)
};
#define NUM_TREES (sizeof(trees) / sizeof(trees[0]))
int main(int argc, char *argv[])
{
unsigned int i;
if (argc != 2) {
fprintf(stderr, "Missing output directory argument\n");
return 1;
}
if (chdir(argv[1]) != 0) {
perror("chdir()");
return 1;
}
for (i = 0; i < NUM_TREES; i++) {
void *blob = trees[i].blob;
const char *filename = trees[i].filename;
int size;
int fd;
int ret;
size = fdt_totalsize(blob);
printf("Tree \"%s\", %d bytes\n", filename, size);
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (fd < 0)
perror("open()");
ret = write(fd, blob, size);
if (ret != size)
perror("write()");
close(fd);
}
exit(0);
}

View file

@ -1,18 +1,10 @@
trees = static_library('trees', files('trees.S'),
build_by_default: false,
include_directories: libfdt_inc)
dumptrees = executable('dumptrees', files('dumptrees.c'),
build_by_default: false,
link_with: trees, dependencies: libfdt_dep)
treegen = executable('treegen', files('treegen.c'), treegen = executable('treegen', files('treegen.c'),
build_by_default: false, build_by_default: false,
dependencies: [util_dep]) dependencies: [util_dep])
dumptrees_dtb = custom_target( treegen_dtb = custom_target(
'dumptrees', 'treegen_dtb',
command: [dumptrees, meson.current_build_dir()], command: [treegen, meson.current_build_dir()],
output: [ output: [
'test_tree1.dtb', 'test_tree1.dtb',
'bad_node_char.dtb', 'bad_node_char.dtb',
@ -23,6 +15,8 @@ dumptrees_dtb = custom_target(
'truncated_string.dtb', 'truncated_string.dtb',
'truncated_memrsv.dtb', 'truncated_memrsv.dtb',
'unterminated_memrsv.dtb', 'unterminated_memrsv.dtb',
'two_roots.dtb',
'named_root.dtb',
] ]
) )
@ -144,10 +138,9 @@ run_test_types = [
'fdtput', 'fdtput',
'fdtdump', 'fdtdump',
'fdtoverlay', 'fdtoverlay',
'treegen'
] ]
run_test_deps = [ run_test_deps = [
dtc_tools, dumptrees_dtb, tests_exe, treegen dtc_tools, treegen_dtb, tests_exe,
] ]
if pylibfdt_enabled if pylibfdt_enabled
run_test_types += 'pylibfdt' run_test_types += 'pylibfdt'

View file

@ -13,26 +13,15 @@ sys.path.insert(0, '../pylibfdt')
import libfdt import libfdt
from libfdt import Fdt, FdtSw, FdtException, QUIET_NOTFOUND, QUIET_ALL from libfdt import Fdt, FdtSw, FdtException, QUIET_NOTFOUND, QUIET_ALL
TEST_ADDR_1H = 0xdeadbeef
TEST_ADDR_1L = 0x00000000
TEST_ADDR_1 = (TEST_ADDR_1H << 32) | TEST_ADDR_1L
TEST_ADDR_1 = 0x8000000000000000 TEST_ADDR_1 = 0x8000000000000000
TEST_SIZE_1H = 0x00000000 TEST_SIZE_1 = 0x0000000000100000
TEST_SIZE_1L = 0x00100000 TEST_ADDR_2 = 123456789
TEST_SIZE_1 = (TEST_SIZE_1H << 32) | TEST_SIZE_1L TEST_SIZE_2 = 0o10000
TEST_ADDR_2H = 0
TEST_ADDR_2L = 123456789
TEST_ADDR_2 = (TEST_ADDR_2H << 32) | TEST_ADDR_2L
TEST_SIZE_2H = 0
TEST_SIZE_2L = 0o10000
TEST_SIZE_2 = (TEST_SIZE_2H << 32) | TEST_SIZE_2L
TEST_VALUE_1 = 0xdeadbeef TEST_VALUE_1 = 0xdeadbeef
TEST_VALUE_2 = 123456789 TEST_VALUE_2 = 123456789
TEST_VALUE64_1H = 0xdeadbeef TEST_VALUE64_1 = 0xdeadbeef01abcdef
TEST_VALUE64_1L = 0x01abcdef
TEST_VALUE64_1 = (TEST_VALUE64_1H << 32) | TEST_VALUE64_1L
PHANDLE_1 = 0x2000 PHANDLE_1 = 0x2000
PHANDLE_2 = 0x2001 PHANDLE_2 = 0x2001

View file

@ -1091,16 +1091,6 @@ fdtoverlay_tests() {
run_wrap_test test "$bd" = "$pd" run_wrap_test test "$bd" = "$pd"
} }
treegen_tests () {
mkdir -p treegen-out
run_wrap_test ./treegen treegen-out
for tree in test_tree1 bad_node_char bad_node_format bad_prop_char \
ovf_size_strings truncated_property truncated_string \
truncated_memrsv unterminated_memrsv two_roots named_root; do
run_wrap_test cmp $tree.dtb treegen-out/$tree.dtb
done
}
pylibfdt_tests () { pylibfdt_tests () {
run_dtc_test -I dts -O dtb -o test_props.dtb "$SRCDIR/test_props.dts" run_dtc_test -I dts -O dtb -o test_props.dtb "$SRCDIR/test_props.dts"
TMP=/tmp/tests.stderr.$$ TMP=/tmp/tests.stderr.$$
@ -1140,7 +1130,7 @@ while getopts "vt:me" ARG ; do
done done
if [ -z "$TESTSETS" ]; then if [ -z "$TESTSETS" ]; then
TESTSETS="libfdt utilfdt dtc dtbs_equal fdtget fdtput fdtdump fdtoverlay treegen" TESTSETS="libfdt utilfdt dtc dtbs_equal fdtget fdtput fdtdump fdtoverlay"
# Test pylibfdt if the libfdt Python module is available. # Test pylibfdt if the libfdt Python module is available.
if ! $no_python; then if ! $no_python; then
@ -1180,9 +1170,6 @@ for set in $TESTSETS; do
"fdtoverlay") "fdtoverlay")
fdtoverlay_tests fdtoverlay_tests
;; ;;
"treegen")
treegen_tests
;;
esac esac
done done

View file

@ -1,28 +1,12 @@
#ifdef __ASSEMBLER__ #define TEST_ADDR_1 0xdeadbeef00000000ULL
#define ASM_CONST_LL(x) (x) #define TEST_SIZE_1 0x0000000000100000ULL
#else #define TEST_ADDR_2 123456789ULL
#define ASM_CONST_LL(x) (x##ULL) #define TEST_SIZE_2 010000ULL
#endif
#define TEST_ADDR_1H ASM_CONST_LL(0xdeadbeef)
#define TEST_ADDR_1L ASM_CONST_LL(0x00000000)
#define TEST_ADDR_1 ((TEST_ADDR_1H << 32) | TEST_ADDR_1L)
#define TEST_SIZE_1H ASM_CONST_LL(0x00000000)
#define TEST_SIZE_1L ASM_CONST_LL(0x00100000)
#define TEST_SIZE_1 ((TEST_SIZE_1H << 32) | TEST_SIZE_1L)
#define TEST_ADDR_2H ASM_CONST_LL(0)
#define TEST_ADDR_2L ASM_CONST_LL(123456789)
#define TEST_ADDR_2 ((TEST_ADDR_2H << 32) | TEST_ADDR_2L)
#define TEST_SIZE_2H ASM_CONST_LL(0)
#define TEST_SIZE_2L ASM_CONST_LL(010000)
#define TEST_SIZE_2 ((TEST_SIZE_2H << 32) | TEST_SIZE_2L)
#define TEST_VALUE_1 0xdeadbeef #define TEST_VALUE_1 0xdeadbeef
#define TEST_VALUE_2 123456789 #define TEST_VALUE_2 123456789
#define TEST_VALUE64_1H ASM_CONST_LL(0xdeadbeef) #define TEST_VALUE64_1 0xdeadbeef01abcdefULL
#define TEST_VALUE64_1L ASM_CONST_LL(0x01abcdef)
#define TEST_VALUE64_1 ((TEST_VALUE64_1H << 32) | TEST_VALUE64_1L)
#define PHANDLE_1 0x2000 #define PHANDLE_1 0x2000
#define PHANDLE_2 0x2001 #define PHANDLE_2 0x2001
@ -45,17 +29,3 @@
#define TEST_MEMREGION_SIZE 0x9abcdef0 #define TEST_MEMREGION_SIZE 0x9abcdef0
#define TEST_MEMREGION_SIZE_HI 0x0fedcba900000000 #define TEST_MEMREGION_SIZE_HI 0x0fedcba900000000
#define TEST_MEMREGION_SIZE_INC 0x1000 #define TEST_MEMREGION_SIZE_INC 0x1000
#ifndef __ASSEMBLER__
extern struct fdt_header test_tree1;
extern struct fdt_header truncated_property;
extern struct fdt_header bad_node_char;
extern struct fdt_header bad_node_format;
extern struct fdt_header bad_prop_char;
extern struct fdt_header ovf_size_strings;
extern struct fdt_header truncated_string;
extern struct fdt_header truncated_memrsv;
extern struct fdt_header unterminated_memrsv;
extern struct fdt_header two_roots;
extern struct fdt_header named_root;
#endif /* ! __ASSEMBLER__ */

View file

@ -1,343 +0,0 @@
#include <fdt.h>
#include "testdata.h"
.macro fdtlong val
.byte ((\val) >> 24) & 0xff
.byte ((\val) >> 16) & 0xff
.byte ((\val) >> 8) & 0xff
.byte (\val) & 0xff
.endm
.macro treehdr tree
.balign 8
.globl \tree
\tree :
fdtlong FDT_MAGIC
fdtlong (\tree\()_end - \tree)
fdtlong (\tree\()_struct - \tree)
fdtlong (\tree\()_strings - \tree)
fdtlong (\tree\()_rsvmap - \tree)
fdtlong 0x11
fdtlong 0x10
fdtlong 0
fdtlong (\tree\()_strings_end - \tree\()_strings)
fdtlong (\tree\()_struct_end - \tree\()_struct)
.endm
.macro rsvmape addrh, addrl, lenh, lenl
fdtlong \addrh
fdtlong \addrl
fdtlong \lenh
fdtlong \lenl
.endm
.macro empty_rsvmap tree
.balign 8
\tree\()_rsvmap:
rsvmape 0, 0, 0, 0
\tree\()_rsvmap_end:
.endm
.macro prophdr tree, name, len
fdtlong FDT_PROP
fdtlong \len
fdtlong (\tree\()_\name - \tree\()_strings)
.endm
.macro propnil tree, name
prophdr \tree, \name, 0
.endm
.macro propu32 tree, name, val
prophdr \tree, \name, 4
fdtlong \val
.endm
.macro propu64 tree, name, valh, vall
prophdr \tree, \name, 8
fdtlong \valh
fdtlong \vall
.endm
.macro propstr tree, name, str:vararg
prophdr \tree, \name, (55f - 54f)
54:
.asciz \str
55:
.balign 4
.endm
.macro beginn name:vararg
fdtlong FDT_BEGIN_NODE
.asciz \name
.balign 4
.endm
.macro endn
fdtlong FDT_END_NODE
.endm
.macro string tree, name, str:vararg
\tree\()_\name :
.asciz \str
.endm
.data
treehdr test_tree1
.balign 8
test_tree1_rsvmap:
rsvmape TEST_ADDR_1H, TEST_ADDR_1L, TEST_SIZE_1H, TEST_SIZE_1L
rsvmape TEST_ADDR_2H, TEST_ADDR_2L, TEST_SIZE_2H, TEST_SIZE_2L
rsvmape 0, 0, 0, 0
test_tree1_rsvmap_end:
test_tree1_struct:
beginn ""
propstr test_tree1, compatible, "test_tree1"
propu32 test_tree1, prop_int, TEST_VALUE_1
propu64 test_tree1, prop_int64, TEST_VALUE64_1H, TEST_VALUE64_1L
propstr test_tree1, prop_str, TEST_STRING_1
propu32 test_tree1, address_cells, 1
propu32 test_tree1, size_cells, 0
beginn "subnode@1"
propstr test_tree1, compatible, "subnode1"
propu32 test_tree1, reg, 1
propu32 test_tree1, prop_int, TEST_VALUE_1
beginn "subsubnode"
propstr test_tree1, compatible, "subsubnode1\0subsubnode"
propstr test_tree1, placeholder, "this is a placeholder string\0string2"
propu32 test_tree1, prop_int, TEST_VALUE_1
endn
beginn "ss1"
endn
endn
beginn "subnode@2"
propu32 test_tree1, reg, 2
propu32 test_tree1, linux_phandle, PHANDLE_1
propu32 test_tree1, prop_int, TEST_VALUE_2
propu32 test_tree1, address_cells, 1
propu32 test_tree1, size_cells, 0
beginn "subsubnode@0"
propu32 test_tree1, reg, 0
propu32 test_tree1, phandle, PHANDLE_2
propstr test_tree1, compatible, "subsubnode2\0subsubnode"
propu32 test_tree1, prop_int, TEST_VALUE_2
endn
beginn "ss2"
endn
endn
endn
fdtlong FDT_END
test_tree1_struct_end:
test_tree1_strings:
string test_tree1, compatible, "compatible"
string test_tree1, prop_int, "prop-int"
string test_tree1, prop_int64, "prop-int64"
string test_tree1, prop_str, "prop-str"
string test_tree1, linux_phandle, "linux,phandle"
string test_tree1, phandle, "phandle"
string test_tree1, reg, "reg"
string test_tree1, placeholder, "placeholder"
string test_tree1, address_cells, "#address-cells"
string test_tree1, size_cells, "#size-cells"
test_tree1_strings_end:
test_tree1_end:
treehdr truncated_property
empty_rsvmap truncated_property
truncated_property_struct:
beginn ""
prophdr truncated_property, prop_truncated, 4
/* Oops, no actual property data here */
truncated_property_struct_end:
truncated_property_strings:
string truncated_property, prop_truncated, "truncated"
truncated_property_strings_end:
truncated_property_end:
treehdr bad_node_char
empty_rsvmap bad_node_char
bad_node_char_struct:
beginn ""
beginn "sub$node"
endn
endn
fdtlong FDT_END
bad_node_char_struct_end:
bad_node_char_strings:
bad_node_char_strings_end:
bad_node_char_end:
treehdr bad_node_format
empty_rsvmap bad_node_format
bad_node_format_struct:
beginn ""
beginn "subnode@1@2"
endn
endn
fdtlong FDT_END
bad_node_format_struct_end:
bad_node_format_strings:
bad_node_format_strings_end:
bad_node_format_end:
treehdr bad_prop_char
empty_rsvmap bad_prop_char
bad_prop_char_struct:
beginn ""
propu32 bad_prop_char, prop, TEST_VALUE_1
endn
fdtlong FDT_END
bad_prop_char_struct_end:
bad_prop_char_strings:
string bad_prop_char, prop, "prop$erty"
bad_prop_char_strings_end:
bad_prop_char_end:
/* overflow_size_strings */
.balign 8
.globl ovf_size_strings
ovf_size_strings:
fdtlong FDT_MAGIC
fdtlong (ovf_size_strings_end - ovf_size_strings)
fdtlong (ovf_size_strings_struct - ovf_size_strings)
fdtlong (ovf_size_strings_strings - ovf_size_strings)
fdtlong (ovf_size_strings_rsvmap - ovf_size_strings)
fdtlong 0x11
fdtlong 0x10
fdtlong 0
fdtlong 0xffffffff
fdtlong (ovf_size_strings_struct_end - ovf_size_strings_struct)
empty_rsvmap ovf_size_strings
ovf_size_strings_struct:
beginn ""
propu32 ovf_size_strings, bad_string, 0
endn
fdtlong FDT_END
ovf_size_strings_struct_end:
ovf_size_strings_strings:
string ovf_size_strings, x, "x"
ovf_size_strings_bad_string = ovf_size_strings_strings + 0x10000000
ovf_size_strings_strings_end:
ovf_size_strings_end:
/* truncated_string */
treehdr truncated_string
empty_rsvmap truncated_string
truncated_string_struct:
beginn ""
propnil truncated_string, good_string
propnil truncated_string, bad_string
endn
fdtlong FDT_END
truncated_string_struct_end:
truncated_string_strings:
string truncated_string, good_string, "good"
truncated_string_bad_string:
.ascii "bad"
/* NOTE: terminating \0 deliberately missing */
truncated_string_strings_end:
truncated_string_end:
/* truncated_memrsv */
treehdr truncated_memrsv
truncated_memrsv_struct:
beginn ""
endn
fdtlong FDT_END
truncated_memrsv_struct_end:
truncated_memrsv_strings:
truncated_memrsv_strings_end:
.balign 8
truncated_memrsv_rsvmap:
rsvmape TEST_ADDR_1H, TEST_ADDR_1L, TEST_SIZE_1H, TEST_SIZE_1L
truncated_memrsv_rsvmap_end:
truncated_memrsv_end:
/* unterminated_memrsv */
treehdr unterminated_memrsv
unterminated_memrsv_rsvmap:
rsvmape TEST_ADDR_1H, TEST_ADDR_1L, TEST_SIZE_1H, TEST_SIZE_1L
unterminated_memrsv_rsvmap_end:
unterminated_memrsv_struct:
beginn ""
endn
fdtlong FDT_END
unterminated_memrsv_struct_end:
unterminated_memrsv_strings:
unterminated_memrsv_strings_end:
unterminated_memrsv_end:
/* two root nodes */
treehdr two_roots
empty_rsvmap two_roots
two_roots_struct:
beginn ""
endn
beginn ""
endn
fdtlong FDT_END
two_roots_struct_end:
two_roots_strings:
two_roots_strings_end:
two_roots_end:
/* root node with a non-empty name */
treehdr named_root
empty_rsvmap named_root
named_root_struct:
beginn "fake"
endn
fdtlong FDT_END
named_root_struct_end:
named_root_strings:
named_root_strings_end:
named_root_end: