1 /* 2 * (C) Copyright 2000-2009 3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de. 4 * 5 * SPDX-License-Identifier: GPL-2.0+ 6 */ 7 8 #ifndef USE_HOSTCC 9 #include <common.h> 10 #include <bootstage.h> 11 #include <bzlib.h> 12 #include <errno.h> 13 #include <fdt_support.h> 14 #include <lmb.h> 15 #include <malloc.h> 16 #include <mapmem.h> 17 #include <asm/io.h> 18 #include <linux/lzo.h> 19 #include <lzma/LzmaTypes.h> 20 #include <lzma/LzmaDec.h> 21 #include <lzma/LzmaTools.h> 22 #if defined(CONFIG_CMD_USB) 23 #include <usb.h> 24 #endif 25 #else 26 #include "mkimage.h" 27 #endif 28 29 #include <command.h> 30 #include <bootm.h> 31 #include <image.h> 32 33 #ifdef USE_HOSTCC 34 #define CONFIG_SYS_BOOTM_LEN 0x4000000 35 #endif 36 37 #ifndef CONFIG_SYS_BOOTM_LEN 38 /* use 8MByte as default max gunzip size */ 39 #define CONFIG_SYS_BOOTM_LEN 0x800000 40 #endif 41 42 #define IH_INITRD_ARCH IH_ARCH_DEFAULT 43 44 #ifndef USE_HOSTCC 45 46 DECLARE_GLOBAL_DATA_PTR; 47 48 bootm_headers_t images; /* pointers to os/initrd/fdt images */ 49 50 __weak int board_do_bootm(int argc, char * const argv[]) 51 { 52 return 0; 53 } 54 55 __weak int bootm_board_start(void) 56 { 57 return 0; 58 } 59 60 static const void *boot_get_kernel(cmd_tbl_t *cmdtp, int flag, int argc, 61 char * const argv[], bootm_headers_t *images, 62 ulong *os_data, ulong *os_len); 63 64 #ifdef CONFIG_LMB 65 static void boot_start_lmb(bootm_headers_t *images) 66 { 67 68 lmb_init(&images->lmb); 69 #ifdef CONFIG_NR_DRAM_BANKS 70 int i; 71 72 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { 73 lmb_add(&images->lmb, gd->bd->bi_dram[i].start, 74 gd->bd->bi_dram[i].size); 75 } 76 #else 77 ulong mem_start; 78 phys_size_t mem_size; 79 80 mem_start = env_get_bootm_low(); 81 mem_size = env_get_bootm_size(); 82 lmb_add(&images->lmb, (phys_addr_t)mem_start, mem_size); 83 #endif 84 arch_lmb_reserve(&images->lmb); 85 board_lmb_reserve(&images->lmb); 86 } 87 #else 88 #define lmb_reserve(lmb, base, size) 89 static inline void boot_start_lmb(bootm_headers_t *images) { } 90 #endif 91 92 static int bootm_start(cmd_tbl_t *cmdtp, int flag, int argc, 93 char * const argv[]) 94 { 95 memset((void *)&images, 0, sizeof(images)); 96 images.verify = env_get_yesno("verify"); 97 98 boot_start_lmb(&images); 99 100 bootstage_mark_name(BOOTSTAGE_ID_BOOTM_START, "bootm_start"); 101 images.state = BOOTM_STATE_START; 102 103 return bootm_board_start(); 104 } 105 106 static int bootm_find_os(cmd_tbl_t *cmdtp, int flag, int argc, 107 char * const argv[]) 108 { 109 const void *os_hdr; 110 bool ep_found = false; 111 int ret; 112 113 /* get kernel image header, start address and length */ 114 os_hdr = boot_get_kernel(cmdtp, flag, argc, argv, 115 &images, &images.os.image_start, &images.os.image_len); 116 if (images.os.image_len == 0) { 117 puts("ERROR: can't get kernel image!\n"); 118 return 1; 119 } 120 121 /* get image parameters */ 122 switch (genimg_get_format(os_hdr)) { 123 #if defined(CONFIG_IMAGE_FORMAT_LEGACY) 124 case IMAGE_FORMAT_LEGACY: 125 images.os.type = image_get_type(os_hdr); 126 images.os.comp = image_get_comp(os_hdr); 127 images.os.os = image_get_os(os_hdr); 128 129 images.os.end = image_get_image_end(os_hdr); 130 images.os.load = image_get_load(os_hdr); 131 images.os.arch = image_get_arch(os_hdr); 132 break; 133 #endif 134 #if IMAGE_ENABLE_FIT 135 case IMAGE_FORMAT_FIT: 136 if (fit_image_get_type(images.fit_hdr_os, 137 images.fit_noffset_os, 138 &images.os.type)) { 139 puts("Can't get image type!\n"); 140 bootstage_error(BOOTSTAGE_ID_FIT_TYPE); 141 return 1; 142 } 143 144 if (fit_image_get_comp(images.fit_hdr_os, 145 images.fit_noffset_os, 146 &images.os.comp)) { 147 puts("Can't get image compression!\n"); 148 bootstage_error(BOOTSTAGE_ID_FIT_COMPRESSION); 149 return 1; 150 } 151 152 if (fit_image_get_os(images.fit_hdr_os, images.fit_noffset_os, 153 &images.os.os)) { 154 puts("Can't get image OS!\n"); 155 bootstage_error(BOOTSTAGE_ID_FIT_OS); 156 return 1; 157 } 158 159 if (fit_image_get_arch(images.fit_hdr_os, 160 images.fit_noffset_os, 161 &images.os.arch)) { 162 puts("Can't get image ARCH!\n"); 163 return 1; 164 } 165 166 images.os.end = fit_get_end(images.fit_hdr_os); 167 168 if (fit_image_get_load(images.fit_hdr_os, images.fit_noffset_os, 169 &images.os.load)) { 170 puts("Can't get image load address!\n"); 171 bootstage_error(BOOTSTAGE_ID_FIT_LOADADDR); 172 return 1; 173 } 174 break; 175 #endif 176 #ifdef CONFIG_ANDROID_BOOT_IMAGE 177 case IMAGE_FORMAT_ANDROID: 178 images.os.type = IH_TYPE_KERNEL; 179 images.os.comp = android_image_get_comp(os_hdr); 180 images.os.os = IH_OS_LINUX; 181 182 images.os.end = android_image_get_end(os_hdr); 183 images.os.load = android_image_get_kload(os_hdr); 184 images.ep = images.os.load; 185 ep_found = true; 186 break; 187 #endif 188 default: 189 puts("ERROR: unknown image format type!\n"); 190 return 1; 191 } 192 193 /* If we have a valid setup.bin, we will use that for entry (x86) */ 194 if (images.os.arch == IH_ARCH_I386 || 195 images.os.arch == IH_ARCH_X86_64) { 196 ulong len; 197 198 ret = boot_get_setup(&images, IH_ARCH_I386, &images.ep, &len); 199 if (ret < 0 && ret != -ENOENT) { 200 puts("Could not find a valid setup.bin for x86\n"); 201 return 1; 202 } 203 /* Kernel entry point is the setup.bin */ 204 } else if (images.legacy_hdr_valid) { 205 images.ep = image_get_ep(&images.legacy_hdr_os_copy); 206 #if IMAGE_ENABLE_FIT 207 } else if (images.fit_uname_os) { 208 int ret; 209 210 ret = fit_image_get_entry(images.fit_hdr_os, 211 images.fit_noffset_os, &images.ep); 212 if (ret) { 213 puts("Can't get entry point property!\n"); 214 return 1; 215 } 216 #endif 217 } else if (!ep_found) { 218 puts("Could not find kernel entry point!\n"); 219 return 1; 220 } 221 222 if (images.os.type == IH_TYPE_KERNEL_NOLOAD) { 223 images.os.load = images.os.image_start; 224 images.ep += images.os.load; 225 } 226 227 images.os.start = map_to_sysmem(os_hdr); 228 229 return 0; 230 } 231 232 /** 233 * bootm_find_images - wrapper to find and locate various images 234 * @flag: Ignored Argument 235 * @argc: command argument count 236 * @argv: command argument list 237 * 238 * boot_find_images() will attempt to load an available ramdisk, 239 * flattened device tree, as well as specifically marked 240 * "loadable" images (loadables are FIT only) 241 * 242 * Note: bootm_find_images will skip an image if it is not found 243 * 244 * @return: 245 * 0, if all existing images were loaded correctly 246 * 1, if an image is found but corrupted, or invalid 247 */ 248 int bootm_find_images(int flag, int argc, char * const argv[]) 249 { 250 int ret; 251 252 /* find ramdisk */ 253 ret = boot_get_ramdisk(argc, argv, &images, IH_INITRD_ARCH, 254 &images.rd_start, &images.rd_end); 255 if (ret) { 256 puts("Ramdisk image is corrupt or invalid\n"); 257 return 1; 258 } 259 260 #if IMAGE_ENABLE_OF_LIBFDT 261 /* find flattened device tree */ 262 ret = boot_get_fdt(flag, argc, argv, IH_ARCH_DEFAULT, &images, 263 &images.ft_addr, &images.ft_len); 264 if (ret) { 265 puts("Could not find a valid device tree\n"); 266 return 1; 267 } 268 set_working_fdt_addr((ulong)images.ft_addr); 269 lmb_reserve(&images.lmb, (ulong)images.ft_addr, (ulong)images.ft_len); 270 #endif 271 272 #if IMAGE_ENABLE_FIT 273 #if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_XILINX) 274 /* find bitstreams */ 275 ret = boot_get_fpga(argc, argv, &images, IH_ARCH_DEFAULT, 276 NULL, NULL); 277 if (ret) { 278 printf("FPGA image is corrupted or invalid\n"); 279 return 1; 280 } 281 #endif 282 283 /* find all of the loadables */ 284 ret = boot_get_loadable(argc, argv, &images, IH_ARCH_DEFAULT, 285 NULL, NULL); 286 if (ret) { 287 printf("Loadable(s) is corrupt or invalid\n"); 288 return 1; 289 } 290 #endif 291 292 return 0; 293 } 294 295 static int bootm_find_other(cmd_tbl_t *cmdtp, int flag, int argc, 296 char * const argv[]) 297 { 298 if (((images.os.type == IH_TYPE_KERNEL) || 299 (images.os.type == IH_TYPE_KERNEL_NOLOAD) || 300 (images.os.type == IH_TYPE_MULTI)) && 301 (images.os.os == IH_OS_LINUX || 302 images.os.os == IH_OS_VXWORKS)) 303 return bootm_find_images(flag, argc, argv); 304 305 return 0; 306 } 307 #endif /* USE_HOSTC */ 308 309 /** 310 * print_decomp_msg() - Print a suitable decompression/loading message 311 * 312 * @type: OS type (IH_OS_...) 313 * @comp_type: Compression type being used (IH_COMP_...) 314 * @is_xip: true if the load address matches the image start 315 */ 316 static void print_decomp_msg(int comp_type, int type, bool is_xip, 317 ulong src, ulong dst) 318 { 319 const char *name = genimg_get_type_name(type); 320 const char *comp_name[] = { 321 [IH_COMP_NONE] = "", 322 [IH_COMP_GZIP] = "GZIP", 323 [IH_COMP_BZIP2] = "BZIP2", 324 [IH_COMP_LZMA] = "LZMA", 325 [IH_COMP_LZO] = "LZO", 326 [IH_COMP_LZ4] = "LZ4", 327 [IH_COMP_ZIMAGE]= "ZIMAGE", 328 }; 329 330 if (comp_type == IH_COMP_NONE) 331 printf(" %s %s from 0x%08lx to 0x%08lx ... ", 332 is_xip ? "XIP" : "Loading", name, src, dst); 333 else 334 printf(" Uncompressing %s %s from 0x%08lx to 0x%08lx ... ", 335 comp_name[comp_type], name, src, dst); 336 } 337 338 /** 339 * handle_decomp_error() - display a decompression error 340 * 341 * This function tries to produce a useful message. In the case where the 342 * uncompressed size is the same as the available space, we can assume that 343 * the image is too large for the buffer. 344 * 345 * @comp_type: Compression type being used (IH_COMP_...) 346 * @uncomp_size: Number of bytes uncompressed 347 * @unc_len: Amount of space available for decompression 348 * @ret: Error code to report 349 * @return BOOTM_ERR_RESET, indicating that the board must be reset 350 */ 351 static int handle_decomp_error(int comp_type, size_t uncomp_size, 352 size_t unc_len, int ret) 353 { 354 const char *name = genimg_get_comp_name(comp_type); 355 356 if (uncomp_size >= unc_len) 357 printf("Image too large(0x%lx >= 0x%lx): increase CONFIG_SYS_BOOTM_LEN\n", 358 (ulong)uncomp_size, (ulong)unc_len); 359 else 360 printf("%s: uncompress error %d\n", name, ret); 361 362 /* 363 * The decompression routines are now safe, so will not write beyond 364 * their bounds. Probably it is not necessary to reset, but maintain 365 * the current behaviour for now. 366 */ 367 printf("Must RESET board to recover\n"); 368 #ifndef USE_HOSTCC 369 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE); 370 #endif 371 372 return BOOTM_ERR_RESET; 373 } 374 375 int bootm_parse_comp(const unsigned char *hdr) 376 { 377 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64) 378 ulong start, end; 379 380 if (!bootz_setup((ulong)hdr, &start, &end)) 381 return IH_COMP_ZIMAGE; 382 #endif 383 #if defined(CONFIG_LZ4) 384 if (lz4_is_valid_header(hdr)) 385 return IH_COMP_LZ4; 386 #endif 387 #if defined(CONFIG_LZO) 388 if (lzop_is_valid_header(hdr)) 389 return IH_COMP_LZO; 390 #endif 391 #if defined(CONFIG_GZIP) 392 if (gzip_parse_header(hdr, 0xffff) > 0) 393 return IH_COMP_GZIP; 394 #endif 395 #if defined(CONFIG_BZIP2) 396 if ((hdr[0] == 'B') && (hdr[1] == 'Z') && (hdr[2] == 'h')) 397 return IH_COMP_BZIP2; 398 #endif 399 return IH_COMP_NONE; 400 } 401 402 int bootm_decomp_image(int comp, ulong load, ulong image_start, int type, 403 void *load_buf, void *image_buf, ulong image_len, 404 uint unc_len, ulong *load_end) 405 { 406 int ret = 0; 407 408 *load_end = load; 409 print_decomp_msg(comp, type, load == image_start, 410 (ulong)image_buf, (ulong)load_buf); 411 412 /* 413 * Load the image to the right place, decompressing if needed. After 414 * this, image_len will be set to the number of uncompressed bytes 415 * loaded, ret will be non-zero on error. 416 */ 417 switch (comp) { 418 case IH_COMP_NONE: 419 if (load == image_start) 420 break; 421 if (image_len <= unc_len) 422 memmove_wd(load_buf, image_buf, image_len, CHUNKSZ); 423 else 424 ret = 1; 425 break; 426 #ifdef CONFIG_GZIP 427 case IH_COMP_GZIP: { 428 ret = gunzip(load_buf, unc_len, image_buf, &image_len); 429 break; 430 } 431 #endif /* CONFIG_GZIP */ 432 #ifdef CONFIG_BZIP2 433 case IH_COMP_BZIP2: { 434 uint size = unc_len; 435 436 /* 437 * If we've got less than 4 MB of malloc() space, 438 * use slower decompression algorithm which requires 439 * at most 2300 KB of memory. 440 */ 441 ret = BZ2_bzBuffToBuffDecompress(load_buf, &size, 442 image_buf, image_len, 443 CONFIG_SYS_MALLOC_LEN < (4096 * 1024), 0); 444 image_len = size; 445 break; 446 } 447 #endif /* CONFIG_BZIP2 */ 448 #ifdef CONFIG_LZMA 449 case IH_COMP_LZMA: { 450 SizeT lzma_len = unc_len; 451 452 ret = lzmaBuffToBuffDecompress(load_buf, &lzma_len, 453 image_buf, image_len); 454 image_len = lzma_len; 455 break; 456 } 457 #endif /* CONFIG_LZMA */ 458 #ifdef CONFIG_LZO 459 case IH_COMP_LZO: { 460 size_t size = unc_len; 461 462 ret = lzop_decompress(image_buf, image_len, load_buf, &size); 463 image_len = size; 464 break; 465 } 466 #endif /* CONFIG_LZO */ 467 #ifdef CONFIG_LZ4 468 case IH_COMP_LZ4: { 469 size_t size = unc_len; 470 471 ret = ulz4fn(image_buf, image_len, load_buf, &size); 472 image_len = size; 473 break; 474 } 475 #endif /* CONFIG_LZ4 */ 476 default: 477 printf("Unimplemented compression type %d\n", comp); 478 return BOOTM_ERR_UNIMPLEMENTED; 479 } 480 481 if (ret) 482 return handle_decomp_error(comp, image_len, unc_len, ret); 483 *load_end = load + image_len; 484 485 if (comp == IH_COMP_NONE || comp == IH_COMP_ZIMAGE) 486 puts("OK\n"); 487 else 488 printf("with %08lx bytes OK\n", image_len); 489 490 return 0; 491 } 492 493 #ifndef USE_HOSTCC 494 static int bootm_load_os(bootm_headers_t *images, unsigned long *load_end, 495 int boot_progress) 496 { 497 image_info_t os = images->os; 498 ulong load = os.load; 499 ulong blob_start = os.start; 500 ulong blob_end = os.end; 501 ulong image_start = os.image_start; 502 ulong image_len = os.image_len; 503 bool no_overlap; 504 void *load_buf, *image_buf; 505 int err; 506 507 load_buf = map_sysmem(load, 0); 508 image_buf = map_sysmem(os.image_start, image_len); 509 err = bootm_decomp_image(os.comp, load, os.image_start, os.type, 510 load_buf, image_buf, image_len, 511 CONFIG_SYS_BOOTM_LEN, load_end); 512 if (err) { 513 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE); 514 return err; 515 } 516 flush_cache(load, ALIGN(*load_end - load, ARCH_DMA_MINALIGN)); 517 518 debug(" kernel loaded at 0x%08lx, end = 0x%08lx\n", load, *load_end); 519 bootstage_mark(BOOTSTAGE_ID_KERNEL_LOADED); 520 521 no_overlap = (os.comp == IH_COMP_NONE && load == image_start); 522 523 if (!no_overlap && (load < blob_end) && (*load_end > blob_start)) { 524 debug("images.os.start = 0x%lX, images.os.end = 0x%lx\n", 525 blob_start, blob_end); 526 debug("images.os.load = 0x%lx, load_end = 0x%lx\n", load, 527 *load_end); 528 529 /* Check what type of image this is. */ 530 if (images->legacy_hdr_valid) { 531 if (image_get_type(&images->legacy_hdr_os_copy) 532 == IH_TYPE_MULTI) 533 puts("WARNING: legacy format multi component image overwritten\n"); 534 return BOOTM_ERR_OVERLAP; 535 } else { 536 puts("ERROR: new format image overwritten - must RESET the board to recover\n"); 537 bootstage_error(BOOTSTAGE_ID_OVERWRITTEN); 538 return BOOTM_ERR_RESET; 539 } 540 } 541 542 return 0; 543 } 544 545 /** 546 * bootm_disable_interrupts() - Disable interrupts in preparation for load/boot 547 * 548 * @return interrupt flag (0 if interrupts were disabled, non-zero if they were 549 * enabled) 550 */ 551 ulong bootm_disable_interrupts(void) 552 { 553 ulong iflag; 554 555 /* 556 * Do not go further if usb is boot device, 557 * We may access usb at late sequence. 558 */ 559 if (!strcmp(env_get("devtype"), "usb")) 560 return 0; 561 562 /* 563 * We have reached the point of no return: we are going to 564 * overwrite all exception vector code, so we cannot easily 565 * recover from any failures any more... 566 */ 567 iflag = disable_interrupts(); 568 #ifdef CONFIG_NETCONSOLE 569 /* Stop the ethernet stack if NetConsole could have left it up */ 570 eth_halt(); 571 # ifndef CONFIG_DM_ETH 572 eth_unregister(eth_get_dev()); 573 # endif 574 #endif 575 576 #if defined(CONFIG_CMD_USB) 577 /* 578 * turn off USB to prevent the host controller from writing to the 579 * SDRAM while Linux is booting. This could happen (at least for OHCI 580 * controller), because the HCCA (Host Controller Communication Area) 581 * lies within the SDRAM and the host controller writes continously to 582 * this area (as busmaster!). The HccaFrameNumber is for example 583 * updated every 1 ms within the HCCA structure in SDRAM! For more 584 * details see the OpenHCI specification. 585 */ 586 usb_stop(); 587 #endif 588 return iflag; 589 } 590 591 #if defined(CONFIG_SILENT_CONSOLE) && !defined(CONFIG_SILENT_U_BOOT_ONLY) 592 593 #define CONSOLE_ARG "console=" 594 #define CONSOLE_ARG_LEN (sizeof(CONSOLE_ARG) - 1) 595 596 static void fixup_silent_linux(void) 597 { 598 char *buf; 599 const char *env_val; 600 char *cmdline = env_get("bootargs"); 601 int want_silent; 602 603 /* 604 * Only fix cmdline when requested. The environment variable can be: 605 * 606 * no - we never fixup 607 * yes - we always fixup 608 * unset - we rely on the console silent flag 609 */ 610 want_silent = env_get_yesno("silent_linux"); 611 if (want_silent == 0) 612 return; 613 else if (want_silent == -1 && !(gd->flags & GD_FLG_SILENT)) 614 return; 615 616 debug("before silent fix-up: %s\n", cmdline); 617 if (cmdline && (cmdline[0] != '\0')) { 618 char *start = strstr(cmdline, CONSOLE_ARG); 619 620 /* Allocate space for maximum possible new command line */ 621 buf = malloc(strlen(cmdline) + 1 + CONSOLE_ARG_LEN + 1); 622 if (!buf) { 623 debug("%s: out of memory\n", __func__); 624 return; 625 } 626 627 if (start) { 628 char *end = strchr(start, ' '); 629 int num_start_bytes = start - cmdline + CONSOLE_ARG_LEN; 630 631 strncpy(buf, cmdline, num_start_bytes); 632 if (end) 633 strcpy(buf + num_start_bytes, end); 634 else 635 buf[num_start_bytes] = '\0'; 636 } else { 637 sprintf(buf, "%s %s", cmdline, CONSOLE_ARG); 638 } 639 env_val = buf; 640 } else { 641 buf = NULL; 642 env_val = CONSOLE_ARG; 643 } 644 645 env_set("bootargs", env_val); 646 debug("after silent fix-up: %s\n", env_val); 647 free(buf); 648 } 649 #endif /* CONFIG_SILENT_CONSOLE */ 650 651 /** 652 * Execute selected states of the bootm command. 653 * 654 * Note the arguments to this state must be the first argument, Any 'bootm' 655 * or sub-command arguments must have already been taken. 656 * 657 * Note that if states contains more than one flag it MUST contain 658 * BOOTM_STATE_START, since this handles and consumes the command line args. 659 * 660 * Also note that aside from boot_os_fn functions and bootm_load_os no other 661 * functions we store the return value of in 'ret' may use a negative return 662 * value, without special handling. 663 * 664 * @param cmdtp Pointer to bootm command table entry 665 * @param flag Command flags (CMD_FLAG_...) 666 * @param argc Number of subcommand arguments (0 = no arguments) 667 * @param argv Arguments 668 * @param states Mask containing states to run (BOOTM_STATE_...) 669 * @param images Image header information 670 * @param boot_progress 1 to show boot progress, 0 to not do this 671 * @return 0 if ok, something else on error. Some errors will cause this 672 * function to perform a reboot! If states contains BOOTM_STATE_OS_GO 673 * then the intent is to boot an OS, so this function will not return 674 * unless the image type is standalone. 675 */ 676 int do_bootm_states(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], 677 int states, bootm_headers_t *images, int boot_progress) 678 { 679 boot_os_fn *boot_fn; 680 ulong iflag = 0; 681 int ret = 0, need_boot_fn; 682 683 images->state |= states; 684 685 /* 686 * Work through the states and see how far we get. We stop on 687 * any error. 688 */ 689 if (states & BOOTM_STATE_START) 690 ret = bootm_start(cmdtp, flag, argc, argv); 691 692 if (!ret && (states & BOOTM_STATE_FINDOS)) 693 ret = bootm_find_os(cmdtp, flag, argc, argv); 694 695 if (!ret && (states & BOOTM_STATE_FINDOTHER)) 696 ret = bootm_find_other(cmdtp, flag, argc, argv); 697 698 /* Load the OS */ 699 if (!ret && (states & BOOTM_STATE_LOADOS)) { 700 ulong load_end; 701 702 iflag = bootm_disable_interrupts(); 703 ret = bootm_load_os(images, &load_end, 0); 704 if (ret == 0) 705 lmb_reserve(&images->lmb, images->os.load, 706 (load_end - images->os.load)); 707 else if (ret && ret != BOOTM_ERR_OVERLAP) 708 goto err; 709 else if (ret == BOOTM_ERR_OVERLAP) 710 ret = 0; 711 } 712 713 /* Resever memory before any lmb_alloc, as early as possible */ 714 #if IMAGE_ENABLE_OF_LIBFDT && defined(CONFIG_LMB) 715 if (!ret && ((states & BOOTM_STATE_RAMDISK) || 716 (states & BOOTM_STATE_FDT))) 717 boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr); 718 #endif 719 /* Relocate the ramdisk */ 720 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH 721 if (!ret && (states & BOOTM_STATE_RAMDISK)) { 722 ulong rd_len = images->rd_end - images->rd_start; 723 724 ret = boot_ramdisk_high(&images->lmb, images->rd_start, 725 rd_len, &images->initrd_start, &images->initrd_end); 726 if (!ret) { 727 env_set_hex("initrd_start", images->initrd_start); 728 env_set_hex("initrd_end", images->initrd_end); 729 } 730 } 731 #endif 732 #if IMAGE_ENABLE_OF_LIBFDT && defined(CONFIG_LMB) 733 if (!ret && (states & BOOTM_STATE_FDT)) { 734 ret = boot_relocate_fdt(&images->lmb, &images->ft_addr, 735 &images->ft_len); 736 } 737 #endif 738 739 /* From now on, we need the OS boot function */ 740 if (ret) 741 return ret; 742 boot_fn = bootm_os_get_boot_func(images->os.os); 743 need_boot_fn = states & (BOOTM_STATE_OS_CMDLINE | 744 BOOTM_STATE_OS_BD_T | BOOTM_STATE_OS_PREP | 745 BOOTM_STATE_OS_FAKE_GO | BOOTM_STATE_OS_GO); 746 if (boot_fn == NULL && need_boot_fn) { 747 if (iflag) 748 enable_interrupts(); 749 printf("ERROR: booting os '%s' (%d) is not supported\n", 750 genimg_get_os_name(images->os.os), images->os.os); 751 bootstage_error(BOOTSTAGE_ID_CHECK_BOOT_OS); 752 return 1; 753 } 754 755 756 /* Call various other states that are not generally used */ 757 if (!ret && (states & BOOTM_STATE_OS_CMDLINE)) 758 ret = boot_fn(BOOTM_STATE_OS_CMDLINE, argc, argv, images); 759 if (!ret && (states & BOOTM_STATE_OS_BD_T)) 760 ret = boot_fn(BOOTM_STATE_OS_BD_T, argc, argv, images); 761 if (!ret && (states & BOOTM_STATE_OS_PREP)) { 762 #if defined(CONFIG_SILENT_CONSOLE) && !defined(CONFIG_SILENT_U_BOOT_ONLY) 763 if (images->os.os == IH_OS_LINUX) 764 fixup_silent_linux(); 765 #endif 766 arch_preboot_os(BOOTM_STATE_OS_PREP); 767 768 ret = boot_fn(BOOTM_STATE_OS_PREP, argc, argv, images); 769 } 770 771 #ifdef CONFIG_TRACE 772 /* Pretend to run the OS, then run a user command */ 773 if (!ret && (states & BOOTM_STATE_OS_FAKE_GO)) { 774 char *cmd_list = env_get("fakegocmd"); 775 776 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_FAKE_GO, 777 images, boot_fn); 778 if (!ret && cmd_list) 779 ret = run_command_list(cmd_list, -1, flag); 780 } 781 #endif 782 783 /* Check for unsupported subcommand. */ 784 if (ret) { 785 puts("subcommand not supported\n"); 786 return ret; 787 } 788 789 /* Now run the OS! We hope this doesn't return */ 790 if (!ret && (states & BOOTM_STATE_OS_GO)) 791 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_GO, 792 images, boot_fn); 793 794 /* Deal with any fallout */ 795 err: 796 if (iflag) 797 enable_interrupts(); 798 799 if (ret == BOOTM_ERR_UNIMPLEMENTED) 800 bootstage_error(BOOTSTAGE_ID_DECOMP_UNIMPL); 801 else if (ret == BOOTM_ERR_RESET) 802 do_reset(cmdtp, flag, argc, argv); 803 804 return ret; 805 } 806 807 #if defined(CONFIG_IMAGE_FORMAT_LEGACY) 808 /** 809 * image_get_kernel - verify legacy format kernel image 810 * @img_addr: in RAM address of the legacy format image to be verified 811 * @verify: data CRC verification flag 812 * 813 * image_get_kernel() verifies legacy image integrity and returns pointer to 814 * legacy image header if image verification was completed successfully. 815 * 816 * returns: 817 * pointer to a legacy image header if valid image was found 818 * otherwise return NULL 819 */ 820 static image_header_t *image_get_kernel(ulong img_addr, int verify) 821 { 822 image_header_t *hdr = (image_header_t *)img_addr; 823 824 if (!image_check_magic(hdr)) { 825 puts("Bad Magic Number\n"); 826 bootstage_error(BOOTSTAGE_ID_CHECK_MAGIC); 827 return NULL; 828 } 829 bootstage_mark(BOOTSTAGE_ID_CHECK_HEADER); 830 831 if (!image_check_hcrc(hdr)) { 832 puts("Bad Header Checksum\n"); 833 bootstage_error(BOOTSTAGE_ID_CHECK_HEADER); 834 return NULL; 835 } 836 837 bootstage_mark(BOOTSTAGE_ID_CHECK_CHECKSUM); 838 image_print_contents(hdr); 839 840 if (verify) { 841 puts(" Verifying Checksum ... "); 842 if (!image_check_dcrc(hdr)) { 843 printf("Bad Data CRC\n"); 844 bootstage_error(BOOTSTAGE_ID_CHECK_CHECKSUM); 845 return NULL; 846 } 847 puts("OK\n"); 848 } 849 bootstage_mark(BOOTSTAGE_ID_CHECK_ARCH); 850 851 if (!image_check_target_arch(hdr)) { 852 printf("Unsupported Architecture 0x%x\n", image_get_arch(hdr)); 853 bootstage_error(BOOTSTAGE_ID_CHECK_ARCH); 854 return NULL; 855 } 856 return hdr; 857 } 858 #endif 859 860 /** 861 * boot_get_kernel - find kernel image 862 * @os_data: pointer to a ulong variable, will hold os data start address 863 * @os_len: pointer to a ulong variable, will hold os data length 864 * 865 * boot_get_kernel() tries to find a kernel image, verifies its integrity 866 * and locates kernel data. 867 * 868 * returns: 869 * pointer to image header if valid image was found, plus kernel start 870 * address and length, otherwise NULL 871 */ 872 static const void *boot_get_kernel(cmd_tbl_t *cmdtp, int flag, int argc, 873 char * const argv[], bootm_headers_t *images, 874 ulong *os_data, ulong *os_len) 875 { 876 #if defined(CONFIG_IMAGE_FORMAT_LEGACY) 877 image_header_t *hdr; 878 #endif 879 ulong img_addr; 880 const void *buf; 881 const char *fit_uname_config = NULL; 882 const char *fit_uname_kernel = NULL; 883 #if IMAGE_ENABLE_FIT 884 int os_noffset; 885 #endif 886 887 img_addr = genimg_get_kernel_addr_fit(argc < 1 ? NULL : argv[0], 888 &fit_uname_config, 889 &fit_uname_kernel); 890 891 bootstage_mark(BOOTSTAGE_ID_CHECK_MAGIC); 892 893 /* check image type, for FIT images get FIT kernel node */ 894 *os_data = *os_len = 0; 895 buf = map_sysmem(img_addr, 0); 896 switch (genimg_get_format(buf)) { 897 #if defined(CONFIG_IMAGE_FORMAT_LEGACY) 898 case IMAGE_FORMAT_LEGACY: 899 printf("## Booting kernel from Legacy Image at %08lx ...\n", 900 img_addr); 901 hdr = image_get_kernel(img_addr, images->verify); 902 if (!hdr) 903 return NULL; 904 bootstage_mark(BOOTSTAGE_ID_CHECK_IMAGETYPE); 905 906 /* get os_data and os_len */ 907 switch (image_get_type(hdr)) { 908 case IH_TYPE_KERNEL: 909 case IH_TYPE_KERNEL_NOLOAD: 910 *os_data = image_get_data(hdr); 911 *os_len = image_get_data_size(hdr); 912 break; 913 case IH_TYPE_MULTI: 914 image_multi_getimg(hdr, 0, os_data, os_len); 915 break; 916 case IH_TYPE_STANDALONE: 917 *os_data = image_get_data(hdr); 918 *os_len = image_get_data_size(hdr); 919 break; 920 default: 921 if (cmdtp) 922 printf("Wrong Image Type for %s command\n", 923 cmdtp->name); 924 bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE); 925 return NULL; 926 } 927 928 /* 929 * copy image header to allow for image overwrites during 930 * kernel decompression. 931 */ 932 memmove(&images->legacy_hdr_os_copy, hdr, 933 sizeof(image_header_t)); 934 935 /* save pointer to image header */ 936 images->legacy_hdr_os = hdr; 937 938 images->legacy_hdr_valid = 1; 939 bootstage_mark(BOOTSTAGE_ID_DECOMP_IMAGE); 940 break; 941 #endif 942 #if IMAGE_ENABLE_FIT 943 case IMAGE_FORMAT_FIT: 944 os_noffset = fit_image_load(images, img_addr, 945 &fit_uname_kernel, &fit_uname_config, 946 IH_ARCH_DEFAULT, IH_TYPE_KERNEL, 947 BOOTSTAGE_ID_FIT_KERNEL_START, 948 FIT_LOAD_IGNORED, os_data, os_len); 949 if (os_noffset < 0) 950 return NULL; 951 952 images->fit_hdr_os = map_sysmem(img_addr, 0); 953 images->fit_uname_os = fit_uname_kernel; 954 images->fit_uname_cfg = fit_uname_config; 955 images->fit_noffset_os = os_noffset; 956 break; 957 #endif 958 #ifdef CONFIG_ANDROID_BOOT_IMAGE 959 case IMAGE_FORMAT_ANDROID: 960 printf("## Booting Android Image at 0x%08lx ...\n", img_addr); 961 if (android_image_get_kernel(buf, images->verify, 962 os_data, os_len)) 963 return NULL; 964 break; 965 #endif 966 default: 967 if (cmdtp) 968 printf("Wrong Image Format for %s command\n", 969 cmdtp->name); 970 bootstage_error(BOOTSTAGE_ID_FIT_KERNEL_INFO); 971 return NULL; 972 } 973 974 debug(" kernel data at 0x%08lx, len = 0x%08lx (%ld)\n", 975 *os_data, *os_len, *os_len); 976 977 return buf; 978 } 979 #else /* USE_HOSTCC */ 980 981 void memmove_wd(void *to, void *from, size_t len, ulong chunksz) 982 { 983 memmove(to, from, len); 984 } 985 986 static int bootm_host_load_image(const void *fit, int req_image_type, int index) 987 { 988 const char *fit_uname_config = NULL; 989 ulong data, len; 990 bootm_headers_t images; 991 int noffset; 992 ulong load_end; 993 uint8_t image_type; 994 uint8_t imape_comp; 995 void *load_buf; 996 int ret; 997 998 memset(&images, '\0', sizeof(images)); 999 images.verify = 1; 1000 noffset = fit_image_load_index(&images, (ulong)fit, 1001 NULL, &fit_uname_config, 1002 IH_ARCH_DEFAULT, req_image_type, index, -1, 1003 FIT_LOAD_IGNORED, &data, &len); 1004 if (noffset < 0) 1005 return noffset; 1006 if (fit_image_get_type(fit, noffset, &image_type)) { 1007 puts("Can't get image type!\n"); 1008 return -EINVAL; 1009 } 1010 1011 if (fit_image_get_comp(fit, noffset, &imape_comp)) { 1012 puts("Can't get image compression!\n"); 1013 return -EINVAL; 1014 } 1015 1016 /* Allow the image to expand by a factor of 4, should be safe */ 1017 load_buf = malloc((1 << 20) + len * 4); 1018 ret = bootm_decomp_image(imape_comp, 0, data, image_type, load_buf, 1019 (void *)data, len, CONFIG_SYS_BOOTM_LEN, 1020 &load_end); 1021 free(load_buf); 1022 1023 if (ret && ret != BOOTM_ERR_UNIMPLEMENTED) 1024 return ret; 1025 1026 return 0; 1027 } 1028 1029 int bootm_host_load_images(const void *fit, int cfg_noffset, int is_spl) 1030 { 1031 static uint8_t image_types[] = { 1032 IH_TYPE_KERNEL, 1033 IH_TYPE_FLATDT, 1034 IH_TYPE_RAMDISK, 1035 }; 1036 #ifdef CONFIG_SPL_ATF 1037 static uint8_t image_types_spl[] = { 1038 IH_TYPE_FLATDT, 1039 IH_TYPE_FIRMWARE, 1040 IH_TYPE_LOADABLE, 1041 IH_TYPE_LOADABLE, 1042 IH_TYPE_LOADABLE, 1043 }; 1044 #else 1045 static uint8_t image_types_spl[] = { 1046 IH_TYPE_FLATDT, 1047 IH_TYPE_FIRMWARE, 1048 IH_TYPE_LOADABLE, 1049 }; 1050 #endif 1051 int loadable_index = 0; 1052 int err = 0; 1053 int index; 1054 int i; 1055 1056 for (i = 0; !is_spl && i < ARRAY_SIZE(image_types); i++) { 1057 int ret; 1058 1059 ret = bootm_host_load_image(fit, image_types[i], 0); 1060 if (!err && ret && ret != -ENOENT) 1061 err = ret; 1062 } 1063 1064 for (i = 0; is_spl && i < ARRAY_SIZE(image_types_spl); i++) { 1065 int ret; 1066 1067 if (image_types_spl[i] == IH_TYPE_LOADABLE) 1068 index = loadable_index++; 1069 else 1070 index = 0; 1071 1072 ret = bootm_host_load_image(fit, image_types_spl[i], index); 1073 if (!err && ret && ret != -ENOENT) 1074 err = ret; 1075 } 1076 1077 /* Return the first error we found */ 1078 return err; 1079 } 1080 1081 #endif /* ndef USE_HOSTCC */ 1082