blob: cb61485c226cb213c7ed55a76177446cd5fbacb5 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassb6396402014-06-12 07:24:46 -06002/*
3 * (C) Copyright 2000-2009
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Simon Glassb6396402014-06-12 07:24:46 -06005 */
6
Simon Glassea51a622014-06-12 07:24:51 -06007#ifndef USE_HOSTCC
Simon Glassb6396402014-06-12 07:24:46 -06008#include <common.h>
Simon Glassea51a622014-06-12 07:24:51 -06009#include <bootstage.h>
Simon Glass51bb3382020-11-05 10:33:48 -070010#include <cli.h>
Simon Glassbe595142023-09-27 08:22:37 -060011#include <command.h>
Simon Glass1eb69ae2019-11-14 12:57:39 -070012#include <cpu_func.h>
Simon Glassc7694dd2019-08-01 09:46:46 -060013#include <env.h>
Simon Glass90268b82014-10-19 21:11:24 -060014#include <errno.h>
Simon Glassb6396402014-06-12 07:24:46 -060015#include <fdt_support.h>
Simon Glass36bf4462019-11-14 12:57:42 -070016#include <irq_func.h>
Simon Glassb6396402014-06-12 07:24:46 -060017#include <lmb.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060018#include <log.h>
Simon Glassb6396402014-06-12 07:24:46 -060019#include <malloc.h>
Joe Hershberger0eb25b62015-03-22 17:08:59 -050020#include <mapmem.h>
Simon Glass90526e92020-05-10 11:39:56 -060021#include <net.h>
22#include <asm/cache.h>
Simon Glass401d1c42020-10-30 21:38:53 -060023#include <asm/global_data.h>
Simon Glassb6396402014-06-12 07:24:46 -060024#include <asm/io.h>
Simon Glassb6386f32020-11-05 10:33:43 -070025#include <linux/sizes.h>
Eddie Jamesdec166d2023-10-24 10:43:50 -050026#include <tpm-v2.h>
Simon Glassb6396402014-06-12 07:24:46 -060027#if defined(CONFIG_CMD_USB)
28#include <usb.h>
29#endif
Simon Glassea51a622014-06-12 07:24:51 -060030#else
31#include "mkimage.h"
32#endif
Simon Glassb6396402014-06-12 07:24:46 -060033
Simon Glassea51a622014-06-12 07:24:51 -060034#include <bootm.h>
35#include <image.h>
Simon Glassb6396402014-06-12 07:24:46 -060036
Simon Glassb6386f32020-11-05 10:33:43 -070037#define MAX_CMDLINE_SIZE SZ_4K
38
Simon Glassb6396402014-06-12 07:24:46 -060039#define IH_INITRD_ARCH IH_ARCH_DEFAULT
40
Simon Glassea51a622014-06-12 07:24:51 -060041#ifndef USE_HOSTCC
42
43DECLARE_GLOBAL_DATA_PTR;
44
Simon Glassd9d7c202022-09-06 20:26:50 -060045struct bootm_headers images; /* pointers to os/initrd/fdt images */
Tom Rini5db28902016-08-12 08:31:15 -040046
Simon Glass09140112020-05-10 11:40:03 -060047static const void *boot_get_kernel(struct cmd_tbl *cmdtp, int flag, int argc,
Simon Glassd9d7c202022-09-06 20:26:50 -060048 char *const argv[], struct bootm_headers *images,
Simon Glassb6396402014-06-12 07:24:46 -060049 ulong *os_data, ulong *os_len);
50
Simon Glass329da482018-05-16 09:42:25 -060051__weak void board_quiesce_devices(void)
52{
53}
54
Simon Glassb6396402014-06-12 07:24:46 -060055#ifdef CONFIG_LMB
Simon Glassd9d7c202022-09-06 20:26:50 -060056static void boot_start_lmb(struct bootm_headers *images)
Simon Glassb6396402014-06-12 07:24:46 -060057{
58 ulong mem_start;
59 phys_size_t mem_size;
60
Simon Glass723806c2017-08-03 12:22:15 -060061 mem_start = env_get_bootm_low();
62 mem_size = env_get_bootm_size();
Simon Glassb6396402014-06-12 07:24:46 -060063
Simon Goldschmidt9cc23232019-01-26 22:13:04 +010064 lmb_init_and_reserve_range(&images->lmb, (phys_addr_t)mem_start,
65 mem_size, NULL);
Simon Glassb6396402014-06-12 07:24:46 -060066}
67#else
68#define lmb_reserve(lmb, base, size)
Simon Glassd9d7c202022-09-06 20:26:50 -060069static inline void boot_start_lmb(struct bootm_headers *images) { }
Simon Glassb6396402014-06-12 07:24:46 -060070#endif
71
Simon Glass09140112020-05-10 11:40:03 -060072static int bootm_start(struct cmd_tbl *cmdtp, int flag, int argc,
73 char *const argv[])
Simon Glassb6396402014-06-12 07:24:46 -060074{
75 memset((void *)&images, 0, sizeof(images));
Simon Glassbfebc8c2017-08-03 12:22:13 -060076 images.verify = env_get_yesno("verify");
Simon Glassb6396402014-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 Reynes9d46e632022-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 Glass494bcf12023-02-05 15:36:24 -0700104 if (IS_ENABLED(CONFIG_CMD_BOOTM_PRE_LOAD))
Philippe Reynes9d46e632022-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 Glass09140112020-05-10 11:40:03 -0600113static int bootm_find_os(struct cmd_tbl *cmdtp, int flag, int argc,
114 char *const argv[])
Simon Glassb6396402014-06-12 07:24:46 -0600115{
116 const void *os_hdr;
Safae Ouajih636da202023-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 Glassb6396402014-06-12 07:24:46 -0600121 bool ep_found = false;
Simon Glass90268b82014-10-19 21:11:24 -0600122 int ret;
Simon Glassb6396402014-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 Rinic76c93a2019-05-23 07:14:07 -0400134#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Simon Glassb6396402014-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 Glass90268b82014-10-19 21:11:24 -0600142 images.os.arch = image_get_arch(os_hdr);
Simon Glassb6396402014-06-12 07:24:46 -0600143 break;
144#endif
Simon Glassbf371b42021-09-25 19:43:20 -0600145#if CONFIG_IS_ENABLED(FIT)
Simon Glassb6396402014-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 Glass90268b82014-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 Glassb6396402014-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 Ouajih636da202023-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 Glassb6396402014-06-12 07:24:46 -0600195 images.os.type = IH_TYPE_KERNEL;
Safae Ouajih636da202023-02-06 00:50:17 +0100196 images.os.comp = android_image_get_kcomp(boot_img, vendor_boot_img);
Simon Glassb6396402014-06-12 07:24:46 -0600197 images.os.os = IH_OS_LINUX;
Safae Ouajih636da202023-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 Draidi86f46952014-10-23 20:50:07 +0300200 images.ep = images.os.load;
201 ep_found = true;
Safae Ouajih636da202023-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 Glassb6396402014-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 Glass90268b82014-10-19 21:11:24 -0600213 /* If we have a valid setup.bin, we will use that for entry (x86) */
Simon Glass5bda35c2014-10-10 08:21:57 -0600214 if (images.os.arch == IH_ARCH_I386 ||
215 images.os.arch == IH_ARCH_X86_64) {
Simon Glass90268b82014-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 Glassb6396402014-06-12 07:24:46 -0600225 images.ep = image_get_ep(&images.legacy_hdr_os_copy);
Simon Glassbf371b42021-09-25 19:43:20 -0600226#if CONFIG_IS_ENABLED(FIT)
Simon Glassb6396402014-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 Glassef65aa32023-02-05 15:36:23 -0700243 if (IS_ENABLED(CONFIG_CMD_BOOTI) &&
Heinrich Schuchardt4533b3d2023-06-13 08:18:27 +0200244 images.os.arch == IH_ARCH_ARM64 &&
245 images.os.os == IH_OS_LINUX) {
Marek Vasut487b5fa2018-06-13 06:13:33 +0200246 ulong image_addr;
247 ulong image_size;
248
249 ret = booti_setup(images.os.image_start, &image_addr,
250 &image_size, true);
251 if (ret != 0)
252 return 1;
253
254 images.os.type = IH_TYPE_KERNEL;
255 images.os.load = image_addr;
256 images.ep = image_addr;
257 } else {
258 images.os.load = images.os.image_start;
259 images.ep += images.os.image_start;
260 }
Simon Glassb6396402014-06-12 07:24:46 -0600261 }
262
Simon Glass7a80de42016-02-24 09:14:42 -0700263 images.os.start = map_to_sysmem(os_hdr);
Simon Glassb6396402014-06-12 07:24:46 -0600264
265 return 0;
266}
267
Karl Apsited52e8572015-05-21 09:52:49 -0400268/**
269 * bootm_find_images - wrapper to find and locate various images
270 * @flag: Ignored Argument
271 * @argc: command argument count
272 * @argv: command argument list
Tero Kristofbde7582020-06-12 15:41:20 +0300273 * @start: OS image start address
274 * @size: OS image size
Karl Apsited52e8572015-05-21 09:52:49 -0400275 *
276 * boot_find_images() will attempt to load an available ramdisk,
277 * flattened device tree, as well as specifically marked
278 * "loadable" images (loadables are FIT only)
279 *
280 * Note: bootm_find_images will skip an image if it is not found
281 *
282 * @return:
283 * 0, if all existing images were loaded correctly
284 * 1, if an image is found but corrupted, or invalid
285 */
Tero Kristofbde7582020-06-12 15:41:20 +0300286int bootm_find_images(int flag, int argc, char *const argv[], ulong start,
287 ulong size)
Simon Glassb6396402014-06-12 07:24:46 -0600288{
289 int ret;
290
291 /* find ramdisk */
292 ret = boot_get_ramdisk(argc, argv, &images, IH_INITRD_ARCH,
293 &images.rd_start, &images.rd_end);
294 if (ret) {
295 puts("Ramdisk image is corrupt or invalid\n");
296 return 1;
297 }
298
Tero Kristofbde7582020-06-12 15:41:20 +0300299 /* check if ramdisk overlaps OS image */
300 if (images.rd_start && (((ulong)images.rd_start >= start &&
Jaehoon Chungef4f4f12020-10-21 14:17:03 +0900301 (ulong)images.rd_start < start + size) ||
302 ((ulong)images.rd_end > start &&
303 (ulong)images.rd_end <= start + size) ||
304 ((ulong)images.rd_start < start &&
305 (ulong)images.rd_end >= start + size))) {
Tero Kristofbde7582020-06-12 15:41:20 +0300306 printf("ERROR: RD image overlaps OS image (OS=0x%lx..0x%lx)\n",
307 start, start + size);
308 return 1;
309 }
310
Simon Glass0c303f92021-09-25 19:43:21 -0600311#if CONFIG_IS_ENABLED(OF_LIBFDT)
Simon Glassb6396402014-06-12 07:24:46 -0600312 /* find flattened device tree */
313 ret = boot_get_fdt(flag, argc, argv, IH_ARCH_DEFAULT, &images,
314 &images.ft_addr, &images.ft_len);
315 if (ret) {
316 puts("Could not find a valid device tree\n");
317 return 1;
318 }
Tero Kristofbde7582020-06-12 15:41:20 +0300319
320 /* check if FDT overlaps OS image */
321 if (images.ft_addr &&
322 (((ulong)images.ft_addr >= start &&
Pali Rohár5acfdfb2022-08-27 14:48:10 +0200323 (ulong)images.ft_addr < start + size) ||
Tero Kristofbde7582020-06-12 15:41:20 +0300324 ((ulong)images.ft_addr + images.ft_len >= start &&
Pali Rohár5acfdfb2022-08-27 14:48:10 +0200325 (ulong)images.ft_addr + images.ft_len < start + size))) {
Tero Kristofbde7582020-06-12 15:41:20 +0300326 printf("ERROR: FDT image overlaps OS image (OS=0x%lx..0x%lx)\n",
327 start, start + size);
328 return 1;
329 }
330
Simon Glass3a09f382023-02-05 15:36:30 -0700331 if (IS_ENABLED(CONFIG_CMD_FDT))
Simon Goldschmidt596be5f2018-12-17 20:14:42 +0100332 set_working_fdt_addr(map_to_sysmem(images.ft_addr));
Simon Glassb6396402014-06-12 07:24:46 -0600333#endif
334
Simon Glassbf371b42021-09-25 19:43:20 -0600335#if CONFIG_IS_ENABLED(FIT)
Simon Glass4ed37ab2021-09-25 07:03:20 -0600336 if (IS_ENABLED(CONFIG_FPGA)) {
337 /* find bitstreams */
338 ret = boot_get_fpga(argc, argv, &images, IH_ARCH_DEFAULT,
339 NULL, NULL);
340 if (ret) {
341 printf("FPGA image is corrupted or invalid\n");
342 return 1;
343 }
Michal Simek62afc602016-05-17 14:03:50 +0200344 }
Michal Simek62afc602016-05-17 14:03:50 +0200345
Karl Apsite84a07db2015-05-21 09:52:48 -0400346 /* find all of the loadables */
347 ret = boot_get_loadable(argc, argv, &images, IH_ARCH_DEFAULT,
348 NULL, NULL);
349 if (ret) {
350 printf("Loadable(s) is corrupt or invalid\n");
351 return 1;
352 }
Karl Apsite84a07db2015-05-21 09:52:48 -0400353#endif
354
Simon Glassb6396402014-06-12 07:24:46 -0600355 return 0;
356}
357
Simon Glass09140112020-05-10 11:40:03 -0600358static int bootm_find_other(struct cmd_tbl *cmdtp, int flag, int argc,
359 char *const argv[])
Simon Glassb6396402014-06-12 07:24:46 -0600360{
361 if (((images.os.type == IH_TYPE_KERNEL) ||
362 (images.os.type == IH_TYPE_KERNEL_NOLOAD) ||
363 (images.os.type == IH_TYPE_MULTI)) &&
364 (images.os.os == IH_OS_LINUX ||
365 images.os.os == IH_OS_VXWORKS))
Tero Kristofbde7582020-06-12 15:41:20 +0300366 return bootm_find_images(flag, argc, argv, 0, 0);
Simon Glassb6396402014-06-12 07:24:46 -0600367
368 return 0;
369}
Simon Glass40e59752014-12-02 13:17:30 -0700370#endif /* USE_HOSTC */
371
Julius Werner20908542019-07-24 19:37:54 -0700372#if !defined(USE_HOSTCC) || defined(CONFIG_FIT_SIGNATURE)
Simon Glass3086c052014-12-02 13:17:37 -0700373/**
374 * handle_decomp_error() - display a decompression error
375 *
376 * This function tries to produce a useful message. In the case where the
377 * uncompressed size is the same as the available space, we can assume that
378 * the image is too large for the buffer.
379 *
380 * @comp_type: Compression type being used (IH_COMP_...)
381 * @uncomp_size: Number of bytes uncompressed
Tom Rinic45568c2022-06-25 19:29:46 -0400382 * @buf_size: Number of bytes the decompresion buffer was
Julius Werner20908542019-07-24 19:37:54 -0700383 * @ret: errno error code received from compression library
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100384 * Return: Appropriate BOOTM_ERR_ error code
Simon Glass3086c052014-12-02 13:17:37 -0700385 */
Tom Rinic45568c2022-06-25 19:29:46 -0400386static int handle_decomp_error(int comp_type, size_t uncomp_size,
387 size_t buf_size, int ret)
Simon Glass40e59752014-12-02 13:17:30 -0700388{
Simon Glass3086c052014-12-02 13:17:37 -0700389 const char *name = genimg_get_comp_name(comp_type);
390
Julius Werner20908542019-07-24 19:37:54 -0700391 /* ENOSYS means unimplemented compression type, don't reset. */
392 if (ret == -ENOSYS)
393 return BOOTM_ERR_UNIMPLEMENTED;
394
Tom Rinic45568c2022-06-25 19:29:46 -0400395 if (uncomp_size >= buf_size)
Simon Glass3086c052014-12-02 13:17:37 -0700396 printf("Image too large: increase CONFIG_SYS_BOOTM_LEN\n");
Simon Glass40e59752014-12-02 13:17:30 -0700397 else
Simon Glass3086c052014-12-02 13:17:37 -0700398 printf("%s: uncompress error %d\n", name, ret);
399
400 /*
401 * The decompression routines are now safe, so will not write beyond
402 * their bounds. Probably it is not necessary to reset, but maintain
403 * the current behaviour for now.
404 */
405 printf("Must RESET board to recover\n");
Simon Glass40e59752014-12-02 13:17:30 -0700406#ifndef USE_HOSTCC
407 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
408#endif
409
410 return BOOTM_ERR_RESET;
411}
Julius Werner20908542019-07-24 19:37:54 -0700412#endif
Simon Glass2b164f12014-06-12 07:24:52 -0600413
Simon Glassce1400f2014-06-12 07:24:53 -0600414#ifndef USE_HOSTCC
Simon Glassd9d7c202022-09-06 20:26:50 -0600415static int bootm_load_os(struct bootm_headers *images, int boot_progress)
Simon Glass2b164f12014-06-12 07:24:52 -0600416{
Simon Glassda79b2f2022-09-06 20:26:51 -0600417 struct image_info os = images->os;
Simon Glass2b164f12014-06-12 07:24:52 -0600418 ulong load = os.load;
Tom Rinicc955352018-05-01 12:32:37 -0400419 ulong load_end;
Simon Glass2b164f12014-06-12 07:24:52 -0600420 ulong blob_start = os.start;
421 ulong blob_end = os.end;
422 ulong image_start = os.image_start;
423 ulong image_len = os.image_len;
Bryan O'Donoghuebbac9222018-04-15 11:48:17 +0100424 ulong flush_start = ALIGN_DOWN(load, ARCH_DMA_MINALIGN);
Simon Glass2b164f12014-06-12 07:24:52 -0600425 bool no_overlap;
426 void *load_buf, *image_buf;
427 int err;
428
429 load_buf = map_sysmem(load, 0);
430 image_buf = map_sysmem(os.image_start, image_len);
Julius Werner20908542019-07-24 19:37:54 -0700431 err = image_decomp(os.comp, load, os.image_start, os.type,
432 load_buf, image_buf, image_len,
433 CONFIG_SYS_BOOTM_LEN, &load_end);
Simon Glass2b164f12014-06-12 07:24:52 -0600434 if (err) {
Tom Rinic45568c2022-06-25 19:29:46 -0400435 err = handle_decomp_error(os.comp, load_end - load,
436 CONFIG_SYS_BOOTM_LEN, err);
Simon Glass2b164f12014-06-12 07:24:52 -0600437 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
438 return err;
439 }
Heinrich Schuchardt21d39462020-08-30 11:34:12 +0200440 /* We need the decompressed image size in the next steps */
441 images->os.image_len = load_end - load;
Bryan O'Donoghuebbac9222018-04-15 11:48:17 +0100442
Trent Piephob4353b32019-03-27 23:50:09 +0000443 flush_cache(flush_start, ALIGN(load_end, ARCH_DMA_MINALIGN) - flush_start);
Simon Glassb6396402014-06-12 07:24:46 -0600444
Tom Rinicc955352018-05-01 12:32:37 -0400445 debug(" kernel loaded at 0x%08lx, end = 0x%08lx\n", load, load_end);
Simon Glassb6396402014-06-12 07:24:46 -0600446 bootstage_mark(BOOTSTAGE_ID_KERNEL_LOADED);
447
Simon Glass2b164f12014-06-12 07:24:52 -0600448 no_overlap = (os.comp == IH_COMP_NONE && load == image_start);
449
Tom Rinicc955352018-05-01 12:32:37 -0400450 if (!no_overlap && load < blob_end && load_end > blob_start) {
Simon Glassb6396402014-06-12 07:24:46 -0600451 debug("images.os.start = 0x%lX, images.os.end = 0x%lx\n",
452 blob_start, blob_end);
453 debug("images.os.load = 0x%lx, load_end = 0x%lx\n", load,
Tom Rinicc955352018-05-01 12:32:37 -0400454 load_end);
Simon Glassb6396402014-06-12 07:24:46 -0600455
456 /* Check what type of image this is. */
457 if (images->legacy_hdr_valid) {
458 if (image_get_type(&images->legacy_hdr_os_copy)
459 == IH_TYPE_MULTI)
460 puts("WARNING: legacy format multi component image overwritten\n");
461 return BOOTM_ERR_OVERLAP;
462 } else {
463 puts("ERROR: new format image overwritten - must RESET the board to recover\n");
464 bootstage_error(BOOTSTAGE_ID_OVERWRITTEN);
465 return BOOTM_ERR_RESET;
466 }
467 }
468
Tom Rinicc955352018-05-01 12:32:37 -0400469 lmb_reserve(&images->lmb, images->os.load, (load_end -
470 images->os.load));
Simon Glassb6396402014-06-12 07:24:46 -0600471 return 0;
472}
473
474/**
475 * bootm_disable_interrupts() - Disable interrupts in preparation for load/boot
476 *
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100477 * Return: interrupt flag (0 if interrupts were disabled, non-zero if they were
Simon Glassb6396402014-06-12 07:24:46 -0600478 * enabled)
479 */
480ulong bootm_disable_interrupts(void)
481{
482 ulong iflag;
483
484 /*
485 * We have reached the point of no return: we are going to
486 * overwrite all exception vector code, so we cannot easily
487 * recover from any failures any more...
488 */
489 iflag = disable_interrupts();
490#ifdef CONFIG_NETCONSOLE
491 /* Stop the ethernet stack if NetConsole could have left it up */
492 eth_halt();
Simon Glassb6396402014-06-12 07:24:46 -0600493#endif
494
495#if defined(CONFIG_CMD_USB)
496 /*
497 * turn off USB to prevent the host controller from writing to the
498 * SDRAM while Linux is booting. This could happen (at least for OHCI
499 * controller), because the HCCA (Host Controller Communication Area)
500 * lies within the SDRAM and the host controller writes continously to
501 * this area (as busmaster!). The HccaFrameNumber is for example
502 * updated every 1 ms within the HCCA structure in SDRAM! For more
503 * details see the OpenHCI specification.
504 */
505 usb_stop();
506#endif
507 return iflag;
508}
509
Simon Glass6cd92b12020-11-05 10:33:42 -0700510#define CONSOLE_ARG "console="
Sean Andersonba9aa402022-05-19 18:26:05 -0400511#define NULL_CONSOLE (CONSOLE_ARG "ttynull")
512#define CONSOLE_ARG_SIZE sizeof(NULL_CONSOLE)
Simon Glassb6396402014-06-12 07:24:46 -0600513
Simon Glassb6386f32020-11-05 10:33:43 -0700514/**
515 * fixup_silent_linux() - Handle silencing the linux boot if required
516 *
517 * This uses the silent_linux envvar to control whether to add/set a "console="
518 * parameter to the command line
519 *
520 * @buf: Buffer containing the string to process
521 * @maxlen: Maximum length of buffer
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100522 * Return: 0 if OK, -ENOSPC if @maxlen is too small
Simon Glassb6386f32020-11-05 10:33:43 -0700523 */
524static int fixup_silent_linux(char *buf, int maxlen)
Simon Glassb6396402014-06-12 07:24:46 -0600525{
Simon Glassb6396402014-06-12 07:24:46 -0600526 int want_silent;
Simon Glassb6386f32020-11-05 10:33:43 -0700527 char *cmdline;
528 int size;
Simon Glassb6396402014-06-12 07:24:46 -0600529
Simon Glassb6386f32020-11-05 10:33:43 -0700530 /*
531 * Move the input string to the end of buffer. The output string will be
532 * built up at the start.
533 */
534 size = strlen(buf) + 1;
535 if (size * 2 > maxlen)
536 return -ENOSPC;
537 cmdline = buf + maxlen - size;
538 memmove(cmdline, buf, size);
Simon Glassb6396402014-06-12 07:24:46 -0600539 /*
540 * Only fix cmdline when requested. The environment variable can be:
541 *
542 * no - we never fixup
543 * yes - we always fixup
544 * unset - we rely on the console silent flag
545 */
Simon Glassbfebc8c2017-08-03 12:22:13 -0600546 want_silent = env_get_yesno("silent_linux");
Simon Glassb6396402014-06-12 07:24:46 -0600547 if (want_silent == 0)
Simon Glass4ae42642020-11-05 10:33:39 -0700548 return 0;
Simon Glassb6396402014-06-12 07:24:46 -0600549 else if (want_silent == -1 && !(gd->flags & GD_FLG_SILENT))
Simon Glass4ae42642020-11-05 10:33:39 -0700550 return 0;
Simon Glassb6396402014-06-12 07:24:46 -0600551
552 debug("before silent fix-up: %s\n", cmdline);
Simon Glassb6386f32020-11-05 10:33:43 -0700553 if (*cmdline) {
Simon Glassb6396402014-06-12 07:24:46 -0600554 char *start = strstr(cmdline, CONSOLE_ARG);
555
Simon Glassb6386f32020-11-05 10:33:43 -0700556 /* Check space for maximum possible new command line */
557 if (size + CONSOLE_ARG_SIZE > maxlen)
Simon Glass4ae42642020-11-05 10:33:39 -0700558 return -ENOSPC;
Simon Glassb6396402014-06-12 07:24:46 -0600559
560 if (start) {
561 char *end = strchr(start, ' ');
Simon Glass6cd92b12020-11-05 10:33:42 -0700562 int start_bytes;
Simon Glassb6396402014-06-12 07:24:46 -0600563
Sean Andersonba9aa402022-05-19 18:26:05 -0400564 start_bytes = start - cmdline;
Simon Glass6cd92b12020-11-05 10:33:42 -0700565 strncpy(buf, cmdline, start_bytes);
Sean Andersonba9aa402022-05-19 18:26:05 -0400566 strncpy(buf + start_bytes, NULL_CONSOLE, CONSOLE_ARG_SIZE);
Simon Glassb6396402014-06-12 07:24:46 -0600567 if (end)
Sean Andersonba9aa402022-05-19 18:26:05 -0400568 strcpy(buf + start_bytes + CONSOLE_ARG_SIZE - 1, end);
Simon Glassb6396402014-06-12 07:24:46 -0600569 else
Sean Andersonba9aa402022-05-19 18:26:05 -0400570 buf[start_bytes + CONSOLE_ARG_SIZE] = '\0';
Simon Glassb6396402014-06-12 07:24:46 -0600571 } else {
Sean Andersonba9aa402022-05-19 18:26:05 -0400572 sprintf(buf, "%s %s", cmdline, NULL_CONSOLE);
Simon Glassb6396402014-06-12 07:24:46 -0600573 }
Simon Glassb6386f32020-11-05 10:33:43 -0700574 if (buf + strlen(buf) >= cmdline)
575 return -ENOSPC;
Simon Glassb6396402014-06-12 07:24:46 -0600576 } else {
Sean Andersonba9aa402022-05-19 18:26:05 -0400577 if (maxlen < CONSOLE_ARG_SIZE)
Simon Glassb6386f32020-11-05 10:33:43 -0700578 return -ENOSPC;
Sean Andersonba9aa402022-05-19 18:26:05 -0400579 strcpy(buf, NULL_CONSOLE);
Simon Glassb6396402014-06-12 07:24:46 -0600580 }
Simon Glassb6386f32020-11-05 10:33:43 -0700581 debug("after silent fix-up: %s\n", buf);
Simon Glassb6396402014-06-12 07:24:46 -0600582
Simon Glassb6386f32020-11-05 10:33:43 -0700583 return 0;
584}
585
Simon Glass51bb3382020-11-05 10:33:48 -0700586/**
587 * process_subst() - Handle substitution of ${...} fields in the environment
588 *
589 * Handle variable substitution in the provided buffer
590 *
591 * @buf: Buffer containing the string to process
592 * @maxlen: Maximum length of buffer
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100593 * Return: 0 if OK, -ENOSPC if @maxlen is too small
Simon Glass51bb3382020-11-05 10:33:48 -0700594 */
595static int process_subst(char *buf, int maxlen)
596{
597 char *cmdline;
598 int size;
599 int ret;
600
601 /* Move to end of buffer */
602 size = strlen(buf) + 1;
603 cmdline = buf + maxlen - size;
604 if (buf + size > cmdline)
605 return -ENOSPC;
606 memmove(cmdline, buf, size);
607
608 ret = cli_simple_process_macros(cmdline, buf, cmdline - buf);
609
610 return ret;
611}
612
Simon Glass4448fe82020-11-05 10:33:45 -0700613int bootm_process_cmdline(char *buf, int maxlen, int flags)
614{
615 int ret;
616
617 /* Check config first to enable compiler to eliminate code */
618 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) &&
619 !IS_ENABLED(CONFIG_SILENT_U_BOOT_ONLY) &&
620 (flags & BOOTM_CL_SILENT)) {
621 ret = fixup_silent_linux(buf, maxlen);
622 if (ret)
623 return log_msg_ret("silent", ret);
624 }
Simon Glassa8d69622021-03-15 18:11:23 +1300625 if (IS_ENABLED(CONFIG_BOOTARGS_SUBST) && IS_ENABLED(CONFIG_CMDLINE) &&
626 (flags & BOOTM_CL_SUBST)) {
Simon Glass51bb3382020-11-05 10:33:48 -0700627 ret = process_subst(buf, maxlen);
628 if (ret)
Simon Glass185756e2021-02-06 09:57:35 -0700629 return log_msg_ret("subst", ret);
Simon Glass51bb3382020-11-05 10:33:48 -0700630 }
Simon Glass4448fe82020-11-05 10:33:45 -0700631
632 return 0;
633}
634
Simon Glassb3c01672020-11-05 10:33:44 -0700635int bootm_process_cmdline_env(int flags)
Simon Glassb6386f32020-11-05 10:33:43 -0700636{
637 const int maxlen = MAX_CMDLINE_SIZE;
Simon Glassb3c01672020-11-05 10:33:44 -0700638 bool do_silent;
Simon Glassb6386f32020-11-05 10:33:43 -0700639 const char *env;
640 char *buf;
641 int ret;
642
643 /* First check if any action is needed */
644 do_silent = IS_ENABLED(CONFIG_SILENT_CONSOLE) &&
Simon Glassb3c01672020-11-05 10:33:44 -0700645 !IS_ENABLED(CONFIG_SILENT_U_BOOT_ONLY) && (flags & BOOTM_CL_SILENT);
Simon Glass51bb3382020-11-05 10:33:48 -0700646 if (!do_silent && !IS_ENABLED(CONFIG_BOOTARGS_SUBST))
Simon Glassb6386f32020-11-05 10:33:43 -0700647 return 0;
648
649 env = env_get("bootargs");
650 if (env && strlen(env) >= maxlen)
651 return -E2BIG;
652 buf = malloc(maxlen);
653 if (!buf)
654 return -ENOMEM;
655 if (env)
656 strcpy(buf, env);
657 else
658 *buf = '\0';
Simon Glass4448fe82020-11-05 10:33:45 -0700659 ret = bootm_process_cmdline(buf, maxlen, flags);
Simon Glassb6386f32020-11-05 10:33:43 -0700660 if (!ret) {
661 ret = env_set("bootargs", buf);
662
663 /*
664 * If buf is "" and bootargs does not exist, this will produce
665 * an error trying to delete bootargs. Ignore it
666 */
667 if (ret == -ENOENT)
668 ret = 0;
669 }
Simon Glassb6396402014-06-12 07:24:46 -0600670 free(buf);
Simon Glassb6386f32020-11-05 10:33:43 -0700671 if (ret)
672 return log_msg_ret("env", ret);
Simon Glass4ae42642020-11-05 10:33:39 -0700673
674 return 0;
Simon Glassb6396402014-06-12 07:24:46 -0600675}
Simon Glassb6396402014-06-12 07:24:46 -0600676
Eddie Jamesdec166d2023-10-24 10:43:50 -0500677int bootm_measure(struct bootm_headers *images)
678{
679 int ret = 0;
680
681 /* Skip measurement if EFI is going to do it */
682 if (images->os.os == IH_OS_EFI &&
683 IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL) &&
684 IS_ENABLED(CONFIG_BOOTM_EFI))
685 return ret;
686
687 if (IS_ENABLED(CONFIG_MEASURED_BOOT)) {
688 struct tcg2_event_log elog;
689 struct udevice *dev;
690 void *initrd_buf;
691 void *image_buf;
692 const char *s;
693 u32 rd_len;
694 bool ign;
695
696 elog.log_size = 0;
697 ign = IS_ENABLED(CONFIG_MEASURE_IGNORE_LOG);
698 ret = tcg2_measurement_init(&dev, &elog, ign);
699 if (ret)
700 return ret;
701
702 image_buf = map_sysmem(images->os.image_start,
703 images->os.image_len);
704 ret = tcg2_measure_data(dev, &elog, 8, images->os.image_len,
705 image_buf, EV_COMPACT_HASH,
706 strlen("linux") + 1, (u8 *)"linux");
707 if (ret)
708 goto unmap_image;
709
710 rd_len = images->rd_end - images->rd_start;
711 initrd_buf = map_sysmem(images->rd_start, rd_len);
712 ret = tcg2_measure_data(dev, &elog, 9, rd_len, initrd_buf,
713 EV_COMPACT_HASH, strlen("initrd") + 1,
714 (u8 *)"initrd");
715 if (ret)
716 goto unmap_initrd;
717
718 if (IS_ENABLED(CONFIG_MEASURE_DEVICETREE)) {
719 ret = tcg2_measure_data(dev, &elog, 0, images->ft_len,
720 (u8 *)images->ft_addr,
721 EV_TABLE_OF_DEVICES,
722 strlen("dts") + 1,
723 (u8 *)"dts");
724 if (ret)
725 goto unmap_initrd;
726 }
727
728 s = env_get("bootargs");
729 if (!s)
730 s = "";
731 ret = tcg2_measure_data(dev, &elog, 1, strlen(s) + 1, (u8 *)s,
732 EV_PLATFORM_CONFIG_FLAGS,
733 strlen(s) + 1, (u8 *)s);
734
735unmap_initrd:
736 unmap_sysmem(initrd_buf);
737
738unmap_image:
739 unmap_sysmem(image_buf);
740 tcg2_measurement_term(dev, &elog, ret != 0);
741 }
742
743 return ret;
744}
745
Simon Glassb6396402014-06-12 07:24:46 -0600746/**
747 * Execute selected states of the bootm command.
748 *
749 * Note the arguments to this state must be the first argument, Any 'bootm'
750 * or sub-command arguments must have already been taken.
751 *
752 * Note that if states contains more than one flag it MUST contain
753 * BOOTM_STATE_START, since this handles and consumes the command line args.
754 *
755 * Also note that aside from boot_os_fn functions and bootm_load_os no other
756 * functions we store the return value of in 'ret' may use a negative return
757 * value, without special handling.
758 *
759 * @param cmdtp Pointer to bootm command table entry
760 * @param flag Command flags (CMD_FLAG_...)
761 * @param argc Number of subcommand arguments (0 = no arguments)
762 * @param argv Arguments
763 * @param states Mask containing states to run (BOOTM_STATE_...)
764 * @param images Image header information
765 * @param boot_progress 1 to show boot progress, 0 to not do this
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100766 * Return: 0 if ok, something else on error. Some errors will cause this
Simon Glassb6396402014-06-12 07:24:46 -0600767 * function to perform a reboot! If states contains BOOTM_STATE_OS_GO
768 * then the intent is to boot an OS, so this function will not return
769 * unless the image type is standalone.
770 */
Simon Glass09140112020-05-10 11:40:03 -0600771int do_bootm_states(struct cmd_tbl *cmdtp, int flag, int argc,
Simon Glassd9d7c202022-09-06 20:26:50 -0600772 char *const argv[], int states, struct bootm_headers *images,
Simon Glass09140112020-05-10 11:40:03 -0600773 int boot_progress)
Simon Glassb6396402014-06-12 07:24:46 -0600774{
775 boot_os_fn *boot_fn;
776 ulong iflag = 0;
777 int ret = 0, need_boot_fn;
778
779 images->state |= states;
780
781 /*
782 * Work through the states and see how far we get. We stop on
783 * any error.
784 */
785 if (states & BOOTM_STATE_START)
786 ret = bootm_start(cmdtp, flag, argc, argv);
787
Philippe Reynes9d46e632022-03-28 22:57:00 +0200788 if (!ret && (states & BOOTM_STATE_PRE_LOAD))
789 ret = bootm_pre_load(cmdtp, flag, argc, argv);
790
Simon Glassb6396402014-06-12 07:24:46 -0600791 if (!ret && (states & BOOTM_STATE_FINDOS))
792 ret = bootm_find_os(cmdtp, flag, argc, argv);
793
Zubair Lutfullah Kakakhelba079842016-09-09 09:18:58 +0100794 if (!ret && (states & BOOTM_STATE_FINDOTHER))
Simon Glassb6396402014-06-12 07:24:46 -0600795 ret = bootm_find_other(cmdtp, flag, argc, argv);
Simon Glassb6396402014-06-12 07:24:46 -0600796
Eddie Jamesdec166d2023-10-24 10:43:50 -0500797 if (IS_ENABLED(CONFIG_MEASURED_BOOT) && !ret &&
798 (states & BOOTM_STATE_MEASURE))
799 bootm_measure(images);
800
Simon Glassb6396402014-06-12 07:24:46 -0600801 /* Load the OS */
802 if (!ret && (states & BOOTM_STATE_LOADOS)) {
Simon Glassb6396402014-06-12 07:24:46 -0600803 iflag = bootm_disable_interrupts();
Tom Rinicc955352018-05-01 12:32:37 -0400804 ret = bootm_load_os(images, 0);
805 if (ret && ret != BOOTM_ERR_OVERLAP)
Simon Glassb6396402014-06-12 07:24:46 -0600806 goto err;
807 else if (ret == BOOTM_ERR_OVERLAP)
808 ret = 0;
Simon Glassb6396402014-06-12 07:24:46 -0600809 }
810
811 /* Relocate the ramdisk */
812#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
813 if (!ret && (states & BOOTM_STATE_RAMDISK)) {
814 ulong rd_len = images->rd_end - images->rd_start;
815
816 ret = boot_ramdisk_high(&images->lmb, images->rd_start,
817 rd_len, &images->initrd_start, &images->initrd_end);
818 if (!ret) {
Simon Glass018f5302017-08-03 12:22:10 -0600819 env_set_hex("initrd_start", images->initrd_start);
820 env_set_hex("initrd_end", images->initrd_end);
Simon Glassb6396402014-06-12 07:24:46 -0600821 }
822 }
823#endif
Simon Glass0c303f92021-09-25 19:43:21 -0600824#if CONFIG_IS_ENABLED(OF_LIBFDT) && defined(CONFIG_LMB)
Simon Glassb6396402014-06-12 07:24:46 -0600825 if (!ret && (states & BOOTM_STATE_FDT)) {
826 boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr);
827 ret = boot_relocate_fdt(&images->lmb, &images->ft_addr,
828 &images->ft_len);
829 }
830#endif
831
832 /* From now on, we need the OS boot function */
833 if (ret)
834 return ret;
835 boot_fn = bootm_os_get_boot_func(images->os.os);
836 need_boot_fn = states & (BOOTM_STATE_OS_CMDLINE |
837 BOOTM_STATE_OS_BD_T | BOOTM_STATE_OS_PREP |
838 BOOTM_STATE_OS_FAKE_GO | BOOTM_STATE_OS_GO);
839 if (boot_fn == NULL && need_boot_fn) {
840 if (iflag)
841 enable_interrupts();
842 printf("ERROR: booting os '%s' (%d) is not supported\n",
843 genimg_get_os_name(images->os.os), images->os.os);
844 bootstage_error(BOOTSTAGE_ID_CHECK_BOOT_OS);
845 return 1;
846 }
847
Hector Palacios19e86492016-07-11 12:34:37 +0200848
Simon Glassb6396402014-06-12 07:24:46 -0600849 /* Call various other states that are not generally used */
850 if (!ret && (states & BOOTM_STATE_OS_CMDLINE))
851 ret = boot_fn(BOOTM_STATE_OS_CMDLINE, argc, argv, images);
852 if (!ret && (states & BOOTM_STATE_OS_BD_T))
853 ret = boot_fn(BOOTM_STATE_OS_BD_T, argc, argv, images);
Hector Palacios19e86492016-07-11 12:34:37 +0200854 if (!ret && (states & BOOTM_STATE_OS_PREP)) {
Simon Glass51bb3382020-11-05 10:33:48 -0700855 ret = bootm_process_cmdline_env(images->os.os == IH_OS_LINUX);
Simon Glassd9477a02020-11-05 10:33:41 -0700856 if (ret) {
857 printf("Cmdline setup failed (err=%d)\n", ret);
858 ret = CMD_RET_FAILURE;
859 goto err;
Simon Glass4ae42642020-11-05 10:33:39 -0700860 }
Simon Glassb6396402014-06-12 07:24:46 -0600861 ret = boot_fn(BOOTM_STATE_OS_PREP, argc, argv, images);
Hector Palacios19e86492016-07-11 12:34:37 +0200862 }
Simon Glassb6396402014-06-12 07:24:46 -0600863
864#ifdef CONFIG_TRACE
865 /* Pretend to run the OS, then run a user command */
866 if (!ret && (states & BOOTM_STATE_OS_FAKE_GO)) {
Simon Glass00caae62017-08-03 12:22:12 -0600867 char *cmd_list = env_get("fakegocmd");
Simon Glassb6396402014-06-12 07:24:46 -0600868
869 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_FAKE_GO,
870 images, boot_fn);
871 if (!ret && cmd_list)
872 ret = run_command_list(cmd_list, -1, flag);
873 }
874#endif
875
876 /* Check for unsupported subcommand. */
877 if (ret) {
Simon Glass13819f02022-10-11 09:47:07 -0600878 printf("subcommand failed (err=%d)\n", ret);
Simon Glassb6396402014-06-12 07:24:46 -0600879 return ret;
880 }
881
882 /* Now run the OS! We hope this doesn't return */
883 if (!ret && (states & BOOTM_STATE_OS_GO))
884 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_GO,
885 images, boot_fn);
886
887 /* Deal with any fallout */
888err:
889 if (iflag)
890 enable_interrupts();
891
892 if (ret == BOOTM_ERR_UNIMPLEMENTED)
893 bootstage_error(BOOTSTAGE_ID_DECOMP_UNIMPL);
894 else if (ret == BOOTM_ERR_RESET)
895 do_reset(cmdtp, flag, argc, argv);
896
897 return ret;
898}
899
Simon Glassdaffb0b2023-07-30 11:17:02 -0600900int bootm_boot_start(ulong addr, const char *cmdline)
901{
902 static struct cmd_tbl cmd = {"bootm"};
903 char addr_str[30];
904 char *argv[] = {addr_str, NULL};
905 int states;
906 int ret;
907
908 /*
909 * TODO(sjg@chromium.org): This uses the command-line interface, but
910 * should not. To clean this up, the various bootm states need to be
911 * passed an info structure instead of cmdline flags. Then this can
912 * set up the required info and move through the states without needing
913 * the command line.
914 */
915 states = BOOTM_STATE_START | BOOTM_STATE_FINDOS | BOOTM_STATE_PRE_LOAD |
916 BOOTM_STATE_FINDOTHER | BOOTM_STATE_LOADOS |
917 BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
918 BOOTM_STATE_OS_GO;
919 if (IS_ENABLED(CONFIG_SYS_BOOT_RAMDISK_HIGH))
920 states |= BOOTM_STATE_RAMDISK;
921 if (IS_ENABLED(CONFIG_PPC) || IS_ENABLED(CONFIG_MIPS))
922 states |= BOOTM_STATE_OS_CMDLINE;
923 images.state |= states;
924
925 snprintf(addr_str, sizeof(addr_str), "%lx", addr);
926
927 ret = env_set("bootargs", cmdline);
928 if (ret) {
929 printf("Failed to set cmdline\n");
930 return ret;
931 }
932 ret = do_bootm_states(&cmd, 0, 1, argv, states, &images, 1);
933
934 return ret;
935}
936
Tom Rinic76c93a2019-05-23 07:14:07 -0400937#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Simon Glassb6396402014-06-12 07:24:46 -0600938/**
939 * image_get_kernel - verify legacy format kernel image
940 * @img_addr: in RAM address of the legacy format image to be verified
941 * @verify: data CRC verification flag
942 *
943 * image_get_kernel() verifies legacy image integrity and returns pointer to
944 * legacy image header if image verification was completed successfully.
945 *
946 * returns:
947 * pointer to a legacy image header if valid image was found
948 * otherwise return NULL
949 */
Simon Glassf3543e62022-09-06 20:26:52 -0600950static struct legacy_img_hdr *image_get_kernel(ulong img_addr, int verify)
Simon Glassb6396402014-06-12 07:24:46 -0600951{
Simon Glassf3543e62022-09-06 20:26:52 -0600952 struct legacy_img_hdr *hdr = (struct legacy_img_hdr *)img_addr;
Simon Glassb6396402014-06-12 07:24:46 -0600953
954 if (!image_check_magic(hdr)) {
955 puts("Bad Magic Number\n");
956 bootstage_error(BOOTSTAGE_ID_CHECK_MAGIC);
957 return NULL;
958 }
959 bootstage_mark(BOOTSTAGE_ID_CHECK_HEADER);
960
961 if (!image_check_hcrc(hdr)) {
962 puts("Bad Header Checksum\n");
963 bootstage_error(BOOTSTAGE_ID_CHECK_HEADER);
964 return NULL;
965 }
966
967 bootstage_mark(BOOTSTAGE_ID_CHECK_CHECKSUM);
968 image_print_contents(hdr);
969
970 if (verify) {
971 puts(" Verifying Checksum ... ");
972 if (!image_check_dcrc(hdr)) {
973 printf("Bad Data CRC\n");
974 bootstage_error(BOOTSTAGE_ID_CHECK_CHECKSUM);
975 return NULL;
976 }
977 puts("OK\n");
978 }
979 bootstage_mark(BOOTSTAGE_ID_CHECK_ARCH);
980
981 if (!image_check_target_arch(hdr)) {
982 printf("Unsupported Architecture 0x%x\n", image_get_arch(hdr));
983 bootstage_error(BOOTSTAGE_ID_CHECK_ARCH);
984 return NULL;
985 }
986 return hdr;
987}
988#endif
989
990/**
991 * boot_get_kernel - find kernel image
992 * @os_data: pointer to a ulong variable, will hold os data start address
993 * @os_len: pointer to a ulong variable, will hold os data length
994 *
995 * boot_get_kernel() tries to find a kernel image, verifies its integrity
996 * and locates kernel data.
997 *
998 * returns:
999 * pointer to image header if valid image was found, plus kernel start
1000 * address and length, otherwise NULL
1001 */
Simon Glass09140112020-05-10 11:40:03 -06001002static const void *boot_get_kernel(struct cmd_tbl *cmdtp, int flag, int argc,
Simon Glassd9d7c202022-09-06 20:26:50 -06001003 char *const argv[], struct bootm_headers *images,
Simon Glassb6396402014-06-12 07:24:46 -06001004 ulong *os_data, ulong *os_len)
1005{
Tom Rinic76c93a2019-05-23 07:14:07 -04001006#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Simon Glassf3543e62022-09-06 20:26:52 -06001007 struct legacy_img_hdr *hdr;
Simon Glassb6396402014-06-12 07:24:46 -06001008#endif
1009 ulong img_addr;
1010 const void *buf;
Simon Glassb6396402014-06-12 07:24:46 -06001011 const char *fit_uname_config = NULL;
1012 const char *fit_uname_kernel = NULL;
Simon Glassbf371b42021-09-25 19:43:20 -06001013#if CONFIG_IS_ENABLED(FIT)
Simon Glassb6396402014-06-12 07:24:46 -06001014 int os_noffset;
1015#endif
1016
Safae Ouajih636da202023-02-06 00:50:17 +01001017#ifdef CONFIG_ANDROID_BOOT_IMAGE
1018 const void *boot_img;
1019 const void *vendor_boot_img;
1020#endif
Bryan Wue6c88a62014-08-15 16:51:39 -07001021 img_addr = genimg_get_kernel_addr_fit(argc < 1 ? NULL : argv[0],
1022 &fit_uname_config,
1023 &fit_uname_kernel);
Simon Glassb6396402014-06-12 07:24:46 -06001024
Simon Glass494bcf12023-02-05 15:36:24 -07001025 if (IS_ENABLED(CONFIG_CMD_BOOTM_PRE_LOAD))
Philippe Reynes9d46e632022-03-28 22:57:00 +02001026 img_addr += image_load_offset;
1027
Simon Glassb6396402014-06-12 07:24:46 -06001028 bootstage_mark(BOOTSTAGE_ID_CHECK_MAGIC);
1029
Simon Glassb6396402014-06-12 07:24:46 -06001030 /* check image type, for FIT images get FIT kernel node */
1031 *os_data = *os_len = 0;
1032 buf = map_sysmem(img_addr, 0);
1033 switch (genimg_get_format(buf)) {
Tom Rinic76c93a2019-05-23 07:14:07 -04001034#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Simon Glassb6396402014-06-12 07:24:46 -06001035 case IMAGE_FORMAT_LEGACY:
1036 printf("## Booting kernel from Legacy Image at %08lx ...\n",
1037 img_addr);
1038 hdr = image_get_kernel(img_addr, images->verify);
1039 if (!hdr)
1040 return NULL;
1041 bootstage_mark(BOOTSTAGE_ID_CHECK_IMAGETYPE);
1042
1043 /* get os_data and os_len */
1044 switch (image_get_type(hdr)) {
1045 case IH_TYPE_KERNEL:
1046 case IH_TYPE_KERNEL_NOLOAD:
1047 *os_data = image_get_data(hdr);
1048 *os_len = image_get_data_size(hdr);
1049 break;
1050 case IH_TYPE_MULTI:
1051 image_multi_getimg(hdr, 0, os_data, os_len);
1052 break;
1053 case IH_TYPE_STANDALONE:
1054 *os_data = image_get_data(hdr);
1055 *os_len = image_get_data_size(hdr);
1056 break;
1057 default:
1058 printf("Wrong Image Type for %s command\n",
1059 cmdtp->name);
1060 bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE);
1061 return NULL;
1062 }
1063
1064 /*
1065 * copy image header to allow for image overwrites during
1066 * kernel decompression.
1067 */
1068 memmove(&images->legacy_hdr_os_copy, hdr,
Simon Glassf3543e62022-09-06 20:26:52 -06001069 sizeof(struct legacy_img_hdr));
Simon Glassb6396402014-06-12 07:24:46 -06001070
1071 /* save pointer to image header */
1072 images->legacy_hdr_os = hdr;
1073
1074 images->legacy_hdr_valid = 1;
1075 bootstage_mark(BOOTSTAGE_ID_DECOMP_IMAGE);
1076 break;
1077#endif
Simon Glassbf371b42021-09-25 19:43:20 -06001078#if CONFIG_IS_ENABLED(FIT)
Simon Glassb6396402014-06-12 07:24:46 -06001079 case IMAGE_FORMAT_FIT:
Simon Glass126cc862014-06-12 07:24:47 -06001080 os_noffset = fit_image_load(images, img_addr,
Simon Glassb6396402014-06-12 07:24:46 -06001081 &fit_uname_kernel, &fit_uname_config,
1082 IH_ARCH_DEFAULT, IH_TYPE_KERNEL,
1083 BOOTSTAGE_ID_FIT_KERNEL_START,
1084 FIT_LOAD_IGNORED, os_data, os_len);
1085 if (os_noffset < 0)
1086 return NULL;
1087
1088 images->fit_hdr_os = map_sysmem(img_addr, 0);
1089 images->fit_uname_os = fit_uname_kernel;
1090 images->fit_uname_cfg = fit_uname_config;
1091 images->fit_noffset_os = os_noffset;
1092 break;
1093#endif
1094#ifdef CONFIG_ANDROID_BOOT_IMAGE
1095 case IMAGE_FORMAT_ANDROID:
Safae Ouajih636da202023-02-06 00:50:17 +01001096 boot_img = buf;
1097 vendor_boot_img = NULL;
1098 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
1099 boot_img = map_sysmem(get_abootimg_addr(), 0);
1100 vendor_boot_img = map_sysmem(get_avendor_bootimg_addr(), 0);
1101 }
Simon Glassb6396402014-06-12 07:24:46 -06001102 printf("## Booting Android Image at 0x%08lx ...\n", img_addr);
Safae Ouajih636da202023-02-06 00:50:17 +01001103 if (android_image_get_kernel(boot_img, vendor_boot_img, images->verify,
Simon Glassb6396402014-06-12 07:24:46 -06001104 os_data, os_len))
1105 return NULL;
Safae Ouajih636da202023-02-06 00:50:17 +01001106 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
1107 unmap_sysmem(vendor_boot_img);
1108 unmap_sysmem(boot_img);
1109 }
Simon Glassb6396402014-06-12 07:24:46 -06001110 break;
1111#endif
1112 default:
1113 printf("Wrong Image Format for %s command\n", cmdtp->name);
1114 bootstage_error(BOOTSTAGE_ID_FIT_KERNEL_INFO);
1115 return NULL;
1116 }
1117
1118 debug(" kernel data at 0x%08lx, len = 0x%08lx (%ld)\n",
1119 *os_data, *os_len, *os_len);
1120
1121 return buf;
1122}
Heinrich Schuchardtf6c6df72019-01-08 18:13:06 +01001123
1124/**
1125 * switch_to_non_secure_mode() - switch to non-secure mode
1126 *
1127 * This routine is overridden by architectures requiring this feature.
1128 */
1129void __weak switch_to_non_secure_mode(void)
1130{
1131}
1132
Simon Glassce1400f2014-06-12 07:24:53 -06001133#else /* USE_HOSTCC */
1134
Fabrice Fontaine93e07882019-05-03 22:37:05 +02001135#if defined(CONFIG_FIT_SIGNATURE)
Simon Glass8a9d0372020-03-18 11:44:02 -06001136static int bootm_host_load_image(const void *fit, int req_image_type,
1137 int cfg_noffset)
Simon Glassce1400f2014-06-12 07:24:53 -06001138{
1139 const char *fit_uname_config = NULL;
1140 ulong data, len;
Simon Glassd9d7c202022-09-06 20:26:50 -06001141 struct bootm_headers images;
Simon Glassce1400f2014-06-12 07:24:53 -06001142 int noffset;
Tom Rinic45568c2022-06-25 19:29:46 -04001143 ulong load_end, buf_size;
Simon Glassce1400f2014-06-12 07:24:53 -06001144 uint8_t image_type;
Daniel Golle0cd57f22022-08-27 04:14:42 +01001145 uint8_t image_comp;
Simon Glassce1400f2014-06-12 07:24:53 -06001146 void *load_buf;
1147 int ret;
1148
Simon Glass8a9d0372020-03-18 11:44:02 -06001149 fit_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
Simon Glassce1400f2014-06-12 07:24:53 -06001150 memset(&images, '\0', sizeof(images));
1151 images.verify = 1;
1152 noffset = fit_image_load(&images, (ulong)fit,
1153 NULL, &fit_uname_config,
1154 IH_ARCH_DEFAULT, req_image_type, -1,
1155 FIT_LOAD_IGNORED, &data, &len);
1156 if (noffset < 0)
1157 return noffset;
1158 if (fit_image_get_type(fit, noffset, &image_type)) {
1159 puts("Can't get image type!\n");
1160 return -EINVAL;
1161 }
1162
Daniel Golle88de6c52022-08-27 04:17:28 +01001163 if (fit_image_get_comp(fit, noffset, &image_comp))
1164 image_comp = IH_COMP_NONE;
Simon Glassce1400f2014-06-12 07:24:53 -06001165
1166 /* Allow the image to expand by a factor of 4, should be safe */
Tom Rinic45568c2022-06-25 19:29:46 -04001167 buf_size = (1 << 20) + len * 4;
1168 load_buf = malloc(buf_size);
Daniel Golle0cd57f22022-08-27 04:14:42 +01001169 ret = image_decomp(image_comp, 0, data, image_type, load_buf,
Tom Rinic45568c2022-06-25 19:29:46 -04001170 (void *)data, len, buf_size, &load_end);
Simon Glassce1400f2014-06-12 07:24:53 -06001171 free(load_buf);
Simon Glass081cc192014-12-02 13:17:33 -07001172
Julius Werner20908542019-07-24 19:37:54 -07001173 if (ret) {
Daniel Golle0cd57f22022-08-27 04:14:42 +01001174 ret = handle_decomp_error(image_comp, load_end - 0, buf_size, ret);
Julius Werner20908542019-07-24 19:37:54 -07001175 if (ret != BOOTM_ERR_UNIMPLEMENTED)
1176 return ret;
1177 }
Simon Glassce1400f2014-06-12 07:24:53 -06001178
1179 return 0;
1180}
1181
1182int bootm_host_load_images(const void *fit, int cfg_noffset)
1183{
1184 static uint8_t image_types[] = {
1185 IH_TYPE_KERNEL,
1186 IH_TYPE_FLATDT,
1187 IH_TYPE_RAMDISK,
1188 };
1189 int err = 0;
1190 int i;
1191
1192 for (i = 0; i < ARRAY_SIZE(image_types); i++) {
1193 int ret;
1194
Simon Glass8a9d0372020-03-18 11:44:02 -06001195 ret = bootm_host_load_image(fit, image_types[i], cfg_noffset);
Simon Glassce1400f2014-06-12 07:24:53 -06001196 if (!err && ret && ret != -ENOENT)
1197 err = ret;
1198 }
1199
1200 /* Return the first error we found */
1201 return err;
1202}
Fabrice Fontaine93e07882019-05-03 22:37:05 +02001203#endif
Simon Glassea51a622014-06-12 07:24:51 -06001204
1205#endif /* ndef USE_HOSTCC */