Sam Protsenko | 2f770a9 | 2018-08-16 23:34:12 +0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * (C) Copyright 2018 Linaro Ltd. |
| 4 | * Sam Protsenko <semen.protsenko@linaro.org> |
| 5 | */ |
| 6 | |
| 7 | #include <image-android-dt.h> |
| 8 | #include <dt_table.h> |
| 9 | #include <common.h> |
| 10 | #include <linux/libfdt.h> |
| 11 | #include <mapmem.h> |
| 12 | |
| 13 | /** |
| 14 | * Check if image header is correct. |
| 15 | * |
| 16 | * @param hdr_addr Start address of DT image |
| 17 | * @return true if header is correct or false if header is incorrect |
| 18 | */ |
| 19 | bool android_dt_check_header(ulong hdr_addr) |
| 20 | { |
| 21 | const struct dt_table_header *hdr; |
| 22 | u32 magic; |
| 23 | |
| 24 | hdr = map_sysmem(hdr_addr, sizeof(*hdr)); |
| 25 | magic = fdt32_to_cpu(hdr->magic); |
| 26 | unmap_sysmem(hdr); |
| 27 | |
| 28 | return magic == DT_TABLE_MAGIC; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Get the address of FDT (dtb or dtbo) in memory by its index in image. |
| 33 | * |
| 34 | * @param hdr_addr Start address of DT image |
| 35 | * @param index Index of desired FDT in image (starting from 0) |
| 36 | * @param[out] addr If not NULL, will contain address to specified FDT |
| 37 | * @param[out] size If not NULL, will contain size of specified FDT |
| 38 | * |
| 39 | * @return true on success or false on error |
| 40 | */ |
| 41 | bool android_dt_get_fdt_by_index(ulong hdr_addr, u32 index, ulong *addr, |
| 42 | u32 *size) |
| 43 | { |
| 44 | const struct dt_table_header *hdr; |
| 45 | const struct dt_table_entry *e; |
| 46 | u32 entry_count, entries_offset, entry_size; |
| 47 | ulong e_addr; |
| 48 | u32 dt_offset, dt_size; |
| 49 | |
| 50 | hdr = map_sysmem(hdr_addr, sizeof(*hdr)); |
| 51 | entry_count = fdt32_to_cpu(hdr->dt_entry_count); |
| 52 | entries_offset = fdt32_to_cpu(hdr->dt_entries_offset); |
| 53 | entry_size = fdt32_to_cpu(hdr->dt_entry_size); |
| 54 | unmap_sysmem(hdr); |
| 55 | |
Eugeniu Rosca | 910c889 | 2019-03-14 18:31:39 +0100 | [diff] [blame] | 56 | if (index >= entry_count) { |
| 57 | printf("Error: index >= dt_entry_count (%u >= %u)\n", index, |
Sam Protsenko | 2f770a9 | 2018-08-16 23:34:12 +0300 | [diff] [blame] | 58 | entry_count); |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | e_addr = hdr_addr + entries_offset + index * entry_size; |
| 63 | e = map_sysmem(e_addr, sizeof(*e)); |
| 64 | dt_offset = fdt32_to_cpu(e->dt_offset); |
| 65 | dt_size = fdt32_to_cpu(e->dt_size); |
| 66 | unmap_sysmem(e); |
| 67 | |
| 68 | if (addr) |
| 69 | *addr = hdr_addr + dt_offset; |
| 70 | if (size) |
| 71 | *size = dt_size; |
| 72 | |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | #if !defined(CONFIG_SPL_BUILD) |
| 77 | static void android_dt_print_fdt_info(const struct fdt_header *fdt) |
| 78 | { |
| 79 | u32 fdt_size; |
| 80 | int root_node_off; |
Sam Protsenko | d40e4a4 | 2019-08-05 20:11:03 +0300 | [diff] [blame] | 81 | const char *compatible; |
Sam Protsenko | 2f770a9 | 2018-08-16 23:34:12 +0300 | [diff] [blame] | 82 | |
Sam Protsenko | 2f770a9 | 2018-08-16 23:34:12 +0300 | [diff] [blame] | 83 | root_node_off = fdt_path_offset(fdt, "/"); |
| 84 | if (root_node_off < 0) { |
| 85 | printf("Error: Root node not found\n"); |
Sam Protsenko | d40e4a4 | 2019-08-05 20:11:03 +0300 | [diff] [blame] | 86 | return; |
Sam Protsenko | 2f770a9 | 2018-08-16 23:34:12 +0300 | [diff] [blame] | 87 | } |
| 88 | |
Sam Protsenko | d40e4a4 | 2019-08-05 20:11:03 +0300 | [diff] [blame] | 89 | fdt_size = fdt_totalsize(fdt); |
| 90 | compatible = fdt_getprop(fdt, root_node_off, "compatible", |
| 91 | NULL); |
| 92 | |
Sam Protsenko | 2f770a9 | 2018-08-16 23:34:12 +0300 | [diff] [blame] | 93 | printf(" (FDT)size = %d\n", fdt_size); |
| 94 | printf(" (FDT)compatible = %s\n", |
| 95 | compatible ? compatible : "(unknown)"); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Print information about DT image structure. |
| 100 | * |
| 101 | * @param hdr_addr Start address of DT image |
| 102 | */ |
| 103 | void android_dt_print_contents(ulong hdr_addr) |
| 104 | { |
| 105 | const struct dt_table_header *hdr; |
| 106 | u32 entry_count, entries_offset, entry_size; |
| 107 | u32 i; |
| 108 | |
| 109 | hdr = map_sysmem(hdr_addr, sizeof(*hdr)); |
| 110 | entry_count = fdt32_to_cpu(hdr->dt_entry_count); |
| 111 | entries_offset = fdt32_to_cpu(hdr->dt_entries_offset); |
| 112 | entry_size = fdt32_to_cpu(hdr->dt_entry_size); |
| 113 | |
| 114 | /* Print image header info */ |
| 115 | printf("dt_table_header:\n"); |
| 116 | printf(" magic = %08x\n", fdt32_to_cpu(hdr->magic)); |
| 117 | printf(" total_size = %d\n", fdt32_to_cpu(hdr->total_size)); |
| 118 | printf(" header_size = %d\n", fdt32_to_cpu(hdr->header_size)); |
| 119 | printf(" dt_entry_size = %d\n", entry_size); |
| 120 | printf(" dt_entry_count = %d\n", entry_count); |
| 121 | printf(" dt_entries_offset = %d\n", entries_offset); |
| 122 | printf(" page_size = %d\n", fdt32_to_cpu(hdr->page_size)); |
| 123 | printf(" version = %d\n", fdt32_to_cpu(hdr->version)); |
| 124 | |
| 125 | unmap_sysmem(hdr); |
| 126 | |
| 127 | /* Print image entries info */ |
| 128 | for (i = 0; i < entry_count; ++i) { |
| 129 | const ulong e_addr = hdr_addr + entries_offset + i * entry_size; |
| 130 | const struct dt_table_entry *e; |
| 131 | const struct fdt_header *fdt; |
| 132 | u32 dt_offset, dt_size; |
| 133 | u32 j; |
| 134 | |
| 135 | e = map_sysmem(e_addr, sizeof(*e)); |
| 136 | dt_offset = fdt32_to_cpu(e->dt_offset); |
| 137 | dt_size = fdt32_to_cpu(e->dt_size); |
| 138 | |
| 139 | printf("dt_table_entry[%d]:\n", i); |
| 140 | printf(" dt_size = %d\n", dt_size); |
| 141 | printf(" dt_offset = %d\n", dt_offset); |
| 142 | printf(" id = %08x\n", fdt32_to_cpu(e->id)); |
| 143 | printf(" rev = %08x\n", fdt32_to_cpu(e->rev)); |
| 144 | for (j = 0; j < 4; ++j) { |
| 145 | printf(" custom[%d] = %08x\n", j, |
| 146 | fdt32_to_cpu(e->custom[j])); |
| 147 | } |
| 148 | |
| 149 | unmap_sysmem(e); |
| 150 | |
| 151 | /* Print FDT info for this entry */ |
| 152 | fdt = map_sysmem(hdr_addr + dt_offset, sizeof(*fdt)); |
| 153 | android_dt_print_fdt_info(fdt); |
| 154 | unmap_sysmem(fdt); |
| 155 | } |
| 156 | } |
| 157 | #endif |