blob: fe6e84601f58f00698493f27b9b5a10d73503981 [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 Glass329da482018-05-16 09:42:25 -060047__weak void board_quiesce_devices(void)
48{
49}
50
Simon Glass7f3b1ee2023-11-18 14:04:56 -070051#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 Glass820110c2023-11-18 14:04:58 -0700105 * boot_get_kernel() - find kernel image
106 *
Simon Glass820110c2023-11-18 14:04:58 -0700107 * @addr_fit: first argument to bootm: address, fit configuration, etc.
Simon Glass7f3b1ee2023-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 Glass820110c2023-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 Glass7721e712023-11-18 14:05:00 -0700112 * @kernp: image header if valid image was found, otherwise NULL
Simon Glass7f3b1ee2023-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 Glassb13e9482023-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 Glass7f3b1ee2023-11-18 14:04:56 -0700119 */
Simon Glassb13e9482023-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 Glass7f3b1ee2023-11-18 14:04:56 -0700122{
123#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
124 struct legacy_img_hdr *hdr;
125#endif
126 ulong img_addr;
127 const void *buf;
Simon Glass530cc472023-11-18 14:04:57 -0700128 const char *fit_uname_config = NULL, *fit_uname_kernel = NULL;
Simon Glass7f3b1ee2023-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 Glass820110c2023-11-18 14:04:58 -0700137 img_addr = genimg_get_kernel_addr_fit(addr_fit, &fit_uname_config,
Simon Glass7f3b1ee2023-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 Glass7721e712023-11-18 14:05:00 -0700155 return -EINVAL;
Simon Glass7f3b1ee2023-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 Glass7f3b1ee2023-11-18 14:04:56 -0700173 bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE);
Simon Glass7721e712023-11-18 14:05:00 -0700174 return -EPROTOTYPE;
Simon Glass7f3b1ee2023-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 Glass7721e712023-11-18 14:05:00 -0700199 return -ENOENT;
Simon Glass7f3b1ee2023-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 Glass7721e712023-11-18 14:05:00 -0700208 case IMAGE_FORMAT_ANDROID: {
209 int ret;
210
Simon Glass7f3b1ee2023-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 Glass7721e712023-11-18 14:05:00 -0700218 ret = android_image_get_kernel(boot_img, vendor_boot_img,
219 images->verify, os_data, os_len);
Simon Glass7f3b1ee2023-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 Glass4f771692023-11-18 14:05:01 -0700224 if (ret)
225 return ret;
Simon Glass7f3b1ee2023-11-18 14:04:56 -0700226 break;
Simon Glass7721e712023-11-18 14:05:00 -0700227 }
Simon Glass7f3b1ee2023-11-18 14:04:56 -0700228#endif
229 default:
Simon Glassbdfa1b62023-11-18 14:05:03 -0700230 bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE);
Simon Glassb13e9482023-11-18 14:05:04 -0700231 return -EPROTOTYPE;
Simon Glass7f3b1ee2023-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 Glass7721e712023-11-18 14:05:00 -0700236 *kernp = buf;
Simon Glass7f3b1ee2023-11-18 14:04:56 -0700237
Simon Glass7721e712023-11-18 14:05:00 -0700238 return 0;
Simon Glass7f3b1ee2023-11-18 14:04:56 -0700239}
240
Simon Glassb6396402014-06-12 07:24:46 -0600241#ifdef CONFIG_LMB
Simon Glassd9d7c202022-09-06 20:26:50 -0600242static void boot_start_lmb(struct bootm_headers *images)
Simon Glassb6396402014-06-12 07:24:46 -0600243{
244 ulong mem_start;
245 phys_size_t mem_size;
246
Simon Glass723806c2017-08-03 12:22:15 -0600247 mem_start = env_get_bootm_low();
248 mem_size = env_get_bootm_size();
Simon Glassb6396402014-06-12 07:24:46 -0600249
Simon Goldschmidt9cc23232019-01-26 22:13:04 +0100250 lmb_init_and_reserve_range(&images->lmb, (phys_addr_t)mem_start,
251 mem_size, NULL);
Simon Glassb6396402014-06-12 07:24:46 -0600252}
253#else
254#define lmb_reserve(lmb, base, size)
Simon Glassd9d7c202022-09-06 20:26:50 -0600255static inline void boot_start_lmb(struct bootm_headers *images) { }
Simon Glassb6396402014-06-12 07:24:46 -0600256#endif
257
Simon Glassa50e8862023-11-18 14:04:54 -0700258static int bootm_start(void)
Simon Glassb6396402014-06-12 07:24:46 -0600259{
260 memset((void *)&images, 0, sizeof(images));
Simon Glassbfebc8c2017-08-03 12:22:13 -0600261 images.verify = env_get_yesno("verify");
Simon Glassb6396402014-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 Glass921070b2023-11-18 14:04:55 -0700271static ulong bootm_data_addr(const char *addr_str)
Philippe Reynes9d46e632022-03-28 22:57:00 +0200272{
273 ulong addr;
274
Simon Glass921070b2023-11-18 14:04:55 -0700275 if (addr_str)
276 addr = hextoul(addr_str, NULL);
Philippe Reynes9d46e632022-03-28 22:57:00 +0200277 else
278 addr = image_load_addr;
279
280 return addr;
281}
282
Simon Glass921070b2023-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 Reynes9d46e632022-03-28 22:57:00 +0200294{
Simon Glass921070b2023-11-18 14:04:55 -0700295 ulong data_addr = bootm_data_addr(addr_str);
Philippe Reynes9d46e632022-03-28 22:57:00 +0200296 int ret = 0;
297
Simon Glass494bcf12023-02-05 15:36:24 -0700298 if (IS_ENABLED(CONFIG_CMD_BOOTM_PRE_LOAD))
Philippe Reynes9d46e632022-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 Glass3e3bd5b2023-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 Glassb6396402014-06-12 07:24:46 -0600315{
316 const void *os_hdr;
Safae Ouajih636da202023-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 Glassb6396402014-06-12 07:24:46 -0600321 bool ep_found = false;
Simon Glass90268b82014-10-19 21:11:24 -0600322 int ret;
Simon Glassb6396402014-06-12 07:24:46 -0600323
324 /* get kernel image header, start address and length */
Simon Glass3e3bd5b2023-11-18 14:05:05 -0700325 ret = boot_get_kernel(addr_fit, &images, &images.os.image_start,
Simon Glassb13e9482023-11-18 14:05:04 -0700326 &images.os.image_len, &os_hdr);
Simon Glass4c76f5e2023-11-18 14:05:02 -0700327 if (ret) {
Simon Glassb13e9482023-11-18 14:05:04 -0700328 if (ret == -EPROTOTYPE)
Simon Glass3e3bd5b2023-11-18 14:05:05 -0700329 printf("Wrong Image Type for %s command\n", cmd_name);
Simon Glassb13e9482023-11-18 14:05:04 -0700330
Simon Glass4c76f5e2023-11-18 14:05:02 -0700331 printf("ERROR %dE: can't get kernel image!\n", ret);
Simon Glassb6396402014-06-12 07:24:46 -0600332 return 1;
333 }
334
335 /* get image parameters */
336 switch (genimg_get_format(os_hdr)) {
Tom Rinic76c93a2019-05-23 07:14:07 -0400337#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Simon Glassb6396402014-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 Glass90268b82014-10-19 21:11:24 -0600345 images.os.arch = image_get_arch(os_hdr);
Simon Glassb6396402014-06-12 07:24:46 -0600346 break;
347#endif
Simon Glassbf371b42021-09-25 19:43:20 -0600348#if CONFIG_IS_ENABLED(FIT)
Simon Glassb6396402014-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 Glass90268b82014-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 Glassb6396402014-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 Ouajih636da202023-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 Glassb6396402014-06-12 07:24:46 -0600398 images.os.type = IH_TYPE_KERNEL;
Safae Ouajih636da202023-02-06 00:50:17 +0100399 images.os.comp = android_image_get_kcomp(boot_img, vendor_boot_img);
Simon Glassb6396402014-06-12 07:24:46 -0600400 images.os.os = IH_OS_LINUX;
Safae Ouajih636da202023-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 Draidi86f46952014-10-23 20:50:07 +0300403 images.ep = images.os.load;
404 ep_found = true;
Safae Ouajih636da202023-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 Glassb6396402014-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 Glass90268b82014-10-19 21:11:24 -0600416 /* If we have a valid setup.bin, we will use that for entry (x86) */
Simon Glass5bda35c2014-10-10 08:21:57 -0600417 if (images.os.arch == IH_ARCH_I386 ||
418 images.os.arch == IH_ARCH_X86_64) {
Simon Glass90268b82014-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 Glassb6396402014-06-12 07:24:46 -0600428 images.ep = image_get_ep(&images.legacy_hdr_os_copy);
Simon Glassbf371b42021-09-25 19:43:20 -0600429#if CONFIG_IS_ENABLED(FIT)
Simon Glassb6396402014-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 Glassef65aa32023-02-05 15:36:23 -0700446 if (IS_ENABLED(CONFIG_CMD_BOOTI) &&
Heinrich Schuchardt4533b3d2023-06-13 08:18:27 +0200447 images.os.arch == IH_ARCH_ARM64 &&
448 images.os.os == IH_OS_LINUX) {
Marek Vasut487b5fa2018-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 Glassb6396402014-06-12 07:24:46 -0600464 }
465
Simon Glass7a80de42016-02-24 09:14:42 -0700466 images.os.start = map_to_sysmem(os_hdr);
Simon Glassb6396402014-06-12 07:24:46 -0600467
468 return 0;
469}
470
Simon Glass3ac85bd2023-11-18 14:05:16 -0700471int bootm_find_images(ulong img_addr, const char *conf_ramdisk,
472 const char *conf_fdt, ulong start, ulong size)
Simon Glassb6396402014-06-12 07:24:46 -0600473{
Simon Glass3ac85bd2023-11-18 14:05:16 -0700474 const char *select = conf_ramdisk;
Simon Glass46aee252023-11-18 14:05:13 -0700475 char addr_str[17];
Simon Glassba5e3f72023-11-18 14:05:09 -0700476 void *buf;
Simon Glassb6396402014-06-12 07:24:46 -0600477 int ret;
478
Simon Glass8eda15b2023-11-18 14:05:06 -0700479 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE)) {
Simon Glass8eda15b2023-11-18 14:05:06 -0700480 /* Look for an Android boot image */
481 buf = map_sysmem(images.os.start, 0);
Simon Glass46aee252023-11-18 14:05:13 -0700482 if (buf && genimg_get_format(buf) == IMAGE_FORMAT_ANDROID) {
483 strcpy(addr_str, simple_xtoa(img_addr));
484 select = addr_str;
485 }
Simon Glass8eda15b2023-11-18 14:05:06 -0700486 }
487
Simon Glass3ac85bd2023-11-18 14:05:16 -0700488 if (conf_ramdisk)
489 select = conf_ramdisk;
Simon Glass8eda15b2023-11-18 14:05:06 -0700490
Simon Glassb6396402014-06-12 07:24:46 -0600491 /* find ramdisk */
Simon Glass8eda15b2023-11-18 14:05:06 -0700492 ret = boot_get_ramdisk(select, &images, IH_INITRD_ARCH,
Simon Glassb6396402014-06-12 07:24:46 -0600493 &images.rd_start, &images.rd_end);
494 if (ret) {
495 puts("Ramdisk image is corrupt or invalid\n");
496 return 1;
497 }
498
Tero Kristofbde7582020-06-12 15:41:20 +0300499 /* check if ramdisk overlaps OS image */
500 if (images.rd_start && (((ulong)images.rd_start >= start &&
Jaehoon Chungef4f4f12020-10-21 14:17:03 +0900501 (ulong)images.rd_start < start + size) ||
502 ((ulong)images.rd_end > start &&
503 (ulong)images.rd_end <= start + size) ||
504 ((ulong)images.rd_start < start &&
505 (ulong)images.rd_end >= start + size))) {
Tero Kristofbde7582020-06-12 15:41:20 +0300506 printf("ERROR: RD image overlaps OS image (OS=0x%lx..0x%lx)\n",
507 start, start + size);
508 return 1;
509 }
510
Simon Glass972d5242023-11-18 14:05:08 -0700511 if (CONFIG_IS_ENABLED(OF_LIBFDT)) {
Simon Glassba5e3f72023-11-18 14:05:09 -0700512 buf = map_sysmem(img_addr, 0);
513
Simon Glass972d5242023-11-18 14:05:08 -0700514 /* find flattened device tree */
Simon Glass3ac85bd2023-11-18 14:05:16 -0700515 ret = boot_get_fdt(buf, conf_fdt, IH_ARCH_DEFAULT, &images,
516 &images.ft_addr, &images.ft_len);
Simon Glass972d5242023-11-18 14:05:08 -0700517 if (ret) {
518 puts("Could not find a valid device tree\n");
519 return 1;
520 }
Tero Kristofbde7582020-06-12 15:41:20 +0300521
Simon Glass972d5242023-11-18 14:05:08 -0700522 /* check if FDT overlaps OS image */
523 if (images.ft_addr &&
524 (((ulong)images.ft_addr >= start &&
525 (ulong)images.ft_addr < start + size) ||
526 ((ulong)images.ft_addr + images.ft_len >= start &&
527 (ulong)images.ft_addr + images.ft_len < start + size))) {
528 printf("ERROR: FDT image overlaps OS image (OS=0x%lx..0x%lx)\n",
529 start, start + size);
530 return 1;
531 }
Tero Kristofbde7582020-06-12 15:41:20 +0300532
Simon Glass972d5242023-11-18 14:05:08 -0700533 if (IS_ENABLED(CONFIG_CMD_FDT))
534 set_working_fdt_addr(map_to_sysmem(images.ft_addr));
535 }
Simon Glassb6396402014-06-12 07:24:46 -0600536
Simon Glassbf371b42021-09-25 19:43:20 -0600537#if CONFIG_IS_ENABLED(FIT)
Simon Glass4ed37ab2021-09-25 07:03:20 -0600538 if (IS_ENABLED(CONFIG_FPGA)) {
539 /* find bitstreams */
Simon Glass745367b2023-11-18 14:05:11 -0700540 ret = boot_get_fpga(&images);
Simon Glass4ed37ab2021-09-25 07:03:20 -0600541 if (ret) {
542 printf("FPGA image is corrupted or invalid\n");
543 return 1;
544 }
Michal Simek62afc602016-05-17 14:03:50 +0200545 }
Michal Simek62afc602016-05-17 14:03:50 +0200546
Karl Apsite84a07db2015-05-21 09:52:48 -0400547 /* find all of the loadables */
Simon Glass96456282023-11-18 14:05:12 -0700548 ret = boot_get_loadable(&images);
Karl Apsite84a07db2015-05-21 09:52:48 -0400549 if (ret) {
550 printf("Loadable(s) is corrupt or invalid\n");
551 return 1;
552 }
Karl Apsite84a07db2015-05-21 09:52:48 -0400553#endif
554
Simon Glassb6396402014-06-12 07:24:46 -0600555 return 0;
556}
557
Simon Glass09140112020-05-10 11:40:03 -0600558static int bootm_find_other(struct cmd_tbl *cmdtp, int flag, int argc,
559 char *const argv[])
Simon Glassb6396402014-06-12 07:24:46 -0600560{
Simon Glasse2fc90e2023-11-18 14:05:14 -0700561 if ((images.os.type == IH_TYPE_KERNEL ||
562 images.os.type == IH_TYPE_KERNEL_NOLOAD ||
563 images.os.type == IH_TYPE_MULTI) &&
564 (images.os.os == IH_OS_LINUX || images.os.os == IH_OS_VXWORKS ||
Simon Glass3ac85bd2023-11-18 14:05:16 -0700565 images.os.os == IH_OS_EFI || images.os.os == IH_OS_TEE)) {
566 ulong img_addr;
567
568 img_addr = argc ? hextoul(argv[0], NULL) : image_load_addr;
569 return bootm_find_images(img_addr, argc > 1 ? argv[1] : NULL,
570 argc > 2 ? argv[2] : NULL, 0, 0);
571 }
Simon Glassb6396402014-06-12 07:24:46 -0600572
573 return 0;
574}
Simon Glass40e59752014-12-02 13:17:30 -0700575#endif /* USE_HOSTC */
576
Julius Werner20908542019-07-24 19:37:54 -0700577#if !defined(USE_HOSTCC) || defined(CONFIG_FIT_SIGNATURE)
Simon Glass3086c052014-12-02 13:17:37 -0700578/**
579 * handle_decomp_error() - display a decompression error
580 *
581 * This function tries to produce a useful message. In the case where the
582 * uncompressed size is the same as the available space, we can assume that
583 * the image is too large for the buffer.
584 *
585 * @comp_type: Compression type being used (IH_COMP_...)
586 * @uncomp_size: Number of bytes uncompressed
Tom Rinic45568c2022-06-25 19:29:46 -0400587 * @buf_size: Number of bytes the decompresion buffer was
Julius Werner20908542019-07-24 19:37:54 -0700588 * @ret: errno error code received from compression library
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100589 * Return: Appropriate BOOTM_ERR_ error code
Simon Glass3086c052014-12-02 13:17:37 -0700590 */
Tom Rinic45568c2022-06-25 19:29:46 -0400591static int handle_decomp_error(int comp_type, size_t uncomp_size,
592 size_t buf_size, int ret)
Simon Glass40e59752014-12-02 13:17:30 -0700593{
Simon Glass3086c052014-12-02 13:17:37 -0700594 const char *name = genimg_get_comp_name(comp_type);
595
Julius Werner20908542019-07-24 19:37:54 -0700596 /* ENOSYS means unimplemented compression type, don't reset. */
597 if (ret == -ENOSYS)
598 return BOOTM_ERR_UNIMPLEMENTED;
599
Tom Rinic45568c2022-06-25 19:29:46 -0400600 if (uncomp_size >= buf_size)
Simon Glass3086c052014-12-02 13:17:37 -0700601 printf("Image too large: increase CONFIG_SYS_BOOTM_LEN\n");
Simon Glass40e59752014-12-02 13:17:30 -0700602 else
Simon Glass3086c052014-12-02 13:17:37 -0700603 printf("%s: uncompress error %d\n", name, ret);
604
605 /*
606 * The decompression routines are now safe, so will not write beyond
607 * their bounds. Probably it is not necessary to reset, but maintain
608 * the current behaviour for now.
609 */
610 printf("Must RESET board to recover\n");
Simon Glass40e59752014-12-02 13:17:30 -0700611#ifndef USE_HOSTCC
612 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
613#endif
614
615 return BOOTM_ERR_RESET;
616}
Julius Werner20908542019-07-24 19:37:54 -0700617#endif
Simon Glass2b164f12014-06-12 07:24:52 -0600618
Simon Glassce1400f2014-06-12 07:24:53 -0600619#ifndef USE_HOSTCC
Simon Glassd9d7c202022-09-06 20:26:50 -0600620static int bootm_load_os(struct bootm_headers *images, int boot_progress)
Simon Glass2b164f12014-06-12 07:24:52 -0600621{
Simon Glassda79b2f2022-09-06 20:26:51 -0600622 struct image_info os = images->os;
Simon Glass2b164f12014-06-12 07:24:52 -0600623 ulong load = os.load;
Tom Rinicc955352018-05-01 12:32:37 -0400624 ulong load_end;
Simon Glass2b164f12014-06-12 07:24:52 -0600625 ulong blob_start = os.start;
626 ulong blob_end = os.end;
627 ulong image_start = os.image_start;
628 ulong image_len = os.image_len;
Bryan O'Donoghuebbac9222018-04-15 11:48:17 +0100629 ulong flush_start = ALIGN_DOWN(load, ARCH_DMA_MINALIGN);
Simon Glass2b164f12014-06-12 07:24:52 -0600630 bool no_overlap;
631 void *load_buf, *image_buf;
632 int err;
633
634 load_buf = map_sysmem(load, 0);
635 image_buf = map_sysmem(os.image_start, image_len);
Julius Werner20908542019-07-24 19:37:54 -0700636 err = image_decomp(os.comp, load, os.image_start, os.type,
637 load_buf, image_buf, image_len,
638 CONFIG_SYS_BOOTM_LEN, &load_end);
Simon Glass2b164f12014-06-12 07:24:52 -0600639 if (err) {
Tom Rinic45568c2022-06-25 19:29:46 -0400640 err = handle_decomp_error(os.comp, load_end - load,
641 CONFIG_SYS_BOOTM_LEN, err);
Simon Glass2b164f12014-06-12 07:24:52 -0600642 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
643 return err;
644 }
Heinrich Schuchardt21d39462020-08-30 11:34:12 +0200645 /* We need the decompressed image size in the next steps */
646 images->os.image_len = load_end - load;
Bryan O'Donoghuebbac9222018-04-15 11:48:17 +0100647
Trent Piephob4353b32019-03-27 23:50:09 +0000648 flush_cache(flush_start, ALIGN(load_end, ARCH_DMA_MINALIGN) - flush_start);
Simon Glassb6396402014-06-12 07:24:46 -0600649
Tom Rinicc955352018-05-01 12:32:37 -0400650 debug(" kernel loaded at 0x%08lx, end = 0x%08lx\n", load, load_end);
Simon Glassb6396402014-06-12 07:24:46 -0600651 bootstage_mark(BOOTSTAGE_ID_KERNEL_LOADED);
652
Simon Glass2b164f12014-06-12 07:24:52 -0600653 no_overlap = (os.comp == IH_COMP_NONE && load == image_start);
654
Tom Rinicc955352018-05-01 12:32:37 -0400655 if (!no_overlap && load < blob_end && load_end > blob_start) {
Simon Glassb6396402014-06-12 07:24:46 -0600656 debug("images.os.start = 0x%lX, images.os.end = 0x%lx\n",
657 blob_start, blob_end);
658 debug("images.os.load = 0x%lx, load_end = 0x%lx\n", load,
Tom Rinicc955352018-05-01 12:32:37 -0400659 load_end);
Simon Glassb6396402014-06-12 07:24:46 -0600660
661 /* Check what type of image this is. */
662 if (images->legacy_hdr_valid) {
663 if (image_get_type(&images->legacy_hdr_os_copy)
664 == IH_TYPE_MULTI)
665 puts("WARNING: legacy format multi component image overwritten\n");
666 return BOOTM_ERR_OVERLAP;
667 } else {
668 puts("ERROR: new format image overwritten - must RESET the board to recover\n");
669 bootstage_error(BOOTSTAGE_ID_OVERWRITTEN);
670 return BOOTM_ERR_RESET;
671 }
672 }
673
Tom Rinicc955352018-05-01 12:32:37 -0400674 lmb_reserve(&images->lmb, images->os.load, (load_end -
675 images->os.load));
Simon Glassb6396402014-06-12 07:24:46 -0600676 return 0;
677}
678
679/**
680 * bootm_disable_interrupts() - Disable interrupts in preparation for load/boot
681 *
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100682 * Return: interrupt flag (0 if interrupts were disabled, non-zero if they were
Simon Glassb6396402014-06-12 07:24:46 -0600683 * enabled)
684 */
685ulong bootm_disable_interrupts(void)
686{
687 ulong iflag;
688
689 /*
690 * We have reached the point of no return: we are going to
691 * overwrite all exception vector code, so we cannot easily
692 * recover from any failures any more...
693 */
694 iflag = disable_interrupts();
695#ifdef CONFIG_NETCONSOLE
696 /* Stop the ethernet stack if NetConsole could have left it up */
697 eth_halt();
Simon Glassb6396402014-06-12 07:24:46 -0600698#endif
699
700#if defined(CONFIG_CMD_USB)
701 /*
702 * turn off USB to prevent the host controller from writing to the
703 * SDRAM while Linux is booting. This could happen (at least for OHCI
704 * controller), because the HCCA (Host Controller Communication Area)
705 * lies within the SDRAM and the host controller writes continously to
706 * this area (as busmaster!). The HccaFrameNumber is for example
707 * updated every 1 ms within the HCCA structure in SDRAM! For more
708 * details see the OpenHCI specification.
709 */
710 usb_stop();
711#endif
712 return iflag;
713}
714
Simon Glass6cd92b12020-11-05 10:33:42 -0700715#define CONSOLE_ARG "console="
Sean Andersonba9aa402022-05-19 18:26:05 -0400716#define NULL_CONSOLE (CONSOLE_ARG "ttynull")
717#define CONSOLE_ARG_SIZE sizeof(NULL_CONSOLE)
Simon Glassb6396402014-06-12 07:24:46 -0600718
Simon Glassb6386f32020-11-05 10:33:43 -0700719/**
720 * fixup_silent_linux() - Handle silencing the linux boot if required
721 *
722 * This uses the silent_linux envvar to control whether to add/set a "console="
723 * parameter to the command line
724 *
725 * @buf: Buffer containing the string to process
726 * @maxlen: Maximum length of buffer
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100727 * Return: 0 if OK, -ENOSPC if @maxlen is too small
Simon Glassb6386f32020-11-05 10:33:43 -0700728 */
729static int fixup_silent_linux(char *buf, int maxlen)
Simon Glassb6396402014-06-12 07:24:46 -0600730{
Simon Glassb6396402014-06-12 07:24:46 -0600731 int want_silent;
Simon Glassb6386f32020-11-05 10:33:43 -0700732 char *cmdline;
733 int size;
Simon Glassb6396402014-06-12 07:24:46 -0600734
Simon Glassb6386f32020-11-05 10:33:43 -0700735 /*
736 * Move the input string to the end of buffer. The output string will be
737 * built up at the start.
738 */
739 size = strlen(buf) + 1;
740 if (size * 2 > maxlen)
741 return -ENOSPC;
742 cmdline = buf + maxlen - size;
743 memmove(cmdline, buf, size);
Simon Glassb6396402014-06-12 07:24:46 -0600744 /*
745 * Only fix cmdline when requested. The environment variable can be:
746 *
747 * no - we never fixup
748 * yes - we always fixup
749 * unset - we rely on the console silent flag
750 */
Simon Glassbfebc8c2017-08-03 12:22:13 -0600751 want_silent = env_get_yesno("silent_linux");
Simon Glassb6396402014-06-12 07:24:46 -0600752 if (want_silent == 0)
Simon Glass4ae42642020-11-05 10:33:39 -0700753 return 0;
Simon Glassb6396402014-06-12 07:24:46 -0600754 else if (want_silent == -1 && !(gd->flags & GD_FLG_SILENT))
Simon Glass4ae42642020-11-05 10:33:39 -0700755 return 0;
Simon Glassb6396402014-06-12 07:24:46 -0600756
757 debug("before silent fix-up: %s\n", cmdline);
Simon Glassb6386f32020-11-05 10:33:43 -0700758 if (*cmdline) {
Simon Glassb6396402014-06-12 07:24:46 -0600759 char *start = strstr(cmdline, CONSOLE_ARG);
760
Simon Glassb6386f32020-11-05 10:33:43 -0700761 /* Check space for maximum possible new command line */
762 if (size + CONSOLE_ARG_SIZE > maxlen)
Simon Glass4ae42642020-11-05 10:33:39 -0700763 return -ENOSPC;
Simon Glassb6396402014-06-12 07:24:46 -0600764
765 if (start) {
766 char *end = strchr(start, ' ');
Simon Glass6cd92b12020-11-05 10:33:42 -0700767 int start_bytes;
Simon Glassb6396402014-06-12 07:24:46 -0600768
Sean Andersonba9aa402022-05-19 18:26:05 -0400769 start_bytes = start - cmdline;
Simon Glass6cd92b12020-11-05 10:33:42 -0700770 strncpy(buf, cmdline, start_bytes);
Sean Andersonba9aa402022-05-19 18:26:05 -0400771 strncpy(buf + start_bytes, NULL_CONSOLE, CONSOLE_ARG_SIZE);
Simon Glassb6396402014-06-12 07:24:46 -0600772 if (end)
Sean Andersonba9aa402022-05-19 18:26:05 -0400773 strcpy(buf + start_bytes + CONSOLE_ARG_SIZE - 1, end);
Simon Glassb6396402014-06-12 07:24:46 -0600774 else
Sean Andersonba9aa402022-05-19 18:26:05 -0400775 buf[start_bytes + CONSOLE_ARG_SIZE] = '\0';
Simon Glassb6396402014-06-12 07:24:46 -0600776 } else {
Sean Andersonba9aa402022-05-19 18:26:05 -0400777 sprintf(buf, "%s %s", cmdline, NULL_CONSOLE);
Simon Glassb6396402014-06-12 07:24:46 -0600778 }
Simon Glassb6386f32020-11-05 10:33:43 -0700779 if (buf + strlen(buf) >= cmdline)
780 return -ENOSPC;
Simon Glassb6396402014-06-12 07:24:46 -0600781 } else {
Sean Andersonba9aa402022-05-19 18:26:05 -0400782 if (maxlen < CONSOLE_ARG_SIZE)
Simon Glassb6386f32020-11-05 10:33:43 -0700783 return -ENOSPC;
Sean Andersonba9aa402022-05-19 18:26:05 -0400784 strcpy(buf, NULL_CONSOLE);
Simon Glassb6396402014-06-12 07:24:46 -0600785 }
Simon Glassb6386f32020-11-05 10:33:43 -0700786 debug("after silent fix-up: %s\n", buf);
Simon Glassb6396402014-06-12 07:24:46 -0600787
Simon Glassb6386f32020-11-05 10:33:43 -0700788 return 0;
789}
790
Simon Glass51bb3382020-11-05 10:33:48 -0700791/**
792 * process_subst() - Handle substitution of ${...} fields in the environment
793 *
794 * Handle variable substitution in the provided buffer
795 *
796 * @buf: Buffer containing the string to process
797 * @maxlen: Maximum length of buffer
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100798 * Return: 0 if OK, -ENOSPC if @maxlen is too small
Simon Glass51bb3382020-11-05 10:33:48 -0700799 */
800static int process_subst(char *buf, int maxlen)
801{
802 char *cmdline;
803 int size;
804 int ret;
805
806 /* Move to end of buffer */
807 size = strlen(buf) + 1;
808 cmdline = buf + maxlen - size;
809 if (buf + size > cmdline)
810 return -ENOSPC;
811 memmove(cmdline, buf, size);
812
813 ret = cli_simple_process_macros(cmdline, buf, cmdline - buf);
814
815 return ret;
816}
817
Simon Glass4448fe82020-11-05 10:33:45 -0700818int bootm_process_cmdline(char *buf, int maxlen, int flags)
819{
820 int ret;
821
822 /* Check config first to enable compiler to eliminate code */
823 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) &&
824 !IS_ENABLED(CONFIG_SILENT_U_BOOT_ONLY) &&
825 (flags & BOOTM_CL_SILENT)) {
826 ret = fixup_silent_linux(buf, maxlen);
827 if (ret)
828 return log_msg_ret("silent", ret);
829 }
Simon Glassa8d69622021-03-15 18:11:23 +1300830 if (IS_ENABLED(CONFIG_BOOTARGS_SUBST) && IS_ENABLED(CONFIG_CMDLINE) &&
831 (flags & BOOTM_CL_SUBST)) {
Simon Glass51bb3382020-11-05 10:33:48 -0700832 ret = process_subst(buf, maxlen);
833 if (ret)
Simon Glass185756e2021-02-06 09:57:35 -0700834 return log_msg_ret("subst", ret);
Simon Glass51bb3382020-11-05 10:33:48 -0700835 }
Simon Glass4448fe82020-11-05 10:33:45 -0700836
837 return 0;
838}
839
Simon Glassb3c01672020-11-05 10:33:44 -0700840int bootm_process_cmdline_env(int flags)
Simon Glassb6386f32020-11-05 10:33:43 -0700841{
842 const int maxlen = MAX_CMDLINE_SIZE;
Simon Glassb3c01672020-11-05 10:33:44 -0700843 bool do_silent;
Simon Glassb6386f32020-11-05 10:33:43 -0700844 const char *env;
845 char *buf;
846 int ret;
847
848 /* First check if any action is needed */
849 do_silent = IS_ENABLED(CONFIG_SILENT_CONSOLE) &&
Simon Glassb3c01672020-11-05 10:33:44 -0700850 !IS_ENABLED(CONFIG_SILENT_U_BOOT_ONLY) && (flags & BOOTM_CL_SILENT);
Simon Glass51bb3382020-11-05 10:33:48 -0700851 if (!do_silent && !IS_ENABLED(CONFIG_BOOTARGS_SUBST))
Simon Glassb6386f32020-11-05 10:33:43 -0700852 return 0;
853
854 env = env_get("bootargs");
855 if (env && strlen(env) >= maxlen)
856 return -E2BIG;
857 buf = malloc(maxlen);
858 if (!buf)
859 return -ENOMEM;
860 if (env)
861 strcpy(buf, env);
862 else
863 *buf = '\0';
Simon Glass4448fe82020-11-05 10:33:45 -0700864 ret = bootm_process_cmdline(buf, maxlen, flags);
Simon Glassb6386f32020-11-05 10:33:43 -0700865 if (!ret) {
866 ret = env_set("bootargs", buf);
867
868 /*
869 * If buf is "" and bootargs does not exist, this will produce
870 * an error trying to delete bootargs. Ignore it
871 */
872 if (ret == -ENOENT)
873 ret = 0;
874 }
Simon Glassb6396402014-06-12 07:24:46 -0600875 free(buf);
Simon Glassb6386f32020-11-05 10:33:43 -0700876 if (ret)
877 return log_msg_ret("env", ret);
Simon Glass4ae42642020-11-05 10:33:39 -0700878
879 return 0;
Simon Glassb6396402014-06-12 07:24:46 -0600880}
Simon Glassb6396402014-06-12 07:24:46 -0600881
Eddie Jamesdec166d2023-10-24 10:43:50 -0500882int bootm_measure(struct bootm_headers *images)
883{
884 int ret = 0;
885
886 /* Skip measurement if EFI is going to do it */
887 if (images->os.os == IH_OS_EFI &&
888 IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL) &&
889 IS_ENABLED(CONFIG_BOOTM_EFI))
890 return ret;
891
892 if (IS_ENABLED(CONFIG_MEASURED_BOOT)) {
893 struct tcg2_event_log elog;
894 struct udevice *dev;
895 void *initrd_buf;
896 void *image_buf;
897 const char *s;
898 u32 rd_len;
899 bool ign;
900
901 elog.log_size = 0;
902 ign = IS_ENABLED(CONFIG_MEASURE_IGNORE_LOG);
903 ret = tcg2_measurement_init(&dev, &elog, ign);
904 if (ret)
905 return ret;
906
907 image_buf = map_sysmem(images->os.image_start,
908 images->os.image_len);
909 ret = tcg2_measure_data(dev, &elog, 8, images->os.image_len,
910 image_buf, EV_COMPACT_HASH,
911 strlen("linux") + 1, (u8 *)"linux");
912 if (ret)
913 goto unmap_image;
914
915 rd_len = images->rd_end - images->rd_start;
916 initrd_buf = map_sysmem(images->rd_start, rd_len);
917 ret = tcg2_measure_data(dev, &elog, 9, rd_len, initrd_buf,
918 EV_COMPACT_HASH, strlen("initrd") + 1,
919 (u8 *)"initrd");
920 if (ret)
921 goto unmap_initrd;
922
923 if (IS_ENABLED(CONFIG_MEASURE_DEVICETREE)) {
924 ret = tcg2_measure_data(dev, &elog, 0, images->ft_len,
925 (u8 *)images->ft_addr,
926 EV_TABLE_OF_DEVICES,
927 strlen("dts") + 1,
928 (u8 *)"dts");
929 if (ret)
930 goto unmap_initrd;
931 }
932
933 s = env_get("bootargs");
934 if (!s)
935 s = "";
936 ret = tcg2_measure_data(dev, &elog, 1, strlen(s) + 1, (u8 *)s,
937 EV_PLATFORM_CONFIG_FLAGS,
938 strlen(s) + 1, (u8 *)s);
939
940unmap_initrd:
941 unmap_sysmem(initrd_buf);
942
943unmap_image:
944 unmap_sysmem(image_buf);
945 tcg2_measurement_term(dev, &elog, ret != 0);
946 }
947
948 return ret;
949}
950
Simon Glassb6396402014-06-12 07:24:46 -0600951/**
952 * Execute selected states of the bootm command.
953 *
954 * Note the arguments to this state must be the first argument, Any 'bootm'
955 * or sub-command arguments must have already been taken.
956 *
957 * Note that if states contains more than one flag it MUST contain
958 * BOOTM_STATE_START, since this handles and consumes the command line args.
959 *
960 * Also note that aside from boot_os_fn functions and bootm_load_os no other
961 * functions we store the return value of in 'ret' may use a negative return
962 * value, without special handling.
963 *
964 * @param cmdtp Pointer to bootm command table entry
965 * @param flag Command flags (CMD_FLAG_...)
966 * @param argc Number of subcommand arguments (0 = no arguments)
967 * @param argv Arguments
968 * @param states Mask containing states to run (BOOTM_STATE_...)
969 * @param images Image header information
970 * @param boot_progress 1 to show boot progress, 0 to not do this
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100971 * Return: 0 if ok, something else on error. Some errors will cause this
Simon Glassb6396402014-06-12 07:24:46 -0600972 * function to perform a reboot! If states contains BOOTM_STATE_OS_GO
973 * then the intent is to boot an OS, so this function will not return
974 * unless the image type is standalone.
975 */
Simon Glass09140112020-05-10 11:40:03 -0600976int do_bootm_states(struct cmd_tbl *cmdtp, int flag, int argc,
Simon Glassd9d7c202022-09-06 20:26:50 -0600977 char *const argv[], int states, struct bootm_headers *images,
Simon Glass09140112020-05-10 11:40:03 -0600978 int boot_progress)
Simon Glassb6396402014-06-12 07:24:46 -0600979{
980 boot_os_fn *boot_fn;
981 ulong iflag = 0;
982 int ret = 0, need_boot_fn;
983
984 images->state |= states;
985
986 /*
987 * Work through the states and see how far we get. We stop on
988 * any error.
989 */
990 if (states & BOOTM_STATE_START)
Simon Glassa50e8862023-11-18 14:04:54 -0700991 ret = bootm_start();
Simon Glassb6396402014-06-12 07:24:46 -0600992
Philippe Reynes9d46e632022-03-28 22:57:00 +0200993 if (!ret && (states & BOOTM_STATE_PRE_LOAD))
Simon Glass921070b2023-11-18 14:04:55 -0700994 ret = bootm_pre_load(argv[0]);
Philippe Reynes9d46e632022-03-28 22:57:00 +0200995
Simon Glassb6396402014-06-12 07:24:46 -0600996 if (!ret && (states & BOOTM_STATE_FINDOS))
Simon Glass3e3bd5b2023-11-18 14:05:05 -0700997 ret = bootm_find_os(cmdtp->name, argv[0]);
Simon Glassb6396402014-06-12 07:24:46 -0600998
Zubair Lutfullah Kakakhelba079842016-09-09 09:18:58 +0100999 if (!ret && (states & BOOTM_STATE_FINDOTHER))
Simon Glassb6396402014-06-12 07:24:46 -06001000 ret = bootm_find_other(cmdtp, flag, argc, argv);
Simon Glassb6396402014-06-12 07:24:46 -06001001
Eddie Jamesdec166d2023-10-24 10:43:50 -05001002 if (IS_ENABLED(CONFIG_MEASURED_BOOT) && !ret &&
1003 (states & BOOTM_STATE_MEASURE))
1004 bootm_measure(images);
1005
Simon Glassb6396402014-06-12 07:24:46 -06001006 /* Load the OS */
1007 if (!ret && (states & BOOTM_STATE_LOADOS)) {
Simon Glassb6396402014-06-12 07:24:46 -06001008 iflag = bootm_disable_interrupts();
Tom Rinicc955352018-05-01 12:32:37 -04001009 ret = bootm_load_os(images, 0);
1010 if (ret && ret != BOOTM_ERR_OVERLAP)
Simon Glassb6396402014-06-12 07:24:46 -06001011 goto err;
1012 else if (ret == BOOTM_ERR_OVERLAP)
1013 ret = 0;
Simon Glassb6396402014-06-12 07:24:46 -06001014 }
1015
1016 /* Relocate the ramdisk */
1017#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
1018 if (!ret && (states & BOOTM_STATE_RAMDISK)) {
1019 ulong rd_len = images->rd_end - images->rd_start;
1020
1021 ret = boot_ramdisk_high(&images->lmb, images->rd_start,
1022 rd_len, &images->initrd_start, &images->initrd_end);
1023 if (!ret) {
Simon Glass018f5302017-08-03 12:22:10 -06001024 env_set_hex("initrd_start", images->initrd_start);
1025 env_set_hex("initrd_end", images->initrd_end);
Simon Glassb6396402014-06-12 07:24:46 -06001026 }
1027 }
1028#endif
Simon Glass0c303f92021-09-25 19:43:21 -06001029#if CONFIG_IS_ENABLED(OF_LIBFDT) && defined(CONFIG_LMB)
Simon Glassb6396402014-06-12 07:24:46 -06001030 if (!ret && (states & BOOTM_STATE_FDT)) {
1031 boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr);
1032 ret = boot_relocate_fdt(&images->lmb, &images->ft_addr,
1033 &images->ft_len);
1034 }
1035#endif
1036
1037 /* From now on, we need the OS boot function */
1038 if (ret)
1039 return ret;
1040 boot_fn = bootm_os_get_boot_func(images->os.os);
1041 need_boot_fn = states & (BOOTM_STATE_OS_CMDLINE |
1042 BOOTM_STATE_OS_BD_T | BOOTM_STATE_OS_PREP |
1043 BOOTM_STATE_OS_FAKE_GO | BOOTM_STATE_OS_GO);
1044 if (boot_fn == NULL && need_boot_fn) {
1045 if (iflag)
1046 enable_interrupts();
1047 printf("ERROR: booting os '%s' (%d) is not supported\n",
1048 genimg_get_os_name(images->os.os), images->os.os);
1049 bootstage_error(BOOTSTAGE_ID_CHECK_BOOT_OS);
1050 return 1;
1051 }
1052
Hector Palacios19e86492016-07-11 12:34:37 +02001053
Simon Glassb6396402014-06-12 07:24:46 -06001054 /* Call various other states that are not generally used */
1055 if (!ret && (states & BOOTM_STATE_OS_CMDLINE))
1056 ret = boot_fn(BOOTM_STATE_OS_CMDLINE, argc, argv, images);
1057 if (!ret && (states & BOOTM_STATE_OS_BD_T))
1058 ret = boot_fn(BOOTM_STATE_OS_BD_T, argc, argv, images);
Hector Palacios19e86492016-07-11 12:34:37 +02001059 if (!ret && (states & BOOTM_STATE_OS_PREP)) {
Simon Glass51bb3382020-11-05 10:33:48 -07001060 ret = bootm_process_cmdline_env(images->os.os == IH_OS_LINUX);
Simon Glassd9477a02020-11-05 10:33:41 -07001061 if (ret) {
1062 printf("Cmdline setup failed (err=%d)\n", ret);
1063 ret = CMD_RET_FAILURE;
1064 goto err;
Simon Glass4ae42642020-11-05 10:33:39 -07001065 }
Simon Glassb6396402014-06-12 07:24:46 -06001066 ret = boot_fn(BOOTM_STATE_OS_PREP, argc, argv, images);
Hector Palacios19e86492016-07-11 12:34:37 +02001067 }
Simon Glassb6396402014-06-12 07:24:46 -06001068
1069#ifdef CONFIG_TRACE
1070 /* Pretend to run the OS, then run a user command */
1071 if (!ret && (states & BOOTM_STATE_OS_FAKE_GO)) {
Simon Glass00caae62017-08-03 12:22:12 -06001072 char *cmd_list = env_get("fakegocmd");
Simon Glassb6396402014-06-12 07:24:46 -06001073
1074 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_FAKE_GO,
1075 images, boot_fn);
1076 if (!ret && cmd_list)
1077 ret = run_command_list(cmd_list, -1, flag);
1078 }
1079#endif
1080
1081 /* Check for unsupported subcommand. */
1082 if (ret) {
Simon Glass13819f02022-10-11 09:47:07 -06001083 printf("subcommand failed (err=%d)\n", ret);
Simon Glassb6396402014-06-12 07:24:46 -06001084 return ret;
1085 }
1086
1087 /* Now run the OS! We hope this doesn't return */
1088 if (!ret && (states & BOOTM_STATE_OS_GO))
1089 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_GO,
1090 images, boot_fn);
1091
1092 /* Deal with any fallout */
1093err:
1094 if (iflag)
1095 enable_interrupts();
1096
1097 if (ret == BOOTM_ERR_UNIMPLEMENTED)
1098 bootstage_error(BOOTSTAGE_ID_DECOMP_UNIMPL);
1099 else if (ret == BOOTM_ERR_RESET)
1100 do_reset(cmdtp, flag, argc, argv);
1101
1102 return ret;
1103}
1104
Simon Glassdaffb0b2023-07-30 11:17:02 -06001105int bootm_boot_start(ulong addr, const char *cmdline)
1106{
1107 static struct cmd_tbl cmd = {"bootm"};
1108 char addr_str[30];
1109 char *argv[] = {addr_str, NULL};
1110 int states;
1111 int ret;
1112
1113 /*
1114 * TODO(sjg@chromium.org): This uses the command-line interface, but
1115 * should not. To clean this up, the various bootm states need to be
1116 * passed an info structure instead of cmdline flags. Then this can
1117 * set up the required info and move through the states without needing
1118 * the command line.
1119 */
1120 states = BOOTM_STATE_START | BOOTM_STATE_FINDOS | BOOTM_STATE_PRE_LOAD |
1121 BOOTM_STATE_FINDOTHER | BOOTM_STATE_LOADOS |
1122 BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
1123 BOOTM_STATE_OS_GO;
1124 if (IS_ENABLED(CONFIG_SYS_BOOT_RAMDISK_HIGH))
1125 states |= BOOTM_STATE_RAMDISK;
1126 if (IS_ENABLED(CONFIG_PPC) || IS_ENABLED(CONFIG_MIPS))
1127 states |= BOOTM_STATE_OS_CMDLINE;
1128 images.state |= states;
1129
1130 snprintf(addr_str, sizeof(addr_str), "%lx", addr);
1131
1132 ret = env_set("bootargs", cmdline);
1133 if (ret) {
1134 printf("Failed to set cmdline\n");
1135 return ret;
1136 }
1137 ret = do_bootm_states(&cmd, 0, 1, argv, states, &images, 1);
1138
1139 return ret;
1140}
1141
Heinrich Schuchardtf6c6df72019-01-08 18:13:06 +01001142/**
1143 * switch_to_non_secure_mode() - switch to non-secure mode
1144 *
1145 * This routine is overridden by architectures requiring this feature.
1146 */
1147void __weak switch_to_non_secure_mode(void)
1148{
1149}
1150
Simon Glassce1400f2014-06-12 07:24:53 -06001151#else /* USE_HOSTCC */
1152
Fabrice Fontaine93e07882019-05-03 22:37:05 +02001153#if defined(CONFIG_FIT_SIGNATURE)
Simon Glass8a9d0372020-03-18 11:44:02 -06001154static int bootm_host_load_image(const void *fit, int req_image_type,
1155 int cfg_noffset)
Simon Glassce1400f2014-06-12 07:24:53 -06001156{
1157 const char *fit_uname_config = NULL;
1158 ulong data, len;
Simon Glassd9d7c202022-09-06 20:26:50 -06001159 struct bootm_headers images;
Simon Glassce1400f2014-06-12 07:24:53 -06001160 int noffset;
Tom Rinic45568c2022-06-25 19:29:46 -04001161 ulong load_end, buf_size;
Simon Glassce1400f2014-06-12 07:24:53 -06001162 uint8_t image_type;
Daniel Golle0cd57f22022-08-27 04:14:42 +01001163 uint8_t image_comp;
Simon Glassce1400f2014-06-12 07:24:53 -06001164 void *load_buf;
1165 int ret;
1166
Simon Glass8a9d0372020-03-18 11:44:02 -06001167 fit_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
Simon Glassce1400f2014-06-12 07:24:53 -06001168 memset(&images, '\0', sizeof(images));
1169 images.verify = 1;
1170 noffset = fit_image_load(&images, (ulong)fit,
1171 NULL, &fit_uname_config,
1172 IH_ARCH_DEFAULT, req_image_type, -1,
1173 FIT_LOAD_IGNORED, &data, &len);
1174 if (noffset < 0)
1175 return noffset;
1176 if (fit_image_get_type(fit, noffset, &image_type)) {
1177 puts("Can't get image type!\n");
1178 return -EINVAL;
1179 }
1180
Daniel Golle88de6c52022-08-27 04:17:28 +01001181 if (fit_image_get_comp(fit, noffset, &image_comp))
1182 image_comp = IH_COMP_NONE;
Simon Glassce1400f2014-06-12 07:24:53 -06001183
1184 /* Allow the image to expand by a factor of 4, should be safe */
Tom Rinic45568c2022-06-25 19:29:46 -04001185 buf_size = (1 << 20) + len * 4;
1186 load_buf = malloc(buf_size);
Daniel Golle0cd57f22022-08-27 04:14:42 +01001187 ret = image_decomp(image_comp, 0, data, image_type, load_buf,
Tom Rinic45568c2022-06-25 19:29:46 -04001188 (void *)data, len, buf_size, &load_end);
Simon Glassce1400f2014-06-12 07:24:53 -06001189 free(load_buf);
Simon Glass081cc192014-12-02 13:17:33 -07001190
Julius Werner20908542019-07-24 19:37:54 -07001191 if (ret) {
Daniel Golle0cd57f22022-08-27 04:14:42 +01001192 ret = handle_decomp_error(image_comp, load_end - 0, buf_size, ret);
Julius Werner20908542019-07-24 19:37:54 -07001193 if (ret != BOOTM_ERR_UNIMPLEMENTED)
1194 return ret;
1195 }
Simon Glassce1400f2014-06-12 07:24:53 -06001196
1197 return 0;
1198}
1199
1200int bootm_host_load_images(const void *fit, int cfg_noffset)
1201{
1202 static uint8_t image_types[] = {
1203 IH_TYPE_KERNEL,
1204 IH_TYPE_FLATDT,
1205 IH_TYPE_RAMDISK,
1206 };
1207 int err = 0;
1208 int i;
1209
1210 for (i = 0; i < ARRAY_SIZE(image_types); i++) {
1211 int ret;
1212
Simon Glass8a9d0372020-03-18 11:44:02 -06001213 ret = bootm_host_load_image(fit, image_types[i], cfg_noffset);
Simon Glassce1400f2014-06-12 07:24:53 -06001214 if (!err && ret && ret != -ENOENT)
1215 err = ret;
1216 }
1217
1218 /* Return the first error we found */
1219 return err;
1220}
Fabrice Fontaine93e07882019-05-03 22:37:05 +02001221#endif
Simon Glassea51a622014-06-12 07:24:51 -06001222
1223#endif /* ndef USE_HOSTCC */