David Gibson | 3c44c87 | 2007-10-24 11:06:09 +1000 | [diff] [blame] | 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 <fdt.h> |
| 27 | #include <libfdt.h> |
| 28 | |
| 29 | #include "tests.h" |
| 30 | #include "testdata.h" |
| 31 | |
| 32 | void compare_mem_rsv(const void *fdt1, const void *fdt2) |
| 33 | { |
| 34 | int i; |
| 35 | uint64_t addr1, size1, addr2, size2; |
| 36 | int err; |
| 37 | |
| 38 | if (fdt_num_mem_rsv(fdt1) != fdt_num_mem_rsv(fdt2)) |
| 39 | FAIL("Trees have different number of reserve entries"); |
| 40 | for (i = 0; i < fdt_num_mem_rsv(fdt1); i++) { |
| 41 | err = fdt_get_mem_rsv(fdt1, i, &addr1, &size1); |
| 42 | if (err) |
| 43 | FAIL("fdt_get_mem_rsv(fdt1, %d, ...): %s", i, |
| 44 | fdt_strerror(err)); |
| 45 | err = fdt_get_mem_rsv(fdt2, i, &addr2, &size2); |
| 46 | if (err) |
| 47 | FAIL("fdt_get_mem_rsv(fdt2, %d, ...): %s", i, |
| 48 | fdt_strerror(err)); |
| 49 | if ((addr1 != addr2) || (size1 != size2)) |
| 50 | FAIL("Mismatch in reserve entry %d: " |
| 51 | "(0x%llx, 0x%llx) != (0x%llx, 0x%llx)", i, |
| 52 | addr1, size1, addr2, size2); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | void compare_structure(const void *fdt1, const void *fdt2) |
| 57 | { |
| 58 | int nextoffset1 = 0, nextoffset2 = 0; |
| 59 | int offset1, offset2; |
| 60 | uint32_t tag1, tag2; |
| 61 | const char *name1, *name2; |
| 62 | int err; |
| 63 | const struct fdt_property *prop1, *prop2; |
| 64 | int len1, len2; |
| 65 | |
| 66 | while (1) { |
| 67 | do { |
| 68 | offset1 = nextoffset1; |
| 69 | tag1 = fdt_next_tag(fdt1, offset1, &nextoffset1); |
| 70 | } while (tag1 == FDT_NOP); |
| 71 | do { |
| 72 | offset2 = nextoffset2; |
| 73 | tag2 = fdt_next_tag(fdt2, offset2, &nextoffset2); |
| 74 | } while (tag2 == FDT_NOP); |
| 75 | |
| 76 | if (tag1 != tag2) |
| 77 | FAIL("Tag mismatch (%d != %d) at (%d, %d)", |
| 78 | tag1, tag2, offset1, offset2); |
| 79 | |
| 80 | switch (tag1) { |
| 81 | case FDT_BEGIN_NODE: |
| 82 | name1 = fdt_get_name(fdt1, offset1, &err); |
| 83 | if (!name1) |
| 84 | FAIL("fdt_get_name(fdt1, %d, ..): %s", |
| 85 | offset1, fdt_strerror(err)); |
| 86 | name2 = fdt_get_name(fdt2, offset2, NULL); |
| 87 | if (!name2) |
| 88 | FAIL("fdt_get_name(fdt2, %d, ..): %s", |
| 89 | offset2, fdt_strerror(err)); |
| 90 | if (!streq(name1, name2)) |
| 91 | FAIL("Name mismatch (\"%s\" != \"%s\") at (%d, %d)", |
| 92 | name1, name2, offset1, offset2); |
| 93 | break; |
| 94 | |
| 95 | case FDT_PROP: |
David Gibson | 2cf8693 | 2007-11-19 17:26:22 +1100 | [diff] [blame] | 96 | prop1 = fdt_offset_ptr(fdt1, offset1, sizeof(*prop1)); |
David Gibson | 3c44c87 | 2007-10-24 11:06:09 +1000 | [diff] [blame] | 97 | if (!prop1) |
| 98 | FAIL("Could get fdt1 property at %d", offset1); |
David Gibson | 2cf8693 | 2007-11-19 17:26:22 +1100 | [diff] [blame] | 99 | prop2 = fdt_offset_ptr(fdt2, offset2, sizeof(*prop2)); |
David Gibson | 3c44c87 | 2007-10-24 11:06:09 +1000 | [diff] [blame] | 100 | if (!prop2) |
| 101 | FAIL("Could get fdt2 property at %d", offset2); |
| 102 | |
| 103 | name1 = fdt_string(fdt1, fdt32_to_cpu(prop1->nameoff)); |
| 104 | name2 = fdt_string(fdt2, fdt32_to_cpu(prop2->nameoff)); |
| 105 | if (!streq(name1, name2)) |
| 106 | FAIL("Property name mismatch \"%s\" != \"%s\" " |
| 107 | "at (%d, %d)", name1, name2, offset1, offset2); |
| 108 | len1 = fdt32_to_cpu(prop1->len); |
| 109 | len2 = fdt32_to_cpu(prop2->len); |
| 110 | if (len1 != len2) |
| 111 | FAIL("Property length mismatch %u != %u " |
| 112 | "at (%d, %d)", len1, len2, offset1, offset2); |
| 113 | |
| 114 | if (memcmp(prop1->data, prop2->data, len1) != 0) |
| 115 | FAIL("Property value mismatch at (%d, %d)", |
| 116 | offset1, offset2); |
| 117 | break; |
| 118 | |
| 119 | case FDT_END: |
| 120 | return; |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | int main(int argc, char *argv[]) |
| 126 | { |
| 127 | void *fdt1, *fdt2; |
| 128 | |
| 129 | test_init(argc, argv); |
| 130 | if (argc != 3) |
| 131 | CONFIG("Usage: %s <dtb file> <dtb file>", argv[0]); |
| 132 | fdt1 = load_blob(argv[1]); |
| 133 | fdt2 = load_blob(argv[2]); |
| 134 | |
| 135 | compare_mem_rsv(fdt1, fdt2); |
| 136 | compare_structure(fdt1, fdt2); |
| 137 | |
| 138 | PASS(); |
| 139 | } |