spl_fit.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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_load_info *info, ulong sector, void *fit)
  108. {
  109. int sectors;
  110. ulong size, load;
  111. unsigned long count;
  112. int node, images;
  113. void *load_ptr;
  114. int fdt_offset, fdt_len;
  115. int data_offset, data_size;
  116. int base_offset, align_len = ARCH_DMA_MINALIGN - 1;
  117. int src_sector;
  118. void *dst, *src;
  119. /*
  120. * Figure out where the external images start. This is the base for the
  121. * data-offset properties in each image.
  122. */
  123. size = fdt_totalsize(fit);
  124. size = (size + 3) & ~3;
  125. base_offset = (size + 3) & ~3;
  126. /*
  127. * So far we only have one block of data from the FIT. Read the entire
  128. * thing, including that first block, placing it so it finishes before
  129. * where we will load the image.
  130. *
  131. * Note that we will load the image such that its first byte will be
  132. * at the load address. Since that byte may be part-way through a
  133. * block, we may load the image up to one block before the load
  134. * address. So take account of that here by subtracting an addition
  135. * block length from the FIT start position.
  136. *
  137. * In fact the FIT has its own load address, but we assume it cannot
  138. * be before CONFIG_SYS_TEXT_BASE.
  139. */
  140. fit = (void *)((CONFIG_SYS_TEXT_BASE - size - info->bl_len -
  141. align_len) & ~align_len);
  142. sectors = get_aligned_image_size(info, size, 0);
  143. count = info->read(info, sector, sectors, fit);
  144. debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu\n",
  145. sector, sectors, fit, count);
  146. if (count == 0)
  147. return -EIO;
  148. /* find the firmware image to load */
  149. images = fdt_path_offset(fit, FIT_IMAGES_PATH);
  150. if (images < 0) {
  151. debug("%s: Cannot find /images node: %d\n", __func__, images);
  152. return -1;
  153. }
  154. node = fdt_first_subnode(fit, images);
  155. if (node < 0) {
  156. debug("%s: Cannot find first image node: %d\n", __func__, node);
  157. return -1;
  158. }
  159. /* Get its information and set up the spl_image structure */
  160. data_offset = fdt_getprop_u32(fit, node, "data-offset");
  161. data_size = fdt_getprop_u32(fit, node, "data-size");
  162. load = fdt_getprop_u32(fit, node, "load");
  163. debug("data_offset=%x, data_size=%x\n", data_offset, data_size);
  164. spl_image.load_addr = load;
  165. spl_image.entry_point = load;
  166. spl_image.os = IH_OS_U_BOOT;
  167. /*
  168. * Work out where to place the image. We read it so that the first
  169. * byte will be at 'load'. This may mean we need to load it starting
  170. * before then, since we can only read whole blocks.
  171. */
  172. data_offset += base_offset;
  173. sectors = get_aligned_image_size(info, data_size, data_offset);
  174. load_ptr = (void *)load;
  175. debug("U-Boot size %x, data %p\n", data_size, load_ptr);
  176. dst = load_ptr;
  177. /* Read the image */
  178. src_sector = sector + get_aligned_image_offset(info, data_offset);
  179. debug("Aligned image read: dst=%p, src_sector=%x, sectors=%x\n",
  180. dst, src_sector, sectors);
  181. count = info->read(info, src_sector, sectors, dst);
  182. if (count != sectors)
  183. return -EIO;
  184. debug("image: dst=%p, data_offset=%x, size=%x\n", dst, data_offset,
  185. data_size);
  186. src = dst + get_aligned_image_overhead(info, data_offset);
  187. #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS
  188. board_fit_image_post_process((void **)&src, (size_t *)&data_size);
  189. #endif
  190. memcpy(dst, src, data_size);
  191. /* Figure out which device tree the board wants to use */
  192. fdt_len = spl_fit_select_fdt(fit, images, &fdt_offset);
  193. if (fdt_len < 0)
  194. return fdt_len;
  195. /*
  196. * Read the device tree and place it after the image. There may be
  197. * some extra data before it since we can only read entire blocks.
  198. * And also align the destination address to ARCH_DMA_MINALIGN.
  199. */
  200. dst = (void *)((load + data_size + align_len) & ~align_len);
  201. fdt_offset += base_offset;
  202. sectors = get_aligned_image_size(info, fdt_len, fdt_offset);
  203. src_sector = sector + get_aligned_image_offset(info, fdt_offset);
  204. count = info->read(info, src_sector, sectors, dst);
  205. debug("Aligned fdt read: dst %p, src_sector = %x, sectors %x\n",
  206. dst, src_sector, sectors);
  207. if (count != sectors)
  208. return -EIO;
  209. /*
  210. * Copy the device tree so that it starts immediately after the image.
  211. * After this we will have the U-Boot image and its device tree ready
  212. * for us to start.
  213. */
  214. debug("fdt: dst=%p, data_offset=%x, size=%x\n", dst, fdt_offset,
  215. fdt_len);
  216. src = dst + get_aligned_image_overhead(info, fdt_offset);
  217. dst = load_ptr + data_size;
  218. #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS
  219. board_fit_image_post_process((void **)&src, (size_t *)&fdt_len);
  220. #endif
  221. memcpy(dst, src, fdt_len);
  222. return 0;
  223. }