xref: /netbsd-src/external/gpl2/dtc/dist/tests/subnode_iterate.c (revision cc7d2833ecf67da5a5ddc470841931eb9f6723e4)
1 /*	$NetBSD: subnode_iterate.c,v 1.1.1.3 2019/12/22 12:34:06 skrll Exp $	*/
2 
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 /*
5  * libfdt - Flat Device Tree manipulation
6  *	Tests that fdt_next_subnode() works as expected
7  *
8  * Copyright (C) 2013 Google, Inc
9  *
10  * Copyright (C) 2007 David Gibson, IBM Corporation.
11  */
12 
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <stdint.h>
17 
18 #include <libfdt.h>
19 
20 #include "tests.h"
21 #include "testdata.h"
22 
test_node(void * fdt,int parent_offset)23 static void test_node(void *fdt, int parent_offset)
24 {
25 	uint32_t subnodes;
26 	const fdt32_t *prop;
27 	int offset;
28 	int count;
29 	int len;
30 
31 	/* This property indicates the number of subnodes to expect */
32 	prop = fdt_getprop(fdt, parent_offset, "subnodes", &len);
33 	if (!prop || len != sizeof(fdt32_t)) {
34 		FAIL("Missing/invalid subnodes property at '%s'",
35 		     fdt_get_name(fdt, parent_offset, NULL));
36 	}
37 	subnodes = fdt32_to_cpu(*prop);
38 
39 	count = 0;
40 	fdt_for_each_subnode(offset, fdt, parent_offset)
41 		count++;
42 
43 	if (count != subnodes) {
44 		FAIL("Node '%s': Expected %d subnodes, got %d\n",
45 		     fdt_get_name(fdt, parent_offset, NULL), subnodes,
46 		     count);
47 	}
48 }
49 
check_fdt_next_subnode(void * fdt)50 static void check_fdt_next_subnode(void *fdt)
51 {
52 	int offset;
53 	int count = 0;
54 
55 	fdt_for_each_subnode(offset, fdt, 0) {
56 		test_node(fdt, offset);
57 		count++;
58 	}
59 
60 	if (count != 2)
61 		FAIL("Expected %d tests, got %d\n", 2, count);
62 }
63 
main(int argc,char * argv[])64 int main(int argc, char *argv[])
65 {
66 	void *fdt;
67 
68 	test_init(argc, argv);
69 	if (argc != 2)
70 		CONFIG("Usage: %s <dtb file>", argv[0]);
71 
72 	fdt = load_blob(argv[1]);
73 	if (!fdt)
74 		FAIL("No device tree available");
75 
76 	check_fdt_next_subnode(fdt);
77 
78 	PASS();
79 }
80