1 // SPDX-License-Identifier: GPL-2.0+
4 * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
14 #include <linux/kconfig.h>
17 #define __ASSEMBLY__ /* Dirty trick to get only #defines */
19 #define __ASM_STUB_PROCESSOR_H__ /* don't include asm/processor. */
23 #if defined(CONFIG_ENV_IS_IN_FLASH)
24 # ifndef CONFIG_ENV_ADDR
25 # define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
27 # ifndef CONFIG_ENV_OFFSET
28 # define CONFIG_ENV_OFFSET (CONFIG_ENV_ADDR - CONFIG_SYS_FLASH_BASE)
30 # if !defined(CONFIG_ENV_ADDR_REDUND) && defined(CONFIG_ENV_OFFSET_REDUND)
31 # define CONFIG_ENV_ADDR_REDUND (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET_REDUND)
33 # ifndef CONFIG_ENV_SIZE
34 # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
36 # if defined(CONFIG_ENV_ADDR_REDUND) && !defined(CONFIG_ENV_SIZE_REDUND)
37 # define CONFIG_ENV_SIZE_REDUND CONFIG_ENV_SIZE
39 # if (CONFIG_ENV_ADDR >= CONFIG_SYS_MONITOR_BASE) && \
40 ((CONFIG_ENV_ADDR + CONFIG_ENV_SIZE) <= (CONFIG_SYS_MONITOR_BASE + CONFIG_SYS_MONITOR_LEN))
41 # define ENV_IS_EMBEDDED
43 # if defined(CONFIG_ENV_ADDR_REDUND) || defined(CONFIG_ENV_OFFSET_REDUND)
44 # define CONFIG_SYS_REDUNDAND_ENVIRONMENT
46 #endif /* CONFIG_ENV_IS_IN_FLASH */
48 #if defined(ENV_IS_EMBEDDED) && !defined(CONFIG_BUILD_ENVCRC)
49 # define CONFIG_BUILD_ENVCRC
52 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
53 # define ENV_HEADER_SIZE (sizeof(uint32_t) + 1)
55 # define ENV_HEADER_SIZE (sizeof(uint32_t))
58 #define ENV_SIZE (CONFIG_ENV_SIZE - ENV_HEADER_SIZE)
61 #ifdef CONFIG_BUILD_ENVCRC
62 # include <env_internal.h>
63 extern unsigned int env_size;
64 extern env_t embedded_environment;
65 #endif /* CONFIG_BUILD_ENVCRC */
67 extern uint32_t crc32 (uint32_t, const unsigned char *, unsigned int);
69 int main (int argc, char **argv)
71 #ifdef CONFIG_BUILD_ENVCRC
72 unsigned char pad = 0x00;
74 unsigned char *envptr = (unsigned char *)&embedded_environment,
75 *dataptr = envptr + ENV_HEADER_SIZE;
76 unsigned int datasize = ENV_SIZE;
79 if (argv[1] && !strncmp(argv[1], "--binary", 8)) {
81 if (argv[1][8] == '=')
82 sscanf(argv[1] + 9, "%i", &ipad);
87 /* find the end of env */
88 for (eoe = 0; eoe < datasize - 1; ++eoe)
89 if (!dataptr[eoe] && !dataptr[eoe+1]) {
93 if (eoe < datasize - 1)
94 memset(dataptr + eoe, pad, datasize - eoe);
97 crc = crc32 (0, dataptr, datasize);
99 /* Check if verbose mode is activated passing a parameter to the program */
101 if (!strncmp(argv[1], "--binary", 8)) {
102 int le = (argc > 2 ? !strcmp(argv[2], "le") : 1);
103 size_t i, start, end, step;
106 end = ENV_HEADER_SIZE;
109 start = ENV_HEADER_SIZE - 1;
113 for (i = start; i != end; i += step)
114 printf("%c", (crc & (0xFF << (i * 8))) >> (i * 8));
115 if (fwrite(dataptr, 1, datasize, stdout) != datasize)
116 fprintf(stderr, "fwrite() failed: %s\n", strerror(errno));
118 printf("CRC32 from offset %08X to %08X of environment = %08X\n",
119 (unsigned int) (dataptr - envptr),
120 (unsigned int) (dataptr - envptr) + datasize,
124 printf ("0x%08X\n", crc);