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