blob: e3233fdf89d7f757a865ca839c59e785b110e745 [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
Simon Glassc38406e2020-11-05 10:33:43 -070036#define MAX_CMDLINE_SIZE SZ_4K
37
Simon Glass3b5866d2014-06-12 07:24:46 -060038#define IH_INITRD_ARCH IH_ARCH_DEFAULT
39
Simon Glass0232ba72014-06-12 07:24:51 -060040#ifndef USE_HOSTCC
41
42DECLARE_GLOBAL_DATA_PTR;
43
Tom Rini36f067f2016-08-12 08:31:15 -040044bootm_headers_t images; /* pointers to os/initrd/fdt images */
45
Simon Glassed38aef2020-05-10 11:40:03 -060046static const void *boot_get_kernel(struct cmd_tbl *cmdtp, int flag, int argc,
47 char *const argv[], bootm_headers_t *images,
Simon Glass3b5866d2014-06-12 07:24:46 -060048 ulong *os_data, ulong *os_len);
49
Simon Glassf55305a2018-05-16 09:42:25 -060050__weak void board_quiesce_devices(void)
51{
52}
53
Simon Glass3b5866d2014-06-12 07:24:46 -060054#ifdef CONFIG_LMB
55static void boot_start_lmb(bootm_headers_t *images)
56{
57 ulong mem_start;
58 phys_size_t mem_size;
59
Simon Glassda1a1342017-08-03 12:22:15 -060060 mem_start = env_get_bootm_low();
61 mem_size = env_get_bootm_size();
Simon Glass3b5866d2014-06-12 07:24:46 -060062
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +010063 lmb_init_and_reserve_range(&images->lmb, (phys_addr_t)mem_start,
64 mem_size, NULL);
Simon Glass3b5866d2014-06-12 07:24:46 -060065}
66#else
67#define lmb_reserve(lmb, base, size)
68static inline void boot_start_lmb(bootm_headers_t *images) { }
69#endif
70
Simon Glassed38aef2020-05-10 11:40:03 -060071static int bootm_start(struct cmd_tbl *cmdtp, int flag, int argc,
72 char *const argv[])
Simon Glass3b5866d2014-06-12 07:24:46 -060073{
74 memset((void *)&images, 0, sizeof(images));
Simon Glass22c34c22017-08-03 12:22:13 -060075 images.verify = env_get_yesno("verify");
Simon Glass3b5866d2014-06-12 07:24:46 -060076
77 boot_start_lmb(&images);
78
79 bootstage_mark_name(BOOTSTAGE_ID_BOOTM_START, "bootm_start");
80 images.state = BOOTM_STATE_START;
81
82 return 0;
83}
84
Philippe Reynesae1f2ca2022-03-28 22:57:00 +020085static ulong bootm_data_addr(int argc, char *const argv[])
86{
87 ulong addr;
88
89 if (argc > 0)
90 addr = simple_strtoul(argv[0], NULL, 16);
91 else
92 addr = image_load_addr;
93
94 return addr;
95}
96
97static int bootm_pre_load(struct cmd_tbl *cmdtp, int flag, int argc,
98 char *const argv[])
99{
100 ulong data_addr = bootm_data_addr(argc, argv);
101 int ret = 0;
102
103 if (CONFIG_IS_ENABLED(CMD_BOOTM_PRE_LOAD))
104 ret = image_pre_load(data_addr);
105
106 if (ret)
107 ret = CMD_RET_FAILURE;
108
109 return ret;
110}
111
Simon Glassed38aef2020-05-10 11:40:03 -0600112static int bootm_find_os(struct cmd_tbl *cmdtp, int flag, int argc,
113 char *const argv[])
Simon Glass3b5866d2014-06-12 07:24:46 -0600114{
115 const void *os_hdr;
116 bool ep_found = false;
Simon Glass0129b522014-10-19 21:11:24 -0600117 int ret;
Simon Glass3b5866d2014-06-12 07:24:46 -0600118
119 /* get kernel image header, start address and length */
120 os_hdr = boot_get_kernel(cmdtp, flag, argc, argv,
121 &images, &images.os.image_start, &images.os.image_len);
122 if (images.os.image_len == 0) {
123 puts("ERROR: can't get kernel image!\n");
124 return 1;
125 }
126
127 /* get image parameters */
128 switch (genimg_get_format(os_hdr)) {
Tom Rinic220bd92019-05-23 07:14:07 -0400129#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Simon Glass3b5866d2014-06-12 07:24:46 -0600130 case IMAGE_FORMAT_LEGACY:
131 images.os.type = image_get_type(os_hdr);
132 images.os.comp = image_get_comp(os_hdr);
133 images.os.os = image_get_os(os_hdr);
134
135 images.os.end = image_get_image_end(os_hdr);
136 images.os.load = image_get_load(os_hdr);
Simon Glass0129b522014-10-19 21:11:24 -0600137 images.os.arch = image_get_arch(os_hdr);
Simon Glass3b5866d2014-06-12 07:24:46 -0600138 break;
139#endif
Simon Glasse719d3b2021-09-25 19:43:20 -0600140#if CONFIG_IS_ENABLED(FIT)
Simon Glass3b5866d2014-06-12 07:24:46 -0600141 case IMAGE_FORMAT_FIT:
142 if (fit_image_get_type(images.fit_hdr_os,
143 images.fit_noffset_os,
144 &images.os.type)) {
145 puts("Can't get image type!\n");
146 bootstage_error(BOOTSTAGE_ID_FIT_TYPE);
147 return 1;
148 }
149
150 if (fit_image_get_comp(images.fit_hdr_os,
151 images.fit_noffset_os,
152 &images.os.comp)) {
153 puts("Can't get image compression!\n");
154 bootstage_error(BOOTSTAGE_ID_FIT_COMPRESSION);
155 return 1;
156 }
157
158 if (fit_image_get_os(images.fit_hdr_os, images.fit_noffset_os,
159 &images.os.os)) {
160 puts("Can't get image OS!\n");
161 bootstage_error(BOOTSTAGE_ID_FIT_OS);
162 return 1;
163 }
164
Simon Glass0129b522014-10-19 21:11:24 -0600165 if (fit_image_get_arch(images.fit_hdr_os,
166 images.fit_noffset_os,
167 &images.os.arch)) {
168 puts("Can't get image ARCH!\n");
169 return 1;
170 }
171
Simon Glass3b5866d2014-06-12 07:24:46 -0600172 images.os.end = fit_get_end(images.fit_hdr_os);
173
174 if (fit_image_get_load(images.fit_hdr_os, images.fit_noffset_os,
175 &images.os.load)) {
176 puts("Can't get image load address!\n");
177 bootstage_error(BOOTSTAGE_ID_FIT_LOADADDR);
178 return 1;
179 }
180 break;
181#endif
182#ifdef CONFIG_ANDROID_BOOT_IMAGE
183 case IMAGE_FORMAT_ANDROID:
184 images.os.type = IH_TYPE_KERNEL;
Eugeniu Rosca1403f392019-04-08 17:35:27 +0200185 images.os.comp = android_image_get_kcomp(os_hdr);
Simon Glass3b5866d2014-06-12 07:24:46 -0600186 images.os.os = IH_OS_LINUX;
Simon Glass3b5866d2014-06-12 07:24:46 -0600187
188 images.os.end = android_image_get_end(os_hdr);
189 images.os.load = android_image_get_kload(os_hdr);
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300190 images.ep = images.os.load;
191 ep_found = true;
Simon Glass3b5866d2014-06-12 07:24:46 -0600192 break;
193#endif
194 default:
195 puts("ERROR: unknown image format type!\n");
196 return 1;
197 }
198
Simon Glass0129b522014-10-19 21:11:24 -0600199 /* If we have a valid setup.bin, we will use that for entry (x86) */
Simon Glass9d428302014-10-10 08:21:57 -0600200 if (images.os.arch == IH_ARCH_I386 ||
201 images.os.arch == IH_ARCH_X86_64) {
Simon Glass0129b522014-10-19 21:11:24 -0600202 ulong len;
203
204 ret = boot_get_setup(&images, IH_ARCH_I386, &images.ep, &len);
205 if (ret < 0 && ret != -ENOENT) {
206 puts("Could not find a valid setup.bin for x86\n");
207 return 1;
208 }
209 /* Kernel entry point is the setup.bin */
210 } else if (images.legacy_hdr_valid) {
Simon Glass3b5866d2014-06-12 07:24:46 -0600211 images.ep = image_get_ep(&images.legacy_hdr_os_copy);
Simon Glasse719d3b2021-09-25 19:43:20 -0600212#if CONFIG_IS_ENABLED(FIT)
Simon Glass3b5866d2014-06-12 07:24:46 -0600213 } else if (images.fit_uname_os) {
214 int ret;
215
216 ret = fit_image_get_entry(images.fit_hdr_os,
217 images.fit_noffset_os, &images.ep);
218 if (ret) {
219 puts("Can't get entry point property!\n");
220 return 1;
221 }
222#endif
223 } else if (!ep_found) {
224 puts("Could not find kernel entry point!\n");
225 return 1;
226 }
227
228 if (images.os.type == IH_TYPE_KERNEL_NOLOAD) {
Marek Vasut21425792018-06-13 06:13:33 +0200229 if (CONFIG_IS_ENABLED(CMD_BOOTI) &&
230 images.os.arch == IH_ARCH_ARM64) {
231 ulong image_addr;
232 ulong image_size;
233
234 ret = booti_setup(images.os.image_start, &image_addr,
235 &image_size, true);
236 if (ret != 0)
237 return 1;
238
239 images.os.type = IH_TYPE_KERNEL;
240 images.os.load = image_addr;
241 images.ep = image_addr;
242 } else {
243 images.os.load = images.os.image_start;
244 images.ep += images.os.image_start;
245 }
Simon Glass3b5866d2014-06-12 07:24:46 -0600246 }
247
Simon Glass5b539a02016-02-24 09:14:42 -0700248 images.os.start = map_to_sysmem(os_hdr);
Simon Glass3b5866d2014-06-12 07:24:46 -0600249
250 return 0;
251}
252
Karl Apsited686ce92015-05-21 09:52:49 -0400253/**
254 * bootm_find_images - wrapper to find and locate various images
255 * @flag: Ignored Argument
256 * @argc: command argument count
257 * @argv: command argument list
Tero Kristo06086942020-06-12 15:41:20 +0300258 * @start: OS image start address
259 * @size: OS image size
Karl Apsited686ce92015-05-21 09:52:49 -0400260 *
261 * boot_find_images() will attempt to load an available ramdisk,
262 * flattened device tree, as well as specifically marked
263 * "loadable" images (loadables are FIT only)
264 *
265 * Note: bootm_find_images will skip an image if it is not found
266 *
267 * @return:
268 * 0, if all existing images were loaded correctly
269 * 1, if an image is found but corrupted, or invalid
270 */
Tero Kristo06086942020-06-12 15:41:20 +0300271int bootm_find_images(int flag, int argc, char *const argv[], ulong start,
272 ulong size)
Simon Glass3b5866d2014-06-12 07:24:46 -0600273{
274 int ret;
275
276 /* find ramdisk */
277 ret = boot_get_ramdisk(argc, argv, &images, IH_INITRD_ARCH,
278 &images.rd_start, &images.rd_end);
279 if (ret) {
280 puts("Ramdisk image is corrupt or invalid\n");
281 return 1;
282 }
283
Tero Kristo06086942020-06-12 15:41:20 +0300284 /* check if ramdisk overlaps OS image */
285 if (images.rd_start && (((ulong)images.rd_start >= start &&
Jaehoon Chung80ffb812020-10-21 14:17:03 +0900286 (ulong)images.rd_start < start + size) ||
287 ((ulong)images.rd_end > start &&
288 (ulong)images.rd_end <= start + size) ||
289 ((ulong)images.rd_start < start &&
290 (ulong)images.rd_end >= start + size))) {
Tero Kristo06086942020-06-12 15:41:20 +0300291 printf("ERROR: RD image overlaps OS image (OS=0x%lx..0x%lx)\n",
292 start, start + size);
293 return 1;
294 }
295
Simon Glass85c057e2021-09-25 19:43:21 -0600296#if CONFIG_IS_ENABLED(OF_LIBFDT)
Simon Glass3b5866d2014-06-12 07:24:46 -0600297 /* find flattened device tree */
298 ret = boot_get_fdt(flag, argc, argv, IH_ARCH_DEFAULT, &images,
299 &images.ft_addr, &images.ft_len);
300 if (ret) {
301 puts("Could not find a valid device tree\n");
302 return 1;
303 }
Tero Kristo06086942020-06-12 15:41:20 +0300304
305 /* check if FDT overlaps OS image */
306 if (images.ft_addr &&
307 (((ulong)images.ft_addr >= start &&
Pali Rohár779a7b692022-08-27 14:48:10 +0200308 (ulong)images.ft_addr < start + size) ||
Tero Kristo06086942020-06-12 15:41:20 +0300309 ((ulong)images.ft_addr + images.ft_len >= start &&
Pali Rohár779a7b692022-08-27 14:48:10 +0200310 (ulong)images.ft_addr + images.ft_len < start + size))) {
Tero Kristo06086942020-06-12 15:41:20 +0300311 printf("ERROR: FDT image overlaps OS image (OS=0x%lx..0x%lx)\n",
312 start, start + size);
313 return 1;
314 }
315
Simon Goldschmidt6127ac02018-12-17 20:14:42 +0100316 if (CONFIG_IS_ENABLED(CMD_FDT))
317 set_working_fdt_addr(map_to_sysmem(images.ft_addr));
Simon Glass3b5866d2014-06-12 07:24:46 -0600318#endif
319
Simon Glasse719d3b2021-09-25 19:43:20 -0600320#if CONFIG_IS_ENABLED(FIT)
Simon Glassbc0c4db2021-09-25 07:03:20 -0600321 if (IS_ENABLED(CONFIG_FPGA)) {
322 /* find bitstreams */
323 ret = boot_get_fpga(argc, argv, &images, IH_ARCH_DEFAULT,
324 NULL, NULL);
325 if (ret) {
326 printf("FPGA image is corrupted or invalid\n");
327 return 1;
328 }
Michal Simekebae78b2016-05-17 14:03:50 +0200329 }
Michal Simekebae78b2016-05-17 14:03:50 +0200330
Karl Apsite1b21c282015-05-21 09:52:48 -0400331 /* find all of the loadables */
332 ret = boot_get_loadable(argc, argv, &images, IH_ARCH_DEFAULT,
333 NULL, NULL);
334 if (ret) {
335 printf("Loadable(s) is corrupt or invalid\n");
336 return 1;
337 }
Karl Apsite1b21c282015-05-21 09:52:48 -0400338#endif
339
Simon Glass3b5866d2014-06-12 07:24:46 -0600340 return 0;
341}
342
Simon Glassed38aef2020-05-10 11:40:03 -0600343static int bootm_find_other(struct cmd_tbl *cmdtp, int flag, int argc,
344 char *const argv[])
Simon Glass3b5866d2014-06-12 07:24:46 -0600345{
346 if (((images.os.type == IH_TYPE_KERNEL) ||
347 (images.os.type == IH_TYPE_KERNEL_NOLOAD) ||
348 (images.os.type == IH_TYPE_MULTI)) &&
349 (images.os.os == IH_OS_LINUX ||
350 images.os.os == IH_OS_VXWORKS))
Tero Kristo06086942020-06-12 15:41:20 +0300351 return bootm_find_images(flag, argc, argv, 0, 0);
Simon Glass3b5866d2014-06-12 07:24:46 -0600352
353 return 0;
354}
Simon Glass10ae4192014-12-02 13:17:30 -0700355#endif /* USE_HOSTC */
356
Julius Werner47ef9862019-07-24 19:37:54 -0700357#if !defined(USE_HOSTCC) || defined(CONFIG_FIT_SIGNATURE)
Simon Glass4f6e6d72014-12-02 13:17:37 -0700358/**
359 * handle_decomp_error() - display a decompression error
360 *
361 * This function tries to produce a useful message. In the case where the
362 * uncompressed size is the same as the available space, we can assume that
363 * the image is too large for the buffer.
364 *
365 * @comp_type: Compression type being used (IH_COMP_...)
366 * @uncomp_size: Number of bytes uncompressed
Tom Rinif3c2f992022-06-25 19:29:46 -0400367 * @buf_size: Number of bytes the decompresion buffer was
Julius Werner47ef9862019-07-24 19:37:54 -0700368 * @ret: errno error code received from compression library
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100369 * Return: Appropriate BOOTM_ERR_ error code
Simon Glass4f6e6d72014-12-02 13:17:37 -0700370 */
Tom Rinif3c2f992022-06-25 19:29:46 -0400371static int handle_decomp_error(int comp_type, size_t uncomp_size,
372 size_t buf_size, int ret)
Simon Glass10ae4192014-12-02 13:17:30 -0700373{
Simon Glass4f6e6d72014-12-02 13:17:37 -0700374 const char *name = genimg_get_comp_name(comp_type);
375
Julius Werner47ef9862019-07-24 19:37:54 -0700376 /* ENOSYS means unimplemented compression type, don't reset. */
377 if (ret == -ENOSYS)
378 return BOOTM_ERR_UNIMPLEMENTED;
379
Tom Rinif3c2f992022-06-25 19:29:46 -0400380 if (uncomp_size >= buf_size)
Simon Glass4f6e6d72014-12-02 13:17:37 -0700381 printf("Image too large: increase CONFIG_SYS_BOOTM_LEN\n");
Simon Glass10ae4192014-12-02 13:17:30 -0700382 else
Simon Glass4f6e6d72014-12-02 13:17:37 -0700383 printf("%s: uncompress error %d\n", name, ret);
384
385 /*
386 * The decompression routines are now safe, so will not write beyond
387 * their bounds. Probably it is not necessary to reset, but maintain
388 * the current behaviour for now.
389 */
390 printf("Must RESET board to recover\n");
Simon Glass10ae4192014-12-02 13:17:30 -0700391#ifndef USE_HOSTCC
392 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
393#endif
394
395 return BOOTM_ERR_RESET;
396}
Julius Werner47ef9862019-07-24 19:37:54 -0700397#endif
Simon Glass686f3bb2014-06-12 07:24:52 -0600398
Simon Glassa51991d2014-06-12 07:24:53 -0600399#ifndef USE_HOSTCC
Tom Rini6c96f5d2018-05-01 12:32:37 -0400400static int bootm_load_os(bootm_headers_t *images, int boot_progress)
Simon Glass686f3bb2014-06-12 07:24:52 -0600401{
402 image_info_t os = images->os;
403 ulong load = os.load;
Tom Rini6c96f5d2018-05-01 12:32:37 -0400404 ulong load_end;
Simon Glass686f3bb2014-06-12 07:24:52 -0600405 ulong blob_start = os.start;
406 ulong blob_end = os.end;
407 ulong image_start = os.image_start;
408 ulong image_len = os.image_len;
Bryan O'Donoghue600388e2018-04-15 11:48:17 +0100409 ulong flush_start = ALIGN_DOWN(load, ARCH_DMA_MINALIGN);
Simon Glass686f3bb2014-06-12 07:24:52 -0600410 bool no_overlap;
411 void *load_buf, *image_buf;
412 int err;
413
414 load_buf = map_sysmem(load, 0);
415 image_buf = map_sysmem(os.image_start, image_len);
Julius Werner47ef9862019-07-24 19:37:54 -0700416 err = image_decomp(os.comp, load, os.image_start, os.type,
417 load_buf, image_buf, image_len,
418 CONFIG_SYS_BOOTM_LEN, &load_end);
Simon Glass686f3bb2014-06-12 07:24:52 -0600419 if (err) {
Tom Rinif3c2f992022-06-25 19:29:46 -0400420 err = handle_decomp_error(os.comp, load_end - load,
421 CONFIG_SYS_BOOTM_LEN, err);
Simon Glass686f3bb2014-06-12 07:24:52 -0600422 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
423 return err;
424 }
Heinrich Schuchardt6d29e0d2020-08-30 11:34:12 +0200425 /* We need the decompressed image size in the next steps */
426 images->os.image_len = load_end - load;
Bryan O'Donoghue600388e2018-04-15 11:48:17 +0100427
Trent Piepho92e51d02019-03-27 23:50:09 +0000428 flush_cache(flush_start, ALIGN(load_end, ARCH_DMA_MINALIGN) - flush_start);
Simon Glass3b5866d2014-06-12 07:24:46 -0600429
Tom Rini6c96f5d2018-05-01 12:32:37 -0400430 debug(" kernel loaded at 0x%08lx, end = 0x%08lx\n", load, load_end);
Simon Glass3b5866d2014-06-12 07:24:46 -0600431 bootstage_mark(BOOTSTAGE_ID_KERNEL_LOADED);
432
Simon Glass686f3bb2014-06-12 07:24:52 -0600433 no_overlap = (os.comp == IH_COMP_NONE && load == image_start);
434
Tom Rini6c96f5d2018-05-01 12:32:37 -0400435 if (!no_overlap && load < blob_end && load_end > blob_start) {
Simon Glass3b5866d2014-06-12 07:24:46 -0600436 debug("images.os.start = 0x%lX, images.os.end = 0x%lx\n",
437 blob_start, blob_end);
438 debug("images.os.load = 0x%lx, load_end = 0x%lx\n", load,
Tom Rini6c96f5d2018-05-01 12:32:37 -0400439 load_end);
Simon Glass3b5866d2014-06-12 07:24:46 -0600440
441 /* Check what type of image this is. */
442 if (images->legacy_hdr_valid) {
443 if (image_get_type(&images->legacy_hdr_os_copy)
444 == IH_TYPE_MULTI)
445 puts("WARNING: legacy format multi component image overwritten\n");
446 return BOOTM_ERR_OVERLAP;
447 } else {
448 puts("ERROR: new format image overwritten - must RESET the board to recover\n");
449 bootstage_error(BOOTSTAGE_ID_OVERWRITTEN);
450 return BOOTM_ERR_RESET;
451 }
452 }
453
Tom Rini6c96f5d2018-05-01 12:32:37 -0400454 lmb_reserve(&images->lmb, images->os.load, (load_end -
455 images->os.load));
Simon Glass3b5866d2014-06-12 07:24:46 -0600456 return 0;
457}
458
459/**
460 * bootm_disable_interrupts() - Disable interrupts in preparation for load/boot
461 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100462 * Return: interrupt flag (0 if interrupts were disabled, non-zero if they were
Simon Glass3b5866d2014-06-12 07:24:46 -0600463 * enabled)
464 */
465ulong bootm_disable_interrupts(void)
466{
467 ulong iflag;
468
469 /*
470 * We have reached the point of no return: we are going to
471 * overwrite all exception vector code, so we cannot easily
472 * recover from any failures any more...
473 */
474 iflag = disable_interrupts();
475#ifdef CONFIG_NETCONSOLE
476 /* Stop the ethernet stack if NetConsole could have left it up */
477 eth_halt();
Bernhard Nortmann1167a6c2015-09-14 15:29:45 +0200478# ifndef CONFIG_DM_ETH
Simon Glass3b5866d2014-06-12 07:24:46 -0600479 eth_unregister(eth_get_dev());
Bernhard Nortmann1167a6c2015-09-14 15:29:45 +0200480# endif
Simon Glass3b5866d2014-06-12 07:24:46 -0600481#endif
482
483#if defined(CONFIG_CMD_USB)
484 /*
485 * turn off USB to prevent the host controller from writing to the
486 * SDRAM while Linux is booting. This could happen (at least for OHCI
487 * controller), because the HCCA (Host Controller Communication Area)
488 * lies within the SDRAM and the host controller writes continously to
489 * this area (as busmaster!). The HccaFrameNumber is for example
490 * updated every 1 ms within the HCCA structure in SDRAM! For more
491 * details see the OpenHCI specification.
492 */
493 usb_stop();
494#endif
495 return iflag;
496}
497
Simon Glass7a8a4292020-11-05 10:33:42 -0700498#define CONSOLE_ARG "console="
Sean Anderson15bdcd42022-05-19 18:26:05 -0400499#define NULL_CONSOLE (CONSOLE_ARG "ttynull")
500#define CONSOLE_ARG_SIZE sizeof(NULL_CONSOLE)
Simon Glass3b5866d2014-06-12 07:24:46 -0600501
Simon Glassc38406e2020-11-05 10:33:43 -0700502/**
503 * fixup_silent_linux() - Handle silencing the linux boot if required
504 *
505 * This uses the silent_linux envvar to control whether to add/set a "console="
506 * parameter to the command line
507 *
508 * @buf: Buffer containing the string to process
509 * @maxlen: Maximum length of buffer
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100510 * Return: 0 if OK, -ENOSPC if @maxlen is too small
Simon Glassc38406e2020-11-05 10:33:43 -0700511 */
512static int fixup_silent_linux(char *buf, int maxlen)
Simon Glass3b5866d2014-06-12 07:24:46 -0600513{
Simon Glass3b5866d2014-06-12 07:24:46 -0600514 int want_silent;
Simon Glassc38406e2020-11-05 10:33:43 -0700515 char *cmdline;
516 int size;
Simon Glass07a88862020-11-05 10:33:38 -0700517
Simon Glass3b5866d2014-06-12 07:24:46 -0600518 /*
Simon Glassc38406e2020-11-05 10:33:43 -0700519 * Move the input string to the end of buffer. The output string will be
520 * built up at the start.
521 */
522 size = strlen(buf) + 1;
523 if (size * 2 > maxlen)
524 return -ENOSPC;
525 cmdline = buf + maxlen - size;
526 memmove(cmdline, buf, size);
527 /*
Simon Glass3b5866d2014-06-12 07:24:46 -0600528 * Only fix cmdline when requested. The environment variable can be:
529 *
530 * no - we never fixup
531 * yes - we always fixup
532 * unset - we rely on the console silent flag
533 */
Simon Glass22c34c22017-08-03 12:22:13 -0600534 want_silent = env_get_yesno("silent_linux");
Simon Glass3b5866d2014-06-12 07:24:46 -0600535 if (want_silent == 0)
Simon Glass2eab0172020-11-05 10:33:39 -0700536 return 0;
Simon Glass3b5866d2014-06-12 07:24:46 -0600537 else if (want_silent == -1 && !(gd->flags & GD_FLG_SILENT))
Simon Glass2eab0172020-11-05 10:33:39 -0700538 return 0;
Simon Glass3b5866d2014-06-12 07:24:46 -0600539
540 debug("before silent fix-up: %s\n", cmdline);
Simon Glassc38406e2020-11-05 10:33:43 -0700541 if (*cmdline) {
Simon Glass3b5866d2014-06-12 07:24:46 -0600542 char *start = strstr(cmdline, CONSOLE_ARG);
543
Simon Glassc38406e2020-11-05 10:33:43 -0700544 /* Check space for maximum possible new command line */
545 if (size + CONSOLE_ARG_SIZE > maxlen)
Simon Glass2eab0172020-11-05 10:33:39 -0700546 return -ENOSPC;
Simon Glass3b5866d2014-06-12 07:24:46 -0600547
548 if (start) {
549 char *end = strchr(start, ' ');
Simon Glass7a8a4292020-11-05 10:33:42 -0700550 int start_bytes;
Simon Glass3b5866d2014-06-12 07:24:46 -0600551
Sean Anderson15bdcd42022-05-19 18:26:05 -0400552 start_bytes = start - cmdline;
Simon Glass7a8a4292020-11-05 10:33:42 -0700553 strncpy(buf, cmdline, start_bytes);
Sean Anderson15bdcd42022-05-19 18:26:05 -0400554 strncpy(buf + start_bytes, NULL_CONSOLE, CONSOLE_ARG_SIZE);
Simon Glass3b5866d2014-06-12 07:24:46 -0600555 if (end)
Sean Anderson15bdcd42022-05-19 18:26:05 -0400556 strcpy(buf + start_bytes + CONSOLE_ARG_SIZE - 1, end);
Simon Glass3b5866d2014-06-12 07:24:46 -0600557 else
Sean Anderson15bdcd42022-05-19 18:26:05 -0400558 buf[start_bytes + CONSOLE_ARG_SIZE] = '\0';
Simon Glass3b5866d2014-06-12 07:24:46 -0600559 } else {
Sean Anderson15bdcd42022-05-19 18:26:05 -0400560 sprintf(buf, "%s %s", cmdline, NULL_CONSOLE);
Simon Glass3b5866d2014-06-12 07:24:46 -0600561 }
Simon Glassc38406e2020-11-05 10:33:43 -0700562 if (buf + strlen(buf) >= cmdline)
563 return -ENOSPC;
Simon Glass3b5866d2014-06-12 07:24:46 -0600564 } else {
Sean Anderson15bdcd42022-05-19 18:26:05 -0400565 if (maxlen < CONSOLE_ARG_SIZE)
Simon Glassc38406e2020-11-05 10:33:43 -0700566 return -ENOSPC;
Sean Anderson15bdcd42022-05-19 18:26:05 -0400567 strcpy(buf, NULL_CONSOLE);
Simon Glass3b5866d2014-06-12 07:24:46 -0600568 }
Simon Glassc38406e2020-11-05 10:33:43 -0700569 debug("after silent fix-up: %s\n", buf);
570
571 return 0;
572}
573
Simon Glass529e2082020-11-05 10:33:48 -0700574/**
575 * process_subst() - Handle substitution of ${...} fields in the environment
576 *
577 * Handle variable substitution in the provided buffer
578 *
579 * @buf: Buffer containing the string to process
580 * @maxlen: Maximum length of buffer
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100581 * Return: 0 if OK, -ENOSPC if @maxlen is too small
Simon Glass529e2082020-11-05 10:33:48 -0700582 */
583static int process_subst(char *buf, int maxlen)
584{
585 char *cmdline;
586 int size;
587 int ret;
588
589 /* Move to end of buffer */
590 size = strlen(buf) + 1;
591 cmdline = buf + maxlen - size;
592 if (buf + size > cmdline)
593 return -ENOSPC;
594 memmove(cmdline, buf, size);
595
596 ret = cli_simple_process_macros(cmdline, buf, cmdline - buf);
597
598 return ret;
599}
600
Simon Glassb4e1b6d2020-11-05 10:33:45 -0700601int bootm_process_cmdline(char *buf, int maxlen, int flags)
602{
603 int ret;
604
605 /* Check config first to enable compiler to eliminate code */
606 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) &&
607 !IS_ENABLED(CONFIG_SILENT_U_BOOT_ONLY) &&
608 (flags & BOOTM_CL_SILENT)) {
609 ret = fixup_silent_linux(buf, maxlen);
610 if (ret)
611 return log_msg_ret("silent", ret);
612 }
Simon Glassed8603d2021-03-15 18:11:23 +1300613 if (IS_ENABLED(CONFIG_BOOTARGS_SUBST) && IS_ENABLED(CONFIG_CMDLINE) &&
614 (flags & BOOTM_CL_SUBST)) {
Simon Glass529e2082020-11-05 10:33:48 -0700615 ret = process_subst(buf, maxlen);
616 if (ret)
Simon Glasscac744e2021-02-06 09:57:35 -0700617 return log_msg_ret("subst", ret);
Simon Glass529e2082020-11-05 10:33:48 -0700618 }
Simon Glassb4e1b6d2020-11-05 10:33:45 -0700619
620 return 0;
621}
622
Simon Glass63660dc2020-11-05 10:33:44 -0700623int bootm_process_cmdline_env(int flags)
Simon Glassc38406e2020-11-05 10:33:43 -0700624{
625 const int maxlen = MAX_CMDLINE_SIZE;
Simon Glass63660dc2020-11-05 10:33:44 -0700626 bool do_silent;
Simon Glassc38406e2020-11-05 10:33:43 -0700627 const char *env;
628 char *buf;
629 int ret;
Simon Glass3b5866d2014-06-12 07:24:46 -0600630
Simon Glassc38406e2020-11-05 10:33:43 -0700631 /* First check if any action is needed */
632 do_silent = IS_ENABLED(CONFIG_SILENT_CONSOLE) &&
Simon Glass63660dc2020-11-05 10:33:44 -0700633 !IS_ENABLED(CONFIG_SILENT_U_BOOT_ONLY) && (flags & BOOTM_CL_SILENT);
Simon Glass529e2082020-11-05 10:33:48 -0700634 if (!do_silent && !IS_ENABLED(CONFIG_BOOTARGS_SUBST))
Simon Glassc38406e2020-11-05 10:33:43 -0700635 return 0;
636
637 env = env_get("bootargs");
638 if (env && strlen(env) >= maxlen)
639 return -E2BIG;
640 buf = malloc(maxlen);
641 if (!buf)
642 return -ENOMEM;
643 if (env)
644 strcpy(buf, env);
645 else
646 *buf = '\0';
Simon Glassb4e1b6d2020-11-05 10:33:45 -0700647 ret = bootm_process_cmdline(buf, maxlen, flags);
Simon Glassc38406e2020-11-05 10:33:43 -0700648 if (!ret) {
649 ret = env_set("bootargs", buf);
650
651 /*
652 * If buf is "" and bootargs does not exist, this will produce
653 * an error trying to delete bootargs. Ignore it
654 */
655 if (ret == -ENOENT)
656 ret = 0;
657 }
Simon Glass3b5866d2014-06-12 07:24:46 -0600658 free(buf);
Simon Glassc38406e2020-11-05 10:33:43 -0700659 if (ret)
660 return log_msg_ret("env", ret);
Simon Glass2eab0172020-11-05 10:33:39 -0700661
662 return 0;
Simon Glass3b5866d2014-06-12 07:24:46 -0600663}
Simon Glass3b5866d2014-06-12 07:24:46 -0600664
665/**
666 * Execute selected states of the bootm command.
667 *
668 * Note the arguments to this state must be the first argument, Any 'bootm'
669 * or sub-command arguments must have already been taken.
670 *
671 * Note that if states contains more than one flag it MUST contain
672 * BOOTM_STATE_START, since this handles and consumes the command line args.
673 *
674 * Also note that aside from boot_os_fn functions and bootm_load_os no other
675 * functions we store the return value of in 'ret' may use a negative return
676 * value, without special handling.
677 *
678 * @param cmdtp Pointer to bootm command table entry
679 * @param flag Command flags (CMD_FLAG_...)
680 * @param argc Number of subcommand arguments (0 = no arguments)
681 * @param argv Arguments
682 * @param states Mask containing states to run (BOOTM_STATE_...)
683 * @param images Image header information
684 * @param boot_progress 1 to show boot progress, 0 to not do this
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100685 * Return: 0 if ok, something else on error. Some errors will cause this
Simon Glass3b5866d2014-06-12 07:24:46 -0600686 * function to perform a reboot! If states contains BOOTM_STATE_OS_GO
687 * then the intent is to boot an OS, so this function will not return
688 * unless the image type is standalone.
689 */
Simon Glassed38aef2020-05-10 11:40:03 -0600690int do_bootm_states(struct cmd_tbl *cmdtp, int flag, int argc,
691 char *const argv[], int states, bootm_headers_t *images,
692 int boot_progress)
Simon Glass3b5866d2014-06-12 07:24:46 -0600693{
694 boot_os_fn *boot_fn;
695 ulong iflag = 0;
696 int ret = 0, need_boot_fn;
697
698 images->state |= states;
699
700 /*
701 * Work through the states and see how far we get. We stop on
702 * any error.
703 */
704 if (states & BOOTM_STATE_START)
705 ret = bootm_start(cmdtp, flag, argc, argv);
706
Philippe Reynesae1f2ca2022-03-28 22:57:00 +0200707 if (!ret && (states & BOOTM_STATE_PRE_LOAD))
708 ret = bootm_pre_load(cmdtp, flag, argc, argv);
709
Simon Glass3b5866d2014-06-12 07:24:46 -0600710 if (!ret && (states & BOOTM_STATE_FINDOS))
711 ret = bootm_find_os(cmdtp, flag, argc, argv);
712
Zubair Lutfullah Kakakhel31eb47d2016-09-09 09:18:58 +0100713 if (!ret && (states & BOOTM_STATE_FINDOTHER))
Simon Glass3b5866d2014-06-12 07:24:46 -0600714 ret = bootm_find_other(cmdtp, flag, argc, argv);
Simon Glass3b5866d2014-06-12 07:24:46 -0600715
716 /* Load the OS */
717 if (!ret && (states & BOOTM_STATE_LOADOS)) {
Simon Glass3b5866d2014-06-12 07:24:46 -0600718 iflag = bootm_disable_interrupts();
Tom Rini6c96f5d2018-05-01 12:32:37 -0400719 ret = bootm_load_os(images, 0);
720 if (ret && ret != BOOTM_ERR_OVERLAP)
Simon Glass3b5866d2014-06-12 07:24:46 -0600721 goto err;
722 else if (ret == BOOTM_ERR_OVERLAP)
723 ret = 0;
Simon Glass3b5866d2014-06-12 07:24:46 -0600724 }
725
726 /* Relocate the ramdisk */
727#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
728 if (!ret && (states & BOOTM_STATE_RAMDISK)) {
729 ulong rd_len = images->rd_end - images->rd_start;
730
731 ret = boot_ramdisk_high(&images->lmb, images->rd_start,
732 rd_len, &images->initrd_start, &images->initrd_end);
733 if (!ret) {
Simon Glass4d949a22017-08-03 12:22:10 -0600734 env_set_hex("initrd_start", images->initrd_start);
735 env_set_hex("initrd_end", images->initrd_end);
Simon Glass3b5866d2014-06-12 07:24:46 -0600736 }
737 }
738#endif
Simon Glass85c057e2021-09-25 19:43:21 -0600739#if CONFIG_IS_ENABLED(OF_LIBFDT) && defined(CONFIG_LMB)
Simon Glass3b5866d2014-06-12 07:24:46 -0600740 if (!ret && (states & BOOTM_STATE_FDT)) {
741 boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr);
742 ret = boot_relocate_fdt(&images->lmb, &images->ft_addr,
743 &images->ft_len);
744 }
745#endif
746
747 /* From now on, we need the OS boot function */
748 if (ret)
749 return ret;
750 boot_fn = bootm_os_get_boot_func(images->os.os);
751 need_boot_fn = states & (BOOTM_STATE_OS_CMDLINE |
752 BOOTM_STATE_OS_BD_T | BOOTM_STATE_OS_PREP |
753 BOOTM_STATE_OS_FAKE_GO | BOOTM_STATE_OS_GO);
754 if (boot_fn == NULL && need_boot_fn) {
755 if (iflag)
756 enable_interrupts();
757 printf("ERROR: booting os '%s' (%d) is not supported\n",
758 genimg_get_os_name(images->os.os), images->os.os);
759 bootstage_error(BOOTSTAGE_ID_CHECK_BOOT_OS);
760 return 1;
761 }
762
Hector Palacios8f9cc4c2016-07-11 12:34:37 +0200763
Simon Glass3b5866d2014-06-12 07:24:46 -0600764 /* Call various other states that are not generally used */
765 if (!ret && (states & BOOTM_STATE_OS_CMDLINE))
766 ret = boot_fn(BOOTM_STATE_OS_CMDLINE, argc, argv, images);
767 if (!ret && (states & BOOTM_STATE_OS_BD_T))
768 ret = boot_fn(BOOTM_STATE_OS_BD_T, argc, argv, images);
Hector Palacios8f9cc4c2016-07-11 12:34:37 +0200769 if (!ret && (states & BOOTM_STATE_OS_PREP)) {
Simon Glass529e2082020-11-05 10:33:48 -0700770 ret = bootm_process_cmdline_env(images->os.os == IH_OS_LINUX);
Simon Glassd87002f2020-11-05 10:33:41 -0700771 if (ret) {
772 printf("Cmdline setup failed (err=%d)\n", ret);
773 ret = CMD_RET_FAILURE;
774 goto err;
Simon Glass2eab0172020-11-05 10:33:39 -0700775 }
Simon Glass3b5866d2014-06-12 07:24:46 -0600776 ret = boot_fn(BOOTM_STATE_OS_PREP, argc, argv, images);
Hector Palacios8f9cc4c2016-07-11 12:34:37 +0200777 }
Simon Glass3b5866d2014-06-12 07:24:46 -0600778
779#ifdef CONFIG_TRACE
780 /* Pretend to run the OS, then run a user command */
781 if (!ret && (states & BOOTM_STATE_OS_FAKE_GO)) {
Simon Glass64b723f2017-08-03 12:22:12 -0600782 char *cmd_list = env_get("fakegocmd");
Simon Glass3b5866d2014-06-12 07:24:46 -0600783
784 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_FAKE_GO,
785 images, boot_fn);
786 if (!ret && cmd_list)
787 ret = run_command_list(cmd_list, -1, flag);
788 }
789#endif
790
791 /* Check for unsupported subcommand. */
792 if (ret) {
793 puts("subcommand not supported\n");
794 return ret;
795 }
796
797 /* Now run the OS! We hope this doesn't return */
798 if (!ret && (states & BOOTM_STATE_OS_GO))
799 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_GO,
800 images, boot_fn);
801
802 /* Deal with any fallout */
803err:
804 if (iflag)
805 enable_interrupts();
806
807 if (ret == BOOTM_ERR_UNIMPLEMENTED)
808 bootstage_error(BOOTSTAGE_ID_DECOMP_UNIMPL);
809 else if (ret == BOOTM_ERR_RESET)
810 do_reset(cmdtp, flag, argc, argv);
811
812 return ret;
813}
814
Tom Rinic220bd92019-05-23 07:14:07 -0400815#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Simon Glass3b5866d2014-06-12 07:24:46 -0600816/**
817 * image_get_kernel - verify legacy format kernel image
818 * @img_addr: in RAM address of the legacy format image to be verified
819 * @verify: data CRC verification flag
820 *
821 * image_get_kernel() verifies legacy image integrity and returns pointer to
822 * legacy image header if image verification was completed successfully.
823 *
824 * returns:
825 * pointer to a legacy image header if valid image was found
826 * otherwise return NULL
827 */
828static image_header_t *image_get_kernel(ulong img_addr, int verify)
829{
830 image_header_t *hdr = (image_header_t *)img_addr;
831
832 if (!image_check_magic(hdr)) {
833 puts("Bad Magic Number\n");
834 bootstage_error(BOOTSTAGE_ID_CHECK_MAGIC);
835 return NULL;
836 }
837 bootstage_mark(BOOTSTAGE_ID_CHECK_HEADER);
838
839 if (!image_check_hcrc(hdr)) {
840 puts("Bad Header Checksum\n");
841 bootstage_error(BOOTSTAGE_ID_CHECK_HEADER);
842 return NULL;
843 }
844
845 bootstage_mark(BOOTSTAGE_ID_CHECK_CHECKSUM);
846 image_print_contents(hdr);
847
848 if (verify) {
849 puts(" Verifying Checksum ... ");
850 if (!image_check_dcrc(hdr)) {
851 printf("Bad Data CRC\n");
852 bootstage_error(BOOTSTAGE_ID_CHECK_CHECKSUM);
853 return NULL;
854 }
855 puts("OK\n");
856 }
857 bootstage_mark(BOOTSTAGE_ID_CHECK_ARCH);
858
859 if (!image_check_target_arch(hdr)) {
860 printf("Unsupported Architecture 0x%x\n", image_get_arch(hdr));
861 bootstage_error(BOOTSTAGE_ID_CHECK_ARCH);
862 return NULL;
863 }
864 return hdr;
865}
866#endif
867
868/**
869 * boot_get_kernel - find kernel image
870 * @os_data: pointer to a ulong variable, will hold os data start address
871 * @os_len: pointer to a ulong variable, will hold os data length
872 *
873 * boot_get_kernel() tries to find a kernel image, verifies its integrity
874 * and locates kernel data.
875 *
876 * returns:
877 * pointer to image header if valid image was found, plus kernel start
878 * address and length, otherwise NULL
879 */
Simon Glassed38aef2020-05-10 11:40:03 -0600880static const void *boot_get_kernel(struct cmd_tbl *cmdtp, int flag, int argc,
881 char *const argv[], bootm_headers_t *images,
Simon Glass3b5866d2014-06-12 07:24:46 -0600882 ulong *os_data, ulong *os_len)
883{
Tom Rinic220bd92019-05-23 07:14:07 -0400884#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Simon Glass3b5866d2014-06-12 07:24:46 -0600885 image_header_t *hdr;
886#endif
887 ulong img_addr;
888 const void *buf;
Simon Glass3b5866d2014-06-12 07:24:46 -0600889 const char *fit_uname_config = NULL;
890 const char *fit_uname_kernel = NULL;
Simon Glasse719d3b2021-09-25 19:43:20 -0600891#if CONFIG_IS_ENABLED(FIT)
Simon Glass3b5866d2014-06-12 07:24:46 -0600892 int os_noffset;
893#endif
894
Bryan Wuc82ea372014-08-15 16:51:39 -0700895 img_addr = genimg_get_kernel_addr_fit(argc < 1 ? NULL : argv[0],
896 &fit_uname_config,
897 &fit_uname_kernel);
Simon Glass3b5866d2014-06-12 07:24:46 -0600898
Philippe Reynesae1f2ca2022-03-28 22:57:00 +0200899 if (CONFIG_IS_ENABLED(CMD_BOOTM_PRE_LOAD))
900 img_addr += image_load_offset;
901
Simon Glass3b5866d2014-06-12 07:24:46 -0600902 bootstage_mark(BOOTSTAGE_ID_CHECK_MAGIC);
903
Simon Glass3b5866d2014-06-12 07:24:46 -0600904 /* check image type, for FIT images get FIT kernel node */
905 *os_data = *os_len = 0;
906 buf = map_sysmem(img_addr, 0);
907 switch (genimg_get_format(buf)) {
Tom Rinic220bd92019-05-23 07:14:07 -0400908#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Simon Glass3b5866d2014-06-12 07:24:46 -0600909 case IMAGE_FORMAT_LEGACY:
910 printf("## Booting kernel from Legacy Image at %08lx ...\n",
911 img_addr);
912 hdr = image_get_kernel(img_addr, images->verify);
913 if (!hdr)
914 return NULL;
915 bootstage_mark(BOOTSTAGE_ID_CHECK_IMAGETYPE);
916
917 /* get os_data and os_len */
918 switch (image_get_type(hdr)) {
919 case IH_TYPE_KERNEL:
920 case IH_TYPE_KERNEL_NOLOAD:
921 *os_data = image_get_data(hdr);
922 *os_len = image_get_data_size(hdr);
923 break;
924 case IH_TYPE_MULTI:
925 image_multi_getimg(hdr, 0, os_data, os_len);
926 break;
927 case IH_TYPE_STANDALONE:
928 *os_data = image_get_data(hdr);
929 *os_len = image_get_data_size(hdr);
930 break;
931 default:
932 printf("Wrong Image Type for %s command\n",
933 cmdtp->name);
934 bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE);
935 return NULL;
936 }
937
938 /*
939 * copy image header to allow for image overwrites during
940 * kernel decompression.
941 */
942 memmove(&images->legacy_hdr_os_copy, hdr,
943 sizeof(image_header_t));
944
945 /* save pointer to image header */
946 images->legacy_hdr_os = hdr;
947
948 images->legacy_hdr_valid = 1;
949 bootstage_mark(BOOTSTAGE_ID_DECOMP_IMAGE);
950 break;
951#endif
Simon Glasse719d3b2021-09-25 19:43:20 -0600952#if CONFIG_IS_ENABLED(FIT)
Simon Glass3b5866d2014-06-12 07:24:46 -0600953 case IMAGE_FORMAT_FIT:
Simon Glassa0c0b632014-06-12 07:24:47 -0600954 os_noffset = fit_image_load(images, img_addr,
Simon Glass3b5866d2014-06-12 07:24:46 -0600955 &fit_uname_kernel, &fit_uname_config,
956 IH_ARCH_DEFAULT, IH_TYPE_KERNEL,
957 BOOTSTAGE_ID_FIT_KERNEL_START,
958 FIT_LOAD_IGNORED, os_data, os_len);
959 if (os_noffset < 0)
960 return NULL;
961
962 images->fit_hdr_os = map_sysmem(img_addr, 0);
963 images->fit_uname_os = fit_uname_kernel;
964 images->fit_uname_cfg = fit_uname_config;
965 images->fit_noffset_os = os_noffset;
966 break;
967#endif
968#ifdef CONFIG_ANDROID_BOOT_IMAGE
969 case IMAGE_FORMAT_ANDROID:
970 printf("## Booting Android Image at 0x%08lx ...\n", img_addr);
Simon Glass95040102014-06-12 07:24:48 -0600971 if (android_image_get_kernel(buf, images->verify,
Simon Glass3b5866d2014-06-12 07:24:46 -0600972 os_data, os_len))
973 return NULL;
974 break;
975#endif
976 default:
977 printf("Wrong Image Format for %s command\n", cmdtp->name);
978 bootstage_error(BOOTSTAGE_ID_FIT_KERNEL_INFO);
979 return NULL;
980 }
981
982 debug(" kernel data at 0x%08lx, len = 0x%08lx (%ld)\n",
983 *os_data, *os_len, *os_len);
984
985 return buf;
986}
Heinrich Schuchardtaa0b11b2019-01-08 18:13:06 +0100987
988/**
989 * switch_to_non_secure_mode() - switch to non-secure mode
990 *
991 * This routine is overridden by architectures requiring this feature.
992 */
993void __weak switch_to_non_secure_mode(void)
994{
995}
996
Simon Glassa51991d2014-06-12 07:24:53 -0600997#else /* USE_HOSTCC */
998
Fabrice Fontained00d6b52019-05-03 22:37:05 +0200999#if defined(CONFIG_FIT_SIGNATURE)
Simon Glass81d13872020-03-18 11:44:02 -06001000static int bootm_host_load_image(const void *fit, int req_image_type,
1001 int cfg_noffset)
Simon Glassa51991d2014-06-12 07:24:53 -06001002{
1003 const char *fit_uname_config = NULL;
1004 ulong data, len;
1005 bootm_headers_t images;
1006 int noffset;
Tom Rinif3c2f992022-06-25 19:29:46 -04001007 ulong load_end, buf_size;
Simon Glassa51991d2014-06-12 07:24:53 -06001008 uint8_t image_type;
Daniel Golle905d4b72022-08-27 04:14:42 +01001009 uint8_t image_comp;
Simon Glassa51991d2014-06-12 07:24:53 -06001010 void *load_buf;
1011 int ret;
1012
Simon Glass81d13872020-03-18 11:44:02 -06001013 fit_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
Simon Glassa51991d2014-06-12 07:24:53 -06001014 memset(&images, '\0', sizeof(images));
1015 images.verify = 1;
1016 noffset = fit_image_load(&images, (ulong)fit,
1017 NULL, &fit_uname_config,
1018 IH_ARCH_DEFAULT, req_image_type, -1,
1019 FIT_LOAD_IGNORED, &data, &len);
1020 if (noffset < 0)
1021 return noffset;
1022 if (fit_image_get_type(fit, noffset, &image_type)) {
1023 puts("Can't get image type!\n");
1024 return -EINVAL;
1025 }
1026
Daniel Golle2ebfd222022-08-27 04:17:28 +01001027 if (fit_image_get_comp(fit, noffset, &image_comp))
1028 image_comp = IH_COMP_NONE;
Simon Glassa51991d2014-06-12 07:24:53 -06001029
1030 /* Allow the image to expand by a factor of 4, should be safe */
Tom Rinif3c2f992022-06-25 19:29:46 -04001031 buf_size = (1 << 20) + len * 4;
1032 load_buf = malloc(buf_size);
Daniel Golle905d4b72022-08-27 04:14:42 +01001033 ret = image_decomp(image_comp, 0, data, image_type, load_buf,
Tom Rinif3c2f992022-06-25 19:29:46 -04001034 (void *)data, len, buf_size, &load_end);
Simon Glassa51991d2014-06-12 07:24:53 -06001035 free(load_buf);
Simon Glass5aeccc22014-12-02 13:17:33 -07001036
Julius Werner47ef9862019-07-24 19:37:54 -07001037 if (ret) {
Daniel Golle905d4b72022-08-27 04:14:42 +01001038 ret = handle_decomp_error(image_comp, load_end - 0, buf_size, ret);
Julius Werner47ef9862019-07-24 19:37:54 -07001039 if (ret != BOOTM_ERR_UNIMPLEMENTED)
1040 return ret;
1041 }
Simon Glassa51991d2014-06-12 07:24:53 -06001042
1043 return 0;
1044}
1045
1046int bootm_host_load_images(const void *fit, int cfg_noffset)
1047{
1048 static uint8_t image_types[] = {
1049 IH_TYPE_KERNEL,
1050 IH_TYPE_FLATDT,
1051 IH_TYPE_RAMDISK,
1052 };
1053 int err = 0;
1054 int i;
1055
1056 for (i = 0; i < ARRAY_SIZE(image_types); i++) {
1057 int ret;
1058
Simon Glass81d13872020-03-18 11:44:02 -06001059 ret = bootm_host_load_image(fit, image_types[i], cfg_noffset);
Simon Glassa51991d2014-06-12 07:24:53 -06001060 if (!err && ret && ret != -ENOENT)
1061 err = ret;
1062 }
1063
1064 /* Return the first error we found */
1065 return err;
1066}
Fabrice Fontained00d6b52019-05-03 22:37:05 +02001067#endif
Simon Glass0232ba72014-06-12 07:24:51 -06001068
1069#endif /* ndef USE_HOSTCC */