1 /*
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.
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
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdint.h>
25 #include <limits.h>
26
27 #include <libfdt.h>
28
29 #include "tests.h"
30 #include "testdata.h"
31
32 static int notequal; /* = 0 */
33
34 #define MISMATCH(fmt, ...) \
35 do { \
36 if (notequal) \
37 PASS(); \
38 else \
39 FAIL(fmt, ##__VA_ARGS__); \
40 } while (0)
41
42 #define MATCH() \
43 do { \
44 if (!notequal) \
45 PASS(); \
46 else \
47 FAIL("Trees match which shouldn't"); \
48 } while (0)
49
50 #define CHECK(code) \
51 { \
52 err = (code); \
53 if (err) \
54 FAIL(#code ": %s", fdt_strerror(err)); \
55 }
56
mem_rsv_cmp(const void * p1,const void * p2)57 static int mem_rsv_cmp(const void *p1, const void *p2)
58 {
59 const struct fdt_reserve_entry *re1 = p1;
60 const struct fdt_reserve_entry *re2 = p2;
61
62 if (fdt64_to_cpu(re1->address) < fdt64_to_cpu(re2->address))
63 return -1;
64 else if (fdt64_to_cpu(re1->address) > fdt64_to_cpu(re2->address))
65 return 1;
66
67 if (fdt64_to_cpu(re1->size) < fdt64_to_cpu(re2->size))
68 return -1;
69 else if (fdt64_to_cpu(re1->size) > fdt64_to_cpu(re2->size))
70 return 1;
71
72 return 0;
73 }
74
compare_mem_rsv(void * fdt1,void * fdt2)75 static void compare_mem_rsv(void *fdt1, void *fdt2)
76 {
77 int i;
78 uint64_t addr1, size1, addr2, size2;
79 int err;
80
81 if (fdt_num_mem_rsv(fdt1) != fdt_num_mem_rsv(fdt2))
82 MISMATCH("Trees have different number of reserve entries");
83
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);
88
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));
92
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);
100 }
101 }
102
compare_properties(const void * fdt1,int offset1,const void * fdt2,int offset2)103 static void compare_properties(const void *fdt1, int offset1,
104 const void *fdt2, int offset2)
105 {
106 int offset = offset1;
107
108 /* Check the properties */
109 for (offset = fdt_first_property_offset(fdt1, offset1);
110 offset >= 0;
111 offset = fdt_next_property_offset(fdt1, offset)) {
112 const char *name;
113 int len1, len2;
114 const void *data1, *data2;
115 int i;
116
117 data1 = fdt_getprop_by_offset(fdt1, offset, &name, &len1);
118 if (!data1)
119 FAIL("fdt_getprop_by_offset(): %s\n",
120 fdt_strerror(len1));
121
122 verbose_printf("Property '%s'\n", name);
123
124 data2 = fdt_getprop(fdt2, offset2, name, &len2);
125 if (!data2) {
126 if (len2 == -FDT_ERR_NOTFOUND)
127 MISMATCH("Property '%s' missing\n", name);
128 else
129 FAIL("fdt_get_property(): %s\n",
130 fdt_strerror(len2));
131 }
132
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");
140
141 if (len1 != len2)
142 MISMATCH("Property '%s' mismatched length %d vs. %d\n",
143 name, len1, len2);
144 else if (memcmp(data1, data2, len1) != 0)
145 MISMATCH("Property '%s' mismatched value\n", name);
146 }
147 }
148
149 static void compare_node(const void *fdt1, int offset1,
150 const void *fdt2, int offset2);
151
compare_subnodes(const void * fdt1,int offset1,const void * fdt2,int offset2,int recurse)152 static void compare_subnodes(const void *fdt1, int offset1,
153 const void *fdt2, int offset2,
154 int recurse)
155 {
156 int coffset1, coffset2, depth;
157
158 for (depth = 0, coffset1 = offset1;
159 (coffset1 >= 0) && (depth >= 0);
160 coffset1 = fdt_next_node(fdt1, coffset1, &depth))
161 if (depth == 1) {
162 const char *name = fdt_get_name(fdt1, coffset1, NULL);
163
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));
171
172 if (recurse)
173 compare_node(fdt1, coffset1, fdt2, coffset2);
174 }
175 }
176
compare_node(const void * fdt1,int offset1,const void * fdt2,int offset2)177 static void compare_node(const void *fdt1, int offset1,
178 const void *fdt2, int offset2)
179 {
180 int err;
181 char path1[PATH_MAX], path2[PATH_MAX];
182
183 CHECK(fdt_get_path(fdt1, offset1, path1, sizeof(path1)));
184 CHECK(fdt_get_path(fdt2, offset2, path2, sizeof(path2)));
185
186 if (!streq(path1, path2))
187 TEST_BUG("Path mismatch %s vs. %s\n", path1, path2);
188
189 verbose_printf("Checking %s\n", path1);
190
191 compare_properties(fdt1, offset1, fdt2, offset2);
192 compare_properties(fdt2, offset2, fdt1, offset1);
193
194 compare_subnodes(fdt1, offset1, fdt2, offset2, 1);
195 compare_subnodes(fdt2, offset2, fdt1, offset1, 0);
196 }
197
main(int argc,char * argv[])198 int main(int argc, char *argv[])
199 {
200 void *fdt1, *fdt2;
201 uint32_t cpuid1, cpuid2;
202
203 test_init(argc, argv);
204 if ((argc != 3)
205 && ((argc != 4) || !streq(argv[1], "-n")))
206 CONFIG("Usage: %s [-n] <dtb file> <dtb file>", argv[0]);
207 if (argc == 4)
208 notequal = 1;
209
210 fdt1 = load_blob(argv[argc-2]);
211 fdt2 = load_blob(argv[argc-1]);
212
213 compare_mem_rsv(fdt1, fdt2);
214 compare_node(fdt1, 0, fdt2, 0);
215
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",
220 cpuid1, cpuid2);
221
222 MATCH();
223 }
224