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