spl_fit.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 int get_aligned_image_offset(struct spl_load_info *info, int offset)
  13. {
  14. /*
  15. * If it is a FS read, get the first address before offset which is
  16. * aligned to ARCH_DMA_MINALIGN. If it is raw read return the
  17. * block number to which offset belongs.
  18. */
  19. if (info->filename)
  20. return offset & ~(ARCH_DMA_MINALIGN - 1);
  21. return offset / info->bl_len;
  22. }
  23. static int get_aligned_image_overhead(struct spl_load_info *info, int offset)
  24. {
  25. /*
  26. * If it is a FS read, get the difference between the offset and
  27. * the first address before offset which is aligned to
  28. * ARCH_DMA_MINALIGN. If it is raw read return the offset within the
  29. * block.
  30. */
  31. if (info->filename)
  32. return offset & (ARCH_DMA_MINALIGN - 1);
  33. return offset % info->bl_len;
  34. }
  35. static int get_aligned_image_size(struct spl_load_info *info, int data_size,
  36. int offset)
  37. {
  38. data_size = data_size + get_aligned_image_overhead(info, offset);
  39. if (info->filename)
  40. return data_size;
  41. return (data_size + info->bl_len - 1) / info->bl_len;
  42. }
  43. int spl_load_simple_fit(struct spl_image_info *spl_image,
  44. struct spl_load_info *info, ulong sector, void *fit)
  45. {
  46. int sectors;
  47. ulong size, load;
  48. unsigned long count;
  49. int node, images;
  50. void *load_ptr;
  51. int fdt_offset, fdt_len;
  52. int data_offset, data_size;
  53. int base_offset, align_len = ARCH_DMA_MINALIGN - 1;
  54. int src_sector;
  55. void *dst, *src;
  56. /*
  57. * Figure out where the external images start. This is the base for the
  58. * data-offset properties in each image.
  59. */
  60. size = fdt_totalsize(fit);
  61. size = (size + 3) & ~3;
  62. base_offset = (size + 3) & ~3;
  63. /*
  64. * So far we only have one block of data from the FIT. Read the entire
  65. * thing, including that first block, placing it so it finishes before
  66. * where we will load the image.
  67. *
  68. * Note that we will load the image such that its first byte will be
  69. * at the load address. Since that byte may be part-way through a
  70. * block, we may load the image up to one block before the load
  71. * address. So take account of that here by subtracting an addition
  72. * block length from the FIT start position.
  73. *
  74. * In fact the FIT has its own load address, but we assume it cannot
  75. * be before CONFIG_SYS_TEXT_BASE.
  76. */
  77. fit = (void *)((CONFIG_SYS_TEXT_BASE - size - info->bl_len -
  78. align_len) & ~align_len);
  79. sectors = get_aligned_image_size(info, size, 0);
  80. count = info->read(info, sector, sectors, fit);
  81. debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu\n",
  82. sector, sectors, fit, count);
  83. if (count == 0)
  84. return -EIO;
  85. /* find the firmware image to load */
  86. images = fdt_path_offset(fit, FIT_IMAGES_PATH);
  87. if (images < 0) {
  88. debug("%s: Cannot find /images node: %d\n", __func__, images);
  89. return -1;
  90. }
  91. node = fdt_first_subnode(fit, images);
  92. if (node < 0) {
  93. debug("%s: Cannot find first image node: %d\n", __func__, node);
  94. return -1;
  95. }
  96. /* Get its information and set up the spl_image structure */
  97. data_offset = fdt_getprop_u32(fit, node, "data-offset");
  98. data_size = fdt_getprop_u32(fit, node, "data-size");
  99. load = fdt_getprop_u32(fit, node, "load");
  100. debug("data_offset=%x, data_size=%x\n", data_offset, data_size);
  101. spl_image->load_addr = load;
  102. spl_image->entry_point = load;
  103. spl_image->os = IH_OS_U_BOOT;
  104. /*
  105. * Work out where to place the image. We read it so that the first
  106. * byte will be at 'load'. This may mean we need to load it starting
  107. * before then, since we can only read whole blocks.
  108. */
  109. data_offset += base_offset;
  110. sectors = get_aligned_image_size(info, data_size, data_offset);
  111. load_ptr = (void *)load;
  112. debug("U-Boot size %x, data %p\n", data_size, load_ptr);
  113. dst = load_ptr;
  114. /* Read the image */
  115. src_sector = sector + get_aligned_image_offset(info, data_offset);
  116. debug("Aligned image read: dst=%p, src_sector=%x, sectors=%x\n",
  117. dst, src_sector, sectors);
  118. count = info->read(info, src_sector, sectors, dst);
  119. if (count != sectors)
  120. return -EIO;
  121. debug("image: dst=%p, data_offset=%x, size=%x\n", dst, data_offset,
  122. data_size);
  123. src = dst + get_aligned_image_overhead(info, data_offset);
  124. #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS
  125. board_fit_image_post_process((void **)&src, (size_t *)&data_size);
  126. #endif
  127. memcpy(dst, src, data_size);
  128. /* Figure out which device tree the board wants to use */
  129. fdt_len = fit_select_fdt(fit, images, &fdt_offset);
  130. if (fdt_len < 0)
  131. return fdt_len;
  132. /*
  133. * Read the device tree and place it after the image. There may be
  134. * some extra data before it since we can only read entire blocks.
  135. * And also align the destination address to ARCH_DMA_MINALIGN.
  136. */
  137. dst = (void *)((load + data_size + align_len) & ~align_len);
  138. fdt_offset += base_offset;
  139. sectors = get_aligned_image_size(info, fdt_len, fdt_offset);
  140. src_sector = sector + get_aligned_image_offset(info, fdt_offset);
  141. count = info->read(info, src_sector, sectors, dst);
  142. debug("Aligned fdt read: dst %p, src_sector = %x, sectors %x\n",
  143. dst, src_sector, sectors);
  144. if (count != sectors)
  145. return -EIO;
  146. /*
  147. * Copy the device tree so that it starts immediately after the image.
  148. * After this we will have the U-Boot image and its device tree ready
  149. * for us to start.
  150. */
  151. debug("fdt: dst=%p, data_offset=%x, size=%x\n", dst, fdt_offset,
  152. fdt_len);
  153. src = dst + get_aligned_image_overhead(info, fdt_offset);
  154. dst = load_ptr + data_size;
  155. #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS
  156. board_fit_image_post_process((void **)&src, (size_t *)&fdt_len);
  157. #endif
  158. memcpy(dst, src, fdt_len);
  159. return 0;
  160. }