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