Add qemu 2.4.0
[kvmfornfv.git] / qemu / dtc / tests / subnode_iterate.c
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
34 static void test_node(void *fdt, int parent_offset)
35 {
36         fdt32_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 = cpu_to_fdt32(*prop);
49
50         count = 0;
51         for (offset = fdt_first_subnode(fdt, parent_offset);
52              offset >= 0;
53              offset = fdt_next_subnode(fdt, offset))
54                 count++;
55
56         if (count != subnodes) {
57                 FAIL("Node '%s': Expected %d subnodes, got %d\n",
58                      fdt_get_name(fdt, parent_offset, NULL), subnodes,
59                      count);
60         }
61 }
62
63 static void check_fdt_next_subnode(void *fdt)
64 {
65         int offset;
66         int count = 0;
67
68         for (offset = fdt_first_subnode(fdt, 0);
69              offset >= 0;
70              offset = fdt_next_subnode(fdt, offset)) {
71                 test_node(fdt, offset);
72                 count++;
73         }
74
75         if (count != 2)
76                 FAIL("Expected %d tests, got %d\n", 2, count);
77 }
78
79 int main(int argc, char *argv[])
80 {
81         void *fdt;
82
83         test_init(argc, argv);
84         if (argc != 2)
85                 CONFIG("Usage: %s <dtb file>", argv[0]);
86
87         fdt = load_blob(argv[1]);
88         if (!fdt)
89                 FAIL("No device tree available");
90
91         check_fdt_next_subnode(fdt);
92
93         PASS();
94 }