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