blob: 00c00aef84aed3398e4b9fe450b9128422534815 [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 Glass529e2082020-11-05 10:33:48 -070010#include <cli.h>
Simon Glass63334482019-11-14 12:57:39 -070011#include <cpu_func.h>
Simon Glass313112a2019-08-01 09:46:46 -060012#include <env.h>
Simon Glass0129b522014-10-19 21:11:24 -060013#include <errno.h>
Simon Glass3b5866d2014-06-12 07:24:46 -060014#include <fdt_support.h>
Simon Glass8f3f7612019-11-14 12:57:42 -070015#include <irq_func.h>
Simon Glass3b5866d2014-06-12 07:24:46 -060016#include <lmb.h>
Simon Glass0f2af882020-05-10 11:40:05 -060017#include <log.h>
Simon Glass3b5866d2014-06-12 07:24:46 -060018#include <malloc.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050019#include <mapmem.h>
Simon Glass274e0b02020-05-10 11:39:56 -060020#include <net.h>
21#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060022#include <asm/global_data.h>
Simon Glass3b5866d2014-06-12 07:24:46 -060023#include <asm/io.h>
Simon Glassc38406e2020-11-05 10:33:43 -070024#include <linux/sizes.h>
Simon Glass3b5866d2014-06-12 07:24:46 -060025#if defined(CONFIG_CMD_USB)
26#include <usb.h>
27#endif
Simon Glass0232ba72014-06-12 07:24:51 -060028#else
29#include "mkimage.h"
30#endif
Simon Glass3b5866d2014-06-12 07:24:46 -060031
Simon Glass0232ba72014-06-12 07:24:51 -060032#include <command.h>
33#include <bootm.h>
34#include <image.h>
Simon Glass3b5866d2014-06-12 07:24:46 -060035
36#ifndef CONFIG_SYS_BOOTM_LEN
37/* use 8MByte as default max gunzip size */
38#define CONFIG_SYS_BOOTM_LEN 0x800000
39#endif
40
Simon Glassc38406e2020-11-05 10:33:43 -070041#define MAX_CMDLINE_SIZE SZ_4K
42
Simon Glass3b5866d2014-06-12 07:24:46 -060043#define IH_INITRD_ARCH IH_ARCH_DEFAULT
44
Simon Glass0232ba72014-06-12 07:24:51 -060045#ifndef USE_HOSTCC
46
47DECLARE_GLOBAL_DATA_PTR;
48
Tom Rini36f067f2016-08-12 08:31:15 -040049bootm_headers_t images; /* pointers to os/initrd/fdt images */
50
Simon Glassed38aef2020-05-10 11:40:03 -060051static const void *boot_get_kernel(struct cmd_tbl *cmdtp, int flag, int argc,
52 char *const argv[], bootm_headers_t *images,
Simon Glass3b5866d2014-06-12 07:24:46 -060053 ulong *os_data, ulong *os_len);
54
Simon Glassf55305a2018-05-16 09:42:25 -060055__weak void board_quiesce_devices(void)
56{
57}
58
Simon Glass3b5866d2014-06-12 07:24:46 -060059#ifdef CONFIG_LMB
60static void boot_start_lmb(bootm_headers_t *images)
61{
62 ulong mem_start;
63 phys_size_t mem_size;
64
Simon Glassda1a1342017-08-03 12:22:15 -060065 mem_start = env_get_bootm_low();
66 mem_size = env_get_bootm_size();
Simon Glass3b5866d2014-06-12 07:24:46 -060067
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +010068 lmb_init_and_reserve_range(&images->lmb, (phys_addr_t)mem_start,
69 mem_size, NULL);
Simon Glass3b5866d2014-06-12 07:24:46 -060070}
71#else
72#define lmb_reserve(lmb, base, size)
73static inline void boot_start_lmb(bootm_headers_t *images) { }
74#endif
75
Simon Glassed38aef2020-05-10 11:40:03 -060076static int bootm_start(struct cmd_tbl *cmdtp, int flag, int argc,
77 char *const argv[])
Simon Glass3b5866d2014-06-12 07:24:46 -060078{
79 memset((void *)&images, 0, sizeof(images));
Simon Glass22c34c22017-08-03 12:22:13 -060080 images.verify = env_get_yesno("verify");
Simon Glass3b5866d2014-06-12 07:24:46 -060081
82 boot_start_lmb(&images);
83
84 bootstage_mark_name(BOOTSTAGE_ID_BOOTM_START, "bootm_start");
85 images.state = BOOTM_STATE_START;
86
87 return 0;
88}
89
Simon Glassed38aef2020-05-10 11:40:03 -060090static int bootm_find_os(struct cmd_tbl *cmdtp, int flag, int argc,
91 char *const argv[])
Simon Glass3b5866d2014-06-12 07:24:46 -060092{
93 const void *os_hdr;
94 bool ep_found = false;
Simon Glass0129b522014-10-19 21:11:24 -060095 int ret;
Simon Glass3b5866d2014-06-12 07:24:46 -060096
97 /* get kernel image header, start address and length */
98 os_hdr = boot_get_kernel(cmdtp, flag, argc, argv,
99 &images, &images.os.image_start, &images.os.image_len);
100 if (images.os.image_len == 0) {
101 puts("ERROR: can't get kernel image!\n");
102 return 1;
103 }
104
105 /* get image parameters */
106 switch (genimg_get_format(os_hdr)) {
Tom Rinic220bd92019-05-23 07:14:07 -0400107#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Simon Glass3b5866d2014-06-12 07:24:46 -0600108 case IMAGE_FORMAT_LEGACY:
109 images.os.type = image_get_type(os_hdr);
110 images.os.comp = image_get_comp(os_hdr);
111 images.os.os = image_get_os(os_hdr);
112
113 images.os.end = image_get_image_end(os_hdr);
114 images.os.load = image_get_load(os_hdr);
Simon Glass0129b522014-10-19 21:11:24 -0600115 images.os.arch = image_get_arch(os_hdr);
Simon Glass3b5866d2014-06-12 07:24:46 -0600116 break;
117#endif
Simon Glasse719d3b2021-09-25 19:43:20 -0600118#if CONFIG_IS_ENABLED(FIT)
Simon Glass3b5866d2014-06-12 07:24:46 -0600119 case IMAGE_FORMAT_FIT:
120 if (fit_image_get_type(images.fit_hdr_os,
121 images.fit_noffset_os,
122 &images.os.type)) {
123 puts("Can't get image type!\n");
124 bootstage_error(BOOTSTAGE_ID_FIT_TYPE);
125 return 1;
126 }
127
128 if (fit_image_get_comp(images.fit_hdr_os,
129 images.fit_noffset_os,
130 &images.os.comp)) {
131 puts("Can't get image compression!\n");
132 bootstage_error(BOOTSTAGE_ID_FIT_COMPRESSION);
133 return 1;
134 }
135
136 if (fit_image_get_os(images.fit_hdr_os, images.fit_noffset_os,
137 &images.os.os)) {
138 puts("Can't get image OS!\n");
139 bootstage_error(BOOTSTAGE_ID_FIT_OS);
140 return 1;
141 }
142
Simon Glass0129b522014-10-19 21:11:24 -0600143 if (fit_image_get_arch(images.fit_hdr_os,
144 images.fit_noffset_os,
145 &images.os.arch)) {
146 puts("Can't get image ARCH!\n");
147 return 1;
148 }
149
Simon Glass3b5866d2014-06-12 07:24:46 -0600150 images.os.end = fit_get_end(images.fit_hdr_os);
151
152 if (fit_image_get_load(images.fit_hdr_os, images.fit_noffset_os,
153 &images.os.load)) {
154 puts("Can't get image load address!\n");
155 bootstage_error(BOOTSTAGE_ID_FIT_LOADADDR);
156 return 1;
157 }
158 break;
159#endif
160#ifdef CONFIG_ANDROID_BOOT_IMAGE
161 case IMAGE_FORMAT_ANDROID:
162 images.os.type = IH_TYPE_KERNEL;
Eugeniu Rosca1403f392019-04-08 17:35:27 +0200163 images.os.comp = android_image_get_kcomp(os_hdr);
Simon Glass3b5866d2014-06-12 07:24:46 -0600164 images.os.os = IH_OS_LINUX;
Simon Glass3b5866d2014-06-12 07:24:46 -0600165
166 images.os.end = android_image_get_end(os_hdr);
167 images.os.load = android_image_get_kload(os_hdr);
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300168 images.ep = images.os.load;
169 ep_found = true;
Simon Glass3b5866d2014-06-12 07:24:46 -0600170 break;
171#endif
172 default:
173 puts("ERROR: unknown image format type!\n");
174 return 1;
175 }
176
Simon Glass0129b522014-10-19 21:11:24 -0600177 /* If we have a valid setup.bin, we will use that for entry (x86) */
Simon Glass9d428302014-10-10 08:21:57 -0600178 if (images.os.arch == IH_ARCH_I386 ||
179 images.os.arch == IH_ARCH_X86_64) {
Simon Glass0129b522014-10-19 21:11:24 -0600180 ulong len;
181
182 ret = boot_get_setup(&images, IH_ARCH_I386, &images.ep, &len);
183 if (ret < 0 && ret != -ENOENT) {
184 puts("Could not find a valid setup.bin for x86\n");
185 return 1;
186 }
187 /* Kernel entry point is the setup.bin */
188 } else if (images.legacy_hdr_valid) {
Simon Glass3b5866d2014-06-12 07:24:46 -0600189 images.ep = image_get_ep(&images.legacy_hdr_os_copy);
Simon Glasse719d3b2021-09-25 19:43:20 -0600190#if CONFIG_IS_ENABLED(FIT)
Simon Glass3b5866d2014-06-12 07:24:46 -0600191 } else if (images.fit_uname_os) {
192 int ret;
193
194 ret = fit_image_get_entry(images.fit_hdr_os,
195 images.fit_noffset_os, &images.ep);
196 if (ret) {
197 puts("Can't get entry point property!\n");
198 return 1;
199 }
200#endif
201 } else if (!ep_found) {
202 puts("Could not find kernel entry point!\n");
203 return 1;
204 }
205
206 if (images.os.type == IH_TYPE_KERNEL_NOLOAD) {
Marek Vasut21425792018-06-13 06:13:33 +0200207 if (CONFIG_IS_ENABLED(CMD_BOOTI) &&
208 images.os.arch == IH_ARCH_ARM64) {
209 ulong image_addr;
210 ulong image_size;
211
212 ret = booti_setup(images.os.image_start, &image_addr,
213 &image_size, true);
214 if (ret != 0)
215 return 1;
216
217 images.os.type = IH_TYPE_KERNEL;
218 images.os.load = image_addr;
219 images.ep = image_addr;
220 } else {
221 images.os.load = images.os.image_start;
222 images.ep += images.os.image_start;
223 }
Simon Glass3b5866d2014-06-12 07:24:46 -0600224 }
225
Simon Glass5b539a02016-02-24 09:14:42 -0700226 images.os.start = map_to_sysmem(os_hdr);
Simon Glass3b5866d2014-06-12 07:24:46 -0600227
228 return 0;
229}
230
Karl Apsited686ce92015-05-21 09:52:49 -0400231/**
232 * bootm_find_images - wrapper to find and locate various images
233 * @flag: Ignored Argument
234 * @argc: command argument count
235 * @argv: command argument list
Tero Kristo06086942020-06-12 15:41:20 +0300236 * @start: OS image start address
237 * @size: OS image size
Karl Apsited686ce92015-05-21 09:52:49 -0400238 *
239 * boot_find_images() will attempt to load an available ramdisk,
240 * flattened device tree, as well as specifically marked
241 * "loadable" images (loadables are FIT only)
242 *
243 * Note: bootm_find_images will skip an image if it is not found
244 *
245 * @return:
246 * 0, if all existing images were loaded correctly
247 * 1, if an image is found but corrupted, or invalid
248 */
Tero Kristo06086942020-06-12 15:41:20 +0300249int bootm_find_images(int flag, int argc, char *const argv[], ulong start,
250 ulong size)
Simon Glass3b5866d2014-06-12 07:24:46 -0600251{
252 int ret;
253
254 /* find ramdisk */
255 ret = boot_get_ramdisk(argc, argv, &images, IH_INITRD_ARCH,
256 &images.rd_start, &images.rd_end);
257 if (ret) {
258 puts("Ramdisk image is corrupt or invalid\n");
259 return 1;
260 }
261
Tero Kristo06086942020-06-12 15:41:20 +0300262 /* check if ramdisk overlaps OS image */
263 if (images.rd_start && (((ulong)images.rd_start >= start &&
Jaehoon Chung80ffb812020-10-21 14:17:03 +0900264 (ulong)images.rd_start < start + size) ||
265 ((ulong)images.rd_end > start &&
266 (ulong)images.rd_end <= start + size) ||
267 ((ulong)images.rd_start < start &&
268 (ulong)images.rd_end >= start + size))) {
Tero Kristo06086942020-06-12 15:41:20 +0300269 printf("ERROR: RD image overlaps OS image (OS=0x%lx..0x%lx)\n",
270 start, start + size);
271 return 1;
272 }
273
Simon Glass85c057e2021-09-25 19:43:21 -0600274#if CONFIG_IS_ENABLED(OF_LIBFDT)
Simon Glass3b5866d2014-06-12 07:24:46 -0600275 /* find flattened device tree */
276 ret = boot_get_fdt(flag, argc, argv, IH_ARCH_DEFAULT, &images,
277 &images.ft_addr, &images.ft_len);
278 if (ret) {
279 puts("Could not find a valid device tree\n");
280 return 1;
281 }
Tero Kristo06086942020-06-12 15:41:20 +0300282
283 /* check if FDT overlaps OS image */
284 if (images.ft_addr &&
285 (((ulong)images.ft_addr >= start &&
286 (ulong)images.ft_addr <= start + size) ||
287 ((ulong)images.ft_addr + images.ft_len >= start &&
288 (ulong)images.ft_addr + images.ft_len <= start + size))) {
289 printf("ERROR: FDT image overlaps OS image (OS=0x%lx..0x%lx)\n",
290 start, start + size);
291 return 1;
292 }
293
Simon Goldschmidt6127ac02018-12-17 20:14:42 +0100294 if (CONFIG_IS_ENABLED(CMD_FDT))
295 set_working_fdt_addr(map_to_sysmem(images.ft_addr));
Simon Glass3b5866d2014-06-12 07:24:46 -0600296#endif
297
Simon Glasse719d3b2021-09-25 19:43:20 -0600298#if CONFIG_IS_ENABLED(FIT)
Simon Glassbc0c4db2021-09-25 07:03:20 -0600299 if (IS_ENABLED(CONFIG_FPGA)) {
300 /* find bitstreams */
301 ret = boot_get_fpga(argc, argv, &images, IH_ARCH_DEFAULT,
302 NULL, NULL);
303 if (ret) {
304 printf("FPGA image is corrupted or invalid\n");
305 return 1;
306 }
Michal Simekebae78b2016-05-17 14:03:50 +0200307 }
Michal Simekebae78b2016-05-17 14:03:50 +0200308
Karl Apsite1b21c282015-05-21 09:52:48 -0400309 /* find all of the loadables */
310 ret = boot_get_loadable(argc, argv, &images, IH_ARCH_DEFAULT,
311 NULL, NULL);
312 if (ret) {
313 printf("Loadable(s) is corrupt or invalid\n");
314 return 1;
315 }
Karl Apsite1b21c282015-05-21 09:52:48 -0400316#endif
317
Simon Glass3b5866d2014-06-12 07:24:46 -0600318 return 0;
319}
320
Simon Glassed38aef2020-05-10 11:40:03 -0600321static int bootm_find_other(struct cmd_tbl *cmdtp, int flag, int argc,
322 char *const argv[])
Simon Glass3b5866d2014-06-12 07:24:46 -0600323{
324 if (((images.os.type == IH_TYPE_KERNEL) ||
325 (images.os.type == IH_TYPE_KERNEL_NOLOAD) ||
326 (images.os.type == IH_TYPE_MULTI)) &&
327 (images.os.os == IH_OS_LINUX ||
328 images.os.os == IH_OS_VXWORKS))
Tero Kristo06086942020-06-12 15:41:20 +0300329 return bootm_find_images(flag, argc, argv, 0, 0);
Simon Glass3b5866d2014-06-12 07:24:46 -0600330
331 return 0;
332}
Simon Glass10ae4192014-12-02 13:17:30 -0700333#endif /* USE_HOSTC */
334
Julius Werner47ef9862019-07-24 19:37:54 -0700335#if !defined(USE_HOSTCC) || defined(CONFIG_FIT_SIGNATURE)
Simon Glass4f6e6d72014-12-02 13:17:37 -0700336/**
337 * handle_decomp_error() - display a decompression error
338 *
339 * This function tries to produce a useful message. In the case where the
340 * uncompressed size is the same as the available space, we can assume that
341 * the image is too large for the buffer.
342 *
343 * @comp_type: Compression type being used (IH_COMP_...)
344 * @uncomp_size: Number of bytes uncompressed
Julius Werner47ef9862019-07-24 19:37:54 -0700345 * @ret: errno error code received from compression library
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100346 * Return: Appropriate BOOTM_ERR_ error code
Simon Glass4f6e6d72014-12-02 13:17:37 -0700347 */
Julius Werner47ef9862019-07-24 19:37:54 -0700348static int handle_decomp_error(int comp_type, size_t uncomp_size, int ret)
Simon Glass10ae4192014-12-02 13:17:30 -0700349{
Simon Glass4f6e6d72014-12-02 13:17:37 -0700350 const char *name = genimg_get_comp_name(comp_type);
351
Julius Werner47ef9862019-07-24 19:37:54 -0700352 /* ENOSYS means unimplemented compression type, don't reset. */
353 if (ret == -ENOSYS)
354 return BOOTM_ERR_UNIMPLEMENTED;
355
356 if (uncomp_size >= CONFIG_SYS_BOOTM_LEN)
Simon Glass4f6e6d72014-12-02 13:17:37 -0700357 printf("Image too large: increase CONFIG_SYS_BOOTM_LEN\n");
Simon Glass10ae4192014-12-02 13:17:30 -0700358 else
Simon Glass4f6e6d72014-12-02 13:17:37 -0700359 printf("%s: uncompress error %d\n", name, ret);
360
361 /*
362 * The decompression routines are now safe, so will not write beyond
363 * their bounds. Probably it is not necessary to reset, but maintain
364 * the current behaviour for now.
365 */
366 printf("Must RESET board to recover\n");
Simon Glass10ae4192014-12-02 13:17:30 -0700367#ifndef USE_HOSTCC
368 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
369#endif
370
371 return BOOTM_ERR_RESET;
372}
Julius Werner47ef9862019-07-24 19:37:54 -0700373#endif
Simon Glass686f3bb2014-06-12 07:24:52 -0600374
Simon Glassa51991d2014-06-12 07:24:53 -0600375#ifndef USE_HOSTCC
Tom Rini6c96f5d2018-05-01 12:32:37 -0400376static int bootm_load_os(bootm_headers_t *images, int boot_progress)
Simon Glass686f3bb2014-06-12 07:24:52 -0600377{
378 image_info_t os = images->os;
379 ulong load = os.load;
Tom Rini6c96f5d2018-05-01 12:32:37 -0400380 ulong load_end;
Simon Glass686f3bb2014-06-12 07:24:52 -0600381 ulong blob_start = os.start;
382 ulong blob_end = os.end;
383 ulong image_start = os.image_start;
384 ulong image_len = os.image_len;
Bryan O'Donoghue600388e2018-04-15 11:48:17 +0100385 ulong flush_start = ALIGN_DOWN(load, ARCH_DMA_MINALIGN);
Simon Glass686f3bb2014-06-12 07:24:52 -0600386 bool no_overlap;
387 void *load_buf, *image_buf;
388 int err;
389
390 load_buf = map_sysmem(load, 0);
391 image_buf = map_sysmem(os.image_start, image_len);
Julius Werner47ef9862019-07-24 19:37:54 -0700392 err = image_decomp(os.comp, load, os.image_start, os.type,
393 load_buf, image_buf, image_len,
394 CONFIG_SYS_BOOTM_LEN, &load_end);
Simon Glass686f3bb2014-06-12 07:24:52 -0600395 if (err) {
Julius Werner47ef9862019-07-24 19:37:54 -0700396 err = handle_decomp_error(os.comp, load_end - load, err);
Simon Glass686f3bb2014-06-12 07:24:52 -0600397 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
398 return err;
399 }
Heinrich Schuchardt6d29e0d2020-08-30 11:34:12 +0200400 /* We need the decompressed image size in the next steps */
401 images->os.image_len = load_end - load;
Bryan O'Donoghue600388e2018-04-15 11:48:17 +0100402
Trent Piepho92e51d02019-03-27 23:50:09 +0000403 flush_cache(flush_start, ALIGN(load_end, ARCH_DMA_MINALIGN) - flush_start);
Simon Glass3b5866d2014-06-12 07:24:46 -0600404
Tom Rini6c96f5d2018-05-01 12:32:37 -0400405 debug(" kernel loaded at 0x%08lx, end = 0x%08lx\n", load, load_end);
Simon Glass3b5866d2014-06-12 07:24:46 -0600406 bootstage_mark(BOOTSTAGE_ID_KERNEL_LOADED);
407
Simon Glass686f3bb2014-06-12 07:24:52 -0600408 no_overlap = (os.comp == IH_COMP_NONE && load == image_start);
409
Tom Rini6c96f5d2018-05-01 12:32:37 -0400410 if (!no_overlap && load < blob_end && load_end > blob_start) {
Simon Glass3b5866d2014-06-12 07:24:46 -0600411 debug("images.os.start = 0x%lX, images.os.end = 0x%lx\n",
412 blob_start, blob_end);
413 debug("images.os.load = 0x%lx, load_end = 0x%lx\n", load,
Tom Rini6c96f5d2018-05-01 12:32:37 -0400414 load_end);
Simon Glass3b5866d2014-06-12 07:24:46 -0600415
416 /* Check what type of image this is. */
417 if (images->legacy_hdr_valid) {
418 if (image_get_type(&images->legacy_hdr_os_copy)
419 == IH_TYPE_MULTI)
420 puts("WARNING: legacy format multi component image overwritten\n");
421 return BOOTM_ERR_OVERLAP;
422 } else {
423 puts("ERROR: new format image overwritten - must RESET the board to recover\n");
424 bootstage_error(BOOTSTAGE_ID_OVERWRITTEN);
425 return BOOTM_ERR_RESET;
426 }
427 }
428
Tom Rini6c96f5d2018-05-01 12:32:37 -0400429 lmb_reserve(&images->lmb, images->os.load, (load_end -
430 images->os.load));
Simon Glass3b5866d2014-06-12 07:24:46 -0600431 return 0;
432}
433
434/**
435 * bootm_disable_interrupts() - Disable interrupts in preparation for load/boot
436 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100437 * Return: interrupt flag (0 if interrupts were disabled, non-zero if they were
Simon Glass3b5866d2014-06-12 07:24:46 -0600438 * enabled)
439 */
440ulong bootm_disable_interrupts(void)
441{
442 ulong iflag;
443
444 /*
445 * We have reached the point of no return: we are going to
446 * overwrite all exception vector code, so we cannot easily
447 * recover from any failures any more...
448 */
449 iflag = disable_interrupts();
450#ifdef CONFIG_NETCONSOLE
451 /* Stop the ethernet stack if NetConsole could have left it up */
452 eth_halt();
Bernhard Nortmann1167a6c2015-09-14 15:29:45 +0200453# ifndef CONFIG_DM_ETH
Simon Glass3b5866d2014-06-12 07:24:46 -0600454 eth_unregister(eth_get_dev());
Bernhard Nortmann1167a6c2015-09-14 15:29:45 +0200455# endif
Simon Glass3b5866d2014-06-12 07:24:46 -0600456#endif
457
458#if defined(CONFIG_CMD_USB)
459 /*
460 * turn off USB to prevent the host controller from writing to the
461 * SDRAM while Linux is booting. This could happen (at least for OHCI
462 * controller), because the HCCA (Host Controller Communication Area)
463 * lies within the SDRAM and the host controller writes continously to
464 * this area (as busmaster!). The HccaFrameNumber is for example
465 * updated every 1 ms within the HCCA structure in SDRAM! For more
466 * details see the OpenHCI specification.
467 */
468 usb_stop();
469#endif
470 return iflag;
471}
472
Simon Glass7a8a4292020-11-05 10:33:42 -0700473#define CONSOLE_ARG "console="
474#define CONSOLE_ARG_SIZE sizeof(CONSOLE_ARG)
Simon Glass3b5866d2014-06-12 07:24:46 -0600475
Simon Glassc38406e2020-11-05 10:33:43 -0700476/**
477 * fixup_silent_linux() - Handle silencing the linux boot if required
478 *
479 * This uses the silent_linux envvar to control whether to add/set a "console="
480 * parameter to the command line
481 *
482 * @buf: Buffer containing the string to process
483 * @maxlen: Maximum length of buffer
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100484 * Return: 0 if OK, -ENOSPC if @maxlen is too small
Simon Glassc38406e2020-11-05 10:33:43 -0700485 */
486static int fixup_silent_linux(char *buf, int maxlen)
Simon Glass3b5866d2014-06-12 07:24:46 -0600487{
Simon Glass3b5866d2014-06-12 07:24:46 -0600488 int want_silent;
Simon Glassc38406e2020-11-05 10:33:43 -0700489 char *cmdline;
490 int size;
Simon Glass07a88862020-11-05 10:33:38 -0700491
Simon Glass3b5866d2014-06-12 07:24:46 -0600492 /*
Simon Glassc38406e2020-11-05 10:33:43 -0700493 * Move the input string to the end of buffer. The output string will be
494 * built up at the start.
495 */
496 size = strlen(buf) + 1;
497 if (size * 2 > maxlen)
498 return -ENOSPC;
499 cmdline = buf + maxlen - size;
500 memmove(cmdline, buf, size);
501 /*
Simon Glass3b5866d2014-06-12 07:24:46 -0600502 * Only fix cmdline when requested. The environment variable can be:
503 *
504 * no - we never fixup
505 * yes - we always fixup
506 * unset - we rely on the console silent flag
507 */
Simon Glass22c34c22017-08-03 12:22:13 -0600508 want_silent = env_get_yesno("silent_linux");
Simon Glass3b5866d2014-06-12 07:24:46 -0600509 if (want_silent == 0)
Simon Glass2eab0172020-11-05 10:33:39 -0700510 return 0;
Simon Glass3b5866d2014-06-12 07:24:46 -0600511 else if (want_silent == -1 && !(gd->flags & GD_FLG_SILENT))
Simon Glass2eab0172020-11-05 10:33:39 -0700512 return 0;
Simon Glass3b5866d2014-06-12 07:24:46 -0600513
514 debug("before silent fix-up: %s\n", cmdline);
Simon Glassc38406e2020-11-05 10:33:43 -0700515 if (*cmdline) {
Simon Glass3b5866d2014-06-12 07:24:46 -0600516 char *start = strstr(cmdline, CONSOLE_ARG);
517
Simon Glassc38406e2020-11-05 10:33:43 -0700518 /* Check space for maximum possible new command line */
519 if (size + CONSOLE_ARG_SIZE > maxlen)
Simon Glass2eab0172020-11-05 10:33:39 -0700520 return -ENOSPC;
Simon Glass3b5866d2014-06-12 07:24:46 -0600521
522 if (start) {
523 char *end = strchr(start, ' ');
Simon Glass7a8a4292020-11-05 10:33:42 -0700524 int start_bytes;
Simon Glass3b5866d2014-06-12 07:24:46 -0600525
Simon Glass7a8a4292020-11-05 10:33:42 -0700526 start_bytes = start - cmdline + CONSOLE_ARG_SIZE - 1;
527 strncpy(buf, cmdline, start_bytes);
Simon Glass3b5866d2014-06-12 07:24:46 -0600528 if (end)
Simon Glass7a8a4292020-11-05 10:33:42 -0700529 strcpy(buf + start_bytes, end);
Simon Glass3b5866d2014-06-12 07:24:46 -0600530 else
Simon Glass7a8a4292020-11-05 10:33:42 -0700531 buf[start_bytes] = '\0';
Simon Glass3b5866d2014-06-12 07:24:46 -0600532 } else {
533 sprintf(buf, "%s %s", cmdline, CONSOLE_ARG);
534 }
Simon Glassc38406e2020-11-05 10:33:43 -0700535 if (buf + strlen(buf) >= cmdline)
536 return -ENOSPC;
Simon Glass3b5866d2014-06-12 07:24:46 -0600537 } else {
Simon Glassc38406e2020-11-05 10:33:43 -0700538 if (maxlen < sizeof(CONSOLE_ARG))
539 return -ENOSPC;
540 strcpy(buf, CONSOLE_ARG);
Simon Glass3b5866d2014-06-12 07:24:46 -0600541 }
Simon Glassc38406e2020-11-05 10:33:43 -0700542 debug("after silent fix-up: %s\n", buf);
543
544 return 0;
545}
546
Simon Glass529e2082020-11-05 10:33:48 -0700547/**
548 * process_subst() - Handle substitution of ${...} fields in the environment
549 *
550 * Handle variable substitution in the provided buffer
551 *
552 * @buf: Buffer containing the string to process
553 * @maxlen: Maximum length of buffer
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100554 * Return: 0 if OK, -ENOSPC if @maxlen is too small
Simon Glass529e2082020-11-05 10:33:48 -0700555 */
556static int process_subst(char *buf, int maxlen)
557{
558 char *cmdline;
559 int size;
560 int ret;
561
562 /* Move to end of buffer */
563 size = strlen(buf) + 1;
564 cmdline = buf + maxlen - size;
565 if (buf + size > cmdline)
566 return -ENOSPC;
567 memmove(cmdline, buf, size);
568
569 ret = cli_simple_process_macros(cmdline, buf, cmdline - buf);
570
571 return ret;
572}
573
Simon Glassb4e1b6d2020-11-05 10:33:45 -0700574int bootm_process_cmdline(char *buf, int maxlen, int flags)
575{
576 int ret;
577
578 /* Check config first to enable compiler to eliminate code */
579 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) &&
580 !IS_ENABLED(CONFIG_SILENT_U_BOOT_ONLY) &&
581 (flags & BOOTM_CL_SILENT)) {
582 ret = fixup_silent_linux(buf, maxlen);
583 if (ret)
584 return log_msg_ret("silent", ret);
585 }
Simon Glassed8603d2021-03-15 18:11:23 +1300586 if (IS_ENABLED(CONFIG_BOOTARGS_SUBST) && IS_ENABLED(CONFIG_CMDLINE) &&
587 (flags & BOOTM_CL_SUBST)) {
Simon Glass529e2082020-11-05 10:33:48 -0700588 ret = process_subst(buf, maxlen);
589 if (ret)
Simon Glasscac744e2021-02-06 09:57:35 -0700590 return log_msg_ret("subst", ret);
Simon Glass529e2082020-11-05 10:33:48 -0700591 }
Simon Glassb4e1b6d2020-11-05 10:33:45 -0700592
593 return 0;
594}
595
Simon Glass63660dc2020-11-05 10:33:44 -0700596int bootm_process_cmdline_env(int flags)
Simon Glassc38406e2020-11-05 10:33:43 -0700597{
598 const int maxlen = MAX_CMDLINE_SIZE;
Simon Glass63660dc2020-11-05 10:33:44 -0700599 bool do_silent;
Simon Glassc38406e2020-11-05 10:33:43 -0700600 const char *env;
601 char *buf;
602 int ret;
Simon Glass3b5866d2014-06-12 07:24:46 -0600603
Simon Glassc38406e2020-11-05 10:33:43 -0700604 /* First check if any action is needed */
605 do_silent = IS_ENABLED(CONFIG_SILENT_CONSOLE) &&
Simon Glass63660dc2020-11-05 10:33:44 -0700606 !IS_ENABLED(CONFIG_SILENT_U_BOOT_ONLY) && (flags & BOOTM_CL_SILENT);
Simon Glass529e2082020-11-05 10:33:48 -0700607 if (!do_silent && !IS_ENABLED(CONFIG_BOOTARGS_SUBST))
Simon Glassc38406e2020-11-05 10:33:43 -0700608 return 0;
609
610 env = env_get("bootargs");
611 if (env && strlen(env) >= maxlen)
612 return -E2BIG;
613 buf = malloc(maxlen);
614 if (!buf)
615 return -ENOMEM;
616 if (env)
617 strcpy(buf, env);
618 else
619 *buf = '\0';
Simon Glassb4e1b6d2020-11-05 10:33:45 -0700620 ret = bootm_process_cmdline(buf, maxlen, flags);
Simon Glassc38406e2020-11-05 10:33:43 -0700621 if (!ret) {
622 ret = env_set("bootargs", buf);
623
624 /*
625 * If buf is "" and bootargs does not exist, this will produce
626 * an error trying to delete bootargs. Ignore it
627 */
628 if (ret == -ENOENT)
629 ret = 0;
630 }
Simon Glass3b5866d2014-06-12 07:24:46 -0600631 free(buf);
Simon Glassc38406e2020-11-05 10:33:43 -0700632 if (ret)
633 return log_msg_ret("env", ret);
Simon Glass2eab0172020-11-05 10:33:39 -0700634
635 return 0;
Simon Glass3b5866d2014-06-12 07:24:46 -0600636}
Simon Glass3b5866d2014-06-12 07:24:46 -0600637
638/**
639 * Execute selected states of the bootm command.
640 *
641 * Note the arguments to this state must be the first argument, Any 'bootm'
642 * or sub-command arguments must have already been taken.
643 *
644 * Note that if states contains more than one flag it MUST contain
645 * BOOTM_STATE_START, since this handles and consumes the command line args.
646 *
647 * Also note that aside from boot_os_fn functions and bootm_load_os no other
648 * functions we store the return value of in 'ret' may use a negative return
649 * value, without special handling.
650 *
651 * @param cmdtp Pointer to bootm command table entry
652 * @param flag Command flags (CMD_FLAG_...)
653 * @param argc Number of subcommand arguments (0 = no arguments)
654 * @param argv Arguments
655 * @param states Mask containing states to run (BOOTM_STATE_...)
656 * @param images Image header information
657 * @param boot_progress 1 to show boot progress, 0 to not do this
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100658 * Return: 0 if ok, something else on error. Some errors will cause this
Simon Glass3b5866d2014-06-12 07:24:46 -0600659 * function to perform a reboot! If states contains BOOTM_STATE_OS_GO
660 * then the intent is to boot an OS, so this function will not return
661 * unless the image type is standalone.
662 */
Simon Glassed38aef2020-05-10 11:40:03 -0600663int do_bootm_states(struct cmd_tbl *cmdtp, int flag, int argc,
664 char *const argv[], int states, bootm_headers_t *images,
665 int boot_progress)
Simon Glass3b5866d2014-06-12 07:24:46 -0600666{
667 boot_os_fn *boot_fn;
668 ulong iflag = 0;
669 int ret = 0, need_boot_fn;
670
671 images->state |= states;
672
673 /*
674 * Work through the states and see how far we get. We stop on
675 * any error.
676 */
677 if (states & BOOTM_STATE_START)
678 ret = bootm_start(cmdtp, flag, argc, argv);
679
680 if (!ret && (states & BOOTM_STATE_FINDOS))
681 ret = bootm_find_os(cmdtp, flag, argc, argv);
682
Zubair Lutfullah Kakakhel31eb47d2016-09-09 09:18:58 +0100683 if (!ret && (states & BOOTM_STATE_FINDOTHER))
Simon Glass3b5866d2014-06-12 07:24:46 -0600684 ret = bootm_find_other(cmdtp, flag, argc, argv);
Simon Glass3b5866d2014-06-12 07:24:46 -0600685
686 /* Load the OS */
687 if (!ret && (states & BOOTM_STATE_LOADOS)) {
Simon Glass3b5866d2014-06-12 07:24:46 -0600688 iflag = bootm_disable_interrupts();
Tom Rini6c96f5d2018-05-01 12:32:37 -0400689 ret = bootm_load_os(images, 0);
690 if (ret && ret != BOOTM_ERR_OVERLAP)
Simon Glass3b5866d2014-06-12 07:24:46 -0600691 goto err;
692 else if (ret == BOOTM_ERR_OVERLAP)
693 ret = 0;
Simon Glass3b5866d2014-06-12 07:24:46 -0600694 }
695
696 /* Relocate the ramdisk */
697#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
698 if (!ret && (states & BOOTM_STATE_RAMDISK)) {
699 ulong rd_len = images->rd_end - images->rd_start;
700
701 ret = boot_ramdisk_high(&images->lmb, images->rd_start,
702 rd_len, &images->initrd_start, &images->initrd_end);
703 if (!ret) {
Simon Glass4d949a22017-08-03 12:22:10 -0600704 env_set_hex("initrd_start", images->initrd_start);
705 env_set_hex("initrd_end", images->initrd_end);
Simon Glass3b5866d2014-06-12 07:24:46 -0600706 }
707 }
708#endif
Simon Glass85c057e2021-09-25 19:43:21 -0600709#if CONFIG_IS_ENABLED(OF_LIBFDT) && defined(CONFIG_LMB)
Simon Glass3b5866d2014-06-12 07:24:46 -0600710 if (!ret && (states & BOOTM_STATE_FDT)) {
711 boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr);
712 ret = boot_relocate_fdt(&images->lmb, &images->ft_addr,
713 &images->ft_len);
714 }
715#endif
716
717 /* From now on, we need the OS boot function */
718 if (ret)
719 return ret;
720 boot_fn = bootm_os_get_boot_func(images->os.os);
721 need_boot_fn = states & (BOOTM_STATE_OS_CMDLINE |
722 BOOTM_STATE_OS_BD_T | BOOTM_STATE_OS_PREP |
723 BOOTM_STATE_OS_FAKE_GO | BOOTM_STATE_OS_GO);
724 if (boot_fn == NULL && need_boot_fn) {
725 if (iflag)
726 enable_interrupts();
727 printf("ERROR: booting os '%s' (%d) is not supported\n",
728 genimg_get_os_name(images->os.os), images->os.os);
729 bootstage_error(BOOTSTAGE_ID_CHECK_BOOT_OS);
730 return 1;
731 }
732
Hector Palacios8f9cc4c2016-07-11 12:34:37 +0200733
Simon Glass3b5866d2014-06-12 07:24:46 -0600734 /* Call various other states that are not generally used */
735 if (!ret && (states & BOOTM_STATE_OS_CMDLINE))
736 ret = boot_fn(BOOTM_STATE_OS_CMDLINE, argc, argv, images);
737 if (!ret && (states & BOOTM_STATE_OS_BD_T))
738 ret = boot_fn(BOOTM_STATE_OS_BD_T, argc, argv, images);
Hector Palacios8f9cc4c2016-07-11 12:34:37 +0200739 if (!ret && (states & BOOTM_STATE_OS_PREP)) {
Simon Glass529e2082020-11-05 10:33:48 -0700740 ret = bootm_process_cmdline_env(images->os.os == IH_OS_LINUX);
Simon Glassd87002f2020-11-05 10:33:41 -0700741 if (ret) {
742 printf("Cmdline setup failed (err=%d)\n", ret);
743 ret = CMD_RET_FAILURE;
744 goto err;
Simon Glass2eab0172020-11-05 10:33:39 -0700745 }
Simon Glass3b5866d2014-06-12 07:24:46 -0600746 ret = boot_fn(BOOTM_STATE_OS_PREP, argc, argv, images);
Hector Palacios8f9cc4c2016-07-11 12:34:37 +0200747 }
Simon Glass3b5866d2014-06-12 07:24:46 -0600748
749#ifdef CONFIG_TRACE
750 /* Pretend to run the OS, then run a user command */
751 if (!ret && (states & BOOTM_STATE_OS_FAKE_GO)) {
Simon Glass64b723f2017-08-03 12:22:12 -0600752 char *cmd_list = env_get("fakegocmd");
Simon Glass3b5866d2014-06-12 07:24:46 -0600753
754 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_FAKE_GO,
755 images, boot_fn);
756 if (!ret && cmd_list)
757 ret = run_command_list(cmd_list, -1, flag);
758 }
759#endif
760
761 /* Check for unsupported subcommand. */
762 if (ret) {
763 puts("subcommand not supported\n");
764 return ret;
765 }
766
767 /* Now run the OS! We hope this doesn't return */
768 if (!ret && (states & BOOTM_STATE_OS_GO))
769 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_GO,
770 images, boot_fn);
771
772 /* Deal with any fallout */
773err:
774 if (iflag)
775 enable_interrupts();
776
777 if (ret == BOOTM_ERR_UNIMPLEMENTED)
778 bootstage_error(BOOTSTAGE_ID_DECOMP_UNIMPL);
779 else if (ret == BOOTM_ERR_RESET)
780 do_reset(cmdtp, flag, argc, argv);
781
782 return ret;
783}
784
Tom Rinic220bd92019-05-23 07:14:07 -0400785#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Simon Glass3b5866d2014-06-12 07:24:46 -0600786/**
787 * image_get_kernel - verify legacy format kernel image
788 * @img_addr: in RAM address of the legacy format image to be verified
789 * @verify: data CRC verification flag
790 *
791 * image_get_kernel() verifies legacy image integrity and returns pointer to
792 * legacy image header if image verification was completed successfully.
793 *
794 * returns:
795 * pointer to a legacy image header if valid image was found
796 * otherwise return NULL
797 */
798static image_header_t *image_get_kernel(ulong img_addr, int verify)
799{
800 image_header_t *hdr = (image_header_t *)img_addr;
801
802 if (!image_check_magic(hdr)) {
803 puts("Bad Magic Number\n");
804 bootstage_error(BOOTSTAGE_ID_CHECK_MAGIC);
805 return NULL;
806 }
807 bootstage_mark(BOOTSTAGE_ID_CHECK_HEADER);
808
809 if (!image_check_hcrc(hdr)) {
810 puts("Bad Header Checksum\n");
811 bootstage_error(BOOTSTAGE_ID_CHECK_HEADER);
812 return NULL;
813 }
814
815 bootstage_mark(BOOTSTAGE_ID_CHECK_CHECKSUM);
816 image_print_contents(hdr);
817
818 if (verify) {
819 puts(" Verifying Checksum ... ");
820 if (!image_check_dcrc(hdr)) {
821 printf("Bad Data CRC\n");
822 bootstage_error(BOOTSTAGE_ID_CHECK_CHECKSUM);
823 return NULL;
824 }
825 puts("OK\n");
826 }
827 bootstage_mark(BOOTSTAGE_ID_CHECK_ARCH);
828
829 if (!image_check_target_arch(hdr)) {
830 printf("Unsupported Architecture 0x%x\n", image_get_arch(hdr));
831 bootstage_error(BOOTSTAGE_ID_CHECK_ARCH);
832 return NULL;
833 }
834 return hdr;
835}
836#endif
837
838/**
839 * boot_get_kernel - find kernel image
840 * @os_data: pointer to a ulong variable, will hold os data start address
841 * @os_len: pointer to a ulong variable, will hold os data length
842 *
843 * boot_get_kernel() tries to find a kernel image, verifies its integrity
844 * and locates kernel data.
845 *
846 * returns:
847 * pointer to image header if valid image was found, plus kernel start
848 * address and length, otherwise NULL
849 */
Simon Glassed38aef2020-05-10 11:40:03 -0600850static const void *boot_get_kernel(struct cmd_tbl *cmdtp, int flag, int argc,
851 char *const argv[], bootm_headers_t *images,
Simon Glass3b5866d2014-06-12 07:24:46 -0600852 ulong *os_data, ulong *os_len)
853{
Tom Rinic220bd92019-05-23 07:14:07 -0400854#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Simon Glass3b5866d2014-06-12 07:24:46 -0600855 image_header_t *hdr;
856#endif
857 ulong img_addr;
858 const void *buf;
Simon Glass3b5866d2014-06-12 07:24:46 -0600859 const char *fit_uname_config = NULL;
860 const char *fit_uname_kernel = NULL;
Simon Glasse719d3b2021-09-25 19:43:20 -0600861#if CONFIG_IS_ENABLED(FIT)
Simon Glass3b5866d2014-06-12 07:24:46 -0600862 int os_noffset;
863#endif
864
Bryan Wuc82ea372014-08-15 16:51:39 -0700865 img_addr = genimg_get_kernel_addr_fit(argc < 1 ? NULL : argv[0],
866 &fit_uname_config,
867 &fit_uname_kernel);
Simon Glass3b5866d2014-06-12 07:24:46 -0600868
869 bootstage_mark(BOOTSTAGE_ID_CHECK_MAGIC);
870
Simon Glass3b5866d2014-06-12 07:24:46 -0600871 /* check image type, for FIT images get FIT kernel node */
872 *os_data = *os_len = 0;
873 buf = map_sysmem(img_addr, 0);
874 switch (genimg_get_format(buf)) {
Tom Rinic220bd92019-05-23 07:14:07 -0400875#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Simon Glass3b5866d2014-06-12 07:24:46 -0600876 case IMAGE_FORMAT_LEGACY:
877 printf("## Booting kernel from Legacy Image at %08lx ...\n",
878 img_addr);
879 hdr = image_get_kernel(img_addr, images->verify);
880 if (!hdr)
881 return NULL;
882 bootstage_mark(BOOTSTAGE_ID_CHECK_IMAGETYPE);
883
884 /* get os_data and os_len */
885 switch (image_get_type(hdr)) {
886 case IH_TYPE_KERNEL:
887 case IH_TYPE_KERNEL_NOLOAD:
888 *os_data = image_get_data(hdr);
889 *os_len = image_get_data_size(hdr);
890 break;
891 case IH_TYPE_MULTI:
892 image_multi_getimg(hdr, 0, os_data, os_len);
893 break;
894 case IH_TYPE_STANDALONE:
895 *os_data = image_get_data(hdr);
896 *os_len = image_get_data_size(hdr);
897 break;
898 default:
899 printf("Wrong Image Type for %s command\n",
900 cmdtp->name);
901 bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE);
902 return NULL;
903 }
904
905 /*
906 * copy image header to allow for image overwrites during
907 * kernel decompression.
908 */
909 memmove(&images->legacy_hdr_os_copy, hdr,
910 sizeof(image_header_t));
911
912 /* save pointer to image header */
913 images->legacy_hdr_os = hdr;
914
915 images->legacy_hdr_valid = 1;
916 bootstage_mark(BOOTSTAGE_ID_DECOMP_IMAGE);
917 break;
918#endif
Simon Glasse719d3b2021-09-25 19:43:20 -0600919#if CONFIG_IS_ENABLED(FIT)
Simon Glass3b5866d2014-06-12 07:24:46 -0600920 case IMAGE_FORMAT_FIT:
Simon Glassa0c0b632014-06-12 07:24:47 -0600921 os_noffset = fit_image_load(images, img_addr,
Simon Glass3b5866d2014-06-12 07:24:46 -0600922 &fit_uname_kernel, &fit_uname_config,
923 IH_ARCH_DEFAULT, IH_TYPE_KERNEL,
924 BOOTSTAGE_ID_FIT_KERNEL_START,
925 FIT_LOAD_IGNORED, os_data, os_len);
926 if (os_noffset < 0)
927 return NULL;
928
929 images->fit_hdr_os = map_sysmem(img_addr, 0);
930 images->fit_uname_os = fit_uname_kernel;
931 images->fit_uname_cfg = fit_uname_config;
932 images->fit_noffset_os = os_noffset;
933 break;
934#endif
935#ifdef CONFIG_ANDROID_BOOT_IMAGE
936 case IMAGE_FORMAT_ANDROID:
937 printf("## Booting Android Image at 0x%08lx ...\n", img_addr);
Simon Glass95040102014-06-12 07:24:48 -0600938 if (android_image_get_kernel(buf, images->verify,
Simon Glass3b5866d2014-06-12 07:24:46 -0600939 os_data, os_len))
940 return NULL;
941 break;
942#endif
943 default:
944 printf("Wrong Image Format for %s command\n", cmdtp->name);
945 bootstage_error(BOOTSTAGE_ID_FIT_KERNEL_INFO);
946 return NULL;
947 }
948
949 debug(" kernel data at 0x%08lx, len = 0x%08lx (%ld)\n",
950 *os_data, *os_len, *os_len);
951
952 return buf;
953}
Heinrich Schuchardtaa0b11b2019-01-08 18:13:06 +0100954
955/**
956 * switch_to_non_secure_mode() - switch to non-secure mode
957 *
958 * This routine is overridden by architectures requiring this feature.
959 */
960void __weak switch_to_non_secure_mode(void)
961{
962}
963
Simon Glassa51991d2014-06-12 07:24:53 -0600964#else /* USE_HOSTCC */
965
Fabrice Fontained00d6b52019-05-03 22:37:05 +0200966#if defined(CONFIG_FIT_SIGNATURE)
Simon Glass81d13872020-03-18 11:44:02 -0600967static int bootm_host_load_image(const void *fit, int req_image_type,
968 int cfg_noffset)
Simon Glassa51991d2014-06-12 07:24:53 -0600969{
970 const char *fit_uname_config = NULL;
971 ulong data, len;
972 bootm_headers_t images;
973 int noffset;
974 ulong load_end;
975 uint8_t image_type;
976 uint8_t imape_comp;
977 void *load_buf;
978 int ret;
979
Simon Glass81d13872020-03-18 11:44:02 -0600980 fit_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
Simon Glassa51991d2014-06-12 07:24:53 -0600981 memset(&images, '\0', sizeof(images));
982 images.verify = 1;
983 noffset = fit_image_load(&images, (ulong)fit,
984 NULL, &fit_uname_config,
985 IH_ARCH_DEFAULT, req_image_type, -1,
986 FIT_LOAD_IGNORED, &data, &len);
987 if (noffset < 0)
988 return noffset;
989 if (fit_image_get_type(fit, noffset, &image_type)) {
990 puts("Can't get image type!\n");
991 return -EINVAL;
992 }
993
994 if (fit_image_get_comp(fit, noffset, &imape_comp)) {
995 puts("Can't get image compression!\n");
996 return -EINVAL;
997 }
998
999 /* Allow the image to expand by a factor of 4, should be safe */
1000 load_buf = malloc((1 << 20) + len * 4);
Julius Werner47ef9862019-07-24 19:37:54 -07001001 ret = image_decomp(imape_comp, 0, data, image_type, load_buf,
1002 (void *)data, len, CONFIG_SYS_BOOTM_LEN,
1003 &load_end);
Simon Glassa51991d2014-06-12 07:24:53 -06001004 free(load_buf);
Simon Glass5aeccc22014-12-02 13:17:33 -07001005
Julius Werner47ef9862019-07-24 19:37:54 -07001006 if (ret) {
1007 ret = handle_decomp_error(imape_comp, load_end - 0, ret);
1008 if (ret != BOOTM_ERR_UNIMPLEMENTED)
1009 return ret;
1010 }
Simon Glassa51991d2014-06-12 07:24:53 -06001011
1012 return 0;
1013}
1014
1015int bootm_host_load_images(const void *fit, int cfg_noffset)
1016{
1017 static uint8_t image_types[] = {
1018 IH_TYPE_KERNEL,
1019 IH_TYPE_FLATDT,
1020 IH_TYPE_RAMDISK,
1021 };
1022 int err = 0;
1023 int i;
1024
1025 for (i = 0; i < ARRAY_SIZE(image_types); i++) {
1026 int ret;
1027
Simon Glass81d13872020-03-18 11:44:02 -06001028 ret = bootm_host_load_image(fit, image_types[i], cfg_noffset);
Simon Glassa51991d2014-06-12 07:24:53 -06001029 if (!err && ret && ret != -ENOENT)
1030 err = ret;
1031 }
1032
1033 /* Return the first error we found */
1034 return err;
1035}
Fabrice Fontained00d6b52019-05-03 22:37:05 +02001036#endif
Simon Glass0232ba72014-06-12 07:24:51 -06001037
1038#endif /* ndef USE_HOSTCC */