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
31 int notequal; /* = 0 */
33 #define MISMATCH(fmt, ...) \
38 FAIL(fmt, ##__VA_ARGS__); \
46 FAIL("Trees match which shouldn't"); \
53 FAIL(#code ": %s", fdt_strerror(err)); \
56 static void compare_mem_rsv(const void *fdt1, const void *fdt2)
59 uint64_t addr1, size1, addr2, size2;
62 if (fdt_num_mem_rsv(fdt1) != fdt_num_mem_rsv(fdt2))
63 MISMATCH("Trees have different number of reserve entries");
64 for (i = 0; i < fdt_num_mem_rsv(fdt1); i++) {
65 CHECK(fdt_get_mem_rsv(fdt1, i, &addr1, &size1));
66 CHECK(fdt_get_mem_rsv(fdt2, i, &addr2, &size2));
68 if ((addr1 != addr2) || (size1 != size2))
69 MISMATCH("Mismatch in reserve entry %d: "
70 "(0x%llx, 0x%llx) != (0x%llx, 0x%llx)", i,
71 (unsigned long long)addr1,
72 (unsigned long long)size1,
73 (unsigned long long)addr2,
74 (unsigned long long)size2);
78 static void compare_structure(const void *fdt1, const void *fdt2)
80 int nextoffset1 = 0, nextoffset2 = 0;
83 const char *name1, *name2;
85 const struct fdt_property *prop1, *prop2;
90 offset1 = nextoffset1;
91 tag1 = fdt_next_tag(fdt1, offset1, &nextoffset1);
92 } while (tag1 == FDT_NOP);
94 offset2 = nextoffset2;
95 tag2 = fdt_next_tag(fdt2, offset2, &nextoffset2);
96 } while (tag2 == FDT_NOP);
99 MISMATCH("Tag mismatch (%d != %d) at (%d, %d)",
100 tag1, tag2, offset1, offset2);
104 name1 = fdt_get_name(fdt1, offset1, &err);
106 FAIL("fdt_get_name(fdt1, %d, ..): %s",
107 offset1, fdt_strerror(err));
108 name2 = fdt_get_name(fdt2, offset2, NULL);
110 FAIL("fdt_get_name(fdt2, %d, ..): %s",
111 offset2, fdt_strerror(err));
113 if (!streq(name1, name2))
114 MISMATCH("Name mismatch (\"%s\" != \"%s\") at (%d, %d)",
115 name1, name2, offset1, offset2);
119 prop1 = fdt_offset_ptr(fdt1, offset1, sizeof(*prop1));
121 FAIL("Could get fdt1 property at %d", offset1);
122 prop2 = fdt_offset_ptr(fdt2, offset2, sizeof(*prop2));
124 FAIL("Could get fdt2 property at %d", offset2);
126 name1 = fdt_string(fdt1, fdt32_to_cpu(prop1->nameoff));
127 name2 = fdt_string(fdt2, fdt32_to_cpu(prop2->nameoff));
128 if (!streq(name1, name2))
129 MISMATCH("Property name mismatch \"%s\" != \"%s\" "
130 "at (%d, %d)", name1, name2, offset1, offset2);
131 len1 = fdt32_to_cpu(prop1->len);
132 len2 = fdt32_to_cpu(prop2->len);
134 MISMATCH("Property length mismatch %u != %u "
135 "at (%d, %d)", len1, len2, offset1, offset2);
137 if (memcmp(prop1->data, prop2->data, len1) != 0)
138 MISMATCH("Property value mismatch at (%d, %d)",
148 int main(int argc, char *argv[])
151 uint32_t cpuid1, cpuid2;
153 test_init(argc, argv);
155 && ((argc != 4) || !streq(argv[1], "-n")))
156 CONFIG("Usage: %s [-n] <dtb file> <dtb file>", argv[0]);
160 fdt1 = load_blob(argv[argc-2]);
161 fdt2 = load_blob(argv[argc-1]);
163 compare_mem_rsv(fdt1, fdt2);
164 compare_structure(fdt1, fdt2);
166 cpuid1 = fdt_boot_cpuid_phys(fdt1);
167 cpuid2 = fdt_boot_cpuid_phys(fdt2);
168 if (cpuid1 != cpuid2)
169 MISMATCH("boot_cpuid_phys mismatch 0x%x != 0x%x",