blob: a9d9b48c76d407cb2c241127e2678037cc1e8846 [file] [log] [blame]
wdenkfe57bb12002-09-18 13:23:15 +00001/*
2 * (C) Copyright 2001
3 * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenkfe57bb12002-09-18 13:23:15 +00006 */
7
Mike Frysingerf67066b2009-10-18 20:43:14 -04008#include <errno.h>
wdenkfe57bb12002-09-18 13:23:15 +00009#include <stdio.h>
Mike Frysinger89cdab72008-03-31 11:02:01 -040010#include <stdint.h>
wdenkfe57bb12002-09-18 13:23:15 +000011#include <stdlib.h>
Mike Frysinger837db3d2009-05-06 08:41:45 -040012#include <string.h>
wdenkfe57bb12002-09-18 13:23:15 +000013#include <unistd.h>
14
Wolfgang Denkdc17fb62005-08-03 22:32:02 +020015#ifndef __ASSEMBLY__
16#define __ASSEMBLY__ /* Dirty trick to get only #defines */
17#endif
18#define __ASM_STUB_PROCESSOR_H__ /* don't include asm/processor. */
wdenkfe57bb12002-09-18 13:23:15 +000019#include <config.h>
20#undef __ASSEMBLY__
21
Jean-Christophe PLAGNIOL-VILLARD5a1aceb2008-09-10 22:48:04 +020022#if defined(CONFIG_ENV_IS_IN_FLASH)
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020023# ifndef CONFIG_ENV_ADDR
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020024# define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
wdenkfe57bb12002-09-18 13:23:15 +000025# endif
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020026# ifndef CONFIG_ENV_OFFSET
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020027# define CONFIG_ENV_OFFSET (CONFIG_ENV_ADDR - CONFIG_SYS_FLASH_BASE)
wdenkfe57bb12002-09-18 13:23:15 +000028# endif
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020029# if !defined(CONFIG_ENV_ADDR_REDUND) && defined(CONFIG_ENV_OFFSET_REDUND)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020030# define CONFIG_ENV_ADDR_REDUND (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET_REDUND)
wdenk43d96162003-03-06 00:02:04 +000031# endif
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020032# ifndef CONFIG_ENV_SIZE
33# define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
wdenkfe57bb12002-09-18 13:23:15 +000034# endif
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020035# if defined(CONFIG_ENV_ADDR_REDUND) && !defined(CONFIG_ENV_SIZE_REDUND)
36# define CONFIG_ENV_SIZE_REDUND CONFIG_ENV_SIZE
wdenk43d96162003-03-06 00:02:04 +000037# endif
Wolfgang Denka747a7f2009-10-27 00:03:32 +010038# if (CONFIG_ENV_ADDR >= CONFIG_SYS_MONITOR_BASE) && \
39 ((CONFIG_ENV_ADDR + CONFIG_ENV_SIZE) <= (CONFIG_SYS_MONITOR_BASE + CONFIG_SYS_MONITOR_LEN))
Igor Grinberg6f403ba2011-12-25 01:42:57 +000040# define ENV_IS_EMBEDDED
Wolfgang Denka747a7f2009-10-27 00:03:32 +010041# endif
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020042# if defined(CONFIG_ENV_ADDR_REDUND) || defined(CONFIG_ENV_OFFSET_REDUND)
Igor Grinberg6f403ba2011-12-25 01:42:57 +000043# define CONFIG_SYS_REDUNDAND_ENVIRONMENT
wdenkfe57bb12002-09-18 13:23:15 +000044# endif
Jean-Christophe PLAGNIOL-VILLARD5a1aceb2008-09-10 22:48:04 +020045#endif /* CONFIG_ENV_IS_IN_FLASH */
wdenkfe57bb12002-09-18 13:23:15 +000046
Mike Frysingerc3eb3fe2011-07-08 10:44:25 +000047#if defined(ENV_IS_EMBEDDED) && !defined(CONFIG_BUILD_ENVCRC)
Igor Grinberg6f403ba2011-12-25 01:42:57 +000048# define CONFIG_BUILD_ENVCRC
Mike Frysingerc3eb3fe2011-07-08 10:44:25 +000049#endif
50
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020051#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
Mike Frysinger89cdab72008-03-31 11:02:01 -040052# define ENV_HEADER_SIZE (sizeof(uint32_t) + 1)
wdenk43d96162003-03-06 00:02:04 +000053#else
Mike Frysinger89cdab72008-03-31 11:02:01 -040054# define ENV_HEADER_SIZE (sizeof(uint32_t))
wdenk43d96162003-03-06 00:02:04 +000055#endif
56
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020057#define ENV_SIZE (CONFIG_ENV_SIZE - ENV_HEADER_SIZE)
wdenk43d96162003-03-06 00:02:04 +000058
59
Mike Frysingerc3eb3fe2011-07-08 10:44:25 +000060#ifdef CONFIG_BUILD_ENVCRC
Igor Grinberg73f94ed2011-11-27 21:57:38 +000061# include <environment.h>
wdenkfe57bb12002-09-18 13:23:15 +000062extern unsigned int env_size;
Igor Grinberg994bc672011-11-17 06:07:23 +000063extern env_t environment;
Mike Frysingerc3eb3fe2011-07-08 10:44:25 +000064#endif /* CONFIG_BUILD_ENVCRC */
wdenkfe57bb12002-09-18 13:23:15 +000065
Igor Grinberg73f94ed2011-11-27 21:57:38 +000066extern uint32_t crc32 (uint32_t, const unsigned char *, unsigned int);
67
wdenkfe57bb12002-09-18 13:23:15 +000068int main (int argc, char **argv)
69{
Mike Frysingerc3eb3fe2011-07-08 10:44:25 +000070#ifdef CONFIG_BUILD_ENVCRC
Mike Frysinger837db3d2009-05-06 08:41:45 -040071 unsigned char pad = 0x00;
Mike Frysinger89cdab72008-03-31 11:02:01 -040072 uint32_t crc;
Igor Grinberg994bc672011-11-17 06:07:23 +000073 unsigned char *envptr = (unsigned char *)&environment,
Wolfgang Denkdc17fb62005-08-03 22:32:02 +020074 *dataptr = envptr + ENV_HEADER_SIZE;
75 unsigned int datasize = ENV_SIZE;
Mike Frysinger837db3d2009-05-06 08:41:45 -040076 unsigned int eoe;
77
78 if (argv[1] && !strncmp(argv[1], "--binary", 8)) {
79 int ipad = 0xff;
80 if (argv[1][8] == '=')
81 sscanf(argv[1] + 9, "%i", &ipad);
82 pad = ipad;
83 }
84
85 if (pad) {
86 /* find the end of env */
87 for (eoe = 0; eoe < datasize - 1; ++eoe)
88 if (!dataptr[eoe] && !dataptr[eoe+1]) {
89 eoe += 2;
90 break;
91 }
92 if (eoe < datasize - 1)
93 memset(dataptr + eoe, pad, datasize - eoe);
94 }
wdenkfe57bb12002-09-18 13:23:15 +000095
Wolfgang Denkdc17fb62005-08-03 22:32:02 +020096 crc = crc32 (0, dataptr, datasize);
wdenkfe57bb12002-09-18 13:23:15 +000097
Wolfgang Denkdc17fb62005-08-03 22:32:02 +020098 /* Check if verbose mode is activated passing a parameter to the program */
99 if (argc > 1) {
Mike Frysinger837db3d2009-05-06 08:41:45 -0400100 if (!strncmp(argv[1], "--binary", 8)) {
101 int le = (argc > 2 ? !strcmp(argv[2], "le") : 1);
102 size_t i, start, end, step;
103 if (le) {
104 start = 0;
105 end = ENV_HEADER_SIZE;
106 step = 1;
107 } else {
108 start = ENV_HEADER_SIZE - 1;
109 end = -1;
110 step = -1;
111 }
112 for (i = start; i != end; i += step)
113 printf("%c", (crc & (0xFF << (i * 8))) >> (i * 8));
Mike Frysingerf67066b2009-10-18 20:43:14 -0400114 if (fwrite(dataptr, 1, datasize, stdout) != datasize)
115 fprintf(stderr, "fwrite() failed: %s\n", strerror(errno));
Mike Frysinger837db3d2009-05-06 08:41:45 -0400116 } else {
117 printf("CRC32 from offset %08X to %08X of environment = %08X\n",
118 (unsigned int) (dataptr - envptr),
119 (unsigned int) (dataptr - envptr) + datasize,
120 crc);
121 }
Wolfgang Denkdc17fb62005-08-03 22:32:02 +0200122 } else {
123 printf ("0x%08X\n", crc);
124 }
Wolfgang Denka747a7f2009-10-27 00:03:32 +0100125#else
126 printf ("0\n");
127#endif
Wolfgang Denkdc17fb62005-08-03 22:32:02 +0200128 return EXIT_SUCCESS;
wdenkfe57bb12002-09-18 13:23:15 +0000129}