1 /* 2 * (C) Copyright 2008 Semihalf 3 * 4 * (C) Copyright 2000-2006 5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de. 6 * 7 * See file CREDITS for list of people who contributed to this 8 * project. 9 * 10 * This program is free software; you can redistribute it and/or 11 * modify it under the terms of the GNU General Public License as 12 * published by the Free Software Foundation; either version 2 of 13 * the License, or (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public License 21 * along with this program; if not, write to the Free Software 22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 23 * MA 02111-1307 USA 24 */ 25 26 #ifndef USE_HOSTCC 27 #include <common.h> 28 #include <watchdog.h> 29 30 #ifdef CONFIG_SHOW_BOOT_PROGRESS 31 #include <status_led.h> 32 #endif 33 34 #ifdef CONFIG_HAS_DATAFLASH 35 #include <dataflash.h> 36 #endif 37 38 #ifdef CONFIG_LOGBUFFER 39 #include <logbuff.h> 40 #endif 41 42 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) 43 #include <rtc.h> 44 #endif 45 46 #include <image.h> 47 48 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT) 49 #include <fdt.h> 50 #include <libfdt.h> 51 #include <fdt_support.h> 52 #endif 53 54 #if defined(CONFIG_FIT) 55 #include <u-boot/md5.h> 56 #include <sha1.h> 57 58 static int fit_check_ramdisk(const void *fit, int os_noffset, 59 uint8_t arch, int verify); 60 #endif 61 62 #ifdef CONFIG_CMD_BDI 63 extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]); 64 #endif 65 66 DECLARE_GLOBAL_DATA_PTR; 67 68 static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch, 69 int verify); 70 #else 71 #include "mkimage.h" 72 #include <u-boot/md5.h> 73 #include <time.h> 74 #include <image.h> 75 #endif /* !USE_HOSTCC*/ 76 77 static const table_entry_t uimage_arch[] = { 78 { IH_ARCH_INVALID, NULL, "Invalid ARCH", }, 79 { IH_ARCH_ALPHA, "alpha", "Alpha", }, 80 { IH_ARCH_ARM, "arm", "ARM", }, 81 { IH_ARCH_I386, "x86", "Intel x86", }, 82 { IH_ARCH_IA64, "ia64", "IA64", }, 83 { IH_ARCH_M68K, "m68k", "M68K", }, 84 { IH_ARCH_MICROBLAZE, "microblaze", "MicroBlaze", }, 85 { IH_ARCH_MIPS, "mips", "MIPS", }, 86 { IH_ARCH_MIPS64, "mips64", "MIPS 64 Bit", }, 87 { IH_ARCH_NIOS2, "nios2", "NIOS II", }, 88 { IH_ARCH_PPC, "powerpc", "PowerPC", }, 89 { IH_ARCH_PPC, "ppc", "PowerPC", }, 90 { IH_ARCH_S390, "s390", "IBM S390", }, 91 { IH_ARCH_SH, "sh", "SuperH", }, 92 { IH_ARCH_SPARC, "sparc", "SPARC", }, 93 { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", }, 94 { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", }, 95 { IH_ARCH_AVR32, "avr32", "AVR32", }, 96 { IH_ARCH_NDS32, "nds32", "NDS32", }, 97 { IH_ARCH_OPENRISC, "or1k", "OpenRISC 1000",}, 98 { -1, "", "", }, 99 }; 100 101 static const table_entry_t uimage_os[] = { 102 { IH_OS_INVALID, NULL, "Invalid OS", }, 103 { IH_OS_LINUX, "linux", "Linux", }, 104 #if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC) 105 { IH_OS_LYNXOS, "lynxos", "LynxOS", }, 106 #endif 107 { IH_OS_NETBSD, "netbsd", "NetBSD", }, 108 { IH_OS_OSE, "ose", "Enea OSE", }, 109 { IH_OS_RTEMS, "rtems", "RTEMS", }, 110 { IH_OS_U_BOOT, "u-boot", "U-Boot", }, 111 #if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC) 112 { IH_OS_QNX, "qnx", "QNX", }, 113 { IH_OS_VXWORKS, "vxworks", "VxWorks", }, 114 #endif 115 #if defined(CONFIG_INTEGRITY) || defined(USE_HOSTCC) 116 { IH_OS_INTEGRITY,"integrity", "INTEGRITY", }, 117 #endif 118 #ifdef USE_HOSTCC 119 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", }, 120 { IH_OS_DELL, "dell", "Dell", }, 121 { IH_OS_ESIX, "esix", "Esix", }, 122 { IH_OS_FREEBSD, "freebsd", "FreeBSD", }, 123 { IH_OS_IRIX, "irix", "Irix", }, 124 { IH_OS_NCR, "ncr", "NCR", }, 125 { IH_OS_OPENBSD, "openbsd", "OpenBSD", }, 126 { IH_OS_PSOS, "psos", "pSOS", }, 127 { IH_OS_SCO, "sco", "SCO", }, 128 { IH_OS_SOLARIS, "solaris", "Solaris", }, 129 { IH_OS_SVR4, "svr4", "SVR4", }, 130 #endif 131 { -1, "", "", }, 132 }; 133 134 static const table_entry_t uimage_type[] = { 135 { IH_TYPE_AISIMAGE, "aisimage", "Davinci AIS image",}, 136 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", }, 137 { IH_TYPE_FIRMWARE, "firmware", "Firmware", }, 138 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", }, 139 { IH_TYPE_KERNEL, "kernel", "Kernel Image", }, 140 { IH_TYPE_KERNEL_NOLOAD, "kernel_noload", "Kernel Image (no loading done)", }, 141 { IH_TYPE_KWBIMAGE, "kwbimage", "Kirkwood Boot Image",}, 142 { IH_TYPE_IMXIMAGE, "imximage", "Freescale i.MX Boot Image",}, 143 { IH_TYPE_INVALID, NULL, "Invalid Image", }, 144 { IH_TYPE_MULTI, "multi", "Multi-File Image", }, 145 { IH_TYPE_OMAPIMAGE, "omapimage", "TI OMAP SPL With GP CH",}, 146 { IH_TYPE_PBLIMAGE, "pblimage", "Freescale PBL Boot Image",}, 147 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", }, 148 { IH_TYPE_SCRIPT, "script", "Script", }, 149 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", }, 150 { IH_TYPE_UBLIMAGE, "ublimage", "Davinci UBL image",}, 151 { -1, "", "", }, 152 }; 153 154 static const table_entry_t uimage_comp[] = { 155 { IH_COMP_NONE, "none", "uncompressed", }, 156 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", }, 157 { IH_COMP_GZIP, "gzip", "gzip compressed", }, 158 { IH_COMP_LZMA, "lzma", "lzma compressed", }, 159 { IH_COMP_LZO, "lzo", "lzo compressed", }, 160 { -1, "", "", }, 161 }; 162 163 uint32_t crc32(uint32_t, const unsigned char *, uint); 164 uint32_t crc32_wd(uint32_t, const unsigned char *, uint, uint); 165 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC) 166 static void genimg_print_time(time_t timestamp); 167 #endif 168 169 /*****************************************************************************/ 170 /* Legacy format routines */ 171 /*****************************************************************************/ 172 int image_check_hcrc(const image_header_t *hdr) 173 { 174 ulong hcrc; 175 ulong len = image_get_header_size(); 176 image_header_t header; 177 178 /* Copy header so we can blank CRC field for re-calculation */ 179 memmove(&header, (char *)hdr, image_get_header_size()); 180 image_set_hcrc(&header, 0); 181 182 hcrc = crc32(0, (unsigned char *)&header, len); 183 184 return (hcrc == image_get_hcrc(hdr)); 185 } 186 187 int image_check_dcrc(const image_header_t *hdr) 188 { 189 ulong data = image_get_data(hdr); 190 ulong len = image_get_data_size(hdr); 191 ulong dcrc = crc32_wd(0, (unsigned char *)data, len, CHUNKSZ_CRC32); 192 193 return (dcrc == image_get_dcrc(hdr)); 194 } 195 196 /** 197 * image_multi_count - get component (sub-image) count 198 * @hdr: pointer to the header of the multi component image 199 * 200 * image_multi_count() returns number of components in a multi 201 * component image. 202 * 203 * Note: no checking of the image type is done, caller must pass 204 * a valid multi component image. 205 * 206 * returns: 207 * number of components 208 */ 209 ulong image_multi_count(const image_header_t *hdr) 210 { 211 ulong i, count = 0; 212 uint32_t *size; 213 214 /* get start of the image payload, which in case of multi 215 * component images that points to a table of component sizes */ 216 size = (uint32_t *)image_get_data(hdr); 217 218 /* count non empty slots */ 219 for (i = 0; size[i]; ++i) 220 count++; 221 222 return count; 223 } 224 225 /** 226 * image_multi_getimg - get component data address and size 227 * @hdr: pointer to the header of the multi component image 228 * @idx: index of the requested component 229 * @data: pointer to a ulong variable, will hold component data address 230 * @len: pointer to a ulong variable, will hold component size 231 * 232 * image_multi_getimg() returns size and data address for the requested 233 * component in a multi component image. 234 * 235 * Note: no checking of the image type is done, caller must pass 236 * a valid multi component image. 237 * 238 * returns: 239 * data address and size of the component, if idx is valid 240 * 0 in data and len, if idx is out of range 241 */ 242 void image_multi_getimg(const image_header_t *hdr, ulong idx, 243 ulong *data, ulong *len) 244 { 245 int i; 246 uint32_t *size; 247 ulong offset, count, img_data; 248 249 /* get number of component */ 250 count = image_multi_count(hdr); 251 252 /* get start of the image payload, which in case of multi 253 * component images that points to a table of component sizes */ 254 size = (uint32_t *)image_get_data(hdr); 255 256 /* get address of the proper component data start, which means 257 * skipping sizes table (add 1 for last, null entry) */ 258 img_data = image_get_data(hdr) + (count + 1) * sizeof(uint32_t); 259 260 if (idx < count) { 261 *len = uimage_to_cpu(size[idx]); 262 offset = 0; 263 264 /* go over all indices preceding requested component idx */ 265 for (i = 0; i < idx; i++) { 266 /* add up i-th component size, rounding up to 4 bytes */ 267 offset += (uimage_to_cpu(size[i]) + 3) & ~3 ; 268 } 269 270 /* calculate idx-th component data address */ 271 *data = img_data + offset; 272 } else { 273 *len = 0; 274 *data = 0; 275 } 276 } 277 278 static void image_print_type(const image_header_t *hdr) 279 { 280 const char *os, *arch, *type, *comp; 281 282 os = genimg_get_os_name(image_get_os(hdr)); 283 arch = genimg_get_arch_name(image_get_arch(hdr)); 284 type = genimg_get_type_name(image_get_type(hdr)); 285 comp = genimg_get_comp_name(image_get_comp(hdr)); 286 287 printf("%s %s %s (%s)\n", arch, os, type, comp); 288 } 289 290 /** 291 * image_print_contents - prints out the contents of the legacy format image 292 * @ptr: pointer to the legacy format image header 293 * @p: pointer to prefix string 294 * 295 * image_print_contents() formats a multi line legacy image contents description. 296 * The routine prints out all header fields followed by the size/offset data 297 * for MULTI/SCRIPT images. 298 * 299 * returns: 300 * no returned results 301 */ 302 void image_print_contents(const void *ptr) 303 { 304 const image_header_t *hdr = (const image_header_t *)ptr; 305 const char *p; 306 307 #ifdef USE_HOSTCC 308 p = ""; 309 #else 310 p = " "; 311 #endif 312 313 printf("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name(hdr)); 314 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC) 315 printf("%sCreated: ", p); 316 genimg_print_time((time_t)image_get_time(hdr)); 317 #endif 318 printf("%sImage Type: ", p); 319 image_print_type(hdr); 320 printf("%sData Size: ", p); 321 genimg_print_size(image_get_data_size(hdr)); 322 printf("%sLoad Address: %08x\n", p, image_get_load(hdr)); 323 printf("%sEntry Point: %08x\n", p, image_get_ep(hdr)); 324 325 if (image_check_type(hdr, IH_TYPE_MULTI) || 326 image_check_type(hdr, IH_TYPE_SCRIPT)) { 327 int i; 328 ulong data, len; 329 ulong count = image_multi_count(hdr); 330 331 printf("%sContents:\n", p); 332 for (i = 0; i < count; i++) { 333 image_multi_getimg(hdr, i, &data, &len); 334 335 printf("%s Image %d: ", p, i); 336 genimg_print_size(len); 337 338 if (image_check_type(hdr, IH_TYPE_SCRIPT) && i > 0) { 339 /* 340 * the user may need to know offsets 341 * if planning to do something with 342 * multiple files 343 */ 344 printf("%s Offset = 0x%08lx\n", p, data); 345 } 346 } 347 } 348 } 349 350 351 #ifndef USE_HOSTCC 352 /** 353 * image_get_ramdisk - get and verify ramdisk image 354 * @rd_addr: ramdisk image start address 355 * @arch: expected ramdisk architecture 356 * @verify: checksum verification flag 357 * 358 * image_get_ramdisk() returns a pointer to the verified ramdisk image 359 * header. Routine receives image start address and expected architecture 360 * flag. Verification done covers data and header integrity and os/type/arch 361 * fields checking. 362 * 363 * If dataflash support is enabled routine checks for dataflash addresses 364 * and handles required dataflash reads. 365 * 366 * returns: 367 * pointer to a ramdisk image header, if image was found and valid 368 * otherwise, return NULL 369 */ 370 static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch, 371 int verify) 372 { 373 const image_header_t *rd_hdr = (const image_header_t *)rd_addr; 374 375 if (!image_check_magic(rd_hdr)) { 376 puts("Bad Magic Number\n"); 377 bootstage_error(BOOTSTAGE_ID_RD_MAGIC); 378 return NULL; 379 } 380 381 if (!image_check_hcrc(rd_hdr)) { 382 puts("Bad Header Checksum\n"); 383 bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM); 384 return NULL; 385 } 386 387 bootstage_mark(BOOTSTAGE_ID_RD_MAGIC); 388 image_print_contents(rd_hdr); 389 390 if (verify) { 391 puts(" Verifying Checksum ... "); 392 if (!image_check_dcrc(rd_hdr)) { 393 puts("Bad Data CRC\n"); 394 bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM); 395 return NULL; 396 } 397 puts("OK\n"); 398 } 399 400 bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM); 401 402 if (!image_check_os(rd_hdr, IH_OS_LINUX) || 403 !image_check_arch(rd_hdr, arch) || 404 !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) { 405 printf("No Linux %s Ramdisk Image\n", 406 genimg_get_arch_name(arch)); 407 bootstage_error(BOOTSTAGE_ID_RAMDISK); 408 return NULL; 409 } 410 411 return rd_hdr; 412 } 413 #endif /* !USE_HOSTCC */ 414 415 /*****************************************************************************/ 416 /* Shared dual-format routines */ 417 /*****************************************************************************/ 418 #ifndef USE_HOSTCC 419 ulong getenv_bootm_low(void) 420 { 421 char *s = getenv("bootm_low"); 422 if (s) { 423 ulong tmp = simple_strtoul(s, NULL, 16); 424 return tmp; 425 } 426 427 #if defined(CONFIG_SYS_SDRAM_BASE) 428 return CONFIG_SYS_SDRAM_BASE; 429 #elif defined(CONFIG_ARM) 430 return gd->bd->bi_dram[0].start; 431 #else 432 return 0; 433 #endif 434 } 435 436 phys_size_t getenv_bootm_size(void) 437 { 438 phys_size_t tmp; 439 char *s = getenv("bootm_size"); 440 if (s) { 441 tmp = (phys_size_t)simple_strtoull(s, NULL, 16); 442 return tmp; 443 } 444 s = getenv("bootm_low"); 445 if (s) 446 tmp = (phys_size_t)simple_strtoull(s, NULL, 16); 447 else 448 tmp = 0; 449 450 451 #if defined(CONFIG_ARM) 452 return gd->bd->bi_dram[0].size - tmp; 453 #else 454 return gd->bd->bi_memsize - tmp; 455 #endif 456 } 457 458 phys_size_t getenv_bootm_mapsize(void) 459 { 460 phys_size_t tmp; 461 char *s = getenv("bootm_mapsize"); 462 if (s) { 463 tmp = (phys_size_t)simple_strtoull(s, NULL, 16); 464 return tmp; 465 } 466 467 #if defined(CONFIG_SYS_BOOTMAPSZ) 468 return CONFIG_SYS_BOOTMAPSZ; 469 #else 470 return getenv_bootm_size(); 471 #endif 472 } 473 474 void memmove_wd(void *to, void *from, size_t len, ulong chunksz) 475 { 476 if (to == from) 477 return; 478 479 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG) 480 while (len > 0) { 481 size_t tail = (len > chunksz) ? chunksz : len; 482 WATCHDOG_RESET(); 483 memmove(to, from, tail); 484 to += tail; 485 from += tail; 486 len -= tail; 487 } 488 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */ 489 memmove(to, from, len); 490 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */ 491 } 492 #endif /* !USE_HOSTCC */ 493 494 void genimg_print_size(uint32_t size) 495 { 496 #ifndef USE_HOSTCC 497 printf("%d Bytes = ", size); 498 print_size(size, "\n"); 499 #else 500 printf("%d Bytes = %.2f kB = %.2f MB\n", 501 size, (double)size / 1.024e3, 502 (double)size / 1.048576e6); 503 #endif 504 } 505 506 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC) 507 static void genimg_print_time(time_t timestamp) 508 { 509 #ifndef USE_HOSTCC 510 struct rtc_time tm; 511 512 to_tm(timestamp, &tm); 513 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n", 514 tm.tm_year, tm.tm_mon, tm.tm_mday, 515 tm.tm_hour, tm.tm_min, tm.tm_sec); 516 #else 517 printf("%s", ctime(×tamp)); 518 #endif 519 } 520 #endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */ 521 522 /** 523 * get_table_entry_name - translate entry id to long name 524 * @table: pointer to a translation table for entries of a specific type 525 * @msg: message to be returned when translation fails 526 * @id: entry id to be translated 527 * 528 * get_table_entry_name() will go over translation table trying to find 529 * entry that matches given id. If matching entry is found, its long 530 * name is returned to the caller. 531 * 532 * returns: 533 * long entry name if translation succeeds 534 * msg otherwise 535 */ 536 char *get_table_entry_name(const table_entry_t *table, char *msg, int id) 537 { 538 for (; table->id >= 0; ++table) { 539 if (table->id == id) 540 #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC) 541 return table->lname; 542 #else 543 return table->lname + gd->reloc_off; 544 #endif 545 } 546 return (msg); 547 } 548 549 const char *genimg_get_os_name(uint8_t os) 550 { 551 return (get_table_entry_name(uimage_os, "Unknown OS", os)); 552 } 553 554 const char *genimg_get_arch_name(uint8_t arch) 555 { 556 return (get_table_entry_name(uimage_arch, "Unknown Architecture", 557 arch)); 558 } 559 560 const char *genimg_get_type_name(uint8_t type) 561 { 562 return (get_table_entry_name(uimage_type, "Unknown Image", type)); 563 } 564 565 const char *genimg_get_comp_name(uint8_t comp) 566 { 567 return (get_table_entry_name(uimage_comp, "Unknown Compression", 568 comp)); 569 } 570 571 /** 572 * get_table_entry_id - translate short entry name to id 573 * @table: pointer to a translation table for entries of a specific type 574 * @table_name: to be used in case of error 575 * @name: entry short name to be translated 576 * 577 * get_table_entry_id() will go over translation table trying to find 578 * entry that matches given short name. If matching entry is found, 579 * its id returned to the caller. 580 * 581 * returns: 582 * entry id if translation succeeds 583 * -1 otherwise 584 */ 585 int get_table_entry_id(const table_entry_t *table, 586 const char *table_name, const char *name) 587 { 588 const table_entry_t *t; 589 #ifdef USE_HOSTCC 590 int first = 1; 591 592 for (t = table; t->id >= 0; ++t) { 593 if (t->sname && strcasecmp(t->sname, name) == 0) 594 return(t->id); 595 } 596 597 fprintf(stderr, "\nInvalid %s Type - valid names are", table_name); 598 for (t = table; t->id >= 0; ++t) { 599 if (t->sname == NULL) 600 continue; 601 fprintf(stderr, "%c %s", (first) ? ':' : ',', t->sname); 602 first = 0; 603 } 604 fprintf(stderr, "\n"); 605 #else 606 for (t = table; t->id >= 0; ++t) { 607 #ifdef CONFIG_NEEDS_MANUAL_RELOC 608 if (t->sname && strcmp(t->sname + gd->reloc_off, name) == 0) 609 #else 610 if (t->sname && strcmp(t->sname, name) == 0) 611 #endif 612 return (t->id); 613 } 614 debug("Invalid %s Type: %s\n", table_name, name); 615 #endif /* USE_HOSTCC */ 616 return (-1); 617 } 618 619 int genimg_get_os_id(const char *name) 620 { 621 return (get_table_entry_id(uimage_os, "OS", name)); 622 } 623 624 int genimg_get_arch_id(const char *name) 625 { 626 return (get_table_entry_id(uimage_arch, "CPU", name)); 627 } 628 629 int genimg_get_type_id(const char *name) 630 { 631 return (get_table_entry_id(uimage_type, "Image", name)); 632 } 633 634 int genimg_get_comp_id(const char *name) 635 { 636 return (get_table_entry_id(uimage_comp, "Compression", name)); 637 } 638 639 #ifndef USE_HOSTCC 640 /** 641 * genimg_get_format - get image format type 642 * @img_addr: image start address 643 * 644 * genimg_get_format() checks whether provided address points to a valid 645 * legacy or FIT image. 646 * 647 * New uImage format and FDT blob are based on a libfdt. FDT blob 648 * may be passed directly or embedded in a FIT image. In both situations 649 * genimg_get_format() must be able to dectect libfdt header. 650 * 651 * returns: 652 * image format type or IMAGE_FORMAT_INVALID if no image is present 653 */ 654 int genimg_get_format(void *img_addr) 655 { 656 ulong format = IMAGE_FORMAT_INVALID; 657 const image_header_t *hdr; 658 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT) 659 char *fit_hdr; 660 #endif 661 662 hdr = (const image_header_t *)img_addr; 663 if (image_check_magic(hdr)) 664 format = IMAGE_FORMAT_LEGACY; 665 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT) 666 else { 667 fit_hdr = (char *)img_addr; 668 if (fdt_check_header(fit_hdr) == 0) 669 format = IMAGE_FORMAT_FIT; 670 } 671 #endif 672 673 return format; 674 } 675 676 /** 677 * genimg_get_image - get image from special storage (if necessary) 678 * @img_addr: image start address 679 * 680 * genimg_get_image() checks if provided image start adddress is located 681 * in a dataflash storage. If so, image is moved to a system RAM memory. 682 * 683 * returns: 684 * image start address after possible relocation from special storage 685 */ 686 ulong genimg_get_image(ulong img_addr) 687 { 688 ulong ram_addr = img_addr; 689 690 #ifdef CONFIG_HAS_DATAFLASH 691 ulong h_size, d_size; 692 693 if (addr_dataflash(img_addr)) { 694 /* ger RAM address */ 695 ram_addr = CONFIG_SYS_LOAD_ADDR; 696 697 /* get header size */ 698 h_size = image_get_header_size(); 699 #if defined(CONFIG_FIT) 700 if (sizeof(struct fdt_header) > h_size) 701 h_size = sizeof(struct fdt_header); 702 #endif 703 704 /* read in header */ 705 debug(" Reading image header from dataflash address " 706 "%08lx to RAM address %08lx\n", img_addr, ram_addr); 707 708 read_dataflash(img_addr, h_size, (char *)ram_addr); 709 710 /* get data size */ 711 switch (genimg_get_format((void *)ram_addr)) { 712 case IMAGE_FORMAT_LEGACY: 713 d_size = image_get_data_size( 714 (const image_header_t *)ram_addr); 715 debug(" Legacy format image found at 0x%08lx, " 716 "size 0x%08lx\n", 717 ram_addr, d_size); 718 break; 719 #if defined(CONFIG_FIT) 720 case IMAGE_FORMAT_FIT: 721 d_size = fit_get_size((const void *)ram_addr) - h_size; 722 debug(" FIT/FDT format image found at 0x%08lx, " 723 "size 0x%08lx\n", 724 ram_addr, d_size); 725 break; 726 #endif 727 default: 728 printf(" No valid image found at 0x%08lx\n", 729 img_addr); 730 return ram_addr; 731 } 732 733 /* read in image data */ 734 debug(" Reading image remaining data from dataflash address " 735 "%08lx to RAM address %08lx\n", img_addr + h_size, 736 ram_addr + h_size); 737 738 read_dataflash(img_addr + h_size, d_size, 739 (char *)(ram_addr + h_size)); 740 741 } 742 #endif /* CONFIG_HAS_DATAFLASH */ 743 744 return ram_addr; 745 } 746 747 /** 748 * fit_has_config - check if there is a valid FIT configuration 749 * @images: pointer to the bootm command headers structure 750 * 751 * fit_has_config() checks if there is a FIT configuration in use 752 * (if FTI support is present). 753 * 754 * returns: 755 * 0, no FIT support or no configuration found 756 * 1, configuration found 757 */ 758 int genimg_has_config(bootm_headers_t *images) 759 { 760 #if defined(CONFIG_FIT) 761 if (images->fit_uname_cfg) 762 return 1; 763 #endif 764 return 0; 765 } 766 767 /** 768 * boot_get_ramdisk - main ramdisk handling routine 769 * @argc: command argument count 770 * @argv: command argument list 771 * @images: pointer to the bootm images structure 772 * @arch: expected ramdisk architecture 773 * @rd_start: pointer to a ulong variable, will hold ramdisk start address 774 * @rd_end: pointer to a ulong variable, will hold ramdisk end 775 * 776 * boot_get_ramdisk() is responsible for finding a valid ramdisk image. 777 * Curently supported are the following ramdisk sources: 778 * - multicomponent kernel/ramdisk image, 779 * - commandline provided address of decicated ramdisk image. 780 * 781 * returns: 782 * 0, if ramdisk image was found and valid, or skiped 783 * rd_start and rd_end are set to ramdisk start/end addresses if 784 * ramdisk image is found and valid 785 * 786 * 1, if ramdisk image is found but corrupted, or invalid 787 * rd_start and rd_end are set to 0 if no ramdisk exists 788 */ 789 int boot_get_ramdisk(int argc, char * const argv[], bootm_headers_t *images, 790 uint8_t arch, ulong *rd_start, ulong *rd_end) 791 { 792 ulong rd_addr, rd_load; 793 ulong rd_data, rd_len; 794 const image_header_t *rd_hdr; 795 #ifdef CONFIG_SUPPORT_RAW_INITRD 796 char *end; 797 #endif 798 #if defined(CONFIG_FIT) 799 void *fit_hdr; 800 const char *fit_uname_config = NULL; 801 const char *fit_uname_ramdisk = NULL; 802 ulong default_addr; 803 int rd_noffset; 804 int cfg_noffset; 805 const void *data; 806 size_t size; 807 #endif 808 809 *rd_start = 0; 810 *rd_end = 0; 811 812 /* 813 * Look for a '-' which indicates to ignore the 814 * ramdisk argument 815 */ 816 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) { 817 debug("## Skipping init Ramdisk\n"); 818 rd_len = rd_data = 0; 819 } else if (argc >= 3 || genimg_has_config(images)) { 820 #if defined(CONFIG_FIT) 821 if (argc >= 3) { 822 /* 823 * If the init ramdisk comes from the FIT image and 824 * the FIT image address is omitted in the command 825 * line argument, try to use os FIT image address or 826 * default load address. 827 */ 828 if (images->fit_uname_os) 829 default_addr = (ulong)images->fit_hdr_os; 830 else 831 default_addr = load_addr; 832 833 if (fit_parse_conf(argv[2], default_addr, 834 &rd_addr, &fit_uname_config)) { 835 debug("* ramdisk: config '%s' from image at " 836 "0x%08lx\n", 837 fit_uname_config, rd_addr); 838 } else if (fit_parse_subimage(argv[2], default_addr, 839 &rd_addr, &fit_uname_ramdisk)) { 840 debug("* ramdisk: subimage '%s' from image at " 841 "0x%08lx\n", 842 fit_uname_ramdisk, rd_addr); 843 } else 844 #endif 845 { 846 rd_addr = simple_strtoul(argv[2], NULL, 16); 847 debug("* ramdisk: cmdline image address = " 848 "0x%08lx\n", 849 rd_addr); 850 } 851 #if defined(CONFIG_FIT) 852 } else { 853 /* use FIT configuration provided in first bootm 854 * command argument 855 */ 856 rd_addr = (ulong)images->fit_hdr_os; 857 fit_uname_config = images->fit_uname_cfg; 858 debug("* ramdisk: using config '%s' from image " 859 "at 0x%08lx\n", 860 fit_uname_config, rd_addr); 861 862 /* 863 * Check whether configuration has ramdisk defined, 864 * if not, don't try to use it, quit silently. 865 */ 866 fit_hdr = (void *)rd_addr; 867 cfg_noffset = fit_conf_get_node(fit_hdr, 868 fit_uname_config); 869 if (cfg_noffset < 0) { 870 debug("* ramdisk: no such config\n"); 871 return 1; 872 } 873 874 rd_noffset = fit_conf_get_ramdisk_node(fit_hdr, 875 cfg_noffset); 876 if (rd_noffset < 0) { 877 debug("* ramdisk: no ramdisk in config\n"); 878 return 0; 879 } 880 } 881 #endif 882 883 /* copy from dataflash if needed */ 884 rd_addr = genimg_get_image(rd_addr); 885 886 /* 887 * Check if there is an initrd image at the 888 * address provided in the second bootm argument 889 * check image type, for FIT images get FIT node. 890 */ 891 switch (genimg_get_format((void *)rd_addr)) { 892 case IMAGE_FORMAT_LEGACY: 893 printf("## Loading init Ramdisk from Legacy " 894 "Image at %08lx ...\n", rd_addr); 895 896 bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK); 897 rd_hdr = image_get_ramdisk(rd_addr, arch, 898 images->verify); 899 900 if (rd_hdr == NULL) 901 return 1; 902 903 rd_data = image_get_data(rd_hdr); 904 rd_len = image_get_data_size(rd_hdr); 905 rd_load = image_get_load(rd_hdr); 906 break; 907 #if defined(CONFIG_FIT) 908 case IMAGE_FORMAT_FIT: 909 fit_hdr = (void *)rd_addr; 910 printf("## Loading init Ramdisk from FIT " 911 "Image at %08lx ...\n", rd_addr); 912 913 bootstage_mark(BOOTSTAGE_ID_FIT_RD_FORMAT); 914 if (!fit_check_format(fit_hdr)) { 915 puts("Bad FIT ramdisk image format!\n"); 916 bootstage_error( 917 BOOTSTAGE_ID_FIT_RD_FORMAT); 918 return 1; 919 } 920 bootstage_mark(BOOTSTAGE_ID_FIT_RD_FORMAT_OK); 921 922 if (!fit_uname_ramdisk) { 923 /* 924 * no ramdisk image node unit name, try to get config 925 * node first. If config unit node name is NULL 926 * fit_conf_get_node() will try to find default config node 927 */ 928 bootstage_mark( 929 BOOTSTAGE_ID_FIT_RD_NO_UNIT_NAME); 930 cfg_noffset = fit_conf_get_node(fit_hdr, 931 fit_uname_config); 932 if (cfg_noffset < 0) { 933 puts("Could not find configuration " 934 "node\n"); 935 bootstage_error( 936 BOOTSTAGE_ID_FIT_RD_NO_UNIT_NAME); 937 return 1; 938 } 939 fit_uname_config = fdt_get_name(fit_hdr, 940 cfg_noffset, NULL); 941 printf(" Using '%s' configuration\n", 942 fit_uname_config); 943 944 rd_noffset = fit_conf_get_ramdisk_node(fit_hdr, 945 cfg_noffset); 946 fit_uname_ramdisk = fit_get_name(fit_hdr, 947 rd_noffset, NULL); 948 } else { 949 /* get ramdisk component image node offset */ 950 bootstage_mark( 951 BOOTSTAGE_ID_FIT_RD_UNIT_NAME); 952 rd_noffset = fit_image_get_node(fit_hdr, 953 fit_uname_ramdisk); 954 } 955 if (rd_noffset < 0) { 956 puts("Could not find subimage node\n"); 957 bootstage_error(BOOTSTAGE_ID_FIT_RD_SUBNODE); 958 return 1; 959 } 960 961 printf(" Trying '%s' ramdisk subimage\n", 962 fit_uname_ramdisk); 963 964 bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK); 965 if (!fit_check_ramdisk(fit_hdr, rd_noffset, arch, 966 images->verify)) 967 return 1; 968 969 /* get ramdisk image data address and length */ 970 if (fit_image_get_data(fit_hdr, rd_noffset, &data, 971 &size)) { 972 puts("Could not find ramdisk subimage data!\n"); 973 bootstage_error(BOOTSTAGE_ID_FIT_RD_GET_DATA); 974 return 1; 975 } 976 bootstage_mark(BOOTSTAGE_ID_FIT_RD_GET_DATA_OK); 977 978 rd_data = (ulong)data; 979 rd_len = size; 980 981 if (fit_image_get_load(fit_hdr, rd_noffset, &rd_load)) { 982 puts("Can't get ramdisk subimage load " 983 "address!\n"); 984 bootstage_error(BOOTSTAGE_ID_FIT_RD_LOAD); 985 return 1; 986 } 987 bootstage_mark(BOOTSTAGE_ID_FIT_RD_LOAD); 988 989 images->fit_hdr_rd = fit_hdr; 990 images->fit_uname_rd = fit_uname_ramdisk; 991 images->fit_noffset_rd = rd_noffset; 992 break; 993 #endif 994 default: 995 #ifdef CONFIG_SUPPORT_RAW_INITRD 996 if (argc >= 3 && (end = strchr(argv[2], ':'))) { 997 rd_len = simple_strtoul(++end, NULL, 16); 998 rd_data = rd_addr; 999 } else 1000 #endif 1001 { 1002 puts("Wrong Ramdisk Image Format\n"); 1003 rd_data = rd_len = rd_load = 0; 1004 return 1; 1005 } 1006 } 1007 } else if (images->legacy_hdr_valid && 1008 image_check_type(&images->legacy_hdr_os_copy, 1009 IH_TYPE_MULTI)) { 1010 1011 /* 1012 * Now check if we have a legacy mult-component image, 1013 * get second entry data start address and len. 1014 */ 1015 bootstage_mark(BOOTSTAGE_ID_RAMDISK); 1016 printf("## Loading init Ramdisk from multi component " 1017 "Legacy Image at %08lx ...\n", 1018 (ulong)images->legacy_hdr_os); 1019 1020 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len); 1021 } else { 1022 /* 1023 * no initrd image 1024 */ 1025 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK); 1026 rd_len = rd_data = 0; 1027 } 1028 1029 if (!rd_data) { 1030 debug("## No init Ramdisk\n"); 1031 } else { 1032 *rd_start = rd_data; 1033 *rd_end = rd_data + rd_len; 1034 } 1035 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n", 1036 *rd_start, *rd_end); 1037 1038 return 0; 1039 } 1040 1041 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH 1042 /** 1043 * boot_ramdisk_high - relocate init ramdisk 1044 * @lmb: pointer to lmb handle, will be used for memory mgmt 1045 * @rd_data: ramdisk data start address 1046 * @rd_len: ramdisk data length 1047 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk 1048 * start address (after possible relocation) 1049 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk 1050 * end address (after possible relocation) 1051 * 1052 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement 1053 * variable and if requested ramdisk data is moved to a specified location. 1054 * 1055 * Initrd_start and initrd_end are set to final (after relocation) ramdisk 1056 * start/end addresses if ramdisk image start and len were provided, 1057 * otherwise set initrd_start and initrd_end set to zeros. 1058 * 1059 * returns: 1060 * 0 - success 1061 * -1 - failure 1062 */ 1063 int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len, 1064 ulong *initrd_start, ulong *initrd_end) 1065 { 1066 char *s; 1067 ulong initrd_high; 1068 int initrd_copy_to_ram = 1; 1069 1070 if ((s = getenv("initrd_high")) != NULL) { 1071 /* a value of "no" or a similar string will act like 0, 1072 * turning the "load high" feature off. This is intentional. 1073 */ 1074 initrd_high = simple_strtoul(s, NULL, 16); 1075 if (initrd_high == ~0) 1076 initrd_copy_to_ram = 0; 1077 } else { 1078 /* not set, no restrictions to load high */ 1079 initrd_high = ~0; 1080 } 1081 1082 1083 #ifdef CONFIG_LOGBUFFER 1084 /* Prevent initrd from overwriting logbuffer */ 1085 lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE); 1086 #endif 1087 1088 debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n", 1089 initrd_high, initrd_copy_to_ram); 1090 1091 if (rd_data) { 1092 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */ 1093 debug(" in-place initrd\n"); 1094 *initrd_start = rd_data; 1095 *initrd_end = rd_data + rd_len; 1096 lmb_reserve(lmb, rd_data, rd_len); 1097 } else { 1098 if (initrd_high) 1099 *initrd_start = (ulong)lmb_alloc_base(lmb, 1100 rd_len, 0x1000, initrd_high); 1101 else 1102 *initrd_start = (ulong)lmb_alloc(lmb, rd_len, 1103 0x1000); 1104 1105 if (*initrd_start == 0) { 1106 puts("ramdisk - allocation error\n"); 1107 goto error; 1108 } 1109 bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK); 1110 1111 *initrd_end = *initrd_start + rd_len; 1112 printf(" Loading Ramdisk to %08lx, end %08lx ... ", 1113 *initrd_start, *initrd_end); 1114 1115 memmove_wd((void *)*initrd_start, 1116 (void *)rd_data, rd_len, CHUNKSZ); 1117 1118 #ifdef CONFIG_MP 1119 /* 1120 * Ensure the image is flushed to memory to handle 1121 * AMP boot scenarios in which we might not be 1122 * HW cache coherent 1123 */ 1124 flush_cache((unsigned long)*initrd_start, rd_len); 1125 #endif 1126 puts("OK\n"); 1127 } 1128 } else { 1129 *initrd_start = 0; 1130 *initrd_end = 0; 1131 } 1132 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n", 1133 *initrd_start, *initrd_end); 1134 1135 return 0; 1136 1137 error: 1138 return -1; 1139 } 1140 #endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */ 1141 1142 #ifdef CONFIG_OF_LIBFDT 1143 static void fdt_error(const char *msg) 1144 { 1145 puts("ERROR: "); 1146 puts(msg); 1147 puts(" - must RESET the board to recover.\n"); 1148 } 1149 1150 static const image_header_t *image_get_fdt(ulong fdt_addr) 1151 { 1152 const image_header_t *fdt_hdr = (const image_header_t *)fdt_addr; 1153 1154 image_print_contents(fdt_hdr); 1155 1156 puts(" Verifying Checksum ... "); 1157 if (!image_check_hcrc(fdt_hdr)) { 1158 fdt_error("fdt header checksum invalid"); 1159 return NULL; 1160 } 1161 1162 if (!image_check_dcrc(fdt_hdr)) { 1163 fdt_error("fdt checksum invalid"); 1164 return NULL; 1165 } 1166 puts("OK\n"); 1167 1168 if (!image_check_type(fdt_hdr, IH_TYPE_FLATDT)) { 1169 fdt_error("uImage is not a fdt"); 1170 return NULL; 1171 } 1172 if (image_get_comp(fdt_hdr) != IH_COMP_NONE) { 1173 fdt_error("uImage is compressed"); 1174 return NULL; 1175 } 1176 if (fdt_check_header((char *)image_get_data(fdt_hdr)) != 0) { 1177 fdt_error("uImage data is not a fdt"); 1178 return NULL; 1179 } 1180 return fdt_hdr; 1181 } 1182 1183 /** 1184 * fit_check_fdt - verify FIT format FDT subimage 1185 * @fit_hdr: pointer to the FIT header 1186 * fdt_noffset: FDT subimage node offset within FIT image 1187 * @verify: data CRC verification flag 1188 * 1189 * fit_check_fdt() verifies integrity of the FDT subimage and from 1190 * specified FIT image. 1191 * 1192 * returns: 1193 * 1, on success 1194 * 0, on failure 1195 */ 1196 #if defined(CONFIG_FIT) 1197 static int fit_check_fdt(const void *fit, int fdt_noffset, int verify) 1198 { 1199 fit_image_print(fit, fdt_noffset, " "); 1200 1201 if (verify) { 1202 puts(" Verifying Hash Integrity ... "); 1203 if (!fit_image_check_hashes(fit, fdt_noffset)) { 1204 fdt_error("Bad Data Hash"); 1205 return 0; 1206 } 1207 puts("OK\n"); 1208 } 1209 1210 if (!fit_image_check_type(fit, fdt_noffset, IH_TYPE_FLATDT)) { 1211 fdt_error("Not a FDT image"); 1212 return 0; 1213 } 1214 1215 if (!fit_image_check_comp(fit, fdt_noffset, IH_COMP_NONE)) { 1216 fdt_error("FDT image is compressed"); 1217 return 0; 1218 } 1219 1220 return 1; 1221 } 1222 #endif /* CONFIG_FIT */ 1223 1224 #ifndef CONFIG_SYS_FDT_PAD 1225 #define CONFIG_SYS_FDT_PAD 0x3000 1226 #endif 1227 1228 #if defined(CONFIG_OF_LIBFDT) 1229 /** 1230 * boot_fdt_add_mem_rsv_regions - Mark the memreserve sections as unusable 1231 * @lmb: pointer to lmb handle, will be used for memory mgmt 1232 * @fdt_blob: pointer to fdt blob base address 1233 * 1234 * Adds the memreserve regions in the dtb to the lmb block. Adding the 1235 * memreserve regions prevents u-boot from using them to store the initrd 1236 * or the fdt blob. 1237 */ 1238 void boot_fdt_add_mem_rsv_regions(struct lmb *lmb, void *fdt_blob) 1239 { 1240 uint64_t addr, size; 1241 int i, total; 1242 1243 if (fdt_check_header(fdt_blob) != 0) 1244 return; 1245 1246 total = fdt_num_mem_rsv(fdt_blob); 1247 for (i = 0; i < total; i++) { 1248 if (fdt_get_mem_rsv(fdt_blob, i, &addr, &size) != 0) 1249 continue; 1250 printf(" reserving fdt memory region: addr=%llx size=%llx\n", 1251 (unsigned long long)addr, (unsigned long long)size); 1252 lmb_reserve(lmb, addr, size); 1253 } 1254 } 1255 1256 /** 1257 * boot_relocate_fdt - relocate flat device tree 1258 * @lmb: pointer to lmb handle, will be used for memory mgmt 1259 * @of_flat_tree: pointer to a char* variable, will hold fdt start address 1260 * @of_size: pointer to a ulong variable, will hold fdt length 1261 * 1262 * boot_relocate_fdt() allocates a region of memory within the bootmap and 1263 * relocates the of_flat_tree into that region, even if the fdt is already in 1264 * the bootmap. It also expands the size of the fdt by CONFIG_SYS_FDT_PAD 1265 * bytes. 1266 * 1267 * of_flat_tree and of_size are set to final (after relocation) values 1268 * 1269 * returns: 1270 * 0 - success 1271 * 1 - failure 1272 */ 1273 int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size) 1274 { 1275 void *fdt_blob = *of_flat_tree; 1276 void *of_start = NULL; 1277 char *fdt_high; 1278 ulong of_len = 0; 1279 int err; 1280 int disable_relocation = 0; 1281 1282 /* nothing to do */ 1283 if (*of_size == 0) 1284 return 0; 1285 1286 if (fdt_check_header(fdt_blob) != 0) { 1287 fdt_error("image is not a fdt"); 1288 goto error; 1289 } 1290 1291 /* position on a 4K boundary before the alloc_current */ 1292 /* Pad the FDT by a specified amount */ 1293 of_len = *of_size + CONFIG_SYS_FDT_PAD; 1294 1295 /* If fdt_high is set use it to select the relocation address */ 1296 fdt_high = getenv("fdt_high"); 1297 if (fdt_high) { 1298 void *desired_addr = (void *)simple_strtoul(fdt_high, NULL, 16); 1299 1300 if (((ulong) desired_addr) == ~0UL) { 1301 /* All ones means use fdt in place */ 1302 of_start = fdt_blob; 1303 lmb_reserve(lmb, (ulong)of_start, of_len); 1304 disable_relocation = 1; 1305 } else if (desired_addr) { 1306 of_start = 1307 (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000, 1308 (ulong)desired_addr); 1309 if (of_start == NULL) { 1310 puts("Failed using fdt_high value for Device Tree"); 1311 goto error; 1312 } 1313 } else { 1314 of_start = 1315 (void *)(ulong) lmb_alloc(lmb, of_len, 0x1000); 1316 } 1317 } else { 1318 of_start = 1319 (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000, 1320 getenv_bootm_mapsize() 1321 + getenv_bootm_low()); 1322 } 1323 1324 if (of_start == NULL) { 1325 puts("device tree - allocation error\n"); 1326 goto error; 1327 } 1328 1329 if (disable_relocation) { 1330 /* We assume there is space after the existing fdt to use for padding */ 1331 fdt_set_totalsize(of_start, of_len); 1332 printf(" Using Device Tree in place at %p, end %p\n", 1333 of_start, of_start + of_len - 1); 1334 } else { 1335 debug("## device tree at %p ... %p (len=%ld [0x%lX])\n", 1336 fdt_blob, fdt_blob + *of_size - 1, of_len, of_len); 1337 1338 printf(" Loading Device Tree to %p, end %p ... ", 1339 of_start, of_start + of_len - 1); 1340 1341 err = fdt_open_into(fdt_blob, of_start, of_len); 1342 if (err != 0) { 1343 fdt_error("fdt move failed"); 1344 goto error; 1345 } 1346 puts("OK\n"); 1347 } 1348 1349 *of_flat_tree = of_start; 1350 *of_size = of_len; 1351 1352 set_working_fdt_addr(*of_flat_tree); 1353 return 0; 1354 1355 error: 1356 return 1; 1357 } 1358 #endif /* CONFIG_OF_LIBFDT */ 1359 1360 /** 1361 * boot_get_fdt - main fdt handling routine 1362 * @argc: command argument count 1363 * @argv: command argument list 1364 * @images: pointer to the bootm images structure 1365 * @of_flat_tree: pointer to a char* variable, will hold fdt start address 1366 * @of_size: pointer to a ulong variable, will hold fdt length 1367 * 1368 * boot_get_fdt() is responsible for finding a valid flat device tree image. 1369 * Curently supported are the following ramdisk sources: 1370 * - multicomponent kernel/ramdisk image, 1371 * - commandline provided address of decicated ramdisk image. 1372 * 1373 * returns: 1374 * 0, if fdt image was found and valid, or skipped 1375 * of_flat_tree and of_size are set to fdt start address and length if 1376 * fdt image is found and valid 1377 * 1378 * 1, if fdt image is found but corrupted 1379 * of_flat_tree and of_size are set to 0 if no fdt exists 1380 */ 1381 int boot_get_fdt(int flag, int argc, char * const argv[], 1382 bootm_headers_t *images, char **of_flat_tree, ulong *of_size) 1383 { 1384 const image_header_t *fdt_hdr; 1385 ulong fdt_addr; 1386 char *fdt_blob = NULL; 1387 ulong image_start, image_data, image_end; 1388 ulong load_start, load_end; 1389 #if defined(CONFIG_FIT) 1390 void *fit_hdr; 1391 const char *fit_uname_config = NULL; 1392 const char *fit_uname_fdt = NULL; 1393 ulong default_addr; 1394 int cfg_noffset; 1395 int fdt_noffset; 1396 const void *data; 1397 size_t size; 1398 #endif 1399 1400 *of_flat_tree = NULL; 1401 *of_size = 0; 1402 1403 if (argc > 3 || genimg_has_config(images)) { 1404 #if defined(CONFIG_FIT) 1405 if (argc > 3) { 1406 /* 1407 * If the FDT blob comes from the FIT image and the 1408 * FIT image address is omitted in the command line 1409 * argument, try to use ramdisk or os FIT image 1410 * address or default load address. 1411 */ 1412 if (images->fit_uname_rd) 1413 default_addr = (ulong)images->fit_hdr_rd; 1414 else if (images->fit_uname_os) 1415 default_addr = (ulong)images->fit_hdr_os; 1416 else 1417 default_addr = load_addr; 1418 1419 if (fit_parse_conf(argv[3], default_addr, 1420 &fdt_addr, &fit_uname_config)) { 1421 debug("* fdt: config '%s' from image at " 1422 "0x%08lx\n", 1423 fit_uname_config, fdt_addr); 1424 } else if (fit_parse_subimage(argv[3], default_addr, 1425 &fdt_addr, &fit_uname_fdt)) { 1426 debug("* fdt: subimage '%s' from image at " 1427 "0x%08lx\n", 1428 fit_uname_fdt, fdt_addr); 1429 } else 1430 #endif 1431 { 1432 fdt_addr = simple_strtoul(argv[3], NULL, 16); 1433 debug("* fdt: cmdline image address = " 1434 "0x%08lx\n", 1435 fdt_addr); 1436 } 1437 #if defined(CONFIG_FIT) 1438 } else { 1439 /* use FIT configuration provided in first bootm 1440 * command argument 1441 */ 1442 fdt_addr = (ulong)images->fit_hdr_os; 1443 fit_uname_config = images->fit_uname_cfg; 1444 debug("* fdt: using config '%s' from image " 1445 "at 0x%08lx\n", 1446 fit_uname_config, fdt_addr); 1447 1448 /* 1449 * Check whether configuration has FDT blob defined, 1450 * if not quit silently. 1451 */ 1452 fit_hdr = (void *)fdt_addr; 1453 cfg_noffset = fit_conf_get_node(fit_hdr, 1454 fit_uname_config); 1455 if (cfg_noffset < 0) { 1456 debug("* fdt: no such config\n"); 1457 return 0; 1458 } 1459 1460 fdt_noffset = fit_conf_get_fdt_node(fit_hdr, 1461 cfg_noffset); 1462 if (fdt_noffset < 0) { 1463 debug("* fdt: no fdt in config\n"); 1464 return 0; 1465 } 1466 } 1467 #endif 1468 1469 debug("## Checking for 'FDT'/'FDT Image' at %08lx\n", 1470 fdt_addr); 1471 1472 /* copy from dataflash if needed */ 1473 fdt_addr = genimg_get_image(fdt_addr); 1474 1475 /* 1476 * Check if there is an FDT image at the 1477 * address provided in the second bootm argument 1478 * check image type, for FIT images get a FIT node. 1479 */ 1480 switch (genimg_get_format((void *)fdt_addr)) { 1481 case IMAGE_FORMAT_LEGACY: 1482 /* verify fdt_addr points to a valid image header */ 1483 printf("## Flattened Device Tree from Legacy Image " 1484 "at %08lx\n", 1485 fdt_addr); 1486 fdt_hdr = image_get_fdt(fdt_addr); 1487 if (!fdt_hdr) 1488 goto error; 1489 1490 /* 1491 * move image data to the load address, 1492 * make sure we don't overwrite initial image 1493 */ 1494 image_start = (ulong)fdt_hdr; 1495 image_data = (ulong)image_get_data(fdt_hdr); 1496 image_end = image_get_image_end(fdt_hdr); 1497 1498 load_start = image_get_load(fdt_hdr); 1499 load_end = load_start + image_get_data_size(fdt_hdr); 1500 1501 if (load_start == image_start || 1502 load_start == image_data) { 1503 fdt_blob = (char *)image_data; 1504 break; 1505 } 1506 1507 if ((load_start < image_end) && (load_end > image_start)) { 1508 fdt_error("fdt overwritten"); 1509 goto error; 1510 } 1511 1512 debug(" Loading FDT from 0x%08lx to 0x%08lx\n", 1513 image_data, load_start); 1514 1515 memmove((void *)load_start, 1516 (void *)image_data, 1517 image_get_data_size(fdt_hdr)); 1518 1519 fdt_blob = (char *)load_start; 1520 break; 1521 case IMAGE_FORMAT_FIT: 1522 /* 1523 * This case will catch both: new uImage format 1524 * (libfdt based) and raw FDT blob (also libfdt 1525 * based). 1526 */ 1527 #if defined(CONFIG_FIT) 1528 /* check FDT blob vs FIT blob */ 1529 if (fit_check_format((const void *)fdt_addr)) { 1530 /* 1531 * FIT image 1532 */ 1533 fit_hdr = (void *)fdt_addr; 1534 printf("## Flattened Device Tree from FIT " 1535 "Image at %08lx\n", 1536 fdt_addr); 1537 1538 if (!fit_uname_fdt) { 1539 /* 1540 * no FDT blob image node unit name, 1541 * try to get config node first. If 1542 * config unit node name is NULL 1543 * fit_conf_get_node() will try to 1544 * find default config node 1545 */ 1546 cfg_noffset = fit_conf_get_node(fit_hdr, 1547 fit_uname_config); 1548 1549 if (cfg_noffset < 0) { 1550 fdt_error("Could not find " 1551 "configuration " 1552 "node\n"); 1553 goto error; 1554 } 1555 1556 fit_uname_config = fdt_get_name(fit_hdr, 1557 cfg_noffset, NULL); 1558 printf(" Using '%s' configuration\n", 1559 fit_uname_config); 1560 1561 fdt_noffset = fit_conf_get_fdt_node( 1562 fit_hdr, 1563 cfg_noffset); 1564 fit_uname_fdt = fit_get_name(fit_hdr, 1565 fdt_noffset, NULL); 1566 } else { 1567 /* get FDT component image node offset */ 1568 fdt_noffset = fit_image_get_node( 1569 fit_hdr, 1570 fit_uname_fdt); 1571 } 1572 if (fdt_noffset < 0) { 1573 fdt_error("Could not find subimage " 1574 "node\n"); 1575 goto error; 1576 } 1577 1578 printf(" Trying '%s' FDT blob subimage\n", 1579 fit_uname_fdt); 1580 1581 if (!fit_check_fdt(fit_hdr, fdt_noffset, 1582 images->verify)) 1583 goto error; 1584 1585 /* get ramdisk image data address and length */ 1586 if (fit_image_get_data(fit_hdr, fdt_noffset, 1587 &data, &size)) { 1588 fdt_error("Could not find FDT " 1589 "subimage data"); 1590 goto error; 1591 } 1592 1593 /* verift that image data is a proper FDT blob */ 1594 if (fdt_check_header((char *)data) != 0) { 1595 fdt_error("Subimage data is not a FTD"); 1596 goto error; 1597 } 1598 1599 /* 1600 * move image data to the load address, 1601 * make sure we don't overwrite initial image 1602 */ 1603 image_start = (ulong)fit_hdr; 1604 image_end = fit_get_end(fit_hdr); 1605 1606 if (fit_image_get_load(fit_hdr, fdt_noffset, 1607 &load_start) == 0) { 1608 load_end = load_start + size; 1609 1610 if ((load_start < image_end) && 1611 (load_end > image_start)) { 1612 fdt_error("FDT overwritten"); 1613 goto error; 1614 } 1615 1616 printf(" Loading FDT from 0x%08lx " 1617 "to 0x%08lx\n", 1618 (ulong)data, 1619 load_start); 1620 1621 memmove((void *)load_start, 1622 (void *)data, size); 1623 1624 fdt_blob = (char *)load_start; 1625 } else { 1626 fdt_blob = (char *)data; 1627 } 1628 1629 images->fit_hdr_fdt = fit_hdr; 1630 images->fit_uname_fdt = fit_uname_fdt; 1631 images->fit_noffset_fdt = fdt_noffset; 1632 break; 1633 } else 1634 #endif 1635 { 1636 /* 1637 * FDT blob 1638 */ 1639 fdt_blob = (char *)fdt_addr; 1640 debug("* fdt: raw FDT blob\n"); 1641 printf("## Flattened Device Tree blob at " 1642 "%08lx\n", (long)fdt_blob); 1643 } 1644 break; 1645 default: 1646 puts("ERROR: Did not find a cmdline Flattened Device " 1647 "Tree\n"); 1648 goto error; 1649 } 1650 1651 printf(" Booting using the fdt blob at 0x%p\n", fdt_blob); 1652 1653 } else if (images->legacy_hdr_valid && 1654 image_check_type(&images->legacy_hdr_os_copy, 1655 IH_TYPE_MULTI)) { 1656 1657 ulong fdt_data, fdt_len; 1658 1659 /* 1660 * Now check if we have a legacy multi-component image, 1661 * get second entry data start address and len. 1662 */ 1663 printf("## Flattened Device Tree from multi " 1664 "component Image at %08lX\n", 1665 (ulong)images->legacy_hdr_os); 1666 1667 image_multi_getimg(images->legacy_hdr_os, 2, &fdt_data, 1668 &fdt_len); 1669 if (fdt_len) { 1670 1671 fdt_blob = (char *)fdt_data; 1672 printf(" Booting using the fdt at 0x%p\n", fdt_blob); 1673 1674 if (fdt_check_header(fdt_blob) != 0) { 1675 fdt_error("image is not a fdt"); 1676 goto error; 1677 } 1678 1679 if (fdt_totalsize(fdt_blob) != fdt_len) { 1680 fdt_error("fdt size != image size"); 1681 goto error; 1682 } 1683 } else { 1684 debug("## No Flattened Device Tree\n"); 1685 return 0; 1686 } 1687 } else { 1688 debug("## No Flattened Device Tree\n"); 1689 return 0; 1690 } 1691 1692 *of_flat_tree = fdt_blob; 1693 *of_size = fdt_totalsize(fdt_blob); 1694 debug(" of_flat_tree at 0x%08lx size 0x%08lx\n", 1695 (ulong)*of_flat_tree, *of_size); 1696 1697 return 0; 1698 1699 error: 1700 *of_flat_tree = NULL; 1701 *of_size = 0; 1702 return 1; 1703 } 1704 #endif /* CONFIG_OF_LIBFDT */ 1705 1706 #ifdef CONFIG_SYS_BOOT_GET_CMDLINE 1707 /** 1708 * boot_get_cmdline - allocate and initialize kernel cmdline 1709 * @lmb: pointer to lmb handle, will be used for memory mgmt 1710 * @cmd_start: pointer to a ulong variable, will hold cmdline start 1711 * @cmd_end: pointer to a ulong variable, will hold cmdline end 1712 * 1713 * boot_get_cmdline() allocates space for kernel command line below 1714 * BOOTMAPSZ + getenv_bootm_low() address. If "bootargs" U-boot environemnt 1715 * variable is present its contents is copied to allocated kernel 1716 * command line. 1717 * 1718 * returns: 1719 * 0 - success 1720 * -1 - failure 1721 */ 1722 int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end) 1723 { 1724 char *cmdline; 1725 char *s; 1726 1727 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf, 1728 getenv_bootm_mapsize() + getenv_bootm_low()); 1729 1730 if (cmdline == NULL) 1731 return -1; 1732 1733 if ((s = getenv("bootargs")) == NULL) 1734 s = ""; 1735 1736 strcpy(cmdline, s); 1737 1738 *cmd_start = (ulong) & cmdline[0]; 1739 *cmd_end = *cmd_start + strlen(cmdline); 1740 1741 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end); 1742 1743 return 0; 1744 } 1745 #endif /* CONFIG_SYS_BOOT_GET_CMDLINE */ 1746 1747 #ifdef CONFIG_SYS_BOOT_GET_KBD 1748 /** 1749 * boot_get_kbd - allocate and initialize kernel copy of board info 1750 * @lmb: pointer to lmb handle, will be used for memory mgmt 1751 * @kbd: double pointer to board info data 1752 * 1753 * boot_get_kbd() allocates space for kernel copy of board info data below 1754 * BOOTMAPSZ + getenv_bootm_low() address and kernel board info is initialized 1755 * with the current u-boot board info data. 1756 * 1757 * returns: 1758 * 0 - success 1759 * -1 - failure 1760 */ 1761 int boot_get_kbd(struct lmb *lmb, bd_t **kbd) 1762 { 1763 *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf, 1764 getenv_bootm_mapsize() + getenv_bootm_low()); 1765 if (*kbd == NULL) 1766 return -1; 1767 1768 **kbd = *(gd->bd); 1769 1770 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd); 1771 1772 #if defined(DEBUG) && defined(CONFIG_CMD_BDI) 1773 do_bdinfo(NULL, 0, 0, NULL); 1774 #endif 1775 1776 return 0; 1777 } 1778 #endif /* CONFIG_SYS_BOOT_GET_KBD */ 1779 #endif /* !USE_HOSTCC */ 1780 1781 #if defined(CONFIG_FIT) 1782 /*****************************************************************************/ 1783 /* New uImage format routines */ 1784 /*****************************************************************************/ 1785 #ifndef USE_HOSTCC 1786 static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr, 1787 ulong *addr, const char **name) 1788 { 1789 const char *sep; 1790 1791 *addr = addr_curr; 1792 *name = NULL; 1793 1794 sep = strchr(spec, sepc); 1795 if (sep) { 1796 if (sep - spec > 0) 1797 *addr = simple_strtoul(spec, NULL, 16); 1798 1799 *name = sep + 1; 1800 return 1; 1801 } 1802 1803 return 0; 1804 } 1805 1806 /** 1807 * fit_parse_conf - parse FIT configuration spec 1808 * @spec: input string, containing configuration spec 1809 * @add_curr: current image address (to be used as a possible default) 1810 * @addr: pointer to a ulong variable, will hold FIT image address of a given 1811 * configuration 1812 * @conf_name double pointer to a char, will hold pointer to a configuration 1813 * unit name 1814 * 1815 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>, 1816 * where <addr> is a FIT image address that contains configuration 1817 * with a <conf> unit name. 1818 * 1819 * Address part is optional, and if omitted default add_curr will 1820 * be used instead. 1821 * 1822 * returns: 1823 * 1 if spec is a valid configuration string, 1824 * addr and conf_name are set accordingly 1825 * 0 otherwise 1826 */ 1827 int fit_parse_conf(const char *spec, ulong addr_curr, 1828 ulong *addr, const char **conf_name) 1829 { 1830 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name); 1831 } 1832 1833 /** 1834 * fit_parse_subimage - parse FIT subimage spec 1835 * @spec: input string, containing subimage spec 1836 * @add_curr: current image address (to be used as a possible default) 1837 * @addr: pointer to a ulong variable, will hold FIT image address of a given 1838 * subimage 1839 * @image_name: double pointer to a char, will hold pointer to a subimage name 1840 * 1841 * fit_parse_subimage() expects subimage spec in the for of 1842 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains 1843 * subimage with a <subimg> unit name. 1844 * 1845 * Address part is optional, and if omitted default add_curr will 1846 * be used instead. 1847 * 1848 * returns: 1849 * 1 if spec is a valid subimage string, 1850 * addr and image_name are set accordingly 1851 * 0 otherwise 1852 */ 1853 int fit_parse_subimage(const char *spec, ulong addr_curr, 1854 ulong *addr, const char **image_name) 1855 { 1856 return fit_parse_spec(spec, ':', addr_curr, addr, image_name); 1857 } 1858 #endif /* !USE_HOSTCC */ 1859 1860 static void fit_get_debug(const void *fit, int noffset, 1861 char *prop_name, int err) 1862 { 1863 debug("Can't get '%s' property from FIT 0x%08lx, " 1864 "node: offset %d, name %s (%s)\n", 1865 prop_name, (ulong)fit, noffset, 1866 fit_get_name(fit, noffset, NULL), 1867 fdt_strerror(err)); 1868 } 1869 1870 /** 1871 * fit_print_contents - prints out the contents of the FIT format image 1872 * @fit: pointer to the FIT format image header 1873 * @p: pointer to prefix string 1874 * 1875 * fit_print_contents() formats a multi line FIT image contents description. 1876 * The routine prints out FIT image properties (root node level) follwed by 1877 * the details of each component image. 1878 * 1879 * returns: 1880 * no returned results 1881 */ 1882 void fit_print_contents(const void *fit) 1883 { 1884 char *desc; 1885 char *uname; 1886 int images_noffset; 1887 int confs_noffset; 1888 int noffset; 1889 int ndepth; 1890 int count = 0; 1891 int ret; 1892 const char *p; 1893 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC) 1894 time_t timestamp; 1895 #endif 1896 1897 #ifdef USE_HOSTCC 1898 p = ""; 1899 #else 1900 p = " "; 1901 #endif 1902 1903 /* Root node properties */ 1904 ret = fit_get_desc(fit, 0, &desc); 1905 printf("%sFIT description: ", p); 1906 if (ret) 1907 printf("unavailable\n"); 1908 else 1909 printf("%s\n", desc); 1910 1911 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC) 1912 ret = fit_get_timestamp(fit, 0, ×tamp); 1913 printf("%sCreated: ", p); 1914 if (ret) 1915 printf("unavailable\n"); 1916 else 1917 genimg_print_time(timestamp); 1918 #endif 1919 1920 /* Find images parent node offset */ 1921 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); 1922 if (images_noffset < 0) { 1923 printf("Can't find images parent node '%s' (%s)\n", 1924 FIT_IMAGES_PATH, fdt_strerror(images_noffset)); 1925 return; 1926 } 1927 1928 /* Process its subnodes, print out component images details */ 1929 for (ndepth = 0, count = 0, 1930 noffset = fdt_next_node(fit, images_noffset, &ndepth); 1931 (noffset >= 0) && (ndepth > 0); 1932 noffset = fdt_next_node(fit, noffset, &ndepth)) { 1933 if (ndepth == 1) { 1934 /* 1935 * Direct child node of the images parent node, 1936 * i.e. component image node. 1937 */ 1938 printf("%s Image %u (%s)\n", p, count++, 1939 fit_get_name(fit, noffset, NULL)); 1940 1941 fit_image_print(fit, noffset, p); 1942 } 1943 } 1944 1945 /* Find configurations parent node offset */ 1946 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH); 1947 if (confs_noffset < 0) { 1948 debug("Can't get configurations parent node '%s' (%s)\n", 1949 FIT_CONFS_PATH, fdt_strerror(confs_noffset)); 1950 return; 1951 } 1952 1953 /* get default configuration unit name from default property */ 1954 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL); 1955 if (uname) 1956 printf("%s Default Configuration: '%s'\n", p, uname); 1957 1958 /* Process its subnodes, print out configurations details */ 1959 for (ndepth = 0, count = 0, 1960 noffset = fdt_next_node(fit, confs_noffset, &ndepth); 1961 (noffset >= 0) && (ndepth > 0); 1962 noffset = fdt_next_node(fit, noffset, &ndepth)) { 1963 if (ndepth == 1) { 1964 /* 1965 * Direct child node of the configurations parent node, 1966 * i.e. configuration node. 1967 */ 1968 printf("%s Configuration %u (%s)\n", p, count++, 1969 fit_get_name(fit, noffset, NULL)); 1970 1971 fit_conf_print(fit, noffset, p); 1972 } 1973 } 1974 } 1975 1976 /** 1977 * fit_image_print - prints out the FIT component image details 1978 * @fit: pointer to the FIT format image header 1979 * @image_noffset: offset of the component image node 1980 * @p: pointer to prefix string 1981 * 1982 * fit_image_print() lists all mandatory properies for the processed component 1983 * image. If present, hash nodes are printed out as well. Load 1984 * address for images of type firmware is also printed out. Since the load 1985 * address is not mandatory for firmware images, it will be output as 1986 * "unavailable" when not present. 1987 * 1988 * returns: 1989 * no returned results 1990 */ 1991 void fit_image_print(const void *fit, int image_noffset, const char *p) 1992 { 1993 char *desc; 1994 uint8_t type, arch, os, comp; 1995 size_t size; 1996 ulong load, entry; 1997 const void *data; 1998 int noffset; 1999 int ndepth; 2000 int ret; 2001 2002 /* Mandatory properties */ 2003 ret = fit_get_desc(fit, image_noffset, &desc); 2004 printf("%s Description: ", p); 2005 if (ret) 2006 printf("unavailable\n"); 2007 else 2008 printf("%s\n", desc); 2009 2010 fit_image_get_type(fit, image_noffset, &type); 2011 printf("%s Type: %s\n", p, genimg_get_type_name(type)); 2012 2013 fit_image_get_comp(fit, image_noffset, &comp); 2014 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp)); 2015 2016 ret = fit_image_get_data(fit, image_noffset, &data, &size); 2017 2018 #ifndef USE_HOSTCC 2019 printf("%s Data Start: ", p); 2020 if (ret) 2021 printf("unavailable\n"); 2022 else 2023 printf("0x%08lx\n", (ulong)data); 2024 #endif 2025 2026 printf("%s Data Size: ", p); 2027 if (ret) 2028 printf("unavailable\n"); 2029 else 2030 genimg_print_size(size); 2031 2032 /* Remaining, type dependent properties */ 2033 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) || 2034 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) || 2035 (type == IH_TYPE_FLATDT)) { 2036 fit_image_get_arch(fit, image_noffset, &arch); 2037 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch)); 2038 } 2039 2040 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK)) { 2041 fit_image_get_os(fit, image_noffset, &os); 2042 printf("%s OS: %s\n", p, genimg_get_os_name(os)); 2043 } 2044 2045 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) || 2046 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK)) { 2047 ret = fit_image_get_load(fit, image_noffset, &load); 2048 printf("%s Load Address: ", p); 2049 if (ret) 2050 printf("unavailable\n"); 2051 else 2052 printf("0x%08lx\n", load); 2053 } 2054 2055 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) || 2056 (type == IH_TYPE_RAMDISK)) { 2057 fit_image_get_entry(fit, image_noffset, &entry); 2058 printf("%s Entry Point: ", p); 2059 if (ret) 2060 printf("unavailable\n"); 2061 else 2062 printf("0x%08lx\n", entry); 2063 } 2064 2065 /* Process all hash subnodes of the component image node */ 2066 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth); 2067 (noffset >= 0) && (ndepth > 0); 2068 noffset = fdt_next_node(fit, noffset, &ndepth)) { 2069 if (ndepth == 1) { 2070 /* Direct child node of the component image node */ 2071 fit_image_print_hash(fit, noffset, p); 2072 } 2073 } 2074 } 2075 2076 /** 2077 * fit_image_print_hash - prints out the hash node details 2078 * @fit: pointer to the FIT format image header 2079 * @noffset: offset of the hash node 2080 * @p: pointer to prefix string 2081 * 2082 * fit_image_print_hash() lists properies for the processed hash node 2083 * 2084 * returns: 2085 * no returned results 2086 */ 2087 void fit_image_print_hash(const void *fit, int noffset, const char *p) 2088 { 2089 char *algo; 2090 uint8_t *value; 2091 int value_len; 2092 int i, ret; 2093 2094 /* 2095 * Check subnode name, must be equal to "hash". 2096 * Multiple hash nodes require unique unit node 2097 * names, e.g. hash@1, hash@2, etc. 2098 */ 2099 if (strncmp(fit_get_name(fit, noffset, NULL), 2100 FIT_HASH_NODENAME, 2101 strlen(FIT_HASH_NODENAME)) != 0) 2102 return; 2103 2104 debug("%s Hash node: '%s'\n", p, 2105 fit_get_name(fit, noffset, NULL)); 2106 2107 printf("%s Hash algo: ", p); 2108 if (fit_image_hash_get_algo(fit, noffset, &algo)) { 2109 printf("invalid/unsupported\n"); 2110 return; 2111 } 2112 printf("%s\n", algo); 2113 2114 ret = fit_image_hash_get_value(fit, noffset, &value, 2115 &value_len); 2116 printf("%s Hash value: ", p); 2117 if (ret) { 2118 printf("unavailable\n"); 2119 } else { 2120 for (i = 0; i < value_len; i++) 2121 printf("%02x", value[i]); 2122 printf("\n"); 2123 } 2124 2125 debug("%s Hash len: %d\n", p, value_len); 2126 } 2127 2128 /** 2129 * fit_get_desc - get node description property 2130 * @fit: pointer to the FIT format image header 2131 * @noffset: node offset 2132 * @desc: double pointer to the char, will hold pointer to the descrption 2133 * 2134 * fit_get_desc() reads description property from a given node, if 2135 * description is found pointer to it is returened in third call argument. 2136 * 2137 * returns: 2138 * 0, on success 2139 * -1, on failure 2140 */ 2141 int fit_get_desc(const void *fit, int noffset, char **desc) 2142 { 2143 int len; 2144 2145 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len); 2146 if (*desc == NULL) { 2147 fit_get_debug(fit, noffset, FIT_DESC_PROP, len); 2148 return -1; 2149 } 2150 2151 return 0; 2152 } 2153 2154 /** 2155 * fit_get_timestamp - get node timestamp property 2156 * @fit: pointer to the FIT format image header 2157 * @noffset: node offset 2158 * @timestamp: pointer to the time_t, will hold read timestamp 2159 * 2160 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp 2161 * is found and has a correct size its value is retured in third call 2162 * argument. 2163 * 2164 * returns: 2165 * 0, on success 2166 * -1, on property read failure 2167 * -2, on wrong timestamp size 2168 */ 2169 int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp) 2170 { 2171 int len; 2172 const void *data; 2173 2174 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len); 2175 if (data == NULL) { 2176 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len); 2177 return -1; 2178 } 2179 if (len != sizeof(uint32_t)) { 2180 debug("FIT timestamp with incorrect size of (%u)\n", len); 2181 return -2; 2182 } 2183 2184 *timestamp = uimage_to_cpu(*((uint32_t *)data)); 2185 return 0; 2186 } 2187 2188 /** 2189 * fit_image_get_node - get node offset for component image of a given unit name 2190 * @fit: pointer to the FIT format image header 2191 * @image_uname: component image node unit name 2192 * 2193 * fit_image_get_node() finds a component image (withing the '/images' 2194 * node) of a provided unit name. If image is found its node offset is 2195 * returned to the caller. 2196 * 2197 * returns: 2198 * image node offset when found (>=0) 2199 * negative number on failure (FDT_ERR_* code) 2200 */ 2201 int fit_image_get_node(const void *fit, const char *image_uname) 2202 { 2203 int noffset, images_noffset; 2204 2205 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); 2206 if (images_noffset < 0) { 2207 debug("Can't find images parent node '%s' (%s)\n", 2208 FIT_IMAGES_PATH, fdt_strerror(images_noffset)); 2209 return images_noffset; 2210 } 2211 2212 noffset = fdt_subnode_offset(fit, images_noffset, image_uname); 2213 if (noffset < 0) { 2214 debug("Can't get node offset for image unit name: '%s' (%s)\n", 2215 image_uname, fdt_strerror(noffset)); 2216 } 2217 2218 return noffset; 2219 } 2220 2221 /** 2222 * fit_image_get_os - get os id for a given component image node 2223 * @fit: pointer to the FIT format image header 2224 * @noffset: component image node offset 2225 * @os: pointer to the uint8_t, will hold os numeric id 2226 * 2227 * fit_image_get_os() finds os property in a given component image node. 2228 * If the property is found, its (string) value is translated to the numeric 2229 * id which is returned to the caller. 2230 * 2231 * returns: 2232 * 0, on success 2233 * -1, on failure 2234 */ 2235 int fit_image_get_os(const void *fit, int noffset, uint8_t *os) 2236 { 2237 int len; 2238 const void *data; 2239 2240 /* Get OS name from property data */ 2241 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len); 2242 if (data == NULL) { 2243 fit_get_debug(fit, noffset, FIT_OS_PROP, len); 2244 *os = -1; 2245 return -1; 2246 } 2247 2248 /* Translate OS name to id */ 2249 *os = genimg_get_os_id(data); 2250 return 0; 2251 } 2252 2253 /** 2254 * fit_image_get_arch - get arch id for a given component image node 2255 * @fit: pointer to the FIT format image header 2256 * @noffset: component image node offset 2257 * @arch: pointer to the uint8_t, will hold arch numeric id 2258 * 2259 * fit_image_get_arch() finds arch property in a given component image node. 2260 * If the property is found, its (string) value is translated to the numeric 2261 * id which is returned to the caller. 2262 * 2263 * returns: 2264 * 0, on success 2265 * -1, on failure 2266 */ 2267 int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch) 2268 { 2269 int len; 2270 const void *data; 2271 2272 /* Get architecture name from property data */ 2273 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len); 2274 if (data == NULL) { 2275 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len); 2276 *arch = -1; 2277 return -1; 2278 } 2279 2280 /* Translate architecture name to id */ 2281 *arch = genimg_get_arch_id(data); 2282 return 0; 2283 } 2284 2285 /** 2286 * fit_image_get_type - get type id for a given component image node 2287 * @fit: pointer to the FIT format image header 2288 * @noffset: component image node offset 2289 * @type: pointer to the uint8_t, will hold type numeric id 2290 * 2291 * fit_image_get_type() finds type property in a given component image node. 2292 * If the property is found, its (string) value is translated to the numeric 2293 * id which is returned to the caller. 2294 * 2295 * returns: 2296 * 0, on success 2297 * -1, on failure 2298 */ 2299 int fit_image_get_type(const void *fit, int noffset, uint8_t *type) 2300 { 2301 int len; 2302 const void *data; 2303 2304 /* Get image type name from property data */ 2305 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len); 2306 if (data == NULL) { 2307 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len); 2308 *type = -1; 2309 return -1; 2310 } 2311 2312 /* Translate image type name to id */ 2313 *type = genimg_get_type_id(data); 2314 return 0; 2315 } 2316 2317 /** 2318 * fit_image_get_comp - get comp id for a given component image node 2319 * @fit: pointer to the FIT format image header 2320 * @noffset: component image node offset 2321 * @comp: pointer to the uint8_t, will hold comp numeric id 2322 * 2323 * fit_image_get_comp() finds comp property in a given component image node. 2324 * If the property is found, its (string) value is translated to the numeric 2325 * id which is returned to the caller. 2326 * 2327 * returns: 2328 * 0, on success 2329 * -1, on failure 2330 */ 2331 int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp) 2332 { 2333 int len; 2334 const void *data; 2335 2336 /* Get compression name from property data */ 2337 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len); 2338 if (data == NULL) { 2339 fit_get_debug(fit, noffset, FIT_COMP_PROP, len); 2340 *comp = -1; 2341 return -1; 2342 } 2343 2344 /* Translate compression name to id */ 2345 *comp = genimg_get_comp_id(data); 2346 return 0; 2347 } 2348 2349 /** 2350 * fit_image_get_load - get load address property for a given component image node 2351 * @fit: pointer to the FIT format image header 2352 * @noffset: component image node offset 2353 * @load: pointer to the uint32_t, will hold load address 2354 * 2355 * fit_image_get_load() finds load address property in a given component image node. 2356 * If the property is found, its value is returned to the caller. 2357 * 2358 * returns: 2359 * 0, on success 2360 * -1, on failure 2361 */ 2362 int fit_image_get_load(const void *fit, int noffset, ulong *load) 2363 { 2364 int len; 2365 const uint32_t *data; 2366 2367 data = fdt_getprop(fit, noffset, FIT_LOAD_PROP, &len); 2368 if (data == NULL) { 2369 fit_get_debug(fit, noffset, FIT_LOAD_PROP, len); 2370 return -1; 2371 } 2372 2373 *load = uimage_to_cpu(*data); 2374 return 0; 2375 } 2376 2377 /** 2378 * fit_image_get_entry - get entry point address property for a given component image node 2379 * @fit: pointer to the FIT format image header 2380 * @noffset: component image node offset 2381 * @entry: pointer to the uint32_t, will hold entry point address 2382 * 2383 * fit_image_get_entry() finds entry point address property in a given component image node. 2384 * If the property is found, its value is returned to the caller. 2385 * 2386 * returns: 2387 * 0, on success 2388 * -1, on failure 2389 */ 2390 int fit_image_get_entry(const void *fit, int noffset, ulong *entry) 2391 { 2392 int len; 2393 const uint32_t *data; 2394 2395 data = fdt_getprop(fit, noffset, FIT_ENTRY_PROP, &len); 2396 if (data == NULL) { 2397 fit_get_debug(fit, noffset, FIT_ENTRY_PROP, len); 2398 return -1; 2399 } 2400 2401 *entry = uimage_to_cpu(*data); 2402 return 0; 2403 } 2404 2405 /** 2406 * fit_image_get_data - get data property and its size for a given component image node 2407 * @fit: pointer to the FIT format image header 2408 * @noffset: component image node offset 2409 * @data: double pointer to void, will hold data property's data address 2410 * @size: pointer to size_t, will hold data property's data size 2411 * 2412 * fit_image_get_data() finds data property in a given component image node. 2413 * If the property is found its data start address and size are returned to 2414 * the caller. 2415 * 2416 * returns: 2417 * 0, on success 2418 * -1, on failure 2419 */ 2420 int fit_image_get_data(const void *fit, int noffset, 2421 const void **data, size_t *size) 2422 { 2423 int len; 2424 2425 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len); 2426 if (*data == NULL) { 2427 fit_get_debug(fit, noffset, FIT_DATA_PROP, len); 2428 *size = 0; 2429 return -1; 2430 } 2431 2432 *size = len; 2433 return 0; 2434 } 2435 2436 /** 2437 * fit_image_hash_get_algo - get hash algorithm name 2438 * @fit: pointer to the FIT format image header 2439 * @noffset: hash node offset 2440 * @algo: double pointer to char, will hold pointer to the algorithm name 2441 * 2442 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node. 2443 * If the property is found its data start address is returned to the caller. 2444 * 2445 * returns: 2446 * 0, on success 2447 * -1, on failure 2448 */ 2449 int fit_image_hash_get_algo(const void *fit, int noffset, char **algo) 2450 { 2451 int len; 2452 2453 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len); 2454 if (*algo == NULL) { 2455 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len); 2456 return -1; 2457 } 2458 2459 return 0; 2460 } 2461 2462 /** 2463 * fit_image_hash_get_value - get hash value and length 2464 * @fit: pointer to the FIT format image header 2465 * @noffset: hash node offset 2466 * @value: double pointer to uint8_t, will hold address of a hash value data 2467 * @value_len: pointer to an int, will hold hash data length 2468 * 2469 * fit_image_hash_get_value() finds hash value property in a given hash node. 2470 * If the property is found its data start address and size are returned to 2471 * the caller. 2472 * 2473 * returns: 2474 * 0, on success 2475 * -1, on failure 2476 */ 2477 int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value, 2478 int *value_len) 2479 { 2480 int len; 2481 2482 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len); 2483 if (*value == NULL) { 2484 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len); 2485 *value_len = 0; 2486 return -1; 2487 } 2488 2489 *value_len = len; 2490 return 0; 2491 } 2492 2493 #ifndef USE_HOSTCC 2494 /** 2495 * fit_image_hash_get_ignore - get hash ignore flag 2496 * @fit: pointer to the FIT format image header 2497 * @noffset: hash node offset 2498 * @ignore: pointer to an int, will hold hash ignore flag 2499 * 2500 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node. 2501 * If the property is found and non-zero, the hash algorithm is not verified by 2502 * u-boot automatically. 2503 * 2504 * returns: 2505 * 0, on ignore not found 2506 * value, on ignore found 2507 */ 2508 int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore) 2509 { 2510 int len; 2511 int *value; 2512 2513 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len); 2514 if (value == NULL || len != sizeof(int)) 2515 *ignore = 0; 2516 else 2517 *ignore = *value; 2518 2519 return 0; 2520 } 2521 #endif 2522 2523 /** 2524 * fit_set_timestamp - set node timestamp property 2525 * @fit: pointer to the FIT format image header 2526 * @noffset: node offset 2527 * @timestamp: timestamp value to be set 2528 * 2529 * fit_set_timestamp() attempts to set timestamp property in the requested 2530 * node and returns operation status to the caller. 2531 * 2532 * returns: 2533 * 0, on success 2534 * -1, on property read failure 2535 */ 2536 int fit_set_timestamp(void *fit, int noffset, time_t timestamp) 2537 { 2538 uint32_t t; 2539 int ret; 2540 2541 t = cpu_to_uimage(timestamp); 2542 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t, 2543 sizeof(uint32_t)); 2544 if (ret) { 2545 printf("Can't set '%s' property for '%s' node (%s)\n", 2546 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL), 2547 fdt_strerror(ret)); 2548 return -1; 2549 } 2550 2551 return 0; 2552 } 2553 2554 /** 2555 * calculate_hash - calculate and return hash for provided input data 2556 * @data: pointer to the input data 2557 * @data_len: data length 2558 * @algo: requested hash algorithm 2559 * @value: pointer to the char, will hold hash value data (caller must 2560 * allocate enough free space) 2561 * value_len: length of the calculated hash 2562 * 2563 * calculate_hash() computes input data hash according to the requested algorithm. 2564 * Resulting hash value is placed in caller provided 'value' buffer, length 2565 * of the calculated hash is returned via value_len pointer argument. 2566 * 2567 * returns: 2568 * 0, on success 2569 * -1, when algo is unsupported 2570 */ 2571 static int calculate_hash(const void *data, int data_len, const char *algo, 2572 uint8_t *value, int *value_len) 2573 { 2574 if (strcmp(algo, "crc32") == 0) { 2575 *((uint32_t *)value) = crc32_wd(0, data, data_len, 2576 CHUNKSZ_CRC32); 2577 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value)); 2578 *value_len = 4; 2579 } else if (strcmp(algo, "sha1") == 0) { 2580 sha1_csum_wd((unsigned char *) data, data_len, 2581 (unsigned char *) value, CHUNKSZ_SHA1); 2582 *value_len = 20; 2583 } else if (strcmp(algo, "md5") == 0) { 2584 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5); 2585 *value_len = 16; 2586 } else { 2587 debug("Unsupported hash alogrithm\n"); 2588 return -1; 2589 } 2590 return 0; 2591 } 2592 2593 #ifdef USE_HOSTCC 2594 /** 2595 * fit_set_hashes - process FIT component image nodes and calculate hashes 2596 * @fit: pointer to the FIT format image header 2597 * 2598 * fit_set_hashes() adds hash values for all component images in the FIT blob. 2599 * Hashes are calculated for all component images which have hash subnodes 2600 * with algorithm property set to one of the supported hash algorithms. 2601 * 2602 * returns 2603 * 0, on success 2604 * libfdt error code, on failure 2605 */ 2606 int fit_set_hashes(void *fit) 2607 { 2608 int images_noffset; 2609 int noffset; 2610 int ndepth; 2611 int ret; 2612 2613 /* Find images parent node offset */ 2614 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); 2615 if (images_noffset < 0) { 2616 printf("Can't find images parent node '%s' (%s)\n", 2617 FIT_IMAGES_PATH, fdt_strerror(images_noffset)); 2618 return images_noffset; 2619 } 2620 2621 /* Process its subnodes, print out component images details */ 2622 for (ndepth = 0, noffset = fdt_next_node(fit, images_noffset, &ndepth); 2623 (noffset >= 0) && (ndepth > 0); 2624 noffset = fdt_next_node(fit, noffset, &ndepth)) { 2625 if (ndepth == 1) { 2626 /* 2627 * Direct child node of the images parent node, 2628 * i.e. component image node. 2629 */ 2630 ret = fit_image_set_hashes(fit, noffset); 2631 if (ret) 2632 return ret; 2633 } 2634 } 2635 2636 return 0; 2637 } 2638 2639 /** 2640 * fit_image_set_hashes - calculate/set hashes for given component image node 2641 * @fit: pointer to the FIT format image header 2642 * @image_noffset: requested component image node 2643 * 2644 * fit_image_set_hashes() adds hash values for an component image node. All 2645 * existing hash subnodes are checked, if algorithm property is set to one of 2646 * the supported hash algorithms, hash value is computed and corresponding 2647 * hash node property is set, for example: 2648 * 2649 * Input component image node structure: 2650 * 2651 * o image@1 (at image_noffset) 2652 * | - data = [binary data] 2653 * o hash@1 2654 * |- algo = "sha1" 2655 * 2656 * Output component image node structure: 2657 * 2658 * o image@1 (at image_noffset) 2659 * | - data = [binary data] 2660 * o hash@1 2661 * |- algo = "sha1" 2662 * |- value = sha1(data) 2663 * 2664 * returns: 2665 * 0 on sucess 2666 * <0 on failure 2667 */ 2668 int fit_image_set_hashes(void *fit, int image_noffset) 2669 { 2670 const void *data; 2671 size_t size; 2672 char *algo; 2673 uint8_t value[FIT_MAX_HASH_LEN]; 2674 int value_len; 2675 int noffset; 2676 int ndepth; 2677 2678 /* Get image data and data length */ 2679 if (fit_image_get_data(fit, image_noffset, &data, &size)) { 2680 printf("Can't get image data/size\n"); 2681 return -1; 2682 } 2683 2684 /* Process all hash subnodes of the component image node */ 2685 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth); 2686 (noffset >= 0) && (ndepth > 0); 2687 noffset = fdt_next_node(fit, noffset, &ndepth)) { 2688 if (ndepth == 1) { 2689 /* Direct child node of the component image node */ 2690 2691 /* 2692 * Check subnode name, must be equal to "hash". 2693 * Multiple hash nodes require unique unit node 2694 * names, e.g. hash@1, hash@2, etc. 2695 */ 2696 if (strncmp(fit_get_name(fit, noffset, NULL), 2697 FIT_HASH_NODENAME, 2698 strlen(FIT_HASH_NODENAME)) != 0) { 2699 /* Not a hash subnode, skip it */ 2700 continue; 2701 } 2702 2703 if (fit_image_hash_get_algo(fit, noffset, &algo)) { 2704 printf("Can't get hash algo property for " 2705 "'%s' hash node in '%s' image node\n", 2706 fit_get_name(fit, noffset, NULL), 2707 fit_get_name(fit, image_noffset, NULL)); 2708 return -1; 2709 } 2710 2711 if (calculate_hash(data, size, algo, value, 2712 &value_len)) { 2713 printf("Unsupported hash algorithm (%s) for " 2714 "'%s' hash node in '%s' image node\n", 2715 algo, fit_get_name(fit, noffset, NULL), 2716 fit_get_name(fit, image_noffset, 2717 NULL)); 2718 return -1; 2719 } 2720 2721 if (fit_image_hash_set_value(fit, noffset, value, 2722 value_len)) { 2723 printf("Can't set hash value for " 2724 "'%s' hash node in '%s' image node\n", 2725 fit_get_name(fit, noffset, NULL), 2726 fit_get_name(fit, image_noffset, NULL)); 2727 return -1; 2728 } 2729 } 2730 } 2731 2732 return 0; 2733 } 2734 2735 /** 2736 * fit_image_hash_set_value - set hash value in requested has node 2737 * @fit: pointer to the FIT format image header 2738 * @noffset: hash node offset 2739 * @value: hash value to be set 2740 * @value_len: hash value length 2741 * 2742 * fit_image_hash_set_value() attempts to set hash value in a node at offset 2743 * given and returns operation status to the caller. 2744 * 2745 * returns 2746 * 0, on success 2747 * -1, on failure 2748 */ 2749 int fit_image_hash_set_value(void *fit, int noffset, uint8_t *value, 2750 int value_len) 2751 { 2752 int ret; 2753 2754 ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len); 2755 if (ret) { 2756 printf("Can't set hash '%s' property for '%s' node(%s)\n", 2757 FIT_VALUE_PROP, fit_get_name(fit, noffset, NULL), 2758 fdt_strerror(ret)); 2759 return -1; 2760 } 2761 2762 return 0; 2763 } 2764 #endif /* USE_HOSTCC */ 2765 2766 /** 2767 * fit_image_check_hashes - verify data intergity 2768 * @fit: pointer to the FIT format image header 2769 * @image_noffset: component image node offset 2770 * 2771 * fit_image_check_hashes() goes over component image hash nodes, 2772 * re-calculates each data hash and compares with the value stored in hash 2773 * node. 2774 * 2775 * returns: 2776 * 1, if all hashes are valid 2777 * 0, otherwise (or on error) 2778 */ 2779 int fit_image_check_hashes(const void *fit, int image_noffset) 2780 { 2781 const void *data; 2782 size_t size; 2783 char *algo; 2784 uint8_t *fit_value; 2785 int fit_value_len; 2786 #ifndef USE_HOSTCC 2787 int ignore; 2788 #endif 2789 uint8_t value[FIT_MAX_HASH_LEN]; 2790 int value_len; 2791 int noffset; 2792 int ndepth; 2793 char *err_msg = ""; 2794 2795 /* Get image data and data length */ 2796 if (fit_image_get_data(fit, image_noffset, &data, &size)) { 2797 printf("Can't get image data/size\n"); 2798 return 0; 2799 } 2800 2801 /* Process all hash subnodes of the component image node */ 2802 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth); 2803 (noffset >= 0) && (ndepth > 0); 2804 noffset = fdt_next_node(fit, noffset, &ndepth)) { 2805 if (ndepth == 1) { 2806 /* Direct child node of the component image node */ 2807 2808 /* 2809 * Check subnode name, must be equal to "hash". 2810 * Multiple hash nodes require unique unit node 2811 * names, e.g. hash@1, hash@2, etc. 2812 */ 2813 if (strncmp(fit_get_name(fit, noffset, NULL), 2814 FIT_HASH_NODENAME, 2815 strlen(FIT_HASH_NODENAME)) != 0) 2816 continue; 2817 2818 if (fit_image_hash_get_algo(fit, noffset, &algo)) { 2819 err_msg = " error!\nCan't get hash algo " 2820 "property"; 2821 goto error; 2822 } 2823 printf("%s", algo); 2824 2825 #ifndef USE_HOSTCC 2826 fit_image_hash_get_ignore(fit, noffset, &ignore); 2827 if (ignore) { 2828 printf("-skipped "); 2829 continue; 2830 } 2831 #endif 2832 2833 if (fit_image_hash_get_value(fit, noffset, &fit_value, 2834 &fit_value_len)) { 2835 err_msg = " error!\nCan't get hash value " 2836 "property"; 2837 goto error; 2838 } 2839 2840 if (calculate_hash(data, size, algo, value, 2841 &value_len)) { 2842 err_msg = " error!\n" 2843 "Unsupported hash algorithm"; 2844 goto error; 2845 } 2846 2847 if (value_len != fit_value_len) { 2848 err_msg = " error !\nBad hash value len"; 2849 goto error; 2850 } else if (memcmp(value, fit_value, value_len) != 0) { 2851 err_msg = " error!\nBad hash value"; 2852 goto error; 2853 } 2854 printf("+ "); 2855 } 2856 } 2857 2858 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) { 2859 err_msg = " error!\nCorrupted or truncated tree"; 2860 goto error; 2861 } 2862 2863 return 1; 2864 2865 error: 2866 printf("%s for '%s' hash node in '%s' image node\n", 2867 err_msg, fit_get_name(fit, noffset, NULL), 2868 fit_get_name(fit, image_noffset, NULL)); 2869 return 0; 2870 } 2871 2872 /** 2873 * fit_all_image_check_hashes - verify data intergity for all images 2874 * @fit: pointer to the FIT format image header 2875 * 2876 * fit_all_image_check_hashes() goes over all images in the FIT and 2877 * for every images checks if all it's hashes are valid. 2878 * 2879 * returns: 2880 * 1, if all hashes of all images are valid 2881 * 0, otherwise (or on error) 2882 */ 2883 int fit_all_image_check_hashes(const void *fit) 2884 { 2885 int images_noffset; 2886 int noffset; 2887 int ndepth; 2888 int count; 2889 2890 /* Find images parent node offset */ 2891 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); 2892 if (images_noffset < 0) { 2893 printf("Can't find images parent node '%s' (%s)\n", 2894 FIT_IMAGES_PATH, fdt_strerror(images_noffset)); 2895 return 0; 2896 } 2897 2898 /* Process all image subnodes, check hashes for each */ 2899 printf("## Checking hash(es) for FIT Image at %08lx ...\n", 2900 (ulong)fit); 2901 for (ndepth = 0, count = 0, 2902 noffset = fdt_next_node(fit, images_noffset, &ndepth); 2903 (noffset >= 0) && (ndepth > 0); 2904 noffset = fdt_next_node(fit, noffset, &ndepth)) { 2905 if (ndepth == 1) { 2906 /* 2907 * Direct child node of the images parent node, 2908 * i.e. component image node. 2909 */ 2910 printf(" Hash(es) for Image %u (%s): ", count++, 2911 fit_get_name(fit, noffset, NULL)); 2912 2913 if (!fit_image_check_hashes(fit, noffset)) 2914 return 0; 2915 printf("\n"); 2916 } 2917 } 2918 return 1; 2919 } 2920 2921 /** 2922 * fit_image_check_os - check whether image node is of a given os type 2923 * @fit: pointer to the FIT format image header 2924 * @noffset: component image node offset 2925 * @os: requested image os 2926 * 2927 * fit_image_check_os() reads image os property and compares its numeric 2928 * id with the requested os. Comparison result is returned to the caller. 2929 * 2930 * returns: 2931 * 1 if image is of given os type 2932 * 0 otherwise (or on error) 2933 */ 2934 int fit_image_check_os(const void *fit, int noffset, uint8_t os) 2935 { 2936 uint8_t image_os; 2937 2938 if (fit_image_get_os(fit, noffset, &image_os)) 2939 return 0; 2940 return (os == image_os); 2941 } 2942 2943 /** 2944 * fit_image_check_arch - check whether image node is of a given arch 2945 * @fit: pointer to the FIT format image header 2946 * @noffset: component image node offset 2947 * @arch: requested imagearch 2948 * 2949 * fit_image_check_arch() reads image arch property and compares its numeric 2950 * id with the requested arch. Comparison result is returned to the caller. 2951 * 2952 * returns: 2953 * 1 if image is of given arch 2954 * 0 otherwise (or on error) 2955 */ 2956 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch) 2957 { 2958 uint8_t image_arch; 2959 2960 if (fit_image_get_arch(fit, noffset, &image_arch)) 2961 return 0; 2962 return (arch == image_arch); 2963 } 2964 2965 /** 2966 * fit_image_check_type - check whether image node is of a given type 2967 * @fit: pointer to the FIT format image header 2968 * @noffset: component image node offset 2969 * @type: requested image type 2970 * 2971 * fit_image_check_type() reads image type property and compares its numeric 2972 * id with the requested type. Comparison result is returned to the caller. 2973 * 2974 * returns: 2975 * 1 if image is of given type 2976 * 0 otherwise (or on error) 2977 */ 2978 int fit_image_check_type(const void *fit, int noffset, uint8_t type) 2979 { 2980 uint8_t image_type; 2981 2982 if (fit_image_get_type(fit, noffset, &image_type)) 2983 return 0; 2984 return (type == image_type); 2985 } 2986 2987 /** 2988 * fit_image_check_comp - check whether image node uses given compression 2989 * @fit: pointer to the FIT format image header 2990 * @noffset: component image node offset 2991 * @comp: requested image compression type 2992 * 2993 * fit_image_check_comp() reads image compression property and compares its 2994 * numeric id with the requested compression type. Comparison result is 2995 * returned to the caller. 2996 * 2997 * returns: 2998 * 1 if image uses requested compression 2999 * 0 otherwise (or on error) 3000 */ 3001 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp) 3002 { 3003 uint8_t image_comp; 3004 3005 if (fit_image_get_comp(fit, noffset, &image_comp)) 3006 return 0; 3007 return (comp == image_comp); 3008 } 3009 3010 /** 3011 * fit_check_format - sanity check FIT image format 3012 * @fit: pointer to the FIT format image header 3013 * 3014 * fit_check_format() runs a basic sanity FIT image verification. 3015 * Routine checks for mandatory properties, nodes, etc. 3016 * 3017 * returns: 3018 * 1, on success 3019 * 0, on failure 3020 */ 3021 int fit_check_format(const void *fit) 3022 { 3023 /* mandatory / node 'description' property */ 3024 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) { 3025 debug("Wrong FIT format: no description\n"); 3026 return 0; 3027 } 3028 3029 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC) 3030 /* mandatory / node 'timestamp' property */ 3031 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) { 3032 debug("Wrong FIT format: no timestamp\n"); 3033 return 0; 3034 } 3035 #endif 3036 3037 /* mandatory subimages parent '/images' node */ 3038 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) { 3039 debug("Wrong FIT format: no images parent node\n"); 3040 return 0; 3041 } 3042 3043 return 1; 3044 } 3045 3046 3047 /** 3048 * fit_conf_find_compat 3049 * @fit: pointer to the FIT format image header 3050 * @fdt: pointer to the device tree to compare against 3051 * 3052 * fit_conf_find_compat() attempts to find the configuration whose fdt is the 3053 * most compatible with the passed in device tree. 3054 * 3055 * Example: 3056 * 3057 * / o image-tree 3058 * |-o images 3059 * | |-o fdt@1 3060 * | |-o fdt@2 3061 * | 3062 * |-o configurations 3063 * |-o config@1 3064 * | |-fdt = fdt@1 3065 * | 3066 * |-o config@2 3067 * |-fdt = fdt@2 3068 * 3069 * / o U-Boot fdt 3070 * |-compatible = "foo,bar", "bim,bam" 3071 * 3072 * / o kernel fdt1 3073 * |-compatible = "foo,bar", 3074 * 3075 * / o kernel fdt2 3076 * |-compatible = "bim,bam", "baz,biz" 3077 * 3078 * Configuration 1 would be picked because the first string in U-Boot's 3079 * compatible list, "foo,bar", matches a compatible string in the root of fdt1. 3080 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1. 3081 * 3082 * returns: 3083 * offset to the configuration to use if one was found 3084 * -1 otherwise 3085 */ 3086 int fit_conf_find_compat(const void *fit, const void *fdt) 3087 { 3088 int ndepth = 0; 3089 int noffset, confs_noffset, images_noffset; 3090 const void *fdt_compat; 3091 int fdt_compat_len; 3092 int best_match_offset = 0; 3093 int best_match_pos = 0; 3094 3095 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH); 3096 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); 3097 if (confs_noffset < 0 || images_noffset < 0) { 3098 debug("Can't find configurations or images nodes.\n"); 3099 return -1; 3100 } 3101 3102 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len); 3103 if (!fdt_compat) { 3104 debug("Fdt for comparison has no \"compatible\" property.\n"); 3105 return -1; 3106 } 3107 3108 /* 3109 * Loop over the configurations in the FIT image. 3110 */ 3111 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth); 3112 (noffset >= 0) && (ndepth > 0); 3113 noffset = fdt_next_node(fit, noffset, &ndepth)) { 3114 const void *kfdt; 3115 const char *kfdt_name; 3116 int kfdt_noffset; 3117 const char *cur_fdt_compat; 3118 int len; 3119 size_t size; 3120 int i; 3121 3122 if (ndepth > 1) 3123 continue; 3124 3125 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len); 3126 if (!kfdt_name) { 3127 debug("No fdt property found.\n"); 3128 continue; 3129 } 3130 kfdt_noffset = fdt_subnode_offset(fit, images_noffset, 3131 kfdt_name); 3132 if (kfdt_noffset < 0) { 3133 debug("No image node named \"%s\" found.\n", 3134 kfdt_name); 3135 continue; 3136 } 3137 /* 3138 * Get a pointer to this configuration's fdt. 3139 */ 3140 if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) { 3141 debug("Failed to get fdt \"%s\".\n", kfdt_name); 3142 continue; 3143 } 3144 3145 len = fdt_compat_len; 3146 cur_fdt_compat = fdt_compat; 3147 /* 3148 * Look for a match for each U-Boot compatibility string in 3149 * turn in this configuration's fdt. 3150 */ 3151 for (i = 0; len > 0 && 3152 (!best_match_offset || best_match_pos > i); i++) { 3153 int cur_len = strlen(cur_fdt_compat) + 1; 3154 3155 if (!fdt_node_check_compatible(kfdt, 0, 3156 cur_fdt_compat)) { 3157 best_match_offset = noffset; 3158 best_match_pos = i; 3159 break; 3160 } 3161 len -= cur_len; 3162 cur_fdt_compat += cur_len; 3163 } 3164 } 3165 if (!best_match_offset) { 3166 debug("No match found.\n"); 3167 return -1; 3168 } 3169 3170 return best_match_offset; 3171 } 3172 3173 /** 3174 * fit_conf_get_node - get node offset for configuration of a given unit name 3175 * @fit: pointer to the FIT format image header 3176 * @conf_uname: configuration node unit name 3177 * 3178 * fit_conf_get_node() finds a configuration (withing the '/configurations' 3179 * parant node) of a provided unit name. If configuration is found its node offset 3180 * is returned to the caller. 3181 * 3182 * When NULL is provided in second argument fit_conf_get_node() will search 3183 * for a default configuration node instead. Default configuration node unit name 3184 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node. 3185 * 3186 * returns: 3187 * configuration node offset when found (>=0) 3188 * negative number on failure (FDT_ERR_* code) 3189 */ 3190 int fit_conf_get_node(const void *fit, const char *conf_uname) 3191 { 3192 int noffset, confs_noffset; 3193 int len; 3194 3195 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH); 3196 if (confs_noffset < 0) { 3197 debug("Can't find configurations parent node '%s' (%s)\n", 3198 FIT_CONFS_PATH, fdt_strerror(confs_noffset)); 3199 return confs_noffset; 3200 } 3201 3202 if (conf_uname == NULL) { 3203 /* get configuration unit name from the default property */ 3204 debug("No configuration specified, trying default...\n"); 3205 conf_uname = (char *)fdt_getprop(fit, confs_noffset, 3206 FIT_DEFAULT_PROP, &len); 3207 if (conf_uname == NULL) { 3208 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP, 3209 len); 3210 return len; 3211 } 3212 debug("Found default configuration: '%s'\n", conf_uname); 3213 } 3214 3215 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname); 3216 if (noffset < 0) { 3217 debug("Can't get node offset for configuration unit name: " 3218 "'%s' (%s)\n", 3219 conf_uname, fdt_strerror(noffset)); 3220 } 3221 3222 return noffset; 3223 } 3224 3225 static int __fit_conf_get_prop_node(const void *fit, int noffset, 3226 const char *prop_name) 3227 { 3228 char *uname; 3229 int len; 3230 3231 /* get kernel image unit name from configuration kernel property */ 3232 uname = (char *)fdt_getprop(fit, noffset, prop_name, &len); 3233 if (uname == NULL) 3234 return len; 3235 3236 return fit_image_get_node(fit, uname); 3237 } 3238 3239 /** 3240 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to 3241 * a given configuration 3242 * @fit: pointer to the FIT format image header 3243 * @noffset: configuration node offset 3244 * 3245 * fit_conf_get_kernel_node() retrives kernel image node unit name from 3246 * configuration FIT_KERNEL_PROP property and translates it to the node 3247 * offset. 3248 * 3249 * returns: 3250 * image node offset when found (>=0) 3251 * negative number on failure (FDT_ERR_* code) 3252 */ 3253 int fit_conf_get_kernel_node(const void *fit, int noffset) 3254 { 3255 return __fit_conf_get_prop_node(fit, noffset, FIT_KERNEL_PROP); 3256 } 3257 3258 /** 3259 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to 3260 * a given configuration 3261 * @fit: pointer to the FIT format image header 3262 * @noffset: configuration node offset 3263 * 3264 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from 3265 * configuration FIT_KERNEL_PROP property and translates it to the node 3266 * offset. 3267 * 3268 * returns: 3269 * image node offset when found (>=0) 3270 * negative number on failure (FDT_ERR_* code) 3271 */ 3272 int fit_conf_get_ramdisk_node(const void *fit, int noffset) 3273 { 3274 return __fit_conf_get_prop_node(fit, noffset, FIT_RAMDISK_PROP); 3275 } 3276 3277 /** 3278 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to 3279 * a given configuration 3280 * @fit: pointer to the FIT format image header 3281 * @noffset: configuration node offset 3282 * 3283 * fit_conf_get_fdt_node() retrives fdt image node unit name from 3284 * configuration FIT_KERNEL_PROP property and translates it to the node 3285 * offset. 3286 * 3287 * returns: 3288 * image node offset when found (>=0) 3289 * negative number on failure (FDT_ERR_* code) 3290 */ 3291 int fit_conf_get_fdt_node(const void *fit, int noffset) 3292 { 3293 return __fit_conf_get_prop_node(fit, noffset, FIT_FDT_PROP); 3294 } 3295 3296 /** 3297 * fit_conf_print - prints out the FIT configuration details 3298 * @fit: pointer to the FIT format image header 3299 * @noffset: offset of the configuration node 3300 * @p: pointer to prefix string 3301 * 3302 * fit_conf_print() lists all mandatory properies for the processed 3303 * configuration node. 3304 * 3305 * returns: 3306 * no returned results 3307 */ 3308 void fit_conf_print(const void *fit, int noffset, const char *p) 3309 { 3310 char *desc; 3311 char *uname; 3312 int ret; 3313 3314 /* Mandatory properties */ 3315 ret = fit_get_desc(fit, noffset, &desc); 3316 printf("%s Description: ", p); 3317 if (ret) 3318 printf("unavailable\n"); 3319 else 3320 printf("%s\n", desc); 3321 3322 uname = (char *)fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL); 3323 printf("%s Kernel: ", p); 3324 if (uname == NULL) 3325 printf("unavailable\n"); 3326 else 3327 printf("%s\n", uname); 3328 3329 /* Optional properties */ 3330 uname = (char *)fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL); 3331 if (uname) 3332 printf("%s Init Ramdisk: %s\n", p, uname); 3333 3334 uname = (char *)fdt_getprop(fit, noffset, FIT_FDT_PROP, NULL); 3335 if (uname) 3336 printf("%s FDT: %s\n", p, uname); 3337 } 3338 3339 /** 3340 * fit_check_ramdisk - verify FIT format ramdisk subimage 3341 * @fit_hdr: pointer to the FIT ramdisk header 3342 * @rd_noffset: ramdisk subimage node offset within FIT image 3343 * @arch: requested ramdisk image architecture type 3344 * @verify: data CRC verification flag 3345 * 3346 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from 3347 * specified FIT image. 3348 * 3349 * returns: 3350 * 1, on success 3351 * 0, on failure 3352 */ 3353 #ifndef USE_HOSTCC 3354 static int fit_check_ramdisk(const void *fit, int rd_noffset, uint8_t arch, 3355 int verify) 3356 { 3357 fit_image_print(fit, rd_noffset, " "); 3358 3359 if (verify) { 3360 puts(" Verifying Hash Integrity ... "); 3361 if (!fit_image_check_hashes(fit, rd_noffset)) { 3362 puts("Bad Data Hash\n"); 3363 bootstage_error(BOOTSTAGE_ID_FIT_RD_HASH); 3364 return 0; 3365 } 3366 puts("OK\n"); 3367 } 3368 3369 bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK_ALL); 3370 if (!fit_image_check_os(fit, rd_noffset, IH_OS_LINUX) || 3371 !fit_image_check_arch(fit, rd_noffset, arch) || 3372 !fit_image_check_type(fit, rd_noffset, IH_TYPE_RAMDISK)) { 3373 printf("No Linux %s Ramdisk Image\n", 3374 genimg_get_arch_name(arch)); 3375 bootstage_error(BOOTSTAGE_ID_FIT_RD_CHECK_ALL); 3376 return 0; 3377 } 3378 3379 bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK_ALL_OK); 3380 return 1; 3381 } 3382 #endif /* USE_HOSTCC */ 3383 #endif /* CONFIG_FIT */ 3384