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