Sam Protsenko | c044733 | 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 |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 17 | * Return: true if header is correct or false if header is incorrect |
Sam Protsenko | c044733 | 2018-08-16 23:34:12 +0300 | [diff] [blame] | 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 | |
Bo Lv | 81aadc3 | 2024-02-28 20:18:44 +0800 | [diff] [blame^] | 31 | #if defined(CONFIG_OF_LIBFDT_OVERLAY) && defined(CONFIG_AMLOGIC_MODIFY) |
| 32 | u32 android_dt_get_totalsize(ulong hdr_addr) |
| 33 | { |
| 34 | const struct dt_table_header *hdr; |
| 35 | u32 totalsize; |
| 36 | |
| 37 | hdr = map_sysmem(hdr_addr, sizeof(*hdr)); |
| 38 | totalsize = fdt32_to_cpu(hdr->total_size); |
| 39 | unmap_sysmem(hdr); |
| 40 | |
| 41 | return totalsize; |
| 42 | } |
| 43 | #endif |
| 44 | |
Sam Protsenko | c044733 | 2018-08-16 23:34:12 +0300 | [diff] [blame] | 45 | /** |
| 46 | * Get the address of FDT (dtb or dtbo) in memory by its index in image. |
| 47 | * |
| 48 | * @param hdr_addr Start address of DT image |
| 49 | * @param index Index of desired FDT in image (starting from 0) |
| 50 | * @param[out] addr If not NULL, will contain address to specified FDT |
| 51 | * @param[out] size If not NULL, will contain size of specified FDT |
| 52 | * |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 53 | * Return: true on success or false on error |
Sam Protsenko | c044733 | 2018-08-16 23:34:12 +0300 | [diff] [blame] | 54 | */ |
| 55 | bool android_dt_get_fdt_by_index(ulong hdr_addr, u32 index, ulong *addr, |
| 56 | u32 *size) |
| 57 | { |
| 58 | const struct dt_table_header *hdr; |
| 59 | const struct dt_table_entry *e; |
| 60 | u32 entry_count, entries_offset, entry_size; |
| 61 | ulong e_addr; |
| 62 | u32 dt_offset, dt_size; |
| 63 | |
| 64 | hdr = map_sysmem(hdr_addr, sizeof(*hdr)); |
| 65 | entry_count = fdt32_to_cpu(hdr->dt_entry_count); |
| 66 | entries_offset = fdt32_to_cpu(hdr->dt_entries_offset); |
| 67 | entry_size = fdt32_to_cpu(hdr->dt_entry_size); |
| 68 | unmap_sysmem(hdr); |
| 69 | |
Eugeniu Rosca | e63bf1b | 2019-03-14 18:31:39 +0100 | [diff] [blame] | 70 | if (index >= entry_count) { |
| 71 | printf("Error: index >= dt_entry_count (%u >= %u)\n", index, |
Sam Protsenko | c044733 | 2018-08-16 23:34:12 +0300 | [diff] [blame] | 72 | entry_count); |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | e_addr = hdr_addr + entries_offset + index * entry_size; |
| 77 | e = map_sysmem(e_addr, sizeof(*e)); |
| 78 | dt_offset = fdt32_to_cpu(e->dt_offset); |
| 79 | dt_size = fdt32_to_cpu(e->dt_size); |
| 80 | unmap_sysmem(e); |
| 81 | |
| 82 | if (addr) |
| 83 | *addr = hdr_addr + dt_offset; |
| 84 | if (size) |
| 85 | *size = dt_size; |
| 86 | |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | #if !defined(CONFIG_SPL_BUILD) |
| 91 | static void android_dt_print_fdt_info(const struct fdt_header *fdt) |
| 92 | { |
| 93 | u32 fdt_size; |
| 94 | int root_node_off; |
Sam Protsenko | 24434ad | 2019-08-05 20:11:03 +0300 | [diff] [blame] | 95 | const char *compatible; |
Sam Protsenko | c044733 | 2018-08-16 23:34:12 +0300 | [diff] [blame] | 96 | |
Sam Protsenko | c044733 | 2018-08-16 23:34:12 +0300 | [diff] [blame] | 97 | root_node_off = fdt_path_offset(fdt, "/"); |
| 98 | if (root_node_off < 0) { |
| 99 | printf("Error: Root node not found\n"); |
Sam Protsenko | 24434ad | 2019-08-05 20:11:03 +0300 | [diff] [blame] | 100 | return; |
Sam Protsenko | c044733 | 2018-08-16 23:34:12 +0300 | [diff] [blame] | 101 | } |
| 102 | |
Sam Protsenko | 24434ad | 2019-08-05 20:11:03 +0300 | [diff] [blame] | 103 | fdt_size = fdt_totalsize(fdt); |
| 104 | compatible = fdt_getprop(fdt, root_node_off, "compatible", |
| 105 | NULL); |
| 106 | |
Sam Protsenko | c044733 | 2018-08-16 23:34:12 +0300 | [diff] [blame] | 107 | printf(" (FDT)size = %d\n", fdt_size); |
| 108 | printf(" (FDT)compatible = %s\n", |
| 109 | compatible ? compatible : "(unknown)"); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Print information about DT image structure. |
| 114 | * |
| 115 | * @param hdr_addr Start address of DT image |
| 116 | */ |
| 117 | void android_dt_print_contents(ulong hdr_addr) |
| 118 | { |
| 119 | const struct dt_table_header *hdr; |
| 120 | u32 entry_count, entries_offset, entry_size; |
| 121 | u32 i; |
| 122 | |
| 123 | hdr = map_sysmem(hdr_addr, sizeof(*hdr)); |
| 124 | entry_count = fdt32_to_cpu(hdr->dt_entry_count); |
| 125 | entries_offset = fdt32_to_cpu(hdr->dt_entries_offset); |
| 126 | entry_size = fdt32_to_cpu(hdr->dt_entry_size); |
| 127 | |
| 128 | /* Print image header info */ |
| 129 | printf("dt_table_header:\n"); |
| 130 | printf(" magic = %08x\n", fdt32_to_cpu(hdr->magic)); |
| 131 | printf(" total_size = %d\n", fdt32_to_cpu(hdr->total_size)); |
| 132 | printf(" header_size = %d\n", fdt32_to_cpu(hdr->header_size)); |
| 133 | printf(" dt_entry_size = %d\n", entry_size); |
| 134 | printf(" dt_entry_count = %d\n", entry_count); |
| 135 | printf(" dt_entries_offset = %d\n", entries_offset); |
| 136 | printf(" page_size = %d\n", fdt32_to_cpu(hdr->page_size)); |
| 137 | printf(" version = %d\n", fdt32_to_cpu(hdr->version)); |
| 138 | |
| 139 | unmap_sysmem(hdr); |
| 140 | |
| 141 | /* Print image entries info */ |
| 142 | for (i = 0; i < entry_count; ++i) { |
| 143 | const ulong e_addr = hdr_addr + entries_offset + i * entry_size; |
| 144 | const struct dt_table_entry *e; |
| 145 | const struct fdt_header *fdt; |
| 146 | u32 dt_offset, dt_size; |
| 147 | u32 j; |
| 148 | |
| 149 | e = map_sysmem(e_addr, sizeof(*e)); |
| 150 | dt_offset = fdt32_to_cpu(e->dt_offset); |
| 151 | dt_size = fdt32_to_cpu(e->dt_size); |
| 152 | |
| 153 | printf("dt_table_entry[%d]:\n", i); |
| 154 | printf(" dt_size = %d\n", dt_size); |
| 155 | printf(" dt_offset = %d\n", dt_offset); |
| 156 | printf(" id = %08x\n", fdt32_to_cpu(e->id)); |
| 157 | printf(" rev = %08x\n", fdt32_to_cpu(e->rev)); |
| 158 | for (j = 0; j < 4; ++j) { |
| 159 | printf(" custom[%d] = %08x\n", j, |
| 160 | fdt32_to_cpu(e->custom[j])); |
| 161 | } |
| 162 | |
| 163 | unmap_sysmem(e); |
| 164 | |
| 165 | /* Print FDT info for this entry */ |
| 166 | fdt = map_sysmem(hdr_addr + dt_offset, sizeof(*fdt)); |
| 167 | android_dt_print_fdt_info(fdt); |
| 168 | unmap_sysmem(fdt); |
| 169 | } |
| 170 | } |
| 171 | #endif |