]> Git Repo - J-u-boot.git/blame - common/image.c
btrfs: Use U-Boot API for decompression
[J-u-boot.git] / common / image.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
b97a2a0a
MB
2/*
3 * (C) Copyright 2008 Semihalf
4 *
5 * (C) Copyright 2000-2006
6 * Wolfgang Denk, DENX Software Engineering, [email protected].
b97a2a0a 7 */
ceaed2b1 8
b97a2a0a 9#ifndef USE_HOSTCC
5ad03eb3 10#include <common.h>
52f24238 11#include <bootstage.h>
1eb69ae4 12#include <cpu_func.h>
7b51b576 13#include <env.h>
4d72caa5 14#include <lmb.h>
f7ae49fc 15#include <log.h>
336d4615 16#include <malloc.h>
90526e9f 17#include <asm/cache.h>
3db71108 18#include <u-boot/crc.h>
5ad03eb3
MB
19#include <watchdog.h>
20
21#ifdef CONFIG_SHOW_BOOT_PROGRESS
22#include <status_led.h>
23#endif
24
94d0a2ef 25#include <abuf.h>
2242f536 26#include <rtc.h>
2242f536 27
0c670fc1 28#include <gzip.h>
5dfb5213 29#include <image.h>
6c03f9e6 30#include <lz4.h>
0eb25b61 31#include <mapmem.h>
5dfb5213 32
aa34fbc0 33#if IMAGE_ENABLE_FIT || IMAGE_ENABLE_OF_LIBFDT
b08c8c48 34#include <linux/libfdt.h>
fff888a1 35#include <fdt_support.h>
62afc601
MS
36#include <fpga.h>
37#include <xilinx.h>
c8779648
MB
38#endif
39
401d1c4f 40#include <asm/global_data.h>
20a14a42 41#include <u-boot/md5.h>
2b9912e6 42#include <u-boot/sha1.h>
1221ce45 43#include <linux/errno.h>
35e7b0f1 44#include <asm/io.h>
c8779648 45
2090854c
JW
46#include <bzlib.h>
47#include <linux/lzo.h>
48#include <lzma/LzmaTypes.h>
49#include <lzma/LzmaDec.h>
50#include <lzma/LzmaTools.h>
26073f9e 51#include <linux/zstd.h>
2090854c 52
b6b0fe64 53#ifdef CONFIG_CMD_BDI
09140113
SG
54extern int do_bdinfo(struct cmd_tbl *cmdtp, int flag, int argc,
55 char *const argv[]);
b6b0fe64
MB
56#endif
57
58DECLARE_GLOBAL_DATA_PTR;
8a5ea3e6 59
c76c93a3 60#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
712fbcf3 61static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
d985c849 62 int verify);
21d29f7f 63#endif
b97a2a0a 64#else
5ad03eb3 65#include "mkimage.h"
20a14a42 66#include <u-boot/md5.h>
5dfb5213 67#include <time.h>
b97a2a0a 68#include <image.h>
80402f34
HS
69
70#ifndef __maybe_unused
71# define __maybe_unused /* unimplemented */
72#endif
5dfb5213 73#endif /* !USE_HOSTCC*/
b97a2a0a 74
0ccff500 75#include <u-boot/crc.h>
5b20d141 76#include <imximage.h>
0ccff500 77
13d06981
SG
78#ifndef CONFIG_SYS_BARGSIZE
79#define CONFIG_SYS_BARGSIZE 512
80#endif
81
7edb186f 82static const table_entry_t uimage_arch[] = {
30495bff 83 { IH_ARCH_INVALID, "invalid", "Invalid ARCH", },
570abb0a
MB
84 { IH_ARCH_ALPHA, "alpha", "Alpha", },
85 { IH_ARCH_ARM, "arm", "ARM", },
86 { IH_ARCH_I386, "x86", "Intel x86", },
87 { IH_ARCH_IA64, "ia64", "IA64", },
88 { IH_ARCH_M68K, "m68k", "M68K", },
89 { IH_ARCH_MICROBLAZE, "microblaze", "MicroBlaze", },
90 { IH_ARCH_MIPS, "mips", "MIPS", },
91 { IH_ARCH_MIPS64, "mips64", "MIPS 64 Bit", },
570abb0a 92 { IH_ARCH_NIOS2, "nios2", "NIOS II", },
e419e12d 93 { IH_ARCH_PPC, "powerpc", "PowerPC", },
570abb0a
MB
94 { IH_ARCH_PPC, "ppc", "PowerPC", },
95 { IH_ARCH_S390, "s390", "IBM S390", },
96 { IH_ARCH_SH, "sh", "SuperH", },
97 { IH_ARCH_SPARC, "sparc", "SPARC", },
98 { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", },
99 { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", },
100 { IH_ARCH_AVR32, "avr32", "AVR32", },
64d61461 101 { IH_ARCH_NDS32, "nds32", "NDS32", },
3ddcaccd 102 { IH_ARCH_OPENRISC, "or1k", "OpenRISC 1000",},
35e7b0f1 103 { IH_ARCH_SANDBOX, "sandbox", "Sandbox", },
0ae76531 104 { IH_ARCH_ARM64, "arm64", "AArch64", },
bc5d5428 105 { IH_ARCH_ARC, "arc", "ARC", },
5bda35cf 106 { IH_ARCH_X86_64, "x86_64", "AMD x86_64", },
de5e5cea 107 { IH_ARCH_XTENSA, "xtensa", "Xtensa", },
86aa65a0 108 { IH_ARCH_RISCV, "riscv", "RISC-V", },
570abb0a
MB
109 { -1, "", "", },
110};
111
7edb186f 112static const table_entry_t uimage_os[] = {
30495bff 113 { IH_OS_INVALID, "invalid", "Invalid OS", },
4914af12 114 { IH_OS_ARM_TRUSTED_FIRMWARE, "arm-trusted-firmware", "ARM Trusted Firmware" },
570abb0a
MB
115 { IH_OS_LINUX, "linux", "Linux", },
116#if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
117 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
118#endif
119 { IH_OS_NETBSD, "netbsd", "NetBSD", },
3df61957 120 { IH_OS_OSE, "ose", "Enea OSE", },
04d41409 121 { IH_OS_PLAN9, "plan9", "Plan 9", },
570abb0a 122 { IH_OS_RTEMS, "rtems", "RTEMS", },
45b55712 123 { IH_OS_TEE, "tee", "Trusted Execution Environment" },
570abb0a 124 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
68b15e83 125 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
570abb0a
MB
126#if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
127 { IH_OS_QNX, "qnx", "QNX", },
570abb0a 128#endif
f5ed9e39
PT
129#if defined(CONFIG_INTEGRITY) || defined(USE_HOSTCC)
130 { IH_OS_INTEGRITY,"integrity", "INTEGRITY", },
131#endif
570abb0a
MB
132#ifdef USE_HOSTCC
133 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
134 { IH_OS_DELL, "dell", "Dell", },
135 { IH_OS_ESIX, "esix", "Esix", },
136 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
137 { IH_OS_IRIX, "irix", "Irix", },
138 { IH_OS_NCR, "ncr", "NCR", },
139 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
140 { IH_OS_PSOS, "psos", "pSOS", },
141 { IH_OS_SCO, "sco", "SCO", },
142 { IH_OS_SOLARIS, "solaris", "Solaris", },
143 { IH_OS_SVR4, "svr4", "SVR4", },
144#endif
67ddd955
MV
145#if defined(CONFIG_BOOTM_OPENRTOS) || defined(USE_HOSTCC)
146 { IH_OS_OPENRTOS, "openrtos", "OpenRTOS", },
147#endif
5e30e45c 148 { IH_OS_OPENSBI, "opensbi", "RISC-V OpenSBI", },
a031b03f 149 { IH_OS_EFI, "efi", "EFI Firmware" },
67ddd955 150
570abb0a
MB
151 { -1, "", "", },
152};
153
7edb186f 154static const table_entry_t uimage_type[] = {
4962e38e 155 { IH_TYPE_AISIMAGE, "aisimage", "Davinci AIS image",},
570abb0a
MB
156 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
157 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
3decb14a 158 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
bf411ea9 159 { IH_TYPE_GPIMAGE, "gpimage", "TI Keystone SPL Image",},
570abb0a 160 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
b9b50e89 161 { IH_TYPE_KERNEL_NOLOAD, "kernel_noload", "Kernel Image (no loading done)", },
4962e38e
SB
162 { IH_TYPE_KWBIMAGE, "kwbimage", "Kirkwood Boot Image",},
163 { IH_TYPE_IMXIMAGE, "imximage", "Freescale i.MX Boot Image",},
a2b96ece 164 { IH_TYPE_IMX8IMAGE, "imx8image", "NXP i.MX8 Boot Image",},
6609c266 165 { IH_TYPE_IMX8MIMAGE, "imx8mimage", "NXP i.MX8M Boot Image",},
30495bff 166 { IH_TYPE_INVALID, "invalid", "Invalid Image", },
570abb0a 167 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
4962e38e 168 { IH_TYPE_OMAPIMAGE, "omapimage", "TI OMAP SPL With GP CH",},
5d898a00 169 { IH_TYPE_PBLIMAGE, "pblimage", "Freescale PBL Boot Image",},
570abb0a
MB
170 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
171 { IH_TYPE_SCRIPT, "script", "Script", },
662abc4f
MV
172 { IH_TYPE_SOCFPGAIMAGE, "socfpgaimage", "Altera SoCFPGA CV/AV preloader",},
173 { IH_TYPE_SOCFPGAIMAGE_V1, "socfpgaimage_v1", "Altera SoCFPGA A10 preloader",},
570abb0a 174 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
7816f2cf 175 { IH_TYPE_UBLIMAGE, "ublimage", "Davinci UBL image",},
bce88370 176 { IH_TYPE_MXSIMAGE, "mxsimage", "Freescale MXS Boot Image",},
7b1a4117 177 { IH_TYPE_ATMELIMAGE, "atmelimage", "ATMEL ROM-Boot Image",},
90268b87 178 { IH_TYPE_X86_SETUP, "x86_setup", "x86 setup.bin", },
39f520bb 179 { IH_TYPE_LPC32XXIMAGE, "lpc32xximage", "LPC32XX Boot Image", },
a131c1f4 180 { IH_TYPE_RKIMAGE, "rkimage", "Rockchip Boot Image" },
f9a3c278 181 { IH_TYPE_RKSD, "rksd", "Rockchip SD Boot Image" },
10b84fe1 182 { IH_TYPE_RKSPI, "rkspi", "Rockchip SPI Boot Image" },
ed0c2c0a 183 { IH_TYPE_VYBRIDIMAGE, "vybridimage", "Vybrid Boot Image", },
66eef1e7 184 { IH_TYPE_ZYNQIMAGE, "zynqimage", "Xilinx Zynq Boot Image" },
d9b58b30 185 { IH_TYPE_ZYNQMPIMAGE, "zynqmpimage", "Xilinx ZynqMP Boot Image" },
6915dcf3 186 { IH_TYPE_ZYNQMPBIF, "zynqmpbif", "Xilinx ZynqMP Boot Image (bif)" },
ed0cea7c 187 { IH_TYPE_FPGA, "fpga", "FPGA Image" },
7e719ee7 188 { IH_TYPE_TEE, "tee", "Trusted Execution Environment Image",},
d21bd69b 189 { IH_TYPE_FIRMWARE_IVT, "firmware_ivt", "Firmware with HABv4 IVT" },
6442c964 190 { IH_TYPE_PMMC, "pmmc", "TI Power Management Micro-Controller Firmware",},
81260e33 191 { IH_TYPE_STM32IMAGE, "stm32image", "STMicroelectronics STM32 Image" },
3b975a14 192 { IH_TYPE_MTKIMAGE, "mtk_image", "MediaTek BootROM loadable Image" },
e7fabe75 193 { IH_TYPE_COPRO, "copro", "Coprocessor Image"},
6d295099 194 { IH_TYPE_SUNXI_EGON, "sunxi_egon", "Allwinner eGON Boot Image" },
570abb0a
MB
195 { -1, "", "", },
196};
197
7edb186f 198static const table_entry_t uimage_comp[] = {
570abb0a
MB
199 { IH_COMP_NONE, "none", "uncompressed", },
200 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
201 { IH_COMP_GZIP, "gzip", "gzip compressed", },
fc9c1727 202 { IH_COMP_LZMA, "lzma", "lzma compressed", },
20dde48b 203 { IH_COMP_LZO, "lzo", "lzo compressed", },
027b728d 204 { IH_COMP_LZ4, "lz4", "lz4 compressed", },
26073f9e 205 { IH_COMP_ZSTD, "zstd", "zstd compressed", },
570abb0a
MB
206 { -1, "", "", },
207};
208
56d7ab74
SG
209struct table_info {
210 const char *desc;
211 int count;
212 const table_entry_t *table;
213};
214
155d6a35
AP
215static const struct comp_magic_map image_comp[] = {
216 { IH_COMP_BZIP2, "bzip2", {0x42, 0x5a},},
217 { IH_COMP_GZIP, "gzip", {0x1f, 0x8b},},
218 { IH_COMP_LZMA, "lzma", {0x5d, 0x00},},
219 { IH_COMP_LZO, "lzo", {0x89, 0x4c},},
2ddb8fcd
AL
220 { IH_COMP_LZ4, "lz4", {0x04, 0x22},},
221 { IH_COMP_ZSTD, "zstd", {0x28, 0xb5},},
155d6a35
AP
222 { IH_COMP_NONE, "none", {}, },
223};
224
56d7ab74
SG
225static const struct table_info table_info[IH_COUNT] = {
226 { "architecture", IH_ARCH_COUNT, uimage_arch },
227 { "compression", IH_COMP_COUNT, uimage_comp },
228 { "operating system", IH_OS_COUNT, uimage_os },
229 { "image type", IH_TYPE_COUNT, uimage_type },
230};
231
9a4daad0
MB
232/*****************************************************************************/
233/* Legacy format routines */
234/*****************************************************************************/
712fbcf3 235int image_check_hcrc(const image_header_t *hdr)
b97a2a0a
MB
236{
237 ulong hcrc;
712fbcf3 238 ulong len = image_get_header_size();
b97a2a0a
MB
239 image_header_t header;
240
241 /* Copy header so we can blank CRC field for re-calculation */
712fbcf3
SW
242 memmove(&header, (char *)hdr, image_get_header_size());
243 image_set_hcrc(&header, 0);
b97a2a0a 244
712fbcf3 245 hcrc = crc32(0, (unsigned char *)&header, len);
b97a2a0a 246
712fbcf3 247 return (hcrc == image_get_hcrc(hdr));
b97a2a0a
MB
248}
249
712fbcf3 250int image_check_dcrc(const image_header_t *hdr)
b97a2a0a 251{
712fbcf3
SW
252 ulong data = image_get_data(hdr);
253 ulong len = image_get_data_size(hdr);
254 ulong dcrc = crc32_wd(0, (unsigned char *)data, len, CHUNKSZ_CRC32);
b97a2a0a 255
712fbcf3 256 return (dcrc == image_get_dcrc(hdr));
b97a2a0a
MB
257}
258
f13e7b2e
MB
259/**
260 * image_multi_count - get component (sub-image) count
261 * @hdr: pointer to the header of the multi component image
262 *
263 * image_multi_count() returns number of components in a multi
264 * component image.
265 *
266 * Note: no checking of the image type is done, caller must pass
267 * a valid multi component image.
268 *
269 * returns:
270 * number of components
271 */
712fbcf3 272ulong image_multi_count(const image_header_t *hdr)
f13e7b2e
MB
273{
274 ulong i, count = 0;
df6f1b89 275 uint32_t *size;
f13e7b2e
MB
276
277 /* get start of the image payload, which in case of multi
278 * component images that points to a table of component sizes */
712fbcf3 279 size = (uint32_t *)image_get_data(hdr);
f13e7b2e
MB
280
281 /* count non empty slots */
282 for (i = 0; size[i]; ++i)
283 count++;
284
285 return count;
286}
287
288/**
289 * image_multi_getimg - get component data address and size
290 * @hdr: pointer to the header of the multi component image
291 * @idx: index of the requested component
292 * @data: pointer to a ulong variable, will hold component data address
293 * @len: pointer to a ulong variable, will hold component size
294 *
295 * image_multi_getimg() returns size and data address for the requested
296 * component in a multi component image.
297 *
298 * Note: no checking of the image type is done, caller must pass
299 * a valid multi component image.
300 *
301 * returns:
302 * data address and size of the component, if idx is valid
303 * 0 in data and len, if idx is out of range
304 */
712fbcf3 305void image_multi_getimg(const image_header_t *hdr, ulong idx,
f13e7b2e
MB
306 ulong *data, ulong *len)
307{
308 int i;
df6f1b89 309 uint32_t *size;
02b9b224 310 ulong offset, count, img_data;
f13e7b2e
MB
311
312 /* get number of component */
712fbcf3 313 count = image_multi_count(hdr);
f13e7b2e
MB
314
315 /* get start of the image payload, which in case of multi
316 * component images that points to a table of component sizes */
712fbcf3 317 size = (uint32_t *)image_get_data(hdr);
f13e7b2e
MB
318
319 /* get address of the proper component data start, which means
320 * skipping sizes table (add 1 for last, null entry) */
712fbcf3 321 img_data = image_get_data(hdr) + (count + 1) * sizeof(uint32_t);
f13e7b2e
MB
322
323 if (idx < count) {
712fbcf3 324 *len = uimage_to_cpu(size[idx]);
f13e7b2e 325 offset = 0;
f13e7b2e
MB
326
327 /* go over all indices preceding requested component idx */
328 for (i = 0; i < idx; i++) {
02b9b224 329 /* add up i-th component size, rounding up to 4 bytes */
712fbcf3 330 offset += (uimage_to_cpu(size[i]) + 3) & ~3 ;
f13e7b2e
MB
331 }
332
333 /* calculate idx-th component data address */
02b9b224 334 *data = img_data + offset;
f13e7b2e
MB
335 } else {
336 *len = 0;
337 *data = 0;
338 }
339}
42b73e8e 340
712fbcf3 341static void image_print_type(const image_header_t *hdr)
9a4daad0 342{
80402f34 343 const char __maybe_unused *os, *arch, *type, *comp;
9a4daad0 344
712fbcf3
SW
345 os = genimg_get_os_name(image_get_os(hdr));
346 arch = genimg_get_arch_name(image_get_arch(hdr));
347 type = genimg_get_type_name(image_get_type(hdr));
348 comp = genimg_get_comp_name(image_get_comp(hdr));
9a4daad0 349
712fbcf3 350 printf("%s %s %s (%s)\n", arch, os, type, comp);
9a4daad0
MB
351}
352
5dfb5213 353/**
edbed247 354 * image_print_contents - prints out the contents of the legacy format image
3a2003f6 355 * @ptr: pointer to the legacy format image header
5dfb5213
MB
356 * @p: pointer to prefix string
357 *
edbed247 358 * image_print_contents() formats a multi line legacy image contents description.
5dfb5213
MB
359 * The routine prints out all header fields followed by the size/offset data
360 * for MULTI/SCRIPT images.
361 *
362 * returns:
363 * no returned results
364 */
712fbcf3 365void image_print_contents(const void *ptr)
9a4daad0 366{
3a2003f6 367 const image_header_t *hdr = (const image_header_t *)ptr;
80402f34 368 const char __maybe_unused *p;
edbed247 369
1fe7d938 370 p = IMAGE_INDENT_STRING;
712fbcf3 371 printf("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name(hdr));
859e92b7
SG
372 if (IMAGE_ENABLE_TIMESTAMP) {
373 printf("%sCreated: ", p);
374 genimg_print_time((time_t)image_get_time(hdr));
375 }
712fbcf3
SW
376 printf("%sImage Type: ", p);
377 image_print_type(hdr);
378 printf("%sData Size: ", p);
379 genimg_print_size(image_get_data_size(hdr));
380 printf("%sLoad Address: %08x\n", p, image_get_load(hdr));
381 printf("%sEntry Point: %08x\n", p, image_get_ep(hdr));
382
383 if (image_check_type(hdr, IH_TYPE_MULTI) ||
384 image_check_type(hdr, IH_TYPE_SCRIPT)) {
9a4daad0
MB
385 int i;
386 ulong data, len;
712fbcf3 387 ulong count = image_multi_count(hdr);
9a4daad0 388
712fbcf3 389 printf("%sContents:\n", p);
9a4daad0 390 for (i = 0; i < count; i++) {
712fbcf3 391 image_multi_getimg(hdr, i, &data, &len);
570abb0a 392
712fbcf3
SW
393 printf("%s Image %d: ", p, i);
394 genimg_print_size(len);
570abb0a 395
712fbcf3 396 if (image_check_type(hdr, IH_TYPE_SCRIPT) && i > 0) {
570abb0a
MB
397 /*
398 * the user may need to know offsets
399 * if planning to do something with
400 * multiple files
401 */
712fbcf3 402 printf("%s Offset = 0x%08lx\n", p, data);
570abb0a 403 }
9a4daad0 404 }
d21bd69b
SE
405 } else if (image_check_type(hdr, IH_TYPE_FIRMWARE_IVT)) {
406 printf("HAB Blocks: 0x%08x 0x0000 0x%08x\n",
5b20d141
BML
407 image_get_load(hdr) - image_get_header_size(),
408 (int)(image_get_size(hdr) + image_get_header_size()
409 + sizeof(flash_header_v2_t) - 0x2060));
9a4daad0
MB
410 }
411}
412
2090854c
JW
413/**
414 * print_decomp_msg() - Print a suitable decompression/loading message
415 *
416 * @type: OS type (IH_OS_...)
417 * @comp_type: Compression type being used (IH_COMP_...)
418 * @is_xip: true if the load address matches the image start
419 */
420static void print_decomp_msg(int comp_type, int type, bool is_xip)
421{
422 const char *name = genimg_get_type_name(type);
423
424 if (comp_type == IH_COMP_NONE)
425 printf(" %s %s\n", is_xip ? "XIP" : "Loading", name);
426 else
427 printf(" Uncompressing %s\n", name);
428}
429
155d6a35
AP
430int image_decomp_type(const unsigned char *buf, ulong len)
431{
432 const struct comp_magic_map *cmagic = image_comp;
433
434 if (len < 2)
435 return -EINVAL;
436
437 for (; cmagic->comp_id > 0; cmagic++) {
438 if (!memcmp(buf, cmagic->magic, 2))
439 break;
440 }
441
442 return cmagic->comp_id;
443}
444
2090854c
JW
445int image_decomp(int comp, ulong load, ulong image_start, int type,
446 void *load_buf, void *image_buf, ulong image_len,
447 uint unc_len, ulong *load_end)
448{
449 int ret = 0;
450
451 *load_end = load;
452 print_decomp_msg(comp, type, load == image_start);
453
454 /*
455 * Load the image to the right place, decompressing if needed. After
456 * this, image_len will be set to the number of uncompressed bytes
457 * loaded, ret will be non-zero on error.
458 */
459 switch (comp) {
460 case IH_COMP_NONE:
461 if (load == image_start)
462 break;
463 if (image_len <= unc_len)
464 memmove_wd(load_buf, image_buf, image_len, CHUNKSZ);
465 else
466 ret = -ENOSPC;
467 break;
3bbe1a7f
FS
468#ifndef USE_HOSTCC
469#if CONFIG_IS_ENABLED(GZIP)
2090854c
JW
470 case IH_COMP_GZIP: {
471 ret = gunzip(load_buf, unc_len, image_buf, &image_len);
472 break;
473 }
474#endif /* CONFIG_GZIP */
3bbe1a7f
FS
475#endif
476#ifndef USE_HOSTCC
477#if CONFIG_IS_ENABLED(BZIP2)
2090854c
JW
478 case IH_COMP_BZIP2: {
479 uint size = unc_len;
480
481 /*
482 * If we've got less than 4 MB of malloc() space,
483 * use slower decompression algorithm which requires
484 * at most 2300 KB of memory.
485 */
486 ret = BZ2_bzBuffToBuffDecompress(load_buf, &size,
487 image_buf, image_len,
488 CONFIG_SYS_MALLOC_LEN < (4096 * 1024), 0);
489 image_len = size;
490 break;
491 }
492#endif /* CONFIG_BZIP2 */
3bbe1a7f
FS
493#endif
494#ifndef USE_HOSTCC
495#if CONFIG_IS_ENABLED(LZMA)
2090854c
JW
496 case IH_COMP_LZMA: {
497 SizeT lzma_len = unc_len;
498
499 ret = lzmaBuffToBuffDecompress(load_buf, &lzma_len,
500 image_buf, image_len);
501 image_len = lzma_len;
502 break;
503 }
504#endif /* CONFIG_LZMA */
3bbe1a7f
FS
505#endif
506#ifndef USE_HOSTCC
507#if CONFIG_IS_ENABLED(LZO)
2090854c
JW
508 case IH_COMP_LZO: {
509 size_t size = unc_len;
510
511 ret = lzop_decompress(image_buf, image_len, load_buf, &size);
512 image_len = size;
513 break;
514 }
515#endif /* CONFIG_LZO */
3bbe1a7f
FS
516#endif
517#ifndef USE_HOSTCC
518#if CONFIG_IS_ENABLED(LZ4)
2090854c
JW
519 case IH_COMP_LZ4: {
520 size_t size = unc_len;
521
522 ret = ulz4fn(image_buf, image_len, load_buf, &size);
523 image_len = size;
524 break;
525 }
526#endif /* CONFIG_LZ4 */
3bbe1a7f
FS
527#endif
528#ifndef USE_HOSTCC
529#if CONFIG_IS_ENABLED(ZSTD)
26073f9e 530 case IH_COMP_ZSTD: {
94d0a2ef
SG
531 struct abuf in, out;
532
533 abuf_init_set(&in, image_buf, image_len);
534 abuf_init_set(&in, load_buf, unc_len);
535 ret = zstd_decompress(&in, &out);
536 if (ret < 0) {
537 printf("ZSTD decompression failed\n");
538 return ret;
26073f9e
RM
539 }
540
94d0a2ef 541 image_len = ret;
26073f9e
RM
542
543 break;
544 }
545#endif /* CONFIG_ZSTD */
3bbe1a7f 546#endif
2090854c
JW
547 default:
548 printf("Unimplemented compression type %d\n", comp);
549 return -ENOSYS;
550 }
551
552 *load_end = load + image_len;
553
554 return ret;
555}
556
570abb0a
MB
557
558#ifndef USE_HOSTCC
c76c93a3 559#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
9a4daad0
MB
560/**
561 * image_get_ramdisk - get and verify ramdisk image
9a4daad0
MB
562 * @rd_addr: ramdisk image start address
563 * @arch: expected ramdisk architecture
564 * @verify: checksum verification flag
565 *
566 * image_get_ramdisk() returns a pointer to the verified ramdisk image
567 * header. Routine receives image start address and expected architecture
568 * flag. Verification done covers data and header integrity and os/type/arch
569 * fields checking.
570 *
9a4daad0
MB
571 * returns:
572 * pointer to a ramdisk image header, if image was found and valid
573 * otherwise, return NULL
574 */
712fbcf3 575static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
d985c849 576 int verify)
9a4daad0 577{
3a2003f6 578 const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
9a4daad0 579
712fbcf3
SW
580 if (!image_check_magic(rd_hdr)) {
581 puts("Bad Magic Number\n");
770605e4 582 bootstage_error(BOOTSTAGE_ID_RD_MAGIC);
9a4daad0
MB
583 return NULL;
584 }
585
712fbcf3
SW
586 if (!image_check_hcrc(rd_hdr)) {
587 puts("Bad Header Checksum\n");
770605e4 588 bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
9a4daad0
MB
589 return NULL;
590 }
591
770605e4 592 bootstage_mark(BOOTSTAGE_ID_RD_MAGIC);
712fbcf3 593 image_print_contents(rd_hdr);
9a4daad0
MB
594
595 if (verify) {
596 puts(" Verifying Checksum ... ");
712fbcf3
SW
597 if (!image_check_dcrc(rd_hdr)) {
598 puts("Bad Data CRC\n");
770605e4 599 bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM);
9a4daad0
MB
600 return NULL;
601 }
602 puts("OK\n");
603 }
604
770605e4 605 bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
9a4daad0 606
712fbcf3
SW
607 if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
608 !image_check_arch(rd_hdr, arch) ||
609 !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
610 printf("No Linux %s Ramdisk Image\n",
9a4daad0 611 genimg_get_arch_name(arch));
770605e4 612 bootstage_error(BOOTSTAGE_ID_RAMDISK);
9a4daad0
MB
613 return NULL;
614 }
615
616 return rd_hdr;
617}
21d29f7f 618#endif
570abb0a 619#endif /* !USE_HOSTCC */
9a4daad0
MB
620
621/*****************************************************************************/
622/* Shared dual-format routines */
623/*****************************************************************************/
570abb0a 624#ifndef USE_HOSTCC
bb872dd9
SG
625ulong image_load_addr = CONFIG_SYS_LOAD_ADDR; /* Default Load Address */
626ulong image_save_addr; /* Default Save Address */
627ulong image_save_size; /* Default Save Size (in bytes) */
1cf0a8b2
JH
628
629static int on_loadaddr(const char *name, const char *value, enum env_op op,
630 int flags)
631{
632 switch (op) {
633 case env_op_create:
634 case env_op_overwrite:
7e5f460e 635 image_load_addr = hextoul(value, NULL);
1cf0a8b2
JH
636 break;
637 default:
638 break;
639 }
640
641 return 0;
642}
643U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
644
723806cc 645ulong env_get_bootm_low(void)
9a4daad0 646{
00caae6d 647 char *s = env_get("bootm_low");
9a4daad0 648 if (s) {
7e5f460e 649 ulong tmp = hextoul(s, NULL);
9a4daad0
MB
650 return tmp;
651 }
652
6d0f6bcf
JCPV
653#if defined(CONFIG_SYS_SDRAM_BASE)
654 return CONFIG_SYS_SDRAM_BASE;
dd573b6b 655#elif defined(CONFIG_ARM) || defined(CONFIG_MICROBLAZE) || defined(CONFIG_RISCV)
afe45c87 656 return gd->bd->bi_dram[0].start;
9a4daad0
MB
657#else
658 return 0;
659#endif
660}
661
723806cc 662phys_size_t env_get_bootm_size(void)
9a4daad0 663{
0cb389dd
MY
664 phys_size_t tmp, size;
665 phys_addr_t start;
00caae6d 666 char *s = env_get("bootm_size");
9a4daad0 667 if (s) {
712fbcf3 668 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
9a4daad0
MB
669 return tmp;
670 }
0cb389dd 671
4963f63f
SR
672 start = gd->ram_base;
673 size = gd->ram_size;
0cb389dd 674
7f98b4ee
BS
675 if (start + size > gd->ram_top)
676 size = gd->ram_top - start;
677
00caae6d 678 s = env_get("bootm_low");
c519facc 679 if (s)
712fbcf3 680 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
c519facc 681 else
0cb389dd 682 tmp = start;
9a4daad0 683
0cb389dd 684 return size - (tmp - start);
9a4daad0
MB
685}
686
723806cc 687phys_size_t env_get_bootm_mapsize(void)
c3624e6e
GL
688{
689 phys_size_t tmp;
00caae6d 690 char *s = env_get("bootm_mapsize");
c3624e6e 691 if (s) {
712fbcf3 692 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
c3624e6e
GL
693 return tmp;
694 }
695
696#if defined(CONFIG_SYS_BOOTMAPSZ)
697 return CONFIG_SYS_BOOTMAPSZ;
698#else
723806cc 699 return env_get_bootm_size();
c3624e6e
GL
700#endif
701}
702
712fbcf3 703void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
9a4daad0 704{
54fa2c5b
LJ
705 if (to == from)
706 return;
707
9a4daad0 708#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
22cfddc2
SZ
709 if (to > from) {
710 from += len;
711 to += len;
712 }
9a4daad0
MB
713 while (len > 0) {
714 size_t tail = (len > chunksz) ? chunksz : len;
712fbcf3 715 WATCHDOG_RESET();
22cfddc2
SZ
716 if (to > from) {
717 to -= tail;
718 from -= tail;
719 }
712fbcf3 720 memmove(to, from, tail);
22cfddc2
SZ
721 if (to < from) {
722 to += tail;
723 from += tail;
724 }
9a4daad0
MB
725 len -= tail;
726 }
727#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
712fbcf3 728 memmove(to, from, len);
9a4daad0
MB
729#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
730}
2090854c
JW
731#else /* USE_HOSTCC */
732void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
733{
734 memmove(to, from, len);
735}
570abb0a 736#endif /* !USE_HOSTCC */
9a4daad0 737
712fbcf3 738void genimg_print_size(uint32_t size)
42b73e8e 739{
570abb0a 740#ifndef USE_HOSTCC
712fbcf3
SW
741 printf("%d Bytes = ", size);
742 print_size(size, "\n");
570abb0a 743#else
cec85d4e 744 printf("%d Bytes = %.2f KiB = %.2f MiB\n",
570abb0a
MB
745 size, (double)size / 1.024e3,
746 (double)size / 1.048576e6);
42b73e8e 747#endif
570abb0a
MB
748}
749
859e92b7
SG
750#if IMAGE_ENABLE_TIMESTAMP
751void genimg_print_time(time_t timestamp)
570abb0a
MB
752{
753#ifndef USE_HOSTCC
754 struct rtc_time tm;
755
9f9276c3 756 rtc_to_tm(timestamp, &tm);
712fbcf3 757 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
570abb0a
MB
758 tm.tm_year, tm.tm_mon, tm.tm_mday,
759 tm.tm_hour, tm.tm_min, tm.tm_sec);
760#else
712fbcf3 761 printf("%s", ctime(&timestamp));
42b73e8e 762#endif
570abb0a 763}
859e92b7 764#endif
42b73e8e 765
5b9d44df
SG
766const table_entry_t *get_table_entry(const table_entry_t *table, int id)
767{
768 for (; table->id >= 0; ++table) {
769 if (table->id == id)
770 return table;
771 }
772 return NULL;
773}
774
1426220b
SG
775static const char *unknown_msg(enum ih_category category)
776{
ae3de0d8 777 static const char unknown_str[] = "Unknown ";
1426220b
SG
778 static char msg[30];
779
ae3de0d8
SG
780 strcpy(msg, unknown_str);
781 strncat(msg, table_info[category].desc,
782 sizeof(msg) - sizeof(unknown_str));
1426220b
SG
783
784 return msg;
785}
786
787/**
898a0849 788 * genimg_get_cat_name - translate entry id to long name
1426220b
SG
789 * @category: category to look up (enum ih_category)
790 * @id: entry id to be translated
791 *
792 * This will scan the translation table trying to find the entry that matches
793 * the given id.
794 *
898a0849 795 * @return long entry name if translation succeeds; error string on failure
1426220b
SG
796 */
797const char *genimg_get_cat_name(enum ih_category category, uint id)
798{
799 const table_entry_t *entry;
800
801 entry = get_table_entry(table_info[category].table, id);
802 if (!entry)
803 return unknown_msg(category);
804#if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
805 return entry->lname;
806#else
807 return entry->lname + gd->reloc_off;
808#endif
809}
810
811/**
898a0849 812 * genimg_get_cat_short_name - translate entry id to short name
1426220b
SG
813 * @category: category to look up (enum ih_category)
814 * @id: entry id to be translated
815 *
816 * This will scan the translation table trying to find the entry that matches
817 * the given id.
818 *
898a0849 819 * @return short entry name if translation succeeds; error string on failure
1426220b
SG
820 */
821const char *genimg_get_cat_short_name(enum ih_category category, uint id)
822{
823 const table_entry_t *entry;
824
825 entry = get_table_entry(table_info[category].table, id);
826 if (!entry)
827 return unknown_msg(category);
828#if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
829 return entry->sname;
830#else
831 return entry->sname + gd->reloc_off;
832#endif
833}
834
835int genimg_get_cat_count(enum ih_category category)
836{
837 return table_info[category].count;
838}
839
840const char *genimg_get_cat_desc(enum ih_category category)
841{
842 return table_info[category].desc;
843}
844
02d41b01
NH
845/**
846 * genimg_cat_has_id - check whether category has entry id
847 * @category: category to look up (enum ih_category)
848 * @id: entry id to be checked
849 *
850 * This will scan the translation table trying to find the entry that matches
851 * the given id.
852 *
853 * @return true if category has entry id; false if not
854 */
855bool genimg_cat_has_id(enum ih_category category, uint id)
856{
857 if (get_table_entry(table_info[category].table, id))
858 return true;
859
860 return false;
861}
862
570abb0a
MB
863/**
864 * get_table_entry_name - translate entry id to long name
865 * @table: pointer to a translation table for entries of a specific type
866 * @msg: message to be returned when translation fails
867 * @id: entry id to be translated
868 *
869 * get_table_entry_name() will go over translation table trying to find
870 * entry that matches given id. If matching entry is found, its long
871 * name is returned to the caller.
872 *
873 * returns:
874 * long entry name if translation succeeds
875 * msg otherwise
876 */
7edb186f 877char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
570abb0a 878{
5b9d44df
SG
879 table = get_table_entry(table, id);
880 if (!table)
881 return msg;
2e5167cc 882#if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
5b9d44df 883 return table->lname;
e3d1ac7b 884#else
5b9d44df 885 return table->lname + gd->reloc_off;
e3d1ac7b 886#endif
570abb0a 887}
42b73e8e 888
712fbcf3 889const char *genimg_get_os_name(uint8_t os)
570abb0a 890{
712fbcf3 891 return (get_table_entry_name(uimage_os, "Unknown OS", os));
42b73e8e
MB
892}
893
712fbcf3 894const char *genimg_get_arch_name(uint8_t arch)
42b73e8e 895{
712fbcf3
SW
896 return (get_table_entry_name(uimage_arch, "Unknown Architecture",
897 arch));
570abb0a 898}
42b73e8e 899
712fbcf3 900const char *genimg_get_type_name(uint8_t type)
570abb0a 901{
712fbcf3 902 return (get_table_entry_name(uimage_type, "Unknown Image", type));
570abb0a 903}
42b73e8e 904
898a0849
NH
905const char *genimg_get_comp_name(uint8_t comp)
906{
907 return (get_table_entry_name(uimage_comp, "Unknown Compression",
908 comp));
909}
910
cef2e514 911static const char *genimg_get_short_name(const table_entry_t *table, int val)
5b9d44df 912{
cef2e514 913 table = get_table_entry(table, val);
5b9d44df
SG
914 if (!table)
915 return "unknown";
916#if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
917 return table->sname;
918#else
919 return table->sname + gd->reloc_off;
920#endif
921}
922
cef2e514
SG
923const char *genimg_get_type_short_name(uint8_t type)
924{
925 return genimg_get_short_name(uimage_type, type);
926}
927
cef2e514
SG
928const char *genimg_get_comp_short_name(uint8_t comp)
929{
930 return genimg_get_short_name(uimage_comp, comp);
931}
932
933const char *genimg_get_os_short_name(uint8_t os)
934{
935 return genimg_get_short_name(uimage_os, os);
936}
937
938const char *genimg_get_arch_short_name(uint8_t arch)
939{
940 return genimg_get_short_name(uimage_arch, arch);
941}
942
570abb0a
MB
943/**
944 * get_table_entry_id - translate short entry name to id
945 * @table: pointer to a translation table for entries of a specific type
946 * @table_name: to be used in case of error
947 * @name: entry short name to be translated
948 *
949 * get_table_entry_id() will go over translation table trying to find
950 * entry that matches given short name. If matching entry is found,
951 * its id returned to the caller.
952 *
953 * returns:
954 * entry id if translation succeeds
955 * -1 otherwise
956 */
7edb186f 957int get_table_entry_id(const table_entry_t *table,
570abb0a 958 const char *table_name, const char *name)
42b73e8e 959{
7edb186f 960 const table_entry_t *t;
42b73e8e 961
570abb0a 962 for (t = table; t->id >= 0; ++t) {
2e5167cc 963#ifdef CONFIG_NEEDS_MANUAL_RELOC
5b9d44df 964 if (t->sname && strcasecmp(t->sname + gd->reloc_off, name) == 0)
2e5167cc 965#else
5b9d44df 966 if (t->sname && strcasecmp(t->sname, name) == 0)
521af04d 967#endif
570abb0a
MB
968 return (t->id);
969 }
712fbcf3 970 debug("Invalid %s Type: %s\n", table_name, name);
5b9d44df
SG
971
972 return -1;
570abb0a
MB
973}
974
712fbcf3 975int genimg_get_os_id(const char *name)
570abb0a 976{
712fbcf3 977 return (get_table_entry_id(uimage_os, "OS", name));
570abb0a
MB
978}
979
712fbcf3 980int genimg_get_arch_id(const char *name)
570abb0a 981{
712fbcf3 982 return (get_table_entry_id(uimage_arch, "CPU", name));
42b73e8e 983}
5ad03eb3 984
712fbcf3 985int genimg_get_type_id(const char *name)
570abb0a 986{
712fbcf3 987 return (get_table_entry_id(uimage_type, "Image", name));
570abb0a
MB
988}
989
712fbcf3 990int genimg_get_comp_id(const char *name)
570abb0a 991{
712fbcf3 992 return (get_table_entry_id(uimage_comp, "Compression", name));
570abb0a
MB
993}
994
995#ifndef USE_HOSTCC
0f64140b 996/**
6c454fed
BW
997 * genimg_get_kernel_addr_fit - get the real kernel address and return 2
998 * FIT strings
0f64140b 999 * @img_addr: a string might contain real image address
6c454fed
BW
1000 * @fit_uname_config: double pointer to a char, will hold pointer to a
1001 * configuration unit name
1002 * @fit_uname_kernel: double pointer to a char, will hold pointer to a subimage
1003 * name
0f64140b 1004 *
6c454fed 1005 * genimg_get_kernel_addr_fit get the real kernel start address from a string
0f64140b
BW
1006 * which is normally the first argv of bootm/bootz
1007 *
1008 * returns:
1009 * kernel start address
1010 */
6c454fed
BW
1011ulong genimg_get_kernel_addr_fit(char * const img_addr,
1012 const char **fit_uname_config,
1013 const char **fit_uname_kernel)
0f64140b 1014{
0f64140b
BW
1015 ulong kernel_addr;
1016
1017 /* find out kernel image address */
1018 if (!img_addr) {
bb872dd9 1019 kernel_addr = image_load_addr;
0f64140b 1020 debug("* kernel: default image load address = 0x%08lx\n",
bb872dd9 1021 image_load_addr);
73223f0e 1022#if CONFIG_IS_ENABLED(FIT)
bb872dd9 1023 } else if (fit_parse_conf(img_addr, image_load_addr, &kernel_addr,
6c454fed 1024 fit_uname_config)) {
0f64140b 1025 debug("* kernel: config '%s' from image at 0x%08lx\n",
6c454fed 1026 *fit_uname_config, kernel_addr);
bb872dd9 1027 } else if (fit_parse_subimage(img_addr, image_load_addr, &kernel_addr,
6c454fed 1028 fit_uname_kernel)) {
0f64140b 1029 debug("* kernel: subimage '%s' from image at 0x%08lx\n",
6c454fed 1030 *fit_uname_kernel, kernel_addr);
0f64140b
BW
1031#endif
1032 } else {
7e5f460e 1033 kernel_addr = hextoul(img_addr, NULL);
0f64140b
BW
1034 debug("* kernel: cmdline image address = 0x%08lx\n",
1035 kernel_addr);
1036 }
1037
1038 return kernel_addr;
1039}
1040
6c454fed
BW
1041/**
1042 * genimg_get_kernel_addr() is the simple version of
1043 * genimg_get_kernel_addr_fit(). It ignores those return FIT strings
1044 */
1045ulong genimg_get_kernel_addr(char * const img_addr)
1046{
1047 const char *fit_uname_config = NULL;
1048 const char *fit_uname_kernel = NULL;
1049
1050 return genimg_get_kernel_addr_fit(img_addr, &fit_uname_config,
1051 &fit_uname_kernel);
1052}
1053
fff888a1 1054/**
9a4daad0 1055 * genimg_get_format - get image format type
fff888a1
MB
1056 * @img_addr: image start address
1057 *
9a4daad0 1058 * genimg_get_format() checks whether provided address points to a valid
fff888a1
MB
1059 * legacy or FIT image.
1060 *
4efbe9db
MB
1061 * New uImage format and FDT blob are based on a libfdt. FDT blob
1062 * may be passed directly or embedded in a FIT image. In both situations
9a4daad0 1063 * genimg_get_format() must be able to dectect libfdt header.
4efbe9db 1064 *
fff888a1
MB
1065 * returns:
1066 * image format type or IMAGE_FORMAT_INVALID if no image is present
1067 */
35e7b0f1 1068int genimg_get_format(const void *img_addr)
fff888a1 1069{
c76c93a3 1070#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
3a2003f6 1071 const image_header_t *hdr;
fff888a1 1072
3a2003f6 1073 hdr = (const image_header_t *)img_addr;
fff888a1 1074 if (image_check_magic(hdr))
21d29f7f
HS
1075 return IMAGE_FORMAT_LEGACY;
1076#endif
aa34fbc0 1077#if IMAGE_ENABLE_FIT || IMAGE_ENABLE_OF_LIBFDT
21d29f7f
HS
1078 if (fdt_check_header(img_addr) == 0)
1079 return IMAGE_FORMAT_FIT;
9ace3fc8
SS
1080#endif
1081#ifdef CONFIG_ANDROID_BOOT_IMAGE
21d29f7f
HS
1082 if (android_image_check_header(img_addr) == 0)
1083 return IMAGE_FORMAT_ANDROID;
fff888a1
MB
1084#endif
1085
21d29f7f 1086 return IMAGE_FORMAT_INVALID;
fff888a1
MB
1087}
1088
f773bea8
MB
1089/**
1090 * fit_has_config - check if there is a valid FIT configuration
1091 * @images: pointer to the bootm command headers structure
1092 *
1093 * fit_has_config() checks if there is a FIT configuration in use
1094 * (if FTI support is present).
1095 *
1096 * returns:
1097 * 0, no FIT support or no configuration found
1098 * 1, configuration found
1099 */
712fbcf3 1100int genimg_has_config(bootm_headers_t *images)
f773bea8 1101{
73223f0e 1102#if IMAGE_ENABLE_FIT
f773bea8
MB
1103 if (images->fit_uname_cfg)
1104 return 1;
1105#endif
1106 return 0;
1107}
1108
5ad03eb3 1109/**
9a4daad0 1110 * boot_get_ramdisk - main ramdisk handling routine
5ad03eb3
MB
1111 * @argc: command argument count
1112 * @argv: command argument list
8a5ea3e6 1113 * @images: pointer to the bootm images structure
5ad03eb3
MB
1114 * @arch: expected ramdisk architecture
1115 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
1116 * @rd_end: pointer to a ulong variable, will hold ramdisk end
1117 *
9a4daad0 1118 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
5ad03eb3
MB
1119 * Curently supported are the following ramdisk sources:
1120 * - multicomponent kernel/ramdisk image,
1121 * - commandline provided address of decicated ramdisk image.
1122 *
1123 * returns:
d985c849 1124 * 0, if ramdisk image was found and valid, or skiped
5ad03eb3
MB
1125 * rd_start and rd_end are set to ramdisk start/end addresses if
1126 * ramdisk image is found and valid
d985c849 1127 *
ea86b9e6 1128 * 1, if ramdisk image is found but corrupted, or invalid
5ad03eb3 1129 * rd_start and rd_end are set to 0 if no ramdisk exists
5ad03eb3 1130 */
09140113
SG
1131int boot_get_ramdisk(int argc, char *const argv[], bootm_headers_t *images,
1132 uint8_t arch, ulong *rd_start, ulong *rd_end)
5ad03eb3 1133{
d5934ad7 1134 ulong rd_addr, rd_load;
5ad03eb3 1135 ulong rd_data, rd_len;
c76c93a3 1136#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
3a2003f6 1137 const image_header_t *rd_hdr;
21d29f7f 1138#endif
35e7b0f1 1139 void *buf;
57d40ab7 1140#ifdef CONFIG_SUPPORT_RAW_INITRD
017e1f3f 1141 char *end;
57d40ab7 1142#endif
73223f0e 1143#if IMAGE_ENABLE_FIT
f320a4d8 1144 const char *fit_uname_config = images->fit_uname_cfg;
d5934ad7
MB
1145 const char *fit_uname_ramdisk = NULL;
1146 ulong default_addr;
c8779648 1147 int rd_noffset;
d5934ad7 1148#endif
983c72f4 1149 const char *select = NULL;
5ad03eb3 1150
c8779648
MB
1151 *rd_start = 0;
1152 *rd_end = 0;
1153
1fec3c5d
TR
1154#ifdef CONFIG_ANDROID_BOOT_IMAGE
1155 /*
1156 * Look for an Android boot image.
1157 */
1158 buf = map_sysmem(images->os.start, 0);
c139b5ff 1159 if (buf && genimg_get_format(buf) == IMAGE_FORMAT_ANDROID)
5e218862 1160 select = (argc == 0) ? env_get("loadaddr") : argv[0];
1fec3c5d
TR
1161#endif
1162
983c72f4
SG
1163 if (argc >= 2)
1164 select = argv[1];
2dd46328 1165
d5934ad7
MB
1166 /*
1167 * Look for a '-' which indicates to ignore the
1168 * ramdisk argument
1169 */
983c72f4 1170 if (select && strcmp(select, "-") == 0) {
712fbcf3 1171 debug("## Skipping init Ramdisk\n");
d5934ad7 1172 rd_len = rd_data = 0;
983c72f4 1173 } else if (select || genimg_has_config(images)) {
73223f0e 1174#if IMAGE_ENABLE_FIT
983c72f4 1175 if (select) {
f773bea8
MB
1176 /*
1177 * If the init ramdisk comes from the FIT image and
1178 * the FIT image address is omitted in the command
1179 * line argument, try to use os FIT image address or
1180 * default load address.
1181 */
1182 if (images->fit_uname_os)
1183 default_addr = (ulong)images->fit_hdr_os;
1184 else
bb872dd9 1185 default_addr = image_load_addr;
f773bea8 1186
983c72f4
SG
1187 if (fit_parse_conf(select, default_addr,
1188 &rd_addr, &fit_uname_config)) {
712fbcf3
SW
1189 debug("* ramdisk: config '%s' from image at "
1190 "0x%08lx\n",
f773bea8 1191 fit_uname_config, rd_addr);
983c72f4 1192 } else if (fit_parse_subimage(select, default_addr,
f773bea8 1193 &rd_addr, &fit_uname_ramdisk)) {
712fbcf3
SW
1194 debug("* ramdisk: subimage '%s' from image at "
1195 "0x%08lx\n",
f773bea8
MB
1196 fit_uname_ramdisk, rd_addr);
1197 } else
d5934ad7 1198#endif
f773bea8 1199 {
7e5f460e 1200 rd_addr = hextoul(select, NULL);
712fbcf3
SW
1201 debug("* ramdisk: cmdline image address = "
1202 "0x%08lx\n",
f773bea8
MB
1203 rd_addr);
1204 }
73223f0e 1205#if IMAGE_ENABLE_FIT
f773bea8
MB
1206 } else {
1207 /* use FIT configuration provided in first bootm
a51ec63b
SG
1208 * command argument. If the property is not defined,
1209 * quit silently.
f773bea8 1210 */
35e7b0f1 1211 rd_addr = map_to_sysmem(images->fit_hdr_os);
a51ec63b
SG
1212 rd_noffset = fit_get_node_from_config(images,
1213 FIT_RAMDISK_PROP, rd_addr);
bd86ef11 1214 if (rd_noffset == -ENOENT)
41266c9b 1215 return 0;
a51ec63b
SG
1216 else if (rd_noffset < 0)
1217 return 1;
d5934ad7 1218 }
f773bea8 1219#endif
d5934ad7 1220
d5934ad7
MB
1221 /*
1222 * Check if there is an initrd image at the
1223 * address provided in the second bootm argument
1224 * check image type, for FIT images get FIT node.
1225 */
35e7b0f1
SG
1226 buf = map_sysmem(rd_addr, 0);
1227 switch (genimg_get_format(buf)) {
c76c93a3 1228#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
d5934ad7 1229 case IMAGE_FORMAT_LEGACY:
712fbcf3 1230 printf("## Loading init Ramdisk from Legacy "
c8779648 1231 "Image at %08lx ...\n", rd_addr);
5ad03eb3 1232
770605e4 1233 bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
712fbcf3 1234 rd_hdr = image_get_ramdisk(rd_addr, arch,
d985c849 1235 images->verify);
5ad03eb3 1236
c8779648 1237 if (rd_hdr == NULL)
274cea2b 1238 return 1;
274cea2b 1239
712fbcf3
SW
1240 rd_data = image_get_data(rd_hdr);
1241 rd_len = image_get_data_size(rd_hdr);
1242 rd_load = image_get_load(rd_hdr);
d5934ad7 1243 break;
21d29f7f 1244#endif
73223f0e 1245#if IMAGE_ENABLE_FIT
d5934ad7 1246 case IMAGE_FORMAT_FIT:
126cc864 1247 rd_noffset = fit_image_load(images,
a51ec63b 1248 rd_addr, &fit_uname_ramdisk,
f320a4d8 1249 &fit_uname_config, arch,
a51ec63b
SG
1250 IH_TYPE_RAMDISK,
1251 BOOTSTAGE_ID_FIT_RD_START,
fe20a81a
SG
1252 FIT_LOAD_OPTIONAL_NON_ZERO,
1253 &rd_data, &rd_len);
a51ec63b 1254 if (rd_noffset < 0)
c78fce69 1255 return 1;
c8779648 1256
a51ec63b 1257 images->fit_hdr_rd = map_sysmem(rd_addr, 0);
c8779648 1258 images->fit_uname_rd = fit_uname_ramdisk;
3dfe1101 1259 images->fit_noffset_rd = rd_noffset;
c8779648 1260 break;
2dd46328
RH
1261#endif
1262#ifdef CONFIG_ANDROID_BOOT_IMAGE
1263 case IMAGE_FORMAT_ANDROID:
1264 android_image_get_ramdisk((void *)images->os.start,
1265 &rd_data, &rd_len);
1266 break;
d5934ad7
MB
1267#endif
1268 default:
017e1f3f 1269#ifdef CONFIG_SUPPORT_RAW_INITRD
983c72f4
SG
1270 end = NULL;
1271 if (select)
1272 end = strchr(select, ':');
1273 if (end) {
7e5f460e 1274 rd_len = hextoul(++end, NULL);
017e1f3f
MV
1275 rd_data = rd_addr;
1276 } else
1277#endif
1278 {
1279 puts("Wrong Ramdisk Image Format\n");
1280 rd_data = rd_len = rd_load = 0;
1281 return 1;
1282 }
d5934ad7 1283 }
d5934ad7 1284 } else if (images->legacy_hdr_valid &&
712fbcf3
SW
1285 image_check_type(&images->legacy_hdr_os_copy,
1286 IH_TYPE_MULTI)) {
1287
5ad03eb3 1288 /*
d5934ad7
MB
1289 * Now check if we have a legacy mult-component image,
1290 * get second entry data start address and len.
5ad03eb3 1291 */
770605e4 1292 bootstage_mark(BOOTSTAGE_ID_RAMDISK);
712fbcf3 1293 printf("## Loading init Ramdisk from multi component "
c8779648 1294 "Legacy Image at %08lx ...\n",
d5934ad7
MB
1295 (ulong)images->legacy_hdr_os);
1296
712fbcf3 1297 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
2dd46328 1298 } else {
5ad03eb3
MB
1299 /*
1300 * no initrd image
1301 */
770605e4 1302 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
5ad03eb3
MB
1303 rd_len = rd_data = 0;
1304 }
1305
1306 if (!rd_data) {
712fbcf3 1307 debug("## No init Ramdisk\n");
5ad03eb3
MB
1308 } else {
1309 *rd_start = rd_data;
1310 *rd_end = rd_data + rd_len;
1311 }
712fbcf3 1312 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
5ad03eb3 1313 *rd_start, *rd_end);
274cea2b
KG
1314
1315 return 0;
5ad03eb3 1316}
ceaed2b1 1317
fca43cc8 1318#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
ceaed2b1 1319/**
9a4daad0 1320 * boot_ramdisk_high - relocate init ramdisk
e822d7fc 1321 * @lmb: pointer to lmb handle, will be used for memory mgmt
ceaed2b1
MB
1322 * @rd_data: ramdisk data start address
1323 * @rd_len: ramdisk data length
ceaed2b1
MB
1324 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
1325 * start address (after possible relocation)
1326 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
1327 * end address (after possible relocation)
1328 *
1bce2aeb 1329 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
ceaed2b1
MB
1330 * variable and if requested ramdisk data is moved to a specified location.
1331 *
9a4daad0
MB
1332 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1333 * start/end addresses if ramdisk image start and len were provided,
1334 * otherwise set initrd_start and initrd_end set to zeros.
1335 *
ceaed2b1 1336 * returns:
9a4daad0
MB
1337 * 0 - success
1338 * -1 - failure
ceaed2b1 1339 */
712fbcf3 1340int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
e822d7fc 1341 ulong *initrd_start, ulong *initrd_end)
ceaed2b1
MB
1342{
1343 char *s;
1344 ulong initrd_high;
1345 int initrd_copy_to_ram = 1;
1346
00caae6d
SG
1347 s = env_get("initrd_high");
1348 if (s) {
ceaed2b1
MB
1349 /* a value of "no" or a similar string will act like 0,
1350 * turning the "load high" feature off. This is intentional.
1351 */
7e5f460e 1352 initrd_high = hextoul(s, NULL);
ceaed2b1
MB
1353 if (initrd_high == ~0)
1354 initrd_copy_to_ram = 0;
1355 } else {
723806cc 1356 initrd_high = env_get_bootm_mapsize() + env_get_bootm_low();
ceaed2b1
MB
1357 }
1358
95d449ad 1359
712fbcf3 1360 debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
ceaed2b1
MB
1361 initrd_high, initrd_copy_to_ram);
1362
1363 if (rd_data) {
1364 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
712fbcf3 1365 debug(" in-place initrd\n");
ceaed2b1
MB
1366 *initrd_start = rd_data;
1367 *initrd_end = rd_data + rd_len;
e822d7fc 1368 lmb_reserve(lmb, rd_data, rd_len);
ceaed2b1 1369 } else {
e822d7fc 1370 if (initrd_high)
712fbcf3
SW
1371 *initrd_start = (ulong)lmb_alloc_base(lmb,
1372 rd_len, 0x1000, initrd_high);
e822d7fc 1373 else
712fbcf3
SW
1374 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
1375 0x1000);
e822d7fc
KG
1376
1377 if (*initrd_start == 0) {
712fbcf3 1378 puts("ramdisk - allocation error\n");
e822d7fc 1379 goto error;
ceaed2b1 1380 }
770605e4 1381 bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
ceaed2b1
MB
1382
1383 *initrd_end = *initrd_start + rd_len;
712fbcf3 1384 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
ceaed2b1
MB
1385 *initrd_start, *initrd_end);
1386
712fbcf3 1387 memmove_wd((void *)*initrd_start,
ceaed2b1
MB
1388 (void *)rd_data, rd_len, CHUNKSZ);
1389
3b200110
KG
1390#ifdef CONFIG_MP
1391 /*
1392 * Ensure the image is flushed to memory to handle
1393 * AMP boot scenarios in which we might not be
1394 * HW cache coherent
1395 */
1a1e7072
HS
1396 flush_cache((unsigned long)*initrd_start,
1397 ALIGN(rd_len, ARCH_DMA_MINALIGN));
3b200110 1398#endif
712fbcf3 1399 puts("OK\n");
ceaed2b1
MB
1400 }
1401 } else {
1402 *initrd_start = 0;
1403 *initrd_end = 0;
1404 }
712fbcf3 1405 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
ceaed2b1 1406 *initrd_start, *initrd_end);
9a4daad0 1407
e822d7fc 1408 return 0;
b6b0fe64 1409
e822d7fc
KG
1410error:
1411 return -1;
b6b0fe64 1412}
fca43cc8 1413#endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
b6b0fe64 1414
90268b87
SG
1415int boot_get_setup(bootm_headers_t *images, uint8_t arch,
1416 ulong *setup_start, ulong *setup_len)
1417{
73223f0e 1418#if IMAGE_ENABLE_FIT
90268b87
SG
1419 return boot_get_setup_fit(images, arch, setup_start, setup_len);
1420#else
1421 return -ENOENT;
1422#endif
1423}
1424
73223f0e 1425#if IMAGE_ENABLE_FIT
8b93a92f 1426#if defined(CONFIG_FPGA)
09140113 1427int boot_get_fpga(int argc, char *const argv[], bootm_headers_t *images,
62afc601
MS
1428 uint8_t arch, const ulong *ld_start, ulong * const ld_len)
1429{
1430 ulong tmp_img_addr, img_data, img_len;
1431 void *buf;
1432 int conf_noffset;
1433 int fit_img_result;
b02e4044 1434 const char *uname, *name;
62afc601
MS
1435 int err;
1436 int devnum = 0; /* TODO support multi fpga platforms */
62afc601
MS
1437
1438 /* Check to see if the images struct has a FIT configuration */
1439 if (!genimg_has_config(images)) {
1440 debug("## FIT configuration was not specified\n");
1441 return 0;
1442 }
1443
1444 /*
1445 * Obtain the os FIT header from the images struct
62afc601
MS
1446 */
1447 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
62afc601
MS
1448 buf = map_sysmem(tmp_img_addr, 0);
1449 /*
1450 * Check image type. For FIT images get FIT node
1451 * and attempt to locate a generic binary.
1452 */
1453 switch (genimg_get_format(buf)) {
1454 case IMAGE_FORMAT_FIT:
1455 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
1456
b02e4044
SG
1457 uname = fdt_stringlist_get(buf, conf_noffset, FIT_FPGA_PROP, 0,
1458 NULL);
1459 if (!uname) {
62afc601
MS
1460 debug("## FPGA image is not specified\n");
1461 return 0;
1462 }
1463 fit_img_result = fit_image_load(images,
1464 tmp_img_addr,
1465 (const char **)&uname,
1466 &(images->fit_uname_cfg),
1467 arch,
1468 IH_TYPE_FPGA,
1469 BOOTSTAGE_ID_FPGA_INIT,
1470 FIT_LOAD_OPTIONAL_NON_ZERO,
1471 &img_data, &img_len);
1472
1473 debug("FPGA image (%s) loaded to 0x%lx/size 0x%lx\n",
1474 uname, img_data, img_len);
1475
1476 if (fit_img_result < 0) {
1477 /* Something went wrong! */
1478 return fit_img_result;
1479 }
1480
8b93a92f 1481 if (!fpga_is_partial_data(devnum, img_len)) {
62afc601
MS
1482 name = "full";
1483 err = fpga_loadbitstream(devnum, (char *)img_data,
1484 img_len, BIT_FULL);
1485 if (err)
1486 err = fpga_load(devnum, (const void *)img_data,
1487 img_len, BIT_FULL);
1488 } else {
1489 name = "partial";
1490 err = fpga_loadbitstream(devnum, (char *)img_data,
1491 img_len, BIT_PARTIAL);
1492 if (err)
1493 err = fpga_load(devnum, (const void *)img_data,
1494 img_len, BIT_PARTIAL);
1495 }
1496
62afc601 1497 if (err)
611a9428
MS
1498 return err;
1499
1500 printf(" Programming %s bitstream... OK\n", name);
62afc601
MS
1501 break;
1502 default:
1503 printf("The given image format is not supported (corrupt?)\n");
1504 return 1;
1505 }
1506
1507 return 0;
1508}
1509#endif
1510
d7be5092
AD
1511static void fit_loadable_process(uint8_t img_type,
1512 ulong img_data,
1513 ulong img_len)
1514{
1515 int i;
1516 const unsigned int count =
1517 ll_entry_count(struct fit_loadable_tbl, fit_loadable);
1518 struct fit_loadable_tbl *fit_loadable_handler =
1519 ll_entry_start(struct fit_loadable_tbl, fit_loadable);
1520 /* For each loadable handler */
1521 for (i = 0; i < count; i++, fit_loadable_handler++)
1522 /* matching this type */
1523 if (fit_loadable_handler->type == img_type)
1524 /* call that handler with this image data */
1525 fit_loadable_handler->handler(img_data, img_len);
1526}
1527
09140113
SG
1528int boot_get_loadable(int argc, char *const argv[], bootm_headers_t *images,
1529 uint8_t arch, const ulong *ld_start, ulong * const ld_len)
84a07dbf
KA
1530{
1531 /*
1532 * These variables are used to hold the current image location
1533 * in system memory.
1534 */
1535 ulong tmp_img_addr;
1536 /*
1537 * These two variables are requirements for fit_image_load, but
1538 * their values are not used
1539 */
1540 ulong img_data, img_len;
1541 void *buf;
1542 int loadables_index;
1543 int conf_noffset;
1544 int fit_img_result;
b02e4044 1545 const char *uname;
d7be5092 1546 uint8_t img_type;
84a07dbf
KA
1547
1548 /* Check to see if the images struct has a FIT configuration */
1549 if (!genimg_has_config(images)) {
1550 debug("## FIT configuration was not specified\n");
1551 return 0;
1552 }
1553
1554 /*
1555 * Obtain the os FIT header from the images struct
84a07dbf
KA
1556 */
1557 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
84a07dbf
KA
1558 buf = map_sysmem(tmp_img_addr, 0);
1559 /*
1560 * Check image type. For FIT images get FIT node
1561 * and attempt to locate a generic binary.
1562 */
1563 switch (genimg_get_format(buf)) {
1564 case IMAGE_FORMAT_FIT:
1565 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
1566
1567 for (loadables_index = 0;
b02e4044
SG
1568 uname = fdt_stringlist_get(buf, conf_noffset,
1569 FIT_LOADABLE_PROP, loadables_index,
1570 NULL), uname;
84a07dbf
KA
1571 loadables_index++)
1572 {
1573 fit_img_result = fit_image_load(images,
1574 tmp_img_addr,
b02e4044 1575 &uname,
84a07dbf
KA
1576 &(images->fit_uname_cfg), arch,
1577 IH_TYPE_LOADABLE,
1578 BOOTSTAGE_ID_FIT_LOADABLE_START,
1579 FIT_LOAD_OPTIONAL_NON_ZERO,
1580 &img_data, &img_len);
1581 if (fit_img_result < 0) {
1582 /* Something went wrong! */
1583 return fit_img_result;
1584 }
d7be5092
AD
1585
1586 fit_img_result = fit_image_get_node(buf, uname);
1587 if (fit_img_result < 0) {
1588 /* Something went wrong! */
1589 return fit_img_result;
1590 }
1591 fit_img_result = fit_image_get_type(buf,
1592 fit_img_result,
1593 &img_type);
1594 if (fit_img_result < 0) {
1595 /* Something went wrong! */
1596 return fit_img_result;
1597 }
1598
1599 fit_loadable_process(img_type, img_data, img_len);
84a07dbf
KA
1600 }
1601 break;
1602 default:
1603 printf("The given image format is not supported (corrupt?)\n");
1604 return 1;
1605 }
1606
1607 return 0;
1608}
1609#endif
1610
fca43cc8 1611#ifdef CONFIG_SYS_BOOT_GET_CMDLINE
b6b0fe64 1612/**
9a4daad0 1613 * boot_get_cmdline - allocate and initialize kernel cmdline
e822d7fc 1614 * @lmb: pointer to lmb handle, will be used for memory mgmt
b6b0fe64
MB
1615 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1616 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1617 *
9a4daad0 1618 * boot_get_cmdline() allocates space for kernel command line below
919d25c9 1619 * BOOTMAPSZ + env_get_bootm_low() address. If "bootargs" U-Boot environment
b6b0fe64
MB
1620 * variable is present its contents is copied to allocated kernel
1621 * command line.
1622 *
1623 * returns:
e822d7fc
KG
1624 * 0 - success
1625 * -1 - failure
b6b0fe64 1626 */
712fbcf3 1627int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
b6b0fe64
MB
1628{
1629 char *cmdline;
1630 char *s;
1631
6d0f6bcf 1632 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
723806cc 1633 env_get_bootm_mapsize() + env_get_bootm_low());
e822d7fc
KG
1634
1635 if (cmdline == NULL)
1636 return -1;
b6b0fe64 1637
00caae6d
SG
1638 s = env_get("bootargs");
1639 if (!s)
b6b0fe64
MB
1640 s = "";
1641
1642 strcpy(cmdline, s);
1643
1644 *cmd_start = (ulong) & cmdline[0];
1645 *cmd_end = *cmd_start + strlen(cmdline);
1646
712fbcf3 1647 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
b6b0fe64 1648
e822d7fc 1649 return 0;
b6b0fe64 1650}
fca43cc8 1651#endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
b6b0fe64 1652
fca43cc8 1653#ifdef CONFIG_SYS_BOOT_GET_KBD
b6b0fe64 1654/**
9a4daad0 1655 * boot_get_kbd - allocate and initialize kernel copy of board info
e822d7fc 1656 * @lmb: pointer to lmb handle, will be used for memory mgmt
b6b0fe64
MB
1657 * @kbd: double pointer to board info data
1658 *
9a4daad0 1659 * boot_get_kbd() allocates space for kernel copy of board info data below
723806cc 1660 * BOOTMAPSZ + env_get_bootm_low() address and kernel board info is initialized
590d3cac 1661 * with the current u-boot board info data.
b6b0fe64
MB
1662 *
1663 * returns:
e822d7fc
KG
1664 * 0 - success
1665 * -1 - failure
b6b0fe64 1666 */
b75d8dc5 1667int boot_get_kbd(struct lmb *lmb, struct bd_info **kbd)
b6b0fe64 1668{
b75d8dc5
MY
1669 *kbd = (struct bd_info *)(ulong)lmb_alloc_base(lmb,
1670 sizeof(struct bd_info),
1671 0xf,
1672 env_get_bootm_mapsize() + env_get_bootm_low());
e822d7fc
KG
1673 if (*kbd == NULL)
1674 return -1;
1675
b6b0fe64
MB
1676 **kbd = *(gd->bd);
1677
712fbcf3 1678 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
b6b0fe64
MB
1679
1680#if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1681 do_bdinfo(NULL, 0, 0, NULL);
1682#endif
1683
e822d7fc 1684 return 0;
ceaed2b1 1685}
fca43cc8 1686#endif /* CONFIG_SYS_BOOT_GET_KBD */
13d06981
SG
1687
1688#ifdef CONFIG_LMB
1689int image_setup_linux(bootm_headers_t *images)
1690{
1691 ulong of_size = images->ft_len;
1692 char **of_flat_tree = &images->ft_addr;
13d06981 1693 struct lmb *lmb = &images->lmb;
13d06981
SG
1694 int ret;
1695
1696 if (IMAGE_ENABLE_OF_LIBFDT)
1697 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
1698
1699 if (IMAGE_BOOT_GET_CMDLINE) {
1700 ret = boot_get_cmdline(lmb, &images->cmdline_start,
1701 &images->cmdline_end);
1702 if (ret) {
1703 puts("ERROR with allocation of cmdline\n");
1704 return ret;
1705 }
1706 }
13d06981
SG
1707
1708 if (IMAGE_ENABLE_OF_LIBFDT) {
1709 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
1710 if (ret)
1711 return ret;
1712 }
1713
1714 if (IMAGE_ENABLE_OF_LIBFDT && of_size) {
1715 ret = image_setup_libfdt(images, *of_flat_tree, of_size, lmb);
1716 if (ret)
1717 return ret;
1718 }
1719
1720 return 0;
1721}
1722#endif /* CONFIG_LMB */
5dfb5213 1723#endif /* !USE_HOSTCC */
This page took 0.85861 seconds and 4 git commands to generate.