spl_fit.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. /**
  13. * spl_fit_get_image_node(): By using the matching configuration subnode,
  14. * retrieve the name of an image, specified by a property name and an index
  15. * into that.
  16. * @fit: Pointer to the FDT blob.
  17. * @images: Offset of the /images subnode.
  18. * @type: Name of the property within the configuration subnode.
  19. * @index: Index into the list of strings in this property.
  20. *
  21. * Return: the node offset of the respective image node or a negative
  22. * error number.
  23. */
  24. static int spl_fit_get_image_node(const void *fit, int images,
  25. const char *type, int index)
  26. {
  27. const char *name, *str;
  28. int node, conf_node;
  29. int len, i;
  30. conf_node = fit_find_config_node(fit);
  31. if (conf_node < 0) {
  32. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  33. printf("No matching DT out of these options:\n");
  34. for (node = fdt_first_subnode(fit, conf_node);
  35. node >= 0;
  36. node = fdt_next_subnode(fit, node)) {
  37. name = fdt_getprop(fit, node, "description", &len);
  38. printf(" %s\n", name);
  39. }
  40. #endif
  41. return conf_node;
  42. }
  43. name = fdt_getprop(fit, conf_node, type, &len);
  44. if (!name) {
  45. debug("cannot find property '%s': %d\n", type, len);
  46. return -EINVAL;
  47. }
  48. str = name;
  49. for (i = 0; i < index; i++) {
  50. str = strchr(str, '\0') + 1;
  51. if (!str || (str - name >= len)) {
  52. debug("no string for index %d\n", index);
  53. return -E2BIG;
  54. }
  55. }
  56. debug("%s: '%s'\n", type, str);
  57. node = fdt_subnode_offset(fit, images, str);
  58. if (node < 0) {
  59. debug("cannot find image node '%s': %d\n", str, node);
  60. return -EINVAL;
  61. }
  62. return node;
  63. }
  64. static int get_aligned_image_offset(struct spl_load_info *info, int offset)
  65. {
  66. /*
  67. * If it is a FS read, get the first address before offset which is
  68. * aligned to ARCH_DMA_MINALIGN. If it is raw read return the
  69. * block number to which offset belongs.
  70. */
  71. if (info->filename)
  72. return offset & ~(ARCH_DMA_MINALIGN - 1);
  73. return offset / info->bl_len;
  74. }
  75. static int get_aligned_image_overhead(struct spl_load_info *info, int offset)
  76. {
  77. /*
  78. * If it is a FS read, get the difference between the offset and
  79. * the first address before offset which is aligned to
  80. * ARCH_DMA_MINALIGN. If it is raw read return the offset within the
  81. * block.
  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_size(struct spl_load_info *info, int data_size,
  88. int offset)
  89. {
  90. data_size = data_size + get_aligned_image_overhead(info, offset);
  91. if (info->filename)
  92. return data_size;
  93. return (data_size + info->bl_len - 1) / info->bl_len;
  94. }
  95. /**
  96. * spl_load_fit_image(): load the image described in a certain FIT node
  97. * @info: points to information about the device to load data from
  98. * @sector: the start sector of the FIT image on the device
  99. * @fit: points to the flattened device tree blob describing the FIT
  100. * image
  101. * @base_offset: the beginning of the data area containing the actual
  102. * image data, relative to the beginning of the FIT
  103. * @node: offset of the DT node describing the image to load (relative
  104. * to @fit)
  105. * @image_info: will be filled with information about the loaded image
  106. * If the FIT node does not contain a "load" (address) property,
  107. * the image gets loaded to the address pointed to by the
  108. * load_addr member in this struct.
  109. *
  110. * Return: 0 on success or a negative error number.
  111. */
  112. static int spl_load_fit_image(struct spl_load_info *info, ulong sector,
  113. void *fit, ulong base_offset, int node,
  114. struct spl_image_info *image_info)
  115. {
  116. ulong offset;
  117. size_t length;
  118. ulong load_addr, load_ptr;
  119. void *src;
  120. ulong overhead;
  121. int nr_sectors;
  122. int align_len = ARCH_DMA_MINALIGN - 1;
  123. offset = fdt_getprop_u32(fit, node, "data-offset");
  124. if (offset == FDT_ERROR)
  125. return -ENOENT;
  126. offset += base_offset;
  127. length = fdt_getprop_u32(fit, node, "data-size");
  128. if (length == FDT_ERROR)
  129. return -ENOENT;
  130. load_addr = fdt_getprop_u32(fit, node, "load");
  131. if (load_addr == FDT_ERROR && image_info)
  132. load_addr = image_info->load_addr;
  133. load_ptr = (load_addr + align_len) & ~align_len;
  134. overhead = get_aligned_image_overhead(info, offset);
  135. nr_sectors = get_aligned_image_size(info, length, offset);
  136. if (info->read(info, sector + get_aligned_image_offset(info, offset),
  137. nr_sectors, (void*)load_ptr) != nr_sectors)
  138. return -EIO;
  139. debug("image: dst=%lx, offset=%lx, size=%lx\n", load_ptr, offset,
  140. (unsigned long)length);
  141. src = (void *)load_ptr + overhead;
  142. #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS
  143. board_fit_image_post_process(&src, &length);
  144. #endif
  145. memcpy((void*)load_addr, src, length);
  146. if (image_info) {
  147. image_info->load_addr = load_addr;
  148. image_info->size = length;
  149. image_info->entry_point = fdt_getprop_u32(fit, node, "entry");
  150. }
  151. return 0;
  152. }
  153. int spl_load_simple_fit(struct spl_image_info *spl_image,
  154. struct spl_load_info *info, ulong sector, void *fit)
  155. {
  156. int sectors;
  157. ulong size;
  158. unsigned long count;
  159. struct spl_image_info image_info;
  160. int node, images, ret;
  161. int base_offset, align_len = ARCH_DMA_MINALIGN - 1;
  162. int index = 0;
  163. /*
  164. * Figure out where the external images start. This is the base for the
  165. * data-offset properties in each image.
  166. */
  167. size = fdt_totalsize(fit);
  168. size = (size + 3) & ~3;
  169. base_offset = (size + 3) & ~3;
  170. /*
  171. * So far we only have one block of data from the FIT. Read the entire
  172. * thing, including that first block, placing it so it finishes before
  173. * where we will load the image.
  174. *
  175. * Note that we will load the image such that its first byte will be
  176. * at the load address. Since that byte may be part-way through a
  177. * block, we may load the image up to one block before the load
  178. * address. So take account of that here by subtracting an addition
  179. * block length from the FIT start position.
  180. *
  181. * In fact the FIT has its own load address, but we assume it cannot
  182. * be before CONFIG_SYS_TEXT_BASE.
  183. */
  184. fit = (void *)((CONFIG_SYS_TEXT_BASE - size - info->bl_len -
  185. align_len) & ~align_len);
  186. sectors = get_aligned_image_size(info, size, 0);
  187. count = info->read(info, sector, sectors, fit);
  188. debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu\n",
  189. sector, sectors, fit, count);
  190. if (count == 0)
  191. return -EIO;
  192. /* find the node holding the images information */
  193. images = fdt_path_offset(fit, FIT_IMAGES_PATH);
  194. if (images < 0) {
  195. debug("%s: Cannot find /images node: %d\n", __func__, images);
  196. return -1;
  197. }
  198. /* find the U-Boot image */
  199. node = spl_fit_get_image_node(fit, images, "firmware", 0);
  200. if (node < 0) {
  201. debug("could not find firmware image, trying loadables...\n");
  202. node = spl_fit_get_image_node(fit, images, "loadables", 0);
  203. /*
  204. * If we pick the U-Boot image from "loadables", start at
  205. * the second image when later loading additional images.
  206. */
  207. index = 1;
  208. }
  209. if (node < 0) {
  210. debug("%s: Cannot find u-boot image node: %d\n",
  211. __func__, node);
  212. return -1;
  213. }
  214. /* Load the image and set up the spl_image structure */
  215. ret = spl_load_fit_image(info, sector, fit, base_offset, node,
  216. spl_image);
  217. if (ret)
  218. return ret;
  219. spl_image->os = IH_OS_U_BOOT;
  220. /* Figure out which device tree the board wants to use */
  221. node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP, 0);
  222. if (node < 0) {
  223. debug("%s: cannot find FDT node\n", __func__);
  224. return node;
  225. }
  226. /*
  227. * Read the device tree and place it after the image.
  228. * Align the destination address to ARCH_DMA_MINALIGN.
  229. */
  230. image_info.load_addr = spl_image->load_addr + spl_image->size;
  231. ret = spl_load_fit_image(info, sector, fit, base_offset, node,
  232. &image_info);
  233. if (ret < 0)
  234. return ret;
  235. /* Now check if there are more images for us to load */
  236. for (; ; index++) {
  237. node = spl_fit_get_image_node(fit, images, "loadables", index);
  238. if (node < 0)
  239. break;
  240. ret = spl_load_fit_image(info, sector, fit, base_offset, node,
  241. &image_info);
  242. if (ret < 0)
  243. continue;
  244. /*
  245. * If the "firmware" image did not provide an entry point,
  246. * use the first valid entry point from the loadables.
  247. */
  248. if (spl_image->entry_point == FDT_ERROR &&
  249. image_info.entry_point != FDT_ERROR)
  250. spl_image->entry_point = image_info.entry_point;
  251. }
  252. /*
  253. * If a platform does not provide CONFIG_SYS_UBOOT_START, U-Boot's
  254. * Makefile will set it to 0 and it will end up as the entry point
  255. * here. What it actually means is: use the load address.
  256. */
  257. if (spl_image->entry_point == FDT_ERROR || spl_image->entry_point == 0)
  258. spl_image->entry_point = spl_image->load_addr;
  259. return 0;
  260. }