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