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