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