spl_fit.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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(fit, node, &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. fdt_getprop(ctx->fit, node, "arch", NULL));
  414. return ret;
  415. }
  416. static int spl_fit_image_is_fpga(const void *fit, int node)
  417. {
  418. const char *type;
  419. if (!IS_ENABLED(CONFIG_SPL_FPGA))
  420. return 0;
  421. type = fdt_getprop(fit, node, FIT_TYPE_PROP, NULL);
  422. if (!type)
  423. return 0;
  424. return !strcmp(type, "fpga");
  425. }
  426. static int spl_fit_image_get_os(const void *fit, int noffset, uint8_t *os)
  427. {
  428. if (!CONFIG_IS_ENABLED(FIT_IMAGE_TINY) || CONFIG_IS_ENABLED(OS_BOOT))
  429. return fit_image_get_os(fit, noffset, os);
  430. const char *name = fdt_getprop(fit, noffset, FIT_OS_PROP, NULL);
  431. if (!name)
  432. return -ENOENT;
  433. /*
  434. * We don't care what the type of the image actually is,
  435. * only whether or not it is U-Boot. This saves some
  436. * space by omitting the large table of OS types.
  437. */
  438. if (!strcmp(name, "u-boot"))
  439. *os = IH_OS_U_BOOT;
  440. else
  441. *os = IH_OS_INVALID;
  442. return 0;
  443. }
  444. /*
  445. * The purpose of the FIT load buffer is to provide a memory location that is
  446. * independent of the load address of any FIT component.
  447. */
  448. static void *spl_get_fit_load_buffer(size_t size)
  449. {
  450. void *buf;
  451. buf = malloc(size);
  452. if (!buf) {
  453. pr_err("Could not get FIT buffer of %lu bytes\n", (ulong)size);
  454. pr_err("\tcheck CONFIG_SYS_SPL_MALLOC_SIZE\n");
  455. buf = spl_get_load_buffer(0, size);
  456. }
  457. return buf;
  458. }
  459. /*
  460. * Weak default function to allow customizing SPL fit loading for load-only
  461. * use cases by allowing to skip the parsing/processing of the FIT contents
  462. * (so that this can be done separately in a more customized fashion)
  463. */
  464. __weak bool spl_load_simple_fit_skip_processing(void)
  465. {
  466. return false;
  467. }
  468. static void warn_deprecated(const char *msg)
  469. {
  470. printf("DEPRECATED: %s\n", msg);
  471. printf("\tSee doc/uImage.FIT/source_file_format.txt\n");
  472. }
  473. static int spl_fit_upload_fpga(struct spl_fit_info *ctx, int node,
  474. struct spl_image_info *fpga_image)
  475. {
  476. const char *compatible;
  477. int ret;
  478. debug("FPGA bitstream at: %x, size: %x\n",
  479. (u32)fpga_image->load_addr, fpga_image->size);
  480. compatible = fdt_getprop(ctx->fit, node, "compatible", NULL);
  481. if (!compatible)
  482. warn_deprecated("'fpga' image without 'compatible' property");
  483. else if (strcmp(compatible, "u-boot,fpga-legacy"))
  484. printf("Ignoring compatible = %s property\n", compatible);
  485. ret = fpga_load(0, (void *)fpga_image->load_addr, fpga_image->size,
  486. BIT_FULL);
  487. if (ret) {
  488. printf("%s: Cannot load the image to the FPGA\n", __func__);
  489. return ret;
  490. }
  491. puts("FPGA image loaded from FIT\n");
  492. return 0;
  493. }
  494. static int spl_fit_load_fpga(struct spl_fit_info *ctx,
  495. struct spl_load_info *info, ulong sector)
  496. {
  497. int node, ret;
  498. struct spl_image_info fpga_image = {
  499. .load_addr = 0,
  500. };
  501. node = spl_fit_get_image_node(ctx, "fpga", 0);
  502. if (node < 0)
  503. return node;
  504. warn_deprecated("'fpga' property in config node. Use 'loadables'");
  505. /* Load the image and set up the fpga_image structure */
  506. ret = spl_load_fit_image(info, sector, ctx, node, &fpga_image);
  507. if (ret) {
  508. printf("%s: Cannot load the FPGA: %i\n", __func__, ret);
  509. return ret;
  510. }
  511. return spl_fit_upload_fpga(ctx, node, &fpga_image);
  512. }
  513. static int spl_simple_fit_read(struct spl_fit_info *ctx,
  514. struct spl_load_info *info, ulong sector,
  515. const void *fit_header)
  516. {
  517. unsigned long count, size;
  518. int sectors;
  519. void *buf;
  520. /*
  521. * For FIT with external data, figure out where the external images
  522. * start. This is the base for the data-offset properties in each
  523. * image.
  524. */
  525. size = ALIGN(fdt_totalsize(fit_header), 4);
  526. size = board_spl_fit_size_align(size);
  527. ctx->ext_data_offset = ALIGN(size, 4);
  528. /*
  529. * So far we only have one block of data from the FIT. Read the entire
  530. * thing, including that first block.
  531. *
  532. * For FIT with data embedded, data is loaded as part of FIT image.
  533. * For FIT with external data, data is not loaded in this step.
  534. */
  535. sectors = get_aligned_image_size(info, size, 0);
  536. buf = spl_get_fit_load_buffer(sectors * info->bl_len);
  537. count = info->read(info, sector, sectors, buf);
  538. ctx->fit = buf;
  539. debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu, size=0x%lx\n",
  540. sector, sectors, buf, count, size);
  541. return (count == 0) ? -EIO : 0;
  542. }
  543. static int spl_simple_fit_parse(struct spl_fit_info *ctx)
  544. {
  545. /* Find the correct subnode under "/configurations" */
  546. ctx->conf_node = fit_find_config_node(ctx->fit);
  547. if (ctx->conf_node < 0)
  548. return -EINVAL;
  549. if (IS_ENABLED(CONFIG_SPL_FIT_SIGNATURE)) {
  550. printf("## Checking hash(es) for config %s ... ",
  551. fit_get_name(ctx->fit, ctx->conf_node, NULL));
  552. if (fit_config_verify(ctx->fit, ctx->conf_node))
  553. return -EPERM;
  554. puts("OK\n");
  555. }
  556. /* find the node holding the images information */
  557. ctx->images_node = fdt_path_offset(ctx->fit, FIT_IMAGES_PATH);
  558. if (ctx->images_node < 0) {
  559. debug("%s: Cannot find /images node: %d\n", __func__,
  560. ctx->images_node);
  561. return -EINVAL;
  562. }
  563. return 0;
  564. }
  565. int spl_load_simple_fit(struct spl_image_info *spl_image,
  566. struct spl_load_info *info, ulong sector, void *fit)
  567. {
  568. struct spl_image_info image_info;
  569. struct spl_fit_info ctx;
  570. int node = -1;
  571. int ret;
  572. int index = 0;
  573. int firmware_node;
  574. ret = spl_simple_fit_read(&ctx, info, sector, fit);
  575. if (ret < 0)
  576. return ret;
  577. /* skip further processing if requested to enable load-only use cases */
  578. if (spl_load_simple_fit_skip_processing())
  579. return 0;
  580. ret = spl_simple_fit_parse(&ctx);
  581. if (ret < 0)
  582. return ret;
  583. if (IS_ENABLED(CONFIG_SPL_FPGA))
  584. spl_fit_load_fpga(&ctx, info, sector);
  585. /*
  586. * Find the U-Boot image using the following search order:
  587. * - start at 'firmware' (e.g. an ARM Trusted Firmware)
  588. * - fall back 'kernel' (e.g. a Falcon-mode OS boot
  589. * - fall back to using the first 'loadables' entry
  590. */
  591. if (node < 0)
  592. node = spl_fit_get_image_node(&ctx, FIT_FIRMWARE_PROP, 0);
  593. if (node < 0 && IS_ENABLED(CONFIG_SPL_OS_BOOT))
  594. node = spl_fit_get_image_node(&ctx, FIT_KERNEL_PROP, 0);
  595. if (node < 0) {
  596. debug("could not find firmware image, trying loadables...\n");
  597. node = spl_fit_get_image_node(&ctx, "loadables", 0);
  598. /*
  599. * If we pick the U-Boot image from "loadables", start at
  600. * the second image when later loading additional images.
  601. */
  602. index = 1;
  603. }
  604. if (node < 0) {
  605. debug("%s: Cannot find u-boot image node: %d\n",
  606. __func__, node);
  607. return -1;
  608. }
  609. /* Load the image and set up the spl_image structure */
  610. ret = spl_load_fit_image(info, sector, &ctx, node, spl_image);
  611. if (ret)
  612. return ret;
  613. /*
  614. * For backward compatibility, we treat the first node that is
  615. * as a U-Boot image, if no OS-type has been declared.
  616. */
  617. if (!spl_fit_image_get_os(ctx.fit, node, &spl_image->os))
  618. debug("Image OS is %s\n", genimg_get_os_name(spl_image->os));
  619. else if (!IS_ENABLED(CONFIG_SPL_OS_BOOT))
  620. spl_image->os = IH_OS_U_BOOT;
  621. /*
  622. * Booting a next-stage U-Boot may require us to append the FDT.
  623. * We allow this to fail, as the U-Boot image might embed its FDT.
  624. */
  625. if (os_takes_devicetree(spl_image->os)) {
  626. ret = spl_fit_append_fdt(spl_image, info, sector, &ctx);
  627. if (ret < 0 && spl_image->os != IH_OS_U_BOOT)
  628. return ret;
  629. }
  630. firmware_node = node;
  631. /* Now check if there are more images for us to load */
  632. for (; ; index++) {
  633. uint8_t os_type = IH_OS_INVALID;
  634. node = spl_fit_get_image_node(&ctx, "loadables", index);
  635. if (node < 0)
  636. break;
  637. /*
  638. * if the firmware is also a loadable, skip it because
  639. * it already has been loaded. This is typically the case with
  640. * u-boot.img generated by mkimage.
  641. */
  642. if (firmware_node == node)
  643. continue;
  644. image_info.load_addr = 0;
  645. ret = spl_load_fit_image(info, sector, &ctx, node, &image_info);
  646. if (ret < 0) {
  647. printf("%s: can't load image loadables index %d (ret = %d)\n",
  648. __func__, index, ret);
  649. return ret;
  650. }
  651. if (spl_fit_image_is_fpga(ctx.fit, node))
  652. spl_fit_upload_fpga(&ctx, node, &image_info);
  653. if (!spl_fit_image_get_os(ctx.fit, node, &os_type))
  654. debug("Loadable is %s\n", genimg_get_os_name(os_type));
  655. if (os_takes_devicetree(os_type)) {
  656. spl_fit_append_fdt(&image_info, info, sector, &ctx);
  657. spl_image->fdt_addr = image_info.fdt_addr;
  658. }
  659. /*
  660. * If the "firmware" image did not provide an entry point,
  661. * use the first valid entry point from the loadables.
  662. */
  663. if (spl_image->entry_point == FDT_ERROR &&
  664. image_info.entry_point != FDT_ERROR)
  665. spl_image->entry_point = image_info.entry_point;
  666. /* Record our loadables into the FDT */
  667. if (spl_image->fdt_addr)
  668. spl_fit_record_loadable(&ctx, index,
  669. spl_image->fdt_addr,
  670. &image_info);
  671. }
  672. /*
  673. * If a platform does not provide CONFIG_SYS_UBOOT_START, U-Boot's
  674. * Makefile will set it to 0 and it will end up as the entry point
  675. * here. What it actually means is: use the load address.
  676. */
  677. if (spl_image->entry_point == FDT_ERROR || spl_image->entry_point == 0)
  678. spl_image->entry_point = spl_image->load_addr;
  679. spl_image->flags |= SPL_FIT_FOUND;
  680. if (IS_ENABLED(CONFIG_IMX_HAB))
  681. board_spl_fit_post_load(ctx.fit);
  682. return 0;
  683. }