Prepare v2023.10
[platform/kernel/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 <u-boot/crc.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       (CFG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
27 # endif
28 # ifndef  CONFIG_ENV_OFFSET
29 #  define CONFIG_ENV_OFFSET (CONFIG_ENV_ADDR - CFG_SYS_FLASH_BASE)
30 # endif
31 # if !defined(CONFIG_ENV_ADDR_REDUND) && defined(CONFIG_ENV_OFFSET_REDUND)
32 #  define CONFIG_ENV_ADDR_REDUND        (CFG_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 (CONFIG_ENV_ADDR >= CONFIG_SYS_MONITOR_BASE) && \
38      ((CONFIG_ENV_ADDR + CONFIG_ENV_SIZE) <= (CONFIG_SYS_MONITOR_BASE + CONFIG_SYS_MONITOR_LEN))
39 #  define ENV_IS_EMBEDDED
40 # endif
41 #endif  /* CONFIG_ENV_IS_IN_FLASH */
42
43 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
44 # define ENV_HEADER_SIZE        (sizeof(uint32_t) + 1)
45 #else
46 # define ENV_HEADER_SIZE        (sizeof(uint32_t))
47 #endif
48
49 #define ENV_SIZE (CONFIG_ENV_SIZE - ENV_HEADER_SIZE)
50
51
52 #ifdef ENV_IS_EMBEDDED
53 # include <env_internal.h>
54 extern unsigned int env_size;
55 extern env_t embedded_environment;
56 #endif  /* ENV_IS_EMBEDDED */
57
58 extern uint32_t crc32(uint32_t, const unsigned char *, unsigned int);
59
60 int main (int argc, char **argv)
61 {
62 #ifdef ENV_IS_EMBEDDED
63         unsigned char pad = 0x00;
64         uint32_t crc;
65         unsigned char *envptr = (unsigned char *)&embedded_environment,
66                 *dataptr = envptr + ENV_HEADER_SIZE;
67         unsigned int datasize = ENV_SIZE;
68         unsigned int eoe;
69
70         if (argv[1] && !strncmp(argv[1], "--binary", 8)) {
71                 int ipad = 0xff;
72                 if (argv[1][8] == '=')
73                         sscanf(argv[1] + 9, "%i", &ipad);
74                 pad = ipad;
75         }
76
77         if (pad) {
78                 /* find the end of env */
79                 for (eoe = 0; eoe < datasize - 1; ++eoe)
80                         if (!dataptr[eoe] && !dataptr[eoe+1]) {
81                                 eoe += 2;
82                                 break;
83                         }
84                 if (eoe < datasize - 1)
85                         memset(dataptr + eoe, pad, datasize - eoe);
86         }
87
88         crc = crc32(0, dataptr, datasize);
89
90         /* Check if verbose mode is activated passing a parameter to the program */
91         if (argc > 1) {
92                 if (!strncmp(argv[1], "--binary", 8)) {
93                         int le = (argc > 2 ? !strcmp(argv[2], "le") : 1);
94                         size_t i, start, end, step;
95                         if (le) {
96                                 start = 0;
97                                 end = ENV_HEADER_SIZE;
98                                 step = 1;
99                         } else {
100                                 start = ENV_HEADER_SIZE - 1;
101                                 end = -1;
102                                 step = -1;
103                         }
104                         for (i = start; i != end; i += step)
105                                 printf("%c", (crc & (0xFF << (i * 8))) >> (i * 8));
106                         if (fwrite(dataptr, 1, datasize, stdout) != datasize)
107                                 fprintf(stderr, "fwrite() failed: %s\n", strerror(errno));
108                 } else {
109                         printf("CRC32 from offset %08X to %08X of environment = %08X\n",
110                                 (unsigned int) (dataptr - envptr),
111                                 (unsigned int) (dataptr - envptr) + datasize,
112                                 crc);
113                 }
114         } else {
115                 printf ("0x%08X\n", crc);
116         }
117 #else
118         printf ("0\n");
119 #endif
120         return EXIT_SUCCESS;
121 }