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