1 /*
2 * libfdt - Flat Device Tree manipulation
3 * Tests that fdt_next_subnode() works as expected
4 *
5 * Copyright (C) 2013 Google, Inc
6 *
7 * Copyright (C) 2007 David Gibson, IBM Corporation.
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1 of
12 * the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdint.h>
28
29 #include <libfdt.h>
30
31 #include "tests.h"
32 #include "testdata.h"
33
test_node(void * fdt,int parent_offset)34 static void test_node(void *fdt, int parent_offset)
35 {
36 uint32_t subnodes;
37 const fdt32_t *prop;
38 int offset;
39 int count;
40 int len;
41
42 /* This property indicates the number of subnodes to expect */
43 prop = fdt_getprop(fdt, parent_offset, "subnodes", &len);
44 if (!prop || len != sizeof(fdt32_t)) {
45 FAIL("Missing/invalid subnodes property at '%s'",
46 fdt_get_name(fdt, parent_offset, NULL));
47 }
48 subnodes = fdt32_to_cpu(*prop);
49
50 count = 0;
51 fdt_for_each_subnode(offset, fdt, parent_offset)
52 count++;
53
54 if (count != subnodes) {
55 FAIL("Node '%s': Expected %d subnodes, got %d\n",
56 fdt_get_name(fdt, parent_offset, NULL), subnodes,
57 count);
58 }
59 }
60
check_fdt_next_subnode(void * fdt)61 static void check_fdt_next_subnode(void *fdt)
62 {
63 int offset;
64 int count = 0;
65
66 fdt_for_each_subnode(offset, fdt, 0) {
67 test_node(fdt, offset);
68 count++;
69 }
70
71 if (count != 2)
72 FAIL("Expected %d tests, got %d\n", 2, count);
73 }
74
main(int argc,char * argv[])75 int main(int argc, char *argv[])
76 {
77 void *fdt;
78
79 test_init(argc, argv);
80 if (argc != 2)
81 CONFIG("Usage: %s <dtb file>", argv[0]);
82
83 fdt = load_blob(argv[1]);
84 if (!fdt)
85 FAIL("No device tree available");
86
87 check_fdt_next_subnode(fdt);
88
89 PASS();
90 }
91