image-android-dt.c 4.3 KB

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