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 ulong src, ulong dst) 304 { 305 const char *name = genimg_get_type_name(type); 306 const char *comp_name[] = { 307 [IH_COMP_NONE] = "", 308 [IH_COMP_GZIP] = "GZIP", 309 [IH_COMP_BZIP2] = "BZIP2", 310 [IH_COMP_LZMA] = "LZMA", 311 [IH_COMP_LZO] = "LZO", 312 [IH_COMP_LZ4] = "LZ4", 313 [IH_COMP_ZIMAGE]= "ZIMAGE", 314 }; 315 316 if (comp_type == IH_COMP_NONE) 317 printf(" %s %s from 0x%08lx to 0x%08lx ... ", 318 is_xip ? "XIP" : "Loading", name, src, dst); 319 else 320 printf(" Uncompressing %s %s from 0x%08lx to 0x%08lx ... ", 321 comp_name[comp_type], name, src, dst); 322 } 323 324 /** 325 * handle_decomp_error() - display a decompression error 326 * 327 * This function tries to produce a useful message. In the case where the 328 * uncompressed size is the same as the available space, we can assume that 329 * the image is too large for the buffer. 330 * 331 * @comp_type: Compression type being used (IH_COMP_...) 332 * @uncomp_size: Number of bytes uncompressed 333 * @unc_len: Amount of space available for decompression 334 * @ret: Error code to report 335 * @return BOOTM_ERR_RESET, indicating that the board must be reset 336 */ 337 static int handle_decomp_error(int comp_type, size_t uncomp_size, 338 size_t unc_len, int ret) 339 { 340 const char *name = genimg_get_comp_name(comp_type); 341 342 if (uncomp_size >= unc_len) 343 printf("Image too large: increase CONFIG_SYS_BOOTM_LEN\n"); 344 else 345 printf("%s: uncompress error %d\n", name, ret); 346 347 /* 348 * The decompression routines are now safe, so will not write beyond 349 * their bounds. Probably it is not necessary to reset, but maintain 350 * the current behaviour for now. 351 */ 352 printf("Must RESET board to recover\n"); 353 #ifndef USE_HOSTCC 354 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE); 355 #endif 356 357 return BOOTM_ERR_RESET; 358 } 359 360 int bootm_parse_comp(const unsigned char *hdr) 361 { 362 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64) 363 ulong start, end; 364 365 if (!bootz_setup((ulong)hdr, &start, &end)) 366 return IH_COMP_ZIMAGE; 367 #endif 368 #if defined(CONFIG_LZ4) 369 if (lz4_is_valid_header(hdr)) 370 return IH_COMP_LZ4; 371 #endif 372 #if defined(CONFIG_LZO) 373 if (lzop_is_valid_header(hdr)) 374 return IH_COMP_LZO; 375 #endif 376 #if defined(CONFIG_GZIP) 377 if (gzip_parse_header(hdr, 0xffff) > 0) 378 return IH_COMP_GZIP; 379 #endif 380 #if defined(CONFIG_BZIP2) 381 if ((hdr[0] == 'B') && (hdr[1] == 'Z') && (hdr[2] == 'h')) 382 return IH_COMP_BZIP2; 383 #endif 384 return IH_COMP_NONE; 385 } 386 387 int bootm_decomp_image(int comp, ulong load, ulong image_start, int type, 388 void *load_buf, void *image_buf, ulong image_len, 389 uint unc_len, ulong *load_end) 390 { 391 int ret = 0; 392 393 *load_end = load; 394 print_decomp_msg(comp, type, load == image_start, 395 (ulong)image_buf, (ulong)load_buf); 396 397 /* 398 * Load the image to the right place, decompressing if needed. After 399 * this, image_len will be set to the number of uncompressed bytes 400 * loaded, ret will be non-zero on error. 401 */ 402 switch (comp) { 403 case IH_COMP_NONE: 404 if (load == image_start) 405 break; 406 if (image_len <= unc_len) 407 memmove_wd(load_buf, image_buf, image_len, CHUNKSZ); 408 else 409 ret = 1; 410 break; 411 #ifdef CONFIG_GZIP 412 case IH_COMP_GZIP: { 413 ret = gunzip(load_buf, unc_len, image_buf, &image_len); 414 break; 415 } 416 #endif /* CONFIG_GZIP */ 417 #ifdef CONFIG_BZIP2 418 case IH_COMP_BZIP2: { 419 uint size = unc_len; 420 421 /* 422 * If we've got less than 4 MB of malloc() space, 423 * use slower decompression algorithm which requires 424 * at most 2300 KB of memory. 425 */ 426 ret = BZ2_bzBuffToBuffDecompress(load_buf, &size, 427 image_buf, image_len, 428 CONFIG_SYS_MALLOC_LEN < (4096 * 1024), 0); 429 image_len = size; 430 break; 431 } 432 #endif /* CONFIG_BZIP2 */ 433 #ifdef CONFIG_LZMA 434 case IH_COMP_LZMA: { 435 SizeT lzma_len = unc_len; 436 437 ret = lzmaBuffToBuffDecompress(load_buf, &lzma_len, 438 image_buf, image_len); 439 image_len = lzma_len; 440 break; 441 } 442 #endif /* CONFIG_LZMA */ 443 #ifdef CONFIG_LZO 444 case IH_COMP_LZO: { 445 size_t size = unc_len; 446 447 ret = lzop_decompress(image_buf, image_len, load_buf, &size); 448 image_len = size; 449 break; 450 } 451 #endif /* CONFIG_LZO */ 452 #ifdef CONFIG_LZ4 453 case IH_COMP_LZ4: { 454 size_t size = unc_len; 455 456 ret = ulz4fn(image_buf, image_len, load_buf, &size); 457 image_len = size; 458 break; 459 } 460 #endif /* CONFIG_LZ4 */ 461 default: 462 printf("Unimplemented compression type %d\n", comp); 463 return BOOTM_ERR_UNIMPLEMENTED; 464 } 465 466 if (ret) 467 return handle_decomp_error(comp, image_len, unc_len, ret); 468 *load_end = load + image_len; 469 470 if (comp == IH_COMP_NONE || comp == IH_COMP_ZIMAGE) 471 puts("OK\n"); 472 else 473 printf("with %08lx bytes OK\n", image_len); 474 475 return 0; 476 } 477 478 #ifndef USE_HOSTCC 479 static int bootm_load_os(bootm_headers_t *images, unsigned long *load_end, 480 int boot_progress) 481 { 482 image_info_t os = images->os; 483 ulong load = os.load; 484 ulong blob_start = os.start; 485 ulong blob_end = os.end; 486 ulong image_start = os.image_start; 487 ulong image_len = os.image_len; 488 bool no_overlap; 489 void *load_buf, *image_buf; 490 int err; 491 492 load_buf = map_sysmem(load, 0); 493 image_buf = map_sysmem(os.image_start, image_len); 494 err = bootm_decomp_image(os.comp, load, os.image_start, os.type, 495 load_buf, image_buf, image_len, 496 CONFIG_SYS_BOOTM_LEN, load_end); 497 if (err) { 498 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE); 499 return err; 500 } 501 flush_cache(load, ALIGN(*load_end - load, ARCH_DMA_MINALIGN)); 502 503 debug(" kernel loaded at 0x%08lx, end = 0x%08lx\n", load, *load_end); 504 bootstage_mark(BOOTSTAGE_ID_KERNEL_LOADED); 505 506 no_overlap = (os.comp == IH_COMP_NONE && load == image_start); 507 508 if (!no_overlap && (load < blob_end) && (*load_end > blob_start)) { 509 debug("images.os.start = 0x%lX, images.os.end = 0x%lx\n", 510 blob_start, blob_end); 511 debug("images.os.load = 0x%lx, load_end = 0x%lx\n", load, 512 *load_end); 513 514 /* Check what type of image this is. */ 515 if (images->legacy_hdr_valid) { 516 if (image_get_type(&images->legacy_hdr_os_copy) 517 == IH_TYPE_MULTI) 518 puts("WARNING: legacy format multi component image overwritten\n"); 519 return BOOTM_ERR_OVERLAP; 520 } else { 521 puts("ERROR: new format image overwritten - must RESET the board to recover\n"); 522 bootstage_error(BOOTSTAGE_ID_OVERWRITTEN); 523 return BOOTM_ERR_RESET; 524 } 525 } 526 527 return 0; 528 } 529 530 /** 531 * bootm_disable_interrupts() - Disable interrupts in preparation for load/boot 532 * 533 * @return interrupt flag (0 if interrupts were disabled, non-zero if they were 534 * enabled) 535 */ 536 ulong bootm_disable_interrupts(void) 537 { 538 ulong iflag; 539 540 /* 541 * Do not go further if usb is boot device, 542 * We may access usb at late sequence. 543 */ 544 if (!strcmp(env_get("devtype"), "usb")) 545 return 0; 546 547 /* 548 * We have reached the point of no return: we are going to 549 * overwrite all exception vector code, so we cannot easily 550 * recover from any failures any more... 551 */ 552 iflag = disable_interrupts(); 553 #ifdef CONFIG_NETCONSOLE 554 /* Stop the ethernet stack if NetConsole could have left it up */ 555 eth_halt(); 556 # ifndef CONFIG_DM_ETH 557 eth_unregister(eth_get_dev()); 558 # endif 559 #endif 560 561 #if defined(CONFIG_CMD_USB) 562 /* 563 * turn off USB to prevent the host controller from writing to the 564 * SDRAM while Linux is booting. This could happen (at least for OHCI 565 * controller), because the HCCA (Host Controller Communication Area) 566 * lies within the SDRAM and the host controller writes continously to 567 * this area (as busmaster!). The HccaFrameNumber is for example 568 * updated every 1 ms within the HCCA structure in SDRAM! For more 569 * details see the OpenHCI specification. 570 */ 571 usb_stop(); 572 #endif 573 return iflag; 574 } 575 576 #if defined(CONFIG_SILENT_CONSOLE) && !defined(CONFIG_SILENT_U_BOOT_ONLY) 577 578 #define CONSOLE_ARG "console=" 579 #define CONSOLE_ARG_LEN (sizeof(CONSOLE_ARG) - 1) 580 581 static void fixup_silent_linux(void) 582 { 583 char *buf; 584 const char *env_val; 585 char *cmdline = env_get("bootargs"); 586 int want_silent; 587 588 /* 589 * Only fix cmdline when requested. The environment variable can be: 590 * 591 * no - we never fixup 592 * yes - we always fixup 593 * unset - we rely on the console silent flag 594 */ 595 want_silent = env_get_yesno("silent_linux"); 596 if (want_silent == 0) 597 return; 598 else if (want_silent == -1 && !(gd->flags & GD_FLG_SILENT)) 599 return; 600 601 debug("before silent fix-up: %s\n", cmdline); 602 if (cmdline && (cmdline[0] != '\0')) { 603 char *start = strstr(cmdline, CONSOLE_ARG); 604 605 /* Allocate space for maximum possible new command line */ 606 buf = malloc(strlen(cmdline) + 1 + CONSOLE_ARG_LEN + 1); 607 if (!buf) { 608 debug("%s: out of memory\n", __func__); 609 return; 610 } 611 612 if (start) { 613 char *end = strchr(start, ' '); 614 int num_start_bytes = start - cmdline + CONSOLE_ARG_LEN; 615 616 strncpy(buf, cmdline, num_start_bytes); 617 if (end) 618 strcpy(buf + num_start_bytes, end); 619 else 620 buf[num_start_bytes] = '\0'; 621 } else { 622 sprintf(buf, "%s %s", cmdline, CONSOLE_ARG); 623 } 624 env_val = buf; 625 } else { 626 buf = NULL; 627 env_val = CONSOLE_ARG; 628 } 629 630 env_set("bootargs", env_val); 631 debug("after silent fix-up: %s\n", env_val); 632 free(buf); 633 } 634 #endif /* CONFIG_SILENT_CONSOLE */ 635 636 /** 637 * Execute selected states of the bootm command. 638 * 639 * Note the arguments to this state must be the first argument, Any 'bootm' 640 * or sub-command arguments must have already been taken. 641 * 642 * Note that if states contains more than one flag it MUST contain 643 * BOOTM_STATE_START, since this handles and consumes the command line args. 644 * 645 * Also note that aside from boot_os_fn functions and bootm_load_os no other 646 * functions we store the return value of in 'ret' may use a negative return 647 * value, without special handling. 648 * 649 * @param cmdtp Pointer to bootm command table entry 650 * @param flag Command flags (CMD_FLAG_...) 651 * @param argc Number of subcommand arguments (0 = no arguments) 652 * @param argv Arguments 653 * @param states Mask containing states to run (BOOTM_STATE_...) 654 * @param images Image header information 655 * @param boot_progress 1 to show boot progress, 0 to not do this 656 * @return 0 if ok, something else on error. Some errors will cause this 657 * function to perform a reboot! If states contains BOOTM_STATE_OS_GO 658 * then the intent is to boot an OS, so this function will not return 659 * unless the image type is standalone. 660 */ 661 int do_bootm_states(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], 662 int states, bootm_headers_t *images, int boot_progress) 663 { 664 boot_os_fn *boot_fn; 665 ulong iflag = 0; 666 int ret = 0, need_boot_fn; 667 668 images->state |= states; 669 670 /* 671 * Work through the states and see how far we get. We stop on 672 * any error. 673 */ 674 if (states & BOOTM_STATE_START) 675 ret = bootm_start(cmdtp, flag, argc, argv); 676 677 if (!ret && (states & BOOTM_STATE_FINDOS)) 678 ret = bootm_find_os(cmdtp, flag, argc, argv); 679 680 if (!ret && (states & BOOTM_STATE_FINDOTHER)) 681 ret = bootm_find_other(cmdtp, flag, argc, argv); 682 683 /* Load the OS */ 684 if (!ret && (states & BOOTM_STATE_LOADOS)) { 685 ulong load_end; 686 687 iflag = bootm_disable_interrupts(); 688 ret = bootm_load_os(images, &load_end, 0); 689 if (ret == 0) 690 lmb_reserve(&images->lmb, images->os.load, 691 (load_end - images->os.load)); 692 else if (ret && ret != BOOTM_ERR_OVERLAP) 693 goto err; 694 else if (ret == BOOTM_ERR_OVERLAP) 695 ret = 0; 696 } 697 698 /* Resever memory before any lmb_alloc, as early as possible */ 699 #if IMAGE_ENABLE_OF_LIBFDT && defined(CONFIG_LMB) 700 if (!ret && ((states & BOOTM_STATE_RAMDISK) || 701 (states & BOOTM_STATE_FDT))) 702 boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr); 703 #endif 704 /* Relocate the ramdisk */ 705 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH 706 if (!ret && (states & BOOTM_STATE_RAMDISK)) { 707 ulong rd_len = images->rd_end - images->rd_start; 708 709 ret = boot_ramdisk_high(&images->lmb, images->rd_start, 710 rd_len, &images->initrd_start, &images->initrd_end); 711 if (!ret) { 712 env_set_hex("initrd_start", images->initrd_start); 713 env_set_hex("initrd_end", images->initrd_end); 714 } 715 } 716 #endif 717 #if IMAGE_ENABLE_OF_LIBFDT && defined(CONFIG_LMB) 718 if (!ret && (states & BOOTM_STATE_FDT)) { 719 ret = boot_relocate_fdt(&images->lmb, &images->ft_addr, 720 &images->ft_len); 721 } 722 #endif 723 724 /* From now on, we need the OS boot function */ 725 if (ret) 726 return ret; 727 boot_fn = bootm_os_get_boot_func(images->os.os); 728 need_boot_fn = states & (BOOTM_STATE_OS_CMDLINE | 729 BOOTM_STATE_OS_BD_T | BOOTM_STATE_OS_PREP | 730 BOOTM_STATE_OS_FAKE_GO | BOOTM_STATE_OS_GO); 731 if (boot_fn == NULL && need_boot_fn) { 732 if (iflag) 733 enable_interrupts(); 734 printf("ERROR: booting os '%s' (%d) is not supported\n", 735 genimg_get_os_name(images->os.os), images->os.os); 736 bootstage_error(BOOTSTAGE_ID_CHECK_BOOT_OS); 737 return 1; 738 } 739 740 741 /* Call various other states that are not generally used */ 742 if (!ret && (states & BOOTM_STATE_OS_CMDLINE)) 743 ret = boot_fn(BOOTM_STATE_OS_CMDLINE, argc, argv, images); 744 if (!ret && (states & BOOTM_STATE_OS_BD_T)) 745 ret = boot_fn(BOOTM_STATE_OS_BD_T, argc, argv, images); 746 if (!ret && (states & BOOTM_STATE_OS_PREP)) { 747 #if defined(CONFIG_SILENT_CONSOLE) && !defined(CONFIG_SILENT_U_BOOT_ONLY) 748 if (images->os.os == IH_OS_LINUX) 749 fixup_silent_linux(); 750 #endif 751 arch_preboot_os(BOOTM_STATE_OS_PREP); 752 753 ret = boot_fn(BOOTM_STATE_OS_PREP, argc, argv, images); 754 } 755 756 #ifdef CONFIG_TRACE 757 /* Pretend to run the OS, then run a user command */ 758 if (!ret && (states & BOOTM_STATE_OS_FAKE_GO)) { 759 char *cmd_list = env_get("fakegocmd"); 760 761 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_FAKE_GO, 762 images, boot_fn); 763 if (!ret && cmd_list) 764 ret = run_command_list(cmd_list, -1, flag); 765 } 766 #endif 767 768 /* Check for unsupported subcommand. */ 769 if (ret) { 770 puts("subcommand not supported\n"); 771 return ret; 772 } 773 774 /* Now run the OS! We hope this doesn't return */ 775 if (!ret && (states & BOOTM_STATE_OS_GO)) 776 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_GO, 777 images, boot_fn); 778 779 /* Deal with any fallout */ 780 err: 781 if (iflag) 782 enable_interrupts(); 783 784 if (ret == BOOTM_ERR_UNIMPLEMENTED) 785 bootstage_error(BOOTSTAGE_ID_DECOMP_UNIMPL); 786 else if (ret == BOOTM_ERR_RESET) 787 do_reset(cmdtp, flag, argc, argv); 788 789 return ret; 790 } 791 792 #if defined(CONFIG_IMAGE_FORMAT_LEGACY) 793 /** 794 * image_get_kernel - verify legacy format kernel image 795 * @img_addr: in RAM address of the legacy format image to be verified 796 * @verify: data CRC verification flag 797 * 798 * image_get_kernel() verifies legacy image integrity and returns pointer to 799 * legacy image header if image verification was completed successfully. 800 * 801 * returns: 802 * pointer to a legacy image header if valid image was found 803 * otherwise return NULL 804 */ 805 static image_header_t *image_get_kernel(ulong img_addr, int verify) 806 { 807 image_header_t *hdr = (image_header_t *)img_addr; 808 809 if (!image_check_magic(hdr)) { 810 puts("Bad Magic Number\n"); 811 bootstage_error(BOOTSTAGE_ID_CHECK_MAGIC); 812 return NULL; 813 } 814 bootstage_mark(BOOTSTAGE_ID_CHECK_HEADER); 815 816 if (!image_check_hcrc(hdr)) { 817 puts("Bad Header Checksum\n"); 818 bootstage_error(BOOTSTAGE_ID_CHECK_HEADER); 819 return NULL; 820 } 821 822 bootstage_mark(BOOTSTAGE_ID_CHECK_CHECKSUM); 823 image_print_contents(hdr); 824 825 if (verify) { 826 puts(" Verifying Checksum ... "); 827 if (!image_check_dcrc(hdr)) { 828 printf("Bad Data CRC\n"); 829 bootstage_error(BOOTSTAGE_ID_CHECK_CHECKSUM); 830 return NULL; 831 } 832 puts("OK\n"); 833 } 834 bootstage_mark(BOOTSTAGE_ID_CHECK_ARCH); 835 836 if (!image_check_target_arch(hdr)) { 837 printf("Unsupported Architecture 0x%x\n", image_get_arch(hdr)); 838 bootstage_error(BOOTSTAGE_ID_CHECK_ARCH); 839 return NULL; 840 } 841 return hdr; 842 } 843 #endif 844 845 /** 846 * boot_get_kernel - find kernel image 847 * @os_data: pointer to a ulong variable, will hold os data start address 848 * @os_len: pointer to a ulong variable, will hold os data length 849 * 850 * boot_get_kernel() tries to find a kernel image, verifies its integrity 851 * and locates kernel data. 852 * 853 * returns: 854 * pointer to image header if valid image was found, plus kernel start 855 * address and length, otherwise NULL 856 */ 857 static const void *boot_get_kernel(cmd_tbl_t *cmdtp, int flag, int argc, 858 char * const argv[], bootm_headers_t *images, 859 ulong *os_data, ulong *os_len) 860 { 861 #if defined(CONFIG_IMAGE_FORMAT_LEGACY) 862 image_header_t *hdr; 863 #endif 864 ulong img_addr; 865 const void *buf; 866 const char *fit_uname_config = NULL; 867 const char *fit_uname_kernel = NULL; 868 #if IMAGE_ENABLE_FIT 869 int os_noffset; 870 #endif 871 872 img_addr = genimg_get_kernel_addr_fit(argc < 1 ? NULL : argv[0], 873 &fit_uname_config, 874 &fit_uname_kernel); 875 876 bootstage_mark(BOOTSTAGE_ID_CHECK_MAGIC); 877 878 /* check image type, for FIT images get FIT kernel node */ 879 *os_data = *os_len = 0; 880 buf = map_sysmem(img_addr, 0); 881 switch (genimg_get_format(buf)) { 882 #if defined(CONFIG_IMAGE_FORMAT_LEGACY) 883 case IMAGE_FORMAT_LEGACY: 884 printf("## Booting kernel from Legacy Image at %08lx ...\n", 885 img_addr); 886 hdr = image_get_kernel(img_addr, images->verify); 887 if (!hdr) 888 return NULL; 889 bootstage_mark(BOOTSTAGE_ID_CHECK_IMAGETYPE); 890 891 /* get os_data and os_len */ 892 switch (image_get_type(hdr)) { 893 case IH_TYPE_KERNEL: 894 case IH_TYPE_KERNEL_NOLOAD: 895 *os_data = image_get_data(hdr); 896 *os_len = image_get_data_size(hdr); 897 break; 898 case IH_TYPE_MULTI: 899 image_multi_getimg(hdr, 0, os_data, os_len); 900 break; 901 case IH_TYPE_STANDALONE: 902 *os_data = image_get_data(hdr); 903 *os_len = image_get_data_size(hdr); 904 break; 905 default: 906 if (cmdtp) 907 printf("Wrong Image Type for %s command\n", 908 cmdtp->name); 909 bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE); 910 return NULL; 911 } 912 913 /* 914 * copy image header to allow for image overwrites during 915 * kernel decompression. 916 */ 917 memmove(&images->legacy_hdr_os_copy, hdr, 918 sizeof(image_header_t)); 919 920 /* save pointer to image header */ 921 images->legacy_hdr_os = hdr; 922 923 images->legacy_hdr_valid = 1; 924 bootstage_mark(BOOTSTAGE_ID_DECOMP_IMAGE); 925 break; 926 #endif 927 #if IMAGE_ENABLE_FIT 928 case IMAGE_FORMAT_FIT: 929 os_noffset = fit_image_load(images, img_addr, 930 &fit_uname_kernel, &fit_uname_config, 931 IH_ARCH_DEFAULT, IH_TYPE_KERNEL, 932 BOOTSTAGE_ID_FIT_KERNEL_START, 933 FIT_LOAD_IGNORED, os_data, os_len); 934 if (os_noffset < 0) 935 return NULL; 936 937 images->fit_hdr_os = map_sysmem(img_addr, 0); 938 images->fit_uname_os = fit_uname_kernel; 939 images->fit_uname_cfg = fit_uname_config; 940 images->fit_noffset_os = os_noffset; 941 break; 942 #endif 943 #ifdef CONFIG_ANDROID_BOOT_IMAGE 944 case IMAGE_FORMAT_ANDROID: 945 printf("## Booting Android Image at 0x%08lx ...\n", img_addr); 946 if (android_image_get_kernel(buf, images->verify, 947 os_data, os_len)) 948 return NULL; 949 break; 950 #endif 951 default: 952 if (cmdtp) 953 printf("Wrong Image Format for %s command\n", 954 cmdtp->name); 955 bootstage_error(BOOTSTAGE_ID_FIT_KERNEL_INFO); 956 return NULL; 957 } 958 959 debug(" kernel data at 0x%08lx, len = 0x%08lx (%ld)\n", 960 *os_data, *os_len, *os_len); 961 962 return buf; 963 } 964 #else /* USE_HOSTCC */ 965 966 void memmove_wd(void *to, void *from, size_t len, ulong chunksz) 967 { 968 memmove(to, from, len); 969 } 970 971 static int bootm_host_load_image(const void *fit, int req_image_type) 972 { 973 const char *fit_uname_config = NULL; 974 ulong data, len; 975 bootm_headers_t images; 976 int noffset; 977 ulong load_end; 978 uint8_t image_type; 979 uint8_t imape_comp; 980 void *load_buf; 981 int ret; 982 983 memset(&images, '\0', sizeof(images)); 984 images.verify = 1; 985 noffset = fit_image_load(&images, (ulong)fit, 986 NULL, &fit_uname_config, 987 IH_ARCH_DEFAULT, req_image_type, -1, 988 FIT_LOAD_IGNORED, &data, &len); 989 if (noffset < 0) 990 return noffset; 991 if (fit_image_get_type(fit, noffset, &image_type)) { 992 puts("Can't get image type!\n"); 993 return -EINVAL; 994 } 995 996 if (fit_image_get_comp(fit, noffset, &imape_comp)) { 997 puts("Can't get image compression!\n"); 998 return -EINVAL; 999 } 1000 1001 /* Allow the image to expand by a factor of 4, should be safe */ 1002 load_buf = malloc((1 << 20) + len * 4); 1003 ret = bootm_decomp_image(imape_comp, 0, data, image_type, load_buf, 1004 (void *)data, len, CONFIG_SYS_BOOTM_LEN, 1005 &load_end); 1006 free(load_buf); 1007 1008 if (ret && ret != BOOTM_ERR_UNIMPLEMENTED) 1009 return ret; 1010 1011 return 0; 1012 } 1013 1014 int bootm_host_load_images(const void *fit, int cfg_noffset) 1015 { 1016 static uint8_t image_types[] = { 1017 IH_TYPE_KERNEL, 1018 IH_TYPE_FLATDT, 1019 IH_TYPE_RAMDISK, 1020 }; 1021 int err = 0; 1022 int i; 1023 1024 for (i = 0; i < ARRAY_SIZE(image_types); i++) { 1025 int ret; 1026 1027 ret = bootm_host_load_image(fit, image_types[i]); 1028 if (!err && ret && ret != -ENOENT) 1029 err = ret; 1030 } 1031 1032 /* Return the first error we found */ 1033 return err; 1034 } 1035 1036 #endif /* ndef USE_HOSTCC */ 1037