spl_fit.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. #ifndef CONFIG_SYS_BOOTM_LEN
  13. #define CONFIG_SYS_BOOTM_LEN (64 << 20)
  14. #endif
  15. /**
  16. * spl_fit_get_image_name(): By using the matching configuration subnode,
  17. * retrieve the name of an image, specified by a property name and an index
  18. * into that.
  19. * @fit: Pointer to the FDT blob.
  20. * @images: Offset of the /images subnode.
  21. * @type: Name of the property within the configuration subnode.
  22. * @index: Index into the list of strings in this property.
  23. * @outname: Name of the image
  24. *
  25. * Return: 0 on success, or a negative error number
  26. */
  27. static int spl_fit_get_image_name(const void *fit, int images,
  28. const char *type, int index,
  29. char **outname)
  30. {
  31. const char *name, *str;
  32. __maybe_unused int node;
  33. int conf_node;
  34. int len, i;
  35. conf_node = fit_find_config_node(fit);
  36. if (conf_node < 0) {
  37. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  38. printf("No matching DT out of these options:\n");
  39. for (node = fdt_first_subnode(fit, conf_node);
  40. node >= 0;
  41. node = fdt_next_subnode(fit, node)) {
  42. name = fdt_getprop(fit, node, "description", &len);
  43. printf(" %s\n", name);
  44. }
  45. #endif
  46. return conf_node;
  47. }
  48. name = fdt_getprop(fit, conf_node, type, &len);
  49. if (!name) {
  50. debug("cannot find property '%s': %d\n", type, len);
  51. return -EINVAL;
  52. }
  53. str = name;
  54. for (i = 0; i < index; i++) {
  55. str = strchr(str, '\0') + 1;
  56. if (!str || (str - name >= len)) {
  57. debug("no string for index %d\n", index);
  58. return -E2BIG;
  59. }
  60. }
  61. *outname = (char *)str;
  62. return 0;
  63. }
  64. /**
  65. * spl_fit_get_image_node(): By using the matching configuration subnode,
  66. * retrieve the name of an image, specified by a property name and an index
  67. * into that.
  68. * @fit: Pointer to the FDT blob.
  69. * @images: Offset of the /images subnode.
  70. * @type: Name of the property within the configuration subnode.
  71. * @index: Index into the list of strings in this property.
  72. *
  73. * Return: the node offset of the respective image node or a negative
  74. * error number.
  75. */
  76. static int spl_fit_get_image_node(const void *fit, int images,
  77. const char *type, int index)
  78. {
  79. char *str;
  80. int err;
  81. int node;
  82. err = spl_fit_get_image_name(fit, images, type, index, &str);
  83. if (err)
  84. return err;
  85. debug("%s: '%s'\n", type, str);
  86. node = fdt_subnode_offset(fit, images, str);
  87. if (node < 0) {
  88. debug("cannot find image node '%s': %d\n", str, node);
  89. return -EINVAL;
  90. }
  91. return node;
  92. }
  93. static int get_aligned_image_offset(struct spl_load_info *info, int offset)
  94. {
  95. /*
  96. * If it is a FS read, get the first address before offset which is
  97. * aligned to ARCH_DMA_MINALIGN. If it is raw read return the
  98. * block number to which offset belongs.
  99. */
  100. if (info->filename)
  101. return offset & ~(ARCH_DMA_MINALIGN - 1);
  102. return offset / info->bl_len;
  103. }
  104. static int get_aligned_image_overhead(struct spl_load_info *info, int offset)
  105. {
  106. /*
  107. * If it is a FS read, get the difference between the offset and
  108. * the first address before offset which is aligned to
  109. * ARCH_DMA_MINALIGN. If it is raw read return the offset within the
  110. * block.
  111. */
  112. if (info->filename)
  113. return offset & (ARCH_DMA_MINALIGN - 1);
  114. return offset % info->bl_len;
  115. }
  116. static int get_aligned_image_size(struct spl_load_info *info, int data_size,
  117. int offset)
  118. {
  119. data_size = data_size + get_aligned_image_overhead(info, offset);
  120. if (info->filename)
  121. return data_size;
  122. return (data_size + info->bl_len - 1) / info->bl_len;
  123. }
  124. /**
  125. * spl_load_fit_image(): load the image described in a certain FIT node
  126. * @info: points to information about the device to load data from
  127. * @sector: the start sector of the FIT image on the device
  128. * @fit: points to the flattened device tree blob describing the FIT
  129. * image
  130. * @base_offset: the beginning of the data area containing the actual
  131. * image data, relative to the beginning of the FIT
  132. * @node: offset of the DT node describing the image to load (relative
  133. * to @fit)
  134. * @image_info: will be filled with information about the loaded image
  135. * If the FIT node does not contain a "load" (address) property,
  136. * the image gets loaded to the address pointed to by the
  137. * load_addr member in this struct.
  138. *
  139. * Return: 0 on success or a negative error number.
  140. */
  141. static int spl_load_fit_image(struct spl_load_info *info, ulong sector,
  142. void *fit, ulong base_offset, int node,
  143. struct spl_image_info *image_info)
  144. {
  145. int offset;
  146. size_t length;
  147. int len;
  148. ulong size;
  149. ulong load_addr, load_ptr;
  150. void *src;
  151. ulong overhead;
  152. int nr_sectors;
  153. int align_len = ARCH_DMA_MINALIGN - 1;
  154. uint8_t image_comp = -1, type = -1;
  155. const void *data;
  156. if (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_SPL_GZIP)) {
  157. if (fit_image_get_comp(fit, node, &image_comp))
  158. puts("Cannot get image compression format.\n");
  159. else
  160. debug("%s ", genimg_get_comp_name(image_comp));
  161. if (fit_image_get_type(fit, node, &type))
  162. puts("Cannot get image type.\n");
  163. else
  164. debug("%s ", genimg_get_type_name(type));
  165. }
  166. if (fit_image_get_load(fit, node, &load_addr))
  167. load_addr = image_info->load_addr;
  168. if (!fit_image_get_data_offset(fit, node, &offset)) {
  169. /* External data */
  170. offset += base_offset;
  171. if (fit_image_get_data_size(fit, node, &len))
  172. return -ENOENT;
  173. load_ptr = (load_addr + align_len) & ~align_len;
  174. length = len;
  175. overhead = get_aligned_image_overhead(info, offset);
  176. nr_sectors = get_aligned_image_size(info, length, offset);
  177. if (info->read(info,
  178. sector + get_aligned_image_offset(info, offset),
  179. nr_sectors, (void *)load_ptr) != nr_sectors)
  180. return -EIO;
  181. debug("External data: dst=%lx, offset=%x, size=%lx\n",
  182. load_ptr, offset, (unsigned long)length);
  183. src = (void *)load_ptr + overhead;
  184. } else {
  185. /* Embedded data */
  186. if (fit_image_get_data(fit, node, &data, &length)) {
  187. puts("Cannot get image data/size\n");
  188. return -ENOENT;
  189. }
  190. debug("Embedded data: dst=%lx, size=%lx\n", load_addr,
  191. (unsigned long)length);
  192. src = (void *)data;
  193. }
  194. #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS
  195. board_fit_image_post_process(&src, &length);
  196. #endif
  197. if (IS_ENABLED(CONFIG_SPL_OS_BOOT) &&
  198. IS_ENABLED(CONFIG_SPL_GZIP) &&
  199. image_comp == IH_COMP_GZIP &&
  200. type == IH_TYPE_KERNEL) {
  201. size = length;
  202. if (gunzip((void *)load_addr, CONFIG_SYS_BOOTM_LEN,
  203. src, &size)) {
  204. puts("Uncompressing error\n");
  205. return -EIO;
  206. }
  207. length = size;
  208. } else {
  209. memcpy((void *)load_addr, src, length);
  210. }
  211. if (image_info) {
  212. image_info->load_addr = load_addr;
  213. image_info->size = length;
  214. image_info->entry_point = fdt_getprop_u32(fit, node, "entry");
  215. }
  216. return 0;
  217. }
  218. static int spl_fit_append_fdt(struct spl_image_info *spl_image,
  219. struct spl_load_info *info, ulong sector,
  220. void *fit, int images, ulong base_offset)
  221. {
  222. struct spl_image_info image_info;
  223. int node, ret;
  224. /* Figure out which device tree the board wants to use */
  225. node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP, 0);
  226. if (node < 0) {
  227. debug("%s: cannot find FDT node\n", __func__);
  228. return node;
  229. }
  230. /*
  231. * Read the device tree and place it after the image.
  232. * Align the destination address to ARCH_DMA_MINALIGN.
  233. */
  234. image_info.load_addr = spl_image->load_addr + spl_image->size;
  235. ret = spl_load_fit_image(info, sector, fit, base_offset, node,
  236. &image_info);
  237. if (ret < 0)
  238. return ret;
  239. /* Make the load-address of the FDT available for the SPL framework */
  240. spl_image->fdt_addr = (void *)image_info.load_addr;
  241. #if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
  242. /* Try to make space, so we can inject details on the loadables */
  243. ret = fdt_shrink_to_minimum(spl_image->fdt_addr, 8192);
  244. #endif
  245. return ret;
  246. }
  247. static int spl_fit_record_loadable(const void *fit, int images, int index,
  248. void *blob, struct spl_image_info *image)
  249. {
  250. int ret = 0;
  251. #if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
  252. char *name;
  253. int node;
  254. ret = spl_fit_get_image_name(fit, images, "loadables",
  255. index, &name);
  256. if (ret < 0)
  257. return ret;
  258. node = spl_fit_get_image_node(fit, images, "loadables", index);
  259. ret = fdt_record_loadable(blob, index, name, image->load_addr,
  260. image->size, image->entry_point,
  261. fdt_getprop(fit, node, "type", NULL),
  262. fdt_getprop(fit, node, "os", NULL));
  263. #endif
  264. return ret;
  265. }
  266. static int spl_fit_image_get_os(const void *fit, int noffset, uint8_t *os)
  267. {
  268. #if CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
  269. return -ENOTSUPP;
  270. #else
  271. return fit_image_get_os(fit, noffset, os);
  272. #endif
  273. }
  274. int spl_load_simple_fit(struct spl_image_info *spl_image,
  275. struct spl_load_info *info, ulong sector, void *fit)
  276. {
  277. int sectors;
  278. ulong size;
  279. unsigned long count;
  280. struct spl_image_info image_info;
  281. int node = -1;
  282. int images, ret;
  283. int base_offset, align_len = ARCH_DMA_MINALIGN - 1;
  284. int index = 0;
  285. /*
  286. * For FIT with external data, figure out where the external images
  287. * start. This is the base for the data-offset properties in each
  288. * image.
  289. */
  290. size = fdt_totalsize(fit);
  291. size = (size + 3) & ~3;
  292. base_offset = (size + 3) & ~3;
  293. /*
  294. * So far we only have one block of data from the FIT. Read the entire
  295. * thing, including that first block, placing it so it finishes before
  296. * where we will load the image.
  297. *
  298. * Note that we will load the image such that its first byte will be
  299. * at the load address. Since that byte may be part-way through a
  300. * block, we may load the image up to one block before the load
  301. * address. So take account of that here by subtracting an addition
  302. * block length from the FIT start position.
  303. *
  304. * In fact the FIT has its own load address, but we assume it cannot
  305. * be before CONFIG_SYS_TEXT_BASE.
  306. *
  307. * For FIT with data embedded, data is loaded as part of FIT image.
  308. * For FIT with external data, data is not loaded in this step.
  309. */
  310. fit = (void *)((CONFIG_SYS_TEXT_BASE - size - info->bl_len -
  311. align_len) & ~align_len);
  312. sectors = get_aligned_image_size(info, size, 0);
  313. count = info->read(info, sector, sectors, fit);
  314. debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu\n",
  315. sector, sectors, fit, count);
  316. if (count == 0)
  317. return -EIO;
  318. /* find the node holding the images information */
  319. images = fdt_path_offset(fit, FIT_IMAGES_PATH);
  320. if (images < 0) {
  321. debug("%s: Cannot find /images node: %d\n", __func__, images);
  322. return -1;
  323. }
  324. /*
  325. * Find the U-Boot image using the following search order:
  326. * - start at 'firmware' (e.g. an ARM Trusted Firmware)
  327. * - fall back 'kernel' (e.g. a Falcon-mode OS boot
  328. * - fall back to using the first 'loadables' entry
  329. */
  330. if (node < 0)
  331. node = spl_fit_get_image_node(fit, images, "firmware", 0);
  332. #ifdef CONFIG_SPL_OS_BOOT
  333. if (node < 0)
  334. node = spl_fit_get_image_node(fit, images, FIT_KERNEL_PROP, 0);
  335. #endif
  336. if (node < 0) {
  337. debug("could not find firmware image, trying loadables...\n");
  338. node = spl_fit_get_image_node(fit, images, "loadables", 0);
  339. /*
  340. * If we pick the U-Boot image from "loadables", start at
  341. * the second image when later loading additional images.
  342. */
  343. index = 1;
  344. }
  345. if (node < 0) {
  346. debug("%s: Cannot find u-boot image node: %d\n",
  347. __func__, node);
  348. return -1;
  349. }
  350. /* Load the image and set up the spl_image structure */
  351. ret = spl_load_fit_image(info, sector, fit, base_offset, node,
  352. spl_image);
  353. if (ret)
  354. return ret;
  355. /*
  356. * For backward compatibility, we treat the first node that is
  357. * as a U-Boot image, if no OS-type has been declared.
  358. */
  359. if (!spl_fit_image_get_os(fit, node, &spl_image->os))
  360. debug("Image OS is %s\n", genimg_get_os_name(spl_image->os));
  361. #if !defined(CONFIG_SPL_OS_BOOT)
  362. else
  363. spl_image->os = IH_OS_U_BOOT;
  364. #endif
  365. /*
  366. * Booting a next-stage U-Boot may require us to append the FDT.
  367. * We allow this to fail, as the U-Boot image might embed its FDT.
  368. */
  369. if (spl_image->os == IH_OS_U_BOOT)
  370. spl_fit_append_fdt(spl_image, info, sector, fit,
  371. images, base_offset);
  372. /* Now check if there are more images for us to load */
  373. for (; ; index++) {
  374. uint8_t os_type = IH_OS_INVALID;
  375. node = spl_fit_get_image_node(fit, images, "loadables", index);
  376. if (node < 0)
  377. break;
  378. ret = spl_load_fit_image(info, sector, fit, base_offset, node,
  379. &image_info);
  380. if (ret < 0)
  381. continue;
  382. if (!spl_fit_image_get_os(fit, node, &os_type))
  383. debug("Loadable is %s\n", genimg_get_os_name(os_type));
  384. if (os_type == IH_OS_U_BOOT) {
  385. spl_fit_append_fdt(&image_info, info, sector,
  386. fit, images, base_offset);
  387. spl_image->fdt_addr = image_info.fdt_addr;
  388. }
  389. /*
  390. * If the "firmware" image did not provide an entry point,
  391. * use the first valid entry point from the loadables.
  392. */
  393. if (spl_image->entry_point == FDT_ERROR &&
  394. image_info.entry_point != FDT_ERROR)
  395. spl_image->entry_point = image_info.entry_point;
  396. /* Record our loadables into the FDT */
  397. if (spl_image->fdt_addr)
  398. spl_fit_record_loadable(fit, images, index,
  399. spl_image->fdt_addr,
  400. &image_info);
  401. }
  402. /*
  403. * If a platform does not provide CONFIG_SYS_UBOOT_START, U-Boot's
  404. * Makefile will set it to 0 and it will end up as the entry point
  405. * here. What it actually means is: use the load address.
  406. */
  407. if (spl_image->entry_point == FDT_ERROR || spl_image->entry_point == 0)
  408. spl_image->entry_point = spl_image->load_addr;
  409. return 0;
  410. }