1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /*
3  * libfdt - Flat Device Tree manipulation
4  *	Testcase for fdt_node_offset_by_phandle()
5  * Copyright (C) 2006 David Gibson, IBM Corporation.
6  */
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <stdint.h>
11 #include <stdarg.h>
12 
13 #include <libfdt.h>
14 
15 #include "tests.h"
16 #include "testdata.h"
17 
check_search(void * fdt,uint32_t phandle,int target)18 static void check_search(void *fdt, uint32_t phandle, int target)
19 {
20 	int offset;
21 
22 	offset = fdt_node_offset_by_phandle(fdt, phandle);
23 
24 	if (offset != target)
25 		FAIL("fdt_node_offset_by_phandle(0x%x) returns %d "
26 		     "instead of %d", phandle, offset, target);
27 }
28 
main(int argc,char * argv[])29 int main(int argc, char *argv[])
30 {
31 	void *fdt;
32 	int subnode2_offset, subsubnode2_offset;
33 
34 	test_init(argc, argv);
35 	fdt = load_blob_arg(argc, argv);
36 
37 	subnode2_offset = fdt_path_offset(fdt, "/subnode@2");
38 	subsubnode2_offset = fdt_path_offset(fdt, "/subnode@2/subsubnode@0");
39 
40 	if ((subnode2_offset < 0) || (subsubnode2_offset < 0))
41 		FAIL("Can't find required nodes");
42 
43 	check_search(fdt, PHANDLE_1, subnode2_offset);
44 	check_search(fdt, PHANDLE_2, subsubnode2_offset);
45 	check_search(fdt, ~PHANDLE_1, -FDT_ERR_NOTFOUND);
46 	check_search(fdt, 0, -FDT_ERR_BADPHANDLE);
47 	check_search(fdt, -1, -FDT_ERR_BADPHANDLE);
48 
49 	PASS();
50 }
51