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>
19 #include <asm/cache.h>
20 #include <asm/global_data.h>
22 #ifndef CONFIG_SYS_BARGSIZE
23 #define CONFIG_SYS_BARGSIZE 512
26 DECLARE_GLOBAL_DATA_PTR;
28 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
30 * image_get_ramdisk - get and verify ramdisk image
31 * @rd_addr: ramdisk image start address
32 * @arch: expected ramdisk architecture
33 * @verify: checksum verification flag
35 * image_get_ramdisk() returns a pointer to the verified ramdisk image
36 * header. Routine receives image start address and expected architecture
37 * flag. Verification done covers data and header integrity and os/type/arch
41 * pointer to a ramdisk image header, if image was found and valid
42 * otherwise, return NULL
44 static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
47 const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
49 if (!image_check_magic(rd_hdr)) {
50 puts("Bad Magic Number\n");
51 bootstage_error(BOOTSTAGE_ID_RD_MAGIC);
55 if (!image_check_hcrc(rd_hdr)) {
56 puts("Bad Header Checksum\n");
57 bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
61 bootstage_mark(BOOTSTAGE_ID_RD_MAGIC);
62 image_print_contents(rd_hdr);
65 puts(" Verifying Checksum ... ");
66 if (!image_check_dcrc(rd_hdr)) {
67 puts("Bad Data CRC\n");
68 bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM);
74 bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
76 if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
77 !image_check_arch(rd_hdr, arch) ||
78 !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
79 printf("No Linux %s Ramdisk Image\n",
80 genimg_get_arch_name(arch));
81 bootstage_error(BOOTSTAGE_ID_RAMDISK);
89 /*****************************************************************************/
90 /* Shared dual-format routines */
91 /*****************************************************************************/
92 ulong image_load_addr = CONFIG_SYS_LOAD_ADDR; /* Default Load Address */
93 ulong image_save_addr; /* Default Save Address */
94 ulong image_save_size; /* Default Save Size (in bytes) */
96 static int on_loadaddr(const char *name, const char *value, enum env_op op,
101 case env_op_overwrite:
102 image_load_addr = hextoul(value, NULL);
110 U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
112 ulong env_get_bootm_low(void)
114 char *s = env_get("bootm_low");
116 ulong tmp = hextoul(s, NULL);
120 #if defined(CONFIG_SYS_SDRAM_BASE)
121 return CONFIG_SYS_SDRAM_BASE;
122 #elif defined(CONFIG_ARM) || defined(CONFIG_MICROBLAZE) || defined(CONFIG_RISCV)
123 return gd->bd->bi_dram[0].start;
129 phys_size_t env_get_bootm_size(void)
131 phys_size_t tmp, size;
133 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");
159 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
163 #if defined(CONFIG_SYS_BOOTMAPSZ)
164 return CONFIG_SYS_BOOTMAPSZ;
166 return env_get_bootm_size();
170 void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
175 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
181 size_t tail = (len > chunksz) ? chunksz : len;
187 memmove(to, from, tail);
194 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
195 memmove(to, from, len);
196 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
200 * genimg_get_kernel_addr_fit - get the real kernel address and return 2
202 * @img_addr: a string might contain real image address
203 * @fit_uname_config: double pointer to a char, will hold pointer to a
204 * configuration unit name
205 * @fit_uname_kernel: double pointer to a char, will hold pointer to a subimage
208 * genimg_get_kernel_addr_fit get the real kernel start address from a string
209 * which is normally the first argv of bootm/bootz
212 * kernel start address
214 ulong genimg_get_kernel_addr_fit(char * const img_addr,
215 const char **fit_uname_config,
216 const char **fit_uname_kernel)
220 /* find out kernel image address */
222 kernel_addr = image_load_addr;
223 debug("* kernel: default image load address = 0x%08lx\n",
225 #if CONFIG_IS_ENABLED(FIT)
226 } else if (fit_parse_conf(img_addr, image_load_addr, &kernel_addr,
228 debug("* kernel: config '%s' from image at 0x%08lx\n",
229 *fit_uname_config, kernel_addr);
230 } else if (fit_parse_subimage(img_addr, image_load_addr, &kernel_addr,
232 debug("* kernel: subimage '%s' from image at 0x%08lx\n",
233 *fit_uname_kernel, kernel_addr);
236 kernel_addr = hextoul(img_addr, NULL);
237 debug("* kernel: cmdline image address = 0x%08lx\n",
245 * genimg_get_kernel_addr() is the simple version of
246 * genimg_get_kernel_addr_fit(). It ignores those return FIT strings
248 ulong genimg_get_kernel_addr(char * const img_addr)
250 const char *fit_uname_config = NULL;
251 const char *fit_uname_kernel = NULL;
253 return genimg_get_kernel_addr_fit(img_addr, &fit_uname_config,
258 * genimg_get_format - get image format type
259 * @img_addr: image start address
261 * genimg_get_format() checks whether provided address points to a valid
262 * legacy or FIT image.
264 * New uImage format and FDT blob are based on a libfdt. FDT blob
265 * may be passed directly or embedded in a FIT image. In both situations
266 * genimg_get_format() must be able to dectect libfdt header.
269 * image format type or IMAGE_FORMAT_INVALID if no image is present
271 int genimg_get_format(const void *img_addr)
273 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
274 const image_header_t *hdr;
276 hdr = (const image_header_t *)img_addr;
277 if (image_check_magic(hdr))
278 return IMAGE_FORMAT_LEGACY;
280 #if IMAGE_ENABLE_FIT || IMAGE_ENABLE_OF_LIBFDT
281 if (fdt_check_header(img_addr) == 0)
282 return IMAGE_FORMAT_FIT;
284 #ifdef CONFIG_ANDROID_BOOT_IMAGE
285 if (android_image_check_header(img_addr) == 0)
286 return IMAGE_FORMAT_ANDROID;
289 return IMAGE_FORMAT_INVALID;
293 * fit_has_config - check if there is a valid FIT configuration
294 * @images: pointer to the bootm command headers structure
296 * fit_has_config() checks if there is a FIT configuration in use
297 * (if FTI support is present).
300 * 0, no FIT support or no configuration found
301 * 1, configuration found
303 int genimg_has_config(bootm_headers_t *images)
306 if (images->fit_uname_cfg)
313 * boot_get_ramdisk - main ramdisk handling routine
314 * @argc: command argument count
315 * @argv: command argument list
316 * @images: pointer to the bootm images structure
317 * @arch: expected ramdisk architecture
318 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
319 * @rd_end: pointer to a ulong variable, will hold ramdisk end
321 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
322 * Curently supported are the following ramdisk sources:
323 * - multicomponent kernel/ramdisk image,
324 * - commandline provided address of decicated ramdisk image.
327 * 0, if ramdisk image was found and valid, or skiped
328 * rd_start and rd_end are set to ramdisk start/end addresses if
329 * ramdisk image is found and valid
331 * 1, if ramdisk image is found but corrupted, or invalid
332 * rd_start and rd_end are set to 0 if no ramdisk exists
334 int boot_get_ramdisk(int argc, char *const argv[], bootm_headers_t *images,
335 uint8_t arch, ulong *rd_start, ulong *rd_end)
337 ulong rd_addr, rd_load;
338 ulong rd_data, rd_len;
339 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
340 const image_header_t *rd_hdr;
343 #ifdef CONFIG_SUPPORT_RAW_INITRD
347 const char *fit_uname_config = images->fit_uname_cfg;
348 const char *fit_uname_ramdisk = NULL;
352 const char *select = NULL;
357 #ifdef CONFIG_ANDROID_BOOT_IMAGE
359 * Look for an Android boot image.
361 buf = map_sysmem(images->os.start, 0);
362 if (buf && genimg_get_format(buf) == IMAGE_FORMAT_ANDROID)
363 select = (argc == 0) ? env_get("loadaddr") : argv[0];
370 * Look for a '-' which indicates to ignore the
373 if (select && strcmp(select, "-") == 0) {
374 debug("## Skipping init Ramdisk\n");
375 rd_len = rd_data = 0;
376 } else if (select || genimg_has_config(images)) {
380 * If the init ramdisk comes from the FIT image and
381 * the FIT image address is omitted in the command
382 * line argument, try to use os FIT image address or
383 * default load address.
385 if (images->fit_uname_os)
386 default_addr = (ulong)images->fit_hdr_os;
388 default_addr = image_load_addr;
390 if (fit_parse_conf(select, default_addr,
391 &rd_addr, &fit_uname_config)) {
392 debug("* ramdisk: config '%s' from image at "
394 fit_uname_config, rd_addr);
395 } else if (fit_parse_subimage(select, default_addr,
396 &rd_addr, &fit_uname_ramdisk)) {
397 debug("* ramdisk: subimage '%s' from image at "
399 fit_uname_ramdisk, rd_addr);
403 rd_addr = hextoul(select, NULL);
404 debug("* ramdisk: cmdline image address = "
410 /* use FIT configuration provided in first bootm
411 * command argument. If the property is not defined,
414 rd_addr = map_to_sysmem(images->fit_hdr_os);
415 rd_noffset = fit_get_node_from_config(images,
416 FIT_RAMDISK_PROP, rd_addr);
417 if (rd_noffset == -ENOENT)
419 else if (rd_noffset < 0)
425 * Check if there is an initrd image at the
426 * address provided in the second bootm argument
427 * check image type, for FIT images get FIT node.
429 buf = map_sysmem(rd_addr, 0);
430 switch (genimg_get_format(buf)) {
431 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
432 case IMAGE_FORMAT_LEGACY:
433 printf("## Loading init Ramdisk from Legacy "
434 "Image at %08lx ...\n", rd_addr);
436 bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
437 rd_hdr = image_get_ramdisk(rd_addr, arch,
443 rd_data = image_get_data(rd_hdr);
444 rd_len = image_get_data_size(rd_hdr);
445 rd_load = image_get_load(rd_hdr);
449 case IMAGE_FORMAT_FIT:
450 rd_noffset = fit_image_load(images,
451 rd_addr, &fit_uname_ramdisk,
452 &fit_uname_config, arch,
454 BOOTSTAGE_ID_FIT_RD_START,
455 FIT_LOAD_OPTIONAL_NON_ZERO,
460 images->fit_hdr_rd = map_sysmem(rd_addr, 0);
461 images->fit_uname_rd = fit_uname_ramdisk;
462 images->fit_noffset_rd = rd_noffset;
465 #ifdef CONFIG_ANDROID_BOOT_IMAGE
466 case IMAGE_FORMAT_ANDROID:
467 android_image_get_ramdisk((void *)images->os.start,
472 #ifdef CONFIG_SUPPORT_RAW_INITRD
475 end = strchr(select, ':');
477 rd_len = hextoul(++end, NULL);
482 puts("Wrong Ramdisk Image Format\n");
483 rd_data = rd_len = rd_load = 0;
487 } else if (images->legacy_hdr_valid &&
488 image_check_type(&images->legacy_hdr_os_copy,
492 * Now check if we have a legacy mult-component image,
493 * get second entry data start address and len.
495 bootstage_mark(BOOTSTAGE_ID_RAMDISK);
496 printf("## Loading init Ramdisk from multi component "
497 "Legacy Image at %08lx ...\n",
498 (ulong)images->legacy_hdr_os);
500 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
505 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
506 rd_len = rd_data = 0;
510 debug("## No init Ramdisk\n");
513 *rd_end = rd_data + rd_len;
515 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
521 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
523 * boot_ramdisk_high - relocate init ramdisk
524 * @lmb: pointer to lmb handle, will be used for memory mgmt
525 * @rd_data: ramdisk data start address
526 * @rd_len: ramdisk data length
527 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
528 * start address (after possible relocation)
529 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
530 * end address (after possible relocation)
532 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
533 * variable and if requested ramdisk data is moved to a specified location.
535 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
536 * start/end addresses if ramdisk image start and len were provided,
537 * otherwise set initrd_start and initrd_end set to zeros.
543 int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
544 ulong *initrd_start, ulong *initrd_end)
548 int initrd_copy_to_ram = 1;
550 s = env_get("initrd_high");
552 /* a value of "no" or a similar string will act like 0,
553 * turning the "load high" feature off. This is intentional.
555 initrd_high = hextoul(s, NULL);
556 if (initrd_high == ~0)
557 initrd_copy_to_ram = 0;
559 initrd_high = env_get_bootm_mapsize() + env_get_bootm_low();
563 debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
564 initrd_high, initrd_copy_to_ram);
567 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
568 debug(" in-place initrd\n");
569 *initrd_start = rd_data;
570 *initrd_end = rd_data + rd_len;
571 lmb_reserve(lmb, rd_data, rd_len);
574 *initrd_start = (ulong)lmb_alloc_base(lmb,
575 rd_len, 0x1000, initrd_high);
577 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
580 if (*initrd_start == 0) {
581 puts("ramdisk - allocation error\n");
584 bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
586 *initrd_end = *initrd_start + rd_len;
587 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
588 *initrd_start, *initrd_end);
590 memmove_wd((void *)*initrd_start,
591 (void *)rd_data, rd_len, CHUNKSZ);
595 * Ensure the image is flushed to memory to handle
596 * AMP boot scenarios in which we might not be
599 flush_cache((unsigned long)*initrd_start,
600 ALIGN(rd_len, ARCH_DMA_MINALIGN));
608 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
609 *initrd_start, *initrd_end);
616 #endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
618 int boot_get_setup(bootm_headers_t *images, uint8_t arch,
619 ulong *setup_start, ulong *setup_len)
622 return boot_get_setup_fit(images, arch, setup_start, setup_len);
629 #if defined(CONFIG_FPGA)
630 int boot_get_fpga(int argc, char *const argv[], bootm_headers_t *images,
631 uint8_t arch, const ulong *ld_start, ulong * const ld_len)
633 ulong tmp_img_addr, img_data, img_len;
637 const char *uname, *name;
639 int devnum = 0; /* TODO support multi fpga platforms */
641 /* Check to see if the images struct has a FIT configuration */
642 if (!genimg_has_config(images)) {
643 debug("## FIT configuration was not specified\n");
648 * Obtain the os FIT header from the images struct
650 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
651 buf = map_sysmem(tmp_img_addr, 0);
653 * Check image type. For FIT images get FIT node
654 * and attempt to locate a generic binary.
656 switch (genimg_get_format(buf)) {
657 case IMAGE_FORMAT_FIT:
658 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
660 uname = fdt_stringlist_get(buf, conf_noffset, FIT_FPGA_PROP, 0,
663 debug("## FPGA image is not specified\n");
666 fit_img_result = fit_image_load(images,
668 (const char **)&uname,
669 &(images->fit_uname_cfg),
672 BOOTSTAGE_ID_FPGA_INIT,
673 FIT_LOAD_OPTIONAL_NON_ZERO,
674 &img_data, &img_len);
676 debug("FPGA image (%s) loaded to 0x%lx/size 0x%lx\n",
677 uname, img_data, img_len);
679 if (fit_img_result < 0) {
680 /* Something went wrong! */
681 return fit_img_result;
684 if (!fpga_is_partial_data(devnum, img_len)) {
686 err = fpga_loadbitstream(devnum, (char *)img_data,
689 err = fpga_load(devnum, (const void *)img_data,
693 err = fpga_loadbitstream(devnum, (char *)img_data,
694 img_len, BIT_PARTIAL);
696 err = fpga_load(devnum, (const void *)img_data,
697 img_len, BIT_PARTIAL);
703 printf(" Programming %s bitstream... OK\n", name);
706 printf("The given image format is not supported (corrupt?)\n");
714 static void fit_loadable_process(uint8_t img_type,
719 const unsigned int count =
720 ll_entry_count(struct fit_loadable_tbl, fit_loadable);
721 struct fit_loadable_tbl *fit_loadable_handler =
722 ll_entry_start(struct fit_loadable_tbl, fit_loadable);
723 /* For each loadable handler */
724 for (i = 0; i < count; i++, fit_loadable_handler++)
725 /* matching this type */
726 if (fit_loadable_handler->type == img_type)
727 /* call that handler with this image data */
728 fit_loadable_handler->handler(img_data, img_len);
731 int boot_get_loadable(int argc, char *const argv[], bootm_headers_t *images,
732 uint8_t arch, const ulong *ld_start, ulong * const ld_len)
735 * These variables are used to hold the current image location
740 * These two variables are requirements for fit_image_load, but
741 * their values are not used
743 ulong img_data, img_len;
751 /* Check to see if the images struct has a FIT configuration */
752 if (!genimg_has_config(images)) {
753 debug("## FIT configuration was not specified\n");
758 * Obtain the os FIT header from the images struct
760 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
761 buf = map_sysmem(tmp_img_addr, 0);
763 * Check image type. For FIT images get FIT node
764 * and attempt to locate a generic binary.
766 switch (genimg_get_format(buf)) {
767 case IMAGE_FORMAT_FIT:
768 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
770 for (loadables_index = 0;
771 uname = fdt_stringlist_get(buf, conf_noffset,
772 FIT_LOADABLE_PROP, loadables_index,
776 fit_img_result = fit_image_load(images,
779 &(images->fit_uname_cfg), arch,
781 BOOTSTAGE_ID_FIT_LOADABLE_START,
782 FIT_LOAD_OPTIONAL_NON_ZERO,
783 &img_data, &img_len);
784 if (fit_img_result < 0) {
785 /* Something went wrong! */
786 return fit_img_result;
789 fit_img_result = fit_image_get_node(buf, uname);
790 if (fit_img_result < 0) {
791 /* Something went wrong! */
792 return fit_img_result;
794 fit_img_result = fit_image_get_type(buf,
797 if (fit_img_result < 0) {
798 /* Something went wrong! */
799 return fit_img_result;
802 fit_loadable_process(img_type, img_data, img_len);
806 printf("The given image format is not supported (corrupt?)\n");
814 #ifdef CONFIG_SYS_BOOT_GET_CMDLINE
816 * boot_get_cmdline - allocate and initialize kernel cmdline
817 * @lmb: pointer to lmb handle, will be used for memory mgmt
818 * @cmd_start: pointer to a ulong variable, will hold cmdline start
819 * @cmd_end: pointer to a ulong variable, will hold cmdline end
821 * boot_get_cmdline() allocates space for kernel command line below
822 * BOOTMAPSZ + env_get_bootm_low() address. If "bootargs" U-Boot environment
823 * variable is present its contents is copied to allocated kernel
830 int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
835 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
836 env_get_bootm_mapsize() + env_get_bootm_low());
841 s = env_get("bootargs");
847 *cmd_start = (ulong) & cmdline[0];
848 *cmd_end = *cmd_start + strlen(cmdline);
850 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
854 #endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
856 #ifdef CONFIG_SYS_BOOT_GET_KBD
858 * boot_get_kbd - allocate and initialize kernel copy of board info
859 * @lmb: pointer to lmb handle, will be used for memory mgmt
860 * @kbd: double pointer to board info data
862 * boot_get_kbd() allocates space for kernel copy of board info data below
863 * BOOTMAPSZ + env_get_bootm_low() address and kernel board info is initialized
864 * with the current u-boot board info data.
870 int boot_get_kbd(struct lmb *lmb, struct bd_info **kbd)
872 *kbd = (struct bd_info *)(ulong)lmb_alloc_base(lmb,
873 sizeof(struct bd_info),
875 env_get_bootm_mapsize() + env_get_bootm_low());
881 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
883 #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
884 do_bdinfo(NULL, 0, 0, NULL);
889 #endif /* CONFIG_SYS_BOOT_GET_KBD */
892 int image_setup_linux(bootm_headers_t *images)
894 ulong of_size = images->ft_len;
895 char **of_flat_tree = &images->ft_addr;
896 struct lmb *lmb = &images->lmb;
899 if (IMAGE_ENABLE_OF_LIBFDT)
900 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
902 if (IMAGE_BOOT_GET_CMDLINE) {
903 ret = boot_get_cmdline(lmb, &images->cmdline_start,
904 &images->cmdline_end);
906 puts("ERROR with allocation of cmdline\n");
911 if (IMAGE_ENABLE_OF_LIBFDT) {
912 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
917 if (IMAGE_ENABLE_OF_LIBFDT && of_size) {
918 ret = image_setup_libfdt(images, *of_flat_tree, of_size, lmb);
925 #endif /* CONFIG_LMB */