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