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 * Do not go further if usb is boot device, 526 * We may access usb at late sequence. 527 */ 528 if (!strcmp(env_get("devtype"), "usb")) 529 return 0; 530 531 /* 532 * We have reached the point of no return: we are going to 533 * overwrite all exception vector code, so we cannot easily 534 * recover from any failures any more... 535 */ 536 iflag = disable_interrupts(); 537 #ifdef CONFIG_NETCONSOLE 538 /* Stop the ethernet stack if NetConsole could have left it up */ 539 eth_halt(); 540 # ifndef CONFIG_DM_ETH 541 eth_unregister(eth_get_dev()); 542 # endif 543 #endif 544 545 #if defined(CONFIG_CMD_USB) 546 /* 547 * turn off USB to prevent the host controller from writing to the 548 * SDRAM while Linux is booting. This could happen (at least for OHCI 549 * controller), because the HCCA (Host Controller Communication Area) 550 * lies within the SDRAM and the host controller writes continously to 551 * this area (as busmaster!). The HccaFrameNumber is for example 552 * updated every 1 ms within the HCCA structure in SDRAM! For more 553 * details see the OpenHCI specification. 554 */ 555 usb_stop(); 556 #endif 557 return iflag; 558 } 559 560 #if defined(CONFIG_SILENT_CONSOLE) && !defined(CONFIG_SILENT_U_BOOT_ONLY) 561 562 #define CONSOLE_ARG "console=" 563 #define CONSOLE_ARG_LEN (sizeof(CONSOLE_ARG) - 1) 564 565 static void fixup_silent_linux(void) 566 { 567 char *buf; 568 const char *env_val; 569 char *cmdline = env_get("bootargs"); 570 int want_silent; 571 572 /* 573 * Only fix cmdline when requested. The environment variable can be: 574 * 575 * no - we never fixup 576 * yes - we always fixup 577 * unset - we rely on the console silent flag 578 */ 579 want_silent = env_get_yesno("silent_linux"); 580 if (want_silent == 0) 581 return; 582 else if (want_silent == -1 && !(gd->flags & GD_FLG_SILENT)) 583 return; 584 585 debug("before silent fix-up: %s\n", cmdline); 586 if (cmdline && (cmdline[0] != '\0')) { 587 char *start = strstr(cmdline, CONSOLE_ARG); 588 589 /* Allocate space for maximum possible new command line */ 590 buf = malloc(strlen(cmdline) + 1 + CONSOLE_ARG_LEN + 1); 591 if (!buf) { 592 debug("%s: out of memory\n", __func__); 593 return; 594 } 595 596 if (start) { 597 char *end = strchr(start, ' '); 598 int num_start_bytes = start - cmdline + CONSOLE_ARG_LEN; 599 600 strncpy(buf, cmdline, num_start_bytes); 601 if (end) 602 strcpy(buf + num_start_bytes, end); 603 else 604 buf[num_start_bytes] = '\0'; 605 } else { 606 sprintf(buf, "%s %s", cmdline, CONSOLE_ARG); 607 } 608 env_val = buf; 609 } else { 610 buf = NULL; 611 env_val = CONSOLE_ARG; 612 } 613 614 env_set("bootargs", env_val); 615 debug("after silent fix-up: %s\n", env_val); 616 free(buf); 617 } 618 #endif /* CONFIG_SILENT_CONSOLE */ 619 620 /** 621 * Execute selected states of the bootm command. 622 * 623 * Note the arguments to this state must be the first argument, Any 'bootm' 624 * or sub-command arguments must have already been taken. 625 * 626 * Note that if states contains more than one flag it MUST contain 627 * BOOTM_STATE_START, since this handles and consumes the command line args. 628 * 629 * Also note that aside from boot_os_fn functions and bootm_load_os no other 630 * functions we store the return value of in 'ret' may use a negative return 631 * value, without special handling. 632 * 633 * @param cmdtp Pointer to bootm command table entry 634 * @param flag Command flags (CMD_FLAG_...) 635 * @param argc Number of subcommand arguments (0 = no arguments) 636 * @param argv Arguments 637 * @param states Mask containing states to run (BOOTM_STATE_...) 638 * @param images Image header information 639 * @param boot_progress 1 to show boot progress, 0 to not do this 640 * @return 0 if ok, something else on error. Some errors will cause this 641 * function to perform a reboot! If states contains BOOTM_STATE_OS_GO 642 * then the intent is to boot an OS, so this function will not return 643 * unless the image type is standalone. 644 */ 645 int do_bootm_states(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], 646 int states, bootm_headers_t *images, int boot_progress) 647 { 648 boot_os_fn *boot_fn; 649 ulong iflag = 0; 650 int ret = 0, need_boot_fn; 651 652 images->state |= states; 653 654 /* 655 * Work through the states and see how far we get. We stop on 656 * any error. 657 */ 658 if (states & BOOTM_STATE_START) 659 ret = bootm_start(cmdtp, flag, argc, argv); 660 661 if (!ret && (states & BOOTM_STATE_FINDOS)) 662 ret = bootm_find_os(cmdtp, flag, argc, argv); 663 664 if (!ret && (states & BOOTM_STATE_FINDOTHER)) 665 ret = bootm_find_other(cmdtp, flag, argc, argv); 666 667 /* Load the OS */ 668 if (!ret && (states & BOOTM_STATE_LOADOS)) { 669 ulong load_end; 670 671 iflag = bootm_disable_interrupts(); 672 ret = bootm_load_os(images, &load_end, 0); 673 if (ret == 0) 674 lmb_reserve(&images->lmb, images->os.load, 675 (load_end - images->os.load)); 676 else if (ret && ret != BOOTM_ERR_OVERLAP) 677 goto err; 678 else if (ret == BOOTM_ERR_OVERLAP) 679 ret = 0; 680 } 681 682 /* Resever memory before any lmb_alloc, as early as possible */ 683 #if IMAGE_ENABLE_OF_LIBFDT && defined(CONFIG_LMB) 684 if (!ret && ((states & BOOTM_STATE_RAMDISK) || 685 (states & BOOTM_STATE_FDT))) 686 boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr); 687 #endif 688 /* Relocate the ramdisk */ 689 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH 690 if (!ret && (states & BOOTM_STATE_RAMDISK)) { 691 ulong rd_len = images->rd_end - images->rd_start; 692 693 ret = boot_ramdisk_high(&images->lmb, images->rd_start, 694 rd_len, &images->initrd_start, &images->initrd_end); 695 if (!ret) { 696 env_set_hex("initrd_start", images->initrd_start); 697 env_set_hex("initrd_end", images->initrd_end); 698 } 699 } 700 #endif 701 #if IMAGE_ENABLE_OF_LIBFDT && defined(CONFIG_LMB) 702 if (!ret && (states & BOOTM_STATE_FDT)) { 703 ret = boot_relocate_fdt(&images->lmb, &images->ft_addr, 704 &images->ft_len); 705 } 706 #endif 707 708 /* From now on, we need the OS boot function */ 709 if (ret) 710 return ret; 711 boot_fn = bootm_os_get_boot_func(images->os.os); 712 need_boot_fn = states & (BOOTM_STATE_OS_CMDLINE | 713 BOOTM_STATE_OS_BD_T | BOOTM_STATE_OS_PREP | 714 BOOTM_STATE_OS_FAKE_GO | BOOTM_STATE_OS_GO); 715 if (boot_fn == NULL && need_boot_fn) { 716 if (iflag) 717 enable_interrupts(); 718 printf("ERROR: booting os '%s' (%d) is not supported\n", 719 genimg_get_os_name(images->os.os), images->os.os); 720 bootstage_error(BOOTSTAGE_ID_CHECK_BOOT_OS); 721 return 1; 722 } 723 724 725 /* Call various other states that are not generally used */ 726 if (!ret && (states & BOOTM_STATE_OS_CMDLINE)) 727 ret = boot_fn(BOOTM_STATE_OS_CMDLINE, argc, argv, images); 728 if (!ret && (states & BOOTM_STATE_OS_BD_T)) 729 ret = boot_fn(BOOTM_STATE_OS_BD_T, argc, argv, images); 730 if (!ret && (states & BOOTM_STATE_OS_PREP)) { 731 #if defined(CONFIG_SILENT_CONSOLE) && !defined(CONFIG_SILENT_U_BOOT_ONLY) 732 if (images->os.os == IH_OS_LINUX) 733 fixup_silent_linux(); 734 #endif 735 arch_preboot_os(BOOTM_STATE_OS_PREP); 736 737 ret = boot_fn(BOOTM_STATE_OS_PREP, argc, argv, images); 738 } 739 740 #ifdef CONFIG_TRACE 741 /* Pretend to run the OS, then run a user command */ 742 if (!ret && (states & BOOTM_STATE_OS_FAKE_GO)) { 743 char *cmd_list = env_get("fakegocmd"); 744 745 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_FAKE_GO, 746 images, boot_fn); 747 if (!ret && cmd_list) 748 ret = run_command_list(cmd_list, -1, flag); 749 } 750 #endif 751 752 /* Check for unsupported subcommand. */ 753 if (ret) { 754 puts("subcommand not supported\n"); 755 return ret; 756 } 757 758 /* Now run the OS! We hope this doesn't return */ 759 if (!ret && (states & BOOTM_STATE_OS_GO)) 760 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_GO, 761 images, boot_fn); 762 763 /* Deal with any fallout */ 764 err: 765 if (iflag) 766 enable_interrupts(); 767 768 if (ret == BOOTM_ERR_UNIMPLEMENTED) 769 bootstage_error(BOOTSTAGE_ID_DECOMP_UNIMPL); 770 else if (ret == BOOTM_ERR_RESET) 771 do_reset(cmdtp, flag, argc, argv); 772 773 return ret; 774 } 775 776 #if defined(CONFIG_IMAGE_FORMAT_LEGACY) 777 /** 778 * image_get_kernel - verify legacy format kernel image 779 * @img_addr: in RAM address of the legacy format image to be verified 780 * @verify: data CRC verification flag 781 * 782 * image_get_kernel() verifies legacy image integrity and returns pointer to 783 * legacy image header if image verification was completed successfully. 784 * 785 * returns: 786 * pointer to a legacy image header if valid image was found 787 * otherwise return NULL 788 */ 789 static image_header_t *image_get_kernel(ulong img_addr, int verify) 790 { 791 image_header_t *hdr = (image_header_t *)img_addr; 792 793 if (!image_check_magic(hdr)) { 794 puts("Bad Magic Number\n"); 795 bootstage_error(BOOTSTAGE_ID_CHECK_MAGIC); 796 return NULL; 797 } 798 bootstage_mark(BOOTSTAGE_ID_CHECK_HEADER); 799 800 if (!image_check_hcrc(hdr)) { 801 puts("Bad Header Checksum\n"); 802 bootstage_error(BOOTSTAGE_ID_CHECK_HEADER); 803 return NULL; 804 } 805 806 bootstage_mark(BOOTSTAGE_ID_CHECK_CHECKSUM); 807 image_print_contents(hdr); 808 809 if (verify) { 810 puts(" Verifying Checksum ... "); 811 if (!image_check_dcrc(hdr)) { 812 printf("Bad Data CRC\n"); 813 bootstage_error(BOOTSTAGE_ID_CHECK_CHECKSUM); 814 return NULL; 815 } 816 puts("OK\n"); 817 } 818 bootstage_mark(BOOTSTAGE_ID_CHECK_ARCH); 819 820 if (!image_check_target_arch(hdr)) { 821 printf("Unsupported Architecture 0x%x\n", image_get_arch(hdr)); 822 bootstage_error(BOOTSTAGE_ID_CHECK_ARCH); 823 return NULL; 824 } 825 return hdr; 826 } 827 #endif 828 829 /** 830 * boot_get_kernel - find kernel image 831 * @os_data: pointer to a ulong variable, will hold os data start address 832 * @os_len: pointer to a ulong variable, will hold os data length 833 * 834 * boot_get_kernel() tries to find a kernel image, verifies its integrity 835 * and locates kernel data. 836 * 837 * returns: 838 * pointer to image header if valid image was found, plus kernel start 839 * address and length, otherwise NULL 840 */ 841 static const void *boot_get_kernel(cmd_tbl_t *cmdtp, int flag, int argc, 842 char * const argv[], bootm_headers_t *images, 843 ulong *os_data, ulong *os_len) 844 { 845 #if defined(CONFIG_IMAGE_FORMAT_LEGACY) 846 image_header_t *hdr; 847 #endif 848 ulong img_addr; 849 const void *buf; 850 const char *fit_uname_config = NULL; 851 const char *fit_uname_kernel = NULL; 852 #if IMAGE_ENABLE_FIT 853 int os_noffset; 854 #endif 855 856 img_addr = genimg_get_kernel_addr_fit(argc < 1 ? NULL : argv[0], 857 &fit_uname_config, 858 &fit_uname_kernel); 859 860 bootstage_mark(BOOTSTAGE_ID_CHECK_MAGIC); 861 862 /* check image type, for FIT images get FIT kernel node */ 863 *os_data = *os_len = 0; 864 buf = map_sysmem(img_addr, 0); 865 switch (genimg_get_format(buf)) { 866 #if defined(CONFIG_IMAGE_FORMAT_LEGACY) 867 case IMAGE_FORMAT_LEGACY: 868 printf("## Booting kernel from Legacy Image at %08lx ...\n", 869 img_addr); 870 hdr = image_get_kernel(img_addr, images->verify); 871 if (!hdr) 872 return NULL; 873 bootstage_mark(BOOTSTAGE_ID_CHECK_IMAGETYPE); 874 875 /* get os_data and os_len */ 876 switch (image_get_type(hdr)) { 877 case IH_TYPE_KERNEL: 878 case IH_TYPE_KERNEL_NOLOAD: 879 *os_data = image_get_data(hdr); 880 *os_len = image_get_data_size(hdr); 881 break; 882 case IH_TYPE_MULTI: 883 image_multi_getimg(hdr, 0, os_data, os_len); 884 break; 885 case IH_TYPE_STANDALONE: 886 *os_data = image_get_data(hdr); 887 *os_len = image_get_data_size(hdr); 888 break; 889 default: 890 if (cmdtp) 891 printf("Wrong Image Type for %s command\n", 892 cmdtp->name); 893 bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE); 894 return NULL; 895 } 896 897 /* 898 * copy image header to allow for image overwrites during 899 * kernel decompression. 900 */ 901 memmove(&images->legacy_hdr_os_copy, hdr, 902 sizeof(image_header_t)); 903 904 /* save pointer to image header */ 905 images->legacy_hdr_os = hdr; 906 907 images->legacy_hdr_valid = 1; 908 bootstage_mark(BOOTSTAGE_ID_DECOMP_IMAGE); 909 break; 910 #endif 911 #if IMAGE_ENABLE_FIT 912 case IMAGE_FORMAT_FIT: 913 os_noffset = fit_image_load(images, img_addr, 914 &fit_uname_kernel, &fit_uname_config, 915 IH_ARCH_DEFAULT, IH_TYPE_KERNEL, 916 BOOTSTAGE_ID_FIT_KERNEL_START, 917 FIT_LOAD_IGNORED, os_data, os_len); 918 if (os_noffset < 0) 919 return NULL; 920 921 images->fit_hdr_os = map_sysmem(img_addr, 0); 922 images->fit_uname_os = fit_uname_kernel; 923 images->fit_uname_cfg = fit_uname_config; 924 images->fit_noffset_os = os_noffset; 925 break; 926 #endif 927 #ifdef CONFIG_ANDROID_BOOT_IMAGE 928 case IMAGE_FORMAT_ANDROID: 929 printf("## Booting Android Image at 0x%08lx ...\n", img_addr); 930 if (android_image_get_kernel(buf, images->verify, 931 os_data, os_len)) 932 return NULL; 933 break; 934 #endif 935 default: 936 if (cmdtp) 937 printf("Wrong Image Format for %s command\n", 938 cmdtp->name); 939 bootstage_error(BOOTSTAGE_ID_FIT_KERNEL_INFO); 940 return NULL; 941 } 942 943 debug(" kernel data at 0x%08lx, len = 0x%08lx (%ld)\n", 944 *os_data, *os_len, *os_len); 945 946 return buf; 947 } 948 #else /* USE_HOSTCC */ 949 950 void memmove_wd(void *to, void *from, size_t len, ulong chunksz) 951 { 952 memmove(to, from, len); 953 } 954 955 static int bootm_host_load_image(const void *fit, int req_image_type) 956 { 957 const char *fit_uname_config = NULL; 958 ulong data, len; 959 bootm_headers_t images; 960 int noffset; 961 ulong load_end; 962 uint8_t image_type; 963 uint8_t imape_comp; 964 void *load_buf; 965 int ret; 966 967 memset(&images, '\0', sizeof(images)); 968 images.verify = 1; 969 noffset = fit_image_load(&images, (ulong)fit, 970 NULL, &fit_uname_config, 971 IH_ARCH_DEFAULT, req_image_type, -1, 972 FIT_LOAD_IGNORED, &data, &len); 973 if (noffset < 0) 974 return noffset; 975 if (fit_image_get_type(fit, noffset, &image_type)) { 976 puts("Can't get image type!\n"); 977 return -EINVAL; 978 } 979 980 if (fit_image_get_comp(fit, noffset, &imape_comp)) { 981 puts("Can't get image compression!\n"); 982 return -EINVAL; 983 } 984 985 /* Allow the image to expand by a factor of 4, should be safe */ 986 load_buf = malloc((1 << 20) + len * 4); 987 ret = bootm_decomp_image(imape_comp, 0, data, image_type, load_buf, 988 (void *)data, len, CONFIG_SYS_BOOTM_LEN, 989 &load_end); 990 free(load_buf); 991 992 if (ret && ret != BOOTM_ERR_UNIMPLEMENTED) 993 return ret; 994 995 return 0; 996 } 997 998 int bootm_host_load_images(const void *fit, int cfg_noffset) 999 { 1000 static uint8_t image_types[] = { 1001 IH_TYPE_KERNEL, 1002 IH_TYPE_FLATDT, 1003 IH_TYPE_RAMDISK, 1004 }; 1005 int err = 0; 1006 int i; 1007 1008 for (i = 0; i < ARRAY_SIZE(image_types); i++) { 1009 int ret; 1010 1011 ret = bootm_host_load_image(fit, image_types[i]); 1012 if (!err && ret && ret != -ENOENT) 1013 err = ret; 1014 } 1015 1016 /* Return the first error we found */ 1017 return err; 1018 } 1019 1020 #endif /* ndef USE_HOSTCC */ 1021