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