test: cmd: fdt: Test fdt get addr

Add 'fdt get addr' test which works as follows:
- Create fuller FDT, map it to sysmem
- Get address of various properties
- Compare addresses calculated by UT and fdt command

This test is special in that it has to go through gruesome remapping scheme
where the test calculates:
- pointer offsets of the generated FDT root and the property being tested
- map_sysmem() result of environment variable "fdtaddr" and the one set
  by the test matching address of property being tested
- difference between the later and the former, to obtain offset of the
  DT property from start of DT
The offsets must match in both the UT and the tested U-Boot, if they do
not, the test fails.

The test case can be triggered using:
"
./u-boot -Dc 'ut fdt'
"
To dump the full output from commands used during test, add '-v' flag.

Signed-off-by: Marek Vasut <marek.vasut+renesas@mailbox.org>
Reviewed-by: Simon Glass <sjg@chromium.org>
diff --git a/test/cmd/fdt.c b/test/cmd/fdt.c
index 6af8cfe..ac2a1c7 100644
--- a/test/cmd/fdt.c
+++ b/test/cmd/fdt.c
@@ -481,6 +481,73 @@
 }
 FDT_TEST(fdt_test_get_name, UT_TESTF_CONSOLE_REC);
 
+static int fdt_test_get_addr_common(struct unit_test_state *uts, char *fdt,
+				    const char *path, const char *prop)
+{
+	unsigned int offset;
+	int path_offset;
+	void *prop_ptr;
+	int len = 0;
+
+	path_offset = fdt_path_offset(fdt, path);
+	ut_assert(path_offset >= 0);
+	prop_ptr = (void *)fdt_getprop(fdt, path_offset, prop, &len);
+	ut_assertnonnull(prop_ptr);
+	offset = (char *)prop_ptr - fdt;
+
+	ut_assertok(console_record_reset_enable());
+	ut_assertok(run_commandf("fdt get addr pstr %s %s", path, prop));
+	ut_asserteq((ulong)map_sysmem(env_get_hex("fdtaddr", 0x1234), 0),
+		    (ulong)(map_sysmem(env_get_hex("pstr", 0x1234), 0) - offset));
+	ut_assertok(ut_check_console_end(uts));
+
+	return 0;
+}
+
+static int fdt_test_get_addr(struct unit_test_state *uts)
+{
+	char fdt[4096];
+	ulong addr;
+
+	ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt)));
+	addr = map_to_sysmem(fdt);
+	set_working_fdt_addr(addr);
+
+	/* Test getting address of root node / string property "compatible" */
+	fdt_test_get_addr_common(uts, fdt, "/", "compatible");
+
+	/* Test getting address of node /test-node@1234 stringlist property "clock-names" */
+	fdt_test_get_addr_common(uts, fdt, "/test-node@1234", "clock-names");
+	fdt_test_get_addr_common(uts, fdt, "testnodealias", "clock-names");
+
+	/* Test getting address of node /test-node@1234 u32 property "clock-frequency" */
+	fdt_test_get_addr_common(uts, fdt, "/test-node@1234", "clock-frequency");
+	fdt_test_get_addr_common(uts, fdt, "testnodealias", "clock-frequency");
+
+	/* Test getting address of node /test-node@1234 empty property "u-boot,empty-property" */
+	fdt_test_get_addr_common(uts, fdt, "/test-node@1234", "u-boot,empty-property");
+	fdt_test_get_addr_common(uts, fdt, "testnodealias", "u-boot,empty-property");
+
+	/* Test getting address of node /test-node@1234 array property "regs" */
+	fdt_test_get_addr_common(uts, fdt, "/test-node@1234", "regs");
+	fdt_test_get_addr_common(uts, fdt, "testnodealias", "regs");
+
+	/* Test getting address of node /test-node@1234/subnode non-existent property "noprop" */
+	ut_assertok(console_record_reset_enable());
+	ut_asserteq(1, run_command("fdt get addr pnoprop /test-node@1234/subnode noprop", 1));
+	ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
+	ut_assertok(ut_check_console_end(uts));
+
+	/* Test getting address of non-existent node /test-node@1234/nonode@1 property "noprop" */
+	ut_assertok(console_record_reset_enable());
+	ut_asserteq(1, run_command("fdt get addr pnonode /test-node@1234/nonode@1 noprop", 1));
+	ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
+	ut_assertok(ut_check_console_end(uts));
+
+	return 0;
+}
+FDT_TEST(fdt_test_get_addr, UT_TESTF_CONSOLE_REC);
+
 int do_ut_fdt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 {
 	struct unit_test *tests = UNIT_TEST_SUITE_START(fdt_test);