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