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