1/*
2 * libfdt - Flat Device Tree manipulation
3 *	Testcase for fdt_subnode_offset()
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 int check_subnode(struct fdt_header *fdt, int parent, const char *name)
31{
32	int offset;
33	const struct fdt_node_header *nh;
34	uint32_t tag;
35
36	verbose_printf("Checking subnode \"%s\" of %d...", name, parent);
37	offset = fdt_subnode_offset(fdt, parent, name);
38	verbose_printf("offset %d...", offset);
39	if (offset < 0)
40		FAIL("fdt_subnode_offset(\"%s\"): %s", name, fdt_strerror(offset));
41	nh = fdt_offset_ptr(fdt, offset, sizeof(*nh));
42	verbose_printf("pointer %p\n", nh);
43	if (! nh)
44		FAIL("NULL retrieving subnode \"%s\"", name);
45
46	tag = fdt32_to_cpu(nh->tag);
47
48	if (tag != FDT_BEGIN_NODE)
49		FAIL("Incorrect tag 0x%08x on property \"%s\"", tag, name);
50	if (!nodename_eq(nh->name, name))
51		FAIL("Subnode name mismatch \"%s\" instead of \"%s\"",
52		     nh->name, name);
53
54	return offset;
55}
56
57int main(int argc, char *argv[])
58{
59	void *fdt;
60	int subnode1_offset, subnode2_offset;
61	int subsubnode1_offset, subsubnode2_offset, subsubnode2_offset2;
62	int ss12_off, ss21_off;
63
64	test_init(argc, argv);
65	fdt = load_blob_arg(argc, argv);
66
67	subnode1_offset = check_subnode(fdt, 0, "subnode@1");
68	subnode2_offset = check_subnode(fdt, 0, "subnode@2");
69
70	if (subnode1_offset == subnode2_offset)
71		FAIL("Different subnodes have same offset");
72
73	check_property_cell(fdt, subnode1_offset, "prop-int", TEST_VALUE_1);
74	check_property_cell(fdt, subnode2_offset, "prop-int", TEST_VALUE_2);
75
76	subsubnode1_offset = check_subnode(fdt, subnode1_offset, "subsubnode");
77	subsubnode2_offset = check_subnode(fdt, subnode2_offset, "subsubnode@0");
78	subsubnode2_offset2 = check_subnode(fdt, subnode2_offset, "subsubnode");
79
80	check_property_cell(fdt, subsubnode1_offset, "prop-int", TEST_VALUE_1);
81	check_property_cell(fdt, subsubnode2_offset, "prop-int", TEST_VALUE_2);
82	check_property_cell(fdt, subsubnode2_offset2, "prop-int", TEST_VALUE_2);
83
84	if (subsubnode2_offset != subsubnode2_offset2)
85		FAIL("Different offsets with and without unit address");
86
87	check_subnode(fdt, subnode1_offset, "ss1");
88	ss21_off = fdt_subnode_offset(fdt, subnode2_offset, "ss1");
89	if (ss21_off != -FDT_ERR_NOTFOUND)
90		FAIL("Incorrectly found ss1 in subnode2");
91
92	ss12_off = fdt_subnode_offset(fdt, subnode1_offset, "ss2");
93	if (ss12_off != -FDT_ERR_NOTFOUND)
94		FAIL("Incorrectly found ss2 in subnode1");
95	check_subnode(fdt, subnode2_offset, "ss2");
96
97	PASS();
98}
99