blob: 598f880d86ddf4124cbdd53a8668274a5572e4c9 [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/**
105 * boot_get_kernel - find kernel image
106 * @os_data: pointer to a ulong variable, will hold os data start address
107 * @os_len: pointer to a ulong variable, will hold os data length
108 *
109 * boot_get_kernel() tries to find a kernel image, verifies its integrity
110 * and locates kernel data.
111 *
112 * returns:
113 * pointer to image header if valid image was found, plus kernel start
114 * address and length, otherwise NULL
115 */
116static const void *boot_get_kernel(struct cmd_tbl *cmdtp, int flag, int argc,
117 char *const argv[], struct bootm_headers *images,
118 ulong *os_data, ulong *os_len)
119{
120#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
121 struct legacy_img_hdr *hdr;
122#endif
123 ulong img_addr;
124 const void *buf;
125 const char *fit_uname_config = NULL;
126 const char *fit_uname_kernel = NULL;
127#if CONFIG_IS_ENABLED(FIT)
128 int os_noffset;
129#endif
130
131#ifdef CONFIG_ANDROID_BOOT_IMAGE
132 const void *boot_img;
133 const void *vendor_boot_img;
134#endif
135 img_addr = genimg_get_kernel_addr_fit(argc < 1 ? NULL : argv[0],
136 &fit_uname_config,
137 &fit_uname_kernel);
138
139 if (IS_ENABLED(CONFIG_CMD_BOOTM_PRE_LOAD))
140 img_addr += image_load_offset;
141
142 bootstage_mark(BOOTSTAGE_ID_CHECK_MAGIC);
143
144 /* check image type, for FIT images get FIT kernel node */
145 *os_data = *os_len = 0;
146 buf = map_sysmem(img_addr, 0);
147 switch (genimg_get_format(buf)) {
148#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
149 case IMAGE_FORMAT_LEGACY:
150 printf("## Booting kernel from Legacy Image at %08lx ...\n",
151 img_addr);
152 hdr = image_get_kernel(img_addr, images->verify);
153 if (!hdr)
154 return NULL;
155 bootstage_mark(BOOTSTAGE_ID_CHECK_IMAGETYPE);
156
157 /* get os_data and os_len */
158 switch (image_get_type(hdr)) {
159 case IH_TYPE_KERNEL:
160 case IH_TYPE_KERNEL_NOLOAD:
161 *os_data = image_get_data(hdr);
162 *os_len = image_get_data_size(hdr);
163 break;
164 case IH_TYPE_MULTI:
165 image_multi_getimg(hdr, 0, os_data, os_len);
166 break;
167 case IH_TYPE_STANDALONE:
168 *os_data = image_get_data(hdr);
169 *os_len = image_get_data_size(hdr);
170 break;
171 default:
172 printf("Wrong Image Type for %s command\n",
173 cmdtp->name);
174 bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE);
175 return NULL;
176 }
177
178 /*
179 * copy image header to allow for image overwrites during
180 * kernel decompression.
181 */
182 memmove(&images->legacy_hdr_os_copy, hdr,
183 sizeof(struct legacy_img_hdr));
184
185 /* save pointer to image header */
186 images->legacy_hdr_os = hdr;
187
188 images->legacy_hdr_valid = 1;
189 bootstage_mark(BOOTSTAGE_ID_DECOMP_IMAGE);
190 break;
191#endif
192#if CONFIG_IS_ENABLED(FIT)
193 case IMAGE_FORMAT_FIT:
194 os_noffset = fit_image_load(images, img_addr,
195 &fit_uname_kernel, &fit_uname_config,
196 IH_ARCH_DEFAULT, IH_TYPE_KERNEL,
197 BOOTSTAGE_ID_FIT_KERNEL_START,
198 FIT_LOAD_IGNORED, os_data, os_len);
199 if (os_noffset < 0)
200 return NULL;
201
202 images->fit_hdr_os = map_sysmem(img_addr, 0);
203 images->fit_uname_os = fit_uname_kernel;
204 images->fit_uname_cfg = fit_uname_config;
205 images->fit_noffset_os = os_noffset;
206 break;
207#endif
208#ifdef CONFIG_ANDROID_BOOT_IMAGE
209 case IMAGE_FORMAT_ANDROID:
210 boot_img = buf;
211 vendor_boot_img = NULL;
212 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
213 boot_img = map_sysmem(get_abootimg_addr(), 0);
214 vendor_boot_img = map_sysmem(get_avendor_bootimg_addr(), 0);
215 }
216 printf("## Booting Android Image at 0x%08lx ...\n", img_addr);
217 if (android_image_get_kernel(boot_img, vendor_boot_img, images->verify,
218 os_data, os_len))
219 return NULL;
220 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
221 unmap_sysmem(vendor_boot_img);
222 unmap_sysmem(boot_img);
223 }
224 break;
225#endif
226 default:
227 printf("Wrong Image Format for %s command\n", cmdtp->name);
228 bootstage_error(BOOTSTAGE_ID_FIT_KERNEL_INFO);
229 return NULL;
230 }
231
232 debug(" kernel data at 0x%08lx, len = 0x%08lx (%ld)\n",
233 *os_data, *os_len, *os_len);
234
235 return buf;
236}
237
Simon Glassb6396402014-06-12 07:24:46 -0600238#ifdef CONFIG_LMB
Simon Glassd9d7c202022-09-06 20:26:50 -0600239static void boot_start_lmb(struct bootm_headers *images)
Simon Glassb6396402014-06-12 07:24:46 -0600240{
241 ulong mem_start;
242 phys_size_t mem_size;
243
Simon Glass723806c2017-08-03 12:22:15 -0600244 mem_start = env_get_bootm_low();
245 mem_size = env_get_bootm_size();
Simon Glassb6396402014-06-12 07:24:46 -0600246
Simon Goldschmidt9cc23232019-01-26 22:13:04 +0100247 lmb_init_and_reserve_range(&images->lmb, (phys_addr_t)mem_start,
248 mem_size, NULL);
Simon Glassb6396402014-06-12 07:24:46 -0600249}
250#else
251#define lmb_reserve(lmb, base, size)
Simon Glassd9d7c202022-09-06 20:26:50 -0600252static inline void boot_start_lmb(struct bootm_headers *images) { }
Simon Glassb6396402014-06-12 07:24:46 -0600253#endif
254
Simon Glassa50e8862023-11-18 14:04:54 -0700255static int bootm_start(void)
Simon Glassb6396402014-06-12 07:24:46 -0600256{
257 memset((void *)&images, 0, sizeof(images));
Simon Glassbfebc8c2017-08-03 12:22:13 -0600258 images.verify = env_get_yesno("verify");
Simon Glassb6396402014-06-12 07:24:46 -0600259
260 boot_start_lmb(&images);
261
262 bootstage_mark_name(BOOTSTAGE_ID_BOOTM_START, "bootm_start");
263 images.state = BOOTM_STATE_START;
264
265 return 0;
266}
267
Simon Glass921070b2023-11-18 14:04:55 -0700268static ulong bootm_data_addr(const char *addr_str)
Philippe Reynes9d46e632022-03-28 22:57:00 +0200269{
270 ulong addr;
271
Simon Glass921070b2023-11-18 14:04:55 -0700272 if (addr_str)
273 addr = hextoul(addr_str, NULL);
Philippe Reynes9d46e632022-03-28 22:57:00 +0200274 else
275 addr = image_load_addr;
276
277 return addr;
278}
279
Simon Glass921070b2023-11-18 14:04:55 -0700280/**
281 * bootm_pre_load() - Handle the pre-load processing
282 *
283 * This can be used to do a full signature check of the image, for example.
284 * It calls image_pre_load() with the data address of the image to check.
285 *
286 * @addr_str: String containing load address in hex, or NULL to use
287 * image_load_addr
288 * Return: 0 if OK, CMD_RET_FAILURE on failure
289 */
290static int bootm_pre_load(const char *addr_str)
Philippe Reynes9d46e632022-03-28 22:57:00 +0200291{
Simon Glass921070b2023-11-18 14:04:55 -0700292 ulong data_addr = bootm_data_addr(addr_str);
Philippe Reynes9d46e632022-03-28 22:57:00 +0200293 int ret = 0;
294
Simon Glass494bcf12023-02-05 15:36:24 -0700295 if (IS_ENABLED(CONFIG_CMD_BOOTM_PRE_LOAD))
Philippe Reynes9d46e632022-03-28 22:57:00 +0200296 ret = image_pre_load(data_addr);
297
298 if (ret)
299 ret = CMD_RET_FAILURE;
300
301 return ret;
302}
303
Simon Glass09140112020-05-10 11:40:03 -0600304static int bootm_find_os(struct cmd_tbl *cmdtp, int flag, int argc,
305 char *const argv[])
Simon Glassb6396402014-06-12 07:24:46 -0600306{
307 const void *os_hdr;
Safae Ouajih636da202023-02-06 00:50:17 +0100308#ifdef CONFIG_ANDROID_BOOT_IMAGE
309 const void *vendor_boot_img;
310 const void *boot_img;
311#endif
Simon Glassb6396402014-06-12 07:24:46 -0600312 bool ep_found = false;
Simon Glass90268b82014-10-19 21:11:24 -0600313 int ret;
Simon Glassb6396402014-06-12 07:24:46 -0600314
315 /* get kernel image header, start address and length */
316 os_hdr = boot_get_kernel(cmdtp, flag, argc, argv,
317 &images, &images.os.image_start, &images.os.image_len);
318 if (images.os.image_len == 0) {
319 puts("ERROR: can't get kernel image!\n");
320 return 1;
321 }
322
323 /* get image parameters */
324 switch (genimg_get_format(os_hdr)) {
Tom Rinic76c93a2019-05-23 07:14:07 -0400325#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Simon Glassb6396402014-06-12 07:24:46 -0600326 case IMAGE_FORMAT_LEGACY:
327 images.os.type = image_get_type(os_hdr);
328 images.os.comp = image_get_comp(os_hdr);
329 images.os.os = image_get_os(os_hdr);
330
331 images.os.end = image_get_image_end(os_hdr);
332 images.os.load = image_get_load(os_hdr);
Simon Glass90268b82014-10-19 21:11:24 -0600333 images.os.arch = image_get_arch(os_hdr);
Simon Glassb6396402014-06-12 07:24:46 -0600334 break;
335#endif
Simon Glassbf371b42021-09-25 19:43:20 -0600336#if CONFIG_IS_ENABLED(FIT)
Simon Glassb6396402014-06-12 07:24:46 -0600337 case IMAGE_FORMAT_FIT:
338 if (fit_image_get_type(images.fit_hdr_os,
339 images.fit_noffset_os,
340 &images.os.type)) {
341 puts("Can't get image type!\n");
342 bootstage_error(BOOTSTAGE_ID_FIT_TYPE);
343 return 1;
344 }
345
346 if (fit_image_get_comp(images.fit_hdr_os,
347 images.fit_noffset_os,
348 &images.os.comp)) {
349 puts("Can't get image compression!\n");
350 bootstage_error(BOOTSTAGE_ID_FIT_COMPRESSION);
351 return 1;
352 }
353
354 if (fit_image_get_os(images.fit_hdr_os, images.fit_noffset_os,
355 &images.os.os)) {
356 puts("Can't get image OS!\n");
357 bootstage_error(BOOTSTAGE_ID_FIT_OS);
358 return 1;
359 }
360
Simon Glass90268b82014-10-19 21:11:24 -0600361 if (fit_image_get_arch(images.fit_hdr_os,
362 images.fit_noffset_os,
363 &images.os.arch)) {
364 puts("Can't get image ARCH!\n");
365 return 1;
366 }
367
Simon Glassb6396402014-06-12 07:24:46 -0600368 images.os.end = fit_get_end(images.fit_hdr_os);
369
370 if (fit_image_get_load(images.fit_hdr_os, images.fit_noffset_os,
371 &images.os.load)) {
372 puts("Can't get image load address!\n");
373 bootstage_error(BOOTSTAGE_ID_FIT_LOADADDR);
374 return 1;
375 }
376 break;
377#endif
378#ifdef CONFIG_ANDROID_BOOT_IMAGE
379 case IMAGE_FORMAT_ANDROID:
Safae Ouajih636da202023-02-06 00:50:17 +0100380 boot_img = os_hdr;
381 vendor_boot_img = NULL;
382 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
383 boot_img = map_sysmem(get_abootimg_addr(), 0);
384 vendor_boot_img = map_sysmem(get_avendor_bootimg_addr(), 0);
385 }
Simon Glassb6396402014-06-12 07:24:46 -0600386 images.os.type = IH_TYPE_KERNEL;
Safae Ouajih636da202023-02-06 00:50:17 +0100387 images.os.comp = android_image_get_kcomp(boot_img, vendor_boot_img);
Simon Glassb6396402014-06-12 07:24:46 -0600388 images.os.os = IH_OS_LINUX;
Safae Ouajih636da202023-02-06 00:50:17 +0100389 images.os.end = android_image_get_end(boot_img, vendor_boot_img);
390 images.os.load = android_image_get_kload(boot_img, vendor_boot_img);
Ahmad Draidi86f46952014-10-23 20:50:07 +0300391 images.ep = images.os.load;
392 ep_found = true;
Safae Ouajih636da202023-02-06 00:50:17 +0100393 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
394 unmap_sysmem(vendor_boot_img);
395 unmap_sysmem(boot_img);
396 }
Simon Glassb6396402014-06-12 07:24:46 -0600397 break;
398#endif
399 default:
400 puts("ERROR: unknown image format type!\n");
401 return 1;
402 }
403
Simon Glass90268b82014-10-19 21:11:24 -0600404 /* If we have a valid setup.bin, we will use that for entry (x86) */
Simon Glass5bda35c2014-10-10 08:21:57 -0600405 if (images.os.arch == IH_ARCH_I386 ||
406 images.os.arch == IH_ARCH_X86_64) {
Simon Glass90268b82014-10-19 21:11:24 -0600407 ulong len;
408
409 ret = boot_get_setup(&images, IH_ARCH_I386, &images.ep, &len);
410 if (ret < 0 && ret != -ENOENT) {
411 puts("Could not find a valid setup.bin for x86\n");
412 return 1;
413 }
414 /* Kernel entry point is the setup.bin */
415 } else if (images.legacy_hdr_valid) {
Simon Glassb6396402014-06-12 07:24:46 -0600416 images.ep = image_get_ep(&images.legacy_hdr_os_copy);
Simon Glassbf371b42021-09-25 19:43:20 -0600417#if CONFIG_IS_ENABLED(FIT)
Simon Glassb6396402014-06-12 07:24:46 -0600418 } else if (images.fit_uname_os) {
419 int ret;
420
421 ret = fit_image_get_entry(images.fit_hdr_os,
422 images.fit_noffset_os, &images.ep);
423 if (ret) {
424 puts("Can't get entry point property!\n");
425 return 1;
426 }
427#endif
428 } else if (!ep_found) {
429 puts("Could not find kernel entry point!\n");
430 return 1;
431 }
432
433 if (images.os.type == IH_TYPE_KERNEL_NOLOAD) {
Simon Glassef65aa32023-02-05 15:36:23 -0700434 if (IS_ENABLED(CONFIG_CMD_BOOTI) &&
Heinrich Schuchardt4533b3d2023-06-13 08:18:27 +0200435 images.os.arch == IH_ARCH_ARM64 &&
436 images.os.os == IH_OS_LINUX) {
Marek Vasut487b5fa2018-06-13 06:13:33 +0200437 ulong image_addr;
438 ulong image_size;
439
440 ret = booti_setup(images.os.image_start, &image_addr,
441 &image_size, true);
442 if (ret != 0)
443 return 1;
444
445 images.os.type = IH_TYPE_KERNEL;
446 images.os.load = image_addr;
447 images.ep = image_addr;
448 } else {
449 images.os.load = images.os.image_start;
450 images.ep += images.os.image_start;
451 }
Simon Glassb6396402014-06-12 07:24:46 -0600452 }
453
Simon Glass7a80de42016-02-24 09:14:42 -0700454 images.os.start = map_to_sysmem(os_hdr);
Simon Glassb6396402014-06-12 07:24:46 -0600455
456 return 0;
457}
458
Karl Apsited52e8572015-05-21 09:52:49 -0400459/**
460 * bootm_find_images - wrapper to find and locate various images
461 * @flag: Ignored Argument
462 * @argc: command argument count
463 * @argv: command argument list
Tero Kristofbde7582020-06-12 15:41:20 +0300464 * @start: OS image start address
465 * @size: OS image size
Karl Apsited52e8572015-05-21 09:52:49 -0400466 *
467 * boot_find_images() will attempt to load an available ramdisk,
468 * flattened device tree, as well as specifically marked
469 * "loadable" images (loadables are FIT only)
470 *
471 * Note: bootm_find_images will skip an image if it is not found
472 *
473 * @return:
474 * 0, if all existing images were loaded correctly
475 * 1, if an image is found but corrupted, or invalid
476 */
Tero Kristofbde7582020-06-12 15:41:20 +0300477int bootm_find_images(int flag, int argc, char *const argv[], ulong start,
478 ulong size)
Simon Glassb6396402014-06-12 07:24:46 -0600479{
480 int ret;
481
482 /* find ramdisk */
483 ret = boot_get_ramdisk(argc, argv, &images, IH_INITRD_ARCH,
484 &images.rd_start, &images.rd_end);
485 if (ret) {
486 puts("Ramdisk image is corrupt or invalid\n");
487 return 1;
488 }
489
Tero Kristofbde7582020-06-12 15:41:20 +0300490 /* check if ramdisk overlaps OS image */
491 if (images.rd_start && (((ulong)images.rd_start >= start &&
Jaehoon Chungef4f4f12020-10-21 14:17:03 +0900492 (ulong)images.rd_start < start + size) ||
493 ((ulong)images.rd_end > start &&
494 (ulong)images.rd_end <= start + size) ||
495 ((ulong)images.rd_start < start &&
496 (ulong)images.rd_end >= start + size))) {
Tero Kristofbde7582020-06-12 15:41:20 +0300497 printf("ERROR: RD image overlaps OS image (OS=0x%lx..0x%lx)\n",
498 start, start + size);
499 return 1;
500 }
501
Simon Glass0c303f92021-09-25 19:43:21 -0600502#if CONFIG_IS_ENABLED(OF_LIBFDT)
Simon Glassb6396402014-06-12 07:24:46 -0600503 /* find flattened device tree */
504 ret = boot_get_fdt(flag, argc, argv, IH_ARCH_DEFAULT, &images,
505 &images.ft_addr, &images.ft_len);
506 if (ret) {
507 puts("Could not find a valid device tree\n");
508 return 1;
509 }
Tero Kristofbde7582020-06-12 15:41:20 +0300510
511 /* check if FDT overlaps OS image */
512 if (images.ft_addr &&
513 (((ulong)images.ft_addr >= start &&
Pali Rohár5acfdfb2022-08-27 14:48:10 +0200514 (ulong)images.ft_addr < start + size) ||
Tero Kristofbde7582020-06-12 15:41:20 +0300515 ((ulong)images.ft_addr + images.ft_len >= start &&
Pali Rohár5acfdfb2022-08-27 14:48:10 +0200516 (ulong)images.ft_addr + images.ft_len < start + size))) {
Tero Kristofbde7582020-06-12 15:41:20 +0300517 printf("ERROR: FDT image overlaps OS image (OS=0x%lx..0x%lx)\n",
518 start, start + size);
519 return 1;
520 }
521
Simon Glass3a09f382023-02-05 15:36:30 -0700522 if (IS_ENABLED(CONFIG_CMD_FDT))
Simon Goldschmidt596be5f2018-12-17 20:14:42 +0100523 set_working_fdt_addr(map_to_sysmem(images.ft_addr));
Simon Glassb6396402014-06-12 07:24:46 -0600524#endif
525
Simon Glassbf371b42021-09-25 19:43:20 -0600526#if CONFIG_IS_ENABLED(FIT)
Simon Glass4ed37ab2021-09-25 07:03:20 -0600527 if (IS_ENABLED(CONFIG_FPGA)) {
528 /* find bitstreams */
529 ret = boot_get_fpga(argc, argv, &images, IH_ARCH_DEFAULT,
530 NULL, NULL);
531 if (ret) {
532 printf("FPGA image is corrupted or invalid\n");
533 return 1;
534 }
Michal Simek62afc602016-05-17 14:03:50 +0200535 }
Michal Simek62afc602016-05-17 14:03:50 +0200536
Karl Apsite84a07db2015-05-21 09:52:48 -0400537 /* find all of the loadables */
538 ret = boot_get_loadable(argc, argv, &images, IH_ARCH_DEFAULT,
539 NULL, NULL);
540 if (ret) {
541 printf("Loadable(s) is corrupt or invalid\n");
542 return 1;
543 }
Karl Apsite84a07db2015-05-21 09:52:48 -0400544#endif
545
Simon Glassb6396402014-06-12 07:24:46 -0600546 return 0;
547}
548
Simon Glass09140112020-05-10 11:40:03 -0600549static int bootm_find_other(struct cmd_tbl *cmdtp, int flag, int argc,
550 char *const argv[])
Simon Glassb6396402014-06-12 07:24:46 -0600551{
552 if (((images.os.type == IH_TYPE_KERNEL) ||
553 (images.os.type == IH_TYPE_KERNEL_NOLOAD) ||
554 (images.os.type == IH_TYPE_MULTI)) &&
555 (images.os.os == IH_OS_LINUX ||
556 images.os.os == IH_OS_VXWORKS))
Tero Kristofbde7582020-06-12 15:41:20 +0300557 return bootm_find_images(flag, argc, argv, 0, 0);
Simon Glassb6396402014-06-12 07:24:46 -0600558
559 return 0;
560}
Simon Glass40e59752014-12-02 13:17:30 -0700561#endif /* USE_HOSTC */
562
Julius Werner20908542019-07-24 19:37:54 -0700563#if !defined(USE_HOSTCC) || defined(CONFIG_FIT_SIGNATURE)
Simon Glass3086c052014-12-02 13:17:37 -0700564/**
565 * handle_decomp_error() - display a decompression error
566 *
567 * This function tries to produce a useful message. In the case where the
568 * uncompressed size is the same as the available space, we can assume that
569 * the image is too large for the buffer.
570 *
571 * @comp_type: Compression type being used (IH_COMP_...)
572 * @uncomp_size: Number of bytes uncompressed
Tom Rinic45568c2022-06-25 19:29:46 -0400573 * @buf_size: Number of bytes the decompresion buffer was
Julius Werner20908542019-07-24 19:37:54 -0700574 * @ret: errno error code received from compression library
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100575 * Return: Appropriate BOOTM_ERR_ error code
Simon Glass3086c052014-12-02 13:17:37 -0700576 */
Tom Rinic45568c2022-06-25 19:29:46 -0400577static int handle_decomp_error(int comp_type, size_t uncomp_size,
578 size_t buf_size, int ret)
Simon Glass40e59752014-12-02 13:17:30 -0700579{
Simon Glass3086c052014-12-02 13:17:37 -0700580 const char *name = genimg_get_comp_name(comp_type);
581
Julius Werner20908542019-07-24 19:37:54 -0700582 /* ENOSYS means unimplemented compression type, don't reset. */
583 if (ret == -ENOSYS)
584 return BOOTM_ERR_UNIMPLEMENTED;
585
Tom Rinic45568c2022-06-25 19:29:46 -0400586 if (uncomp_size >= buf_size)
Simon Glass3086c052014-12-02 13:17:37 -0700587 printf("Image too large: increase CONFIG_SYS_BOOTM_LEN\n");
Simon Glass40e59752014-12-02 13:17:30 -0700588 else
Simon Glass3086c052014-12-02 13:17:37 -0700589 printf("%s: uncompress error %d\n", name, ret);
590
591 /*
592 * The decompression routines are now safe, so will not write beyond
593 * their bounds. Probably it is not necessary to reset, but maintain
594 * the current behaviour for now.
595 */
596 printf("Must RESET board to recover\n");
Simon Glass40e59752014-12-02 13:17:30 -0700597#ifndef USE_HOSTCC
598 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
599#endif
600
601 return BOOTM_ERR_RESET;
602}
Julius Werner20908542019-07-24 19:37:54 -0700603#endif
Simon Glass2b164f12014-06-12 07:24:52 -0600604
Simon Glassce1400f2014-06-12 07:24:53 -0600605#ifndef USE_HOSTCC
Simon Glassd9d7c202022-09-06 20:26:50 -0600606static int bootm_load_os(struct bootm_headers *images, int boot_progress)
Simon Glass2b164f12014-06-12 07:24:52 -0600607{
Simon Glassda79b2f2022-09-06 20:26:51 -0600608 struct image_info os = images->os;
Simon Glass2b164f12014-06-12 07:24:52 -0600609 ulong load = os.load;
Tom Rinicc955352018-05-01 12:32:37 -0400610 ulong load_end;
Simon Glass2b164f12014-06-12 07:24:52 -0600611 ulong blob_start = os.start;
612 ulong blob_end = os.end;
613 ulong image_start = os.image_start;
614 ulong image_len = os.image_len;
Bryan O'Donoghuebbac9222018-04-15 11:48:17 +0100615 ulong flush_start = ALIGN_DOWN(load, ARCH_DMA_MINALIGN);
Simon Glass2b164f12014-06-12 07:24:52 -0600616 bool no_overlap;
617 void *load_buf, *image_buf;
618 int err;
619
620 load_buf = map_sysmem(load, 0);
621 image_buf = map_sysmem(os.image_start, image_len);
Julius Werner20908542019-07-24 19:37:54 -0700622 err = image_decomp(os.comp, load, os.image_start, os.type,
623 load_buf, image_buf, image_len,
624 CONFIG_SYS_BOOTM_LEN, &load_end);
Simon Glass2b164f12014-06-12 07:24:52 -0600625 if (err) {
Tom Rinic45568c2022-06-25 19:29:46 -0400626 err = handle_decomp_error(os.comp, load_end - load,
627 CONFIG_SYS_BOOTM_LEN, err);
Simon Glass2b164f12014-06-12 07:24:52 -0600628 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
629 return err;
630 }
Heinrich Schuchardt21d39462020-08-30 11:34:12 +0200631 /* We need the decompressed image size in the next steps */
632 images->os.image_len = load_end - load;
Bryan O'Donoghuebbac9222018-04-15 11:48:17 +0100633
Trent Piephob4353b32019-03-27 23:50:09 +0000634 flush_cache(flush_start, ALIGN(load_end, ARCH_DMA_MINALIGN) - flush_start);
Simon Glassb6396402014-06-12 07:24:46 -0600635
Tom Rinicc955352018-05-01 12:32:37 -0400636 debug(" kernel loaded at 0x%08lx, end = 0x%08lx\n", load, load_end);
Simon Glassb6396402014-06-12 07:24:46 -0600637 bootstage_mark(BOOTSTAGE_ID_KERNEL_LOADED);
638
Simon Glass2b164f12014-06-12 07:24:52 -0600639 no_overlap = (os.comp == IH_COMP_NONE && load == image_start);
640
Tom Rinicc955352018-05-01 12:32:37 -0400641 if (!no_overlap && load < blob_end && load_end > blob_start) {
Simon Glassb6396402014-06-12 07:24:46 -0600642 debug("images.os.start = 0x%lX, images.os.end = 0x%lx\n",
643 blob_start, blob_end);
644 debug("images.os.load = 0x%lx, load_end = 0x%lx\n", load,
Tom Rinicc955352018-05-01 12:32:37 -0400645 load_end);
Simon Glassb6396402014-06-12 07:24:46 -0600646
647 /* Check what type of image this is. */
648 if (images->legacy_hdr_valid) {
649 if (image_get_type(&images->legacy_hdr_os_copy)
650 == IH_TYPE_MULTI)
651 puts("WARNING: legacy format multi component image overwritten\n");
652 return BOOTM_ERR_OVERLAP;
653 } else {
654 puts("ERROR: new format image overwritten - must RESET the board to recover\n");
655 bootstage_error(BOOTSTAGE_ID_OVERWRITTEN);
656 return BOOTM_ERR_RESET;
657 }
658 }
659
Tom Rinicc955352018-05-01 12:32:37 -0400660 lmb_reserve(&images->lmb, images->os.load, (load_end -
661 images->os.load));
Simon Glassb6396402014-06-12 07:24:46 -0600662 return 0;
663}
664
665/**
666 * bootm_disable_interrupts() - Disable interrupts in preparation for load/boot
667 *
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100668 * Return: interrupt flag (0 if interrupts were disabled, non-zero if they were
Simon Glassb6396402014-06-12 07:24:46 -0600669 * enabled)
670 */
671ulong bootm_disable_interrupts(void)
672{
673 ulong iflag;
674
675 /*
676 * We have reached the point of no return: we are going to
677 * overwrite all exception vector code, so we cannot easily
678 * recover from any failures any more...
679 */
680 iflag = disable_interrupts();
681#ifdef CONFIG_NETCONSOLE
682 /* Stop the ethernet stack if NetConsole could have left it up */
683 eth_halt();
Simon Glassb6396402014-06-12 07:24:46 -0600684#endif
685
686#if defined(CONFIG_CMD_USB)
687 /*
688 * turn off USB to prevent the host controller from writing to the
689 * SDRAM while Linux is booting. This could happen (at least for OHCI
690 * controller), because the HCCA (Host Controller Communication Area)
691 * lies within the SDRAM and the host controller writes continously to
692 * this area (as busmaster!). The HccaFrameNumber is for example
693 * updated every 1 ms within the HCCA structure in SDRAM! For more
694 * details see the OpenHCI specification.
695 */
696 usb_stop();
697#endif
698 return iflag;
699}
700
Simon Glass6cd92b12020-11-05 10:33:42 -0700701#define CONSOLE_ARG "console="
Sean Andersonba9aa402022-05-19 18:26:05 -0400702#define NULL_CONSOLE (CONSOLE_ARG "ttynull")
703#define CONSOLE_ARG_SIZE sizeof(NULL_CONSOLE)
Simon Glassb6396402014-06-12 07:24:46 -0600704
Simon Glassb6386f32020-11-05 10:33:43 -0700705/**
706 * fixup_silent_linux() - Handle silencing the linux boot if required
707 *
708 * This uses the silent_linux envvar to control whether to add/set a "console="
709 * parameter to the command line
710 *
711 * @buf: Buffer containing the string to process
712 * @maxlen: Maximum length of buffer
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100713 * Return: 0 if OK, -ENOSPC if @maxlen is too small
Simon Glassb6386f32020-11-05 10:33:43 -0700714 */
715static int fixup_silent_linux(char *buf, int maxlen)
Simon Glassb6396402014-06-12 07:24:46 -0600716{
Simon Glassb6396402014-06-12 07:24:46 -0600717 int want_silent;
Simon Glassb6386f32020-11-05 10:33:43 -0700718 char *cmdline;
719 int size;
Simon Glassb6396402014-06-12 07:24:46 -0600720
Simon Glassb6386f32020-11-05 10:33:43 -0700721 /*
722 * Move the input string to the end of buffer. The output string will be
723 * built up at the start.
724 */
725 size = strlen(buf) + 1;
726 if (size * 2 > maxlen)
727 return -ENOSPC;
728 cmdline = buf + maxlen - size;
729 memmove(cmdline, buf, size);
Simon Glassb6396402014-06-12 07:24:46 -0600730 /*
731 * Only fix cmdline when requested. The environment variable can be:
732 *
733 * no - we never fixup
734 * yes - we always fixup
735 * unset - we rely on the console silent flag
736 */
Simon Glassbfebc8c2017-08-03 12:22:13 -0600737 want_silent = env_get_yesno("silent_linux");
Simon Glassb6396402014-06-12 07:24:46 -0600738 if (want_silent == 0)
Simon Glass4ae42642020-11-05 10:33:39 -0700739 return 0;
Simon Glassb6396402014-06-12 07:24:46 -0600740 else if (want_silent == -1 && !(gd->flags & GD_FLG_SILENT))
Simon Glass4ae42642020-11-05 10:33:39 -0700741 return 0;
Simon Glassb6396402014-06-12 07:24:46 -0600742
743 debug("before silent fix-up: %s\n", cmdline);
Simon Glassb6386f32020-11-05 10:33:43 -0700744 if (*cmdline) {
Simon Glassb6396402014-06-12 07:24:46 -0600745 char *start = strstr(cmdline, CONSOLE_ARG);
746
Simon Glassb6386f32020-11-05 10:33:43 -0700747 /* Check space for maximum possible new command line */
748 if (size + CONSOLE_ARG_SIZE > maxlen)
Simon Glass4ae42642020-11-05 10:33:39 -0700749 return -ENOSPC;
Simon Glassb6396402014-06-12 07:24:46 -0600750
751 if (start) {
752 char *end = strchr(start, ' ');
Simon Glass6cd92b12020-11-05 10:33:42 -0700753 int start_bytes;
Simon Glassb6396402014-06-12 07:24:46 -0600754
Sean Andersonba9aa402022-05-19 18:26:05 -0400755 start_bytes = start - cmdline;
Simon Glass6cd92b12020-11-05 10:33:42 -0700756 strncpy(buf, cmdline, start_bytes);
Sean Andersonba9aa402022-05-19 18:26:05 -0400757 strncpy(buf + start_bytes, NULL_CONSOLE, CONSOLE_ARG_SIZE);
Simon Glassb6396402014-06-12 07:24:46 -0600758 if (end)
Sean Andersonba9aa402022-05-19 18:26:05 -0400759 strcpy(buf + start_bytes + CONSOLE_ARG_SIZE - 1, end);
Simon Glassb6396402014-06-12 07:24:46 -0600760 else
Sean Andersonba9aa402022-05-19 18:26:05 -0400761 buf[start_bytes + CONSOLE_ARG_SIZE] = '\0';
Simon Glassb6396402014-06-12 07:24:46 -0600762 } else {
Sean Andersonba9aa402022-05-19 18:26:05 -0400763 sprintf(buf, "%s %s", cmdline, NULL_CONSOLE);
Simon Glassb6396402014-06-12 07:24:46 -0600764 }
Simon Glassb6386f32020-11-05 10:33:43 -0700765 if (buf + strlen(buf) >= cmdline)
766 return -ENOSPC;
Simon Glassb6396402014-06-12 07:24:46 -0600767 } else {
Sean Andersonba9aa402022-05-19 18:26:05 -0400768 if (maxlen < CONSOLE_ARG_SIZE)
Simon Glassb6386f32020-11-05 10:33:43 -0700769 return -ENOSPC;
Sean Andersonba9aa402022-05-19 18:26:05 -0400770 strcpy(buf, NULL_CONSOLE);
Simon Glassb6396402014-06-12 07:24:46 -0600771 }
Simon Glassb6386f32020-11-05 10:33:43 -0700772 debug("after silent fix-up: %s\n", buf);
Simon Glassb6396402014-06-12 07:24:46 -0600773
Simon Glassb6386f32020-11-05 10:33:43 -0700774 return 0;
775}
776
Simon Glass51bb3382020-11-05 10:33:48 -0700777/**
778 * process_subst() - Handle substitution of ${...} fields in the environment
779 *
780 * Handle variable substitution in the provided buffer
781 *
782 * @buf: Buffer containing the string to process
783 * @maxlen: Maximum length of buffer
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100784 * Return: 0 if OK, -ENOSPC if @maxlen is too small
Simon Glass51bb3382020-11-05 10:33:48 -0700785 */
786static int process_subst(char *buf, int maxlen)
787{
788 char *cmdline;
789 int size;
790 int ret;
791
792 /* Move to end of buffer */
793 size = strlen(buf) + 1;
794 cmdline = buf + maxlen - size;
795 if (buf + size > cmdline)
796 return -ENOSPC;
797 memmove(cmdline, buf, size);
798
799 ret = cli_simple_process_macros(cmdline, buf, cmdline - buf);
800
801 return ret;
802}
803
Simon Glass4448fe82020-11-05 10:33:45 -0700804int bootm_process_cmdline(char *buf, int maxlen, int flags)
805{
806 int ret;
807
808 /* Check config first to enable compiler to eliminate code */
809 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) &&
810 !IS_ENABLED(CONFIG_SILENT_U_BOOT_ONLY) &&
811 (flags & BOOTM_CL_SILENT)) {
812 ret = fixup_silent_linux(buf, maxlen);
813 if (ret)
814 return log_msg_ret("silent", ret);
815 }
Simon Glassa8d69622021-03-15 18:11:23 +1300816 if (IS_ENABLED(CONFIG_BOOTARGS_SUBST) && IS_ENABLED(CONFIG_CMDLINE) &&
817 (flags & BOOTM_CL_SUBST)) {
Simon Glass51bb3382020-11-05 10:33:48 -0700818 ret = process_subst(buf, maxlen);
819 if (ret)
Simon Glass185756e2021-02-06 09:57:35 -0700820 return log_msg_ret("subst", ret);
Simon Glass51bb3382020-11-05 10:33:48 -0700821 }
Simon Glass4448fe82020-11-05 10:33:45 -0700822
823 return 0;
824}
825
Simon Glassb3c01672020-11-05 10:33:44 -0700826int bootm_process_cmdline_env(int flags)
Simon Glassb6386f32020-11-05 10:33:43 -0700827{
828 const int maxlen = MAX_CMDLINE_SIZE;
Simon Glassb3c01672020-11-05 10:33:44 -0700829 bool do_silent;
Simon Glassb6386f32020-11-05 10:33:43 -0700830 const char *env;
831 char *buf;
832 int ret;
833
834 /* First check if any action is needed */
835 do_silent = IS_ENABLED(CONFIG_SILENT_CONSOLE) &&
Simon Glassb3c01672020-11-05 10:33:44 -0700836 !IS_ENABLED(CONFIG_SILENT_U_BOOT_ONLY) && (flags & BOOTM_CL_SILENT);
Simon Glass51bb3382020-11-05 10:33:48 -0700837 if (!do_silent && !IS_ENABLED(CONFIG_BOOTARGS_SUBST))
Simon Glassb6386f32020-11-05 10:33:43 -0700838 return 0;
839
840 env = env_get("bootargs");
841 if (env && strlen(env) >= maxlen)
842 return -E2BIG;
843 buf = malloc(maxlen);
844 if (!buf)
845 return -ENOMEM;
846 if (env)
847 strcpy(buf, env);
848 else
849 *buf = '\0';
Simon Glass4448fe82020-11-05 10:33:45 -0700850 ret = bootm_process_cmdline(buf, maxlen, flags);
Simon Glassb6386f32020-11-05 10:33:43 -0700851 if (!ret) {
852 ret = env_set("bootargs", buf);
853
854 /*
855 * If buf is "" and bootargs does not exist, this will produce
856 * an error trying to delete bootargs. Ignore it
857 */
858 if (ret == -ENOENT)
859 ret = 0;
860 }
Simon Glassb6396402014-06-12 07:24:46 -0600861 free(buf);
Simon Glassb6386f32020-11-05 10:33:43 -0700862 if (ret)
863 return log_msg_ret("env", ret);
Simon Glass4ae42642020-11-05 10:33:39 -0700864
865 return 0;
Simon Glassb6396402014-06-12 07:24:46 -0600866}
Simon Glassb6396402014-06-12 07:24:46 -0600867
Eddie Jamesdec166d2023-10-24 10:43:50 -0500868int bootm_measure(struct bootm_headers *images)
869{
870 int ret = 0;
871
872 /* Skip measurement if EFI is going to do it */
873 if (images->os.os == IH_OS_EFI &&
874 IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL) &&
875 IS_ENABLED(CONFIG_BOOTM_EFI))
876 return ret;
877
878 if (IS_ENABLED(CONFIG_MEASURED_BOOT)) {
879 struct tcg2_event_log elog;
880 struct udevice *dev;
881 void *initrd_buf;
882 void *image_buf;
883 const char *s;
884 u32 rd_len;
885 bool ign;
886
887 elog.log_size = 0;
888 ign = IS_ENABLED(CONFIG_MEASURE_IGNORE_LOG);
889 ret = tcg2_measurement_init(&dev, &elog, ign);
890 if (ret)
891 return ret;
892
893 image_buf = map_sysmem(images->os.image_start,
894 images->os.image_len);
895 ret = tcg2_measure_data(dev, &elog, 8, images->os.image_len,
896 image_buf, EV_COMPACT_HASH,
897 strlen("linux") + 1, (u8 *)"linux");
898 if (ret)
899 goto unmap_image;
900
901 rd_len = images->rd_end - images->rd_start;
902 initrd_buf = map_sysmem(images->rd_start, rd_len);
903 ret = tcg2_measure_data(dev, &elog, 9, rd_len, initrd_buf,
904 EV_COMPACT_HASH, strlen("initrd") + 1,
905 (u8 *)"initrd");
906 if (ret)
907 goto unmap_initrd;
908
909 if (IS_ENABLED(CONFIG_MEASURE_DEVICETREE)) {
910 ret = tcg2_measure_data(dev, &elog, 0, images->ft_len,
911 (u8 *)images->ft_addr,
912 EV_TABLE_OF_DEVICES,
913 strlen("dts") + 1,
914 (u8 *)"dts");
915 if (ret)
916 goto unmap_initrd;
917 }
918
919 s = env_get("bootargs");
920 if (!s)
921 s = "";
922 ret = tcg2_measure_data(dev, &elog, 1, strlen(s) + 1, (u8 *)s,
923 EV_PLATFORM_CONFIG_FLAGS,
924 strlen(s) + 1, (u8 *)s);
925
926unmap_initrd:
927 unmap_sysmem(initrd_buf);
928
929unmap_image:
930 unmap_sysmem(image_buf);
931 tcg2_measurement_term(dev, &elog, ret != 0);
932 }
933
934 return ret;
935}
936
Simon Glassb6396402014-06-12 07:24:46 -0600937/**
938 * Execute selected states of the bootm command.
939 *
940 * Note the arguments to this state must be the first argument, Any 'bootm'
941 * or sub-command arguments must have already been taken.
942 *
943 * Note that if states contains more than one flag it MUST contain
944 * BOOTM_STATE_START, since this handles and consumes the command line args.
945 *
946 * Also note that aside from boot_os_fn functions and bootm_load_os no other
947 * functions we store the return value of in 'ret' may use a negative return
948 * value, without special handling.
949 *
950 * @param cmdtp Pointer to bootm command table entry
951 * @param flag Command flags (CMD_FLAG_...)
952 * @param argc Number of subcommand arguments (0 = no arguments)
953 * @param argv Arguments
954 * @param states Mask containing states to run (BOOTM_STATE_...)
955 * @param images Image header information
956 * @param boot_progress 1 to show boot progress, 0 to not do this
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100957 * Return: 0 if ok, something else on error. Some errors will cause this
Simon Glassb6396402014-06-12 07:24:46 -0600958 * function to perform a reboot! If states contains BOOTM_STATE_OS_GO
959 * then the intent is to boot an OS, so this function will not return
960 * unless the image type is standalone.
961 */
Simon Glass09140112020-05-10 11:40:03 -0600962int do_bootm_states(struct cmd_tbl *cmdtp, int flag, int argc,
Simon Glassd9d7c202022-09-06 20:26:50 -0600963 char *const argv[], int states, struct bootm_headers *images,
Simon Glass09140112020-05-10 11:40:03 -0600964 int boot_progress)
Simon Glassb6396402014-06-12 07:24:46 -0600965{
966 boot_os_fn *boot_fn;
967 ulong iflag = 0;
968 int ret = 0, need_boot_fn;
969
970 images->state |= states;
971
972 /*
973 * Work through the states and see how far we get. We stop on
974 * any error.
975 */
976 if (states & BOOTM_STATE_START)
Simon Glassa50e8862023-11-18 14:04:54 -0700977 ret = bootm_start();
Simon Glassb6396402014-06-12 07:24:46 -0600978
Philippe Reynes9d46e632022-03-28 22:57:00 +0200979 if (!ret && (states & BOOTM_STATE_PRE_LOAD))
Simon Glass921070b2023-11-18 14:04:55 -0700980 ret = bootm_pre_load(argv[0]);
Philippe Reynes9d46e632022-03-28 22:57:00 +0200981
Simon Glassb6396402014-06-12 07:24:46 -0600982 if (!ret && (states & BOOTM_STATE_FINDOS))
983 ret = bootm_find_os(cmdtp, flag, argc, argv);
984
Zubair Lutfullah Kakakhelba079842016-09-09 09:18:58 +0100985 if (!ret && (states & BOOTM_STATE_FINDOTHER))
Simon Glassb6396402014-06-12 07:24:46 -0600986 ret = bootm_find_other(cmdtp, flag, argc, argv);
Simon Glassb6396402014-06-12 07:24:46 -0600987
Eddie Jamesdec166d2023-10-24 10:43:50 -0500988 if (IS_ENABLED(CONFIG_MEASURED_BOOT) && !ret &&
989 (states & BOOTM_STATE_MEASURE))
990 bootm_measure(images);
991
Simon Glassb6396402014-06-12 07:24:46 -0600992 /* Load the OS */
993 if (!ret && (states & BOOTM_STATE_LOADOS)) {
Simon Glassb6396402014-06-12 07:24:46 -0600994 iflag = bootm_disable_interrupts();
Tom Rinicc955352018-05-01 12:32:37 -0400995 ret = bootm_load_os(images, 0);
996 if (ret && ret != BOOTM_ERR_OVERLAP)
Simon Glassb6396402014-06-12 07:24:46 -0600997 goto err;
998 else if (ret == BOOTM_ERR_OVERLAP)
999 ret = 0;
Simon Glassb6396402014-06-12 07:24:46 -06001000 }
1001
1002 /* Relocate the ramdisk */
1003#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
1004 if (!ret && (states & BOOTM_STATE_RAMDISK)) {
1005 ulong rd_len = images->rd_end - images->rd_start;
1006
1007 ret = boot_ramdisk_high(&images->lmb, images->rd_start,
1008 rd_len, &images->initrd_start, &images->initrd_end);
1009 if (!ret) {
Simon Glass018f5302017-08-03 12:22:10 -06001010 env_set_hex("initrd_start", images->initrd_start);
1011 env_set_hex("initrd_end", images->initrd_end);
Simon Glassb6396402014-06-12 07:24:46 -06001012 }
1013 }
1014#endif
Simon Glass0c303f92021-09-25 19:43:21 -06001015#if CONFIG_IS_ENABLED(OF_LIBFDT) && defined(CONFIG_LMB)
Simon Glassb6396402014-06-12 07:24:46 -06001016 if (!ret && (states & BOOTM_STATE_FDT)) {
1017 boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr);
1018 ret = boot_relocate_fdt(&images->lmb, &images->ft_addr,
1019 &images->ft_len);
1020 }
1021#endif
1022
1023 /* From now on, we need the OS boot function */
1024 if (ret)
1025 return ret;
1026 boot_fn = bootm_os_get_boot_func(images->os.os);
1027 need_boot_fn = states & (BOOTM_STATE_OS_CMDLINE |
1028 BOOTM_STATE_OS_BD_T | BOOTM_STATE_OS_PREP |
1029 BOOTM_STATE_OS_FAKE_GO | BOOTM_STATE_OS_GO);
1030 if (boot_fn == NULL && need_boot_fn) {
1031 if (iflag)
1032 enable_interrupts();
1033 printf("ERROR: booting os '%s' (%d) is not supported\n",
1034 genimg_get_os_name(images->os.os), images->os.os);
1035 bootstage_error(BOOTSTAGE_ID_CHECK_BOOT_OS);
1036 return 1;
1037 }
1038
Hector Palacios19e86492016-07-11 12:34:37 +02001039
Simon Glassb6396402014-06-12 07:24:46 -06001040 /* Call various other states that are not generally used */
1041 if (!ret && (states & BOOTM_STATE_OS_CMDLINE))
1042 ret = boot_fn(BOOTM_STATE_OS_CMDLINE, argc, argv, images);
1043 if (!ret && (states & BOOTM_STATE_OS_BD_T))
1044 ret = boot_fn(BOOTM_STATE_OS_BD_T, argc, argv, images);
Hector Palacios19e86492016-07-11 12:34:37 +02001045 if (!ret && (states & BOOTM_STATE_OS_PREP)) {
Simon Glass51bb3382020-11-05 10:33:48 -07001046 ret = bootm_process_cmdline_env(images->os.os == IH_OS_LINUX);
Simon Glassd9477a02020-11-05 10:33:41 -07001047 if (ret) {
1048 printf("Cmdline setup failed (err=%d)\n", ret);
1049 ret = CMD_RET_FAILURE;
1050 goto err;
Simon Glass4ae42642020-11-05 10:33:39 -07001051 }
Simon Glassb6396402014-06-12 07:24:46 -06001052 ret = boot_fn(BOOTM_STATE_OS_PREP, argc, argv, images);
Hector Palacios19e86492016-07-11 12:34:37 +02001053 }
Simon Glassb6396402014-06-12 07:24:46 -06001054
1055#ifdef CONFIG_TRACE
1056 /* Pretend to run the OS, then run a user command */
1057 if (!ret && (states & BOOTM_STATE_OS_FAKE_GO)) {
Simon Glass00caae62017-08-03 12:22:12 -06001058 char *cmd_list = env_get("fakegocmd");
Simon Glassb6396402014-06-12 07:24:46 -06001059
1060 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_FAKE_GO,
1061 images, boot_fn);
1062 if (!ret && cmd_list)
1063 ret = run_command_list(cmd_list, -1, flag);
1064 }
1065#endif
1066
1067 /* Check for unsupported subcommand. */
1068 if (ret) {
Simon Glass13819f02022-10-11 09:47:07 -06001069 printf("subcommand failed (err=%d)\n", ret);
Simon Glassb6396402014-06-12 07:24:46 -06001070 return ret;
1071 }
1072
1073 /* Now run the OS! We hope this doesn't return */
1074 if (!ret && (states & BOOTM_STATE_OS_GO))
1075 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_GO,
1076 images, boot_fn);
1077
1078 /* Deal with any fallout */
1079err:
1080 if (iflag)
1081 enable_interrupts();
1082
1083 if (ret == BOOTM_ERR_UNIMPLEMENTED)
1084 bootstage_error(BOOTSTAGE_ID_DECOMP_UNIMPL);
1085 else if (ret == BOOTM_ERR_RESET)
1086 do_reset(cmdtp, flag, argc, argv);
1087
1088 return ret;
1089}
1090
Simon Glassdaffb0b2023-07-30 11:17:02 -06001091int bootm_boot_start(ulong addr, const char *cmdline)
1092{
1093 static struct cmd_tbl cmd = {"bootm"};
1094 char addr_str[30];
1095 char *argv[] = {addr_str, NULL};
1096 int states;
1097 int ret;
1098
1099 /*
1100 * TODO(sjg@chromium.org): This uses the command-line interface, but
1101 * should not. To clean this up, the various bootm states need to be
1102 * passed an info structure instead of cmdline flags. Then this can
1103 * set up the required info and move through the states without needing
1104 * the command line.
1105 */
1106 states = BOOTM_STATE_START | BOOTM_STATE_FINDOS | BOOTM_STATE_PRE_LOAD |
1107 BOOTM_STATE_FINDOTHER | BOOTM_STATE_LOADOS |
1108 BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
1109 BOOTM_STATE_OS_GO;
1110 if (IS_ENABLED(CONFIG_SYS_BOOT_RAMDISK_HIGH))
1111 states |= BOOTM_STATE_RAMDISK;
1112 if (IS_ENABLED(CONFIG_PPC) || IS_ENABLED(CONFIG_MIPS))
1113 states |= BOOTM_STATE_OS_CMDLINE;
1114 images.state |= states;
1115
1116 snprintf(addr_str, sizeof(addr_str), "%lx", addr);
1117
1118 ret = env_set("bootargs", cmdline);
1119 if (ret) {
1120 printf("Failed to set cmdline\n");
1121 return ret;
1122 }
1123 ret = do_bootm_states(&cmd, 0, 1, argv, states, &images, 1);
1124
1125 return ret;
1126}
1127
Heinrich Schuchardtf6c6df72019-01-08 18:13:06 +01001128/**
1129 * switch_to_non_secure_mode() - switch to non-secure mode
1130 *
1131 * This routine is overridden by architectures requiring this feature.
1132 */
1133void __weak switch_to_non_secure_mode(void)
1134{
1135}
1136
Simon Glassce1400f2014-06-12 07:24:53 -06001137#else /* USE_HOSTCC */
1138
Fabrice Fontaine93e07882019-05-03 22:37:05 +02001139#if defined(CONFIG_FIT_SIGNATURE)
Simon Glass8a9d0372020-03-18 11:44:02 -06001140static int bootm_host_load_image(const void *fit, int req_image_type,
1141 int cfg_noffset)
Simon Glassce1400f2014-06-12 07:24:53 -06001142{
1143 const char *fit_uname_config = NULL;
1144 ulong data, len;
Simon Glassd9d7c202022-09-06 20:26:50 -06001145 struct bootm_headers images;
Simon Glassce1400f2014-06-12 07:24:53 -06001146 int noffset;
Tom Rinic45568c2022-06-25 19:29:46 -04001147 ulong load_end, buf_size;
Simon Glassce1400f2014-06-12 07:24:53 -06001148 uint8_t image_type;
Daniel Golle0cd57f22022-08-27 04:14:42 +01001149 uint8_t image_comp;
Simon Glassce1400f2014-06-12 07:24:53 -06001150 void *load_buf;
1151 int ret;
1152
Simon Glass8a9d0372020-03-18 11:44:02 -06001153 fit_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
Simon Glassce1400f2014-06-12 07:24:53 -06001154 memset(&images, '\0', sizeof(images));
1155 images.verify = 1;
1156 noffset = fit_image_load(&images, (ulong)fit,
1157 NULL, &fit_uname_config,
1158 IH_ARCH_DEFAULT, req_image_type, -1,
1159 FIT_LOAD_IGNORED, &data, &len);
1160 if (noffset < 0)
1161 return noffset;
1162 if (fit_image_get_type(fit, noffset, &image_type)) {
1163 puts("Can't get image type!\n");
1164 return -EINVAL;
1165 }
1166
Daniel Golle88de6c52022-08-27 04:17:28 +01001167 if (fit_image_get_comp(fit, noffset, &image_comp))
1168 image_comp = IH_COMP_NONE;
Simon Glassce1400f2014-06-12 07:24:53 -06001169
1170 /* Allow the image to expand by a factor of 4, should be safe */
Tom Rinic45568c2022-06-25 19:29:46 -04001171 buf_size = (1 << 20) + len * 4;
1172 load_buf = malloc(buf_size);
Daniel Golle0cd57f22022-08-27 04:14:42 +01001173 ret = image_decomp(image_comp, 0, data, image_type, load_buf,
Tom Rinic45568c2022-06-25 19:29:46 -04001174 (void *)data, len, buf_size, &load_end);
Simon Glassce1400f2014-06-12 07:24:53 -06001175 free(load_buf);
Simon Glass081cc192014-12-02 13:17:33 -07001176
Julius Werner20908542019-07-24 19:37:54 -07001177 if (ret) {
Daniel Golle0cd57f22022-08-27 04:14:42 +01001178 ret = handle_decomp_error(image_comp, load_end - 0, buf_size, ret);
Julius Werner20908542019-07-24 19:37:54 -07001179 if (ret != BOOTM_ERR_UNIMPLEMENTED)
1180 return ret;
1181 }
Simon Glassce1400f2014-06-12 07:24:53 -06001182
1183 return 0;
1184}
1185
1186int bootm_host_load_images(const void *fit, int cfg_noffset)
1187{
1188 static uint8_t image_types[] = {
1189 IH_TYPE_KERNEL,
1190 IH_TYPE_FLATDT,
1191 IH_TYPE_RAMDISK,
1192 };
1193 int err = 0;
1194 int i;
1195
1196 for (i = 0; i < ARRAY_SIZE(image_types); i++) {
1197 int ret;
1198
Simon Glass8a9d0372020-03-18 11:44:02 -06001199 ret = bootm_host_load_image(fit, image_types[i], cfg_noffset);
Simon Glassce1400f2014-06-12 07:24:53 -06001200 if (!err && ret && ret != -ENOENT)
1201 err = ret;
1202 }
1203
1204 /* Return the first error we found */
1205 return err;
1206}
Fabrice Fontaine93e07882019-05-03 22:37:05 +02001207#endif
Simon Glassea51a622014-06-12 07:24:51 -06001208
1209#endif /* ndef USE_HOSTCC */