xref: /netbsd-src/external/gpl2/dtc/dist/tests/dtbs_equal_ordered.c (revision fc4f42693f9b1c31f39f9cf50af1bf2010325808)
1 /*	$NetBSD: dtbs_equal_ordered.c,v 1.1.1.2 2017/06/08 15:59:26 skrll Exp $	*/
2 
3 /*
4  * libfdt - Flat Device Tree manipulation
5  *	Tests if two given dtbs are structurally equal (including order)
6  * Copyright (C) 2007 David Gibson, IBM Corporation.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdint.h>
27 
28 #include <libfdt.h>
29 
30 #include "tests.h"
31 #include "testdata.h"
32 
33 static int notequal; /* = 0 */
34 
35 #define MISMATCH(fmt, ...)			\
36 	do { \
37 		if (notequal) \
38 			PASS(); \
39 		else \
40 			FAIL(fmt, ##__VA_ARGS__);	\
41 	} while (0)
42 
43 #define MATCH()			\
44 	do { \
45 		if (!notequal) \
46 			PASS(); \
47 		else \
48 			FAIL("Trees match which shouldn't");	\
49 	} while (0)
50 
51 #define CHECK(code) \
52 	{ \
53 		err = (code); \
54 		if (err) \
55 			FAIL(#code ": %s", fdt_strerror(err)); \
56 	}
57 
58 static void compare_mem_rsv(const void *fdt1, const void *fdt2)
59 {
60 	int i;
61 	uint64_t addr1, size1, addr2, size2;
62 	int err;
63 
64 	if (fdt_num_mem_rsv(fdt1) != fdt_num_mem_rsv(fdt2))
65 		MISMATCH("Trees have different number of reserve entries");
66 	for (i = 0; i < fdt_num_mem_rsv(fdt1); i++) {
67 		CHECK(fdt_get_mem_rsv(fdt1, i, &addr1, &size1));
68 		CHECK(fdt_get_mem_rsv(fdt2, i, &addr2, &size2));
69 
70 		if ((addr1 != addr2) || (size1 != size2))
71 			MISMATCH("Mismatch in reserve entry %d: "
72 				 "(0x%llx, 0x%llx) != (0x%llx, 0x%llx)", i,
73 				 (unsigned long long)addr1,
74 				 (unsigned long long)size1,
75 				 (unsigned long long)addr2,
76 				 (unsigned long long)size2);
77 	}
78 }
79 
80 static void compare_structure(const void *fdt1, const void *fdt2)
81 {
82 	int nextoffset1 = 0, nextoffset2 = 0;
83 	int offset1, offset2;
84 	uint32_t tag1, tag2;
85 	const char *name1, *name2;
86 	int err;
87 	const struct fdt_property *prop1, *prop2;
88 	int len1, len2;
89 
90 	while (1) {
91 		do {
92 			offset1 = nextoffset1;
93 			tag1 = fdt_next_tag(fdt1, offset1, &nextoffset1);
94 		} while (tag1 == FDT_NOP);
95 		do {
96 			offset2 = nextoffset2;
97 			tag2 = fdt_next_tag(fdt2, offset2, &nextoffset2);
98 		} while (tag2 == FDT_NOP);
99 
100 		if (tag1 != tag2)
101 			MISMATCH("Tag mismatch (%d != %d) at (%d, %d)",
102 			     tag1, tag2, offset1, offset2);
103 
104 		switch (tag1) {
105 		case FDT_BEGIN_NODE:
106 			name1 = fdt_get_name(fdt1, offset1, &err);
107 			if (!name1)
108 				FAIL("fdt_get_name(fdt1, %d, ..): %s",
109 				     offset1, fdt_strerror(err));
110 			name2 = fdt_get_name(fdt2, offset2, NULL);
111 			if (!name2)
112 				FAIL("fdt_get_name(fdt2, %d, ..): %s",
113 				     offset2, fdt_strerror(err));
114 
115 			if (!streq(name1, name2))
116 			    MISMATCH("Name mismatch (\"%s\" != \"%s\") at (%d, %d)",
117 				     name1, name2, offset1, offset2);
118 			break;
119 
120 		case FDT_PROP:
121 			prop1 = fdt_offset_ptr(fdt1, offset1, sizeof(*prop1));
122 			if (!prop1)
123 				FAIL("Could get fdt1 property at %d", offset1);
124 			prop2 = fdt_offset_ptr(fdt2, offset2, sizeof(*prop2));
125 			if (!prop2)
126 				FAIL("Could get fdt2 property at %d", offset2);
127 
128 			name1 = fdt_string(fdt1, fdt32_to_cpu(prop1->nameoff));
129 			name2 = fdt_string(fdt2, fdt32_to_cpu(prop2->nameoff));
130 			if (!streq(name1, name2))
131 				MISMATCH("Property name mismatch \"%s\" != \"%s\" "
132 					 "at (%d, %d)", name1, name2, offset1, offset2);
133 			len1 = fdt32_to_cpu(prop1->len);
134 			len2 = fdt32_to_cpu(prop2->len);
135 			if (len1 != len2)
136 				MISMATCH("Property length mismatch %u != %u "
137 					 "at (%d, %d)", len1, len2, offset1, offset2);
138 
139 			if (memcmp(prop1->data, prop2->data, len1) != 0)
140 				MISMATCH("Property value mismatch at (%d, %d)",
141 					 offset1, offset2);
142 			break;
143 
144 		case FDT_END:
145 			return;
146 		}
147 	}
148 }
149 
150 int main(int argc, char *argv[])
151 {
152 	void *fdt1, *fdt2;
153 	uint32_t cpuid1, cpuid2;
154 
155 	test_init(argc, argv);
156 	if ((argc != 3)
157 	    && ((argc != 4) || !streq(argv[1], "-n")))
158 		CONFIG("Usage: %s [-n] <dtb file> <dtb file>", argv[0]);
159 	if (argc == 4)
160 		notequal = 1;
161 
162 	fdt1 = load_blob(argv[argc-2]);
163 	fdt2 = load_blob(argv[argc-1]);
164 
165 	compare_mem_rsv(fdt1, fdt2);
166 	compare_structure(fdt1, fdt2);
167 
168 	cpuid1 = fdt_boot_cpuid_phys(fdt1);
169 	cpuid2 = fdt_boot_cpuid_phys(fdt2);
170 	if (cpuid1 != cpuid2)
171 		MISMATCH("boot_cpuid_phys mismatch 0x%x != 0x%x",
172 			 cpuid1, cpuid2);
173 
174 	MATCH();
175 }
176