2 * libfdt - Flat Device Tree manipulation
3 * Tests if two given dtbs are structurally equal (including order)
4 * Copyright (C) 2007 David Gibson, IBM Corporation.
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.
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.
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
32 int notequal; /* = 0 */
34 #define MISMATCH(fmt, ...) \
39 FAIL(fmt, ##__VA_ARGS__); \
47 FAIL("Trees match which shouldn't"); \
54 FAIL(#code ": %s", fdt_strerror(err)); \
57 static int mem_rsv_cmp(const void *p1, const void *p2)
59 const struct fdt_reserve_entry *re1 = p1;
60 const struct fdt_reserve_entry *re2 = p2;
62 if (re1->address < re2->address)
64 else if (re1->address > re2->address)
67 if (re1->size < re2->size)
69 else if (re1->size > re2->size)
75 static void compare_mem_rsv(void *fdt1, void *fdt2)
78 uint64_t addr1, size1, addr2, size2;
81 if (fdt_num_mem_rsv(fdt1) != fdt_num_mem_rsv(fdt2))
82 MISMATCH("Trees have different number of reserve entries");
84 qsort((char *)fdt1 + fdt_off_mem_rsvmap(fdt1), fdt_num_mem_rsv(fdt1),
85 sizeof(struct fdt_reserve_entry), mem_rsv_cmp);
86 qsort((char *)fdt2 + fdt_off_mem_rsvmap(fdt2), fdt_num_mem_rsv(fdt2),
87 sizeof(struct fdt_reserve_entry), mem_rsv_cmp);
89 for (i = 0; i < fdt_num_mem_rsv(fdt1); i++) {
90 CHECK(fdt_get_mem_rsv(fdt1, i, &addr1, &size1));
91 CHECK(fdt_get_mem_rsv(fdt2, i, &addr2, &size2));
93 if ((addr1 != addr2) || (size1 != size2))
94 MISMATCH("Mismatch in reserve entry %d: "
95 "(0x%llx, 0x%llx) != (0x%llx, 0x%llx)", i,
96 (unsigned long long)addr1,
97 (unsigned long long)size1,
98 (unsigned long long)addr2,
99 (unsigned long long)size2);
103 static void compare_properties(const void *fdt1, int offset1,
104 const void *fdt2, int offset2)
106 int offset = offset1;
108 /* Check the properties */
109 for (offset = fdt_first_property_offset(fdt1, offset1);
111 offset = fdt_next_property_offset(fdt1, offset)) {
114 const void *data1, *data2;
117 data1 = fdt_getprop_by_offset(fdt1, offset, &name, &len1);
119 FAIL("fdt_getprop_by_offset(): %s\n",
122 verbose_printf("Property '%s'\n", name);
124 data2 = fdt_getprop(fdt2, offset2, name, &len2);
126 if (len2 == -FDT_ERR_NOTFOUND)
127 MISMATCH("Property '%s' missing\n", name);
129 FAIL("fdt_get_property(): %s\n",
133 verbose_printf("len1=%d data1=", len1);
134 for (i = 0; i < len1; i++)
135 verbose_printf(" %02x", ((const char *)data1)[i]);
136 verbose_printf("\nlen2=%d data2=", len2);
137 for (i = 0; i < len1; i++)
138 verbose_printf(" %02x", ((const char *)data2)[i]);
139 verbose_printf("\n");
142 MISMATCH("Property '%s' mismatched length %d vs. %d\n",
144 else if (memcmp(data1, data2, len1) != 0)
145 MISMATCH("Property '%s' mismatched value\n", name);
149 static void compare_node(const void *fdt1, int offset1,
150 const void *fdt2, int offset2);
152 static void compare_subnodes(const void *fdt1, int offset1,
153 const void *fdt2, int offset2,
156 int coffset1, coffset2, depth;
158 for (depth = 0, coffset1 = offset1;
159 (coffset1 >= 0) && (depth >= 0);
160 coffset1 = fdt_next_node(fdt1, coffset1, &depth))
162 const char *name = fdt_get_name(fdt1, coffset1, NULL);
164 verbose_printf("Subnode %s\n", name);
165 coffset2 = fdt_subnode_offset(fdt2, offset2, name);
166 if (coffset2 == -FDT_ERR_NOTFOUND)
167 MISMATCH("Subnode %s missing\n", name);
168 else if (coffset2 < 0)
169 FAIL("fdt_subnode_offset(): %s\n",
170 fdt_strerror(coffset2));
173 compare_node(fdt1, coffset1, fdt2, coffset2);
177 static void compare_node(const void *fdt1, int offset1,
178 const void *fdt2, int offset2)
181 char path1[PATH_MAX], path2[PATH_MAX];
183 CHECK(fdt_get_path(fdt1, offset1, path1, sizeof(path1)));
184 CHECK(fdt_get_path(fdt2, offset2, path2, sizeof(path2)));
186 if (!streq(path1, path2))
187 TEST_BUG("Path mismatch %s vs. %s\n", path1, path2);
189 verbose_printf("Checking %s\n", path1);
191 compare_properties(fdt1, offset1, fdt2, offset2);
192 compare_properties(fdt2, offset2, fdt1, offset1);
194 compare_subnodes(fdt1, offset1, fdt2, offset2, 1);
195 compare_subnodes(fdt2, offset2, fdt1, offset1, 0);
198 int main(int argc, char *argv[])
201 uint32_t cpuid1, cpuid2;
203 test_init(argc, argv);
205 && ((argc != 4) || !streq(argv[1], "-n")))
206 CONFIG("Usage: %s [-n] <dtb file> <dtb file>", argv[0]);
210 fdt1 = load_blob(argv[argc-2]);
211 fdt2 = load_blob(argv[argc-1]);
213 compare_mem_rsv(fdt1, fdt2);
214 compare_node(fdt1, 0, fdt2, 0);
216 cpuid1 = fdt_boot_cpuid_phys(fdt1);
217 cpuid2 = fdt_boot_cpuid_phys(fdt2);
218 if (cpuid1 != cpuid2)
219 MISMATCH("boot_cpuid_phys mismatch 0x%x != 0x%x",