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