blob: db1466cecf2d115acb5eac9a9a737d384454c991 [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 Glass7715b312023-11-18 14:04:56 -070047__weak void board_quiesce_devices(void)
48{
49}
50
51#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
52/**
53 * image_get_kernel - verify legacy format kernel image
54 * @img_addr: in RAM address of the legacy format image to be verified
55 * @verify: data CRC verification flag
56 *
57 * image_get_kernel() verifies legacy image integrity and returns pointer to
58 * legacy image header if image verification was completed successfully.
59 *
60 * returns:
61 * pointer to a legacy image header if valid image was found
62 * otherwise return NULL
63 */
64static struct legacy_img_hdr *image_get_kernel(ulong img_addr, int verify)
65{
66 struct legacy_img_hdr *hdr = (struct legacy_img_hdr *)img_addr;
67
68 if (!image_check_magic(hdr)) {
69 puts("Bad Magic Number\n");
70 bootstage_error(BOOTSTAGE_ID_CHECK_MAGIC);
71 return NULL;
72 }
73 bootstage_mark(BOOTSTAGE_ID_CHECK_HEADER);
74
75 if (!image_check_hcrc(hdr)) {
76 puts("Bad Header Checksum\n");
77 bootstage_error(BOOTSTAGE_ID_CHECK_HEADER);
78 return NULL;
79 }
80
81 bootstage_mark(BOOTSTAGE_ID_CHECK_CHECKSUM);
82 image_print_contents(hdr);
83
84 if (verify) {
85 puts(" Verifying Checksum ... ");
86 if (!image_check_dcrc(hdr)) {
87 printf("Bad Data CRC\n");
88 bootstage_error(BOOTSTAGE_ID_CHECK_CHECKSUM);
89 return NULL;
90 }
91 puts("OK\n");
92 }
93 bootstage_mark(BOOTSTAGE_ID_CHECK_ARCH);
94
95 if (!image_check_target_arch(hdr)) {
96 printf("Unsupported Architecture 0x%x\n", image_get_arch(hdr));
97 bootstage_error(BOOTSTAGE_ID_CHECK_ARCH);
98 return NULL;
99 }
100 return hdr;
101}
102#endif
103
104/**
Simon Glass8333dd52023-11-18 14:04:58 -0700105 * boot_get_kernel() - find kernel image
106 *
Simon Glass8333dd52023-11-18 14:04:58 -0700107 * @addr_fit: first argument to bootm: address, fit configuration, etc.
Simon Glass7715b312023-11-18 14:04:56 -0700108 * @os_data: pointer to a ulong variable, will hold os data start address
109 * @os_len: pointer to a ulong variable, will hold os data length
Simon Glass8333dd52023-11-18 14:04:58 -0700110 * address and length, otherwise NULL
111 * pointer to image header if valid image was found, plus kernel start
Simon Glass9d085c12023-11-18 14:05:00 -0700112 * @kernp: image header if valid image was found, otherwise NULL
Simon Glass7715b312023-11-18 14:04:56 -0700113 *
114 * boot_get_kernel() tries to find a kernel image, verifies its integrity
115 * and locates kernel data.
116 *
Simon Glassc46784c2023-11-18 14:05:04 -0700117 * Return: 0 on success, -ve on error. -EPROTOTYPE means that the image is in
118 * a wrong or unsupported format
Simon Glass7715b312023-11-18 14:04:56 -0700119 */
Simon Glassc46784c2023-11-18 14:05:04 -0700120static int boot_get_kernel(const char *addr_fit, struct bootm_headers *images,
121 ulong *os_data, ulong *os_len, const void **kernp)
Simon Glassf55305a2018-05-16 09:42:25 -0600122{
Simon Glass7715b312023-11-18 14:04:56 -0700123#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
124 struct legacy_img_hdr *hdr;
125#endif
126 ulong img_addr;
127 const void *buf;
Simon Glass4e8ec472023-11-18 14:04:57 -0700128 const char *fit_uname_config = NULL, *fit_uname_kernel = NULL;
Simon Glass7715b312023-11-18 14:04:56 -0700129#if CONFIG_IS_ENABLED(FIT)
130 int os_noffset;
131#endif
132
133#ifdef CONFIG_ANDROID_BOOT_IMAGE
134 const void *boot_img;
135 const void *vendor_boot_img;
136#endif
Simon Glass8333dd52023-11-18 14:04:58 -0700137 img_addr = genimg_get_kernel_addr_fit(addr_fit, &fit_uname_config,
Simon Glass7715b312023-11-18 14:04:56 -0700138 &fit_uname_kernel);
139
140 if (IS_ENABLED(CONFIG_CMD_BOOTM_PRE_LOAD))
141 img_addr += image_load_offset;
142
143 bootstage_mark(BOOTSTAGE_ID_CHECK_MAGIC);
144
145 /* check image type, for FIT images get FIT kernel node */
146 *os_data = *os_len = 0;
147 buf = map_sysmem(img_addr, 0);
148 switch (genimg_get_format(buf)) {
149#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
150 case IMAGE_FORMAT_LEGACY:
151 printf("## Booting kernel from Legacy Image at %08lx ...\n",
152 img_addr);
153 hdr = image_get_kernel(img_addr, images->verify);
154 if (!hdr)
Simon Glass9d085c12023-11-18 14:05:00 -0700155 return -EINVAL;
Simon Glass7715b312023-11-18 14:04:56 -0700156 bootstage_mark(BOOTSTAGE_ID_CHECK_IMAGETYPE);
157
158 /* get os_data and os_len */
159 switch (image_get_type(hdr)) {
160 case IH_TYPE_KERNEL:
161 case IH_TYPE_KERNEL_NOLOAD:
162 *os_data = image_get_data(hdr);
163 *os_len = image_get_data_size(hdr);
164 break;
165 case IH_TYPE_MULTI:
166 image_multi_getimg(hdr, 0, os_data, os_len);
167 break;
168 case IH_TYPE_STANDALONE:
169 *os_data = image_get_data(hdr);
170 *os_len = image_get_data_size(hdr);
171 break;
172 default:
Simon Glass7715b312023-11-18 14:04:56 -0700173 bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE);
Simon Glass9d085c12023-11-18 14:05:00 -0700174 return -EPROTOTYPE;
Simon Glass7715b312023-11-18 14:04:56 -0700175 }
176
177 /*
178 * copy image header to allow for image overwrites during
179 * kernel decompression.
180 */
181 memmove(&images->legacy_hdr_os_copy, hdr,
182 sizeof(struct legacy_img_hdr));
183
184 /* save pointer to image header */
185 images->legacy_hdr_os = hdr;
186
187 images->legacy_hdr_valid = 1;
188 bootstage_mark(BOOTSTAGE_ID_DECOMP_IMAGE);
189 break;
190#endif
191#if CONFIG_IS_ENABLED(FIT)
192 case IMAGE_FORMAT_FIT:
193 os_noffset = fit_image_load(images, img_addr,
194 &fit_uname_kernel, &fit_uname_config,
195 IH_ARCH_DEFAULT, IH_TYPE_KERNEL,
196 BOOTSTAGE_ID_FIT_KERNEL_START,
197 FIT_LOAD_IGNORED, os_data, os_len);
198 if (os_noffset < 0)
Simon Glass9d085c12023-11-18 14:05:00 -0700199 return -ENOENT;
Simon Glass7715b312023-11-18 14:04:56 -0700200
201 images->fit_hdr_os = map_sysmem(img_addr, 0);
202 images->fit_uname_os = fit_uname_kernel;
203 images->fit_uname_cfg = fit_uname_config;
204 images->fit_noffset_os = os_noffset;
205 break;
206#endif
207#ifdef CONFIG_ANDROID_BOOT_IMAGE
Simon Glass9d085c12023-11-18 14:05:00 -0700208 case IMAGE_FORMAT_ANDROID: {
209 int ret;
210
Simon Glass7715b312023-11-18 14:04:56 -0700211 boot_img = buf;
212 vendor_boot_img = NULL;
213 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
214 boot_img = map_sysmem(get_abootimg_addr(), 0);
215 vendor_boot_img = map_sysmem(get_avendor_bootimg_addr(), 0);
216 }
217 printf("## Booting Android Image at 0x%08lx ...\n", img_addr);
Simon Glass9d085c12023-11-18 14:05:00 -0700218 ret = android_image_get_kernel(boot_img, vendor_boot_img,
219 images->verify, os_data, os_len);
Simon Glass7715b312023-11-18 14:04:56 -0700220 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
221 unmap_sysmem(vendor_boot_img);
222 unmap_sysmem(boot_img);
223 }
Simon Glassae217b32023-11-18 14:05:01 -0700224 if (ret)
225 return ret;
Simon Glass7715b312023-11-18 14:04:56 -0700226 break;
Simon Glass9d085c12023-11-18 14:05:00 -0700227 }
Simon Glass7715b312023-11-18 14:04:56 -0700228#endif
229 default:
Simon Glassd6d6ed82023-11-18 14:05:03 -0700230 bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE);
Simon Glassc46784c2023-11-18 14:05:04 -0700231 return -EPROTOTYPE;
Simon Glass7715b312023-11-18 14:04:56 -0700232 }
233
234 debug(" kernel data at 0x%08lx, len = 0x%08lx (%ld)\n",
235 *os_data, *os_len, *os_len);
Simon Glass9d085c12023-11-18 14:05:00 -0700236 *kernp = buf;
Simon Glass7715b312023-11-18 14:04:56 -0700237
Simon Glass9d085c12023-11-18 14:05:00 -0700238 return 0;
Simon Glassf55305a2018-05-16 09:42:25 -0600239}
240
Simon Glass3b5866d2014-06-12 07:24:46 -0600241#ifdef CONFIG_LMB
Simon Glassdf00afa2022-09-06 20:26:50 -0600242static void boot_start_lmb(struct bootm_headers *images)
Simon Glass3b5866d2014-06-12 07:24:46 -0600243{
244 ulong mem_start;
245 phys_size_t mem_size;
246
Simon Glassda1a1342017-08-03 12:22:15 -0600247 mem_start = env_get_bootm_low();
248 mem_size = env_get_bootm_size();
Simon Glass3b5866d2014-06-12 07:24:46 -0600249
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +0100250 lmb_init_and_reserve_range(&images->lmb, (phys_addr_t)mem_start,
251 mem_size, NULL);
Simon Glass3b5866d2014-06-12 07:24:46 -0600252}
253#else
254#define lmb_reserve(lmb, base, size)
Simon Glassdf00afa2022-09-06 20:26:50 -0600255static inline void boot_start_lmb(struct bootm_headers *images) { }
Simon Glass3b5866d2014-06-12 07:24:46 -0600256#endif
257
Simon Glass5f1f4f22023-11-18 14:04:54 -0700258static int bootm_start(void)
Simon Glass3b5866d2014-06-12 07:24:46 -0600259{
260 memset((void *)&images, 0, sizeof(images));
Simon Glass22c34c22017-08-03 12:22:13 -0600261 images.verify = env_get_yesno("verify");
Simon Glass3b5866d2014-06-12 07:24:46 -0600262
263 boot_start_lmb(&images);
264
265 bootstage_mark_name(BOOTSTAGE_ID_BOOTM_START, "bootm_start");
266 images.state = BOOTM_STATE_START;
267
268 return 0;
269}
270
Simon Glass2e719d52023-11-18 14:04:55 -0700271static ulong bootm_data_addr(const char *addr_str)
Philippe Reynesae1f2ca2022-03-28 22:57:00 +0200272{
273 ulong addr;
274
Simon Glass2e719d52023-11-18 14:04:55 -0700275 if (addr_str)
276 addr = hextoul(addr_str, NULL);
Philippe Reynesae1f2ca2022-03-28 22:57:00 +0200277 else
278 addr = image_load_addr;
279
280 return addr;
281}
282
Simon Glass2e719d52023-11-18 14:04:55 -0700283/**
284 * bootm_pre_load() - Handle the pre-load processing
285 *
286 * This can be used to do a full signature check of the image, for example.
287 * It calls image_pre_load() with the data address of the image to check.
288 *
289 * @addr_str: String containing load address in hex, or NULL to use
290 * image_load_addr
291 * Return: 0 if OK, CMD_RET_FAILURE on failure
292 */
293static int bootm_pre_load(const char *addr_str)
Philippe Reynesae1f2ca2022-03-28 22:57:00 +0200294{
Simon Glass2e719d52023-11-18 14:04:55 -0700295 ulong data_addr = bootm_data_addr(addr_str);
Philippe Reynesae1f2ca2022-03-28 22:57:00 +0200296 int ret = 0;
297
Simon Glass88b6f922023-02-05 15:36:24 -0700298 if (IS_ENABLED(CONFIG_CMD_BOOTM_PRE_LOAD))
Philippe Reynesae1f2ca2022-03-28 22:57:00 +0200299 ret = image_pre_load(data_addr);
300
301 if (ret)
302 ret = CMD_RET_FAILURE;
303
304 return ret;
305}
306
Simon Glassf3c57b62023-11-18 14:05:05 -0700307/**
308 * bootm_find_os(): Find the OS to boot
309 *
310 * @cmd_name: Command name that started this boot, e.g. "bootm"
311 * @addr_fit: Address and/or FIT specifier (first arg of bootm command)
312 * Return: 0 on success, -ve on error
313 */
314static int bootm_find_os(const char *cmd_name, const char *addr_fit)
Simon Glass3b5866d2014-06-12 07:24:46 -0600315{
316 const void *os_hdr;
Safae Ouajih51c981b2023-02-06 00:50:17 +0100317#ifdef CONFIG_ANDROID_BOOT_IMAGE
318 const void *vendor_boot_img;
319 const void *boot_img;
320#endif
Simon Glass3b5866d2014-06-12 07:24:46 -0600321 bool ep_found = false;
Simon Glass0129b522014-10-19 21:11:24 -0600322 int ret;
Simon Glass3b5866d2014-06-12 07:24:46 -0600323
324 /* get kernel image header, start address and length */
Simon Glassf3c57b62023-11-18 14:05:05 -0700325 ret = boot_get_kernel(addr_fit, &images, &images.os.image_start,
Simon Glassc46784c2023-11-18 14:05:04 -0700326 &images.os.image_len, &os_hdr);
Simon Glassa5d35f92023-11-18 14:05:02 -0700327 if (ret) {
Simon Glassc46784c2023-11-18 14:05:04 -0700328 if (ret == -EPROTOTYPE)
Simon Glassf3c57b62023-11-18 14:05:05 -0700329 printf("Wrong Image Type for %s command\n", cmd_name);
Simon Glassc46784c2023-11-18 14:05:04 -0700330
Simon Glassa5d35f92023-11-18 14:05:02 -0700331 printf("ERROR %dE: can't get kernel image!\n", ret);
Simon Glass3b5866d2014-06-12 07:24:46 -0600332 return 1;
333 }
334
335 /* get image parameters */
336 switch (genimg_get_format(os_hdr)) {
Tom Rinic220bd92019-05-23 07:14:07 -0400337#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Simon Glass3b5866d2014-06-12 07:24:46 -0600338 case IMAGE_FORMAT_LEGACY:
339 images.os.type = image_get_type(os_hdr);
340 images.os.comp = image_get_comp(os_hdr);
341 images.os.os = image_get_os(os_hdr);
342
343 images.os.end = image_get_image_end(os_hdr);
344 images.os.load = image_get_load(os_hdr);
Simon Glass0129b522014-10-19 21:11:24 -0600345 images.os.arch = image_get_arch(os_hdr);
Simon Glass3b5866d2014-06-12 07:24:46 -0600346 break;
347#endif
Simon Glasse719d3b2021-09-25 19:43:20 -0600348#if CONFIG_IS_ENABLED(FIT)
Simon Glass3b5866d2014-06-12 07:24:46 -0600349 case IMAGE_FORMAT_FIT:
350 if (fit_image_get_type(images.fit_hdr_os,
351 images.fit_noffset_os,
352 &images.os.type)) {
353 puts("Can't get image type!\n");
354 bootstage_error(BOOTSTAGE_ID_FIT_TYPE);
355 return 1;
356 }
357
358 if (fit_image_get_comp(images.fit_hdr_os,
359 images.fit_noffset_os,
360 &images.os.comp)) {
361 puts("Can't get image compression!\n");
362 bootstage_error(BOOTSTAGE_ID_FIT_COMPRESSION);
363 return 1;
364 }
365
366 if (fit_image_get_os(images.fit_hdr_os, images.fit_noffset_os,
367 &images.os.os)) {
368 puts("Can't get image OS!\n");
369 bootstage_error(BOOTSTAGE_ID_FIT_OS);
370 return 1;
371 }
372
Simon Glass0129b522014-10-19 21:11:24 -0600373 if (fit_image_get_arch(images.fit_hdr_os,
374 images.fit_noffset_os,
375 &images.os.arch)) {
376 puts("Can't get image ARCH!\n");
377 return 1;
378 }
379
Simon Glass3b5866d2014-06-12 07:24:46 -0600380 images.os.end = fit_get_end(images.fit_hdr_os);
381
382 if (fit_image_get_load(images.fit_hdr_os, images.fit_noffset_os,
383 &images.os.load)) {
384 puts("Can't get image load address!\n");
385 bootstage_error(BOOTSTAGE_ID_FIT_LOADADDR);
386 return 1;
387 }
388 break;
389#endif
390#ifdef CONFIG_ANDROID_BOOT_IMAGE
391 case IMAGE_FORMAT_ANDROID:
Safae Ouajih51c981b2023-02-06 00:50:17 +0100392 boot_img = os_hdr;
393 vendor_boot_img = NULL;
394 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
395 boot_img = map_sysmem(get_abootimg_addr(), 0);
396 vendor_boot_img = map_sysmem(get_avendor_bootimg_addr(), 0);
397 }
Simon Glass3b5866d2014-06-12 07:24:46 -0600398 images.os.type = IH_TYPE_KERNEL;
Safae Ouajih51c981b2023-02-06 00:50:17 +0100399 images.os.comp = android_image_get_kcomp(boot_img, vendor_boot_img);
Simon Glass3b5866d2014-06-12 07:24:46 -0600400 images.os.os = IH_OS_LINUX;
Safae Ouajih51c981b2023-02-06 00:50:17 +0100401 images.os.end = android_image_get_end(boot_img, vendor_boot_img);
402 images.os.load = android_image_get_kload(boot_img, vendor_boot_img);
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300403 images.ep = images.os.load;
404 ep_found = true;
Safae Ouajih51c981b2023-02-06 00:50:17 +0100405 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
406 unmap_sysmem(vendor_boot_img);
407 unmap_sysmem(boot_img);
408 }
Simon Glass3b5866d2014-06-12 07:24:46 -0600409 break;
410#endif
411 default:
412 puts("ERROR: unknown image format type!\n");
413 return 1;
414 }
415
Simon Glass0129b522014-10-19 21:11:24 -0600416 /* If we have a valid setup.bin, we will use that for entry (x86) */
Simon Glass9d428302014-10-10 08:21:57 -0600417 if (images.os.arch == IH_ARCH_I386 ||
418 images.os.arch == IH_ARCH_X86_64) {
Simon Glass0129b522014-10-19 21:11:24 -0600419 ulong len;
420
421 ret = boot_get_setup(&images, IH_ARCH_I386, &images.ep, &len);
422 if (ret < 0 && ret != -ENOENT) {
423 puts("Could not find a valid setup.bin for x86\n");
424 return 1;
425 }
426 /* Kernel entry point is the setup.bin */
427 } else if (images.legacy_hdr_valid) {
Simon Glass3b5866d2014-06-12 07:24:46 -0600428 images.ep = image_get_ep(&images.legacy_hdr_os_copy);
Simon Glasse719d3b2021-09-25 19:43:20 -0600429#if CONFIG_IS_ENABLED(FIT)
Simon Glass3b5866d2014-06-12 07:24:46 -0600430 } else if (images.fit_uname_os) {
431 int ret;
432
433 ret = fit_image_get_entry(images.fit_hdr_os,
434 images.fit_noffset_os, &images.ep);
435 if (ret) {
436 puts("Can't get entry point property!\n");
437 return 1;
438 }
439#endif
440 } else if (!ep_found) {
441 puts("Could not find kernel entry point!\n");
442 return 1;
443 }
444
445 if (images.os.type == IH_TYPE_KERNEL_NOLOAD) {
Simon Glassdf650d22023-02-05 15:36:23 -0700446 if (IS_ENABLED(CONFIG_CMD_BOOTI) &&
Heinrich Schuchardtd254b3e2023-06-13 08:18:27 +0200447 images.os.arch == IH_ARCH_ARM64 &&
448 images.os.os == IH_OS_LINUX) {
Marek Vasut21425792018-06-13 06:13:33 +0200449 ulong image_addr;
450 ulong image_size;
451
452 ret = booti_setup(images.os.image_start, &image_addr,
453 &image_size, true);
454 if (ret != 0)
455 return 1;
456
457 images.os.type = IH_TYPE_KERNEL;
458 images.os.load = image_addr;
459 images.ep = image_addr;
460 } else {
461 images.os.load = images.os.image_start;
462 images.ep += images.os.image_start;
463 }
Simon Glass3b5866d2014-06-12 07:24:46 -0600464 }
465
Simon Glass5b539a02016-02-24 09:14:42 -0700466 images.os.start = map_to_sysmem(os_hdr);
Simon Glass3b5866d2014-06-12 07:24:46 -0600467
468 return 0;
469}
470
Karl Apsited686ce92015-05-21 09:52:49 -0400471/**
472 * bootm_find_images - wrapper to find and locate various images
473 * @flag: Ignored Argument
474 * @argc: command argument count
475 * @argv: command argument list
Tero Kristo06086942020-06-12 15:41:20 +0300476 * @start: OS image start address
477 * @size: OS image size
Karl Apsited686ce92015-05-21 09:52:49 -0400478 *
479 * boot_find_images() will attempt to load an available ramdisk,
480 * flattened device tree, as well as specifically marked
481 * "loadable" images (loadables are FIT only)
482 *
483 * Note: bootm_find_images will skip an image if it is not found
484 *
485 * @return:
486 * 0, if all existing images were loaded correctly
487 * 1, if an image is found but corrupted, or invalid
488 */
Tero Kristo06086942020-06-12 15:41:20 +0300489int bootm_find_images(int flag, int argc, char *const argv[], ulong start,
490 ulong size)
Simon Glass3b5866d2014-06-12 07:24:46 -0600491{
Simon Glass8eef77e2023-11-18 14:05:06 -0700492 const char *select = NULL;
Simon Glass5fb729b2023-11-18 14:05:13 -0700493 char addr_str[17];
Simon Glassaf2c72c2023-11-18 14:05:09 -0700494 ulong img_addr;
495 void *buf;
Simon Glass3b5866d2014-06-12 07:24:46 -0600496 int ret;
497
Simon Glass5fb729b2023-11-18 14:05:13 -0700498 img_addr = argc ? hextoul(argv[0], NULL) : image_load_addr;
499
Simon Glass8eef77e2023-11-18 14:05:06 -0700500 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE)) {
Simon Glass8eef77e2023-11-18 14:05:06 -0700501 /* Look for an Android boot image */
502 buf = map_sysmem(images.os.start, 0);
Simon Glass5fb729b2023-11-18 14:05:13 -0700503 if (buf && genimg_get_format(buf) == IMAGE_FORMAT_ANDROID) {
504 strcpy(addr_str, simple_xtoa(img_addr));
505 select = addr_str;
506 }
Simon Glass8eef77e2023-11-18 14:05:06 -0700507 }
508
509 if (argc >= 2)
510 select = argv[1];
511
Simon Glass3b5866d2014-06-12 07:24:46 -0600512 /* find ramdisk */
Simon Glass8eef77e2023-11-18 14:05:06 -0700513 ret = boot_get_ramdisk(select, &images, IH_INITRD_ARCH,
Simon Glass3b5866d2014-06-12 07:24:46 -0600514 &images.rd_start, &images.rd_end);
515 if (ret) {
516 puts("Ramdisk image is corrupt or invalid\n");
517 return 1;
518 }
519
Tero Kristo06086942020-06-12 15:41:20 +0300520 /* check if ramdisk overlaps OS image */
521 if (images.rd_start && (((ulong)images.rd_start >= start &&
Jaehoon Chung80ffb812020-10-21 14:17:03 +0900522 (ulong)images.rd_start < start + size) ||
523 ((ulong)images.rd_end > start &&
524 (ulong)images.rd_end <= start + size) ||
525 ((ulong)images.rd_start < start &&
526 (ulong)images.rd_end >= start + size))) {
Tero Kristo06086942020-06-12 15:41:20 +0300527 printf("ERROR: RD image overlaps OS image (OS=0x%lx..0x%lx)\n",
528 start, start + size);
529 return 1;
530 }
531
Simon Glass7f0a5a62023-11-18 14:05:08 -0700532 if (CONFIG_IS_ENABLED(OF_LIBFDT)) {
Simon Glassaf2c72c2023-11-18 14:05:09 -0700533 buf = map_sysmem(img_addr, 0);
534
Simon Glass7f0a5a62023-11-18 14:05:08 -0700535 /* find flattened device tree */
Simon Glassdb125e02023-11-18 14:05:10 -0700536 ret = boot_get_fdt(buf, argc > 2 ? argv[2] : NULL,
537 IH_ARCH_DEFAULT, &images, &images.ft_addr,
538 &images.ft_len);
Simon Glass7f0a5a62023-11-18 14:05:08 -0700539 if (ret) {
540 puts("Could not find a valid device tree\n");
541 return 1;
542 }
Tero Kristo06086942020-06-12 15:41:20 +0300543
Simon Glass7f0a5a62023-11-18 14:05:08 -0700544 /* check if FDT overlaps OS image */
545 if (images.ft_addr &&
546 (((ulong)images.ft_addr >= start &&
547 (ulong)images.ft_addr < start + size) ||
548 ((ulong)images.ft_addr + images.ft_len >= start &&
549 (ulong)images.ft_addr + images.ft_len < start + size))) {
550 printf("ERROR: FDT image overlaps OS image (OS=0x%lx..0x%lx)\n",
551 start, start + size);
552 return 1;
553 }
Tero Kristo06086942020-06-12 15:41:20 +0300554
Simon Glass7f0a5a62023-11-18 14:05:08 -0700555 if (IS_ENABLED(CONFIG_CMD_FDT))
556 set_working_fdt_addr(map_to_sysmem(images.ft_addr));
557 }
Simon Glass3b5866d2014-06-12 07:24:46 -0600558
Simon Glasse719d3b2021-09-25 19:43:20 -0600559#if CONFIG_IS_ENABLED(FIT)
Simon Glassbc0c4db2021-09-25 07:03:20 -0600560 if (IS_ENABLED(CONFIG_FPGA)) {
561 /* find bitstreams */
Simon Glass01080ab2023-11-18 14:05:11 -0700562 ret = boot_get_fpga(&images);
Simon Glassbc0c4db2021-09-25 07:03:20 -0600563 if (ret) {
564 printf("FPGA image is corrupted or invalid\n");
565 return 1;
566 }
Michal Simekebae78b2016-05-17 14:03:50 +0200567 }
Michal Simekebae78b2016-05-17 14:03:50 +0200568
Karl Apsite1b21c282015-05-21 09:52:48 -0400569 /* find all of the loadables */
Simon Glass4fdc5592023-11-18 14:05:12 -0700570 ret = boot_get_loadable(&images);
Karl Apsite1b21c282015-05-21 09:52:48 -0400571 if (ret) {
572 printf("Loadable(s) is corrupt or invalid\n");
573 return 1;
574 }
Karl Apsite1b21c282015-05-21 09:52:48 -0400575#endif
576
Simon Glass3b5866d2014-06-12 07:24:46 -0600577 return 0;
578}
579
Simon Glassed38aef2020-05-10 11:40:03 -0600580static int bootm_find_other(struct cmd_tbl *cmdtp, int flag, int argc,
581 char *const argv[])
Simon Glass3b5866d2014-06-12 07:24:46 -0600582{
583 if (((images.os.type == IH_TYPE_KERNEL) ||
584 (images.os.type == IH_TYPE_KERNEL_NOLOAD) ||
585 (images.os.type == IH_TYPE_MULTI)) &&
586 (images.os.os == IH_OS_LINUX ||
587 images.os.os == IH_OS_VXWORKS))
Tero Kristo06086942020-06-12 15:41:20 +0300588 return bootm_find_images(flag, argc, argv, 0, 0);
Simon Glass3b5866d2014-06-12 07:24:46 -0600589
590 return 0;
591}
Simon Glass10ae4192014-12-02 13:17:30 -0700592#endif /* USE_HOSTC */
593
Julius Werner47ef9862019-07-24 19:37:54 -0700594#if !defined(USE_HOSTCC) || defined(CONFIG_FIT_SIGNATURE)
Simon Glass4f6e6d72014-12-02 13:17:37 -0700595/**
596 * handle_decomp_error() - display a decompression error
597 *
598 * This function tries to produce a useful message. In the case where the
599 * uncompressed size is the same as the available space, we can assume that
600 * the image is too large for the buffer.
601 *
602 * @comp_type: Compression type being used (IH_COMP_...)
603 * @uncomp_size: Number of bytes uncompressed
Tom Rinif3c2f992022-06-25 19:29:46 -0400604 * @buf_size: Number of bytes the decompresion buffer was
Julius Werner47ef9862019-07-24 19:37:54 -0700605 * @ret: errno error code received from compression library
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100606 * Return: Appropriate BOOTM_ERR_ error code
Simon Glass4f6e6d72014-12-02 13:17:37 -0700607 */
Tom Rinif3c2f992022-06-25 19:29:46 -0400608static int handle_decomp_error(int comp_type, size_t uncomp_size,
609 size_t buf_size, int ret)
Simon Glass10ae4192014-12-02 13:17:30 -0700610{
Simon Glass4f6e6d72014-12-02 13:17:37 -0700611 const char *name = genimg_get_comp_name(comp_type);
612
Julius Werner47ef9862019-07-24 19:37:54 -0700613 /* ENOSYS means unimplemented compression type, don't reset. */
614 if (ret == -ENOSYS)
615 return BOOTM_ERR_UNIMPLEMENTED;
616
Tom Rinif3c2f992022-06-25 19:29:46 -0400617 if (uncomp_size >= buf_size)
Simon Glass4f6e6d72014-12-02 13:17:37 -0700618 printf("Image too large: increase CONFIG_SYS_BOOTM_LEN\n");
Simon Glass10ae4192014-12-02 13:17:30 -0700619 else
Simon Glass4f6e6d72014-12-02 13:17:37 -0700620 printf("%s: uncompress error %d\n", name, ret);
621
622 /*
623 * The decompression routines are now safe, so will not write beyond
624 * their bounds. Probably it is not necessary to reset, but maintain
625 * the current behaviour for now.
626 */
627 printf("Must RESET board to recover\n");
Simon Glass10ae4192014-12-02 13:17:30 -0700628#ifndef USE_HOSTCC
629 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
630#endif
631
632 return BOOTM_ERR_RESET;
633}
Julius Werner47ef9862019-07-24 19:37:54 -0700634#endif
Simon Glass686f3bb2014-06-12 07:24:52 -0600635
Simon Glassa51991d2014-06-12 07:24:53 -0600636#ifndef USE_HOSTCC
Simon Glassdf00afa2022-09-06 20:26:50 -0600637static int bootm_load_os(struct bootm_headers *images, int boot_progress)
Simon Glass686f3bb2014-06-12 07:24:52 -0600638{
Simon Glass74578f52022-09-06 20:26:51 -0600639 struct image_info os = images->os;
Simon Glass686f3bb2014-06-12 07:24:52 -0600640 ulong load = os.load;
Tom Rini6c96f5d2018-05-01 12:32:37 -0400641 ulong load_end;
Simon Glass686f3bb2014-06-12 07:24:52 -0600642 ulong blob_start = os.start;
643 ulong blob_end = os.end;
644 ulong image_start = os.image_start;
645 ulong image_len = os.image_len;
Bryan O'Donoghue600388e2018-04-15 11:48:17 +0100646 ulong flush_start = ALIGN_DOWN(load, ARCH_DMA_MINALIGN);
Simon Glass686f3bb2014-06-12 07:24:52 -0600647 bool no_overlap;
648 void *load_buf, *image_buf;
649 int err;
650
651 load_buf = map_sysmem(load, 0);
652 image_buf = map_sysmem(os.image_start, image_len);
Julius Werner47ef9862019-07-24 19:37:54 -0700653 err = image_decomp(os.comp, load, os.image_start, os.type,
654 load_buf, image_buf, image_len,
655 CONFIG_SYS_BOOTM_LEN, &load_end);
Simon Glass686f3bb2014-06-12 07:24:52 -0600656 if (err) {
Tom Rinif3c2f992022-06-25 19:29:46 -0400657 err = handle_decomp_error(os.comp, load_end - load,
658 CONFIG_SYS_BOOTM_LEN, err);
Simon Glass686f3bb2014-06-12 07:24:52 -0600659 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
660 return err;
661 }
Heinrich Schuchardt6d29e0d2020-08-30 11:34:12 +0200662 /* We need the decompressed image size in the next steps */
663 images->os.image_len = load_end - load;
Bryan O'Donoghue600388e2018-04-15 11:48:17 +0100664
Trent Piepho92e51d02019-03-27 23:50:09 +0000665 flush_cache(flush_start, ALIGN(load_end, ARCH_DMA_MINALIGN) - flush_start);
Simon Glass3b5866d2014-06-12 07:24:46 -0600666
Tom Rini6c96f5d2018-05-01 12:32:37 -0400667 debug(" kernel loaded at 0x%08lx, end = 0x%08lx\n", load, load_end);
Simon Glass3b5866d2014-06-12 07:24:46 -0600668 bootstage_mark(BOOTSTAGE_ID_KERNEL_LOADED);
669
Simon Glass686f3bb2014-06-12 07:24:52 -0600670 no_overlap = (os.comp == IH_COMP_NONE && load == image_start);
671
Tom Rini6c96f5d2018-05-01 12:32:37 -0400672 if (!no_overlap && load < blob_end && load_end > blob_start) {
Simon Glass3b5866d2014-06-12 07:24:46 -0600673 debug("images.os.start = 0x%lX, images.os.end = 0x%lx\n",
674 blob_start, blob_end);
675 debug("images.os.load = 0x%lx, load_end = 0x%lx\n", load,
Tom Rini6c96f5d2018-05-01 12:32:37 -0400676 load_end);
Simon Glass3b5866d2014-06-12 07:24:46 -0600677
678 /* Check what type of image this is. */
679 if (images->legacy_hdr_valid) {
680 if (image_get_type(&images->legacy_hdr_os_copy)
681 == IH_TYPE_MULTI)
682 puts("WARNING: legacy format multi component image overwritten\n");
683 return BOOTM_ERR_OVERLAP;
684 } else {
685 puts("ERROR: new format image overwritten - must RESET the board to recover\n");
686 bootstage_error(BOOTSTAGE_ID_OVERWRITTEN);
687 return BOOTM_ERR_RESET;
688 }
689 }
690
Tom Rini6c96f5d2018-05-01 12:32:37 -0400691 lmb_reserve(&images->lmb, images->os.load, (load_end -
692 images->os.load));
Simon Glass3b5866d2014-06-12 07:24:46 -0600693 return 0;
694}
695
696/**
697 * bootm_disable_interrupts() - Disable interrupts in preparation for load/boot
698 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100699 * Return: interrupt flag (0 if interrupts were disabled, non-zero if they were
Simon Glass3b5866d2014-06-12 07:24:46 -0600700 * enabled)
701 */
702ulong bootm_disable_interrupts(void)
703{
704 ulong iflag;
705
706 /*
707 * We have reached the point of no return: we are going to
708 * overwrite all exception vector code, so we cannot easily
709 * recover from any failures any more...
710 */
711 iflag = disable_interrupts();
712#ifdef CONFIG_NETCONSOLE
713 /* Stop the ethernet stack if NetConsole could have left it up */
714 eth_halt();
Simon Glass3b5866d2014-06-12 07:24:46 -0600715#endif
716
717#if defined(CONFIG_CMD_USB)
718 /*
719 * turn off USB to prevent the host controller from writing to the
720 * SDRAM while Linux is booting. This could happen (at least for OHCI
721 * controller), because the HCCA (Host Controller Communication Area)
722 * lies within the SDRAM and the host controller writes continously to
723 * this area (as busmaster!). The HccaFrameNumber is for example
724 * updated every 1 ms within the HCCA structure in SDRAM! For more
725 * details see the OpenHCI specification.
726 */
727 usb_stop();
728#endif
729 return iflag;
730}
731
Simon Glass7a8a4292020-11-05 10:33:42 -0700732#define CONSOLE_ARG "console="
Sean Anderson15bdcd42022-05-19 18:26:05 -0400733#define NULL_CONSOLE (CONSOLE_ARG "ttynull")
734#define CONSOLE_ARG_SIZE sizeof(NULL_CONSOLE)
Simon Glass3b5866d2014-06-12 07:24:46 -0600735
Simon Glassc38406e2020-11-05 10:33:43 -0700736/**
737 * fixup_silent_linux() - Handle silencing the linux boot if required
738 *
739 * This uses the silent_linux envvar to control whether to add/set a "console="
740 * parameter to the command line
741 *
742 * @buf: Buffer containing the string to process
743 * @maxlen: Maximum length of buffer
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100744 * Return: 0 if OK, -ENOSPC if @maxlen is too small
Simon Glassc38406e2020-11-05 10:33:43 -0700745 */
746static int fixup_silent_linux(char *buf, int maxlen)
Simon Glass3b5866d2014-06-12 07:24:46 -0600747{
Simon Glass3b5866d2014-06-12 07:24:46 -0600748 int want_silent;
Simon Glassc38406e2020-11-05 10:33:43 -0700749 char *cmdline;
750 int size;
Simon Glass07a88862020-11-05 10:33:38 -0700751
Simon Glass3b5866d2014-06-12 07:24:46 -0600752 /*
Simon Glassc38406e2020-11-05 10:33:43 -0700753 * Move the input string to the end of buffer. The output string will be
754 * built up at the start.
755 */
756 size = strlen(buf) + 1;
757 if (size * 2 > maxlen)
758 return -ENOSPC;
759 cmdline = buf + maxlen - size;
760 memmove(cmdline, buf, size);
761 /*
Simon Glass3b5866d2014-06-12 07:24:46 -0600762 * Only fix cmdline when requested. The environment variable can be:
763 *
764 * no - we never fixup
765 * yes - we always fixup
766 * unset - we rely on the console silent flag
767 */
Simon Glass22c34c22017-08-03 12:22:13 -0600768 want_silent = env_get_yesno("silent_linux");
Simon Glass3b5866d2014-06-12 07:24:46 -0600769 if (want_silent == 0)
Simon Glass2eab0172020-11-05 10:33:39 -0700770 return 0;
Simon Glass3b5866d2014-06-12 07:24:46 -0600771 else if (want_silent == -1 && !(gd->flags & GD_FLG_SILENT))
Simon Glass2eab0172020-11-05 10:33:39 -0700772 return 0;
Simon Glass3b5866d2014-06-12 07:24:46 -0600773
774 debug("before silent fix-up: %s\n", cmdline);
Simon Glassc38406e2020-11-05 10:33:43 -0700775 if (*cmdline) {
Simon Glass3b5866d2014-06-12 07:24:46 -0600776 char *start = strstr(cmdline, CONSOLE_ARG);
777
Simon Glassc38406e2020-11-05 10:33:43 -0700778 /* Check space for maximum possible new command line */
779 if (size + CONSOLE_ARG_SIZE > maxlen)
Simon Glass2eab0172020-11-05 10:33:39 -0700780 return -ENOSPC;
Simon Glass3b5866d2014-06-12 07:24:46 -0600781
782 if (start) {
783 char *end = strchr(start, ' ');
Simon Glass7a8a4292020-11-05 10:33:42 -0700784 int start_bytes;
Simon Glass3b5866d2014-06-12 07:24:46 -0600785
Sean Anderson15bdcd42022-05-19 18:26:05 -0400786 start_bytes = start - cmdline;
Simon Glass7a8a4292020-11-05 10:33:42 -0700787 strncpy(buf, cmdline, start_bytes);
Sean Anderson15bdcd42022-05-19 18:26:05 -0400788 strncpy(buf + start_bytes, NULL_CONSOLE, CONSOLE_ARG_SIZE);
Simon Glass3b5866d2014-06-12 07:24:46 -0600789 if (end)
Sean Anderson15bdcd42022-05-19 18:26:05 -0400790 strcpy(buf + start_bytes + CONSOLE_ARG_SIZE - 1, end);
Simon Glass3b5866d2014-06-12 07:24:46 -0600791 else
Sean Anderson15bdcd42022-05-19 18:26:05 -0400792 buf[start_bytes + CONSOLE_ARG_SIZE] = '\0';
Simon Glass3b5866d2014-06-12 07:24:46 -0600793 } else {
Sean Anderson15bdcd42022-05-19 18:26:05 -0400794 sprintf(buf, "%s %s", cmdline, NULL_CONSOLE);
Simon Glass3b5866d2014-06-12 07:24:46 -0600795 }
Simon Glassc38406e2020-11-05 10:33:43 -0700796 if (buf + strlen(buf) >= cmdline)
797 return -ENOSPC;
Simon Glass3b5866d2014-06-12 07:24:46 -0600798 } else {
Sean Anderson15bdcd42022-05-19 18:26:05 -0400799 if (maxlen < CONSOLE_ARG_SIZE)
Simon Glassc38406e2020-11-05 10:33:43 -0700800 return -ENOSPC;
Sean Anderson15bdcd42022-05-19 18:26:05 -0400801 strcpy(buf, NULL_CONSOLE);
Simon Glass3b5866d2014-06-12 07:24:46 -0600802 }
Simon Glassc38406e2020-11-05 10:33:43 -0700803 debug("after silent fix-up: %s\n", buf);
804
805 return 0;
806}
807
Simon Glass529e2082020-11-05 10:33:48 -0700808/**
809 * process_subst() - Handle substitution of ${...} fields in the environment
810 *
811 * Handle variable substitution in the provided buffer
812 *
813 * @buf: Buffer containing the string to process
814 * @maxlen: Maximum length of buffer
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100815 * Return: 0 if OK, -ENOSPC if @maxlen is too small
Simon Glass529e2082020-11-05 10:33:48 -0700816 */
817static int process_subst(char *buf, int maxlen)
818{
819 char *cmdline;
820 int size;
821 int ret;
822
823 /* Move to end of buffer */
824 size = strlen(buf) + 1;
825 cmdline = buf + maxlen - size;
826 if (buf + size > cmdline)
827 return -ENOSPC;
828 memmove(cmdline, buf, size);
829
830 ret = cli_simple_process_macros(cmdline, buf, cmdline - buf);
831
832 return ret;
833}
834
Simon Glassb4e1b6d2020-11-05 10:33:45 -0700835int bootm_process_cmdline(char *buf, int maxlen, int flags)
836{
837 int ret;
838
839 /* Check config first to enable compiler to eliminate code */
840 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) &&
841 !IS_ENABLED(CONFIG_SILENT_U_BOOT_ONLY) &&
842 (flags & BOOTM_CL_SILENT)) {
843 ret = fixup_silent_linux(buf, maxlen);
844 if (ret)
845 return log_msg_ret("silent", ret);
846 }
Simon Glassed8603d2021-03-15 18:11:23 +1300847 if (IS_ENABLED(CONFIG_BOOTARGS_SUBST) && IS_ENABLED(CONFIG_CMDLINE) &&
848 (flags & BOOTM_CL_SUBST)) {
Simon Glass529e2082020-11-05 10:33:48 -0700849 ret = process_subst(buf, maxlen);
850 if (ret)
Simon Glasscac744e2021-02-06 09:57:35 -0700851 return log_msg_ret("subst", ret);
Simon Glass529e2082020-11-05 10:33:48 -0700852 }
Simon Glassb4e1b6d2020-11-05 10:33:45 -0700853
854 return 0;
855}
856
Simon Glass63660dc2020-11-05 10:33:44 -0700857int bootm_process_cmdline_env(int flags)
Simon Glassc38406e2020-11-05 10:33:43 -0700858{
859 const int maxlen = MAX_CMDLINE_SIZE;
Simon Glass63660dc2020-11-05 10:33:44 -0700860 bool do_silent;
Simon Glassc38406e2020-11-05 10:33:43 -0700861 const char *env;
862 char *buf;
863 int ret;
Simon Glass3b5866d2014-06-12 07:24:46 -0600864
Simon Glassc38406e2020-11-05 10:33:43 -0700865 /* First check if any action is needed */
866 do_silent = IS_ENABLED(CONFIG_SILENT_CONSOLE) &&
Simon Glass63660dc2020-11-05 10:33:44 -0700867 !IS_ENABLED(CONFIG_SILENT_U_BOOT_ONLY) && (flags & BOOTM_CL_SILENT);
Simon Glass529e2082020-11-05 10:33:48 -0700868 if (!do_silent && !IS_ENABLED(CONFIG_BOOTARGS_SUBST))
Simon Glassc38406e2020-11-05 10:33:43 -0700869 return 0;
870
871 env = env_get("bootargs");
872 if (env && strlen(env) >= maxlen)
873 return -E2BIG;
874 buf = malloc(maxlen);
875 if (!buf)
876 return -ENOMEM;
877 if (env)
878 strcpy(buf, env);
879 else
880 *buf = '\0';
Simon Glassb4e1b6d2020-11-05 10:33:45 -0700881 ret = bootm_process_cmdline(buf, maxlen, flags);
Simon Glassc38406e2020-11-05 10:33:43 -0700882 if (!ret) {
883 ret = env_set("bootargs", buf);
884
885 /*
886 * If buf is "" and bootargs does not exist, this will produce
887 * an error trying to delete bootargs. Ignore it
888 */
889 if (ret == -ENOENT)
890 ret = 0;
891 }
Simon Glass3b5866d2014-06-12 07:24:46 -0600892 free(buf);
Simon Glassc38406e2020-11-05 10:33:43 -0700893 if (ret)
894 return log_msg_ret("env", ret);
Simon Glass2eab0172020-11-05 10:33:39 -0700895
896 return 0;
Simon Glass3b5866d2014-06-12 07:24:46 -0600897}
Simon Glass3b5866d2014-06-12 07:24:46 -0600898
Eddie James32401ba2023-10-24 10:43:50 -0500899int bootm_measure(struct bootm_headers *images)
900{
901 int ret = 0;
902
903 /* Skip measurement if EFI is going to do it */
904 if (images->os.os == IH_OS_EFI &&
905 IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL) &&
906 IS_ENABLED(CONFIG_BOOTM_EFI))
907 return ret;
908
909 if (IS_ENABLED(CONFIG_MEASURED_BOOT)) {
910 struct tcg2_event_log elog;
911 struct udevice *dev;
912 void *initrd_buf;
913 void *image_buf;
914 const char *s;
915 u32 rd_len;
916 bool ign;
917
918 elog.log_size = 0;
919 ign = IS_ENABLED(CONFIG_MEASURE_IGNORE_LOG);
920 ret = tcg2_measurement_init(&dev, &elog, ign);
921 if (ret)
922 return ret;
923
924 image_buf = map_sysmem(images->os.image_start,
925 images->os.image_len);
926 ret = tcg2_measure_data(dev, &elog, 8, images->os.image_len,
927 image_buf, EV_COMPACT_HASH,
928 strlen("linux") + 1, (u8 *)"linux");
929 if (ret)
930 goto unmap_image;
931
932 rd_len = images->rd_end - images->rd_start;
933 initrd_buf = map_sysmem(images->rd_start, rd_len);
934 ret = tcg2_measure_data(dev, &elog, 9, rd_len, initrd_buf,
935 EV_COMPACT_HASH, strlen("initrd") + 1,
936 (u8 *)"initrd");
937 if (ret)
938 goto unmap_initrd;
939
940 if (IS_ENABLED(CONFIG_MEASURE_DEVICETREE)) {
941 ret = tcg2_measure_data(dev, &elog, 0, images->ft_len,
942 (u8 *)images->ft_addr,
943 EV_TABLE_OF_DEVICES,
944 strlen("dts") + 1,
945 (u8 *)"dts");
946 if (ret)
947 goto unmap_initrd;
948 }
949
950 s = env_get("bootargs");
951 if (!s)
952 s = "";
953 ret = tcg2_measure_data(dev, &elog, 1, strlen(s) + 1, (u8 *)s,
954 EV_PLATFORM_CONFIG_FLAGS,
955 strlen(s) + 1, (u8 *)s);
956
957unmap_initrd:
958 unmap_sysmem(initrd_buf);
959
960unmap_image:
961 unmap_sysmem(image_buf);
962 tcg2_measurement_term(dev, &elog, ret != 0);
963 }
964
965 return ret;
966}
967
Simon Glass3b5866d2014-06-12 07:24:46 -0600968/**
969 * Execute selected states of the bootm command.
970 *
971 * Note the arguments to this state must be the first argument, Any 'bootm'
972 * or sub-command arguments must have already been taken.
973 *
974 * Note that if states contains more than one flag it MUST contain
975 * BOOTM_STATE_START, since this handles and consumes the command line args.
976 *
977 * Also note that aside from boot_os_fn functions and bootm_load_os no other
978 * functions we store the return value of in 'ret' may use a negative return
979 * value, without special handling.
980 *
981 * @param cmdtp Pointer to bootm command table entry
982 * @param flag Command flags (CMD_FLAG_...)
983 * @param argc Number of subcommand arguments (0 = no arguments)
984 * @param argv Arguments
985 * @param states Mask containing states to run (BOOTM_STATE_...)
986 * @param images Image header information
987 * @param boot_progress 1 to show boot progress, 0 to not do this
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100988 * Return: 0 if ok, something else on error. Some errors will cause this
Simon Glass3b5866d2014-06-12 07:24:46 -0600989 * function to perform a reboot! If states contains BOOTM_STATE_OS_GO
990 * then the intent is to boot an OS, so this function will not return
991 * unless the image type is standalone.
992 */
Simon Glassed38aef2020-05-10 11:40:03 -0600993int do_bootm_states(struct cmd_tbl *cmdtp, int flag, int argc,
Simon Glassdf00afa2022-09-06 20:26:50 -0600994 char *const argv[], int states, struct bootm_headers *images,
Simon Glassed38aef2020-05-10 11:40:03 -0600995 int boot_progress)
Simon Glass3b5866d2014-06-12 07:24:46 -0600996{
997 boot_os_fn *boot_fn;
998 ulong iflag = 0;
999 int ret = 0, need_boot_fn;
1000
1001 images->state |= states;
1002
1003 /*
1004 * Work through the states and see how far we get. We stop on
1005 * any error.
1006 */
1007 if (states & BOOTM_STATE_START)
Simon Glass5f1f4f22023-11-18 14:04:54 -07001008 ret = bootm_start();
Simon Glass3b5866d2014-06-12 07:24:46 -06001009
Philippe Reynesae1f2ca2022-03-28 22:57:00 +02001010 if (!ret && (states & BOOTM_STATE_PRE_LOAD))
Simon Glass2e719d52023-11-18 14:04:55 -07001011 ret = bootm_pre_load(argv[0]);
Philippe Reynesae1f2ca2022-03-28 22:57:00 +02001012
Simon Glass3b5866d2014-06-12 07:24:46 -06001013 if (!ret && (states & BOOTM_STATE_FINDOS))
Simon Glassf3c57b62023-11-18 14:05:05 -07001014 ret = bootm_find_os(cmdtp->name, argv[0]);
Simon Glass3b5866d2014-06-12 07:24:46 -06001015
Zubair Lutfullah Kakakhel31eb47d2016-09-09 09:18:58 +01001016 if (!ret && (states & BOOTM_STATE_FINDOTHER))
Simon Glass3b5866d2014-06-12 07:24:46 -06001017 ret = bootm_find_other(cmdtp, flag, argc, argv);
Simon Glass3b5866d2014-06-12 07:24:46 -06001018
Eddie James32401ba2023-10-24 10:43:50 -05001019 if (IS_ENABLED(CONFIG_MEASURED_BOOT) && !ret &&
1020 (states & BOOTM_STATE_MEASURE))
1021 bootm_measure(images);
1022
Simon Glass3b5866d2014-06-12 07:24:46 -06001023 /* Load the OS */
1024 if (!ret && (states & BOOTM_STATE_LOADOS)) {
Simon Glass3b5866d2014-06-12 07:24:46 -06001025 iflag = bootm_disable_interrupts();
Tom Rini6c96f5d2018-05-01 12:32:37 -04001026 ret = bootm_load_os(images, 0);
1027 if (ret && ret != BOOTM_ERR_OVERLAP)
Simon Glass3b5866d2014-06-12 07:24:46 -06001028 goto err;
1029 else if (ret == BOOTM_ERR_OVERLAP)
1030 ret = 0;
Simon Glass3b5866d2014-06-12 07:24:46 -06001031 }
1032
1033 /* Relocate the ramdisk */
1034#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
1035 if (!ret && (states & BOOTM_STATE_RAMDISK)) {
1036 ulong rd_len = images->rd_end - images->rd_start;
1037
1038 ret = boot_ramdisk_high(&images->lmb, images->rd_start,
1039 rd_len, &images->initrd_start, &images->initrd_end);
1040 if (!ret) {
Simon Glass4d949a22017-08-03 12:22:10 -06001041 env_set_hex("initrd_start", images->initrd_start);
1042 env_set_hex("initrd_end", images->initrd_end);
Simon Glass3b5866d2014-06-12 07:24:46 -06001043 }
1044 }
1045#endif
Simon Glass85c057e2021-09-25 19:43:21 -06001046#if CONFIG_IS_ENABLED(OF_LIBFDT) && defined(CONFIG_LMB)
Simon Glass3b5866d2014-06-12 07:24:46 -06001047 if (!ret && (states & BOOTM_STATE_FDT)) {
1048 boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr);
1049 ret = boot_relocate_fdt(&images->lmb, &images->ft_addr,
1050 &images->ft_len);
1051 }
1052#endif
1053
1054 /* From now on, we need the OS boot function */
1055 if (ret)
1056 return ret;
1057 boot_fn = bootm_os_get_boot_func(images->os.os);
1058 need_boot_fn = states & (BOOTM_STATE_OS_CMDLINE |
1059 BOOTM_STATE_OS_BD_T | BOOTM_STATE_OS_PREP |
1060 BOOTM_STATE_OS_FAKE_GO | BOOTM_STATE_OS_GO);
1061 if (boot_fn == NULL && need_boot_fn) {
1062 if (iflag)
1063 enable_interrupts();
1064 printf("ERROR: booting os '%s' (%d) is not supported\n",
1065 genimg_get_os_name(images->os.os), images->os.os);
1066 bootstage_error(BOOTSTAGE_ID_CHECK_BOOT_OS);
1067 return 1;
1068 }
1069
Hector Palacios8f9cc4c2016-07-11 12:34:37 +02001070
Simon Glass3b5866d2014-06-12 07:24:46 -06001071 /* Call various other states that are not generally used */
1072 if (!ret && (states & BOOTM_STATE_OS_CMDLINE))
1073 ret = boot_fn(BOOTM_STATE_OS_CMDLINE, argc, argv, images);
1074 if (!ret && (states & BOOTM_STATE_OS_BD_T))
1075 ret = boot_fn(BOOTM_STATE_OS_BD_T, argc, argv, images);
Hector Palacios8f9cc4c2016-07-11 12:34:37 +02001076 if (!ret && (states & BOOTM_STATE_OS_PREP)) {
Simon Glass529e2082020-11-05 10:33:48 -07001077 ret = bootm_process_cmdline_env(images->os.os == IH_OS_LINUX);
Simon Glassd87002f2020-11-05 10:33:41 -07001078 if (ret) {
1079 printf("Cmdline setup failed (err=%d)\n", ret);
1080 ret = CMD_RET_FAILURE;
1081 goto err;
Simon Glass2eab0172020-11-05 10:33:39 -07001082 }
Simon Glass3b5866d2014-06-12 07:24:46 -06001083 ret = boot_fn(BOOTM_STATE_OS_PREP, argc, argv, images);
Hector Palacios8f9cc4c2016-07-11 12:34:37 +02001084 }
Simon Glass3b5866d2014-06-12 07:24:46 -06001085
1086#ifdef CONFIG_TRACE
1087 /* Pretend to run the OS, then run a user command */
1088 if (!ret && (states & BOOTM_STATE_OS_FAKE_GO)) {
Simon Glass64b723f2017-08-03 12:22:12 -06001089 char *cmd_list = env_get("fakegocmd");
Simon Glass3b5866d2014-06-12 07:24:46 -06001090
1091 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_FAKE_GO,
1092 images, boot_fn);
1093 if (!ret && cmd_list)
1094 ret = run_command_list(cmd_list, -1, flag);
1095 }
1096#endif
1097
1098 /* Check for unsupported subcommand. */
1099 if (ret) {
Simon Glassb90cf902022-10-11 09:47:07 -06001100 printf("subcommand failed (err=%d)\n", ret);
Simon Glass3b5866d2014-06-12 07:24:46 -06001101 return ret;
1102 }
1103
1104 /* Now run the OS! We hope this doesn't return */
1105 if (!ret && (states & BOOTM_STATE_OS_GO))
1106 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_GO,
1107 images, boot_fn);
1108
1109 /* Deal with any fallout */
1110err:
1111 if (iflag)
1112 enable_interrupts();
1113
1114 if (ret == BOOTM_ERR_UNIMPLEMENTED)
1115 bootstage_error(BOOTSTAGE_ID_DECOMP_UNIMPL);
1116 else if (ret == BOOTM_ERR_RESET)
1117 do_reset(cmdtp, flag, argc, argv);
1118
1119 return ret;
1120}
1121
Simon Glass18700262023-07-30 11:17:02 -06001122int bootm_boot_start(ulong addr, const char *cmdline)
1123{
1124 static struct cmd_tbl cmd = {"bootm"};
1125 char addr_str[30];
1126 char *argv[] = {addr_str, NULL};
1127 int states;
1128 int ret;
1129
1130 /*
1131 * TODO(sjg@chromium.org): This uses the command-line interface, but
1132 * should not. To clean this up, the various bootm states need to be
1133 * passed an info structure instead of cmdline flags. Then this can
1134 * set up the required info and move through the states without needing
1135 * the command line.
1136 */
1137 states = BOOTM_STATE_START | BOOTM_STATE_FINDOS | BOOTM_STATE_PRE_LOAD |
1138 BOOTM_STATE_FINDOTHER | BOOTM_STATE_LOADOS |
1139 BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
1140 BOOTM_STATE_OS_GO;
1141 if (IS_ENABLED(CONFIG_SYS_BOOT_RAMDISK_HIGH))
1142 states |= BOOTM_STATE_RAMDISK;
1143 if (IS_ENABLED(CONFIG_PPC) || IS_ENABLED(CONFIG_MIPS))
1144 states |= BOOTM_STATE_OS_CMDLINE;
1145 images.state |= states;
1146
1147 snprintf(addr_str, sizeof(addr_str), "%lx", addr);
1148
1149 ret = env_set("bootargs", cmdline);
1150 if (ret) {
1151 printf("Failed to set cmdline\n");
1152 return ret;
1153 }
1154 ret = do_bootm_states(&cmd, 0, 1, argv, states, &images, 1);
1155
1156 return ret;
1157}
1158
Heinrich Schuchardtaa0b11b2019-01-08 18:13:06 +01001159/**
1160 * switch_to_non_secure_mode() - switch to non-secure mode
1161 *
1162 * This routine is overridden by architectures requiring this feature.
1163 */
1164void __weak switch_to_non_secure_mode(void)
1165{
1166}
1167
Simon Glassa51991d2014-06-12 07:24:53 -06001168#else /* USE_HOSTCC */
1169
Fabrice Fontained00d6b52019-05-03 22:37:05 +02001170#if defined(CONFIG_FIT_SIGNATURE)
Simon Glass81d13872020-03-18 11:44:02 -06001171static int bootm_host_load_image(const void *fit, int req_image_type,
1172 int cfg_noffset)
Simon Glassa51991d2014-06-12 07:24:53 -06001173{
1174 const char *fit_uname_config = NULL;
1175 ulong data, len;
Simon Glassdf00afa2022-09-06 20:26:50 -06001176 struct bootm_headers images;
Simon Glassa51991d2014-06-12 07:24:53 -06001177 int noffset;
Tom Rinif3c2f992022-06-25 19:29:46 -04001178 ulong load_end, buf_size;
Simon Glassa51991d2014-06-12 07:24:53 -06001179 uint8_t image_type;
Daniel Golle905d4b72022-08-27 04:14:42 +01001180 uint8_t image_comp;
Simon Glassa51991d2014-06-12 07:24:53 -06001181 void *load_buf;
1182 int ret;
1183
Simon Glass81d13872020-03-18 11:44:02 -06001184 fit_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
Simon Glassa51991d2014-06-12 07:24:53 -06001185 memset(&images, '\0', sizeof(images));
1186 images.verify = 1;
1187 noffset = fit_image_load(&images, (ulong)fit,
1188 NULL, &fit_uname_config,
1189 IH_ARCH_DEFAULT, req_image_type, -1,
1190 FIT_LOAD_IGNORED, &data, &len);
1191 if (noffset < 0)
1192 return noffset;
1193 if (fit_image_get_type(fit, noffset, &image_type)) {
1194 puts("Can't get image type!\n");
1195 return -EINVAL;
1196 }
1197
Daniel Golle2ebfd222022-08-27 04:17:28 +01001198 if (fit_image_get_comp(fit, noffset, &image_comp))
1199 image_comp = IH_COMP_NONE;
Simon Glassa51991d2014-06-12 07:24:53 -06001200
1201 /* Allow the image to expand by a factor of 4, should be safe */
Tom Rinif3c2f992022-06-25 19:29:46 -04001202 buf_size = (1 << 20) + len * 4;
1203 load_buf = malloc(buf_size);
Daniel Golle905d4b72022-08-27 04:14:42 +01001204 ret = image_decomp(image_comp, 0, data, image_type, load_buf,
Tom Rinif3c2f992022-06-25 19:29:46 -04001205 (void *)data, len, buf_size, &load_end);
Simon Glassa51991d2014-06-12 07:24:53 -06001206 free(load_buf);
Simon Glass5aeccc22014-12-02 13:17:33 -07001207
Julius Werner47ef9862019-07-24 19:37:54 -07001208 if (ret) {
Daniel Golle905d4b72022-08-27 04:14:42 +01001209 ret = handle_decomp_error(image_comp, load_end - 0, buf_size, ret);
Julius Werner47ef9862019-07-24 19:37:54 -07001210 if (ret != BOOTM_ERR_UNIMPLEMENTED)
1211 return ret;
1212 }
Simon Glassa51991d2014-06-12 07:24:53 -06001213
1214 return 0;
1215}
1216
1217int bootm_host_load_images(const void *fit, int cfg_noffset)
1218{
1219 static uint8_t image_types[] = {
1220 IH_TYPE_KERNEL,
1221 IH_TYPE_FLATDT,
1222 IH_TYPE_RAMDISK,
1223 };
1224 int err = 0;
1225 int i;
1226
1227 for (i = 0; i < ARRAY_SIZE(image_types); i++) {
1228 int ret;
1229
Simon Glass81d13872020-03-18 11:44:02 -06001230 ret = bootm_host_load_image(fit, image_types[i], cfg_noffset);
Simon Glassa51991d2014-06-12 07:24:53 -06001231 if (!err && ret && ret != -ENOENT)
1232 err = ret;
1233 }
1234
1235 /* Return the first error we found */
1236 return err;
1237}
Fabrice Fontained00d6b52019-05-03 22:37:05 +02001238#endif
Simon Glass0232ba72014-06-12 07:24:51 -06001239
1240#endif /* ndef USE_HOSTCC */