blob: 20fa4bd4974f5fdf98778c17088ea008b43bb09f [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marian Balakowiczb97a2a02008-01-08 18:14:09 +01002/*
3 * (C) Copyright 2008 Semihalf
4 *
5 * (C) Copyright 2000-2006
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Marian Balakowiczb97a2a02008-01-08 18:14:09 +01007 */
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01008
Marian Balakowiczb97a2a02008-01-08 18:14:09 +01009#ifndef USE_HOSTCC
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +010010#include <common.h>
Simon Glass52f24232020-05-10 11:40:00 -060011#include <bootstage.h>
Simon Glass1eb69ae2019-11-14 12:57:39 -070012#include <cpu_func.h>
Simon Glass7b51b572019-08-01 09:46:52 -060013#include <env.h>
Simon Glass336d4612020-02-03 07:36:16 -070014#include <malloc.h>
Simon Glass90526e92020-05-10 11:39:56 -060015#include <asm/cache.h>
Simon Glass3db71102019-11-14 12:57:16 -070016#include <u-boot/crc.h>
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +010017#include <watchdog.h>
18
19#ifdef CONFIG_SHOW_BOOT_PROGRESS
20#include <status_led.h>
21#endif
22
Marian Balakowicz2242f532008-02-21 17:27:41 +010023#include <rtc.h>
Marian Balakowicz2242f532008-02-21 17:27:41 +010024
Simon Glass0c670fc2019-08-01 09:46:36 -060025#include <gzip.h>
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010026#include <image.h>
Simon Glass6c03f9e2019-11-14 12:57:25 -070027#include <lz4.h>
Joe Hershberger0eb25b62015-03-22 17:08:59 -050028#include <mapmem.h>
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010029
Simon Glassaa34fbc2016-02-22 22:55:45 -070030#if IMAGE_ENABLE_FIT || IMAGE_ENABLE_OF_LIBFDT
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090031#include <linux/libfdt.h>
Marian Balakowiczfff888a12008-02-21 17:20:19 +010032#include <fdt_support.h>
Michal Simek62afc602016-05-17 14:03:50 +020033#include <fpga.h>
34#include <xilinx.h>
Marian Balakowiczc8779642008-03-12 10:12:37 +010035#endif
36
Andy Fleming20a14a42008-04-02 16:19:07 -050037#include <u-boot/md5.h>
Jeroen Hofstee2b9912e2014-06-12 22:27:12 +020038#include <u-boot/sha1.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090039#include <linux/errno.h>
Simon Glass35e7b0f2013-05-07 06:12:03 +000040#include <asm/io.h>
Marian Balakowiczc8779642008-03-12 10:12:37 +010041
Julius Werner20908542019-07-24 19:37:54 -070042#include <bzlib.h>
43#include <linux/lzo.h>
44#include <lzma/LzmaTypes.h>
45#include <lzma/LzmaDec.h>
46#include <lzma/LzmaTools.h>
47
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +010048#ifdef CONFIG_CMD_BDI
Wolfgang Denk54841ab2010-06-28 22:00:46 +020049extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +010050#endif
51
52DECLARE_GLOBAL_DATA_PTR;
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +010053
Tom Rinic76c93a2019-05-23 07:14:07 -040054#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Stephen Warren712fbcf2011-10-18 11:11:49 +000055static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
Marian Balakowiczd985c842008-03-12 10:14:38 +010056 int verify);
Heiko Schocher21d29f72014-05-28 11:33:33 +020057#endif
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010058#else
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +010059#include "mkimage.h"
Andy Fleming20a14a42008-04-02 16:19:07 -050060#include <u-boot/md5.h>
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010061#include <time.h>
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010062#include <image.h>
Heiko Schocher80402f32015-06-29 09:10:46 +020063
64#ifndef __maybe_unused
65# define __maybe_unused /* unimplemented */
66#endif
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010067#endif /* !USE_HOSTCC*/
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010068
Simon Glass0ccff502013-02-24 17:33:25 +000069#include <u-boot/crc.h>
Breno Matheus Lima5b20d142019-09-23 18:39:47 +000070#include <imximage.h>
Simon Glass0ccff502013-02-24 17:33:25 +000071
Simon Glass13d06982013-05-08 08:06:01 +000072#ifndef CONFIG_SYS_BARGSIZE
73#define CONFIG_SYS_BARGSIZE 512
74#endif
75
Mike Frysinger7edb1862010-10-20 07:17:39 -040076static const table_entry_t uimage_arch[] = {
Simon Glass30495bf2016-06-30 10:52:15 -060077 { IH_ARCH_INVALID, "invalid", "Invalid ARCH", },
Marian Balakowicz570abb02008-02-29 15:59:59 +010078 { IH_ARCH_ALPHA, "alpha", "Alpha", },
79 { IH_ARCH_ARM, "arm", "ARM", },
80 { IH_ARCH_I386, "x86", "Intel x86", },
81 { IH_ARCH_IA64, "ia64", "IA64", },
82 { IH_ARCH_M68K, "m68k", "M68K", },
83 { IH_ARCH_MICROBLAZE, "microblaze", "MicroBlaze", },
84 { IH_ARCH_MIPS, "mips", "MIPS", },
85 { IH_ARCH_MIPS64, "mips64", "MIPS 64 Bit", },
Marian Balakowicz570abb02008-02-29 15:59:59 +010086 { IH_ARCH_NIOS2, "nios2", "NIOS II", },
Grant Ericksone419e122008-05-04 16:45:01 -070087 { IH_ARCH_PPC, "powerpc", "PowerPC", },
Marian Balakowicz570abb02008-02-29 15:59:59 +010088 { IH_ARCH_PPC, "ppc", "PowerPC", },
89 { IH_ARCH_S390, "s390", "IBM S390", },
90 { IH_ARCH_SH, "sh", "SuperH", },
91 { IH_ARCH_SPARC, "sparc", "SPARC", },
92 { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", },
93 { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", },
94 { IH_ARCH_AVR32, "avr32", "AVR32", },
Macpaul Lin64d61462011-10-19 20:41:09 +000095 { IH_ARCH_NDS32, "nds32", "NDS32", },
Stefan Kristiansson3ddcacc2011-11-26 19:04:50 +000096 { IH_ARCH_OPENRISC, "or1k", "OpenRISC 1000",},
Simon Glass35e7b0f2013-05-07 06:12:03 +000097 { IH_ARCH_SANDBOX, "sandbox", "Sandbox", },
David Feng0ae76532013-12-14 11:47:35 +080098 { IH_ARCH_ARM64, "arm64", "AArch64", },
Alexey Brodkinbc5d5422014-02-04 12:56:16 +040099 { IH_ARCH_ARC, "arc", "ARC", },
Simon Glass5bda35c2014-10-10 08:21:57 -0600100 { IH_ARCH_X86_64, "x86_64", "AMD x86_64", },
Chris Zankelde5e5ce2016-08-10 18:36:43 +0300101 { IH_ARCH_XTENSA, "xtensa", "Xtensa", },
Rick Chen86aa65a2018-03-13 13:37:29 +0800102 { IH_ARCH_RISCV, "riscv", "RISC-V", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100103 { -1, "", "", },
104};
105
Mike Frysinger7edb1862010-10-20 07:17:39 -0400106static const table_entry_t uimage_os[] = {
Simon Glass30495bf2016-06-30 10:52:15 -0600107 { IH_OS_INVALID, "invalid", "Invalid OS", },
Philipp Tomsich4914af12017-09-13 21:29:29 +0200108 { IH_OS_ARM_TRUSTED_FIRMWARE, "arm-trusted-firmware", "ARM Trusted Firmware" },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100109 { IH_OS_LINUX, "linux", "Linux", },
110#if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
111 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
112#endif
113 { IH_OS_NETBSD, "netbsd", "NetBSD", },
Torkel Lundgren3df61952010-09-28 11:05:36 +0200114 { IH_OS_OSE, "ose", "Enea OSE", },
Steven Stallion04d41402013-03-20 06:31:35 +0000115 { IH_OS_PLAN9, "plan9", "Plan 9", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100116 { IH_OS_RTEMS, "rtems", "RTEMS", },
Bryan O'Donoghue45b55712018-03-13 16:50:35 +0000117 { IH_OS_TEE, "tee", "Trusted Execution Environment" },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100118 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
miao.yan@windriver.com68b15e82013-12-27 16:01:50 +0800119 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100120#if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
121 { IH_OS_QNX, "qnx", "QNX", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100122#endif
Peter Tyserf5ed9e32008-09-08 14:56:49 -0500123#if defined(CONFIG_INTEGRITY) || defined(USE_HOSTCC)
124 { IH_OS_INTEGRITY,"integrity", "INTEGRITY", },
125#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100126#ifdef USE_HOSTCC
127 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
128 { IH_OS_DELL, "dell", "Dell", },
129 { IH_OS_ESIX, "esix", "Esix", },
130 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
131 { IH_OS_IRIX, "irix", "Irix", },
132 { IH_OS_NCR, "ncr", "NCR", },
133 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
134 { IH_OS_PSOS, "psos", "pSOS", },
135 { IH_OS_SCO, "sco", "SCO", },
136 { IH_OS_SOLARIS, "solaris", "Solaris", },
137 { IH_OS_SVR4, "svr4", "SVR4", },
138#endif
Marek Vasut67ddd952014-12-16 14:07:21 +0100139#if defined(CONFIG_BOOTM_OPENRTOS) || defined(USE_HOSTCC)
140 { IH_OS_OPENRTOS, "openrtos", "OpenRTOS", },
141#endif
Lukas Auer5e30e452019-08-21 21:14:44 +0200142 { IH_OS_OPENSBI, "opensbi", "RISC-V OpenSBI", },
Cristian Ciocalteaa031b032019-12-24 18:05:38 +0200143 { IH_OS_EFI, "efi", "EFI Firmware" },
Marek Vasut67ddd952014-12-16 14:07:21 +0100144
Marian Balakowicz570abb02008-02-29 15:59:59 +0100145 { -1, "", "", },
146};
147
Mike Frysinger7edb1862010-10-20 07:17:39 -0400148static const table_entry_t uimage_type[] = {
Stefano Babic4962e382011-10-17 00:07:43 +0000149 { IH_TYPE_AISIMAGE, "aisimage", "Davinci AIS image",},
Marian Balakowicz570abb02008-02-29 15:59:59 +0100150 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
151 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
John Rigby3decb142011-07-21 09:10:30 -0400152 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
Karicheri, Muralidharanbf411ea2014-04-04 13:16:48 -0400153 { IH_TYPE_GPIMAGE, "gpimage", "TI Keystone SPL Image",},
Marian Balakowicz570abb02008-02-29 15:59:59 +0100154 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
Stephen Warrenb9b50e82011-11-10 13:17:53 -0700155 { IH_TYPE_KERNEL_NOLOAD, "kernel_noload", "Kernel Image (no loading done)", },
Stefano Babic4962e382011-10-17 00:07:43 +0000156 { IH_TYPE_KWBIMAGE, "kwbimage", "Kirkwood Boot Image",},
157 { IH_TYPE_IMXIMAGE, "imximage", "Freescale i.MX Boot Image",},
Peng Fana2b96ec2018-10-16 04:50:30 +0000158 { IH_TYPE_IMX8IMAGE, "imx8image", "NXP i.MX8 Boot Image",},
Peng Fan6609c262018-11-20 10:19:36 +0000159 { IH_TYPE_IMX8MIMAGE, "imx8mimage", "NXP i.MX8M Boot Image",},
Simon Glass30495bf2016-06-30 10:52:15 -0600160 { IH_TYPE_INVALID, "invalid", "Invalid Image", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100161 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
Stefano Babic4962e382011-10-17 00:07:43 +0000162 { IH_TYPE_OMAPIMAGE, "omapimage", "TI OMAP SPL With GP CH",},
Shaohui Xie5d898a02012-08-10 02:49:35 +0000163 { IH_TYPE_PBLIMAGE, "pblimage", "Freescale PBL Boot Image",},
Marian Balakowicz570abb02008-02-29 15:59:59 +0100164 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
165 { IH_TYPE_SCRIPT, "script", "Script", },
Marek Vasut662abc42018-04-15 13:15:33 +0200166 { IH_TYPE_SOCFPGAIMAGE, "socfpgaimage", "Altera SoCFPGA CV/AV preloader",},
167 { IH_TYPE_SOCFPGAIMAGE_V1, "socfpgaimage_v1", "Altera SoCFPGA A10 preloader",},
Marian Balakowicz570abb02008-02-29 15:59:59 +0100168 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
Heiko Schocher7816f2c2011-07-16 00:06:42 +0000169 { IH_TYPE_UBLIMAGE, "ublimage", "Davinci UBL image",},
Marek Vasutbce88372013-08-26 20:43:33 +0200170 { IH_TYPE_MXSIMAGE, "mxsimage", "Freescale MXS Boot Image",},
Andreas Bießmann7b1a4112014-05-19 14:23:39 +0200171 { IH_TYPE_ATMELIMAGE, "atmelimage", "ATMEL ROM-Boot Image",},
Simon Glass90268b82014-10-19 21:11:24 -0600172 { IH_TYPE_X86_SETUP, "x86_setup", "x86 setup.bin", },
Albert ARIBAUD \(3ADEV\)39f520b2015-03-31 11:40:49 +0200173 { IH_TYPE_LPC32XXIMAGE, "lpc32xximage", "LPC32XX Boot Image", },
Simon Glassa131c1f2015-08-30 16:55:24 -0600174 { IH_TYPE_RKIMAGE, "rkimage", "Rockchip Boot Image" },
Simon Glassf9a3c272015-08-30 16:55:25 -0600175 { IH_TYPE_RKSD, "rksd", "Rockchip SD Boot Image" },
Simon Glass10b84fe2015-08-30 16:55:26 -0600176 { IH_TYPE_RKSPI, "rkspi", "Rockchip SPI Boot Image" },
Albert ARIBAUD \(3ADEV\)ed0c2c02016-09-26 09:08:06 +0200177 { IH_TYPE_VYBRIDIMAGE, "vybridimage", "Vybrid Boot Image", },
Nathan Rossi66eef1e2015-11-17 22:56:56 +1000178 { IH_TYPE_ZYNQIMAGE, "zynqimage", "Xilinx Zynq Boot Image" },
Michal Simekd9b58b32016-04-27 14:03:29 +0200179 { IH_TYPE_ZYNQMPIMAGE, "zynqmpimage", "Xilinx ZynqMP Boot Image" },
Alexander Graf6915dcf2018-04-13 14:18:52 +0200180 { IH_TYPE_ZYNQMPBIF, "zynqmpbif", "Xilinx ZynqMP Boot Image (bif)" },
Michal Simeked0cea72016-05-17 13:58:44 +0200181 { IH_TYPE_FPGA, "fpga", "FPGA Image" },
Andrew F. Davis7e719ee2016-11-29 16:33:21 -0600182 { IH_TYPE_TEE, "tee", "Trusted Execution Environment Image",},
Sven Ebenfeldd21bd692016-11-06 16:37:56 +0100183 { IH_TYPE_FIRMWARE_IVT, "firmware_ivt", "Firmware with HABv4 IVT" },
Andrew F. Davis6442c962017-07-31 10:58:20 -0500184 { IH_TYPE_PMMC, "pmmc", "TI Power Management Micro-Controller Firmware",},
Patrick Delaunay81260e32018-03-12 10:46:04 +0100185 { IH_TYPE_STM32IMAGE, "stm32image", "STMicroelectronics STM32 Image" },
Ryder Lee3b975a12018-11-15 10:07:49 +0800186 { IH_TYPE_MTKIMAGE, "mtk_image", "MediaTek BootROM loadable Image" },
Patrick Delaunaye7fabe72019-08-02 15:07:19 +0200187 { IH_TYPE_COPRO, "copro", "Coprocessor Image"},
Marian Balakowicz570abb02008-02-29 15:59:59 +0100188 { -1, "", "", },
189};
190
Mike Frysinger7edb1862010-10-20 07:17:39 -0400191static const table_entry_t uimage_comp[] = {
Marian Balakowicz570abb02008-02-29 15:59:59 +0100192 { IH_COMP_NONE, "none", "uncompressed", },
193 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
194 { IH_COMP_GZIP, "gzip", "gzip compressed", },
Luigi 'Comio' Mantellinifc9c1722008-09-08 02:46:13 +0200195 { IH_COMP_LZMA, "lzma", "lzma compressed", },
Peter Korsgaard20dde482009-11-19 11:37:51 +0100196 { IH_COMP_LZO, "lzo", "lzo compressed", },
Julius Werner027b7282015-10-06 20:03:53 -0700197 { IH_COMP_LZ4, "lz4", "lz4 compressed", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100198 { -1, "", "", },
199};
200
Simon Glass56d7ab72016-06-30 10:52:14 -0600201struct table_info {
202 const char *desc;
203 int count;
204 const table_entry_t *table;
205};
206
Atish Patra155d6a32020-03-05 16:24:22 -0800207static const struct comp_magic_map image_comp[] = {
208 { IH_COMP_BZIP2, "bzip2", {0x42, 0x5a},},
209 { IH_COMP_GZIP, "gzip", {0x1f, 0x8b},},
210 { IH_COMP_LZMA, "lzma", {0x5d, 0x00},},
211 { IH_COMP_LZO, "lzo", {0x89, 0x4c},},
212 { IH_COMP_NONE, "none", {}, },
213};
214
Simon Glass56d7ab72016-06-30 10:52:14 -0600215static const struct table_info table_info[IH_COUNT] = {
216 { "architecture", IH_ARCH_COUNT, uimage_arch },
217 { "compression", IH_COMP_COUNT, uimage_comp },
218 { "operating system", IH_OS_COUNT, uimage_os },
219 { "image type", IH_TYPE_COUNT, uimage_type },
220};
221
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100222/*****************************************************************************/
223/* Legacy format routines */
224/*****************************************************************************/
Stephen Warren712fbcf2011-10-18 11:11:49 +0000225int image_check_hcrc(const image_header_t *hdr)
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100226{
227 ulong hcrc;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000228 ulong len = image_get_header_size();
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100229 image_header_t header;
230
231 /* Copy header so we can blank CRC field for re-calculation */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000232 memmove(&header, (char *)hdr, image_get_header_size());
233 image_set_hcrc(&header, 0);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100234
Stephen Warren712fbcf2011-10-18 11:11:49 +0000235 hcrc = crc32(0, (unsigned char *)&header, len);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100236
Stephen Warren712fbcf2011-10-18 11:11:49 +0000237 return (hcrc == image_get_hcrc(hdr));
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100238}
239
Stephen Warren712fbcf2011-10-18 11:11:49 +0000240int image_check_dcrc(const image_header_t *hdr)
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100241{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000242 ulong data = image_get_data(hdr);
243 ulong len = image_get_data_size(hdr);
244 ulong dcrc = crc32_wd(0, (unsigned char *)data, len, CHUNKSZ_CRC32);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100245
Stephen Warren712fbcf2011-10-18 11:11:49 +0000246 return (dcrc == image_get_dcrc(hdr));
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100247}
248
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100249/**
250 * image_multi_count - get component (sub-image) count
251 * @hdr: pointer to the header of the multi component image
252 *
253 * image_multi_count() returns number of components in a multi
254 * component image.
255 *
256 * Note: no checking of the image type is done, caller must pass
257 * a valid multi component image.
258 *
259 * returns:
260 * number of components
261 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000262ulong image_multi_count(const image_header_t *hdr)
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100263{
264 ulong i, count = 0;
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100265 uint32_t *size;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100266
267 /* get start of the image payload, which in case of multi
268 * component images that points to a table of component sizes */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000269 size = (uint32_t *)image_get_data(hdr);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100270
271 /* count non empty slots */
272 for (i = 0; size[i]; ++i)
273 count++;
274
275 return count;
276}
277
278/**
279 * image_multi_getimg - get component data address and size
280 * @hdr: pointer to the header of the multi component image
281 * @idx: index of the requested component
282 * @data: pointer to a ulong variable, will hold component data address
283 * @len: pointer to a ulong variable, will hold component size
284 *
285 * image_multi_getimg() returns size and data address for the requested
286 * component in a multi component image.
287 *
288 * Note: no checking of the image type is done, caller must pass
289 * a valid multi component image.
290 *
291 * returns:
292 * data address and size of the component, if idx is valid
293 * 0 in data and len, if idx is out of range
294 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000295void image_multi_getimg(const image_header_t *hdr, ulong idx,
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100296 ulong *data, ulong *len)
297{
298 int i;
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100299 uint32_t *size;
Nick Spence02b9b222008-05-10 14:02:04 -0700300 ulong offset, count, img_data;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100301
302 /* get number of component */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000303 count = image_multi_count(hdr);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100304
305 /* get start of the image payload, which in case of multi
306 * component images that points to a table of component sizes */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000307 size = (uint32_t *)image_get_data(hdr);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100308
309 /* get address of the proper component data start, which means
310 * skipping sizes table (add 1 for last, null entry) */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000311 img_data = image_get_data(hdr) + (count + 1) * sizeof(uint32_t);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100312
313 if (idx < count) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000314 *len = uimage_to_cpu(size[idx]);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100315 offset = 0;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100316
317 /* go over all indices preceding requested component idx */
318 for (i = 0; i < idx; i++) {
Nick Spence02b9b222008-05-10 14:02:04 -0700319 /* add up i-th component size, rounding up to 4 bytes */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000320 offset += (uimage_to_cpu(size[i]) + 3) & ~3 ;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100321 }
322
323 /* calculate idx-th component data address */
Nick Spence02b9b222008-05-10 14:02:04 -0700324 *data = img_data + offset;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100325 } else {
326 *len = 0;
327 *data = 0;
328 }
329}
Marian Balakowicz42b73e82008-01-31 13:20:07 +0100330
Stephen Warren712fbcf2011-10-18 11:11:49 +0000331static void image_print_type(const image_header_t *hdr)
Marian Balakowicz2242f532008-02-21 17:27:41 +0100332{
Heiko Schocher80402f32015-06-29 09:10:46 +0200333 const char __maybe_unused *os, *arch, *type, *comp;
Marian Balakowicz2242f532008-02-21 17:27:41 +0100334
Stephen Warren712fbcf2011-10-18 11:11:49 +0000335 os = genimg_get_os_name(image_get_os(hdr));
336 arch = genimg_get_arch_name(image_get_arch(hdr));
337 type = genimg_get_type_name(image_get_type(hdr));
338 comp = genimg_get_comp_name(image_get_comp(hdr));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100339
Stephen Warren712fbcf2011-10-18 11:11:49 +0000340 printf("%s %s %s (%s)\n", arch, os, type, comp);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100341}
342
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100343/**
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200344 * image_print_contents - prints out the contents of the legacy format image
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200345 * @ptr: pointer to the legacy format image header
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100346 * @p: pointer to prefix string
347 *
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200348 * image_print_contents() formats a multi line legacy image contents description.
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100349 * The routine prints out all header fields followed by the size/offset data
350 * for MULTI/SCRIPT images.
351 *
352 * returns:
353 * no returned results
354 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000355void image_print_contents(const void *ptr)
Marian Balakowicz2242f532008-02-21 17:27:41 +0100356{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200357 const image_header_t *hdr = (const image_header_t *)ptr;
Heiko Schocher80402f32015-06-29 09:10:46 +0200358 const char __maybe_unused *p;
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200359
Simon Glass1fe7d932013-05-08 08:05:58 +0000360 p = IMAGE_INDENT_STRING;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000361 printf("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name(hdr));
Simon Glass859e92b2013-05-07 06:11:51 +0000362 if (IMAGE_ENABLE_TIMESTAMP) {
363 printf("%sCreated: ", p);
364 genimg_print_time((time_t)image_get_time(hdr));
365 }
Stephen Warren712fbcf2011-10-18 11:11:49 +0000366 printf("%sImage Type: ", p);
367 image_print_type(hdr);
368 printf("%sData Size: ", p);
369 genimg_print_size(image_get_data_size(hdr));
370 printf("%sLoad Address: %08x\n", p, image_get_load(hdr));
371 printf("%sEntry Point: %08x\n", p, image_get_ep(hdr));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100372
Stephen Warren712fbcf2011-10-18 11:11:49 +0000373 if (image_check_type(hdr, IH_TYPE_MULTI) ||
374 image_check_type(hdr, IH_TYPE_SCRIPT)) {
Marian Balakowicz2242f532008-02-21 17:27:41 +0100375 int i;
376 ulong data, len;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000377 ulong count = image_multi_count(hdr);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100378
Stephen Warren712fbcf2011-10-18 11:11:49 +0000379 printf("%sContents:\n", p);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100380 for (i = 0; i < count; i++) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000381 image_multi_getimg(hdr, i, &data, &len);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100382
Stephen Warren712fbcf2011-10-18 11:11:49 +0000383 printf("%s Image %d: ", p, i);
384 genimg_print_size(len);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100385
Stephen Warren712fbcf2011-10-18 11:11:49 +0000386 if (image_check_type(hdr, IH_TYPE_SCRIPT) && i > 0) {
Marian Balakowicz570abb02008-02-29 15:59:59 +0100387 /*
388 * the user may need to know offsets
389 * if planning to do something with
390 * multiple files
391 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000392 printf("%s Offset = 0x%08lx\n", p, data);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100393 }
Marian Balakowicz2242f532008-02-21 17:27:41 +0100394 }
Sven Ebenfeldd21bd692016-11-06 16:37:56 +0100395 } else if (image_check_type(hdr, IH_TYPE_FIRMWARE_IVT)) {
396 printf("HAB Blocks: 0x%08x 0x0000 0x%08x\n",
Breno Matheus Lima5b20d142019-09-23 18:39:47 +0000397 image_get_load(hdr) - image_get_header_size(),
398 (int)(image_get_size(hdr) + image_get_header_size()
399 + sizeof(flash_header_v2_t) - 0x2060));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100400 }
401}
402
Julius Werner20908542019-07-24 19:37:54 -0700403/**
404 * print_decomp_msg() - Print a suitable decompression/loading message
405 *
406 * @type: OS type (IH_OS_...)
407 * @comp_type: Compression type being used (IH_COMP_...)
408 * @is_xip: true if the load address matches the image start
409 */
410static void print_decomp_msg(int comp_type, int type, bool is_xip)
411{
412 const char *name = genimg_get_type_name(type);
413
414 if (comp_type == IH_COMP_NONE)
415 printf(" %s %s\n", is_xip ? "XIP" : "Loading", name);
416 else
417 printf(" Uncompressing %s\n", name);
418}
419
Atish Patra155d6a32020-03-05 16:24:22 -0800420int image_decomp_type(const unsigned char *buf, ulong len)
421{
422 const struct comp_magic_map *cmagic = image_comp;
423
424 if (len < 2)
425 return -EINVAL;
426
427 for (; cmagic->comp_id > 0; cmagic++) {
428 if (!memcmp(buf, cmagic->magic, 2))
429 break;
430 }
431
432 return cmagic->comp_id;
433}
434
Julius Werner20908542019-07-24 19:37:54 -0700435int image_decomp(int comp, ulong load, ulong image_start, int type,
436 void *load_buf, void *image_buf, ulong image_len,
437 uint unc_len, ulong *load_end)
438{
439 int ret = 0;
440
441 *load_end = load;
442 print_decomp_msg(comp, type, load == image_start);
443
444 /*
445 * Load the image to the right place, decompressing if needed. After
446 * this, image_len will be set to the number of uncompressed bytes
447 * loaded, ret will be non-zero on error.
448 */
449 switch (comp) {
450 case IH_COMP_NONE:
451 if (load == image_start)
452 break;
453 if (image_len <= unc_len)
454 memmove_wd(load_buf, image_buf, image_len, CHUNKSZ);
455 else
456 ret = -ENOSPC;
457 break;
458#ifdef CONFIG_GZIP
459 case IH_COMP_GZIP: {
460 ret = gunzip(load_buf, unc_len, image_buf, &image_len);
461 break;
462 }
463#endif /* CONFIG_GZIP */
464#ifdef CONFIG_BZIP2
465 case IH_COMP_BZIP2: {
466 uint size = unc_len;
467
468 /*
469 * If we've got less than 4 MB of malloc() space,
470 * use slower decompression algorithm which requires
471 * at most 2300 KB of memory.
472 */
473 ret = BZ2_bzBuffToBuffDecompress(load_buf, &size,
474 image_buf, image_len,
475 CONFIG_SYS_MALLOC_LEN < (4096 * 1024), 0);
476 image_len = size;
477 break;
478 }
479#endif /* CONFIG_BZIP2 */
480#ifdef CONFIG_LZMA
481 case IH_COMP_LZMA: {
482 SizeT lzma_len = unc_len;
483
484 ret = lzmaBuffToBuffDecompress(load_buf, &lzma_len,
485 image_buf, image_len);
486 image_len = lzma_len;
487 break;
488 }
489#endif /* CONFIG_LZMA */
490#ifdef CONFIG_LZO
491 case IH_COMP_LZO: {
492 size_t size = unc_len;
493
494 ret = lzop_decompress(image_buf, image_len, load_buf, &size);
495 image_len = size;
496 break;
497 }
498#endif /* CONFIG_LZO */
499#ifdef CONFIG_LZ4
500 case IH_COMP_LZ4: {
501 size_t size = unc_len;
502
503 ret = ulz4fn(image_buf, image_len, load_buf, &size);
504 image_len = size;
505 break;
506 }
507#endif /* CONFIG_LZ4 */
508 default:
509 printf("Unimplemented compression type %d\n", comp);
510 return -ENOSYS;
511 }
512
513 *load_end = load + image_len;
514
515 return ret;
516}
517
Marian Balakowicz570abb02008-02-29 15:59:59 +0100518
519#ifndef USE_HOSTCC
Tom Rinic76c93a2019-05-23 07:14:07 -0400520#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100521/**
522 * image_get_ramdisk - get and verify ramdisk image
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100523 * @rd_addr: ramdisk image start address
524 * @arch: expected ramdisk architecture
525 * @verify: checksum verification flag
526 *
527 * image_get_ramdisk() returns a pointer to the verified ramdisk image
528 * header. Routine receives image start address and expected architecture
529 * flag. Verification done covers data and header integrity and os/type/arch
530 * fields checking.
531 *
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100532 * returns:
533 * pointer to a ramdisk image header, if image was found and valid
Kumar Gala274cea22008-02-27 21:51:46 -0600534 * otherwise, return NULL
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100535 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000536static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
Marian Balakowiczd985c842008-03-12 10:14:38 +0100537 int verify)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100538{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200539 const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100540
Stephen Warren712fbcf2011-10-18 11:11:49 +0000541 if (!image_check_magic(rd_hdr)) {
542 puts("Bad Magic Number\n");
Simon Glass770605e2012-02-13 13:51:18 +0000543 bootstage_error(BOOTSTAGE_ID_RD_MAGIC);
Kumar Gala274cea22008-02-27 21:51:46 -0600544 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100545 }
546
Stephen Warren712fbcf2011-10-18 11:11:49 +0000547 if (!image_check_hcrc(rd_hdr)) {
548 puts("Bad Header Checksum\n");
Simon Glass770605e2012-02-13 13:51:18 +0000549 bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
Kumar Gala274cea22008-02-27 21:51:46 -0600550 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100551 }
552
Simon Glass770605e2012-02-13 13:51:18 +0000553 bootstage_mark(BOOTSTAGE_ID_RD_MAGIC);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000554 image_print_contents(rd_hdr);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100555
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100556 if (verify) {
557 puts(" Verifying Checksum ... ");
Stephen Warren712fbcf2011-10-18 11:11:49 +0000558 if (!image_check_dcrc(rd_hdr)) {
559 puts("Bad Data CRC\n");
Simon Glass770605e2012-02-13 13:51:18 +0000560 bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM);
Kumar Gala274cea22008-02-27 21:51:46 -0600561 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100562 }
563 puts("OK\n");
564 }
565
Simon Glass770605e2012-02-13 13:51:18 +0000566 bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100567
Stephen Warren712fbcf2011-10-18 11:11:49 +0000568 if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
569 !image_check_arch(rd_hdr, arch) ||
570 !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
571 printf("No Linux %s Ramdisk Image\n",
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100572 genimg_get_arch_name(arch));
Simon Glass770605e2012-02-13 13:51:18 +0000573 bootstage_error(BOOTSTAGE_ID_RAMDISK);
Kumar Gala274cea22008-02-27 21:51:46 -0600574 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100575 }
576
577 return rd_hdr;
578}
Heiko Schocher21d29f72014-05-28 11:33:33 +0200579#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100580#endif /* !USE_HOSTCC */
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100581
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100582/*****************************************************************************/
583/* Shared dual-format routines */
584/*****************************************************************************/
Marian Balakowicz570abb02008-02-29 15:59:59 +0100585#ifndef USE_HOSTCC
Simon Glassbb872dd2019-12-28 10:45:02 -0700586ulong image_load_addr = CONFIG_SYS_LOAD_ADDR; /* Default Load Address */
587ulong image_save_addr; /* Default Save Address */
588ulong image_save_size; /* Default Save Size (in bytes) */
Joe Hershberger1cf0a8b2012-12-11 22:16:28 -0600589
590static int on_loadaddr(const char *name, const char *value, enum env_op op,
591 int flags)
592{
593 switch (op) {
594 case env_op_create:
595 case env_op_overwrite:
Simon Glassbb872dd2019-12-28 10:45:02 -0700596 image_load_addr = simple_strtoul(value, NULL, 16);
Joe Hershberger1cf0a8b2012-12-11 22:16:28 -0600597 break;
598 default:
599 break;
600 }
601
602 return 0;
603}
604U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
605
Simon Glass723806c2017-08-03 12:22:15 -0600606ulong env_get_bootm_low(void)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100607{
Simon Glass00caae62017-08-03 12:22:12 -0600608 char *s = env_get("bootm_low");
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100609 if (s) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000610 ulong tmp = simple_strtoul(s, NULL, 16);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100611 return tmp;
612 }
613
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200614#if defined(CONFIG_SYS_SDRAM_BASE)
615 return CONFIG_SYS_SDRAM_BASE;
Michal Simeka750ded2019-09-25 09:11:48 +0200616#elif defined(CONFIG_ARM) || defined(CONFIG_MICROBLAZE)
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100617 return gd->bd->bi_dram[0].start;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100618#else
619 return 0;
620#endif
621}
622
Simon Glass723806c2017-08-03 12:22:15 -0600623phys_size_t env_get_bootm_size(void)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100624{
Masahiro Yamada0cb389d2016-02-05 16:12:50 +0900625 phys_size_t tmp, size;
626 phys_addr_t start;
Simon Glass00caae62017-08-03 12:22:12 -0600627 char *s = env_get("bootm_size");
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100628 if (s) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000629 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100630 return tmp;
631 }
Masahiro Yamada0cb389d2016-02-05 16:12:50 +0900632
Michal Simeka750ded2019-09-25 09:11:48 +0200633#if (defined(CONFIG_ARM) || defined(CONFIG_MICROBLAZE)) && \
634 defined(CONFIG_NR_DRAM_BANKS)
Masahiro Yamada0cb389d2016-02-05 16:12:50 +0900635 start = gd->bd->bi_dram[0].start;
636 size = gd->bd->bi_dram[0].size;
637#else
638 start = gd->bd->bi_memstart;
639 size = gd->bd->bi_memsize;
640#endif
641
Simon Glass00caae62017-08-03 12:22:12 -0600642 s = env_get("bootm_low");
Matthew McClintockc519fac2010-07-08 10:11:08 -0500643 if (s)
Stephen Warren712fbcf2011-10-18 11:11:49 +0000644 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
Matthew McClintockc519fac2010-07-08 10:11:08 -0500645 else
Masahiro Yamada0cb389d2016-02-05 16:12:50 +0900646 tmp = start;
Matthew McClintockc519fac2010-07-08 10:11:08 -0500647
Masahiro Yamada0cb389d2016-02-05 16:12:50 +0900648 return size - (tmp - start);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100649}
650
Simon Glass723806c2017-08-03 12:22:15 -0600651phys_size_t env_get_bootm_mapsize(void)
Grant Likelyc3624e62011-03-28 09:58:43 +0000652{
653 phys_size_t tmp;
Simon Glass00caae62017-08-03 12:22:12 -0600654 char *s = env_get("bootm_mapsize");
Grant Likelyc3624e62011-03-28 09:58:43 +0000655 if (s) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000656 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
Grant Likelyc3624e62011-03-28 09:58:43 +0000657 return tmp;
658 }
659
660#if defined(CONFIG_SYS_BOOTMAPSZ)
661 return CONFIG_SYS_BOOTMAPSZ;
662#else
Simon Glass723806c2017-08-03 12:22:15 -0600663 return env_get_bootm_size();
Grant Likelyc3624e62011-03-28 09:58:43 +0000664#endif
665}
666
Stephen Warren712fbcf2011-10-18 11:11:49 +0000667void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100668{
Larry Johnson54fa2c52010-04-20 08:09:43 -0400669 if (to == from)
670 return;
671
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100672#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
Sonic Zhang22cfddc2014-12-10 18:20:53 +0800673 if (to > from) {
674 from += len;
675 to += len;
676 }
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100677 while (len > 0) {
678 size_t tail = (len > chunksz) ? chunksz : len;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000679 WATCHDOG_RESET();
Sonic Zhang22cfddc2014-12-10 18:20:53 +0800680 if (to > from) {
681 to -= tail;
682 from -= tail;
683 }
Stephen Warren712fbcf2011-10-18 11:11:49 +0000684 memmove(to, from, tail);
Sonic Zhang22cfddc2014-12-10 18:20:53 +0800685 if (to < from) {
686 to += tail;
687 from += tail;
688 }
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100689 len -= tail;
690 }
691#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000692 memmove(to, from, len);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100693#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
694}
Julius Werner20908542019-07-24 19:37:54 -0700695#else /* USE_HOSTCC */
696void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
697{
698 memmove(to, from, len);
699}
Marian Balakowicz570abb02008-02-29 15:59:59 +0100700#endif /* !USE_HOSTCC */
701
Stephen Warren712fbcf2011-10-18 11:11:49 +0000702void genimg_print_size(uint32_t size)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100703{
704#ifndef USE_HOSTCC
Stephen Warren712fbcf2011-10-18 11:11:49 +0000705 printf("%d Bytes = ", size);
706 print_size(size, "\n");
Marian Balakowicz570abb02008-02-29 15:59:59 +0100707#else
xypron.glpk@gmx.decec85d42017-01-04 14:04:44 +0100708 printf("%d Bytes = %.2f KiB = %.2f MiB\n",
Marian Balakowicz570abb02008-02-29 15:59:59 +0100709 size, (double)size / 1.024e3,
710 (double)size / 1.048576e6);
711#endif
712}
713
Simon Glass859e92b2013-05-07 06:11:51 +0000714#if IMAGE_ENABLE_TIMESTAMP
715void genimg_print_time(time_t timestamp)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100716{
717#ifndef USE_HOSTCC
718 struct rtc_time tm;
719
Simon Glass9f9276c2015-04-20 12:37:18 -0600720 rtc_to_tm(timestamp, &tm);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000721 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
Marian Balakowicz570abb02008-02-29 15:59:59 +0100722 tm.tm_year, tm.tm_mon, tm.tm_mday,
723 tm.tm_hour, tm.tm_min, tm.tm_sec);
724#else
Stephen Warren712fbcf2011-10-18 11:11:49 +0000725 printf("%s", ctime(&timestamp));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100726#endif
727}
Simon Glass859e92b2013-05-07 06:11:51 +0000728#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100729
Simon Glass5b9d44d2015-06-23 15:38:25 -0600730const table_entry_t *get_table_entry(const table_entry_t *table, int id)
731{
732 for (; table->id >= 0; ++table) {
733 if (table->id == id)
734 return table;
735 }
736 return NULL;
737}
738
Simon Glass14262202016-06-30 10:52:16 -0600739static const char *unknown_msg(enum ih_category category)
740{
Simon Glassae3de0d2016-10-31 10:21:09 -0600741 static const char unknown_str[] = "Unknown ";
Simon Glass14262202016-06-30 10:52:16 -0600742 static char msg[30];
743
Simon Glassae3de0d2016-10-31 10:21:09 -0600744 strcpy(msg, unknown_str);
745 strncat(msg, table_info[category].desc,
746 sizeof(msg) - sizeof(unknown_str));
Simon Glass14262202016-06-30 10:52:16 -0600747
748 return msg;
749}
750
751/**
752 * get_cat_table_entry_name - translate entry id to long name
753 * @category: category to look up (enum ih_category)
754 * @id: entry id to be translated
755 *
756 * This will scan the translation table trying to find the entry that matches
757 * the given id.
758 *
759 * @retur long entry name if translation succeeds; error string on failure
760 */
761const char *genimg_get_cat_name(enum ih_category category, uint id)
762{
763 const table_entry_t *entry;
764
765 entry = get_table_entry(table_info[category].table, id);
766 if (!entry)
767 return unknown_msg(category);
768#if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
769 return entry->lname;
770#else
771 return entry->lname + gd->reloc_off;
772#endif
773}
774
775/**
776 * get_cat_table_entry_short_name - translate entry id to short name
777 * @category: category to look up (enum ih_category)
778 * @id: entry id to be translated
779 *
780 * This will scan the translation table trying to find the entry that matches
781 * the given id.
782 *
783 * @retur short entry name if translation succeeds; error string on failure
784 */
785const char *genimg_get_cat_short_name(enum ih_category category, uint id)
786{
787 const table_entry_t *entry;
788
789 entry = get_table_entry(table_info[category].table, id);
790 if (!entry)
791 return unknown_msg(category);
792#if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
793 return entry->sname;
794#else
795 return entry->sname + gd->reloc_off;
796#endif
797}
798
799int genimg_get_cat_count(enum ih_category category)
800{
801 return table_info[category].count;
802}
803
804const char *genimg_get_cat_desc(enum ih_category category)
805{
806 return table_info[category].desc;
807}
808
Marian Balakowicz570abb02008-02-29 15:59:59 +0100809/**
810 * get_table_entry_name - translate entry id to long name
811 * @table: pointer to a translation table for entries of a specific type
812 * @msg: message to be returned when translation fails
813 * @id: entry id to be translated
814 *
815 * get_table_entry_name() will go over translation table trying to find
816 * entry that matches given id. If matching entry is found, its long
817 * name is returned to the caller.
818 *
819 * returns:
820 * long entry name if translation succeeds
821 * msg otherwise
822 */
Mike Frysinger7edb1862010-10-20 07:17:39 -0400823char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100824{
Simon Glass5b9d44d2015-06-23 15:38:25 -0600825 table = get_table_entry(table, id);
826 if (!table)
827 return msg;
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200828#if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
Simon Glass5b9d44d2015-06-23 15:38:25 -0600829 return table->lname;
Scott Woode3d1ac72009-04-02 16:15:10 -0500830#else
Simon Glass5b9d44d2015-06-23 15:38:25 -0600831 return table->lname + gd->reloc_off;
Scott Woode3d1ac72009-04-02 16:15:10 -0500832#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100833}
834
Stephen Warren712fbcf2011-10-18 11:11:49 +0000835const char *genimg_get_os_name(uint8_t os)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100836{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000837 return (get_table_entry_name(uimage_os, "Unknown OS", os));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100838}
839
Stephen Warren712fbcf2011-10-18 11:11:49 +0000840const char *genimg_get_arch_name(uint8_t arch)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100841{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000842 return (get_table_entry_name(uimage_arch, "Unknown Architecture",
843 arch));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100844}
845
Stephen Warren712fbcf2011-10-18 11:11:49 +0000846const char *genimg_get_type_name(uint8_t type)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100847{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000848 return (get_table_entry_name(uimage_type, "Unknown Image", type));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100849}
850
Simon Glasscef2e512016-02-22 22:55:50 -0700851static const char *genimg_get_short_name(const table_entry_t *table, int val)
Simon Glass5b9d44d2015-06-23 15:38:25 -0600852{
Simon Glasscef2e512016-02-22 22:55:50 -0700853 table = get_table_entry(table, val);
Simon Glass5b9d44d2015-06-23 15:38:25 -0600854 if (!table)
855 return "unknown";
856#if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
857 return table->sname;
858#else
859 return table->sname + gd->reloc_off;
860#endif
861}
862
Simon Glasscef2e512016-02-22 22:55:50 -0700863const char *genimg_get_type_short_name(uint8_t type)
864{
865 return genimg_get_short_name(uimage_type, type);
866}
867
Stephen Warren712fbcf2011-10-18 11:11:49 +0000868const char *genimg_get_comp_name(uint8_t comp)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100869{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000870 return (get_table_entry_name(uimage_comp, "Unknown Compression",
871 comp));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100872}
873
Simon Glasscef2e512016-02-22 22:55:50 -0700874const char *genimg_get_comp_short_name(uint8_t comp)
875{
876 return genimg_get_short_name(uimage_comp, comp);
877}
878
879const char *genimg_get_os_short_name(uint8_t os)
880{
881 return genimg_get_short_name(uimage_os, os);
882}
883
884const char *genimg_get_arch_short_name(uint8_t arch)
885{
886 return genimg_get_short_name(uimage_arch, arch);
887}
888
Marian Balakowicz570abb02008-02-29 15:59:59 +0100889/**
890 * get_table_entry_id - translate short entry name to id
891 * @table: pointer to a translation table for entries of a specific type
892 * @table_name: to be used in case of error
893 * @name: entry short name to be translated
894 *
895 * get_table_entry_id() will go over translation table trying to find
896 * entry that matches given short name. If matching entry is found,
897 * its id returned to the caller.
898 *
899 * returns:
900 * entry id if translation succeeds
901 * -1 otherwise
902 */
Mike Frysinger7edb1862010-10-20 07:17:39 -0400903int get_table_entry_id(const table_entry_t *table,
Marian Balakowicz570abb02008-02-29 15:59:59 +0100904 const char *table_name, const char *name)
905{
Mike Frysinger7edb1862010-10-20 07:17:39 -0400906 const table_entry_t *t;
Marian Balakowicz570abb02008-02-29 15:59:59 +0100907
908 for (t = table; t->id >= 0; ++t) {
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200909#ifdef CONFIG_NEEDS_MANUAL_RELOC
Simon Glass5b9d44d2015-06-23 15:38:25 -0600910 if (t->sname && strcasecmp(t->sname + gd->reloc_off, name) == 0)
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200911#else
Simon Glass5b9d44d2015-06-23 15:38:25 -0600912 if (t->sname && strcasecmp(t->sname, name) == 0)
Peter Tyser521af042009-09-21 11:20:36 -0500913#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100914 return (t->id);
915 }
Stephen Warren712fbcf2011-10-18 11:11:49 +0000916 debug("Invalid %s Type: %s\n", table_name, name);
Simon Glass5b9d44d2015-06-23 15:38:25 -0600917
918 return -1;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100919}
920
Stephen Warren712fbcf2011-10-18 11:11:49 +0000921int genimg_get_os_id(const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100922{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000923 return (get_table_entry_id(uimage_os, "OS", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100924}
925
Stephen Warren712fbcf2011-10-18 11:11:49 +0000926int genimg_get_arch_id(const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100927{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000928 return (get_table_entry_id(uimage_arch, "CPU", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100929}
930
Stephen Warren712fbcf2011-10-18 11:11:49 +0000931int genimg_get_type_id(const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100932{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000933 return (get_table_entry_id(uimage_type, "Image", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100934}
935
Stephen Warren712fbcf2011-10-18 11:11:49 +0000936int genimg_get_comp_id(const char *name)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100937{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000938 return (get_table_entry_id(uimage_comp, "Compression", name));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100939}
940
941#ifndef USE_HOSTCC
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100942/**
Bryan Wu6c454fe2014-08-15 16:51:38 -0700943 * genimg_get_kernel_addr_fit - get the real kernel address and return 2
944 * FIT strings
Bryan Wu0f641402014-07-31 17:39:58 -0700945 * @img_addr: a string might contain real image address
Bryan Wu6c454fe2014-08-15 16:51:38 -0700946 * @fit_uname_config: double pointer to a char, will hold pointer to a
947 * configuration unit name
948 * @fit_uname_kernel: double pointer to a char, will hold pointer to a subimage
949 * name
Bryan Wu0f641402014-07-31 17:39:58 -0700950 *
Bryan Wu6c454fe2014-08-15 16:51:38 -0700951 * genimg_get_kernel_addr_fit get the real kernel start address from a string
Bryan Wu0f641402014-07-31 17:39:58 -0700952 * which is normally the first argv of bootm/bootz
953 *
954 * returns:
955 * kernel start address
956 */
Bryan Wu6c454fe2014-08-15 16:51:38 -0700957ulong genimg_get_kernel_addr_fit(char * const img_addr,
958 const char **fit_uname_config,
959 const char **fit_uname_kernel)
Bryan Wu0f641402014-07-31 17:39:58 -0700960{
Bryan Wu0f641402014-07-31 17:39:58 -0700961 ulong kernel_addr;
962
963 /* find out kernel image address */
964 if (!img_addr) {
Simon Glassbb872dd2019-12-28 10:45:02 -0700965 kernel_addr = image_load_addr;
Bryan Wu0f641402014-07-31 17:39:58 -0700966 debug("* kernel: default image load address = 0x%08lx\n",
Simon Glassbb872dd2019-12-28 10:45:02 -0700967 image_load_addr);
Simon Glass73223f02016-02-22 22:55:43 -0700968#if CONFIG_IS_ENABLED(FIT)
Simon Glassbb872dd2019-12-28 10:45:02 -0700969 } else if (fit_parse_conf(img_addr, image_load_addr, &kernel_addr,
Bryan Wu6c454fe2014-08-15 16:51:38 -0700970 fit_uname_config)) {
Bryan Wu0f641402014-07-31 17:39:58 -0700971 debug("* kernel: config '%s' from image at 0x%08lx\n",
Bryan Wu6c454fe2014-08-15 16:51:38 -0700972 *fit_uname_config, kernel_addr);
Simon Glassbb872dd2019-12-28 10:45:02 -0700973 } else if (fit_parse_subimage(img_addr, image_load_addr, &kernel_addr,
Bryan Wu6c454fe2014-08-15 16:51:38 -0700974 fit_uname_kernel)) {
Bryan Wu0f641402014-07-31 17:39:58 -0700975 debug("* kernel: subimage '%s' from image at 0x%08lx\n",
Bryan Wu6c454fe2014-08-15 16:51:38 -0700976 *fit_uname_kernel, kernel_addr);
Bryan Wu0f641402014-07-31 17:39:58 -0700977#endif
978 } else {
979 kernel_addr = simple_strtoul(img_addr, NULL, 16);
980 debug("* kernel: cmdline image address = 0x%08lx\n",
981 kernel_addr);
982 }
983
984 return kernel_addr;
985}
986
987/**
Bryan Wu6c454fe2014-08-15 16:51:38 -0700988 * genimg_get_kernel_addr() is the simple version of
989 * genimg_get_kernel_addr_fit(). It ignores those return FIT strings
990 */
991ulong genimg_get_kernel_addr(char * const img_addr)
992{
993 const char *fit_uname_config = NULL;
994 const char *fit_uname_kernel = NULL;
995
996 return genimg_get_kernel_addr_fit(img_addr, &fit_uname_config,
997 &fit_uname_kernel);
998}
999
1000/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001001 * genimg_get_format - get image format type
1002 * @img_addr: image start address
1003 *
1004 * genimg_get_format() checks whether provided address points to a valid
1005 * legacy or FIT image.
1006 *
1007 * New uImage format and FDT blob are based on a libfdt. FDT blob
1008 * may be passed directly or embedded in a FIT image. In both situations
1009 * genimg_get_format() must be able to dectect libfdt header.
1010 *
1011 * returns:
1012 * image format type or IMAGE_FORMAT_INVALID if no image is present
1013 */
Simon Glass35e7b0f2013-05-07 06:12:03 +00001014int genimg_get_format(const void *img_addr)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001015{
Tom Rinic76c93a2019-05-23 07:14:07 -04001016#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Wolfgang Denk3a2003f2009-08-19 11:42:56 +02001017 const image_header_t *hdr;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001018
Wolfgang Denk3a2003f2009-08-19 11:42:56 +02001019 hdr = (const image_header_t *)img_addr;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001020 if (image_check_magic(hdr))
Heiko Schocher21d29f72014-05-28 11:33:33 +02001021 return IMAGE_FORMAT_LEGACY;
1022#endif
Simon Glassaa34fbc2016-02-22 22:55:45 -07001023#if IMAGE_ENABLE_FIT || IMAGE_ENABLE_OF_LIBFDT
Heiko Schocher21d29f72014-05-28 11:33:33 +02001024 if (fdt_check_header(img_addr) == 0)
1025 return IMAGE_FORMAT_FIT;
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -05001026#endif
1027#ifdef CONFIG_ANDROID_BOOT_IMAGE
Heiko Schocher21d29f72014-05-28 11:33:33 +02001028 if (android_image_check_header(img_addr) == 0)
1029 return IMAGE_FORMAT_ANDROID;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001030#endif
1031
Heiko Schocher21d29f72014-05-28 11:33:33 +02001032 return IMAGE_FORMAT_INVALID;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001033}
1034
1035/**
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001036 * fit_has_config - check if there is a valid FIT configuration
1037 * @images: pointer to the bootm command headers structure
1038 *
1039 * fit_has_config() checks if there is a FIT configuration in use
1040 * (if FTI support is present).
1041 *
1042 * returns:
1043 * 0, no FIT support or no configuration found
1044 * 1, configuration found
1045 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001046int genimg_has_config(bootm_headers_t *images)
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001047{
Simon Glass73223f02016-02-22 22:55:43 -07001048#if IMAGE_ENABLE_FIT
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001049 if (images->fit_uname_cfg)
1050 return 1;
1051#endif
1052 return 0;
1053}
1054
1055/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001056 * boot_get_ramdisk - main ramdisk handling routine
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001057 * @argc: command argument count
1058 * @argv: command argument list
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +01001059 * @images: pointer to the bootm images structure
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001060 * @arch: expected ramdisk architecture
1061 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
1062 * @rd_end: pointer to a ulong variable, will hold ramdisk end
1063 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001064 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001065 * Curently supported are the following ramdisk sources:
1066 * - multicomponent kernel/ramdisk image,
1067 * - commandline provided address of decicated ramdisk image.
1068 *
1069 * returns:
Marian Balakowiczd985c842008-03-12 10:14:38 +01001070 * 0, if ramdisk image was found and valid, or skiped
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001071 * rd_start and rd_end are set to ramdisk start/end addresses if
1072 * ramdisk image is found and valid
Marian Balakowiczd985c842008-03-12 10:14:38 +01001073 *
Kumar Galaea86b9e2008-08-29 19:08:29 -05001074 * 1, if ramdisk image is found but corrupted, or invalid
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001075 * rd_start and rd_end are set to 0 if no ramdisk exists
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001076 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001077int boot_get_ramdisk(int argc, char * const argv[], bootm_headers_t *images,
Marian Balakowiczd985c842008-03-12 10:14:38 +01001078 uint8_t arch, ulong *rd_start, ulong *rd_end)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001079{
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001080 ulong rd_addr, rd_load;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001081 ulong rd_data, rd_len;
Tom Rinic76c93a2019-05-23 07:14:07 -04001082#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Wolfgang Denk3a2003f2009-08-19 11:42:56 +02001083 const image_header_t *rd_hdr;
Heiko Schocher21d29f72014-05-28 11:33:33 +02001084#endif
Simon Glass35e7b0f2013-05-07 06:12:03 +00001085 void *buf;
Marek Vasut57d40ab2012-03-30 23:19:10 +02001086#ifdef CONFIG_SUPPORT_RAW_INITRD
Marek Vasut017e1f32012-03-18 11:47:58 +00001087 char *end;
Marek Vasut57d40ab2012-03-30 23:19:10 +02001088#endif
Simon Glass73223f02016-02-22 22:55:43 -07001089#if IMAGE_ENABLE_FIT
Simon Glassf320a4d2013-07-10 23:08:10 -07001090 const char *fit_uname_config = images->fit_uname_cfg;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001091 const char *fit_uname_ramdisk = NULL;
1092 ulong default_addr;
Marian Balakowiczc8779642008-03-12 10:12:37 +01001093 int rd_noffset;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001094#endif
Simon Glass983c72f2013-06-11 11:14:46 -07001095 const char *select = NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001096
Marian Balakowiczc8779642008-03-12 10:12:37 +01001097 *rd_start = 0;
1098 *rd_end = 0;
1099
Tom Rini1fec3c52015-08-27 15:42:41 -04001100#ifdef CONFIG_ANDROID_BOOT_IMAGE
1101 /*
1102 * Look for an Android boot image.
1103 */
1104 buf = map_sysmem(images->os.start, 0);
Tom Rinic139b5f2015-10-27 19:04:40 -04001105 if (buf && genimg_get_format(buf) == IMAGE_FORMAT_ANDROID)
Shawn Guo5e218862019-02-22 16:28:06 +08001106 select = (argc == 0) ? env_get("loadaddr") : argv[0];
Tom Rini1fec3c52015-08-27 15:42:41 -04001107#endif
1108
Simon Glass983c72f2013-06-11 11:14:46 -07001109 if (argc >= 2)
1110 select = argv[1];
Rob Herring2dd46322015-07-17 10:57:17 -05001111
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001112 /*
1113 * Look for a '-' which indicates to ignore the
1114 * ramdisk argument
1115 */
Simon Glass983c72f2013-06-11 11:14:46 -07001116 if (select && strcmp(select, "-") == 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001117 debug("## Skipping init Ramdisk\n");
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001118 rd_len = rd_data = 0;
Simon Glass983c72f2013-06-11 11:14:46 -07001119 } else if (select || genimg_has_config(images)) {
Simon Glass73223f02016-02-22 22:55:43 -07001120#if IMAGE_ENABLE_FIT
Simon Glass983c72f2013-06-11 11:14:46 -07001121 if (select) {
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001122 /*
1123 * If the init ramdisk comes from the FIT image and
1124 * the FIT image address is omitted in the command
1125 * line argument, try to use os FIT image address or
1126 * default load address.
1127 */
1128 if (images->fit_uname_os)
1129 default_addr = (ulong)images->fit_hdr_os;
1130 else
Simon Glassbb872dd2019-12-28 10:45:02 -07001131 default_addr = image_load_addr;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001132
Simon Glass983c72f2013-06-11 11:14:46 -07001133 if (fit_parse_conf(select, default_addr,
1134 &rd_addr, &fit_uname_config)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001135 debug("* ramdisk: config '%s' from image at "
1136 "0x%08lx\n",
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001137 fit_uname_config, rd_addr);
Simon Glass983c72f2013-06-11 11:14:46 -07001138 } else if (fit_parse_subimage(select, default_addr,
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001139 &rd_addr, &fit_uname_ramdisk)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001140 debug("* ramdisk: subimage '%s' from image at "
1141 "0x%08lx\n",
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001142 fit_uname_ramdisk, rd_addr);
1143 } else
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001144#endif
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001145 {
Simon Glass983c72f2013-06-11 11:14:46 -07001146 rd_addr = simple_strtoul(select, NULL, 16);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001147 debug("* ramdisk: cmdline image address = "
1148 "0x%08lx\n",
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001149 rd_addr);
1150 }
Simon Glass73223f02016-02-22 22:55:43 -07001151#if IMAGE_ENABLE_FIT
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001152 } else {
1153 /* use FIT configuration provided in first bootm
Simon Glassa51ec632013-05-16 13:53:22 +00001154 * command argument. If the property is not defined,
1155 * quit silently.
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001156 */
Simon Glass35e7b0f2013-05-07 06:12:03 +00001157 rd_addr = map_to_sysmem(images->fit_hdr_os);
Simon Glassa51ec632013-05-16 13:53:22 +00001158 rd_noffset = fit_get_node_from_config(images,
1159 FIT_RAMDISK_PROP, rd_addr);
Paul Burtonbd86ef12016-09-20 18:17:12 +01001160 if (rd_noffset == -ENOENT)
Peter Tyser41266c92008-08-05 10:51:57 -05001161 return 0;
Simon Glassa51ec632013-05-16 13:53:22 +00001162 else if (rd_noffset < 0)
1163 return 1;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001164 }
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001165#endif
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001166
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001167 /*
1168 * Check if there is an initrd image at the
1169 * address provided in the second bootm argument
1170 * check image type, for FIT images get FIT node.
1171 */
Simon Glass35e7b0f2013-05-07 06:12:03 +00001172 buf = map_sysmem(rd_addr, 0);
1173 switch (genimg_get_format(buf)) {
Tom Rinic76c93a2019-05-23 07:14:07 -04001174#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001175 case IMAGE_FORMAT_LEGACY:
Stephen Warren712fbcf2011-10-18 11:11:49 +00001176 printf("## Loading init Ramdisk from Legacy "
Marian Balakowiczc8779642008-03-12 10:12:37 +01001177 "Image at %08lx ...\n", rd_addr);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001178
Simon Glass770605e2012-02-13 13:51:18 +00001179 bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001180 rd_hdr = image_get_ramdisk(rd_addr, arch,
Marian Balakowiczd985c842008-03-12 10:14:38 +01001181 images->verify);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001182
Marian Balakowiczc8779642008-03-12 10:12:37 +01001183 if (rd_hdr == NULL)
Kumar Gala274cea22008-02-27 21:51:46 -06001184 return 1;
Kumar Gala274cea22008-02-27 21:51:46 -06001185
Stephen Warren712fbcf2011-10-18 11:11:49 +00001186 rd_data = image_get_data(rd_hdr);
1187 rd_len = image_get_data_size(rd_hdr);
1188 rd_load = image_get_load(rd_hdr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001189 break;
Heiko Schocher21d29f72014-05-28 11:33:33 +02001190#endif
Simon Glass73223f02016-02-22 22:55:43 -07001191#if IMAGE_ENABLE_FIT
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001192 case IMAGE_FORMAT_FIT:
Simon Glass126cc862014-06-12 07:24:47 -06001193 rd_noffset = fit_image_load(images,
Simon Glassa51ec632013-05-16 13:53:22 +00001194 rd_addr, &fit_uname_ramdisk,
Simon Glassf320a4d2013-07-10 23:08:10 -07001195 &fit_uname_config, arch,
Simon Glassa51ec632013-05-16 13:53:22 +00001196 IH_TYPE_RAMDISK,
1197 BOOTSTAGE_ID_FIT_RD_START,
Simon Glassfe20a812014-08-22 14:26:43 -06001198 FIT_LOAD_OPTIONAL_NON_ZERO,
1199 &rd_data, &rd_len);
Simon Glassa51ec632013-05-16 13:53:22 +00001200 if (rd_noffset < 0)
Michal Simekc78fce62008-07-11 10:43:13 +02001201 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +01001202
Simon Glassa51ec632013-05-16 13:53:22 +00001203 images->fit_hdr_rd = map_sysmem(rd_addr, 0);
Marian Balakowiczc8779642008-03-12 10:12:37 +01001204 images->fit_uname_rd = fit_uname_ramdisk;
Marian Balakowicz3dfe1102008-03-12 10:32:59 +01001205 images->fit_noffset_rd = rd_noffset;
Marian Balakowiczc8779642008-03-12 10:12:37 +01001206 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001207#endif
Rob Herring2dd46322015-07-17 10:57:17 -05001208#ifdef CONFIG_ANDROID_BOOT_IMAGE
1209 case IMAGE_FORMAT_ANDROID:
1210 android_image_get_ramdisk((void *)images->os.start,
1211 &rd_data, &rd_len);
1212 break;
1213#endif
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001214 default:
Marek Vasut017e1f32012-03-18 11:47:58 +00001215#ifdef CONFIG_SUPPORT_RAW_INITRD
Simon Glass983c72f2013-06-11 11:14:46 -07001216 end = NULL;
1217 if (select)
1218 end = strchr(select, ':');
1219 if (end) {
Marek Vasut017e1f32012-03-18 11:47:58 +00001220 rd_len = simple_strtoul(++end, NULL, 16);
1221 rd_data = rd_addr;
1222 } else
1223#endif
1224 {
1225 puts("Wrong Ramdisk Image Format\n");
1226 rd_data = rd_len = rd_load = 0;
1227 return 1;
1228 }
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001229 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001230 } else if (images->legacy_hdr_valid &&
Stephen Warren712fbcf2011-10-18 11:11:49 +00001231 image_check_type(&images->legacy_hdr_os_copy,
1232 IH_TYPE_MULTI)) {
1233
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001234 /*
1235 * Now check if we have a legacy mult-component image,
1236 * get second entry data start address and len.
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001237 */
Simon Glass770605e2012-02-13 13:51:18 +00001238 bootstage_mark(BOOTSTAGE_ID_RAMDISK);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001239 printf("## Loading init Ramdisk from multi component "
Marian Balakowiczc8779642008-03-12 10:12:37 +01001240 "Legacy Image at %08lx ...\n",
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001241 (ulong)images->legacy_hdr_os);
1242
Stephen Warren712fbcf2011-10-18 11:11:49 +00001243 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
Rob Herring2dd46322015-07-17 10:57:17 -05001244 } else {
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001245 /*
1246 * no initrd image
1247 */
Simon Glass770605e2012-02-13 13:51:18 +00001248 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001249 rd_len = rd_data = 0;
1250 }
1251
1252 if (!rd_data) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001253 debug("## No init Ramdisk\n");
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001254 } else {
1255 *rd_start = rd_data;
1256 *rd_end = rd_data + rd_len;
1257 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00001258 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001259 *rd_start, *rd_end);
Kumar Gala274cea22008-02-27 21:51:46 -06001260
1261 return 0;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001262}
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001263
John Rigbyfca43cc2010-10-13 13:57:35 -06001264#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001265/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001266 * boot_ramdisk_high - relocate init ramdisk
Kumar Galae822d7f2008-02-27 21:51:49 -06001267 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001268 * @rd_data: ramdisk data start address
1269 * @rd_len: ramdisk data length
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001270 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
1271 * start address (after possible relocation)
1272 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
1273 * end address (after possible relocation)
1274 *
Robert P. J. Day1bce2ae2013-09-16 07:15:45 -04001275 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001276 * variable and if requested ramdisk data is moved to a specified location.
1277 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001278 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1279 * start/end addresses if ramdisk image start and len were provided,
1280 * otherwise set initrd_start and initrd_end set to zeros.
1281 *
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001282 * returns:
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001283 * 0 - success
1284 * -1 - failure
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001285 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001286int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
Kumar Galae822d7f2008-02-27 21:51:49 -06001287 ulong *initrd_start, ulong *initrd_end)
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001288{
1289 char *s;
1290 ulong initrd_high;
1291 int initrd_copy_to_ram = 1;
1292
Simon Glass00caae62017-08-03 12:22:12 -06001293 s = env_get("initrd_high");
1294 if (s) {
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001295 /* a value of "no" or a similar string will act like 0,
1296 * turning the "load high" feature off. This is intentional.
1297 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001298 initrd_high = simple_strtoul(s, NULL, 16);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001299 if (initrd_high == ~0)
1300 initrd_copy_to_ram = 0;
1301 } else {
Simon Glass723806c2017-08-03 12:22:15 -06001302 initrd_high = env_get_bootm_mapsize() + env_get_bootm_low();
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001303 }
1304
Marian Balakowicz95d449a2008-05-13 15:53:29 +02001305
Stephen Warren712fbcf2011-10-18 11:11:49 +00001306 debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001307 initrd_high, initrd_copy_to_ram);
1308
1309 if (rd_data) {
1310 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001311 debug(" in-place initrd\n");
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001312 *initrd_start = rd_data;
1313 *initrd_end = rd_data + rd_len;
Kumar Galae822d7f2008-02-27 21:51:49 -06001314 lmb_reserve(lmb, rd_data, rd_len);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001315 } else {
Kumar Galae822d7f2008-02-27 21:51:49 -06001316 if (initrd_high)
Stephen Warren712fbcf2011-10-18 11:11:49 +00001317 *initrd_start = (ulong)lmb_alloc_base(lmb,
1318 rd_len, 0x1000, initrd_high);
Kumar Galae822d7f2008-02-27 21:51:49 -06001319 else
Stephen Warren712fbcf2011-10-18 11:11:49 +00001320 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
1321 0x1000);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001322
Kumar Galae822d7f2008-02-27 21:51:49 -06001323 if (*initrd_start == 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001324 puts("ramdisk - allocation error\n");
Kumar Galae822d7f2008-02-27 21:51:49 -06001325 goto error;
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001326 }
Simon Glass770605e2012-02-13 13:51:18 +00001327 bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001328
1329 *initrd_end = *initrd_start + rd_len;
Stephen Warren712fbcf2011-10-18 11:11:49 +00001330 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001331 *initrd_start, *initrd_end);
1332
Stephen Warren712fbcf2011-10-18 11:11:49 +00001333 memmove_wd((void *)*initrd_start,
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001334 (void *)rd_data, rd_len, CHUNKSZ);
1335
Kumar Gala3b200112011-12-07 04:42:58 +00001336#ifdef CONFIG_MP
1337 /*
1338 * Ensure the image is flushed to memory to handle
1339 * AMP boot scenarios in which we might not be
1340 * HW cache coherent
1341 */
Heiko Schocher1a1e7072017-12-14 11:19:22 +01001342 flush_cache((unsigned long)*initrd_start,
1343 ALIGN(rd_len, ARCH_DMA_MINALIGN));
Kumar Gala3b200112011-12-07 04:42:58 +00001344#endif
Stephen Warren712fbcf2011-10-18 11:11:49 +00001345 puts("OK\n");
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001346 }
1347 } else {
1348 *initrd_start = 0;
1349 *initrd_end = 0;
1350 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00001351 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001352 *initrd_start, *initrd_end);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001353
Kumar Galae822d7f2008-02-27 21:51:49 -06001354 return 0;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001355
Kumar Galae822d7f2008-02-27 21:51:49 -06001356error:
1357 return -1;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001358}
John Rigbyfca43cc2010-10-13 13:57:35 -06001359#endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001360
Simon Glass90268b82014-10-19 21:11:24 -06001361int boot_get_setup(bootm_headers_t *images, uint8_t arch,
1362 ulong *setup_start, ulong *setup_len)
1363{
Simon Glass73223f02016-02-22 22:55:43 -07001364#if IMAGE_ENABLE_FIT
Simon Glass90268b82014-10-19 21:11:24 -06001365 return boot_get_setup_fit(images, arch, setup_start, setup_len);
1366#else
1367 return -ENOENT;
1368#endif
1369}
1370
Simon Glass73223f02016-02-22 22:55:43 -07001371#if IMAGE_ENABLE_FIT
Goldschmidt Simon8b93a922017-11-10 14:17:41 +00001372#if defined(CONFIG_FPGA)
Michal Simek62afc602016-05-17 14:03:50 +02001373int boot_get_fpga(int argc, char * const argv[], bootm_headers_t *images,
1374 uint8_t arch, const ulong *ld_start, ulong * const ld_len)
1375{
1376 ulong tmp_img_addr, img_data, img_len;
1377 void *buf;
1378 int conf_noffset;
1379 int fit_img_result;
Simon Glassb02e4042016-10-02 17:59:28 -06001380 const char *uname, *name;
Michal Simek62afc602016-05-17 14:03:50 +02001381 int err;
1382 int devnum = 0; /* TODO support multi fpga platforms */
Michal Simek62afc602016-05-17 14:03:50 +02001383
1384 /* Check to see if the images struct has a FIT configuration */
1385 if (!genimg_has_config(images)) {
1386 debug("## FIT configuration was not specified\n");
1387 return 0;
1388 }
1389
1390 /*
1391 * Obtain the os FIT header from the images struct
Michal Simek62afc602016-05-17 14:03:50 +02001392 */
1393 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
Michal Simek62afc602016-05-17 14:03:50 +02001394 buf = map_sysmem(tmp_img_addr, 0);
1395 /*
1396 * Check image type. For FIT images get FIT node
1397 * and attempt to locate a generic binary.
1398 */
1399 switch (genimg_get_format(buf)) {
1400 case IMAGE_FORMAT_FIT:
1401 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
1402
Simon Glassb02e4042016-10-02 17:59:28 -06001403 uname = fdt_stringlist_get(buf, conf_noffset, FIT_FPGA_PROP, 0,
1404 NULL);
1405 if (!uname) {
Michal Simek62afc602016-05-17 14:03:50 +02001406 debug("## FPGA image is not specified\n");
1407 return 0;
1408 }
1409 fit_img_result = fit_image_load(images,
1410 tmp_img_addr,
1411 (const char **)&uname,
1412 &(images->fit_uname_cfg),
1413 arch,
1414 IH_TYPE_FPGA,
1415 BOOTSTAGE_ID_FPGA_INIT,
1416 FIT_LOAD_OPTIONAL_NON_ZERO,
1417 &img_data, &img_len);
1418
1419 debug("FPGA image (%s) loaded to 0x%lx/size 0x%lx\n",
1420 uname, img_data, img_len);
1421
1422 if (fit_img_result < 0) {
1423 /* Something went wrong! */
1424 return fit_img_result;
1425 }
1426
Goldschmidt Simon8b93a922017-11-10 14:17:41 +00001427 if (!fpga_is_partial_data(devnum, img_len)) {
Michal Simek62afc602016-05-17 14:03:50 +02001428 name = "full";
1429 err = fpga_loadbitstream(devnum, (char *)img_data,
1430 img_len, BIT_FULL);
1431 if (err)
1432 err = fpga_load(devnum, (const void *)img_data,
1433 img_len, BIT_FULL);
1434 } else {
1435 name = "partial";
1436 err = fpga_loadbitstream(devnum, (char *)img_data,
1437 img_len, BIT_PARTIAL);
1438 if (err)
1439 err = fpga_load(devnum, (const void *)img_data,
1440 img_len, BIT_PARTIAL);
1441 }
1442
Michal Simek62afc602016-05-17 14:03:50 +02001443 if (err)
Michal Simek611a9422016-12-16 10:35:40 +01001444 return err;
1445
1446 printf(" Programming %s bitstream... OK\n", name);
Michal Simek62afc602016-05-17 14:03:50 +02001447 break;
1448 default:
1449 printf("The given image format is not supported (corrupt?)\n");
1450 return 1;
1451 }
1452
1453 return 0;
1454}
1455#endif
1456
Andrew F. Davisd7be5092016-11-29 16:33:20 -06001457static void fit_loadable_process(uint8_t img_type,
1458 ulong img_data,
1459 ulong img_len)
1460{
1461 int i;
1462 const unsigned int count =
1463 ll_entry_count(struct fit_loadable_tbl, fit_loadable);
1464 struct fit_loadable_tbl *fit_loadable_handler =
1465 ll_entry_start(struct fit_loadable_tbl, fit_loadable);
1466 /* For each loadable handler */
1467 for (i = 0; i < count; i++, fit_loadable_handler++)
1468 /* matching this type */
1469 if (fit_loadable_handler->type == img_type)
1470 /* call that handler with this image data */
1471 fit_loadable_handler->handler(img_data, img_len);
1472}
1473
Karl Apsite84a07db2015-05-21 09:52:48 -04001474int boot_get_loadable(int argc, char * const argv[], bootm_headers_t *images,
1475 uint8_t arch, const ulong *ld_start, ulong * const ld_len)
1476{
1477 /*
1478 * These variables are used to hold the current image location
1479 * in system memory.
1480 */
1481 ulong tmp_img_addr;
1482 /*
1483 * These two variables are requirements for fit_image_load, but
1484 * their values are not used
1485 */
1486 ulong img_data, img_len;
1487 void *buf;
1488 int loadables_index;
1489 int conf_noffset;
1490 int fit_img_result;
Simon Glassb02e4042016-10-02 17:59:28 -06001491 const char *uname;
Andrew F. Davisd7be5092016-11-29 16:33:20 -06001492 uint8_t img_type;
Karl Apsite84a07db2015-05-21 09:52:48 -04001493
1494 /* Check to see if the images struct has a FIT configuration */
1495 if (!genimg_has_config(images)) {
1496 debug("## FIT configuration was not specified\n");
1497 return 0;
1498 }
1499
1500 /*
1501 * Obtain the os FIT header from the images struct
Karl Apsite84a07db2015-05-21 09:52:48 -04001502 */
1503 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
Karl Apsite84a07db2015-05-21 09:52:48 -04001504 buf = map_sysmem(tmp_img_addr, 0);
1505 /*
1506 * Check image type. For FIT images get FIT node
1507 * and attempt to locate a generic binary.
1508 */
1509 switch (genimg_get_format(buf)) {
1510 case IMAGE_FORMAT_FIT:
1511 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
1512
1513 for (loadables_index = 0;
Simon Glassb02e4042016-10-02 17:59:28 -06001514 uname = fdt_stringlist_get(buf, conf_noffset,
1515 FIT_LOADABLE_PROP, loadables_index,
1516 NULL), uname;
Karl Apsite84a07db2015-05-21 09:52:48 -04001517 loadables_index++)
1518 {
1519 fit_img_result = fit_image_load(images,
1520 tmp_img_addr,
Simon Glassb02e4042016-10-02 17:59:28 -06001521 &uname,
Karl Apsite84a07db2015-05-21 09:52:48 -04001522 &(images->fit_uname_cfg), arch,
1523 IH_TYPE_LOADABLE,
1524 BOOTSTAGE_ID_FIT_LOADABLE_START,
1525 FIT_LOAD_OPTIONAL_NON_ZERO,
1526 &img_data, &img_len);
1527 if (fit_img_result < 0) {
1528 /* Something went wrong! */
1529 return fit_img_result;
1530 }
Andrew F. Davisd7be5092016-11-29 16:33:20 -06001531
1532 fit_img_result = fit_image_get_node(buf, uname);
1533 if (fit_img_result < 0) {
1534 /* Something went wrong! */
1535 return fit_img_result;
1536 }
1537 fit_img_result = fit_image_get_type(buf,
1538 fit_img_result,
1539 &img_type);
1540 if (fit_img_result < 0) {
1541 /* Something went wrong! */
1542 return fit_img_result;
1543 }
1544
1545 fit_loadable_process(img_type, img_data, img_len);
Karl Apsite84a07db2015-05-21 09:52:48 -04001546 }
1547 break;
1548 default:
1549 printf("The given image format is not supported (corrupt?)\n");
1550 return 1;
1551 }
1552
1553 return 0;
1554}
1555#endif
1556
John Rigbyfca43cc2010-10-13 13:57:35 -06001557#ifdef CONFIG_SYS_BOOT_GET_CMDLINE
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001558/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001559 * boot_get_cmdline - allocate and initialize kernel cmdline
Kumar Galae822d7f2008-02-27 21:51:49 -06001560 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001561 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1562 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1563 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001564 * boot_get_cmdline() allocates space for kernel command line below
Shyam Saini919d25c2018-06-07 19:47:19 +05301565 * BOOTMAPSZ + env_get_bootm_low() address. If "bootargs" U-Boot environment
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001566 * variable is present its contents is copied to allocated kernel
1567 * command line.
1568 *
1569 * returns:
Kumar Galae822d7f2008-02-27 21:51:49 -06001570 * 0 - success
1571 * -1 - failure
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001572 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001573int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001574{
1575 char *cmdline;
1576 char *s;
1577
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001578 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
Simon Glass723806c2017-08-03 12:22:15 -06001579 env_get_bootm_mapsize() + env_get_bootm_low());
Kumar Galae822d7f2008-02-27 21:51:49 -06001580
1581 if (cmdline == NULL)
1582 return -1;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001583
Simon Glass00caae62017-08-03 12:22:12 -06001584 s = env_get("bootargs");
1585 if (!s)
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001586 s = "";
1587
1588 strcpy(cmdline, s);
1589
1590 *cmd_start = (ulong) & cmdline[0];
1591 *cmd_end = *cmd_start + strlen(cmdline);
1592
Stephen Warren712fbcf2011-10-18 11:11:49 +00001593 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001594
Kumar Galae822d7f2008-02-27 21:51:49 -06001595 return 0;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001596}
John Rigbyfca43cc2010-10-13 13:57:35 -06001597#endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001598
John Rigbyfca43cc2010-10-13 13:57:35 -06001599#ifdef CONFIG_SYS_BOOT_GET_KBD
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001600/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001601 * boot_get_kbd - allocate and initialize kernel copy of board info
Kumar Galae822d7f2008-02-27 21:51:49 -06001602 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001603 * @kbd: double pointer to board info data
1604 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001605 * boot_get_kbd() allocates space for kernel copy of board info data below
Simon Glass723806c2017-08-03 12:22:15 -06001606 * BOOTMAPSZ + env_get_bootm_low() address and kernel board info is initialized
Grant Likely590d3ca2011-03-28 09:58:34 +00001607 * with the current u-boot board info data.
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001608 *
1609 * returns:
Kumar Galae822d7f2008-02-27 21:51:49 -06001610 * 0 - success
1611 * -1 - failure
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001612 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001613int boot_get_kbd(struct lmb *lmb, bd_t **kbd)
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001614{
Becky Bruce391fd932008-06-09 20:37:18 -05001615 *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
Simon Glass723806c2017-08-03 12:22:15 -06001616 env_get_bootm_mapsize() + env_get_bootm_low());
Kumar Galae822d7f2008-02-27 21:51:49 -06001617 if (*kbd == NULL)
1618 return -1;
1619
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001620 **kbd = *(gd->bd);
1621
Stephen Warren712fbcf2011-10-18 11:11:49 +00001622 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001623
1624#if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1625 do_bdinfo(NULL, 0, 0, NULL);
1626#endif
1627
Kumar Galae822d7f2008-02-27 21:51:49 -06001628 return 0;
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001629}
John Rigbyfca43cc2010-10-13 13:57:35 -06001630#endif /* CONFIG_SYS_BOOT_GET_KBD */
Simon Glass13d06982013-05-08 08:06:01 +00001631
1632#ifdef CONFIG_LMB
1633int image_setup_linux(bootm_headers_t *images)
1634{
1635 ulong of_size = images->ft_len;
1636 char **of_flat_tree = &images->ft_addr;
Simon Glass13d06982013-05-08 08:06:01 +00001637 struct lmb *lmb = &images->lmb;
Simon Glass13d06982013-05-08 08:06:01 +00001638 int ret;
1639
1640 if (IMAGE_ENABLE_OF_LIBFDT)
1641 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
1642
1643 if (IMAGE_BOOT_GET_CMDLINE) {
1644 ret = boot_get_cmdline(lmb, &images->cmdline_start,
1645 &images->cmdline_end);
1646 if (ret) {
1647 puts("ERROR with allocation of cmdline\n");
1648 return ret;
1649 }
1650 }
Simon Glass13d06982013-05-08 08:06:01 +00001651
1652 if (IMAGE_ENABLE_OF_LIBFDT) {
1653 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
1654 if (ret)
1655 return ret;
1656 }
1657
1658 if (IMAGE_ENABLE_OF_LIBFDT && of_size) {
1659 ret = image_setup_libfdt(images, *of_flat_tree, of_size, lmb);
1660 if (ret)
1661 return ret;
1662 }
1663
1664 return 0;
1665}
1666#endif /* CONFIG_LMB */
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001667#endif /* !USE_HOSTCC */