]> git.ipfire.org Git - thirdparty/u-boot.git/blame - tools/envcrc.c
buildman: Update the TODO items
[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>
3db71108 12#include <u-boot/crc.h>
fe57bb19
WD
13#include <unistd.h>
14
f33f3e07
YS
15#include <linux/kconfig.h>
16
dc17fb6d
WD
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. */
fe57bb19
WD
21#include <config.h>
22#undef __ASSEMBLY__
23
5a1aceb0 24#if defined(CONFIG_ENV_IS_IN_FLASH)
0e8d1586 25# ifndef CONFIG_ENV_ADDR
6d0f6bcf 26# define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
fe57bb19 27# endif
0e8d1586 28# ifndef CONFIG_ENV_OFFSET
6d0f6bcf 29# define CONFIG_ENV_OFFSET (CONFIG_ENV_ADDR - CONFIG_SYS_FLASH_BASE)
fe57bb19 30# endif
0e8d1586 31# if !defined(CONFIG_ENV_ADDR_REDUND) && defined(CONFIG_ENV_OFFSET_REDUND)
6d0f6bcf 32# define CONFIG_ENV_ADDR_REDUND (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET_REDUND)
43d9616c 33# endif
0e8d1586
JCPV
34# ifndef CONFIG_ENV_SIZE
35# define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
fe57bb19 36# endif
a747a7f3
WD
37# if (CONFIG_ENV_ADDR >= CONFIG_SYS_MONITOR_BASE) && \
38 ((CONFIG_ENV_ADDR + CONFIG_ENV_SIZE) <= (CONFIG_SYS_MONITOR_BASE + CONFIG_SYS_MONITOR_LEN))
6f403bad 39# define ENV_IS_EMBEDDED
a747a7f3 40# endif
5a1aceb0 41#endif /* CONFIG_ENV_IS_IN_FLASH */
fe57bb19 42
c3eb3fe4 43#if defined(ENV_IS_EMBEDDED) && !defined(CONFIG_BUILD_ENVCRC)
6f403bad 44# define CONFIG_BUILD_ENVCRC
c3eb3fe4
MF
45#endif
46
6d0f6bcf 47#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
89cdab78 48# define ENV_HEADER_SIZE (sizeof(uint32_t) + 1)
43d9616c 49#else
89cdab78 50# define ENV_HEADER_SIZE (sizeof(uint32_t))
43d9616c
WD
51#endif
52
0e8d1586 53#define ENV_SIZE (CONFIG_ENV_SIZE - ENV_HEADER_SIZE)
43d9616c
WD
54
55
c3eb3fe4 56#ifdef CONFIG_BUILD_ENVCRC
f3998fdc 57# include <env_internal.h>
fe57bb19 58extern unsigned int env_size;
be54ec16 59extern env_t embedded_environment;
c3eb3fe4 60#endif /* CONFIG_BUILD_ENVCRC */
fe57bb19 61
b2ea91ba 62extern uint32_t crc32(uint32_t, const unsigned char *, unsigned int);
73f94ed4 63
fe57bb19
WD
64int main (int argc, char **argv)
65{
c3eb3fe4 66#ifdef CONFIG_BUILD_ENVCRC
837db3d8 67 unsigned char pad = 0x00;
89cdab78 68 uint32_t crc;
be54ec16 69 unsigned char *envptr = (unsigned char *)&embedded_environment,
dc17fb6d
WD
70 *dataptr = envptr + ENV_HEADER_SIZE;
71 unsigned int datasize = ENV_SIZE;
837db3d8
MF
72 unsigned int eoe;
73
74 if (argv[1] && !strncmp(argv[1], "--binary", 8)) {
75 int ipad = 0xff;
76 if (argv[1][8] == '=')
77 sscanf(argv[1] + 9, "%i", &ipad);
78 pad = ipad;
79 }
80
81 if (pad) {
82 /* find the end of env */
83 for (eoe = 0; eoe < datasize - 1; ++eoe)
84 if (!dataptr[eoe] && !dataptr[eoe+1]) {
85 eoe += 2;
86 break;
87 }
88 if (eoe < datasize - 1)
89 memset(dataptr + eoe, pad, datasize - eoe);
90 }
fe57bb19 91
b2ea91ba 92 crc = crc32(0, dataptr, datasize);
fe57bb19 93
dc17fb6d
WD
94 /* Check if verbose mode is activated passing a parameter to the program */
95 if (argc > 1) {
837db3d8
MF
96 if (!strncmp(argv[1], "--binary", 8)) {
97 int le = (argc > 2 ? !strcmp(argv[2], "le") : 1);
98 size_t i, start, end, step;
99 if (le) {
100 start = 0;
101 end = ENV_HEADER_SIZE;
102 step = 1;
103 } else {
104 start = ENV_HEADER_SIZE - 1;
105 end = -1;
106 step = -1;
107 }
108 for (i = start; i != end; i += step)
109 printf("%c", (crc & (0xFF << (i * 8))) >> (i * 8));
f67066b6
MF
110 if (fwrite(dataptr, 1, datasize, stdout) != datasize)
111 fprintf(stderr, "fwrite() failed: %s\n", strerror(errno));
837db3d8
MF
112 } else {
113 printf("CRC32 from offset %08X to %08X of environment = %08X\n",
114 (unsigned int) (dataptr - envptr),
115 (unsigned int) (dataptr - envptr) + datasize,
116 crc);
117 }
dc17fb6d
WD
118 } else {
119 printf ("0x%08X\n", crc);
120 }
a747a7f3
WD
121#else
122 printf ("0\n");
123#endif
dc17fb6d 124 return EXIT_SUCCESS;
fe57bb19 125}