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 #include <u-boot/md5.h>
56 extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
59 DECLARE_GLOBAL_DATA_PTR;
61 static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
65 #include <u-boot/md5.h>
68 #endif /* !USE_HOSTCC*/
70 #include <u-boot/crc.h>
72 static const table_entry_t uimage_arch[] = {
73 { IH_ARCH_INVALID, NULL, "Invalid ARCH", },
74 { IH_ARCH_ALPHA, "alpha", "Alpha", },
75 { IH_ARCH_ARM, "arm", "ARM", },
76 { IH_ARCH_I386, "x86", "Intel x86", },
77 { IH_ARCH_IA64, "ia64", "IA64", },
78 { IH_ARCH_M68K, "m68k", "M68K", },
79 { IH_ARCH_MICROBLAZE, "microblaze", "MicroBlaze", },
80 { IH_ARCH_MIPS, "mips", "MIPS", },
81 { IH_ARCH_MIPS64, "mips64", "MIPS 64 Bit", },
82 { IH_ARCH_NIOS2, "nios2", "NIOS II", },
83 { IH_ARCH_PPC, "powerpc", "PowerPC", },
84 { IH_ARCH_PPC, "ppc", "PowerPC", },
85 { IH_ARCH_S390, "s390", "IBM S390", },
86 { IH_ARCH_SH, "sh", "SuperH", },
87 { IH_ARCH_SPARC, "sparc", "SPARC", },
88 { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", },
89 { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", },
90 { IH_ARCH_AVR32, "avr32", "AVR32", },
91 { IH_ARCH_NDS32, "nds32", "NDS32", },
92 { IH_ARCH_OPENRISC, "or1k", "OpenRISC 1000",},
96 static const table_entry_t uimage_os[] = {
97 { IH_OS_INVALID, NULL, "Invalid OS", },
98 { IH_OS_LINUX, "linux", "Linux", },
99 #if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
100 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
102 { IH_OS_NETBSD, "netbsd", "NetBSD", },
103 { IH_OS_OSE, "ose", "Enea OSE", },
104 { IH_OS_PLAN9, "plan9", "Plan 9", },
105 { IH_OS_RTEMS, "rtems", "RTEMS", },
106 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
107 #if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
108 { IH_OS_QNX, "qnx", "QNX", },
109 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
111 #if defined(CONFIG_INTEGRITY) || defined(USE_HOSTCC)
112 { IH_OS_INTEGRITY,"integrity", "INTEGRITY", },
115 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
116 { IH_OS_DELL, "dell", "Dell", },
117 { IH_OS_ESIX, "esix", "Esix", },
118 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
119 { IH_OS_IRIX, "irix", "Irix", },
120 { IH_OS_NCR, "ncr", "NCR", },
121 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
122 { IH_OS_PSOS, "psos", "pSOS", },
123 { IH_OS_SCO, "sco", "SCO", },
124 { IH_OS_SOLARIS, "solaris", "Solaris", },
125 { IH_OS_SVR4, "svr4", "SVR4", },
130 static const table_entry_t uimage_type[] = {
131 { IH_TYPE_AISIMAGE, "aisimage", "Davinci AIS image",},
132 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
133 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
134 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
135 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
136 { IH_TYPE_KERNEL_NOLOAD, "kernel_noload", "Kernel Image (no loading done)", },
137 { IH_TYPE_KWBIMAGE, "kwbimage", "Kirkwood Boot Image",},
138 { IH_TYPE_IMXIMAGE, "imximage", "Freescale i.MX Boot Image",},
139 { IH_TYPE_INVALID, NULL, "Invalid Image", },
140 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
141 { IH_TYPE_OMAPIMAGE, "omapimage", "TI OMAP SPL With GP CH",},
142 { IH_TYPE_PBLIMAGE, "pblimage", "Freescale PBL Boot Image",},
143 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
144 { IH_TYPE_SCRIPT, "script", "Script", },
145 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
146 { IH_TYPE_UBLIMAGE, "ublimage", "Davinci UBL image",},
150 static const table_entry_t uimage_comp[] = {
151 { IH_COMP_NONE, "none", "uncompressed", },
152 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
153 { IH_COMP_GZIP, "gzip", "gzip compressed", },
154 { IH_COMP_LZMA, "lzma", "lzma compressed", },
155 { IH_COMP_LZO, "lzo", "lzo compressed", },
159 /*****************************************************************************/
160 /* Legacy format routines */
161 /*****************************************************************************/
162 int image_check_hcrc(const image_header_t *hdr)
165 ulong len = image_get_header_size();
166 image_header_t header;
168 /* Copy header so we can blank CRC field for re-calculation */
169 memmove(&header, (char *)hdr, image_get_header_size());
170 image_set_hcrc(&header, 0);
172 hcrc = crc32(0, (unsigned char *)&header, len);
174 return (hcrc == image_get_hcrc(hdr));
177 int image_check_dcrc(const image_header_t *hdr)
179 ulong data = image_get_data(hdr);
180 ulong len = image_get_data_size(hdr);
181 ulong dcrc = crc32_wd(0, (unsigned char *)data, len, CHUNKSZ_CRC32);
183 return (dcrc == image_get_dcrc(hdr));
187 * image_multi_count - get component (sub-image) count
188 * @hdr: pointer to the header of the multi component image
190 * image_multi_count() returns number of components in a multi
193 * Note: no checking of the image type is done, caller must pass
194 * a valid multi component image.
197 * number of components
199 ulong image_multi_count(const image_header_t *hdr)
204 /* get start of the image payload, which in case of multi
205 * component images that points to a table of component sizes */
206 size = (uint32_t *)image_get_data(hdr);
208 /* count non empty slots */
209 for (i = 0; size[i]; ++i)
216 * image_multi_getimg - get component data address and size
217 * @hdr: pointer to the header of the multi component image
218 * @idx: index of the requested component
219 * @data: pointer to a ulong variable, will hold component data address
220 * @len: pointer to a ulong variable, will hold component size
222 * image_multi_getimg() returns size and data address for the requested
223 * component in a multi component image.
225 * Note: no checking of the image type is done, caller must pass
226 * a valid multi component image.
229 * data address and size of the component, if idx is valid
230 * 0 in data and len, if idx is out of range
232 void image_multi_getimg(const image_header_t *hdr, ulong idx,
233 ulong *data, ulong *len)
237 ulong offset, count, img_data;
239 /* get number of component */
240 count = image_multi_count(hdr);
242 /* get start of the image payload, which in case of multi
243 * component images that points to a table of component sizes */
244 size = (uint32_t *)image_get_data(hdr);
246 /* get address of the proper component data start, which means
247 * skipping sizes table (add 1 for last, null entry) */
248 img_data = image_get_data(hdr) + (count + 1) * sizeof(uint32_t);
251 *len = uimage_to_cpu(size[idx]);
254 /* go over all indices preceding requested component idx */
255 for (i = 0; i < idx; i++) {
256 /* add up i-th component size, rounding up to 4 bytes */
257 offset += (uimage_to_cpu(size[i]) + 3) & ~3 ;
260 /* calculate idx-th component data address */
261 *data = img_data + offset;
268 static void image_print_type(const image_header_t *hdr)
270 const char *os, *arch, *type, *comp;
272 os = genimg_get_os_name(image_get_os(hdr));
273 arch = genimg_get_arch_name(image_get_arch(hdr));
274 type = genimg_get_type_name(image_get_type(hdr));
275 comp = genimg_get_comp_name(image_get_comp(hdr));
277 printf("%s %s %s (%s)\n", arch, os, type, comp);
281 * image_print_contents - prints out the contents of the legacy format image
282 * @ptr: pointer to the legacy format image header
283 * @p: pointer to prefix string
285 * image_print_contents() formats a multi line legacy image contents description.
286 * The routine prints out all header fields followed by the size/offset data
287 * for MULTI/SCRIPT images.
290 * no returned results
292 void image_print_contents(const void *ptr)
294 const image_header_t *hdr = (const image_header_t *)ptr;
303 printf("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name(hdr));
304 if (IMAGE_ENABLE_TIMESTAMP) {
305 printf("%sCreated: ", p);
306 genimg_print_time((time_t)image_get_time(hdr));
308 printf("%sImage Type: ", p);
309 image_print_type(hdr);
310 printf("%sData Size: ", p);
311 genimg_print_size(image_get_data_size(hdr));
312 printf("%sLoad Address: %08x\n", p, image_get_load(hdr));
313 printf("%sEntry Point: %08x\n", p, image_get_ep(hdr));
315 if (image_check_type(hdr, IH_TYPE_MULTI) ||
316 image_check_type(hdr, IH_TYPE_SCRIPT)) {
319 ulong count = image_multi_count(hdr);
321 printf("%sContents:\n", p);
322 for (i = 0; i < count; i++) {
323 image_multi_getimg(hdr, i, &data, &len);
325 printf("%s Image %d: ", p, i);
326 genimg_print_size(len);
328 if (image_check_type(hdr, IH_TYPE_SCRIPT) && i > 0) {
330 * the user may need to know offsets
331 * if planning to do something with
334 printf("%s Offset = 0x%08lx\n", p, data);
343 * image_get_ramdisk - get and verify ramdisk image
344 * @rd_addr: ramdisk image start address
345 * @arch: expected ramdisk architecture
346 * @verify: checksum verification flag
348 * image_get_ramdisk() returns a pointer to the verified ramdisk image
349 * header. Routine receives image start address and expected architecture
350 * flag. Verification done covers data and header integrity and os/type/arch
353 * If dataflash support is enabled routine checks for dataflash addresses
354 * and handles required dataflash reads.
357 * pointer to a ramdisk image header, if image was found and valid
358 * otherwise, return NULL
360 static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
363 const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
365 if (!image_check_magic(rd_hdr)) {
366 puts("Bad Magic Number\n");
367 bootstage_error(BOOTSTAGE_ID_RD_MAGIC);
371 if (!image_check_hcrc(rd_hdr)) {
372 puts("Bad Header Checksum\n");
373 bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
377 bootstage_mark(BOOTSTAGE_ID_RD_MAGIC);
378 image_print_contents(rd_hdr);
381 puts(" Verifying Checksum ... ");
382 if (!image_check_dcrc(rd_hdr)) {
383 puts("Bad Data CRC\n");
384 bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM);
390 bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
392 if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
393 !image_check_arch(rd_hdr, arch) ||
394 !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
395 printf("No Linux %s Ramdisk Image\n",
396 genimg_get_arch_name(arch));
397 bootstage_error(BOOTSTAGE_ID_RAMDISK);
403 #endif /* !USE_HOSTCC */
405 /*****************************************************************************/
406 /* Shared dual-format routines */
407 /*****************************************************************************/
409 ulong load_addr = CONFIG_SYS_LOAD_ADDR; /* Default Load Address */
410 ulong save_addr; /* Default Save Address */
411 ulong save_size; /* Default Save Size (in bytes) */
413 static int on_loadaddr(const char *name, const char *value, enum env_op op,
418 case env_op_overwrite:
419 load_addr = simple_strtoul(value, NULL, 16);
427 U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
429 ulong getenv_bootm_low(void)
431 char *s = getenv("bootm_low");
433 ulong tmp = simple_strtoul(s, NULL, 16);
437 #if defined(CONFIG_SYS_SDRAM_BASE)
438 return CONFIG_SYS_SDRAM_BASE;
439 #elif defined(CONFIG_ARM)
440 return gd->bd->bi_dram[0].start;
446 phys_size_t getenv_bootm_size(void)
449 char *s = getenv("bootm_size");
451 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
454 s = getenv("bootm_low");
456 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
461 #if defined(CONFIG_ARM)
462 return gd->bd->bi_dram[0].size - tmp;
464 return gd->bd->bi_memsize - tmp;
468 phys_size_t getenv_bootm_mapsize(void)
471 char *s = getenv("bootm_mapsize");
473 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
477 #if defined(CONFIG_SYS_BOOTMAPSZ)
478 return CONFIG_SYS_BOOTMAPSZ;
480 return getenv_bootm_size();
484 void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
489 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
491 size_t tail = (len > chunksz) ? chunksz : len;
493 memmove(to, from, tail);
498 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
499 memmove(to, from, len);
500 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
502 #endif /* !USE_HOSTCC */
504 void genimg_print_size(uint32_t size)
507 printf("%d Bytes = ", size);
508 print_size(size, "\n");
510 printf("%d Bytes = %.2f kB = %.2f MB\n",
511 size, (double)size / 1.024e3,
512 (double)size / 1.048576e6);
516 #if IMAGE_ENABLE_TIMESTAMP
517 void genimg_print_time(time_t timestamp)
522 to_tm(timestamp, &tm);
523 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
524 tm.tm_year, tm.tm_mon, tm.tm_mday,
525 tm.tm_hour, tm.tm_min, tm.tm_sec);
527 printf("%s", ctime(×tamp));
533 * get_table_entry_name - translate entry id to long name
534 * @table: pointer to a translation table for entries of a specific type
535 * @msg: message to be returned when translation fails
536 * @id: entry id to be translated
538 * get_table_entry_name() will go over translation table trying to find
539 * entry that matches given id. If matching entry is found, its long
540 * name is returned to the caller.
543 * long entry name if translation succeeds
546 char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
548 for (; table->id >= 0; ++table) {
550 #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
553 return table->lname + gd->reloc_off;
559 const char *genimg_get_os_name(uint8_t os)
561 return (get_table_entry_name(uimage_os, "Unknown OS", os));
564 const char *genimg_get_arch_name(uint8_t arch)
566 return (get_table_entry_name(uimage_arch, "Unknown Architecture",
570 const char *genimg_get_type_name(uint8_t type)
572 return (get_table_entry_name(uimage_type, "Unknown Image", type));
575 const char *genimg_get_comp_name(uint8_t comp)
577 return (get_table_entry_name(uimage_comp, "Unknown Compression",
582 * get_table_entry_id - translate short entry name to id
583 * @table: pointer to a translation table for entries of a specific type
584 * @table_name: to be used in case of error
585 * @name: entry short name to be translated
587 * get_table_entry_id() will go over translation table trying to find
588 * entry that matches given short name. If matching entry is found,
589 * its id returned to the caller.
592 * entry id if translation succeeds
595 int get_table_entry_id(const table_entry_t *table,
596 const char *table_name, const char *name)
598 const table_entry_t *t;
602 for (t = table; t->id >= 0; ++t) {
603 if (t->sname && strcasecmp(t->sname, name) == 0)
607 fprintf(stderr, "\nInvalid %s Type - valid names are", table_name);
608 for (t = table; t->id >= 0; ++t) {
609 if (t->sname == NULL)
611 fprintf(stderr, "%c %s", (first) ? ':' : ',', t->sname);
614 fprintf(stderr, "\n");
616 for (t = table; t->id >= 0; ++t) {
617 #ifdef CONFIG_NEEDS_MANUAL_RELOC
618 if (t->sname && strcmp(t->sname + gd->reloc_off, name) == 0)
620 if (t->sname && strcmp(t->sname, name) == 0)
624 debug("Invalid %s Type: %s\n", table_name, name);
625 #endif /* USE_HOSTCC */
629 int genimg_get_os_id(const char *name)
631 return (get_table_entry_id(uimage_os, "OS", name));
634 int genimg_get_arch_id(const char *name)
636 return (get_table_entry_id(uimage_arch, "CPU", name));
639 int genimg_get_type_id(const char *name)
641 return (get_table_entry_id(uimage_type, "Image", name));
644 int genimg_get_comp_id(const char *name)
646 return (get_table_entry_id(uimage_comp, "Compression", name));
651 * genimg_get_format - get image format type
652 * @img_addr: image start address
654 * genimg_get_format() checks whether provided address points to a valid
655 * legacy or FIT image.
657 * New uImage format and FDT blob are based on a libfdt. FDT blob
658 * may be passed directly or embedded in a FIT image. In both situations
659 * genimg_get_format() must be able to dectect libfdt header.
662 * image format type or IMAGE_FORMAT_INVALID if no image is present
664 int genimg_get_format(void *img_addr)
666 ulong format = IMAGE_FORMAT_INVALID;
667 const image_header_t *hdr;
668 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
672 hdr = (const image_header_t *)img_addr;
673 if (image_check_magic(hdr))
674 format = IMAGE_FORMAT_LEGACY;
675 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
677 fit_hdr = (char *)img_addr;
678 if (fdt_check_header(fit_hdr) == 0)
679 format = IMAGE_FORMAT_FIT;
687 * genimg_get_image - get image from special storage (if necessary)
688 * @img_addr: image start address
690 * genimg_get_image() checks if provided image start adddress is located
691 * in a dataflash storage. If so, image is moved to a system RAM memory.
694 * image start address after possible relocation from special storage
696 ulong genimg_get_image(ulong img_addr)
698 ulong ram_addr = img_addr;
700 #ifdef CONFIG_HAS_DATAFLASH
701 ulong h_size, d_size;
703 if (addr_dataflash(img_addr)) {
704 /* ger RAM address */
705 ram_addr = CONFIG_SYS_LOAD_ADDR;
707 /* get header size */
708 h_size = image_get_header_size();
709 #if defined(CONFIG_FIT)
710 if (sizeof(struct fdt_header) > h_size)
711 h_size = sizeof(struct fdt_header);
715 debug(" Reading image header from dataflash address "
716 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
718 read_dataflash(img_addr, h_size, (char *)ram_addr);
721 switch (genimg_get_format((void *)ram_addr)) {
722 case IMAGE_FORMAT_LEGACY:
723 d_size = image_get_data_size(
724 (const image_header_t *)ram_addr);
725 debug(" Legacy format image found at 0x%08lx, "
729 #if defined(CONFIG_FIT)
730 case IMAGE_FORMAT_FIT:
731 d_size = fit_get_size((const void *)ram_addr) - h_size;
732 debug(" FIT/FDT format image found at 0x%08lx, "
738 printf(" No valid image found at 0x%08lx\n",
743 /* read in image data */
744 debug(" Reading image remaining data from dataflash address "
745 "%08lx to RAM address %08lx\n", img_addr + h_size,
748 read_dataflash(img_addr + h_size, d_size,
749 (char *)(ram_addr + h_size));
752 #endif /* CONFIG_HAS_DATAFLASH */
758 * fit_has_config - check if there is a valid FIT configuration
759 * @images: pointer to the bootm command headers structure
761 * fit_has_config() checks if there is a FIT configuration in use
762 * (if FTI support is present).
765 * 0, no FIT support or no configuration found
766 * 1, configuration found
768 int genimg_has_config(bootm_headers_t *images)
770 #if defined(CONFIG_FIT)
771 if (images->fit_uname_cfg)
778 * boot_get_ramdisk - main ramdisk handling routine
779 * @argc: command argument count
780 * @argv: command argument list
781 * @images: pointer to the bootm images structure
782 * @arch: expected ramdisk architecture
783 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
784 * @rd_end: pointer to a ulong variable, will hold ramdisk end
786 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
787 * Curently supported are the following ramdisk sources:
788 * - multicomponent kernel/ramdisk image,
789 * - commandline provided address of decicated ramdisk image.
792 * 0, if ramdisk image was found and valid, or skiped
793 * rd_start and rd_end are set to ramdisk start/end addresses if
794 * ramdisk image is found and valid
796 * 1, if ramdisk image is found but corrupted, or invalid
797 * rd_start and rd_end are set to 0 if no ramdisk exists
799 int boot_get_ramdisk(int argc, char * const argv[], bootm_headers_t *images,
800 uint8_t arch, ulong *rd_start, ulong *rd_end)
802 ulong rd_addr, rd_load;
803 ulong rd_data, rd_len;
804 const image_header_t *rd_hdr;
805 #ifdef CONFIG_SUPPORT_RAW_INITRD
808 #if defined(CONFIG_FIT)
810 const char *fit_uname_config = NULL;
811 const char *fit_uname_ramdisk = NULL;
823 * Look for a '-' which indicates to ignore the
826 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
827 debug("## Skipping init Ramdisk\n");
828 rd_len = rd_data = 0;
829 } else if (argc >= 3 || genimg_has_config(images)) {
830 #if defined(CONFIG_FIT)
833 * If the init ramdisk comes from the FIT image and
834 * the FIT image address is omitted in the command
835 * line argument, try to use os FIT image address or
836 * default load address.
838 if (images->fit_uname_os)
839 default_addr = (ulong)images->fit_hdr_os;
841 default_addr = load_addr;
843 if (fit_parse_conf(argv[2], default_addr,
844 &rd_addr, &fit_uname_config)) {
845 debug("* ramdisk: config '%s' from image at "
847 fit_uname_config, rd_addr);
848 } else if (fit_parse_subimage(argv[2], default_addr,
849 &rd_addr, &fit_uname_ramdisk)) {
850 debug("* ramdisk: subimage '%s' from image at "
852 fit_uname_ramdisk, rd_addr);
856 rd_addr = simple_strtoul(argv[2], NULL, 16);
857 debug("* ramdisk: cmdline image address = "
861 #if defined(CONFIG_FIT)
863 /* use FIT configuration provided in first bootm
866 rd_addr = (ulong)images->fit_hdr_os;
867 fit_uname_config = images->fit_uname_cfg;
868 debug("* ramdisk: using config '%s' from image "
870 fit_uname_config, rd_addr);
873 * Check whether configuration has ramdisk defined,
874 * if not, don't try to use it, quit silently.
876 fit_hdr = (void *)rd_addr;
877 cfg_noffset = fit_conf_get_node(fit_hdr,
879 if (cfg_noffset < 0) {
880 debug("* ramdisk: no such config\n");
884 rd_noffset = fit_conf_get_ramdisk_node(fit_hdr,
886 if (rd_noffset < 0) {
887 debug("* ramdisk: no ramdisk in config\n");
893 /* copy from dataflash if needed */
894 rd_addr = genimg_get_image(rd_addr);
897 * Check if there is an initrd image at the
898 * address provided in the second bootm argument
899 * check image type, for FIT images get FIT node.
901 switch (genimg_get_format((void *)rd_addr)) {
902 case IMAGE_FORMAT_LEGACY:
903 printf("## Loading init Ramdisk from Legacy "
904 "Image at %08lx ...\n", rd_addr);
906 bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
907 rd_hdr = image_get_ramdisk(rd_addr, arch,
913 rd_data = image_get_data(rd_hdr);
914 rd_len = image_get_data_size(rd_hdr);
915 rd_load = image_get_load(rd_hdr);
917 #if defined(CONFIG_FIT)
918 case IMAGE_FORMAT_FIT:
919 fit_hdr = (void *)rd_addr;
920 printf("## Loading init Ramdisk from FIT "
921 "Image at %08lx ...\n", rd_addr);
923 bootstage_mark(BOOTSTAGE_ID_FIT_RD_FORMAT);
924 if (!fit_check_format(fit_hdr)) {
925 puts("Bad FIT ramdisk image format!\n");
927 BOOTSTAGE_ID_FIT_RD_FORMAT);
930 bootstage_mark(BOOTSTAGE_ID_FIT_RD_FORMAT_OK);
932 if (!fit_uname_ramdisk) {
934 * no ramdisk image node unit name, try to get config
935 * node first. If config unit node name is NULL
936 * fit_conf_get_node() will try to find default config node
939 BOOTSTAGE_ID_FIT_RD_NO_UNIT_NAME);
940 cfg_noffset = fit_conf_get_node(fit_hdr,
942 if (cfg_noffset < 0) {
943 puts("Could not find configuration "
946 BOOTSTAGE_ID_FIT_RD_NO_UNIT_NAME);
949 fit_uname_config = fdt_get_name(fit_hdr,
951 printf(" Using '%s' configuration\n",
954 rd_noffset = fit_conf_get_ramdisk_node(fit_hdr,
956 fit_uname_ramdisk = fit_get_name(fit_hdr,
959 /* get ramdisk component image node offset */
961 BOOTSTAGE_ID_FIT_RD_UNIT_NAME);
962 rd_noffset = fit_image_get_node(fit_hdr,
965 if (rd_noffset < 0) {
966 puts("Could not find subimage node\n");
967 bootstage_error(BOOTSTAGE_ID_FIT_RD_SUBNODE);
971 printf(" Trying '%s' ramdisk subimage\n",
974 bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK);
975 if (!fit_check_ramdisk(fit_hdr, rd_noffset, arch,
979 /* get ramdisk image data address and length */
980 if (fit_image_get_data(fit_hdr, rd_noffset, &data,
982 puts("Could not find ramdisk subimage data!\n");
983 bootstage_error(BOOTSTAGE_ID_FIT_RD_GET_DATA);
986 bootstage_mark(BOOTSTAGE_ID_FIT_RD_GET_DATA_OK);
988 rd_data = (ulong)data;
991 if (fit_image_get_load(fit_hdr, rd_noffset, &rd_load)) {
992 puts("Can't get ramdisk subimage load "
994 bootstage_error(BOOTSTAGE_ID_FIT_RD_LOAD);
997 bootstage_mark(BOOTSTAGE_ID_FIT_RD_LOAD);
999 images->fit_hdr_rd = fit_hdr;
1000 images->fit_uname_rd = fit_uname_ramdisk;
1001 images->fit_noffset_rd = rd_noffset;
1005 #ifdef CONFIG_SUPPORT_RAW_INITRD
1006 if (argc >= 3 && (end = strchr(argv[2], ':'))) {
1007 rd_len = simple_strtoul(++end, NULL, 16);
1012 puts("Wrong Ramdisk Image Format\n");
1013 rd_data = rd_len = rd_load = 0;
1017 } else if (images->legacy_hdr_valid &&
1018 image_check_type(&images->legacy_hdr_os_copy,
1022 * Now check if we have a legacy mult-component image,
1023 * get second entry data start address and len.
1025 bootstage_mark(BOOTSTAGE_ID_RAMDISK);
1026 printf("## Loading init Ramdisk from multi component "
1027 "Legacy Image at %08lx ...\n",
1028 (ulong)images->legacy_hdr_os);
1030 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
1035 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
1036 rd_len = rd_data = 0;
1040 debug("## No init Ramdisk\n");
1042 *rd_start = rd_data;
1043 *rd_end = rd_data + rd_len;
1045 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
1046 *rd_start, *rd_end);
1051 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
1053 * boot_ramdisk_high - relocate init ramdisk
1054 * @lmb: pointer to lmb handle, will be used for memory mgmt
1055 * @rd_data: ramdisk data start address
1056 * @rd_len: ramdisk data length
1057 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
1058 * start address (after possible relocation)
1059 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
1060 * end address (after possible relocation)
1062 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
1063 * variable and if requested ramdisk data is moved to a specified location.
1065 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1066 * start/end addresses if ramdisk image start and len were provided,
1067 * otherwise set initrd_start and initrd_end set to zeros.
1073 int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
1074 ulong *initrd_start, ulong *initrd_end)
1078 int initrd_copy_to_ram = 1;
1080 if ((s = getenv("initrd_high")) != NULL) {
1081 /* a value of "no" or a similar string will act like 0,
1082 * turning the "load high" feature off. This is intentional.
1084 initrd_high = simple_strtoul(s, NULL, 16);
1085 if (initrd_high == ~0)
1086 initrd_copy_to_ram = 0;
1088 /* not set, no restrictions to load high */
1093 #ifdef CONFIG_LOGBUFFER
1094 /* Prevent initrd from overwriting logbuffer */
1095 lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
1098 debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1099 initrd_high, initrd_copy_to_ram);
1102 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
1103 debug(" in-place initrd\n");
1104 *initrd_start = rd_data;
1105 *initrd_end = rd_data + rd_len;
1106 lmb_reserve(lmb, rd_data, rd_len);
1109 *initrd_start = (ulong)lmb_alloc_base(lmb,
1110 rd_len, 0x1000, initrd_high);
1112 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
1115 if (*initrd_start == 0) {
1116 puts("ramdisk - allocation error\n");
1119 bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
1121 *initrd_end = *initrd_start + rd_len;
1122 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
1123 *initrd_start, *initrd_end);
1125 memmove_wd((void *)*initrd_start,
1126 (void *)rd_data, rd_len, CHUNKSZ);
1130 * Ensure the image is flushed to memory to handle
1131 * AMP boot scenarios in which we might not be
1134 flush_cache((unsigned long)*initrd_start, rd_len);
1142 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1143 *initrd_start, *initrd_end);
1150 #endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
1152 #ifdef CONFIG_OF_LIBFDT
1153 static void fdt_error(const char *msg)
1157 puts(" - must RESET the board to recover.\n");
1160 static const image_header_t *image_get_fdt(ulong fdt_addr)
1162 const image_header_t *fdt_hdr = (const image_header_t *)fdt_addr;
1164 image_print_contents(fdt_hdr);
1166 puts(" Verifying Checksum ... ");
1167 if (!image_check_hcrc(fdt_hdr)) {
1168 fdt_error("fdt header checksum invalid");
1172 if (!image_check_dcrc(fdt_hdr)) {
1173 fdt_error("fdt checksum invalid");
1178 if (!image_check_type(fdt_hdr, IH_TYPE_FLATDT)) {
1179 fdt_error("uImage is not a fdt");
1182 if (image_get_comp(fdt_hdr) != IH_COMP_NONE) {
1183 fdt_error("uImage is compressed");
1186 if (fdt_check_header((char *)image_get_data(fdt_hdr)) != 0) {
1187 fdt_error("uImage data is not a fdt");
1194 * fit_check_fdt - verify FIT format FDT subimage
1195 * @fit_hdr: pointer to the FIT header
1196 * fdt_noffset: FDT subimage node offset within FIT image
1197 * @verify: data CRC verification flag
1199 * fit_check_fdt() verifies integrity of the FDT subimage and from
1200 * specified FIT image.
1206 #if defined(CONFIG_FIT)
1207 static int fit_check_fdt(const void *fit, int fdt_noffset, int verify)
1209 fit_image_print(fit, fdt_noffset, " ");
1212 puts(" Verifying Hash Integrity ... ");
1213 if (!fit_image_check_hashes(fit, fdt_noffset)) {
1214 fdt_error("Bad Data Hash");
1220 if (!fit_image_check_type(fit, fdt_noffset, IH_TYPE_FLATDT)) {
1221 fdt_error("Not a FDT image");
1225 if (!fit_image_check_comp(fit, fdt_noffset, IH_COMP_NONE)) {
1226 fdt_error("FDT image is compressed");
1232 #endif /* CONFIG_FIT */
1234 #ifndef CONFIG_SYS_FDT_PAD
1235 #define CONFIG_SYS_FDT_PAD 0x3000
1238 #if defined(CONFIG_OF_LIBFDT)
1240 * boot_fdt_add_mem_rsv_regions - Mark the memreserve sections as unusable
1241 * @lmb: pointer to lmb handle, will be used for memory mgmt
1242 * @fdt_blob: pointer to fdt blob base address
1244 * Adds the memreserve regions in the dtb to the lmb block. Adding the
1245 * memreserve regions prevents u-boot from using them to store the initrd
1248 void boot_fdt_add_mem_rsv_regions(struct lmb *lmb, void *fdt_blob)
1250 uint64_t addr, size;
1253 if (fdt_check_header(fdt_blob) != 0)
1256 total = fdt_num_mem_rsv(fdt_blob);
1257 for (i = 0; i < total; i++) {
1258 if (fdt_get_mem_rsv(fdt_blob, i, &addr, &size) != 0)
1260 printf(" reserving fdt memory region: addr=%llx size=%llx\n",
1261 (unsigned long long)addr, (unsigned long long)size);
1262 lmb_reserve(lmb, addr, size);
1267 * boot_relocate_fdt - relocate flat device tree
1268 * @lmb: pointer to lmb handle, will be used for memory mgmt
1269 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1270 * @of_size: pointer to a ulong variable, will hold fdt length
1272 * boot_relocate_fdt() allocates a region of memory within the bootmap and
1273 * relocates the of_flat_tree into that region, even if the fdt is already in
1274 * the bootmap. It also expands the size of the fdt by CONFIG_SYS_FDT_PAD
1277 * of_flat_tree and of_size are set to final (after relocation) values
1283 int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size)
1285 void *fdt_blob = *of_flat_tree;
1286 void *of_start = NULL;
1290 int disable_relocation = 0;
1296 if (fdt_check_header(fdt_blob) != 0) {
1297 fdt_error("image is not a fdt");
1301 /* position on a 4K boundary before the alloc_current */
1302 /* Pad the FDT by a specified amount */
1303 of_len = *of_size + CONFIG_SYS_FDT_PAD;
1305 /* If fdt_high is set use it to select the relocation address */
1306 fdt_high = getenv("fdt_high");
1308 void *desired_addr = (void *)simple_strtoul(fdt_high, NULL, 16);
1310 if (((ulong) desired_addr) == ~0UL) {
1311 /* All ones means use fdt in place */
1312 of_start = fdt_blob;
1313 lmb_reserve(lmb, (ulong)of_start, of_len);
1314 disable_relocation = 1;
1315 } else if (desired_addr) {
1317 (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
1318 (ulong)desired_addr);
1319 if (of_start == NULL) {
1320 puts("Failed using fdt_high value for Device Tree");
1325 (void *)(ulong) lmb_alloc(lmb, of_len, 0x1000);
1329 (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
1330 getenv_bootm_mapsize()
1331 + getenv_bootm_low());
1334 if (of_start == NULL) {
1335 puts("device tree - allocation error\n");
1339 if (disable_relocation) {
1340 /* We assume there is space after the existing fdt to use for padding */
1341 fdt_set_totalsize(of_start, of_len);
1342 printf(" Using Device Tree in place at %p, end %p\n",
1343 of_start, of_start + of_len - 1);
1345 debug("## device tree at %p ... %p (len=%ld [0x%lX])\n",
1346 fdt_blob, fdt_blob + *of_size - 1, of_len, of_len);
1348 printf(" Loading Device Tree to %p, end %p ... ",
1349 of_start, of_start + of_len - 1);
1351 err = fdt_open_into(fdt_blob, of_start, of_len);
1353 fdt_error("fdt move failed");
1359 *of_flat_tree = of_start;
1362 set_working_fdt_addr(*of_flat_tree);
1368 #endif /* CONFIG_OF_LIBFDT */
1371 * boot_get_fdt - main fdt handling routine
1372 * @argc: command argument count
1373 * @argv: command argument list
1374 * @images: pointer to the bootm images structure
1375 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1376 * @of_size: pointer to a ulong variable, will hold fdt length
1378 * boot_get_fdt() is responsible for finding a valid flat device tree image.
1379 * Curently supported are the following ramdisk sources:
1380 * - multicomponent kernel/ramdisk image,
1381 * - commandline provided address of decicated ramdisk image.
1384 * 0, if fdt image was found and valid, or skipped
1385 * of_flat_tree and of_size are set to fdt start address and length if
1386 * fdt image is found and valid
1388 * 1, if fdt image is found but corrupted
1389 * of_flat_tree and of_size are set to 0 if no fdt exists
1391 int boot_get_fdt(int flag, int argc, char * const argv[],
1392 bootm_headers_t *images, char **of_flat_tree, ulong *of_size)
1394 const image_header_t *fdt_hdr;
1396 char *fdt_blob = NULL;
1397 ulong image_start, image_data, image_end;
1398 ulong load_start, load_end;
1399 #if defined(CONFIG_FIT)
1401 const char *fit_uname_config = NULL;
1402 const char *fit_uname_fdt = NULL;
1410 *of_flat_tree = NULL;
1413 if (argc > 3 || genimg_has_config(images)) {
1414 #if defined(CONFIG_FIT)
1417 * If the FDT blob comes from the FIT image and the
1418 * FIT image address is omitted in the command line
1419 * argument, try to use ramdisk or os FIT image
1420 * address or default load address.
1422 if (images->fit_uname_rd)
1423 default_addr = (ulong)images->fit_hdr_rd;
1424 else if (images->fit_uname_os)
1425 default_addr = (ulong)images->fit_hdr_os;
1427 default_addr = load_addr;
1429 if (fit_parse_conf(argv[3], default_addr,
1430 &fdt_addr, &fit_uname_config)) {
1431 debug("* fdt: config '%s' from image at "
1433 fit_uname_config, fdt_addr);
1434 } else if (fit_parse_subimage(argv[3], default_addr,
1435 &fdt_addr, &fit_uname_fdt)) {
1436 debug("* fdt: subimage '%s' from image at "
1438 fit_uname_fdt, fdt_addr);
1442 fdt_addr = simple_strtoul(argv[3], NULL, 16);
1443 debug("* fdt: cmdline image address = "
1447 #if defined(CONFIG_FIT)
1449 /* use FIT configuration provided in first bootm
1452 fdt_addr = (ulong)images->fit_hdr_os;
1453 fit_uname_config = images->fit_uname_cfg;
1454 debug("* fdt: using config '%s' from image "
1456 fit_uname_config, fdt_addr);
1459 * Check whether configuration has FDT blob defined,
1460 * if not quit silently.
1462 fit_hdr = (void *)fdt_addr;
1463 cfg_noffset = fit_conf_get_node(fit_hdr,
1465 if (cfg_noffset < 0) {
1466 debug("* fdt: no such config\n");
1470 fdt_noffset = fit_conf_get_fdt_node(fit_hdr,
1472 if (fdt_noffset < 0) {
1473 debug("* fdt: no fdt in config\n");
1479 debug("## Checking for 'FDT'/'FDT Image' at %08lx\n",
1482 /* copy from dataflash if needed */
1483 fdt_addr = genimg_get_image(fdt_addr);
1486 * Check if there is an FDT image at the
1487 * address provided in the second bootm argument
1488 * check image type, for FIT images get a FIT node.
1490 switch (genimg_get_format((void *)fdt_addr)) {
1491 case IMAGE_FORMAT_LEGACY:
1492 /* verify fdt_addr points to a valid image header */
1493 printf("## Flattened Device Tree from Legacy Image "
1496 fdt_hdr = image_get_fdt(fdt_addr);
1501 * move image data to the load address,
1502 * make sure we don't overwrite initial image
1504 image_start = (ulong)fdt_hdr;
1505 image_data = (ulong)image_get_data(fdt_hdr);
1506 image_end = image_get_image_end(fdt_hdr);
1508 load_start = image_get_load(fdt_hdr);
1509 load_end = load_start + image_get_data_size(fdt_hdr);
1511 if (load_start == image_start ||
1512 load_start == image_data) {
1513 fdt_blob = (char *)image_data;
1517 if ((load_start < image_end) && (load_end > image_start)) {
1518 fdt_error("fdt overwritten");
1522 debug(" Loading FDT from 0x%08lx to 0x%08lx\n",
1523 image_data, load_start);
1525 memmove((void *)load_start,
1527 image_get_data_size(fdt_hdr));
1529 fdt_blob = (char *)load_start;
1531 case IMAGE_FORMAT_FIT:
1533 * This case will catch both: new uImage format
1534 * (libfdt based) and raw FDT blob (also libfdt
1537 #if defined(CONFIG_FIT)
1538 /* check FDT blob vs FIT blob */
1539 if (fit_check_format((const void *)fdt_addr)) {
1543 fit_hdr = (void *)fdt_addr;
1544 printf("## Flattened Device Tree from FIT "
1548 if (!fit_uname_fdt) {
1550 * no FDT blob image node unit name,
1551 * try to get config node first. If
1552 * config unit node name is NULL
1553 * fit_conf_get_node() will try to
1554 * find default config node
1556 cfg_noffset = fit_conf_get_node(fit_hdr,
1559 if (cfg_noffset < 0) {
1560 fdt_error("Could not find "
1566 fit_uname_config = fdt_get_name(fit_hdr,
1568 printf(" Using '%s' configuration\n",
1571 fdt_noffset = fit_conf_get_fdt_node(
1574 fit_uname_fdt = fit_get_name(fit_hdr,
1577 /* get FDT component image node offset */
1578 fdt_noffset = fit_image_get_node(
1582 if (fdt_noffset < 0) {
1583 fdt_error("Could not find subimage "
1588 printf(" Trying '%s' FDT blob subimage\n",
1591 if (!fit_check_fdt(fit_hdr, fdt_noffset,
1595 /* get ramdisk image data address and length */
1596 if (fit_image_get_data(fit_hdr, fdt_noffset,
1598 fdt_error("Could not find FDT "
1603 /* verift that image data is a proper FDT blob */
1604 if (fdt_check_header((char *)data) != 0) {
1605 fdt_error("Subimage data is not a FTD");
1610 * move image data to the load address,
1611 * make sure we don't overwrite initial image
1613 image_start = (ulong)fit_hdr;
1614 image_end = fit_get_end(fit_hdr);
1616 if (fit_image_get_load(fit_hdr, fdt_noffset,
1617 &load_start) == 0) {
1618 load_end = load_start + size;
1620 if ((load_start < image_end) &&
1621 (load_end > image_start)) {
1622 fdt_error("FDT overwritten");
1626 printf(" Loading FDT from 0x%08lx "
1631 memmove((void *)load_start,
1632 (void *)data, size);
1634 fdt_blob = (char *)load_start;
1636 fdt_blob = (char *)data;
1639 images->fit_hdr_fdt = fit_hdr;
1640 images->fit_uname_fdt = fit_uname_fdt;
1641 images->fit_noffset_fdt = fdt_noffset;
1649 fdt_blob = (char *)fdt_addr;
1650 debug("* fdt: raw FDT blob\n");
1651 printf("## Flattened Device Tree blob at "
1652 "%08lx\n", (long)fdt_blob);
1656 puts("ERROR: Did not find a cmdline Flattened Device "
1661 printf(" Booting using the fdt blob at 0x%p\n", fdt_blob);
1663 } else if (images->legacy_hdr_valid &&
1664 image_check_type(&images->legacy_hdr_os_copy,
1667 ulong fdt_data, fdt_len;
1670 * Now check if we have a legacy multi-component image,
1671 * get second entry data start address and len.
1673 printf("## Flattened Device Tree from multi "
1674 "component Image at %08lX\n",
1675 (ulong)images->legacy_hdr_os);
1677 image_multi_getimg(images->legacy_hdr_os, 2, &fdt_data,
1681 fdt_blob = (char *)fdt_data;
1682 printf(" Booting using the fdt at 0x%p\n", fdt_blob);
1684 if (fdt_check_header(fdt_blob) != 0) {
1685 fdt_error("image is not a fdt");
1689 if (fdt_totalsize(fdt_blob) != fdt_len) {
1690 fdt_error("fdt size != image size");
1694 debug("## No Flattened Device Tree\n");
1698 debug("## No Flattened Device Tree\n");
1702 *of_flat_tree = fdt_blob;
1703 *of_size = fdt_totalsize(fdt_blob);
1704 debug(" of_flat_tree at 0x%08lx size 0x%08lx\n",
1705 (ulong)*of_flat_tree, *of_size);
1710 *of_flat_tree = NULL;
1714 #endif /* CONFIG_OF_LIBFDT */
1716 #ifdef CONFIG_SYS_BOOT_GET_CMDLINE
1718 * boot_get_cmdline - allocate and initialize kernel cmdline
1719 * @lmb: pointer to lmb handle, will be used for memory mgmt
1720 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1721 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1723 * boot_get_cmdline() allocates space for kernel command line below
1724 * BOOTMAPSZ + getenv_bootm_low() address. If "bootargs" U-boot environemnt
1725 * variable is present its contents is copied to allocated kernel
1732 int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
1737 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
1738 getenv_bootm_mapsize() + getenv_bootm_low());
1740 if (cmdline == NULL)
1743 if ((s = getenv("bootargs")) == NULL)
1748 *cmd_start = (ulong) & cmdline[0];
1749 *cmd_end = *cmd_start + strlen(cmdline);
1751 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1755 #endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
1757 #ifdef CONFIG_SYS_BOOT_GET_KBD
1759 * boot_get_kbd - allocate and initialize kernel copy of board info
1760 * @lmb: pointer to lmb handle, will be used for memory mgmt
1761 * @kbd: double pointer to board info data
1763 * boot_get_kbd() allocates space for kernel copy of board info data below
1764 * BOOTMAPSZ + getenv_bootm_low() address and kernel board info is initialized
1765 * with the current u-boot board info data.
1771 int boot_get_kbd(struct lmb *lmb, bd_t **kbd)
1773 *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
1774 getenv_bootm_mapsize() + getenv_bootm_low());
1780 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1782 #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1783 do_bdinfo(NULL, 0, 0, NULL);
1788 #endif /* CONFIG_SYS_BOOT_GET_KBD */
1789 #endif /* !USE_HOSTCC */
1791 #if defined(CONFIG_FIT)
1792 /*****************************************************************************/
1793 /* New uImage format routines */
1794 /*****************************************************************************/
1796 static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
1797 ulong *addr, const char **name)
1804 sep = strchr(spec, sepc);
1807 *addr = simple_strtoul(spec, NULL, 16);
1817 * fit_parse_conf - parse FIT configuration spec
1818 * @spec: input string, containing configuration spec
1819 * @add_curr: current image address (to be used as a possible default)
1820 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1822 * @conf_name double pointer to a char, will hold pointer to a configuration
1825 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1826 * where <addr> is a FIT image address that contains configuration
1827 * with a <conf> unit name.
1829 * Address part is optional, and if omitted default add_curr will
1833 * 1 if spec is a valid configuration string,
1834 * addr and conf_name are set accordingly
1837 int fit_parse_conf(const char *spec, ulong addr_curr,
1838 ulong *addr, const char **conf_name)
1840 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
1844 * fit_parse_subimage - parse FIT subimage spec
1845 * @spec: input string, containing subimage spec
1846 * @add_curr: current image address (to be used as a possible default)
1847 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1849 * @image_name: double pointer to a char, will hold pointer to a subimage name
1851 * fit_parse_subimage() expects subimage spec in the for of
1852 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1853 * subimage with a <subimg> unit name.
1855 * Address part is optional, and if omitted default add_curr will
1859 * 1 if spec is a valid subimage string,
1860 * addr and image_name are set accordingly
1863 int fit_parse_subimage(const char *spec, ulong addr_curr,
1864 ulong *addr, const char **image_name)
1866 return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
1868 #endif /* !USE_HOSTCC */
1870 static void fit_get_debug(const void *fit, int noffset,
1871 char *prop_name, int err)
1873 debug("Can't get '%s' property from FIT 0x%08lx, "
1874 "node: offset %d, name %s (%s)\n",
1875 prop_name, (ulong)fit, noffset,
1876 fit_get_name(fit, noffset, NULL),
1881 * fit_print_contents - prints out the contents of the FIT format image
1882 * @fit: pointer to the FIT format image header
1883 * @p: pointer to prefix string
1885 * fit_print_contents() formats a multi line FIT image contents description.
1886 * The routine prints out FIT image properties (root node level) follwed by
1887 * the details of each component image.
1890 * no returned results
1892 void fit_print_contents(const void *fit)
1911 /* Root node properties */
1912 ret = fit_get_desc(fit, 0, &desc);
1913 printf("%sFIT description: ", p);
1915 printf("unavailable\n");
1917 printf("%s\n", desc);
1919 if (IMAGE_ENABLE_TIMESTAMP) {
1920 ret = fit_get_timestamp(fit, 0, ×tamp);
1921 printf("%sCreated: ", p);
1923 printf("unavailable\n");
1925 genimg_print_time(timestamp);
1928 /* Find images parent node offset */
1929 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1930 if (images_noffset < 0) {
1931 printf("Can't find images parent node '%s' (%s)\n",
1932 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1936 /* Process its subnodes, print out component images details */
1937 for (ndepth = 0, count = 0,
1938 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1939 (noffset >= 0) && (ndepth > 0);
1940 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1943 * Direct child node of the images parent node,
1944 * i.e. component image node.
1946 printf("%s Image %u (%s)\n", p, count++,
1947 fit_get_name(fit, noffset, NULL));
1949 fit_image_print(fit, noffset, p);
1953 /* Find configurations parent node offset */
1954 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1955 if (confs_noffset < 0) {
1956 debug("Can't get configurations parent node '%s' (%s)\n",
1957 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1961 /* get default configuration unit name from default property */
1962 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
1964 printf("%s Default Configuration: '%s'\n", p, uname);
1966 /* Process its subnodes, print out configurations details */
1967 for (ndepth = 0, count = 0,
1968 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1969 (noffset >= 0) && (ndepth > 0);
1970 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1973 * Direct child node of the configurations parent node,
1974 * i.e. configuration node.
1976 printf("%s Configuration %u (%s)\n", p, count++,
1977 fit_get_name(fit, noffset, NULL));
1979 fit_conf_print(fit, noffset, p);
1985 * fit_image_print - prints out the FIT component image details
1986 * @fit: pointer to the FIT format image header
1987 * @image_noffset: offset of the component image node
1988 * @p: pointer to prefix string
1990 * fit_image_print() lists all mandatory properies for the processed component
1991 * image. If present, hash nodes are printed out as well. Load
1992 * address for images of type firmware is also printed out. Since the load
1993 * address is not mandatory for firmware images, it will be output as
1994 * "unavailable" when not present.
1997 * no returned results
1999 void fit_image_print(const void *fit, int image_noffset, const char *p)
2002 uint8_t type, arch, os, comp;
2010 /* Mandatory properties */
2011 ret = fit_get_desc(fit, image_noffset, &desc);
2012 printf("%s Description: ", p);
2014 printf("unavailable\n");
2016 printf("%s\n", desc);
2018 fit_image_get_type(fit, image_noffset, &type);
2019 printf("%s Type: %s\n", p, genimg_get_type_name(type));
2021 fit_image_get_comp(fit, image_noffset, &comp);
2022 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
2024 ret = fit_image_get_data(fit, image_noffset, &data, &size);
2027 printf("%s Data Start: ", p);
2029 printf("unavailable\n");
2031 printf("0x%08lx\n", (ulong)data);
2034 printf("%s Data Size: ", p);
2036 printf("unavailable\n");
2038 genimg_print_size(size);
2040 /* Remaining, type dependent properties */
2041 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
2042 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
2043 (type == IH_TYPE_FLATDT)) {
2044 fit_image_get_arch(fit, image_noffset, &arch);
2045 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
2048 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK)) {
2049 fit_image_get_os(fit, image_noffset, &os);
2050 printf("%s OS: %s\n", p, genimg_get_os_name(os));
2053 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
2054 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK)) {
2055 ret = fit_image_get_load(fit, image_noffset, &load);
2056 printf("%s Load Address: ", p);
2058 printf("unavailable\n");
2060 printf("0x%08lx\n", load);
2063 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
2064 (type == IH_TYPE_RAMDISK)) {
2065 fit_image_get_entry(fit, image_noffset, &entry);
2066 printf("%s Entry Point: ", p);
2068 printf("unavailable\n");
2070 printf("0x%08lx\n", entry);
2073 /* Process all hash subnodes of the component image node */
2074 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
2075 (noffset >= 0) && (ndepth > 0);
2076 noffset = fdt_next_node(fit, noffset, &ndepth)) {
2078 /* Direct child node of the component image node */
2079 fit_image_print_hash(fit, noffset, p);
2085 * fit_image_print_hash - prints out the hash node details
2086 * @fit: pointer to the FIT format image header
2087 * @noffset: offset of the hash node
2088 * @p: pointer to prefix string
2090 * fit_image_print_hash() lists properies for the processed hash node
2093 * no returned results
2095 void fit_image_print_hash(const void *fit, int noffset, const char *p)
2103 * Check subnode name, must be equal to "hash".
2104 * Multiple hash nodes require unique unit node
2105 * names, e.g. hash@1, hash@2, etc.
2107 if (strncmp(fit_get_name(fit, noffset, NULL),
2109 strlen(FIT_HASH_NODENAME)) != 0)
2112 debug("%s Hash node: '%s'\n", p,
2113 fit_get_name(fit, noffset, NULL));
2115 printf("%s Hash algo: ", p);
2116 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
2117 printf("invalid/unsupported\n");
2120 printf("%s\n", algo);
2122 ret = fit_image_hash_get_value(fit, noffset, &value,
2124 printf("%s Hash value: ", p);
2126 printf("unavailable\n");
2128 for (i = 0; i < value_len; i++)
2129 printf("%02x", value[i]);
2133 debug("%s Hash len: %d\n", p, value_len);
2137 * fit_get_desc - get node description property
2138 * @fit: pointer to the FIT format image header
2139 * @noffset: node offset
2140 * @desc: double pointer to the char, will hold pointer to the descrption
2142 * fit_get_desc() reads description property from a given node, if
2143 * description is found pointer to it is returened in third call argument.
2149 int fit_get_desc(const void *fit, int noffset, char **desc)
2153 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
2154 if (*desc == NULL) {
2155 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
2163 * fit_get_timestamp - get node timestamp property
2164 * @fit: pointer to the FIT format image header
2165 * @noffset: node offset
2166 * @timestamp: pointer to the time_t, will hold read timestamp
2168 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
2169 * is found and has a correct size its value is retured in third call
2174 * -1, on property read failure
2175 * -2, on wrong timestamp size
2177 int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
2182 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
2184 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
2187 if (len != sizeof(uint32_t)) {
2188 debug("FIT timestamp with incorrect size of (%u)\n", len);
2192 *timestamp = uimage_to_cpu(*((uint32_t *)data));
2197 * fit_image_get_node - get node offset for component image of a given unit name
2198 * @fit: pointer to the FIT format image header
2199 * @image_uname: component image node unit name
2201 * fit_image_get_node() finds a component image (withing the '/images'
2202 * node) of a provided unit name. If image is found its node offset is
2203 * returned to the caller.
2206 * image node offset when found (>=0)
2207 * negative number on failure (FDT_ERR_* code)
2209 int fit_image_get_node(const void *fit, const char *image_uname)
2211 int noffset, images_noffset;
2213 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
2214 if (images_noffset < 0) {
2215 debug("Can't find images parent node '%s' (%s)\n",
2216 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
2217 return images_noffset;
2220 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
2222 debug("Can't get node offset for image unit name: '%s' (%s)\n",
2223 image_uname, fdt_strerror(noffset));
2230 * fit_image_get_os - get os id for a given component image node
2231 * @fit: pointer to the FIT format image header
2232 * @noffset: component image node offset
2233 * @os: pointer to the uint8_t, will hold os numeric id
2235 * fit_image_get_os() finds os property in a given component image node.
2236 * If the property is found, its (string) value is translated to the numeric
2237 * id which is returned to the caller.
2243 int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
2248 /* Get OS name from property data */
2249 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
2251 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
2256 /* Translate OS name to id */
2257 *os = genimg_get_os_id(data);
2262 * fit_image_get_arch - get arch id for a given component image node
2263 * @fit: pointer to the FIT format image header
2264 * @noffset: component image node offset
2265 * @arch: pointer to the uint8_t, will hold arch numeric id
2267 * fit_image_get_arch() finds arch property in a given component image node.
2268 * If the property is found, its (string) value is translated to the numeric
2269 * id which is returned to the caller.
2275 int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
2280 /* Get architecture name from property data */
2281 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
2283 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
2288 /* Translate architecture name to id */
2289 *arch = genimg_get_arch_id(data);
2294 * fit_image_get_type - get type id for a given component image node
2295 * @fit: pointer to the FIT format image header
2296 * @noffset: component image node offset
2297 * @type: pointer to the uint8_t, will hold type numeric id
2299 * fit_image_get_type() finds type property in a given component image node.
2300 * If the property is found, its (string) value is translated to the numeric
2301 * id which is returned to the caller.
2307 int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
2312 /* Get image type name from property data */
2313 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
2315 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
2320 /* Translate image type name to id */
2321 *type = genimg_get_type_id(data);
2326 * fit_image_get_comp - get comp id for a given component image node
2327 * @fit: pointer to the FIT format image header
2328 * @noffset: component image node offset
2329 * @comp: pointer to the uint8_t, will hold comp numeric id
2331 * fit_image_get_comp() finds comp property in a given component image node.
2332 * If the property is found, its (string) value is translated to the numeric
2333 * id which is returned to the caller.
2339 int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
2344 /* Get compression name from property data */
2345 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
2347 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
2352 /* Translate compression name to id */
2353 *comp = genimg_get_comp_id(data);
2358 * fit_image_get_load - get load address property for a given component image node
2359 * @fit: pointer to the FIT format image header
2360 * @noffset: component image node offset
2361 * @load: pointer to the uint32_t, will hold load address
2363 * fit_image_get_load() finds load address property in a given component image node.
2364 * If the property is found, its value is returned to the caller.
2370 int fit_image_get_load(const void *fit, int noffset, ulong *load)
2373 const uint32_t *data;
2375 data = fdt_getprop(fit, noffset, FIT_LOAD_PROP, &len);
2377 fit_get_debug(fit, noffset, FIT_LOAD_PROP, len);
2381 *load = uimage_to_cpu(*data);
2386 * fit_image_get_entry - get entry point address property for a given component image node
2387 * @fit: pointer to the FIT format image header
2388 * @noffset: component image node offset
2389 * @entry: pointer to the uint32_t, will hold entry point address
2391 * fit_image_get_entry() finds entry point address property in a given component image node.
2392 * If the property is found, its value is returned to the caller.
2398 int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
2401 const uint32_t *data;
2403 data = fdt_getprop(fit, noffset, FIT_ENTRY_PROP, &len);
2405 fit_get_debug(fit, noffset, FIT_ENTRY_PROP, len);
2409 *entry = uimage_to_cpu(*data);
2414 * fit_image_get_data - get data property and its size for a given component image node
2415 * @fit: pointer to the FIT format image header
2416 * @noffset: component image node offset
2417 * @data: double pointer to void, will hold data property's data address
2418 * @size: pointer to size_t, will hold data property's data size
2420 * fit_image_get_data() finds data property in a given component image node.
2421 * If the property is found its data start address and size are returned to
2428 int fit_image_get_data(const void *fit, int noffset,
2429 const void **data, size_t *size)
2433 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
2434 if (*data == NULL) {
2435 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
2445 * fit_image_hash_get_algo - get hash algorithm name
2446 * @fit: pointer to the FIT format image header
2447 * @noffset: hash node offset
2448 * @algo: double pointer to char, will hold pointer to the algorithm name
2450 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
2451 * If the property is found its data start address is returned to the caller.
2457 int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
2461 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
2462 if (*algo == NULL) {
2463 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
2471 * fit_image_hash_get_value - get hash value and length
2472 * @fit: pointer to the FIT format image header
2473 * @noffset: hash node offset
2474 * @value: double pointer to uint8_t, will hold address of a hash value data
2475 * @value_len: pointer to an int, will hold hash data length
2477 * fit_image_hash_get_value() finds hash value property in a given hash node.
2478 * If the property is found its data start address and size are returned to
2485 int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
2490 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
2491 if (*value == NULL) {
2492 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
2503 * fit_image_hash_get_ignore - get hash ignore flag
2504 * @fit: pointer to the FIT format image header
2505 * @noffset: hash node offset
2506 * @ignore: pointer to an int, will hold hash ignore flag
2508 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
2509 * If the property is found and non-zero, the hash algorithm is not verified by
2510 * u-boot automatically.
2513 * 0, on ignore not found
2514 * value, on ignore found
2516 int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
2521 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
2522 if (value == NULL || len != sizeof(int))
2532 * fit_set_timestamp - set node timestamp property
2533 * @fit: pointer to the FIT format image header
2534 * @noffset: node offset
2535 * @timestamp: timestamp value to be set
2537 * fit_set_timestamp() attempts to set timestamp property in the requested
2538 * node and returns operation status to the caller.
2542 * -1, on property read failure
2544 int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
2549 t = cpu_to_uimage(timestamp);
2550 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
2553 printf("Can't set '%s' property for '%s' node (%s)\n",
2554 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
2563 * calculate_hash - calculate and return hash for provided input data
2564 * @data: pointer to the input data
2565 * @data_len: data length
2566 * @algo: requested hash algorithm
2567 * @value: pointer to the char, will hold hash value data (caller must
2568 * allocate enough free space)
2569 * value_len: length of the calculated hash
2571 * calculate_hash() computes input data hash according to the requested algorithm.
2572 * Resulting hash value is placed in caller provided 'value' buffer, length
2573 * of the calculated hash is returned via value_len pointer argument.
2577 * -1, when algo is unsupported
2579 static int calculate_hash(const void *data, int data_len, const char *algo,
2580 uint8_t *value, int *value_len)
2582 if (strcmp(algo, "crc32") == 0) {
2583 *((uint32_t *)value) = crc32_wd(0, data, data_len,
2585 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
2587 } else if (strcmp(algo, "sha1") == 0) {
2588 sha1_csum_wd((unsigned char *) data, data_len,
2589 (unsigned char *) value, CHUNKSZ_SHA1);
2591 } else if (strcmp(algo, "md5") == 0) {
2592 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
2595 debug("Unsupported hash alogrithm\n");
2603 * fit_set_hashes - process FIT component image nodes and calculate hashes
2604 * @fit: pointer to the FIT format image header
2606 * fit_set_hashes() adds hash values for all component images in the FIT blob.
2607 * Hashes are calculated for all component images which have hash subnodes
2608 * with algorithm property set to one of the supported hash algorithms.
2612 * libfdt error code, on failure
2614 int fit_set_hashes(void *fit)
2621 /* Find images parent node offset */
2622 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
2623 if (images_noffset < 0) {
2624 printf("Can't find images parent node '%s' (%s)\n",
2625 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
2626 return images_noffset;
2629 /* Process its subnodes, print out component images details */
2630 for (ndepth = 0, noffset = fdt_next_node(fit, images_noffset, &ndepth);
2631 (noffset >= 0) && (ndepth > 0);
2632 noffset = fdt_next_node(fit, noffset, &ndepth)) {
2635 * Direct child node of the images parent node,
2636 * i.e. component image node.
2638 ret = fit_image_set_hashes(fit, noffset);
2648 * fit_image_set_hashes - calculate/set hashes for given component image node
2649 * @fit: pointer to the FIT format image header
2650 * @image_noffset: requested component image node
2652 * fit_image_set_hashes() adds hash values for an component image node. All
2653 * existing hash subnodes are checked, if algorithm property is set to one of
2654 * the supported hash algorithms, hash value is computed and corresponding
2655 * hash node property is set, for example:
2657 * Input component image node structure:
2659 * o image@1 (at image_noffset)
2660 * | - data = [binary data]
2664 * Output component image node structure:
2666 * o image@1 (at image_noffset)
2667 * | - data = [binary data]
2670 * |- value = sha1(data)
2676 int fit_image_set_hashes(void *fit, int image_noffset)
2681 uint8_t value[FIT_MAX_HASH_LEN];
2686 /* Get image data and data length */
2687 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
2688 printf("Can't get image data/size\n");
2692 /* Process all hash subnodes of the component image node */
2693 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
2694 (noffset >= 0) && (ndepth > 0);
2695 noffset = fdt_next_node(fit, noffset, &ndepth)) {
2697 /* Direct child node of the component image node */
2700 * Check subnode name, must be equal to "hash".
2701 * Multiple hash nodes require unique unit node
2702 * names, e.g. hash@1, hash@2, etc.
2704 if (strncmp(fit_get_name(fit, noffset, NULL),
2706 strlen(FIT_HASH_NODENAME)) != 0) {
2707 /* Not a hash subnode, skip it */
2711 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
2712 printf("Can't get hash algo property for "
2713 "'%s' hash node in '%s' image node\n",
2714 fit_get_name(fit, noffset, NULL),
2715 fit_get_name(fit, image_noffset, NULL));
2719 if (calculate_hash(data, size, algo, value,
2721 printf("Unsupported hash algorithm (%s) for "
2722 "'%s' hash node in '%s' image node\n",
2723 algo, fit_get_name(fit, noffset, NULL),
2724 fit_get_name(fit, image_noffset,
2729 if (fit_image_hash_set_value(fit, noffset, value,
2731 printf("Can't set hash value for "
2732 "'%s' hash node in '%s' image node\n",
2733 fit_get_name(fit, noffset, NULL),
2734 fit_get_name(fit, image_noffset, NULL));
2744 * fit_image_hash_set_value - set hash value in requested has node
2745 * @fit: pointer to the FIT format image header
2746 * @noffset: hash node offset
2747 * @value: hash value to be set
2748 * @value_len: hash value length
2750 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2751 * given and returns operation status to the caller.
2757 int fit_image_hash_set_value(void *fit, int noffset, uint8_t *value,
2762 ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
2764 printf("Can't set hash '%s' property for '%s' node(%s)\n",
2765 FIT_VALUE_PROP, fit_get_name(fit, noffset, NULL),
2772 #endif /* USE_HOSTCC */
2775 * fit_image_check_hashes - verify data intergity
2776 * @fit: pointer to the FIT format image header
2777 * @image_noffset: component image node offset
2779 * fit_image_check_hashes() goes over component image hash nodes,
2780 * re-calculates each data hash and compares with the value stored in hash
2784 * 1, if all hashes are valid
2785 * 0, otherwise (or on error)
2787 int fit_image_check_hashes(const void *fit, int image_noffset)
2797 uint8_t value[FIT_MAX_HASH_LEN];
2803 /* Get image data and data length */
2804 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
2805 printf("Can't get image data/size\n");
2809 /* Process all hash subnodes of the component image node */
2810 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
2811 (noffset >= 0) && (ndepth > 0);
2812 noffset = fdt_next_node(fit, noffset, &ndepth)) {
2814 /* Direct child node of the component image node */
2817 * Check subnode name, must be equal to "hash".
2818 * Multiple hash nodes require unique unit node
2819 * names, e.g. hash@1, hash@2, etc.
2821 if (strncmp(fit_get_name(fit, noffset, NULL),
2823 strlen(FIT_HASH_NODENAME)) != 0)
2826 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
2827 err_msg = " error!\nCan't get hash algo "
2834 fit_image_hash_get_ignore(fit, noffset, &ignore);
2836 printf("-skipped ");
2841 if (fit_image_hash_get_value(fit, noffset, &fit_value,
2843 err_msg = " error!\nCan't get hash value "
2848 if (calculate_hash(data, size, algo, value,
2850 err_msg = " error!\n"
2851 "Unsupported hash algorithm";
2855 if (value_len != fit_value_len) {
2856 err_msg = " error !\nBad hash value len";
2858 } else if (memcmp(value, fit_value, value_len) != 0) {
2859 err_msg = " error!\nBad hash value";
2866 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
2867 err_msg = " error!\nCorrupted or truncated tree";
2874 printf("%s for '%s' hash node in '%s' image node\n",
2875 err_msg, fit_get_name(fit, noffset, NULL),
2876 fit_get_name(fit, image_noffset, NULL));
2881 * fit_all_image_check_hashes - verify data intergity for all images
2882 * @fit: pointer to the FIT format image header
2884 * fit_all_image_check_hashes() goes over all images in the FIT and
2885 * for every images checks if all it's hashes are valid.
2888 * 1, if all hashes of all images are valid
2889 * 0, otherwise (or on error)
2891 int fit_all_image_check_hashes(const void *fit)
2898 /* Find images parent node offset */
2899 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
2900 if (images_noffset < 0) {
2901 printf("Can't find images parent node '%s' (%s)\n",
2902 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
2906 /* Process all image subnodes, check hashes for each */
2907 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
2909 for (ndepth = 0, count = 0,
2910 noffset = fdt_next_node(fit, images_noffset, &ndepth);
2911 (noffset >= 0) && (ndepth > 0);
2912 noffset = fdt_next_node(fit, noffset, &ndepth)) {
2915 * Direct child node of the images parent node,
2916 * i.e. component image node.
2918 printf(" Hash(es) for Image %u (%s): ", count++,
2919 fit_get_name(fit, noffset, NULL));
2921 if (!fit_image_check_hashes(fit, noffset))
2930 * fit_image_check_os - check whether image node is of a given os type
2931 * @fit: pointer to the FIT format image header
2932 * @noffset: component image node offset
2933 * @os: requested image os
2935 * fit_image_check_os() reads image os property and compares its numeric
2936 * id with the requested os. Comparison result is returned to the caller.
2939 * 1 if image is of given os type
2940 * 0 otherwise (or on error)
2942 int fit_image_check_os(const void *fit, int noffset, uint8_t os)
2946 if (fit_image_get_os(fit, noffset, &image_os))
2948 return (os == image_os);
2952 * fit_image_check_arch - check whether image node is of a given arch
2953 * @fit: pointer to the FIT format image header
2954 * @noffset: component image node offset
2955 * @arch: requested imagearch
2957 * fit_image_check_arch() reads image arch property and compares its numeric
2958 * id with the requested arch. Comparison result is returned to the caller.
2961 * 1 if image is of given arch
2962 * 0 otherwise (or on error)
2964 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
2968 if (fit_image_get_arch(fit, noffset, &image_arch))
2970 return (arch == image_arch);
2974 * fit_image_check_type - check whether image node is of a given type
2975 * @fit: pointer to the FIT format image header
2976 * @noffset: component image node offset
2977 * @type: requested image type
2979 * fit_image_check_type() reads image type property and compares its numeric
2980 * id with the requested type. Comparison result is returned to the caller.
2983 * 1 if image is of given type
2984 * 0 otherwise (or on error)
2986 int fit_image_check_type(const void *fit, int noffset, uint8_t type)
2990 if (fit_image_get_type(fit, noffset, &image_type))
2992 return (type == image_type);
2996 * fit_image_check_comp - check whether image node uses given compression
2997 * @fit: pointer to the FIT format image header
2998 * @noffset: component image node offset
2999 * @comp: requested image compression type
3001 * fit_image_check_comp() reads image compression property and compares its
3002 * numeric id with the requested compression type. Comparison result is
3003 * returned to the caller.
3006 * 1 if image uses requested compression
3007 * 0 otherwise (or on error)
3009 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
3013 if (fit_image_get_comp(fit, noffset, &image_comp))
3015 return (comp == image_comp);
3019 * fit_check_format - sanity check FIT image format
3020 * @fit: pointer to the FIT format image header
3022 * fit_check_format() runs a basic sanity FIT image verification.
3023 * Routine checks for mandatory properties, nodes, etc.
3029 int fit_check_format(const void *fit)
3031 /* mandatory / node 'description' property */
3032 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
3033 debug("Wrong FIT format: no description\n");
3037 if (IMAGE_ENABLE_TIMESTAMP) {
3038 /* mandatory / node 'timestamp' property */
3039 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
3040 debug("Wrong FIT format: no timestamp\n");
3045 /* mandatory subimages parent '/images' node */
3046 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
3047 debug("Wrong FIT format: no images parent node\n");
3056 * fit_conf_find_compat
3057 * @fit: pointer to the FIT format image header
3058 * @fdt: pointer to the device tree to compare against
3060 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
3061 * most compatible with the passed in device tree.
3070 * |-o configurations
3078 * |-compatible = "foo,bar", "bim,bam"
3081 * |-compatible = "foo,bar",
3084 * |-compatible = "bim,bam", "baz,biz"
3086 * Configuration 1 would be picked because the first string in U-Boot's
3087 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
3088 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
3091 * offset to the configuration to use if one was found
3094 int fit_conf_find_compat(const void *fit, const void *fdt)
3097 int noffset, confs_noffset, images_noffset;
3098 const void *fdt_compat;
3100 int best_match_offset = 0;
3101 int best_match_pos = 0;
3103 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
3104 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
3105 if (confs_noffset < 0 || images_noffset < 0) {
3106 debug("Can't find configurations or images nodes.\n");
3110 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
3112 debug("Fdt for comparison has no \"compatible\" property.\n");
3117 * Loop over the configurations in the FIT image.
3119 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
3120 (noffset >= 0) && (ndepth > 0);
3121 noffset = fdt_next_node(fit, noffset, &ndepth)) {
3123 const char *kfdt_name;
3125 const char *cur_fdt_compat;
3133 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
3135 debug("No fdt property found.\n");
3138 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
3140 if (kfdt_noffset < 0) {
3141 debug("No image node named \"%s\" found.\n",
3146 * Get a pointer to this configuration's fdt.
3148 if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) {
3149 debug("Failed to get fdt \"%s\".\n", kfdt_name);
3153 len = fdt_compat_len;
3154 cur_fdt_compat = fdt_compat;
3156 * Look for a match for each U-Boot compatibility string in
3157 * turn in this configuration's fdt.
3159 for (i = 0; len > 0 &&
3160 (!best_match_offset || best_match_pos > i); i++) {
3161 int cur_len = strlen(cur_fdt_compat) + 1;
3163 if (!fdt_node_check_compatible(kfdt, 0,
3165 best_match_offset = noffset;
3170 cur_fdt_compat += cur_len;
3173 if (!best_match_offset) {
3174 debug("No match found.\n");
3178 return best_match_offset;
3182 * fit_conf_get_node - get node offset for configuration of a given unit name
3183 * @fit: pointer to the FIT format image header
3184 * @conf_uname: configuration node unit name
3186 * fit_conf_get_node() finds a configuration (withing the '/configurations'
3187 * parant node) of a provided unit name. If configuration is found its node offset
3188 * is returned to the caller.
3190 * When NULL is provided in second argument fit_conf_get_node() will search
3191 * for a default configuration node instead. Default configuration node unit name
3192 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
3195 * configuration node offset when found (>=0)
3196 * negative number on failure (FDT_ERR_* code)
3198 int fit_conf_get_node(const void *fit, const char *conf_uname)
3200 int noffset, confs_noffset;
3203 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
3204 if (confs_noffset < 0) {
3205 debug("Can't find configurations parent node '%s' (%s)\n",
3206 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
3207 return confs_noffset;
3210 if (conf_uname == NULL) {
3211 /* get configuration unit name from the default property */
3212 debug("No configuration specified, trying default...\n");
3213 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
3214 FIT_DEFAULT_PROP, &len);
3215 if (conf_uname == NULL) {
3216 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
3220 debug("Found default configuration: '%s'\n", conf_uname);
3223 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
3225 debug("Can't get node offset for configuration unit name: "
3227 conf_uname, fdt_strerror(noffset));
3233 static int __fit_conf_get_prop_node(const void *fit, int noffset,
3234 const char *prop_name)
3239 /* get kernel image unit name from configuration kernel property */
3240 uname = (char *)fdt_getprop(fit, noffset, prop_name, &len);
3244 return fit_image_get_node(fit, uname);
3248 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
3249 * a given configuration
3250 * @fit: pointer to the FIT format image header
3251 * @noffset: configuration node offset
3253 * fit_conf_get_kernel_node() retrives kernel image node unit name from
3254 * configuration FIT_KERNEL_PROP property and translates it to the node
3258 * image node offset when found (>=0)
3259 * negative number on failure (FDT_ERR_* code)
3261 int fit_conf_get_kernel_node(const void *fit, int noffset)
3263 return __fit_conf_get_prop_node(fit, noffset, FIT_KERNEL_PROP);
3267 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
3268 * a given configuration
3269 * @fit: pointer to the FIT format image header
3270 * @noffset: configuration node offset
3272 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
3273 * configuration FIT_KERNEL_PROP property and translates it to the node
3277 * image node offset when found (>=0)
3278 * negative number on failure (FDT_ERR_* code)
3280 int fit_conf_get_ramdisk_node(const void *fit, int noffset)
3282 return __fit_conf_get_prop_node(fit, noffset, FIT_RAMDISK_PROP);
3286 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
3287 * a given configuration
3288 * @fit: pointer to the FIT format image header
3289 * @noffset: configuration node offset
3291 * fit_conf_get_fdt_node() retrives fdt image node unit name from
3292 * configuration FIT_KERNEL_PROP property and translates it to the node
3296 * image node offset when found (>=0)
3297 * negative number on failure (FDT_ERR_* code)
3299 int fit_conf_get_fdt_node(const void *fit, int noffset)
3301 return __fit_conf_get_prop_node(fit, noffset, FIT_FDT_PROP);
3305 * fit_conf_print - prints out the FIT configuration details
3306 * @fit: pointer to the FIT format image header
3307 * @noffset: offset of the configuration node
3308 * @p: pointer to prefix string
3310 * fit_conf_print() lists all mandatory properies for the processed
3311 * configuration node.
3314 * no returned results
3316 void fit_conf_print(const void *fit, int noffset, const char *p)
3322 /* Mandatory properties */
3323 ret = fit_get_desc(fit, noffset, &desc);
3324 printf("%s Description: ", p);
3326 printf("unavailable\n");
3328 printf("%s\n", desc);
3330 uname = (char *)fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
3331 printf("%s Kernel: ", p);
3333 printf("unavailable\n");
3335 printf("%s\n", uname);
3337 /* Optional properties */
3338 uname = (char *)fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
3340 printf("%s Init Ramdisk: %s\n", p, uname);
3342 uname = (char *)fdt_getprop(fit, noffset, FIT_FDT_PROP, NULL);
3344 printf("%s FDT: %s\n", p, uname);
3348 * fit_check_ramdisk - verify FIT format ramdisk subimage
3349 * @fit_hdr: pointer to the FIT ramdisk header
3350 * @rd_noffset: ramdisk subimage node offset within FIT image
3351 * @arch: requested ramdisk image architecture type
3352 * @verify: data CRC verification flag
3354 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
3355 * specified FIT image.
3362 int fit_check_ramdisk(const void *fit, int rd_noffset, uint8_t arch,
3365 fit_image_print(fit, rd_noffset, " ");
3368 puts(" Verifying Hash Integrity ... ");
3369 if (!fit_image_check_hashes(fit, rd_noffset)) {
3370 puts("Bad Data Hash\n");
3371 bootstage_error(BOOTSTAGE_ID_FIT_RD_HASH);
3377 bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK_ALL);
3378 if (!fit_image_check_os(fit, rd_noffset, IH_OS_LINUX) ||
3379 !fit_image_check_arch(fit, rd_noffset, arch) ||
3380 !fit_image_check_type(fit, rd_noffset, IH_TYPE_RAMDISK)) {
3381 printf("No Linux %s Ramdisk Image\n",
3382 genimg_get_arch_name(arch));
3383 bootstage_error(BOOTSTAGE_ID_FIT_RD_CHECK_ALL);
3387 bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK_ALL_OK);
3390 #endif /* USE_HOSTCC */
3391 #endif /* CONFIG_FIT */