2 * (C) Copyright 2008 Semihalf
4 * (C) Copyright 2000-2006
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 * SPDX-License-Identifier: GPL-2.0+
14 #ifdef CONFIG_SHOW_BOOT_PROGRESS
15 #include <status_led.h>
18 #ifdef CONFIG_HAS_DATAFLASH
19 #include <dataflash.h>
22 #ifdef CONFIG_LOGBUFFER
28 #include <environment.h>
32 #if IMAGE_ENABLE_FIT || IMAGE_ENABLE_OF_LIBFDT
34 #include <fdt_support.h>
39 #include <u-boot/md5.h>
40 #include <u-boot/sha1.h>
41 #include <linux/errno.h>
45 extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
48 DECLARE_GLOBAL_DATA_PTR;
50 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
51 static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
56 #include <u-boot/md5.h>
60 #ifndef __maybe_unused
61 # define __maybe_unused /* unimplemented */
63 #endif /* !USE_HOSTCC*/
65 #include <u-boot/crc.h>
67 #ifndef CONFIG_SYS_BARGSIZE
68 #define CONFIG_SYS_BARGSIZE 512
71 static const table_entry_t uimage_arch[] = {
72 { IH_ARCH_INVALID, "invalid", "Invalid ARCH", },
73 { IH_ARCH_ALPHA, "alpha", "Alpha", },
74 { IH_ARCH_ARM, "arm", "ARM", },
75 { IH_ARCH_I386, "x86", "Intel x86", },
76 { IH_ARCH_IA64, "ia64", "IA64", },
77 { IH_ARCH_M68K, "m68k", "M68K", },
78 { IH_ARCH_MICROBLAZE, "microblaze", "MicroBlaze", },
79 { IH_ARCH_MIPS, "mips", "MIPS", },
80 { IH_ARCH_MIPS64, "mips64", "MIPS 64 Bit", },
81 { IH_ARCH_NIOS2, "nios2", "NIOS II", },
82 { IH_ARCH_PPC, "powerpc", "PowerPC", },
83 { IH_ARCH_PPC, "ppc", "PowerPC", },
84 { IH_ARCH_S390, "s390", "IBM S390", },
85 { IH_ARCH_SH, "sh", "SuperH", },
86 { IH_ARCH_SPARC, "sparc", "SPARC", },
87 { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", },
88 { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", },
89 { IH_ARCH_AVR32, "avr32", "AVR32", },
90 { IH_ARCH_NDS32, "nds32", "NDS32", },
91 { IH_ARCH_OPENRISC, "or1k", "OpenRISC 1000",},
92 { IH_ARCH_SANDBOX, "sandbox", "Sandbox", },
93 { IH_ARCH_ARM64, "arm64", "AArch64", },
94 { IH_ARCH_ARC, "arc", "ARC", },
95 { IH_ARCH_X86_64, "x86_64", "AMD x86_64", },
96 { IH_ARCH_XTENSA, "xtensa", "Xtensa", },
100 static const table_entry_t uimage_os[] = {
101 { IH_OS_INVALID, "invalid", "Invalid OS", },
102 { IH_OS_LINUX, "linux", "Linux", },
103 #if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
104 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
106 { IH_OS_NETBSD, "netbsd", "NetBSD", },
107 { IH_OS_OSE, "ose", "Enea OSE", },
108 { IH_OS_PLAN9, "plan9", "Plan 9", },
109 { IH_OS_RTEMS, "rtems", "RTEMS", },
110 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
111 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
112 #if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
113 { IH_OS_QNX, "qnx", "QNX", },
115 #if defined(CONFIG_INTEGRITY) || defined(USE_HOSTCC)
116 { IH_OS_INTEGRITY,"integrity", "INTEGRITY", },
119 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
120 { IH_OS_DELL, "dell", "Dell", },
121 { IH_OS_ESIX, "esix", "Esix", },
122 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
123 { IH_OS_IRIX, "irix", "Irix", },
124 { IH_OS_NCR, "ncr", "NCR", },
125 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
126 { IH_OS_PSOS, "psos", "pSOS", },
127 { IH_OS_SCO, "sco", "SCO", },
128 { IH_OS_SOLARIS, "solaris", "Solaris", },
129 { IH_OS_SVR4, "svr4", "SVR4", },
131 #if defined(CONFIG_BOOTM_OPENRTOS) || defined(USE_HOSTCC)
132 { IH_OS_OPENRTOS, "openrtos", "OpenRTOS", },
138 static const table_entry_t uimage_type[] = {
139 { IH_TYPE_AISIMAGE, "aisimage", "Davinci AIS image",},
140 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
141 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
142 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
143 { IH_TYPE_GPIMAGE, "gpimage", "TI Keystone SPL Image",},
144 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
145 { IH_TYPE_KERNEL_NOLOAD, "kernel_noload", "Kernel Image (no loading done)", },
146 { IH_TYPE_KWBIMAGE, "kwbimage", "Kirkwood Boot Image",},
147 { IH_TYPE_IMXIMAGE, "imximage", "Freescale i.MX Boot Image",},
148 { IH_TYPE_INVALID, "invalid", "Invalid Image", },
149 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
150 { IH_TYPE_OMAPIMAGE, "omapimage", "TI OMAP SPL With GP CH",},
151 { IH_TYPE_PBLIMAGE, "pblimage", "Freescale PBL Boot Image",},
152 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
153 { IH_TYPE_SCRIPT, "script", "Script", },
154 { IH_TYPE_SOCFPGAIMAGE, "socfpgaimage", "Altera SOCFPGA preloader",},
155 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
156 { IH_TYPE_UBLIMAGE, "ublimage", "Davinci UBL image",},
157 { IH_TYPE_MXSIMAGE, "mxsimage", "Freescale MXS Boot Image",},
158 { IH_TYPE_ATMELIMAGE, "atmelimage", "ATMEL ROM-Boot Image",},
159 { IH_TYPE_X86_SETUP, "x86_setup", "x86 setup.bin", },
160 { IH_TYPE_LPC32XXIMAGE, "lpc32xximage", "LPC32XX Boot Image", },
161 { IH_TYPE_RKIMAGE, "rkimage", "Rockchip Boot Image" },
162 { IH_TYPE_RKSD, "rksd", "Rockchip SD Boot Image" },
163 { IH_TYPE_RKSPI, "rkspi", "Rockchip SPI Boot Image" },
164 { IH_TYPE_VYBRIDIMAGE, "vybridimage", "Vybrid Boot Image", },
165 { IH_TYPE_ZYNQIMAGE, "zynqimage", "Xilinx Zynq Boot Image" },
166 { IH_TYPE_ZYNQMPIMAGE, "zynqmpimage", "Xilinx ZynqMP Boot Image" },
167 { IH_TYPE_FPGA, "fpga", "FPGA Image" },
168 { IH_TYPE_TEE, "tee", "Trusted Execution Environment Image",},
172 static const table_entry_t uimage_comp[] = {
173 { IH_COMP_NONE, "none", "uncompressed", },
174 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
175 { IH_COMP_GZIP, "gzip", "gzip compressed", },
176 { IH_COMP_LZMA, "lzma", "lzma compressed", },
177 { IH_COMP_LZO, "lzo", "lzo compressed", },
178 { IH_COMP_LZ4, "lz4", "lz4 compressed", },
185 const table_entry_t *table;
188 static const struct table_info table_info[IH_COUNT] = {
189 { "architecture", IH_ARCH_COUNT, uimage_arch },
190 { "compression", IH_COMP_COUNT, uimage_comp },
191 { "operating system", IH_OS_COUNT, uimage_os },
192 { "image type", IH_TYPE_COUNT, uimage_type },
195 /*****************************************************************************/
196 /* Legacy format routines */
197 /*****************************************************************************/
198 int image_check_hcrc(const image_header_t *hdr)
201 ulong len = image_get_header_size();
202 image_header_t header;
204 /* Copy header so we can blank CRC field for re-calculation */
205 memmove(&header, (char *)hdr, image_get_header_size());
206 image_set_hcrc(&header, 0);
208 hcrc = crc32(0, (unsigned char *)&header, len);
210 return (hcrc == image_get_hcrc(hdr));
213 int image_check_dcrc(const image_header_t *hdr)
215 ulong data = image_get_data(hdr);
216 ulong len = image_get_data_size(hdr);
217 ulong dcrc = crc32_wd(0, (unsigned char *)data, len, CHUNKSZ_CRC32);
219 return (dcrc == image_get_dcrc(hdr));
223 * image_multi_count - get component (sub-image) count
224 * @hdr: pointer to the header of the multi component image
226 * image_multi_count() returns number of components in a multi
229 * Note: no checking of the image type is done, caller must pass
230 * a valid multi component image.
233 * number of components
235 ulong image_multi_count(const image_header_t *hdr)
240 /* get start of the image payload, which in case of multi
241 * component images that points to a table of component sizes */
242 size = (uint32_t *)image_get_data(hdr);
244 /* count non empty slots */
245 for (i = 0; size[i]; ++i)
252 * image_multi_getimg - get component data address and size
253 * @hdr: pointer to the header of the multi component image
254 * @idx: index of the requested component
255 * @data: pointer to a ulong variable, will hold component data address
256 * @len: pointer to a ulong variable, will hold component size
258 * image_multi_getimg() returns size and data address for the requested
259 * component in a multi component image.
261 * Note: no checking of the image type is done, caller must pass
262 * a valid multi component image.
265 * data address and size of the component, if idx is valid
266 * 0 in data and len, if idx is out of range
268 void image_multi_getimg(const image_header_t *hdr, ulong idx,
269 ulong *data, ulong *len)
273 ulong offset, count, img_data;
275 /* get number of component */
276 count = image_multi_count(hdr);
278 /* get start of the image payload, which in case of multi
279 * component images that points to a table of component sizes */
280 size = (uint32_t *)image_get_data(hdr);
282 /* get address of the proper component data start, which means
283 * skipping sizes table (add 1 for last, null entry) */
284 img_data = image_get_data(hdr) + (count + 1) * sizeof(uint32_t);
287 *len = uimage_to_cpu(size[idx]);
290 /* go over all indices preceding requested component idx */
291 for (i = 0; i < idx; i++) {
292 /* add up i-th component size, rounding up to 4 bytes */
293 offset += (uimage_to_cpu(size[i]) + 3) & ~3 ;
296 /* calculate idx-th component data address */
297 *data = img_data + offset;
304 static void image_print_type(const image_header_t *hdr)
306 const char __maybe_unused *os, *arch, *type, *comp;
308 os = genimg_get_os_name(image_get_os(hdr));
309 arch = genimg_get_arch_name(image_get_arch(hdr));
310 type = genimg_get_type_name(image_get_type(hdr));
311 comp = genimg_get_comp_name(image_get_comp(hdr));
313 printf("%s %s %s (%s)\n", arch, os, type, comp);
317 * image_print_contents - prints out the contents of the legacy format image
318 * @ptr: pointer to the legacy format image header
319 * @p: pointer to prefix string
321 * image_print_contents() formats a multi line legacy image contents description.
322 * The routine prints out all header fields followed by the size/offset data
323 * for MULTI/SCRIPT images.
326 * no returned results
328 void image_print_contents(const void *ptr)
330 const image_header_t *hdr = (const image_header_t *)ptr;
331 const char __maybe_unused *p;
333 p = IMAGE_INDENT_STRING;
334 printf("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name(hdr));
335 if (IMAGE_ENABLE_TIMESTAMP) {
336 printf("%sCreated: ", p);
337 genimg_print_time((time_t)image_get_time(hdr));
339 printf("%sImage Type: ", p);
340 image_print_type(hdr);
341 printf("%sData Size: ", p);
342 genimg_print_size(image_get_data_size(hdr));
343 printf("%sLoad Address: %08x\n", p, image_get_load(hdr));
344 printf("%sEntry Point: %08x\n", p, image_get_ep(hdr));
346 if (image_check_type(hdr, IH_TYPE_MULTI) ||
347 image_check_type(hdr, IH_TYPE_SCRIPT)) {
350 ulong count = image_multi_count(hdr);
352 printf("%sContents:\n", p);
353 for (i = 0; i < count; i++) {
354 image_multi_getimg(hdr, i, &data, &len);
356 printf("%s Image %d: ", p, i);
357 genimg_print_size(len);
359 if (image_check_type(hdr, IH_TYPE_SCRIPT) && i > 0) {
361 * the user may need to know offsets
362 * if planning to do something with
365 printf("%s Offset = 0x%08lx\n", p, data);
373 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
375 * image_get_ramdisk - get and verify ramdisk image
376 * @rd_addr: ramdisk image start address
377 * @arch: expected ramdisk architecture
378 * @verify: checksum verification flag
380 * image_get_ramdisk() returns a pointer to the verified ramdisk image
381 * header. Routine receives image start address and expected architecture
382 * flag. Verification done covers data and header integrity and os/type/arch
385 * If dataflash support is enabled routine checks for dataflash addresses
386 * and handles required dataflash reads.
389 * pointer to a ramdisk image header, if image was found and valid
390 * otherwise, return NULL
392 static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
395 const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
397 if (!image_check_magic(rd_hdr)) {
398 puts("Bad Magic Number\n");
399 bootstage_error(BOOTSTAGE_ID_RD_MAGIC);
403 if (!image_check_hcrc(rd_hdr)) {
404 puts("Bad Header Checksum\n");
405 bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
409 bootstage_mark(BOOTSTAGE_ID_RD_MAGIC);
410 image_print_contents(rd_hdr);
413 puts(" Verifying Checksum ... ");
414 if (!image_check_dcrc(rd_hdr)) {
415 puts("Bad Data CRC\n");
416 bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM);
422 bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
424 if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
425 !image_check_arch(rd_hdr, arch) ||
426 !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
427 printf("No Linux %s Ramdisk Image\n",
428 genimg_get_arch_name(arch));
429 bootstage_error(BOOTSTAGE_ID_RAMDISK);
436 #endif /* !USE_HOSTCC */
438 /*****************************************************************************/
439 /* Shared dual-format routines */
440 /*****************************************************************************/
442 ulong load_addr = CONFIG_SYS_LOAD_ADDR; /* Default Load Address */
443 ulong save_addr; /* Default Save Address */
444 ulong save_size; /* Default Save Size (in bytes) */
446 static int on_loadaddr(const char *name, const char *value, enum env_op op,
451 case env_op_overwrite:
452 load_addr = simple_strtoul(value, NULL, 16);
460 U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
462 ulong getenv_bootm_low(void)
464 char *s = getenv("bootm_low");
466 ulong tmp = simple_strtoul(s, NULL, 16);
470 #if defined(CONFIG_SYS_SDRAM_BASE)
471 return CONFIG_SYS_SDRAM_BASE;
472 #elif defined(CONFIG_ARM)
473 return gd->bd->bi_dram[0].start;
479 phys_size_t getenv_bootm_size(void)
481 phys_size_t tmp, size;
483 char *s = getenv("bootm_size");
485 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
489 #if defined(CONFIG_ARM) && defined(CONFIG_NR_DRAM_BANKS)
490 start = gd->bd->bi_dram[0].start;
491 size = gd->bd->bi_dram[0].size;
493 start = gd->bd->bi_memstart;
494 size = gd->bd->bi_memsize;
497 s = getenv("bootm_low");
499 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
503 return size - (tmp - start);
506 phys_size_t getenv_bootm_mapsize(void)
509 char *s = getenv("bootm_mapsize");
511 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
515 #if defined(CONFIG_SYS_BOOTMAPSZ)
516 return CONFIG_SYS_BOOTMAPSZ;
518 return getenv_bootm_size();
522 void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
527 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
533 size_t tail = (len > chunksz) ? chunksz : len;
539 memmove(to, from, tail);
546 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
547 memmove(to, from, len);
548 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
550 #endif /* !USE_HOSTCC */
552 void genimg_print_size(uint32_t size)
555 printf("%d Bytes = ", size);
556 print_size(size, "\n");
558 printf("%d Bytes = %.2f kB = %.2f MB\n",
559 size, (double)size / 1.024e3,
560 (double)size / 1.048576e6);
564 #if IMAGE_ENABLE_TIMESTAMP
565 void genimg_print_time(time_t timestamp)
570 rtc_to_tm(timestamp, &tm);
571 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
572 tm.tm_year, tm.tm_mon, tm.tm_mday,
573 tm.tm_hour, tm.tm_min, tm.tm_sec);
575 printf("%s", ctime(×tamp));
580 const table_entry_t *get_table_entry(const table_entry_t *table, int id)
582 for (; table->id >= 0; ++table) {
589 static const char *unknown_msg(enum ih_category category)
591 static const char unknown_str[] = "Unknown ";
594 strcpy(msg, unknown_str);
595 strncat(msg, table_info[category].desc,
596 sizeof(msg) - sizeof(unknown_str));
602 * get_cat_table_entry_name - translate entry id to long name
603 * @category: category to look up (enum ih_category)
604 * @id: entry id to be translated
606 * This will scan the translation table trying to find the entry that matches
609 * @retur long entry name if translation succeeds; error string on failure
611 const char *genimg_get_cat_name(enum ih_category category, uint id)
613 const table_entry_t *entry;
615 entry = get_table_entry(table_info[category].table, id);
617 return unknown_msg(category);
618 #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
621 return entry->lname + gd->reloc_off;
626 * get_cat_table_entry_short_name - translate entry id to short name
627 * @category: category to look up (enum ih_category)
628 * @id: entry id to be translated
630 * This will scan the translation table trying to find the entry that matches
633 * @retur short entry name if translation succeeds; error string on failure
635 const char *genimg_get_cat_short_name(enum ih_category category, uint id)
637 const table_entry_t *entry;
639 entry = get_table_entry(table_info[category].table, id);
641 return unknown_msg(category);
642 #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
645 return entry->sname + gd->reloc_off;
649 int genimg_get_cat_count(enum ih_category category)
651 return table_info[category].count;
654 const char *genimg_get_cat_desc(enum ih_category category)
656 return table_info[category].desc;
660 * get_table_entry_name - translate entry id to long name
661 * @table: pointer to a translation table for entries of a specific type
662 * @msg: message to be returned when translation fails
663 * @id: entry id to be translated
665 * get_table_entry_name() will go over translation table trying to find
666 * entry that matches given id. If matching entry is found, its long
667 * name is returned to the caller.
670 * long entry name if translation succeeds
673 char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
675 table = get_table_entry(table, id);
678 #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
681 return table->lname + gd->reloc_off;
685 const char *genimg_get_os_name(uint8_t os)
687 return (get_table_entry_name(uimage_os, "Unknown OS", os));
690 const char *genimg_get_arch_name(uint8_t arch)
692 return (get_table_entry_name(uimage_arch, "Unknown Architecture",
696 const char *genimg_get_type_name(uint8_t type)
698 return (get_table_entry_name(uimage_type, "Unknown Image", type));
701 static const char *genimg_get_short_name(const table_entry_t *table, int val)
703 table = get_table_entry(table, val);
706 #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
709 return table->sname + gd->reloc_off;
713 const char *genimg_get_type_short_name(uint8_t type)
715 return genimg_get_short_name(uimage_type, type);
718 const char *genimg_get_comp_name(uint8_t comp)
720 return (get_table_entry_name(uimage_comp, "Unknown Compression",
724 const char *genimg_get_comp_short_name(uint8_t comp)
726 return genimg_get_short_name(uimage_comp, comp);
729 const char *genimg_get_os_short_name(uint8_t os)
731 return genimg_get_short_name(uimage_os, os);
734 const char *genimg_get_arch_short_name(uint8_t arch)
736 return genimg_get_short_name(uimage_arch, arch);
740 * get_table_entry_id - translate short entry name to id
741 * @table: pointer to a translation table for entries of a specific type
742 * @table_name: to be used in case of error
743 * @name: entry short name to be translated
745 * get_table_entry_id() will go over translation table trying to find
746 * entry that matches given short name. If matching entry is found,
747 * its id returned to the caller.
750 * entry id if translation succeeds
753 int get_table_entry_id(const table_entry_t *table,
754 const char *table_name, const char *name)
756 const table_entry_t *t;
758 for (t = table; t->id >= 0; ++t) {
759 #ifdef CONFIG_NEEDS_MANUAL_RELOC
760 if (t->sname && strcasecmp(t->sname + gd->reloc_off, name) == 0)
762 if (t->sname && strcasecmp(t->sname, name) == 0)
766 debug("Invalid %s Type: %s\n", table_name, name);
771 int genimg_get_os_id(const char *name)
773 return (get_table_entry_id(uimage_os, "OS", name));
776 int genimg_get_arch_id(const char *name)
778 return (get_table_entry_id(uimage_arch, "CPU", name));
781 int genimg_get_type_id(const char *name)
783 return (get_table_entry_id(uimage_type, "Image", name));
786 int genimg_get_comp_id(const char *name)
788 return (get_table_entry_id(uimage_comp, "Compression", name));
793 * genimg_get_kernel_addr_fit - get the real kernel address and return 2
795 * @img_addr: a string might contain real image address
796 * @fit_uname_config: double pointer to a char, will hold pointer to a
797 * configuration unit name
798 * @fit_uname_kernel: double pointer to a char, will hold pointer to a subimage
801 * genimg_get_kernel_addr_fit get the real kernel start address from a string
802 * which is normally the first argv of bootm/bootz
805 * kernel start address
807 ulong genimg_get_kernel_addr_fit(char * const img_addr,
808 const char **fit_uname_config,
809 const char **fit_uname_kernel)
813 /* find out kernel image address */
815 kernel_addr = load_addr;
816 debug("* kernel: default image load address = 0x%08lx\n",
818 #if CONFIG_IS_ENABLED(FIT)
819 } else if (fit_parse_conf(img_addr, load_addr, &kernel_addr,
821 debug("* kernel: config '%s' from image at 0x%08lx\n",
822 *fit_uname_config, kernel_addr);
823 } else if (fit_parse_subimage(img_addr, load_addr, &kernel_addr,
825 debug("* kernel: subimage '%s' from image at 0x%08lx\n",
826 *fit_uname_kernel, kernel_addr);
829 kernel_addr = simple_strtoul(img_addr, NULL, 16);
830 debug("* kernel: cmdline image address = 0x%08lx\n",
838 * genimg_get_kernel_addr() is the simple version of
839 * genimg_get_kernel_addr_fit(). It ignores those return FIT strings
841 ulong genimg_get_kernel_addr(char * const img_addr)
843 const char *fit_uname_config = NULL;
844 const char *fit_uname_kernel = NULL;
846 return genimg_get_kernel_addr_fit(img_addr, &fit_uname_config,
851 * genimg_get_format - get image format type
852 * @img_addr: image start address
854 * genimg_get_format() checks whether provided address points to a valid
855 * legacy or FIT image.
857 * New uImage format and FDT blob are based on a libfdt. FDT blob
858 * may be passed directly or embedded in a FIT image. In both situations
859 * genimg_get_format() must be able to dectect libfdt header.
862 * image format type or IMAGE_FORMAT_INVALID if no image is present
864 int genimg_get_format(const void *img_addr)
866 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
867 const image_header_t *hdr;
869 hdr = (const image_header_t *)img_addr;
870 if (image_check_magic(hdr))
871 return IMAGE_FORMAT_LEGACY;
873 #if IMAGE_ENABLE_FIT || IMAGE_ENABLE_OF_LIBFDT
874 if (fdt_check_header(img_addr) == 0)
875 return IMAGE_FORMAT_FIT;
877 #ifdef CONFIG_ANDROID_BOOT_IMAGE
878 if (android_image_check_header(img_addr) == 0)
879 return IMAGE_FORMAT_ANDROID;
882 return IMAGE_FORMAT_INVALID;
886 * genimg_get_image - get image from special storage (if necessary)
887 * @img_addr: image start address
889 * genimg_get_image() checks if provided image start address is located
890 * in a dataflash storage. If so, image is moved to a system RAM memory.
893 * image start address after possible relocation from special storage
895 ulong genimg_get_image(ulong img_addr)
897 ulong ram_addr = img_addr;
899 #ifdef CONFIG_HAS_DATAFLASH
900 ulong h_size, d_size;
902 if (addr_dataflash(img_addr)) {
905 /* ger RAM address */
906 ram_addr = CONFIG_SYS_LOAD_ADDR;
908 /* get header size */
909 h_size = image_get_header_size();
911 if (sizeof(struct fdt_header) > h_size)
912 h_size = sizeof(struct fdt_header);
916 debug(" Reading image header from dataflash address "
917 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
919 buf = map_sysmem(ram_addr, 0);
920 read_dataflash(img_addr, h_size, buf);
923 switch (genimg_get_format(buf)) {
924 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
925 case IMAGE_FORMAT_LEGACY:
926 d_size = image_get_data_size(buf);
927 debug(" Legacy format image found at 0x%08lx, "
933 case IMAGE_FORMAT_FIT:
934 d_size = fit_get_size(buf) - h_size;
935 debug(" FIT/FDT format image found at 0x%08lx, "
941 printf(" No valid image found at 0x%08lx\n",
946 /* read in image data */
947 debug(" Reading image remaining data from dataflash address "
948 "%08lx to RAM address %08lx\n", img_addr + h_size,
951 read_dataflash(img_addr + h_size, d_size,
952 (char *)(buf + h_size));
955 #endif /* CONFIG_HAS_DATAFLASH */
961 * fit_has_config - check if there is a valid FIT configuration
962 * @images: pointer to the bootm command headers structure
964 * fit_has_config() checks if there is a FIT configuration in use
965 * (if FTI support is present).
968 * 0, no FIT support or no configuration found
969 * 1, configuration found
971 int genimg_has_config(bootm_headers_t *images)
974 if (images->fit_uname_cfg)
981 * boot_get_ramdisk - main ramdisk handling routine
982 * @argc: command argument count
983 * @argv: command argument list
984 * @images: pointer to the bootm images structure
985 * @arch: expected ramdisk architecture
986 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
987 * @rd_end: pointer to a ulong variable, will hold ramdisk end
989 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
990 * Curently supported are the following ramdisk sources:
991 * - multicomponent kernel/ramdisk image,
992 * - commandline provided address of decicated ramdisk image.
995 * 0, if ramdisk image was found and valid, or skiped
996 * rd_start and rd_end are set to ramdisk start/end addresses if
997 * ramdisk image is found and valid
999 * 1, if ramdisk image is found but corrupted, or invalid
1000 * rd_start and rd_end are set to 0 if no ramdisk exists
1002 int boot_get_ramdisk(int argc, char * const argv[], bootm_headers_t *images,
1003 uint8_t arch, ulong *rd_start, ulong *rd_end)
1005 ulong rd_addr, rd_load;
1006 ulong rd_data, rd_len;
1007 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
1008 const image_header_t *rd_hdr;
1011 #ifdef CONFIG_SUPPORT_RAW_INITRD
1014 #if IMAGE_ENABLE_FIT
1015 const char *fit_uname_config = images->fit_uname_cfg;
1016 const char *fit_uname_ramdisk = NULL;
1020 const char *select = NULL;
1025 #ifdef CONFIG_ANDROID_BOOT_IMAGE
1027 * Look for an Android boot image.
1029 buf = map_sysmem(images->os.start, 0);
1030 if (buf && genimg_get_format(buf) == IMAGE_FORMAT_ANDROID)
1038 * Look for a '-' which indicates to ignore the
1041 if (select && strcmp(select, "-") == 0) {
1042 debug("## Skipping init Ramdisk\n");
1043 rd_len = rd_data = 0;
1044 } else if (select || genimg_has_config(images)) {
1045 #if IMAGE_ENABLE_FIT
1048 * If the init ramdisk comes from the FIT image and
1049 * the FIT image address is omitted in the command
1050 * line argument, try to use os FIT image address or
1051 * default load address.
1053 if (images->fit_uname_os)
1054 default_addr = (ulong)images->fit_hdr_os;
1056 default_addr = load_addr;
1058 if (fit_parse_conf(select, default_addr,
1059 &rd_addr, &fit_uname_config)) {
1060 debug("* ramdisk: config '%s' from image at "
1062 fit_uname_config, rd_addr);
1063 } else if (fit_parse_subimage(select, default_addr,
1064 &rd_addr, &fit_uname_ramdisk)) {
1065 debug("* ramdisk: subimage '%s' from image at "
1067 fit_uname_ramdisk, rd_addr);
1071 rd_addr = simple_strtoul(select, NULL, 16);
1072 debug("* ramdisk: cmdline image address = "
1076 #if IMAGE_ENABLE_FIT
1078 /* use FIT configuration provided in first bootm
1079 * command argument. If the property is not defined,
1082 rd_addr = map_to_sysmem(images->fit_hdr_os);
1083 rd_noffset = fit_get_node_from_config(images,
1084 FIT_RAMDISK_PROP, rd_addr);
1085 if (rd_noffset == -ENOENT)
1087 else if (rd_noffset < 0)
1092 /* copy from dataflash if needed */
1093 rd_addr = genimg_get_image(rd_addr);
1096 * Check if there is an initrd image at the
1097 * address provided in the second bootm argument
1098 * check image type, for FIT images get FIT node.
1100 buf = map_sysmem(rd_addr, 0);
1101 switch (genimg_get_format(buf)) {
1102 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
1103 case IMAGE_FORMAT_LEGACY:
1104 printf("## Loading init Ramdisk from Legacy "
1105 "Image at %08lx ...\n", rd_addr);
1107 bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
1108 rd_hdr = image_get_ramdisk(rd_addr, arch,
1114 rd_data = image_get_data(rd_hdr);
1115 rd_len = image_get_data_size(rd_hdr);
1116 rd_load = image_get_load(rd_hdr);
1119 #if IMAGE_ENABLE_FIT
1120 case IMAGE_FORMAT_FIT:
1121 rd_noffset = fit_image_load(images,
1122 rd_addr, &fit_uname_ramdisk,
1123 &fit_uname_config, arch,
1125 BOOTSTAGE_ID_FIT_RD_START,
1126 FIT_LOAD_OPTIONAL_NON_ZERO,
1131 images->fit_hdr_rd = map_sysmem(rd_addr, 0);
1132 images->fit_uname_rd = fit_uname_ramdisk;
1133 images->fit_noffset_rd = rd_noffset;
1136 #ifdef CONFIG_ANDROID_BOOT_IMAGE
1137 case IMAGE_FORMAT_ANDROID:
1138 android_image_get_ramdisk((void *)images->os.start,
1143 #ifdef CONFIG_SUPPORT_RAW_INITRD
1146 end = strchr(select, ':');
1148 rd_len = simple_strtoul(++end, NULL, 16);
1153 puts("Wrong Ramdisk Image Format\n");
1154 rd_data = rd_len = rd_load = 0;
1158 } else if (images->legacy_hdr_valid &&
1159 image_check_type(&images->legacy_hdr_os_copy,
1163 * Now check if we have a legacy mult-component image,
1164 * get second entry data start address and len.
1166 bootstage_mark(BOOTSTAGE_ID_RAMDISK);
1167 printf("## Loading init Ramdisk from multi component "
1168 "Legacy Image at %08lx ...\n",
1169 (ulong)images->legacy_hdr_os);
1171 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
1176 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
1177 rd_len = rd_data = 0;
1181 debug("## No init Ramdisk\n");
1183 *rd_start = rd_data;
1184 *rd_end = rd_data + rd_len;
1186 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
1187 *rd_start, *rd_end);
1192 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
1194 * boot_ramdisk_high - relocate init ramdisk
1195 * @lmb: pointer to lmb handle, will be used for memory mgmt
1196 * @rd_data: ramdisk data start address
1197 * @rd_len: ramdisk data length
1198 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
1199 * start address (after possible relocation)
1200 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
1201 * end address (after possible relocation)
1203 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
1204 * variable and if requested ramdisk data is moved to a specified location.
1206 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1207 * start/end addresses if ramdisk image start and len were provided,
1208 * otherwise set initrd_start and initrd_end set to zeros.
1214 int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
1215 ulong *initrd_start, ulong *initrd_end)
1219 int initrd_copy_to_ram = 1;
1221 if ((s = getenv("initrd_high")) != NULL) {
1222 /* a value of "no" or a similar string will act like 0,
1223 * turning the "load high" feature off. This is intentional.
1225 initrd_high = simple_strtoul(s, NULL, 16);
1226 if (initrd_high == ~0)
1227 initrd_copy_to_ram = 0;
1229 initrd_high = getenv_bootm_mapsize() + getenv_bootm_low();
1233 #ifdef CONFIG_LOGBUFFER
1234 /* Prevent initrd from overwriting logbuffer */
1235 lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
1238 debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1239 initrd_high, initrd_copy_to_ram);
1242 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
1243 debug(" in-place initrd\n");
1244 *initrd_start = rd_data;
1245 *initrd_end = rd_data + rd_len;
1246 lmb_reserve(lmb, rd_data, rd_len);
1249 *initrd_start = (ulong)lmb_alloc_base(lmb,
1250 rd_len, 0x1000, initrd_high);
1252 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
1255 if (*initrd_start == 0) {
1256 puts("ramdisk - allocation error\n");
1259 bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
1261 *initrd_end = *initrd_start + rd_len;
1262 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
1263 *initrd_start, *initrd_end);
1265 memmove_wd((void *)*initrd_start,
1266 (void *)rd_data, rd_len, CHUNKSZ);
1270 * Ensure the image is flushed to memory to handle
1271 * AMP boot scenarios in which we might not be
1274 flush_cache((unsigned long)*initrd_start, rd_len);
1282 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1283 *initrd_start, *initrd_end);
1290 #endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
1292 int boot_get_setup(bootm_headers_t *images, uint8_t arch,
1293 ulong *setup_start, ulong *setup_len)
1295 #if IMAGE_ENABLE_FIT
1296 return boot_get_setup_fit(images, arch, setup_start, setup_len);
1302 #if IMAGE_ENABLE_FIT
1303 #if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_XILINX)
1304 int boot_get_fpga(int argc, char * const argv[], bootm_headers_t *images,
1305 uint8_t arch, const ulong *ld_start, ulong * const ld_len)
1307 ulong tmp_img_addr, img_data, img_len;
1311 const char *uname, *name;
1313 int devnum = 0; /* TODO support multi fpga platforms */
1314 const fpga_desc * const desc = fpga_get_desc(devnum);
1315 xilinx_desc *desc_xilinx = desc->devdesc;
1317 /* Check to see if the images struct has a FIT configuration */
1318 if (!genimg_has_config(images)) {
1319 debug("## FIT configuration was not specified\n");
1324 * Obtain the os FIT header from the images struct
1325 * copy from dataflash if needed
1327 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
1328 tmp_img_addr = genimg_get_image(tmp_img_addr);
1329 buf = map_sysmem(tmp_img_addr, 0);
1331 * Check image type. For FIT images get FIT node
1332 * and attempt to locate a generic binary.
1334 switch (genimg_get_format(buf)) {
1335 case IMAGE_FORMAT_FIT:
1336 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
1338 uname = fdt_stringlist_get(buf, conf_noffset, FIT_FPGA_PROP, 0,
1341 debug("## FPGA image is not specified\n");
1344 fit_img_result = fit_image_load(images,
1346 (const char **)&uname,
1347 &(images->fit_uname_cfg),
1350 BOOTSTAGE_ID_FPGA_INIT,
1351 FIT_LOAD_OPTIONAL_NON_ZERO,
1352 &img_data, &img_len);
1354 debug("FPGA image (%s) loaded to 0x%lx/size 0x%lx\n",
1355 uname, img_data, img_len);
1357 if (fit_img_result < 0) {
1358 /* Something went wrong! */
1359 return fit_img_result;
1362 if (img_len >= desc_xilinx->size) {
1364 err = fpga_loadbitstream(devnum, (char *)img_data,
1367 err = fpga_load(devnum, (const void *)img_data,
1371 err = fpga_loadbitstream(devnum, (char *)img_data,
1372 img_len, BIT_PARTIAL);
1374 err = fpga_load(devnum, (const void *)img_data,
1375 img_len, BIT_PARTIAL);
1378 printf(" Programming %s bitstream... ", name);
1385 printf("The given image format is not supported (corrupt?)\n");
1393 static void fit_loadable_process(uint8_t img_type,
1398 const unsigned int count =
1399 ll_entry_count(struct fit_loadable_tbl, fit_loadable);
1400 struct fit_loadable_tbl *fit_loadable_handler =
1401 ll_entry_start(struct fit_loadable_tbl, fit_loadable);
1402 /* For each loadable handler */
1403 for (i = 0; i < count; i++, fit_loadable_handler++)
1404 /* matching this type */
1405 if (fit_loadable_handler->type == img_type)
1406 /* call that handler with this image data */
1407 fit_loadable_handler->handler(img_data, img_len);
1410 int boot_get_loadable(int argc, char * const argv[], bootm_headers_t *images,
1411 uint8_t arch, const ulong *ld_start, ulong * const ld_len)
1414 * These variables are used to hold the current image location
1419 * These two variables are requirements for fit_image_load, but
1420 * their values are not used
1422 ulong img_data, img_len;
1424 int loadables_index;
1430 /* Check to see if the images struct has a FIT configuration */
1431 if (!genimg_has_config(images)) {
1432 debug("## FIT configuration was not specified\n");
1437 * Obtain the os FIT header from the images struct
1438 * copy from dataflash if needed
1440 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
1441 tmp_img_addr = genimg_get_image(tmp_img_addr);
1442 buf = map_sysmem(tmp_img_addr, 0);
1444 * Check image type. For FIT images get FIT node
1445 * and attempt to locate a generic binary.
1447 switch (genimg_get_format(buf)) {
1448 case IMAGE_FORMAT_FIT:
1449 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
1451 for (loadables_index = 0;
1452 uname = fdt_stringlist_get(buf, conf_noffset,
1453 FIT_LOADABLE_PROP, loadables_index,
1457 fit_img_result = fit_image_load(images,
1460 &(images->fit_uname_cfg), arch,
1462 BOOTSTAGE_ID_FIT_LOADABLE_START,
1463 FIT_LOAD_OPTIONAL_NON_ZERO,
1464 &img_data, &img_len);
1465 if (fit_img_result < 0) {
1466 /* Something went wrong! */
1467 return fit_img_result;
1470 fit_img_result = fit_image_get_node(buf, uname);
1471 if (fit_img_result < 0) {
1472 /* Something went wrong! */
1473 return fit_img_result;
1475 fit_img_result = fit_image_get_type(buf,
1478 if (fit_img_result < 0) {
1479 /* Something went wrong! */
1480 return fit_img_result;
1483 fit_loadable_process(img_type, img_data, img_len);
1487 printf("The given image format is not supported (corrupt?)\n");
1495 #ifdef CONFIG_SYS_BOOT_GET_CMDLINE
1497 * boot_get_cmdline - allocate and initialize kernel cmdline
1498 * @lmb: pointer to lmb handle, will be used for memory mgmt
1499 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1500 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1502 * boot_get_cmdline() allocates space for kernel command line below
1503 * BOOTMAPSZ + getenv_bootm_low() address. If "bootargs" U-Boot environemnt
1504 * variable is present its contents is copied to allocated kernel
1511 int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
1516 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
1517 getenv_bootm_mapsize() + getenv_bootm_low());
1519 if (cmdline == NULL)
1522 if ((s = getenv("bootargs")) == NULL)
1527 *cmd_start = (ulong) & cmdline[0];
1528 *cmd_end = *cmd_start + strlen(cmdline);
1530 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1534 #endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
1536 #ifdef CONFIG_SYS_BOOT_GET_KBD
1538 * boot_get_kbd - allocate and initialize kernel copy of board info
1539 * @lmb: pointer to lmb handle, will be used for memory mgmt
1540 * @kbd: double pointer to board info data
1542 * boot_get_kbd() allocates space for kernel copy of board info data below
1543 * BOOTMAPSZ + getenv_bootm_low() address and kernel board info is initialized
1544 * with the current u-boot board info data.
1550 int boot_get_kbd(struct lmb *lmb, bd_t **kbd)
1552 *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
1553 getenv_bootm_mapsize() + getenv_bootm_low());
1559 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1561 #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1562 do_bdinfo(NULL, 0, 0, NULL);
1567 #endif /* CONFIG_SYS_BOOT_GET_KBD */
1570 int image_setup_linux(bootm_headers_t *images)
1572 ulong of_size = images->ft_len;
1573 char **of_flat_tree = &images->ft_addr;
1574 ulong *initrd_start = &images->initrd_start;
1575 ulong *initrd_end = &images->initrd_end;
1576 struct lmb *lmb = &images->lmb;
1580 if (IMAGE_ENABLE_OF_LIBFDT)
1581 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
1583 if (IMAGE_BOOT_GET_CMDLINE) {
1584 ret = boot_get_cmdline(lmb, &images->cmdline_start,
1585 &images->cmdline_end);
1587 puts("ERROR with allocation of cmdline\n");
1591 if (IMAGE_ENABLE_RAMDISK_HIGH) {
1592 rd_len = images->rd_end - images->rd_start;
1593 ret = boot_ramdisk_high(lmb, images->rd_start, rd_len,
1594 initrd_start, initrd_end);
1599 if (IMAGE_ENABLE_OF_LIBFDT) {
1600 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
1605 if (IMAGE_ENABLE_OF_LIBFDT && of_size) {
1606 ret = image_setup_libfdt(images, *of_flat_tree, of_size, lmb);
1613 #endif /* CONFIG_LMB */
1614 #endif /* !USE_HOSTCC */