]> git.ipfire.org Git - thirdparty/u-boot.git/blame - boot/bootm.c
Merge tag 'u-boot-rockchip-20231007' of https://source.denx.de/u-boot/custodians...
[thirdparty/u-boot.git] / boot / bootm.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
b6396403
SG
2/*
3 * (C) Copyright 2000-2009
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
b6396403
SG
5 */
6
ea51a628 7#ifndef USE_HOSTCC
b6396403 8#include <common.h>
ea51a628 9#include <bootstage.h>
51bb3384 10#include <cli.h>
be595146 11#include <command.h>
1eb69ae4 12#include <cpu_func.h>
c7694dd4 13#include <env.h>
90268b87 14#include <errno.h>
b6396403 15#include <fdt_support.h>
36bf446b 16#include <irq_func.h>
b6396403 17#include <lmb.h>
f7ae49fc 18#include <log.h>
b6396403 19#include <malloc.h>
0eb25b61 20#include <mapmem.h>
90526e9f
SG
21#include <net.h>
22#include <asm/cache.h>
401d1c4f 23#include <asm/global_data.h>
b6396403 24#include <asm/io.h>
b6386f38 25#include <linux/sizes.h>
b6396403
SG
26#if defined(CONFIG_CMD_USB)
27#include <usb.h>
28#endif
ea51a628
SG
29#else
30#include "mkimage.h"
31#endif
b6396403 32
ea51a628
SG
33#include <bootm.h>
34#include <image.h>
b6396403 35
b6386f38
SG
36#define MAX_CMDLINE_SIZE SZ_4K
37
b6396403
SG
38#define IH_INITRD_ARCH IH_ARCH_DEFAULT
39
ea51a628
SG
40#ifndef USE_HOSTCC
41
42DECLARE_GLOBAL_DATA_PTR;
43
d9d7c20b 44struct bootm_headers images; /* pointers to os/initrd/fdt images */
5db28905 45
09140113 46static const void *boot_get_kernel(struct cmd_tbl *cmdtp, int flag, int argc,
d9d7c20b 47 char *const argv[], struct bootm_headers *images,
b6396403
SG
48 ulong *os_data, ulong *os_len);
49
329da485
SG
50__weak void board_quiesce_devices(void)
51{
52}
53
b6396403 54#ifdef CONFIG_LMB
d9d7c20b 55static void boot_start_lmb(struct bootm_headers *images)
b6396403
SG
56{
57 ulong mem_start;
58 phys_size_t mem_size;
59
723806cc
SG
60 mem_start = env_get_bootm_low();
61 mem_size = env_get_bootm_size();
b6396403 62
9cc2323f
SG
63 lmb_init_and_reserve_range(&images->lmb, (phys_addr_t)mem_start,
64 mem_size, NULL);
b6396403
SG
65}
66#else
67#define lmb_reserve(lmb, base, size)
d9d7c20b 68static inline void boot_start_lmb(struct bootm_headers *images) { }
b6396403
SG
69#endif
70
09140113
SG
71static int bootm_start(struct cmd_tbl *cmdtp, int flag, int argc,
72 char *const argv[])
b6396403
SG
73{
74 memset((void *)&images, 0, sizeof(images));
bfebc8c9 75 images.verify = env_get_yesno("verify");
b6396403
SG
76
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
9d46e63d
PR
85static 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
494bcf1a 103 if (IS_ENABLED(CONFIG_CMD_BOOTM_PRE_LOAD))
9d46e63d
PR
104 ret = image_pre_load(data_addr);
105
106 if (ret)
107 ret = CMD_RET_FAILURE;
108
109 return ret;
110}
111
09140113
SG
112static int bootm_find_os(struct cmd_tbl *cmdtp, int flag, int argc,
113 char *const argv[])
b6396403
SG
114{
115 const void *os_hdr;
636da203
SO
116#ifdef CONFIG_ANDROID_BOOT_IMAGE
117 const void *vendor_boot_img;
118 const void *boot_img;
119#endif
b6396403 120 bool ep_found = false;
90268b87 121 int ret;
b6396403
SG
122
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)) {
c76c93a3 133#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
b6396403
SG
134 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);
90268b87 141 images.os.arch = image_get_arch(os_hdr);
b6396403
SG
142 break;
143#endif
bf371b4c 144#if CONFIG_IS_ENABLED(FIT)
b6396403
SG
145 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
90268b87
SG
169 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
b6396403
SG
176 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:
636da203
SO
188 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 }
b6396403 194 images.os.type = IH_TYPE_KERNEL;
636da203 195 images.os.comp = android_image_get_kcomp(boot_img, vendor_boot_img);
b6396403 196 images.os.os = IH_OS_LINUX;
636da203
SO
197 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);
86f4695b
AD
199 images.ep = images.os.load;
200 ep_found = true;
636da203
SO
201 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
202 unmap_sysmem(vendor_boot_img);
203 unmap_sysmem(boot_img);
204 }
b6396403
SG
205 break;
206#endif
207 default:
208 puts("ERROR: unknown image format type!\n");
209 return 1;
210 }
211
90268b87 212 /* If we have a valid setup.bin, we will use that for entry (x86) */
5bda35cf
SG
213 if (images.os.arch == IH_ARCH_I386 ||
214 images.os.arch == IH_ARCH_X86_64) {
90268b87
SG
215 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) {
b6396403 224 images.ep = image_get_ep(&images.legacy_hdr_os_copy);
bf371b4c 225#if CONFIG_IS_ENABLED(FIT)
b6396403
SG
226 } 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) {
ef65aa35 242 if (IS_ENABLED(CONFIG_CMD_BOOTI) &&
4533b3d0
HS
243 images.os.arch == IH_ARCH_ARM64 &&
244 images.os.os == IH_OS_LINUX) {
487b5fa6
MV
245 ulong image_addr;
246 ulong image_size;
247
248 ret = booti_setup(images.os.image_start, &image_addr,
249 &image_size, true);
250 if (ret != 0)
251 return 1;
252
253 images.os.type = IH_TYPE_KERNEL;
254 images.os.load = image_addr;
255 images.ep = image_addr;
256 } else {
257 images.os.load = images.os.image_start;
258 images.ep += images.os.image_start;
259 }
b6396403
SG
260 }
261
7a80de46 262 images.os.start = map_to_sysmem(os_hdr);
b6396403
SG
263
264 return 0;
265}
266
d52e8575
KA
267/**
268 * bootm_find_images - wrapper to find and locate various images
269 * @flag: Ignored Argument
270 * @argc: command argument count
271 * @argv: command argument list
fbde7589
TK
272 * @start: OS image start address
273 * @size: OS image size
d52e8575
KA
274 *
275 * boot_find_images() will attempt to load an available ramdisk,
276 * flattened device tree, as well as specifically marked
277 * "loadable" images (loadables are FIT only)
278 *
279 * Note: bootm_find_images will skip an image if it is not found
280 *
281 * @return:
282 * 0, if all existing images were loaded correctly
283 * 1, if an image is found but corrupted, or invalid
284 */
fbde7589
TK
285int bootm_find_images(int flag, int argc, char *const argv[], ulong start,
286 ulong size)
b6396403
SG
287{
288 int ret;
289
290 /* find ramdisk */
291 ret = boot_get_ramdisk(argc, argv, &images, IH_INITRD_ARCH,
292 &images.rd_start, &images.rd_end);
293 if (ret) {
294 puts("Ramdisk image is corrupt or invalid\n");
295 return 1;
296 }
297
fbde7589
TK
298 /* check if ramdisk overlaps OS image */
299 if (images.rd_start && (((ulong)images.rd_start >= start &&
ef4f4f1f
JC
300 (ulong)images.rd_start < start + size) ||
301 ((ulong)images.rd_end > start &&
302 (ulong)images.rd_end <= start + size) ||
303 ((ulong)images.rd_start < start &&
304 (ulong)images.rd_end >= start + size))) {
fbde7589
TK
305 printf("ERROR: RD image overlaps OS image (OS=0x%lx..0x%lx)\n",
306 start, start + size);
307 return 1;
308 }
309
0c303f9a 310#if CONFIG_IS_ENABLED(OF_LIBFDT)
b6396403
SG
311 /* find flattened device tree */
312 ret = boot_get_fdt(flag, argc, argv, IH_ARCH_DEFAULT, &images,
313 &images.ft_addr, &images.ft_len);
314 if (ret) {
315 puts("Could not find a valid device tree\n");
316 return 1;
317 }
fbde7589
TK
318
319 /* check if FDT overlaps OS image */
320 if (images.ft_addr &&
321 (((ulong)images.ft_addr >= start &&
5acfdfbd 322 (ulong)images.ft_addr < start + size) ||
fbde7589 323 ((ulong)images.ft_addr + images.ft_len >= start &&
5acfdfbd 324 (ulong)images.ft_addr + images.ft_len < start + size))) {
fbde7589
TK
325 printf("ERROR: FDT image overlaps OS image (OS=0x%lx..0x%lx)\n",
326 start, start + size);
327 return 1;
328 }
329
3a09f38d 330 if (IS_ENABLED(CONFIG_CMD_FDT))
596be5f3 331 set_working_fdt_addr(map_to_sysmem(images.ft_addr));
b6396403
SG
332#endif
333
bf371b4c 334#if CONFIG_IS_ENABLED(FIT)
4ed37abc
SG
335 if (IS_ENABLED(CONFIG_FPGA)) {
336 /* find bitstreams */
337 ret = boot_get_fpga(argc, argv, &images, IH_ARCH_DEFAULT,
338 NULL, NULL);
339 if (ret) {
340 printf("FPGA image is corrupted or invalid\n");
341 return 1;
342 }
62afc601 343 }
62afc601 344
84a07dbf
KA
345 /* find all of the loadables */
346 ret = boot_get_loadable(argc, argv, &images, IH_ARCH_DEFAULT,
347 NULL, NULL);
348 if (ret) {
349 printf("Loadable(s) is corrupt or invalid\n");
350 return 1;
351 }
84a07dbf
KA
352#endif
353
b6396403
SG
354 return 0;
355}
356
09140113
SG
357static int bootm_find_other(struct cmd_tbl *cmdtp, int flag, int argc,
358 char *const argv[])
b6396403
SG
359{
360 if (((images.os.type == IH_TYPE_KERNEL) ||
361 (images.os.type == IH_TYPE_KERNEL_NOLOAD) ||
362 (images.os.type == IH_TYPE_MULTI)) &&
363 (images.os.os == IH_OS_LINUX ||
364 images.os.os == IH_OS_VXWORKS))
fbde7589 365 return bootm_find_images(flag, argc, argv, 0, 0);
b6396403
SG
366
367 return 0;
368}
40e5975f
SG
369#endif /* USE_HOSTC */
370
2090854c 371#if !defined(USE_HOSTCC) || defined(CONFIG_FIT_SIGNATURE)
3086c055
SG
372/**
373 * handle_decomp_error() - display a decompression error
374 *
375 * This function tries to produce a useful message. In the case where the
376 * uncompressed size is the same as the available space, we can assume that
377 * the image is too large for the buffer.
378 *
379 * @comp_type: Compression type being used (IH_COMP_...)
380 * @uncomp_size: Number of bytes uncompressed
c45568cc 381 * @buf_size: Number of bytes the decompresion buffer was
2090854c 382 * @ret: errno error code received from compression library
185f812c 383 * Return: Appropriate BOOTM_ERR_ error code
3086c055 384 */
c45568cc
TR
385static int handle_decomp_error(int comp_type, size_t uncomp_size,
386 size_t buf_size, int ret)
40e5975f 387{
3086c055
SG
388 const char *name = genimg_get_comp_name(comp_type);
389
2090854c
JW
390 /* ENOSYS means unimplemented compression type, don't reset. */
391 if (ret == -ENOSYS)
392 return BOOTM_ERR_UNIMPLEMENTED;
393
c45568cc 394 if (uncomp_size >= buf_size)
3086c055 395 printf("Image too large: increase CONFIG_SYS_BOOTM_LEN\n");
40e5975f 396 else
3086c055
SG
397 printf("%s: uncompress error %d\n", name, ret);
398
399 /*
400 * The decompression routines are now safe, so will not write beyond
401 * their bounds. Probably it is not necessary to reset, but maintain
402 * the current behaviour for now.
403 */
404 printf("Must RESET board to recover\n");
40e5975f
SG
405#ifndef USE_HOSTCC
406 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
407#endif
408
409 return BOOTM_ERR_RESET;
410}
2090854c 411#endif
2b164f1c 412
ce1400f6 413#ifndef USE_HOSTCC
d9d7c20b 414static int bootm_load_os(struct bootm_headers *images, int boot_progress)
2b164f1c 415{
da79b2f2 416 struct image_info os = images->os;
2b164f1c 417 ulong load = os.load;
cc955358 418 ulong load_end;
2b164f1c
SG
419 ulong blob_start = os.start;
420 ulong blob_end = os.end;
421 ulong image_start = os.image_start;
422 ulong image_len = os.image_len;
bbac9222 423 ulong flush_start = ALIGN_DOWN(load, ARCH_DMA_MINALIGN);
2b164f1c
SG
424 bool no_overlap;
425 void *load_buf, *image_buf;
426 int err;
427
428 load_buf = map_sysmem(load, 0);
429 image_buf = map_sysmem(os.image_start, image_len);
2090854c
JW
430 err = image_decomp(os.comp, load, os.image_start, os.type,
431 load_buf, image_buf, image_len,
432 CONFIG_SYS_BOOTM_LEN, &load_end);
2b164f1c 433 if (err) {
c45568cc
TR
434 err = handle_decomp_error(os.comp, load_end - load,
435 CONFIG_SYS_BOOTM_LEN, err);
2b164f1c
SG
436 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
437 return err;
438 }
21d39468
HS
439 /* We need the decompressed image size in the next steps */
440 images->os.image_len = load_end - load;
bbac9222 441
b4353b37 442 flush_cache(flush_start, ALIGN(load_end, ARCH_DMA_MINALIGN) - flush_start);
b6396403 443
cc955358 444 debug(" kernel loaded at 0x%08lx, end = 0x%08lx\n", load, load_end);
b6396403
SG
445 bootstage_mark(BOOTSTAGE_ID_KERNEL_LOADED);
446
2b164f1c
SG
447 no_overlap = (os.comp == IH_COMP_NONE && load == image_start);
448
cc955358 449 if (!no_overlap && load < blob_end && load_end > blob_start) {
b6396403
SG
450 debug("images.os.start = 0x%lX, images.os.end = 0x%lx\n",
451 blob_start, blob_end);
452 debug("images.os.load = 0x%lx, load_end = 0x%lx\n", load,
cc955358 453 load_end);
b6396403
SG
454
455 /* Check what type of image this is. */
456 if (images->legacy_hdr_valid) {
457 if (image_get_type(&images->legacy_hdr_os_copy)
458 == IH_TYPE_MULTI)
459 puts("WARNING: legacy format multi component image overwritten\n");
460 return BOOTM_ERR_OVERLAP;
461 } else {
462 puts("ERROR: new format image overwritten - must RESET the board to recover\n");
463 bootstage_error(BOOTSTAGE_ID_OVERWRITTEN);
464 return BOOTM_ERR_RESET;
465 }
466 }
467
cc955358
TR
468 lmb_reserve(&images->lmb, images->os.load, (load_end -
469 images->os.load));
b6396403
SG
470 return 0;
471}
472
473/**
474 * bootm_disable_interrupts() - Disable interrupts in preparation for load/boot
475 *
185f812c 476 * Return: interrupt flag (0 if interrupts were disabled, non-zero if they were
b6396403
SG
477 * enabled)
478 */
479ulong bootm_disable_interrupts(void)
480{
481 ulong iflag;
482
483 /*
484 * We have reached the point of no return: we are going to
485 * overwrite all exception vector code, so we cannot easily
486 * recover from any failures any more...
487 */
488 iflag = disable_interrupts();
489#ifdef CONFIG_NETCONSOLE
490 /* Stop the ethernet stack if NetConsole could have left it up */
491 eth_halt();
b6396403
SG
492#endif
493
494#if defined(CONFIG_CMD_USB)
495 /*
496 * turn off USB to prevent the host controller from writing to the
497 * SDRAM while Linux is booting. This could happen (at least for OHCI
498 * controller), because the HCCA (Host Controller Communication Area)
499 * lies within the SDRAM and the host controller writes continously to
500 * this area (as busmaster!). The HccaFrameNumber is for example
501 * updated every 1 ms within the HCCA structure in SDRAM! For more
502 * details see the OpenHCI specification.
503 */
504 usb_stop();
505#endif
506 return iflag;
507}
508
6cd92b1a 509#define CONSOLE_ARG "console="
ba9aa40b
SA
510#define NULL_CONSOLE (CONSOLE_ARG "ttynull")
511#define CONSOLE_ARG_SIZE sizeof(NULL_CONSOLE)
b6396403 512
b6386f38
SG
513/**
514 * fixup_silent_linux() - Handle silencing the linux boot if required
515 *
516 * This uses the silent_linux envvar to control whether to add/set a "console="
517 * parameter to the command line
518 *
519 * @buf: Buffer containing the string to process
520 * @maxlen: Maximum length of buffer
185f812c 521 * Return: 0 if OK, -ENOSPC if @maxlen is too small
b6386f38
SG
522 */
523static int fixup_silent_linux(char *buf, int maxlen)
b6396403 524{
b6396403 525 int want_silent;
b6386f38
SG
526 char *cmdline;
527 int size;
b6396403 528
b6386f38
SG
529 /*
530 * Move the input string to the end of buffer. The output string will be
531 * built up at the start.
532 */
533 size = strlen(buf) + 1;
534 if (size * 2 > maxlen)
535 return -ENOSPC;
536 cmdline = buf + maxlen - size;
537 memmove(cmdline, buf, size);
b6396403
SG
538 /*
539 * Only fix cmdline when requested. The environment variable can be:
540 *
541 * no - we never fixup
542 * yes - we always fixup
543 * unset - we rely on the console silent flag
544 */
bfebc8c9 545 want_silent = env_get_yesno("silent_linux");
b6396403 546 if (want_silent == 0)
4ae42643 547 return 0;
b6396403 548 else if (want_silent == -1 && !(gd->flags & GD_FLG_SILENT))
4ae42643 549 return 0;
b6396403
SG
550
551 debug("before silent fix-up: %s\n", cmdline);
b6386f38 552 if (*cmdline) {
b6396403
SG
553 char *start = strstr(cmdline, CONSOLE_ARG);
554
b6386f38
SG
555 /* Check space for maximum possible new command line */
556 if (size + CONSOLE_ARG_SIZE > maxlen)
4ae42643 557 return -ENOSPC;
b6396403
SG
558
559 if (start) {
560 char *end = strchr(start, ' ');
6cd92b1a 561 int start_bytes;
b6396403 562
ba9aa40b 563 start_bytes = start - cmdline;
6cd92b1a 564 strncpy(buf, cmdline, start_bytes);
ba9aa40b 565 strncpy(buf + start_bytes, NULL_CONSOLE, CONSOLE_ARG_SIZE);
b6396403 566 if (end)
ba9aa40b 567 strcpy(buf + start_bytes + CONSOLE_ARG_SIZE - 1, end);
b6396403 568 else
ba9aa40b 569 buf[start_bytes + CONSOLE_ARG_SIZE] = '\0';
b6396403 570 } else {
ba9aa40b 571 sprintf(buf, "%s %s", cmdline, NULL_CONSOLE);
b6396403 572 }
b6386f38
SG
573 if (buf + strlen(buf) >= cmdline)
574 return -ENOSPC;
b6396403 575 } else {
ba9aa40b 576 if (maxlen < CONSOLE_ARG_SIZE)
b6386f38 577 return -ENOSPC;
ba9aa40b 578 strcpy(buf, NULL_CONSOLE);
b6396403 579 }
b6386f38
SG
580 debug("after silent fix-up: %s\n", buf);
581
582 return 0;
583}
584
51bb3384
SG
585/**
586 * process_subst() - Handle substitution of ${...} fields in the environment
587 *
588 * Handle variable substitution in the provided buffer
589 *
590 * @buf: Buffer containing the string to process
591 * @maxlen: Maximum length of buffer
185f812c 592 * Return: 0 if OK, -ENOSPC if @maxlen is too small
51bb3384
SG
593 */
594static int process_subst(char *buf, int maxlen)
595{
596 char *cmdline;
597 int size;
598 int ret;
599
600 /* Move to end of buffer */
601 size = strlen(buf) + 1;
602 cmdline = buf + maxlen - size;
603 if (buf + size > cmdline)
604 return -ENOSPC;
605 memmove(cmdline, buf, size);
606
607 ret = cli_simple_process_macros(cmdline, buf, cmdline - buf);
608
609 return ret;
610}
611
4448fe8e
SG
612int bootm_process_cmdline(char *buf, int maxlen, int flags)
613{
614 int ret;
615
616 /* Check config first to enable compiler to eliminate code */
617 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) &&
618 !IS_ENABLED(CONFIG_SILENT_U_BOOT_ONLY) &&
619 (flags & BOOTM_CL_SILENT)) {
620 ret = fixup_silent_linux(buf, maxlen);
621 if (ret)
622 return log_msg_ret("silent", ret);
623 }
a8d69627
SG
624 if (IS_ENABLED(CONFIG_BOOTARGS_SUBST) && IS_ENABLED(CONFIG_CMDLINE) &&
625 (flags & BOOTM_CL_SUBST)) {
51bb3384
SG
626 ret = process_subst(buf, maxlen);
627 if (ret)
185756ec 628 return log_msg_ret("subst", ret);
51bb3384 629 }
4448fe8e
SG
630
631 return 0;
632}
633
b3c01678 634int bootm_process_cmdline_env(int flags)
b6386f38
SG
635{
636 const int maxlen = MAX_CMDLINE_SIZE;
b3c01678 637 bool do_silent;
b6386f38
SG
638 const char *env;
639 char *buf;
640 int ret;
b6396403 641
b6386f38
SG
642 /* First check if any action is needed */
643 do_silent = IS_ENABLED(CONFIG_SILENT_CONSOLE) &&
b3c01678 644 !IS_ENABLED(CONFIG_SILENT_U_BOOT_ONLY) && (flags & BOOTM_CL_SILENT);
51bb3384 645 if (!do_silent && !IS_ENABLED(CONFIG_BOOTARGS_SUBST))
b6386f38
SG
646 return 0;
647
648 env = env_get("bootargs");
649 if (env && strlen(env) >= maxlen)
650 return -E2BIG;
651 buf = malloc(maxlen);
652 if (!buf)
653 return -ENOMEM;
654 if (env)
655 strcpy(buf, env);
656 else
657 *buf = '\0';
4448fe8e 658 ret = bootm_process_cmdline(buf, maxlen, flags);
b6386f38
SG
659 if (!ret) {
660 ret = env_set("bootargs", buf);
661
662 /*
663 * If buf is "" and bootargs does not exist, this will produce
664 * an error trying to delete bootargs. Ignore it
665 */
666 if (ret == -ENOENT)
667 ret = 0;
668 }
b6396403 669 free(buf);
b6386f38
SG
670 if (ret)
671 return log_msg_ret("env", ret);
4ae42643
SG
672
673 return 0;
b6396403 674}
b6396403
SG
675
676/**
677 * Execute selected states of the bootm command.
678 *
679 * Note the arguments to this state must be the first argument, Any 'bootm'
680 * or sub-command arguments must have already been taken.
681 *
682 * Note that if states contains more than one flag it MUST contain
683 * BOOTM_STATE_START, since this handles and consumes the command line args.
684 *
685 * Also note that aside from boot_os_fn functions and bootm_load_os no other
686 * functions we store the return value of in 'ret' may use a negative return
687 * value, without special handling.
688 *
689 * @param cmdtp Pointer to bootm command table entry
690 * @param flag Command flags (CMD_FLAG_...)
691 * @param argc Number of subcommand arguments (0 = no arguments)
692 * @param argv Arguments
693 * @param states Mask containing states to run (BOOTM_STATE_...)
694 * @param images Image header information
695 * @param boot_progress 1 to show boot progress, 0 to not do this
185f812c 696 * Return: 0 if ok, something else on error. Some errors will cause this
b6396403
SG
697 * function to perform a reboot! If states contains BOOTM_STATE_OS_GO
698 * then the intent is to boot an OS, so this function will not return
699 * unless the image type is standalone.
700 */
09140113 701int do_bootm_states(struct cmd_tbl *cmdtp, int flag, int argc,
d9d7c20b 702 char *const argv[], int states, struct bootm_headers *images,
09140113 703 int boot_progress)
b6396403
SG
704{
705 boot_os_fn *boot_fn;
706 ulong iflag = 0;
707 int ret = 0, need_boot_fn;
708
709 images->state |= states;
710
711 /*
712 * Work through the states and see how far we get. We stop on
713 * any error.
714 */
715 if (states & BOOTM_STATE_START)
716 ret = bootm_start(cmdtp, flag, argc, argv);
717
9d46e63d
PR
718 if (!ret && (states & BOOTM_STATE_PRE_LOAD))
719 ret = bootm_pre_load(cmdtp, flag, argc, argv);
720
b6396403
SG
721 if (!ret && (states & BOOTM_STATE_FINDOS))
722 ret = bootm_find_os(cmdtp, flag, argc, argv);
723
ba079840 724 if (!ret && (states & BOOTM_STATE_FINDOTHER))
b6396403 725 ret = bootm_find_other(cmdtp, flag, argc, argv);
b6396403
SG
726
727 /* Load the OS */
728 if (!ret && (states & BOOTM_STATE_LOADOS)) {
b6396403 729 iflag = bootm_disable_interrupts();
cc955358
TR
730 ret = bootm_load_os(images, 0);
731 if (ret && ret != BOOTM_ERR_OVERLAP)
b6396403
SG
732 goto err;
733 else if (ret == BOOTM_ERR_OVERLAP)
734 ret = 0;
b6396403
SG
735 }
736
737 /* Relocate the ramdisk */
738#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
739 if (!ret && (states & BOOTM_STATE_RAMDISK)) {
740 ulong rd_len = images->rd_end - images->rd_start;
741
742 ret = boot_ramdisk_high(&images->lmb, images->rd_start,
743 rd_len, &images->initrd_start, &images->initrd_end);
744 if (!ret) {
018f5303
SG
745 env_set_hex("initrd_start", images->initrd_start);
746 env_set_hex("initrd_end", images->initrd_end);
b6396403
SG
747 }
748 }
749#endif
0c303f9a 750#if CONFIG_IS_ENABLED(OF_LIBFDT) && defined(CONFIG_LMB)
b6396403
SG
751 if (!ret && (states & BOOTM_STATE_FDT)) {
752 boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr);
753 ret = boot_relocate_fdt(&images->lmb, &images->ft_addr,
754 &images->ft_len);
755 }
756#endif
757
758 /* From now on, we need the OS boot function */
759 if (ret)
760 return ret;
761 boot_fn = bootm_os_get_boot_func(images->os.os);
762 need_boot_fn = states & (BOOTM_STATE_OS_CMDLINE |
763 BOOTM_STATE_OS_BD_T | BOOTM_STATE_OS_PREP |
764 BOOTM_STATE_OS_FAKE_GO | BOOTM_STATE_OS_GO);
765 if (boot_fn == NULL && need_boot_fn) {
766 if (iflag)
767 enable_interrupts();
768 printf("ERROR: booting os '%s' (%d) is not supported\n",
769 genimg_get_os_name(images->os.os), images->os.os);
770 bootstage_error(BOOTSTAGE_ID_CHECK_BOOT_OS);
771 return 1;
772 }
773
19e8649e 774
b6396403
SG
775 /* Call various other states that are not generally used */
776 if (!ret && (states & BOOTM_STATE_OS_CMDLINE))
777 ret = boot_fn(BOOTM_STATE_OS_CMDLINE, argc, argv, images);
778 if (!ret && (states & BOOTM_STATE_OS_BD_T))
779 ret = boot_fn(BOOTM_STATE_OS_BD_T, argc, argv, images);
19e8649e 780 if (!ret && (states & BOOTM_STATE_OS_PREP)) {
51bb3384 781 ret = bootm_process_cmdline_env(images->os.os == IH_OS_LINUX);
d9477a0a
SG
782 if (ret) {
783 printf("Cmdline setup failed (err=%d)\n", ret);
784 ret = CMD_RET_FAILURE;
785 goto err;
4ae42643 786 }
b6396403 787 ret = boot_fn(BOOTM_STATE_OS_PREP, argc, argv, images);
19e8649e 788 }
b6396403
SG
789
790#ifdef CONFIG_TRACE
791 /* Pretend to run the OS, then run a user command */
792 if (!ret && (states & BOOTM_STATE_OS_FAKE_GO)) {
00caae6d 793 char *cmd_list = env_get("fakegocmd");
b6396403
SG
794
795 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_FAKE_GO,
796 images, boot_fn);
797 if (!ret && cmd_list)
798 ret = run_command_list(cmd_list, -1, flag);
799 }
800#endif
801
802 /* Check for unsupported subcommand. */
803 if (ret) {
13819f07 804 printf("subcommand failed (err=%d)\n", ret);
b6396403
SG
805 return ret;
806 }
807
808 /* Now run the OS! We hope this doesn't return */
809 if (!ret && (states & BOOTM_STATE_OS_GO))
810 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_GO,
811 images, boot_fn);
812
813 /* Deal with any fallout */
814err:
815 if (iflag)
816 enable_interrupts();
817
818 if (ret == BOOTM_ERR_UNIMPLEMENTED)
819 bootstage_error(BOOTSTAGE_ID_DECOMP_UNIMPL);
820 else if (ret == BOOTM_ERR_RESET)
821 do_reset(cmdtp, flag, argc, argv);
822
823 return ret;
824}
825
daffb0be
SG
826int bootm_boot_start(ulong addr, const char *cmdline)
827{
828 static struct cmd_tbl cmd = {"bootm"};
829 char addr_str[30];
830 char *argv[] = {addr_str, NULL};
831 int states;
832 int ret;
833
834 /*
835 * TODO(sjg@chromium.org): This uses the command-line interface, but
836 * should not. To clean this up, the various bootm states need to be
837 * passed an info structure instead of cmdline flags. Then this can
838 * set up the required info and move through the states without needing
839 * the command line.
840 */
841 states = BOOTM_STATE_START | BOOTM_STATE_FINDOS | BOOTM_STATE_PRE_LOAD |
842 BOOTM_STATE_FINDOTHER | BOOTM_STATE_LOADOS |
843 BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
844 BOOTM_STATE_OS_GO;
845 if (IS_ENABLED(CONFIG_SYS_BOOT_RAMDISK_HIGH))
846 states |= BOOTM_STATE_RAMDISK;
847 if (IS_ENABLED(CONFIG_PPC) || IS_ENABLED(CONFIG_MIPS))
848 states |= BOOTM_STATE_OS_CMDLINE;
849 images.state |= states;
850
851 snprintf(addr_str, sizeof(addr_str), "%lx", addr);
852
853 ret = env_set("bootargs", cmdline);
854 if (ret) {
855 printf("Failed to set cmdline\n");
856 return ret;
857 }
858 ret = do_bootm_states(&cmd, 0, 1, argv, states, &images, 1);
859
860 return ret;
861}
862
c76c93a3 863#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
b6396403
SG
864/**
865 * image_get_kernel - verify legacy format kernel image
866 * @img_addr: in RAM address of the legacy format image to be verified
867 * @verify: data CRC verification flag
868 *
869 * image_get_kernel() verifies legacy image integrity and returns pointer to
870 * legacy image header if image verification was completed successfully.
871 *
872 * returns:
873 * pointer to a legacy image header if valid image was found
874 * otherwise return NULL
875 */
f3543e69 876static struct legacy_img_hdr *image_get_kernel(ulong img_addr, int verify)
b6396403 877{
f3543e69 878 struct legacy_img_hdr *hdr = (struct legacy_img_hdr *)img_addr;
b6396403
SG
879
880 if (!image_check_magic(hdr)) {
881 puts("Bad Magic Number\n");
882 bootstage_error(BOOTSTAGE_ID_CHECK_MAGIC);
883 return NULL;
884 }
885 bootstage_mark(BOOTSTAGE_ID_CHECK_HEADER);
886
887 if (!image_check_hcrc(hdr)) {
888 puts("Bad Header Checksum\n");
889 bootstage_error(BOOTSTAGE_ID_CHECK_HEADER);
890 return NULL;
891 }
892
893 bootstage_mark(BOOTSTAGE_ID_CHECK_CHECKSUM);
894 image_print_contents(hdr);
895
896 if (verify) {
897 puts(" Verifying Checksum ... ");
898 if (!image_check_dcrc(hdr)) {
899 printf("Bad Data CRC\n");
900 bootstage_error(BOOTSTAGE_ID_CHECK_CHECKSUM);
901 return NULL;
902 }
903 puts("OK\n");
904 }
905 bootstage_mark(BOOTSTAGE_ID_CHECK_ARCH);
906
907 if (!image_check_target_arch(hdr)) {
908 printf("Unsupported Architecture 0x%x\n", image_get_arch(hdr));
909 bootstage_error(BOOTSTAGE_ID_CHECK_ARCH);
910 return NULL;
911 }
912 return hdr;
913}
914#endif
915
916/**
917 * boot_get_kernel - find kernel image
918 * @os_data: pointer to a ulong variable, will hold os data start address
919 * @os_len: pointer to a ulong variable, will hold os data length
920 *
921 * boot_get_kernel() tries to find a kernel image, verifies its integrity
922 * and locates kernel data.
923 *
924 * returns:
925 * pointer to image header if valid image was found, plus kernel start
926 * address and length, otherwise NULL
927 */
09140113 928static const void *boot_get_kernel(struct cmd_tbl *cmdtp, int flag, int argc,
d9d7c20b 929 char *const argv[], struct bootm_headers *images,
b6396403
SG
930 ulong *os_data, ulong *os_len)
931{
c76c93a3 932#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
f3543e69 933 struct legacy_img_hdr *hdr;
b6396403
SG
934#endif
935 ulong img_addr;
936 const void *buf;
b6396403
SG
937 const char *fit_uname_config = NULL;
938 const char *fit_uname_kernel = NULL;
bf371b4c 939#if CONFIG_IS_ENABLED(FIT)
b6396403
SG
940 int os_noffset;
941#endif
942
636da203
SO
943#ifdef CONFIG_ANDROID_BOOT_IMAGE
944 const void *boot_img;
945 const void *vendor_boot_img;
946#endif
e6c88a6b
BW
947 img_addr = genimg_get_kernel_addr_fit(argc < 1 ? NULL : argv[0],
948 &fit_uname_config,
949 &fit_uname_kernel);
b6396403 950
494bcf1a 951 if (IS_ENABLED(CONFIG_CMD_BOOTM_PRE_LOAD))
9d46e63d
PR
952 img_addr += image_load_offset;
953
b6396403
SG
954 bootstage_mark(BOOTSTAGE_ID_CHECK_MAGIC);
955
b6396403
SG
956 /* check image type, for FIT images get FIT kernel node */
957 *os_data = *os_len = 0;
958 buf = map_sysmem(img_addr, 0);
959 switch (genimg_get_format(buf)) {
c76c93a3 960#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
b6396403
SG
961 case IMAGE_FORMAT_LEGACY:
962 printf("## Booting kernel from Legacy Image at %08lx ...\n",
963 img_addr);
964 hdr = image_get_kernel(img_addr, images->verify);
965 if (!hdr)
966 return NULL;
967 bootstage_mark(BOOTSTAGE_ID_CHECK_IMAGETYPE);
968
969 /* get os_data and os_len */
970 switch (image_get_type(hdr)) {
971 case IH_TYPE_KERNEL:
972 case IH_TYPE_KERNEL_NOLOAD:
973 *os_data = image_get_data(hdr);
974 *os_len = image_get_data_size(hdr);
975 break;
976 case IH_TYPE_MULTI:
977 image_multi_getimg(hdr, 0, os_data, os_len);
978 break;
979 case IH_TYPE_STANDALONE:
980 *os_data = image_get_data(hdr);
981 *os_len = image_get_data_size(hdr);
982 break;
983 default:
984 printf("Wrong Image Type for %s command\n",
985 cmdtp->name);
986 bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE);
987 return NULL;
988 }
989
990 /*
991 * copy image header to allow for image overwrites during
992 * kernel decompression.
993 */
994 memmove(&images->legacy_hdr_os_copy, hdr,
f3543e69 995 sizeof(struct legacy_img_hdr));
b6396403
SG
996
997 /* save pointer to image header */
998 images->legacy_hdr_os = hdr;
999
1000 images->legacy_hdr_valid = 1;
1001 bootstage_mark(BOOTSTAGE_ID_DECOMP_IMAGE);
1002 break;
1003#endif
bf371b4c 1004#if CONFIG_IS_ENABLED(FIT)
b6396403 1005 case IMAGE_FORMAT_FIT:
126cc864 1006 os_noffset = fit_image_load(images, img_addr,
b6396403
SG
1007 &fit_uname_kernel, &fit_uname_config,
1008 IH_ARCH_DEFAULT, IH_TYPE_KERNEL,
1009 BOOTSTAGE_ID_FIT_KERNEL_START,
1010 FIT_LOAD_IGNORED, os_data, os_len);
1011 if (os_noffset < 0)
1012 return NULL;
1013
1014 images->fit_hdr_os = map_sysmem(img_addr, 0);
1015 images->fit_uname_os = fit_uname_kernel;
1016 images->fit_uname_cfg = fit_uname_config;
1017 images->fit_noffset_os = os_noffset;
1018 break;
1019#endif
1020#ifdef CONFIG_ANDROID_BOOT_IMAGE
1021 case IMAGE_FORMAT_ANDROID:
636da203
SO
1022 boot_img = buf;
1023 vendor_boot_img = NULL;
1024 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
1025 boot_img = map_sysmem(get_abootimg_addr(), 0);
1026 vendor_boot_img = map_sysmem(get_avendor_bootimg_addr(), 0);
1027 }
b6396403 1028 printf("## Booting Android Image at 0x%08lx ...\n", img_addr);
636da203 1029 if (android_image_get_kernel(boot_img, vendor_boot_img, images->verify,
b6396403
SG
1030 os_data, os_len))
1031 return NULL;
636da203
SO
1032 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
1033 unmap_sysmem(vendor_boot_img);
1034 unmap_sysmem(boot_img);
1035 }
b6396403
SG
1036 break;
1037#endif
1038 default:
1039 printf("Wrong Image Format for %s command\n", cmdtp->name);
1040 bootstage_error(BOOTSTAGE_ID_FIT_KERNEL_INFO);
1041 return NULL;
1042 }
1043
1044 debug(" kernel data at 0x%08lx, len = 0x%08lx (%ld)\n",
1045 *os_data, *os_len, *os_len);
1046
1047 return buf;
1048}
f6c6df7e
HS
1049
1050/**
1051 * switch_to_non_secure_mode() - switch to non-secure mode
1052 *
1053 * This routine is overridden by architectures requiring this feature.
1054 */
1055void __weak switch_to_non_secure_mode(void)
1056{
1057}
1058
ce1400f6
SG
1059#else /* USE_HOSTCC */
1060
93e07880 1061#if defined(CONFIG_FIT_SIGNATURE)
8a9d0373
SG
1062static int bootm_host_load_image(const void *fit, int req_image_type,
1063 int cfg_noffset)
ce1400f6
SG
1064{
1065 const char *fit_uname_config = NULL;
1066 ulong data, len;
d9d7c20b 1067 struct bootm_headers images;
ce1400f6 1068 int noffset;
c45568cc 1069 ulong load_end, buf_size;
ce1400f6 1070 uint8_t image_type;
0cd57f29 1071 uint8_t image_comp;
ce1400f6
SG
1072 void *load_buf;
1073 int ret;
1074
8a9d0373 1075 fit_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
ce1400f6
SG
1076 memset(&images, '\0', sizeof(images));
1077 images.verify = 1;
1078 noffset = fit_image_load(&images, (ulong)fit,
1079 NULL, &fit_uname_config,
1080 IH_ARCH_DEFAULT, req_image_type, -1,
1081 FIT_LOAD_IGNORED, &data, &len);
1082 if (noffset < 0)
1083 return noffset;
1084 if (fit_image_get_type(fit, noffset, &image_type)) {
1085 puts("Can't get image type!\n");
1086 return -EINVAL;
1087 }
1088
88de6c51
DG
1089 if (fit_image_get_comp(fit, noffset, &image_comp))
1090 image_comp = IH_COMP_NONE;
ce1400f6
SG
1091
1092 /* Allow the image to expand by a factor of 4, should be safe */
c45568cc
TR
1093 buf_size = (1 << 20) + len * 4;
1094 load_buf = malloc(buf_size);
0cd57f29 1095 ret = image_decomp(image_comp, 0, data, image_type, load_buf,
c45568cc 1096 (void *)data, len, buf_size, &load_end);
ce1400f6 1097 free(load_buf);
081cc197 1098
2090854c 1099 if (ret) {
0cd57f29 1100 ret = handle_decomp_error(image_comp, load_end - 0, buf_size, ret);
2090854c
JW
1101 if (ret != BOOTM_ERR_UNIMPLEMENTED)
1102 return ret;
1103 }
ce1400f6
SG
1104
1105 return 0;
1106}
1107
1108int bootm_host_load_images(const void *fit, int cfg_noffset)
1109{
1110 static uint8_t image_types[] = {
1111 IH_TYPE_KERNEL,
1112 IH_TYPE_FLATDT,
1113 IH_TYPE_RAMDISK,
1114 };
1115 int err = 0;
1116 int i;
1117
1118 for (i = 0; i < ARRAY_SIZE(image_types); i++) {
1119 int ret;
1120
8a9d0373 1121 ret = bootm_host_load_image(fit, image_types[i], cfg_noffset);
ce1400f6
SG
1122 if (!err && ret && ret != -ENOENT)
1123 err = ret;
1124 }
1125
1126 /* Return the first error we found */
1127 return err;
1128}
93e07880 1129#endif
ea51a628
SG
1130
1131#endif /* ndef USE_HOSTCC */