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