blob: e58c192f7ab140ffcb02bb8f6e4bb4c5f9d465b0 [file] [log] [blame]
David Gibson3da0f9a2006-11-27 16:21:28 +11001/*
2 * libfdt - Flat Device Tree manipulation
3 * Testcase for fdt_subnode_offset()
4 * Copyright (C) 2006 David Gibson, IBM Corporation.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20#include <stdlib.h>
21#include <stdio.h>
22#include <string.h>
David Gibson857f54e2007-03-23 15:16:54 +110023#include <stdint.h>
David Gibson3da0f9a2006-11-27 16:21:28 +110024
25#include <fdt.h>
26#include <libfdt.h>
27
28#include "tests.h"
29#include "testdata.h"
30
David Gibson01a2d8a2008-08-04 15:30:13 +100031static int check_subnode(struct fdt_header *fdt, int parent, const char *name)
David Gibson3da0f9a2006-11-27 16:21:28 +110032{
33 int offset;
David Gibson2cf86932007-11-19 17:26:22 +110034 const struct fdt_node_header *nh;
David Gibson3da0f9a2006-11-27 16:21:28 +110035 uint32_t tag;
36
37 verbose_printf("Checking subnode \"%s\" of %d...", name, parent);
38 offset = fdt_subnode_offset(fdt, parent, name);
39 verbose_printf("offset %d...", offset);
David Gibson9a9fdf52006-12-15 15:12:51 +110040 if (offset < 0)
41 FAIL("fdt_subnode_offset(\"%s\"): %s", name, fdt_strerror(offset));
David Gibson2cf86932007-11-19 17:26:22 +110042 nh = fdt_offset_ptr(fdt, offset, sizeof(*nh));
David Gibson3da0f9a2006-11-27 16:21:28 +110043 verbose_printf("pointer %p\n", nh);
44 if (! nh)
45 FAIL("NULL retrieving subnode \"%s\"", name);
46
47 tag = fdt32_to_cpu(nh->tag);
48
49 if (tag != FDT_BEGIN_NODE)
50 FAIL("Incorrect tag 0x%08x on property \"%s\"", tag, name);
David Gibsond2a9da02007-09-28 15:51:04 +100051 if (!nodename_eq(nh->name, name))
David Gibson3da0f9a2006-11-27 16:21:28 +110052 FAIL("Subnode name mismatch \"%s\" instead of \"%s\"",
53 nh->name, name);
54
55 return offset;
56}
57
58int main(int argc, char *argv[])
59{
David Gibson73d60922006-12-15 15:12:47 +110060 void *fdt;
David Gibson3da0f9a2006-11-27 16:21:28 +110061 int subnode1_offset, subnode2_offset;
David Gibsond2a9da02007-09-28 15:51:04 +100062 int subsubnode1_offset, subsubnode2_offset, subsubnode2_offset2;
David Gibson83df28b2011-09-12 11:18:43 +100063 int ss12_off, ss21_off;
David Gibson3da0f9a2006-11-27 16:21:28 +110064
65 test_init(argc, argv);
David Gibson4e6221c2006-11-28 17:20:01 +110066 fdt = load_blob_arg(argc, argv);
David Gibson3da0f9a2006-11-27 16:21:28 +110067
David Gibsond2a9da02007-09-28 15:51:04 +100068 subnode1_offset = check_subnode(fdt, 0, "subnode@1");
69 subnode2_offset = check_subnode(fdt, 0, "subnode@2");
David Gibson3da0f9a2006-11-27 16:21:28 +110070
71 if (subnode1_offset == subnode2_offset)
72 FAIL("Different subnodes have same offset");
73
David Gibson9521dc52007-11-20 13:35:46 +110074 check_property_cell(fdt, subnode1_offset, "prop-int", TEST_VALUE_1);
75 check_property_cell(fdt, subnode2_offset, "prop-int", TEST_VALUE_2);
David Gibson3da0f9a2006-11-27 16:21:28 +110076
77 subsubnode1_offset = check_subnode(fdt, subnode1_offset, "subsubnode");
David Gibsond2a9da02007-09-28 15:51:04 +100078 subsubnode2_offset = check_subnode(fdt, subnode2_offset, "subsubnode@0");
79 subsubnode2_offset2 = check_subnode(fdt, subnode2_offset, "subsubnode");
David Gibson3da0f9a2006-11-27 16:21:28 +110080
David Gibson9521dc52007-11-20 13:35:46 +110081 check_property_cell(fdt, subsubnode1_offset, "prop-int", TEST_VALUE_1);
82 check_property_cell(fdt, subsubnode2_offset, "prop-int", TEST_VALUE_2);
83 check_property_cell(fdt, subsubnode2_offset2, "prop-int", TEST_VALUE_2);
David Gibsond2a9da02007-09-28 15:51:04 +100084
85 if (subsubnode2_offset != subsubnode2_offset2)
86 FAIL("Different offsets with and without unit address");
David Gibson3da0f9a2006-11-27 16:21:28 +110087
David Gibson83df28b2011-09-12 11:18:43 +100088 check_subnode(fdt, subnode1_offset, "ss1");
David Gibsonf99cd152008-10-30 13:41:08 +110089 ss21_off = fdt_subnode_offset(fdt, subnode2_offset, "ss1");
90 if (ss21_off != -FDT_ERR_NOTFOUND)
91 FAIL("Incorrectly found ss1 in subnode2");
92
93 ss12_off = fdt_subnode_offset(fdt, subnode1_offset, "ss2");
94 if (ss12_off != -FDT_ERR_NOTFOUND)
95 FAIL("Incorrectly found ss2 in subnode1");
David Gibson83df28b2011-09-12 11:18:43 +100096 check_subnode(fdt, subnode2_offset, "ss2");
David Gibsonf99cd152008-10-30 13:41:08 +110097
David Gibson3da0f9a2006-11-27 16:21:28 +110098 PASS();
99}