spl_fit.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright (C) 2016 Google, Inc
  3. * Written by Simon Glass <sjg@chromium.org>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <errno.h>
  9. #include <image.h>
  10. #include <libfdt.h>
  11. #include <spl.h>
  12. static ulong fdt_getprop_u32(const void *fdt, int node, const char *prop)
  13. {
  14. const u32 *cell;
  15. int len;
  16. cell = fdt_getprop(fdt, node, prop, &len);
  17. if (len != sizeof(*cell))
  18. return -1U;
  19. return fdt32_to_cpu(*cell);
  20. }
  21. static int spl_fit_select_fdt(const void *fdt, int images, int *fdt_offsetp)
  22. {
  23. const char *name, *fdt_name;
  24. int conf, node, fdt_node;
  25. int len;
  26. *fdt_offsetp = 0;
  27. conf = fdt_path_offset(fdt, FIT_CONFS_PATH);
  28. if (conf < 0) {
  29. debug("%s: Cannot find /configurations node: %d\n", __func__,
  30. conf);
  31. return -EINVAL;
  32. }
  33. for (node = fdt_first_subnode(fdt, conf);
  34. node >= 0;
  35. node = fdt_next_subnode(fdt, node)) {
  36. name = fdt_getprop(fdt, node, "description", &len);
  37. if (!name) {
  38. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  39. printf("%s: Missing FDT description in DTB\n",
  40. __func__);
  41. #endif
  42. return -EINVAL;
  43. }
  44. if (board_fit_config_name_match(name))
  45. continue;
  46. debug("Selecting config '%s'", name);
  47. fdt_name = fdt_getprop(fdt, node, FIT_FDT_PROP, &len);
  48. if (!fdt_name) {
  49. debug("%s: Cannot find fdt name property: %d\n",
  50. __func__, len);
  51. return -EINVAL;
  52. }
  53. debug(", fdt '%s'\n", fdt_name);
  54. fdt_node = fdt_subnode_offset(fdt, images, fdt_name);
  55. if (fdt_node < 0) {
  56. debug("%s: Cannot find fdt node '%s': %d\n",
  57. __func__, fdt_name, fdt_node);
  58. return -EINVAL;
  59. }
  60. *fdt_offsetp = fdt_getprop_u32(fdt, fdt_node, "data-offset");
  61. len = fdt_getprop_u32(fdt, fdt_node, "data-size");
  62. debug("FIT: Selected '%s'\n", name);
  63. return len;
  64. }
  65. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  66. printf("No matching DT out of these options:\n");
  67. for (node = fdt_first_subnode(fdt, conf);
  68. node >= 0;
  69. node = fdt_next_subnode(fdt, node)) {
  70. name = fdt_getprop(fdt, node, "description", &len);
  71. printf(" %s\n", name);
  72. }
  73. #endif
  74. return -ENOENT;
  75. }
  76. static int get_aligned_image_offset(struct spl_load_info *info, int offset)
  77. {
  78. /*
  79. * If it is a FS read, get the first address before offset which is
  80. * aligned to ARCH_DMA_MINALIGN. If it is raw read return the
  81. * block number to which offset belongs.
  82. */
  83. if (info->filename)
  84. return offset & ~(ARCH_DMA_MINALIGN - 1);
  85. return offset / info->bl_len;
  86. }
  87. static int get_aligned_image_overhead(struct spl_load_info *info, int offset)
  88. {
  89. /*
  90. * If it is a FS read, get the difference between the offset and
  91. * the first address before offset which is aligned to
  92. * ARCH_DMA_MINALIGN. If it is raw read return the offset within the
  93. * block.
  94. */
  95. if (info->filename)
  96. return offset & (ARCH_DMA_MINALIGN - 1);
  97. return offset % info->bl_len;
  98. }
  99. static int get_aligned_image_size(struct spl_load_info *info, int data_size,
  100. int offset)
  101. {
  102. data_size = data_size + get_aligned_image_overhead(info, offset);
  103. if (info->filename)
  104. return data_size;
  105. return (data_size + info->bl_len - 1) / info->bl_len;
  106. }
  107. int spl_load_simple_fit(struct spl_image_info *spl_image,
  108. struct spl_load_info *info, ulong sector, void *fit)
  109. {
  110. int sectors;
  111. ulong size, load;
  112. unsigned long count;
  113. int node, images;
  114. void *load_ptr;
  115. int fdt_offset, fdt_len;
  116. int data_offset, data_size;
  117. int base_offset, align_len = ARCH_DMA_MINALIGN - 1;
  118. int src_sector;
  119. void *dst, *src;
  120. /*
  121. * Figure out where the external images start. This is the base for the
  122. * data-offset properties in each image.
  123. */
  124. size = fdt_totalsize(fit);
  125. size = (size + 3) & ~3;
  126. base_offset = (size + 3) & ~3;
  127. /*
  128. * So far we only have one block of data from the FIT. Read the entire
  129. * thing, including that first block, placing it so it finishes before
  130. * where we will load the image.
  131. *
  132. * Note that we will load the image such that its first byte will be
  133. * at the load address. Since that byte may be part-way through a
  134. * block, we may load the image up to one block before the load
  135. * address. So take account of that here by subtracting an addition
  136. * block length from the FIT start position.
  137. *
  138. * In fact the FIT has its own load address, but we assume it cannot
  139. * be before CONFIG_SYS_TEXT_BASE.
  140. */
  141. fit = (void *)((CONFIG_SYS_TEXT_BASE - size - info->bl_len -
  142. align_len) & ~align_len);
  143. sectors = get_aligned_image_size(info, size, 0);
  144. count = info->read(info, sector, sectors, fit);
  145. debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu\n",
  146. sector, sectors, fit, count);
  147. if (count == 0)
  148. return -EIO;
  149. /* find the firmware image to load */
  150. images = fdt_path_offset(fit, FIT_IMAGES_PATH);
  151. if (images < 0) {
  152. debug("%s: Cannot find /images node: %d\n", __func__, images);
  153. return -1;
  154. }
  155. node = fdt_first_subnode(fit, images);
  156. if (node < 0) {
  157. debug("%s: Cannot find first image node: %d\n", __func__, node);
  158. return -1;
  159. }
  160. /* Get its information and set up the spl_image structure */
  161. data_offset = fdt_getprop_u32(fit, node, "data-offset");
  162. data_size = fdt_getprop_u32(fit, node, "data-size");
  163. load = fdt_getprop_u32(fit, node, "load");
  164. debug("data_offset=%x, data_size=%x\n", data_offset, data_size);
  165. spl_image->load_addr = load;
  166. spl_image->entry_point = load;
  167. spl_image->os = IH_OS_U_BOOT;
  168. /*
  169. * Work out where to place the image. We read it so that the first
  170. * byte will be at 'load'. This may mean we need to load it starting
  171. * before then, since we can only read whole blocks.
  172. */
  173. data_offset += base_offset;
  174. sectors = get_aligned_image_size(info, data_size, data_offset);
  175. load_ptr = (void *)load;
  176. debug("U-Boot size %x, data %p\n", data_size, load_ptr);
  177. dst = load_ptr;
  178. /* Read the image */
  179. src_sector = sector + get_aligned_image_offset(info, data_offset);
  180. debug("Aligned image read: dst=%p, src_sector=%x, sectors=%x\n",
  181. dst, src_sector, sectors);
  182. count = info->read(info, src_sector, sectors, dst);
  183. if (count != sectors)
  184. return -EIO;
  185. debug("image: dst=%p, data_offset=%x, size=%x\n", dst, data_offset,
  186. data_size);
  187. src = dst + get_aligned_image_overhead(info, data_offset);
  188. #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS
  189. board_fit_image_post_process((void **)&src, (size_t *)&data_size);
  190. #endif
  191. memcpy(dst, src, data_size);
  192. /* Figure out which device tree the board wants to use */
  193. fdt_len = spl_fit_select_fdt(fit, images, &fdt_offset);
  194. if (fdt_len < 0)
  195. return fdt_len;
  196. /*
  197. * Read the device tree and place it after the image. There may be
  198. * some extra data before it since we can only read entire blocks.
  199. * And also align the destination address to ARCH_DMA_MINALIGN.
  200. */
  201. dst = (void *)((load + data_size + align_len) & ~align_len);
  202. fdt_offset += base_offset;
  203. sectors = get_aligned_image_size(info, fdt_len, fdt_offset);
  204. src_sector = sector + get_aligned_image_offset(info, fdt_offset);
  205. count = info->read(info, src_sector, sectors, dst);
  206. debug("Aligned fdt read: dst %p, src_sector = %x, sectors %x\n",
  207. dst, src_sector, sectors);
  208. if (count != sectors)
  209. return -EIO;
  210. /*
  211. * Copy the device tree so that it starts immediately after the image.
  212. * After this we will have the U-Boot image and its device tree ready
  213. * for us to start.
  214. */
  215. debug("fdt: dst=%p, data_offset=%x, size=%x\n", dst, fdt_offset,
  216. fdt_len);
  217. src = dst + get_aligned_image_overhead(info, fdt_offset);
  218. dst = load_ptr + data_size;
  219. #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS
  220. board_fit_image_post_process((void **)&src, (size_t *)&fdt_len);
  221. #endif
  222. memcpy(dst, src, fdt_len);
  223. return 0;
  224. }