2 * (C) Copyright 2008 Semihalf
4 * (C) Copyright 2000-2006
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 * See file CREDITS for list of people who contributed to this
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.
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.
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,
30 #ifdef CONFIG_SHOW_BOOT_PROGRESS
31 #include <status_led.h>
34 #ifdef CONFIG_HAS_DATAFLASH
35 #include <dataflash.h>
38 #ifdef CONFIG_LOGBUFFER
44 #include <environment.h>
47 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
49 #include <fdt_support.h>
52 #if defined(CONFIG_FIT)
53 #include <u-boot/md5.h>
56 static int fit_check_ramdisk(const void *fit, int os_noffset,
57 uint8_t arch, int verify);
61 extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
64 DECLARE_GLOBAL_DATA_PTR;
66 static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
70 #include <u-boot/md5.h>
73 #endif /* !USE_HOSTCC*/
75 #include <u-boot/crc.h>
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 { IH_ARCH_NDS32, "nds32", "NDS32", },
97 { IH_ARCH_OPENRISC, "or1k", "OpenRISC 1000",},
101 static const table_entry_t uimage_os[] = {
102 { IH_OS_INVALID, NULL, "Invalid OS", },
103 { IH_OS_LINUX, "linux", "Linux", },
104 #if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
105 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
107 { IH_OS_NETBSD, "netbsd", "NetBSD", },
108 { IH_OS_OSE, "ose", "Enea OSE", },
109 { IH_OS_PLAN9, "plan9", "Plan 9", },
110 { IH_OS_RTEMS, "rtems", "RTEMS", },
111 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
112 #if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
113 { IH_OS_QNX, "qnx", "QNX", },
114 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
116 #if defined(CONFIG_INTEGRITY) || defined(USE_HOSTCC)
117 { IH_OS_INTEGRITY,"integrity", "INTEGRITY", },
120 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
121 { IH_OS_DELL, "dell", "Dell", },
122 { IH_OS_ESIX, "esix", "Esix", },
123 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
124 { IH_OS_IRIX, "irix", "Irix", },
125 { IH_OS_NCR, "ncr", "NCR", },
126 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
127 { IH_OS_PSOS, "psos", "pSOS", },
128 { IH_OS_SCO, "sco", "SCO", },
129 { IH_OS_SOLARIS, "solaris", "Solaris", },
130 { IH_OS_SVR4, "svr4", "SVR4", },
135 static const table_entry_t uimage_type[] = {
136 { IH_TYPE_AISIMAGE, "aisimage", "Davinci AIS image",},
137 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
138 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
139 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
140 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
141 { IH_TYPE_KERNEL_NOLOAD, "kernel_noload", "Kernel Image (no loading done)", },
142 { IH_TYPE_KWBIMAGE, "kwbimage", "Kirkwood Boot Image",},
143 { IH_TYPE_IMXIMAGE, "imximage", "Freescale i.MX Boot Image",},
144 { IH_TYPE_INVALID, NULL, "Invalid Image", },
145 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
146 { IH_TYPE_OMAPIMAGE, "omapimage", "TI OMAP SPL With GP CH",},
147 { IH_TYPE_PBLIMAGE, "pblimage", "Freescale PBL Boot Image",},
148 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
149 { IH_TYPE_SCRIPT, "script", "Script", },
150 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
151 { IH_TYPE_UBLIMAGE, "ublimage", "Davinci UBL image",},
155 static const table_entry_t uimage_comp[] = {
156 { IH_COMP_NONE, "none", "uncompressed", },
157 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
158 { IH_COMP_GZIP, "gzip", "gzip compressed", },
159 { IH_COMP_LZMA, "lzma", "lzma compressed", },
160 { IH_COMP_LZO, "lzo", "lzo compressed", },
164 /*****************************************************************************/
165 /* Legacy format routines */
166 /*****************************************************************************/
167 int image_check_hcrc(const image_header_t *hdr)
170 ulong len = image_get_header_size();
171 image_header_t header;
173 /* Copy header so we can blank CRC field for re-calculation */
174 memmove(&header, (char *)hdr, image_get_header_size());
175 image_set_hcrc(&header, 0);
177 hcrc = crc32(0, (unsigned char *)&header, len);
179 return (hcrc == image_get_hcrc(hdr));
182 int image_check_dcrc(const image_header_t *hdr)
184 ulong data = image_get_data(hdr);
185 ulong len = image_get_data_size(hdr);
186 ulong dcrc = crc32_wd(0, (unsigned char *)data, len, CHUNKSZ_CRC32);
188 return (dcrc == image_get_dcrc(hdr));
192 * image_multi_count - get component (sub-image) count
193 * @hdr: pointer to the header of the multi component image
195 * image_multi_count() returns number of components in a multi
198 * Note: no checking of the image type is done, caller must pass
199 * a valid multi component image.
202 * number of components
204 ulong image_multi_count(const image_header_t *hdr)
209 /* get start of the image payload, which in case of multi
210 * component images that points to a table of component sizes */
211 size = (uint32_t *)image_get_data(hdr);
213 /* count non empty slots */
214 for (i = 0; size[i]; ++i)
221 * image_multi_getimg - get component data address and size
222 * @hdr: pointer to the header of the multi component image
223 * @idx: index of the requested component
224 * @data: pointer to a ulong variable, will hold component data address
225 * @len: pointer to a ulong variable, will hold component size
227 * image_multi_getimg() returns size and data address for the requested
228 * component in a multi component image.
230 * Note: no checking of the image type is done, caller must pass
231 * a valid multi component image.
234 * data address and size of the component, if idx is valid
235 * 0 in data and len, if idx is out of range
237 void image_multi_getimg(const image_header_t *hdr, ulong idx,
238 ulong *data, ulong *len)
242 ulong offset, count, img_data;
244 /* get number of component */
245 count = image_multi_count(hdr);
247 /* get start of the image payload, which in case of multi
248 * component images that points to a table of component sizes */
249 size = (uint32_t *)image_get_data(hdr);
251 /* get address of the proper component data start, which means
252 * skipping sizes table (add 1 for last, null entry) */
253 img_data = image_get_data(hdr) + (count + 1) * sizeof(uint32_t);
256 *len = uimage_to_cpu(size[idx]);
259 /* go over all indices preceding requested component idx */
260 for (i = 0; i < idx; i++) {
261 /* add up i-th component size, rounding up to 4 bytes */
262 offset += (uimage_to_cpu(size[i]) + 3) & ~3 ;
265 /* calculate idx-th component data address */
266 *data = img_data + offset;
273 static void image_print_type(const image_header_t *hdr)
275 const char *os, *arch, *type, *comp;
277 os = genimg_get_os_name(image_get_os(hdr));
278 arch = genimg_get_arch_name(image_get_arch(hdr));
279 type = genimg_get_type_name(image_get_type(hdr));
280 comp = genimg_get_comp_name(image_get_comp(hdr));
282 printf("%s %s %s (%s)\n", arch, os, type, comp);
286 * image_print_contents - prints out the contents of the legacy format image
287 * @ptr: pointer to the legacy format image header
288 * @p: pointer to prefix string
290 * image_print_contents() formats a multi line legacy image contents description.
291 * The routine prints out all header fields followed by the size/offset data
292 * for MULTI/SCRIPT images.
295 * no returned results
297 void image_print_contents(const void *ptr)
299 const image_header_t *hdr = (const image_header_t *)ptr;
308 printf("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name(hdr));
309 if (IMAGE_ENABLE_TIMESTAMP) {
310 printf("%sCreated: ", p);
311 genimg_print_time((time_t)image_get_time(hdr));
313 printf("%sImage Type: ", p);
314 image_print_type(hdr);
315 printf("%sData Size: ", p);
316 genimg_print_size(image_get_data_size(hdr));
317 printf("%sLoad Address: %08x\n", p, image_get_load(hdr));
318 printf("%sEntry Point: %08x\n", p, image_get_ep(hdr));
320 if (image_check_type(hdr, IH_TYPE_MULTI) ||
321 image_check_type(hdr, IH_TYPE_SCRIPT)) {
324 ulong count = image_multi_count(hdr);
326 printf("%sContents:\n", p);
327 for (i = 0; i < count; i++) {
328 image_multi_getimg(hdr, i, &data, &len);
330 printf("%s Image %d: ", p, i);
331 genimg_print_size(len);
333 if (image_check_type(hdr, IH_TYPE_SCRIPT) && i > 0) {
335 * the user may need to know offsets
336 * if planning to do something with
339 printf("%s Offset = 0x%08lx\n", p, data);
348 * image_get_ramdisk - get and verify ramdisk image
349 * @rd_addr: ramdisk image start address
350 * @arch: expected ramdisk architecture
351 * @verify: checksum verification flag
353 * image_get_ramdisk() returns a pointer to the verified ramdisk image
354 * header. Routine receives image start address and expected architecture
355 * flag. Verification done covers data and header integrity and os/type/arch
358 * If dataflash support is enabled routine checks for dataflash addresses
359 * and handles required dataflash reads.
362 * pointer to a ramdisk image header, if image was found and valid
363 * otherwise, return NULL
365 static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
368 const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
370 if (!image_check_magic(rd_hdr)) {
371 puts("Bad Magic Number\n");
372 bootstage_error(BOOTSTAGE_ID_RD_MAGIC);
376 if (!image_check_hcrc(rd_hdr)) {
377 puts("Bad Header Checksum\n");
378 bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
382 bootstage_mark(BOOTSTAGE_ID_RD_MAGIC);
383 image_print_contents(rd_hdr);
386 puts(" Verifying Checksum ... ");
387 if (!image_check_dcrc(rd_hdr)) {
388 puts("Bad Data CRC\n");
389 bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM);
395 bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
397 if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
398 !image_check_arch(rd_hdr, arch) ||
399 !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
400 printf("No Linux %s Ramdisk Image\n",
401 genimg_get_arch_name(arch));
402 bootstage_error(BOOTSTAGE_ID_RAMDISK);
408 #endif /* !USE_HOSTCC */
410 /*****************************************************************************/
411 /* Shared dual-format routines */
412 /*****************************************************************************/
414 ulong load_addr = CONFIG_SYS_LOAD_ADDR; /* Default Load Address */
415 ulong save_addr; /* Default Save Address */
416 ulong save_size; /* Default Save Size (in bytes) */
418 static int on_loadaddr(const char *name, const char *value, enum env_op op,
423 case env_op_overwrite:
424 load_addr = simple_strtoul(value, NULL, 16);
432 U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
434 ulong getenv_bootm_low(void)
436 char *s = getenv("bootm_low");
438 ulong tmp = simple_strtoul(s, NULL, 16);
442 #if defined(CONFIG_SYS_SDRAM_BASE)
443 return CONFIG_SYS_SDRAM_BASE;
444 #elif defined(CONFIG_ARM)
445 return gd->bd->bi_dram[0].start;
451 phys_size_t getenv_bootm_size(void)
454 char *s = getenv("bootm_size");
456 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
459 s = getenv("bootm_low");
461 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
466 #if defined(CONFIG_ARM)
467 return gd->bd->bi_dram[0].size - tmp;
469 return gd->bd->bi_memsize - tmp;
473 phys_size_t getenv_bootm_mapsize(void)
476 char *s = getenv("bootm_mapsize");
478 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
482 #if defined(CONFIG_SYS_BOOTMAPSZ)
483 return CONFIG_SYS_BOOTMAPSZ;
485 return getenv_bootm_size();
489 void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
494 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
496 size_t tail = (len > chunksz) ? chunksz : len;
498 memmove(to, from, tail);
503 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
504 memmove(to, from, len);
505 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
507 #endif /* !USE_HOSTCC */
509 void genimg_print_size(uint32_t size)
512 printf("%d Bytes = ", size);
513 print_size(size, "\n");
515 printf("%d Bytes = %.2f kB = %.2f MB\n",
516 size, (double)size / 1.024e3,
517 (double)size / 1.048576e6);
521 #if IMAGE_ENABLE_TIMESTAMP
522 void genimg_print_time(time_t timestamp)
527 to_tm(timestamp, &tm);
528 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
529 tm.tm_year, tm.tm_mon, tm.tm_mday,
530 tm.tm_hour, tm.tm_min, tm.tm_sec);
532 printf("%s", ctime(×tamp));
538 * get_table_entry_name - translate entry id to long name
539 * @table: pointer to a translation table for entries of a specific type
540 * @msg: message to be returned when translation fails
541 * @id: entry id to be translated
543 * get_table_entry_name() will go over translation table trying to find
544 * entry that matches given id. If matching entry is found, its long
545 * name is returned to the caller.
548 * long entry name if translation succeeds
551 char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
553 for (; table->id >= 0; ++table) {
555 #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
558 return table->lname + gd->reloc_off;
564 const char *genimg_get_os_name(uint8_t os)
566 return (get_table_entry_name(uimage_os, "Unknown OS", os));
569 const char *genimg_get_arch_name(uint8_t arch)
571 return (get_table_entry_name(uimage_arch, "Unknown Architecture",
575 const char *genimg_get_type_name(uint8_t type)
577 return (get_table_entry_name(uimage_type, "Unknown Image", type));
580 const char *genimg_get_comp_name(uint8_t comp)
582 return (get_table_entry_name(uimage_comp, "Unknown Compression",
587 * get_table_entry_id - translate short entry name to id
588 * @table: pointer to a translation table for entries of a specific type
589 * @table_name: to be used in case of error
590 * @name: entry short name to be translated
592 * get_table_entry_id() will go over translation table trying to find
593 * entry that matches given short name. If matching entry is found,
594 * its id returned to the caller.
597 * entry id if translation succeeds
600 int get_table_entry_id(const table_entry_t *table,
601 const char *table_name, const char *name)
603 const table_entry_t *t;
607 for (t = table; t->id >= 0; ++t) {
608 if (t->sname && strcasecmp(t->sname, name) == 0)
612 fprintf(stderr, "\nInvalid %s Type - valid names are", table_name);
613 for (t = table; t->id >= 0; ++t) {
614 if (t->sname == NULL)
616 fprintf(stderr, "%c %s", (first) ? ':' : ',', t->sname);
619 fprintf(stderr, "\n");
621 for (t = table; t->id >= 0; ++t) {
622 #ifdef CONFIG_NEEDS_MANUAL_RELOC
623 if (t->sname && strcmp(t->sname + gd->reloc_off, name) == 0)
625 if (t->sname && strcmp(t->sname, name) == 0)
629 debug("Invalid %s Type: %s\n", table_name, name);
630 #endif /* USE_HOSTCC */
634 int genimg_get_os_id(const char *name)
636 return (get_table_entry_id(uimage_os, "OS", name));
639 int genimg_get_arch_id(const char *name)
641 return (get_table_entry_id(uimage_arch, "CPU", name));
644 int genimg_get_type_id(const char *name)
646 return (get_table_entry_id(uimage_type, "Image", name));
649 int genimg_get_comp_id(const char *name)
651 return (get_table_entry_id(uimage_comp, "Compression", name));
656 * genimg_get_format - get image format type
657 * @img_addr: image start address
659 * genimg_get_format() checks whether provided address points to a valid
660 * legacy or FIT image.
662 * New uImage format and FDT blob are based on a libfdt. FDT blob
663 * may be passed directly or embedded in a FIT image. In both situations
664 * genimg_get_format() must be able to dectect libfdt header.
667 * image format type or IMAGE_FORMAT_INVALID if no image is present
669 int genimg_get_format(void *img_addr)
671 ulong format = IMAGE_FORMAT_INVALID;
672 const image_header_t *hdr;
673 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
677 hdr = (const image_header_t *)img_addr;
678 if (image_check_magic(hdr))
679 format = IMAGE_FORMAT_LEGACY;
680 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
682 fit_hdr = (char *)img_addr;
683 if (fdt_check_header(fit_hdr) == 0)
684 format = IMAGE_FORMAT_FIT;
692 * genimg_get_image - get image from special storage (if necessary)
693 * @img_addr: image start address
695 * genimg_get_image() checks if provided image start adddress is located
696 * in a dataflash storage. If so, image is moved to a system RAM memory.
699 * image start address after possible relocation from special storage
701 ulong genimg_get_image(ulong img_addr)
703 ulong ram_addr = img_addr;
705 #ifdef CONFIG_HAS_DATAFLASH
706 ulong h_size, d_size;
708 if (addr_dataflash(img_addr)) {
709 /* ger RAM address */
710 ram_addr = CONFIG_SYS_LOAD_ADDR;
712 /* get header size */
713 h_size = image_get_header_size();
714 #if defined(CONFIG_FIT)
715 if (sizeof(struct fdt_header) > h_size)
716 h_size = sizeof(struct fdt_header);
720 debug(" Reading image header from dataflash address "
721 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
723 read_dataflash(img_addr, h_size, (char *)ram_addr);
726 switch (genimg_get_format((void *)ram_addr)) {
727 case IMAGE_FORMAT_LEGACY:
728 d_size = image_get_data_size(
729 (const image_header_t *)ram_addr);
730 debug(" Legacy format image found at 0x%08lx, "
734 #if defined(CONFIG_FIT)
735 case IMAGE_FORMAT_FIT:
736 d_size = fit_get_size((const void *)ram_addr) - h_size;
737 debug(" FIT/FDT format image found at 0x%08lx, "
743 printf(" No valid image found at 0x%08lx\n",
748 /* read in image data */
749 debug(" Reading image remaining data from dataflash address "
750 "%08lx to RAM address %08lx\n", img_addr + h_size,
753 read_dataflash(img_addr + h_size, d_size,
754 (char *)(ram_addr + h_size));
757 #endif /* CONFIG_HAS_DATAFLASH */
763 * fit_has_config - check if there is a valid FIT configuration
764 * @images: pointer to the bootm command headers structure
766 * fit_has_config() checks if there is a FIT configuration in use
767 * (if FTI support is present).
770 * 0, no FIT support or no configuration found
771 * 1, configuration found
773 int genimg_has_config(bootm_headers_t *images)
775 #if defined(CONFIG_FIT)
776 if (images->fit_uname_cfg)
783 * boot_get_ramdisk - main ramdisk handling routine
784 * @argc: command argument count
785 * @argv: command argument list
786 * @images: pointer to the bootm images structure
787 * @arch: expected ramdisk architecture
788 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
789 * @rd_end: pointer to a ulong variable, will hold ramdisk end
791 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
792 * Curently supported are the following ramdisk sources:
793 * - multicomponent kernel/ramdisk image,
794 * - commandline provided address of decicated ramdisk image.
797 * 0, if ramdisk image was found and valid, or skiped
798 * rd_start and rd_end are set to ramdisk start/end addresses if
799 * ramdisk image is found and valid
801 * 1, if ramdisk image is found but corrupted, or invalid
802 * rd_start and rd_end are set to 0 if no ramdisk exists
804 int boot_get_ramdisk(int argc, char * const argv[], bootm_headers_t *images,
805 uint8_t arch, ulong *rd_start, ulong *rd_end)
807 ulong rd_addr, rd_load;
808 ulong rd_data, rd_len;
809 const image_header_t *rd_hdr;
810 #ifdef CONFIG_SUPPORT_RAW_INITRD
813 #if defined(CONFIG_FIT)
815 const char *fit_uname_config = NULL;
816 const char *fit_uname_ramdisk = NULL;
828 * Look for a '-' which indicates to ignore the
831 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
832 debug("## Skipping init Ramdisk\n");
833 rd_len = rd_data = 0;
834 } else if (argc >= 3 || genimg_has_config(images)) {
835 #if defined(CONFIG_FIT)
838 * If the init ramdisk comes from the FIT image and
839 * the FIT image address is omitted in the command
840 * line argument, try to use os FIT image address or
841 * default load address.
843 if (images->fit_uname_os)
844 default_addr = (ulong)images->fit_hdr_os;
846 default_addr = load_addr;
848 if (fit_parse_conf(argv[2], default_addr,
849 &rd_addr, &fit_uname_config)) {
850 debug("* ramdisk: config '%s' from image at "
852 fit_uname_config, rd_addr);
853 } else if (fit_parse_subimage(argv[2], default_addr,
854 &rd_addr, &fit_uname_ramdisk)) {
855 debug("* ramdisk: subimage '%s' from image at "
857 fit_uname_ramdisk, rd_addr);
861 rd_addr = simple_strtoul(argv[2], NULL, 16);
862 debug("* ramdisk: cmdline image address = "
866 #if defined(CONFIG_FIT)
868 /* use FIT configuration provided in first bootm
871 rd_addr = (ulong)images->fit_hdr_os;
872 fit_uname_config = images->fit_uname_cfg;
873 debug("* ramdisk: using config '%s' from image "
875 fit_uname_config, rd_addr);
878 * Check whether configuration has ramdisk defined,
879 * if not, don't try to use it, quit silently.
881 fit_hdr = (void *)rd_addr;
882 cfg_noffset = fit_conf_get_node(fit_hdr,
884 if (cfg_noffset < 0) {
885 debug("* ramdisk: no such config\n");
889 rd_noffset = fit_conf_get_ramdisk_node(fit_hdr,
891 if (rd_noffset < 0) {
892 debug("* ramdisk: no ramdisk in config\n");
898 /* copy from dataflash if needed */
899 rd_addr = genimg_get_image(rd_addr);
902 * Check if there is an initrd image at the
903 * address provided in the second bootm argument
904 * check image type, for FIT images get FIT node.
906 switch (genimg_get_format((void *)rd_addr)) {
907 case IMAGE_FORMAT_LEGACY:
908 printf("## Loading init Ramdisk from Legacy "
909 "Image at %08lx ...\n", rd_addr);
911 bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
912 rd_hdr = image_get_ramdisk(rd_addr, arch,
918 rd_data = image_get_data(rd_hdr);
919 rd_len = image_get_data_size(rd_hdr);
920 rd_load = image_get_load(rd_hdr);
922 #if defined(CONFIG_FIT)
923 case IMAGE_FORMAT_FIT:
924 fit_hdr = (void *)rd_addr;
925 printf("## Loading init Ramdisk from FIT "
926 "Image at %08lx ...\n", rd_addr);
928 bootstage_mark(BOOTSTAGE_ID_FIT_RD_FORMAT);
929 if (!fit_check_format(fit_hdr)) {
930 puts("Bad FIT ramdisk image format!\n");
932 BOOTSTAGE_ID_FIT_RD_FORMAT);
935 bootstage_mark(BOOTSTAGE_ID_FIT_RD_FORMAT_OK);
937 if (!fit_uname_ramdisk) {
939 * no ramdisk image node unit name, try to get config
940 * node first. If config unit node name is NULL
941 * fit_conf_get_node() will try to find default config node
944 BOOTSTAGE_ID_FIT_RD_NO_UNIT_NAME);
945 cfg_noffset = fit_conf_get_node(fit_hdr,
947 if (cfg_noffset < 0) {
948 puts("Could not find configuration "
951 BOOTSTAGE_ID_FIT_RD_NO_UNIT_NAME);
954 fit_uname_config = fdt_get_name(fit_hdr,
956 printf(" Using '%s' configuration\n",
959 rd_noffset = fit_conf_get_ramdisk_node(fit_hdr,
961 fit_uname_ramdisk = fit_get_name(fit_hdr,
964 /* get ramdisk component image node offset */
966 BOOTSTAGE_ID_FIT_RD_UNIT_NAME);
967 rd_noffset = fit_image_get_node(fit_hdr,
970 if (rd_noffset < 0) {
971 puts("Could not find subimage node\n");
972 bootstage_error(BOOTSTAGE_ID_FIT_RD_SUBNODE);
976 printf(" Trying '%s' ramdisk subimage\n",
979 bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK);
980 if (!fit_check_ramdisk(fit_hdr, rd_noffset, arch,
984 /* get ramdisk image data address and length */
985 if (fit_image_get_data(fit_hdr, rd_noffset, &data,
987 puts("Could not find ramdisk subimage data!\n");
988 bootstage_error(BOOTSTAGE_ID_FIT_RD_GET_DATA);
991 bootstage_mark(BOOTSTAGE_ID_FIT_RD_GET_DATA_OK);
993 rd_data = (ulong)data;
996 if (fit_image_get_load(fit_hdr, rd_noffset, &rd_load)) {
997 puts("Can't get ramdisk subimage load "
999 bootstage_error(BOOTSTAGE_ID_FIT_RD_LOAD);
1002 bootstage_mark(BOOTSTAGE_ID_FIT_RD_LOAD);
1004 images->fit_hdr_rd = fit_hdr;
1005 images->fit_uname_rd = fit_uname_ramdisk;
1006 images->fit_noffset_rd = rd_noffset;
1010 #ifdef CONFIG_SUPPORT_RAW_INITRD
1011 if (argc >= 3 && (end = strchr(argv[2], ':'))) {
1012 rd_len = simple_strtoul(++end, NULL, 16);
1017 puts("Wrong Ramdisk Image Format\n");
1018 rd_data = rd_len = rd_load = 0;
1022 } else if (images->legacy_hdr_valid &&
1023 image_check_type(&images->legacy_hdr_os_copy,
1027 * Now check if we have a legacy mult-component image,
1028 * get second entry data start address and len.
1030 bootstage_mark(BOOTSTAGE_ID_RAMDISK);
1031 printf("## Loading init Ramdisk from multi component "
1032 "Legacy Image at %08lx ...\n",
1033 (ulong)images->legacy_hdr_os);
1035 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
1040 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
1041 rd_len = rd_data = 0;
1045 debug("## No init Ramdisk\n");
1047 *rd_start = rd_data;
1048 *rd_end = rd_data + rd_len;
1050 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
1051 *rd_start, *rd_end);
1056 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
1058 * boot_ramdisk_high - relocate init ramdisk
1059 * @lmb: pointer to lmb handle, will be used for memory mgmt
1060 * @rd_data: ramdisk data start address
1061 * @rd_len: ramdisk data length
1062 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
1063 * start address (after possible relocation)
1064 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
1065 * end address (after possible relocation)
1067 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
1068 * variable and if requested ramdisk data is moved to a specified location.
1070 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1071 * start/end addresses if ramdisk image start and len were provided,
1072 * otherwise set initrd_start and initrd_end set to zeros.
1078 int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
1079 ulong *initrd_start, ulong *initrd_end)
1083 int initrd_copy_to_ram = 1;
1085 if ((s = getenv("initrd_high")) != NULL) {
1086 /* a value of "no" or a similar string will act like 0,
1087 * turning the "load high" feature off. This is intentional.
1089 initrd_high = simple_strtoul(s, NULL, 16);
1090 if (initrd_high == ~0)
1091 initrd_copy_to_ram = 0;
1093 /* not set, no restrictions to load high */
1098 #ifdef CONFIG_LOGBUFFER
1099 /* Prevent initrd from overwriting logbuffer */
1100 lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
1103 debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1104 initrd_high, initrd_copy_to_ram);
1107 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
1108 debug(" in-place initrd\n");
1109 *initrd_start = rd_data;
1110 *initrd_end = rd_data + rd_len;
1111 lmb_reserve(lmb, rd_data, rd_len);
1114 *initrd_start = (ulong)lmb_alloc_base(lmb,
1115 rd_len, 0x1000, initrd_high);
1117 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
1120 if (*initrd_start == 0) {
1121 puts("ramdisk - allocation error\n");
1124 bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
1126 *initrd_end = *initrd_start + rd_len;
1127 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
1128 *initrd_start, *initrd_end);
1130 memmove_wd((void *)*initrd_start,
1131 (void *)rd_data, rd_len, CHUNKSZ);
1135 * Ensure the image is flushed to memory to handle
1136 * AMP boot scenarios in which we might not be
1139 flush_cache((unsigned long)*initrd_start, rd_len);
1147 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1148 *initrd_start, *initrd_end);
1155 #endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
1157 #ifdef CONFIG_OF_LIBFDT
1158 static void fdt_error(const char *msg)
1162 puts(" - must RESET the board to recover.\n");
1165 static const image_header_t *image_get_fdt(ulong fdt_addr)
1167 const image_header_t *fdt_hdr = (const image_header_t *)fdt_addr;
1169 image_print_contents(fdt_hdr);
1171 puts(" Verifying Checksum ... ");
1172 if (!image_check_hcrc(fdt_hdr)) {
1173 fdt_error("fdt header checksum invalid");
1177 if (!image_check_dcrc(fdt_hdr)) {
1178 fdt_error("fdt checksum invalid");
1183 if (!image_check_type(fdt_hdr, IH_TYPE_FLATDT)) {
1184 fdt_error("uImage is not a fdt");
1187 if (image_get_comp(fdt_hdr) != IH_COMP_NONE) {
1188 fdt_error("uImage is compressed");
1191 if (fdt_check_header((char *)image_get_data(fdt_hdr)) != 0) {
1192 fdt_error("uImage data is not a fdt");
1199 * fit_check_fdt - verify FIT format FDT subimage
1200 * @fit_hdr: pointer to the FIT header
1201 * fdt_noffset: FDT subimage node offset within FIT image
1202 * @verify: data CRC verification flag
1204 * fit_check_fdt() verifies integrity of the FDT subimage and from
1205 * specified FIT image.
1211 #if defined(CONFIG_FIT)
1212 static int fit_check_fdt(const void *fit, int fdt_noffset, int verify)
1214 fit_image_print(fit, fdt_noffset, " ");
1217 puts(" Verifying Hash Integrity ... ");
1218 if (!fit_image_check_hashes(fit, fdt_noffset)) {
1219 fdt_error("Bad Data Hash");
1225 if (!fit_image_check_type(fit, fdt_noffset, IH_TYPE_FLATDT)) {
1226 fdt_error("Not a FDT image");
1230 if (!fit_image_check_comp(fit, fdt_noffset, IH_COMP_NONE)) {
1231 fdt_error("FDT image is compressed");
1237 #endif /* CONFIG_FIT */
1239 #ifndef CONFIG_SYS_FDT_PAD
1240 #define CONFIG_SYS_FDT_PAD 0x3000
1243 #if defined(CONFIG_OF_LIBFDT)
1245 * boot_fdt_add_mem_rsv_regions - Mark the memreserve sections as unusable
1246 * @lmb: pointer to lmb handle, will be used for memory mgmt
1247 * @fdt_blob: pointer to fdt blob base address
1249 * Adds the memreserve regions in the dtb to the lmb block. Adding the
1250 * memreserve regions prevents u-boot from using them to store the initrd
1253 void boot_fdt_add_mem_rsv_regions(struct lmb *lmb, void *fdt_blob)
1255 uint64_t addr, size;
1258 if (fdt_check_header(fdt_blob) != 0)
1261 total = fdt_num_mem_rsv(fdt_blob);
1262 for (i = 0; i < total; i++) {
1263 if (fdt_get_mem_rsv(fdt_blob, i, &addr, &size) != 0)
1265 printf(" reserving fdt memory region: addr=%llx size=%llx\n",
1266 (unsigned long long)addr, (unsigned long long)size);
1267 lmb_reserve(lmb, addr, size);
1272 * boot_relocate_fdt - relocate flat device tree
1273 * @lmb: pointer to lmb handle, will be used for memory mgmt
1274 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1275 * @of_size: pointer to a ulong variable, will hold fdt length
1277 * boot_relocate_fdt() allocates a region of memory within the bootmap and
1278 * relocates the of_flat_tree into that region, even if the fdt is already in
1279 * the bootmap. It also expands the size of the fdt by CONFIG_SYS_FDT_PAD
1282 * of_flat_tree and of_size are set to final (after relocation) values
1288 int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size)
1290 void *fdt_blob = *of_flat_tree;
1291 void *of_start = NULL;
1295 int disable_relocation = 0;
1301 if (fdt_check_header(fdt_blob) != 0) {
1302 fdt_error("image is not a fdt");
1306 /* position on a 4K boundary before the alloc_current */
1307 /* Pad the FDT by a specified amount */
1308 of_len = *of_size + CONFIG_SYS_FDT_PAD;
1310 /* If fdt_high is set use it to select the relocation address */
1311 fdt_high = getenv("fdt_high");
1313 void *desired_addr = (void *)simple_strtoul(fdt_high, NULL, 16);
1315 if (((ulong) desired_addr) == ~0UL) {
1316 /* All ones means use fdt in place */
1317 of_start = fdt_blob;
1318 lmb_reserve(lmb, (ulong)of_start, of_len);
1319 disable_relocation = 1;
1320 } else if (desired_addr) {
1322 (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
1323 (ulong)desired_addr);
1324 if (of_start == NULL) {
1325 puts("Failed using fdt_high value for Device Tree");
1330 (void *)(ulong) lmb_alloc(lmb, of_len, 0x1000);
1334 (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
1335 getenv_bootm_mapsize()
1336 + getenv_bootm_low());
1339 if (of_start == NULL) {
1340 puts("device tree - allocation error\n");
1344 if (disable_relocation) {
1345 /* We assume there is space after the existing fdt to use for padding */
1346 fdt_set_totalsize(of_start, of_len);
1347 printf(" Using Device Tree in place at %p, end %p\n",
1348 of_start, of_start + of_len - 1);
1350 debug("## device tree at %p ... %p (len=%ld [0x%lX])\n",
1351 fdt_blob, fdt_blob + *of_size - 1, of_len, of_len);
1353 printf(" Loading Device Tree to %p, end %p ... ",
1354 of_start, of_start + of_len - 1);
1356 err = fdt_open_into(fdt_blob, of_start, of_len);
1358 fdt_error("fdt move failed");
1364 *of_flat_tree = of_start;
1367 set_working_fdt_addr(*of_flat_tree);
1373 #endif /* CONFIG_OF_LIBFDT */
1376 * boot_get_fdt - main fdt handling routine
1377 * @argc: command argument count
1378 * @argv: command argument list
1379 * @images: pointer to the bootm images structure
1380 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1381 * @of_size: pointer to a ulong variable, will hold fdt length
1383 * boot_get_fdt() is responsible for finding a valid flat device tree image.
1384 * Curently supported are the following ramdisk sources:
1385 * - multicomponent kernel/ramdisk image,
1386 * - commandline provided address of decicated ramdisk image.
1389 * 0, if fdt image was found and valid, or skipped
1390 * of_flat_tree and of_size are set to fdt start address and length if
1391 * fdt image is found and valid
1393 * 1, if fdt image is found but corrupted
1394 * of_flat_tree and of_size are set to 0 if no fdt exists
1396 int boot_get_fdt(int flag, int argc, char * const argv[],
1397 bootm_headers_t *images, char **of_flat_tree, ulong *of_size)
1399 const image_header_t *fdt_hdr;
1401 char *fdt_blob = NULL;
1402 ulong image_start, image_data, image_end;
1403 ulong load_start, load_end;
1404 #if defined(CONFIG_FIT)
1406 const char *fit_uname_config = NULL;
1407 const char *fit_uname_fdt = NULL;
1415 *of_flat_tree = NULL;
1418 if (argc > 3 || genimg_has_config(images)) {
1419 #if defined(CONFIG_FIT)
1422 * If the FDT blob comes from the FIT image and the
1423 * FIT image address is omitted in the command line
1424 * argument, try to use ramdisk or os FIT image
1425 * address or default load address.
1427 if (images->fit_uname_rd)
1428 default_addr = (ulong)images->fit_hdr_rd;
1429 else if (images->fit_uname_os)
1430 default_addr = (ulong)images->fit_hdr_os;
1432 default_addr = load_addr;
1434 if (fit_parse_conf(argv[3], default_addr,
1435 &fdt_addr, &fit_uname_config)) {
1436 debug("* fdt: config '%s' from image at "
1438 fit_uname_config, fdt_addr);
1439 } else if (fit_parse_subimage(argv[3], default_addr,
1440 &fdt_addr, &fit_uname_fdt)) {
1441 debug("* fdt: subimage '%s' from image at "
1443 fit_uname_fdt, fdt_addr);
1447 fdt_addr = simple_strtoul(argv[3], NULL, 16);
1448 debug("* fdt: cmdline image address = "
1452 #if defined(CONFIG_FIT)
1454 /* use FIT configuration provided in first bootm
1457 fdt_addr = (ulong)images->fit_hdr_os;
1458 fit_uname_config = images->fit_uname_cfg;
1459 debug("* fdt: using config '%s' from image "
1461 fit_uname_config, fdt_addr);
1464 * Check whether configuration has FDT blob defined,
1465 * if not quit silently.
1467 fit_hdr = (void *)fdt_addr;
1468 cfg_noffset = fit_conf_get_node(fit_hdr,
1470 if (cfg_noffset < 0) {
1471 debug("* fdt: no such config\n");
1475 fdt_noffset = fit_conf_get_fdt_node(fit_hdr,
1477 if (fdt_noffset < 0) {
1478 debug("* fdt: no fdt in config\n");
1484 debug("## Checking for 'FDT'/'FDT Image' at %08lx\n",
1487 /* copy from dataflash if needed */
1488 fdt_addr = genimg_get_image(fdt_addr);
1491 * Check if there is an FDT image at the
1492 * address provided in the second bootm argument
1493 * check image type, for FIT images get a FIT node.
1495 switch (genimg_get_format((void *)fdt_addr)) {
1496 case IMAGE_FORMAT_LEGACY:
1497 /* verify fdt_addr points to a valid image header */
1498 printf("## Flattened Device Tree from Legacy Image "
1501 fdt_hdr = image_get_fdt(fdt_addr);
1506 * move image data to the load address,
1507 * make sure we don't overwrite initial image
1509 image_start = (ulong)fdt_hdr;
1510 image_data = (ulong)image_get_data(fdt_hdr);
1511 image_end = image_get_image_end(fdt_hdr);
1513 load_start = image_get_load(fdt_hdr);
1514 load_end = load_start + image_get_data_size(fdt_hdr);
1516 if (load_start == image_start ||
1517 load_start == image_data) {
1518 fdt_blob = (char *)image_data;
1522 if ((load_start < image_end) && (load_end > image_start)) {
1523 fdt_error("fdt overwritten");
1527 debug(" Loading FDT from 0x%08lx to 0x%08lx\n",
1528 image_data, load_start);
1530 memmove((void *)load_start,
1532 image_get_data_size(fdt_hdr));
1534 fdt_blob = (char *)load_start;
1536 case IMAGE_FORMAT_FIT:
1538 * This case will catch both: new uImage format
1539 * (libfdt based) and raw FDT blob (also libfdt
1542 #if defined(CONFIG_FIT)
1543 /* check FDT blob vs FIT blob */
1544 if (fit_check_format((const void *)fdt_addr)) {
1548 fit_hdr = (void *)fdt_addr;
1549 printf("## Flattened Device Tree from FIT "
1553 if (!fit_uname_fdt) {
1555 * no FDT blob image node unit name,
1556 * try to get config node first. If
1557 * config unit node name is NULL
1558 * fit_conf_get_node() will try to
1559 * find default config node
1561 cfg_noffset = fit_conf_get_node(fit_hdr,
1564 if (cfg_noffset < 0) {
1565 fdt_error("Could not find "
1571 fit_uname_config = fdt_get_name(fit_hdr,
1573 printf(" Using '%s' configuration\n",
1576 fdt_noffset = fit_conf_get_fdt_node(
1579 fit_uname_fdt = fit_get_name(fit_hdr,
1582 /* get FDT component image node offset */
1583 fdt_noffset = fit_image_get_node(
1587 if (fdt_noffset < 0) {
1588 fdt_error("Could not find subimage "
1593 printf(" Trying '%s' FDT blob subimage\n",
1596 if (!fit_check_fdt(fit_hdr, fdt_noffset,
1600 /* get ramdisk image data address and length */
1601 if (fit_image_get_data(fit_hdr, fdt_noffset,
1603 fdt_error("Could not find FDT "
1608 /* verift that image data is a proper FDT blob */
1609 if (fdt_check_header((char *)data) != 0) {
1610 fdt_error("Subimage data is not a FTD");
1615 * move image data to the load address,
1616 * make sure we don't overwrite initial image
1618 image_start = (ulong)fit_hdr;
1619 image_end = fit_get_end(fit_hdr);
1621 if (fit_image_get_load(fit_hdr, fdt_noffset,
1622 &load_start) == 0) {
1623 load_end = load_start + size;
1625 if ((load_start < image_end) &&
1626 (load_end > image_start)) {
1627 fdt_error("FDT overwritten");
1631 printf(" Loading FDT from 0x%08lx "
1636 memmove((void *)load_start,
1637 (void *)data, size);
1639 fdt_blob = (char *)load_start;
1641 fdt_blob = (char *)data;
1644 images->fit_hdr_fdt = fit_hdr;
1645 images->fit_uname_fdt = fit_uname_fdt;
1646 images->fit_noffset_fdt = fdt_noffset;
1654 fdt_blob = (char *)fdt_addr;
1655 debug("* fdt: raw FDT blob\n");
1656 printf("## Flattened Device Tree blob at "
1657 "%08lx\n", (long)fdt_blob);
1661 puts("ERROR: Did not find a cmdline Flattened Device "
1666 printf(" Booting using the fdt blob at 0x%p\n", fdt_blob);
1668 } else if (images->legacy_hdr_valid &&
1669 image_check_type(&images->legacy_hdr_os_copy,
1672 ulong fdt_data, fdt_len;
1675 * Now check if we have a legacy multi-component image,
1676 * get second entry data start address and len.
1678 printf("## Flattened Device Tree from multi "
1679 "component Image at %08lX\n",
1680 (ulong)images->legacy_hdr_os);
1682 image_multi_getimg(images->legacy_hdr_os, 2, &fdt_data,
1686 fdt_blob = (char *)fdt_data;
1687 printf(" Booting using the fdt at 0x%p\n", fdt_blob);
1689 if (fdt_check_header(fdt_blob) != 0) {
1690 fdt_error("image is not a fdt");
1694 if (fdt_totalsize(fdt_blob) != fdt_len) {
1695 fdt_error("fdt size != image size");
1699 debug("## No Flattened Device Tree\n");
1703 debug("## No Flattened Device Tree\n");
1707 *of_flat_tree = fdt_blob;
1708 *of_size = fdt_totalsize(fdt_blob);
1709 debug(" of_flat_tree at 0x%08lx size 0x%08lx\n",
1710 (ulong)*of_flat_tree, *of_size);
1715 *of_flat_tree = NULL;
1719 #endif /* CONFIG_OF_LIBFDT */
1721 #ifdef CONFIG_SYS_BOOT_GET_CMDLINE
1723 * boot_get_cmdline - allocate and initialize kernel cmdline
1724 * @lmb: pointer to lmb handle, will be used for memory mgmt
1725 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1726 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1728 * boot_get_cmdline() allocates space for kernel command line below
1729 * BOOTMAPSZ + getenv_bootm_low() address. If "bootargs" U-boot environemnt
1730 * variable is present its contents is copied to allocated kernel
1737 int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
1742 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
1743 getenv_bootm_mapsize() + getenv_bootm_low());
1745 if (cmdline == NULL)
1748 if ((s = getenv("bootargs")) == NULL)
1753 *cmd_start = (ulong) & cmdline[0];
1754 *cmd_end = *cmd_start + strlen(cmdline);
1756 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1760 #endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
1762 #ifdef CONFIG_SYS_BOOT_GET_KBD
1764 * boot_get_kbd - allocate and initialize kernel copy of board info
1765 * @lmb: pointer to lmb handle, will be used for memory mgmt
1766 * @kbd: double pointer to board info data
1768 * boot_get_kbd() allocates space for kernel copy of board info data below
1769 * BOOTMAPSZ + getenv_bootm_low() address and kernel board info is initialized
1770 * with the current u-boot board info data.
1776 int boot_get_kbd(struct lmb *lmb, bd_t **kbd)
1778 *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
1779 getenv_bootm_mapsize() + getenv_bootm_low());
1785 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1787 #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1788 do_bdinfo(NULL, 0, 0, NULL);
1793 #endif /* CONFIG_SYS_BOOT_GET_KBD */
1794 #endif /* !USE_HOSTCC */
1796 #if defined(CONFIG_FIT)
1797 /*****************************************************************************/
1798 /* New uImage format routines */
1799 /*****************************************************************************/
1801 static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
1802 ulong *addr, const char **name)
1809 sep = strchr(spec, sepc);
1812 *addr = simple_strtoul(spec, NULL, 16);
1822 * fit_parse_conf - parse FIT configuration spec
1823 * @spec: input string, containing configuration spec
1824 * @add_curr: current image address (to be used as a possible default)
1825 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1827 * @conf_name double pointer to a char, will hold pointer to a configuration
1830 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1831 * where <addr> is a FIT image address that contains configuration
1832 * with a <conf> unit name.
1834 * Address part is optional, and if omitted default add_curr will
1838 * 1 if spec is a valid configuration string,
1839 * addr and conf_name are set accordingly
1842 int fit_parse_conf(const char *spec, ulong addr_curr,
1843 ulong *addr, const char **conf_name)
1845 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
1849 * fit_parse_subimage - parse FIT subimage spec
1850 * @spec: input string, containing subimage spec
1851 * @add_curr: current image address (to be used as a possible default)
1852 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1854 * @image_name: double pointer to a char, will hold pointer to a subimage name
1856 * fit_parse_subimage() expects subimage spec in the for of
1857 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1858 * subimage with a <subimg> unit name.
1860 * Address part is optional, and if omitted default add_curr will
1864 * 1 if spec is a valid subimage string,
1865 * addr and image_name are set accordingly
1868 int fit_parse_subimage(const char *spec, ulong addr_curr,
1869 ulong *addr, const char **image_name)
1871 return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
1873 #endif /* !USE_HOSTCC */
1875 static void fit_get_debug(const void *fit, int noffset,
1876 char *prop_name, int err)
1878 debug("Can't get '%s' property from FIT 0x%08lx, "
1879 "node: offset %d, name %s (%s)\n",
1880 prop_name, (ulong)fit, noffset,
1881 fit_get_name(fit, noffset, NULL),
1886 * fit_print_contents - prints out the contents of the FIT format image
1887 * @fit: pointer to the FIT format image header
1888 * @p: pointer to prefix string
1890 * fit_print_contents() formats a multi line FIT image contents description.
1891 * The routine prints out FIT image properties (root node level) follwed by
1892 * the details of each component image.
1895 * no returned results
1897 void fit_print_contents(const void *fit)
1916 /* Root node properties */
1917 ret = fit_get_desc(fit, 0, &desc);
1918 printf("%sFIT description: ", p);
1920 printf("unavailable\n");
1922 printf("%s\n", desc);
1924 if (IMAGE_ENABLE_TIMESTAMP) {
1925 ret = fit_get_timestamp(fit, 0, ×tamp);
1926 printf("%sCreated: ", p);
1928 printf("unavailable\n");
1930 genimg_print_time(timestamp);
1933 /* Find images parent node offset */
1934 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1935 if (images_noffset < 0) {
1936 printf("Can't find images parent node '%s' (%s)\n",
1937 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1941 /* Process its subnodes, print out component images details */
1942 for (ndepth = 0, count = 0,
1943 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1944 (noffset >= 0) && (ndepth > 0);
1945 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1948 * Direct child node of the images parent node,
1949 * i.e. component image node.
1951 printf("%s Image %u (%s)\n", p, count++,
1952 fit_get_name(fit, noffset, NULL));
1954 fit_image_print(fit, noffset, p);
1958 /* Find configurations parent node offset */
1959 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1960 if (confs_noffset < 0) {
1961 debug("Can't get configurations parent node '%s' (%s)\n",
1962 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1966 /* get default configuration unit name from default property */
1967 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
1969 printf("%s Default Configuration: '%s'\n", p, uname);
1971 /* Process its subnodes, print out configurations details */
1972 for (ndepth = 0, count = 0,
1973 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1974 (noffset >= 0) && (ndepth > 0);
1975 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1978 * Direct child node of the configurations parent node,
1979 * i.e. configuration node.
1981 printf("%s Configuration %u (%s)\n", p, count++,
1982 fit_get_name(fit, noffset, NULL));
1984 fit_conf_print(fit, noffset, p);
1990 * fit_image_print - prints out the FIT component image details
1991 * @fit: pointer to the FIT format image header
1992 * @image_noffset: offset of the component image node
1993 * @p: pointer to prefix string
1995 * fit_image_print() lists all mandatory properies for the processed component
1996 * image. If present, hash nodes are printed out as well. Load
1997 * address for images of type firmware is also printed out. Since the load
1998 * address is not mandatory for firmware images, it will be output as
1999 * "unavailable" when not present.
2002 * no returned results
2004 void fit_image_print(const void *fit, int image_noffset, const char *p)
2007 uint8_t type, arch, os, comp;
2015 /* Mandatory properties */
2016 ret = fit_get_desc(fit, image_noffset, &desc);
2017 printf("%s Description: ", p);
2019 printf("unavailable\n");
2021 printf("%s\n", desc);
2023 fit_image_get_type(fit, image_noffset, &type);
2024 printf("%s Type: %s\n", p, genimg_get_type_name(type));
2026 fit_image_get_comp(fit, image_noffset, &comp);
2027 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
2029 ret = fit_image_get_data(fit, image_noffset, &data, &size);
2032 printf("%s Data Start: ", p);
2034 printf("unavailable\n");
2036 printf("0x%08lx\n", (ulong)data);
2039 printf("%s Data Size: ", p);
2041 printf("unavailable\n");
2043 genimg_print_size(size);
2045 /* Remaining, type dependent properties */
2046 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
2047 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
2048 (type == IH_TYPE_FLATDT)) {
2049 fit_image_get_arch(fit, image_noffset, &arch);
2050 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
2053 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK)) {
2054 fit_image_get_os(fit, image_noffset, &os);
2055 printf("%s OS: %s\n", p, genimg_get_os_name(os));
2058 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
2059 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK)) {
2060 ret = fit_image_get_load(fit, image_noffset, &load);
2061 printf("%s Load Address: ", p);
2063 printf("unavailable\n");
2065 printf("0x%08lx\n", load);
2068 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
2069 (type == IH_TYPE_RAMDISK)) {
2070 fit_image_get_entry(fit, image_noffset, &entry);
2071 printf("%s Entry Point: ", p);
2073 printf("unavailable\n");
2075 printf("0x%08lx\n", entry);
2078 /* Process all hash subnodes of the component image node */
2079 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
2080 (noffset >= 0) && (ndepth > 0);
2081 noffset = fdt_next_node(fit, noffset, &ndepth)) {
2083 /* Direct child node of the component image node */
2084 fit_image_print_hash(fit, noffset, p);
2090 * fit_image_print_hash - prints out the hash node details
2091 * @fit: pointer to the FIT format image header
2092 * @noffset: offset of the hash node
2093 * @p: pointer to prefix string
2095 * fit_image_print_hash() lists properies for the processed hash node
2098 * no returned results
2100 void fit_image_print_hash(const void *fit, int noffset, const char *p)
2108 * Check subnode name, must be equal to "hash".
2109 * Multiple hash nodes require unique unit node
2110 * names, e.g. hash@1, hash@2, etc.
2112 if (strncmp(fit_get_name(fit, noffset, NULL),
2114 strlen(FIT_HASH_NODENAME)) != 0)
2117 debug("%s Hash node: '%s'\n", p,
2118 fit_get_name(fit, noffset, NULL));
2120 printf("%s Hash algo: ", p);
2121 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
2122 printf("invalid/unsupported\n");
2125 printf("%s\n", algo);
2127 ret = fit_image_hash_get_value(fit, noffset, &value,
2129 printf("%s Hash value: ", p);
2131 printf("unavailable\n");
2133 for (i = 0; i < value_len; i++)
2134 printf("%02x", value[i]);
2138 debug("%s Hash len: %d\n", p, value_len);
2142 * fit_get_desc - get node description property
2143 * @fit: pointer to the FIT format image header
2144 * @noffset: node offset
2145 * @desc: double pointer to the char, will hold pointer to the descrption
2147 * fit_get_desc() reads description property from a given node, if
2148 * description is found pointer to it is returened in third call argument.
2154 int fit_get_desc(const void *fit, int noffset, char **desc)
2158 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
2159 if (*desc == NULL) {
2160 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
2168 * fit_get_timestamp - get node timestamp property
2169 * @fit: pointer to the FIT format image header
2170 * @noffset: node offset
2171 * @timestamp: pointer to the time_t, will hold read timestamp
2173 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
2174 * is found and has a correct size its value is retured in third call
2179 * -1, on property read failure
2180 * -2, on wrong timestamp size
2182 int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
2187 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
2189 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
2192 if (len != sizeof(uint32_t)) {
2193 debug("FIT timestamp with incorrect size of (%u)\n", len);
2197 *timestamp = uimage_to_cpu(*((uint32_t *)data));
2202 * fit_image_get_node - get node offset for component image of a given unit name
2203 * @fit: pointer to the FIT format image header
2204 * @image_uname: component image node unit name
2206 * fit_image_get_node() finds a component image (withing the '/images'
2207 * node) of a provided unit name. If image is found its node offset is
2208 * returned to the caller.
2211 * image node offset when found (>=0)
2212 * negative number on failure (FDT_ERR_* code)
2214 int fit_image_get_node(const void *fit, const char *image_uname)
2216 int noffset, images_noffset;
2218 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
2219 if (images_noffset < 0) {
2220 debug("Can't find images parent node '%s' (%s)\n",
2221 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
2222 return images_noffset;
2225 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
2227 debug("Can't get node offset for image unit name: '%s' (%s)\n",
2228 image_uname, fdt_strerror(noffset));
2235 * fit_image_get_os - get os id for a given component image node
2236 * @fit: pointer to the FIT format image header
2237 * @noffset: component image node offset
2238 * @os: pointer to the uint8_t, will hold os numeric id
2240 * fit_image_get_os() finds os property in a given component image node.
2241 * If the property is found, its (string) value is translated to the numeric
2242 * id which is returned to the caller.
2248 int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
2253 /* Get OS name from property data */
2254 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
2256 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
2261 /* Translate OS name to id */
2262 *os = genimg_get_os_id(data);
2267 * fit_image_get_arch - get arch id for a given component image node
2268 * @fit: pointer to the FIT format image header
2269 * @noffset: component image node offset
2270 * @arch: pointer to the uint8_t, will hold arch numeric id
2272 * fit_image_get_arch() finds arch property in a given component image node.
2273 * If the property is found, its (string) value is translated to the numeric
2274 * id which is returned to the caller.
2280 int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
2285 /* Get architecture name from property data */
2286 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
2288 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
2293 /* Translate architecture name to id */
2294 *arch = genimg_get_arch_id(data);
2299 * fit_image_get_type - get type id for a given component image node
2300 * @fit: pointer to the FIT format image header
2301 * @noffset: component image node offset
2302 * @type: pointer to the uint8_t, will hold type numeric id
2304 * fit_image_get_type() finds type property in a given component image node.
2305 * If the property is found, its (string) value is translated to the numeric
2306 * id which is returned to the caller.
2312 int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
2317 /* Get image type name from property data */
2318 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
2320 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
2325 /* Translate image type name to id */
2326 *type = genimg_get_type_id(data);
2331 * fit_image_get_comp - get comp id for a given component image node
2332 * @fit: pointer to the FIT format image header
2333 * @noffset: component image node offset
2334 * @comp: pointer to the uint8_t, will hold comp numeric id
2336 * fit_image_get_comp() finds comp property in a given component image node.
2337 * If the property is found, its (string) value is translated to the numeric
2338 * id which is returned to the caller.
2344 int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
2349 /* Get compression name from property data */
2350 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
2352 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
2357 /* Translate compression name to id */
2358 *comp = genimg_get_comp_id(data);
2363 * fit_image_get_load - get load address property for a given component image node
2364 * @fit: pointer to the FIT format image header
2365 * @noffset: component image node offset
2366 * @load: pointer to the uint32_t, will hold load address
2368 * fit_image_get_load() finds load address property in a given component image node.
2369 * If the property is found, its value is returned to the caller.
2375 int fit_image_get_load(const void *fit, int noffset, ulong *load)
2378 const uint32_t *data;
2380 data = fdt_getprop(fit, noffset, FIT_LOAD_PROP, &len);
2382 fit_get_debug(fit, noffset, FIT_LOAD_PROP, len);
2386 *load = uimage_to_cpu(*data);
2391 * fit_image_get_entry - get entry point address property for a given component image node
2392 * @fit: pointer to the FIT format image header
2393 * @noffset: component image node offset
2394 * @entry: pointer to the uint32_t, will hold entry point address
2396 * fit_image_get_entry() finds entry point address property in a given component image node.
2397 * If the property is found, its value is returned to the caller.
2403 int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
2406 const uint32_t *data;
2408 data = fdt_getprop(fit, noffset, FIT_ENTRY_PROP, &len);
2410 fit_get_debug(fit, noffset, FIT_ENTRY_PROP, len);
2414 *entry = uimage_to_cpu(*data);
2419 * fit_image_get_data - get data property and its size for a given component image node
2420 * @fit: pointer to the FIT format image header
2421 * @noffset: component image node offset
2422 * @data: double pointer to void, will hold data property's data address
2423 * @size: pointer to size_t, will hold data property's data size
2425 * fit_image_get_data() finds data property in a given component image node.
2426 * If the property is found its data start address and size are returned to
2433 int fit_image_get_data(const void *fit, int noffset,
2434 const void **data, size_t *size)
2438 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
2439 if (*data == NULL) {
2440 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
2450 * fit_image_hash_get_algo - get hash algorithm name
2451 * @fit: pointer to the FIT format image header
2452 * @noffset: hash node offset
2453 * @algo: double pointer to char, will hold pointer to the algorithm name
2455 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
2456 * If the property is found its data start address is returned to the caller.
2462 int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
2466 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
2467 if (*algo == NULL) {
2468 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
2476 * fit_image_hash_get_value - get hash value and length
2477 * @fit: pointer to the FIT format image header
2478 * @noffset: hash node offset
2479 * @value: double pointer to uint8_t, will hold address of a hash value data
2480 * @value_len: pointer to an int, will hold hash data length
2482 * fit_image_hash_get_value() finds hash value property in a given hash node.
2483 * If the property is found its data start address and size are returned to
2490 int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
2495 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
2496 if (*value == NULL) {
2497 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
2508 * fit_image_hash_get_ignore - get hash ignore flag
2509 * @fit: pointer to the FIT format image header
2510 * @noffset: hash node offset
2511 * @ignore: pointer to an int, will hold hash ignore flag
2513 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
2514 * If the property is found and non-zero, the hash algorithm is not verified by
2515 * u-boot automatically.
2518 * 0, on ignore not found
2519 * value, on ignore found
2521 int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
2526 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
2527 if (value == NULL || len != sizeof(int))
2537 * fit_set_timestamp - set node timestamp property
2538 * @fit: pointer to the FIT format image header
2539 * @noffset: node offset
2540 * @timestamp: timestamp value to be set
2542 * fit_set_timestamp() attempts to set timestamp property in the requested
2543 * node and returns operation status to the caller.
2547 * -1, on property read failure
2549 int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
2554 t = cpu_to_uimage(timestamp);
2555 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
2558 printf("Can't set '%s' property for '%s' node (%s)\n",
2559 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
2568 * calculate_hash - calculate and return hash for provided input data
2569 * @data: pointer to the input data
2570 * @data_len: data length
2571 * @algo: requested hash algorithm
2572 * @value: pointer to the char, will hold hash value data (caller must
2573 * allocate enough free space)
2574 * value_len: length of the calculated hash
2576 * calculate_hash() computes input data hash according to the requested algorithm.
2577 * Resulting hash value is placed in caller provided 'value' buffer, length
2578 * of the calculated hash is returned via value_len pointer argument.
2582 * -1, when algo is unsupported
2584 static int calculate_hash(const void *data, int data_len, const char *algo,
2585 uint8_t *value, int *value_len)
2587 if (strcmp(algo, "crc32") == 0) {
2588 *((uint32_t *)value) = crc32_wd(0, data, data_len,
2590 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
2592 } else if (strcmp(algo, "sha1") == 0) {
2593 sha1_csum_wd((unsigned char *) data, data_len,
2594 (unsigned char *) value, CHUNKSZ_SHA1);
2596 } else if (strcmp(algo, "md5") == 0) {
2597 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
2600 debug("Unsupported hash alogrithm\n");
2608 * fit_set_hashes - process FIT component image nodes and calculate hashes
2609 * @fit: pointer to the FIT format image header
2611 * fit_set_hashes() adds hash values for all component images in the FIT blob.
2612 * Hashes are calculated for all component images which have hash subnodes
2613 * with algorithm property set to one of the supported hash algorithms.
2617 * libfdt error code, on failure
2619 int fit_set_hashes(void *fit)
2626 /* Find images parent node offset */
2627 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
2628 if (images_noffset < 0) {
2629 printf("Can't find images parent node '%s' (%s)\n",
2630 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
2631 return images_noffset;
2634 /* Process its subnodes, print out component images details */
2635 for (ndepth = 0, noffset = fdt_next_node(fit, images_noffset, &ndepth);
2636 (noffset >= 0) && (ndepth > 0);
2637 noffset = fdt_next_node(fit, noffset, &ndepth)) {
2640 * Direct child node of the images parent node,
2641 * i.e. component image node.
2643 ret = fit_image_set_hashes(fit, noffset);
2653 * fit_image_set_hashes - calculate/set hashes for given component image node
2654 * @fit: pointer to the FIT format image header
2655 * @image_noffset: requested component image node
2657 * fit_image_set_hashes() adds hash values for an component image node. All
2658 * existing hash subnodes are checked, if algorithm property is set to one of
2659 * the supported hash algorithms, hash value is computed and corresponding
2660 * hash node property is set, for example:
2662 * Input component image node structure:
2664 * o image@1 (at image_noffset)
2665 * | - data = [binary data]
2669 * Output component image node structure:
2671 * o image@1 (at image_noffset)
2672 * | - data = [binary data]
2675 * |- value = sha1(data)
2681 int fit_image_set_hashes(void *fit, int image_noffset)
2686 uint8_t value[FIT_MAX_HASH_LEN];
2691 /* Get image data and data length */
2692 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
2693 printf("Can't get image data/size\n");
2697 /* Process all hash subnodes of the component image node */
2698 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
2699 (noffset >= 0) && (ndepth > 0);
2700 noffset = fdt_next_node(fit, noffset, &ndepth)) {
2702 /* Direct child node of the component image node */
2705 * Check subnode name, must be equal to "hash".
2706 * Multiple hash nodes require unique unit node
2707 * names, e.g. hash@1, hash@2, etc.
2709 if (strncmp(fit_get_name(fit, noffset, NULL),
2711 strlen(FIT_HASH_NODENAME)) != 0) {
2712 /* Not a hash subnode, skip it */
2716 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
2717 printf("Can't get hash algo property for "
2718 "'%s' hash node in '%s' image node\n",
2719 fit_get_name(fit, noffset, NULL),
2720 fit_get_name(fit, image_noffset, NULL));
2724 if (calculate_hash(data, size, algo, value,
2726 printf("Unsupported hash algorithm (%s) for "
2727 "'%s' hash node in '%s' image node\n",
2728 algo, fit_get_name(fit, noffset, NULL),
2729 fit_get_name(fit, image_noffset,
2734 if (fit_image_hash_set_value(fit, noffset, value,
2736 printf("Can't set hash value for "
2737 "'%s' hash node in '%s' image node\n",
2738 fit_get_name(fit, noffset, NULL),
2739 fit_get_name(fit, image_noffset, NULL));
2749 * fit_image_hash_set_value - set hash value in requested has node
2750 * @fit: pointer to the FIT format image header
2751 * @noffset: hash node offset
2752 * @value: hash value to be set
2753 * @value_len: hash value length
2755 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2756 * given and returns operation status to the caller.
2762 int fit_image_hash_set_value(void *fit, int noffset, uint8_t *value,
2767 ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
2769 printf("Can't set hash '%s' property for '%s' node(%s)\n",
2770 FIT_VALUE_PROP, fit_get_name(fit, noffset, NULL),
2777 #endif /* USE_HOSTCC */
2780 * fit_image_check_hashes - verify data intergity
2781 * @fit: pointer to the FIT format image header
2782 * @image_noffset: component image node offset
2784 * fit_image_check_hashes() goes over component image hash nodes,
2785 * re-calculates each data hash and compares with the value stored in hash
2789 * 1, if all hashes are valid
2790 * 0, otherwise (or on error)
2792 int fit_image_check_hashes(const void *fit, int image_noffset)
2802 uint8_t value[FIT_MAX_HASH_LEN];
2808 /* Get image data and data length */
2809 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
2810 printf("Can't get image data/size\n");
2814 /* Process all hash subnodes of the component image node */
2815 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
2816 (noffset >= 0) && (ndepth > 0);
2817 noffset = fdt_next_node(fit, noffset, &ndepth)) {
2819 /* Direct child node of the component image node */
2822 * Check subnode name, must be equal to "hash".
2823 * Multiple hash nodes require unique unit node
2824 * names, e.g. hash@1, hash@2, etc.
2826 if (strncmp(fit_get_name(fit, noffset, NULL),
2828 strlen(FIT_HASH_NODENAME)) != 0)
2831 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
2832 err_msg = " error!\nCan't get hash algo "
2839 fit_image_hash_get_ignore(fit, noffset, &ignore);
2841 printf("-skipped ");
2846 if (fit_image_hash_get_value(fit, noffset, &fit_value,
2848 err_msg = " error!\nCan't get hash value "
2853 if (calculate_hash(data, size, algo, value,
2855 err_msg = " error!\n"
2856 "Unsupported hash algorithm";
2860 if (value_len != fit_value_len) {
2861 err_msg = " error !\nBad hash value len";
2863 } else if (memcmp(value, fit_value, value_len) != 0) {
2864 err_msg = " error!\nBad hash value";
2871 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
2872 err_msg = " error!\nCorrupted or truncated tree";
2879 printf("%s for '%s' hash node in '%s' image node\n",
2880 err_msg, fit_get_name(fit, noffset, NULL),
2881 fit_get_name(fit, image_noffset, NULL));
2886 * fit_all_image_check_hashes - verify data intergity for all images
2887 * @fit: pointer to the FIT format image header
2889 * fit_all_image_check_hashes() goes over all images in the FIT and
2890 * for every images checks if all it's hashes are valid.
2893 * 1, if all hashes of all images are valid
2894 * 0, otherwise (or on error)
2896 int fit_all_image_check_hashes(const void *fit)
2903 /* Find images parent node offset */
2904 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
2905 if (images_noffset < 0) {
2906 printf("Can't find images parent node '%s' (%s)\n",
2907 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
2911 /* Process all image subnodes, check hashes for each */
2912 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
2914 for (ndepth = 0, count = 0,
2915 noffset = fdt_next_node(fit, images_noffset, &ndepth);
2916 (noffset >= 0) && (ndepth > 0);
2917 noffset = fdt_next_node(fit, noffset, &ndepth)) {
2920 * Direct child node of the images parent node,
2921 * i.e. component image node.
2923 printf(" Hash(es) for Image %u (%s): ", count++,
2924 fit_get_name(fit, noffset, NULL));
2926 if (!fit_image_check_hashes(fit, noffset))
2935 * fit_image_check_os - check whether image node is of a given os type
2936 * @fit: pointer to the FIT format image header
2937 * @noffset: component image node offset
2938 * @os: requested image os
2940 * fit_image_check_os() reads image os property and compares its numeric
2941 * id with the requested os. Comparison result is returned to the caller.
2944 * 1 if image is of given os type
2945 * 0 otherwise (or on error)
2947 int fit_image_check_os(const void *fit, int noffset, uint8_t os)
2951 if (fit_image_get_os(fit, noffset, &image_os))
2953 return (os == image_os);
2957 * fit_image_check_arch - check whether image node is of a given arch
2958 * @fit: pointer to the FIT format image header
2959 * @noffset: component image node offset
2960 * @arch: requested imagearch
2962 * fit_image_check_arch() reads image arch property and compares its numeric
2963 * id with the requested arch. Comparison result is returned to the caller.
2966 * 1 if image is of given arch
2967 * 0 otherwise (or on error)
2969 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
2973 if (fit_image_get_arch(fit, noffset, &image_arch))
2975 return (arch == image_arch);
2979 * fit_image_check_type - check whether image node is of a given type
2980 * @fit: pointer to the FIT format image header
2981 * @noffset: component image node offset
2982 * @type: requested image type
2984 * fit_image_check_type() reads image type property and compares its numeric
2985 * id with the requested type. Comparison result is returned to the caller.
2988 * 1 if image is of given type
2989 * 0 otherwise (or on error)
2991 int fit_image_check_type(const void *fit, int noffset, uint8_t type)
2995 if (fit_image_get_type(fit, noffset, &image_type))
2997 return (type == image_type);
3001 * fit_image_check_comp - check whether image node uses given compression
3002 * @fit: pointer to the FIT format image header
3003 * @noffset: component image node offset
3004 * @comp: requested image compression type
3006 * fit_image_check_comp() reads image compression property and compares its
3007 * numeric id with the requested compression type. Comparison result is
3008 * returned to the caller.
3011 * 1 if image uses requested compression
3012 * 0 otherwise (or on error)
3014 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
3018 if (fit_image_get_comp(fit, noffset, &image_comp))
3020 return (comp == image_comp);
3024 * fit_check_format - sanity check FIT image format
3025 * @fit: pointer to the FIT format image header
3027 * fit_check_format() runs a basic sanity FIT image verification.
3028 * Routine checks for mandatory properties, nodes, etc.
3034 int fit_check_format(const void *fit)
3036 /* mandatory / node 'description' property */
3037 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
3038 debug("Wrong FIT format: no description\n");
3042 if (IMAGE_ENABLE_TIMESTAMP) {
3043 /* mandatory / node 'timestamp' property */
3044 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
3045 debug("Wrong FIT format: no timestamp\n");
3050 /* mandatory subimages parent '/images' node */
3051 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
3052 debug("Wrong FIT format: no images parent node\n");
3061 * fit_conf_find_compat
3062 * @fit: pointer to the FIT format image header
3063 * @fdt: pointer to the device tree to compare against
3065 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
3066 * most compatible with the passed in device tree.
3075 * |-o configurations
3083 * |-compatible = "foo,bar", "bim,bam"
3086 * |-compatible = "foo,bar",
3089 * |-compatible = "bim,bam", "baz,biz"
3091 * Configuration 1 would be picked because the first string in U-Boot's
3092 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
3093 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
3096 * offset to the configuration to use if one was found
3099 int fit_conf_find_compat(const void *fit, const void *fdt)
3102 int noffset, confs_noffset, images_noffset;
3103 const void *fdt_compat;
3105 int best_match_offset = 0;
3106 int best_match_pos = 0;
3108 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
3109 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
3110 if (confs_noffset < 0 || images_noffset < 0) {
3111 debug("Can't find configurations or images nodes.\n");
3115 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
3117 debug("Fdt for comparison has no \"compatible\" property.\n");
3122 * Loop over the configurations in the FIT image.
3124 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
3125 (noffset >= 0) && (ndepth > 0);
3126 noffset = fdt_next_node(fit, noffset, &ndepth)) {
3128 const char *kfdt_name;
3130 const char *cur_fdt_compat;
3138 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
3140 debug("No fdt property found.\n");
3143 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
3145 if (kfdt_noffset < 0) {
3146 debug("No image node named \"%s\" found.\n",
3151 * Get a pointer to this configuration's fdt.
3153 if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) {
3154 debug("Failed to get fdt \"%s\".\n", kfdt_name);
3158 len = fdt_compat_len;
3159 cur_fdt_compat = fdt_compat;
3161 * Look for a match for each U-Boot compatibility string in
3162 * turn in this configuration's fdt.
3164 for (i = 0; len > 0 &&
3165 (!best_match_offset || best_match_pos > i); i++) {
3166 int cur_len = strlen(cur_fdt_compat) + 1;
3168 if (!fdt_node_check_compatible(kfdt, 0,
3170 best_match_offset = noffset;
3175 cur_fdt_compat += cur_len;
3178 if (!best_match_offset) {
3179 debug("No match found.\n");
3183 return best_match_offset;
3187 * fit_conf_get_node - get node offset for configuration of a given unit name
3188 * @fit: pointer to the FIT format image header
3189 * @conf_uname: configuration node unit name
3191 * fit_conf_get_node() finds a configuration (withing the '/configurations'
3192 * parant node) of a provided unit name. If configuration is found its node offset
3193 * is returned to the caller.
3195 * When NULL is provided in second argument fit_conf_get_node() will search
3196 * for a default configuration node instead. Default configuration node unit name
3197 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
3200 * configuration node offset when found (>=0)
3201 * negative number on failure (FDT_ERR_* code)
3203 int fit_conf_get_node(const void *fit, const char *conf_uname)
3205 int noffset, confs_noffset;
3208 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
3209 if (confs_noffset < 0) {
3210 debug("Can't find configurations parent node '%s' (%s)\n",
3211 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
3212 return confs_noffset;
3215 if (conf_uname == NULL) {
3216 /* get configuration unit name from the default property */
3217 debug("No configuration specified, trying default...\n");
3218 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
3219 FIT_DEFAULT_PROP, &len);
3220 if (conf_uname == NULL) {
3221 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
3225 debug("Found default configuration: '%s'\n", conf_uname);
3228 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
3230 debug("Can't get node offset for configuration unit name: "
3232 conf_uname, fdt_strerror(noffset));
3238 static int __fit_conf_get_prop_node(const void *fit, int noffset,
3239 const char *prop_name)
3244 /* get kernel image unit name from configuration kernel property */
3245 uname = (char *)fdt_getprop(fit, noffset, prop_name, &len);
3249 return fit_image_get_node(fit, uname);
3253 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
3254 * a given configuration
3255 * @fit: pointer to the FIT format image header
3256 * @noffset: configuration node offset
3258 * fit_conf_get_kernel_node() retrives kernel image node unit name from
3259 * configuration FIT_KERNEL_PROP property and translates it to the node
3263 * image node offset when found (>=0)
3264 * negative number on failure (FDT_ERR_* code)
3266 int fit_conf_get_kernel_node(const void *fit, int noffset)
3268 return __fit_conf_get_prop_node(fit, noffset, FIT_KERNEL_PROP);
3272 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
3273 * a given configuration
3274 * @fit: pointer to the FIT format image header
3275 * @noffset: configuration node offset
3277 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
3278 * configuration FIT_KERNEL_PROP property and translates it to the node
3282 * image node offset when found (>=0)
3283 * negative number on failure (FDT_ERR_* code)
3285 int fit_conf_get_ramdisk_node(const void *fit, int noffset)
3287 return __fit_conf_get_prop_node(fit, noffset, FIT_RAMDISK_PROP);
3291 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
3292 * a given configuration
3293 * @fit: pointer to the FIT format image header
3294 * @noffset: configuration node offset
3296 * fit_conf_get_fdt_node() retrives fdt image node unit name from
3297 * configuration FIT_KERNEL_PROP property and translates it to the node
3301 * image node offset when found (>=0)
3302 * negative number on failure (FDT_ERR_* code)
3304 int fit_conf_get_fdt_node(const void *fit, int noffset)
3306 return __fit_conf_get_prop_node(fit, noffset, FIT_FDT_PROP);
3310 * fit_conf_print - prints out the FIT configuration details
3311 * @fit: pointer to the FIT format image header
3312 * @noffset: offset of the configuration node
3313 * @p: pointer to prefix string
3315 * fit_conf_print() lists all mandatory properies for the processed
3316 * configuration node.
3319 * no returned results
3321 void fit_conf_print(const void *fit, int noffset, const char *p)
3327 /* Mandatory properties */
3328 ret = fit_get_desc(fit, noffset, &desc);
3329 printf("%s Description: ", p);
3331 printf("unavailable\n");
3333 printf("%s\n", desc);
3335 uname = (char *)fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
3336 printf("%s Kernel: ", p);
3338 printf("unavailable\n");
3340 printf("%s\n", uname);
3342 /* Optional properties */
3343 uname = (char *)fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
3345 printf("%s Init Ramdisk: %s\n", p, uname);
3347 uname = (char *)fdt_getprop(fit, noffset, FIT_FDT_PROP, NULL);
3349 printf("%s FDT: %s\n", p, uname);
3353 * fit_check_ramdisk - verify FIT format ramdisk subimage
3354 * @fit_hdr: pointer to the FIT ramdisk header
3355 * @rd_noffset: ramdisk subimage node offset within FIT image
3356 * @arch: requested ramdisk image architecture type
3357 * @verify: data CRC verification flag
3359 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
3360 * specified FIT image.
3367 static int fit_check_ramdisk(const void *fit, int rd_noffset, uint8_t arch,
3370 fit_image_print(fit, rd_noffset, " ");
3373 puts(" Verifying Hash Integrity ... ");
3374 if (!fit_image_check_hashes(fit, rd_noffset)) {
3375 puts("Bad Data Hash\n");
3376 bootstage_error(BOOTSTAGE_ID_FIT_RD_HASH);
3382 bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK_ALL);
3383 if (!fit_image_check_os(fit, rd_noffset, IH_OS_LINUX) ||
3384 !fit_image_check_arch(fit, rd_noffset, arch) ||
3385 !fit_image_check_type(fit, rd_noffset, IH_TYPE_RAMDISK)) {
3386 printf("No Linux %s Ramdisk Image\n",
3387 genimg_get_arch_name(arch));
3388 bootstage_error(BOOTSTAGE_ID_FIT_RD_CHECK_ALL);
3392 bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK_ALL_OK);
3395 #endif /* USE_HOSTCC */
3396 #endif /* CONFIG_FIT */