xref: /openbmc/u-boot/tools/envcrc.c (revision 15855700)
1 /*
2  * (C) Copyright 2001
3  * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23 
24 #include <errno.h>
25 #include <stdio.h>
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 
31 #ifndef __ASSEMBLY__
32 #define	__ASSEMBLY__			/* Dirty trick to get only #defines	*/
33 #endif
34 #define	__ASM_STUB_PROCESSOR_H__	/* don't include asm/processor.		*/
35 #include <config.h>
36 #undef	__ASSEMBLY__
37 
38 #if defined(CONFIG_ENV_IS_IN_FLASH)
39 # ifndef  CONFIG_ENV_ADDR
40 #  define CONFIG_ENV_ADDR	(CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
41 # endif
42 # ifndef  CONFIG_ENV_OFFSET
43 #  define CONFIG_ENV_OFFSET (CONFIG_ENV_ADDR - CONFIG_SYS_FLASH_BASE)
44 # endif
45 # if !defined(CONFIG_ENV_ADDR_REDUND) && defined(CONFIG_ENV_OFFSET_REDUND)
46 #  define CONFIG_ENV_ADDR_REDUND	(CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET_REDUND)
47 # endif
48 # ifndef  CONFIG_ENV_SIZE
49 #  define CONFIG_ENV_SIZE	CONFIG_ENV_SECT_SIZE
50 # endif
51 # if defined(CONFIG_ENV_ADDR_REDUND) && !defined(CONFIG_ENV_SIZE_REDUND)
52 #  define CONFIG_ENV_SIZE_REDUND	CONFIG_ENV_SIZE
53 # endif
54 # if (CONFIG_ENV_ADDR >= CONFIG_SYS_MONITOR_BASE) && \
55      ((CONFIG_ENV_ADDR + CONFIG_ENV_SIZE) <= (CONFIG_SYS_MONITOR_BASE + CONFIG_SYS_MONITOR_LEN))
56 #  define ENV_IS_EMBEDDED	1
57 # endif
58 # if defined(CONFIG_ENV_ADDR_REDUND) || defined(CONFIG_ENV_OFFSET_REDUND)
59 #  define CONFIG_SYS_REDUNDAND_ENVIRONMENT	1
60 # endif
61 #endif	/* CONFIG_ENV_IS_IN_FLASH */
62 
63 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
64 # define ENV_HEADER_SIZE	(sizeof(uint32_t) + 1)
65 #else
66 # define ENV_HEADER_SIZE	(sizeof(uint32_t))
67 #endif
68 
69 #define ENV_SIZE (CONFIG_ENV_SIZE - ENV_HEADER_SIZE)
70 
71 
72 extern uint32_t crc32 (uint32_t, const unsigned char *, unsigned int);
73 
74 #ifdef	ENV_IS_EMBEDDED
75 extern unsigned int env_size;
76 extern unsigned char environment;
77 #endif	/* ENV_IS_EMBEDDED */
78 
79 int main (int argc, char **argv)
80 {
81 #ifdef	ENV_IS_EMBEDDED
82 	unsigned char pad = 0x00;
83 	uint32_t crc;
84 	unsigned char *envptr = &environment,
85 		*dataptr = envptr + ENV_HEADER_SIZE;
86 	unsigned int datasize = ENV_SIZE;
87 	unsigned int eoe;
88 
89 	if (argv[1] && !strncmp(argv[1], "--binary", 8)) {
90 		int ipad = 0xff;
91 		if (argv[1][8] == '=')
92 			sscanf(argv[1] + 9, "%i", &ipad);
93 		pad = ipad;
94 	}
95 
96 	if (pad) {
97 		/* find the end of env */
98 		for (eoe = 0; eoe < datasize - 1; ++eoe)
99 			if (!dataptr[eoe] && !dataptr[eoe+1]) {
100 				eoe += 2;
101 				break;
102 			}
103 		if (eoe < datasize - 1)
104 			memset(dataptr + eoe, pad, datasize - eoe);
105 	}
106 
107 	crc = crc32 (0, dataptr, datasize);
108 
109 	/* Check if verbose mode is activated passing a parameter to the program */
110 	if (argc > 1) {
111 		if (!strncmp(argv[1], "--binary", 8)) {
112 			int le = (argc > 2 ? !strcmp(argv[2], "le") : 1);
113 			size_t i, start, end, step;
114 			if (le) {
115 				start = 0;
116 				end = ENV_HEADER_SIZE;
117 				step = 1;
118 			} else {
119 				start = ENV_HEADER_SIZE - 1;
120 				end = -1;
121 				step = -1;
122 			}
123 			for (i = start; i != end; i += step)
124 				printf("%c", (crc & (0xFF << (i * 8))) >> (i * 8));
125 			if (fwrite(dataptr, 1, datasize, stdout) != datasize)
126 				fprintf(stderr, "fwrite() failed: %s\n", strerror(errno));
127 		} else {
128 			printf("CRC32 from offset %08X to %08X of environment = %08X\n",
129 				(unsigned int) (dataptr - envptr),
130 				(unsigned int) (dataptr - envptr) + datasize,
131 				crc);
132 		}
133 	} else {
134 		printf ("0x%08X\n", crc);
135 	}
136 #else
137 	printf ("0\n");
138 #endif
139 	return EXIT_SUCCESS;
140 }
141