Home | History | Annotate | Download | only in tests
      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 
     26 #include <libfdt.h>
     27 
     28 #include "tests.h"
     29 #include "testdata.h"
     30 
     31 static int notequal; /* = 0 */
     32 
     33 #define MISMATCH(fmt, ...)			\
     34 	do { \
     35 		if (notequal) \
     36 			PASS(); \
     37 		else \
     38 			FAIL(fmt, ##__VA_ARGS__);	\
     39 	} while (0)
     40 
     41 #define MATCH()			\
     42 	do { \
     43 		if (!notequal) \
     44 			PASS(); \
     45 		else \
     46 			FAIL("Trees match which shouldn't");	\
     47 	} while (0)
     48 
     49 #define CHECK(code) \
     50 	{ \
     51 		err = (code); \
     52 		if (err) \
     53 			FAIL(#code ": %s", fdt_strerror(err)); \
     54 	}
     55 
     56 static void compare_mem_rsv(const void *fdt1, const void *fdt2)
     57 {
     58 	int i;
     59 	uint64_t addr1, size1, addr2, size2;
     60 	int err;
     61 
     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));
     67 
     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);
     75 	}
     76 }
     77 
     78 static void compare_structure(const void *fdt1, const void *fdt2)
     79 {
     80 	int nextoffset1 = 0, nextoffset2 = 0;
     81 	int offset1, offset2;
     82 	uint32_t tag1, tag2;
     83 	const char *name1, *name2;
     84 	int err;
     85 	const struct fdt_property *prop1, *prop2;
     86 	int len1, len2;
     87 
     88 	while (1) {
     89 		do {
     90 			offset1 = nextoffset1;
     91 			tag1 = fdt_next_tag(fdt1, offset1, &nextoffset1);
     92 		} while (tag1 == FDT_NOP);
     93 		do {
     94 			offset2 = nextoffset2;
     95 			tag2 = fdt_next_tag(fdt2, offset2, &nextoffset2);
     96 		} while (tag2 == FDT_NOP);
     97 
     98 		if (tag1 != tag2)
     99 			MISMATCH("Tag mismatch (%d != %d) at (%d, %d)",
    100 			     tag1, tag2, offset1, offset2);
    101 
    102 		switch (tag1) {
    103 		case FDT_BEGIN_NODE:
    104 			name1 = fdt_get_name(fdt1, offset1, &err);
    105 			if (!name1)
    106 				FAIL("fdt_get_name(fdt1, %d, ..): %s",
    107 				     offset1, fdt_strerror(err));
    108 			name2 = fdt_get_name(fdt2, offset2, NULL);
    109 			if (!name2)
    110 				FAIL("fdt_get_name(fdt2, %d, ..): %s",
    111 				     offset2, fdt_strerror(err));
    112 
    113 			if (!streq(name1, name2))
    114 			    MISMATCH("Name mismatch (\"%s\" != \"%s\") at (%d, %d)",
    115 				     name1, name2, offset1, offset2);
    116 			break;
    117 
    118 		case FDT_PROP:
    119 			prop1 = fdt_offset_ptr(fdt1, offset1, sizeof(*prop1));
    120 			if (!prop1)
    121 				FAIL("Could get fdt1 property at %d", offset1);
    122 			prop2 = fdt_offset_ptr(fdt2, offset2, sizeof(*prop2));
    123 			if (!prop2)
    124 				FAIL("Could get fdt2 property at %d", offset2);
    125 
    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);
    133 			if (len1 != len2)
    134 				MISMATCH("Property length mismatch %u != %u "
    135 					 "at (%d, %d)", len1, len2, offset1, offset2);
    136 
    137 			if (memcmp(prop1->data, prop2->data, len1) != 0)
    138 				MISMATCH("Property value mismatch at (%d, %d)",
    139 					 offset1, offset2);
    140 			break;
    141 
    142 		case FDT_END:
    143 			return;
    144 		}
    145 	}
    146 }
    147 
    148 int main(int argc, char *argv[])
    149 {
    150 	void *fdt1, *fdt2;
    151 	uint32_t cpuid1, cpuid2;
    152 
    153 	test_init(argc, argv);
    154 	if ((argc != 3)
    155 	    && ((argc != 4) || !streq(argv[1], "-n")))
    156 		CONFIG("Usage: %s [-n] <dtb file> <dtb file>", argv[0]);
    157 	if (argc == 4)
    158 		notequal = 1;
    159 
    160 	fdt1 = load_blob(argv[argc-2]);
    161 	fdt2 = load_blob(argv[argc-1]);
    162 
    163 	compare_mem_rsv(fdt1, fdt2);
    164 	compare_structure(fdt1, fdt2);
    165 
    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",
    170 			 cpuid1, cpuid2);
    171 
    172 	MATCH();
    173 }
    174