blob: 4144ff3b03189929927a7bf90ad4da0e2292dde8 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass3b5866d2014-06-12 07:24:46 -06002/*
3 * (C) Copyright 2000-2009
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Simon Glass3b5866d2014-06-12 07:24:46 -06005 */
6
Simon Glass0232ba72014-06-12 07:24:51 -06007#ifndef USE_HOSTCC
Simon Glass3b5866d2014-06-12 07:24:46 -06008#include <common.h>
Simon Glass0232ba72014-06-12 07:24:51 -06009#include <bootstage.h>
Simon Glass529e2082020-11-05 10:33:48 -070010#include <cli.h>
Simon Glass63334482019-11-14 12:57:39 -070011#include <cpu_func.h>
Simon Glass313112a2019-08-01 09:46:46 -060012#include <env.h>
Simon Glass0129b522014-10-19 21:11:24 -060013#include <errno.h>
Simon Glass3b5866d2014-06-12 07:24:46 -060014#include <fdt_support.h>
Simon Glass8f3f7612019-11-14 12:57:42 -070015#include <irq_func.h>
Simon Glass3b5866d2014-06-12 07:24:46 -060016#include <lmb.h>
Simon Glass0f2af882020-05-10 11:40:05 -060017#include <log.h>
Simon Glass3b5866d2014-06-12 07:24:46 -060018#include <malloc.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050019#include <mapmem.h>
Simon Glass274e0b02020-05-10 11:39:56 -060020#include <net.h>
21#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060022#include <asm/global_data.h>
Simon Glass3b5866d2014-06-12 07:24:46 -060023#include <asm/io.h>
Simon Glassc38406e2020-11-05 10:33:43 -070024#include <linux/sizes.h>
Simon Glass3b5866d2014-06-12 07:24:46 -060025#if defined(CONFIG_CMD_USB)
26#include <usb.h>
27#endif
Simon Glass0232ba72014-06-12 07:24:51 -060028#else
29#include "mkimage.h"
30#endif
Simon Glass3b5866d2014-06-12 07:24:46 -060031
Simon Glass0232ba72014-06-12 07:24:51 -060032#include <command.h>
33#include <bootm.h>
34#include <image.h>
Simon Glass3b5866d2014-06-12 07:24:46 -060035
Simon Glassc38406e2020-11-05 10:33:43 -070036#define MAX_CMDLINE_SIZE SZ_4K
37
Simon Glass3b5866d2014-06-12 07:24:46 -060038#define IH_INITRD_ARCH IH_ARCH_DEFAULT
39
Simon Glass0232ba72014-06-12 07:24:51 -060040#ifndef USE_HOSTCC
41
42DECLARE_GLOBAL_DATA_PTR;
43
Simon Glassdf00afa2022-09-06 20:26:50 -060044struct bootm_headers images; /* pointers to os/initrd/fdt images */
Tom Rini36f067f2016-08-12 08:31:15 -040045
Simon Glassed38aef2020-05-10 11:40:03 -060046static const void *boot_get_kernel(struct cmd_tbl *cmdtp, int flag, int argc,
Simon Glassdf00afa2022-09-06 20:26:50 -060047 char *const argv[], struct bootm_headers *images,
Simon Glass3b5866d2014-06-12 07:24:46 -060048 ulong *os_data, ulong *os_len);
49
Simon Glassf55305a2018-05-16 09:42:25 -060050__weak void board_quiesce_devices(void)
51{
52}
53
Simon Glass3b5866d2014-06-12 07:24:46 -060054#ifdef CONFIG_LMB
Simon Glassdf00afa2022-09-06 20:26:50 -060055static void boot_start_lmb(struct bootm_headers *images)
Simon Glass3b5866d2014-06-12 07:24:46 -060056{
57 ulong mem_start;
58 phys_size_t mem_size;
59
Simon Glassda1a1342017-08-03 12:22:15 -060060 mem_start = env_get_bootm_low();
61 mem_size = env_get_bootm_size();
Simon Glass3b5866d2014-06-12 07:24:46 -060062
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +010063 lmb_init_and_reserve_range(&images->lmb, (phys_addr_t)mem_start,
64 mem_size, NULL);
Simon Glass3b5866d2014-06-12 07:24:46 -060065}
66#else
67#define lmb_reserve(lmb, base, size)
Simon Glassdf00afa2022-09-06 20:26:50 -060068static inline void boot_start_lmb(struct bootm_headers *images) { }
Simon Glass3b5866d2014-06-12 07:24:46 -060069#endif
70
Simon Glassed38aef2020-05-10 11:40:03 -060071static int bootm_start(struct cmd_tbl *cmdtp, int flag, int argc,
72 char *const argv[])
Simon Glass3b5866d2014-06-12 07:24:46 -060073{
74 memset((void *)&images, 0, sizeof(images));
Simon Glass22c34c22017-08-03 12:22:13 -060075 images.verify = env_get_yesno("verify");
Simon Glass3b5866d2014-06-12 07:24:46 -060076
77 boot_start_lmb(&images);
78
79 bootstage_mark_name(BOOTSTAGE_ID_BOOTM_START, "bootm_start");
80 images.state = BOOTM_STATE_START;
81
82 return 0;
83}
84
Philippe Reynesae1f2ca2022-03-28 22:57:00 +020085static ulong bootm_data_addr(int argc, char *const argv[])
86{
87 ulong addr;
88
89 if (argc > 0)
90 addr = simple_strtoul(argv[0], NULL, 16);
91 else
92 addr = image_load_addr;
93
94 return addr;
95}
96
97static int bootm_pre_load(struct cmd_tbl *cmdtp, int flag, int argc,
98 char *const argv[])
99{
100 ulong data_addr = bootm_data_addr(argc, argv);
101 int ret = 0;
102
Simon Glass88b6f922023-02-05 15:36:24 -0700103 if (IS_ENABLED(CONFIG_CMD_BOOTM_PRE_LOAD))
Philippe Reynesae1f2ca2022-03-28 22:57:00 +0200104 ret = image_pre_load(data_addr);
105
106 if (ret)
107 ret = CMD_RET_FAILURE;
108
109 return ret;
110}
111
Simon Glassed38aef2020-05-10 11:40:03 -0600112static int bootm_find_os(struct cmd_tbl *cmdtp, int flag, int argc,
113 char *const argv[])
Simon Glass3b5866d2014-06-12 07:24:46 -0600114{
115 const void *os_hdr;
Safae Ouajih51c981b2023-02-06 00:50:17 +0100116#ifdef CONFIG_ANDROID_BOOT_IMAGE
117 const void *vendor_boot_img;
118 const void *boot_img;
119#endif
Simon Glass3b5866d2014-06-12 07:24:46 -0600120 bool ep_found = false;
Simon Glass0129b522014-10-19 21:11:24 -0600121 int ret;
Simon Glass3b5866d2014-06-12 07:24:46 -0600122
123 /* get kernel image header, start address and length */
124 os_hdr = boot_get_kernel(cmdtp, flag, argc, argv,
125 &images, &images.os.image_start, &images.os.image_len);
126 if (images.os.image_len == 0) {
127 puts("ERROR: can't get kernel image!\n");
128 return 1;
129 }
130
131 /* get image parameters */
132 switch (genimg_get_format(os_hdr)) {
Tom Rinic220bd92019-05-23 07:14:07 -0400133#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Simon Glass3b5866d2014-06-12 07:24:46 -0600134 case IMAGE_FORMAT_LEGACY:
135 images.os.type = image_get_type(os_hdr);
136 images.os.comp = image_get_comp(os_hdr);
137 images.os.os = image_get_os(os_hdr);
138
139 images.os.end = image_get_image_end(os_hdr);
140 images.os.load = image_get_load(os_hdr);
Simon Glass0129b522014-10-19 21:11:24 -0600141 images.os.arch = image_get_arch(os_hdr);
Simon Glass3b5866d2014-06-12 07:24:46 -0600142 break;
143#endif
Simon Glasse719d3b2021-09-25 19:43:20 -0600144#if CONFIG_IS_ENABLED(FIT)
Simon Glass3b5866d2014-06-12 07:24:46 -0600145 case IMAGE_FORMAT_FIT:
146 if (fit_image_get_type(images.fit_hdr_os,
147 images.fit_noffset_os,
148 &images.os.type)) {
149 puts("Can't get image type!\n");
150 bootstage_error(BOOTSTAGE_ID_FIT_TYPE);
151 return 1;
152 }
153
154 if (fit_image_get_comp(images.fit_hdr_os,
155 images.fit_noffset_os,
156 &images.os.comp)) {
157 puts("Can't get image compression!\n");
158 bootstage_error(BOOTSTAGE_ID_FIT_COMPRESSION);
159 return 1;
160 }
161
162 if (fit_image_get_os(images.fit_hdr_os, images.fit_noffset_os,
163 &images.os.os)) {
164 puts("Can't get image OS!\n");
165 bootstage_error(BOOTSTAGE_ID_FIT_OS);
166 return 1;
167 }
168
Simon Glass0129b522014-10-19 21:11:24 -0600169 if (fit_image_get_arch(images.fit_hdr_os,
170 images.fit_noffset_os,
171 &images.os.arch)) {
172 puts("Can't get image ARCH!\n");
173 return 1;
174 }
175
Simon Glass3b5866d2014-06-12 07:24:46 -0600176 images.os.end = fit_get_end(images.fit_hdr_os);
177
178 if (fit_image_get_load(images.fit_hdr_os, images.fit_noffset_os,
179 &images.os.load)) {
180 puts("Can't get image load address!\n");
181 bootstage_error(BOOTSTAGE_ID_FIT_LOADADDR);
182 return 1;
183 }
184 break;
185#endif
186#ifdef CONFIG_ANDROID_BOOT_IMAGE
187 case IMAGE_FORMAT_ANDROID:
Safae Ouajih51c981b2023-02-06 00:50:17 +0100188 boot_img = os_hdr;
189 vendor_boot_img = NULL;
190 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
191 boot_img = map_sysmem(get_abootimg_addr(), 0);
192 vendor_boot_img = map_sysmem(get_avendor_bootimg_addr(), 0);
193 }
Simon Glass3b5866d2014-06-12 07:24:46 -0600194 images.os.type = IH_TYPE_KERNEL;
Safae Ouajih51c981b2023-02-06 00:50:17 +0100195 images.os.comp = android_image_get_kcomp(boot_img, vendor_boot_img);
Simon Glass3b5866d2014-06-12 07:24:46 -0600196 images.os.os = IH_OS_LINUX;
Safae Ouajih51c981b2023-02-06 00:50:17 +0100197 images.os.end = android_image_get_end(boot_img, vendor_boot_img);
198 images.os.load = android_image_get_kload(boot_img, vendor_boot_img);
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300199 images.ep = images.os.load;
200 ep_found = true;
Safae Ouajih51c981b2023-02-06 00:50:17 +0100201 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
202 unmap_sysmem(vendor_boot_img);
203 unmap_sysmem(boot_img);
204 }
Simon Glass3b5866d2014-06-12 07:24:46 -0600205 break;
206#endif
207 default:
208 puts("ERROR: unknown image format type!\n");
209 return 1;
210 }
211
Simon Glass0129b522014-10-19 21:11:24 -0600212 /* If we have a valid setup.bin, we will use that for entry (x86) */
Simon Glass9d428302014-10-10 08:21:57 -0600213 if (images.os.arch == IH_ARCH_I386 ||
214 images.os.arch == IH_ARCH_X86_64) {
Simon Glass0129b522014-10-19 21:11:24 -0600215 ulong len;
216
217 ret = boot_get_setup(&images, IH_ARCH_I386, &images.ep, &len);
218 if (ret < 0 && ret != -ENOENT) {
219 puts("Could not find a valid setup.bin for x86\n");
220 return 1;
221 }
222 /* Kernel entry point is the setup.bin */
223 } else if (images.legacy_hdr_valid) {
Simon Glass3b5866d2014-06-12 07:24:46 -0600224 images.ep = image_get_ep(&images.legacy_hdr_os_copy);
Simon Glasse719d3b2021-09-25 19:43:20 -0600225#if CONFIG_IS_ENABLED(FIT)
Simon Glass3b5866d2014-06-12 07:24:46 -0600226 } else if (images.fit_uname_os) {
227 int ret;
228
229 ret = fit_image_get_entry(images.fit_hdr_os,
230 images.fit_noffset_os, &images.ep);
231 if (ret) {
232 puts("Can't get entry point property!\n");
233 return 1;
234 }
235#endif
236 } else if (!ep_found) {
237 puts("Could not find kernel entry point!\n");
238 return 1;
239 }
240
241 if (images.os.type == IH_TYPE_KERNEL_NOLOAD) {
Simon Glassdf650d22023-02-05 15:36:23 -0700242 if (IS_ENABLED(CONFIG_CMD_BOOTI) &&
Marek Vasut21425792018-06-13 06:13:33 +0200243 images.os.arch == IH_ARCH_ARM64) {
244 ulong image_addr;
245 ulong image_size;
246
247 ret = booti_setup(images.os.image_start, &image_addr,
248 &image_size, true);
249 if (ret != 0)
250 return 1;
251
252 images.os.type = IH_TYPE_KERNEL;
253 images.os.load = image_addr;
254 images.ep = image_addr;
255 } else {
256 images.os.load = images.os.image_start;
257 images.ep += images.os.image_start;
258 }
Simon Glass3b5866d2014-06-12 07:24:46 -0600259 }
260
Simon Glass5b539a02016-02-24 09:14:42 -0700261 images.os.start = map_to_sysmem(os_hdr);
Simon Glass3b5866d2014-06-12 07:24:46 -0600262
263 return 0;
264}
265
Karl Apsited686ce92015-05-21 09:52:49 -0400266/**
267 * bootm_find_images - wrapper to find and locate various images
268 * @flag: Ignored Argument
269 * @argc: command argument count
270 * @argv: command argument list
Tero Kristo06086942020-06-12 15:41:20 +0300271 * @start: OS image start address
272 * @size: OS image size
Karl Apsited686ce92015-05-21 09:52:49 -0400273 *
274 * boot_find_images() will attempt to load an available ramdisk,
275 * flattened device tree, as well as specifically marked
276 * "loadable" images (loadables are FIT only)
277 *
278 * Note: bootm_find_images will skip an image if it is not found
279 *
280 * @return:
281 * 0, if all existing images were loaded correctly
282 * 1, if an image is found but corrupted, or invalid
283 */
Tero Kristo06086942020-06-12 15:41:20 +0300284int bootm_find_images(int flag, int argc, char *const argv[], ulong start,
285 ulong size)
Simon Glass3b5866d2014-06-12 07:24:46 -0600286{
287 int ret;
288
289 /* find ramdisk */
290 ret = boot_get_ramdisk(argc, argv, &images, IH_INITRD_ARCH,
291 &images.rd_start, &images.rd_end);
292 if (ret) {
293 puts("Ramdisk image is corrupt or invalid\n");
294 return 1;
295 }
296
Tero Kristo06086942020-06-12 15:41:20 +0300297 /* check if ramdisk overlaps OS image */
298 if (images.rd_start && (((ulong)images.rd_start >= start &&
Jaehoon Chung80ffb812020-10-21 14:17:03 +0900299 (ulong)images.rd_start < start + size) ||
300 ((ulong)images.rd_end > start &&
301 (ulong)images.rd_end <= start + size) ||
302 ((ulong)images.rd_start < start &&
303 (ulong)images.rd_end >= start + size))) {
Tero Kristo06086942020-06-12 15:41:20 +0300304 printf("ERROR: RD image overlaps OS image (OS=0x%lx..0x%lx)\n",
305 start, start + size);
306 return 1;
307 }
308
Simon Glass85c057e2021-09-25 19:43:21 -0600309#if CONFIG_IS_ENABLED(OF_LIBFDT)
Simon Glass3b5866d2014-06-12 07:24:46 -0600310 /* find flattened device tree */
311 ret = boot_get_fdt(flag, argc, argv, IH_ARCH_DEFAULT, &images,
312 &images.ft_addr, &images.ft_len);
313 if (ret) {
314 puts("Could not find a valid device tree\n");
315 return 1;
316 }
Tero Kristo06086942020-06-12 15:41:20 +0300317
318 /* check if FDT overlaps OS image */
319 if (images.ft_addr &&
320 (((ulong)images.ft_addr >= start &&
Pali Rohár779a7b692022-08-27 14:48:10 +0200321 (ulong)images.ft_addr < start + size) ||
Tero Kristo06086942020-06-12 15:41:20 +0300322 ((ulong)images.ft_addr + images.ft_len >= start &&
Pali Rohár779a7b692022-08-27 14:48:10 +0200323 (ulong)images.ft_addr + images.ft_len < start + size))) {
Tero Kristo06086942020-06-12 15:41:20 +0300324 printf("ERROR: FDT image overlaps OS image (OS=0x%lx..0x%lx)\n",
325 start, start + size);
326 return 1;
327 }
328
Simon Glasse2d49d92023-02-05 15:36:30 -0700329 if (IS_ENABLED(CONFIG_CMD_FDT))
Simon Goldschmidt6127ac02018-12-17 20:14:42 +0100330 set_working_fdt_addr(map_to_sysmem(images.ft_addr));
Simon Glass3b5866d2014-06-12 07:24:46 -0600331#endif
332
Simon Glasse719d3b2021-09-25 19:43:20 -0600333#if CONFIG_IS_ENABLED(FIT)
Simon Glassbc0c4db2021-09-25 07:03:20 -0600334 if (IS_ENABLED(CONFIG_FPGA)) {
335 /* find bitstreams */
336 ret = boot_get_fpga(argc, argv, &images, IH_ARCH_DEFAULT,
337 NULL, NULL);
338 if (ret) {
339 printf("FPGA image is corrupted or invalid\n");
340 return 1;
341 }
Michal Simekebae78b2016-05-17 14:03:50 +0200342 }
Michal Simekebae78b2016-05-17 14:03:50 +0200343
Karl Apsite1b21c282015-05-21 09:52:48 -0400344 /* find all of the loadables */
345 ret = boot_get_loadable(argc, argv, &images, IH_ARCH_DEFAULT,
346 NULL, NULL);
347 if (ret) {
348 printf("Loadable(s) is corrupt or invalid\n");
349 return 1;
350 }
Karl Apsite1b21c282015-05-21 09:52:48 -0400351#endif
352
Simon Glass3b5866d2014-06-12 07:24:46 -0600353 return 0;
354}
355
Simon Glassed38aef2020-05-10 11:40:03 -0600356static int bootm_find_other(struct cmd_tbl *cmdtp, int flag, int argc,
357 char *const argv[])
Simon Glass3b5866d2014-06-12 07:24:46 -0600358{
359 if (((images.os.type == IH_TYPE_KERNEL) ||
360 (images.os.type == IH_TYPE_KERNEL_NOLOAD) ||
361 (images.os.type == IH_TYPE_MULTI)) &&
362 (images.os.os == IH_OS_LINUX ||
363 images.os.os == IH_OS_VXWORKS))
Tero Kristo06086942020-06-12 15:41:20 +0300364 return bootm_find_images(flag, argc, argv, 0, 0);
Simon Glass3b5866d2014-06-12 07:24:46 -0600365
366 return 0;
367}
Simon Glass10ae4192014-12-02 13:17:30 -0700368#endif /* USE_HOSTC */
369
Julius Werner47ef9862019-07-24 19:37:54 -0700370#if !defined(USE_HOSTCC) || defined(CONFIG_FIT_SIGNATURE)
Simon Glass4f6e6d72014-12-02 13:17:37 -0700371/**
372 * handle_decomp_error() - display a decompression error
373 *
374 * This function tries to produce a useful message. In the case where the
375 * uncompressed size is the same as the available space, we can assume that
376 * the image is too large for the buffer.
377 *
378 * @comp_type: Compression type being used (IH_COMP_...)
379 * @uncomp_size: Number of bytes uncompressed
Tom Rinif3c2f992022-06-25 19:29:46 -0400380 * @buf_size: Number of bytes the decompresion buffer was
Julius Werner47ef9862019-07-24 19:37:54 -0700381 * @ret: errno error code received from compression library
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100382 * Return: Appropriate BOOTM_ERR_ error code
Simon Glass4f6e6d72014-12-02 13:17:37 -0700383 */
Tom Rinif3c2f992022-06-25 19:29:46 -0400384static int handle_decomp_error(int comp_type, size_t uncomp_size,
385 size_t buf_size, int ret)
Simon Glass10ae4192014-12-02 13:17:30 -0700386{
Simon Glass4f6e6d72014-12-02 13:17:37 -0700387 const char *name = genimg_get_comp_name(comp_type);
388
Julius Werner47ef9862019-07-24 19:37:54 -0700389 /* ENOSYS means unimplemented compression type, don't reset. */
390 if (ret == -ENOSYS)
391 return BOOTM_ERR_UNIMPLEMENTED;
392
Tom Rinif3c2f992022-06-25 19:29:46 -0400393 if (uncomp_size >= buf_size)
Simon Glass4f6e6d72014-12-02 13:17:37 -0700394 printf("Image too large: increase CONFIG_SYS_BOOTM_LEN\n");
Simon Glass10ae4192014-12-02 13:17:30 -0700395 else
Simon Glass4f6e6d72014-12-02 13:17:37 -0700396 printf("%s: uncompress error %d\n", name, ret);
397
398 /*
399 * The decompression routines are now safe, so will not write beyond
400 * their bounds. Probably it is not necessary to reset, but maintain
401 * the current behaviour for now.
402 */
403 printf("Must RESET board to recover\n");
Simon Glass10ae4192014-12-02 13:17:30 -0700404#ifndef USE_HOSTCC
405 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
406#endif
407
408 return BOOTM_ERR_RESET;
409}
Julius Werner47ef9862019-07-24 19:37:54 -0700410#endif
Simon Glass686f3bb2014-06-12 07:24:52 -0600411
Simon Glassa51991d2014-06-12 07:24:53 -0600412#ifndef USE_HOSTCC
Simon Glassdf00afa2022-09-06 20:26:50 -0600413static int bootm_load_os(struct bootm_headers *images, int boot_progress)
Simon Glass686f3bb2014-06-12 07:24:52 -0600414{
Simon Glass74578f52022-09-06 20:26:51 -0600415 struct image_info os = images->os;
Simon Glass686f3bb2014-06-12 07:24:52 -0600416 ulong load = os.load;
Tom Rini6c96f5d2018-05-01 12:32:37 -0400417 ulong load_end;
Simon Glass686f3bb2014-06-12 07:24:52 -0600418 ulong blob_start = os.start;
419 ulong blob_end = os.end;
420 ulong image_start = os.image_start;
421 ulong image_len = os.image_len;
Bryan O'Donoghue600388e2018-04-15 11:48:17 +0100422 ulong flush_start = ALIGN_DOWN(load, ARCH_DMA_MINALIGN);
Simon Glass686f3bb2014-06-12 07:24:52 -0600423 bool no_overlap;
424 void *load_buf, *image_buf;
425 int err;
426
427 load_buf = map_sysmem(load, 0);
428 image_buf = map_sysmem(os.image_start, image_len);
Julius Werner47ef9862019-07-24 19:37:54 -0700429 err = image_decomp(os.comp, load, os.image_start, os.type,
430 load_buf, image_buf, image_len,
431 CONFIG_SYS_BOOTM_LEN, &load_end);
Simon Glass686f3bb2014-06-12 07:24:52 -0600432 if (err) {
Tom Rinif3c2f992022-06-25 19:29:46 -0400433 err = handle_decomp_error(os.comp, load_end - load,
434 CONFIG_SYS_BOOTM_LEN, err);
Simon Glass686f3bb2014-06-12 07:24:52 -0600435 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
436 return err;
437 }
Heinrich Schuchardt6d29e0d2020-08-30 11:34:12 +0200438 /* We need the decompressed image size in the next steps */
439 images->os.image_len = load_end - load;
Bryan O'Donoghue600388e2018-04-15 11:48:17 +0100440
Trent Piepho92e51d02019-03-27 23:50:09 +0000441 flush_cache(flush_start, ALIGN(load_end, ARCH_DMA_MINALIGN) - flush_start);
Simon Glass3b5866d2014-06-12 07:24:46 -0600442
Tom Rini6c96f5d2018-05-01 12:32:37 -0400443 debug(" kernel loaded at 0x%08lx, end = 0x%08lx\n", load, load_end);
Simon Glass3b5866d2014-06-12 07:24:46 -0600444 bootstage_mark(BOOTSTAGE_ID_KERNEL_LOADED);
445
Simon Glass686f3bb2014-06-12 07:24:52 -0600446 no_overlap = (os.comp == IH_COMP_NONE && load == image_start);
447
Tom Rini6c96f5d2018-05-01 12:32:37 -0400448 if (!no_overlap && load < blob_end && load_end > blob_start) {
Simon Glass3b5866d2014-06-12 07:24:46 -0600449 debug("images.os.start = 0x%lX, images.os.end = 0x%lx\n",
450 blob_start, blob_end);
451 debug("images.os.load = 0x%lx, load_end = 0x%lx\n", load,
Tom Rini6c96f5d2018-05-01 12:32:37 -0400452 load_end);
Simon Glass3b5866d2014-06-12 07:24:46 -0600453
454 /* Check what type of image this is. */
455 if (images->legacy_hdr_valid) {
456 if (image_get_type(&images->legacy_hdr_os_copy)
457 == IH_TYPE_MULTI)
458 puts("WARNING: legacy format multi component image overwritten\n");
459 return BOOTM_ERR_OVERLAP;
460 } else {
461 puts("ERROR: new format image overwritten - must RESET the board to recover\n");
462 bootstage_error(BOOTSTAGE_ID_OVERWRITTEN);
463 return BOOTM_ERR_RESET;
464 }
465 }
466
Tom Rini6c96f5d2018-05-01 12:32:37 -0400467 lmb_reserve(&images->lmb, images->os.load, (load_end -
468 images->os.load));
Simon Glass3b5866d2014-06-12 07:24:46 -0600469 return 0;
470}
471
472/**
473 * bootm_disable_interrupts() - Disable interrupts in preparation for load/boot
474 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100475 * Return: interrupt flag (0 if interrupts were disabled, non-zero if they were
Simon Glass3b5866d2014-06-12 07:24:46 -0600476 * enabled)
477 */
478ulong bootm_disable_interrupts(void)
479{
480 ulong iflag;
481
482 /*
483 * We have reached the point of no return: we are going to
484 * overwrite all exception vector code, so we cannot easily
485 * recover from any failures any more...
486 */
487 iflag = disable_interrupts();
488#ifdef CONFIG_NETCONSOLE
489 /* Stop the ethernet stack if NetConsole could have left it up */
490 eth_halt();
Simon Glass3b5866d2014-06-12 07:24:46 -0600491#endif
492
493#if defined(CONFIG_CMD_USB)
494 /*
495 * turn off USB to prevent the host controller from writing to the
496 * SDRAM while Linux is booting. This could happen (at least for OHCI
497 * controller), because the HCCA (Host Controller Communication Area)
498 * lies within the SDRAM and the host controller writes continously to
499 * this area (as busmaster!). The HccaFrameNumber is for example
500 * updated every 1 ms within the HCCA structure in SDRAM! For more
501 * details see the OpenHCI specification.
502 */
503 usb_stop();
504#endif
505 return iflag;
506}
507
Simon Glass7a8a4292020-11-05 10:33:42 -0700508#define CONSOLE_ARG "console="
Sean Anderson15bdcd42022-05-19 18:26:05 -0400509#define NULL_CONSOLE (CONSOLE_ARG "ttynull")
510#define CONSOLE_ARG_SIZE sizeof(NULL_CONSOLE)
Simon Glass3b5866d2014-06-12 07:24:46 -0600511
Simon Glassc38406e2020-11-05 10:33:43 -0700512/**
513 * fixup_silent_linux() - Handle silencing the linux boot if required
514 *
515 * This uses the silent_linux envvar to control whether to add/set a "console="
516 * parameter to the command line
517 *
518 * @buf: Buffer containing the string to process
519 * @maxlen: Maximum length of buffer
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100520 * Return: 0 if OK, -ENOSPC if @maxlen is too small
Simon Glassc38406e2020-11-05 10:33:43 -0700521 */
522static int fixup_silent_linux(char *buf, int maxlen)
Simon Glass3b5866d2014-06-12 07:24:46 -0600523{
Simon Glass3b5866d2014-06-12 07:24:46 -0600524 int want_silent;
Simon Glassc38406e2020-11-05 10:33:43 -0700525 char *cmdline;
526 int size;
Simon Glass07a88862020-11-05 10:33:38 -0700527
Simon Glass3b5866d2014-06-12 07:24:46 -0600528 /*
Simon Glassc38406e2020-11-05 10:33:43 -0700529 * Move the input string to the end of buffer. The output string will be
530 * built up at the start.
531 */
532 size = strlen(buf) + 1;
533 if (size * 2 > maxlen)
534 return -ENOSPC;
535 cmdline = buf + maxlen - size;
536 memmove(cmdline, buf, size);
537 /*
Simon Glass3b5866d2014-06-12 07:24:46 -0600538 * Only fix cmdline when requested. The environment variable can be:
539 *
540 * no - we never fixup
541 * yes - we always fixup
542 * unset - we rely on the console silent flag
543 */
Simon Glass22c34c22017-08-03 12:22:13 -0600544 want_silent = env_get_yesno("silent_linux");
Simon Glass3b5866d2014-06-12 07:24:46 -0600545 if (want_silent == 0)
Simon Glass2eab0172020-11-05 10:33:39 -0700546 return 0;
Simon Glass3b5866d2014-06-12 07:24:46 -0600547 else if (want_silent == -1 && !(gd->flags & GD_FLG_SILENT))
Simon Glass2eab0172020-11-05 10:33:39 -0700548 return 0;
Simon Glass3b5866d2014-06-12 07:24:46 -0600549
550 debug("before silent fix-up: %s\n", cmdline);
Simon Glassc38406e2020-11-05 10:33:43 -0700551 if (*cmdline) {
Simon Glass3b5866d2014-06-12 07:24:46 -0600552 char *start = strstr(cmdline, CONSOLE_ARG);
553
Simon Glassc38406e2020-11-05 10:33:43 -0700554 /* Check space for maximum possible new command line */
555 if (size + CONSOLE_ARG_SIZE > maxlen)
Simon Glass2eab0172020-11-05 10:33:39 -0700556 return -ENOSPC;
Simon Glass3b5866d2014-06-12 07:24:46 -0600557
558 if (start) {
559 char *end = strchr(start, ' ');
Simon Glass7a8a4292020-11-05 10:33:42 -0700560 int start_bytes;
Simon Glass3b5866d2014-06-12 07:24:46 -0600561
Sean Anderson15bdcd42022-05-19 18:26:05 -0400562 start_bytes = start - cmdline;
Simon Glass7a8a4292020-11-05 10:33:42 -0700563 strncpy(buf, cmdline, start_bytes);
Sean Anderson15bdcd42022-05-19 18:26:05 -0400564 strncpy(buf + start_bytes, NULL_CONSOLE, CONSOLE_ARG_SIZE);
Simon Glass3b5866d2014-06-12 07:24:46 -0600565 if (end)
Sean Anderson15bdcd42022-05-19 18:26:05 -0400566 strcpy(buf + start_bytes + CONSOLE_ARG_SIZE - 1, end);
Simon Glass3b5866d2014-06-12 07:24:46 -0600567 else
Sean Anderson15bdcd42022-05-19 18:26:05 -0400568 buf[start_bytes + CONSOLE_ARG_SIZE] = '\0';
Simon Glass3b5866d2014-06-12 07:24:46 -0600569 } else {
Sean Anderson15bdcd42022-05-19 18:26:05 -0400570 sprintf(buf, "%s %s", cmdline, NULL_CONSOLE);
Simon Glass3b5866d2014-06-12 07:24:46 -0600571 }
Simon Glassc38406e2020-11-05 10:33:43 -0700572 if (buf + strlen(buf) >= cmdline)
573 return -ENOSPC;
Simon Glass3b5866d2014-06-12 07:24:46 -0600574 } else {
Sean Anderson15bdcd42022-05-19 18:26:05 -0400575 if (maxlen < CONSOLE_ARG_SIZE)
Simon Glassc38406e2020-11-05 10:33:43 -0700576 return -ENOSPC;
Sean Anderson15bdcd42022-05-19 18:26:05 -0400577 strcpy(buf, NULL_CONSOLE);
Simon Glass3b5866d2014-06-12 07:24:46 -0600578 }
Simon Glassc38406e2020-11-05 10:33:43 -0700579 debug("after silent fix-up: %s\n", buf);
580
581 return 0;
582}
583
Simon Glass529e2082020-11-05 10:33:48 -0700584/**
585 * process_subst() - Handle substitution of ${...} fields in the environment
586 *
587 * Handle variable substitution in the provided buffer
588 *
589 * @buf: Buffer containing the string to process
590 * @maxlen: Maximum length of buffer
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100591 * Return: 0 if OK, -ENOSPC if @maxlen is too small
Simon Glass529e2082020-11-05 10:33:48 -0700592 */
593static int process_subst(char *buf, int maxlen)
594{
595 char *cmdline;
596 int size;
597 int ret;
598
599 /* Move to end of buffer */
600 size = strlen(buf) + 1;
601 cmdline = buf + maxlen - size;
602 if (buf + size > cmdline)
603 return -ENOSPC;
604 memmove(cmdline, buf, size);
605
606 ret = cli_simple_process_macros(cmdline, buf, cmdline - buf);
607
608 return ret;
609}
610
Simon Glassb4e1b6d2020-11-05 10:33:45 -0700611int bootm_process_cmdline(char *buf, int maxlen, int flags)
612{
613 int ret;
614
615 /* Check config first to enable compiler to eliminate code */
616 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) &&
617 !IS_ENABLED(CONFIG_SILENT_U_BOOT_ONLY) &&
618 (flags & BOOTM_CL_SILENT)) {
619 ret = fixup_silent_linux(buf, maxlen);
620 if (ret)
621 return log_msg_ret("silent", ret);
622 }
Simon Glassed8603d2021-03-15 18:11:23 +1300623 if (IS_ENABLED(CONFIG_BOOTARGS_SUBST) && IS_ENABLED(CONFIG_CMDLINE) &&
624 (flags & BOOTM_CL_SUBST)) {
Simon Glass529e2082020-11-05 10:33:48 -0700625 ret = process_subst(buf, maxlen);
626 if (ret)
Simon Glasscac744e2021-02-06 09:57:35 -0700627 return log_msg_ret("subst", ret);
Simon Glass529e2082020-11-05 10:33:48 -0700628 }
Simon Glassb4e1b6d2020-11-05 10:33:45 -0700629
630 return 0;
631}
632
Simon Glass63660dc2020-11-05 10:33:44 -0700633int bootm_process_cmdline_env(int flags)
Simon Glassc38406e2020-11-05 10:33:43 -0700634{
635 const int maxlen = MAX_CMDLINE_SIZE;
Simon Glass63660dc2020-11-05 10:33:44 -0700636 bool do_silent;
Simon Glassc38406e2020-11-05 10:33:43 -0700637 const char *env;
638 char *buf;
639 int ret;
Simon Glass3b5866d2014-06-12 07:24:46 -0600640
Simon Glassc38406e2020-11-05 10:33:43 -0700641 /* First check if any action is needed */
642 do_silent = IS_ENABLED(CONFIG_SILENT_CONSOLE) &&
Simon Glass63660dc2020-11-05 10:33:44 -0700643 !IS_ENABLED(CONFIG_SILENT_U_BOOT_ONLY) && (flags & BOOTM_CL_SILENT);
Simon Glass529e2082020-11-05 10:33:48 -0700644 if (!do_silent && !IS_ENABLED(CONFIG_BOOTARGS_SUBST))
Simon Glassc38406e2020-11-05 10:33:43 -0700645 return 0;
646
647 env = env_get("bootargs");
648 if (env && strlen(env) >= maxlen)
649 return -E2BIG;
650 buf = malloc(maxlen);
651 if (!buf)
652 return -ENOMEM;
653 if (env)
654 strcpy(buf, env);
655 else
656 *buf = '\0';
Simon Glassb4e1b6d2020-11-05 10:33:45 -0700657 ret = bootm_process_cmdline(buf, maxlen, flags);
Simon Glassc38406e2020-11-05 10:33:43 -0700658 if (!ret) {
659 ret = env_set("bootargs", buf);
660
661 /*
662 * If buf is "" and bootargs does not exist, this will produce
663 * an error trying to delete bootargs. Ignore it
664 */
665 if (ret == -ENOENT)
666 ret = 0;
667 }
Simon Glass3b5866d2014-06-12 07:24:46 -0600668 free(buf);
Simon Glassc38406e2020-11-05 10:33:43 -0700669 if (ret)
670 return log_msg_ret("env", ret);
Simon Glass2eab0172020-11-05 10:33:39 -0700671
672 return 0;
Simon Glass3b5866d2014-06-12 07:24:46 -0600673}
Simon Glass3b5866d2014-06-12 07:24:46 -0600674
675/**
676 * Execute selected states of the bootm command.
677 *
678 * Note the arguments to this state must be the first argument, Any 'bootm'
679 * or sub-command arguments must have already been taken.
680 *
681 * Note that if states contains more than one flag it MUST contain
682 * BOOTM_STATE_START, since this handles and consumes the command line args.
683 *
684 * Also note that aside from boot_os_fn functions and bootm_load_os no other
685 * functions we store the return value of in 'ret' may use a negative return
686 * value, without special handling.
687 *
688 * @param cmdtp Pointer to bootm command table entry
689 * @param flag Command flags (CMD_FLAG_...)
690 * @param argc Number of subcommand arguments (0 = no arguments)
691 * @param argv Arguments
692 * @param states Mask containing states to run (BOOTM_STATE_...)
693 * @param images Image header information
694 * @param boot_progress 1 to show boot progress, 0 to not do this
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100695 * Return: 0 if ok, something else on error. Some errors will cause this
Simon Glass3b5866d2014-06-12 07:24:46 -0600696 * function to perform a reboot! If states contains BOOTM_STATE_OS_GO
697 * then the intent is to boot an OS, so this function will not return
698 * unless the image type is standalone.
699 */
Simon Glassed38aef2020-05-10 11:40:03 -0600700int do_bootm_states(struct cmd_tbl *cmdtp, int flag, int argc,
Simon Glassdf00afa2022-09-06 20:26:50 -0600701 char *const argv[], int states, struct bootm_headers *images,
Simon Glassed38aef2020-05-10 11:40:03 -0600702 int boot_progress)
Simon Glass3b5866d2014-06-12 07:24:46 -0600703{
704 boot_os_fn *boot_fn;
705 ulong iflag = 0;
706 int ret = 0, need_boot_fn;
707
708 images->state |= states;
709
710 /*
711 * Work through the states and see how far we get. We stop on
712 * any error.
713 */
714 if (states & BOOTM_STATE_START)
715 ret = bootm_start(cmdtp, flag, argc, argv);
716
Philippe Reynesae1f2ca2022-03-28 22:57:00 +0200717 if (!ret && (states & BOOTM_STATE_PRE_LOAD))
718 ret = bootm_pre_load(cmdtp, flag, argc, argv);
719
Simon Glass3b5866d2014-06-12 07:24:46 -0600720 if (!ret && (states & BOOTM_STATE_FINDOS))
721 ret = bootm_find_os(cmdtp, flag, argc, argv);
722
Zubair Lutfullah Kakakhel31eb47d2016-09-09 09:18:58 +0100723 if (!ret && (states & BOOTM_STATE_FINDOTHER))
Simon Glass3b5866d2014-06-12 07:24:46 -0600724 ret = bootm_find_other(cmdtp, flag, argc, argv);
Simon Glass3b5866d2014-06-12 07:24:46 -0600725
726 /* Load the OS */
727 if (!ret && (states & BOOTM_STATE_LOADOS)) {
Simon Glass3b5866d2014-06-12 07:24:46 -0600728 iflag = bootm_disable_interrupts();
Tom Rini6c96f5d2018-05-01 12:32:37 -0400729 ret = bootm_load_os(images, 0);
730 if (ret && ret != BOOTM_ERR_OVERLAP)
Simon Glass3b5866d2014-06-12 07:24:46 -0600731 goto err;
732 else if (ret == BOOTM_ERR_OVERLAP)
733 ret = 0;
Simon Glass3b5866d2014-06-12 07:24:46 -0600734 }
735
736 /* Relocate the ramdisk */
737#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
738 if (!ret && (states & BOOTM_STATE_RAMDISK)) {
739 ulong rd_len = images->rd_end - images->rd_start;
740
741 ret = boot_ramdisk_high(&images->lmb, images->rd_start,
742 rd_len, &images->initrd_start, &images->initrd_end);
743 if (!ret) {
Simon Glass4d949a22017-08-03 12:22:10 -0600744 env_set_hex("initrd_start", images->initrd_start);
745 env_set_hex("initrd_end", images->initrd_end);
Simon Glass3b5866d2014-06-12 07:24:46 -0600746 }
747 }
748#endif
Simon Glass85c057e2021-09-25 19:43:21 -0600749#if CONFIG_IS_ENABLED(OF_LIBFDT) && defined(CONFIG_LMB)
Simon Glass3b5866d2014-06-12 07:24:46 -0600750 if (!ret && (states & BOOTM_STATE_FDT)) {
751 boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr);
752 ret = boot_relocate_fdt(&images->lmb, &images->ft_addr,
753 &images->ft_len);
754 }
755#endif
756
757 /* From now on, we need the OS boot function */
758 if (ret)
759 return ret;
760 boot_fn = bootm_os_get_boot_func(images->os.os);
761 need_boot_fn = states & (BOOTM_STATE_OS_CMDLINE |
762 BOOTM_STATE_OS_BD_T | BOOTM_STATE_OS_PREP |
763 BOOTM_STATE_OS_FAKE_GO | BOOTM_STATE_OS_GO);
764 if (boot_fn == NULL && need_boot_fn) {
765 if (iflag)
766 enable_interrupts();
767 printf("ERROR: booting os '%s' (%d) is not supported\n",
768 genimg_get_os_name(images->os.os), images->os.os);
769 bootstage_error(BOOTSTAGE_ID_CHECK_BOOT_OS);
770 return 1;
771 }
772
Hector Palacios8f9cc4c2016-07-11 12:34:37 +0200773
Simon Glass3b5866d2014-06-12 07:24:46 -0600774 /* Call various other states that are not generally used */
775 if (!ret && (states & BOOTM_STATE_OS_CMDLINE))
776 ret = boot_fn(BOOTM_STATE_OS_CMDLINE, argc, argv, images);
777 if (!ret && (states & BOOTM_STATE_OS_BD_T))
778 ret = boot_fn(BOOTM_STATE_OS_BD_T, argc, argv, images);
Hector Palacios8f9cc4c2016-07-11 12:34:37 +0200779 if (!ret && (states & BOOTM_STATE_OS_PREP)) {
Simon Glass529e2082020-11-05 10:33:48 -0700780 ret = bootm_process_cmdline_env(images->os.os == IH_OS_LINUX);
Simon Glassd87002f2020-11-05 10:33:41 -0700781 if (ret) {
782 printf("Cmdline setup failed (err=%d)\n", ret);
783 ret = CMD_RET_FAILURE;
784 goto err;
Simon Glass2eab0172020-11-05 10:33:39 -0700785 }
Simon Glass3b5866d2014-06-12 07:24:46 -0600786 ret = boot_fn(BOOTM_STATE_OS_PREP, argc, argv, images);
Hector Palacios8f9cc4c2016-07-11 12:34:37 +0200787 }
Simon Glass3b5866d2014-06-12 07:24:46 -0600788
789#ifdef CONFIG_TRACE
790 /* Pretend to run the OS, then run a user command */
791 if (!ret && (states & BOOTM_STATE_OS_FAKE_GO)) {
Simon Glass64b723f2017-08-03 12:22:12 -0600792 char *cmd_list = env_get("fakegocmd");
Simon Glass3b5866d2014-06-12 07:24:46 -0600793
794 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_FAKE_GO,
795 images, boot_fn);
796 if (!ret && cmd_list)
797 ret = run_command_list(cmd_list, -1, flag);
798 }
799#endif
800
801 /* Check for unsupported subcommand. */
802 if (ret) {
Simon Glassb90cf902022-10-11 09:47:07 -0600803 printf("subcommand failed (err=%d)\n", ret);
Simon Glass3b5866d2014-06-12 07:24:46 -0600804 return ret;
805 }
806
807 /* Now run the OS! We hope this doesn't return */
808 if (!ret && (states & BOOTM_STATE_OS_GO))
809 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_GO,
810 images, boot_fn);
811
812 /* Deal with any fallout */
813err:
814 if (iflag)
815 enable_interrupts();
816
817 if (ret == BOOTM_ERR_UNIMPLEMENTED)
818 bootstage_error(BOOTSTAGE_ID_DECOMP_UNIMPL);
819 else if (ret == BOOTM_ERR_RESET)
820 do_reset(cmdtp, flag, argc, argv);
821
822 return ret;
823}
824
Tom Rinic220bd92019-05-23 07:14:07 -0400825#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Simon Glass3b5866d2014-06-12 07:24:46 -0600826/**
827 * image_get_kernel - verify legacy format kernel image
828 * @img_addr: in RAM address of the legacy format image to be verified
829 * @verify: data CRC verification flag
830 *
831 * image_get_kernel() verifies legacy image integrity and returns pointer to
832 * legacy image header if image verification was completed successfully.
833 *
834 * returns:
835 * pointer to a legacy image header if valid image was found
836 * otherwise return NULL
837 */
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600838static struct legacy_img_hdr *image_get_kernel(ulong img_addr, int verify)
Simon Glass3b5866d2014-06-12 07:24:46 -0600839{
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600840 struct legacy_img_hdr *hdr = (struct legacy_img_hdr *)img_addr;
Simon Glass3b5866d2014-06-12 07:24:46 -0600841
842 if (!image_check_magic(hdr)) {
843 puts("Bad Magic Number\n");
844 bootstage_error(BOOTSTAGE_ID_CHECK_MAGIC);
845 return NULL;
846 }
847 bootstage_mark(BOOTSTAGE_ID_CHECK_HEADER);
848
849 if (!image_check_hcrc(hdr)) {
850 puts("Bad Header Checksum\n");
851 bootstage_error(BOOTSTAGE_ID_CHECK_HEADER);
852 return NULL;
853 }
854
855 bootstage_mark(BOOTSTAGE_ID_CHECK_CHECKSUM);
856 image_print_contents(hdr);
857
858 if (verify) {
859 puts(" Verifying Checksum ... ");
860 if (!image_check_dcrc(hdr)) {
861 printf("Bad Data CRC\n");
862 bootstage_error(BOOTSTAGE_ID_CHECK_CHECKSUM);
863 return NULL;
864 }
865 puts("OK\n");
866 }
867 bootstage_mark(BOOTSTAGE_ID_CHECK_ARCH);
868
869 if (!image_check_target_arch(hdr)) {
870 printf("Unsupported Architecture 0x%x\n", image_get_arch(hdr));
871 bootstage_error(BOOTSTAGE_ID_CHECK_ARCH);
872 return NULL;
873 }
874 return hdr;
875}
876#endif
877
878/**
879 * boot_get_kernel - find kernel image
880 * @os_data: pointer to a ulong variable, will hold os data start address
881 * @os_len: pointer to a ulong variable, will hold os data length
882 *
883 * boot_get_kernel() tries to find a kernel image, verifies its integrity
884 * and locates kernel data.
885 *
886 * returns:
887 * pointer to image header if valid image was found, plus kernel start
888 * address and length, otherwise NULL
889 */
Simon Glassed38aef2020-05-10 11:40:03 -0600890static const void *boot_get_kernel(struct cmd_tbl *cmdtp, int flag, int argc,
Simon Glassdf00afa2022-09-06 20:26:50 -0600891 char *const argv[], struct bootm_headers *images,
Simon Glass3b5866d2014-06-12 07:24:46 -0600892 ulong *os_data, ulong *os_len)
893{
Tom Rinic220bd92019-05-23 07:14:07 -0400894#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600895 struct legacy_img_hdr *hdr;
Simon Glass3b5866d2014-06-12 07:24:46 -0600896#endif
897 ulong img_addr;
898 const void *buf;
Simon Glass3b5866d2014-06-12 07:24:46 -0600899 const char *fit_uname_config = NULL;
900 const char *fit_uname_kernel = NULL;
Simon Glasse719d3b2021-09-25 19:43:20 -0600901#if CONFIG_IS_ENABLED(FIT)
Simon Glass3b5866d2014-06-12 07:24:46 -0600902 int os_noffset;
903#endif
904
Safae Ouajih51c981b2023-02-06 00:50:17 +0100905#ifdef CONFIG_ANDROID_BOOT_IMAGE
906 const void *boot_img;
907 const void *vendor_boot_img;
908#endif
Bryan Wuc82ea372014-08-15 16:51:39 -0700909 img_addr = genimg_get_kernel_addr_fit(argc < 1 ? NULL : argv[0],
910 &fit_uname_config,
911 &fit_uname_kernel);
Simon Glass3b5866d2014-06-12 07:24:46 -0600912
Simon Glass88b6f922023-02-05 15:36:24 -0700913 if (IS_ENABLED(CONFIG_CMD_BOOTM_PRE_LOAD))
Philippe Reynesae1f2ca2022-03-28 22:57:00 +0200914 img_addr += image_load_offset;
915
Simon Glass3b5866d2014-06-12 07:24:46 -0600916 bootstage_mark(BOOTSTAGE_ID_CHECK_MAGIC);
917
Simon Glass3b5866d2014-06-12 07:24:46 -0600918 /* check image type, for FIT images get FIT kernel node */
919 *os_data = *os_len = 0;
920 buf = map_sysmem(img_addr, 0);
921 switch (genimg_get_format(buf)) {
Tom Rinic220bd92019-05-23 07:14:07 -0400922#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Simon Glass3b5866d2014-06-12 07:24:46 -0600923 case IMAGE_FORMAT_LEGACY:
924 printf("## Booting kernel from Legacy Image at %08lx ...\n",
925 img_addr);
926 hdr = image_get_kernel(img_addr, images->verify);
927 if (!hdr)
928 return NULL;
929 bootstage_mark(BOOTSTAGE_ID_CHECK_IMAGETYPE);
930
931 /* get os_data and os_len */
932 switch (image_get_type(hdr)) {
933 case IH_TYPE_KERNEL:
934 case IH_TYPE_KERNEL_NOLOAD:
935 *os_data = image_get_data(hdr);
936 *os_len = image_get_data_size(hdr);
937 break;
938 case IH_TYPE_MULTI:
939 image_multi_getimg(hdr, 0, os_data, os_len);
940 break;
941 case IH_TYPE_STANDALONE:
942 *os_data = image_get_data(hdr);
943 *os_len = image_get_data_size(hdr);
944 break;
945 default:
946 printf("Wrong Image Type for %s command\n",
947 cmdtp->name);
948 bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE);
949 return NULL;
950 }
951
952 /*
953 * copy image header to allow for image overwrites during
954 * kernel decompression.
955 */
956 memmove(&images->legacy_hdr_os_copy, hdr,
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600957 sizeof(struct legacy_img_hdr));
Simon Glass3b5866d2014-06-12 07:24:46 -0600958
959 /* save pointer to image header */
960 images->legacy_hdr_os = hdr;
961
962 images->legacy_hdr_valid = 1;
963 bootstage_mark(BOOTSTAGE_ID_DECOMP_IMAGE);
964 break;
965#endif
Simon Glasse719d3b2021-09-25 19:43:20 -0600966#if CONFIG_IS_ENABLED(FIT)
Simon Glass3b5866d2014-06-12 07:24:46 -0600967 case IMAGE_FORMAT_FIT:
Simon Glassa0c0b632014-06-12 07:24:47 -0600968 os_noffset = fit_image_load(images, img_addr,
Simon Glass3b5866d2014-06-12 07:24:46 -0600969 &fit_uname_kernel, &fit_uname_config,
970 IH_ARCH_DEFAULT, IH_TYPE_KERNEL,
971 BOOTSTAGE_ID_FIT_KERNEL_START,
972 FIT_LOAD_IGNORED, os_data, os_len);
973 if (os_noffset < 0)
974 return NULL;
975
976 images->fit_hdr_os = map_sysmem(img_addr, 0);
977 images->fit_uname_os = fit_uname_kernel;
978 images->fit_uname_cfg = fit_uname_config;
979 images->fit_noffset_os = os_noffset;
980 break;
981#endif
982#ifdef CONFIG_ANDROID_BOOT_IMAGE
983 case IMAGE_FORMAT_ANDROID:
Safae Ouajih51c981b2023-02-06 00:50:17 +0100984 boot_img = buf;
985 vendor_boot_img = NULL;
986 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
987 boot_img = map_sysmem(get_abootimg_addr(), 0);
988 vendor_boot_img = map_sysmem(get_avendor_bootimg_addr(), 0);
989 }
Simon Glass3b5866d2014-06-12 07:24:46 -0600990 printf("## Booting Android Image at 0x%08lx ...\n", img_addr);
Safae Ouajih51c981b2023-02-06 00:50:17 +0100991 if (android_image_get_kernel(boot_img, vendor_boot_img, images->verify,
Simon Glass3b5866d2014-06-12 07:24:46 -0600992 os_data, os_len))
993 return NULL;
Safae Ouajih51c981b2023-02-06 00:50:17 +0100994 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
995 unmap_sysmem(vendor_boot_img);
996 unmap_sysmem(boot_img);
997 }
Simon Glass3b5866d2014-06-12 07:24:46 -0600998 break;
999#endif
1000 default:
1001 printf("Wrong Image Format for %s command\n", cmdtp->name);
1002 bootstage_error(BOOTSTAGE_ID_FIT_KERNEL_INFO);
1003 return NULL;
1004 }
1005
1006 debug(" kernel data at 0x%08lx, len = 0x%08lx (%ld)\n",
1007 *os_data, *os_len, *os_len);
1008
1009 return buf;
1010}
Heinrich Schuchardtaa0b11b2019-01-08 18:13:06 +01001011
1012/**
1013 * switch_to_non_secure_mode() - switch to non-secure mode
1014 *
1015 * This routine is overridden by architectures requiring this feature.
1016 */
1017void __weak switch_to_non_secure_mode(void)
1018{
1019}
1020
Simon Glassa51991d2014-06-12 07:24:53 -06001021#else /* USE_HOSTCC */
1022
Fabrice Fontained00d6b52019-05-03 22:37:05 +02001023#if defined(CONFIG_FIT_SIGNATURE)
Simon Glass81d13872020-03-18 11:44:02 -06001024static int bootm_host_load_image(const void *fit, int req_image_type,
1025 int cfg_noffset)
Simon Glassa51991d2014-06-12 07:24:53 -06001026{
1027 const char *fit_uname_config = NULL;
1028 ulong data, len;
Simon Glassdf00afa2022-09-06 20:26:50 -06001029 struct bootm_headers images;
Simon Glassa51991d2014-06-12 07:24:53 -06001030 int noffset;
Tom Rinif3c2f992022-06-25 19:29:46 -04001031 ulong load_end, buf_size;
Simon Glassa51991d2014-06-12 07:24:53 -06001032 uint8_t image_type;
Daniel Golle905d4b72022-08-27 04:14:42 +01001033 uint8_t image_comp;
Simon Glassa51991d2014-06-12 07:24:53 -06001034 void *load_buf;
1035 int ret;
1036
Simon Glass81d13872020-03-18 11:44:02 -06001037 fit_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
Simon Glassa51991d2014-06-12 07:24:53 -06001038 memset(&images, '\0', sizeof(images));
1039 images.verify = 1;
1040 noffset = fit_image_load(&images, (ulong)fit,
1041 NULL, &fit_uname_config,
1042 IH_ARCH_DEFAULT, req_image_type, -1,
1043 FIT_LOAD_IGNORED, &data, &len);
1044 if (noffset < 0)
1045 return noffset;
1046 if (fit_image_get_type(fit, noffset, &image_type)) {
1047 puts("Can't get image type!\n");
1048 return -EINVAL;
1049 }
1050
Daniel Golle2ebfd222022-08-27 04:17:28 +01001051 if (fit_image_get_comp(fit, noffset, &image_comp))
1052 image_comp = IH_COMP_NONE;
Simon Glassa51991d2014-06-12 07:24:53 -06001053
1054 /* Allow the image to expand by a factor of 4, should be safe */
Tom Rinif3c2f992022-06-25 19:29:46 -04001055 buf_size = (1 << 20) + len * 4;
1056 load_buf = malloc(buf_size);
Daniel Golle905d4b72022-08-27 04:14:42 +01001057 ret = image_decomp(image_comp, 0, data, image_type, load_buf,
Tom Rinif3c2f992022-06-25 19:29:46 -04001058 (void *)data, len, buf_size, &load_end);
Simon Glassa51991d2014-06-12 07:24:53 -06001059 free(load_buf);
Simon Glass5aeccc22014-12-02 13:17:33 -07001060
Julius Werner47ef9862019-07-24 19:37:54 -07001061 if (ret) {
Daniel Golle905d4b72022-08-27 04:14:42 +01001062 ret = handle_decomp_error(image_comp, load_end - 0, buf_size, ret);
Julius Werner47ef9862019-07-24 19:37:54 -07001063 if (ret != BOOTM_ERR_UNIMPLEMENTED)
1064 return ret;
1065 }
Simon Glassa51991d2014-06-12 07:24:53 -06001066
1067 return 0;
1068}
1069
1070int bootm_host_load_images(const void *fit, int cfg_noffset)
1071{
1072 static uint8_t image_types[] = {
1073 IH_TYPE_KERNEL,
1074 IH_TYPE_FLATDT,
1075 IH_TYPE_RAMDISK,
1076 };
1077 int err = 0;
1078 int i;
1079
1080 for (i = 0; i < ARRAY_SIZE(image_types); i++) {
1081 int ret;
1082
Simon Glass81d13872020-03-18 11:44:02 -06001083 ret = bootm_host_load_image(fit, image_types[i], cfg_noffset);
Simon Glassa51991d2014-06-12 07:24:53 -06001084 if (!err && ret && ret != -ENOENT)
1085 err = ret;
1086 }
1087
1088 /* Return the first error we found */
1089 return err;
1090}
Fabrice Fontained00d6b52019-05-03 22:37:05 +02001091#endif
Simon Glass0232ba72014-06-12 07:24:51 -06001092
1093#endif /* ndef USE_HOSTCC */