blob: 8ac57081fddfd63b0153e2be87d73a229272586d [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 Glass4d72caa2020-05-10 11:40:01 -060014#include <lmb.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060015#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070016#include <malloc.h>
Simon Glass90526e92020-05-10 11:39:56 -060017#include <asm/cache.h>
Simon Glass3db71102019-11-14 12:57:16 -070018#include <u-boot/crc.h>
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +010019#include <watchdog.h>
20
21#ifdef CONFIG_SHOW_BOOT_PROGRESS
22#include <status_led.h>
23#endif
24
Marian Balakowicz2242f532008-02-21 17:27:41 +010025#include <rtc.h>
Marian Balakowicz2242f532008-02-21 17:27:41 +010026
Simon Glass0c670fc2019-08-01 09:46:36 -060027#include <gzip.h>
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010028#include <image.h>
Simon Glass6c03f9e2019-11-14 12:57:25 -070029#include <lz4.h>
Joe Hershberger0eb25b62015-03-22 17:08:59 -050030#include <mapmem.h>
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010031
Simon Glassaa34fbc2016-02-22 22:55:45 -070032#if IMAGE_ENABLE_FIT || IMAGE_ENABLE_OF_LIBFDT
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090033#include <linux/libfdt.h>
Marian Balakowiczfff888a12008-02-21 17:20:19 +010034#include <fdt_support.h>
Michal Simek62afc602016-05-17 14:03:50 +020035#include <fpga.h>
36#include <xilinx.h>
Marian Balakowiczc8779642008-03-12 10:12:37 +010037#endif
38
Simon Glass401d1c42020-10-30 21:38:53 -060039#include <asm/global_data.h>
Andy Fleming20a14a42008-04-02 16:19:07 -050040#include <u-boot/md5.h>
Jeroen Hofstee2b9912e2014-06-12 22:27:12 +020041#include <u-boot/sha1.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090042#include <linux/errno.h>
Simon Glass35e7b0f2013-05-07 06:12:03 +000043#include <asm/io.h>
Marian Balakowiczc8779642008-03-12 10:12:37 +010044
Julius Werner20908542019-07-24 19:37:54 -070045#include <bzlib.h>
46#include <linux/lzo.h>
47#include <lzma/LzmaTypes.h>
48#include <lzma/LzmaDec.h>
49#include <lzma/LzmaTools.h>
Robert Marko26073f92020-04-25 19:37:21 +020050#include <linux/zstd.h>
Julius Werner20908542019-07-24 19:37:54 -070051
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +010052#ifdef CONFIG_CMD_BDI
Simon Glass09140112020-05-10 11:40:03 -060053extern int do_bdinfo(struct cmd_tbl *cmdtp, int flag, int argc,
54 char *const argv[]);
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +010055#endif
56
57DECLARE_GLOBAL_DATA_PTR;
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +010058
Tom Rinic76c93a2019-05-23 07:14:07 -040059#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Stephen Warren712fbcf2011-10-18 11:11:49 +000060static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
Marian Balakowiczd985c842008-03-12 10:14:38 +010061 int verify);
Heiko Schocher21d29f72014-05-28 11:33:33 +020062#endif
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010063#else
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +010064#include "mkimage.h"
Andy Fleming20a14a42008-04-02 16:19:07 -050065#include <u-boot/md5.h>
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010066#include <time.h>
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010067#include <image.h>
Heiko Schocher80402f32015-06-29 09:10:46 +020068
69#ifndef __maybe_unused
70# define __maybe_unused /* unimplemented */
71#endif
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010072#endif /* !USE_HOSTCC*/
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010073
Simon Glass0ccff502013-02-24 17:33:25 +000074#include <u-boot/crc.h>
Breno Matheus Lima5b20d142019-09-23 18:39:47 +000075#include <imximage.h>
Simon Glass0ccff502013-02-24 17:33:25 +000076
Simon Glass13d06982013-05-08 08:06:01 +000077#ifndef CONFIG_SYS_BARGSIZE
78#define CONFIG_SYS_BARGSIZE 512
79#endif
80
Mike Frysinger7edb1862010-10-20 07:17:39 -040081static const table_entry_t uimage_arch[] = {
Simon Glass30495bf2016-06-30 10:52:15 -060082 { IH_ARCH_INVALID, "invalid", "Invalid ARCH", },
Marian Balakowicz570abb02008-02-29 15:59:59 +010083 { IH_ARCH_ALPHA, "alpha", "Alpha", },
84 { IH_ARCH_ARM, "arm", "ARM", },
85 { IH_ARCH_I386, "x86", "Intel x86", },
86 { IH_ARCH_IA64, "ia64", "IA64", },
87 { IH_ARCH_M68K, "m68k", "M68K", },
88 { IH_ARCH_MICROBLAZE, "microblaze", "MicroBlaze", },
89 { IH_ARCH_MIPS, "mips", "MIPS", },
90 { IH_ARCH_MIPS64, "mips64", "MIPS 64 Bit", },
Marian Balakowicz570abb02008-02-29 15:59:59 +010091 { IH_ARCH_NIOS2, "nios2", "NIOS II", },
Grant Ericksone419e122008-05-04 16:45:01 -070092 { IH_ARCH_PPC, "powerpc", "PowerPC", },
Marian Balakowicz570abb02008-02-29 15:59:59 +010093 { IH_ARCH_PPC, "ppc", "PowerPC", },
94 { IH_ARCH_S390, "s390", "IBM S390", },
95 { IH_ARCH_SH, "sh", "SuperH", },
96 { IH_ARCH_SPARC, "sparc", "SPARC", },
97 { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", },
98 { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", },
99 { IH_ARCH_AVR32, "avr32", "AVR32", },
Macpaul Lin64d61462011-10-19 20:41:09 +0000100 { IH_ARCH_NDS32, "nds32", "NDS32", },
Stefan Kristiansson3ddcacc2011-11-26 19:04:50 +0000101 { IH_ARCH_OPENRISC, "or1k", "OpenRISC 1000",},
Simon Glass35e7b0f2013-05-07 06:12:03 +0000102 { IH_ARCH_SANDBOX, "sandbox", "Sandbox", },
David Feng0ae76532013-12-14 11:47:35 +0800103 { IH_ARCH_ARM64, "arm64", "AArch64", },
Alexey Brodkinbc5d5422014-02-04 12:56:16 +0400104 { IH_ARCH_ARC, "arc", "ARC", },
Simon Glass5bda35c2014-10-10 08:21:57 -0600105 { IH_ARCH_X86_64, "x86_64", "AMD x86_64", },
Chris Zankelde5e5ce2016-08-10 18:36:43 +0300106 { IH_ARCH_XTENSA, "xtensa", "Xtensa", },
Rick Chen86aa65a2018-03-13 13:37:29 +0800107 { IH_ARCH_RISCV, "riscv", "RISC-V", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100108 { -1, "", "", },
109};
110
Mike Frysinger7edb1862010-10-20 07:17:39 -0400111static const table_entry_t uimage_os[] = {
Simon Glass30495bf2016-06-30 10:52:15 -0600112 { IH_OS_INVALID, "invalid", "Invalid OS", },
Philipp Tomsich4914af12017-09-13 21:29:29 +0200113 { IH_OS_ARM_TRUSTED_FIRMWARE, "arm-trusted-firmware", "ARM Trusted Firmware" },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100114 { IH_OS_LINUX, "linux", "Linux", },
115#if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
116 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
117#endif
118 { IH_OS_NETBSD, "netbsd", "NetBSD", },
Torkel Lundgren3df61952010-09-28 11:05:36 +0200119 { IH_OS_OSE, "ose", "Enea OSE", },
Steven Stallion04d41402013-03-20 06:31:35 +0000120 { IH_OS_PLAN9, "plan9", "Plan 9", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100121 { IH_OS_RTEMS, "rtems", "RTEMS", },
Bryan O'Donoghue45b55712018-03-13 16:50:35 +0000122 { IH_OS_TEE, "tee", "Trusted Execution Environment" },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100123 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
miao.yan@windriver.com68b15e82013-12-27 16:01:50 +0800124 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100125#if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
126 { IH_OS_QNX, "qnx", "QNX", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100127#endif
Peter Tyserf5ed9e32008-09-08 14:56:49 -0500128#if defined(CONFIG_INTEGRITY) || defined(USE_HOSTCC)
129 { IH_OS_INTEGRITY,"integrity", "INTEGRITY", },
130#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100131#ifdef USE_HOSTCC
132 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
133 { IH_OS_DELL, "dell", "Dell", },
134 { IH_OS_ESIX, "esix", "Esix", },
135 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
136 { IH_OS_IRIX, "irix", "Irix", },
137 { IH_OS_NCR, "ncr", "NCR", },
138 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
139 { IH_OS_PSOS, "psos", "pSOS", },
140 { IH_OS_SCO, "sco", "SCO", },
141 { IH_OS_SOLARIS, "solaris", "Solaris", },
142 { IH_OS_SVR4, "svr4", "SVR4", },
143#endif
Marek Vasut67ddd952014-12-16 14:07:21 +0100144#if defined(CONFIG_BOOTM_OPENRTOS) || defined(USE_HOSTCC)
145 { IH_OS_OPENRTOS, "openrtos", "OpenRTOS", },
146#endif
Lukas Auer5e30e452019-08-21 21:14:44 +0200147 { IH_OS_OPENSBI, "opensbi", "RISC-V OpenSBI", },
Cristian Ciocalteaa031b032019-12-24 18:05:38 +0200148 { IH_OS_EFI, "efi", "EFI Firmware" },
Marek Vasut67ddd952014-12-16 14:07:21 +0100149
Marian Balakowicz570abb02008-02-29 15:59:59 +0100150 { -1, "", "", },
151};
152
Mike Frysinger7edb1862010-10-20 07:17:39 -0400153static const table_entry_t uimage_type[] = {
Stefano Babic4962e382011-10-17 00:07:43 +0000154 { IH_TYPE_AISIMAGE, "aisimage", "Davinci AIS image",},
Marian Balakowicz570abb02008-02-29 15:59:59 +0100155 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
156 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
John Rigby3decb142011-07-21 09:10:30 -0400157 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
Karicheri, Muralidharanbf411ea2014-04-04 13:16:48 -0400158 { IH_TYPE_GPIMAGE, "gpimage", "TI Keystone SPL Image",},
Marian Balakowicz570abb02008-02-29 15:59:59 +0100159 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
Stephen Warrenb9b50e892011-11-10 13:17:53 -0700160 { IH_TYPE_KERNEL_NOLOAD, "kernel_noload", "Kernel Image (no loading done)", },
Stefano Babic4962e382011-10-17 00:07:43 +0000161 { IH_TYPE_KWBIMAGE, "kwbimage", "Kirkwood Boot Image",},
162 { IH_TYPE_IMXIMAGE, "imximage", "Freescale i.MX Boot Image",},
Peng Fana2b96ec2018-10-16 04:50:30 +0000163 { IH_TYPE_IMX8IMAGE, "imx8image", "NXP i.MX8 Boot Image",},
Peng Fan6609c262018-11-20 10:19:36 +0000164 { IH_TYPE_IMX8MIMAGE, "imx8mimage", "NXP i.MX8M Boot Image",},
Simon Glass30495bf2016-06-30 10:52:15 -0600165 { IH_TYPE_INVALID, "invalid", "Invalid Image", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100166 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
Stefano Babic4962e382011-10-17 00:07:43 +0000167 { IH_TYPE_OMAPIMAGE, "omapimage", "TI OMAP SPL With GP CH",},
Shaohui Xie5d898a02012-08-10 02:49:35 +0000168 { IH_TYPE_PBLIMAGE, "pblimage", "Freescale PBL Boot Image",},
Marian Balakowicz570abb02008-02-29 15:59:59 +0100169 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
170 { IH_TYPE_SCRIPT, "script", "Script", },
Marek Vasut662abc42018-04-15 13:15:33 +0200171 { IH_TYPE_SOCFPGAIMAGE, "socfpgaimage", "Altera SoCFPGA CV/AV preloader",},
172 { IH_TYPE_SOCFPGAIMAGE_V1, "socfpgaimage_v1", "Altera SoCFPGA A10 preloader",},
Marian Balakowicz570abb02008-02-29 15:59:59 +0100173 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
Heiko Schocher7816f2c2011-07-16 00:06:42 +0000174 { IH_TYPE_UBLIMAGE, "ublimage", "Davinci UBL image",},
Marek Vasutbce88372013-08-26 20:43:33 +0200175 { IH_TYPE_MXSIMAGE, "mxsimage", "Freescale MXS Boot Image",},
Andreas Bießmann7b1a4112014-05-19 14:23:39 +0200176 { IH_TYPE_ATMELIMAGE, "atmelimage", "ATMEL ROM-Boot Image",},
Simon Glass90268b82014-10-19 21:11:24 -0600177 { IH_TYPE_X86_SETUP, "x86_setup", "x86 setup.bin", },
Albert ARIBAUD \(3ADEV\)39f520b2015-03-31 11:40:49 +0200178 { IH_TYPE_LPC32XXIMAGE, "lpc32xximage", "LPC32XX Boot Image", },
Simon Glassa131c1f2015-08-30 16:55:24 -0600179 { IH_TYPE_RKIMAGE, "rkimage", "Rockchip Boot Image" },
Simon Glassf9a3c272015-08-30 16:55:25 -0600180 { IH_TYPE_RKSD, "rksd", "Rockchip SD Boot Image" },
Simon Glass10b84fe2015-08-30 16:55:26 -0600181 { IH_TYPE_RKSPI, "rkspi", "Rockchip SPI Boot Image" },
Albert ARIBAUD \(3ADEV\)ed0c2c02016-09-26 09:08:06 +0200182 { IH_TYPE_VYBRIDIMAGE, "vybridimage", "Vybrid Boot Image", },
Nathan Rossi66eef1e2015-11-17 22:56:56 +1000183 { IH_TYPE_ZYNQIMAGE, "zynqimage", "Xilinx Zynq Boot Image" },
Michal Simekd9b58b32016-04-27 14:03:29 +0200184 { IH_TYPE_ZYNQMPIMAGE, "zynqmpimage", "Xilinx ZynqMP Boot Image" },
Alexander Graf6915dcf2018-04-13 14:18:52 +0200185 { IH_TYPE_ZYNQMPBIF, "zynqmpbif", "Xilinx ZynqMP Boot Image (bif)" },
Michal Simeked0cea72016-05-17 13:58:44 +0200186 { IH_TYPE_FPGA, "fpga", "FPGA Image" },
Andrew F. Davis7e719ee2016-11-29 16:33:21 -0600187 { IH_TYPE_TEE, "tee", "Trusted Execution Environment Image",},
Sven Ebenfeldd21bd692016-11-06 16:37:56 +0100188 { IH_TYPE_FIRMWARE_IVT, "firmware_ivt", "Firmware with HABv4 IVT" },
Andrew F. Davis6442c962017-07-31 10:58:20 -0500189 { IH_TYPE_PMMC, "pmmc", "TI Power Management Micro-Controller Firmware",},
Patrick Delaunay81260e32018-03-12 10:46:04 +0100190 { IH_TYPE_STM32IMAGE, "stm32image", "STMicroelectronics STM32 Image" },
Ryder Lee3b975a12018-11-15 10:07:49 +0800191 { IH_TYPE_MTKIMAGE, "mtk_image", "MediaTek BootROM loadable Image" },
Patrick Delaunaye7fabe72019-08-02 15:07:19 +0200192 { IH_TYPE_COPRO, "copro", "Coprocessor Image"},
Andre Przywara6d295092018-12-20 01:15:18 +0000193 { IH_TYPE_SUNXI_EGON, "sunxi_egon", "Allwinner eGON Boot Image" },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100194 { -1, "", "", },
195};
196
Mike Frysinger7edb1862010-10-20 07:17:39 -0400197static const table_entry_t uimage_comp[] = {
Marian Balakowicz570abb02008-02-29 15:59:59 +0100198 { IH_COMP_NONE, "none", "uncompressed", },
199 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
200 { IH_COMP_GZIP, "gzip", "gzip compressed", },
Luigi 'Comio' Mantellinifc9c1722008-09-08 02:46:13 +0200201 { IH_COMP_LZMA, "lzma", "lzma compressed", },
Peter Korsgaard20dde482009-11-19 11:37:51 +0100202 { IH_COMP_LZO, "lzo", "lzo compressed", },
Julius Werner027b7282015-10-06 20:03:53 -0700203 { IH_COMP_LZ4, "lz4", "lz4 compressed", },
Robert Marko26073f92020-04-25 19:37:21 +0200204 { IH_COMP_ZSTD, "zstd", "zstd compressed", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100205 { -1, "", "", },
206};
207
Simon Glass56d7ab72016-06-30 10:52:14 -0600208struct table_info {
209 const char *desc;
210 int count;
211 const table_entry_t *table;
212};
213
Atish Patra155d6a32020-03-05 16:24:22 -0800214static const struct comp_magic_map image_comp[] = {
215 { IH_COMP_BZIP2, "bzip2", {0x42, 0x5a},},
216 { IH_COMP_GZIP, "gzip", {0x1f, 0x8b},},
217 { IH_COMP_LZMA, "lzma", {0x5d, 0x00},},
218 { IH_COMP_LZO, "lzo", {0x89, 0x4c},},
Artem Lapkin2ddb8fc2021-08-31 18:22:18 +0800219 { IH_COMP_LZ4, "lz4", {0x04, 0x22},},
220 { IH_COMP_ZSTD, "zstd", {0x28, 0xb5},},
Atish Patra155d6a32020-03-05 16:24:22 -0800221 { IH_COMP_NONE, "none", {}, },
222};
223
Simon Glass56d7ab72016-06-30 10:52:14 -0600224static const struct table_info table_info[IH_COUNT] = {
225 { "architecture", IH_ARCH_COUNT, uimage_arch },
226 { "compression", IH_COMP_COUNT, uimage_comp },
227 { "operating system", IH_OS_COUNT, uimage_os },
228 { "image type", IH_TYPE_COUNT, uimage_type },
229};
230
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100231/*****************************************************************************/
232/* Legacy format routines */
233/*****************************************************************************/
Stephen Warren712fbcf2011-10-18 11:11:49 +0000234int image_check_hcrc(const image_header_t *hdr)
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100235{
236 ulong hcrc;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000237 ulong len = image_get_header_size();
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100238 image_header_t header;
239
240 /* Copy header so we can blank CRC field for re-calculation */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000241 memmove(&header, (char *)hdr, image_get_header_size());
242 image_set_hcrc(&header, 0);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100243
Stephen Warren712fbcf2011-10-18 11:11:49 +0000244 hcrc = crc32(0, (unsigned char *)&header, len);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100245
Stephen Warren712fbcf2011-10-18 11:11:49 +0000246 return (hcrc == image_get_hcrc(hdr));
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100247}
248
Stephen Warren712fbcf2011-10-18 11:11:49 +0000249int image_check_dcrc(const image_header_t *hdr)
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100250{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000251 ulong data = image_get_data(hdr);
252 ulong len = image_get_data_size(hdr);
253 ulong dcrc = crc32_wd(0, (unsigned char *)data, len, CHUNKSZ_CRC32);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100254
Stephen Warren712fbcf2011-10-18 11:11:49 +0000255 return (dcrc == image_get_dcrc(hdr));
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100256}
257
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100258/**
259 * image_multi_count - get component (sub-image) count
260 * @hdr: pointer to the header of the multi component image
261 *
262 * image_multi_count() returns number of components in a multi
263 * component image.
264 *
265 * Note: no checking of the image type is done, caller must pass
266 * a valid multi component image.
267 *
268 * returns:
269 * number of components
270 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000271ulong image_multi_count(const image_header_t *hdr)
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100272{
273 ulong i, count = 0;
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100274 uint32_t *size;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100275
276 /* get start of the image payload, which in case of multi
277 * component images that points to a table of component sizes */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000278 size = (uint32_t *)image_get_data(hdr);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100279
280 /* count non empty slots */
281 for (i = 0; size[i]; ++i)
282 count++;
283
284 return count;
285}
286
287/**
288 * image_multi_getimg - get component data address and size
289 * @hdr: pointer to the header of the multi component image
290 * @idx: index of the requested component
291 * @data: pointer to a ulong variable, will hold component data address
292 * @len: pointer to a ulong variable, will hold component size
293 *
294 * image_multi_getimg() returns size and data address for the requested
295 * component in a multi component image.
296 *
297 * Note: no checking of the image type is done, caller must pass
298 * a valid multi component image.
299 *
300 * returns:
301 * data address and size of the component, if idx is valid
302 * 0 in data and len, if idx is out of range
303 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000304void image_multi_getimg(const image_header_t *hdr, ulong idx,
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100305 ulong *data, ulong *len)
306{
307 int i;
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100308 uint32_t *size;
Nick Spence02b9b222008-05-10 14:02:04 -0700309 ulong offset, count, img_data;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100310
311 /* get number of component */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000312 count = image_multi_count(hdr);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100313
314 /* get start of the image payload, which in case of multi
315 * component images that points to a table of component sizes */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000316 size = (uint32_t *)image_get_data(hdr);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100317
318 /* get address of the proper component data start, which means
319 * skipping sizes table (add 1 for last, null entry) */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000320 img_data = image_get_data(hdr) + (count + 1) * sizeof(uint32_t);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100321
322 if (idx < count) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000323 *len = uimage_to_cpu(size[idx]);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100324 offset = 0;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100325
326 /* go over all indices preceding requested component idx */
327 for (i = 0; i < idx; i++) {
Nick Spence02b9b222008-05-10 14:02:04 -0700328 /* add up i-th component size, rounding up to 4 bytes */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000329 offset += (uimage_to_cpu(size[i]) + 3) & ~3 ;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100330 }
331
332 /* calculate idx-th component data address */
Nick Spence02b9b222008-05-10 14:02:04 -0700333 *data = img_data + offset;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100334 } else {
335 *len = 0;
336 *data = 0;
337 }
338}
Marian Balakowicz42b73e82008-01-31 13:20:07 +0100339
Stephen Warren712fbcf2011-10-18 11:11:49 +0000340static void image_print_type(const image_header_t *hdr)
Marian Balakowicz2242f532008-02-21 17:27:41 +0100341{
Heiko Schocher80402f32015-06-29 09:10:46 +0200342 const char __maybe_unused *os, *arch, *type, *comp;
Marian Balakowicz2242f532008-02-21 17:27:41 +0100343
Stephen Warren712fbcf2011-10-18 11:11:49 +0000344 os = genimg_get_os_name(image_get_os(hdr));
345 arch = genimg_get_arch_name(image_get_arch(hdr));
346 type = genimg_get_type_name(image_get_type(hdr));
347 comp = genimg_get_comp_name(image_get_comp(hdr));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100348
Stephen Warren712fbcf2011-10-18 11:11:49 +0000349 printf("%s %s %s (%s)\n", arch, os, type, comp);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100350}
351
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100352/**
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200353 * image_print_contents - prints out the contents of the legacy format image
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200354 * @ptr: pointer to the legacy format image header
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100355 * @p: pointer to prefix string
356 *
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200357 * image_print_contents() formats a multi line legacy image contents description.
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100358 * The routine prints out all header fields followed by the size/offset data
359 * for MULTI/SCRIPT images.
360 *
361 * returns:
362 * no returned results
363 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000364void image_print_contents(const void *ptr)
Marian Balakowicz2242f532008-02-21 17:27:41 +0100365{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200366 const image_header_t *hdr = (const image_header_t *)ptr;
Heiko Schocher80402f32015-06-29 09:10:46 +0200367 const char __maybe_unused *p;
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200368
Simon Glass1fe7d932013-05-08 08:05:58 +0000369 p = IMAGE_INDENT_STRING;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000370 printf("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name(hdr));
Simon Glass859e92b2013-05-07 06:11:51 +0000371 if (IMAGE_ENABLE_TIMESTAMP) {
372 printf("%sCreated: ", p);
373 genimg_print_time((time_t)image_get_time(hdr));
374 }
Stephen Warren712fbcf2011-10-18 11:11:49 +0000375 printf("%sImage Type: ", p);
376 image_print_type(hdr);
377 printf("%sData Size: ", p);
378 genimg_print_size(image_get_data_size(hdr));
379 printf("%sLoad Address: %08x\n", p, image_get_load(hdr));
380 printf("%sEntry Point: %08x\n", p, image_get_ep(hdr));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100381
Stephen Warren712fbcf2011-10-18 11:11:49 +0000382 if (image_check_type(hdr, IH_TYPE_MULTI) ||
383 image_check_type(hdr, IH_TYPE_SCRIPT)) {
Marian Balakowicz2242f532008-02-21 17:27:41 +0100384 int i;
385 ulong data, len;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000386 ulong count = image_multi_count(hdr);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100387
Stephen Warren712fbcf2011-10-18 11:11:49 +0000388 printf("%sContents:\n", p);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100389 for (i = 0; i < count; i++) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000390 image_multi_getimg(hdr, i, &data, &len);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100391
Stephen Warren712fbcf2011-10-18 11:11:49 +0000392 printf("%s Image %d: ", p, i);
393 genimg_print_size(len);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100394
Stephen Warren712fbcf2011-10-18 11:11:49 +0000395 if (image_check_type(hdr, IH_TYPE_SCRIPT) && i > 0) {
Marian Balakowicz570abb02008-02-29 15:59:59 +0100396 /*
397 * the user may need to know offsets
398 * if planning to do something with
399 * multiple files
400 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000401 printf("%s Offset = 0x%08lx\n", p, data);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100402 }
Marian Balakowicz2242f532008-02-21 17:27:41 +0100403 }
Sven Ebenfeldd21bd692016-11-06 16:37:56 +0100404 } else if (image_check_type(hdr, IH_TYPE_FIRMWARE_IVT)) {
405 printf("HAB Blocks: 0x%08x 0x0000 0x%08x\n",
Breno Matheus Lima5b20d142019-09-23 18:39:47 +0000406 image_get_load(hdr) - image_get_header_size(),
407 (int)(image_get_size(hdr) + image_get_header_size()
408 + sizeof(flash_header_v2_t) - 0x2060));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100409 }
410}
411
Julius Werner20908542019-07-24 19:37:54 -0700412/**
413 * print_decomp_msg() - Print a suitable decompression/loading message
414 *
415 * @type: OS type (IH_OS_...)
416 * @comp_type: Compression type being used (IH_COMP_...)
417 * @is_xip: true if the load address matches the image start
418 */
419static void print_decomp_msg(int comp_type, int type, bool is_xip)
420{
421 const char *name = genimg_get_type_name(type);
422
423 if (comp_type == IH_COMP_NONE)
424 printf(" %s %s\n", is_xip ? "XIP" : "Loading", name);
425 else
426 printf(" Uncompressing %s\n", name);
427}
428
Atish Patra155d6a32020-03-05 16:24:22 -0800429int image_decomp_type(const unsigned char *buf, ulong len)
430{
431 const struct comp_magic_map *cmagic = image_comp;
432
433 if (len < 2)
434 return -EINVAL;
435
436 for (; cmagic->comp_id > 0; cmagic++) {
437 if (!memcmp(buf, cmagic->magic, 2))
438 break;
439 }
440
441 return cmagic->comp_id;
442}
443
Julius Werner20908542019-07-24 19:37:54 -0700444int image_decomp(int comp, ulong load, ulong image_start, int type,
445 void *load_buf, void *image_buf, ulong image_len,
446 uint unc_len, ulong *load_end)
447{
448 int ret = 0;
449
450 *load_end = load;
451 print_decomp_msg(comp, type, load == image_start);
452
453 /*
454 * Load the image to the right place, decompressing if needed. After
455 * this, image_len will be set to the number of uncompressed bytes
456 * loaded, ret will be non-zero on error.
457 */
458 switch (comp) {
459 case IH_COMP_NONE:
460 if (load == image_start)
461 break;
462 if (image_len <= unc_len)
463 memmove_wd(load_buf, image_buf, image_len, CHUNKSZ);
464 else
465 ret = -ENOSPC;
466 break;
Frieder Schrempf3bbe1a72021-02-05 16:11:06 -0800467#ifndef USE_HOSTCC
468#if CONFIG_IS_ENABLED(GZIP)
Julius Werner20908542019-07-24 19:37:54 -0700469 case IH_COMP_GZIP: {
470 ret = gunzip(load_buf, unc_len, image_buf, &image_len);
471 break;
472 }
473#endif /* CONFIG_GZIP */
Frieder Schrempf3bbe1a72021-02-05 16:11:06 -0800474#endif
475#ifndef USE_HOSTCC
476#if CONFIG_IS_ENABLED(BZIP2)
Julius Werner20908542019-07-24 19:37:54 -0700477 case IH_COMP_BZIP2: {
478 uint size = unc_len;
479
480 /*
481 * If we've got less than 4 MB of malloc() space,
482 * use slower decompression algorithm which requires
483 * at most 2300 KB of memory.
484 */
485 ret = BZ2_bzBuffToBuffDecompress(load_buf, &size,
486 image_buf, image_len,
487 CONFIG_SYS_MALLOC_LEN < (4096 * 1024), 0);
488 image_len = size;
489 break;
490 }
491#endif /* CONFIG_BZIP2 */
Frieder Schrempf3bbe1a72021-02-05 16:11:06 -0800492#endif
493#ifndef USE_HOSTCC
494#if CONFIG_IS_ENABLED(LZMA)
Julius Werner20908542019-07-24 19:37:54 -0700495 case IH_COMP_LZMA: {
496 SizeT lzma_len = unc_len;
497
498 ret = lzmaBuffToBuffDecompress(load_buf, &lzma_len,
499 image_buf, image_len);
500 image_len = lzma_len;
501 break;
502 }
503#endif /* CONFIG_LZMA */
Frieder Schrempf3bbe1a72021-02-05 16:11:06 -0800504#endif
505#ifndef USE_HOSTCC
506#if CONFIG_IS_ENABLED(LZO)
Julius Werner20908542019-07-24 19:37:54 -0700507 case IH_COMP_LZO: {
508 size_t size = unc_len;
509
510 ret = lzop_decompress(image_buf, image_len, load_buf, &size);
511 image_len = size;
512 break;
513 }
514#endif /* CONFIG_LZO */
Frieder Schrempf3bbe1a72021-02-05 16:11:06 -0800515#endif
516#ifndef USE_HOSTCC
517#if CONFIG_IS_ENABLED(LZ4)
Julius Werner20908542019-07-24 19:37:54 -0700518 case IH_COMP_LZ4: {
519 size_t size = unc_len;
520
521 ret = ulz4fn(image_buf, image_len, load_buf, &size);
522 image_len = size;
523 break;
524 }
525#endif /* CONFIG_LZ4 */
Frieder Schrempf3bbe1a72021-02-05 16:11:06 -0800526#endif
527#ifndef USE_HOSTCC
528#if CONFIG_IS_ENABLED(ZSTD)
Robert Marko26073f92020-04-25 19:37:21 +0200529 case IH_COMP_ZSTD: {
530 size_t size = unc_len;
531 ZSTD_DStream *dstream;
532 ZSTD_inBuffer in_buf;
533 ZSTD_outBuffer out_buf;
534 void *workspace;
535 size_t wsize;
536
537 wsize = ZSTD_DStreamWorkspaceBound(image_len);
538 workspace = malloc(wsize);
539 if (!workspace) {
540 debug("%s: cannot allocate workspace of size %zu\n", __func__,
541 wsize);
542 return -1;
543 }
544
545 dstream = ZSTD_initDStream(image_len, workspace, wsize);
546 if (!dstream) {
547 printf("%s: ZSTD_initDStream failed\n", __func__);
548 return ZSTD_getErrorCode(ret);
549 }
550
551 in_buf.src = image_buf;
552 in_buf.pos = 0;
553 in_buf.size = image_len;
554
555 out_buf.dst = load_buf;
556 out_buf.pos = 0;
557 out_buf.size = size;
558
559 while (1) {
560 size_t ret;
561
562 ret = ZSTD_decompressStream(dstream, &out_buf, &in_buf);
563 if (ZSTD_isError(ret)) {
564 printf("%s: ZSTD_decompressStream error %d\n", __func__,
565 ZSTD_getErrorCode(ret));
566 return ZSTD_getErrorCode(ret);
567 }
568
569 if (in_buf.pos >= image_len || !ret)
570 break;
571 }
572
573 image_len = out_buf.pos;
574
575 break;
576 }
577#endif /* CONFIG_ZSTD */
Frieder Schrempf3bbe1a72021-02-05 16:11:06 -0800578#endif
Julius Werner20908542019-07-24 19:37:54 -0700579 default:
580 printf("Unimplemented compression type %d\n", comp);
581 return -ENOSYS;
582 }
583
584 *load_end = load + image_len;
585
586 return ret;
587}
588
Marian Balakowicz570abb02008-02-29 15:59:59 +0100589
590#ifndef USE_HOSTCC
Tom Rinic76c93a2019-05-23 07:14:07 -0400591#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100592/**
593 * image_get_ramdisk - get and verify ramdisk image
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100594 * @rd_addr: ramdisk image start address
595 * @arch: expected ramdisk architecture
596 * @verify: checksum verification flag
597 *
598 * image_get_ramdisk() returns a pointer to the verified ramdisk image
599 * header. Routine receives image start address and expected architecture
600 * flag. Verification done covers data and header integrity and os/type/arch
601 * fields checking.
602 *
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100603 * returns:
604 * pointer to a ramdisk image header, if image was found and valid
Kumar Gala274cea22008-02-27 21:51:46 -0600605 * otherwise, return NULL
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100606 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000607static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
Marian Balakowiczd985c842008-03-12 10:14:38 +0100608 int verify)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100609{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200610 const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100611
Stephen Warren712fbcf2011-10-18 11:11:49 +0000612 if (!image_check_magic(rd_hdr)) {
613 puts("Bad Magic Number\n");
Simon Glass770605e2012-02-13 13:51:18 +0000614 bootstage_error(BOOTSTAGE_ID_RD_MAGIC);
Kumar Gala274cea22008-02-27 21:51:46 -0600615 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100616 }
617
Stephen Warren712fbcf2011-10-18 11:11:49 +0000618 if (!image_check_hcrc(rd_hdr)) {
619 puts("Bad Header Checksum\n");
Simon Glass770605e2012-02-13 13:51:18 +0000620 bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
Kumar Gala274cea22008-02-27 21:51:46 -0600621 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100622 }
623
Simon Glass770605e2012-02-13 13:51:18 +0000624 bootstage_mark(BOOTSTAGE_ID_RD_MAGIC);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000625 image_print_contents(rd_hdr);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100626
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100627 if (verify) {
628 puts(" Verifying Checksum ... ");
Stephen Warren712fbcf2011-10-18 11:11:49 +0000629 if (!image_check_dcrc(rd_hdr)) {
630 puts("Bad Data CRC\n");
Simon Glass770605e2012-02-13 13:51:18 +0000631 bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM);
Kumar Gala274cea22008-02-27 21:51:46 -0600632 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100633 }
634 puts("OK\n");
635 }
636
Simon Glass770605e2012-02-13 13:51:18 +0000637 bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100638
Stephen Warren712fbcf2011-10-18 11:11:49 +0000639 if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
640 !image_check_arch(rd_hdr, arch) ||
641 !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
642 printf("No Linux %s Ramdisk Image\n",
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100643 genimg_get_arch_name(arch));
Simon Glass770605e2012-02-13 13:51:18 +0000644 bootstage_error(BOOTSTAGE_ID_RAMDISK);
Kumar Gala274cea22008-02-27 21:51:46 -0600645 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100646 }
647
648 return rd_hdr;
649}
Heiko Schocher21d29f72014-05-28 11:33:33 +0200650#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100651#endif /* !USE_HOSTCC */
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100652
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100653/*****************************************************************************/
654/* Shared dual-format routines */
655/*****************************************************************************/
Marian Balakowicz570abb02008-02-29 15:59:59 +0100656#ifndef USE_HOSTCC
Simon Glassbb872dd2019-12-28 10:45:02 -0700657ulong image_load_addr = CONFIG_SYS_LOAD_ADDR; /* Default Load Address */
658ulong image_save_addr; /* Default Save Address */
659ulong image_save_size; /* Default Save Size (in bytes) */
Joe Hershberger1cf0a8b2012-12-11 22:16:28 -0600660
661static int on_loadaddr(const char *name, const char *value, enum env_op op,
662 int flags)
663{
664 switch (op) {
665 case env_op_create:
666 case env_op_overwrite:
Simon Glass7e5f4602021-07-24 09:03:29 -0600667 image_load_addr = hextoul(value, NULL);
Joe Hershberger1cf0a8b2012-12-11 22:16:28 -0600668 break;
669 default:
670 break;
671 }
672
673 return 0;
674}
675U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
676
Simon Glass723806c2017-08-03 12:22:15 -0600677ulong env_get_bootm_low(void)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100678{
Simon Glass00caae62017-08-03 12:22:12 -0600679 char *s = env_get("bootm_low");
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100680 if (s) {
Simon Glass7e5f4602021-07-24 09:03:29 -0600681 ulong tmp = hextoul(s, NULL);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100682 return tmp;
683 }
684
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200685#if defined(CONFIG_SYS_SDRAM_BASE)
686 return CONFIG_SYS_SDRAM_BASE;
Samuel Hollanddd573b62021-09-12 11:26:45 -0500687#elif defined(CONFIG_ARM) || defined(CONFIG_MICROBLAZE) || defined(CONFIG_RISCV)
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100688 return gd->bd->bi_dram[0].start;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100689#else
690 return 0;
691#endif
692}
693
Simon Glass723806c2017-08-03 12:22:15 -0600694phys_size_t env_get_bootm_size(void)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100695{
Masahiro Yamada0cb389d2016-02-05 16:12:50 +0900696 phys_size_t tmp, size;
697 phys_addr_t start;
Simon Glass00caae62017-08-03 12:22:12 -0600698 char *s = env_get("bootm_size");
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100699 if (s) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000700 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100701 return tmp;
702 }
Masahiro Yamada0cb389d2016-02-05 16:12:50 +0900703
Stefan Roese4963f632020-08-12 12:57:18 +0200704 start = gd->ram_base;
705 size = gd->ram_size;
Masahiro Yamada0cb389d2016-02-05 16:12:50 +0900706
Baruch Siach7f98b4e2020-08-24 20:14:05 +0300707 if (start + size > gd->ram_top)
708 size = gd->ram_top - start;
709
Simon Glass00caae62017-08-03 12:22:12 -0600710 s = env_get("bootm_low");
Matthew McClintockc519fac2010-07-08 10:11:08 -0500711 if (s)
Stephen Warren712fbcf2011-10-18 11:11:49 +0000712 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
Matthew McClintockc519fac2010-07-08 10:11:08 -0500713 else
Masahiro Yamada0cb389d2016-02-05 16:12:50 +0900714 tmp = start;
Matthew McClintockc519fac2010-07-08 10:11:08 -0500715
Masahiro Yamada0cb389d2016-02-05 16:12:50 +0900716 return size - (tmp - start);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100717}
718
Simon Glass723806c2017-08-03 12:22:15 -0600719phys_size_t env_get_bootm_mapsize(void)
Grant Likelyc3624e62011-03-28 09:58:43 +0000720{
721 phys_size_t tmp;
Simon Glass00caae62017-08-03 12:22:12 -0600722 char *s = env_get("bootm_mapsize");
Grant Likelyc3624e62011-03-28 09:58:43 +0000723 if (s) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000724 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
Grant Likelyc3624e62011-03-28 09:58:43 +0000725 return tmp;
726 }
727
728#if defined(CONFIG_SYS_BOOTMAPSZ)
729 return CONFIG_SYS_BOOTMAPSZ;
730#else
Simon Glass723806c2017-08-03 12:22:15 -0600731 return env_get_bootm_size();
Grant Likelyc3624e62011-03-28 09:58:43 +0000732#endif
733}
734
Stephen Warren712fbcf2011-10-18 11:11:49 +0000735void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100736{
Larry Johnson54fa2c52010-04-20 08:09:43 -0400737 if (to == from)
738 return;
739
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100740#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
Sonic Zhang22cfddc2014-12-10 18:20:53 +0800741 if (to > from) {
742 from += len;
743 to += len;
744 }
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100745 while (len > 0) {
746 size_t tail = (len > chunksz) ? chunksz : len;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000747 WATCHDOG_RESET();
Sonic Zhang22cfddc2014-12-10 18:20:53 +0800748 if (to > from) {
749 to -= tail;
750 from -= tail;
751 }
Stephen Warren712fbcf2011-10-18 11:11:49 +0000752 memmove(to, from, tail);
Sonic Zhang22cfddc2014-12-10 18:20:53 +0800753 if (to < from) {
754 to += tail;
755 from += tail;
756 }
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100757 len -= tail;
758 }
759#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000760 memmove(to, from, len);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100761#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
762}
Julius Werner20908542019-07-24 19:37:54 -0700763#else /* USE_HOSTCC */
764void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
765{
766 memmove(to, from, len);
767}
Marian Balakowicz570abb02008-02-29 15:59:59 +0100768#endif /* !USE_HOSTCC */
769
Stephen Warren712fbcf2011-10-18 11:11:49 +0000770void genimg_print_size(uint32_t size)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100771{
772#ifndef USE_HOSTCC
Stephen Warren712fbcf2011-10-18 11:11:49 +0000773 printf("%d Bytes = ", size);
774 print_size(size, "\n");
Marian Balakowicz570abb02008-02-29 15:59:59 +0100775#else
xypron.glpk@gmx.decec85d42017-01-04 14:04:44 +0100776 printf("%d Bytes = %.2f KiB = %.2f MiB\n",
Marian Balakowicz570abb02008-02-29 15:59:59 +0100777 size, (double)size / 1.024e3,
778 (double)size / 1.048576e6);
779#endif
780}
781
Simon Glass859e92b2013-05-07 06:11:51 +0000782#if IMAGE_ENABLE_TIMESTAMP
783void genimg_print_time(time_t timestamp)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100784{
785#ifndef USE_HOSTCC
786 struct rtc_time tm;
787
Simon Glass9f9276c2015-04-20 12:37:18 -0600788 rtc_to_tm(timestamp, &tm);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000789 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
Marian Balakowicz570abb02008-02-29 15:59:59 +0100790 tm.tm_year, tm.tm_mon, tm.tm_mday,
791 tm.tm_hour, tm.tm_min, tm.tm_sec);
792#else
Stephen Warren712fbcf2011-10-18 11:11:49 +0000793 printf("%s", ctime(&timestamp));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100794#endif
795}
Simon Glass859e92b2013-05-07 06:11:51 +0000796#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100797
Simon Glass5b9d44d2015-06-23 15:38:25 -0600798const table_entry_t *get_table_entry(const table_entry_t *table, int id)
799{
800 for (; table->id >= 0; ++table) {
801 if (table->id == id)
802 return table;
803 }
804 return NULL;
805}
806
Simon Glass14262202016-06-30 10:52:16 -0600807static const char *unknown_msg(enum ih_category category)
808{
Simon Glassae3de0d2016-10-31 10:21:09 -0600809 static const char unknown_str[] = "Unknown ";
Simon Glass14262202016-06-30 10:52:16 -0600810 static char msg[30];
811
Simon Glassae3de0d2016-10-31 10:21:09 -0600812 strcpy(msg, unknown_str);
813 strncat(msg, table_info[category].desc,
814 sizeof(msg) - sizeof(unknown_str));
Simon Glass14262202016-06-30 10:52:16 -0600815
816 return msg;
817}
818
819/**
Naoki Hayama898a0842020-10-07 11:22:24 +0900820 * genimg_get_cat_name - translate entry id to long name
Simon Glass14262202016-06-30 10:52:16 -0600821 * @category: category to look up (enum ih_category)
822 * @id: entry id to be translated
823 *
824 * This will scan the translation table trying to find the entry that matches
825 * the given id.
826 *
Naoki Hayama898a0842020-10-07 11:22:24 +0900827 * @return long entry name if translation succeeds; error string on failure
Simon Glass14262202016-06-30 10:52:16 -0600828 */
829const char *genimg_get_cat_name(enum ih_category category, uint id)
830{
831 const table_entry_t *entry;
832
833 entry = get_table_entry(table_info[category].table, id);
834 if (!entry)
835 return unknown_msg(category);
836#if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
837 return entry->lname;
838#else
839 return entry->lname + gd->reloc_off;
840#endif
841}
842
843/**
Naoki Hayama898a0842020-10-07 11:22:24 +0900844 * genimg_get_cat_short_name - translate entry id to short name
Simon Glass14262202016-06-30 10:52:16 -0600845 * @category: category to look up (enum ih_category)
846 * @id: entry id to be translated
847 *
848 * This will scan the translation table trying to find the entry that matches
849 * the given id.
850 *
Naoki Hayama898a0842020-10-07 11:22:24 +0900851 * @return short entry name if translation succeeds; error string on failure
Simon Glass14262202016-06-30 10:52:16 -0600852 */
853const char *genimg_get_cat_short_name(enum ih_category category, uint id)
854{
855 const table_entry_t *entry;
856
857 entry = get_table_entry(table_info[category].table, id);
858 if (!entry)
859 return unknown_msg(category);
860#if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
861 return entry->sname;
862#else
863 return entry->sname + gd->reloc_off;
864#endif
865}
866
867int genimg_get_cat_count(enum ih_category category)
868{
869 return table_info[category].count;
870}
871
872const char *genimg_get_cat_desc(enum ih_category category)
873{
874 return table_info[category].desc;
875}
876
Marian Balakowicz570abb02008-02-29 15:59:59 +0100877/**
Naoki Hayama02d41b02020-10-07 11:21:25 +0900878 * genimg_cat_has_id - check whether category has entry id
879 * @category: category to look up (enum ih_category)
880 * @id: entry id to be checked
881 *
882 * This will scan the translation table trying to find the entry that matches
883 * the given id.
884 *
885 * @return true if category has entry id; false if not
886 */
887bool genimg_cat_has_id(enum ih_category category, uint id)
888{
889 if (get_table_entry(table_info[category].table, id))
890 return true;
891
892 return false;
893}
894
895/**
Marian Balakowicz570abb02008-02-29 15:59:59 +0100896 * get_table_entry_name - translate entry id to long name
897 * @table: pointer to a translation table for entries of a specific type
898 * @msg: message to be returned when translation fails
899 * @id: entry id to be translated
900 *
901 * get_table_entry_name() will go over translation table trying to find
902 * entry that matches given id. If matching entry is found, its long
903 * name is returned to the caller.
904 *
905 * returns:
906 * long entry name if translation succeeds
907 * msg otherwise
908 */
Mike Frysinger7edb1862010-10-20 07:17:39 -0400909char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100910{
Simon Glass5b9d44d2015-06-23 15:38:25 -0600911 table = get_table_entry(table, id);
912 if (!table)
913 return msg;
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200914#if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
Simon Glass5b9d44d2015-06-23 15:38:25 -0600915 return table->lname;
Scott Woode3d1ac72009-04-02 16:15:10 -0500916#else
Simon Glass5b9d44d2015-06-23 15:38:25 -0600917 return table->lname + gd->reloc_off;
Scott Woode3d1ac72009-04-02 16:15:10 -0500918#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100919}
920
Stephen Warren712fbcf2011-10-18 11:11:49 +0000921const char *genimg_get_os_name(uint8_t os)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100922{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000923 return (get_table_entry_name(uimage_os, "Unknown OS", os));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100924}
925
Stephen Warren712fbcf2011-10-18 11:11:49 +0000926const char *genimg_get_arch_name(uint8_t arch)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100927{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000928 return (get_table_entry_name(uimage_arch, "Unknown Architecture",
929 arch));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100930}
931
Stephen Warren712fbcf2011-10-18 11:11:49 +0000932const char *genimg_get_type_name(uint8_t type)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100933{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000934 return (get_table_entry_name(uimage_type, "Unknown Image", type));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100935}
936
Naoki Hayama898a0842020-10-07 11:22:24 +0900937const char *genimg_get_comp_name(uint8_t comp)
938{
939 return (get_table_entry_name(uimage_comp, "Unknown Compression",
940 comp));
941}
942
Simon Glasscef2e512016-02-22 22:55:50 -0700943static const char *genimg_get_short_name(const table_entry_t *table, int val)
Simon Glass5b9d44d2015-06-23 15:38:25 -0600944{
Simon Glasscef2e512016-02-22 22:55:50 -0700945 table = get_table_entry(table, val);
Simon Glass5b9d44d2015-06-23 15:38:25 -0600946 if (!table)
947 return "unknown";
948#if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
949 return table->sname;
950#else
951 return table->sname + gd->reloc_off;
952#endif
953}
954
Simon Glasscef2e512016-02-22 22:55:50 -0700955const char *genimg_get_type_short_name(uint8_t type)
956{
957 return genimg_get_short_name(uimage_type, type);
958}
959
Simon Glasscef2e512016-02-22 22:55:50 -0700960const char *genimg_get_comp_short_name(uint8_t comp)
961{
962 return genimg_get_short_name(uimage_comp, comp);
963}
964
965const char *genimg_get_os_short_name(uint8_t os)
966{
967 return genimg_get_short_name(uimage_os, os);
968}
969
970const char *genimg_get_arch_short_name(uint8_t arch)
971{
972 return genimg_get_short_name(uimage_arch, arch);
973}
974
Marian Balakowicz570abb02008-02-29 15:59:59 +0100975/**
976 * get_table_entry_id - translate short entry name to id
977 * @table: pointer to a translation table for entries of a specific type
978 * @table_name: to be used in case of error
979 * @name: entry short name to be translated
980 *
981 * get_table_entry_id() will go over translation table trying to find
982 * entry that matches given short name. If matching entry is found,
983 * its id returned to the caller.
984 *
985 * returns:
986 * entry id if translation succeeds
987 * -1 otherwise
988 */
Mike Frysinger7edb1862010-10-20 07:17:39 -0400989int get_table_entry_id(const table_entry_t *table,
Marian Balakowicz570abb02008-02-29 15:59:59 +0100990 const char *table_name, const char *name)
991{
Mike Frysinger7edb1862010-10-20 07:17:39 -0400992 const table_entry_t *t;
Marian Balakowicz570abb02008-02-29 15:59:59 +0100993
994 for (t = table; t->id >= 0; ++t) {
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200995#ifdef CONFIG_NEEDS_MANUAL_RELOC
Simon Glass5b9d44d2015-06-23 15:38:25 -0600996 if (t->sname && strcasecmp(t->sname + gd->reloc_off, name) == 0)
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200997#else
Simon Glass5b9d44d2015-06-23 15:38:25 -0600998 if (t->sname && strcasecmp(t->sname, name) == 0)
Peter Tyser521af042009-09-21 11:20:36 -0500999#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +01001000 return (t->id);
1001 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00001002 debug("Invalid %s Type: %s\n", table_name, name);
Simon Glass5b9d44d2015-06-23 15:38:25 -06001003
1004 return -1;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001005}
1006
Stephen Warren712fbcf2011-10-18 11:11:49 +00001007int genimg_get_os_id(const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001008{
Stephen Warren712fbcf2011-10-18 11:11:49 +00001009 return (get_table_entry_id(uimage_os, "OS", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001010}
1011
Stephen Warren712fbcf2011-10-18 11:11:49 +00001012int genimg_get_arch_id(const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001013{
Stephen Warren712fbcf2011-10-18 11:11:49 +00001014 return (get_table_entry_id(uimage_arch, "CPU", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001015}
1016
Stephen Warren712fbcf2011-10-18 11:11:49 +00001017int genimg_get_type_id(const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001018{
Stephen Warren712fbcf2011-10-18 11:11:49 +00001019 return (get_table_entry_id(uimage_type, "Image", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001020}
1021
Stephen Warren712fbcf2011-10-18 11:11:49 +00001022int genimg_get_comp_id(const char *name)
Marian Balakowicz570abb02008-02-29 15:59:59 +01001023{
Stephen Warren712fbcf2011-10-18 11:11:49 +00001024 return (get_table_entry_id(uimage_comp, "Compression", name));
Marian Balakowicz570abb02008-02-29 15:59:59 +01001025}
1026
1027#ifndef USE_HOSTCC
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001028/**
Bryan Wu6c454fe2014-08-15 16:51:38 -07001029 * genimg_get_kernel_addr_fit - get the real kernel address and return 2
1030 * FIT strings
Bryan Wu0f641402014-07-31 17:39:58 -07001031 * @img_addr: a string might contain real image address
Bryan Wu6c454fe2014-08-15 16:51:38 -07001032 * @fit_uname_config: double pointer to a char, will hold pointer to a
1033 * configuration unit name
1034 * @fit_uname_kernel: double pointer to a char, will hold pointer to a subimage
1035 * name
Bryan Wu0f641402014-07-31 17:39:58 -07001036 *
Bryan Wu6c454fe2014-08-15 16:51:38 -07001037 * genimg_get_kernel_addr_fit get the real kernel start address from a string
Bryan Wu0f641402014-07-31 17:39:58 -07001038 * which is normally the first argv of bootm/bootz
1039 *
1040 * returns:
1041 * kernel start address
1042 */
Bryan Wu6c454fe2014-08-15 16:51:38 -07001043ulong genimg_get_kernel_addr_fit(char * const img_addr,
1044 const char **fit_uname_config,
1045 const char **fit_uname_kernel)
Bryan Wu0f641402014-07-31 17:39:58 -07001046{
Bryan Wu0f641402014-07-31 17:39:58 -07001047 ulong kernel_addr;
1048
1049 /* find out kernel image address */
1050 if (!img_addr) {
Simon Glassbb872dd2019-12-28 10:45:02 -07001051 kernel_addr = image_load_addr;
Bryan Wu0f641402014-07-31 17:39:58 -07001052 debug("* kernel: default image load address = 0x%08lx\n",
Simon Glassbb872dd2019-12-28 10:45:02 -07001053 image_load_addr);
Simon Glass73223f02016-02-22 22:55:43 -07001054#if CONFIG_IS_ENABLED(FIT)
Simon Glassbb872dd2019-12-28 10:45:02 -07001055 } else if (fit_parse_conf(img_addr, image_load_addr, &kernel_addr,
Bryan Wu6c454fe2014-08-15 16:51:38 -07001056 fit_uname_config)) {
Bryan Wu0f641402014-07-31 17:39:58 -07001057 debug("* kernel: config '%s' from image at 0x%08lx\n",
Bryan Wu6c454fe2014-08-15 16:51:38 -07001058 *fit_uname_config, kernel_addr);
Simon Glassbb872dd2019-12-28 10:45:02 -07001059 } else if (fit_parse_subimage(img_addr, image_load_addr, &kernel_addr,
Bryan Wu6c454fe2014-08-15 16:51:38 -07001060 fit_uname_kernel)) {
Bryan Wu0f641402014-07-31 17:39:58 -07001061 debug("* kernel: subimage '%s' from image at 0x%08lx\n",
Bryan Wu6c454fe2014-08-15 16:51:38 -07001062 *fit_uname_kernel, kernel_addr);
Bryan Wu0f641402014-07-31 17:39:58 -07001063#endif
1064 } else {
Simon Glass7e5f4602021-07-24 09:03:29 -06001065 kernel_addr = hextoul(img_addr, NULL);
Bryan Wu0f641402014-07-31 17:39:58 -07001066 debug("* kernel: cmdline image address = 0x%08lx\n",
1067 kernel_addr);
1068 }
1069
1070 return kernel_addr;
1071}
1072
1073/**
Bryan Wu6c454fe2014-08-15 16:51:38 -07001074 * genimg_get_kernel_addr() is the simple version of
1075 * genimg_get_kernel_addr_fit(). It ignores those return FIT strings
1076 */
1077ulong genimg_get_kernel_addr(char * const img_addr)
1078{
1079 const char *fit_uname_config = NULL;
1080 const char *fit_uname_kernel = NULL;
1081
1082 return genimg_get_kernel_addr_fit(img_addr, &fit_uname_config,
1083 &fit_uname_kernel);
1084}
1085
1086/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001087 * genimg_get_format - get image format type
1088 * @img_addr: image start address
1089 *
1090 * genimg_get_format() checks whether provided address points to a valid
1091 * legacy or FIT image.
1092 *
1093 * New uImage format and FDT blob are based on a libfdt. FDT blob
1094 * may be passed directly or embedded in a FIT image. In both situations
1095 * genimg_get_format() must be able to dectect libfdt header.
1096 *
1097 * returns:
1098 * image format type or IMAGE_FORMAT_INVALID if no image is present
1099 */
Simon Glass35e7b0f2013-05-07 06:12:03 +00001100int genimg_get_format(const void *img_addr)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001101{
Tom Rinic76c93a2019-05-23 07:14:07 -04001102#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Wolfgang Denk3a2003f2009-08-19 11:42:56 +02001103 const image_header_t *hdr;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001104
Wolfgang Denk3a2003f2009-08-19 11:42:56 +02001105 hdr = (const image_header_t *)img_addr;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001106 if (image_check_magic(hdr))
Heiko Schocher21d29f72014-05-28 11:33:33 +02001107 return IMAGE_FORMAT_LEGACY;
1108#endif
Simon Glassaa34fbc2016-02-22 22:55:45 -07001109#if IMAGE_ENABLE_FIT || IMAGE_ENABLE_OF_LIBFDT
Heiko Schocher21d29f72014-05-28 11:33:33 +02001110 if (fdt_check_header(img_addr) == 0)
1111 return IMAGE_FORMAT_FIT;
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -05001112#endif
1113#ifdef CONFIG_ANDROID_BOOT_IMAGE
Heiko Schocher21d29f72014-05-28 11:33:33 +02001114 if (android_image_check_header(img_addr) == 0)
1115 return IMAGE_FORMAT_ANDROID;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001116#endif
1117
Heiko Schocher21d29f72014-05-28 11:33:33 +02001118 return IMAGE_FORMAT_INVALID;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001119}
1120
1121/**
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001122 * fit_has_config - check if there is a valid FIT configuration
1123 * @images: pointer to the bootm command headers structure
1124 *
1125 * fit_has_config() checks if there is a FIT configuration in use
1126 * (if FTI support is present).
1127 *
1128 * returns:
1129 * 0, no FIT support or no configuration found
1130 * 1, configuration found
1131 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001132int genimg_has_config(bootm_headers_t *images)
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001133{
Simon Glass73223f02016-02-22 22:55:43 -07001134#if IMAGE_ENABLE_FIT
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001135 if (images->fit_uname_cfg)
1136 return 1;
1137#endif
1138 return 0;
1139}
1140
1141/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001142 * boot_get_ramdisk - main ramdisk handling routine
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001143 * @argc: command argument count
1144 * @argv: command argument list
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +01001145 * @images: pointer to the bootm images structure
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001146 * @arch: expected ramdisk architecture
1147 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
1148 * @rd_end: pointer to a ulong variable, will hold ramdisk end
1149 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001150 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001151 * Curently supported are the following ramdisk sources:
1152 * - multicomponent kernel/ramdisk image,
1153 * - commandline provided address of decicated ramdisk image.
1154 *
1155 * returns:
Marian Balakowiczd985c842008-03-12 10:14:38 +01001156 * 0, if ramdisk image was found and valid, or skiped
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001157 * rd_start and rd_end are set to ramdisk start/end addresses if
1158 * ramdisk image is found and valid
Marian Balakowiczd985c842008-03-12 10:14:38 +01001159 *
Kumar Galaea86b9e2008-08-29 19:08:29 -05001160 * 1, if ramdisk image is found but corrupted, or invalid
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001161 * rd_start and rd_end are set to 0 if no ramdisk exists
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001162 */
Simon Glass09140112020-05-10 11:40:03 -06001163int boot_get_ramdisk(int argc, char *const argv[], bootm_headers_t *images,
1164 uint8_t arch, ulong *rd_start, ulong *rd_end)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001165{
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001166 ulong rd_addr, rd_load;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001167 ulong rd_data, rd_len;
Tom Rinic76c93a2019-05-23 07:14:07 -04001168#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Wolfgang Denk3a2003f2009-08-19 11:42:56 +02001169 const image_header_t *rd_hdr;
Heiko Schocher21d29f72014-05-28 11:33:33 +02001170#endif
Simon Glass35e7b0f2013-05-07 06:12:03 +00001171 void *buf;
Marek Vasut57d40ab2012-03-30 23:19:10 +02001172#ifdef CONFIG_SUPPORT_RAW_INITRD
Marek Vasut017e1f32012-03-18 11:47:58 +00001173 char *end;
Marek Vasut57d40ab2012-03-30 23:19:10 +02001174#endif
Simon Glass73223f02016-02-22 22:55:43 -07001175#if IMAGE_ENABLE_FIT
Simon Glassf320a4d2013-07-10 23:08:10 -07001176 const char *fit_uname_config = images->fit_uname_cfg;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001177 const char *fit_uname_ramdisk = NULL;
1178 ulong default_addr;
Marian Balakowiczc8779642008-03-12 10:12:37 +01001179 int rd_noffset;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001180#endif
Simon Glass983c72f2013-06-11 11:14:46 -07001181 const char *select = NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001182
Marian Balakowiczc8779642008-03-12 10:12:37 +01001183 *rd_start = 0;
1184 *rd_end = 0;
1185
Tom Rini1fec3c52015-08-27 15:42:41 -04001186#ifdef CONFIG_ANDROID_BOOT_IMAGE
1187 /*
1188 * Look for an Android boot image.
1189 */
1190 buf = map_sysmem(images->os.start, 0);
Tom Rinic139b5f2015-10-27 19:04:40 -04001191 if (buf && genimg_get_format(buf) == IMAGE_FORMAT_ANDROID)
Shawn Guo5e218862019-02-22 16:28:06 +08001192 select = (argc == 0) ? env_get("loadaddr") : argv[0];
Tom Rini1fec3c52015-08-27 15:42:41 -04001193#endif
1194
Simon Glass983c72f2013-06-11 11:14:46 -07001195 if (argc >= 2)
1196 select = argv[1];
Rob Herring2dd46322015-07-17 10:57:17 -05001197
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001198 /*
1199 * Look for a '-' which indicates to ignore the
1200 * ramdisk argument
1201 */
Simon Glass983c72f2013-06-11 11:14:46 -07001202 if (select && strcmp(select, "-") == 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001203 debug("## Skipping init Ramdisk\n");
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001204 rd_len = rd_data = 0;
Simon Glass983c72f2013-06-11 11:14:46 -07001205 } else if (select || genimg_has_config(images)) {
Simon Glass73223f02016-02-22 22:55:43 -07001206#if IMAGE_ENABLE_FIT
Simon Glass983c72f2013-06-11 11:14:46 -07001207 if (select) {
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001208 /*
1209 * If the init ramdisk comes from the FIT image and
1210 * the FIT image address is omitted in the command
1211 * line argument, try to use os FIT image address or
1212 * default load address.
1213 */
1214 if (images->fit_uname_os)
1215 default_addr = (ulong)images->fit_hdr_os;
1216 else
Simon Glassbb872dd2019-12-28 10:45:02 -07001217 default_addr = image_load_addr;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001218
Simon Glass983c72f2013-06-11 11:14:46 -07001219 if (fit_parse_conf(select, default_addr,
1220 &rd_addr, &fit_uname_config)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001221 debug("* ramdisk: config '%s' from image at "
1222 "0x%08lx\n",
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001223 fit_uname_config, rd_addr);
Simon Glass983c72f2013-06-11 11:14:46 -07001224 } else if (fit_parse_subimage(select, default_addr,
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001225 &rd_addr, &fit_uname_ramdisk)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001226 debug("* ramdisk: subimage '%s' from image at "
1227 "0x%08lx\n",
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001228 fit_uname_ramdisk, rd_addr);
1229 } else
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001230#endif
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001231 {
Simon Glass7e5f4602021-07-24 09:03:29 -06001232 rd_addr = hextoul(select, NULL);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001233 debug("* ramdisk: cmdline image address = "
1234 "0x%08lx\n",
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001235 rd_addr);
1236 }
Simon Glass73223f02016-02-22 22:55:43 -07001237#if IMAGE_ENABLE_FIT
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001238 } else {
1239 /* use FIT configuration provided in first bootm
Simon Glassa51ec632013-05-16 13:53:22 +00001240 * command argument. If the property is not defined,
1241 * quit silently.
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001242 */
Simon Glass35e7b0f2013-05-07 06:12:03 +00001243 rd_addr = map_to_sysmem(images->fit_hdr_os);
Simon Glassa51ec632013-05-16 13:53:22 +00001244 rd_noffset = fit_get_node_from_config(images,
1245 FIT_RAMDISK_PROP, rd_addr);
Paul Burtonbd86ef12016-09-20 18:17:12 +01001246 if (rd_noffset == -ENOENT)
Peter Tyser41266c92008-08-05 10:51:57 -05001247 return 0;
Simon Glassa51ec632013-05-16 13:53:22 +00001248 else if (rd_noffset < 0)
1249 return 1;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001250 }
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001251#endif
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001252
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001253 /*
1254 * Check if there is an initrd image at the
1255 * address provided in the second bootm argument
1256 * check image type, for FIT images get FIT node.
1257 */
Simon Glass35e7b0f2013-05-07 06:12:03 +00001258 buf = map_sysmem(rd_addr, 0);
1259 switch (genimg_get_format(buf)) {
Tom Rinic76c93a2019-05-23 07:14:07 -04001260#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001261 case IMAGE_FORMAT_LEGACY:
Stephen Warren712fbcf2011-10-18 11:11:49 +00001262 printf("## Loading init Ramdisk from Legacy "
Marian Balakowiczc8779642008-03-12 10:12:37 +01001263 "Image at %08lx ...\n", rd_addr);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001264
Simon Glass770605e2012-02-13 13:51:18 +00001265 bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001266 rd_hdr = image_get_ramdisk(rd_addr, arch,
Marian Balakowiczd985c842008-03-12 10:14:38 +01001267 images->verify);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001268
Marian Balakowiczc8779642008-03-12 10:12:37 +01001269 if (rd_hdr == NULL)
Kumar Gala274cea22008-02-27 21:51:46 -06001270 return 1;
Kumar Gala274cea22008-02-27 21:51:46 -06001271
Stephen Warren712fbcf2011-10-18 11:11:49 +00001272 rd_data = image_get_data(rd_hdr);
1273 rd_len = image_get_data_size(rd_hdr);
1274 rd_load = image_get_load(rd_hdr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001275 break;
Heiko Schocher21d29f72014-05-28 11:33:33 +02001276#endif
Simon Glass73223f02016-02-22 22:55:43 -07001277#if IMAGE_ENABLE_FIT
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001278 case IMAGE_FORMAT_FIT:
Simon Glass126cc862014-06-12 07:24:47 -06001279 rd_noffset = fit_image_load(images,
Simon Glassa51ec632013-05-16 13:53:22 +00001280 rd_addr, &fit_uname_ramdisk,
Simon Glassf320a4d2013-07-10 23:08:10 -07001281 &fit_uname_config, arch,
Simon Glassa51ec632013-05-16 13:53:22 +00001282 IH_TYPE_RAMDISK,
1283 BOOTSTAGE_ID_FIT_RD_START,
Simon Glassfe20a812014-08-22 14:26:43 -06001284 FIT_LOAD_OPTIONAL_NON_ZERO,
1285 &rd_data, &rd_len);
Simon Glassa51ec632013-05-16 13:53:22 +00001286 if (rd_noffset < 0)
Michal Simekc78fce62008-07-11 10:43:13 +02001287 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +01001288
Simon Glassa51ec632013-05-16 13:53:22 +00001289 images->fit_hdr_rd = map_sysmem(rd_addr, 0);
Marian Balakowiczc8779642008-03-12 10:12:37 +01001290 images->fit_uname_rd = fit_uname_ramdisk;
Marian Balakowicz3dfe1102008-03-12 10:32:59 +01001291 images->fit_noffset_rd = rd_noffset;
Marian Balakowiczc8779642008-03-12 10:12:37 +01001292 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001293#endif
Rob Herring2dd46322015-07-17 10:57:17 -05001294#ifdef CONFIG_ANDROID_BOOT_IMAGE
1295 case IMAGE_FORMAT_ANDROID:
1296 android_image_get_ramdisk((void *)images->os.start,
1297 &rd_data, &rd_len);
1298 break;
1299#endif
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001300 default:
Marek Vasut017e1f32012-03-18 11:47:58 +00001301#ifdef CONFIG_SUPPORT_RAW_INITRD
Simon Glass983c72f2013-06-11 11:14:46 -07001302 end = NULL;
1303 if (select)
1304 end = strchr(select, ':');
1305 if (end) {
Simon Glass7e5f4602021-07-24 09:03:29 -06001306 rd_len = hextoul(++end, NULL);
Marek Vasut017e1f32012-03-18 11:47:58 +00001307 rd_data = rd_addr;
1308 } else
1309#endif
1310 {
1311 puts("Wrong Ramdisk Image Format\n");
1312 rd_data = rd_len = rd_load = 0;
1313 return 1;
1314 }
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001315 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001316 } else if (images->legacy_hdr_valid &&
Stephen Warren712fbcf2011-10-18 11:11:49 +00001317 image_check_type(&images->legacy_hdr_os_copy,
1318 IH_TYPE_MULTI)) {
1319
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001320 /*
1321 * Now check if we have a legacy mult-component image,
1322 * get second entry data start address and len.
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001323 */
Simon Glass770605e2012-02-13 13:51:18 +00001324 bootstage_mark(BOOTSTAGE_ID_RAMDISK);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001325 printf("## Loading init Ramdisk from multi component "
Marian Balakowiczc8779642008-03-12 10:12:37 +01001326 "Legacy Image at %08lx ...\n",
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001327 (ulong)images->legacy_hdr_os);
1328
Stephen Warren712fbcf2011-10-18 11:11:49 +00001329 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
Rob Herring2dd46322015-07-17 10:57:17 -05001330 } else {
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001331 /*
1332 * no initrd image
1333 */
Simon Glass770605e2012-02-13 13:51:18 +00001334 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001335 rd_len = rd_data = 0;
1336 }
1337
1338 if (!rd_data) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001339 debug("## No init Ramdisk\n");
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001340 } else {
1341 *rd_start = rd_data;
1342 *rd_end = rd_data + rd_len;
1343 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00001344 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001345 *rd_start, *rd_end);
Kumar Gala274cea22008-02-27 21:51:46 -06001346
1347 return 0;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001348}
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001349
John Rigbyfca43cc2010-10-13 13:57:35 -06001350#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001351/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001352 * boot_ramdisk_high - relocate init ramdisk
Kumar Galae822d7f2008-02-27 21:51:49 -06001353 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001354 * @rd_data: ramdisk data start address
1355 * @rd_len: ramdisk data length
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001356 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
1357 * start address (after possible relocation)
1358 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
1359 * end address (after possible relocation)
1360 *
Robert P. J. Day1bce2ae2013-09-16 07:15:45 -04001361 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001362 * variable and if requested ramdisk data is moved to a specified location.
1363 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001364 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1365 * start/end addresses if ramdisk image start and len were provided,
1366 * otherwise set initrd_start and initrd_end set to zeros.
1367 *
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001368 * returns:
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001369 * 0 - success
1370 * -1 - failure
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001371 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001372int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
Kumar Galae822d7f2008-02-27 21:51:49 -06001373 ulong *initrd_start, ulong *initrd_end)
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001374{
1375 char *s;
1376 ulong initrd_high;
1377 int initrd_copy_to_ram = 1;
1378
Simon Glass00caae62017-08-03 12:22:12 -06001379 s = env_get("initrd_high");
1380 if (s) {
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001381 /* a value of "no" or a similar string will act like 0,
1382 * turning the "load high" feature off. This is intentional.
1383 */
Simon Glass7e5f4602021-07-24 09:03:29 -06001384 initrd_high = hextoul(s, NULL);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001385 if (initrd_high == ~0)
1386 initrd_copy_to_ram = 0;
1387 } else {
Simon Glass723806c2017-08-03 12:22:15 -06001388 initrd_high = env_get_bootm_mapsize() + env_get_bootm_low();
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001389 }
1390
Marian Balakowicz95d449a2008-05-13 15:53:29 +02001391
Stephen Warren712fbcf2011-10-18 11:11:49 +00001392 debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001393 initrd_high, initrd_copy_to_ram);
1394
1395 if (rd_data) {
1396 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001397 debug(" in-place initrd\n");
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001398 *initrd_start = rd_data;
1399 *initrd_end = rd_data + rd_len;
Kumar Galae822d7f2008-02-27 21:51:49 -06001400 lmb_reserve(lmb, rd_data, rd_len);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001401 } else {
Kumar Galae822d7f2008-02-27 21:51:49 -06001402 if (initrd_high)
Stephen Warren712fbcf2011-10-18 11:11:49 +00001403 *initrd_start = (ulong)lmb_alloc_base(lmb,
1404 rd_len, 0x1000, initrd_high);
Kumar Galae822d7f2008-02-27 21:51:49 -06001405 else
Stephen Warren712fbcf2011-10-18 11:11:49 +00001406 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
1407 0x1000);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001408
Kumar Galae822d7f2008-02-27 21:51:49 -06001409 if (*initrd_start == 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001410 puts("ramdisk - allocation error\n");
Kumar Galae822d7f2008-02-27 21:51:49 -06001411 goto error;
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001412 }
Simon Glass770605e2012-02-13 13:51:18 +00001413 bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001414
1415 *initrd_end = *initrd_start + rd_len;
Stephen Warren712fbcf2011-10-18 11:11:49 +00001416 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001417 *initrd_start, *initrd_end);
1418
Stephen Warren712fbcf2011-10-18 11:11:49 +00001419 memmove_wd((void *)*initrd_start,
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001420 (void *)rd_data, rd_len, CHUNKSZ);
1421
Kumar Gala3b200112011-12-07 04:42:58 +00001422#ifdef CONFIG_MP
1423 /*
1424 * Ensure the image is flushed to memory to handle
1425 * AMP boot scenarios in which we might not be
1426 * HW cache coherent
1427 */
Heiko Schocher1a1e7072017-12-14 11:19:22 +01001428 flush_cache((unsigned long)*initrd_start,
1429 ALIGN(rd_len, ARCH_DMA_MINALIGN));
Kumar Gala3b200112011-12-07 04:42:58 +00001430#endif
Stephen Warren712fbcf2011-10-18 11:11:49 +00001431 puts("OK\n");
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001432 }
1433 } else {
1434 *initrd_start = 0;
1435 *initrd_end = 0;
1436 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00001437 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001438 *initrd_start, *initrd_end);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001439
Kumar Galae822d7f2008-02-27 21:51:49 -06001440 return 0;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001441
Kumar Galae822d7f2008-02-27 21:51:49 -06001442error:
1443 return -1;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001444}
John Rigbyfca43cc2010-10-13 13:57:35 -06001445#endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001446
Simon Glass90268b82014-10-19 21:11:24 -06001447int boot_get_setup(bootm_headers_t *images, uint8_t arch,
1448 ulong *setup_start, ulong *setup_len)
1449{
Simon Glass73223f02016-02-22 22:55:43 -07001450#if IMAGE_ENABLE_FIT
Simon Glass90268b82014-10-19 21:11:24 -06001451 return boot_get_setup_fit(images, arch, setup_start, setup_len);
1452#else
1453 return -ENOENT;
1454#endif
1455}
1456
Simon Glass73223f02016-02-22 22:55:43 -07001457#if IMAGE_ENABLE_FIT
Goldschmidt Simon8b93a922017-11-10 14:17:41 +00001458#if defined(CONFIG_FPGA)
Simon Glass09140112020-05-10 11:40:03 -06001459int boot_get_fpga(int argc, char *const argv[], bootm_headers_t *images,
Michal Simek62afc602016-05-17 14:03:50 +02001460 uint8_t arch, const ulong *ld_start, ulong * const ld_len)
1461{
1462 ulong tmp_img_addr, img_data, img_len;
1463 void *buf;
1464 int conf_noffset;
1465 int fit_img_result;
Simon Glassb02e4042016-10-02 17:59:28 -06001466 const char *uname, *name;
Michal Simek62afc602016-05-17 14:03:50 +02001467 int err;
1468 int devnum = 0; /* TODO support multi fpga platforms */
Michal Simek62afc602016-05-17 14:03:50 +02001469
1470 /* Check to see if the images struct has a FIT configuration */
1471 if (!genimg_has_config(images)) {
1472 debug("## FIT configuration was not specified\n");
1473 return 0;
1474 }
1475
1476 /*
1477 * Obtain the os FIT header from the images struct
Michal Simek62afc602016-05-17 14:03:50 +02001478 */
1479 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
Michal Simek62afc602016-05-17 14:03:50 +02001480 buf = map_sysmem(tmp_img_addr, 0);
1481 /*
1482 * Check image type. For FIT images get FIT node
1483 * and attempt to locate a generic binary.
1484 */
1485 switch (genimg_get_format(buf)) {
1486 case IMAGE_FORMAT_FIT:
1487 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
1488
Simon Glassb02e4042016-10-02 17:59:28 -06001489 uname = fdt_stringlist_get(buf, conf_noffset, FIT_FPGA_PROP, 0,
1490 NULL);
1491 if (!uname) {
Michal Simek62afc602016-05-17 14:03:50 +02001492 debug("## FPGA image is not specified\n");
1493 return 0;
1494 }
1495 fit_img_result = fit_image_load(images,
1496 tmp_img_addr,
1497 (const char **)&uname,
1498 &(images->fit_uname_cfg),
1499 arch,
1500 IH_TYPE_FPGA,
1501 BOOTSTAGE_ID_FPGA_INIT,
1502 FIT_LOAD_OPTIONAL_NON_ZERO,
1503 &img_data, &img_len);
1504
1505 debug("FPGA image (%s) loaded to 0x%lx/size 0x%lx\n",
1506 uname, img_data, img_len);
1507
1508 if (fit_img_result < 0) {
1509 /* Something went wrong! */
1510 return fit_img_result;
1511 }
1512
Goldschmidt Simon8b93a922017-11-10 14:17:41 +00001513 if (!fpga_is_partial_data(devnum, img_len)) {
Michal Simek62afc602016-05-17 14:03:50 +02001514 name = "full";
1515 err = fpga_loadbitstream(devnum, (char *)img_data,
1516 img_len, BIT_FULL);
1517 if (err)
1518 err = fpga_load(devnum, (const void *)img_data,
1519 img_len, BIT_FULL);
1520 } else {
1521 name = "partial";
1522 err = fpga_loadbitstream(devnum, (char *)img_data,
1523 img_len, BIT_PARTIAL);
1524 if (err)
1525 err = fpga_load(devnum, (const void *)img_data,
1526 img_len, BIT_PARTIAL);
1527 }
1528
Michal Simek62afc602016-05-17 14:03:50 +02001529 if (err)
Michal Simek611a9422016-12-16 10:35:40 +01001530 return err;
1531
1532 printf(" Programming %s bitstream... OK\n", name);
Michal Simek62afc602016-05-17 14:03:50 +02001533 break;
1534 default:
1535 printf("The given image format is not supported (corrupt?)\n");
1536 return 1;
1537 }
1538
1539 return 0;
1540}
1541#endif
1542
Andrew F. Davisd7be5092016-11-29 16:33:20 -06001543static void fit_loadable_process(uint8_t img_type,
1544 ulong img_data,
1545 ulong img_len)
1546{
1547 int i;
1548 const unsigned int count =
1549 ll_entry_count(struct fit_loadable_tbl, fit_loadable);
1550 struct fit_loadable_tbl *fit_loadable_handler =
1551 ll_entry_start(struct fit_loadable_tbl, fit_loadable);
1552 /* For each loadable handler */
1553 for (i = 0; i < count; i++, fit_loadable_handler++)
1554 /* matching this type */
1555 if (fit_loadable_handler->type == img_type)
1556 /* call that handler with this image data */
1557 fit_loadable_handler->handler(img_data, img_len);
1558}
1559
Simon Glass09140112020-05-10 11:40:03 -06001560int boot_get_loadable(int argc, char *const argv[], bootm_headers_t *images,
1561 uint8_t arch, const ulong *ld_start, ulong * const ld_len)
Karl Apsite84a07db2015-05-21 09:52:48 -04001562{
1563 /*
1564 * These variables are used to hold the current image location
1565 * in system memory.
1566 */
1567 ulong tmp_img_addr;
1568 /*
1569 * These two variables are requirements for fit_image_load, but
1570 * their values are not used
1571 */
1572 ulong img_data, img_len;
1573 void *buf;
1574 int loadables_index;
1575 int conf_noffset;
1576 int fit_img_result;
Simon Glassb02e4042016-10-02 17:59:28 -06001577 const char *uname;
Andrew F. Davisd7be5092016-11-29 16:33:20 -06001578 uint8_t img_type;
Karl Apsite84a07db2015-05-21 09:52:48 -04001579
1580 /* Check to see if the images struct has a FIT configuration */
1581 if (!genimg_has_config(images)) {
1582 debug("## FIT configuration was not specified\n");
1583 return 0;
1584 }
1585
1586 /*
1587 * Obtain the os FIT header from the images struct
Karl Apsite84a07db2015-05-21 09:52:48 -04001588 */
1589 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
Karl Apsite84a07db2015-05-21 09:52:48 -04001590 buf = map_sysmem(tmp_img_addr, 0);
1591 /*
1592 * Check image type. For FIT images get FIT node
1593 * and attempt to locate a generic binary.
1594 */
1595 switch (genimg_get_format(buf)) {
1596 case IMAGE_FORMAT_FIT:
1597 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
1598
1599 for (loadables_index = 0;
Simon Glassb02e4042016-10-02 17:59:28 -06001600 uname = fdt_stringlist_get(buf, conf_noffset,
1601 FIT_LOADABLE_PROP, loadables_index,
1602 NULL), uname;
Karl Apsite84a07db2015-05-21 09:52:48 -04001603 loadables_index++)
1604 {
1605 fit_img_result = fit_image_load(images,
1606 tmp_img_addr,
Simon Glassb02e4042016-10-02 17:59:28 -06001607 &uname,
Karl Apsite84a07db2015-05-21 09:52:48 -04001608 &(images->fit_uname_cfg), arch,
1609 IH_TYPE_LOADABLE,
1610 BOOTSTAGE_ID_FIT_LOADABLE_START,
1611 FIT_LOAD_OPTIONAL_NON_ZERO,
1612 &img_data, &img_len);
1613 if (fit_img_result < 0) {
1614 /* Something went wrong! */
1615 return fit_img_result;
1616 }
Andrew F. Davisd7be5092016-11-29 16:33:20 -06001617
1618 fit_img_result = fit_image_get_node(buf, uname);
1619 if (fit_img_result < 0) {
1620 /* Something went wrong! */
1621 return fit_img_result;
1622 }
1623 fit_img_result = fit_image_get_type(buf,
1624 fit_img_result,
1625 &img_type);
1626 if (fit_img_result < 0) {
1627 /* Something went wrong! */
1628 return fit_img_result;
1629 }
1630
1631 fit_loadable_process(img_type, img_data, img_len);
Karl Apsite84a07db2015-05-21 09:52:48 -04001632 }
1633 break;
1634 default:
1635 printf("The given image format is not supported (corrupt?)\n");
1636 return 1;
1637 }
1638
1639 return 0;
1640}
1641#endif
1642
John Rigbyfca43cc2010-10-13 13:57:35 -06001643#ifdef CONFIG_SYS_BOOT_GET_CMDLINE
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001644/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001645 * boot_get_cmdline - allocate and initialize kernel cmdline
Kumar Galae822d7f2008-02-27 21:51:49 -06001646 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001647 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1648 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1649 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001650 * boot_get_cmdline() allocates space for kernel command line below
Shyam Saini919d25c2018-06-07 19:47:19 +05301651 * BOOTMAPSZ + env_get_bootm_low() address. If "bootargs" U-Boot environment
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001652 * variable is present its contents is copied to allocated kernel
1653 * command line.
1654 *
1655 * returns:
Kumar Galae822d7f2008-02-27 21:51:49 -06001656 * 0 - success
1657 * -1 - failure
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001658 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001659int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001660{
1661 char *cmdline;
1662 char *s;
1663
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001664 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
Simon Glass723806c2017-08-03 12:22:15 -06001665 env_get_bootm_mapsize() + env_get_bootm_low());
Kumar Galae822d7f2008-02-27 21:51:49 -06001666
1667 if (cmdline == NULL)
1668 return -1;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001669
Simon Glass00caae62017-08-03 12:22:12 -06001670 s = env_get("bootargs");
1671 if (!s)
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001672 s = "";
1673
1674 strcpy(cmdline, s);
1675
1676 *cmd_start = (ulong) & cmdline[0];
1677 *cmd_end = *cmd_start + strlen(cmdline);
1678
Stephen Warren712fbcf2011-10-18 11:11:49 +00001679 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001680
Kumar Galae822d7f2008-02-27 21:51:49 -06001681 return 0;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001682}
John Rigbyfca43cc2010-10-13 13:57:35 -06001683#endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001684
John Rigbyfca43cc2010-10-13 13:57:35 -06001685#ifdef CONFIG_SYS_BOOT_GET_KBD
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001686/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001687 * boot_get_kbd - allocate and initialize kernel copy of board info
Kumar Galae822d7f2008-02-27 21:51:49 -06001688 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001689 * @kbd: double pointer to board info data
1690 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001691 * boot_get_kbd() allocates space for kernel copy of board info data below
Simon Glass723806c2017-08-03 12:22:15 -06001692 * BOOTMAPSZ + env_get_bootm_low() address and kernel board info is initialized
Grant Likely590d3ca2011-03-28 09:58:34 +00001693 * with the current u-boot board info data.
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001694 *
1695 * returns:
Kumar Galae822d7f2008-02-27 21:51:49 -06001696 * 0 - success
1697 * -1 - failure
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001698 */
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +09001699int boot_get_kbd(struct lmb *lmb, struct bd_info **kbd)
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001700{
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +09001701 *kbd = (struct bd_info *)(ulong)lmb_alloc_base(lmb,
1702 sizeof(struct bd_info),
1703 0xf,
1704 env_get_bootm_mapsize() + env_get_bootm_low());
Kumar Galae822d7f2008-02-27 21:51:49 -06001705 if (*kbd == NULL)
1706 return -1;
1707
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001708 **kbd = *(gd->bd);
1709
Stephen Warren712fbcf2011-10-18 11:11:49 +00001710 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001711
1712#if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1713 do_bdinfo(NULL, 0, 0, NULL);
1714#endif
1715
Kumar Galae822d7f2008-02-27 21:51:49 -06001716 return 0;
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001717}
John Rigbyfca43cc2010-10-13 13:57:35 -06001718#endif /* CONFIG_SYS_BOOT_GET_KBD */
Simon Glass13d06982013-05-08 08:06:01 +00001719
1720#ifdef CONFIG_LMB
1721int image_setup_linux(bootm_headers_t *images)
1722{
1723 ulong of_size = images->ft_len;
1724 char **of_flat_tree = &images->ft_addr;
Simon Glass13d06982013-05-08 08:06:01 +00001725 struct lmb *lmb = &images->lmb;
Simon Glass13d06982013-05-08 08:06:01 +00001726 int ret;
1727
1728 if (IMAGE_ENABLE_OF_LIBFDT)
1729 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
1730
1731 if (IMAGE_BOOT_GET_CMDLINE) {
1732 ret = boot_get_cmdline(lmb, &images->cmdline_start,
1733 &images->cmdline_end);
1734 if (ret) {
1735 puts("ERROR with allocation of cmdline\n");
1736 return ret;
1737 }
1738 }
Simon Glass13d06982013-05-08 08:06:01 +00001739
1740 if (IMAGE_ENABLE_OF_LIBFDT) {
1741 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
1742 if (ret)
1743 return ret;
1744 }
1745
1746 if (IMAGE_ENABLE_OF_LIBFDT && of_size) {
1747 ret = image_setup_libfdt(images, *of_flat_tree, of_size, lmb);
1748 if (ret)
1749 return ret;
1750 }
1751
1752 return 0;
1753}
1754#endif /* CONFIG_LMB */
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001755#endif /* !USE_HOSTCC */