1 // SPDX-License-Identifier: GPL-2.0+
3 * Image code used by boards (and not host tools)
5 * (C) Copyright 2008 Semihalf
7 * (C) Copyright 2000-2006
8 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
12 #include <bootstage.h>
14 #include <display_options.h>
23 #include <asm/cache.h>
24 #include <asm/global_data.h>
26 DECLARE_GLOBAL_DATA_PTR;
29 * image_get_ramdisk - get and verify ramdisk image
30 * @rd_addr: ramdisk image start address
31 * @arch: expected ramdisk architecture
32 * @verify: checksum verification flag
34 * image_get_ramdisk() returns a pointer to the verified ramdisk image
35 * header. Routine receives image start address and expected architecture
36 * flag. Verification done covers data and header integrity and os/type/arch
40 * pointer to a ramdisk image header, if image was found and valid
41 * otherwise, return NULL
43 static const struct legacy_img_hdr *image_get_ramdisk(ulong rd_addr, u8 arch,
46 const struct legacy_img_hdr *rd_hdr = (const struct legacy_img_hdr *)rd_addr;
48 if (!image_check_magic(rd_hdr)) {
49 puts("Bad Magic Number\n");
50 bootstage_error(BOOTSTAGE_ID_RD_MAGIC);
54 if (!image_check_hcrc(rd_hdr)) {
55 puts("Bad Header Checksum\n");
56 bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
60 bootstage_mark(BOOTSTAGE_ID_RD_MAGIC);
61 image_print_contents(rd_hdr);
64 puts(" Verifying Checksum ... ");
65 if (!image_check_dcrc(rd_hdr)) {
66 puts("Bad Data CRC\n");
67 bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM);
73 bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
75 if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
76 !image_check_arch(rd_hdr, arch) ||
77 !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
78 printf("No Linux %s Ramdisk Image\n",
79 genimg_get_arch_name(arch));
80 bootstage_error(BOOTSTAGE_ID_RAMDISK);
87 /*****************************************************************************/
88 /* Shared dual-format routines */
89 /*****************************************************************************/
90 ulong image_load_addr = CONFIG_SYS_LOAD_ADDR; /* Default Load Address */
91 ulong image_save_addr; /* Default Save Address */
92 ulong image_save_size; /* Default Save Size (in bytes) */
94 static int on_loadaddr(const char *name, const char *value, enum env_op op,
99 case env_op_overwrite:
100 image_load_addr = hextoul(value, NULL);
108 U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
110 ulong env_get_bootm_low(void)
112 char *s = env_get("bootm_low");
115 ulong tmp = hextoul(s, NULL);
119 #if defined(CFG_SYS_SDRAM_BASE)
120 return CFG_SYS_SDRAM_BASE;
121 #elif defined(CONFIG_ARM) || defined(CONFIG_MICROBLAZE) || defined(CONFIG_RISCV)
122 return gd->bd->bi_dram[0].start;
128 phys_size_t env_get_bootm_size(void)
130 phys_size_t tmp, size;
132 char *s = env_get("bootm_size");
135 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
139 start = gd->ram_base;
142 if (start + size > gd->ram_top)
143 size = gd->ram_top - start;
145 s = env_get("bootm_low");
147 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
151 return size - (tmp - start);
154 phys_size_t env_get_bootm_mapsize(void)
157 char *s = env_get("bootm_mapsize");
160 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
164 #if defined(CFG_SYS_BOOTMAPSZ)
165 return CFG_SYS_BOOTMAPSZ;
167 return env_get_bootm_size();
171 void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
176 if (IS_ENABLED(CONFIG_HW_WATCHDOG) || IS_ENABLED(CONFIG_WATCHDOG)) {
182 size_t tail = (len > chunksz) ? chunksz : len;
189 memmove(to, from, tail);
197 memmove(to, from, len);
202 * genimg_get_kernel_addr_fit - get the real kernel address and return 2
204 * @img_addr: a string might contain real image address
205 * @fit_uname_config: double pointer to a char, will hold pointer to a
206 * configuration unit name
207 * @fit_uname_kernel: double pointer to a char, will hold pointer to a subimage
210 * genimg_get_kernel_addr_fit get the real kernel start address from a string
211 * which is normally the first argv of bootm/bootz
214 * kernel start address
216 ulong genimg_get_kernel_addr_fit(char * const img_addr,
217 const char **fit_uname_config,
218 const char **fit_uname_kernel)
222 /* find out kernel image address */
224 kernel_addr = image_load_addr;
225 debug("* kernel: default image load address = 0x%08lx\n",
227 } else if (CONFIG_IS_ENABLED(FIT) &&
228 fit_parse_conf(img_addr, image_load_addr, &kernel_addr,
230 debug("* kernel: config '%s' from image at 0x%08lx\n",
231 *fit_uname_config, kernel_addr);
232 } else if (CONFIG_IS_ENABLED(FIT) &&
233 fit_parse_subimage(img_addr, image_load_addr, &kernel_addr,
235 debug("* kernel: subimage '%s' from image at 0x%08lx\n",
236 *fit_uname_kernel, kernel_addr);
238 kernel_addr = hextoul(img_addr, NULL);
239 debug("* kernel: cmdline image address = 0x%08lx\n",
247 * genimg_get_kernel_addr() is the simple version of
248 * genimg_get_kernel_addr_fit(). It ignores those return FIT strings
250 ulong genimg_get_kernel_addr(char * const img_addr)
252 const char *fit_uname_config = NULL;
253 const char *fit_uname_kernel = NULL;
255 return genimg_get_kernel_addr_fit(img_addr, &fit_uname_config,
260 * genimg_get_format - get image format type
261 * @img_addr: image start address
263 * genimg_get_format() checks whether provided address points to a valid
264 * legacy or FIT image.
266 * New uImage format and FDT blob are based on a libfdt. FDT blob
267 * may be passed directly or embedded in a FIT image. In both situations
268 * genimg_get_format() must be able to dectect libfdt header.
271 * image format type or IMAGE_FORMAT_INVALID if no image is present
273 int genimg_get_format(const void *img_addr)
275 if (CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)) {
276 const struct legacy_img_hdr *hdr;
278 hdr = (const struct legacy_img_hdr *)img_addr;
279 if (image_check_magic(hdr))
280 return IMAGE_FORMAT_LEGACY;
282 if (CONFIG_IS_ENABLED(FIT) || CONFIG_IS_ENABLED(OF_LIBFDT)) {
283 if (!fdt_check_header(img_addr))
284 return IMAGE_FORMAT_FIT;
286 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE) &&
287 is_android_boot_image_header(img_addr))
288 return IMAGE_FORMAT_ANDROID;
290 return IMAGE_FORMAT_INVALID;
294 * fit_has_config - check if there is a valid FIT configuration
295 * @images: pointer to the bootm command headers structure
297 * fit_has_config() checks if there is a FIT configuration in use
298 * (if FTI support is present).
301 * 0, no FIT support or no configuration found
302 * 1, configuration found
304 int genimg_has_config(struct bootm_headers *images)
306 if (CONFIG_IS_ENABLED(FIT) && images->fit_uname_cfg)
313 * select_ramdisk() - Select and locate the ramdisk to use
315 * @images: pointer to the bootm images structure
316 * @select: name of ramdisk to select, or hex address, NULL for any
317 * @arch: expected ramdisk architecture
318 * @rd_datap: pointer to a ulong variable, will hold ramdisk pointer
319 * @rd_lenp: pointer to a ulong variable, will hold ramdisk length
320 * Return: 0 if OK, -ENOPKG if no ramdisk (but an error should not be reported),
321 * other -ve value on other error
323 static int select_ramdisk(struct bootm_headers *images, const char *select, u8 arch,
324 ulong *rd_datap, ulong *rd_lenp)
326 const char *fit_uname_config;
327 const char *fit_uname_ramdisk;
328 bool done_select = !select;
334 if (CONFIG_IS_ENABLED(FIT)) {
335 fit_uname_config = images->fit_uname_cfg;
336 fit_uname_ramdisk = NULL;
341 * If the init ramdisk comes from the FIT image and
342 * the FIT image address is omitted in the command
343 * line argument, try to use os FIT image address or
344 * default load address.
346 if (images->fit_uname_os)
347 default_addr = (ulong)images->fit_hdr_os;
349 default_addr = image_load_addr;
351 if (fit_parse_conf(select, default_addr, &rd_addr,
352 &fit_uname_config)) {
353 debug("* ramdisk: config '%s' from image at 0x%08lx\n",
354 fit_uname_config, rd_addr);
356 } else if (fit_parse_subimage(select, default_addr,
358 &fit_uname_ramdisk)) {
359 debug("* ramdisk: subimage '%s' from image at 0x%08lx\n",
360 fit_uname_ramdisk, rd_addr);
366 rd_addr = hextoul(select, NULL);
367 debug("* ramdisk: cmdline image address = 0x%08lx\n", rd_addr);
369 if (CONFIG_IS_ENABLED(FIT) && !select) {
370 /* use FIT configuration provided in first bootm
371 * command argument. If the property is not defined,
372 * quit silently (with -ENOPKG)
374 rd_addr = map_to_sysmem(images->fit_hdr_os);
375 rd_noffset = fit_get_node_from_config(images, FIT_RAMDISK_PROP,
377 if (rd_noffset == -ENOENT)
379 else if (rd_noffset < 0)
384 * Check if there is an initrd image at the
385 * address provided in the second bootm argument
386 * check image type, for FIT images get FIT node.
388 buf = map_sysmem(rd_addr, 0);
389 switch (genimg_get_format(buf)) {
390 case IMAGE_FORMAT_LEGACY:
391 if (CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)) {
392 const struct legacy_img_hdr *rd_hdr;
394 printf("## Loading init Ramdisk from Legacy Image at %08lx ...\n",
397 bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
398 rd_hdr = image_get_ramdisk(rd_addr, arch,
404 *rd_datap = image_get_data(rd_hdr);
405 *rd_lenp = image_get_data_size(rd_hdr);
409 case IMAGE_FORMAT_FIT:
410 if (CONFIG_IS_ENABLED(FIT)) {
411 rd_noffset = fit_image_load(images, rd_addr,
414 arch, IH_TYPE_RAMDISK,
415 BOOTSTAGE_ID_FIT_RD_START,
416 FIT_LOAD_OPTIONAL_NON_ZERO,
421 images->fit_hdr_rd = map_sysmem(rd_addr, 0);
422 images->fit_uname_rd = fit_uname_ramdisk;
423 images->fit_noffset_rd = rd_noffset;
427 case IMAGE_FORMAT_ANDROID:
428 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE)) {
430 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
431 void *boot_img = map_sysmem(get_abootimg_addr(), 0);
432 void *vendor_boot_img = map_sysmem(get_avendor_bootimg_addr(), 0);
434 ret = android_image_get_ramdisk(boot_img, vendor_boot_img,
436 unmap_sysmem(vendor_boot_img);
437 unmap_sysmem(boot_img);
439 void *ptr = map_sysmem(images->os.start, 0);
441 ret = android_image_get_ramdisk(ptr, NULL, rd_datap, rd_lenp);
453 if (IS_ENABLED(CONFIG_SUPPORT_RAW_INITRD)) {
457 end = strchr(select, ':');
459 *rd_lenp = hextoul(++end, NULL);
466 puts("Wrong Ramdisk Image Format\n");
475 * boot_get_ramdisk - main ramdisk handling routine
476 * @argc: command argument count
477 * @argv: command argument list
478 * @images: pointer to the bootm images structure
479 * @arch: expected ramdisk architecture
480 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
481 * @rd_end: pointer to a ulong variable, will hold ramdisk end
483 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
484 * Currently supported are the following ramdisk sources:
485 * - multicomponent kernel/ramdisk image,
486 * - commandline provided address of decicated ramdisk image.
489 * 0, if ramdisk image was found and valid, or skiped
490 * rd_start and rd_end are set to ramdisk start/end addresses if
491 * ramdisk image is found and valid
493 * 1, if ramdisk image is found but corrupted, or invalid
494 * rd_start and rd_end are set to 0 if no ramdisk exists
496 int boot_get_ramdisk(int argc, char *const argv[], struct bootm_headers *images,
497 u8 arch, ulong *rd_start, ulong *rd_end)
499 ulong rd_data, rd_len;
500 const char *select = NULL;
505 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE)) {
508 /* Look for an Android boot image */
509 buf = map_sysmem(images->os.start, 0);
510 if (buf && genimg_get_format(buf) == IMAGE_FORMAT_ANDROID)
511 select = (argc == 0) ? env_get("loadaddr") : argv[0];
518 * Look for a '-' which indicates to ignore the
521 if (select && strcmp(select, "-") == 0) {
522 debug("## Skipping init Ramdisk\n");
525 } else if (select || genimg_has_config(images)) {
528 ret = select_ramdisk(images, select, arch, &rd_data, &rd_len);
533 } else if (images->legacy_hdr_valid &&
534 image_check_type(&images->legacy_hdr_os_copy,
537 * Now check if we have a legacy mult-component image,
538 * get second entry data start address and len.
540 bootstage_mark(BOOTSTAGE_ID_RAMDISK);
541 printf("## Loading init Ramdisk from multi component Legacy Image at %08lx ...\n",
542 (ulong)images->legacy_hdr_os);
544 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
549 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
555 debug("## No init Ramdisk\n");
558 *rd_end = rd_data + rd_len;
560 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
567 * boot_ramdisk_high - relocate init ramdisk
568 * @lmb: pointer to lmb handle, will be used for memory mgmt
569 * @rd_data: ramdisk data start address
570 * @rd_len: ramdisk data length
571 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
572 * start address (after possible relocation)
573 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
574 * end address (after possible relocation)
576 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
577 * variable and if requested ramdisk data is moved to a specified location.
579 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
580 * start/end addresses if ramdisk image start and len were provided,
581 * otherwise set initrd_start and initrd_end set to zeros.
587 int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
588 ulong *initrd_start, ulong *initrd_end)
592 int initrd_copy_to_ram = 1;
594 s = env_get("initrd_high");
596 /* a value of "no" or a similar string will act like 0,
597 * turning the "load high" feature off. This is intentional.
599 initrd_high = hextoul(s, NULL);
600 if (initrd_high == ~0)
601 initrd_copy_to_ram = 0;
603 initrd_high = env_get_bootm_mapsize() + env_get_bootm_low();
606 debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
607 initrd_high, initrd_copy_to_ram);
610 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
611 debug(" in-place initrd\n");
612 *initrd_start = rd_data;
613 *initrd_end = rd_data + rd_len;
614 lmb_reserve(lmb, rd_data, rd_len);
617 *initrd_start = (ulong)lmb_alloc_base(lmb,
618 rd_len, 0x1000, initrd_high);
620 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
623 if (*initrd_start == 0) {
624 puts("ramdisk - allocation error\n");
627 bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
629 *initrd_end = *initrd_start + rd_len;
630 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
631 *initrd_start, *initrd_end);
633 memmove_wd((void *)*initrd_start,
634 (void *)rd_data, rd_len, CHUNKSZ);
637 * Ensure the image is flushed to memory to handle
638 * AMP boot scenarios in which we might not be
641 if (IS_ENABLED(CONFIG_MP)) {
642 flush_cache((unsigned long)*initrd_start,
643 ALIGN(rd_len, ARCH_DMA_MINALIGN));
651 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
652 *initrd_start, *initrd_end);
660 int boot_get_setup(struct bootm_headers *images, u8 arch,
661 ulong *setup_start, ulong *setup_len)
663 if (!CONFIG_IS_ENABLED(FIT))
666 return boot_get_setup_fit(images, arch, setup_start, setup_len);
669 int boot_get_fpga(int argc, char *const argv[], struct bootm_headers *images,
670 u8 arch, const ulong *ld_start, ulong * const ld_len)
672 ulong tmp_img_addr, img_data, img_len;
676 const char *uname, *name;
678 int devnum = 0; /* TODO support multi fpga platforms */
680 if (!IS_ENABLED(CONFIG_FPGA))
683 /* Check to see if the images struct has a FIT configuration */
684 if (!genimg_has_config(images)) {
685 debug("## FIT configuration was not specified\n");
690 * Obtain the os FIT header from the images struct
692 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
693 buf = map_sysmem(tmp_img_addr, 0);
695 * Check image type. For FIT images get FIT node
696 * and attempt to locate a generic binary.
698 switch (genimg_get_format(buf)) {
699 case IMAGE_FORMAT_FIT:
700 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
702 uname = fdt_stringlist_get(buf, conf_noffset, FIT_FPGA_PROP, 0,
705 debug("## FPGA image is not specified\n");
708 fit_img_result = fit_image_load(images,
710 (const char **)&uname,
711 &images->fit_uname_cfg,
714 BOOTSTAGE_ID_FPGA_INIT,
715 FIT_LOAD_OPTIONAL_NON_ZERO,
716 &img_data, &img_len);
718 debug("FPGA image (%s) loaded to 0x%lx/size 0x%lx\n",
719 uname, img_data, img_len);
721 if (fit_img_result < 0) {
722 /* Something went wrong! */
723 return fit_img_result;
726 if (!fpga_is_partial_data(devnum, img_len)) {
728 err = fpga_loadbitstream(devnum, (char *)img_data,
731 err = fpga_load(devnum, (const void *)img_data,
732 img_len, BIT_FULL, 0);
735 err = fpga_loadbitstream(devnum, (char *)img_data,
736 img_len, BIT_PARTIAL);
738 err = fpga_load(devnum, (const void *)img_data,
739 img_len, BIT_PARTIAL, 0);
745 printf(" Programming %s bitstream... OK\n", name);
748 printf("The given image format is not supported (corrupt?)\n");
755 static void fit_loadable_process(u8 img_type,
760 const unsigned int count =
761 ll_entry_count(struct fit_loadable_tbl, fit_loadable);
762 struct fit_loadable_tbl *fit_loadable_handler =
763 ll_entry_start(struct fit_loadable_tbl, fit_loadable);
764 /* For each loadable handler */
765 for (i = 0; i < count; i++, fit_loadable_handler++)
766 /* matching this type */
767 if (fit_loadable_handler->type == img_type)
768 /* call that handler with this image data */
769 fit_loadable_handler->handler(img_data, img_len);
772 int boot_get_loadable(int argc, char *const argv[], struct bootm_headers *images,
773 u8 arch, const ulong *ld_start, ulong * const ld_len)
776 * These variables are used to hold the current image location
781 * These two variables are requirements for fit_image_load, but
782 * their values are not used
784 ulong img_data, img_len;
792 /* Check to see if the images struct has a FIT configuration */
793 if (!genimg_has_config(images)) {
794 debug("## FIT configuration was not specified\n");
799 * Obtain the os FIT header from the images struct
801 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
802 buf = map_sysmem(tmp_img_addr, 0);
804 * Check image type. For FIT images get FIT node
805 * and attempt to locate a generic binary.
807 switch (genimg_get_format(buf)) {
808 case IMAGE_FORMAT_FIT:
809 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
811 for (loadables_index = 0;
812 uname = fdt_stringlist_get(buf, conf_noffset,
814 loadables_index, NULL), uname;
816 fit_img_result = fit_image_load(images, tmp_img_addr,
818 &images->fit_uname_cfg,
819 arch, IH_TYPE_LOADABLE,
820 BOOTSTAGE_ID_FIT_LOADABLE_START,
821 FIT_LOAD_OPTIONAL_NON_ZERO,
822 &img_data, &img_len);
823 if (fit_img_result < 0) {
824 /* Something went wrong! */
825 return fit_img_result;
828 fit_img_result = fit_image_get_node(buf, uname);
829 if (fit_img_result < 0) {
830 /* Something went wrong! */
831 return fit_img_result;
833 fit_img_result = fit_image_get_type(buf,
836 if (fit_img_result < 0) {
837 /* Something went wrong! */
838 return fit_img_result;
841 fit_loadable_process(img_type, img_data, img_len);
845 printf("The given image format is not supported (corrupt?)\n");
853 * boot_get_cmdline - allocate and initialize kernel cmdline
854 * @lmb: pointer to lmb handle, will be used for memory mgmt
855 * @cmd_start: pointer to a ulong variable, will hold cmdline start
856 * @cmd_end: pointer to a ulong variable, will hold cmdline end
858 * This allocates space for kernel command line below
859 * BOOTMAPSZ + env_get_bootm_low() address. If "bootargs" U-Boot environment
860 * variable is present its contents is copied to allocated kernel
867 int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
874 * Help the compiler detect that this function is only called when
875 * CONFIG_SYS_BOOT_GET_CMDLINE is enabled
877 if (!IS_ENABLED(CONFIG_SYS_BOOT_GET_CMDLINE))
880 barg = IF_ENABLED_INT(CONFIG_SYS_BOOT_GET_CMDLINE, CONFIG_SYS_BARGSIZE);
881 cmdline = (char *)(ulong)lmb_alloc_base(lmb, barg, 0xf,
882 env_get_bootm_mapsize() + env_get_bootm_low());
886 s = env_get("bootargs");
892 *cmd_start = (ulong)cmdline;
893 *cmd_end = *cmd_start + strlen(cmdline);
895 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
901 * boot_get_kbd - allocate and initialize kernel copy of board info
902 * @lmb: pointer to lmb handle, will be used for memory mgmt
903 * @kbd: double pointer to board info data
905 * boot_get_kbd() allocates space for kernel copy of board info data below
906 * BOOTMAPSZ + env_get_bootm_low() address and kernel board info is initialized
907 * with the current u-boot board info data.
913 int boot_get_kbd(struct lmb *lmb, struct bd_info **kbd)
915 *kbd = (struct bd_info *)(ulong)lmb_alloc_base(lmb,
916 sizeof(struct bd_info),
918 env_get_bootm_mapsize() +
919 env_get_bootm_low());
925 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
927 if (_DEBUG && IS_ENABLED(CONFIG_CMD_BDI))
928 do_bdinfo(NULL, 0, 0, NULL);
933 int image_setup_linux(struct bootm_headers *images)
935 ulong of_size = images->ft_len;
936 char **of_flat_tree = &images->ft_addr;
937 struct lmb *lmb = images_lmb(images);
940 /* This function cannot be called without lmb support */
941 if (!IS_ENABLED(CONFIG_LMB))
943 if (CONFIG_IS_ENABLED(OF_LIBFDT))
944 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
946 if (IS_ENABLED(CONFIG_SYS_BOOT_GET_CMDLINE)) {
947 ret = boot_get_cmdline(lmb, &images->cmdline_start,
948 &images->cmdline_end);
950 puts("ERROR with allocation of cmdline\n");
955 if (CONFIG_IS_ENABLED(OF_LIBFDT)) {
956 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
961 if (CONFIG_IS_ENABLED(OF_LIBFDT) && of_size) {
962 ret = image_setup_libfdt(images, *of_flat_tree, of_size, lmb);
970 void genimg_print_size(uint32_t size)
972 printf("%d Bytes = ", size);
973 print_size(size, "\n");
976 void genimg_print_time(time_t timestamp)
980 rtc_to_tm(timestamp, &tm);
981 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
982 tm.tm_year, tm.tm_mon, tm.tm_mday,
983 tm.tm_hour, tm.tm_min, tm.tm_sec);
987 * get_default_image() - Return default property from /images
989 * Return: Pointer to value of default property (or NULL)
991 static const char *get_default_image(const void *fit)
995 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
996 if (images_noffset < 0)
999 return fdt_getprop(fit, images_noffset, FIT_DEFAULT_PROP, NULL);
1002 int image_locate_script(void *buf, int size, const char *fit_uname,
1003 const char *confname, char **datap, uint *lenp)
1005 const struct legacy_img_hdr *hdr;
1006 const void *fit_data;
1007 const void *fit_hdr;
1014 verify = env_get_yesno("verify");
1016 switch (genimg_get_format(buf)) {
1017 case IMAGE_FORMAT_LEGACY:
1018 if (!IS_ENABLED(CONFIG_LEGACY_IMAGE_FORMAT)) {
1019 goto exit_image_format;
1023 if (!image_check_magic(hdr)) {
1024 puts("Bad magic number\n");
1028 if (!image_check_hcrc(hdr)) {
1029 puts("Bad header crc\n");
1034 if (!image_check_dcrc(hdr)) {
1035 puts("Bad data crc\n");
1040 if (!image_check_type(hdr, IH_TYPE_SCRIPT)) {
1041 puts("Bad image type\n");
1045 /* get length of script */
1046 data = (u32 *)image_get_data(hdr);
1048 len = uimage_to_cpu(*data);
1050 puts("Empty Script\n");
1055 * scripts are just multi-image files with one
1056 * component, so seek past the zero-terminated sequence
1057 * of image lengths to get to the actual image data
1062 case IMAGE_FORMAT_FIT:
1063 if (!IS_ENABLED(CONFIG_FIT)) {
1064 goto exit_image_format;
1067 if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) {
1068 puts("Bad FIT image format\n");
1073 /* If confname is empty, use the default */
1074 if (confname && *confname)
1075 noffset = fit_conf_get_node(fit_hdr, confname);
1077 noffset = fit_conf_get_node(fit_hdr, NULL);
1081 printf("Could not find config %s\n", confname);
1085 if (verify && fit_config_verify(fit_hdr, noffset))
1088 noffset = fit_conf_get_prop_node(fit_hdr,
1095 printf("Could not find script in %s\n", confname);
1100 if (!fit_uname || !*fit_uname)
1101 fit_uname = get_default_image(fit_hdr);
1103 puts("No FIT subimage unit name\n");
1107 /* get script component image node offset */
1108 noffset = fit_image_get_node(fit_hdr, fit_uname);
1110 printf("Can't find '%s' FIT subimage\n",
1116 if (!fit_image_check_type(fit_hdr, noffset,
1118 puts("Not a image image\n");
1122 /* verify integrity */
1123 if (verify && !fit_image_verify(fit_hdr, noffset)) {
1124 puts("Bad Data Hash\n");
1128 /* get script subimage data address and length */
1129 if (fit_image_get_data_and_size(fit_hdr, noffset,
1130 &fit_data, &fit_len)) {
1131 puts("Could not find script subimage data\n");
1135 data = (u32 *)fit_data;
1136 len = (ulong)fit_len;
1140 goto exit_image_format;
1143 *datap = (char *)data;
1149 puts("Wrong image format for \"source\" command\n");