1*cd60bc56SAndroid Build Coastguard Worker // SPDX-License-Identifier: LGPL-2.1-or-later
2*cd60bc56SAndroid Build Coastguard Worker /*
3*cd60bc56SAndroid Build Coastguard Worker * libfdt - Flat Device Tree manipulation
4*cd60bc56SAndroid Build Coastguard Worker * Testcase for fdt_subnode_offset()
5*cd60bc56SAndroid Build Coastguard Worker * Copyright (C) 2006 David Gibson, IBM Corporation.
6*cd60bc56SAndroid Build Coastguard Worker */
7*cd60bc56SAndroid Build Coastguard Worker #include <stdlib.h>
8*cd60bc56SAndroid Build Coastguard Worker #include <stdio.h>
9*cd60bc56SAndroid Build Coastguard Worker #include <string.h>
10*cd60bc56SAndroid Build Coastguard Worker #include <stdint.h>
11*cd60bc56SAndroid Build Coastguard Worker
12*cd60bc56SAndroid Build Coastguard Worker #include <libfdt.h>
13*cd60bc56SAndroid Build Coastguard Worker
14*cd60bc56SAndroid Build Coastguard Worker #include "tests.h"
15*cd60bc56SAndroid Build Coastguard Worker #include "testdata.h"
16*cd60bc56SAndroid Build Coastguard Worker
check_subnode(struct fdt_header * fdt,int parent,const char * name)17*cd60bc56SAndroid Build Coastguard Worker static int check_subnode(struct fdt_header *fdt, int parent, const char *name)
18*cd60bc56SAndroid Build Coastguard Worker {
19*cd60bc56SAndroid Build Coastguard Worker int offset;
20*cd60bc56SAndroid Build Coastguard Worker const struct fdt_node_header *nh;
21*cd60bc56SAndroid Build Coastguard Worker uint32_t tag;
22*cd60bc56SAndroid Build Coastguard Worker
23*cd60bc56SAndroid Build Coastguard Worker verbose_printf("Checking subnode \"%s\" of %d...", name, parent);
24*cd60bc56SAndroid Build Coastguard Worker offset = fdt_subnode_offset(fdt, parent, name);
25*cd60bc56SAndroid Build Coastguard Worker verbose_printf("offset %d...", offset);
26*cd60bc56SAndroid Build Coastguard Worker if (offset < 0)
27*cd60bc56SAndroid Build Coastguard Worker FAIL("fdt_subnode_offset(\"%s\"): %s", name, fdt_strerror(offset));
28*cd60bc56SAndroid Build Coastguard Worker nh = fdt_offset_ptr(fdt, offset, sizeof(*nh));
29*cd60bc56SAndroid Build Coastguard Worker verbose_printf("pointer %p\n", nh);
30*cd60bc56SAndroid Build Coastguard Worker if (! nh)
31*cd60bc56SAndroid Build Coastguard Worker FAIL("NULL retrieving subnode \"%s\"", name);
32*cd60bc56SAndroid Build Coastguard Worker
33*cd60bc56SAndroid Build Coastguard Worker tag = fdt32_to_cpu(nh->tag);
34*cd60bc56SAndroid Build Coastguard Worker
35*cd60bc56SAndroid Build Coastguard Worker if (tag != FDT_BEGIN_NODE)
36*cd60bc56SAndroid Build Coastguard Worker FAIL("Incorrect tag 0x%08x on property \"%s\"", tag, name);
37*cd60bc56SAndroid Build Coastguard Worker if (!nodename_eq(nh->name, name))
38*cd60bc56SAndroid Build Coastguard Worker FAIL("Subnode name mismatch \"%s\" instead of \"%s\"",
39*cd60bc56SAndroid Build Coastguard Worker nh->name, name);
40*cd60bc56SAndroid Build Coastguard Worker
41*cd60bc56SAndroid Build Coastguard Worker return offset;
42*cd60bc56SAndroid Build Coastguard Worker }
43*cd60bc56SAndroid Build Coastguard Worker
main(int argc,char * argv[])44*cd60bc56SAndroid Build Coastguard Worker int main(int argc, char *argv[])
45*cd60bc56SAndroid Build Coastguard Worker {
46*cd60bc56SAndroid Build Coastguard Worker void *fdt;
47*cd60bc56SAndroid Build Coastguard Worker int subnode1_offset, subnode2_offset;
48*cd60bc56SAndroid Build Coastguard Worker int subsubnode1_offset, subsubnode2_offset, subsubnode2_offset2;
49*cd60bc56SAndroid Build Coastguard Worker int ss12_off, ss21_off;
50*cd60bc56SAndroid Build Coastguard Worker
51*cd60bc56SAndroid Build Coastguard Worker test_init(argc, argv);
52*cd60bc56SAndroid Build Coastguard Worker fdt = load_blob_arg(argc, argv);
53*cd60bc56SAndroid Build Coastguard Worker
54*cd60bc56SAndroid Build Coastguard Worker subnode1_offset = check_subnode(fdt, 0, "subnode@1");
55*cd60bc56SAndroid Build Coastguard Worker subnode2_offset = check_subnode(fdt, 0, "subnode@2");
56*cd60bc56SAndroid Build Coastguard Worker
57*cd60bc56SAndroid Build Coastguard Worker if (subnode1_offset == subnode2_offset)
58*cd60bc56SAndroid Build Coastguard Worker FAIL("Different subnodes have same offset");
59*cd60bc56SAndroid Build Coastguard Worker
60*cd60bc56SAndroid Build Coastguard Worker check_property_cell(fdt, subnode1_offset, "prop-int", TEST_VALUE_1);
61*cd60bc56SAndroid Build Coastguard Worker check_property_cell(fdt, subnode2_offset, "prop-int", TEST_VALUE_2);
62*cd60bc56SAndroid Build Coastguard Worker
63*cd60bc56SAndroid Build Coastguard Worker subsubnode1_offset = check_subnode(fdt, subnode1_offset, "subsubnode");
64*cd60bc56SAndroid Build Coastguard Worker subsubnode2_offset = check_subnode(fdt, subnode2_offset, "subsubnode@0");
65*cd60bc56SAndroid Build Coastguard Worker subsubnode2_offset2 = check_subnode(fdt, subnode2_offset, "subsubnode");
66*cd60bc56SAndroid Build Coastguard Worker
67*cd60bc56SAndroid Build Coastguard Worker check_property_cell(fdt, subsubnode1_offset, "prop-int", TEST_VALUE_1);
68*cd60bc56SAndroid Build Coastguard Worker check_property_cell(fdt, subsubnode2_offset, "prop-int", TEST_VALUE_2);
69*cd60bc56SAndroid Build Coastguard Worker check_property_cell(fdt, subsubnode2_offset2, "prop-int", TEST_VALUE_2);
70*cd60bc56SAndroid Build Coastguard Worker
71*cd60bc56SAndroid Build Coastguard Worker if (subsubnode2_offset != subsubnode2_offset2)
72*cd60bc56SAndroid Build Coastguard Worker FAIL("Different offsets with and without unit address");
73*cd60bc56SAndroid Build Coastguard Worker
74*cd60bc56SAndroid Build Coastguard Worker check_subnode(fdt, subnode1_offset, "ss1");
75*cd60bc56SAndroid Build Coastguard Worker ss21_off = fdt_subnode_offset(fdt, subnode2_offset, "ss1");
76*cd60bc56SAndroid Build Coastguard Worker if (ss21_off != -FDT_ERR_NOTFOUND)
77*cd60bc56SAndroid Build Coastguard Worker FAIL("Incorrectly found ss1 in subnode2");
78*cd60bc56SAndroid Build Coastguard Worker
79*cd60bc56SAndroid Build Coastguard Worker ss12_off = fdt_subnode_offset(fdt, subnode1_offset, "ss2");
80*cd60bc56SAndroid Build Coastguard Worker if (ss12_off != -FDT_ERR_NOTFOUND)
81*cd60bc56SAndroid Build Coastguard Worker FAIL("Incorrectly found ss2 in subnode1");
82*cd60bc56SAndroid Build Coastguard Worker check_subnode(fdt, subnode2_offset, "ss2");
83*cd60bc56SAndroid Build Coastguard Worker
84*cd60bc56SAndroid Build Coastguard Worker PASS();
85*cd60bc56SAndroid Build Coastguard Worker }
86