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 properties;
24 const fdt32_t *prop;
25 int offset, property;
26 unsigned int count;
27 int len;
28
29 /*
30 * This property indicates the number of properties in our
31 * test node to expect
32 */
33 prop = fdt_getprop(fdt, parent_offset, "test-properties", &len);
34 if (!prop || len != sizeof(fdt32_t)) {
35 FAIL("Missing/invalid test-properties property at '%s'",
36 fdt_get_name(fdt, parent_offset, NULL));
37 }
38 properties = fdt32_to_cpu(*prop);
39
40 count = 0;
41 offset = fdt_first_subnode(fdt, parent_offset);
42 if (offset < 0)
43 FAIL("Missing test node\n");
44
45 fdt_for_each_property_offset(property, fdt, offset)
46 count++;
47
48 if (count != properties) {
49 FAIL("Node '%s': Expected %d properties, got %d\n",
50 fdt_get_name(fdt, parent_offset, NULL), properties,
51 count);
52 }
53 }
54
check_fdt_next_subnode(void * fdt)55 static void check_fdt_next_subnode(void *fdt)
56 {
57 int offset;
58 int count = 0;
59
60 fdt_for_each_subnode(offset, fdt, 0) {
61 test_node(fdt, offset);
62 count++;
63 }
64
65 if (count != 2)
66 FAIL("Expected %d tests, got %d\n", 2, count);
67 }
68
main(int argc,char * argv[])69 int main(int argc, char *argv[])
70 {
71 void *fdt;
72
73 test_init(argc, argv);
74 if (argc != 2)
75 CONFIG("Usage: %s <dtb file>", argv[0]);
76
77 fdt = load_blob(argv[1]);
78 if (!fdt)
79 FAIL("No device tree available");
80
81 check_fdt_next_subnode(fdt);
82
83 PASS();
84 }
85