spl_fit.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2016 Google, Inc
  4. * Written by Simon Glass <sjg@chromium.org>
  5. */
  6. #include <common.h>
  7. #include <errno.h>
  8. #include <fpga.h>
  9. #include <gzip.h>
  10. #include <image.h>
  11. #include <log.h>
  12. #include <malloc.h>
  13. #include <mapmem.h>
  14. #include <spl.h>
  15. #include <sysinfo.h>
  16. #include <asm/cache.h>
  17. #include <asm/global_data.h>
  18. #include <linux/libfdt.h>
  19. DECLARE_GLOBAL_DATA_PTR;
  20. #ifndef CONFIG_SPL_LOAD_FIT_APPLY_OVERLAY_BUF_SZ
  21. #define CONFIG_SPL_LOAD_FIT_APPLY_OVERLAY_BUF_SZ (64 * 1024)
  22. #endif
  23. #ifndef CONFIG_SYS_BOOTM_LEN
  24. #define CONFIG_SYS_BOOTM_LEN (64 << 20)
  25. #endif
  26. struct spl_fit_info {
  27. const void *fit; /* Pointer to a valid FIT blob */
  28. size_t ext_data_offset; /* Offset to FIT external data (end of FIT) */
  29. int images_node; /* FDT offset to "/images" node */
  30. int conf_node; /* FDT offset to selected configuration node */
  31. };
  32. __weak void board_spl_fit_post_load(const void *fit)
  33. {
  34. }
  35. __weak ulong board_spl_fit_size_align(ulong size)
  36. {
  37. return size;
  38. }
  39. static int find_node_from_desc(const void *fit, int node, const char *str)
  40. {
  41. int child;
  42. if (node < 0)
  43. return -EINVAL;
  44. /* iterate the FIT nodes and find a matching description */
  45. for (child = fdt_first_subnode(fit, node); child >= 0;
  46. child = fdt_next_subnode(fit, child)) {
  47. int len;
  48. const char *desc = fdt_getprop(fit, child, "description", &len);
  49. if (!desc)
  50. continue;
  51. if (!strcmp(desc, str))
  52. return child;
  53. }
  54. return -ENOENT;
  55. }
  56. /**
  57. * spl_fit_get_image_name(): By using the matching configuration subnode,
  58. * retrieve the name of an image, specified by a property name and an index
  59. * into that.
  60. * @fit: Pointer to the FDT blob.
  61. * @images: Offset of the /images subnode.
  62. * @type: Name of the property within the configuration subnode.
  63. * @index: Index into the list of strings in this property.
  64. * @outname: Name of the image
  65. *
  66. * Return: 0 on success, or a negative error number
  67. */
  68. static int spl_fit_get_image_name(const struct spl_fit_info *ctx,
  69. const char *type, int index,
  70. const char **outname)
  71. {
  72. struct udevice *sysinfo;
  73. const char *name, *str;
  74. __maybe_unused int node;
  75. int len, i;
  76. bool found = true;
  77. name = fdt_getprop(ctx->fit, ctx->conf_node, type, &len);
  78. if (!name) {
  79. debug("cannot find property '%s': %d\n", type, len);
  80. return -EINVAL;
  81. }
  82. str = name;
  83. for (i = 0; i < index; i++) {
  84. str = strchr(str, '\0') + 1;
  85. if (!str || (str - name >= len)) {
  86. found = false;
  87. break;
  88. }
  89. }
  90. if (!found && CONFIG_IS_ENABLED(SYSINFO) && !sysinfo_get(&sysinfo)) {
  91. int rc;
  92. /*
  93. * no string in the property for this index. Check if the
  94. * sysinfo-level code can supply one.
  95. */
  96. rc = sysinfo_detect(sysinfo);
  97. if (rc)
  98. return rc;
  99. rc = sysinfo_get_fit_loadable(sysinfo, index - i - 1, type,
  100. &str);
  101. if (rc && rc != -ENOENT)
  102. return rc;
  103. if (!rc) {
  104. /*
  105. * The sysinfo provided a name for a loadable.
  106. * Try to match it against the description properties
  107. * first. If no matching node is found, use it as a
  108. * node name.
  109. */
  110. int node;
  111. int images = fdt_path_offset(ctx->fit, FIT_IMAGES_PATH);
  112. node = find_node_from_desc(ctx->fit, images, str);
  113. if (node > 0)
  114. str = fdt_get_name(ctx->fit, node, NULL);
  115. found = true;
  116. }
  117. }
  118. if (!found) {
  119. debug("no string for index %d\n", index);
  120. return -E2BIG;
  121. }
  122. *outname = str;
  123. return 0;
  124. }
  125. /**
  126. * spl_fit_get_image_node(): By using the matching configuration subnode,
  127. * retrieve the name of an image, specified by a property name and an index
  128. * into that.
  129. * @fit: Pointer to the FDT blob.
  130. * @images: Offset of the /images subnode.
  131. * @type: Name of the property within the configuration subnode.
  132. * @index: Index into the list of strings in this property.
  133. *
  134. * Return: the node offset of the respective image node or a negative
  135. * error number.
  136. */
  137. static int spl_fit_get_image_node(const struct spl_fit_info *ctx,
  138. const char *type, int index)
  139. {
  140. const char *str;
  141. int err;
  142. int node;
  143. err = spl_fit_get_image_name(ctx, type, index, &str);
  144. if (err)
  145. return err;
  146. debug("%s: '%s'\n", type, str);
  147. node = fdt_subnode_offset(ctx->fit, ctx->images_node, str);
  148. if (node < 0) {
  149. pr_err("cannot find image node '%s': %d\n", str, node);
  150. return -EINVAL;
  151. }
  152. return node;
  153. }
  154. static int get_aligned_image_offset(struct spl_load_info *info, int offset)
  155. {
  156. /*
  157. * If it is a FS read, get the first address before offset which is
  158. * aligned to ARCH_DMA_MINALIGN. If it is raw read return the
  159. * block number to which offset belongs.
  160. */
  161. if (info->filename)
  162. return offset & ~(ARCH_DMA_MINALIGN - 1);
  163. return offset / info->bl_len;
  164. }
  165. static int get_aligned_image_overhead(struct spl_load_info *info, int offset)
  166. {
  167. /*
  168. * If it is a FS read, get the difference between the offset and
  169. * the first address before offset which is aligned to
  170. * ARCH_DMA_MINALIGN. If it is raw read return the offset within the
  171. * block.
  172. */
  173. if (info->filename)
  174. return offset & (ARCH_DMA_MINALIGN - 1);
  175. return offset % info->bl_len;
  176. }
  177. static int get_aligned_image_size(struct spl_load_info *info, int data_size,
  178. int offset)
  179. {
  180. data_size = data_size + get_aligned_image_overhead(info, offset);
  181. if (info->filename)
  182. return data_size;
  183. return (data_size + info->bl_len - 1) / info->bl_len;
  184. }
  185. /**
  186. * spl_load_fit_image(): load the image described in a certain FIT node
  187. * @info: points to information about the device to load data from
  188. * @sector: the start sector of the FIT image on the device
  189. * @ctx: points to the FIT context structure
  190. * @node: offset of the DT node describing the image to load (relative
  191. * to @fit)
  192. * @image_info: will be filled with information about the loaded image
  193. * If the FIT node does not contain a "load" (address) property,
  194. * the image gets loaded to the address pointed to by the
  195. * load_addr member in this struct, if load_addr is not 0
  196. *
  197. * Return: 0 on success or a negative error number.
  198. */
  199. static int spl_load_fit_image(struct spl_load_info *info, ulong sector,
  200. const struct spl_fit_info *ctx, int node,
  201. struct spl_image_info *image_info)
  202. {
  203. int offset;
  204. size_t length;
  205. int len;
  206. ulong size;
  207. ulong load_addr;
  208. void *load_ptr;
  209. void *src;
  210. ulong overhead;
  211. int nr_sectors;
  212. uint8_t image_comp = -1, type = -1;
  213. const void *data;
  214. const void *fit = ctx->fit;
  215. bool external_data = false;
  216. if (IS_ENABLED(CONFIG_SPL_FPGA) ||
  217. (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_SPL_GZIP))) {
  218. if (fit_image_get_type(fit, node, &type))
  219. puts("Cannot get image type.\n");
  220. else
  221. debug("%s ", genimg_get_type_name(type));
  222. }
  223. if (IS_ENABLED(CONFIG_SPL_GZIP)) {
  224. fit_image_get_comp(fit, node, &image_comp);
  225. debug("%s ", genimg_get_comp_name(image_comp));
  226. }
  227. if (fit_image_get_load(fit, node, &load_addr)) {
  228. if (!image_info->load_addr) {
  229. printf("Can't load %s: No load address and no buffer\n",
  230. fit_get_name(fit, node, NULL));
  231. return -ENOBUFS;
  232. }
  233. load_addr = image_info->load_addr;
  234. }
  235. if (!fit_image_get_data_position(fit, node, &offset)) {
  236. external_data = true;
  237. } else if (!fit_image_get_data_offset(fit, node, &offset)) {
  238. offset += ctx->ext_data_offset;
  239. external_data = true;
  240. }
  241. if (external_data) {
  242. void *src_ptr;
  243. /* External data */
  244. if (fit_image_get_data_size(fit, node, &len))
  245. return -ENOENT;
  246. src_ptr = map_sysmem(ALIGN(load_addr, ARCH_DMA_MINALIGN), len);
  247. length = len;
  248. overhead = get_aligned_image_overhead(info, offset);
  249. nr_sectors = get_aligned_image_size(info, length, offset);
  250. if (info->read(info,
  251. sector + get_aligned_image_offset(info, offset),
  252. nr_sectors, src_ptr) != nr_sectors)
  253. return -EIO;
  254. debug("External data: dst=%p, offset=%x, size=%lx\n",
  255. src_ptr, offset, (unsigned long)length);
  256. src = src_ptr + overhead;
  257. } else {
  258. /* Embedded data */
  259. if (fit_image_get_data(fit, node, &data, &length)) {
  260. puts("Cannot get image data/size\n");
  261. return -ENOENT;
  262. }
  263. debug("Embedded data: dst=%lx, size=%lx\n", load_addr,
  264. (unsigned long)length);
  265. src = (void *)data; /* cast away const */
  266. }
  267. if (CONFIG_IS_ENABLED(FIT_SIGNATURE)) {
  268. printf("## Checking hash(es) for Image %s ... ",
  269. fit_get_name(fit, node, NULL));
  270. if (!fit_image_verify_with_data(fit, node, src, length))
  271. return -EPERM;
  272. puts("OK\n");
  273. }
  274. if (CONFIG_IS_ENABLED(FIT_IMAGE_POST_PROCESS))
  275. board_fit_image_post_process(&src, &length);
  276. load_ptr = map_sysmem(load_addr, length);
  277. if (IS_ENABLED(CONFIG_SPL_GZIP) && image_comp == IH_COMP_GZIP) {
  278. size = length;
  279. if (gunzip(load_ptr, CONFIG_SYS_BOOTM_LEN, src, &size)) {
  280. puts("Uncompressing error\n");
  281. return -EIO;
  282. }
  283. length = size;
  284. } else {
  285. memcpy(load_ptr, src, length);
  286. }
  287. if (image_info) {
  288. ulong entry_point;
  289. image_info->load_addr = load_addr;
  290. image_info->size = length;
  291. if (!fit_image_get_entry(fit, node, &entry_point))
  292. image_info->entry_point = entry_point;
  293. else
  294. image_info->entry_point = FDT_ERROR;
  295. }
  296. return 0;
  297. }
  298. static bool os_takes_devicetree(uint8_t os)
  299. {
  300. switch (os) {
  301. case IH_OS_U_BOOT:
  302. return true;
  303. case IH_OS_LINUX:
  304. return IS_ENABLED(CONFIG_SPL_OS_BOOT);
  305. default:
  306. return false;
  307. }
  308. }
  309. static int spl_fit_append_fdt(struct spl_image_info *spl_image,
  310. struct spl_load_info *info, ulong sector,
  311. const struct spl_fit_info *ctx)
  312. {
  313. struct spl_image_info image_info;
  314. int node, ret = 0, index = 0;
  315. /*
  316. * Use the address following the image as target address for the
  317. * device tree.
  318. */
  319. image_info.load_addr = spl_image->load_addr + spl_image->size;
  320. /* Figure out which device tree the board wants to use */
  321. node = spl_fit_get_image_node(ctx, FIT_FDT_PROP, index++);
  322. if (node < 0) {
  323. debug("%s: cannot find FDT node\n", __func__);
  324. /*
  325. * U-Boot did not find a device tree inside the FIT image. Use
  326. * the U-Boot device tree instead.
  327. */
  328. if (gd->fdt_blob)
  329. memcpy((void *)image_info.load_addr, gd->fdt_blob,
  330. fdt_totalsize(gd->fdt_blob));
  331. else
  332. return node;
  333. } else {
  334. ret = spl_load_fit_image(info, sector, ctx, node,
  335. &image_info);
  336. if (ret < 0)
  337. return ret;
  338. }
  339. /* Make the load-address of the FDT available for the SPL framework */
  340. spl_image->fdt_addr = map_sysmem(image_info.load_addr, 0);
  341. if (CONFIG_IS_ENABLED(FIT_IMAGE_TINY))
  342. return 0;
  343. if (CONFIG_IS_ENABLED(LOAD_FIT_APPLY_OVERLAY)) {
  344. void *tmpbuffer = NULL;
  345. for (; ; index++) {
  346. node = spl_fit_get_image_node(ctx, FIT_FDT_PROP, index);
  347. if (node == -E2BIG) {
  348. debug("%s: No additional FDT node\n", __func__);
  349. break;
  350. } else if (node < 0) {
  351. debug("%s: unable to find FDT node %d\n",
  352. __func__, index);
  353. continue;
  354. }
  355. if (!tmpbuffer) {
  356. /*
  357. * allocate memory to store the DT overlay
  358. * before it is applied. It may not be used
  359. * depending on how the overlay is stored, so
  360. * don't fail yet if the allocation failed.
  361. */
  362. tmpbuffer = malloc(CONFIG_SPL_LOAD_FIT_APPLY_OVERLAY_BUF_SZ);
  363. if (!tmpbuffer)
  364. debug("%s: unable to allocate space for overlays\n",
  365. __func__);
  366. }
  367. image_info.load_addr = (ulong)tmpbuffer;
  368. ret = spl_load_fit_image(info, sector, ctx,
  369. node, &image_info);
  370. if (ret < 0)
  371. break;
  372. /* Make room in FDT for changes from the overlay */
  373. ret = fdt_increase_size(spl_image->fdt_addr,
  374. image_info.size);
  375. if (ret < 0)
  376. break;
  377. ret = fdt_overlay_apply_verbose(spl_image->fdt_addr,
  378. (void *)image_info.load_addr);
  379. if (ret) {
  380. pr_err("failed to apply DT overlay %s\n",
  381. fit_get_name(ctx->fit, node, NULL));
  382. break;
  383. }
  384. debug("%s: DT overlay %s applied\n", __func__,
  385. fit_get_name(ctx->fit, node, NULL));
  386. }
  387. free(tmpbuffer);
  388. if (ret)
  389. return ret;
  390. }
  391. /* Try to make space, so we can inject details on the loadables */
  392. ret = fdt_shrink_to_minimum(spl_image->fdt_addr, 8192);
  393. if (ret < 0)
  394. return ret;
  395. return ret;
  396. }
  397. static int spl_fit_record_loadable(const struct spl_fit_info *ctx, int index,
  398. void *blob, struct spl_image_info *image)
  399. {
  400. int ret = 0;
  401. const char *name;
  402. int node;
  403. if (CONFIG_IS_ENABLED(FIT_IMAGE_TINY))
  404. return 0;
  405. ret = spl_fit_get_image_name(ctx, "loadables", index, &name);
  406. if (ret < 0)
  407. return ret;
  408. node = spl_fit_get_image_node(ctx, "loadables", index);
  409. ret = fdt_record_loadable(blob, index, name, image->load_addr,
  410. image->size, image->entry_point,
  411. fdt_getprop(ctx->fit, node, "type", NULL),
  412. fdt_getprop(ctx->fit, node, "os", NULL));
  413. return ret;
  414. }
  415. static int spl_fit_image_is_fpga(const void *fit, int node)
  416. {
  417. const char *type;
  418. if (!IS_ENABLED(CONFIG_SPL_FPGA))
  419. return 0;
  420. type = fdt_getprop(fit, node, FIT_TYPE_PROP, NULL);
  421. if (!type)
  422. return 0;
  423. return !strcmp(type, "fpga");
  424. }
  425. static int spl_fit_image_get_os(const void *fit, int noffset, uint8_t *os)
  426. {
  427. if (!CONFIG_IS_ENABLED(FIT_IMAGE_TINY) || CONFIG_IS_ENABLED(OS_BOOT))
  428. return fit_image_get_os(fit, noffset, os);
  429. const char *name = fdt_getprop(fit, noffset, FIT_OS_PROP, NULL);
  430. if (!name)
  431. return -ENOENT;
  432. /*
  433. * We don't care what the type of the image actually is,
  434. * only whether or not it is U-Boot. This saves some
  435. * space by omitting the large table of OS types.
  436. */
  437. if (!strcmp(name, "u-boot"))
  438. *os = IH_OS_U_BOOT;
  439. else
  440. *os = IH_OS_INVALID;
  441. return 0;
  442. }
  443. /*
  444. * The purpose of the FIT load buffer is to provide a memory location that is
  445. * independent of the load address of any FIT component.
  446. */
  447. static void *spl_get_fit_load_buffer(size_t size)
  448. {
  449. void *buf;
  450. buf = malloc(size);
  451. if (!buf) {
  452. pr_err("Could not get FIT buffer of %lu bytes\n", (ulong)size);
  453. pr_err("\tcheck CONFIG_SYS_SPL_MALLOC_SIZE\n");
  454. buf = spl_get_load_buffer(0, size);
  455. }
  456. return buf;
  457. }
  458. /*
  459. * Weak default function to allow customizing SPL fit loading for load-only
  460. * use cases by allowing to skip the parsing/processing of the FIT contents
  461. * (so that this can be done separately in a more customized fashion)
  462. */
  463. __weak bool spl_load_simple_fit_skip_processing(void)
  464. {
  465. return false;
  466. }
  467. static void warn_deprecated(const char *msg)
  468. {
  469. printf("DEPRECATED: %s\n", msg);
  470. printf("\tSee doc/uImage.FIT/source_file_format.txt\n");
  471. }
  472. static int spl_fit_upload_fpga(struct spl_fit_info *ctx, int node,
  473. struct spl_image_info *fpga_image)
  474. {
  475. const char *compatible;
  476. int ret;
  477. debug("FPGA bitstream at: %x, size: %x\n",
  478. (u32)fpga_image->load_addr, fpga_image->size);
  479. compatible = fdt_getprop(ctx->fit, node, "compatible", NULL);
  480. if (!compatible)
  481. warn_deprecated("'fpga' image without 'compatible' property");
  482. else if (strcmp(compatible, "u-boot,fpga-legacy"))
  483. printf("Ignoring compatible = %s property\n", compatible);
  484. ret = fpga_load(0, (void *)fpga_image->load_addr, fpga_image->size,
  485. BIT_FULL);
  486. if (ret) {
  487. printf("%s: Cannot load the image to the FPGA\n", __func__);
  488. return ret;
  489. }
  490. puts("FPGA image loaded from FIT\n");
  491. return 0;
  492. }
  493. static int spl_fit_load_fpga(struct spl_fit_info *ctx,
  494. struct spl_load_info *info, ulong sector)
  495. {
  496. int node, ret;
  497. struct spl_image_info fpga_image = {
  498. .load_addr = 0,
  499. };
  500. node = spl_fit_get_image_node(ctx, "fpga", 0);
  501. if (node < 0)
  502. return node;
  503. warn_deprecated("'fpga' property in config node. Use 'loadables'");
  504. /* Load the image and set up the fpga_image structure */
  505. ret = spl_load_fit_image(info, sector, ctx, node, &fpga_image);
  506. if (ret) {
  507. printf("%s: Cannot load the FPGA: %i\n", __func__, ret);
  508. return ret;
  509. }
  510. return spl_fit_upload_fpga(ctx, node, &fpga_image);
  511. }
  512. static int spl_simple_fit_read(struct spl_fit_info *ctx,
  513. struct spl_load_info *info, ulong sector,
  514. const void *fit_header)
  515. {
  516. unsigned long count, size;
  517. int sectors;
  518. void *buf;
  519. /*
  520. * For FIT with external data, figure out where the external images
  521. * start. This is the base for the data-offset properties in each
  522. * image.
  523. */
  524. size = ALIGN(fdt_totalsize(fit_header), 4);
  525. size = board_spl_fit_size_align(size);
  526. ctx->ext_data_offset = ALIGN(size, 4);
  527. /*
  528. * So far we only have one block of data from the FIT. Read the entire
  529. * thing, including that first block.
  530. *
  531. * For FIT with data embedded, data is loaded as part of FIT image.
  532. * For FIT with external data, data is not loaded in this step.
  533. */
  534. sectors = get_aligned_image_size(info, size, 0);
  535. buf = spl_get_fit_load_buffer(sectors * info->bl_len);
  536. count = info->read(info, sector, sectors, buf);
  537. ctx->fit = buf;
  538. debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu, size=0x%lx\n",
  539. sector, sectors, buf, count, size);
  540. return (count == 0) ? -EIO : 0;
  541. }
  542. static int spl_simple_fit_parse(struct spl_fit_info *ctx)
  543. {
  544. /* Find the correct subnode under "/configurations" */
  545. ctx->conf_node = fit_find_config_node(ctx->fit);
  546. if (ctx->conf_node < 0)
  547. return -EINVAL;
  548. if (IS_ENABLED(CONFIG_SPL_FIT_SIGNATURE)) {
  549. printf("## Checking hash(es) for config %s ... ",
  550. fit_get_name(ctx->fit, ctx->conf_node, NULL));
  551. if (fit_config_verify(ctx->fit, ctx->conf_node))
  552. return -EPERM;
  553. puts("OK\n");
  554. }
  555. /* find the node holding the images information */
  556. ctx->images_node = fdt_path_offset(ctx->fit, FIT_IMAGES_PATH);
  557. if (ctx->images_node < 0) {
  558. debug("%s: Cannot find /images node: %d\n", __func__,
  559. ctx->images_node);
  560. return -EINVAL;
  561. }
  562. return 0;
  563. }
  564. int spl_load_simple_fit(struct spl_image_info *spl_image,
  565. struct spl_load_info *info, ulong sector, void *fit)
  566. {
  567. struct spl_image_info image_info;
  568. struct spl_fit_info ctx;
  569. int node = -1;
  570. int ret;
  571. int index = 0;
  572. int firmware_node;
  573. ret = spl_simple_fit_read(&ctx, info, sector, fit);
  574. if (ret < 0)
  575. return ret;
  576. /* skip further processing if requested to enable load-only use cases */
  577. if (spl_load_simple_fit_skip_processing())
  578. return 0;
  579. ret = spl_simple_fit_parse(&ctx);
  580. if (ret < 0)
  581. return ret;
  582. if (IS_ENABLED(CONFIG_SPL_FPGA))
  583. spl_fit_load_fpga(&ctx, info, sector);
  584. /*
  585. * Find the U-Boot image using the following search order:
  586. * - start at 'firmware' (e.g. an ARM Trusted Firmware)
  587. * - fall back 'kernel' (e.g. a Falcon-mode OS boot
  588. * - fall back to using the first 'loadables' entry
  589. */
  590. if (node < 0)
  591. node = spl_fit_get_image_node(&ctx, FIT_FIRMWARE_PROP, 0);
  592. if (node < 0 && IS_ENABLED(CONFIG_SPL_OS_BOOT))
  593. node = spl_fit_get_image_node(&ctx, FIT_KERNEL_PROP, 0);
  594. if (node < 0) {
  595. debug("could not find firmware image, trying loadables...\n");
  596. node = spl_fit_get_image_node(&ctx, "loadables", 0);
  597. /*
  598. * If we pick the U-Boot image from "loadables", start at
  599. * the second image when later loading additional images.
  600. */
  601. index = 1;
  602. }
  603. if (node < 0) {
  604. debug("%s: Cannot find u-boot image node: %d\n",
  605. __func__, node);
  606. return -1;
  607. }
  608. /* Load the image and set up the spl_image structure */
  609. ret = spl_load_fit_image(info, sector, &ctx, node, spl_image);
  610. if (ret)
  611. return ret;
  612. /*
  613. * For backward compatibility, we treat the first node that is
  614. * as a U-Boot image, if no OS-type has been declared.
  615. */
  616. if (!spl_fit_image_get_os(ctx.fit, node, &spl_image->os))
  617. debug("Image OS is %s\n", genimg_get_os_name(spl_image->os));
  618. else if (!IS_ENABLED(CONFIG_SPL_OS_BOOT))
  619. spl_image->os = IH_OS_U_BOOT;
  620. /*
  621. * Booting a next-stage U-Boot may require us to append the FDT.
  622. * We allow this to fail, as the U-Boot image might embed its FDT.
  623. */
  624. if (os_takes_devicetree(spl_image->os)) {
  625. ret = spl_fit_append_fdt(spl_image, info, sector, &ctx);
  626. if (ret < 0 && spl_image->os != IH_OS_U_BOOT)
  627. return ret;
  628. }
  629. firmware_node = node;
  630. /* Now check if there are more images for us to load */
  631. for (; ; index++) {
  632. uint8_t os_type = IH_OS_INVALID;
  633. node = spl_fit_get_image_node(&ctx, "loadables", index);
  634. if (node < 0)
  635. break;
  636. /*
  637. * if the firmware is also a loadable, skip it because
  638. * it already has been loaded. This is typically the case with
  639. * u-boot.img generated by mkimage.
  640. */
  641. if (firmware_node == node)
  642. continue;
  643. image_info.load_addr = 0;
  644. ret = spl_load_fit_image(info, sector, &ctx, node, &image_info);
  645. if (ret < 0) {
  646. printf("%s: can't load image loadables index %d (ret = %d)\n",
  647. __func__, index, ret);
  648. return ret;
  649. }
  650. if (spl_fit_image_is_fpga(ctx.fit, node))
  651. spl_fit_upload_fpga(&ctx, node, &image_info);
  652. if (!spl_fit_image_get_os(ctx.fit, node, &os_type))
  653. debug("Loadable is %s\n", genimg_get_os_name(os_type));
  654. if (os_takes_devicetree(os_type)) {
  655. spl_fit_append_fdt(&image_info, info, sector, &ctx);
  656. spl_image->fdt_addr = image_info.fdt_addr;
  657. }
  658. /*
  659. * If the "firmware" image did not provide an entry point,
  660. * use the first valid entry point from the loadables.
  661. */
  662. if (spl_image->entry_point == FDT_ERROR &&
  663. image_info.entry_point != FDT_ERROR)
  664. spl_image->entry_point = image_info.entry_point;
  665. /* Record our loadables into the FDT */
  666. if (spl_image->fdt_addr)
  667. spl_fit_record_loadable(&ctx, index,
  668. spl_image->fdt_addr,
  669. &image_info);
  670. }
  671. /*
  672. * If a platform does not provide CONFIG_SYS_UBOOT_START, U-Boot's
  673. * Makefile will set it to 0 and it will end up as the entry point
  674. * here. What it actually means is: use the load address.
  675. */
  676. if (spl_image->entry_point == FDT_ERROR || spl_image->entry_point == 0)
  677. spl_image->entry_point = spl_image->load_addr;
  678. spl_image->flags |= SPL_FIT_FOUND;
  679. if (IS_ENABLED(CONFIG_IMX_HAB))
  680. board_spl_fit_post_load(ctx.fit);
  681. return 0;
  682. }