subnode_offset.c revision 1.1.1.2
1/*	$NetBSD: subnode_offset.c,v 1.1.1.2 2017/06/08 15:59:27 skrll Exp $	*/
2
3/*
4 * libfdt - Flat Device Tree manipulation
5 *	Testcase for fdt_subnode_offset()
6 * Copyright (C) 2006 David Gibson, IBM Corporation.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 2.1 of
11 * the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22#include <stdlib.h>
23#include <stdio.h>
24#include <string.h>
25#include <stdint.h>
26
27#include <libfdt.h>
28
29#include "tests.h"
30#include "testdata.h"
31
32static int check_subnode(struct fdt_header *fdt, int parent, const char *name)
33{
34	int offset;
35	const struct fdt_node_header *nh;
36	uint32_t tag;
37
38	verbose_printf("Checking subnode \"%s\" of %d...", name, parent);
39	offset = fdt_subnode_offset(fdt, parent, name);
40	verbose_printf("offset %d...", offset);
41	if (offset < 0)
42		FAIL("fdt_subnode_offset(\"%s\"): %s", name, fdt_strerror(offset));
43	nh = fdt_offset_ptr(fdt, offset, sizeof(*nh));
44	verbose_printf("pointer %p\n", nh);
45	if (! nh)
46		FAIL("NULL retrieving subnode \"%s\"", name);
47
48	tag = fdt32_to_cpu(nh->tag);
49
50	if (tag != FDT_BEGIN_NODE)
51		FAIL("Incorrect tag 0x%08x on property \"%s\"", tag, name);
52	if (!nodename_eq(nh->name, name))
53		FAIL("Subnode name mismatch \"%s\" instead of \"%s\"",
54		     nh->name, name);
55
56	return offset;
57}
58
59int main(int argc, char *argv[])
60{
61	void *fdt;
62	int subnode1_offset, subnode2_offset;
63	int subsubnode1_offset, subsubnode2_offset, subsubnode2_offset2;
64	int ss12_off, ss21_off;
65
66	test_init(argc, argv);
67	fdt = load_blob_arg(argc, argv);
68
69	subnode1_offset = check_subnode(fdt, 0, "subnode@1");
70	subnode2_offset = check_subnode(fdt, 0, "subnode@2");
71
72	if (subnode1_offset == subnode2_offset)
73		FAIL("Different subnodes have same offset");
74
75	check_property_cell(fdt, subnode1_offset, "prop-int", TEST_VALUE_1);
76	check_property_cell(fdt, subnode2_offset, "prop-int", TEST_VALUE_2);
77
78	subsubnode1_offset = check_subnode(fdt, subnode1_offset, "subsubnode");
79	subsubnode2_offset = check_subnode(fdt, subnode2_offset, "subsubnode@0");
80	subsubnode2_offset2 = check_subnode(fdt, subnode2_offset, "subsubnode");
81
82	check_property_cell(fdt, subsubnode1_offset, "prop-int", TEST_VALUE_1);
83	check_property_cell(fdt, subsubnode2_offset, "prop-int", TEST_VALUE_2);
84	check_property_cell(fdt, subsubnode2_offset2, "prop-int", TEST_VALUE_2);
85
86	if (subsubnode2_offset != subsubnode2_offset2)
87		FAIL("Different offsets with and without unit address");
88
89	check_subnode(fdt, subnode1_offset, "ss1");
90	ss21_off = fdt_subnode_offset(fdt, subnode2_offset, "ss1");
91	if (ss21_off != -FDT_ERR_NOTFOUND)
92		FAIL("Incorrectly found ss1 in subnode2");
93
94	ss12_off = fdt_subnode_offset(fdt, subnode1_offset, "ss2");
95	if (ss12_off != -FDT_ERR_NOTFOUND)
96		FAIL("Incorrectly found ss2 in subnode1");
97	check_subnode(fdt, subnode2_offset, "ss2");
98
99	PASS();
100}
101