spl_fit.c 15 KB

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