183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2b6396403SSimon Glass /*
3b6396403SSimon Glass * (C) Copyright 2000-2009
4b6396403SSimon Glass * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5b6396403SSimon Glass */
6b6396403SSimon Glass
7ea51a628SSimon Glass #ifndef USE_HOSTCC
8b6396403SSimon Glass #include <common.h>
9ea51a628SSimon Glass #include <bootstage.h>
10b6396403SSimon Glass #include <bzlib.h>
1190268b87SSimon Glass #include <errno.h>
12b6396403SSimon Glass #include <fdt_support.h>
13b6396403SSimon Glass #include <lmb.h>
14b6396403SSimon Glass #include <malloc.h>
150eb25b61SJoe Hershberger #include <mapmem.h>
16b6396403SSimon Glass #include <asm/io.h>
17b6396403SSimon Glass #include <linux/lzo.h>
18b6396403SSimon Glass #include <lzma/LzmaTypes.h>
19b6396403SSimon Glass #include <lzma/LzmaDec.h>
20b6396403SSimon Glass #include <lzma/LzmaTools.h>
21*06499e6bSEddie James #include <tpm-v2.h>
22b6396403SSimon Glass #if defined(CONFIG_CMD_USB)
23b6396403SSimon Glass #include <usb.h>
24b6396403SSimon Glass #endif
25ea51a628SSimon Glass #else
26ea51a628SSimon Glass #include "mkimage.h"
27ea51a628SSimon Glass #endif
28b6396403SSimon Glass
29ea51a628SSimon Glass #include <command.h>
30ea51a628SSimon Glass #include <bootm.h>
31ea51a628SSimon Glass #include <image.h>
32b6396403SSimon Glass
33b6396403SSimon Glass #ifndef CONFIG_SYS_BOOTM_LEN
34b6396403SSimon Glass /* use 8MByte as default max gunzip size */
35b6396403SSimon Glass #define CONFIG_SYS_BOOTM_LEN 0x800000
36b6396403SSimon Glass #endif
37b6396403SSimon Glass
38b6396403SSimon Glass #define IH_INITRD_ARCH IH_ARCH_DEFAULT
39b6396403SSimon Glass
40ea51a628SSimon Glass #ifndef USE_HOSTCC
41ea51a628SSimon Glass
42ea51a628SSimon Glass DECLARE_GLOBAL_DATA_PTR;
43ea51a628SSimon Glass
445db28905STom Rini bootm_headers_t images; /* pointers to os/initrd/fdt images */
455db28905STom Rini
46b6396403SSimon Glass static const void *boot_get_kernel(cmd_tbl_t *cmdtp, int flag, int argc,
47b6396403SSimon Glass char * const argv[], bootm_headers_t *images,
48b6396403SSimon Glass ulong *os_data, ulong *os_len);
49b6396403SSimon Glass
board_quiesce_devices(void)50329da485SSimon Glass __weak void board_quiesce_devices(void)
51329da485SSimon Glass {
52329da485SSimon Glass }
53329da485SSimon Glass
54b6396403SSimon Glass #ifdef CONFIG_LMB
boot_start_lmb(bootm_headers_t * images)55b6396403SSimon Glass static void boot_start_lmb(bootm_headers_t *images)
56b6396403SSimon Glass {
57b6396403SSimon Glass ulong mem_start;
58b6396403SSimon Glass phys_size_t mem_size;
59b6396403SSimon Glass
60723806ccSSimon Glass mem_start = env_get_bootm_low();
61723806ccSSimon Glass mem_size = env_get_bootm_size();
62b6396403SSimon Glass
639cc2323fSSimon Goldschmidt lmb_init_and_reserve_range(&images->lmb, (phys_addr_t)mem_start,
649cc2323fSSimon Goldschmidt mem_size, NULL);
65b6396403SSimon Glass }
66b6396403SSimon Glass #else
67b6396403SSimon Glass #define lmb_reserve(lmb, base, size)
boot_start_lmb(bootm_headers_t * images)68b6396403SSimon Glass static inline void boot_start_lmb(bootm_headers_t *images) { }
69b6396403SSimon Glass #endif
70b6396403SSimon Glass
bootm_start(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])71b6396403SSimon Glass static int bootm_start(cmd_tbl_t *cmdtp, int flag, int argc,
72b6396403SSimon Glass char * const argv[])
73b6396403SSimon Glass {
74b6396403SSimon Glass memset((void *)&images, 0, sizeof(images));
75bfebc8c9SSimon Glass images.verify = env_get_yesno("verify");
76b6396403SSimon Glass
77b6396403SSimon Glass boot_start_lmb(&images);
78b6396403SSimon Glass
79b6396403SSimon Glass bootstage_mark_name(BOOTSTAGE_ID_BOOTM_START, "bootm_start");
80b6396403SSimon Glass images.state = BOOTM_STATE_START;
81b6396403SSimon Glass
82b6396403SSimon Glass return 0;
83b6396403SSimon Glass }
84b6396403SSimon Glass
bootm_find_os(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])85b6396403SSimon Glass static int bootm_find_os(cmd_tbl_t *cmdtp, int flag, int argc,
86b6396403SSimon Glass char * const argv[])
87b6396403SSimon Glass {
88b6396403SSimon Glass const void *os_hdr;
89b6396403SSimon Glass bool ep_found = false;
9090268b87SSimon Glass int ret;
91b6396403SSimon Glass
92b6396403SSimon Glass /* get kernel image header, start address and length */
93b6396403SSimon Glass os_hdr = boot_get_kernel(cmdtp, flag, argc, argv,
94b6396403SSimon Glass &images, &images.os.image_start, &images.os.image_len);
95b6396403SSimon Glass if (images.os.image_len == 0) {
96b6396403SSimon Glass puts("ERROR: can't get kernel image!\n");
97b6396403SSimon Glass return 1;
98b6396403SSimon Glass }
99b6396403SSimon Glass
100b6396403SSimon Glass /* get image parameters */
101b6396403SSimon Glass switch (genimg_get_format(os_hdr)) {
102b6396403SSimon Glass #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
103b6396403SSimon Glass case IMAGE_FORMAT_LEGACY:
104b6396403SSimon Glass images.os.type = image_get_type(os_hdr);
105b6396403SSimon Glass images.os.comp = image_get_comp(os_hdr);
106b6396403SSimon Glass images.os.os = image_get_os(os_hdr);
107b6396403SSimon Glass
108b6396403SSimon Glass images.os.end = image_get_image_end(os_hdr);
109b6396403SSimon Glass images.os.load = image_get_load(os_hdr);
11090268b87SSimon Glass images.os.arch = image_get_arch(os_hdr);
111b6396403SSimon Glass break;
112b6396403SSimon Glass #endif
11373223f0eSSimon Glass #if IMAGE_ENABLE_FIT
114b6396403SSimon Glass case IMAGE_FORMAT_FIT:
115b6396403SSimon Glass if (fit_image_get_type(images.fit_hdr_os,
116b6396403SSimon Glass images.fit_noffset_os,
117b6396403SSimon Glass &images.os.type)) {
118b6396403SSimon Glass puts("Can't get image type!\n");
119b6396403SSimon Glass bootstage_error(BOOTSTAGE_ID_FIT_TYPE);
120b6396403SSimon Glass return 1;
121b6396403SSimon Glass }
122b6396403SSimon Glass
123b6396403SSimon Glass if (fit_image_get_comp(images.fit_hdr_os,
124b6396403SSimon Glass images.fit_noffset_os,
125b6396403SSimon Glass &images.os.comp)) {
126b6396403SSimon Glass puts("Can't get image compression!\n");
127b6396403SSimon Glass bootstage_error(BOOTSTAGE_ID_FIT_COMPRESSION);
128b6396403SSimon Glass return 1;
129b6396403SSimon Glass }
130b6396403SSimon Glass
131b6396403SSimon Glass if (fit_image_get_os(images.fit_hdr_os, images.fit_noffset_os,
132b6396403SSimon Glass &images.os.os)) {
133b6396403SSimon Glass puts("Can't get image OS!\n");
134b6396403SSimon Glass bootstage_error(BOOTSTAGE_ID_FIT_OS);
135b6396403SSimon Glass return 1;
136b6396403SSimon Glass }
137b6396403SSimon Glass
13890268b87SSimon Glass if (fit_image_get_arch(images.fit_hdr_os,
13990268b87SSimon Glass images.fit_noffset_os,
14090268b87SSimon Glass &images.os.arch)) {
14190268b87SSimon Glass puts("Can't get image ARCH!\n");
14290268b87SSimon Glass return 1;
14390268b87SSimon Glass }
14490268b87SSimon Glass
145b6396403SSimon Glass images.os.end = fit_get_end(images.fit_hdr_os);
146b6396403SSimon Glass
147b6396403SSimon Glass if (fit_image_get_load(images.fit_hdr_os, images.fit_noffset_os,
148b6396403SSimon Glass &images.os.load)) {
149b6396403SSimon Glass puts("Can't get image load address!\n");
150b6396403SSimon Glass bootstage_error(BOOTSTAGE_ID_FIT_LOADADDR);
151b6396403SSimon Glass return 1;
152b6396403SSimon Glass }
153b6396403SSimon Glass break;
154b6396403SSimon Glass #endif
155b6396403SSimon Glass #ifdef CONFIG_ANDROID_BOOT_IMAGE
156b6396403SSimon Glass case IMAGE_FORMAT_ANDROID:
157b6396403SSimon Glass images.os.type = IH_TYPE_KERNEL;
158b6396403SSimon Glass images.os.comp = IH_COMP_NONE;
159b6396403SSimon Glass images.os.os = IH_OS_LINUX;
160b6396403SSimon Glass
161b6396403SSimon Glass images.os.end = android_image_get_end(os_hdr);
162b6396403SSimon Glass images.os.load = android_image_get_kload(os_hdr);
16386f4695bSAhmad Draidi images.ep = images.os.load;
16486f4695bSAhmad Draidi ep_found = true;
165b6396403SSimon Glass break;
166b6396403SSimon Glass #endif
167b6396403SSimon Glass default:
168b6396403SSimon Glass puts("ERROR: unknown image format type!\n");
169b6396403SSimon Glass return 1;
170b6396403SSimon Glass }
171b6396403SSimon Glass
17290268b87SSimon Glass /* If we have a valid setup.bin, we will use that for entry (x86) */
1735bda35cfSSimon Glass if (images.os.arch == IH_ARCH_I386 ||
1745bda35cfSSimon Glass images.os.arch == IH_ARCH_X86_64) {
17590268b87SSimon Glass ulong len;
17690268b87SSimon Glass
17790268b87SSimon Glass ret = boot_get_setup(&images, IH_ARCH_I386, &images.ep, &len);
17890268b87SSimon Glass if (ret < 0 && ret != -ENOENT) {
17990268b87SSimon Glass puts("Could not find a valid setup.bin for x86\n");
18090268b87SSimon Glass return 1;
18190268b87SSimon Glass }
18290268b87SSimon Glass /* Kernel entry point is the setup.bin */
18390268b87SSimon Glass } else if (images.legacy_hdr_valid) {
184b6396403SSimon Glass images.ep = image_get_ep(&images.legacy_hdr_os_copy);
18573223f0eSSimon Glass #if IMAGE_ENABLE_FIT
186b6396403SSimon Glass } else if (images.fit_uname_os) {
187b6396403SSimon Glass int ret;
188b6396403SSimon Glass
189b6396403SSimon Glass ret = fit_image_get_entry(images.fit_hdr_os,
190b6396403SSimon Glass images.fit_noffset_os, &images.ep);
191b6396403SSimon Glass if (ret) {
192b6396403SSimon Glass puts("Can't get entry point property!\n");
193b6396403SSimon Glass return 1;
194b6396403SSimon Glass }
195b6396403SSimon Glass #endif
196b6396403SSimon Glass } else if (!ep_found) {
197b6396403SSimon Glass puts("Could not find kernel entry point!\n");
198b6396403SSimon Glass return 1;
199b6396403SSimon Glass }
200b6396403SSimon Glass
201b6396403SSimon Glass if (images.os.type == IH_TYPE_KERNEL_NOLOAD) {
202487b5fa6SMarek Vasut if (CONFIG_IS_ENABLED(CMD_BOOTI) &&
203487b5fa6SMarek Vasut images.os.arch == IH_ARCH_ARM64) {
204487b5fa6SMarek Vasut ulong image_addr;
205487b5fa6SMarek Vasut ulong image_size;
206487b5fa6SMarek Vasut
207487b5fa6SMarek Vasut ret = booti_setup(images.os.image_start, &image_addr,
208487b5fa6SMarek Vasut &image_size, true);
209487b5fa6SMarek Vasut if (ret != 0)
210487b5fa6SMarek Vasut return 1;
211487b5fa6SMarek Vasut
212487b5fa6SMarek Vasut images.os.type = IH_TYPE_KERNEL;
213487b5fa6SMarek Vasut images.os.load = image_addr;
214487b5fa6SMarek Vasut images.ep = image_addr;
215487b5fa6SMarek Vasut } else {
216b6396403SSimon Glass images.os.load = images.os.image_start;
217487b5fa6SMarek Vasut images.ep += images.os.image_start;
218487b5fa6SMarek Vasut }
219b6396403SSimon Glass }
220b6396403SSimon Glass
2217a80de46SSimon Glass images.os.start = map_to_sysmem(os_hdr);
222b6396403SSimon Glass
223b6396403SSimon Glass return 0;
224b6396403SSimon Glass }
225b6396403SSimon Glass
226d52e8575SKarl Apsite /**
227d52e8575SKarl Apsite * bootm_find_images - wrapper to find and locate various images
228d52e8575SKarl Apsite * @flag: Ignored Argument
229d52e8575SKarl Apsite * @argc: command argument count
230d52e8575SKarl Apsite * @argv: command argument list
231d52e8575SKarl Apsite *
232d52e8575SKarl Apsite * boot_find_images() will attempt to load an available ramdisk,
233d52e8575SKarl Apsite * flattened device tree, as well as specifically marked
234d52e8575SKarl Apsite * "loadable" images (loadables are FIT only)
235d52e8575SKarl Apsite *
236d52e8575SKarl Apsite * Note: bootm_find_images will skip an image if it is not found
237d52e8575SKarl Apsite *
238d52e8575SKarl Apsite * @return:
239d52e8575SKarl Apsite * 0, if all existing images were loaded correctly
240d52e8575SKarl Apsite * 1, if an image is found but corrupted, or invalid
241d52e8575SKarl Apsite */
bootm_find_images(int flag,int argc,char * const argv[])242d52e8575SKarl Apsite int bootm_find_images(int flag, int argc, char * const argv[])
243b6396403SSimon Glass {
244b6396403SSimon Glass int ret;
245b6396403SSimon Glass
246b6396403SSimon Glass /* find ramdisk */
247b6396403SSimon Glass ret = boot_get_ramdisk(argc, argv, &images, IH_INITRD_ARCH,
248b6396403SSimon Glass &images.rd_start, &images.rd_end);
249b6396403SSimon Glass if (ret) {
250b6396403SSimon Glass puts("Ramdisk image is corrupt or invalid\n");
251b6396403SSimon Glass return 1;
252b6396403SSimon Glass }
253b6396403SSimon Glass
254aa34fbc0SSimon Glass #if IMAGE_ENABLE_OF_LIBFDT
255b6396403SSimon Glass /* find flattened device tree */
256b6396403SSimon Glass ret = boot_get_fdt(flag, argc, argv, IH_ARCH_DEFAULT, &images,
257b6396403SSimon Glass &images.ft_addr, &images.ft_len);
258b6396403SSimon Glass if (ret) {
259b6396403SSimon Glass puts("Could not find a valid device tree\n");
260b6396403SSimon Glass return 1;
261b6396403SSimon Glass }
262596be5f3SSimon Goldschmidt if (CONFIG_IS_ENABLED(CMD_FDT))
263eaac4fb2SAlexander Graf set_working_fdt_addr(map_to_sysmem(images.ft_addr));
264b6396403SSimon Glass #endif
265b6396403SSimon Glass
26673223f0eSSimon Glass #if IMAGE_ENABLE_FIT
2678b93a92fSGoldschmidt Simon #if defined(CONFIG_FPGA)
26862afc601SMichal Simek /* find bitstreams */
26962afc601SMichal Simek ret = boot_get_fpga(argc, argv, &images, IH_ARCH_DEFAULT,
27062afc601SMichal Simek NULL, NULL);
27162afc601SMichal Simek if (ret) {
27262afc601SMichal Simek printf("FPGA image is corrupted or invalid\n");
27362afc601SMichal Simek return 1;
27462afc601SMichal Simek }
27562afc601SMichal Simek #endif
27662afc601SMichal Simek
27784a07dbfSKarl Apsite /* find all of the loadables */
27884a07dbfSKarl Apsite ret = boot_get_loadable(argc, argv, &images, IH_ARCH_DEFAULT,
27984a07dbfSKarl Apsite NULL, NULL);
28084a07dbfSKarl Apsite if (ret) {
28184a07dbfSKarl Apsite printf("Loadable(s) is corrupt or invalid\n");
28284a07dbfSKarl Apsite return 1;
28384a07dbfSKarl Apsite }
28484a07dbfSKarl Apsite #endif
28584a07dbfSKarl Apsite
286b6396403SSimon Glass return 0;
287b6396403SSimon Glass }
288b6396403SSimon Glass
bootm_find_other(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])289b6396403SSimon Glass static int bootm_find_other(cmd_tbl_t *cmdtp, int flag, int argc,
290b6396403SSimon Glass char * const argv[])
291b6396403SSimon Glass {
292b6396403SSimon Glass if (((images.os.type == IH_TYPE_KERNEL) ||
293b6396403SSimon Glass (images.os.type == IH_TYPE_KERNEL_NOLOAD) ||
294b6396403SSimon Glass (images.os.type == IH_TYPE_MULTI)) &&
295b6396403SSimon Glass (images.os.os == IH_OS_LINUX ||
296b6396403SSimon Glass images.os.os == IH_OS_VXWORKS))
297d52e8575SKarl Apsite return bootm_find_images(flag, argc, argv);
298b6396403SSimon Glass
299b6396403SSimon Glass return 0;
300b6396403SSimon Glass }
30140e5975fSSimon Glass #endif /* USE_HOSTC */
30240e5975fSSimon Glass
3038fd6a4b5SSimon Glass /**
3048fd6a4b5SSimon Glass * print_decomp_msg() - Print a suitable decompression/loading message
3058fd6a4b5SSimon Glass *
3068fd6a4b5SSimon Glass * @type: OS type (IH_OS_...)
3078fd6a4b5SSimon Glass * @comp_type: Compression type being used (IH_COMP_...)
3088fd6a4b5SSimon Glass * @is_xip: true if the load address matches the image start
3098fd6a4b5SSimon Glass */
print_decomp_msg(int comp_type,int type,bool is_xip)3108fd6a4b5SSimon Glass static void print_decomp_msg(int comp_type, int type, bool is_xip)
31140e5975fSSimon Glass {
3128fd6a4b5SSimon Glass const char *name = genimg_get_type_name(type);
3138fd6a4b5SSimon Glass
3148fd6a4b5SSimon Glass if (comp_type == IH_COMP_NONE)
3158fd6a4b5SSimon Glass printf(" %s %s ... ", is_xip ? "XIP" : "Loading", name);
3168fd6a4b5SSimon Glass else
3178fd6a4b5SSimon Glass printf(" Uncompressing %s ... ", name);
31840e5975fSSimon Glass }
31940e5975fSSimon Glass
3203086c055SSimon Glass /**
3213086c055SSimon Glass * handle_decomp_error() - display a decompression error
3223086c055SSimon Glass *
3233086c055SSimon Glass * This function tries to produce a useful message. In the case where the
3243086c055SSimon Glass * uncompressed size is the same as the available space, we can assume that
3253086c055SSimon Glass * the image is too large for the buffer.
3263086c055SSimon Glass *
3273086c055SSimon Glass * @comp_type: Compression type being used (IH_COMP_...)
3283086c055SSimon Glass * @uncomp_size: Number of bytes uncompressed
3293086c055SSimon Glass * @unc_len: Amount of space available for decompression
3303086c055SSimon Glass * @ret: Error code to report
3313086c055SSimon Glass * @return BOOTM_ERR_RESET, indicating that the board must be reset
3323086c055SSimon Glass */
handle_decomp_error(int comp_type,size_t uncomp_size,size_t unc_len,int ret)3333086c055SSimon Glass static int handle_decomp_error(int comp_type, size_t uncomp_size,
3343086c055SSimon Glass size_t unc_len, int ret)
33540e5975fSSimon Glass {
3363086c055SSimon Glass const char *name = genimg_get_comp_name(comp_type);
3373086c055SSimon Glass
3383086c055SSimon Glass if (uncomp_size >= unc_len)
3393086c055SSimon Glass printf("Image too large: increase CONFIG_SYS_BOOTM_LEN\n");
34040e5975fSSimon Glass else
3413086c055SSimon Glass printf("%s: uncompress error %d\n", name, ret);
3423086c055SSimon Glass
3433086c055SSimon Glass /*
3443086c055SSimon Glass * The decompression routines are now safe, so will not write beyond
3453086c055SSimon Glass * their bounds. Probably it is not necessary to reset, but maintain
3463086c055SSimon Glass * the current behaviour for now.
3473086c055SSimon Glass */
3483086c055SSimon Glass printf("Must RESET board to recover\n");
34940e5975fSSimon Glass #ifndef USE_HOSTCC
35040e5975fSSimon Glass bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
35140e5975fSSimon Glass #endif
35240e5975fSSimon Glass
35340e5975fSSimon Glass return BOOTM_ERR_RESET;
35440e5975fSSimon Glass }
355b6396403SSimon Glass
bootm_decomp_image(int comp,ulong load,ulong image_start,int type,void * load_buf,void * image_buf,ulong image_len,uint unc_len,ulong * load_end)356081cc197SSimon Glass int bootm_decomp_image(int comp, ulong load, ulong image_start, int type,
3572b164f1cSSimon Glass void *load_buf, void *image_buf, ulong image_len,
358081cc197SSimon Glass uint unc_len, ulong *load_end)
359b6396403SSimon Glass {
3603086c055SSimon Glass int ret = 0;
3613086c055SSimon Glass
3622b164f1cSSimon Glass *load_end = load;
3638fd6a4b5SSimon Glass print_decomp_msg(comp, type, load == image_start);
3643086c055SSimon Glass
3653086c055SSimon Glass /*
3663086c055SSimon Glass * Load the image to the right place, decompressing if needed. After
3673086c055SSimon Glass * this, image_len will be set to the number of uncompressed bytes
3683086c055SSimon Glass * loaded, ret will be non-zero on error.
3693086c055SSimon Glass */
370b6396403SSimon Glass switch (comp) {
371b6396403SSimon Glass case IH_COMP_NONE:
3723086c055SSimon Glass if (load == image_start)
3733086c055SSimon Glass break;
3743086c055SSimon Glass if (image_len <= unc_len)
375b6396403SSimon Glass memmove_wd(load_buf, image_buf, image_len, CHUNKSZ);
3763086c055SSimon Glass else
3773086c055SSimon Glass ret = 1;
378b6396403SSimon Glass break;
379b6396403SSimon Glass #ifdef CONFIG_GZIP
38040e5975fSSimon Glass case IH_COMP_GZIP: {
38140e5975fSSimon Glass ret = gunzip(load_buf, unc_len, image_buf, &image_len);
382b6396403SSimon Glass break;
38340e5975fSSimon Glass }
384b6396403SSimon Glass #endif /* CONFIG_GZIP */
385b6396403SSimon Glass #ifdef CONFIG_BZIP2
38640e5975fSSimon Glass case IH_COMP_BZIP2: {
3873086c055SSimon Glass uint size = unc_len;
38840e5975fSSimon Glass
389b6396403SSimon Glass /*
390b6396403SSimon Glass * If we've got less than 4 MB of malloc() space,
391b6396403SSimon Glass * use slower decompression algorithm which requires
392b6396403SSimon Glass * at most 2300 KB of memory.
393b6396403SSimon Glass */
3943086c055SSimon Glass ret = BZ2_bzBuffToBuffDecompress(load_buf, &size,
395b6396403SSimon Glass image_buf, image_len,
396b6396403SSimon Glass CONFIG_SYS_MALLOC_LEN < (4096 * 1024), 0);
3973086c055SSimon Glass image_len = size;
398b6396403SSimon Glass break;
39940e5975fSSimon Glass }
400b6396403SSimon Glass #endif /* CONFIG_BZIP2 */
401b6396403SSimon Glass #ifdef CONFIG_LZMA
402b6396403SSimon Glass case IH_COMP_LZMA: {
403b6396403SSimon Glass SizeT lzma_len = unc_len;
4042b164f1cSSimon Glass
405b6396403SSimon Glass ret = lzmaBuffToBuffDecompress(load_buf, &lzma_len,
406b6396403SSimon Glass image_buf, image_len);
4073086c055SSimon Glass image_len = lzma_len;
408b6396403SSimon Glass break;
409b6396403SSimon Glass }
410b6396403SSimon Glass #endif /* CONFIG_LZMA */
411b6396403SSimon Glass #ifdef CONFIG_LZO
412b6396403SSimon Glass case IH_COMP_LZO: {
413b6396403SSimon Glass size_t size = unc_len;
414b6396403SSimon Glass
415b6396403SSimon Glass ret = lzop_decompress(image_buf, image_len, load_buf, &size);
4163086c055SSimon Glass image_len = size;
417b6396403SSimon Glass break;
418b6396403SSimon Glass }
419b6396403SSimon Glass #endif /* CONFIG_LZO */
420027b728dSJulius Werner #ifdef CONFIG_LZ4
421027b728dSJulius Werner case IH_COMP_LZ4: {
422027b728dSJulius Werner size_t size = unc_len;
423027b728dSJulius Werner
424027b728dSJulius Werner ret = ulz4fn(image_buf, image_len, load_buf, &size);
425027b728dSJulius Werner image_len = size;
426027b728dSJulius Werner break;
427027b728dSJulius Werner }
428027b728dSJulius Werner #endif /* CONFIG_LZ4 */
429b6396403SSimon Glass default:
430b6396403SSimon Glass printf("Unimplemented compression type %d\n", comp);
431b6396403SSimon Glass return BOOTM_ERR_UNIMPLEMENTED;
432b6396403SSimon Glass }
433b6396403SSimon Glass
4343086c055SSimon Glass if (ret)
4353086c055SSimon Glass return handle_decomp_error(comp, image_len, unc_len, ret);
4363086c055SSimon Glass *load_end = load + image_len;
4373086c055SSimon Glass
4382b164f1cSSimon Glass puts("OK\n");
4392b164f1cSSimon Glass
4402b164f1cSSimon Glass return 0;
4412b164f1cSSimon Glass }
4422b164f1cSSimon Glass
443ce1400f6SSimon Glass #ifndef USE_HOSTCC
bootm_load_os(bootm_headers_t * images,int boot_progress)444cc955358STom Rini static int bootm_load_os(bootm_headers_t *images, int boot_progress)
4452b164f1cSSimon Glass {
4462b164f1cSSimon Glass image_info_t os = images->os;
4472b164f1cSSimon Glass ulong load = os.load;
448cc955358STom Rini ulong load_end;
4492b164f1cSSimon Glass ulong blob_start = os.start;
4502b164f1cSSimon Glass ulong blob_end = os.end;
4512b164f1cSSimon Glass ulong image_start = os.image_start;
4522b164f1cSSimon Glass ulong image_len = os.image_len;
453bbac9222SBryan O'Donoghue ulong flush_start = ALIGN_DOWN(load, ARCH_DMA_MINALIGN);
454cc955358STom Rini ulong flush_len;
4552b164f1cSSimon Glass bool no_overlap;
4562b164f1cSSimon Glass void *load_buf, *image_buf;
4572b164f1cSSimon Glass int err;
4582b164f1cSSimon Glass
4592b164f1cSSimon Glass load_buf = map_sysmem(load, 0);
4602b164f1cSSimon Glass image_buf = map_sysmem(os.image_start, image_len);
461081cc197SSimon Glass err = bootm_decomp_image(os.comp, load, os.image_start, os.type,
462081cc197SSimon Glass load_buf, image_buf, image_len,
463cc955358STom Rini CONFIG_SYS_BOOTM_LEN, &load_end);
4642b164f1cSSimon Glass if (err) {
4652b164f1cSSimon Glass bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
4662b164f1cSSimon Glass return err;
4672b164f1cSSimon Glass }
468bbac9222SBryan O'Donoghue
469cc955358STom Rini flush_len = load_end - load;
470bbac9222SBryan O'Donoghue if (flush_start < load)
471bbac9222SBryan O'Donoghue flush_len += load - flush_start;
472bbac9222SBryan O'Donoghue
473bbac9222SBryan O'Donoghue flush_cache(flush_start, ALIGN(flush_len, ARCH_DMA_MINALIGN));
474b6396403SSimon Glass
475cc955358STom Rini debug(" kernel loaded at 0x%08lx, end = 0x%08lx\n", load, load_end);
476b6396403SSimon Glass bootstage_mark(BOOTSTAGE_ID_KERNEL_LOADED);
477b6396403SSimon Glass
4782b164f1cSSimon Glass no_overlap = (os.comp == IH_COMP_NONE && load == image_start);
4792b164f1cSSimon Glass
480cc955358STom Rini if (!no_overlap && load < blob_end && load_end > blob_start) {
481b6396403SSimon Glass debug("images.os.start = 0x%lX, images.os.end = 0x%lx\n",
482b6396403SSimon Glass blob_start, blob_end);
483b6396403SSimon Glass debug("images.os.load = 0x%lx, load_end = 0x%lx\n", load,
484cc955358STom Rini load_end);
485b6396403SSimon Glass
486b6396403SSimon Glass /* Check what type of image this is. */
487b6396403SSimon Glass if (images->legacy_hdr_valid) {
488b6396403SSimon Glass if (image_get_type(&images->legacy_hdr_os_copy)
489b6396403SSimon Glass == IH_TYPE_MULTI)
490b6396403SSimon Glass puts("WARNING: legacy format multi component image overwritten\n");
491b6396403SSimon Glass return BOOTM_ERR_OVERLAP;
492b6396403SSimon Glass } else {
493b6396403SSimon Glass puts("ERROR: new format image overwritten - must RESET the board to recover\n");
494b6396403SSimon Glass bootstage_error(BOOTSTAGE_ID_OVERWRITTEN);
495b6396403SSimon Glass return BOOTM_ERR_RESET;
496b6396403SSimon Glass }
497b6396403SSimon Glass }
498b6396403SSimon Glass
499cc955358STom Rini lmb_reserve(&images->lmb, images->os.load, (load_end -
500cc955358STom Rini images->os.load));
501b6396403SSimon Glass return 0;
502b6396403SSimon Glass }
503b6396403SSimon Glass
504b6396403SSimon Glass /**
505b6396403SSimon Glass * bootm_disable_interrupts() - Disable interrupts in preparation for load/boot
506b6396403SSimon Glass *
507b6396403SSimon Glass * @return interrupt flag (0 if interrupts were disabled, non-zero if they were
508b6396403SSimon Glass * enabled)
509b6396403SSimon Glass */
bootm_disable_interrupts(void)510b6396403SSimon Glass ulong bootm_disable_interrupts(void)
511b6396403SSimon Glass {
512b6396403SSimon Glass ulong iflag;
513b6396403SSimon Glass
514b6396403SSimon Glass /*
515b6396403SSimon Glass * We have reached the point of no return: we are going to
516b6396403SSimon Glass * overwrite all exception vector code, so we cannot easily
517b6396403SSimon Glass * recover from any failures any more...
518b6396403SSimon Glass */
519b6396403SSimon Glass iflag = disable_interrupts();
520b6396403SSimon Glass #ifdef CONFIG_NETCONSOLE
521b6396403SSimon Glass /* Stop the ethernet stack if NetConsole could have left it up */
522b6396403SSimon Glass eth_halt();
5234917c061SBernhard Nortmann # ifndef CONFIG_DM_ETH
524b6396403SSimon Glass eth_unregister(eth_get_dev());
525b6396403SSimon Glass # endif
5264917c061SBernhard Nortmann #endif
527b6396403SSimon Glass
528b6396403SSimon Glass #if defined(CONFIG_CMD_USB)
529b6396403SSimon Glass /*
530b6396403SSimon Glass * turn off USB to prevent the host controller from writing to the
531b6396403SSimon Glass * SDRAM while Linux is booting. This could happen (at least for OHCI
532b6396403SSimon Glass * controller), because the HCCA (Host Controller Communication Area)
533b6396403SSimon Glass * lies within the SDRAM and the host controller writes continously to
534b6396403SSimon Glass * this area (as busmaster!). The HccaFrameNumber is for example
535b6396403SSimon Glass * updated every 1 ms within the HCCA structure in SDRAM! For more
536b6396403SSimon Glass * details see the OpenHCI specification.
537b6396403SSimon Glass */
538b6396403SSimon Glass usb_stop();
539b6396403SSimon Glass #endif
540b6396403SSimon Glass return iflag;
541b6396403SSimon Glass }
542b6396403SSimon Glass
543b6396403SSimon Glass #if defined(CONFIG_SILENT_CONSOLE) && !defined(CONFIG_SILENT_U_BOOT_ONLY)
544b6396403SSimon Glass
545b6396403SSimon Glass #define CONSOLE_ARG "console="
546b6396403SSimon Glass #define CONSOLE_ARG_LEN (sizeof(CONSOLE_ARG) - 1)
547b6396403SSimon Glass
fixup_silent_linux(void)548b6396403SSimon Glass static void fixup_silent_linux(void)
549b6396403SSimon Glass {
550b6396403SSimon Glass char *buf;
551b6396403SSimon Glass const char *env_val;
55200caae6dSSimon Glass char *cmdline = env_get("bootargs");
553b6396403SSimon Glass int want_silent;
554b6396403SSimon Glass
555b6396403SSimon Glass /*
556b6396403SSimon Glass * Only fix cmdline when requested. The environment variable can be:
557b6396403SSimon Glass *
558b6396403SSimon Glass * no - we never fixup
559b6396403SSimon Glass * yes - we always fixup
560b6396403SSimon Glass * unset - we rely on the console silent flag
561b6396403SSimon Glass */
562bfebc8c9SSimon Glass want_silent = env_get_yesno("silent_linux");
563b6396403SSimon Glass if (want_silent == 0)
564b6396403SSimon Glass return;
565b6396403SSimon Glass else if (want_silent == -1 && !(gd->flags & GD_FLG_SILENT))
566b6396403SSimon Glass return;
567b6396403SSimon Glass
568b6396403SSimon Glass debug("before silent fix-up: %s\n", cmdline);
569b6396403SSimon Glass if (cmdline && (cmdline[0] != '\0')) {
570b6396403SSimon Glass char *start = strstr(cmdline, CONSOLE_ARG);
571b6396403SSimon Glass
572b6396403SSimon Glass /* Allocate space for maximum possible new command line */
573b6396403SSimon Glass buf = malloc(strlen(cmdline) + 1 + CONSOLE_ARG_LEN + 1);
574b6396403SSimon Glass if (!buf) {
575b6396403SSimon Glass debug("%s: out of memory\n", __func__);
576b6396403SSimon Glass return;
577b6396403SSimon Glass }
578b6396403SSimon Glass
579b6396403SSimon Glass if (start) {
580b6396403SSimon Glass char *end = strchr(start, ' ');
581b6396403SSimon Glass int num_start_bytes = start - cmdline + CONSOLE_ARG_LEN;
582b6396403SSimon Glass
583b6396403SSimon Glass strncpy(buf, cmdline, num_start_bytes);
584b6396403SSimon Glass if (end)
585b6396403SSimon Glass strcpy(buf + num_start_bytes, end);
586b6396403SSimon Glass else
587b6396403SSimon Glass buf[num_start_bytes] = '\0';
588b6396403SSimon Glass } else {
589b6396403SSimon Glass sprintf(buf, "%s %s", cmdline, CONSOLE_ARG);
590b6396403SSimon Glass }
591b6396403SSimon Glass env_val = buf;
592b6396403SSimon Glass } else {
593b6396403SSimon Glass buf = NULL;
594b6396403SSimon Glass env_val = CONSOLE_ARG;
595b6396403SSimon Glass }
596b6396403SSimon Glass
597382bee57SSimon Glass env_set("bootargs", env_val);
598b6396403SSimon Glass debug("after silent fix-up: %s\n", env_val);
599b6396403SSimon Glass free(buf);
600b6396403SSimon Glass }
601b6396403SSimon Glass #endif /* CONFIG_SILENT_CONSOLE */
602b6396403SSimon Glass
bootm_measure(struct bootm_headers * images)603*06499e6bSEddie James int bootm_measure(struct bootm_headers *images)
604*06499e6bSEddie James {
605*06499e6bSEddie James int ret = 0;
606*06499e6bSEddie James
607*06499e6bSEddie James if (IS_ENABLED(CONFIG_MEASURED_BOOT)) {
608*06499e6bSEddie James struct tcg2_event_log elog;
609*06499e6bSEddie James struct udevice *dev;
610*06499e6bSEddie James void *initrd_buf;
611*06499e6bSEddie James void *image_buf;
612*06499e6bSEddie James const char *s;
613*06499e6bSEddie James u32 rd_len;
614*06499e6bSEddie James bool ign;
615*06499e6bSEddie James
616*06499e6bSEddie James elog.log_size = 0;
617*06499e6bSEddie James ign = IS_ENABLED(CONFIG_MEASURE_IGNORE_LOG);
618*06499e6bSEddie James ret = tcg2_measurement_init(&dev, &elog, ign);
619*06499e6bSEddie James if (ret)
620*06499e6bSEddie James return ret;
621*06499e6bSEddie James
622*06499e6bSEddie James image_buf = map_sysmem(images->os.image_start,
623*06499e6bSEddie James images->os.image_len);
624*06499e6bSEddie James ret = tcg2_measure_data(dev, &elog, 8, images->os.image_len,
625*06499e6bSEddie James image_buf, EV_COMPACT_HASH,
626*06499e6bSEddie James strlen("linux") + 1, (u8 *)"linux");
627*06499e6bSEddie James if (ret)
628*06499e6bSEddie James goto unmap_image;
629*06499e6bSEddie James
630*06499e6bSEddie James rd_len = images->rd_end - images->rd_start;
631*06499e6bSEddie James initrd_buf = map_sysmem(images->rd_start, rd_len);
632*06499e6bSEddie James ret = tcg2_measure_data(dev, &elog, 9, rd_len, initrd_buf,
633*06499e6bSEddie James EV_COMPACT_HASH, strlen("initrd") + 1,
634*06499e6bSEddie James (u8 *)"initrd");
635*06499e6bSEddie James if (ret)
636*06499e6bSEddie James goto unmap_initrd;
637*06499e6bSEddie James
638*06499e6bSEddie James if (IS_ENABLED(CONFIG_MEASURE_DEVICETREE)) {
639*06499e6bSEddie James ret = tcg2_measure_data(dev, &elog, 0, images->ft_len,
640*06499e6bSEddie James (u8 *)images->ft_addr,
641*06499e6bSEddie James EV_TABLE_OF_DEVICES,
642*06499e6bSEddie James strlen("dts") + 1,
643*06499e6bSEddie James (u8 *)"dts");
644*06499e6bSEddie James if (ret)
645*06499e6bSEddie James goto unmap_initrd;
646*06499e6bSEddie James }
647*06499e6bSEddie James
648*06499e6bSEddie James s = env_get("bootargs");
649*06499e6bSEddie James if (!s)
650*06499e6bSEddie James s = "";
651*06499e6bSEddie James ret = tcg2_measure_data(dev, &elog, 1, strlen(s) + 1, (u8 *)s,
652*06499e6bSEddie James EV_PLATFORM_CONFIG_FLAGS,
653*06499e6bSEddie James strlen(s) + 1, (u8 *)s);
654*06499e6bSEddie James
655*06499e6bSEddie James unmap_initrd:
656*06499e6bSEddie James unmap_sysmem(initrd_buf);
657*06499e6bSEddie James
658*06499e6bSEddie James unmap_image:
659*06499e6bSEddie James unmap_sysmem(image_buf);
660*06499e6bSEddie James tcg2_measurement_term(dev, &elog, ret != 0);
661*06499e6bSEddie James }
662*06499e6bSEddie James
663*06499e6bSEddie James return ret;
664*06499e6bSEddie James }
665*06499e6bSEddie James
666b6396403SSimon Glass /**
667b6396403SSimon Glass * Execute selected states of the bootm command.
668b6396403SSimon Glass *
669b6396403SSimon Glass * Note the arguments to this state must be the first argument, Any 'bootm'
670b6396403SSimon Glass * or sub-command arguments must have already been taken.
671b6396403SSimon Glass *
672b6396403SSimon Glass * Note that if states contains more than one flag it MUST contain
673b6396403SSimon Glass * BOOTM_STATE_START, since this handles and consumes the command line args.
674b6396403SSimon Glass *
675b6396403SSimon Glass * Also note that aside from boot_os_fn functions and bootm_load_os no other
676b6396403SSimon Glass * functions we store the return value of in 'ret' may use a negative return
677b6396403SSimon Glass * value, without special handling.
678b6396403SSimon Glass *
679b6396403SSimon Glass * @param cmdtp Pointer to bootm command table entry
680b6396403SSimon Glass * @param flag Command flags (CMD_FLAG_...)
681b6396403SSimon Glass * @param argc Number of subcommand arguments (0 = no arguments)
682b6396403SSimon Glass * @param argv Arguments
683b6396403SSimon Glass * @param states Mask containing states to run (BOOTM_STATE_...)
684b6396403SSimon Glass * @param images Image header information
685b6396403SSimon Glass * @param boot_progress 1 to show boot progress, 0 to not do this
686b6396403SSimon Glass * @return 0 if ok, something else on error. Some errors will cause this
687b6396403SSimon Glass * function to perform a reboot! If states contains BOOTM_STATE_OS_GO
688b6396403SSimon Glass * then the intent is to boot an OS, so this function will not return
689b6396403SSimon Glass * unless the image type is standalone.
690b6396403SSimon Glass */
do_bootm_states(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[],int states,bootm_headers_t * images,int boot_progress)691b6396403SSimon Glass int do_bootm_states(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
692b6396403SSimon Glass int states, bootm_headers_t *images, int boot_progress)
693b6396403SSimon Glass {
694b6396403SSimon Glass boot_os_fn *boot_fn;
695b6396403SSimon Glass ulong iflag = 0;
696b6396403SSimon Glass int ret = 0, need_boot_fn;
697b6396403SSimon Glass
698b6396403SSimon Glass images->state |= states;
699b6396403SSimon Glass
700b6396403SSimon Glass /*
701b6396403SSimon Glass * Work through the states and see how far we get. We stop on
702b6396403SSimon Glass * any error.
703b6396403SSimon Glass */
704b6396403SSimon Glass if (states & BOOTM_STATE_START)
705b6396403SSimon Glass ret = bootm_start(cmdtp, flag, argc, argv);
706b6396403SSimon Glass
707b6396403SSimon Glass if (!ret && (states & BOOTM_STATE_FINDOS))
708b6396403SSimon Glass ret = bootm_find_os(cmdtp, flag, argc, argv);
709b6396403SSimon Glass
710ba079840SZubair Lutfullah Kakakhel if (!ret && (states & BOOTM_STATE_FINDOTHER))
711b6396403SSimon Glass ret = bootm_find_other(cmdtp, flag, argc, argv);
712b6396403SSimon Glass
713*06499e6bSEddie James if (IS_ENABLED(CONFIG_MEASURED_BOOT) && !ret &&
714*06499e6bSEddie James (states & BOOTM_STATE_MEASURE))
715*06499e6bSEddie James bootm_measure(images);
716*06499e6bSEddie James
717b6396403SSimon Glass /* Load the OS */
718b6396403SSimon Glass if (!ret && (states & BOOTM_STATE_LOADOS)) {
719b6396403SSimon Glass iflag = bootm_disable_interrupts();
720cc955358STom Rini ret = bootm_load_os(images, 0);
721cc955358STom Rini if (ret && ret != BOOTM_ERR_OVERLAP)
722b6396403SSimon Glass goto err;
723b6396403SSimon Glass else if (ret == BOOTM_ERR_OVERLAP)
724b6396403SSimon Glass ret = 0;
725b6396403SSimon Glass }
726b6396403SSimon Glass
727b6396403SSimon Glass /* Relocate the ramdisk */
728b6396403SSimon Glass #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
729b6396403SSimon Glass if (!ret && (states & BOOTM_STATE_RAMDISK)) {
730b6396403SSimon Glass ulong rd_len = images->rd_end - images->rd_start;
731b6396403SSimon Glass
732b6396403SSimon Glass ret = boot_ramdisk_high(&images->lmb, images->rd_start,
733b6396403SSimon Glass rd_len, &images->initrd_start, &images->initrd_end);
734b6396403SSimon Glass if (!ret) {
735018f5303SSimon Glass env_set_hex("initrd_start", images->initrd_start);
736018f5303SSimon Glass env_set_hex("initrd_end", images->initrd_end);
737b6396403SSimon Glass }
738b6396403SSimon Glass }
739b6396403SSimon Glass #endif
740aa34fbc0SSimon Glass #if IMAGE_ENABLE_OF_LIBFDT && defined(CONFIG_LMB)
741b6396403SSimon Glass if (!ret && (states & BOOTM_STATE_FDT)) {
742b6396403SSimon Glass boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr);
743b6396403SSimon Glass ret = boot_relocate_fdt(&images->lmb, &images->ft_addr,
744b6396403SSimon Glass &images->ft_len);
745b6396403SSimon Glass }
746b6396403SSimon Glass #endif
747b6396403SSimon Glass
748b6396403SSimon Glass /* From now on, we need the OS boot function */
749b6396403SSimon Glass if (ret)
750b6396403SSimon Glass return ret;
751b6396403SSimon Glass boot_fn = bootm_os_get_boot_func(images->os.os);
752b6396403SSimon Glass need_boot_fn = states & (BOOTM_STATE_OS_CMDLINE |
753b6396403SSimon Glass BOOTM_STATE_OS_BD_T | BOOTM_STATE_OS_PREP |
754b6396403SSimon Glass BOOTM_STATE_OS_FAKE_GO | BOOTM_STATE_OS_GO);
755b6396403SSimon Glass if (boot_fn == NULL && need_boot_fn) {
756b6396403SSimon Glass if (iflag)
757b6396403SSimon Glass enable_interrupts();
758b6396403SSimon Glass printf("ERROR: booting os '%s' (%d) is not supported\n",
759b6396403SSimon Glass genimg_get_os_name(images->os.os), images->os.os);
760b6396403SSimon Glass bootstage_error(BOOTSTAGE_ID_CHECK_BOOT_OS);
761b6396403SSimon Glass return 1;
762b6396403SSimon Glass }
763b6396403SSimon Glass
76419e8649eSHector Palacios
765b6396403SSimon Glass /* Call various other states that are not generally used */
766b6396403SSimon Glass if (!ret && (states & BOOTM_STATE_OS_CMDLINE))
767b6396403SSimon Glass ret = boot_fn(BOOTM_STATE_OS_CMDLINE, argc, argv, images);
768b6396403SSimon Glass if (!ret && (states & BOOTM_STATE_OS_BD_T))
769b6396403SSimon Glass ret = boot_fn(BOOTM_STATE_OS_BD_T, argc, argv, images);
77019e8649eSHector Palacios if (!ret && (states & BOOTM_STATE_OS_PREP)) {
77119e8649eSHector Palacios #if defined(CONFIG_SILENT_CONSOLE) && !defined(CONFIG_SILENT_U_BOOT_ONLY)
77219e8649eSHector Palacios if (images->os.os == IH_OS_LINUX)
77319e8649eSHector Palacios fixup_silent_linux();
77419e8649eSHector Palacios #endif
775b6396403SSimon Glass ret = boot_fn(BOOTM_STATE_OS_PREP, argc, argv, images);
77619e8649eSHector Palacios }
777b6396403SSimon Glass
778b6396403SSimon Glass #ifdef CONFIG_TRACE
779b6396403SSimon Glass /* Pretend to run the OS, then run a user command */
780b6396403SSimon Glass if (!ret && (states & BOOTM_STATE_OS_FAKE_GO)) {
78100caae6dSSimon Glass char *cmd_list = env_get("fakegocmd");
782b6396403SSimon Glass
783b6396403SSimon Glass ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_FAKE_GO,
784b6396403SSimon Glass images, boot_fn);
785b6396403SSimon Glass if (!ret && cmd_list)
786b6396403SSimon Glass ret = run_command_list(cmd_list, -1, flag);
787b6396403SSimon Glass }
788b6396403SSimon Glass #endif
789b6396403SSimon Glass
790b6396403SSimon Glass /* Check for unsupported subcommand. */
791b6396403SSimon Glass if (ret) {
792b6396403SSimon Glass puts("subcommand not supported\n");
793b6396403SSimon Glass return ret;
794b6396403SSimon Glass }
795b6396403SSimon Glass
796b6396403SSimon Glass /* Now run the OS! We hope this doesn't return */
797b6396403SSimon Glass if (!ret && (states & BOOTM_STATE_OS_GO))
798b6396403SSimon Glass ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_GO,
799b6396403SSimon Glass images, boot_fn);
800b6396403SSimon Glass
801b6396403SSimon Glass /* Deal with any fallout */
802b6396403SSimon Glass err:
803b6396403SSimon Glass if (iflag)
804b6396403SSimon Glass enable_interrupts();
805b6396403SSimon Glass
806b6396403SSimon Glass if (ret == BOOTM_ERR_UNIMPLEMENTED)
807b6396403SSimon Glass bootstage_error(BOOTSTAGE_ID_DECOMP_UNIMPL);
808b6396403SSimon Glass else if (ret == BOOTM_ERR_RESET)
809b6396403SSimon Glass do_reset(cmdtp, flag, argc, argv);
810b6396403SSimon Glass
811b6396403SSimon Glass return ret;
812b6396403SSimon Glass }
813b6396403SSimon Glass
814b6396403SSimon Glass #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
815b6396403SSimon Glass /**
816b6396403SSimon Glass * image_get_kernel - verify legacy format kernel image
817b6396403SSimon Glass * @img_addr: in RAM address of the legacy format image to be verified
818b6396403SSimon Glass * @verify: data CRC verification flag
819b6396403SSimon Glass *
820b6396403SSimon Glass * image_get_kernel() verifies legacy image integrity and returns pointer to
821b6396403SSimon Glass * legacy image header if image verification was completed successfully.
822b6396403SSimon Glass *
823b6396403SSimon Glass * returns:
824b6396403SSimon Glass * pointer to a legacy image header if valid image was found
825b6396403SSimon Glass * otherwise return NULL
826b6396403SSimon Glass */
image_get_kernel(ulong img_addr,int verify)827b6396403SSimon Glass static image_header_t *image_get_kernel(ulong img_addr, int verify)
828b6396403SSimon Glass {
829b6396403SSimon Glass image_header_t *hdr = (image_header_t *)img_addr;
830b6396403SSimon Glass
831b6396403SSimon Glass if (!image_check_magic(hdr)) {
832b6396403SSimon Glass puts("Bad Magic Number\n");
833b6396403SSimon Glass bootstage_error(BOOTSTAGE_ID_CHECK_MAGIC);
834b6396403SSimon Glass return NULL;
835b6396403SSimon Glass }
836b6396403SSimon Glass bootstage_mark(BOOTSTAGE_ID_CHECK_HEADER);
837b6396403SSimon Glass
838b6396403SSimon Glass if (!image_check_hcrc(hdr)) {
839b6396403SSimon Glass puts("Bad Header Checksum\n");
840b6396403SSimon Glass bootstage_error(BOOTSTAGE_ID_CHECK_HEADER);
841b6396403SSimon Glass return NULL;
842b6396403SSimon Glass }
843b6396403SSimon Glass
844b6396403SSimon Glass bootstage_mark(BOOTSTAGE_ID_CHECK_CHECKSUM);
845b6396403SSimon Glass image_print_contents(hdr);
846b6396403SSimon Glass
847b6396403SSimon Glass if (verify) {
848b6396403SSimon Glass puts(" Verifying Checksum ... ");
849b6396403SSimon Glass if (!image_check_dcrc(hdr)) {
850b6396403SSimon Glass printf("Bad Data CRC\n");
851b6396403SSimon Glass bootstage_error(BOOTSTAGE_ID_CHECK_CHECKSUM);
852b6396403SSimon Glass return NULL;
853b6396403SSimon Glass }
854b6396403SSimon Glass puts("OK\n");
855b6396403SSimon Glass }
856b6396403SSimon Glass bootstage_mark(BOOTSTAGE_ID_CHECK_ARCH);
857b6396403SSimon Glass
858b6396403SSimon Glass if (!image_check_target_arch(hdr)) {
859b6396403SSimon Glass printf("Unsupported Architecture 0x%x\n", image_get_arch(hdr));
860b6396403SSimon Glass bootstage_error(BOOTSTAGE_ID_CHECK_ARCH);
861b6396403SSimon Glass return NULL;
862b6396403SSimon Glass }
863b6396403SSimon Glass return hdr;
864b6396403SSimon Glass }
865b6396403SSimon Glass #endif
866b6396403SSimon Glass
867b6396403SSimon Glass /**
868b6396403SSimon Glass * boot_get_kernel - find kernel image
869b6396403SSimon Glass * @os_data: pointer to a ulong variable, will hold os data start address
870b6396403SSimon Glass * @os_len: pointer to a ulong variable, will hold os data length
871b6396403SSimon Glass *
872b6396403SSimon Glass * boot_get_kernel() tries to find a kernel image, verifies its integrity
873b6396403SSimon Glass * and locates kernel data.
874b6396403SSimon Glass *
875b6396403SSimon Glass * returns:
876b6396403SSimon Glass * pointer to image header if valid image was found, plus kernel start
877b6396403SSimon Glass * address and length, otherwise NULL
878b6396403SSimon Glass */
boot_get_kernel(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[],bootm_headers_t * images,ulong * os_data,ulong * os_len)879b6396403SSimon Glass static const void *boot_get_kernel(cmd_tbl_t *cmdtp, int flag, int argc,
880b6396403SSimon Glass char * const argv[], bootm_headers_t *images,
881b6396403SSimon Glass ulong *os_data, ulong *os_len)
882b6396403SSimon Glass {
883b6396403SSimon Glass #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
884b6396403SSimon Glass image_header_t *hdr;
885b6396403SSimon Glass #endif
886b6396403SSimon Glass ulong img_addr;
887b6396403SSimon Glass const void *buf;
888b6396403SSimon Glass const char *fit_uname_config = NULL;
889b6396403SSimon Glass const char *fit_uname_kernel = NULL;
89073223f0eSSimon Glass #if IMAGE_ENABLE_FIT
891b6396403SSimon Glass int os_noffset;
892b6396403SSimon Glass #endif
893b6396403SSimon Glass
894e6c88a6bSBryan Wu img_addr = genimg_get_kernel_addr_fit(argc < 1 ? NULL : argv[0],
895e6c88a6bSBryan Wu &fit_uname_config,
8966c454fedSBryan Wu &fit_uname_kernel);
897b6396403SSimon Glass
898b6396403SSimon Glass bootstage_mark(BOOTSTAGE_ID_CHECK_MAGIC);
899b6396403SSimon Glass
900b6396403SSimon Glass /* check image type, for FIT images get FIT kernel node */
901b6396403SSimon Glass *os_data = *os_len = 0;
902b6396403SSimon Glass buf = map_sysmem(img_addr, 0);
903b6396403SSimon Glass switch (genimg_get_format(buf)) {
904b6396403SSimon Glass #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
905b6396403SSimon Glass case IMAGE_FORMAT_LEGACY:
906b6396403SSimon Glass printf("## Booting kernel from Legacy Image at %08lx ...\n",
907b6396403SSimon Glass img_addr);
908b6396403SSimon Glass hdr = image_get_kernel(img_addr, images->verify);
909b6396403SSimon Glass if (!hdr)
910b6396403SSimon Glass return NULL;
911b6396403SSimon Glass bootstage_mark(BOOTSTAGE_ID_CHECK_IMAGETYPE);
912b6396403SSimon Glass
913b6396403SSimon Glass /* get os_data and os_len */
914b6396403SSimon Glass switch (image_get_type(hdr)) {
915b6396403SSimon Glass case IH_TYPE_KERNEL:
916b6396403SSimon Glass case IH_TYPE_KERNEL_NOLOAD:
917b6396403SSimon Glass *os_data = image_get_data(hdr);
918b6396403SSimon Glass *os_len = image_get_data_size(hdr);
919b6396403SSimon Glass break;
920b6396403SSimon Glass case IH_TYPE_MULTI:
921b6396403SSimon Glass image_multi_getimg(hdr, 0, os_data, os_len);
922b6396403SSimon Glass break;
923b6396403SSimon Glass case IH_TYPE_STANDALONE:
924b6396403SSimon Glass *os_data = image_get_data(hdr);
925b6396403SSimon Glass *os_len = image_get_data_size(hdr);
926b6396403SSimon Glass break;
927b6396403SSimon Glass default:
928b6396403SSimon Glass printf("Wrong Image Type for %s command\n",
929b6396403SSimon Glass cmdtp->name);
930b6396403SSimon Glass bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE);
931b6396403SSimon Glass return NULL;
932b6396403SSimon Glass }
933b6396403SSimon Glass
934b6396403SSimon Glass /*
935b6396403SSimon Glass * copy image header to allow for image overwrites during
936b6396403SSimon Glass * kernel decompression.
937b6396403SSimon Glass */
938b6396403SSimon Glass memmove(&images->legacy_hdr_os_copy, hdr,
939b6396403SSimon Glass sizeof(image_header_t));
940b6396403SSimon Glass
941b6396403SSimon Glass /* save pointer to image header */
942b6396403SSimon Glass images->legacy_hdr_os = hdr;
943b6396403SSimon Glass
944b6396403SSimon Glass images->legacy_hdr_valid = 1;
945b6396403SSimon Glass bootstage_mark(BOOTSTAGE_ID_DECOMP_IMAGE);
946b6396403SSimon Glass break;
947b6396403SSimon Glass #endif
94873223f0eSSimon Glass #if IMAGE_ENABLE_FIT
949b6396403SSimon Glass case IMAGE_FORMAT_FIT:
950126cc864SSimon Glass os_noffset = fit_image_load(images, img_addr,
951b6396403SSimon Glass &fit_uname_kernel, &fit_uname_config,
952b6396403SSimon Glass IH_ARCH_DEFAULT, IH_TYPE_KERNEL,
953b6396403SSimon Glass BOOTSTAGE_ID_FIT_KERNEL_START,
954b6396403SSimon Glass FIT_LOAD_IGNORED, os_data, os_len);
955b6396403SSimon Glass if (os_noffset < 0)
956b6396403SSimon Glass return NULL;
957b6396403SSimon Glass
958b6396403SSimon Glass images->fit_hdr_os = map_sysmem(img_addr, 0);
959b6396403SSimon Glass images->fit_uname_os = fit_uname_kernel;
960b6396403SSimon Glass images->fit_uname_cfg = fit_uname_config;
961b6396403SSimon Glass images->fit_noffset_os = os_noffset;
962b6396403SSimon Glass break;
963b6396403SSimon Glass #endif
964b6396403SSimon Glass #ifdef CONFIG_ANDROID_BOOT_IMAGE
965b6396403SSimon Glass case IMAGE_FORMAT_ANDROID:
966b6396403SSimon Glass printf("## Booting Android Image at 0x%08lx ...\n", img_addr);
96707c0cd71SSimon Glass if (android_image_get_kernel(buf, images->verify,
968b6396403SSimon Glass os_data, os_len))
969b6396403SSimon Glass return NULL;
970b6396403SSimon Glass break;
971b6396403SSimon Glass #endif
972b6396403SSimon Glass default:
973b6396403SSimon Glass printf("Wrong Image Format for %s command\n", cmdtp->name);
974b6396403SSimon Glass bootstage_error(BOOTSTAGE_ID_FIT_KERNEL_INFO);
975b6396403SSimon Glass return NULL;
976b6396403SSimon Glass }
977b6396403SSimon Glass
978b6396403SSimon Glass debug(" kernel data at 0x%08lx, len = 0x%08lx (%ld)\n",
979b6396403SSimon Glass *os_data, *os_len, *os_len);
980b6396403SSimon Glass
981b6396403SSimon Glass return buf;
982b6396403SSimon Glass }
983f6c6df7eSHeinrich Schuchardt
984f6c6df7eSHeinrich Schuchardt /**
985f6c6df7eSHeinrich Schuchardt * switch_to_non_secure_mode() - switch to non-secure mode
986f6c6df7eSHeinrich Schuchardt *
987f6c6df7eSHeinrich Schuchardt * This routine is overridden by architectures requiring this feature.
988f6c6df7eSHeinrich Schuchardt */
switch_to_non_secure_mode(void)989f6c6df7eSHeinrich Schuchardt void __weak switch_to_non_secure_mode(void)
990f6c6df7eSHeinrich Schuchardt {
991f6c6df7eSHeinrich Schuchardt }
992f6c6df7eSHeinrich Schuchardt
993ce1400f6SSimon Glass #else /* USE_HOSTCC */
994ce1400f6SSimon Glass
memmove_wd(void * to,void * from,size_t len,ulong chunksz)995ce1400f6SSimon Glass void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
996ce1400f6SSimon Glass {
997ce1400f6SSimon Glass memmove(to, from, len);
998ce1400f6SSimon Glass }
999ce1400f6SSimon Glass
bootm_host_load_image(const void * fit,int req_image_type)1000ce1400f6SSimon Glass static int bootm_host_load_image(const void *fit, int req_image_type)
1001ce1400f6SSimon Glass {
1002ce1400f6SSimon Glass const char *fit_uname_config = NULL;
1003ce1400f6SSimon Glass ulong data, len;
1004ce1400f6SSimon Glass bootm_headers_t images;
1005ce1400f6SSimon Glass int noffset;
1006ce1400f6SSimon Glass ulong load_end;
1007ce1400f6SSimon Glass uint8_t image_type;
1008ce1400f6SSimon Glass uint8_t imape_comp;
1009ce1400f6SSimon Glass void *load_buf;
1010ce1400f6SSimon Glass int ret;
1011ce1400f6SSimon Glass
1012ce1400f6SSimon Glass memset(&images, '\0', sizeof(images));
1013ce1400f6SSimon Glass images.verify = 1;
1014ce1400f6SSimon Glass noffset = fit_image_load(&images, (ulong)fit,
1015ce1400f6SSimon Glass NULL, &fit_uname_config,
1016ce1400f6SSimon Glass IH_ARCH_DEFAULT, req_image_type, -1,
1017ce1400f6SSimon Glass FIT_LOAD_IGNORED, &data, &len);
1018ce1400f6SSimon Glass if (noffset < 0)
1019ce1400f6SSimon Glass return noffset;
1020ce1400f6SSimon Glass if (fit_image_get_type(fit, noffset, &image_type)) {
1021ce1400f6SSimon Glass puts("Can't get image type!\n");
1022ce1400f6SSimon Glass return -EINVAL;
1023ce1400f6SSimon Glass }
1024ce1400f6SSimon Glass
1025ce1400f6SSimon Glass if (fit_image_get_comp(fit, noffset, &imape_comp)) {
1026ce1400f6SSimon Glass puts("Can't get image compression!\n");
1027ce1400f6SSimon Glass return -EINVAL;
1028ce1400f6SSimon Glass }
1029ce1400f6SSimon Glass
1030ce1400f6SSimon Glass /* Allow the image to expand by a factor of 4, should be safe */
1031ce1400f6SSimon Glass load_buf = malloc((1 << 20) + len * 4);
1032081cc197SSimon Glass ret = bootm_decomp_image(imape_comp, 0, data, image_type, load_buf,
1033081cc197SSimon Glass (void *)data, len, CONFIG_SYS_BOOTM_LEN,
1034081cc197SSimon Glass &load_end);
1035ce1400f6SSimon Glass free(load_buf);
1036081cc197SSimon Glass
1037ce1400f6SSimon Glass if (ret && ret != BOOTM_ERR_UNIMPLEMENTED)
1038ce1400f6SSimon Glass return ret;
1039ce1400f6SSimon Glass
1040ce1400f6SSimon Glass return 0;
1041ce1400f6SSimon Glass }
1042ce1400f6SSimon Glass
bootm_host_load_images(const void * fit,int cfg_noffset)1043ce1400f6SSimon Glass int bootm_host_load_images(const void *fit, int cfg_noffset)
1044ce1400f6SSimon Glass {
1045ce1400f6SSimon Glass static uint8_t image_types[] = {
1046ce1400f6SSimon Glass IH_TYPE_KERNEL,
1047ce1400f6SSimon Glass IH_TYPE_FLATDT,
1048ce1400f6SSimon Glass IH_TYPE_RAMDISK,
1049ce1400f6SSimon Glass };
1050ce1400f6SSimon Glass int err = 0;
1051ce1400f6SSimon Glass int i;
1052ce1400f6SSimon Glass
1053ce1400f6SSimon Glass for (i = 0; i < ARRAY_SIZE(image_types); i++) {
1054ce1400f6SSimon Glass int ret;
1055ce1400f6SSimon Glass
1056ce1400f6SSimon Glass ret = bootm_host_load_image(fit, image_types[i]);
1057ce1400f6SSimon Glass if (!err && ret && ret != -ENOENT)
1058ce1400f6SSimon Glass err = ret;
1059ce1400f6SSimon Glass }
1060ce1400f6SSimon Glass
1061ce1400f6SSimon Glass /* Return the first error we found */
1062ce1400f6SSimon Glass return err;
1063ce1400f6SSimon Glass }
1064ea51a628SSimon Glass
1065ea51a628SSimon Glass #endif /* ndef USE_HOSTCC */
1066