1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2008 Semihalf
5 * (C) Copyright 2000-2006
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
12 #include <u-boot/crc.h>
15 #ifdef CONFIG_SHOW_BOOT_PROGRESS
16 #include <status_led.h>
25 #if IMAGE_ENABLE_FIT || IMAGE_ENABLE_OF_LIBFDT
26 #include <linux/libfdt.h>
27 #include <fdt_support.h>
32 #include <u-boot/md5.h>
33 #include <u-boot/sha1.h>
34 #include <linux/errno.h>
38 #include <linux/lzo.h>
39 #include <lzma/LzmaTypes.h>
40 #include <lzma/LzmaDec.h>
41 #include <lzma/LzmaTools.h>
44 extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
47 DECLARE_GLOBAL_DATA_PTR;
49 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
50 static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
55 #include <u-boot/md5.h>
59 #ifndef __maybe_unused
60 # define __maybe_unused /* unimplemented */
62 #endif /* !USE_HOSTCC*/
64 #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", },
97 { IH_ARCH_RISCV, "riscv", "RISC-V", },
101 static const table_entry_t uimage_os[] = {
102 { IH_OS_INVALID, "invalid", "Invalid OS", },
103 { IH_OS_ARM_TRUSTED_FIRMWARE, "arm-trusted-firmware", "ARM Trusted Firmware" },
104 { IH_OS_LINUX, "linux", "Linux", },
105 #if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
106 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
108 { IH_OS_NETBSD, "netbsd", "NetBSD", },
109 { IH_OS_OSE, "ose", "Enea OSE", },
110 { IH_OS_PLAN9, "plan9", "Plan 9", },
111 { IH_OS_RTEMS, "rtems", "RTEMS", },
112 { IH_OS_TEE, "tee", "Trusted Execution Environment" },
113 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
114 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
115 #if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
116 { IH_OS_QNX, "qnx", "QNX", },
118 #if defined(CONFIG_INTEGRITY) || defined(USE_HOSTCC)
119 { IH_OS_INTEGRITY,"integrity", "INTEGRITY", },
122 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
123 { IH_OS_DELL, "dell", "Dell", },
124 { IH_OS_ESIX, "esix", "Esix", },
125 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
126 { IH_OS_IRIX, "irix", "Irix", },
127 { IH_OS_NCR, "ncr", "NCR", },
128 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
129 { IH_OS_PSOS, "psos", "pSOS", },
130 { IH_OS_SCO, "sco", "SCO", },
131 { IH_OS_SOLARIS, "solaris", "Solaris", },
132 { IH_OS_SVR4, "svr4", "SVR4", },
134 #if defined(CONFIG_BOOTM_OPENRTOS) || defined(USE_HOSTCC)
135 { IH_OS_OPENRTOS, "openrtos", "OpenRTOS", },
137 { IH_OS_OPENSBI, "opensbi", "RISC-V OpenSBI", },
142 static const table_entry_t uimage_type[] = {
143 { IH_TYPE_AISIMAGE, "aisimage", "Davinci AIS image",},
144 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
145 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
146 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
147 { IH_TYPE_GPIMAGE, "gpimage", "TI Keystone SPL Image",},
148 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
149 { IH_TYPE_KERNEL_NOLOAD, "kernel_noload", "Kernel Image (no loading done)", },
150 { IH_TYPE_KWBIMAGE, "kwbimage", "Kirkwood Boot Image",},
151 { IH_TYPE_IMXIMAGE, "imximage", "Freescale i.MX Boot Image",},
152 { IH_TYPE_IMX8IMAGE, "imx8image", "NXP i.MX8 Boot Image",},
153 { IH_TYPE_IMX8MIMAGE, "imx8mimage", "NXP i.MX8M Boot Image",},
154 { IH_TYPE_INVALID, "invalid", "Invalid Image", },
155 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
156 { IH_TYPE_OMAPIMAGE, "omapimage", "TI OMAP SPL With GP CH",},
157 { IH_TYPE_PBLIMAGE, "pblimage", "Freescale PBL Boot Image",},
158 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
159 { IH_TYPE_SCRIPT, "script", "Script", },
160 { IH_TYPE_SOCFPGAIMAGE, "socfpgaimage", "Altera SoCFPGA CV/AV preloader",},
161 { IH_TYPE_SOCFPGAIMAGE_V1, "socfpgaimage_v1", "Altera SoCFPGA A10 preloader",},
162 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
163 { IH_TYPE_UBLIMAGE, "ublimage", "Davinci UBL image",},
164 { IH_TYPE_MXSIMAGE, "mxsimage", "Freescale MXS Boot Image",},
165 { IH_TYPE_ATMELIMAGE, "atmelimage", "ATMEL ROM-Boot Image",},
166 { IH_TYPE_X86_SETUP, "x86_setup", "x86 setup.bin", },
167 { IH_TYPE_LPC32XXIMAGE, "lpc32xximage", "LPC32XX Boot Image", },
168 { IH_TYPE_RKIMAGE, "rkimage", "Rockchip Boot Image" },
169 { IH_TYPE_RKSD, "rksd", "Rockchip SD Boot Image" },
170 { IH_TYPE_RKSPI, "rkspi", "Rockchip SPI Boot Image" },
171 { IH_TYPE_VYBRIDIMAGE, "vybridimage", "Vybrid Boot Image", },
172 { IH_TYPE_ZYNQIMAGE, "zynqimage", "Xilinx Zynq Boot Image" },
173 { IH_TYPE_ZYNQMPIMAGE, "zynqmpimage", "Xilinx ZynqMP Boot Image" },
174 { IH_TYPE_ZYNQMPBIF, "zynqmpbif", "Xilinx ZynqMP Boot Image (bif)" },
175 { IH_TYPE_FPGA, "fpga", "FPGA Image" },
176 { IH_TYPE_TEE, "tee", "Trusted Execution Environment Image",},
177 { IH_TYPE_FIRMWARE_IVT, "firmware_ivt", "Firmware with HABv4 IVT" },
178 { IH_TYPE_PMMC, "pmmc", "TI Power Management Micro-Controller Firmware",},
179 { IH_TYPE_STM32IMAGE, "stm32image", "STMicroelectronics STM32 Image" },
180 { IH_TYPE_MTKIMAGE, "mtk_image", "MediaTek BootROM loadable Image" },
181 { IH_TYPE_COPRO, "copro", "Coprocessor Image"},
185 static const table_entry_t uimage_comp[] = {
186 { IH_COMP_NONE, "none", "uncompressed", },
187 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
188 { IH_COMP_GZIP, "gzip", "gzip compressed", },
189 { IH_COMP_LZMA, "lzma", "lzma compressed", },
190 { IH_COMP_LZO, "lzo", "lzo compressed", },
191 { IH_COMP_LZ4, "lz4", "lz4 compressed", },
198 const table_entry_t *table;
201 static const struct table_info table_info[IH_COUNT] = {
202 { "architecture", IH_ARCH_COUNT, uimage_arch },
203 { "compression", IH_COMP_COUNT, uimage_comp },
204 { "operating system", IH_OS_COUNT, uimage_os },
205 { "image type", IH_TYPE_COUNT, uimage_type },
208 /*****************************************************************************/
209 /* Legacy format routines */
210 /*****************************************************************************/
211 int image_check_hcrc(const image_header_t *hdr)
214 ulong len = image_get_header_size();
215 image_header_t header;
217 /* Copy header so we can blank CRC field for re-calculation */
218 memmove(&header, (char *)hdr, image_get_header_size());
219 image_set_hcrc(&header, 0);
221 hcrc = crc32(0, (unsigned char *)&header, len);
223 return (hcrc == image_get_hcrc(hdr));
226 int image_check_dcrc(const image_header_t *hdr)
228 ulong data = image_get_data(hdr);
229 ulong len = image_get_data_size(hdr);
230 ulong dcrc = crc32_wd(0, (unsigned char *)data, len, CHUNKSZ_CRC32);
232 return (dcrc == image_get_dcrc(hdr));
236 * image_multi_count - get component (sub-image) count
237 * @hdr: pointer to the header of the multi component image
239 * image_multi_count() returns number of components in a multi
242 * Note: no checking of the image type is done, caller must pass
243 * a valid multi component image.
246 * number of components
248 ulong image_multi_count(const image_header_t *hdr)
253 /* get start of the image payload, which in case of multi
254 * component images that points to a table of component sizes */
255 size = (uint32_t *)image_get_data(hdr);
257 /* count non empty slots */
258 for (i = 0; size[i]; ++i)
265 * image_multi_getimg - get component data address and size
266 * @hdr: pointer to the header of the multi component image
267 * @idx: index of the requested component
268 * @data: pointer to a ulong variable, will hold component data address
269 * @len: pointer to a ulong variable, will hold component size
271 * image_multi_getimg() returns size and data address for the requested
272 * component in a multi component image.
274 * Note: no checking of the image type is done, caller must pass
275 * a valid multi component image.
278 * data address and size of the component, if idx is valid
279 * 0 in data and len, if idx is out of range
281 void image_multi_getimg(const image_header_t *hdr, ulong idx,
282 ulong *data, ulong *len)
286 ulong offset, count, img_data;
288 /* get number of component */
289 count = image_multi_count(hdr);
291 /* get start of the image payload, which in case of multi
292 * component images that points to a table of component sizes */
293 size = (uint32_t *)image_get_data(hdr);
295 /* get address of the proper component data start, which means
296 * skipping sizes table (add 1 for last, null entry) */
297 img_data = image_get_data(hdr) + (count + 1) * sizeof(uint32_t);
300 *len = uimage_to_cpu(size[idx]);
303 /* go over all indices preceding requested component idx */
304 for (i = 0; i < idx; i++) {
305 /* add up i-th component size, rounding up to 4 bytes */
306 offset += (uimage_to_cpu(size[i]) + 3) & ~3 ;
309 /* calculate idx-th component data address */
310 *data = img_data + offset;
317 static void image_print_type(const image_header_t *hdr)
319 const char __maybe_unused *os, *arch, *type, *comp;
321 os = genimg_get_os_name(image_get_os(hdr));
322 arch = genimg_get_arch_name(image_get_arch(hdr));
323 type = genimg_get_type_name(image_get_type(hdr));
324 comp = genimg_get_comp_name(image_get_comp(hdr));
326 printf("%s %s %s (%s)\n", arch, os, type, comp);
330 * image_print_contents - prints out the contents of the legacy format image
331 * @ptr: pointer to the legacy format image header
332 * @p: pointer to prefix string
334 * image_print_contents() formats a multi line legacy image contents description.
335 * The routine prints out all header fields followed by the size/offset data
336 * for MULTI/SCRIPT images.
339 * no returned results
341 void image_print_contents(const void *ptr)
343 const image_header_t *hdr = (const image_header_t *)ptr;
344 const char __maybe_unused *p;
346 p = IMAGE_INDENT_STRING;
347 printf("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name(hdr));
348 if (IMAGE_ENABLE_TIMESTAMP) {
349 printf("%sCreated: ", p);
350 genimg_print_time((time_t)image_get_time(hdr));
352 printf("%sImage Type: ", p);
353 image_print_type(hdr);
354 printf("%sData Size: ", p);
355 genimg_print_size(image_get_data_size(hdr));
356 printf("%sLoad Address: %08x\n", p, image_get_load(hdr));
357 printf("%sEntry Point: %08x\n", p, image_get_ep(hdr));
359 if (image_check_type(hdr, IH_TYPE_MULTI) ||
360 image_check_type(hdr, IH_TYPE_SCRIPT)) {
363 ulong count = image_multi_count(hdr);
365 printf("%sContents:\n", p);
366 for (i = 0; i < count; i++) {
367 image_multi_getimg(hdr, i, &data, &len);
369 printf("%s Image %d: ", p, i);
370 genimg_print_size(len);
372 if (image_check_type(hdr, IH_TYPE_SCRIPT) && i > 0) {
374 * the user may need to know offsets
375 * if planning to do something with
378 printf("%s Offset = 0x%08lx\n", p, data);
381 } else if (image_check_type(hdr, IH_TYPE_FIRMWARE_IVT)) {
382 printf("HAB Blocks: 0x%08x 0x0000 0x%08x\n",
383 image_get_load(hdr) - image_get_header_size(),
384 (int)(image_get_size(hdr) + image_get_header_size()
385 + sizeof(flash_header_v2_t) - 0x2060));
390 * print_decomp_msg() - Print a suitable decompression/loading message
392 * @type: OS type (IH_OS_...)
393 * @comp_type: Compression type being used (IH_COMP_...)
394 * @is_xip: true if the load address matches the image start
396 static void print_decomp_msg(int comp_type, int type, bool is_xip)
398 const char *name = genimg_get_type_name(type);
400 if (comp_type == IH_COMP_NONE)
401 printf(" %s %s\n", is_xip ? "XIP" : "Loading", name);
403 printf(" Uncompressing %s\n", name);
406 int image_decomp(int comp, ulong load, ulong image_start, int type,
407 void *load_buf, void *image_buf, ulong image_len,
408 uint unc_len, ulong *load_end)
413 print_decomp_msg(comp, type, load == image_start);
416 * Load the image to the right place, decompressing if needed. After
417 * this, image_len will be set to the number of uncompressed bytes
418 * loaded, ret will be non-zero on error.
422 if (load == image_start)
424 if (image_len <= unc_len)
425 memmove_wd(load_buf, image_buf, image_len, CHUNKSZ);
431 ret = gunzip(load_buf, unc_len, image_buf, &image_len);
434 #endif /* CONFIG_GZIP */
436 case IH_COMP_BZIP2: {
440 * If we've got less than 4 MB of malloc() space,
441 * use slower decompression algorithm which requires
442 * at most 2300 KB of memory.
444 ret = BZ2_bzBuffToBuffDecompress(load_buf, &size,
445 image_buf, image_len,
446 CONFIG_SYS_MALLOC_LEN < (4096 * 1024), 0);
450 #endif /* CONFIG_BZIP2 */
453 SizeT lzma_len = unc_len;
455 ret = lzmaBuffToBuffDecompress(load_buf, &lzma_len,
456 image_buf, image_len);
457 image_len = lzma_len;
460 #endif /* CONFIG_LZMA */
463 size_t size = unc_len;
465 ret = lzop_decompress(image_buf, image_len, load_buf, &size);
469 #endif /* CONFIG_LZO */
472 size_t size = unc_len;
474 ret = ulz4fn(image_buf, image_len, load_buf, &size);
478 #endif /* CONFIG_LZ4 */
480 printf("Unimplemented compression type %d\n", comp);
484 *load_end = load + image_len;
491 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
493 * image_get_ramdisk - get and verify ramdisk image
494 * @rd_addr: ramdisk image start address
495 * @arch: expected ramdisk architecture
496 * @verify: checksum verification flag
498 * image_get_ramdisk() returns a pointer to the verified ramdisk image
499 * header. Routine receives image start address and expected architecture
500 * flag. Verification done covers data and header integrity and os/type/arch
504 * pointer to a ramdisk image header, if image was found and valid
505 * otherwise, return NULL
507 static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
510 const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
512 if (!image_check_magic(rd_hdr)) {
513 puts("Bad Magic Number\n");
514 bootstage_error(BOOTSTAGE_ID_RD_MAGIC);
518 if (!image_check_hcrc(rd_hdr)) {
519 puts("Bad Header Checksum\n");
520 bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
524 bootstage_mark(BOOTSTAGE_ID_RD_MAGIC);
525 image_print_contents(rd_hdr);
528 puts(" Verifying Checksum ... ");
529 if (!image_check_dcrc(rd_hdr)) {
530 puts("Bad Data CRC\n");
531 bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM);
537 bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
539 if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
540 !image_check_arch(rd_hdr, arch) ||
541 !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
542 printf("No Linux %s Ramdisk Image\n",
543 genimg_get_arch_name(arch));
544 bootstage_error(BOOTSTAGE_ID_RAMDISK);
551 #endif /* !USE_HOSTCC */
553 /*****************************************************************************/
554 /* Shared dual-format routines */
555 /*****************************************************************************/
557 ulong load_addr = CONFIG_SYS_LOAD_ADDR; /* Default Load Address */
558 ulong save_addr; /* Default Save Address */
559 ulong save_size; /* Default Save Size (in bytes) */
561 static int on_loadaddr(const char *name, const char *value, enum env_op op,
566 case env_op_overwrite:
567 load_addr = simple_strtoul(value, NULL, 16);
575 U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
577 ulong env_get_bootm_low(void)
579 char *s = env_get("bootm_low");
581 ulong tmp = simple_strtoul(s, NULL, 16);
585 #if defined(CONFIG_SYS_SDRAM_BASE)
586 return CONFIG_SYS_SDRAM_BASE;
587 #elif defined(CONFIG_ARM) || defined(CONFIG_MICROBLAZE)
588 return gd->bd->bi_dram[0].start;
594 phys_size_t env_get_bootm_size(void)
596 phys_size_t tmp, size;
598 char *s = env_get("bootm_size");
600 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
604 #if (defined(CONFIG_ARM) || defined(CONFIG_MICROBLAZE)) && \
605 defined(CONFIG_NR_DRAM_BANKS)
606 start = gd->bd->bi_dram[0].start;
607 size = gd->bd->bi_dram[0].size;
609 start = gd->bd->bi_memstart;
610 size = gd->bd->bi_memsize;
613 s = env_get("bootm_low");
615 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
619 return size - (tmp - start);
622 phys_size_t env_get_bootm_mapsize(void)
625 char *s = env_get("bootm_mapsize");
627 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
631 #if defined(CONFIG_SYS_BOOTMAPSZ)
632 return CONFIG_SYS_BOOTMAPSZ;
634 return env_get_bootm_size();
638 void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
643 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
649 size_t tail = (len > chunksz) ? chunksz : len;
655 memmove(to, from, tail);
662 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
663 memmove(to, from, len);
664 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
666 #else /* USE_HOSTCC */
667 void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
669 memmove(to, from, len);
671 #endif /* !USE_HOSTCC */
673 void genimg_print_size(uint32_t size)
676 printf("%d Bytes = ", size);
677 print_size(size, "\n");
679 printf("%d Bytes = %.2f KiB = %.2f MiB\n",
680 size, (double)size / 1.024e3,
681 (double)size / 1.048576e6);
685 #if IMAGE_ENABLE_TIMESTAMP
686 void genimg_print_time(time_t timestamp)
691 rtc_to_tm(timestamp, &tm);
692 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
693 tm.tm_year, tm.tm_mon, tm.tm_mday,
694 tm.tm_hour, tm.tm_min, tm.tm_sec);
696 printf("%s", ctime(×tamp));
701 const table_entry_t *get_table_entry(const table_entry_t *table, int id)
703 for (; table->id >= 0; ++table) {
710 static const char *unknown_msg(enum ih_category category)
712 static const char unknown_str[] = "Unknown ";
715 strcpy(msg, unknown_str);
716 strncat(msg, table_info[category].desc,
717 sizeof(msg) - sizeof(unknown_str));
723 * get_cat_table_entry_name - translate entry id to long name
724 * @category: category to look up (enum ih_category)
725 * @id: entry id to be translated
727 * This will scan the translation table trying to find the entry that matches
730 * @retur long entry name if translation succeeds; error string on failure
732 const char *genimg_get_cat_name(enum ih_category category, uint id)
734 const table_entry_t *entry;
736 entry = get_table_entry(table_info[category].table, id);
738 return unknown_msg(category);
739 #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
742 return entry->lname + gd->reloc_off;
747 * get_cat_table_entry_short_name - translate entry id to short name
748 * @category: category to look up (enum ih_category)
749 * @id: entry id to be translated
751 * This will scan the translation table trying to find the entry that matches
754 * @retur short entry name if translation succeeds; error string on failure
756 const char *genimg_get_cat_short_name(enum ih_category category, uint id)
758 const table_entry_t *entry;
760 entry = get_table_entry(table_info[category].table, id);
762 return unknown_msg(category);
763 #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
766 return entry->sname + gd->reloc_off;
770 int genimg_get_cat_count(enum ih_category category)
772 return table_info[category].count;
775 const char *genimg_get_cat_desc(enum ih_category category)
777 return table_info[category].desc;
781 * get_table_entry_name - translate entry id to long name
782 * @table: pointer to a translation table for entries of a specific type
783 * @msg: message to be returned when translation fails
784 * @id: entry id to be translated
786 * get_table_entry_name() will go over translation table trying to find
787 * entry that matches given id. If matching entry is found, its long
788 * name is returned to the caller.
791 * long entry name if translation succeeds
794 char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
796 table = get_table_entry(table, id);
799 #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
802 return table->lname + gd->reloc_off;
806 const char *genimg_get_os_name(uint8_t os)
808 return (get_table_entry_name(uimage_os, "Unknown OS", os));
811 const char *genimg_get_arch_name(uint8_t arch)
813 return (get_table_entry_name(uimage_arch, "Unknown Architecture",
817 const char *genimg_get_type_name(uint8_t type)
819 return (get_table_entry_name(uimage_type, "Unknown Image", type));
822 static const char *genimg_get_short_name(const table_entry_t *table, int val)
824 table = get_table_entry(table, val);
827 #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
830 return table->sname + gd->reloc_off;
834 const char *genimg_get_type_short_name(uint8_t type)
836 return genimg_get_short_name(uimage_type, type);
839 const char *genimg_get_comp_name(uint8_t comp)
841 return (get_table_entry_name(uimage_comp, "Unknown Compression",
845 const char *genimg_get_comp_short_name(uint8_t comp)
847 return genimg_get_short_name(uimage_comp, comp);
850 const char *genimg_get_os_short_name(uint8_t os)
852 return genimg_get_short_name(uimage_os, os);
855 const char *genimg_get_arch_short_name(uint8_t arch)
857 return genimg_get_short_name(uimage_arch, arch);
861 * get_table_entry_id - translate short entry name to id
862 * @table: pointer to a translation table for entries of a specific type
863 * @table_name: to be used in case of error
864 * @name: entry short name to be translated
866 * get_table_entry_id() will go over translation table trying to find
867 * entry that matches given short name. If matching entry is found,
868 * its id returned to the caller.
871 * entry id if translation succeeds
874 int get_table_entry_id(const table_entry_t *table,
875 const char *table_name, const char *name)
877 const table_entry_t *t;
879 for (t = table; t->id >= 0; ++t) {
880 #ifdef CONFIG_NEEDS_MANUAL_RELOC
881 if (t->sname && strcasecmp(t->sname + gd->reloc_off, name) == 0)
883 if (t->sname && strcasecmp(t->sname, name) == 0)
887 debug("Invalid %s Type: %s\n", table_name, name);
892 int genimg_get_os_id(const char *name)
894 return (get_table_entry_id(uimage_os, "OS", name));
897 int genimg_get_arch_id(const char *name)
899 return (get_table_entry_id(uimage_arch, "CPU", name));
902 int genimg_get_type_id(const char *name)
904 return (get_table_entry_id(uimage_type, "Image", name));
907 int genimg_get_comp_id(const char *name)
909 return (get_table_entry_id(uimage_comp, "Compression", name));
914 * genimg_get_kernel_addr_fit - get the real kernel address and return 2
916 * @img_addr: a string might contain real image address
917 * @fit_uname_config: double pointer to a char, will hold pointer to a
918 * configuration unit name
919 * @fit_uname_kernel: double pointer to a char, will hold pointer to a subimage
922 * genimg_get_kernel_addr_fit get the real kernel start address from a string
923 * which is normally the first argv of bootm/bootz
926 * kernel start address
928 ulong genimg_get_kernel_addr_fit(char * const img_addr,
929 const char **fit_uname_config,
930 const char **fit_uname_kernel)
934 /* find out kernel image address */
936 kernel_addr = load_addr;
937 debug("* kernel: default image load address = 0x%08lx\n",
939 #if CONFIG_IS_ENABLED(FIT)
940 } else if (fit_parse_conf(img_addr, load_addr, &kernel_addr,
942 debug("* kernel: config '%s' from image at 0x%08lx\n",
943 *fit_uname_config, kernel_addr);
944 } else if (fit_parse_subimage(img_addr, load_addr, &kernel_addr,
946 debug("* kernel: subimage '%s' from image at 0x%08lx\n",
947 *fit_uname_kernel, kernel_addr);
950 kernel_addr = simple_strtoul(img_addr, NULL, 16);
951 debug("* kernel: cmdline image address = 0x%08lx\n",
959 * genimg_get_kernel_addr() is the simple version of
960 * genimg_get_kernel_addr_fit(). It ignores those return FIT strings
962 ulong genimg_get_kernel_addr(char * const img_addr)
964 const char *fit_uname_config = NULL;
965 const char *fit_uname_kernel = NULL;
967 return genimg_get_kernel_addr_fit(img_addr, &fit_uname_config,
972 * genimg_get_format - get image format type
973 * @img_addr: image start address
975 * genimg_get_format() checks whether provided address points to a valid
976 * legacy or FIT image.
978 * New uImage format and FDT blob are based on a libfdt. FDT blob
979 * may be passed directly or embedded in a FIT image. In both situations
980 * genimg_get_format() must be able to dectect libfdt header.
983 * image format type or IMAGE_FORMAT_INVALID if no image is present
985 int genimg_get_format(const void *img_addr)
987 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
988 const image_header_t *hdr;
990 hdr = (const image_header_t *)img_addr;
991 if (image_check_magic(hdr))
992 return IMAGE_FORMAT_LEGACY;
994 #if IMAGE_ENABLE_FIT || IMAGE_ENABLE_OF_LIBFDT
995 if (fdt_check_header(img_addr) == 0)
996 return IMAGE_FORMAT_FIT;
998 #ifdef CONFIG_ANDROID_BOOT_IMAGE
999 if (android_image_check_header(img_addr) == 0)
1000 return IMAGE_FORMAT_ANDROID;
1003 return IMAGE_FORMAT_INVALID;
1007 * fit_has_config - check if there is a valid FIT configuration
1008 * @images: pointer to the bootm command headers structure
1010 * fit_has_config() checks if there is a FIT configuration in use
1011 * (if FTI support is present).
1014 * 0, no FIT support or no configuration found
1015 * 1, configuration found
1017 int genimg_has_config(bootm_headers_t *images)
1019 #if IMAGE_ENABLE_FIT
1020 if (images->fit_uname_cfg)
1027 * boot_get_ramdisk - main ramdisk handling routine
1028 * @argc: command argument count
1029 * @argv: command argument list
1030 * @images: pointer to the bootm images structure
1031 * @arch: expected ramdisk architecture
1032 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
1033 * @rd_end: pointer to a ulong variable, will hold ramdisk end
1035 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
1036 * Curently supported are the following ramdisk sources:
1037 * - multicomponent kernel/ramdisk image,
1038 * - commandline provided address of decicated ramdisk image.
1041 * 0, if ramdisk image was found and valid, or skiped
1042 * rd_start and rd_end are set to ramdisk start/end addresses if
1043 * ramdisk image is found and valid
1045 * 1, if ramdisk image is found but corrupted, or invalid
1046 * rd_start and rd_end are set to 0 if no ramdisk exists
1048 int boot_get_ramdisk(int argc, char * const argv[], bootm_headers_t *images,
1049 uint8_t arch, ulong *rd_start, ulong *rd_end)
1051 ulong rd_addr, rd_load;
1052 ulong rd_data, rd_len;
1053 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
1054 const image_header_t *rd_hdr;
1057 #ifdef CONFIG_SUPPORT_RAW_INITRD
1060 #if IMAGE_ENABLE_FIT
1061 const char *fit_uname_config = images->fit_uname_cfg;
1062 const char *fit_uname_ramdisk = NULL;
1066 const char *select = NULL;
1071 #ifdef CONFIG_ANDROID_BOOT_IMAGE
1073 * Look for an Android boot image.
1075 buf = map_sysmem(images->os.start, 0);
1076 if (buf && genimg_get_format(buf) == IMAGE_FORMAT_ANDROID)
1077 select = (argc == 0) ? env_get("loadaddr") : argv[0];
1084 * Look for a '-' which indicates to ignore the
1087 if (select && strcmp(select, "-") == 0) {
1088 debug("## Skipping init Ramdisk\n");
1089 rd_len = rd_data = 0;
1090 } else if (select || genimg_has_config(images)) {
1091 #if IMAGE_ENABLE_FIT
1094 * If the init ramdisk comes from the FIT image and
1095 * the FIT image address is omitted in the command
1096 * line argument, try to use os FIT image address or
1097 * default load address.
1099 if (images->fit_uname_os)
1100 default_addr = (ulong)images->fit_hdr_os;
1102 default_addr = load_addr;
1104 if (fit_parse_conf(select, default_addr,
1105 &rd_addr, &fit_uname_config)) {
1106 debug("* ramdisk: config '%s' from image at "
1108 fit_uname_config, rd_addr);
1109 } else if (fit_parse_subimage(select, default_addr,
1110 &rd_addr, &fit_uname_ramdisk)) {
1111 debug("* ramdisk: subimage '%s' from image at "
1113 fit_uname_ramdisk, rd_addr);
1117 rd_addr = simple_strtoul(select, NULL, 16);
1118 debug("* ramdisk: cmdline image address = "
1122 #if IMAGE_ENABLE_FIT
1124 /* use FIT configuration provided in first bootm
1125 * command argument. If the property is not defined,
1128 rd_addr = map_to_sysmem(images->fit_hdr_os);
1129 rd_noffset = fit_get_node_from_config(images,
1130 FIT_RAMDISK_PROP, rd_addr);
1131 if (rd_noffset == -ENOENT)
1133 else if (rd_noffset < 0)
1139 * Check if there is an initrd image at the
1140 * address provided in the second bootm argument
1141 * check image type, for FIT images get FIT node.
1143 buf = map_sysmem(rd_addr, 0);
1144 switch (genimg_get_format(buf)) {
1145 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
1146 case IMAGE_FORMAT_LEGACY:
1147 printf("## Loading init Ramdisk from Legacy "
1148 "Image at %08lx ...\n", rd_addr);
1150 bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
1151 rd_hdr = image_get_ramdisk(rd_addr, arch,
1157 rd_data = image_get_data(rd_hdr);
1158 rd_len = image_get_data_size(rd_hdr);
1159 rd_load = image_get_load(rd_hdr);
1162 #if IMAGE_ENABLE_FIT
1163 case IMAGE_FORMAT_FIT:
1164 rd_noffset = fit_image_load(images,
1165 rd_addr, &fit_uname_ramdisk,
1166 &fit_uname_config, arch,
1168 BOOTSTAGE_ID_FIT_RD_START,
1169 FIT_LOAD_OPTIONAL_NON_ZERO,
1174 images->fit_hdr_rd = map_sysmem(rd_addr, 0);
1175 images->fit_uname_rd = fit_uname_ramdisk;
1176 images->fit_noffset_rd = rd_noffset;
1179 #ifdef CONFIG_ANDROID_BOOT_IMAGE
1180 case IMAGE_FORMAT_ANDROID:
1181 android_image_get_ramdisk((void *)images->os.start,
1186 #ifdef CONFIG_SUPPORT_RAW_INITRD
1189 end = strchr(select, ':');
1191 rd_len = simple_strtoul(++end, NULL, 16);
1196 puts("Wrong Ramdisk Image Format\n");
1197 rd_data = rd_len = rd_load = 0;
1201 } else if (images->legacy_hdr_valid &&
1202 image_check_type(&images->legacy_hdr_os_copy,
1206 * Now check if we have a legacy mult-component image,
1207 * get second entry data start address and len.
1209 bootstage_mark(BOOTSTAGE_ID_RAMDISK);
1210 printf("## Loading init Ramdisk from multi component "
1211 "Legacy Image at %08lx ...\n",
1212 (ulong)images->legacy_hdr_os);
1214 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
1219 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
1220 rd_len = rd_data = 0;
1224 debug("## No init Ramdisk\n");
1226 *rd_start = rd_data;
1227 *rd_end = rd_data + rd_len;
1229 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
1230 *rd_start, *rd_end);
1235 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
1237 * boot_ramdisk_high - relocate init ramdisk
1238 * @lmb: pointer to lmb handle, will be used for memory mgmt
1239 * @rd_data: ramdisk data start address
1240 * @rd_len: ramdisk data length
1241 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
1242 * start address (after possible relocation)
1243 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
1244 * end address (after possible relocation)
1246 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
1247 * variable and if requested ramdisk data is moved to a specified location.
1249 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1250 * start/end addresses if ramdisk image start and len were provided,
1251 * otherwise set initrd_start and initrd_end set to zeros.
1257 int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
1258 ulong *initrd_start, ulong *initrd_end)
1262 int initrd_copy_to_ram = 1;
1264 s = env_get("initrd_high");
1266 /* a value of "no" or a similar string will act like 0,
1267 * turning the "load high" feature off. This is intentional.
1269 initrd_high = simple_strtoul(s, NULL, 16);
1270 if (initrd_high == ~0)
1271 initrd_copy_to_ram = 0;
1273 initrd_high = env_get_bootm_mapsize() + env_get_bootm_low();
1277 debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1278 initrd_high, initrd_copy_to_ram);
1281 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
1282 debug(" in-place initrd\n");
1283 *initrd_start = rd_data;
1284 *initrd_end = rd_data + rd_len;
1285 lmb_reserve(lmb, rd_data, rd_len);
1288 *initrd_start = (ulong)lmb_alloc_base(lmb,
1289 rd_len, 0x1000, initrd_high);
1291 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
1294 if (*initrd_start == 0) {
1295 puts("ramdisk - allocation error\n");
1298 bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
1300 *initrd_end = *initrd_start + rd_len;
1301 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
1302 *initrd_start, *initrd_end);
1304 memmove_wd((void *)*initrd_start,
1305 (void *)rd_data, rd_len, CHUNKSZ);
1309 * Ensure the image is flushed to memory to handle
1310 * AMP boot scenarios in which we might not be
1313 flush_cache((unsigned long)*initrd_start,
1314 ALIGN(rd_len, ARCH_DMA_MINALIGN));
1322 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1323 *initrd_start, *initrd_end);
1330 #endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
1332 int boot_get_setup(bootm_headers_t *images, uint8_t arch,
1333 ulong *setup_start, ulong *setup_len)
1335 #if IMAGE_ENABLE_FIT
1336 return boot_get_setup_fit(images, arch, setup_start, setup_len);
1342 #if IMAGE_ENABLE_FIT
1343 #if defined(CONFIG_FPGA)
1344 int boot_get_fpga(int argc, char * const argv[], bootm_headers_t *images,
1345 uint8_t arch, const ulong *ld_start, ulong * const ld_len)
1347 ulong tmp_img_addr, img_data, img_len;
1351 const char *uname, *name;
1353 int devnum = 0; /* TODO support multi fpga platforms */
1355 /* Check to see if the images struct has a FIT configuration */
1356 if (!genimg_has_config(images)) {
1357 debug("## FIT configuration was not specified\n");
1362 * Obtain the os FIT header from the images struct
1364 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
1365 buf = map_sysmem(tmp_img_addr, 0);
1367 * Check image type. For FIT images get FIT node
1368 * and attempt to locate a generic binary.
1370 switch (genimg_get_format(buf)) {
1371 case IMAGE_FORMAT_FIT:
1372 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
1374 uname = fdt_stringlist_get(buf, conf_noffset, FIT_FPGA_PROP, 0,
1377 debug("## FPGA image is not specified\n");
1380 fit_img_result = fit_image_load(images,
1382 (const char **)&uname,
1383 &(images->fit_uname_cfg),
1386 BOOTSTAGE_ID_FPGA_INIT,
1387 FIT_LOAD_OPTIONAL_NON_ZERO,
1388 &img_data, &img_len);
1390 debug("FPGA image (%s) loaded to 0x%lx/size 0x%lx\n",
1391 uname, img_data, img_len);
1393 if (fit_img_result < 0) {
1394 /* Something went wrong! */
1395 return fit_img_result;
1398 if (!fpga_is_partial_data(devnum, img_len)) {
1400 err = fpga_loadbitstream(devnum, (char *)img_data,
1403 err = fpga_load(devnum, (const void *)img_data,
1407 err = fpga_loadbitstream(devnum, (char *)img_data,
1408 img_len, BIT_PARTIAL);
1410 err = fpga_load(devnum, (const void *)img_data,
1411 img_len, BIT_PARTIAL);
1417 printf(" Programming %s bitstream... OK\n", name);
1420 printf("The given image format is not supported (corrupt?)\n");
1428 static void fit_loadable_process(uint8_t img_type,
1433 const unsigned int count =
1434 ll_entry_count(struct fit_loadable_tbl, fit_loadable);
1435 struct fit_loadable_tbl *fit_loadable_handler =
1436 ll_entry_start(struct fit_loadable_tbl, fit_loadable);
1437 /* For each loadable handler */
1438 for (i = 0; i < count; i++, fit_loadable_handler++)
1439 /* matching this type */
1440 if (fit_loadable_handler->type == img_type)
1441 /* call that handler with this image data */
1442 fit_loadable_handler->handler(img_data, img_len);
1445 int boot_get_loadable(int argc, char * const argv[], bootm_headers_t *images,
1446 uint8_t arch, const ulong *ld_start, ulong * const ld_len)
1449 * These variables are used to hold the current image location
1454 * These two variables are requirements for fit_image_load, but
1455 * their values are not used
1457 ulong img_data, img_len;
1459 int loadables_index;
1465 /* Check to see if the images struct has a FIT configuration */
1466 if (!genimg_has_config(images)) {
1467 debug("## FIT configuration was not specified\n");
1472 * Obtain the os FIT header from the images struct
1474 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
1475 buf = map_sysmem(tmp_img_addr, 0);
1477 * Check image type. For FIT images get FIT node
1478 * and attempt to locate a generic binary.
1480 switch (genimg_get_format(buf)) {
1481 case IMAGE_FORMAT_FIT:
1482 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
1484 for (loadables_index = 0;
1485 uname = fdt_stringlist_get(buf, conf_noffset,
1486 FIT_LOADABLE_PROP, loadables_index,
1490 fit_img_result = fit_image_load(images,
1493 &(images->fit_uname_cfg), arch,
1495 BOOTSTAGE_ID_FIT_LOADABLE_START,
1496 FIT_LOAD_OPTIONAL_NON_ZERO,
1497 &img_data, &img_len);
1498 if (fit_img_result < 0) {
1499 /* Something went wrong! */
1500 return fit_img_result;
1503 fit_img_result = fit_image_get_node(buf, uname);
1504 if (fit_img_result < 0) {
1505 /* Something went wrong! */
1506 return fit_img_result;
1508 fit_img_result = fit_image_get_type(buf,
1511 if (fit_img_result < 0) {
1512 /* Something went wrong! */
1513 return fit_img_result;
1516 fit_loadable_process(img_type, img_data, img_len);
1520 printf("The given image format is not supported (corrupt?)\n");
1528 #ifdef CONFIG_SYS_BOOT_GET_CMDLINE
1530 * boot_get_cmdline - allocate and initialize kernel cmdline
1531 * @lmb: pointer to lmb handle, will be used for memory mgmt
1532 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1533 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1535 * boot_get_cmdline() allocates space for kernel command line below
1536 * BOOTMAPSZ + env_get_bootm_low() address. If "bootargs" U-Boot environment
1537 * variable is present its contents is copied to allocated kernel
1544 int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
1549 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
1550 env_get_bootm_mapsize() + env_get_bootm_low());
1552 if (cmdline == NULL)
1555 s = env_get("bootargs");
1561 *cmd_start = (ulong) & cmdline[0];
1562 *cmd_end = *cmd_start + strlen(cmdline);
1564 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1568 #endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
1570 #ifdef CONFIG_SYS_BOOT_GET_KBD
1572 * boot_get_kbd - allocate and initialize kernel copy of board info
1573 * @lmb: pointer to lmb handle, will be used for memory mgmt
1574 * @kbd: double pointer to board info data
1576 * boot_get_kbd() allocates space for kernel copy of board info data below
1577 * BOOTMAPSZ + env_get_bootm_low() address and kernel board info is initialized
1578 * with the current u-boot board info data.
1584 int boot_get_kbd(struct lmb *lmb, bd_t **kbd)
1586 *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
1587 env_get_bootm_mapsize() + env_get_bootm_low());
1593 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1595 #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1596 do_bdinfo(NULL, 0, 0, NULL);
1601 #endif /* CONFIG_SYS_BOOT_GET_KBD */
1604 int image_setup_linux(bootm_headers_t *images)
1606 ulong of_size = images->ft_len;
1607 char **of_flat_tree = &images->ft_addr;
1608 struct lmb *lmb = &images->lmb;
1611 if (IMAGE_ENABLE_OF_LIBFDT)
1612 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
1614 if (IMAGE_BOOT_GET_CMDLINE) {
1615 ret = boot_get_cmdline(lmb, &images->cmdline_start,
1616 &images->cmdline_end);
1618 puts("ERROR with allocation of cmdline\n");
1623 if (IMAGE_ENABLE_OF_LIBFDT) {
1624 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
1629 if (IMAGE_ENABLE_OF_LIBFDT && of_size) {
1630 ret = image_setup_libfdt(images, *of_flat_tree, of_size, lmb);
1637 #endif /* CONFIG_LMB */
1638 #endif /* !USE_HOSTCC */