1/*
2 * libfdt - Flat Device Tree manipulation
3 *	Testcase for fdt_path_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#include <stdarg.h>
25
26#include <libfdt.h>
27
28#include "tests.h"
29#include "testdata.h"
30
31static void vcheck_search(void *fdt, const char *propname, const void *propval,
32		  int proplen, va_list ap)
33{
34	int offset = -1, target;
35
36	do {
37		target = va_arg(ap, int);
38		verbose_printf("Searching (target = %d): %d ->",
39			       target, offset);
40		offset = fdt_node_offset_by_prop_value(fdt, offset, propname,
41						       propval, proplen);
42		verbose_printf("%d\n", offset);
43
44		if (offset != target)
45			FAIL("fdt_node_offset_by_prop_value() returns %d "
46			     "instead of %d", offset, target);
47	} while (target >= 0);
48}
49
50static void check_search(void *fdt, const char *propname, const void *propval,
51		  int proplen, ...)
52{
53	va_list ap;
54
55	va_start(ap, proplen);
56	vcheck_search(fdt, propname, propval, proplen, ap);
57	va_end(ap);
58}
59
60static void check_search_str(void *fdt, const char *propname,
61			     const char *propval, ...)
62{
63	va_list ap;
64
65	va_start(ap, propval);
66	vcheck_search(fdt, propname, propval, strlen(propval)+1, ap);
67	va_end(ap);
68}
69
70#define check_search_cell(fdt, propname, propval, ...) \
71	{ \
72		fdt32_t val = cpu_to_fdt32(propval); \
73		check_search((fdt), (propname), &val, sizeof(val), \
74			     ##__VA_ARGS__); \
75	}
76
77int main(int argc, char *argv[])
78{
79	void *fdt;
80	int subnode1_offset, subnode2_offset;
81	int subsubnode1_offset, subsubnode2_offset;
82
83	test_init(argc, argv);
84	fdt = load_blob_arg(argc, argv);
85
86	subnode1_offset = fdt_path_offset(fdt, "/subnode@1");
87	subnode2_offset = fdt_path_offset(fdt, "/subnode@2");
88	subsubnode1_offset = fdt_path_offset(fdt, "/subnode@1/subsubnode");
89	subsubnode2_offset = fdt_path_offset(fdt, "/subnode@2/subsubnode@0");
90
91	if ((subnode1_offset < 0) || (subnode2_offset < 0)
92	    || (subsubnode1_offset < 0) || (subsubnode2_offset < 0))
93		FAIL("Can't find required nodes");
94
95	check_search_cell(fdt, "prop-int", TEST_VALUE_1, 0, subnode1_offset,
96			  subsubnode1_offset, -FDT_ERR_NOTFOUND);
97
98	check_search_cell(fdt, "prop-int", TEST_VALUE_2, subnode2_offset,
99			  subsubnode2_offset, -FDT_ERR_NOTFOUND);
100
101	check_search_str(fdt, "prop-str", TEST_STRING_1, 0, -FDT_ERR_NOTFOUND);
102
103	check_search_str(fdt, "prop-str", "no such string", -FDT_ERR_NOTFOUND);
104
105	check_search_cell(fdt, "prop-int", TEST_VALUE_1+1, -FDT_ERR_NOTFOUND);
106
107	check_search(fdt, "no-such-prop", NULL, 0, -FDT_ERR_NOTFOUND);
108
109	PASS();
110}
111