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