blob: e16d433b91df15e98cd9407c7938c50685d538f5 [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
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010027#include <image.h>
Joe Hershberger0eb25b62015-03-22 17:08:59 -050028#include <mapmem.h>
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010029
Simon Glassaa34fbc2016-02-22 22:55:45 -070030#if IMAGE_ENABLE_FIT || IMAGE_ENABLE_OF_LIBFDT
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090031#include <linux/libfdt.h>
Marian Balakowiczfff888a12008-02-21 17:20:19 +010032#include <fdt_support.h>
Michal Simek62afc602016-05-17 14:03:50 +020033#include <fpga.h>
34#include <xilinx.h>
Marian Balakowiczc8779642008-03-12 10:12:37 +010035#endif
36
Simon Glass401d1c42020-10-30 21:38:53 -060037#include <asm/global_data.h>
Andy Fleming20a14a42008-04-02 16:19:07 -050038#include <u-boot/md5.h>
Jeroen Hofstee2b9912e2014-06-12 22:27:12 +020039#include <u-boot/sha1.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090040#include <linux/errno.h>
Simon Glass35e7b0f2013-05-07 06:12:03 +000041#include <asm/io.h>
Marian Balakowiczc8779642008-03-12 10:12:37 +010042
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +010043#ifdef CONFIG_CMD_BDI
Simon Glass09140112020-05-10 11:40:03 -060044extern int do_bdinfo(struct cmd_tbl *cmdtp, int flag, int argc,
45 char *const argv[]);
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +010046#endif
47
48DECLARE_GLOBAL_DATA_PTR;
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +010049
Tom Rinic76c93a2019-05-23 07:14:07 -040050#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Stephen Warren712fbcf2011-10-18 11:11:49 +000051static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
Marian Balakowiczd985c842008-03-12 10:14:38 +010052 int verify);
Heiko Schocher21d29f72014-05-28 11:33:33 +020053#endif
Simon Glass458b30a2021-09-25 07:03:14 -060054
55/* Set this if we have less than 4 MB of malloc() space */
56#if CONFIG_SYS_MALLOC_LEN < (4096 * 1024)
57#define CONSERVE_MEMORY true
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010058#else
Simon Glass458b30a2021-09-25 07:03:14 -060059#define CONSERVE_MEMORY false
60#endif
61
62#else /* USE_HOSTCC */
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +010063#include "mkimage.h"
Andy Fleming20a14a42008-04-02 16:19:07 -050064#include <u-boot/md5.h>
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010065#include <time.h>
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010066#include <image.h>
Heiko Schocher80402f32015-06-29 09:10:46 +020067
68#ifndef __maybe_unused
69# define __maybe_unused /* unimplemented */
70#endif
Simon Glass458b30a2021-09-25 07:03:14 -060071
72#define CONSERVE_MEMORY false
73
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010074#endif /* !USE_HOSTCC*/
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010075
Simon Glass458b30a2021-09-25 07:03:14 -060076#include <abuf.h>
77#include <bzlib.h>
78#include <gzip.h>
79#include <lz4.h>
Breno Matheus Lima5b20d142019-09-23 18:39:47 +000080#include <imximage.h>
Simon Glass458b30a2021-09-25 07:03:14 -060081#include <linux/lzo.h>
82#include <linux/zstd.h>
83#include <linux/kconfig.h>
84#include <lzma/LzmaTypes.h>
85#include <lzma/LzmaDec.h>
86#include <lzma/LzmaTools.h>
87#include <u-boot/crc.h>
Simon Glass0ccff502013-02-24 17:33:25 +000088
Simon Glass13d06982013-05-08 08:06:01 +000089#ifndef CONFIG_SYS_BARGSIZE
90#define CONFIG_SYS_BARGSIZE 512
91#endif
92
Mike Frysinger7edb1862010-10-20 07:17:39 -040093static const table_entry_t uimage_arch[] = {
Simon Glass30495bf2016-06-30 10:52:15 -060094 { IH_ARCH_INVALID, "invalid", "Invalid ARCH", },
Marian Balakowicz570abb02008-02-29 15:59:59 +010095 { IH_ARCH_ALPHA, "alpha", "Alpha", },
96 { IH_ARCH_ARM, "arm", "ARM", },
97 { IH_ARCH_I386, "x86", "Intel x86", },
98 { IH_ARCH_IA64, "ia64", "IA64", },
99 { IH_ARCH_M68K, "m68k", "M68K", },
100 { IH_ARCH_MICROBLAZE, "microblaze", "MicroBlaze", },
101 { IH_ARCH_MIPS, "mips", "MIPS", },
102 { IH_ARCH_MIPS64, "mips64", "MIPS 64 Bit", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100103 { IH_ARCH_NIOS2, "nios2", "NIOS II", },
Grant Ericksone419e122008-05-04 16:45:01 -0700104 { IH_ARCH_PPC, "powerpc", "PowerPC", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100105 { IH_ARCH_PPC, "ppc", "PowerPC", },
106 { IH_ARCH_S390, "s390", "IBM S390", },
107 { IH_ARCH_SH, "sh", "SuperH", },
108 { IH_ARCH_SPARC, "sparc", "SPARC", },
109 { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", },
110 { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", },
111 { IH_ARCH_AVR32, "avr32", "AVR32", },
Macpaul Lin64d61462011-10-19 20:41:09 +0000112 { IH_ARCH_NDS32, "nds32", "NDS32", },
Stefan Kristiansson3ddcacc2011-11-26 19:04:50 +0000113 { IH_ARCH_OPENRISC, "or1k", "OpenRISC 1000",},
Simon Glass35e7b0f2013-05-07 06:12:03 +0000114 { IH_ARCH_SANDBOX, "sandbox", "Sandbox", },
David Feng0ae76532013-12-14 11:47:35 +0800115 { IH_ARCH_ARM64, "arm64", "AArch64", },
Alexey Brodkinbc5d5422014-02-04 12:56:16 +0400116 { IH_ARCH_ARC, "arc", "ARC", },
Simon Glass5bda35c2014-10-10 08:21:57 -0600117 { IH_ARCH_X86_64, "x86_64", "AMD x86_64", },
Chris Zankelde5e5ce2016-08-10 18:36:43 +0300118 { IH_ARCH_XTENSA, "xtensa", "Xtensa", },
Rick Chen86aa65a2018-03-13 13:37:29 +0800119 { IH_ARCH_RISCV, "riscv", "RISC-V", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100120 { -1, "", "", },
121};
122
Mike Frysinger7edb1862010-10-20 07:17:39 -0400123static const table_entry_t uimage_os[] = {
Simon Glass30495bf2016-06-30 10:52:15 -0600124 { IH_OS_INVALID, "invalid", "Invalid OS", },
Philipp Tomsich4914af12017-09-13 21:29:29 +0200125 { IH_OS_ARM_TRUSTED_FIRMWARE, "arm-trusted-firmware", "ARM Trusted Firmware" },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100126 { IH_OS_LINUX, "linux", "Linux", },
127#if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
128 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
129#endif
130 { IH_OS_NETBSD, "netbsd", "NetBSD", },
Torkel Lundgren3df61952010-09-28 11:05:36 +0200131 { IH_OS_OSE, "ose", "Enea OSE", },
Steven Stallion04d41402013-03-20 06:31:35 +0000132 { IH_OS_PLAN9, "plan9", "Plan 9", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100133 { IH_OS_RTEMS, "rtems", "RTEMS", },
Bryan O'Donoghue45b55712018-03-13 16:50:35 +0000134 { IH_OS_TEE, "tee", "Trusted Execution Environment" },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100135 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
miao.yan@windriver.com68b15e82013-12-27 16:01:50 +0800136 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100137#if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
138 { IH_OS_QNX, "qnx", "QNX", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100139#endif
Peter Tyserf5ed9e32008-09-08 14:56:49 -0500140#if defined(CONFIG_INTEGRITY) || defined(USE_HOSTCC)
141 { IH_OS_INTEGRITY,"integrity", "INTEGRITY", },
142#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100143#ifdef USE_HOSTCC
144 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
145 { IH_OS_DELL, "dell", "Dell", },
146 { IH_OS_ESIX, "esix", "Esix", },
147 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
148 { IH_OS_IRIX, "irix", "Irix", },
149 { IH_OS_NCR, "ncr", "NCR", },
150 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
151 { IH_OS_PSOS, "psos", "pSOS", },
152 { IH_OS_SCO, "sco", "SCO", },
153 { IH_OS_SOLARIS, "solaris", "Solaris", },
154 { IH_OS_SVR4, "svr4", "SVR4", },
155#endif
Marek Vasut67ddd952014-12-16 14:07:21 +0100156#if defined(CONFIG_BOOTM_OPENRTOS) || defined(USE_HOSTCC)
157 { IH_OS_OPENRTOS, "openrtos", "OpenRTOS", },
158#endif
Lukas Auer5e30e452019-08-21 21:14:44 +0200159 { IH_OS_OPENSBI, "opensbi", "RISC-V OpenSBI", },
Cristian Ciocalteaa031b032019-12-24 18:05:38 +0200160 { IH_OS_EFI, "efi", "EFI Firmware" },
Marek Vasut67ddd952014-12-16 14:07:21 +0100161
Marian Balakowicz570abb02008-02-29 15:59:59 +0100162 { -1, "", "", },
163};
164
Mike Frysinger7edb1862010-10-20 07:17:39 -0400165static const table_entry_t uimage_type[] = {
Stefano Babic4962e382011-10-17 00:07:43 +0000166 { IH_TYPE_AISIMAGE, "aisimage", "Davinci AIS image",},
Marian Balakowicz570abb02008-02-29 15:59:59 +0100167 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
168 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
John Rigby3decb142011-07-21 09:10:30 -0400169 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
Karicheri, Muralidharanbf411ea2014-04-04 13:16:48 -0400170 { IH_TYPE_GPIMAGE, "gpimage", "TI Keystone SPL Image",},
Marian Balakowicz570abb02008-02-29 15:59:59 +0100171 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
Stephen Warrenb9b50e82011-11-10 13:17:53 -0700172 { IH_TYPE_KERNEL_NOLOAD, "kernel_noload", "Kernel Image (no loading done)", },
Stefano Babic4962e382011-10-17 00:07:43 +0000173 { IH_TYPE_KWBIMAGE, "kwbimage", "Kirkwood Boot Image",},
174 { IH_TYPE_IMXIMAGE, "imximage", "Freescale i.MX Boot Image",},
Peng Fana2b96ec2018-10-16 04:50:30 +0000175 { IH_TYPE_IMX8IMAGE, "imx8image", "NXP i.MX8 Boot Image",},
Peng Fan6609c262018-11-20 10:19:36 +0000176 { IH_TYPE_IMX8MIMAGE, "imx8mimage", "NXP i.MX8M Boot Image",},
Simon Glass30495bf2016-06-30 10:52:15 -0600177 { IH_TYPE_INVALID, "invalid", "Invalid Image", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100178 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
Stefano Babic4962e382011-10-17 00:07:43 +0000179 { IH_TYPE_OMAPIMAGE, "omapimage", "TI OMAP SPL With GP CH",},
Shaohui Xie5d898a02012-08-10 02:49:35 +0000180 { IH_TYPE_PBLIMAGE, "pblimage", "Freescale PBL Boot Image",},
Marian Balakowicz570abb02008-02-29 15:59:59 +0100181 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
182 { IH_TYPE_SCRIPT, "script", "Script", },
Marek Vasut662abc42018-04-15 13:15:33 +0200183 { IH_TYPE_SOCFPGAIMAGE, "socfpgaimage", "Altera SoCFPGA CV/AV preloader",},
184 { IH_TYPE_SOCFPGAIMAGE_V1, "socfpgaimage_v1", "Altera SoCFPGA A10 preloader",},
Marian Balakowicz570abb02008-02-29 15:59:59 +0100185 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
Heiko Schocher7816f2c2011-07-16 00:06:42 +0000186 { IH_TYPE_UBLIMAGE, "ublimage", "Davinci UBL image",},
Marek Vasutbce88372013-08-26 20:43:33 +0200187 { IH_TYPE_MXSIMAGE, "mxsimage", "Freescale MXS Boot Image",},
Andreas Bießmann7b1a4112014-05-19 14:23:39 +0200188 { IH_TYPE_ATMELIMAGE, "atmelimage", "ATMEL ROM-Boot Image",},
Simon Glass90268b82014-10-19 21:11:24 -0600189 { IH_TYPE_X86_SETUP, "x86_setup", "x86 setup.bin", },
Albert ARIBAUD \(3ADEV\)39f520b2015-03-31 11:40:49 +0200190 { IH_TYPE_LPC32XXIMAGE, "lpc32xximage", "LPC32XX Boot Image", },
Simon Glassa131c1f2015-08-30 16:55:24 -0600191 { IH_TYPE_RKIMAGE, "rkimage", "Rockchip Boot Image" },
Simon Glassf9a3c272015-08-30 16:55:25 -0600192 { IH_TYPE_RKSD, "rksd", "Rockchip SD Boot Image" },
Simon Glass10b84fe2015-08-30 16:55:26 -0600193 { IH_TYPE_RKSPI, "rkspi", "Rockchip SPI Boot Image" },
Albert ARIBAUD \(3ADEV\)ed0c2c02016-09-26 09:08:06 +0200194 { IH_TYPE_VYBRIDIMAGE, "vybridimage", "Vybrid Boot Image", },
Nathan Rossi66eef1e2015-11-17 22:56:56 +1000195 { IH_TYPE_ZYNQIMAGE, "zynqimage", "Xilinx Zynq Boot Image" },
Michal Simekd9b58b32016-04-27 14:03:29 +0200196 { IH_TYPE_ZYNQMPIMAGE, "zynqmpimage", "Xilinx ZynqMP Boot Image" },
Alexander Graf6915dcf2018-04-13 14:18:52 +0200197 { IH_TYPE_ZYNQMPBIF, "zynqmpbif", "Xilinx ZynqMP Boot Image (bif)" },
Michal Simeked0cea72016-05-17 13:58:44 +0200198 { IH_TYPE_FPGA, "fpga", "FPGA Image" },
Andrew F. Davis7e719ee2016-11-29 16:33:21 -0600199 { IH_TYPE_TEE, "tee", "Trusted Execution Environment Image",},
Sven Ebenfeldd21bd692016-11-06 16:37:56 +0100200 { IH_TYPE_FIRMWARE_IVT, "firmware_ivt", "Firmware with HABv4 IVT" },
Andrew F. Davis6442c962017-07-31 10:58:20 -0500201 { IH_TYPE_PMMC, "pmmc", "TI Power Management Micro-Controller Firmware",},
Patrick Delaunay81260e32018-03-12 10:46:04 +0100202 { IH_TYPE_STM32IMAGE, "stm32image", "STMicroelectronics STM32 Image" },
Ryder Lee3b975a12018-11-15 10:07:49 +0800203 { IH_TYPE_MTKIMAGE, "mtk_image", "MediaTek BootROM loadable Image" },
Patrick Delaunaye7fabe72019-08-02 15:07:19 +0200204 { IH_TYPE_COPRO, "copro", "Coprocessor Image"},
Andre Przywara6d295092018-12-20 01:15:18 +0000205 { IH_TYPE_SUNXI_EGON, "sunxi_egon", "Allwinner eGON Boot Image" },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100206 { -1, "", "", },
207};
208
Mike Frysinger7edb1862010-10-20 07:17:39 -0400209static const table_entry_t uimage_comp[] = {
Marian Balakowicz570abb02008-02-29 15:59:59 +0100210 { IH_COMP_NONE, "none", "uncompressed", },
211 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
212 { IH_COMP_GZIP, "gzip", "gzip compressed", },
Luigi 'Comio' Mantellinifc9c1722008-09-08 02:46:13 +0200213 { IH_COMP_LZMA, "lzma", "lzma compressed", },
Peter Korsgaard20dde482009-11-19 11:37:51 +0100214 { IH_COMP_LZO, "lzo", "lzo compressed", },
Julius Werner027b7282015-10-06 20:03:53 -0700215 { IH_COMP_LZ4, "lz4", "lz4 compressed", },
Robert Marko26073f92020-04-25 19:37:21 +0200216 { IH_COMP_ZSTD, "zstd", "zstd compressed", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100217 { -1, "", "", },
218};
219
Simon Glass56d7ab72016-06-30 10:52:14 -0600220struct table_info {
221 const char *desc;
222 int count;
223 const table_entry_t *table;
224};
225
Atish Patra155d6a32020-03-05 16:24:22 -0800226static const struct comp_magic_map image_comp[] = {
227 { IH_COMP_BZIP2, "bzip2", {0x42, 0x5a},},
228 { IH_COMP_GZIP, "gzip", {0x1f, 0x8b},},
229 { IH_COMP_LZMA, "lzma", {0x5d, 0x00},},
230 { IH_COMP_LZO, "lzo", {0x89, 0x4c},},
Artem Lapkin2ddb8fc2021-08-31 18:22:18 +0800231 { IH_COMP_LZ4, "lz4", {0x04, 0x22},},
232 { IH_COMP_ZSTD, "zstd", {0x28, 0xb5},},
Atish Patra155d6a32020-03-05 16:24:22 -0800233 { IH_COMP_NONE, "none", {}, },
234};
235
Simon Glass56d7ab72016-06-30 10:52:14 -0600236static const struct table_info table_info[IH_COUNT] = {
237 { "architecture", IH_ARCH_COUNT, uimage_arch },
238 { "compression", IH_COMP_COUNT, uimage_comp },
239 { "operating system", IH_OS_COUNT, uimage_os },
240 { "image type", IH_TYPE_COUNT, uimage_type },
241};
242
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100243/*****************************************************************************/
244/* Legacy format routines */
245/*****************************************************************************/
Stephen Warren712fbcf2011-10-18 11:11:49 +0000246int image_check_hcrc(const image_header_t *hdr)
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100247{
248 ulong hcrc;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000249 ulong len = image_get_header_size();
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100250 image_header_t header;
251
252 /* Copy header so we can blank CRC field for re-calculation */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000253 memmove(&header, (char *)hdr, image_get_header_size());
254 image_set_hcrc(&header, 0);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100255
Stephen Warren712fbcf2011-10-18 11:11:49 +0000256 hcrc = crc32(0, (unsigned char *)&header, len);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100257
Stephen Warren712fbcf2011-10-18 11:11:49 +0000258 return (hcrc == image_get_hcrc(hdr));
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100259}
260
Stephen Warren712fbcf2011-10-18 11:11:49 +0000261int image_check_dcrc(const image_header_t *hdr)
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100262{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000263 ulong data = image_get_data(hdr);
264 ulong len = image_get_data_size(hdr);
265 ulong dcrc = crc32_wd(0, (unsigned char *)data, len, CHUNKSZ_CRC32);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100266
Stephen Warren712fbcf2011-10-18 11:11:49 +0000267 return (dcrc == image_get_dcrc(hdr));
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100268}
269
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100270/**
271 * image_multi_count - get component (sub-image) count
272 * @hdr: pointer to the header of the multi component image
273 *
274 * image_multi_count() returns number of components in a multi
275 * component image.
276 *
277 * Note: no checking of the image type is done, caller must pass
278 * a valid multi component image.
279 *
280 * returns:
281 * number of components
282 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000283ulong image_multi_count(const image_header_t *hdr)
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100284{
285 ulong i, count = 0;
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100286 uint32_t *size;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100287
288 /* get start of the image payload, which in case of multi
289 * component images that points to a table of component sizes */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000290 size = (uint32_t *)image_get_data(hdr);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100291
292 /* count non empty slots */
293 for (i = 0; size[i]; ++i)
294 count++;
295
296 return count;
297}
298
299/**
300 * image_multi_getimg - get component data address and size
301 * @hdr: pointer to the header of the multi component image
302 * @idx: index of the requested component
303 * @data: pointer to a ulong variable, will hold component data address
304 * @len: pointer to a ulong variable, will hold component size
305 *
306 * image_multi_getimg() returns size and data address for the requested
307 * component in a multi component image.
308 *
309 * Note: no checking of the image type is done, caller must pass
310 * a valid multi component image.
311 *
312 * returns:
313 * data address and size of the component, if idx is valid
314 * 0 in data and len, if idx is out of range
315 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000316void image_multi_getimg(const image_header_t *hdr, ulong idx,
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100317 ulong *data, ulong *len)
318{
319 int i;
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100320 uint32_t *size;
Nick Spence02b9b222008-05-10 14:02:04 -0700321 ulong offset, count, img_data;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100322
323 /* get number of component */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000324 count = image_multi_count(hdr);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100325
326 /* get start of the image payload, which in case of multi
327 * component images that points to a table of component sizes */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000328 size = (uint32_t *)image_get_data(hdr);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100329
330 /* get address of the proper component data start, which means
331 * skipping sizes table (add 1 for last, null entry) */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000332 img_data = image_get_data(hdr) + (count + 1) * sizeof(uint32_t);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100333
334 if (idx < count) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000335 *len = uimage_to_cpu(size[idx]);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100336 offset = 0;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100337
338 /* go over all indices preceding requested component idx */
339 for (i = 0; i < idx; i++) {
Nick Spence02b9b222008-05-10 14:02:04 -0700340 /* add up i-th component size, rounding up to 4 bytes */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000341 offset += (uimage_to_cpu(size[i]) + 3) & ~3 ;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100342 }
343
344 /* calculate idx-th component data address */
Nick Spence02b9b222008-05-10 14:02:04 -0700345 *data = img_data + offset;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100346 } else {
347 *len = 0;
348 *data = 0;
349 }
350}
Marian Balakowicz42b73e82008-01-31 13:20:07 +0100351
Stephen Warren712fbcf2011-10-18 11:11:49 +0000352static void image_print_type(const image_header_t *hdr)
Marian Balakowicz2242f532008-02-21 17:27:41 +0100353{
Heiko Schocher80402f32015-06-29 09:10:46 +0200354 const char __maybe_unused *os, *arch, *type, *comp;
Marian Balakowicz2242f532008-02-21 17:27:41 +0100355
Stephen Warren712fbcf2011-10-18 11:11:49 +0000356 os = genimg_get_os_name(image_get_os(hdr));
357 arch = genimg_get_arch_name(image_get_arch(hdr));
358 type = genimg_get_type_name(image_get_type(hdr));
359 comp = genimg_get_comp_name(image_get_comp(hdr));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100360
Stephen Warren712fbcf2011-10-18 11:11:49 +0000361 printf("%s %s %s (%s)\n", arch, os, type, comp);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100362}
363
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100364/**
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200365 * image_print_contents - prints out the contents of the legacy format image
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200366 * @ptr: pointer to the legacy format image header
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100367 * @p: pointer to prefix string
368 *
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200369 * image_print_contents() formats a multi line legacy image contents description.
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100370 * The routine prints out all header fields followed by the size/offset data
371 * for MULTI/SCRIPT images.
372 *
373 * returns:
374 * no returned results
375 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000376void image_print_contents(const void *ptr)
Marian Balakowicz2242f532008-02-21 17:27:41 +0100377{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200378 const image_header_t *hdr = (const image_header_t *)ptr;
Heiko Schocher80402f32015-06-29 09:10:46 +0200379 const char __maybe_unused *p;
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200380
Simon Glass1fe7d932013-05-08 08:05:58 +0000381 p = IMAGE_INDENT_STRING;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000382 printf("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name(hdr));
Simon Glass859e92b2013-05-07 06:11:51 +0000383 if (IMAGE_ENABLE_TIMESTAMP) {
384 printf("%sCreated: ", p);
385 genimg_print_time((time_t)image_get_time(hdr));
386 }
Stephen Warren712fbcf2011-10-18 11:11:49 +0000387 printf("%sImage Type: ", p);
388 image_print_type(hdr);
389 printf("%sData Size: ", p);
390 genimg_print_size(image_get_data_size(hdr));
391 printf("%sLoad Address: %08x\n", p, image_get_load(hdr));
392 printf("%sEntry Point: %08x\n", p, image_get_ep(hdr));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100393
Stephen Warren712fbcf2011-10-18 11:11:49 +0000394 if (image_check_type(hdr, IH_TYPE_MULTI) ||
395 image_check_type(hdr, IH_TYPE_SCRIPT)) {
Marian Balakowicz2242f532008-02-21 17:27:41 +0100396 int i;
397 ulong data, len;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000398 ulong count = image_multi_count(hdr);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100399
Stephen Warren712fbcf2011-10-18 11:11:49 +0000400 printf("%sContents:\n", p);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100401 for (i = 0; i < count; i++) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000402 image_multi_getimg(hdr, i, &data, &len);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100403
Stephen Warren712fbcf2011-10-18 11:11:49 +0000404 printf("%s Image %d: ", p, i);
405 genimg_print_size(len);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100406
Stephen Warren712fbcf2011-10-18 11:11:49 +0000407 if (image_check_type(hdr, IH_TYPE_SCRIPT) && i > 0) {
Marian Balakowicz570abb02008-02-29 15:59:59 +0100408 /*
409 * the user may need to know offsets
410 * if planning to do something with
411 * multiple files
412 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000413 printf("%s Offset = 0x%08lx\n", p, data);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100414 }
Marian Balakowicz2242f532008-02-21 17:27:41 +0100415 }
Sven Ebenfeldd21bd692016-11-06 16:37:56 +0100416 } else if (image_check_type(hdr, IH_TYPE_FIRMWARE_IVT)) {
417 printf("HAB Blocks: 0x%08x 0x0000 0x%08x\n",
Breno Matheus Lima5b20d142019-09-23 18:39:47 +0000418 image_get_load(hdr) - image_get_header_size(),
419 (int)(image_get_size(hdr) + image_get_header_size()
420 + sizeof(flash_header_v2_t) - 0x2060));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100421 }
422}
423
Julius Werner20908542019-07-24 19:37:54 -0700424/**
425 * print_decomp_msg() - Print a suitable decompression/loading message
426 *
427 * @type: OS type (IH_OS_...)
428 * @comp_type: Compression type being used (IH_COMP_...)
429 * @is_xip: true if the load address matches the image start
430 */
431static void print_decomp_msg(int comp_type, int type, bool is_xip)
432{
433 const char *name = genimg_get_type_name(type);
434
435 if (comp_type == IH_COMP_NONE)
436 printf(" %s %s\n", is_xip ? "XIP" : "Loading", name);
437 else
438 printf(" Uncompressing %s\n", name);
439}
440
Atish Patra155d6a32020-03-05 16:24:22 -0800441int image_decomp_type(const unsigned char *buf, ulong len)
442{
443 const struct comp_magic_map *cmagic = image_comp;
444
445 if (len < 2)
446 return -EINVAL;
447
448 for (; cmagic->comp_id > 0; cmagic++) {
449 if (!memcmp(buf, cmagic->magic, 2))
450 break;
451 }
452
453 return cmagic->comp_id;
454}
455
Julius Werner20908542019-07-24 19:37:54 -0700456int image_decomp(int comp, ulong load, ulong image_start, int type,
457 void *load_buf, void *image_buf, ulong image_len,
458 uint unc_len, ulong *load_end)
459{
Simon Glassb876eb82021-09-25 07:03:11 -0600460 int ret = -ENOSYS;
Julius Werner20908542019-07-24 19:37:54 -0700461
462 *load_end = load;
463 print_decomp_msg(comp, type, load == image_start);
464
465 /*
466 * Load the image to the right place, decompressing if needed. After
467 * this, image_len will be set to the number of uncompressed bytes
468 * loaded, ret will be non-zero on error.
469 */
470 switch (comp) {
471 case IH_COMP_NONE:
Simon Glassb876eb82021-09-25 07:03:11 -0600472 ret = 0;
Julius Werner20908542019-07-24 19:37:54 -0700473 if (load == image_start)
474 break;
475 if (image_len <= unc_len)
476 memmove_wd(load_buf, image_buf, image_len, CHUNKSZ);
477 else
478 ret = -ENOSPC;
479 break;
Simon Glass458b30a2021-09-25 07:03:14 -0600480 case IH_COMP_GZIP:
481 if (!host_build() && CONFIG_IS_ENABLED(GZIP))
482 ret = gunzip(load_buf, unc_len, image_buf, &image_len);
Julius Werner20908542019-07-24 19:37:54 -0700483 break;
Simon Glass458b30a2021-09-25 07:03:14 -0600484 case IH_COMP_BZIP2:
485 if (!host_build() && CONFIG_IS_ENABLED(BZIP2)) {
486 uint size = unc_len;
Julius Werner20908542019-07-24 19:37:54 -0700487
Simon Glass458b30a2021-09-25 07:03:14 -0600488 /*
489 * If we've got less than 4 MB of malloc() space,
490 * use slower decompression algorithm which requires
491 * at most 2300 KB of memory.
492 */
493 ret = BZ2_bzBuffToBuffDecompress(load_buf, &size,
494 image_buf, image_len, CONSERVE_MEMORY, 0);
495 image_len = size;
Robert Marko26073f92020-04-25 19:37:21 +0200496 }
Robert Marko26073f92020-04-25 19:37:21 +0200497 break;
Simon Glass458b30a2021-09-25 07:03:14 -0600498 case IH_COMP_LZMA:
499 if (!host_build() && CONFIG_IS_ENABLED(LZMA)) {
500 SizeT lzma_len = unc_len;
501
502 ret = lzmaBuffToBuffDecompress(load_buf, &lzma_len,
503 image_buf, image_len);
504 image_len = lzma_len;
505 }
506 break;
507 case IH_COMP_LZO:
508 if (!host_build() && CONFIG_IS_ENABLED(LZO)) {
509 size_t size = unc_len;
510
511 ret = lzop_decompress(image_buf, image_len, load_buf, &size);
512 image_len = size;
513 }
514 break;
515 case IH_COMP_LZ4:
516 if (!host_build() && CONFIG_IS_ENABLED(LZ4)) {
517 size_t size = unc_len;
518
519 ret = ulz4fn(image_buf, image_len, load_buf, &size);
520 image_len = size;
521 }
522 break;
523 case IH_COMP_ZSTD:
524 if (!host_build() && CONFIG_IS_ENABLED(ZSTD)) {
525 struct abuf in, out;
526
527 abuf_init_set(&in, image_buf, image_len);
528 abuf_init_set(&in, load_buf, unc_len);
529 ret = zstd_decompress(&in, &out);
530 if (ret >= 0) {
531 image_len = ret;
532 ret = 0;
533 }
534 }
535 break;
Julius Werner20908542019-07-24 19:37:54 -0700536 }
Simon Glassb876eb82021-09-25 07:03:11 -0600537 if (ret == -ENOSYS) {
538 printf("Unimplemented compression type %d\n", comp);
539 return ret;
540 }
541 if (ret)
542 return ret;
Julius Werner20908542019-07-24 19:37:54 -0700543
544 *load_end = load + image_len;
545
Simon Glassb876eb82021-09-25 07:03:11 -0600546 return 0;
Julius Werner20908542019-07-24 19:37:54 -0700547}
548
Marian Balakowicz570abb02008-02-29 15:59:59 +0100549#ifndef USE_HOSTCC
Tom Rinic76c93a2019-05-23 07:14:07 -0400550#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100551/**
552 * image_get_ramdisk - get and verify ramdisk image
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100553 * @rd_addr: ramdisk image start address
554 * @arch: expected ramdisk architecture
555 * @verify: checksum verification flag
556 *
557 * image_get_ramdisk() returns a pointer to the verified ramdisk image
558 * header. Routine receives image start address and expected architecture
559 * flag. Verification done covers data and header integrity and os/type/arch
560 * fields checking.
561 *
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100562 * returns:
563 * pointer to a ramdisk image header, if image was found and valid
Kumar Gala274cea22008-02-27 21:51:46 -0600564 * otherwise, return NULL
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100565 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000566static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
Marian Balakowiczd985c842008-03-12 10:14:38 +0100567 int verify)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100568{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200569 const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100570
Stephen Warren712fbcf2011-10-18 11:11:49 +0000571 if (!image_check_magic(rd_hdr)) {
572 puts("Bad Magic Number\n");
Simon Glass770605e2012-02-13 13:51:18 +0000573 bootstage_error(BOOTSTAGE_ID_RD_MAGIC);
Kumar Gala274cea22008-02-27 21:51:46 -0600574 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100575 }
576
Stephen Warren712fbcf2011-10-18 11:11:49 +0000577 if (!image_check_hcrc(rd_hdr)) {
578 puts("Bad Header Checksum\n");
Simon Glass770605e2012-02-13 13:51:18 +0000579 bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
Kumar Gala274cea22008-02-27 21:51:46 -0600580 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100581 }
582
Simon Glass770605e2012-02-13 13:51:18 +0000583 bootstage_mark(BOOTSTAGE_ID_RD_MAGIC);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000584 image_print_contents(rd_hdr);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100585
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100586 if (verify) {
587 puts(" Verifying Checksum ... ");
Stephen Warren712fbcf2011-10-18 11:11:49 +0000588 if (!image_check_dcrc(rd_hdr)) {
589 puts("Bad Data CRC\n");
Simon Glass770605e2012-02-13 13:51:18 +0000590 bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM);
Kumar Gala274cea22008-02-27 21:51:46 -0600591 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100592 }
593 puts("OK\n");
594 }
595
Simon Glass770605e2012-02-13 13:51:18 +0000596 bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100597
Stephen Warren712fbcf2011-10-18 11:11:49 +0000598 if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
599 !image_check_arch(rd_hdr, arch) ||
600 !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
601 printf("No Linux %s Ramdisk Image\n",
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100602 genimg_get_arch_name(arch));
Simon Glass770605e2012-02-13 13:51:18 +0000603 bootstage_error(BOOTSTAGE_ID_RAMDISK);
Kumar Gala274cea22008-02-27 21:51:46 -0600604 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100605 }
606
607 return rd_hdr;
608}
Heiko Schocher21d29f72014-05-28 11:33:33 +0200609#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100610#endif /* !USE_HOSTCC */
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100611
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100612/*****************************************************************************/
613/* Shared dual-format routines */
614/*****************************************************************************/
Marian Balakowicz570abb02008-02-29 15:59:59 +0100615#ifndef USE_HOSTCC
Simon Glassbb872dd2019-12-28 10:45:02 -0700616ulong image_load_addr = CONFIG_SYS_LOAD_ADDR; /* Default Load Address */
617ulong image_save_addr; /* Default Save Address */
618ulong image_save_size; /* Default Save Size (in bytes) */
Joe Hershberger1cf0a8b2012-12-11 22:16:28 -0600619
620static int on_loadaddr(const char *name, const char *value, enum env_op op,
621 int flags)
622{
623 switch (op) {
624 case env_op_create:
625 case env_op_overwrite:
Simon Glass7e5f4602021-07-24 09:03:29 -0600626 image_load_addr = hextoul(value, NULL);
Joe Hershberger1cf0a8b2012-12-11 22:16:28 -0600627 break;
628 default:
629 break;
630 }
631
632 return 0;
633}
634U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
635
Simon Glass723806c2017-08-03 12:22:15 -0600636ulong env_get_bootm_low(void)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100637{
Simon Glass00caae62017-08-03 12:22:12 -0600638 char *s = env_get("bootm_low");
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100639 if (s) {
Simon Glass7e5f4602021-07-24 09:03:29 -0600640 ulong tmp = hextoul(s, NULL);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100641 return tmp;
642 }
643
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200644#if defined(CONFIG_SYS_SDRAM_BASE)
645 return CONFIG_SYS_SDRAM_BASE;
Samuel Hollanddd573b62021-09-12 11:26:45 -0500646#elif defined(CONFIG_ARM) || defined(CONFIG_MICROBLAZE) || defined(CONFIG_RISCV)
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100647 return gd->bd->bi_dram[0].start;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100648#else
649 return 0;
650#endif
651}
652
Simon Glass723806c2017-08-03 12:22:15 -0600653phys_size_t env_get_bootm_size(void)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100654{
Masahiro Yamada0cb389d2016-02-05 16:12:50 +0900655 phys_size_t tmp, size;
656 phys_addr_t start;
Simon Glass00caae62017-08-03 12:22:12 -0600657 char *s = env_get("bootm_size");
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100658 if (s) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000659 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100660 return tmp;
661 }
Masahiro Yamada0cb389d2016-02-05 16:12:50 +0900662
Stefan Roese4963f632020-08-12 12:57:18 +0200663 start = gd->ram_base;
664 size = gd->ram_size;
Masahiro Yamada0cb389d2016-02-05 16:12:50 +0900665
Baruch Siach7f98b4e2020-08-24 20:14:05 +0300666 if (start + size > gd->ram_top)
667 size = gd->ram_top - start;
668
Simon Glass00caae62017-08-03 12:22:12 -0600669 s = env_get("bootm_low");
Matthew McClintockc519fac2010-07-08 10:11:08 -0500670 if (s)
Stephen Warren712fbcf2011-10-18 11:11:49 +0000671 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
Matthew McClintockc519fac2010-07-08 10:11:08 -0500672 else
Masahiro Yamada0cb389d2016-02-05 16:12:50 +0900673 tmp = start;
Matthew McClintockc519fac2010-07-08 10:11:08 -0500674
Masahiro Yamada0cb389d2016-02-05 16:12:50 +0900675 return size - (tmp - start);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100676}
677
Simon Glass723806c2017-08-03 12:22:15 -0600678phys_size_t env_get_bootm_mapsize(void)
Grant Likelyc3624e62011-03-28 09:58:43 +0000679{
680 phys_size_t tmp;
Simon Glass00caae62017-08-03 12:22:12 -0600681 char *s = env_get("bootm_mapsize");
Grant Likelyc3624e62011-03-28 09:58:43 +0000682 if (s) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000683 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
Grant Likelyc3624e62011-03-28 09:58:43 +0000684 return tmp;
685 }
686
687#if defined(CONFIG_SYS_BOOTMAPSZ)
688 return CONFIG_SYS_BOOTMAPSZ;
689#else
Simon Glass723806c2017-08-03 12:22:15 -0600690 return env_get_bootm_size();
Grant Likelyc3624e62011-03-28 09:58:43 +0000691#endif
692}
693
Stephen Warren712fbcf2011-10-18 11:11:49 +0000694void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100695{
Larry Johnson54fa2c52010-04-20 08:09:43 -0400696 if (to == from)
697 return;
698
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100699#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
Sonic Zhang22cfddc2014-12-10 18:20:53 +0800700 if (to > from) {
701 from += len;
702 to += len;
703 }
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100704 while (len > 0) {
705 size_t tail = (len > chunksz) ? chunksz : len;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000706 WATCHDOG_RESET();
Sonic Zhang22cfddc2014-12-10 18:20:53 +0800707 if (to > from) {
708 to -= tail;
709 from -= tail;
710 }
Stephen Warren712fbcf2011-10-18 11:11:49 +0000711 memmove(to, from, tail);
Sonic Zhang22cfddc2014-12-10 18:20:53 +0800712 if (to < from) {
713 to += tail;
714 from += tail;
715 }
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100716 len -= tail;
717 }
718#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000719 memmove(to, from, len);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100720#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
721}
Julius Werner20908542019-07-24 19:37:54 -0700722#else /* USE_HOSTCC */
723void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
724{
725 memmove(to, from, len);
726}
Marian Balakowicz570abb02008-02-29 15:59:59 +0100727#endif /* !USE_HOSTCC */
728
Stephen Warren712fbcf2011-10-18 11:11:49 +0000729void genimg_print_size(uint32_t size)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100730{
731#ifndef USE_HOSTCC
Stephen Warren712fbcf2011-10-18 11:11:49 +0000732 printf("%d Bytes = ", size);
733 print_size(size, "\n");
Marian Balakowicz570abb02008-02-29 15:59:59 +0100734#else
xypron.glpk@gmx.decec85d42017-01-04 14:04:44 +0100735 printf("%d Bytes = %.2f KiB = %.2f MiB\n",
Marian Balakowicz570abb02008-02-29 15:59:59 +0100736 size, (double)size / 1.024e3,
737 (double)size / 1.048576e6);
738#endif
739}
740
Simon Glass859e92b2013-05-07 06:11:51 +0000741#if IMAGE_ENABLE_TIMESTAMP
742void genimg_print_time(time_t timestamp)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100743{
744#ifndef USE_HOSTCC
745 struct rtc_time tm;
746
Simon Glass9f9276c2015-04-20 12:37:18 -0600747 rtc_to_tm(timestamp, &tm);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000748 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
Marian Balakowicz570abb02008-02-29 15:59:59 +0100749 tm.tm_year, tm.tm_mon, tm.tm_mday,
750 tm.tm_hour, tm.tm_min, tm.tm_sec);
751#else
Stephen Warren712fbcf2011-10-18 11:11:49 +0000752 printf("%s", ctime(&timestamp));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100753#endif
754}
Simon Glass859e92b2013-05-07 06:11:51 +0000755#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100756
Simon Glass5b9d44d2015-06-23 15:38:25 -0600757const table_entry_t *get_table_entry(const table_entry_t *table, int id)
758{
759 for (; table->id >= 0; ++table) {
760 if (table->id == id)
761 return table;
762 }
763 return NULL;
764}
765
Simon Glass14262202016-06-30 10:52:16 -0600766static const char *unknown_msg(enum ih_category category)
767{
Simon Glassae3de0d2016-10-31 10:21:09 -0600768 static const char unknown_str[] = "Unknown ";
Simon Glass14262202016-06-30 10:52:16 -0600769 static char msg[30];
770
Simon Glassae3de0d2016-10-31 10:21:09 -0600771 strcpy(msg, unknown_str);
772 strncat(msg, table_info[category].desc,
773 sizeof(msg) - sizeof(unknown_str));
Simon Glass14262202016-06-30 10:52:16 -0600774
775 return msg;
776}
777
778/**
Naoki Hayama898a0842020-10-07 11:22:24 +0900779 * genimg_get_cat_name - translate entry id to long name
Simon Glass14262202016-06-30 10:52:16 -0600780 * @category: category to look up (enum ih_category)
781 * @id: entry id to be translated
782 *
783 * This will scan the translation table trying to find the entry that matches
784 * the given id.
785 *
Naoki Hayama898a0842020-10-07 11:22:24 +0900786 * @return long entry name if translation succeeds; error string on failure
Simon Glass14262202016-06-30 10:52:16 -0600787 */
788const char *genimg_get_cat_name(enum ih_category category, uint id)
789{
790 const table_entry_t *entry;
791
792 entry = get_table_entry(table_info[category].table, id);
793 if (!entry)
794 return unknown_msg(category);
795#if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
796 return entry->lname;
797#else
798 return entry->lname + gd->reloc_off;
799#endif
800}
801
802/**
Naoki Hayama898a0842020-10-07 11:22:24 +0900803 * genimg_get_cat_short_name - translate entry id to short name
Simon Glass14262202016-06-30 10:52:16 -0600804 * @category: category to look up (enum ih_category)
805 * @id: entry id to be translated
806 *
807 * This will scan the translation table trying to find the entry that matches
808 * the given id.
809 *
Naoki Hayama898a0842020-10-07 11:22:24 +0900810 * @return short entry name if translation succeeds; error string on failure
Simon Glass14262202016-06-30 10:52:16 -0600811 */
812const char *genimg_get_cat_short_name(enum ih_category category, uint id)
813{
814 const table_entry_t *entry;
815
816 entry = get_table_entry(table_info[category].table, id);
817 if (!entry)
818 return unknown_msg(category);
819#if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
820 return entry->sname;
821#else
822 return entry->sname + gd->reloc_off;
823#endif
824}
825
826int genimg_get_cat_count(enum ih_category category)
827{
828 return table_info[category].count;
829}
830
831const char *genimg_get_cat_desc(enum ih_category category)
832{
833 return table_info[category].desc;
834}
835
Marian Balakowicz570abb02008-02-29 15:59:59 +0100836/**
Naoki Hayama02d41b02020-10-07 11:21:25 +0900837 * genimg_cat_has_id - check whether category has entry id
838 * @category: category to look up (enum ih_category)
839 * @id: entry id to be checked
840 *
841 * This will scan the translation table trying to find the entry that matches
842 * the given id.
843 *
844 * @return true if category has entry id; false if not
845 */
846bool genimg_cat_has_id(enum ih_category category, uint id)
847{
848 if (get_table_entry(table_info[category].table, id))
849 return true;
850
851 return false;
852}
853
854/**
Marian Balakowicz570abb02008-02-29 15:59:59 +0100855 * get_table_entry_name - translate entry id to long name
856 * @table: pointer to a translation table for entries of a specific type
857 * @msg: message to be returned when translation fails
858 * @id: entry id to be translated
859 *
860 * get_table_entry_name() will go over translation table trying to find
861 * entry that matches given id. If matching entry is found, its long
862 * name is returned to the caller.
863 *
864 * returns:
865 * long entry name if translation succeeds
866 * msg otherwise
867 */
Mike Frysinger7edb1862010-10-20 07:17:39 -0400868char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100869{
Simon Glass5b9d44d2015-06-23 15:38:25 -0600870 table = get_table_entry(table, id);
871 if (!table)
872 return msg;
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200873#if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
Simon Glass5b9d44d2015-06-23 15:38:25 -0600874 return table->lname;
Scott Woode3d1ac72009-04-02 16:15:10 -0500875#else
Simon Glass5b9d44d2015-06-23 15:38:25 -0600876 return table->lname + gd->reloc_off;
Scott Woode3d1ac72009-04-02 16:15:10 -0500877#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100878}
879
Stephen Warren712fbcf2011-10-18 11:11:49 +0000880const char *genimg_get_os_name(uint8_t os)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100881{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000882 return (get_table_entry_name(uimage_os, "Unknown OS", os));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100883}
884
Stephen Warren712fbcf2011-10-18 11:11:49 +0000885const char *genimg_get_arch_name(uint8_t arch)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100886{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000887 return (get_table_entry_name(uimage_arch, "Unknown Architecture",
888 arch));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100889}
890
Stephen Warren712fbcf2011-10-18 11:11:49 +0000891const char *genimg_get_type_name(uint8_t type)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100892{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000893 return (get_table_entry_name(uimage_type, "Unknown Image", type));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100894}
895
Naoki Hayama898a0842020-10-07 11:22:24 +0900896const char *genimg_get_comp_name(uint8_t comp)
897{
898 return (get_table_entry_name(uimage_comp, "Unknown Compression",
899 comp));
900}
901
Simon Glasscef2e512016-02-22 22:55:50 -0700902static const char *genimg_get_short_name(const table_entry_t *table, int val)
Simon Glass5b9d44d2015-06-23 15:38:25 -0600903{
Simon Glasscef2e512016-02-22 22:55:50 -0700904 table = get_table_entry(table, val);
Simon Glass5b9d44d2015-06-23 15:38:25 -0600905 if (!table)
906 return "unknown";
907#if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
908 return table->sname;
909#else
910 return table->sname + gd->reloc_off;
911#endif
912}
913
Simon Glasscef2e512016-02-22 22:55:50 -0700914const char *genimg_get_type_short_name(uint8_t type)
915{
916 return genimg_get_short_name(uimage_type, type);
917}
918
Simon Glasscef2e512016-02-22 22:55:50 -0700919const char *genimg_get_comp_short_name(uint8_t comp)
920{
921 return genimg_get_short_name(uimage_comp, comp);
922}
923
924const char *genimg_get_os_short_name(uint8_t os)
925{
926 return genimg_get_short_name(uimage_os, os);
927}
928
929const char *genimg_get_arch_short_name(uint8_t arch)
930{
931 return genimg_get_short_name(uimage_arch, arch);
932}
933
Marian Balakowicz570abb02008-02-29 15:59:59 +0100934/**
935 * get_table_entry_id - translate short entry name to id
936 * @table: pointer to a translation table for entries of a specific type
937 * @table_name: to be used in case of error
938 * @name: entry short name to be translated
939 *
940 * get_table_entry_id() will go over translation table trying to find
941 * entry that matches given short name. If matching entry is found,
942 * its id returned to the caller.
943 *
944 * returns:
945 * entry id if translation succeeds
946 * -1 otherwise
947 */
Mike Frysinger7edb1862010-10-20 07:17:39 -0400948int get_table_entry_id(const table_entry_t *table,
Marian Balakowicz570abb02008-02-29 15:59:59 +0100949 const char *table_name, const char *name)
950{
Mike Frysinger7edb1862010-10-20 07:17:39 -0400951 const table_entry_t *t;
Marian Balakowicz570abb02008-02-29 15:59:59 +0100952
953 for (t = table; t->id >= 0; ++t) {
Simon Glass458b30a2021-09-25 07:03:14 -0600954#if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
Simon Glass5b9d44d2015-06-23 15:38:25 -0600955 if (t->sname && strcasecmp(t->sname + gd->reloc_off, name) == 0)
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200956#else
Simon Glass5b9d44d2015-06-23 15:38:25 -0600957 if (t->sname && strcasecmp(t->sname, name) == 0)
Peter Tyser521af042009-09-21 11:20:36 -0500958#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100959 return (t->id);
960 }
Stephen Warren712fbcf2011-10-18 11:11:49 +0000961 debug("Invalid %s Type: %s\n", table_name, name);
Simon Glass5b9d44d2015-06-23 15:38:25 -0600962
963 return -1;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100964}
965
Stephen Warren712fbcf2011-10-18 11:11:49 +0000966int genimg_get_os_id(const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100967{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000968 return (get_table_entry_id(uimage_os, "OS", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100969}
970
Stephen Warren712fbcf2011-10-18 11:11:49 +0000971int genimg_get_arch_id(const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100972{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000973 return (get_table_entry_id(uimage_arch, "CPU", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100974}
975
Stephen Warren712fbcf2011-10-18 11:11:49 +0000976int genimg_get_type_id(const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100977{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000978 return (get_table_entry_id(uimage_type, "Image", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100979}
980
Stephen Warren712fbcf2011-10-18 11:11:49 +0000981int genimg_get_comp_id(const char *name)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100982{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000983 return (get_table_entry_id(uimage_comp, "Compression", name));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100984}
985
986#ifndef USE_HOSTCC
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100987/**
Bryan Wu6c454fe2014-08-15 16:51:38 -0700988 * genimg_get_kernel_addr_fit - get the real kernel address and return 2
989 * FIT strings
Bryan Wu0f641402014-07-31 17:39:58 -0700990 * @img_addr: a string might contain real image address
Bryan Wu6c454fe2014-08-15 16:51:38 -0700991 * @fit_uname_config: double pointer to a char, will hold pointer to a
992 * configuration unit name
993 * @fit_uname_kernel: double pointer to a char, will hold pointer to a subimage
994 * name
Bryan Wu0f641402014-07-31 17:39:58 -0700995 *
Bryan Wu6c454fe2014-08-15 16:51:38 -0700996 * genimg_get_kernel_addr_fit get the real kernel start address from a string
Bryan Wu0f641402014-07-31 17:39:58 -0700997 * which is normally the first argv of bootm/bootz
998 *
999 * returns:
1000 * kernel start address
1001 */
Bryan Wu6c454fe2014-08-15 16:51:38 -07001002ulong genimg_get_kernel_addr_fit(char * const img_addr,
1003 const char **fit_uname_config,
1004 const char **fit_uname_kernel)
Bryan Wu0f641402014-07-31 17:39:58 -07001005{
Bryan Wu0f641402014-07-31 17:39:58 -07001006 ulong kernel_addr;
1007
1008 /* find out kernel image address */
1009 if (!img_addr) {
Simon Glassbb872dd2019-12-28 10:45:02 -07001010 kernel_addr = image_load_addr;
Bryan Wu0f641402014-07-31 17:39:58 -07001011 debug("* kernel: default image load address = 0x%08lx\n",
Simon Glassbb872dd2019-12-28 10:45:02 -07001012 image_load_addr);
Simon Glass73223f02016-02-22 22:55:43 -07001013#if CONFIG_IS_ENABLED(FIT)
Simon Glassbb872dd2019-12-28 10:45:02 -07001014 } else if (fit_parse_conf(img_addr, image_load_addr, &kernel_addr,
Bryan Wu6c454fe2014-08-15 16:51:38 -07001015 fit_uname_config)) {
Bryan Wu0f641402014-07-31 17:39:58 -07001016 debug("* kernel: config '%s' from image at 0x%08lx\n",
Bryan Wu6c454fe2014-08-15 16:51:38 -07001017 *fit_uname_config, kernel_addr);
Simon Glassbb872dd2019-12-28 10:45:02 -07001018 } else if (fit_parse_subimage(img_addr, image_load_addr, &kernel_addr,
Bryan Wu6c454fe2014-08-15 16:51:38 -07001019 fit_uname_kernel)) {
Bryan Wu0f641402014-07-31 17:39:58 -07001020 debug("* kernel: subimage '%s' from image at 0x%08lx\n",
Bryan Wu6c454fe2014-08-15 16:51:38 -07001021 *fit_uname_kernel, kernel_addr);
Bryan Wu0f641402014-07-31 17:39:58 -07001022#endif
1023 } else {
Simon Glass7e5f4602021-07-24 09:03:29 -06001024 kernel_addr = hextoul(img_addr, NULL);
Bryan Wu0f641402014-07-31 17:39:58 -07001025 debug("* kernel: cmdline image address = 0x%08lx\n",
1026 kernel_addr);
1027 }
1028
1029 return kernel_addr;
1030}
1031
1032/**
Bryan Wu6c454fe2014-08-15 16:51:38 -07001033 * genimg_get_kernel_addr() is the simple version of
1034 * genimg_get_kernel_addr_fit(). It ignores those return FIT strings
1035 */
1036ulong genimg_get_kernel_addr(char * const img_addr)
1037{
1038 const char *fit_uname_config = NULL;
1039 const char *fit_uname_kernel = NULL;
1040
1041 return genimg_get_kernel_addr_fit(img_addr, &fit_uname_config,
1042 &fit_uname_kernel);
1043}
1044
1045/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001046 * genimg_get_format - get image format type
1047 * @img_addr: image start address
1048 *
1049 * genimg_get_format() checks whether provided address points to a valid
1050 * legacy or FIT image.
1051 *
1052 * New uImage format and FDT blob are based on a libfdt. FDT blob
1053 * may be passed directly or embedded in a FIT image. In both situations
1054 * genimg_get_format() must be able to dectect libfdt header.
1055 *
1056 * returns:
1057 * image format type or IMAGE_FORMAT_INVALID if no image is present
1058 */
Simon Glass35e7b0f2013-05-07 06:12:03 +00001059int genimg_get_format(const void *img_addr)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001060{
Tom Rinic76c93a2019-05-23 07:14:07 -04001061#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Wolfgang Denk3a2003f2009-08-19 11:42:56 +02001062 const image_header_t *hdr;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001063
Wolfgang Denk3a2003f2009-08-19 11:42:56 +02001064 hdr = (const image_header_t *)img_addr;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001065 if (image_check_magic(hdr))
Heiko Schocher21d29f72014-05-28 11:33:33 +02001066 return IMAGE_FORMAT_LEGACY;
1067#endif
Simon Glassaa34fbc2016-02-22 22:55:45 -07001068#if IMAGE_ENABLE_FIT || IMAGE_ENABLE_OF_LIBFDT
Heiko Schocher21d29f72014-05-28 11:33:33 +02001069 if (fdt_check_header(img_addr) == 0)
1070 return IMAGE_FORMAT_FIT;
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -05001071#endif
1072#ifdef CONFIG_ANDROID_BOOT_IMAGE
Heiko Schocher21d29f72014-05-28 11:33:33 +02001073 if (android_image_check_header(img_addr) == 0)
1074 return IMAGE_FORMAT_ANDROID;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001075#endif
1076
Heiko Schocher21d29f72014-05-28 11:33:33 +02001077 return IMAGE_FORMAT_INVALID;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001078}
1079
1080/**
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001081 * fit_has_config - check if there is a valid FIT configuration
1082 * @images: pointer to the bootm command headers structure
1083 *
1084 * fit_has_config() checks if there is a FIT configuration in use
1085 * (if FTI support is present).
1086 *
1087 * returns:
1088 * 0, no FIT support or no configuration found
1089 * 1, configuration found
1090 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001091int genimg_has_config(bootm_headers_t *images)
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001092{
Simon Glass73223f02016-02-22 22:55:43 -07001093#if IMAGE_ENABLE_FIT
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001094 if (images->fit_uname_cfg)
1095 return 1;
1096#endif
1097 return 0;
1098}
1099
1100/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001101 * boot_get_ramdisk - main ramdisk handling routine
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001102 * @argc: command argument count
1103 * @argv: command argument list
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +01001104 * @images: pointer to the bootm images structure
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001105 * @arch: expected ramdisk architecture
1106 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
1107 * @rd_end: pointer to a ulong variable, will hold ramdisk end
1108 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001109 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001110 * Curently supported are the following ramdisk sources:
1111 * - multicomponent kernel/ramdisk image,
1112 * - commandline provided address of decicated ramdisk image.
1113 *
1114 * returns:
Marian Balakowiczd985c842008-03-12 10:14:38 +01001115 * 0, if ramdisk image was found and valid, or skiped
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001116 * rd_start and rd_end are set to ramdisk start/end addresses if
1117 * ramdisk image is found and valid
Marian Balakowiczd985c842008-03-12 10:14:38 +01001118 *
Kumar Galaea86b9e2008-08-29 19:08:29 -05001119 * 1, if ramdisk image is found but corrupted, or invalid
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001120 * rd_start and rd_end are set to 0 if no ramdisk exists
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001121 */
Simon Glass09140112020-05-10 11:40:03 -06001122int boot_get_ramdisk(int argc, char *const argv[], bootm_headers_t *images,
1123 uint8_t arch, ulong *rd_start, ulong *rd_end)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001124{
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001125 ulong rd_addr, rd_load;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001126 ulong rd_data, rd_len;
Tom Rinic76c93a2019-05-23 07:14:07 -04001127#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Wolfgang Denk3a2003f2009-08-19 11:42:56 +02001128 const image_header_t *rd_hdr;
Heiko Schocher21d29f72014-05-28 11:33:33 +02001129#endif
Simon Glass35e7b0f2013-05-07 06:12:03 +00001130 void *buf;
Marek Vasut57d40ab2012-03-30 23:19:10 +02001131#ifdef CONFIG_SUPPORT_RAW_INITRD
Marek Vasut017e1f32012-03-18 11:47:58 +00001132 char *end;
Marek Vasut57d40ab2012-03-30 23:19:10 +02001133#endif
Simon Glass73223f02016-02-22 22:55:43 -07001134#if IMAGE_ENABLE_FIT
Simon Glassf320a4d2013-07-10 23:08:10 -07001135 const char *fit_uname_config = images->fit_uname_cfg;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001136 const char *fit_uname_ramdisk = NULL;
1137 ulong default_addr;
Marian Balakowiczc8779642008-03-12 10:12:37 +01001138 int rd_noffset;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001139#endif
Simon Glass983c72f2013-06-11 11:14:46 -07001140 const char *select = NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001141
Marian Balakowiczc8779642008-03-12 10:12:37 +01001142 *rd_start = 0;
1143 *rd_end = 0;
1144
Tom Rini1fec3c52015-08-27 15:42:41 -04001145#ifdef CONFIG_ANDROID_BOOT_IMAGE
1146 /*
1147 * Look for an Android boot image.
1148 */
1149 buf = map_sysmem(images->os.start, 0);
Tom Rinic139b5f2015-10-27 19:04:40 -04001150 if (buf && genimg_get_format(buf) == IMAGE_FORMAT_ANDROID)
Shawn Guo5e218862019-02-22 16:28:06 +08001151 select = (argc == 0) ? env_get("loadaddr") : argv[0];
Tom Rini1fec3c52015-08-27 15:42:41 -04001152#endif
1153
Simon Glass983c72f2013-06-11 11:14:46 -07001154 if (argc >= 2)
1155 select = argv[1];
Rob Herring2dd46322015-07-17 10:57:17 -05001156
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001157 /*
1158 * Look for a '-' which indicates to ignore the
1159 * ramdisk argument
1160 */
Simon Glass983c72f2013-06-11 11:14:46 -07001161 if (select && strcmp(select, "-") == 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001162 debug("## Skipping init Ramdisk\n");
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001163 rd_len = rd_data = 0;
Simon Glass983c72f2013-06-11 11:14:46 -07001164 } else if (select || genimg_has_config(images)) {
Simon Glass73223f02016-02-22 22:55:43 -07001165#if IMAGE_ENABLE_FIT
Simon Glass983c72f2013-06-11 11:14:46 -07001166 if (select) {
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001167 /*
1168 * If the init ramdisk comes from the FIT image and
1169 * the FIT image address is omitted in the command
1170 * line argument, try to use os FIT image address or
1171 * default load address.
1172 */
1173 if (images->fit_uname_os)
1174 default_addr = (ulong)images->fit_hdr_os;
1175 else
Simon Glassbb872dd2019-12-28 10:45:02 -07001176 default_addr = image_load_addr;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001177
Simon Glass983c72f2013-06-11 11:14:46 -07001178 if (fit_parse_conf(select, default_addr,
1179 &rd_addr, &fit_uname_config)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001180 debug("* ramdisk: config '%s' from image at "
1181 "0x%08lx\n",
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001182 fit_uname_config, rd_addr);
Simon Glass983c72f2013-06-11 11:14:46 -07001183 } else if (fit_parse_subimage(select, default_addr,
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001184 &rd_addr, &fit_uname_ramdisk)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001185 debug("* ramdisk: subimage '%s' from image at "
1186 "0x%08lx\n",
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001187 fit_uname_ramdisk, rd_addr);
1188 } else
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001189#endif
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001190 {
Simon Glass7e5f4602021-07-24 09:03:29 -06001191 rd_addr = hextoul(select, NULL);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001192 debug("* ramdisk: cmdline image address = "
1193 "0x%08lx\n",
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001194 rd_addr);
1195 }
Simon Glass73223f02016-02-22 22:55:43 -07001196#if IMAGE_ENABLE_FIT
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001197 } else {
1198 /* use FIT configuration provided in first bootm
Simon Glassa51ec632013-05-16 13:53:22 +00001199 * command argument. If the property is not defined,
1200 * quit silently.
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001201 */
Simon Glass35e7b0f2013-05-07 06:12:03 +00001202 rd_addr = map_to_sysmem(images->fit_hdr_os);
Simon Glassa51ec632013-05-16 13:53:22 +00001203 rd_noffset = fit_get_node_from_config(images,
1204 FIT_RAMDISK_PROP, rd_addr);
Paul Burtonbd86ef12016-09-20 18:17:12 +01001205 if (rd_noffset == -ENOENT)
Peter Tyser41266c92008-08-05 10:51:57 -05001206 return 0;
Simon Glassa51ec632013-05-16 13:53:22 +00001207 else if (rd_noffset < 0)
1208 return 1;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001209 }
Marian Balakowiczf773bea2008-03-12 10:35:46 +01001210#endif
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001211
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001212 /*
1213 * Check if there is an initrd image at the
1214 * address provided in the second bootm argument
1215 * check image type, for FIT images get FIT node.
1216 */
Simon Glass35e7b0f2013-05-07 06:12:03 +00001217 buf = map_sysmem(rd_addr, 0);
1218 switch (genimg_get_format(buf)) {
Tom Rinic76c93a2019-05-23 07:14:07 -04001219#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001220 case IMAGE_FORMAT_LEGACY:
Stephen Warren712fbcf2011-10-18 11:11:49 +00001221 printf("## Loading init Ramdisk from Legacy "
Marian Balakowiczc8779642008-03-12 10:12:37 +01001222 "Image at %08lx ...\n", rd_addr);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001223
Simon Glass770605e2012-02-13 13:51:18 +00001224 bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001225 rd_hdr = image_get_ramdisk(rd_addr, arch,
Marian Balakowiczd985c842008-03-12 10:14:38 +01001226 images->verify);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001227
Marian Balakowiczc8779642008-03-12 10:12:37 +01001228 if (rd_hdr == NULL)
Kumar Gala274cea22008-02-27 21:51:46 -06001229 return 1;
Kumar Gala274cea22008-02-27 21:51:46 -06001230
Stephen Warren712fbcf2011-10-18 11:11:49 +00001231 rd_data = image_get_data(rd_hdr);
1232 rd_len = image_get_data_size(rd_hdr);
1233 rd_load = image_get_load(rd_hdr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001234 break;
Heiko Schocher21d29f72014-05-28 11:33:33 +02001235#endif
Simon Glass73223f02016-02-22 22:55:43 -07001236#if IMAGE_ENABLE_FIT
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001237 case IMAGE_FORMAT_FIT:
Simon Glass126cc862014-06-12 07:24:47 -06001238 rd_noffset = fit_image_load(images,
Simon Glassa51ec632013-05-16 13:53:22 +00001239 rd_addr, &fit_uname_ramdisk,
Simon Glassf320a4d2013-07-10 23:08:10 -07001240 &fit_uname_config, arch,
Simon Glassa51ec632013-05-16 13:53:22 +00001241 IH_TYPE_RAMDISK,
1242 BOOTSTAGE_ID_FIT_RD_START,
Simon Glassfe20a812014-08-22 14:26:43 -06001243 FIT_LOAD_OPTIONAL_NON_ZERO,
1244 &rd_data, &rd_len);
Simon Glassa51ec632013-05-16 13:53:22 +00001245 if (rd_noffset < 0)
Michal Simekc78fce62008-07-11 10:43:13 +02001246 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +01001247
Simon Glassa51ec632013-05-16 13:53:22 +00001248 images->fit_hdr_rd = map_sysmem(rd_addr, 0);
Marian Balakowiczc8779642008-03-12 10:12:37 +01001249 images->fit_uname_rd = fit_uname_ramdisk;
Marian Balakowicz3dfe1102008-03-12 10:32:59 +01001250 images->fit_noffset_rd = rd_noffset;
Marian Balakowiczc8779642008-03-12 10:12:37 +01001251 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001252#endif
Rob Herring2dd46322015-07-17 10:57:17 -05001253#ifdef CONFIG_ANDROID_BOOT_IMAGE
1254 case IMAGE_FORMAT_ANDROID:
1255 android_image_get_ramdisk((void *)images->os.start,
1256 &rd_data, &rd_len);
1257 break;
1258#endif
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001259 default:
Marek Vasut017e1f32012-03-18 11:47:58 +00001260#ifdef CONFIG_SUPPORT_RAW_INITRD
Simon Glass983c72f2013-06-11 11:14:46 -07001261 end = NULL;
1262 if (select)
1263 end = strchr(select, ':');
1264 if (end) {
Simon Glass7e5f4602021-07-24 09:03:29 -06001265 rd_len = hextoul(++end, NULL);
Marek Vasut017e1f32012-03-18 11:47:58 +00001266 rd_data = rd_addr;
1267 } else
1268#endif
1269 {
1270 puts("Wrong Ramdisk Image Format\n");
1271 rd_data = rd_len = rd_load = 0;
1272 return 1;
1273 }
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001274 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001275 } else if (images->legacy_hdr_valid &&
Stephen Warren712fbcf2011-10-18 11:11:49 +00001276 image_check_type(&images->legacy_hdr_os_copy,
1277 IH_TYPE_MULTI)) {
1278
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001279 /*
1280 * Now check if we have a legacy mult-component image,
1281 * get second entry data start address and len.
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001282 */
Simon Glass770605e2012-02-13 13:51:18 +00001283 bootstage_mark(BOOTSTAGE_ID_RAMDISK);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001284 printf("## Loading init Ramdisk from multi component "
Marian Balakowiczc8779642008-03-12 10:12:37 +01001285 "Legacy Image at %08lx ...\n",
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001286 (ulong)images->legacy_hdr_os);
1287
Stephen Warren712fbcf2011-10-18 11:11:49 +00001288 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
Rob Herring2dd46322015-07-17 10:57:17 -05001289 } else {
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001290 /*
1291 * no initrd image
1292 */
Simon Glass770605e2012-02-13 13:51:18 +00001293 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001294 rd_len = rd_data = 0;
1295 }
1296
1297 if (!rd_data) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001298 debug("## No init Ramdisk\n");
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001299 } else {
1300 *rd_start = rd_data;
1301 *rd_end = rd_data + rd_len;
1302 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00001303 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001304 *rd_start, *rd_end);
Kumar Gala274cea22008-02-27 21:51:46 -06001305
1306 return 0;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001307}
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001308
John Rigbyfca43cc2010-10-13 13:57:35 -06001309#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001310/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001311 * boot_ramdisk_high - relocate init ramdisk
Kumar Galae822d7f2008-02-27 21:51:49 -06001312 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001313 * @rd_data: ramdisk data start address
1314 * @rd_len: ramdisk data length
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001315 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
1316 * start address (after possible relocation)
1317 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
1318 * end address (after possible relocation)
1319 *
Robert P. J. Day1bce2ae2013-09-16 07:15:45 -04001320 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001321 * variable and if requested ramdisk data is moved to a specified location.
1322 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001323 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1324 * start/end addresses if ramdisk image start and len were provided,
1325 * otherwise set initrd_start and initrd_end set to zeros.
1326 *
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001327 * returns:
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001328 * 0 - success
1329 * -1 - failure
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001330 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001331int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
Kumar Galae822d7f2008-02-27 21:51:49 -06001332 ulong *initrd_start, ulong *initrd_end)
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001333{
1334 char *s;
1335 ulong initrd_high;
1336 int initrd_copy_to_ram = 1;
1337
Simon Glass00caae62017-08-03 12:22:12 -06001338 s = env_get("initrd_high");
1339 if (s) {
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001340 /* a value of "no" or a similar string will act like 0,
1341 * turning the "load high" feature off. This is intentional.
1342 */
Simon Glass7e5f4602021-07-24 09:03:29 -06001343 initrd_high = hextoul(s, NULL);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001344 if (initrd_high == ~0)
1345 initrd_copy_to_ram = 0;
1346 } else {
Simon Glass723806c2017-08-03 12:22:15 -06001347 initrd_high = env_get_bootm_mapsize() + env_get_bootm_low();
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001348 }
1349
Marian Balakowicz95d449a2008-05-13 15:53:29 +02001350
Stephen Warren712fbcf2011-10-18 11:11:49 +00001351 debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001352 initrd_high, initrd_copy_to_ram);
1353
1354 if (rd_data) {
1355 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001356 debug(" in-place initrd\n");
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001357 *initrd_start = rd_data;
1358 *initrd_end = rd_data + rd_len;
Kumar Galae822d7f2008-02-27 21:51:49 -06001359 lmb_reserve(lmb, rd_data, rd_len);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001360 } else {
Kumar Galae822d7f2008-02-27 21:51:49 -06001361 if (initrd_high)
Stephen Warren712fbcf2011-10-18 11:11:49 +00001362 *initrd_start = (ulong)lmb_alloc_base(lmb,
1363 rd_len, 0x1000, initrd_high);
Kumar Galae822d7f2008-02-27 21:51:49 -06001364 else
Stephen Warren712fbcf2011-10-18 11:11:49 +00001365 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
1366 0x1000);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001367
Kumar Galae822d7f2008-02-27 21:51:49 -06001368 if (*initrd_start == 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001369 puts("ramdisk - allocation error\n");
Kumar Galae822d7f2008-02-27 21:51:49 -06001370 goto error;
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001371 }
Simon Glass770605e2012-02-13 13:51:18 +00001372 bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001373
1374 *initrd_end = *initrd_start + rd_len;
Stephen Warren712fbcf2011-10-18 11:11:49 +00001375 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001376 *initrd_start, *initrd_end);
1377
Stephen Warren712fbcf2011-10-18 11:11:49 +00001378 memmove_wd((void *)*initrd_start,
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001379 (void *)rd_data, rd_len, CHUNKSZ);
1380
Kumar Gala3b200112011-12-07 04:42:58 +00001381#ifdef CONFIG_MP
1382 /*
1383 * Ensure the image is flushed to memory to handle
1384 * AMP boot scenarios in which we might not be
1385 * HW cache coherent
1386 */
Heiko Schocher1a1e7072017-12-14 11:19:22 +01001387 flush_cache((unsigned long)*initrd_start,
1388 ALIGN(rd_len, ARCH_DMA_MINALIGN));
Kumar Gala3b200112011-12-07 04:42:58 +00001389#endif
Stephen Warren712fbcf2011-10-18 11:11:49 +00001390 puts("OK\n");
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001391 }
1392 } else {
1393 *initrd_start = 0;
1394 *initrd_end = 0;
1395 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00001396 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001397 *initrd_start, *initrd_end);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001398
Kumar Galae822d7f2008-02-27 21:51:49 -06001399 return 0;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001400
Kumar Galae822d7f2008-02-27 21:51:49 -06001401error:
1402 return -1;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001403}
John Rigbyfca43cc2010-10-13 13:57:35 -06001404#endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001405
Simon Glass90268b82014-10-19 21:11:24 -06001406int boot_get_setup(bootm_headers_t *images, uint8_t arch,
1407 ulong *setup_start, ulong *setup_len)
1408{
Simon Glass73223f02016-02-22 22:55:43 -07001409#if IMAGE_ENABLE_FIT
Simon Glass90268b82014-10-19 21:11:24 -06001410 return boot_get_setup_fit(images, arch, setup_start, setup_len);
1411#else
1412 return -ENOENT;
1413#endif
1414}
1415
Simon Glass73223f02016-02-22 22:55:43 -07001416#if IMAGE_ENABLE_FIT
Goldschmidt Simon8b93a922017-11-10 14:17:41 +00001417#if defined(CONFIG_FPGA)
Simon Glass09140112020-05-10 11:40:03 -06001418int boot_get_fpga(int argc, char *const argv[], bootm_headers_t *images,
Michal Simek62afc602016-05-17 14:03:50 +02001419 uint8_t arch, const ulong *ld_start, ulong * const ld_len)
1420{
1421 ulong tmp_img_addr, img_data, img_len;
1422 void *buf;
1423 int conf_noffset;
1424 int fit_img_result;
Simon Glassb02e4042016-10-02 17:59:28 -06001425 const char *uname, *name;
Michal Simek62afc602016-05-17 14:03:50 +02001426 int err;
1427 int devnum = 0; /* TODO support multi fpga platforms */
Michal Simek62afc602016-05-17 14:03:50 +02001428
1429 /* Check to see if the images struct has a FIT configuration */
1430 if (!genimg_has_config(images)) {
1431 debug("## FIT configuration was not specified\n");
1432 return 0;
1433 }
1434
1435 /*
1436 * Obtain the os FIT header from the images struct
Michal Simek62afc602016-05-17 14:03:50 +02001437 */
1438 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
Michal Simek62afc602016-05-17 14:03:50 +02001439 buf = map_sysmem(tmp_img_addr, 0);
1440 /*
1441 * Check image type. For FIT images get FIT node
1442 * and attempt to locate a generic binary.
1443 */
1444 switch (genimg_get_format(buf)) {
1445 case IMAGE_FORMAT_FIT:
1446 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
1447
Simon Glassb02e4042016-10-02 17:59:28 -06001448 uname = fdt_stringlist_get(buf, conf_noffset, FIT_FPGA_PROP, 0,
1449 NULL);
1450 if (!uname) {
Michal Simek62afc602016-05-17 14:03:50 +02001451 debug("## FPGA image is not specified\n");
1452 return 0;
1453 }
1454 fit_img_result = fit_image_load(images,
1455 tmp_img_addr,
1456 (const char **)&uname,
1457 &(images->fit_uname_cfg),
1458 arch,
1459 IH_TYPE_FPGA,
1460 BOOTSTAGE_ID_FPGA_INIT,
1461 FIT_LOAD_OPTIONAL_NON_ZERO,
1462 &img_data, &img_len);
1463
1464 debug("FPGA image (%s) loaded to 0x%lx/size 0x%lx\n",
1465 uname, img_data, img_len);
1466
1467 if (fit_img_result < 0) {
1468 /* Something went wrong! */
1469 return fit_img_result;
1470 }
1471
Goldschmidt Simon8b93a922017-11-10 14:17:41 +00001472 if (!fpga_is_partial_data(devnum, img_len)) {
Michal Simek62afc602016-05-17 14:03:50 +02001473 name = "full";
1474 err = fpga_loadbitstream(devnum, (char *)img_data,
1475 img_len, BIT_FULL);
1476 if (err)
1477 err = fpga_load(devnum, (const void *)img_data,
1478 img_len, BIT_FULL);
1479 } else {
1480 name = "partial";
1481 err = fpga_loadbitstream(devnum, (char *)img_data,
1482 img_len, BIT_PARTIAL);
1483 if (err)
1484 err = fpga_load(devnum, (const void *)img_data,
1485 img_len, BIT_PARTIAL);
1486 }
1487
Michal Simek62afc602016-05-17 14:03:50 +02001488 if (err)
Michal Simek611a9422016-12-16 10:35:40 +01001489 return err;
1490
1491 printf(" Programming %s bitstream... OK\n", name);
Michal Simek62afc602016-05-17 14:03:50 +02001492 break;
1493 default:
1494 printf("The given image format is not supported (corrupt?)\n");
1495 return 1;
1496 }
1497
1498 return 0;
1499}
1500#endif
1501
Andrew F. Davisd7be5092016-11-29 16:33:20 -06001502static void fit_loadable_process(uint8_t img_type,
1503 ulong img_data,
1504 ulong img_len)
1505{
1506 int i;
1507 const unsigned int count =
1508 ll_entry_count(struct fit_loadable_tbl, fit_loadable);
1509 struct fit_loadable_tbl *fit_loadable_handler =
1510 ll_entry_start(struct fit_loadable_tbl, fit_loadable);
1511 /* For each loadable handler */
1512 for (i = 0; i < count; i++, fit_loadable_handler++)
1513 /* matching this type */
1514 if (fit_loadable_handler->type == img_type)
1515 /* call that handler with this image data */
1516 fit_loadable_handler->handler(img_data, img_len);
1517}
1518
Simon Glass09140112020-05-10 11:40:03 -06001519int boot_get_loadable(int argc, char *const argv[], bootm_headers_t *images,
1520 uint8_t arch, const ulong *ld_start, ulong * const ld_len)
Karl Apsite84a07db2015-05-21 09:52:48 -04001521{
1522 /*
1523 * These variables are used to hold the current image location
1524 * in system memory.
1525 */
1526 ulong tmp_img_addr;
1527 /*
1528 * These two variables are requirements for fit_image_load, but
1529 * their values are not used
1530 */
1531 ulong img_data, img_len;
1532 void *buf;
1533 int loadables_index;
1534 int conf_noffset;
1535 int fit_img_result;
Simon Glassb02e4042016-10-02 17:59:28 -06001536 const char *uname;
Andrew F. Davisd7be5092016-11-29 16:33:20 -06001537 uint8_t img_type;
Karl Apsite84a07db2015-05-21 09:52:48 -04001538
1539 /* Check to see if the images struct has a FIT configuration */
1540 if (!genimg_has_config(images)) {
1541 debug("## FIT configuration was not specified\n");
1542 return 0;
1543 }
1544
1545 /*
1546 * Obtain the os FIT header from the images struct
Karl Apsite84a07db2015-05-21 09:52:48 -04001547 */
1548 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
Karl Apsite84a07db2015-05-21 09:52:48 -04001549 buf = map_sysmem(tmp_img_addr, 0);
1550 /*
1551 * Check image type. For FIT images get FIT node
1552 * and attempt to locate a generic binary.
1553 */
1554 switch (genimg_get_format(buf)) {
1555 case IMAGE_FORMAT_FIT:
1556 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
1557
1558 for (loadables_index = 0;
Simon Glassb02e4042016-10-02 17:59:28 -06001559 uname = fdt_stringlist_get(buf, conf_noffset,
1560 FIT_LOADABLE_PROP, loadables_index,
1561 NULL), uname;
Karl Apsite84a07db2015-05-21 09:52:48 -04001562 loadables_index++)
1563 {
1564 fit_img_result = fit_image_load(images,
1565 tmp_img_addr,
Simon Glassb02e4042016-10-02 17:59:28 -06001566 &uname,
Karl Apsite84a07db2015-05-21 09:52:48 -04001567 &(images->fit_uname_cfg), arch,
1568 IH_TYPE_LOADABLE,
1569 BOOTSTAGE_ID_FIT_LOADABLE_START,
1570 FIT_LOAD_OPTIONAL_NON_ZERO,
1571 &img_data, &img_len);
1572 if (fit_img_result < 0) {
1573 /* Something went wrong! */
1574 return fit_img_result;
1575 }
Andrew F. Davisd7be5092016-11-29 16:33:20 -06001576
1577 fit_img_result = fit_image_get_node(buf, uname);
1578 if (fit_img_result < 0) {
1579 /* Something went wrong! */
1580 return fit_img_result;
1581 }
1582 fit_img_result = fit_image_get_type(buf,
1583 fit_img_result,
1584 &img_type);
1585 if (fit_img_result < 0) {
1586 /* Something went wrong! */
1587 return fit_img_result;
1588 }
1589
1590 fit_loadable_process(img_type, img_data, img_len);
Karl Apsite84a07db2015-05-21 09:52:48 -04001591 }
1592 break;
1593 default:
1594 printf("The given image format is not supported (corrupt?)\n");
1595 return 1;
1596 }
1597
1598 return 0;
1599}
1600#endif
1601
John Rigbyfca43cc2010-10-13 13:57:35 -06001602#ifdef CONFIG_SYS_BOOT_GET_CMDLINE
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001603/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001604 * boot_get_cmdline - allocate and initialize kernel cmdline
Kumar Galae822d7f2008-02-27 21:51:49 -06001605 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001606 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1607 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1608 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001609 * boot_get_cmdline() allocates space for kernel command line below
Shyam Saini919d25c2018-06-07 19:47:19 +05301610 * BOOTMAPSZ + env_get_bootm_low() address. If "bootargs" U-Boot environment
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001611 * variable is present its contents is copied to allocated kernel
1612 * command line.
1613 *
1614 * returns:
Kumar Galae822d7f2008-02-27 21:51:49 -06001615 * 0 - success
1616 * -1 - failure
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001617 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001618int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001619{
1620 char *cmdline;
1621 char *s;
1622
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001623 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
Simon Glass723806c2017-08-03 12:22:15 -06001624 env_get_bootm_mapsize() + env_get_bootm_low());
Kumar Galae822d7f2008-02-27 21:51:49 -06001625
1626 if (cmdline == NULL)
1627 return -1;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001628
Simon Glass00caae62017-08-03 12:22:12 -06001629 s = env_get("bootargs");
1630 if (!s)
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001631 s = "";
1632
1633 strcpy(cmdline, s);
1634
1635 *cmd_start = (ulong) & cmdline[0];
1636 *cmd_end = *cmd_start + strlen(cmdline);
1637
Stephen Warren712fbcf2011-10-18 11:11:49 +00001638 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001639
Kumar Galae822d7f2008-02-27 21:51:49 -06001640 return 0;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001641}
John Rigbyfca43cc2010-10-13 13:57:35 -06001642#endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001643
John Rigbyfca43cc2010-10-13 13:57:35 -06001644#ifdef CONFIG_SYS_BOOT_GET_KBD
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001645/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001646 * boot_get_kbd - allocate and initialize kernel copy of board info
Kumar Galae822d7f2008-02-27 21:51:49 -06001647 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001648 * @kbd: double pointer to board info data
1649 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001650 * boot_get_kbd() allocates space for kernel copy of board info data below
Simon Glass723806c2017-08-03 12:22:15 -06001651 * BOOTMAPSZ + env_get_bootm_low() address and kernel board info is initialized
Grant Likely590d3ca2011-03-28 09:58:34 +00001652 * with the current u-boot board info data.
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001653 *
1654 * returns:
Kumar Galae822d7f2008-02-27 21:51:49 -06001655 * 0 - success
1656 * -1 - failure
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001657 */
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +09001658int boot_get_kbd(struct lmb *lmb, struct bd_info **kbd)
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001659{
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +09001660 *kbd = (struct bd_info *)(ulong)lmb_alloc_base(lmb,
1661 sizeof(struct bd_info),
1662 0xf,
1663 env_get_bootm_mapsize() + env_get_bootm_low());
Kumar Galae822d7f2008-02-27 21:51:49 -06001664 if (*kbd == NULL)
1665 return -1;
1666
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001667 **kbd = *(gd->bd);
1668
Stephen Warren712fbcf2011-10-18 11:11:49 +00001669 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001670
1671#if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1672 do_bdinfo(NULL, 0, 0, NULL);
1673#endif
1674
Kumar Galae822d7f2008-02-27 21:51:49 -06001675 return 0;
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001676}
John Rigbyfca43cc2010-10-13 13:57:35 -06001677#endif /* CONFIG_SYS_BOOT_GET_KBD */
Simon Glass13d06982013-05-08 08:06:01 +00001678
1679#ifdef CONFIG_LMB
1680int image_setup_linux(bootm_headers_t *images)
1681{
1682 ulong of_size = images->ft_len;
1683 char **of_flat_tree = &images->ft_addr;
Simon Glass13d06982013-05-08 08:06:01 +00001684 struct lmb *lmb = &images->lmb;
Simon Glass13d06982013-05-08 08:06:01 +00001685 int ret;
1686
1687 if (IMAGE_ENABLE_OF_LIBFDT)
1688 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
1689
1690 if (IMAGE_BOOT_GET_CMDLINE) {
1691 ret = boot_get_cmdline(lmb, &images->cmdline_start,
1692 &images->cmdline_end);
1693 if (ret) {
1694 puts("ERROR with allocation of cmdline\n");
1695 return ret;
1696 }
1697 }
Simon Glass13d06982013-05-08 08:06:01 +00001698
1699 if (IMAGE_ENABLE_OF_LIBFDT) {
1700 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
1701 if (ret)
1702 return ret;
1703 }
1704
1705 if (IMAGE_ENABLE_OF_LIBFDT && of_size) {
1706 ret = image_setup_libfdt(images, *of_flat_tree, of_size, lmb);
1707 if (ret)
1708 return ret;
1709 }
1710
1711 return 0;
1712}
1713#endif /* CONFIG_LMB */
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001714#endif /* !USE_HOSTCC */