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 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) 39 #include <rtc.h> 40 #endif 41 42 #include <image.h> 43 44 #if defined(CONFIG_FIT) || defined (CONFIG_OF_LIBFDT) 45 #include <fdt.h> 46 #include <libfdt.h> 47 #include <fdt_support.h> 48 #endif 49 50 #if defined(CONFIG_FIT) 51 #include <u-boot/md5.h> 52 #include <sha1.h> 53 54 static int fit_check_ramdisk (const void *fit, int os_noffset, 55 uint8_t arch, int verify); 56 #endif 57 58 #ifdef CONFIG_CMD_BDI 59 extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]); 60 #endif 61 62 DECLARE_GLOBAL_DATA_PTR; 63 64 static image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch, 65 int verify); 66 #else 67 #include "mkimage.h" 68 #include <u-boot/md5.h> 69 #include <time.h> 70 #include <image.h> 71 #endif /* !USE_HOSTCC*/ 72 73 typedef struct table_entry { 74 int id; /* as defined in image.h */ 75 char *sname; /* short (input) name */ 76 char *lname; /* long (output) name */ 77 } table_entry_t; 78 79 static table_entry_t uimage_arch[] = { 80 { IH_ARCH_INVALID, NULL, "Invalid ARCH", }, 81 { IH_ARCH_ALPHA, "alpha", "Alpha", }, 82 { IH_ARCH_ARM, "arm", "ARM", }, 83 { IH_ARCH_I386, "x86", "Intel x86", }, 84 { IH_ARCH_IA64, "ia64", "IA64", }, 85 { IH_ARCH_M68K, "m68k", "M68K", }, 86 { IH_ARCH_MICROBLAZE, "microblaze", "MicroBlaze", }, 87 { IH_ARCH_MIPS, "mips", "MIPS", }, 88 { IH_ARCH_MIPS64, "mips64", "MIPS 64 Bit", }, 89 { IH_ARCH_NIOS, "nios", "NIOS", }, 90 { IH_ARCH_NIOS2, "nios2", "NIOS II", }, 91 { IH_ARCH_PPC, "powerpc", "PowerPC", }, 92 { IH_ARCH_PPC, "ppc", "PowerPC", }, 93 { IH_ARCH_S390, "s390", "IBM S390", }, 94 { IH_ARCH_SH, "sh", "SuperH", }, 95 { IH_ARCH_SPARC, "sparc", "SPARC", }, 96 { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", }, 97 { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", }, 98 { IH_ARCH_AVR32, "avr32", "AVR32", }, 99 { -1, "", "", }, 100 }; 101 102 static table_entry_t uimage_os[] = { 103 { IH_OS_INVALID, NULL, "Invalid OS", }, 104 #if defined(CONFIG_ARTOS) || defined(USE_HOSTCC) 105 { IH_OS_ARTOS, "artos", "ARTOS", }, 106 #endif 107 { IH_OS_LINUX, "linux", "Linux", }, 108 #if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC) 109 { IH_OS_LYNXOS, "lynxos", "LynxOS", }, 110 #endif 111 { IH_OS_NETBSD, "netbsd", "NetBSD", }, 112 { IH_OS_RTEMS, "rtems", "RTEMS", }, 113 { IH_OS_U_BOOT, "u-boot", "U-Boot", }, 114 #if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC) 115 { IH_OS_QNX, "qnx", "QNX", }, 116 { IH_OS_VXWORKS, "vxworks", "VxWorks", }, 117 #endif 118 #ifdef USE_HOSTCC 119 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", }, 120 { IH_OS_DELL, "dell", "Dell", }, 121 { IH_OS_ESIX, "esix", "Esix", }, 122 { IH_OS_FREEBSD, "freebsd", "FreeBSD", }, 123 { IH_OS_IRIX, "irix", "Irix", }, 124 { IH_OS_NCR, "ncr", "NCR", }, 125 { IH_OS_OPENBSD, "openbsd", "OpenBSD", }, 126 { IH_OS_PSOS, "psos", "pSOS", }, 127 { IH_OS_SCO, "sco", "SCO", }, 128 { IH_OS_SOLARIS, "solaris", "Solaris", }, 129 { IH_OS_SVR4, "svr4", "SVR4", }, 130 #endif 131 { -1, "", "", }, 132 }; 133 134 static table_entry_t uimage_type[] = { 135 { IH_TYPE_INVALID, NULL, "Invalid Image", }, 136 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", }, 137 { IH_TYPE_FIRMWARE, "firmware", "Firmware", }, 138 { IH_TYPE_KERNEL, "kernel", "Kernel Image", }, 139 { IH_TYPE_MULTI, "multi", "Multi-File Image", }, 140 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", }, 141 { IH_TYPE_SCRIPT, "script", "Script", }, 142 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", }, 143 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", }, 144 { -1, "", "", }, 145 }; 146 147 static table_entry_t uimage_comp[] = { 148 { IH_COMP_NONE, "none", "uncompressed", }, 149 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", }, 150 { IH_COMP_GZIP, "gzip", "gzip compressed", }, 151 { -1, "", "", }, 152 }; 153 154 uint32_t crc32 (uint32_t, const unsigned char *, uint); 155 uint32_t crc32_wd (uint32_t, const unsigned char *, uint, uint); 156 static void genimg_print_size (uint32_t size); 157 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC) 158 static void genimg_print_time (time_t timestamp); 159 #endif 160 161 /*****************************************************************************/ 162 /* Legacy format routines */ 163 /*****************************************************************************/ 164 int image_check_hcrc (image_header_t *hdr) 165 { 166 ulong hcrc; 167 ulong len = image_get_header_size (); 168 image_header_t header; 169 170 /* Copy header so we can blank CRC field for re-calculation */ 171 memmove (&header, (char *)hdr, image_get_header_size ()); 172 image_set_hcrc (&header, 0); 173 174 hcrc = crc32 (0, (unsigned char *)&header, len); 175 176 return (hcrc == image_get_hcrc (hdr)); 177 } 178 179 int image_check_dcrc (image_header_t *hdr) 180 { 181 ulong data = image_get_data (hdr); 182 ulong len = image_get_data_size (hdr); 183 ulong dcrc = crc32_wd (0, (unsigned char *)data, len, CHUNKSZ_CRC32); 184 185 return (dcrc == image_get_dcrc (hdr)); 186 } 187 188 189 /** 190 * image_multi_count - get component (sub-image) count 191 * @hdr: pointer to the header of the multi component image 192 * 193 * image_multi_count() returns number of components in a multi 194 * component image. 195 * 196 * Note: no checking of the image type is done, caller must pass 197 * a valid multi component image. 198 * 199 * returns: 200 * number of components 201 */ 202 ulong image_multi_count (image_header_t *hdr) 203 { 204 ulong i, count = 0; 205 uint32_t *size; 206 207 /* get start of the image payload, which in case of multi 208 * component images that points to a table of component sizes */ 209 size = (uint32_t *)image_get_data (hdr); 210 211 /* count non empty slots */ 212 for (i = 0; size[i]; ++i) 213 count++; 214 215 return count; 216 } 217 218 /** 219 * image_multi_getimg - get component data address and size 220 * @hdr: pointer to the header of the multi component image 221 * @idx: index of the requested component 222 * @data: pointer to a ulong variable, will hold component data address 223 * @len: pointer to a ulong variable, will hold component size 224 * 225 * image_multi_getimg() returns size and data address for the requested 226 * component in a multi component image. 227 * 228 * Note: no checking of the image type is done, caller must pass 229 * a valid multi component image. 230 * 231 * returns: 232 * data address and size of the component, if idx is valid 233 * 0 in data and len, if idx is out of range 234 */ 235 void image_multi_getimg (image_header_t *hdr, ulong idx, 236 ulong *data, ulong *len) 237 { 238 int i; 239 uint32_t *size; 240 ulong offset, count, img_data; 241 242 /* get number of component */ 243 count = image_multi_count (hdr); 244 245 /* get start of the image payload, which in case of multi 246 * component images that points to a table of component sizes */ 247 size = (uint32_t *)image_get_data (hdr); 248 249 /* get address of the proper component data start, which means 250 * skipping sizes table (add 1 for last, null entry) */ 251 img_data = image_get_data (hdr) + (count + 1) * sizeof (uint32_t); 252 253 if (idx < count) { 254 *len = uimage_to_cpu (size[idx]); 255 offset = 0; 256 257 /* go over all indices preceding requested component idx */ 258 for (i = 0; i < idx; i++) { 259 /* add up i-th component size, rounding up to 4 bytes */ 260 offset += (uimage_to_cpu (size[i]) + 3) & ~3 ; 261 } 262 263 /* calculate idx-th component data address */ 264 *data = img_data + offset; 265 } else { 266 *len = 0; 267 *data = 0; 268 } 269 } 270 271 static void image_print_type (image_header_t *hdr) 272 { 273 const char *os, *arch, *type, *comp; 274 275 os = genimg_get_os_name (image_get_os (hdr)); 276 arch = genimg_get_arch_name (image_get_arch (hdr)); 277 type = genimg_get_type_name (image_get_type (hdr)); 278 comp = genimg_get_comp_name (image_get_comp (hdr)); 279 280 printf ("%s %s %s (%s)\n", arch, os, type, comp); 281 } 282 283 /** 284 * image_print_contents - prints out the contents of the legacy format image 285 * @hdr: pointer to the legacy format image header 286 * @p: pointer to prefix string 287 * 288 * image_print_contents() formats a multi line legacy image contents description. 289 * The routine prints out all header fields followed by the size/offset data 290 * for MULTI/SCRIPT images. 291 * 292 * returns: 293 * no returned results 294 */ 295 void image_print_contents (image_header_t *hdr) 296 { 297 const char *p; 298 299 #ifdef USE_HOSTCC 300 p = ""; 301 #else 302 p = " "; 303 #endif 304 305 printf ("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name (hdr)); 306 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC) 307 printf ("%sCreated: ", p); 308 genimg_print_time ((time_t)image_get_time (hdr)); 309 #endif 310 printf ("%sImage Type: ", p); 311 image_print_type (hdr); 312 printf ("%sData Size: ", p); 313 genimg_print_size (image_get_data_size (hdr)); 314 printf ("%sLoad Address: %08x\n", p, image_get_load (hdr)); 315 printf ("%sEntry Point: %08x\n", p, image_get_ep (hdr)); 316 317 if (image_check_type (hdr, IH_TYPE_MULTI) || 318 image_check_type (hdr, IH_TYPE_SCRIPT)) { 319 int i; 320 ulong data, len; 321 ulong count = image_multi_count (hdr); 322 323 printf ("%sContents:\n", p); 324 for (i = 0; i < count; i++) { 325 image_multi_getimg (hdr, i, &data, &len); 326 327 printf ("%s Image %d: ", p, i); 328 genimg_print_size (len); 329 330 if (image_check_type (hdr, IH_TYPE_SCRIPT) && i > 0) { 331 /* 332 * the user may need to know offsets 333 * if planning to do something with 334 * multiple files 335 */ 336 printf ("%s Offset = 0x%08lx\n", p, data); 337 } 338 } 339 } 340 } 341 342 343 #ifndef USE_HOSTCC 344 /** 345 * image_get_ramdisk - get and verify ramdisk image 346 * @rd_addr: ramdisk image start address 347 * @arch: expected ramdisk architecture 348 * @verify: checksum verification flag 349 * 350 * image_get_ramdisk() returns a pointer to the verified ramdisk image 351 * header. Routine receives image start address and expected architecture 352 * flag. Verification done covers data and header integrity and os/type/arch 353 * fields checking. 354 * 355 * If dataflash support is enabled routine checks for dataflash addresses 356 * and handles required dataflash reads. 357 * 358 * returns: 359 * pointer to a ramdisk image header, if image was found and valid 360 * otherwise, return NULL 361 */ 362 static image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch, 363 int verify) 364 { 365 image_header_t *rd_hdr = (image_header_t *)rd_addr; 366 367 if (!image_check_magic (rd_hdr)) { 368 puts ("Bad Magic Number\n"); 369 show_boot_progress (-10); 370 return NULL; 371 } 372 373 if (!image_check_hcrc (rd_hdr)) { 374 puts ("Bad Header Checksum\n"); 375 show_boot_progress (-11); 376 return NULL; 377 } 378 379 show_boot_progress (10); 380 image_print_contents (rd_hdr); 381 382 if (verify) { 383 puts(" Verifying Checksum ... "); 384 if (!image_check_dcrc (rd_hdr)) { 385 puts ("Bad Data CRC\n"); 386 show_boot_progress (-12); 387 return NULL; 388 } 389 puts("OK\n"); 390 } 391 392 show_boot_progress (11); 393 394 if (!image_check_os (rd_hdr, IH_OS_LINUX) || 395 !image_check_arch (rd_hdr, arch) || 396 !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) { 397 printf ("No Linux %s Ramdisk Image\n", 398 genimg_get_arch_name(arch)); 399 show_boot_progress (-13); 400 return NULL; 401 } 402 403 return rd_hdr; 404 } 405 #endif /* !USE_HOSTCC */ 406 407 /*****************************************************************************/ 408 /* Shared dual-format routines */ 409 /*****************************************************************************/ 410 #ifndef USE_HOSTCC 411 int getenv_yesno (char *var) 412 { 413 char *s = getenv (var); 414 return (s && (*s == 'n')) ? 0 : 1; 415 } 416 417 ulong getenv_bootm_low(void) 418 { 419 char *s = getenv ("bootm_low"); 420 if (s) { 421 ulong tmp = simple_strtoul (s, NULL, 16); 422 return tmp; 423 } 424 425 #if defined(CFG_SDRAM_BASE) 426 return CFG_SDRAM_BASE; 427 #elif defined(CONFIG_ARM) 428 return gd->bd->bi_dram[0].start; 429 #else 430 return 0; 431 #endif 432 } 433 434 ulong getenv_bootm_size(void) 435 { 436 char *s = getenv ("bootm_size"); 437 if (s) { 438 ulong tmp = simple_strtoul (s, NULL, 16); 439 return tmp; 440 } 441 442 #if defined(CONFIG_ARM) 443 return gd->bd->bi_dram[0].size; 444 #else 445 return gd->bd->bi_memsize; 446 #endif 447 } 448 449 void memmove_wd (void *to, void *from, size_t len, ulong chunksz) 450 { 451 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG) 452 while (len > 0) { 453 size_t tail = (len > chunksz) ? chunksz : len; 454 WATCHDOG_RESET (); 455 memmove (to, from, tail); 456 to += tail; 457 from += tail; 458 len -= tail; 459 } 460 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */ 461 memmove (to, from, len); 462 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */ 463 } 464 #endif /* !USE_HOSTCC */ 465 466 static void genimg_print_size (uint32_t size) 467 { 468 #ifndef USE_HOSTCC 469 printf ("%d Bytes = ", size); 470 print_size (size, "\n"); 471 #else 472 printf ("%d Bytes = %.2f kB = %.2f MB\n", 473 size, (double)size / 1.024e3, 474 (double)size / 1.048576e6); 475 #endif 476 } 477 478 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC) 479 static void genimg_print_time (time_t timestamp) 480 { 481 #ifndef USE_HOSTCC 482 struct rtc_time tm; 483 484 to_tm (timestamp, &tm); 485 printf ("%4d-%02d-%02d %2d:%02d:%02d UTC\n", 486 tm.tm_year, tm.tm_mon, tm.tm_mday, 487 tm.tm_hour, tm.tm_min, tm.tm_sec); 488 #else 489 printf ("%s", ctime(×tamp)); 490 #endif 491 } 492 #endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */ 493 494 /** 495 * get_table_entry_name - translate entry id to long name 496 * @table: pointer to a translation table for entries of a specific type 497 * @msg: message to be returned when translation fails 498 * @id: entry id to be translated 499 * 500 * get_table_entry_name() will go over translation table trying to find 501 * entry that matches given id. If matching entry is found, its long 502 * name is returned to the caller. 503 * 504 * returns: 505 * long entry name if translation succeeds 506 * msg otherwise 507 */ 508 static char *get_table_entry_name (table_entry_t *table, char *msg, int id) 509 { 510 for (; table->id >= 0; ++table) { 511 if (table->id == id) 512 return (table->lname); 513 } 514 return (msg); 515 } 516 517 const char *genimg_get_os_name (uint8_t os) 518 { 519 return (get_table_entry_name (uimage_os, "Unknown OS", os)); 520 } 521 522 const char *genimg_get_arch_name (uint8_t arch) 523 { 524 return (get_table_entry_name (uimage_arch, "Unknown Architecture", arch)); 525 } 526 527 const char *genimg_get_type_name (uint8_t type) 528 { 529 return (get_table_entry_name (uimage_type, "Unknown Image", type)); 530 } 531 532 const char *genimg_get_comp_name (uint8_t comp) 533 { 534 return (get_table_entry_name (uimage_comp, "Unknown Compression", comp)); 535 } 536 537 /** 538 * get_table_entry_id - translate short entry name to id 539 * @table: pointer to a translation table for entries of a specific type 540 * @table_name: to be used in case of error 541 * @name: entry short name to be translated 542 * 543 * get_table_entry_id() will go over translation table trying to find 544 * entry that matches given short name. If matching entry is found, 545 * its id returned to the caller. 546 * 547 * returns: 548 * entry id if translation succeeds 549 * -1 otherwise 550 */ 551 static int get_table_entry_id (table_entry_t *table, 552 const char *table_name, const char *name) 553 { 554 table_entry_t *t; 555 #ifdef USE_HOSTCC 556 int first = 1; 557 558 for (t = table; t->id >= 0; ++t) { 559 if (t->sname && strcasecmp(t->sname, name) == 0) 560 return (t->id); 561 } 562 563 fprintf (stderr, "\nInvalid %s Type - valid names are", table_name); 564 for (t = table; t->id >= 0; ++t) { 565 if (t->sname == NULL) 566 continue; 567 fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname); 568 first = 0; 569 } 570 fprintf (stderr, "\n"); 571 #else 572 for (t = table; t->id >= 0; ++t) { 573 if (t->sname && strcmp(t->sname, name) == 0) 574 return (t->id); 575 } 576 debug ("Invalid %s Type: %s\n", table_name, name); 577 #endif /* USE_HOSTCC */ 578 return (-1); 579 } 580 581 int genimg_get_os_id (const char *name) 582 { 583 return (get_table_entry_id (uimage_os, "OS", name)); 584 } 585 586 int genimg_get_arch_id (const char *name) 587 { 588 return (get_table_entry_id (uimage_arch, "CPU", name)); 589 } 590 591 int genimg_get_type_id (const char *name) 592 { 593 return (get_table_entry_id (uimage_type, "Image", name)); 594 } 595 596 int genimg_get_comp_id (const char *name) 597 { 598 return (get_table_entry_id (uimage_comp, "Compression", name)); 599 } 600 601 #ifndef USE_HOSTCC 602 /** 603 * genimg_get_format - get image format type 604 * @img_addr: image start address 605 * 606 * genimg_get_format() checks whether provided address points to a valid 607 * legacy or FIT image. 608 * 609 * New uImage format and FDT blob are based on a libfdt. FDT blob 610 * may be passed directly or embedded in a FIT image. In both situations 611 * genimg_get_format() must be able to dectect libfdt header. 612 * 613 * returns: 614 * image format type or IMAGE_FORMAT_INVALID if no image is present 615 */ 616 int genimg_get_format (void *img_addr) 617 { 618 ulong format = IMAGE_FORMAT_INVALID; 619 image_header_t *hdr; 620 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT) 621 char *fit_hdr; 622 #endif 623 624 hdr = (image_header_t *)img_addr; 625 if (image_check_magic(hdr)) 626 format = IMAGE_FORMAT_LEGACY; 627 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT) 628 else { 629 fit_hdr = (char *)img_addr; 630 if (fdt_check_header (fit_hdr) == 0) 631 format = IMAGE_FORMAT_FIT; 632 } 633 #endif 634 635 return format; 636 } 637 638 /** 639 * genimg_get_image - get image from special storage (if necessary) 640 * @img_addr: image start address 641 * 642 * genimg_get_image() checks if provided image start adddress is located 643 * in a dataflash storage. If so, image is moved to a system RAM memory. 644 * 645 * returns: 646 * image start address after possible relocation from special storage 647 */ 648 ulong genimg_get_image (ulong img_addr) 649 { 650 ulong ram_addr = img_addr; 651 652 #ifdef CONFIG_HAS_DATAFLASH 653 ulong h_size, d_size; 654 655 if (addr_dataflash (img_addr)){ 656 /* ger RAM address */ 657 ram_addr = CFG_LOAD_ADDR; 658 659 /* get header size */ 660 h_size = image_get_header_size (); 661 #if defined(CONFIG_FIT) 662 if (sizeof(struct fdt_header) > h_size) 663 h_size = sizeof(struct fdt_header); 664 #endif 665 666 /* read in header */ 667 debug (" Reading image header from dataflash address " 668 "%08lx to RAM address %08lx\n", img_addr, ram_addr); 669 670 read_dataflash (img_addr, h_size, (char *)ram_addr); 671 672 /* get data size */ 673 switch (genimg_get_format ((void *)ram_addr)) { 674 case IMAGE_FORMAT_LEGACY: 675 d_size = image_get_data_size ((image_header_t *)ram_addr); 676 debug (" Legacy format image found at 0x%08lx, size 0x%08lx\n", 677 ram_addr, d_size); 678 break; 679 #if defined(CONFIG_FIT) 680 case IMAGE_FORMAT_FIT: 681 d_size = fit_get_size ((const void *)ram_addr) - h_size; 682 debug (" FIT/FDT format image found at 0x%08lx, size 0x%08lx\n", 683 ram_addr, d_size); 684 break; 685 #endif 686 default: 687 printf (" No valid image found at 0x%08lx\n", img_addr); 688 return ram_addr; 689 } 690 691 /* read in image data */ 692 debug (" Reading image remaining data from dataflash address " 693 "%08lx to RAM address %08lx\n", img_addr + h_size, 694 ram_addr + h_size); 695 696 read_dataflash (img_addr + h_size, d_size, 697 (char *)(ram_addr + h_size)); 698 699 } 700 #endif /* CONFIG_HAS_DATAFLASH */ 701 702 return ram_addr; 703 } 704 705 /** 706 * fit_has_config - check if there is a valid FIT configuration 707 * @images: pointer to the bootm command headers structure 708 * 709 * fit_has_config() checks if there is a FIT configuration in use 710 * (if FTI support is present). 711 * 712 * returns: 713 * 0, no FIT support or no configuration found 714 * 1, configuration found 715 */ 716 int genimg_has_config (bootm_headers_t *images) 717 { 718 #if defined(CONFIG_FIT) 719 if (images->fit_uname_cfg) 720 return 1; 721 #endif 722 return 0; 723 } 724 725 /** 726 * boot_get_ramdisk - main ramdisk handling routine 727 * @argc: command argument count 728 * @argv: command argument list 729 * @images: pointer to the bootm images structure 730 * @arch: expected ramdisk architecture 731 * @rd_start: pointer to a ulong variable, will hold ramdisk start address 732 * @rd_end: pointer to a ulong variable, will hold ramdisk end 733 * 734 * boot_get_ramdisk() is responsible for finding a valid ramdisk image. 735 * Curently supported are the following ramdisk sources: 736 * - multicomponent kernel/ramdisk image, 737 * - commandline provided address of decicated ramdisk image. 738 * 739 * returns: 740 * 0, if ramdisk image was found and valid, or skiped 741 * rd_start and rd_end are set to ramdisk start/end addresses if 742 * ramdisk image is found and valid 743 * 744 * 1, if ramdisk image is found but corrupted 745 * rd_start and rd_end are set to 0 if no ramdisk exists 746 */ 747 int boot_get_ramdisk (int argc, char *argv[], bootm_headers_t *images, 748 uint8_t arch, ulong *rd_start, ulong *rd_end) 749 { 750 ulong rd_addr, rd_load; 751 ulong rd_data, rd_len; 752 image_header_t *rd_hdr; 753 #if defined(CONFIG_FIT) 754 void *fit_hdr; 755 const char *fit_uname_config = NULL; 756 const char *fit_uname_ramdisk = NULL; 757 ulong default_addr; 758 int rd_noffset; 759 int cfg_noffset; 760 const void *data; 761 size_t size; 762 #endif 763 764 *rd_start = 0; 765 *rd_end = 0; 766 767 /* 768 * Look for a '-' which indicates to ignore the 769 * ramdisk argument 770 */ 771 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) { 772 debug ("## Skipping init Ramdisk\n"); 773 rd_len = rd_data = 0; 774 } else if (argc >= 3 || genimg_has_config (images)) { 775 #if defined(CONFIG_FIT) 776 if (argc >= 3) { 777 /* 778 * If the init ramdisk comes from the FIT image and 779 * the FIT image address is omitted in the command 780 * line argument, try to use os FIT image address or 781 * default load address. 782 */ 783 if (images->fit_uname_os) 784 default_addr = (ulong)images->fit_hdr_os; 785 else 786 default_addr = load_addr; 787 788 if (fit_parse_conf (argv[2], default_addr, 789 &rd_addr, &fit_uname_config)) { 790 debug ("* ramdisk: config '%s' from image at 0x%08lx\n", 791 fit_uname_config, rd_addr); 792 } else if (fit_parse_subimage (argv[2], default_addr, 793 &rd_addr, &fit_uname_ramdisk)) { 794 debug ("* ramdisk: subimage '%s' from image at 0x%08lx\n", 795 fit_uname_ramdisk, rd_addr); 796 } else 797 #endif 798 { 799 rd_addr = simple_strtoul(argv[2], NULL, 16); 800 debug ("* ramdisk: cmdline image address = 0x%08lx\n", 801 rd_addr); 802 } 803 #if defined(CONFIG_FIT) 804 } else { 805 /* use FIT configuration provided in first bootm 806 * command argument 807 */ 808 rd_addr = (ulong)images->fit_hdr_os; 809 fit_uname_config = images->fit_uname_cfg; 810 debug ("* ramdisk: using config '%s' from image at 0x%08lx\n", 811 fit_uname_config, rd_addr); 812 813 /* 814 * Check whether configuration has ramdisk defined, 815 * if not, don't try to use it, quit silently. 816 */ 817 fit_hdr = (void *)rd_addr; 818 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config); 819 if (cfg_noffset < 0) { 820 debug ("* ramdisk: no such config\n"); 821 return 0; 822 } 823 824 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset); 825 if (rd_noffset < 0) { 826 debug ("* ramdisk: no ramdisk in config\n"); 827 return 0; 828 } 829 } 830 #endif 831 832 /* copy from dataflash if needed */ 833 rd_addr = genimg_get_image (rd_addr); 834 835 /* 836 * Check if there is an initrd image at the 837 * address provided in the second bootm argument 838 * check image type, for FIT images get FIT node. 839 */ 840 switch (genimg_get_format ((void *)rd_addr)) { 841 case IMAGE_FORMAT_LEGACY: 842 printf ("## Loading init Ramdisk from Legacy " 843 "Image at %08lx ...\n", rd_addr); 844 845 show_boot_progress (9); 846 rd_hdr = image_get_ramdisk (rd_addr, arch, 847 images->verify); 848 849 if (rd_hdr == NULL) 850 return 1; 851 852 rd_data = image_get_data (rd_hdr); 853 rd_len = image_get_data_size (rd_hdr); 854 rd_load = image_get_load (rd_hdr); 855 break; 856 #if defined(CONFIG_FIT) 857 case IMAGE_FORMAT_FIT: 858 fit_hdr = (void *)rd_addr; 859 printf ("## Loading init Ramdisk from FIT " 860 "Image at %08lx ...\n", rd_addr); 861 862 show_boot_progress (120); 863 if (!fit_check_format (fit_hdr)) { 864 puts ("Bad FIT ramdisk image format!\n"); 865 show_boot_progress (-120); 866 return 0; 867 } 868 show_boot_progress (121); 869 870 if (!fit_uname_ramdisk) { 871 /* 872 * no ramdisk image node unit name, try to get config 873 * node first. If config unit node name is NULL 874 * fit_conf_get_node() will try to find default config node 875 */ 876 show_boot_progress (122); 877 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config); 878 if (cfg_noffset < 0) { 879 puts ("Could not find configuration node\n"); 880 show_boot_progress (-122); 881 return 0; 882 } 883 fit_uname_config = fdt_get_name (fit_hdr, cfg_noffset, NULL); 884 printf (" Using '%s' configuration\n", fit_uname_config); 885 886 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset); 887 fit_uname_ramdisk = fit_get_name (fit_hdr, rd_noffset, NULL); 888 } else { 889 /* get ramdisk component image node offset */ 890 show_boot_progress (123); 891 rd_noffset = fit_image_get_node (fit_hdr, fit_uname_ramdisk); 892 } 893 if (rd_noffset < 0) { 894 puts ("Could not find subimage node\n"); 895 show_boot_progress (-124); 896 return 0; 897 } 898 899 printf (" Trying '%s' ramdisk subimage\n", fit_uname_ramdisk); 900 901 show_boot_progress (125); 902 if (!fit_check_ramdisk (fit_hdr, rd_noffset, arch, images->verify)) 903 return 0; 904 905 /* get ramdisk image data address and length */ 906 if (fit_image_get_data (fit_hdr, rd_noffset, &data, &size)) { 907 puts ("Could not find ramdisk subimage data!\n"); 908 show_boot_progress (-127); 909 return 0; 910 } 911 show_boot_progress (128); 912 913 rd_data = (ulong)data; 914 rd_len = size; 915 916 if (fit_image_get_load (fit_hdr, rd_noffset, &rd_load)) { 917 puts ("Can't get ramdisk subimage load address!\n"); 918 show_boot_progress (-129); 919 return 0; 920 } 921 show_boot_progress (129); 922 923 images->fit_hdr_rd = fit_hdr; 924 images->fit_uname_rd = fit_uname_ramdisk; 925 images->fit_noffset_rd = rd_noffset; 926 break; 927 #endif 928 default: 929 puts ("Wrong Ramdisk Image Format\n"); 930 rd_data = rd_len = rd_load = 0; 931 } 932 933 #if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO) 934 /* 935 * We need to copy the ramdisk to SRAM to let Linux boot 936 */ 937 if (rd_data) { 938 memmove ((void *)rd_load, (uchar *)rd_data, rd_len); 939 rd_data = rd_load; 940 } 941 #endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */ 942 943 } else if (images->legacy_hdr_valid && 944 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) { 945 /* 946 * Now check if we have a legacy mult-component image, 947 * get second entry data start address and len. 948 */ 949 show_boot_progress (13); 950 printf ("## Loading init Ramdisk from multi component " 951 "Legacy Image at %08lx ...\n", 952 (ulong)images->legacy_hdr_os); 953 954 image_multi_getimg (images->legacy_hdr_os, 1, &rd_data, &rd_len); 955 } else { 956 /* 957 * no initrd image 958 */ 959 show_boot_progress (14); 960 rd_len = rd_data = 0; 961 } 962 963 if (!rd_data) { 964 debug ("## No init Ramdisk\n"); 965 } else { 966 *rd_start = rd_data; 967 *rd_end = rd_data + rd_len; 968 } 969 debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n", 970 *rd_start, *rd_end); 971 972 return 0; 973 } 974 975 #if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_SPARC) 976 /** 977 * boot_ramdisk_high - relocate init ramdisk 978 * @lmb: pointer to lmb handle, will be used for memory mgmt 979 * @rd_data: ramdisk data start address 980 * @rd_len: ramdisk data length 981 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk 982 * start address (after possible relocation) 983 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk 984 * end address (after possible relocation) 985 * 986 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement 987 * variable and if requested ramdisk data is moved to a specified location. 988 * 989 * Initrd_start and initrd_end are set to final (after relocation) ramdisk 990 * start/end addresses if ramdisk image start and len were provided, 991 * otherwise set initrd_start and initrd_end set to zeros. 992 * 993 * returns: 994 * 0 - success 995 * -1 - failure 996 */ 997 int boot_ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len, 998 ulong *initrd_start, ulong *initrd_end) 999 { 1000 char *s; 1001 ulong initrd_high; 1002 int initrd_copy_to_ram = 1; 1003 1004 if ((s = getenv ("initrd_high")) != NULL) { 1005 /* a value of "no" or a similar string will act like 0, 1006 * turning the "load high" feature off. This is intentional. 1007 */ 1008 initrd_high = simple_strtoul (s, NULL, 16); 1009 if (initrd_high == ~0) 1010 initrd_copy_to_ram = 0; 1011 } else { 1012 /* not set, no restrictions to load high */ 1013 initrd_high = ~0; 1014 } 1015 1016 debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n", 1017 initrd_high, initrd_copy_to_ram); 1018 1019 if (rd_data) { 1020 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */ 1021 debug (" in-place initrd\n"); 1022 *initrd_start = rd_data; 1023 *initrd_end = rd_data + rd_len; 1024 lmb_reserve(lmb, rd_data, rd_len); 1025 } else { 1026 if (initrd_high) 1027 *initrd_start = lmb_alloc_base (lmb, rd_len, 0x1000, initrd_high); 1028 else 1029 *initrd_start = lmb_alloc (lmb, rd_len, 0x1000); 1030 1031 if (*initrd_start == 0) { 1032 puts ("ramdisk - allocation error\n"); 1033 goto error; 1034 } 1035 show_boot_progress (12); 1036 1037 *initrd_end = *initrd_start + rd_len; 1038 printf (" Loading Ramdisk to %08lx, end %08lx ... ", 1039 *initrd_start, *initrd_end); 1040 1041 memmove_wd ((void *)*initrd_start, 1042 (void *)rd_data, rd_len, CHUNKSZ); 1043 1044 puts ("OK\n"); 1045 } 1046 } else { 1047 *initrd_start = 0; 1048 *initrd_end = 0; 1049 } 1050 debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n", 1051 *initrd_start, *initrd_end); 1052 1053 return 0; 1054 1055 error: 1056 return -1; 1057 } 1058 1059 /** 1060 * boot_get_cmdline - allocate and initialize kernel cmdline 1061 * @lmb: pointer to lmb handle, will be used for memory mgmt 1062 * @cmd_start: pointer to a ulong variable, will hold cmdline start 1063 * @cmd_end: pointer to a ulong variable, will hold cmdline end 1064 * @bootmap_base: ulong variable, holds offset in physical memory to 1065 * base of bootmap 1066 * 1067 * boot_get_cmdline() allocates space for kernel command line below 1068 * BOOTMAPSZ + bootmap_base address. If "bootargs" U-boot environemnt 1069 * variable is present its contents is copied to allocated kernel 1070 * command line. 1071 * 1072 * returns: 1073 * 0 - success 1074 * -1 - failure 1075 */ 1076 int boot_get_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end, 1077 ulong bootmap_base) 1078 { 1079 char *cmdline; 1080 char *s; 1081 1082 cmdline = (char *)lmb_alloc_base(lmb, CFG_BARGSIZE, 0xf, 1083 CFG_BOOTMAPSZ + bootmap_base); 1084 1085 if (cmdline == NULL) 1086 return -1; 1087 1088 if ((s = getenv("bootargs")) == NULL) 1089 s = ""; 1090 1091 strcpy(cmdline, s); 1092 1093 *cmd_start = (ulong) & cmdline[0]; 1094 *cmd_end = *cmd_start + strlen(cmdline); 1095 1096 debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end); 1097 1098 return 0; 1099 } 1100 1101 /** 1102 * boot_get_kbd - allocate and initialize kernel copy of board info 1103 * @lmb: pointer to lmb handle, will be used for memory mgmt 1104 * @kbd: double pointer to board info data 1105 * @bootmap_base: ulong variable, holds offset in physical memory to 1106 * base of bootmap 1107 * 1108 * boot_get_kbd() allocates space for kernel copy of board info data below 1109 * BOOTMAPSZ + bootmap_base address and kernel board info is initialized with 1110 * the current u-boot board info data. 1111 * 1112 * returns: 1113 * 0 - success 1114 * -1 - failure 1115 */ 1116 int boot_get_kbd (struct lmb *lmb, bd_t **kbd, ulong bootmap_base) 1117 { 1118 *kbd = (bd_t *)lmb_alloc_base(lmb, sizeof(bd_t), 0xf, 1119 CFG_BOOTMAPSZ + bootmap_base); 1120 if (*kbd == NULL) 1121 return -1; 1122 1123 **kbd = *(gd->bd); 1124 1125 debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd); 1126 1127 #if defined(DEBUG) && defined(CONFIG_CMD_BDI) 1128 do_bdinfo(NULL, 0, 0, NULL); 1129 #endif 1130 1131 return 0; 1132 } 1133 #endif /* CONFIG_PPC || CONFIG_M68K */ 1134 #endif /* !USE_HOSTCC */ 1135 1136 #if defined(CONFIG_FIT) 1137 /*****************************************************************************/ 1138 /* New uImage format routines */ 1139 /*****************************************************************************/ 1140 #ifndef USE_HOSTCC 1141 static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr, 1142 ulong *addr, const char **name) 1143 { 1144 const char *sep; 1145 1146 *addr = addr_curr; 1147 *name = NULL; 1148 1149 sep = strchr (spec, sepc); 1150 if (sep) { 1151 if (sep - spec > 0) 1152 *addr = simple_strtoul (spec, NULL, 16); 1153 1154 *name = sep + 1; 1155 return 1; 1156 } 1157 1158 return 0; 1159 } 1160 1161 /** 1162 * fit_parse_conf - parse FIT configuration spec 1163 * @spec: input string, containing configuration spec 1164 * @add_curr: current image address (to be used as a possible default) 1165 * @addr: pointer to a ulong variable, will hold FIT image address of a given 1166 * configuration 1167 * @conf_name double pointer to a char, will hold pointer to a configuration 1168 * unit name 1169 * 1170 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>, 1171 * where <addr> is a FIT image address that contains configuration 1172 * with a <conf> unit name. 1173 * 1174 * Address part is optional, and if omitted default add_curr will 1175 * be used instead. 1176 * 1177 * returns: 1178 * 1 if spec is a valid configuration string, 1179 * addr and conf_name are set accordingly 1180 * 0 otherwise 1181 */ 1182 inline int fit_parse_conf (const char *spec, ulong addr_curr, 1183 ulong *addr, const char **conf_name) 1184 { 1185 return fit_parse_spec (spec, '#', addr_curr, addr, conf_name); 1186 } 1187 1188 /** 1189 * fit_parse_subimage - parse FIT subimage spec 1190 * @spec: input string, containing subimage spec 1191 * @add_curr: current image address (to be used as a possible default) 1192 * @addr: pointer to a ulong variable, will hold FIT image address of a given 1193 * subimage 1194 * @image_name: double pointer to a char, will hold pointer to a subimage name 1195 * 1196 * fit_parse_subimage() expects subimage spec in the for of 1197 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains 1198 * subimage with a <subimg> unit name. 1199 * 1200 * Address part is optional, and if omitted default add_curr will 1201 * be used instead. 1202 * 1203 * returns: 1204 * 1 if spec is a valid subimage string, 1205 * addr and image_name are set accordingly 1206 * 0 otherwise 1207 */ 1208 inline int fit_parse_subimage (const char *spec, ulong addr_curr, 1209 ulong *addr, const char **image_name) 1210 { 1211 return fit_parse_spec (spec, ':', addr_curr, addr, image_name); 1212 } 1213 #endif /* !USE_HOSTCC */ 1214 1215 static void fit_get_debug (const void *fit, int noffset, 1216 char *prop_name, int err) 1217 { 1218 debug ("Can't get '%s' property from FIT 0x%08lx, " 1219 "node: offset %d, name %s (%s)\n", 1220 prop_name, (ulong)fit, noffset, 1221 fit_get_name (fit, noffset, NULL), 1222 fdt_strerror (err)); 1223 } 1224 1225 /** 1226 * fit_print_contents - prints out the contents of the FIT format image 1227 * @fit: pointer to the FIT format image header 1228 * @p: pointer to prefix string 1229 * 1230 * fit_print_contents() formats a multi line FIT image contents description. 1231 * The routine prints out FIT image properties (root node level) follwed by 1232 * the details of each component image. 1233 * 1234 * returns: 1235 * no returned results 1236 */ 1237 void fit_print_contents (const void *fit) 1238 { 1239 char *desc; 1240 char *uname; 1241 int images_noffset; 1242 int confs_noffset; 1243 int noffset; 1244 int ndepth; 1245 int count = 0; 1246 int ret; 1247 const char *p; 1248 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC) 1249 time_t timestamp; 1250 #endif 1251 1252 #ifdef USE_HOSTCC 1253 p = ""; 1254 #else 1255 p = " "; 1256 #endif 1257 1258 /* Root node properties */ 1259 ret = fit_get_desc (fit, 0, &desc); 1260 printf ("%sFIT description: ", p); 1261 if (ret) 1262 printf ("unavailable\n"); 1263 else 1264 printf ("%s\n", desc); 1265 1266 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC) 1267 ret = fit_get_timestamp (fit, 0, ×tamp); 1268 printf ("%sCreated: ", p); 1269 if (ret) 1270 printf ("unavailable\n"); 1271 else 1272 genimg_print_time (timestamp); 1273 #endif 1274 1275 /* Find images parent node offset */ 1276 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH); 1277 if (images_noffset < 0) { 1278 printf ("Can't find images parent node '%s' (%s)\n", 1279 FIT_IMAGES_PATH, fdt_strerror (images_noffset)); 1280 return; 1281 } 1282 1283 /* Process its subnodes, print out component images details */ 1284 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth); 1285 (noffset >= 0) && (ndepth > 0); 1286 noffset = fdt_next_node (fit, noffset, &ndepth)) { 1287 if (ndepth == 1) { 1288 /* 1289 * Direct child node of the images parent node, 1290 * i.e. component image node. 1291 */ 1292 printf ("%s Image %u (%s)\n", p, count++, 1293 fit_get_name(fit, noffset, NULL)); 1294 1295 fit_image_print (fit, noffset, p); 1296 } 1297 } 1298 1299 /* Find configurations parent node offset */ 1300 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH); 1301 if (confs_noffset < 0) { 1302 debug ("Can't get configurations parent node '%s' (%s)\n", 1303 FIT_CONFS_PATH, fdt_strerror (confs_noffset)); 1304 return; 1305 } 1306 1307 /* get default configuration unit name from default property */ 1308 uname = (char *)fdt_getprop (fit, noffset, FIT_DEFAULT_PROP, NULL); 1309 if (uname) 1310 printf ("%s Default Configuration: '%s'\n", p, uname); 1311 1312 /* Process its subnodes, print out configurations details */ 1313 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, confs_noffset, &ndepth); 1314 (noffset >= 0) && (ndepth > 0); 1315 noffset = fdt_next_node (fit, noffset, &ndepth)) { 1316 if (ndepth == 1) { 1317 /* 1318 * Direct child node of the configurations parent node, 1319 * i.e. configuration node. 1320 */ 1321 printf ("%s Configuration %u (%s)\n", p, count++, 1322 fit_get_name(fit, noffset, NULL)); 1323 1324 fit_conf_print (fit, noffset, p); 1325 } 1326 } 1327 } 1328 1329 /** 1330 * fit_image_print - prints out the FIT component image details 1331 * @fit: pointer to the FIT format image header 1332 * @image_noffset: offset of the component image node 1333 * @p: pointer to prefix string 1334 * 1335 * fit_image_print() lists all mandatory properies for the processed component 1336 * image. If present, hash nodes are printed out as well. 1337 * 1338 * returns: 1339 * no returned results 1340 */ 1341 void fit_image_print (const void *fit, int image_noffset, const char *p) 1342 { 1343 char *desc; 1344 uint8_t type, arch, os, comp; 1345 size_t size; 1346 ulong load, entry; 1347 const void *data; 1348 int noffset; 1349 int ndepth; 1350 int ret; 1351 1352 /* Mandatory properties */ 1353 ret = fit_get_desc (fit, image_noffset, &desc); 1354 printf ("%s Description: ", p); 1355 if (ret) 1356 printf ("unavailable\n"); 1357 else 1358 printf ("%s\n", desc); 1359 1360 fit_image_get_type (fit, image_noffset, &type); 1361 printf ("%s Type: %s\n", p, genimg_get_type_name (type)); 1362 1363 fit_image_get_comp (fit, image_noffset, &comp); 1364 printf ("%s Compression: %s\n", p, genimg_get_comp_name (comp)); 1365 1366 ret = fit_image_get_data (fit, image_noffset, &data, &size); 1367 1368 #ifndef USE_HOSTCC 1369 printf ("%s Data Start: ", p); 1370 if (ret) 1371 printf ("unavailable\n"); 1372 else 1373 printf ("0x%08lx\n", (ulong)data); 1374 #endif 1375 1376 printf ("%s Data Size: ", p); 1377 if (ret) 1378 printf ("unavailable\n"); 1379 else 1380 genimg_print_size (size); 1381 1382 /* Remaining, type dependent properties */ 1383 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) || 1384 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) || 1385 (type == IH_TYPE_FLATDT)) { 1386 fit_image_get_arch (fit, image_noffset, &arch); 1387 printf ("%s Architecture: %s\n", p, genimg_get_arch_name (arch)); 1388 } 1389 1390 if (type == IH_TYPE_KERNEL) { 1391 fit_image_get_os (fit, image_noffset, &os); 1392 printf ("%s OS: %s\n", p, genimg_get_os_name (os)); 1393 } 1394 1395 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) { 1396 ret = fit_image_get_load (fit, image_noffset, &load); 1397 printf ("%s Load Address: ", p); 1398 if (ret) 1399 printf ("unavailable\n"); 1400 else 1401 printf ("0x%08lx\n", load); 1402 1403 fit_image_get_entry (fit, image_noffset, &entry); 1404 printf ("%s Entry Point: ", p); 1405 if (ret) 1406 printf ("unavailable\n"); 1407 else 1408 printf ("0x%08lx\n", entry); 1409 } 1410 1411 /* Process all hash subnodes of the component image node */ 1412 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth); 1413 (noffset >= 0) && (ndepth > 0); 1414 noffset = fdt_next_node (fit, noffset, &ndepth)) { 1415 if (ndepth == 1) { 1416 /* Direct child node of the component image node */ 1417 fit_image_print_hash (fit, noffset, p); 1418 } 1419 } 1420 } 1421 1422 /** 1423 * fit_image_print_hash - prints out the hash node details 1424 * @fit: pointer to the FIT format image header 1425 * @noffset: offset of the hash node 1426 * @p: pointer to prefix string 1427 * 1428 * fit_image_print_hash() lists properies for the processed hash node 1429 * 1430 * returns: 1431 * no returned results 1432 */ 1433 void fit_image_print_hash (const void *fit, int noffset, const char *p) 1434 { 1435 char *algo; 1436 uint8_t *value; 1437 int value_len; 1438 int i, ret; 1439 1440 /* 1441 * Check subnode name, must be equal to "hash". 1442 * Multiple hash nodes require unique unit node 1443 * names, e.g. hash@1, hash@2, etc. 1444 */ 1445 if (strncmp (fit_get_name(fit, noffset, NULL), 1446 FIT_HASH_NODENAME, 1447 strlen(FIT_HASH_NODENAME)) != 0) 1448 return; 1449 1450 debug ("%s Hash node: '%s'\n", p, 1451 fit_get_name (fit, noffset, NULL)); 1452 1453 printf ("%s Hash algo: ", p); 1454 if (fit_image_hash_get_algo (fit, noffset, &algo)) { 1455 printf ("invalid/unsupported\n"); 1456 return; 1457 } 1458 printf ("%s\n", algo); 1459 1460 ret = fit_image_hash_get_value (fit, noffset, &value, 1461 &value_len); 1462 printf ("%s Hash value: ", p); 1463 if (ret) { 1464 printf ("unavailable\n"); 1465 } else { 1466 for (i = 0; i < value_len; i++) 1467 printf ("%02x", value[i]); 1468 printf ("\n"); 1469 } 1470 1471 debug ("%s Hash len: %d\n", p, value_len); 1472 } 1473 1474 /** 1475 * fit_get_desc - get node description property 1476 * @fit: pointer to the FIT format image header 1477 * @noffset: node offset 1478 * @desc: double pointer to the char, will hold pointer to the descrption 1479 * 1480 * fit_get_desc() reads description property from a given node, if 1481 * description is found pointer to it is returened in third call argument. 1482 * 1483 * returns: 1484 * 0, on success 1485 * -1, on failure 1486 */ 1487 int fit_get_desc (const void *fit, int noffset, char **desc) 1488 { 1489 int len; 1490 1491 *desc = (char *)fdt_getprop (fit, noffset, FIT_DESC_PROP, &len); 1492 if (*desc == NULL) { 1493 fit_get_debug (fit, noffset, FIT_DESC_PROP, len); 1494 return -1; 1495 } 1496 1497 return 0; 1498 } 1499 1500 /** 1501 * fit_get_timestamp - get node timestamp property 1502 * @fit: pointer to the FIT format image header 1503 * @noffset: node offset 1504 * @timestamp: pointer to the time_t, will hold read timestamp 1505 * 1506 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp 1507 * is found and has a correct size its value is retured in third call 1508 * argument. 1509 * 1510 * returns: 1511 * 0, on success 1512 * -1, on property read failure 1513 * -2, on wrong timestamp size 1514 */ 1515 int fit_get_timestamp (const void *fit, int noffset, time_t *timestamp) 1516 { 1517 int len; 1518 const void *data; 1519 1520 data = fdt_getprop (fit, noffset, FIT_TIMESTAMP_PROP, &len); 1521 if (data == NULL) { 1522 fit_get_debug (fit, noffset, FIT_TIMESTAMP_PROP, len); 1523 return -1; 1524 } 1525 if (len != sizeof (uint32_t)) { 1526 debug ("FIT timestamp with incorrect size of (%u)\n", len); 1527 return -2; 1528 } 1529 1530 *timestamp = uimage_to_cpu (*((uint32_t *)data)); 1531 return 0; 1532 } 1533 1534 /** 1535 * fit_image_get_node - get node offset for component image of a given unit name 1536 * @fit: pointer to the FIT format image header 1537 * @image_uname: component image node unit name 1538 * 1539 * fit_image_get_node() finds a component image (withing the '/images' 1540 * node) of a provided unit name. If image is found its node offset is 1541 * returned to the caller. 1542 * 1543 * returns: 1544 * image node offset when found (>=0) 1545 * negative number on failure (FDT_ERR_* code) 1546 */ 1547 int fit_image_get_node (const void *fit, const char *image_uname) 1548 { 1549 int noffset, images_noffset; 1550 1551 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH); 1552 if (images_noffset < 0) { 1553 debug ("Can't find images parent node '%s' (%s)\n", 1554 FIT_IMAGES_PATH, fdt_strerror (images_noffset)); 1555 return images_noffset; 1556 } 1557 1558 noffset = fdt_subnode_offset (fit, images_noffset, image_uname); 1559 if (noffset < 0) { 1560 debug ("Can't get node offset for image unit name: '%s' (%s)\n", 1561 image_uname, fdt_strerror (noffset)); 1562 } 1563 1564 return noffset; 1565 } 1566 1567 /** 1568 * fit_image_get_os - get os id for a given component image node 1569 * @fit: pointer to the FIT format image header 1570 * @noffset: component image node offset 1571 * @os: pointer to the uint8_t, will hold os numeric id 1572 * 1573 * fit_image_get_os() finds os property in a given component image node. 1574 * If the property is found, its (string) value is translated to the numeric 1575 * id which is returned to the caller. 1576 * 1577 * returns: 1578 * 0, on success 1579 * -1, on failure 1580 */ 1581 int fit_image_get_os (const void *fit, int noffset, uint8_t *os) 1582 { 1583 int len; 1584 const void *data; 1585 1586 /* Get OS name from property data */ 1587 data = fdt_getprop (fit, noffset, FIT_OS_PROP, &len); 1588 if (data == NULL) { 1589 fit_get_debug (fit, noffset, FIT_OS_PROP, len); 1590 *os = -1; 1591 return -1; 1592 } 1593 1594 /* Translate OS name to id */ 1595 *os = genimg_get_os_id (data); 1596 return 0; 1597 } 1598 1599 /** 1600 * fit_image_get_arch - get arch id for a given component image node 1601 * @fit: pointer to the FIT format image header 1602 * @noffset: component image node offset 1603 * @arch: pointer to the uint8_t, will hold arch numeric id 1604 * 1605 * fit_image_get_arch() finds arch property in a given component image node. 1606 * If the property is found, its (string) value is translated to the numeric 1607 * id which is returned to the caller. 1608 * 1609 * returns: 1610 * 0, on success 1611 * -1, on failure 1612 */ 1613 int fit_image_get_arch (const void *fit, int noffset, uint8_t *arch) 1614 { 1615 int len; 1616 const void *data; 1617 1618 /* Get architecture name from property data */ 1619 data = fdt_getprop (fit, noffset, FIT_ARCH_PROP, &len); 1620 if (data == NULL) { 1621 fit_get_debug (fit, noffset, FIT_ARCH_PROP, len); 1622 *arch = -1; 1623 return -1; 1624 } 1625 1626 /* Translate architecture name to id */ 1627 *arch = genimg_get_arch_id (data); 1628 return 0; 1629 } 1630 1631 /** 1632 * fit_image_get_type - get type id for a given component image node 1633 * @fit: pointer to the FIT format image header 1634 * @noffset: component image node offset 1635 * @type: pointer to the uint8_t, will hold type numeric id 1636 * 1637 * fit_image_get_type() finds type property in a given component image node. 1638 * If the property is found, its (string) value is translated to the numeric 1639 * id which is returned to the caller. 1640 * 1641 * returns: 1642 * 0, on success 1643 * -1, on failure 1644 */ 1645 int fit_image_get_type (const void *fit, int noffset, uint8_t *type) 1646 { 1647 int len; 1648 const void *data; 1649 1650 /* Get image type name from property data */ 1651 data = fdt_getprop (fit, noffset, FIT_TYPE_PROP, &len); 1652 if (data == NULL) { 1653 fit_get_debug (fit, noffset, FIT_TYPE_PROP, len); 1654 *type = -1; 1655 return -1; 1656 } 1657 1658 /* Translate image type name to id */ 1659 *type = genimg_get_type_id (data); 1660 return 0; 1661 } 1662 1663 /** 1664 * fit_image_get_comp - get comp id for a given component image node 1665 * @fit: pointer to the FIT format image header 1666 * @noffset: component image node offset 1667 * @comp: pointer to the uint8_t, will hold comp numeric id 1668 * 1669 * fit_image_get_comp() finds comp property in a given component image node. 1670 * If the property is found, its (string) value is translated to the numeric 1671 * id which is returned to the caller. 1672 * 1673 * returns: 1674 * 0, on success 1675 * -1, on failure 1676 */ 1677 int fit_image_get_comp (const void *fit, int noffset, uint8_t *comp) 1678 { 1679 int len; 1680 const void *data; 1681 1682 /* Get compression name from property data */ 1683 data = fdt_getprop (fit, noffset, FIT_COMP_PROP, &len); 1684 if (data == NULL) { 1685 fit_get_debug (fit, noffset, FIT_COMP_PROP, len); 1686 *comp = -1; 1687 return -1; 1688 } 1689 1690 /* Translate compression name to id */ 1691 *comp = genimg_get_comp_id (data); 1692 return 0; 1693 } 1694 1695 /** 1696 * fit_image_get_load - get load address property for a given component image node 1697 * @fit: pointer to the FIT format image header 1698 * @noffset: component image node offset 1699 * @load: pointer to the uint32_t, will hold load address 1700 * 1701 * fit_image_get_load() finds load address property in a given component image node. 1702 * If the property is found, its value is returned to the caller. 1703 * 1704 * returns: 1705 * 0, on success 1706 * -1, on failure 1707 */ 1708 int fit_image_get_load (const void *fit, int noffset, ulong *load) 1709 { 1710 int len; 1711 const uint32_t *data; 1712 1713 data = fdt_getprop (fit, noffset, FIT_LOAD_PROP, &len); 1714 if (data == NULL) { 1715 fit_get_debug (fit, noffset, FIT_LOAD_PROP, len); 1716 return -1; 1717 } 1718 1719 *load = uimage_to_cpu (*data); 1720 return 0; 1721 } 1722 1723 /** 1724 * fit_image_get_entry - get entry point address property for a given component image node 1725 * @fit: pointer to the FIT format image header 1726 * @noffset: component image node offset 1727 * @entry: pointer to the uint32_t, will hold entry point address 1728 * 1729 * fit_image_get_entry() finds entry point address property in a given component image node. 1730 * If the property is found, its value is returned to the caller. 1731 * 1732 * returns: 1733 * 0, on success 1734 * -1, on failure 1735 */ 1736 int fit_image_get_entry (const void *fit, int noffset, ulong *entry) 1737 { 1738 int len; 1739 const uint32_t *data; 1740 1741 data = fdt_getprop (fit, noffset, FIT_ENTRY_PROP, &len); 1742 if (data == NULL) { 1743 fit_get_debug (fit, noffset, FIT_ENTRY_PROP, len); 1744 return -1; 1745 } 1746 1747 *entry = uimage_to_cpu (*data); 1748 return 0; 1749 } 1750 1751 /** 1752 * fit_image_get_data - get data property and its size for a given component image node 1753 * @fit: pointer to the FIT format image header 1754 * @noffset: component image node offset 1755 * @data: double pointer to void, will hold data property's data address 1756 * @size: pointer to size_t, will hold data property's data size 1757 * 1758 * fit_image_get_data() finds data property in a given component image node. 1759 * If the property is found its data start address and size are returned to 1760 * the caller. 1761 * 1762 * returns: 1763 * 0, on success 1764 * -1, on failure 1765 */ 1766 int fit_image_get_data (const void *fit, int noffset, 1767 const void **data, size_t *size) 1768 { 1769 int len; 1770 1771 *data = fdt_getprop (fit, noffset, FIT_DATA_PROP, &len); 1772 if (*data == NULL) { 1773 fit_get_debug (fit, noffset, FIT_DATA_PROP, len); 1774 *size = 0; 1775 return -1; 1776 } 1777 1778 *size = len; 1779 return 0; 1780 } 1781 1782 /** 1783 * fit_image_hash_get_algo - get hash algorithm name 1784 * @fit: pointer to the FIT format image header 1785 * @noffset: hash node offset 1786 * @algo: double pointer to char, will hold pointer to the algorithm name 1787 * 1788 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node. 1789 * If the property is found its data start address is returned to the caller. 1790 * 1791 * returns: 1792 * 0, on success 1793 * -1, on failure 1794 */ 1795 int fit_image_hash_get_algo (const void *fit, int noffset, char **algo) 1796 { 1797 int len; 1798 1799 *algo = (char *)fdt_getprop (fit, noffset, FIT_ALGO_PROP, &len); 1800 if (*algo == NULL) { 1801 fit_get_debug (fit, noffset, FIT_ALGO_PROP, len); 1802 return -1; 1803 } 1804 1805 return 0; 1806 } 1807 1808 /** 1809 * fit_image_hash_get_value - get hash value and length 1810 * @fit: pointer to the FIT format image header 1811 * @noffset: hash node offset 1812 * @value: double pointer to uint8_t, will hold address of a hash value data 1813 * @value_len: pointer to an int, will hold hash data length 1814 * 1815 * fit_image_hash_get_value() finds hash value property in a given hash node. 1816 * If the property is found its data start address and size are returned to 1817 * the caller. 1818 * 1819 * returns: 1820 * 0, on success 1821 * -1, on failure 1822 */ 1823 int fit_image_hash_get_value (const void *fit, int noffset, uint8_t **value, 1824 int *value_len) 1825 { 1826 int len; 1827 1828 *value = (uint8_t *)fdt_getprop (fit, noffset, FIT_VALUE_PROP, &len); 1829 if (*value == NULL) { 1830 fit_get_debug (fit, noffset, FIT_VALUE_PROP, len); 1831 *value_len = 0; 1832 return -1; 1833 } 1834 1835 *value_len = len; 1836 return 0; 1837 } 1838 1839 /** 1840 * fit_set_timestamp - set node timestamp property 1841 * @fit: pointer to the FIT format image header 1842 * @noffset: node offset 1843 * @timestamp: timestamp value to be set 1844 * 1845 * fit_set_timestamp() attempts to set timestamp property in the requested 1846 * node and returns operation status to the caller. 1847 * 1848 * returns: 1849 * 0, on success 1850 * -1, on property read failure 1851 */ 1852 int fit_set_timestamp (void *fit, int noffset, time_t timestamp) 1853 { 1854 uint32_t t; 1855 int ret; 1856 1857 t = cpu_to_uimage (timestamp); 1858 ret = fdt_setprop (fit, noffset, FIT_TIMESTAMP_PROP, &t, 1859 sizeof (uint32_t)); 1860 if (ret) { 1861 printf ("Can't set '%s' property for '%s' node (%s)\n", 1862 FIT_TIMESTAMP_PROP, fit_get_name (fit, noffset, NULL), 1863 fdt_strerror (ret)); 1864 return -1; 1865 } 1866 1867 return 0; 1868 } 1869 1870 /** 1871 * calculate_hash - calculate and return hash for provided input data 1872 * @data: pointer to the input data 1873 * @data_len: data length 1874 * @algo: requested hash algorithm 1875 * @value: pointer to the char, will hold hash value data (caller must 1876 * allocate enough free space) 1877 * value_len: length of the calculated hash 1878 * 1879 * calculate_hash() computes input data hash according to the requested algorithm. 1880 * Resulting hash value is placed in caller provided 'value' buffer, length 1881 * of the calculated hash is returned via value_len pointer argument. 1882 * 1883 * returns: 1884 * 0, on success 1885 * -1, when algo is unsupported 1886 */ 1887 static int calculate_hash (const void *data, int data_len, const char *algo, 1888 uint8_t *value, int *value_len) 1889 { 1890 if (strcmp (algo, "crc32") == 0 ) { 1891 *((uint32_t *)value) = crc32_wd (0, data, data_len, 1892 CHUNKSZ_CRC32); 1893 *((uint32_t *)value) = cpu_to_uimage (*((uint32_t *)value)); 1894 *value_len = 4; 1895 } else if (strcmp (algo, "sha1") == 0 ) { 1896 sha1_csum_wd ((unsigned char *) data, data_len, 1897 (unsigned char *) value, CHUNKSZ_SHA1); 1898 *value_len = 20; 1899 } else if (strcmp (algo, "md5") == 0 ) { 1900 md5_wd ((unsigned char *)data, data_len, value, CHUNKSZ_MD5); 1901 *value_len = 16; 1902 } else { 1903 debug ("Unsupported hash alogrithm\n"); 1904 return -1; 1905 } 1906 return 0; 1907 } 1908 1909 #ifdef USE_HOSTCC 1910 /** 1911 * fit_set_hashes - process FIT component image nodes and calculate hashes 1912 * @fit: pointer to the FIT format image header 1913 * 1914 * fit_set_hashes() adds hash values for all component images in the FIT blob. 1915 * Hashes are calculated for all component images which have hash subnodes 1916 * with algorithm property set to one of the supported hash algorithms. 1917 * 1918 * returns 1919 * 0, on success 1920 * libfdt error code, on failure 1921 */ 1922 int fit_set_hashes (void *fit) 1923 { 1924 int images_noffset; 1925 int noffset; 1926 int ndepth; 1927 int ret; 1928 1929 /* Find images parent node offset */ 1930 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH); 1931 if (images_noffset < 0) { 1932 printf ("Can't find images parent node '%s' (%s)\n", 1933 FIT_IMAGES_PATH, fdt_strerror (images_noffset)); 1934 return images_noffset; 1935 } 1936 1937 /* Process its subnodes, print out component images details */ 1938 for (ndepth = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth); 1939 (noffset >= 0) && (ndepth > 0); 1940 noffset = fdt_next_node (fit, noffset, &ndepth)) { 1941 if (ndepth == 1) { 1942 /* 1943 * Direct child node of the images parent node, 1944 * i.e. component image node. 1945 */ 1946 ret = fit_image_set_hashes (fit, noffset); 1947 if (ret) 1948 return ret; 1949 } 1950 } 1951 1952 return 0; 1953 } 1954 1955 /** 1956 * fit_image_set_hashes - calculate/set hashes for given component image node 1957 * @fit: pointer to the FIT format image header 1958 * @image_noffset: requested component image node 1959 * 1960 * fit_image_set_hashes() adds hash values for an component image node. All 1961 * existing hash subnodes are checked, if algorithm property is set to one of 1962 * the supported hash algorithms, hash value is computed and corresponding 1963 * hash node property is set, for example: 1964 * 1965 * Input component image node structure: 1966 * 1967 * o image@1 (at image_noffset) 1968 * | - data = [binary data] 1969 * o hash@1 1970 * |- algo = "sha1" 1971 * 1972 * Output component image node structure: 1973 * 1974 * o image@1 (at image_noffset) 1975 * | - data = [binary data] 1976 * o hash@1 1977 * |- algo = "sha1" 1978 * |- value = sha1(data) 1979 * 1980 * returns: 1981 * 0 on sucess 1982 * <0 on failure 1983 */ 1984 int fit_image_set_hashes (void *fit, int image_noffset) 1985 { 1986 const void *data; 1987 size_t size; 1988 char *algo; 1989 uint8_t value[FIT_MAX_HASH_LEN]; 1990 int value_len; 1991 int noffset; 1992 int ndepth; 1993 1994 /* Get image data and data length */ 1995 if (fit_image_get_data (fit, image_noffset, &data, &size)) { 1996 printf ("Can't get image data/size\n"); 1997 return -1; 1998 } 1999 2000 /* Process all hash subnodes of the component image node */ 2001 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth); 2002 (noffset >= 0) && (ndepth > 0); 2003 noffset = fdt_next_node (fit, noffset, &ndepth)) { 2004 if (ndepth == 1) { 2005 /* Direct child node of the component image node */ 2006 2007 /* 2008 * Check subnode name, must be equal to "hash". 2009 * Multiple hash nodes require unique unit node 2010 * names, e.g. hash@1, hash@2, etc. 2011 */ 2012 if (strncmp (fit_get_name(fit, noffset, NULL), 2013 FIT_HASH_NODENAME, 2014 strlen(FIT_HASH_NODENAME)) != 0) { 2015 /* Not a hash subnode, skip it */ 2016 continue; 2017 } 2018 2019 if (fit_image_hash_get_algo (fit, noffset, &algo)) { 2020 printf ("Can't get hash algo property for " 2021 "'%s' hash node in '%s' image node\n", 2022 fit_get_name (fit, noffset, NULL), 2023 fit_get_name (fit, image_noffset, NULL)); 2024 return -1; 2025 } 2026 2027 if (calculate_hash (data, size, algo, value, &value_len)) { 2028 printf ("Unsupported hash algorithm (%s) for " 2029 "'%s' hash node in '%s' image node\n", 2030 algo, fit_get_name (fit, noffset, NULL), 2031 fit_get_name (fit, image_noffset, NULL)); 2032 return -1; 2033 } 2034 2035 if (fit_image_hash_set_value (fit, noffset, value, 2036 value_len)) { 2037 printf ("Can't set hash value for " 2038 "'%s' hash node in '%s' image node\n", 2039 fit_get_name (fit, noffset, NULL), 2040 fit_get_name (fit, image_noffset, NULL)); 2041 return -1; 2042 } 2043 } 2044 } 2045 2046 return 0; 2047 } 2048 2049 /** 2050 * fit_image_hash_set_value - set hash value in requested has node 2051 * @fit: pointer to the FIT format image header 2052 * @noffset: hash node offset 2053 * @value: hash value to be set 2054 * @value_len: hash value length 2055 * 2056 * fit_image_hash_set_value() attempts to set hash value in a node at offset 2057 * given and returns operation status to the caller. 2058 * 2059 * returns 2060 * 0, on success 2061 * -1, on failure 2062 */ 2063 int fit_image_hash_set_value (void *fit, int noffset, uint8_t *value, 2064 int value_len) 2065 { 2066 int ret; 2067 2068 ret = fdt_setprop (fit, noffset, FIT_VALUE_PROP, value, value_len); 2069 if (ret) { 2070 printf ("Can't set hash '%s' property for '%s' node (%s)\n", 2071 FIT_VALUE_PROP, fit_get_name (fit, noffset, NULL), 2072 fdt_strerror (ret)); 2073 return -1; 2074 } 2075 2076 return 0; 2077 } 2078 #endif /* USE_HOSTCC */ 2079 2080 /** 2081 * fit_image_check_hashes - verify data intergity 2082 * @fit: pointer to the FIT format image header 2083 * @image_noffset: component image node offset 2084 * 2085 * fit_image_check_hashes() goes over component image hash nodes, 2086 * re-calculates each data hash and compares with the value stored in hash 2087 * node. 2088 * 2089 * returns: 2090 * 1, if all hashes are valid 2091 * 0, otherwise (or on error) 2092 */ 2093 int fit_image_check_hashes (const void *fit, int image_noffset) 2094 { 2095 const void *data; 2096 size_t size; 2097 char *algo; 2098 uint8_t *fit_value; 2099 int fit_value_len; 2100 uint8_t value[FIT_MAX_HASH_LEN]; 2101 int value_len; 2102 int noffset; 2103 int ndepth; 2104 char *err_msg = ""; 2105 2106 /* Get image data and data length */ 2107 if (fit_image_get_data (fit, image_noffset, &data, &size)) { 2108 printf ("Can't get image data/size\n"); 2109 return 0; 2110 } 2111 2112 /* Process all hash subnodes of the component image node */ 2113 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth); 2114 (noffset >= 0) && (ndepth > 0); 2115 noffset = fdt_next_node (fit, noffset, &ndepth)) { 2116 if (ndepth == 1) { 2117 /* Direct child node of the component image node */ 2118 2119 /* 2120 * Check subnode name, must be equal to "hash". 2121 * Multiple hash nodes require unique unit node 2122 * names, e.g. hash@1, hash@2, etc. 2123 */ 2124 if (strncmp (fit_get_name(fit, noffset, NULL), 2125 FIT_HASH_NODENAME, 2126 strlen(FIT_HASH_NODENAME)) != 0) 2127 continue; 2128 2129 if (fit_image_hash_get_algo (fit, noffset, &algo)) { 2130 err_msg = "Can't get hash algo property"; 2131 goto error; 2132 } 2133 printf ("%s", algo); 2134 2135 if (fit_image_hash_get_value (fit, noffset, &fit_value, 2136 &fit_value_len)) { 2137 err_msg = "Can't get hash value property"; 2138 goto error; 2139 } 2140 2141 if (calculate_hash (data, size, algo, value, &value_len)) { 2142 err_msg = "Unsupported hash algorithm"; 2143 goto error; 2144 } 2145 2146 if (value_len != fit_value_len) { 2147 err_msg = "Bad hash value len"; 2148 goto error; 2149 } else if (memcmp (value, fit_value, value_len) != 0) { 2150 err_msg = "Bad hash value"; 2151 goto error; 2152 } 2153 printf ("+ "); 2154 } 2155 } 2156 2157 return 1; 2158 2159 error: 2160 printf ("%s for '%s' hash node in '%s' image node\n", 2161 err_msg, fit_get_name (fit, noffset, NULL), 2162 fit_get_name (fit, image_noffset, NULL)); 2163 return 0; 2164 } 2165 2166 /** 2167 * fit_image_check_os - check whether image node is of a given os type 2168 * @fit: pointer to the FIT format image header 2169 * @noffset: component image node offset 2170 * @os: requested image os 2171 * 2172 * fit_image_check_os() reads image os property and compares its numeric 2173 * id with the requested os. Comparison result is returned to the caller. 2174 * 2175 * returns: 2176 * 1 if image is of given os type 2177 * 0 otherwise (or on error) 2178 */ 2179 int fit_image_check_os (const void *fit, int noffset, uint8_t os) 2180 { 2181 uint8_t image_os; 2182 2183 if (fit_image_get_os (fit, noffset, &image_os)) 2184 return 0; 2185 return (os == image_os); 2186 } 2187 2188 /** 2189 * fit_image_check_arch - check whether image node is of a given arch 2190 * @fit: pointer to the FIT format image header 2191 * @noffset: component image node offset 2192 * @arch: requested imagearch 2193 * 2194 * fit_image_check_arch() reads image arch property and compares its numeric 2195 * id with the requested arch. Comparison result is returned to the caller. 2196 * 2197 * returns: 2198 * 1 if image is of given arch 2199 * 0 otherwise (or on error) 2200 */ 2201 int fit_image_check_arch (const void *fit, int noffset, uint8_t arch) 2202 { 2203 uint8_t image_arch; 2204 2205 if (fit_image_get_arch (fit, noffset, &image_arch)) 2206 return 0; 2207 return (arch == image_arch); 2208 } 2209 2210 /** 2211 * fit_image_check_type - check whether image node is of a given type 2212 * @fit: pointer to the FIT format image header 2213 * @noffset: component image node offset 2214 * @type: requested image type 2215 * 2216 * fit_image_check_type() reads image type property and compares its numeric 2217 * id with the requested type. Comparison result is returned to the caller. 2218 * 2219 * returns: 2220 * 1 if image is of given type 2221 * 0 otherwise (or on error) 2222 */ 2223 int fit_image_check_type (const void *fit, int noffset, uint8_t type) 2224 { 2225 uint8_t image_type; 2226 2227 if (fit_image_get_type (fit, noffset, &image_type)) 2228 return 0; 2229 return (type == image_type); 2230 } 2231 2232 /** 2233 * fit_image_check_comp - check whether image node uses given compression 2234 * @fit: pointer to the FIT format image header 2235 * @noffset: component image node offset 2236 * @comp: requested image compression type 2237 * 2238 * fit_image_check_comp() reads image compression property and compares its 2239 * numeric id with the requested compression type. Comparison result is 2240 * returned to the caller. 2241 * 2242 * returns: 2243 * 1 if image uses requested compression 2244 * 0 otherwise (or on error) 2245 */ 2246 int fit_image_check_comp (const void *fit, int noffset, uint8_t comp) 2247 { 2248 uint8_t image_comp; 2249 2250 if (fit_image_get_comp (fit, noffset, &image_comp)) 2251 return 0; 2252 return (comp == image_comp); 2253 } 2254 2255 /** 2256 * fit_check_format - sanity check FIT image format 2257 * @fit: pointer to the FIT format image header 2258 * 2259 * fit_check_format() runs a basic sanity FIT image verification. 2260 * Routine checks for mandatory properties, nodes, etc. 2261 * 2262 * returns: 2263 * 1, on success 2264 * 0, on failure 2265 */ 2266 int fit_check_format (const void *fit) 2267 { 2268 /* mandatory / node 'description' property */ 2269 if (fdt_getprop (fit, 0, FIT_DESC_PROP, NULL) == NULL) { 2270 debug ("Wrong FIT format: no description\n"); 2271 return 0; 2272 } 2273 2274 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC) 2275 /* mandatory / node 'timestamp' property */ 2276 if (fdt_getprop (fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) { 2277 debug ("Wrong FIT format: no description\n"); 2278 return 0; 2279 } 2280 #endif 2281 2282 /* mandatory subimages parent '/images' node */ 2283 if (fdt_path_offset (fit, FIT_IMAGES_PATH) < 0) { 2284 debug ("Wrong FIT format: no images parent node\n"); 2285 return 0; 2286 } 2287 2288 return 1; 2289 } 2290 2291 /** 2292 * fit_conf_get_node - get node offset for configuration of a given unit name 2293 * @fit: pointer to the FIT format image header 2294 * @conf_uname: configuration node unit name 2295 * 2296 * fit_conf_get_node() finds a configuration (withing the '/configurations' 2297 * parant node) of a provided unit name. If configuration is found its node offset 2298 * is returned to the caller. 2299 * 2300 * When NULL is provided in second argument fit_conf_get_node() will search 2301 * for a default configuration node instead. Default configuration node unit name 2302 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node. 2303 * 2304 * returns: 2305 * configuration node offset when found (>=0) 2306 * negative number on failure (FDT_ERR_* code) 2307 */ 2308 int fit_conf_get_node (const void *fit, const char *conf_uname) 2309 { 2310 int noffset, confs_noffset; 2311 int len; 2312 2313 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH); 2314 if (confs_noffset < 0) { 2315 debug ("Can't find configurations parent node '%s' (%s)\n", 2316 FIT_CONFS_PATH, fdt_strerror (confs_noffset)); 2317 return confs_noffset; 2318 } 2319 2320 if (conf_uname == NULL) { 2321 /* get configuration unit name from the default property */ 2322 debug ("No configuration specified, trying default...\n"); 2323 conf_uname = (char *)fdt_getprop (fit, confs_noffset, FIT_DEFAULT_PROP, &len); 2324 if (conf_uname == NULL) { 2325 fit_get_debug (fit, confs_noffset, FIT_DEFAULT_PROP, len); 2326 return len; 2327 } 2328 debug ("Found default configuration: '%s'\n", conf_uname); 2329 } 2330 2331 noffset = fdt_subnode_offset (fit, confs_noffset, conf_uname); 2332 if (noffset < 0) { 2333 debug ("Can't get node offset for configuration unit name: '%s' (%s)\n", 2334 conf_uname, fdt_strerror (noffset)); 2335 } 2336 2337 return noffset; 2338 } 2339 2340 static int __fit_conf_get_prop_node (const void *fit, int noffset, 2341 const char *prop_name) 2342 { 2343 char *uname; 2344 int len; 2345 2346 /* get kernel image unit name from configuration kernel property */ 2347 uname = (char *)fdt_getprop (fit, noffset, prop_name, &len); 2348 if (uname == NULL) 2349 return len; 2350 2351 return fit_image_get_node (fit, uname); 2352 } 2353 2354 /** 2355 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to 2356 * a given configuration 2357 * @fit: pointer to the FIT format image header 2358 * @noffset: configuration node offset 2359 * 2360 * fit_conf_get_kernel_node() retrives kernel image node unit name from 2361 * configuration FIT_KERNEL_PROP property and translates it to the node 2362 * offset. 2363 * 2364 * returns: 2365 * image node offset when found (>=0) 2366 * negative number on failure (FDT_ERR_* code) 2367 */ 2368 int fit_conf_get_kernel_node (const void *fit, int noffset) 2369 { 2370 return __fit_conf_get_prop_node (fit, noffset, FIT_KERNEL_PROP); 2371 } 2372 2373 /** 2374 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to 2375 * a given configuration 2376 * @fit: pointer to the FIT format image header 2377 * @noffset: configuration node offset 2378 * 2379 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from 2380 * configuration FIT_KERNEL_PROP property and translates it to the node 2381 * offset. 2382 * 2383 * returns: 2384 * image node offset when found (>=0) 2385 * negative number on failure (FDT_ERR_* code) 2386 */ 2387 int fit_conf_get_ramdisk_node (const void *fit, int noffset) 2388 { 2389 return __fit_conf_get_prop_node (fit, noffset, FIT_RAMDISK_PROP); 2390 } 2391 2392 /** 2393 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to 2394 * a given configuration 2395 * @fit: pointer to the FIT format image header 2396 * @noffset: configuration node offset 2397 * 2398 * fit_conf_get_fdt_node() retrives fdt image node unit name from 2399 * configuration FIT_KERNEL_PROP property and translates it to the node 2400 * offset. 2401 * 2402 * returns: 2403 * image node offset when found (>=0) 2404 * negative number on failure (FDT_ERR_* code) 2405 */ 2406 int fit_conf_get_fdt_node (const void *fit, int noffset) 2407 { 2408 return __fit_conf_get_prop_node (fit, noffset, FIT_FDT_PROP); 2409 } 2410 2411 /** 2412 * fit_conf_print - prints out the FIT configuration details 2413 * @fit: pointer to the FIT format image header 2414 * @noffset: offset of the configuration node 2415 * @p: pointer to prefix string 2416 * 2417 * fit_conf_print() lists all mandatory properies for the processed 2418 * configuration node. 2419 * 2420 * returns: 2421 * no returned results 2422 */ 2423 void fit_conf_print (const void *fit, int noffset, const char *p) 2424 { 2425 char *desc; 2426 char *uname; 2427 int ret; 2428 2429 /* Mandatory properties */ 2430 ret = fit_get_desc (fit, noffset, &desc); 2431 printf ("%s Description: ", p); 2432 if (ret) 2433 printf ("unavailable\n"); 2434 else 2435 printf ("%s\n", desc); 2436 2437 uname = (char *)fdt_getprop (fit, noffset, FIT_KERNEL_PROP, NULL); 2438 printf ("%s Kernel: ", p); 2439 if (uname == NULL) 2440 printf ("unavailable\n"); 2441 else 2442 printf ("%s\n", uname); 2443 2444 /* Optional properties */ 2445 uname = (char *)fdt_getprop (fit, noffset, FIT_RAMDISK_PROP, NULL); 2446 if (uname) 2447 printf ("%s Init Ramdisk: %s\n", p, uname); 2448 2449 uname = (char *)fdt_getprop (fit, noffset, FIT_FDT_PROP, NULL); 2450 if (uname) 2451 printf ("%s FDT: %s\n", p, uname); 2452 } 2453 2454 /** 2455 * fit_check_ramdisk - verify FIT format ramdisk subimage 2456 * @fit_hdr: pointer to the FIT ramdisk header 2457 * @rd_noffset: ramdisk subimage node offset within FIT image 2458 * @arch: requested ramdisk image architecture type 2459 * @verify: data CRC verification flag 2460 * 2461 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from 2462 * specified FIT image. 2463 * 2464 * returns: 2465 * 1, on success 2466 * 0, on failure 2467 */ 2468 #ifndef USE_HOSTCC 2469 static int fit_check_ramdisk (const void *fit, int rd_noffset, uint8_t arch, int verify) 2470 { 2471 fit_image_print (fit, rd_noffset, " "); 2472 2473 if (verify) { 2474 puts (" Verifying Hash Integrity ... "); 2475 if (!fit_image_check_hashes (fit, rd_noffset)) { 2476 puts ("Bad Data Hash\n"); 2477 show_boot_progress (-125); 2478 return 0; 2479 } 2480 puts ("OK\n"); 2481 } 2482 2483 show_boot_progress (126); 2484 if (!fit_image_check_os (fit, rd_noffset, IH_OS_LINUX) || 2485 !fit_image_check_arch (fit, rd_noffset, arch) || 2486 !fit_image_check_type (fit, rd_noffset, IH_TYPE_RAMDISK)) { 2487 printf ("No Linux %s Ramdisk Image\n", 2488 genimg_get_arch_name(arch)); 2489 show_boot_progress (-126); 2490 return 0; 2491 } 2492 2493 show_boot_progress (127); 2494 return 1; 2495 } 2496 #endif /* USE_HOSTCC */ 2497 #endif /* CONFIG_FIT */ 2498