"Das U-Boot" Source Tree
at master 1112 lines 28 kB view raw
1// SPDX-License-Identifier: GPL-2.0+ 2/* 3 * Image code used by boards (and not host tools) 4 * 5 * (C) Copyright 2008 Semihalf 6 * 7 * (C) Copyright 2000-2006 8 * Wolfgang Denk, DENX Software Engineering, wd@denx.de. 9 */ 10 11#include <config.h> 12#include <bootstage.h> 13#include <cpu_func.h> 14#include <display_options.h> 15#include <env.h> 16#include <fpga.h> 17#include <image.h> 18#include <init.h> 19#include <log.h> 20#include <mapmem.h> 21#include <rtc.h> 22#include <watchdog.h> 23#include <asm/cache.h> 24#include <asm/global_data.h> 25 26DECLARE_GLOBAL_DATA_PTR; 27 28/** 29 * image_get_ramdisk - get and verify ramdisk image 30 * @rd_addr: ramdisk image start address 31 * @arch: expected ramdisk architecture 32 * @verify: checksum verification flag 33 * 34 * image_get_ramdisk() returns a pointer to the verified ramdisk image 35 * header. Routine receives image start address and expected architecture 36 * flag. Verification done covers data and header integrity and os/type/arch 37 * fields checking. 38 * 39 * returns: 40 * pointer to a ramdisk image header, if image was found and valid 41 * otherwise, return NULL 42 */ 43static const struct legacy_img_hdr *image_get_ramdisk(ulong rd_addr, u8 arch, 44 int verify) 45{ 46 const struct legacy_img_hdr *rd_hdr = (const struct legacy_img_hdr *)rd_addr; 47 48 if (!image_check_magic(rd_hdr)) { 49 puts("Bad Magic Number\n"); 50 bootstage_error(BOOTSTAGE_ID_RD_MAGIC); 51 return NULL; 52 } 53 54 if (!image_check_hcrc(rd_hdr)) { 55 puts("Bad Header Checksum\n"); 56 bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM); 57 return NULL; 58 } 59 60 bootstage_mark(BOOTSTAGE_ID_RD_MAGIC); 61 image_print_contents(rd_hdr); 62 63 if (verify) { 64 puts(" Verifying Checksum ... "); 65 if (!image_check_dcrc(rd_hdr)) { 66 puts("Bad Data CRC\n"); 67 bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM); 68 return NULL; 69 } 70 puts("OK\n"); 71 } 72 73 bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM); 74 75 if (!image_check_os(rd_hdr, IH_OS_LINUX) || 76 !image_check_arch(rd_hdr, arch) || 77 !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) { 78 printf("No Linux %s Ramdisk Image\n", 79 genimg_get_arch_name(arch)); 80 bootstage_error(BOOTSTAGE_ID_RAMDISK); 81 return NULL; 82 } 83 84 return rd_hdr; 85} 86 87/*****************************************************************************/ 88/* Shared dual-format routines */ 89/*****************************************************************************/ 90ulong image_load_addr = CONFIG_SYS_LOAD_ADDR; /* Default Load Address */ 91ulong image_save_addr; /* Default Save Address */ 92ulong image_save_size; /* Default Save Size (in bytes) */ 93 94static int on_loadaddr(const char *name, const char *value, enum env_op op, 95 int flags) 96{ 97 switch (op) { 98 case env_op_create: 99 case env_op_overwrite: 100 image_load_addr = hextoul(value, NULL); 101 break; 102 default: 103 break; 104 } 105 106 return 0; 107} 108U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr); 109 110phys_addr_t env_get_bootm_low(void) 111{ 112 char *s = env_get("bootm_low"); 113 114 if (s) 115 return simple_strtoull(s, NULL, 16); 116 117#if defined(CFG_SYS_SDRAM_BASE) 118 return CFG_SYS_SDRAM_BASE; 119#elif defined(CONFIG_ARM) || defined(CONFIG_MICROBLAZE) || defined(CONFIG_RISCV) 120 return gd->bd->bi_dram[0].start; 121#else 122 return 0; 123#endif 124} 125 126phys_size_t env_get_bootm_size(void) 127{ 128 phys_addr_t start, low; 129 phys_size_t size; 130 char *s = env_get("bootm_size"); 131 132 if (s) 133 return simple_strtoull(s, NULL, 16); 134 135 start = gd->ram_base; 136 size = gd->ram_size; 137 138 if (start + size > gd->ram_top) 139 size = gd->ram_top - start; 140 141 s = env_get("bootm_low"); 142 if (s) 143 low = simple_strtoull(s, NULL, 16); 144 else 145 low = start; 146 147 return size - (low - start); 148} 149 150phys_size_t env_get_bootm_mapsize(void) 151{ 152 char *s = env_get("bootm_mapsize"); 153 154 if (s) 155 return simple_strtoull(s, NULL, 16); 156 157#if defined(CFG_SYS_BOOTMAPSZ) 158 return CFG_SYS_BOOTMAPSZ; 159#else 160 return env_get_bootm_size(); 161#endif 162} 163 164void memmove_wd(void *to, void *from, size_t len, ulong chunksz) 165{ 166 if (to == from) 167 return; 168 169 if (IS_ENABLED(CONFIG_HW_WATCHDOG) || IS_ENABLED(CONFIG_WATCHDOG)) { 170 if (to > from) { 171 from += len; 172 to += len; 173 } 174 while (len > 0) { 175 size_t tail = (len > chunksz) ? chunksz : len; 176 177 schedule(); 178 if (to > from) { 179 to -= tail; 180 from -= tail; 181 } 182 memmove(to, from, tail); 183 if (to < from) { 184 to += tail; 185 from += tail; 186 } 187 len -= tail; 188 } 189 } else { 190 memmove(to, from, len); 191 } 192} 193 194ulong genimg_get_kernel_addr_fit(const char *const img_addr, 195 const char **fit_uname_config, 196 const char **fit_uname_kernel) 197{ 198 ulong kernel_addr; 199 200 /* find out kernel image address */ 201 if (!img_addr) { 202 kernel_addr = image_load_addr; 203 debug("* kernel: default image load address = 0x%08lx\n", 204 image_load_addr); 205 } else if (CONFIG_IS_ENABLED(FIT) && 206 fit_parse_conf(img_addr, image_load_addr, &kernel_addr, 207 fit_uname_config)) { 208 debug("* kernel: config '%s' from image at 0x%08lx\n", 209 *fit_uname_config, kernel_addr); 210 } else if (CONFIG_IS_ENABLED(FIT) && 211 fit_parse_subimage(img_addr, image_load_addr, &kernel_addr, 212 fit_uname_kernel)) { 213 debug("* kernel: subimage '%s' from image at 0x%08lx\n", 214 *fit_uname_kernel, kernel_addr); 215 } else { 216 kernel_addr = hextoul(img_addr, NULL); 217 debug("* kernel: cmdline image address = 0x%08lx\n", 218 kernel_addr); 219 } 220 221 return kernel_addr; 222} 223 224/** 225 * genimg_get_kernel_addr() is the simple version of 226 * genimg_get_kernel_addr_fit(). It ignores those return FIT strings 227 */ 228ulong genimg_get_kernel_addr(char * const img_addr) 229{ 230 const char *fit_uname_config = NULL; 231 const char *fit_uname_kernel = NULL; 232 233 return genimg_get_kernel_addr_fit(img_addr, &fit_uname_config, 234 &fit_uname_kernel); 235} 236 237/** 238 * genimg_get_format - get image format type 239 * @img_addr: image start address 240 * 241 * genimg_get_format() checks whether provided address points to a valid 242 * legacy or FIT image. 243 * 244 * New uImage format and FDT blob are based on a libfdt. FDT blob 245 * may be passed directly or embedded in a FIT image. In both situations 246 * genimg_get_format() must be able to dectect libfdt header. 247 * 248 * returns: 249 * image format type or IMAGE_FORMAT_INVALID if no image is present 250 */ 251int genimg_get_format(const void *img_addr) 252{ 253 if (CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)) { 254 const struct legacy_img_hdr *hdr; 255 256 hdr = (const struct legacy_img_hdr *)img_addr; 257 if (image_check_magic(hdr)) 258 return IMAGE_FORMAT_LEGACY; 259 } 260 if (CONFIG_IS_ENABLED(FIT) || CONFIG_IS_ENABLED(OF_LIBFDT)) { 261 if (!fdt_check_header(img_addr)) 262 return IMAGE_FORMAT_FIT; 263 } 264 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE) && 265 is_android_boot_image_header(img_addr)) 266 return IMAGE_FORMAT_ANDROID; 267 268 return IMAGE_FORMAT_INVALID; 269} 270 271/** 272 * fit_has_config - check if there is a valid FIT configuration 273 * @images: pointer to the bootm command headers structure 274 * 275 * fit_has_config() checks if there is a FIT configuration in use 276 * (if FTI support is present). 277 * 278 * returns: 279 * 0, no FIT support or no configuration found 280 * 1, configuration found 281 */ 282int genimg_has_config(struct bootm_headers *images) 283{ 284 if (CONFIG_IS_ENABLED(FIT) && images->fit_uname_cfg) 285 return 1; 286 287 return 0; 288} 289 290/** 291 * select_ramdisk() - Select and locate the ramdisk to use 292 * 293 * @images: pointer to the bootm images structure 294 * @select: name of ramdisk to select, or hex address, NULL for any 295 * @arch: expected ramdisk architecture 296 * @rd_datap: pointer to a ulong variable, will hold ramdisk pointer 297 * @rd_lenp: pointer to a ulong variable, will hold ramdisk length 298 * Return: 0 if OK, -ENOPKG if no ramdisk (but an error should not be reported), 299 * other -ve value on other error 300 */ 301static int select_ramdisk(struct bootm_headers *images, const char *select, u8 arch, 302 ulong *rd_datap, ulong *rd_lenp) 303{ 304 const char *fit_uname_config; 305 const char *fit_uname_ramdisk; 306 bool done_select = !select; 307 bool done = false; 308 int rd_noffset; 309 ulong rd_addr = 0; 310 char *buf; 311 312 if (CONFIG_IS_ENABLED(FIT)) { 313 fit_uname_config = images->fit_uname_cfg; 314 fit_uname_ramdisk = NULL; 315 316 if (select) { 317 ulong default_addr; 318 /* 319 * If the init ramdisk comes from the FIT image and 320 * the FIT image address is omitted in the command 321 * line argument, try to use os FIT image address or 322 * default load address. 323 */ 324 if (images->fit_uname_os) 325 default_addr = (ulong)images->fit_hdr_os; 326 else 327 default_addr = image_load_addr; 328 329 if (fit_parse_conf(select, default_addr, &rd_addr, 330 &fit_uname_config)) { 331 debug("* ramdisk: config '%s' from image at 0x%08lx\n", 332 fit_uname_config, rd_addr); 333 done_select = true; 334 } else if (fit_parse_subimage(select, default_addr, 335 &rd_addr, 336 &fit_uname_ramdisk)) { 337 debug("* ramdisk: subimage '%s' from image at 0x%08lx\n", 338 fit_uname_ramdisk, rd_addr); 339 done_select = true; 340 } 341 } 342 } 343 if (!done_select) { 344 rd_addr = hextoul(select, NULL); 345 debug("* ramdisk: cmdline image address = 0x%08lx\n", rd_addr); 346 } 347 if (CONFIG_IS_ENABLED(FIT) && !select) { 348 /* use FIT configuration provided in first bootm 349 * command argument. If the property is not defined, 350 * quit silently (with -ENOPKG) 351 */ 352 rd_addr = map_to_sysmem(images->fit_hdr_os); 353 rd_noffset = fit_get_node_from_config(images, FIT_RAMDISK_PROP, 354 rd_addr); 355 if (rd_noffset == -ENOENT) 356 return -ENOPKG; 357 else if (rd_noffset < 0) 358 return rd_noffset; 359 } 360 361 /* 362 * Check if there is an initrd image at the 363 * address provided in the second bootm argument 364 * check image type, for FIT images get FIT node. 365 */ 366 buf = map_sysmem(rd_addr, 0); 367 switch (genimg_get_format(buf)) { 368 case IMAGE_FORMAT_LEGACY: 369 if (CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)) { 370 const struct legacy_img_hdr *rd_hdr; 371 372 printf("## Loading init Ramdisk from Legacy Image at %08lx ...\n", 373 rd_addr); 374 375 bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK); 376 rd_hdr = image_get_ramdisk(rd_addr, arch, 377 images->verify); 378 379 if (!rd_hdr) 380 return -ENOENT; 381 382 *rd_datap = image_get_data(rd_hdr); 383 *rd_lenp = image_get_data_size(rd_hdr); 384 done = true; 385 } 386 break; 387 case IMAGE_FORMAT_FIT: 388 if (CONFIG_IS_ENABLED(FIT)) { 389 rd_noffset = fit_image_load(images, rd_addr, 390 &fit_uname_ramdisk, 391 &fit_uname_config, 392 arch, IH_TYPE_RAMDISK, 393 BOOTSTAGE_ID_FIT_RD_START, 394 FIT_LOAD_OPTIONAL_NON_ZERO, 395 rd_datap, rd_lenp); 396 if (rd_noffset < 0) 397 return rd_noffset; 398 399 images->fit_hdr_rd = map_sysmem(rd_addr, 0); 400 images->fit_uname_rd = fit_uname_ramdisk; 401 images->fit_noffset_rd = rd_noffset; 402 done = true; 403 } 404 break; 405 case IMAGE_FORMAT_ANDROID: 406 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE)) { 407 int ret; 408 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) { 409 ulong boot_img = get_abootimg_addr(); 410 ulong init_boot_img = get_ainit_bootimg_addr(); 411 void *vendor_boot_img = map_sysmem(get_avendor_bootimg_addr(), 0); 412 void *ramdisk_img; 413 414 if (init_boot_img == -1) 415 ramdisk_img = map_sysmem(boot_img, 0); 416 else 417 ramdisk_img = map_sysmem(init_boot_img, 0); 418 419 ret = android_image_get_ramdisk(ramdisk_img, vendor_boot_img, 420 rd_datap, rd_lenp); 421 unmap_sysmem(vendor_boot_img); 422 unmap_sysmem(ramdisk_img); 423 } else { 424 void *ptr = map_sysmem(images->os.start, 0); 425 426 ret = android_image_get_ramdisk(ptr, NULL, rd_datap, rd_lenp); 427 unmap_sysmem(ptr); 428 } 429 430 if (ret == -ENOENT) 431 return -ENOPKG; 432 else if (ret) 433 return ret; 434 done = true; 435 } 436 break; 437 } 438 439 if (!done) { 440 if (IS_ENABLED(CONFIG_SUPPORT_RAW_INITRD)) { 441 char *end = NULL; 442 443 if (select) 444 end = strchr(select, ':'); 445 if (end) { 446 *rd_lenp = hextoul(++end, NULL); 447 *rd_datap = rd_addr; 448 done = true; 449 } 450 } 451 452 if (!done) { 453 puts("Wrong Ramdisk Image Format\n"); 454 return -EINVAL; 455 } 456 } 457 458 return 0; 459} 460 461int boot_get_ramdisk(char const *select, struct bootm_headers *images, 462 uint arch, ulong *rd_start, ulong *rd_end) 463{ 464 ulong rd_data, rd_len; 465 466 *rd_start = 0; 467 *rd_end = 0; 468 469 /* 470 * Look for a '-' which indicates to ignore the 471 * ramdisk argument 472 */ 473 if (select && strcmp(select, "-") == 0) { 474 debug("## Skipping init Ramdisk\n"); 475 rd_len = 0; 476 rd_data = 0; 477 } else if (select || genimg_has_config(images)) { 478 int ret; 479 480 ret = select_ramdisk(images, select, arch, &rd_data, &rd_len); 481 if (ret == -ENOPKG) 482 return 0; 483 else if (ret) 484 return ret; 485 } else if (images->legacy_hdr_valid && 486 image_check_type(&images->legacy_hdr_os_copy, 487 IH_TYPE_MULTI)) { 488 /* 489 * Now check if we have a legacy mult-component image, 490 * get second entry data start address and len. 491 */ 492 bootstage_mark(BOOTSTAGE_ID_RAMDISK); 493 printf("## Loading init Ramdisk from multi component Legacy Image at %08lx ...\n", 494 (ulong)images->legacy_hdr_os); 495 496 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len); 497 } else { 498 /* 499 * no initrd image 500 */ 501 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK); 502 rd_len = 0; 503 rd_data = 0; 504 } 505 506 if (!rd_data) { 507 debug("## No init Ramdisk\n"); 508 } else { 509 *rd_start = rd_data; 510 *rd_end = rd_data + rd_len; 511 } 512 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n", 513 *rd_start, *rd_end); 514 515 return 0; 516} 517 518/** 519 * boot_ramdisk_high - relocate init ramdisk 520 * @rd_data: ramdisk data start address 521 * @rd_len: ramdisk data length 522 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk 523 * start address (after possible relocation) 524 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk 525 * end address (after possible relocation) 526 * 527 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment 528 * variable and if requested ramdisk data is moved to a specified location. 529 * 530 * Initrd_start and initrd_end are set to final (after relocation) ramdisk 531 * start/end addresses if ramdisk image start and len were provided, 532 * otherwise set initrd_start and initrd_end set to zeros. 533 * 534 * returns: 535 * 0 - success 536 * -1 - failure 537 */ 538int boot_ramdisk_high(ulong rd_data, ulong rd_len, ulong *initrd_start, 539 ulong *initrd_end) 540{ 541 char *s; 542 phys_addr_t initrd_high; 543 int initrd_copy_to_ram = 1; 544 545 s = env_get("initrd_high"); 546 if (s) { 547 /* a value of "no" or a similar string will act like 0, 548 * turning the "load high" feature off. This is intentional. 549 */ 550 initrd_high = hextoul(s, NULL); 551 if (initrd_high == ~0) 552 initrd_copy_to_ram = 0; 553 } else { 554 initrd_high = env_get_bootm_mapsize() + env_get_bootm_low(); 555 } 556 557 debug("## initrd_high = 0x%llx, copy_to_ram = %d\n", 558 (u64)initrd_high, initrd_copy_to_ram); 559 560 if (rd_data) { 561 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */ 562 debug(" in-place initrd\n"); 563 *initrd_start = rd_data; 564 *initrd_end = rd_data + rd_len; 565 lmb_reserve(rd_data, rd_len, LMB_NONE); 566 } else { 567 if (initrd_high) 568 *initrd_start = 569 (ulong)lmb_alloc_base(rd_len, 570 0x1000, 571 initrd_high, 572 LMB_NONE); 573 else 574 *initrd_start = (ulong)lmb_alloc(rd_len, 575 0x1000); 576 577 if (*initrd_start == 0) { 578 puts("ramdisk - allocation error\n"); 579 goto error; 580 } 581 bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK); 582 583 *initrd_end = *initrd_start + rd_len; 584 printf(" Loading Ramdisk to %08lx, end %08lx ... ", 585 *initrd_start, *initrd_end); 586 587 memmove_wd((void *)*initrd_start, 588 (void *)rd_data, rd_len, CHUNKSZ); 589 590 /* 591 * Ensure the image is flushed to memory to handle 592 * AMP boot scenarios in which we might not be 593 * HW cache coherent 594 */ 595 if (IS_ENABLED(CONFIG_MP)) { 596 flush_cache((unsigned long)*initrd_start, 597 ALIGN(rd_len, ARCH_DMA_MINALIGN)); 598 } 599 puts("OK\n"); 600 } 601 } else { 602 *initrd_start = 0; 603 *initrd_end = 0; 604 } 605 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n", 606 *initrd_start, *initrd_end); 607 608 return 0; 609 610error: 611 return -1; 612} 613 614int boot_get_setup(struct bootm_headers *images, u8 arch, 615 ulong *setup_start, ulong *setup_len) 616{ 617 if (!CONFIG_IS_ENABLED(FIT)) 618 return -ENOENT; 619 620 return boot_get_setup_fit(images, arch, setup_start, setup_len); 621} 622 623int boot_get_fpga(struct bootm_headers *images) 624{ 625 ulong tmp_img_addr, img_data, img_len; 626 void *buf; 627 int conf_noffset; 628 int fit_img_result; 629 const char *uname, *name, *compatible; 630 int err; 631 int devnum = 0; /* TODO support multi fpga platforms */ 632 int flags = 0; 633 634 if (!IS_ENABLED(CONFIG_FPGA)) 635 return -ENOSYS; 636 637 /* Check to see if the images struct has a FIT configuration */ 638 if (!genimg_has_config(images)) { 639 debug("## FIT configuration was not specified\n"); 640 return 0; 641 } 642 643 /* 644 * Obtain the os FIT header from the images struct 645 */ 646 tmp_img_addr = map_to_sysmem(images->fit_hdr_os); 647 buf = map_sysmem(tmp_img_addr, 0); 648 /* 649 * Check image type. For FIT images get FIT node 650 * and attempt to locate a generic binary. 651 */ 652 switch (genimg_get_format(buf)) { 653 case IMAGE_FORMAT_FIT: 654 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg); 655 656 uname = fdt_stringlist_get(buf, conf_noffset, FIT_FPGA_PROP, 0, 657 NULL); 658 if (!uname) { 659 debug("## FPGA image is not specified\n"); 660 return 0; 661 } 662 fit_img_result = fit_image_load(images, 663 tmp_img_addr, 664 (const char **)&uname, 665 &images->fit_uname_cfg, 666 IH_ARCH_DEFAULT, 667 IH_TYPE_FPGA, 668 BOOTSTAGE_ID_FPGA_INIT, 669 FIT_LOAD_OPTIONAL_NON_ZERO, 670 &img_data, &img_len); 671 672 debug("FPGA image (%s) loaded to 0x%lx/size 0x%lx\n", 673 uname, img_data, img_len); 674 675 if (fit_img_result < 0) { 676 /* Something went wrong! */ 677 return fit_img_result; 678 } 679 680 conf_noffset = fit_image_get_node(buf, uname); 681 compatible = fdt_getprop(buf, conf_noffset, "compatible", NULL); 682 if (!compatible) { 683 printf("'fpga' image without 'compatible' property\n"); 684 } else { 685 if (CONFIG_IS_ENABLED(FPGA_LOAD_SECURE)) 686 flags = fpga_compatible2flag(devnum, compatible); 687 } 688 689 if (!fpga_is_partial_data(devnum, img_len)) { 690 name = "full"; 691 err = fpga_loadbitstream(devnum, (char *)img_data, 692 img_len, BIT_FULL); 693 if (err) 694 err = fpga_load(devnum, (const void *)img_data, 695 img_len, BIT_FULL, flags); 696 } else { 697 name = "partial"; 698 err = fpga_loadbitstream(devnum, (char *)img_data, 699 img_len, BIT_PARTIAL); 700 if (err) 701 err = fpga_load(devnum, (const void *)img_data, 702 img_len, BIT_PARTIAL, flags); 703 } 704 705 if (err) 706 return err; 707 708 printf(" Programming %s bitstream... OK\n", name); 709 break; 710 default: 711 printf("The given image format is not supported (corrupt?)\n"); 712 return 1; 713 } 714 715 return 0; 716} 717 718static void fit_loadable_process(u8 img_type, 719 ulong img_data, 720 ulong img_len) 721{ 722 int i; 723 const unsigned int count = 724 ll_entry_count(struct fit_loadable_tbl, fit_loadable); 725 struct fit_loadable_tbl *fit_loadable_handler = 726 ll_entry_start(struct fit_loadable_tbl, fit_loadable); 727 /* For each loadable handler */ 728 for (i = 0; i < count; i++, fit_loadable_handler++) 729 /* matching this type */ 730 if (fit_loadable_handler->type == img_type) 731 /* call that handler with this image data */ 732 fit_loadable_handler->handler(img_data, img_len); 733} 734 735int boot_get_loadable(struct bootm_headers *images) 736{ 737 /* 738 * These variables are used to hold the current image location 739 * in system memory. 740 */ 741 ulong tmp_img_addr; 742 /* 743 * These two variables are requirements for fit_image_load, but 744 * their values are not used 745 */ 746 ulong img_data, img_len; 747 void *buf; 748 int loadables_index; 749 int conf_noffset; 750 int fit_img_result; 751 const char *uname; 752 u8 img_type; 753 754 /* Check to see if the images struct has a FIT configuration */ 755 if (!genimg_has_config(images)) { 756 debug("## FIT configuration was not specified\n"); 757 return 0; 758 } 759 760 /* 761 * Obtain the os FIT header from the images struct 762 */ 763 tmp_img_addr = map_to_sysmem(images->fit_hdr_os); 764 buf = map_sysmem(tmp_img_addr, 0); 765 /* 766 * Check image type. For FIT images get FIT node 767 * and attempt to locate a generic binary. 768 */ 769 switch (genimg_get_format(buf)) { 770 case IMAGE_FORMAT_FIT: 771 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg); 772 773 for (loadables_index = 0; 774 uname = fdt_stringlist_get(buf, conf_noffset, 775 FIT_LOADABLE_PROP, 776 loadables_index, NULL), uname; 777 loadables_index++) { 778 fit_img_result = fit_image_load(images, tmp_img_addr, 779 &uname, 780 &images->fit_uname_cfg, 781 IH_ARCH_DEFAULT, 782 IH_TYPE_LOADABLE, 783 BOOTSTAGE_ID_FIT_LOADABLE_START, 784 FIT_LOAD_OPTIONAL_NON_ZERO, 785 &img_data, &img_len); 786 if (fit_img_result < 0) { 787 /* Something went wrong! */ 788 return fit_img_result; 789 } 790 791 fit_img_result = fit_image_get_node(buf, uname); 792 if (fit_img_result < 0) { 793 /* Something went wrong! */ 794 return fit_img_result; 795 } 796 fit_img_result = fit_image_get_type(buf, 797 fit_img_result, 798 &img_type); 799 if (fit_img_result < 0) { 800 /* Something went wrong! */ 801 return fit_img_result; 802 } 803 804 fit_loadable_process(img_type, img_data, img_len); 805 } 806 break; 807 default: 808 printf("The given image format is not supported (corrupt?)\n"); 809 return 1; 810 } 811 812 return 0; 813} 814 815/** 816 * boot_get_cmdline - allocate and initialize kernel cmdline 817 * @cmd_start: pointer to a ulong variable, will hold cmdline start 818 * @cmd_end: pointer to a ulong variable, will hold cmdline end 819 * 820 * This allocates space for kernel command line below 821 * BOOTMAPSZ + env_get_bootm_low() address. If "bootargs" U-Boot environment 822 * variable is present its contents is copied to allocated kernel 823 * command line. 824 * 825 * returns: 826 * 0 - success 827 * -1 - failure 828 */ 829int boot_get_cmdline(ulong *cmd_start, ulong *cmd_end) 830{ 831 int barg; 832 char *cmdline; 833 char *s; 834 835 /* 836 * Help the compiler detect that this function is only called when 837 * CONFIG_SYS_BOOT_GET_CMDLINE is enabled 838 */ 839 if (!IS_ENABLED(CONFIG_SYS_BOOT_GET_CMDLINE)) 840 return 0; 841 842 barg = IF_ENABLED_INT(CONFIG_SYS_BOOT_GET_CMDLINE, CONFIG_SYS_BARGSIZE); 843 cmdline = (char *)(ulong)lmb_alloc_base(barg, 0xf, 844 env_get_bootm_mapsize() + env_get_bootm_low(), 845 LMB_NONE); 846 if (!cmdline) 847 return -1; 848 849 s = env_get("bootargs"); 850 if (!s) 851 s = ""; 852 853 strcpy(cmdline, s); 854 855 *cmd_start = (ulong)cmdline; 856 *cmd_end = *cmd_start + strlen(cmdline); 857 858 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end); 859 860 return 0; 861} 862 863/** 864 * boot_get_kbd - allocate and initialize kernel copy of board info 865 * @kbd: double pointer to board info data 866 * 867 * boot_get_kbd() allocates space for kernel copy of board info data below 868 * BOOTMAPSZ + env_get_bootm_low() address and kernel board info is initialized 869 * with the current u-boot board info data. 870 * 871 * returns: 872 * 0 - success 873 * -1 - failure 874 */ 875int boot_get_kbd(struct bd_info **kbd) 876{ 877 *kbd = (struct bd_info *)(ulong)lmb_alloc_base(sizeof(struct bd_info), 878 0xf, 879 env_get_bootm_mapsize() + 880 env_get_bootm_low(), 881 LMB_NONE); 882 if (!*kbd) 883 return -1; 884 885 **kbd = *gd->bd; 886 887 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd); 888 889 if (_DEBUG && IS_ENABLED(CONFIG_CMD_BDI)) 890 do_bdinfo(NULL, 0, 0, NULL); 891 892 return 0; 893} 894 895int image_setup_linux(struct bootm_headers *images) 896{ 897 ulong of_size = images->ft_len; 898 char **of_flat_tree = &images->ft_addr; 899 int ret; 900 901 /* This function cannot be called without lmb support */ 902 if (!CONFIG_IS_ENABLED(LMB)) 903 return -EFAULT; 904 if (CONFIG_IS_ENABLED(OF_LIBFDT)) 905 boot_fdt_add_mem_rsv_regions(*of_flat_tree); 906 907 if (IS_ENABLED(CONFIG_SYS_BOOT_GET_CMDLINE)) { 908 ret = boot_get_cmdline(&images->cmdline_start, 909 &images->cmdline_end); 910 if (ret) { 911 puts("ERROR with allocation of cmdline\n"); 912 return ret; 913 } 914 } 915 916 if (CONFIG_IS_ENABLED(OF_LIBFDT)) { 917 ret = boot_relocate_fdt(of_flat_tree, &of_size); 918 if (ret) 919 return ret; 920 } 921 922 if (CONFIG_IS_ENABLED(OF_LIBFDT) && of_size) { 923 ret = image_setup_libfdt(images, *of_flat_tree, true); 924 if (ret) 925 return ret; 926 } 927 928 return 0; 929} 930 931void genimg_print_size(uint32_t size) 932{ 933 printf("%d Bytes = ", size); 934 print_size(size, "\n"); 935} 936 937void genimg_print_time(time_t timestamp) 938{ 939 struct rtc_time tm; 940 941 rtc_to_tm(timestamp, &tm); 942 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n", 943 tm.tm_year, tm.tm_mon, tm.tm_mday, 944 tm.tm_hour, tm.tm_min, tm.tm_sec); 945} 946 947/** 948 * get_default_image() - Return default property from /images 949 * 950 * Return: Pointer to value of default property (or NULL) 951 */ 952static const char *get_default_image(const void *fit) 953{ 954 int images_noffset; 955 956 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); 957 if (images_noffset < 0) 958 return NULL; 959 960 return fdt_getprop(fit, images_noffset, FIT_DEFAULT_PROP, NULL); 961} 962 963int image_locate_script(void *buf, int size, const char *fit_uname, 964 const char *confname, char **datap, uint *lenp) 965{ 966 const struct legacy_img_hdr *hdr; 967 const void *fit_data; 968 const void *fit_hdr; 969 size_t fit_len; 970 int noffset; 971 int verify; 972 ulong len; 973 u32 *data; 974 975 verify = env_get_yesno("verify"); 976 977 switch (genimg_get_format(buf)) { 978 case IMAGE_FORMAT_LEGACY: 979 if (!IS_ENABLED(CONFIG_LEGACY_IMAGE_FORMAT)) { 980 goto exit_image_format; 981 } else { 982 hdr = buf; 983 984 if (!image_check_magic(hdr)) { 985 puts("Bad magic number\n"); 986 return 1; 987 } 988 989 if (!image_check_hcrc(hdr)) { 990 puts("Bad header crc\n"); 991 return 1; 992 } 993 994 if (verify) { 995 if (!image_check_dcrc(hdr)) { 996 puts("Bad data crc\n"); 997 return 1; 998 } 999 } 1000 1001 if (!image_check_type(hdr, IH_TYPE_SCRIPT)) { 1002 puts("Bad image type\n"); 1003 return 1; 1004 } 1005 1006 /* get length of script */ 1007 data = (u32 *)image_get_data(hdr); 1008 1009 len = uimage_to_cpu(*data); 1010 if (!len) { 1011 puts("Empty Script\n"); 1012 return 1; 1013 } 1014 1015 /* 1016 * scripts are just multi-image files with one 1017 * component, so seek past the zero-terminated sequence 1018 * of image lengths to get to the actual image data 1019 */ 1020 while (*data++); 1021 } 1022 break; 1023 case IMAGE_FORMAT_FIT: 1024 if (!IS_ENABLED(CONFIG_FIT)) { 1025 goto exit_image_format; 1026 } else { 1027 fit_hdr = buf; 1028 if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) { 1029 puts("Bad FIT image format\n"); 1030 return 1; 1031 } 1032 1033 if (!fit_uname) { 1034 /* If confname is empty, use the default */ 1035 if (confname && *confname) 1036 noffset = fit_conf_get_node(fit_hdr, confname); 1037 else 1038 noffset = fit_conf_get_node(fit_hdr, NULL); 1039 if (noffset < 0) { 1040 if (!confname) 1041 goto fallback; 1042 printf("Could not find config %s\n", confname); 1043 return 1; 1044 } 1045 1046 if (verify && fit_config_verify(fit_hdr, noffset)) 1047 return 1; 1048 1049 noffset = fit_conf_get_prop_node(fit_hdr, 1050 noffset, 1051 FIT_SCRIPT_PROP, 1052 IH_PHASE_NONE); 1053 if (noffset < 0) { 1054 if (!confname) 1055 goto fallback; 1056 printf("Could not find script in %s\n", confname); 1057 return 1; 1058 } 1059 } else { 1060fallback: 1061 if (!fit_uname || !*fit_uname) 1062 fit_uname = get_default_image(fit_hdr); 1063 if (!fit_uname) { 1064 puts("No FIT subimage unit name\n"); 1065 return 1; 1066 } 1067 1068 /* get script component image node offset */ 1069 noffset = fit_image_get_node(fit_hdr, fit_uname); 1070 if (noffset < 0) { 1071 printf("Can't find '%s' FIT subimage\n", 1072 fit_uname); 1073 return 1; 1074 } 1075 } 1076 1077 if (!fit_image_check_type(fit_hdr, noffset, 1078 IH_TYPE_SCRIPT)) { 1079 puts("Not a image image\n"); 1080 return 1; 1081 } 1082 1083 /* verify integrity */ 1084 if (verify && !fit_image_verify(fit_hdr, noffset)) { 1085 puts("Bad Data Hash\n"); 1086 return 1; 1087 } 1088 1089 /* get script subimage data address and length */ 1090 if (fit_image_get_data(fit_hdr, noffset, &fit_data, 1091 &fit_len)) { 1092 puts("Could not find script subimage data\n"); 1093 return 1; 1094 } 1095 1096 data = (u32 *)fit_data; 1097 len = (ulong)fit_len; 1098 } 1099 break; 1100 default: 1101 goto exit_image_format; 1102 } 1103 1104 *datap = (char *)data; 1105 *lenp = len; 1106 1107 return 0; 1108 1109exit_image_format: 1110 puts("Wrong image format for \"source\" command\n"); 1111 return -EPERM; 1112}