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