blob: 020449db3fb4ca31fe72573c9c26afe3ed6d077d [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass3b5866d2014-06-12 07:24:46 -06002/*
3 * (C) Copyright 2000-2009
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Simon Glass3b5866d2014-06-12 07:24:46 -06005 */
6
Simon Glass0232ba72014-06-12 07:24:51 -06007#ifndef USE_HOSTCC
Simon Glass3b5866d2014-06-12 07:24:46 -06008#include <common.h>
Simon Glass0232ba72014-06-12 07:24:51 -06009#include <bootstage.h>
Simon Glass63334482019-11-14 12:57:39 -070010#include <cpu_func.h>
Simon Glass313112a2019-08-01 09:46:46 -060011#include <env.h>
Simon Glass0129b522014-10-19 21:11:24 -060012#include <errno.h>
Simon Glass3b5866d2014-06-12 07:24:46 -060013#include <fdt_support.h>
Simon Glass8f3f7612019-11-14 12:57:42 -070014#include <irq_func.h>
Simon Glass3b5866d2014-06-12 07:24:46 -060015#include <lmb.h>
Simon Glass0f2af882020-05-10 11:40:05 -060016#include <log.h>
Simon Glass3b5866d2014-06-12 07:24:46 -060017#include <malloc.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050018#include <mapmem.h>
Simon Glass274e0b02020-05-10 11:39:56 -060019#include <net.h>
20#include <asm/cache.h>
Simon Glass3b5866d2014-06-12 07:24:46 -060021#include <asm/io.h>
Simon Glassc38406e2020-11-05 10:33:43 -070022#include <linux/sizes.h>
Simon Glass3b5866d2014-06-12 07:24:46 -060023#if defined(CONFIG_CMD_USB)
24#include <usb.h>
25#endif
Simon Glass0232ba72014-06-12 07:24:51 -060026#else
27#include "mkimage.h"
28#endif
Simon Glass3b5866d2014-06-12 07:24:46 -060029
Simon Glass0232ba72014-06-12 07:24:51 -060030#include <command.h>
31#include <bootm.h>
32#include <image.h>
Simon Glass3b5866d2014-06-12 07:24:46 -060033
34#ifndef CONFIG_SYS_BOOTM_LEN
35/* use 8MByte as default max gunzip size */
36#define CONFIG_SYS_BOOTM_LEN 0x800000
37#endif
38
Simon Glassc38406e2020-11-05 10:33:43 -070039#define MAX_CMDLINE_SIZE SZ_4K
40
Simon Glass3b5866d2014-06-12 07:24:46 -060041#define IH_INITRD_ARCH IH_ARCH_DEFAULT
42
Simon Glass0232ba72014-06-12 07:24:51 -060043#ifndef USE_HOSTCC
44
45DECLARE_GLOBAL_DATA_PTR;
46
Tom Rini36f067f2016-08-12 08:31:15 -040047bootm_headers_t images; /* pointers to os/initrd/fdt images */
48
Simon Glassed38aef2020-05-10 11:40:03 -060049static const void *boot_get_kernel(struct cmd_tbl *cmdtp, int flag, int argc,
50 char *const argv[], bootm_headers_t *images,
Simon Glass3b5866d2014-06-12 07:24:46 -060051 ulong *os_data, ulong *os_len);
52
Simon Glassf55305a2018-05-16 09:42:25 -060053__weak void board_quiesce_devices(void)
54{
55}
56
Simon Glass3b5866d2014-06-12 07:24:46 -060057#ifdef CONFIG_LMB
58static void boot_start_lmb(bootm_headers_t *images)
59{
60 ulong mem_start;
61 phys_size_t mem_size;
62
Simon Glassda1a1342017-08-03 12:22:15 -060063 mem_start = env_get_bootm_low();
64 mem_size = env_get_bootm_size();
Simon Glass3b5866d2014-06-12 07:24:46 -060065
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +010066 lmb_init_and_reserve_range(&images->lmb, (phys_addr_t)mem_start,
67 mem_size, NULL);
Simon Glass3b5866d2014-06-12 07:24:46 -060068}
69#else
70#define lmb_reserve(lmb, base, size)
71static inline void boot_start_lmb(bootm_headers_t *images) { }
72#endif
73
Simon Glassed38aef2020-05-10 11:40:03 -060074static int bootm_start(struct cmd_tbl *cmdtp, int flag, int argc,
75 char *const argv[])
Simon Glass3b5866d2014-06-12 07:24:46 -060076{
77 memset((void *)&images, 0, sizeof(images));
Simon Glass22c34c22017-08-03 12:22:13 -060078 images.verify = env_get_yesno("verify");
Simon Glass3b5866d2014-06-12 07:24:46 -060079
80 boot_start_lmb(&images);
81
82 bootstage_mark_name(BOOTSTAGE_ID_BOOTM_START, "bootm_start");
83 images.state = BOOTM_STATE_START;
84
85 return 0;
86}
87
Simon Glassed38aef2020-05-10 11:40:03 -060088static int bootm_find_os(struct cmd_tbl *cmdtp, int flag, int argc,
89 char *const argv[])
Simon Glass3b5866d2014-06-12 07:24:46 -060090{
91 const void *os_hdr;
92 bool ep_found = false;
Simon Glass0129b522014-10-19 21:11:24 -060093 int ret;
Simon Glass3b5866d2014-06-12 07:24:46 -060094
95 /* get kernel image header, start address and length */
96 os_hdr = boot_get_kernel(cmdtp, flag, argc, argv,
97 &images, &images.os.image_start, &images.os.image_len);
98 if (images.os.image_len == 0) {
99 puts("ERROR: can't get kernel image!\n");
100 return 1;
101 }
102
103 /* get image parameters */
104 switch (genimg_get_format(os_hdr)) {
Tom Rinic220bd92019-05-23 07:14:07 -0400105#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Simon Glass3b5866d2014-06-12 07:24:46 -0600106 case IMAGE_FORMAT_LEGACY:
107 images.os.type = image_get_type(os_hdr);
108 images.os.comp = image_get_comp(os_hdr);
109 images.os.os = image_get_os(os_hdr);
110
111 images.os.end = image_get_image_end(os_hdr);
112 images.os.load = image_get_load(os_hdr);
Simon Glass0129b522014-10-19 21:11:24 -0600113 images.os.arch = image_get_arch(os_hdr);
Simon Glass3b5866d2014-06-12 07:24:46 -0600114 break;
115#endif
Simon Glasse3ee2fb2016-02-22 22:55:43 -0700116#if IMAGE_ENABLE_FIT
Simon Glass3b5866d2014-06-12 07:24:46 -0600117 case IMAGE_FORMAT_FIT:
118 if (fit_image_get_type(images.fit_hdr_os,
119 images.fit_noffset_os,
120 &images.os.type)) {
121 puts("Can't get image type!\n");
122 bootstage_error(BOOTSTAGE_ID_FIT_TYPE);
123 return 1;
124 }
125
126 if (fit_image_get_comp(images.fit_hdr_os,
127 images.fit_noffset_os,
128 &images.os.comp)) {
129 puts("Can't get image compression!\n");
130 bootstage_error(BOOTSTAGE_ID_FIT_COMPRESSION);
131 return 1;
132 }
133
134 if (fit_image_get_os(images.fit_hdr_os, images.fit_noffset_os,
135 &images.os.os)) {
136 puts("Can't get image OS!\n");
137 bootstage_error(BOOTSTAGE_ID_FIT_OS);
138 return 1;
139 }
140
Simon Glass0129b522014-10-19 21:11:24 -0600141 if (fit_image_get_arch(images.fit_hdr_os,
142 images.fit_noffset_os,
143 &images.os.arch)) {
144 puts("Can't get image ARCH!\n");
145 return 1;
146 }
147
Simon Glass3b5866d2014-06-12 07:24:46 -0600148 images.os.end = fit_get_end(images.fit_hdr_os);
149
150 if (fit_image_get_load(images.fit_hdr_os, images.fit_noffset_os,
151 &images.os.load)) {
152 puts("Can't get image load address!\n");
153 bootstage_error(BOOTSTAGE_ID_FIT_LOADADDR);
154 return 1;
155 }
156 break;
157#endif
158#ifdef CONFIG_ANDROID_BOOT_IMAGE
159 case IMAGE_FORMAT_ANDROID:
160 images.os.type = IH_TYPE_KERNEL;
Eugeniu Rosca1403f392019-04-08 17:35:27 +0200161 images.os.comp = android_image_get_kcomp(os_hdr);
Simon Glass3b5866d2014-06-12 07:24:46 -0600162 images.os.os = IH_OS_LINUX;
Simon Glass3b5866d2014-06-12 07:24:46 -0600163
164 images.os.end = android_image_get_end(os_hdr);
165 images.os.load = android_image_get_kload(os_hdr);
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300166 images.ep = images.os.load;
167 ep_found = true;
Simon Glass3b5866d2014-06-12 07:24:46 -0600168 break;
169#endif
170 default:
171 puts("ERROR: unknown image format type!\n");
172 return 1;
173 }
174
Simon Glass0129b522014-10-19 21:11:24 -0600175 /* If we have a valid setup.bin, we will use that for entry (x86) */
Simon Glass9d428302014-10-10 08:21:57 -0600176 if (images.os.arch == IH_ARCH_I386 ||
177 images.os.arch == IH_ARCH_X86_64) {
Simon Glass0129b522014-10-19 21:11:24 -0600178 ulong len;
179
180 ret = boot_get_setup(&images, IH_ARCH_I386, &images.ep, &len);
181 if (ret < 0 && ret != -ENOENT) {
182 puts("Could not find a valid setup.bin for x86\n");
183 return 1;
184 }
185 /* Kernel entry point is the setup.bin */
186 } else if (images.legacy_hdr_valid) {
Simon Glass3b5866d2014-06-12 07:24:46 -0600187 images.ep = image_get_ep(&images.legacy_hdr_os_copy);
Simon Glasse3ee2fb2016-02-22 22:55:43 -0700188#if IMAGE_ENABLE_FIT
Simon Glass3b5866d2014-06-12 07:24:46 -0600189 } else if (images.fit_uname_os) {
190 int ret;
191
192 ret = fit_image_get_entry(images.fit_hdr_os,
193 images.fit_noffset_os, &images.ep);
194 if (ret) {
195 puts("Can't get entry point property!\n");
196 return 1;
197 }
198#endif
199 } else if (!ep_found) {
200 puts("Could not find kernel entry point!\n");
201 return 1;
202 }
203
204 if (images.os.type == IH_TYPE_KERNEL_NOLOAD) {
Marek Vasut21425792018-06-13 06:13:33 +0200205 if (CONFIG_IS_ENABLED(CMD_BOOTI) &&
206 images.os.arch == IH_ARCH_ARM64) {
207 ulong image_addr;
208 ulong image_size;
209
210 ret = booti_setup(images.os.image_start, &image_addr,
211 &image_size, true);
212 if (ret != 0)
213 return 1;
214
215 images.os.type = IH_TYPE_KERNEL;
216 images.os.load = image_addr;
217 images.ep = image_addr;
218 } else {
219 images.os.load = images.os.image_start;
220 images.ep += images.os.image_start;
221 }
Simon Glass3b5866d2014-06-12 07:24:46 -0600222 }
223
Simon Glass5b539a02016-02-24 09:14:42 -0700224 images.os.start = map_to_sysmem(os_hdr);
Simon Glass3b5866d2014-06-12 07:24:46 -0600225
226 return 0;
227}
228
Karl Apsited686ce92015-05-21 09:52:49 -0400229/**
230 * bootm_find_images - wrapper to find and locate various images
231 * @flag: Ignored Argument
232 * @argc: command argument count
233 * @argv: command argument list
Tero Kristo06086942020-06-12 15:41:20 +0300234 * @start: OS image start address
235 * @size: OS image size
Karl Apsited686ce92015-05-21 09:52:49 -0400236 *
237 * boot_find_images() will attempt to load an available ramdisk,
238 * flattened device tree, as well as specifically marked
239 * "loadable" images (loadables are FIT only)
240 *
241 * Note: bootm_find_images will skip an image if it is not found
242 *
243 * @return:
244 * 0, if all existing images were loaded correctly
245 * 1, if an image is found but corrupted, or invalid
246 */
Tero Kristo06086942020-06-12 15:41:20 +0300247int bootm_find_images(int flag, int argc, char *const argv[], ulong start,
248 ulong size)
Simon Glass3b5866d2014-06-12 07:24:46 -0600249{
250 int ret;
251
252 /* find ramdisk */
253 ret = boot_get_ramdisk(argc, argv, &images, IH_INITRD_ARCH,
254 &images.rd_start, &images.rd_end);
255 if (ret) {
256 puts("Ramdisk image is corrupt or invalid\n");
257 return 1;
258 }
259
Tero Kristo06086942020-06-12 15:41:20 +0300260 /* check if ramdisk overlaps OS image */
261 if (images.rd_start && (((ulong)images.rd_start >= start &&
Jaehoon Chung80ffb812020-10-21 14:17:03 +0900262 (ulong)images.rd_start < start + size) ||
263 ((ulong)images.rd_end > start &&
264 (ulong)images.rd_end <= start + size) ||
265 ((ulong)images.rd_start < start &&
266 (ulong)images.rd_end >= start + size))) {
Tero Kristo06086942020-06-12 15:41:20 +0300267 printf("ERROR: RD image overlaps OS image (OS=0x%lx..0x%lx)\n",
268 start, start + size);
269 return 1;
270 }
271
Simon Glass8b426922016-02-22 22:55:45 -0700272#if IMAGE_ENABLE_OF_LIBFDT
Simon Glass3b5866d2014-06-12 07:24:46 -0600273 /* find flattened device tree */
274 ret = boot_get_fdt(flag, argc, argv, IH_ARCH_DEFAULT, &images,
275 &images.ft_addr, &images.ft_len);
276 if (ret) {
277 puts("Could not find a valid device tree\n");
278 return 1;
279 }
Tero Kristo06086942020-06-12 15:41:20 +0300280
281 /* check if FDT overlaps OS image */
282 if (images.ft_addr &&
283 (((ulong)images.ft_addr >= start &&
284 (ulong)images.ft_addr <= start + size) ||
285 ((ulong)images.ft_addr + images.ft_len >= start &&
286 (ulong)images.ft_addr + images.ft_len <= start + size))) {
287 printf("ERROR: FDT image overlaps OS image (OS=0x%lx..0x%lx)\n",
288 start, start + size);
289 return 1;
290 }
291
Simon Goldschmidt6127ac02018-12-17 20:14:42 +0100292 if (CONFIG_IS_ENABLED(CMD_FDT))
293 set_working_fdt_addr(map_to_sysmem(images.ft_addr));
Simon Glass3b5866d2014-06-12 07:24:46 -0600294#endif
295
Simon Glasse3ee2fb2016-02-22 22:55:43 -0700296#if IMAGE_ENABLE_FIT
Goldschmidt Simon9179c812017-11-10 14:17:41 +0000297#if defined(CONFIG_FPGA)
Michal Simekebae78b2016-05-17 14:03:50 +0200298 /* find bitstreams */
299 ret = boot_get_fpga(argc, argv, &images, IH_ARCH_DEFAULT,
300 NULL, NULL);
301 if (ret) {
302 printf("FPGA image is corrupted or invalid\n");
303 return 1;
304 }
305#endif
306
Karl Apsite1b21c282015-05-21 09:52:48 -0400307 /* find all of the loadables */
308 ret = boot_get_loadable(argc, argv, &images, IH_ARCH_DEFAULT,
309 NULL, NULL);
310 if (ret) {
311 printf("Loadable(s) is corrupt or invalid\n");
312 return 1;
313 }
Karl Apsite1b21c282015-05-21 09:52:48 -0400314#endif
315
Simon Glass3b5866d2014-06-12 07:24:46 -0600316 return 0;
317}
318
Simon Glassed38aef2020-05-10 11:40:03 -0600319static int bootm_find_other(struct cmd_tbl *cmdtp, int flag, int argc,
320 char *const argv[])
Simon Glass3b5866d2014-06-12 07:24:46 -0600321{
322 if (((images.os.type == IH_TYPE_KERNEL) ||
323 (images.os.type == IH_TYPE_KERNEL_NOLOAD) ||
324 (images.os.type == IH_TYPE_MULTI)) &&
325 (images.os.os == IH_OS_LINUX ||
326 images.os.os == IH_OS_VXWORKS))
Tero Kristo06086942020-06-12 15:41:20 +0300327 return bootm_find_images(flag, argc, argv, 0, 0);
Simon Glass3b5866d2014-06-12 07:24:46 -0600328
329 return 0;
330}
Simon Glass10ae4192014-12-02 13:17:30 -0700331#endif /* USE_HOSTC */
332
Julius Werner47ef9862019-07-24 19:37:54 -0700333#if !defined(USE_HOSTCC) || defined(CONFIG_FIT_SIGNATURE)
Simon Glass4f6e6d72014-12-02 13:17:37 -0700334/**
335 * handle_decomp_error() - display a decompression error
336 *
337 * This function tries to produce a useful message. In the case where the
338 * uncompressed size is the same as the available space, we can assume that
339 * the image is too large for the buffer.
340 *
341 * @comp_type: Compression type being used (IH_COMP_...)
342 * @uncomp_size: Number of bytes uncompressed
Julius Werner47ef9862019-07-24 19:37:54 -0700343 * @ret: errno error code received from compression library
344 * @return Appropriate BOOTM_ERR_ error code
Simon Glass4f6e6d72014-12-02 13:17:37 -0700345 */
Julius Werner47ef9862019-07-24 19:37:54 -0700346static int handle_decomp_error(int comp_type, size_t uncomp_size, int ret)
Simon Glass10ae4192014-12-02 13:17:30 -0700347{
Simon Glass4f6e6d72014-12-02 13:17:37 -0700348 const char *name = genimg_get_comp_name(comp_type);
349
Julius Werner47ef9862019-07-24 19:37:54 -0700350 /* ENOSYS means unimplemented compression type, don't reset. */
351 if (ret == -ENOSYS)
352 return BOOTM_ERR_UNIMPLEMENTED;
353
354 if (uncomp_size >= CONFIG_SYS_BOOTM_LEN)
Simon Glass4f6e6d72014-12-02 13:17:37 -0700355 printf("Image too large: increase CONFIG_SYS_BOOTM_LEN\n");
Simon Glass10ae4192014-12-02 13:17:30 -0700356 else
Simon Glass4f6e6d72014-12-02 13:17:37 -0700357 printf("%s: uncompress error %d\n", name, ret);
358
359 /*
360 * The decompression routines are now safe, so will not write beyond
361 * their bounds. Probably it is not necessary to reset, but maintain
362 * the current behaviour for now.
363 */
364 printf("Must RESET board to recover\n");
Simon Glass10ae4192014-12-02 13:17:30 -0700365#ifndef USE_HOSTCC
366 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
367#endif
368
369 return BOOTM_ERR_RESET;
370}
Julius Werner47ef9862019-07-24 19:37:54 -0700371#endif
Simon Glass686f3bb2014-06-12 07:24:52 -0600372
Simon Glassa51991d2014-06-12 07:24:53 -0600373#ifndef USE_HOSTCC
Tom Rini6c96f5d2018-05-01 12:32:37 -0400374static int bootm_load_os(bootm_headers_t *images, int boot_progress)
Simon Glass686f3bb2014-06-12 07:24:52 -0600375{
376 image_info_t os = images->os;
377 ulong load = os.load;
Tom Rini6c96f5d2018-05-01 12:32:37 -0400378 ulong load_end;
Simon Glass686f3bb2014-06-12 07:24:52 -0600379 ulong blob_start = os.start;
380 ulong blob_end = os.end;
381 ulong image_start = os.image_start;
382 ulong image_len = os.image_len;
Bryan O'Donoghue600388e2018-04-15 11:48:17 +0100383 ulong flush_start = ALIGN_DOWN(load, ARCH_DMA_MINALIGN);
Simon Glass686f3bb2014-06-12 07:24:52 -0600384 bool no_overlap;
385 void *load_buf, *image_buf;
386 int err;
387
388 load_buf = map_sysmem(load, 0);
389 image_buf = map_sysmem(os.image_start, image_len);
Julius Werner47ef9862019-07-24 19:37:54 -0700390 err = image_decomp(os.comp, load, os.image_start, os.type,
391 load_buf, image_buf, image_len,
392 CONFIG_SYS_BOOTM_LEN, &load_end);
Simon Glass686f3bb2014-06-12 07:24:52 -0600393 if (err) {
Julius Werner47ef9862019-07-24 19:37:54 -0700394 err = handle_decomp_error(os.comp, load_end - load, err);
Simon Glass686f3bb2014-06-12 07:24:52 -0600395 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
396 return err;
397 }
Heinrich Schuchardt6d29e0d2020-08-30 11:34:12 +0200398 /* We need the decompressed image size in the next steps */
399 images->os.image_len = load_end - load;
Bryan O'Donoghue600388e2018-04-15 11:48:17 +0100400
Trent Piepho92e51d02019-03-27 23:50:09 +0000401 flush_cache(flush_start, ALIGN(load_end, ARCH_DMA_MINALIGN) - flush_start);
Simon Glass3b5866d2014-06-12 07:24:46 -0600402
Tom Rini6c96f5d2018-05-01 12:32:37 -0400403 debug(" kernel loaded at 0x%08lx, end = 0x%08lx\n", load, load_end);
Simon Glass3b5866d2014-06-12 07:24:46 -0600404 bootstage_mark(BOOTSTAGE_ID_KERNEL_LOADED);
405
Simon Glass686f3bb2014-06-12 07:24:52 -0600406 no_overlap = (os.comp == IH_COMP_NONE && load == image_start);
407
Tom Rini6c96f5d2018-05-01 12:32:37 -0400408 if (!no_overlap && load < blob_end && load_end > blob_start) {
Simon Glass3b5866d2014-06-12 07:24:46 -0600409 debug("images.os.start = 0x%lX, images.os.end = 0x%lx\n",
410 blob_start, blob_end);
411 debug("images.os.load = 0x%lx, load_end = 0x%lx\n", load,
Tom Rini6c96f5d2018-05-01 12:32:37 -0400412 load_end);
Simon Glass3b5866d2014-06-12 07:24:46 -0600413
414 /* Check what type of image this is. */
415 if (images->legacy_hdr_valid) {
416 if (image_get_type(&images->legacy_hdr_os_copy)
417 == IH_TYPE_MULTI)
418 puts("WARNING: legacy format multi component image overwritten\n");
419 return BOOTM_ERR_OVERLAP;
420 } else {
421 puts("ERROR: new format image overwritten - must RESET the board to recover\n");
422 bootstage_error(BOOTSTAGE_ID_OVERWRITTEN);
423 return BOOTM_ERR_RESET;
424 }
425 }
426
Tom Rini6c96f5d2018-05-01 12:32:37 -0400427 lmb_reserve(&images->lmb, images->os.load, (load_end -
428 images->os.load));
Simon Glass3b5866d2014-06-12 07:24:46 -0600429 return 0;
430}
431
432/**
433 * bootm_disable_interrupts() - Disable interrupts in preparation for load/boot
434 *
435 * @return interrupt flag (0 if interrupts were disabled, non-zero if they were
436 * enabled)
437 */
438ulong bootm_disable_interrupts(void)
439{
440 ulong iflag;
441
442 /*
443 * We have reached the point of no return: we are going to
444 * overwrite all exception vector code, so we cannot easily
445 * recover from any failures any more...
446 */
447 iflag = disable_interrupts();
448#ifdef CONFIG_NETCONSOLE
449 /* Stop the ethernet stack if NetConsole could have left it up */
450 eth_halt();
Bernhard Nortmann1167a6c2015-09-14 15:29:45 +0200451# ifndef CONFIG_DM_ETH
Simon Glass3b5866d2014-06-12 07:24:46 -0600452 eth_unregister(eth_get_dev());
Bernhard Nortmann1167a6c2015-09-14 15:29:45 +0200453# endif
Simon Glass3b5866d2014-06-12 07:24:46 -0600454#endif
455
456#if defined(CONFIG_CMD_USB)
457 /*
458 * turn off USB to prevent the host controller from writing to the
459 * SDRAM while Linux is booting. This could happen (at least for OHCI
460 * controller), because the HCCA (Host Controller Communication Area)
461 * lies within the SDRAM and the host controller writes continously to
462 * this area (as busmaster!). The HccaFrameNumber is for example
463 * updated every 1 ms within the HCCA structure in SDRAM! For more
464 * details see the OpenHCI specification.
465 */
466 usb_stop();
467#endif
468 return iflag;
469}
470
Simon Glass7a8a4292020-11-05 10:33:42 -0700471#define CONSOLE_ARG "console="
472#define CONSOLE_ARG_SIZE sizeof(CONSOLE_ARG)
Simon Glass3b5866d2014-06-12 07:24:46 -0600473
Simon Glassc38406e2020-11-05 10:33:43 -0700474/**
475 * fixup_silent_linux() - Handle silencing the linux boot if required
476 *
477 * This uses the silent_linux envvar to control whether to add/set a "console="
478 * parameter to the command line
479 *
480 * @buf: Buffer containing the string to process
481 * @maxlen: Maximum length of buffer
482 * @return 0 if OK, -ENOSPC if @maxlen is too small
483 */
484static int fixup_silent_linux(char *buf, int maxlen)
Simon Glass3b5866d2014-06-12 07:24:46 -0600485{
Simon Glass3b5866d2014-06-12 07:24:46 -0600486 int want_silent;
Simon Glassc38406e2020-11-05 10:33:43 -0700487 char *cmdline;
488 int size;
Simon Glass07a88862020-11-05 10:33:38 -0700489
Simon Glass3b5866d2014-06-12 07:24:46 -0600490 /*
Simon Glassc38406e2020-11-05 10:33:43 -0700491 * Move the input string to the end of buffer. The output string will be
492 * built up at the start.
493 */
494 size = strlen(buf) + 1;
495 if (size * 2 > maxlen)
496 return -ENOSPC;
497 cmdline = buf + maxlen - size;
498 memmove(cmdline, buf, size);
499 /*
Simon Glass3b5866d2014-06-12 07:24:46 -0600500 * Only fix cmdline when requested. The environment variable can be:
501 *
502 * no - we never fixup
503 * yes - we always fixup
504 * unset - we rely on the console silent flag
505 */
Simon Glass22c34c22017-08-03 12:22:13 -0600506 want_silent = env_get_yesno("silent_linux");
Simon Glass3b5866d2014-06-12 07:24:46 -0600507 if (want_silent == 0)
Simon Glass2eab0172020-11-05 10:33:39 -0700508 return 0;
Simon Glass3b5866d2014-06-12 07:24:46 -0600509 else if (want_silent == -1 && !(gd->flags & GD_FLG_SILENT))
Simon Glass2eab0172020-11-05 10:33:39 -0700510 return 0;
Simon Glass3b5866d2014-06-12 07:24:46 -0600511
512 debug("before silent fix-up: %s\n", cmdline);
Simon Glassc38406e2020-11-05 10:33:43 -0700513 if (*cmdline) {
Simon Glass3b5866d2014-06-12 07:24:46 -0600514 char *start = strstr(cmdline, CONSOLE_ARG);
515
Simon Glassc38406e2020-11-05 10:33:43 -0700516 /* Check space for maximum possible new command line */
517 if (size + CONSOLE_ARG_SIZE > maxlen)
Simon Glass2eab0172020-11-05 10:33:39 -0700518 return -ENOSPC;
Simon Glass3b5866d2014-06-12 07:24:46 -0600519
520 if (start) {
521 char *end = strchr(start, ' ');
Simon Glass7a8a4292020-11-05 10:33:42 -0700522 int start_bytes;
Simon Glass3b5866d2014-06-12 07:24:46 -0600523
Simon Glass7a8a4292020-11-05 10:33:42 -0700524 start_bytes = start - cmdline + CONSOLE_ARG_SIZE - 1;
525 strncpy(buf, cmdline, start_bytes);
Simon Glass3b5866d2014-06-12 07:24:46 -0600526 if (end)
Simon Glass7a8a4292020-11-05 10:33:42 -0700527 strcpy(buf + start_bytes, end);
Simon Glass3b5866d2014-06-12 07:24:46 -0600528 else
Simon Glass7a8a4292020-11-05 10:33:42 -0700529 buf[start_bytes] = '\0';
Simon Glass3b5866d2014-06-12 07:24:46 -0600530 } else {
531 sprintf(buf, "%s %s", cmdline, CONSOLE_ARG);
532 }
Simon Glassc38406e2020-11-05 10:33:43 -0700533 if (buf + strlen(buf) >= cmdline)
534 return -ENOSPC;
Simon Glass3b5866d2014-06-12 07:24:46 -0600535 } else {
Simon Glassc38406e2020-11-05 10:33:43 -0700536 if (maxlen < sizeof(CONSOLE_ARG))
537 return -ENOSPC;
538 strcpy(buf, CONSOLE_ARG);
Simon Glass3b5866d2014-06-12 07:24:46 -0600539 }
Simon Glassc38406e2020-11-05 10:33:43 -0700540 debug("after silent fix-up: %s\n", buf);
541
542 return 0;
543}
544
Simon Glassb4e1b6d2020-11-05 10:33:45 -0700545int bootm_process_cmdline(char *buf, int maxlen, int flags)
546{
547 int ret;
548
549 /* Check config first to enable compiler to eliminate code */
550 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) &&
551 !IS_ENABLED(CONFIG_SILENT_U_BOOT_ONLY) &&
552 (flags & BOOTM_CL_SILENT)) {
553 ret = fixup_silent_linux(buf, maxlen);
554 if (ret)
555 return log_msg_ret("silent", ret);
556 }
557
558 return 0;
559}
560
Simon Glass63660dc2020-11-05 10:33:44 -0700561int bootm_process_cmdline_env(int flags)
Simon Glassc38406e2020-11-05 10:33:43 -0700562{
563 const int maxlen = MAX_CMDLINE_SIZE;
Simon Glass63660dc2020-11-05 10:33:44 -0700564 bool do_silent;
Simon Glassc38406e2020-11-05 10:33:43 -0700565 const char *env;
566 char *buf;
567 int ret;
Simon Glass3b5866d2014-06-12 07:24:46 -0600568
Simon Glassc38406e2020-11-05 10:33:43 -0700569 /* First check if any action is needed */
570 do_silent = IS_ENABLED(CONFIG_SILENT_CONSOLE) &&
Simon Glass63660dc2020-11-05 10:33:44 -0700571 !IS_ENABLED(CONFIG_SILENT_U_BOOT_ONLY) && (flags & BOOTM_CL_SILENT);
Simon Glassc38406e2020-11-05 10:33:43 -0700572 if (!do_silent)
573 return 0;
574
575 env = env_get("bootargs");
576 if (env && strlen(env) >= maxlen)
577 return -E2BIG;
578 buf = malloc(maxlen);
579 if (!buf)
580 return -ENOMEM;
581 if (env)
582 strcpy(buf, env);
583 else
584 *buf = '\0';
Simon Glassb4e1b6d2020-11-05 10:33:45 -0700585 ret = bootm_process_cmdline(buf, maxlen, flags);
Simon Glassc38406e2020-11-05 10:33:43 -0700586 if (!ret) {
587 ret = env_set("bootargs", buf);
588
589 /*
590 * If buf is "" and bootargs does not exist, this will produce
591 * an error trying to delete bootargs. Ignore it
592 */
593 if (ret == -ENOENT)
594 ret = 0;
595 }
Simon Glass3b5866d2014-06-12 07:24:46 -0600596 free(buf);
Simon Glassc38406e2020-11-05 10:33:43 -0700597 if (ret)
598 return log_msg_ret("env", ret);
Simon Glass2eab0172020-11-05 10:33:39 -0700599
600 return 0;
Simon Glass3b5866d2014-06-12 07:24:46 -0600601}
Simon Glass3b5866d2014-06-12 07:24:46 -0600602
603/**
604 * Execute selected states of the bootm command.
605 *
606 * Note the arguments to this state must be the first argument, Any 'bootm'
607 * or sub-command arguments must have already been taken.
608 *
609 * Note that if states contains more than one flag it MUST contain
610 * BOOTM_STATE_START, since this handles and consumes the command line args.
611 *
612 * Also note that aside from boot_os_fn functions and bootm_load_os no other
613 * functions we store the return value of in 'ret' may use a negative return
614 * value, without special handling.
615 *
616 * @param cmdtp Pointer to bootm command table entry
617 * @param flag Command flags (CMD_FLAG_...)
618 * @param argc Number of subcommand arguments (0 = no arguments)
619 * @param argv Arguments
620 * @param states Mask containing states to run (BOOTM_STATE_...)
621 * @param images Image header information
622 * @param boot_progress 1 to show boot progress, 0 to not do this
623 * @return 0 if ok, something else on error. Some errors will cause this
624 * function to perform a reboot! If states contains BOOTM_STATE_OS_GO
625 * then the intent is to boot an OS, so this function will not return
626 * unless the image type is standalone.
627 */
Simon Glassed38aef2020-05-10 11:40:03 -0600628int do_bootm_states(struct cmd_tbl *cmdtp, int flag, int argc,
629 char *const argv[], int states, bootm_headers_t *images,
630 int boot_progress)
Simon Glass3b5866d2014-06-12 07:24:46 -0600631{
632 boot_os_fn *boot_fn;
633 ulong iflag = 0;
634 int ret = 0, need_boot_fn;
635
636 images->state |= states;
637
638 /*
639 * Work through the states and see how far we get. We stop on
640 * any error.
641 */
642 if (states & BOOTM_STATE_START)
643 ret = bootm_start(cmdtp, flag, argc, argv);
644
645 if (!ret && (states & BOOTM_STATE_FINDOS))
646 ret = bootm_find_os(cmdtp, flag, argc, argv);
647
Zubair Lutfullah Kakakhel31eb47d2016-09-09 09:18:58 +0100648 if (!ret && (states & BOOTM_STATE_FINDOTHER))
Simon Glass3b5866d2014-06-12 07:24:46 -0600649 ret = bootm_find_other(cmdtp, flag, argc, argv);
Simon Glass3b5866d2014-06-12 07:24:46 -0600650
651 /* Load the OS */
652 if (!ret && (states & BOOTM_STATE_LOADOS)) {
Simon Glass3b5866d2014-06-12 07:24:46 -0600653 iflag = bootm_disable_interrupts();
Tom Rini6c96f5d2018-05-01 12:32:37 -0400654 ret = bootm_load_os(images, 0);
655 if (ret && ret != BOOTM_ERR_OVERLAP)
Simon Glass3b5866d2014-06-12 07:24:46 -0600656 goto err;
657 else if (ret == BOOTM_ERR_OVERLAP)
658 ret = 0;
Simon Glass3b5866d2014-06-12 07:24:46 -0600659 }
660
661 /* Relocate the ramdisk */
662#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
663 if (!ret && (states & BOOTM_STATE_RAMDISK)) {
664 ulong rd_len = images->rd_end - images->rd_start;
665
666 ret = boot_ramdisk_high(&images->lmb, images->rd_start,
667 rd_len, &images->initrd_start, &images->initrd_end);
668 if (!ret) {
Simon Glass4d949a22017-08-03 12:22:10 -0600669 env_set_hex("initrd_start", images->initrd_start);
670 env_set_hex("initrd_end", images->initrd_end);
Simon Glass3b5866d2014-06-12 07:24:46 -0600671 }
672 }
673#endif
Simon Glass8b426922016-02-22 22:55:45 -0700674#if IMAGE_ENABLE_OF_LIBFDT && defined(CONFIG_LMB)
Simon Glass3b5866d2014-06-12 07:24:46 -0600675 if (!ret && (states & BOOTM_STATE_FDT)) {
676 boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr);
677 ret = boot_relocate_fdt(&images->lmb, &images->ft_addr,
678 &images->ft_len);
679 }
680#endif
681
682 /* From now on, we need the OS boot function */
683 if (ret)
684 return ret;
685 boot_fn = bootm_os_get_boot_func(images->os.os);
686 need_boot_fn = states & (BOOTM_STATE_OS_CMDLINE |
687 BOOTM_STATE_OS_BD_T | BOOTM_STATE_OS_PREP |
688 BOOTM_STATE_OS_FAKE_GO | BOOTM_STATE_OS_GO);
689 if (boot_fn == NULL && need_boot_fn) {
690 if (iflag)
691 enable_interrupts();
692 printf("ERROR: booting os '%s' (%d) is not supported\n",
693 genimg_get_os_name(images->os.os), images->os.os);
694 bootstage_error(BOOTSTAGE_ID_CHECK_BOOT_OS);
695 return 1;
696 }
697
Hector Palacios8f9cc4c2016-07-11 12:34:37 +0200698
Simon Glass3b5866d2014-06-12 07:24:46 -0600699 /* Call various other states that are not generally used */
700 if (!ret && (states & BOOTM_STATE_OS_CMDLINE))
701 ret = boot_fn(BOOTM_STATE_OS_CMDLINE, argc, argv, images);
702 if (!ret && (states & BOOTM_STATE_OS_BD_T))
703 ret = boot_fn(BOOTM_STATE_OS_BD_T, argc, argv, images);
Hector Palacios8f9cc4c2016-07-11 12:34:37 +0200704 if (!ret && (states & BOOTM_STATE_OS_PREP)) {
Simon Glass63660dc2020-11-05 10:33:44 -0700705 ret = bootm_process_cmdline_env(images->os.os == IH_OS_LINUX ?
706 BOOTM_CL_SILENT : 0);
Simon Glassd87002f2020-11-05 10:33:41 -0700707 if (ret) {
708 printf("Cmdline setup failed (err=%d)\n", ret);
709 ret = CMD_RET_FAILURE;
710 goto err;
Simon Glass2eab0172020-11-05 10:33:39 -0700711 }
Simon Glass3b5866d2014-06-12 07:24:46 -0600712 ret = boot_fn(BOOTM_STATE_OS_PREP, argc, argv, images);
Hector Palacios8f9cc4c2016-07-11 12:34:37 +0200713 }
Simon Glass3b5866d2014-06-12 07:24:46 -0600714
715#ifdef CONFIG_TRACE
716 /* Pretend to run the OS, then run a user command */
717 if (!ret && (states & BOOTM_STATE_OS_FAKE_GO)) {
Simon Glass64b723f2017-08-03 12:22:12 -0600718 char *cmd_list = env_get("fakegocmd");
Simon Glass3b5866d2014-06-12 07:24:46 -0600719
720 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_FAKE_GO,
721 images, boot_fn);
722 if (!ret && cmd_list)
723 ret = run_command_list(cmd_list, -1, flag);
724 }
725#endif
726
727 /* Check for unsupported subcommand. */
728 if (ret) {
729 puts("subcommand not supported\n");
730 return ret;
731 }
732
733 /* Now run the OS! We hope this doesn't return */
734 if (!ret && (states & BOOTM_STATE_OS_GO))
735 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_GO,
736 images, boot_fn);
737
738 /* Deal with any fallout */
739err:
740 if (iflag)
741 enable_interrupts();
742
743 if (ret == BOOTM_ERR_UNIMPLEMENTED)
744 bootstage_error(BOOTSTAGE_ID_DECOMP_UNIMPL);
745 else if (ret == BOOTM_ERR_RESET)
746 do_reset(cmdtp, flag, argc, argv);
747
748 return ret;
749}
750
Tom Rinic220bd92019-05-23 07:14:07 -0400751#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Simon Glass3b5866d2014-06-12 07:24:46 -0600752/**
753 * image_get_kernel - verify legacy format kernel image
754 * @img_addr: in RAM address of the legacy format image to be verified
755 * @verify: data CRC verification flag
756 *
757 * image_get_kernel() verifies legacy image integrity and returns pointer to
758 * legacy image header if image verification was completed successfully.
759 *
760 * returns:
761 * pointer to a legacy image header if valid image was found
762 * otherwise return NULL
763 */
764static image_header_t *image_get_kernel(ulong img_addr, int verify)
765{
766 image_header_t *hdr = (image_header_t *)img_addr;
767
768 if (!image_check_magic(hdr)) {
769 puts("Bad Magic Number\n");
770 bootstage_error(BOOTSTAGE_ID_CHECK_MAGIC);
771 return NULL;
772 }
773 bootstage_mark(BOOTSTAGE_ID_CHECK_HEADER);
774
775 if (!image_check_hcrc(hdr)) {
776 puts("Bad Header Checksum\n");
777 bootstage_error(BOOTSTAGE_ID_CHECK_HEADER);
778 return NULL;
779 }
780
781 bootstage_mark(BOOTSTAGE_ID_CHECK_CHECKSUM);
782 image_print_contents(hdr);
783
784 if (verify) {
785 puts(" Verifying Checksum ... ");
786 if (!image_check_dcrc(hdr)) {
787 printf("Bad Data CRC\n");
788 bootstage_error(BOOTSTAGE_ID_CHECK_CHECKSUM);
789 return NULL;
790 }
791 puts("OK\n");
792 }
793 bootstage_mark(BOOTSTAGE_ID_CHECK_ARCH);
794
795 if (!image_check_target_arch(hdr)) {
796 printf("Unsupported Architecture 0x%x\n", image_get_arch(hdr));
797 bootstage_error(BOOTSTAGE_ID_CHECK_ARCH);
798 return NULL;
799 }
800 return hdr;
801}
802#endif
803
804/**
805 * boot_get_kernel - find kernel image
806 * @os_data: pointer to a ulong variable, will hold os data start address
807 * @os_len: pointer to a ulong variable, will hold os data length
808 *
809 * boot_get_kernel() tries to find a kernel image, verifies its integrity
810 * and locates kernel data.
811 *
812 * returns:
813 * pointer to image header if valid image was found, plus kernel start
814 * address and length, otherwise NULL
815 */
Simon Glassed38aef2020-05-10 11:40:03 -0600816static const void *boot_get_kernel(struct cmd_tbl *cmdtp, int flag, int argc,
817 char *const argv[], bootm_headers_t *images,
Simon Glass3b5866d2014-06-12 07:24:46 -0600818 ulong *os_data, ulong *os_len)
819{
Tom Rinic220bd92019-05-23 07:14:07 -0400820#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Simon Glass3b5866d2014-06-12 07:24:46 -0600821 image_header_t *hdr;
822#endif
823 ulong img_addr;
824 const void *buf;
Simon Glass3b5866d2014-06-12 07:24:46 -0600825 const char *fit_uname_config = NULL;
826 const char *fit_uname_kernel = NULL;
Simon Glasse3ee2fb2016-02-22 22:55:43 -0700827#if IMAGE_ENABLE_FIT
Simon Glass3b5866d2014-06-12 07:24:46 -0600828 int os_noffset;
829#endif
830
Bryan Wuc82ea372014-08-15 16:51:39 -0700831 img_addr = genimg_get_kernel_addr_fit(argc < 1 ? NULL : argv[0],
832 &fit_uname_config,
833 &fit_uname_kernel);
Simon Glass3b5866d2014-06-12 07:24:46 -0600834
835 bootstage_mark(BOOTSTAGE_ID_CHECK_MAGIC);
836
Simon Glass3b5866d2014-06-12 07:24:46 -0600837 /* check image type, for FIT images get FIT kernel node */
838 *os_data = *os_len = 0;
839 buf = map_sysmem(img_addr, 0);
840 switch (genimg_get_format(buf)) {
Tom Rinic220bd92019-05-23 07:14:07 -0400841#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Simon Glass3b5866d2014-06-12 07:24:46 -0600842 case IMAGE_FORMAT_LEGACY:
843 printf("## Booting kernel from Legacy Image at %08lx ...\n",
844 img_addr);
845 hdr = image_get_kernel(img_addr, images->verify);
846 if (!hdr)
847 return NULL;
848 bootstage_mark(BOOTSTAGE_ID_CHECK_IMAGETYPE);
849
850 /* get os_data and os_len */
851 switch (image_get_type(hdr)) {
852 case IH_TYPE_KERNEL:
853 case IH_TYPE_KERNEL_NOLOAD:
854 *os_data = image_get_data(hdr);
855 *os_len = image_get_data_size(hdr);
856 break;
857 case IH_TYPE_MULTI:
858 image_multi_getimg(hdr, 0, os_data, os_len);
859 break;
860 case IH_TYPE_STANDALONE:
861 *os_data = image_get_data(hdr);
862 *os_len = image_get_data_size(hdr);
863 break;
864 default:
865 printf("Wrong Image Type for %s command\n",
866 cmdtp->name);
867 bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE);
868 return NULL;
869 }
870
871 /*
872 * copy image header to allow for image overwrites during
873 * kernel decompression.
874 */
875 memmove(&images->legacy_hdr_os_copy, hdr,
876 sizeof(image_header_t));
877
878 /* save pointer to image header */
879 images->legacy_hdr_os = hdr;
880
881 images->legacy_hdr_valid = 1;
882 bootstage_mark(BOOTSTAGE_ID_DECOMP_IMAGE);
883 break;
884#endif
Simon Glasse3ee2fb2016-02-22 22:55:43 -0700885#if IMAGE_ENABLE_FIT
Simon Glass3b5866d2014-06-12 07:24:46 -0600886 case IMAGE_FORMAT_FIT:
Simon Glassa0c0b632014-06-12 07:24:47 -0600887 os_noffset = fit_image_load(images, img_addr,
Simon Glass3b5866d2014-06-12 07:24:46 -0600888 &fit_uname_kernel, &fit_uname_config,
889 IH_ARCH_DEFAULT, IH_TYPE_KERNEL,
890 BOOTSTAGE_ID_FIT_KERNEL_START,
891 FIT_LOAD_IGNORED, os_data, os_len);
892 if (os_noffset < 0)
893 return NULL;
894
895 images->fit_hdr_os = map_sysmem(img_addr, 0);
896 images->fit_uname_os = fit_uname_kernel;
897 images->fit_uname_cfg = fit_uname_config;
898 images->fit_noffset_os = os_noffset;
899 break;
900#endif
901#ifdef CONFIG_ANDROID_BOOT_IMAGE
902 case IMAGE_FORMAT_ANDROID:
903 printf("## Booting Android Image at 0x%08lx ...\n", img_addr);
Simon Glass95040102014-06-12 07:24:48 -0600904 if (android_image_get_kernel(buf, images->verify,
Simon Glass3b5866d2014-06-12 07:24:46 -0600905 os_data, os_len))
906 return NULL;
907 break;
908#endif
909 default:
910 printf("Wrong Image Format for %s command\n", cmdtp->name);
911 bootstage_error(BOOTSTAGE_ID_FIT_KERNEL_INFO);
912 return NULL;
913 }
914
915 debug(" kernel data at 0x%08lx, len = 0x%08lx (%ld)\n",
916 *os_data, *os_len, *os_len);
917
918 return buf;
919}
Heinrich Schuchardtaa0b11b2019-01-08 18:13:06 +0100920
921/**
922 * switch_to_non_secure_mode() - switch to non-secure mode
923 *
924 * This routine is overridden by architectures requiring this feature.
925 */
926void __weak switch_to_non_secure_mode(void)
927{
928}
929
Simon Glassa51991d2014-06-12 07:24:53 -0600930#else /* USE_HOSTCC */
931
Fabrice Fontained00d6b52019-05-03 22:37:05 +0200932#if defined(CONFIG_FIT_SIGNATURE)
Simon Glass81d13872020-03-18 11:44:02 -0600933static int bootm_host_load_image(const void *fit, int req_image_type,
934 int cfg_noffset)
Simon Glassa51991d2014-06-12 07:24:53 -0600935{
936 const char *fit_uname_config = NULL;
937 ulong data, len;
938 bootm_headers_t images;
939 int noffset;
940 ulong load_end;
941 uint8_t image_type;
942 uint8_t imape_comp;
943 void *load_buf;
944 int ret;
945
Simon Glass81d13872020-03-18 11:44:02 -0600946 fit_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
Simon Glassa51991d2014-06-12 07:24:53 -0600947 memset(&images, '\0', sizeof(images));
948 images.verify = 1;
949 noffset = fit_image_load(&images, (ulong)fit,
950 NULL, &fit_uname_config,
951 IH_ARCH_DEFAULT, req_image_type, -1,
952 FIT_LOAD_IGNORED, &data, &len);
953 if (noffset < 0)
954 return noffset;
955 if (fit_image_get_type(fit, noffset, &image_type)) {
956 puts("Can't get image type!\n");
957 return -EINVAL;
958 }
959
960 if (fit_image_get_comp(fit, noffset, &imape_comp)) {
961 puts("Can't get image compression!\n");
962 return -EINVAL;
963 }
964
965 /* Allow the image to expand by a factor of 4, should be safe */
966 load_buf = malloc((1 << 20) + len * 4);
Julius Werner47ef9862019-07-24 19:37:54 -0700967 ret = image_decomp(imape_comp, 0, data, image_type, load_buf,
968 (void *)data, len, CONFIG_SYS_BOOTM_LEN,
969 &load_end);
Simon Glassa51991d2014-06-12 07:24:53 -0600970 free(load_buf);
Simon Glass5aeccc22014-12-02 13:17:33 -0700971
Julius Werner47ef9862019-07-24 19:37:54 -0700972 if (ret) {
973 ret = handle_decomp_error(imape_comp, load_end - 0, ret);
974 if (ret != BOOTM_ERR_UNIMPLEMENTED)
975 return ret;
976 }
Simon Glassa51991d2014-06-12 07:24:53 -0600977
978 return 0;
979}
980
981int bootm_host_load_images(const void *fit, int cfg_noffset)
982{
983 static uint8_t image_types[] = {
984 IH_TYPE_KERNEL,
985 IH_TYPE_FLATDT,
986 IH_TYPE_RAMDISK,
987 };
988 int err = 0;
989 int i;
990
991 for (i = 0; i < ARRAY_SIZE(image_types); i++) {
992 int ret;
993
Simon Glass81d13872020-03-18 11:44:02 -0600994 ret = bootm_host_load_image(fit, image_types[i], cfg_noffset);
Simon Glassa51991d2014-06-12 07:24:53 -0600995 if (!err && ret && ret != -ENOENT)
996 err = ret;
997 }
998
999 /* Return the first error we found */
1000 return err;
1001}
Fabrice Fontained00d6b52019-05-03 22:37:05 +02001002#endif
Simon Glass0232ba72014-06-12 07:24:51 -06001003
1004#endif /* ndef USE_HOSTCC */