1/*
2 * libfdt - Flat Device Tree manipulation
3 *	Testcase for string references in dtc
4 * Copyright (C) 2006 David Gibson, IBM Corporation.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20#include <stdlib.h>
21#include <stdio.h>
22#include <string.h>
23#include <stdint.h>
24
25#include <libfdt.h>
26
27#include "tests.h"
28#include "testdata.h"
29
30static void check_ref(const void *fdt, int node, const char *checkpath)
31{
32	const char *p;
33	int len;
34
35	p = fdt_getprop(fdt, node, "ref", &len);
36	if (!p)
37		FAIL("fdt_getprop(%d, \"ref\"): %s", node, fdt_strerror(len));
38	if (!streq(p, checkpath))
39		FAIL("'ref' in node at %d has value \"%s\" instead of \"%s\"",
40		     node, p, checkpath);
41
42	p = fdt_getprop(fdt, node, "lref", &len);
43	if (!p)
44		FAIL("fdt_getprop(%d, \"lref\"): %s", node, fdt_strerror(len));
45	if (!streq(p, checkpath))
46		FAIL("'lref' in node at %d has value \"%s\" instead of \"%s\"",
47		     node, p, checkpath);
48}
49
50static void check_rref(const void *fdt)
51{
52	const char *p;
53	int len;
54
55	/* Check reference to root node */
56	p = fdt_getprop(fdt, 0, "rref", &len);
57	if (!p)
58		FAIL("fdt_getprop(0, \"rref\"): %s", fdt_strerror(len));
59	if (!streq(p, "/"))
60		FAIL("'rref' in root node has value \"%s\" instead of \"/\"",
61		     p);
62}
63
64int main(int argc, char *argv[])
65{
66	void *fdt;
67	const char *p;
68	int len, multilen;
69	int n1, n2, n3, n4;
70
71	test_init(argc, argv);
72	fdt = load_blob_arg(argc, argv);
73
74	n1 = fdt_path_offset(fdt, "/node1");
75	if (n1 < 0)
76		FAIL("fdt_path_offset(/node1): %s", fdt_strerror(n1));
77	n2 = fdt_path_offset(fdt, "/node2");
78	if (n2 < 0)
79		FAIL("fdt_path_offset(/node2): %s", fdt_strerror(n2));
80
81	check_ref(fdt, n1, "/node2");
82	check_ref(fdt, n2, "/node1");
83
84	/* Check multiple reference */
85	multilen = strlen("/node1") + strlen("/node2") + 2;
86	p = fdt_getprop(fdt, 0, "multiref", &len);
87	if (!p)
88		FAIL("fdt_getprop(0, \"multiref\"): %s", fdt_strerror(len));
89	if (len != multilen)
90		FAIL("multiref has wrong length, %d instead of %d",
91		     len, multilen);
92	if ((!streq(p, "/node1") || !streq(p + strlen("/node1") + 1, "/node2")))
93		FAIL("multiref has wrong value");
94
95	/* Check reference to nested nodes with common prefix */
96	n3 = fdt_path_offset(fdt, "/foo/baz");
97	if (n3 < 0)
98		FAIL("fdt_path_offset(/foo/baz): %s", fdt_strerror(n3));
99	n4 = fdt_path_offset(fdt, "/foobar/baz");
100	if (n4 < 0)
101		FAIL("fdt_path_offset(/foobar/baz): %s", fdt_strerror(n4));
102	check_ref(fdt, n3, "/foobar/baz");
103	check_ref(fdt, n4, "/foo/baz");
104
105	check_rref(fdt);
106
107	PASS();
108}
109