spl_fit.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. return -EINVAL;
  39. if (board_fit_config_name_match(name))
  40. continue;
  41. debug("Selecting config '%s'", name);
  42. fdt_name = fdt_getprop(fdt, node, FIT_FDT_PROP, &len);
  43. if (!fdt_name) {
  44. debug("%s: Cannot find fdt name property: %d\n",
  45. __func__, len);
  46. return -EINVAL;
  47. }
  48. debug(", fdt '%s'\n", fdt_name);
  49. fdt_node = fdt_subnode_offset(fdt, images, fdt_name);
  50. if (fdt_node < 0) {
  51. debug("%s: Cannot find fdt node '%s': %d\n",
  52. __func__, fdt_name, fdt_node);
  53. return -EINVAL;
  54. }
  55. *fdt_offsetp = fdt_getprop_u32(fdt, fdt_node, "data-offset");
  56. len = fdt_getprop_u32(fdt, fdt_node, "data-size");
  57. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  58. printf("FIT: Selected '%s'\n", name);
  59. #endif
  60. return len;
  61. }
  62. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  63. printf("No matching DT out of these options:\n");
  64. for (node = fdt_first_subnode(fdt, conf);
  65. node >= 0;
  66. node = fdt_next_subnode(fdt, node)) {
  67. name = fdt_getprop(fdt, node, "name", &len);
  68. printf(" %s\n", name);
  69. }
  70. #endif
  71. return -ENOENT;
  72. }
  73. int spl_load_simple_fit(struct spl_load_info *info, ulong sector, void *fit)
  74. {
  75. int sectors;
  76. ulong size, load;
  77. unsigned long count;
  78. int node, images;
  79. void *load_ptr;
  80. int fdt_offset, fdt_len;
  81. int data_offset, data_size;
  82. int base_offset;
  83. int src_sector;
  84. void *dst;
  85. /*
  86. * Figure out where the external images start. This is the base for the
  87. * data-offset properties in each image.
  88. */
  89. size = fdt_totalsize(fit);
  90. size = (size + 3) & ~3;
  91. base_offset = (size + 3) & ~3;
  92. /*
  93. * So far we only have one block of data from the FIT. Read the entire
  94. * thing, including that first block, placing it so it finishes before
  95. * where we will load the image.
  96. *
  97. * Note that we will load the image such that its first byte will be
  98. * at the load address. Since that byte may be part-way through a
  99. * block, we may load the image up to one block before the load
  100. * address. So take account of that here by subtracting an addition
  101. * block length from the FIT start position.
  102. *
  103. * In fact the FIT has its own load address, but we assume it cannot
  104. * be before CONFIG_SYS_TEXT_BASE.
  105. */
  106. fit = (void *)(CONFIG_SYS_TEXT_BASE - size - info->bl_len);
  107. sectors = (size + info->bl_len - 1) / info->bl_len;
  108. count = info->read(info, sector, sectors, fit);
  109. debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu\n",
  110. sector, sectors, fit, count);
  111. if (count == 0)
  112. return -EIO;
  113. /* find the firmware image to load */
  114. images = fdt_path_offset(fit, FIT_IMAGES_PATH);
  115. if (images < 0) {
  116. debug("%s: Cannot find /images node: %d\n", __func__, images);
  117. return -1;
  118. }
  119. node = fdt_first_subnode(fit, images);
  120. if (node < 0) {
  121. debug("%s: Cannot find first image node: %d\n", __func__, node);
  122. return -1;
  123. }
  124. /* Get its information and set up the spl_image structure */
  125. data_offset = fdt_getprop_u32(fit, node, "data-offset");
  126. data_size = fdt_getprop_u32(fit, node, "data-size");
  127. load = fdt_getprop_u32(fit, node, "load");
  128. debug("data_offset=%x, data_size=%x\n", data_offset, data_size);
  129. spl_image.load_addr = load;
  130. spl_image.entry_point = load;
  131. spl_image.os = IH_OS_U_BOOT;
  132. /*
  133. * Work out where to place the image. We read it so that the first
  134. * byte will be at 'load'. This may mean we need to load it starting
  135. * before then, since we can only read whole blocks.
  136. */
  137. sectors = (data_size + info->bl_len - 1) / info->bl_len;
  138. data_offset += base_offset;
  139. load_ptr = (void *)load;
  140. debug("U-Boot size %x, data %p\n", data_size, load_ptr);
  141. dst = load_ptr - (data_offset % info->bl_len);
  142. /* Read the image */
  143. src_sector = sector + data_offset / info->bl_len;
  144. debug("image: data_offset=%x, dst=%p, src_sector=%x, sectors=%x\n",
  145. data_offset, dst, src_sector, sectors);
  146. count = info->read(info, src_sector, sectors, dst);
  147. if (count != sectors)
  148. return -EIO;
  149. /* Figure out which device tree the board wants to use */
  150. fdt_len = spl_fit_select_fdt(fit, images, &fdt_offset);
  151. if (fdt_len < 0)
  152. return fdt_len;
  153. /*
  154. * Read the device tree and place it after the image. There may be
  155. * some extra data before it since we can only read entire blocks.
  156. */
  157. dst = load_ptr + data_size;
  158. fdt_offset += base_offset;
  159. count = info->read(info, sector + fdt_offset / info->bl_len, sectors,
  160. dst);
  161. debug("fit read %x sectors to %x, dst %p, data_offset %x\n",
  162. sectors, spl_image.load_addr, dst, fdt_offset);
  163. if (count != sectors)
  164. return -EIO;
  165. /*
  166. * Copy the device tree so that it starts immediately after the image.
  167. * After this we will have the U-Boot image and its device tree ready
  168. * for us to start.
  169. */
  170. memcpy(dst, dst + fdt_offset % info->bl_len, fdt_len);
  171. return 0;
  172. }