blob: 7c1b4a34c4cc4c6d900ce25406d7784596d6f610 [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 Glassb3c2aa52023-09-27 08:22:37 -060011#include <command.h>
Simon Glass63334482019-11-14 12:57:39 -070012#include <cpu_func.h>
Simon Glass313112a2019-08-01 09:46:46 -060013#include <env.h>
Simon Glass0129b522014-10-19 21:11:24 -060014#include <errno.h>
Simon Glass3b5866d2014-06-12 07:24:46 -060015#include <fdt_support.h>
Simon Glass8f3f7612019-11-14 12:57:42 -070016#include <irq_func.h>
Simon Glass3b5866d2014-06-12 07:24:46 -060017#include <lmb.h>
Simon Glass0f2af882020-05-10 11:40:05 -060018#include <log.h>
Simon Glass3b5866d2014-06-12 07:24:46 -060019#include <malloc.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050020#include <mapmem.h>
Simon Glass274e0b02020-05-10 11:39:56 -060021#include <net.h>
22#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060023#include <asm/global_data.h>
Simon Glass3b5866d2014-06-12 07:24:46 -060024#include <asm/io.h>
Simon Glassc38406e2020-11-05 10:33:43 -070025#include <linux/sizes.h>
Eddie James32401ba2023-10-24 10:43:50 -050026#include <tpm-v2.h>
Simon Glass3b5866d2014-06-12 07:24:46 -060027#if defined(CONFIG_CMD_USB)
28#include <usb.h>
29#endif
Simon Glass0232ba72014-06-12 07:24:51 -060030#else
31#include "mkimage.h"
32#endif
Simon Glass3b5866d2014-06-12 07:24:46 -060033
Simon Glass0232ba72014-06-12 07:24:51 -060034#include <bootm.h>
35#include <image.h>
Simon Glass3b5866d2014-06-12 07:24:46 -060036
Simon Glassc38406e2020-11-05 10:33:43 -070037#define MAX_CMDLINE_SIZE SZ_4K
38
Simon Glass3b5866d2014-06-12 07:24:46 -060039#define IH_INITRD_ARCH IH_ARCH_DEFAULT
40
Simon Glass0232ba72014-06-12 07:24:51 -060041#ifndef USE_HOSTCC
42
43DECLARE_GLOBAL_DATA_PTR;
44
Simon Glassdf00afa2022-09-06 20:26:50 -060045struct bootm_headers images; /* pointers to os/initrd/fdt images */
Tom Rini36f067f2016-08-12 08:31:15 -040046
Simon Glassed38aef2020-05-10 11:40:03 -060047static const void *boot_get_kernel(struct cmd_tbl *cmdtp, int flag, int argc,
Simon Glassdf00afa2022-09-06 20:26:50 -060048 char *const argv[], struct bootm_headers *images,
Simon Glass3b5866d2014-06-12 07:24:46 -060049 ulong *os_data, ulong *os_len);
50
Simon Glassf55305a2018-05-16 09:42:25 -060051__weak void board_quiesce_devices(void)
52{
53}
54
Simon Glass3b5866d2014-06-12 07:24:46 -060055#ifdef CONFIG_LMB
Simon Glassdf00afa2022-09-06 20:26:50 -060056static void boot_start_lmb(struct bootm_headers *images)
Simon Glass3b5866d2014-06-12 07:24:46 -060057{
58 ulong mem_start;
59 phys_size_t mem_size;
60
Simon Glassda1a1342017-08-03 12:22:15 -060061 mem_start = env_get_bootm_low();
62 mem_size = env_get_bootm_size();
Simon Glass3b5866d2014-06-12 07:24:46 -060063
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +010064 lmb_init_and_reserve_range(&images->lmb, (phys_addr_t)mem_start,
65 mem_size, NULL);
Simon Glass3b5866d2014-06-12 07:24:46 -060066}
67#else
68#define lmb_reserve(lmb, base, size)
Simon Glassdf00afa2022-09-06 20:26:50 -060069static inline void boot_start_lmb(struct bootm_headers *images) { }
Simon Glass3b5866d2014-06-12 07:24:46 -060070#endif
71
Simon Glassed38aef2020-05-10 11:40:03 -060072static int bootm_start(struct cmd_tbl *cmdtp, int flag, int argc,
73 char *const argv[])
Simon Glass3b5866d2014-06-12 07:24:46 -060074{
75 memset((void *)&images, 0, sizeof(images));
Simon Glass22c34c22017-08-03 12:22:13 -060076 images.verify = env_get_yesno("verify");
Simon Glass3b5866d2014-06-12 07:24:46 -060077
78 boot_start_lmb(&images);
79
80 bootstage_mark_name(BOOTSTAGE_ID_BOOTM_START, "bootm_start");
81 images.state = BOOTM_STATE_START;
82
83 return 0;
84}
85
Philippe Reynesae1f2ca2022-03-28 22:57:00 +020086static ulong bootm_data_addr(int argc, char *const argv[])
87{
88 ulong addr;
89
90 if (argc > 0)
91 addr = simple_strtoul(argv[0], NULL, 16);
92 else
93 addr = image_load_addr;
94
95 return addr;
96}
97
98static int bootm_pre_load(struct cmd_tbl *cmdtp, int flag, int argc,
99 char *const argv[])
100{
101 ulong data_addr = bootm_data_addr(argc, argv);
102 int ret = 0;
103
Simon Glass88b6f922023-02-05 15:36:24 -0700104 if (IS_ENABLED(CONFIG_CMD_BOOTM_PRE_LOAD))
Philippe Reynesae1f2ca2022-03-28 22:57:00 +0200105 ret = image_pre_load(data_addr);
106
107 if (ret)
108 ret = CMD_RET_FAILURE;
109
110 return ret;
111}
112
Simon Glassed38aef2020-05-10 11:40:03 -0600113static int bootm_find_os(struct cmd_tbl *cmdtp, int flag, int argc,
114 char *const argv[])
Simon Glass3b5866d2014-06-12 07:24:46 -0600115{
116 const void *os_hdr;
Safae Ouajih51c981b2023-02-06 00:50:17 +0100117#ifdef CONFIG_ANDROID_BOOT_IMAGE
118 const void *vendor_boot_img;
119 const void *boot_img;
120#endif
Simon Glass3b5866d2014-06-12 07:24:46 -0600121 bool ep_found = false;
Simon Glass0129b522014-10-19 21:11:24 -0600122 int ret;
Simon Glass3b5866d2014-06-12 07:24:46 -0600123
124 /* get kernel image header, start address and length */
125 os_hdr = boot_get_kernel(cmdtp, flag, argc, argv,
126 &images, &images.os.image_start, &images.os.image_len);
127 if (images.os.image_len == 0) {
128 puts("ERROR: can't get kernel image!\n");
129 return 1;
130 }
131
132 /* get image parameters */
133 switch (genimg_get_format(os_hdr)) {
Tom Rinic220bd92019-05-23 07:14:07 -0400134#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Simon Glass3b5866d2014-06-12 07:24:46 -0600135 case IMAGE_FORMAT_LEGACY:
136 images.os.type = image_get_type(os_hdr);
137 images.os.comp = image_get_comp(os_hdr);
138 images.os.os = image_get_os(os_hdr);
139
140 images.os.end = image_get_image_end(os_hdr);
141 images.os.load = image_get_load(os_hdr);
Simon Glass0129b522014-10-19 21:11:24 -0600142 images.os.arch = image_get_arch(os_hdr);
Simon Glass3b5866d2014-06-12 07:24:46 -0600143 break;
144#endif
Simon Glasse719d3b2021-09-25 19:43:20 -0600145#if CONFIG_IS_ENABLED(FIT)
Simon Glass3b5866d2014-06-12 07:24:46 -0600146 case IMAGE_FORMAT_FIT:
147 if (fit_image_get_type(images.fit_hdr_os,
148 images.fit_noffset_os,
149 &images.os.type)) {
150 puts("Can't get image type!\n");
151 bootstage_error(BOOTSTAGE_ID_FIT_TYPE);
152 return 1;
153 }
154
155 if (fit_image_get_comp(images.fit_hdr_os,
156 images.fit_noffset_os,
157 &images.os.comp)) {
158 puts("Can't get image compression!\n");
159 bootstage_error(BOOTSTAGE_ID_FIT_COMPRESSION);
160 return 1;
161 }
162
163 if (fit_image_get_os(images.fit_hdr_os, images.fit_noffset_os,
164 &images.os.os)) {
165 puts("Can't get image OS!\n");
166 bootstage_error(BOOTSTAGE_ID_FIT_OS);
167 return 1;
168 }
169
Simon Glass0129b522014-10-19 21:11:24 -0600170 if (fit_image_get_arch(images.fit_hdr_os,
171 images.fit_noffset_os,
172 &images.os.arch)) {
173 puts("Can't get image ARCH!\n");
174 return 1;
175 }
176
Simon Glass3b5866d2014-06-12 07:24:46 -0600177 images.os.end = fit_get_end(images.fit_hdr_os);
178
179 if (fit_image_get_load(images.fit_hdr_os, images.fit_noffset_os,
180 &images.os.load)) {
181 puts("Can't get image load address!\n");
182 bootstage_error(BOOTSTAGE_ID_FIT_LOADADDR);
183 return 1;
184 }
185 break;
186#endif
187#ifdef CONFIG_ANDROID_BOOT_IMAGE
188 case IMAGE_FORMAT_ANDROID:
Safae Ouajih51c981b2023-02-06 00:50:17 +0100189 boot_img = os_hdr;
190 vendor_boot_img = NULL;
191 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
192 boot_img = map_sysmem(get_abootimg_addr(), 0);
193 vendor_boot_img = map_sysmem(get_avendor_bootimg_addr(), 0);
194 }
Simon Glass3b5866d2014-06-12 07:24:46 -0600195 images.os.type = IH_TYPE_KERNEL;
Safae Ouajih51c981b2023-02-06 00:50:17 +0100196 images.os.comp = android_image_get_kcomp(boot_img, vendor_boot_img);
Simon Glass3b5866d2014-06-12 07:24:46 -0600197 images.os.os = IH_OS_LINUX;
Safae Ouajih51c981b2023-02-06 00:50:17 +0100198 images.os.end = android_image_get_end(boot_img, vendor_boot_img);
199 images.os.load = android_image_get_kload(boot_img, vendor_boot_img);
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300200 images.ep = images.os.load;
201 ep_found = true;
Safae Ouajih51c981b2023-02-06 00:50:17 +0100202 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
203 unmap_sysmem(vendor_boot_img);
204 unmap_sysmem(boot_img);
205 }
Simon Glass3b5866d2014-06-12 07:24:46 -0600206 break;
207#endif
208 default:
209 puts("ERROR: unknown image format type!\n");
210 return 1;
211 }
212
Simon Glass0129b522014-10-19 21:11:24 -0600213 /* If we have a valid setup.bin, we will use that for entry (x86) */
Simon Glass9d428302014-10-10 08:21:57 -0600214 if (images.os.arch == IH_ARCH_I386 ||
215 images.os.arch == IH_ARCH_X86_64) {
Simon Glass0129b522014-10-19 21:11:24 -0600216 ulong len;
217
218 ret = boot_get_setup(&images, IH_ARCH_I386, &images.ep, &len);
219 if (ret < 0 && ret != -ENOENT) {
220 puts("Could not find a valid setup.bin for x86\n");
221 return 1;
222 }
223 /* Kernel entry point is the setup.bin */
224 } else if (images.legacy_hdr_valid) {
Simon Glass3b5866d2014-06-12 07:24:46 -0600225 images.ep = image_get_ep(&images.legacy_hdr_os_copy);
Simon Glasse719d3b2021-09-25 19:43:20 -0600226#if CONFIG_IS_ENABLED(FIT)
Simon Glass3b5866d2014-06-12 07:24:46 -0600227 } else if (images.fit_uname_os) {
228 int ret;
229
230 ret = fit_image_get_entry(images.fit_hdr_os,
231 images.fit_noffset_os, &images.ep);
232 if (ret) {
233 puts("Can't get entry point property!\n");
234 return 1;
235 }
236#endif
237 } else if (!ep_found) {
238 puts("Could not find kernel entry point!\n");
239 return 1;
240 }
241
242 if (images.os.type == IH_TYPE_KERNEL_NOLOAD) {
Simon Glassb1f90ca2023-11-19 07:43:33 -0700243 images.os.load = images.os.image_start;
244 images.ep += images.os.image_start;
Simon Glass3b5866d2014-06-12 07:24:46 -0600245 }
246
Simon Glass5b539a02016-02-24 09:14:42 -0700247 images.os.start = map_to_sysmem(os_hdr);
Simon Glass3b5866d2014-06-12 07:24:46 -0600248
249 return 0;
250}
251
Karl Apsited686ce92015-05-21 09:52:49 -0400252/**
253 * bootm_find_images - wrapper to find and locate various images
254 * @flag: Ignored Argument
255 * @argc: command argument count
256 * @argv: command argument list
Tero Kristo06086942020-06-12 15:41:20 +0300257 * @start: OS image start address
258 * @size: OS image size
Karl Apsited686ce92015-05-21 09:52:49 -0400259 *
260 * boot_find_images() will attempt to load an available ramdisk,
261 * flattened device tree, as well as specifically marked
262 * "loadable" images (loadables are FIT only)
263 *
264 * Note: bootm_find_images will skip an image if it is not found
265 *
266 * @return:
267 * 0, if all existing images were loaded correctly
268 * 1, if an image is found but corrupted, or invalid
269 */
Tero Kristo06086942020-06-12 15:41:20 +0300270int bootm_find_images(int flag, int argc, char *const argv[], ulong start,
271 ulong size)
Simon Glass3b5866d2014-06-12 07:24:46 -0600272{
273 int ret;
274
275 /* find ramdisk */
276 ret = boot_get_ramdisk(argc, argv, &images, IH_INITRD_ARCH,
277 &images.rd_start, &images.rd_end);
278 if (ret) {
279 puts("Ramdisk image is corrupt or invalid\n");
280 return 1;
281 }
282
Tero Kristo06086942020-06-12 15:41:20 +0300283 /* check if ramdisk overlaps OS image */
284 if (images.rd_start && (((ulong)images.rd_start >= start &&
Jaehoon Chung80ffb812020-10-21 14:17:03 +0900285 (ulong)images.rd_start < start + size) ||
286 ((ulong)images.rd_end > start &&
287 (ulong)images.rd_end <= start + size) ||
288 ((ulong)images.rd_start < start &&
289 (ulong)images.rd_end >= start + size))) {
Tero Kristo06086942020-06-12 15:41:20 +0300290 printf("ERROR: RD image overlaps OS image (OS=0x%lx..0x%lx)\n",
291 start, start + size);
292 return 1;
293 }
294
Simon Glass85c057e2021-09-25 19:43:21 -0600295#if CONFIG_IS_ENABLED(OF_LIBFDT)
Simon Glass3b5866d2014-06-12 07:24:46 -0600296 /* find flattened device tree */
297 ret = boot_get_fdt(flag, argc, argv, IH_ARCH_DEFAULT, &images,
298 &images.ft_addr, &images.ft_len);
299 if (ret) {
300 puts("Could not find a valid device tree\n");
301 return 1;
302 }
Tero Kristo06086942020-06-12 15:41:20 +0300303
304 /* check if FDT overlaps OS image */
305 if (images.ft_addr &&
306 (((ulong)images.ft_addr >= start &&
Pali Rohár779a7b692022-08-27 14:48:10 +0200307 (ulong)images.ft_addr < start + size) ||
Tero Kristo06086942020-06-12 15:41:20 +0300308 ((ulong)images.ft_addr + images.ft_len >= start &&
Pali Rohár779a7b692022-08-27 14:48:10 +0200309 (ulong)images.ft_addr + images.ft_len < start + size))) {
Tero Kristo06086942020-06-12 15:41:20 +0300310 printf("ERROR: FDT image overlaps OS image (OS=0x%lx..0x%lx)\n",
311 start, start + size);
312 return 1;
313 }
314
Simon Glasse2d49d92023-02-05 15:36:30 -0700315 if (IS_ENABLED(CONFIG_CMD_FDT))
Simon Goldschmidt6127ac02018-12-17 20:14:42 +0100316 set_working_fdt_addr(map_to_sysmem(images.ft_addr));
Simon Glass3b5866d2014-06-12 07:24:46 -0600317#endif
318
Simon Glasse719d3b2021-09-25 19:43:20 -0600319#if CONFIG_IS_ENABLED(FIT)
Simon Glassbc0c4db2021-09-25 07:03:20 -0600320 if (IS_ENABLED(CONFIG_FPGA)) {
321 /* find bitstreams */
322 ret = boot_get_fpga(argc, argv, &images, IH_ARCH_DEFAULT,
323 NULL, NULL);
324 if (ret) {
325 printf("FPGA image is corrupted or invalid\n");
326 return 1;
327 }
Michal Simekebae78b2016-05-17 14:03:50 +0200328 }
Michal Simekebae78b2016-05-17 14:03:50 +0200329
Karl Apsite1b21c282015-05-21 09:52:48 -0400330 /* find all of the loadables */
331 ret = boot_get_loadable(argc, argv, &images, IH_ARCH_DEFAULT,
332 NULL, NULL);
333 if (ret) {
334 printf("Loadable(s) is corrupt or invalid\n");
335 return 1;
336 }
Karl Apsite1b21c282015-05-21 09:52:48 -0400337#endif
338
Simon Glass3b5866d2014-06-12 07:24:46 -0600339 return 0;
340}
341
Simon Glassed38aef2020-05-10 11:40:03 -0600342static int bootm_find_other(struct cmd_tbl *cmdtp, int flag, int argc,
343 char *const argv[])
Simon Glass3b5866d2014-06-12 07:24:46 -0600344{
345 if (((images.os.type == IH_TYPE_KERNEL) ||
346 (images.os.type == IH_TYPE_KERNEL_NOLOAD) ||
347 (images.os.type == IH_TYPE_MULTI)) &&
348 (images.os.os == IH_OS_LINUX ||
349 images.os.os == IH_OS_VXWORKS))
Tero Kristo06086942020-06-12 15:41:20 +0300350 return bootm_find_images(flag, argc, argv, 0, 0);
Simon Glass3b5866d2014-06-12 07:24:46 -0600351
352 return 0;
353}
Simon Glass10ae4192014-12-02 13:17:30 -0700354#endif /* USE_HOSTC */
355
Julius Werner47ef9862019-07-24 19:37:54 -0700356#if !defined(USE_HOSTCC) || defined(CONFIG_FIT_SIGNATURE)
Simon Glass4f6e6d72014-12-02 13:17:37 -0700357/**
358 * handle_decomp_error() - display a decompression error
359 *
360 * This function tries to produce a useful message. In the case where the
361 * uncompressed size is the same as the available space, we can assume that
362 * the image is too large for the buffer.
363 *
364 * @comp_type: Compression type being used (IH_COMP_...)
365 * @uncomp_size: Number of bytes uncompressed
Tom Rinif3c2f992022-06-25 19:29:46 -0400366 * @buf_size: Number of bytes the decompresion buffer was
Julius Werner47ef9862019-07-24 19:37:54 -0700367 * @ret: errno error code received from compression library
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100368 * Return: Appropriate BOOTM_ERR_ error code
Simon Glass4f6e6d72014-12-02 13:17:37 -0700369 */
Tom Rinif3c2f992022-06-25 19:29:46 -0400370static int handle_decomp_error(int comp_type, size_t uncomp_size,
371 size_t buf_size, int ret)
Simon Glass10ae4192014-12-02 13:17:30 -0700372{
Simon Glass4f6e6d72014-12-02 13:17:37 -0700373 const char *name = genimg_get_comp_name(comp_type);
374
Julius Werner47ef9862019-07-24 19:37:54 -0700375 /* ENOSYS means unimplemented compression type, don't reset. */
376 if (ret == -ENOSYS)
377 return BOOTM_ERR_UNIMPLEMENTED;
378
Tom Rinif3c2f992022-06-25 19:29:46 -0400379 if (uncomp_size >= buf_size)
Simon Glass4f6e6d72014-12-02 13:17:37 -0700380 printf("Image too large: increase CONFIG_SYS_BOOTM_LEN\n");
Simon Glass10ae4192014-12-02 13:17:30 -0700381 else
Simon Glass4f6e6d72014-12-02 13:17:37 -0700382 printf("%s: uncompress error %d\n", name, ret);
383
384 /*
385 * The decompression routines are now safe, so will not write beyond
386 * their bounds. Probably it is not necessary to reset, but maintain
387 * the current behaviour for now.
388 */
389 printf("Must RESET board to recover\n");
Simon Glass10ae4192014-12-02 13:17:30 -0700390#ifndef USE_HOSTCC
391 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
392#endif
393
394 return BOOTM_ERR_RESET;
395}
Julius Werner47ef9862019-07-24 19:37:54 -0700396#endif
Simon Glass686f3bb2014-06-12 07:24:52 -0600397
Simon Glassa51991d2014-06-12 07:24:53 -0600398#ifndef USE_HOSTCC
Simon Glassdf00afa2022-09-06 20:26:50 -0600399static int bootm_load_os(struct bootm_headers *images, int boot_progress)
Simon Glass686f3bb2014-06-12 07:24:52 -0600400{
Simon Glass74578f52022-09-06 20:26:51 -0600401 struct image_info os = images->os;
Simon Glass686f3bb2014-06-12 07:24:52 -0600402 ulong load = os.load;
Tom Rini6c96f5d2018-05-01 12:32:37 -0400403 ulong load_end;
Simon Glass686f3bb2014-06-12 07:24:52 -0600404 ulong blob_start = os.start;
405 ulong blob_end = os.end;
406 ulong image_start = os.image_start;
407 ulong image_len = os.image_len;
Bryan O'Donoghue600388e2018-04-15 11:48:17 +0100408 ulong flush_start = ALIGN_DOWN(load, ARCH_DMA_MINALIGN);
Simon Glass686f3bb2014-06-12 07:24:52 -0600409 bool no_overlap;
410 void *load_buf, *image_buf;
411 int err;
412
Simon Glass50b8be32023-11-19 07:43:34 -0700413 /*
414 * For a "noload" compressed kernel we need to allocate a buffer large
415 * enough to decompress in to and use that as the load address now.
416 * Assume that the kernel compression is at most a factor of 4 since
417 * zstd almost achieves that.
418 * Use an alignment of 2MB since this might help arm64
419 */
420 if (os.type == IH_TYPE_KERNEL_NOLOAD && os.comp != IH_COMP_NONE) {
421 ulong req_size = ALIGN(image_len * 4, SZ_1M);
422
423 load = lmb_alloc(&images->lmb, req_size, SZ_2M);
424 if (!load)
425 return 1;
426 os.load = load;
427 debug("Allocated %lx bytes at %lx for kernel (size %lx) decompression\n",
428 req_size, load, image_len);
429 }
430
Simon Glass686f3bb2014-06-12 07:24:52 -0600431 load_buf = map_sysmem(load, 0);
432 image_buf = map_sysmem(os.image_start, image_len);
Julius Werner47ef9862019-07-24 19:37:54 -0700433 err = image_decomp(os.comp, load, os.image_start, os.type,
434 load_buf, image_buf, image_len,
435 CONFIG_SYS_BOOTM_LEN, &load_end);
Simon Glass686f3bb2014-06-12 07:24:52 -0600436 if (err) {
Tom Rinif3c2f992022-06-25 19:29:46 -0400437 err = handle_decomp_error(os.comp, load_end - load,
438 CONFIG_SYS_BOOTM_LEN, err);
Simon Glass686f3bb2014-06-12 07:24:52 -0600439 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
440 return err;
441 }
Heinrich Schuchardt6d29e0d2020-08-30 11:34:12 +0200442 /* We need the decompressed image size in the next steps */
443 images->os.image_len = load_end - load;
Bryan O'Donoghue600388e2018-04-15 11:48:17 +0100444
Trent Piepho92e51d02019-03-27 23:50:09 +0000445 flush_cache(flush_start, ALIGN(load_end, ARCH_DMA_MINALIGN) - flush_start);
Simon Glass3b5866d2014-06-12 07:24:46 -0600446
Tom Rini6c96f5d2018-05-01 12:32:37 -0400447 debug(" kernel loaded at 0x%08lx, end = 0x%08lx\n", load, load_end);
Simon Glass3b5866d2014-06-12 07:24:46 -0600448 bootstage_mark(BOOTSTAGE_ID_KERNEL_LOADED);
449
Simon Glass686f3bb2014-06-12 07:24:52 -0600450 no_overlap = (os.comp == IH_COMP_NONE && load == image_start);
451
Tom Rini6c96f5d2018-05-01 12:32:37 -0400452 if (!no_overlap && load < blob_end && load_end > blob_start) {
Simon Glass3b5866d2014-06-12 07:24:46 -0600453 debug("images.os.start = 0x%lX, images.os.end = 0x%lx\n",
454 blob_start, blob_end);
455 debug("images.os.load = 0x%lx, load_end = 0x%lx\n", load,
Tom Rini6c96f5d2018-05-01 12:32:37 -0400456 load_end);
Simon Glass3b5866d2014-06-12 07:24:46 -0600457
458 /* Check what type of image this is. */
459 if (images->legacy_hdr_valid) {
460 if (image_get_type(&images->legacy_hdr_os_copy)
461 == IH_TYPE_MULTI)
462 puts("WARNING: legacy format multi component image overwritten\n");
463 return BOOTM_ERR_OVERLAP;
464 } else {
465 puts("ERROR: new format image overwritten - must RESET the board to recover\n");
466 bootstage_error(BOOTSTAGE_ID_OVERWRITTEN);
467 return BOOTM_ERR_RESET;
468 }
469 }
470
Simon Glassb1f90ca2023-11-19 07:43:33 -0700471 if (IS_ENABLED(CONFIG_CMD_BOOTI) && images->os.arch == IH_ARCH_ARM64 &&
472 images->os.os == IH_OS_LINUX) {
473 ulong relocated_addr;
474 ulong image_size;
475 int ret;
476
477 ret = booti_setup(load, &relocated_addr, &image_size, false);
478 if (ret) {
479 printf("Failed to prep arm64 kernel (err=%d)\n", ret);
480 return BOOTM_ERR_RESET;
481 }
482
483 /* Handle BOOTM_STATE_LOADOS */
484 if (relocated_addr != load) {
485 printf("Moving Image from 0x%lx to 0x%lx, end=%lx\n",
486 load, relocated_addr,
487 relocated_addr + image_size);
488 memmove((void *)relocated_addr, load_buf, image_size);
489 }
490
491 images->ep = relocated_addr;
492 images->os.start = relocated_addr;
493 images->os.end = relocated_addr + image_size;
494 }
495
Tom Rini6c96f5d2018-05-01 12:32:37 -0400496 lmb_reserve(&images->lmb, images->os.load, (load_end -
497 images->os.load));
Simon Glass3b5866d2014-06-12 07:24:46 -0600498 return 0;
499}
500
501/**
502 * bootm_disable_interrupts() - Disable interrupts in preparation for load/boot
503 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100504 * Return: interrupt flag (0 if interrupts were disabled, non-zero if they were
Simon Glass3b5866d2014-06-12 07:24:46 -0600505 * enabled)
506 */
507ulong bootm_disable_interrupts(void)
508{
509 ulong iflag;
510
511 /*
512 * We have reached the point of no return: we are going to
513 * overwrite all exception vector code, so we cannot easily
514 * recover from any failures any more...
515 */
516 iflag = disable_interrupts();
517#ifdef CONFIG_NETCONSOLE
518 /* Stop the ethernet stack if NetConsole could have left it up */
519 eth_halt();
Simon Glass3b5866d2014-06-12 07:24:46 -0600520#endif
521
522#if defined(CONFIG_CMD_USB)
523 /*
524 * turn off USB to prevent the host controller from writing to the
525 * SDRAM while Linux is booting. This could happen (at least for OHCI
526 * controller), because the HCCA (Host Controller Communication Area)
527 * lies within the SDRAM and the host controller writes continously to
528 * this area (as busmaster!). The HccaFrameNumber is for example
529 * updated every 1 ms within the HCCA structure in SDRAM! For more
530 * details see the OpenHCI specification.
531 */
532 usb_stop();
533#endif
534 return iflag;
535}
536
Simon Glass7a8a4292020-11-05 10:33:42 -0700537#define CONSOLE_ARG "console="
Sean Anderson15bdcd42022-05-19 18:26:05 -0400538#define NULL_CONSOLE (CONSOLE_ARG "ttynull")
539#define CONSOLE_ARG_SIZE sizeof(NULL_CONSOLE)
Simon Glass3b5866d2014-06-12 07:24:46 -0600540
Simon Glassc38406e2020-11-05 10:33:43 -0700541/**
542 * fixup_silent_linux() - Handle silencing the linux boot if required
543 *
544 * This uses the silent_linux envvar to control whether to add/set a "console="
545 * parameter to the command line
546 *
547 * @buf: Buffer containing the string to process
548 * @maxlen: Maximum length of buffer
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100549 * Return: 0 if OK, -ENOSPC if @maxlen is too small
Simon Glassc38406e2020-11-05 10:33:43 -0700550 */
551static int fixup_silent_linux(char *buf, int maxlen)
Simon Glass3b5866d2014-06-12 07:24:46 -0600552{
Simon Glass3b5866d2014-06-12 07:24:46 -0600553 int want_silent;
Simon Glassc38406e2020-11-05 10:33:43 -0700554 char *cmdline;
555 int size;
Simon Glass07a88862020-11-05 10:33:38 -0700556
Simon Glass3b5866d2014-06-12 07:24:46 -0600557 /*
Simon Glassc38406e2020-11-05 10:33:43 -0700558 * Move the input string to the end of buffer. The output string will be
559 * built up at the start.
560 */
561 size = strlen(buf) + 1;
562 if (size * 2 > maxlen)
563 return -ENOSPC;
564 cmdline = buf + maxlen - size;
565 memmove(cmdline, buf, size);
566 /*
Simon Glass3b5866d2014-06-12 07:24:46 -0600567 * Only fix cmdline when requested. The environment variable can be:
568 *
569 * no - we never fixup
570 * yes - we always fixup
571 * unset - we rely on the console silent flag
572 */
Simon Glass22c34c22017-08-03 12:22:13 -0600573 want_silent = env_get_yesno("silent_linux");
Simon Glass3b5866d2014-06-12 07:24:46 -0600574 if (want_silent == 0)
Simon Glass2eab0172020-11-05 10:33:39 -0700575 return 0;
Simon Glass3b5866d2014-06-12 07:24:46 -0600576 else if (want_silent == -1 && !(gd->flags & GD_FLG_SILENT))
Simon Glass2eab0172020-11-05 10:33:39 -0700577 return 0;
Simon Glass3b5866d2014-06-12 07:24:46 -0600578
579 debug("before silent fix-up: %s\n", cmdline);
Simon Glassc38406e2020-11-05 10:33:43 -0700580 if (*cmdline) {
Simon Glass3b5866d2014-06-12 07:24:46 -0600581 char *start = strstr(cmdline, CONSOLE_ARG);
582
Simon Glassc38406e2020-11-05 10:33:43 -0700583 /* Check space for maximum possible new command line */
584 if (size + CONSOLE_ARG_SIZE > maxlen)
Simon Glass2eab0172020-11-05 10:33:39 -0700585 return -ENOSPC;
Simon Glass3b5866d2014-06-12 07:24:46 -0600586
587 if (start) {
588 char *end = strchr(start, ' ');
Simon Glass7a8a4292020-11-05 10:33:42 -0700589 int start_bytes;
Simon Glass3b5866d2014-06-12 07:24:46 -0600590
Sean Anderson15bdcd42022-05-19 18:26:05 -0400591 start_bytes = start - cmdline;
Simon Glass7a8a4292020-11-05 10:33:42 -0700592 strncpy(buf, cmdline, start_bytes);
Sean Anderson15bdcd42022-05-19 18:26:05 -0400593 strncpy(buf + start_bytes, NULL_CONSOLE, CONSOLE_ARG_SIZE);
Simon Glass3b5866d2014-06-12 07:24:46 -0600594 if (end)
Sean Anderson15bdcd42022-05-19 18:26:05 -0400595 strcpy(buf + start_bytes + CONSOLE_ARG_SIZE - 1, end);
Simon Glass3b5866d2014-06-12 07:24:46 -0600596 else
Sean Anderson15bdcd42022-05-19 18:26:05 -0400597 buf[start_bytes + CONSOLE_ARG_SIZE] = '\0';
Simon Glass3b5866d2014-06-12 07:24:46 -0600598 } else {
Sean Anderson15bdcd42022-05-19 18:26:05 -0400599 sprintf(buf, "%s %s", cmdline, NULL_CONSOLE);
Simon Glass3b5866d2014-06-12 07:24:46 -0600600 }
Simon Glassc38406e2020-11-05 10:33:43 -0700601 if (buf + strlen(buf) >= cmdline)
602 return -ENOSPC;
Simon Glass3b5866d2014-06-12 07:24:46 -0600603 } else {
Sean Anderson15bdcd42022-05-19 18:26:05 -0400604 if (maxlen < CONSOLE_ARG_SIZE)
Simon Glassc38406e2020-11-05 10:33:43 -0700605 return -ENOSPC;
Sean Anderson15bdcd42022-05-19 18:26:05 -0400606 strcpy(buf, NULL_CONSOLE);
Simon Glass3b5866d2014-06-12 07:24:46 -0600607 }
Simon Glassc38406e2020-11-05 10:33:43 -0700608 debug("after silent fix-up: %s\n", buf);
609
610 return 0;
611}
612
Simon Glass529e2082020-11-05 10:33:48 -0700613/**
614 * process_subst() - Handle substitution of ${...} fields in the environment
615 *
616 * Handle variable substitution in the provided buffer
617 *
618 * @buf: Buffer containing the string to process
619 * @maxlen: Maximum length of buffer
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100620 * Return: 0 if OK, -ENOSPC if @maxlen is too small
Simon Glass529e2082020-11-05 10:33:48 -0700621 */
622static int process_subst(char *buf, int maxlen)
623{
624 char *cmdline;
625 int size;
626 int ret;
627
628 /* Move to end of buffer */
629 size = strlen(buf) + 1;
630 cmdline = buf + maxlen - size;
631 if (buf + size > cmdline)
632 return -ENOSPC;
633 memmove(cmdline, buf, size);
634
635 ret = cli_simple_process_macros(cmdline, buf, cmdline - buf);
636
637 return ret;
638}
639
Simon Glassb4e1b6d2020-11-05 10:33:45 -0700640int bootm_process_cmdline(char *buf, int maxlen, int flags)
641{
642 int ret;
643
644 /* Check config first to enable compiler to eliminate code */
645 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) &&
646 !IS_ENABLED(CONFIG_SILENT_U_BOOT_ONLY) &&
647 (flags & BOOTM_CL_SILENT)) {
648 ret = fixup_silent_linux(buf, maxlen);
649 if (ret)
650 return log_msg_ret("silent", ret);
651 }
Simon Glassed8603d2021-03-15 18:11:23 +1300652 if (IS_ENABLED(CONFIG_BOOTARGS_SUBST) && IS_ENABLED(CONFIG_CMDLINE) &&
653 (flags & BOOTM_CL_SUBST)) {
Simon Glass529e2082020-11-05 10:33:48 -0700654 ret = process_subst(buf, maxlen);
655 if (ret)
Simon Glasscac744e2021-02-06 09:57:35 -0700656 return log_msg_ret("subst", ret);
Simon Glass529e2082020-11-05 10:33:48 -0700657 }
Simon Glassb4e1b6d2020-11-05 10:33:45 -0700658
659 return 0;
660}
661
Simon Glass63660dc2020-11-05 10:33:44 -0700662int bootm_process_cmdline_env(int flags)
Simon Glassc38406e2020-11-05 10:33:43 -0700663{
664 const int maxlen = MAX_CMDLINE_SIZE;
Simon Glass63660dc2020-11-05 10:33:44 -0700665 bool do_silent;
Simon Glassc38406e2020-11-05 10:33:43 -0700666 const char *env;
667 char *buf;
668 int ret;
Simon Glass3b5866d2014-06-12 07:24:46 -0600669
Simon Glassc38406e2020-11-05 10:33:43 -0700670 /* First check if any action is needed */
671 do_silent = IS_ENABLED(CONFIG_SILENT_CONSOLE) &&
Simon Glass63660dc2020-11-05 10:33:44 -0700672 !IS_ENABLED(CONFIG_SILENT_U_BOOT_ONLY) && (flags & BOOTM_CL_SILENT);
Simon Glass529e2082020-11-05 10:33:48 -0700673 if (!do_silent && !IS_ENABLED(CONFIG_BOOTARGS_SUBST))
Simon Glassc38406e2020-11-05 10:33:43 -0700674 return 0;
675
676 env = env_get("bootargs");
677 if (env && strlen(env) >= maxlen)
678 return -E2BIG;
679 buf = malloc(maxlen);
680 if (!buf)
681 return -ENOMEM;
682 if (env)
683 strcpy(buf, env);
684 else
685 *buf = '\0';
Simon Glassb4e1b6d2020-11-05 10:33:45 -0700686 ret = bootm_process_cmdline(buf, maxlen, flags);
Simon Glassc38406e2020-11-05 10:33:43 -0700687 if (!ret) {
688 ret = env_set("bootargs", buf);
689
690 /*
691 * If buf is "" and bootargs does not exist, this will produce
692 * an error trying to delete bootargs. Ignore it
693 */
694 if (ret == -ENOENT)
695 ret = 0;
696 }
Simon Glass3b5866d2014-06-12 07:24:46 -0600697 free(buf);
Simon Glassc38406e2020-11-05 10:33:43 -0700698 if (ret)
699 return log_msg_ret("env", ret);
Simon Glass2eab0172020-11-05 10:33:39 -0700700
701 return 0;
Simon Glass3b5866d2014-06-12 07:24:46 -0600702}
Simon Glass3b5866d2014-06-12 07:24:46 -0600703
Eddie James32401ba2023-10-24 10:43:50 -0500704int bootm_measure(struct bootm_headers *images)
705{
706 int ret = 0;
707
708 /* Skip measurement if EFI is going to do it */
709 if (images->os.os == IH_OS_EFI &&
710 IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL) &&
711 IS_ENABLED(CONFIG_BOOTM_EFI))
712 return ret;
713
714 if (IS_ENABLED(CONFIG_MEASURED_BOOT)) {
715 struct tcg2_event_log elog;
716 struct udevice *dev;
717 void *initrd_buf;
718 void *image_buf;
719 const char *s;
720 u32 rd_len;
721 bool ign;
722
723 elog.log_size = 0;
724 ign = IS_ENABLED(CONFIG_MEASURE_IGNORE_LOG);
725 ret = tcg2_measurement_init(&dev, &elog, ign);
726 if (ret)
727 return ret;
728
729 image_buf = map_sysmem(images->os.image_start,
730 images->os.image_len);
731 ret = tcg2_measure_data(dev, &elog, 8, images->os.image_len,
732 image_buf, EV_COMPACT_HASH,
733 strlen("linux") + 1, (u8 *)"linux");
734 if (ret)
735 goto unmap_image;
736
737 rd_len = images->rd_end - images->rd_start;
738 initrd_buf = map_sysmem(images->rd_start, rd_len);
739 ret = tcg2_measure_data(dev, &elog, 9, rd_len, initrd_buf,
740 EV_COMPACT_HASH, strlen("initrd") + 1,
741 (u8 *)"initrd");
742 if (ret)
743 goto unmap_initrd;
744
745 if (IS_ENABLED(CONFIG_MEASURE_DEVICETREE)) {
746 ret = tcg2_measure_data(dev, &elog, 0, images->ft_len,
747 (u8 *)images->ft_addr,
748 EV_TABLE_OF_DEVICES,
749 strlen("dts") + 1,
750 (u8 *)"dts");
751 if (ret)
752 goto unmap_initrd;
753 }
754
755 s = env_get("bootargs");
756 if (!s)
757 s = "";
758 ret = tcg2_measure_data(dev, &elog, 1, strlen(s) + 1, (u8 *)s,
759 EV_PLATFORM_CONFIG_FLAGS,
760 strlen(s) + 1, (u8 *)s);
761
762unmap_initrd:
763 unmap_sysmem(initrd_buf);
764
765unmap_image:
766 unmap_sysmem(image_buf);
767 tcg2_measurement_term(dev, &elog, ret != 0);
768 }
769
770 return ret;
771}
772
Simon Glass3b5866d2014-06-12 07:24:46 -0600773/**
774 * Execute selected states of the bootm command.
775 *
776 * Note the arguments to this state must be the first argument, Any 'bootm'
777 * or sub-command arguments must have already been taken.
778 *
779 * Note that if states contains more than one flag it MUST contain
780 * BOOTM_STATE_START, since this handles and consumes the command line args.
781 *
782 * Also note that aside from boot_os_fn functions and bootm_load_os no other
783 * functions we store the return value of in 'ret' may use a negative return
784 * value, without special handling.
785 *
786 * @param cmdtp Pointer to bootm command table entry
787 * @param flag Command flags (CMD_FLAG_...)
788 * @param argc Number of subcommand arguments (0 = no arguments)
789 * @param argv Arguments
790 * @param states Mask containing states to run (BOOTM_STATE_...)
791 * @param images Image header information
792 * @param boot_progress 1 to show boot progress, 0 to not do this
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100793 * Return: 0 if ok, something else on error. Some errors will cause this
Simon Glass3b5866d2014-06-12 07:24:46 -0600794 * function to perform a reboot! If states contains BOOTM_STATE_OS_GO
795 * then the intent is to boot an OS, so this function will not return
796 * unless the image type is standalone.
797 */
Simon Glassed38aef2020-05-10 11:40:03 -0600798int do_bootm_states(struct cmd_tbl *cmdtp, int flag, int argc,
Simon Glassdf00afa2022-09-06 20:26:50 -0600799 char *const argv[], int states, struct bootm_headers *images,
Simon Glassed38aef2020-05-10 11:40:03 -0600800 int boot_progress)
Simon Glass3b5866d2014-06-12 07:24:46 -0600801{
802 boot_os_fn *boot_fn;
803 ulong iflag = 0;
804 int ret = 0, need_boot_fn;
805
806 images->state |= states;
807
808 /*
809 * Work through the states and see how far we get. We stop on
810 * any error.
811 */
812 if (states & BOOTM_STATE_START)
813 ret = bootm_start(cmdtp, flag, argc, argv);
814
Philippe Reynesae1f2ca2022-03-28 22:57:00 +0200815 if (!ret && (states & BOOTM_STATE_PRE_LOAD))
816 ret = bootm_pre_load(cmdtp, flag, argc, argv);
817
Simon Glass3b5866d2014-06-12 07:24:46 -0600818 if (!ret && (states & BOOTM_STATE_FINDOS))
819 ret = bootm_find_os(cmdtp, flag, argc, argv);
820
Zubair Lutfullah Kakakhel31eb47d2016-09-09 09:18:58 +0100821 if (!ret && (states & BOOTM_STATE_FINDOTHER))
Simon Glass3b5866d2014-06-12 07:24:46 -0600822 ret = bootm_find_other(cmdtp, flag, argc, argv);
Simon Glass3b5866d2014-06-12 07:24:46 -0600823
Eddie James32401ba2023-10-24 10:43:50 -0500824 if (IS_ENABLED(CONFIG_MEASURED_BOOT) && !ret &&
825 (states & BOOTM_STATE_MEASURE))
826 bootm_measure(images);
827
Simon Glass3b5866d2014-06-12 07:24:46 -0600828 /* Load the OS */
829 if (!ret && (states & BOOTM_STATE_LOADOS)) {
Simon Glass3b5866d2014-06-12 07:24:46 -0600830 iflag = bootm_disable_interrupts();
Tom Rini6c96f5d2018-05-01 12:32:37 -0400831 ret = bootm_load_os(images, 0);
832 if (ret && ret != BOOTM_ERR_OVERLAP)
Simon Glass3b5866d2014-06-12 07:24:46 -0600833 goto err;
834 else if (ret == BOOTM_ERR_OVERLAP)
835 ret = 0;
Simon Glass3b5866d2014-06-12 07:24:46 -0600836 }
837
838 /* Relocate the ramdisk */
839#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
840 if (!ret && (states & BOOTM_STATE_RAMDISK)) {
841 ulong rd_len = images->rd_end - images->rd_start;
842
843 ret = boot_ramdisk_high(&images->lmb, images->rd_start,
844 rd_len, &images->initrd_start, &images->initrd_end);
845 if (!ret) {
Simon Glass4d949a22017-08-03 12:22:10 -0600846 env_set_hex("initrd_start", images->initrd_start);
847 env_set_hex("initrd_end", images->initrd_end);
Simon Glass3b5866d2014-06-12 07:24:46 -0600848 }
849 }
850#endif
Simon Glass85c057e2021-09-25 19:43:21 -0600851#if CONFIG_IS_ENABLED(OF_LIBFDT) && defined(CONFIG_LMB)
Simon Glass3b5866d2014-06-12 07:24:46 -0600852 if (!ret && (states & BOOTM_STATE_FDT)) {
853 boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr);
854 ret = boot_relocate_fdt(&images->lmb, &images->ft_addr,
855 &images->ft_len);
856 }
857#endif
858
859 /* From now on, we need the OS boot function */
860 if (ret)
861 return ret;
862 boot_fn = bootm_os_get_boot_func(images->os.os);
863 need_boot_fn = states & (BOOTM_STATE_OS_CMDLINE |
864 BOOTM_STATE_OS_BD_T | BOOTM_STATE_OS_PREP |
865 BOOTM_STATE_OS_FAKE_GO | BOOTM_STATE_OS_GO);
866 if (boot_fn == NULL && need_boot_fn) {
867 if (iflag)
868 enable_interrupts();
869 printf("ERROR: booting os '%s' (%d) is not supported\n",
870 genimg_get_os_name(images->os.os), images->os.os);
871 bootstage_error(BOOTSTAGE_ID_CHECK_BOOT_OS);
872 return 1;
873 }
874
Hector Palacios8f9cc4c2016-07-11 12:34:37 +0200875
Simon Glass3b5866d2014-06-12 07:24:46 -0600876 /* Call various other states that are not generally used */
877 if (!ret && (states & BOOTM_STATE_OS_CMDLINE))
878 ret = boot_fn(BOOTM_STATE_OS_CMDLINE, argc, argv, images);
879 if (!ret && (states & BOOTM_STATE_OS_BD_T))
880 ret = boot_fn(BOOTM_STATE_OS_BD_T, argc, argv, images);
Hector Palacios8f9cc4c2016-07-11 12:34:37 +0200881 if (!ret && (states & BOOTM_STATE_OS_PREP)) {
Simon Glass529e2082020-11-05 10:33:48 -0700882 ret = bootm_process_cmdline_env(images->os.os == IH_OS_LINUX);
Simon Glassd87002f2020-11-05 10:33:41 -0700883 if (ret) {
884 printf("Cmdline setup failed (err=%d)\n", ret);
885 ret = CMD_RET_FAILURE;
886 goto err;
Simon Glass2eab0172020-11-05 10:33:39 -0700887 }
Simon Glass3b5866d2014-06-12 07:24:46 -0600888 ret = boot_fn(BOOTM_STATE_OS_PREP, argc, argv, images);
Hector Palacios8f9cc4c2016-07-11 12:34:37 +0200889 }
Simon Glass3b5866d2014-06-12 07:24:46 -0600890
891#ifdef CONFIG_TRACE
892 /* Pretend to run the OS, then run a user command */
893 if (!ret && (states & BOOTM_STATE_OS_FAKE_GO)) {
Simon Glass64b723f2017-08-03 12:22:12 -0600894 char *cmd_list = env_get("fakegocmd");
Simon Glass3b5866d2014-06-12 07:24:46 -0600895
896 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_FAKE_GO,
897 images, boot_fn);
898 if (!ret && cmd_list)
899 ret = run_command_list(cmd_list, -1, flag);
900 }
901#endif
902
903 /* Check for unsupported subcommand. */
904 if (ret) {
Simon Glassb90cf902022-10-11 09:47:07 -0600905 printf("subcommand failed (err=%d)\n", ret);
Simon Glass3b5866d2014-06-12 07:24:46 -0600906 return ret;
907 }
908
909 /* Now run the OS! We hope this doesn't return */
910 if (!ret && (states & BOOTM_STATE_OS_GO))
911 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_GO,
912 images, boot_fn);
913
914 /* Deal with any fallout */
915err:
916 if (iflag)
917 enable_interrupts();
918
919 if (ret == BOOTM_ERR_UNIMPLEMENTED)
920 bootstage_error(BOOTSTAGE_ID_DECOMP_UNIMPL);
921 else if (ret == BOOTM_ERR_RESET)
922 do_reset(cmdtp, flag, argc, argv);
923
924 return ret;
925}
926
Simon Glass18700262023-07-30 11:17:02 -0600927int bootm_boot_start(ulong addr, const char *cmdline)
928{
929 static struct cmd_tbl cmd = {"bootm"};
930 char addr_str[30];
931 char *argv[] = {addr_str, NULL};
932 int states;
933 int ret;
934
935 /*
936 * TODO(sjg@chromium.org): This uses the command-line interface, but
937 * should not. To clean this up, the various bootm states need to be
938 * passed an info structure instead of cmdline flags. Then this can
939 * set up the required info and move through the states without needing
940 * the command line.
941 */
942 states = BOOTM_STATE_START | BOOTM_STATE_FINDOS | BOOTM_STATE_PRE_LOAD |
943 BOOTM_STATE_FINDOTHER | BOOTM_STATE_LOADOS |
944 BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
945 BOOTM_STATE_OS_GO;
946 if (IS_ENABLED(CONFIG_SYS_BOOT_RAMDISK_HIGH))
947 states |= BOOTM_STATE_RAMDISK;
948 if (IS_ENABLED(CONFIG_PPC) || IS_ENABLED(CONFIG_MIPS))
949 states |= BOOTM_STATE_OS_CMDLINE;
950 images.state |= states;
951
952 snprintf(addr_str, sizeof(addr_str), "%lx", addr);
953
954 ret = env_set("bootargs", cmdline);
955 if (ret) {
956 printf("Failed to set cmdline\n");
957 return ret;
958 }
959 ret = do_bootm_states(&cmd, 0, 1, argv, states, &images, 1);
960
961 return ret;
962}
963
Tom Rinic220bd92019-05-23 07:14:07 -0400964#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Simon Glass3b5866d2014-06-12 07:24:46 -0600965/**
966 * image_get_kernel - verify legacy format kernel image
967 * @img_addr: in RAM address of the legacy format image to be verified
968 * @verify: data CRC verification flag
969 *
970 * image_get_kernel() verifies legacy image integrity and returns pointer to
971 * legacy image header if image verification was completed successfully.
972 *
973 * returns:
974 * pointer to a legacy image header if valid image was found
975 * otherwise return NULL
976 */
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600977static struct legacy_img_hdr *image_get_kernel(ulong img_addr, int verify)
Simon Glass3b5866d2014-06-12 07:24:46 -0600978{
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600979 struct legacy_img_hdr *hdr = (struct legacy_img_hdr *)img_addr;
Simon Glass3b5866d2014-06-12 07:24:46 -0600980
981 if (!image_check_magic(hdr)) {
982 puts("Bad Magic Number\n");
983 bootstage_error(BOOTSTAGE_ID_CHECK_MAGIC);
984 return NULL;
985 }
986 bootstage_mark(BOOTSTAGE_ID_CHECK_HEADER);
987
988 if (!image_check_hcrc(hdr)) {
989 puts("Bad Header Checksum\n");
990 bootstage_error(BOOTSTAGE_ID_CHECK_HEADER);
991 return NULL;
992 }
993
994 bootstage_mark(BOOTSTAGE_ID_CHECK_CHECKSUM);
995 image_print_contents(hdr);
996
997 if (verify) {
998 puts(" Verifying Checksum ... ");
999 if (!image_check_dcrc(hdr)) {
1000 printf("Bad Data CRC\n");
1001 bootstage_error(BOOTSTAGE_ID_CHECK_CHECKSUM);
1002 return NULL;
1003 }
1004 puts("OK\n");
1005 }
1006 bootstage_mark(BOOTSTAGE_ID_CHECK_ARCH);
1007
1008 if (!image_check_target_arch(hdr)) {
1009 printf("Unsupported Architecture 0x%x\n", image_get_arch(hdr));
1010 bootstage_error(BOOTSTAGE_ID_CHECK_ARCH);
1011 return NULL;
1012 }
1013 return hdr;
1014}
1015#endif
1016
1017/**
1018 * boot_get_kernel - find kernel image
1019 * @os_data: pointer to a ulong variable, will hold os data start address
1020 * @os_len: pointer to a ulong variable, will hold os data length
1021 *
1022 * boot_get_kernel() tries to find a kernel image, verifies its integrity
1023 * and locates kernel data.
1024 *
1025 * returns:
1026 * pointer to image header if valid image was found, plus kernel start
1027 * address and length, otherwise NULL
1028 */
Simon Glassed38aef2020-05-10 11:40:03 -06001029static const void *boot_get_kernel(struct cmd_tbl *cmdtp, int flag, int argc,
Simon Glassdf00afa2022-09-06 20:26:50 -06001030 char *const argv[], struct bootm_headers *images,
Simon Glass3b5866d2014-06-12 07:24:46 -06001031 ulong *os_data, ulong *os_len)
1032{
Tom Rinic220bd92019-05-23 07:14:07 -04001033#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Simon Glassbb7d3bb2022-09-06 20:26:52 -06001034 struct legacy_img_hdr *hdr;
Simon Glass3b5866d2014-06-12 07:24:46 -06001035#endif
1036 ulong img_addr;
1037 const void *buf;
Simon Glass3b5866d2014-06-12 07:24:46 -06001038 const char *fit_uname_config = NULL;
1039 const char *fit_uname_kernel = NULL;
Simon Glasse719d3b2021-09-25 19:43:20 -06001040#if CONFIG_IS_ENABLED(FIT)
Simon Glass3b5866d2014-06-12 07:24:46 -06001041 int os_noffset;
1042#endif
1043
Safae Ouajih51c981b2023-02-06 00:50:17 +01001044#ifdef CONFIG_ANDROID_BOOT_IMAGE
1045 const void *boot_img;
1046 const void *vendor_boot_img;
1047#endif
Bryan Wuc82ea372014-08-15 16:51:39 -07001048 img_addr = genimg_get_kernel_addr_fit(argc < 1 ? NULL : argv[0],
1049 &fit_uname_config,
1050 &fit_uname_kernel);
Simon Glass3b5866d2014-06-12 07:24:46 -06001051
Simon Glass88b6f922023-02-05 15:36:24 -07001052 if (IS_ENABLED(CONFIG_CMD_BOOTM_PRE_LOAD))
Philippe Reynesae1f2ca2022-03-28 22:57:00 +02001053 img_addr += image_load_offset;
1054
Simon Glass3b5866d2014-06-12 07:24:46 -06001055 bootstage_mark(BOOTSTAGE_ID_CHECK_MAGIC);
1056
Simon Glass3b5866d2014-06-12 07:24:46 -06001057 /* check image type, for FIT images get FIT kernel node */
1058 *os_data = *os_len = 0;
1059 buf = map_sysmem(img_addr, 0);
1060 switch (genimg_get_format(buf)) {
Tom Rinic220bd92019-05-23 07:14:07 -04001061#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Simon Glass3b5866d2014-06-12 07:24:46 -06001062 case IMAGE_FORMAT_LEGACY:
1063 printf("## Booting kernel from Legacy Image at %08lx ...\n",
1064 img_addr);
1065 hdr = image_get_kernel(img_addr, images->verify);
1066 if (!hdr)
1067 return NULL;
1068 bootstage_mark(BOOTSTAGE_ID_CHECK_IMAGETYPE);
1069
1070 /* get os_data and os_len */
1071 switch (image_get_type(hdr)) {
1072 case IH_TYPE_KERNEL:
1073 case IH_TYPE_KERNEL_NOLOAD:
1074 *os_data = image_get_data(hdr);
1075 *os_len = image_get_data_size(hdr);
1076 break;
1077 case IH_TYPE_MULTI:
1078 image_multi_getimg(hdr, 0, os_data, os_len);
1079 break;
1080 case IH_TYPE_STANDALONE:
1081 *os_data = image_get_data(hdr);
1082 *os_len = image_get_data_size(hdr);
1083 break;
1084 default:
1085 printf("Wrong Image Type for %s command\n",
1086 cmdtp->name);
1087 bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE);
1088 return NULL;
1089 }
1090
1091 /*
1092 * copy image header to allow for image overwrites during
1093 * kernel decompression.
1094 */
1095 memmove(&images->legacy_hdr_os_copy, hdr,
Simon Glassbb7d3bb2022-09-06 20:26:52 -06001096 sizeof(struct legacy_img_hdr));
Simon Glass3b5866d2014-06-12 07:24:46 -06001097
1098 /* save pointer to image header */
1099 images->legacy_hdr_os = hdr;
1100
1101 images->legacy_hdr_valid = 1;
1102 bootstage_mark(BOOTSTAGE_ID_DECOMP_IMAGE);
1103 break;
1104#endif
Simon Glasse719d3b2021-09-25 19:43:20 -06001105#if CONFIG_IS_ENABLED(FIT)
Simon Glass3b5866d2014-06-12 07:24:46 -06001106 case IMAGE_FORMAT_FIT:
Simon Glassa0c0b632014-06-12 07:24:47 -06001107 os_noffset = fit_image_load(images, img_addr,
Simon Glass3b5866d2014-06-12 07:24:46 -06001108 &fit_uname_kernel, &fit_uname_config,
1109 IH_ARCH_DEFAULT, IH_TYPE_KERNEL,
1110 BOOTSTAGE_ID_FIT_KERNEL_START,
1111 FIT_LOAD_IGNORED, os_data, os_len);
1112 if (os_noffset < 0)
1113 return NULL;
1114
1115 images->fit_hdr_os = map_sysmem(img_addr, 0);
1116 images->fit_uname_os = fit_uname_kernel;
1117 images->fit_uname_cfg = fit_uname_config;
1118 images->fit_noffset_os = os_noffset;
1119 break;
1120#endif
1121#ifdef CONFIG_ANDROID_BOOT_IMAGE
1122 case IMAGE_FORMAT_ANDROID:
Safae Ouajih51c981b2023-02-06 00:50:17 +01001123 boot_img = buf;
1124 vendor_boot_img = NULL;
1125 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
1126 boot_img = map_sysmem(get_abootimg_addr(), 0);
1127 vendor_boot_img = map_sysmem(get_avendor_bootimg_addr(), 0);
1128 }
Simon Glass3b5866d2014-06-12 07:24:46 -06001129 printf("## Booting Android Image at 0x%08lx ...\n", img_addr);
Safae Ouajih51c981b2023-02-06 00:50:17 +01001130 if (android_image_get_kernel(boot_img, vendor_boot_img, images->verify,
Simon Glass3b5866d2014-06-12 07:24:46 -06001131 os_data, os_len))
1132 return NULL;
Safae Ouajih51c981b2023-02-06 00:50:17 +01001133 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
1134 unmap_sysmem(vendor_boot_img);
1135 unmap_sysmem(boot_img);
1136 }
Simon Glass3b5866d2014-06-12 07:24:46 -06001137 break;
1138#endif
1139 default:
1140 printf("Wrong Image Format for %s command\n", cmdtp->name);
1141 bootstage_error(BOOTSTAGE_ID_FIT_KERNEL_INFO);
1142 return NULL;
1143 }
1144
1145 debug(" kernel data at 0x%08lx, len = 0x%08lx (%ld)\n",
1146 *os_data, *os_len, *os_len);
1147
1148 return buf;
1149}
Heinrich Schuchardtaa0b11b2019-01-08 18:13:06 +01001150
1151/**
1152 * switch_to_non_secure_mode() - switch to non-secure mode
1153 *
1154 * This routine is overridden by architectures requiring this feature.
1155 */
1156void __weak switch_to_non_secure_mode(void)
1157{
1158}
1159
Simon Glassa51991d2014-06-12 07:24:53 -06001160#else /* USE_HOSTCC */
1161
Fabrice Fontained00d6b52019-05-03 22:37:05 +02001162#if defined(CONFIG_FIT_SIGNATURE)
Simon Glass81d13872020-03-18 11:44:02 -06001163static int bootm_host_load_image(const void *fit, int req_image_type,
1164 int cfg_noffset)
Simon Glassa51991d2014-06-12 07:24:53 -06001165{
1166 const char *fit_uname_config = NULL;
1167 ulong data, len;
Simon Glassdf00afa2022-09-06 20:26:50 -06001168 struct bootm_headers images;
Simon Glassa51991d2014-06-12 07:24:53 -06001169 int noffset;
Tom Rinif3c2f992022-06-25 19:29:46 -04001170 ulong load_end, buf_size;
Simon Glassa51991d2014-06-12 07:24:53 -06001171 uint8_t image_type;
Daniel Golle905d4b72022-08-27 04:14:42 +01001172 uint8_t image_comp;
Simon Glassa51991d2014-06-12 07:24:53 -06001173 void *load_buf;
1174 int ret;
1175
Simon Glass81d13872020-03-18 11:44:02 -06001176 fit_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
Simon Glassa51991d2014-06-12 07:24:53 -06001177 memset(&images, '\0', sizeof(images));
1178 images.verify = 1;
1179 noffset = fit_image_load(&images, (ulong)fit,
1180 NULL, &fit_uname_config,
1181 IH_ARCH_DEFAULT, req_image_type, -1,
1182 FIT_LOAD_IGNORED, &data, &len);
1183 if (noffset < 0)
1184 return noffset;
1185 if (fit_image_get_type(fit, noffset, &image_type)) {
1186 puts("Can't get image type!\n");
1187 return -EINVAL;
1188 }
1189
Daniel Golle2ebfd222022-08-27 04:17:28 +01001190 if (fit_image_get_comp(fit, noffset, &image_comp))
1191 image_comp = IH_COMP_NONE;
Simon Glassa51991d2014-06-12 07:24:53 -06001192
1193 /* Allow the image to expand by a factor of 4, should be safe */
Tom Rinif3c2f992022-06-25 19:29:46 -04001194 buf_size = (1 << 20) + len * 4;
1195 load_buf = malloc(buf_size);
Daniel Golle905d4b72022-08-27 04:14:42 +01001196 ret = image_decomp(image_comp, 0, data, image_type, load_buf,
Tom Rinif3c2f992022-06-25 19:29:46 -04001197 (void *)data, len, buf_size, &load_end);
Simon Glassa51991d2014-06-12 07:24:53 -06001198 free(load_buf);
Simon Glass5aeccc22014-12-02 13:17:33 -07001199
Julius Werner47ef9862019-07-24 19:37:54 -07001200 if (ret) {
Daniel Golle905d4b72022-08-27 04:14:42 +01001201 ret = handle_decomp_error(image_comp, load_end - 0, buf_size, ret);
Julius Werner47ef9862019-07-24 19:37:54 -07001202 if (ret != BOOTM_ERR_UNIMPLEMENTED)
1203 return ret;
1204 }
Simon Glassa51991d2014-06-12 07:24:53 -06001205
1206 return 0;
1207}
1208
1209int bootm_host_load_images(const void *fit, int cfg_noffset)
1210{
1211 static uint8_t image_types[] = {
1212 IH_TYPE_KERNEL,
1213 IH_TYPE_FLATDT,
1214 IH_TYPE_RAMDISK,
1215 };
1216 int err = 0;
1217 int i;
1218
1219 for (i = 0; i < ARRAY_SIZE(image_types); i++) {
1220 int ret;
1221
Simon Glass81d13872020-03-18 11:44:02 -06001222 ret = bootm_host_load_image(fit, image_types[i], cfg_noffset);
Simon Glassa51991d2014-06-12 07:24:53 -06001223 if (!err && ret && ret != -ENOENT)
1224 err = ret;
1225 }
1226
1227 /* Return the first error we found */
1228 return err;
1229}
Fabrice Fontained00d6b52019-05-03 22:37:05 +02001230#endif
Simon Glass0232ba72014-06-12 07:24:51 -06001231
1232#endif /* ndef USE_HOSTCC */