envcrc.c 3.4 KB
Newer Older
wdenk's avatar
wdenk committed
1 2 3 4
/*
 * (C) Copyright 2001
 * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
 *
5
 * SPDX-License-Identifier:	GPL-2.0+
wdenk's avatar
wdenk committed
6 7
 */

8
#include <errno.h>
wdenk's avatar
wdenk committed
9
#include <stdio.h>
10
#include <stdint.h>
wdenk's avatar
wdenk committed
11
#include <stdlib.h>
12
#include <string.h>
wdenk's avatar
wdenk committed
13 14
#include <unistd.h>

15 16 17 18
#ifndef __ASSEMBLY__
#define	__ASSEMBLY__			/* Dirty trick to get only #defines	*/
#endif
#define	__ASM_STUB_PROCESSOR_H__	/* don't include asm/processor.		*/
wdenk's avatar
wdenk committed
19 20 21
#include <config.h>
#undef	__ASSEMBLY__

22
#if defined(CONFIG_ENV_IS_IN_FLASH)
23
# ifndef  CONFIG_ENV_ADDR
24
#  define CONFIG_ENV_ADDR	(CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
wdenk's avatar
wdenk committed
25
# endif
26
# ifndef  CONFIG_ENV_OFFSET
27
#  define CONFIG_ENV_OFFSET (CONFIG_ENV_ADDR - CONFIG_SYS_FLASH_BASE)
wdenk's avatar
wdenk committed
28
# endif
29
# if !defined(CONFIG_ENV_ADDR_REDUND) && defined(CONFIG_ENV_OFFSET_REDUND)
30
#  define CONFIG_ENV_ADDR_REDUND	(CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET_REDUND)
31
# endif
32 33
# ifndef  CONFIG_ENV_SIZE
#  define CONFIG_ENV_SIZE	CONFIG_ENV_SECT_SIZE
wdenk's avatar
wdenk committed
34
# endif
35 36
# if defined(CONFIG_ENV_ADDR_REDUND) && !defined(CONFIG_ENV_SIZE_REDUND)
#  define CONFIG_ENV_SIZE_REDUND	CONFIG_ENV_SIZE
37
# endif
38 39
# if (CONFIG_ENV_ADDR >= CONFIG_SYS_MONITOR_BASE) && \
     ((CONFIG_ENV_ADDR + CONFIG_ENV_SIZE) <= (CONFIG_SYS_MONITOR_BASE + CONFIG_SYS_MONITOR_LEN))
40
#  define ENV_IS_EMBEDDED
41
# endif
42
# if defined(CONFIG_ENV_ADDR_REDUND) || defined(CONFIG_ENV_OFFSET_REDUND)
43
#  define CONFIG_SYS_REDUNDAND_ENVIRONMENT
wdenk's avatar
wdenk committed
44
# endif
45
#endif	/* CONFIG_ENV_IS_IN_FLASH */
wdenk's avatar
wdenk committed
46

47
#if defined(ENV_IS_EMBEDDED) && !defined(CONFIG_BUILD_ENVCRC)
48
# define CONFIG_BUILD_ENVCRC
49 50
#endif

51
#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
52
# define ENV_HEADER_SIZE	(sizeof(uint32_t) + 1)
53
#else
54
# define ENV_HEADER_SIZE	(sizeof(uint32_t))
55 56
#endif

57
#define ENV_SIZE (CONFIG_ENV_SIZE - ENV_HEADER_SIZE)
58 59


60
#ifdef CONFIG_BUILD_ENVCRC
61
# include <environment.h>
wdenk's avatar
wdenk committed
62
extern unsigned int env_size;
63
extern env_t environment;
64
#endif	/* CONFIG_BUILD_ENVCRC */
wdenk's avatar
wdenk committed
65

66 67
extern uint32_t crc32 (uint32_t, const unsigned char *, unsigned int);

wdenk's avatar
wdenk committed
68 69
int main (int argc, char **argv)
{
70
#ifdef CONFIG_BUILD_ENVCRC
71
	unsigned char pad = 0x00;
72
	uint32_t crc;
73
	unsigned char *envptr = (unsigned char *)&environment,
74 75
		*dataptr = envptr + ENV_HEADER_SIZE;
	unsigned int datasize = ENV_SIZE;
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
	unsigned int eoe;

	if (argv[1] && !strncmp(argv[1], "--binary", 8)) {
		int ipad = 0xff;
		if (argv[1][8] == '=')
			sscanf(argv[1] + 9, "%i", &ipad);
		pad = ipad;
	}

	if (pad) {
		/* find the end of env */
		for (eoe = 0; eoe < datasize - 1; ++eoe)
			if (!dataptr[eoe] && !dataptr[eoe+1]) {
				eoe += 2;
				break;
			}
		if (eoe < datasize - 1)
			memset(dataptr + eoe, pad, datasize - eoe);
	}
wdenk's avatar
wdenk committed
95

96
	crc = crc32 (0, dataptr, datasize);
wdenk's avatar
wdenk committed
97

98 99
	/* Check if verbose mode is activated passing a parameter to the program */
	if (argc > 1) {
100 101 102 103 104 105 106 107 108 109 110 111 112 113
		if (!strncmp(argv[1], "--binary", 8)) {
			int le = (argc > 2 ? !strcmp(argv[2], "le") : 1);
			size_t i, start, end, step;
			if (le) {
				start = 0;
				end = ENV_HEADER_SIZE;
				step = 1;
			} else {
				start = ENV_HEADER_SIZE - 1;
				end = -1;
				step = -1;
			}
			for (i = start; i != end; i += step)
				printf("%c", (crc & (0xFF << (i * 8))) >> (i * 8));
114 115
			if (fwrite(dataptr, 1, datasize, stdout) != datasize)
				fprintf(stderr, "fwrite() failed: %s\n", strerror(errno));
116 117 118 119 120 121
		} else {
			printf("CRC32 from offset %08X to %08X of environment = %08X\n",
				(unsigned int) (dataptr - envptr),
				(unsigned int) (dataptr - envptr) + datasize,
				crc);
		}
122 123 124
	} else {
		printf ("0x%08X\n", crc);
	}
125 126 127
#else
	printf ("0\n");
#endif
128
	return EXIT_SUCCESS;
wdenk's avatar
wdenk committed
129
}