xref: /openbmc/u-boot/tools/envcrc.c (revision fcf2fba4)
1 /*
2  * (C) Copyright 2001
3  * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 #include <errno.h>
9 #include <stdio.h>
10 #include <stdint.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <unistd.h>
14 
15 #include <linux/kconfig.h>
16 
17 #ifndef __ASSEMBLY__
18 #define	__ASSEMBLY__			/* Dirty trick to get only #defines	*/
19 #endif
20 #define	__ASM_STUB_PROCESSOR_H__	/* don't include asm/processor.		*/
21 #include <config.h>
22 #undef	__ASSEMBLY__
23 
24 #if defined(CONFIG_ENV_IS_IN_FLASH)
25 # ifndef  CONFIG_ENV_ADDR
26 #  define CONFIG_ENV_ADDR	(CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
27 # endif
28 # ifndef  CONFIG_ENV_OFFSET
29 #  define CONFIG_ENV_OFFSET (CONFIG_ENV_ADDR - CONFIG_SYS_FLASH_BASE)
30 # endif
31 # if !defined(CONFIG_ENV_ADDR_REDUND) && defined(CONFIG_ENV_OFFSET_REDUND)
32 #  define CONFIG_ENV_ADDR_REDUND	(CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET_REDUND)
33 # endif
34 # ifndef  CONFIG_ENV_SIZE
35 #  define CONFIG_ENV_SIZE	CONFIG_ENV_SECT_SIZE
36 # endif
37 # if defined(CONFIG_ENV_ADDR_REDUND) && !defined(CONFIG_ENV_SIZE_REDUND)
38 #  define CONFIG_ENV_SIZE_REDUND	CONFIG_ENV_SIZE
39 # endif
40 # if (CONFIG_ENV_ADDR >= CONFIG_SYS_MONITOR_BASE) && \
41      ((CONFIG_ENV_ADDR + CONFIG_ENV_SIZE) <= (CONFIG_SYS_MONITOR_BASE + CONFIG_SYS_MONITOR_LEN))
42 #  define ENV_IS_EMBEDDED
43 # endif
44 # if defined(CONFIG_ENV_ADDR_REDUND) || defined(CONFIG_ENV_OFFSET_REDUND)
45 #  define CONFIG_SYS_REDUNDAND_ENVIRONMENT
46 # endif
47 #endif	/* CONFIG_ENV_IS_IN_FLASH */
48 
49 #if defined(ENV_IS_EMBEDDED) && !defined(CONFIG_BUILD_ENVCRC)
50 # define CONFIG_BUILD_ENVCRC
51 #endif
52 
53 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
54 # define ENV_HEADER_SIZE	(sizeof(uint32_t) + 1)
55 #else
56 # define ENV_HEADER_SIZE	(sizeof(uint32_t))
57 #endif
58 
59 #define ENV_SIZE (CONFIG_ENV_SIZE - ENV_HEADER_SIZE)
60 
61 
62 #ifdef CONFIG_BUILD_ENVCRC
63 # include <environment.h>
64 extern unsigned int env_size;
65 extern env_t environment;
66 #endif	/* CONFIG_BUILD_ENVCRC */
67 
68 extern uint32_t crc32 (uint32_t, const unsigned char *, unsigned int);
69 
70 int main (int argc, char **argv)
71 {
72 #ifdef CONFIG_BUILD_ENVCRC
73 	unsigned char pad = 0x00;
74 	uint32_t crc;
75 	unsigned char *envptr = (unsigned char *)&environment,
76 		*dataptr = envptr + ENV_HEADER_SIZE;
77 	unsigned int datasize = ENV_SIZE;
78 	unsigned int eoe;
79 
80 	if (argv[1] && !strncmp(argv[1], "--binary", 8)) {
81 		int ipad = 0xff;
82 		if (argv[1][8] == '=')
83 			sscanf(argv[1] + 9, "%i", &ipad);
84 		pad = ipad;
85 	}
86 
87 	if (pad) {
88 		/* find the end of env */
89 		for (eoe = 0; eoe < datasize - 1; ++eoe)
90 			if (!dataptr[eoe] && !dataptr[eoe+1]) {
91 				eoe += 2;
92 				break;
93 			}
94 		if (eoe < datasize - 1)
95 			memset(dataptr + eoe, pad, datasize - eoe);
96 	}
97 
98 	crc = crc32 (0, dataptr, datasize);
99 
100 	/* Check if verbose mode is activated passing a parameter to the program */
101 	if (argc > 1) {
102 		if (!strncmp(argv[1], "--binary", 8)) {
103 			int le = (argc > 2 ? !strcmp(argv[2], "le") : 1);
104 			size_t i, start, end, step;
105 			if (le) {
106 				start = 0;
107 				end = ENV_HEADER_SIZE;
108 				step = 1;
109 			} else {
110 				start = ENV_HEADER_SIZE - 1;
111 				end = -1;
112 				step = -1;
113 			}
114 			for (i = start; i != end; i += step)
115 				printf("%c", (crc & (0xFF << (i * 8))) >> (i * 8));
116 			if (fwrite(dataptr, 1, datasize, stdout) != datasize)
117 				fprintf(stderr, "fwrite() failed: %s\n", strerror(errno));
118 		} else {
119 			printf("CRC32 from offset %08X to %08X of environment = %08X\n",
120 				(unsigned int) (dataptr - envptr),
121 				(unsigned int) (dataptr - envptr) + datasize,
122 				crc);
123 		}
124 	} else {
125 		printf ("0x%08X\n", crc);
126 	}
127 #else
128 	printf ("0\n");
129 #endif
130 	return EXIT_SUCCESS;
131 }
132