blob: 992ebbad51ac110e4e9f8cfbb0b5d7c27bdc7b4c [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 Glass7b51b572019-08-01 09:46:52 -060011#include <env.h>
Simon Glass3db71102019-11-14 12:57:16 -070012#include <u-boot/crc.h>
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +010013#include <watchdog.h>
14
15#ifdef CONFIG_SHOW_BOOT_PROGRESS
16#include <status_led.h>
17#endif
18
Marian Balakowicz2242f532008-02-21 17:27:41 +010019#include <rtc.h>
Marian Balakowicz2242f532008-02-21 17:27:41 +010020
Simon Glass0c670fc2019-08-01 09:46:36 -060021#include <gzip.h>
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010022#include <image.h>
Joe Hershberger0eb25b62015-03-22 17:08:59 -050023#include <mapmem.h>
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010024
Simon Glassaa34fbc2016-02-22 22:55:45 -070025#if IMAGE_ENABLE_FIT || IMAGE_ENABLE_OF_LIBFDT
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090026#include <linux/libfdt.h>
Marian Balakowiczfff888a12008-02-21 17:20:19 +010027#include <fdt_support.h>
Michal Simek62afc602016-05-17 14:03:50 +020028#include <fpga.h>
29#include <xilinx.h>
Marian Balakowiczc8779642008-03-12 10:12:37 +010030#endif
31
Andy Fleming20a14a42008-04-02 16:19:07 -050032#include <u-boot/md5.h>
Jeroen Hofstee2b9912e2014-06-12 22:27:12 +020033#include <u-boot/sha1.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090034#include <linux/errno.h>
Simon Glass35e7b0f2013-05-07 06:12:03 +000035#include <asm/io.h>
Marian Balakowiczc8779642008-03-12 10:12:37 +010036
Julius Werner20908542019-07-24 19:37:54 -070037#include <bzlib.h>
38#include <linux/lzo.h>
39#include <lzma/LzmaTypes.h>
40#include <lzma/LzmaDec.h>
41#include <lzma/LzmaTools.h>
42
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +010043#ifdef CONFIG_CMD_BDI
Wolfgang Denk54841ab2010-06-28 22:00:46 +020044extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +010045#endif
46
47DECLARE_GLOBAL_DATA_PTR;
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +010048
Tom Rinic76c93a2019-05-23 07:14:07 -040049#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Stephen Warren712fbcf2011-10-18 11:11:49 +000050static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
Marian Balakowiczd985c842008-03-12 10:14:38 +010051 int verify);
Heiko Schocher21d29f72014-05-28 11:33:33 +020052#endif
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010053#else
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +010054#include "mkimage.h"
Andy Fleming20a14a42008-04-02 16:19:07 -050055#include <u-boot/md5.h>
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010056#include <time.h>
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010057#include <image.h>
Heiko Schocher80402f32015-06-29 09:10:46 +020058
59#ifndef __maybe_unused
60# define __maybe_unused /* unimplemented */
61#endif
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010062#endif /* !USE_HOSTCC*/
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010063
Simon Glass0ccff502013-02-24 17:33:25 +000064#include <u-boot/crc.h>
Breno Matheus Lima5b20d142019-09-23 18:39:47 +000065#include <imximage.h>
Simon Glass0ccff502013-02-24 17:33:25 +000066
Simon Glass13d06982013-05-08 08:06:01 +000067#ifndef CONFIG_SYS_BARGSIZE
68#define CONFIG_SYS_BARGSIZE 512
69#endif
70
Mike Frysinger7edb1862010-10-20 07:17:39 -040071static const table_entry_t uimage_arch[] = {
Simon Glass30495bf2016-06-30 10:52:15 -060072 { IH_ARCH_INVALID, "invalid", "Invalid ARCH", },
Marian Balakowicz570abb02008-02-29 15:59:59 +010073 { 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", },
Marian Balakowicz570abb02008-02-29 15:59:59 +010081 { IH_ARCH_NIOS2, "nios2", "NIOS II", },
Grant Ericksone419e122008-05-04 16:45:01 -070082 { IH_ARCH_PPC, "powerpc", "PowerPC", },
Marian Balakowicz570abb02008-02-29 15:59:59 +010083 { 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", },
Macpaul Lin64d61462011-10-19 20:41:09 +000090 { IH_ARCH_NDS32, "nds32", "NDS32", },
Stefan Kristiansson3ddcacc2011-11-26 19:04:50 +000091 { IH_ARCH_OPENRISC, "or1k", "OpenRISC 1000",},
Simon Glass35e7b0f2013-05-07 06:12:03 +000092 { IH_ARCH_SANDBOX, "sandbox", "Sandbox", },
David Feng0ae76532013-12-14 11:47:35 +080093 { IH_ARCH_ARM64, "arm64", "AArch64", },
Alexey Brodkinbc5d5422014-02-04 12:56:16 +040094 { IH_ARCH_ARC, "arc", "ARC", },
Simon Glass5bda35c2014-10-10 08:21:57 -060095 { IH_ARCH_X86_64, "x86_64", "AMD x86_64", },
Chris Zankelde5e5ce2016-08-10 18:36:43 +030096 { IH_ARCH_XTENSA, "xtensa", "Xtensa", },
Rick Chen86aa65a2018-03-13 13:37:29 +080097 { IH_ARCH_RISCV, "riscv", "RISC-V", },
Marian Balakowicz570abb02008-02-29 15:59:59 +010098 { -1, "", "", },
99};
100
Mike Frysinger7edb1862010-10-20 07:17:39 -0400101static const table_entry_t uimage_os[] = {
Simon Glass30495bf2016-06-30 10:52:15 -0600102 { IH_OS_INVALID, "invalid", "Invalid OS", },
Philipp Tomsich4914af12017-09-13 21:29:29 +0200103 { IH_OS_ARM_TRUSTED_FIRMWARE, "arm-trusted-firmware", "ARM Trusted Firmware" },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100104 { IH_OS_LINUX, "linux", "Linux", },
105#if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
106 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
107#endif
108 { IH_OS_NETBSD, "netbsd", "NetBSD", },
Torkel Lundgren3df61952010-09-28 11:05:36 +0200109 { IH_OS_OSE, "ose", "Enea OSE", },
Steven Stallion04d41402013-03-20 06:31:35 +0000110 { IH_OS_PLAN9, "plan9", "Plan 9", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100111 { IH_OS_RTEMS, "rtems", "RTEMS", },
Bryan O'Donoghue45b55712018-03-13 16:50:35 +0000112 { IH_OS_TEE, "tee", "Trusted Execution Environment" },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100113 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
miao.yan@windriver.com68b15e82013-12-27 16:01:50 +0800114 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100115#if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
116 { IH_OS_QNX, "qnx", "QNX", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100117#endif
Peter Tyserf5ed9e32008-09-08 14:56:49 -0500118#if defined(CONFIG_INTEGRITY) || defined(USE_HOSTCC)
119 { IH_OS_INTEGRITY,"integrity", "INTEGRITY", },
120#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100121#ifdef USE_HOSTCC
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", },
133#endif
Marek Vasut67ddd952014-12-16 14:07:21 +0100134#if defined(CONFIG_BOOTM_OPENRTOS) || defined(USE_HOSTCC)
135 { IH_OS_OPENRTOS, "openrtos", "OpenRTOS", },
136#endif
Lukas Auer5e30e452019-08-21 21:14:44 +0200137 { IH_OS_OPENSBI, "opensbi", "RISC-V OpenSBI", },
Marek Vasut67ddd952014-12-16 14:07:21 +0100138
Marian Balakowicz570abb02008-02-29 15:59:59 +0100139 { -1, "", "", },
140};
141
Mike Frysinger7edb1862010-10-20 07:17:39 -0400142static const table_entry_t uimage_type[] = {
Stefano Babic4962e382011-10-17 00:07:43 +0000143 { IH_TYPE_AISIMAGE, "aisimage", "Davinci AIS image",},
Marian Balakowicz570abb02008-02-29 15:59:59 +0100144 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
145 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
John Rigby3decb142011-07-21 09:10:30 -0400146 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
Karicheri, Muralidharanbf411ea2014-04-04 13:16:48 -0400147 { IH_TYPE_GPIMAGE, "gpimage", "TI Keystone SPL Image",},
Marian Balakowicz570abb02008-02-29 15:59:59 +0100148 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
Stephen Warrenb9b50e82011-11-10 13:17:53 -0700149 { IH_TYPE_KERNEL_NOLOAD, "kernel_noload", "Kernel Image (no loading done)", },
Stefano Babic4962e382011-10-17 00:07:43 +0000150 { IH_TYPE_KWBIMAGE, "kwbimage", "Kirkwood Boot Image",},
151 { IH_TYPE_IMXIMAGE, "imximage", "Freescale i.MX Boot Image",},
Peng Fana2b96ec2018-10-16 04:50:30 +0000152 { IH_TYPE_IMX8IMAGE, "imx8image", "NXP i.MX8 Boot Image",},
Peng Fan6609c262018-11-20 10:19:36 +0000153 { IH_TYPE_IMX8MIMAGE, "imx8mimage", "NXP i.MX8M Boot Image",},
Simon Glass30495bf2016-06-30 10:52:15 -0600154 { IH_TYPE_INVALID, "invalid", "Invalid Image", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100155 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
Stefano Babic4962e382011-10-17 00:07:43 +0000156 { IH_TYPE_OMAPIMAGE, "omapimage", "TI OMAP SPL With GP CH",},
Shaohui Xie5d898a02012-08-10 02:49:35 +0000157 { IH_TYPE_PBLIMAGE, "pblimage", "Freescale PBL Boot Image",},
Marian Balakowicz570abb02008-02-29 15:59:59 +0100158 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
159 { IH_TYPE_SCRIPT, "script", "Script", },
Marek Vasut662abc42018-04-15 13:15:33 +0200160 { IH_TYPE_SOCFPGAIMAGE, "socfpgaimage", "Altera SoCFPGA CV/AV preloader",},
161 { IH_TYPE_SOCFPGAIMAGE_V1, "socfpgaimage_v1", "Altera SoCFPGA A10 preloader",},
Marian Balakowicz570abb02008-02-29 15:59:59 +0100162 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
Heiko Schocher7816f2c2011-07-16 00:06:42 +0000163 { IH_TYPE_UBLIMAGE, "ublimage", "Davinci UBL image",},
Marek Vasutbce88372013-08-26 20:43:33 +0200164 { IH_TYPE_MXSIMAGE, "mxsimage", "Freescale MXS Boot Image",},
Andreas Bießmann7b1a4112014-05-19 14:23:39 +0200165 { IH_TYPE_ATMELIMAGE, "atmelimage", "ATMEL ROM-Boot Image",},
Simon Glass90268b82014-10-19 21:11:24 -0600166 { IH_TYPE_X86_SETUP, "x86_setup", "x86 setup.bin", },
Albert ARIBAUD \(3ADEV\)39f520b2015-03-31 11:40:49 +0200167 { IH_TYPE_LPC32XXIMAGE, "lpc32xximage", "LPC32XX Boot Image", },
Simon Glassa131c1f2015-08-30 16:55:24 -0600168 { IH_TYPE_RKIMAGE, "rkimage", "Rockchip Boot Image" },
Simon Glassf9a3c272015-08-30 16:55:25 -0600169 { IH_TYPE_RKSD, "rksd", "Rockchip SD Boot Image" },
Simon Glass10b84fe2015-08-30 16:55:26 -0600170 { IH_TYPE_RKSPI, "rkspi", "Rockchip SPI Boot Image" },
Albert ARIBAUD \(3ADEV\)ed0c2c02016-09-26 09:08:06 +0200171 { IH_TYPE_VYBRIDIMAGE, "vybridimage", "Vybrid Boot Image", },
Nathan Rossi66eef1e2015-11-17 22:56:56 +1000172 { IH_TYPE_ZYNQIMAGE, "zynqimage", "Xilinx Zynq Boot Image" },
Michal Simekd9b58b32016-04-27 14:03:29 +0200173 { IH_TYPE_ZYNQMPIMAGE, "zynqmpimage", "Xilinx ZynqMP Boot Image" },
Alexander Graf6915dcf2018-04-13 14:18:52 +0200174 { IH_TYPE_ZYNQMPBIF, "zynqmpbif", "Xilinx ZynqMP Boot Image (bif)" },
Michal Simeked0cea72016-05-17 13:58:44 +0200175 { IH_TYPE_FPGA, "fpga", "FPGA Image" },
Andrew F. Davis7e719ee2016-11-29 16:33:21 -0600176 { IH_TYPE_TEE, "tee", "Trusted Execution Environment Image",},
Sven Ebenfeldd21bd692016-11-06 16:37:56 +0100177 { IH_TYPE_FIRMWARE_IVT, "firmware_ivt", "Firmware with HABv4 IVT" },
Andrew F. Davis6442c962017-07-31 10:58:20 -0500178 { IH_TYPE_PMMC, "pmmc", "TI Power Management Micro-Controller Firmware",},
Patrick Delaunay81260e32018-03-12 10:46:04 +0100179 { IH_TYPE_STM32IMAGE, "stm32image", "STMicroelectronics STM32 Image" },
Ryder Lee3b975a12018-11-15 10:07:49 +0800180 { IH_TYPE_MTKIMAGE, "mtk_image", "MediaTek BootROM loadable Image" },
Patrick Delaunaye7fabe72019-08-02 15:07:19 +0200181 { IH_TYPE_COPRO, "copro", "Coprocessor Image"},
Marian Balakowicz570abb02008-02-29 15:59:59 +0100182 { -1, "", "", },
183};
184
Mike Frysinger7edb1862010-10-20 07:17:39 -0400185static const table_entry_t uimage_comp[] = {
Marian Balakowicz570abb02008-02-29 15:59:59 +0100186 { IH_COMP_NONE, "none", "uncompressed", },
187 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
188 { IH_COMP_GZIP, "gzip", "gzip compressed", },
Luigi 'Comio' Mantellinifc9c1722008-09-08 02:46:13 +0200189 { IH_COMP_LZMA, "lzma", "lzma compressed", },
Peter Korsgaard20dde482009-11-19 11:37:51 +0100190 { IH_COMP_LZO, "lzo", "lzo compressed", },
Julius Werner027b7282015-10-06 20:03:53 -0700191 { IH_COMP_LZ4, "lz4", "lz4 compressed", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100192 { -1, "", "", },
193};
194
Simon Glass56d7ab72016-06-30 10:52:14 -0600195struct table_info {
196 const char *desc;
197 int count;
198 const table_entry_t *table;
199};
200
201static 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 },
206};
207
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100208/*****************************************************************************/
209/* Legacy format routines */
210/*****************************************************************************/
Stephen Warren712fbcf2011-10-18 11:11:49 +0000211int image_check_hcrc(const image_header_t *hdr)
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100212{
213 ulong hcrc;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000214 ulong len = image_get_header_size();
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100215 image_header_t header;
216
217 /* Copy header so we can blank CRC field for re-calculation */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000218 memmove(&header, (char *)hdr, image_get_header_size());
219 image_set_hcrc(&header, 0);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100220
Stephen Warren712fbcf2011-10-18 11:11:49 +0000221 hcrc = crc32(0, (unsigned char *)&header, len);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100222
Stephen Warren712fbcf2011-10-18 11:11:49 +0000223 return (hcrc == image_get_hcrc(hdr));
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100224}
225
Stephen Warren712fbcf2011-10-18 11:11:49 +0000226int image_check_dcrc(const image_header_t *hdr)
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100227{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000228 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);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100231
Stephen Warren712fbcf2011-10-18 11:11:49 +0000232 return (dcrc == image_get_dcrc(hdr));
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100233}
234
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100235/**
236 * image_multi_count - get component (sub-image) count
237 * @hdr: pointer to the header of the multi component image
238 *
239 * image_multi_count() returns number of components in a multi
240 * component image.
241 *
242 * Note: no checking of the image type is done, caller must pass
243 * a valid multi component image.
244 *
245 * returns:
246 * number of components
247 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000248ulong image_multi_count(const image_header_t *hdr)
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100249{
250 ulong i, count = 0;
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100251 uint32_t *size;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100252
253 /* get start of the image payload, which in case of multi
254 * component images that points to a table of component sizes */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000255 size = (uint32_t *)image_get_data(hdr);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100256
257 /* count non empty slots */
258 for (i = 0; size[i]; ++i)
259 count++;
260
261 return count;
262}
263
264/**
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
270 *
271 * image_multi_getimg() returns size and data address for the requested
272 * component in a multi component image.
273 *
274 * Note: no checking of the image type is done, caller must pass
275 * a valid multi component image.
276 *
277 * returns:
278 * data address and size of the component, if idx is valid
279 * 0 in data and len, if idx is out of range
280 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000281void image_multi_getimg(const image_header_t *hdr, ulong idx,
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100282 ulong *data, ulong *len)
283{
284 int i;
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100285 uint32_t *size;
Nick Spence02b9b222008-05-10 14:02:04 -0700286 ulong offset, count, img_data;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100287
288 /* get number of component */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000289 count = image_multi_count(hdr);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100290
291 /* get start of the image payload, which in case of multi
292 * component images that points to a table of component sizes */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000293 size = (uint32_t *)image_get_data(hdr);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100294
295 /* get address of the proper component data start, which means
296 * skipping sizes table (add 1 for last, null entry) */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000297 img_data = image_get_data(hdr) + (count + 1) * sizeof(uint32_t);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100298
299 if (idx < count) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000300 *len = uimage_to_cpu(size[idx]);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100301 offset = 0;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100302
303 /* go over all indices preceding requested component idx */
304 for (i = 0; i < idx; i++) {
Nick Spence02b9b222008-05-10 14:02:04 -0700305 /* add up i-th component size, rounding up to 4 bytes */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000306 offset += (uimage_to_cpu(size[i]) + 3) & ~3 ;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100307 }
308
309 /* calculate idx-th component data address */
Nick Spence02b9b222008-05-10 14:02:04 -0700310 *data = img_data + offset;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100311 } else {
312 *len = 0;
313 *data = 0;
314 }
315}
Marian Balakowicz42b73e82008-01-31 13:20:07 +0100316
Stephen Warren712fbcf2011-10-18 11:11:49 +0000317static void image_print_type(const image_header_t *hdr)
Marian Balakowicz2242f532008-02-21 17:27:41 +0100318{
Heiko Schocher80402f32015-06-29 09:10:46 +0200319 const char __maybe_unused *os, *arch, *type, *comp;
Marian Balakowicz2242f532008-02-21 17:27:41 +0100320
Stephen Warren712fbcf2011-10-18 11:11:49 +0000321 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));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100325
Stephen Warren712fbcf2011-10-18 11:11:49 +0000326 printf("%s %s %s (%s)\n", arch, os, type, comp);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100327}
328
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100329/**
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200330 * image_print_contents - prints out the contents of the legacy format image
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200331 * @ptr: pointer to the legacy format image header
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100332 * @p: pointer to prefix string
333 *
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200334 * image_print_contents() formats a multi line legacy image contents description.
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100335 * The routine prints out all header fields followed by the size/offset data
336 * for MULTI/SCRIPT images.
337 *
338 * returns:
339 * no returned results
340 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000341void image_print_contents(const void *ptr)
Marian Balakowicz2242f532008-02-21 17:27:41 +0100342{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200343 const image_header_t *hdr = (const image_header_t *)ptr;
Heiko Schocher80402f32015-06-29 09:10:46 +0200344 const char __maybe_unused *p;
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200345
Simon Glass1fe7d932013-05-08 08:05:58 +0000346 p = IMAGE_INDENT_STRING;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000347 printf("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name(hdr));
Simon Glass859e92b2013-05-07 06:11:51 +0000348 if (IMAGE_ENABLE_TIMESTAMP) {
349 printf("%sCreated: ", p);
350 genimg_print_time((time_t)image_get_time(hdr));
351 }
Stephen Warren712fbcf2011-10-18 11:11:49 +0000352 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));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100358
Stephen Warren712fbcf2011-10-18 11:11:49 +0000359 if (image_check_type(hdr, IH_TYPE_MULTI) ||
360 image_check_type(hdr, IH_TYPE_SCRIPT)) {
Marian Balakowicz2242f532008-02-21 17:27:41 +0100361 int i;
362 ulong data, len;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000363 ulong count = image_multi_count(hdr);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100364
Stephen Warren712fbcf2011-10-18 11:11:49 +0000365 printf("%sContents:\n", p);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100366 for (i = 0; i < count; i++) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000367 image_multi_getimg(hdr, i, &data, &len);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100368
Stephen Warren712fbcf2011-10-18 11:11:49 +0000369 printf("%s Image %d: ", p, i);
370 genimg_print_size(len);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100371
Stephen Warren712fbcf2011-10-18 11:11:49 +0000372 if (image_check_type(hdr, IH_TYPE_SCRIPT) && i > 0) {
Marian Balakowicz570abb02008-02-29 15:59:59 +0100373 /*
374 * the user may need to know offsets
375 * if planning to do something with
376 * multiple files
377 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000378 printf("%s Offset = 0x%08lx\n", p, data);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100379 }
Marian Balakowicz2242f532008-02-21 17:27:41 +0100380 }
Sven Ebenfeldd21bd692016-11-06 16:37:56 +0100381 } else if (image_check_type(hdr, IH_TYPE_FIRMWARE_IVT)) {
382 printf("HAB Blocks: 0x%08x 0x0000 0x%08x\n",
Breno Matheus Lima5b20d142019-09-23 18:39:47 +0000383 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));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100386 }
387}
388
Julius Werner20908542019-07-24 19:37:54 -0700389/**
390 * print_decomp_msg() - Print a suitable decompression/loading message
391 *
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
395 */
396static void print_decomp_msg(int comp_type, int type, bool is_xip)
397{
398 const char *name = genimg_get_type_name(type);
399
400 if (comp_type == IH_COMP_NONE)
401 printf(" %s %s\n", is_xip ? "XIP" : "Loading", name);
402 else
403 printf(" Uncompressing %s\n", name);
404}
405
406int 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)
409{
410 int ret = 0;
411
412 *load_end = load;
413 print_decomp_msg(comp, type, load == image_start);
414
415 /*
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.
419 */
420 switch (comp) {
421 case IH_COMP_NONE:
422 if (load == image_start)
423 break;
424 if (image_len <= unc_len)
425 memmove_wd(load_buf, image_buf, image_len, CHUNKSZ);
426 else
427 ret = -ENOSPC;
428 break;
429#ifdef CONFIG_GZIP
430 case IH_COMP_GZIP: {
431 ret = gunzip(load_buf, unc_len, image_buf, &image_len);
432 break;
433 }
434#endif /* CONFIG_GZIP */
435#ifdef CONFIG_BZIP2
436 case IH_COMP_BZIP2: {
437 uint size = unc_len;
438
439 /*
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.
443 */
444 ret = BZ2_bzBuffToBuffDecompress(load_buf, &size,
445 image_buf, image_len,
446 CONFIG_SYS_MALLOC_LEN < (4096 * 1024), 0);
447 image_len = size;
448 break;
449 }
450#endif /* CONFIG_BZIP2 */
451#ifdef CONFIG_LZMA
452 case IH_COMP_LZMA: {
453 SizeT lzma_len = unc_len;
454
455 ret = lzmaBuffToBuffDecompress(load_buf, &lzma_len,
456 image_buf, image_len);
457 image_len = lzma_len;
458 break;
459 }
460#endif /* CONFIG_LZMA */
461#ifdef CONFIG_LZO
462 case IH_COMP_LZO: {
463 size_t size = unc_len;
464
465 ret = lzop_decompress(image_buf, image_len, load_buf, &size);
466 image_len = size;
467 break;
468 }
469#endif /* CONFIG_LZO */
470#ifdef CONFIG_LZ4
471 case IH_COMP_LZ4: {
472 size_t size = unc_len;
473
474 ret = ulz4fn(image_buf, image_len, load_buf, &size);
475 image_len = size;
476 break;
477 }
478#endif /* CONFIG_LZ4 */
479 default:
480 printf("Unimplemented compression type %d\n", comp);
481 return -ENOSYS;
482 }
483
484 *load_end = load + image_len;
485
486 return ret;
487}
488
Marian Balakowicz570abb02008-02-29 15:59:59 +0100489
490#ifndef USE_HOSTCC
Tom Rinic76c93a2019-05-23 07:14:07 -0400491#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100492/**
493 * image_get_ramdisk - get and verify ramdisk image
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100494 * @rd_addr: ramdisk image start address
495 * @arch: expected ramdisk architecture
496 * @verify: checksum verification flag
497 *
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
501 * fields checking.
502 *
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100503 * returns:
504 * pointer to a ramdisk image header, if image was found and valid
Kumar Gala274cea22008-02-27 21:51:46 -0600505 * otherwise, return NULL
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100506 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000507static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
Marian Balakowiczd985c842008-03-12 10:14:38 +0100508 int verify)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100509{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200510 const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100511
Stephen Warren712fbcf2011-10-18 11:11:49 +0000512 if (!image_check_magic(rd_hdr)) {
513 puts("Bad Magic Number\n");
Simon Glass770605e2012-02-13 13:51:18 +0000514 bootstage_error(BOOTSTAGE_ID_RD_MAGIC);
Kumar Gala274cea22008-02-27 21:51:46 -0600515 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100516 }
517
Stephen Warren712fbcf2011-10-18 11:11:49 +0000518 if (!image_check_hcrc(rd_hdr)) {
519 puts("Bad Header Checksum\n");
Simon Glass770605e2012-02-13 13:51:18 +0000520 bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
Kumar Gala274cea22008-02-27 21:51:46 -0600521 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100522 }
523
Simon Glass770605e2012-02-13 13:51:18 +0000524 bootstage_mark(BOOTSTAGE_ID_RD_MAGIC);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000525 image_print_contents(rd_hdr);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100526
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100527 if (verify) {
528 puts(" Verifying Checksum ... ");
Stephen Warren712fbcf2011-10-18 11:11:49 +0000529 if (!image_check_dcrc(rd_hdr)) {
530 puts("Bad Data CRC\n");
Simon Glass770605e2012-02-13 13:51:18 +0000531 bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM);
Kumar Gala274cea22008-02-27 21:51:46 -0600532 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100533 }
534 puts("OK\n");
535 }
536
Simon Glass770605e2012-02-13 13:51:18 +0000537 bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100538
Stephen Warren712fbcf2011-10-18 11:11:49 +0000539 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",
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100543 genimg_get_arch_name(arch));
Simon Glass770605e2012-02-13 13:51:18 +0000544 bootstage_error(BOOTSTAGE_ID_RAMDISK);
Kumar Gala274cea22008-02-27 21:51:46 -0600545 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100546 }
547
548 return rd_hdr;
549}
Heiko Schocher21d29f72014-05-28 11:33:33 +0200550#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100551#endif /* !USE_HOSTCC */
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100552
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100553/*****************************************************************************/
554/* Shared dual-format routines */
555/*****************************************************************************/
Marian Balakowicz570abb02008-02-29 15:59:59 +0100556#ifndef USE_HOSTCC
Joe Hershberger1cf0a8b2012-12-11 22:16:28 -0600557ulong load_addr = CONFIG_SYS_LOAD_ADDR; /* Default Load Address */
558ulong save_addr; /* Default Save Address */
559ulong save_size; /* Default Save Size (in bytes) */
560
561static int on_loadaddr(const char *name, const char *value, enum env_op op,
562 int flags)
563{
564 switch (op) {
565 case env_op_create:
566 case env_op_overwrite:
567 load_addr = simple_strtoul(value, NULL, 16);
568 break;
569 default:
570 break;
571 }
572
573 return 0;
574}
575U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
576
Simon Glass723806c2017-08-03 12:22:15 -0600577ulong env_get_bootm_low(void)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100578{
Simon Glass00caae62017-08-03 12:22:12 -0600579 char *s = env_get("bootm_low");
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100580 if (s) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000581 ulong tmp = simple_strtoul(s, NULL, 16);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100582 return tmp;
583 }
584
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200585#if defined(CONFIG_SYS_SDRAM_BASE)
586 return CONFIG_SYS_SDRAM_BASE;
Michal Simeka750ded2019-09-25 09:11:48 +0200587#elif defined(CONFIG_ARM) || defined(CONFIG_MICROBLAZE)
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100588 return gd->bd->bi_dram[0].start;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100589#else
590 return 0;
591#endif
592}
593
Simon Glass723806c2017-08-03 12:22:15 -0600594phys_size_t env_get_bootm_size(void)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100595{
Masahiro Yamada0cb389d2016-02-05 16:12:50 +0900596 phys_size_t tmp, size;
597 phys_addr_t start;
Simon Glass00caae62017-08-03 12:22:12 -0600598 char *s = env_get("bootm_size");
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100599 if (s) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000600 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100601 return tmp;
602 }
Masahiro Yamada0cb389d2016-02-05 16:12:50 +0900603
Michal Simeka750ded2019-09-25 09:11:48 +0200604#if (defined(CONFIG_ARM) || defined(CONFIG_MICROBLAZE)) && \
605 defined(CONFIG_NR_DRAM_BANKS)
Masahiro Yamada0cb389d2016-02-05 16:12:50 +0900606 start = gd->bd->bi_dram[0].start;
607 size = gd->bd->bi_dram[0].size;
608#else
609 start = gd->bd->bi_memstart;
610 size = gd->bd->bi_memsize;
611#endif
612
Simon Glass00caae62017-08-03 12:22:12 -0600613 s = env_get("bootm_low");
Matthew McClintockc519fac2010-07-08 10:11:08 -0500614 if (s)
Stephen Warren712fbcf2011-10-18 11:11:49 +0000615 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
Matthew McClintockc519fac2010-07-08 10:11:08 -0500616 else
Masahiro Yamada0cb389d2016-02-05 16:12:50 +0900617 tmp = start;
Matthew McClintockc519fac2010-07-08 10:11:08 -0500618
Masahiro Yamada0cb389d2016-02-05 16:12:50 +0900619 return size - (tmp - start);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100620}
621
Simon Glass723806c2017-08-03 12:22:15 -0600622phys_size_t env_get_bootm_mapsize(void)
Grant Likelyc3624e62011-03-28 09:58:43 +0000623{
624 phys_size_t tmp;
Simon Glass00caae62017-08-03 12:22:12 -0600625 char *s = env_get("bootm_mapsize");
Grant Likelyc3624e62011-03-28 09:58:43 +0000626 if (s) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000627 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
Grant Likelyc3624e62011-03-28 09:58:43 +0000628 return tmp;
629 }
630
631#if defined(CONFIG_SYS_BOOTMAPSZ)
632 return CONFIG_SYS_BOOTMAPSZ;
633#else
Simon Glass723806c2017-08-03 12:22:15 -0600634 return env_get_bootm_size();
Grant Likelyc3624e62011-03-28 09:58:43 +0000635#endif
636}
637
Stephen Warren712fbcf2011-10-18 11:11:49 +0000638void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100639{
Larry Johnson54fa2c52010-04-20 08:09:43 -0400640 if (to == from)
641 return;
642
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100643#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
Sonic Zhang22cfddc2014-12-10 18:20:53 +0800644 if (to > from) {
645 from += len;
646 to += len;
647 }
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100648 while (len > 0) {
649 size_t tail = (len > chunksz) ? chunksz : len;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000650 WATCHDOG_RESET();
Sonic Zhang22cfddc2014-12-10 18:20:53 +0800651 if (to > from) {
652 to -= tail;
653 from -= tail;
654 }
Stephen Warren712fbcf2011-10-18 11:11:49 +0000655 memmove(to, from, tail);
Sonic Zhang22cfddc2014-12-10 18:20:53 +0800656 if (to < from) {
657 to += tail;
658 from += tail;
659 }
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100660 len -= tail;
661 }
662#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000663 memmove(to, from, len);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100664#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
665}
Julius Werner20908542019-07-24 19:37:54 -0700666#else /* USE_HOSTCC */
667void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
668{
669 memmove(to, from, len);
670}
Marian Balakowicz570abb02008-02-29 15:59:59 +0100671#endif /* !USE_HOSTCC */
672
Stephen Warren712fbcf2011-10-18 11:11:49 +0000673void genimg_print_size(uint32_t size)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100674{
675#ifndef USE_HOSTCC
Stephen Warren712fbcf2011-10-18 11:11:49 +0000676 printf("%d Bytes = ", size);
677 print_size(size, "\n");
Marian Balakowicz570abb02008-02-29 15:59:59 +0100678#else
xypron.glpk@gmx.decec85d42017-01-04 14:04:44 +0100679 printf("%d Bytes = %.2f KiB = %.2f MiB\n",
Marian Balakowicz570abb02008-02-29 15:59:59 +0100680 size, (double)size / 1.024e3,
681 (double)size / 1.048576e6);
682#endif
683}
684
Simon Glass859e92b2013-05-07 06:11:51 +0000685#if IMAGE_ENABLE_TIMESTAMP
686void genimg_print_time(time_t timestamp)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100687{
688#ifndef USE_HOSTCC
689 struct rtc_time tm;
690
Simon Glass9f9276c2015-04-20 12:37:18 -0600691 rtc_to_tm(timestamp, &tm);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000692 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
Marian Balakowicz570abb02008-02-29 15:59:59 +0100693 tm.tm_year, tm.tm_mon, tm.tm_mday,
694 tm.tm_hour, tm.tm_min, tm.tm_sec);
695#else
Stephen Warren712fbcf2011-10-18 11:11:49 +0000696 printf("%s", ctime(&timestamp));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100697#endif
698}
Simon Glass859e92b2013-05-07 06:11:51 +0000699#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100700
Simon Glass5b9d44d2015-06-23 15:38:25 -0600701const table_entry_t *get_table_entry(const table_entry_t *table, int id)
702{
703 for (; table->id >= 0; ++table) {
704 if (table->id == id)
705 return table;
706 }
707 return NULL;
708}
709
Simon Glass14262202016-06-30 10:52:16 -0600710static const char *unknown_msg(enum ih_category category)
711{
Simon Glassae3de0d2016-10-31 10:21:09 -0600712 static const char unknown_str[] = "Unknown ";
Simon Glass14262202016-06-30 10:52:16 -0600713 static char msg[30];
714
Simon Glassae3de0d2016-10-31 10:21:09 -0600715 strcpy(msg, unknown_str);
716 strncat(msg, table_info[category].desc,
717 sizeof(msg) - sizeof(unknown_str));
Simon Glass14262202016-06-30 10:52:16 -0600718
719 return msg;
720}
721
722/**
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
726 *
727 * This will scan the translation table trying to find the entry that matches
728 * the given id.
729 *
730 * @retur long entry name if translation succeeds; error string on failure
731 */
732const char *genimg_get_cat_name(enum ih_category category, uint id)
733{
734 const table_entry_t *entry;
735
736 entry = get_table_entry(table_info[category].table, id);
737 if (!entry)
738 return unknown_msg(category);
739#if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
740 return entry->lname;
741#else
742 return entry->lname + gd->reloc_off;
743#endif
744}
745
746/**
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
750 *
751 * This will scan the translation table trying to find the entry that matches
752 * the given id.
753 *
754 * @retur short entry name if translation succeeds; error string on failure
755 */
756const char *genimg_get_cat_short_name(enum ih_category category, uint id)
757{
758 const table_entry_t *entry;
759
760 entry = get_table_entry(table_info[category].table, id);
761 if (!entry)
762 return unknown_msg(category);
763#if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
764 return entry->sname;
765#else
766 return entry->sname + gd->reloc_off;
767#endif
768}
769
770int genimg_get_cat_count(enum ih_category category)
771{
772 return table_info[category].count;
773}
774
775const char *genimg_get_cat_desc(enum ih_category category)
776{
777 return table_info[category].desc;
778}
779
Marian Balakowicz570abb02008-02-29 15:59:59 +0100780/**
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
785 *
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.
789 *
790 * returns:
791 * long entry name if translation succeeds
792 * msg otherwise
793 */
Mike Frysinger7edb1862010-10-20 07:17:39 -0400794char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100795{
Simon Glass5b9d44d2015-06-23 15:38:25 -0600796 table = get_table_entry(table, id);
797 if (!table)
798 return msg;
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200799#if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
Simon Glass5b9d44d2015-06-23 15:38:25 -0600800 return table->lname;
Scott Woode3d1ac72009-04-02 16:15:10 -0500801#else
Simon Glass5b9d44d2015-06-23 15:38:25 -0600802 return table->lname + gd->reloc_off;
Scott Woode3d1ac72009-04-02 16:15:10 -0500803#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100804}
805
Stephen Warren712fbcf2011-10-18 11:11:49 +0000806const char *genimg_get_os_name(uint8_t os)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100807{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000808 return (get_table_entry_name(uimage_os, "Unknown OS", os));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100809}
810
Stephen Warren712fbcf2011-10-18 11:11:49 +0000811const char *genimg_get_arch_name(uint8_t arch)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100812{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000813 return (get_table_entry_name(uimage_arch, "Unknown Architecture",
814 arch));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100815}
816
Stephen Warren712fbcf2011-10-18 11:11:49 +0000817const char *genimg_get_type_name(uint8_t type)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100818{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000819 return (get_table_entry_name(uimage_type, "Unknown Image", type));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100820}
821
Simon Glasscef2e512016-02-22 22:55:50 -0700822static const char *genimg_get_short_name(const table_entry_t *table, int val)
Simon Glass5b9d44d2015-06-23 15:38:25 -0600823{
Simon Glasscef2e512016-02-22 22:55:50 -0700824 table = get_table_entry(table, val);
Simon Glass5b9d44d2015-06-23 15:38:25 -0600825 if (!table)
826 return "unknown";
827#if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
828 return table->sname;
829#else
830 return table->sname + gd->reloc_off;
831#endif
832}
833
Simon Glasscef2e512016-02-22 22:55:50 -0700834const char *genimg_get_type_short_name(uint8_t type)
835{
836 return genimg_get_short_name(uimage_type, type);
837}
838
Stephen Warren712fbcf2011-10-18 11:11:49 +0000839const char *genimg_get_comp_name(uint8_t comp)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100840{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000841 return (get_table_entry_name(uimage_comp, "Unknown Compression",
842 comp));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100843}
844
Simon Glasscef2e512016-02-22 22:55:50 -0700845const char *genimg_get_comp_short_name(uint8_t comp)
846{
847 return genimg_get_short_name(uimage_comp, comp);
848}
849
850const char *genimg_get_os_short_name(uint8_t os)
851{
852 return genimg_get_short_name(uimage_os, os);
853}
854
855const char *genimg_get_arch_short_name(uint8_t arch)
856{
857 return genimg_get_short_name(uimage_arch, arch);
858}
859
Marian Balakowicz570abb02008-02-29 15:59:59 +0100860/**
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
865 *
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.
869 *
870 * returns:
871 * entry id if translation succeeds
872 * -1 otherwise
873 */
Mike Frysinger7edb1862010-10-20 07:17:39 -0400874int get_table_entry_id(const table_entry_t *table,
Marian Balakowicz570abb02008-02-29 15:59:59 +0100875 const char *table_name, const char *name)
876{
Mike Frysinger7edb1862010-10-20 07:17:39 -0400877 const table_entry_t *t;
Marian Balakowicz570abb02008-02-29 15:59:59 +0100878
879 for (t = table; t->id >= 0; ++t) {
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200880#ifdef CONFIG_NEEDS_MANUAL_RELOC
Simon Glass5b9d44d2015-06-23 15:38:25 -0600881 if (t->sname && strcasecmp(t->sname + gd->reloc_off, name) == 0)
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200882#else
Simon Glass5b9d44d2015-06-23 15:38:25 -0600883 if (t->sname && strcasecmp(t->sname, name) == 0)
Peter Tyser521af042009-09-21 11:20:36 -0500884#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100885 return (t->id);
886 }
Stephen Warren712fbcf2011-10-18 11:11:49 +0000887 debug("Invalid %s Type: %s\n", table_name, name);
Simon Glass5b9d44d2015-06-23 15:38:25 -0600888
889 return -1;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100890}
891
Stephen Warren712fbcf2011-10-18 11:11:49 +0000892int genimg_get_os_id(const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100893{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000894 return (get_table_entry_id(uimage_os, "OS", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100895}
896
Stephen Warren712fbcf2011-10-18 11:11:49 +0000897int genimg_get_arch_id(const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100898{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000899 return (get_table_entry_id(uimage_arch, "CPU", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100900}
901
Stephen Warren712fbcf2011-10-18 11:11:49 +0000902int genimg_get_type_id(const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100903{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000904 return (get_table_entry_id(uimage_type, "Image", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100905}
906
Stephen Warren712fbcf2011-10-18 11:11:49 +0000907int genimg_get_comp_id(const char *name)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100908{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000909 return (get_table_entry_id(uimage_comp, "Compression", name));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100910}
911
912#ifndef USE_HOSTCC
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100913/**
Bryan Wu6c454fe2014-08-15 16:51:38 -0700914 * genimg_get_kernel_addr_fit - get the real kernel address and return 2
915 * FIT strings
Bryan Wu0f641402014-07-31 17:39:58 -0700916 * @img_addr: a string might contain real image address
Bryan Wu6c454fe2014-08-15 16:51:38 -0700917 * @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
920 * name
Bryan Wu0f641402014-07-31 17:39:58 -0700921 *
Bryan Wu6c454fe2014-08-15 16:51:38 -0700922 * genimg_get_kernel_addr_fit get the real kernel start address from a string
Bryan Wu0f641402014-07-31 17:39:58 -0700923 * which is normally the first argv of bootm/bootz
924 *
925 * returns:
926 * kernel start address
927 */
Bryan Wu6c454fe2014-08-15 16:51:38 -0700928ulong genimg_get_kernel_addr_fit(char * const img_addr,
929 const char **fit_uname_config,
930 const char **fit_uname_kernel)
Bryan Wu0f641402014-07-31 17:39:58 -0700931{
Bryan Wu0f641402014-07-31 17:39:58 -0700932 ulong kernel_addr;
933
934 /* find out kernel image address */
935 if (!img_addr) {
936 kernel_addr = load_addr;
937 debug("* kernel: default image load address = 0x%08lx\n",
938 load_addr);
Simon Glass73223f02016-02-22 22:55:43 -0700939#if CONFIG_IS_ENABLED(FIT)
Bryan Wu0f641402014-07-31 17:39:58 -0700940 } else if (fit_parse_conf(img_addr, load_addr, &kernel_addr,
Bryan Wu6c454fe2014-08-15 16:51:38 -0700941 fit_uname_config)) {
Bryan Wu0f641402014-07-31 17:39:58 -0700942 debug("* kernel: config '%s' from image at 0x%08lx\n",
Bryan Wu6c454fe2014-08-15 16:51:38 -0700943 *fit_uname_config, kernel_addr);
Bryan Wu0f641402014-07-31 17:39:58 -0700944 } else if (fit_parse_subimage(img_addr, load_addr, &kernel_addr,
Bryan Wu6c454fe2014-08-15 16:51:38 -0700945 fit_uname_kernel)) {
Bryan Wu0f641402014-07-31 17:39:58 -0700946 debug("* kernel: subimage '%s' from image at 0x%08lx\n",
Bryan Wu6c454fe2014-08-15 16:51:38 -0700947 *fit_uname_kernel, kernel_addr);
Bryan Wu0f641402014-07-31 17:39:58 -0700948#endif
949 } else {
950 kernel_addr = simple_strtoul(img_addr, NULL, 16);
951 debug("* kernel: cmdline image address = 0x%08lx\n",
952 kernel_addr);
953 }
954
955 return kernel_addr;
956}
957
958/**
Bryan Wu6c454fe2014-08-15 16:51:38 -0700959 * genimg_get_kernel_addr() is the simple version of
960 * genimg_get_kernel_addr_fit(). It ignores those return FIT strings
961 */
962ulong genimg_get_kernel_addr(char * const img_addr)
963{
964 const char *fit_uname_config = NULL;
965 const char *fit_uname_kernel = NULL;
966
967 return genimg_get_kernel_addr_fit(img_addr, &fit_uname_config,
968 &fit_uname_kernel);
969}
970
971/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100972 * genimg_get_format - get image format type
973 * @img_addr: image start address
974 *
975 * genimg_get_format() checks whether provided address points to a valid
976 * legacy or FIT image.
977 *
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.
981 *
982 * returns:
983 * image format type or IMAGE_FORMAT_INVALID if no image is present
984 */
Simon Glass35e7b0f2013-05-07 06:12:03 +0000985int genimg_get_format(const void *img_addr)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100986{
Tom Rinic76c93a2019-05-23 07:14:07 -0400987#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200988 const image_header_t *hdr;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100989
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200990 hdr = (const image_header_t *)img_addr;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100991 if (image_check_magic(hdr))
Heiko Schocher21d29f72014-05-28 11:33:33 +0200992 return IMAGE_FORMAT_LEGACY;
993#endif
Simon Glassaa34fbc2016-02-22 22:55:45 -0700994#if IMAGE_ENABLE_FIT || IMAGE_ENABLE_OF_LIBFDT
Heiko Schocher21d29f72014-05-28 11:33:33 +0200995 if (fdt_check_header(img_addr) == 0)
996 return IMAGE_FORMAT_FIT;
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500997#endif
998#ifdef CONFIG_ANDROID_BOOT_IMAGE
Heiko Schocher21d29f72014-05-28 11:33:33 +0200999 if (android_image_check_header(img_addr) == 0)
1000 return IMAGE_FORMAT_ANDROID;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001001#endif
1002
Heiko Schocher21d29f72014-05-28 11:33:33 +02001003 return IMAGE_FORMAT_INVALID;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001004}
1005
1006/**
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001007 * fit_has_config - check if there is a valid FIT configuration
1008 * @images: pointer to the bootm command headers structure
1009 *
1010 * fit_has_config() checks if there is a FIT configuration in use
1011 * (if FTI support is present).
1012 *
1013 * returns:
1014 * 0, no FIT support or no configuration found
1015 * 1, configuration found
1016 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001017int genimg_has_config(bootm_headers_t *images)
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001018{
Simon Glass73223f02016-02-22 22:55:43 -07001019#if IMAGE_ENABLE_FIT
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001020 if (images->fit_uname_cfg)
1021 return 1;
1022#endif
1023 return 0;
1024}
1025
1026/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001027 * boot_get_ramdisk - main ramdisk handling routine
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001028 * @argc: command argument count
1029 * @argv: command argument list
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +01001030 * @images: pointer to the bootm images structure
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001031 * @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
1034 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001035 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001036 * Curently supported are the following ramdisk sources:
1037 * - multicomponent kernel/ramdisk image,
1038 * - commandline provided address of decicated ramdisk image.
1039 *
1040 * returns:
Marian Balakowiczd985c842008-03-12 10:14:38 +01001041 * 0, if ramdisk image was found and valid, or skiped
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001042 * rd_start and rd_end are set to ramdisk start/end addresses if
1043 * ramdisk image is found and valid
Marian Balakowiczd985c842008-03-12 10:14:38 +01001044 *
Kumar Galaea86b9e2008-08-29 19:08:29 -05001045 * 1, if ramdisk image is found but corrupted, or invalid
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001046 * rd_start and rd_end are set to 0 if no ramdisk exists
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001047 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001048int boot_get_ramdisk(int argc, char * const argv[], bootm_headers_t *images,
Marian Balakowiczd985c842008-03-12 10:14:38 +01001049 uint8_t arch, ulong *rd_start, ulong *rd_end)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001050{
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001051 ulong rd_addr, rd_load;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001052 ulong rd_data, rd_len;
Tom Rinic76c93a2019-05-23 07:14:07 -04001053#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Wolfgang Denk3a2003f2009-08-19 11:42:56 +02001054 const image_header_t *rd_hdr;
Heiko Schocher21d29f72014-05-28 11:33:33 +02001055#endif
Simon Glass35e7b0f2013-05-07 06:12:03 +00001056 void *buf;
Marek Vasut57d40ab2012-03-30 23:19:10 +02001057#ifdef CONFIG_SUPPORT_RAW_INITRD
Marek Vasut017e1f32012-03-18 11:47:58 +00001058 char *end;
Marek Vasut57d40ab2012-03-30 23:19:10 +02001059#endif
Simon Glass73223f02016-02-22 22:55:43 -07001060#if IMAGE_ENABLE_FIT
Simon Glassf320a4d2013-07-10 23:08:10 -07001061 const char *fit_uname_config = images->fit_uname_cfg;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001062 const char *fit_uname_ramdisk = NULL;
1063 ulong default_addr;
Marian Balakowiczc8779642008-03-12 10:12:37 +01001064 int rd_noffset;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001065#endif
Simon Glass983c72f2013-06-11 11:14:46 -07001066 const char *select = NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001067
Marian Balakowiczc8779642008-03-12 10:12:37 +01001068 *rd_start = 0;
1069 *rd_end = 0;
1070
Tom Rini1fec3c52015-08-27 15:42:41 -04001071#ifdef CONFIG_ANDROID_BOOT_IMAGE
1072 /*
1073 * Look for an Android boot image.
1074 */
1075 buf = map_sysmem(images->os.start, 0);
Tom Rinic139b5f2015-10-27 19:04:40 -04001076 if (buf && genimg_get_format(buf) == IMAGE_FORMAT_ANDROID)
Shawn Guo5e218862019-02-22 16:28:06 +08001077 select = (argc == 0) ? env_get("loadaddr") : argv[0];
Tom Rini1fec3c52015-08-27 15:42:41 -04001078#endif
1079
Simon Glass983c72f2013-06-11 11:14:46 -07001080 if (argc >= 2)
1081 select = argv[1];
Rob Herring2dd46322015-07-17 10:57:17 -05001082
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001083 /*
1084 * Look for a '-' which indicates to ignore the
1085 * ramdisk argument
1086 */
Simon Glass983c72f2013-06-11 11:14:46 -07001087 if (select && strcmp(select, "-") == 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001088 debug("## Skipping init Ramdisk\n");
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001089 rd_len = rd_data = 0;
Simon Glass983c72f2013-06-11 11:14:46 -07001090 } else if (select || genimg_has_config(images)) {
Simon Glass73223f02016-02-22 22:55:43 -07001091#if IMAGE_ENABLE_FIT
Simon Glass983c72f2013-06-11 11:14:46 -07001092 if (select) {
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001093 /*
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.
1098 */
1099 if (images->fit_uname_os)
1100 default_addr = (ulong)images->fit_hdr_os;
1101 else
1102 default_addr = load_addr;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001103
Simon Glass983c72f2013-06-11 11:14:46 -07001104 if (fit_parse_conf(select, default_addr,
1105 &rd_addr, &fit_uname_config)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001106 debug("* ramdisk: config '%s' from image at "
1107 "0x%08lx\n",
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001108 fit_uname_config, rd_addr);
Simon Glass983c72f2013-06-11 11:14:46 -07001109 } else if (fit_parse_subimage(select, default_addr,
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001110 &rd_addr, &fit_uname_ramdisk)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001111 debug("* ramdisk: subimage '%s' from image at "
1112 "0x%08lx\n",
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001113 fit_uname_ramdisk, rd_addr);
1114 } else
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001115#endif
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001116 {
Simon Glass983c72f2013-06-11 11:14:46 -07001117 rd_addr = simple_strtoul(select, NULL, 16);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001118 debug("* ramdisk: cmdline image address = "
1119 "0x%08lx\n",
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001120 rd_addr);
1121 }
Simon Glass73223f02016-02-22 22:55:43 -07001122#if IMAGE_ENABLE_FIT
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001123 } else {
1124 /* use FIT configuration provided in first bootm
Simon Glassa51ec632013-05-16 13:53:22 +00001125 * command argument. If the property is not defined,
1126 * quit silently.
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001127 */
Simon Glass35e7b0f2013-05-07 06:12:03 +00001128 rd_addr = map_to_sysmem(images->fit_hdr_os);
Simon Glassa51ec632013-05-16 13:53:22 +00001129 rd_noffset = fit_get_node_from_config(images,
1130 FIT_RAMDISK_PROP, rd_addr);
Paul Burtonbd86ef12016-09-20 18:17:12 +01001131 if (rd_noffset == -ENOENT)
Peter Tyser41266c92008-08-05 10:51:57 -05001132 return 0;
Simon Glassa51ec632013-05-16 13:53:22 +00001133 else if (rd_noffset < 0)
1134 return 1;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001135 }
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001136#endif
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001137
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001138 /*
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.
1142 */
Simon Glass35e7b0f2013-05-07 06:12:03 +00001143 buf = map_sysmem(rd_addr, 0);
1144 switch (genimg_get_format(buf)) {
Tom Rinic76c93a2019-05-23 07:14:07 -04001145#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001146 case IMAGE_FORMAT_LEGACY:
Stephen Warren712fbcf2011-10-18 11:11:49 +00001147 printf("## Loading init Ramdisk from Legacy "
Marian Balakowiczc8779642008-03-12 10:12:37 +01001148 "Image at %08lx ...\n", rd_addr);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001149
Simon Glass770605e2012-02-13 13:51:18 +00001150 bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001151 rd_hdr = image_get_ramdisk(rd_addr, arch,
Marian Balakowiczd985c842008-03-12 10:14:38 +01001152 images->verify);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001153
Marian Balakowiczc8779642008-03-12 10:12:37 +01001154 if (rd_hdr == NULL)
Kumar Gala274cea22008-02-27 21:51:46 -06001155 return 1;
Kumar Gala274cea22008-02-27 21:51:46 -06001156
Stephen Warren712fbcf2011-10-18 11:11:49 +00001157 rd_data = image_get_data(rd_hdr);
1158 rd_len = image_get_data_size(rd_hdr);
1159 rd_load = image_get_load(rd_hdr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001160 break;
Heiko Schocher21d29f72014-05-28 11:33:33 +02001161#endif
Simon Glass73223f02016-02-22 22:55:43 -07001162#if IMAGE_ENABLE_FIT
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001163 case IMAGE_FORMAT_FIT:
Simon Glass126cc862014-06-12 07:24:47 -06001164 rd_noffset = fit_image_load(images,
Simon Glassa51ec632013-05-16 13:53:22 +00001165 rd_addr, &fit_uname_ramdisk,
Simon Glassf320a4d2013-07-10 23:08:10 -07001166 &fit_uname_config, arch,
Simon Glassa51ec632013-05-16 13:53:22 +00001167 IH_TYPE_RAMDISK,
1168 BOOTSTAGE_ID_FIT_RD_START,
Simon Glassfe20a812014-08-22 14:26:43 -06001169 FIT_LOAD_OPTIONAL_NON_ZERO,
1170 &rd_data, &rd_len);
Simon Glassa51ec632013-05-16 13:53:22 +00001171 if (rd_noffset < 0)
Michal Simekc78fce62008-07-11 10:43:13 +02001172 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +01001173
Simon Glassa51ec632013-05-16 13:53:22 +00001174 images->fit_hdr_rd = map_sysmem(rd_addr, 0);
Marian Balakowiczc8779642008-03-12 10:12:37 +01001175 images->fit_uname_rd = fit_uname_ramdisk;
Marian Balakowicz3dfe1102008-03-12 10:32:59 +01001176 images->fit_noffset_rd = rd_noffset;
Marian Balakowiczc8779642008-03-12 10:12:37 +01001177 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001178#endif
Rob Herring2dd46322015-07-17 10:57:17 -05001179#ifdef CONFIG_ANDROID_BOOT_IMAGE
1180 case IMAGE_FORMAT_ANDROID:
1181 android_image_get_ramdisk((void *)images->os.start,
1182 &rd_data, &rd_len);
1183 break;
1184#endif
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001185 default:
Marek Vasut017e1f32012-03-18 11:47:58 +00001186#ifdef CONFIG_SUPPORT_RAW_INITRD
Simon Glass983c72f2013-06-11 11:14:46 -07001187 end = NULL;
1188 if (select)
1189 end = strchr(select, ':');
1190 if (end) {
Marek Vasut017e1f32012-03-18 11:47:58 +00001191 rd_len = simple_strtoul(++end, NULL, 16);
1192 rd_data = rd_addr;
1193 } else
1194#endif
1195 {
1196 puts("Wrong Ramdisk Image Format\n");
1197 rd_data = rd_len = rd_load = 0;
1198 return 1;
1199 }
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001200 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001201 } else if (images->legacy_hdr_valid &&
Stephen Warren712fbcf2011-10-18 11:11:49 +00001202 image_check_type(&images->legacy_hdr_os_copy,
1203 IH_TYPE_MULTI)) {
1204
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001205 /*
1206 * Now check if we have a legacy mult-component image,
1207 * get second entry data start address and len.
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001208 */
Simon Glass770605e2012-02-13 13:51:18 +00001209 bootstage_mark(BOOTSTAGE_ID_RAMDISK);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001210 printf("## Loading init Ramdisk from multi component "
Marian Balakowiczc8779642008-03-12 10:12:37 +01001211 "Legacy Image at %08lx ...\n",
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001212 (ulong)images->legacy_hdr_os);
1213
Stephen Warren712fbcf2011-10-18 11:11:49 +00001214 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
Rob Herring2dd46322015-07-17 10:57:17 -05001215 } else {
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001216 /*
1217 * no initrd image
1218 */
Simon Glass770605e2012-02-13 13:51:18 +00001219 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001220 rd_len = rd_data = 0;
1221 }
1222
1223 if (!rd_data) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001224 debug("## No init Ramdisk\n");
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001225 } else {
1226 *rd_start = rd_data;
1227 *rd_end = rd_data + rd_len;
1228 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00001229 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001230 *rd_start, *rd_end);
Kumar Gala274cea22008-02-27 21:51:46 -06001231
1232 return 0;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001233}
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001234
John Rigbyfca43cc2010-10-13 13:57:35 -06001235#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001236/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001237 * boot_ramdisk_high - relocate init ramdisk
Kumar Galae822d7f2008-02-27 21:51:49 -06001238 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001239 * @rd_data: ramdisk data start address
1240 * @rd_len: ramdisk data length
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001241 * @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)
1245 *
Robert P. J. Day1bce2ae2013-09-16 07:15:45 -04001246 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001247 * variable and if requested ramdisk data is moved to a specified location.
1248 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001249 * 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.
1252 *
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001253 * returns:
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001254 * 0 - success
1255 * -1 - failure
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001256 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001257int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
Kumar Galae822d7f2008-02-27 21:51:49 -06001258 ulong *initrd_start, ulong *initrd_end)
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001259{
1260 char *s;
1261 ulong initrd_high;
1262 int initrd_copy_to_ram = 1;
1263
Simon Glass00caae62017-08-03 12:22:12 -06001264 s = env_get("initrd_high");
1265 if (s) {
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001266 /* a value of "no" or a similar string will act like 0,
1267 * turning the "load high" feature off. This is intentional.
1268 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001269 initrd_high = simple_strtoul(s, NULL, 16);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001270 if (initrd_high == ~0)
1271 initrd_copy_to_ram = 0;
1272 } else {
Simon Glass723806c2017-08-03 12:22:15 -06001273 initrd_high = env_get_bootm_mapsize() + env_get_bootm_low();
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001274 }
1275
Marian Balakowicz95d449a2008-05-13 15:53:29 +02001276
Stephen Warren712fbcf2011-10-18 11:11:49 +00001277 debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001278 initrd_high, initrd_copy_to_ram);
1279
1280 if (rd_data) {
1281 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001282 debug(" in-place initrd\n");
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001283 *initrd_start = rd_data;
1284 *initrd_end = rd_data + rd_len;
Kumar Galae822d7f2008-02-27 21:51:49 -06001285 lmb_reserve(lmb, rd_data, rd_len);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001286 } else {
Kumar Galae822d7f2008-02-27 21:51:49 -06001287 if (initrd_high)
Stephen Warren712fbcf2011-10-18 11:11:49 +00001288 *initrd_start = (ulong)lmb_alloc_base(lmb,
1289 rd_len, 0x1000, initrd_high);
Kumar Galae822d7f2008-02-27 21:51:49 -06001290 else
Stephen Warren712fbcf2011-10-18 11:11:49 +00001291 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
1292 0x1000);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001293
Kumar Galae822d7f2008-02-27 21:51:49 -06001294 if (*initrd_start == 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001295 puts("ramdisk - allocation error\n");
Kumar Galae822d7f2008-02-27 21:51:49 -06001296 goto error;
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001297 }
Simon Glass770605e2012-02-13 13:51:18 +00001298 bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001299
1300 *initrd_end = *initrd_start + rd_len;
Stephen Warren712fbcf2011-10-18 11:11:49 +00001301 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001302 *initrd_start, *initrd_end);
1303
Stephen Warren712fbcf2011-10-18 11:11:49 +00001304 memmove_wd((void *)*initrd_start,
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001305 (void *)rd_data, rd_len, CHUNKSZ);
1306
Kumar Gala3b200112011-12-07 04:42:58 +00001307#ifdef CONFIG_MP
1308 /*
1309 * Ensure the image is flushed to memory to handle
1310 * AMP boot scenarios in which we might not be
1311 * HW cache coherent
1312 */
Heiko Schocher1a1e7072017-12-14 11:19:22 +01001313 flush_cache((unsigned long)*initrd_start,
1314 ALIGN(rd_len, ARCH_DMA_MINALIGN));
Kumar Gala3b200112011-12-07 04:42:58 +00001315#endif
Stephen Warren712fbcf2011-10-18 11:11:49 +00001316 puts("OK\n");
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001317 }
1318 } else {
1319 *initrd_start = 0;
1320 *initrd_end = 0;
1321 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00001322 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001323 *initrd_start, *initrd_end);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001324
Kumar Galae822d7f2008-02-27 21:51:49 -06001325 return 0;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001326
Kumar Galae822d7f2008-02-27 21:51:49 -06001327error:
1328 return -1;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001329}
John Rigbyfca43cc2010-10-13 13:57:35 -06001330#endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001331
Simon Glass90268b82014-10-19 21:11:24 -06001332int boot_get_setup(bootm_headers_t *images, uint8_t arch,
1333 ulong *setup_start, ulong *setup_len)
1334{
Simon Glass73223f02016-02-22 22:55:43 -07001335#if IMAGE_ENABLE_FIT
Simon Glass90268b82014-10-19 21:11:24 -06001336 return boot_get_setup_fit(images, arch, setup_start, setup_len);
1337#else
1338 return -ENOENT;
1339#endif
1340}
1341
Simon Glass73223f02016-02-22 22:55:43 -07001342#if IMAGE_ENABLE_FIT
Goldschmidt Simon8b93a922017-11-10 14:17:41 +00001343#if defined(CONFIG_FPGA)
Michal Simek62afc602016-05-17 14:03:50 +02001344int boot_get_fpga(int argc, char * const argv[], bootm_headers_t *images,
1345 uint8_t arch, const ulong *ld_start, ulong * const ld_len)
1346{
1347 ulong tmp_img_addr, img_data, img_len;
1348 void *buf;
1349 int conf_noffset;
1350 int fit_img_result;
Simon Glassb02e4042016-10-02 17:59:28 -06001351 const char *uname, *name;
Michal Simek62afc602016-05-17 14:03:50 +02001352 int err;
1353 int devnum = 0; /* TODO support multi fpga platforms */
Michal Simek62afc602016-05-17 14:03:50 +02001354
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");
1358 return 0;
1359 }
1360
1361 /*
1362 * Obtain the os FIT header from the images struct
Michal Simek62afc602016-05-17 14:03:50 +02001363 */
1364 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
Michal Simek62afc602016-05-17 14:03:50 +02001365 buf = map_sysmem(tmp_img_addr, 0);
1366 /*
1367 * Check image type. For FIT images get FIT node
1368 * and attempt to locate a generic binary.
1369 */
1370 switch (genimg_get_format(buf)) {
1371 case IMAGE_FORMAT_FIT:
1372 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
1373
Simon Glassb02e4042016-10-02 17:59:28 -06001374 uname = fdt_stringlist_get(buf, conf_noffset, FIT_FPGA_PROP, 0,
1375 NULL);
1376 if (!uname) {
Michal Simek62afc602016-05-17 14:03:50 +02001377 debug("## FPGA image is not specified\n");
1378 return 0;
1379 }
1380 fit_img_result = fit_image_load(images,
1381 tmp_img_addr,
1382 (const char **)&uname,
1383 &(images->fit_uname_cfg),
1384 arch,
1385 IH_TYPE_FPGA,
1386 BOOTSTAGE_ID_FPGA_INIT,
1387 FIT_LOAD_OPTIONAL_NON_ZERO,
1388 &img_data, &img_len);
1389
1390 debug("FPGA image (%s) loaded to 0x%lx/size 0x%lx\n",
1391 uname, img_data, img_len);
1392
1393 if (fit_img_result < 0) {
1394 /* Something went wrong! */
1395 return fit_img_result;
1396 }
1397
Goldschmidt Simon8b93a922017-11-10 14:17:41 +00001398 if (!fpga_is_partial_data(devnum, img_len)) {
Michal Simek62afc602016-05-17 14:03:50 +02001399 name = "full";
1400 err = fpga_loadbitstream(devnum, (char *)img_data,
1401 img_len, BIT_FULL);
1402 if (err)
1403 err = fpga_load(devnum, (const void *)img_data,
1404 img_len, BIT_FULL);
1405 } else {
1406 name = "partial";
1407 err = fpga_loadbitstream(devnum, (char *)img_data,
1408 img_len, BIT_PARTIAL);
1409 if (err)
1410 err = fpga_load(devnum, (const void *)img_data,
1411 img_len, BIT_PARTIAL);
1412 }
1413
Michal Simek62afc602016-05-17 14:03:50 +02001414 if (err)
Michal Simek611a9422016-12-16 10:35:40 +01001415 return err;
1416
1417 printf(" Programming %s bitstream... OK\n", name);
Michal Simek62afc602016-05-17 14:03:50 +02001418 break;
1419 default:
1420 printf("The given image format is not supported (corrupt?)\n");
1421 return 1;
1422 }
1423
1424 return 0;
1425}
1426#endif
1427
Andrew F. Davisd7be5092016-11-29 16:33:20 -06001428static void fit_loadable_process(uint8_t img_type,
1429 ulong img_data,
1430 ulong img_len)
1431{
1432 int i;
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);
1443}
1444
Karl Apsite84a07db2015-05-21 09:52:48 -04001445int boot_get_loadable(int argc, char * const argv[], bootm_headers_t *images,
1446 uint8_t arch, const ulong *ld_start, ulong * const ld_len)
1447{
1448 /*
1449 * These variables are used to hold the current image location
1450 * in system memory.
1451 */
1452 ulong tmp_img_addr;
1453 /*
1454 * These two variables are requirements for fit_image_load, but
1455 * their values are not used
1456 */
1457 ulong img_data, img_len;
1458 void *buf;
1459 int loadables_index;
1460 int conf_noffset;
1461 int fit_img_result;
Simon Glassb02e4042016-10-02 17:59:28 -06001462 const char *uname;
Andrew F. Davisd7be5092016-11-29 16:33:20 -06001463 uint8_t img_type;
Karl Apsite84a07db2015-05-21 09:52:48 -04001464
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");
1468 return 0;
1469 }
1470
1471 /*
1472 * Obtain the os FIT header from the images struct
Karl Apsite84a07db2015-05-21 09:52:48 -04001473 */
1474 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
Karl Apsite84a07db2015-05-21 09:52:48 -04001475 buf = map_sysmem(tmp_img_addr, 0);
1476 /*
1477 * Check image type. For FIT images get FIT node
1478 * and attempt to locate a generic binary.
1479 */
1480 switch (genimg_get_format(buf)) {
1481 case IMAGE_FORMAT_FIT:
1482 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
1483
1484 for (loadables_index = 0;
Simon Glassb02e4042016-10-02 17:59:28 -06001485 uname = fdt_stringlist_get(buf, conf_noffset,
1486 FIT_LOADABLE_PROP, loadables_index,
1487 NULL), uname;
Karl Apsite84a07db2015-05-21 09:52:48 -04001488 loadables_index++)
1489 {
1490 fit_img_result = fit_image_load(images,
1491 tmp_img_addr,
Simon Glassb02e4042016-10-02 17:59:28 -06001492 &uname,
Karl Apsite84a07db2015-05-21 09:52:48 -04001493 &(images->fit_uname_cfg), arch,
1494 IH_TYPE_LOADABLE,
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;
1501 }
Andrew F. Davisd7be5092016-11-29 16:33:20 -06001502
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;
1507 }
1508 fit_img_result = fit_image_get_type(buf,
1509 fit_img_result,
1510 &img_type);
1511 if (fit_img_result < 0) {
1512 /* Something went wrong! */
1513 return fit_img_result;
1514 }
1515
1516 fit_loadable_process(img_type, img_data, img_len);
Karl Apsite84a07db2015-05-21 09:52:48 -04001517 }
1518 break;
1519 default:
1520 printf("The given image format is not supported (corrupt?)\n");
1521 return 1;
1522 }
1523
1524 return 0;
1525}
1526#endif
1527
John Rigbyfca43cc2010-10-13 13:57:35 -06001528#ifdef CONFIG_SYS_BOOT_GET_CMDLINE
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001529/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001530 * boot_get_cmdline - allocate and initialize kernel cmdline
Kumar Galae822d7f2008-02-27 21:51:49 -06001531 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001532 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1533 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1534 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001535 * boot_get_cmdline() allocates space for kernel command line below
Shyam Saini919d25c2018-06-07 19:47:19 +05301536 * BOOTMAPSZ + env_get_bootm_low() address. If "bootargs" U-Boot environment
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001537 * variable is present its contents is copied to allocated kernel
1538 * command line.
1539 *
1540 * returns:
Kumar Galae822d7f2008-02-27 21:51:49 -06001541 * 0 - success
1542 * -1 - failure
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001543 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001544int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001545{
1546 char *cmdline;
1547 char *s;
1548
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001549 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
Simon Glass723806c2017-08-03 12:22:15 -06001550 env_get_bootm_mapsize() + env_get_bootm_low());
Kumar Galae822d7f2008-02-27 21:51:49 -06001551
1552 if (cmdline == NULL)
1553 return -1;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001554
Simon Glass00caae62017-08-03 12:22:12 -06001555 s = env_get("bootargs");
1556 if (!s)
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001557 s = "";
1558
1559 strcpy(cmdline, s);
1560
1561 *cmd_start = (ulong) & cmdline[0];
1562 *cmd_end = *cmd_start + strlen(cmdline);
1563
Stephen Warren712fbcf2011-10-18 11:11:49 +00001564 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001565
Kumar Galae822d7f2008-02-27 21:51:49 -06001566 return 0;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001567}
John Rigbyfca43cc2010-10-13 13:57:35 -06001568#endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001569
John Rigbyfca43cc2010-10-13 13:57:35 -06001570#ifdef CONFIG_SYS_BOOT_GET_KBD
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001571/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001572 * boot_get_kbd - allocate and initialize kernel copy of board info
Kumar Galae822d7f2008-02-27 21:51:49 -06001573 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001574 * @kbd: double pointer to board info data
1575 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001576 * boot_get_kbd() allocates space for kernel copy of board info data below
Simon Glass723806c2017-08-03 12:22:15 -06001577 * BOOTMAPSZ + env_get_bootm_low() address and kernel board info is initialized
Grant Likely590d3ca2011-03-28 09:58:34 +00001578 * with the current u-boot board info data.
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001579 *
1580 * returns:
Kumar Galae822d7f2008-02-27 21:51:49 -06001581 * 0 - success
1582 * -1 - failure
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001583 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001584int boot_get_kbd(struct lmb *lmb, bd_t **kbd)
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001585{
Becky Bruce391fd932008-06-09 20:37:18 -05001586 *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
Simon Glass723806c2017-08-03 12:22:15 -06001587 env_get_bootm_mapsize() + env_get_bootm_low());
Kumar Galae822d7f2008-02-27 21:51:49 -06001588 if (*kbd == NULL)
1589 return -1;
1590
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001591 **kbd = *(gd->bd);
1592
Stephen Warren712fbcf2011-10-18 11:11:49 +00001593 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001594
1595#if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1596 do_bdinfo(NULL, 0, 0, NULL);
1597#endif
1598
Kumar Galae822d7f2008-02-27 21:51:49 -06001599 return 0;
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001600}
John Rigbyfca43cc2010-10-13 13:57:35 -06001601#endif /* CONFIG_SYS_BOOT_GET_KBD */
Simon Glass13d06982013-05-08 08:06:01 +00001602
1603#ifdef CONFIG_LMB
1604int image_setup_linux(bootm_headers_t *images)
1605{
1606 ulong of_size = images->ft_len;
1607 char **of_flat_tree = &images->ft_addr;
Simon Glass13d06982013-05-08 08:06:01 +00001608 struct lmb *lmb = &images->lmb;
Simon Glass13d06982013-05-08 08:06:01 +00001609 int ret;
1610
1611 if (IMAGE_ENABLE_OF_LIBFDT)
1612 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
1613
1614 if (IMAGE_BOOT_GET_CMDLINE) {
1615 ret = boot_get_cmdline(lmb, &images->cmdline_start,
1616 &images->cmdline_end);
1617 if (ret) {
1618 puts("ERROR with allocation of cmdline\n");
1619 return ret;
1620 }
1621 }
Simon Glass13d06982013-05-08 08:06:01 +00001622
1623 if (IMAGE_ENABLE_OF_LIBFDT) {
1624 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
1625 if (ret)
1626 return ret;
1627 }
1628
1629 if (IMAGE_ENABLE_OF_LIBFDT && of_size) {
1630 ret = image_setup_libfdt(images, *of_flat_tree, of_size, lmb);
1631 if (ret)
1632 return ret;
1633 }
1634
1635 return 0;
1636}
1637#endif /* CONFIG_LMB */
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001638#endif /* !USE_HOSTCC */