spl_fit.c 14 KB

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